xref: /openbmc/qemu/hw/misc/bcm2835_property.c (revision 64552b6b)
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/irq.h"
10 #include "hw/misc/bcm2835_mbox_defs.h"
11 #include "sysemu/dma.h"
12 #include "qemu/log.h"
13 #include "qemu/module.h"
14 
15 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
16 
17 static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value)
18 {
19     uint32_t tag;
20     uint32_t bufsize;
21     uint32_t tot_len;
22     size_t resplen;
23     uint32_t tmp;
24     int n;
25     uint32_t offset, length, color;
26 
27     /*
28      * Copy the current state of the framebuffer config; we will update
29      * this copy as we process tags and then ask the framebuffer to use
30      * it at the end.
31      */
32     BCM2835FBConfig fbconfig = s->fbdev->config;
33     bool fbconfig_updated = false;
34 
35     value &= ~0xf;
36 
37     s->addr = value;
38 
39     tot_len = ldl_le_phys(&s->dma_as, value);
40 
41     /* @(addr + 4) : Buffer response code */
42     value = s->addr + 8;
43     while (value + 8 <= s->addr + tot_len) {
44         tag = ldl_le_phys(&s->dma_as, value);
45         bufsize = ldl_le_phys(&s->dma_as, value + 4);
46         /* @(value + 8) : Request/response indicator */
47         resplen = 0;
48         switch (tag) {
49         case 0x00000000: /* End tag */
50             break;
51         case 0x00000001: /* Get firmware revision */
52             stl_le_phys(&s->dma_as, value + 12, 346337);
53             resplen = 4;
54             break;
55         case 0x00010001: /* Get board model */
56             qemu_log_mask(LOG_UNIMP,
57                           "bcm2835_property: %x get board model NYI\n", tag);
58             resplen = 4;
59             break;
60         case 0x00010002: /* Get board revision */
61             stl_le_phys(&s->dma_as, value + 12, s->board_rev);
62             resplen = 4;
63             break;
64         case 0x00010003: /* Get board MAC address */
65             resplen = sizeof(s->macaddr.a);
66             dma_memory_write(&s->dma_as, value + 12, s->macaddr.a, resplen);
67             break;
68         case 0x00010004: /* Get board serial */
69             qemu_log_mask(LOG_UNIMP,
70                           "bcm2835_property: %x get board serial NYI\n", tag);
71             resplen = 8;
72             break;
73         case 0x00010005: /* Get ARM memory */
74             /* base */
75             stl_le_phys(&s->dma_as, value + 12, 0);
76             /* size */
77             stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_base);
78             resplen = 8;
79             break;
80         case 0x00010006: /* Get VC memory */
81             /* base */
82             stl_le_phys(&s->dma_as, value + 12, s->fbdev->vcram_base);
83             /* size */
84             stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_size);
85             resplen = 8;
86             break;
87         case 0x00028001: /* Set power state */
88             /* Assume that whatever device they asked for exists,
89              * and we'll just claim we set it to the desired state
90              */
91             tmp = ldl_le_phys(&s->dma_as, value + 16);
92             stl_le_phys(&s->dma_as, value + 16, (tmp & 1));
93             resplen = 8;
94             break;
95 
96         /* Clocks */
97 
98         case 0x00030001: /* Get clock state */
99             stl_le_phys(&s->dma_as, value + 16, 0x1);
100             resplen = 8;
101             break;
102 
103         case 0x00038001: /* Set clock state */
104             qemu_log_mask(LOG_UNIMP,
105                           "bcm2835_property: %x set clock state NYI\n", tag);
106             resplen = 8;
107             break;
108 
109         case 0x00030002: /* Get clock rate */
110         case 0x00030004: /* Get max clock rate */
111         case 0x00030007: /* Get min clock rate */
112             switch (ldl_le_phys(&s->dma_as, value + 12)) {
113             case 1: /* EMMC */
114                 stl_le_phys(&s->dma_as, value + 16, 50000000);
115                 break;
116             case 2: /* UART */
117                 stl_le_phys(&s->dma_as, value + 16, 3000000);
118                 break;
119             default:
120                 stl_le_phys(&s->dma_as, value + 16, 700000000);
121                 break;
122             }
123             resplen = 8;
124             break;
125 
126         case 0x00038002: /* Set clock rate */
127         case 0x00038004: /* Set max clock rate */
128         case 0x00038007: /* Set min clock rate */
129             qemu_log_mask(LOG_UNIMP,
130                           "bcm2835_property: %x set clock rates NYI\n", tag);
131             resplen = 8;
132             break;
133 
134         /* Temperature */
135 
136         case 0x00030006: /* Get temperature */
137             stl_le_phys(&s->dma_as, value + 16, 25000);
138             resplen = 8;
139             break;
140 
141         case 0x0003000A: /* Get max temperature */
142             stl_le_phys(&s->dma_as, value + 16, 99000);
143             resplen = 8;
144             break;
145 
146         /* Frame buffer */
147 
148         case 0x00040001: /* Allocate buffer */
149             stl_le_phys(&s->dma_as, value + 12, fbconfig.base);
150             stl_le_phys(&s->dma_as, value + 16,
151                         bcm2835_fb_get_size(&fbconfig));
152             resplen = 8;
153             break;
154         case 0x00048001: /* Release buffer */
155             resplen = 0;
156             break;
157         case 0x00040002: /* Blank screen */
158             resplen = 4;
159             break;
160         case 0x00044003: /* Test physical display width/height */
161         case 0x00044004: /* Test virtual display width/height */
162             resplen = 8;
163             break;
164         case 0x00048003: /* Set physical display width/height */
165             fbconfig.xres = ldl_le_phys(&s->dma_as, value + 12);
166             fbconfig.yres = ldl_le_phys(&s->dma_as, value + 16);
167             bcm2835_fb_validate_config(&fbconfig);
168             fbconfig_updated = true;
169             /* fall through */
170         case 0x00040003: /* Get physical display width/height */
171             stl_le_phys(&s->dma_as, value + 12, fbconfig.xres);
172             stl_le_phys(&s->dma_as, value + 16, fbconfig.yres);
173             resplen = 8;
174             break;
175         case 0x00048004: /* Set virtual display width/height */
176             fbconfig.xres_virtual = ldl_le_phys(&s->dma_as, value + 12);
177             fbconfig.yres_virtual = ldl_le_phys(&s->dma_as, value + 16);
178             bcm2835_fb_validate_config(&fbconfig);
179             fbconfig_updated = true;
180             /* fall through */
181         case 0x00040004: /* Get virtual display width/height */
182             stl_le_phys(&s->dma_as, value + 12, fbconfig.xres_virtual);
183             stl_le_phys(&s->dma_as, value + 16, fbconfig.yres_virtual);
184             resplen = 8;
185             break;
186         case 0x00044005: /* Test depth */
187             resplen = 4;
188             break;
189         case 0x00048005: /* Set depth */
190             fbconfig.bpp = ldl_le_phys(&s->dma_as, value + 12);
191             bcm2835_fb_validate_config(&fbconfig);
192             fbconfig_updated = true;
193             /* fall through */
194         case 0x00040005: /* Get depth */
195             stl_le_phys(&s->dma_as, value + 12, fbconfig.bpp);
196             resplen = 4;
197             break;
198         case 0x00044006: /* Test pixel order */
199             resplen = 4;
200             break;
201         case 0x00048006: /* Set pixel order */
202             fbconfig.pixo = ldl_le_phys(&s->dma_as, value + 12);
203             bcm2835_fb_validate_config(&fbconfig);
204             fbconfig_updated = true;
205             /* fall through */
206         case 0x00040006: /* Get pixel order */
207             stl_le_phys(&s->dma_as, value + 12, fbconfig.pixo);
208             resplen = 4;
209             break;
210         case 0x00044007: /* Test pixel alpha */
211             resplen = 4;
212             break;
213         case 0x00048007: /* Set alpha */
214             fbconfig.alpha = ldl_le_phys(&s->dma_as, value + 12);
215             bcm2835_fb_validate_config(&fbconfig);
216             fbconfig_updated = true;
217             /* fall through */
218         case 0x00040007: /* Get alpha */
219             stl_le_phys(&s->dma_as, value + 12, fbconfig.alpha);
220             resplen = 4;
221             break;
222         case 0x00040008: /* Get pitch */
223             stl_le_phys(&s->dma_as, value + 12,
224                         bcm2835_fb_get_pitch(&fbconfig));
225             resplen = 4;
226             break;
227         case 0x00044009: /* Test virtual offset */
228             resplen = 8;
229             break;
230         case 0x00048009: /* Set virtual offset */
231             fbconfig.xoffset = ldl_le_phys(&s->dma_as, value + 12);
232             fbconfig.yoffset = ldl_le_phys(&s->dma_as, value + 16);
233             bcm2835_fb_validate_config(&fbconfig);
234             fbconfig_updated = true;
235             /* fall through */
236         case 0x00040009: /* Get virtual offset */
237             stl_le_phys(&s->dma_as, value + 12, fbconfig.xoffset);
238             stl_le_phys(&s->dma_as, value + 16, fbconfig.yoffset);
239             resplen = 8;
240             break;
241         case 0x0004000a: /* Get/Test/Set overscan */
242         case 0x0004400a:
243         case 0x0004800a:
244             stl_le_phys(&s->dma_as, value + 12, 0);
245             stl_le_phys(&s->dma_as, value + 16, 0);
246             stl_le_phys(&s->dma_as, value + 20, 0);
247             stl_le_phys(&s->dma_as, value + 24, 0);
248             resplen = 16;
249             break;
250         case 0x0004800b: /* Set palette */
251             offset = ldl_le_phys(&s->dma_as, value + 12);
252             length = ldl_le_phys(&s->dma_as, value + 16);
253             n = 0;
254             while (n < length - offset) {
255                 color = ldl_le_phys(&s->dma_as, value + 20 + (n << 2));
256                 stl_le_phys(&s->dma_as,
257                             s->fbdev->vcram_base + ((offset + n) << 2), color);
258                 n++;
259             }
260             stl_le_phys(&s->dma_as, value + 12, 0);
261             resplen = 4;
262             break;
263 
264         case 0x00060001: /* Get DMA channels */
265             /* channels 2-5 */
266             stl_le_phys(&s->dma_as, value + 12, 0x003C);
267             resplen = 4;
268             break;
269 
270         case 0x00050001: /* Get command line */
271             resplen = 0;
272             break;
273 
274         default:
275             qemu_log_mask(LOG_GUEST_ERROR,
276                           "bcm2835_property: unhandled tag %08x\n", tag);
277             break;
278         }
279 
280         if (tag == 0) {
281             break;
282         }
283 
284         stl_le_phys(&s->dma_as, value + 8, (1 << 31) | resplen);
285         value += bufsize + 12;
286     }
287 
288     /* Reconfigure framebuffer if required */
289     if (fbconfig_updated) {
290         bcm2835_fb_reconfigure(s->fbdev, &fbconfig);
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