Merge remote-tracking branch 'stefanha/trivial-patches' into staging
[qemu.git] / hw / spapr_vio.c
blob35818e18f1e0a16d1b41d9ff42fc375336db10c2
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 QLIST_FOREACH(qdev, &bus->bus.children, sibling) {
67 dev = (VIOsPAPRDevice *)qdev;
68 if (dev->reg == reg) {
69 break;
73 return dev;
76 #ifdef CONFIG_FDT
77 static int vio_make_devnode(VIOsPAPRDevice *dev,
78 void *fdt)
80 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)dev->qdev.info;
81 int vdevice_off, node_off;
82 int ret;
84 vdevice_off = fdt_path_offset(fdt, "/vdevice");
85 if (vdevice_off < 0) {
86 return vdevice_off;
89 node_off = fdt_add_subnode(fdt, vdevice_off, dev->qdev.id);
90 if (node_off < 0) {
91 return node_off;
94 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
95 if (ret < 0) {
96 return ret;
99 if (info->dt_type) {
100 ret = fdt_setprop_string(fdt, node_off, "device_type",
101 info->dt_type);
102 if (ret < 0) {
103 return ret;
107 if (info->dt_compatible) {
108 ret = fdt_setprop_string(fdt, node_off, "compatible",
109 info->dt_compatible);
110 if (ret < 0) {
111 return ret;
115 if (dev->qirq) {
116 uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0};
118 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
119 sizeof(ints_prop));
120 if (ret < 0) {
121 return ret;
125 if (dev->rtce_window_size) {
126 uint32_t dma_prop[] = {cpu_to_be32(dev->reg),
127 0, 0,
128 0, cpu_to_be32(dev->rtce_window_size)};
130 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
131 if (ret < 0) {
132 return ret;
135 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
136 if (ret < 0) {
137 return ret;
140 ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop,
141 sizeof(dma_prop));
142 if (ret < 0) {
143 return ret;
147 if (info->devnode) {
148 ret = (info->devnode)(dev, fdt, node_off);
149 if (ret < 0) {
150 return ret;
154 return node_off;
156 #endif /* CONFIG_FDT */
159 * RTCE handling
162 static void rtce_init(VIOsPAPRDevice *dev)
164 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
165 * sizeof(VIOsPAPR_RTCE);
167 if (size) {
168 dev->rtce_table = g_malloc0(size);
172 static target_ulong h_put_tce(CPUState *env, sPAPREnvironment *spapr,
173 target_ulong opcode, target_ulong *args)
175 target_ulong liobn = args[0];
176 target_ulong ioba = args[1];
177 target_ulong tce = args[2];
178 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, liobn);
179 VIOsPAPR_RTCE *rtce;
181 if (!dev) {
182 hcall_dprintf("spapr_vio_put_tce on non-existent LIOBN "
183 TARGET_FMT_lx "\n", liobn);
184 return H_PARAMETER;
187 ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1);
189 #ifdef DEBUG_TCE
190 fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx
191 " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce);
192 #endif
194 if (ioba >= dev->rtce_window_size) {
195 hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
196 TARGET_FMT_lx "\n", ioba);
197 return H_PARAMETER;
200 rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT);
201 rtce->tce = tce;
203 return H_SUCCESS;
206 int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba,
207 target_ulong len, enum VIOsPAPR_TCEAccess access)
209 int start, end, i;
211 start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT;
212 end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT;
214 for (i = start; i <= end; i++) {
215 if ((dev->rtce_table[i].tce & access) != access) {
216 #ifdef DEBUG_TCE
217 fprintf(stderr, "FAIL on %d\n", i);
218 #endif
219 return -1;
223 return 0;
226 int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf,
227 uint32_t size)
229 #ifdef DEBUG_TCE
230 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
231 (unsigned long long)taddr, size);
232 #endif
234 /* Check for bypass */
235 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
236 cpu_physical_memory_write(taddr, buf, size);
237 return 0;
240 while (size) {
241 uint64_t tce;
242 uint32_t lsize;
243 uint64_t txaddr;
245 /* Check if we are in bound */
246 if (taddr >= dev->rtce_window_size) {
247 #ifdef DEBUG_TCE
248 fprintf(stderr, "spapr_tce_dma_write out of bounds\n");
249 #endif
250 return H_DEST_PARM;
252 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
254 /* How much til end of page ? */
255 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
257 /* Check TCE */
258 if (!(tce & 2)) {
259 return H_DEST_PARM;
262 /* Translate */
263 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
264 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
266 #ifdef DEBUG_TCE
267 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
268 (unsigned long long)txaddr, lsize);
269 #endif
271 /* Do it */
272 cpu_physical_memory_write(txaddr, buf, lsize);
273 buf += lsize;
274 taddr += lsize;
275 size -= lsize;
277 return 0;
280 int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size)
282 /* FIXME: allocating a temp buffer is nasty, but just stepping
283 * through writing zeroes is awkward. This will do for now. */
284 uint8_t zeroes[size];
286 #ifdef DEBUG_TCE
287 fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
288 (unsigned long long)taddr, size);
289 #endif
291 memset(zeroes, 0, size);
292 return spapr_tce_dma_write(dev, taddr, zeroes, size);
295 void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val)
297 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
300 void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val)
302 val = tswap16(val);
303 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
307 void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val)
309 val = tswap32(val);
310 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
313 void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val)
315 val = tswap64(val);
316 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
319 int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf,
320 uint32_t size)
322 #ifdef DEBUG_TCE
323 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
324 (unsigned long long)taddr, size);
325 #endif
327 /* Check for bypass */
328 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
329 cpu_physical_memory_read(taddr, buf, size);
330 return 0;
333 while (size) {
334 uint64_t tce;
335 uint32_t lsize;
336 uint64_t txaddr;
338 /* Check if we are in bound */
339 if (taddr >= dev->rtce_window_size) {
340 #ifdef DEBUG_TCE
341 fprintf(stderr, "spapr_tce_dma_read out of bounds\n");
342 #endif
343 return H_DEST_PARM;
345 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
347 /* How much til end of page ? */
348 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
350 /* Check TCE */
351 if (!(tce & 1)) {
352 return H_DEST_PARM;
355 /* Translate */
356 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
357 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
359 #ifdef DEBUG_TCE
360 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
361 (unsigned long long)txaddr, lsize);
362 #endif
363 /* Do it */
364 cpu_physical_memory_read(txaddr, buf, lsize);
365 buf += lsize;
366 taddr += lsize;
367 size -= lsize;
369 return H_SUCCESS;
372 uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr)
374 uint64_t val;
376 spapr_tce_dma_read(dev, taddr, &val, sizeof(val));
377 return tswap64(val);
381 * CRQ handling
383 static target_ulong h_reg_crq(CPUState *env, sPAPREnvironment *spapr,
384 target_ulong opcode, target_ulong *args)
386 target_ulong reg = args[0];
387 target_ulong queue_addr = args[1];
388 target_ulong queue_len = args[2];
389 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
391 if (!dev) {
392 hcall_dprintf("h_reg_crq on non-existent unit 0x"
393 TARGET_FMT_lx "\n", reg);
394 return H_PARAMETER;
397 /* We can't grok a queue size bigger than 256M for now */
398 if (queue_len < 0x1000 || queue_len > 0x10000000) {
399 hcall_dprintf("h_reg_crq, queue size too small or too big (0x%llx)\n",
400 (unsigned long long)queue_len);
401 return H_PARAMETER;
404 /* Check queue alignment */
405 if (queue_addr & 0xfff) {
406 hcall_dprintf("h_reg_crq, queue not aligned (0x%llx)\n",
407 (unsigned long long)queue_addr);
408 return H_PARAMETER;
411 /* Check if device supports CRQs */
412 if (!dev->crq.SendFunc) {
413 return H_NOT_FOUND;
417 /* Already a queue ? */
418 if (dev->crq.qsize) {
419 return H_RESOURCE;
421 dev->crq.qladdr = queue_addr;
422 dev->crq.qsize = queue_len;
423 dev->crq.qnext = 0;
425 dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
426 TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
427 reg, queue_addr, queue_len);
428 return H_SUCCESS;
431 static target_ulong h_free_crq(CPUState *env, sPAPREnvironment *spapr,
432 target_ulong opcode, target_ulong *args)
434 target_ulong reg = args[0];
435 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
437 if (!dev) {
438 hcall_dprintf("h_free_crq on non-existent unit 0x"
439 TARGET_FMT_lx "\n", reg);
440 return H_PARAMETER;
443 dev->crq.qladdr = 0;
444 dev->crq.qsize = 0;
445 dev->crq.qnext = 0;
447 dprintf("CRQ for dev 0x" TARGET_FMT_lx " freed\n", reg);
449 return H_SUCCESS;
452 static target_ulong h_send_crq(CPUState *env, sPAPREnvironment *spapr,
453 target_ulong opcode, target_ulong *args)
455 target_ulong reg = args[0];
456 target_ulong msg_hi = args[1];
457 target_ulong msg_lo = args[2];
458 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
459 uint64_t crq_mangle[2];
461 if (!dev) {
462 hcall_dprintf("h_send_crq on non-existent unit 0x"
463 TARGET_FMT_lx "\n", reg);
464 return H_PARAMETER;
466 crq_mangle[0] = cpu_to_be64(msg_hi);
467 crq_mangle[1] = cpu_to_be64(msg_lo);
469 if (dev->crq.SendFunc) {
470 return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
473 return H_HARDWARE;
476 static target_ulong h_enable_crq(CPUState *env, sPAPREnvironment *spapr,
477 target_ulong opcode, target_ulong *args)
479 target_ulong reg = args[0];
480 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
482 if (!dev) {
483 hcall_dprintf("h_enable_crq on non-existent unit 0x"
484 TARGET_FMT_lx "\n", reg);
485 return H_PARAMETER;
488 return 0;
491 /* Returns negative error, 0 success, or positive: queue full */
492 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
494 int rc;
495 uint8_t byte;
497 if (!dev->crq.qsize) {
498 fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
499 return -1;
502 /* Maybe do a fast path for KVM just writing to the pages */
503 rc = spapr_tce_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
504 if (rc) {
505 return rc;
507 if (byte != 0) {
508 return 1;
511 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
512 &crq[8], 8);
513 if (rc) {
514 return rc;
517 kvmppc_eieio();
519 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
520 if (rc) {
521 return rc;
524 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
526 if (dev->signal_state & 1) {
527 qemu_irq_pulse(dev->qirq);
530 return 0;
533 /* "quiesce" handling */
535 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
537 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
539 if (dev->rtce_table) {
540 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
541 * sizeof(VIOsPAPR_RTCE);
542 memset(dev->rtce_table, 0, size);
545 dev->crq.qladdr = 0;
546 dev->crq.qsize = 0;
547 dev->crq.qnext = 0;
550 static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token,
551 uint32_t nargs, target_ulong args,
552 uint32_t nret, target_ulong rets)
554 VIOsPAPRBus *bus = spapr->vio_bus;
555 VIOsPAPRDevice *dev;
556 uint32_t unit, enable;
558 if (nargs != 2) {
559 rtas_st(rets, 0, -3);
560 return;
562 unit = rtas_ld(args, 0);
563 enable = rtas_ld(args, 1);
564 dev = spapr_vio_find_by_reg(bus, unit);
565 if (!dev) {
566 rtas_st(rets, 0, -3);
567 return;
569 if (enable) {
570 dev->flags |= VIO_PAPR_FLAG_DMA_BYPASS;
571 } else {
572 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
575 rtas_st(rets, 0, 0);
578 static void rtas_quiesce(sPAPREnvironment *spapr, uint32_t token,
579 uint32_t nargs, target_ulong args,
580 uint32_t nret, target_ulong rets)
582 VIOsPAPRBus *bus = spapr->vio_bus;
583 DeviceState *qdev;
584 VIOsPAPRDevice *dev = NULL;
586 if (nargs != 0) {
587 rtas_st(rets, 0, -3);
588 return;
591 QLIST_FOREACH(qdev, &bus->bus.children, sibling) {
592 dev = (VIOsPAPRDevice *)qdev;
593 spapr_vio_quiesce_one(dev);
596 rtas_st(rets, 0, 0);
599 static int spapr_vio_busdev_init(DeviceState *qdev, DeviceInfo *qinfo)
601 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)qinfo;
602 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
603 char *id;
605 if (asprintf(&id, "%s@%x", info->dt_name, dev->reg) < 0) {
606 return -1;
609 dev->qdev.id = id;
611 dev->qirq = spapr_allocate_irq(dev->vio_irq_num, &dev->vio_irq_num);
612 if (!dev->qirq) {
613 return -1;
616 rtce_init(dev);
618 return info->init(dev);
621 void spapr_vio_bus_register_withprop(VIOsPAPRDeviceInfo *info)
623 info->qdev.init = spapr_vio_busdev_init;
624 info->qdev.bus_info = &spapr_vio_bus_info;
626 assert(info->qdev.size >= sizeof(VIOsPAPRDevice));
627 qdev_register(&info->qdev);
630 static target_ulong h_vio_signal(CPUState *env, sPAPREnvironment *spapr,
631 target_ulong opcode,
632 target_ulong *args)
634 target_ulong reg = args[0];
635 target_ulong mode = args[1];
636 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
637 VIOsPAPRDeviceInfo *info;
639 if (!dev) {
640 return H_PARAMETER;
643 info = (VIOsPAPRDeviceInfo *)dev->qdev.info;
645 if (mode & ~info->signal_mask) {
646 return H_PARAMETER;
649 dev->signal_state = mode;
651 return H_SUCCESS;
654 VIOsPAPRBus *spapr_vio_bus_init(void)
656 VIOsPAPRBus *bus;
657 BusState *qbus;
658 DeviceState *dev;
659 DeviceInfo *qinfo;
661 /* Create bridge device */
662 dev = qdev_create(NULL, "spapr-vio-bridge");
663 qdev_init_nofail(dev);
665 /* Create bus on bridge device */
667 qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio");
668 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
670 /* hcall-vio */
671 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
673 /* hcall-tce */
674 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
676 /* hcall-crq */
677 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
678 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
679 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
680 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
682 /* RTAS calls */
683 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass);
684 spapr_rtas_register("quiesce", rtas_quiesce);
686 for (qinfo = device_info_list; qinfo; qinfo = qinfo->next) {
687 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)qinfo;
689 if (qinfo->bus_info != &spapr_vio_bus_info) {
690 continue;
693 if (info->hcalls) {
694 info->hcalls(bus);
698 return bus;
701 /* Represents sPAPR hcall VIO devices */
703 static int spapr_vio_bridge_init(SysBusDevice *dev)
705 /* nothing */
706 return 0;
709 static SysBusDeviceInfo spapr_vio_bridge_info = {
710 .init = spapr_vio_bridge_init,
711 .qdev.name = "spapr-vio-bridge",
712 .qdev.size = sizeof(SysBusDevice),
713 .qdev.no_user = 1,
716 static void spapr_vio_register_devices(void)
718 sysbus_register_withprop(&spapr_vio_bridge_info);
721 device_init(spapr_vio_register_devices)
723 #ifdef CONFIG_FDT
724 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
726 DeviceState *qdev;
727 int ret = 0;
729 QLIST_FOREACH(qdev, &bus->bus.children, sibling) {
730 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
732 ret = vio_make_devnode(dev, fdt);
734 if (ret < 0) {
735 return ret;
739 return 0;
741 #endif /* CONFIG_FDT */