2 * Copyright © 2017 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include <linux/slab.h>
27 #include "i915_syncmap.h"
29 #include "i915_gem.h" /* GEM_BUG_ON() */
30 #include "i915_selftest.h"
32 #define SHIFT ilog2(KSYNCMAP)
33 #define MASK (KSYNCMAP - 1)
36 * struct i915_syncmap is a layer of a radixtree that maps a u64 fence
37 * context id to the last u32 fence seqno waited upon from that context.
38 * Unlike lib/radixtree it uses a parent pointer that allows traversal back to
39 * the root. This allows us to access the whole tree via a single pointer
40 * to the most recently used layer. We expect fence contexts to be dense
41 * and most reuse to be on the same i915_gem_context but on neighbouring
42 * engines (i.e. on adjacent contexts) and reuse the same leaf, a very
43 * effective lookup cache. If the new lookup is not on the same leaf, we
44 * expect it to be on the neighbouring branch.
46 * A leaf holds an array of u32 seqno, and has height 0. The bitmap field
47 * allows us to store whether a particular seqno is valid (i.e. allows us
48 * to distinguish unset from 0).
50 * A branch holds an array of layer pointers, and has height > 0, and always
51 * has at least 2 layers (either branches or leaves) below it.
55 * 0 1 2 0x10 0x11 0x200 0x201
56 * 0x500000 0x500001 0x503000 0x503001
58 * i915_syncmap_set(&sync, x, lower_32_bits(x));
59 * will build a tree like:
61 * 0-> 0x0000000000XXXXXX
62 * | 0-> 0x0000000000000XXX
63 * | | 0-> 0x00000000000000XX
64 * | | | 0-> 0x000000000000000X 0:0, 1:1, 2:2
65 * | | | 1-> 0x000000000000001X 0:10, 1:11
66 * | | 2-> 0x000000000000020X 0:200, 1:201
67 * | 5-> 0x000000000050XXXX
68 * | 0-> 0x000000000050000X 0:500000, 1:500001
69 * | 3-> 0x000000000050300X 0:503000, 1:503001
70 * e-> 0xe00000000000000X e:e
77 struct i915_syncmap *parent;
79 DECLARE_FLEX_ARRAY(u32, seqno);
80 DECLARE_FLEX_ARRAY(struct i915_syncmap *, child);
85 * i915_syncmap_init -- initialise the #i915_syncmap
86 * @root: pointer to the #i915_syncmap
88 void i915_syncmap_init(struct i915_syncmap **root)
90 BUILD_BUG_ON_NOT_POWER_OF_2(KSYNCMAP);
91 BUILD_BUG_ON_NOT_POWER_OF_2(SHIFT);
92 BUILD_BUG_ON(KSYNCMAP > BITS_PER_TYPE((*root)->bitmap));
96 static inline u32 *__sync_seqno(struct i915_syncmap *p)
98 GEM_BUG_ON(p->height);
102 static inline struct i915_syncmap **__sync_child(struct i915_syncmap *p)
104 GEM_BUG_ON(!p->height);
108 static inline unsigned int
109 __sync_branch_idx(const struct i915_syncmap *p, u64 id)
111 return (id >> p->height) & MASK;
114 static inline unsigned int
115 __sync_leaf_idx(const struct i915_syncmap *p, u64 id)
117 GEM_BUG_ON(p->height);
121 static inline u64 __sync_branch_prefix(const struct i915_syncmap *p, u64 id)
123 return id >> p->height >> SHIFT;
126 static inline u64 __sync_leaf_prefix(const struct i915_syncmap *p, u64 id)
128 GEM_BUG_ON(p->height);
132 static inline bool seqno_later(u32 a, u32 b)
134 return (s32)(a - b) >= 0;
138 * i915_syncmap_is_later -- compare against the last know sync point
139 * @root: pointer to the #i915_syncmap
140 * @id: the context id (other timeline) we are synchronising to
141 * @seqno: the sequence number along the other timeline
143 * If we have already synchronised this @root timeline with another (@id) then
144 * we can omit any repeated or earlier synchronisation requests. If the two
145 * timelines are already coupled, we can also omit the dependency between the
146 * two as that is already known via the timeline.
148 * Returns true if the two timelines are already synchronised wrt to @seqno,
149 * false if not and the synchronisation must be emitted.
151 bool i915_syncmap_is_later(struct i915_syncmap **root, u64 id, u32 seqno)
153 struct i915_syncmap *p;
160 if (likely(__sync_leaf_prefix(p, id) == p->prefix))
163 /* First climb the tree back to a parent branch */
169 if (__sync_branch_prefix(p, id) == p->prefix)
173 /* And then descend again until we find our leaf */
178 p = __sync_child(p)[__sync_branch_idx(p, id)];
182 if (__sync_branch_prefix(p, id) != p->prefix)
188 idx = __sync_leaf_idx(p, id);
189 if (!(p->bitmap & BIT(idx)))
192 return seqno_later(__sync_seqno(p)[idx], seqno);
195 static struct i915_syncmap *
196 __sync_alloc_leaf(struct i915_syncmap *parent, u64 id)
198 struct i915_syncmap *p;
200 p = kmalloc(struct_size(p, seqno, KSYNCMAP), GFP_KERNEL);
207 p->prefix = __sync_leaf_prefix(p, id);
211 static inline void __sync_set_seqno(struct i915_syncmap *p, u64 id, u32 seqno)
213 unsigned int idx = __sync_leaf_idx(p, id);
215 p->bitmap |= BIT(idx);
216 __sync_seqno(p)[idx] = seqno;
219 static inline void __sync_set_child(struct i915_syncmap *p,
221 struct i915_syncmap *child)
223 p->bitmap |= BIT(idx);
224 __sync_child(p)[idx] = child;
227 static noinline int __sync_set(struct i915_syncmap **root, u64 id, u32 seqno)
229 struct i915_syncmap *p = *root;
233 p = __sync_alloc_leaf(NULL, id);
240 /* Caller handled the likely cached case */
241 GEM_BUG_ON(__sync_leaf_prefix(p, id) == p->prefix);
243 /* Climb back up the tree until we find a common prefix */
250 if (__sync_branch_prefix(p, id) == p->prefix)
255 * No shortcut, we have to descend the tree to find the right layer
256 * containing this fence.
258 * Each layer in the tree holds 16 (KSYNCMAP) pointers, either fences
259 * or lower layers. Leaf nodes (height = 0) contain the fences, all
260 * other nodes (height > 0) are internal layers that point to a lower
261 * node. Each internal layer has at least 2 descendents.
263 * Starting at the top, we check whether the current prefix matches. If
264 * it doesn't, we have gone past our target and need to insert a join
265 * into the tree, and a new leaf node for the target as a descendent
266 * of the join, as well as the original layer.
268 * The matching prefix means we are still following the right branch
269 * of the tree. If it has height 0, we have found our leaf and just
270 * need to replace the fence slot with ourselves. If the height is
271 * not zero, our slot contains the next layer in the tree (unless
272 * it is empty, in which case we can add ourselves as a new leaf).
273 * As descend the tree the prefix grows (and height decreases).
276 struct i915_syncmap *next;
278 if (__sync_branch_prefix(p, id) != p->prefix) {
281 /* Insert a join above the current layer */
282 next = kzalloc(struct_size(next, child, KSYNCMAP),
287 /* Compute the height at which these two diverge */
288 above = fls64(__sync_branch_prefix(p, id) ^ p->prefix);
289 above = round_up(above, SHIFT);
290 next->height = above + p->height;
291 next->prefix = __sync_branch_prefix(next, id);
293 /* Insert the join into the parent */
295 idx = __sync_branch_idx(p->parent, id);
296 __sync_child(p->parent)[idx] = next;
297 GEM_BUG_ON(!(p->parent->bitmap & BIT(idx)));
299 next->parent = p->parent;
301 /* Compute the idx of the other branch, not our id! */
302 idx = p->prefix >> (above - SHIFT) & MASK;
303 __sync_set_child(next, idx, p);
306 /* Ascend to the join */
313 /* Descend into the next layer */
314 GEM_BUG_ON(!p->height);
315 idx = __sync_branch_idx(p, id);
316 next = __sync_child(p)[idx];
318 next = __sync_alloc_leaf(p, id);
322 __sync_set_child(p, idx, next);
331 GEM_BUG_ON(p->prefix != __sync_leaf_prefix(p, id));
332 __sync_set_seqno(p, id, seqno);
338 * i915_syncmap_set -- mark the most recent syncpoint between contexts
339 * @root: pointer to the #i915_syncmap
340 * @id: the context id (other timeline) we have synchronised to
341 * @seqno: the sequence number along the other timeline
343 * When we synchronise this @root timeline with another (@id), we also know
344 * that we have synchronized with all previous seqno along that timeline. If
345 * we then have a request to synchronise with the same seqno or older, we can
346 * omit it, see i915_syncmap_is_later()
348 * Returns 0 on success, or a negative error code.
350 int i915_syncmap_set(struct i915_syncmap **root, u64 id, u32 seqno)
352 struct i915_syncmap *p = *root;
355 * We expect to be called in sequence following is_later(id), which
356 * should have preloaded the root for us.
358 if (likely(p && __sync_leaf_prefix(p, id) == p->prefix)) {
359 __sync_set_seqno(p, id, seqno);
363 return __sync_set(root, id, seqno);
366 static void __sync_free(struct i915_syncmap *p)
371 while ((i = ffs(p->bitmap))) {
372 p->bitmap &= ~0u << i;
373 __sync_free(__sync_child(p)[i - 1]);
381 * i915_syncmap_free -- free all memory associated with the syncmap
382 * @root: pointer to the #i915_syncmap
384 * Either when the timeline is to be freed and we no longer need the sync
385 * point tracking, or when the fences are all known to be signaled and the
386 * sync point tracking is redundant, we can free the #i915_syncmap to recover
389 * Will reinitialise the @root pointer so that the #i915_syncmap is ready for
392 void i915_syncmap_free(struct i915_syncmap **root)
394 struct i915_syncmap *p;
407 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
408 #include "selftests/i915_syncmap.c"