Include hw/irq.h a lot less
[qemu/kevin.git] / hw / display / bcm2835_fb.c
blobe446355f2993aca73f25263208eaa9c2e32209dc
1 /*
2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * Refactoring for Pi2 Copyright (c) 2015, Microsoft. Written by Andrew Baumann.
4 * This code is licensed under the GNU GPLv2 and later.
6 * Heavily based on milkymist-vgafb.c, copyright terms below:
7 * QEMU model of the Milkymist VGA framebuffer.
9 * Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "hw/display/bcm2835_fb.h"
29 #include "hw/irq.h"
30 #include "framebuffer.h"
31 #include "ui/pixel_ops.h"
32 #include "hw/misc/bcm2835_mbox_defs.h"
33 #include "qemu/log.h"
34 #include "qemu/module.h"
36 #define DEFAULT_VCRAM_SIZE 0x4000000
37 #define BCM2835_FB_OFFSET 0x00100000
39 /* Maximum permitted framebuffer size; experimentally determined on an rpi2 */
40 #define XRES_MAX 3840
41 #define YRES_MAX 2560
42 /* Framebuffer size used if guest requests zero size */
43 #define XRES_SMALL 592
44 #define YRES_SMALL 488
46 static void fb_invalidate_display(void *opaque)
48 BCM2835FBState *s = BCM2835_FB(opaque);
50 s->invalidate = true;
53 static void draw_line_src16(void *opaque, uint8_t *dst, const uint8_t *src,
54 int width, int deststep)
56 BCM2835FBState *s = opaque;
57 uint16_t rgb565;
58 uint32_t rgb888;
59 uint8_t r, g, b;
60 DisplaySurface *surface = qemu_console_surface(s->con);
61 int bpp = surface_bits_per_pixel(surface);
63 while (width--) {
64 switch (s->config.bpp) {
65 case 8:
66 /* lookup palette starting at video ram base
67 * TODO: cache translation, rather than doing this each time!
69 rgb888 = ldl_le_phys(&s->dma_as, s->vcram_base + (*src << 2));
70 r = (rgb888 >> 0) & 0xff;
71 g = (rgb888 >> 8) & 0xff;
72 b = (rgb888 >> 16) & 0xff;
73 src++;
74 break;
75 case 16:
76 rgb565 = lduw_le_p(src);
77 r = ((rgb565 >> 11) & 0x1f) << 3;
78 g = ((rgb565 >> 5) & 0x3f) << 2;
79 b = ((rgb565 >> 0) & 0x1f) << 3;
80 src += 2;
81 break;
82 case 24:
83 rgb888 = ldl_le_p(src);
84 r = (rgb888 >> 0) & 0xff;
85 g = (rgb888 >> 8) & 0xff;
86 b = (rgb888 >> 16) & 0xff;
87 src += 3;
88 break;
89 case 32:
90 rgb888 = ldl_le_p(src);
91 r = (rgb888 >> 0) & 0xff;
92 g = (rgb888 >> 8) & 0xff;
93 b = (rgb888 >> 16) & 0xff;
94 src += 4;
95 break;
96 default:
97 r = 0;
98 g = 0;
99 b = 0;
100 break;
103 if (s->config.pixo == 0) {
104 /* swap to BGR pixel format */
105 uint8_t tmp = r;
106 r = b;
107 b = tmp;
110 switch (bpp) {
111 case 8:
112 *dst++ = rgb_to_pixel8(r, g, b);
113 break;
114 case 15:
115 *(uint16_t *)dst = rgb_to_pixel15(r, g, b);
116 dst += 2;
117 break;
118 case 16:
119 *(uint16_t *)dst = rgb_to_pixel16(r, g, b);
120 dst += 2;
121 break;
122 case 24:
123 rgb888 = rgb_to_pixel24(r, g, b);
124 *dst++ = rgb888 & 0xff;
125 *dst++ = (rgb888 >> 8) & 0xff;
126 *dst++ = (rgb888 >> 16) & 0xff;
127 break;
128 case 32:
129 *(uint32_t *)dst = rgb_to_pixel32(r, g, b);
130 dst += 4;
131 break;
132 default:
133 return;
138 static bool fb_use_offsets(BCM2835FBConfig *config)
141 * Return true if we should use the viewport offsets.
142 * Experimentally, the hardware seems to do this only if the
143 * viewport size is larger than the physical screen. (It doesn't
144 * prevent the guest setting this silly viewport setting, though...)
146 return config->xres_virtual > config->xres &&
147 config->yres_virtual > config->yres;
150 static void fb_update_display(void *opaque)
152 BCM2835FBState *s = opaque;
153 DisplaySurface *surface = qemu_console_surface(s->con);
154 int first = 0;
155 int last = 0;
156 int src_width = 0;
157 int dest_width = 0;
158 uint32_t xoff = 0, yoff = 0;
160 if (s->lock || !s->config.xres) {
161 return;
164 src_width = bcm2835_fb_get_pitch(&s->config);
165 if (fb_use_offsets(&s->config)) {
166 xoff = s->config.xoffset;
167 yoff = s->config.yoffset;
170 dest_width = s->config.xres;
172 switch (surface_bits_per_pixel(surface)) {
173 case 0:
174 return;
175 case 8:
176 break;
177 case 15:
178 dest_width *= 2;
179 break;
180 case 16:
181 dest_width *= 2;
182 break;
183 case 24:
184 dest_width *= 3;
185 break;
186 case 32:
187 dest_width *= 4;
188 break;
189 default:
190 hw_error("bcm2835_fb: bad color depth\n");
191 break;
194 if (s->invalidate) {
195 hwaddr base = s->config.base + xoff + (hwaddr)yoff * src_width;
196 framebuffer_update_memory_section(&s->fbsection, s->dma_mr,
197 base,
198 s->config.yres, src_width);
201 framebuffer_update_display(surface, &s->fbsection,
202 s->config.xres, s->config.yres,
203 src_width, dest_width, 0, s->invalidate,
204 draw_line_src16, s, &first, &last);
206 if (first >= 0) {
207 dpy_gfx_update(s->con, 0, first, s->config.xres,
208 last - first + 1);
211 s->invalidate = false;
214 void bcm2835_fb_validate_config(BCM2835FBConfig *config)
217 * Validate the config, and clip any bogus values into range,
218 * as the hardware does. Note that fb_update_display() relies on
219 * this happening to prevent it from performing out-of-range
220 * accesses on redraw.
222 config->xres = MIN(config->xres, XRES_MAX);
223 config->xres_virtual = MIN(config->xres_virtual, XRES_MAX);
224 config->yres = MIN(config->yres, YRES_MAX);
225 config->yres_virtual = MIN(config->yres_virtual, YRES_MAX);
228 * These are not minima: a 40x40 framebuffer will be accepted.
229 * They're only used as defaults if the guest asks for zero size.
231 if (config->xres == 0) {
232 config->xres = XRES_SMALL;
234 if (config->yres == 0) {
235 config->yres = YRES_SMALL;
237 if (config->xres_virtual == 0) {
238 config->xres_virtual = config->xres;
240 if (config->yres_virtual == 0) {
241 config->yres_virtual = config->yres;
244 if (fb_use_offsets(config)) {
245 /* Clip the offsets so the viewport is within the physical screen */
246 config->xoffset = MIN(config->xoffset,
247 config->xres_virtual - config->xres);
248 config->yoffset = MIN(config->yoffset,
249 config->yres_virtual - config->yres);
253 void bcm2835_fb_reconfigure(BCM2835FBState *s, BCM2835FBConfig *newconfig)
255 s->lock = true;
257 s->config = *newconfig;
259 s->invalidate = true;
260 qemu_console_resize(s->con, s->config.xres, s->config.yres);
261 s->lock = false;
264 static void bcm2835_fb_mbox_push(BCM2835FBState *s, uint32_t value)
266 uint32_t pitch;
267 uint32_t size;
268 BCM2835FBConfig newconf;
270 value &= ~0xf;
272 newconf.xres = ldl_le_phys(&s->dma_as, value);
273 newconf.yres = ldl_le_phys(&s->dma_as, value + 4);
274 newconf.xres_virtual = ldl_le_phys(&s->dma_as, value + 8);
275 newconf.yres_virtual = ldl_le_phys(&s->dma_as, value + 12);
276 newconf.bpp = ldl_le_phys(&s->dma_as, value + 20);
277 newconf.xoffset = ldl_le_phys(&s->dma_as, value + 24);
278 newconf.yoffset = ldl_le_phys(&s->dma_as, value + 28);
280 newconf.base = s->vcram_base | (value & 0xc0000000);
281 newconf.base += BCM2835_FB_OFFSET;
283 bcm2835_fb_validate_config(&newconf);
285 pitch = bcm2835_fb_get_pitch(&newconf);
286 size = bcm2835_fb_get_size(&newconf);
288 stl_le_phys(&s->dma_as, value + 16, pitch);
289 stl_le_phys(&s->dma_as, value + 32, newconf.base);
290 stl_le_phys(&s->dma_as, value + 36, size);
292 bcm2835_fb_reconfigure(s, &newconf);
295 static uint64_t bcm2835_fb_read(void *opaque, hwaddr offset, unsigned size)
297 BCM2835FBState *s = opaque;
298 uint32_t res = 0;
300 switch (offset) {
301 case MBOX_AS_DATA:
302 res = MBOX_CHAN_FB;
303 s->pending = false;
304 qemu_set_irq(s->mbox_irq, 0);
305 break;
307 case MBOX_AS_PENDING:
308 res = s->pending;
309 break;
311 default:
312 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
313 __func__, offset);
314 return 0;
317 return res;
320 static void bcm2835_fb_write(void *opaque, hwaddr offset, uint64_t value,
321 unsigned size)
323 BCM2835FBState *s = opaque;
325 switch (offset) {
326 case MBOX_AS_DATA:
327 /* bcm2835_mbox should check our pending status before pushing */
328 assert(!s->pending);
329 s->pending = true;
330 bcm2835_fb_mbox_push(s, value);
331 qemu_set_irq(s->mbox_irq, 1);
332 break;
334 default:
335 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
336 __func__, offset);
337 return;
341 static const MemoryRegionOps bcm2835_fb_ops = {
342 .read = bcm2835_fb_read,
343 .write = bcm2835_fb_write,
344 .endianness = DEVICE_NATIVE_ENDIAN,
345 .valid.min_access_size = 4,
346 .valid.max_access_size = 4,
349 static const VMStateDescription vmstate_bcm2835_fb = {
350 .name = TYPE_BCM2835_FB,
351 .version_id = 1,
352 .minimum_version_id = 1,
353 .fields = (VMStateField[]) {
354 VMSTATE_BOOL(lock, BCM2835FBState),
355 VMSTATE_BOOL(invalidate, BCM2835FBState),
356 VMSTATE_BOOL(pending, BCM2835FBState),
357 VMSTATE_UINT32(config.xres, BCM2835FBState),
358 VMSTATE_UINT32(config.yres, BCM2835FBState),
359 VMSTATE_UINT32(config.xres_virtual, BCM2835FBState),
360 VMSTATE_UINT32(config.yres_virtual, BCM2835FBState),
361 VMSTATE_UINT32(config.xoffset, BCM2835FBState),
362 VMSTATE_UINT32(config.yoffset, BCM2835FBState),
363 VMSTATE_UINT32(config.bpp, BCM2835FBState),
364 VMSTATE_UINT32(config.base, BCM2835FBState),
365 VMSTATE_UNUSED(8), /* Was pitch and size */
366 VMSTATE_UINT32(config.pixo, BCM2835FBState),
367 VMSTATE_UINT32(config.alpha, BCM2835FBState),
368 VMSTATE_END_OF_LIST()
372 static const GraphicHwOps vgafb_ops = {
373 .invalidate = fb_invalidate_display,
374 .gfx_update = fb_update_display,
377 static void bcm2835_fb_init(Object *obj)
379 BCM2835FBState *s = BCM2835_FB(obj);
381 memory_region_init_io(&s->iomem, obj, &bcm2835_fb_ops, s, TYPE_BCM2835_FB,
382 0x10);
383 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
384 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
387 static void bcm2835_fb_reset(DeviceState *dev)
389 BCM2835FBState *s = BCM2835_FB(dev);
391 s->pending = false;
393 s->config = s->initial_config;
395 s->invalidate = true;
396 s->lock = false;
399 static void bcm2835_fb_realize(DeviceState *dev, Error **errp)
401 BCM2835FBState *s = BCM2835_FB(dev);
402 Error *err = NULL;
403 Object *obj;
405 if (s->vcram_base == 0) {
406 error_setg(errp, "%s: required vcram-base property not set", __func__);
407 return;
410 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
411 if (obj == NULL) {
412 error_setg(errp, "%s: required dma-mr link not found: %s",
413 __func__, error_get_pretty(err));
414 return;
417 /* Fill in the parts of initial_config that are not set by QOM properties */
418 s->initial_config.xres_virtual = s->initial_config.xres;
419 s->initial_config.yres_virtual = s->initial_config.yres;
420 s->initial_config.xoffset = 0;
421 s->initial_config.yoffset = 0;
422 s->initial_config.base = s->vcram_base + BCM2835_FB_OFFSET;
424 s->dma_mr = MEMORY_REGION(obj);
425 address_space_init(&s->dma_as, s->dma_mr, NULL);
427 bcm2835_fb_reset(dev);
429 s->con = graphic_console_init(dev, 0, &vgafb_ops, s);
430 qemu_console_resize(s->con, s->config.xres, s->config.yres);
433 static Property bcm2835_fb_props[] = {
434 DEFINE_PROP_UINT32("vcram-base", BCM2835FBState, vcram_base, 0),/*required*/
435 DEFINE_PROP_UINT32("vcram-size", BCM2835FBState, vcram_size,
436 DEFAULT_VCRAM_SIZE),
437 DEFINE_PROP_UINT32("xres", BCM2835FBState, initial_config.xres, 640),
438 DEFINE_PROP_UINT32("yres", BCM2835FBState, initial_config.yres, 480),
439 DEFINE_PROP_UINT32("bpp", BCM2835FBState, initial_config.bpp, 16),
440 DEFINE_PROP_UINT32("pixo", BCM2835FBState,
441 initial_config.pixo, 1), /* 1=RGB, 0=BGR */
442 DEFINE_PROP_UINT32("alpha", BCM2835FBState,
443 initial_config.alpha, 2), /* alpha ignored */
444 DEFINE_PROP_END_OF_LIST()
447 static void bcm2835_fb_class_init(ObjectClass *klass, void *data)
449 DeviceClass *dc = DEVICE_CLASS(klass);
451 dc->props = bcm2835_fb_props;
452 dc->realize = bcm2835_fb_realize;
453 dc->reset = bcm2835_fb_reset;
454 dc->vmsd = &vmstate_bcm2835_fb;
457 static TypeInfo bcm2835_fb_info = {
458 .name = TYPE_BCM2835_FB,
459 .parent = TYPE_SYS_BUS_DEVICE,
460 .instance_size = sizeof(BCM2835FBState),
461 .class_init = bcm2835_fb_class_init,
462 .instance_init = bcm2835_fb_init,
465 static void bcm2835_fb_register_types(void)
467 type_register_static(&bcm2835_fb_info);
470 type_init(bcm2835_fb_register_types)