amdgpu_kms.c 27 KB

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