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 "framebuffer.h"
30 #include "ui/pixel_ops.h"
31 #include "hw/misc/bcm2835_mbox_defs.h"
34 #define DEFAULT_VCRAM_SIZE 0x4000000
35 #define BCM2835_FB_OFFSET 0x00100000
37 static void fb_invalidate_display(void *opaque
)
39 BCM2835FBState
*s
= BCM2835_FB(opaque
);
44 static void draw_line_src16(void *opaque
, uint8_t *dst
, const uint8_t *src
,
45 int width
, int deststep
)
47 BCM2835FBState
*s
= opaque
;
51 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
52 int bpp
= surface_bits_per_pixel(surface
);
57 /* lookup palette starting at video ram base
58 * TODO: cache translation, rather than doing this each time!
60 rgb888
= ldl_le_phys(&s
->dma_as
, s
->vcram_base
+ (*src
<< 2));
61 r
= (rgb888
>> 0) & 0xff;
62 g
= (rgb888
>> 8) & 0xff;
63 b
= (rgb888
>> 16) & 0xff;
67 rgb565
= lduw_le_p(src
);
68 r
= ((rgb565
>> 11) & 0x1f) << 3;
69 g
= ((rgb565
>> 5) & 0x3f) << 2;
70 b
= ((rgb565
>> 0) & 0x1f) << 3;
74 rgb888
= ldl_le_p(src
);
75 r
= (rgb888
>> 0) & 0xff;
76 g
= (rgb888
>> 8) & 0xff;
77 b
= (rgb888
>> 16) & 0xff;
81 rgb888
= ldl_le_p(src
);
82 r
= (rgb888
>> 0) & 0xff;
83 g
= (rgb888
>> 8) & 0xff;
84 b
= (rgb888
>> 16) & 0xff;
95 /* swap to BGR pixel format */
103 *dst
++ = rgb_to_pixel8(r
, g
, b
);
106 *(uint16_t *)dst
= rgb_to_pixel15(r
, g
, b
);
110 *(uint16_t *)dst
= rgb_to_pixel16(r
, g
, b
);
114 rgb888
= rgb_to_pixel24(r
, g
, b
);
115 *dst
++ = rgb888
& 0xff;
116 *dst
++ = (rgb888
>> 8) & 0xff;
117 *dst
++ = (rgb888
>> 16) & 0xff;
120 *(uint32_t *)dst
= rgb_to_pixel32(r
, g
, b
);
129 static void fb_update_display(void *opaque
)
131 BCM2835FBState
*s
= opaque
;
132 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
138 if (s
->lock
|| !s
->xres
) {
142 src_width
= s
->xres
* (s
->bpp
>> 3);
143 dest_width
= s
->xres
;
145 switch (surface_bits_per_pixel(surface
)) {
163 hw_error("bcm2835_fb: bad color depth\n");
168 framebuffer_update_memory_section(&s
->fbsection
, s
->dma_mr
, s
->base
,
172 framebuffer_update_display(surface
, &s
->fbsection
, s
->xres
, s
->yres
,
173 src_width
, dest_width
, 0, s
->invalidate
,
174 draw_line_src16
, s
, &first
, &last
);
177 dpy_gfx_update(s
->con
, 0, first
, s
->xres
, last
- first
+ 1);
180 s
->invalidate
= false;
183 static void bcm2835_fb_mbox_push(BCM2835FBState
*s
, uint32_t value
)
189 s
->xres
= ldl_le_phys(&s
->dma_as
, value
);
190 s
->yres
= ldl_le_phys(&s
->dma_as
, value
+ 4);
191 s
->xres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 8);
192 s
->yres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 12);
193 s
->bpp
= ldl_le_phys(&s
->dma_as
, value
+ 20);
194 s
->xoffset
= ldl_le_phys(&s
->dma_as
, value
+ 24);
195 s
->yoffset
= ldl_le_phys(&s
->dma_as
, value
+ 28);
197 s
->base
= s
->vcram_base
| (value
& 0xc0000000);
198 s
->base
+= BCM2835_FB_OFFSET
;
200 /* TODO - Manage properly virtual resolution */
202 s
->pitch
= s
->xres
* (s
->bpp
>> 3);
203 s
->size
= s
->yres
* s
->pitch
;
205 stl_le_phys(&s
->dma_as
, value
+ 16, s
->pitch
);
206 stl_le_phys(&s
->dma_as
, value
+ 32, s
->base
);
207 stl_le_phys(&s
->dma_as
, value
+ 36, s
->size
);
209 s
->invalidate
= true;
210 qemu_console_resize(s
->con
, s
->xres
, s
->yres
);
214 void bcm2835_fb_reconfigure(BCM2835FBState
*s
, uint32_t *xres
, uint32_t *yres
,
215 uint32_t *xoffset
, uint32_t *yoffset
, uint32_t *bpp
,
216 uint32_t *pixo
, uint32_t *alpha
)
220 /* TODO: input validation! */
228 s
->xoffset
= *xoffset
;
231 s
->yoffset
= *yoffset
;
243 /* TODO - Manage properly virtual resolution */
245 s
->pitch
= s
->xres
* (s
->bpp
>> 3);
246 s
->size
= s
->yres
* s
->pitch
;
248 s
->invalidate
= true;
249 qemu_console_resize(s
->con
, s
->xres
, s
->yres
);
253 static uint64_t bcm2835_fb_read(void *opaque
, hwaddr offset
, unsigned size
)
255 BCM2835FBState
*s
= opaque
;
262 qemu_set_irq(s
->mbox_irq
, 0);
265 case MBOX_AS_PENDING
:
270 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
278 static void bcm2835_fb_write(void *opaque
, hwaddr offset
, uint64_t value
,
281 BCM2835FBState
*s
= opaque
;
285 /* bcm2835_mbox should check our pending status before pushing */
288 bcm2835_fb_mbox_push(s
, value
);
289 qemu_set_irq(s
->mbox_irq
, 1);
293 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
299 static const MemoryRegionOps bcm2835_fb_ops
= {
300 .read
= bcm2835_fb_read
,
301 .write
= bcm2835_fb_write
,
302 .endianness
= DEVICE_NATIVE_ENDIAN
,
303 .valid
.min_access_size
= 4,
304 .valid
.max_access_size
= 4,
307 static const VMStateDescription vmstate_bcm2835_fb
= {
308 .name
= TYPE_BCM2835_FB
,
310 .minimum_version_id
= 1,
311 .fields
= (VMStateField
[]) {
312 VMSTATE_BOOL(lock
, BCM2835FBState
),
313 VMSTATE_BOOL(invalidate
, BCM2835FBState
),
314 VMSTATE_BOOL(pending
, BCM2835FBState
),
315 VMSTATE_UINT32(xres
, BCM2835FBState
),
316 VMSTATE_UINT32(yres
, BCM2835FBState
),
317 VMSTATE_UINT32(xres_virtual
, BCM2835FBState
),
318 VMSTATE_UINT32(yres_virtual
, BCM2835FBState
),
319 VMSTATE_UINT32(xoffset
, BCM2835FBState
),
320 VMSTATE_UINT32(yoffset
, BCM2835FBState
),
321 VMSTATE_UINT32(bpp
, BCM2835FBState
),
322 VMSTATE_UINT32(base
, BCM2835FBState
),
323 VMSTATE_UINT32(pitch
, BCM2835FBState
),
324 VMSTATE_UINT32(size
, BCM2835FBState
),
325 VMSTATE_UINT32(pixo
, BCM2835FBState
),
326 VMSTATE_UINT32(alpha
, BCM2835FBState
),
327 VMSTATE_END_OF_LIST()
331 static const GraphicHwOps vgafb_ops
= {
332 .invalidate
= fb_invalidate_display
,
333 .gfx_update
= fb_update_display
,
336 static void bcm2835_fb_init(Object
*obj
)
338 BCM2835FBState
*s
= BCM2835_FB(obj
);
340 memory_region_init_io(&s
->iomem
, obj
, &bcm2835_fb_ops
, s
, TYPE_BCM2835_FB
,
342 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
343 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
346 static void bcm2835_fb_reset(DeviceState
*dev
)
348 BCM2835FBState
*s
= BCM2835_FB(dev
);
352 s
->xres_virtual
= s
->xres
;
353 s
->yres_virtual
= s
->yres
;
356 s
->base
= s
->vcram_base
+ BCM2835_FB_OFFSET
;
357 s
->pitch
= s
->xres
* (s
->bpp
>> 3);
358 s
->size
= s
->yres
* s
->pitch
;
360 s
->invalidate
= true;
364 static void bcm2835_fb_realize(DeviceState
*dev
, Error
**errp
)
366 BCM2835FBState
*s
= BCM2835_FB(dev
);
370 if (s
->vcram_base
== 0) {
371 error_setg(errp
, "%s: required vcram-base property not set", __func__
);
375 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
377 error_setg(errp
, "%s: required dma-mr link not found: %s",
378 __func__
, error_get_pretty(err
));
382 s
->dma_mr
= MEMORY_REGION(obj
);
383 address_space_init(&s
->dma_as
, s
->dma_mr
, NULL
);
385 bcm2835_fb_reset(dev
);
387 s
->con
= graphic_console_init(dev
, 0, &vgafb_ops
, s
);
388 qemu_console_resize(s
->con
, s
->xres
, s
->yres
);
391 static Property bcm2835_fb_props
[] = {
392 DEFINE_PROP_UINT32("vcram-base", BCM2835FBState
, vcram_base
, 0),/*required*/
393 DEFINE_PROP_UINT32("vcram-size", BCM2835FBState
, vcram_size
,
395 DEFINE_PROP_UINT32("xres", BCM2835FBState
, xres
, 640),
396 DEFINE_PROP_UINT32("yres", BCM2835FBState
, yres
, 480),
397 DEFINE_PROP_UINT32("bpp", BCM2835FBState
, bpp
, 16),
398 DEFINE_PROP_UINT32("pixo", BCM2835FBState
, pixo
, 1), /* 1=RGB, 0=BGR */
399 DEFINE_PROP_UINT32("alpha", BCM2835FBState
, alpha
, 2), /* alpha ignored */
400 DEFINE_PROP_END_OF_LIST()
403 static void bcm2835_fb_class_init(ObjectClass
*klass
, void *data
)
405 DeviceClass
*dc
= DEVICE_CLASS(klass
);
407 dc
->props
= bcm2835_fb_props
;
408 dc
->realize
= bcm2835_fb_realize
;
409 dc
->reset
= bcm2835_fb_reset
;
410 dc
->vmsd
= &vmstate_bcm2835_fb
;
413 static TypeInfo bcm2835_fb_info
= {
414 .name
= TYPE_BCM2835_FB
,
415 .parent
= TYPE_SYS_BUS_DEVICE
,
416 .instance_size
= sizeof(BCM2835FBState
),
417 .class_init
= bcm2835_fb_class_init
,
418 .instance_init
= bcm2835_fb_init
,
421 static void bcm2835_fb_register_types(void)
423 type_register_static(&bcm2835_fb_info
);
426 type_init(bcm2835_fb_register_types
)