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 /* Maximum permitted framebuffer size; experimentally determined on an rpi2 */
40 /* Framebuffer size used if guest requests zero size */
41 #define XRES_SMALL 592
42 #define YRES_SMALL 488
44 static void fb_invalidate_display(void *opaque
)
46 BCM2835FBState
*s
= BCM2835_FB(opaque
);
51 static void draw_line_src16(void *opaque
, uint8_t *dst
, const uint8_t *src
,
52 int width
, int deststep
)
54 BCM2835FBState
*s
= opaque
;
58 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
59 int bpp
= surface_bits_per_pixel(surface
);
62 switch (s
->config
.bpp
) {
64 /* lookup palette starting at video ram base
65 * TODO: cache translation, rather than doing this each time!
67 rgb888
= ldl_le_phys(&s
->dma_as
, s
->vcram_base
+ (*src
<< 2));
68 r
= (rgb888
>> 0) & 0xff;
69 g
= (rgb888
>> 8) & 0xff;
70 b
= (rgb888
>> 16) & 0xff;
74 rgb565
= lduw_le_p(src
);
75 r
= ((rgb565
>> 11) & 0x1f) << 3;
76 g
= ((rgb565
>> 5) & 0x3f) << 2;
77 b
= ((rgb565
>> 0) & 0x1f) << 3;
81 rgb888
= ldl_le_p(src
);
82 r
= (rgb888
>> 0) & 0xff;
83 g
= (rgb888
>> 8) & 0xff;
84 b
= (rgb888
>> 16) & 0xff;
88 rgb888
= ldl_le_p(src
);
89 r
= (rgb888
>> 0) & 0xff;
90 g
= (rgb888
>> 8) & 0xff;
91 b
= (rgb888
>> 16) & 0xff;
101 if (s
->config
.pixo
== 0) {
102 /* swap to BGR pixel format */
110 *dst
++ = rgb_to_pixel8(r
, g
, b
);
113 *(uint16_t *)dst
= rgb_to_pixel15(r
, g
, b
);
117 *(uint16_t *)dst
= rgb_to_pixel16(r
, g
, b
);
121 rgb888
= rgb_to_pixel24(r
, g
, b
);
122 *dst
++ = rgb888
& 0xff;
123 *dst
++ = (rgb888
>> 8) & 0xff;
124 *dst
++ = (rgb888
>> 16) & 0xff;
127 *(uint32_t *)dst
= rgb_to_pixel32(r
, g
, b
);
136 static bool fb_use_offsets(BCM2835FBConfig
*config
)
139 * Return true if we should use the viewport offsets.
140 * Experimentally, the hardware seems to do this only if the
141 * viewport size is larger than the physical screen. (It doesn't
142 * prevent the guest setting this silly viewport setting, though...)
144 return config
->xres_virtual
> config
->xres
&&
145 config
->yres_virtual
> config
->yres
;
148 static void fb_update_display(void *opaque
)
150 BCM2835FBState
*s
= opaque
;
151 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
156 uint32_t xoff
= 0, yoff
= 0;
158 if (s
->lock
|| !s
->config
.xres
) {
162 src_width
= bcm2835_fb_get_pitch(&s
->config
);
163 if (fb_use_offsets(&s
->config
)) {
164 xoff
= s
->config
.xoffset
;
165 yoff
= s
->config
.yoffset
;
168 dest_width
= s
->config
.xres
;
170 switch (surface_bits_per_pixel(surface
)) {
188 hw_error("bcm2835_fb: bad color depth\n");
193 hwaddr base
= s
->config
.base
+ xoff
+ (hwaddr
)yoff
* src_width
;
194 framebuffer_update_memory_section(&s
->fbsection
, s
->dma_mr
,
196 s
->config
.yres
, src_width
);
199 framebuffer_update_display(surface
, &s
->fbsection
,
200 s
->config
.xres
, s
->config
.yres
,
201 src_width
, dest_width
, 0, s
->invalidate
,
202 draw_line_src16
, s
, &first
, &last
);
205 dpy_gfx_update(s
->con
, 0, first
, s
->config
.xres
,
209 s
->invalidate
= false;
212 void bcm2835_fb_validate_config(BCM2835FBConfig
*config
)
215 * Validate the config, and clip any bogus values into range,
216 * as the hardware does. Note that fb_update_display() relies on
217 * this happening to prevent it from performing out-of-range
218 * accesses on redraw.
220 config
->xres
= MIN(config
->xres
, XRES_MAX
);
221 config
->xres_virtual
= MIN(config
->xres_virtual
, XRES_MAX
);
222 config
->yres
= MIN(config
->yres
, YRES_MAX
);
223 config
->yres_virtual
= MIN(config
->yres_virtual
, YRES_MAX
);
226 * These are not minima: a 40x40 framebuffer will be accepted.
227 * They're only used as defaults if the guest asks for zero size.
229 if (config
->xres
== 0) {
230 config
->xres
= XRES_SMALL
;
232 if (config
->yres
== 0) {
233 config
->yres
= YRES_SMALL
;
235 if (config
->xres_virtual
== 0) {
236 config
->xres_virtual
= config
->xres
;
238 if (config
->yres_virtual
== 0) {
239 config
->yres_virtual
= config
->yres
;
242 if (fb_use_offsets(config
)) {
243 /* Clip the offsets so the viewport is within the physical screen */
244 config
->xoffset
= MIN(config
->xoffset
,
245 config
->xres_virtual
- config
->xres
);
246 config
->yoffset
= MIN(config
->yoffset
,
247 config
->yres_virtual
- config
->yres
);
251 void bcm2835_fb_reconfigure(BCM2835FBState
*s
, BCM2835FBConfig
*newconfig
)
255 s
->config
= *newconfig
;
257 s
->invalidate
= true;
258 qemu_console_resize(s
->con
, s
->config
.xres
, s
->config
.yres
);
262 static void bcm2835_fb_mbox_push(BCM2835FBState
*s
, uint32_t value
)
266 BCM2835FBConfig newconf
;
270 newconf
.xres
= ldl_le_phys(&s
->dma_as
, value
);
271 newconf
.yres
= ldl_le_phys(&s
->dma_as
, value
+ 4);
272 newconf
.xres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 8);
273 newconf
.yres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 12);
274 newconf
.bpp
= ldl_le_phys(&s
->dma_as
, value
+ 20);
275 newconf
.xoffset
= ldl_le_phys(&s
->dma_as
, value
+ 24);
276 newconf
.yoffset
= ldl_le_phys(&s
->dma_as
, value
+ 28);
278 newconf
.base
= s
->vcram_base
| (value
& 0xc0000000);
279 newconf
.base
+= BCM2835_FB_OFFSET
;
281 bcm2835_fb_validate_config(&newconf
);
283 pitch
= bcm2835_fb_get_pitch(&newconf
);
284 size
= bcm2835_fb_get_size(&newconf
);
286 stl_le_phys(&s
->dma_as
, value
+ 16, pitch
);
287 stl_le_phys(&s
->dma_as
, value
+ 32, newconf
.base
);
288 stl_le_phys(&s
->dma_as
, value
+ 36, size
);
290 bcm2835_fb_reconfigure(s
, &newconf
);
293 static uint64_t bcm2835_fb_read(void *opaque
, hwaddr offset
, unsigned size
)
295 BCM2835FBState
*s
= opaque
;
302 qemu_set_irq(s
->mbox_irq
, 0);
305 case MBOX_AS_PENDING
:
310 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
318 static void bcm2835_fb_write(void *opaque
, hwaddr offset
, uint64_t value
,
321 BCM2835FBState
*s
= opaque
;
325 /* bcm2835_mbox should check our pending status before pushing */
328 bcm2835_fb_mbox_push(s
, value
);
329 qemu_set_irq(s
->mbox_irq
, 1);
333 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
339 static const MemoryRegionOps bcm2835_fb_ops
= {
340 .read
= bcm2835_fb_read
,
341 .write
= bcm2835_fb_write
,
342 .endianness
= DEVICE_NATIVE_ENDIAN
,
343 .valid
.min_access_size
= 4,
344 .valid
.max_access_size
= 4,
347 static const VMStateDescription vmstate_bcm2835_fb
= {
348 .name
= TYPE_BCM2835_FB
,
350 .minimum_version_id
= 1,
351 .fields
= (VMStateField
[]) {
352 VMSTATE_BOOL(lock
, BCM2835FBState
),
353 VMSTATE_BOOL(invalidate
, BCM2835FBState
),
354 VMSTATE_BOOL(pending
, BCM2835FBState
),
355 VMSTATE_UINT32(config
.xres
, BCM2835FBState
),
356 VMSTATE_UINT32(config
.yres
, BCM2835FBState
),
357 VMSTATE_UINT32(config
.xres_virtual
, BCM2835FBState
),
358 VMSTATE_UINT32(config
.yres_virtual
, BCM2835FBState
),
359 VMSTATE_UINT32(config
.xoffset
, BCM2835FBState
),
360 VMSTATE_UINT32(config
.yoffset
, BCM2835FBState
),
361 VMSTATE_UINT32(config
.bpp
, BCM2835FBState
),
362 VMSTATE_UINT32(config
.base
, BCM2835FBState
),
363 VMSTATE_UNUSED(8), /* Was pitch and size */
364 VMSTATE_UINT32(config
.pixo
, BCM2835FBState
),
365 VMSTATE_UINT32(config
.alpha
, BCM2835FBState
),
366 VMSTATE_END_OF_LIST()
370 static const GraphicHwOps vgafb_ops
= {
371 .invalidate
= fb_invalidate_display
,
372 .gfx_update
= fb_update_display
,
375 static void bcm2835_fb_init(Object
*obj
)
377 BCM2835FBState
*s
= BCM2835_FB(obj
);
379 memory_region_init_io(&s
->iomem
, obj
, &bcm2835_fb_ops
, s
, TYPE_BCM2835_FB
,
381 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
382 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
385 static void bcm2835_fb_reset(DeviceState
*dev
)
387 BCM2835FBState
*s
= BCM2835_FB(dev
);
391 s
->config
= s
->initial_config
;
393 s
->invalidate
= true;
397 static void bcm2835_fb_realize(DeviceState
*dev
, Error
**errp
)
399 BCM2835FBState
*s
= BCM2835_FB(dev
);
403 if (s
->vcram_base
== 0) {
404 error_setg(errp
, "%s: required vcram-base property not set", __func__
);
408 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
410 error_setg(errp
, "%s: required dma-mr link not found: %s",
411 __func__
, error_get_pretty(err
));
415 /* Fill in the parts of initial_config that are not set by QOM properties */
416 s
->initial_config
.xres_virtual
= s
->initial_config
.xres
;
417 s
->initial_config
.yres_virtual
= s
->initial_config
.yres
;
418 s
->initial_config
.xoffset
= 0;
419 s
->initial_config
.yoffset
= 0;
420 s
->initial_config
.base
= s
->vcram_base
+ BCM2835_FB_OFFSET
;
422 s
->dma_mr
= MEMORY_REGION(obj
);
423 address_space_init(&s
->dma_as
, s
->dma_mr
, NULL
);
425 bcm2835_fb_reset(dev
);
427 s
->con
= graphic_console_init(dev
, 0, &vgafb_ops
, s
);
428 qemu_console_resize(s
->con
, s
->config
.xres
, s
->config
.yres
);
431 static Property bcm2835_fb_props
[] = {
432 DEFINE_PROP_UINT32("vcram-base", BCM2835FBState
, vcram_base
, 0),/*required*/
433 DEFINE_PROP_UINT32("vcram-size", BCM2835FBState
, vcram_size
,
435 DEFINE_PROP_UINT32("xres", BCM2835FBState
, initial_config
.xres
, 640),
436 DEFINE_PROP_UINT32("yres", BCM2835FBState
, initial_config
.yres
, 480),
437 DEFINE_PROP_UINT32("bpp", BCM2835FBState
, initial_config
.bpp
, 16),
438 DEFINE_PROP_UINT32("pixo", BCM2835FBState
,
439 initial_config
.pixo
, 1), /* 1=RGB, 0=BGR */
440 DEFINE_PROP_UINT32("alpha", BCM2835FBState
,
441 initial_config
.alpha
, 2), /* alpha ignored */
442 DEFINE_PROP_END_OF_LIST()
445 static void bcm2835_fb_class_init(ObjectClass
*klass
, void *data
)
447 DeviceClass
*dc
= DEVICE_CLASS(klass
);
449 dc
->props
= bcm2835_fb_props
;
450 dc
->realize
= bcm2835_fb_realize
;
451 dc
->reset
= bcm2835_fb_reset
;
452 dc
->vmsd
= &vmstate_bcm2835_fb
;
455 static TypeInfo bcm2835_fb_info
= {
456 .name
= TYPE_BCM2835_FB
,
457 .parent
= TYPE_SYS_BUS_DEVICE
,
458 .instance_size
= sizeof(BCM2835FBState
),
459 .class_init
= bcm2835_fb_class_init
,
460 .instance_init
= bcm2835_fb_init
,
463 static void bcm2835_fb_register_types(void)
465 type_register_static(&bcm2835_fb_info
);
468 type_init(bcm2835_fb_register_types
)