iceland_smc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/firmware.h>
  24. #include "drmP.h"
  25. #include "amdgpu.h"
  26. #include "ppsmc.h"
  27. #include "iceland_smumgr.h"
  28. #include "smu_ucode_xfer_vi.h"
  29. #include "amdgpu_ucode.h"
  30. #include "smu/smu_7_1_1_d.h"
  31. #include "smu/smu_7_1_1_sh_mask.h"
  32. #define ICELAND_SMC_SIZE 0x20000
  33. static int iceland_set_smc_sram_address(struct amdgpu_device *adev,
  34. uint32_t smc_address, uint32_t limit)
  35. {
  36. uint32_t val;
  37. if (smc_address & 3)
  38. return -EINVAL;
  39. if ((smc_address + 3) > limit)
  40. return -EINVAL;
  41. WREG32(mmSMC_IND_INDEX_0, smc_address);
  42. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  43. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
  44. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  45. return 0;
  46. }
  47. static int iceland_copy_bytes_to_smc(struct amdgpu_device *adev,
  48. uint32_t smc_start_address,
  49. const uint8_t *src,
  50. uint32_t byte_count, uint32_t limit)
  51. {
  52. uint32_t addr;
  53. uint32_t data, orig_data;
  54. int result = 0;
  55. uint32_t extra_shift;
  56. unsigned long flags;
  57. if (smc_start_address & 3)
  58. return -EINVAL;
  59. if ((smc_start_address + byte_count) > limit)
  60. return -EINVAL;
  61. addr = smc_start_address;
  62. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  63. while (byte_count >= 4) {
  64. /* Bytes are written into the SMC addres space with the MSB first */
  65. data = (src[0] << 24) + (src[1] << 16) + (src[2] << 8) + src[3];
  66. result = iceland_set_smc_sram_address(adev, addr, limit);
  67. if (result)
  68. goto out;
  69. WREG32(mmSMC_IND_DATA_0, data);
  70. src += 4;
  71. byte_count -= 4;
  72. addr += 4;
  73. }
  74. if (0 != byte_count) {
  75. /* Now write odd bytes left, do a read modify write cycle */
  76. data = 0;
  77. result = iceland_set_smc_sram_address(adev, addr, limit);
  78. if (result)
  79. goto out;
  80. orig_data = RREG32(mmSMC_IND_DATA_0);
  81. extra_shift = 8 * (4 - byte_count);
  82. while (byte_count > 0) {
  83. data = (data << 8) + *src++;
  84. byte_count--;
  85. }
  86. data <<= extra_shift;
  87. data |= (orig_data & ~((~0UL) << extra_shift));
  88. result = iceland_set_smc_sram_address(adev, addr, limit);
  89. if (result)
  90. goto out;
  91. WREG32(mmSMC_IND_DATA_0, data);
  92. }
  93. out:
  94. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  95. return result;
  96. }
  97. void iceland_start_smc(struct amdgpu_device *adev)
  98. {
  99. uint32_t val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  100. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  101. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  102. }
  103. void iceland_reset_smc(struct amdgpu_device *adev)
  104. {
  105. uint32_t val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  106. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  107. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  108. }
  109. static int iceland_program_jump_on_start(struct amdgpu_device *adev)
  110. {
  111. static unsigned char data[] = {0xE0, 0x00, 0x80, 0x40};
  112. iceland_copy_bytes_to_smc(adev, 0x0, data, 4, sizeof(data)+1);
  113. return 0;
  114. }
  115. void iceland_stop_smc_clock(struct amdgpu_device *adev)
  116. {
  117. uint32_t val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  118. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 1);
  119. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  120. }
  121. void iceland_start_smc_clock(struct amdgpu_device *adev)
  122. {
  123. uint32_t val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  124. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
  125. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  126. }
  127. static bool iceland_is_smc_ram_running(struct amdgpu_device *adev)
  128. {
  129. uint32_t val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  130. val = REG_GET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable);
  131. return ((0 == val) && (0x20100 <= RREG32_SMC(ixSMC_PC_C)));
  132. }
  133. static int wait_smu_response(struct amdgpu_device *adev)
  134. {
  135. int i;
  136. uint32_t val;
  137. for (i = 0; i < adev->usec_timeout; i++) {
  138. val = RREG32(mmSMC_RESP_0);
  139. if (REG_GET_FIELD(val, SMC_RESP_0, SMC_RESP))
  140. break;
  141. udelay(1);
  142. }
  143. if (i == adev->usec_timeout)
  144. return -EINVAL;
  145. return 0;
  146. }
  147. static int iceland_send_msg_to_smc(struct amdgpu_device *adev, PPSMC_Msg msg)
  148. {
  149. if (!iceland_is_smc_ram_running(adev))
  150. return -EINVAL;
  151. if (wait_smu_response(adev)) {
  152. DRM_ERROR("Failed to send previous message\n");
  153. return -EINVAL;
  154. }
  155. WREG32(mmSMC_MESSAGE_0, msg);
  156. if (wait_smu_response(adev)) {
  157. DRM_ERROR("Failed to send message\n");
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. static int iceland_send_msg_to_smc_without_waiting(struct amdgpu_device *adev,
  163. PPSMC_Msg msg)
  164. {
  165. if (!iceland_is_smc_ram_running(adev))
  166. return -EINVAL;;
  167. if (wait_smu_response(adev)) {
  168. DRM_ERROR("Failed to send previous message\n");
  169. return -EINVAL;
  170. }
  171. WREG32(mmSMC_MESSAGE_0, msg);
  172. return 0;
  173. }
  174. static int iceland_send_msg_to_smc_with_parameter(struct amdgpu_device *adev,
  175. PPSMC_Msg msg,
  176. uint32_t parameter)
  177. {
  178. WREG32(mmSMC_MSG_ARG_0, parameter);
  179. return iceland_send_msg_to_smc(adev, msg);
  180. }
  181. static int iceland_send_msg_to_smc_with_parameter_without_waiting(
  182. struct amdgpu_device *adev,
  183. PPSMC_Msg msg, uint32_t parameter)
  184. {
  185. WREG32(mmSMC_MSG_ARG_0, parameter);
  186. return iceland_send_msg_to_smc_without_waiting(adev, msg);
  187. }
  188. #if 0 /* not used yet */
  189. static int iceland_wait_for_smc_inactive(struct amdgpu_device *adev)
  190. {
  191. int i;
  192. uint32_t val;
  193. if (!iceland_is_smc_ram_running(adev))
  194. return -EINVAL;
  195. for (i = 0; i < adev->usec_timeout; i++) {
  196. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  197. if (REG_GET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, cken) == 0)
  198. break;
  199. udelay(1);
  200. }
  201. if (i == adev->usec_timeout)
  202. return -EINVAL;
  203. return 0;
  204. }
  205. #endif
  206. static int iceland_smu_upload_firmware_image(struct amdgpu_device *adev)
  207. {
  208. const struct smc_firmware_header_v1_0 *hdr;
  209. uint32_t ucode_size;
  210. uint32_t ucode_start_address;
  211. const uint8_t *src;
  212. uint32_t val;
  213. uint32_t byte_count;
  214. uint32_t data;
  215. unsigned long flags;
  216. int i;
  217. if (!adev->pm.fw)
  218. return -EINVAL;
  219. hdr = (const struct smc_firmware_header_v1_0 *)adev->pm.fw->data;
  220. amdgpu_ucode_print_smc_hdr(&hdr->header);
  221. adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
  222. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  223. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  224. src = (const uint8_t *)
  225. (adev->pm.fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  226. if (ucode_size & 3) {
  227. DRM_ERROR("SMC ucode is not 4 bytes aligned\n");
  228. return -EINVAL;
  229. }
  230. if (ucode_size > ICELAND_SMC_SIZE) {
  231. DRM_ERROR("SMC address is beyond the SMC RAM area\n");
  232. return -EINVAL;
  233. }
  234. for (i = 0; i < adev->usec_timeout; i++) {
  235. val = RREG32_SMC(ixRCU_UC_EVENTS);
  236. if (REG_GET_FIELD(val, RCU_UC_EVENTS, boot_seq_done) == 0)
  237. break;
  238. udelay(1);
  239. }
  240. val = RREG32_SMC(ixSMC_SYSCON_MISC_CNTL);
  241. WREG32_SMC(ixSMC_SYSCON_MISC_CNTL, val | 1);
  242. iceland_stop_smc_clock(adev);
  243. iceland_reset_smc(adev);
  244. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  245. WREG32(mmSMC_IND_INDEX_0, ucode_start_address);
  246. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  247. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 1);
  248. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  249. byte_count = ucode_size;
  250. while (byte_count >= 4) {
  251. data = (src[0] << 24) + (src[1] << 16) + (src[2] << 8) + src[3];
  252. WREG32(mmSMC_IND_DATA_0, data);
  253. src += 4;
  254. byte_count -= 4;
  255. }
  256. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  257. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
  258. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  259. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  260. return 0;
  261. }
  262. #if 0 /* not used yet */
  263. static int iceland_read_smc_sram_dword(struct amdgpu_device *adev,
  264. uint32_t smc_address,
  265. uint32_t *value,
  266. uint32_t limit)
  267. {
  268. int result;
  269. unsigned long flags;
  270. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  271. result = iceland_set_smc_sram_address(adev, smc_address, limit);
  272. if (result == 0)
  273. *value = RREG32(mmSMC_IND_DATA_0);
  274. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  275. return result;
  276. }
  277. static int iceland_write_smc_sram_dword(struct amdgpu_device *adev,
  278. uint32_t smc_address,
  279. uint32_t value,
  280. uint32_t limit)
  281. {
  282. int result;
  283. unsigned long flags;
  284. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  285. result = iceland_set_smc_sram_address(adev, smc_address, limit);
  286. if (result == 0)
  287. WREG32(mmSMC_IND_DATA_0, value);
  288. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  289. return result;
  290. }
  291. static int iceland_smu_stop_smc(struct amdgpu_device *adev)
  292. {
  293. iceland_reset_smc(adev);
  294. iceland_stop_smc_clock(adev);
  295. return 0;
  296. }
  297. #endif
  298. static int iceland_smu_start_smc(struct amdgpu_device *adev)
  299. {
  300. int i;
  301. uint32_t val;
  302. iceland_program_jump_on_start(adev);
  303. iceland_start_smc_clock(adev);
  304. iceland_start_smc(adev);
  305. for (i = 0; i < adev->usec_timeout; i++) {
  306. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  307. if (REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED) == 1)
  308. break;
  309. udelay(1);
  310. }
  311. return 0;
  312. }
  313. static enum AMDGPU_UCODE_ID iceland_convert_fw_type(uint32_t fw_type)
  314. {
  315. switch (fw_type) {
  316. case UCODE_ID_SDMA0:
  317. return AMDGPU_UCODE_ID_SDMA0;
  318. case UCODE_ID_SDMA1:
  319. return AMDGPU_UCODE_ID_SDMA1;
  320. case UCODE_ID_CP_CE:
  321. return AMDGPU_UCODE_ID_CP_CE;
  322. case UCODE_ID_CP_PFP:
  323. return AMDGPU_UCODE_ID_CP_PFP;
  324. case UCODE_ID_CP_ME:
  325. return AMDGPU_UCODE_ID_CP_ME;
  326. case UCODE_ID_CP_MEC:
  327. case UCODE_ID_CP_MEC_JT1:
  328. return AMDGPU_UCODE_ID_CP_MEC1;
  329. case UCODE_ID_CP_MEC_JT2:
  330. return AMDGPU_UCODE_ID_CP_MEC2;
  331. case UCODE_ID_RLC_G:
  332. return AMDGPU_UCODE_ID_RLC_G;
  333. default:
  334. DRM_ERROR("ucode type is out of range!\n");
  335. return AMDGPU_UCODE_ID_MAXIMUM;
  336. }
  337. }
  338. static uint32_t iceland_smu_get_mask_for_fw_type(uint32_t fw_type)
  339. {
  340. switch (fw_type) {
  341. case AMDGPU_UCODE_ID_SDMA0:
  342. return UCODE_ID_SDMA0_MASK;
  343. case AMDGPU_UCODE_ID_SDMA1:
  344. return UCODE_ID_SDMA1_MASK;
  345. case AMDGPU_UCODE_ID_CP_CE:
  346. return UCODE_ID_CP_CE_MASK;
  347. case AMDGPU_UCODE_ID_CP_PFP:
  348. return UCODE_ID_CP_PFP_MASK;
  349. case AMDGPU_UCODE_ID_CP_ME:
  350. return UCODE_ID_CP_ME_MASK;
  351. case AMDGPU_UCODE_ID_CP_MEC1:
  352. return UCODE_ID_CP_MEC_MASK | UCODE_ID_CP_MEC_JT1_MASK;
  353. case AMDGPU_UCODE_ID_CP_MEC2:
  354. return UCODE_ID_CP_MEC_MASK;
  355. case AMDGPU_UCODE_ID_RLC_G:
  356. return UCODE_ID_RLC_G_MASK;
  357. default:
  358. DRM_ERROR("ucode type is out of range!\n");
  359. return 0;
  360. }
  361. }
  362. static int iceland_smu_populate_single_firmware_entry(struct amdgpu_device *adev,
  363. uint32_t fw_type,
  364. struct SMU_Entry *entry)
  365. {
  366. enum AMDGPU_UCODE_ID id = iceland_convert_fw_type(fw_type);
  367. struct amdgpu_firmware_info *ucode = &adev->firmware.ucode[id];
  368. const struct gfx_firmware_header_v1_0 *header = NULL;
  369. uint64_t gpu_addr;
  370. uint32_t data_size;
  371. if (ucode->fw == NULL)
  372. return -EINVAL;
  373. gpu_addr = ucode->mc_addr;
  374. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  375. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  376. entry->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  377. entry->id = (uint16_t)fw_type;
  378. entry->image_addr_high = upper_32_bits(gpu_addr);
  379. entry->image_addr_low = lower_32_bits(gpu_addr);
  380. entry->meta_data_addr_high = 0;
  381. entry->meta_data_addr_low = 0;
  382. entry->data_size_byte = data_size;
  383. entry->num_register_entries = 0;
  384. entry->flags = 0;
  385. return 0;
  386. }
  387. static int iceland_smu_request_load_fw(struct amdgpu_device *adev)
  388. {
  389. struct iceland_smu_private_data *private = (struct iceland_smu_private_data *)adev->smu.priv;
  390. struct SMU_DRAMData_TOC *toc;
  391. uint32_t fw_to_load;
  392. toc = (struct SMU_DRAMData_TOC *)private->header;
  393. toc->num_entries = 0;
  394. toc->structure_version = 1;
  395. if (!adev->firmware.smu_load)
  396. return 0;
  397. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_RLC_G,
  398. &toc->entry[toc->num_entries++])) {
  399. DRM_ERROR("Failed to get firmware entry for RLC\n");
  400. return -EINVAL;
  401. }
  402. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_CE,
  403. &toc->entry[toc->num_entries++])) {
  404. DRM_ERROR("Failed to get firmware entry for CE\n");
  405. return -EINVAL;
  406. }
  407. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_PFP,
  408. &toc->entry[toc->num_entries++])) {
  409. DRM_ERROR("Failed to get firmware entry for PFP\n");
  410. return -EINVAL;
  411. }
  412. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_ME,
  413. &toc->entry[toc->num_entries++])) {
  414. DRM_ERROR("Failed to get firmware entry for ME\n");
  415. return -EINVAL;
  416. }
  417. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC,
  418. &toc->entry[toc->num_entries++])) {
  419. DRM_ERROR("Failed to get firmware entry for MEC\n");
  420. return -EINVAL;
  421. }
  422. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT1,
  423. &toc->entry[toc->num_entries++])) {
  424. DRM_ERROR("Failed to get firmware entry for MEC_JT1\n");
  425. return -EINVAL;
  426. }
  427. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA0,
  428. &toc->entry[toc->num_entries++])) {
  429. DRM_ERROR("Failed to get firmware entry for SDMA0\n");
  430. return -EINVAL;
  431. }
  432. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA1,
  433. &toc->entry[toc->num_entries++])) {
  434. DRM_ERROR("Failed to get firmware entry for SDMA1\n");
  435. return -EINVAL;
  436. }
  437. iceland_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_HI, private->header_addr_high);
  438. iceland_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_LO, private->header_addr_low);
  439. fw_to_load = UCODE_ID_RLC_G_MASK |
  440. UCODE_ID_SDMA0_MASK |
  441. UCODE_ID_SDMA1_MASK |
  442. UCODE_ID_CP_CE_MASK |
  443. UCODE_ID_CP_ME_MASK |
  444. UCODE_ID_CP_PFP_MASK |
  445. UCODE_ID_CP_MEC_MASK |
  446. UCODE_ID_CP_MEC_JT1_MASK;
  447. if (iceland_send_msg_to_smc_with_parameter_without_waiting(adev, PPSMC_MSG_LoadUcodes, fw_to_load)) {
  448. DRM_ERROR("Fail to request SMU load ucode\n");
  449. return -EINVAL;
  450. }
  451. return 0;
  452. }
  453. static int iceland_smu_check_fw_load_finish(struct amdgpu_device *adev,
  454. uint32_t fw_type)
  455. {
  456. uint32_t fw_mask = iceland_smu_get_mask_for_fw_type(fw_type);
  457. int i;
  458. for (i = 0; i < adev->usec_timeout; i++) {
  459. if (fw_mask == (RREG32_SMC(ixSOFT_REGISTERS_TABLE_27) & fw_mask))
  460. break;
  461. udelay(1);
  462. }
  463. if (i == adev->usec_timeout) {
  464. DRM_ERROR("check firmware loading failed\n");
  465. return -EINVAL;
  466. }
  467. return 0;
  468. }
  469. int iceland_smu_start(struct amdgpu_device *adev)
  470. {
  471. int result;
  472. result = iceland_smu_upload_firmware_image(adev);
  473. if (result)
  474. return result;
  475. result = iceland_smu_start_smc(adev);
  476. if (result)
  477. return result;
  478. return iceland_smu_request_load_fw(adev);
  479. }
  480. static const struct amdgpu_smumgr_funcs iceland_smumgr_funcs = {
  481. .check_fw_load_finish = iceland_smu_check_fw_load_finish,
  482. .request_smu_load_fw = NULL,
  483. .request_smu_specific_fw = NULL,
  484. };
  485. int iceland_smu_init(struct amdgpu_device *adev)
  486. {
  487. struct iceland_smu_private_data *private;
  488. uint32_t image_size = ((sizeof(struct SMU_DRAMData_TOC) / 4096) + 1) * 4096;
  489. struct amdgpu_bo **toc_buf = &adev->smu.toc_buf;
  490. uint64_t mc_addr;
  491. void *toc_buf_ptr;
  492. int ret;
  493. private = kzalloc(sizeof(struct iceland_smu_private_data), GFP_KERNEL);
  494. if (NULL == private)
  495. return -ENOMEM;
  496. /* allocate firmware buffers */
  497. if (adev->firmware.smu_load)
  498. amdgpu_ucode_init_bo(adev);
  499. adev->smu.priv = private;
  500. adev->smu.fw_flags = 0;
  501. /* Allocate FW image data structure and header buffer */
  502. ret = amdgpu_bo_create(adev, image_size, PAGE_SIZE,
  503. true, AMDGPU_GEM_DOMAIN_VRAM,
  504. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  505. NULL, NULL, toc_buf);
  506. if (ret) {
  507. DRM_ERROR("Failed to allocate memory for TOC buffer\n");
  508. return -ENOMEM;
  509. }
  510. /* Retrieve GPU address for header buffer and internal buffer */
  511. ret = amdgpu_bo_reserve(adev->smu.toc_buf, false);
  512. if (ret) {
  513. amdgpu_bo_unref(&adev->smu.toc_buf);
  514. DRM_ERROR("Failed to reserve the TOC buffer\n");
  515. return -EINVAL;
  516. }
  517. ret = amdgpu_bo_pin(adev->smu.toc_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  518. if (ret) {
  519. amdgpu_bo_unreserve(adev->smu.toc_buf);
  520. amdgpu_bo_unref(&adev->smu.toc_buf);
  521. DRM_ERROR("Failed to pin the TOC buffer\n");
  522. return -EINVAL;
  523. }
  524. ret = amdgpu_bo_kmap(*toc_buf, &toc_buf_ptr);
  525. if (ret) {
  526. amdgpu_bo_unreserve(adev->smu.toc_buf);
  527. amdgpu_bo_unref(&adev->smu.toc_buf);
  528. DRM_ERROR("Failed to map the TOC buffer\n");
  529. return -EINVAL;
  530. }
  531. amdgpu_bo_unreserve(adev->smu.toc_buf);
  532. private->header_addr_low = lower_32_bits(mc_addr);
  533. private->header_addr_high = upper_32_bits(mc_addr);
  534. private->header = toc_buf_ptr;
  535. adev->smu.smumgr_funcs = &iceland_smumgr_funcs;
  536. return 0;
  537. }
  538. int iceland_smu_fini(struct amdgpu_device *adev)
  539. {
  540. amdgpu_bo_unref(&adev->smu.toc_buf);
  541. kfree(adev->smu.priv);
  542. adev->smu.priv = NULL;
  543. if (adev->firmware.fw_buf)
  544. amdgpu_ucode_fini_bo(adev);
  545. return 0;
  546. }