amdgpu_kms.c 30 KB

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