amdgpu_kms.c 39 KB

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