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