blob: 27ea00489ecc41b08adc8b780bd18f62fa36a791 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2008 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Jerome Glisse <glisse@freedesktop.org>
26 */
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/radeon_drm.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020029#include "radeon_reg.h"
30#include "radeon.h"
Christian König860024e2013-09-07 18:29:01 +020031#include "radeon_trace.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020032
Lauri Kasanen1109ca02012-08-31 13:43:50 -040033static int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020034{
35 struct drm_device *ddev = p->rdev->ddev;
36 struct radeon_cs_chunk *chunk;
37 unsigned i, j;
38 bool duplicate;
39
40 if (p->chunk_relocs_idx == -1) {
41 return 0;
42 }
43 chunk = &p->chunks[p->chunk_relocs_idx];
Alex Deuchercf4ccd02011-11-18 10:19:47 -050044 p->dma_reloc_idx = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020045 /* FIXME: we assume that each relocs use 4 dwords */
46 p->nrelocs = chunk->length_dw / 4;
47 p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
48 if (p->relocs_ptr == NULL) {
49 return -ENOMEM;
50 }
51 p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
52 if (p->relocs == NULL) {
53 return -ENOMEM;
54 }
55 for (i = 0; i < p->nrelocs; i++) {
56 struct drm_radeon_cs_reloc *r;
57
58 duplicate = false;
59 r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
Christian König16557f12011-10-24 14:59:17 +020060 for (j = 0; j < i; j++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +020061 if (r->handle == p->relocs[j].handle) {
62 p->relocs_ptr[i] = &p->relocs[j];
63 duplicate = true;
64 break;
65 }
66 }
Christian König4474f3a2013-04-08 12:41:28 +020067 if (duplicate) {
Christian König16557f12011-10-24 14:59:17 +020068 p->relocs[i].handle = 0;
Christian König4474f3a2013-04-08 12:41:28 +020069 continue;
70 }
71
72 p->relocs[i].gobj = drm_gem_object_lookup(ddev, p->filp,
73 r->handle);
74 if (p->relocs[i].gobj == NULL) {
75 DRM_ERROR("gem object lookup failed 0x%x\n",
76 r->handle);
77 return -ENOENT;
78 }
79 p->relocs_ptr[i] = &p->relocs[i];
80 p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
81 p->relocs[i].lobj.bo = p->relocs[i].robj;
82 p->relocs[i].lobj.written = !!r->write_domain;
83
Christian Königf2ba57b2013-04-08 12:41:29 +020084 /* the first reloc of an UVD job is the
85 msg and that must be in VRAM */
86 if (p->ring == R600_RING_TYPE_UVD_INDEX && i == 0) {
87 /* TODO: is this still needed for NI+ ? */
88 p->relocs[i].lobj.domain =
89 RADEON_GEM_DOMAIN_VRAM;
90
91 p->relocs[i].lobj.alt_domain =
92 RADEON_GEM_DOMAIN_VRAM;
93
94 } else {
95 uint32_t domain = r->write_domain ?
96 r->write_domain : r->read_domains;
97
98 p->relocs[i].lobj.domain = domain;
99 if (domain == RADEON_GEM_DOMAIN_VRAM)
100 domain |= RADEON_GEM_DOMAIN_GTT;
101 p->relocs[i].lobj.alt_domain = domain;
102 }
Christian König4474f3a2013-04-08 12:41:28 +0200103
104 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
105 p->relocs[i].handle = r->handle;
106
107 radeon_bo_list_add_object(&p->relocs[i].lobj,
108 &p->validated);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200109 }
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200110 return radeon_bo_list_validate(&p->ticket, &p->validated, p->ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200111}
112
Jerome Glisse721604a2012-01-05 22:11:05 -0500113static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
114{
115 p->priority = priority;
116
117 switch (ring) {
118 default:
119 DRM_ERROR("unknown ring id: %d\n", ring);
120 return -EINVAL;
121 case RADEON_CS_RING_GFX:
122 p->ring = RADEON_RING_TYPE_GFX_INDEX;
123 break;
124 case RADEON_CS_RING_COMPUTE:
Alex Deucher963e81f2013-06-26 17:37:11 -0400125 if (p->rdev->family >= CHIP_TAHITI) {
Alex Deucher8d5ef7b2012-03-20 17:18:24 -0400126 if (p->priority > 0)
127 p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
128 else
129 p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
130 } else
131 p->ring = RADEON_RING_TYPE_GFX_INDEX;
Jerome Glisse721604a2012-01-05 22:11:05 -0500132 break;
Alex Deucher278a3342012-12-13 12:27:28 -0500133 case RADEON_CS_RING_DMA:
134 if (p->rdev->family >= CHIP_CAYMAN) {
135 if (p->priority > 0)
136 p->ring = R600_RING_TYPE_DMA_INDEX;
137 else
138 p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
139 } else if (p->rdev->family >= CHIP_R600) {
140 p->ring = R600_RING_TYPE_DMA_INDEX;
141 } else {
142 return -EINVAL;
143 }
144 break;
Christian Königf2ba57b2013-04-08 12:41:29 +0200145 case RADEON_CS_RING_UVD:
146 p->ring = R600_RING_TYPE_UVD_INDEX;
147 break;
Jerome Glisse721604a2012-01-05 22:11:05 -0500148 }
149 return 0;
150}
151
Christian König220907d2012-05-10 16:46:43 +0200152static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
Christian König93504fc2012-01-05 22:11:06 -0500153{
Christian König220907d2012-05-10 16:46:43 +0200154 int i;
Christian König93504fc2012-01-05 22:11:06 -0500155
Christian Königcdac5502012-02-23 15:18:42 +0100156 for (i = 0; i < p->nrelocs; i++) {
Christian Königf82cbdd2012-08-09 16:35:36 +0200157 if (!p->relocs[i].robj)
Christian Königcdac5502012-02-23 15:18:42 +0100158 continue;
159
Alex Deucher43f12142013-02-01 17:32:42 +0100160 radeon_ib_sync_to(&p->ib, p->relocs[i].robj->tbo.sync_obj);
Christian Königcdac5502012-02-23 15:18:42 +0100161 }
Christian König93504fc2012-01-05 22:11:06 -0500162}
163
Alex Deucher9b001472012-05-30 10:09:30 -0400164/* XXX: note that this is called from the legacy UMS CS ioctl as well */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200165int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
166{
167 struct drm_radeon_cs *cs = data;
168 uint64_t *chunk_array_ptr;
Jerome Glisse721604a2012-01-05 22:11:05 -0500169 unsigned size, i;
170 u32 ring = RADEON_CS_RING_GFX;
171 s32 priority = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172
173 if (!cs->num_chunks) {
174 return 0;
175 }
176 /* get chunks */
177 INIT_LIST_HEAD(&p->validated);
178 p->idx = 0;
Jerome Glissef2e39222012-05-09 15:35:02 +0200179 p->ib.sa_bo = NULL;
180 p->ib.semaphore = NULL;
181 p->const_ib.sa_bo = NULL;
182 p->const_ib.semaphore = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200183 p->chunk_ib_idx = -1;
184 p->chunk_relocs_idx = -1;
Jerome Glisse721604a2012-01-05 22:11:05 -0500185 p->chunk_flags_idx = -1;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400186 p->chunk_const_ib_idx = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200187 p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
188 if (p->chunks_array == NULL) {
189 return -ENOMEM;
190 }
191 chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
192 if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
193 sizeof(uint64_t)*cs->num_chunks)) {
194 return -EFAULT;
195 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500196 p->cs_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200197 p->nchunks = cs->num_chunks;
198 p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
199 if (p->chunks == NULL) {
200 return -ENOMEM;
201 }
202 for (i = 0; i < p->nchunks; i++) {
203 struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
204 struct drm_radeon_cs_chunk user_chunk;
205 uint32_t __user *cdata;
206
207 chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
208 if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
209 sizeof(struct drm_radeon_cs_chunk))) {
210 return -EFAULT;
211 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000212 p->chunks[i].length_dw = user_chunk.length_dw;
213 p->chunks[i].kdata = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200214 p->chunks[i].chunk_id = user_chunk.chunk_id;
Ilija Hadzic580f8392013-01-02 18:27:37 -0500215 p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200216 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
217 p->chunk_relocs_idx = i;
218 }
219 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
220 p->chunk_ib_idx = i;
Dave Airlie5176fdc2009-06-30 11:47:14 +1000221 /* zero length IB isn't useful */
222 if (p->chunks[i].length_dw == 0)
223 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200224 }
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400225 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
226 p->chunk_const_ib_idx = i;
227 /* zero length CONST IB isn't useful */
228 if (p->chunks[i].length_dw == 0)
229 return -EINVAL;
230 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500231 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
232 p->chunk_flags_idx = i;
233 /* zero length flags aren't useful */
234 if (p->chunks[i].length_dw == 0)
235 return -EINVAL;
Marek Olšáke70f2242011-10-25 01:38:45 +0200236 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000237
Dave Airlie513bcb42009-09-23 16:56:27 +1000238 cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse721604a2012-01-05 22:11:05 -0500239 if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
240 (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
Dave Airlie513bcb42009-09-23 16:56:27 +1000241 size = p->chunks[i].length_dw * sizeof(uint32_t);
242 p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
243 if (p->chunks[i].kdata == NULL) {
244 return -ENOMEM;
245 }
246 if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
247 p->chunks[i].user_ptr, size)) {
248 return -EFAULT;
249 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200250 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500251 p->cs_flags = p->chunks[i].kdata[0];
252 if (p->chunks[i].length_dw > 1)
253 ring = p->chunks[i].kdata[1];
254 if (p->chunks[i].length_dw > 2)
255 priority = (s32)p->chunks[i].kdata[2];
Marek Olšáke70f2242011-10-25 01:38:45 +0200256 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200257 }
258 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500259
Alex Deucher9b001472012-05-30 10:09:30 -0400260 /* these are KMS only */
261 if (p->rdev) {
262 if ((p->cs_flags & RADEON_CS_USE_VM) &&
263 !p->rdev->vm_manager.enabled) {
264 DRM_ERROR("VM not active on asic!\n");
265 return -EINVAL;
266 }
267
Alex Deucher9b001472012-05-30 10:09:30 -0400268 if (radeon_cs_get_ring(p, ring, priority))
269 return -EINVAL;
Christian König57449042013-04-08 12:41:27 +0200270
271 /* we only support VM on some SI+ rings */
Christian König76a0df82013-08-13 11:56:50 +0200272 if ((p->rdev->asic->ring[p->ring]->cs_parse == NULL) &&
Christian König57449042013-04-08 12:41:27 +0200273 ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
274 DRM_ERROR("Ring %d requires VM!\n", p->ring);
275 return -EINVAL;
276 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200277 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200278
Jerome Glisse721604a2012-01-05 22:11:05 -0500279 /* deal with non-vm */
280 if ((p->chunk_ib_idx != -1) &&
281 ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
282 (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
283 if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
284 DRM_ERROR("cs IB too big: %d\n",
285 p->chunks[p->chunk_ib_idx].length_dw);
286 return -EINVAL;
287 }
Ilija Hadzicff4bd082013-01-07 18:21:57 -0500288 if (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) {
Dave Airlie6a7068b2012-04-03 16:23:41 +0100289 p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
290 p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
291 if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
292 p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
Ilija Hadzic25d89992013-01-07 18:21:59 -0500293 kfree(p->chunks[p->chunk_ib_idx].kpage[0]);
294 kfree(p->chunks[p->chunk_ib_idx].kpage[1]);
Ilija Hadzic1da80cf2013-01-23 13:59:05 -0500295 p->chunks[p->chunk_ib_idx].kpage[0] = NULL;
296 p->chunks[p->chunk_ib_idx].kpage[1] = NULL;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100297 return -ENOMEM;
298 }
299 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500300 p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
301 p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
302 p->chunks[p->chunk_ib_idx].last_copied_page = -1;
303 p->chunks[p->chunk_ib_idx].last_page_index =
304 ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
305 }
306
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200307 return 0;
308}
309
310/**
311 * cs_parser_fini() - clean parser states
312 * @parser: parser structure holding parsing context.
313 * @error: error number
314 *
315 * If error is set than unvalidate buffer, otherwise just free memory
316 * used by parsing context.
317 **/
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200318static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error, bool backoff)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200319{
320 unsigned i;
321
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400322 if (!error) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200323 ttm_eu_fence_buffer_objects(&parser->ticket,
324 &parser->validated,
Jerome Glissef2e39222012-05-09 15:35:02 +0200325 parser->ib.fence);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200326 } else if (backoff) {
327 ttm_eu_backoff_reservation(&parser->ticket,
328 &parser->validated);
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400329 }
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000330
Pauli Nieminenfcbc4512010-03-19 07:44:33 +0000331 if (parser->relocs != NULL) {
332 for (i = 0; i < parser->nrelocs; i++) {
333 if (parser->relocs[i].gobj)
334 drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
335 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200336 }
Michel Dänzer48e113e2009-09-15 17:09:32 +0200337 kfree(parser->track);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200338 kfree(parser->relocs);
339 kfree(parser->relocs_ptr);
340 for (i = 0; i < parser->nchunks; i++) {
341 kfree(parser->chunks[i].kdata);
Dave Airlie6a7068b2012-04-03 16:23:41 +0100342 if ((parser->rdev->flags & RADEON_IS_AGP)) {
343 kfree(parser->chunks[i].kpage[0]);
344 kfree(parser->chunks[i].kpage[1]);
345 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346 }
347 kfree(parser->chunks);
348 kfree(parser->chunks_array);
349 radeon_ib_free(parser->rdev, &parser->ib);
Jerome Glissef2e39222012-05-09 15:35:02 +0200350 radeon_ib_free(parser->rdev, &parser->const_ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351}
352
Jerome Glisse721604a2012-01-05 22:11:05 -0500353static int radeon_cs_ib_chunk(struct radeon_device *rdev,
354 struct radeon_cs_parser *parser)
355{
356 struct radeon_cs_chunk *ib_chunk;
357 int r;
358
359 if (parser->chunk_ib_idx == -1)
360 return 0;
361
362 if (parser->cs_flags & RADEON_CS_USE_VM)
363 return 0;
364
365 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
366 /* Copy the packet into the IB, the parser will read from the
367 * input memory (cached) and write to the IB (which can be
368 * uncached).
369 */
370 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200371 NULL, ib_chunk->length_dw * 4);
Jerome Glisse721604a2012-01-05 22:11:05 -0500372 if (r) {
373 DRM_ERROR("Failed to get ib !\n");
374 return r;
375 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200376 parser->ib.length_dw = ib_chunk->length_dw;
Christian Königeb0c19c2012-02-23 15:18:44 +0100377 r = radeon_cs_parse(rdev, parser->ring, parser);
Jerome Glisse721604a2012-01-05 22:11:05 -0500378 if (r || parser->parser_error) {
379 DRM_ERROR("Invalid command stream !\n");
380 return r;
381 }
382 r = radeon_cs_finish_pages(parser);
383 if (r) {
384 DRM_ERROR("Invalid command stream !\n");
385 return r;
386 }
Alex Deucherce3537d2013-07-24 12:12:49 -0400387
388 if (parser->ring == R600_RING_TYPE_UVD_INDEX)
389 radeon_uvd_note_usage(rdev);
390
Christian König220907d2012-05-10 16:46:43 +0200391 radeon_cs_sync_rings(parser);
Christian König4ef72562012-07-13 13:06:00 +0200392 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
Jerome Glisse721604a2012-01-05 22:11:05 -0500393 if (r) {
394 DRM_ERROR("Failed to schedule IB !\n");
395 }
Christian König93bf8882012-07-03 14:05:41 +0200396 return r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500397}
398
399static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
400 struct radeon_vm *vm)
401{
Jerome Glisse3e8970f2012-08-13 12:07:33 -0400402 struct radeon_device *rdev = parser->rdev;
Jerome Glisse721604a2012-01-05 22:11:05 -0500403 struct radeon_bo_list *lobj;
404 struct radeon_bo *bo;
405 int r;
406
Jerome Glisse3e8970f2012-08-13 12:07:33 -0400407 r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
408 if (r) {
409 return r;
410 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500411 list_for_each_entry(lobj, &parser->validated, tv.head) {
412 bo = lobj->bo;
413 r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
414 if (r) {
415 return r;
416 }
417 }
418 return 0;
419}
420
421static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
422 struct radeon_cs_parser *parser)
423{
424 struct radeon_cs_chunk *ib_chunk;
425 struct radeon_fpriv *fpriv = parser->filp->driver_priv;
426 struct radeon_vm *vm = &fpriv->vm;
427 int r;
428
429 if (parser->chunk_ib_idx == -1)
430 return 0;
Jerome Glisse721604a2012-01-05 22:11:05 -0500431 if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
432 return 0;
433
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400434 if ((rdev->family >= CHIP_TAHITI) &&
435 (parser->chunk_const_ib_idx != -1)) {
436 ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
437 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
438 DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
439 return -EINVAL;
440 }
441 r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200442 vm, ib_chunk->length_dw * 4);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400443 if (r) {
444 DRM_ERROR("Failed to get const ib !\n");
445 return r;
446 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200447 parser->const_ib.is_const_ib = true;
448 parser->const_ib.length_dw = ib_chunk->length_dw;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400449 /* Copy the packet into the IB */
Jerome Glissef2e39222012-05-09 15:35:02 +0200450 if (DRM_COPY_FROM_USER(parser->const_ib.ptr, ib_chunk->user_ptr,
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400451 ib_chunk->length_dw * 4)) {
452 return -EFAULT;
453 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200454 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400455 if (r) {
456 return r;
457 }
458 }
459
Jerome Glisse721604a2012-01-05 22:11:05 -0500460 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
461 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
462 DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
463 return -EINVAL;
464 }
465 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200466 vm, ib_chunk->length_dw * 4);
Jerome Glisse721604a2012-01-05 22:11:05 -0500467 if (r) {
468 DRM_ERROR("Failed to get ib !\n");
469 return r;
470 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200471 parser->ib.length_dw = ib_chunk->length_dw;
Jerome Glisse721604a2012-01-05 22:11:05 -0500472 /* Copy the packet into the IB */
Jerome Glissef2e39222012-05-09 15:35:02 +0200473 if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr,
Jerome Glisse721604a2012-01-05 22:11:05 -0500474 ib_chunk->length_dw * 4)) {
475 return -EFAULT;
476 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200477 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
Jerome Glisse721604a2012-01-05 22:11:05 -0500478 if (r) {
479 return r;
480 }
481
Alex Deucherce3537d2013-07-24 12:12:49 -0400482 if (parser->ring == R600_RING_TYPE_UVD_INDEX)
483 radeon_uvd_note_usage(rdev);
484
Christian König36ff39c2012-05-09 10:07:08 +0200485 mutex_lock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500486 mutex_lock(&vm->mutex);
Christian Königddf03f52012-08-09 20:02:28 +0200487 r = radeon_vm_alloc_pt(rdev, vm);
Jerome Glisse721604a2012-01-05 22:11:05 -0500488 if (r) {
489 goto out;
490 }
491 r = radeon_bo_vm_update_pte(parser, vm);
492 if (r) {
493 goto out;
494 }
Christian König220907d2012-05-10 16:46:43 +0200495 radeon_cs_sync_rings(parser);
Alex Deucher43f12142013-02-01 17:32:42 +0100496 radeon_ib_sync_to(&parser->ib, vm->fence);
497 radeon_ib_sync_to(&parser->ib, radeon_vm_grab_id(
498 rdev, vm, parser->ring));
Christian König4ef72562012-07-13 13:06:00 +0200499
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400500 if ((rdev->family >= CHIP_TAHITI) &&
501 (parser->chunk_const_ib_idx != -1)) {
Christian König4ef72562012-07-13 13:06:00 +0200502 r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
503 } else {
504 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400505 }
506
Jerome Glisse721604a2012-01-05 22:11:05 -0500507 if (!r) {
Christian Königee60e292012-08-09 16:21:08 +0200508 radeon_vm_fence(rdev, vm, parser->ib.fence);
Jerome Glisse721604a2012-01-05 22:11:05 -0500509 }
Christian Königee60e292012-08-09 16:21:08 +0200510
511out:
Christian König13e55c32012-10-09 13:31:19 +0200512 radeon_vm_add_to_lru(rdev, vm);
Christian König36ff39c2012-05-09 10:07:08 +0200513 mutex_unlock(&vm->mutex);
514 mutex_unlock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500515 return r;
516}
517
Christian König6c6f4782012-05-02 15:11:19 +0200518static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
519{
520 if (r == -EDEADLK) {
521 r = radeon_gpu_reset(rdev);
522 if (!r)
523 r = -EAGAIN;
524 }
525 return r;
526}
527
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200528int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
529{
530 struct radeon_device *rdev = dev->dev_private;
531 struct radeon_cs_parser parser;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200532 int r;
533
Jerome Glissedee53e72012-07-02 12:45:19 -0400534 down_read(&rdev->exclusive_lock);
Jerome Glisse6b7746e2012-02-20 17:57:20 -0500535 if (!rdev->accel_working) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400536 up_read(&rdev->exclusive_lock);
Jerome Glisse6b7746e2012-02-20 17:57:20 -0500537 return -EBUSY;
538 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200539 /* initialize parser */
540 memset(&parser, 0, sizeof(struct radeon_cs_parser));
541 parser.filp = filp;
542 parser.rdev = rdev;
Jerome Glissec8c15ff2010-01-18 13:01:36 +0100543 parser.dev = rdev->dev;
Dave Airlie428c6e32011-06-08 19:58:29 +1000544 parser.family = rdev->family;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200545 r = radeon_cs_parser_init(&parser, data);
546 if (r) {
547 DRM_ERROR("Failed to initialize parser !\n");
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200548 radeon_cs_parser_fini(&parser, r, false);
Jerome Glissedee53e72012-07-02 12:45:19 -0400549 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200550 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200551 return r;
552 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200553 r = radeon_cs_parser_relocs(&parser);
554 if (r) {
Dave Airlie97f23b32010-03-19 10:33:44 +1000555 if (r != -ERESTARTSYS)
556 DRM_ERROR("Failed to parse relocation %d!\n", r);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200557 radeon_cs_parser_fini(&parser, r, false);
Jerome Glissedee53e72012-07-02 12:45:19 -0400558 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200559 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200560 return r;
561 }
Christian König55b51c82013-04-18 15:25:59 +0200562
Christian König860024e2013-09-07 18:29:01 +0200563 trace_radeon_cs(&parser);
564
Jerome Glisse721604a2012-01-05 22:11:05 -0500565 r = radeon_cs_ib_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200566 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500567 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200568 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500569 r = radeon_cs_ib_vm_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200570 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500571 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200572 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500573out:
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200574 radeon_cs_parser_fini(&parser, r, true);
Jerome Glissedee53e72012-07-02 12:45:19 -0400575 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200576 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200577 return r;
578}
Dave Airlie513bcb42009-09-23 16:56:27 +1000579
580int radeon_cs_finish_pages(struct radeon_cs_parser *p)
581{
582 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
583 int i;
584 int size = PAGE_SIZE;
585
586 for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
587 if (i == ibc->last_page_index) {
588 size = (ibc->length_dw * 4) % PAGE_SIZE;
589 if (size == 0)
590 size = PAGE_SIZE;
591 }
592
Jerome Glissef2e39222012-05-09 15:35:02 +0200593 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
Dave Airlie513bcb42009-09-23 16:56:27 +1000594 ibc->user_ptr + (i * PAGE_SIZE),
595 size))
596 return -EFAULT;
597 }
598 return 0;
599}
600
Dave Airliec4c7f312012-05-26 17:34:24 +0100601static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
Dave Airlie513bcb42009-09-23 16:56:27 +1000602{
603 int new_page;
Dave Airlie513bcb42009-09-23 16:56:27 +1000604 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
605 int i;
606 int size = PAGE_SIZE;
Ilija Hadzicff4bd082013-01-07 18:21:57 -0500607 bool copy1 = (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) ?
608 false : true;
Dave Airlie513bcb42009-09-23 16:56:27 +1000609
Dave Airliec5e617e2009-09-26 09:03:39 +1000610 for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
Jerome Glissef2e39222012-05-09 15:35:02 +0200611 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
Dave Airlie513bcb42009-09-23 16:56:27 +1000612 ibc->user_ptr + (i * PAGE_SIZE),
613 PAGE_SIZE)) {
614 p->parser_error = -EFAULT;
615 return 0;
616 }
617 }
618
Dave Airlie513bcb42009-09-23 16:56:27 +1000619 if (pg_idx == ibc->last_page_index) {
620 size = (ibc->length_dw * 4) % PAGE_SIZE;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100621 if (size == 0)
622 size = PAGE_SIZE;
Dave Airlie513bcb42009-09-23 16:56:27 +1000623 }
624
Dave Airlie6a7068b2012-04-03 16:23:41 +0100625 new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
626 if (copy1)
Jerome Glissef2e39222012-05-09 15:35:02 +0200627 ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4));
Dave Airlie6a7068b2012-04-03 16:23:41 +0100628
Dave Airlie513bcb42009-09-23 16:56:27 +1000629 if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
630 ibc->user_ptr + (pg_idx * PAGE_SIZE),
631 size)) {
632 p->parser_error = -EFAULT;
633 return 0;
634 }
635
Dave Airlie6a7068b2012-04-03 16:23:41 +0100636 /* copy to IB for non single case */
637 if (!copy1)
Jerome Glissef2e39222012-05-09 15:35:02 +0200638 memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
Dave Airlie513bcb42009-09-23 16:56:27 +1000639
640 ibc->last_copied_page = pg_idx;
641 ibc->kpage_idx[new_page] = pg_idx;
642
643 return new_page;
644}
Dave Airliec4c7f312012-05-26 17:34:24 +0100645
646u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
647{
648 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
649 u32 pg_idx, pg_offset;
650 u32 idx_value = 0;
651 int new_page;
652
653 pg_idx = (idx * 4) / PAGE_SIZE;
654 pg_offset = (idx * 4) % PAGE_SIZE;
655
656 if (ibc->kpage_idx[0] == pg_idx)
657 return ibc->kpage[0][pg_offset/4];
658 if (ibc->kpage_idx[1] == pg_idx)
659 return ibc->kpage[1][pg_offset/4];
660
661 new_page = radeon_cs_update_pages(p, pg_idx);
662 if (new_page < 0) {
663 p->parser_error = new_page;
664 return 0;
665 }
666
667 idx_value = ibc->kpage[new_page][pg_offset/4];
668 return idx_value;
669}
Ilija Hadzic4db01312013-01-02 18:27:40 -0500670
671/**
672 * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
673 * @parser: parser structure holding parsing context.
674 * @pkt: where to store packet information
675 *
676 * Assume that chunk_ib_index is properly set. Will return -EINVAL
677 * if packet is bigger than remaining ib size. or if packets is unknown.
678 **/
679int radeon_cs_packet_parse(struct radeon_cs_parser *p,
680 struct radeon_cs_packet *pkt,
681 unsigned idx)
682{
683 struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
684 struct radeon_device *rdev = p->rdev;
685 uint32_t header;
686
687 if (idx >= ib_chunk->length_dw) {
688 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
689 idx, ib_chunk->length_dw);
690 return -EINVAL;
691 }
692 header = radeon_get_ib_value(p, idx);
693 pkt->idx = idx;
694 pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
695 pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
696 pkt->one_reg_wr = 0;
697 switch (pkt->type) {
698 case RADEON_PACKET_TYPE0:
699 if (rdev->family < CHIP_R600) {
700 pkt->reg = R100_CP_PACKET0_GET_REG(header);
701 pkt->one_reg_wr =
702 RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
703 } else
704 pkt->reg = R600_CP_PACKET0_GET_REG(header);
705 break;
706 case RADEON_PACKET_TYPE3:
707 pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
708 break;
709 case RADEON_PACKET_TYPE2:
710 pkt->count = -1;
711 break;
712 default:
713 DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
714 return -EINVAL;
715 }
716 if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
717 DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
718 pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
719 return -EINVAL;
720 }
721 return 0;
722}
Ilija Hadzic9ffb7a62013-01-02 18:27:42 -0500723
724/**
725 * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
726 * @p: structure holding the parser context.
727 *
728 * Check if the next packet is NOP relocation packet3.
729 **/
730bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
731{
732 struct radeon_cs_packet p3reloc;
733 int r;
734
735 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
736 if (r)
737 return false;
738 if (p3reloc.type != RADEON_PACKET_TYPE3)
739 return false;
740 if (p3reloc.opcode != RADEON_PACKET3_NOP)
741 return false;
742 return true;
743}
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -0500744
745/**
746 * radeon_cs_dump_packet() - dump raw packet context
747 * @p: structure holding the parser context.
748 * @pkt: structure holding the packet.
749 *
750 * Used mostly for debugging and error reporting.
751 **/
752void radeon_cs_dump_packet(struct radeon_cs_parser *p,
753 struct radeon_cs_packet *pkt)
754{
755 volatile uint32_t *ib;
756 unsigned i;
757 unsigned idx;
758
759 ib = p->ib.ptr;
760 idx = pkt->idx;
761 for (i = 0; i <= (pkt->count + 1); i++, idx++)
762 DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
763}
764
Ilija Hadzice9716992013-01-02 18:27:46 -0500765/**
766 * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
767 * @parser: parser structure holding parsing context.
768 * @data: pointer to relocation data
769 * @offset_start: starting offset
770 * @offset_mask: offset mask (to align start offset on)
771 * @reloc: reloc informations
772 *
773 * Check if next packet is relocation packet3, do bo validation and compute
774 * GPU offset using the provided start.
775 **/
776int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
777 struct radeon_cs_reloc **cs_reloc,
778 int nomm)
779{
780 struct radeon_cs_chunk *relocs_chunk;
781 struct radeon_cs_packet p3reloc;
782 unsigned idx;
783 int r;
784
785 if (p->chunk_relocs_idx == -1) {
786 DRM_ERROR("No relocation chunk !\n");
787 return -EINVAL;
788 }
789 *cs_reloc = NULL;
790 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
791 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
792 if (r)
793 return r;
794 p->idx += p3reloc.count + 2;
795 if (p3reloc.type != RADEON_PACKET_TYPE3 ||
796 p3reloc.opcode != RADEON_PACKET3_NOP) {
797 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
798 p3reloc.idx);
799 radeon_cs_dump_packet(p, &p3reloc);
800 return -EINVAL;
801 }
802 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
803 if (idx >= relocs_chunk->length_dw) {
804 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
805 idx, relocs_chunk->length_dw);
806 radeon_cs_dump_packet(p, &p3reloc);
807 return -EINVAL;
808 }
809 /* FIXME: we assume reloc size is 4 dwords */
810 if (nomm) {
811 *cs_reloc = p->relocs;
812 (*cs_reloc)->lobj.gpu_offset =
813 (u64)relocs_chunk->kdata[idx + 3] << 32;
814 (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
815 } else
816 *cs_reloc = p->relocs_ptr[(idx / 4)];
817 return 0;
818}