0001
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
0022
0023
0024
0025
0026
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
0042
0043 ofdev->name = dev_name(&ofdev->dev);
0044 ofdev->id = PLATFORM_DEVID_NONE;
0045
0046
0047
0048
0049
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
0069
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
0081
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
0093
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
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
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
0128
0129
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
0138 for (dma_start = ~0; r->size; r++) {
0139
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
0149
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
0166
0167
0168
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
0182
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
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
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
0256
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
0308
0309
0310
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
0328
0329
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
0348
0349
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
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);