remove bogus exec perms
[qemu/ar7.git] / hw / misc / bcm2835_property.c
blob8219b9bd6820f7a80375ddc568f859caf8b4da87
1 /*
2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
4 */
6 #include "qemu/osdep.h"
7 #include "hw/misc/bcm2835_property.h"
8 #include "hw/misc/bcm2835_mbox_defs.h"
9 #include "sysemu/dma.h"
11 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
13 static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value)
15 uint32_t tag;
16 uint32_t bufsize;
17 uint32_t tot_len;
18 int n;
19 size_t resplen;
20 uint32_t offset, length, color;
21 uint32_t tmp;
22 uint32_t xres, yres, xoffset, yoffset, bpp, pixo, alpha;
23 uint32_t *newxres = NULL, *newyres = NULL, *newxoffset = NULL,
24 *newyoffset = NULL, *newbpp = NULL, *newpixo = NULL, *newalpha = NULL;
26 value &= ~0xf;
28 s->addr = value;
30 tot_len = ldl_phys(&s->dma_as, value);
32 /* @(addr + 4) : Buffer response code */
33 value = s->addr + 8;
34 while (value + 8 <= s->addr + tot_len) {
35 tag = ldl_phys(&s->dma_as, value);
36 bufsize = ldl_phys(&s->dma_as, value + 4);
37 /* @(value + 8) : Request/response indicator */
38 resplen = 0;
39 switch (tag) {
40 case 0x00000000: /* End tag */
41 break;
42 case 0x00000001: /* Get firmware revision */
43 stl_phys(&s->dma_as, value + 12, 346337);
44 resplen = 4;
45 break;
46 case 0x00010001: /* Get board model */
47 qemu_log_mask(LOG_UNIMP,
48 "bcm2835_property: %x get board model NYI\n", tag);
49 resplen = 4;
50 break;
51 case 0x00010002: /* Get board revision */
52 stl_phys(&s->dma_as, value + 12, s->board_rev);
53 resplen = 4;
54 break;
55 case 0x00010003: /* Get board MAC address */
56 resplen = sizeof(s->macaddr.a);
57 dma_memory_write(&s->dma_as, value + 12, s->macaddr.a, resplen);
58 break;
59 case 0x00010004: /* Get board serial */
60 qemu_log_mask(LOG_UNIMP,
61 "bcm2835_property: %x get board serial NYI\n", tag);
62 resplen = 8;
63 break;
64 case 0x00010005: /* Get ARM memory */
65 /* base */
66 stl_phys(&s->dma_as, value + 12, 0);
67 /* size */
68 stl_phys(&s->dma_as, value + 16, s->fbdev->vcram_base);
69 resplen = 8;
70 break;
71 case 0x00010006: /* Get VC memory */
72 /* base */
73 stl_phys(&s->dma_as, value + 12, s->fbdev->vcram_base);
74 /* size */
75 stl_phys(&s->dma_as, value + 16, s->fbdev->vcram_size);
76 resplen = 8;
77 break;
78 case 0x00028001: /* Set power state */
79 /* Assume that whatever device they asked for exists,
80 * and we'll just claim we set it to the desired state
82 tmp = ldl_phys(&s->dma_as, value + 16);
83 stl_phys(&s->dma_as, value + 16, (tmp & 1));
84 resplen = 8;
85 break;
87 /* Clocks */
89 case 0x00030001: /* Get clock state */
90 stl_phys(&s->dma_as, value + 16, 0x1);
91 resplen = 8;
92 break;
94 case 0x00038001: /* Set clock state */
95 qemu_log_mask(LOG_UNIMP,
96 "bcm2835_property: %x set clock state NYI\n", tag);
97 resplen = 8;
98 break;
100 case 0x00030002: /* Get clock rate */
101 case 0x00030004: /* Get max clock rate */
102 case 0x00030007: /* Get min clock rate */
103 switch (ldl_phys(&s->dma_as, value + 12)) {
104 case 1: /* EMMC */
105 stl_phys(&s->dma_as, value + 16, 50000000);
106 break;
107 case 2: /* UART */
108 stl_phys(&s->dma_as, value + 16, 3000000);
109 break;
110 default:
111 stl_phys(&s->dma_as, value + 16, 700000000);
112 break;
114 resplen = 8;
115 break;
117 case 0x00038002: /* Set clock rate */
118 case 0x00038004: /* Set max clock rate */
119 case 0x00038007: /* Set min clock rate */
120 qemu_log_mask(LOG_UNIMP,
121 "bcm2835_property: %x set clock rates NYI\n", tag);
122 resplen = 8;
123 break;
125 /* Temperature */
127 case 0x00030006: /* Get temperature */
128 stl_phys(&s->dma_as, value + 16, 25000);
129 resplen = 8;
130 break;
132 case 0x0003000A: /* Get max temperature */
133 stl_phys(&s->dma_as, value + 16, 99000);
134 resplen = 8;
135 break;
138 /* Frame buffer */
140 case 0x00040001: /* Allocate buffer */
141 stl_phys(&s->dma_as, value + 12, s->fbdev->base);
142 stl_phys(&s->dma_as, value + 16, s->fbdev->size);
143 resplen = 8;
144 break;
145 case 0x00048001: /* Release buffer */
146 resplen = 0;
147 break;
148 case 0x00040002: /* Blank screen */
149 resplen = 4;
150 break;
151 case 0x00040003: /* Get display width/height */
152 case 0x00040004:
153 stl_phys(&s->dma_as, value + 12, s->fbdev->xres);
154 stl_phys(&s->dma_as, value + 16, s->fbdev->yres);
155 resplen = 8;
156 break;
157 case 0x00044003: /* Test display width/height */
158 case 0x00044004:
159 resplen = 8;
160 break;
161 case 0x00048003: /* Set display width/height */
162 case 0x00048004:
163 xres = ldl_phys(&s->dma_as, value + 12);
164 newxres = &xres;
165 yres = ldl_phys(&s->dma_as, value + 16);
166 newyres = &yres;
167 resplen = 8;
168 break;
169 case 0x00040005: /* Get depth */
170 stl_phys(&s->dma_as, value + 12, s->fbdev->bpp);
171 resplen = 4;
172 break;
173 case 0x00044005: /* Test depth */
174 resplen = 4;
175 break;
176 case 0x00048005: /* Set depth */
177 bpp = ldl_phys(&s->dma_as, value + 12);
178 newbpp = &bpp;
179 resplen = 4;
180 break;
181 case 0x00040006: /* Get pixel order */
182 stl_phys(&s->dma_as, value + 12, s->fbdev->pixo);
183 resplen = 4;
184 break;
185 case 0x00044006: /* Test pixel order */
186 resplen = 4;
187 break;
188 case 0x00048006: /* Set pixel order */
189 pixo = ldl_phys(&s->dma_as, value + 12);
190 newpixo = &pixo;
191 resplen = 4;
192 break;
193 case 0x00040007: /* Get alpha */
194 stl_phys(&s->dma_as, value + 12, s->fbdev->alpha);
195 resplen = 4;
196 break;
197 case 0x00044007: /* Test pixel alpha */
198 resplen = 4;
199 break;
200 case 0x00048007: /* Set alpha */
201 alpha = ldl_phys(&s->dma_as, value + 12);
202 newalpha = &alpha;
203 resplen = 4;
204 break;
205 case 0x00040008: /* Get pitch */
206 stl_phys(&s->dma_as, value + 12, s->fbdev->pitch);
207 resplen = 4;
208 break;
209 case 0x00040009: /* Get virtual offset */
210 stl_phys(&s->dma_as, value + 12, s->fbdev->xoffset);
211 stl_phys(&s->dma_as, value + 16, s->fbdev->yoffset);
212 resplen = 8;
213 break;
214 case 0x00044009: /* Test virtual offset */
215 resplen = 8;
216 break;
217 case 0x00048009: /* Set virtual offset */
218 xoffset = ldl_phys(&s->dma_as, value + 12);
219 newxoffset = &xoffset;
220 yoffset = ldl_phys(&s->dma_as, value + 16);
221 newyoffset = &yoffset;
223 stl_phys(&s->dma_as, value + 12, bcm2835_fb.xres);
224 stl_phys(&s->dma_as, value + 16, bcm2835_fb.yres);
226 resplen = 8;
227 break;
228 case 0x0004000a: /* Get/Test/Set overscan */
229 case 0x0004400a:
230 case 0x0004800a:
231 stl_phys(&s->dma_as, value + 12, 0);
232 stl_phys(&s->dma_as, value + 16, 0);
233 stl_phys(&s->dma_as, value + 20, 0);
234 stl_phys(&s->dma_as, value + 24, 0);
235 resplen = 16;
236 break;
238 case 0x0004800b: /* Set palette */
239 offset = ldl_phys(&s->dma_as, value + 12);
240 length = ldl_phys(&s->dma_as, value + 16);
241 n = 0;
242 while (n < length - offset) {
243 color = ldl_phys(&s->dma_as, value + 20 + (n << 2));
244 stl_phys(&s->dma_as,
245 s->fbdev->vcram_base + ((offset + n) << 2), color);
246 n++;
248 stl_phys(&s->dma_as, value + 12, 0);
249 resplen = 4;
250 break;
252 case 0x00060001: /* Get DMA channels */
253 /* channels 2-5 */
254 stl_phys(&s->dma_as, value + 12, 0x003C);
255 resplen = 4;
256 break;
258 case 0x00050001: /* Get command line */
259 resplen = 0;
260 break;
262 default:
263 qemu_log_mask(LOG_GUEST_ERROR,
264 "bcm2835_property: unhandled tag %08x\n", tag);
265 break;
268 if (tag == 0) {
269 break;
272 stl_phys(&s->dma_as, value + 8, (1 << 31) | resplen);
273 value += bufsize + 12;
276 if (newxres || newyres || newxoffset || newyoffset || newbpp || newpixo
277 || newalpha) {
278 bcm2835_fb_reconfigure(s->fbdev, newxres, newyres, newxoffset,
279 newyoffset, newbpp, newpixo, newalpha);
282 /* Buffer response code */
283 stl_phys(&s->dma_as, s->addr + 4, (1 << 31));
286 static uint64_t bcm2835_property_read(void *opaque, hwaddr offset,
287 unsigned size)
289 BCM2835PropertyState *s = opaque;
290 uint32_t res = 0;
292 switch (offset) {
293 case MBOX_AS_DATA:
294 res = MBOX_CHAN_PROPERTY | s->addr;
295 s->pending = false;
296 qemu_set_irq(s->mbox_irq, 0);
297 break;
299 case MBOX_AS_PENDING:
300 res = s->pending;
301 break;
303 default:
304 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
305 __func__, offset);
306 return 0;
309 return res;
312 static void bcm2835_property_write(void *opaque, hwaddr offset,
313 uint64_t value, unsigned size)
315 BCM2835PropertyState *s = opaque;
317 switch (offset) {
318 case MBOX_AS_DATA:
319 /* bcm2835_mbox should check our pending status before pushing */
320 assert(!s->pending);
321 s->pending = true;
322 bcm2835_property_mbox_push(s, value);
323 qemu_set_irq(s->mbox_irq, 1);
324 break;
326 default:
327 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
328 __func__, offset);
329 return;
333 static const MemoryRegionOps bcm2835_property_ops = {
334 .read = bcm2835_property_read,
335 .write = bcm2835_property_write,
336 .endianness = DEVICE_NATIVE_ENDIAN,
337 .valid.min_access_size = 4,
338 .valid.max_access_size = 4,
341 static const VMStateDescription vmstate_bcm2835_property = {
342 .name = TYPE_BCM2835_PROPERTY,
343 .version_id = 1,
344 .minimum_version_id = 1,
345 .fields = (VMStateField[]) {
346 VMSTATE_MACADDR(macaddr, BCM2835PropertyState),
347 VMSTATE_UINT32(addr, BCM2835PropertyState),
348 VMSTATE_BOOL(pending, BCM2835PropertyState),
349 VMSTATE_END_OF_LIST()
353 static void bcm2835_property_init(Object *obj)
355 BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
357 memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s,
358 TYPE_BCM2835_PROPERTY, 0x10);
359 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
360 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
363 static void bcm2835_property_reset(DeviceState *dev)
365 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
367 s->pending = false;
370 static void bcm2835_property_realize(DeviceState *dev, Error **errp)
372 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
373 Object *obj;
374 Error *err = NULL;
376 obj = object_property_get_link(OBJECT(dev), "fb", &err);
377 if (obj == NULL) {
378 error_setg(errp, "%s: required fb link not found: %s",
379 __func__, error_get_pretty(err));
380 return;
383 s->fbdev = BCM2835_FB(obj);
385 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
386 if (obj == NULL) {
387 error_setg(errp, "%s: required dma-mr link not found: %s",
388 __func__, error_get_pretty(err));
389 return;
392 s->dma_mr = MEMORY_REGION(obj);
393 address_space_init(&s->dma_as, s->dma_mr, NULL);
395 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
396 qemu_macaddr_default_if_unset(&s->macaddr);
398 bcm2835_property_reset(dev);
401 static Property bcm2835_property_props[] = {
402 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState, board_rev, 0),
403 DEFINE_PROP_END_OF_LIST()
406 static void bcm2835_property_class_init(ObjectClass *klass, void *data)
408 DeviceClass *dc = DEVICE_CLASS(klass);
410 dc->props = bcm2835_property_props;
411 dc->realize = bcm2835_property_realize;
412 dc->vmsd = &vmstate_bcm2835_property;
415 static TypeInfo bcm2835_property_info = {
416 .name = TYPE_BCM2835_PROPERTY,
417 .parent = TYPE_SYS_BUS_DEVICE,
418 .instance_size = sizeof(BCM2835PropertyState),
419 .class_init = bcm2835_property_class_init,
420 .instance_init = bcm2835_property_init,
423 static void bcm2835_property_register_types(void)
425 type_register_static(&bcm2835_property_info);
428 type_init(bcm2835_property_register_types)