brintos

brintos / linux-shallow public Read only

0
0
Text · 29.7 KiB · 286f0c1 Raw
1175 lines · c
1// SPDX-License-Identifier: GPL-2.02#define pr_fmt(fmt)	"OF: " fmt3 4#include <linux/device.h>5#include <linux/fwnode.h>6#include <linux/io.h>7#include <linux/ioport.h>8#include <linux/logic_pio.h>9#include <linux/module.h>10#include <linux/of_address.h>11#include <linux/overflow.h>12#include <linux/pci.h>13#include <linux/pci_regs.h>14#include <linux/sizes.h>15#include <linux/slab.h>16#include <linux/string.h>17#include <linux/dma-direct.h> /* for bus_dma_region */18 19#include "of_private.h"20 21/* Max address size we deal with */22#define OF_MAX_ADDR_CELLS	423#define OF_CHECK_ADDR_COUNT(na)	((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)24#define OF_CHECK_COUNTS(na, ns)	(OF_CHECK_ADDR_COUNT(na) && (ns) > 0)25 26/* Debug utility */27#ifdef DEBUG28static void of_dump_addr(const char *s, const __be32 *addr, int na)29{30	pr_debug("%s", s);31	while (na--)32		pr_cont(" %08x", be32_to_cpu(*(addr++)));33	pr_cont("\n");34}35#else36static void of_dump_addr(const char *s, const __be32 *addr, int na) { }37#endif38 39/* Callbacks for bus specific translators */40struct 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);46	u64		(*map)(__be32 *addr, const __be32 *range,47				int na, int ns, int pna, int fna);48	int		(*translate)(__be32 *addr, u64 offset, int na);49	int		flag_cells;50	unsigned int	(*get_flags)(const __be32 *addr);51};52 53/*54 * Default translator (generic bus)55 */56 57static void of_bus_default_count_cells(struct device_node *dev,58				       int *addrc, int *sizec)59{60	if (addrc)61		*addrc = of_n_addr_cells(dev);62	if (sizec)63		*sizec = of_n_size_cells(dev);64}65 66static u64 of_bus_default_map(__be32 *addr, const __be32 *range,67		int na, int ns, int pna, int fna)68{69	u64 cp, s, da;70 71	cp = of_read_number(range + fna, na - fna);72	s  = of_read_number(range + na + pna, ns);73	da = of_read_number(addr + fna, na - fna);74 75	pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);76 77	if (da < cp || da >= (cp + s))78		return OF_BAD_ADDR;79	return da - cp;80}81 82static int of_bus_default_translate(__be32 *addr, u64 offset, int na)83{84	u64 a = of_read_number(addr, na);85	memset(addr, 0, na * 4);86	a += offset;87	if (na > 1)88		addr[na - 2] = cpu_to_be32(a >> 32);89	addr[na - 1] = cpu_to_be32(a & 0xffffffffu);90 91	return 0;92}93 94static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)95{96	return of_read_number(addr, 1);97}98 99static unsigned int of_bus_default_get_flags(const __be32 *addr)100{101	return IORESOURCE_MEM;102}103 104static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,105				    int ns, int pna, int fna)106{107	/* Check that flags match */108	if (*addr != *range)109		return OF_BAD_ADDR;110 111	return of_bus_default_map(addr, range, na, ns, pna, fna);112}113 114static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)115{116	/* Keep "flags" part (high cell) in translated address */117	return of_bus_default_translate(addr + 1, offset, na - 1);118}119 120#ifdef CONFIG_PCI121static unsigned int of_bus_pci_get_flags(const __be32 *addr)122{123	unsigned int flags = 0;124	u32 w = be32_to_cpup(addr);125 126	if (!IS_ENABLED(CONFIG_PCI))127		return 0;128 129	switch((w >> 24) & 0x03) {130	case 0x01:131		flags |= IORESOURCE_IO;132		break;133	case 0x02: /* 32 bits */134		flags |= IORESOURCE_MEM;135		break;136 137	case 0x03: /* 64 bits */138		flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;139		break;140	}141	if (w & 0x40000000)142		flags |= IORESOURCE_PREFETCH;143	return flags;144}145 146/*147 * PCI bus specific translator148 */149 150static bool of_node_is_pcie(struct device_node *np)151{152	bool is_pcie = of_node_name_eq(np, "pcie");153 154	if (is_pcie)155		pr_warn_once("%pOF: Missing device_type\n", np);156 157	return is_pcie;158}159 160static int of_bus_pci_match(struct device_node *np)161{162	/*163 	 * "pciex" is PCI Express164	 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs165	 * "ht" is hypertransport166	 *167	 * If none of the device_type match, and that the node name is168	 * "pcie", accept the device as PCI (with a warning).169	 */170	return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||171		of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||172		of_node_is_pcie(np);173}174 175static void of_bus_pci_count_cells(struct device_node *np,176				   int *addrc, int *sizec)177{178	if (addrc)179		*addrc = 3;180	if (sizec)181		*sizec = 2;182}183 184static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,185		int pna, int fna)186{187	unsigned int af, rf;188 189	af = of_bus_pci_get_flags(addr);190	rf = of_bus_pci_get_flags(range);191 192	/* Check address type match */193	if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))194		return OF_BAD_ADDR;195 196	return of_bus_default_map(addr, range, na, ns, pna, fna);197}198 199#endif /* CONFIG_PCI */200 201static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)202{203	u64 end = start;204 205	if (overflows_type(start, r->start))206		return -EOVERFLOW;207	if (size && check_add_overflow(end, size - 1, &end))208		return -EOVERFLOW;209	if (overflows_type(end, r->end))210		return -EOVERFLOW;211 212	r->start = start;213	r->end = end;214 215	return 0;216}217 218/*219 * of_pci_range_to_resource - Create a resource from an of_pci_range220 * @range:	the PCI range that describes the resource221 * @np:		device node where the range belongs to222 * @res:	pointer to a valid resource that will be updated to223 *              reflect the values contained in the range.224 *225 * Returns -EINVAL if the range cannot be converted to resource.226 *227 * Note that if the range is an IO range, the resource will be converted228 * using pci_address_to_pio() which can fail if it is called too early or229 * if the range cannot be matched to any host bridge IO space (our case here).230 * To guard against that we try to register the IO range first.231 * If that fails we know that pci_address_to_pio() will do too.232 */233int of_pci_range_to_resource(struct of_pci_range *range,234			     struct device_node *np, struct resource *res)235{236	u64 start;237	int err;238	res->flags = range->flags;239	res->parent = res->child = res->sibling = NULL;240	res->name = np->full_name;241 242	if (res->flags & IORESOURCE_IO) {243		unsigned long port;244		err = pci_register_io_range(&np->fwnode, range->cpu_addr,245				range->size);246		if (err)247			goto invalid_range;248		port = pci_address_to_pio(range->cpu_addr);249		if (port == (unsigned long)-1) {250			err = -EINVAL;251			goto invalid_range;252		}253		start = port;254	} else {255		start = range->cpu_addr;256	}257	return __of_address_resource_bounds(res, start, range->size);258 259invalid_range:260	res->start = (resource_size_t)OF_BAD_ADDR;261	res->end = (resource_size_t)OF_BAD_ADDR;262	return err;263}264EXPORT_SYMBOL(of_pci_range_to_resource);265 266/*267 * of_range_to_resource - Create a resource from a ranges entry268 * @np:		device node where the range belongs to269 * @index:	the 'ranges' index to convert to a resource270 * @res:	pointer to a valid resource that will be updated to271 *              reflect the values contained in the range.272 *273 * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range274 * cannot be converted to resource.275 */276int of_range_to_resource(struct device_node *np, int index, struct resource *res)277{278	int ret, i = 0;279	struct of_range_parser parser;280	struct of_range range;281 282	ret = of_range_parser_init(&parser, np);283	if (ret)284		return ret;285 286	for_each_of_range(&parser, &range)287		if (i++ == index)288			return of_pci_range_to_resource(&range, np, res);289 290	return -ENOENT;291}292EXPORT_SYMBOL(of_range_to_resource);293 294/*295 * ISA bus specific translator296 */297 298static int of_bus_isa_match(struct device_node *np)299{300	return of_node_name_eq(np, "isa");301}302 303static void of_bus_isa_count_cells(struct device_node *child,304				   int *addrc, int *sizec)305{306	if (addrc)307		*addrc = 2;308	if (sizec)309		*sizec = 1;310}311 312static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,313		int pna, int fna)314{315	/* Check address type match */316	if ((addr[0] ^ range[0]) & cpu_to_be32(1))317		return OF_BAD_ADDR;318 319	return of_bus_default_map(addr, range, na, ns, pna, fna);320}321 322static unsigned int of_bus_isa_get_flags(const __be32 *addr)323{324	unsigned int flags = 0;325	u32 w = be32_to_cpup(addr);326 327	if (w & 1)328		flags |= IORESOURCE_IO;329	else330		flags |= IORESOURCE_MEM;331	return flags;332}333 334static int of_bus_default_flags_match(struct device_node *np)335{336	return of_bus_n_addr_cells(np) == 3;337}338 339/*340 * Array of bus specific translators341 */342 343static struct of_bus of_busses[] = {344#ifdef CONFIG_PCI345	/* PCI */346	{347		.name = "pci",348		.addresses = "assigned-addresses",349		.match = of_bus_pci_match,350		.count_cells = of_bus_pci_count_cells,351		.map = of_bus_pci_map,352		.translate = of_bus_default_flags_translate,353		.flag_cells = 1,354		.get_flags = of_bus_pci_get_flags,355	},356#endif /* CONFIG_PCI */357	/* ISA */358	{359		.name = "isa",360		.addresses = "reg",361		.match = of_bus_isa_match,362		.count_cells = of_bus_isa_count_cells,363		.map = of_bus_isa_map,364		.translate = of_bus_default_flags_translate,365		.flag_cells = 1,366		.get_flags = of_bus_isa_get_flags,367	},368	/* Default with flags cell */369	{370		.name = "default-flags",371		.addresses = "reg",372		.match = of_bus_default_flags_match,373		.count_cells = of_bus_default_count_cells,374		.map = of_bus_default_flags_map,375		.translate = of_bus_default_flags_translate,376		.flag_cells = 1,377		.get_flags = of_bus_default_flags_get_flags,378	},379	/* Default */380	{381		.name = "default",382		.addresses = "reg",383		.match = NULL,384		.count_cells = of_bus_default_count_cells,385		.map = of_bus_default_map,386		.translate = of_bus_default_translate,387		.get_flags = of_bus_default_get_flags,388	},389};390 391static struct of_bus *of_match_bus(struct device_node *np)392{393	int i;394 395	for (i = 0; i < ARRAY_SIZE(of_busses); i++)396		if (!of_busses[i].match || of_busses[i].match(np))397			return &of_busses[i];398	BUG();399	return NULL;400}401 402static int of_empty_ranges_quirk(struct device_node *np)403{404	if (IS_ENABLED(CONFIG_PPC)) {405		/* To save cycles, we cache the result for global "Mac" setting */406		static int quirk_state = -1;407 408		/* PA-SEMI sdc DT bug */409		if (of_device_is_compatible(np, "1682m-sdc"))410			return true;411 412		/* Make quirk cached */413		if (quirk_state < 0)414			quirk_state =415				of_machine_is_compatible("Power Macintosh") ||416				of_machine_is_compatible("MacRISC");417		return quirk_state;418	}419	return false;420}421 422static int of_translate_one(struct device_node *parent, struct of_bus *bus,423			    struct of_bus *pbus, __be32 *addr,424			    int na, int ns, int pna, const char *rprop)425{426	const __be32 *ranges;427	unsigned int rlen;428	int rone;429	u64 offset = OF_BAD_ADDR;430 431	/*432	 * Normally, an absence of a "ranges" property means we are433	 * crossing a non-translatable boundary, and thus the addresses434	 * below the current cannot be converted to CPU physical ones.435	 * Unfortunately, while this is very clear in the spec, it's not436	 * what Apple understood, and they do have things like /uni-n or437	 * /ht nodes with no "ranges" property and a lot of perfectly438	 * useable mapped devices below them. Thus we treat the absence of439	 * "ranges" as equivalent to an empty "ranges" property which means440	 * a 1:1 translation at that level. It's up to the caller not to try441	 * to translate addresses that aren't supposed to be translated in442	 * the first place. --BenH.443	 *444	 * As far as we know, this damage only exists on Apple machines, so445	 * This code is only enabled on powerpc. --gcl446	 *447	 * This quirk also applies for 'dma-ranges' which frequently exist in448	 * child nodes without 'dma-ranges' in the parent nodes. --RobH449	 */450	ranges = of_get_property(parent, rprop, &rlen);451	if (ranges == NULL && !of_empty_ranges_quirk(parent) &&452	    strcmp(rprop, "dma-ranges")) {453		pr_debug("no ranges; cannot translate\n");454		return 1;455	}456	if (ranges == NULL || rlen == 0) {457		offset = of_read_number(addr, na);458		memset(addr, 0, pna * 4);459		pr_debug("empty ranges; 1:1 translation\n");460		goto finish;461	}462 463	pr_debug("walking ranges...\n");464 465	/* Now walk through the ranges */466	rlen /= 4;467	rone = na + pna + ns;468	for (; rlen >= rone; rlen -= rone, ranges += rone) {469		offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);470		if (offset != OF_BAD_ADDR)471			break;472	}473	if (offset == OF_BAD_ADDR) {474		pr_debug("not found !\n");475		return 1;476	}477	memcpy(addr, ranges + na, 4 * pna);478 479 finish:480	of_dump_addr("parent translation for:", addr, pna);481	pr_debug("with offset: %llx\n", offset);482 483	/* Translate it into parent bus space */484	return pbus->translate(addr, offset, pna);485}486 487/*488 * Translate an address from the device-tree into a CPU physical address,489 * this walks up the tree and applies the various bus mappings on the490 * way.491 *492 * Note: We consider that crossing any level with #size-cells == 0 to mean493 * that translation is impossible (that is we are not dealing with a value494 * that can be mapped to a cpu physical address). This is not really specified495 * that way, but this is traditionally the way IBM at least do things496 *497 * Whenever the translation fails, the *host pointer will be set to the498 * device that had registered logical PIO mapping, and the return code is499 * relative to that node.500 */501static u64 __of_translate_address(struct device_node *node,502				  struct device_node *(*get_parent)(const struct device_node *),503				  const __be32 *in_addr, const char *rprop,504				  struct device_node **host)505{506	struct device_node *dev __free(device_node) = of_node_get(node);507	struct device_node *parent __free(device_node) = get_parent(dev);508	struct of_bus *bus, *pbus;509	__be32 addr[OF_MAX_ADDR_CELLS];510	int na, ns, pna, pns;511 512	pr_debug("** translation for device %pOF **\n", dev);513 514	*host = NULL;515 516	if (parent == NULL)517		return OF_BAD_ADDR;518	bus = of_match_bus(parent);519 520	/* Count address cells & copy address locally */521	bus->count_cells(dev, &na, &ns);522	if (!OF_CHECK_COUNTS(na, ns)) {523		pr_debug("Bad cell count for %pOF\n", dev);524		return OF_BAD_ADDR;525	}526	memcpy(addr, in_addr, na * 4);527 528	pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",529	    bus->name, na, ns, parent);530	of_dump_addr("translating address:", addr, na);531 532	/* Translate */533	for (;;) {534		struct logic_pio_hwaddr *iorange;535 536		/* Switch to parent bus */537		of_node_put(dev);538		dev = parent;539		parent = get_parent(dev);540 541		/* If root, we have finished */542		if (parent == NULL) {543			pr_debug("reached root node\n");544			return of_read_number(addr, na);545		}546 547		/*548		 * For indirectIO device which has no ranges property, get549		 * the address from reg directly.550		 */551		iorange = find_io_range_by_fwnode(&dev->fwnode);552		if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {553			u64 result = of_read_number(addr + 1, na - 1);554			pr_debug("indirectIO matched(%pOF) 0x%llx\n",555				 dev, result);556			*host = no_free_ptr(dev);557			return result;558		}559 560		/* Get new parent bus and counts */561		pbus = of_match_bus(parent);562		pbus->count_cells(dev, &pna, &pns);563		if (!OF_CHECK_COUNTS(pna, pns)) {564			pr_err("Bad cell count for %pOF\n", dev);565			return OF_BAD_ADDR;566		}567 568		pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",569		    pbus->name, pna, pns, parent);570 571		/* Apply bus translation */572		if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))573			return OF_BAD_ADDR;574 575		/* Complete the move up one level */576		na = pna;577		ns = pns;578		bus = pbus;579 580		of_dump_addr("one level translation:", addr, na);581	}582 583	unreachable();584}585 586u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)587{588	struct device_node *host;589	u64 ret;590 591	ret = __of_translate_address(dev, of_get_parent,592				     in_addr, "ranges", &host);593	if (host) {594		of_node_put(host);595		return OF_BAD_ADDR;596	}597 598	return ret;599}600EXPORT_SYMBOL(of_translate_address);601 602#ifdef CONFIG_HAS_DMA603struct device_node *__of_get_dma_parent(const struct device_node *np)604{605	struct of_phandle_args args;606	int ret, index;607 608	index = of_property_match_string(np, "interconnect-names", "dma-mem");609	if (index < 0)610		return of_get_parent(np);611 612	ret = of_parse_phandle_with_args(np, "interconnects",613					 "#interconnect-cells",614					 index, &args);615	if (ret < 0)616		return of_get_parent(np);617 618	return of_node_get(args.np);619}620#endif621 622static struct device_node *of_get_next_dma_parent(struct device_node *np)623{624	struct device_node *parent;625 626	parent = __of_get_dma_parent(np);627	of_node_put(np);628 629	return parent;630}631 632u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)633{634	struct device_node *host;635	u64 ret;636 637	ret = __of_translate_address(dev, __of_get_dma_parent,638				     in_addr, "dma-ranges", &host);639 640	if (host) {641		of_node_put(host);642		return OF_BAD_ADDR;643	}644 645	return ret;646}647EXPORT_SYMBOL(of_translate_dma_address);648 649/**650 * of_translate_dma_region - Translate device tree address and size tuple651 * @dev: device tree node for which to translate652 * @prop: pointer into array of cells653 * @start: return value for the start of the DMA range654 * @length: return value for the length of the DMA range655 *656 * Returns a pointer to the cell immediately following the translated DMA region.657 */658const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,659				      phys_addr_t *start, size_t *length)660{661	struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);662	u64 address, size;663	int na, ns;664 665	if (!parent)666		return NULL;667 668	na = of_bus_n_addr_cells(parent);669	ns = of_bus_n_size_cells(parent);670 671	address = of_translate_dma_address(dev, prop);672	if (address == OF_BAD_ADDR)673		return NULL;674 675	size = of_read_number(prop + na, ns);676 677	if (start)678		*start = address;679 680	if (length)681		*length = size;682 683	return prop + na + ns;684}685EXPORT_SYMBOL(of_translate_dma_region);686 687const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,688			       u64 *size, unsigned int *flags)689{690	const __be32 *prop;691	unsigned int psize;692	struct device_node *parent __free(device_node) = of_get_parent(dev);693	struct of_bus *bus;694	int onesize, i, na, ns;695 696	if (parent == NULL)697		return NULL;698 699	/* match the parent's bus type */700	bus = of_match_bus(parent);701	if (strcmp(bus->name, "pci") && (bar_no >= 0))702		return NULL;703 704	bus->count_cells(dev, &na, &ns);705	if (!OF_CHECK_ADDR_COUNT(na))706		return NULL;707 708	/* Get "reg" or "assigned-addresses" property */709	prop = of_get_property(dev, bus->addresses, &psize);710	if (prop == NULL)711		return NULL;712	psize /= 4;713 714	onesize = na + ns;715	for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {716		u32 val = be32_to_cpu(prop[0]);717		/* PCI bus matches on BAR number instead of index */718		if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||719		    ((index >= 0) && (i == index))) {720			if (size)721				*size = of_read_number(prop + na, ns);722			if (flags)723				*flags = bus->get_flags(prop);724			return prop;725		}726	}727	return NULL;728}729EXPORT_SYMBOL(__of_get_address);730 731/**732 * of_property_read_reg - Retrieve the specified "reg" entry index without translating733 * @np: device tree node for which to retrieve "reg" from734 * @idx: "reg" entry index to read735 * @addr: return value for the untranslated address736 * @size: return value for the entry size737 *738 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and739 * size values filled in.740 */741int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)742{743	const __be32 *prop = of_get_address(np, idx, size, NULL);744 745	if (!prop)746		return -EINVAL;747 748	*addr = of_read_number(prop, of_n_addr_cells(np));749 750	return 0;751}752EXPORT_SYMBOL(of_property_read_reg);753 754static int parser_init(struct of_pci_range_parser *parser,755			struct device_node *node, const char *name)756{757	int rlen;758 759	parser->node = node;760	parser->pna = of_n_addr_cells(node);761	parser->na = of_bus_n_addr_cells(node);762	parser->ns = of_bus_n_size_cells(node);763	parser->dma = !strcmp(name, "dma-ranges");764	parser->bus = of_match_bus(node);765 766	parser->range = of_get_property(node, name, &rlen);767	if (parser->range == NULL)768		return -ENOENT;769 770	parser->end = parser->range + rlen / sizeof(__be32);771 772	return 0;773}774 775int of_pci_range_parser_init(struct of_pci_range_parser *parser,776				struct device_node *node)777{778	return parser_init(parser, node, "ranges");779}780EXPORT_SYMBOL_GPL(of_pci_range_parser_init);781 782int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,783				struct device_node *node)784{785	return parser_init(parser, node, "dma-ranges");786}787EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);788#define of_dma_range_parser_init of_pci_dma_range_parser_init789 790struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,791						struct of_pci_range *range)792{793	int na = parser->na;794	int ns = parser->ns;795	int np = parser->pna + na + ns;796	int busflag_na = parser->bus->flag_cells;797 798	if (!range)799		return NULL;800 801	if (!parser->range || parser->range + np > parser->end)802		return NULL;803 804	range->flags = parser->bus->get_flags(parser->range);805 806	range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);807 808	if (parser->dma)809		range->cpu_addr = of_translate_dma_address(parser->node,810				parser->range + na);811	else812		range->cpu_addr = of_translate_address(parser->node,813				parser->range + na);814	range->size = of_read_number(parser->range + parser->pna + na, ns);815 816	parser->range += np;817 818	/* Now consume following elements while they are contiguous */819	while (parser->range + np <= parser->end) {820		u32 flags = 0;821		u64 bus_addr, cpu_addr, size;822 823		flags = parser->bus->get_flags(parser->range);824		bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);825		if (parser->dma)826			cpu_addr = of_translate_dma_address(parser->node,827					parser->range + na);828		else829			cpu_addr = of_translate_address(parser->node,830					parser->range + na);831		size = of_read_number(parser->range + parser->pna + na, ns);832 833		if (flags != range->flags)834			break;835		if (bus_addr != range->bus_addr + range->size ||836		    cpu_addr != range->cpu_addr + range->size)837			break;838 839		range->size += size;840		parser->range += np;841	}842 843	return range;844}845EXPORT_SYMBOL_GPL(of_pci_range_parser_one);846 847static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,848			u64 size)849{850	u64 taddr;851	unsigned long port;852	struct device_node *host;853 854	taddr = __of_translate_address(dev, of_get_parent,855				       in_addr, "ranges", &host);856	if (host) {857		/* host-specific port access */858		port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);859		of_node_put(host);860	} else {861		/* memory-mapped I/O range */862		port = pci_address_to_pio(taddr);863	}864 865	if (port == (unsigned long)-1)866		return OF_BAD_ADDR;867 868	return port;869}870 871#ifdef CONFIG_HAS_DMA872/**873 * of_dma_get_range - Get DMA range info and put it into a map array874 * @np:		device node to get DMA range info875 * @map:	dma range structure to return876 *877 * Look in bottom up direction for the first "dma-ranges" property878 * and parse it.  Put the information into a DMA offset map array.879 *880 * dma-ranges format:881 *	DMA addr (dma_addr)	: naddr cells882 *	CPU addr (phys_addr_t)	: pna cells883 *	size			: nsize cells884 *885 * It returns -ENODEV if "dma-ranges" property was not found for this886 * device in the DT.887 */888int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)889{890	struct device_node *node __free(device_node) = of_node_get(np);891	const __be32 *ranges = NULL;892	bool found_dma_ranges = false;893	struct of_range_parser parser;894	struct of_range range;895	struct bus_dma_region *r;896	int len, num_ranges = 0;897 898	while (node) {899		ranges = of_get_property(node, "dma-ranges", &len);900 901		/* Ignore empty ranges, they imply no translation required */902		if (ranges && len > 0)903			break;904 905		/* Once we find 'dma-ranges', then a missing one is an error */906		if (found_dma_ranges && !ranges)907			return -ENODEV;908 909		found_dma_ranges = true;910 911		node = of_get_next_dma_parent(node);912	}913 914	if (!node || !ranges) {915		pr_debug("no dma-ranges found for node(%pOF)\n", np);916		return -ENODEV;917	}918	of_dma_range_parser_init(&parser, node);919	for_each_of_range(&parser, &range) {920		if (range.cpu_addr == OF_BAD_ADDR) {921			pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",922			       range.bus_addr, node);923			continue;924		}925		num_ranges++;926	}927 928	if (!num_ranges)929		return -EINVAL;930 931	r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);932	if (!r)933		return -ENOMEM;934 935	/*936	 * Record all info in the generic DMA ranges array for struct device,937	 * returning an error if we don't find any parsable ranges.938	 */939	*map = r;940	of_dma_range_parser_init(&parser, node);941	for_each_of_range(&parser, &range) {942		pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",943			 range.bus_addr, range.cpu_addr, range.size);944		if (range.cpu_addr == OF_BAD_ADDR)945			continue;946		r->cpu_start = range.cpu_addr;947		r->dma_start = range.bus_addr;948		r->size = range.size;949		r++;950	}951	return 0;952}953#endif /* CONFIG_HAS_DMA */954 955/**956 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA957 * @np: The node to start searching from or NULL to start from the root958 *959 * Gets the highest CPU physical address that is addressable by all DMA masters960 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no961 * DMA constrained device is found, it returns PHYS_ADDR_MAX.962 */963phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)964{965	phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;966	struct of_range_parser parser;967	phys_addr_t subtree_max_addr;968	struct device_node *child;969	struct of_range range;970	const __be32 *ranges;971	u64 cpu_end = 0;972	int len;973 974	if (!np)975		np = of_root;976 977	ranges = of_get_property(np, "dma-ranges", &len);978	if (ranges && len) {979		of_dma_range_parser_init(&parser, np);980		for_each_of_range(&parser, &range)981			if (range.cpu_addr + range.size > cpu_end)982				cpu_end = range.cpu_addr + range.size - 1;983 984		if (max_cpu_addr > cpu_end)985			max_cpu_addr = cpu_end;986	}987 988	for_each_available_child_of_node(np, child) {989		subtree_max_addr = of_dma_get_max_cpu_address(child);990		if (max_cpu_addr > subtree_max_addr)991			max_cpu_addr = subtree_max_addr;992	}993 994	return max_cpu_addr;995}996 997/**998 * of_dma_is_coherent - Check if device is coherent999 * @np:	device node1000 *1001 * It returns true if "dma-coherent" property was found1002 * for this device in the DT, or if DMA is coherent by1003 * default for OF devices on the current platform and no1004 * "dma-noncoherent" property was found for this device.1005 */1006bool of_dma_is_coherent(struct device_node *np)1007{1008	struct device_node *node __free(device_node) = of_node_get(np);1009 1010	while (node) {1011		if (of_property_read_bool(node, "dma-coherent"))1012			return true;1013 1014		if (of_property_read_bool(node, "dma-noncoherent"))1015			return false;1016 1017		node = of_get_next_dma_parent(node);1018	}1019	return dma_default_coherent;1020}1021EXPORT_SYMBOL_GPL(of_dma_is_coherent);1022 1023/**1024 * of_mmio_is_nonposted - Check if device uses non-posted MMIO1025 * @np:	device node1026 *1027 * Returns true if the "nonposted-mmio" property was found for1028 * the device's bus.1029 *1030 * This is currently only enabled on builds that support Apple ARM devices, as1031 * an optimization.1032 */1033static bool of_mmio_is_nonposted(struct device_node *np)1034{1035	if (!IS_ENABLED(CONFIG_ARCH_APPLE))1036		return false;1037 1038	struct device_node *parent __free(device_node) = of_get_parent(np);1039	if (!parent)1040		return false;1041 1042	return of_property_read_bool(parent, "nonposted-mmio");1043}1044 1045static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,1046		struct resource *r)1047{1048	u64 taddr;1049	const __be32	*addrp;1050	u64		size;1051	unsigned int	flags;1052	const char	*name = NULL;1053 1054	addrp = __of_get_address(dev, index, bar_no, &size, &flags);1055	if (addrp == NULL)1056		return -EINVAL;1057 1058	/* Get optional "reg-names" property to add a name to a resource */1059	if (index >= 0)1060		of_property_read_string_index(dev, "reg-names",	index, &name);1061 1062	if (flags & IORESOURCE_MEM)1063		taddr = of_translate_address(dev, addrp);1064	else if (flags & IORESOURCE_IO)1065		taddr = of_translate_ioport(dev, addrp, size);1066	else1067		return -EINVAL;1068 1069	if (taddr == OF_BAD_ADDR)1070		return -EINVAL;1071	memset(r, 0, sizeof(struct resource));1072 1073	if (of_mmio_is_nonposted(dev))1074		flags |= IORESOURCE_MEM_NONPOSTED;1075 1076	r->flags = flags;1077	r->name = name ? name : dev->full_name;1078 1079	return __of_address_resource_bounds(r, taddr, size);1080}1081 1082/**1083 * of_address_to_resource - Translate device tree address and return as resource1084 * @dev:	Caller's Device Node1085 * @index:	Index into the array1086 * @r:		Pointer to resource array1087 *1088 * Returns -EINVAL if the range cannot be converted to resource.1089 *1090 * Note that if your address is a PIO address, the conversion will fail if1091 * the physical address can't be internally converted to an IO token with1092 * pci_address_to_pio(), that is because it's either called too early or it1093 * can't be matched to any host bridge IO space1094 */1095int of_address_to_resource(struct device_node *dev, int index,1096			   struct resource *r)1097{1098	return __of_address_to_resource(dev, index, -1, r);1099}1100EXPORT_SYMBOL_GPL(of_address_to_resource);1101 1102int of_pci_address_to_resource(struct device_node *dev, int bar,1103			       struct resource *r)1104{1105 1106	if (!IS_ENABLED(CONFIG_PCI))1107		return -ENOSYS;1108 1109	return __of_address_to_resource(dev, -1, bar, r);1110}1111EXPORT_SYMBOL_GPL(of_pci_address_to_resource);1112 1113/**1114 * of_iomap - Maps the memory mapped IO for a given device_node1115 * @np:		the device whose io range will be mapped1116 * @index:	index of the io range1117 *1118 * Returns a pointer to the mapped memory1119 */1120void __iomem *of_iomap(struct device_node *np, int index)1121{1122	struct resource res;1123 1124	if (of_address_to_resource(np, index, &res))1125		return NULL;1126 1127	if (res.flags & IORESOURCE_MEM_NONPOSTED)1128		return ioremap_np(res.start, resource_size(&res));1129	else1130		return ioremap(res.start, resource_size(&res));1131}1132EXPORT_SYMBOL(of_iomap);1133 1134/*1135 * of_io_request_and_map - Requests a resource and maps the memory mapped IO1136 *			   for a given device_node1137 * @device:	the device whose io range will be mapped1138 * @index:	index of the io range1139 * @name:	name "override" for the memory region request or NULL1140 *1141 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded1142 * error code on failure. Usage example:1143 *1144 *	base = of_io_request_and_map(node, 0, "foo");1145 *	if (IS_ERR(base))1146 *		return PTR_ERR(base);1147 */1148void __iomem *of_io_request_and_map(struct device_node *np, int index,1149				    const char *name)1150{1151	struct resource res;1152	void __iomem *mem;1153 1154	if (of_address_to_resource(np, index, &res))1155		return IOMEM_ERR_PTR(-EINVAL);1156 1157	if (!name)1158		name = res.name;1159	if (!request_mem_region(res.start, resource_size(&res), name))1160		return IOMEM_ERR_PTR(-EBUSY);1161 1162	if (res.flags & IORESOURCE_MEM_NONPOSTED)1163		mem = ioremap_np(res.start, resource_size(&res));1164	else1165		mem = ioremap(res.start, resource_size(&res));1166 1167	if (!mem) {1168		release_mem_region(res.start, resource_size(&res));1169		return IOMEM_ERR_PTR(-ENOMEM);1170	}1171 1172	return mem;1173}1174EXPORT_SYMBOL(of_io_request_and_map);1175