diff options
Diffstat (limited to 'drivers/of/device.c')
| -rw-r--r-- | drivers/of/device.c | 372 |
1 files changed, 171 insertions, 201 deletions
diff --git a/drivers/of/device.c b/drivers/of/device.c index 28c38c756f92..f7e75e527667 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -1,22 +1,24 @@ -#include <linux/string.h> +// SPDX-License-Identifier: GPL-2.0 #include <linux/kernel.h> #include <linux/of.h> #include <linux/of_device.h> #include <linux/of_address.h> #include <linux/of_iommu.h> -#include <linux/dma-mapping.h> +#include <linux/of_reserved_mem.h> +#include <linux/dma-direct.h> /* for bus_dma_region */ +#include <linux/dma-map-ops.h> #include <linux/init.h> -#include <linux/module.h> #include <linux/mod_devicetable.h> #include <linux/slab.h> +#include <linux/platform_device.h> #include <asm/errno.h> #include "of_private.h" /** * of_match_device - Tell if a struct device matches an of_device_id list - * @ids: array of of device match structures to search in - * @dev: the of device structure to match against + * @matches: array of of_device_id match structures to search in + * @dev: the OF device structure to match against * * Used by a driver to check whether an platform_device present in the * system is in its list of supported devices. @@ -24,56 +26,53 @@ const struct of_device_id *of_match_device(const struct of_device_id *matches, const struct device *dev) { - if ((!matches) || (!dev->of_node)) + if (!matches || !dev->of_node || dev->of_node_reused) return NULL; return of_match_node(matches, dev->of_node); } EXPORT_SYMBOL(of_match_device); -struct platform_device *of_dev_get(struct platform_device *dev) +static void +of_dma_set_restricted_buffer(struct device *dev, struct device_node *np) { - struct device *tmp; + struct device_node *of_node = dev->of_node; + struct of_phandle_iterator it; + int rc, i = 0; - if (!dev) - return NULL; - tmp = get_device(&dev->dev); - if (tmp) - return to_platform_device(tmp); - else - return NULL; -} -EXPORT_SYMBOL(of_dev_get); - -void of_dev_put(struct platform_device *dev) -{ - if (dev) - put_device(&dev->dev); -} -EXPORT_SYMBOL(of_dev_put); - -int of_device_add(struct platform_device *ofdev) -{ - BUG_ON(ofdev->dev.of_node == NULL); - - /* name and id have to be set so that the platform bus doesn't get - * confused on matching */ - ofdev->name = dev_name(&ofdev->dev); - ofdev->id = -1; + if (!IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL)) + return; /* - * If this device has not binding numa node in devicetree, that is - * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this - * device is on the same node as the parent. + * If dev->of_node doesn't exist or doesn't contain memory-region, try + * the OF node having DMA configuration. */ - set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node)); + if (!of_property_present(of_node, "memory-region")) + of_node = np; + + of_for_each_phandle(&it, rc, of_node, "memory-region", NULL, 0) { + /* + * There might be multiple memory regions, but only one + * restricted-dma-pool region is allowed. + */ + if (of_device_is_compatible(it.node, "restricted-dma-pool") && + of_device_is_available(it.node)) { + if (of_reserved_mem_device_init_by_idx(dev, of_node, i)) + dev_warn(dev, "failed to initialise \"restricted-dma-pool\" memory node\n"); + of_node_put(it.node); + break; + } + i++; + } - return device_add(&ofdev->dev); } /** - * of_dma_configure - Setup DMA configuration + * of_dma_configure_id - Setup DMA configuration * @dev: Device to apply DMA configuration * @np: Pointer to OF node having DMA configuration + * @force_dma: Whether device is to be set up by of_dma_configure() even if + * DMA capability is not explicitly described by firmware. + * @id: Optional const pointer value input id * * Try to get devices's DMA configuration from DT and update it * accordingly. @@ -82,104 +81,94 @@ int of_device_add(struct platform_device *ofdev) * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events * to fix up DMA configuration. */ -int of_dma_configure(struct device *dev, struct device_node *np) +int of_dma_configure_id(struct device *dev, struct device_node *np, + bool force_dma, const u32 *id) { - u64 dma_addr, paddr, size; + const struct bus_dma_region *map = NULL; + struct device_node *bus_np; + u64 mask, end = 0; + bool coherent, set_map = false; int ret; - bool coherent; - unsigned long offset; - const struct iommu_ops *iommu; - /* - * Set default coherent_dma_mask to 32 bit. Drivers are expected to - * setup the correct supported mask. - */ - if (!dev->coherent_dma_mask) - dev->coherent_dma_mask = DMA_BIT_MASK(32); + if (dev->dma_range_map) { + dev_dbg(dev, "dma_range_map already set\n"); + goto skip_map; + } - /* - * Set it to coherent_dma_mask by default if the architecture - * code has not set it. - */ - if (!dev->dma_mask) - dev->dma_mask = &dev->coherent_dma_mask; + if (np == dev->of_node) + bus_np = __of_get_dma_parent(np); + else + bus_np = of_node_get(np); - ret = of_dma_get_range(np, &dma_addr, &paddr, &size); + ret = of_dma_get_range(bus_np, &map); + of_node_put(bus_np); if (ret < 0) { - dma_addr = offset = 0; - size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); - } else { - offset = PFN_DOWN(paddr - dma_addr); - /* - * Add a work around to treat the size as mask + 1 in case - * it is defined in DT as a mask. + * For legacy reasons, we have to assume some devices need + * DMA configuration regardless of whether "dma-ranges" is + * correctly specified or not. */ - if (size & 1) { - dev_warn(dev, "Invalid size 0x%llx for dma-range\n", - size); - size = size + 1; - } - - if (!size) { - dev_err(dev, "Adjusted size 0x%llx invalid\n", size); - return -EINVAL; - } - dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset); + if (!force_dma) + return ret == -ENODEV ? 0 : ret; + } else { + /* Determine the overall bounds of all DMA regions */ + end = dma_range_map_max(map); + set_map = true; + } +skip_map: + /* + * If @dev is expected to be DMA-capable then the bus code that created + * it should have initialised its dma_mask pointer by this point. For + * now, we'll continue the legacy behaviour of coercing it to the + * coherent mask if not, but we'll no longer do so quietly. + */ + if (!dev->dma_mask) { + dev_warn(dev, "DMA mask not set\n"); + dev->dma_mask = &dev->coherent_dma_mask; } - dev->dma_pfn_offset = offset; + if (!end && dev->coherent_dma_mask) + end = dev->coherent_dma_mask; + else if (!end) + end = (1ULL << 32) - 1; /* * Limit coherent and dma mask based on size and default mask * set by the driver. */ - dev->coherent_dma_mask = min(dev->coherent_dma_mask, - DMA_BIT_MASK(ilog2(dma_addr + size))); - *dev->dma_mask = min((*dev->dma_mask), - DMA_BIT_MASK(ilog2(dma_addr + size))); + mask = DMA_BIT_MASK(ilog2(end) + 1); + dev->coherent_dma_mask &= mask; + *dev->dma_mask &= mask; + /* ...but only set bus limit and range map if we found valid dma-ranges earlier */ + if (set_map) { + dev->bus_dma_limit = end; + dev->dma_range_map = map; + } coherent = of_dma_is_coherent(np); dev_dbg(dev, "device is%sdma coherent\n", coherent ? " " : " not "); - iommu = of_iommu_configure(dev, np); - if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER) + ret = of_iommu_configure(dev, np, id); + if (ret == -EPROBE_DEFER) { + /* Don't touch range map if it wasn't set from a valid dma-ranges */ + if (set_map) + dev->dma_range_map = NULL; + kfree(map); return -EPROBE_DEFER; - + } + /* Take all other IOMMU errors to mean we'll just carry on without it */ dev_dbg(dev, "device is%sbehind an iommu\n", - iommu ? " " : " not "); + !ret ? " " : " not "); - arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent); + arch_setup_dma_ops(dev, coherent); - return 0; -} -EXPORT_SYMBOL_GPL(of_dma_configure); - -/** - * of_dma_deconfigure - Clean up DMA configuration - * @dev: Device for which to clean up DMA configuration - * - * Clean up all configuration performed by of_dma_configure_ops() and free all - * resources that have been allocated. - */ -void of_dma_deconfigure(struct device *dev) -{ - arch_teardown_dma_ops(dev); -} - -int of_device_register(struct platform_device *pdev) -{ - device_initialize(&pdev->dev); - return of_device_add(pdev); -} -EXPORT_SYMBOL(of_device_register); + if (ret) + of_dma_set_restricted_buffer(dev, np); -void of_device_unregister(struct platform_device *ofdev) -{ - device_unregister(&ofdev->dev); + return 0; } -EXPORT_SYMBOL(of_device_unregister); +EXPORT_SYMBOL_GPL(of_dma_configure_id); const void *of_device_get_match_data(const struct device *dev) { @@ -193,87 +182,24 @@ const void *of_device_get_match_data(const struct device *dev) } EXPORT_SYMBOL(of_device_get_match_data); -static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len) -{ - const char *compat; - int cplen, i; - ssize_t tsize, csize, repend; - - if ((!dev) || (!dev->of_node)) - return -ENODEV; - - /* Name & Type */ - csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name, - dev->of_node->type); - - /* Get compatible property if any */ - compat = of_get_property(dev->of_node, "compatible", &cplen); - if (!compat) - return csize; - - /* Find true end (we tolerate multiple \0 at the end */ - for (i = (cplen - 1); i >= 0 && !compat[i]; i--) - cplen--; - if (!cplen) - return csize; - cplen++; - - /* Check space (need cplen+1 chars including final \0) */ - tsize = csize + cplen; - repend = tsize; - - if (csize >= len) /* @ the limit, all is already filled */ - return tsize; - - if (tsize >= len) { /* limit compat list */ - cplen = len - csize - 1; - repend = len; - } - - /* Copy and do char replacement */ - memcpy(&str[csize + 1], compat, cplen); - for (i = csize; i < repend; i++) { - char c = str[i]; - if (c == '\0') - str[i] = 'C'; - else if (c == ' ') - str[i] = '_'; - } - - return repend; -} - -int of_device_request_module(struct device *dev) -{ - char *str; - ssize_t size; - int ret; - - size = of_device_get_modalias(dev, NULL, 0); - if (size < 0) - return size; - - str = kmalloc(size + 1, GFP_KERNEL); - if (!str) - return -ENOMEM; - - of_device_get_modalias(dev, str, size); - str[size] = '\0'; - ret = request_module(str); - kfree(str); - - return ret; -} -EXPORT_SYMBOL_GPL(of_device_request_module); - /** * of_device_modalias - Fill buffer with newline terminated modalias string + * @dev: Calling device + * @str: Modalias string + * @len: Size of @str */ ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len) { - ssize_t sl = of_device_get_modalias(dev, str, len - 2); + ssize_t sl; + + if (!dev || !dev->of_node || dev->of_node_reused) + return -ENODEV; + + sl = of_modalias(dev->of_node, str, len - 2); if (sl < 0) return sl; + if (sl > len - 2) + return -ENOMEM; str[sl++] = '\n'; str[sl] = 0; @@ -283,30 +209,30 @@ EXPORT_SYMBOL_GPL(of_device_modalias); /** * of_device_uevent - Display OF related uevent information + * @dev: Device to display the uevent information for + * @env: Kernel object's userspace event reference to fill up */ -void of_device_uevent(struct device *dev, struct kobj_uevent_env *env) +void of_device_uevent(const struct device *dev, struct kobj_uevent_env *env) { - const char *compat; + const char *compat, *type; struct alias_prop *app; - int seen = 0, cplen, sl; + struct property *p; + int seen = 0; if ((!dev) || (!dev->of_node)) return; - add_uevent_var(env, "OF_NAME=%s", dev->of_node->name); - add_uevent_var(env, "OF_FULLNAME=%s", dev->of_node->full_name); - if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0) - add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type); + add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node); + add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node); + type = of_node_get_device_type(dev->of_node); + if (type) + add_uevent_var(env, "OF_TYPE=%s", type); /* Since the compatible field can contain pretty much anything * it's not really legal to split it out with commas. We split it * up using a number of environment variables instead. */ - compat = of_get_property(dev->of_node, "compatible", &cplen); - while (compat && *compat && cplen > 0) { + of_property_for_each_string(dev->of_node, "compatible", p, compat) { add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat); - sl = strlen(compat) + 1; - compat += sl; - cplen -= sl; seen++; } add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen); @@ -322,20 +248,23 @@ void of_device_uevent(struct device *dev, struct kobj_uevent_env *env) } mutex_unlock(&of_mutex); } +EXPORT_SYMBOL_GPL(of_device_uevent); -int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) +int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *env) { int sl; - if ((!dev) || (!dev->of_node)) + if ((!dev) || (!dev->of_node) || dev->of_node_reused) return -ENODEV; /* Devicetree modalias is tricky, we add it in 2 steps */ if (add_uevent_var(env, "MODALIAS=")) return -ENOMEM; - sl = of_device_get_modalias(dev, &env->buf[env->buflen-1], - sizeof(env->buf) - env->buflen); + sl = of_modalias(dev->of_node, &env->buf[env->buflen-1], + sizeof(env->buf) - env->buflen); + if (sl < 0) + return sl; if (sl >= (sizeof(env->buf) - env->buflen)) return -ENOMEM; env->buflen += sl; @@ -343,3 +272,44 @@ int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) return 0; } EXPORT_SYMBOL_GPL(of_device_uevent_modalias); + +/** + * of_device_make_bus_id - Use the device node data to assign a unique name + * @dev: pointer to device structure that is linked to a device tree node + * + * This routine will first try using the translated bus address to + * derive a unique name. If it cannot, then it will prepend names from + * parent nodes until a unique name can be derived. + */ +void of_device_make_bus_id(struct device *dev) +{ + struct device_node *node = dev->of_node; + const __be32 *reg; + u64 addr; + u32 mask; + + /* Construct the name, using parent nodes if necessary to ensure uniqueness */ + while (node->parent) { + /* + * If the address can be translated, then that is as much + * uniqueness as we need. Make it the first component and return + */ + reg = of_get_property(node, "reg", NULL); + if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) { + if (!of_property_read_u32(node, "mask", &mask)) + dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn", + addr, ffs(mask) - 1, node, dev_name(dev)); + + else + dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn", + addr, node, dev_name(dev)); + return; + } + + /* format arguments only used if dev_name() resolves to NULL */ + dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", + kbasename(node->full_name), dev_name(dev)); + node = node->parent; + } +} +EXPORT_SYMBOL_GPL(of_device_make_bus_id); |
