amdgpu_cgs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. ((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0xD1)) ||
  315. ((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0xD3))) {
  316. info->is_kicker = true;
  317. strcpy(fw_name, "amdgpu/topaz_k_smc.bin");
  318. } else
  319. strcpy(fw_name, "amdgpu/topaz_smc.bin");
  320. break;
  321. case CHIP_TONGA:
  322. if (((adev->pdev->device == 0x6939) && (adev->pdev->revision == 0xf1)) ||
  323. ((adev->pdev->device == 0x6938) && (adev->pdev->revision == 0xf1))) {
  324. info->is_kicker = true;
  325. strcpy(fw_name, "amdgpu/tonga_k_smc.bin");
  326. } else
  327. strcpy(fw_name, "amdgpu/tonga_smc.bin");
  328. break;
  329. case CHIP_FIJI:
  330. strcpy(fw_name, "amdgpu/fiji_smc.bin");
  331. break;
  332. case CHIP_POLARIS11:
  333. if (type == CGS_UCODE_ID_SMU) {
  334. if (((adev->pdev->device == 0x67ef) &&
  335. ((adev->pdev->revision == 0xe0) ||
  336. (adev->pdev->revision == 0xe5))) ||
  337. ((adev->pdev->device == 0x67ff) &&
  338. ((adev->pdev->revision == 0xcf) ||
  339. (adev->pdev->revision == 0xef) ||
  340. (adev->pdev->revision == 0xff)))) {
  341. info->is_kicker = true;
  342. strcpy(fw_name, "amdgpu/polaris11_k_smc.bin");
  343. } else if ((adev->pdev->device == 0x67ef) &&
  344. (adev->pdev->revision == 0xe2)) {
  345. info->is_kicker = true;
  346. strcpy(fw_name, "amdgpu/polaris11_k2_smc.bin");
  347. } else {
  348. strcpy(fw_name, "amdgpu/polaris11_smc.bin");
  349. }
  350. } else if (type == CGS_UCODE_ID_SMU_SK) {
  351. strcpy(fw_name, "amdgpu/polaris11_smc_sk.bin");
  352. }
  353. break;
  354. case CHIP_POLARIS10:
  355. if (type == CGS_UCODE_ID_SMU) {
  356. if (((adev->pdev->device == 0x67df) &&
  357. ((adev->pdev->revision == 0xe0) ||
  358. (adev->pdev->revision == 0xe3) ||
  359. (adev->pdev->revision == 0xe4) ||
  360. (adev->pdev->revision == 0xe5) ||
  361. (adev->pdev->revision == 0xe7) ||
  362. (adev->pdev->revision == 0xef))) ||
  363. ((adev->pdev->device == 0x6fdf) &&
  364. ((adev->pdev->revision == 0xef) ||
  365. (adev->pdev->revision == 0xff)))) {
  366. info->is_kicker = true;
  367. strcpy(fw_name, "amdgpu/polaris10_k_smc.bin");
  368. } else if ((adev->pdev->device == 0x67df) &&
  369. ((adev->pdev->revision == 0xe1) ||
  370. (adev->pdev->revision == 0xf7))) {
  371. info->is_kicker = true;
  372. strcpy(fw_name, "amdgpu/polaris10_k2_smc.bin");
  373. } else {
  374. strcpy(fw_name, "amdgpu/polaris10_smc.bin");
  375. }
  376. } else if (type == CGS_UCODE_ID_SMU_SK) {
  377. strcpy(fw_name, "amdgpu/polaris10_smc_sk.bin");
  378. }
  379. break;
  380. case CHIP_POLARIS12:
  381. if (((adev->pdev->device == 0x6987) &&
  382. ((adev->pdev->revision == 0xc0) ||
  383. (adev->pdev->revision == 0xc3))) ||
  384. ((adev->pdev->device == 0x6981) &&
  385. ((adev->pdev->revision == 0x00) ||
  386. (adev->pdev->revision == 0x01) ||
  387. (adev->pdev->revision == 0x10)))) {
  388. info->is_kicker = true;
  389. strcpy(fw_name, "amdgpu/polaris12_k_smc.bin");
  390. } else {
  391. strcpy(fw_name, "amdgpu/polaris12_smc.bin");
  392. }
  393. break;
  394. case CHIP_VEGAM:
  395. strcpy(fw_name, "amdgpu/vegam_smc.bin");
  396. break;
  397. case CHIP_VEGA10:
  398. if ((adev->pdev->device == 0x687f) &&
  399. ((adev->pdev->revision == 0xc0) ||
  400. (adev->pdev->revision == 0xc1) ||
  401. (adev->pdev->revision == 0xc3)))
  402. strcpy(fw_name, "amdgpu/vega10_acg_smc.bin");
  403. else
  404. strcpy(fw_name, "amdgpu/vega10_smc.bin");
  405. break;
  406. case CHIP_VEGA12:
  407. strcpy(fw_name, "amdgpu/vega12_smc.bin");
  408. break;
  409. case CHIP_VEGA20:
  410. strcpy(fw_name, "amdgpu/vega20_smc.bin");
  411. break;
  412. default:
  413. DRM_ERROR("SMC firmware not supported\n");
  414. return -EINVAL;
  415. }
  416. err = request_firmware(&adev->pm.fw, fw_name, adev->dev);
  417. if (err) {
  418. DRM_ERROR("Failed to request firmware\n");
  419. return err;
  420. }
  421. err = amdgpu_ucode_validate(adev->pm.fw);
  422. if (err) {
  423. DRM_ERROR("Failed to load firmware \"%s\"", fw_name);
  424. release_firmware(adev->pm.fw);
  425. adev->pm.fw = NULL;
  426. return err;
  427. }
  428. if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
  429. ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
  430. ucode->ucode_id = AMDGPU_UCODE_ID_SMC;
  431. ucode->fw = adev->pm.fw;
  432. header = (const struct common_firmware_header *)ucode->fw->data;
  433. adev->firmware.fw_size +=
  434. ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
  435. }
  436. }
  437. hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;
  438. amdgpu_ucode_print_smc_hdr(&hdr->header);
  439. adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
  440. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  441. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  442. src = (const uint8_t *)(adev->pm.fw->data +
  443. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  444. info->version = adev->pm.fw_version;
  445. info->image_size = ucode_size;
  446. info->ucode_start_address = ucode_start_address;
  447. info->kptr = (void *)src;
  448. }
  449. return 0;
  450. }
  451. static const struct cgs_ops amdgpu_cgs_ops = {
  452. .read_register = amdgpu_cgs_read_register,
  453. .write_register = amdgpu_cgs_write_register,
  454. .read_ind_register = amdgpu_cgs_read_ind_register,
  455. .write_ind_register = amdgpu_cgs_write_ind_register,
  456. .get_firmware_info = amdgpu_cgs_get_firmware_info,
  457. };
  458. struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev)
  459. {
  460. struct amdgpu_cgs_device *cgs_device =
  461. kmalloc(sizeof(*cgs_device), GFP_KERNEL);
  462. if (!cgs_device) {
  463. DRM_ERROR("Couldn't allocate CGS device structure\n");
  464. return NULL;
  465. }
  466. cgs_device->base.ops = &amdgpu_cgs_ops;
  467. cgs_device->adev = adev;
  468. return (struct cgs_device *)cgs_device;
  469. }
  470. void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device)
  471. {
  472. kfree(cgs_device);
  473. }