amdgpu_kms.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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_uvd.h"
  32. #include "amdgpu_vce.h"
  33. #include <linux/vga_switcheroo.h>
  34. #include <linux/slab.h>
  35. #include <linux/pm_runtime.h>
  36. #include "amdgpu_amdkfd.h"
  37. #if defined(CONFIG_VGA_SWITCHEROO)
  38. bool amdgpu_has_atpx(void);
  39. #else
  40. static inline bool amdgpu_has_atpx(void) { return false; }
  41. #endif
  42. /**
  43. * amdgpu_driver_unload_kms - Main unload function for KMS.
  44. *
  45. * @dev: drm dev pointer
  46. *
  47. * This is the main unload function for KMS (all asics).
  48. * Returns 0 on success.
  49. */
  50. int amdgpu_driver_unload_kms(struct drm_device *dev)
  51. {
  52. struct amdgpu_device *adev = dev->dev_private;
  53. if (adev == NULL)
  54. return 0;
  55. if (adev->rmmio == NULL)
  56. goto done_free;
  57. pm_runtime_get_sync(dev->dev);
  58. amdgpu_amdkfd_device_fini(adev);
  59. amdgpu_acpi_fini(adev);
  60. amdgpu_device_fini(adev);
  61. done_free:
  62. kfree(adev);
  63. dev->dev_private = NULL;
  64. return 0;
  65. }
  66. /**
  67. * amdgpu_driver_load_kms - Main load function for KMS.
  68. *
  69. * @dev: drm dev pointer
  70. * @flags: device flags
  71. *
  72. * This is the main load function for KMS (all asics).
  73. * Returns 0 on success, error on failure.
  74. */
  75. int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
  76. {
  77. struct amdgpu_device *adev;
  78. int r, acpi_status;
  79. adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL);
  80. if (adev == NULL) {
  81. return -ENOMEM;
  82. }
  83. dev->dev_private = (void *)adev;
  84. if ((amdgpu_runtime_pm != 0) &&
  85. amdgpu_has_atpx() &&
  86. ((flags & AMD_IS_APU) == 0))
  87. flags |= AMD_IS_PX;
  88. /* amdgpu_device_init should report only fatal error
  89. * like memory allocation failure or iomapping failure,
  90. * or memory manager initialization failure, it must
  91. * properly initialize the GPU MC controller and permit
  92. * VRAM allocation
  93. */
  94. r = amdgpu_device_init(adev, dev, dev->pdev, flags);
  95. if (r) {
  96. dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
  97. goto out;
  98. }
  99. /* Call ACPI methods: require modeset init
  100. * but failure is not fatal
  101. */
  102. if (!r) {
  103. acpi_status = amdgpu_acpi_init(adev);
  104. if (acpi_status)
  105. dev_dbg(&dev->pdev->dev,
  106. "Error during ACPI methods call\n");
  107. }
  108. amdgpu_amdkfd_load_interface(adev);
  109. amdgpu_amdkfd_device_probe(adev);
  110. amdgpu_amdkfd_device_init(adev);
  111. if (amdgpu_device_is_px(dev)) {
  112. pm_runtime_use_autosuspend(dev->dev);
  113. pm_runtime_set_autosuspend_delay(dev->dev, 5000);
  114. pm_runtime_set_active(dev->dev);
  115. pm_runtime_allow(dev->dev);
  116. pm_runtime_mark_last_busy(dev->dev);
  117. pm_runtime_put_autosuspend(dev->dev);
  118. }
  119. out:
  120. if (r)
  121. amdgpu_driver_unload_kms(dev);
  122. return r;
  123. }
  124. /*
  125. * Userspace get information ioctl
  126. */
  127. /**
  128. * amdgpu_info_ioctl - answer a device specific request.
  129. *
  130. * @adev: amdgpu device pointer
  131. * @data: request object
  132. * @filp: drm filp
  133. *
  134. * This function is used to pass device specific parameters to the userspace
  135. * drivers. Examples include: pci device id, pipeline parms, tiling params,
  136. * etc. (all asics).
  137. * Returns 0 on success, -EINVAL on failure.
  138. */
  139. static int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  140. {
  141. struct amdgpu_device *adev = dev->dev_private;
  142. struct drm_amdgpu_info *info = data;
  143. struct amdgpu_mode_info *minfo = &adev->mode_info;
  144. void __user *out = (void __user *)(long)info->return_pointer;
  145. uint32_t size = info->return_size;
  146. struct drm_crtc *crtc;
  147. uint32_t ui32 = 0;
  148. uint64_t ui64 = 0;
  149. int i, found;
  150. if (!info->return_size || !info->return_pointer)
  151. return -EINVAL;
  152. switch (info->query) {
  153. case AMDGPU_INFO_ACCEL_WORKING:
  154. ui32 = adev->accel_working;
  155. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  156. case AMDGPU_INFO_CRTC_FROM_ID:
  157. for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
  158. crtc = (struct drm_crtc *)minfo->crtcs[i];
  159. if (crtc && crtc->base.id == info->mode_crtc.id) {
  160. struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
  161. ui32 = amdgpu_crtc->crtc_id;
  162. found = 1;
  163. break;
  164. }
  165. }
  166. if (!found) {
  167. DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
  168. return -EINVAL;
  169. }
  170. return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
  171. case AMDGPU_INFO_HW_IP_INFO: {
  172. struct drm_amdgpu_info_hw_ip ip = {};
  173. enum amd_ip_block_type type;
  174. uint32_t ring_mask = 0;
  175. uint32_t ib_start_alignment = 0;
  176. uint32_t ib_size_alignment = 0;
  177. if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  178. return -EINVAL;
  179. switch (info->query_hw_ip.type) {
  180. case AMDGPU_HW_IP_GFX:
  181. type = AMD_IP_BLOCK_TYPE_GFX;
  182. for (i = 0; i < adev->gfx.num_gfx_rings; i++)
  183. ring_mask |= ((adev->gfx.gfx_ring[i].ready ? 1 : 0) << i);
  184. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  185. ib_size_alignment = 8;
  186. break;
  187. case AMDGPU_HW_IP_COMPUTE:
  188. type = AMD_IP_BLOCK_TYPE_GFX;
  189. for (i = 0; i < adev->gfx.num_compute_rings; i++)
  190. ring_mask |= ((adev->gfx.compute_ring[i].ready ? 1 : 0) << i);
  191. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  192. ib_size_alignment = 8;
  193. break;
  194. case AMDGPU_HW_IP_DMA:
  195. type = AMD_IP_BLOCK_TYPE_SDMA;
  196. for (i = 0; i < adev->sdma.num_instances; i++)
  197. ring_mask |= ((adev->sdma.instance[i].ring.ready ? 1 : 0) << i);
  198. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  199. ib_size_alignment = 1;
  200. break;
  201. case AMDGPU_HW_IP_UVD:
  202. type = AMD_IP_BLOCK_TYPE_UVD;
  203. ring_mask = adev->uvd.ring.ready ? 1 : 0;
  204. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  205. ib_size_alignment = 16;
  206. break;
  207. case AMDGPU_HW_IP_VCE:
  208. type = AMD_IP_BLOCK_TYPE_VCE;
  209. for (i = 0; i < AMDGPU_MAX_VCE_RINGS; i++)
  210. ring_mask |= ((adev->vce.ring[i].ready ? 1 : 0) << i);
  211. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  212. ib_size_alignment = 8;
  213. break;
  214. default:
  215. return -EINVAL;
  216. }
  217. for (i = 0; i < adev->num_ip_blocks; i++) {
  218. if (adev->ip_blocks[i].type == type &&
  219. adev->ip_block_status[i].valid) {
  220. ip.hw_ip_version_major = adev->ip_blocks[i].major;
  221. ip.hw_ip_version_minor = adev->ip_blocks[i].minor;
  222. ip.capabilities_flags = 0;
  223. ip.available_rings = ring_mask;
  224. ip.ib_start_alignment = ib_start_alignment;
  225. ip.ib_size_alignment = ib_size_alignment;
  226. break;
  227. }
  228. }
  229. return copy_to_user(out, &ip,
  230. min((size_t)size, sizeof(ip))) ? -EFAULT : 0;
  231. }
  232. case AMDGPU_INFO_HW_IP_COUNT: {
  233. enum amd_ip_block_type type;
  234. uint32_t count = 0;
  235. switch (info->query_hw_ip.type) {
  236. case AMDGPU_HW_IP_GFX:
  237. type = AMD_IP_BLOCK_TYPE_GFX;
  238. break;
  239. case AMDGPU_HW_IP_COMPUTE:
  240. type = AMD_IP_BLOCK_TYPE_GFX;
  241. break;
  242. case AMDGPU_HW_IP_DMA:
  243. type = AMD_IP_BLOCK_TYPE_SDMA;
  244. break;
  245. case AMDGPU_HW_IP_UVD:
  246. type = AMD_IP_BLOCK_TYPE_UVD;
  247. break;
  248. case AMDGPU_HW_IP_VCE:
  249. type = AMD_IP_BLOCK_TYPE_VCE;
  250. break;
  251. default:
  252. return -EINVAL;
  253. }
  254. for (i = 0; i < adev->num_ip_blocks; i++)
  255. if (adev->ip_blocks[i].type == type &&
  256. adev->ip_block_status[i].valid &&
  257. count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  258. count++;
  259. return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
  260. }
  261. case AMDGPU_INFO_TIMESTAMP:
  262. ui64 = amdgpu_asic_get_gpu_clock_counter(adev);
  263. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  264. case AMDGPU_INFO_FW_VERSION: {
  265. struct drm_amdgpu_info_firmware fw_info;
  266. /* We only support one instance of each IP block right now. */
  267. if (info->query_fw.ip_instance != 0)
  268. return -EINVAL;
  269. switch (info->query_fw.fw_type) {
  270. case AMDGPU_INFO_FW_VCE:
  271. fw_info.ver = adev->vce.fw_version;
  272. fw_info.feature = adev->vce.fb_version;
  273. break;
  274. case AMDGPU_INFO_FW_UVD:
  275. fw_info.ver = adev->uvd.fw_version;
  276. fw_info.feature = 0;
  277. break;
  278. case AMDGPU_INFO_FW_GMC:
  279. fw_info.ver = adev->mc.fw_version;
  280. fw_info.feature = 0;
  281. break;
  282. case AMDGPU_INFO_FW_GFX_ME:
  283. fw_info.ver = adev->gfx.me_fw_version;
  284. fw_info.feature = adev->gfx.me_feature_version;
  285. break;
  286. case AMDGPU_INFO_FW_GFX_PFP:
  287. fw_info.ver = adev->gfx.pfp_fw_version;
  288. fw_info.feature = adev->gfx.pfp_feature_version;
  289. break;
  290. case AMDGPU_INFO_FW_GFX_CE:
  291. fw_info.ver = adev->gfx.ce_fw_version;
  292. fw_info.feature = adev->gfx.ce_feature_version;
  293. break;
  294. case AMDGPU_INFO_FW_GFX_RLC:
  295. fw_info.ver = adev->gfx.rlc_fw_version;
  296. fw_info.feature = adev->gfx.rlc_feature_version;
  297. break;
  298. case AMDGPU_INFO_FW_GFX_MEC:
  299. if (info->query_fw.index == 0) {
  300. fw_info.ver = adev->gfx.mec_fw_version;
  301. fw_info.feature = adev->gfx.mec_feature_version;
  302. } else if (info->query_fw.index == 1) {
  303. fw_info.ver = adev->gfx.mec2_fw_version;
  304. fw_info.feature = adev->gfx.mec2_feature_version;
  305. } else
  306. return -EINVAL;
  307. break;
  308. case AMDGPU_INFO_FW_SMC:
  309. fw_info.ver = adev->pm.fw_version;
  310. fw_info.feature = 0;
  311. break;
  312. case AMDGPU_INFO_FW_SDMA:
  313. if (info->query_fw.index >= adev->sdma.num_instances)
  314. return -EINVAL;
  315. fw_info.ver = adev->sdma.instance[info->query_fw.index].fw_version;
  316. fw_info.feature = adev->sdma.instance[info->query_fw.index].feature_version;
  317. break;
  318. default:
  319. return -EINVAL;
  320. }
  321. return copy_to_user(out, &fw_info,
  322. min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
  323. }
  324. case AMDGPU_INFO_NUM_BYTES_MOVED:
  325. ui64 = atomic64_read(&adev->num_bytes_moved);
  326. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  327. case AMDGPU_INFO_VRAM_USAGE:
  328. ui64 = atomic64_read(&adev->vram_usage);
  329. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  330. case AMDGPU_INFO_VIS_VRAM_USAGE:
  331. ui64 = atomic64_read(&adev->vram_vis_usage);
  332. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  333. case AMDGPU_INFO_GTT_USAGE:
  334. ui64 = atomic64_read(&adev->gtt_usage);
  335. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  336. case AMDGPU_INFO_GDS_CONFIG: {
  337. struct drm_amdgpu_info_gds gds_info;
  338. memset(&gds_info, 0, sizeof(gds_info));
  339. gds_info.gds_gfx_partition_size = adev->gds.mem.gfx_partition_size >> AMDGPU_GDS_SHIFT;
  340. gds_info.compute_partition_size = adev->gds.mem.cs_partition_size >> AMDGPU_GDS_SHIFT;
  341. gds_info.gds_total_size = adev->gds.mem.total_size >> AMDGPU_GDS_SHIFT;
  342. gds_info.gws_per_gfx_partition = adev->gds.gws.gfx_partition_size >> AMDGPU_GWS_SHIFT;
  343. gds_info.gws_per_compute_partition = adev->gds.gws.cs_partition_size >> AMDGPU_GWS_SHIFT;
  344. gds_info.oa_per_gfx_partition = adev->gds.oa.gfx_partition_size >> AMDGPU_OA_SHIFT;
  345. gds_info.oa_per_compute_partition = adev->gds.oa.cs_partition_size >> AMDGPU_OA_SHIFT;
  346. return copy_to_user(out, &gds_info,
  347. min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
  348. }
  349. case AMDGPU_INFO_VRAM_GTT: {
  350. struct drm_amdgpu_info_vram_gtt vram_gtt;
  351. vram_gtt.vram_size = adev->mc.real_vram_size;
  352. vram_gtt.vram_cpu_accessible_size = adev->mc.visible_vram_size;
  353. vram_gtt.vram_cpu_accessible_size -= adev->vram_pin_size;
  354. vram_gtt.gtt_size = adev->mc.gtt_size;
  355. vram_gtt.gtt_size -= adev->gart_pin_size;
  356. return copy_to_user(out, &vram_gtt,
  357. min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
  358. }
  359. case AMDGPU_INFO_READ_MMR_REG: {
  360. unsigned n, alloc_size;
  361. uint32_t *regs;
  362. unsigned se_num = (info->read_mmr_reg.instance >>
  363. AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
  364. AMDGPU_INFO_MMR_SE_INDEX_MASK;
  365. unsigned sh_num = (info->read_mmr_reg.instance >>
  366. AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
  367. AMDGPU_INFO_MMR_SH_INDEX_MASK;
  368. /* set full masks if the userspace set all bits
  369. * in the bitfields */
  370. if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
  371. se_num = 0xffffffff;
  372. if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
  373. sh_num = 0xffffffff;
  374. regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
  375. if (!regs)
  376. return -ENOMEM;
  377. alloc_size = info->read_mmr_reg.count * sizeof(*regs);
  378. for (i = 0; i < info->read_mmr_reg.count; i++)
  379. if (amdgpu_asic_read_register(adev, se_num, sh_num,
  380. info->read_mmr_reg.dword_offset + i,
  381. &regs[i])) {
  382. DRM_DEBUG_KMS("unallowed offset %#x\n",
  383. info->read_mmr_reg.dword_offset + i);
  384. kfree(regs);
  385. return -EFAULT;
  386. }
  387. n = copy_to_user(out, regs, min(size, alloc_size));
  388. kfree(regs);
  389. return n ? -EFAULT : 0;
  390. }
  391. case AMDGPU_INFO_DEV_INFO: {
  392. struct drm_amdgpu_info_device dev_info = {};
  393. struct amdgpu_cu_info cu_info;
  394. dev_info.device_id = dev->pdev->device;
  395. dev_info.chip_rev = adev->rev_id;
  396. dev_info.external_rev = adev->external_rev_id;
  397. dev_info.pci_rev = dev->pdev->revision;
  398. dev_info.family = adev->family;
  399. dev_info.num_shader_engines = adev->gfx.config.max_shader_engines;
  400. dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
  401. /* return all clocks in KHz */
  402. dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
  403. if (adev->pm.dpm_enabled) {
  404. dev_info.max_engine_clock =
  405. adev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk * 10;
  406. dev_info.max_memory_clock =
  407. adev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk * 10;
  408. } else {
  409. dev_info.max_engine_clock = adev->pm.default_sclk * 10;
  410. dev_info.max_memory_clock = adev->pm.default_mclk * 10;
  411. }
  412. dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
  413. dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se *
  414. adev->gfx.config.max_shader_engines;
  415. dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
  416. dev_info._pad = 0;
  417. dev_info.ids_flags = 0;
  418. if (adev->flags & AMD_IS_APU)
  419. dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
  420. dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
  421. dev_info.virtual_address_max = (uint64_t)adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
  422. dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
  423. dev_info.pte_fragment_size = (1 << AMDGPU_LOG2_PAGES_PER_FRAG) *
  424. AMDGPU_GPU_PAGE_SIZE;
  425. dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE;
  426. amdgpu_asic_get_cu_info(adev, &cu_info);
  427. dev_info.cu_active_number = cu_info.number;
  428. dev_info.cu_ao_mask = cu_info.ao_cu_mask;
  429. dev_info.ce_ram_size = adev->gfx.ce_ram_size;
  430. memcpy(&dev_info.cu_bitmap[0], &cu_info.bitmap[0], sizeof(cu_info.bitmap));
  431. dev_info.vram_type = adev->mc.vram_type;
  432. dev_info.vram_bit_width = adev->mc.vram_width;
  433. dev_info.vce_harvest_config = adev->vce.harvest_config;
  434. return copy_to_user(out, &dev_info,
  435. min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0;
  436. }
  437. default:
  438. DRM_DEBUG_KMS("Invalid request %d\n", info->query);
  439. return -EINVAL;
  440. }
  441. return 0;
  442. }
  443. /*
  444. * Outdated mess for old drm with Xorg being in charge (void function now).
  445. */
  446. /**
  447. * amdgpu_driver_lastclose_kms - drm callback for last close
  448. *
  449. * @dev: drm dev pointer
  450. *
  451. * Switch vga_switcheroo state after last close (all asics).
  452. */
  453. void amdgpu_driver_lastclose_kms(struct drm_device *dev)
  454. {
  455. struct amdgpu_device *adev = dev->dev_private;
  456. amdgpu_fbdev_restore_mode(adev);
  457. vga_switcheroo_process_delayed_switch();
  458. }
  459. /**
  460. * amdgpu_driver_open_kms - drm callback for open
  461. *
  462. * @dev: drm dev pointer
  463. * @file_priv: drm file
  464. *
  465. * On device open, init vm on cayman+ (all asics).
  466. * Returns 0 on success, error on failure.
  467. */
  468. int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
  469. {
  470. struct amdgpu_device *adev = dev->dev_private;
  471. struct amdgpu_fpriv *fpriv;
  472. int r;
  473. file_priv->driver_priv = NULL;
  474. r = pm_runtime_get_sync(dev->dev);
  475. if (r < 0)
  476. return r;
  477. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  478. if (unlikely(!fpriv))
  479. return -ENOMEM;
  480. r = amdgpu_vm_init(adev, &fpriv->vm);
  481. if (r)
  482. goto error_free;
  483. mutex_init(&fpriv->bo_list_lock);
  484. idr_init(&fpriv->bo_list_handles);
  485. amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
  486. file_priv->driver_priv = fpriv;
  487. pm_runtime_mark_last_busy(dev->dev);
  488. pm_runtime_put_autosuspend(dev->dev);
  489. return 0;
  490. error_free:
  491. kfree(fpriv);
  492. return r;
  493. }
  494. /**
  495. * amdgpu_driver_postclose_kms - drm callback for post close
  496. *
  497. * @dev: drm dev pointer
  498. * @file_priv: drm file
  499. *
  500. * On device post close, tear down vm on cayman+ (all asics).
  501. */
  502. void amdgpu_driver_postclose_kms(struct drm_device *dev,
  503. struct drm_file *file_priv)
  504. {
  505. struct amdgpu_device *adev = dev->dev_private;
  506. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  507. struct amdgpu_bo_list *list;
  508. int handle;
  509. if (!fpriv)
  510. return;
  511. amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
  512. amdgpu_vm_fini(adev, &fpriv->vm);
  513. idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
  514. amdgpu_bo_list_free(list);
  515. idr_destroy(&fpriv->bo_list_handles);
  516. mutex_destroy(&fpriv->bo_list_lock);
  517. kfree(fpriv);
  518. file_priv->driver_priv = NULL;
  519. }
  520. /**
  521. * amdgpu_driver_preclose_kms - drm callback for pre close
  522. *
  523. * @dev: drm dev pointer
  524. * @file_priv: drm file
  525. *
  526. * On device pre close, tear down hyperz and cmask filps on r1xx-r5xx
  527. * (all asics).
  528. */
  529. void amdgpu_driver_preclose_kms(struct drm_device *dev,
  530. struct drm_file *file_priv)
  531. {
  532. struct amdgpu_device *adev = dev->dev_private;
  533. amdgpu_uvd_free_handles(adev, file_priv);
  534. amdgpu_vce_free_handles(adev, file_priv);
  535. }
  536. /*
  537. * VBlank related functions.
  538. */
  539. /**
  540. * amdgpu_get_vblank_counter_kms - get frame count
  541. *
  542. * @dev: drm dev pointer
  543. * @pipe: crtc to get the frame count from
  544. *
  545. * Gets the frame count on the requested crtc (all asics).
  546. * Returns frame count on success, -EINVAL on failure.
  547. */
  548. u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
  549. {
  550. struct amdgpu_device *adev = dev->dev_private;
  551. int vpos, hpos, stat;
  552. u32 count;
  553. if (pipe >= adev->mode_info.num_crtc) {
  554. DRM_ERROR("Invalid crtc %u\n", pipe);
  555. return -EINVAL;
  556. }
  557. /* The hw increments its frame counter at start of vsync, not at start
  558. * of vblank, as is required by DRM core vblank counter handling.
  559. * Cook the hw count here to make it appear to the caller as if it
  560. * incremented at start of vblank. We measure distance to start of
  561. * vblank in vpos. vpos therefore will be >= 0 between start of vblank
  562. * and start of vsync, so vpos >= 0 means to bump the hw frame counter
  563. * result by 1 to give the proper appearance to caller.
  564. */
  565. if (adev->mode_info.crtcs[pipe]) {
  566. /* Repeat readout if needed to provide stable result if
  567. * we cross start of vsync during the queries.
  568. */
  569. do {
  570. count = amdgpu_display_vblank_get_counter(adev, pipe);
  571. /* Ask amdgpu_get_crtc_scanoutpos to return vpos as
  572. * distance to start of vblank, instead of regular
  573. * vertical scanout pos.
  574. */
  575. stat = amdgpu_get_crtc_scanoutpos(
  576. dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
  577. &vpos, &hpos, NULL, NULL,
  578. &adev->mode_info.crtcs[pipe]->base.hwmode);
  579. } while (count != amdgpu_display_vblank_get_counter(adev, pipe));
  580. if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
  581. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
  582. DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
  583. } else {
  584. DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
  585. pipe, vpos);
  586. /* Bump counter if we are at >= leading edge of vblank,
  587. * but before vsync where vpos would turn negative and
  588. * the hw counter really increments.
  589. */
  590. if (vpos >= 0)
  591. count++;
  592. }
  593. } else {
  594. /* Fallback to use value as is. */
  595. count = amdgpu_display_vblank_get_counter(adev, pipe);
  596. DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
  597. }
  598. return count;
  599. }
  600. /**
  601. * amdgpu_enable_vblank_kms - enable vblank interrupt
  602. *
  603. * @dev: drm dev pointer
  604. * @pipe: crtc to enable vblank interrupt for
  605. *
  606. * Enable the interrupt on the requested crtc (all asics).
  607. * Returns 0 on success, -EINVAL on failure.
  608. */
  609. int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  610. {
  611. struct amdgpu_device *adev = dev->dev_private;
  612. int idx = amdgpu_crtc_idx_to_irq_type(adev, pipe);
  613. return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
  614. }
  615. /**
  616. * amdgpu_disable_vblank_kms - disable vblank interrupt
  617. *
  618. * @dev: drm dev pointer
  619. * @pipe: crtc to disable vblank interrupt for
  620. *
  621. * Disable the interrupt on the requested crtc (all asics).
  622. */
  623. void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  624. {
  625. struct amdgpu_device *adev = dev->dev_private;
  626. int idx = amdgpu_crtc_idx_to_irq_type(adev, pipe);
  627. amdgpu_irq_put(adev, &adev->crtc_irq, idx);
  628. }
  629. /**
  630. * amdgpu_get_vblank_timestamp_kms - get vblank timestamp
  631. *
  632. * @dev: drm dev pointer
  633. * @crtc: crtc to get the timestamp for
  634. * @max_error: max error
  635. * @vblank_time: time value
  636. * @flags: flags passed to the driver
  637. *
  638. * Gets the timestamp on the requested crtc based on the
  639. * scanout position. (all asics).
  640. * Returns postive status flags on success, negative error on failure.
  641. */
  642. int amdgpu_get_vblank_timestamp_kms(struct drm_device *dev, unsigned int pipe,
  643. int *max_error,
  644. struct timeval *vblank_time,
  645. unsigned flags)
  646. {
  647. struct drm_crtc *crtc;
  648. struct amdgpu_device *adev = dev->dev_private;
  649. if (pipe >= dev->num_crtcs) {
  650. DRM_ERROR("Invalid crtc %u\n", pipe);
  651. return -EINVAL;
  652. }
  653. /* Get associated drm_crtc: */
  654. crtc = &adev->mode_info.crtcs[pipe]->base;
  655. /* Helper routine in DRM core does all the work: */
  656. return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
  657. vblank_time, flags,
  658. &crtc->hwmode);
  659. }
  660. const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  661. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  662. DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  663. DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  664. /* KMS */
  665. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  666. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  667. DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  668. DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  669. DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  670. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  671. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  672. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  673. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  674. };
  675. int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);