amdgpu_kms.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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_sched.h"
  32. #include "amdgpu_uvd.h"
  33. #include "amdgpu_vce.h"
  34. #include "atom.h"
  35. #include <linux/vga_switcheroo.h>
  36. #include <linux/slab.h>
  37. #include <linux/pm_runtime.h>
  38. #include "amdgpu_amdkfd.h"
  39. #include "amdgpu_gem.h"
  40. #include "amdgpu_display.h"
  41. static void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev)
  42. {
  43. struct amdgpu_gpu_instance *gpu_instance;
  44. int i;
  45. mutex_lock(&mgpu_info.mutex);
  46. for (i = 0; i < mgpu_info.num_gpu; i++) {
  47. gpu_instance = &(mgpu_info.gpu_ins[i]);
  48. if (gpu_instance->adev == adev) {
  49. mgpu_info.gpu_ins[i] =
  50. mgpu_info.gpu_ins[mgpu_info.num_gpu - 1];
  51. mgpu_info.num_gpu--;
  52. if (adev->flags & AMD_IS_APU)
  53. mgpu_info.num_apu--;
  54. else
  55. mgpu_info.num_dgpu--;
  56. break;
  57. }
  58. }
  59. mutex_unlock(&mgpu_info.mutex);
  60. }
  61. /**
  62. * amdgpu_driver_unload_kms - Main unload function for KMS.
  63. *
  64. * @dev: drm dev pointer
  65. *
  66. * This is the main unload function for KMS (all asics).
  67. * Returns 0 on success.
  68. */
  69. void amdgpu_driver_unload_kms(struct drm_device *dev)
  70. {
  71. struct amdgpu_device *adev = dev->dev_private;
  72. if (adev == NULL)
  73. return;
  74. amdgpu_unregister_gpu_instance(adev);
  75. if (adev->rmmio == NULL)
  76. goto done_free;
  77. if (amdgpu_sriov_vf(adev))
  78. amdgpu_virt_request_full_gpu(adev, false);
  79. if (amdgpu_device_is_px(dev)) {
  80. pm_runtime_get_sync(dev->dev);
  81. pm_runtime_forbid(dev->dev);
  82. }
  83. amdgpu_acpi_fini(adev);
  84. amdgpu_device_fini(adev);
  85. done_free:
  86. kfree(adev);
  87. dev->dev_private = NULL;
  88. }
  89. static void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
  90. {
  91. struct amdgpu_gpu_instance *gpu_instance;
  92. mutex_lock(&mgpu_info.mutex);
  93. if (mgpu_info.num_gpu >= MAX_GPU_INSTANCE) {
  94. DRM_ERROR("Cannot register more gpu instance\n");
  95. mutex_unlock(&mgpu_info.mutex);
  96. return;
  97. }
  98. gpu_instance = &(mgpu_info.gpu_ins[mgpu_info.num_gpu]);
  99. gpu_instance->adev = adev;
  100. gpu_instance->mgpu_fan_enabled = 0;
  101. mgpu_info.num_gpu++;
  102. if (adev->flags & AMD_IS_APU)
  103. mgpu_info.num_apu++;
  104. else
  105. mgpu_info.num_dgpu++;
  106. mutex_unlock(&mgpu_info.mutex);
  107. }
  108. /**
  109. * amdgpu_driver_load_kms - Main load function for KMS.
  110. *
  111. * @dev: drm dev pointer
  112. * @flags: device flags
  113. *
  114. * This is the main load function for KMS (all asics).
  115. * Returns 0 on success, error on failure.
  116. */
  117. int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
  118. {
  119. struct amdgpu_device *adev;
  120. int r, acpi_status;
  121. #ifdef CONFIG_DRM_AMDGPU_SI
  122. if (!amdgpu_si_support) {
  123. switch (flags & AMD_ASIC_MASK) {
  124. case CHIP_TAHITI:
  125. case CHIP_PITCAIRN:
  126. case CHIP_VERDE:
  127. case CHIP_OLAND:
  128. case CHIP_HAINAN:
  129. dev_info(dev->dev,
  130. "SI support provided by radeon.\n");
  131. dev_info(dev->dev,
  132. "Use radeon.si_support=0 amdgpu.si_support=1 to override.\n"
  133. );
  134. return -ENODEV;
  135. }
  136. }
  137. #endif
  138. #ifdef CONFIG_DRM_AMDGPU_CIK
  139. if (!amdgpu_cik_support) {
  140. switch (flags & AMD_ASIC_MASK) {
  141. case CHIP_KAVERI:
  142. case CHIP_BONAIRE:
  143. case CHIP_HAWAII:
  144. case CHIP_KABINI:
  145. case CHIP_MULLINS:
  146. dev_info(dev->dev,
  147. "CIK support provided by radeon.\n");
  148. dev_info(dev->dev,
  149. "Use radeon.cik_support=0 amdgpu.cik_support=1 to override.\n"
  150. );
  151. return -ENODEV;
  152. }
  153. }
  154. #endif
  155. adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL);
  156. if (adev == NULL) {
  157. return -ENOMEM;
  158. }
  159. dev->dev_private = (void *)adev;
  160. if ((amdgpu_runtime_pm != 0) &&
  161. amdgpu_has_atpx() &&
  162. (amdgpu_is_atpx_hybrid() ||
  163. amdgpu_has_atpx_dgpu_power_cntl()) &&
  164. ((flags & AMD_IS_APU) == 0) &&
  165. !pci_is_thunderbolt_attached(dev->pdev))
  166. flags |= AMD_IS_PX;
  167. /* amdgpu_device_init should report only fatal error
  168. * like memory allocation failure or iomapping failure,
  169. * or memory manager initialization failure, it must
  170. * properly initialize the GPU MC controller and permit
  171. * VRAM allocation
  172. */
  173. r = amdgpu_device_init(adev, dev, dev->pdev, flags);
  174. if (r) {
  175. dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
  176. goto out;
  177. }
  178. /* Call ACPI methods: require modeset init
  179. * but failure is not fatal
  180. */
  181. if (!r) {
  182. acpi_status = amdgpu_acpi_init(adev);
  183. if (acpi_status)
  184. dev_dbg(&dev->pdev->dev,
  185. "Error during ACPI methods call\n");
  186. }
  187. if (amdgpu_device_is_px(dev)) {
  188. pm_runtime_use_autosuspend(dev->dev);
  189. pm_runtime_set_autosuspend_delay(dev->dev, 5000);
  190. pm_runtime_set_active(dev->dev);
  191. pm_runtime_allow(dev->dev);
  192. pm_runtime_mark_last_busy(dev->dev);
  193. pm_runtime_put_autosuspend(dev->dev);
  194. }
  195. amdgpu_register_gpu_instance(adev);
  196. out:
  197. if (r) {
  198. /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
  199. if (adev->rmmio && amdgpu_device_is_px(dev))
  200. pm_runtime_put_noidle(dev->dev);
  201. amdgpu_driver_unload_kms(dev);
  202. }
  203. return r;
  204. }
  205. static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info,
  206. struct drm_amdgpu_query_fw *query_fw,
  207. struct amdgpu_device *adev)
  208. {
  209. switch (query_fw->fw_type) {
  210. case AMDGPU_INFO_FW_VCE:
  211. fw_info->ver = adev->vce.fw_version;
  212. fw_info->feature = adev->vce.fb_version;
  213. break;
  214. case AMDGPU_INFO_FW_UVD:
  215. fw_info->ver = adev->uvd.fw_version;
  216. fw_info->feature = 0;
  217. break;
  218. case AMDGPU_INFO_FW_VCN:
  219. fw_info->ver = adev->vcn.fw_version;
  220. fw_info->feature = 0;
  221. break;
  222. case AMDGPU_INFO_FW_GMC:
  223. fw_info->ver = adev->gmc.fw_version;
  224. fw_info->feature = 0;
  225. break;
  226. case AMDGPU_INFO_FW_GFX_ME:
  227. fw_info->ver = adev->gfx.me_fw_version;
  228. fw_info->feature = adev->gfx.me_feature_version;
  229. break;
  230. case AMDGPU_INFO_FW_GFX_PFP:
  231. fw_info->ver = adev->gfx.pfp_fw_version;
  232. fw_info->feature = adev->gfx.pfp_feature_version;
  233. break;
  234. case AMDGPU_INFO_FW_GFX_CE:
  235. fw_info->ver = adev->gfx.ce_fw_version;
  236. fw_info->feature = adev->gfx.ce_feature_version;
  237. break;
  238. case AMDGPU_INFO_FW_GFX_RLC:
  239. fw_info->ver = adev->gfx.rlc_fw_version;
  240. fw_info->feature = adev->gfx.rlc_feature_version;
  241. break;
  242. case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL:
  243. fw_info->ver = adev->gfx.rlc_srlc_fw_version;
  244. fw_info->feature = adev->gfx.rlc_srlc_feature_version;
  245. break;
  246. case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM:
  247. fw_info->ver = adev->gfx.rlc_srlg_fw_version;
  248. fw_info->feature = adev->gfx.rlc_srlg_feature_version;
  249. break;
  250. case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM:
  251. fw_info->ver = adev->gfx.rlc_srls_fw_version;
  252. fw_info->feature = adev->gfx.rlc_srls_feature_version;
  253. break;
  254. case AMDGPU_INFO_FW_GFX_MEC:
  255. if (query_fw->index == 0) {
  256. fw_info->ver = adev->gfx.mec_fw_version;
  257. fw_info->feature = adev->gfx.mec_feature_version;
  258. } else if (query_fw->index == 1) {
  259. fw_info->ver = adev->gfx.mec2_fw_version;
  260. fw_info->feature = adev->gfx.mec2_feature_version;
  261. } else
  262. return -EINVAL;
  263. break;
  264. case AMDGPU_INFO_FW_SMC:
  265. fw_info->ver = adev->pm.fw_version;
  266. fw_info->feature = 0;
  267. break;
  268. case AMDGPU_INFO_FW_SDMA:
  269. if (query_fw->index >= adev->sdma.num_instances)
  270. return -EINVAL;
  271. fw_info->ver = adev->sdma.instance[query_fw->index].fw_version;
  272. fw_info->feature = adev->sdma.instance[query_fw->index].feature_version;
  273. break;
  274. case AMDGPU_INFO_FW_SOS:
  275. fw_info->ver = adev->psp.sos_fw_version;
  276. fw_info->feature = adev->psp.sos_feature_version;
  277. break;
  278. case AMDGPU_INFO_FW_ASD:
  279. fw_info->ver = adev->psp.asd_fw_version;
  280. fw_info->feature = adev->psp.asd_feature_version;
  281. break;
  282. case AMDGPU_INFO_FW_DMCU:
  283. fw_info->ver = adev->dm.dmcu_fw_version;
  284. fw_info->feature = 0;
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. return 0;
  290. }
  291. static int amdgpu_hw_ip_info(struct amdgpu_device *adev,
  292. struct drm_amdgpu_info *info,
  293. struct drm_amdgpu_info_hw_ip *result)
  294. {
  295. uint32_t ib_start_alignment = 0;
  296. uint32_t ib_size_alignment = 0;
  297. enum amd_ip_block_type type;
  298. unsigned int num_rings = 0;
  299. unsigned int i, j;
  300. if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  301. return -EINVAL;
  302. switch (info->query_hw_ip.type) {
  303. case AMDGPU_HW_IP_GFX:
  304. type = AMD_IP_BLOCK_TYPE_GFX;
  305. for (i = 0; i < adev->gfx.num_gfx_rings; i++)
  306. if (adev->gfx.gfx_ring[i].ready)
  307. ++num_rings;
  308. ib_start_alignment = 32;
  309. ib_size_alignment = 32;
  310. break;
  311. case AMDGPU_HW_IP_COMPUTE:
  312. type = AMD_IP_BLOCK_TYPE_GFX;
  313. for (i = 0; i < adev->gfx.num_compute_rings; i++)
  314. if (adev->gfx.compute_ring[i].ready)
  315. ++num_rings;
  316. ib_start_alignment = 32;
  317. ib_size_alignment = 32;
  318. break;
  319. case AMDGPU_HW_IP_DMA:
  320. type = AMD_IP_BLOCK_TYPE_SDMA;
  321. for (i = 0; i < adev->sdma.num_instances; i++)
  322. if (adev->sdma.instance[i].ring.ready)
  323. ++num_rings;
  324. ib_start_alignment = 256;
  325. ib_size_alignment = 4;
  326. break;
  327. case AMDGPU_HW_IP_UVD:
  328. type = AMD_IP_BLOCK_TYPE_UVD;
  329. for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
  330. if (adev->uvd.harvest_config & (1 << i))
  331. continue;
  332. if (adev->uvd.inst[i].ring.ready)
  333. ++num_rings;
  334. }
  335. ib_start_alignment = 64;
  336. ib_size_alignment = 64;
  337. break;
  338. case AMDGPU_HW_IP_VCE:
  339. type = AMD_IP_BLOCK_TYPE_VCE;
  340. for (i = 0; i < adev->vce.num_rings; i++)
  341. if (adev->vce.ring[i].ready)
  342. ++num_rings;
  343. ib_start_alignment = 4;
  344. ib_size_alignment = 1;
  345. break;
  346. case AMDGPU_HW_IP_UVD_ENC:
  347. type = AMD_IP_BLOCK_TYPE_UVD;
  348. for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
  349. if (adev->uvd.harvest_config & (1 << i))
  350. continue;
  351. for (j = 0; j < adev->uvd.num_enc_rings; j++)
  352. if (adev->uvd.inst[i].ring_enc[j].ready)
  353. ++num_rings;
  354. }
  355. ib_start_alignment = 64;
  356. ib_size_alignment = 64;
  357. break;
  358. case AMDGPU_HW_IP_VCN_DEC:
  359. type = AMD_IP_BLOCK_TYPE_VCN;
  360. if (adev->vcn.ring_dec.ready)
  361. ++num_rings;
  362. ib_start_alignment = 16;
  363. ib_size_alignment = 16;
  364. break;
  365. case AMDGPU_HW_IP_VCN_ENC:
  366. type = AMD_IP_BLOCK_TYPE_VCN;
  367. for (i = 0; i < adev->vcn.num_enc_rings; i++)
  368. if (adev->vcn.ring_enc[i].ready)
  369. ++num_rings;
  370. ib_start_alignment = 64;
  371. ib_size_alignment = 1;
  372. break;
  373. case AMDGPU_HW_IP_VCN_JPEG:
  374. type = AMD_IP_BLOCK_TYPE_VCN;
  375. if (adev->vcn.ring_jpeg.ready)
  376. ++num_rings;
  377. ib_start_alignment = 16;
  378. ib_size_alignment = 16;
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. for (i = 0; i < adev->num_ip_blocks; i++)
  384. if (adev->ip_blocks[i].version->type == type &&
  385. adev->ip_blocks[i].status.valid)
  386. break;
  387. if (i == adev->num_ip_blocks)
  388. return 0;
  389. num_rings = min(amdgpu_ctx_num_entities[info->query_hw_ip.type],
  390. num_rings);
  391. result->hw_ip_version_major = adev->ip_blocks[i].version->major;
  392. result->hw_ip_version_minor = adev->ip_blocks[i].version->minor;
  393. result->capabilities_flags = 0;
  394. result->available_rings = (1 << num_rings) - 1;
  395. result->ib_start_alignment = ib_start_alignment;
  396. result->ib_size_alignment = ib_size_alignment;
  397. return 0;
  398. }
  399. /*
  400. * Userspace get information ioctl
  401. */
  402. /**
  403. * amdgpu_info_ioctl - answer a device specific request.
  404. *
  405. * @adev: amdgpu device pointer
  406. * @data: request object
  407. * @filp: drm filp
  408. *
  409. * This function is used to pass device specific parameters to the userspace
  410. * drivers. Examples include: pci device id, pipeline parms, tiling params,
  411. * etc. (all asics).
  412. * Returns 0 on success, -EINVAL on failure.
  413. */
  414. static int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  415. {
  416. struct amdgpu_device *adev = dev->dev_private;
  417. struct drm_amdgpu_info *info = data;
  418. struct amdgpu_mode_info *minfo = &adev->mode_info;
  419. void __user *out = (void __user *)(uintptr_t)info->return_pointer;
  420. uint32_t size = info->return_size;
  421. struct drm_crtc *crtc;
  422. uint32_t ui32 = 0;
  423. uint64_t ui64 = 0;
  424. int i, found;
  425. int ui32_size = sizeof(ui32);
  426. if (!info->return_size || !info->return_pointer)
  427. return -EINVAL;
  428. switch (info->query) {
  429. case AMDGPU_INFO_ACCEL_WORKING:
  430. ui32 = adev->accel_working;
  431. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  432. case AMDGPU_INFO_CRTC_FROM_ID:
  433. for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
  434. crtc = (struct drm_crtc *)minfo->crtcs[i];
  435. if (crtc && crtc->base.id == info->mode_crtc.id) {
  436. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  437. ui32 = amdgpu_crtc->crtc_id;
  438. found = 1;
  439. break;
  440. }
  441. }
  442. if (!found) {
  443. DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
  444. return -EINVAL;
  445. }
  446. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  447. case AMDGPU_INFO_HW_IP_INFO: {
  448. struct drm_amdgpu_info_hw_ip ip = {};
  449. int ret;
  450. ret = amdgpu_hw_ip_info(adev, info, &ip);
  451. if (ret)
  452. return ret;
  453. ret = copy_to_user(out, &ip, min((size_t)size, sizeof(ip)));
  454. return ret ? -EFAULT : 0;
  455. }
  456. case AMDGPU_INFO_HW_IP_COUNT: {
  457. enum amd_ip_block_type type;
  458. uint32_t count = 0;
  459. switch (info->query_hw_ip.type) {
  460. case AMDGPU_HW_IP_GFX:
  461. type = AMD_IP_BLOCK_TYPE_GFX;
  462. break;
  463. case AMDGPU_HW_IP_COMPUTE:
  464. type = AMD_IP_BLOCK_TYPE_GFX;
  465. break;
  466. case AMDGPU_HW_IP_DMA:
  467. type = AMD_IP_BLOCK_TYPE_SDMA;
  468. break;
  469. case AMDGPU_HW_IP_UVD:
  470. type = AMD_IP_BLOCK_TYPE_UVD;
  471. break;
  472. case AMDGPU_HW_IP_VCE:
  473. type = AMD_IP_BLOCK_TYPE_VCE;
  474. break;
  475. case AMDGPU_HW_IP_UVD_ENC:
  476. type = AMD_IP_BLOCK_TYPE_UVD;
  477. break;
  478. case AMDGPU_HW_IP_VCN_DEC:
  479. case AMDGPU_HW_IP_VCN_ENC:
  480. case AMDGPU_HW_IP_VCN_JPEG:
  481. type = AMD_IP_BLOCK_TYPE_VCN;
  482. break;
  483. default:
  484. return -EINVAL;
  485. }
  486. for (i = 0; i < adev->num_ip_blocks; i++)
  487. if (adev->ip_blocks[i].version->type == type &&
  488. adev->ip_blocks[i].status.valid &&
  489. count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  490. count++;
  491. return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
  492. }
  493. case AMDGPU_INFO_TIMESTAMP:
  494. ui64 = amdgpu_gfx_get_gpu_clock_counter(adev);
  495. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  496. case AMDGPU_INFO_FW_VERSION: {
  497. struct drm_amdgpu_info_firmware fw_info;
  498. int ret;
  499. /* We only support one instance of each IP block right now. */
  500. if (info->query_fw.ip_instance != 0)
  501. return -EINVAL;
  502. ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev);
  503. if (ret)
  504. return ret;
  505. return copy_to_user(out, &fw_info,
  506. min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
  507. }
  508. case AMDGPU_INFO_NUM_BYTES_MOVED:
  509. ui64 = atomic64_read(&adev->num_bytes_moved);
  510. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  511. case AMDGPU_INFO_NUM_EVICTIONS:
  512. ui64 = atomic64_read(&adev->num_evictions);
  513. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  514. case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS:
  515. ui64 = atomic64_read(&adev->num_vram_cpu_page_faults);
  516. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  517. case AMDGPU_INFO_VRAM_USAGE:
  518. ui64 = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
  519. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  520. case AMDGPU_INFO_VIS_VRAM_USAGE:
  521. ui64 = amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
  522. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  523. case AMDGPU_INFO_GTT_USAGE:
  524. ui64 = amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]);
  525. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  526. case AMDGPU_INFO_GDS_CONFIG: {
  527. struct drm_amdgpu_info_gds gds_info;
  528. memset(&gds_info, 0, sizeof(gds_info));
  529. gds_info.gds_gfx_partition_size = adev->gds.mem.gfx_partition_size;
  530. gds_info.compute_partition_size = adev->gds.mem.cs_partition_size;
  531. gds_info.gds_total_size = adev->gds.mem.total_size;
  532. gds_info.gws_per_gfx_partition = adev->gds.gws.gfx_partition_size;
  533. gds_info.gws_per_compute_partition = adev->gds.gws.cs_partition_size;
  534. gds_info.oa_per_gfx_partition = adev->gds.oa.gfx_partition_size;
  535. gds_info.oa_per_compute_partition = adev->gds.oa.cs_partition_size;
  536. return copy_to_user(out, &gds_info,
  537. min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
  538. }
  539. case AMDGPU_INFO_VRAM_GTT: {
  540. struct drm_amdgpu_info_vram_gtt vram_gtt;
  541. vram_gtt.vram_size = adev->gmc.real_vram_size -
  542. atomic64_read(&adev->vram_pin_size);
  543. vram_gtt.vram_cpu_accessible_size = adev->gmc.visible_vram_size -
  544. atomic64_read(&adev->visible_pin_size);
  545. vram_gtt.gtt_size = adev->mman.bdev.man[TTM_PL_TT].size;
  546. vram_gtt.gtt_size *= PAGE_SIZE;
  547. vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size);
  548. return copy_to_user(out, &vram_gtt,
  549. min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
  550. }
  551. case AMDGPU_INFO_MEMORY: {
  552. struct drm_amdgpu_memory_info mem;
  553. memset(&mem, 0, sizeof(mem));
  554. mem.vram.total_heap_size = adev->gmc.real_vram_size;
  555. mem.vram.usable_heap_size = adev->gmc.real_vram_size -
  556. atomic64_read(&adev->vram_pin_size);
  557. mem.vram.heap_usage =
  558. amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
  559. mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
  560. mem.cpu_accessible_vram.total_heap_size =
  561. adev->gmc.visible_vram_size;
  562. mem.cpu_accessible_vram.usable_heap_size = adev->gmc.visible_vram_size -
  563. atomic64_read(&adev->visible_pin_size);
  564. mem.cpu_accessible_vram.heap_usage =
  565. amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
  566. mem.cpu_accessible_vram.max_allocation =
  567. mem.cpu_accessible_vram.usable_heap_size * 3 / 4;
  568. mem.gtt.total_heap_size = adev->mman.bdev.man[TTM_PL_TT].size;
  569. mem.gtt.total_heap_size *= PAGE_SIZE;
  570. mem.gtt.usable_heap_size = mem.gtt.total_heap_size -
  571. atomic64_read(&adev->gart_pin_size);
  572. mem.gtt.heap_usage =
  573. amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]);
  574. mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4;
  575. return copy_to_user(out, &mem,
  576. min((size_t)size, sizeof(mem)))
  577. ? -EFAULT : 0;
  578. }
  579. case AMDGPU_INFO_READ_MMR_REG: {
  580. unsigned n, alloc_size;
  581. uint32_t *regs;
  582. unsigned se_num = (info->read_mmr_reg.instance >>
  583. AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
  584. AMDGPU_INFO_MMR_SE_INDEX_MASK;
  585. unsigned sh_num = (info->read_mmr_reg.instance >>
  586. AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
  587. AMDGPU_INFO_MMR_SH_INDEX_MASK;
  588. /* set full masks if the userspace set all bits
  589. * in the bitfields */
  590. if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
  591. se_num = 0xffffffff;
  592. if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
  593. sh_num = 0xffffffff;
  594. regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
  595. if (!regs)
  596. return -ENOMEM;
  597. alloc_size = info->read_mmr_reg.count * sizeof(*regs);
  598. for (i = 0; i < info->read_mmr_reg.count; i++)
  599. if (amdgpu_asic_read_register(adev, se_num, sh_num,
  600. info->read_mmr_reg.dword_offset + i,
  601. &regs[i])) {
  602. DRM_DEBUG_KMS("unallowed offset %#x\n",
  603. info->read_mmr_reg.dword_offset + i);
  604. kfree(regs);
  605. return -EFAULT;
  606. }
  607. n = copy_to_user(out, regs, min(size, alloc_size));
  608. kfree(regs);
  609. return n ? -EFAULT : 0;
  610. }
  611. case AMDGPU_INFO_DEV_INFO: {
  612. struct drm_amdgpu_info_device dev_info = {};
  613. uint64_t vm_size;
  614. dev_info.device_id = dev->pdev->device;
  615. dev_info.chip_rev = adev->rev_id;
  616. dev_info.external_rev = adev->external_rev_id;
  617. dev_info.pci_rev = dev->pdev->revision;
  618. dev_info.family = adev->family;
  619. dev_info.num_shader_engines = adev->gfx.config.max_shader_engines;
  620. dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
  621. /* return all clocks in KHz */
  622. dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
  623. if (adev->pm.dpm_enabled) {
  624. dev_info.max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10;
  625. dev_info.max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10;
  626. } else {
  627. dev_info.max_engine_clock = adev->clock.default_sclk * 10;
  628. dev_info.max_memory_clock = adev->clock.default_mclk * 10;
  629. }
  630. dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
  631. dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se *
  632. adev->gfx.config.max_shader_engines;
  633. dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
  634. dev_info._pad = 0;
  635. dev_info.ids_flags = 0;
  636. if (adev->flags & AMD_IS_APU)
  637. dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
  638. if (amdgpu_sriov_vf(adev))
  639. dev_info.ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION;
  640. vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
  641. vm_size -= AMDGPU_VA_RESERVED_SIZE;
  642. /* Older VCE FW versions are buggy and can handle only 40bits */
  643. if (adev->vce.fw_version &&
  644. adev->vce.fw_version < AMDGPU_VCE_FW_53_45)
  645. vm_size = min(vm_size, 1ULL << 40);
  646. dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
  647. dev_info.virtual_address_max =
  648. min(vm_size, AMDGPU_GMC_HOLE_START);
  649. if (vm_size > AMDGPU_GMC_HOLE_START) {
  650. dev_info.high_va_offset = AMDGPU_GMC_HOLE_END;
  651. dev_info.high_va_max = AMDGPU_GMC_HOLE_END | vm_size;
  652. }
  653. dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
  654. dev_info.pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE;
  655. dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE;
  656. dev_info.cu_active_number = adev->gfx.cu_info.number;
  657. dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
  658. dev_info.ce_ram_size = adev->gfx.ce_ram_size;
  659. memcpy(&dev_info.cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0],
  660. sizeof(adev->gfx.cu_info.ao_cu_bitmap));
  661. memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
  662. sizeof(adev->gfx.cu_info.bitmap));
  663. dev_info.vram_type = adev->gmc.vram_type;
  664. dev_info.vram_bit_width = adev->gmc.vram_width;
  665. dev_info.vce_harvest_config = adev->vce.harvest_config;
  666. dev_info.gc_double_offchip_lds_buf =
  667. adev->gfx.config.double_offchip_lds_buf;
  668. if (amdgpu_ngg) {
  669. dev_info.prim_buf_gpu_addr = adev->gfx.ngg.buf[NGG_PRIM].gpu_addr;
  670. dev_info.prim_buf_size = adev->gfx.ngg.buf[NGG_PRIM].size;
  671. dev_info.pos_buf_gpu_addr = adev->gfx.ngg.buf[NGG_POS].gpu_addr;
  672. dev_info.pos_buf_size = adev->gfx.ngg.buf[NGG_POS].size;
  673. dev_info.cntl_sb_buf_gpu_addr = adev->gfx.ngg.buf[NGG_CNTL].gpu_addr;
  674. dev_info.cntl_sb_buf_size = adev->gfx.ngg.buf[NGG_CNTL].size;
  675. dev_info.param_buf_gpu_addr = adev->gfx.ngg.buf[NGG_PARAM].gpu_addr;
  676. dev_info.param_buf_size = adev->gfx.ngg.buf[NGG_PARAM].size;
  677. }
  678. dev_info.wave_front_size = adev->gfx.cu_info.wave_front_size;
  679. dev_info.num_shader_visible_vgprs = adev->gfx.config.max_gprs;
  680. dev_info.num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
  681. dev_info.num_tcc_blocks = adev->gfx.config.max_texture_channel_caches;
  682. dev_info.gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth;
  683. dev_info.gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth;
  684. dev_info.max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads;
  685. return copy_to_user(out, &dev_info,
  686. min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0;
  687. }
  688. case AMDGPU_INFO_VCE_CLOCK_TABLE: {
  689. unsigned i;
  690. struct drm_amdgpu_info_vce_clock_table vce_clk_table = {};
  691. struct amd_vce_state *vce_state;
  692. for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) {
  693. vce_state = amdgpu_dpm_get_vce_clock_state(adev, i);
  694. if (vce_state) {
  695. vce_clk_table.entries[i].sclk = vce_state->sclk;
  696. vce_clk_table.entries[i].mclk = vce_state->mclk;
  697. vce_clk_table.entries[i].eclk = vce_state->evclk;
  698. vce_clk_table.num_valid_entries++;
  699. }
  700. }
  701. return copy_to_user(out, &vce_clk_table,
  702. min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0;
  703. }
  704. case AMDGPU_INFO_VBIOS: {
  705. uint32_t bios_size = adev->bios_size;
  706. switch (info->vbios_info.type) {
  707. case AMDGPU_INFO_VBIOS_SIZE:
  708. return copy_to_user(out, &bios_size,
  709. min((size_t)size, sizeof(bios_size)))
  710. ? -EFAULT : 0;
  711. case AMDGPU_INFO_VBIOS_IMAGE: {
  712. uint8_t *bios;
  713. uint32_t bios_offset = info->vbios_info.offset;
  714. if (bios_offset >= bios_size)
  715. return -EINVAL;
  716. bios = adev->bios + bios_offset;
  717. return copy_to_user(out, bios,
  718. min((size_t)size, (size_t)(bios_size - bios_offset)))
  719. ? -EFAULT : 0;
  720. }
  721. default:
  722. DRM_DEBUG_KMS("Invalid request %d\n",
  723. info->vbios_info.type);
  724. return -EINVAL;
  725. }
  726. }
  727. case AMDGPU_INFO_NUM_HANDLES: {
  728. struct drm_amdgpu_info_num_handles handle;
  729. switch (info->query_hw_ip.type) {
  730. case AMDGPU_HW_IP_UVD:
  731. /* Starting Polaris, we support unlimited UVD handles */
  732. if (adev->asic_type < CHIP_POLARIS10) {
  733. handle.uvd_max_handles = adev->uvd.max_handles;
  734. handle.uvd_used_handles = amdgpu_uvd_used_handles(adev);
  735. return copy_to_user(out, &handle,
  736. min((size_t)size, sizeof(handle))) ? -EFAULT : 0;
  737. } else {
  738. return -ENODATA;
  739. }
  740. break;
  741. default:
  742. return -EINVAL;
  743. }
  744. }
  745. case AMDGPU_INFO_SENSOR: {
  746. if (!adev->pm.dpm_enabled)
  747. return -ENOENT;
  748. switch (info->sensor_info.type) {
  749. case AMDGPU_INFO_SENSOR_GFX_SCLK:
  750. /* get sclk in Mhz */
  751. if (amdgpu_dpm_read_sensor(adev,
  752. AMDGPU_PP_SENSOR_GFX_SCLK,
  753. (void *)&ui32, &ui32_size)) {
  754. return -EINVAL;
  755. }
  756. ui32 /= 100;
  757. break;
  758. case AMDGPU_INFO_SENSOR_GFX_MCLK:
  759. /* get mclk in Mhz */
  760. if (amdgpu_dpm_read_sensor(adev,
  761. AMDGPU_PP_SENSOR_GFX_MCLK,
  762. (void *)&ui32, &ui32_size)) {
  763. return -EINVAL;
  764. }
  765. ui32 /= 100;
  766. break;
  767. case AMDGPU_INFO_SENSOR_GPU_TEMP:
  768. /* get temperature in millidegrees C */
  769. if (amdgpu_dpm_read_sensor(adev,
  770. AMDGPU_PP_SENSOR_GPU_TEMP,
  771. (void *)&ui32, &ui32_size)) {
  772. return -EINVAL;
  773. }
  774. break;
  775. case AMDGPU_INFO_SENSOR_GPU_LOAD:
  776. /* get GPU load */
  777. if (amdgpu_dpm_read_sensor(adev,
  778. AMDGPU_PP_SENSOR_GPU_LOAD,
  779. (void *)&ui32, &ui32_size)) {
  780. return -EINVAL;
  781. }
  782. break;
  783. case AMDGPU_INFO_SENSOR_GPU_AVG_POWER:
  784. /* get average GPU power */
  785. if (amdgpu_dpm_read_sensor(adev,
  786. AMDGPU_PP_SENSOR_GPU_POWER,
  787. (void *)&ui32, &ui32_size)) {
  788. return -EINVAL;
  789. }
  790. ui32 >>= 8;
  791. break;
  792. case AMDGPU_INFO_SENSOR_VDDNB:
  793. /* get VDDNB in millivolts */
  794. if (amdgpu_dpm_read_sensor(adev,
  795. AMDGPU_PP_SENSOR_VDDNB,
  796. (void *)&ui32, &ui32_size)) {
  797. return -EINVAL;
  798. }
  799. break;
  800. case AMDGPU_INFO_SENSOR_VDDGFX:
  801. /* get VDDGFX in millivolts */
  802. if (amdgpu_dpm_read_sensor(adev,
  803. AMDGPU_PP_SENSOR_VDDGFX,
  804. (void *)&ui32, &ui32_size)) {
  805. return -EINVAL;
  806. }
  807. break;
  808. case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK:
  809. /* get stable pstate sclk in Mhz */
  810. if (amdgpu_dpm_read_sensor(adev,
  811. AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK,
  812. (void *)&ui32, &ui32_size)) {
  813. return -EINVAL;
  814. }
  815. ui32 /= 100;
  816. break;
  817. case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK:
  818. /* get stable pstate mclk in Mhz */
  819. if (amdgpu_dpm_read_sensor(adev,
  820. AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK,
  821. (void *)&ui32, &ui32_size)) {
  822. return -EINVAL;
  823. }
  824. ui32 /= 100;
  825. break;
  826. default:
  827. DRM_DEBUG_KMS("Invalid request %d\n",
  828. info->sensor_info.type);
  829. return -EINVAL;
  830. }
  831. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  832. }
  833. case AMDGPU_INFO_VRAM_LOST_COUNTER:
  834. ui32 = atomic_read(&adev->vram_lost_counter);
  835. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  836. default:
  837. DRM_DEBUG_KMS("Invalid request %d\n", info->query);
  838. return -EINVAL;
  839. }
  840. return 0;
  841. }
  842. /*
  843. * Outdated mess for old drm with Xorg being in charge (void function now).
  844. */
  845. /**
  846. * amdgpu_driver_lastclose_kms - drm callback for last close
  847. *
  848. * @dev: drm dev pointer
  849. *
  850. * Switch vga_switcheroo state after last close (all asics).
  851. */
  852. void amdgpu_driver_lastclose_kms(struct drm_device *dev)
  853. {
  854. drm_fb_helper_lastclose(dev);
  855. vga_switcheroo_process_delayed_switch();
  856. }
  857. /**
  858. * amdgpu_driver_open_kms - drm callback for open
  859. *
  860. * @dev: drm dev pointer
  861. * @file_priv: drm file
  862. *
  863. * On device open, init vm on cayman+ (all asics).
  864. * Returns 0 on success, error on failure.
  865. */
  866. int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
  867. {
  868. struct amdgpu_device *adev = dev->dev_private;
  869. struct amdgpu_fpriv *fpriv;
  870. int r, pasid;
  871. /* Ensure IB tests are run on ring */
  872. flush_delayed_work(&adev->late_init_work);
  873. file_priv->driver_priv = NULL;
  874. r = pm_runtime_get_sync(dev->dev);
  875. if (r < 0)
  876. return r;
  877. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  878. if (unlikely(!fpriv)) {
  879. r = -ENOMEM;
  880. goto out_suspend;
  881. }
  882. pasid = amdgpu_pasid_alloc(16);
  883. if (pasid < 0) {
  884. dev_warn(adev->dev, "No more PASIDs available!");
  885. pasid = 0;
  886. }
  887. r = amdgpu_vm_init(adev, &fpriv->vm, AMDGPU_VM_CONTEXT_GFX, pasid);
  888. if (r)
  889. goto error_pasid;
  890. fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL);
  891. if (!fpriv->prt_va) {
  892. r = -ENOMEM;
  893. goto error_vm;
  894. }
  895. if (amdgpu_sriov_vf(adev)) {
  896. r = amdgpu_map_static_csa(adev, &fpriv->vm, &fpriv->csa_va);
  897. if (r)
  898. goto error_vm;
  899. }
  900. mutex_init(&fpriv->bo_list_lock);
  901. idr_init(&fpriv->bo_list_handles);
  902. amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
  903. file_priv->driver_priv = fpriv;
  904. goto out_suspend;
  905. error_vm:
  906. amdgpu_vm_fini(adev, &fpriv->vm);
  907. error_pasid:
  908. if (pasid)
  909. amdgpu_pasid_free(pasid);
  910. kfree(fpriv);
  911. out_suspend:
  912. pm_runtime_mark_last_busy(dev->dev);
  913. pm_runtime_put_autosuspend(dev->dev);
  914. return r;
  915. }
  916. /**
  917. * amdgpu_driver_postclose_kms - drm callback for post close
  918. *
  919. * @dev: drm dev pointer
  920. * @file_priv: drm file
  921. *
  922. * On device post close, tear down vm on cayman+ (all asics).
  923. */
  924. void amdgpu_driver_postclose_kms(struct drm_device *dev,
  925. struct drm_file *file_priv)
  926. {
  927. struct amdgpu_device *adev = dev->dev_private;
  928. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  929. struct amdgpu_bo_list *list;
  930. struct amdgpu_bo *pd;
  931. unsigned int pasid;
  932. int handle;
  933. if (!fpriv)
  934. return;
  935. pm_runtime_get_sync(dev->dev);
  936. if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL)
  937. amdgpu_uvd_free_handles(adev, file_priv);
  938. if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL)
  939. amdgpu_vce_free_handles(adev, file_priv);
  940. amdgpu_vm_bo_rmv(adev, fpriv->prt_va);
  941. if (amdgpu_sriov_vf(adev)) {
  942. /* TODO: how to handle reserve failure */
  943. BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true));
  944. amdgpu_vm_bo_rmv(adev, fpriv->csa_va);
  945. fpriv->csa_va = NULL;
  946. amdgpu_bo_unreserve(adev->virt.csa_obj);
  947. }
  948. pasid = fpriv->vm.pasid;
  949. pd = amdgpu_bo_ref(fpriv->vm.root.base.bo);
  950. amdgpu_vm_fini(adev, &fpriv->vm);
  951. amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
  952. if (pasid)
  953. amdgpu_pasid_free_delayed(pd->tbo.resv, pasid);
  954. amdgpu_bo_unref(&pd);
  955. idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
  956. amdgpu_bo_list_put(list);
  957. idr_destroy(&fpriv->bo_list_handles);
  958. mutex_destroy(&fpriv->bo_list_lock);
  959. kfree(fpriv);
  960. file_priv->driver_priv = NULL;
  961. pm_runtime_mark_last_busy(dev->dev);
  962. pm_runtime_put_autosuspend(dev->dev);
  963. }
  964. /*
  965. * VBlank related functions.
  966. */
  967. /**
  968. * amdgpu_get_vblank_counter_kms - get frame count
  969. *
  970. * @dev: drm dev pointer
  971. * @pipe: crtc to get the frame count from
  972. *
  973. * Gets the frame count on the requested crtc (all asics).
  974. * Returns frame count on success, -EINVAL on failure.
  975. */
  976. u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
  977. {
  978. struct amdgpu_device *adev = dev->dev_private;
  979. int vpos, hpos, stat;
  980. u32 count;
  981. if (pipe >= adev->mode_info.num_crtc) {
  982. DRM_ERROR("Invalid crtc %u\n", pipe);
  983. return -EINVAL;
  984. }
  985. /* The hw increments its frame counter at start of vsync, not at start
  986. * of vblank, as is required by DRM core vblank counter handling.
  987. * Cook the hw count here to make it appear to the caller as if it
  988. * incremented at start of vblank. We measure distance to start of
  989. * vblank in vpos. vpos therefore will be >= 0 between start of vblank
  990. * and start of vsync, so vpos >= 0 means to bump the hw frame counter
  991. * result by 1 to give the proper appearance to caller.
  992. */
  993. if (adev->mode_info.crtcs[pipe]) {
  994. /* Repeat readout if needed to provide stable result if
  995. * we cross start of vsync during the queries.
  996. */
  997. do {
  998. count = amdgpu_display_vblank_get_counter(adev, pipe);
  999. /* Ask amdgpu_display_get_crtc_scanoutpos to return
  1000. * vpos as distance to start of vblank, instead of
  1001. * regular vertical scanout pos.
  1002. */
  1003. stat = amdgpu_display_get_crtc_scanoutpos(
  1004. dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
  1005. &vpos, &hpos, NULL, NULL,
  1006. &adev->mode_info.crtcs[pipe]->base.hwmode);
  1007. } while (count != amdgpu_display_vblank_get_counter(adev, pipe));
  1008. if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
  1009. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
  1010. DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
  1011. } else {
  1012. DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
  1013. pipe, vpos);
  1014. /* Bump counter if we are at >= leading edge of vblank,
  1015. * but before vsync where vpos would turn negative and
  1016. * the hw counter really increments.
  1017. */
  1018. if (vpos >= 0)
  1019. count++;
  1020. }
  1021. } else {
  1022. /* Fallback to use value as is. */
  1023. count = amdgpu_display_vblank_get_counter(adev, pipe);
  1024. DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
  1025. }
  1026. return count;
  1027. }
  1028. /**
  1029. * amdgpu_enable_vblank_kms - enable vblank interrupt
  1030. *
  1031. * @dev: drm dev pointer
  1032. * @pipe: crtc to enable vblank interrupt for
  1033. *
  1034. * Enable the interrupt on the requested crtc (all asics).
  1035. * Returns 0 on success, -EINVAL on failure.
  1036. */
  1037. int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  1038. {
  1039. struct amdgpu_device *adev = dev->dev_private;
  1040. int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
  1041. return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
  1042. }
  1043. /**
  1044. * amdgpu_disable_vblank_kms - disable vblank interrupt
  1045. *
  1046. * @dev: drm dev pointer
  1047. * @pipe: crtc to disable vblank interrupt for
  1048. *
  1049. * Disable the interrupt on the requested crtc (all asics).
  1050. */
  1051. void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  1052. {
  1053. struct amdgpu_device *adev = dev->dev_private;
  1054. int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
  1055. amdgpu_irq_put(adev, &adev->crtc_irq, idx);
  1056. }
  1057. const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  1058. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1059. DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1060. DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1061. DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER),
  1062. DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1063. DRM_IOCTL_DEF_DRV(AMDGPU_FENCE_TO_HANDLE, amdgpu_cs_fence_to_handle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1064. /* KMS */
  1065. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1066. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1067. DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1068. DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1069. DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1070. DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1071. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1072. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1073. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1074. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW)
  1075. };
  1076. const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);
  1077. /*
  1078. * Debugfs info
  1079. */
  1080. #if defined(CONFIG_DEBUG_FS)
  1081. static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data)
  1082. {
  1083. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1084. struct drm_device *dev = node->minor->dev;
  1085. struct amdgpu_device *adev = dev->dev_private;
  1086. struct drm_amdgpu_info_firmware fw_info;
  1087. struct drm_amdgpu_query_fw query_fw;
  1088. struct atom_context *ctx = adev->mode_info.atom_context;
  1089. int ret, i;
  1090. /* VCE */
  1091. query_fw.fw_type = AMDGPU_INFO_FW_VCE;
  1092. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1093. if (ret)
  1094. return ret;
  1095. seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
  1096. fw_info.feature, fw_info.ver);
  1097. /* UVD */
  1098. query_fw.fw_type = AMDGPU_INFO_FW_UVD;
  1099. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1100. if (ret)
  1101. return ret;
  1102. seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
  1103. fw_info.feature, fw_info.ver);
  1104. /* GMC */
  1105. query_fw.fw_type = AMDGPU_INFO_FW_GMC;
  1106. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1107. if (ret)
  1108. return ret;
  1109. seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
  1110. fw_info.feature, fw_info.ver);
  1111. /* ME */
  1112. query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
  1113. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1114. if (ret)
  1115. return ret;
  1116. seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
  1117. fw_info.feature, fw_info.ver);
  1118. /* PFP */
  1119. query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
  1120. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1121. if (ret)
  1122. return ret;
  1123. seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
  1124. fw_info.feature, fw_info.ver);
  1125. /* CE */
  1126. query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
  1127. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1128. if (ret)
  1129. return ret;
  1130. seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
  1131. fw_info.feature, fw_info.ver);
  1132. /* RLC */
  1133. query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
  1134. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1135. if (ret)
  1136. return ret;
  1137. seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
  1138. fw_info.feature, fw_info.ver);
  1139. /* RLC SAVE RESTORE LIST CNTL */
  1140. query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL;
  1141. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1142. if (ret)
  1143. return ret;
  1144. seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n",
  1145. fw_info.feature, fw_info.ver);
  1146. /* RLC SAVE RESTORE LIST GPM MEM */
  1147. query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM;
  1148. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1149. if (ret)
  1150. return ret;
  1151. seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n",
  1152. fw_info.feature, fw_info.ver);
  1153. /* RLC SAVE RESTORE LIST SRM MEM */
  1154. query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM;
  1155. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1156. if (ret)
  1157. return ret;
  1158. seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n",
  1159. fw_info.feature, fw_info.ver);
  1160. /* MEC */
  1161. query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
  1162. query_fw.index = 0;
  1163. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1164. if (ret)
  1165. return ret;
  1166. seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
  1167. fw_info.feature, fw_info.ver);
  1168. /* MEC2 */
  1169. if (adev->asic_type == CHIP_KAVERI ||
  1170. (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) {
  1171. query_fw.index = 1;
  1172. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1173. if (ret)
  1174. return ret;
  1175. seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
  1176. fw_info.feature, fw_info.ver);
  1177. }
  1178. /* PSP SOS */
  1179. query_fw.fw_type = AMDGPU_INFO_FW_SOS;
  1180. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1181. if (ret)
  1182. return ret;
  1183. seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n",
  1184. fw_info.feature, fw_info.ver);
  1185. /* PSP ASD */
  1186. query_fw.fw_type = AMDGPU_INFO_FW_ASD;
  1187. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1188. if (ret)
  1189. return ret;
  1190. seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n",
  1191. fw_info.feature, fw_info.ver);
  1192. /* SMC */
  1193. query_fw.fw_type = AMDGPU_INFO_FW_SMC;
  1194. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1195. if (ret)
  1196. return ret;
  1197. seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n",
  1198. fw_info.feature, fw_info.ver);
  1199. /* SDMA */
  1200. query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
  1201. for (i = 0; i < adev->sdma.num_instances; i++) {
  1202. query_fw.index = i;
  1203. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1204. if (ret)
  1205. return ret;
  1206. seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
  1207. i, fw_info.feature, fw_info.ver);
  1208. }
  1209. /* VCN */
  1210. query_fw.fw_type = AMDGPU_INFO_FW_VCN;
  1211. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1212. if (ret)
  1213. return ret;
  1214. seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n",
  1215. fw_info.feature, fw_info.ver);
  1216. /* DMCU */
  1217. query_fw.fw_type = AMDGPU_INFO_FW_DMCU;
  1218. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  1219. if (ret)
  1220. return ret;
  1221. seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n",
  1222. fw_info.feature, fw_info.ver);
  1223. seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version);
  1224. return 0;
  1225. }
  1226. static const struct drm_info_list amdgpu_firmware_info_list[] = {
  1227. {"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL},
  1228. };
  1229. #endif
  1230. int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
  1231. {
  1232. #if defined(CONFIG_DEBUG_FS)
  1233. return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list,
  1234. ARRAY_SIZE(amdgpu_firmware_info_list));
  1235. #else
  1236. return 0;
  1237. #endif
  1238. }