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