amdgpu_kms.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 amd_ip_block_type type;
  169. uint32_t ring_mask = 0;
  170. uint32_t ib_start_alignment = 0;
  171. uint32_t ib_size_alignment = 0;
  172. if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  173. return -EINVAL;
  174. switch (info->query_hw_ip.type) {
  175. case AMDGPU_HW_IP_GFX:
  176. type = AMD_IP_BLOCK_TYPE_GFX;
  177. for (i = 0; i < adev->gfx.num_gfx_rings; i++)
  178. ring_mask |= ((adev->gfx.gfx_ring[i].ready ? 1 : 0) << i);
  179. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  180. ib_size_alignment = 8;
  181. break;
  182. case AMDGPU_HW_IP_COMPUTE:
  183. type = AMD_IP_BLOCK_TYPE_GFX;
  184. for (i = 0; i < adev->gfx.num_compute_rings; i++)
  185. ring_mask |= ((adev->gfx.compute_ring[i].ready ? 1 : 0) << i);
  186. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  187. ib_size_alignment = 8;
  188. break;
  189. case AMDGPU_HW_IP_DMA:
  190. type = AMD_IP_BLOCK_TYPE_SDMA;
  191. ring_mask = adev->sdma[0].ring.ready ? 1 : 0;
  192. ring_mask |= ((adev->sdma[1].ring.ready ? 1 : 0) << 1);
  193. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  194. ib_size_alignment = 1;
  195. break;
  196. case AMDGPU_HW_IP_UVD:
  197. type = AMD_IP_BLOCK_TYPE_UVD;
  198. ring_mask = adev->uvd.ring.ready ? 1 : 0;
  199. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  200. ib_size_alignment = 8;
  201. break;
  202. case AMDGPU_HW_IP_VCE:
  203. type = AMD_IP_BLOCK_TYPE_VCE;
  204. for (i = 0; i < AMDGPU_MAX_VCE_RINGS; i++)
  205. ring_mask |= ((adev->vce.ring[i].ready ? 1 : 0) << i);
  206. ib_start_alignment = AMDGPU_GPU_PAGE_SIZE;
  207. ib_size_alignment = 8;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. for (i = 0; i < adev->num_ip_blocks; i++) {
  213. if (adev->ip_blocks[i].type == type &&
  214. adev->ip_block_enabled[i]) {
  215. ip.hw_ip_version_major = adev->ip_blocks[i].major;
  216. ip.hw_ip_version_minor = adev->ip_blocks[i].minor;
  217. ip.capabilities_flags = 0;
  218. ip.available_rings = ring_mask;
  219. ip.ib_start_alignment = ib_start_alignment;
  220. ip.ib_size_alignment = ib_size_alignment;
  221. break;
  222. }
  223. }
  224. return copy_to_user(out, &ip,
  225. min((size_t)size, sizeof(ip))) ? -EFAULT : 0;
  226. }
  227. case AMDGPU_INFO_HW_IP_COUNT: {
  228. enum amd_ip_block_type type;
  229. uint32_t count = 0;
  230. switch (info->query_hw_ip.type) {
  231. case AMDGPU_HW_IP_GFX:
  232. type = AMD_IP_BLOCK_TYPE_GFX;
  233. break;
  234. case AMDGPU_HW_IP_COMPUTE:
  235. type = AMD_IP_BLOCK_TYPE_GFX;
  236. break;
  237. case AMDGPU_HW_IP_DMA:
  238. type = AMD_IP_BLOCK_TYPE_SDMA;
  239. break;
  240. case AMDGPU_HW_IP_UVD:
  241. type = AMD_IP_BLOCK_TYPE_UVD;
  242. break;
  243. case AMDGPU_HW_IP_VCE:
  244. type = AMD_IP_BLOCK_TYPE_VCE;
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. for (i = 0; i < adev->num_ip_blocks; i++)
  250. if (adev->ip_blocks[i].type == type &&
  251. adev->ip_block_enabled[i] &&
  252. count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
  253. count++;
  254. return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
  255. }
  256. case AMDGPU_INFO_TIMESTAMP:
  257. ui64 = amdgpu_asic_get_gpu_clock_counter(adev);
  258. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  259. case AMDGPU_INFO_FW_VERSION: {
  260. struct drm_amdgpu_info_firmware fw_info;
  261. /* We only support one instance of each IP block right now. */
  262. if (info->query_fw.ip_instance != 0)
  263. return -EINVAL;
  264. switch (info->query_fw.fw_type) {
  265. case AMDGPU_INFO_FW_VCE:
  266. fw_info.ver = adev->vce.fw_version;
  267. fw_info.feature = adev->vce.fb_version;
  268. break;
  269. case AMDGPU_INFO_FW_UVD:
  270. fw_info.ver = 0;
  271. fw_info.feature = 0;
  272. break;
  273. case AMDGPU_INFO_FW_GMC:
  274. fw_info.ver = adev->mc.fw_version;
  275. fw_info.feature = 0;
  276. break;
  277. case AMDGPU_INFO_FW_GFX_ME:
  278. fw_info.ver = adev->gfx.me_fw_version;
  279. fw_info.feature = adev->gfx.me_feature_version;
  280. break;
  281. case AMDGPU_INFO_FW_GFX_PFP:
  282. fw_info.ver = adev->gfx.pfp_fw_version;
  283. fw_info.feature = adev->gfx.pfp_feature_version;
  284. break;
  285. case AMDGPU_INFO_FW_GFX_CE:
  286. fw_info.ver = adev->gfx.ce_fw_version;
  287. fw_info.feature = adev->gfx.ce_feature_version;
  288. break;
  289. case AMDGPU_INFO_FW_GFX_RLC:
  290. fw_info.ver = adev->gfx.rlc_fw_version;
  291. fw_info.feature = 0;
  292. break;
  293. case AMDGPU_INFO_FW_GFX_MEC:
  294. if (info->query_fw.index == 0)
  295. fw_info.ver = adev->gfx.mec_fw_version;
  296. else if (info->query_fw.index == 1)
  297. fw_info.ver = adev->gfx.mec2_fw_version;
  298. else
  299. return -EINVAL;
  300. fw_info.feature = 0;
  301. break;
  302. case AMDGPU_INFO_FW_SMC:
  303. fw_info.ver = adev->pm.fw_version;
  304. fw_info.feature = 0;
  305. break;
  306. case AMDGPU_INFO_FW_SDMA:
  307. if (info->query_fw.index >= 2)
  308. return -EINVAL;
  309. fw_info.ver = adev->sdma[info->query_fw.index].fw_version;
  310. fw_info.feature = 0;
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. return copy_to_user(out, &fw_info,
  316. min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
  317. }
  318. case AMDGPU_INFO_NUM_BYTES_MOVED:
  319. ui64 = atomic64_read(&adev->num_bytes_moved);
  320. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  321. case AMDGPU_INFO_VRAM_USAGE:
  322. ui64 = atomic64_read(&adev->vram_usage);
  323. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  324. case AMDGPU_INFO_VIS_VRAM_USAGE:
  325. ui64 = atomic64_read(&adev->vram_vis_usage);
  326. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  327. case AMDGPU_INFO_GTT_USAGE:
  328. ui64 = atomic64_read(&adev->gtt_usage);
  329. return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
  330. case AMDGPU_INFO_GDS_CONFIG: {
  331. struct drm_amdgpu_info_gds gds_info;
  332. memset(&gds_info, 0, sizeof(gds_info));
  333. gds_info.gds_gfx_partition_size = adev->gds.mem.gfx_partition_size >> AMDGPU_GDS_SHIFT;
  334. gds_info.compute_partition_size = adev->gds.mem.cs_partition_size >> AMDGPU_GDS_SHIFT;
  335. gds_info.gds_total_size = adev->gds.mem.total_size >> AMDGPU_GDS_SHIFT;
  336. gds_info.gws_per_gfx_partition = adev->gds.gws.gfx_partition_size >> AMDGPU_GWS_SHIFT;
  337. gds_info.gws_per_compute_partition = adev->gds.gws.cs_partition_size >> AMDGPU_GWS_SHIFT;
  338. gds_info.oa_per_gfx_partition = adev->gds.oa.gfx_partition_size >> AMDGPU_OA_SHIFT;
  339. gds_info.oa_per_compute_partition = adev->gds.oa.cs_partition_size >> AMDGPU_OA_SHIFT;
  340. return copy_to_user(out, &gds_info,
  341. min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
  342. }
  343. case AMDGPU_INFO_VRAM_GTT: {
  344. struct drm_amdgpu_info_vram_gtt vram_gtt;
  345. vram_gtt.vram_size = adev->mc.real_vram_size;
  346. vram_gtt.vram_cpu_accessible_size = adev->mc.visible_vram_size;
  347. vram_gtt.vram_cpu_accessible_size -= adev->vram_pin_size;
  348. vram_gtt.gtt_size = adev->mc.gtt_size;
  349. vram_gtt.gtt_size -= adev->gart_pin_size;
  350. return copy_to_user(out, &vram_gtt,
  351. min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
  352. }
  353. case AMDGPU_INFO_READ_MMR_REG: {
  354. unsigned n, alloc_size = info->read_mmr_reg.count * 4;
  355. uint32_t *regs;
  356. unsigned se_num = (info->read_mmr_reg.instance >>
  357. AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
  358. AMDGPU_INFO_MMR_SE_INDEX_MASK;
  359. unsigned sh_num = (info->read_mmr_reg.instance >>
  360. AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
  361. AMDGPU_INFO_MMR_SH_INDEX_MASK;
  362. /* set full masks if the userspace set all bits
  363. * in the bitfields */
  364. if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
  365. se_num = 0xffffffff;
  366. if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
  367. sh_num = 0xffffffff;
  368. regs = kmalloc(alloc_size, GFP_KERNEL);
  369. if (!regs)
  370. return -ENOMEM;
  371. for (i = 0; i < info->read_mmr_reg.count; i++)
  372. if (amdgpu_asic_read_register(adev, se_num, sh_num,
  373. info->read_mmr_reg.dword_offset + i,
  374. &regs[i])) {
  375. DRM_DEBUG_KMS("unallowed offset %#x\n",
  376. info->read_mmr_reg.dword_offset + i);
  377. kfree(regs);
  378. return -EFAULT;
  379. }
  380. n = copy_to_user(out, regs, min(size, alloc_size));
  381. kfree(regs);
  382. return n ? -EFAULT : 0;
  383. }
  384. case AMDGPU_INFO_DEV_INFO: {
  385. struct drm_amdgpu_info_device dev_info;
  386. struct amdgpu_cu_info cu_info;
  387. dev_info.device_id = dev->pdev->device;
  388. dev_info.chip_rev = adev->rev_id;
  389. dev_info.external_rev = adev->external_rev_id;
  390. dev_info.pci_rev = dev->pdev->revision;
  391. dev_info.family = adev->family;
  392. dev_info.num_shader_engines = adev->gfx.config.max_shader_engines;
  393. dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
  394. /* return all clocks in KHz */
  395. dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
  396. if (adev->pm.dpm_enabled) {
  397. dev_info.max_engine_clock =
  398. adev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk * 10;
  399. dev_info.max_memory_clock =
  400. adev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk * 10;
  401. } else {
  402. dev_info.max_engine_clock = adev->pm.default_sclk * 10;
  403. dev_info.max_memory_clock = adev->pm.default_mclk * 10;
  404. }
  405. dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
  406. dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se *
  407. adev->gfx.config.max_shader_engines;
  408. dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
  409. dev_info._pad = 0;
  410. dev_info.ids_flags = 0;
  411. if (adev->flags & AMDGPU_IS_APU)
  412. dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
  413. dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
  414. dev_info.virtual_address_max = (uint64_t)adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
  415. dev_info.virtual_address_alignment = max(PAGE_SIZE, 0x10000UL);
  416. dev_info.pte_fragment_size = (1 << AMDGPU_LOG2_PAGES_PER_FRAG) *
  417. AMDGPU_GPU_PAGE_SIZE;
  418. dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE;
  419. amdgpu_asic_get_cu_info(adev, &cu_info);
  420. dev_info.cu_active_number = cu_info.number;
  421. dev_info.cu_ao_mask = cu_info.ao_cu_mask;
  422. dev_info.ce_ram_size = adev->gfx.ce_ram_size;
  423. memcpy(&dev_info.cu_bitmap[0], &cu_info.bitmap[0], sizeof(cu_info.bitmap));
  424. dev_info.vram_type = adev->mc.vram_type;
  425. dev_info.vram_bit_width = adev->mc.vram_width;
  426. return copy_to_user(out, &dev_info,
  427. min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0;
  428. }
  429. default:
  430. DRM_DEBUG_KMS("Invalid request %d\n", info->query);
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. /*
  436. * Outdated mess for old drm with Xorg being in charge (void function now).
  437. */
  438. /**
  439. * amdgpu_driver_firstopen_kms - drm callback for last close
  440. *
  441. * @dev: drm dev pointer
  442. *
  443. * Switch vga switcheroo state after last close (all asics).
  444. */
  445. void amdgpu_driver_lastclose_kms(struct drm_device *dev)
  446. {
  447. vga_switcheroo_process_delayed_switch();
  448. }
  449. /**
  450. * amdgpu_driver_open_kms - drm callback for open
  451. *
  452. * @dev: drm dev pointer
  453. * @file_priv: drm file
  454. *
  455. * On device open, init vm on cayman+ (all asics).
  456. * Returns 0 on success, error on failure.
  457. */
  458. int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
  459. {
  460. struct amdgpu_device *adev = dev->dev_private;
  461. struct amdgpu_fpriv *fpriv;
  462. int r;
  463. file_priv->driver_priv = NULL;
  464. r = pm_runtime_get_sync(dev->dev);
  465. if (r < 0)
  466. return r;
  467. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  468. if (unlikely(!fpriv))
  469. return -ENOMEM;
  470. r = amdgpu_vm_init(adev, &fpriv->vm);
  471. if (r)
  472. goto error_free;
  473. mutex_init(&fpriv->bo_list_lock);
  474. idr_init(&fpriv->bo_list_handles);
  475. /* init context manager */
  476. mutex_init(&fpriv->ctx_mgr.lock);
  477. idr_init(&fpriv->ctx_mgr.ctx_handles);
  478. fpriv->ctx_mgr.adev = adev;
  479. file_priv->driver_priv = fpriv;
  480. pm_runtime_mark_last_busy(dev->dev);
  481. pm_runtime_put_autosuspend(dev->dev);
  482. return 0;
  483. error_free:
  484. kfree(fpriv);
  485. return r;
  486. }
  487. /**
  488. * amdgpu_driver_postclose_kms - drm callback for post close
  489. *
  490. * @dev: drm dev pointer
  491. * @file_priv: drm file
  492. *
  493. * On device post close, tear down vm on cayman+ (all asics).
  494. */
  495. void amdgpu_driver_postclose_kms(struct drm_device *dev,
  496. struct drm_file *file_priv)
  497. {
  498. struct amdgpu_device *adev = dev->dev_private;
  499. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  500. struct amdgpu_bo_list *list;
  501. int handle;
  502. if (!fpriv)
  503. return;
  504. amdgpu_vm_fini(adev, &fpriv->vm);
  505. idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
  506. amdgpu_bo_list_free(list);
  507. idr_destroy(&fpriv->bo_list_handles);
  508. mutex_destroy(&fpriv->bo_list_lock);
  509. /* release context */
  510. amdgpu_ctx_fini(fpriv);
  511. kfree(fpriv);
  512. file_priv->driver_priv = NULL;
  513. }
  514. /**
  515. * amdgpu_driver_preclose_kms - drm callback for pre close
  516. *
  517. * @dev: drm dev pointer
  518. * @file_priv: drm file
  519. *
  520. * On device pre close, tear down hyperz and cmask filps on r1xx-r5xx
  521. * (all asics).
  522. */
  523. void amdgpu_driver_preclose_kms(struct drm_device *dev,
  524. struct drm_file *file_priv)
  525. {
  526. struct amdgpu_device *adev = dev->dev_private;
  527. amdgpu_uvd_free_handles(adev, file_priv);
  528. amdgpu_vce_free_handles(adev, file_priv);
  529. }
  530. /*
  531. * VBlank related functions.
  532. */
  533. /**
  534. * amdgpu_get_vblank_counter_kms - get frame count
  535. *
  536. * @dev: drm dev pointer
  537. * @crtc: crtc to get the frame count from
  538. *
  539. * Gets the frame count on the requested crtc (all asics).
  540. * Returns frame count on success, -EINVAL on failure.
  541. */
  542. u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, int crtc)
  543. {
  544. struct amdgpu_device *adev = dev->dev_private;
  545. if (crtc < 0 || crtc >= adev->mode_info.num_crtc) {
  546. DRM_ERROR("Invalid crtc %d\n", crtc);
  547. return -EINVAL;
  548. }
  549. return amdgpu_display_vblank_get_counter(adev, crtc);
  550. }
  551. /**
  552. * amdgpu_enable_vblank_kms - enable vblank interrupt
  553. *
  554. * @dev: drm dev pointer
  555. * @crtc: crtc to enable vblank interrupt for
  556. *
  557. * Enable the interrupt on the requested crtc (all asics).
  558. * Returns 0 on success, -EINVAL on failure.
  559. */
  560. int amdgpu_enable_vblank_kms(struct drm_device *dev, int crtc)
  561. {
  562. struct amdgpu_device *adev = dev->dev_private;
  563. int idx = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  564. return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
  565. }
  566. /**
  567. * amdgpu_disable_vblank_kms - disable vblank interrupt
  568. *
  569. * @dev: drm dev pointer
  570. * @crtc: crtc to disable vblank interrupt for
  571. *
  572. * Disable the interrupt on the requested crtc (all asics).
  573. */
  574. void amdgpu_disable_vblank_kms(struct drm_device *dev, int crtc)
  575. {
  576. struct amdgpu_device *adev = dev->dev_private;
  577. int idx = amdgpu_crtc_idx_to_irq_type(adev, crtc);
  578. amdgpu_irq_put(adev, &adev->crtc_irq, idx);
  579. }
  580. /**
  581. * amdgpu_get_vblank_timestamp_kms - get vblank timestamp
  582. *
  583. * @dev: drm dev pointer
  584. * @crtc: crtc to get the timestamp for
  585. * @max_error: max error
  586. * @vblank_time: time value
  587. * @flags: flags passed to the driver
  588. *
  589. * Gets the timestamp on the requested crtc based on the
  590. * scanout position. (all asics).
  591. * Returns postive status flags on success, negative error on failure.
  592. */
  593. int amdgpu_get_vblank_timestamp_kms(struct drm_device *dev, int crtc,
  594. int *max_error,
  595. struct timeval *vblank_time,
  596. unsigned flags)
  597. {
  598. struct drm_crtc *drmcrtc;
  599. struct amdgpu_device *adev = dev->dev_private;
  600. if (crtc < 0 || crtc >= dev->num_crtcs) {
  601. DRM_ERROR("Invalid crtc %d\n", crtc);
  602. return -EINVAL;
  603. }
  604. /* Get associated drm_crtc: */
  605. drmcrtc = &adev->mode_info.crtcs[crtc]->base;
  606. /* Helper routine in DRM core does all the work: */
  607. return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
  608. vblank_time, flags,
  609. drmcrtc, &drmcrtc->hwmode);
  610. }
  611. const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  612. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  613. DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  614. DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  615. /* KMS */
  616. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  617. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  618. DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  619. DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  620. DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  621. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  622. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  623. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  624. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
  625. };
  626. int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);