2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #include "gem/i915_gem_object.h"
10 #include "intel_engine.h"
11 #include "intel_ring.h"
12 #include "intel_timeline.h"
14 unsigned int intel_ring_update_space(struct intel_ring *ring)
18 space = __intel_ring_space(ring->head, ring->emit, ring->size);
24 int intel_ring_pin(struct intel_ring *ring)
26 struct i915_vma *vma = ring->vma;
31 if (atomic_fetch_inc(&ring->pin_count))
34 /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
35 flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
38 flags |= PIN_MAPPABLE;
42 ret = i915_ggtt_pin(vma, 0, flags);
46 if (i915_vma_is_map_and_fenceable(vma))
47 addr = (void __force *)i915_vma_pin_iomap(vma);
49 addr = i915_gem_object_pin_map(vma->obj,
50 i915_coherent_map_type(vma->vm->i915));
56 i915_vma_make_unshrinkable(vma);
58 /* Discard any unused bytes beyond that submitted to hw. */
59 intel_ring_reset(ring, ring->emit);
67 atomic_dec(&ring->pin_count);
71 void intel_ring_reset(struct intel_ring *ring, u32 tail)
73 tail = intel_ring_wrap(ring, tail);
77 intel_ring_update_space(ring);
80 void intel_ring_unpin(struct intel_ring *ring)
82 struct i915_vma *vma = ring->vma;
84 if (!atomic_dec_and_test(&ring->pin_count))
87 i915_vma_unset_ggtt_write(vma);
88 if (i915_vma_is_map_and_fenceable(vma))
89 i915_vma_unpin_iomap(vma);
91 i915_gem_object_unpin_map(vma->obj);
93 i915_vma_make_purgeable(vma);
97 static struct i915_vma *create_ring_vma(struct i915_ggtt *ggtt, int size)
99 struct i915_address_space *vm = &ggtt->vm;
100 struct drm_i915_private *i915 = vm->i915;
101 struct drm_i915_gem_object *obj;
102 struct i915_vma *vma;
104 obj = ERR_PTR(-ENODEV);
105 if (i915_ggtt_has_aperture(ggtt))
106 obj = i915_gem_object_create_stolen(i915, size);
108 obj = i915_gem_object_create_internal(i915, size);
110 return ERR_CAST(obj);
113 * Mark ring buffers as read-only from GPU side (so no stray overwrites)
114 * if supported by the platform's GGTT.
116 if (vm->has_read_only)
117 i915_gem_object_set_readonly(obj);
119 vma = i915_vma_instance(obj, vm, NULL);
126 i915_gem_object_put(obj);
131 intel_engine_create_ring(struct intel_engine_cs *engine, int size)
133 struct drm_i915_private *i915 = engine->i915;
134 struct intel_ring *ring;
135 struct i915_vma *vma;
137 GEM_BUG_ON(!is_power_of_2(size));
138 GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
140 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
142 return ERR_PTR(-ENOMEM);
144 kref_init(&ring->ref);
146 ring->wrap = BITS_PER_TYPE(ring->size) - ilog2(size);
149 * Workaround an erratum on the i830 which causes a hang if
150 * the TAIL pointer points to within the last 2 cachelines
153 ring->effective_size = size;
154 if (IS_I830(i915) || IS_I845G(i915))
155 ring->effective_size -= 2 * CACHELINE_BYTES;
157 intel_ring_update_space(ring);
159 vma = create_ring_vma(engine->gt->ggtt, size);
162 return ERR_CAST(vma);
169 void intel_ring_free(struct kref *ref)
171 struct intel_ring *ring = container_of(ref, typeof(*ring), ref);
173 i915_vma_put(ring->vma);
178 wait_for_space(struct intel_ring *ring,
179 struct intel_timeline *tl,
182 struct i915_request *target;
185 if (intel_ring_update_space(ring) >= bytes)
188 GEM_BUG_ON(list_empty(&tl->requests));
189 list_for_each_entry(target, &tl->requests, link) {
190 if (target->ring != ring)
193 /* Would completion of this request free enough space? */
194 if (bytes <= __intel_ring_space(target->postfix,
195 ring->emit, ring->size))
199 if (GEM_WARN_ON(&target->link == &tl->requests))
202 timeout = i915_request_wait(target,
203 I915_WAIT_INTERRUPTIBLE,
204 MAX_SCHEDULE_TIMEOUT);
208 i915_request_retire_upto(target);
210 intel_ring_update_space(ring);
211 GEM_BUG_ON(ring->space < bytes);
215 u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
217 struct intel_ring *ring = rq->ring;
218 const unsigned int remain_usable = ring->effective_size - ring->emit;
219 const unsigned int bytes = num_dwords * sizeof(u32);
220 unsigned int need_wrap = 0;
221 unsigned int total_bytes;
224 /* Packets must be qword aligned. */
225 GEM_BUG_ON(num_dwords & 1);
227 total_bytes = bytes + rq->reserved_space;
228 GEM_BUG_ON(total_bytes > ring->effective_size);
230 if (unlikely(total_bytes > remain_usable)) {
231 const int remain_actual = ring->size - ring->emit;
233 if (bytes > remain_usable) {
235 * Not enough space for the basic request. So need to
236 * flush out the remainder and then wait for
239 total_bytes += remain_actual;
240 need_wrap = remain_actual | 1;
243 * The base request will fit but the reserved space
244 * falls off the end. So we don't need an immediate
245 * wrap and only need to effectively wait for the
246 * reserved size from the start of ringbuffer.
248 total_bytes = rq->reserved_space + remain_actual;
252 if (unlikely(total_bytes > ring->space)) {
256 * Space is reserved in the ringbuffer for finalising the
257 * request, as that cannot be allowed to fail. During request
258 * finalisation, reserved_space is set to 0 to stop the
259 * overallocation and the assumption is that then we never need
260 * to wait (which has the risk of failing with EINTR).
262 * See also i915_request_alloc() and i915_request_add().
264 GEM_BUG_ON(!rq->reserved_space);
266 ret = wait_for_space(ring,
267 i915_request_timeline(rq),
273 if (unlikely(need_wrap)) {
275 GEM_BUG_ON(need_wrap > ring->space);
276 GEM_BUG_ON(ring->emit + need_wrap > ring->size);
277 GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
279 /* Fill the tail with MI_NOOP */
280 memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
281 ring->space -= need_wrap;
285 GEM_BUG_ON(ring->emit > ring->size - bytes);
286 GEM_BUG_ON(ring->space < bytes);
287 cs = ring->vaddr + ring->emit;
288 GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
290 ring->space -= bytes;
295 /* Align the ring tail to a cacheline boundary */
296 int intel_ring_cacheline_align(struct i915_request *rq)
301 num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
305 num_dwords = CACHELINE_DWORDS - num_dwords;
306 GEM_BUG_ON(num_dwords & 1);
308 cs = intel_ring_begin(rq, num_dwords);
312 memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
313 intel_ring_advance(rq, cs + num_dwords);
315 GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));