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