fiji_smc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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_smumgr.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. hdr = (const struct smc_firmware_header_v1_0 *)adev->pm.fw->data;
  215. amdgpu_ucode_print_smc_hdr(&hdr->header);
  216. adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
  217. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  218. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  219. src = (const uint8_t *)
  220. (adev->pm.fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  221. if (ucode_size & 3) {
  222. DRM_ERROR("SMC ucode is not 4 bytes aligned\n");
  223. return -EINVAL;
  224. }
  225. if (ucode_size > FIJI_SMC_SIZE) {
  226. DRM_ERROR("SMC address is beyond the SMC RAM area\n");
  227. return -EINVAL;
  228. }
  229. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  230. WREG32(mmSMC_IND_INDEX_0, ucode_start_address);
  231. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  232. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 1);
  233. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  234. byte_count = ucode_size;
  235. data = (uint32_t *)src;
  236. for (; byte_count >= 4; data++, byte_count -= 4)
  237. WREG32(mmSMC_IND_DATA_0, data[0]);
  238. val = RREG32(mmSMC_IND_ACCESS_CNTL);
  239. val = REG_SET_FIELD(val, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
  240. WREG32(mmSMC_IND_ACCESS_CNTL, val);
  241. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  242. return 0;
  243. }
  244. #if 0 /* not used yet */
  245. static int fiji_read_smc_sram_dword(struct amdgpu_device *adev,
  246. uint32_t smc_address,
  247. uint32_t *value,
  248. uint32_t limit)
  249. {
  250. int result;
  251. unsigned long flags;
  252. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  253. result = fiji_set_smc_sram_address(adev, smc_address, limit);
  254. if (result == 0)
  255. *value = RREG32(mmSMC_IND_DATA_0);
  256. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  257. return result;
  258. }
  259. static int fiji_write_smc_sram_dword(struct amdgpu_device *adev,
  260. uint32_t smc_address,
  261. uint32_t value,
  262. uint32_t limit)
  263. {
  264. int result;
  265. unsigned long flags;
  266. spin_lock_irqsave(&adev->smc_idx_lock, flags);
  267. result = fiji_set_smc_sram_address(adev, smc_address, limit);
  268. if (result == 0)
  269. WREG32(mmSMC_IND_DATA_0, value);
  270. spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
  271. return result;
  272. }
  273. static int fiji_smu_stop_smc(struct amdgpu_device *adev)
  274. {
  275. uint32_t val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  276. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  277. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  278. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  279. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 1);
  280. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  281. return 0;
  282. }
  283. #endif
  284. static enum AMDGPU_UCODE_ID fiji_convert_fw_type(uint32_t fw_type)
  285. {
  286. switch (fw_type) {
  287. case UCODE_ID_SDMA0:
  288. return AMDGPU_UCODE_ID_SDMA0;
  289. case UCODE_ID_SDMA1:
  290. return AMDGPU_UCODE_ID_SDMA1;
  291. case UCODE_ID_CP_CE:
  292. return AMDGPU_UCODE_ID_CP_CE;
  293. case UCODE_ID_CP_PFP:
  294. return AMDGPU_UCODE_ID_CP_PFP;
  295. case UCODE_ID_CP_ME:
  296. return AMDGPU_UCODE_ID_CP_ME;
  297. case UCODE_ID_CP_MEC:
  298. case UCODE_ID_CP_MEC_JT1:
  299. case UCODE_ID_CP_MEC_JT2:
  300. return AMDGPU_UCODE_ID_CP_MEC1;
  301. case UCODE_ID_RLC_G:
  302. return AMDGPU_UCODE_ID_RLC_G;
  303. default:
  304. DRM_ERROR("ucode type is out of range!\n");
  305. return AMDGPU_UCODE_ID_MAXIMUM;
  306. }
  307. }
  308. static int fiji_smu_populate_single_firmware_entry(struct amdgpu_device *adev,
  309. uint32_t fw_type,
  310. struct SMU_Entry *entry)
  311. {
  312. enum AMDGPU_UCODE_ID id = fiji_convert_fw_type(fw_type);
  313. struct amdgpu_firmware_info *ucode = &adev->firmware.ucode[id];
  314. const struct gfx_firmware_header_v1_0 *header = NULL;
  315. uint64_t gpu_addr;
  316. uint32_t data_size;
  317. if (ucode->fw == NULL)
  318. return -EINVAL;
  319. gpu_addr = ucode->mc_addr;
  320. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  321. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  322. if ((fw_type == UCODE_ID_CP_MEC_JT1) ||
  323. (fw_type == UCODE_ID_CP_MEC_JT2)) {
  324. gpu_addr += le32_to_cpu(header->jt_offset) << 2;
  325. data_size = le32_to_cpu(header->jt_size) << 2;
  326. }
  327. entry->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  328. entry->id = (uint16_t)fw_type;
  329. entry->image_addr_high = upper_32_bits(gpu_addr);
  330. entry->image_addr_low = lower_32_bits(gpu_addr);
  331. entry->meta_data_addr_high = 0;
  332. entry->meta_data_addr_low = 0;
  333. entry->data_size_byte = data_size;
  334. entry->num_register_entries = 0;
  335. if (fw_type == UCODE_ID_RLC_G)
  336. entry->flags = 1;
  337. else
  338. entry->flags = 0;
  339. return 0;
  340. }
  341. static int fiji_smu_request_load_fw(struct amdgpu_device *adev)
  342. {
  343. struct fiji_smu_private_data *private = (struct fiji_smu_private_data *)adev->smu.priv;
  344. struct SMU_DRAMData_TOC *toc;
  345. uint32_t fw_to_load;
  346. WREG32_SMC(ixSOFT_REGISTERS_TABLE_28, 0);
  347. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_SMU_DRAM_ADDR_HI, private->smu_buffer_addr_high);
  348. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_SMU_DRAM_ADDR_LO, private->smu_buffer_addr_low);
  349. toc = (struct SMU_DRAMData_TOC *)private->header;
  350. toc->num_entries = 0;
  351. toc->structure_version = 1;
  352. if (!adev->firmware.smu_load)
  353. return 0;
  354. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_RLC_G,
  355. &toc->entry[toc->num_entries++])) {
  356. DRM_ERROR("Failed to get firmware entry for RLC\n");
  357. return -EINVAL;
  358. }
  359. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_CE,
  360. &toc->entry[toc->num_entries++])) {
  361. DRM_ERROR("Failed to get firmware entry for CE\n");
  362. return -EINVAL;
  363. }
  364. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_PFP,
  365. &toc->entry[toc->num_entries++])) {
  366. DRM_ERROR("Failed to get firmware entry for PFP\n");
  367. return -EINVAL;
  368. }
  369. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_ME,
  370. &toc->entry[toc->num_entries++])) {
  371. DRM_ERROR("Failed to get firmware entry for ME\n");
  372. return -EINVAL;
  373. }
  374. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC,
  375. &toc->entry[toc->num_entries++])) {
  376. DRM_ERROR("Failed to get firmware entry for MEC\n");
  377. return -EINVAL;
  378. }
  379. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT1,
  380. &toc->entry[toc->num_entries++])) {
  381. DRM_ERROR("Failed to get firmware entry for MEC_JT1\n");
  382. return -EINVAL;
  383. }
  384. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT2,
  385. &toc->entry[toc->num_entries++])) {
  386. DRM_ERROR("Failed to get firmware entry for MEC_JT2\n");
  387. return -EINVAL;
  388. }
  389. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA0,
  390. &toc->entry[toc->num_entries++])) {
  391. DRM_ERROR("Failed to get firmware entry for SDMA0\n");
  392. return -EINVAL;
  393. }
  394. if (fiji_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA1,
  395. &toc->entry[toc->num_entries++])) {
  396. DRM_ERROR("Failed to get firmware entry for SDMA1\n");
  397. return -EINVAL;
  398. }
  399. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_HI, private->header_addr_high);
  400. fiji_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_LO, private->header_addr_low);
  401. fw_to_load = UCODE_ID_RLC_G_MASK |
  402. UCODE_ID_SDMA0_MASK |
  403. UCODE_ID_SDMA1_MASK |
  404. UCODE_ID_CP_CE_MASK |
  405. UCODE_ID_CP_ME_MASK |
  406. UCODE_ID_CP_PFP_MASK |
  407. UCODE_ID_CP_MEC_MASK;
  408. if (fiji_send_msg_to_smc_with_parameter_without_waiting(adev, PPSMC_MSG_LoadUcodes, fw_to_load)) {
  409. DRM_ERROR("Fail to request SMU load ucode\n");
  410. return -EINVAL;
  411. }
  412. return 0;
  413. }
  414. static uint32_t fiji_smu_get_mask_for_fw_type(uint32_t fw_type)
  415. {
  416. switch (fw_type) {
  417. case AMDGPU_UCODE_ID_SDMA0:
  418. return UCODE_ID_SDMA0_MASK;
  419. case AMDGPU_UCODE_ID_SDMA1:
  420. return UCODE_ID_SDMA1_MASK;
  421. case AMDGPU_UCODE_ID_CP_CE:
  422. return UCODE_ID_CP_CE_MASK;
  423. case AMDGPU_UCODE_ID_CP_PFP:
  424. return UCODE_ID_CP_PFP_MASK;
  425. case AMDGPU_UCODE_ID_CP_ME:
  426. return UCODE_ID_CP_ME_MASK;
  427. case AMDGPU_UCODE_ID_CP_MEC1:
  428. return UCODE_ID_CP_MEC_MASK;
  429. case AMDGPU_UCODE_ID_CP_MEC2:
  430. return UCODE_ID_CP_MEC_MASK;
  431. case AMDGPU_UCODE_ID_RLC_G:
  432. return UCODE_ID_RLC_G_MASK;
  433. default:
  434. DRM_ERROR("ucode type is out of range!\n");
  435. return 0;
  436. }
  437. }
  438. static int fiji_smu_check_fw_load_finish(struct amdgpu_device *adev,
  439. uint32_t fw_type)
  440. {
  441. uint32_t fw_mask = fiji_smu_get_mask_for_fw_type(fw_type);
  442. int i;
  443. for (i = 0; i < adev->usec_timeout; i++) {
  444. if (fw_mask == (RREG32_SMC(ixSOFT_REGISTERS_TABLE_28) & fw_mask))
  445. break;
  446. udelay(1);
  447. }
  448. if (i == adev->usec_timeout) {
  449. DRM_ERROR("check firmware loading failed\n");
  450. return -EINVAL;
  451. }
  452. return 0;
  453. }
  454. static int fiji_smu_start_in_protection_mode(struct amdgpu_device *adev)
  455. {
  456. int result;
  457. uint32_t val;
  458. int i;
  459. /* Assert reset */
  460. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  461. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  462. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  463. result = fiji_smu_upload_firmware_image(adev);
  464. if (result)
  465. return result;
  466. /* Clear status */
  467. WREG32_SMC(ixSMU_STATUS, 0);
  468. /* Enable clock */
  469. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  470. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
  471. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  472. /* De-assert reset */
  473. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  474. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  475. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  476. /* Set SMU Auto Start */
  477. val = RREG32_SMC(ixSMU_INPUT_DATA);
  478. val = REG_SET_FIELD(val, SMU_INPUT_DATA, AUTO_START, 1);
  479. WREG32_SMC(ixSMU_INPUT_DATA, val);
  480. /* Clear firmware interrupt enable flag */
  481. WREG32_SMC(ixFIRMWARE_FLAGS, 0);
  482. for (i = 0; i < adev->usec_timeout; i++) {
  483. val = RREG32_SMC(ixRCU_UC_EVENTS);
  484. if (REG_GET_FIELD(val, RCU_UC_EVENTS, INTERRUPTS_ENABLED))
  485. break;
  486. udelay(1);
  487. }
  488. if (i == adev->usec_timeout) {
  489. DRM_ERROR("Interrupt is not enabled by firmware\n");
  490. return -EINVAL;
  491. }
  492. /* Call Test SMU message with 0x20000 offset
  493. * to trigger SMU start
  494. */
  495. fiji_send_msg_to_smc_offset(adev);
  496. DRM_INFO("[FM]try triger smu start\n");
  497. /* Wait for done bit to be set */
  498. for (i = 0; i < adev->usec_timeout; i++) {
  499. val = RREG32_SMC(ixSMU_STATUS);
  500. if (REG_GET_FIELD(val, SMU_STATUS, SMU_DONE))
  501. break;
  502. udelay(1);
  503. }
  504. if (i == adev->usec_timeout) {
  505. DRM_ERROR("Timeout for SMU start\n");
  506. return -EINVAL;
  507. }
  508. /* Check pass/failed indicator */
  509. val = RREG32_SMC(ixSMU_STATUS);
  510. if (!REG_GET_FIELD(val, SMU_STATUS, SMU_PASS)) {
  511. DRM_ERROR("SMU Firmware start failed\n");
  512. return -EINVAL;
  513. }
  514. DRM_INFO("[FM]smu started\n");
  515. /* Wait for firmware to initialize */
  516. for (i = 0; i < adev->usec_timeout; i++) {
  517. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  518. if(REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED))
  519. break;
  520. udelay(1);
  521. }
  522. if (i == adev->usec_timeout) {
  523. DRM_ERROR("SMU firmware initialization failed\n");
  524. return -EINVAL;
  525. }
  526. DRM_INFO("[FM]smu initialized\n");
  527. return 0;
  528. }
  529. static int fiji_smu_start_in_non_protection_mode(struct amdgpu_device *adev)
  530. {
  531. int i, result;
  532. uint32_t val;
  533. /* wait for smc boot up */
  534. for (i = 0; i < adev->usec_timeout; i++) {
  535. val = RREG32_SMC(ixRCU_UC_EVENTS);
  536. val = REG_GET_FIELD(val, RCU_UC_EVENTS, boot_seq_done);
  537. if (val)
  538. break;
  539. udelay(1);
  540. }
  541. if (i == adev->usec_timeout) {
  542. DRM_ERROR("SMC boot sequence is not completed\n");
  543. return -EINVAL;
  544. }
  545. /* Clear firmware interrupt enable flag */
  546. WREG32_SMC(ixFIRMWARE_FLAGS, 0);
  547. /* Assert reset */
  548. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  549. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  550. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  551. result = fiji_smu_upload_firmware_image(adev);
  552. if (result)
  553. return result;
  554. /* Set smc instruct start point at 0x0 */
  555. fiji_program_jump_on_start(adev);
  556. /* Enable clock */
  557. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  558. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
  559. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  560. /* De-assert reset */
  561. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  562. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  563. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  564. /* Wait for firmware to initialize */
  565. for (i = 0; i < adev->usec_timeout; i++) {
  566. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  567. if (REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED))
  568. break;
  569. udelay(1);
  570. }
  571. if (i == adev->usec_timeout) {
  572. DRM_ERROR("Timeout for SMC firmware initialization\n");
  573. return -EINVAL;
  574. }
  575. return 0;
  576. }
  577. int fiji_smu_start(struct amdgpu_device *adev)
  578. {
  579. int result;
  580. uint32_t val;
  581. if (!fiji_is_smc_ram_running(adev)) {
  582. val = RREG32_SMC(ixSMU_FIRMWARE);
  583. if (!REG_GET_FIELD(val, SMU_FIRMWARE, SMU_MODE)) {
  584. DRM_INFO("[FM]start smu in nonprotection mode\n");
  585. result = fiji_smu_start_in_non_protection_mode(adev);
  586. if (result)
  587. return result;
  588. } else {
  589. DRM_INFO("[FM]start smu in protection mode\n");
  590. result = fiji_smu_start_in_protection_mode(adev);
  591. if (result)
  592. return result;
  593. }
  594. }
  595. return fiji_smu_request_load_fw(adev);
  596. }
  597. static const struct amdgpu_smumgr_funcs fiji_smumgr_funcs = {
  598. .check_fw_load_finish = fiji_smu_check_fw_load_finish,
  599. .request_smu_load_fw = NULL,
  600. .request_smu_specific_fw = NULL,
  601. };
  602. int fiji_smu_init(struct amdgpu_device *adev)
  603. {
  604. struct fiji_smu_private_data *private;
  605. uint32_t image_size = ((sizeof(struct SMU_DRAMData_TOC) / 4096) + 1) * 4096;
  606. uint32_t smu_internal_buffer_size = 200*4096;
  607. struct amdgpu_bo **toc_buf = &adev->smu.toc_buf;
  608. struct amdgpu_bo **smu_buf = &adev->smu.smu_buf;
  609. uint64_t mc_addr;
  610. void *toc_buf_ptr;
  611. void *smu_buf_ptr;
  612. int ret;
  613. private = kzalloc(sizeof(struct fiji_smu_private_data), GFP_KERNEL);
  614. if (NULL == private)
  615. return -ENOMEM;
  616. /* allocate firmware buffers */
  617. if (adev->firmware.smu_load)
  618. amdgpu_ucode_init_bo(adev);
  619. adev->smu.priv = private;
  620. adev->smu.fw_flags = 0;
  621. /* Allocate FW image data structure and header buffer */
  622. ret = amdgpu_bo_create(adev, image_size, PAGE_SIZE,
  623. true, AMDGPU_GEM_DOMAIN_VRAM,
  624. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  625. NULL, NULL, toc_buf);
  626. if (ret) {
  627. DRM_ERROR("Failed to allocate memory for TOC buffer\n");
  628. return -ENOMEM;
  629. }
  630. /* Allocate buffer for SMU internal buffer */
  631. ret = amdgpu_bo_create(adev, smu_internal_buffer_size, PAGE_SIZE,
  632. true, AMDGPU_GEM_DOMAIN_VRAM,
  633. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  634. NULL, NULL, smu_buf);
  635. if (ret) {
  636. DRM_ERROR("Failed to allocate memory for SMU internal buffer\n");
  637. return -ENOMEM;
  638. }
  639. /* Retrieve GPU address for header buffer and internal buffer */
  640. ret = amdgpu_bo_reserve(adev->smu.toc_buf, false);
  641. if (ret) {
  642. amdgpu_bo_unref(&adev->smu.toc_buf);
  643. DRM_ERROR("Failed to reserve the TOC buffer\n");
  644. return -EINVAL;
  645. }
  646. ret = amdgpu_bo_pin(adev->smu.toc_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  647. if (ret) {
  648. amdgpu_bo_unreserve(adev->smu.toc_buf);
  649. amdgpu_bo_unref(&adev->smu.toc_buf);
  650. DRM_ERROR("Failed to pin the TOC buffer\n");
  651. return -EINVAL;
  652. }
  653. ret = amdgpu_bo_kmap(*toc_buf, &toc_buf_ptr);
  654. if (ret) {
  655. amdgpu_bo_unreserve(adev->smu.toc_buf);
  656. amdgpu_bo_unref(&adev->smu.toc_buf);
  657. DRM_ERROR("Failed to map the TOC buffer\n");
  658. return -EINVAL;
  659. }
  660. amdgpu_bo_unreserve(adev->smu.toc_buf);
  661. private->header_addr_low = lower_32_bits(mc_addr);
  662. private->header_addr_high = upper_32_bits(mc_addr);
  663. private->header = toc_buf_ptr;
  664. ret = amdgpu_bo_reserve(adev->smu.smu_buf, false);
  665. if (ret) {
  666. amdgpu_bo_unref(&adev->smu.smu_buf);
  667. amdgpu_bo_unref(&adev->smu.toc_buf);
  668. DRM_ERROR("Failed to reserve the SMU internal buffer\n");
  669. return -EINVAL;
  670. }
  671. ret = amdgpu_bo_pin(adev->smu.smu_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  672. if (ret) {
  673. amdgpu_bo_unreserve(adev->smu.smu_buf);
  674. amdgpu_bo_unref(&adev->smu.smu_buf);
  675. amdgpu_bo_unref(&adev->smu.toc_buf);
  676. DRM_ERROR("Failed to pin the SMU internal buffer\n");
  677. return -EINVAL;
  678. }
  679. ret = amdgpu_bo_kmap(*smu_buf, &smu_buf_ptr);
  680. if (ret) {
  681. amdgpu_bo_unreserve(adev->smu.smu_buf);
  682. amdgpu_bo_unref(&adev->smu.smu_buf);
  683. amdgpu_bo_unref(&adev->smu.toc_buf);
  684. DRM_ERROR("Failed to map the SMU internal buffer\n");
  685. return -EINVAL;
  686. }
  687. amdgpu_bo_unreserve(adev->smu.smu_buf);
  688. private->smu_buffer_addr_low = lower_32_bits(mc_addr);
  689. private->smu_buffer_addr_high = upper_32_bits(mc_addr);
  690. adev->smu.smumgr_funcs = &fiji_smumgr_funcs;
  691. return 0;
  692. }
  693. int fiji_smu_fini(struct amdgpu_device *adev)
  694. {
  695. amdgpu_bo_unref(&adev->smu.toc_buf);
  696. amdgpu_bo_unref(&adev->smu.smu_buf);
  697. kfree(adev->smu.priv);
  698. adev->smu.priv = NULL;
  699. if (adev->firmware.fw_buf)
  700. amdgpu_ucode_fini_bo(adev);
  701. return 0;
  702. }