target-alpha: make cpu-qom.h not target specific
[qemu/ar7.git] / hw / display / bcm2835_fb.c
blob506f1d3d90697009ce706061a79a225bcd697462
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/display/framebuffer.h"
30 #include "ui/pixel_ops.h"
31 #include "hw/misc/bcm2835_mbox_defs.h"
33 #define DEFAULT_VCRAM_SIZE 0x4000000
34 #define BCM2835_FB_OFFSET 0x00100000
36 static void fb_invalidate_display(void *opaque)
38 BCM2835FBState *s = BCM2835_FB(opaque);
40 s->invalidate = true;
43 static void draw_line_src16(void *opaque, uint8_t *dst, const uint8_t *src,
44 int width, int deststep)
46 BCM2835FBState *s = opaque;
47 uint16_t rgb565;
48 uint32_t rgb888;
49 uint8_t r, g, b;
50 DisplaySurface *surface = qemu_console_surface(s->con);
51 int bpp = surface_bits_per_pixel(surface);
53 while (width--) {
54 switch (s->bpp) {
55 case 8:
56 /* lookup palette starting at video ram base
57 * TODO: cache translation, rather than doing this each time!
59 rgb888 = ldl_le_phys(&s->dma_as, s->vcram_base + (*src << 2));
60 r = (rgb888 >> 0) & 0xff;
61 g = (rgb888 >> 8) & 0xff;
62 b = (rgb888 >> 16) & 0xff;
63 src++;
64 break;
65 case 16:
66 rgb565 = lduw_le_p(src);
67 r = ((rgb565 >> 11) & 0x1f) << 3;
68 g = ((rgb565 >> 5) & 0x3f) << 2;
69 b = ((rgb565 >> 0) & 0x1f) << 3;
70 src += 2;
71 break;
72 case 24:
73 rgb888 = ldl_le_p(src);
74 r = (rgb888 >> 0) & 0xff;
75 g = (rgb888 >> 8) & 0xff;
76 b = (rgb888 >> 16) & 0xff;
77 src += 3;
78 break;
79 case 32:
80 rgb888 = ldl_le_p(src);
81 r = (rgb888 >> 0) & 0xff;
82 g = (rgb888 >> 8) & 0xff;
83 b = (rgb888 >> 16) & 0xff;
84 src += 4;
85 break;
86 default:
87 r = 0;
88 g = 0;
89 b = 0;
90 break;
93 if (s->pixo == 0) {
94 /* swap to BGR pixel format */
95 uint8_t tmp = r;
96 r = b;
97 b = tmp;
100 switch (bpp) {
101 case 8:
102 *dst++ = rgb_to_pixel8(r, g, b);
103 break;
104 case 15:
105 *(uint16_t *)dst = rgb_to_pixel15(r, g, b);
106 dst += 2;
107 break;
108 case 16:
109 *(uint16_t *)dst = rgb_to_pixel16(r, g, b);
110 dst += 2;
111 break;
112 case 24:
113 rgb888 = rgb_to_pixel24(r, g, b);
114 *dst++ = rgb888 & 0xff;
115 *dst++ = (rgb888 >> 8) & 0xff;
116 *dst++ = (rgb888 >> 16) & 0xff;
117 break;
118 case 32:
119 *(uint32_t *)dst = rgb_to_pixel32(r, g, b);
120 dst += 4;
121 break;
122 default:
123 return;
128 static void fb_update_display(void *opaque)
130 BCM2835FBState *s = opaque;
131 DisplaySurface *surface = qemu_console_surface(s->con);
132 int first = 0;
133 int last = 0;
134 int src_width = 0;
135 int dest_width = 0;
137 if (s->lock || !s->xres) {
138 return;
141 src_width = s->xres * (s->bpp >> 3);
142 dest_width = s->xres;
144 switch (surface_bits_per_pixel(surface)) {
145 case 0:
146 return;
147 case 8:
148 break;
149 case 15:
150 dest_width *= 2;
151 break;
152 case 16:
153 dest_width *= 2;
154 break;
155 case 24:
156 dest_width *= 3;
157 break;
158 case 32:
159 dest_width *= 4;
160 break;
161 default:
162 hw_error("bcm2835_fb: bad color depth\n");
163 break;
166 if (s->invalidate) {
167 framebuffer_update_memory_section(&s->fbsection, s->dma_mr, s->base,
168 s->yres, src_width);
171 framebuffer_update_display(surface, &s->fbsection, s->xres, s->yres,
172 src_width, dest_width, 0, s->invalidate,
173 draw_line_src16, s, &first, &last);
175 if (first >= 0) {
176 dpy_gfx_update(s->con, 0, first, s->xres, last - first + 1);
179 s->invalidate = false;
182 static void bcm2835_fb_mbox_push(BCM2835FBState *s, uint32_t value)
184 value &= ~0xf;
186 s->lock = true;
188 s->xres = ldl_le_phys(&s->dma_as, value);
189 s->yres = ldl_le_phys(&s->dma_as, value + 4);
190 s->xres_virtual = ldl_le_phys(&s->dma_as, value + 8);
191 s->yres_virtual = ldl_le_phys(&s->dma_as, value + 12);
192 s->bpp = ldl_le_phys(&s->dma_as, value + 20);
193 s->xoffset = ldl_le_phys(&s->dma_as, value + 24);
194 s->yoffset = ldl_le_phys(&s->dma_as, value + 28);
196 s->base = s->vcram_base | (value & 0xc0000000);
197 s->base += BCM2835_FB_OFFSET;
199 /* TODO - Manage properly virtual resolution */
201 s->pitch = s->xres * (s->bpp >> 3);
202 s->size = s->yres * s->pitch;
204 stl_le_phys(&s->dma_as, value + 16, s->pitch);
205 stl_le_phys(&s->dma_as, value + 32, s->base);
206 stl_le_phys(&s->dma_as, value + 36, s->size);
208 s->invalidate = true;
209 qemu_console_resize(s->con, s->xres, s->yres);
210 s->lock = false;
213 void bcm2835_fb_reconfigure(BCM2835FBState *s, uint32_t *xres, uint32_t *yres,
214 uint32_t *xoffset, uint32_t *yoffset, uint32_t *bpp,
215 uint32_t *pixo, uint32_t *alpha)
217 s->lock = true;
219 /* TODO: input validation! */
220 if (xres) {
221 s->xres = *xres;
223 if (yres) {
224 s->yres = *yres;
226 if (xoffset) {
227 s->xoffset = *xoffset;
229 if (yoffset) {
230 s->yoffset = *yoffset;
232 if (bpp) {
233 s->bpp = *bpp;
235 if (pixo) {
236 s->pixo = *pixo;
238 if (alpha) {
239 s->alpha = *alpha;
242 /* TODO - Manage properly virtual resolution */
244 s->pitch = s->xres * (s->bpp >> 3);
245 s->size = s->yres * s->pitch;
247 s->invalidate = true;
248 qemu_console_resize(s->con, s->xres, s->yres);
249 s->lock = false;
252 static uint64_t bcm2835_fb_read(void *opaque, hwaddr offset, unsigned size)
254 BCM2835FBState *s = opaque;
255 uint32_t res = 0;
257 switch (offset) {
258 case MBOX_AS_DATA:
259 res = MBOX_CHAN_FB;
260 s->pending = false;
261 qemu_set_irq(s->mbox_irq, 0);
262 break;
264 case MBOX_AS_PENDING:
265 res = s->pending;
266 break;
268 default:
269 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
270 __func__, offset);
271 return 0;
274 return res;
277 static void bcm2835_fb_write(void *opaque, hwaddr offset, uint64_t value,
278 unsigned size)
280 BCM2835FBState *s = opaque;
282 switch (offset) {
283 case MBOX_AS_DATA:
284 /* bcm2835_mbox should check our pending status before pushing */
285 assert(!s->pending);
286 s->pending = true;
287 bcm2835_fb_mbox_push(s, value);
288 qemu_set_irq(s->mbox_irq, 1);
289 break;
291 default:
292 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
293 __func__, offset);
294 return;
298 static const MemoryRegionOps bcm2835_fb_ops = {
299 .read = bcm2835_fb_read,
300 .write = bcm2835_fb_write,
301 .endianness = DEVICE_NATIVE_ENDIAN,
302 .valid.min_access_size = 4,
303 .valid.max_access_size = 4,
306 static const VMStateDescription vmstate_bcm2835_fb = {
307 .name = TYPE_BCM2835_FB,
308 .version_id = 1,
309 .minimum_version_id = 1,
310 .fields = (VMStateField[]) {
311 VMSTATE_BOOL(lock, BCM2835FBState),
312 VMSTATE_BOOL(invalidate, BCM2835FBState),
313 VMSTATE_BOOL(pending, BCM2835FBState),
314 VMSTATE_UINT32(xres, BCM2835FBState),
315 VMSTATE_UINT32(yres, BCM2835FBState),
316 VMSTATE_UINT32(xres_virtual, BCM2835FBState),
317 VMSTATE_UINT32(yres_virtual, BCM2835FBState),
318 VMSTATE_UINT32(xoffset, BCM2835FBState),
319 VMSTATE_UINT32(yoffset, BCM2835FBState),
320 VMSTATE_UINT32(bpp, BCM2835FBState),
321 VMSTATE_UINT32(base, BCM2835FBState),
322 VMSTATE_UINT32(pitch, BCM2835FBState),
323 VMSTATE_UINT32(size, BCM2835FBState),
324 VMSTATE_UINT32(pixo, BCM2835FBState),
325 VMSTATE_UINT32(alpha, BCM2835FBState),
326 VMSTATE_END_OF_LIST()
330 static const GraphicHwOps vgafb_ops = {
331 .invalidate = fb_invalidate_display,
332 .gfx_update = fb_update_display,
335 static void bcm2835_fb_init(Object *obj)
337 BCM2835FBState *s = BCM2835_FB(obj);
339 memory_region_init_io(&s->iomem, obj, &bcm2835_fb_ops, s, TYPE_BCM2835_FB,
340 0x10);
341 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
342 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
345 static void bcm2835_fb_reset(DeviceState *dev)
347 BCM2835FBState *s = BCM2835_FB(dev);
349 s->pending = false;
351 s->xres_virtual = s->xres;
352 s->yres_virtual = s->yres;
353 s->xoffset = 0;
354 s->yoffset = 0;
355 s->base = s->vcram_base + BCM2835_FB_OFFSET;
356 s->pitch = s->xres * (s->bpp >> 3);
357 s->size = s->yres * s->pitch;
359 s->invalidate = true;
360 s->lock = false;
363 static void bcm2835_fb_realize(DeviceState *dev, Error **errp)
365 BCM2835FBState *s = BCM2835_FB(dev);
366 Error *err = NULL;
367 Object *obj;
369 if (s->vcram_base == 0) {
370 error_setg(errp, "%s: required vcram-base property not set", __func__);
371 return;
374 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
375 if (obj == NULL) {
376 error_setg(errp, "%s: required dma-mr link not found: %s",
377 __func__, error_get_pretty(err));
378 return;
381 s->dma_mr = MEMORY_REGION(obj);
382 address_space_init(&s->dma_as, s->dma_mr, NULL);
384 bcm2835_fb_reset(dev);
386 s->con = graphic_console_init(dev, 0, &vgafb_ops, s);
387 qemu_console_resize(s->con, s->xres, s->yres);
390 static Property bcm2835_fb_props[] = {
391 DEFINE_PROP_UINT32("vcram-base", BCM2835FBState, vcram_base, 0),/*required*/
392 DEFINE_PROP_UINT32("vcram-size", BCM2835FBState, vcram_size,
393 DEFAULT_VCRAM_SIZE),
394 DEFINE_PROP_UINT32("xres", BCM2835FBState, xres, 640),
395 DEFINE_PROP_UINT32("yres", BCM2835FBState, yres, 480),
396 DEFINE_PROP_UINT32("bpp", BCM2835FBState, bpp, 16),
397 DEFINE_PROP_UINT32("pixo", BCM2835FBState, pixo, 1), /* 1=RGB, 0=BGR */
398 DEFINE_PROP_UINT32("alpha", BCM2835FBState, alpha, 2), /* alpha ignored */
399 DEFINE_PROP_END_OF_LIST()
402 static void bcm2835_fb_class_init(ObjectClass *klass, void *data)
404 DeviceClass *dc = DEVICE_CLASS(klass);
406 dc->props = bcm2835_fb_props;
407 dc->realize = bcm2835_fb_realize;
408 dc->reset = bcm2835_fb_reset;
409 dc->vmsd = &vmstate_bcm2835_fb;
412 static TypeInfo bcm2835_fb_info = {
413 .name = TYPE_BCM2835_FB,
414 .parent = TYPE_SYS_BUS_DEVICE,
415 .instance_size = sizeof(BCM2835FBState),
416 .class_init = bcm2835_fb_class_init,
417 .instance_init = bcm2835_fb_init,
420 static void bcm2835_fb_register_types(void)
422 type_register_static(&bcm2835_fb_info);
425 type_init(bcm2835_fb_register_types)