target-i386: Add API to get note's size
[qemu/ar7.git] / hw / spapr_vio.c
blob315ab8091ccc200b8b0991fef6085ff5447782ec
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.h"
24 #include "boards.h"
25 #include "monitor.h"
26 #include "loader.h"
27 #include "elf.h"
28 #include "hw/sysbus.h"
29 #include "kvm.h"
30 #include "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 */
42 /* #define DEBUG_TCE */
44 #ifdef DEBUG_SPAPR
45 #define dprintf(fmt, ...) \
46 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
47 #else
48 #define dprintf(fmt, ...) \
49 do { } while (0)
50 #endif
52 static struct BusInfo spapr_vio_bus_info = {
53 .name = "spapr-vio",
54 .size = sizeof(VIOsPAPRBus),
55 .props = (Property[]) {
56 DEFINE_PROP_UINT32("irq", VIOsPAPRDevice, vio_irq_num, 0), \
57 DEFINE_PROP_END_OF_LIST(),
61 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
63 DeviceState *qdev;
64 VIOsPAPRDevice *dev = NULL;
66 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
67 dev = (VIOsPAPRDevice *)qdev;
68 if (dev->reg == reg) {
69 return dev;
73 return NULL;
76 static char *vio_format_dev_name(VIOsPAPRDevice *dev)
78 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
79 char *name;
81 /* Device tree style name device@reg */
82 if (asprintf(&name, "%s@%x", pc->dt_name, dev->reg) < 0) {
83 return NULL;
86 return name;
89 #ifdef CONFIG_FDT
90 static int vio_make_devnode(VIOsPAPRDevice *dev,
91 void *fdt)
93 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
94 int vdevice_off, node_off, ret;
95 char *dt_name;
97 vdevice_off = fdt_path_offset(fdt, "/vdevice");
98 if (vdevice_off < 0) {
99 return vdevice_off;
102 dt_name = vio_format_dev_name(dev);
103 if (!dt_name) {
104 return -ENOMEM;
107 node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
108 free(dt_name);
109 if (node_off < 0) {
110 return node_off;
113 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
114 if (ret < 0) {
115 return ret;
118 if (pc->dt_type) {
119 ret = fdt_setprop_string(fdt, node_off, "device_type",
120 pc->dt_type);
121 if (ret < 0) {
122 return ret;
126 if (pc->dt_compatible) {
127 ret = fdt_setprop_string(fdt, node_off, "compatible",
128 pc->dt_compatible);
129 if (ret < 0) {
130 return ret;
134 if (dev->qirq) {
135 uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0};
137 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
138 sizeof(ints_prop));
139 if (ret < 0) {
140 return ret;
144 if (dev->rtce_window_size) {
145 uint32_t dma_prop[] = {cpu_to_be32(dev->reg),
146 0, 0,
147 0, cpu_to_be32(dev->rtce_window_size)};
149 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
150 if (ret < 0) {
151 return ret;
154 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
155 if (ret < 0) {
156 return ret;
159 ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop,
160 sizeof(dma_prop));
161 if (ret < 0) {
162 return ret;
166 if (pc->devnode) {
167 ret = (pc->devnode)(dev, fdt, node_off);
168 if (ret < 0) {
169 return ret;
173 return node_off;
175 #endif /* CONFIG_FDT */
178 * RTCE handling
181 static void rtce_init(VIOsPAPRDevice *dev)
183 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
184 * sizeof(VIOsPAPR_RTCE);
186 if (size) {
187 dev->rtce_table = kvmppc_create_spapr_tce(dev->reg,
188 dev->rtce_window_size,
189 &dev->kvmtce_fd);
191 if (!dev->rtce_table) {
192 dev->rtce_table = g_malloc0(size);
197 static target_ulong h_put_tce(CPUPPCState *env, sPAPREnvironment *spapr,
198 target_ulong opcode, target_ulong *args)
200 target_ulong liobn = args[0];
201 target_ulong ioba = args[1];
202 target_ulong tce = args[2];
203 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, liobn);
204 VIOsPAPR_RTCE *rtce;
206 if (!dev) {
207 hcall_dprintf("LIOBN 0x" TARGET_FMT_lx " does not exist\n", liobn);
208 return H_PARAMETER;
211 ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1);
213 #ifdef DEBUG_TCE
214 fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx
215 " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce);
216 #endif
218 if (ioba >= dev->rtce_window_size) {
219 hcall_dprintf("Out-of-bounds IOBA 0x" TARGET_FMT_lx "\n", ioba);
220 return H_PARAMETER;
223 rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT);
224 rtce->tce = tce;
226 return H_SUCCESS;
229 int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba,
230 target_ulong len, enum VIOsPAPR_TCEAccess access)
232 int start, end, i;
234 start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT;
235 end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT;
237 for (i = start; i <= end; i++) {
238 if ((dev->rtce_table[i].tce & access) != access) {
239 #ifdef DEBUG_TCE
240 fprintf(stderr, "FAIL on %d\n", i);
241 #endif
242 return -1;
246 return 0;
249 int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf,
250 uint32_t size)
252 #ifdef DEBUG_TCE
253 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
254 (unsigned long long)taddr, size);
255 #endif
257 /* Check for bypass */
258 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
259 cpu_physical_memory_write(taddr, buf, size);
260 return 0;
263 while (size) {
264 uint64_t tce;
265 uint32_t lsize;
266 uint64_t txaddr;
268 /* Check if we are in bound */
269 if (taddr >= dev->rtce_window_size) {
270 #ifdef DEBUG_TCE
271 fprintf(stderr, "spapr_tce_dma_write out of bounds\n");
272 #endif
273 return H_DEST_PARM;
275 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
277 /* How much til end of page ? */
278 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
280 /* Check TCE */
281 if (!(tce & 2)) {
282 return H_DEST_PARM;
285 /* Translate */
286 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
287 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
289 #ifdef DEBUG_TCE
290 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
291 (unsigned long long)txaddr, lsize);
292 #endif
294 /* Do it */
295 cpu_physical_memory_write(txaddr, buf, lsize);
296 buf += lsize;
297 taddr += lsize;
298 size -= lsize;
300 return 0;
303 int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size)
305 /* FIXME: allocating a temp buffer is nasty, but just stepping
306 * through writing zeroes is awkward. This will do for now. */
307 uint8_t zeroes[size];
309 #ifdef DEBUG_TCE
310 fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
311 (unsigned long long)taddr, size);
312 #endif
314 memset(zeroes, 0, size);
315 return spapr_tce_dma_write(dev, taddr, zeroes, size);
318 void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val)
320 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
323 void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val)
325 val = tswap16(val);
326 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
330 void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val)
332 val = tswap32(val);
333 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
336 void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val)
338 val = tswap64(val);
339 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
342 int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf,
343 uint32_t size)
345 #ifdef DEBUG_TCE
346 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
347 (unsigned long long)taddr, size);
348 #endif
350 /* Check for bypass */
351 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
352 cpu_physical_memory_read(taddr, buf, size);
353 return 0;
356 while (size) {
357 uint64_t tce;
358 uint32_t lsize;
359 uint64_t txaddr;
361 /* Check if we are in bound */
362 if (taddr >= dev->rtce_window_size) {
363 #ifdef DEBUG_TCE
364 fprintf(stderr, "spapr_tce_dma_read out of bounds\n");
365 #endif
366 return H_DEST_PARM;
368 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
370 /* How much til end of page ? */
371 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
373 /* Check TCE */
374 if (!(tce & 1)) {
375 return H_DEST_PARM;
378 /* Translate */
379 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
380 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
382 #ifdef DEBUG_TCE
383 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
384 (unsigned long long)txaddr, lsize);
385 #endif
386 /* Do it */
387 cpu_physical_memory_read(txaddr, buf, lsize);
388 buf += lsize;
389 taddr += lsize;
390 size -= lsize;
392 return H_SUCCESS;
395 uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr)
397 uint64_t val;
399 spapr_tce_dma_read(dev, taddr, &val, sizeof(val));
400 return tswap64(val);
404 * CRQ handling
406 static target_ulong h_reg_crq(CPUPPCState *env, sPAPREnvironment *spapr,
407 target_ulong opcode, target_ulong *args)
409 target_ulong reg = args[0];
410 target_ulong queue_addr = args[1];
411 target_ulong queue_len = args[2];
412 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
414 if (!dev) {
415 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
416 return H_PARAMETER;
419 /* We can't grok a queue size bigger than 256M for now */
420 if (queue_len < 0x1000 || queue_len > 0x10000000) {
421 hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
422 ")\n", queue_len);
423 return H_PARAMETER;
426 /* Check queue alignment */
427 if (queue_addr & 0xfff) {
428 hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx ")\n", queue_addr);
429 return H_PARAMETER;
432 /* Check if device supports CRQs */
433 if (!dev->crq.SendFunc) {
434 hcall_dprintf("Device does not support CRQ\n");
435 return H_NOT_FOUND;
438 /* Already a queue ? */
439 if (dev->crq.qsize) {
440 hcall_dprintf("CRQ already registered\n");
441 return H_RESOURCE;
443 dev->crq.qladdr = queue_addr;
444 dev->crq.qsize = queue_len;
445 dev->crq.qnext = 0;
447 dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
448 TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
449 reg, queue_addr, queue_len);
450 return H_SUCCESS;
453 static target_ulong free_crq(VIOsPAPRDevice *dev)
455 dev->crq.qladdr = 0;
456 dev->crq.qsize = 0;
457 dev->crq.qnext = 0;
459 dprintf("CRQ for dev 0x%" PRIx32 " freed\n", dev->reg);
461 return H_SUCCESS;
464 static target_ulong h_free_crq(CPUPPCState *env, sPAPREnvironment *spapr,
465 target_ulong opcode, target_ulong *args)
467 target_ulong reg = args[0];
468 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
470 if (!dev) {
471 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
472 return H_PARAMETER;
475 return free_crq(dev);
478 static target_ulong h_send_crq(CPUPPCState *env, sPAPREnvironment *spapr,
479 target_ulong opcode, target_ulong *args)
481 target_ulong reg = args[0];
482 target_ulong msg_hi = args[1];
483 target_ulong msg_lo = args[2];
484 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
485 uint64_t crq_mangle[2];
487 if (!dev) {
488 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
489 return H_PARAMETER;
491 crq_mangle[0] = cpu_to_be64(msg_hi);
492 crq_mangle[1] = cpu_to_be64(msg_lo);
494 if (dev->crq.SendFunc) {
495 return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
498 return H_HARDWARE;
501 static target_ulong h_enable_crq(CPUPPCState *env, sPAPREnvironment *spapr,
502 target_ulong opcode, target_ulong *args)
504 target_ulong reg = args[0];
505 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
507 if (!dev) {
508 hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
509 return H_PARAMETER;
512 return 0;
515 /* Returns negative error, 0 success, or positive: queue full */
516 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
518 int rc;
519 uint8_t byte;
521 if (!dev->crq.qsize) {
522 fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
523 return -1;
526 /* Maybe do a fast path for KVM just writing to the pages */
527 rc = spapr_tce_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
528 if (rc) {
529 return rc;
531 if (byte != 0) {
532 return 1;
535 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
536 &crq[8], 8);
537 if (rc) {
538 return rc;
541 kvmppc_eieio();
543 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
544 if (rc) {
545 return rc;
548 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
550 if (dev->signal_state & 1) {
551 qemu_irq_pulse(dev->qirq);
554 return 0;
557 /* "quiesce" handling */
559 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
561 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
563 if (dev->rtce_table) {
564 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
565 * sizeof(VIOsPAPR_RTCE);
566 memset(dev->rtce_table, 0, size);
569 dev->crq.qladdr = 0;
570 dev->crq.qsize = 0;
571 dev->crq.qnext = 0;
574 static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token,
575 uint32_t nargs, target_ulong args,
576 uint32_t nret, target_ulong rets)
578 VIOsPAPRBus *bus = spapr->vio_bus;
579 VIOsPAPRDevice *dev;
580 uint32_t unit, enable;
582 if (nargs != 2) {
583 rtas_st(rets, 0, -3);
584 return;
586 unit = rtas_ld(args, 0);
587 enable = rtas_ld(args, 1);
588 dev = spapr_vio_find_by_reg(bus, unit);
589 if (!dev) {
590 rtas_st(rets, 0, -3);
591 return;
593 if (enable) {
594 dev->flags |= VIO_PAPR_FLAG_DMA_BYPASS;
595 } else {
596 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
599 rtas_st(rets, 0, 0);
602 static void rtas_quiesce(sPAPREnvironment *spapr, uint32_t token,
603 uint32_t nargs, target_ulong args,
604 uint32_t nret, target_ulong rets)
606 VIOsPAPRBus *bus = spapr->vio_bus;
607 DeviceState *qdev;
608 VIOsPAPRDevice *dev = NULL;
610 if (nargs != 0) {
611 rtas_st(rets, 0, -3);
612 return;
615 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
616 dev = (VIOsPAPRDevice *)qdev;
617 spapr_vio_quiesce_one(dev);
620 rtas_st(rets, 0, 0);
623 static VIOsPAPRDevice *reg_conflict(VIOsPAPRDevice *dev)
625 VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
626 DeviceState *qdev;
627 VIOsPAPRDevice *other;
630 * Check for a device other than the given one which is already
631 * using the requested address. We have to open code this because
632 * the given dev might already be in the list.
634 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
635 other = DO_UPCAST(VIOsPAPRDevice, qdev, qdev);
637 if (other != dev && other->reg == dev->reg) {
638 return other;
642 return 0;
645 static void spapr_vio_busdev_reset(DeviceState *qdev)
647 VIOsPAPRDevice *dev = DO_UPCAST(VIOsPAPRDevice, qdev, qdev);
648 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
650 if (dev->crq.qsize) {
651 free_crq(dev);
654 if (pc->reset) {
655 pc->reset(dev);
659 static int spapr_vio_busdev_init(DeviceState *qdev)
661 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
662 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
663 char *id;
665 if (dev->reg != -1) {
667 * Explicitly assigned address, just verify that no-one else
668 * is using it. other mechanism). We have to open code this
669 * rather than using spapr_vio_find_by_reg() because sdev
670 * itself is already in the list.
672 VIOsPAPRDevice *other = reg_conflict(dev);
674 if (other) {
675 fprintf(stderr, "vio: %s and %s devices conflict at address %#x\n",
676 object_get_typename(OBJECT(qdev)),
677 object_get_typename(OBJECT(&other->qdev)),
678 dev->reg);
679 return -1;
681 } else {
682 /* Need to assign an address */
683 VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
685 do {
686 dev->reg = bus->next_reg++;
687 } while (reg_conflict(dev));
690 /* Don't overwrite ids assigned on the command line */
691 if (!dev->qdev.id) {
692 id = vio_format_dev_name(dev);
693 if (!id) {
694 return -1;
696 dev->qdev.id = id;
699 dev->qirq = spapr_allocate_msi(dev->vio_irq_num, &dev->vio_irq_num);
700 if (!dev->qirq) {
701 return -1;
704 rtce_init(dev);
706 return pc->init(dev);
709 static target_ulong h_vio_signal(CPUPPCState *env, sPAPREnvironment *spapr,
710 target_ulong opcode,
711 target_ulong *args)
713 target_ulong reg = args[0];
714 target_ulong mode = args[1];
715 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
716 VIOsPAPRDeviceClass *pc;
718 if (!dev) {
719 return H_PARAMETER;
722 pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
724 if (mode & ~pc->signal_mask) {
725 return H_PARAMETER;
728 dev->signal_state = mode;
730 return H_SUCCESS;
733 VIOsPAPRBus *spapr_vio_bus_init(void)
735 VIOsPAPRBus *bus;
736 BusState *qbus;
737 DeviceState *dev;
739 /* Create bridge device */
740 dev = qdev_create(NULL, "spapr-vio-bridge");
741 qdev_init_nofail(dev);
743 /* Create bus on bridge device */
745 qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio");
746 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
747 bus->next_reg = 0x1000;
749 /* hcall-vio */
750 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
752 /* hcall-tce */
753 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
755 /* hcall-crq */
756 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
757 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
758 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
759 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
761 /* RTAS calls */
762 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass);
763 spapr_rtas_register("quiesce", rtas_quiesce);
765 return bus;
768 /* Represents sPAPR hcall VIO devices */
770 static int spapr_vio_bridge_init(SysBusDevice *dev)
772 /* nothing */
773 return 0;
776 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
778 DeviceClass *dc = DEVICE_CLASS(klass);
779 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
781 k->init = spapr_vio_bridge_init;
782 dc->no_user = 1;
785 static TypeInfo spapr_vio_bridge_info = {
786 .name = "spapr-vio-bridge",
787 .parent = TYPE_SYS_BUS_DEVICE,
788 .instance_size = sizeof(SysBusDevice),
789 .class_init = spapr_vio_bridge_class_init,
792 static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
794 DeviceClass *k = DEVICE_CLASS(klass);
795 k->init = spapr_vio_busdev_init;
796 k->reset = spapr_vio_busdev_reset;
797 k->bus_info = &spapr_vio_bus_info;
800 static TypeInfo spapr_vio_type_info = {
801 .name = TYPE_VIO_SPAPR_DEVICE,
802 .parent = TYPE_DEVICE,
803 .instance_size = sizeof(VIOsPAPRDevice),
804 .abstract = true,
805 .class_size = sizeof(VIOsPAPRDeviceClass),
806 .class_init = vio_spapr_device_class_init,
809 static void spapr_vio_register_types(void)
811 type_register_static(&spapr_vio_bridge_info);
812 type_register_static(&spapr_vio_type_info);
815 type_init(spapr_vio_register_types)
817 #ifdef CONFIG_FDT
818 static int compare_reg(const void *p1, const void *p2)
820 VIOsPAPRDevice const *dev1, *dev2;
822 dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
823 dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
825 if (dev1->reg < dev2->reg) {
826 return -1;
828 if (dev1->reg == dev2->reg) {
829 return 0;
832 /* dev1->reg > dev2->reg */
833 return 1;
836 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
838 DeviceState *qdev, **qdevs;
839 int i, num, ret = 0;
841 /* Count qdevs on the bus list */
842 num = 0;
843 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
844 num++;
847 /* Copy out into an array of pointers */
848 qdevs = g_malloc(sizeof(qdev) * num);
849 num = 0;
850 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
851 qdevs[num++] = qdev;
854 /* Sort the array */
855 qsort(qdevs, num, sizeof(qdev), compare_reg);
857 /* Hack alert. Give the devices to libfdt in reverse order, we happen
858 * to know that will mean they are in forward order in the tree. */
859 for (i = num - 1; i >= 0; i--) {
860 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
862 ret = vio_make_devnode(dev, fdt);
864 if (ret < 0) {
865 goto out;
869 ret = 0;
870 out:
871 free(qdevs);
873 return ret;
876 int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus)
878 VIOsPAPRDevice *dev;
879 char *name, *path;
880 int ret, offset;
882 dev = spapr_vty_get_default(bus);
883 if (!dev)
884 return 0;
886 offset = fdt_path_offset(fdt, "/chosen");
887 if (offset < 0) {
888 return offset;
891 name = vio_format_dev_name(dev);
892 if (!name) {
893 return -ENOMEM;
896 if (asprintf(&path, "/vdevice/%s", name) < 0) {
897 path = NULL;
898 ret = -ENOMEM;
899 goto out;
902 ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path);
903 out:
904 free(name);
905 free(path);
907 return ret;
909 #endif /* CONFIG_FDT */