drm/radeon: initial VCE support v4
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / radeon / radeon_cs.c
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 <drm/drmP.h>
28 #include <drm/radeon_drm.h>
29 #include "radeon_reg.h"
30 #include "radeon.h"
31 #include "radeon_trace.h"
32
33 static int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
34 {
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];
44         p->dma_reloc_idx = 0;
45         /* 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];
60                 for (j = 0; j < i; j++) {
61                         if (r->handle == p->relocs[j].handle) {
62                                 p->relocs_ptr[i] = &p->relocs[j];
63                                 duplicate = true;
64                                 break;
65                         }
66                 }
67                 if (duplicate) {
68                         p->relocs[i].handle = 0;
69                         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
84                 /* the first reloc of an UVD job is the msg and that must be in
85                    VRAM, also but everything into VRAM on AGP cards to avoid
86                    image corruptions */
87                 if (p->ring == R600_RING_TYPE_UVD_INDEX &&
88                     (i == 0 || drm_pci_device_is_agp(p->rdev->ddev))) {
89                         /* TODO: is this still needed for NI+ ? */
90                         p->relocs[i].lobj.domain =
91                                 RADEON_GEM_DOMAIN_VRAM;
92
93                         p->relocs[i].lobj.alt_domain =
94                                 RADEON_GEM_DOMAIN_VRAM;
95
96                 } else {
97                         uint32_t domain = r->write_domain ?
98                                 r->write_domain : r->read_domains;
99
100                         p->relocs[i].lobj.domain = domain;
101                         if (domain == RADEON_GEM_DOMAIN_VRAM)
102                                 domain |= RADEON_GEM_DOMAIN_GTT;
103                         p->relocs[i].lobj.alt_domain = domain;
104                 }
105
106                 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
107                 p->relocs[i].handle = r->handle;
108
109                 radeon_bo_list_add_object(&p->relocs[i].lobj,
110                                           &p->validated);
111         }
112         return radeon_bo_list_validate(&p->ticket, &p->validated, p->ring);
113 }
114
115 static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
116 {
117         p->priority = priority;
118
119         switch (ring) {
120         default:
121                 DRM_ERROR("unknown ring id: %d\n", ring);
122                 return -EINVAL;
123         case RADEON_CS_RING_GFX:
124                 p->ring = RADEON_RING_TYPE_GFX_INDEX;
125                 break;
126         case RADEON_CS_RING_COMPUTE:
127                 if (p->rdev->family >= CHIP_TAHITI) {
128                         if (p->priority > 0)
129                                 p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
130                         else
131                                 p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
132                 } else
133                         p->ring = RADEON_RING_TYPE_GFX_INDEX;
134                 break;
135         case RADEON_CS_RING_DMA:
136                 if (p->rdev->family >= CHIP_CAYMAN) {
137                         if (p->priority > 0)
138                                 p->ring = R600_RING_TYPE_DMA_INDEX;
139                         else
140                                 p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
141                 } else if (p->rdev->family >= CHIP_RV770) {
142                         p->ring = R600_RING_TYPE_DMA_INDEX;
143                 } else {
144                         return -EINVAL;
145                 }
146                 break;
147         case RADEON_CS_RING_UVD:
148                 p->ring = R600_RING_TYPE_UVD_INDEX;
149                 break;
150         case RADEON_CS_RING_VCE:
151                 /* TODO: only use the low priority ring for now */
152                 p->ring = TN_RING_TYPE_VCE1_INDEX;
153                 break;
154         }
155         return 0;
156 }
157
158 static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
159 {
160         int i;
161
162         for (i = 0; i < p->nrelocs; i++) {
163                 if (!p->relocs[i].robj)
164                         continue;
165
166                 radeon_semaphore_sync_to(p->ib.semaphore,
167                                          p->relocs[i].robj->tbo.sync_obj);
168         }
169 }
170
171 /* XXX: note that this is called from the legacy UMS CS ioctl as well */
172 int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
173 {
174         struct drm_radeon_cs *cs = data;
175         uint64_t *chunk_array_ptr;
176         unsigned size, i;
177         u32 ring = RADEON_CS_RING_GFX;
178         s32 priority = 0;
179
180         if (!cs->num_chunks) {
181                 return 0;
182         }
183         /* get chunks */
184         INIT_LIST_HEAD(&p->validated);
185         p->idx = 0;
186         p->ib.sa_bo = NULL;
187         p->ib.semaphore = NULL;
188         p->const_ib.sa_bo = NULL;
189         p->const_ib.semaphore = NULL;
190         p->chunk_ib_idx = -1;
191         p->chunk_relocs_idx = -1;
192         p->chunk_flags_idx = -1;
193         p->chunk_const_ib_idx = -1;
194         p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
195         if (p->chunks_array == NULL) {
196                 return -ENOMEM;
197         }
198         chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
199         if (copy_from_user(p->chunks_array, chunk_array_ptr,
200                                sizeof(uint64_t)*cs->num_chunks)) {
201                 return -EFAULT;
202         }
203         p->cs_flags = 0;
204         p->nchunks = cs->num_chunks;
205         p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
206         if (p->chunks == NULL) {
207                 return -ENOMEM;
208         }
209         for (i = 0; i < p->nchunks; i++) {
210                 struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
211                 struct drm_radeon_cs_chunk user_chunk;
212                 uint32_t __user *cdata;
213
214                 chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
215                 if (copy_from_user(&user_chunk, chunk_ptr,
216                                        sizeof(struct drm_radeon_cs_chunk))) {
217                         return -EFAULT;
218                 }
219                 p->chunks[i].length_dw = user_chunk.length_dw;
220                 p->chunks[i].chunk_id = user_chunk.chunk_id;
221                 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
222                         p->chunk_relocs_idx = i;
223                 }
224                 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
225                         p->chunk_ib_idx = i;
226                         /* zero length IB isn't useful */
227                         if (p->chunks[i].length_dw == 0)
228                                 return -EINVAL;
229                 }
230                 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
231                         p->chunk_const_ib_idx = i;
232                         /* zero length CONST IB isn't useful */
233                         if (p->chunks[i].length_dw == 0)
234                                 return -EINVAL;
235                 }
236                 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
237                         p->chunk_flags_idx = i;
238                         /* zero length flags aren't useful */
239                         if (p->chunks[i].length_dw == 0)
240                                 return -EINVAL;
241                 }
242
243                 size = p->chunks[i].length_dw;
244                 cdata = (void __user *)(unsigned long)user_chunk.chunk_data;
245                 p->chunks[i].user_ptr = cdata;
246                 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB)
247                         continue;
248
249                 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
250                         if (!p->rdev || !(p->rdev->flags & RADEON_IS_AGP))
251                                 continue;
252                 }
253
254                 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t));
255                 size *= sizeof(uint32_t);
256                 if (p->chunks[i].kdata == NULL) {
257                         return -ENOMEM;
258                 }
259                 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
260                         return -EFAULT;
261                 }
262                 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
263                         p->cs_flags = p->chunks[i].kdata[0];
264                         if (p->chunks[i].length_dw > 1)
265                                 ring = p->chunks[i].kdata[1];
266                         if (p->chunks[i].length_dw > 2)
267                                 priority = (s32)p->chunks[i].kdata[2];
268                 }
269         }
270
271         /* these are KMS only */
272         if (p->rdev) {
273                 if ((p->cs_flags & RADEON_CS_USE_VM) &&
274                     !p->rdev->vm_manager.enabled) {
275                         DRM_ERROR("VM not active on asic!\n");
276                         return -EINVAL;
277                 }
278
279                 if (radeon_cs_get_ring(p, ring, priority))
280                         return -EINVAL;
281
282                 /* we only support VM on some SI+ rings */
283                 if ((p->rdev->asic->ring[p->ring]->cs_parse == NULL) &&
284                    ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
285                         DRM_ERROR("Ring %d requires VM!\n", p->ring);
286                         return -EINVAL;
287                 }
288         }
289
290         return 0;
291 }
292
293 /**
294  * cs_parser_fini() - clean parser states
295  * @parser:     parser structure holding parsing context.
296  * @error:      error number
297  *
298  * If error is set than unvalidate buffer, otherwise just free memory
299  * used by parsing context.
300  **/
301 static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error, bool backoff)
302 {
303         unsigned i;
304
305         if (!error) {
306                 ttm_eu_fence_buffer_objects(&parser->ticket,
307                                             &parser->validated,
308                                             parser->ib.fence);
309         } else if (backoff) {
310                 ttm_eu_backoff_reservation(&parser->ticket,
311                                            &parser->validated);
312         }
313
314         if (parser->relocs != NULL) {
315                 for (i = 0; i < parser->nrelocs; i++) {
316                         if (parser->relocs[i].gobj)
317                                 drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
318                 }
319         }
320         kfree(parser->track);
321         kfree(parser->relocs);
322         kfree(parser->relocs_ptr);
323         for (i = 0; i < parser->nchunks; i++)
324                 drm_free_large(parser->chunks[i].kdata);
325         kfree(parser->chunks);
326         kfree(parser->chunks_array);
327         radeon_ib_free(parser->rdev, &parser->ib);
328         radeon_ib_free(parser->rdev, &parser->const_ib);
329 }
330
331 static int radeon_cs_ib_chunk(struct radeon_device *rdev,
332                               struct radeon_cs_parser *parser)
333 {
334         int r;
335
336         if (parser->chunk_ib_idx == -1)
337                 return 0;
338
339         if (parser->cs_flags & RADEON_CS_USE_VM)
340                 return 0;
341
342         r = radeon_cs_parse(rdev, parser->ring, parser);
343         if (r || parser->parser_error) {
344                 DRM_ERROR("Invalid command stream !\n");
345                 return r;
346         }
347
348         if (parser->ring == R600_RING_TYPE_UVD_INDEX)
349                 radeon_uvd_note_usage(rdev);
350
351         radeon_cs_sync_rings(parser);
352         r = radeon_ib_schedule(rdev, &parser->ib, NULL);
353         if (r) {
354                 DRM_ERROR("Failed to schedule IB !\n");
355         }
356         return r;
357 }
358
359 static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
360                                    struct radeon_vm *vm)
361 {
362         struct radeon_device *rdev = parser->rdev;
363         struct radeon_bo_list *lobj;
364         struct radeon_bo *bo;
365         int r;
366
367         r = radeon_vm_bo_update(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
368         if (r) {
369                 return r;
370         }
371         list_for_each_entry(lobj, &parser->validated, tv.head) {
372                 bo = lobj->bo;
373                 r = radeon_vm_bo_update(parser->rdev, vm, bo, &bo->tbo.mem);
374                 if (r) {
375                         return r;
376                 }
377         }
378         return 0;
379 }
380
381 static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
382                                  struct radeon_cs_parser *parser)
383 {
384         struct radeon_fpriv *fpriv = parser->filp->driver_priv;
385         struct radeon_vm *vm = &fpriv->vm;
386         int r;
387
388         if (parser->chunk_ib_idx == -1)
389                 return 0;
390         if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
391                 return 0;
392
393         if (parser->const_ib.length_dw) {
394                 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
395                 if (r) {
396                         return r;
397                 }
398         }
399
400         r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
401         if (r) {
402                 return r;
403         }
404
405         if (parser->ring == R600_RING_TYPE_UVD_INDEX)
406                 radeon_uvd_note_usage(rdev);
407
408         mutex_lock(&rdev->vm_manager.lock);
409         mutex_lock(&vm->mutex);
410         r = radeon_vm_alloc_pt(rdev, vm);
411         if (r) {
412                 goto out;
413         }
414         r = radeon_bo_vm_update_pte(parser, vm);
415         if (r) {
416                 goto out;
417         }
418         radeon_cs_sync_rings(parser);
419         radeon_semaphore_sync_to(parser->ib.semaphore, vm->fence);
420         radeon_semaphore_sync_to(parser->ib.semaphore,
421                                  radeon_vm_grab_id(rdev, vm, parser->ring));
422
423         if ((rdev->family >= CHIP_TAHITI) &&
424             (parser->chunk_const_ib_idx != -1)) {
425                 r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
426         } else {
427                 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
428         }
429
430         if (!r) {
431                 radeon_vm_fence(rdev, vm, parser->ib.fence);
432         }
433
434 out:
435         radeon_vm_add_to_lru(rdev, vm);
436         mutex_unlock(&vm->mutex);
437         mutex_unlock(&rdev->vm_manager.lock);
438         return r;
439 }
440
441 static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
442 {
443         if (r == -EDEADLK) {
444                 r = radeon_gpu_reset(rdev);
445                 if (!r)
446                         r = -EAGAIN;
447         }
448         return r;
449 }
450
451 static int radeon_cs_ib_fill(struct radeon_device *rdev, struct radeon_cs_parser *parser)
452 {
453         struct radeon_cs_chunk *ib_chunk;
454         struct radeon_vm *vm = NULL;
455         int r;
456
457         if (parser->chunk_ib_idx == -1)
458                 return 0;
459
460         if (parser->cs_flags & RADEON_CS_USE_VM) {
461                 struct radeon_fpriv *fpriv = parser->filp->driver_priv;
462                 vm = &fpriv->vm;
463
464                 if ((rdev->family >= CHIP_TAHITI) &&
465                     (parser->chunk_const_ib_idx != -1)) {
466                         ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
467                         if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
468                                 DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
469                                 return -EINVAL;
470                         }
471                         r =  radeon_ib_get(rdev, parser->ring, &parser->const_ib,
472                                            vm, ib_chunk->length_dw * 4);
473                         if (r) {
474                                 DRM_ERROR("Failed to get const ib !\n");
475                                 return r;
476                         }
477                         parser->const_ib.is_const_ib = true;
478                         parser->const_ib.length_dw = ib_chunk->length_dw;
479                         if (copy_from_user(parser->const_ib.ptr,
480                                                ib_chunk->user_ptr,
481                                                ib_chunk->length_dw * 4))
482                                 return -EFAULT;
483                 }
484
485                 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
486                 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
487                         DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
488                         return -EINVAL;
489                 }
490         }
491         ib_chunk = &parser->chunks[parser->chunk_ib_idx];
492
493         r =  radeon_ib_get(rdev, parser->ring, &parser->ib,
494                            vm, ib_chunk->length_dw * 4);
495         if (r) {
496                 DRM_ERROR("Failed to get ib !\n");
497                 return r;
498         }
499         parser->ib.length_dw = ib_chunk->length_dw;
500         if (ib_chunk->kdata)
501                 memcpy(parser->ib.ptr, ib_chunk->kdata, ib_chunk->length_dw * 4);
502         else if (copy_from_user(parser->ib.ptr, ib_chunk->user_ptr, ib_chunk->length_dw * 4))
503                 return -EFAULT;
504         return 0;
505 }
506
507 int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
508 {
509         struct radeon_device *rdev = dev->dev_private;
510         struct radeon_cs_parser parser;
511         int r;
512
513         down_read(&rdev->exclusive_lock);
514         if (!rdev->accel_working) {
515                 up_read(&rdev->exclusive_lock);
516                 return -EBUSY;
517         }
518         /* initialize parser */
519         memset(&parser, 0, sizeof(struct radeon_cs_parser));
520         parser.filp = filp;
521         parser.rdev = rdev;
522         parser.dev = rdev->dev;
523         parser.family = rdev->family;
524         r = radeon_cs_parser_init(&parser, data);
525         if (r) {
526                 DRM_ERROR("Failed to initialize parser !\n");
527                 radeon_cs_parser_fini(&parser, r, false);
528                 up_read(&rdev->exclusive_lock);
529                 r = radeon_cs_handle_lockup(rdev, r);
530                 return r;
531         }
532
533         r = radeon_cs_ib_fill(rdev, &parser);
534         if (!r) {
535                 r = radeon_cs_parser_relocs(&parser);
536                 if (r && r != -ERESTARTSYS)
537                         DRM_ERROR("Failed to parse relocation %d!\n", r);
538         }
539
540         if (r) {
541                 radeon_cs_parser_fini(&parser, r, false);
542                 up_read(&rdev->exclusive_lock);
543                 r = radeon_cs_handle_lockup(rdev, r);
544                 return r;
545         }
546
547         trace_radeon_cs(&parser);
548
549         r = radeon_cs_ib_chunk(rdev, &parser);
550         if (r) {
551                 goto out;
552         }
553         r = radeon_cs_ib_vm_chunk(rdev, &parser);
554         if (r) {
555                 goto out;
556         }
557 out:
558         radeon_cs_parser_fini(&parser, r, true);
559         up_read(&rdev->exclusive_lock);
560         r = radeon_cs_handle_lockup(rdev, r);
561         return r;
562 }
563
564 /**
565  * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
566  * @parser:     parser structure holding parsing context.
567  * @pkt:        where to store packet information
568  *
569  * Assume that chunk_ib_index is properly set. Will return -EINVAL
570  * if packet is bigger than remaining ib size. or if packets is unknown.
571  **/
572 int radeon_cs_packet_parse(struct radeon_cs_parser *p,
573                            struct radeon_cs_packet *pkt,
574                            unsigned idx)
575 {
576         struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
577         struct radeon_device *rdev = p->rdev;
578         uint32_t header;
579
580         if (idx >= ib_chunk->length_dw) {
581                 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
582                           idx, ib_chunk->length_dw);
583                 return -EINVAL;
584         }
585         header = radeon_get_ib_value(p, idx);
586         pkt->idx = idx;
587         pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
588         pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
589         pkt->one_reg_wr = 0;
590         switch (pkt->type) {
591         case RADEON_PACKET_TYPE0:
592                 if (rdev->family < CHIP_R600) {
593                         pkt->reg = R100_CP_PACKET0_GET_REG(header);
594                         pkt->one_reg_wr =
595                                 RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
596                 } else
597                         pkt->reg = R600_CP_PACKET0_GET_REG(header);
598                 break;
599         case RADEON_PACKET_TYPE3:
600                 pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
601                 break;
602         case RADEON_PACKET_TYPE2:
603                 pkt->count = -1;
604                 break;
605         default:
606                 DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
607                 return -EINVAL;
608         }
609         if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
610                 DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
611                           pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
612                 return -EINVAL;
613         }
614         return 0;
615 }
616
617 /**
618  * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
619  * @p:          structure holding the parser context.
620  *
621  * Check if the next packet is NOP relocation packet3.
622  **/
623 bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
624 {
625         struct radeon_cs_packet p3reloc;
626         int r;
627
628         r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
629         if (r)
630                 return false;
631         if (p3reloc.type != RADEON_PACKET_TYPE3)
632                 return false;
633         if (p3reloc.opcode != RADEON_PACKET3_NOP)
634                 return false;
635         return true;
636 }
637
638 /**
639  * radeon_cs_dump_packet() - dump raw packet context
640  * @p:          structure holding the parser context.
641  * @pkt:        structure holding the packet.
642  *
643  * Used mostly for debugging and error reporting.
644  **/
645 void radeon_cs_dump_packet(struct radeon_cs_parser *p,
646                            struct radeon_cs_packet *pkt)
647 {
648         volatile uint32_t *ib;
649         unsigned i;
650         unsigned idx;
651
652         ib = p->ib.ptr;
653         idx = pkt->idx;
654         for (i = 0; i <= (pkt->count + 1); i++, idx++)
655                 DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
656 }
657
658 /**
659  * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
660  * @parser:             parser structure holding parsing context.
661  * @data:               pointer to relocation data
662  * @offset_start:       starting offset
663  * @offset_mask:        offset mask (to align start offset on)
664  * @reloc:              reloc informations
665  *
666  * Check if next packet is relocation packet3, do bo validation and compute
667  * GPU offset using the provided start.
668  **/
669 int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
670                                 struct radeon_cs_reloc **cs_reloc,
671                                 int nomm)
672 {
673         struct radeon_cs_chunk *relocs_chunk;
674         struct radeon_cs_packet p3reloc;
675         unsigned idx;
676         int r;
677
678         if (p->chunk_relocs_idx == -1) {
679                 DRM_ERROR("No relocation chunk !\n");
680                 return -EINVAL;
681         }
682         *cs_reloc = NULL;
683         relocs_chunk = &p->chunks[p->chunk_relocs_idx];
684         r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
685         if (r)
686                 return r;
687         p->idx += p3reloc.count + 2;
688         if (p3reloc.type != RADEON_PACKET_TYPE3 ||
689             p3reloc.opcode != RADEON_PACKET3_NOP) {
690                 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
691                           p3reloc.idx);
692                 radeon_cs_dump_packet(p, &p3reloc);
693                 return -EINVAL;
694         }
695         idx = radeon_get_ib_value(p, p3reloc.idx + 1);
696         if (idx >= relocs_chunk->length_dw) {
697                 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
698                           idx, relocs_chunk->length_dw);
699                 radeon_cs_dump_packet(p, &p3reloc);
700                 return -EINVAL;
701         }
702         /* FIXME: we assume reloc size is 4 dwords */
703         if (nomm) {
704                 *cs_reloc = p->relocs;
705                 (*cs_reloc)->lobj.gpu_offset =
706                         (u64)relocs_chunk->kdata[idx + 3] << 32;
707                 (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
708         } else
709                 *cs_reloc = p->relocs_ptr[(idx / 4)];
710         return 0;
711 }