4cd54c569911
[openwrt/staging/blogic.git] /
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2014-2018 Intel Corporation
5 */
6
7 #include "gem/i915_gem_object.h"
8
9 #include "i915_drv.h"
10 #include "intel_engine_pm.h"
11 #include "intel_engine_pool.h"
12
13 static struct intel_engine_cs *to_engine(struct intel_engine_pool *pool)
14 {
15 return container_of(pool, struct intel_engine_cs, pool);
16 }
17
18 static struct list_head *
19 bucket_for_size(struct intel_engine_pool *pool, size_t sz)
20 {
21 int n;
22
23 /*
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).
27 */
28 n = fls(sz >> PAGE_SHIFT) - 1;
29 if (n >= ARRAY_SIZE(pool->cache_list))
30 n = ARRAY_SIZE(pool->cache_list) - 1;
31
32 return &pool->cache_list[n];
33 }
34
35 static void node_free(struct intel_engine_pool_node *node)
36 {
37 i915_gem_object_put(node->obj);
38 i915_active_fini(&node->active);
39 kfree(node);
40 }
41
42 static int pool_active(struct i915_active *ref)
43 {
44 struct intel_engine_pool_node *node =
45 container_of(ref, typeof(*node), active);
46 struct dma_resv *resv = node->obj->base.resv;
47 int err;
48
49 if (dma_resv_trylock(resv)) {
50 dma_resv_add_excl_fence(resv, NULL);
51 dma_resv_unlock(resv);
52 }
53
54 err = i915_gem_object_pin_pages(node->obj);
55 if (err)
56 return err;
57
58 /* Hide this pinned object from the shrinker until retired */
59 i915_gem_object_make_unshrinkable(node->obj);
60
61 return 0;
62 }
63
64 static void pool_retire(struct i915_active *ref)
65 {
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);
70 unsigned long flags;
71
72 GEM_BUG_ON(!intel_engine_pm_is_awake(to_engine(pool)));
73
74 i915_gem_object_unpin_pages(node->obj);
75
76 /* Return this object to the shrinker pool */
77 i915_gem_object_make_purgeable(node->obj);
78
79 spin_lock_irqsave(&pool->lock, flags);
80 list_add(&node->link, list);
81 spin_unlock_irqrestore(&pool->lock, flags);
82 }
83
84 static struct intel_engine_pool_node *
85 node_create(struct intel_engine_pool *pool, size_t sz)
86 {
87 struct intel_engine_cs *engine = to_engine(pool);
88 struct intel_engine_pool_node *node;
89 struct drm_i915_gem_object *obj;
90
91 node = kmalloc(sizeof(*node),
92 GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
93 if (!node)
94 return ERR_PTR(-ENOMEM);
95
96 node->pool = pool;
97 i915_active_init(engine->i915, &node->active, pool_active, pool_retire);
98
99 obj = i915_gem_object_create_internal(engine->i915, sz);
100 if (IS_ERR(obj)) {
101 i915_active_fini(&node->active);
102 kfree(node);
103 return ERR_CAST(obj);
104 }
105
106 node->obj = obj;
107 return node;
108 }
109
110 struct intel_engine_pool_node *
111 intel_engine_pool_get(struct intel_engine_pool *pool, size_t size)
112 {
113 struct intel_engine_pool_node *node;
114 struct list_head *list;
115 unsigned long flags;
116 int ret;
117
118 GEM_BUG_ON(!intel_engine_pm_is_awake(to_engine(pool)));
119
120 size = PAGE_ALIGN(size);
121 list = bucket_for_size(pool, size);
122
123 spin_lock_irqsave(&pool->lock, flags);
124 list_for_each_entry(node, list, link) {
125 if (node->obj->base.size < size)
126 continue;
127 list_del(&node->link);
128 break;
129 }
130 spin_unlock_irqrestore(&pool->lock, flags);
131
132 if (&node->link == list) {
133 node = node_create(pool, size);
134 if (IS_ERR(node))
135 return node;
136 }
137
138 ret = i915_active_acquire(&node->active);
139 if (ret) {
140 node_free(node);
141 return ERR_PTR(ret);
142 }
143
144 return node;
145 }
146
147 void intel_engine_pool_init(struct intel_engine_pool *pool)
148 {
149 int n;
150
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]);
154 }
155
156 void intel_engine_pool_park(struct intel_engine_pool *pool)
157 {
158 int n;
159
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;
163
164 list_for_each_entry_safe(node, nn, list, link)
165 node_free(node);
166
167 INIT_LIST_HEAD(list);
168 }
169 }
170
171 void intel_engine_pool_fini(struct intel_engine_pool *pool)
172 {
173 int n;
174
175 for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++)
176 GEM_BUG_ON(!list_empty(&pool->cache_list[n]));
177 }