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