summaryrefslogtreecommitdiff
path: root/replayx_drv.c
blob: e7b925b5a5733281d04ed19e9df2c29e83cc0389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright 2012 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *      Jerome Glisse
 */
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "replayx.h"
#include "xf86drm.h"
#include "radeon_drm.h"
#include "radeon_family.h"

int ctx_bo(struct ctx *ctx, struct ctx_bo *bo, void *data)
{
    struct drm_radeon_gem_create args;
    int r;

    args.size = bo->size;
    args.alignment = bo->alignment;
    args.initial_domain = RADEON_GEM_DOMAIN_CPU;
    args.flags = bo->flags;
    args.handle = 0;
    r = drmCommandWriteRead(ctx->fd, DRM_RADEON_GEM_CREATE, &args, sizeof(args));
    bo->handle = args.handle;
    if (r) {
        fprintf(stderr, "Failed to allocate :\n");
        fprintf(stderr, "   size      : %d bytes\n", bo->size);
        fprintf(stderr, "   alignment : %d bytes\n", bo->alignment);
    }
    if (data) {
        r = ctx_bo_map(ctx, bo);
        if (r) {
            return r;
        }
        memcpy(bo->data, data, bo->size);
    }
    return r;
}

int ctx_bo_map(struct ctx *ctx, struct ctx_bo *bo)
{
    struct drm_radeon_gem_mmap args;
    void *ptr;
    int r;

    if (bo->mapcount++ != 0) {
        return 0;
    }
    /* Zero out args to make valgrind happy */
    memset(&args, 0, sizeof(args));
    args.handle = bo->handle;
    args.offset = 0;
    args.size = (uint64_t)bo->size;
    r = drmCommandWriteRead(ctx->fd, DRM_RADEON_GEM_MMAP, &args, sizeof(args));
    if (r) {
        fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
            bo, bo->handle, r);
        return r;
    }
    ptr = mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED, ctx->fd, args.addr_ptr);
    if (ptr == MAP_FAILED) {
        fprintf(stderr, "%s failed to map bo\n", __func__);
        return -errno;
    }
    bo->data = ptr;
    return 0;
}

void ctx_bo_unmap(struct ctx_bo *bo)
{
    if (--bo->mapcount > 0) {
        return;
    }
    munmap(bo->data, bo->size);
    bo->data = NULL;
}

