fiji_smc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 "fiji_ppsmc.h"
  27. #include "fiji_smum.h"
  28. #include "smu_ucode_xfer_vi.h"
  29. #include "amdgpu_ucode.h"
  30. #include "smu/smu_7_1_3_d.h"
  31. #include "smu/smu_7_1_3_sh_mask.h"
  32. #define FIJI_SMC_SIZE 0x20000
  33. static int fiji_set_smc_sram_address(struct amdgpu_device *adev, uint32_t smc_address, uint32_t limit)
  34. {
  35. uint32_t val;
  36. if (smc_address & 3)
  37. return -EINVAL;
  38. if ((smc_address + 3) > limit)
  39. return -EINVAL;
  40. WREG32(mmSMC_IND_INDEX_0, smc_address);
  41. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  42. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
  43. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  44. return 0;
  45. }
  46. static int fiji_copy_bytes_to_smc(struct amdgpu_device *adev, uint32_t smc_start_address, const uint8_t *src, uint32_t byte_count, uint32_t limit)
  47. {
  48. uint32_t addr;
  49. uint32_t data, orig_data;
  50. int result = 0;
  51. uint32_t extra_shift;
  52. unsigned long flags;
  53. if (smc_start_address & 3)
  54. return -EINVAL;
  55. if ((smc_start_address + byte_count) > limit)
  56. return -EINVAL;
  57. addr = smc_start_address;
  58. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  59. while (byte_count >= 4) {
  60. /* Bytes are written into the SMC addres space with the MSB first */
  61. data = (src[0] << 24) + (src[1] << 16) + (src[2] << 8) + src[3];
  62. result = fiji_set_smc_sram_address(adev, addr, limit);
  63. if (result)
  64. goto out;
  65. WREG32(mmSMC_IND_DATA_0, data);
  66. src += 4;
  67. byte_count -= 4;
  68. addr += 4;
  69. }
  70. if (0 != byte_count) {
  71. /* Now write odd bytes left, do a read modify write cycle */
  72. data = 0;
  73. result = fiji_set_smc_sram_address(adev, addr, limit);
  74. if (result)
  75. goto out;
  76. orig_data = RREG32(mmSMC_IND_DATA_0);
  77. extra_shift = 8 * (4 - byte_count);
  78. while (byte_count > 0) {
  79. data = (data << 8) + *src++;
  80. byte_count--;
  81. }
  82. data <<= extra_shift;
  83. data |= (orig_data & ~((~0UL) << extra_shift));
  84. result = fiji_set_smc_sram_address(adev, addr, limit);
  85. if (result)
  86. goto out;
  87. WREG32(mmSMC_IND_DATA_0, data);
  88. }
  89. out:
  90. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  91. return result;
  92. }
  93. static int fiji_program_jump_on_start(struct amdgpu_device *adev)
  94. {
  95. static unsigned char data[] = {0xE0, 0x00, 0x80, 0x40};
  96. fiji_copy_bytes_to_smc(adev, 0x0, data, 4, sizeof(data)+1);
  97. return 0;
  98. }
  99. static bool fiji_is_smc_ram_running(struct amdgpu_device *adev)
  100. {
  101. uint32_t val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  102. val = REG_GET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable);
  103. return ((0 == val) && (0x20100 <= RREG32_SMC(ixSMC_PC_C)));
  104. }
  105. static int wait_smu_response(struct amdgpu_device *adev)
  106. {
  107. int i;
  108. uint32_t val;
  109. for (i = 0; i < adev->usec_timeout; i++) {
  110. val = RREG32(mmSMC_RESP_0);
  111. if (REG_GET_FIELD(val, SMC_RESP_0, SMC_RESP))
  112. break;
  113. udelay(1);
  114. }
  115. if (i == adev->usec_timeout)
  116. return -EINVAL;
  117. return 0;
  118. }
  119. static int fiji_send_msg_to_smc_offset(struct amdgpu_device *adev)
  120. {
  121. if (wait_smu_response(adev)) {
  122. DRM_ERROR("Failed to send previous message\n");
  123. return -EINVAL;
  124. }
  125. WREG32(mmSMC_MSG_ARG_0, 0x20000);
  126. WREG32(mmSMC_MESSAGE_0, PPSMC_MSG_Test);
  127. if (wait_smu_response(adev)) {
  128. DRM_ERROR("Failed to send message\n");
  129. return -EINVAL;
  130. }
  131. return 0;
  132. }
  133. static int fiji_send_msg_to_smc(struct amdgpu_device *adev, PPSMC_Msg msg)
  134. {
  135. if (!fiji_is_smc_ram_running(adev))
  136. {
  137. return -EINVAL;
  138. }
  139. if (wait_smu_response(adev)) {
  140. DRM_ERROR("Failed to send previous message\n");
  141. return -EINVAL;
  142. }
  143. WREG32(mmSMC_MESSAGE_0, msg);
  144. if (wait_smu_response(adev)) {
  145. DRM_ERROR("Failed to send message\n");
  146. return -EINVAL;
  147. }
  148. return 0;
  149. }
  150. static int fiji_send_msg_to_smc_without_waiting(struct amdgpu_device *adev,
  151. PPSMC_Msg msg)
  152. {
  153. if (wait_smu_response(adev)) {
  154. DRM_ERROR("Failed to send previous message\n");
  155. return -EINVAL;
  156. }
  157. WREG32(mmSMC_MESSAGE_0, msg);
  158. return 0;
  159. }
  160. static int fiji_send_msg_to_smc_with_parameter(struct amdgpu_device *adev,
  161. PPSMC_Msg msg,
  162. uint32_t parameter)
  163. {
  164. if (!fiji_is_smc_ram_running(adev))
  165. return -EINVAL;
  166. if (wait_smu_response(adev)) {
  167. DRM_ERROR("Failed to send previous message\n");
  168. return -EINVAL;
  169. }
  170. WREG32(mmSMC_MSG_ARG_0, parameter);
  171. return fiji_send_msg_to_smc(adev, msg);
  172. }
  173. static int fiji_send_msg_to_smc_with_parameter_without_waiting(
  174. struct amdgpu_device *adev,
  175. PPSMC_Msg msg, uint32_t parameter)
  176. {
  177. if (wait_smu_response(adev)) {
  178. DRM_ERROR("Failed to send previous message\n");
  179. return -EINVAL;
  180. }
  181. WREG32(mmSMC_MSG_ARG_0, parameter);
  182. return fiji_send_msg_to_smc_without_waiting(adev, msg);
  183. }
  184. #if 0 /* not used yet */
  185. static int fiji_wait_for_smc_inactive(struct amdgpu_device *adev)
  186. {
  187. int i;
  188. uint32_t val;
  189. if (!fiji_is_smc_ram_running(adev))
  190. return -EINVAL;
  191. for (i = 0; i < adev->usec_timeout; i++) {
  192. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  193. if (REG_GET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, cken) == 0)
  194. break;
  195. udelay(1);
  196. }
  197. if (i == adev->usec_timeout)
  198. return -EINVAL;
  199. return 0;
  200. }
  201. #endif
  202. static int fiji_smu_upload_firmware_image(struct amdgpu_device *adev)
  203. {
  204. const struct smc_firmware_header_v1_0 *hdr;
  205. uint32_t ucode_size;
  206. uint32_t ucode_start_address;
  207. const uint8_t *src;
  208. uint32_t val;
  209. uint32_t byte_count;
  210. uint32_t *data;
  211. unsigned long flags;
  212. if (!adev->pm.fw)
  213. return -EINVAL;
  214. /* Skip SMC ucode loading on SR-IOV capable boards.
  215. * vbios does this for us in asic_init in that case.
  216. */
  217. if (adev->virtualization.supports_sr_iov)
  218. return 0;
  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 > FIJI_SMC_SIZE) {
  231. DRM_ERROR("SMC address is beyond the SMC RAM area\n");
  232. return -EINVAL;
  233. }
  234. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  235. WREG32(mmSMC_IND_INDEX_0, ucode_start_address);
  236. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  237. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 1);
  238. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  239. byte_count = ucode_size;
  240. data = (uint32_t *)src;
  241. for (; byte_count >= 4; data++, byte_count -= 4)
  242. WREG32(mmSMC_IND_DATA_0, data[0]);
  243. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  244. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
  245. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  246. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  247. return 0;
  248. }
  249. #if 0 /* not used yet */
  250. static int fiji_read_smc_sram_dword(struct amdgpu_device *adev,
  251. uint32_t smc_address,
  252. uint32_t *value,
  253. uint32_t limit)
  254. {
  255. int result;
  256. unsigned long flags;
  257. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  258. result = fiji_set_smc_sram_address(adev, smc_address, limit);
  259. if (result == 0)
  260. *value = RREG32(mmSMC_IND_DATA_0);
  261. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  262. return result;
  263. }
  264. static int fiji_write_smc_sram_dword(struct amdgpu_device *adev,
  265. uint32_t smc_address,
  266. uint32_t value,
  267. uint32_t limit)
  268. {
  269. int result;
  270. unsigned long flags;
  271. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  272. result = fiji_set_smc_sram_address(adev, smc_address, limit);
  273. if (result == 0)
  274. WREG32(mmSMC_IND_DATA_0, value);
  275. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  276. return result;
  277. }
  278. static int fiji_smu_stop_smc(struct amdgpu_device *adev)
  279. {
  280. uint32_t val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  281. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  282. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  283. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  284. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 1);
  285. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  286. return 0;
  287. }
  288. #endif
  289. static enum AMDGPU_UCODE_ID fiji_convert_fw_type(uint32_t fw_type)
  290. {
  291. switch (fw_type) {
  292. case UCODE_ID_SDMA0:
  293. return AMDGPU_UCODE_ID_SDMA0;
  294. case UCODE_ID_SDMA1:
  295. return AMDGPU_UCODE_ID_SDMA1;
  296. case UCODE_ID_CP_CE:
  297. return AMDGPU_UCODE_ID_CP_CE;
  298. case UCODE_ID_CP_PFP:
  299. return AMDGPU_UCODE_ID_CP_PFP;
  300. case UCODE_ID_CP_ME:
  301. return AMDGPU_UCODE_ID_CP_ME;
  302. case UCODE_ID_CP_MEC:
  303. case UCODE_ID_CP_MEC_JT1:
  304. case UCODE_ID_CP_MEC_JT2:
  305. return AMDGPU_UCODE_ID_CP_MEC1;
  306. case UCODE_ID_RLC_G:
  307. return AMDGPU_UCODE_ID_RLC_G;
  308. default:
  309. DRM_ERROR("ucode type is out of range!\n");
  310. return AMDGPU_UCODE_ID_MAXIMUM;
  311. }
  312. }
  313. static int fiji_smu_populate_single_firmware_entry(struct amdgpu_device *adev,
  314. uint32_t fw_type,
  315. struct SMU_Entry *entry)
  316. {
  317. enum AMDGPU_UCODE_ID id = fiji_convert_fw_type(fw_type);
  318. struct amdgpu_firmware_info *ucode = &adev->firmware.ucode[id];
  319. const struct gfx_firmware_header_v1_0 *header = NULL;
  320. uint64_t gpu_addr;
  321. uint32_t data_size;
  322. if (ucode->fw == NULL)
  323. return -EINVAL;
  324. gpu_addr = ucode->mc_addr;
  325. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  326. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  327. if ((fw_type == UCODE_ID_CP_MEC_JT1) ||
  328. (fw_type == UCODE_ID_CP_MEC_JT2)) {
  329. gpu_addr += le32_to_cpu(header->jt_offset) << 2;
  330. data_size = le32_to_cpu(header->jt_size) << 2;
  331. }
  332. entry->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  333. entry->id = (uint16_t)fw_type;
  334. entry->image_addr_high = upper_32_bits(gpu_addr);
  335. entry->image_addr_low = lower_32_bits(gpu_addr);
  336. entry->meta_data_addr_high = 0;
  337. entry->meta_data_addr_low = 0;
  338. entry->data_size_byte = data_size;
  339. entry->num_register_entries = 0;
  340. if (fw_type == UCODE_ID_RLC_G)
  341. entry->flags = 1;
  342. else
  343. entry->flags = 0;
  344. return 0;
  345. }
  346. static int fiji_smu_request_load_fw(struct amdgpu_device *adev)
  347. {
  348. struct fiji_smu_private_data *private = (struct fiji_smu_private_data *)adev->smu.priv;
  349. struct SMU_DRAMData_TOC *toc;
  350. uint32_t fw_to_load;
  351. WREG32_SMC(ixSOFT_REGISTERS_TABLE_28, 0);
  352. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_SMU_DRAM_ADDR_HI, private->smu_buffer_addr_high);
  353. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_SMU_DRAM_ADDR_LO, private->smu_buffer_addr_low);
  354. toc = (struct SMU_DRAMData_TOC *)private->header;
  355. toc->num_entries = 0;
  356. toc->structure_version = 1;
  357. if (!adev->firmware.smu_load)
  358. return 0;
  359. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_RLC_G,
  360. &toc->entry[toc->num_entries++])) {
  361. DRM_ERROR("Failed to get firmware entry for RLC\n");
  362. return -EINVAL;
  363. }
  364. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_CE,
  365. &toc->entry[toc->num_entries++])) {
  366. DRM_ERROR("Failed to get firmware entry for CE\n");
  367. return -EINVAL;
  368. }
  369. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_PFP,
  370. &toc->entry[toc->num_entries++])) {
  371. DRM_ERROR("Failed to get firmware entry for PFP\n");
  372. return -EINVAL;
  373. }
  374. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_ME,
  375. &toc->entry[toc->num_entries++])) {
  376. DRM_ERROR("Failed to get firmware entry for ME\n");
  377. return -EINVAL;
  378. }
  379. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC,
  380. &toc->entry[toc->num_entries++])) {
  381. DRM_ERROR("Failed to get firmware entry for MEC\n");
  382. return -EINVAL;
  383. }
  384. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT1,
  385. &toc->entry[toc->num_entries++])) {
  386. DRM_ERROR("Failed to get firmware entry for MEC_JT1\n");
  387. return -EINVAL;
  388. }
  389. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT2,
  390. &toc->entry[toc->num_entries++])) {
  391. DRM_ERROR("Failed to get firmware entry for MEC_JT2\n");
  392. return -EINVAL;
  393. }
  394. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA0,
  395. &toc->entry[toc->num_entries++])) {
  396. DRM_ERROR("Failed to get firmware entry for SDMA0\n");
  397. return -EINVAL;
  398. }
  399. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA1,
  400. &toc->entry[toc->num_entries++])) {
  401. DRM_ERROR("Failed to get firmware entry for SDMA1\n");
  402. return -EINVAL;
  403. }
  404. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_HI, private->header_addr_high);
  405. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_LO, private->header_addr_low);
  406. fw_to_load = UCODE_ID_RLC_G_MASK |
  407. UCODE_ID_SDMA0_MASK |
  408. UCODE_ID_SDMA1_MASK |
  409. UCODE_ID_CP_CE_MASK |
  410. UCODE_ID_CP_ME_MASK |
  411. UCODE_ID_CP_PFP_MASK |
  412. UCODE_ID_CP_MEC_MASK;
  413. if (fiji_send_msg_to_smc_with_parameter_without_waiting(adev, PPSMC_MSG_LoadUcodes, fw_to_load)) {
  414. DRM_ERROR("Fail to request SMU load ucode\n");
  415. return -EINVAL;
  416. }
  417. return 0;
  418. }
  419. static uint32_t fiji_smu_get_mask_for_fw_type(uint32_t fw_type)
  420. {
  421. switch (fw_type) {
  422. case AMDGPU_UCODE_ID_SDMA0:
  423. return UCODE_ID_SDMA0_MASK;
  424. case AMDGPU_UCODE_ID_SDMA1:
  425. return UCODE_ID_SDMA1_MASK;
  426. case AMDGPU_UCODE_ID_CP_CE:
  427. return UCODE_ID_CP_CE_MASK;
  428. case AMDGPU_UCODE_ID_CP_PFP:
  429. return UCODE_ID_CP_PFP_MASK;
  430. case AMDGPU_UCODE_ID_CP_ME:
  431. return UCODE_ID_CP_ME_MASK;
  432. case AMDGPU_UCODE_ID_CP_MEC1:
  433. return UCODE_ID_CP_MEC_MASK;
  434. case AMDGPU_UCODE_ID_CP_MEC2:
  435. return UCODE_ID_CP_MEC_MASK;
  436. case AMDGPU_UCODE_ID_RLC_G:
  437. return UCODE_ID_RLC_G_MASK;
  438. default:
  439. DRM_ERROR("ucode type is out of range!\n");
  440. return 0;
  441. }
  442. }
  443. static int fiji_smu_check_fw_load_finish(struct amdgpu_device *adev,
  444. uint32_t fw_type)
  445. {
  446. uint32_t fw_mask = fiji_smu_get_mask_for_fw_type(fw_type);
  447. int i;
  448. for (i = 0; i < adev->usec_timeout; i++) {
  449. if (fw_mask == (RREG32_SMC(ixSOFT_REGISTERS_TABLE_28) & fw_mask))
  450. break;
  451. udelay(1);
  452. }
  453. if (i == adev->usec_timeout) {
  454. DRM_ERROR("check firmware loading failed\n");
  455. return -EINVAL;
  456. }
  457. return 0;
  458. }
  459. static int fiji_smu_start_in_protection_mode(struct amdgpu_device *adev)
  460. {
  461. int result;
  462. uint32_t val;
  463. int i;
  464. /* Assert reset */
  465. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  466. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  467. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  468. result = fiji_smu_upload_firmware_image(adev);
  469. if (result)
  470. return result;
  471. /* Clear status */
  472. WREG32_SMC(ixSMU_STATUS, 0);
  473. /* Enable clock */
  474. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  475. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
  476. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  477. /* De-assert reset */
  478. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  479. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  480. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  481. /* Set SMU Auto Start */
  482. val = RREG32_SMC(ixSMU_INPUT_DATA);
  483. val = REG_SET_FIELD(val, SMU_INPUT_DATA, AUTO_START, 1);
  484. WREG32_SMC(ixSMU_INPUT_DATA, val);
  485. /* Clear firmware interrupt enable flag */
  486. WREG32_SMC(ixFIRMWARE_FLAGS, 0);
  487. for (i = 0; i < adev->usec_timeout; i++) {
  488. val = RREG32_SMC(ixRCU_UC_EVENTS);
  489. if (REG_GET_FIELD(val, RCU_UC_EVENTS, INTERRUPTS_ENABLED))
  490. break;
  491. udelay(1);
  492. }
  493. if (i == adev->usec_timeout) {
  494. DRM_ERROR("Interrupt is not enabled by firmware\n");
  495. return -EINVAL;
  496. }
  497. /* Call Test SMU message with 0x20000 offset
  498. * to trigger SMU start
  499. */
  500. fiji_send_msg_to_smc_offset(adev);
  501. DRM_INFO("[FM]try triger smu start\n");
  502. /* Wait for done bit to be set */
  503. for (i = 0; i < adev->usec_timeout; i++) {
  504. val = RREG32_SMC(ixSMU_STATUS);
  505. if (REG_GET_FIELD(val, SMU_STATUS, SMU_DONE))
  506. break;
  507. udelay(1);
  508. }
  509. if (i == adev->usec_timeout) {
  510. DRM_ERROR("Timeout for SMU start\n");
  511. return -EINVAL;
  512. }
  513. /* Check pass/failed indicator */
  514. val = RREG32_SMC(ixSMU_STATUS);
  515. if (!REG_GET_FIELD(val, SMU_STATUS, SMU_PASS)) {
  516. DRM_ERROR("SMU Firmware start failed\n");
  517. return -EINVAL;
  518. }
  519. DRM_INFO("[FM]smu started\n");
  520. /* Wait for firmware to initialize */
  521. for (i = 0; i < adev->usec_timeout; i++) {
  522. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  523. if(REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED))
  524. break;
  525. udelay(1);
  526. }
  527. if (i == adev->usec_timeout) {
  528. DRM_ERROR("SMU firmware initialization failed\n");
  529. return -EINVAL;
  530. }
  531. DRM_INFO("[FM]smu initialized\n");
  532. return 0;
  533. }
  534. static int fiji_smu_start_in_non_protection_mode(struct amdgpu_device *adev)
  535. {
  536. int i, result;
  537. uint32_t val;
  538. /* wait for smc boot up */
  539. for (i = 0; i < adev->usec_timeout; i++) {
  540. val = RREG32_SMC(ixRCU_UC_EVENTS);
  541. val = REG_GET_FIELD(val, RCU_UC_EVENTS, boot_seq_done);
  542. if (val)
  543. break;
  544. udelay(1);
  545. }
  546. if (i == adev->usec_timeout) {
  547. DRM_ERROR("SMC boot sequence is not completed\n");
  548. return -EINVAL;
  549. }
  550. /* Clear firmware interrupt enable flag */
  551. WREG32_SMC(ixFIRMWARE_FLAGS, 0);
  552. /* Assert reset */
  553. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  554. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  555. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  556. result = fiji_smu_upload_firmware_image(adev);
  557. if (result)
  558. return result;
  559. /* Set smc instruct start point at 0x0 */
  560. fiji_program_jump_on_start(adev);
  561. /* Enable clock */
  562. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  563. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
  564. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  565. /* De-assert reset */
  566. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  567. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  568. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  569. /* Wait for firmware to initialize */
  570. for (i = 0; i < adev->usec_timeout; i++) {
  571. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  572. if (REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED))
  573. break;
  574. udelay(1);
  575. }
  576. if (i == adev->usec_timeout) {
  577. DRM_ERROR("Timeout for SMC firmware initialization\n");
  578. return -EINVAL;
  579. }
  580. return 0;
  581. }
  582. int fiji_smu_start(struct amdgpu_device *adev)
  583. {
  584. int result;
  585. uint32_t val;
  586. if (!fiji_is_smc_ram_running(adev)) {
  587. val = RREG32_SMC(ixSMU_FIRMWARE);
  588. if (!REG_GET_FIELD(val, SMU_FIRMWARE, SMU_MODE)) {
  589. DRM_INFO("[FM]start smu in nonprotection mode\n");
  590. result = fiji_smu_start_in_non_protection_mode(adev);
  591. if (result)
  592. return result;
  593. } else {
  594. DRM_INFO("[FM]start smu in protection mode\n");
  595. result = fiji_smu_start_in_protection_mode(adev);
  596. if (result)
  597. return result;
  598. }
  599. }
  600. return fiji_smu_request_load_fw(adev);
  601. }
  602. static const struct amdgpu_smumgr_funcs fiji_smumgr_funcs = {
  603. .check_fw_load_finish = fiji_smu_check_fw_load_finish,
  604. .request_smu_load_fw = NULL,
  605. .request_smu_specific_fw = NULL,
  606. };
  607. int fiji_smu_init(struct amdgpu_device *adev)
  608. {
  609. struct fiji_smu_private_data *private;
  610. uint32_t image_size = ((sizeof(struct SMU_DRAMData_TOC) / 4096) + 1) * 4096;
  611. uint32_t smu_internal_buffer_size = 200*4096;
  612. struct amdgpu_bo **toc_buf = &adev->smu.toc_buf;
  613. struct amdgpu_bo **smu_buf = &adev->smu.smu_buf;
  614. uint64_t mc_addr;
  615. void *toc_buf_ptr;
  616. void *smu_buf_ptr;
  617. int ret;
  618. private = kzalloc(sizeof(struct fiji_smu_private_data), GFP_KERNEL);
  619. if (NULL == private)
  620. return -ENOMEM;
  621. /* allocate firmware buffers */
  622. if (adev->firmware.smu_load)
  623. amdgpu_ucode_init_bo(adev);
  624. adev->smu.priv = private;
  625. adev->smu.fw_flags = 0;
  626. /* Allocate FW image data structure and header buffer */
  627. ret = amdgpu_bo_create(adev, image_size, PAGE_SIZE,
  628. true, AMDGPU_GEM_DOMAIN_VRAM,
  629. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  630. NULL, NULL, toc_buf);
  631. if (ret) {
  632. DRM_ERROR("Failed to allocate memory for TOC buffer\n");
  633. return -ENOMEM;
  634. }
  635. /* Allocate buffer for SMU internal buffer */
  636. ret = amdgpu_bo_create(adev, smu_internal_buffer_size, PAGE_SIZE,
  637. true, AMDGPU_GEM_DOMAIN_VRAM,
  638. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  639. NULL, NULL, smu_buf);
  640. if (ret) {
  641. DRM_ERROR("Failed to allocate memory for SMU internal buffer\n");
  642. return -ENOMEM;
  643. }
  644. /* Retrieve GPU address for header buffer and internal buffer */
  645. ret = amdgpu_bo_reserve(adev->smu.toc_buf, false);
  646. if (ret) {
  647. amdgpu_bo_unref(&adev->smu.toc_buf);
  648. DRM_ERROR("Failed to reserve the TOC buffer\n");
  649. return -EINVAL;
  650. }
  651. ret = amdgpu_bo_pin(adev->smu.toc_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  652. if (ret) {
  653. amdgpu_bo_unreserve(adev->smu.toc_buf);
  654. amdgpu_bo_unref(&adev->smu.toc_buf);
  655. DRM_ERROR("Failed to pin the TOC buffer\n");
  656. return -EINVAL;
  657. }
  658. ret = amdgpu_bo_kmap(*toc_buf, &toc_buf_ptr);
  659. if (ret) {
  660. amdgpu_bo_unreserve(adev->smu.toc_buf);
  661. amdgpu_bo_unref(&adev->smu.toc_buf);
  662. DRM_ERROR("Failed to map the TOC buffer\n");
  663. return -EINVAL;
  664. }
  665. amdgpu_bo_unreserve(adev->smu.toc_buf);
  666. private->header_addr_low = lower_32_bits(mc_addr);
  667. private->header_addr_high = upper_32_bits(mc_addr);
  668. private->header = toc_buf_ptr;
  669. ret = amdgpu_bo_reserve(adev->smu.smu_buf, false);
  670. if (ret) {
  671. amdgpu_bo_unref(&adev->smu.smu_buf);
  672. amdgpu_bo_unref(&adev->smu.toc_buf);
  673. DRM_ERROR("Failed to reserve the SMU internal buffer\n");
  674. return -EINVAL;
  675. }
  676. ret = amdgpu_bo_pin(adev->smu.smu_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  677. if (ret) {
  678. amdgpu_bo_unreserve(adev->smu.smu_buf);
  679. amdgpu_bo_unref(&adev->smu.smu_buf);
  680. amdgpu_bo_unref(&adev->smu.toc_buf);
  681. DRM_ERROR("Failed to pin the SMU internal buffer\n");
  682. return -EINVAL;
  683. }
  684. ret = amdgpu_bo_kmap(*smu_buf, &smu_buf_ptr);
  685. if (ret) {
  686. amdgpu_bo_unreserve(adev->smu.smu_buf);
  687. amdgpu_bo_unref(&adev->smu.smu_buf);
  688. amdgpu_bo_unref(&adev->smu.toc_buf);
  689. DRM_ERROR("Failed to map the SMU internal buffer\n");
  690. return -EINVAL;
  691. }
  692. amdgpu_bo_unreserve(adev->smu.smu_buf);
  693. private->smu_buffer_addr_low = lower_32_bits(mc_addr);
  694. private->smu_buffer_addr_high = upper_32_bits(mc_addr);
  695. adev->smu.smumgr_funcs = &fiji_smumgr_funcs;
  696. return 0;
  697. }
  698. int fiji_smu_fini(struct amdgpu_device *adev)
  699. {
  700. amdgpu_bo_unref(&adev->smu.toc_buf);
  701. amdgpu_bo_unref(&adev->smu.smu_buf);
  702. kfree(adev->smu.priv);
  703. adev->smu.priv = NULL;
  704. if (adev->firmware.fw_buf)
  705. amdgpu_ucode_fini_bo(adev);
  706. return 0;
  707. }