amdgpu_cgs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 int amdgpu_cgs_set_clockgating_state(struct cgs_device *cgs_device,
  104. enum amd_ip_block_type block_type,
  105. enum amd_clockgating_state state)
  106. {
  107. CGS_FUNC_ADEV;
  108. int i, r = -1;
  109. for (i = 0; i < adev->num_ip_blocks; i++) {
  110. if (!adev->ip_blocks[i].status.valid)
  111. continue;
  112. if (adev->ip_blocks[i].version->type == block_type) {
  113. r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
  114. (void *)adev,
  115. state);
  116. break;
  117. }
  118. }
  119. return r;
  120. }
  121. static int amdgpu_cgs_set_powergating_state(struct cgs_device *cgs_device,
  122. enum amd_ip_block_type block_type,
  123. enum amd_powergating_state state)
  124. {
  125. CGS_FUNC_ADEV;
  126. int i, r = -1;
  127. for (i = 0; i < adev->num_ip_blocks; i++) {
  128. if (!adev->ip_blocks[i].status.valid)
  129. continue;
  130. if (adev->ip_blocks[i].version->type == block_type) {
  131. r = adev->ip_blocks[i].version->funcs->set_powergating_state(
  132. (void *)adev,
  133. state);
  134. break;
  135. }
  136. }
  137. return r;
  138. }
  139. static uint32_t fw_type_convert(struct cgs_device *cgs_device, uint32_t fw_type)
  140. {
  141. CGS_FUNC_ADEV;
  142. enum AMDGPU_UCODE_ID result = AMDGPU_UCODE_ID_MAXIMUM;
  143. switch (fw_type) {
  144. case CGS_UCODE_ID_SDMA0:
  145. result = AMDGPU_UCODE_ID_SDMA0;
  146. break;
  147. case CGS_UCODE_ID_SDMA1:
  148. result = AMDGPU_UCODE_ID_SDMA1;
  149. break;
  150. case CGS_UCODE_ID_CP_CE:
  151. result = AMDGPU_UCODE_ID_CP_CE;
  152. break;
  153. case CGS_UCODE_ID_CP_PFP:
  154. result = AMDGPU_UCODE_ID_CP_PFP;
  155. break;
  156. case CGS_UCODE_ID_CP_ME:
  157. result = AMDGPU_UCODE_ID_CP_ME;
  158. break;
  159. case CGS_UCODE_ID_CP_MEC:
  160. case CGS_UCODE_ID_CP_MEC_JT1:
  161. result = AMDGPU_UCODE_ID_CP_MEC1;
  162. break;
  163. case CGS_UCODE_ID_CP_MEC_JT2:
  164. /* for VI. JT2 should be the same as JT1, because:
  165. 1, MEC2 and MEC1 use exactly same FW.
  166. 2, JT2 is not pached but JT1 is.
  167. */
  168. if (adev->asic_type >= CHIP_TOPAZ)
  169. result = AMDGPU_UCODE_ID_CP_MEC1;
  170. else
  171. result = AMDGPU_UCODE_ID_CP_MEC2;
  172. break;
  173. case CGS_UCODE_ID_RLC_G:
  174. result = AMDGPU_UCODE_ID_RLC_G;
  175. break;
  176. case CGS_UCODE_ID_STORAGE:
  177. result = AMDGPU_UCODE_ID_STORAGE;
  178. break;
  179. default:
  180. DRM_ERROR("Firmware type not supported\n");
  181. }
  182. return result;
  183. }
  184. static uint16_t amdgpu_get_firmware_version(struct cgs_device *cgs_device,
  185. enum cgs_ucode_id type)
  186. {
  187. CGS_FUNC_ADEV;
  188. uint16_t fw_version = 0;
  189. switch (type) {
  190. case CGS_UCODE_ID_SDMA0:
  191. fw_version = adev->sdma.instance[0].fw_version;
  192. break;
  193. case CGS_UCODE_ID_SDMA1:
  194. fw_version = adev->sdma.instance[1].fw_version;
  195. break;
  196. case CGS_UCODE_ID_CP_CE:
  197. fw_version = adev->gfx.ce_fw_version;
  198. break;
  199. case CGS_UCODE_ID_CP_PFP:
  200. fw_version = adev->gfx.pfp_fw_version;
  201. break;
  202. case CGS_UCODE_ID_CP_ME:
  203. fw_version = adev->gfx.me_fw_version;
  204. break;
  205. case CGS_UCODE_ID_CP_MEC:
  206. fw_version = adev->gfx.mec_fw_version;
  207. break;
  208. case CGS_UCODE_ID_CP_MEC_JT1:
  209. fw_version = adev->gfx.mec_fw_version;
  210. break;
  211. case CGS_UCODE_ID_CP_MEC_JT2:
  212. fw_version = adev->gfx.mec_fw_version;
  213. break;
  214. case CGS_UCODE_ID_RLC_G:
  215. fw_version = adev->gfx.rlc_fw_version;
  216. break;
  217. case CGS_UCODE_ID_STORAGE:
  218. break;
  219. default:
  220. DRM_ERROR("firmware type %d do not have version\n", type);
  221. break;
  222. }
  223. return fw_version;
  224. }
  225. static int amdgpu_cgs_get_firmware_info(struct cgs_device *cgs_device,
  226. enum cgs_ucode_id type,
  227. struct cgs_firmware_info *info)
  228. {
  229. CGS_FUNC_ADEV;
  230. if ((CGS_UCODE_ID_SMU != type) && (CGS_UCODE_ID_SMU_SK != type)) {
  231. uint64_t gpu_addr;
  232. uint32_t data_size;
  233. const struct gfx_firmware_header_v1_0 *header;
  234. enum AMDGPU_UCODE_ID id;
  235. struct amdgpu_firmware_info *ucode;
  236. id = fw_type_convert(cgs_device, type);
  237. ucode = &adev->firmware.ucode[id];
  238. if (ucode->fw == NULL)
  239. return -EINVAL;
  240. gpu_addr = ucode->mc_addr;
  241. header = (const struct gfx_firmware_header_v1_0 *)ucode->fw->data;
  242. data_size = le32_to_cpu(header->header.ucode_size_bytes);
  243. if ((type == CGS_UCODE_ID_CP_MEC_JT1) ||
  244. (type == CGS_UCODE_ID_CP_MEC_JT2)) {
  245. gpu_addr += ALIGN(le32_to_cpu(header->header.ucode_size_bytes), PAGE_SIZE);
  246. data_size = le32_to_cpu(header->jt_size) << 2;
  247. }
  248. info->kptr = ucode->kaddr;
  249. info->image_size = data_size;
  250. info->mc_addr = gpu_addr;
  251. info->version = (uint16_t)le32_to_cpu(header->header.ucode_version);
  252. if (CGS_UCODE_ID_CP_MEC == type)
  253. info->image_size = le32_to_cpu(header->jt_offset) << 2;
  254. info->fw_version = amdgpu_get_firmware_version(cgs_device, type);
  255. info->feature_version = (uint16_t)le32_to_cpu(header->ucode_feature_version);
  256. } else {
  257. char fw_name[30] = {0};
  258. int err = 0;
  259. uint32_t ucode_size;
  260. uint32_t ucode_start_address;
  261. const uint8_t *src;
  262. const struct smc_firmware_header_v1_0 *hdr;
  263. const struct common_firmware_header *header;
  264. struct amdgpu_firmware_info *ucode = NULL;
  265. if (!adev->pm.fw) {
  266. switch (adev->asic_type) {
  267. case CHIP_TAHITI:
  268. strcpy(fw_name, "radeon/tahiti_smc.bin");
  269. break;
  270. case CHIP_PITCAIRN:
  271. if ((adev->pdev->revision == 0x81) &&
  272. ((adev->pdev->device == 0x6810) ||
  273. (adev->pdev->device == 0x6811))) {
  274. info->is_kicker = true;
  275. strcpy(fw_name, "radeon/pitcairn_k_smc.bin");
  276. } else {
  277. strcpy(fw_name, "radeon/pitcairn_smc.bin");
  278. }
  279. break;
  280. case CHIP_VERDE:
  281. if (((adev->pdev->device == 0x6820) &&
  282. ((adev->pdev->revision == 0x81) ||
  283. (adev->pdev->revision == 0x83))) ||
  284. ((adev->pdev->device == 0x6821) &&
  285. ((adev->pdev->revision == 0x83) ||
  286. (adev->pdev->revision == 0x87))) ||
  287. ((adev->pdev->revision == 0x87) &&
  288. ((adev->pdev->device == 0x6823) ||
  289. (adev->pdev->device == 0x682b)))) {
  290. info->is_kicker = true;
  291. strcpy(fw_name, "radeon/verde_k_smc.bin");
  292. } else {
  293. strcpy(fw_name, "radeon/verde_smc.bin");
  294. }
  295. break;
  296. case CHIP_OLAND:
  297. if (((adev->pdev->revision == 0x81) &&
  298. ((adev->pdev->device == 0x6600) ||
  299. (adev->pdev->device == 0x6604) ||
  300. (adev->pdev->device == 0x6605) ||
  301. (adev->pdev->device == 0x6610))) ||
  302. ((adev->pdev->revision == 0x83) &&
  303. (adev->pdev->device == 0x6610))) {
  304. info->is_kicker = true;
  305. strcpy(fw_name, "radeon/oland_k_smc.bin");
  306. } else {
  307. strcpy(fw_name, "radeon/oland_smc.bin");
  308. }
  309. break;
  310. case CHIP_HAINAN:
  311. if (((adev->pdev->revision == 0x81) &&
  312. (adev->pdev->device == 0x6660)) ||
  313. ((adev->pdev->revision == 0x83) &&
  314. ((adev->pdev->device == 0x6660) ||
  315. (adev->pdev->device == 0x6663) ||
  316. (adev->pdev->device == 0x6665) ||
  317. (adev->pdev->device == 0x6667)))) {
  318. info->is_kicker = true;
  319. strcpy(fw_name, "radeon/hainan_k_smc.bin");
  320. } else if ((adev->pdev->revision == 0xc3) &&
  321. (adev->pdev->device == 0x6665)) {
  322. info->is_kicker = true;
  323. strcpy(fw_name, "radeon/banks_k_2_smc.bin");
  324. } else {
  325. strcpy(fw_name, "radeon/hainan_smc.bin");
  326. }
  327. break;
  328. case CHIP_BONAIRE:
  329. if ((adev->pdev->revision == 0x80) ||
  330. (adev->pdev->revision == 0x81) ||
  331. (adev->pdev->device == 0x665f)) {
  332. info->is_kicker = true;
  333. strcpy(fw_name, "radeon/bonaire_k_smc.bin");
  334. } else {
  335. strcpy(fw_name, "radeon/bonaire_smc.bin");
  336. }
  337. break;
  338. case CHIP_HAWAII:
  339. if (adev->pdev->revision == 0x80) {
  340. info->is_kicker = true;
  341. strcpy(fw_name, "radeon/hawaii_k_smc.bin");
  342. } else {
  343. strcpy(fw_name, "radeon/hawaii_smc.bin");
  344. }
  345. break;
  346. case CHIP_TOPAZ:
  347. if (((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x81)) ||
  348. ((adev->pdev->device == 0x6900) && (adev->pdev->revision == 0x83)) ||
  349. ((adev->pdev->device == 0x6907) && (adev->pdev->revision == 0x87))) {
  350. info->is_kicker = true;
  351. strcpy(fw_name, "amdgpu/topaz_k_smc.bin");
  352. } else
  353. strcpy(fw_name, "amdgpu/topaz_smc.bin");
  354. break;
  355. case CHIP_TONGA:
  356. if (((adev->pdev->device == 0x6939) && (adev->pdev->revision == 0xf1)) ||
  357. ((adev->pdev->device == 0x6938) && (adev->pdev->revision == 0xf1))) {
  358. info->is_kicker = true;
  359. strcpy(fw_name, "amdgpu/tonga_k_smc.bin");
  360. } else
  361. strcpy(fw_name, "amdgpu/tonga_smc.bin");
  362. break;
  363. case CHIP_FIJI:
  364. strcpy(fw_name, "amdgpu/fiji_smc.bin");
  365. break;
  366. case CHIP_POLARIS11:
  367. if (type == CGS_UCODE_ID_SMU) {
  368. if (((adev->pdev->device == 0x67ef) &&
  369. ((adev->pdev->revision == 0xe0) ||
  370. (adev->pdev->revision == 0xe2) ||
  371. (adev->pdev->revision == 0xe5))) ||
  372. ((adev->pdev->device == 0x67ff) &&
  373. ((adev->pdev->revision == 0xcf) ||
  374. (adev->pdev->revision == 0xef) ||
  375. (adev->pdev->revision == 0xff)))) {
  376. info->is_kicker = true;
  377. strcpy(fw_name, "amdgpu/polaris11_k_smc.bin");
  378. } else
  379. strcpy(fw_name, "amdgpu/polaris11_smc.bin");
  380. } else if (type == CGS_UCODE_ID_SMU_SK) {
  381. strcpy(fw_name, "amdgpu/polaris11_smc_sk.bin");
  382. }
  383. break;
  384. case CHIP_POLARIS10:
  385. if (type == CGS_UCODE_ID_SMU) {
  386. if ((adev->pdev->device == 0x67df) &&
  387. ((adev->pdev->revision == 0xe0) ||
  388. (adev->pdev->revision == 0xe3) ||
  389. (adev->pdev->revision == 0xe4) ||
  390. (adev->pdev->revision == 0xe5) ||
  391. (adev->pdev->revision == 0xe7) ||
  392. (adev->pdev->revision == 0xef))) {
  393. info->is_kicker = true;
  394. strcpy(fw_name, "amdgpu/polaris10_k_smc.bin");
  395. } else
  396. strcpy(fw_name, "amdgpu/polaris10_smc.bin");
  397. } else if (type == CGS_UCODE_ID_SMU_SK) {
  398. strcpy(fw_name, "amdgpu/polaris10_smc_sk.bin");
  399. }
  400. break;
  401. case CHIP_POLARIS12:
  402. strcpy(fw_name, "amdgpu/polaris12_smc.bin");
  403. break;
  404. case CHIP_VEGA10:
  405. if ((adev->pdev->device == 0x687f) &&
  406. ((adev->pdev->revision == 0xc0) ||
  407. (adev->pdev->revision == 0xc1) ||
  408. (adev->pdev->revision == 0xc3)))
  409. strcpy(fw_name, "amdgpu/vega10_acg_smc.bin");
  410. else
  411. strcpy(fw_name, "amdgpu/vega10_smc.bin");
  412. break;
  413. case CHIP_VEGA12:
  414. strcpy(fw_name, "amdgpu/vega12_smc.bin");
  415. break;
  416. default:
  417. DRM_ERROR("SMC firmware not supported\n");
  418. return -EINVAL;
  419. }
  420. err = request_firmware(&adev->pm.fw, fw_name, adev->dev);
  421. if (err) {
  422. DRM_ERROR("Failed to request firmware\n");
  423. return err;
  424. }
  425. err = amdgpu_ucode_validate(adev->pm.fw);
  426. if (err) {
  427. DRM_ERROR("Failed to load firmware \"%s\"", fw_name);
  428. release_firmware(adev->pm.fw);
  429. adev->pm.fw = NULL;
  430. return err;
  431. }
  432. if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
  433. ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
  434. ucode->ucode_id = AMDGPU_UCODE_ID_SMC;
  435. ucode->fw = adev->pm.fw;
  436. header = (const struct common_firmware_header *)ucode->fw->data;
  437. adev->firmware.fw_size +=
  438. ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
  439. }
  440. }
  441. hdr = (const struct smc_firmware_header_v1_0 *) adev->pm.fw->data;
  442. amdgpu_ucode_print_smc_hdr(&hdr->header);
  443. adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
  444. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  445. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  446. src = (const uint8_t *)(adev->pm.fw->data +
  447. le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  448. info->version = adev->pm.fw_version;
  449. info->image_size = ucode_size;
  450. info->ucode_start_address = ucode_start_address;
  451. info->kptr = (void *)src;
  452. }
  453. return 0;
  454. }
  455. static const struct cgs_ops amdgpu_cgs_ops = {
  456. .read_register = amdgpu_cgs_read_register,
  457. .write_register = amdgpu_cgs_write_register,
  458. .read_ind_register = amdgpu_cgs_read_ind_register,
  459. .write_ind_register = amdgpu_cgs_write_ind_register,
  460. .get_firmware_info = amdgpu_cgs_get_firmware_info,
  461. .set_powergating_state = amdgpu_cgs_set_powergating_state,
  462. .set_clockgating_state = amdgpu_cgs_set_clockgating_state,
  463. };
  464. struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev)
  465. {
  466. struct amdgpu_cgs_device *cgs_device =
  467. kmalloc(sizeof(*cgs_device), GFP_KERNEL);
  468. if (!cgs_device) {
  469. DRM_ERROR("Couldn't allocate CGS device structure\n");
  470. return NULL;
  471. }
  472. cgs_device->base.ops = &amdgpu_cgs_ops;
  473. cgs_device->adev = adev;
  474. return (struct cgs_device *)cgs_device;
  475. }
  476. void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device)
  477. {
  478. kfree(cgs_device);
  479. }