xref: /openbmc/qemu/hw/display/bcm2835_fb.c (revision b917da4c)
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 "hw/display/bcm2835_fb.h"
28 #include "hw/display/framebuffer.h"
29 #include "ui/pixel_ops.h"
30 #include "hw/misc/bcm2835_mbox_defs.h"
31 
32 #define DEFAULT_VCRAM_SIZE 0x4000000
33 #define BCM2835_FB_OFFSET  0x00100000
34 
35 static void fb_invalidate_display(void *opaque)
36 {
37     BCM2835FBState *s = BCM2835_FB(opaque);
38 
39     s->invalidate = true;
40 }
41 
42 static void draw_line_src16(void *opaque, uint8_t *dst, const uint8_t *src,
43                             int width, int deststep)
44 {
45     BCM2835FBState *s = opaque;
46     uint16_t rgb565;
47     uint32_t rgb888;
48     uint8_t r, g, b;
49     DisplaySurface *surface = qemu_console_surface(s->con);
50     int bpp = surface_bits_per_pixel(surface);
51 
52     while (width--) {
53         switch (s->bpp) {
54         case 8:
55             /* lookup palette starting at video ram base
56              * TODO: cache translation, rather than doing this each time!
57              */
58             rgb888 = ldl_le_phys(&s->dma_as, s->vcram_base + (*src << 2));
59             r = (rgb888 >> 0) & 0xff;
60             g = (rgb888 >> 8) & 0xff;
61             b = (rgb888 >> 16) & 0xff;
62             src++;
63             break;
64         case 16:
65             rgb565 = lduw_le_p(src);
66             r = ((rgb565 >> 11) & 0x1f) << 3;
67             g = ((rgb565 >>  5) & 0x3f) << 2;
68             b = ((rgb565 >>  0) & 0x1f) << 3;
69             src += 2;
70             break;
71         case 24:
72             rgb888 = ldl_le_p(src);
73             r = (rgb888 >> 0) & 0xff;
74             g = (rgb888 >> 8) & 0xff;
75             b = (rgb888 >> 16) & 0xff;
76             src += 3;
77             break;
78         case 32:
79             rgb888 = ldl_le_p(src);
80             r = (rgb888 >> 0) & 0xff;
81             g = (rgb888 >> 8) & 0xff;
82             b = (rgb888 >> 16) & 0xff;
83             src += 4;
84             break;
85         default:
86             r = 0;
87             g = 0;
88             b = 0;
89             break;
90         }
91 
92         if (s->pixo == 0) {
93             /* swap to BGR pixel format */
94             uint8_t tmp = r;
95             r = b;
96             b = tmp;
97         }
98 
99         switch (bpp) {
100         case 8:
101             *dst++ = rgb_to_pixel8(r, g, b);
102             break;
103         case 15:
104             *(uint16_t *)dst = rgb_to_pixel15(r, g, b);
105             dst += 2;
106             break;
107         case 16:
108             *(uint16_t *)dst = rgb_to_pixel16(r, g, b);
109             dst += 2;
110             break;
111         case 24:
112             rgb888 = rgb_to_pixel24(r, g, b);
113             *dst++ = rgb888 & 0xff;
114             *dst++ = (rgb888 >> 8) & 0xff;
115             *dst++ = (rgb888 >> 16) & 0xff;
116             break;
117         case 32:
118             *(uint32_t *)dst = rgb_to_pixel32(r, g, b);
119             dst += 4;
120             break;
121         default:
122             return;
123         }
124     }
125 }
126 
127 static void fb_update_display(void *opaque)
128 {
129     BCM2835FBState *s = opaque;
130     DisplaySurface *surface = qemu_console_surface(s->con);
131     int first = 0;
132     int last = 0;
133     int src_width = 0;
134     int dest_width = 0;
135 
136     if (s->lock || !s->xres) {
137         return;
138     }
139 
140     src_width = s->xres * (s->bpp >> 3);
141     dest_width = s->xres;
142 
143     switch (surface_bits_per_pixel(surface)) {
144     case 0:
145         return;
146     case 8:
147         break;
148     case 15:
149         dest_width *= 2;
150         break;
151     case 16:
152         dest_width *= 2;
153         break;
154     case 24:
155         dest_width *= 3;
156         break;
157     case 32:
158         dest_width *= 4;
159         break;
160     default:
161         hw_error("bcm2835_fb: bad color depth\n");
162         break;
163     }
164 
165     if (s->invalidate) {
166         framebuffer_update_memory_section(&s->fbsection, s->dma_mr, s->base,
167                                           s->yres, src_width);
168     }
169 
170     framebuffer_update_display(surface, &s->fbsection, s->xres, s->yres,
171                                src_width, dest_width, 0, s->invalidate,
172                                draw_line_src16, s, &first, &last);
173 
174     if (first >= 0) {
175         dpy_gfx_update(s->con, 0, first, s->xres, last - first + 1);
176     }
177 
178     s->invalidate = false;
179 }
180 
181 static void bcm2835_fb_mbox_push(BCM2835FBState *s, uint32_t value)
182 {
183     value &= ~0xf;
184 
185     s->lock = true;
186 
187     s->xres = ldl_le_phys(&s->dma_as, value);
188     s->yres = ldl_le_phys(&s->dma_as, value + 4);
189     s->xres_virtual = ldl_le_phys(&s->dma_as, value + 8);
190     s->yres_virtual = ldl_le_phys(&s->dma_as, value + 12);
191     s->bpp = ldl_le_phys(&s->dma_as, value + 20);
192     s->xoffset = ldl_le_phys(&s->dma_as, value + 24);
193     s->yoffset = ldl_le_phys(&s->dma_as, value + 28);
194 
195     s->base = s->vcram_base | (value & 0xc0000000);
196     s->base += BCM2835_FB_OFFSET;
197 
198     /* TODO - Manage properly virtual resolution */
199 
200     s->pitch = s->xres * (s->bpp >> 3);
201     s->size = s->yres * s->pitch;
202 
203     stl_le_phys(&s->dma_as, value + 16, s->pitch);
204     stl_le_phys(&s->dma_as, value + 32, s->base);
205     stl_le_phys(&s->dma_as, value + 36, s->size);
206 
207     s->invalidate = true;
208     qemu_console_resize(s->con, s->xres, s->yres);
209     s->lock = false;
210 }
211 
212 void bcm2835_fb_reconfigure(BCM2835FBState *s, uint32_t *xres, uint32_t *yres,
213                             uint32_t *xoffset, uint32_t *yoffset, uint32_t *bpp,
214                             uint32_t *pixo, uint32_t *alpha)
215 {
216     s->lock = true;
217 
218     /* TODO: input validation! */
219     if (xres) {
220         s->xres = *xres;
221     }
222     if (yres) {
223         s->yres = *yres;
224     }
225     if (xoffset) {
226         s->xoffset = *xoffset;
227     }
228     if (yoffset) {
229         s->yoffset = *yoffset;
230     }
231     if (bpp) {
232         s->bpp = *bpp;
233     }
234     if (pixo) {
235         s->pixo = *pixo;
236     }
237     if (alpha) {
238         s->alpha = *alpha;
239     }
240 
241     /* TODO - Manage properly virtual resolution */
242 
243     s->pitch = s->xres * (s->bpp >> 3);
244     s->size = s->yres * s->pitch;
245 
246     s->invalidate = true;
247     qemu_console_resize(s->con, s->xres, s->yres);
248     s->lock = false;
249 }
250 
251 static uint64_t bcm2835_fb_read(void *opaque, hwaddr offset, unsigned size)
252 {
253     BCM2835FBState *s = opaque;
254     uint32_t res = 0;
255 
256     switch (offset) {
257     case MBOX_AS_DATA:
258         res = MBOX_CHAN_FB;
259         s->pending = false;
260         qemu_set_irq(s->mbox_irq, 0);
261         break;
262 
263     case MBOX_AS_PENDING:
264         res = s->pending;
265         break;
266 
267     default:
268         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
269                       __func__, offset);
270         return 0;
271     }
272 
273     return res;
274 }
275 
276 static void bcm2835_fb_write(void *opaque, hwaddr offset, uint64_t value,
277                              unsigned size)
278 {
279     BCM2835FBState *s = opaque;
280 
281     switch (offset) {
282     case MBOX_AS_DATA:
283         /* bcm2835_mbox should check our pending status before pushing */
284         assert(!s->pending);
285         s->pending = true;
286         bcm2835_fb_mbox_push(s, value);
287         qemu_set_irq(s->mbox_irq, 1);
288         break;
289 
290     default:
291         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
292                       __func__, offset);
293         return;
294     }
295 }
296 
297 static const MemoryRegionOps bcm2835_fb_ops = {
298     .read = bcm2835_fb_read,
299     .write = bcm2835_fb_write,
300     .endianness = DEVICE_NATIVE_ENDIAN,
301     .valid.min_access_size = 4,
302     .valid.max_access_size = 4,
303 };
304 
305 static const VMStateDescription vmstate_bcm2835_fb = {
306     .name = TYPE_BCM2835_FB,
307     .version_id = 1,
308     .minimum_version_id = 1,
309     .fields = (VMStateField[]) {
310         VMSTATE_BOOL(lock, BCM2835FBState),
311         VMSTATE_BOOL(invalidate, BCM2835FBState),
312         VMSTATE_BOOL(pending, BCM2835FBState),
313         VMSTATE_UINT32(xres, BCM2835FBState),
314         VMSTATE_UINT32(yres, BCM2835FBState),
315         VMSTATE_UINT32(xres_virtual, BCM2835FBState),
316         VMSTATE_UINT32(yres_virtual, BCM2835FBState),
317         VMSTATE_UINT32(xoffset, BCM2835FBState),
318         VMSTATE_UINT32(yoffset, BCM2835FBState),
319         VMSTATE_UINT32(bpp, BCM2835FBState),
320         VMSTATE_UINT32(base, BCM2835FBState),
321         VMSTATE_UINT32(pitch, BCM2835FBState),
322         VMSTATE_UINT32(size, BCM2835FBState),
323         VMSTATE_UINT32(pixo, BCM2835FBState),
324         VMSTATE_UINT32(alpha, BCM2835FBState),
325         VMSTATE_END_OF_LIST()
326     }
327 };
328 
329 static const GraphicHwOps vgafb_ops = {
330     .invalidate  = fb_invalidate_display,
331     .gfx_update  = fb_update_display,
332 };
333 
334 static void bcm2835_fb_init(Object *obj)
335 {
336     BCM2835FBState *s = BCM2835_FB(obj);
337 
338     memory_region_init_io(&s->iomem, obj, &bcm2835_fb_ops, s, TYPE_BCM2835_FB,
339                           0x10);
340     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
341     sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
342 }
343 
344 static void bcm2835_fb_reset(DeviceState *dev)
345 {
346     BCM2835FBState *s = BCM2835_FB(dev);
347 
348     s->pending = false;
349 
350     s->xres_virtual = s->xres;
351     s->yres_virtual = s->yres;
352     s->xoffset = 0;
353     s->yoffset = 0;
354     s->base = s->vcram_base + BCM2835_FB_OFFSET;
355     s->pitch = s->xres * (s->bpp >> 3);
356     s->size = s->yres * s->pitch;
357 
358     s->invalidate = true;
359     s->lock = false;
360 }
361 
362 static void bcm2835_fb_realize(DeviceState *dev, Error **errp)
363 {
364     BCM2835FBState *s = BCM2835_FB(dev);
365     Error *err = NULL;
366     Object *obj;
367 
368     if (s->vcram_base == 0) {
369         error_setg(errp, "%s: required vcram-base property not set", __func__);
370         return;
371     }
372 
373     obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
374     if (obj == NULL) {
375         error_setg(errp, "%s: required dma-mr link not found: %s",
376                    __func__, error_get_pretty(err));
377         return;
378     }
379 
380     s->dma_mr = MEMORY_REGION(obj);
381     address_space_init(&s->dma_as, s->dma_mr, NULL);
382 
383     bcm2835_fb_reset(dev);
384 
385     s->con = graphic_console_init(dev, 0, &vgafb_ops, s);
386     qemu_console_resize(s->con, s->xres, s->yres);
387 }
388 
389 static Property bcm2835_fb_props[] = {
390     DEFINE_PROP_UINT32("vcram-base", BCM2835FBState, vcram_base, 0),/*required*/
391     DEFINE_PROP_UINT32("vcram-size", BCM2835FBState, vcram_size,
392                        DEFAULT_VCRAM_SIZE),
393     DEFINE_PROP_UINT32("xres", BCM2835FBState, xres, 640),
394     DEFINE_PROP_UINT32("yres", BCM2835FBState, yres, 480),
395     DEFINE_PROP_UINT32("bpp", BCM2835FBState, bpp, 16),
396     DEFINE_PROP_UINT32("pixo", BCM2835FBState, pixo, 1), /* 1=RGB, 0=BGR */
397     DEFINE_PROP_UINT32("alpha", BCM2835FBState, alpha, 2), /* alpha ignored */
398     DEFINE_PROP_END_OF_LIST()
399 };
400 
401 static void bcm2835_fb_class_init(ObjectClass *klass, void *data)
402 {
403     DeviceClass *dc = DEVICE_CLASS(klass);
404 
405     dc->props = bcm2835_fb_props;
406     dc->realize = bcm2835_fb_realize;
407     dc->reset = bcm2835_fb_reset;
408     dc->vmsd = &vmstate_bcm2835_fb;
409 }
410 
411 static TypeInfo bcm2835_fb_info = {
412     .name          = TYPE_BCM2835_FB,
413     .parent        = TYPE_SYS_BUS_DEVICE,
414     .instance_size = sizeof(BCM2835FBState),
415     .class_init    = bcm2835_fb_class_init,
416     .instance_init = bcm2835_fb_init,
417 };
418 
419 static void bcm2835_fb_register_types(void)
420 {
421     type_register_static(&bcm2835_fb_info);
422 }
423 
424 type_init(bcm2835_fb_register_types)
425