xref: /openbmc/qemu/hw/display/bcm2835_fb.c (revision 500eb6db)
1 /*
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.
5  *
6  * Heavily based on milkymist-vgafb.c, copyright terms below:
7  *  QEMU model of the Milkymist VGA framebuffer.
8  *
9  *  Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
10  *
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.
15  *
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.
20  *
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/>.
23  *
24  */
25 
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"
32 #include "qemu/log.h"
33 #include "qemu/module.h"
34 
35 #define DEFAULT_VCRAM_SIZE 0x4000000
36 #define BCM2835_FB_OFFSET  0x00100000
37 
38 /* Maximum permitted framebuffer size; experimentally determined on an rpi2 */
39 #define XRES_MAX 3840
40 #define YRES_MAX 2560
41 /* Framebuffer size used if guest requests zero size */
42 #define XRES_SMALL 592
43 #define YRES_SMALL 488
44 
45 static void fb_invalidate_display(void *opaque)
46 {
47     BCM2835FBState *s = BCM2835_FB(opaque);
48 
49     s->invalidate = true;
50 }
51 
52 static void draw_line_src16(void *opaque, uint8_t *dst, const uint8_t *src,
53                             int width, int deststep)
54 {
55     BCM2835FBState *s = opaque;
56     uint16_t rgb565;
57     uint32_t rgb888;
58     uint8_t r, g, b;
59     DisplaySurface *surface = qemu_console_surface(s->con);
60     int bpp = surface_bits_per_pixel(surface);
61 
62     while (width--) {
63         switch (s->config.bpp) {
64         case 8:
65             /* lookup palette starting at video ram base
66              * TODO: cache translation, rather than doing this each time!
67              */
68             rgb888 = ldl_le_phys(&s->dma_as, s->vcram_base + (*src << 2));
69             r = (rgb888 >> 0) & 0xff;
70             g = (rgb888 >> 8) & 0xff;
71             b = (rgb888 >> 16) & 0xff;
72             src++;
73             break;
74         case 16:
75             rgb565 = lduw_le_p(src);
76             r = ((rgb565 >> 11) & 0x1f) << 3;
77             g = ((rgb565 >>  5) & 0x3f) << 2;
78             b = ((rgb565 >>  0) & 0x1f) << 3;
79             src += 2;
80             break;
81         case 24:
82             rgb888 = ldl_le_p(src);
83             r = (rgb888 >> 0) & 0xff;
84             g = (rgb888 >> 8) & 0xff;
85             b = (rgb888 >> 16) & 0xff;
86             src += 3;
87             break;
88         case 32:
89             rgb888 = ldl_le_p(src);
90             r = (rgb888 >> 0) & 0xff;
91             g = (rgb888 >> 8) & 0xff;
92             b = (rgb888 >> 16) & 0xff;
93             src += 4;
94             break;
95         default:
96             r = 0;
97             g = 0;
98             b = 0;
99             break;
100         }
101 
102         if (s->config.pixo == 0) {
103             /* swap to BGR pixel format */
104             uint8_t tmp = r;
105             r = b;
106             b = tmp;
107         }
108 
109         switch (bpp) {
110         case 8:
111             *dst++ = rgb_to_pixel8(r, g, b);
112             break;
113         case 15:
114             *(uint16_t *)dst = rgb_to_pixel15(r, g, b);
115             dst += 2;
116             break;
117         case 16:
118             *(uint16_t *)dst = rgb_to_pixel16(r, g, b);
119             dst += 2;
120             break;
121         case 24:
122             rgb888 = rgb_to_pixel24(r, g, b);
123             *dst++ = rgb888 & 0xff;
124             *dst++ = (rgb888 >> 8) & 0xff;
125             *dst++ = (rgb888 >> 16) & 0xff;
126             break;
127         case 32:
128             *(uint32_t *)dst = rgb_to_pixel32(r, g, b);
129             dst += 4;
130             break;
131         default:
132             return;
133         }
134     }
135 }
136 
137 static bool fb_use_offsets(BCM2835FBConfig *config)
138 {
139     /*
140      * Return true if we should use the viewport offsets.
141      * Experimentally, the hardware seems to do this only if the
142      * viewport size is larger than the physical screen. (It doesn't
143      * prevent the guest setting this silly viewport setting, though...)
144      */
145     return config->xres_virtual > config->xres &&
146         config->yres_virtual > config->yres;
147 }
148 
149 static void fb_update_display(void *opaque)
150 {
151     BCM2835FBState *s = opaque;
152     DisplaySurface *surface = qemu_console_surface(s->con);
153     int first = 0;
154     int last = 0;
155     int src_width = 0;
156     int dest_width = 0;
157     uint32_t xoff = 0, yoff = 0;
158 
159     if (s->lock || !s->config.xres) {
160         return;
161     }
162 
163     src_width = bcm2835_fb_get_pitch(&s->config);
164     if (fb_use_offsets(&s->config)) {
165         xoff = s->config.xoffset;
166         yoff = s->config.yoffset;
167     }
168 
169     dest_width = s->config.xres;
170 
171     switch (surface_bits_per_pixel(surface)) {
172     case 0:
173         return;
174     case 8:
175         break;
176     case 15:
177         dest_width *= 2;
178         break;
179     case 16:
180         dest_width *= 2;
181         break;
182     case 24:
183         dest_width *= 3;
184         break;
185     case 32:
186         dest_width *= 4;
187         break;
188     default:
189         hw_error("bcm2835_fb: bad color depth\n");
190         break;
191     }
192 
193     if (s->invalidate) {
194         hwaddr base = s->config.base + xoff + (hwaddr)yoff * src_width;
195         framebuffer_update_memory_section(&s->fbsection, s->dma_mr,
196                                           base,
197                                           s->config.yres, src_width);
198     }
199 
200     framebuffer_update_display(surface, &s->fbsection,
201                                s->config.xres, s->config.yres,
202                                src_width, dest_width, 0, s->invalidate,
203                                draw_line_src16, s, &first, &last);
204 
205     if (first >= 0) {
206         dpy_gfx_update(s->con, 0, first, s->config.xres,
207                        last - first + 1);
208     }
209 
210     s->invalidate = false;
211 }
212 
213 void bcm2835_fb_validate_config(BCM2835FBConfig *config)
214 {
215     /*
216      * Validate the config, and clip any bogus values into range,
217      * as the hardware does. Note that fb_update_display() relies on
218      * this happening to prevent it from performing out-of-range
219      * accesses on redraw.
220      */
221     config->xres = MIN(config->xres, XRES_MAX);
222     config->xres_virtual = MIN(config->xres_virtual, XRES_MAX);
223     config->yres = MIN(config->yres, YRES_MAX);
224     config->yres_virtual = MIN(config->yres_virtual, YRES_MAX);
225 
226     /*
227      * These are not minima: a 40x40 framebuffer will be accepted.
228      * They're only used as defaults if the guest asks for zero size.
229      */
230     if (config->xres == 0) {
231         config->xres = XRES_SMALL;
232     }
233     if (config->yres == 0) {
234         config->yres = YRES_SMALL;
235     }
236     if (config->xres_virtual == 0) {
237         config->xres_virtual = config->xres;
238     }
239     if (config->yres_virtual == 0) {
240         config->yres_virtual = config->yres;
241     }
242 
243     if (fb_use_offsets(config)) {
244         /* Clip the offsets so the viewport is within the physical screen */
245         config->xoffset = MIN(config->xoffset,
246                               config->xres_virtual - config->xres);
247         config->yoffset = MIN(config->yoffset,
248                               config->yres_virtual - config->yres);
249     }
250 }
251 
252 void bcm2835_fb_reconfigure(BCM2835FBState *s, BCM2835FBConfig *newconfig)
253 {
254     s->lock = true;
255 
256     s->config = *newconfig;
257 
258     s->invalidate = true;
259     qemu_console_resize(s->con, s->config.xres, s->config.yres);
260     s->lock = false;
261 }
262 
263 static void bcm2835_fb_mbox_push(BCM2835FBState *s, uint32_t value)
264 {
265     uint32_t pitch;
266     uint32_t size;
267     BCM2835FBConfig newconf;
268 
269     value &= ~0xf;
270 
271     newconf.xres = ldl_le_phys(&s->dma_as, value);
272     newconf.yres = ldl_le_phys(&s->dma_as, value + 4);
273     newconf.xres_virtual = ldl_le_phys(&s->dma_as, value + 8);
274     newconf.yres_virtual = ldl_le_phys(&s->dma_as, value + 12);
275     newconf.bpp = ldl_le_phys(&s->dma_as, value + 20);
276     newconf.xoffset = ldl_le_phys(&s->dma_as, value + 24);
277     newconf.yoffset = ldl_le_phys(&s->dma_as, value + 28);
278 
279     newconf.base = s->vcram_base | (value & 0xc0000000);
280     newconf.base += BCM2835_FB_OFFSET;
281 
282     bcm2835_fb_validate_config(&newconf);
283 
284     pitch = bcm2835_fb_get_pitch(&newconf);
285     size = bcm2835_fb_get_size(&newconf);
286 
287     stl_le_phys(&s->dma_as, value + 16, pitch);
288     stl_le_phys(&s->dma_as, value + 32, newconf.base);
289     stl_le_phys(&s->dma_as, value + 36, size);
290 
291     bcm2835_fb_reconfigure(s, &newconf);
292 }
293 
294 static uint64_t bcm2835_fb_read(void *opaque, hwaddr offset, unsigned size)
295 {
296     BCM2835FBState *s = opaque;
297     uint32_t res = 0;
298 
299     switch (offset) {
300     case MBOX_AS_DATA:
301         res = MBOX_CHAN_FB;
302         s->pending = false;
303         qemu_set_irq(s->mbox_irq, 0);
304         break;
305 
306     case MBOX_AS_PENDING:
307         res = s->pending;
308         break;
309 
310     default:
311         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
312                       __func__, offset);
313         return 0;
314     }
315 
316     return res;
317 }
318 
319 static void bcm2835_fb_write(void *opaque, hwaddr offset, uint64_t value,
320                              unsigned size)
321 {
322     BCM2835FBState *s = opaque;
323 
324     switch (offset) {
325     case MBOX_AS_DATA:
326         /* bcm2835_mbox should check our pending status before pushing */
327         assert(!s->pending);
328         s->pending = true;
329         bcm2835_fb_mbox_push(s, value);
330         qemu_set_irq(s->mbox_irq, 1);
331         break;
332 
333     default:
334         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
335                       __func__, offset);
336         return;
337     }
338 }
339 
340 static const MemoryRegionOps bcm2835_fb_ops = {
341     .read = bcm2835_fb_read,
342     .write = bcm2835_fb_write,
343     .endianness = DEVICE_NATIVE_ENDIAN,
344     .valid.min_access_size = 4,
345     .valid.max_access_size = 4,
346 };
347 
348 static const VMStateDescription vmstate_bcm2835_fb = {
349     .name = TYPE_BCM2835_FB,
350     .version_id = 1,
351     .minimum_version_id = 1,
352     .fields = (VMStateField[]) {
353         VMSTATE_BOOL(lock, BCM2835FBState),
354         VMSTATE_BOOL(invalidate, BCM2835FBState),
355         VMSTATE_BOOL(pending, BCM2835FBState),
356         VMSTATE_UINT32(config.xres, BCM2835FBState),
357         VMSTATE_UINT32(config.yres, BCM2835FBState),
358         VMSTATE_UINT32(config.xres_virtual, BCM2835FBState),
359         VMSTATE_UINT32(config.yres_virtual, BCM2835FBState),
360         VMSTATE_UINT32(config.xoffset, BCM2835FBState),
361         VMSTATE_UINT32(config.yoffset, BCM2835FBState),
362         VMSTATE_UINT32(config.bpp, BCM2835FBState),
363         VMSTATE_UINT32(config.base, BCM2835FBState),
364         VMSTATE_UNUSED(8), /* Was pitch and size */
365         VMSTATE_UINT32(config.pixo, BCM2835FBState),
366         VMSTATE_UINT32(config.alpha, BCM2835FBState),
367         VMSTATE_END_OF_LIST()
368     }
369 };
370 
371 static const GraphicHwOps vgafb_ops = {
372     .invalidate  = fb_invalidate_display,
373     .gfx_update  = fb_update_display,
374 };
375 
376 static void bcm2835_fb_init(Object *obj)
377 {
378     BCM2835FBState *s = BCM2835_FB(obj);
379 
380     memory_region_init_io(&s->iomem, obj, &bcm2835_fb_ops, s, TYPE_BCM2835_FB,
381                           0x10);
382     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
383     sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
384 }
385 
386 static void bcm2835_fb_reset(DeviceState *dev)
387 {
388     BCM2835FBState *s = BCM2835_FB(dev);
389 
390     s->pending = false;
391 
392     s->config = s->initial_config;
393 
394     s->invalidate = true;
395     s->lock = false;
396 }
397 
398 static void bcm2835_fb_realize(DeviceState *dev, Error **errp)
399 {
400     BCM2835FBState *s = BCM2835_FB(dev);
401     Error *err = NULL;
402     Object *obj;
403 
404     if (s->vcram_base == 0) {
405         error_setg(errp, "%s: required vcram-base property not set", __func__);
406         return;
407     }
408 
409     obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
410     if (obj == NULL) {
411         error_setg(errp, "%s: required dma-mr link not found: %s",
412                    __func__, error_get_pretty(err));
413         return;
414     }
415 
416     /* Fill in the parts of initial_config that are not set by QOM properties */
417     s->initial_config.xres_virtual = s->initial_config.xres;
418     s->initial_config.yres_virtual = s->initial_config.yres;
419     s->initial_config.xoffset = 0;
420     s->initial_config.yoffset = 0;
421     s->initial_config.base = s->vcram_base + BCM2835_FB_OFFSET;
422 
423     s->dma_mr = MEMORY_REGION(obj);
424     address_space_init(&s->dma_as, s->dma_mr, NULL);
425 
426     bcm2835_fb_reset(dev);
427 
428     s->con = graphic_console_init(dev, 0, &vgafb_ops, s);
429     qemu_console_resize(s->con, s->config.xres, s->config.yres);
430 }
431 
432 static Property bcm2835_fb_props[] = {
433     DEFINE_PROP_UINT32("vcram-base", BCM2835FBState, vcram_base, 0),/*required*/
434     DEFINE_PROP_UINT32("vcram-size", BCM2835FBState, vcram_size,
435                        DEFAULT_VCRAM_SIZE),
436     DEFINE_PROP_UINT32("xres", BCM2835FBState, initial_config.xres, 640),
437     DEFINE_PROP_UINT32("yres", BCM2835FBState, initial_config.yres, 480),
438     DEFINE_PROP_UINT32("bpp", BCM2835FBState, initial_config.bpp, 16),
439     DEFINE_PROP_UINT32("pixo", BCM2835FBState,
440                        initial_config.pixo, 1), /* 1=RGB, 0=BGR */
441     DEFINE_PROP_UINT32("alpha", BCM2835FBState,
442                        initial_config.alpha, 2), /* alpha ignored */
443     DEFINE_PROP_END_OF_LIST()
444 };
445 
446 static void bcm2835_fb_class_init(ObjectClass *klass, void *data)
447 {
448     DeviceClass *dc = DEVICE_CLASS(klass);
449 
450     dc->props = bcm2835_fb_props;
451     dc->realize = bcm2835_fb_realize;
452     dc->reset = bcm2835_fb_reset;
453     dc->vmsd = &vmstate_bcm2835_fb;
454 }
455 
456 static TypeInfo bcm2835_fb_info = {
457     .name          = TYPE_BCM2835_FB,
458     .parent        = TYPE_SYS_BUS_DEVICE,
459     .instance_size = sizeof(BCM2835FBState),
460     .class_init    = bcm2835_fb_class_init,
461     .instance_init = bcm2835_fb_init,
462 };
463 
464 static void bcm2835_fb_register_types(void)
465 {
466     type_register_static(&bcm2835_fb_info);
467 }
468 
469 type_init(bcm2835_fb_register_types)
470