1 /* 2 * ARM Platform Bus device tree generation helpers 3 * 4 * Copyright (c) 2014 Linaro Limited 5 * 6 * Authors: 7 * Alex Graf <agraf@suse.de> 8 * Eric Auger <eric.auger@linaro.org> 9 * 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. 13 * 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. 18 * 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/>. 21 * 22 */ 23 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/core/sysbus-fdt.h" 31 #include "qemu/error-report.h" 32 #include "system/device_tree.h" 33 #include "system/tpm.h" 34 #include "hw/platform-bus.h" 35 #include "hw/vfio/vfio-platform.h" 36 #include "hw/vfio/vfio-calxeda-xgmac.h" 37 #include "hw/vfio/vfio-amd-xgbe.h" 38 #include "hw/vfio/vfio-region.h" 39 #include "hw/display/ramfb.h" 40 #include "hw/uefi/var-service-api.h" 41 #include "hw/arm/fdt.h" 42 43 /* 44 * internal struct that contains the information to create dynamic 45 * sysbus device node 46 */ 47 typedef struct PlatformBusFDTData { 48 void *fdt; /* device tree handle */ 49 int irq_start; /* index of the first IRQ usable by platform bus devices */ 50 const char *pbus_node_name; /* name of the platform bus node */ 51 PlatformBusDevice *pbus; 52 } PlatformBusFDTData; 53 54 /* struct that allows to match a device and create its FDT node */ 55 typedef struct BindingEntry { 56 const char *typename; 57 const char *compat; 58 int (*add_fn)(SysBusDevice *sbdev, void *opaque); 59 bool (*match_fn)(SysBusDevice *sbdev, const struct BindingEntry *combo); 60 } BindingEntry; 61 62 /* helpers */ 63 64 typedef struct HostProperty { 65 const char *name; 66 bool optional; 67 } HostProperty; 68 69 #ifdef CONFIG_LINUX 70 71 /** 72 * copy_properties_from_host 73 * 74 * copies properties listed in an array from host device tree to 75 * guest device tree. If a non optional property is not found, the 76 * function asserts. An optional property is ignored if not found 77 * in the host device tree. 78 * @props: array of HostProperty to copy 79 * @nb_props: number of properties in the array 80 * @host_dt: host device tree blob 81 * @guest_dt: guest device tree blob 82 * @node_path: host dt node path where the property is supposed to be 83 found 84 * @nodename: guest node name the properties should be added to 85 */ 86 static void copy_properties_from_host(HostProperty *props, int nb_props, 87 void *host_fdt, void *guest_fdt, 88 char *node_path, char *nodename) 89 { 90 int i, prop_len; 91 const void *r; 92 Error *err = NULL; 93 94 for (i = 0; i < nb_props; i++) { 95 r = qemu_fdt_getprop(host_fdt, node_path, 96 props[i].name, 97 &prop_len, 98 &err); 99 if (r) { 100 qemu_fdt_setprop(guest_fdt, nodename, 101 props[i].name, r, prop_len); 102 } else { 103 if (props[i].optional && prop_len == -FDT_ERR_NOTFOUND) { 104 /* optional property does not exist */ 105 error_free(err); 106 } else { 107 error_report_err(err); 108 } 109 if (!props[i].optional) { 110 /* mandatory property not found: bail out */ 111 exit(1); 112 } 113 err = NULL; 114 } 115 } 116 } 117 118 /* clock properties whose values are copied/pasted from host */ 119 static HostProperty clock_copied_properties[] = { 120 {"compatible", false}, 121 {"#clock-cells", false}, 122 {"clock-frequency", true}, 123 {"clock-output-names", true}, 124 }; 125 126 /** 127 * fdt_build_clock_node 128 * 129 * Build a guest clock node, used as a dependency from a passthrough'ed 130 * device. Most information are retrieved from the host clock node. 131 * Also check the host clock is a fixed one. 132 * 133 * @host_fdt: host device tree blob from which info are retrieved 134 * @guest_fdt: guest device tree blob where the clock node is added 135 * @host_phandle: phandle of the clock in host device tree 136 * @guest_phandle: phandle to assign to the guest node 137 */ 138 static void fdt_build_clock_node(void *host_fdt, void *guest_fdt, 139 uint32_t host_phandle, 140 uint32_t guest_phandle) 141 { 142 char *node_path = NULL; 143 char *nodename; 144 const void *r; 145 int ret, node_offset, prop_len, path_len = 16; 146 147 node_offset = fdt_node_offset_by_phandle(host_fdt, host_phandle); 148 if (node_offset <= 0) { 149 error_report("not able to locate clock handle %d in host device tree", 150 host_phandle); 151 exit(1); 152 } 153 node_path = g_malloc(path_len); 154 while ((ret = fdt_get_path(host_fdt, node_offset, node_path, path_len)) 155 == -FDT_ERR_NOSPACE) { 156 path_len += 16; 157 node_path = g_realloc(node_path, path_len); 158 } 159 if (ret < 0) { 160 error_report("not able to retrieve node path for clock handle %d", 161 host_phandle); 162 exit(1); 163 } 164 165 r = qemu_fdt_getprop(host_fdt, node_path, "compatible", &prop_len, 166 &error_fatal); 167 if (strcmp(r, "fixed-clock")) { 168 error_report("clock handle %d is not a fixed clock", host_phandle); 169 exit(1); 170 } 171 172 nodename = strrchr(node_path, '/'); 173 qemu_fdt_add_subnode(guest_fdt, nodename); 174 175 copy_properties_from_host(clock_copied_properties, 176 ARRAY_SIZE(clock_copied_properties), 177 host_fdt, guest_fdt, 178 node_path, nodename); 179 180 qemu_fdt_setprop_cell(guest_fdt, nodename, "phandle", guest_phandle); 181 182 g_free(node_path); 183 } 184 185 /** 186 * sysfs_to_dt_name: convert the name found in sysfs into the node name 187 * for instance e0900000.xgmac is converted into xgmac@e0900000 188 * @sysfs_name: directory name in sysfs 189 * 190 * returns the device tree name upon success or NULL in case the sysfs name 191 * does not match the expected format 192 */ 193 static char *sysfs_to_dt_name(const char *sysfs_name) 194 { 195 gchar **substrings = g_strsplit(sysfs_name, ".", 2); 196 char *dt_name = NULL; 197 198 if (!substrings || !substrings[0] || !substrings[1]) { 199 goto out; 200 } 201 dt_name = g_strdup_printf("%s@%s", substrings[1], substrings[0]); 202 out: 203 g_strfreev(substrings); 204 return dt_name; 205 } 206 207 /* Device Specific Code */ 208 209 /** 210 * add_calxeda_midway_xgmac_fdt_node 211 * 212 * Generates a simple node with following properties: 213 * compatible string, regs, interrupts, dma-coherent 214 */ 215 static int add_calxeda_midway_xgmac_fdt_node(SysBusDevice *sbdev, void *opaque) 216 { 217 PlatformBusFDTData *data = opaque; 218 PlatformBusDevice *pbus = data->pbus; 219 void *fdt = data->fdt; 220 const char *parent_node = data->pbus_node_name; 221 int compat_str_len, i; 222 char *nodename; 223 uint32_t *irq_attr, *reg_attr; 224 uint64_t mmio_base, irq_number; 225 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev); 226 VFIODevice *vbasedev = &vdev->vbasedev; 227 228 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0); 229 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node, 230 vbasedev->name, mmio_base); 231 qemu_fdt_add_subnode(fdt, nodename); 232 233 compat_str_len = strlen(vdev->compat) + 1; 234 qemu_fdt_setprop(fdt, nodename, "compatible", 235 vdev->compat, compat_str_len); 236 237 qemu_fdt_setprop(fdt, nodename, "dma-coherent", "", 0); 238 239 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2); 240 for (i = 0; i < vbasedev->num_regions; i++) { 241 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i); 242 reg_attr[2 * i] = cpu_to_be32(mmio_base); 243 reg_attr[2 * i + 1] = cpu_to_be32( 244 memory_region_size(vdev->regions[i]->mem)); 245 } 246 qemu_fdt_setprop(fdt, nodename, "reg", reg_attr, 247 vbasedev->num_regions * 2 * sizeof(uint32_t)); 248 249 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3); 250 for (i = 0; i < vbasedev->num_irqs; i++) { 251 irq_number = platform_bus_get_irqn(pbus, sbdev , i) 252 + data->irq_start; 253 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI); 254 irq_attr[3 * i + 1] = cpu_to_be32(irq_number); 255 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI); 256 } 257 qemu_fdt_setprop(fdt, nodename, "interrupts", 258 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t)); 259 g_free(irq_attr); 260 g_free(reg_attr); 261 g_free(nodename); 262 return 0; 263 } 264 265 /* AMD xgbe properties whose values are copied/pasted from host */ 266 static HostProperty amd_xgbe_copied_properties[] = { 267 {"compatible", false}, 268 {"dma-coherent", true}, 269 {"amd,per-channel-interrupt", true}, 270 {"phy-mode", false}, 271 {"mac-address", true}, 272 {"amd,speed-set", false}, 273 {"amd,serdes-blwc", true}, 274 {"amd,serdes-cdr-rate", true}, 275 {"amd,serdes-pq-skew", true}, 276 {"amd,serdes-tx-amp", true}, 277 {"amd,serdes-dfe-tap-config", true}, 278 {"amd,serdes-dfe-tap-enable", true}, 279 {"clock-names", false}, 280 }; 281 282 /** 283 * add_amd_xgbe_fdt_node 284 * 285 * Generates the combined xgbe/phy node following kernel >=4.2 286 * binding documentation: 287 * Documentation/devicetree/bindings/net/amd-xgbe.txt: 288 * Also 2 clock nodes are created (dma and ptp) 289 * 290 * Asserts in case of error 291 */ 292 static int add_amd_xgbe_fdt_node(SysBusDevice *sbdev, void *opaque) 293 { 294 PlatformBusFDTData *data = opaque; 295 PlatformBusDevice *pbus = data->pbus; 296 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev); 297 VFIODevice *vbasedev = &vdev->vbasedev; 298 VFIOINTp *intp; 299 const char *parent_node = data->pbus_node_name; 300 char **node_path, *nodename, *dt_name; 301 void *guest_fdt = data->fdt, *host_fdt; 302 const void *r; 303 int i, prop_len; 304 uint32_t *irq_attr, *reg_attr; 305 const uint32_t *host_clock_phandles; 306 uint64_t mmio_base, irq_number; 307 uint32_t guest_clock_phandles[2]; 308 309 host_fdt = load_device_tree_from_sysfs(); 310 311 dt_name = sysfs_to_dt_name(vbasedev->name); 312 if (!dt_name) { 313 error_report("%s incorrect sysfs device name %s", 314 __func__, vbasedev->name); 315 exit(1); 316 } 317 node_path = qemu_fdt_node_path(host_fdt, dt_name, vdev->compat, 318 &error_fatal); 319 if (!node_path || !node_path[0]) { 320 error_report("%s unable to retrieve node path for %s/%s", 321 __func__, dt_name, vdev->compat); 322 exit(1); 323 } 324 325 if (node_path[1]) { 326 error_report("%s more than one node matching %s/%s!", 327 __func__, dt_name, vdev->compat); 328 exit(1); 329 } 330 331 g_free(dt_name); 332 333 if (vbasedev->num_regions != 5) { 334 error_report("%s Does the host dt node combine XGBE/PHY?", __func__); 335 exit(1); 336 } 337 338 /* generate nodes for DMA_CLK and PTP_CLK */ 339 r = qemu_fdt_getprop(host_fdt, node_path[0], "clocks", 340 &prop_len, &error_fatal); 341 if (prop_len != 8) { 342 error_report("%s clocks property should contain 2 handles", __func__); 343 exit(1); 344 } 345 host_clock_phandles = r; 346 guest_clock_phandles[0] = qemu_fdt_alloc_phandle(guest_fdt); 347 guest_clock_phandles[1] = qemu_fdt_alloc_phandle(guest_fdt); 348 349 /** 350 * clock handles fetched from host dt are in be32 layout whereas 351 * rest of the code uses cpu layout. Also guest clock handles are 352 * in cpu layout. 353 */ 354 fdt_build_clock_node(host_fdt, guest_fdt, 355 be32_to_cpu(host_clock_phandles[0]), 356 guest_clock_phandles[0]); 357 358 fdt_build_clock_node(host_fdt, guest_fdt, 359 be32_to_cpu(host_clock_phandles[1]), 360 guest_clock_phandles[1]); 361 362 /* combined XGBE/PHY node */ 363 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0); 364 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node, 365 vbasedev->name, mmio_base); 366 qemu_fdt_add_subnode(guest_fdt, nodename); 367 368 copy_properties_from_host(amd_xgbe_copied_properties, 369 ARRAY_SIZE(amd_xgbe_copied_properties), 370 host_fdt, guest_fdt, 371 node_path[0], nodename); 372 373 qemu_fdt_setprop_cells(guest_fdt, nodename, "clocks", 374 guest_clock_phandles[0], 375 guest_clock_phandles[1]); 376 377 reg_attr = g_new(uint32_t, vbasedev->num_regions * 2); 378 for (i = 0; i < vbasedev->num_regions; i++) { 379 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, i); 380 reg_attr[2 * i] = cpu_to_be32(mmio_base); 381 reg_attr[2 * i + 1] = cpu_to_be32( 382 memory_region_size(vdev->regions[i]->mem)); 383 } 384 qemu_fdt_setprop(guest_fdt, nodename, "reg", reg_attr, 385 vbasedev->num_regions * 2 * sizeof(uint32_t)); 386 387 irq_attr = g_new(uint32_t, vbasedev->num_irqs * 3); 388 for (i = 0; i < vbasedev->num_irqs; i++) { 389 irq_number = platform_bus_get_irqn(pbus, sbdev , i) 390 + data->irq_start; 391 irq_attr[3 * i] = cpu_to_be32(GIC_FDT_IRQ_TYPE_SPI); 392 irq_attr[3 * i + 1] = cpu_to_be32(irq_number); 393 /* 394 * General device interrupt and PCS auto-negotiation interrupts are 395 * level-sensitive while the 4 per-channel interrupts are edge 396 * sensitive 397 */ 398 QLIST_FOREACH(intp, &vdev->intp_list, next) { 399 if (intp->pin == i) { 400 break; 401 } 402 } 403 if (intp->flags & VFIO_IRQ_INFO_AUTOMASKED) { 404 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_LEVEL_HI); 405 } else { 406 irq_attr[3 * i + 2] = cpu_to_be32(GIC_FDT_IRQ_FLAGS_EDGE_LO_HI); 407 } 408 } 409 qemu_fdt_setprop(guest_fdt, nodename, "interrupts", 410 irq_attr, vbasedev->num_irqs * 3 * sizeof(uint32_t)); 411 412 g_free(host_fdt); 413 g_strfreev(node_path); 414 g_free(irq_attr); 415 g_free(reg_attr); 416 g_free(nodename); 417 return 0; 418 } 419 420 /* DT compatible matching */ 421 static bool vfio_platform_match(SysBusDevice *sbdev, 422 const BindingEntry *entry) 423 { 424 VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(sbdev); 425 const char *compat; 426 unsigned int n; 427 428 for (n = vdev->num_compat, compat = vdev->compat; n > 0; 429 n--, compat += strlen(compat) + 1) { 430 if (!strcmp(entry->compat, compat)) { 431 return true; 432 } 433 } 434 435 return false; 436 } 437 438 #define VFIO_PLATFORM_BINDING(compat, add_fn) \ 439 {TYPE_VFIO_PLATFORM, (compat), (add_fn), vfio_platform_match} 440 441 #endif /* CONFIG_LINUX */ 442 443 #ifdef CONFIG_TPM 444 /* 445 * add_tpm_tis_fdt_node: Create a DT node for TPM TIS 446 * 447 * See kernel documentation: 448 * Documentation/devicetree/bindings/security/tpm/tpm_tis_mmio.txt 449 * Optional interrupt for command completion is not exposed 450 */ 451 static int add_tpm_tis_fdt_node(SysBusDevice *sbdev, void *opaque) 452 { 453 PlatformBusFDTData *data = opaque; 454 PlatformBusDevice *pbus = data->pbus; 455 void *fdt = data->fdt; 456 const char *parent_node = data->pbus_node_name; 457 char *nodename; 458 uint32_t reg_attr[2]; 459 uint64_t mmio_base; 460 461 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0); 462 nodename = g_strdup_printf("%s/tpm_tis@%" PRIx64, parent_node, mmio_base); 463 qemu_fdt_add_subnode(fdt, nodename); 464 465 qemu_fdt_setprop_string(fdt, nodename, "compatible", "tcg,tpm-tis-mmio"); 466 467 reg_attr[0] = cpu_to_be32(mmio_base); 468 reg_attr[1] = cpu_to_be32(0x5000); 469 qemu_fdt_setprop(fdt, nodename, "reg", reg_attr, 2 * sizeof(uint32_t)); 470 471 g_free(nodename); 472 return 0; 473 } 474 #endif 475 476 static int add_uefi_vars_node(SysBusDevice *sbdev, void *opaque) 477 { 478 PlatformBusFDTData *data = opaque; 479 PlatformBusDevice *pbus = data->pbus; 480 const char *parent_node = data->pbus_node_name; 481 void *fdt = data->fdt; 482 uint64_t mmio_base; 483 char *nodename; 484 485 mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0); 486 nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node, 487 UEFI_VARS_FDT_NODE, mmio_base); 488 qemu_fdt_add_subnode(fdt, nodename); 489 qemu_fdt_setprop_string(fdt, nodename, 490 "compatible", UEFI_VARS_FDT_COMPAT); 491 qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", 492 1, mmio_base, 493 1, UEFI_VARS_REGS_SIZE); 494 g_free(nodename); 495 return 0; 496 } 497 498 static int no_fdt_node(SysBusDevice *sbdev, void *opaque) 499 { 500 return 0; 501 } 502 503 /* Device type based matching */ 504 static bool type_match(SysBusDevice *sbdev, const BindingEntry *entry) 505 { 506 return !strcmp(object_get_typename(OBJECT(sbdev)), entry->typename); 507 } 508 509 #define TYPE_BINDING(type, add_fn) {(type), NULL, (add_fn), NULL} 510 511 /* list of supported dynamic sysbus bindings */ 512 static const BindingEntry bindings[] = { 513 #ifdef CONFIG_LINUX 514 TYPE_BINDING(TYPE_VFIO_CALXEDA_XGMAC, add_calxeda_midway_xgmac_fdt_node), 515 TYPE_BINDING(TYPE_VFIO_AMD_XGBE, add_amd_xgbe_fdt_node), 516 VFIO_PLATFORM_BINDING("amd,xgbe-seattle-v1a", add_amd_xgbe_fdt_node), 517 #endif 518 #ifdef CONFIG_TPM 519 TYPE_BINDING(TYPE_TPM_TIS_SYSBUS, add_tpm_tis_fdt_node), 520 #endif 521 TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node), 522 TYPE_BINDING(TYPE_UEFI_VARS_SYSBUS, add_uefi_vars_node), 523 TYPE_BINDING("", NULL), /* last element */ 524 }; 525 526 /* Generic Code */ 527 528 /** 529 * add_fdt_node - add the device tree node of a dynamic sysbus device 530 * 531 * @sbdev: handle to the sysbus device 532 * @opaque: handle to the PlatformBusFDTData 533 * 534 * Checks the sysbus type belongs to the list of device types that 535 * are dynamically instantiable and if so call the node creation 536 * function. 537 */ 538 static void add_fdt_node(SysBusDevice *sbdev, void *opaque) 539 { 540 int i, ret; 541 542 for (i = 0; i < ARRAY_SIZE(bindings); i++) { 543 const BindingEntry *iter = &bindings[i]; 544 545 if (type_match(sbdev, iter)) { 546 if (!iter->match_fn || iter->match_fn(sbdev, iter)) { 547 ret = iter->add_fn(sbdev, opaque); 548 assert(!ret); 549 return; 550 } 551 } 552 } 553 error_report("Device %s can not be dynamically instantiated", 554 qdev_fw_name(DEVICE(sbdev))); 555 exit(1); 556 } 557 558 void platform_bus_add_all_fdt_nodes(void *fdt, const char *intc, hwaddr addr, 559 hwaddr bus_size, int irq_start) 560 { 561 const char platcomp[] = "qemu,platform\0simple-bus"; 562 PlatformBusDevice *pbus; 563 DeviceState *dev; 564 gchar *node; 565 566 assert(fdt); 567 568 node = g_strdup_printf("/platform-bus@%"PRIx64, addr); 569 570 /* Create a /platform node that we can put all devices into */ 571 qemu_fdt_add_subnode(fdt, node); 572 qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp)); 573 574 /* Our platform bus region is less than 32bits, so 1 cell is enough for 575 * address and size 576 */ 577 qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1); 578 qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1); 579 qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, bus_size); 580 581 qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc); 582 583 dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE); 584 pbus = PLATFORM_BUS_DEVICE(dev); 585 586 PlatformBusFDTData data = { 587 .fdt = fdt, 588 .irq_start = irq_start, 589 .pbus_node_name = node, 590 .pbus = pbus, 591 }; 592 593 /* Loop through all dynamic sysbus devices and create their node */ 594 foreach_dynamic_sysbus_device(add_fdt_node, &data); 595 596 g_free(node); 597 } 598