1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "qapi/error.h"
26#include <libfdt.h>
27#include "qemu-common.h"
28#ifdef CONFIG_LINUX
29#include <linux/vfio.h>
30#endif
31#include "hw/arm/sysbus-fdt.h"
32#include "qemu/error-report.h"
33#include "sysemu/device_tree.h"
34#include "hw/platform-bus.h"
35#include "sysemu/sysemu.h"
36#include "hw/vfio/vfio-platform.h"
37#include "hw/vfio/vfio-calxeda-xgmac.h"
38#include "hw/vfio/vfio-amd-xgbe.h"
39#include "hw/display/ramfb.h"
40#include "hw/arm/fdt.h"
41
42
43
44
45
46typedef struct PlatformBusFDTData {
47 void *fdt;
48 int irq_start;
49 const char *pbus_node_name;
50 PlatformBusDevice *pbus;
51} PlatformBusFDTData;
52
53
54typedef struct BindingEntry {
55 const char *typename;
56 const char *compat;
57 int (*add_fn)(SysBusDevice *sbdev, void *opaque);
58 bool (*match_fn)(SysBusDevice *sbdev, const struct BindingEntry *combo);
59} BindingEntry;
60
61
62
63typedef struct HostProperty {
64 const char *name;
65 bool optional;
66} HostProperty;
67
68#ifdef CONFIG_LINUX
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85static void copy_properties_from_host(HostProperty *props, int nb_props,
86 void *host_fdt, void *guest_fdt,
87 char *node_path, char *nodename)
88{
89 int i, prop_len;
90 const void *r;
91 Error *err = NULL;
92
93 for (i = 0; i < nb_props; i++) {
94 r = qemu_fdt_getprop(host_fdt, node_path,
95 props[i].name,
96 &prop_len,
97 &err);
98 if (r) {
99 qemu_fdt_setprop(guest_fdt, nodename,
100 props[i].name, r, prop_len);
101 } else {
102 if (props[i].optional && prop_len == -FDT_ERR_NOTFOUND) {
103
104 error_free(err);
105 } else {
106 error_report_err(err);
107 }
108 if (!props[i].optional) {
109
110 exit(1);
111 }
112 err = NULL;
113 }
114 }
115}
116
117
118static HostProperty clock_copied_properties[] = {
119 {"compatible", false},
120 {"#clock-cells", false},
121 {"clock-frequency", true},
122 {"clock-output-names", true},
123};
124
125
126
127
128
129
130
131
132
133
134
135
136
137static void fdt_build_clock_node(void *host_fdt, void *guest_fdt,
138 uint32_t host_phandle,
139 uint32_t guest_phandle)
140{
141 char *node_path = NULL;
142 char *nodename;
143 const void *r;
144 int ret, node_offset, prop_len, path_len = 16;
145
146 node_offset = fdt_node_offset_by_phandle(host_fdt, host_phandle);
147 if (node_offset <= 0) {
148 error_report("not able to locate clock handle %d in host device tree",
149 host_phandle);
150 exit(1);
151 }
152 node_path = g_malloc(path_len);
153 while ((ret = fdt_get_path(host_fdt, node_offset, node_path, path_len))
154 == -FDT_ERR_NOSPACE) {
155 path_len += 16;
156 node_path = g_realloc(node_path, path_len);
157 }
158 if (ret < 0) {
159 error_report("not able to retrieve node path for clock handle %d",
160 host_phandle);
161 exit(1);
162 }
163
164 r = qemu_fdt_getprop(host_fdt, node_path, "compatible", &prop_len,
165 &error_fatal);
166 if (strcmp(r, "fixed-clock")) {
167 error_report("clock handle %d is not a fixed clock", host_phandle);
168 exit(1);
169 }
170
171 nodename = strrchr(node_path, '/');
172 qemu_fdt_add_subnode(guest_fdt, nodename);
173
174 copy_properties_from_host(clock_copied_properties,
175 ARRAY_SIZE(clock_copied_properties),
176 host_fdt, guest_fdt,
177 node_path, nodename);
178
179 qemu_fdt_setprop_cell(guest_fdt, nodename, "phandle", guest_phandle);
180
181 g_free(node_path);
182}
183
184
185
186
187
188
189
190
191
192static char *sysfs_to_dt_name(const char *sysfs_name)
193{
194 gchar **substrings = g_strsplit(sysfs_name, ".", 2);
195 char *dt_name = NULL;
196
197 if (!substrings || !substrings[0] || !substrings[1]) {
198 goto out;
199 }
200 dt_name = g_strdup_printf("%s@%s", substrings[1], substrings[0]);
201out:
202 g_strfreev(substrings);
203 return dt_name;
204}
205
206
207
208
209
210
211
212
213
214static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
215{
216 PlatformBusFDTData *data = opaque;
217 PlatformBusDevice *pbus = data->pbus;
218 void *fdt = data->fdt;
219 const char *parent_node = data->pbus_node_name;
220 int compat_str_len, i;
221 char *nodename;
222 uint32_t *irq_attr, *reg_attr;
223 uint64_t mmio_base, irq_number;
224 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
225 VFIODevice *vbasedev = &vdev->vbasedev;
226
227 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
228 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
229 vbasedev->name, mmio_base);
230 qemu_fdt_add_subnode(fdt, nodename);
231
232 compat_str_len = strlen(vdev->compat) + 1;
233 qemu_fdt_setprop(fdt, nodename, "compatible",
234 vdev->compat, compat_str_len);
235
236 qemu_fdt_setprop(fdt, nodename, "dma-coherent", "", 0);
237
238 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
239 for (i = 0; i < vbasedev->num_regions; i++) {
240 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
241 reg_attr[2 * i] = cpu_to_be32(mmio_base);
242 reg_attr[2 * i + 1] = cpu_to_be32(
243 memory_region_size(vdev->regions[i]->mem));
244 }
245 qemu_fdt_setprop(fdt, nodename, "reg", reg_attr,
246 vbasedev->num_regions * 2 * sizeof(uint32_t));
247
248 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
249 for (i = 0; i < vbasedev->num_irqs; i++) {
250 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
251 + data->irq_start;
252 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
253 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
254 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
255 }
256 qemu_fdt_setprop(fdt, nodename, "interrupts",
257 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
258 g_free(irq_attr);
259 g_free(reg_attr);
260 g_free(nodename);
261 return 0;
262}
263
264
265static HostProperty amd_xgbe_copied_properties[] = {
266 {"compatible", false},
267 {"dma-coherent", true},
268 {"amd,per-channel-interrupt", true},
269 {"phy-mode", false},
270 {"mac-address", true},
271 {"amd,speed-set", false},
272 {"amd,serdes-blwc", true},
273 {"amd,serdes-cdr-rate", true},
274 {"amd,serdes-pq-skew", true},
275 {"amd,serdes-tx-amp", true},
276 {"amd,serdes-dfe-tap-config", true},
277 {"amd,serdes-dfe-tap-enable", true},
278 {"clock-names", false},
279};
280
281
282
283
284
285
286
287
288
289
290
291static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque)
292{
293 PlatformBusFDTData *data = opaque;
294 PlatformBusDevice *pbus = data->pbus;
295 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
296 VFIODevice *vbasedev = &vdev->vbasedev;
297 VFIOINTp *intp;
298 const char *parent_node = data->pbus_node_name;
299 char **node_path, *nodename, *dt_name;
300 void *guest_fdt = data->fdt, *host_fdt;
301 const void *r;
302 int i, prop_len;
303 uint32_t *irq_attr, *reg_attr, *host_clock_phandles;
304 uint64_t mmio_base, irq_number;
305 uint32_t guest_clock_phandles[2];
306
307 host_fdt = load_device_tree_from_sysfs();
308
309 dt_name = sysfs_to_dt_name(vbasedev->name);
310 if (!dt_name) {
311 error_report("%s incorrect sysfs device name %s",
312 __func__, vbasedev->name);
313 exit(1);
314 }
315 node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
316 &error_fatal);
317 if (!node_path || !node_path[0]) {
318 error_report("%s unable to retrieve node path for %s/%s",
319 __func__, dt_name, vdev->compat);
320 exit(1);
321 }
322
323 if (node_path[1]) {
324 error_report("%s more than one node matching %s/%s!",
325 __func__, dt_name, vdev->compat);
326 exit(1);
327 }
328
329 g_free(dt_name);
330
331 if (vbasedev->num_regions != 5) {
332 error_report("%s Does the host dt node combine XGBE/PHY?", __func__);
333 exit(1);
334 }
335
336
337 r = qemu_fdt_getprop(host_fdt, node_path[0], "clocks",
338 &prop_len, &error_fatal);
339 if (prop_len != 8) {
340 error_report("%s clocks property should contain 2 handles", __func__);
341 exit(1);
342 }
343 host_clock_phandles = (uint32_t *)r;
344 guest_clock_phandles[0] = qemu_fdt_alloc_phandle(guest_fdt);
345 guest_clock_phandles[1] = qemu_fdt_alloc_phandle(guest_fdt);
346
347
348
349
350
351
352 fdt_build_clock_node(host_fdt, guest_fdt,
353 be32_to_cpu(host_clock_phandles[0]),
354 guest_clock_phandles[0]);
355
356 fdt_build_clock_node(host_fdt, guest_fdt,
357 be32_to_cpu(host_clock_phandles[1]),
358 guest_clock_phandles[1]);
359
360
361 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
362 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
363 vbasedev->name, mmio_base);
364 qemu_fdt_add_subnode(guest_fdt, nodename);
365
366 copy_properties_from_host(amd_xgbe_copied_properties,
367 ARRAY_SIZE(amd_xgbe_copied_properties),
368 host_fdt, guest_fdt,
369 node_path[0], nodename);
370
371 qemu_fdt_setprop_cells(guest_fdt, nodename, "clocks",
372 guest_clock_phandles[0],
373 guest_clock_phandles[1]);
374
375 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
376 for (i = 0; i < vbasedev->num_regions; i++) {
377 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
378 reg_attr[2 * i] = cpu_to_be32(mmio_base);
379 reg_attr[2 * i + 1] = cpu_to_be32(
380 memory_region_size(vdev->regions[i]->mem));
381 }
382 qemu_fdt_setprop(guest_fdt, nodename, "reg", reg_attr,
383 vbasedev->num_regions * 2 * sizeof(uint32_t));
384
385 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
386 for (i = 0; i < vbasedev->num_irqs; i++) {
387 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
388 + data->irq_start;
389 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
390 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
391
392
393
394
395
396 QLIST_FOREACH(intp, &vdev->intp_list, next) {
397 if (intp->pin == i) {
398 break;
399 }
400 }
401 if (intp->flags & VFIO_IRQ_INFO_AUTOMASKED) {
402 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
403 } else {
404 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
405 }
406 }
407 qemu_fdt_setprop(guest_fdt, nodename, "interrupts",
408 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
409
410 g_free(host_fdt);
411 g_strfreev(node_path);
412 g_free(irq_attr);
413 g_free(reg_attr);
414 g_free(nodename);
415 return 0;
416}
417
418
419static bool vfio_platform_match(SysBusDevice *sbdev,
420 const BindingEntry *entry)
421{
422 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
423 const char *compat;
424 unsigned int n;
425
426 for (n = vdev->num_compat, compat = vdev->compat; n > 0;
427 n--, compat += strlen(compat) + 1) {
428 if (!strcmp(entry->compat, compat)) {
429 return true;
430 }
431 }
432
433 return false;
434}
435
436#define VFIO_PLATFORM_BINDING(compat, add_fn) \
437 {TYPE_VFIO_PLATFORM, (compat), (add_fn), vfio_platform_match}
438
439#endif
440
441static int no_fdt_node(SysBusDevice *sbdev, void *opaque)
442{
443 return 0;
444}
445
446
447static bool type_match(SysBusDevice *sbdev, const BindingEntry *entry)
448{
449 return !strcmp(object_get_typename(OBJECT(sbdev)), entry->typename);
450}
451
452#define TYPE_BINDING(type, add_fn) {(type), NULL, (add_fn), NULL}
453
454
455static const BindingEntry bindings[] = {
456#ifdef CONFIG_LINUX
457 TYPE_BINDING(TYPE_VFIO_CALXEDA_XGMAC, add_calxeda_midway_xgmac_fdt_node),
458 TYPE_BINDING(TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node),
459 VFIO_PLATFORM_BINDING("amd,xgbe-seattle-v1a", add_amd_xgbe_fdt_node),
460#endif
461 TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node),
462 TYPE_BINDING("", NULL),
463};
464
465
466
467
468
469
470
471
472
473
474
475
476
477static void add_fdt_node(SysBusDevice *sbdev, void *opaque)
478{
479 int i, ret;
480
481 for (i = 0; i < ARRAY_SIZE(bindings); i++) {
482 const BindingEntry *iter = &bindings[i];
483
484 if (type_match(sbdev, iter)) {
485 if (!iter->match_fn || iter->match_fn(sbdev, iter)) {
486 ret = iter->add_fn(sbdev, opaque);
487 assert(!ret);
488 return;
489 }
490 }
491 }
492 error_report("Device %s can not be dynamically instantiated",
493 qdev_fw_name(DEVICE(sbdev)));
494 exit(1);
495}
496
497void platform_bus_add_all_fdt_nodes(void *fdt, const char *intc, hwaddr addr,
498 hwaddr bus_size, int irq_start)
499{
500 const char platcomp[] = "qemu,platform\0simple-bus";
501 PlatformBusDevice *pbus;
502 DeviceState *dev;
503 gchar *node;
504
505 assert(fdt);
506
507 node = g_strdup_printf("/platform@%"PRIx64, addr);
508
509
510 qemu_fdt_add_subnode(fdt, node);
511 qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp));
512
513
514
515
516 qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1);
517 qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1);
518 qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, bus_size);
519
520 qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc);
521
522 dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE);
523 pbus = PLATFORM_BUS_DEVICE(dev);
524
525 PlatformBusFDTData data = {
526 .fdt = fdt,
527 .irq_start = irq_start,
528 .pbus_node_name = node,
529 .pbus = pbus,
530 };
531
532
533 foreach_dynamic_sysbus_device(add_fdt_node, &data);
534
535 g_free(node);
536}
537