tonga_smc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 "tonga_ppsmc.h"
  27. #include "tonga_smum.h"
  28. #include "smu_ucode_xfer_vi.h"
  29. #include "amdgpu_ucode.h"
  30. #include "smu/smu_7_1_2_d.h"
  31. #include "smu/smu_7_1_2_sh_mask.h"
  32. #define TONGA_SMC_SIZE 0x20000
  33. static int tonga_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 tonga_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 = tonga_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 = tonga_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 = tonga_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 tonga_program_jump_on_start(struct amdgpu_device *adev)
  94. {
  95. static unsigned char data[] = {0xE0, 0x00, 0x80, 0x40};
  96. tonga_copy_bytes_to_smc(adev, 0x0, data, 4, sizeof(data)+1);
  97. return 0;
  98. }
  99. static bool tonga_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 tonga_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 tonga_send_msg_to_smc(struct amdgpu_device *adev, PPSMC_Msg msg)
  134. {
  135. if (!tonga_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 tonga_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 tonga_send_msg_to_smc_with_parameter(struct amdgpu_device *adev,
  161. PPSMC_Msg msg,
  162. uint32_t parameter)
  163. {
  164. if (!tonga_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 tonga_send_msg_to_smc(adev, msg);
  172. }
  173. static int tonga_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 tonga_send_msg_to_smc_without_waiting(adev, msg);
  183. }
  184. #if 0 /* not used yet */
  185. static int tonga_wait_for_smc_inactive(struct amdgpu_device *adev)
  186. {
  187. int i;
  188. uint32_t val;
  189. if (!tonga_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 tonga_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 > TONGA_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 tonga_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 = tonga_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 tonga_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 = tonga_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 tonga_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 tonga_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. return AMDGPU_UCODE_ID_CP_MEC1;
  305. case UCODE_ID_CP_MEC_JT2:
  306. return AMDGPU_UCODE_ID_CP_MEC2;
  307. case UCODE_ID_RLC_G:
  308. return AMDGPU_UCODE_ID_RLC_G;
  309. default:
  310. DRM_ERROR("ucode type is out of range!\n");
  311. return AMDGPU_UCODE_ID_MAXIMUM;
  312. }
  313. }
  314. static int tonga_smu_populate_single_firmware_entry(struct amdgpu_device *adev,
  315. uint32_t fw_type,
  316. struct SMU_Entry *entry)
  317. {
  318. enum AMDGPU_UCODE_ID id = tonga_convert_fw_type(fw_type);
  319. struct amdgpu_firmware_info *ucode = &adev->firmware.ucode[id];
  320. const struct gfx_firmware_header_v1_0 *header = NULL;
  321. uint64_t gpu_addr;
  322. uint32_t data_size;
  323. if (ucode->fw == NULL)
  324. return -EINVAL;
  325. gpu_addr = ucode->mc_addr;
  326. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  327. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  328. if ((fw_type == UCODE_ID_CP_MEC_JT1) ||
  329. (fw_type == UCODE_ID_CP_MEC_JT2)) {
  330. gpu_addr += le32_to_cpu(header->jt_offset) << 2;
  331. data_size = le32_to_cpu(header->jt_size) << 2;
  332. }
  333. entry->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  334. entry->id = (uint16_t)fw_type;
  335. entry->image_addr_high = upper_32_bits(gpu_addr);
  336. entry->image_addr_low = lower_32_bits(gpu_addr);
  337. entry->meta_data_addr_high = 0;
  338. entry->meta_data_addr_low = 0;
  339. entry->data_size_byte = data_size;
  340. entry->num_register_entries = 0;
  341. if (fw_type == UCODE_ID_RLC_G)
  342. entry->flags = 1;
  343. else
  344. entry->flags = 0;
  345. return 0;
  346. }
  347. static int tonga_smu_request_load_fw(struct amdgpu_device *adev)
  348. {
  349. struct tonga_smu_private_data *private = (struct tonga_smu_private_data *)adev->smu.priv;
  350. struct SMU_DRAMData_TOC *toc;
  351. uint32_t fw_to_load;
  352. WREG32_SMC(ixSOFT_REGISTERS_TABLE_28, 0);
  353. tonga_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_SMU_DRAM_ADDR_HI, private->smu_buffer_addr_high);
  354. tonga_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_SMU_DRAM_ADDR_LO, private->smu_buffer_addr_low);
  355. toc = (struct SMU_DRAMData_TOC *)private->header;
  356. toc->num_entries = 0;
  357. toc->structure_version = 1;
  358. if (!adev->firmware.smu_load)
  359. return 0;
  360. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_RLC_G,
  361. &toc->entry[toc->num_entries++])) {
  362. DRM_ERROR("Failed to get firmware entry for RLC\n");
  363. return -EINVAL;
  364. }
  365. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_CE,
  366. &toc->entry[toc->num_entries++])) {
  367. DRM_ERROR("Failed to get firmware entry for CE\n");
  368. return -EINVAL;
  369. }
  370. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_PFP,
  371. &toc->entry[toc->num_entries++])) {
  372. DRM_ERROR("Failed to get firmware entry for PFP\n");
  373. return -EINVAL;
  374. }
  375. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_ME,
  376. &toc->entry[toc->num_entries++])) {
  377. DRM_ERROR("Failed to get firmware entry for ME\n");
  378. return -EINVAL;
  379. }
  380. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC,
  381. &toc->entry[toc->num_entries++])) {
  382. DRM_ERROR("Failed to get firmware entry for MEC\n");
  383. return -EINVAL;
  384. }
  385. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT1,
  386. &toc->entry[toc->num_entries++])) {
  387. DRM_ERROR("Failed to get firmware entry for MEC_JT1\n");
  388. return -EINVAL;
  389. }
  390. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_CP_MEC_JT2,
  391. &toc->entry[toc->num_entries++])) {
  392. DRM_ERROR("Failed to get firmware entry for MEC_JT2\n");
  393. return -EINVAL;
  394. }
  395. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA0,
  396. &toc->entry[toc->num_entries++])) {
  397. DRM_ERROR("Failed to get firmware entry for SDMA0\n");
  398. return -EINVAL;
  399. }
  400. if (tonga_smu_populate_single_firmware_entry(adev, UCODE_ID_SDMA1,
  401. &toc->entry[toc->num_entries++])) {
  402. DRM_ERROR("Failed to get firmware entry for SDMA1\n");
  403. return -EINVAL;
  404. }
  405. tonga_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_HI, private->header_addr_high);
  406. tonga_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DRV_DRAM_ADDR_LO, private->header_addr_low);
  407. fw_to_load = UCODE_ID_RLC_G_MASK |
  408. UCODE_ID_SDMA0_MASK |
  409. UCODE_ID_SDMA1_MASK |
  410. UCODE_ID_CP_CE_MASK |
  411. UCODE_ID_CP_ME_MASK |
  412. UCODE_ID_CP_PFP_MASK |
  413. UCODE_ID_CP_MEC_MASK;
  414. if (tonga_send_msg_to_smc_with_parameter_without_waiting(adev, PPSMC_MSG_LoadUcodes, fw_to_load)) {
  415. DRM_ERROR("Fail to request SMU load ucode\n");
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. static uint32_t tonga_smu_get_mask_for_fw_type(uint32_t fw_type)
  421. {
  422. switch (fw_type) {
  423. case AMDGPU_UCODE_ID_SDMA0:
  424. return UCODE_ID_SDMA0_MASK;
  425. case AMDGPU_UCODE_ID_SDMA1:
  426. return UCODE_ID_SDMA1_MASK;
  427. case AMDGPU_UCODE_ID_CP_CE:
  428. return UCODE_ID_CP_CE_MASK;
  429. case AMDGPU_UCODE_ID_CP_PFP:
  430. return UCODE_ID_CP_PFP_MASK;
  431. case AMDGPU_UCODE_ID_CP_ME:
  432. return UCODE_ID_CP_ME_MASK;
  433. case AMDGPU_UCODE_ID_CP_MEC1:
  434. return UCODE_ID_CP_MEC_MASK;
  435. case AMDGPU_UCODE_ID_CP_MEC2:
  436. return UCODE_ID_CP_MEC_MASK;
  437. case AMDGPU_UCODE_ID_RLC_G:
  438. return UCODE_ID_RLC_G_MASK;
  439. default:
  440. DRM_ERROR("ucode type is out of range!\n");
  441. return 0;
  442. }
  443. }
  444. static int tonga_smu_check_fw_load_finish(struct amdgpu_device *adev,
  445. uint32_t fw_type)
  446. {
  447. uint32_t fw_mask = tonga_smu_get_mask_for_fw_type(fw_type);
  448. int i;
  449. for (i = 0; i < adev->usec_timeout; i++) {
  450. if (fw_mask == (RREG32_SMC(ixSOFT_REGISTERS_TABLE_28) & fw_mask))
  451. break;
  452. udelay(1);
  453. }
  454. if (i == adev->usec_timeout) {
  455. DRM_ERROR("check firmware loading failed\n");
  456. return -EINVAL;
  457. }
  458. return 0;
  459. }
  460. static int tonga_smu_start_in_protection_mode(struct amdgpu_device *adev)
  461. {
  462. int result;
  463. uint32_t val;
  464. int i;
  465. /* Assert reset */
  466. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  467. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  468. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  469. result = tonga_smu_upload_firmware_image(adev);
  470. if (result)
  471. return result;
  472. /* Clear status */
  473. WREG32_SMC(ixSMU_STATUS, 0);
  474. /* Enable clock */
  475. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  476. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
  477. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  478. /* De-assert reset */
  479. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  480. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  481. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  482. /* Set SMU Auto Start */
  483. val = RREG32_SMC(ixSMU_INPUT_DATA);
  484. val = REG_SET_FIELD(val, SMU_INPUT_DATA, AUTO_START, 1);
  485. WREG32_SMC(ixSMU_INPUT_DATA, val);
  486. /* Clear firmware interrupt enable flag */
  487. WREG32_SMC(ixFIRMWARE_FLAGS, 0);
  488. for (i = 0; i < adev->usec_timeout; i++) {
  489. val = RREG32_SMC(ixRCU_UC_EVENTS);
  490. if (REG_GET_FIELD(val, RCU_UC_EVENTS, INTERRUPTS_ENABLED))
  491. break;
  492. udelay(1);
  493. }
  494. if (i == adev->usec_timeout) {
  495. DRM_ERROR("Interrupt is not enabled by firmware\n");
  496. return -EINVAL;
  497. }
  498. /* Call Test SMU message with 0x20000 offset
  499. * to trigger SMU start
  500. */
  501. tonga_send_msg_to_smc_offset(adev);
  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. /* Wait for firmware to initialize */
  520. for (i = 0; i < adev->usec_timeout; i++) {
  521. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  522. if(REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED))
  523. break;
  524. udelay(1);
  525. }
  526. if (i == adev->usec_timeout) {
  527. DRM_ERROR("SMU firmware initialization failed\n");
  528. return -EINVAL;
  529. }
  530. return 0;
  531. }
  532. static int tonga_smu_start_in_non_protection_mode(struct amdgpu_device *adev)
  533. {
  534. int i, result;
  535. uint32_t val;
  536. /* wait for smc boot up */
  537. for (i = 0; i < adev->usec_timeout; i++) {
  538. val = RREG32_SMC(ixRCU_UC_EVENTS);
  539. val = REG_GET_FIELD(val, RCU_UC_EVENTS, boot_seq_done);
  540. if (val)
  541. break;
  542. udelay(1);
  543. }
  544. if (i == adev->usec_timeout) {
  545. DRM_ERROR("SMC boot sequence is not completed\n");
  546. return -EINVAL;
  547. }
  548. /* Clear firmware interrupt enable flag */
  549. WREG32_SMC(ixFIRMWARE_FLAGS, 0);
  550. /* Assert reset */
  551. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  552. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 1);
  553. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  554. result = tonga_smu_upload_firmware_image(adev);
  555. if (result)
  556. return result;
  557. /* Set smc instruct start point at 0x0 */
  558. tonga_program_jump_on_start(adev);
  559. /* Enable clock */
  560. val = RREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0);
  561. val = REG_SET_FIELD(val, SMC_SYSCON_CLOCK_CNTL_0, ck_disable, 0);
  562. WREG32_SMC(ixSMC_SYSCON_CLOCK_CNTL_0, val);
  563. /* De-assert reset */
  564. val = RREG32_SMC(ixSMC_SYSCON_RESET_CNTL);
  565. val = REG_SET_FIELD(val, SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  566. WREG32_SMC(ixSMC_SYSCON_RESET_CNTL, val);
  567. /* Wait for firmware to initialize */
  568. for (i = 0; i < adev->usec_timeout; i++) {
  569. val = RREG32_SMC(ixFIRMWARE_FLAGS);
  570. if (REG_GET_FIELD(val, FIRMWARE_FLAGS, INTERRUPTS_ENABLED))
  571. break;
  572. udelay(1);
  573. }
  574. if (i == adev->usec_timeout) {
  575. DRM_ERROR("Timeout for SMC firmware initialization\n");
  576. return -EINVAL;
  577. }
  578. return 0;
  579. }
  580. int tonga_smu_start(struct amdgpu_device *adev)
  581. {
  582. int result;
  583. uint32_t val;
  584. if (!tonga_is_smc_ram_running(adev)) {
  585. val = RREG32_SMC(ixSMU_FIRMWARE);
  586. if (!REG_GET_FIELD(val, SMU_FIRMWARE, SMU_MODE)) {
  587. result = tonga_smu_start_in_non_protection_mode(adev);
  588. if (result)
  589. return result;
  590. } else {
  591. result = tonga_smu_start_in_protection_mode(adev);
  592. if (result)
  593. return result;
  594. }
  595. }
  596. return tonga_smu_request_load_fw(adev);
  597. }
  598. static const struct amdgpu_smumgr_funcs tonga_smumgr_funcs = {
  599. .check_fw_load_finish = tonga_smu_check_fw_load_finish,
  600. .request_smu_load_fw = NULL,
  601. .request_smu_specific_fw = NULL,
  602. };
  603. int tonga_smu_init(struct amdgpu_device *adev)
  604. {
  605. struct tonga_smu_private_data *private;
  606. uint32_t image_size = ((sizeof(struct SMU_DRAMData_TOC) / 4096) + 1) * 4096;
  607. uint32_t smu_internal_buffer_size = 200*4096;
  608. struct amdgpu_bo **toc_buf = &adev->smu.toc_buf;
  609. struct amdgpu_bo **smu_buf = &adev->smu.smu_buf;
  610. uint64_t mc_addr;
  611. void *toc_buf_ptr;
  612. void *smu_buf_ptr;
  613. int ret;
  614. private = kzalloc(sizeof(struct tonga_smu_private_data), GFP_KERNEL);
  615. if (NULL == private)
  616. return -ENOMEM;
  617. /* allocate firmware buffers */
  618. if (adev->firmware.smu_load)
  619. amdgpu_ucode_init_bo(adev);
  620. adev->smu.priv = private;
  621. adev->smu.fw_flags = 0;
  622. /* Allocate FW image data structure and header buffer */
  623. ret = amdgpu_bo_create(adev, image_size, PAGE_SIZE,
  624. true, AMDGPU_GEM_DOMAIN_VRAM,
  625. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  626. NULL, NULL, toc_buf);
  627. if (ret) {
  628. DRM_ERROR("Failed to allocate memory for TOC buffer\n");
  629. return -ENOMEM;
  630. }
  631. /* Allocate buffer for SMU internal buffer */
  632. ret = amdgpu_bo_create(adev, smu_internal_buffer_size, PAGE_SIZE,
  633. true, AMDGPU_GEM_DOMAIN_VRAM,
  634. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
  635. NULL, NULL, smu_buf);
  636. if (ret) {
  637. DRM_ERROR("Failed to allocate memory for SMU internal buffer\n");
  638. return -ENOMEM;
  639. }
  640. /* Retrieve GPU address for header buffer and internal buffer */
  641. ret = amdgpu_bo_reserve(adev->smu.toc_buf, false);
  642. if (ret) {
  643. amdgpu_bo_unref(&adev->smu.toc_buf);
  644. DRM_ERROR("Failed to reserve the TOC buffer\n");
  645. return -EINVAL;
  646. }
  647. ret = amdgpu_bo_pin(adev->smu.toc_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  648. if (ret) {
  649. amdgpu_bo_unreserve(adev->smu.toc_buf);
  650. amdgpu_bo_unref(&adev->smu.toc_buf);
  651. DRM_ERROR("Failed to pin the TOC buffer\n");
  652. return -EINVAL;
  653. }
  654. ret = amdgpu_bo_kmap(*toc_buf, &toc_buf_ptr);
  655. if (ret) {
  656. amdgpu_bo_unreserve(adev->smu.toc_buf);
  657. amdgpu_bo_unref(&adev->smu.toc_buf);
  658. DRM_ERROR("Failed to map the TOC buffer\n");
  659. return -EINVAL;
  660. }
  661. amdgpu_bo_unreserve(adev->smu.toc_buf);
  662. private->header_addr_low = lower_32_bits(mc_addr);
  663. private->header_addr_high = upper_32_bits(mc_addr);
  664. private->header = toc_buf_ptr;
  665. ret = amdgpu_bo_reserve(adev->smu.smu_buf, false);
  666. if (ret) {
  667. amdgpu_bo_unref(&adev->smu.smu_buf);
  668. amdgpu_bo_unref(&adev->smu.toc_buf);
  669. DRM_ERROR("Failed to reserve the SMU internal buffer\n");
  670. return -EINVAL;
  671. }
  672. ret = amdgpu_bo_pin(adev->smu.smu_buf, AMDGPU_GEM_DOMAIN_VRAM, &mc_addr);
  673. if (ret) {
  674. amdgpu_bo_unreserve(adev->smu.smu_buf);
  675. amdgpu_bo_unref(&adev->smu.smu_buf);
  676. amdgpu_bo_unref(&adev->smu.toc_buf);
  677. DRM_ERROR("Failed to pin the SMU internal buffer\n");
  678. return -EINVAL;
  679. }
  680. ret = amdgpu_bo_kmap(*smu_buf, &smu_buf_ptr);
  681. if (ret) {
  682. amdgpu_bo_unreserve(adev->smu.smu_buf);
  683. amdgpu_bo_unref(&adev->smu.smu_buf);
  684. amdgpu_bo_unref(&adev->smu.toc_buf);
  685. DRM_ERROR("Failed to map the SMU internal buffer\n");
  686. return -EINVAL;
  687. }
  688. amdgpu_bo_unreserve(adev->smu.smu_buf);
  689. private->smu_buffer_addr_low = lower_32_bits(mc_addr);
  690. private->smu_buffer_addr_high = upper_32_bits(mc_addr);
  691. adev->smu.smumgr_funcs = &tonga_smumgr_funcs;
  692. return 0;
  693. }
  694. int tonga_smu_fini(struct amdgpu_device *adev)
  695. {
  696. amdgpu_bo_unref(&adev->smu.toc_buf);
  697. amdgpu_bo_unref(&adev->smu.smu_buf);
  698. kfree(adev->smu.priv);
  699. adev->smu.priv = NULL;
  700. if (adev->firmware.fw_buf)
  701. amdgpu_ucode_fini_bo(adev);
  702. return 0;
  703. }