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 "hw/display/bcm2835_fb.h"
28 #include "hw/display/framebuffer.h"
29 #include "ui/pixel_ops.h"
30 #include "hw/misc/bcm2835_mbox_defs.h"
32 #define DEFAULT_VCRAM_SIZE 0x4000000
33 #define BCM2835_FB_OFFSET 0x00100000
35 static void fb_invalidate_display(void *opaque
)
37 BCM2835FBState
*s
= BCM2835_FB(opaque
);
42 static void draw_line_src16(void *opaque
, uint8_t *dst
, const uint8_t *src
,
43 int width
, int deststep
)
45 BCM2835FBState
*s
= opaque
;
49 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
50 int bpp
= surface_bits_per_pixel(surface
);
55 /* lookup palette starting at video ram base
56 * TODO: cache translation, rather than doing this each time!
58 rgb888
= ldl_le_phys(&s
->dma_as
, s
->vcram_base
+ (*src
<< 2));
59 r
= (rgb888
>> 0) & 0xff;
60 g
= (rgb888
>> 8) & 0xff;
61 b
= (rgb888
>> 16) & 0xff;
65 rgb565
= lduw_le_p(src
);
66 r
= ((rgb565
>> 11) & 0x1f) << 3;
67 g
= ((rgb565
>> 5) & 0x3f) << 2;
68 b
= ((rgb565
>> 0) & 0x1f) << 3;
72 rgb888
= ldl_le_p(src
);
73 r
= (rgb888
>> 0) & 0xff;
74 g
= (rgb888
>> 8) & 0xff;
75 b
= (rgb888
>> 16) & 0xff;
79 rgb888
= ldl_le_p(src
);
80 r
= (rgb888
>> 0) & 0xff;
81 g
= (rgb888
>> 8) & 0xff;
82 b
= (rgb888
>> 16) & 0xff;
93 /* swap to BGR pixel format */
101 *dst
++ = rgb_to_pixel8(r
, g
, b
);
104 *(uint16_t *)dst
= rgb_to_pixel15(r
, g
, b
);
108 *(uint16_t *)dst
= rgb_to_pixel16(r
, g
, b
);
112 rgb888
= rgb_to_pixel24(r
, g
, b
);
113 *dst
++ = rgb888
& 0xff;
114 *dst
++ = (rgb888
>> 8) & 0xff;
115 *dst
++ = (rgb888
>> 16) & 0xff;
118 *(uint32_t *)dst
= rgb_to_pixel32(r
, g
, b
);
127 static void fb_update_display(void *opaque
)
129 BCM2835FBState
*s
= opaque
;
130 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
136 if (s
->lock
|| !s
->xres
) {
140 src_width
= s
->xres
* (s
->bpp
>> 3);
141 dest_width
= s
->xres
;
143 switch (surface_bits_per_pixel(surface
)) {
161 hw_error("bcm2835_fb: bad color depth\n");
166 framebuffer_update_memory_section(&s
->fbsection
, s
->dma_mr
, s
->base
,
170 framebuffer_update_display(surface
, &s
->fbsection
, s
->xres
, s
->yres
,
171 src_width
, dest_width
, 0, s
->invalidate
,
172 draw_line_src16
, s
, &first
, &last
);
175 dpy_gfx_update(s
->con
, 0, first
, s
->xres
, last
- first
+ 1);
178 s
->invalidate
= false;
181 static void bcm2835_fb_mbox_push(BCM2835FBState
*s
, uint32_t value
)
187 s
->xres
= ldl_le_phys(&s
->dma_as
, value
);
188 s
->yres
= ldl_le_phys(&s
->dma_as
, value
+ 4);
189 s
->xres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 8);
190 s
->yres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 12);
191 s
->bpp
= ldl_le_phys(&s
->dma_as
, value
+ 20);
192 s
->xoffset
= ldl_le_phys(&s
->dma_as
, value
+ 24);
193 s
->yoffset
= ldl_le_phys(&s
->dma_as
, value
+ 28);
195 s
->base
= s
->vcram_base
| (value
& 0xc0000000);
196 s
->base
+= BCM2835_FB_OFFSET
;
198 /* TODO - Manage properly virtual resolution */
200 s
->pitch
= s
->xres
* (s
->bpp
>> 3);
201 s
->size
= s
->yres
* s
->pitch
;
203 stl_le_phys(&s
->dma_as
, value
+ 16, s
->pitch
);
204 stl_le_phys(&s
->dma_as
, value
+ 32, s
->base
);
205 stl_le_phys(&s
->dma_as
, value
+ 36, s
->size
);
207 s
->invalidate
= true;
208 qemu_console_resize(s
->con
, s
->xres
, s
->yres
);
212 void bcm2835_fb_reconfigure(BCM2835FBState
*s
, uint32_t *xres
, uint32_t *yres
,
213 uint32_t *xoffset
, uint32_t *yoffset
, uint32_t *bpp
,
214 uint32_t *pixo
, uint32_t *alpha
)
218 /* TODO: input validation! */
226 s
->xoffset
= *xoffset
;
229 s
->yoffset
= *yoffset
;
241 /* TODO - Manage properly virtual resolution */
243 s
->pitch
= s
->xres
* (s
->bpp
>> 3);
244 s
->size
= s
->yres
* s
->pitch
;
246 s
->invalidate
= true;
247 qemu_console_resize(s
->con
, s
->xres
, s
->yres
);
251 static uint64_t bcm2835_fb_read(void *opaque
, hwaddr offset
, unsigned size
)
253 BCM2835FBState
*s
= opaque
;
260 qemu_set_irq(s
->mbox_irq
, 0);
263 case MBOX_AS_PENDING
:
268 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
276 static void bcm2835_fb_write(void *opaque
, hwaddr offset
, uint64_t value
,
279 BCM2835FBState
*s
= opaque
;
283 /* bcm2835_mbox should check our pending status before pushing */
286 bcm2835_fb_mbox_push(s
, value
);
287 qemu_set_irq(s
->mbox_irq
, 1);
291 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
297 static const MemoryRegionOps bcm2835_fb_ops
= {
298 .read
= bcm2835_fb_read
,
299 .write
= bcm2835_fb_write
,
300 .endianness
= DEVICE_NATIVE_ENDIAN
,
301 .valid
.min_access_size
= 4,
302 .valid
.max_access_size
= 4,
305 static const VMStateDescription vmstate_bcm2835_fb
= {
306 .name
= TYPE_BCM2835_FB
,
308 .minimum_version_id
= 1,
309 .fields
= (VMStateField
[]) {
310 VMSTATE_BOOL(lock
, BCM2835FBState
),
311 VMSTATE_BOOL(invalidate
, BCM2835FBState
),
312 VMSTATE_BOOL(pending
, BCM2835FBState
),
313 VMSTATE_UINT32(xres
, BCM2835FBState
),
314 VMSTATE_UINT32(yres
, BCM2835FBState
),
315 VMSTATE_UINT32(xres_virtual
, BCM2835FBState
),
316 VMSTATE_UINT32(yres_virtual
, BCM2835FBState
),
317 VMSTATE_UINT32(xoffset
, BCM2835FBState
),
318 VMSTATE_UINT32(yoffset
, BCM2835FBState
),
319 VMSTATE_UINT32(bpp
, BCM2835FBState
),
320 VMSTATE_UINT32(base
, BCM2835FBState
),
321 VMSTATE_UINT32(pitch
, BCM2835FBState
),
322 VMSTATE_UINT32(size
, BCM2835FBState
),
323 VMSTATE_UINT32(pixo
, BCM2835FBState
),
324 VMSTATE_UINT32(alpha
, BCM2835FBState
),
325 VMSTATE_END_OF_LIST()
329 static const GraphicHwOps vgafb_ops
= {
330 .invalidate
= fb_invalidate_display
,
331 .gfx_update
= fb_update_display
,
334 static void bcm2835_fb_init(Object
*obj
)
336 BCM2835FBState
*s
= BCM2835_FB(obj
);
338 memory_region_init_io(&s
->iomem
, obj
, &bcm2835_fb_ops
, s
, TYPE_BCM2835_FB
,
340 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
341 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
344 static void bcm2835_fb_reset(DeviceState
*dev
)
346 BCM2835FBState
*s
= BCM2835_FB(dev
);
350 s
->xres_virtual
= s
->xres
;
351 s
->yres_virtual
= s
->yres
;
354 s
->base
= s
->vcram_base
+ BCM2835_FB_OFFSET
;
355 s
->pitch
= s
->xres
* (s
->bpp
>> 3);
356 s
->size
= s
->yres
* s
->pitch
;
358 s
->invalidate
= true;
362 static void bcm2835_fb_realize(DeviceState
*dev
, Error
**errp
)
364 BCM2835FBState
*s
= BCM2835_FB(dev
);
368 if (s
->vcram_base
== 0) {
369 error_setg(errp
, "%s: required vcram-base property not set", __func__
);
373 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
375 error_setg(errp
, "%s: required dma-mr link not found: %s",
376 __func__
, error_get_pretty(err
));
380 s
->dma_mr
= MEMORY_REGION(obj
);
381 address_space_init(&s
->dma_as
, s
->dma_mr
, NULL
);
383 bcm2835_fb_reset(dev
);
385 s
->con
= graphic_console_init(dev
, 0, &vgafb_ops
, s
);
386 qemu_console_resize(s
->con
, s
->xres
, s
->yres
);
389 static Property bcm2835_fb_props
[] = {
390 DEFINE_PROP_UINT32("vcram-base", BCM2835FBState
, vcram_base
, 0),/*required*/
391 DEFINE_PROP_UINT32("vcram-size", BCM2835FBState
, vcram_size
,
393 DEFINE_PROP_UINT32("xres", BCM2835FBState
, xres
, 640),
394 DEFINE_PROP_UINT32("yres", BCM2835FBState
, yres
, 480),
395 DEFINE_PROP_UINT32("bpp", BCM2835FBState
, bpp
, 16),
396 DEFINE_PROP_UINT32("pixo", BCM2835FBState
, pixo
, 1), /* 1=RGB, 0=BGR */
397 DEFINE_PROP_UINT32("alpha", BCM2835FBState
, alpha
, 2), /* alpha ignored */
398 DEFINE_PROP_END_OF_LIST()
401 static void bcm2835_fb_class_init(ObjectClass
*klass
, void *data
)
403 DeviceClass
*dc
= DEVICE_CLASS(klass
);
405 dc
->props
= bcm2835_fb_props
;
406 dc
->realize
= bcm2835_fb_realize
;
407 dc
->reset
= bcm2835_fb_reset
;
408 dc
->vmsd
= &vmstate_bcm2835_fb
;
411 static TypeInfo bcm2835_fb_info
= {
412 .name
= TYPE_BCM2835_FB
,
413 .parent
= TYPE_SYS_BUS_DEVICE
,
414 .instance_size
= sizeof(BCM2835FBState
),
415 .class_init
= bcm2835_fb_class_init
,
416 .instance_init
= bcm2835_fb_init
,
419 static void bcm2835_fb_register_types(void)
421 type_register_static(&bcm2835_fb_info
);
424 type_init(bcm2835_fb_register_types
)