12c9916cdSFrançois Tigeot /*
22c9916cdSFrançois Tigeot * Copyright © 2014 Intel Corporation
32c9916cdSFrançois Tigeot *
42c9916cdSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
52c9916cdSFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
62c9916cdSFrançois Tigeot * to deal in the Software without restriction, including without limitation
72c9916cdSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
82c9916cdSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
92c9916cdSFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
102c9916cdSFrançois Tigeot *
112c9916cdSFrançois Tigeot * The above copyright notice and this permission notice (including the next
122c9916cdSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the
132c9916cdSFrançois Tigeot * Software.
142c9916cdSFrançois Tigeot *
152c9916cdSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162c9916cdSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172c9916cdSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
182c9916cdSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192c9916cdSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
202c9916cdSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
212c9916cdSFrançois Tigeot * IN THE SOFTWARE.
222c9916cdSFrançois Tigeot *
232c9916cdSFrançois Tigeot */
242c9916cdSFrançois Tigeot
252c9916cdSFrançois Tigeot #include "i915_drv.h"
2619c468b4SFrançois Tigeot #include "i915_gem_batch_pool.h"
272c9916cdSFrançois Tigeot
282c9916cdSFrançois Tigeot /**
292c9916cdSFrançois Tigeot * DOC: batch pool
302c9916cdSFrançois Tigeot *
312c9916cdSFrançois Tigeot * In order to submit batch buffers as 'secure', the software command parser
322c9916cdSFrançois Tigeot * must ensure that a batch buffer cannot be modified after parsing. It does
332c9916cdSFrançois Tigeot * this by copying the user provided batch buffer contents to a kernel owned
342c9916cdSFrançois Tigeot * buffer from which the hardware will actually execute, and by carefully
352c9916cdSFrançois Tigeot * managing the address space bindings for such buffers.
362c9916cdSFrançois Tigeot *
372c9916cdSFrançois Tigeot * The batch pool framework provides a mechanism for the driver to manage a
382c9916cdSFrançois Tigeot * set of scratch buffers to use for this purpose. The framework can be
392c9916cdSFrançois Tigeot * extended to support other uses cases should they arise.
402c9916cdSFrançois Tigeot */
412c9916cdSFrançois Tigeot
422c9916cdSFrançois Tigeot /**
432c9916cdSFrançois Tigeot * i915_gem_batch_pool_init() - initialize a batch buffer pool
4471f41f3eSFrançois Tigeot * @engine: the associated request submission engine
452c9916cdSFrançois Tigeot * @pool: the batch buffer pool
462c9916cdSFrançois Tigeot */
i915_gem_batch_pool_init(struct intel_engine_cs * engine,struct i915_gem_batch_pool * pool)4771f41f3eSFrançois Tigeot void i915_gem_batch_pool_init(struct intel_engine_cs *engine,
482c9916cdSFrançois Tigeot struct i915_gem_batch_pool *pool)
492c9916cdSFrançois Tigeot {
5019c468b4SFrançois Tigeot int n;
5119c468b4SFrançois Tigeot
5271f41f3eSFrançois Tigeot pool->engine = engine;
5319c468b4SFrançois Tigeot
5419c468b4SFrançois Tigeot for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++)
5519c468b4SFrançois Tigeot INIT_LIST_HEAD(&pool->cache_list[n]);
562c9916cdSFrançois Tigeot }
572c9916cdSFrançois Tigeot
582c9916cdSFrançois Tigeot /**
592c9916cdSFrançois Tigeot * i915_gem_batch_pool_fini() - clean up a batch buffer pool
602c9916cdSFrançois Tigeot * @pool: the pool to clean up
612c9916cdSFrançois Tigeot *
622c9916cdSFrançois Tigeot * Note: Callers must hold the struct_mutex.
632c9916cdSFrançois Tigeot */
i915_gem_batch_pool_fini(struct i915_gem_batch_pool * pool)642c9916cdSFrançois Tigeot void i915_gem_batch_pool_fini(struct i915_gem_batch_pool *pool)
652c9916cdSFrançois Tigeot {
6619c468b4SFrançois Tigeot int n;
6719c468b4SFrançois Tigeot
6871f41f3eSFrançois Tigeot lockdep_assert_held(&pool->engine->i915->drm.struct_mutex);
692c9916cdSFrançois Tigeot
7019c468b4SFrançois Tigeot for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++) {
7171f41f3eSFrançois Tigeot struct drm_i915_gem_object *obj, *next;
722c9916cdSFrançois Tigeot
7371f41f3eSFrançois Tigeot list_for_each_entry_safe(obj, next,
7471f41f3eSFrançois Tigeot &pool->cache_list[n],
7571f41f3eSFrançois Tigeot batch_pool_link)
764be47400SFrançois Tigeot __i915_gem_object_release_unless_active(obj);
7771f41f3eSFrançois Tigeot
7871f41f3eSFrançois Tigeot INIT_LIST_HEAD(&pool->cache_list[n]);
792c9916cdSFrançois Tigeot }
8019c468b4SFrançois Tigeot }
812c9916cdSFrançois Tigeot
822c9916cdSFrançois Tigeot /**
8319c468b4SFrançois Tigeot * i915_gem_batch_pool_get() - allocate a buffer from the pool
842c9916cdSFrançois Tigeot * @pool: the batch buffer pool
852c9916cdSFrançois Tigeot * @size: the minimum desired size of the returned buffer
862c9916cdSFrançois Tigeot *
8719c468b4SFrançois Tigeot * Returns an inactive buffer from @pool with at least @size bytes,
8819c468b4SFrançois Tigeot * with the pages pinned. The caller must i915_gem_object_unpin_pages()
8919c468b4SFrançois Tigeot * on the returned object.
902c9916cdSFrançois Tigeot *
912c9916cdSFrançois Tigeot * Note: Callers must hold the struct_mutex
922c9916cdSFrançois Tigeot *
9319c468b4SFrançois Tigeot * Return: the buffer object or an error pointer
942c9916cdSFrançois Tigeot */
952c9916cdSFrançois Tigeot struct drm_i915_gem_object *
i915_gem_batch_pool_get(struct i915_gem_batch_pool * pool,size_t size)962c9916cdSFrançois Tigeot i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
972c9916cdSFrançois Tigeot size_t size)
982c9916cdSFrançois Tigeot {
99a85cb24fSFrançois Tigeot struct drm_i915_gem_object *obj;
10019c468b4SFrançois Tigeot struct list_head *list;
1014be47400SFrançois Tigeot int n, ret;
1022c9916cdSFrançois Tigeot
10371f41f3eSFrançois Tigeot lockdep_assert_held(&pool->engine->i915->drm.struct_mutex);
1042c9916cdSFrançois Tigeot
10519c468b4SFrançois Tigeot /* Compute a power-of-two bucket, but throw everything greater than
10619c468b4SFrançois Tigeot * 16KiB into the same bucket: i.e. the the buckets hold objects of
10719c468b4SFrançois Tigeot * (1 page, 2 pages, 4 pages, 8+ pages).
10819c468b4SFrançois Tigeot */
10919c468b4SFrançois Tigeot n = fls(size >> PAGE_SHIFT) - 1;
11019c468b4SFrançois Tigeot if (n >= ARRAY_SIZE(pool->cache_list))
11119c468b4SFrançois Tigeot n = ARRAY_SIZE(pool->cache_list) - 1;
11219c468b4SFrançois Tigeot list = &pool->cache_list[n];
1132c9916cdSFrançois Tigeot
114a85cb24fSFrançois Tigeot list_for_each_entry(obj, list, batch_pool_link) {
11519c468b4SFrançois Tigeot /* The batches are strictly LRU ordered */
116a85cb24fSFrançois Tigeot if (i915_gem_object_is_active(obj)) {
117*3f2dd94aSFrançois Tigeot struct reservation_object *resv = obj->resv;
118*3f2dd94aSFrançois Tigeot
119*3f2dd94aSFrançois Tigeot if (!reservation_object_test_signaled_rcu(resv, true))
12019c468b4SFrançois Tigeot break;
1212c9916cdSFrançois Tigeot
122a85cb24fSFrançois Tigeot i915_gem_retire_requests(pool->engine->i915);
123a85cb24fSFrançois Tigeot GEM_BUG_ON(i915_gem_object_is_active(obj));
124*3f2dd94aSFrançois Tigeot
125*3f2dd94aSFrançois Tigeot /*
126*3f2dd94aSFrançois Tigeot * The object is now idle, clear the array of shared
127*3f2dd94aSFrançois Tigeot * fences before we add a new request. Although, we
128*3f2dd94aSFrançois Tigeot * remain on the same engine, we may be on a different
129*3f2dd94aSFrançois Tigeot * timeline and so may continually grow the array,
130*3f2dd94aSFrançois Tigeot * trapping a reference to all the old fences, rather
131*3f2dd94aSFrançois Tigeot * than replace the existing fence.
132*3f2dd94aSFrançois Tigeot */
133*3f2dd94aSFrançois Tigeot if (rcu_access_pointer(resv->fence)) {
134*3f2dd94aSFrançois Tigeot reservation_object_lock(resv, NULL);
135*3f2dd94aSFrançois Tigeot reservation_object_add_excl_fence(resv, NULL);
136*3f2dd94aSFrançois Tigeot reservation_object_unlock(resv);
137*3f2dd94aSFrançois Tigeot }
138a85cb24fSFrançois Tigeot }
139a85cb24fSFrançois Tigeot
140a85cb24fSFrançois Tigeot GEM_BUG_ON(!reservation_object_test_signaled_rcu(obj->resv,
1414be47400SFrançois Tigeot true));
1422c9916cdSFrançois Tigeot
143a85cb24fSFrançois Tigeot if (obj->base.size >= size)
144a85cb24fSFrançois Tigeot goto found;
1452c9916cdSFrançois Tigeot }
1462c9916cdSFrançois Tigeot
1474be47400SFrançois Tigeot obj = i915_gem_object_create_internal(pool->engine->i915, size);
1481487f786SFrançois Tigeot if (IS_ERR(obj))
1491487f786SFrançois Tigeot return obj;
1502c9916cdSFrançois Tigeot
151a85cb24fSFrançois Tigeot found:
1524be47400SFrançois Tigeot ret = i915_gem_object_pin_pages(obj);
15319c468b4SFrançois Tigeot if (ret)
15419c468b4SFrançois Tigeot return ERR_PTR(ret);
15519c468b4SFrançois Tigeot
15619c468b4SFrançois Tigeot list_move_tail(&obj->batch_pool_link, list);
1572c9916cdSFrançois Tigeot return obj;
1582c9916cdSFrançois Tigeot }
159