amdgpu_kms.c 23 KB

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