Skip to content

Commit 493018d

Browse files
Brad Volkindanvet
authored andcommitted
drm/i915: Implement a framework for batch buffer pools
This adds a small module for managing a pool of batch buffers. The only current use case is for the command parser, as described in the kerneldoc in the patch. The code is simple, but separating it out makes it easier to change the underlying algorithms and to extend to future use cases should they arise. The interface is simple: init to create an empty pool, fini to clean it up, get to obtain a new buffer. Note that all buffers are expected to be inactive before cleaning up the pool. Locking is currently based on the caller holding the struct_mutex. We already do that in the places where we will use the batch pool for the command parser. v2: - s/BUG_ON/WARN_ON/ for locking assertions - Remove the cap on pool size - Switch from alloc/free to init/fini v3: - Idiomatic looping structure in _fini - Correct handling of purged objects - Don't return a buffer that's too much larger than needed v4: - Rebased to latest -nightly v5: - Remove _put() function and clean up comments to match v6: - Move purged check inside the loop (danvet, from v4 1/7 feedback) v7: - Use single list instead of two. (Chris W) - s/active_list/cache_list - Squashed in debug patches (Chris W) drm/i915: Add a batch pool debugfs file It provides some useful information about the buffers in the global command parser batch pool. v2: rebase on global pool instead of per-ring pools v3: rebase drm/i915: Add batch pool details to i915_gem_objects debugfs To better account for the potentially large memory consumption of the batch pool. v8: - Keep cache in LRU order (danvet, from v6 1/5 feedback) Issue: VIZ-4719 Signed-off-by: Brad Volkin <bradley.d.volkin@intel.com> Reviewed-By: Jon Bloomfield <jon.bloomfield@intel.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
1 parent c8bd0e4 commit 493018d

File tree

6 files changed

+225
-9
lines changed

6 files changed

+225
-9
lines changed

Documentation/DocBook/drm.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,6 +4032,11 @@ int num_ioctls;</synopsis>
40324032
<title>Batchbuffer Parsing</title>
40334033
!Pdrivers/gpu/drm/i915/i915_cmd_parser.c batch buffer command parser
40344034
!Idrivers/gpu/drm/i915/i915_cmd_parser.c
4035+
</sect2>
4036+
<sect2>
4037+
<title>Batchbuffer Pools</title>
4038+
!Pdrivers/gpu/drm/i915/i915_gem_batch_pool.c batch pool
4039+
!Idrivers/gpu/drm/i915/i915_gem_batch_pool.c
40354040
</sect2>
40364041
<sect2>
40374042
<title>Logical Rings, Logical Ring Contexts and Execlists</title>

drivers/gpu/drm/i915/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ i915-$(CONFIG_DEBUG_FS) += i915_debugfs.o
1919

2020
# GEM code
2121
i915-y += i915_cmd_parser.o \
22+
i915_gem_batch_pool.o \
2223
i915_gem_context.o \
2324
i915_gem_render_state.o \
2425
i915_gem_debug.o \

drivers/gpu/drm/i915/i915_debugfs.c

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,33 @@ static int per_file_stats(int id, void *ptr, void *data)
360360
return 0;
361361
}
362362

363+
#define print_file_stats(m, name, stats) \
364+
seq_printf(m, "%s: %u objects, %zu bytes (%zu active, %zu inactive, %zu global, %zu shared, %zu unbound)\n", \
365+
name, \
366+
stats.count, \
367+
stats.total, \
368+
stats.active, \
369+
stats.inactive, \
370+
stats.global, \
371+
stats.shared, \
372+
stats.unbound)
373+
374+
static void print_batch_pool_stats(struct seq_file *m,
375+
struct drm_i915_private *dev_priv)
376+
{
377+
struct drm_i915_gem_object *obj;
378+
struct file_stats stats;
379+
380+
memset(&stats, 0, sizeof(stats));
381+
382+
list_for_each_entry(obj,
383+
&dev_priv->mm.batch_pool.cache_list,
384+
batch_pool_list)
385+
per_file_stats(0, obj, &stats);
386+
387+
print_file_stats(m, "batch pool", stats);
388+
}
389+
363390
#define count_vmas(list, member) do { \
364391
list_for_each_entry(vma, list, member) { \
365392
size += i915_gem_obj_ggtt_size(vma->obj); \
@@ -441,6 +468,9 @@ static int i915_gem_object_info(struct seq_file *m, void* data)
441468
dev_priv->gtt.base.total,
442469
dev_priv->gtt.mappable_end - dev_priv->gtt.base.start);
443470

