2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
8 #include "qemu/osdep.h"
9 #include "qapi/error.h"
10 #include "hw/misc/bcm2835_property.h"
11 #include "hw/qdev-properties.h"
12 #include "migration/vmstate.h"
14 #include "hw/misc/bcm2835_mbox_defs.h"
15 #include "hw/arm/raspberrypi-fw-defs.h"
16 #include "sysemu/dma.h"
18 #include "qemu/module.h"
20 #include "hw/arm/raspi_platform.h"
22 #define VCHI_BUSADDR_SIZE sizeof(uint32_t)
24 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
26 static void bcm2835_property_mbox_push(BCM2835PropertyState
*s
, uint32_t value
)
34 uint32_t offset
, length
, color
;
37 * Copy the current state of the framebuffer config; we will update
38 * this copy as we process tags and then ask the framebuffer to use
41 BCM2835FBConfig fbconfig
= s
->fbdev
->config
;
42 bool fbconfig_updated
= false;
48 tot_len
= ldl_le_phys(&s
->dma_as
, value
);
50 /* @(addr + 4) : Buffer response code */
52 while (value
+ 8 <= s
->addr
+ tot_len
) {
53 tag
= ldl_le_phys(&s
->dma_as
, value
);
54 bufsize
= ldl_le_phys(&s
->dma_as
, value
+ 4);
55 /* @(value + 8) : Request/response indicator */
58 case RPI_FWREQ_PROPERTY_END
:
60 case RPI_FWREQ_GET_FIRMWARE_REVISION
:
61 stl_le_phys(&s
->dma_as
, value
+ 12, 346337);
64 case RPI_FWREQ_GET_BOARD_MODEL
:
65 qemu_log_mask(LOG_UNIMP
,
66 "bcm2835_property: 0x%08x get board model NYI\n",
70 case RPI_FWREQ_GET_BOARD_REVISION
:
71 stl_le_phys(&s
->dma_as
, value
+ 12, s
->board_rev
);
74 case RPI_FWREQ_GET_BOARD_MAC_ADDRESS
:
75 resplen
= sizeof(s
->macaddr
.a
);
76 dma_memory_write(&s
->dma_as
, value
+ 12, s
->macaddr
.a
, resplen
,
77 MEMTXATTRS_UNSPECIFIED
);
79 case RPI_FWREQ_GET_BOARD_SERIAL
:
80 qemu_log_mask(LOG_UNIMP
,
81 "bcm2835_property: 0x%08x get board serial NYI\n",
85 case RPI_FWREQ_GET_ARM_MEMORY
:
87 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
89 stl_le_phys(&s
->dma_as
, value
+ 16, s
->fbdev
->vcram_base
);
92 case RPI_FWREQ_GET_VC_MEMORY
:
94 stl_le_phys(&s
->dma_as
, value
+ 12, s
->fbdev
->vcram_base
);
96 stl_le_phys(&s
->dma_as
, value
+ 16, s
->fbdev
->vcram_size
);
99 case RPI_FWREQ_SET_POWER_STATE
:
100 /* Assume that whatever device they asked for exists,
101 * and we'll just claim we set it to the desired state
103 tmp
= ldl_le_phys(&s
->dma_as
, value
+ 16);
104 stl_le_phys(&s
->dma_as
, value
+ 16, (tmp
& 1));
110 case RPI_FWREQ_GET_CLOCK_STATE
:
111 stl_le_phys(&s
->dma_as
, value
+ 16, 0x1);
115 case RPI_FWREQ_SET_CLOCK_STATE
:
116 qemu_log_mask(LOG_UNIMP
,
117 "bcm2835_property: 0x%08x set clock state NYI\n",
122 case RPI_FWREQ_GET_CLOCK_RATE
:
123 case RPI_FWREQ_GET_MAX_CLOCK_RATE
:
124 case RPI_FWREQ_GET_MIN_CLOCK_RATE
:
125 switch (ldl_le_phys(&s
->dma_as
, value
+ 12)) {
126 case RPI_FIRMWARE_EMMC_CLK_ID
:
127 stl_le_phys(&s
->dma_as
, value
+ 16, RPI_FIRMWARE_EMMC_CLK_RATE
);
129 case RPI_FIRMWARE_UART_CLK_ID
:
130 stl_le_phys(&s
->dma_as
, value
+ 16, RPI_FIRMWARE_UART_CLK_RATE
);
132 case RPI_FIRMWARE_CORE_CLK_ID
:
133 stl_le_phys(&s
->dma_as
, value
+ 16, RPI_FIRMWARE_CORE_CLK_RATE
);
136 stl_le_phys(&s
->dma_as
, value
+ 16,
137 RPI_FIRMWARE_DEFAULT_CLK_RATE
);
143 case RPI_FWREQ_GET_CLOCKS
:
144 /* TODO: add more clock IDs if needed */
145 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
146 stl_le_phys(&s
->dma_as
, value
+ 16, RPI_FIRMWARE_ARM_CLK_ID
);
150 case RPI_FWREQ_SET_CLOCK_RATE
:
151 case RPI_FWREQ_SET_MAX_CLOCK_RATE
:
152 case RPI_FWREQ_SET_MIN_CLOCK_RATE
:
153 qemu_log_mask(LOG_UNIMP
,
154 "bcm2835_property: 0x%08x set clock rate NYI\n",
161 case RPI_FWREQ_GET_TEMPERATURE
:
162 stl_le_phys(&s
->dma_as
, value
+ 16, 25000);
166 case RPI_FWREQ_GET_MAX_TEMPERATURE
:
167 stl_le_phys(&s
->dma_as
, value
+ 16, 99000);
173 case RPI_FWREQ_FRAMEBUFFER_ALLOCATE
:
174 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.base
);
175 stl_le_phys(&s
->dma_as
, value
+ 16,
176 bcm2835_fb_get_size(&fbconfig
));
179 case RPI_FWREQ_FRAMEBUFFER_RELEASE
:
182 case RPI_FWREQ_FRAMEBUFFER_BLANK
:
185 case RPI_FWREQ_FRAMEBUFFER_TEST_PHYSICAL_WIDTH_HEIGHT
:
186 case RPI_FWREQ_FRAMEBUFFER_TEST_VIRTUAL_WIDTH_HEIGHT
:
189 case RPI_FWREQ_FRAMEBUFFER_SET_PHYSICAL_WIDTH_HEIGHT
:
190 fbconfig
.xres
= ldl_le_phys(&s
->dma_as
, value
+ 12);
191 fbconfig
.yres
= ldl_le_phys(&s
->dma_as
, value
+ 16);
192 bcm2835_fb_validate_config(&fbconfig
);
193 fbconfig_updated
= true;
195 case RPI_FWREQ_FRAMEBUFFER_GET_PHYSICAL_WIDTH_HEIGHT
:
196 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xres
);
197 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yres
);
200 case RPI_FWREQ_FRAMEBUFFER_SET_VIRTUAL_WIDTH_HEIGHT
:
201 fbconfig
.xres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 12);
202 fbconfig
.yres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 16);
203 bcm2835_fb_validate_config(&fbconfig
);
204 fbconfig_updated
= true;
206 case RPI_FWREQ_FRAMEBUFFER_GET_VIRTUAL_WIDTH_HEIGHT
:
207 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xres_virtual
);
208 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yres_virtual
);
211 case RPI_FWREQ_FRAMEBUFFER_TEST_DEPTH
:
214 case RPI_FWREQ_FRAMEBUFFER_SET_DEPTH
:
215 fbconfig
.bpp
= ldl_le_phys(&s
->dma_as
, value
+ 12);
216 bcm2835_fb_validate_config(&fbconfig
);
217 fbconfig_updated
= true;
219 case RPI_FWREQ_FRAMEBUFFER_GET_DEPTH
:
220 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.bpp
);
223 case RPI_FWREQ_FRAMEBUFFER_TEST_PIXEL_ORDER
:
226 case RPI_FWREQ_FRAMEBUFFER_SET_PIXEL_ORDER
:
227 fbconfig
.pixo
= ldl_le_phys(&s
->dma_as
, value
+ 12);
228 bcm2835_fb_validate_config(&fbconfig
);
229 fbconfig_updated
= true;
231 case RPI_FWREQ_FRAMEBUFFER_GET_PIXEL_ORDER
:
232 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.pixo
);
235 case RPI_FWREQ_FRAMEBUFFER_TEST_ALPHA_MODE
:
238 case RPI_FWREQ_FRAMEBUFFER_SET_ALPHA_MODE
:
239 fbconfig
.alpha
= ldl_le_phys(&s
->dma_as
, value
+ 12);
240 bcm2835_fb_validate_config(&fbconfig
);
241 fbconfig_updated
= true;
243 case RPI_FWREQ_FRAMEBUFFER_GET_ALPHA_MODE
:
244 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.alpha
);
247 case RPI_FWREQ_FRAMEBUFFER_GET_PITCH
:
248 stl_le_phys(&s
->dma_as
, value
+ 12,
249 bcm2835_fb_get_pitch(&fbconfig
));
252 case RPI_FWREQ_FRAMEBUFFER_TEST_VIRTUAL_OFFSET
:
255 case RPI_FWREQ_FRAMEBUFFER_SET_VIRTUAL_OFFSET
:
256 fbconfig
.xoffset
= ldl_le_phys(&s
->dma_as
, value
+ 12);
257 fbconfig
.yoffset
= ldl_le_phys(&s
->dma_as
, value
+ 16);
258 bcm2835_fb_validate_config(&fbconfig
);
259 fbconfig_updated
= true;
261 case RPI_FWREQ_FRAMEBUFFER_GET_VIRTUAL_OFFSET
:
262 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xoffset
);
263 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yoffset
);
266 case RPI_FWREQ_FRAMEBUFFER_GET_OVERSCAN
:
267 case RPI_FWREQ_FRAMEBUFFER_TEST_OVERSCAN
:
268 case RPI_FWREQ_FRAMEBUFFER_SET_OVERSCAN
:
269 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
270 stl_le_phys(&s
->dma_as
, value
+ 16, 0);
271 stl_le_phys(&s
->dma_as
, value
+ 20, 0);
272 stl_le_phys(&s
->dma_as
, value
+ 24, 0);
275 case RPI_FWREQ_FRAMEBUFFER_SET_PALETTE
:
276 offset
= ldl_le_phys(&s
->dma_as
, value
+ 12);
277 length
= ldl_le_phys(&s
->dma_as
, value
+ 16);
279 while (n
< length
- offset
) {
280 color
= ldl_le_phys(&s
->dma_as
, value
+ 20 + (n
<< 2));
281 stl_le_phys(&s
->dma_as
,
282 s
->fbdev
->vcram_base
+ ((offset
+ n
) << 2), color
);
285 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
289 case RPI_FWREQ_FRAMEBUFFER_GET_NUM_DISPLAYS
:
290 stl_le_phys(&s
->dma_as
, value
+ 12, 1);
294 case RPI_FWREQ_GET_DMA_CHANNELS
:
296 stl_le_phys(&s
->dma_as
, value
+ 12, 0x003C);
300 case RPI_FWREQ_GET_COMMAND_LINE
:
302 * We follow the firmware behaviour: no NUL terminator is
303 * written to the buffer, and if the buffer is too short
304 * we report the required length in the response header
305 * and copy nothing to the buffer.
307 resplen
= strlen(s
->command_line
);
308 if (bufsize
>= resplen
)
309 address_space_write(&s
->dma_as
, value
+ 12,
310 MEMTXATTRS_UNSPECIFIED
, s
->command_line
,
314 case RPI_FWREQ_GET_THROTTLED
:
315 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
319 case RPI_FWREQ_VCHIQ_INIT
:
320 stl_le_phys(&s
->dma_as
,
321 value
+ offsetof(rpi_firmware_prop_request_t
, payload
),
323 resplen
= VCHI_BUSADDR_SIZE
;
326 qemu_log_mask(LOG_UNIMP
,
327 "bcm2835_property: unhandled tag 0x%08x\n", tag
);
331 trace_bcm2835_mbox_property(tag
, bufsize
, resplen
);
336 stl_le_phys(&s
->dma_as
, value
+ 8, (1 << 31) | resplen
);
337 value
+= bufsize
+ 12;
340 /* Reconfigure framebuffer if required */
341 if (fbconfig_updated
) {
342 bcm2835_fb_reconfigure(s
->fbdev
, &fbconfig
);
345 /* Buffer response code */
346 stl_le_phys(&s
->dma_as
, s
->addr
+ 4, (1 << 31));
349 static uint64_t bcm2835_property_read(void *opaque
, hwaddr offset
,
352 BCM2835PropertyState
*s
= opaque
;
357 res
= MBOX_CHAN_PROPERTY
| s
->addr
;
359 qemu_set_irq(s
->mbox_irq
, 0);
362 case MBOX_AS_PENDING
:
367 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
375 static void bcm2835_property_write(void *opaque
, hwaddr offset
,
376 uint64_t value
, unsigned size
)
378 BCM2835PropertyState
*s
= opaque
;
382 /* bcm2835_mbox should check our pending status before pushing */
385 bcm2835_property_mbox_push(s
, value
);
386 qemu_set_irq(s
->mbox_irq
, 1);
390 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
396 static const MemoryRegionOps bcm2835_property_ops
= {
397 .read
= bcm2835_property_read
,
398 .write
= bcm2835_property_write
,
399 .endianness
= DEVICE_NATIVE_ENDIAN
,
400 .valid
.min_access_size
= 4,
401 .valid
.max_access_size
= 4,
404 static const VMStateDescription vmstate_bcm2835_property
= {
405 .name
= TYPE_BCM2835_PROPERTY
,
407 .minimum_version_id
= 1,
408 .fields
= (const VMStateField
[]) {
409 VMSTATE_MACADDR(macaddr
, BCM2835PropertyState
),
410 VMSTATE_UINT32(addr
, BCM2835PropertyState
),
411 VMSTATE_BOOL(pending
, BCM2835PropertyState
),
412 VMSTATE_END_OF_LIST()
416 static void bcm2835_property_init(Object
*obj
)
418 BCM2835PropertyState
*s
= BCM2835_PROPERTY(obj
);
420 memory_region_init_io(&s
->iomem
, OBJECT(s
), &bcm2835_property_ops
, s
,
421 TYPE_BCM2835_PROPERTY
, 0x10);
424 * bcm2835_property_ops call into bcm2835_mbox, which in-turn reads from
425 * iomem. As such, mark iomem as re-entracy safe.
427 s
->iomem
.disable_reentrancy_guard
= true;
429 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
430 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
433 static void bcm2835_property_reset(DeviceState
*dev
)
435 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
440 static void bcm2835_property_realize(DeviceState
*dev
, Error
**errp
)
442 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
445 obj
= object_property_get_link(OBJECT(dev
), "fb", &error_abort
);
446 s
->fbdev
= BCM2835_FB(obj
);
448 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &error_abort
);
449 s
->dma_mr
= MEMORY_REGION(obj
);
450 address_space_init(&s
->dma_as
, s
->dma_mr
, TYPE_BCM2835_PROPERTY
"-memory");
452 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
453 qemu_macaddr_default_if_unset(&s
->macaddr
);
455 bcm2835_property_reset(dev
);
458 static Property bcm2835_property_props
[] = {
459 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState
, board_rev
, 0),
460 DEFINE_PROP_STRING("command-line", BCM2835PropertyState
, command_line
),
461 DEFINE_PROP_END_OF_LIST()
464 static void bcm2835_property_class_init(ObjectClass
*klass
, void *data
)
466 DeviceClass
*dc
= DEVICE_CLASS(klass
);
468 device_class_set_props(dc
, bcm2835_property_props
);
469 dc
->realize
= bcm2835_property_realize
;
470 dc
->vmsd
= &vmstate_bcm2835_property
;
473 static const TypeInfo bcm2835_property_info
= {
474 .name
= TYPE_BCM2835_PROPERTY
,
475 .parent
= TYPE_SYS_BUS_DEVICE
,
476 .instance_size
= sizeof(BCM2835PropertyState
),
477 .class_init
= bcm2835_property_class_init
,
478 .instance_init
= bcm2835_property_init
,
481 static void bcm2835_property_register_types(void)
483 type_register_static(&bcm2835_property_info
);
486 type_init(bcm2835_property_register_types
)