summaryrefslogtreecommitdiff
path: root/compote.c
blob: ba825c4f67de7f3b25d2d6e40c078f9274f6e930 (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
/*
 * Copyright 2017 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors: Jérôme Glisse <jglisse@redhat.com>
 */
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>

#include "compote.h"
#include "compote-uapi.h"

static inline uint32_t nvk_sq_cmd(unsigned subc, unsigned method, unsigned len)
{
    return ((method >> 2) & 0x1fff) |
           ((len & 0xfff) << 16) |
           ((subc & 0x7) << 13) |
           (0x1 << 29);
}

static inline uint32_t nvk_ni_cmd(unsigned subc, unsigned method, unsigned len)
{
    return ((method >> 2) & 0x1fff) |
           ((len & 0xfff) << 16) |
           ((subc & 0x7) << 13) |
           (0x3 << 29);
}

int compote_context_new(compote_context_t **ctxp)
{
    struct compote_ioctl_channel_alloc arg;
    compote_context_t *ctx;
    int fd, ret;

    fd = open("/dev/compote", O_RDWR, 0);
    if (fd < 0) {
        fprintf(stderr, "could not open compote device\n");
        return -ENODEV;
    }

    ctx = calloc(1, sizeof(*ctx));
    if (ctx == NULL) {
        close(fd);
        return -ENOMEM;
    }

    ctx->channel.id = -1UL;
    ctx->fd = fd;

    ret = compote_context_ioctl(ctx, COMPOTE_IOCTL_CHAN_ALLOC, &arg);
    if (ret) {
        compote_context_del(&ctx);
        return ret;
    }
    ctx->channel.id = arg.channel;

    *ctxp = ctx;
    return 0;
}

void compote_context_del(compote_context_t **ctxp)
{
    compote_context_t *ctx = *ctxp;

    *ctxp = NULL;
    if (ctx->channel.id != -1UL) {
        struct compote_ioctl_channel_free arg;

        arg.channel = ctx->channel.id;
        compote_context_ioctl(ctx, COMPOTE_IOCTL_CHAN_FREE, &arg);
    }

    close(ctx->fd);
    free(ctx);
}

int compote_context_ioctl(compote_context_t *ctx, int command, void *arg)
{
    do {
        int ret;

        ret = ioctl(ctx->fd, command, arg);
        if (ret && errno != EINTR) {
            printf("ret %d errno %d\n", ret, errno);
            return ret;
        }
    } while (errno == EINTR);

    return 0;
}

int compote_mo_new(compote_context_t *ctx, compote_mo_t **mop, uint64_t nbytes)
{
    static const unsigned long mask = ~((1UL << 12) - 1);
    static unsigned long addr = 1UL << 40;
    struct compote_ioctl_mem_alloc arg;
    compote_mo_t *mo;
    int ret;

    if (addr < nbytes) {
        return -ENOMEM;
    }

    *mop = NULL;
    mo = calloc(1, sizeof(*mo));
    if (mo == NULL) {
        return -ENOMEM;
    }

    arg.nbytes = nbytes;
    ret = compote_context_ioctl(ctx, COMPOTE_IOCTL_MEM_ALLOC, &arg);
    if (ret) {
        free(mo);
        return ret;
    }

    mo->foffset = arg.foffset;
    mo->nbytes = nbytes;

    do {
        if (addr < nbytes) {
            mo->ptr = NULL;
            compote_mo_del(ctx, &mo);
            return -ENOMEM;
        }
        addr = (addr - nbytes) & mask;
        mo->ptr = mmap((void *)addr, mo->nbytes, PROT_READ | PROT_WRITE,
                   MAP_SHARED | MAP_FIXED, ctx->fd, mo->foffset);
    } while (mo->ptr == MAP_FAILED);

    memset(mo->ptr, 0, mo->nbytes);

    *mop = mo;
    return 0;
}

void compote_mo_del(compote_context_t *ctx, compote_mo_t **mop)
{
    struct compote_ioctl_mem_free arg;
    compote_mo_t *mo = *mop;

    *mop = NULL;
    if (mo == NULL) {
        return;
    }

    arg.foffset = mo->foffset;
    munmap(mo->ptr, mo->nbytes);
    compote_context_ioctl(ctx, COMPOTE_IOCTL_MEM_FREE, &arg);
    free(mo);
}

int compote_context_execute(compote_context_t *ctx,
                            void *addr, unsigned ndw)
{
    struct compote_ioctl_channel_execute arg;

    arg.channel = ctx->channel.id;
    arg.addr = (unsigned long)addr;
    arg.ndw = ndw;

    // Must be 32bits aligned
    if ((arg.addr & 3)) {
        return -EINVAL;
    }

    return compote_context_ioctl(ctx, COMPOTE_IOCTL_CHAN_EXEC, &arg);
}

int main(int argc, char *argv[])
{
    compote_context_t *ctx;
    compote_mo_t *mo;
    int ret;

    ret = compote_context_new(&ctx);
    if (ret) {
        return ret;
    }

    ret = compote_mo_new(ctx, &mo, 64 << 10);
    if (ret) {
        goto out;
    }

    {
        uint32_t *ptr = mo->ptr;
        uint32_t *sem = &ptr[128 >> 2];

        ptr[128 >> 2] = 0xcafedead;

        ptr[0] = nvk_sq_cmd(1, 0x10, 4);
        ptr[1] = ((unsigned long)sem) >> 32;
        ptr[2] = ((unsigned long)sem) & 0xffffffff;
        ptr[3] = 0xdeadcafe;
        ptr[4] = 0x00000002;

        ret = compote_context_execute(ctx, ptr, 5);
        if (ret) {
            goto out;
        }

        for (unsigned c = 0; (c < 10) && (ptr[128 >> 2] != 0xdeadcafe); c++) {
            printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
            sleep(1);
        }
        printf("[%4d] = 0x%08x 0x%08x 0x%08x\n", 128 >> 2, sem[0], sem[1], sem[2]);
    }

    printf("La compote c'est bon !\n");

out:
    compote_mo_del(ctx, &mo);
    compote_context_del(&ctx);
    return ret;
}