1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include "gem/i915_gem_internal.h"
7 #include "gem/i915_gem_lmem.h"
8 #include "gem/i915_gem_object.h"
12 #include "intel_engine.h"
13 #include "intel_engine_regs.h"
14 #include "intel_gpu_commands.h"
15 #include "intel_ring.h"
17 #include "intel_timeline.h"
19 unsigned int intel_ring_update_space(struct intel_ring *ring)
23 space = __intel_ring_space(ring->head, ring->emit, ring->size);
29 void __intel_ring_pin(struct intel_ring *ring)
31 GEM_BUG_ON(!atomic_read(&ring->pin_count));
32 atomic_inc(&ring->pin_count);
35 int intel_ring_pin(struct intel_ring *ring, struct i915_gem_ww_ctx *ww)
37 struct i915_vma *vma = ring->vma;
42 if (atomic_fetch_inc(&ring->pin_count))
45 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
46 flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
48 if (i915_gem_object_is_stolen(vma->obj))
49 flags |= PIN_MAPPABLE;
53 ret = i915_ggtt_pin(vma, ww, 0, flags);
57 if (i915_vma_is_map_and_fenceable(vma) && !HAS_LLC(vma->vm->i915)) {
58 addr = (void __force *)i915_vma_pin_iomap(vma);
60 int type = intel_gt_coherent_map_type(vma->vm->gt, vma->obj, false);
62 addr = i915_gem_object_pin_map(vma->obj, type);
70 i915_vma_make_unshrinkable(vma);
72 /* Discard any unused bytes beyond that submitted to hw. */
73 intel_ring_reset(ring, ring->emit);
81 atomic_dec(&ring->pin_count);
85 void intel_ring_reset(struct intel_ring *ring, u32 tail)
87 tail = intel_ring_wrap(ring, tail);
91 intel_ring_update_space(ring);
94 void intel_ring_unpin(struct intel_ring *ring)
96 struct i915_vma *vma = ring->vma;
98 if (!atomic_dec_and_test(&ring->pin_count))
101 i915_vma_unset_ggtt_write(vma);
102 if (i915_vma_is_map_and_fenceable(vma) && !HAS_LLC(vma->vm->i915))
103 i915_vma_unpin_iomap(vma);
105 i915_gem_object_unpin_map(vma->obj);
107 i915_vma_make_purgeable(vma);
111 static struct i915_vma *create_ring_vma(struct i915_ggtt *ggtt, int size)
113 struct i915_address_space *vm = &ggtt->vm;
114 struct drm_i915_private *i915 = vm->i915;
115 struct drm_i915_gem_object *obj;
116 struct i915_vma *vma;
118 obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_VOLATILE |
119 I915_BO_ALLOC_PM_VOLATILE);
120 if (IS_ERR(obj) && i915_ggtt_has_aperture(ggtt) && !HAS_LLC(i915))
121 obj = i915_gem_object_create_stolen(i915, size);
123 obj = i915_gem_object_create_internal(i915, size);
125 return ERR_CAST(obj);
128 * Mark ring buffers as read-only from GPU side (so no stray overwrites)
129 * if supported by the platform's GGTT.
131 if (vm->has_read_only)
132 i915_gem_object_set_readonly(obj);
134 vma = i915_vma_instance(obj, vm, NULL);
141 i915_gem_object_put(obj);
146 intel_engine_create_ring(struct intel_engine_cs *engine, int size)
148 struct drm_i915_private *i915 = engine->i915;
149 struct intel_ring *ring;
150 struct i915_vma *vma;
152 GEM_BUG_ON(!is_power_of_2(size));
153 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
155 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
157 return ERR_PTR(-ENOMEM);
159 kref_init(&ring->ref);
161 ring->wrap = BITS_PER_TYPE(ring->size) - ilog2(size);
164 * Workaround an erratum on the i830 which causes a hang if
165 * the TAIL pointer points to within the last 2 cachelines
168 ring->effective_size = size;
169 if (IS_I830(i915) || IS_I845G(i915))
170 ring->effective_size -= 2 * CACHELINE_BYTES;
172 intel_ring_update_space(ring);
174 vma = create_ring_vma(engine->gt->ggtt, size);
177 return ERR_CAST(vma);
184 void intel_ring_free(struct kref *ref)
186 struct intel_ring *ring = container_of(ref, typeof(*ring), ref);
188 i915_vma_put(ring->vma);
193 wait_for_space(struct intel_ring *ring,
194 struct intel_timeline *tl,
197 struct i915_request *target;
200 if (intel_ring_update_space(ring) >= bytes)
203 GEM_BUG_ON(list_empty(&tl->requests));
204 list_for_each_entry(target, &tl->requests, link) {
205 if (target->ring != ring)
208 /* Would completion of this request free enough space? */
209 if (bytes <= __intel_ring_space(target->postfix,
210 ring->emit, ring->size))
214 if (GEM_WARN_ON(&target->link == &tl->requests))
217 timeout = i915_request_wait(target,
218 I915_WAIT_INTERRUPTIBLE,
219 MAX_SCHEDULE_TIMEOUT);
223 i915_request_retire_upto(target);
225 intel_ring_update_space(ring);
226 GEM_BUG_ON(ring->space < bytes);
230 u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
232 struct intel_ring *ring = rq->ring;
233 const unsigned int remain_usable = ring->effective_size - ring->emit;
234 const unsigned int bytes = num_dwords * sizeof(u32);
235 unsigned int need_wrap = 0;
236 unsigned int total_bytes;
239 /* Packets must be qword aligned. */
240 GEM_BUG_ON(num_dwords & 1);
242 total_bytes = bytes + rq->reserved_space;
243 GEM_BUG_ON(total_bytes > ring->effective_size);
245 if (unlikely(total_bytes > remain_usable)) {
246 const int remain_actual = ring->size - ring->emit;
248 if (bytes > remain_usable) {
250 * Not enough space for the basic request. So need to
251 * flush out the remainder and then wait for
254 total_bytes += remain_actual;
255 need_wrap = remain_actual | 1;
258 * The base request will fit but the reserved space
259 * falls off the end. So we don't need an immediate
260 * wrap and only need to effectively wait for the
261 * reserved size from the start of ringbuffer.
263 total_bytes = rq->reserved_space + remain_actual;
267 if (unlikely(total_bytes > ring->space)) {
271 * Space is reserved in the ringbuffer for finalising the
272 * request, as that cannot be allowed to fail. During request
273 * finalisation, reserved_space is set to 0 to stop the
274 * overallocation and the assumption is that then we never need
275 * to wait (which has the risk of failing with EINTR).
277 * See also i915_request_alloc() and i915_request_add().
279 GEM_BUG_ON(!rq->reserved_space);
281 ret = wait_for_space(ring,
282 i915_request_timeline(rq),
288 if (unlikely(need_wrap)) {
290 GEM_BUG_ON(need_wrap > ring->space);
291 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
292 GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
294 /* Fill the tail with MI_NOOP */
295 memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
296 ring->space -= need_wrap;
300 GEM_BUG_ON(ring->emit > ring->size - bytes);
301 GEM_BUG_ON(ring->space < bytes);
302 cs = ring->vaddr + ring->emit;
303 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
304 memset32(cs, POISON_INUSE, bytes / sizeof(*cs));
306 ring->space -= bytes;
311 /* Align the ring tail to a cacheline boundary */
312 int intel_ring_cacheline_align(struct i915_request *rq)
317 num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
321 num_dwords = CACHELINE_DWORDS - num_dwords;
322 GEM_BUG_ON(num_dwords & 1);
324 cs = intel_ring_begin(rq, num_dwords);
328 memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
329 intel_ring_advance(rq, cs + num_dwords);
331 GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
335 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
336 #include "selftest_ring.c"