amdgpu_kms.c 30 KB

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