amdgpu_kms.c 28 KB

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