Replace all setjmp()/longjmp() with sigsetjmp()/siglongjmp()
[qemu/pbrook.git] / hw / spapr_vio.c
blob34c9ca6b65e0a85da172c70757afe5b81a170e0e
1 /*
2 * QEMU sPAPR VIO code
4 * Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
5 * Based on the s390 virtio bus code:
6 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "hw.h"
23 #include "sysemu/sysemu.h"
24 #include "boards.h"
25 #include "monitor/monitor.h"
26 #include "loader.h"
27 #include "elf.h"
28 #include "hw/sysbus.h"
29 #include "sysemu/kvm.h"
30 #include "sysemu/device_tree.h"
31 #include "kvm_ppc.h"
33 #include "hw/spapr.h"
34 #include "hw/spapr_vio.h"
35 #include "hw/xics.h"
37 #ifdef CONFIG_FDT
38 #include <libfdt.h>
39 #endif /* CONFIG_FDT */
41 /* #define DEBUG_SPAPR */
43 #ifdef DEBUG_SPAPR
44 #define dprintf(fmt, ...) \
45 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
46 #else
47 #define dprintf(fmt, ...) \
48 do { } while (0)
49 #endif
51 static Property spapr_vio_props[] = {
52 DEFINE_PROP_UINT32("irq", VIOsPAPRDevice, irq, 0), \
53 DEFINE_PROP_END_OF_LIST(),
56 static const TypeInfo spapr_vio_bus_info = {
57 .name = TYPE_SPAPR_VIO_BUS,
58 .parent = TYPE_BUS,
59 .instance_size = sizeof(VIOsPAPRBus),
62 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
64 BusChild *kid;
65 VIOsPAPRDevice *dev = NULL;
67 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
68 dev = (VIOsPAPRDevice *)kid->child;
69 if (dev->reg == reg) {
70 return dev;
74 return NULL;
77 static char *vio_format_dev_name(VIOsPAPRDevice *dev)
79 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
80 char *name;
82 /* Device tree style name device@reg */
83 name = g_strdup_printf("%s@%x", pc->dt_name, dev->reg);
85 return name;
88 #ifdef CONFIG_FDT
89 static int vio_make_devnode(VIOsPAPRDevice *dev,
90 void *fdt)
92 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
93 int vdevice_off, node_off, ret;
94 char *dt_name;
96 vdevice_off = fdt_path_offset(fdt, "/vdevice");
97 if (vdevice_off < 0) {
98 return vdevice_off;
101 dt_name = vio_format_dev_name(dev);
102 node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
103 g_free(dt_name);
104 if (node_off < 0) {
105 return node_off;
108 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
109 if (ret < 0) {
110 return ret;
113 if (pc->dt_type) {
114 ret = fdt_setprop_string(fdt, node_off, "device_type",
115 pc->dt_type);
116 if (ret < 0) {
117 return ret;
121 if (pc->dt_compatible) {
122 ret = fdt_setprop_string(fdt, node_off, "compatible",
123 pc->dt_compatible);
124 if (ret < 0) {
125 return ret;
129 if (dev->irq) {
130 uint32_t ints_prop[] = {cpu_to_be32(dev->irq), 0};
132 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
133 sizeof(ints_prop));
134 if (ret < 0) {
135 return ret;
139 ret = spapr_tcet_dma_dt(fdt, node_off, "ibm,my-dma-window", dev->dma);
140 if (ret < 0) {
141 return ret;
144 if (pc->devnode) {
145 ret = (pc->devnode)(dev, fdt, node_off);
146 if (ret < 0) {
147 return ret;
151 return node_off;
153 #endif /* CONFIG_FDT */
156 * CRQ handling
158 static target_ulong h_reg_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
159 target_ulong opcode, target_ulong *args)
161 target_ulong reg = args[0];
162 target_ulong queue_addr = args[1];
163 target_ulong queue_len = args[2];
164 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
166 if (!dev) {
167 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
168 return H_PARAMETER;
171 /* We can't grok a queue size bigger than 256M for now */
172 if (queue_len < 0x1000 || queue_len > 0x10000000) {
173 hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
174 ")\n", queue_len);
175 return H_PARAMETER;
178 /* Check queue alignment */
179 if (queue_addr & 0xfff) {
180 hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx ")\n", queue_addr);
181 return H_PARAMETER;
184 /* Check if device supports CRQs */
185 if (!dev->crq.SendFunc) {
186 hcall_dprintf("Device does not support CRQ\n");
187 return H_NOT_FOUND;
190 /* Already a queue ? */
191 if (dev->crq.qsize) {
192 hcall_dprintf("CRQ already registered\n");
193 return H_RESOURCE;
195 dev->crq.qladdr = queue_addr;
196 dev->crq.qsize = queue_len;
197 dev->crq.qnext = 0;
199 dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
200 TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
201 reg, queue_addr, queue_len);
202 return H_SUCCESS;
205 static target_ulong free_crq(VIOsPAPRDevice *dev)
207 dev->crq.qladdr = 0;
208 dev->crq.qsize = 0;
209 dev->crq.qnext = 0;
211 dprintf("CRQ for dev 0x%" PRIx32 " freed\n", dev->reg);
213 return H_SUCCESS;
216 static target_ulong h_free_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
217 target_ulong opcode, target_ulong *args)
219 target_ulong reg = args[0];
220 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
222 if (!dev) {
223 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
224 return H_PARAMETER;
227 return free_crq(dev);
230 static target_ulong h_send_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
231 target_ulong opcode, target_ulong *args)
233 target_ulong reg = args[0];
234 target_ulong msg_hi = args[1];
235 target_ulong msg_lo = args[2];
236 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
237 uint64_t crq_mangle[2];
239 if (!dev) {
240 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
241 return H_PARAMETER;
243 crq_mangle[0] = cpu_to_be64(msg_hi);
244 crq_mangle[1] = cpu_to_be64(msg_lo);
246 if (dev->crq.SendFunc) {
247 return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
250 return H_HARDWARE;
253 static target_ulong h_enable_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
254 target_ulong opcode, target_ulong *args)
256 target_ulong reg = args[0];
257 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
259 if (!dev) {
260 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
261 return H_PARAMETER;
264 return 0;
267 /* Returns negative error, 0 success, or positive: queue full */
268 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
270 int rc;
271 uint8_t byte;
273 if (!dev->crq.qsize) {
274 fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
275 return -1;
278 /* Maybe do a fast path for KVM just writing to the pages */
279 rc = spapr_vio_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
280 if (rc) {
281 return rc;
283 if (byte != 0) {
284 return 1;
287 rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
288 &crq[8], 8);
289 if (rc) {
290 return rc;
293 kvmppc_eieio();
295 rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
296 if (rc) {
297 return rc;
300 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
302 if (dev->signal_state & 1) {
303 qemu_irq_pulse(spapr_vio_qirq(dev));
306 return 0;
309 /* "quiesce" handling */
311 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
313 if (dev->dma) {
314 spapr_tce_reset(dev->dma);
316 free_crq(dev);
319 static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token,
320 uint32_t nargs, target_ulong args,
321 uint32_t nret, target_ulong rets)
323 VIOsPAPRBus *bus = spapr->vio_bus;
324 VIOsPAPRDevice *dev;
325 uint32_t unit, enable;
327 if (nargs != 2) {
328 rtas_st(rets, 0, -3);
329 return;
331 unit = rtas_ld(args, 0);
332 enable = rtas_ld(args, 1);
333 dev = spapr_vio_find_by_reg(bus, unit);
334 if (!dev) {
335 rtas_st(rets, 0, -3);
336 return;
339 if (!dev->dma) {
340 rtas_st(rets, 0, -3);
341 return;
344 spapr_tce_set_bypass(dev->dma, !!enable);
346 rtas_st(rets, 0, 0);
349 static void rtas_quiesce(sPAPREnvironment *spapr, uint32_t token,
350 uint32_t nargs, target_ulong args,
351 uint32_t nret, target_ulong rets)
353 VIOsPAPRBus *bus = spapr->vio_bus;
354 BusChild *kid;
355 VIOsPAPRDevice *dev = NULL;
357 if (nargs != 0) {
358 rtas_st(rets, 0, -3);
359 return;
362 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
363 dev = (VIOsPAPRDevice *)kid->child;
364 spapr_vio_quiesce_one(dev);
367 rtas_st(rets, 0, 0);
370 static VIOsPAPRDevice *reg_conflict(VIOsPAPRDevice *dev)
372 VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
373 BusChild *kid;
374 VIOsPAPRDevice *other;
377 * Check for a device other than the given one which is already
378 * using the requested address. We have to open code this because
379 * the given dev might already be in the list.
381 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
382 other = DO_UPCAST(VIOsPAPRDevice, qdev, kid->child);
384 if (other != dev && other->reg == dev->reg) {
385 return other;
389 return 0;
392 static void spapr_vio_busdev_reset(DeviceState *qdev)
394 VIOsPAPRDevice *dev = DO_UPCAST(VIOsPAPRDevice, qdev, qdev);
395 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
397 /* Shut down the request queue and TCEs if necessary */
398 spapr_vio_quiesce_one(dev);
400 dev->signal_state = 0;
402 if (pc->reset) {
403 pc->reset(dev);
407 static int spapr_vio_busdev_init(DeviceState *qdev)
409 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
410 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
411 char *id;
413 if (dev->reg != -1) {
415 * Explicitly assigned address, just verify that no-one else
416 * is using it. other mechanism). We have to open code this
417 * rather than using spapr_vio_find_by_reg() because sdev
418 * itself is already in the list.
420 VIOsPAPRDevice *other = reg_conflict(dev);
422 if (other) {
423 fprintf(stderr, "vio: %s and %s devices conflict at address %#x\n",
424 object_get_typename(OBJECT(qdev)),
425 object_get_typename(OBJECT(&other->qdev)),
426 dev->reg);
427 return -1;
429 } else {
430 /* Need to assign an address */
431 VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
433 do {
434 dev->reg = bus->next_reg++;
435 } while (reg_conflict(dev));
438 /* Don't overwrite ids assigned on the command line */
439 if (!dev->qdev.id) {
440 id = vio_format_dev_name(dev);
441 dev->qdev.id = id;
444 dev->irq = spapr_allocate_msi(dev->irq);
445 if (!dev->irq) {
446 return -1;
449 if (pc->rtce_window_size) {
450 uint32_t liobn = SPAPR_VIO_BASE_LIOBN | dev->reg;
451 dev->dma = spapr_tce_new_dma_context(liobn, pc->rtce_window_size);
454 return pc->init(dev);
457 static target_ulong h_vio_signal(PowerPCCPU *cpu, sPAPREnvironment *spapr,
458 target_ulong opcode,
459 target_ulong *args)
461 target_ulong reg = args[0];
462 target_ulong mode = args[1];
463 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
464 VIOsPAPRDeviceClass *pc;
466 if (!dev) {
467 return H_PARAMETER;
470 pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
472 if (mode & ~pc->signal_mask) {
473 return H_PARAMETER;
476 dev->signal_state = mode;
478 return H_SUCCESS;
481 VIOsPAPRBus *spapr_vio_bus_init(void)
483 VIOsPAPRBus *bus;
484 BusState *qbus;
485 DeviceState *dev;
487 /* Create bridge device */
488 dev = qdev_create(NULL, "spapr-vio-bridge");
489 qdev_init_nofail(dev);
491 /* Create bus on bridge device */
493 qbus = qbus_create(TYPE_SPAPR_VIO_BUS, dev, "spapr-vio");
494 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
495 bus->next_reg = 0x71000000;
497 /* hcall-vio */
498 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
500 /* hcall-crq */
501 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
502 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
503 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
504 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
506 /* RTAS calls */
507 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass);
508 spapr_rtas_register("quiesce", rtas_quiesce);
510 return bus;
513 /* Represents sPAPR hcall VIO devices */
515 static int spapr_vio_bridge_init(SysBusDevice *dev)
517 /* nothing */
518 return 0;
521 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
523 DeviceClass *dc = DEVICE_CLASS(klass);
524 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
526 k->init = spapr_vio_bridge_init;
527 dc->no_user = 1;
530 static const TypeInfo spapr_vio_bridge_info = {
531 .name = "spapr-vio-bridge",
532 .parent = TYPE_SYS_BUS_DEVICE,
533 .instance_size = sizeof(SysBusDevice),
534 .class_init = spapr_vio_bridge_class_init,
537 static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
539 DeviceClass *k = DEVICE_CLASS(klass);
540 k->init = spapr_vio_busdev_init;
541 k->reset = spapr_vio_busdev_reset;
542 k->bus_type = TYPE_SPAPR_VIO_BUS;
543 k->props = spapr_vio_props;
546 static const TypeInfo spapr_vio_type_info = {
547 .name = TYPE_VIO_SPAPR_DEVICE,
548 .parent = TYPE_DEVICE,
549 .instance_size = sizeof(VIOsPAPRDevice),
550 .abstract = true,
551 .class_size = sizeof(VIOsPAPRDeviceClass),
552 .class_init = vio_spapr_device_class_init,
555 static void spapr_vio_register_types(void)
557 type_register_static(&spapr_vio_bus_info);
558 type_register_static(&spapr_vio_bridge_info);
559 type_register_static(&spapr_vio_type_info);
562 type_init(spapr_vio_register_types)
564 #ifdef CONFIG_FDT
565 static int compare_reg(const void *p1, const void *p2)
567 VIOsPAPRDevice const *dev1, *dev2;
569 dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
570 dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
572 if (dev1->reg < dev2->reg) {
573 return -1;
575 if (dev1->reg == dev2->reg) {
576 return 0;
579 /* dev1->reg > dev2->reg */
580 return 1;
583 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
585 DeviceState *qdev, **qdevs;
586 BusChild *kid;
587 int i, num, ret = 0;
589 /* Count qdevs on the bus list */
590 num = 0;
591 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
592 num++;
595 /* Copy out into an array of pointers */
596 qdevs = g_malloc(sizeof(qdev) * num);
597 num = 0;
598 QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
599 qdevs[num++] = kid->child;
602 /* Sort the array */
603 qsort(qdevs, num, sizeof(qdev), compare_reg);
605 /* Hack alert. Give the devices to libfdt in reverse order, we happen
606 * to know that will mean they are in forward order in the tree. */
607 for (i = num - 1; i >= 0; i--) {
608 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
610 ret = vio_make_devnode(dev, fdt);
612 if (ret < 0) {
613 goto out;
617 ret = 0;
618 out:
619 free(qdevs);
621 return ret;
624 int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus)
626 VIOsPAPRDevice *dev;
627 char *name, *path;
628 int ret, offset;
630 dev = spapr_vty_get_default(bus);
631 if (!dev)
632 return 0;
634 offset = fdt_path_offset(fdt, "/chosen");
635 if (offset < 0) {
636 return offset;
639 name = vio_format_dev_name(dev);
640 path = g_strdup_printf("/vdevice/%s", name);
642 ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path);
644 g_free(name);
645 g_free(path);
647 return ret;
649 #endif /* CONFIG_FDT */