Home | History | Annotate | Line # | Download | only in nouveau
nouveau_dma.c revision 1.1.1.3
      1 /*	$NetBSD: nouveau_dma.c,v 1.1.1.3 2018/08/27 01:34:55 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2007 Ben Skeggs.
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining
      8  * a copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sublicense, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * The above copyright notice and this permission notice (including the
     16  * next paragraph) shall be included in all copies or substantial
     17  * portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     22  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
     23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  *
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: nouveau_dma.c,v 1.1.1.3 2018/08/27 01:34:55 riastradh Exp $");
     31 
     32 #include "nouveau_drm.h"
     33 #include "nouveau_dma.h"
     34 
     35 void
     36 OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned nr_dwords)
     37 {
     38 	bool is_iomem;
     39 	u32 *mem = ttm_kmap_obj_virtual(&chan->push.buffer->kmap, &is_iomem);
     40 	mem = &mem[chan->dma.cur];
     41 	if (is_iomem)
     42 		memcpy_toio((void __force __iomem *)mem, data, nr_dwords * 4);
     43 	else
     44 		memcpy(mem, data, nr_dwords * 4);
     45 	chan->dma.cur += nr_dwords;
     46 }
     47 
     48 /* Fetch and adjust GPU GET pointer
     49  *
     50  * Returns:
     51  *  value >= 0, the adjusted GET pointer
     52  *  -EINVAL if GET pointer currently outside main push buffer
     53  *  -EBUSY if timeout exceeded
     54  */
     55 static inline int
     56 READ_GET(struct nouveau_channel *chan, uint64_t *prev_get, int *timeout)
     57 {
     58 	uint64_t val;
     59 
     60 	val = nvif_rd32(&chan->user, chan->user_get);
     61         if (chan->user_get_hi)
     62                 val |= (uint64_t)nvif_rd32(&chan->user, chan->user_get_hi) << 32;
     63 
     64 	/* reset counter as long as GET is still advancing, this is
     65 	 * to avoid misdetecting a GPU lockup if the GPU happens to
     66 	 * just be processing an operation that takes a long time
     67 	 */
     68 	if (val != *prev_get) {
     69 		*prev_get = val;
     70 		*timeout = 0;
     71 	}
     72 
     73 	if ((++*timeout & 0xff) == 0) {
     74 		udelay(1);
     75 		if (*timeout > 100000)
     76 			return -EBUSY;
     77 	}
     78 
     79 	if (val < chan->push.vma.offset ||
     80 	    val > chan->push.vma.offset + (chan->dma.max << 2))
     81 		return -EINVAL;
     82 
     83 	return (val - chan->push.vma.offset) >> 2;
     84 }
     85 
     86 void
     87 nv50_dma_push(struct nouveau_channel *chan, struct nouveau_bo *bo,
     88 	      int delta, int length)
     89 {
     90 	struct nouveau_cli *cli = (void *)chan->user.client;
     91 	struct nouveau_bo *pb = chan->push.buffer;
     92 	struct nvkm_vma *vma;
     93 	int ip = (chan->dma.ib_put * 2) + chan->dma.ib_base;
     94 	u64 offset;
     95 
     96 	vma = nouveau_bo_vma_find(bo, cli->vm);
     97 	BUG_ON(!vma);
     98 	offset = vma->offset + delta;
     99 
    100 	BUG_ON(chan->dma.ib_free < 1);
    101 
    102 	nouveau_bo_wr32(pb, ip++, lower_32_bits(offset));
    103 	nouveau_bo_wr32(pb, ip++, upper_32_bits(offset) | length << 8);
    104 
    105 	chan->dma.ib_put = (chan->dma.ib_put + 1) & chan->dma.ib_max;
    106 
    107 	mb();
    108 	/* Flush writes. */
    109 	nouveau_bo_rd32(pb, 0);
    110 
    111 	nvif_wr32(&chan->user, 0x8c, chan->dma.ib_put);
    112 	chan->dma.ib_free--;
    113 }
    114 
    115 static int
    116 nv50_dma_push_wait(struct nouveau_channel *chan, int count)
    117 {
    118 	uint32_t cnt = 0, prev_get = 0;
    119 
    120 	while (chan->dma.ib_free < count) {
    121 		uint32_t get = nvif_rd32(&chan->user, 0x88);
    122 		if (get != prev_get) {
    123 			prev_get = get;
    124 			cnt = 0;
    125 		}
    126 
    127 		if ((++cnt & 0xff) == 0) {
    128 			DRM_UDELAY(1);
    129 			if (cnt > 100000)
    130 				return -EBUSY;
    131 		}
    132 
    133 		chan->dma.ib_free = get - chan->dma.ib_put;
    134 		if (chan->dma.ib_free <= 0)
    135 			chan->dma.ib_free += chan->dma.ib_max;
    136 	}
    137 
    138 	return 0;
    139 }
    140 
    141 static int
    142 nv50_dma_wait(struct nouveau_channel *chan, int slots, int count)
    143 {
    144 	uint64_t prev_get = 0;
    145 	int ret, cnt = 0;
    146 
    147 	ret = nv50_dma_push_wait(chan, slots + 1);
    148 	if (unlikely(ret))
    149 		return ret;
    150 
    151 	while (chan->dma.free < count) {
    152 		int get = READ_GET(chan, &prev_get, &cnt);
    153 		if (unlikely(get < 0)) {
    154 			if (get == -EINVAL)
    155 				continue;
    156 
    157 			return get;
    158 		}
    159 
    160 		if (get <= chan->dma.cur) {
    161 			chan->dma.free = chan->dma.max - chan->dma.cur;
    162 			if (chan->dma.free >= count)
    163 				break;
    164 
    165 			FIRE_RING(chan);
    166 			do {
    167 				get = READ_GET(chan, &prev_get, &cnt);
    168 				if (unlikely(get < 0)) {
    169 					if (get == -EINVAL)
    170 						continue;
    171 					return get;
    172 				}
    173 			} while (get == 0);
    174 			chan->dma.cur = 0;
    175 			chan->dma.put = 0;
    176 		}
    177 
    178 		chan->dma.free = get - chan->dma.cur - 1;
    179 	}
    180 
    181 	return 0;
    182 }
    183 
    184 int
    185 nouveau_dma_wait(struct nouveau_channel *chan, int slots, int size)
    186 {
    187 	uint64_t prev_get = 0;
    188 	int cnt = 0, get;
    189 
    190 	if (chan->dma.ib_max)
    191 		return nv50_dma_wait(chan, slots, size);
    192 
    193 	while (chan->dma.free < size) {
    194 		get = READ_GET(chan, &prev_get, &cnt);
    195 		if (unlikely(get == -EBUSY))
    196 			return -EBUSY;
    197 
    198 		/* loop until we have a usable GET pointer.  the value
    199 		 * we read from the GPU may be outside the main ring if
    200 		 * PFIFO is processing a buffer called from the main ring,
    201 		 * discard these values until something sensible is seen.
    202 		 *
    203 		 * the other case we discard GET is while the GPU is fetching
    204 		 * from the SKIPS area, so the code below doesn't have to deal
    205 		 * with some fun corner cases.
    206 		 */
    207 		if (unlikely(get == -EINVAL) || get < NOUVEAU_DMA_SKIPS)
    208 			continue;
    209 
    210 		if (get <= chan->dma.cur) {
    211 			/* engine is fetching behind us, or is completely
    212 			 * idle (GET == PUT) so we have free space up until
    213 			 * the end of the push buffer
    214 			 *
    215 			 * we can only hit that path once per call due to
    216 			 * looping back to the beginning of the push buffer,
    217 			 * we'll hit the fetching-ahead-of-us path from that
    218 			 * point on.
    219 			 *
    220 			 * the *one* exception to that rule is if we read
    221 			 * GET==PUT, in which case the below conditional will
    222 			 * always succeed and break us out of the wait loop.
    223 			 */
    224 			chan->dma.free = chan->dma.max - chan->dma.cur;
    225 			if (chan->dma.free >= size)
    226 				break;
    227 
    228 			/* not enough space left at the end of the push buffer,
    229 			 * instruct the GPU to jump back to the start right
    230 			 * after processing the currently pending commands.
    231 			 */
    232 			OUT_RING(chan, chan->push.vma.offset | 0x20000000);
    233 
    234 			/* wait for GET to depart from the skips area.
    235 			 * prevents writing GET==PUT and causing a race
    236 			 * condition that causes us to think the GPU is
    237 			 * idle when it's not.
    238 			 */
    239 			do {
    240 				get = READ_GET(chan, &prev_get, &cnt);
    241 				if (unlikely(get == -EBUSY))
    242 					return -EBUSY;
    243 				if (unlikely(get == -EINVAL))
    244 					continue;
    245 			} while (get <= NOUVEAU_DMA_SKIPS);
    246 			WRITE_PUT(NOUVEAU_DMA_SKIPS);
    247 
    248 			/* we're now submitting commands at the start of
    249 			 * the push buffer.
    250 			 */
    251 			chan->dma.cur  =
    252 			chan->dma.put  = NOUVEAU_DMA_SKIPS;
    253 		}
    254 
    255 		/* engine fetching ahead of us, we have space up until the
    256 		 * current GET pointer.  the "- 1" is to ensure there's
    257 		 * space left to emit a jump back to the beginning of the
    258 		 * push buffer if we require it.  we can never get GET == PUT
    259 		 * here, so this is safe.
    260 		 */
    261 		chan->dma.free = get - chan->dma.cur - 1;
    262 	}
    263 
    264 	return 0;
    265 }
    266 
    267