/* * Copyright © 2017 Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. * */ #include <linux/slab.h> #include "i915_syncmap.h" #include "i915_gem.h" /* GEM_BUG_ON() */ #include "i915_selftest.h" #define SHIFT … #define MASK … /* * struct i915_syncmap is a layer of a radixtree that maps a u64 fence * context id to the last u32 fence seqno waited upon from that context. * Unlike lib/radixtree it uses a parent pointer that allows traversal back to * the root. This allows us to access the whole tree via a single pointer * to the most recently used layer. We expect fence contexts to be dense * and most reuse to be on the same i915_gem_context but on neighbouring * engines (i.e. on adjacent contexts) and reuse the same leaf, a very * effective lookup cache. If the new lookup is not on the same leaf, we * expect it to be on the neighbouring branch. * * A leaf holds an array of u32 seqno, and has height 0. The bitmap field * allows us to store whether a particular seqno is valid (i.e. allows us * to distinguish unset from 0). * * A branch holds an array of layer pointers, and has height > 0, and always * has at least 2 layers (either branches or leaves) below it. * * For example, * for x in * 0 1 2 0x10 0x11 0x200 0x201 * 0x500000 0x500001 0x503000 0x503001 * 0xE<<60: * i915_syncmap_set(&sync, x, lower_32_bits(x)); * will build a tree like: * 0xXXXXXXXXXXXXXXXX * 0-> 0x0000000000XXXXXX * | 0-> 0x0000000000000XXX * | | 0-> 0x00000000000000XX * | | | 0-> 0x000000000000000X 0:0, 1:1, 2:2 * | | | 1-> 0x000000000000001X 0:10, 1:11 * | | 2-> 0x000000000000020X 0:200, 1:201 * | 5-> 0x000000000050XXXX * | 0-> 0x000000000050000X 0:500000, 1:500001 * | 3-> 0x000000000050300X 0:503000, 1:503001 * e-> 0xe00000000000000X e:e */ struct i915_syncmap { … }; /** * i915_syncmap_init -- initialise the #i915_syncmap * @root: pointer to the #i915_syncmap */ void i915_syncmap_init(struct i915_syncmap **root) { … } static inline u32 *__sync_seqno(struct i915_syncmap *p) { … } static inline struct i915_syncmap **__sync_child(struct i915_syncmap *p) { … } static inline unsigned int __sync_branch_idx(const struct i915_syncmap *p, u64 id) { … } static inline unsigned int __sync_leaf_idx(const struct i915_syncmap *p, u64 id) { … } static inline u64 __sync_branch_prefix(const struct i915_syncmap *p, u64 id) { … } static inline u64 __sync_leaf_prefix(const struct i915_syncmap *p, u64 id) { … } static inline bool seqno_later(u32 a, u32 b) { … } /** * i915_syncmap_is_later -- compare against the last know sync point * @root: pointer to the #i915_syncmap * @id: the context id (other timeline) we are synchronising to * @seqno: the sequence number along the other timeline * * If we have already synchronised this @root timeline with another (@id) then * we can omit any repeated or earlier synchronisation requests. If the two * timelines are already coupled, we can also omit the dependency between the * two as that is already known via the timeline. * * Returns true if the two timelines are already synchronised wrt to @seqno, * false if not and the synchronisation must be emitted. */ bool i915_syncmap_is_later(struct i915_syncmap **root, u64 id, u32 seqno) { … } static struct i915_syncmap * __sync_alloc_leaf(struct i915_syncmap *parent, u64 id) { … } static inline void __sync_set_seqno(struct i915_syncmap *p, u64 id, u32 seqno) { … } static inline void __sync_set_child(struct i915_syncmap *p, unsigned int idx, struct i915_syncmap *child) { … } static noinline int __sync_set(struct i915_syncmap **root, u64 id, u32 seqno) { … } /** * i915_syncmap_set -- mark the most recent syncpoint between contexts * @root: pointer to the #i915_syncmap * @id: the context id (other timeline) we have synchronised to * @seqno: the sequence number along the other timeline * * When we synchronise this @root timeline with another (@id), we also know * that we have synchronized with all previous seqno along that timeline. If * we then have a request to synchronise with the same seqno or older, we can * omit it, see i915_syncmap_is_later() * * Returns 0 on success, or a negative error code. */ int i915_syncmap_set(struct i915_syncmap **root, u64 id, u32 seqno) { … } static void __sync_free(struct i915_syncmap *p) { … } /** * i915_syncmap_free -- free all memory associated with the syncmap * @root: pointer to the #i915_syncmap * * Either when the timeline is to be freed and we no longer need the sync * point tracking, or when the fences are all known to be signaled and the * sync point tracking is redundant, we can free the #i915_syncmap to recover * its allocations. * * Will reinitialise the @root pointer so that the #i915_syncmap is ready for * reuse. */ void i915_syncmap_free(struct i915_syncmap **root) { … } #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) #include "selftests/i915_syncmap.c" #endif