Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/string.h>
0003 #include <linux/kernel.h>
0004 #include <linux/of.h>
0005 #include <linux/of_device.h>
0006 #include <linux/of_address.h>
0007 #include <linux/of_iommu.h>
0008 #include <linux/of_reserved_mem.h>
0009 #include <linux/dma-direct.h> /* for bus_dma_region */
0010 #include <linux/dma-map-ops.h>
0011 #include <linux/init.h>
0012 #include <linux/module.h>
0013 #include <linux/mod_devicetable.h>
0014 #include <linux/slab.h>
0015 #include <linux/platform_device.h>
0016 
0017 #include <asm/errno.h>
0018 #include "of_private.h"
0019 
0020 /**
0021  * of_match_device - Tell if a struct device matches an of_device_id list
0022  * @matches: array of of device match structures to search in
0023  * @dev: the of device structure to match against
0024  *
0025  * Used by a driver to check whether an platform_device present in the
0026  * system is in its list of supported devices.
0027  */
0028 const struct of_device_id *of_match_device(const struct of_device_id *matches,
0029                        const struct device *dev)
0030 {
0031     if (!matches || !dev->of_node || dev->of_node_reused)
0032         return NULL;
0033     return of_match_node(matches, dev->of_node);
0034 }
0035 EXPORT_SYMBOL(of_match_device);
0036 
0037 int of_device_add(struct platform_device *ofdev)
0038 {
0039     BUG_ON(ofdev->dev.of_node == NULL);
0040 
0041     /* name and id have to be set so that the platform bus doesn't get
0042      * confused on matching */
0043     ofdev->name = dev_name(&ofdev->dev);
0044     ofdev->id = PLATFORM_DEVID_NONE;
0045 
0046     /*
0047      * If this device has not binding numa node in devicetree, that is
0048      * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
0049      * device is on the same node as the parent.
0050      */
0051     set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
0052 
0053     return device_add(&ofdev->dev);
0054 }
0055 
0056 static void
0057 of_dma_set_restricted_buffer(struct device *dev, struct device_node *np)
0058 {
0059     struct device_node *node, *of_node = dev->of_node;
0060     int count, i;
0061 
0062     if (!IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL))
0063         return;
0064 
0065     count = of_property_count_elems_of_size(of_node, "memory-region",
0066                         sizeof(u32));
0067     /*
0068      * If dev->of_node doesn't exist or doesn't contain memory-region, try
0069      * the OF node having DMA configuration.
0070      */
0071     if (count <= 0) {
0072         of_node = np;
0073         count = of_property_count_elems_of_size(
0074             of_node, "memory-region", sizeof(u32));
0075     }
0076 
0077     for (i = 0; i < count; i++) {
0078         node = of_parse_phandle(of_node, "memory-region", i);
0079         /*
0080          * There might be multiple memory regions, but only one
0081          * restricted-dma-pool region is allowed.
0082          */
0083         if (of_device_is_compatible(node, "restricted-dma-pool") &&
0084             of_device_is_available(node)) {
0085             of_node_put(node);
0086             break;
0087         }
0088         of_node_put(node);
0089     }
0090 
0091     /*
0092      * Attempt to initialize a restricted-dma-pool region if one was found.
0093      * Note that count can hold a negative error code.
0094      */
0095     if (i < count && of_reserved_mem_device_init_by_idx(dev, of_node, i))
0096         dev_warn(dev, "failed to initialise \"restricted-dma-pool\" memory node\n");
0097 }
0098 
0099 /**
0100  * of_dma_configure_id - Setup DMA configuration
0101  * @dev:    Device to apply DMA configuration
0102  * @np:     Pointer to OF node having DMA configuration
0103  * @force_dma:  Whether device is to be set up by of_dma_configure() even if
0104  *      DMA capability is not explicitly described by firmware.
0105  * @id:     Optional const pointer value input id
0106  *
0107  * Try to get devices's DMA configuration from DT and update it
0108  * accordingly.
0109  *
0110  * If platform code needs to use its own special DMA configuration, it
0111  * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
0112  * to fix up DMA configuration.
0113  */
0114 int of_dma_configure_id(struct device *dev, struct device_node *np,
0115             bool force_dma, const u32 *id)
0116 {
0117     const struct iommu_ops *iommu;
0118     const struct bus_dma_region *map = NULL;
0119     u64 dma_start = 0;
0120     u64 mask, end, size = 0;
0121     bool coherent;
0122     int ret;
0123 
0124     ret = of_dma_get_range(np, &map);
0125     if (ret < 0) {
0126         /*
0127          * For legacy reasons, we have to assume some devices need
0128          * DMA configuration regardless of whether "dma-ranges" is
0129          * correctly specified or not.
0130          */
0131         if (!force_dma)
0132             return ret == -ENODEV ? 0 : ret;
0133     } else {
0134         const struct bus_dma_region *r = map;
0135         u64 dma_end = 0;
0136 
0137         /* Determine the overall bounds of all DMA regions */
0138         for (dma_start = ~0; r->size; r++) {
0139             /* Take lower and upper limits */
0140             if (r->dma_start < dma_start)
0141                 dma_start = r->dma_start;
0142             if (r->dma_start + r->size > dma_end)
0143                 dma_end = r->dma_start + r->size;
0144         }
0145         size = dma_end - dma_start;
0146 
0147         /*
0148          * Add a work around to treat the size as mask + 1 in case
0149          * it is defined in DT as a mask.
0150          */
0151         if (size & 1) {
0152             dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
0153                  size);
0154             size = size + 1;
0155         }
0156 
0157         if (!size) {
0158             dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
0159             kfree(map);
0160             return -EINVAL;
0161         }
0162     }
0163 
0164     /*
0165      * If @dev is expected to be DMA-capable then the bus code that created
0166      * it should have initialised its dma_mask pointer by this point. For
0167      * now, we'll continue the legacy behaviour of coercing it to the
0168      * coherent mask if not, but we'll no longer do so quietly.
0169      */
0170     if (!dev->dma_mask) {
0171         dev_warn(dev, "DMA mask not set\n");
0172         dev->dma_mask = &dev->coherent_dma_mask;
0173     }
0174 
0175     if (!size && dev->coherent_dma_mask)
0176         size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
0177     else if (!size)
0178         size = 1ULL << 32;
0179 
0180     /*
0181      * Limit coherent and dma mask based on size and default mask
0182      * set by the driver.
0183      */
0184     end = dma_start + size - 1;
0185     mask = DMA_BIT_MASK(ilog2(end) + 1);
0186     dev->coherent_dma_mask &= mask;
0187     *dev->dma_mask &= mask;
0188     /* ...but only set bus limit and range map if we found valid dma-ranges earlier */
0189     if (!ret) {
0190         dev->bus_dma_limit = end;
0191         dev->dma_range_map = map;
0192     }
0193 
0194     coherent = of_dma_is_coherent(np);
0195     dev_dbg(dev, "device is%sdma coherent\n",
0196         coherent ? " " : " not ");
0197 
0198     iommu = of_iommu_configure(dev, np, id);
0199     if (PTR_ERR(iommu) == -EPROBE_DEFER) {
0200         /* Don't touch range map if it wasn't set from a valid dma-ranges */
0201         if (!ret)
0202             dev->dma_range_map = NULL;
0203         kfree(map);
0204         return -EPROBE_DEFER;
0205     }
0206 
0207     dev_dbg(dev, "device is%sbehind an iommu\n",
0208         iommu ? " " : " not ");
0209 
0210     arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
0211 
0212     if (!iommu)
0213         of_dma_set_restricted_buffer(dev, np);
0214 
0215     return 0;
0216 }
0217 EXPORT_SYMBOL_GPL(of_dma_configure_id);
0218 
0219 int of_device_register(struct platform_device *pdev)
0220 {
0221     device_initialize(&pdev->dev);
0222     return of_device_add(pdev);
0223 }
0224 EXPORT_SYMBOL(of_device_register);
0225 
0226 void of_device_unregister(struct platform_device *ofdev)
0227 {
0228     device_unregister(&ofdev->dev);
0229 }
0230 EXPORT_SYMBOL(of_device_unregister);
0231 
0232 const void *of_device_get_match_data(const struct device *dev)
0233 {
0234     const struct of_device_id *match;
0235 
0236     match = of_match_device(dev->driver->of_match_table, dev);
0237     if (!match)
0238         return NULL;
0239 
0240     return match->data;
0241 }
0242 EXPORT_SYMBOL(of_device_get_match_data);
0243 
0244 static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
0245 {
0246     const char *compat;
0247     char *c;
0248     struct property *p;
0249     ssize_t csize;
0250     ssize_t tsize;
0251 
0252     if ((!dev) || (!dev->of_node))
0253         return -ENODEV;
0254 
0255     /* Name & Type */
0256     /* %p eats all alphanum characters, so %c must be used here */
0257     csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
0258              of_node_get_device_type(dev->of_node));
0259     tsize = csize;
0260     len -= csize;
0261     if (str)
0262         str += csize;
0263 
0264     of_property_for_each_string(dev->of_node, "compatible", p, compat) {
0265         csize = strlen(compat) + 1;
0266         tsize += csize;
0267         if (csize > len)
0268             continue;
0269 
0270         csize = snprintf(str, len, "C%s", compat);
0271         for (c = str; c; ) {
0272             c = strchr(c, ' ');
0273             if (c)
0274                 *c++ = '_';
0275         }
0276         len -= csize;
0277         str += csize;
0278     }
0279 
0280     return tsize;
0281 }
0282 
0283 int of_device_request_module(struct device *dev)
0284 {
0285     char *str;
0286     ssize_t size;
0287     int ret;
0288 
0289     size = of_device_get_modalias(dev, NULL, 0);
0290     if (size < 0)
0291         return size;
0292 
0293     str = kmalloc(size + 1, GFP_KERNEL);
0294     if (!str)
0295         return -ENOMEM;
0296 
0297     of_device_get_modalias(dev, str, size);
0298     str[size] = '\0';
0299     ret = request_module(str);
0300     kfree(str);
0301 
0302     return ret;
0303 }
0304 EXPORT_SYMBOL_GPL(of_device_request_module);
0305 
0306 /**
0307  * of_device_modalias - Fill buffer with newline terminated modalias string
0308  * @dev:    Calling device
0309  * @str:    Modalias string
0310  * @len:    Size of @str
0311  */
0312 ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
0313 {
0314     ssize_t sl = of_device_get_modalias(dev, str, len - 2);
0315     if (sl < 0)
0316         return sl;
0317     if (sl > len - 2)
0318         return -ENOMEM;
0319 
0320     str[sl++] = '\n';
0321     str[sl] = 0;
0322     return sl;
0323 }
0324 EXPORT_SYMBOL_GPL(of_device_modalias);
0325 
0326 /**
0327  * of_device_uevent - Display OF related uevent information
0328  * @dev:    Device to apply DMA configuration
0329  * @env:    Kernel object's userspace event reference
0330  */
0331 void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
0332 {
0333     const char *compat, *type;
0334     struct alias_prop *app;
0335     struct property *p;
0336     int seen = 0;
0337 
0338     if ((!dev) || (!dev->of_node))
0339         return;
0340 
0341     add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
0342     add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
0343     type = of_node_get_device_type(dev->of_node);
0344     if (type)
0345         add_uevent_var(env, "OF_TYPE=%s", type);
0346 
0347     /* Since the compatible field can contain pretty much anything
0348      * it's not really legal to split it out with commas. We split it
0349      * up using a number of environment variables instead. */
0350     of_property_for_each_string(dev->of_node, "compatible", p, compat) {
0351         add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
0352         seen++;
0353     }
0354     add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
0355 
0356     seen = 0;
0357     mutex_lock(&of_mutex);
0358     list_for_each_entry(app, &aliases_lookup, link) {
0359         if (dev->of_node == app->np) {
0360             add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
0361                        app->alias);
0362             seen++;
0363         }
0364     }
0365     mutex_unlock(&of_mutex);
0366 }
0367 
0368 int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
0369 {
0370     int sl;
0371 
0372     if ((!dev) || (!dev->of_node))
0373         return -ENODEV;
0374 
0375     /* Devicetree modalias is tricky, we add it in 2 steps */
0376     if (add_uevent_var(env, "MODALIAS="))
0377         return -ENOMEM;
0378 
0379     sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
0380                     sizeof(env->buf) - env->buflen);
0381     if (sl >= (sizeof(env->buf) - env->buflen))
0382         return -ENOMEM;
0383     env->buflen += sl;
0384 
0385     return 0;
0386 }
0387 EXPORT_SYMBOL_GPL(of_device_uevent_modalias);