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