void ctx_bo_free(struct ctx *ctx, struct ctx_bo *bo)
{
    struct drm_gem_close args;

    if (bo == NULL) {
        return;
    }

    if (bo->data) {
        munmap(bo->data, bo->size);
    }
    memset(&args, 0, sizeof(args));
    args.handle = bo->handle;
    drmIoctl(ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
    memset(bo, 0, sizeof(struct ctx_bo));
}

int ctx_bo_wait(struct ctx *ctx, struct ctx_bo *bo)
{
    struct drm_radeon_gem_wait_idle args;
    int ret;

    /* Zero out args to make valgrind happy */
    memset(&args, 0, sizeof(args));
    args.handle = bo->handle;
    do {
        ret = drmCommandWriteRead(ctx->fd, DRM_RADEON_GEM_WAIT_IDLE,
                                  &args, sizeof(args));
    } while (ret == -EBUSY);
    return ret;
}

int ctx_rati_load(struct ctx *ctx, const char *filename)
{
    unsigned i;
    int r;

    r = rati_file_read(&ctx->rfile, filename);
    if (r) {
        fprintf(stderr, "failed reading %s\n", filename);
        return r;
    }

    ctx->ntarget = 0;
    ctx->nbos = ctx->rfile.header.ndata_buffers;
    ctx->bos = calloc(1, sizeof(*ctx->bos) * ctx->nbos);
    ctx->relocs = calloc(1, sizeof(*ctx->relocs) * ctx->nbos);
    ctx->target = calloc(1, sizeof(void*) * ctx->nbos);
    if (ctx->bos == NULL || ctx->relocs == NULL || ctx->target == NULL) {
        free(ctx->bos);
        free(ctx->relocs);
        return -ENOMEM;
    }

    for (i = 0; i < ctx->nbos; ++i) {
        ctx->bos[i].size = ctx->rfile.data_buffer[i].size;
        ctx->bos[i].alignment = ctx->rfile.data_buffer[i].alignment;
        r = ctx_bo(ctx, &ctx->bos[i], ctx->rfile.data_buffer_ptr[i]);
        if (r) {
            return r;
        }
        ctx->relocs[i].handle = ctx->bos[i].handle;
        ctx->relocs[i].read_domain = RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT;
        ctx->relocs[i].write_domain = RADEON_GEM_DOMAIN_VRAM | RADEON_GEM_DOMAIN_GTT;
        ctx->relocs[i].flags = 0;
    }

    return 0;
}

int ctx_cs(struct ctx *ctx, void *cs, unsigned ndw, void *relocs, unsigned nrelocs)
{
    struct drm_radeon_cs drmib;
    struct drm_radeon_cs_chunk chunks[2];
    uint64_t chunk_array[2];
    int r;

    drmib.num_chunks = 2;
    drmib.chunks = (uint64_t)(uintptr_t)chunk_array;
    chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
    chunks[0].length_dw = ndw;
    chunks[0].chunk_data = (uintptr_t)cs;
    chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
    chunks[1].length_dw = nrelocs * 4;
    chunks[1].chunk_data = (uintptr_t)relocs;
    chunk_array[0] = (uintptr_t)&chunks[0];
    chunk_array[1] = (uintptr_t)&chunks[1];
#if 1
    r = drmCommandWriteRead(ctx->fd, DRM_RADEON_CS, &drmib, sizeof(struct drm_radeon_cs));
#endif
    return r;
}

int ctx_cs_rati(struct ctx *ctx)
{
    return ctx_cs(ctx, ctx->rfile.cmd_buffer_ptr[0],
                  ctx->rfile.cmd_buffer[0].ndw,
                  ctx->relocs, ctx->nbos);
}

static int ctx_get_drm_value(struct ctx *ctx, unsigned request, uint32_t *out)
{
    struct drm_radeon_info info;

    memset(&info, 0, sizeof(info));
    info.value = (unsigned long)out;
    info.request = request;
    return drmCommandWriteRead(ctx->fd, DRM_RADEON_INFO, &info, sizeof(info));
}

int ctx_drv_init(struct ctx *ctx)
{
    const struct radeon_chipinfo *chipinfo;
    int r;

    r = ctx_get_drm_value(ctx, RADEON_INFO_DEVICE_ID, &ctx->pciid);
    if (r) {
        return r;
    }
    ctx->pciid |= 0x10020000;
    chipinfo = radeon_chipinfo_from_pciid(ctx->pciid);
    if (chipinfo == NULL) {
        fprintf(stderr, "%s unknown gpu 0x%08x\n", __func__, ctx->pciid);
        return -EINVAL;
    }
    ctx->family = chipinfo->family;

    switch (ctx->family) {
    case CHIP_R600:
    case CHIP_RV610:
    case CHIP_RV630:
    case CHIP_RV670:
    case CHIP_RV620:
    case CHIP_RV635:
    case CHIP_RS780:
    case CHIP_RS880:
        ctx->drv = _r6xx_drv;
        break;
    case CHIP_RV770:
    case CHIP_RV730:
    case CHIP_RV710:
    case CHIP_RV740:
    case CHIP_R100:
    case CHIP_RV100:
    case CHIP_RS100:
    case CHIP_RV200:
    case CHIP_RS200:
    case CHIP_R200:
    case CHIP_RV250:
    case CHIP_RS300:
    case CHIP_RV280:
    case CHIP_R300:
    case CHIP_R350:
    case CHIP_RV350:
    case CHIP_RV380:
    case CHIP_R420:
    case CHIP_R423:
    case CHIP_RV410:
    case CHIP_RS400:
    case CHIP_RS480:
    case CHIP_RS600:
    case CHIP_RS690:
    case CHIP_RS740:
    case CHIP_RV515:
    case CHIP_R520:
    case CHIP_RV530:
    case CHIP_RV560:
    case CHIP_RV570:
    case CHIP_R580:
    case CHIP_CEDAR:
    case CHIP_REDWOOD:
    case CHIP_JUNIPER:
    case CHIP_CYPRESS:
    case CHIP_HEMLOCK:
    case CHIP_PALM:
    case CHIP_SUMO:
    case CHIP_SUMO2:
    case CHIP_BARTS:
    case CHIP_TURKS:
    case CHIP_CAICOS:
    case CHIP_CAYMAN:
    case CHIP_ARUBA:
    case CHIP_TAHITI:
    case CHIP_PITCAIRN:
    case CHIP_VERDE:
    default:
        fprintf(stderr, "%s unsupported gpu %s 0x%08x\n", __func__, chipinfo->name, ctx->pciid);
        return -EINVAL;
    }

    r = ctx->drv.compatible(ctx);
    if (r) {
        return r;
    }

    return ctx->drv.blit_init(ctx);
}

void ctx_drv_fini(struct ctx *ctx)
{
    ctx->drv.blit_fini(ctx);
}