xref: /openbmc/qemu/hw/display/bcm2835_fb.c (revision 438c78da)
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 
34 #define DEFAULT_VCRAM_SIZE 0x4000000
35 #define BCM2835_FB_OFFSET  0x00100000
36 
37 /* Maximum permitted framebuffer size; experimentally determined on an rpi2 */
38 #define XRES_MAX 3840
39 #define YRES_MAX 2560
40 /* Framebuffer size used if guest requests zero size */
41 #define XRES_SMALL 592
42 #define YRES_SMALL 488
43 
44 static void fb_invalidate_display(void *opaque)
45 {
46     BCM2835FBState *s = BCM2835_FB(opaque);
47 
48     s->invalidate = true;
49 }
50 
51 static void draw_line_src16(void *opaque, uint8_t *dst, const uint8_t *src,
52                             int width, int deststep)
53 {
54     BCM2835FBState *s = opaque;
55     uint16_t rgb565;
56     uint32_t rgb888;
57     uint8_t r, g, b;
58     DisplaySurface *surface = qemu_console_surface(s->con);
59     int bpp = surface_bits_per_pixel(surface);
60 
61     while (width--) {
62         switch (s->config.bpp) {
63         case 8:
64             /* lookup palette starting at video ram base
65              * TODO: cache translation, rather than doing this each time!
66              */
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;
71             src++;
72             break;
73         case 16:
74             rgb565 = lduw_le_p(src);
75             r = ((rgb565 >> 11) & 0x1f) << 3;
76             g = ((rgb565 >>  5) & 0x3f) << 2;
77             b = ((rgb565 >>  0) & 0x1f) << 3;
78             src += 2;
79             break;
80         case 24:
81             rgb888 = ldl_le_p(src);
82             r = (rgb888 >> 0) & 0xff;
83             g = (rgb888 >> 8) & 0xff;
84             b = (rgb888 >> 16) & 0xff;
85             src += 3;
86             break;
87         case 32:
88             rgb888 = ldl_le_p(src);
89             r = (rgb888 >> 0) & 0xff;
90             g = (rgb888 >> 8) & 0xff;
91             b = (rgb888 >> 16) & 0xff;
92             src += 4;
93             break;
94         default:
95             r = 0;
96             g = 0;
97             b = 0;
98             break;
99         }
100 
101         if (s->config.pixo == 0) {
102             /* swap to BGR pixel format */
103             uint8_t tmp = r;
104             r = b;
105             b = tmp;
106         }
107 
108         switch (bpp) {
109         case 8:
110             *dst++ = rgb_to_pixel8(r, g, b);
111             break;
112         case 15:
113             *(uint16_t *)dst = rgb_to_pixel15(r, g, b);
114             dst += 2;
115             break;
116         case 16:
117             *(uint16_t *)dst = rgb_to_pixel16(r, g, b);
118             dst += 2;
119             break;
120         case 24:
121             rgb888 = rgb_to_pixel24(r, g, b);
122             *dst++ = rgb888 & 0xff;
123             *dst++ = (rgb888 >> 8) & 0xff;
124             *dst++ = (rgb888 >> 16) & 0xff;
125             break;
126         case 32:
127             *(uint32_t *)dst = rgb_to_pixel32(r, g, b);
128             dst += 4;
129             break;
130         default:
131             return;
132         }
133     }
134 }
135 
136 static bool fb_use_offsets(BCM2835FBConfig *config)
137 {
138     /*
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...)
143      */
144     return config->xres_virtual > config->xres &&
145         config->yres_virtual > config->yres;
146 }
147 
148 static void fb_update_display(void *opaque)
149 {
150     BCM2835FBState *s = opaque;
151     DisplaySurface *surface = qemu_console_surface(s->con);
152     int first = 0;
153     int last = 0;
154     int src_width = 0;
155     int dest_width = 0;
156     uint32_t xoff = 0, yoff = 0;
157 
158     if (s->lock || !s->config.xres) {
159         return;
160     }
161 
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;
166     }
167 
168     dest_width = s->config.xres;
169 
170     switch (surface_bits_per_pixel(surface)) {
171     case 0:
172         return;
173     case 8:
174         break;
175     case 15:
176         dest_width *= 2;
177         break;
178     case 16:
179         dest_width *= 2;
180         break;
181     case 24:
182         dest_width *= 3;
183         break;
184     case 32:
185         dest_width *= 4;
186         break;
187     default:
188         hw_error("bcm2835_fb: bad color depth\n");
189         break;
190     }
191 
192     if (s->invalidate) {
193         hwaddr base = s->config.base + xoff + (hwaddr)yoff * src_width;
194         framebuffer_update_memory_section(&s->fbsection, s->dma_mr,
195                                           base,
196                                           s->config.yres, src_width);
197     }
198 
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);
203 
204     if (first >= 0) {
205         dpy_gfx_update(s->con, 0, first, s->config.xres,
206                        last - first + 1);
207     }
208 
209     s->invalidate = false;
210 }
211 
212 void bcm2835_fb_validate_config(BCM2835FBConfig *config)
213 {
214     /*
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.
219      */
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);
224 
225     /*
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.
228      */
229     if (config->xres == 0) {
230         config->xres = XRES_SMALL;
231     }
232     if (config->yres == 0) {
233         config->yres = YRES_SMALL;
234     }
235     if (config->xres_virtual == 0) {
236         config->xres_virtual = config->xres;
237     }
238     if (config->yres_virtual == 0) {
239         config->yres_virtual = config->yres;
240     }
241 
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);
248     }
249 }
250 
251 void bcm2835_fb_reconfigure(BCM2835FBState *s, BCM2835FBConfig *newconfig)
252 {
253     s->lock = true;
254 
255     s->config = *newconfig;
256 
257     s->invalidate = true;
258     qemu_console_resize(s->con, s->config.xres, s->config.yres);
259     s->lock = false;
260 }
261 
262 static void bcm2835_fb_mbox_push(BCM2835FBState *s, uint32_t value)
263 {
264     uint32_t pitch;
265     uint32_t size;
266     BCM2835FBConfig newconf;
267 
268     value &= ~0xf;
269 
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);
277 
278     newconf.base = s->vcram_base | (value & 0xc0000000);
279     newconf.base += BCM2835_FB_OFFSET;
280 
281     bcm2835_fb_validate_config(&newconf);
282 
283     pitch = bcm2835_fb_get_pitch(&newconf);
284     size = bcm2835_fb_get_size(&newconf);
285 
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);
289 
290     bcm2835_fb_reconfigure(s, &newconf);
291 }
292 
293 static uint64_t bcm2835_fb_read(void *opaque, hwaddr offset, unsigned size)
294 {
295     BCM2835FBState *s = opaque;
296     uint32_t res = 0;
297 
298     switch (offset) {
299     case MBOX_AS_DATA:
300         res = MBOX_CHAN_FB;
301         s->pending = false;
302         qemu_set_irq(s->mbox_irq, 0);
303         break;
304 
305     case MBOX_AS_PENDING:
306         res = s->pending;
307         break;
308 
309     default:
310         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
311                       __func__, offset);
312         return 0;
313     }
314 
315     return res;
316 }
317 
318 static void bcm2835_fb_write(void *opaque, hwaddr offset, uint64_t value,
319                              unsigned size)
320 {
321     BCM2835FBState *s = opaque;
322 
323     switch (offset) {
324     case MBOX_AS_DATA:
325         /* bcm2835_mbox should check our pending status before pushing */
326         assert(!s->pending);
327         s->pending = true;
328         bcm2835_fb_mbox_push(s, value);
329         qemu_set_irq(s->mbox_irq, 1);
330         break;
331 
332     default:
333         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
334                       __func__, offset);
335         return;
336     }
337 }
338 
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,
345 };
346 
347 static const VMStateDescription vmstate_bcm2835_fb = {
348     .name = TYPE_BCM2835_FB,
349     .version_id = 1,
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()
367     }
368 };
369 
370 static const GraphicHwOps vgafb_ops = {
371     .invalidate  = fb_invalidate_display,
372     .gfx_update  = fb_update_display,
373 };
374 
375 static void bcm2835_fb_init(Object *obj)
376 {
377     BCM2835FBState *s = BCM2835_FB(obj);
378 
379     memory_region_init_io(&s->iomem, obj, &bcm2835_fb_ops, s, TYPE_BCM2835_FB,
380                           0x10);
381     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
382     sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
383 }
384 
385 static void bcm2835_fb_reset(DeviceState *dev)
386 {
387     BCM2835FBState *s = BCM2835_FB(dev);
388 
389     s->pending = false;
390 
391     s->config = s->initial_config;
392 
393     s->invalidate = true;
394     s->lock = false;
395 }
396 
397 static void bcm2835_fb_realize(DeviceState *dev, Error **errp)
398 {
399     BCM2835FBState *s = BCM2835_FB(dev);
400     Error *err = NULL;
401     Object *obj;
402 
403     if (s->vcram_base == 0) {
404         error_setg(errp, "%s: required vcram-base property not set", __func__);
405         return;
406     }
407 
408     obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
409     if (obj == NULL) {
410         error_setg(errp, "%s: required dma-mr link not found: %s",
411                    __func__, error_get_pretty(err));
412         return;
413     }
414 
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;
421 
422     s->dma_mr = MEMORY_REGION(obj);
423     address_space_init(&s->dma_as, s->dma_mr, NULL);
424 
425     bcm2835_fb_reset(dev);
426 
427     s->con = graphic_console_init(dev, 0, &vgafb_ops, s);
428     qemu_console_resize(s->con, s->config.xres, s->config.yres);
429 }
430 
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,
434                        DEFAULT_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()
443 };
444 
445 static void bcm2835_fb_class_init(ObjectClass *klass, void *data)
446 {
447     DeviceClass *dc = DEVICE_CLASS(klass);
448 
449     dc->props = bcm2835_fb_props;
450     dc->realize = bcm2835_fb_realize;
451     dc->reset = bcm2835_fb_reset;
452     dc->vmsd = &vmstate_bcm2835_fb;
453 }
454 
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,
461 };
462 
463 static void bcm2835_fb_register_types(void)
464 {
465     type_register_static(&bcm2835_fb_info);
466 }
467 
468 type_init(bcm2835_fb_register_types)
469