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