]> git.proxmox.com Git - mirror_qemu.git/blame - hw/display/g364fb.c
macfb: don't clear interrupts when writing to DAFB_RESET
[mirror_qemu.git] / hw / display / g364fb.c
CommitLineData
1fc3d392
AJ
1/*
2 * QEMU G364 framebuffer Emulator.
3 *
97a3f6ff 4 * Copyright (c) 2007-2011 Herve Poussineau
1fc3d392
AJ
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
fad6cb1a 16 * You should have received a copy of the GNU General Public License along
8167ee88 17 * with this program; if not, see <http://www.gnu.org/licenses/>.
1fc3d392
AJ
18 */
19
47df5154 20#include "qemu/osdep.h"
f0353b0d 21#include "qemu/units.h"
83c9f4ca 22#include "hw/hw.h"
64552b6b 23#include "hw/irq.h"
a27bd6c7 24#include "hw/qdev-properties.h"
d5bfbaca 25#include "qapi/error.h"
d49b6836 26#include "qemu/error-report.h"
0b8fa32f 27#include "qemu/module.h"
28ecbaee
PB
28#include "ui/console.h"
29#include "ui/pixel_ops.h"
b213b370 30#include "trace.h"
83c9f4ca 31#include "hw/sysbus.h"
d6454270 32#include "migration/vmstate.h"
db1015e9 33#include "qom/object.h"
0add30cf 34
1fc3d392 35typedef struct G364State {
0add30cf 36 /* hardware */
97a3f6ff 37 uint32_t vram_size;
0add30cf 38 qemu_irq irq;
97a3f6ff
HP
39 MemoryRegion mem_vram;
40 MemoryRegion mem_ctrl;
0add30cf
AJ
41 /* registers */
42 uint8_t color_palette[256][3];
43 uint8_t cursor_palette[3][3];
44 uint16_t cursor[512];
45 uint32_t cursor_position;
1fc3d392 46 uint32_t ctla;
0add30cf
AJ
47 uint32_t top_of_screen;
48 uint32_t width, height; /* in pixels */
1fc3d392 49 /* display refresh support */
c78f7137 50 QemuConsole *con;
0add30cf
AJ
51 int depth;
52 int blanked;
1fc3d392
AJ
53} G364State;
54
97a3f6ff
HP
55#define REG_BOOT 0x000000
56#define REG_DISPLAY 0x000118
57#define REG_VDISPLAY 0x000150
58#define REG_CTLA 0x000300
59#define REG_TOP 0x000400
60#define REG_CURS_PAL 0x000508
61#define REG_CURS_POS 0x000638
62#define REG_CLR_PAL 0x000800
63#define REG_CURS_PAT 0x001000
64#define REG_RESET 0x100000
0add30cf
AJ
65
66#define CTLA_FORCE_BLANK 0x00000400
67#define CTLA_NO_CURSOR 0x00800000
68
1213406b
BS
69#define G364_PAGE_SIZE 4096
70
f7189ac8 71static inline int check_dirty(G364State *s, DirtyBitmapSnapshot *snap, ram_addr_t page)
0add30cf 72{
f7189ac8 73 return memory_region_snapshot_get_dirty(&s->mem_vram, snap, page, G364_PAGE_SIZE);
0add30cf
AJ
74}
75
76static void g364fb_draw_graphic8(G364State *s)
1fc3d392 77{
c78f7137 78 DisplaySurface *surface = qemu_console_surface(s->con);
f7189ac8 79 DirtyBitmapSnapshot *snap;
0add30cf
AJ
80 int i, w;
81 uint8_t *vram;
82 uint8_t *data_display, *dd;
7fcf0c24 83 ram_addr_t page;
0add30cf
AJ
84 int x, y;
85 int xmin, xmax;
86 int ymin, ymax;
87 int xcursor, ycursor;
88 unsigned int (*rgb_to_pixel)(unsigned int r, unsigned int g, unsigned int b);
89
c78f7137 90 switch (surface_bits_per_pixel(surface)) {
1fc3d392 91 case 8:
0add30cf
AJ
92 rgb_to_pixel = rgb_to_pixel8;
93 w = 1;
1fc3d392
AJ
94 break;
95 case 15:
0add30cf
AJ
96 rgb_to_pixel = rgb_to_pixel15;
97 w = 2;
1fc3d392
AJ
98 break;
99 case 16:
0add30cf
AJ
100 rgb_to_pixel = rgb_to_pixel16;
101 w = 2;
1fc3d392
AJ
102 break;
103 case 32:
0add30cf
AJ
104 rgb_to_pixel = rgb_to_pixel32;
105 w = 4;
1fc3d392
AJ
106 break;
107 default:
b213b370 108 hw_error("g364: unknown host depth %d",
c78f7137 109 surface_bits_per_pixel(surface));
1fc3d392
AJ
110 return;
111 }
112
97a3f6ff 113 page = 0;
0add30cf
AJ
114
115 x = y = 0;
116 xmin = s->width;
117 xmax = 0;
118 ymin = s->height;
119 ymax = 0;
120
121 if (!(s->ctla & CTLA_NO_CURSOR)) {
122 xcursor = s->cursor_position >> 12;
123 ycursor = s->cursor_position & 0xfff;
124 } else {
125 xcursor = ycursor = -65;
126 }
127
d5bfbaca 128 vram = memory_region_get_ram_ptr(&s->mem_vram) + s->top_of_screen;
0add30cf 129 /* XXX: out of range in vram? */
c78f7137 130 data_display = dd = surface_data(surface);
f7189ac8
PB
131 snap = memory_region_snapshot_and_clear_dirty(&s->mem_vram, 0, s->vram_size,
132 DIRTY_MEMORY_VGA);
0add30cf 133 while (y < s->height) {
f7189ac8 134 if (check_dirty(s, snap, page)) {
0add30cf
AJ
135 if (y < ymin)
136 ymin = ymax = y;
0add30cf
AJ
137 if (x < xmin)
138 xmin = x;
1213406b 139 for (i = 0; i < G364_PAGE_SIZE; i++) {
0add30cf
AJ
140 uint8_t index;
141 unsigned int color;
142 if (unlikely((y >= ycursor && y < ycursor + 64) &&
143 (x >= xcursor && x < xcursor + 64))) {
144 /* pointer area */
145 int xdiff = x - xcursor;
146 uint16_t curs = s->cursor[(y - ycursor) * 8 + xdiff / 8];
147 int op = (curs >> ((xdiff & 7) * 2)) & 3;
148 if (likely(op == 0)) {
149 /* transparent */
150 index = *vram;
151 color = (*rgb_to_pixel)(
152 s->color_palette[index][0],
153 s->color_palette[index][1],
154 s->color_palette[index][2]);
155 } else {
156 /* get cursor color */
157 index = op - 1;
158 color = (*rgb_to_pixel)(
159 s->cursor_palette[index][0],
160 s->cursor_palette[index][1],
161 s->cursor_palette[index][2]);
162 }
163 } else {
164 /* normal area */
165 index = *vram;
166 color = (*rgb_to_pixel)(
167 s->color_palette[index][0],
168 s->color_palette[index][1],
169 s->color_palette[index][2]);
170 }
171 memcpy(dd, &color, w);
172 dd += w;
173 x++;
174 vram++;
175 if (x == s->width) {
176 xmax = s->width - 1;
177 y++;
178 if (y == s->height) {
179 ymax = s->height - 1;
180 goto done;
181 }
c78f7137 182 data_display = dd = data_display + surface_stride(surface);
0add30cf
AJ
183 xmin = 0;
184 x = 0;
185 }
186 }
187 if (x > xmax)
188 xmax = x;
189 if (y > ymax)
190 ymax = y;
191 } else {
192 int dy;
7fcf0c24 193 if (xmax || ymax) {
c78f7137 194 dpy_gfx_update(s->con, xmin, ymin,
a93a4a22 195 xmax - xmin + 1, ymax - ymin + 1);
0add30cf
AJ
196 xmin = s->width;
197 xmax = 0;
198 ymin = s->height;
199 ymax = 0;
200 }
1213406b 201 x += G364_PAGE_SIZE;
0add30cf
AJ
202 dy = x / s->width;
203 x = x % s->width;
204 y += dy;
1213406b 205 vram += G364_PAGE_SIZE;
c78f7137 206 data_display += dy * surface_stride(surface);
0add30cf
AJ
207 dd = data_display + x * w;
208 }
1213406b 209 page += G364_PAGE_SIZE;
0add30cf
AJ
210 }
211
212done:
7fcf0c24 213 if (xmax || ymax) {
c78f7137 214 dpy_gfx_update(s->con, xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
0add30cf 215 }
723250d6 216 g_free(snap);
1fc3d392
AJ
217}
218
0add30cf 219static void g364fb_draw_blank(G364State *s)
1fc3d392 220{
c78f7137 221 DisplaySurface *surface = qemu_console_surface(s->con);
1fc3d392
AJ
222 int i, w;
223 uint8_t *d;
224
0add30cf
AJ
225 if (s->blanked) {
226 /* Screen is already blank. No need to redraw it */
1fc3d392 227 return;
0add30cf 228 }
1fc3d392 229
c78f7137
GH
230 w = s->width * surface_bytes_per_pixel(surface);
231 d = surface_data(surface);
0add30cf 232 for (i = 0; i < s->height; i++) {
1fc3d392 233 memset(d, 0, w);
c78f7137 234 d += surface_stride(surface);
1fc3d392 235 }
221bb2d5 236
91155f8b 237 dpy_gfx_update_full(s->con);
0add30cf 238 s->blanked = 1;
1fc3d392
AJ
239}
240
1fc3d392
AJ
241static void g364fb_update_display(void *opaque)
242{
243 G364State *s = opaque;
c78f7137 244 DisplaySurface *surface = qemu_console_surface(s->con);
1fc3d392 245
e9a07334
JK
246 qemu_flush_coalesced_mmio_buffer();
247
0add30cf 248 if (s->width == 0 || s->height == 0)
221bb2d5
AJ
249 return;
250
c78f7137
GH
251 if (s->width != surface_width(surface) ||
252 s->height != surface_height(surface)) {
253 qemu_console_resize(s->con, s->width, s->height);
221bb2d5 254 }
0add30cf
AJ
255
256 if (s->ctla & CTLA_FORCE_BLANK) {
257 g364fb_draw_blank(s);
258 } else if (s->depth == 8) {
259 g364fb_draw_graphic8(s);
260 } else {
b213b370 261 error_report("g364: unknown guest depth %d", s->depth);
1fc3d392 262 }
0add30cf
AJ
263
264 qemu_irq_raise(s->irq);
1fc3d392
AJ
265}
266
86178a57 267static inline void g364fb_invalidate_display(void *opaque)
1fc3d392
AJ
268{
269 G364State *s = opaque;
0add30cf
AJ
270
271 s->blanked = 0;
fd4aa979 272 memory_region_set_dirty(&s->mem_vram, 0, s->vram_size);
1fc3d392
AJ
273}
274
97a3f6ff 275static void g364fb_reset(G364State *s)
1fc3d392 276{
d5bfbaca
MCA
277 uint8_t *vram = memory_region_get_ram_ptr(&s->mem_vram);
278
0add30cf
AJ
279 qemu_irq_lower(s->irq);
280
281 memset(s->color_palette, 0, sizeof(s->color_palette));
282 memset(s->cursor_palette, 0, sizeof(s->cursor_palette));
283 memset(s->cursor, 0, sizeof(s->cursor));
284 s->cursor_position = 0;
285 s->ctla = 0;
286 s->top_of_screen = 0;
287 s->width = s->height = 0;
d5bfbaca 288 memset(vram, 0, s->vram_size);
97a3f6ff 289 g364fb_invalidate_display(s);
1fc3d392
AJ
290}
291
1fc3d392 292/* called for accesses to io ports */
97a3f6ff 293static uint64_t g364fb_ctrl_read(void *opaque,
a8170e5e 294 hwaddr addr,
97a3f6ff 295 unsigned int size)
1fc3d392 296{
0add30cf 297 G364State *s = opaque;
1fc3d392
AJ
298 uint32_t val;
299
0add30cf
AJ
300 if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
301 /* cursor pattern */
302 int idx = (addr - REG_CURS_PAT) >> 3;
303 val = s->cursor[idx];
304 } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
305 /* cursor palette */
306 int idx = (addr - REG_CURS_PAL) >> 3;
307 val = ((uint32_t)s->cursor_palette[idx][0] << 16);
308 val |= ((uint32_t)s->cursor_palette[idx][1] << 8);
309 val |= ((uint32_t)s->cursor_palette[idx][2] << 0);
310 } else {
311 switch (addr) {
0add30cf
AJ
312 case REG_DISPLAY:
313 val = s->width / 4;
314 break;
315 case REG_VDISPLAY:
316 val = s->height * 2;
317 break;
318 case REG_CTLA:
319 val = s->ctla;
320 break;
321 default:
322 {
883f2c59 323 error_report("g364: invalid read at [" HWADDR_FMT_plx "]",
b213b370 324 addr);
0add30cf
AJ
325 val = 0;
326 break;
327 }
328 }
1fc3d392
AJ
329 }
330
b213b370 331 trace_g364fb_read(addr, val);
1fc3d392
AJ
332
333 return val;
334}
335
0add30cf 336static void g364fb_update_depth(G364State *s)
1fc3d392 337{
38972938 338 static const int depths[8] = { 1, 2, 4, 8, 15, 16, 0 };
0add30cf
AJ
339 s->depth = depths[(s->ctla & 0x00700000) >> 20];
340}
1fc3d392 341
0add30cf
AJ
342static void g364_invalidate_cursor_position(G364State *s)
343{
c78f7137 344 DisplaySurface *surface = qemu_console_surface(s->con);
fd4aa979 345 int ymin, ymax, start, end;
1fc3d392 346
0add30cf
AJ
347 /* invalidate only near the cursor */
348 ymin = s->cursor_position & 0xfff;
349 ymax = MIN(s->height, ymin + 64);
c78f7137
GH
350 start = ymin * surface_stride(surface);
351 end = (ymax + 1) * surface_stride(surface);
1fc3d392 352
fd4aa979 353 memory_region_set_dirty(&s->mem_vram, start, end - start);
0add30cf
AJ
354}
355
97a3f6ff 356static void g364fb_ctrl_write(void *opaque,
a8170e5e 357 hwaddr addr,
97a3f6ff
HP
358 uint64_t val,
359 unsigned int size)
0add30cf
AJ
360{
361 G364State *s = opaque;
362
b213b370 363 trace_g364fb_write(addr, val);
0add30cf
AJ
364
365 if (addr >= REG_CLR_PAL && addr < REG_CLR_PAL + 0x800) {
1fc3d392 366 /* color palette */
0add30cf
AJ
367 int idx = (addr - REG_CLR_PAL) >> 3;
368 s->color_palette[idx][0] = (val >> 16) & 0xff;
369 s->color_palette[idx][1] = (val >> 8) & 0xff;
370 s->color_palette[idx][2] = val & 0xff;
371 g364fb_invalidate_display(s);
372 } else if (addr >= REG_CURS_PAT && addr < REG_CURS_PAT + 0x1000) {
373 /* cursor pattern */
374 int idx = (addr - REG_CURS_PAT) >> 3;
375 s->cursor[idx] = val;
376 g364fb_invalidate_display(s);
377 } else if (addr >= REG_CURS_PAL && addr < REG_CURS_PAL + 0x18) {
378 /* cursor palette */
379 int idx = (addr - REG_CURS_PAL) >> 3;
380 s->cursor_palette[idx][0] = (val >> 16) & 0xff;
381 s->cursor_palette[idx][1] = (val >> 8) & 0xff;
382 s->cursor_palette[idx][2] = val & 0xff;
383 g364fb_invalidate_display(s);
1fc3d392
AJ
384 } else {
385 switch (addr) {
97a3f6ff
HP
386 case REG_BOOT: /* Boot timing */
387 case 0x00108: /* Line timing: half sync */
388 case 0x00110: /* Line timing: back porch */
389 case 0x00120: /* Line timing: short display */
390 case 0x00128: /* Frame timing: broad pulse */
391 case 0x00130: /* Frame timing: v sync */
392 case 0x00138: /* Frame timing: v preequalise */
393 case 0x00140: /* Frame timing: v postequalise */
394 case 0x00148: /* Frame timing: v blank */
395 case 0x00158: /* Line timing: line time */
396 case 0x00160: /* Frame store: line start */
397 case 0x00168: /* vram cycle: mem init */
398 case 0x00170: /* vram cycle: transfer delay */
399 case 0x00200: /* vram cycle: mask register */
400 /* ignore */
401 break;
402 case REG_TOP:
403 s->top_of_screen = val;
404 g364fb_invalidate_display(s);
405 break;
406 case REG_DISPLAY:
407 s->width = val * 4;
408 break;
409 case REG_VDISPLAY:
410 s->height = val / 2;
411 break;
412 case REG_CTLA:
413 s->ctla = val;
414 g364fb_update_depth(s);
415 g364fb_invalidate_display(s);
416 break;
417 case REG_CURS_POS:
418 g364_invalidate_cursor_position(s);
419 s->cursor_position = val;
420 g364_invalidate_cursor_position(s);
421 break;
422 case REG_RESET:
423 g364fb_reset(s);
424 break;
425 default:
426 error_report("g364: invalid write of 0x%" PRIx64
883f2c59 427 " at [" HWADDR_FMT_plx "]", val, addr);
97a3f6ff 428 break;
1fc3d392
AJ
429 }
430 }
0add30cf 431 qemu_irq_lower(s->irq);
1fc3d392
AJ
432}
433
97a3f6ff
HP
434static const MemoryRegionOps g364fb_ctrl_ops = {
435 .read = g364fb_ctrl_read,
436 .write = g364fb_ctrl_write,
437 .endianness = DEVICE_LITTLE_ENDIAN,
438 .impl.min_access_size = 4,
439 .impl.max_access_size = 4,
1fc3d392
AJ
440};
441
97a3f6ff 442static int g364fb_post_load(void *opaque, int version_id)
1fc3d392
AJ
443{
444 G364State *s = opaque;
0add30cf
AJ
445
446 /* force refresh */
447 g364fb_update_depth(s);
448 g364fb_invalidate_display(s);
1fc3d392 449
0add30cf 450 return 0;
1fc3d392
AJ
451}
452
97a3f6ff
HP
453static const VMStateDescription vmstate_g364fb = {
454 .name = "g364fb",
d5bfbaca
MCA
455 .version_id = 2,
456 .minimum_version_id = 2,
97a3f6ff
HP
457 .post_load = g364fb_post_load,
458 .fields = (VMStateField[]) {
97a3f6ff
HP
459 VMSTATE_BUFFER_UNSAFE(color_palette, G364State, 0, 256 * 3),
460 VMSTATE_BUFFER_UNSAFE(cursor_palette, G364State, 0, 9),
461 VMSTATE_UINT16_ARRAY(cursor, G364State, 512),
462 VMSTATE_UINT32(cursor_position, G364State),
463 VMSTATE_UINT32(ctla, G364State),
464 VMSTATE_UINT32(top_of_screen, G364State),
465 VMSTATE_UINT32(width, G364State),
466 VMSTATE_UINT32(height, G364State),
467 VMSTATE_END_OF_LIST()
468 }
469};
1fc3d392 470
380cd056
GH
471static const GraphicHwOps g364fb_ops = {
472 .invalidate = g364fb_invalidate_display,
473 .gfx_update = g364fb_update_display,
474};
475
97a3f6ff 476static void g364fb_init(DeviceState *dev, G364State *s)
1fc3d392 477{
5643706a 478 s->con = graphic_console_init(dev, 0, &g364fb_ops, s);
1fc3d392 479
b9fc4f6e
PMD
480 memory_region_init_io(&s->mem_ctrl, OBJECT(dev), &g364fb_ctrl_ops, s,
481 "ctrl", 0x180000);
d5bfbaca
MCA
482 memory_region_init_ram(&s->mem_vram, NULL, "g364fb.vram", s->vram_size,
483 &error_fatal);
74259ae5 484 memory_region_set_log(&s->mem_vram, true, DIRTY_MEMORY_VGA);
97a3f6ff
HP
485}
486
0f31aa86 487#define TYPE_G364 "sysbus-g364"
8063396b 488OBJECT_DECLARE_SIMPLE_TYPE(G364SysBusState, G364)
0f31aa86 489
db1015e9 490struct G364SysBusState {
0f31aa86
AF
491 SysBusDevice parent_obj;
492
97a3f6ff 493 G364State g364;
db1015e9 494};
1fc3d392 495
0323ee43 496static void g364fb_sysbus_realize(DeviceState *dev, Error **errp)
97a3f6ff 497{
0f31aa86
AF
498 G364SysBusState *sbs = G364(dev);
499 G364State *s = &sbs->g364;
0323ee43 500 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
97a3f6ff 501
0f31aa86
AF
502 g364fb_init(dev, s);
503 sysbus_init_irq(sbd, &s->irq);
504 sysbus_init_mmio(sbd, &s->mem_ctrl);
505 sysbus_init_mmio(sbd, &s->mem_vram);
1fc3d392 506}
97a3f6ff
HP
507
508static void g364fb_sysbus_reset(DeviceState *d)
509{
0f31aa86
AF
510 G364SysBusState *s = G364(d);
511
97a3f6ff
HP
512 g364fb_reset(&s->g364);
513}
514
999e12bb 515static Property g364fb_sysbus_properties[] = {
f0353b0d 516 DEFINE_PROP_UINT32("vram_size", G364SysBusState, g364.vram_size, 8 * MiB),
999e12bb
AL
517 DEFINE_PROP_END_OF_LIST(),
518};
519
8660df5e
MCA
520static const VMStateDescription vmstate_g364fb_sysbus = {
521 .name = "g364fb-sysbus",
522 .version_id = 2,
523 .minimum_version_id = 2,
524 .fields = (VMStateField[]) {
525 VMSTATE_STRUCT(g364, G364SysBusState, 2, vmstate_g364fb, G364State),
526 VMSTATE_END_OF_LIST()
527 }
528};
529
999e12bb
AL
530static void g364fb_sysbus_class_init(ObjectClass *klass, void *data)
531{
39bffca2 532 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 533
0323ee43 534 dc->realize = g364fb_sysbus_realize;
125ee0ed 535 set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
39bffca2
AL
536 dc->desc = "G364 framebuffer";
537 dc->reset = g364fb_sysbus_reset;
8660df5e 538 dc->vmsd = &vmstate_g364fb_sysbus;
4f67d30b 539 device_class_set_props(dc, g364fb_sysbus_properties);
999e12bb
AL
540}
541
8c43a6f0 542static const TypeInfo g364fb_sysbus_info = {
0f31aa86 543 .name = TYPE_G364,
39bffca2
AL
544 .parent = TYPE_SYS_BUS_DEVICE,
545 .instance_size = sizeof(G364SysBusState),
546 .class_init = g364fb_sysbus_class_init,
97a3f6ff
HP
547};
548
83f7d43a 549static void g364fb_register_types(void)
97a3f6ff 550{
39bffca2 551 type_register_static(&g364fb_sysbus_info);
97a3f6ff
HP
552}
553
83f7d43a 554type_init(g364fb_register_types)