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