amdgpu_kms.c 38 KB

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