// SPDX-License-Identifier: GPL-2.0 #define pr_fmt(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/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 "of_private.h" /* Max address size we deal with */ #define OF_MAX_ADDR_CELLS … #define OF_CHECK_ADDR_COUNT(na) … #define OF_CHECK_COUNTS(na, ns) … /* Debug utility */ #ifdef DEBUG static void of_dump_addr(const char *s, const __be32 *addr, int na) { pr_debug("%s", s); while (na--) pr_cont(" %08x", be32_to_cpu(*(addr++))); pr_cont("\n"); } #else static void of_dump_addr(const char *s, const __be32 *addr, int na) { … } #endif /* Callbacks for bus specific translators */ struct of_bus { … }; /* * Default translator (generic bus) */ static void of_bus_default_count_cells(struct device_node *dev, int *addrc, int *sizec) { … } static u64 of_bus_default_map(__be32 *addr, const __be32 *range, int na, int ns, int pna, int fna) { … } static int of_bus_default_translate(__be32 *addr, u64 offset, int na) { … } static unsigned int of_bus_default_flags_get_flags(const __be32 *addr) { … } static unsigned int of_bus_default_get_flags(const __be32 *addr) { … } static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na, int ns, int pna, int fna) { … } static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na) { … } #ifdef CONFIG_PCI static unsigned int of_bus_pci_get_flags(const __be32 *addr) { … } /* * PCI bus specific translator */ static bool of_node_is_pcie(struct device_node *np) { … } static int of_bus_pci_match(struct device_node *np) { … } static void of_bus_pci_count_cells(struct device_node *np, int *addrc, int *sizec) { … } static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, int pna, int fna) { … } #endif /* CONFIG_PCI */ /* * 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(struct of_pci_range *range, struct device_node *np, struct resource *res) { … } EXPORT_SYMBOL(…); /* * 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 EINVAL if the range cannot be * converted to resource. */ int of_range_to_resource(struct device_node *np, int index, struct resource *res) { … } EXPORT_SYMBOL(…); /* * ISA bus specific translator */ static int of_bus_isa_match(struct device_node *np) { … } static void of_bus_isa_count_cells(struct device_node *child, int *addrc, int *sizec) { … } static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns, int pna, int fna) { … } static unsigned int of_bus_isa_get_flags(const __be32 *addr) { … } static int of_bus_default_flags_match(struct device_node *np) { … } /* * Array of bus specific translators */ static struct of_bus of_busses[] = …; static struct of_bus *of_match_bus(struct device_node *np) { … } static int of_empty_ranges_quirk(struct device_node *np) { … } static int of_translate_one(struct device_node *parent, struct of_bus *bus, struct of_bus *pbus, __be32 *addr, int na, int ns, int pna, const char *rprop) { … } /* * 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) { … } u64 of_translate_address(struct device_node *dev, const __be32 *in_addr) { … } EXPORT_SYMBOL(…); #ifdef CONFIG_HAS_DMA struct device_node *__of_get_dma_parent(const struct device_node *np) { … } #endif static struct device_node *of_get_next_dma_parent(struct device_node *np) { … } u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr) { … } EXPORT_SYMBOL(…); /** * of_translate_dma_region - Translate device tree address and size tuple * @dev: device tree node for which to translate * @prop: pointer into array of cells * @start: return value for the start of the DMA range * @length: return value for the length of the DMA range * * Returns a pointer to the cell immediately following the translated DMA region. */ const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop, phys_addr_t *start, size_t *length) { … } EXPORT_SYMBOL(…); const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no, u64 *size, unsigned int *flags) { … } EXPORT_SYMBOL(…); /** * of_property_read_reg - Retrieve the specified "reg" entry index without translating * @np: device tree node for which to retrieve "reg" from * @idx: "reg" entry index to read * @addr: return value for the untranslated address * @size: return value for the entry size * * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and * size values filled in. */ int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size) { … } EXPORT_SYMBOL(…); static int parser_init(struct of_pci_range_parser *parser, struct device_node *node, const char *name) { … } int of_pci_range_parser_init(struct of_pci_range_parser *parser, struct device_node *node) { … } EXPORT_SYMBOL_GPL(…); int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser, struct device_node *node) { … } EXPORT_SYMBOL_GPL(…); #define of_dma_range_parser_init … struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, struct of_pci_range *range) { … } EXPORT_SYMBOL_GPL(…); static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr, u64 size) { … } #ifdef CONFIG_HAS_DMA /** * of_dma_get_range - Get DMA range info and put it into a map array * @np: device node to get DMA range info * @map: dma range structure to return * * Look in bottom up direction for the first "dma-ranges" property * and parse it. Put the information into a DMA offset map array. * * dma-ranges format: * DMA addr (dma_addr) : naddr cells * CPU addr (phys_addr_t) : pna cells * size : nsize cells * * It returns -ENODEV if "dma-ranges" property was not found for this * device in the DT. */ int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map) { … } #endif /* CONFIG_HAS_DMA */ /** * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA * @np: The node to start searching from or NULL to start from the root * * Gets the highest CPU physical address that is addressable by all DMA masters * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no * DMA constrained device is found, it returns PHYS_ADDR_MAX. */ phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np) { … } /** * of_dma_is_coherent - Check if device is coherent * @np: device node * * It returns true if "dma-coherent" property was found * for this device in the DT, or if DMA is coherent by * default for OF devices on the current platform and no * "dma-noncoherent" property was found for this device. */ bool of_dma_is_coherent(struct device_node *np) { … } EXPORT_SYMBOL_GPL(…); /** * of_mmio_is_nonposted - Check if device uses non-posted MMIO * @np: device node * * Returns true if the "nonposted-mmio" property was found for * the device's bus. * * This is currently only enabled on builds that support Apple ARM devices, as * an optimization. */ static bool of_mmio_is_nonposted(struct device_node *np) { … } static int __of_address_to_resource(struct device_node *dev, int index, int bar_no, struct resource *r) { … } /** * of_address_to_resource - Translate device tree address and return as resource * @dev: Caller's Device Node * @index: Index into the array * @r: Pointer to resource array * * Returns -EINVAL if the range cannot be converted to resource. * * Note that if your address is a PIO address, the conversion will fail if * the physical address can't be internally converted to an IO token with * pci_address_to_pio(), that is because it's either called too early or it * can't be matched to any host bridge IO space */ int of_address_to_resource(struct device_node *dev, int index, struct resource *r) { … } EXPORT_SYMBOL_GPL(…); int of_pci_address_to_resource(struct device_node *dev, int bar, struct resource *r) { … } EXPORT_SYMBOL_GPL(…); /** * of_iomap - Maps the memory mapped IO for a given device_node * @np: the device whose io range will be mapped * @index: index of the io range * * Returns a pointer to the mapped memory */ void __iomem *of_iomap(struct device_node *np, int index) { … } EXPORT_SYMBOL(…); /* * of_io_request_and_map - Requests a resource and maps the memory mapped IO * for a given device_node * @device: the device whose io range will be mapped * @index: index of the io range * @name: name "override" for the memory region request or NULL * * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded * error code on failure. Usage example: * * base = of_io_request_and_map(node, 0, "foo"); * if (IS_ERR(base)) * return PTR_ERR(base); */ void __iomem *of_io_request_and_map(struct device_node *np, int index, const char *name) { … } EXPORT_SYMBOL(…);