xref: /openbmc/qemu/hw/display/macfb.c (revision 4ec27073fd0e5a82a87b1122dfdca7a820cb1561)
1 /*
2  * QEMU Motorola 680x0 Macintosh Video Card Emulation
3  *                 Copyright (c) 2012-2018 Laurent Vivier
4  *
5  * some parts from QEMU G364 framebuffer Emulator.
6  *                 Copyright (c) 2007-2011 Herve Poussineau
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  *
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/units.h"
15 #include "hw/sysbus.h"
16 #include "ui/console.h"
17 #include "ui/pixel_ops.h"
18 #include "hw/nubus/nubus.h"
19 #include "hw/display/macfb.h"
20 #include "qapi/error.h"
21 #include "hw/qdev-properties.h"
22 #include "migration/vmstate.h"
23 #include "trace.h"
24 
25 #define VIDEO_BASE 0x00001000
26 #define DAFB_BASE  0x00800000
27 
28 #define MACFB_PAGE_SIZE 4096
29 #define MACFB_VRAM_SIZE (4 * MiB)
30 
31 #define DAFB_RESET      0x200
32 #define DAFB_LUT        0x213
33 
34 
35 typedef void macfb_draw_line_func(MacfbState *s, uint8_t *d, uint32_t addr,
36                                   int width);
37 
38 static inline uint8_t macfb_read_byte(MacfbState *s, uint32_t addr)
39 {
40     return s->vram[addr & s->vram_bit_mask];
41 }
42 
43 /* 1-bit color */
44 static void macfb_draw_line1(MacfbState *s, uint8_t *d, uint32_t addr,
45                              int width)
46 {
47     uint8_t r, g, b;
48     int x;
49 
50     for (x = 0; x < width; x++) {
51         int bit = x & 7;
52         int idx = (macfb_read_byte(s, addr) >> (7 - bit)) & 1;
53         r = g = b  = ((1 - idx) << 7);
54         addr += (bit == 7);
55 
56         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
57         d += 4;
58     }
59 }
60 
61 /* 2-bit color */
62 static void macfb_draw_line2(MacfbState *s, uint8_t *d, uint32_t addr,
63                              int width)
64 {
65     uint8_t r, g, b;
66     int x;
67 
68     for (x = 0; x < width; x++) {
69         int bit = (x & 3);
70         int idx = (macfb_read_byte(s, addr) >> ((3 - bit) << 1)) & 3;
71         r = s->color_palette[idx * 3];
72         g = s->color_palette[idx * 3 + 1];
73         b = s->color_palette[idx * 3 + 2];
74         addr += (bit == 3);
75 
76         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
77         d += 4;
78     }
79 }
80 
81 /* 4-bit color */
82 static void macfb_draw_line4(MacfbState *s, uint8_t *d, uint32_t addr,
83                              int width)
84 {
85     uint8_t r, g, b;
86     int x;
87 
88     for (x = 0; x < width; x++) {
89         int bit = x & 1;
90         int idx = (macfb_read_byte(s, addr) >> ((1 - bit) << 2)) & 15;
91         r = s->color_palette[idx * 3];
92         g = s->color_palette[idx * 3 + 1];
93         b = s->color_palette[idx * 3 + 2];
94         addr += (bit == 1);
95 
96         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
97         d += 4;
98     }
99 }
100 
101 /* 8-bit color */
102 static void macfb_draw_line8(MacfbState *s, uint8_t *d, uint32_t addr,
103                              int width)
104 {
105     uint8_t r, g, b;
106     int x;
107 
108     for (x = 0; x < width; x++) {
109         r = s->color_palette[macfb_read_byte(s, addr) * 3];
110         g = s->color_palette[macfb_read_byte(s, addr) * 3 + 1];
111         b = s->color_palette[macfb_read_byte(s, addr) * 3 + 2];
112         addr++;
113 
114         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
115         d += 4;
116     }
117 }
118 
119 /* 16-bit color */
120 static void macfb_draw_line16(MacfbState *s, uint8_t *d, uint32_t addr,
121                               int width)
122 {
123     uint8_t r, g, b;
124     int x;
125 
126     for (x = 0; x < width; x++) {
127         uint16_t pixel;
128         pixel = (macfb_read_byte(s, addr) << 8) | macfb_read_byte(s, addr + 1);
129         r = ((pixel >> 10) & 0x1f) << 3;
130         g = ((pixel >> 5) & 0x1f) << 3;
131         b = (pixel & 0x1f) << 3;
132         addr += 2;
133 
134         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
135         d += 4;
136     }
137 }
138 
139 /* 24-bit color */
140 static void macfb_draw_line24(MacfbState *s, uint8_t *d, uint32_t addr,
141                               int width)
142 {
143     uint8_t r, g, b;
144     int x;
145 
146     for (x = 0; x < width; x++) {
147         r = macfb_read_byte(s, addr);
148         g = macfb_read_byte(s, addr + 1);
149         b = macfb_read_byte(s, addr + 2);
150         addr += 3;
151 
152         *(uint32_t *)d = rgb_to_pixel32(r, g, b);
153         d += 4;
154     }
155 }
156 
157 
158 enum {
159     MACFB_DRAW_LINE1,
160     MACFB_DRAW_LINE2,
161     MACFB_DRAW_LINE4,
162     MACFB_DRAW_LINE8,
163     MACFB_DRAW_LINE16,
164     MACFB_DRAW_LINE24,
165     MACFB_DRAW_LINE_NB,
166 };
167 
168 static macfb_draw_line_func * const
169                               macfb_draw_line_table[MACFB_DRAW_LINE_NB] = {
170     macfb_draw_line1,
171     macfb_draw_line2,
172     macfb_draw_line4,
173     macfb_draw_line8,
174     macfb_draw_line16,
175     macfb_draw_line24,
176 };
177 
178 static int macfb_check_dirty(MacfbState *s, DirtyBitmapSnapshot *snap,
179                              ram_addr_t addr, int len)
180 {
181     return memory_region_snapshot_get_dirty(&s->mem_vram, snap, addr, len);
182 }
183 
184 static void macfb_draw_graphic(MacfbState *s)
185 {
186     DisplaySurface *surface = qemu_console_surface(s->con);
187     DirtyBitmapSnapshot *snap = NULL;
188     ram_addr_t page;
189     uint32_t v = 0;
190     int y, ymin;
191     int macfb_stride = (s->depth * s->width + 7) / 8;
192     macfb_draw_line_func *macfb_draw_line;
193 
194     switch (s->depth) {
195     case 1:
196         v = MACFB_DRAW_LINE1;
197         break;
198     case 2:
199         v = MACFB_DRAW_LINE2;
200         break;
201     case 4:
202         v = MACFB_DRAW_LINE4;
203         break;
204     case 8:
205         v = MACFB_DRAW_LINE8;
206         break;
207     case 16:
208         v = MACFB_DRAW_LINE16;
209         break;
210     case 24:
211         v = MACFB_DRAW_LINE24;
212         break;
213     }
214 
215     macfb_draw_line = macfb_draw_line_table[v];
216     assert(macfb_draw_line != NULL);
217 
218     snap = memory_region_snapshot_and_clear_dirty(&s->mem_vram, 0x0,
219                                              memory_region_size(&s->mem_vram),
220                                              DIRTY_MEMORY_VGA);
221 
222     ymin = -1;
223     page = 0;
224     for (y = 0; y < s->height; y++, page += macfb_stride) {
225         if (macfb_check_dirty(s, snap, page, macfb_stride)) {
226             uint8_t *data_display;
227 
228             data_display = surface_data(surface) + y * surface_stride(surface);
229             macfb_draw_line(s, data_display, page, s->width);
230 
231             if (ymin < 0) {
232                 ymin = y;
233             }
234         } else {
235             if (ymin >= 0) {
236                 dpy_gfx_update(s->con, 0, ymin, s->width, y - ymin);
237                 ymin = -1;
238             }
239         }
240     }
241 
242     if (ymin >= 0) {
243         dpy_gfx_update(s->con, 0, ymin, s->width, y - ymin);
244     }
245 
246     g_free(snap);
247 }
248 
249 static void macfb_invalidate_display(void *opaque)
250 {
251     MacfbState *s = opaque;
252 
253     memory_region_set_dirty(&s->mem_vram, 0, MACFB_VRAM_SIZE);
254 }
255 
256 static void macfb_update_display(void *opaque)
257 {
258     MacfbState *s = opaque;
259     DisplaySurface *surface = qemu_console_surface(s->con);
260 
261     qemu_flush_coalesced_mmio_buffer();
262 
263     if (s->width == 0 || s->height == 0) {
264         return;
265     }
266 
267     if (s->width != surface_width(surface) ||
268         s->height != surface_height(surface)) {
269         qemu_console_resize(s->con, s->width, s->height);
270     }
271 
272     macfb_draw_graphic(s);
273 }
274 
275 static void macfb_reset(MacfbState *s)
276 {
277     int i;
278 
279     s->palette_current = 0;
280     for (i = 0; i < 256; i++) {
281         s->color_palette[i * 3] = 255 - i;
282         s->color_palette[i * 3 + 1] = 255 - i;
283         s->color_palette[i * 3 + 2] = 255 - i;
284     }
285     memset(s->vram, 0, MACFB_VRAM_SIZE);
286     macfb_invalidate_display(s);
287 }
288 
289 static uint64_t macfb_ctrl_read(void *opaque,
290                                 hwaddr addr,
291                                 unsigned int size)
292 {
293     uint64_t val = 0;
294 
295     trace_macfb_ctrl_read(addr, val, size);
296     return val;
297 }
298 
299 static void macfb_ctrl_write(void *opaque,
300                              hwaddr addr,
301                              uint64_t val,
302                              unsigned int size)
303 {
304     MacfbState *s = opaque;
305     switch (addr) {
306     case DAFB_RESET:
307         s->palette_current = 0;
308         break;
309     case DAFB_LUT:
310         s->color_palette[s->palette_current] = val;
311         s->palette_current = (s->palette_current + 1) %
312                              ARRAY_SIZE(s->color_palette);
313         if (s->palette_current % 3) {
314             macfb_invalidate_display(s);
315         }
316         break;
317     }
318 
319     trace_macfb_ctrl_write(addr, val, size);
320 }
321 
322 static const MemoryRegionOps macfb_ctrl_ops = {
323     .read = macfb_ctrl_read,
324     .write = macfb_ctrl_write,
325     .endianness = DEVICE_BIG_ENDIAN,
326     .impl.min_access_size = 1,
327     .impl.max_access_size = 4,
328 };
329 
330 static int macfb_post_load(void *opaque, int version_id)
331 {
332     macfb_invalidate_display(opaque);
333     return 0;
334 }
335 
336 static const VMStateDescription vmstate_macfb = {
337     .name = "macfb",
338     .version_id = 1,
339     .minimum_version_id = 1,
340     .minimum_version_id_old = 1,
341     .post_load = macfb_post_load,
342     .fields = (VMStateField[]) {
343         VMSTATE_UINT8_ARRAY(color_palette, MacfbState, 256 * 3),
344         VMSTATE_UINT32(palette_current, MacfbState),
345         VMSTATE_END_OF_LIST()
346     }
347 };
348 
349 static const GraphicHwOps macfb_ops = {
350     .invalidate = macfb_invalidate_display,
351     .gfx_update = macfb_update_display,
352 };
353 
354 static bool macfb_common_realize(DeviceState *dev, MacfbState *s, Error **errp)
355 {
356     DisplaySurface *surface;
357 
358     if (s->depth != 1 && s->depth != 2 && s->depth != 4 && s->depth != 8 &&
359         s->depth != 16 && s->depth != 24) {
360         error_setg(errp, "unknown guest depth %d", s->depth);
361         return false;
362     }
363 
364     s->con = graphic_console_init(dev, 0, &macfb_ops, s);
365     surface = qemu_console_surface(s->con);
366 
367     if (surface_bits_per_pixel(surface) != 32) {
368         error_setg(errp, "unknown host depth %d",
369                    surface_bits_per_pixel(surface));
370         return false;
371     }
372 
373     memory_region_init_io(&s->mem_ctrl, OBJECT(dev), &macfb_ctrl_ops, s,
374                           "macfb-ctrl", 0x1000);
375 
376     memory_region_init_ram(&s->mem_vram, OBJECT(dev), "macfb-vram",
377                            MACFB_VRAM_SIZE, &error_abort);
378     s->vram = memory_region_get_ram_ptr(&s->mem_vram);
379     s->vram_bit_mask = MACFB_VRAM_SIZE - 1;
380     memory_region_set_coalescing(&s->mem_vram);
381 
382     return true;
383 }
384 
385 static void macfb_sysbus_realize(DeviceState *dev, Error **errp)
386 {
387     MacfbSysBusState *s = MACFB(dev);
388     MacfbState *ms = &s->macfb;
389 
390     if (!macfb_common_realize(dev, ms, errp)) {
391         return;
392     }
393 
394     sysbus_init_mmio(SYS_BUS_DEVICE(s), &ms->mem_ctrl);
395     sysbus_init_mmio(SYS_BUS_DEVICE(s), &ms->mem_vram);
396 }
397 
398 static void macfb_nubus_realize(DeviceState *dev, Error **errp)
399 {
400     NubusDevice *nd = NUBUS_DEVICE(dev);
401     MacfbNubusState *s = NUBUS_MACFB(dev);
402     MacfbNubusDeviceClass *ndc = NUBUS_MACFB_GET_CLASS(dev);
403     MacfbState *ms = &s->macfb;
404 
405     ndc->parent_realize(dev, errp);
406     if (*errp) {
407         return;
408     }
409 
410     if (!macfb_common_realize(dev, ms, errp)) {
411         return;
412     }
413 
414     memory_region_add_subregion(&nd->slot_mem, DAFB_BASE, &ms->mem_ctrl);
415     memory_region_add_subregion(&nd->slot_mem, VIDEO_BASE, &ms->mem_vram);
416 }
417 
418 static void macfb_sysbus_reset(DeviceState *d)
419 {
420     MacfbSysBusState *s = MACFB(d);
421     macfb_reset(&s->macfb);
422 }
423 
424 static void macfb_nubus_reset(DeviceState *d)
425 {
426     MacfbNubusState *s = NUBUS_MACFB(d);
427     macfb_reset(&s->macfb);
428 }
429 
430 static Property macfb_sysbus_properties[] = {
431     DEFINE_PROP_UINT32("width", MacfbSysBusState, macfb.width, 640),
432     DEFINE_PROP_UINT32("height", MacfbSysBusState, macfb.height, 480),
433     DEFINE_PROP_UINT8("depth", MacfbSysBusState, macfb.depth, 8),
434     DEFINE_PROP_END_OF_LIST(),
435 };
436 
437 static Property macfb_nubus_properties[] = {
438     DEFINE_PROP_UINT32("width", MacfbNubusState, macfb.width, 640),
439     DEFINE_PROP_UINT32("height", MacfbNubusState, macfb.height, 480),
440     DEFINE_PROP_UINT8("depth", MacfbNubusState, macfb.depth, 8),
441     DEFINE_PROP_END_OF_LIST(),
442 };
443 
444 static void macfb_sysbus_class_init(ObjectClass *klass, void *data)
445 {
446     DeviceClass *dc = DEVICE_CLASS(klass);
447 
448     dc->realize = macfb_sysbus_realize;
449     dc->desc = "SysBus Macintosh framebuffer";
450     dc->reset = macfb_sysbus_reset;
451     dc->vmsd = &vmstate_macfb;
452     device_class_set_props(dc, macfb_sysbus_properties);
453 }
454 
455 static void macfb_nubus_class_init(ObjectClass *klass, void *data)
456 {
457     DeviceClass *dc = DEVICE_CLASS(klass);
458     MacfbNubusDeviceClass *ndc = NUBUS_MACFB_CLASS(klass);
459 
460     device_class_set_parent_realize(dc, macfb_nubus_realize,
461                                     &ndc->parent_realize);
462     dc->desc = "Nubus Macintosh framebuffer";
463     dc->reset = macfb_nubus_reset;
464     dc->vmsd = &vmstate_macfb;
465     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
466     device_class_set_props(dc, macfb_nubus_properties);
467 }
468 
469 static TypeInfo macfb_sysbus_info = {
470     .name          = TYPE_MACFB,
471     .parent        = TYPE_SYS_BUS_DEVICE,
472     .instance_size = sizeof(MacfbSysBusState),
473     .class_init    = macfb_sysbus_class_init,
474 };
475 
476 static TypeInfo macfb_nubus_info = {
477     .name          = TYPE_NUBUS_MACFB,
478     .parent        = TYPE_NUBUS_DEVICE,
479     .instance_size = sizeof(MacfbNubusState),
480     .class_init    = macfb_nubus_class_init,
481     .class_size    = sizeof(MacfbNubusDeviceClass),
482 };
483 
484 static void macfb_register_types(void)
485 {
486     type_register_static(&macfb_sysbus_info);
487     type_register_static(&macfb_nubus_info);
488 }
489 
490 type_init(macfb_register_types)
491