xref: /qemu/hw/ppc/spapr_vio.c (revision 72ac97cd)
1 /*
2  * QEMU sPAPR VIO code
3  *
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>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21 
22 #include "hw/hw.h"
23 #include "sysemu/sysemu.h"
24 #include "hw/boards.h"
25 #include "monitor/monitor.h"
26 #include "hw/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"
32 
33 #include "hw/ppc/spapr.h"
34 #include "hw/ppc/spapr_vio.h"
35 #include "hw/ppc/xics.h"
36 
37 #include <libfdt.h>
38 
39 /* #define DEBUG_SPAPR */
40 
41 #ifdef DEBUG_SPAPR
42 #define DPRINTF(fmt, ...) \
43     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
44 #else
45 #define DPRINTF(fmt, ...) \
46     do { } while (0)
47 #endif
48 
49 static Property spapr_vio_props[] = {
50     DEFINE_PROP_UINT32("irq", VIOsPAPRDevice, irq, 0), \
51     DEFINE_PROP_END_OF_LIST(),
52 };
53 
54 static char *spapr_vio_get_dev_name(DeviceState *qdev)
55 {
56     VIOsPAPRDevice *dev = VIO_SPAPR_DEVICE(qdev);
57     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
58     char *name;
59 
60     /* Device tree style name device@reg */
61     name = g_strdup_printf("%s@%x", pc->dt_name, dev->reg);
62 
63     return name;
64 }
65 
66 static void spapr_vio_bus_class_init(ObjectClass *klass, void *data)
67 {
68     BusClass *k = BUS_CLASS(klass);
69 
70     k->get_dev_path = spapr_vio_get_dev_name;
71     k->get_fw_dev_path = spapr_vio_get_dev_name;
72 }
73 
74 static const TypeInfo spapr_vio_bus_info = {
75     .name = TYPE_SPAPR_VIO_BUS,
76     .parent = TYPE_BUS,
77     .class_init = spapr_vio_bus_class_init,
78     .instance_size = sizeof(VIOsPAPRBus),
79 };
80 
81 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
82 {
83     BusChild *kid;
84     VIOsPAPRDevice *dev = NULL;
85 
86     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
87         dev = (VIOsPAPRDevice *)kid->child;
88         if (dev->reg == reg) {
89             return dev;
90         }
91     }
92 
93     return NULL;
94 }
95 
96 static int vio_make_devnode(VIOsPAPRDevice *dev,
97                             void *fdt)
98 {
99     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
100     int vdevice_off, node_off, ret;
101     char *dt_name;
102 
103     vdevice_off = fdt_path_offset(fdt, "/vdevice");
104     if (vdevice_off < 0) {
105         return vdevice_off;
106     }
107 
108     dt_name = spapr_vio_get_dev_name(DEVICE(dev));
109     node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
110     g_free(dt_name);
111     if (node_off < 0) {
112         return node_off;
113     }
114 
115     ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
116     if (ret < 0) {
117         return ret;
118     }
119 
120     if (pc->dt_type) {
121         ret = fdt_setprop_string(fdt, node_off, "device_type",
122                                  pc->dt_type);
123         if (ret < 0) {
124             return ret;
125         }
126     }
127 
128     if (pc->dt_compatible) {
129         ret = fdt_setprop_string(fdt, node_off, "compatible",
130                                  pc->dt_compatible);
131         if (ret < 0) {
132             return ret;
133         }
134     }
135 
136     if (dev->irq) {
137         uint32_t ints_prop[] = {cpu_to_be32(dev->irq), 0};
138 
139         ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
140                           sizeof(ints_prop));
141         if (ret < 0) {
142             return ret;
143         }
144     }
145 
146     ret = spapr_tcet_dma_dt(fdt, node_off, "ibm,my-dma-window", dev->tcet);
147     if (ret < 0) {
148         return ret;
149     }
150 
151     if (pc->devnode) {
152         ret = (pc->devnode)(dev, fdt, node_off);
153         if (ret < 0) {
154             return ret;
155         }
156     }
157 
158     return node_off;
159 }
160 
161 /*
162  * CRQ handling
163  */
164 static target_ulong h_reg_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
165                               target_ulong opcode, target_ulong *args)
166 {
167     target_ulong reg = args[0];
168     target_ulong queue_addr = args[1];
169     target_ulong queue_len = args[2];
170     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
171 
172     if (!dev) {
173         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
174         return H_PARAMETER;
175     }
176 
177     /* We can't grok a queue size bigger than 256M for now */
178     if (queue_len < 0x1000 || queue_len > 0x10000000) {
179         hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
180                       ")\n", queue_len);
181         return H_PARAMETER;
182     }
183 
184     /* Check queue alignment */
185     if (queue_addr & 0xfff) {
186         hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx ")\n", queue_addr);
187         return H_PARAMETER;
188     }
189 
190     /* Check if device supports CRQs */
191     if (!dev->crq.SendFunc) {
192         hcall_dprintf("Device does not support CRQ\n");
193         return H_NOT_FOUND;
194     }
195 
196     /* Already a queue ? */
197     if (dev->crq.qsize) {
198         hcall_dprintf("CRQ already registered\n");
199         return H_RESOURCE;
200     }
201     dev->crq.qladdr = queue_addr;
202     dev->crq.qsize = queue_len;
203     dev->crq.qnext = 0;
204 
205     DPRINTF("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
206             TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
207             reg, queue_addr, queue_len);
208     return H_SUCCESS;
209 }
210 
211 static target_ulong free_crq(VIOsPAPRDevice *dev)
212 {
213     dev->crq.qladdr = 0;
214     dev->crq.qsize = 0;
215     dev->crq.qnext = 0;
216 
217     DPRINTF("CRQ for dev 0x%" PRIx32 " freed\n", dev->reg);
218 
219     return H_SUCCESS;
220 }
221 
222 static target_ulong h_free_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
223                                target_ulong opcode, target_ulong *args)
224 {
225     target_ulong reg = args[0];
226     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
227 
228     if (!dev) {
229         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
230         return H_PARAMETER;
231     }
232 
233     return free_crq(dev);
234 }
235 
236 static target_ulong h_send_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
237                                target_ulong opcode, target_ulong *args)
238 {
239     target_ulong reg = args[0];
240     target_ulong msg_hi = args[1];
241     target_ulong msg_lo = args[2];
242     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
243     uint64_t crq_mangle[2];
244 
245     if (!dev) {
246         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
247         return H_PARAMETER;
248     }
249     crq_mangle[0] = cpu_to_be64(msg_hi);
250     crq_mangle[1] = cpu_to_be64(msg_lo);
251 
252     if (dev->crq.SendFunc) {
253         return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
254     }
255 
256     return H_HARDWARE;
257 }
258 
259 static target_ulong h_enable_crq(PowerPCCPU *cpu, sPAPREnvironment *spapr,
260                                  target_ulong opcode, target_ulong *args)
261 {
262     target_ulong reg = args[0];
263     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
264 
265     if (!dev) {
266         hcall_dprintf("Unit 0x" TARGET_FMT_lx " does not exist\n", reg);
267         return H_PARAMETER;
268     }
269 
270     return 0;
271 }
272 
273 /* Returns negative error, 0 success, or positive: queue full */
274 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
275 {
276     int rc;
277     uint8_t byte;
278 
279     if (!dev->crq.qsize) {
280         fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
281         return -1;
282     }
283 
284     /* Maybe do a fast path for KVM just writing to the pages */
285     rc = spapr_vio_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
286     if (rc) {
287         return rc;
288     }
289     if (byte != 0) {
290         return 1;
291     }
292 
293     rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
294                              &crq[8], 8);
295     if (rc) {
296         return rc;
297     }
298 
299     kvmppc_eieio();
300 
301     rc = spapr_vio_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
302     if (rc) {
303         return rc;
304     }
305 
306     dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
307 
308     if (dev->signal_state & 1) {
309         qemu_irq_pulse(spapr_vio_qirq(dev));
310     }
311 
312     return 0;
313 }
314 
315 /* "quiesce" handling */
316 
317 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
318 {
319     if (dev->tcet) {
320         device_reset(DEVICE(dev->tcet));
321     }
322     free_crq(dev);
323 }
324 
325 static void rtas_set_tce_bypass(PowerPCCPU *cpu, sPAPREnvironment *spapr,
326                                 uint32_t token,
327                                 uint32_t nargs, target_ulong args,
328                                 uint32_t nret, target_ulong rets)
329 {
330     VIOsPAPRBus *bus = spapr->vio_bus;
331     VIOsPAPRDevice *dev;
332     uint32_t unit, enable;
333 
334     if (nargs != 2) {
335         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
336         return;
337     }
338     unit = rtas_ld(args, 0);
339     enable = rtas_ld(args, 1);
340     dev = spapr_vio_find_by_reg(bus, unit);
341     if (!dev) {
342         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
343         return;
344     }
345 
346     if (!dev->tcet) {
347         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
348         return;
349     }
350 
351     spapr_tce_set_bypass(dev->tcet, !!enable);
352 
353     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
354 }
355 
356 static void rtas_quiesce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
357                          uint32_t token,
358                          uint32_t nargs, target_ulong args,
359                          uint32_t nret, target_ulong rets)
360 {
361     VIOsPAPRBus *bus = spapr->vio_bus;
362     BusChild *kid;
363     VIOsPAPRDevice *dev = NULL;
364 
365     if (nargs != 0) {
366         rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
367         return;
368     }
369 
370     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
371         dev = (VIOsPAPRDevice *)kid->child;
372         spapr_vio_quiesce_one(dev);
373     }
374 
375     rtas_st(rets, 0, RTAS_OUT_SUCCESS);
376 }
377 
378 static VIOsPAPRDevice *reg_conflict(VIOsPAPRDevice *dev)
379 {
380     VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
381     BusChild *kid;
382     VIOsPAPRDevice *other;
383 
384     /*
385      * Check for a device other than the given one which is already
386      * using the requested address. We have to open code this because
387      * the given dev might already be in the list.
388      */
389     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
390         other = VIO_SPAPR_DEVICE(kid->child);
391 
392         if (other != dev && other->reg == dev->reg) {
393             return other;
394         }
395     }
396 
397     return 0;
398 }
399 
400 static void spapr_vio_busdev_reset(DeviceState *qdev)
401 {
402     VIOsPAPRDevice *dev = VIO_SPAPR_DEVICE(qdev);
403     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
404 
405     /* Shut down the request queue and TCEs if necessary */
406     spapr_vio_quiesce_one(dev);
407 
408     dev->signal_state = 0;
409 
410     if (pc->reset) {
411         pc->reset(dev);
412     }
413 }
414 
415 static int spapr_vio_busdev_init(DeviceState *qdev)
416 {
417     VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
418     VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
419     char *id;
420 
421     if (dev->reg != -1) {
422         /*
423          * Explicitly assigned address, just verify that no-one else
424          * is using it.  other mechanism). We have to open code this
425          * rather than using spapr_vio_find_by_reg() because sdev
426          * itself is already in the list.
427          */
428         VIOsPAPRDevice *other = reg_conflict(dev);
429 
430         if (other) {
431             fprintf(stderr, "vio: %s and %s devices conflict at address %#x\n",
432                     object_get_typename(OBJECT(qdev)),
433                     object_get_typename(OBJECT(&other->qdev)),
434                     dev->reg);
435             return -1;
436         }
437     } else {
438         /* Need to assign an address */
439         VIOsPAPRBus *bus = DO_UPCAST(VIOsPAPRBus, bus, dev->qdev.parent_bus);
440 
441         do {
442             dev->reg = bus->next_reg++;
443         } while (reg_conflict(dev));
444     }
445 
446     /* Don't overwrite ids assigned on the command line */
447     if (!dev->qdev.id) {
448         id = spapr_vio_get_dev_name(DEVICE(dev));
449         dev->qdev.id = id;
450     }
451 
452     dev->irq = spapr_allocate_msi(dev->irq);
453     if (!dev->irq) {
454         return -1;
455     }
456 
457     if (pc->rtce_window_size) {
458         uint32_t liobn = SPAPR_VIO_BASE_LIOBN | dev->reg;
459         dev->tcet = spapr_tce_new_table(qdev, liobn, pc->rtce_window_size);
460         address_space_init(&dev->as, spapr_tce_get_iommu(dev->tcet), qdev->id);
461     }
462 
463     return pc->init(dev);
464 }
465 
466 static target_ulong h_vio_signal(PowerPCCPU *cpu, sPAPREnvironment *spapr,
467                                  target_ulong opcode,
468                                  target_ulong *args)
469 {
470     target_ulong reg = args[0];
471     target_ulong mode = args[1];
472     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
473     VIOsPAPRDeviceClass *pc;
474 
475     if (!dev) {
476         return H_PARAMETER;
477     }
478 
479     pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
480 
481     if (mode & ~pc->signal_mask) {
482         return H_PARAMETER;
483     }
484 
485     dev->signal_state = mode;
486 
487     return H_SUCCESS;
488 }
489 
490 VIOsPAPRBus *spapr_vio_bus_init(void)
491 {
492     VIOsPAPRBus *bus;
493     BusState *qbus;
494     DeviceState *dev;
495 
496     /* Create bridge device */
497     dev = qdev_create(NULL, "spapr-vio-bridge");
498     qdev_init_nofail(dev);
499 
500     /* Create bus on bridge device */
501 
502     qbus = qbus_create(TYPE_SPAPR_VIO_BUS, dev, "spapr-vio");
503     bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
504     bus->next_reg = 0x71000000;
505 
506     /* hcall-vio */
507     spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
508 
509     /* hcall-crq */
510     spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
511     spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
512     spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
513     spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
514 
515     /* RTAS calls */
516     spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass);
517     spapr_rtas_register("quiesce", rtas_quiesce);
518 
519     return bus;
520 }
521 
522 /* Represents sPAPR hcall VIO devices */
523 
524 static int spapr_vio_bridge_init(SysBusDevice *dev)
525 {
526     /* nothing */
527     return 0;
528 }
529 
530 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
531 {
532     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
533     DeviceClass *dc = DEVICE_CLASS(klass);
534 
535     dc->fw_name = "vdevice";
536     k->init = spapr_vio_bridge_init;
537 }
538 
539 static const TypeInfo spapr_vio_bridge_info = {
540     .name          = "spapr-vio-bridge",
541     .parent        = TYPE_SYS_BUS_DEVICE,
542     .instance_size = sizeof(SysBusDevice),
543     .class_init    = spapr_vio_bridge_class_init,
544 };
545 
546 const VMStateDescription vmstate_spapr_vio = {
547     .name = "spapr_vio",
548     .version_id = 1,
549     .minimum_version_id = 1,
550     .fields = (VMStateField[]) {
551         /* Sanity check */
552         VMSTATE_UINT32_EQUAL(reg, VIOsPAPRDevice),
553         VMSTATE_UINT32_EQUAL(irq, VIOsPAPRDevice),
554 
555         /* General VIO device state */
556         VMSTATE_UINTTL(signal_state, VIOsPAPRDevice),
557         VMSTATE_UINT64(crq.qladdr, VIOsPAPRDevice),
558         VMSTATE_UINT32(crq.qsize, VIOsPAPRDevice),
559         VMSTATE_UINT32(crq.qnext, VIOsPAPRDevice),
560 
561         VMSTATE_END_OF_LIST()
562     },
563 };
564 
565 static void vio_spapr_device_class_init(ObjectClass *klass, void *data)
566 {
567     DeviceClass *k = DEVICE_CLASS(klass);
568     k->init = spapr_vio_busdev_init;
569     k->reset = spapr_vio_busdev_reset;
570     k->bus_type = TYPE_SPAPR_VIO_BUS;
571     k->props = spapr_vio_props;
572 }
573 
574 static const TypeInfo spapr_vio_type_info = {
575     .name = TYPE_VIO_SPAPR_DEVICE,
576     .parent = TYPE_DEVICE,
577     .instance_size = sizeof(VIOsPAPRDevice),
578     .abstract = true,
579     .class_size = sizeof(VIOsPAPRDeviceClass),
580     .class_init = vio_spapr_device_class_init,
581 };
582 
583 static void spapr_vio_register_types(void)
584 {
585     type_register_static(&spapr_vio_bus_info);
586     type_register_static(&spapr_vio_bridge_info);
587     type_register_static(&spapr_vio_type_info);
588 }
589 
590 type_init(spapr_vio_register_types)
591 
592 static int compare_reg(const void *p1, const void *p2)
593 {
594     VIOsPAPRDevice const *dev1, *dev2;
595 
596     dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
597     dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
598 
599     if (dev1->reg < dev2->reg) {
600         return -1;
601     }
602     if (dev1->reg == dev2->reg) {
603         return 0;
604     }
605 
606     /* dev1->reg > dev2->reg */
607     return 1;
608 }
609 
610 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
611 {
612     DeviceState *qdev, **qdevs;
613     BusChild *kid;
614     int i, num, ret = 0;
615 
616     /* Count qdevs on the bus list */
617     num = 0;
618     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
619         num++;
620     }
621 
622     /* Copy out into an array of pointers */
623     qdevs = g_malloc(sizeof(qdev) * num);
624     num = 0;
625     QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
626         qdevs[num++] = kid->child;
627     }
628 
629     /* Sort the array */
630     qsort(qdevs, num, sizeof(qdev), compare_reg);
631 
632     /* Hack alert. Give the devices to libfdt in reverse order, we happen
633      * to know that will mean they are in forward order in the tree. */
634     for (i = num - 1; i >= 0; i--) {
635         VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
636 
637         ret = vio_make_devnode(dev, fdt);
638 
639         if (ret < 0) {
640             goto out;
641         }
642     }
643 
644     ret = 0;
645 out:
646     free(qdevs);
647 
648     return ret;
649 }
650 
651 int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus)
652 {
653     VIOsPAPRDevice *dev;
654     char *name, *path;
655     int ret, offset;
656 
657     dev = spapr_vty_get_default(bus);
658     if (!dev)
659         return 0;
660 
661     offset = fdt_path_offset(fdt, "/chosen");
662     if (offset < 0) {
663         return offset;
664     }
665 
666     name = spapr_vio_get_dev_name(DEVICE(dev));
667     path = g_strdup_printf("/vdevice/%s", name);
668 
669     ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path);
670 
671     g_free(name);
672     g_free(path);
673 
674     return ret;
675 }
676