2 * SPDX-License-Identifier: MIT
4 * Copyright © 2014-2018 Intel Corporation
7 #include "gem/i915_gem_object.h"
10 #include "intel_engine_pm.h"
11 #include "intel_engine_pool.h"
13 static struct intel_engine_cs *to_engine(struct intel_engine_pool *pool)
15 return container_of(pool, struct intel_engine_cs, pool);
18 static struct list_head *
19 bucket_for_size(struct intel_engine_pool *pool, size_t sz)
24 * Compute a power-of-two bucket, but throw everything greater than
25 * 16KiB into the same bucket: i.e. the buckets hold objects of
26 * (1 page, 2 pages, 4 pages, 8+ pages).
28 n = fls(sz >> PAGE_SHIFT) - 1;
29 if (n >= ARRAY_SIZE(pool->cache_list))
30 n = ARRAY_SIZE(pool->cache_list) - 1;
32 return &pool->cache_list[n];
35 static void node_free(struct intel_engine_pool_node *node)
37 i915_gem_object_put(node->obj);
38 i915_active_fini(&node->active);
42 static int pool_active(struct i915_active *ref)
44 struct intel_engine_pool_node *node =
45 container_of(ref, typeof(*node), active);
46 struct reservation_object *resv = node->obj->base.resv;
49 if (reservation_object_trylock(resv)) {
50 reservation_object_add_excl_fence(resv, NULL);
51 reservation_object_unlock(resv);
54 err = i915_gem_object_pin_pages(node->obj);
58 /* Hide this pinned object from the shrinker until retired */
59 i915_gem_object_make_unshrinkable(node->obj);
64 static void pool_retire(struct i915_active *ref)
66 struct intel_engine_pool_node *node =
67 container_of(ref, typeof(*node), active);
68 struct intel_engine_pool *pool = node->pool;
69 struct list_head *list = bucket_for_size(pool, node->obj->base.size);
72 GEM_BUG_ON(!intel_engine_pm_is_awake(to_engine(pool)));
74 i915_gem_object_unpin_pages(node->obj);
76 /* Return this object to the shrinker pool */
77 i915_gem_object_make_purgeable(node->obj);
79 spin_lock_irqsave(&pool->lock, flags);
80 list_add(&node->link, list);
81 spin_unlock_irqrestore(&pool->lock, flags);
84 static struct intel_engine_pool_node *
85 node_create(struct intel_engine_pool *pool, size_t sz)
87 struct intel_engine_cs *engine = to_engine(pool);
88 struct intel_engine_pool_node *node;
89 struct drm_i915_gem_object *obj;
91 node = kmalloc(sizeof(*node),
92 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
94 return ERR_PTR(-ENOMEM);
97 i915_active_init(engine->i915, &node->active, pool_active, pool_retire);
99 obj = i915_gem_object_create_internal(engine->i915, sz);
101 i915_active_fini(&node->active);
103 return ERR_CAST(obj);
110 struct intel_engine_pool_node *
111 intel_engine_pool_get(struct intel_engine_pool *pool, size_t size)
113 struct intel_engine_pool_node *node;
114 struct list_head *list;
118 GEM_BUG_ON(!intel_engine_pm_is_awake(to_engine(pool)));
120 size = PAGE_ALIGN(size);
121 list = bucket_for_size(pool, size);
123 spin_lock_irqsave(&pool->lock, flags);
124 list_for_each_entry(node, list, link) {
125 if (node->obj->base.size < size)
127 list_del(&node->link);
130 spin_unlock_irqrestore(&pool->lock, flags);
132 if (&node->link == list) {
133 node = node_create(pool, size);
138 ret = i915_active_acquire(&node->active);
147 void intel_engine_pool_init(struct intel_engine_pool *pool)
151 spin_lock_init(&pool->lock);
152 for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++)
153 INIT_LIST_HEAD(&pool->cache_list[n]);
156 void intel_engine_pool_park(struct intel_engine_pool *pool)
160 for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++) {
161 struct list_head *list = &pool->cache_list[n];
162 struct intel_engine_pool_node *node, *nn;
164 list_for_each_entry_safe(node, nn, list, link)
167 INIT_LIST_HEAD(list);
171 void intel_engine_pool_fini(struct intel_engine_pool *pool)
175 for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++)
176 GEM_BUG_ON(!list_empty(&pool->cache_list[n]));