amdgpu_kms.c 39 KB

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