amdgpu_cgs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright 2015 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. */
  24. #include <linux/list.h>
  25. #include <linux/slab.h>
  26. #include <drm/drmP.h>
  27. #include <linux/firmware.h>
  28. #include <drm/amdgpu_drm.h>
  29. #include "amdgpu.h"
  30. #include "atom.h"
  31. #include "amdgpu_ucode.h"
  32. struct amdgpu_cgs_device {
  33. struct cgs_device base;
  34. struct amdgpu_device *adev;
  35. };
  36. #define CGS_FUNC_ADEV \
  37. struct amdgpu_device *adev = \
  38. ((struct amdgpu_cgs_device *)cgs_device)->adev
  39. static uint32_t amdgpu_cgs_read_register(struct cgs_device *cgs_device, unsigned offset)
  40. {
  41. CGS_FUNC_ADEV;
  42. return RREG32(offset);
  43. }
  44. static void amdgpu_cgs_write_register(struct cgs_device *cgs_device, unsigned offset,
  45. uint32_t value)
  46. {
  47. CGS_FUNC_ADEV;
  48. WREG32(offset, value);
  49. }
  50. static uint32_t amdgpu_cgs_read_ind_register(struct cgs_device *cgs_device,
  51. enum cgs_ind_reg space,
  52. unsigned index)
  53. {
  54. CGS_FUNC_ADEV;
  55. switch (space) {
  56. case CGS_IND_REG__MMIO:
  57. return RREG32_IDX(index);
  58. case CGS_IND_REG__PCIE:
  59. return RREG32_PCIE(index);
  60. case CGS_IND_REG__SMC:
  61. return RREG32_SMC(index);
  62. case CGS_IND_REG__UVD_CTX:
  63. return RREG32_UVD_CTX(index);
  64. case CGS_IND_REG__DIDT:
  65. return RREG32_DIDT(index);
  66. case CGS_IND_REG_GC_CAC:
  67. return RREG32_GC_CAC(index);
  68. case CGS_IND_REG_SE_CAC:
  69. return RREG32_SE_CAC(index);
  70. case CGS_IND_REG__AUDIO_ENDPT:
  71. DRM_ERROR("audio endpt register access not implemented.\n");
  72. return 0;
  73. }
  74. WARN(1, "Invalid indirect register space");
  75. return 0;
  76. }
  77. static void amdgpu_cgs_write_ind_register(struct cgs_device *cgs_device,
  78. enum cgs_ind_reg space,
  79. unsigned index, uint32_t value)
  80. {
  81. CGS_FUNC_ADEV;
  82. switch (space) {
  83. case CGS_IND_REG__MMIO:
  84. return WREG32_IDX(index, value);
  85. case CGS_IND_REG__PCIE:
  86. return WREG32_PCIE(index, value);
  87. case CGS_IND_REG__SMC:
  88. return WREG32_SMC(index, value);
  89. case CGS_IND_REG__UVD_CTX:
  90. return WREG32_UVD_CTX(index, value);
  91. case CGS_IND_REG__DIDT:
  92. return WREG32_DIDT(index, value);
  93. case CGS_IND_REG_GC_CAC:
  94. return WREG32_GC_CAC(index, value);
  95. case CGS_IND_REG_SE_CAC:
  96. return WREG32_SE_CAC(index, value);
  97. case CGS_IND_REG__AUDIO_ENDPT:
  98. DRM_ERROR("audio endpt register access not implemented.\n");
  99. return;
  100. }
  101. WARN(1, "Invalid indirect register space");
  102. }
  103. static uint32_t fw_type_convert(struct cgs_device *cgs_device, uint32_t fw_type)
  104. {
  105. CGS_FUNC_ADEV;
  106. enum AMDGPU_UCODE_ID result = AMDGPU_UCODE_ID_MAXIMUM;
  107. switch (fw_type) {
  108. case CGS_UCODE_ID_SDMA0:
  109. result = AMDGPU_UCODE_ID_SDMA0;
  110. break;
  111. case CGS_UCODE_ID_SDMA1:
  112. result = AMDGPU_UCODE_ID_SDMA1;
  113. break;
  114. case CGS_UCODE_ID_CP_CE:
  115. result = AMDGPU_UCODE_ID_CP_CE;
  116. break;
  117. case CGS_UCODE_ID_CP_PFP:
  118. result = AMDGPU_UCODE_ID_CP_PFP;
  119. break;
  120. case CGS_UCODE_ID_CP_ME:
  121. result = AMDGPU_UCODE_ID_CP_ME;
  122. break;
  123. case CGS_UCODE_ID_CP_MEC:
  124. case CGS_UCODE_ID_CP_MEC_JT1:
  125. result = AMDGPU_UCODE_ID_CP_MEC1;
  126. break;
  127. case CGS_UCODE_ID_CP_MEC_JT2:
  128. /* for VI. JT2 should be the same as JT1, because:
  129. 1, MEC2 and MEC1 use exactly same FW.
  130. 2, JT2 is not pached but JT1 is.
  131. */
  132. if (adev->asic_type >= CHIP_TOPAZ)
  133. result = AMDGPU_UCODE_ID_CP_MEC1;
  134. else
  135. result = AMDGPU_UCODE_ID_CP_MEC2;
  136. break;
  137. case CGS_UCODE_ID_RLC_G:
  138. result = AMDGPU_UCODE_ID_RLC_G;
  139. break;
  140. case CGS_UCODE_ID_STORAGE:
  141. result = AMDGPU_UCODE_ID_STORAGE;
  142. break;
  143. default:
  144. DRM_ERROR("Firmware type not supported\n");
  145. }
  146. return result;
  147. }
  148. static uint16_t amdgpu_get_firmware_version(struct cgs_device *cgs_device,
  149. enum cgs_ucode_id type)
  150. {
  151. CGS_FUNC_ADEV;
  152. uint16_t fw_version = 0;
  153. switch (type) {
  154. case CGS_UCODE_ID_SDMA0:
  155. fw_version = adev->sdma.instance[0].fw_version;
  156. break;
  157. case CGS_UCODE_ID_SDMA1:
  158. fw_version = adev->sdma.instance[1].fw_version;
  159. break;
  160. case CGS_UCODE_ID_CP_CE:
  161. fw_version = adev->gfx.ce_fw_version;
  162. break;
  163. case CGS_UCODE_ID_CP_PFP:
  164. fw_version = adev->gfx.pfp_fw_version;
  165. break;
  166. case CGS_UCODE_ID_CP_ME:
  167. fw_version = adev->gfx.me_fw_version;
  168. break;
  169. case CGS_UCODE_ID_CP_MEC:
  170. fw_version = adev->gfx.mec_fw_version;
  171. break;
  172. case CGS_UCODE_ID_CP_MEC_JT1:
  173. fw_version = adev->gfx.mec_fw_version;
  174. break;
  175. case CGS_UCODE_ID_CP_MEC_JT2:
  176. fw_version = adev->gfx.mec_fw_version;
  177. break;
  178. case CGS_UCODE_ID_RLC_G:
  179. fw_version = adev->gfx.rlc_fw_version;
  180. break;
  181. case CGS_UCODE_ID_STORAGE:
  182. break;
  183. default:
  184. DRM_ERROR("firmware type %d do not have version\n", type);
  185. break;
  186. }
  187. return fw_version;
  188. }
  189. static int amdgpu_cgs_get_firmware_info(struct cgs_device *cgs_device,
  190. enum cgs_ucode_id type,
  191. struct cgs_firmware_info *info)
  192. {
  193. CGS_FUNC_ADEV;
  194. if ((CGS_UCODE_ID_SMU != type) && (CGS_UCODE_ID_SMU_SK != type)) {
  195. uint64_t gpu_addr;
  196. uint32_t data_size;
  197. const struct gfx_firmware_header_v1_0 *header;
  198. enum AMDGPU_UCODE_ID id;
  199. struct amdgpu_firmware_info *ucode;
  200. id = fw_type_convert(cgs_device, type);
  201. ucode = &adev->firmware.ucode[id];
  202. if (ucode->fw == NULL)
  203. return -EINVAL;
  204. gpu_addr = ucode->mc_addr;
  205. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  206. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  207. if ((type == CGS_UCODE_ID_CP_MEC_JT1) ||
  208. (type == CGS_UCODE_ID_CP_MEC_JT2)) {
  209. gpu_addr += ALIGN(le32_to_cpu(header->header.ucode_size_bytes), PAGE_SIZE);
  210. data_size = le32_to_cpu(header->jt_size) << 2;
  211. }
  212. info->kptr = ucode->kaddr;
  213. info->image_size = data_size;
  214. info->mc_addr = gpu_addr;
  215. info->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  216. if (CGS_UCODE_ID_CP_MEC == type)
  217. info->image_size = le32_to_cpu(header->jt_offset) << 2;
  218. info->fw_version = amdgpu_get_firmware_version(cgs_device, type);
  219. info->feature_version = (uint16_t)le32_to_cpu(header->ucode_feature_version);
  220. } else {
  221. char fw_name[30] = {0};
  222. int err = 0;
  223. uint32_t ucode_size;
  224. uint32_t ucode_start_address;
  225. const uint8_t *src;
  226. const struct smc_firmware_header_v1_0 *hdr;
  227. const struct common_firmware_header *header;
  228. struct amdgpu_firmware_info *ucode = NULL;
  229. if (!adev->pm.fw) {
  230. switch (adev->asic_type) {
  231. case CHIP_TAHITI:
  232. strcpy(fw_name, "radeon/tahiti_smc.bin");
  233. break;
  234. case CHIP_PITCAIRN:
  235. if ((adev->pdev->revision == 0x81) &&
  236. ((adev->pdev->device == 0x6810) ||
  237. (adev->pdev->device == 0x6811))) {
  238. info->is_kicker = true;
  239. strcpy(fw_name, "radeon/pitcairn_k_smc.bin");
  240. } else {
  241. strcpy(fw_name, "radeon/pitcairn_smc.bin");
  242. }
  243. break;
  244. case CHIP_VERDE:
  245. if (((adev->pdev->device == 0x6820) &&
  246. ((adev->pdev->revision == 0x81) ||
  247. (adev->pdev->revision == 0x83))) ||
  248. ((adev->pdev->device == 0x6821) &&
  249. ((adev->pdev->revision == 0x83) ||
  250. (adev->pdev->revision == 0x87))) ||
  251. ((adev->pdev->revision == 0x87) &&
  252. ((adev->pdev->device == 0x6823) ||
  253. (adev->pdev->device == 0x682b)))) {
  254. info->is_kicker = true;
  255. strcpy(fw_name, "radeon/verde_k_smc.bin");
  256. } else {
  257. strcpy(fw_name, "radeon/verde_smc.bin");
  258. }
  259. break;
  260. case CHIP_OLAND:
  261. if (((adev->pdev->revision == 0x81) &&
  262. ((adev->pdev->device == 0x6600) ||
  263. (adev->pdev->device == 0x6604) ||
  264. (adev->pdev->device == 0x6605) ||
  265. (adev->pdev->device == 0x6610))) ||
  266. ((adev->pdev->revision == 0x83) &&
  267. (adev->pdev->device == 0x6610))) {
  268. info->is_kicker = true;
  269. strcpy(fw_name, "radeon/oland_k_smc.bin");
  270. } else {
  271. strcpy(fw_name, "radeon/oland_smc.bin");
  272. }
  273. break;
  274. case CHIP_HAINAN:
  275. if (((adev->pdev->revision == 0x81) &&
  276. (adev->pdev->device == 0x6660)) ||
  277. ((adev->pdev->revision == 0x83) &&
  278. ((adev->pdev->device == 0x6660) ||
  279. (adev->pdev->device == 0x6663) ||
  280. (adev->pdev->device == 0x6665) ||
  281. (adev->pdev->device == 0x6667)))) {
  282. info->is_kicker = true;
  283. strcpy(fw_name, "radeon/hainan_k_smc.bin");
  284. } else if ((adev->pdev->revision == 0xc3) &&
  285. (adev->pdev->device == 0x6665)) {
  286. info->is_kicker = true;
  287. strcpy(fw_name, "radeon/banks_k_2_smc.bin");
  288. } else {
  289. strcpy(fw_name, "radeon/hainan_smc.bin");
  290. }
  291. break;
  292. case CHIP_BONAIRE:
  293. if ((adev->pdev->revision == 0x80) ||
  294. (adev->pdev->revision == 0x81) ||
  295. (adev->pdev->device == 0x665f)) {
  296. info->is_kicker = true;
  297. strcpy(fw_name, "amdgpu/bonaire_k_smc.bin");
  298. } else {
  299. strcpy(fw_name, "amdgpu/bonaire_smc.bin");
  300. }
  301. break;
  302. case CHIP_HAWAII:
  303. if (adev->pdev->revision == 0x80) {
  304. info->is_kicker = true;
  305. strcpy(fw_name, "amdgpu/hawaii_k_smc.bin");
  306. } else {
  307. strcpy(fw_name, "amdgpu/hawaii_smc.bin");
  308. }
  309. break;
  310. case CHIP_TOPAZ:
  311. if (((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x81)) ||
  312. ((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x83)) ||
  313. ((adev->pdev->device == 0x6907) && (adev->pdev->revision == 0x87))) {
  314. info->is_kicker = true;
  315. strcpy(fw_name, "amdgpu/topaz_k_smc.bin");
  316. } else
  317. strcpy(fw_name, "amdgpu/topaz_smc.bin");
  318. break;
  319. case CHIP_TONGA:
  320. if (((adev->pdev->device == 0x6939) && (adev->pdev->revision == 0xf1)) ||
  321. ((adev->pdev->device == 0x6938) && (adev->pdev->revision == 0xf1))) {
  322. info->is_kicker = true;
  323. strcpy(fw_name, "amdgpu/tonga_k_smc.bin");
  324. } else
  325. strcpy(fw_name, "amdgpu/tonga_smc.bin");
  326. break;
  327. case CHIP_FIJI:
  328. strcpy(fw_name, "amdgpu/fiji_smc.bin");
  329. break;
  330. case CHIP_POLARIS11:
  331. if (type == CGS_UCODE_ID_SMU) {
  332. if (((adev->pdev->device == 0x67ef) &&
  333. ((adev->pdev->revision == 0xe0) ||
  334. (adev->pdev->revision == 0xe2) ||
  335. (adev->pdev->revision == 0xe5))) ||
  336. ((adev->pdev->device == 0x67ff) &&
  337. ((adev->pdev->revision == 0xcf) ||
  338. (adev->pdev->revision == 0xef) ||
  339. (adev->pdev->revision == 0xff)))) {
  340. info->is_kicker = true;
  341. strcpy(fw_name, "amdgpu/polaris11_k_smc.bin");
  342. } else
  343. strcpy(fw_name, "amdgpu/polaris11_smc.bin");
  344. } else if (type == CGS_UCODE_ID_SMU_SK) {
  345. strcpy(fw_name, "amdgpu/polaris11_smc_sk.bin");
  346. }
  347. break;
  348. case CHIP_POLARIS10:
  349. if (type == CGS_UCODE_ID_SMU) {
  350. if (((adev->pdev->device == 0x67df) &&
  351. ((adev->pdev->revision == 0xe0) ||
  352. (adev->pdev->revision == 0xe3) ||
  353. (adev->pdev->revision == 0xe4) ||
  354. (adev->pdev->revision == 0xe5) ||
  355. (adev->pdev->revision == 0xe7) ||
  356. (adev->pdev->revision == 0xef))) ||
  357. ((adev->pdev->device == 0x6fdf) &&
  358. (adev->pdev->revision == 0xef))) {
  359. info->is_kicker = true;
  360. strcpy(fw_name, "amdgpu/polaris10_k_smc.bin");
  361. } else
  362. strcpy(fw_name, "amdgpu/polaris10_smc.bin");
  363. } else if (type == CGS_UCODE_ID_SMU_SK) {
  364. strcpy(fw_name, "amdgpu/polaris10_smc_sk.bin");
  365. }
  366. break;
  367. case CHIP_POLARIS12:
  368. strcpy(fw_name, "amdgpu/polaris12_smc.bin");
  369. break;
  370. case CHIP_VEGAM:
  371. strcpy(fw_name, "amdgpu/vegam_smc.bin");
  372. break;
  373. case CHIP_VEGA10:
  374. if ((adev->pdev->device == 0x687f) &&
  375. ((adev->pdev->revision == 0xc0) ||
  376. (adev->pdev->revision == 0xc1) ||
  377. (adev->pdev->revision == 0xc3)))
  378. strcpy(fw_name, "amdgpu/vega10_acg_smc.bin");
  379. else
  380. strcpy(fw_name, "amdgpu/vega10_smc.bin");
  381. break;
  382. case CHIP_VEGA12:
  383. strcpy(fw_name, "amdgpu/vega12_smc.bin");
  384. break;
  385. case CHIP_VEGA20:
  386. strcpy(fw_name, "amdgpu/vega20_smc.bin");
  387. break;
  388. default:
  389. DRM_ERROR("SMC firmware not supported\n");
  390. return -EINVAL;
  391. }
  392. err = request_firmware(&adev->pm.fw, fw_name, adev->dev);
  393. if (err) {
  394. DRM_ERROR("Failed to request firmware\n");
  395. return err;
  396. }
  397. err = amdgpu_ucode_validate(adev->pm.fw);
  398. if (err) {
  399. DRM_ERROR("Failed to load firmware \"%s\"", fw_name);
  400. release_firmware(adev->pm.fw);
  401. adev->pm.fw = NULL;
  402. return err;
  403. }
  404. if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
  405. ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
  406. ucode->ucode_id = AMDGPU_UCODE_ID_SMC;
  407. ucode->fw = adev->pm.fw;
  408. header = (const struct common_firmware_header *)ucode->fw->data;
  409. adev->firmware.fw_size +=
  410. ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
  411. }
  412. }
  413. hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;
  414. amdgpu_ucode_print_smc_hdr(&hdr->header);
  415. adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
  416. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  417. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  418. src = (const uint8_t *)(adev->pm.fw->data +
  419. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  420. info->version = adev->pm.fw_version;
  421. info->image_size = ucode_size;
  422. info->ucode_start_address = ucode_start_address;
  423. info->kptr = (void *)src;
  424. }
  425. return 0;
  426. }
  427. static const struct cgs_ops amdgpu_cgs_ops = {
  428. .read_register = amdgpu_cgs_read_register,
  429. .write_register = amdgpu_cgs_write_register,
  430. .read_ind_register = amdgpu_cgs_read_ind_register,
  431. .write_ind_register = amdgpu_cgs_write_ind_register,
  432. .get_firmware_info = amdgpu_cgs_get_firmware_info,
  433. };
  434. struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev)
  435. {
  436. struct amdgpu_cgs_device *cgs_device =
  437. kmalloc(sizeof(*cgs_device), GFP_KERNEL);
  438. if (!cgs_device) {
  439. DRM_ERROR("Couldn't allocate CGS device structure\n");
  440. return NULL;
  441. }
  442. cgs_device->base.ops = &amdgpu_cgs_ops;
  443. cgs_device->adev = adev;
  444. return (struct cgs_device *)cgs_device;
  445. }
  446. void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device)
  447. {
  448. kfree(cgs_device);
  449. }