amdgpu_kms.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <drm/drmP.h>
  29. #include "amdgpu.h"
  30. #include <drm/amdgpu_drm.h>
  31. #include "amdgpu_uvd.h"
  32. #include "amdgpu_vce.h"
  33. #include <linux/vga_switcheroo.h>
  34. #include <linux/slab.h>
  35. #include <linux/pm_runtime.h>
  36. #include "amdgpu_amdkfd.h"
  37. #if defined(CONFIG_VGA_SWITCHEROO)
  38. bool amdgpu_has_atpx(void);
  39. #else
  40. static inline bool amdgpu_has_atpx(void) { return false; }
  41. #endif
  42. /**
  43. * amdgpu_driver_unload_kms - Main unload function for KMS.
  44. *
  45. * @dev: drm dev pointer
  46. *
  47. * This is the main unload function for KMS (all asics).
  48. * Returns 0 on success.
  49. */
  50. void amdgpu_driver_unload_kms(struct drm_device *dev)
  51. {
  52. struct amdgpu_device *adev = dev->dev_private;
  53. if (adev == NULL)
  54. return;
  55. if (adev->rmmio == NULL)
  56. goto done_free;
  57. if (amdgpu_sriov_vf(adev))
  58. amdgpu_virt_request_full_gpu(adev, false);
  59. if (amdgpu_device_is_px(dev)) {
  60. pm_runtime_get_sync(dev->dev);
  61. pm_runtime_forbid(dev->dev);
  62. }
  63. amdgpu_amdkfd_device_fini(adev);
  64. amdgpu_acpi_fini(adev);
  65. amdgpu_device_fini(adev);
  66. done_free:
  67. kfree(adev);
  68. dev->dev_private = NULL;
  69. }
  70. /**
  71. * amdgpu_driver_load_kms - Main load function for KMS.
  72. *
  73. * @dev: drm dev pointer
  74. * @flags: device flags
  75. *
  76. * This is the main load function for KMS (all asics).
  77. * Returns 0 on success, error on failure.
  78. */
  79. int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
  80. {
  81. struct amdgpu_device *adev;
  82. int r, acpi_status;
  83. adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL);
  84. if (adev == NULL) {
  85. return -ENOMEM;
  86. }
  87. dev->dev_private = (void *)adev;
  88. if ((amdgpu_runtime_pm != 0) &&
  89. amdgpu_has_atpx() &&
  90. (amdgpu_is_atpx_hybrid() ||
  91. amdgpu_has_atpx_dgpu_power_cntl()) &&
  92. ((flags & AMD_IS_APU) == 0) &&
  93. !pci_is_thunderbolt_attached(dev->pdev))
  94. flags |= AMD_IS_PX;
  95. /* amdgpu_device_init should report only fatal error
  96. * like memory allocation failure or iomapping failure,
  97. * or memory manager initialization failure, it must
  98. * properly initialize the GPU MC controller and permit
  99. * VRAM allocation
  100. */
  101. r = amdgpu_device_init(adev, dev, dev->pdev, flags);
  102. if (r) {
  103. dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
  104. goto out;
  105. }
  106. /* Call ACPI methods: require modeset init
  107. * but failure is not fatal
  108. */
  109. if (!r) {
  110. acpi_status = amdgpu_acpi_init(adev);
  111. if (acpi_status)
  112. dev_dbg(&dev->pdev->dev,
  113. "Error during ACPI methods call\n");
  114. }
  115. amdgpu_amdkfd_load_interface(adev);
  116. amdgpu_amdkfd_device_probe(adev);
  117. amdgpu_amdkfd_device_init(adev);
  118. if (amdgpu_device_is_px(dev)) {
  119. pm_runtime_use_autosuspend(dev->dev);
  120. pm_runtime_set_autosuspend_delay(dev->dev, 5000);
  121. pm_runtime_set_active(dev->dev);
  122. pm_runtime_allow(dev->dev);
  123. pm_runtime_mark_last_busy(dev->dev);
  124. pm_runtime_put_autosuspend(dev->dev);
  125. }
  126. if (amdgpu_sriov_vf(adev))
  127. amdgpu_virt_release_full_gpu(adev, true);
  128. out:
  129. if (r) {
  130. /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
  131. if (adev->rmmio && amdgpu_device_is_px(dev))
  132. pm_runtime_put_noidle(dev->dev);
  133. amdgpu_driver_unload_kms(dev);
  134. }
  135. return r;
  136. }
  137. static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info,
  138. struct drm_amdgpu_query_fw *query_fw,
  139. struct amdgpu_device *adev)
  140. {
  141. switch (query_fw->fw_type) {
  142. case AMDGPU_INFO_FW_VCE:
  143. fw_info->ver = adev->vce.fw_version;
  144. fw_info->feature = adev->vce.fb_version;
  145. break;
  146. case AMDGPU_INFO_FW_UVD:
  147. fw_info->ver = adev->uvd.fw_version;
  148. fw_info->feature = 0;
  149. break;
  150. case AMDGPU_INFO_FW_GMC:
  151. fw_info->ver = adev->mc.fw_version;
  152. fw_info->feature = 0;
  153. break;
  154. case AMDGPU_INFO_FW_GFX_ME:
  155. fw_info->ver = adev->gfx.me_fw_version;
  156. fw_info->feature = adev->gfx.me_feature_version;
  157. break;
  158. case AMDGPU_INFO_FW_GFX_PFP:
  159. fw_info->ver = adev->gfx.pfp_fw_version;
  160. fw_info->feature = adev->gfx.pfp_feature_version;
  161. break;
  162. case AMDGPU_INFO_FW_GFX_CE:
  163. fw_info->ver = adev->gfx.ce_fw_version;
  164. fw_info->feature = adev->gfx.ce_feature_version;
  165. break;
  166. case AMDGPU_INFO_FW_GFX_RLC:
  167. fw_info->ver = adev->gfx.rlc_fw_version;
  168. fw_info->feature = adev->gfx.rlc_feature_version;
  169. break;
  170. case AMDGPU_INFO_FW_GFX_MEC:
  171. if (query_fw->index == 0) {
  172. fw_info->ver = adev->gfx.mec_fw_version;
  173. fw_info->feature = adev->gfx.mec_feature_version;
  174. } else if (query_fw->index == 1) {
  175. fw_info->ver = adev->gfx.mec2_fw_version;
  176. fw_info->feature = adev->gfx.mec2_feature_version;
  177. } else
  178. return -EINVAL;
  179. break;
  180. case AMDGPU_INFO_FW_SMC:
  181. fw_info->ver = adev->pm.fw_version;
  182. fw_info->feature = 0;
  183. break;
  184. case AMDGPU_INFO_FW_SDMA:
  185. if (query_fw->index >= adev->sdma.num_instances)
  186. return -EINVAL;
  187. fw_info->ver = adev->sdma.instance[query_fw->index].fw_version;
  188. fw_info->feature = adev->sdma.instance[query_fw->index].feature_version;
  189. break;
  190. case AMDGPU_INFO_FW_SOS:
  191. fw_info->ver = adev->psp.sos_fw_version;
  192. fw_info->feature = adev->psp.sos_feature_version;
  193. break;
  194. case AMDGPU_INFO_FW_ASD:
  195. fw_info->ver = adev->psp.asd_fw_version;
  196. fw_info->feature = adev->psp.asd_feature_version;
  197. break;
  198. default:
  199. return -EINVAL;
  200. }
  201. return 0;
  202. }
  203. /*
  204. * Userspace get information ioctl
  205. */
  206. /**
  207. * amdgpu_info_ioctl - answer a device specific request.
  208. *
  209. * @adev: amdgpu device pointer
  210. * @data: request object
  211. * @filp: drm filp
  212. *
  213. * This function is used to pass device specific parameters to the userspace
  214. * drivers. Examples include: pci device id, pipeline parms, tiling params,
  215. * etc. (all asics).
  216. * Returns 0 on success, -EINVAL on failure.
  217. */
  218. static int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  219. {
  220. struct amdgpu_device *adev = dev->dev_private;
  221. struct drm_amdgpu_info *info = data;
  222. struct amdgpu_mode_info *minfo = &adev->mode_info;
  223. void __user *out = (void __user *)(long)info->return_pointer;
  224. uint32_t size = info->return_size;
  225. struct drm_crtc *crtc;
  226. uint32_t ui32 = 0;
  227. uint64_t ui64 = 0;
  228. int i, found;
  229. int ui32_size = sizeof(ui32);
  230. if (!info->return_size || !info->return_pointer)
  231. return -EINVAL;
  232. switch (info->query) {
  233. case AMDGPU_INFO_ACCEL_WORKING:
  234. ui32 = adev->accel_working;
  235. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  236. case AMDGPU_INFO_CRTC_FROM_ID:
  237. for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
  238. crtc = (struct drm_crtc *)minfo->crtcs[i];
  239. if (crtc && crtc->base.id == info->mode_crtc.id) {
  240. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  241. ui32 = amdgpu_crtc->crtc_id;
  242. found = 1;
  243. break;
  244. }
  245. }
  246. if (!found) {
  247. DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
  248. return -EINVAL;
  249. }
  250. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  251. case AMDGPU_INFO_HW_IP_INFO: {
  252. struct drm_amdgpu_info_hw_ip ip = {};
  253. enum amd_ip_block_type type;
  254. uint32_t ring_mask = 0;
  255. uint32_t ib_start_alignment = 0;
  256. uint32_t ib_size_alignment = 0;
  257. if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  258. return -EINVAL;
  259. switch (info->query_hw_ip.type) {
  260. case AMDGPU_HW_IP_GFX:
  261. type = AMD_IP_BLOCK_TYPE_GFX;
  262. for (i = 0; i < adev->gfx.num_gfx_rings; i++)
  263. ring_mask |= ((adev->gfx.gfx_ring[i].ready ? 1 : 0) << i);
  264. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  265. ib_size_alignment = 8;
  266. break;
  267. case AMDGPU_HW_IP_COMPUTE:
  268. type = AMD_IP_BLOCK_TYPE_GFX;
  269. for (i = 0; i < adev->gfx.num_compute_rings; i++)
  270. ring_mask |= ((adev->gfx.compute_ring[i].ready ? 1 : 0) << i);
  271. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  272. ib_size_alignment = 8;
  273. break;
  274. case AMDGPU_HW_IP_DMA:
  275. type = AMD_IP_BLOCK_TYPE_SDMA;
  276. for (i = 0; i < adev->sdma.num_instances; i++)
  277. ring_mask |= ((adev->sdma.instance[i].ring.ready ? 1 : 0) << i);
  278. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  279. ib_size_alignment = 1;
  280. break;
  281. case AMDGPU_HW_IP_UVD:
  282. type = AMD_IP_BLOCK_TYPE_UVD;
  283. ring_mask = adev->uvd.ring.ready ? 1 : 0;
  284. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  285. ib_size_alignment = 16;
  286. break;
  287. case AMDGPU_HW_IP_VCE:
  288. type = AMD_IP_BLOCK_TYPE_VCE;
  289. for (i = 0; i < adev->vce.num_rings; i++)
  290. ring_mask |= ((adev->vce.ring[i].ready ? 1 : 0) << i);
  291. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  292. ib_size_alignment = 1;
  293. break;
  294. case AMDGPU_HW_IP_UVD_ENC:
  295. type = AMD_IP_BLOCK_TYPE_UVD;
  296. for (i = 0; i < adev->uvd.num_enc_rings; i++)
  297. ring_mask |= ((adev->uvd.ring_enc[i].ready ? 1 : 0) << i);
  298. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  299. ib_size_alignment = 1;
  300. break;
  301. default:
  302. return -EINVAL;
  303. }
  304. for (i = 0; i < adev->num_ip_blocks; i++) {
  305. if (adev->ip_blocks[i].version->type == type &&
  306. adev->ip_blocks[i].status.valid) {
  307. ip.hw_ip_version_major = adev->ip_blocks[i].version->major;
  308. ip.hw_ip_version_minor = adev->ip_blocks[i].version->minor;
  309. ip.capabilities_flags = 0;
  310. ip.available_rings = ring_mask;
  311. ip.ib_start_alignment = ib_start_alignment;
  312. ip.ib_size_alignment = ib_size_alignment;
  313. break;
  314. }
  315. }
  316. return copy_to_user(out, &ip,
  317. min((size_t)size, sizeof(ip))) ? -EFAULT : 0;
  318. }
  319. case AMDGPU_INFO_HW_IP_COUNT: {
  320. enum amd_ip_block_type type;
  321. uint32_t count = 0;
  322. switch (info->query_hw_ip.type) {
  323. case AMDGPU_HW_IP_GFX:
  324. type = AMD_IP_BLOCK_TYPE_GFX;
  325. break;
  326. case AMDGPU_HW_IP_COMPUTE:
  327. type = AMD_IP_BLOCK_TYPE_GFX;
  328. break;
  329. case AMDGPU_HW_IP_DMA:
  330. type = AMD_IP_BLOCK_TYPE_SDMA;
  331. break;
  332. case AMDGPU_HW_IP_UVD:
  333. type = AMD_IP_BLOCK_TYPE_UVD;
  334. break;
  335. case AMDGPU_HW_IP_VCE:
  336. type = AMD_IP_BLOCK_TYPE_VCE;
  337. break;
  338. case AMDGPU_HW_IP_UVD_ENC:
  339. type = AMD_IP_BLOCK_TYPE_UVD;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. for (i = 0; i < adev->num_ip_blocks; i++)
  345. if (adev->ip_blocks[i].version->type == type &&
  346. adev->ip_blocks[i].status.valid &&
  347. count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  348. count++;
  349. return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
  350. }
  351. case AMDGPU_INFO_TIMESTAMP:
  352. ui64 = amdgpu_gfx_get_gpu_clock_counter(adev);
  353. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  354. case AMDGPU_INFO_FW_VERSION: {
  355. struct drm_amdgpu_info_firmware fw_info;
  356. int ret;
  357. /* We only support one instance of each IP block right now. */
  358. if (info->query_fw.ip_instance != 0)
  359. return -EINVAL;
  360. ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev);
  361. if (ret)
  362. return ret;
  363. return copy_to_user(out, &fw_info,
  364. min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
  365. }
  366. case AMDGPU_INFO_NUM_BYTES_MOVED:
  367. ui64 = atomic64_read(&adev->num_bytes_moved);
  368. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  369. case AMDGPU_INFO_NUM_EVICTIONS:
  370. ui64 = atomic64_read(&adev->num_evictions);
  371. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  372. case AMDGPU_INFO_VRAM_USAGE:
  373. ui64 = atomic64_read(&adev->vram_usage);
  374. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  375. case AMDGPU_INFO_VIS_VRAM_USAGE:
  376. ui64 = atomic64_read(&adev->vram_vis_usage);
  377. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  378. case AMDGPU_INFO_GTT_USAGE:
  379. ui64 = atomic64_read(&adev->gtt_usage);
  380. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  381. case AMDGPU_INFO_GDS_CONFIG: {
  382. struct drm_amdgpu_info_gds gds_info;
  383. memset(&gds_info, 0, sizeof(gds_info));
  384. gds_info.gds_gfx_partition_size = adev->gds.mem.gfx_partition_size >> AMDGPU_GDS_SHIFT;
  385. gds_info.compute_partition_size = adev->gds.mem.cs_partition_size >> AMDGPU_GDS_SHIFT;
  386. gds_info.gds_total_size = adev->gds.mem.total_size >> AMDGPU_GDS_SHIFT;
  387. gds_info.gws_per_gfx_partition = adev->gds.gws.gfx_partition_size >> AMDGPU_GWS_SHIFT;
  388. gds_info.gws_per_compute_partition = adev->gds.gws.cs_partition_size >> AMDGPU_GWS_SHIFT;
  389. gds_info.oa_per_gfx_partition = adev->gds.oa.gfx_partition_size >> AMDGPU_OA_SHIFT;
  390. gds_info.oa_per_compute_partition = adev->gds.oa.cs_partition_size >> AMDGPU_OA_SHIFT;
  391. return copy_to_user(out, &gds_info,
  392. min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
  393. }
  394. case AMDGPU_INFO_VRAM_GTT: {
  395. struct drm_amdgpu_info_vram_gtt vram_gtt;
  396. vram_gtt.vram_size = adev->mc.real_vram_size;
  397. vram_gtt.vram_size -= adev->vram_pin_size;
  398. vram_gtt.vram_cpu_accessible_size = adev->mc.visible_vram_size;
  399. vram_gtt.vram_cpu_accessible_size -= (adev->vram_pin_size - adev->invisible_pin_size);
  400. vram_gtt.gtt_size = adev->mc.gtt_size;
  401. vram_gtt.gtt_size -= adev->gart_pin_size;
  402. return copy_to_user(out, &vram_gtt,
  403. min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
  404. }
  405. case AMDGPU_INFO_MEMORY: {
  406. struct drm_amdgpu_memory_info mem;
  407. memset(&mem, 0, sizeof(mem));
  408. mem.vram.total_heap_size = adev->mc.real_vram_size;
  409. mem.vram.usable_heap_size =
  410. adev->mc.real_vram_size - adev->vram_pin_size;
  411. mem.vram.heap_usage = atomic64_read(&adev->vram_usage);
  412. mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
  413. mem.cpu_accessible_vram.total_heap_size =
  414. adev->mc.visible_vram_size;
  415. mem.cpu_accessible_vram.usable_heap_size =
  416. adev->mc.visible_vram_size -
  417. (adev->vram_pin_size - adev->invisible_pin_size);
  418. mem.cpu_accessible_vram.heap_usage =
  419. atomic64_read(&adev->vram_vis_usage);
  420. mem.cpu_accessible_vram.max_allocation =
  421. mem.cpu_accessible_vram.usable_heap_size * 3 / 4;
  422. mem.gtt.total_heap_size = adev->mc.gtt_size;
  423. mem.gtt.usable_heap_size =
  424. adev->mc.gtt_size - adev->gart_pin_size;
  425. mem.gtt.heap_usage = atomic64_read(&adev->gtt_usage);
  426. mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4;
  427. return copy_to_user(out, &mem,
  428. min((size_t)size, sizeof(mem)))
  429. ? -EFAULT : 0;
  430. }
  431. case AMDGPU_INFO_READ_MMR_REG: {
  432. unsigned n, alloc_size;
  433. uint32_t *regs;
  434. unsigned se_num = (info->read_mmr_reg.instance >>
  435. AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
  436. AMDGPU_INFO_MMR_SE_INDEX_MASK;
  437. unsigned sh_num = (info->read_mmr_reg.instance >>
  438. AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
  439. AMDGPU_INFO_MMR_SH_INDEX_MASK;
  440. /* set full masks if the userspace set all bits
  441. * in the bitfields */
  442. if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
  443. se_num = 0xffffffff;
  444. if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
  445. sh_num = 0xffffffff;
  446. regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
  447. if (!regs)
  448. return -ENOMEM;
  449. alloc_size = info->read_mmr_reg.count * sizeof(*regs);
  450. for (i = 0; i < info->read_mmr_reg.count; i++)
  451. if (amdgpu_asic_read_register(adev, se_num, sh_num,
  452. info->read_mmr_reg.dword_offset + i,
  453. &regs[i])) {
  454. DRM_DEBUG_KMS("unallowed offset %#x\n",
  455. info->read_mmr_reg.dword_offset + i);
  456. kfree(regs);
  457. return -EFAULT;
  458. }
  459. n = copy_to_user(out, regs, min(size, alloc_size));
  460. kfree(regs);
  461. return n ? -EFAULT : 0;
  462. }
  463. case AMDGPU_INFO_DEV_INFO: {
  464. struct drm_amdgpu_info_device dev_info = {};
  465. dev_info.device_id = dev->pdev->device;
  466. dev_info.chip_rev = adev->rev_id;
  467. dev_info.external_rev = adev->external_rev_id;
  468. dev_info.pci_rev = dev->pdev->revision;
  469. dev_info.family = adev->family;
  470. dev_info.num_shader_engines = adev->gfx.config.max_shader_engines;
  471. dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
  472. /* return all clocks in KHz */
  473. dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
  474. if (adev->pm.dpm_enabled) {
  475. dev_info.max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10;
  476. dev_info.max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10;
  477. } else {
  478. dev_info.max_engine_clock = adev->pm.default_sclk * 10;
  479. dev_info.max_memory_clock = adev->pm.default_mclk * 10;
  480. }
  481. dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
  482. dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se *
  483. adev->gfx.config.max_shader_engines;
  484. dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
  485. dev_info._pad = 0;
  486. dev_info.ids_flags = 0;
  487. if (adev->flags & AMD_IS_APU)
  488. dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
  489. if (amdgpu_sriov_vf(adev))
  490. dev_info.ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION;
  491. dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
  492. dev_info.virtual_address_max = (uint64_t)adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
  493. dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
  494. dev_info.pte_fragment_size = (1 << AMDGPU_LOG2_PAGES_PER_FRAG) *
  495. AMDGPU_GPU_PAGE_SIZE;
  496. dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE;
  497. dev_info.cu_active_number = adev->gfx.cu_info.number;
  498. dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
  499. dev_info.ce_ram_size = adev->gfx.ce_ram_size;
  500. memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
  501. sizeof(adev->gfx.cu_info.bitmap));
  502. dev_info.vram_type = adev->mc.vram_type;
  503. dev_info.vram_bit_width = adev->mc.vram_width;
  504. dev_info.vce_harvest_config = adev->vce.harvest_config;
  505. dev_info.gc_double_offchip_lds_buf =
  506. adev->gfx.config.double_offchip_lds_buf;
  507. if (amdgpu_ngg) {
  508. dev_info.prim_buf_gpu_addr = adev->gfx.ngg.buf[PRIM].gpu_addr;
  509. dev_info.pos_buf_gpu_addr = adev->gfx.ngg.buf[POS].gpu_addr;
  510. dev_info.cntl_sb_buf_gpu_addr = adev->gfx.ngg.buf[CNTL].gpu_addr;
  511. dev_info.param_buf_gpu_addr = adev->gfx.ngg.buf[PARAM].gpu_addr;
  512. }
  513. return copy_to_user(out, &dev_info,
  514. min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0;
  515. }
  516. case AMDGPU_INFO_VCE_CLOCK_TABLE: {
  517. unsigned i;
  518. struct drm_amdgpu_info_vce_clock_table vce_clk_table = {};
  519. struct amd_vce_state *vce_state;
  520. for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) {
  521. vce_state = amdgpu_dpm_get_vce_clock_state(adev, i);
  522. if (vce_state) {
  523. vce_clk_table.entries[i].sclk = vce_state->sclk;
  524. vce_clk_table.entries[i].mclk = vce_state->mclk;
  525. vce_clk_table.entries[i].eclk = vce_state->evclk;
  526. vce_clk_table.num_valid_entries++;
  527. }
  528. }
  529. return copy_to_user(out, &vce_clk_table,
  530. min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0;
  531. }
  532. case AMDGPU_INFO_VBIOS: {
  533. uint32_t bios_size = adev->bios_size;
  534. switch (info->vbios_info.type) {
  535. case AMDGPU_INFO_VBIOS_SIZE:
  536. return copy_to_user(out, &bios_size,
  537. min((size_t)size, sizeof(bios_size)))
  538. ? -EFAULT : 0;
  539. case AMDGPU_INFO_VBIOS_IMAGE: {
  540. uint8_t *bios;
  541. uint32_t bios_offset = info->vbios_info.offset;
  542. if (bios_offset >= bios_size)
  543. return -EINVAL;
  544. bios = adev->bios + bios_offset;
  545. return copy_to_user(out, bios,
  546. min((size_t)size, (size_t)(bios_size - bios_offset)))
  547. ? -EFAULT : 0;
  548. }
  549. default:
  550. DRM_DEBUG_KMS("Invalid request %d\n",
  551. info->vbios_info.type);
  552. return -EINVAL;
  553. }
  554. }
  555. case AMDGPU_INFO_NUM_HANDLES: {
  556. struct drm_amdgpu_info_num_handles handle;
  557. switch (info->query_hw_ip.type) {
  558. case AMDGPU_HW_IP_UVD:
  559. /* Starting Polaris, we support unlimited UVD handles */
  560. if (adev->asic_type < CHIP_POLARIS10) {
  561. handle.uvd_max_handles = adev->uvd.max_handles;
  562. handle.uvd_used_handles = amdgpu_uvd_used_handles(adev);
  563. return copy_to_user(out, &handle,
  564. min((size_t)size, sizeof(handle))) ? -EFAULT : 0;
  565. } else {
  566. return -ENODATA;
  567. }
  568. break;
  569. default:
  570. return -EINVAL;
  571. }
  572. }
  573. case AMDGPU_INFO_SENSOR: {
  574. struct pp_gpu_power query = {0};
  575. int query_size = sizeof(query);
  576. if (amdgpu_dpm == 0)
  577. return -ENOENT;
  578. switch (info->sensor_info.type) {
  579. case AMDGPU_INFO_SENSOR_GFX_SCLK:
  580. /* get sclk in Mhz */
  581. if (amdgpu_dpm_read_sensor(adev,
  582. AMDGPU_PP_SENSOR_GFX_SCLK,
  583. (void *)&ui32, &ui32_size)) {
  584. return -EINVAL;
  585. }
  586. ui32 /= 100;
  587. break;
  588. case AMDGPU_INFO_SENSOR_GFX_MCLK:
  589. /* get mclk in Mhz */
  590. if (amdgpu_dpm_read_sensor(adev,
  591. AMDGPU_PP_SENSOR_GFX_MCLK,
  592. (void *)&ui32, &ui32_size)) {
  593. return -EINVAL;
  594. }
  595. ui32 /= 100;
  596. break;
  597. case AMDGPU_INFO_SENSOR_GPU_TEMP:
  598. /* get temperature in millidegrees C */
  599. if (amdgpu_dpm_read_sensor(adev,
  600. AMDGPU_PP_SENSOR_GPU_TEMP,
  601. (void *)&ui32, &ui32_size)) {
  602. return -EINVAL;
  603. }
  604. break;
  605. case AMDGPU_INFO_SENSOR_GPU_LOAD:
  606. /* get GPU load */
  607. if (amdgpu_dpm_read_sensor(adev,
  608. AMDGPU_PP_SENSOR_GPU_LOAD,
  609. (void *)&ui32, &ui32_size)) {
  610. return -EINVAL;
  611. }
  612. break;
  613. case AMDGPU_INFO_SENSOR_GPU_AVG_POWER:
  614. /* get average GPU power */
  615. if (amdgpu_dpm_read_sensor(adev,
  616. AMDGPU_PP_SENSOR_GPU_POWER,
  617. (void *)&query, &query_size)) {
  618. return -EINVAL;
  619. }
  620. ui32 = query.average_gpu_power >> 8;
  621. break;
  622. case AMDGPU_INFO_SENSOR_VDDNB:
  623. /* get VDDNB in millivolts */
  624. if (amdgpu_dpm_read_sensor(adev,
  625. AMDGPU_PP_SENSOR_VDDNB,
  626. (void *)&ui32, &ui32_size)) {
  627. return -EINVAL;
  628. }
  629. break;
  630. case AMDGPU_INFO_SENSOR_VDDGFX:
  631. /* get VDDGFX in millivolts */
  632. if (amdgpu_dpm_read_sensor(adev,
  633. AMDGPU_PP_SENSOR_VDDGFX,
  634. (void *)&ui32, &ui32_size)) {
  635. return -EINVAL;
  636. }
  637. break;
  638. default:
  639. DRM_DEBUG_KMS("Invalid request %d\n",
  640. info->sensor_info.type);
  641. return -EINVAL;
  642. }
  643. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  644. }
  645. default:
  646. DRM_DEBUG_KMS("Invalid request %d\n", info->query);
  647. return -EINVAL;
  648. }
  649. return 0;
  650. }
  651. /*
  652. * Outdated mess for old drm with Xorg being in charge (void function now).
  653. */
  654. /**
  655. * amdgpu_driver_lastclose_kms - drm callback for last close
  656. *
  657. * @dev: drm dev pointer
  658. *
  659. * Switch vga_switcheroo state after last close (all asics).
  660. */
  661. void amdgpu_driver_lastclose_kms(struct drm_device *dev)
  662. {
  663. struct amdgpu_device *adev = dev->dev_private;
  664. amdgpu_fbdev_restore_mode(adev);
  665. vga_switcheroo_process_delayed_switch();
  666. }
  667. /**
  668. * amdgpu_driver_open_kms - drm callback for open
  669. *
  670. * @dev: drm dev pointer
  671. * @file_priv: drm file
  672. *
  673. * On device open, init vm on cayman+ (all asics).
  674. * Returns 0 on success, error on failure.
  675. */
  676. int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
  677. {
  678. struct amdgpu_device *adev = dev->dev_private;
  679. struct amdgpu_fpriv *fpriv;
  680. int r;
  681. file_priv->driver_priv = NULL;
  682. r = pm_runtime_get_sync(dev->dev);
  683. if (r < 0)
  684. return r;
  685. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  686. if (unlikely(!fpriv)) {
  687. r = -ENOMEM;
  688. goto out_suspend;
  689. }
  690. r = amdgpu_vm_init(adev, &fpriv->vm);
  691. if (r) {
  692. kfree(fpriv);
  693. goto out_suspend;
  694. }
  695. fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL);
  696. if (!fpriv->prt_va) {
  697. r = -ENOMEM;
  698. amdgpu_vm_fini(adev, &fpriv->vm);
  699. kfree(fpriv);
  700. goto out_suspend;
  701. }
  702. if (amdgpu_sriov_vf(adev)) {
  703. r = amdgpu_map_static_csa(adev, &fpriv->vm);
  704. if (r)
  705. goto out_suspend;
  706. }
  707. mutex_init(&fpriv->bo_list_lock);
  708. idr_init(&fpriv->bo_list_handles);
  709. amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
  710. file_priv->driver_priv = fpriv;
  711. out_suspend:
  712. pm_runtime_mark_last_busy(dev->dev);
  713. pm_runtime_put_autosuspend(dev->dev);
  714. return r;
  715. }
  716. /**
  717. * amdgpu_driver_postclose_kms - drm callback for post close
  718. *
  719. * @dev: drm dev pointer
  720. * @file_priv: drm file
  721. *
  722. * On device post close, tear down vm on cayman+ (all asics).
  723. */
  724. void amdgpu_driver_postclose_kms(struct drm_device *dev,
  725. struct drm_file *file_priv)
  726. {
  727. struct amdgpu_device *adev = dev->dev_private;
  728. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  729. struct amdgpu_bo_list *list;
  730. int handle;
  731. if (!fpriv)
  732. return;
  733. pm_runtime_get_sync(dev->dev);
  734. amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
  735. amdgpu_uvd_free_handles(adev, file_priv);
  736. amdgpu_vce_free_handles(adev, file_priv);
  737. amdgpu_vm_bo_rmv(adev, fpriv->prt_va);
  738. if (amdgpu_sriov_vf(adev)) {
  739. /* TODO: how to handle reserve failure */
  740. BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, false));
  741. amdgpu_vm_bo_rmv(adev, fpriv->vm.csa_bo_va);
  742. fpriv->vm.csa_bo_va = NULL;
  743. amdgpu_bo_unreserve(adev->virt.csa_obj);
  744. }
  745. amdgpu_vm_fini(adev, &fpriv->vm);
  746. idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
  747. amdgpu_bo_list_free(list);
  748. idr_destroy(&fpriv->bo_list_handles);
  749. mutex_destroy(&fpriv->bo_list_lock);
  750. kfree(fpriv);
  751. file_priv->driver_priv = NULL;
  752. pm_runtime_mark_last_busy(dev->dev);
  753. pm_runtime_put_autosuspend(dev->dev);
  754. }
  755. /*
  756. * VBlank related functions.
  757. */
  758. /**
  759. * amdgpu_get_vblank_counter_kms - get frame count
  760. *
  761. * @dev: drm dev pointer
  762. * @pipe: crtc to get the frame count from
  763. *
  764. * Gets the frame count on the requested crtc (all asics).
  765. * Returns frame count on success, -EINVAL on failure.
  766. */
  767. u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
  768. {
  769. struct amdgpu_device *adev = dev->dev_private;
  770. int vpos, hpos, stat;
  771. u32 count;
  772. if (pipe >= adev->mode_info.num_crtc) {
  773. DRM_ERROR("Invalid crtc %u\n", pipe);
  774. return -EINVAL;
  775. }
  776. /* The hw increments its frame counter at start of vsync, not at start
  777. * of vblank, as is required by DRM core vblank counter handling.
  778. * Cook the hw count here to make it appear to the caller as if it
  779. * incremented at start of vblank. We measure distance to start of
  780. * vblank in vpos. vpos therefore will be >= 0 between start of vblank
  781. * and start of vsync, so vpos >= 0 means to bump the hw frame counter
  782. * result by 1 to give the proper appearance to caller.
  783. */
  784. if (adev->mode_info.crtcs[pipe]) {
  785. /* Repeat readout if needed to provide stable result if
  786. * we cross start of vsync during the queries.
  787. */
  788. do {
  789. count = amdgpu_display_vblank_get_counter(adev, pipe);
  790. /* Ask amdgpu_get_crtc_scanoutpos to return vpos as
  791. * distance to start of vblank, instead of regular
  792. * vertical scanout pos.
  793. */
  794. stat = amdgpu_get_crtc_scanoutpos(
  795. dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
  796. &vpos, &hpos, NULL, NULL,
  797. &adev->mode_info.crtcs[pipe]->base.hwmode);
  798. } while (count != amdgpu_display_vblank_get_counter(adev, pipe));
  799. if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
  800. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
  801. DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
  802. } else {
  803. DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
  804. pipe, vpos);
  805. /* Bump counter if we are at >= leading edge of vblank,
  806. * but before vsync where vpos would turn negative and
  807. * the hw counter really increments.
  808. */
  809. if (vpos >= 0)
  810. count++;
  811. }
  812. } else {
  813. /* Fallback to use value as is. */
  814. count = amdgpu_display_vblank_get_counter(adev, pipe);
  815. DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
  816. }
  817. return count;
  818. }
  819. /**
  820. * amdgpu_enable_vblank_kms - enable vblank interrupt
  821. *
  822. * @dev: drm dev pointer
  823. * @pipe: crtc to enable vblank interrupt for
  824. *
  825. * Enable the interrupt on the requested crtc (all asics).
  826. * Returns 0 on success, -EINVAL on failure.
  827. */
  828. int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  829. {
  830. struct amdgpu_device *adev = dev->dev_private;
  831. int idx = amdgpu_crtc_idx_to_irq_type(adev, pipe);
  832. return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
  833. }
  834. /**
  835. * amdgpu_disable_vblank_kms - disable vblank interrupt
  836. *
  837. * @dev: drm dev pointer
  838. * @pipe: crtc to disable vblank interrupt for
  839. *
  840. * Disable the interrupt on the requested crtc (all asics).
  841. */
  842. void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  843. {
  844. struct amdgpu_device *adev = dev->dev_private;
  845. int idx = amdgpu_crtc_idx_to_irq_type(adev, pipe);
  846. amdgpu_irq_put(adev, &adev->crtc_irq, idx);
  847. }
  848. /**
  849. * amdgpu_get_vblank_timestamp_kms - get vblank timestamp
  850. *
  851. * @dev: drm dev pointer
  852. * @crtc: crtc to get the timestamp for
  853. * @max_error: max error
  854. * @vblank_time: time value
  855. * @flags: flags passed to the driver
  856. *
  857. * Gets the timestamp on the requested crtc based on the
  858. * scanout position. (all asics).
  859. * Returns postive status flags on success, negative error on failure.
  860. */
  861. int amdgpu_get_vblank_timestamp_kms(struct drm_device *dev, unsigned int pipe,
  862. int *max_error,
  863. struct timeval *vblank_time,
  864. unsigned flags)
  865. {
  866. struct drm_crtc *crtc;
  867. struct amdgpu_device *adev = dev->dev_private;
  868. if (pipe >= dev->num_crtcs) {
  869. DRM_ERROR("Invalid crtc %u\n", pipe);
  870. return -EINVAL;
  871. }
  872. /* Get associated drm_crtc: */
  873. crtc = &adev->mode_info.crtcs[pipe]->base;
  874. if (!crtc) {
  875. /* This can occur on driver load if some component fails to
  876. * initialize completely and driver is unloaded */
  877. DRM_ERROR("Uninitialized crtc %d\n", pipe);
  878. return -EINVAL;
  879. }
  880. /* Helper routine in DRM core does all the work: */
  881. return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
  882. vblank_time, flags,
  883. &crtc->hwmode);
  884. }
  885. const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  886. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  887. DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  888. DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  889. /* KMS */
  890. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  891. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  892. DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  893. DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  894. DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  895. DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  896. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  897. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  898. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  899. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  900. };
  901. const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);
  902. /*
  903. * Debugfs info
  904. */
  905. #if defined(CONFIG_DEBUG_FS)
  906. static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data)
  907. {
  908. struct drm_info_node *node = (struct drm_info_node *) m->private;
  909. struct drm_device *dev = node->minor->dev;
  910. struct amdgpu_device *adev = dev->dev_private;
  911. struct drm_amdgpu_info_firmware fw_info;
  912. struct drm_amdgpu_query_fw query_fw;
  913. int ret, i;
  914. /* VCE */
  915. query_fw.fw_type = AMDGPU_INFO_FW_VCE;
  916. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  917. if (ret)
  918. return ret;
  919. seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
  920. fw_info.feature, fw_info.ver);
  921. /* UVD */
  922. query_fw.fw_type = AMDGPU_INFO_FW_UVD;
  923. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  924. if (ret)
  925. return ret;
  926. seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
  927. fw_info.feature, fw_info.ver);
  928. /* GMC */
  929. query_fw.fw_type = AMDGPU_INFO_FW_GMC;
  930. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  931. if (ret)
  932. return ret;
  933. seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
  934. fw_info.feature, fw_info.ver);
  935. /* ME */
  936. query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
  937. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  938. if (ret)
  939. return ret;
  940. seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
  941. fw_info.feature, fw_info.ver);
  942. /* PFP */
  943. query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
  944. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  945. if (ret)
  946. return ret;
  947. seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
  948. fw_info.feature, fw_info.ver);
  949. /* CE */
  950. query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
  951. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  952. if (ret)
  953. return ret;
  954. seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
  955. fw_info.feature, fw_info.ver);
  956. /* RLC */
  957. query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
  958. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  959. if (ret)
  960. return ret;
  961. seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
  962. fw_info.feature, fw_info.ver);
  963. /* MEC */
  964. query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
  965. query_fw.index = 0;
  966. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  967. if (ret)
  968. return ret;
  969. seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
  970. fw_info.feature, fw_info.ver);
  971. /* MEC2 */
  972. if (adev->asic_type == CHIP_KAVERI ||
  973. (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) {
  974. query_fw.index = 1;
  975. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  976. if (ret)
  977. return ret;
  978. seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
  979. fw_info.feature, fw_info.ver);
  980. }
  981. /* PSP SOS */
  982. query_fw.fw_type = AMDGPU_INFO_FW_SOS;
  983. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  984. if (ret)
  985. return ret;
  986. seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n",
  987. fw_info.feature, fw_info.ver);
  988. /* PSP ASD */
  989. query_fw.fw_type = AMDGPU_INFO_FW_ASD;
  990. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  991. if (ret)
  992. return ret;
  993. seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n",
  994. fw_info.feature, fw_info.ver);
  995. /* SMC */
  996. query_fw.fw_type = AMDGPU_INFO_FW_SMC;
  997. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  998. if (ret)
  999. return ret;
  1000. seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n",
  1001. fw_info.feature, fw_info.ver);
  1002. /* SDMA */
  1003. query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
  1004. for (i = 0; i < adev->sdma.num_instances; i++) {
  1005. query_fw.index = i;
  1006. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1007. if (ret)
  1008. return ret;
  1009. seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
  1010. i, fw_info.feature, fw_info.ver);
  1011. }
  1012. return 0;
  1013. }
  1014. static const struct drm_info_list amdgpu_firmware_info_list[] = {
  1015. {"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL},
  1016. };
  1017. #endif
  1018. int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
  1019. {
  1020. #if defined(CONFIG_DEBUG_FS)
  1021. return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list,
  1022. ARRAY_SIZE(amdgpu_firmware_info_list));
  1023. #else
  1024. return 0;
  1025. #endif
  1026. }