Home | History | Annotate | Line # | Download | only in radeon
radeon_ib.c revision 1.2.2.2
      1 /*	$NetBSD: radeon_ib.c,v 1.2.2.2 2018/09/06 06:56:32 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright 2008 Advanced Micro Devices, Inc.
      5  * Copyright 2008 Red Hat Inc.
      6  * Copyright 2009 Jerome Glisse.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the "Software"),
     10  * to deal in the Software without restriction, including without limitation
     11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  * and/or sell copies of the Software, and to permit persons to whom the
     13  * Software is furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in
     16  * all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  * OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  * Authors: Dave Airlie
     27  *          Alex Deucher
     28  *          Jerome Glisse
     29  *          Christian Knig
     30  */
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: radeon_ib.c,v 1.2.2.2 2018/09/06 06:56:32 pgoyette Exp $");
     33 
     34 #include <drm/drmP.h>
     35 #include "radeon.h"
     36 
     37 /*
     38  * IB
     39  * IBs (Indirect Buffers) and areas of GPU accessible memory where
     40  * commands are stored.  You can put a pointer to the IB in the
     41  * command ring and the hw will fetch the commands from the IB
     42  * and execute them.  Generally userspace acceleration drivers
     43  * produce command buffers which are send to the kernel and
     44  * put in IBs for execution by the requested ring.
     45  */
     46 static int radeon_debugfs_sa_init(struct radeon_device *rdev);
     47 
     48 /**
     49  * radeon_ib_get - request an IB (Indirect Buffer)
     50  *
     51  * @rdev: radeon_device pointer
     52  * @ring: ring index the IB is associated with
     53  * @ib: IB object returned
     54  * @size: requested IB size
     55  *
     56  * Request an IB (all asics).  IBs are allocated using the
     57  * suballocator.
     58  * Returns 0 on success, error on failure.
     59  */
     60 int radeon_ib_get(struct radeon_device *rdev, int ring,
     61 		  struct radeon_ib *ib, struct radeon_vm *vm,
     62 		  unsigned size)
     63 {
     64 	int r;
     65 
     66 	r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256);
     67 	if (r) {
     68 		dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
     69 		return r;
     70 	}
     71 
     72 	radeon_sync_create(&ib->sync);
     73 
     74 	ib->ring = ring;
     75 	ib->fence = NULL;
     76 	ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
     77 	ib->vm = vm;
     78 	if (vm) {
     79 		/* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address
     80 		 * space and soffset is the offset inside the pool bo
     81 		 */
     82 		ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET;
     83 	} else {
     84 		ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
     85 	}
     86 	ib->is_const_ib = false;
     87 
     88 	return 0;
     89 }
     90 
     91 /**
     92  * radeon_ib_free - free an IB (Indirect Buffer)
     93  *
     94  * @rdev: radeon_device pointer
     95  * @ib: IB object to free
     96  *
     97  * Free an IB (all asics).
     98  */
     99 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
    100 {
    101 	radeon_sync_free(rdev, &ib->sync, ib->fence);
    102 	radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
    103 	radeon_fence_unref(&ib->fence);
    104 }
    105 
    106 /**
    107  * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring
    108  *
    109  * @rdev: radeon_device pointer
    110  * @ib: IB object to schedule
    111  * @const_ib: Const IB to schedule (SI only)
    112  * @hdp_flush: Whether or not to perform an HDP cache flush
    113  *
    114  * Schedule an IB on the associated ring (all asics).
    115  * Returns 0 on success, error on failure.
    116  *
    117  * On SI, there are two parallel engines fed from the primary ring,
    118  * the CE (Constant Engine) and the DE (Drawing Engine).  Since
    119  * resource descriptors have moved to memory, the CE allows you to
    120  * prime the caches while the DE is updating register state so that
    121  * the resource descriptors will be already in cache when the draw is
    122  * processed.  To accomplish this, the userspace driver submits two
    123  * IBs, one for the CE and one for the DE.  If there is a CE IB (called
    124  * a CONST_IB), it will be put on the ring prior to the DE IB.  Prior
    125  * to SI there was just a DE IB.
    126  */
    127 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
    128 		       struct radeon_ib *const_ib, bool hdp_flush)
    129 {
    130 	struct radeon_ring *ring = &rdev->ring[ib->ring];
    131 	int r = 0;
    132 
    133 	if (!ib->length_dw || !ring->ready) {
    134 		/* TODO: Nothings in the ib we should report. */
    135 		dev_err(rdev->dev, "couldn't schedule ib\n");
    136 		return -EINVAL;
    137 	}
    138 
    139 	/* 64 dwords should be enough for fence too */
    140 	r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8);
    141 	if (r) {
    142 		dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
    143 		return r;
    144 	}
    145 
    146 	/* grab a vm id if necessary */
    147 	if (ib->vm) {
    148 		struct radeon_fence *vm_id_fence;
    149 		vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring);
    150 		radeon_sync_fence(&ib->sync, vm_id_fence);
    151 	}
    152 
    153 	/* sync with other rings */
    154 	r = radeon_sync_rings(rdev, &ib->sync, ib->ring);
    155 	if (r) {
    156 		dev_err(rdev->dev, "failed to sync rings (%d)\n", r);
    157 		radeon_ring_unlock_undo(rdev, ring);
    158 		return r;
    159 	}
    160 
    161 	if (ib->vm)
    162 		radeon_vm_flush(rdev, ib->vm, ib->ring,
    163 				ib->sync.last_vm_update);
    164 
    165 	if (const_ib) {
    166 		radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
    167 		radeon_sync_free(rdev, &const_ib->sync, NULL);
    168 	}
    169 	radeon_ring_ib_execute(rdev, ib->ring, ib);
    170 	r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
    171 	if (r) {
    172 		dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
    173 		radeon_ring_unlock_undo(rdev, ring);
    174 		return r;
    175 	}
    176 	if (const_ib) {
    177 		const_ib->fence = radeon_fence_ref(ib->fence);
    178 	}
    179 
    180 	if (ib->vm)
    181 		radeon_vm_fence(rdev, ib->vm, ib->fence);
    182 
    183 	radeon_ring_unlock_commit(rdev, ring, hdp_flush);
    184 	return 0;
    185 }
    186 
    187 /**
    188  * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
    189  *
    190  * @rdev: radeon_device pointer
    191  *
    192  * Initialize the suballocator to manage a pool of memory
    193  * for use as IBs (all asics).
    194  * Returns 0 on success, error on failure.
    195  */
    196 int radeon_ib_pool_init(struct radeon_device *rdev)
    197 {
    198 	int r;
    199 
    200 	if (rdev->ib_pool_ready) {
    201 		return 0;
    202 	}
    203 
    204 	if (rdev->family >= CHIP_BONAIRE) {
    205 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
    206 					      RADEON_IB_POOL_SIZE*64*1024,
    207 					      RADEON_GPU_PAGE_SIZE,
    208 					      RADEON_GEM_DOMAIN_GTT,
    209 					      RADEON_GEM_GTT_WC);
    210 	} else {
    211 		/* Before CIK, it's better to stick to cacheable GTT due
    212 		 * to the command stream checking
    213 		 */
    214 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
    215 					      RADEON_IB_POOL_SIZE*64*1024,
    216 					      RADEON_GPU_PAGE_SIZE,
    217 					      RADEON_GEM_DOMAIN_GTT, 0);
    218 	}
    219 	if (r) {
    220 		return r;
    221 	}
    222 
    223 	r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
    224 	if (r) {
    225 		return r;
    226 	}
    227 
    228 	rdev->ib_pool_ready = true;
    229 	if (radeon_debugfs_sa_init(rdev)) {
    230 		dev_err(rdev->dev, "failed to register debugfs file for SA\n");
    231 	}
    232 	return 0;
    233 }
    234 
    235 /**
    236  * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
    237  *
    238  * @rdev: radeon_device pointer
    239  *
    240  * Tear down the suballocator managing the pool of memory
    241  * for use as IBs (all asics).
    242  */
    243 void radeon_ib_pool_fini(struct radeon_device *rdev)
    244 {
    245 	if (rdev->ib_pool_ready) {
    246 		radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
    247 		radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
    248 		rdev->ib_pool_ready = false;
    249 	}
    250 }
    251 
    252 /**
    253  * radeon_ib_ring_tests - test IBs on the rings
    254  *
    255  * @rdev: radeon_device pointer
    256  *
    257  * Test an IB (Indirect Buffer) on each ring.
    258  * If the test fails, disable the ring.
    259  * Returns 0 on success, error if the primary GFX ring
    260  * IB test fails.
    261  */
    262 int radeon_ib_ring_tests(struct radeon_device *rdev)
    263 {
    264 	unsigned i;
    265 	int r;
    266 
    267 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    268 		struct radeon_ring *ring = &rdev->ring[i];
    269 
    270 		if (!ring->ready)
    271 			continue;
    272 
    273 		r = radeon_ib_test(rdev, i, ring);
    274 		if (r) {
    275 			radeon_fence_driver_force_completion(rdev, i);
    276 			ring->ready = false;
    277 			rdev->needs_reset = false;
    278 
    279 			if (i == RADEON_RING_TYPE_GFX_INDEX) {
    280 				/* oh, oh, that's really bad */
    281 				DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
    282 		                rdev->accel_working = false;
    283 				return r;
    284 
    285 			} else {
    286 				/* still not good, but we can live with it */
    287 				DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
    288 			}
    289 		}
    290 	}
    291 	return 0;
    292 }
    293 
    294 /*
    295  * Debugfs info
    296  */
    297 #if defined(CONFIG_DEBUG_FS)
    298 
    299 static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
    300 {
    301 	struct drm_info_node *node = (struct drm_info_node *) m->private;
    302 	struct drm_device *dev = node->minor->dev;
    303 	struct radeon_device *rdev = dev->dev_private;
    304 
    305 	radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
    306 
    307 	return 0;
    308 
    309 }
    310 
    311 static struct drm_info_list radeon_debugfs_sa_list[] = {
    312         {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
    313 };
    314 
    315 #endif
    316 
    317 static int radeon_debugfs_sa_init(struct radeon_device *rdev)
    318 {
    319 #if defined(CONFIG_DEBUG_FS)
    320 	return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
    321 #else
    322 	return 0;
    323 #endif
    324 }
    325