amdgpu_kms.c 37 KB

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