]> git.proxmox.com Git - qemu.git/blob - hw/spapr_vio.c
Implement PAPR CRQ hypercalls
[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
36 #ifdef CONFIG_FDT
37 #include <libfdt.h>
38 #endif /* CONFIG_FDT */
39
40 /* #define DEBUG_SPAPR */
41 /* #define DEBUG_TCE */
42
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
50
51 static struct BusInfo spapr_vio_bus_info = {
52 .name = "spapr-vio",
53 .size = sizeof(VIOsPAPRBus),
54 };
55
56 VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
57 {
58 DeviceState *qdev;
59 VIOsPAPRDevice *dev = NULL;
60
61 QLIST_FOREACH(qdev, &bus->bus.children, sibling) {
62 dev = (VIOsPAPRDevice *)qdev;
63 if (dev->reg == reg) {
64 break;
65 }
66 }
67
68 return dev;
69 }
70
71 #ifdef CONFIG_FDT
72 static int vio_make_devnode(VIOsPAPRDevice *dev,
73 void *fdt)
74 {
75 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)dev->qdev.info;
76 int vdevice_off, node_off;
77 int ret;
78
79 vdevice_off = fdt_path_offset(fdt, "/vdevice");
80 if (vdevice_off < 0) {
81 return vdevice_off;
82 }
83
84 node_off = fdt_add_subnode(fdt, vdevice_off, dev->qdev.id);
85 if (node_off < 0) {
86 return node_off;
87 }
88
89 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
90 if (ret < 0) {
91 return ret;
92 }
93
94 if (info->dt_type) {
95 ret = fdt_setprop_string(fdt, node_off, "device_type",
96 info->dt_type);
97 if (ret < 0) {
98 return ret;
99 }
100 }
101
102 if (info->dt_compatible) {
103 ret = fdt_setprop_string(fdt, node_off, "compatible",
104 info->dt_compatible);
105 if (ret < 0) {
106 return ret;
107 }
108 }
109
110 if (dev->qirq) {
111 uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0};
112
113 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
114 sizeof(ints_prop));
115 if (ret < 0) {
116 return ret;
117 }
118 }
119
120 if (dev->rtce_window_size) {
121 uint32_t dma_prop[] = {cpu_to_be32(dev->reg),
122 0, 0,
123 0, cpu_to_be32(dev->rtce_window_size)};
124
125 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
126 if (ret < 0) {
127 return ret;
128 }
129
130 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
131 if (ret < 0) {
132 return ret;
133 }
134
135 ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop,
136 sizeof(dma_prop));
137 if (ret < 0) {
138 return ret;
139 }
140 }
141
142 if (info->devnode) {
143 ret = (info->devnode)(dev, fdt, node_off);
144 if (ret < 0) {
145 return ret;
146 }
147 }
148
149 return node_off;
150 }
151 #endif /* CONFIG_FDT */
152
153 /*
154 * RTCE handling
155 */
156
157 static void rtce_init(VIOsPAPRDevice *dev)
158 {
159 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
160 * sizeof(VIOsPAPR_RTCE);
161
162 if (size) {
163 dev->rtce_table = qemu_mallocz(size);
164 }
165 }
166
167 static target_ulong h_put_tce(CPUState *env, sPAPREnvironment *spapr,
168 target_ulong opcode, target_ulong *args)
169 {
170 target_ulong liobn = args[0];
171 target_ulong ioba = args[1];
172 target_ulong tce = args[2];
173 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, liobn);
174 VIOsPAPR_RTCE *rtce;
175
176 if (!dev) {
177 hcall_dprintf("spapr_vio_put_tce on non-existent LIOBN "
178 TARGET_FMT_lx "\n", liobn);
179 return H_PARAMETER;
180 }
181
182 ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1);
183
184 #ifdef DEBUG_TCE
185 fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx
186 " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce);
187 #endif
188
189 if (ioba >= dev->rtce_window_size) {
190 hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
191 TARGET_FMT_lx "\n", ioba);
192 return H_PARAMETER;
193 }
194
195 rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT);
196 rtce->tce = tce;
197
198 return H_SUCCESS;
199 }
200
201 int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba,
202 target_ulong len, enum VIOsPAPR_TCEAccess access)
203 {
204 int start, end, i;
205
206 start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT;
207 end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT;
208
209 for (i = start; i <= end; i++) {
210 if ((dev->rtce_table[i].tce & access) != access) {
211 #ifdef DEBUG_TCE
212 fprintf(stderr, "FAIL on %d\n", i);
213 #endif
214 return -1;
215 }
216 }
217
218 return 0;
219 }
220
221 int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf,
222 uint32_t size)
223 {
224 #ifdef DEBUG_TCE
225 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
226 (unsigned long long)taddr, size);
227 #endif
228
229 while (size) {
230 uint64_t tce;
231 uint32_t lsize;
232 uint64_t txaddr;
233
234 /* Check if we are in bound */
235 if (taddr >= dev->rtce_window_size) {
236 #ifdef DEBUG_TCE
237 fprintf(stderr, "spapr_tce_dma_write out of bounds\n");
238 #endif
239 return H_DEST_PARM;
240 }
241 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
242
243 /* How much til end of page ? */
244 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
245
246 /* Check TCE */
247 if (!(tce & 2)) {
248 return H_DEST_PARM;
249 }
250
251 /* Translate */
252 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
253 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
254
255 #ifdef DEBUG_TCE
256 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
257 (unsigned long long)txaddr, lsize);
258 #endif
259
260 /* Do it */
261 cpu_physical_memory_write(txaddr, buf, lsize);
262 buf += lsize;
263 taddr += lsize;
264 size -= lsize;
265 }
266 return 0;
267 }
268
269 int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size)
270 {
271 /* FIXME: allocating a temp buffer is nasty, but just stepping
272 * through writing zeroes is awkward. This will do for now. */
273 uint8_t zeroes[size];
274
275 #ifdef DEBUG_TCE
276 fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
277 (unsigned long long)taddr, size);
278 #endif
279
280 memset(zeroes, 0, size);
281 return spapr_tce_dma_write(dev, taddr, zeroes, size);
282 }
283
284 void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val)
285 {
286 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
287 }
288
289 void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val)
290 {
291 val = tswap16(val);
292 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
293 }
294
295
296 void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val)
297 {
298 val = tswap32(val);
299 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
300 }
301
302 void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val)
303 {
304 val = tswap64(val);
305 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
306 }
307
308 int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf,
309 uint32_t size)
310 {
311 #ifdef DEBUG_TCE
312 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
313 (unsigned long long)taddr, size);
314 #endif
315
316 while (size) {
317 uint64_t tce;
318 uint32_t lsize;
319 uint64_t txaddr;
320
321 /* Check if we are in bound */
322 if (taddr >= dev->rtce_window_size) {
323 #ifdef DEBUG_TCE
324 fprintf(stderr, "spapr_tce_dma_read out of bounds\n");
325 #endif
326 return H_DEST_PARM;
327 }
328 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
329
330 /* How much til end of page ? */
331 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
332
333 /* Check TCE */
334 if (!(tce & 1)) {
335 return H_DEST_PARM;
336 }
337
338 /* Translate */
339 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
340 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
341
342 #ifdef DEBUG_TCE
343 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
344 (unsigned long long)txaddr, lsize);
345 #endif
346 /* Do it */
347 cpu_physical_memory_read(txaddr, buf, lsize);
348 buf += lsize;
349 taddr += lsize;
350 size -= lsize;
351 }
352 return H_SUCCESS;
353 }
354
355 uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr)
356 {
357 uint64_t val;
358
359 spapr_tce_dma_read(dev, taddr, &val, sizeof(val));
360 return tswap64(val);
361 }
362
363 /*
364 * CRQ handling
365 */
366 static target_ulong h_reg_crq(CPUState *env, sPAPREnvironment *spapr,
367 target_ulong opcode, target_ulong *args)
368 {
369 target_ulong reg = args[0];
370 target_ulong queue_addr = args[1];
371 target_ulong queue_len = args[2];
372 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
373
374 if (!dev) {
375 hcall_dprintf("h_reg_crq on non-existent unit 0x"
376 TARGET_FMT_lx "\n", reg);
377 return H_PARAMETER;
378 }
379
380 /* We can't grok a queue size bigger than 256M for now */
381 if (queue_len < 0x1000 || queue_len > 0x10000000) {
382 hcall_dprintf("h_reg_crq, queue size too small or too big (0x%llx)\n",
383 (unsigned long long)queue_len);
384 return H_PARAMETER;
385 }
386
387 /* Check queue alignment */
388 if (queue_addr & 0xfff) {
389 hcall_dprintf("h_reg_crq, queue not aligned (0x%llx)\n",
390 (unsigned long long)queue_addr);
391 return H_PARAMETER;
392 }
393
394 /* Check if device supports CRQs */
395 if (!dev->crq.SendFunc) {
396 return H_NOT_FOUND;
397 }
398
399
400 /* Already a queue ? */
401 if (dev->crq.qsize) {
402 return H_RESOURCE;
403 }
404 dev->crq.qladdr = queue_addr;
405 dev->crq.qsize = queue_len;
406 dev->crq.qnext = 0;
407
408 dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x"
409 TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n",
410 reg, queue_addr, queue_len);
411 return H_SUCCESS;
412 }
413
414 static target_ulong h_free_crq(CPUState *env, sPAPREnvironment *spapr,
415 target_ulong opcode, target_ulong *args)
416 {
417 target_ulong reg = args[0];
418 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
419
420 if (!dev) {
421 hcall_dprintf("h_free_crq on non-existent unit 0x"
422 TARGET_FMT_lx "\n", reg);
423 return H_PARAMETER;
424 }
425
426 dev->crq.qladdr = 0;
427 dev->crq.qsize = 0;
428 dev->crq.qnext = 0;
429
430 dprintf("CRQ for dev 0x" TARGET_FMT_lx " freed\n", reg);
431
432 return H_SUCCESS;
433 }
434
435 static target_ulong h_send_crq(CPUState *env, sPAPREnvironment *spapr,
436 target_ulong opcode, target_ulong *args)
437 {
438 target_ulong reg = args[0];
439 target_ulong msg_hi = args[1];
440 target_ulong msg_lo = args[2];
441 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
442 uint64_t crq_mangle[2];
443
444 if (!dev) {
445 hcall_dprintf("h_send_crq on non-existent unit 0x"
446 TARGET_FMT_lx "\n", reg);
447 return H_PARAMETER;
448 }
449 crq_mangle[0] = cpu_to_be64(msg_hi);
450 crq_mangle[1] = cpu_to_be64(msg_lo);
451
452 if (dev->crq.SendFunc) {
453 return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle);
454 }
455
456 return H_HARDWARE;
457 }
458
459 static target_ulong h_enable_crq(CPUState *env, sPAPREnvironment *spapr,
460 target_ulong opcode, target_ulong *args)
461 {
462 target_ulong reg = args[0];
463 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
464
465 if (!dev) {
466 hcall_dprintf("h_enable_crq on non-existent unit 0x"
467 TARGET_FMT_lx "\n", reg);
468 return H_PARAMETER;
469 }
470
471 return 0;
472 }
473
474 /* Returns negative error, 0 success, or positive: queue full */
475 int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq)
476 {
477 int rc;
478 uint8_t byte;
479
480 if (!dev->crq.qsize) {
481 fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n");
482 return -1;
483 }
484
485 /* Maybe do a fast path for KVM just writing to the pages */
486 rc = spapr_tce_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1);
487 if (rc) {
488 return rc;
489 }
490 if (byte != 0) {
491 return 1;
492 }
493
494 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8,
495 &crq[8], 8);
496 if (rc) {
497 return rc;
498 }
499
500 kvmppc_eieio();
501
502 rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8);
503 if (rc) {
504 return rc;
505 }
506
507 dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize;
508
509 if (dev->signal_state & 1) {
510 qemu_irq_pulse(dev->qirq);
511 }
512
513 return 0;
514 }
515
516 static int spapr_vio_busdev_init(DeviceState *qdev, DeviceInfo *qinfo)
517 {
518 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)qinfo;
519 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
520 char *id;
521
522 if (asprintf(&id, "%s@%x", info->dt_name, dev->reg) < 0) {
523 return -1;
524 }
525
526 dev->qdev.id = id;
527
528 rtce_init(dev);
529
530 return info->init(dev);
531 }
532
533 void spapr_vio_bus_register_withprop(VIOsPAPRDeviceInfo *info)
534 {
535 info->qdev.init = spapr_vio_busdev_init;
536 info->qdev.bus_info = &spapr_vio_bus_info;
537
538 assert(info->qdev.size >= sizeof(VIOsPAPRDevice));
539 qdev_register(&info->qdev);
540 }
541
542 static target_ulong h_vio_signal(CPUState *env, sPAPREnvironment *spapr,
543 target_ulong opcode,
544 target_ulong *args)
545 {
546 target_ulong reg = args[0];
547 target_ulong mode = args[1];
548 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
549 VIOsPAPRDeviceInfo *info;
550
551 if (!dev) {
552 return H_PARAMETER;
553 }
554
555 info = (VIOsPAPRDeviceInfo *)dev->qdev.info;
556
557 if (mode & ~info->signal_mask) {
558 return H_PARAMETER;
559 }
560
561 dev->signal_state = mode;
562
563 return H_SUCCESS;
564 }
565
566 VIOsPAPRBus *spapr_vio_bus_init(void)
567 {
568 VIOsPAPRBus *bus;
569 BusState *qbus;
570 DeviceState *dev;
571 DeviceInfo *qinfo;
572
573 /* Create bridge device */
574 dev = qdev_create(NULL, "spapr-vio-bridge");
575 qdev_init_nofail(dev);
576
577 /* Create bus on bridge device */
578
579 qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio");
580 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
581
582 /* hcall-vio */
583 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
584
585 /* hcall-tce */
586 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
587
588 /* hcall-crq */
589 spapr_register_hypercall(H_REG_CRQ, h_reg_crq);
590 spapr_register_hypercall(H_FREE_CRQ, h_free_crq);
591 spapr_register_hypercall(H_SEND_CRQ, h_send_crq);
592 spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq);
593
594 for (qinfo = device_info_list; qinfo; qinfo = qinfo->next) {
595 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)qinfo;
596
597 if (qinfo->bus_info != &spapr_vio_bus_info) {
598 continue;
599 }
600
601 if (info->hcalls) {
602 info->hcalls(bus);
603 }
604 }
605
606 return bus;
607 }
608
609 /* Represents sPAPR hcall VIO devices */
610
611 static int spapr_vio_bridge_init(SysBusDevice *dev)
612 {
613 /* nothing */
614 return 0;
615 }
616
617 static SysBusDeviceInfo spapr_vio_bridge_info = {
618 .init = spapr_vio_bridge_init,
619 .qdev.name = "spapr-vio-bridge",
620 .qdev.size = sizeof(SysBusDevice),
621 .qdev.no_user = 1,
622 };
623
624 static void spapr_vio_register_devices(void)
625 {
626 sysbus_register_withprop(&spapr_vio_bridge_info);
627 }
628
629 device_init(spapr_vio_register_devices)
630
631 #ifdef CONFIG_FDT
632 int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
633 {
634 DeviceState *qdev;
635 int ret = 0;
636
637 QLIST_FOREACH(qdev, &bus->bus.children, sibling) {
638 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
639
640 ret = vio_make_devnode(dev, fdt);
641
642 if (ret < 0) {
643 return ret;
644 }
645 }
646
647 return 0;
648 }
649 #endif /* CONFIG_FDT */