]> git.proxmox.com Git - qemu.git/blame - hw/spapr_vio.c
Implement sPAPR Virtual LAN (ibmveth)
[qemu.git] / hw / spapr_vio.c
CommitLineData
4040ab72
DG
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
32#include "hw/spapr.h"
33#include "hw/spapr_vio.h"
34
35#ifdef CONFIG_FDT
36#include <libfdt.h>
37#endif /* CONFIG_FDT */
38
39/* #define DEBUG_SPAPR */
ee86dfee 40/* #define DEBUG_TCE */
4040ab72
DG
41
42#ifdef DEBUG_SPAPR
43#define dprintf(fmt, ...) \
44 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
45#else
46#define dprintf(fmt, ...) \
47 do { } while (0)
48#endif
49
50static struct BusInfo spapr_vio_bus_info = {
51 .name = "spapr-vio",
52 .size = sizeof(VIOsPAPRBus),
53};
54
55VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg)
56{
57 DeviceState *qdev;
58 VIOsPAPRDevice *dev = NULL;
59
60 QLIST_FOREACH(qdev, &bus->bus.children, sibling) {
61 dev = (VIOsPAPRDevice *)qdev;
62 if (dev->reg == reg) {
63 break;
64 }
65 }
66
67 return dev;
68}
69
70#ifdef CONFIG_FDT
71static int vio_make_devnode(VIOsPAPRDevice *dev,
72 void *fdt)
73{
74 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)dev->qdev.info;
75 int vdevice_off, node_off;
76 int ret;
77
78 vdevice_off = fdt_path_offset(fdt, "/vdevice");
79 if (vdevice_off < 0) {
80 return vdevice_off;
81 }
82
83 node_off = fdt_add_subnode(fdt, vdevice_off, dev->qdev.id);
84 if (node_off < 0) {
85 return node_off;
86 }
87
88 ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg);
89 if (ret < 0) {
90 return ret;
91 }
92
93 if (info->dt_type) {
94 ret = fdt_setprop_string(fdt, node_off, "device_type",
95 info->dt_type);
96 if (ret < 0) {
97 return ret;
98 }
99 }
100
101 if (info->dt_compatible) {
102 ret = fdt_setprop_string(fdt, node_off, "compatible",
103 info->dt_compatible);
104 if (ret < 0) {
105 return ret;
106 }
107 }
108
00dc738d
DG
109 if (dev->qirq) {
110 uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0};
111
112 ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop,
113 sizeof(ints_prop));
114 if (ret < 0) {
115 return ret;
116 }
117 }
118
ee86dfee
DG
119 if (dev->rtce_window_size) {
120 uint32_t dma_prop[] = {cpu_to_be32(dev->reg),
121 0, 0,
122 0, cpu_to_be32(dev->rtce_window_size)};
123
124 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
125 if (ret < 0) {
126 return ret;
127 }
128
129 ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
130 if (ret < 0) {
131 return ret;
132 }
133
134 ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop,
135 sizeof(dma_prop));
136 if (ret < 0) {
137 return ret;
138 }
139 }
140
4040ab72
DG
141 if (info->devnode) {
142 ret = (info->devnode)(dev, fdt, node_off);
143 if (ret < 0) {
144 return ret;
145 }
146 }
147
148 return node_off;
149}
150#endif /* CONFIG_FDT */
151
ee86dfee
DG
152/*
153 * RTCE handling
154 */
155
156static void rtce_init(VIOsPAPRDevice *dev)
157{
158 size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT)
159 * sizeof(VIOsPAPR_RTCE);
160
161 if (size) {
162 dev->rtce_table = qemu_mallocz(size);
163 }
164}
165
166static target_ulong h_put_tce(CPUState *env, sPAPREnvironment *spapr,
167 target_ulong opcode, target_ulong *args)
168{
169 target_ulong liobn = args[0];
170 target_ulong ioba = args[1];
171 target_ulong tce = args[2];
172 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, liobn);
173 VIOsPAPR_RTCE *rtce;
174
175 if (!dev) {
176 hcall_dprintf("spapr_vio_put_tce on non-existent LIOBN "
177 TARGET_FMT_lx "\n", liobn);
178 return H_PARAMETER;
179 }
180
181 ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1);
182
183#ifdef DEBUG_TCE
184 fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx
185 " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce);
186#endif
187
188 if (ioba >= dev->rtce_window_size) {
189 hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
190 TARGET_FMT_lx "\n", ioba);
191 return H_PARAMETER;
192 }
193
194 rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT);
195 rtce->tce = tce;
196
197 return H_SUCCESS;
198}
199
200int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba,
201 target_ulong len, enum VIOsPAPR_TCEAccess access)
202{
203 int start, end, i;
204
205 start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT;
206 end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT;
207
208 for (i = start; i <= end; i++) {
209 if ((dev->rtce_table[i].tce & access) != access) {
210#ifdef DEBUG_TCE
211 fprintf(stderr, "FAIL on %d\n", i);
212#endif
213 return -1;
214 }
215 }
216
217 return 0;
218}
219
220int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf,
221 uint32_t size)
222{
223#ifdef DEBUG_TCE
224 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
225 (unsigned long long)taddr, size);
226#endif
227
228 while (size) {
229 uint64_t tce;
230 uint32_t lsize;
231 uint64_t txaddr;
232
233 /* Check if we are in bound */
234 if (taddr >= dev->rtce_window_size) {
235#ifdef DEBUG_TCE
236 fprintf(stderr, "spapr_tce_dma_write out of bounds\n");
237#endif
238 return H_DEST_PARM;
239 }
240 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
241
242 /* How much til end of page ? */
243 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
244
245 /* Check TCE */
246 if (!(tce & 2)) {
247 return H_DEST_PARM;
248 }
249
250 /* Translate */
251 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
252 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
253
254#ifdef DEBUG_TCE
255 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
256 (unsigned long long)txaddr, lsize);
257#endif
258
259 /* Do it */
260 cpu_physical_memory_write(txaddr, buf, lsize);
261 buf += lsize;
262 taddr += lsize;
263 size -= lsize;
264 }
265 return 0;
266}
267
268int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size)
269{
270 /* FIXME: allocating a temp buffer is nasty, but just stepping
271 * through writing zeroes is awkward. This will do for now. */
272 uint8_t zeroes[size];
273
274#ifdef DEBUG_TCE
275 fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n",
276 (unsigned long long)taddr, size);
277#endif
278
279 memset(zeroes, 0, size);
280 return spapr_tce_dma_write(dev, taddr, zeroes, size);
281}
282
283void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val)
284{
285 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
286}
287
288void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val)
289{
290 val = tswap16(val);
291 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
292}
293
294
295void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val)
296{
297 val = tswap32(val);
298 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
299}
300
301void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val)
302{
303 val = tswap64(val);
304 spapr_tce_dma_write(dev, taddr, &val, sizeof(val));
305}
306
307int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf,
308 uint32_t size)
309{
310#ifdef DEBUG_TCE
311 fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n",
312 (unsigned long long)taddr, size);
313#endif
314
315 while (size) {
316 uint64_t tce;
317 uint32_t lsize;
318 uint64_t txaddr;
319
320 /* Check if we are in bound */
321 if (taddr >= dev->rtce_window_size) {
322#ifdef DEBUG_TCE
323 fprintf(stderr, "spapr_tce_dma_read out of bounds\n");
324#endif
325 return H_DEST_PARM;
326 }
327 tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce;
328
329 /* How much til end of page ? */
330 lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1);
331
332 /* Check TCE */
333 if (!(tce & 1)) {
334 return H_DEST_PARM;
335 }
336
337 /* Translate */
338 txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) |
339 (taddr & SPAPR_VIO_TCE_PAGE_MASK);
340
341#ifdef DEBUG_TCE
342 fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n",
343 (unsigned long long)txaddr, lsize);
344#endif
345 /* Do it */
346 cpu_physical_memory_read(txaddr, buf, lsize);
347 buf += lsize;
348 taddr += lsize;
349 size -= lsize;
350 }
351 return H_SUCCESS;
352}
353
354uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr)
355{
356 uint64_t val;
357
358 spapr_tce_dma_read(dev, taddr, &val, sizeof(val));
359 return tswap64(val);
360}
361
4040ab72
DG
362static int spapr_vio_busdev_init(DeviceState *qdev, DeviceInfo *qinfo)
363{
364 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)qinfo;
365 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
366 char *id;
367
368 if (asprintf(&id, "%s@%x", info->dt_name, dev->reg) < 0) {
369 return -1;
370 }
371
372 dev->qdev.id = id;
373
ee86dfee
DG
374 rtce_init(dev);
375
4040ab72
DG
376 return info->init(dev);
377}
378
379void spapr_vio_bus_register_withprop(VIOsPAPRDeviceInfo *info)
380{
381 info->qdev.init = spapr_vio_busdev_init;
382 info->qdev.bus_info = &spapr_vio_bus_info;
383
384 assert(info->qdev.size >= sizeof(VIOsPAPRDevice));
385 qdev_register(&info->qdev);
386}
387
00dc738d
DG
388static target_ulong h_vio_signal(CPUState *env, sPAPREnvironment *spapr,
389 target_ulong opcode,
390 target_ulong *args)
391{
392 target_ulong reg = args[0];
393 target_ulong mode = args[1];
394 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
395 VIOsPAPRDeviceInfo *info;
396
397 if (!dev) {
398 return H_PARAMETER;
399 }
400
401 info = (VIOsPAPRDeviceInfo *)dev->qdev.info;
402
403 if (mode & ~info->signal_mask) {
404 return H_PARAMETER;
405 }
406
407 dev->signal_state = mode;
408
409 return H_SUCCESS;
410}
411
4040ab72
DG
412VIOsPAPRBus *spapr_vio_bus_init(void)
413{
414 VIOsPAPRBus *bus;
415 BusState *qbus;
416 DeviceState *dev;
417 DeviceInfo *qinfo;
418
419 /* Create bridge device */
420 dev = qdev_create(NULL, "spapr-vio-bridge");
421 qdev_init_nofail(dev);
422
423 /* Create bus on bridge device */
424
425 qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio");
426 bus = DO_UPCAST(VIOsPAPRBus, bus, qbus);
427
00dc738d
DG
428 /* hcall-vio */
429 spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal);
430
ee86dfee
DG
431 /* hcall-tce */
432 spapr_register_hypercall(H_PUT_TCE, h_put_tce);
433
4040ab72
DG
434 for (qinfo = device_info_list; qinfo; qinfo = qinfo->next) {
435 VIOsPAPRDeviceInfo *info = (VIOsPAPRDeviceInfo *)qinfo;
436
437 if (qinfo->bus_info != &spapr_vio_bus_info) {
438 continue;
439 }
440
441 if (info->hcalls) {
442 info->hcalls(bus);
443 }
444 }
445
446 return bus;
447}
448
449/* Represents sPAPR hcall VIO devices */
450
451static int spapr_vio_bridge_init(SysBusDevice *dev)
452{
453 /* nothing */
454 return 0;
455}
456
457static SysBusDeviceInfo spapr_vio_bridge_info = {
458 .init = spapr_vio_bridge_init,
459 .qdev.name = "spapr-vio-bridge",
460 .qdev.size = sizeof(SysBusDevice),
461 .qdev.no_user = 1,
462};
463
464static void spapr_vio_register_devices(void)
465{
466 sysbus_register_withprop(&spapr_vio_bridge_info);
467}
468
469device_init(spapr_vio_register_devices)
470
471#ifdef CONFIG_FDT
472int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt)
473{
474 DeviceState *qdev;
475 int ret = 0;
476
477 QLIST_FOREACH(qdev, &bus->bus.children, sibling) {
478 VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev;
479
480 ret = vio_make_devnode(dev, fdt);
481
482 if (ret < 0) {
483 return ret;
484 }
485 }
486
487 return 0;
488}
489#endif /* CONFIG_FDT */