Add access control support to qemu bridge helper
[qemu.git] / hw / spapr_vio.c
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.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"
32
33 #include "hw/spapr.h"
34 #include "hw/spapr_vio.h"
35 #include "hw/xics.h"
36
37 #ifdef CONFIG_FDT
38 #include <libfdt.h>
39 #endif /* CONFIG_FDT */
40
41 /* #define DEBUG_SPAPR */
42 /* #define DEBUG_TCE */
43
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
51
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(),
58 },
59 };
60
61 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
62 {
63 DeviceState *qdev;
64 VIOsPAPRDevice *dev = NULL;
65
66 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
67 dev = (VIOsPAPRDevice *)qdev;
68 if (dev->reg == reg) {
69 return dev;
70 }
71 }
72
73 return NULL;
74 }
75
76 static char *vio_format_dev_name(VIOsPAPRDevice *dev)
77 {
78 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
79 char *name;
80
81 /* Device tree style name device@reg */
82 if (asprintf(&name, "%s@%x", pc->dt_name, dev->reg) < 0) {
83 return NULL;
84 }
85
86 return name;
87 }
88
89 #ifdef CONFIG_FDT
90 static int vio_make_devnode(VIOsPAPRDevice *dev,
91 void *fdt)
92 {
93 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
94 int vdevice_off, node_off, ret;
95 char *dt_name;
96
97 vdevice_off = fdt_path_offset(fdt, "/vdevice");
98 if (vdevice_off < 0) {
99 return vdevice_off;
100 }
101
102 dt_name = vio_format_dev_name(dev);
103 if (!dt_name) {
104 return -ENOMEM;
105 }
106
107 node_off = fdt_add_subnode(fdt, vdevice_off, dt_name);
108 free(dt_name);
109 if (node_off < 0) {
110 return node_off;
111 }
112
113 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
114 if (ret < 0) {
115 return ret;
116 }
117
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;
123 }
124 }
125
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;
131 }
132 }
133
134 if (dev->qirq) {
135 uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0};
136
137 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
138 sizeof(ints_prop));
139 if (ret < 0) {
140 return ret;
141 }
142 }
143
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)};
148
149 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
150 if (ret < 0) {
151 return ret;
152 }
153
154 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
155 if (ret < 0) {
156 return ret;
157 }
158
159 ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop,
160 sizeof(dma_prop));
161 if (ret < 0) {
162 return ret;
163 }
164 }
165
166 if (pc->devnode) {
167 ret = (pc->devnode)(dev, fdt, node_off);
168 if (ret < 0) {
169 return ret;
170 }
171 }
172
173 return node_off;
174 }
175 #endif /* CONFIG_FDT */
176
177 /*
178 * RTCE handling
179 */
180
181 static void rtce_init(VIOsPAPRDevice *dev)
182 {
183 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
184 * sizeof(VIOsPAPR_RTCE);
185
186 if (size) {
187 dev->rtce_table = kvmppc_create_spapr_tce(dev->reg,
188 dev->rtce_window_size,
189 &dev->kvmtce_fd);
190
191 if (!dev->rtce_table) {
192 dev->rtce_table = g_malloc0(size);
193 }
194 }
195 }
196
197 static target_ulong h_put_tce(CPUState *env, sPAPREnvironment *spapr,
198 target_ulong opcode, target_ulong *args)
199 {
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;
205
206 if (!dev) {
207 hcall_dprintf("spapr_vio_put_tce on non-existent LIOBN "
208 TARGET_FMT_lx "\n", liobn);
209 return H_PARAMETER;
210 }
211
212 ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1);
213
214 #ifdef DEBUG_TCE
215 fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx
216 " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce);
217 #endif
218
219 if (ioba >= dev->rtce_window_size) {
220 hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
221 TARGET_FMT_lx "\n", ioba);
222 return H_PARAMETER;
223 }
224
225 rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT);
226 rtce->tce = tce;
227
228 return H_SUCCESS;
229 }
230
231 int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba,
232 target_ulong len, enum VIOsPAPR_TCEAccess access)
233 {
234 int start, end, i;
235
236 start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT;
237 end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT;
238
239 for (i = start; i <= end; i++) {
240 if ((dev->rtce_table[i].tce & access) != access) {
241 #ifdef DEBUG_TCE
242 fprintf(stderr, "FAIL on %d\n", i);
243 #endif
244 return -1;
245 }
246 }
247
248 return 0;
249 }
250
251 int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf,
252 uint32_t size)
253 {
254 #ifdef DEBUG_TCE
255 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
256 (unsigned long long)taddr, size);
257 #endif
258
259 /* Check for bypass */
260 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
261 cpu_physical_memory_write(taddr, buf, size);
262 return 0;
263 }
264
265 while (size) {
266 uint64_t tce;
267 uint32_t lsize;
268 uint64_t txaddr;
269
270 /* Check if we are in bound */
271 if (taddr >= dev->rtce_window_size) {
272 #ifdef DEBUG_TCE
273 fprintf(stderr, "spapr_tce_dma_write out of bounds\n");
274 #endif
275 return H_DEST_PARM;
276 }
277 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
278
279 /* How much til end of page ? */
280 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
281
282 /* Check TCE */
283 if (!(tce & 2)) {
284 return H_DEST_PARM;
285 }
286
287 /* Translate */
288 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
289 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
290
291 #ifdef DEBUG_TCE
292 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
293 (unsigned long long)txaddr, lsize);
294 #endif
295
296 /* Do it */
297 cpu_physical_memory_write(txaddr, buf, lsize);
298 buf += lsize;
299 taddr += lsize;
300 size -= lsize;
301 }
302 return 0;
303 }
304
305 int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size)
306 {
307 /* FIXME: allocating a temp buffer is nasty, but just stepping
308 * through writing zeroes is awkward. This will do for now. */
309 uint8_t zeroes[size];
310
311 #ifdef DEBUG_TCE
312 fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
313 (unsigned long long)taddr, size);
314 #endif
315
316 memset(zeroes, 0, size);
317 return spapr_tce_dma_write(dev, taddr, zeroes, size);
318 }
319
320 void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val)
321 {
322 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
323 }
324
325 void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val)
326 {
327 val = tswap16(val);
328 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
329 }
330
331
332 void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val)
333 {
334 val = tswap32(val);
335 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
336 }
337
338 void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val)
339 {
340 val = tswap64(val);
341 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
342 }
343
344 int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf,
345 uint32_t size)
346 {
347 #ifdef DEBUG_TCE
348 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
349 (unsigned long long)taddr, size);
350 #endif
351
352 /* Check for bypass */
353 if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) {
354 cpu_physical_memory_read(taddr, buf, size);
355 return 0;
356 }
357
358 while (size) {
359 uint64_t tce;
360 uint32_t lsize;
361 uint64_t txaddr;
362
363 /* Check if we are in bound */
364 if (taddr >= dev->rtce_window_size) {
365 #ifdef DEBUG_TCE
366 fprintf(stderr, "spapr_tce_dma_read out of bounds\n");
367 #endif
368 return H_DEST_PARM;
369 }
370 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
371
372 /* How much til end of page ? */
373 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
374
375 /* Check TCE */
376 if (!(tce & 1)) {
377 return H_DEST_PARM;
378 }
379
380 /* Translate */
381 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
382 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
383
384 #ifdef DEBUG_TCE
385 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
386 (unsigned long long)txaddr, lsize);
387 #endif
388 /* Do it */
389 cpu_physical_memory_read(txaddr, buf, lsize);
390 buf += lsize;
391 taddr += lsize;
392 size -= lsize;
393 }
394 return H_SUCCESS;
395 }
396
397 uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr)
398 {
399 uint64_t val;
400
401 spapr_tce_dma_read(dev, taddr, &val, sizeof(val));
402 return tswap64(val);
403 }
404
405 /*
406 * CRQ handling
407 */
408 static target_ulong h_reg_crq(CPUState *env, sPAPREnvironment *spapr,
409 target_ulong opcode, target_ulong *args)
410 {
411 target_ulong reg = args[0];
412 target_ulong queue_addr = args[1];
413 target_ulong queue_len = args[2];
414 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
415
416 if (!dev) {
417 hcall_dprintf("h_reg_crq on non-existent unit 0x"
418 TARGET_FMT_lx "\n", reg);
419 return H_PARAMETER;
420 }
421
422 /* We can't grok a queue size bigger than 256M for now */
423 if (queue_len < 0x1000 || queue_len > 0x10000000) {
424 hcall_dprintf("h_reg_crq, queue size too small or too big (0x%llx)\n",
425 (unsigned long long)queue_len);
426 return H_PARAMETER;
427 }
428
429 /* Check queue alignment */
430 if (queue_addr & 0xfff) {
431 hcall_dprintf("h_reg_crq, queue not aligned (0x%llx)\n",
432 (unsigned long long)queue_addr);
433 return H_PARAMETER;
434 }
435
436 /* Check if device supports CRQs */
437 if (!dev->crq.SendFunc) {
438 return H_NOT_FOUND;
439 }
440
441
442 /* Already a queue ? */
443 if (dev->crq.qsize) {
444 return H_RESOURCE;
445 }
446 dev->crq.qladdr = queue_addr;
447 dev->crq.qsize = queue_len;
448 dev->crq.qnext = 0;
449
450 dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
451 TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
452 reg, queue_addr, queue_len);
453 return H_SUCCESS;
454 }
455
456 static target_ulong h_free_crq(CPUState *env, sPAPREnvironment *spapr,
457 target_ulong opcode, target_ulong *args)
458 {
459 target_ulong reg = args[0];
460 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
461
462 if (!dev) {
463 hcall_dprintf("h_free_crq on non-existent unit 0x"
464 TARGET_FMT_lx "\n", reg);
465 return H_PARAMETER;
466 }
467
468 dev->crq.qladdr = 0;
469 dev->crq.qsize = 0;
470 dev->crq.qnext = 0;
471
472 dprintf("CRQ for dev 0x" TARGET_FMT_lx " freed\n", reg);
473
474 return H_SUCCESS;
475 }
476
477 static target_ulong h_send_crq(CPUState *env, sPAPREnvironment *spapr,
478 target_ulong opcode, target_ulong *args)
479 {
480 target_ulong reg = args[0];
481 target_ulong msg_hi = args[1];
482 target_ulong msg_lo = args[2];
483 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
484 uint64_t crq_mangle[2];
485
486 if (!dev) {
487 hcall_dprintf("h_send_crq on non-existent unit 0x"
488 TARGET_FMT_lx "\n", reg);
489 return H_PARAMETER;
490 }
491 crq_mangle[0] = cpu_to_be64(msg_hi);
492 crq_mangle[1] = cpu_to_be64(msg_lo);
493
494 if (dev->crq.SendFunc) {
495 return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
496 }
497
498 return H_HARDWARE;
499 }
500
501 static target_ulong h_enable_crq(CPUState *env, sPAPREnvironment *spapr,
502 target_ulong opcode, target_ulong *args)
503 {
504 target_ulong reg = args[0];
505 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
506
507 if (!dev) {
508 hcall_dprintf("h_enable_crq on non-existent unit 0x"
509 TARGET_FMT_lx "\n", reg);
510 return H_PARAMETER;
511 }
512
513 return 0;
514 }
515
516 /* Returns negative error, 0 success, or positive: queue full */
517 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
518 {
519 int rc;
520 uint8_t byte;
521
522 if (!dev->crq.qsize) {
523 fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
524 return -1;
525 }
526
527 /* Maybe do a fast path for KVM just writing to the pages */
528 rc = spapr_tce_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
529 if (rc) {
530 return rc;
531 }
532 if (byte != 0) {
533 return 1;
534 }
535
536 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
537 &crq[8], 8);
538 if (rc) {
539 return rc;
540 }
541
542 kvmppc_eieio();
543
544 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
545 if (rc) {
546 return rc;
547 }
548
549 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
550
551 if (dev->signal_state & 1) {
552 qemu_irq_pulse(dev->qirq);
553 }
554
555 return 0;
556 }
557
558 /* "quiesce" handling */
559
560 static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev)
561 {
562 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
563
564 if (dev->rtce_table) {
565 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
566 * sizeof(VIOsPAPR_RTCE);
567 memset(dev->rtce_table, 0, size);
568 }
569
570 dev->crq.qladdr = 0;
571 dev->crq.qsize = 0;
572 dev->crq.qnext = 0;
573 }
574
575 static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token,
576 uint32_t nargs, target_ulong args,
577 uint32_t nret, target_ulong rets)
578 {
579 VIOsPAPRBus *bus = spapr->vio_bus;
580 VIOsPAPRDevice *dev;
581 uint32_t unit, enable;
582
583 if (nargs != 2) {
584 rtas_st(rets, 0, -3);
585 return;
586 }
587 unit = rtas_ld(args, 0);
588 enable = rtas_ld(args, 1);
589 dev = spapr_vio_find_by_reg(bus, unit);
590 if (!dev) {
591 rtas_st(rets, 0, -3);
592 return;
593 }
594 if (enable) {
595 dev->flags |= VIO_PAPR_FLAG_DMA_BYPASS;
596 } else {
597 dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS;
598 }
599
600 rtas_st(rets, 0, 0);
601 }
602
603 static void rtas_quiesce(sPAPREnvironment *spapr, uint32_t token,
604 uint32_t nargs, target_ulong args,
605 uint32_t nret, target_ulong rets)
606 {
607 VIOsPAPRBus *bus = spapr->vio_bus;
608 DeviceState *qdev;
609 VIOsPAPRDevice *dev = NULL;
610
611 if (nargs != 0) {
612 rtas_st(rets, 0, -3);
613 return;
614 }
615
616 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
617 dev = (VIOsPAPRDevice *)qdev;
618 spapr_vio_quiesce_one(dev);
619 }
620
621 rtas_st(rets, 0, 0);
622 }
623
624 static int spapr_vio_check_reg(VIOsPAPRDevice *sdev)
625 {
626 VIOsPAPRDevice *other_sdev;
627 DeviceState *qdev;
628 VIOsPAPRBus *sbus;
629
630 sbus = DO_UPCAST(VIOsPAPRBus, bus, sdev->qdev.parent_bus);
631
632 /*
633 * Check two device aren't given clashing addresses by the user (or some
634 * other mechanism). We have to open code this because we have to check
635 * for matches with devices other than us.
636 */
637 QTAILQ_FOREACH(qdev, &sbus->bus.children, sibling) {
638 other_sdev = DO_UPCAST(VIOsPAPRDevice, qdev, qdev);
639
640 if (other_sdev != sdev && other_sdev->reg == sdev->reg) {
641 fprintf(stderr, "vio: %s and %s devices conflict at address %#x\n",
642 object_get_typename(OBJECT(sdev)),
643 object_get_typename(OBJECT(qdev)),
644 sdev->reg);
645 return -EEXIST;
646 }
647 }
648
649 return 0;
650 }
651
652 static int spapr_vio_busdev_init(DeviceState *qdev, DeviceInfo *qinfo)
653 {
654 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
655 VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
656 char *id;
657 int ret;
658
659 ret = spapr_vio_check_reg(dev);
660 if (ret) {
661 return ret;
662 }
663
664 /* Don't overwrite ids assigned on the command line */
665 if (!dev->qdev.id) {
666 id = vio_format_dev_name(dev);
667 if (!id) {
668 return -1;
669 }
670 dev->qdev.id = id;
671 }
672
673 dev->qirq = spapr_allocate_irq(dev->vio_irq_num, &dev->vio_irq_num);
674 if (!dev->qirq) {
675 return -1;
676 }
677
678 rtce_init(dev);
679
680 return pc->init(dev);
681 }
682
683 void spapr_vio_bus_register_withprop(DeviceInfo *info)
684 {
685 info->init = spapr_vio_busdev_init;
686 info->bus_info = &spapr_vio_bus_info;
687
688 assert(info->size >= sizeof(VIOsPAPRDevice));
689 qdev_register_subclass(info, TYPE_VIO_SPAPR_DEVICE);
690 }
691
692 static target_ulong h_vio_signal(CPUState *env, sPAPREnvironment *spapr,
693 target_ulong opcode,
694 target_ulong *args)
695 {
696 target_ulong reg = args[0];
697 target_ulong mode = args[1];
698 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
699 VIOsPAPRDeviceClass *pc;
700
701 if (!dev) {
702 return H_PARAMETER;
703 }
704
705 pc = VIO_SPAPR_DEVICE_GET_CLASS(dev);
706
707 if (mode & ~pc->signal_mask) {
708 return H_PARAMETER;
709 }
710
711 dev->signal_state = mode;
712
713 return H_SUCCESS;
714 }
715
716 VIOsPAPRBus *spapr_vio_bus_init(void)
717 {
718 VIOsPAPRBus *bus;
719 BusState *qbus;
720 DeviceState *dev;
721
722 /* Create bridge device */
723 dev = qdev_create(NULL, "spapr-vio-bridge");
724 qdev_init_nofail(dev);
725
726 /* Create bus on bridge device */
727
728 qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio");
729 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
730
731 /* hcall-vio */
732 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
733
734 /* hcall-tce */
735 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
736
737 /* hcall-crq */
738 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
739 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
740 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
741 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
742
743 /* RTAS calls */
744 spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass);
745 spapr_rtas_register("quiesce", rtas_quiesce);
746
747 return bus;
748 }
749
750 /* Represents sPAPR hcall VIO devices */
751
752 static int spapr_vio_bridge_init(SysBusDevice *dev)
753 {
754 /* nothing */
755 return 0;
756 }
757
758 static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data)
759 {
760 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
761
762 sbc->init = spapr_vio_bridge_init;
763 }
764
765 static DeviceInfo spapr_vio_bridge_info = {
766 .name = "spapr-vio-bridge",
767 .size = sizeof(SysBusDevice),
768 .no_user = 1,
769 .class_init = spapr_vio_bridge_class_init,
770 };
771
772 static TypeInfo spapr_vio_type_info = {
773 .name = TYPE_VIO_SPAPR_DEVICE,
774 .parent = TYPE_DEVICE,
775 .instance_size = sizeof(VIOsPAPRDevice),
776 .abstract = true,
777 .class_size = sizeof(VIOsPAPRDeviceClass),
778 };
779
780 static void spapr_vio_register_devices(void)
781 {
782 sysbus_register_withprop(&spapr_vio_bridge_info);
783 type_register_static(&spapr_vio_type_info);
784 }
785
786 device_init(spapr_vio_register_devices)
787
788 #ifdef CONFIG_FDT
789 static int compare_reg(const void *p1, const void *p2)
790 {
791 VIOsPAPRDevice const *dev1, *dev2;
792
793 dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1;
794 dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2;
795
796 if (dev1->reg < dev2->reg) {
797 return -1;
798 }
799 if (dev1->reg == dev2->reg) {
800 return 0;
801 }
802
803 /* dev1->reg > dev2->reg */
804 return 1;
805 }
806
807 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
808 {
809 DeviceState *qdev, **qdevs;
810 int i, num, ret = 0;
811
812 /* Count qdevs on the bus list */
813 num = 0;
814 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
815 num++;
816 }
817
818 /* Copy out into an array of pointers */
819 qdevs = g_malloc(sizeof(qdev) * num);
820 num = 0;
821 QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) {
822 qdevs[num++] = qdev;
823 }
824
825 /* Sort the array */
826 qsort(qdevs, num, sizeof(qdev), compare_reg);
827
828 /* Hack alert. Give the devices to libfdt in reverse order, we happen
829 * to know that will mean they are in forward order in the tree. */
830 for (i = num - 1; i >= 0; i--) {
831 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]);
832
833 ret = vio_make_devnode(dev, fdt);
834
835 if (ret < 0) {
836 goto out;
837 }
838 }
839
840 ret = 0;
841 out:
842 free(qdevs);
843
844 return ret;
845 }
846
847 int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus)
848 {
849 VIOsPAPRDevice *dev;
850 char *name, *path;
851 int ret, offset;
852
853 dev = spapr_vty_get_default(bus);
854 if (!dev)
855 return 0;
856
857 offset = fdt_path_offset(fdt, "/chosen");
858 if (offset < 0) {
859 return offset;
860 }
861
862 name = vio_format_dev_name(dev);
863 if (!name) {
864 return -ENOMEM;
865 }
866
867 if (asprintf(&path, "/vdevice/%s", name) < 0) {
868 path = NULL;
869 ret = -ENOMEM;
870 goto out;
871 }
872
873 ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path);
874 out:
875 free(name);
876 free(path);
877
878 return ret;
879 }
880 #endif /* CONFIG_FDT */