blob: 3ce44fdfce4e84ad409bcf88d4d6c4c51c09eb50 [file] [log] [blame]
Michael Walle0670dad2011-03-07 23:32:40 +01001/*
2 * QEMU model of the Milkymist texture mapping unit.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 * Copyright (c) 2010 Sebastien Bourdeauducq
6 * <sebastien.bourdeauducq@lekernel.net>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 *
21 *
22 * Specification available at:
Michael Walle6dbbe242016-06-20 17:08:41 +010023 * http://milkymist.walle.cc/socdoc/tmu2.pdf
Michael Walle0670dad2011-03-07 23:32:40 +010024 *
25 */
26
Peter Maydellea99dde2016-01-26 18:16:57 +000027#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010028#include "hw/hw.h"
29#include "hw/sysbus.h"
Michael Walle0670dad2011-03-07 23:32:40 +010030#include "trace.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010031#include "qapi/error.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/error-report.h"
Gerd Hoffmann55543e72016-06-21 12:11:51 +020033#include "qapi/error.h"
Michael Walle0670dad2011-03-07 23:32:40 +010034
35#include <X11/Xlib.h>
OGAWA Hirofumifb719562015-10-27 02:45:48 +090036#include <epoxy/gl.h>
37#include <epoxy/glx.h>
Michael Walle0670dad2011-03-07 23:32:40 +010038
39enum {
40 R_CTL = 0,
41 R_HMESHLAST,
42 R_VMESHLAST,
43 R_BRIGHTNESS,
44 R_CHROMAKEY,
45 R_VERTICESADDR,
46 R_TEXFBUF,
47 R_TEXHRES,
48 R_TEXVRES,
49 R_TEXHMASK,
50 R_TEXVMASK,
51 R_DSTFBUF,
52 R_DSTHRES,
53 R_DSTVRES,
54 R_DSTHOFFSET,
55 R_DSTVOFFSET,
56 R_DSTSQUAREW,
57 R_DSTSQUAREH,
58 R_ALPHA,
59 R_MAX
60};
61
62enum {
63 CTL_START_BUSY = (1<<0),
64 CTL_CHROMAKEY = (1<<1),
65};
66
67enum {
68 MAX_BRIGHTNESS = 63,
69 MAX_ALPHA = 63,
70};
71
72enum {
73 MESH_MAXSIZE = 128,
74};
75
76struct vertex {
77 int x;
78 int y;
Stefan Weil541dc0d2011-08-31 12:38:01 +020079} QEMU_PACKED;
Michael Walle0670dad2011-03-07 23:32:40 +010080
Andreas Färber56299132013-07-25 00:48:23 +020081#define TYPE_MILKYMIST_TMU2 "milkymist-tmu2"
82#define MILKYMIST_TMU2(obj) \
83 OBJECT_CHECK(MilkymistTMU2State, (obj), TYPE_MILKYMIST_TMU2)
84
Michael Walle0670dad2011-03-07 23:32:40 +010085struct MilkymistTMU2State {
Andreas Färber56299132013-07-25 00:48:23 +020086 SysBusDevice parent_obj;
87
Michael Walle71004532011-08-31 16:48:44 +020088 MemoryRegion regs_region;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +030089 Chardev *chr;
Michael Walle0670dad2011-03-07 23:32:40 +010090 qemu_irq irq;
91
92 uint32_t regs[R_MAX];
93
94 Display *dpy;
95 GLXFBConfig glx_fb_config;
96 GLXContext glx_context;
97};
98typedef struct MilkymistTMU2State MilkymistTMU2State;
99
100static const int glx_fbconfig_attr[] = {
101 GLX_GREEN_SIZE, 5,
102 GLX_GREEN_SIZE, 6,
103 GLX_BLUE_SIZE, 5,
104 None
105};
106
107static int tmu2_glx_init(MilkymistTMU2State *s)
108{
109 GLXFBConfig *configs;
110 int nelements;
111
112 s->dpy = XOpenDisplay(NULL); /* FIXME: call XCloseDisplay() */
113 if (s->dpy == NULL) {
114 return 1;
115 }
116
117 configs = glXChooseFBConfig(s->dpy, 0, glx_fbconfig_attr, &nelements);
118 if (configs == NULL) {
119 return 1;
120 }
121
122 s->glx_fb_config = *configs;
123 XFree(configs);
124
125 /* FIXME: call glXDestroyContext() */
126 s->glx_context = glXCreateNewContext(s->dpy, s->glx_fb_config,
127 GLX_RGBA_TYPE, NULL, 1);
128 if (s->glx_context == NULL) {
129 return 1;
130 }
131
132 return 0;
133}
134
135static void tmu2_gl_map(struct vertex *mesh, int texhres, int texvres,
136 int hmeshlast, int vmeshlast, int ho, int vo, int sw, int sh)
137{
138 int x, y;
139 int x0, y0, x1, y1;
140 int u0, v0, u1, v1, u2, v2, u3, v3;
141 double xscale = 1.0 / ((double)(64 * texhres));
142 double yscale = 1.0 / ((double)(64 * texvres));
143
144 glLoadIdentity();
145 glTranslatef(ho, vo, 0);
146 glEnable(GL_TEXTURE_2D);
147 glBegin(GL_QUADS);
148
149 for (y = 0; y < vmeshlast; y++) {
150 y0 = y * sh;
151 y1 = y0 + sh;
152 for (x = 0; x < hmeshlast; x++) {
153 x0 = x * sw;
154 x1 = x0 + sw;
155
156 u0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].x);
157 v0 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x].y);
158 u1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].x);
159 v1 = be32_to_cpu(mesh[MESH_MAXSIZE * y + x + 1].y);
160 u2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].x);
161 v2 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x + 1].y);
162 u3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].x);
163 v3 = be32_to_cpu(mesh[MESH_MAXSIZE * (y + 1) + x].y);
164
165 glTexCoord2d(((double)u0) * xscale, ((double)v0) * yscale);
166 glVertex3i(x0, y0, 0);
167 glTexCoord2d(((double)u1) * xscale, ((double)v1) * yscale);
168 glVertex3i(x1, y0, 0);
169 glTexCoord2d(((double)u2) * xscale, ((double)v2) * yscale);
170 glVertex3i(x1, y1, 0);
171 glTexCoord2d(((double)u3) * xscale, ((double)v3) * yscale);
172 glVertex3i(x0, y1, 0);
173 }
174 }
175
176 glEnd();
177}
178
179static void tmu2_start(MilkymistTMU2State *s)
180{
181 int pbuffer_attrib[6] = {
182 GLX_PBUFFER_WIDTH,
183 0,
184 GLX_PBUFFER_HEIGHT,
185 0,
186 GLX_PRESERVED_CONTENTS,
187 True
188 };
189
190 GLXPbuffer pbuffer;
191 GLuint texture;
192 void *fb;
Avi Kivitya8170e52012-10-23 12:30:10 +0200193 hwaddr fb_len;
Michael Walle0670dad2011-03-07 23:32:40 +0100194 void *mesh;
Avi Kivitya8170e52012-10-23 12:30:10 +0200195 hwaddr mesh_len;
Michael Walle0670dad2011-03-07 23:32:40 +0100196 float m;
197
198 trace_milkymist_tmu2_start();
199
200 /* Create and set up a suitable OpenGL context */
201 pbuffer_attrib[1] = s->regs[R_DSTHRES];
202 pbuffer_attrib[3] = s->regs[R_DSTVRES];
203 pbuffer = glXCreatePbuffer(s->dpy, s->glx_fb_config, pbuffer_attrib);
204 glXMakeContextCurrent(s->dpy, pbuffer, pbuffer, s->glx_context);
205
206 /* Fixup endianness. TODO: would it work on BE hosts? */
207 glPixelStorei(GL_UNPACK_SWAP_BYTES, 1);
208 glPixelStorei(GL_PACK_SWAP_BYTES, 1);
209
210 /* Row alignment */
211 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
212 glPixelStorei(GL_PACK_ALIGNMENT, 2);
213
214 /* Read the QEMU source framebuffer into an OpenGL texture */
215 glGenTextures(1, &texture);
216 glBindTexture(GL_TEXTURE_2D, texture);
Michael Walle237a8652016-10-13 00:35:07 +0200217 fb_len = 2ULL * s->regs[R_TEXHRES] * s->regs[R_TEXVRES];
Michael Walle0670dad2011-03-07 23:32:40 +0100218 fb = cpu_physical_memory_map(s->regs[R_TEXFBUF], &fb_len, 0);
219 if (fb == NULL) {
220 glDeleteTextures(1, &texture);
221 glXMakeContextCurrent(s->dpy, None, None, NULL);
222 glXDestroyPbuffer(s->dpy, pbuffer);
223 return;
224 }
225 glTexImage2D(GL_TEXTURE_2D, 0, 3, s->regs[R_TEXHRES], s->regs[R_TEXVRES],
226 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, fb);
227 cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
228
229 /* Set up texturing options */
230 /* WARNING:
231 * Many cases of TMU2 masking are not supported by OpenGL.
232 * We only implement the most common ones:
233 * - full bilinear filtering vs. nearest texel
234 * - texture clamping vs. texture wrapping
235 */
236 if ((s->regs[R_TEXHMASK] & 0x3f) > 0x20) {
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
238 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
239 } else {
240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
242 }
243 if ((s->regs[R_TEXHMASK] >> 6) & s->regs[R_TEXHRES]) {
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
245 } else {
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
247 }
248 if ((s->regs[R_TEXVMASK] >> 6) & s->regs[R_TEXVRES]) {
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
250 } else {
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
252 }
253
254 /* Translucency and decay */
255 glEnable(GL_BLEND);
256 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
257 m = (float)(s->regs[R_BRIGHTNESS] + 1) / 64.0f;
258 glColor4f(m, m, m, (float)(s->regs[R_ALPHA] + 1) / 64.0f);
259
260 /* Read the QEMU dest. framebuffer into the OpenGL framebuffer */
Peter Maydell4382fa62017-01-06 17:45:14 +0000261 fb_len = 2ULL * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
Michael Walle0670dad2011-03-07 23:32:40 +0100262 fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 0);
263 if (fb == NULL) {
264 glDeleteTextures(1, &texture);
265 glXMakeContextCurrent(s->dpy, None, None, NULL);
266 glXDestroyPbuffer(s->dpy, pbuffer);
267 return;
268 }
269
270 glDrawPixels(s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
271 GL_UNSIGNED_SHORT_5_6_5, fb);
272 cpu_physical_memory_unmap(fb, fb_len, 0, fb_len);
273 glViewport(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES]);
274 glMatrixMode(GL_PROJECTION);
275 glLoadIdentity();
276 glOrtho(0.0, s->regs[R_DSTHRES], 0.0, s->regs[R_DSTVRES], -1.0, 1.0);
277 glMatrixMode(GL_MODELVIEW);
278
279 /* Map the texture */
280 mesh_len = MESH_MAXSIZE*MESH_MAXSIZE*sizeof(struct vertex);
281 mesh = cpu_physical_memory_map(s->regs[R_VERTICESADDR], &mesh_len, 0);
282 if (mesh == NULL) {
283 glDeleteTextures(1, &texture);
284 glXMakeContextCurrent(s->dpy, None, None, NULL);
285 glXDestroyPbuffer(s->dpy, pbuffer);
286 return;
287 }
288
289 tmu2_gl_map((struct vertex *)mesh,
290 s->regs[R_TEXHRES], s->regs[R_TEXVRES],
291 s->regs[R_HMESHLAST], s->regs[R_VMESHLAST],
292 s->regs[R_DSTHOFFSET], s->regs[R_DSTVOFFSET],
293 s->regs[R_DSTSQUAREW], s->regs[R_DSTSQUAREH]);
294 cpu_physical_memory_unmap(mesh, mesh_len, 0, mesh_len);
295
296 /* Write back the OpenGL framebuffer to the QEMU framebuffer */
Peter Maydell3d74ee72017-02-16 17:26:48 +0000297 fb_len = 2ULL * s->regs[R_DSTHRES] * s->regs[R_DSTVRES];
Michael Walle0670dad2011-03-07 23:32:40 +0100298 fb = cpu_physical_memory_map(s->regs[R_DSTFBUF], &fb_len, 1);
299 if (fb == NULL) {
300 glDeleteTextures(1, &texture);
301 glXMakeContextCurrent(s->dpy, None, None, NULL);
302 glXDestroyPbuffer(s->dpy, pbuffer);
303 return;
304 }
305
306 glReadPixels(0, 0, s->regs[R_DSTHRES], s->regs[R_DSTVRES], GL_RGB,
307 GL_UNSIGNED_SHORT_5_6_5, fb);
308 cpu_physical_memory_unmap(fb, fb_len, 1, fb_len);
309
310 /* Free OpenGL allocs */
311 glDeleteTextures(1, &texture);
312 glXMakeContextCurrent(s->dpy, None, None, NULL);
313 glXDestroyPbuffer(s->dpy, pbuffer);
314
315 s->regs[R_CTL] &= ~CTL_START_BUSY;
316
317 trace_milkymist_tmu2_pulse_irq();
318 qemu_irq_pulse(s->irq);
319}
320
Avi Kivitya8170e52012-10-23 12:30:10 +0200321static uint64_t tmu2_read(void *opaque, hwaddr addr,
Michael Walle71004532011-08-31 16:48:44 +0200322 unsigned size)
Michael Walle0670dad2011-03-07 23:32:40 +0100323{
324 MilkymistTMU2State *s = opaque;
325 uint32_t r = 0;
326
327 addr >>= 2;
328 switch (addr) {
329 case R_CTL:
330 case R_HMESHLAST:
331 case R_VMESHLAST:
332 case R_BRIGHTNESS:
333 case R_CHROMAKEY:
334 case R_VERTICESADDR:
335 case R_TEXFBUF:
336 case R_TEXHRES:
337 case R_TEXVRES:
338 case R_TEXHMASK:
339 case R_TEXVMASK:
340 case R_DSTFBUF:
341 case R_DSTHRES:
342 case R_DSTVRES:
343 case R_DSTHOFFSET:
344 case R_DSTVOFFSET:
345 case R_DSTSQUAREW:
346 case R_DSTSQUAREH:
347 case R_ALPHA:
348 r = s->regs[addr];
349 break;
350
351 default:
352 error_report("milkymist_tmu2: read access to unknown register 0x"
353 TARGET_FMT_plx, addr << 2);
354 break;
355 }
356
357 trace_milkymist_tmu2_memory_read(addr << 2, r);
358
359 return r;
360}
361
362static void tmu2_check_registers(MilkymistTMU2State *s)
363{
364 if (s->regs[R_BRIGHTNESS] > MAX_BRIGHTNESS) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200365 error_report("milkymist_tmu2: max brightness is %d", MAX_BRIGHTNESS);
Michael Walle0670dad2011-03-07 23:32:40 +0100366 }
367
368 if (s->regs[R_ALPHA] > MAX_ALPHA) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200369 error_report("milkymist_tmu2: max alpha is %d", MAX_ALPHA);
Michael Walle0670dad2011-03-07 23:32:40 +0100370 }
371
372 if (s->regs[R_VERTICESADDR] & 0x07) {
373 error_report("milkymist_tmu2: vertex mesh address has to be 64-bit "
Markus Armbruster6daf1942011-06-22 14:03:54 +0200374 "aligned");
Michael Walle0670dad2011-03-07 23:32:40 +0100375 }
376
377 if (s->regs[R_TEXFBUF] & 0x01) {
378 error_report("milkymist_tmu2: texture buffer address has to be "
Markus Armbruster6daf1942011-06-22 14:03:54 +0200379 "16-bit aligned");
Michael Walle0670dad2011-03-07 23:32:40 +0100380 }
381}
382
Avi Kivitya8170e52012-10-23 12:30:10 +0200383static void tmu2_write(void *opaque, hwaddr addr, uint64_t value,
Michael Walle71004532011-08-31 16:48:44 +0200384 unsigned size)
Michael Walle0670dad2011-03-07 23:32:40 +0100385{
386 MilkymistTMU2State *s = opaque;
387
388 trace_milkymist_tmu2_memory_write(addr, value);
389
390 addr >>= 2;
391 switch (addr) {
392 case R_CTL:
393 s->regs[addr] = value;
394 if (value & CTL_START_BUSY) {
395 tmu2_start(s);
396 }
397 break;
398 case R_BRIGHTNESS:
399 case R_HMESHLAST:
400 case R_VMESHLAST:
401 case R_CHROMAKEY:
402 case R_VERTICESADDR:
403 case R_TEXFBUF:
404 case R_TEXHRES:
405 case R_TEXVRES:
406 case R_TEXHMASK:
407 case R_TEXVMASK:
408 case R_DSTFBUF:
409 case R_DSTHRES:
410 case R_DSTVRES:
411 case R_DSTHOFFSET:
412 case R_DSTVOFFSET:
413 case R_DSTSQUAREW:
414 case R_DSTSQUAREH:
415 case R_ALPHA:
416 s->regs[addr] = value;
417 break;
418
419 default:
420 error_report("milkymist_tmu2: write access to unknown register 0x"
421 TARGET_FMT_plx, addr << 2);
422 break;
423 }
424
425 tmu2_check_registers(s);
426}
427
Michael Walle71004532011-08-31 16:48:44 +0200428static const MemoryRegionOps tmu2_mmio_ops = {
429 .read = tmu2_read,
430 .write = tmu2_write,
431 .valid = {
432 .min_access_size = 4,
433 .max_access_size = 4,
434 },
435 .endianness = DEVICE_NATIVE_ENDIAN,
Michael Walle0670dad2011-03-07 23:32:40 +0100436};
437
438static void milkymist_tmu2_reset(DeviceState *d)
439{
Andreas Färber56299132013-07-25 00:48:23 +0200440 MilkymistTMU2State *s = MILKYMIST_TMU2(d);
Michael Walle0670dad2011-03-07 23:32:40 +0100441 int i;
442
443 for (i = 0; i < R_MAX; i++) {
444 s->regs[i] = 0;
445 }
446}
447
xiaoqiang zhaocf79c642016-05-06 18:59:33 +0800448static void milkymist_tmu2_init(Object *obj)
449{
450 MilkymistTMU2State *s = MILKYMIST_TMU2(obj);
451 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
452
453 sysbus_init_irq(dev, &s->irq);
454
455 memory_region_init_io(&s->regs_region, obj, &tmu2_mmio_ops, s,
456 "milkymist-tmu2", R_MAX * 4);
457 sysbus_init_mmio(dev, &s->regs_region);
458}
459
460static void milkymist_tmu2_realize(DeviceState *dev, Error **errp)
Michael Walle0670dad2011-03-07 23:32:40 +0100461{
Andreas Färber56299132013-07-25 00:48:23 +0200462 MilkymistTMU2State *s = MILKYMIST_TMU2(dev);
Michael Walle0670dad2011-03-07 23:32:40 +0100463
464 if (tmu2_glx_init(s)) {
xiaoqiang zhaocf79c642016-05-06 18:59:33 +0800465 error_setg(errp, "tmu2_glx_init failed");
Michael Walle0670dad2011-03-07 23:32:40 +0100466 }
Michael Walle0670dad2011-03-07 23:32:40 +0100467}
468
469static const VMStateDescription vmstate_milkymist_tmu2 = {
470 .name = "milkymist-tmu2",
471 .version_id = 1,
472 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200473 .fields = (VMStateField[]) {
Michael Walle0670dad2011-03-07 23:32:40 +0100474 VMSTATE_UINT32_ARRAY(regs, MilkymistTMU2State, R_MAX),
475 VMSTATE_END_OF_LIST()
476 }
477};
478
Anthony Liguori999e12b2012-01-24 13:12:29 -0600479static void milkymist_tmu2_class_init(ObjectClass *klass, void *data)
480{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600481 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600482
xiaoqiang zhaocf79c642016-05-06 18:59:33 +0800483 dc->realize = milkymist_tmu2_realize;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600484 dc->reset = milkymist_tmu2_reset;
485 dc->vmsd = &vmstate_milkymist_tmu2;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600486}
487
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100488static const TypeInfo milkymist_tmu2_info = {
Andreas Färber56299132013-07-25 00:48:23 +0200489 .name = TYPE_MILKYMIST_TMU2,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600490 .parent = TYPE_SYS_BUS_DEVICE,
491 .instance_size = sizeof(MilkymistTMU2State),
xiaoqiang zhaocf79c642016-05-06 18:59:33 +0800492 .instance_init = milkymist_tmu2_init,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600493 .class_init = milkymist_tmu2_class_init,
Michael Walle0670dad2011-03-07 23:32:40 +0100494};
495
Andreas Färber83f7d432012-02-09 15:20:55 +0100496static void milkymist_tmu2_register_types(void)
Michael Walle0670dad2011-03-07 23:32:40 +0100497{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600498 type_register_static(&milkymist_tmu2_info);
Michael Walle0670dad2011-03-07 23:32:40 +0100499}
500
Andreas Färber83f7d432012-02-09 15:20:55 +0100501type_init(milkymist_tmu2_register_types)