amdgpu_kms.c 20 KB

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