drm/radeon: remove r600 blit mutex v2
[linux-2.6-block.git] / drivers / gpu / drm / radeon / radeon_cs.c
CommitLineData
771fe6b9
JG
1/*
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];
16557f1e 61 for (j = 0; j < i; j++) {
771fe6b9
JG
62 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);
bf79cb91 75 return -ENOENT;
771fe6b9
JG
76 }
77 p->relocs_ptr[i] = &p->relocs[i];
7e4d15d9 78 p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
4c788679 79 p->relocs[i].lobj.bo = p->relocs[i].robj;
771fe6b9 80 p->relocs[i].lobj.wdomain = r->write_domain;
147666fb
TH
81 p->relocs[i].lobj.rdomain = r->read_domains;
82 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
771fe6b9
JG
83 p->relocs[i].handle = r->handle;
84 p->relocs[i].flags = r->flags;
4c788679 85 radeon_bo_list_add_object(&p->relocs[i].lobj,
147666fb 86 &p->validated);
93504fce 87
16557f1e
CK
88 } else
89 p->relocs[i].handle = 0;
771fe6b9 90 }
94429bb6 91 return radeon_bo_list_validate(&p->validated);
771fe6b9
JG
92}
93
721604a1
JG
94static 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:
8d5ef7b1
AD
106 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;
721604a1
JG
113 break;
114 }
115 return 0;
116}
117
93504fce
CK
118static int radeon_cs_sync_rings(struct radeon_cs_parser *p)
119{
cdac5504 120 bool sync_to_ring[RADEON_NUM_RINGS] = { };
8f676c4c 121 bool need_sync = false;
93504fce
CK
122 int i, r;
123
cdac5504 124 for (i = 0; i < p->nrelocs; i++) {
133f4cb3
JG
125 struct radeon_fence *fence;
126
cdac5504
CK
127 if (!p->relocs[i].robj || !p->relocs[i].robj->tbo.sync_obj)
128 continue;
129
133f4cb3
JG
130 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;
cdac5504
CK
134 }
135 }
136
8f676c4c
CK
137 if (!need_sync) {
138 return 0;
139 }
93504fce 140
68470ae7 141 r = radeon_semaphore_create(p->rdev, &p->ib->semaphore);
8f676c4c
CK
142 if (r) {
143 return r;
93504fce 144 }
8f676c4c 145
68470ae7 146 return radeon_semaphore_sync_rings(p->rdev, p->ib->semaphore,
8f676c4c 147 sync_to_ring, p->ring);
93504fce
CK
148}
149
771fe6b9
JG
150int 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;
721604a1
JG
154 unsigned size, i;
155 u32 ring = RADEON_CS_RING_GFX;
156 s32 priority = 0;
771fe6b9
JG
157
158 if (!cs->num_chunks) {
159 return 0;
160 }
161 /* get chunks */
162 INIT_LIST_HEAD(&p->validated);
163 p->idx = 0;
b7f6413a
JG
164 p->ib = NULL;
165 p->const_ib = NULL;
771fe6b9
JG
166 p->chunk_ib_idx = -1;
167 p->chunk_relocs_idx = -1;
721604a1 168 p->chunk_flags_idx = -1;
dfcf5f36 169 p->chunk_const_ib_idx = -1;
771fe6b9
JG
170 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 }
721604a1 179 p->cs_flags = 0;
771fe6b9
JG
180 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 }
5176fdc4
DA
195 p->chunks[i].length_dw = user_chunk.length_dw;
196 p->chunks[i].kdata = NULL;
771fe6b9 197 p->chunks[i].chunk_id = user_chunk.chunk_id;
5176fdc4 198
771fe6b9
JG
199 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;
5176fdc4
DA
204 /* zero length IB isn't useful */
205 if (p->chunks[i].length_dw == 0)
206 return -EINVAL;
771fe6b9 207 }
dfcf5f36
AD
208 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 }
721604a1
JG
214 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;
e70f224c 219 }
5176fdc4 220
771fe6b9 221 p->chunks[i].length_dw = user_chunk.length_dw;
513bcb46 222 p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
771fe6b9 223
513bcb46 224 cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
721604a1
JG
225 if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
226 (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
513bcb46
DA
227 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 }
e70f224c 236 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
721604a1
JG
237 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];
e70f224c 242 }
771fe6b9
JG
243 }
244 }
721604a1
JG
245
246 if ((p->cs_flags & RADEON_CS_USE_VM) &&
67e915e4
AD
247 !p->rdev->vm_manager.enabled) {
248 DRM_ERROR("VM not active on asic!\n");
771fe6b9
JG
249 return -EINVAL;
250 }
e70f224c 251
1b5475db
AD
252 /* 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
f48bb04a 259 if (radeon_cs_get_ring(p, ring, priority))
721604a1 260 return -EINVAL;
721604a1
JG
261
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 }
6a7068b4
DA
272 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 }
721604a1
JG
282 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
771fe6b9
JG
289 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
147666fb
TH
304
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
fcbc451b
PN
311 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 }
771fe6b9 316 }
48e113e5 317 kfree(parser->track);
771fe6b9
JG
318 kfree(parser->relocs);
319 kfree(parser->relocs_ptr);
320 for (i = 0; i < parser->nchunks; i++) {
321 kfree(parser->chunks[i].kdata);
6a7068b4
DA
322 if ((parser->rdev->flags & RADEON_IS_AGP)) {
323 kfree(parser->chunks[i].kpage[0]);
324 kfree(parser->chunks[i].kpage[1]);
325 }
771fe6b9
JG
326 }
327 kfree(parser->chunks);
328 kfree(parser->chunks_array);
329 radeon_ib_free(parser->rdev, &parser->ib);
b7f6413a
JG
330 if (parser->const_ib) {
331 radeon_ib_free(parser->rdev, &parser->const_ib);
332 }
771fe6b9
JG
333}
334
721604a1
JG
335static 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;
eb0c19c5 359 r = radeon_cs_parse(rdev, parser->ring, parser);
721604a1
JG
360 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 }
93504fce
CK
369 r = radeon_cs_sync_rings(parser);
370 if (r) {
371 DRM_ERROR("Failed to synchronize rings !\n");
372 }
721604a1
JG
373 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
dfcf5f36
AD
412 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
721604a1
JG
438 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 }
93504fce
CK
469 r = radeon_cs_sync_rings(parser);
470 if (r) {
471 DRM_ERROR("Failed to synchronize rings !\n");
472 }
dfcf5f36
AD
473
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 */
2e0d9910 480 parser->const_ib->gpu_addr = parser->const_ib->sa_bo->soffset;
dfcf5f36
AD
481 r = radeon_ib_schedule(rdev, parser->const_ib);
482 if (r)
483 goto out;
484 }
485
721604a1
JG
486 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 */
2e0d9910 490 parser->ib->gpu_addr = parser->ib->sa_bo->soffset;
dfcf5f36 491 parser->ib->is_const_ib = false;
721604a1
JG
492 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
6c6f4783
CK
504static 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
771fe6b9
JG
514int 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;
771fe6b9
JG
518 int r;
519
7a1619b9 520 radeon_mutex_lock(&rdev->cs_mutex);
6b7746e8
JG
521 if (!rdev->accel_working) {
522 radeon_mutex_unlock(&rdev->cs_mutex);
523 return -EBUSY;
524 }
771fe6b9
JG
525 /* initialize parser */
526 memset(&parser, 0, sizeof(struct radeon_cs_parser));
527 parser.filp = filp;
528 parser.rdev = rdev;
c8c15ff1 529 parser.dev = rdev->dev;
428c6e36 530 parser.family = rdev->family;
771fe6b9
JG
531 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);
6c6f4783 535 r = radeon_cs_handle_lockup(rdev, r);
7a1619b9 536 radeon_mutex_unlock(&rdev->cs_mutex);
771fe6b9
JG
537 return r;
538 }
771fe6b9
JG
539 r = radeon_cs_parser_relocs(&parser);
540 if (r) {
97f23b3d
DA
541 if (r != -ERESTARTSYS)
542 DRM_ERROR("Failed to parse relocation %d!\n", r);
771fe6b9 543 radeon_cs_parser_fini(&parser, r);
6c6f4783 544 r = radeon_cs_handle_lockup(rdev, r);
7a1619b9 545 radeon_mutex_unlock(&rdev->cs_mutex);
771fe6b9
JG
546 return r;
547 }
721604a1 548 r = radeon_cs_ib_chunk(rdev, &parser);
771fe6b9 549 if (r) {
721604a1 550 goto out;
771fe6b9 551 }
721604a1 552 r = radeon_cs_ib_vm_chunk(rdev, &parser);
771fe6b9 553 if (r) {
721604a1 554 goto out;
771fe6b9 555 }
721604a1 556out:
771fe6b9 557 radeon_cs_parser_fini(&parser, r);
6c6f4783 558 r = radeon_cs_handle_lockup(rdev, r);
7a1619b9 559 radeon_mutex_unlock(&rdev->cs_mutex);
771fe6b9
JG
560 return r;
561}
513bcb46
DA
562
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;
513bcb46
DA
587 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
588 int i;
589 int size = PAGE_SIZE;
6a7068b4 590 bool copy1 = (p->rdev->flags & RADEON_IS_AGP) ? false : true;
513bcb46 591
c5e617e2 592 for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
513bcb46
DA
593 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
513bcb46
DA
601 if (pg_idx == ibc->last_page_index) {
602 size = (ibc->length_dw * 4) % PAGE_SIZE;
6a7068b4
DA
603 if (size == 0)
604 size = PAGE_SIZE;
513bcb46
DA
605 }
606
6a7068b4
DA
607 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
513bcb46
DA
611 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
6a7068b4
DA
618 /* 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);
513bcb46
DA
621
622 ibc->last_copied_page = pg_idx;
623 ibc->kpage_idx[new_page] = pg_idx;
624
625 return new_page;
626}