amdgpu_kms.c 39 KB

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