471+
seq_putc(m, '\n');
472+
print_batch_pool_stats(m, dev_priv);
473+
444474
seq_putc(m, '\n');
445475
list_for_each_entry_reverse(file, &dev->filelist, lhead) {
446476
struct file_stats stats;
@@ -459,15 +489,7 @@ static int i915_gem_object_info(struct seq_file *m, void* data)
459489
*/
460490
rcu_read_lock();
461491
task = pid_task(file->pid, PIDTYPE_PID);
462-
seq_printf(m, "%s: %u objects, %zu bytes (%zu active, %zu inactive, %zu global, %zu shared, %zu unbound)\n",
463-
task ? task->comm : "<unknown>",
464-
stats.count,
465-
stats.total,
466-
stats.active,
467-
stats.inactive,
468-
stats.global,
469-
stats.shared,
470-
stats.unbound);
492+
print_file_stats(m, task ? task->comm : "<unknown>", stats);
471493
rcu_read_unlock();
472494
}
473495

@@ -584,6 +606,36 @@ static int i915_gem_pageflip_info(struct seq_file *m, void *data)
584606
return 0;
585607
}
586608

609+
static int i915_gem_batch_pool_info(struct seq_file *m, void *data)
610+
{
611+
struct drm_info_node *node = m->private;
612+
struct drm_device *dev = node->minor->dev;
613+
struct drm_i915_private *dev_priv = dev->dev_private;
614+
struct drm_i915_gem_object *obj;
615+
int count = 0;
616+
int ret;
617+
618+
ret = mutex_lock_interruptible(&dev->struct_mutex);
619+
if (ret)
620+
return ret;
621+
622+
seq_puts(m, "cache:\n");
623+
list_for_each_entry(obj,
624+
&dev_priv->mm.batch_pool.cache_list,
625+
batch_pool_list) {
626+
seq_puts(m, " ");
627+
describe_obj(m, obj);
628+
seq_putc(m, '\n');
629+
count++;
630+
}
631+
632+
seq_printf(m, "total: %d\n", count);
633+
634+
mutex_unlock(&dev->struct_mutex);
635+
636+
return 0;
637+
}
638+
587639
static int i915_gem_request_info(struct seq_file *m, void *data)
588640
{
589641
struct drm_info_node *node = m->private;
@@ -4357,6 +4409,7 @@ static const struct drm_info_list i915_debugfs_list[] = {
43574409
{"i915_gem_hws_blt", i915_hws_info, 0, (void *)BCS},
43584410
{"i915_gem_hws_bsd", i915_hws_info, 0, (void *)VCS},
43594411
{"i915_gem_hws_vebox", i915_hws_info, 0, (void *)VECS},
4412+
{"i915_gem_batch_pool", i915_gem_batch_pool_info, 0},
43604413
{"i915_frequency_info", i915_frequency_info, 0},
43614414
{"i915_drpc_info", i915_drpc_info, 0},
43624415
{"i915_emon_status", i915_emon_status, 0},

drivers/gpu/drm/i915/i915_drv.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,11 @@ struct intel_l3_parity {
11411141
int which_slice;
11421142
};
11431143

1144+
struct i915_gem_batch_pool {
1145+
struct drm_device *dev;
1146+
struct list_head cache_list;
1147+
};
1148+
11441149
struct i915_gem_mm {
11451150
/** Memory allocator for GTT stolen memory */
11461151
struct drm_mm stolen;
@@ -1154,6 +1159,13 @@ struct i915_gem_mm {
11541159
*/
11551160
struct list_head unbound_list;
11561161

1162+
/*
1163+
* A pool of objects to use as shadow copies of client batch buffers
1164+
* when the command parser is enabled. Prevents the client from
1165+
* modifying the batch contents after software parsing.
1166+
*/
1167+
struct i915_gem_batch_pool batch_pool;
1168+
11571169
/** Usable portion of the GTT for GEM */
11581170
unsigned long stolen_base; /* limited to low memory (32-bit) */
11591171

@@ -1885,6 +1897,8 @@ struct drm_i915_gem_object {
18851897
/** Used in execbuf to temporarily hold a ref */
18861898
struct list_head obj_exec_link;
18871899

1900+
struct list_head batch_pool_list;
1901+
18881902
/**
18891903
* This is set if the object is on the active lists (has pending
18901904
* rendering and so a non-zero seqno), and is not set if it i s on
@@ -2935,6 +2949,13 @@ void i915_destroy_error_state(struct drm_device *dev);
29352949
void i915_get_extra_instdone(struct drm_device *dev, uint32_t *instdone);
29362950
const char *i915_cache_level_str(struct drm_i915_private *i915, int type);
29372951

2952+
/* i915_gem_batch_pool.c */
2953+
void i915_gem_batch_pool_init(struct drm_device *dev,
2954+
struct i915_gem_batch_pool *pool);
2955+
void i915_gem_batch_pool_fini(struct i915_gem_batch_pool *pool);
2956+
struct drm_i915_gem_object*
2957+
i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool, size_t size);
2958+
29382959
/* i915_cmd_parser.c */
29392960
int i915_cmd_parser_get_version(void);
29402961
int i915_cmd_parser_init_ring(struct intel_engine_cs *ring);

drivers/gpu/drm/i915/i915_gem.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,6 +4393,7 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
43934393
INIT_LIST_HEAD(&obj->ring_list);
43944394
INIT_LIST_HEAD(&obj->obj_exec_link);
43954395
INIT_LIST_HEAD(&obj->vma_list);
4396+
INIT_LIST_HEAD(&obj->batch_pool_list);
43964397

43974398
obj->ops = ops;
43984399

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
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"
26+
27+
/**
28+
* DOC: batch pool
29+
*
30+
* In order to submit batch buffers as 'secure', the software command parser
31+
* must ensure that a batch buffer cannot be modified after parsing. It does
32+
* this by copying the user provided batch buffer contents to a kernel owned
33+
* buffer from which the hardware will actually execute, and by carefully
34+
* managing the address space bindings for such buffers.
35+
*
36+
* The batch pool framework provides a mechanism for the driver to manage a
37+
* set of scratch buffers to use for this purpose. The framework can be
38+
* extended to support other uses cases should they arise.
39+
*/
40+
41+
/**
42+
* i915_gem_batch_pool_init() - initialize a batch buffer pool
43+
* @dev: the drm device
44+
* @pool: the batch buffer pool
45+
*/
46+
void i915_gem_batch_pool_init(struct drm_device *dev,
47+
struct i915_gem_batch_pool *pool)
48+
{
49+
pool->dev = dev;
50+
INIT_LIST_HEAD(&pool->cache_list);
51+
}
52+
53+
/**
54+
* i915_gem_batch_pool_fini() - clean up a batch buffer pool
55+
* @pool: the pool to clean up
56+
*
57+
* Note: Callers must hold the struct_mutex.
58+
*/
59+
void i915_gem_batch_pool_fini(struct i915_gem_batch_pool *pool)
60+
{
61+
WARN_ON(!mutex_is_locked(&pool->dev->struct_mutex));
62+
63+
while (!list_empty(&pool->cache_list)) {
64+
struct drm_i915_gem_object *obj =
65+
list_first_entry(&pool->cache_list,
66+
struct drm_i915_gem_object,
67+
batch_pool_list);
68+
69+
WARN_ON(obj->active);
70+
71+
list_del_init(&obj->batch_pool_list);
72+
drm_gem_object_unreference(&obj->base);
73+
}
74+
}
75+
76+
/**
77+
* i915_gem_batch_pool_get() - select a buffer from the pool
78+
* @pool: the batch buffer pool
79+
* @size: the minimum desired size of the returned buffer
80+
*
81+
* Finds or allocates a batch buffer in the pool with at least the requested
82+
* size. The caller is responsible for any domain, active/inactive, or
83+
* purgeability management for the returned buffer.
84+
*
85+
* Note: Callers must hold the struct_mutex
86+
*
87+
* Return: the selected batch buffer object
88+
*/
89+
struct drm_i915_gem_object *
90+
i915_gem_batch_pool_get(struct i915_gem_batch_pool *pool,
91+
size_t size)
92+
{
93+
struct drm_i915_gem_object *obj = NULL;
94+
struct drm_i915_gem_object *tmp, *next;
95+
96+
WARN_ON(!mutex_is_locked(&pool->dev->struct_mutex));
97+
98+
list_for_each_entry_safe(tmp, next,
99+
&pool->cache_list, batch_pool_list) {
100+
101+
if (tmp->active)
102+
continue;
103+
104+
/* While we're looping, do some clean up */
105+
if (tmp->madv == __I915_MADV_PURGED) {
106+
list_del(&tmp->batch_pool_list);
107+
drm_gem_object_unreference(&tmp->base);
108+
continue;
109+
}
110+
111+
/*
112+
* Select a buffer that is at least as big as needed
113+
* but not 'too much' bigger. A better way to do this
114+
* might be to bucket the pool objects based on size.
115+
*/
116+
if (tmp->base.size >= size &&
117+
tmp->base.size <= (2 * size)) {
118+
obj = tmp;
119+
break;
120+
}
121+
}
122+
123+
if (!obj) {
124+
obj = i915_gem_alloc_object(pool->dev, size);
125+
if (!obj)
126+
return ERR_PTR(-ENOMEM);
127+
128+
list_add_tail(&obj->batch_pool_list, &pool->cache_list);
129+
}
130+
else
131+
/* Keep list in LRU order */
132+
list_move_tail(&obj->batch_pool_list, &pool->cache_list);
133+
134+
return obj;
135+
}

0 commit comments

Comments
 (0)