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