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