iceland_smc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. /* Skip SMC ucode loading on SR-IOV capable boards.
  220. * vbios does this for us in asic_init in that case.
  221. */
  222. if (adev->virtualization.supports_sr_iov)
  223. return 0;
  224. hdr = (const struct smc_firmware_header_v1_0 *)adev->pm.fw->data;
  225. amdgpu_ucode_print_smc_hdr(&hdr->header);
  226. adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
  227. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  228. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  229. src = (const uint8_t *)
  230. (adev->pm.fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  231. if (ucode_size & 3) {
  232. DRM_ERROR("SMC ucode is not 4 bytes aligned\n");
  233. return -EINVAL;
  234. }
  235. if (ucode_size > ICELAND_SMC_SIZE) {
  236. DRM_ERROR("SMC address is beyond the SMC RAM area\n");
  237. return -EINVAL;
  238. }
  239. for (i = 0; i < adev->usec_timeout; i++) {
  240. val = RREG32_SMC(ixRCU_UC_EVENTS);
  241. if (REG_GET_FIELD(val, RCU_UC_EVENTS, boot_seq_done) == 0)
  242. break;
  243. udelay(1);
  244. }
  245. val = RREG32_SMC(ixSMC_SYSCON_MISC_CNTL);
  246. WREG32_SMC(ixSMC_SYSCON_MISC_CNTL, val | 1);
  247. iceland_stop_smc_clock(adev);
  248. iceland_reset_smc(adev);
  249. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  250. WREG32(mmSMC_IND_INDEX_0, ucode_start_address);
  251. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  252. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 1);
  253. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  254. byte_count = ucode_size;
  255. while (byte_count >= 4) {
  256. data = (src[0] << 24) + (src[1] << 16) + (src[2] << 8) + src[3];
  257. WREG32(mmSMC_IND_DATA_0, data);
  258. src += 4;
  259. byte_count -= 4;
  260. }
  261. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  262. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
  263. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  264. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  265. return 0;
  266. }
  267. #if 0 /* not used yet */
  268. static int iceland_read_smc_sram_dword(struct amdgpu_device *adev,
  269. uint32_t smc_address,
  270. uint32_t *value,
  271. uint32_t limit)
  272. {
  273. int result;
  274. unsigned long flags;
  275. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  276. result = iceland_set_smc_sram_address(adev, smc_address, limit);
  277. if (result == 0)
  278. *value = RREG32(mmSMC_IND_DATA_0);
  279. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  280. return result;
  281. }
  282. static int iceland_write_smc_sram_dword(struct amdgpu_device *adev,
  283. uint32_t smc_address,
  284. uint32_t value,
  285. uint32_t limit)
  286. {
  287. int result;
  288. unsigned long flags;
  289. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  290. result = iceland_set_smc_sram_address(adev, smc_address, limit);
  291. if (result == 0)
  292. WREG32(mmSMC_IND_DATA_0, value);
  293. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  294. return result;
  295. }
  296. static int iceland_smu_stop_smc(struct amdgpu_device *adev)
  297. {
  298. iceland_reset_smc(adev);
  299. iceland_stop_smc_clock(adev);
  300. return 0;
  301. }
  302. #endif
  303. static int iceland_smu_start_smc(struct amdgpu_device *adev)
  304. {
  305. int i;
  306. uint32_t val;
  307. iceland_program_jump_on_start(adev);
  308. iceland_start_smc_clock(adev);
  309. iceland_start_smc(adev);
  310. for (i = 0; i < adev->usec_timeout; i++) {
  311. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  312. if (REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED) == 1)
  313. break;
  314. udelay(1);
  315. }
  316. return 0;
  317. }
  318. static enum AMDGPU_UCODE_ID iceland_convert_fw_type(uint32_t fw_type)
  319. {
  320. switch (fw_type) {
  321. case UCODE_ID_SDMA0:
  322. return AMDGPU_UCODE_ID_SDMA0;
  323. case UCODE_ID_SDMA1:
  324. return AMDGPU_UCODE_ID_SDMA1;
  325. case UCODE_ID_CP_CE:
  326. return AMDGPU_UCODE_ID_CP_CE;
  327. case UCODE_ID_CP_PFP:
  328. return AMDGPU_UCODE_ID_CP_PFP;
  329. case UCODE_ID_CP_ME:
  330. return AMDGPU_UCODE_ID_CP_ME;
  331. case UCODE_ID_CP_MEC:
  332. case UCODE_ID_CP_MEC_JT1:
  333. return AMDGPU_UCODE_ID_CP_MEC1;
  334. case UCODE_ID_CP_MEC_JT2:
  335. return AMDGPU_UCODE_ID_CP_MEC2;
  336. case UCODE_ID_RLC_G:
  337. return AMDGPU_UCODE_ID_RLC_G;
  338. default:
  339. DRM_ERROR("ucode type is out of range!\n");
  340. return AMDGPU_UCODE_ID_MAXIMUM;
  341. }
  342. }
  343. static uint32_t iceland_smu_get_mask_for_fw_type(uint32_t fw_type)
  344. {
  345. switch (fw_type) {
  346. case AMDGPU_UCODE_ID_SDMA0:
  347. return UCODE_ID_SDMA0_MASK;
  348. case AMDGPU_UCODE_ID_SDMA1:
  349. return UCODE_ID_SDMA1_MASK;
  350. case AMDGPU_UCODE_ID_CP_CE:
  351. return UCODE_ID_CP_CE_MASK;
  352. case AMDGPU_UCODE_ID_CP_PFP:
  353. return UCODE_ID_CP_PFP_MASK;
  354. case AMDGPU_UCODE_ID_CP_ME:
  355. return UCODE_ID_CP_ME_MASK;
  356. case AMDGPU_UCODE_ID_CP_MEC1:
  357. return UCODE_ID_CP_MEC_MASK | UCODE_ID_CP_MEC_JT1_MASK;
  358. case AMDGPU_UCODE_ID_CP_MEC2:
  359. return UCODE_ID_CP_MEC_MASK;
  360. case AMDGPU_UCODE_ID_RLC_G:
  361. return UCODE_ID_RLC_G_MASK;
  362. default:
  363. DRM_ERROR("ucode type is out of range!\n");
  364. return 0;
  365. }
  366. }
  367. static int iceland_smu_populate_single_firmware_entry(struct amdgpu_device *adev,
  368. uint32_t fw_type,
  369. struct SMU_Entry *entry)
  370. {
  371. enum AMDGPU_UCODE_ID id = iceland_convert_fw_type(fw_type);
  372. struct amdgpu_firmware_info *ucode = &adev->firmware.ucode[id];
  373. const struct gfx_firmware_header_v1_0 *header = NULL;
  374. uint64_t gpu_addr;
  375. uint32_t data_size;
  376. if (ucode->fw == NULL)
  377. return -EINVAL;
  378. gpu_addr = ucode->mc_addr;
  379. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  380. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  381. entry->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  382. entry->id = (uint16_t)fw_type;
  383. entry->image_addr_high = upper_32_bits(gpu_addr);
  384. entry->image_addr_low = lower_32_bits(gpu_addr);
  385. entry->meta_data_addr_high = 0;
  386. entry->meta_data_addr_low = 0;
  387. entry->data_size_byte = data_size;
  388. entry->num_register_entries = 0;
  389. entry->flags = 0;
  390. return 0;
  391. }
  392. static int iceland_smu_request_load_fw(struct amdgpu_device *adev)
  393. {
  394. struct iceland_smu_private_data *private = (struct iceland_smu_private_data *)adev->smu.priv;
  395. struct SMU_DRAMData_TOC *toc;
  396. uint32_t fw_to_load;
  397. toc = (struct SMU_DRAMData_TOC *)private->header;
  398. toc->num_entries = 0;
  399. toc->structure_version = 1;
  400. if (!adev->firmware.smu_load)
  401. return 0;
  402. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_RLC_G,
  403. &toc->entry[toc->num_entries++])) {
  404. DRM_ERROR("Failed to get firmware entry for RLC\n");
  405. return -EINVAL;
  406. }
  407. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_CE,
  408. &toc->entry[toc->num_entries++])) {
  409. DRM_ERROR("Failed to get firmware entry for CE\n");
  410. return -EINVAL;
  411. }
  412. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_PFP,
  413. &toc->entry[toc->num_entries++])) {
  414. DRM_ERROR("Failed to get firmware entry for PFP\n");
  415. return -EINVAL;
  416. }
  417. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_ME,
  418. &toc->entry[toc->num_entries++])) {
  419. DRM_ERROR("Failed to get firmware entry for ME\n");
  420. return -EINVAL;
  421. }
  422. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC,
  423. &toc->entry[toc->num_entries++])) {
  424. DRM_ERROR("Failed to get firmware entry for MEC\n");
  425. return -EINVAL;
  426. }
  427. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT1,
  428. &toc->entry[toc->num_entries++])) {
  429. DRM_ERROR("Failed to get firmware entry for MEC_JT1\n");
  430. return -EINVAL;
  431. }
  432. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA0,
  433. &toc->entry[toc->num_entries++])) {
  434. DRM_ERROR("Failed to get firmware entry for SDMA0\n");
  435. return -EINVAL;
  436. }
  437. if (iceland_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA1,
  438. &toc->entry[toc->num_entries++])) {
  439. DRM_ERROR("Failed to get firmware entry for SDMA1\n");
  440. return -EINVAL;
  441. }
  442. iceland_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_HI, private->header_addr_high);
  443. iceland_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_LO, private->header_addr_low);
  444. fw_to_load = UCODE_ID_RLC_G_MASK |
  445. UCODE_ID_SDMA0_MASK |
  446. UCODE_ID_SDMA1_MASK |
  447. UCODE_ID_CP_CE_MASK |
  448. UCODE_ID_CP_ME_MASK |
  449. UCODE_ID_CP_PFP_MASK |
  450. UCODE_ID_CP_MEC_MASK |
  451. UCODE_ID_CP_MEC_JT1_MASK;
  452. if (iceland_send_msg_to_smc_with_parameter_without_waiting(adev, PPSMC_MSG_LoadUcodes, fw_to_load)) {
  453. DRM_ERROR("Fail to request SMU load ucode\n");
  454. return -EINVAL;
  455. }
  456. return 0;
  457. }
  458. static int iceland_smu_check_fw_load_finish(struct amdgpu_device *adev,
  459. uint32_t fw_type)
  460. {
  461. uint32_t fw_mask = iceland_smu_get_mask_for_fw_type(fw_type);
  462. int i;
  463. for (i = 0; i < adev->usec_timeout; i++) {
  464. if (fw_mask == (RREG32_SMC(ixSOFT_REGISTERS_TABLE_27) & fw_mask))
  465. break;
  466. udelay(1);
  467. }
  468. if (i == adev->usec_timeout) {
  469. DRM_ERROR("check firmware loading failed\n");
  470. return -EINVAL;
  471. }
  472. return 0;
  473. }
  474. int iceland_smu_start(struct amdgpu_device *adev)
  475. {
  476. int result;
  477. result = iceland_smu_upload_firmware_image(adev);
  478. if (result)
  479. return result;
  480. result = iceland_smu_start_smc(adev);
  481. if (result)
  482. return result;
  483. return iceland_smu_request_load_fw(adev);
  484. }
  485. static const struct amdgpu_smumgr_funcs iceland_smumgr_funcs = {
  486. .check_fw_load_finish = iceland_smu_check_fw_load_finish,
  487. .request_smu_load_fw = NULL,
  488. .request_smu_specific_fw = NULL,
  489. };
  490. int iceland_smu_init(struct amdgpu_device *adev)
  491. {
  492. struct iceland_smu_private_data *private;
  493. uint32_t image_size = ((sizeof(struct SMU_DRAMData_TOC) / 4096) + 1) * 4096;
  494. struct amdgpu_bo **toc_buf = &adev->smu.toc_buf;
  495. uint64_t mc_addr;
  496. void *toc_buf_ptr;
  497. int ret;
  498. private = kzalloc(sizeof(struct iceland_smu_private_data), GFP_KERNEL);
  499. if (NULL == private)
  500. return -ENOMEM;
  501. /* allocate firmware buffers */
  502. if (adev->firmware.smu_load)
  503. amdgpu_ucode_init_bo(adev);
  504. adev->smu.priv = private;
  505. adev->smu.fw_flags = 0;
  506. /* Allocate FW image data structure and header buffer */
  507. ret = amdgpu_bo_create(adev, image_size, PAGE_SIZE,
  508. true, AMDGPU_GEM_DOMAIN_VRAM,
  509. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  510. NULL, NULL, toc_buf);
  511. if (ret) {
  512. DRM_ERROR("Failed to allocate memory for TOC buffer\n");
  513. return -ENOMEM;
  514. }
  515. /* Retrieve GPU address for header buffer and internal buffer */
  516. ret = amdgpu_bo_reserve(adev->smu.toc_buf, false);
  517. if (ret) {
  518. amdgpu_bo_unref(&adev->smu.toc_buf);
  519. DRM_ERROR("Failed to reserve the TOC buffer\n");
  520. return -EINVAL;
  521. }
  522. ret = amdgpu_bo_pin(adev->smu.toc_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  523. if (ret) {
  524. amdgpu_bo_unreserve(adev->smu.toc_buf);
  525. amdgpu_bo_unref(&adev->smu.toc_buf);
  526. DRM_ERROR("Failed to pin the TOC buffer\n");
  527. return -EINVAL;
  528. }
  529. ret = amdgpu_bo_kmap(*toc_buf, &toc_buf_ptr);
  530. if (ret) {
  531. amdgpu_bo_unreserve(adev->smu.toc_buf);
  532. amdgpu_bo_unref(&adev->smu.toc_buf);
  533. DRM_ERROR("Failed to map the TOC buffer\n");
  534. return -EINVAL;
  535. }
  536. amdgpu_bo_unreserve(adev->smu.toc_buf);
  537. private->header_addr_low = lower_32_bits(mc_addr);
  538. private->header_addr_high = upper_32_bits(mc_addr);
  539. private->header = toc_buf_ptr;
  540. adev->smu.smumgr_funcs = &iceland_smumgr_funcs;
  541. return 0;
  542. }
  543. int iceland_smu_fini(struct amdgpu_device *adev)
  544. {
  545. amdgpu_bo_unref(&adev->smu.toc_buf);
  546. kfree(adev->smu.priv);
  547. adev->smu.priv = NULL;
  548. if (adev->firmware.fw_buf)
  549. amdgpu_ucode_fini_bo(adev);
  550. return 0;
  551. }