18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2008 Advanced Micro Devices, Inc. 38c2ecf20Sopenharmony_ci * Copyright 2008 Red Hat Inc. 48c2ecf20Sopenharmony_ci * Copyright 2009 Jerome Glisse. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 88c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 98c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 108c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 118c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 148c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software. 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 178c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 188c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 198c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 208c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 218c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 228c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * Authors: Dave Airlie 258c2ecf20Sopenharmony_ci * Alex Deucher 268c2ecf20Sopenharmony_ci * Jerome Glisse 278c2ecf20Sopenharmony_ci * Christian König 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#include <drm/drm_debugfs.h> 318c2ecf20Sopenharmony_ci#include <drm/drm_file.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#include "radeon.h" 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* 368c2ecf20Sopenharmony_ci * IB 378c2ecf20Sopenharmony_ci * IBs (Indirect Buffers) and areas of GPU accessible memory where 388c2ecf20Sopenharmony_ci * commands are stored. You can put a pointer to the IB in the 398c2ecf20Sopenharmony_ci * command ring and the hw will fetch the commands from the IB 408c2ecf20Sopenharmony_ci * and execute them. Generally userspace acceleration drivers 418c2ecf20Sopenharmony_ci * produce command buffers which are send to the kernel and 428c2ecf20Sopenharmony_ci * put in IBs for execution by the requested ring. 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_cistatic int radeon_debugfs_sa_init(struct radeon_device *rdev); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/** 478c2ecf20Sopenharmony_ci * radeon_ib_get - request an IB (Indirect Buffer) 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 508c2ecf20Sopenharmony_ci * @ring: ring index the IB is associated with 518c2ecf20Sopenharmony_ci * @ib: IB object returned 528c2ecf20Sopenharmony_ci * @size: requested IB size 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * Request an IB (all asics). IBs are allocated using the 558c2ecf20Sopenharmony_ci * suballocator. 568c2ecf20Sopenharmony_ci * Returns 0 on success, error on failure. 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_ciint radeon_ib_get(struct radeon_device *rdev, int ring, 598c2ecf20Sopenharmony_ci struct radeon_ib *ib, struct radeon_vm *vm, 608c2ecf20Sopenharmony_ci unsigned size) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci int r; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256); 658c2ecf20Sopenharmony_ci if (r) { 668c2ecf20Sopenharmony_ci dev_err(rdev->dev, "failed to get a new IB (%d)\n", r); 678c2ecf20Sopenharmony_ci return r; 688c2ecf20Sopenharmony_ci } 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci radeon_sync_create(&ib->sync); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci ib->ring = ring; 738c2ecf20Sopenharmony_ci ib->fence = NULL; 748c2ecf20Sopenharmony_ci ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo); 758c2ecf20Sopenharmony_ci ib->vm = vm; 768c2ecf20Sopenharmony_ci if (vm) { 778c2ecf20Sopenharmony_ci /* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address 788c2ecf20Sopenharmony_ci * space and soffset is the offset inside the pool bo 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ci ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET; 818c2ecf20Sopenharmony_ci } else { 828c2ecf20Sopenharmony_ci ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo); 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci ib->is_const_ib = false; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci return 0; 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci/** 908c2ecf20Sopenharmony_ci * radeon_ib_free - free an IB (Indirect Buffer) 918c2ecf20Sopenharmony_ci * 928c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 938c2ecf20Sopenharmony_ci * @ib: IB object to free 948c2ecf20Sopenharmony_ci * 958c2ecf20Sopenharmony_ci * Free an IB (all asics). 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_civoid radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci radeon_sync_free(rdev, &ib->sync, ib->fence); 1008c2ecf20Sopenharmony_ci radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence); 1018c2ecf20Sopenharmony_ci radeon_fence_unref(&ib->fence); 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci/** 1058c2ecf20Sopenharmony_ci * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring 1068c2ecf20Sopenharmony_ci * 1078c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 1088c2ecf20Sopenharmony_ci * @ib: IB object to schedule 1098c2ecf20Sopenharmony_ci * @const_ib: Const IB to schedule (SI only) 1108c2ecf20Sopenharmony_ci * @hdp_flush: Whether or not to perform an HDP cache flush 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * Schedule an IB on the associated ring (all asics). 1138c2ecf20Sopenharmony_ci * Returns 0 on success, error on failure. 1148c2ecf20Sopenharmony_ci * 1158c2ecf20Sopenharmony_ci * On SI, there are two parallel engines fed from the primary ring, 1168c2ecf20Sopenharmony_ci * the CE (Constant Engine) and the DE (Drawing Engine). Since 1178c2ecf20Sopenharmony_ci * resource descriptors have moved to memory, the CE allows you to 1188c2ecf20Sopenharmony_ci * prime the caches while the DE is updating register state so that 1198c2ecf20Sopenharmony_ci * the resource descriptors will be already in cache when the draw is 1208c2ecf20Sopenharmony_ci * processed. To accomplish this, the userspace driver submits two 1218c2ecf20Sopenharmony_ci * IBs, one for the CE and one for the DE. If there is a CE IB (called 1228c2ecf20Sopenharmony_ci * a CONST_IB), it will be put on the ring prior to the DE IB. Prior 1238c2ecf20Sopenharmony_ci * to SI there was just a DE IB. 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_ciint radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib, 1268c2ecf20Sopenharmony_ci struct radeon_ib *const_ib, bool hdp_flush) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci struct radeon_ring *ring = &rdev->ring[ib->ring]; 1298c2ecf20Sopenharmony_ci int r = 0; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci if (!ib->length_dw || !ring->ready) { 1328c2ecf20Sopenharmony_ci /* TODO: Nothings in the ib we should report. */ 1338c2ecf20Sopenharmony_ci dev_err(rdev->dev, "couldn't schedule ib\n"); 1348c2ecf20Sopenharmony_ci return -EINVAL; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci /* 64 dwords should be enough for fence too */ 1388c2ecf20Sopenharmony_ci r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8); 1398c2ecf20Sopenharmony_ci if (r) { 1408c2ecf20Sopenharmony_ci dev_err(rdev->dev, "scheduling IB failed (%d).\n", r); 1418c2ecf20Sopenharmony_ci return r; 1428c2ecf20Sopenharmony_ci } 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* grab a vm id if necessary */ 1458c2ecf20Sopenharmony_ci if (ib->vm) { 1468c2ecf20Sopenharmony_ci struct radeon_fence *vm_id_fence; 1478c2ecf20Sopenharmony_ci vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring); 1488c2ecf20Sopenharmony_ci radeon_sync_fence(&ib->sync, vm_id_fence); 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* sync with other rings */ 1528c2ecf20Sopenharmony_ci r = radeon_sync_rings(rdev, &ib->sync, ib->ring); 1538c2ecf20Sopenharmony_ci if (r) { 1548c2ecf20Sopenharmony_ci dev_err(rdev->dev, "failed to sync rings (%d)\n", r); 1558c2ecf20Sopenharmony_ci radeon_ring_unlock_undo(rdev, ring); 1568c2ecf20Sopenharmony_ci return r; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci if (ib->vm) 1608c2ecf20Sopenharmony_ci radeon_vm_flush(rdev, ib->vm, ib->ring, 1618c2ecf20Sopenharmony_ci ib->sync.last_vm_update); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci if (const_ib) { 1648c2ecf20Sopenharmony_ci radeon_ring_ib_execute(rdev, const_ib->ring, const_ib); 1658c2ecf20Sopenharmony_ci radeon_sync_free(rdev, &const_ib->sync, NULL); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci radeon_ring_ib_execute(rdev, ib->ring, ib); 1688c2ecf20Sopenharmony_ci r = radeon_fence_emit(rdev, &ib->fence, ib->ring); 1698c2ecf20Sopenharmony_ci if (r) { 1708c2ecf20Sopenharmony_ci dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r); 1718c2ecf20Sopenharmony_ci radeon_ring_unlock_undo(rdev, ring); 1728c2ecf20Sopenharmony_ci return r; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci if (const_ib) { 1758c2ecf20Sopenharmony_ci const_ib->fence = radeon_fence_ref(ib->fence); 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci if (ib->vm) 1798c2ecf20Sopenharmony_ci radeon_vm_fence(rdev, ib->vm, ib->fence); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci radeon_ring_unlock_commit(rdev, ring, hdp_flush); 1828c2ecf20Sopenharmony_ci return 0; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci/** 1868c2ecf20Sopenharmony_ci * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool 1878c2ecf20Sopenharmony_ci * 1888c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 1898c2ecf20Sopenharmony_ci * 1908c2ecf20Sopenharmony_ci * Initialize the suballocator to manage a pool of memory 1918c2ecf20Sopenharmony_ci * for use as IBs (all asics). 1928c2ecf20Sopenharmony_ci * Returns 0 on success, error on failure. 1938c2ecf20Sopenharmony_ci */ 1948c2ecf20Sopenharmony_ciint radeon_ib_pool_init(struct radeon_device *rdev) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci int r; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci if (rdev->ib_pool_ready) { 1998c2ecf20Sopenharmony_ci return 0; 2008c2ecf20Sopenharmony_ci } 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci if (rdev->family >= CHIP_BONAIRE) { 2038c2ecf20Sopenharmony_ci r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo, 2048c2ecf20Sopenharmony_ci RADEON_IB_POOL_SIZE*64*1024, 2058c2ecf20Sopenharmony_ci RADEON_GPU_PAGE_SIZE, 2068c2ecf20Sopenharmony_ci RADEON_GEM_DOMAIN_GTT, 2078c2ecf20Sopenharmony_ci RADEON_GEM_GTT_WC); 2088c2ecf20Sopenharmony_ci } else { 2098c2ecf20Sopenharmony_ci /* Before CIK, it's better to stick to cacheable GTT due 2108c2ecf20Sopenharmony_ci * to the command stream checking 2118c2ecf20Sopenharmony_ci */ 2128c2ecf20Sopenharmony_ci r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo, 2138c2ecf20Sopenharmony_ci RADEON_IB_POOL_SIZE*64*1024, 2148c2ecf20Sopenharmony_ci RADEON_GPU_PAGE_SIZE, 2158c2ecf20Sopenharmony_ci RADEON_GEM_DOMAIN_GTT, 0); 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci if (r) { 2188c2ecf20Sopenharmony_ci return r; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo); 2228c2ecf20Sopenharmony_ci if (r) { 2238c2ecf20Sopenharmony_ci return r; 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci rdev->ib_pool_ready = true; 2278c2ecf20Sopenharmony_ci if (radeon_debugfs_sa_init(rdev)) { 2288c2ecf20Sopenharmony_ci dev_err(rdev->dev, "failed to register debugfs file for SA\n"); 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci return 0; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci/** 2348c2ecf20Sopenharmony_ci * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool 2358c2ecf20Sopenharmony_ci * 2368c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 2378c2ecf20Sopenharmony_ci * 2388c2ecf20Sopenharmony_ci * Tear down the suballocator managing the pool of memory 2398c2ecf20Sopenharmony_ci * for use as IBs (all asics). 2408c2ecf20Sopenharmony_ci */ 2418c2ecf20Sopenharmony_civoid radeon_ib_pool_fini(struct radeon_device *rdev) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci if (rdev->ib_pool_ready) { 2448c2ecf20Sopenharmony_ci radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo); 2458c2ecf20Sopenharmony_ci radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo); 2468c2ecf20Sopenharmony_ci rdev->ib_pool_ready = false; 2478c2ecf20Sopenharmony_ci } 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci/** 2518c2ecf20Sopenharmony_ci * radeon_ib_ring_tests - test IBs on the rings 2528c2ecf20Sopenharmony_ci * 2538c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer 2548c2ecf20Sopenharmony_ci * 2558c2ecf20Sopenharmony_ci * Test an IB (Indirect Buffer) on each ring. 2568c2ecf20Sopenharmony_ci * If the test fails, disable the ring. 2578c2ecf20Sopenharmony_ci * Returns 0 on success, error if the primary GFX ring 2588c2ecf20Sopenharmony_ci * IB test fails. 2598c2ecf20Sopenharmony_ci */ 2608c2ecf20Sopenharmony_ciint radeon_ib_ring_tests(struct radeon_device *rdev) 2618c2ecf20Sopenharmony_ci{ 2628c2ecf20Sopenharmony_ci unsigned i; 2638c2ecf20Sopenharmony_ci int r; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci for (i = 0; i < RADEON_NUM_RINGS; ++i) { 2668c2ecf20Sopenharmony_ci struct radeon_ring *ring = &rdev->ring[i]; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci if (!ring->ready) 2698c2ecf20Sopenharmony_ci continue; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci r = radeon_ib_test(rdev, i, ring); 2728c2ecf20Sopenharmony_ci if (r) { 2738c2ecf20Sopenharmony_ci radeon_fence_driver_force_completion(rdev, i); 2748c2ecf20Sopenharmony_ci ring->ready = false; 2758c2ecf20Sopenharmony_ci rdev->needs_reset = false; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci if (i == RADEON_RING_TYPE_GFX_INDEX) { 2788c2ecf20Sopenharmony_ci /* oh, oh, that's really bad */ 2798c2ecf20Sopenharmony_ci DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r); 2808c2ecf20Sopenharmony_ci rdev->accel_working = false; 2818c2ecf20Sopenharmony_ci return r; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci } else { 2848c2ecf20Sopenharmony_ci /* still not good, but we can live with it */ 2858c2ecf20Sopenharmony_ci DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r); 2868c2ecf20Sopenharmony_ci } 2878c2ecf20Sopenharmony_ci } 2888c2ecf20Sopenharmony_ci } 2898c2ecf20Sopenharmony_ci return 0; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci/* 2938c2ecf20Sopenharmony_ci * Debugfs info 2948c2ecf20Sopenharmony_ci */ 2958c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS) 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic int radeon_debugfs_sa_info(struct seq_file *m, void *data) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci struct drm_info_node *node = (struct drm_info_node *) m->private; 3008c2ecf20Sopenharmony_ci struct drm_device *dev = node->minor->dev; 3018c2ecf20Sopenharmony_ci struct radeon_device *rdev = dev->dev_private; 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m); 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci return 0; 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_cistatic struct drm_info_list radeon_debugfs_sa_list[] = { 3108c2ecf20Sopenharmony_ci {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL}, 3118c2ecf20Sopenharmony_ci}; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci#endif 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_cistatic int radeon_debugfs_sa_init(struct radeon_device *rdev) 3168c2ecf20Sopenharmony_ci{ 3178c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS) 3188c2ecf20Sopenharmony_ci return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1); 3198c2ecf20Sopenharmony_ci#else 3208c2ecf20Sopenharmony_ci return 0; 3218c2ecf20Sopenharmony_ci#endif 3228c2ecf20Sopenharmony_ci} 323