summaryrefslogtreecommitdiff
path: root/hw/yagl/yagl_device.c
blob: f7a5dab723a7ceffd16657e92785a2ac2340a997 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * yagl
 *
 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * Stanislav Vorobiov <s.vorobiov@samsung.com>
 * Jinhyung Jo <jinhyung.jo@samsung.com>
 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#include "yagl_server.h"
#include "yagl_log.h"
#include "yagl_handle_gen.h"
#include "yagl_stats.h"
#include "yagl_process.h"
#include "yagl_thread.h"
#include "yagl_egl_driver.h"
#include "yagl_drivers/gles_ogl/yagl_gles_ogl.h"
#include "yagl_drivers/gles_onscreen/yagl_gles_onscreen.h"
#include "yagl_backends/egl_offscreen/yagl_egl_offscreen.h"
#include "yagl_backends/egl_onscreen/yagl_egl_onscreen.h"
#include "exec/cpu-all.h"
#include "hw/hw.h"
#include "hw/pci/pci.h"
#include <GL/gl.h>
#include "winsys.h"
#include "yagl_gles_driver.h"

#define PCI_VENDOR_ID_YAGL 0x19B1
#define PCI_DEVICE_ID_YAGL 0x1010

#define YAGL_REG_BUFFPTR 0
#define YAGL_REG_TRIGGER 4
#define YAGL_REGS_SIZE   8

#define YAGL_MEM_SIZE 0x1000

#define YAGL_MAX_USERS (YAGL_MEM_SIZE / YAGL_REGS_SIZE)

struct yagl_user
{
    bool activated;
    yagl_pid process_id;
    yagl_tid thread_id;
};

typedef struct YaGLState
{
    PCIDevice dev;
    void *display;
    struct winsys_interface *wsi;
    MemoryRegion iomem;
    struct yagl_server_state *ss;
    struct yagl_user users[YAGL_MAX_USERS];
} YaGLState;

#define TYPE_YAGL_DEVICE "yagl"

static void yagl_device_operate(YaGLState *s, int user_index, hwaddr buff_pa)
{
    yagl_pid target_pid;
    yagl_tid target_tid;
    hwaddr buff_len = TARGET_PAGE_SIZE;
    uint8_t *buff = NULL;

    YAGL_LOG_FUNC_ENTER(yagl_device_operate,
                        "user_index = %d, buff_pa = 0x%X",
                        user_index,
                        (uint32_t)buff_pa);

    if (!buff_pa && !s->users[user_index].activated) {
        YAGL_LOG_CRITICAL("user %d is not activated", user_index);
        goto out;
    }

    if (buff_pa) {
        buff = cpu_physical_memory_map(buff_pa, &buff_len, false);

        if (!buff || (buff_len != TARGET_PAGE_SIZE)) {
            YAGL_LOG_CRITICAL("cpu_physical_memory_map(read) failed for user %d, buff_pa = 0x%X",
                              user_index,
                              (uint32_t)buff_pa);
            goto out;
        }

        if (s->users[user_index].activated) {
            /*
             * Update user.
             */

            yagl_server_dispatch_update(s->ss,
                                        s->users[user_index].process_id,
                                        s->users[user_index].thread_id,
                                        buff);
        } else {
            /*
             * Activate user.
             */

            if (yagl_server_dispatch_init(s->ss,
                                          buff,
                                          &target_pid,
                                          &target_tid)) {
                s->users[user_index].activated = true;
                s->users[user_index].process_id = target_pid;
                s->users[user_index].thread_id = target_tid;

                YAGL_LOG_INFO("user %d activated", user_index);

                /*
                 * The buff is now owned by client.
                 */
                buff = NULL;
            }
        }
    } else {
        /*
         * Deactivate user.
         */

        yagl_server_dispatch_exit(s->ss,
                                  s->users[user_index].process_id,
                                  s->users[user_index].thread_id);

        memset(&s->users[user_index], 0, sizeof(s->users[user_index]));

        YAGL_LOG_INFO("user %d deactivated", user_index);
    }

out:
    if (buff) {
        cpu_physical_memory_unmap(buff,
                                  TARGET_PAGE_SIZE,
                                  0,
                                  TARGET_PAGE_SIZE);
    }

    YAGL_LOG_FUNC_EXIT(NULL);
}

static void yagl_device_trigger(YaGLState *s, int user_index, uint32_t offset)
{
    YAGL_LOG_FUNC_ENTER(yagl_device_trigger, "%d, %u", user_index, offset);

    if (s->users[user_index].activated) {
        yagl_server_dispatch_batch(s->ss,
                                   s->users[user_index].process_id,
                                   s->users[user_index].thread_id,
                                   offset);
    } else {
        YAGL_LOG_CRITICAL("user %d not activated", user_index);
    }

    YAGL_LOG_FUNC_EXIT(NULL);
}

static uint64_t yagl_device_read(void *opaque, hwaddr offset,
                                 unsigned size)
{
    return 0;
}

