nouveau: add Android build support
[platform/upstream/libdrm.git] / radeon / radeon_cs_gem.c
1 /*
2  * Copyright © 2008 Jérôme Glisse
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17  * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  */
26 /*
27  * Authors:
28  *      Aapo Tahkola <aet@rasterburn.org>
29  *      Nicolai Haehnle <prefect_@gmx.net>
30  *      Jérôme Glisse <glisse@freedesktop.org>
31  */
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <pthread.h>
40 #include <sys/mman.h>
41 #include <sys/ioctl.h>
42 #include "radeon_cs.h"
43 #include "radeon_cs_int.h"
44 #include "radeon_bo_int.h"
45 #include "radeon_cs_gem.h"
46 #include "radeon_bo_gem.h"
47 #include "drm.h"
48 #include "libdrm.h"
49 #include "xf86drm.h"
50 #include "xf86atomic.h"
51 #include "radeon_drm.h"
52 #include "bof.h"
53
54 #define CS_BOF_DUMP 0
55
56 struct radeon_cs_manager_gem {
57     struct radeon_cs_manager    base;
58     uint32_t                    device_id;
59     unsigned                    nbof;
60 };
61
62 #pragma pack(1)
63 struct cs_reloc_gem {
64     uint32_t    handle;
65     uint32_t    read_domain;
66     uint32_t    write_domain;
67     uint32_t    flags;
68 };
69
70 #pragma pack()
71 #define RELOC_SIZE (sizeof(struct cs_reloc_gem) / sizeof(uint32_t))
72
73 struct cs_gem {
74     struct radeon_cs_int        base;
75     struct drm_radeon_cs        cs;
76     struct drm_radeon_cs_chunk  chunks[2];
77     unsigned                    nrelocs;
78     uint32_t                    *relocs;
79     struct radeon_bo_int        **relocs_bo;
80 };
81
82 static pthread_mutex_t id_mutex = PTHREAD_MUTEX_INITIALIZER;
83 static uint32_t cs_id_source = 0;
84
85 /**
86  * result is undefined if called with ~0
87  */
88 static uint32_t get_first_zero(const uint32_t n)
89 {
90     /* __builtin_ctz returns number of trailing zeros. */
91     return 1 << __builtin_ctz(~n);
92 }
93
94 /**
95  * Returns a free id for cs.
96  * If there is no free id we return zero
97  **/
98 static uint32_t generate_id(void)
99 {
100     uint32_t r = 0;
101     pthread_mutex_lock( &id_mutex );
102     /* check for free ids */
103     if (cs_id_source != ~r) {
104         /* find first zero bit */
105         r = get_first_zero(cs_id_source);
106
107         /* set id as reserved */
108         cs_id_source |= r;
109     }
110     pthread_mutex_unlock( &id_mutex );
111     return r;
112 }
113
114 /**
115  * Free the id for later reuse
116  **/
117 static void free_id(uint32_t id)
118 {
119     pthread_mutex_lock( &id_mutex );
120
121     cs_id_source &= ~id;
122
123     pthread_mutex_unlock( &id_mutex );
124 }
125
126 static struct radeon_cs_int *cs_gem_create(struct radeon_cs_manager *csm,
127                                        uint32_t ndw)
128 {
129     struct cs_gem *csg;
130
131     /* max cmd buffer size is 64Kb */
132     if (ndw > (64 * 1024 / 4)) {
133         return NULL;
134     }
135     csg = (struct cs_gem*)calloc(1, sizeof(struct cs_gem));
136     if (csg == NULL) {
137         return NULL;
138     }
139     csg->base.csm = csm;
140     csg->base.ndw = 64 * 1024 / 4;
141     csg->base.packets = (uint32_t*)calloc(1, 64 * 1024);
142     if (csg->base.packets == NULL) {
143         free(csg);
144         return NULL;
145     }
146     csg->base.relocs_total_size = 0;
147     csg->base.crelocs = 0;
148     csg->base.id = generate_id();
149     csg->nrelocs = 4096 / (4 * 4) ;
150     csg->relocs_bo = (struct radeon_bo_int**)calloc(1,
151                                                 csg->nrelocs*sizeof(void*));
152     if (csg->relocs_bo == NULL) {
153         free(csg->base.packets);
154         free(csg);
155         return NULL;
156     }
157     csg->base.relocs = csg->relocs = (uint32_t*)calloc(1, 4096);
158     if (csg->relocs == NULL) {
159         free(csg->relocs_bo);
160         free(csg->base.packets);
161         free(csg);
162         return NULL;
163     }
164     csg->chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
165     csg->chunks[0].length_dw = 0;
166     csg->chunks[0].chunk_data = (uint64_t)(uintptr_t)csg->base.packets;
167     csg->chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
168     csg->chunks[1].length_dw = 0;
169     csg->chunks[1].chunk_data = (uint64_t)(uintptr_t)csg->relocs;
170     return (struct radeon_cs_int*)csg;
171 }
172
173 static int cs_gem_write_reloc(struct radeon_cs_int *cs,
174                               struct radeon_bo *bo,
175                               uint32_t read_domain,
176                               uint32_t write_domain,
177                               uint32_t flags)
178 {
179     struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
180     struct cs_gem *csg = (struct cs_gem*)cs;
181     struct cs_reloc_gem *reloc;
182     uint32_t idx;
183     unsigned i;
184
185     assert(boi->space_accounted);
186
187     /* check domains */
188     if ((read_domain && write_domain) || (!read_domain && !write_domain)) {
189         /* in one CS a bo can only be in read or write domain but not
190          * in read & write domain at the same sime
191          */
192         return -EINVAL;
193     }
194     if (read_domain == RADEON_GEM_DOMAIN_CPU) {
195         return -EINVAL;
196     }
197     if (write_domain == RADEON_GEM_DOMAIN_CPU) {
198         return -EINVAL;
199     }
200     /* use bit field hash function to determine
201        if this bo is for sure not in this cs.*/
202     if ((atomic_read((atomic_t *)radeon_gem_get_reloc_in_cs(bo)) & cs->id)) {
203         /* check if bo is already referenced.
204          * Scanning from end to begin reduces cycles with mesa because
205          * it often relocates same shared dma bo again. */
206         for(i = cs->crelocs; i != 0;) {
207             --i;
208             idx = i * RELOC_SIZE;
209             reloc = (struct cs_reloc_gem*)&csg->relocs[idx];
210             if (reloc->handle == bo->handle) {
211                 /* Check domains must be in read or write. As we check already
212                  * checked that in argument one of the read or write domain was
213                  * set we only need to check that if previous reloc as the read
214                  * domain set then the read_domain should also be set for this
215                  * new relocation.
216                  */
217                 /* the DDX expects to read and write from same pixmap */
218                 if (write_domain && (reloc->read_domain & write_domain)) {
219                     reloc->read_domain = 0;
220                     reloc->write_domain = write_domain;
221                 } else if (read_domain & reloc->write_domain) {
222                     reloc->read_domain = 0;
223                 } else {
224                     if (write_domain != reloc->write_domain)
225                         return -EINVAL;
226                     if (read_domain != reloc->read_domain)
227                         return -EINVAL;
228                 }
229
230                 reloc->read_domain |= read_domain;
231                 reloc->write_domain |= write_domain;
232                 /* update flags */
233                 reloc->flags |= (flags & reloc->flags);
234                 /* write relocation packet */
235                 radeon_cs_write_dword((struct radeon_cs *)cs, 0xc0001000);
236                 radeon_cs_write_dword((struct radeon_cs *)cs, idx);
237                 return 0;
238             }
239         }
240     }
241     /* new relocation */
242     if (csg->base.crelocs >= csg->nrelocs) {
243         /* allocate more memory (TODO: should use a slab allocatore maybe) */
244         uint32_t *tmp, size;
245         size = ((csg->nrelocs + 1) * sizeof(struct radeon_bo*));
246         tmp = (uint32_t*)realloc(csg->relocs_bo, size);
247         if (tmp == NULL) {
248             return -ENOMEM;
249         }
250         csg->relocs_bo = (struct radeon_bo_int **)tmp;
251         size = ((csg->nrelocs + 1) * RELOC_SIZE * 4);
252         tmp = (uint32_t*)realloc(csg->relocs, size);
253         if (tmp == NULL) {
254             return -ENOMEM;
255         }
256         cs->relocs = csg->relocs = tmp;
257         csg->nrelocs += 1;
258         csg->chunks[1].chunk_data = (uint64_t)(uintptr_t)csg->relocs;
259     }
260     csg->relocs_bo[csg->base.crelocs] = boi;
261     idx = (csg->base.crelocs++) * RELOC_SIZE;
262     reloc = (struct cs_reloc_gem*)&csg->relocs[idx];
263     reloc->handle = bo->handle;
264     reloc->read_domain = read_domain;
265     reloc->write_domain = write_domain;
266     reloc->flags = flags;
267     csg->chunks[1].length_dw += RELOC_SIZE;
268     radeon_bo_ref(bo);
269     /* bo might be referenced from another context so have to use atomic opertions */
270     atomic_add((atomic_t *)radeon_gem_get_reloc_in_cs(bo), cs->id);
271     cs->relocs_total_size += boi->size;
272     radeon_cs_write_dword((struct radeon_cs *)cs, 0xc0001000);
273     radeon_cs_write_dword((struct radeon_cs *)cs, idx);
274     return 0;
275 }
276
277 static int cs_gem_begin(struct radeon_cs_int *cs,
278                         uint32_t ndw,
279                         const char *file,
280                         const char *func,
281                         int line)
282 {
283
284     if (cs->section_ndw) {
285         fprintf(stderr, "CS already in a section(%s,%s,%d)\n",
286                 cs->section_file, cs->section_func, cs->section_line);
287         fprintf(stderr, "CS can't start section(%s,%s,%d)\n",
288                 file, func, line);
289         return -EPIPE;
290     }
291     cs->section_ndw = ndw;
292     cs->section_cdw = 0;
293     cs->section_file = file;
294     cs->section_func = func;
295     cs->section_line = line;
296
297     if (cs->cdw + ndw > cs->ndw) {
298         uint32_t tmp, *ptr;
299
300         /* round up the required size to a multiple of 1024 */
301         tmp = (cs->cdw + ndw + 0x3FF) & (~0x3FF);
302         ptr = (uint32_t*)realloc(cs->packets, 4 * tmp);
303         if (ptr == NULL) {
304             return -ENOMEM;
305         }
306         cs->packets = ptr;
307         cs->ndw = tmp;
308     }
309     return 0;
310 }
311
312 static int cs_gem_end(struct radeon_cs_int *cs,
313                       const char *file,
314                       const char *func,
315                       int line)
316
317 {
318     if (!cs->section_ndw) {
319         fprintf(stderr, "CS no section to end at (%s,%s,%d)\n",
320                 file, func, line);
321         return -EPIPE;
322     }
323     if (cs->section_ndw != cs->section_cdw) {
324         fprintf(stderr, "CS section size missmatch start at (%s,%s,%d) %d vs %d\n",
325                 cs->section_file, cs->section_func, cs->section_line, cs->section_ndw, cs->section_cdw);
326         fprintf(stderr, "CS section end at (%s,%s,%d)\n",
327                 file, func, line);
328
329         /* We must reset the section even when there is error. */
330         cs->section_ndw = 0;
331         return -EPIPE;
332     }
333     cs->section_ndw = 0;
334     return 0;
335 }
336
337 #if CS_BOF_DUMP
338 static void cs_gem_dump_bof(struct radeon_cs_int *cs)
339 {
340     struct cs_gem *csg = (struct cs_gem*)cs;
341     struct radeon_cs_manager_gem *csm;
342     bof_t *bcs, *blob, *array, *bo, *size, *handle, *device_id, *root;
343     char tmp[256];
344     unsigned i;
345
346     csm = (struct radeon_cs_manager_gem *)cs->csm;
347     root = device_id = bcs = blob = array = bo = size = handle = NULL;
348     root = bof_object();
349     if (root == NULL)
350         goto out_err;
351     device_id = bof_int32(csm->device_id);
352     if (device_id == NULL)
353         return;
354     if (bof_object_set(root, "device_id", device_id))
355         goto out_err;
356     bof_decref(device_id);
357     device_id = NULL;
358     /* dump relocs */
359     blob = bof_blob(csg->nrelocs * 16, csg->relocs);
360     if (blob == NULL)
361         goto out_err;
362     if (bof_object_set(root, "reloc", blob))
363         goto out_err;
364     bof_decref(blob);
365     blob = NULL;
366     /* dump cs */
367     blob = bof_blob(cs->cdw * 4, cs->packets);
368     if (blob == NULL)
369         goto out_err;
370     if (bof_object_set(root, "pm4", blob))
371         goto out_err;
372     bof_decref(blob);
373     blob = NULL;
374     /* dump bo */
375     array = bof_array();
376     if (array == NULL)
377         goto out_err;
378     for (i = 0; i < csg->base.crelocs; i++) {
379         bo = bof_object();
380         if (bo == NULL)
381             goto out_err;
382         size = bof_int32(csg->relocs_bo[i]->size);
383         if (size == NULL)
384             goto out_err;
385         if (bof_object_set(bo, "size", size))
386             goto out_err;
387         bof_decref(size);
388         size = NULL;
389         handle = bof_int32(csg->relocs_bo[i]->handle);
390         if (handle == NULL)
391             goto out_err;
392         if (bof_object_set(bo, "handle", handle))
393             goto out_err;
394         bof_decref(handle);
395         handle = NULL;
396         radeon_bo_map((struct radeon_bo*)csg->relocs_bo[i], 0);
397         blob = bof_blob(csg->relocs_bo[i]->size, csg->relocs_bo[i]->ptr);
398         radeon_bo_unmap((struct radeon_bo*)csg->relocs_bo[i]);
399         if (blob == NULL)
400             goto out_err;
401         if (bof_object_set(bo, "data", blob))
402             goto out_err;
403         bof_decref(blob);
404         blob = NULL;
405         if (bof_array_append(array, bo))
406             goto out_err;
407         bof_decref(bo);
408         bo = NULL;
409     }
410     if (bof_object_set(root, "bo", array))
411         goto out_err;
412     sprintf(tmp, "d-0x%04X-%08d.bof", csm->device_id, csm->nbof++);
413     bof_dump_file(root, tmp);
414 out_err:
415     bof_decref(blob);
416     bof_decref(array);
417     bof_decref(bo);
418     bof_decref(size);
419     bof_decref(handle);
420     bof_decref(device_id);
421     bof_decref(root);
422 }
423 #endif
424
425 static int cs_gem_emit(struct radeon_cs_int *cs)
426 {
427     struct cs_gem *csg = (struct cs_gem*)cs;
428     uint64_t chunk_array[2];
429     unsigned i;
430     int r;
431
432     while (cs->cdw & 7)
433         radeon_cs_write_dword((struct radeon_cs *)cs, 0x80000000);
434
435 #if CS_BOF_DUMP
436     cs_gem_dump_bof(cs);
437 #endif
438     csg->chunks[0].length_dw = cs->cdw;
439
440     chunk_array[0] = (uint64_t)(uintptr_t)&csg->chunks[0];
441     chunk_array[1] = (uint64_t)(uintptr_t)&csg->chunks[1];
442
443     csg->cs.num_chunks = 2;
444     csg->cs.chunks = (uint64_t)(uintptr_t)chunk_array;
445
446     r = drmCommandWriteRead(cs->csm->fd, DRM_RADEON_CS,
447                             &csg->cs, sizeof(struct drm_radeon_cs));
448     for (i = 0; i < csg->base.crelocs; i++) {
449         csg->relocs_bo[i]->space_accounted = 0;
450         /* bo might be referenced from another context so have to use atomic opertions */
451         atomic_dec((atomic_t *)radeon_gem_get_reloc_in_cs((struct radeon_bo*)csg->relocs_bo[i]), cs->id);
452         radeon_bo_unref((struct radeon_bo *)csg->relocs_bo[i]);
453         csg->relocs_bo[i] = NULL;
454     }
455
456     cs->csm->read_used = 0;
457     cs->csm->vram_write_used = 0;
458     cs->csm->gart_write_used = 0;
459     return r;
460 }
461
462 static int cs_gem_destroy(struct radeon_cs_int *cs)
463 {
464     struct cs_gem *csg = (struct cs_gem*)cs;
465
466     free_id(cs->id);
467     free(csg->relocs_bo);
468     free(cs->relocs);
469     free(cs->packets);
470     free(cs);
471     return 0;
472 }
473
474 static int cs_gem_erase(struct radeon_cs_int *cs)
475 {
476     struct cs_gem *csg = (struct cs_gem*)cs;
477     unsigned i;
478
479     if (csg->relocs_bo) {
480         for (i = 0; i < csg->base.crelocs; i++) {
481             if (csg->relocs_bo[i]) {
482                 /* bo might be referenced from another context so have to use atomic opertions */
483                 atomic_dec((atomic_t *)radeon_gem_get_reloc_in_cs((struct radeon_bo*)csg->relocs_bo[i]), cs->id);
484                 radeon_bo_unref((struct radeon_bo *)csg->relocs_bo[i]);
485                 csg->relocs_bo[i] = NULL;
486             }
487         }
488     }
489     cs->relocs_total_size = 0;
490     cs->cdw = 0;
491     cs->section_ndw = 0;
492     cs->crelocs = 0;
493     csg->chunks[0].length_dw = 0;
494     csg->chunks[1].length_dw = 0;
495     return 0;
496 }
497
498 static int cs_gem_need_flush(struct radeon_cs_int *cs)
499 {
500     return 0; //(cs->relocs_total_size > (32*1024*1024));
501 }
502
503 static void cs_gem_print(struct radeon_cs_int *cs, FILE *file)
504 {
505     struct radeon_cs_manager_gem *csm;
506     unsigned int i;
507
508     csm = (struct radeon_cs_manager_gem *)cs->csm;
509     fprintf(file, "VENDORID:DEVICEID 0x%04X:0x%04X\n", 0x1002, csm->device_id);
510     for (i = 0; i < cs->cdw; i++) {
511         fprintf(file, "0x%08X\n", cs->packets[i]);
512     }
513 }
514
515 static struct radeon_cs_funcs radeon_cs_gem_funcs = {
516     cs_gem_create,
517     cs_gem_write_reloc,
518     cs_gem_begin,
519     cs_gem_end,
520     cs_gem_emit,
521     cs_gem_destroy,
522     cs_gem_erase,
523     cs_gem_need_flush,
524     cs_gem_print,
525 };
526
527 static int radeon_get_device_id(int fd, uint32_t *device_id)
528 {
529     struct drm_radeon_info info = {};
530     int r;
531
532     *device_id = 0;
533     info.request = RADEON_INFO_DEVICE_ID;
534     info.value = (uintptr_t)device_id;
535     r = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info,
536                             sizeof(struct drm_radeon_info));
537     return r;
538 }
539
540 drm_public struct radeon_cs_manager *radeon_cs_manager_gem_ctor(int fd)
541 {
542     struct radeon_cs_manager_gem *csm;
543
544     csm = calloc(1, sizeof(struct radeon_cs_manager_gem));
545     if (csm == NULL) {
546         return NULL;
547     }
548     csm->base.funcs = &radeon_cs_gem_funcs;
549     csm->base.fd = fd;
550     radeon_get_device_id(fd, &csm->device_id);
551     return &csm->base;
552 }
553
554 drm_public void radeon_cs_manager_gem_dtor(struct radeon_cs_manager *csm)
555 {
556     free(csm);
557 }