hw/display: add standalone ramfb device
[qemu.git] / hw / arm / sysbus-fdt.c
blob277ed872e7c58c9f6a5cd19c186b71900928aebc
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 #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"
43 * internal struct that contains the information to create dynamic
44 * sysbus device node
46 typedef struct PlatformBusFDTData {
47 void *fdt; /* device tree handle */
48 int irq_start; /* index of the first IRQ usable by platform bus devices */
49 const char *pbus_node_name; /* name of the platform bus node */
50 PlatformBusDevice *pbus;
51 } PlatformBusFDTData;
53 /* struct that associates a device type name and a node creation function */
54 typedef struct NodeCreationPair {
55 const char *typename;
56 int (*add_fdt_node_fn)(SysBusDevice *sbdev, void *opaque);
57 } NodeCreationPair;
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 props[i].optional ? &err : &error_fatal);
96 if (r) {
97 qemu_fdt_setprop(guest_fdt, nodename,
98 props[i].name, r, prop_len);
99 } else {
100 if (prop_len != -FDT_ERR_NOTFOUND) {
101 /* optional property not returned although property exists */
102 error_report_err(err);
103 } else {
104 error_free(err);
110 /* clock properties whose values are copied/pasted from host */
111 static HostProperty clock_copied_properties[] = {
112 {"compatible", false},
113 {"#clock-cells", false},
114 {"clock-frequency", true},
115 {"clock-output-names", true},
119 * fdt_build_clock_node
121 * Build a guest clock node, used as a dependency from a passthrough'ed
122 * device. Most information are retrieved from the host clock node.
123 * Also check the host clock is a fixed one.
125 * @host_fdt: host device tree blob from which info are retrieved
126 * @guest_fdt: guest device tree blob where the clock node is added
127 * @host_phandle: phandle of the clock in host device tree
128 * @guest_phandle: phandle to assign to the guest node
130 static void fdt_build_clock_node(void *host_fdt, void *guest_fdt,
131 uint32_t host_phandle,
132 uint32_t guest_phandle)
134 char *node_path = NULL;
135 char *nodename;
136 const void *r;
137 int ret, node_offset, prop_len, path_len = 16;
139 node_offset = fdt_node_offset_by_phandle(host_fdt, host_phandle);
140 if (node_offset <= 0) {
141 error_setg(&error_fatal,
142 "not able to locate clock handle %d in host device tree",
143 host_phandle);
145 node_path = g_malloc(path_len);
146 while ((ret = fdt_get_path(host_fdt, node_offset, node_path, path_len))
147 == -FDT_ERR_NOSPACE) {
148 path_len += 16;
149 node_path = g_realloc(node_path, path_len);
151 if (ret < 0) {
152 error_setg(&error_fatal,
153 "not able to retrieve node path for clock handle %d",
154 host_phandle);
157 r = qemu_fdt_getprop(host_fdt, node_path, "compatible", &prop_len,
158 &error_fatal);
159 if (strcmp(r, "fixed-clock")) {
160 error_setg(&error_fatal,
161 "clock handle %d is not a fixed clock", host_phandle);
164 nodename = strrchr(node_path, '/');
165 qemu_fdt_add_subnode(guest_fdt, nodename);
167 copy_properties_from_host(clock_copied_properties,
168 ARRAY_SIZE(clock_copied_properties),
169 host_fdt, guest_fdt,
170 node_path, nodename);
172 qemu_fdt_setprop_cell(guest_fdt, nodename, "phandle", guest_phandle);
174 g_free(node_path);
178 * sysfs_to_dt_name: convert the name found in sysfs into the node name
179 * for instance e0900000.xgmac is converted into xgmac@e0900000
180 * @sysfs_name: directory name in sysfs
182 * returns the device tree name upon success or NULL in case the sysfs name
183 * does not match the expected format
185 static char *sysfs_to_dt_name(const char *sysfs_name)
187 gchar **substrings = g_strsplit(sysfs_name, ".", 2);
188 char *dt_name = NULL;
190 if (!substrings || !substrings[0] || !substrings[1]) {
191 goto out;
193 dt_name = g_strdup_printf("%s@%s", substrings[1], substrings[0]);
194 out:
195 g_strfreev(substrings);
196 return dt_name;
199 /* Device Specific Code */
202 * add_calxeda_midway_xgmac_fdt_node
204 * Generates a simple node with following properties:
205 * compatible string, regs, interrupts, dma-coherent
207 static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque)
209 PlatformBusFDTData *data = opaque;
210 PlatformBusDevice *pbus = data->pbus;
211 void *fdt = data->fdt;
212 const char *parent_node = data->pbus_node_name;
213 int compat_str_len, i;
214 char *nodename;
215 uint32_t *irq_attr, *reg_attr;
216 uint64_t mmio_base, irq_number;
217 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
218 VFIODevice *vbasedev = &vdev->vbasedev;
220 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
221 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
222 vbasedev->name, mmio_base);
223 qemu_fdt_add_subnode(fdt, nodename);
225 compat_str_len = strlen(vdev->compat) + 1;
226 qemu_fdt_setprop(fdt, nodename, "compatible",
227 vdev->compat, compat_str_len);
229 qemu_fdt_setprop(fdt, nodename, "dma-coherent", "", 0);
231 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
232 for (i = 0; i < vbasedev->num_regions; i++) {
233 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
234 reg_attr[2 * i] = cpu_to_be32(mmio_base);
235 reg_attr[2 * i + 1] = cpu_to_be32(
236 memory_region_size(vdev->regions[i]->mem));
238 qemu_fdt_setprop(fdt, nodename, "reg", reg_attr,
239 vbasedev->num_regions * 2 * sizeof(uint32_t));
241 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
242 for (i = 0; i < vbasedev->num_irqs; i++) {
243 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
244 + data->irq_start;
245 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
246 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
247 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
249 qemu_fdt_setprop(fdt, nodename, "interrupts",
250 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
251 g_free(irq_attr);
252 g_free(reg_attr);
253 g_free(nodename);
254 return 0;
257 /* AMD xgbe properties whose values are copied/pasted from host */
258 static HostProperty amd_xgbe_copied_properties[] = {
259 {"compatible", false},
260 {"dma-coherent", true},
261 {"amd,per-channel-interrupt", true},
262 {"phy-mode", false},
263 {"mac-address", true},
264 {"amd,speed-set", false},
265 {"amd,serdes-blwc", true},
266 {"amd,serdes-cdr-rate", true},
267 {"amd,serdes-pq-skew", true},
268 {"amd,serdes-tx-amp", true},
269 {"amd,serdes-dfe-tap-config", true},
270 {"amd,serdes-dfe-tap-enable", true},
271 {"clock-names", false},
275 * add_amd_xgbe_fdt_node
277 * Generates the combined xgbe/phy node following kernel >=4.2
278 * binding documentation:
279 * Documentation/devicetree/bindings/net/amd-xgbe.txt:
280 * Also 2 clock nodes are created (dma and ptp)
282 * Asserts in case of error
284 static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque)
286 PlatformBusFDTData *data = opaque;
287 PlatformBusDevice *pbus = data->pbus;
288 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev);
289 VFIODevice *vbasedev = &vdev->vbasedev;
290 VFIOINTp *intp;
291 const char *parent_node = data->pbus_node_name;
292 char **node_path, *nodename, *dt_name;
293 void *guest_fdt = data->fdt, *host_fdt;
294 const void *r;
295 int i, prop_len;
296 uint32_t *irq_attr, *reg_attr, *host_clock_phandles;
297 uint64_t mmio_base, irq_number;
298 uint32_t guest_clock_phandles[2];
300 host_fdt = load_device_tree_from_sysfs();
302 dt_name = sysfs_to_dt_name(vbasedev->name);
303 if (!dt_name) {
304 error_setg(&error_fatal, "%s incorrect sysfs device name %s",
305 __func__, vbasedev->name);
307 node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat,
308 &error_fatal);
309 if (!node_path || !node_path[0]) {
310 error_setg(&error_fatal, "%s unable to retrieve node path for %s/%s",
311 __func__, dt_name, vdev->compat);
314 if (node_path[1]) {
315 error_setg(&error_fatal, "%s more than one node matching %s/%s!",
316 __func__, dt_name, vdev->compat);
319 g_free(dt_name);
321 if (vbasedev->num_regions != 5) {
322 error_setg(&error_fatal, "%s Does the host dt node combine XGBE/PHY?",
323 __func__);
326 /* generate nodes for DMA_CLK and PTP_CLK */
327 r = qemu_fdt_getprop(host_fdt, node_path[0], "clocks",
328 &prop_len, &error_fatal);
329 if (prop_len != 8) {
330 error_setg(&error_fatal, "%s clocks property should contain 2 handles",
331 __func__);
333 host_clock_phandles = (uint32_t *)r;
334 guest_clock_phandles[0] = qemu_fdt_alloc_phandle(guest_fdt);
335 guest_clock_phandles[1] = qemu_fdt_alloc_phandle(guest_fdt);
338 * clock handles fetched from host dt are in be32 layout whereas
339 * rest of the code uses cpu layout. Also guest clock handles are
340 * in cpu layout.
342 fdt_build_clock_node(host_fdt, guest_fdt,
343 be32_to_cpu(host_clock_phandles[0]),
344 guest_clock_phandles[0]);
346 fdt_build_clock_node(host_fdt, guest_fdt,
347 be32_to_cpu(host_clock_phandles[1]),
348 guest_clock_phandles[1]);
350 /* combined XGBE/PHY node */
351 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0);
352 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node,
353 vbasedev->name, mmio_base);
354 qemu_fdt_add_subnode(guest_fdt, nodename);
356 copy_properties_from_host(amd_xgbe_copied_properties,
357 ARRAY_SIZE(amd_xgbe_copied_properties),
358 host_fdt, guest_fdt,
359 node_path[0], nodename);
361 qemu_fdt_setprop_cells(guest_fdt, nodename, "clocks",
362 guest_clock_phandles[0],
363 guest_clock_phandles[1]);
365 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2);
366 for (i = 0; i < vbasedev->num_regions; i++) {
367 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i);
368 reg_attr[2 * i] = cpu_to_be32(mmio_base);
369 reg_attr[2 * i + 1] = cpu_to_be32(
370 memory_region_size(vdev->regions[i]->mem));
372 qemu_fdt_setprop(guest_fdt, nodename, "reg", reg_attr,
373 vbasedev->num_regions * 2 * sizeof(uint32_t));
375 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3);
376 for (i = 0; i < vbasedev->num_irqs; i++) {
377 irq_number = platform_bus_get_irqn(pbus, sbdev , i)
378 + data->irq_start;
379 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI);
380 irq_attr[3 * i + 1] = cpu_to_be32(irq_number);
382 * General device interrupt and PCS auto-negotiation interrupts are
383 * level-sensitive while the 4 per-channel interrupts are edge
384 * sensitive
386 QLIST_FOREACH(intp, &vdev->intp_list, next) {
387 if (intp->pin == i) {
388 break;
391 if (intp->flags & VFIO_IRQ_INFO_AUTOMASKED) {
392 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI);
393 } else {
394 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_EDGE_LO_HI);
397 qemu_fdt_setprop(guest_fdt, nodename, "interrupts",
398 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t));
400 g_free(host_fdt);
401 g_strfreev(node_path);
402 g_free(irq_attr);
403 g_free(reg_attr);
404 g_free(nodename);
405 return 0;
408 #endif /* CONFIG_LINUX */
410 static int no_fdt_node(SysBusDevice *sbdev, void *opaque)
412 return 0;
415 /* list of supported dynamic sysbus devices */
416 static const NodeCreationPair add_fdt_node_functions[] = {
417 #ifdef CONFIG_LINUX
418 {TYPE_VFIO_CALXEDA_XGMAC, add_calxeda_midway_xgmac_fdt_node},
419 {TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node},
420 #endif
421 {TYPE_RAMFB_DEVICE, no_fdt_node},
422 {"", NULL}, /* last element */
425 /* Generic Code */
428 * add_fdt_node - add the device tree node of a dynamic sysbus device
430 * @sbdev: handle to the sysbus device
431 * @opaque: handle to the PlatformBusFDTData
433 * Checks the sysbus type belongs to the list of device types that
434 * are dynamically instantiable and if so call the node creation
435 * function.
437 static void add_fdt_node(SysBusDevice *sbdev, void *opaque)
439 int i, ret;
441 for (i = 0; i < ARRAY_SIZE(add_fdt_node_functions); i++) {
442 if (!strcmp(object_get_typename(OBJECT(sbdev)),
443 add_fdt_node_functions[i].typename)) {
444 ret = add_fdt_node_functions[i].add_fdt_node_fn(sbdev, opaque);
445 assert(!ret);
446 return;
449 error_report("Device %s can not be dynamically instantiated",
450 qdev_fw_name(DEVICE(sbdev)));
451 exit(1);
454 void platform_bus_add_all_fdt_nodes(void *fdt, const char *intc, hwaddr addr,
455 hwaddr bus_size, int irq_start)
457 const char platcomp[] = "qemu,platform\0simple-bus";
458 PlatformBusDevice *pbus;
459 DeviceState *dev;
460 gchar *node;
462 assert(fdt);
464 node = g_strdup_printf("/platform@%"PRIx64, addr);
466 /* Create a /platform node that we can put all devices into */
467 qemu_fdt_add_subnode(fdt, node);
468 qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp));
470 /* Our platform bus region is less than 32bits, so 1 cell is enough for
471 * address and size
473 qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1);
474 qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1);
475 qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, bus_size);
477 qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc);
479 dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE);
480 pbus = PLATFORM_BUS_DEVICE(dev);
482 PlatformBusFDTData data = {
483 .fdt = fdt,
484 .irq_start = irq_start,
485 .pbus_node_name = node,
486 .pbus = pbus,
489 /* Loop through all dynamic sysbus devices and create their node */
490 foreach_dynamic_sysbus_device(add_fdt_node, &data);
492 g_free(node);