Rob Herring | af6074f | 2017-12-27 12:55:14 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 2 | #define pr_fmt(fmt) "OF: " fmt |
| 3 | |
Arnd Bergmann | 5019f0b | 2012-04-11 17:30:11 +0000 | [diff] [blame] | 4 | #include <linux/device.h> |
Gabriele Paoloni | fcfaab3 | 2018-03-15 02:15:52 +0800 | [diff] [blame] | 5 | #include <linux/fwnode.h> |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 6 | #include <linux/io.h> |
| 7 | #include <linux/ioport.h> |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 8 | #include <linux/logic_pio.h> |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 9 | #include <linux/module.h> |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 10 | #include <linux/of_address.h> |
Tomasz Nowicki | c5076cf | 2016-05-11 17:34:51 -0500 | [diff] [blame] | 11 | #include <linux/pci.h> |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 12 | #include <linux/pci_regs.h> |
Liviu Dudau | 41f8bba | 2014-09-29 15:29:21 +0100 | [diff] [blame] | 13 | #include <linux/sizes.h> |
| 14 | #include <linux/slab.h> |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 15 | #include <linux/string.h> |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 16 | |
Robin Murphy | b68ac8d | 2019-07-02 18:42:39 +0100 | [diff] [blame] | 17 | #include "of_private.h" |
| 18 | |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 19 | /* Max address size we deal with */ |
| 20 | #define OF_MAX_ADDR_CELLS 4 |
Stephen Warren | 5d61b16 | 2012-07-25 17:34:37 -0600 | [diff] [blame] | 21 | #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS) |
| 22 | #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 23 | |
| 24 | static struct of_bus *of_match_bus(struct device_node *np); |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 25 | static int __of_address_to_resource(struct device_node *dev, |
| 26 | const __be32 *addrp, u64 size, unsigned int flags, |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 27 | const char *name, struct resource *r); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 28 | |
| 29 | /* Debug utility */ |
| 30 | #ifdef DEBUG |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 31 | static void of_dump_addr(const char *s, const __be32 *addr, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 32 | { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 33 | pr_debug("%s", s); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 34 | while (na--) |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 35 | pr_cont(" %08x", be32_to_cpu(*(addr++))); |
| 36 | pr_cont("\n"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 37 | } |
| 38 | #else |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 39 | static void of_dump_addr(const char *s, const __be32 *addr, int na) { } |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | /* Callbacks for bus specific translators */ |
| 43 | struct of_bus { |
| 44 | const char *name; |
| 45 | const char *addresses; |
| 46 | int (*match)(struct device_node *parent); |
| 47 | void (*count_cells)(struct device_node *child, |
| 48 | int *addrc, int *sizec); |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 49 | u64 (*map)(__be32 *addr, const __be32 *range, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 50 | int na, int ns, int pna); |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 51 | int (*translate)(__be32 *addr, u64 offset, int na); |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 52 | bool has_flags; |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 53 | unsigned int (*get_flags)(const __be32 *addr); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * Default translator (generic bus) |
| 58 | */ |
| 59 | |
| 60 | static void of_bus_default_count_cells(struct device_node *dev, |
| 61 | int *addrc, int *sizec) |
| 62 | { |
| 63 | if (addrc) |
| 64 | *addrc = of_n_addr_cells(dev); |
| 65 | if (sizec) |
| 66 | *sizec = of_n_size_cells(dev); |
| 67 | } |
| 68 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 69 | static u64 of_bus_default_map(__be32 *addr, const __be32 *range, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 70 | int na, int ns, int pna) |
| 71 | { |
| 72 | u64 cp, s, da; |
| 73 | |
| 74 | cp = of_read_number(range, na); |
| 75 | s = of_read_number(range + na + pna, ns); |
| 76 | da = of_read_number(addr, na); |
| 77 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 78 | pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 79 | (unsigned long long)cp, (unsigned long long)s, |
| 80 | (unsigned long long)da); |
| 81 | |
| 82 | if (da < cp || da >= (cp + s)) |
| 83 | return OF_BAD_ADDR; |
| 84 | return da - cp; |
| 85 | } |
| 86 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 87 | static int of_bus_default_translate(__be32 *addr, u64 offset, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 88 | { |
| 89 | u64 a = of_read_number(addr, na); |
| 90 | memset(addr, 0, na * 4); |
| 91 | a += offset; |
| 92 | if (na > 1) |
Grant Likely | 154063a9 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 93 | addr[na - 2] = cpu_to_be32(a >> 32); |
| 94 | addr[na - 1] = cpu_to_be32(a & 0xffffffffu); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 99 | static unsigned int of_bus_default_get_flags(const __be32 *addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 100 | { |
| 101 | return IORESOURCE_MEM; |
| 102 | } |
| 103 | |
Jiaxun Yang | 0fc0ead | 2020-07-30 08:56:14 +0800 | [diff] [blame] | 104 | #ifdef CONFIG_PCI |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 105 | static unsigned int of_bus_pci_get_flags(const __be32 *addr) |
| 106 | { |
| 107 | unsigned int flags = 0; |
| 108 | u32 w = be32_to_cpup(addr); |
| 109 | |
| 110 | if (!IS_ENABLED(CONFIG_PCI)) |
| 111 | return 0; |
| 112 | |
| 113 | switch((w >> 24) & 0x03) { |
| 114 | case 0x01: |
| 115 | flags |= IORESOURCE_IO; |
| 116 | break; |
| 117 | case 0x02: /* 32 bits */ |
| 118 | case 0x03: /* 64 bits */ |
| 119 | flags |= IORESOURCE_MEM; |
| 120 | break; |
| 121 | } |
| 122 | if (w & 0x40000000) |
| 123 | flags |= IORESOURCE_PREFETCH; |
| 124 | return flags; |
| 125 | } |
| 126 | |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 127 | /* |
| 128 | * PCI bus specific translator |
| 129 | */ |
| 130 | |
| 131 | static int of_bus_pci_match(struct device_node *np) |
| 132 | { |
Benjamin Herrenschmidt | 6dd18e4 | 2013-07-03 16:01:10 +1000 | [diff] [blame] | 133 | /* |
Kleber Sacilotto de Souza | 14e2abb | 2014-02-03 13:31:03 -0200 | [diff] [blame] | 134 | * "pciex" is PCI Express |
Benjamin Herrenschmidt | 6dd18e4 | 2013-07-03 16:01:10 +1000 | [diff] [blame] | 135 | * "vci" is for the /chaos bridge on 1st-gen PCI powermacs |
| 136 | * "ht" is hypertransport |
| 137 | */ |
Rob Herring | e8b1dee2 | 2018-08-29 08:36:12 -0500 | [diff] [blame] | 138 | return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") || |
| 139 | of_node_is_type(np, "vci") || of_node_is_type(np, "ht"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void of_bus_pci_count_cells(struct device_node *np, |
| 143 | int *addrc, int *sizec) |
| 144 | { |
| 145 | if (addrc) |
| 146 | *addrc = 3; |
| 147 | if (sizec) |
| 148 | *sizec = 2; |
| 149 | } |
| 150 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 151 | static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 152 | int pna) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 153 | { |
| 154 | u64 cp, s, da; |
| 155 | unsigned int af, rf; |
| 156 | |
| 157 | af = of_bus_pci_get_flags(addr); |
| 158 | rf = of_bus_pci_get_flags(range); |
| 159 | |
| 160 | /* Check address type match */ |
| 161 | if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) |
| 162 | return OF_BAD_ADDR; |
| 163 | |
| 164 | /* Read address values, skipping high cell */ |
| 165 | cp = of_read_number(range + 1, na - 1); |
| 166 | s = of_read_number(range + na + pna, ns); |
| 167 | da = of_read_number(addr + 1, na - 1); |
| 168 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 169 | pr_debug("PCI map, cp=%llx, s=%llx, da=%llx\n", |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 170 | (unsigned long long)cp, (unsigned long long)s, |
| 171 | (unsigned long long)da); |
| 172 | |
| 173 | if (da < cp || da >= (cp + s)) |
| 174 | return OF_BAD_ADDR; |
| 175 | return da - cp; |
| 176 | } |
| 177 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 178 | static int of_bus_pci_translate(__be32 *addr, u64 offset, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 179 | { |
| 180 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 181 | } |
| 182 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 183 | const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 184 | unsigned int *flags) |
| 185 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 186 | const __be32 *prop; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 187 | unsigned int psize; |
| 188 | struct device_node *parent; |
| 189 | struct of_bus *bus; |
| 190 | int onesize, i, na, ns; |
| 191 | |
| 192 | /* Get parent & match bus type */ |
| 193 | parent = of_get_parent(dev); |
| 194 | if (parent == NULL) |
| 195 | return NULL; |
| 196 | bus = of_match_bus(parent); |
| 197 | if (strcmp(bus->name, "pci")) { |
| 198 | of_node_put(parent); |
| 199 | return NULL; |
| 200 | } |
| 201 | bus->count_cells(dev, &na, &ns); |
| 202 | of_node_put(parent); |
Stephen Warren | 5d61b16 | 2012-07-25 17:34:37 -0600 | [diff] [blame] | 203 | if (!OF_CHECK_ADDR_COUNT(na)) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 204 | return NULL; |
| 205 | |
| 206 | /* Get "reg" or "assigned-addresses" property */ |
| 207 | prop = of_get_property(dev, bus->addresses, &psize); |
| 208 | if (prop == NULL) |
| 209 | return NULL; |
| 210 | psize /= 4; |
| 211 | |
| 212 | onesize = na + ns; |
Grant Likely | 154063a9 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 213 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) { |
| 214 | u32 val = be32_to_cpu(prop[0]); |
| 215 | if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 216 | if (size) |
| 217 | *size = of_read_number(prop + na, ns); |
| 218 | if (flags) |
| 219 | *flags = bus->get_flags(prop); |
| 220 | return prop; |
| 221 | } |
Grant Likely | 154063a9 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 222 | } |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 223 | return NULL; |
| 224 | } |
| 225 | EXPORT_SYMBOL(of_get_pci_address); |
| 226 | |
| 227 | int of_pci_address_to_resource(struct device_node *dev, int bar, |
| 228 | struct resource *r) |
| 229 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 230 | const __be32 *addrp; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 231 | u64 size; |
| 232 | unsigned int flags; |
| 233 | |
| 234 | addrp = of_get_pci_address(dev, bar, &size, &flags); |
| 235 | if (addrp == NULL) |
| 236 | return -EINVAL; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 237 | return __of_address_to_resource(dev, addrp, size, flags, NULL, r); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL_GPL(of_pci_address_to_resource); |
Andrew Murray | 29b635c | 2013-05-16 17:55:17 +0200 | [diff] [blame] | 240 | |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 241 | /* |
| 242 | * of_pci_range_to_resource - Create a resource from an of_pci_range |
| 243 | * @range: the PCI range that describes the resource |
| 244 | * @np: device node where the range belongs to |
| 245 | * @res: pointer to a valid resource that will be updated to |
| 246 | * reflect the values contained in the range. |
| 247 | * |
| 248 | * Returns EINVAL if the range cannot be converted to resource. |
| 249 | * |
| 250 | * Note that if the range is an IO range, the resource will be converted |
| 251 | * using pci_address_to_pio() which can fail if it is called too early or |
| 252 | * if the range cannot be matched to any host bridge IO space (our case here). |
| 253 | * To guard against that we try to register the IO range first. |
| 254 | * If that fails we know that pci_address_to_pio() will do too. |
| 255 | */ |
| 256 | int of_pci_range_to_resource(struct of_pci_range *range, |
| 257 | struct device_node *np, struct resource *res) |
Liviu Dudau | 83bbde1 | 2014-09-29 15:29:24 +0100 | [diff] [blame] | 258 | { |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 259 | int err; |
Liviu Dudau | 83bbde1 | 2014-09-29 15:29:24 +0100 | [diff] [blame] | 260 | res->flags = range->flags; |
Liviu Dudau | 83bbde1 | 2014-09-29 15:29:24 +0100 | [diff] [blame] | 261 | res->parent = res->child = res->sibling = NULL; |
| 262 | res->name = np->full_name; |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 263 | |
| 264 | if (res->flags & IORESOURCE_IO) { |
| 265 | unsigned long port; |
Gabriele Paoloni | fcfaab3 | 2018-03-15 02:15:52 +0800 | [diff] [blame] | 266 | err = pci_register_io_range(&np->fwnode, range->cpu_addr, |
| 267 | range->size); |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 268 | if (err) |
| 269 | goto invalid_range; |
| 270 | port = pci_address_to_pio(range->cpu_addr); |
| 271 | if (port == (unsigned long)-1) { |
| 272 | err = -EINVAL; |
| 273 | goto invalid_range; |
| 274 | } |
| 275 | res->start = port; |
| 276 | } else { |
Pavel Fedin | 4af9710 | 2015-10-08 10:24:25 +0300 | [diff] [blame] | 277 | if ((sizeof(resource_size_t) < 8) && |
| 278 | upper_32_bits(range->cpu_addr)) { |
| 279 | err = -EINVAL; |
| 280 | goto invalid_range; |
| 281 | } |
| 282 | |
Liviu Dudau | 0b0b089 | 2014-09-29 15:29:25 +0100 | [diff] [blame] | 283 | res->start = range->cpu_addr; |
| 284 | } |
| 285 | res->end = res->start + range->size - 1; |
| 286 | return 0; |
| 287 | |
| 288 | invalid_range: |
| 289 | res->start = (resource_size_t)OF_BAD_ADDR; |
| 290 | res->end = (resource_size_t)OF_BAD_ADDR; |
| 291 | return err; |
Liviu Dudau | 83bbde1 | 2014-09-29 15:29:24 +0100 | [diff] [blame] | 292 | } |
Manikanta Maddireddy | bf6681e | 2018-01-11 11:38:02 +0530 | [diff] [blame] | 293 | EXPORT_SYMBOL(of_pci_range_to_resource); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 294 | #endif /* CONFIG_PCI */ |
| 295 | |
| 296 | /* |
| 297 | * ISA bus specific translator |
| 298 | */ |
| 299 | |
| 300 | static int of_bus_isa_match(struct device_node *np) |
| 301 | { |
Rob Herring | b3e46d1 | 2018-08-27 08:37:06 -0500 | [diff] [blame] | 302 | return of_node_name_eq(np, "isa"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void of_bus_isa_count_cells(struct device_node *child, |
| 306 | int *addrc, int *sizec) |
| 307 | { |
| 308 | if (addrc) |
| 309 | *addrc = 2; |
| 310 | if (sizec) |
| 311 | *sizec = 1; |
| 312 | } |
| 313 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 314 | static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns, |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 315 | int pna) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 316 | { |
| 317 | u64 cp, s, da; |
| 318 | |
| 319 | /* Check address type match */ |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 320 | if ((addr[0] ^ range[0]) & cpu_to_be32(1)) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 321 | return OF_BAD_ADDR; |
| 322 | |
| 323 | /* Read address values, skipping high cell */ |
| 324 | cp = of_read_number(range + 1, na - 1); |
| 325 | s = of_read_number(range + na + pna, ns); |
| 326 | da = of_read_number(addr + 1, na - 1); |
| 327 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 328 | pr_debug("ISA map, cp=%llx, s=%llx, da=%llx\n", |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 329 | (unsigned long long)cp, (unsigned long long)s, |
| 330 | (unsigned long long)da); |
| 331 | |
| 332 | if (da < cp || da >= (cp + s)) |
| 333 | return OF_BAD_ADDR; |
| 334 | return da - cp; |
| 335 | } |
| 336 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 337 | static int of_bus_isa_translate(__be32 *addr, u64 offset, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 338 | { |
| 339 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 340 | } |
| 341 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 342 | static unsigned int of_bus_isa_get_flags(const __be32 *addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 343 | { |
| 344 | unsigned int flags = 0; |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 345 | u32 w = be32_to_cpup(addr); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 346 | |
| 347 | if (w & 1) |
| 348 | flags |= IORESOURCE_IO; |
| 349 | else |
| 350 | flags |= IORESOURCE_MEM; |
| 351 | return flags; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Array of bus specific translators |
| 356 | */ |
| 357 | |
| 358 | static struct of_bus of_busses[] = { |
Rob Herring | 4670d61 | 2018-01-17 17:36:39 -0600 | [diff] [blame] | 359 | #ifdef CONFIG_PCI |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 360 | /* PCI */ |
| 361 | { |
| 362 | .name = "pci", |
| 363 | .addresses = "assigned-addresses", |
| 364 | .match = of_bus_pci_match, |
| 365 | .count_cells = of_bus_pci_count_cells, |
| 366 | .map = of_bus_pci_map, |
| 367 | .translate = of_bus_pci_translate, |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 368 | .has_flags = true, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 369 | .get_flags = of_bus_pci_get_flags, |
| 370 | }, |
Rob Herring | 4670d61 | 2018-01-17 17:36:39 -0600 | [diff] [blame] | 371 | #endif /* CONFIG_PCI */ |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 372 | /* ISA */ |
| 373 | { |
| 374 | .name = "isa", |
| 375 | .addresses = "reg", |
| 376 | .match = of_bus_isa_match, |
| 377 | .count_cells = of_bus_isa_count_cells, |
| 378 | .map = of_bus_isa_map, |
| 379 | .translate = of_bus_isa_translate, |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 380 | .has_flags = true, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 381 | .get_flags = of_bus_isa_get_flags, |
| 382 | }, |
| 383 | /* Default */ |
| 384 | { |
| 385 | .name = "default", |
| 386 | .addresses = "reg", |
| 387 | .match = NULL, |
| 388 | .count_cells = of_bus_default_count_cells, |
| 389 | .map = of_bus_default_map, |
| 390 | .translate = of_bus_default_translate, |
| 391 | .get_flags = of_bus_default_get_flags, |
| 392 | }, |
| 393 | }; |
| 394 | |
| 395 | static struct of_bus *of_match_bus(struct device_node *np) |
| 396 | { |
| 397 | int i; |
| 398 | |
| 399 | for (i = 0; i < ARRAY_SIZE(of_busses); i++) |
| 400 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 401 | return &of_busses[i]; |
| 402 | BUG(); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Benjamin Herrenschmidt | 41d9489 | 2015-03-23 14:16:38 +1100 | [diff] [blame] | 406 | static int of_empty_ranges_quirk(struct device_node *np) |
Benjamin Herrenschmidt | 746c9e9 | 2014-11-14 17:55:03 +1100 | [diff] [blame] | 407 | { |
| 408 | if (IS_ENABLED(CONFIG_PPC)) { |
Benjamin Herrenschmidt | 41d9489 | 2015-03-23 14:16:38 +1100 | [diff] [blame] | 409 | /* To save cycles, we cache the result for global "Mac" setting */ |
Benjamin Herrenschmidt | 746c9e9 | 2014-11-14 17:55:03 +1100 | [diff] [blame] | 410 | static int quirk_state = -1; |
| 411 | |
Benjamin Herrenschmidt | 41d9489 | 2015-03-23 14:16:38 +1100 | [diff] [blame] | 412 | /* PA-SEMI sdc DT bug */ |
| 413 | if (of_device_is_compatible(np, "1682m-sdc")) |
| 414 | return true; |
| 415 | |
| 416 | /* Make quirk cached */ |
Benjamin Herrenschmidt | 746c9e9 | 2014-11-14 17:55:03 +1100 | [diff] [blame] | 417 | if (quirk_state < 0) |
| 418 | quirk_state = |
| 419 | of_machine_is_compatible("Power Macintosh") || |
| 420 | of_machine_is_compatible("MacRISC"); |
| 421 | return quirk_state; |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 426 | static int of_translate_one(struct device_node *parent, struct of_bus *bus, |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 427 | struct of_bus *pbus, __be32 *addr, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 428 | int na, int ns, int pna, const char *rprop) |
| 429 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 430 | const __be32 *ranges; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 431 | unsigned int rlen; |
| 432 | int rone; |
| 433 | u64 offset = OF_BAD_ADDR; |
| 434 | |
Masahiro Yamada | ba85edb | 2015-11-30 15:22:37 +0900 | [diff] [blame] | 435 | /* |
| 436 | * Normally, an absence of a "ranges" property means we are |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 437 | * crossing a non-translatable boundary, and thus the addresses |
Masahiro Yamada | ba85edb | 2015-11-30 15:22:37 +0900 | [diff] [blame] | 438 | * below the current cannot be converted to CPU physical ones. |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 439 | * Unfortunately, while this is very clear in the spec, it's not |
| 440 | * what Apple understood, and they do have things like /uni-n or |
| 441 | * /ht nodes with no "ranges" property and a lot of perfectly |
| 442 | * useable mapped devices below them. Thus we treat the absence of |
| 443 | * "ranges" as equivalent to an empty "ranges" property which means |
| 444 | * a 1:1 translation at that level. It's up to the caller not to try |
| 445 | * to translate addresses that aren't supposed to be translated in |
| 446 | * the first place. --BenH. |
Grant Likely | 3930f29 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 447 | * |
| 448 | * As far as we know, this damage only exists on Apple machines, so |
| 449 | * This code is only enabled on powerpc. --gcl |
Rob Herring | 81db12e | 2019-09-04 11:43:30 +0100 | [diff] [blame] | 450 | * |
| 451 | * This quirk also applies for 'dma-ranges' which frequently exist in |
| 452 | * child nodes without 'dma-ranges' in the parent nodes. --RobH |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 453 | */ |
| 454 | ranges = of_get_property(parent, rprop, &rlen); |
Rob Herring | 81db12e | 2019-09-04 11:43:30 +0100 | [diff] [blame] | 455 | if (ranges == NULL && !of_empty_ranges_quirk(parent) && |
| 456 | strcmp(rprop, "dma-ranges")) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 457 | pr_debug("no ranges; cannot translate\n"); |
Grant Likely | 3930f29 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 458 | return 1; |
| 459 | } |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 460 | if (ranges == NULL || rlen == 0) { |
| 461 | offset = of_read_number(addr, na); |
| 462 | memset(addr, 0, pna * 4); |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 463 | pr_debug("empty ranges; 1:1 translation\n"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 464 | goto finish; |
| 465 | } |
| 466 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 467 | pr_debug("walking ranges...\n"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 468 | |
| 469 | /* Now walk through the ranges */ |
| 470 | rlen /= 4; |
| 471 | rone = na + pna + ns; |
| 472 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 473 | offset = bus->map(addr, ranges, na, ns, pna); |
| 474 | if (offset != OF_BAD_ADDR) |
| 475 | break; |
| 476 | } |
| 477 | if (offset == OF_BAD_ADDR) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 478 | pr_debug("not found !\n"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 479 | return 1; |
| 480 | } |
| 481 | memcpy(addr, ranges + na, 4 * pna); |
| 482 | |
| 483 | finish: |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 484 | of_dump_addr("parent translation for:", addr, pna); |
| 485 | pr_debug("with offset: %llx\n", (unsigned long long)offset); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 486 | |
| 487 | /* Translate it into parent bus space */ |
| 488 | return pbus->translate(addr, offset, pna); |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Translate an address from the device-tree into a CPU physical address, |
| 493 | * this walks up the tree and applies the various bus mappings on the |
| 494 | * way. |
| 495 | * |
| 496 | * Note: We consider that crossing any level with #size-cells == 0 to mean |
| 497 | * that translation is impossible (that is we are not dealing with a value |
| 498 | * that can be mapped to a cpu physical address). This is not really specified |
| 499 | * that way, but this is traditionally the way IBM at least do things |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 500 | * |
| 501 | * Whenever the translation fails, the *host pointer will be set to the |
| 502 | * device that had registered logical PIO mapping, and the return code is |
| 503 | * relative to that node. |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 504 | */ |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 505 | static u64 __of_translate_address(struct device_node *dev, |
Maxime Ripard | 95835a8 | 2019-04-01 10:56:43 +0200 | [diff] [blame] | 506 | struct device_node *(*get_parent)(const struct device_node *), |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 507 | const __be32 *in_addr, const char *rprop, |
| 508 | struct device_node **host) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 509 | { |
| 510 | struct device_node *parent = NULL; |
| 511 | struct of_bus *bus, *pbus; |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 512 | __be32 addr[OF_MAX_ADDR_CELLS]; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 513 | int na, ns, pna, pns; |
| 514 | u64 result = OF_BAD_ADDR; |
| 515 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 516 | pr_debug("** translation for device %pOF **\n", dev); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 517 | |
| 518 | /* Increase refcount at current level */ |
| 519 | of_node_get(dev); |
| 520 | |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 521 | *host = NULL; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 522 | /* Get parent & match bus type */ |
Maxime Ripard | 95835a8 | 2019-04-01 10:56:43 +0200 | [diff] [blame] | 523 | parent = get_parent(dev); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 524 | if (parent == NULL) |
| 525 | goto bail; |
| 526 | bus = of_match_bus(parent); |
| 527 | |
Andrew Murray | 59f5ca4 | 2012-12-13 10:11:23 +0000 | [diff] [blame] | 528 | /* Count address cells & copy address locally */ |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 529 | bus->count_cells(dev, &na, &ns); |
| 530 | if (!OF_CHECK_COUNTS(na, ns)) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 531 | pr_debug("Bad cell count for %pOF\n", dev); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 532 | goto bail; |
| 533 | } |
| 534 | memcpy(addr, in_addr, na * 4); |
| 535 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 536 | pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n", |
| 537 | bus->name, na, ns, parent); |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 538 | of_dump_addr("translating address:", addr, na); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 539 | |
| 540 | /* Translate */ |
| 541 | for (;;) { |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 542 | struct logic_pio_hwaddr *iorange; |
| 543 | |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 544 | /* Switch to parent bus */ |
| 545 | of_node_put(dev); |
| 546 | dev = parent; |
Maxime Ripard | 95835a8 | 2019-04-01 10:56:43 +0200 | [diff] [blame] | 547 | parent = get_parent(dev); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 548 | |
| 549 | /* If root, we have finished */ |
| 550 | if (parent == NULL) { |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 551 | pr_debug("reached root node\n"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 552 | result = of_read_number(addr, na); |
| 553 | break; |
| 554 | } |
| 555 | |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 556 | /* |
| 557 | * For indirectIO device which has no ranges property, get |
| 558 | * the address from reg directly. |
| 559 | */ |
| 560 | iorange = find_io_range_by_fwnode(&dev->fwnode); |
| 561 | if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) { |
| 562 | result = of_read_number(addr + 1, na - 1); |
| 563 | pr_debug("indirectIO matched(%pOF) 0x%llx\n", |
| 564 | dev, result); |
| 565 | *host = of_node_get(dev); |
| 566 | break; |
| 567 | } |
| 568 | |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 569 | /* Get new parent bus and counts */ |
| 570 | pbus = of_match_bus(parent); |
| 571 | pbus->count_cells(dev, &pna, &pns); |
| 572 | if (!OF_CHECK_COUNTS(pna, pns)) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 573 | pr_err("Bad cell count for %pOF\n", dev); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 577 | pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n", |
| 578 | pbus->name, pna, pns, parent); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 579 | |
| 580 | /* Apply bus translation */ |
| 581 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) |
| 582 | break; |
| 583 | |
| 584 | /* Complete the move up one level */ |
| 585 | na = pna; |
| 586 | ns = pns; |
| 587 | bus = pbus; |
| 588 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 589 | of_dump_addr("one level translation:", addr, na); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 590 | } |
| 591 | bail: |
| 592 | of_node_put(parent); |
| 593 | of_node_put(dev); |
| 594 | |
| 595 | return result; |
| 596 | } |
| 597 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 598 | u64 of_translate_address(struct device_node *dev, const __be32 *in_addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 599 | { |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 600 | struct device_node *host; |
| 601 | u64 ret; |
| 602 | |
Maxime Ripard | 95835a8 | 2019-04-01 10:56:43 +0200 | [diff] [blame] | 603 | ret = __of_translate_address(dev, of_get_parent, |
| 604 | in_addr, "ranges", &host); |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 605 | if (host) { |
| 606 | of_node_put(host); |
| 607 | return OF_BAD_ADDR; |
| 608 | } |
| 609 | |
| 610 | return ret; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 611 | } |
| 612 | EXPORT_SYMBOL(of_translate_address); |
| 613 | |
Maxime Ripard | f83a6e5 | 2019-04-01 10:56:44 +0200 | [diff] [blame] | 614 | static struct device_node *__of_get_dma_parent(const struct device_node *np) |
| 615 | { |
| 616 | struct of_phandle_args args; |
| 617 | int ret, index; |
| 618 | |
| 619 | index = of_property_match_string(np, "interconnect-names", "dma-mem"); |
| 620 | if (index < 0) |
| 621 | return of_get_parent(np); |
| 622 | |
| 623 | ret = of_parse_phandle_with_args(np, "interconnects", |
| 624 | "#interconnect-cells", |
| 625 | index, &args); |
| 626 | if (ret < 0) |
| 627 | return of_get_parent(np); |
| 628 | |
| 629 | return of_node_get(args.np); |
| 630 | } |
| 631 | |
Robin Murphy | 862ab55 | 2019-07-03 18:23:01 +0100 | [diff] [blame] | 632 | static struct device_node *of_get_next_dma_parent(struct device_node *np) |
| 633 | { |
| 634 | struct device_node *parent; |
| 635 | |
| 636 | parent = __of_get_dma_parent(np); |
| 637 | of_node_put(np); |
| 638 | |
| 639 | return parent; |
| 640 | } |
| 641 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 642 | u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 643 | { |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 644 | struct device_node *host; |
| 645 | u64 ret; |
| 646 | |
Maxime Ripard | f83a6e5 | 2019-04-01 10:56:44 +0200 | [diff] [blame] | 647 | ret = __of_translate_address(dev, __of_get_dma_parent, |
Maxime Ripard | 95835a8 | 2019-04-01 10:56:43 +0200 | [diff] [blame] | 648 | in_addr, "dma-ranges", &host); |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 649 | |
| 650 | if (host) { |
| 651 | of_node_put(host); |
| 652 | return OF_BAD_ADDR; |
| 653 | } |
| 654 | |
| 655 | return ret; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 656 | } |
| 657 | EXPORT_SYMBOL(of_translate_dma_address); |
| 658 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 659 | const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 660 | unsigned int *flags) |
| 661 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 662 | const __be32 *prop; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 663 | unsigned int psize; |
| 664 | struct device_node *parent; |
| 665 | struct of_bus *bus; |
| 666 | int onesize, i, na, ns; |
| 667 | |
| 668 | /* Get parent & match bus type */ |
| 669 | parent = of_get_parent(dev); |
| 670 | if (parent == NULL) |
| 671 | return NULL; |
| 672 | bus = of_match_bus(parent); |
| 673 | bus->count_cells(dev, &na, &ns); |
| 674 | of_node_put(parent); |
Stephen Warren | 5d61b16 | 2012-07-25 17:34:37 -0600 | [diff] [blame] | 675 | if (!OF_CHECK_ADDR_COUNT(na)) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 676 | return NULL; |
| 677 | |
| 678 | /* Get "reg" or "assigned-addresses" property */ |
| 679 | prop = of_get_property(dev, bus->addresses, &psize); |
| 680 | if (prop == NULL) |
| 681 | return NULL; |
| 682 | psize /= 4; |
| 683 | |
| 684 | onesize = na + ns; |
| 685 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) |
| 686 | if (i == index) { |
| 687 | if (size) |
| 688 | *size = of_read_number(prop + na, ns); |
| 689 | if (flags) |
| 690 | *flags = bus->get_flags(prop); |
| 691 | return prop; |
| 692 | } |
| 693 | return NULL; |
| 694 | } |
| 695 | EXPORT_SYMBOL(of_get_address); |
| 696 | |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 697 | static int parser_init(struct of_pci_range_parser *parser, |
| 698 | struct device_node *node, const char *name) |
| 699 | { |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 700 | int rlen; |
| 701 | |
| 702 | parser->node = node; |
| 703 | parser->pna = of_n_addr_cells(node); |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 704 | parser->na = of_bus_n_addr_cells(node); |
| 705 | parser->ns = of_bus_n_size_cells(node); |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 706 | parser->dma = !strcmp(name, "dma-ranges"); |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 707 | parser->bus = of_match_bus(node); |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 708 | |
| 709 | parser->range = of_get_property(node, name, &rlen); |
| 710 | if (parser->range == NULL) |
| 711 | return -ENOENT; |
| 712 | |
| 713 | parser->end = parser->range + rlen / sizeof(__be32); |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | int of_pci_range_parser_init(struct of_pci_range_parser *parser, |
| 719 | struct device_node *node) |
| 720 | { |
| 721 | return parser_init(parser, node, "ranges"); |
| 722 | } |
| 723 | EXPORT_SYMBOL_GPL(of_pci_range_parser_init); |
| 724 | |
| 725 | int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser, |
| 726 | struct device_node *node) |
| 727 | { |
| 728 | return parser_init(parser, node, "dma-ranges"); |
| 729 | } |
| 730 | EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init); |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 731 | #define of_dma_range_parser_init of_pci_dma_range_parser_init |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 732 | |
| 733 | struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, |
| 734 | struct of_pci_range *range) |
| 735 | { |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 736 | int na = parser->na; |
| 737 | int ns = parser->ns; |
| 738 | int np = parser->pna + na + ns; |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 739 | int busflag_na = 0; |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 740 | |
| 741 | if (!range) |
| 742 | return NULL; |
| 743 | |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 744 | if (!parser->range || parser->range + np > parser->end) |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 745 | return NULL; |
| 746 | |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 747 | range->flags = parser->bus->get_flags(parser->range); |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 748 | |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 749 | /* A extra cell for resource flags */ |
| 750 | if (parser->bus->has_flags) |
| 751 | busflag_na = 1; |
| 752 | |
| 753 | range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 754 | |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 755 | if (parser->dma) |
| 756 | range->cpu_addr = of_translate_dma_address(parser->node, |
| 757 | parser->range + na); |
| 758 | else |
| 759 | range->cpu_addr = of_translate_address(parser->node, |
| 760 | parser->range + na); |
| 761 | range->size = of_read_number(parser->range + parser->pna + na, ns); |
| 762 | |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 763 | parser->range += np; |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 764 | |
| 765 | /* Now consume following elements while they are contiguous */ |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 766 | while (parser->range + np <= parser->end) { |
| 767 | u32 flags = 0; |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 768 | u64 bus_addr, cpu_addr, size; |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 769 | |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 770 | flags = parser->bus->get_flags(parser->range); |
| 771 | bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na); |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 772 | if (parser->dma) |
| 773 | cpu_addr = of_translate_dma_address(parser->node, |
| 774 | parser->range + na); |
| 775 | else |
| 776 | cpu_addr = of_translate_address(parser->node, |
| 777 | parser->range + na); |
| 778 | size = of_read_number(parser->range + parser->pna + na, ns); |
| 779 | |
| 780 | if (flags != range->flags) |
| 781 | break; |
Jiaxun Yang | 2f96593 | 2020-07-28 23:36:55 +0800 | [diff] [blame] | 782 | if (bus_addr != range->bus_addr + range->size || |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 783 | cpu_addr != range->cpu_addr + range->size) |
| 784 | break; |
| 785 | |
| 786 | range->size += size; |
Rob Herring | bc5e522 | 2020-02-06 14:01:05 +0000 | [diff] [blame] | 787 | parser->range += np; |
Rob Herring | 67ccd2b | 2020-02-06 14:34:54 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | return range; |
| 791 | } |
| 792 | EXPORT_SYMBOL_GPL(of_pci_range_parser_one); |
| 793 | |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 794 | static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr, |
| 795 | u64 size) |
| 796 | { |
| 797 | u64 taddr; |
| 798 | unsigned long port; |
| 799 | struct device_node *host; |
| 800 | |
Maxime Ripard | 95835a8 | 2019-04-01 10:56:43 +0200 | [diff] [blame] | 801 | taddr = __of_translate_address(dev, of_get_parent, |
| 802 | in_addr, "ranges", &host); |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 803 | if (host) { |
| 804 | /* host-specific port access */ |
| 805 | port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size); |
| 806 | of_node_put(host); |
| 807 | } else { |
| 808 | /* memory-mapped I/O range */ |
| 809 | port = pci_address_to_pio(taddr); |
| 810 | } |
| 811 | |
| 812 | if (port == (unsigned long)-1) |
| 813 | return OF_BAD_ADDR; |
| 814 | |
| 815 | return port; |
| 816 | } |
| 817 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 818 | static int __of_address_to_resource(struct device_node *dev, |
| 819 | const __be32 *addrp, u64 size, unsigned int flags, |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 820 | const char *name, struct resource *r) |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 821 | { |
| 822 | u64 taddr; |
| 823 | |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 824 | if (flags & IORESOURCE_MEM) |
| 825 | taddr = of_translate_address(dev, addrp); |
| 826 | else if (flags & IORESOURCE_IO) |
| 827 | taddr = of_translate_ioport(dev, addrp, size); |
| 828 | else |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 829 | return -EINVAL; |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 830 | |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 831 | if (taddr == OF_BAD_ADDR) |
| 832 | return -EINVAL; |
| 833 | memset(r, 0, sizeof(struct resource)); |
Zhichang Yuan | 65af618 | 2018-03-15 02:15:54 +0800 | [diff] [blame] | 834 | |
| 835 | r->start = taddr; |
| 836 | r->end = taddr + size - 1; |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 837 | r->flags = flags; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 838 | r->name = name ? name : dev->full_name; |
| 839 | |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * of_address_to_resource - Translate device tree address and return as resource |
| 845 | * |
| 846 | * Note that if your address is a PIO address, the conversion will fail if |
| 847 | * the physical address can't be internally converted to an IO token with |
Frank Rowand | 7602f42 | 2017-06-20 16:50:11 -0700 | [diff] [blame] | 848 | * pci_address_to_pio(), that is because it's either called too early or it |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 849 | * can't be matched to any host bridge IO space |
| 850 | */ |
| 851 | int of_address_to_resource(struct device_node *dev, int index, |
| 852 | struct resource *r) |
| 853 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 854 | const __be32 *addrp; |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 855 | u64 size; |
| 856 | unsigned int flags; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 857 | const char *name = NULL; |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 858 | |
| 859 | addrp = of_get_address(dev, index, &size, &flags); |
| 860 | if (addrp == NULL) |
| 861 | return -EINVAL; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 862 | |
| 863 | /* Get optional "reg-names" property to add a name to a resource */ |
| 864 | of_property_read_string_index(dev, "reg-names", index, &name); |
| 865 | |
| 866 | return __of_address_to_resource(dev, addrp, size, flags, name, r); |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 867 | } |
| 868 | EXPORT_SYMBOL_GPL(of_address_to_resource); |
| 869 | |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 870 | /** |
| 871 | * of_iomap - Maps the memory mapped IO for a given device_node |
Wenchao Hao | 1094d5d | 2020-07-02 00:24:44 +0800 | [diff] [blame] | 872 | * @np: the device whose io range will be mapped |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 873 | * @index: index of the io range |
| 874 | * |
| 875 | * Returns a pointer to the mapped memory |
| 876 | */ |
| 877 | void __iomem *of_iomap(struct device_node *np, int index) |
| 878 | { |
| 879 | struct resource res; |
| 880 | |
| 881 | if (of_address_to_resource(np, index, &res)) |
| 882 | return NULL; |
| 883 | |
Felipe Balbi | 28c1b6d | 2011-07-22 22:47:21 +0300 | [diff] [blame] | 884 | return ioremap(res.start, resource_size(&res)); |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 885 | } |
| 886 | EXPORT_SYMBOL(of_iomap); |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 887 | |
Matthias Brugger | efd342f | 2014-07-18 11:36:39 +0200 | [diff] [blame] | 888 | /* |
| 889 | * of_io_request_and_map - Requests a resource and maps the memory mapped IO |
| 890 | * for a given device_node |
| 891 | * @device: the device whose io range will be mapped |
| 892 | * @index: index of the io range |
Benjamin Herrenschmidt | b01dcdd | 2018-06-12 10:01:07 +1000 | [diff] [blame] | 893 | * @name: name "override" for the memory region request or NULL |
Matthias Brugger | efd342f | 2014-07-18 11:36:39 +0200 | [diff] [blame] | 894 | * |
| 895 | * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded |
| 896 | * error code on failure. Usage example: |
| 897 | * |
| 898 | * base = of_io_request_and_map(node, 0, "foo"); |
| 899 | * if (IS_ERR(base)) |
| 900 | * return PTR_ERR(base); |
| 901 | */ |
| 902 | void __iomem *of_io_request_and_map(struct device_node *np, int index, |
Benjamin Herrenschmidt | b01dcdd | 2018-06-12 10:01:07 +1000 | [diff] [blame] | 903 | const char *name) |
Matthias Brugger | efd342f | 2014-07-18 11:36:39 +0200 | [diff] [blame] | 904 | { |
| 905 | struct resource res; |
| 906 | void __iomem *mem; |
| 907 | |
| 908 | if (of_address_to_resource(np, index, &res)) |
| 909 | return IOMEM_ERR_PTR(-EINVAL); |
| 910 | |
Benjamin Herrenschmidt | b01dcdd | 2018-06-12 10:01:07 +1000 | [diff] [blame] | 911 | if (!name) |
| 912 | name = res.name; |
Matthias Brugger | efd342f | 2014-07-18 11:36:39 +0200 | [diff] [blame] | 913 | if (!request_mem_region(res.start, resource_size(&res), name)) |
| 914 | return IOMEM_ERR_PTR(-EBUSY); |
| 915 | |
| 916 | mem = ioremap(res.start, resource_size(&res)); |
| 917 | if (!mem) { |
| 918 | release_mem_region(res.start, resource_size(&res)); |
| 919 | return IOMEM_ERR_PTR(-ENOMEM); |
| 920 | } |
| 921 | |
| 922 | return mem; |
| 923 | } |
| 924 | EXPORT_SYMBOL(of_io_request_and_map); |
| 925 | |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 926 | /** |
| 927 | * of_dma_get_range - Get DMA range info |
| 928 | * @np: device node to get DMA range info |
| 929 | * @dma_addr: pointer to store initial DMA address of DMA range |
| 930 | * @paddr: pointer to store initial CPU address of DMA range |
| 931 | * @size: pointer to store size of DMA range |
| 932 | * |
| 933 | * Look in bottom up direction for the first "dma-ranges" property |
| 934 | * and parse it. |
| 935 | * dma-ranges format: |
| 936 | * DMA addr (dma_addr) : naddr cells |
| 937 | * CPU addr (phys_addr_t) : pna cells |
| 938 | * size : nsize cells |
| 939 | * |
| 940 | * It returns -ENODEV if "dma-ranges" property was not found |
| 941 | * for this device in DT. |
| 942 | */ |
| 943 | int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size) |
| 944 | { |
| 945 | struct device_node *node = of_node_get(np); |
| 946 | const __be32 *ranges = NULL; |
Rob Herring | 7a8b64d | 2020-02-06 14:02:30 +0000 | [diff] [blame] | 947 | int len; |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 948 | int ret = 0; |
Robin Murphy | 951d488 | 2019-07-03 18:42:20 +0100 | [diff] [blame] | 949 | bool found_dma_ranges = false; |
Rob Herring | 7a8b64d | 2020-02-06 14:02:30 +0000 | [diff] [blame] | 950 | struct of_range_parser parser; |
| 951 | struct of_range range; |
Rob Herring | 9d55beb | 2020-02-06 14:26:40 +0000 | [diff] [blame] | 952 | u64 dma_start = U64_MAX, dma_end = 0, dma_offset = 0; |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 953 | |
Robin Murphy | 951d488 | 2019-07-03 18:42:20 +0100 | [diff] [blame] | 954 | while (node) { |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 955 | ranges = of_get_property(node, "dma-ranges", &len); |
| 956 | |
| 957 | /* Ignore empty ranges, they imply no translation required */ |
| 958 | if (ranges && len > 0) |
| 959 | break; |
| 960 | |
Robin Murphy | 951d488 | 2019-07-03 18:42:20 +0100 | [diff] [blame] | 961 | /* Once we find 'dma-ranges', then a missing one is an error */ |
| 962 | if (found_dma_ranges && !ranges) { |
| 963 | ret = -ENODEV; |
| 964 | goto out; |
| 965 | } |
| 966 | found_dma_ranges = true; |
| 967 | |
| 968 | node = of_get_next_dma_parent(node); |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 969 | } |
| 970 | |
Robin Murphy | 951d488 | 2019-07-03 18:42:20 +0100 | [diff] [blame] | 971 | if (!node || !ranges) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 972 | pr_debug("no dma-ranges found for node(%pOF)\n", np); |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 973 | ret = -ENODEV; |
| 974 | goto out; |
| 975 | } |
| 976 | |
Rob Herring | 7a8b64d | 2020-02-06 14:02:30 +0000 | [diff] [blame] | 977 | of_dma_range_parser_init(&parser, node); |
| 978 | |
| 979 | for_each_of_range(&parser, &range) { |
| 980 | pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", |
| 981 | range.bus_addr, range.cpu_addr, range.size); |
| 982 | |
Rob Herring | 9d55beb | 2020-02-06 14:26:40 +0000 | [diff] [blame] | 983 | if (dma_offset && range.cpu_addr - range.bus_addr != dma_offset) { |
| 984 | pr_warn("Can't handle multiple dma-ranges with different offsets on node(%pOF)\n", node); |
| 985 | /* Don't error out as we'd break some existing DTs */ |
| 986 | continue; |
| 987 | } |
Colin Ian King | f49c7fa | 2020-08-17 12:32:08 +0100 | [diff] [blame^] | 988 | if (range.cpu_addr == OF_BAD_ADDR) { |
| 989 | pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n", |
| 990 | range.bus_addr, node); |
| 991 | continue; |
| 992 | } |
Rob Herring | 9d55beb | 2020-02-06 14:26:40 +0000 | [diff] [blame] | 993 | dma_offset = range.cpu_addr - range.bus_addr; |
Rob Herring | 7a8b64d | 2020-02-06 14:02:30 +0000 | [diff] [blame] | 994 | |
Rob Herring | 9d55beb | 2020-02-06 14:26:40 +0000 | [diff] [blame] | 995 | /* Take lower and upper limits */ |
| 996 | if (range.bus_addr < dma_start) |
| 997 | dma_start = range.bus_addr; |
| 998 | if (range.bus_addr + range.size > dma_end) |
| 999 | dma_end = range.bus_addr + range.size; |
| 1000 | } |
| 1001 | |
| 1002 | if (dma_start >= dma_end) { |
| 1003 | ret = -EINVAL; |
| 1004 | pr_debug("Invalid DMA ranges configuration on node(%pOF)\n", |
| 1005 | node); |
Robin Murphy | 951d488 | 2019-07-03 18:42:20 +0100 | [diff] [blame] | 1006 | goto out; |
| 1007 | } |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 1008 | |
Rob Herring | 9d55beb | 2020-02-06 14:26:40 +0000 | [diff] [blame] | 1009 | *dma_addr = dma_start; |
| 1010 | *size = dma_end - dma_start; |
| 1011 | *paddr = dma_start + dma_offset; |
| 1012 | |
| 1013 | pr_debug("final: dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", |
| 1014 | *dma_addr, *paddr, *size); |
Grygorii Strashko | 18308c9 | 2014-04-24 11:30:02 -0400 | [diff] [blame] | 1015 | |
| 1016 | out: |
| 1017 | of_node_put(node); |
| 1018 | |
| 1019 | return ret; |
| 1020 | } |
Santosh Shilimkar | 92ea637 | 2014-04-24 11:30:03 -0400 | [diff] [blame] | 1021 | |
| 1022 | /** |
| 1023 | * of_dma_is_coherent - Check if device is coherent |
| 1024 | * @np: device node |
| 1025 | * |
| 1026 | * It returns true if "dma-coherent" property was found |
Michael Ellerman | dabf6b3 | 2020-01-26 22:52:47 +1100 | [diff] [blame] | 1027 | * for this device in the DT, or if DMA is coherent by |
| 1028 | * default for OF devices on the current platform. |
Santosh Shilimkar | 92ea637 | 2014-04-24 11:30:03 -0400 | [diff] [blame] | 1029 | */ |
| 1030 | bool of_dma_is_coherent(struct device_node *np) |
| 1031 | { |
| 1032 | struct device_node *node = of_node_get(np); |
| 1033 | |
Michael Ellerman | dabf6b3 | 2020-01-26 22:52:47 +1100 | [diff] [blame] | 1034 | if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT)) |
| 1035 | return true; |
| 1036 | |
Santosh Shilimkar | 92ea637 | 2014-04-24 11:30:03 -0400 | [diff] [blame] | 1037 | while (node) { |
| 1038 | if (of_property_read_bool(node, "dma-coherent")) { |
| 1039 | of_node_put(node); |
| 1040 | return true; |
| 1041 | } |
Robin Murphy | c60bf3e | 2019-07-03 14:47:31 +0100 | [diff] [blame] | 1042 | node = of_get_next_dma_parent(node); |
Santosh Shilimkar | 92ea637 | 2014-04-24 11:30:03 -0400 | [diff] [blame] | 1043 | } |
| 1044 | of_node_put(node); |
| 1045 | return false; |
| 1046 | } |
Linus Torvalds | eb3d3ec | 2014-06-05 15:57:04 -0700 | [diff] [blame] | 1047 | EXPORT_SYMBOL_GPL(of_dma_is_coherent); |