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