aspeed/scu: Create separate write callbacks
[qemu/ar7.git] / hw / arm / sysbus-fdt.c
blob022fc97ecd88ac568fe43a83697204f4ce4706ee
1 /*
2 * ARM Platform Bus device tree generation helpers
4 * Copyright (c) 2014 Linaro Limited
6 * Authors:
7 * Alex Graf <agraf@suse.de>
8 * Eric Auger <eric.auger@linaro.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2 or later, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include <libfdt.h>
27 #ifdef CONFIG_LINUX
28 #include <linux/vfio.h>
29 #endif
30 #include "hw/arm/sysbus-fdt.h"
31 #include "qemu/error-report.h"
32 #include "sysemu/device_tree.h"
33 #include "hw/platform-bus.h"
34 #include "hw/vfio/vfio-platform.h"
35 #include "hw/vfio/vfio-calxeda-xgmac.h"
36 #include "hw/vfio/vfio-amd-xgbe.h"
37 #include "hw/display/ramfb.h"
38 #include "hw/arm/fdt.h"
41 * internal struct that contains the information to create dynamic
42 * sysbus device node
44 typedef struct PlatformBusFDTData {
45 void *fdt; /* device tree handle */
46 int irq_start; /* index of the first IRQ usable by platform bus devices */
47 const char *pbus_node_name; /* name of the platform bus node */
48 PlatformBusDevice *pbus;
49 } PlatformBusFDTData;
51 /* struct that allows to match a device and create its FDT node */
52 typedef struct BindingEntry {
53 const char *typename;
54 const char *compat;
55 int (*add_fn)(SysBusDevice *sbdev, void *opaque);
56 bool (*match_fn)(SysBusDevice *sbdev, const struct BindingEntry *combo);
57 } BindingEntry;
59 /* helpers */
61 typedef struct HostProperty {
62 const char *name;
63 bool optional;
64 } HostProperty;
66 #ifdef CONFIG_LINUX
68 /**
69 * copy_properties_from_host
71 * copies properties listed in an array from host device tree to
72 * guest device tree. If a non optional property is not found, the
73 * function asserts. An optional property is ignored if not found
74 * in the host device tree.
75 * @props: array of HostProperty to copy
76 * @nb_props: number of properties in the array
77 * @host_dt: host device tree blob
78 * @guest_dt: guest device tree blob
79 * @node_path: host dt node path where the property is supposed to be
80 found
81 * @nodename: guest node name the properties should be added to
83 static void copy_properties_from_host(HostProperty *props, int nb_props,
84 void *host_fdt, void *guest_fdt,
85 char *node_path, char *nodename)
87 int i, prop_len;
88 const void *r;
89 Error *err = NULL;
91 for (i = 0; i < nb_props; i++) {
92 r = qemu_fdt_getprop(host_fdt, node_path,
93 props[i].name,
94 &prop_len,
95 &err);
96 if (r) {
97 qemu_fdt_setprop(guest_fdt, nodename,
98 props[i].name, r, prop_len);
99 } else {
100 if (props[i].optional && prop_len == -FDT_ERR_NOTFOUND) {
101 /* optional property does not exist */
102 error_free(err);
103 } else {
104 error_report_err(err);
106 if (!props[i].optional) {
107 /* mandatory property not found: bail out */
108 exit(1);
110 err = NULL;
115 /* clock properties whose values are copied/pasted from host */
116 static HostProperty clock_copied_properties[] = {
117 {"compatible", false},
118 {"#clock-cells", false},
119 {"clock-frequency", true},
120 {"clock-output-names", true},
124 * fdt_build_clock_node
126 * Build a guest clock node, used as a dependency from a passthrough'ed
127 * device. Most information are retrieved from the host clock node.
128 * Also check the host clock is a fixed one.
130 * @host_fdt: host device tree blob from which info are retrieved
131 * @guest_fdt: guest device tree blob where the clock node is added
132 * @host_phandle: phandle of the clock in host device tree
133 * @guest_phandle: phandle to assign to the guest node
135 static void fdt_build_clock_node(void *host_fdt, void *guest_fdt,
136 uint32_t host_phandle,
137 uint32_t guest_phandle)
139 char *node_path = NULL;
140 char *nodename;
141 const void *r;
142 int ret, node_offset, prop_len, path_len = 16;
144 node_offset = fdt_node_offset_by_phandle(host_fdt, host_phandle);
145 if (node_offset <= 0) {
146 error_report("not able to locate clock handle %d in host device tree",
147 host_phandle);
148 exit(1);
150 node_path = g_malloc(path_len);
151 while ((ret = fdt_get_path(host_fdt, node_offset, node_path, path_len))
152 == -FDT_ERR_NOSPACE) {
153 path_len += 16;
154 node_path = g_realloc(node_path, path_len);
156 if (ret < 0) {
157 error_report("not able to retrieve node path for clock handle %d",
158 host_phandle);
159 exit(1);
162 r = qemu_fdt_getprop(host_fdt, node_path, "compatible", &prop_len,
163 &error_fatal);
164 if (strcmp(r, "fixed-clock")) {
165 error_report("clock handle %d is not a fixed clock", host_phandle);
166 exit(1);
169 nodename = strrchr(node_path, '/');
170 qemu_fdt_add_subnode(guest_fdt, nodename);
172 copy_properties_from_host(clock_copied_properties,
173 ARRAY_SIZE(clock_copied_properties),
174 host_fdt, guest_fdt,
175 node_path, nodename);
177 qemu_fdt_setprop_cell(guest_fdt, nodename, "phandle", guest_phandle);
179 g_free(node_path);
183 * sysfs_to_dt_name: convert the name found in sysfs into the node name
184 * for instance e0900000.xgmac is converted into xgmac@e0900000
185 * @sysfs_name: directory name in sysfs
187 * returns the device tree name upon success or NULL in case the sysfs name
188 * does not match the expected format
190 static char *sysfs_to_dt_name(const char *sysfs_name)
192 gchar **substrings = g_strsplit(sysfs_name, ".", 2);
193 char *dt_name = NULL;
195 if (!substrings || !substrings[0] || !substrings[1]) {
196 goto out;
198 dt_name = g_strdup_printf("%s@%s", substrings[1], substrings[0]);
199 out:
200 g_strfreev(substrings);
201 return dt_name;
204 /* Device Specific Code */
207 * add_calxeda_midway_xgmac_fdt_node
209 * Generates a simple node with following properties:
210 * compatible string, regs, interrupts, dma-coherent
212 static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
214 PlatformBusFDTData *data = opaque;
215 PlatformBusDevice *pbus = data->pbus;
216 void *fdt = data->fdt;
217 const char *parent_node = data->pbus_node_name;
218 int compat_str_len, i;
219 char *nodename;
220 uint32_t *irq_attr, *reg_attr;
221 uint64_t mmio_base, irq_number;
222 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
223 VFIODevice *vbasedev = &vdev->vbasedev;
225 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
226 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
227 vbasedev->name, mmio_base);
228 qemu_fdt_add_subnode(fdt, nodename);
230 compat_str_len = strlen(vdev->compat) + 1;
231 qemu_fdt_setprop(fdt, nodename, "compatible",
232 vdev->compat, compat_str_len);
234 qemu_fdt_setprop(fdt, nodename, "dma-coherent", "", 0);
236 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
237 for (i = 0; i < vbasedev->num_regions; i++) {
238 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
239 reg_attr[2 * i] = cpu_to_be32(mmio_base);
240 reg_attr[2 * i + 1] = cpu_to_be32(
241 memory_region_size(vdev->regions[i]->mem));
243 qemu_fdt_setprop(fdt, nodename, "reg", reg_attr,
244 vbasedev->num_regions * 2 * sizeof(uint32_t));
246 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
247 for (i = 0; i < vbasedev->num_irqs; i++) {
248 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
249 + data->irq_start;
250 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
251 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
252 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
254 qemu_fdt_setprop(fdt, nodename, "interrupts",
255 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
256 g_free(irq_attr);
257 g_free(reg_attr);
258 g_free(nodename);
259 return 0;
262 /* AMD xgbe properties whose values are copied/pasted from host */
263 static HostProperty amd_xgbe_copied_properties[] = {
264 {"compatible", false},
265 {"dma-coherent", true},
266 {"amd,per-channel-interrupt", true},
267 {"phy-mode", false},
268 {"mac-address", true},
269 {"amd,speed-set", false},
270 {"amd,serdes-blwc", true},
271 {"amd,serdes-cdr-rate", true},
272 {"amd,serdes-pq-skew", true},
273 {"amd,serdes-tx-amp", true},
274 {"amd,serdes-dfe-tap-config", true},
275 {"amd,serdes-dfe-tap-enable", true},
276 {"clock-names", false},
280 * add_amd_xgbe_fdt_node
282 * Generates the combined xgbe/phy node following kernel >=4.2
283 * binding documentation:
284 * Documentation/devicetree/bindings/net/amd-xgbe.txt:
285 * Also 2 clock nodes are created (dma and ptp)
287 * Asserts in case of error
289 static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque)
291 PlatformBusFDTData *data = opaque;
292 PlatformBusDevice *pbus = data->pbus;
293 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
294 VFIODevice *vbasedev = &vdev->vbasedev;
295 VFIOINTp *intp;
296 const char *parent_node = data->pbus_node_name;
297 char **node_path, *nodename, *dt_name;
298 void *guest_fdt = data->fdt, *host_fdt;
299 const void *r;
300 int i, prop_len;
301 uint32_t *irq_attr, *reg_attr, *host_clock_phandles;
302 uint64_t mmio_base, irq_number;
303 uint32_t guest_clock_phandles[2];
305 host_fdt = load_device_tree_from_sysfs();
307 dt_name = sysfs_to_dt_name(vbasedev->name);
308 if (!dt_name) {
309 error_report("%s incorrect sysfs device name %s",
310 __func__, vbasedev->name);
311 exit(1);
313 node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
314 &error_fatal);
315 if (!node_path || !node_path[0]) {
316 error_report("%s unable to retrieve node path for %s/%s",
317 __func__, dt_name, vdev->compat);
318 exit(1);
321 if (node_path[1]) {
322 error_report("%s more than one node matching %s/%s!",
323 __func__, dt_name, vdev->compat);
324 exit(1);
327 g_free(dt_name);
329 if (vbasedev->num_regions != 5) {
330 error_report("%s Does the host dt node combine XGBE/PHY?", __func__);
331 exit(1);
334 /* generate nodes for DMA_CLK and PTP_CLK */
335 r = qemu_fdt_getprop(host_fdt, node_path[0], "clocks",
336 &prop_len, &error_fatal);
337 if (prop_len != 8) {
338 error_report("%s clocks property should contain 2 handles", __func__);
339 exit(1);
341 host_clock_phandles = (uint32_t *)r;
342 guest_clock_phandles[0] = qemu_fdt_alloc_phandle(guest_fdt);
343 guest_clock_phandles[1] = qemu_fdt_alloc_phandle(guest_fdt);
346 * clock handles fetched from host dt are in be32 layout whereas
347 * rest of the code uses cpu layout. Also guest clock handles are
348 * in cpu layout.
350 fdt_build_clock_node(host_fdt, guest_fdt,
351 be32_to_cpu(host_clock_phandles[0]),
352 guest_clock_phandles[0]);
354 fdt_build_clock_node(host_fdt, guest_fdt,
355 be32_to_cpu(host_clock_phandles[1]),
356 guest_clock_phandles[1]);
358 /* combined XGBE/PHY node */
359 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
360 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
361 vbasedev->name, mmio_base);
362 qemu_fdt_add_subnode(guest_fdt, nodename);
364 copy_properties_from_host(amd_xgbe_copied_properties,
365 ARRAY_SIZE(amd_xgbe_copied_properties),
366 host_fdt, guest_fdt,
367 node_path[0], nodename);
369 qemu_fdt_setprop_cells(guest_fdt, nodename, "clocks",
370 guest_clock_phandles[0],
371 guest_clock_phandles[1]);
373 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
374 for (i = 0; i < vbasedev->num_regions; i++) {
375 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
376 reg_attr[2 * i] = cpu_to_be32(mmio_base);
377 reg_attr[2 * i + 1] = cpu_to_be32(
378 memory_region_size(vdev->regions[i]->mem));
380 qemu_fdt_setprop(guest_fdt, nodename, "reg", reg_attr,
381 vbasedev->num_regions * 2 * sizeof(uint32_t));
383 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
384 for (i = 0; i < vbasedev->num_irqs; i++) {
385 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
386 + data->irq_start;
387 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
388 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
390 * General device interrupt and PCS auto-negotiation interrupts are
391 * level-sensitive while the 4 per-channel interrupts are edge
392 * sensitive
394 QLIST_FOREACH(intp, &vdev->intp_list, next) {
395 if (intp->pin == i) {
396 break;
399 if (intp->flags & VFIO_IRQ_INFO_AUTOMASKED) {
400 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
401 } else {
402 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
405 qemu_fdt_setprop(guest_fdt, nodename, "interrupts",
406 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
408 g_free(host_fdt);
409 g_strfreev(node_path);
410 g_free(irq_attr);
411 g_free(reg_attr);
412 g_free(nodename);
413 return 0;
416 /* DT compatible matching */
417 static bool vfio_platform_match(SysBusDevice *sbdev,
418 const BindingEntry *entry)
420 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
421 const char *compat;
422 unsigned int n;
424 for (n = vdev->num_compat, compat = vdev->compat; n > 0;
425 n--, compat += strlen(compat) + 1) {
426 if (!strcmp(entry->compat, compat)) {
427 return true;
431 return false;
434 #define VFIO_PLATFORM_BINDING(compat, add_fn) \
435 {TYPE_VFIO_PLATFORM, (compat), (add_fn), vfio_platform_match}
437 #endif /* CONFIG_LINUX */
439 static int no_fdt_node(SysBusDevice *sbdev, void *opaque)
441 return 0;
444 /* Device type based matching */
445 static bool type_match(SysBusDevice *sbdev, const BindingEntry *entry)
447 return !strcmp(object_get_typename(OBJECT(sbdev)), entry->typename);
450 #define TYPE_BINDING(type, add_fn) {(type), NULL, (add_fn), NULL}
452 /* list of supported dynamic sysbus bindings */
453 static const BindingEntry bindings[] = {
454 #ifdef CONFIG_LINUX
455 TYPE_BINDING(TYPE_VFIO_CALXEDA_XGMAC, add_calxeda_midway_xgmac_fdt_node),
456 TYPE_BINDING(TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node),
457 VFIO_PLATFORM_BINDING("amd,xgbe-seattle-v1a", add_amd_xgbe_fdt_node),
458 #endif
459 TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node),
460 TYPE_BINDING("", NULL), /* last element */
463 /* Generic Code */
466 * add_fdt_node - add the device tree node of a dynamic sysbus device
468 * @sbdev: handle to the sysbus device
469 * @opaque: handle to the PlatformBusFDTData
471 * Checks the sysbus type belongs to the list of device types that
472 * are dynamically instantiable and if so call the node creation
473 * function.
475 static void add_fdt_node(SysBusDevice *sbdev, void *opaque)
477 int i, ret;
479 for (i = 0; i < ARRAY_SIZE(bindings); i++) {
480 const BindingEntry *iter = &bindings[i];
482 if (type_match(sbdev, iter)) {
483 if (!iter->match_fn || iter->match_fn(sbdev, iter)) {
484 ret = iter->add_fn(sbdev, opaque);
485 assert(!ret);
486 return;
490 error_report("Device %s can not be dynamically instantiated",
491 qdev_fw_name(DEVICE(sbdev)));
492 exit(1);
495 void platform_bus_add_all_fdt_nodes(void *fdt, const char *intc, hwaddr addr,
496 hwaddr bus_size, int irq_start)
498 const char platcomp[] = "qemu,platform\0simple-bus";
499 PlatformBusDevice *pbus;
500 DeviceState *dev;
501 gchar *node;
503 assert(fdt);
505 node = g_strdup_printf("/platform@%"PRIx64, addr);
507 /* Create a /platform node that we can put all devices into */
508 qemu_fdt_add_subnode(fdt, node);
509 qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp));
511 /* Our platform bus region is less than 32bits, so 1 cell is enough for
512 * address and size
514 qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1);
515 qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1);
516 qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, bus_size);
518 qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc);
520 dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE);
521 pbus = PLATFORM_BUS_DEVICE(dev);
523 PlatformBusFDTData data = {
524 .fdt = fdt,
525 .irq_start = irq_start,
526 .pbus_node_name = node,
527 .pbus = pbus,
530 /* Loop through all dynamic sysbus devices and create their node */
531 foreach_dynamic_sysbus_device(add_fdt_node, &data);
533 g_free(node);