blob: ed989596d9a38249c87ac223c71285abb5d512f5 [file] [log] [blame]
Brad Volkin493018d2014-12-11 12:13:08 -08001/*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 */
24
25#include "i915_drv.h"
Chris Wilsoned9ddd22015-04-07 16:20:34 +010026#include "i915_gem_batch_pool.h"
Brad Volkin493018d2014-12-11 12:13:08 -080027
28/**
29 * DOC: batch pool
30 *
31 * In order to submit batch buffers as 'secure', the software command parser
32 * must ensure that a batch buffer cannot be modified after parsing. It does
33 * this by copying the user provided batch buffer contents to a kernel owned
34 * buffer from which the hardware will actually execute, and by carefully
35 * managing the address space bindings for such buffers.
36 *
37 * The batch pool framework provides a mechanism for the driver to manage a
38 * set of scratch buffers to use for this purpose. The framework can be
39 * extended to support other uses cases should they arise.
40 */
41
42/**
43 * i915_gem_batch_pool_init() - initialize a batch buffer pool
Chris Wilson115003e92016-08-04 16:32:19 +010044 * @engine: the associated request submission engine
Brad Volkin493018d2014-12-11 12:13:08 -080045 * @pool: the batch buffer pool
46 */
Chris Wilson115003e92016-08-04 16:32:19 +010047void i915_gem_batch_pool_init(struct intel_engine_cs *engine,
Brad Volkin493018d2014-12-11 12:13:08 -080048 struct i915_gem_batch_pool *pool)
49{
Chris Wilson8d9d5742015-04-07 16:20:38 +010050 int n;
51
Chris Wilson115003e92016-08-04 16:32:19 +010052 pool->engine = engine;
Chris Wilson8d9d5742015-04-07 16:20:38 +010053
54 for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++)
55 INIT_LIST_HEAD(&pool->cache_list[n]);
Brad Volkin493018d2014-12-11 12:13:08 -080056}
57
58/**
59 * i915_gem_batch_pool_fini() - clean up a batch buffer pool
60 * @pool: the pool to clean up
61 *
62 * Note: Callers must hold the struct_mutex.
63 */
64void i915_gem_batch_pool_fini(struct i915_gem_batch_pool *pool)
65{
Chris Wilson8d9d5742015-04-07 16:20:38 +010066 int n;
67
Chris Wilson115003e92016-08-04 16:32:19 +010068 lockdep_assert_held(&pool->engine->i915->drm.struct_mutex);
Brad Volkin493018d2014-12-11 12:13:08 -080069
Chris Wilson8d9d5742015-04-07 16:20:38 +010070 for (n = 0; n < ARRAY_SIZE(pool->cache_list); n++) {
Chris Wilsonf67cbce2016-07-26 12:01:53 +010071 struct drm_i915_gem_object *obj, *next;
Brad Volkin493018d2014-12-11 12:13:08 -080072
Chris Wilsonf67cbce2016-07-26 12:01:53 +010073 list_for_each_entry_safe(obj, next,
74 &pool->cache_list[n],
75 batch_pool_link)
Chris Wilsonf8c417c2016-07-20 13:31:53 +010076 i915_gem_object_put(obj);
Chris Wilsonf67cbce2016-07-26 12:01:53 +010077
78 INIT_LIST_HEAD(&pool->cache_list[n]);
Brad Volkin493018d2014-12-11 12:13:08 -080079 }
80}
81
82/**
Chris Wilsonde4e7832015-04-07 16:20:35 +010083 * i915_gem_batch_pool_get() - allocate a buffer from the pool
Brad Volkin493018d2014-12-11 12:13:08 -080084 * @pool: the batch buffer pool
85 * @size: the minimum desired size of the returned buffer
86 *
Chris Wilsonde4e7832015-04-07 16:20:35 +010087 * Returns an inactive buffer from @pool with at least @size bytes,
88 * with the pages pinned. The caller must i915_gem_object_unpin_pages()
89 * on the returned object.
Brad Volkin493018d2014-12-11 12:13:08 -080090 *
91 * Note: Callers must hold the struct_mutex
92 *
Chris Wilsonde4e7832015-04-07 16:20:35 +010093 * Return: the buffer object or an error pointer
Brad Volkin493018d2014-12-11 12:13:08 -080094 */
95struct drm_i915_gem_object *
96i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
97 size_t size)
98{
99 struct drm_i915_gem_object *obj = NULL;
100 struct drm_i915_gem_object *tmp, *next;
Chris Wilson8d9d5742015-04-07 16:20:38 +0100101 struct list_head *list;
102 int n;
Brad Volkin493018d2014-12-11 12:13:08 -0800103
Chris Wilson115003e92016-08-04 16:32:19 +0100104 lockdep_assert_held(&pool->engine->i915->drm.struct_mutex);
Brad Volkin493018d2014-12-11 12:13:08 -0800105
Chris Wilson8d9d5742015-04-07 16:20:38 +0100106 /* Compute a power-of-two bucket, but throw everything greater than
107 * 16KiB into the same bucket: i.e. the the buckets hold objects of
108 * (1 page, 2 pages, 4 pages, 8+ pages).
109 */
110 n = fls(size >> PAGE_SHIFT) - 1;
111 if (n >= ARRAY_SIZE(pool->cache_list))
112 n = ARRAY_SIZE(pool->cache_list) - 1;
113 list = &pool->cache_list[n];
114
115 list_for_each_entry_safe(tmp, next, list, batch_pool_link) {
Chris Wilson06fbca72015-04-07 16:20:36 +0100116 /* The batches are strictly LRU ordered */
Chris Wilson115003e92016-08-04 16:32:19 +0100117 if (!i915_gem_active_is_idle(&tmp->last_read[pool->engine->id],
118 &tmp->base.dev->struct_mutex))
Chris Wilson06fbca72015-04-07 16:20:36 +0100119 break;
Brad Volkin493018d2014-12-11 12:13:08 -0800120
121 /* While we're looping, do some clean up */
122 if (tmp->madv == __I915_MADV_PURGED) {
Chris Wilson8d9d5742015-04-07 16:20:38 +0100123 list_del(&tmp->batch_pool_link);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100124 i915_gem_object_put(tmp);
Brad Volkin493018d2014-12-11 12:13:08 -0800125 continue;
126 }
127
Chris Wilson8d9d5742015-04-07 16:20:38 +0100128 if (tmp->base.size >= size) {
Brad Volkin493018d2014-12-11 12:13:08 -0800129 obj = tmp;
130 break;
131 }
132 }
133
Chris Wilsonde4e7832015-04-07 16:20:35 +0100134 if (obj == NULL) {
135 int ret;
136
Chris Wilson115003e92016-08-04 16:32:19 +0100137 obj = i915_gem_object_create(&pool->engine->i915->drm, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100138 if (IS_ERR(obj))
139 return obj;
Brad Volkin493018d2014-12-11 12:13:08 -0800140
Chris Wilsonde4e7832015-04-07 16:20:35 +0100141 ret = i915_gem_object_get_pages(obj);
142 if (ret)
143 return ERR_PTR(ret);
144
145 obj->madv = I915_MADV_DONTNEED;
Brad Volkin493018d2014-12-11 12:13:08 -0800146 }
Brad Volkin493018d2014-12-11 12:13:08 -0800147
Chris Wilson8d9d5742015-04-07 16:20:38 +0100148 list_move_tail(&obj->batch_pool_link, list);
Chris Wilsonde4e7832015-04-07 16:20:35 +0100149 i915_gem_object_pin_pages(obj);
Brad Volkin493018d2014-12-11 12:13:08 -0800150 return obj;
151}