| 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/arm/smmuv3.h" |
| 35 | #include "hw/core/platform-bus.h" |
| 36 | #include "hw/display/ramfb.h" |
| 37 | #include "hw/uefi/var-service-api.h" |
| 38 | #include "hw/arm/fdt.h" |
| 39 | #include "hw/watchdog/sbsa_gwdt.h" |
| 40 | |
| 41 | /* |
| 42 | * internal struct that contains the information to create dynamic |
| 43 | * sysbus device node |
| 44 | */ |
| 45 | typedef struct PlatformBusFDTData { |
| 46 | void *fdt; /* device tree handle */ |
| 47 | int irq_start; /* index of the first IRQ usable by platform bus devices */ |
| 48 | const char *pbus_node_name; /* name of the platform bus node */ |
| 49 | PlatformBusDevice *pbus; |
| 50 | } PlatformBusFDTData; |
| 51 | |
| 52 | /* struct that allows to match a device and create its FDT node */ |
| 53 | typedef struct BindingEntry { |
| 54 | const char *typename; |
| 55 | const char *compat; |
| 56 | int (*add_fn)(SysBusDevice *sbdev, void *opaque); |
| 57 | bool (*match_fn)(SysBusDevice *sbdev, const struct BindingEntry *combo); |
| 58 | } BindingEntry; |
| 59 | |
| 60 | /* helpers */ |
| 61 | |
| 62 | typedef struct HostProperty { |
| 63 | const char *name; |
| 64 | bool optional; |
| 65 | } HostProperty; |
| 66 | |
| 67 | #ifdef CONFIG_TPM |
| 68 | /* |
| 69 | * add_tpm_tis_fdt_node: Create a DT node for TPM TIS |
| 70 | * |
| 71 | * See kernel documentation: |
| 72 | * Documentation/devicetree/bindings/security/tpm/tpm_tis_mmio.txt |
| 73 | * Optional interrupt for command completion is not exposed |
| 74 | */ |
| 75 | static int add_tpm_tis_fdt_node(SysBusDevice *sbdev, void *opaque) |
| 76 | { |
| 77 | PlatformBusFDTData *data = opaque; |
| 78 | PlatformBusDevice *pbus = data->pbus; |
| 79 | void *fdt = data->fdt; |
| 80 | const char *parent_node = data->pbus_node_name; |
| 81 | char *nodename; |
| 82 | uint32_t reg_attr[2]; |
| 83 | uint64_t mmio_base; |
| 84 | |
| 85 | mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0); |
| 86 | nodename = g_strdup_printf("%s/tpm_tis@%" PRIx64, parent_node, mmio_base); |
| 87 | qemu_fdt_add_subnode(fdt, nodename); |
| 88 | |
| 89 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "tcg,tpm-tis-mmio"); |
| 90 | |
| 91 | reg_attr[0] = cpu_to_be32(mmio_base); |
| 92 | reg_attr[1] = cpu_to_be32(0x5000); |
| 93 | qemu_fdt_setprop(fdt, nodename, "reg", reg_attr, 2 * sizeof(uint32_t)); |
| 94 | |
| 95 | g_free(nodename); |
| 96 | return 0; |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | static int add_uefi_vars_node(SysBusDevice *sbdev, void *opaque) |
| 101 | { |
| 102 | PlatformBusFDTData *data = opaque; |
| 103 | PlatformBusDevice *pbus = data->pbus; |
| 104 | const char *parent_node = data->pbus_node_name; |
| 105 | void *fdt = data->fdt; |
| 106 | uint64_t mmio_base; |
| 107 | char *nodename; |
| 108 | |
| 109 | mmio_base = platform_bus_get_mmio_addr(pbus, sbdev, 0); |
| 110 | nodename = g_strdup_printf("%s/%s@%" PRIx64, parent_node, |
| 111 | UEFI_VARS_FDT_NODE, mmio_base); |
| 112 | qemu_fdt_add_subnode(fdt, nodename); |
| 113 | qemu_fdt_setprop_string(fdt, nodename, |
| 114 | "compatible", UEFI_VARS_FDT_COMPAT); |
| 115 | qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", |
| 116 | 1, mmio_base, |
| 117 | 1, UEFI_VARS_REGS_SIZE); |
| 118 | g_free(nodename); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int no_fdt_node(SysBusDevice *sbdev, void *opaque) |
| 123 | { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* Device type based matching */ |
| 128 | static bool type_match(SysBusDevice *sbdev, const BindingEntry *entry) |
| 129 | { |
| 130 | return !strcmp(object_get_typename(OBJECT(sbdev)), entry->typename); |
| 131 | } |
| 132 | |
| 133 | #define TYPE_BINDING(type, add_fn) {(type), NULL, (add_fn), NULL} |
| 134 | |
| 135 | /* list of supported dynamic sysbus bindings */ |
| 136 | static const BindingEntry bindings[] = { |
| 137 | #ifdef CONFIG_TPM |
| 138 | TYPE_BINDING(TYPE_TPM_TIS_SYSBUS, add_tpm_tis_fdt_node), |
| 139 | #endif |
| 140 | /* No generic DT support for smmuv3 dev. Support added for arm virt only */ |
| 141 | TYPE_BINDING(TYPE_ARM_SMMUV3, no_fdt_node), |
| 142 | TYPE_BINDING(TYPE_RAMFB_DEVICE, no_fdt_node), |
| 143 | TYPE_BINDING(TYPE_UEFI_VARS_SYSBUS, add_uefi_vars_node), |
| 144 | TYPE_BINDING(TYPE_WDT_SBSA, no_fdt_node), |
| 145 | TYPE_BINDING("", NULL), /* last element */ |
| 146 | }; |
| 147 | |
| 148 | /* Generic Code */ |
| 149 | |
| 150 | /** |
| 151 | * add_fdt_node - add the device tree node of a dynamic sysbus device |
| 152 | * |
| 153 | * @sbdev: handle to the sysbus device |
| 154 | * @opaque: handle to the PlatformBusFDTData |
| 155 | * |
| 156 | * Checks the sysbus type belongs to the list of device types that |
| 157 | * are dynamically instantiable and if so call the node creation |
| 158 | * function. |
| 159 | */ |
| 160 | static void add_fdt_node(SysBusDevice *sbdev, void *opaque) |
| 161 | { |
| 162 | int i, ret; |
| 163 | |
| 164 | for (i = 0; i < ARRAY_SIZE(bindings); i++) { |
| 165 | const BindingEntry *iter = &bindings[i]; |
| 166 | |
| 167 | if (type_match(sbdev, iter)) { |
| 168 | if (!iter->match_fn || iter->match_fn(sbdev, iter)) { |
| 169 | ret = iter->add_fn(sbdev, opaque); |
| 170 | assert(!ret); |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | error_report("Device %s can not be dynamically instantiated", |
| 176 | qdev_fw_name(DEVICE(sbdev))); |
| 177 | exit(1); |
| 178 | } |
| 179 | |
| 180 | void platform_bus_add_all_fdt_nodes(void *fdt, const char *intc, hwaddr addr, |
| 181 | hwaddr bus_size, int irq_start) |
| 182 | { |
| 183 | const char platcomp[] = "qemu,platform\0simple-bus"; |
| 184 | PlatformBusDevice *pbus; |
| 185 | DeviceState *dev; |
| 186 | gchar *node; |
| 187 | |
| 188 | assert(fdt); |
| 189 | |
| 190 | node = g_strdup_printf("/platform-bus@%"PRIx64, addr); |
| 191 | |
| 192 | /* Create a /platform node that we can put all devices into */ |
| 193 | qemu_fdt_add_subnode(fdt, node); |
| 194 | qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp)); |
| 195 | |
| 196 | /* Our platform bus region is less than 32bits, so 1 cell is enough for |
| 197 | * address and size |
| 198 | */ |
| 199 | qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1); |
| 200 | qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1); |
| 201 | qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, bus_size); |
| 202 | |
| 203 | qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", intc); |
| 204 | |
| 205 | dev = qdev_find_recursive(sysbus_get_default(), TYPE_PLATFORM_BUS_DEVICE); |
| 206 | pbus = PLATFORM_BUS_DEVICE(dev); |
| 207 | |
| 208 | PlatformBusFDTData data = { |
| 209 | .fdt = fdt, |
| 210 | .irq_start = irq_start, |
| 211 | .pbus_node_name = node, |
| 212 | .pbus = pbus, |
| 213 | }; |
| 214 | |
| 215 | /* Loop through all dynamic sysbus devices and create their node */ |
| 216 | foreach_dynamic_sysbus_device(add_fdt_node, &data); |
| 217 | |
| 218 | g_free(node); |
| 219 | } |