2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * Refactoring for Pi2 Copyright (c) 2015, Microsoft. Written by Andrew Baumann.
5 * Heavily based on milkymist-vgafb.c, copyright terms below:
6 * QEMU model of the Milkymist VGA framebuffer.
8 * Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "hw/display/bcm2835_fb.h"
30 #include "framebuffer.h"
31 #include "ui/pixel_ops.h"
32 #include "hw/misc/bcm2835_mbox_defs.h"
33 #include "hw/qdev-properties.h"
34 #include "migration/vmstate.h"
36 #include "qemu/module.h"
38 #define DEFAULT_VCRAM_SIZE 0x4000000
39 #define BCM2835_FB_OFFSET 0x00100000
41 /* Maximum permitted framebuffer size; experimentally determined on an rpi2 */
44 /* Framebuffer size used if guest requests zero size */
45 #define XRES_SMALL 592
46 #define YRES_SMALL 488
48 static void fb_invalidate_display(void *opaque
)
50 BCM2835FBState
*s
= BCM2835_FB(opaque
);
55 static void draw_line_src16(void *opaque
, uint8_t *dst
, const uint8_t *src
,
56 int width
, int deststep
)
58 BCM2835FBState
*s
= opaque
;
62 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
63 int bpp
= surface_bits_per_pixel(surface
);
66 switch (s
->config
.bpp
) {
68 /* lookup palette starting at video ram base
69 * TODO: cache translation, rather than doing this each time!
71 rgb888
= ldl_le_phys(&s
->dma_as
, s
->vcram_base
+ (*src
<< 2));
72 r
= (rgb888
>> 0) & 0xff;
73 g
= (rgb888
>> 8) & 0xff;
74 b
= (rgb888
>> 16) & 0xff;
78 rgb565
= lduw_le_p(src
);
79 r
= ((rgb565
>> 11) & 0x1f) << 3;
80 g
= ((rgb565
>> 5) & 0x3f) << 2;
81 b
= ((rgb565
>> 0) & 0x1f) << 3;
85 rgb888
= ldl_le_p(src
);
86 r
= (rgb888
>> 0) & 0xff;
87 g
= (rgb888
>> 8) & 0xff;
88 b
= (rgb888
>> 16) & 0xff;
92 rgb888
= ldl_le_p(src
);
93 r
= (rgb888
>> 0) & 0xff;
94 g
= (rgb888
>> 8) & 0xff;
95 b
= (rgb888
>> 16) & 0xff;
105 if (s
->config
.pixo
== 0) {
106 /* swap to BGR pixel format */
114 *dst
++ = rgb_to_pixel8(r
, g
, b
);
117 *(uint16_t *)dst
= rgb_to_pixel15(r
, g
, b
);
121 *(uint16_t *)dst
= rgb_to_pixel16(r
, g
, b
);
125 rgb888
= rgb_to_pixel24(r
, g
, b
);
126 *dst
++ = rgb888
& 0xff;
127 *dst
++ = (rgb888
>> 8) & 0xff;
128 *dst
++ = (rgb888
>> 16) & 0xff;
131 *(uint32_t *)dst
= rgb_to_pixel32(r
, g
, b
);
140 static bool fb_use_offsets(BCM2835FBConfig
*config
)
143 * Return true if we should use the viewport offsets.
144 * Experimentally, the hardware seems to do this only if the
145 * viewport size is larger than the physical screen. (It doesn't
146 * prevent the guest setting this silly viewport setting, though...)
148 return config
->xres_virtual
> config
->xres
&&
149 config
->yres_virtual
> config
->yres
;
152 static void fb_update_display(void *opaque
)
154 BCM2835FBState
*s
= opaque
;
155 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
160 uint32_t xoff
= 0, yoff
= 0;
162 if (s
->lock
|| !s
->config
.xres
) {
166 src_width
= bcm2835_fb_get_pitch(&s
->config
);
167 if (fb_use_offsets(&s
->config
)) {
168 xoff
= s
->config
.xoffset
;
169 yoff
= s
->config
.yoffset
;
172 dest_width
= s
->config
.xres
;
174 switch (surface_bits_per_pixel(surface
)) {
192 hw_error("bcm2835_fb: bad color depth\n");
197 hwaddr base
= s
->config
.base
+ xoff
+ (hwaddr
)yoff
* src_width
;
198 framebuffer_update_memory_section(&s
->fbsection
, s
->dma_mr
,
200 s
->config
.yres
, src_width
);
203 framebuffer_update_display(surface
, &s
->fbsection
,
204 s
->config
.xres
, s
->config
.yres
,
205 src_width
, dest_width
, 0, s
->invalidate
,
206 draw_line_src16
, s
, &first
, &last
);
209 dpy_gfx_update(s
->con
, 0, first
, s
->config
.xres
,
213 s
->invalidate
= false;
216 void bcm2835_fb_validate_config(BCM2835FBConfig
*config
)
219 * Validate the config, and clip any bogus values into range,
220 * as the hardware does. Note that fb_update_display() relies on
221 * this happening to prevent it from performing out-of-range
222 * accesses on redraw.
224 config
->xres
= MIN(config
->xres
, XRES_MAX
);
225 config
->xres_virtual
= MIN(config
->xres_virtual
, XRES_MAX
);
226 config
->yres
= MIN(config
->yres
, YRES_MAX
);
227 config
->yres_virtual
= MIN(config
->yres_virtual
, YRES_MAX
);
230 * These are not minima: a 40x40 framebuffer will be accepted.
231 * They're only used as defaults if the guest asks for zero size.
233 if (config
->xres
== 0) {
234 config
->xres
= XRES_SMALL
;
236 if (config
->yres
== 0) {
237 config
->yres
= YRES_SMALL
;
239 if (config
->xres_virtual
== 0) {
240 config
->xres_virtual
= config
->xres
;
242 if (config
->yres_virtual
== 0) {
243 config
->yres_virtual
= config
->yres
;
246 if (fb_use_offsets(config
)) {
247 /* Clip the offsets so the viewport is within the physical screen */
248 config
->xoffset
= MIN(config
->xoffset
,
249 config
->xres_virtual
- config
->xres
);
250 config
->yoffset
= MIN(config
->yoffset
,
251 config
->yres_virtual
- config
->yres
);
255 void bcm2835_fb_reconfigure(BCM2835FBState
*s
, BCM2835FBConfig
*newconfig
)
259 s
->config
= *newconfig
;
261 s
->invalidate
= true;
262 qemu_console_resize(s
->con
, s
->config
.xres
, s
->config
.yres
);
266 static void bcm2835_fb_mbox_push(BCM2835FBState
*s
, uint32_t value
)
270 BCM2835FBConfig newconf
;
274 newconf
.xres
= ldl_le_phys(&s
->dma_as
, value
);
275 newconf
.yres
= ldl_le_phys(&s
->dma_as
, value
+ 4);
276 newconf
.xres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 8);
277 newconf
.yres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 12);
278 newconf
.bpp
= ldl_le_phys(&s
->dma_as
, value
+ 20);
279 newconf
.xoffset
= ldl_le_phys(&s
->dma_as
, value
+ 24);
280 newconf
.yoffset
= ldl_le_phys(&s
->dma_as
, value
+ 28);
282 newconf
.base
= s
->vcram_base
| (value
& 0xc0000000);
283 newconf
.base
+= BCM2835_FB_OFFSET
;
285 bcm2835_fb_validate_config(&newconf
);
287 pitch
= bcm2835_fb_get_pitch(&newconf
);
288 size
= bcm2835_fb_get_size(&newconf
);
290 stl_le_phys(&s
->dma_as
, value
+ 16, pitch
);
291 stl_le_phys(&s
->dma_as
, value
+ 32, newconf
.base
);
292 stl_le_phys(&s
->dma_as
, value
+ 36, size
);
294 bcm2835_fb_reconfigure(s
, &newconf
);
297 static uint64_t bcm2835_fb_read(void *opaque
, hwaddr offset
, unsigned size
)
299 BCM2835FBState
*s
= opaque
;
306 qemu_set_irq(s
->mbox_irq
, 0);
309 case MBOX_AS_PENDING
:
314 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
322 static void bcm2835_fb_write(void *opaque
, hwaddr offset
, uint64_t value
,
325 BCM2835FBState
*s
= opaque
;
329 /* bcm2835_mbox should check our pending status before pushing */
332 bcm2835_fb_mbox_push(s
, value
);
333 qemu_set_irq(s
->mbox_irq
, 1);
337 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
343 static const MemoryRegionOps bcm2835_fb_ops
= {
344 .read
= bcm2835_fb_read
,
345 .write
= bcm2835_fb_write
,
346 .endianness
= DEVICE_NATIVE_ENDIAN
,
347 .valid
.min_access_size
= 4,
348 .valid
.max_access_size
= 4,
351 static const VMStateDescription vmstate_bcm2835_fb
= {
352 .name
= TYPE_BCM2835_FB
,
354 .minimum_version_id
= 1,
355 .fields
= (VMStateField
[]) {
356 VMSTATE_BOOL(lock
, BCM2835FBState
),
357 VMSTATE_BOOL(invalidate
, BCM2835FBState
),
358 VMSTATE_BOOL(pending
, BCM2835FBState
),
359 VMSTATE_UINT32(config
.xres
, BCM2835FBState
),
360 VMSTATE_UINT32(config
.yres
, BCM2835FBState
),
361 VMSTATE_UINT32(config
.xres_virtual
, BCM2835FBState
),
362 VMSTATE_UINT32(config
.yres_virtual
, BCM2835FBState
),
363 VMSTATE_UINT32(config
.xoffset
, BCM2835FBState
),
364 VMSTATE_UINT32(config
.yoffset
, BCM2835FBState
),
365 VMSTATE_UINT32(config
.bpp
, BCM2835FBState
),
366 VMSTATE_UINT32(config
.base
, BCM2835FBState
),
367 VMSTATE_UNUSED(8), /* Was pitch and size */
368 VMSTATE_UINT32(config
.pixo
, BCM2835FBState
),
369 VMSTATE_UINT32(config
.alpha
, BCM2835FBState
),
370 VMSTATE_END_OF_LIST()
374 static const GraphicHwOps vgafb_ops
= {
375 .invalidate
= fb_invalidate_display
,
376 .gfx_update
= fb_update_display
,
379 static void bcm2835_fb_init(Object
*obj
)
381 BCM2835FBState
*s
= BCM2835_FB(obj
);
383 memory_region_init_io(&s
->iomem
, obj
, &bcm2835_fb_ops
, s
, TYPE_BCM2835_FB
,
385 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
386 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
389 static void bcm2835_fb_reset(DeviceState
*dev
)
391 BCM2835FBState
*s
= BCM2835_FB(dev
);
395 s
->config
= s
->initial_config
;
397 s
->invalidate
= true;
401 static void bcm2835_fb_realize(DeviceState
*dev
, Error
**errp
)
403 BCM2835FBState
*s
= BCM2835_FB(dev
);
407 if (s
->vcram_base
== 0) {
408 error_setg(errp
, "%s: required vcram-base property not set", __func__
);
412 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
414 error_setg(errp
, "%s: required dma-mr link not found: %s",
415 __func__
, error_get_pretty(err
));
419 /* Fill in the parts of initial_config that are not set by QOM properties */
420 s
->initial_config
.xres_virtual
= s
->initial_config
.xres
;
421 s
->initial_config
.yres_virtual
= s
->initial_config
.yres
;
422 s
->initial_config
.xoffset
= 0;
423 s
->initial_config
.yoffset
= 0;
424 s
->initial_config
.base
= s
->vcram_base
+ BCM2835_FB_OFFSET
;
426 s
->dma_mr
= MEMORY_REGION(obj
);
427 address_space_init(&s
->dma_as
, s
->dma_mr
, TYPE_BCM2835_FB
"-memory");
429 bcm2835_fb_reset(dev
);
431 s
->con
= graphic_console_init(dev
, 0, &vgafb_ops
, s
);
432 qemu_console_resize(s
->con
, s
->config
.xres
, s
->config
.yres
);
435 static Property bcm2835_fb_props
[] = {
436 DEFINE_PROP_UINT32("vcram-base", BCM2835FBState
, vcram_base
, 0),/*required*/
437 DEFINE_PROP_UINT32("vcram-size", BCM2835FBState
, vcram_size
,
439 DEFINE_PROP_UINT32("xres", BCM2835FBState
, initial_config
.xres
, 640),
440 DEFINE_PROP_UINT32("yres", BCM2835FBState
, initial_config
.yres
, 480),
441 DEFINE_PROP_UINT32("bpp", BCM2835FBState
, initial_config
.bpp
, 16),
442 DEFINE_PROP_UINT32("pixo", BCM2835FBState
,
443 initial_config
.pixo
, 1), /* 1=RGB, 0=BGR */
444 DEFINE_PROP_UINT32("alpha", BCM2835FBState
,
445 initial_config
.alpha
, 2), /* alpha ignored */
446 DEFINE_PROP_END_OF_LIST()
449 static void bcm2835_fb_class_init(ObjectClass
*klass
, void *data
)
451 DeviceClass
*dc
= DEVICE_CLASS(klass
);
453 device_class_set_props(dc
, bcm2835_fb_props
);
454 dc
->realize
= bcm2835_fb_realize
;
455 dc
->reset
= bcm2835_fb_reset
;
456 dc
->vmsd
= &vmstate_bcm2835_fb
;
459 static TypeInfo bcm2835_fb_info
= {
460 .name
= TYPE_BCM2835_FB
,
461 .parent
= TYPE_SYS_BUS_DEVICE
,
462 .instance_size
= sizeof(BCM2835FBState
),
463 .class_init
= bcm2835_fb_class_init
,
464 .instance_init
= bcm2835_fb_init
,
467 static void bcm2835_fb_register_types(void)
469 type_register_static(&bcm2835_fb_info
);
472 type_init(bcm2835_fb_register_types
)