static void yagl_device_write(void *opaque, hwaddr offset,
                              uint64_t value, unsigned size)
{
    YaGLState *s = (YaGLState*)opaque;
    int user_index = (offset / YAGL_REGS_SIZE);
    offset -= user_index * YAGL_REGS_SIZE;

    assert(user_index < YAGL_MAX_USERS);

    if (user_index >= YAGL_MAX_USERS) {
        YAGL_LOG_CRITICAL("bad user index = %d", user_index);
        return;
    }

    switch (offset) {
    case YAGL_REG_BUFFPTR:
        yagl_device_operate(s, user_index, value);
        break;
    case YAGL_REG_TRIGGER:
        yagl_device_trigger(s, user_index, value);
        break;
    default:
        YAGL_LOG_CRITICAL("user %d, bad offset = %d", user_index, offset);
        break;
    }
}

static const MemoryRegionOps yagl_device_ops =
{
    .read = yagl_device_read,
    .write = yagl_device_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};

static int yagl_device_init(PCIDevice *dev)
{
    YaGLState *s = DO_UPCAST(YaGLState, dev, dev);
    struct yagl_egl_driver *egl_driver = NULL;
    struct yagl_egl_backend *egl_backend = NULL;
    struct yagl_gles_driver *gles_driver = NULL;

    yagl_log_init();

    YAGL_LOG_FUNC_ENTER(yagl_device_init, NULL);

    memory_region_init_io(&s->iomem, OBJECT(s),
                          &yagl_device_ops,
                          s,
                          TYPE_YAGL_DEVICE,
                          YAGL_MEM_SIZE);

    yagl_handle_gen_init();

    egl_driver = yagl_egl_driver_create(s->display);

    if (!egl_driver) {
        goto fail;
    }

    gles_driver = yagl_gles_ogl_create(egl_driver->dyn_lib);

    if (!gles_driver) {
        goto fail;
    }

    if (s->wsi) {
        egl_backend = yagl_egl_onscreen_create(s->wsi,
                                               egl_driver,
                                               gles_driver);
        gles_driver = yagl_gles_onscreen_create(gles_driver);
    } else {
        egl_backend = yagl_egl_offscreen_create(egl_driver, gles_driver);
    }

    if (!egl_backend) {
        goto fail;
    }

    /*
     * Now owned by EGL backend.
     */
    egl_driver = NULL;

    s->ss = yagl_server_state_create(egl_backend, gles_driver);

    /*
     * Owned/destroyed by server state.
     */
    egl_backend = NULL;
    gles_driver = NULL;

    if (!s->ss) {
        goto fail;
    }

    pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->iomem);

    YAGL_LOG_FUNC_EXIT(NULL);

    return 0;

fail:
    if (egl_backend) {
        egl_backend->destroy(egl_backend);
    }

    if (gles_driver) {
        gles_driver->destroy(gles_driver);
    }

    if (egl_driver) {
        egl_driver->destroy(egl_driver);
    }

    yagl_handle_gen_cleanup();

    YAGL_LOG_FUNC_EXIT(NULL);

    yagl_log_cleanup();

    return -1;
}

static void yagl_device_reset(DeviceState *d)
{
    YaGLState *s = container_of(d, YaGLState, dev.qdev);
    int i;

    YAGL_LOG_FUNC_ENTER(yagl_device_reset, NULL);

    yagl_server_reset(s->ss);

    yagl_handle_gen_reset();

    for (i = 0; i < YAGL_MAX_USERS; ++i) {
        memset(&s->users[i], 0, sizeof(s->users[i]));
    }

    YAGL_LOG_FUNC_EXIT(NULL);
}

static void yagl_device_exit(PCIDevice *dev)
{
    YaGLState *s = DO_UPCAST(YaGLState, dev, dev);

    YAGL_LOG_FUNC_ENTER(yagl_device_exit, NULL);

    memory_region_destroy(&s->iomem);

    yagl_server_state_destroy(s->ss);

    yagl_handle_gen_cleanup();

    YAGL_LOG_FUNC_EXIT(NULL);

    yagl_log_cleanup();
}

static Property yagl_properties[] = {
    {
        .name   = "display",
        .info   = &qdev_prop_ptr,
        .offset = offsetof(YaGLState, display),
    },
    {
        .name   = "winsys_gl_interface",
        .info   = &qdev_prop_ptr,
        .offset = offsetof(YaGLState, wsi),
    },
    DEFINE_PROP_END_OF_LIST(),
};

static void yagl_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = yagl_device_init;
    k->exit = yagl_device_exit;
    k->vendor_id = PCI_VENDOR_ID_YAGL;
    k->device_id = PCI_DEVICE_ID_YAGL;
    k->class_id = PCI_CLASS_OTHERS;
    dc->reset = yagl_device_reset;
    dc->props = yagl_properties;
    dc->desc = "YaGL device";
}

static TypeInfo yagl_device_info =
{
    .name          = TYPE_YAGL_DEVICE,
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(YaGLState),
    .class_init    = yagl_class_init,
};

static void yagl_register_types(void)
{
    type_register_static(&yagl_device_info);
}

type_init(yagl_register_types)