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