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
);
43 static void draw_line_src16(void *opaque
, uint8_t *dst
, const uint8_t *src
,
44 int width
, int deststep
)
46 BCM2835FBState
*s
= opaque
;
50 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
51 int bpp
= surface_bits_per_pixel(surface
);
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;
66 rgb565
= lduw_le_p(src
);
67 r
= ((rgb565
>> 11) & 0x1f) << 3;
68 g
= ((rgb565
>> 5) & 0x3f) << 2;
69 b
= ((rgb565
>> 0) & 0x1f) << 3;
73 rgb888
= ldl_le_p(src
);
74 r
= (rgb888
>> 0) & 0xff;
75 g
= (rgb888
>> 8) & 0xff;
76 b
= (rgb888
>> 16) & 0xff;
80 rgb888
= ldl_le_p(src
);
81 r
= (rgb888
>> 0) & 0xff;
82 g
= (rgb888
>> 8) & 0xff;
83 b
= (rgb888
>> 16) & 0xff;
94 /* swap to BGR pixel format */
102 *dst
++ = rgb_to_pixel8(r
, g
, b
);
105 *(uint16_t *)dst
= rgb_to_pixel15(r
, g
, b
);
109 *(uint16_t *)dst
= rgb_to_pixel16(r
, g
, b
);
113 rgb888
= rgb_to_pixel24(r
, g
, b
);
114 *dst
++ = rgb888
& 0xff;
115 *dst
++ = (rgb888
>> 8) & 0xff;
116 *dst
++ = (rgb888
>> 16) & 0xff;
119 *(uint32_t *)dst
= rgb_to_pixel32(r
, g
, b
);
128 static void fb_update_display(void *opaque
)
130 BCM2835FBState
*s
= opaque
;
131 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
137 if (s
->lock
|| !s
->xres
) {
141 src_width
= s
->xres
* (s
->bpp
>> 3);
142 dest_width
= s
->xres
;
144 switch (surface_bits_per_pixel(surface
)) {
162 hw_error("bcm2835_fb: bad color depth\n");
167 framebuffer_update_memory_section(&s
->fbsection
, s
->dma_mr
, s
->base
,
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
);
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
)
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
);
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
)
219 /* TODO: input validation! */
227 s
->xoffset
= *xoffset
;
230 s
->yoffset
= *yoffset
;
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
);
252 static uint64_t bcm2835_fb_read(void *opaque
, hwaddr offset
, unsigned size
)
254 BCM2835FBState
*s
= opaque
;
261 qemu_set_irq(s
->mbox_irq
, 0);
264 case MBOX_AS_PENDING
:
269 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
277 static void bcm2835_fb_write(void *opaque
, hwaddr offset
, uint64_t value
,
280 BCM2835FBState
*s
= opaque
;
284 /* bcm2835_mbox should check our pending status before pushing */
287 bcm2835_fb_mbox_push(s
, value
);
288 qemu_set_irq(s
->mbox_irq
, 1);
292 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
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
,
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
,
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
);
351 s
->xres_virtual
= s
->xres
;
352 s
->yres_virtual
= s
->yres
;
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;
363 static void bcm2835_fb_realize(DeviceState
*dev
, Error
**errp
)
365 BCM2835FBState
*s
= BCM2835_FB(dev
);
369 if (s
->vcram_base
== 0) {
370 error_setg(errp
, "%s: required vcram-base property not set", __func__
);
374 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
376 error_setg(errp
, "%s: required dma-mr link not found: %s",
377 __func__
, error_get_pretty(err
));
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
,
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
)