amdgpu_kms.c 27 KB

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