xref: /linux/drivers/gpu/drm/i915/gt/intel_ring.c (revision b9d7eb6a31be296ca0af95641a23c4c758703c0a)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include "gem/i915_gem_lmem.h"
7 #include "gem/i915_gem_object.h"
8 
9 #include "i915_drv.h"
10 #include "i915_vma.h"
11 #include "intel_engine.h"
12 #include "intel_engine_regs.h"
13 #include "intel_gpu_commands.h"
14 #include "intel_ring.h"
15 #include "intel_timeline.h"
16 
17 unsigned int intel_ring_update_space(struct intel_ring *ring)
18 {
19 	unsigned int space;
20 
21 	space = __intel_ring_space(ring->head, ring->emit, ring->size);
22 
23 	ring->space = space;
24 	return space;
25 }
26 
27 void __intel_ring_pin(struct intel_ring *ring)
28 {
29 	GEM_BUG_ON(!atomic_read(&ring->pin_count));
30 	atomic_inc(&ring->pin_count);
31 }
32 
33 int intel_ring_pin(struct intel_ring *ring, struct i915_gem_ww_ctx *ww)
34 {
35 	struct i915_vma *vma = ring->vma;
36 	unsigned int flags;
37 	void *addr;
38 	int ret;
39 
40 	if (atomic_fetch_inc(&ring->pin_count))
41 		return 0;
42 
43 	/* Ring wraparound at offset 0 sometimes hangs. No idea why. */
44 	flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
45 
46 	if (i915_gem_object_is_stolen(vma->obj))
47 		flags |= PIN_MAPPABLE;
48 	else
49 		flags |= PIN_HIGH;
50 
51 	ret = i915_ggtt_pin(vma, ww, 0, flags);
52 	if (unlikely(ret))
53 		goto err_unpin;
54 
55 	if (i915_vma_is_map_and_fenceable(vma)) {
56 		addr = (void __force *)i915_vma_pin_iomap(vma);
57 	} else {
58 		int type = i915_coherent_map_type(vma->vm->i915, vma->obj, false);
59 
60 		addr = i915_gem_object_pin_map(vma->obj, type);
61 	}
62 
63 	if (IS_ERR(addr)) {
64 		ret = PTR_ERR(addr);
65 		goto err_ring;
66 	}
67 
68 	i915_vma_make_unshrinkable(vma);
69 
70 	/* Discard any unused bytes beyond that submitted to hw. */
71 	intel_ring_reset(ring, ring->emit);
72 
73 	ring->vaddr = addr;
74 	return 0;
75 
76 err_ring:
77 	i915_vma_unpin(vma);
78 err_unpin:
79 	atomic_dec(&ring->pin_count);
80 	return ret;
81 }
82 
83 void intel_ring_reset(struct intel_ring *ring, u32 tail)
84 {
85 	tail = intel_ring_wrap(ring, tail);
86 	ring->tail = tail;
87 	ring->head = tail;
88 	ring->emit = tail;
89 	intel_ring_update_space(ring);
90 }
91 
92 void intel_ring_unpin(struct intel_ring *ring)
93 {
94 	struct i915_vma *vma = ring->vma;
95 
96 	if (!atomic_dec_and_test(&ring->pin_count))
97 		return;
98 
99 	i915_vma_unset_ggtt_write(vma);
100 	if (i915_vma_is_map_and_fenceable(vma))
101 		i915_vma_unpin_iomap(vma);
102 	else
103 		i915_gem_object_unpin_map(vma->obj);
104 
105 	i915_vma_make_purgeable(vma);
106 	i915_vma_unpin(vma);
107 }
108 
109 static struct i915_vma *create_ring_vma(struct i915_ggtt *ggtt, int size)
110 {
111 	struct i915_address_space *vm = &ggtt->vm;
112 	struct drm_i915_private *i915 = vm->i915;
113 	struct drm_i915_gem_object *obj;
114 	struct i915_vma *vma;
115 
116 	obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_VOLATILE |
117 					  I915_BO_ALLOC_PM_VOLATILE);
118 	if (IS_ERR(obj) && i915_ggtt_has_aperture(ggtt))
119 		obj = i915_gem_object_create_stolen(i915, size);
120 	if (IS_ERR(obj))
121 		obj = i915_gem_object_create_internal(i915, size);
122 	if (IS_ERR(obj))
123 		return ERR_CAST(obj);
124 
125 	/*
126 	 * Mark ring buffers as read-only from GPU side (so no stray overwrites)
127 	 * if supported by the platform's GGTT.
128 	 */
129 	if (vm->has_read_only)
130 		i915_gem_object_set_readonly(obj);
131 
132 	vma = i915_vma_instance(obj, vm, NULL);
133 	if (IS_ERR(vma))
134 		goto err;
135 
136 	return vma;
137 
138 err:
139 	i915_gem_object_put(obj);
140 	return vma;
141 }
142 
143 struct intel_ring *
144 intel_engine_create_ring(struct intel_engine_cs *engine, int size)
145 {
146 	struct drm_i915_private *i915 = engine->i915;
147 	struct intel_ring *ring;
148 	struct i915_vma *vma;
149 
150 	GEM_BUG_ON(!is_power_of_2(size));
151 	GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
152 
153 	ring = kzalloc(sizeof(*ring), GFP_KERNEL);
154 	if (!ring)
155 		return ERR_PTR(-ENOMEM);
156 
157 	kref_init(&ring->ref);
158 	ring->size = size;
159 	ring->wrap = BITS_PER_TYPE(ring->size) - ilog2(size);
160 
161 	/*
162 	 * Workaround an erratum on the i830 which causes a hang if
163 	 * the TAIL pointer points to within the last 2 cachelines
164 	 * of the buffer.
165 	 */
166 	ring->effective_size = size;
167 	if (IS_I830(i915) || IS_I845G(i915))
168 		ring->effective_size -= 2 * CACHELINE_BYTES;
169 
170 	intel_ring_update_space(ring);
171 
172 	vma = create_ring_vma(engine->gt->ggtt, size);
173 	if (IS_ERR(vma)) {
174 		kfree(ring);
175 		return ERR_CAST(vma);
176 	}
177 	ring->vma = vma;
178 
179 	return ring;
180 }
181 
182 void intel_ring_free(struct kref *ref)
183 {
184 	struct intel_ring *ring = container_of(ref, typeof(*ring), ref);
185 
186 	i915_vma_put(ring->vma);
187 	kfree(ring);
188 }
189 
190 static noinline int
191 wait_for_space(struct intel_ring *ring,
192 	       struct intel_timeline *tl,
193 	       unsigned int bytes)
194 {
195 	struct i915_request *target;
196 	long timeout;
197 
198 	if (intel_ring_update_space(ring) >= bytes)
199 		return 0;
200 
201 	GEM_BUG_ON(list_empty(&tl->requests));
202 	list_for_each_entry(target, &tl->requests, link) {
203 		if (target->ring != ring)
204 			continue;
205 
206 		/* Would completion of this request free enough space? */
207 		if (bytes <= __intel_ring_space(target->postfix,
208 						ring->emit, ring->size))
209 			break;
210 	}
211 
212 	if (GEM_WARN_ON(&target->link == &tl->requests))
213 		return -ENOSPC;
214 
215 	timeout = i915_request_wait(target,
216 				    I915_WAIT_INTERRUPTIBLE,
217 				    MAX_SCHEDULE_TIMEOUT);
218 	if (timeout < 0)
219 		return timeout;
220 
221 	i915_request_retire_upto(target);
222 
223 	intel_ring_update_space(ring);
224 	GEM_BUG_ON(ring->space < bytes);
225 	return 0;
226 }
227 
228 u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
229 {
230 	struct intel_ring *ring = rq->ring;
231 	const unsigned int remain_usable = ring->effective_size - ring->emit;
232 	const unsigned int bytes = num_dwords * sizeof(u32);
233 	unsigned int need_wrap = 0;
234 	unsigned int total_bytes;
235 	u32 *cs;
236 
237 	/* Packets must be qword aligned. */
238 	GEM_BUG_ON(num_dwords & 1);
239 
240 	total_bytes = bytes + rq->reserved_space;
241 	GEM_BUG_ON(total_bytes > ring->effective_size);
242 
243 	if (unlikely(total_bytes > remain_usable)) {
244 		const int remain_actual = ring->size - ring->emit;
245 
246 		if (bytes > remain_usable) {
247 			/*
248 			 * Not enough space for the basic request. So need to
249 			 * flush out the remainder and then wait for
250 			 * base + reserved.
251 			 */
252 			total_bytes += remain_actual;
253 			need_wrap = remain_actual | 1;
254 		} else  {
255 			/*
256 			 * The base request will fit but the reserved space
257 			 * falls off the end. So we don't need an immediate
258 			 * wrap and only need to effectively wait for the
259 			 * reserved size from the start of ringbuffer.
260 			 */
261 			total_bytes = rq->reserved_space + remain_actual;
262 		}
263 	}
264 
265 	if (unlikely(total_bytes > ring->space)) {
266 		int ret;
267 
268 		/*
269 		 * Space is reserved in the ringbuffer for finalising the
270 		 * request, as that cannot be allowed to fail. During request
271 		 * finalisation, reserved_space is set to 0 to stop the
272 		 * overallocation and the assumption is that then we never need
273 		 * to wait (which has the risk of failing with EINTR).
274 		 *
275 		 * See also i915_request_alloc() and i915_request_add().
276 		 */
277 		GEM_BUG_ON(!rq->reserved_space);
278 
279 		ret = wait_for_space(ring,
280 				     i915_request_timeline(rq),
281 				     total_bytes);
282 		if (unlikely(ret))
283 			return ERR_PTR(ret);
284 	}
285 
286 	if (unlikely(need_wrap)) {
287 		need_wrap &= ~1;
288 		GEM_BUG_ON(need_wrap > ring->space);
289 		GEM_BUG_ON(ring->emit + need_wrap > ring->size);
290 		GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
291 
292 		/* Fill the tail with MI_NOOP */
293 		memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
294 		ring->space -= need_wrap;
295 		ring->emit = 0;
296 	}
297 
298 	GEM_BUG_ON(ring->emit > ring->size - bytes);
299 	GEM_BUG_ON(ring->space < bytes);
300 	cs = ring->vaddr + ring->emit;
301 	GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
302 	ring->emit += bytes;
303 	ring->space -= bytes;
304 
305 	return cs;
306 }
307 
308 /* Align the ring tail to a cacheline boundary */
309 int intel_ring_cacheline_align(struct i915_request *rq)
310 {
311 	int num_dwords;
312 	void *cs;
313 
314 	num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
315 	if (num_dwords == 0)
316 		return 0;
317 
318 	num_dwords = CACHELINE_DWORDS - num_dwords;
319 	GEM_BUG_ON(num_dwords & 1);
320 
321 	cs = intel_ring_begin(rq, num_dwords);
322 	if (IS_ERR(cs))
323 		return PTR_ERR(cs);
324 
325 	memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
326 	intel_ring_advance(rq, cs + num_dwords);
327 
328 	GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
329 	return 0;
330 }
331 
332 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
333 #include "selftest_ring.c"
334 #endif
335