1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* Copyright © 2013 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Brad Volkin <bradley.d.volkin@intel.com>
*
*/
#include "i915_drv.h"
#include "i915_drm.h"
#define LENGTH_BIAS 2
static const struct drm_i915_cmd_descriptor*
find_cmd_in_table(const struct drm_i915_cmd_table *table,
unsigned int cmd_header)
{
int i;
for (i = 0; i < table->count; i++) {
const struct drm_i915_cmd_descriptor *desc = &table->table[i];
unsigned int masked_cmd = desc->cmd.mask & cmd_header;
unsigned int masked_value = desc->cmd.value & desc->cmd.mask;
if (masked_cmd == masked_value)
return desc;
}
return NULL;
}
static const struct drm_i915_cmd_descriptor*
find_cmd(struct intel_ring_buffer *ring, unsigned int cmd_header)
{
static struct drm_i915_cmd_descriptor default_desc = {
.flags = CMD_DESC_SKIP
};
unsigned int mask;
int i;
drm_i915_private_t *dev_priv = ring->dev->dev_private;
for (i = 0; i < ring->cmd_table_count; i++) {
const struct drm_i915_cmd_descriptor *desc;
desc = find_cmd_in_table(&ring->cmd_tables[i], cmd_header);
if (desc)
return desc;
}
if (dev_priv->append_cmd_table[ring->id]) {
const struct drm_i915_cmd_descriptor *desc;
desc = find_cmd_in_table(dev_priv->append_cmd_table[ring->id],
cmd_header);
if (desc)
return desc;
}
mask = ring->get_cmd_length_mask(cmd_header);
if (!mask)
return NULL;
default_desc.length.mask = mask;
return &default_desc;
}
static int valid_reg(const unsigned int *table, int count, unsigned int addr)
{
if (table && count != 0) {
int i;
for (i = 0; i < count; i++) {
if (table[i] == addr)
return 1;
}
}
return 0;
}
int i915_parse_cmds(struct intel_ring_buffer *ring,
u32 batch_start_offset,
u32 *batch_base,
u32 batch_obj_size)
{
int ret = 0;
unsigned int *cmd, *batch_end;
/* Needed because find_cmd() currently uses a static variable and
* upstream may rework the locking such that multiple execbuffer2
* calls may execute in parallel. We would need to rework find_cmd
* at that point.
*/
WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
/* No command tables currently indicates a platform without parsing */
if (!ring->cmd_tables)
return 0;
if (!batch_base) {
DRM_ERROR("CMD: Failed to vmap batch\n");
return -ENOMEM;
}
cmd = batch_base + (batch_start_offset / sizeof(*cmd));
batch_end = cmd + (batch_obj_size / sizeof(*batch_end));
while (cmd < batch_end) {
const struct drm_i915_cmd_descriptor *desc;
unsigned int length;
if (*cmd == MI_BATCH_BUFFER_END)
break;
desc = find_cmd(ring, *cmd);
if (!desc) {
DRM_ERROR("CMD: Unrecognized command: 0x%08X\n",
*cmd);
ret = -EINVAL;
break;
}
if (desc->flags & CMD_DESC_FIXED)
length = desc->length.fixed;
else
length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
if ((batch_end - cmd) < length) {
DRM_ERROR("CMD: Command length exceeds batch length: 0x%08X length=%d batchlen=%d\n",
*cmd,
length,
(int)(batch_end - cmd));
ret = -EINVAL;
break;
}
if (desc->flags & CMD_DESC_REJECT) {
DRM_ERROR("CMD: Rejected command: 0x%08X\n", *cmd);
ret = -EINVAL;
break;
}
if (desc->flags & CMD_DESC_REGISTER) {
unsigned int reg_addr =
cmd[desc->reg.offset] & desc->reg.mask;
if (!valid_reg(ring->reg_table,
ring->reg_count, reg_addr)) {
drm_i915_private_t *dev_priv =
ring->dev->dev_private;
unsigned int *append_table =
dev_priv->append_reg[ring->id].table;
int append_count =
dev_priv->append_reg[ring->id].count;
if (!append_table ||
!valid_reg(append_table,
append_count,
reg_addr)) {
DRM_ERROR("CMD: Rejected register 0x%08X in command: 0x%08X (ring=%d)\n",
reg_addr, *cmd, ring->id);
ret = -EINVAL;
break;
}
}
}
if (desc->flags & CMD_DESC_BITMASK) {
int i;
for (i = 0; i < desc->bits_count; i++) {
unsigned int dword =
cmd[desc->bits[i].offset] &
desc->bits[i].mask;
if (desc->bits[i].condition_mask != 0) {
unsigned int condition =
cmd[desc->bits[i].condition_offset] &
desc->bits[i].condition_mask;
if (condition == 0)
continue;
}
if (dword != desc->bits[i].expected) {
DRM_ERROR("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (ring=%d)\n",
*cmd, desc->bits[i].mask,
desc->bits[i].expected, dword, ring->id);
ret = -EINVAL;
break;
}
}
if (ret)
break;
}
cmd += length;
}
if (cmd >= batch_end) {
DRM_ERROR("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
ret = -EINVAL;
}
return ret;
}
static void cleanup_append_cmd_table(drm_i915_private_t *dev_priv, int ring_id)
{
if (dev_priv->append_cmd_table[ring_id]) {
const struct drm_i915_cmd_descriptor *table =
dev_priv->append_cmd_table[ring_id]->table;
drm_free_large((void *)table);
drm_free_large((void *)dev_priv->append_cmd_table[ring_id]);
dev_priv->append_cmd_table[ring_id] = NULL;
}
}
static void cleanup_append_reg_table(drm_i915_private_t *dev_priv, int ring_id)
{
if (dev_priv->append_reg[ring_id].table) {
drm_free_large((void *)dev_priv->append_reg[ring_id].table);
dev_priv->append_reg[ring_id].table = NULL;
dev_priv->append_reg[ring_id].count = 0;
}
}
void i915_cmd_parser_cleanup(drm_i915_private_t *dev_priv)
{
int i;
for (i = 0; i < I915_NUM_RINGS; i++) {
cleanup_append_cmd_table(dev_priv, i);
cleanup_append_reg_table(dev_priv, i);
}
}
static int append_cmds(drm_i915_private_t *dev_priv,
int ring_id,
struct drm_i915_cmd_parser_append *args)
{
struct drm_i915_cmd_table *cmd_table;
int ret;
cmd_table = drm_malloc_ab(sizeof(*cmd_table), 1);
if (!cmd_table)
return -ENOMEM;
cmd_table->count = args->cmd_count;
cmd_table->table = drm_malloc_ab(sizeof(*cmd_table->table),
args->cmd_count);
if (!cmd_table->table) {
drm_free_large(cmd_table);
return -ENOMEM;
}
ret = copy_from_user((void *)cmd_table->table,
(struct drm_i915_cmd_descriptor __user *)
(uintptr_t)args->cmds,
sizeof(*cmd_table->table) * args->cmd_count);
if (ret) {
drm_free_large((void *)cmd_table->table);
drm_free_large(cmd_table);
return -EFAULT;
}
dev_priv->append_cmd_table[ring_id] = cmd_table;
return 0;
}
static int append_regs(drm_i915_private_t *dev_priv,
int ring_id,
struct drm_i915_cmd_parser_append *args)
{
unsigned int *regs;
int ret;
regs = drm_malloc_ab(sizeof(*regs), args->reg_count);
if (!regs)
return -ENOMEM;
ret = copy_from_user(regs,
(unsigned int __user *)(uintptr_t)args->regs,
sizeof(*regs) * args->reg_count);
if (ret) {
drm_free_large(regs);
return -EFAULT;
}
dev_priv->append_reg[ring_id].table = regs;
dev_priv->append_reg[ring_id].count = args->reg_count;
return 0;
}
int i915_cmd_parser_append_ioctl(struct drm_device *dev, void *data,
struct drm_file *file_priv)
{
drm_i915_private_t *dev_priv = dev->dev_private;
struct drm_i915_cmd_parser_append *args = data;
struct intel_ring_buffer *ring = NULL;
int ret = 0;
mutex_lock(&dev->struct_mutex);
/* This ioctl has DRM_ROOT_ONLY set but the code to check that flag in
* drm_ioctl is/was removed from the VLV kernel. To be sure, check for
* the appropriate permission here.
*/
if (!capable(CAP_SYS_ADMIN)) {
DRM_ERROR("CMD: append from non-root user\n");
ret = -EACCES;
goto out;
}
if ((args->cmd_count < 1 && args->reg_count < 1) ||
(!args->cmds && !args->regs)) {
DRM_ERROR("CMD: append with invalid lists cmd=(0x%llx, %d) reg=(0x%llx, %d)\n",
args->cmds, args->cmd_count,
args->regs, args->reg_count);
ret = -EINVAL;
goto out;
}
switch (args->ring & I915_EXEC_RING_MASK) {
case I915_EXEC_DEFAULT:
case I915_EXEC_RENDER:
ring = &dev_priv->ring[RCS];
break;
case I915_EXEC_BSD:
ring = &dev_priv->ring[VCS];
break;
case I915_EXEC_BLT:
ring = &dev_priv->ring[BCS];
break;
case I915_EXEC_VEBOX:
ring = &dev_priv->ring[VECS];
break;
default:
DRM_ERROR("CMD: append with unknown ring: %d\n",
(int)(args->ring & I915_EXEC_RING_MASK));
ret = -EINVAL;
goto out;
}
if (args->cmds) {
if (dev_priv->append_cmd_table[ring->id]) {
DRM_ERROR("CMD: append cmd was already sent\n");
ret = -EEXIST;
} else {
ret = append_cmds(dev_priv, ring->id, args);
}
if (ret)
goto out;
}
if (args->regs) {
if (dev_priv->append_reg[ring->id].table) {
DRM_ERROR("CMD: append reg was already sent\n");
ret = -EEXIST;
} else {
ret = append_regs(dev_priv, ring->id, args);
if (ret)
cleanup_append_cmd_table(dev_priv, ring->id);
}
if (ret)
goto out;
}
out:
mutex_unlock(&dev->struct_mutex);
return ret;
}
|