amdgpu_kms.c 28 KB

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