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