1 /* 2 * Raspberry Pi emulation (c) 2012 Gregory Estrade 3 * This code is licensed under the GNU GPLv2 and later. 4 */ 5 6 #include "qemu/osdep.h" 7 #include "qapi/error.h" 8 #include "hw/misc/bcm2835_property.h" 9 #include "hw/misc/bcm2835_mbox_defs.h" 10 #include "sysemu/dma.h" 11 12 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */ 13 14 static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value) 15 { 16 uint32_t tag; 17 uint32_t bufsize; 18 uint32_t tot_len; 19 size_t resplen; 20 uint32_t tmp; 21 int n; 22 uint32_t offset, length, color; 23 uint32_t xres, yres, xoffset, yoffset, bpp, pixo, alpha; 24 uint32_t tmp_xres, tmp_yres, tmp_xoffset, tmp_yoffset; 25 uint32_t tmp_bpp, tmp_pixo, tmp_alpha; 26 uint32_t *newxres = NULL, *newyres = NULL, *newxoffset = NULL, 27 *newyoffset = NULL, *newbpp = NULL, *newpixo = NULL, *newalpha = NULL; 28 29 value &= ~0xf; 30 31 s->addr = value; 32 33 tot_len = ldl_le_phys(&s->dma_as, value); 34 35 /* @(addr + 4) : Buffer response code */ 36 value = s->addr + 8; 37 while (value + 8 <= s->addr + tot_len) { 38 tag = ldl_le_phys(&s->dma_as, value); 39 bufsize = ldl_le_phys(&s->dma_as, value + 4); 40 /* @(value + 8) : Request/response indicator */ 41 resplen = 0; 42 switch (tag) { 43 case 0x00000000: /* End tag */ 44 break; 45 case 0x00000001: /* Get firmware revision */ 46 stl_le_phys(&s->dma_as, value + 12, 346337); 47 resplen = 4; 48 break; 49 case 0x00010001: /* Get board model */ 50 qemu_log_mask(LOG_UNIMP, 51 "bcm2835_property: %x get board model NYI\n", tag); 52 resplen = 4; 53 break; 54 case 0x00010002: /* Get board revision */ 55 stl_le_phys(&s->dma_as, value + 12, s->board_rev); 56 resplen = 4; 57 break; 58 case 0x00010003: /* Get board MAC address */ 59 resplen = sizeof(s->macaddr.a); 60 dma_memory_write(&s->dma_as, value + 12, s->macaddr.a, resplen); 61 break; 62 case 0x00010004: /* Get board serial */ 63 qemu_log_mask(LOG_UNIMP, 64 "bcm2835_property: %x get board serial NYI\n", tag); 65 resplen = 8; 66 break; 67 case 0x00010005: /* Get ARM memory */ 68 /* base */ 69 stl_le_phys(&s->dma_as, value + 12, 0); 70 /* size */ 71 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_base); 72 resplen = 8; 73 break; 74 case 0x00010006: /* Get VC memory */ 75 /* base */ 76 stl_le_phys(&s->dma_as, value + 12, s->fbdev->vcram_base); 77 /* size */ 78 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_size); 79 resplen = 8; 80 break; 81 case 0x00028001: /* Set power state */ 82 /* Assume that whatever device they asked for exists, 83 * and we'll just claim we set it to the desired state 84 */ 85 tmp = ldl_le_phys(&s->dma_as, value + 16); 86 stl_le_phys(&s->dma_as, value + 16, (tmp & 1)); 87 resplen = 8; 88 break; 89 90 /* Clocks */ 91 92 case 0x00030001: /* Get clock state */ 93 stl_le_phys(&s->dma_as, value + 16, 0x1); 94 resplen = 8; 95 break; 96 97 case 0x00038001: /* Set clock state */ 98 qemu_log_mask(LOG_UNIMP, 99 "bcm2835_property: %x set clock state NYI\n", tag); 100 resplen = 8; 101 break; 102 103 case 0x00030002: /* Get clock rate */ 104 case 0x00030004: /* Get max clock rate */ 105 case 0x00030007: /* Get min clock rate */ 106 switch (ldl_le_phys(&s->dma_as, value + 12)) { 107 case 1: /* EMMC */ 108 stl_le_phys(&s->dma_as, value + 16, 50000000); 109 break; 110 case 2: /* UART */ 111 stl_le_phys(&s->dma_as, value + 16, 3000000); 112 break; 113 default: 114 stl_le_phys(&s->dma_as, value + 16, 700000000); 115 break; 116 } 117 resplen = 8; 118 break; 119 120 case 0x00038002: /* Set clock rate */ 121 case 0x00038004: /* Set max clock rate */ 122 case 0x00038007: /* Set min clock rate */ 123 qemu_log_mask(LOG_UNIMP, 124 "bcm2835_property: %x set clock rates NYI\n", tag); 125 resplen = 8; 126 break; 127 128 /* Temperature */ 129 130 case 0x00030006: /* Get temperature */ 131 stl_le_phys(&s->dma_as, value + 16, 25000); 132 resplen = 8; 133 break; 134 135 case 0x0003000A: /* Get max temperature */ 136 stl_le_phys(&s->dma_as, value + 16, 99000); 137 resplen = 8; 138 break; 139 140 /* Frame buffer */ 141 142 case 0x00040001: /* Allocate buffer */ 143 stl_le_phys(&s->dma_as, value + 12, s->fbdev->base); 144 tmp_xres = newxres != NULL ? *newxres : s->fbdev->xres; 145 tmp_yres = newyres != NULL ? *newyres : s->fbdev->yres; 146 tmp_bpp = newbpp != NULL ? *newbpp : s->fbdev->bpp; 147 stl_le_phys(&s->dma_as, value + 16, 148 tmp_xres * tmp_yres * tmp_bpp / 8); 149 resplen = 8; 150 break; 151 case 0x00048001: /* Release buffer */ 152 resplen = 0; 153 break; 154 case 0x00040002: /* Blank screen */ 155 resplen = 4; 156 break; 157 case 0x00040003: /* Get display width/height */ 158 case 0x00040004: 159 tmp_xres = newxres != NULL ? *newxres : s->fbdev->xres; 160 tmp_yres = newyres != NULL ? *newyres : s->fbdev->yres; 161 stl_le_phys(&s->dma_as, value + 12, tmp_xres); 162 stl_le_phys(&s->dma_as, value + 16, tmp_yres); 163 resplen = 8; 164 break; 165 case 0x00044003: /* Test display width/height */ 166 case 0x00044004: 167 resplen = 8; 168 break; 169 case 0x00048003: /* Set display width/height */ 170 case 0x00048004: 171 xres = ldl_le_phys(&s->dma_as, value + 12); 172 newxres = &xres; 173 yres = ldl_le_phys(&s->dma_as, value + 16); 174 newyres = &yres; 175 resplen = 8; 176 break; 177 case 0x00040005: /* Get depth */ 178 tmp_bpp = newbpp != NULL ? *newbpp : s->fbdev->bpp; 179 stl_le_phys(&s->dma_as, value + 12, tmp_bpp); 180 resplen = 4; 181 break; 182 case 0x00044005: /* Test depth */ 183 resplen = 4; 184 break; 185 case 0x00048005: /* Set depth */ 186 bpp = ldl_le_phys(&s->dma_as, value + 12); 187 newbpp = &bpp; 188 resplen = 4; 189 break; 190 case 0x00040006: /* Get pixel order */ 191 tmp_pixo = newpixo != NULL ? *newpixo : s->fbdev->pixo; 192 stl_le_phys(&s->dma_as, value + 12, tmp_pixo); 193 resplen = 4; 194 break; 195 case 0x00044006: /* Test pixel order */ 196 resplen = 4; 197 break; 198 case 0x00048006: /* Set pixel order */ 199 pixo = ldl_le_phys(&s->dma_as, value + 12); 200 newpixo = &pixo; 201 resplen = 4; 202 break; 203 case 0x00040007: /* Get alpha */ 204 tmp_alpha = newalpha != NULL ? *newalpha : s->fbdev->alpha; 205 stl_le_phys(&s->dma_as, value + 12, tmp_alpha); 206 resplen = 4; 207 break; 208 case 0x00044007: /* Test pixel alpha */ 209 resplen = 4; 210 break; 211 case 0x00048007: /* Set alpha */ 212 alpha = ldl_le_phys(&s->dma_as, value + 12); 213 newalpha = α 214 resplen = 4; 215 break; 216 case 0x00040008: /* Get pitch */ 217 tmp_xres = newxres != NULL ? *newxres : s->fbdev->xres; 218 tmp_bpp = newbpp != NULL ? *newbpp : s->fbdev->bpp; 219 stl_le_phys(&s->dma_as, value + 12, tmp_xres * tmp_bpp / 8); 220 resplen = 4; 221 break; 222 case 0x00040009: /* Get virtual offset */ 223 tmp_xoffset = newxoffset != NULL ? *newxoffset : s->fbdev->xoffset; 224 tmp_yoffset = newyoffset != NULL ? *newyoffset : s->fbdev->yoffset; 225 stl_le_phys(&s->dma_as, value + 12, tmp_xoffset); 226 stl_le_phys(&s->dma_as, value + 16, tmp_yoffset); 227 resplen = 8; 228 break; 229 case 0x00044009: /* Test virtual offset */ 230 resplen = 8; 231 break; 232 case 0x00048009: /* Set virtual offset */ 233 xoffset = ldl_le_phys(&s->dma_as, value + 12); 234 newxoffset = &xoffset; 235 yoffset = ldl_le_phys(&s->dma_as, value + 16); 236 newyoffset = &yoffset; 237 resplen = 8; 238 break; 239 case 0x0004000a: /* Get/Test/Set overscan */ 240 case 0x0004400a: 241 case 0x0004800a: 242 stl_le_phys(&s->dma_as, value + 12, 0); 243 stl_le_phys(&s->dma_as, value + 16, 0); 244 stl_le_phys(&s->dma_as, value + 20, 0); 245 stl_le_phys(&s->dma_as, value + 24, 0); 246 resplen = 16; 247 break; 248 case 0x0004800b: /* Set palette */ 249 offset = ldl_le_phys(&s->dma_as, value + 12); 250 length = ldl_le_phys(&s->dma_as, value + 16); 251 n = 0; 252 while (n < length - offset) { 253 color = ldl_le_phys(&s->dma_as, value + 20 + (n << 2)); 254 stl_le_phys(&s->dma_as, 255 s->fbdev->vcram_base + ((offset + n) << 2), color); 256 n++; 257 } 258 stl_le_phys(&s->dma_as, value + 12, 0); 259 resplen = 4; 260 break; 261 262 case 0x00060001: /* Get DMA channels */ 263 /* channels 2-5 */ 264 stl_le_phys(&s->dma_as, value + 12, 0x003C); 265 resplen = 4; 266 break; 267 268 case 0x00050001: /* Get command line */ 269 resplen = 0; 270 break; 271 272 default: 273 qemu_log_mask(LOG_GUEST_ERROR, 274 "bcm2835_property: unhandled tag %08x\n", tag); 275 break; 276 } 277 278 if (tag == 0) { 279 break; 280 } 281 282 stl_le_phys(&s->dma_as, value + 8, (1 << 31) | resplen); 283 value += bufsize + 12; 284 } 285 286 /* Reconfigure framebuffer if required */ 287 if (newxres || newyres || newxoffset || newyoffset || newbpp || newpixo 288 || newalpha) { 289 bcm2835_fb_reconfigure(s->fbdev, newxres, newyres, newxoffset, 290 newyoffset, newbpp, newpixo, newalpha); 291 } 292 293 /* Buffer response code */ 294 stl_le_phys(&s->dma_as, s->addr + 4, (1 << 31)); 295 } 296 297 static uint64_t bcm2835_property_read(void *opaque, hwaddr offset, 298 unsigned size) 299 { 300 BCM2835PropertyState *s = opaque; 301 uint32_t res = 0; 302 303 switch (offset) { 304 case MBOX_AS_DATA: 305 res = MBOX_CHAN_PROPERTY | s->addr; 306 s->pending = false; 307 qemu_set_irq(s->mbox_irq, 0); 308 break; 309 310 case MBOX_AS_PENDING: 311 res = s->pending; 312 break; 313 314 default: 315 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n", 316 __func__, offset); 317 return 0; 318 } 319 320 return res; 321 } 322 323 static void bcm2835_property_write(void *opaque, hwaddr offset, 324 uint64_t value, unsigned size) 325 { 326 BCM2835PropertyState *s = opaque; 327 328 switch (offset) { 329 case MBOX_AS_DATA: 330 /* bcm2835_mbox should check our pending status before pushing */ 331 assert(!s->pending); 332 s->pending = true; 333 bcm2835_property_mbox_push(s, value); 334 qemu_set_irq(s->mbox_irq, 1); 335 break; 336 337 default: 338 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n", 339 __func__, offset); 340 return; 341 } 342 } 343 344 static const MemoryRegionOps bcm2835_property_ops = { 345 .read = bcm2835_property_read, 346 .write = bcm2835_property_write, 347 .endianness = DEVICE_NATIVE_ENDIAN, 348 .valid.min_access_size = 4, 349 .valid.max_access_size = 4, 350 }; 351 352 static const VMStateDescription vmstate_bcm2835_property = { 353 .name = TYPE_BCM2835_PROPERTY, 354 .version_id = 1, 355 .minimum_version_id = 1, 356 .fields = (VMStateField[]) { 357 VMSTATE_MACADDR(macaddr, BCM2835PropertyState), 358 VMSTATE_UINT32(addr, BCM2835PropertyState), 359 VMSTATE_BOOL(pending, BCM2835PropertyState), 360 VMSTATE_END_OF_LIST() 361 } 362 }; 363 364 static void bcm2835_property_init(Object *obj) 365 { 366 BCM2835PropertyState *s = BCM2835_PROPERTY(obj); 367 368 memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s, 369 TYPE_BCM2835_PROPERTY, 0x10); 370 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem); 371 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq); 372 } 373 374 static void bcm2835_property_reset(DeviceState *dev) 375 { 376 BCM2835PropertyState *s = BCM2835_PROPERTY(dev); 377 378 s->pending = false; 379 } 380 381 static void bcm2835_property_realize(DeviceState *dev, Error **errp) 382 { 383 BCM2835PropertyState *s = BCM2835_PROPERTY(dev); 384 Object *obj; 385 Error *err = NULL; 386 387 obj = object_property_get_link(OBJECT(dev), "fb", &err); 388 if (obj == NULL) { 389 error_setg(errp, "%s: required fb link not found: %s", 390 __func__, error_get_pretty(err)); 391 return; 392 } 393 394 s->fbdev = BCM2835_FB(obj); 395 396 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err); 397 if (obj == NULL) { 398 error_setg(errp, "%s: required dma-mr link not found: %s", 399 __func__, error_get_pretty(err)); 400 return; 401 } 402 403 s->dma_mr = MEMORY_REGION(obj); 404 address_space_init(&s->dma_as, s->dma_mr, NULL); 405 406 /* TODO: connect to MAC address of USB NIC device, once we emulate it */ 407 qemu_macaddr_default_if_unset(&s->macaddr); 408 409 bcm2835_property_reset(dev); 410 } 411 412 static Property bcm2835_property_props[] = { 413 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState, board_rev, 0), 414 DEFINE_PROP_END_OF_LIST() 415 }; 416 417 static void bcm2835_property_class_init(ObjectClass *klass, void *data) 418 { 419 DeviceClass *dc = DEVICE_CLASS(klass); 420 421 dc->props = bcm2835_property_props; 422 dc->realize = bcm2835_property_realize; 423 dc->vmsd = &vmstate_bcm2835_property; 424 } 425 426 static TypeInfo bcm2835_property_info = { 427 .name = TYPE_BCM2835_PROPERTY, 428 .parent = TYPE_SYS_BUS_DEVICE, 429 .instance_size = sizeof(BCM2835PropertyState), 430 .class_init = bcm2835_property_class_init, 431 .instance_init = bcm2835_property_init, 432 }; 433 434 static void bcm2835_property_register_types(void) 435 { 436 type_register_static(&bcm2835_property_info); 437 } 438 439 type_init(bcm2835_property_register_types) 440