blob: dcfe2a0bcdc0f50aae36bcc99a96e3a58af31747 [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 */
27#include "drmP.h"
28#include "radeon_drm.h"
29#include "radeon_reg.h"
30#include "radeon.h"
31
32void r100_cs_dump_packet(struct radeon_cs_parser *p,
33 struct radeon_cs_packet *pkt);
34
35int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
36{
37 struct drm_device *ddev = p->rdev->ddev;
38 struct radeon_cs_chunk *chunk;
39 unsigned i, j;
40 bool duplicate;
41
42 if (p->chunk_relocs_idx == -1) {
43 return 0;
44 }
45 chunk = &p->chunks[p->chunk_relocs_idx];
46 /* FIXME: we assume that each relocs use 4 dwords */
47 p->nrelocs = chunk->length_dw / 4;
48 p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
49 if (p->relocs_ptr == NULL) {
50 return -ENOMEM;
51 }
52 p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
53 if (p->relocs == NULL) {
54 return -ENOMEM;
55 }
56 for (i = 0; i < p->nrelocs; i++) {
57 struct drm_radeon_cs_reloc *r;
58
59 duplicate = false;
60 r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
Christian König16557f12011-10-24 14:59:17 +020061 for (j = 0; j < i; j++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +020062 if (r->handle == p->relocs[j].handle) {
63 p->relocs_ptr[i] = &p->relocs[j];
64 duplicate = true;
65 break;
66 }
67 }
68 if (!duplicate) {
69 p->relocs[i].gobj = drm_gem_object_lookup(ddev,
70 p->filp,
71 r->handle);
72 if (p->relocs[i].gobj == NULL) {
73 DRM_ERROR("gem object lookup failed 0x%x\n",
74 r->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +010075 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020076 }
77 p->relocs_ptr[i] = &p->relocs[i];
Daniel Vetter7e4d15d2011-02-18 17:59:17 +010078 p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +010079 p->relocs[i].lobj.bo = p->relocs[i].robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020080 p->relocs[i].lobj.wdomain = r->write_domain;
Thomas Hellstrom147666f2010-11-17 12:38:32 +000081 p->relocs[i].lobj.rdomain = r->read_domains;
82 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020083 p->relocs[i].handle = r->handle;
84 p->relocs[i].flags = r->flags;
Jerome Glisse4c788672009-11-20 14:29:23 +010085 radeon_bo_list_add_object(&p->relocs[i].lobj,
Thomas Hellstrom147666f2010-11-17 12:38:32 +000086 &p->validated);
Christian König93504fc2012-01-05 22:11:06 -050087
Christian König16557f12011-10-24 14:59:17 +020088 } else
89 p->relocs[i].handle = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020090 }
Jerome Glisse94429bb2010-02-15 21:36:33 +010091 return radeon_bo_list_validate(&p->validated);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020092}
93
Jerome Glisse721604a2012-01-05 22:11:05 -050094static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
95{
96 p->priority = priority;
97
98 switch (ring) {
99 default:
100 DRM_ERROR("unknown ring id: %d\n", ring);
101 return -EINVAL;
102 case RADEON_CS_RING_GFX:
103 p->ring = RADEON_RING_TYPE_GFX_INDEX;
104 break;
105 case RADEON_CS_RING_COMPUTE:
Alex Deucher8d5ef7b2012-03-20 17:18:24 -0400106 if (p->rdev->family >= CHIP_TAHITI) {
107 if (p->priority > 0)
108 p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
109 else
110 p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
111 } else
112 p->ring = RADEON_RING_TYPE_GFX_INDEX;
Jerome Glisse721604a2012-01-05 22:11:05 -0500113 break;
114 }
115 return 0;
116}
117
Christian König93504fc2012-01-05 22:11:06 -0500118static int radeon_cs_sync_rings(struct radeon_cs_parser *p)
119{
Christian Königcdac5502012-02-23 15:18:42 +0100120 bool sync_to_ring[RADEON_NUM_RINGS] = { };
Christian König8f676c42012-05-02 15:11:18 +0200121 bool need_sync = false;
Christian König93504fc2012-01-05 22:11:06 -0500122 int i, r;
123
Christian Königcdac5502012-02-23 15:18:42 +0100124 for (i = 0; i < p->nrelocs; i++) {
Jerome Glisse133f4cb2012-05-09 15:34:44 +0200125 struct radeon_fence *fence;
126
Christian Königcdac5502012-02-23 15:18:42 +0100127 if (!p->relocs[i].robj || !p->relocs[i].robj->tbo.sync_obj)
128 continue;
129
Jerome Glisse133f4cb2012-05-09 15:34:44 +0200130 fence = p->relocs[i].robj->tbo.sync_obj;
131 if (fence->ring != p->ring && !radeon_fence_signaled(fence)) {
132 sync_to_ring[fence->ring] = true;
133 need_sync = true;
Christian Königcdac5502012-02-23 15:18:42 +0100134 }
135 }
136
Christian König8f676c42012-05-02 15:11:18 +0200137 if (!need_sync) {
138 return 0;
Christian König93504fc2012-01-05 22:11:06 -0500139 }
Christian König8f676c42012-05-02 15:11:18 +0200140
Jerome Glisse68470ae2012-05-09 15:35:00 +0200141 r = radeon_semaphore_create(p->rdev, &p->ib->semaphore);
Christian König8f676c42012-05-02 15:11:18 +0200142 if (r) {
143 return r;
144 }
145
Jerome Glisse68470ae2012-05-09 15:35:00 +0200146 return radeon_semaphore_sync_rings(p->rdev, p->ib->semaphore,
Christian König8f676c42012-05-02 15:11:18 +0200147 sync_to_ring, p->ring);
Christian König93504fc2012-01-05 22:11:06 -0500148}
149
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200150int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
151{
152 struct drm_radeon_cs *cs = data;
153 uint64_t *chunk_array_ptr;
Jerome Glisse721604a2012-01-05 22:11:05 -0500154 unsigned size, i;
155 u32 ring = RADEON_CS_RING_GFX;
156 s32 priority = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200157
158 if (!cs->num_chunks) {
159 return 0;
160 }
161 /* get chunks */
162 INIT_LIST_HEAD(&p->validated);
163 p->idx = 0;
Jerome Glisseb7f64132012-05-02 16:24:40 -0400164 p->ib = NULL;
165 p->const_ib = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200166 p->chunk_ib_idx = -1;
167 p->chunk_relocs_idx = -1;
Jerome Glisse721604a2012-01-05 22:11:05 -0500168 p->chunk_flags_idx = -1;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400169 p->chunk_const_ib_idx = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200170 p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
171 if (p->chunks_array == NULL) {
172 return -ENOMEM;
173 }
174 chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
175 if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
176 sizeof(uint64_t)*cs->num_chunks)) {
177 return -EFAULT;
178 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500179 p->cs_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200180 p->nchunks = cs->num_chunks;
181 p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
182 if (p->chunks == NULL) {
183 return -ENOMEM;
184 }
185 for (i = 0; i < p->nchunks; i++) {
186 struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
187 struct drm_radeon_cs_chunk user_chunk;
188 uint32_t __user *cdata;
189
190 chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
191 if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
192 sizeof(struct drm_radeon_cs_chunk))) {
193 return -EFAULT;
194 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000195 p->chunks[i].length_dw = user_chunk.length_dw;
196 p->chunks[i].kdata = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200197 p->chunks[i].chunk_id = user_chunk.chunk_id;
Dave Airlie5176fdc2009-06-30 11:47:14 +1000198
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200199 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
200 p->chunk_relocs_idx = i;
201 }
202 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
203 p->chunk_ib_idx = i;
Dave Airlie5176fdc2009-06-30 11:47:14 +1000204 /* zero length IB isn't useful */
205 if (p->chunks[i].length_dw == 0)
206 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200207 }
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400208 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
209 p->chunk_const_ib_idx = i;
210 /* zero length CONST IB isn't useful */
211 if (p->chunks[i].length_dw == 0)
212 return -EINVAL;
213 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500214 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
215 p->chunk_flags_idx = i;
216 /* zero length flags aren't useful */
217 if (p->chunks[i].length_dw == 0)
218 return -EINVAL;
Marek Olšáke70f2242011-10-25 01:38:45 +0200219 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000220
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200221 p->chunks[i].length_dw = user_chunk.length_dw;
Dave Airlie513bcb42009-09-23 16:56:27 +1000222 p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200223
Dave Airlie513bcb42009-09-23 16:56:27 +1000224 cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse721604a2012-01-05 22:11:05 -0500225 if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
226 (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
Dave Airlie513bcb42009-09-23 16:56:27 +1000227 size = p->chunks[i].length_dw * sizeof(uint32_t);
228 p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
229 if (p->chunks[i].kdata == NULL) {
230 return -ENOMEM;
231 }
232 if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
233 p->chunks[i].user_ptr, size)) {
234 return -EFAULT;
235 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200236 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500237 p->cs_flags = p->chunks[i].kdata[0];
238 if (p->chunks[i].length_dw > 1)
239 ring = p->chunks[i].kdata[1];
240 if (p->chunks[i].length_dw > 2)
241 priority = (s32)p->chunks[i].kdata[2];
Marek Olšáke70f2242011-10-25 01:38:45 +0200242 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200243 }
244 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500245
246 if ((p->cs_flags & RADEON_CS_USE_VM) &&
Alex Deucher67e915e2012-01-06 09:38:15 -0500247 !p->rdev->vm_manager.enabled) {
248 DRM_ERROR("VM not active on asic!\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200249 return -EINVAL;
250 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200251
Alex Deucher1b5475d2012-03-20 17:18:16 -0400252 /* we only support VM on SI+ */
253 if ((p->rdev->family >= CHIP_TAHITI) &&
254 ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
255 DRM_ERROR("VM required on SI+!\n");
256 return -EINVAL;
257 }
258
Julia Lawallf48bb042012-03-17 18:03:29 +0100259 if (radeon_cs_get_ring(p, ring, priority))
Jerome Glisse721604a2012-01-05 22:11:05 -0500260 return -EINVAL;
Jerome Glisse721604a2012-01-05 22:11:05 -0500261
262
263 /* deal with non-vm */
264 if ((p->chunk_ib_idx != -1) &&
265 ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
266 (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
267 if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
268 DRM_ERROR("cs IB too big: %d\n",
269 p->chunks[p->chunk_ib_idx].length_dw);
270 return -EINVAL;
271 }
Dave Airlie6a7068b2012-04-03 16:23:41 +0100272 if ((p->rdev->flags & RADEON_IS_AGP)) {
273 p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
274 p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
275 if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
276 p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
277 kfree(p->chunks[i].kpage[0]);
278 kfree(p->chunks[i].kpage[1]);
279 return -ENOMEM;
280 }
281 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500282 p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
283 p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
284 p->chunks[p->chunk_ib_idx].last_copied_page = -1;
285 p->chunks[p->chunk_ib_idx].last_page_index =
286 ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
287 }
288
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200289 return 0;
290}
291
292/**
293 * cs_parser_fini() - clean parser states
294 * @parser: parser structure holding parsing context.
295 * @error: error number
296 *
297 * If error is set than unvalidate buffer, otherwise just free memory
298 * used by parsing context.
299 **/
300static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
301{
302 unsigned i;
303
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000304
305 if (!error && parser->ib)
306 ttm_eu_fence_buffer_objects(&parser->validated,
307 parser->ib->fence);
308 else
309 ttm_eu_backoff_reservation(&parser->validated);
310
Pauli Nieminenfcbc4512010-03-19 07:44:33 +0000311 if (parser->relocs != NULL) {
312 for (i = 0; i < parser->nrelocs; i++) {
313 if (parser->relocs[i].gobj)
314 drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
315 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200316 }
Michel Dänzer48e113e2009-09-15 17:09:32 +0200317 kfree(parser->track);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200318 kfree(parser->relocs);
319 kfree(parser->relocs_ptr);
320 for (i = 0; i < parser->nchunks; i++) {
321 kfree(parser->chunks[i].kdata);
Dave Airlie6a7068b2012-04-03 16:23:41 +0100322 if ((parser->rdev->flags & RADEON_IS_AGP)) {
323 kfree(parser->chunks[i].kpage[0]);
324 kfree(parser->chunks[i].kpage[1]);
325 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200326 }
327 kfree(parser->chunks);
328 kfree(parser->chunks_array);
329 radeon_ib_free(parser->rdev, &parser->ib);
Jerome Glisseb7f64132012-05-02 16:24:40 -0400330 if (parser->const_ib) {
331 radeon_ib_free(parser->rdev, &parser->const_ib);
332 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200333}
334
Jerome Glisse721604a2012-01-05 22:11:05 -0500335static int radeon_cs_ib_chunk(struct radeon_device *rdev,
336 struct radeon_cs_parser *parser)
337{
338 struct radeon_cs_chunk *ib_chunk;
339 int r;
340
341 if (parser->chunk_ib_idx == -1)
342 return 0;
343
344 if (parser->cs_flags & RADEON_CS_USE_VM)
345 return 0;
346
347 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
348 /* Copy the packet into the IB, the parser will read from the
349 * input memory (cached) and write to the IB (which can be
350 * uncached).
351 */
352 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
353 ib_chunk->length_dw * 4);
354 if (r) {
355 DRM_ERROR("Failed to get ib !\n");
356 return r;
357 }
358 parser->ib->length_dw = ib_chunk->length_dw;
Christian Königeb0c19c2012-02-23 15:18:44 +0100359 r = radeon_cs_parse(rdev, parser->ring, parser);
Jerome Glisse721604a2012-01-05 22:11:05 -0500360 if (r || parser->parser_error) {
361 DRM_ERROR("Invalid command stream !\n");
362 return r;
363 }
364 r = radeon_cs_finish_pages(parser);
365 if (r) {
366 DRM_ERROR("Invalid command stream !\n");
367 return r;
368 }
Christian König93504fc2012-01-05 22:11:06 -0500369 r = radeon_cs_sync_rings(parser);
370 if (r) {
371 DRM_ERROR("Failed to synchronize rings !\n");
372 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500373 parser->ib->vm_id = 0;
374 r = radeon_ib_schedule(rdev, parser->ib);
375 if (r) {
376 DRM_ERROR("Failed to schedule IB !\n");
377 }
378 return 0;
379}
380
381static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
382 struct radeon_vm *vm)
383{
384 struct radeon_bo_list *lobj;
385 struct radeon_bo *bo;
386 int r;
387
388 list_for_each_entry(lobj, &parser->validated, tv.head) {
389 bo = lobj->bo;
390 r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
391 if (r) {
392 return r;
393 }
394 }
395 return 0;
396}
397
398static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
399 struct radeon_cs_parser *parser)
400{
401 struct radeon_cs_chunk *ib_chunk;
402 struct radeon_fpriv *fpriv = parser->filp->driver_priv;
403 struct radeon_vm *vm = &fpriv->vm;
404 int r;
405
406 if (parser->chunk_ib_idx == -1)
407 return 0;
408
409 if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
410 return 0;
411
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400412 if ((rdev->family >= CHIP_TAHITI) &&
413 (parser->chunk_const_ib_idx != -1)) {
414 ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
415 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
416 DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
417 return -EINVAL;
418 }
419 r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
420 ib_chunk->length_dw * 4);
421 if (r) {
422 DRM_ERROR("Failed to get const ib !\n");
423 return r;
424 }
425 parser->const_ib->is_const_ib = true;
426 parser->const_ib->length_dw = ib_chunk->length_dw;
427 /* Copy the packet into the IB */
428 if (DRM_COPY_FROM_USER(parser->const_ib->ptr, ib_chunk->user_ptr,
429 ib_chunk->length_dw * 4)) {
430 return -EFAULT;
431 }
432 r = radeon_ring_ib_parse(rdev, parser->ring, parser->const_ib);
433 if (r) {
434 return r;
435 }
436 }
437
Jerome Glisse721604a2012-01-05 22:11:05 -0500438 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
439 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
440 DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
441 return -EINVAL;
442 }
443 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
444 ib_chunk->length_dw * 4);
445 if (r) {
446 DRM_ERROR("Failed to get ib !\n");
447 return r;
448 }
449 parser->ib->length_dw = ib_chunk->length_dw;
450 /* Copy the packet into the IB */
451 if (DRM_COPY_FROM_USER(parser->ib->ptr, ib_chunk->user_ptr,
452 ib_chunk->length_dw * 4)) {
453 return -EFAULT;
454 }
455 r = radeon_ring_ib_parse(rdev, parser->ring, parser->ib);
456 if (r) {
457 return r;
458 }
459
460 mutex_lock(&vm->mutex);
461 r = radeon_vm_bind(rdev, vm);
462 if (r) {
463 goto out;
464 }
465 r = radeon_bo_vm_update_pte(parser, vm);
466 if (r) {
467 goto out;
468 }
Christian König93504fc2012-01-05 22:11:06 -0500469 r = radeon_cs_sync_rings(parser);
470 if (r) {
471 DRM_ERROR("Failed to synchronize rings !\n");
472 }
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400473
474 if ((rdev->family >= CHIP_TAHITI) &&
475 (parser->chunk_const_ib_idx != -1)) {
476 parser->const_ib->vm_id = vm->id;
477 /* ib pool is bind at 0 in virtual address space to gpu_addr is the
478 * offset inside the pool bo
479 */
Christian König2e0d9912012-05-09 15:34:53 +0200480 parser->const_ib->gpu_addr = parser->const_ib->sa_bo->soffset;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400481 r = radeon_ib_schedule(rdev, parser->const_ib);
482 if (r)
483 goto out;
484 }
485
Jerome Glisse721604a2012-01-05 22:11:05 -0500486 parser->ib->vm_id = vm->id;
487 /* ib pool is bind at 0 in virtual address space to gpu_addr is the
488 * offset inside the pool bo
489 */
Christian König2e0d9912012-05-09 15:34:53 +0200490 parser->ib->gpu_addr = parser->ib->sa_bo->soffset;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400491 parser->ib->is_const_ib = false;
Jerome Glisse721604a2012-01-05 22:11:05 -0500492 r = radeon_ib_schedule(rdev, parser->ib);
493out:
494 if (!r) {
495 if (vm->fence) {
496 radeon_fence_unref(&vm->fence);
497 }
498 vm->fence = radeon_fence_ref(parser->ib->fence);
499 }
500 mutex_unlock(&fpriv->vm.mutex);
501 return r;
502}
503
Christian König6c6f4782012-05-02 15:11:19 +0200504static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
505{
506 if (r == -EDEADLK) {
507 r = radeon_gpu_reset(rdev);
508 if (!r)
509 r = -EAGAIN;
510 }
511 return r;
512}
513
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200514int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
515{
516 struct radeon_device *rdev = dev->dev_private;
517 struct radeon_cs_parser parser;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200518 int r;
519
Michel Dänzer7a1619b2011-11-10 18:57:26 +0100520 radeon_mutex_lock(&rdev->cs_mutex);
Jerome Glisse6b7746e2012-02-20 17:57:20 -0500521 if (!rdev->accel_working) {
522 radeon_mutex_unlock(&rdev->cs_mutex);
523 return -EBUSY;
524 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200525 /* initialize parser */
526 memset(&parser, 0, sizeof(struct radeon_cs_parser));
527 parser.filp = filp;
528 parser.rdev = rdev;
Jerome Glissec8c15ff2010-01-18 13:01:36 +0100529 parser.dev = rdev->dev;
Dave Airlie428c6e32011-06-08 19:58:29 +1000530 parser.family = rdev->family;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200531 r = radeon_cs_parser_init(&parser, data);
532 if (r) {
533 DRM_ERROR("Failed to initialize parser !\n");
534 radeon_cs_parser_fini(&parser, r);
Christian König6c6f4782012-05-02 15:11:19 +0200535 r = radeon_cs_handle_lockup(rdev, r);
Michel Dänzer7a1619b2011-11-10 18:57:26 +0100536 radeon_mutex_unlock(&rdev->cs_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200537 return r;
538 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200539 r = radeon_cs_parser_relocs(&parser);
540 if (r) {
Dave Airlie97f23b32010-03-19 10:33:44 +1000541 if (r != -ERESTARTSYS)
542 DRM_ERROR("Failed to parse relocation %d!\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200543 radeon_cs_parser_fini(&parser, r);
Christian König6c6f4782012-05-02 15:11:19 +0200544 r = radeon_cs_handle_lockup(rdev, r);
Michel Dänzer7a1619b2011-11-10 18:57:26 +0100545 radeon_mutex_unlock(&rdev->cs_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200546 return r;
547 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500548 r = radeon_cs_ib_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200549 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500550 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200551 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500552 r = radeon_cs_ib_vm_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200553 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500554 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200555 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500556out:
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200557 radeon_cs_parser_fini(&parser, r);
Christian König6c6f4782012-05-02 15:11:19 +0200558 r = radeon_cs_handle_lockup(rdev, r);
Michel Dänzer7a1619b2011-11-10 18:57:26 +0100559 radeon_mutex_unlock(&rdev->cs_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200560 return r;
561}
Dave Airlie513bcb42009-09-23 16:56:27 +1000562
563int radeon_cs_finish_pages(struct radeon_cs_parser *p)
564{
565 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
566 int i;
567 int size = PAGE_SIZE;
568
569 for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
570 if (i == ibc->last_page_index) {
571 size = (ibc->length_dw * 4) % PAGE_SIZE;
572 if (size == 0)
573 size = PAGE_SIZE;
574 }
575
576 if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
577 ibc->user_ptr + (i * PAGE_SIZE),
578 size))
579 return -EFAULT;
580 }
581 return 0;
582}
583
584int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
585{
586 int new_page;
Dave Airlie513bcb42009-09-23 16:56:27 +1000587 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
588 int i;
589 int size = PAGE_SIZE;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100590 bool copy1 = (p->rdev->flags & RADEON_IS_AGP) ? false : true;
Dave Airlie513bcb42009-09-23 16:56:27 +1000591
Dave Airliec5e617e2009-09-26 09:03:39 +1000592 for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
Dave Airlie513bcb42009-09-23 16:56:27 +1000593 if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
594 ibc->user_ptr + (i * PAGE_SIZE),
595 PAGE_SIZE)) {
596 p->parser_error = -EFAULT;
597 return 0;
598 }
599 }
600
Dave Airlie513bcb42009-09-23 16:56:27 +1000601 if (pg_idx == ibc->last_page_index) {
602 size = (ibc->length_dw * 4) % PAGE_SIZE;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100603 if (size == 0)
604 size = PAGE_SIZE;
Dave Airlie513bcb42009-09-23 16:56:27 +1000605 }
606
Dave Airlie6a7068b2012-04-03 16:23:41 +0100607 new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
608 if (copy1)
609 ibc->kpage[new_page] = p->ib->ptr + (pg_idx * (PAGE_SIZE / 4));
610
Dave Airlie513bcb42009-09-23 16:56:27 +1000611 if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
612 ibc->user_ptr + (pg_idx * PAGE_SIZE),
613 size)) {
614 p->parser_error = -EFAULT;
615 return 0;
616 }
617
Dave Airlie6a7068b2012-04-03 16:23:41 +0100618 /* copy to IB for non single case */
619 if (!copy1)
620 memcpy((void *)(p->ib->ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
Dave Airlie513bcb42009-09-23 16:56:27 +1000621
622 ibc->last_copied_page = pg_idx;
623 ibc->kpage_idx[new_page] = pg_idx;
624
625 return new_page;
626}