amdgpu_kms.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 =
  425. adev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk * 10;
  426. dev_info.max_memory_clock =
  427. adev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk * 10;
  428. } else {
  429. dev_info.max_engine_clock = adev->pm.default_sclk * 10;
  430. dev_info.max_memory_clock = adev->pm.default_mclk * 10;
  431. }
  432. dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
  433. dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se *
  434. adev->gfx.config.max_shader_engines;
  435. dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
  436. dev_info._pad = 0;
  437. dev_info.ids_flags = 0;
  438. if (adev->flags & AMD_IS_APU)
  439. dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
  440. dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
  441. dev_info.virtual_address_max = (uint64_t)adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
  442. dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
  443. dev_info.pte_fragment_size = (1 << AMDGPU_LOG2_PAGES_PER_FRAG) *
  444. AMDGPU_GPU_PAGE_SIZE;
  445. dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE;
  446. dev_info.cu_active_number = adev->gfx.cu_info.number;
  447. dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
  448. dev_info.ce_ram_size = adev->gfx.ce_ram_size;
  449. memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
  450. sizeof(adev->gfx.cu_info.bitmap));
  451. dev_info.vram_type = adev->mc.vram_type;
  452. dev_info.vram_bit_width = adev->mc.vram_width;
  453. dev_info.vce_harvest_config = adev->vce.harvest_config;
  454. return copy_to_user(out, &dev_info,
  455. min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0;
  456. }
  457. default:
  458. DRM_DEBUG_KMS("Invalid request %d\n", info->query);
  459. return -EINVAL;
  460. }
  461. return 0;
  462. }
  463. /*
  464. * Outdated mess for old drm with Xorg being in charge (void function now).
  465. */
  466. /**
  467. * amdgpu_driver_lastclose_kms - drm callback for last close
  468. *
  469. * @dev: drm dev pointer
  470. *
  471. * Switch vga_switcheroo state after last close (all asics).
  472. */
  473. void amdgpu_driver_lastclose_kms(struct drm_device *dev)
  474. {
  475. struct amdgpu_device *adev = dev->dev_private;
  476. amdgpu_fbdev_restore_mode(adev);
  477. vga_switcheroo_process_delayed_switch();
  478. }
  479. /**
  480. * amdgpu_driver_open_kms - drm callback for open
  481. *
  482. * @dev: drm dev pointer
  483. * @file_priv: drm file
  484. *
  485. * On device open, init vm on cayman+ (all asics).
  486. * Returns 0 on success, error on failure.
  487. */
  488. int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
  489. {
  490. struct amdgpu_device *adev = dev->dev_private;
  491. struct amdgpu_fpriv *fpriv;
  492. int r;
  493. file_priv->driver_priv = NULL;
  494. r = pm_runtime_get_sync(dev->dev);
  495. if (r < 0)
  496. return r;
  497. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  498. if (unlikely(!fpriv)) {
  499. r = -ENOMEM;
  500. goto out_suspend;
  501. }
  502. r = amdgpu_vm_init(adev, &fpriv->vm);
  503. if (r) {
  504. kfree(fpriv);
  505. goto out_suspend;
  506. }
  507. mutex_init(&fpriv->bo_list_lock);
  508. idr_init(&fpriv->bo_list_handles);
  509. amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
  510. file_priv->driver_priv = fpriv;
  511. out_suspend:
  512. pm_runtime_mark_last_busy(dev->dev);
  513. pm_runtime_put_autosuspend(dev->dev);
  514. return r;
  515. }
  516. /**
  517. * amdgpu_driver_postclose_kms - drm callback for post close
  518. *
  519. * @dev: drm dev pointer
  520. * @file_priv: drm file
  521. *
  522. * On device post close, tear down vm on cayman+ (all asics).
  523. */
  524. void amdgpu_driver_postclose_kms(struct drm_device *dev,
  525. struct drm_file *file_priv)
  526. {
  527. struct amdgpu_device *adev = dev->dev_private;
  528. struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  529. struct amdgpu_bo_list *list;
  530. int handle;
  531. if (!fpriv)
  532. return;
  533. amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
  534. amdgpu_uvd_free_handles(adev, file_priv);
  535. amdgpu_vce_free_handles(adev, file_priv);
  536. amdgpu_vm_fini(adev, &fpriv->vm);
  537. idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
  538. amdgpu_bo_list_free(list);
  539. idr_destroy(&fpriv->bo_list_handles);
  540. mutex_destroy(&fpriv->bo_list_lock);
  541. kfree(fpriv);
  542. file_priv->driver_priv = NULL;
  543. pm_runtime_mark_last_busy(dev->dev);
  544. pm_runtime_put_autosuspend(dev->dev);
  545. }
  546. /**
  547. * amdgpu_driver_preclose_kms - drm callback for pre close
  548. *
  549. * @dev: drm dev pointer
  550. * @file_priv: drm file
  551. *
  552. * On device pre close, tear down hyperz and cmask filps on r1xx-r5xx
  553. * (all asics).
  554. */
  555. void amdgpu_driver_preclose_kms(struct drm_device *dev,
  556. struct drm_file *file_priv)
  557. {
  558. pm_runtime_get_sync(dev->dev);
  559. }
  560. /*
  561. * VBlank related functions.
  562. */
  563. /**
  564. * amdgpu_get_vblank_counter_kms - get frame count
  565. *
  566. * @dev: drm dev pointer
  567. * @pipe: crtc to get the frame count from
  568. *
  569. * Gets the frame count on the requested crtc (all asics).
  570. * Returns frame count on success, -EINVAL on failure.
  571. */
  572. u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
  573. {
  574. struct amdgpu_device *adev = dev->dev_private;
  575. int vpos, hpos, stat;
  576. u32 count;
  577. if (pipe >= adev->mode_info.num_crtc) {
  578. DRM_ERROR("Invalid crtc %u\n", pipe);
  579. return -EINVAL;
  580. }
  581. /* The hw increments its frame counter at start of vsync, not at start
  582. * of vblank, as is required by DRM core vblank counter handling.
  583. * Cook the hw count here to make it appear to the caller as if it
  584. * incremented at start of vblank. We measure distance to start of
  585. * vblank in vpos. vpos therefore will be >= 0 between start of vblank
  586. * and start of vsync, so vpos >= 0 means to bump the hw frame counter
  587. * result by 1 to give the proper appearance to caller.
  588. */
  589. if (adev->mode_info.crtcs[pipe]) {
  590. /* Repeat readout if needed to provide stable result if
  591. * we cross start of vsync during the queries.
  592. */
  593. do {
  594. count = amdgpu_display_vblank_get_counter(adev, pipe);
  595. /* Ask amdgpu_get_crtc_scanoutpos to return vpos as
  596. * distance to start of vblank, instead of regular
  597. * vertical scanout pos.
  598. */
  599. stat = amdgpu_get_crtc_scanoutpos(
  600. dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
  601. &vpos, &hpos, NULL, NULL,
  602. &adev->mode_info.crtcs[pipe]->base.hwmode);
  603. } while (count != amdgpu_display_vblank_get_counter(adev, pipe));
  604. if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
  605. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
  606. DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
  607. } else {
  608. DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
  609. pipe, vpos);
  610. /* Bump counter if we are at >= leading edge of vblank,
  611. * but before vsync where vpos would turn negative and
  612. * the hw counter really increments.
  613. */
  614. if (vpos >= 0)
  615. count++;
  616. }
  617. } else {
  618. /* Fallback to use value as is. */
  619. count = amdgpu_display_vblank_get_counter(adev, pipe);
  620. DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
  621. }
  622. return count;
  623. }
  624. /**
  625. * amdgpu_enable_vblank_kms - enable vblank interrupt
  626. *
  627. * @dev: drm dev pointer
  628. * @pipe: crtc to enable vblank interrupt for
  629. *
  630. * Enable the interrupt on the requested crtc (all asics).
  631. * Returns 0 on success, -EINVAL on failure.
  632. */
  633. int amdgpu_enable_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. return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
  638. }
  639. /**
  640. * amdgpu_disable_vblank_kms - disable vblank interrupt
  641. *
  642. * @dev: drm dev pointer
  643. * @pipe: crtc to disable vblank interrupt for
  644. *
  645. * Disable the interrupt on the requested crtc (all asics).
  646. */
  647. void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
  648. {
  649. struct amdgpu_device *adev = dev->dev_private;
  650. int idx = amdgpu_crtc_idx_to_irq_type(adev, pipe);
  651. amdgpu_irq_put(adev, &adev->crtc_irq, idx);
  652. }
  653. /**
  654. * amdgpu_get_vblank_timestamp_kms - get vblank timestamp
  655. *
  656. * @dev: drm dev pointer
  657. * @crtc: crtc to get the timestamp for
  658. * @max_error: max error
  659. * @vblank_time: time value
  660. * @flags: flags passed to the driver
  661. *
  662. * Gets the timestamp on the requested crtc based on the
  663. * scanout position. (all asics).
  664. * Returns postive status flags on success, negative error on failure.
  665. */
  666. int amdgpu_get_vblank_timestamp_kms(struct drm_device *dev, unsigned int pipe,
  667. int *max_error,
  668. struct timeval *vblank_time,
  669. unsigned flags)
  670. {
  671. struct drm_crtc *crtc;
  672. struct amdgpu_device *adev = dev->dev_private;
  673. if (pipe >= dev->num_crtcs) {
  674. DRM_ERROR("Invalid crtc %u\n", pipe);
  675. return -EINVAL;
  676. }
  677. /* Get associated drm_crtc: */
  678. crtc = &adev->mode_info.crtcs[pipe]->base;
  679. if (!crtc) {
  680. /* This can occur on driver load if some component fails to
  681. * initialize completely and driver is unloaded */
  682. DRM_ERROR("Uninitialized crtc %d\n", pipe);
  683. return -EINVAL;
  684. }
  685. /* Helper routine in DRM core does all the work: */
  686. return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
  687. vblank_time, flags,
  688. &crtc->hwmode);
  689. }
  690. const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  691. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  692. DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  693. DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  694. /* KMS */
  695. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  696. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  697. DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  698. DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  699. DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  700. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  701. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  702. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  703. DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  704. };
  705. const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);
  706. /*
  707. * Debugfs info
  708. */
  709. #if defined(CONFIG_DEBUG_FS)
  710. static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data)
  711. {
  712. struct drm_info_node *node = (struct drm_info_node *) m->private;
  713. struct drm_device *dev = node->minor->dev;
  714. struct amdgpu_device *adev = dev->dev_private;
  715. struct drm_amdgpu_info_firmware fw_info;
  716. struct drm_amdgpu_query_fw query_fw;
  717. int ret, i;
  718. /* VCE */
  719. query_fw.fw_type = AMDGPU_INFO_FW_VCE;
  720. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  721. if (ret)
  722. return ret;
  723. seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
  724. fw_info.feature, fw_info.ver);
  725. /* UVD */
  726. query_fw.fw_type = AMDGPU_INFO_FW_UVD;
  727. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  728. if (ret)
  729. return ret;
  730. seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
  731. fw_info.feature, fw_info.ver);
  732. /* GMC */
  733. query_fw.fw_type = AMDGPU_INFO_FW_GMC;
  734. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  735. if (ret)
  736. return ret;
  737. seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
  738. fw_info.feature, fw_info.ver);
  739. /* ME */
  740. query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
  741. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  742. if (ret)
  743. return ret;
  744. seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
  745. fw_info.feature, fw_info.ver);
  746. /* PFP */
  747. query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
  748. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  749. if (ret)
  750. return ret;
  751. seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
  752. fw_info.feature, fw_info.ver);
  753. /* CE */
  754. query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
  755. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  756. if (ret)
  757. return ret;
  758. seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
  759. fw_info.feature, fw_info.ver);
  760. /* RLC */
  761. query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
  762. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  763. if (ret)
  764. return ret;
  765. seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
  766. fw_info.feature, fw_info.ver);
  767. /* MEC */
  768. query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
  769. query_fw.index = 0;
  770. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  771. if (ret)
  772. return ret;
  773. seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
  774. fw_info.feature, fw_info.ver);
  775. /* MEC2 */
  776. if (adev->asic_type == CHIP_KAVERI ||
  777. (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) {
  778. query_fw.index = 1;
  779. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  780. if (ret)
  781. return ret;
  782. seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
  783. fw_info.feature, fw_info.ver);
  784. }
  785. /* SMC */
  786. query_fw.fw_type = AMDGPU_INFO_FW_SMC;
  787. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  788. if (ret)
  789. return ret;
  790. seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n",
  791. fw_info.feature, fw_info.ver);
  792. /* SDMA */
  793. query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
  794. for (i = 0; i < adev->sdma.num_instances; i++) {
  795. query_fw.index = i;
  796. ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
  797. if (ret)
  798. return ret;
  799. seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
  800. i, fw_info.feature, fw_info.ver);
  801. }
  802. return 0;
  803. }
  804. static const struct drm_info_list amdgpu_firmware_info_list[] = {
  805. {"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL},
  806. };
  807. #endif
  808. int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
  809. {
  810. #if defined(CONFIG_DEBUG_FS)
  811. return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list,
  812. ARRAY_SIZE(amdgpu_firmware_info_list));
  813. #else
  814. return 0;
  815. #endif
  816. }