]>
Commit | Line | Data |
---|---|---|
04f1ab15 AB |
1 | /* |
2 | * Raspberry Pi emulation (c) 2012 Gregory Estrade | |
3 | * This code is licensed under the GNU GPLv2 and later. | |
4 | */ | |
5 | ||
c964b660 | 6 | #include "qemu/osdep.h" |
da34e65c | 7 | #include "qapi/error.h" |
04f1ab15 | 8 | #include "hw/misc/bcm2835_property.h" |
a27bd6c7 | 9 | #include "hw/qdev-properties.h" |
d6454270 | 10 | #include "migration/vmstate.h" |
64552b6b | 11 | #include "hw/irq.h" |
04f1ab15 AB |
12 | #include "hw/misc/bcm2835_mbox_defs.h" |
13 | #include "sysemu/dma.h" | |
03dd024f | 14 | #include "qemu/log.h" |
0b8fa32f | 15 | #include "qemu/module.h" |
19845504 | 16 | #include "trace.h" |
04f1ab15 AB |
17 | |
18 | /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */ | |
19 | ||
20 | static 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; | |
355a8ccc GE |
27 | int n; |
28 | uint32_t offset, length, color; | |
193100b5 PM |
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; | |
04f1ab15 AB |
37 | |
38 | value &= ~0xf; | |
39 | ||
40 | s->addr = value; | |
41 | ||
eab71394 | 42 | tot_len = ldl_le_phys(&s->dma_as, value); |
04f1ab15 AB |
43 | |
44 | /* @(addr + 4) : Buffer response code */ | |
45 | value = s->addr + 8; | |
46 | while (value + 8 <= s->addr + tot_len) { | |
eab71394 AB |
47 | tag = ldl_le_phys(&s->dma_as, value); |
48 | bufsize = ldl_le_phys(&s->dma_as, value + 4); | |
04f1ab15 AB |
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 */ | |
eab71394 | 55 | stl_le_phys(&s->dma_as, value + 12, 346337); |
04f1ab15 AB |
56 | resplen = 4; |
57 | break; | |
58 | case 0x00010001: /* Get board model */ | |
59 | qemu_log_mask(LOG_UNIMP, | |
e1ecf8c8 PMD |
60 | "bcm2835_property: 0x%08x get board model NYI\n", |
61 | tag); | |
04f1ab15 AB |
62 | resplen = 4; |
63 | break; | |
64 | case 0x00010002: /* Get board revision */ | |
eab71394 | 65 | stl_le_phys(&s->dma_as, value + 12, s->board_rev); |
04f1ab15 AB |
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, | |
e1ecf8c8 PMD |
74 | "bcm2835_property: 0x%08x get board serial NYI\n", |
75 | tag); | |
04f1ab15 AB |
76 | resplen = 8; |
77 | break; | |
78 | case 0x00010005: /* Get ARM memory */ | |
79 | /* base */ | |
eab71394 | 80 | stl_le_phys(&s->dma_as, value + 12, 0); |
04f1ab15 | 81 | /* size */ |
355a8ccc GE |
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); | |
04f1ab15 AB |
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 | */ | |
eab71394 AB |
96 | tmp = ldl_le_phys(&s->dma_as, value + 16); |
97 | stl_le_phys(&s->dma_as, value + 16, (tmp & 1)); | |
04f1ab15 AB |
98 | resplen = 8; |
99 | break; | |
100 | ||
101 | /* Clocks */ | |
102 | ||
103 | case 0x00030001: /* Get clock state */ | |
eab71394 | 104 | stl_le_phys(&s->dma_as, value + 16, 0x1); |
04f1ab15 AB |
105 | resplen = 8; |
106 | break; | |
107 | ||
108 | case 0x00038001: /* Set clock state */ | |
109 | qemu_log_mask(LOG_UNIMP, | |
e1ecf8c8 PMD |
110 | "bcm2835_property: 0x%08x set clock state NYI\n", |
111 | tag); | |
04f1ab15 AB |
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 */ | |
eab71394 | 118 | switch (ldl_le_phys(&s->dma_as, value + 12)) { |
04f1ab15 | 119 | case 1: /* EMMC */ |
eab71394 | 120 | stl_le_phys(&s->dma_as, value + 16, 50000000); |
04f1ab15 AB |
121 | break; |
122 | case 2: /* UART */ | |
eab71394 | 123 | stl_le_phys(&s->dma_as, value + 16, 3000000); |
04f1ab15 AB |
124 | break; |
125 | default: | |
eab71394 | 126 | stl_le_phys(&s->dma_as, value + 16, 700000000); |
04f1ab15 AB |
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, | |
e1ecf8c8 PMD |
136 | "bcm2835_property: 0x%08x set clock rate NYI\n", |
137 | tag); | |
04f1ab15 AB |
138 | resplen = 8; |
139 | break; | |
140 | ||
141 | /* Temperature */ | |
142 | ||
143 | case 0x00030006: /* Get temperature */ | |
eab71394 | 144 | stl_le_phys(&s->dma_as, value + 16, 25000); |
04f1ab15 AB |
145 | resplen = 8; |
146 | break; | |
147 | ||
148 | case 0x0003000A: /* Get max temperature */ | |
eab71394 | 149 | stl_le_phys(&s->dma_as, value + 16, 99000); |
04f1ab15 AB |
150 | resplen = 8; |
151 | break; | |
152 | ||
355a8ccc GE |
153 | /* Frame buffer */ |
154 | ||
155 | case 0x00040001: /* Allocate buffer */ | |
193100b5 | 156 | stl_le_phys(&s->dma_as, value + 12, fbconfig.base); |
27a5dc7b | 157 | stl_le_phys(&s->dma_as, value + 16, |
9a1f03f4 | 158 | bcm2835_fb_get_size(&fbconfig)); |
355a8ccc GE |
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; | |
01f18af9 PM |
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 */ | |
193100b5 PM |
172 | fbconfig.xres = ldl_le_phys(&s->dma_as, value + 12); |
173 | fbconfig.yres = ldl_le_phys(&s->dma_as, value + 16); | |
f8add62c | 174 | bcm2835_fb_validate_config(&fbconfig); |
193100b5 | 175 | fbconfig_updated = true; |
f8add62c PM |
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); | |
355a8ccc GE |
180 | resplen = 8; |
181 | break; | |
01f18af9 PM |
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); | |
f8add62c | 185 | bcm2835_fb_validate_config(&fbconfig); |
01f18af9 | 186 | fbconfig_updated = true; |
f8add62c PM |
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); | |
01f18af9 PM |
191 | resplen = 8; |
192 | break; | |
355a8ccc GE |
193 | case 0x00044005: /* Test depth */ |
194 | resplen = 4; | |
195 | break; | |
196 | case 0x00048005: /* Set depth */ | |
193100b5 | 197 | fbconfig.bpp = ldl_le_phys(&s->dma_as, value + 12); |
f8add62c | 198 | bcm2835_fb_validate_config(&fbconfig); |
193100b5 | 199 | fbconfig_updated = true; |
f8add62c PM |
200 | /* fall through */ |
201 | case 0x00040005: /* Get depth */ | |
202 | stl_le_phys(&s->dma_as, value + 12, fbconfig.bpp); | |
355a8ccc GE |
203 | resplen = 4; |
204 | break; | |
205 | case 0x00044006: /* Test pixel order */ | |
206 | resplen = 4; | |
207 | break; | |
208 | case 0x00048006: /* Set pixel order */ | |
193100b5 | 209 | fbconfig.pixo = ldl_le_phys(&s->dma_as, value + 12); |
f8add62c | 210 | bcm2835_fb_validate_config(&fbconfig); |
193100b5 | 211 | fbconfig_updated = true; |
f8add62c PM |
212 | /* fall through */ |
213 | case 0x00040006: /* Get pixel order */ | |
214 | stl_le_phys(&s->dma_as, value + 12, fbconfig.pixo); | |
355a8ccc GE |
215 | resplen = 4; |
216 | break; | |
217 | case 0x00044007: /* Test pixel alpha */ | |
218 | resplen = 4; | |
219 | break; | |
220 | case 0x00048007: /* Set alpha */ | |
193100b5 | 221 | fbconfig.alpha = ldl_le_phys(&s->dma_as, value + 12); |
f8add62c | 222 | bcm2835_fb_validate_config(&fbconfig); |
193100b5 | 223 | fbconfig_updated = true; |
f8add62c PM |
224 | /* fall through */ |
225 | case 0x00040007: /* Get alpha */ | |
226 | stl_le_phys(&s->dma_as, value + 12, fbconfig.alpha); | |
355a8ccc GE |
227 | resplen = 4; |
228 | break; | |
229 | case 0x00040008: /* Get pitch */ | |
193100b5 | 230 | stl_le_phys(&s->dma_as, value + 12, |
9a1f03f4 | 231 | bcm2835_fb_get_pitch(&fbconfig)); |
355a8ccc GE |
232 | resplen = 4; |
233 | break; | |
355a8ccc GE |
234 | case 0x00044009: /* Test virtual offset */ |
235 | resplen = 8; | |
236 | break; | |
237 | case 0x00048009: /* Set virtual offset */ | |
193100b5 PM |
238 | fbconfig.xoffset = ldl_le_phys(&s->dma_as, value + 12); |
239 | fbconfig.yoffset = ldl_le_phys(&s->dma_as, value + 16); | |
f8add62c | 240 | bcm2835_fb_validate_config(&fbconfig); |
193100b5 | 241 | fbconfig_updated = true; |
f8add62c PM |
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); | |
355a8ccc GE |
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; | |
04f1ab15 AB |
270 | |
271 | case 0x00060001: /* Get DMA channels */ | |
272 | /* channels 2-5 */ | |
eab71394 | 273 | stl_le_phys(&s->dma_as, value + 12, 0x003C); |
04f1ab15 AB |
274 | resplen = 4; |
275 | break; | |
276 | ||
277 | case 0x00050001: /* Get command line */ | |
278 | resplen = 0; | |
279 | break; | |
280 | ||
281 | default: | |
e1ecf8c8 PMD |
282 | qemu_log_mask(LOG_UNIMP, |
283 | "bcm2835_property: unhandled tag 0x%08x\n", tag); | |
04f1ab15 AB |
284 | break; |
285 | } | |
286 | ||
19845504 | 287 | trace_bcm2835_mbox_property(tag, bufsize, resplen); |
04f1ab15 AB |
288 | if (tag == 0) { |
289 | break; | |
290 | } | |
291 | ||
eab71394 | 292 | stl_le_phys(&s->dma_as, value + 8, (1 << 31) | resplen); |
04f1ab15 AB |
293 | value += bufsize + 12; |
294 | } | |
295 | ||
355a8ccc | 296 | /* Reconfigure framebuffer if required */ |
193100b5 PM |
297 | if (fbconfig_updated) { |
298 | bcm2835_fb_reconfigure(s->fbdev, &fbconfig); | |
355a8ccc GE |
299 | } |
300 | ||
04f1ab15 | 301 | /* Buffer response code */ |
eab71394 | 302 | stl_le_phys(&s->dma_as, s->addr + 4, (1 << 31)); |
04f1ab15 AB |
303 | } |
304 | ||
305 | static 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 | ||
331 | static 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 | ||
352 | static 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 | ||
360 | static 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 | ||
372 | static 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 | ||
382 | static void bcm2835_property_reset(DeviceState *dev) | |
383 | { | |
384 | BCM2835PropertyState *s = BCM2835_PROPERTY(dev); | |
385 | ||
386 | s->pending = false; | |
387 | } | |
388 | ||
389 | static void bcm2835_property_realize(DeviceState *dev, Error **errp) | |
390 | { | |
391 | BCM2835PropertyState *s = BCM2835_PROPERTY(dev); | |
392 | Object *obj; | |
393 | Error *err = NULL; | |
394 | ||
355a8ccc GE |
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 | ||
04f1ab15 AB |
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); | |
e55a8b37 | 412 | address_space_init(&s->dma_as, s->dma_mr, TYPE_BCM2835_PROPERTY "-memory"); |
04f1ab15 AB |
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 | ||
420 | static Property bcm2835_property_props[] = { | |
f0afa731 | 421 | DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState, board_rev, 0), |
04f1ab15 AB |
422 | DEFINE_PROP_END_OF_LIST() |
423 | }; | |
424 | ||
425 | static 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 | ||
434 | static 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 | ||
442 | static void bcm2835_property_register_types(void) | |
443 | { | |
444 | type_register_static(&bcm2835_property_info); | |
445 | } | |
446 | ||
447 | type_init(bcm2835_property_register_types) |