1180 lines · 30.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0#define pr_fmt(fmt)	"OF: " fmt #include <linux/device.h>#include <linux/fwnode.h>#include <linux/io.h>#include <linux/ioport.h>#include <linux/logic_pio.h>#include <linux/module.h>#include <linux/of_address.h>#include <linux/overflow.h>#include <linux/pci.h>#include <linux/pci_regs.h>#include <linux/sizes.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/dma-direct.h> /* for bus_dma_region */ #include <kunit/visibility.h> /* Uncomment me to enable of_dump_addr() debugging output */// #define DEBUG #include "of_private.h" /* Callbacks for bus specific translators */struct of_bus {	const char	*name;	const char	*addresses;	int		(*match)(struct device_node *parent);	void		(*count_cells)(struct device_node *child,				       int *addrc, int *sizec);	u64		(*map)(__be32 *addr, const __be32 *range,				int na, int ns, int pna, int fna);	int		(*translate)(__be32 *addr, u64 offset, int na);	int		flag_cells;	unsigned int	(*get_flags)(const __be32 *addr);}; /* * Default translator (generic bus) */ static void of_bus_default_count_cells(struct device_node *dev,				       int *addrc, int *sizec){	if (addrc)		*addrc = of_n_addr_cells(dev);	if (sizec)		*sizec = of_n_size_cells(dev);} static u64 of_bus_default_map(__be32 *addr, const __be32 *range,		int na, int ns, int pna, int fna){	u64 cp, s, da; 	cp = of_read_number(range + fna, na - fna);	s  = of_read_number(range + na + pna, ns);	da = of_read_number(addr + fna, na - fna); 	pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da); 	if (da < cp || da >= (cp + s))		return OF_BAD_ADDR;	return da - cp;} static int of_bus_default_translate(__be32 *addr, u64 offset, int na){	u64 a = of_read_number(addr, na);	memset(addr, 0, na * 4);	a += offset;	if (na > 1)		addr[na - 2] = cpu_to_be32(a >> 32);	addr[na - 1] = cpu_to_be32(a & 0xffffffffu); 	return 0;} static unsigned int of_bus_default_flags_get_flags(const __be32 *addr){	return of_read_number(addr, 1);} static unsigned int of_bus_default_get_flags(const __be32 *addr){	return IORESOURCE_MEM;} static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,				    int ns, int pna, int fna){	/* Check that flags match */	if (*addr != *range)		return OF_BAD_ADDR; 	return of_bus_default_map(addr, range, na, ns, pna, fna);} static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na){	/* Keep "flags" part (high cell) in translated address */	return of_bus_default_translate(addr + 1, offset, na - 1);} #ifdef CONFIG_PCIstatic unsigned int of_bus_pci_get_flags(const __be32 *addr){	unsigned int flags = 0;	u32 w = be32_to_cpup(addr); 	if (!IS_ENABLED(CONFIG_PCI))		return 0; 	switch((w >> 24) & 0x03) {	case 0x01:		flags |= IORESOURCE_IO;		break;	case 0x02: /* 32 bits */		flags |= IORESOURCE_MEM;		break; 	case 0x03: /* 64 bits */		flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;		break;	}	if (w & 0x40000000)		flags |= IORESOURCE_PREFETCH;	return flags;} /* * PCI bus specific translator */ static bool of_node_is_pcie(const struct device_node *np){	bool is_pcie = of_node_name_eq(np, "pcie"); 	if (is_pcie)		pr_warn_once("%pOF: Missing device_type\n", np); 	return is_pcie;} static int of_bus_pci_match(struct device_node *np){	/* 	 * "pciex" is PCI Express	 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs	 * "ht" is hypertransport	 *	 * If none of the device_type match, and that the node name is	 * "pcie", accept the device as PCI (with a warning).	 */	return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||		of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||		of_node_is_pcie(np);} static void of_bus_pci_count_cells(struct device_node *np,				   int *addrc, int *sizec){	if (addrc)		*addrc = 3;	if (sizec)		*sizec = 2;} static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,		int pna, int fna){	unsigned int af, rf; 	af = of_bus_pci_get_flags(addr);	rf = of_bus_pci_get_flags(range); 	/* Check address type match */	if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))		return OF_BAD_ADDR; 	return of_bus_default_map(addr, range, na, ns, pna, fna);} #endif /* CONFIG_PCI */ VISIBLE_IF_KUNIT int __of_address_resource_bounds(struct resource *r, u64 start, u64 size){	if (overflows_type(start, r->start))		return -EOVERFLOW; 	r->start = start; 	if (!size)		r->end = wrapping_sub(typeof(r->end), r->start, 1);	else if (size && check_add_overflow(r->start, size - 1, &r->end))		return -EOVERFLOW; 	return 0;}EXPORT_SYMBOL_IF_KUNIT(__of_address_resource_bounds); /* * of_pci_range_to_resource - Create a resource from an of_pci_range * @range:	the PCI range that describes the resource * @np:		device node where the range belongs to * @res:	pointer to a valid resource that will be updated to *              reflect the values contained in the range. * * Returns -EINVAL if the range cannot be converted to resource. * * Note that if the range is an IO range, the resource will be converted * using pci_address_to_pio() which can fail if it is called too early or * if the range cannot be matched to any host bridge IO space (our case here). * To guard against that we try to register the IO range first. * If that fails we know that pci_address_to_pio() will do too. */int of_pci_range_to_resource(const struct of_pci_range *range,			     const struct device_node *np, struct resource *res){	u64 start;	int err;	res->flags = range->flags;	res->parent = res->child = res->sibling = NULL;	res->name = np->full_name; 	if (res->flags & IORESOURCE_IO) {		unsigned long port;		err = pci_register_io_range(&np->fwnode, range->cpu_addr,				range->size);		if (err)			goto invalid_range;		port = pci_address_to_pio(range->cpu_addr);		if (port == (unsigned long)-1) {			err = -EINVAL;			goto invalid_range;		}		start = port;	} else {		start = range->cpu_addr;	}	return __of_address_resource_bounds(res, start, range->size); invalid_range:	res->start = (resource_size_t)OF_BAD_ADDR;	res->end = (resource_size_t)OF_BAD_ADDR;	return err;}EXPORT_SYMBOL(of_pci_range_to_resource); /* * of_range_to_resource - Create a resource from a ranges entry * @np:		device node where the range belongs to * @index:	the 'ranges' index to convert to a resource * @res:	pointer to a valid resource that will be updated to *              reflect the values contained in the range. * * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range * cannot be converted to resource. */int of_range_to_resource(struct device_node *np, int index, struct resource *res){	int ret, i = 0;	struct of_range_parser parser;	struct of_range range; 	ret = of_range_parser_init(&parser, np);	if (ret)		return ret; 	for_each_of_range(&parser, &range)		if (i++ == index)			return of_pci_range_to_resource(&range, np, res); 	return -ENOENT;}EXPORT_SYMBOL(of_range_to_resource); /* * ISA bus specific translator */ static int of_bus_isa_match(struct device_node *np){	return of_node_name_eq(np, "isa");} static void of_bus_isa_count_cells(struct device_node *child,				   int *addrc, int *sizec){	if (addrc)		*addrc = 2;	if (sizec)		*sizec = 1;} static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,		int pna, int fna){	/* Check address type match */	if ((addr[0] ^ range[0]) & cpu_to_be32(1))		return OF_BAD_ADDR; 	return of_bus_default_map(addr, range, na, ns, pna, fna);} static unsigned int of_bus_isa_get_flags(const __be32 *addr){	unsigned int flags = 0;	u32 w = be32_to_cpup(addr); 	if (w & 1)		flags |= IORESOURCE_IO;	else		flags |= IORESOURCE_MEM;	return flags;} static int of_bus_default_flags_match(struct device_node *np){	/*	 * Check for presence first since of_bus_n_addr_cells() will warn when	 * walking parent nodes.	 */	return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);} static int of_bus_default_match(struct device_node *np){	return of_property_present(np, "#address-cells");} /* * Array of bus specific translators */ static const struct of_bus of_busses[] = {#ifdef CONFIG_PCI	/* PCI */	{		.name = "pci",		.addresses = "assigned-addresses",		.match = of_bus_pci_match,		.count_cells = of_bus_pci_count_cells,		.map = of_bus_pci_map,		.translate = of_bus_default_flags_translate,		.flag_cells = 1,		.get_flags = of_bus_pci_get_flags,	},#endif /* CONFIG_PCI */	/* ISA */	{		.name = "isa",		.addresses = "reg",		.match = of_bus_isa_match,		.count_cells = of_bus_isa_count_cells,		.map = of_bus_isa_map,		.translate = of_bus_default_flags_translate,		.flag_cells = 1,		.get_flags = of_bus_isa_get_flags,	},	/* Default with flags cell */	{		.name = "default-flags",		.addresses = "reg",		.match = of_bus_default_flags_match,		.count_cells = of_bus_default_count_cells,		.map = of_bus_default_flags_map,		.translate = of_bus_default_flags_translate,		.flag_cells = 1,		.get_flags = of_bus_default_flags_get_flags,	},	/* Default */	{		.name = "default",		.addresses = "reg",		.match = of_bus_default_match,		.count_cells = of_bus_default_count_cells,		.map = of_bus_default_map,		.translate = of_bus_default_translate,		.get_flags = of_bus_default_get_flags,	},}; static const struct of_bus *of_match_bus(struct device_node *np){	int i; 	for (i = 0; i < ARRAY_SIZE(of_busses); i++)		if (!of_busses[i].match || of_busses[i].match(np))			return &of_busses[i];	return NULL;} static int of_empty_ranges_quirk(const struct device_node *np){	if (IS_ENABLED(CONFIG_PPC)) {		/* To save cycles, we cache the result for global "Mac" setting */		static int quirk_state = -1; 		/* PA-SEMI sdc DT bug */		if (of_device_is_compatible(np, "1682m-sdc"))			return true; 		/* Make quirk cached */		if (quirk_state < 0)			quirk_state =				of_machine_is_compatible("Power Macintosh") ||				of_machine_is_compatible("MacRISC");		return quirk_state;	}	return false;} static int of_translate_one(const struct device_node *parent, const struct of_bus *bus,			    const struct of_bus *pbus, __be32 *addr,			    int na, int ns, int pna, const char *rprop){	const __be32 *ranges;	unsigned int rlen;	int rone;	u64 offset = OF_BAD_ADDR; 	/*	 * Normally, an absence of a "ranges" property means we are	 * crossing a non-translatable boundary, and thus the addresses	 * below the current cannot be converted to CPU physical ones.	 * Unfortunately, while this is very clear in the spec, it's not	 * what Apple understood, and they do have things like /uni-n or	 * /ht nodes with no "ranges" property and a lot of perfectly	 * useable mapped devices below them. Thus we treat the absence of	 * "ranges" as equivalent to an empty "ranges" property which means	 * a 1:1 translation at that level. It's up to the caller not to try	 * to translate addresses that aren't supposed to be translated in	 * the first place. --BenH.	 *	 * As far as we know, this damage only exists on Apple machines, so	 * This code is only enabled on powerpc. --gcl	 *	 * This quirk also applies for 'dma-ranges' which frequently exist in	 * child nodes without 'dma-ranges' in the parent nodes. --RobH	 */	ranges = of_get_property(parent, rprop, &rlen);	if (ranges == NULL && !of_empty_ranges_quirk(parent) &&	    strcmp(rprop, "dma-ranges")) {		pr_debug("no ranges; cannot translate\n");		return 1;	}	if (ranges == NULL || rlen == 0) {		offset = of_read_number(addr, na);		/* set address to zero, pass flags through */		memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);		pr_debug("empty ranges; 1:1 translation\n");		goto finish;	} 	pr_debug("walking ranges...\n"); 	/* Now walk through the ranges */	rlen /= 4;	rone = na + pna + ns;	for (; rlen >= rone; rlen -= rone, ranges += rone) {		offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);		if (offset != OF_BAD_ADDR)			break;	}	if (offset == OF_BAD_ADDR) {		pr_debug("not found !\n");		return 1;	}	memcpy(addr, ranges + na, 4 * pna);  finish:	of_dump_addr("parent translation for:", addr, pna);	pr_debug("with offset: %llx\n", offset); 	/* Translate it into parent bus space */	return pbus->translate(addr, offset, pna);} /* * Translate an address from the device-tree into a CPU physical address, * this walks up the tree and applies the various bus mappings on the * way. * * Note: We consider that crossing any level with #size-cells == 0 to mean * that translation is impossible (that is we are not dealing with a value * that can be mapped to a cpu physical address). This is not really specified * that way, but this is traditionally the way IBM at least do things * * Whenever the translation fails, the *host pointer will be set to the * device that had registered logical PIO mapping, and the return code is * relative to that node. */static u64 __of_translate_address(struct device_node *node,				  struct device_node *(*get_parent)(const struct device_node *),				  const __be32 *in_addr, const char *rprop,				  struct device_node **host){

Lines 1–500 of 1180.