amdgpu_kms.c 40 KB

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