blob: c5a9473a5fb1aa405d96b44532669f11c2d09843 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0
Stephen Rothwellf85ff302007-05-01 16:40:36 +10002#include <linux/string.h>
3#include <linux/kernel.h>
4#include <linux/of.h>
Stephen Rothwellf898f8d2007-05-01 16:49:51 +10005#include <linux/of_device.h>
Murali Karicheri1f5c69a2015-03-03 12:52:09 -05006#include <linux/of_address.h>
7#include <linux/of_iommu.h>
Jim Quinlane0d07272020-09-17 18:43:40 +02008#include <linux/dma-direct.h> /* for bus_dma_region */
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +02009#include <linux/dma-map-ops.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +100010#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/mod_devicetable.h>
13#include <linux/slab.h>
Robin Murphy72328882017-08-31 11:32:54 +010014#include <linux/platform_device.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +100015
16#include <asm/errno.h>
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080017#include "of_private.h"
Stephen Rothwellf85ff302007-05-01 16:40:36 +100018
19/**
Grant Likely44504b22010-04-13 16:13:22 -070020 * of_match_device - Tell if a struct device matches an of_device_id list
Jojo Zeng8e94fd32019-04-25 09:45:54 +080021 * @matches: array of of device match structures to search in
Stephen Rothwellf85ff302007-05-01 16:40:36 +100022 * @dev: the of device structure to match against
23 *
Grant Likely2dc11582010-08-06 09:25:50 -060024 * Used by a driver to check whether an platform_device present in the
Stephen Rothwellf85ff302007-05-01 16:40:36 +100025 * system is in its list of supported devices.
26 */
27const struct of_device_id *of_match_device(const struct of_device_id *matches,
Grant Likely44504b22010-04-13 16:13:22 -070028 const struct device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100029{
Grant Likely8cec0e72010-06-08 07:48:17 -060030 if ((!matches) || (!dev->of_node))
Stephen Rothwellf85ff302007-05-01 16:40:36 +100031 return NULL;
Grant Likely44504b22010-04-13 16:13:22 -070032 return of_match_node(matches, dev->of_node);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100033}
34EXPORT_SYMBOL(of_match_device);
35
Grant Likely7096d042010-10-20 11:45:13 -060036int of_device_add(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100037{
Grant Likely61c7a082010-04-13 16:12:29 -070038 BUG_ON(ofdev->dev.of_node == NULL);
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000039
Grant Likelyeca39302010-06-08 07:48:21 -060040 /* name and id have to be set so that the platform bus doesn't get
41 * confused on matching */
42 ofdev->name = dev_name(&ofdev->dev);
Andy Shevchenko6d7e3bf82017-08-25 18:33:13 +030043 ofdev->id = PLATFORM_DEVID_NONE;
Grant Likelyeca39302010-06-08 07:48:21 -060044
Zhen Lei56f2de82015-08-25 12:08:22 +080045 /*
46 * If this device has not binding numa node in devicetree, that is
47 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
48 * device is on the same node as the parent.
49 */
50 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000051
52 return device_add(&ofdev->dev);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100053}
Grant Likely7096d042010-10-20 11:45:13 -060054
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050055/**
Lee Jonescb61e9d2021-03-18 10:40:27 +000056 * of_dma_configure_id - Setup DMA configuration
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050057 * @dev: Device to apply DMA configuration
58 * @np: Pointer to OF node having DMA configuration
Christoph Hellwig3d6ce862018-05-03 16:25:08 +020059 * @force_dma: Whether device is to be set up by of_dma_configure() even if
60 * DMA capability is not explicitly described by firmware.
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +010061 * @id: Optional const pointer value input id
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050062 *
63 * Try to get devices's DMA configuration from DT and update it
64 * accordingly.
65 *
66 * If platform code needs to use its own special DMA configuration, it
67 * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
68 * to fix up DMA configuration.
69 */
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +010070int of_dma_configure_id(struct device *dev, struct device_node *np,
71 bool force_dma, const u32 *id)
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050072{
Robin Murphy53c92d72016-04-07 18:42:05 +010073 const struct iommu_ops *iommu;
Jim Quinlane0d07272020-09-17 18:43:40 +020074 const struct bus_dma_region *map = NULL;
Geert Uytterhoeven48ab6d52020-10-26 16:27:55 +010075 u64 dma_start = 0;
Jim Quinlane0d07272020-09-17 18:43:40 +020076 u64 mask, end, size = 0;
77 bool coherent;
78 int ret;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050079
Jim Quinlane0d07272020-09-17 18:43:40 +020080 ret = of_dma_get_range(np, &map);
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050081 if (ret < 0) {
Robin Murphy72328882017-08-31 11:32:54 +010082 /*
83 * For legacy reasons, we have to assume some devices need
84 * DMA configuration regardless of whether "dma-ranges" is
85 * correctly specified or not.
86 */
Christoph Hellwig3d6ce862018-05-03 16:25:08 +020087 if (!force_dma)
Robin Murphy72328882017-08-31 11:32:54 +010088 return ret == -ENODEV ? 0 : ret;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050089 } else {
Jim Quinlane0d07272020-09-17 18:43:40 +020090 const struct bus_dma_region *r = map;
Geert Uytterhoeven48ab6d52020-10-26 16:27:55 +010091 u64 dma_end = 0;
Jim Quinlane0d07272020-09-17 18:43:40 +020092
93 /* Determine the overall bounds of all DMA regions */
Geert Uytterhoeven495023e2020-11-02 11:54:22 +010094 for (dma_start = ~0; r->size; r++) {
Jim Quinlane0d07272020-09-17 18:43:40 +020095 /* Take lower and upper limits */
96 if (r->dma_start < dma_start)
97 dma_start = r->dma_start;
98 if (r->dma_start + r->size > dma_end)
99 dma_end = r->dma_start + r->size;
100 }
101 size = dma_end - dma_start;
Murali Karicheri0c79c812015-03-03 12:52:10 -0500102
103 /*
104 * Add a work around to treat the size as mask + 1 in case
105 * it is defined in DT as a mask.
106 */
107 if (size & 1) {
Jim Quinlane0d07272020-09-17 18:43:40 +0200108 dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
Murali Karicheri0c79c812015-03-03 12:52:10 -0500109 size);
110 size = size + 1;
111 }
112
113 if (!size) {
114 dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
Jim Quinlane0d07272020-09-17 18:43:40 +0200115 kfree(map);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530116 return -EINVAL;
Murali Karicheri0c79c812015-03-03 12:52:10 -0500117 }
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500118 }
119
Robin Murphy72328882017-08-31 11:32:54 +0100120 /*
Robin Murphy4d8bde82018-07-23 23:16:12 +0100121 * If @dev is expected to be DMA-capable then the bus code that created
122 * it should have initialised its dma_mask pointer by this point. For
123 * now, we'll continue the legacy behaviour of coercing it to the
124 * coherent mask if not, but we'll no longer do so quietly.
Robin Murphy72328882017-08-31 11:32:54 +0100125 */
Robin Murphy4d8bde82018-07-23 23:16:12 +0100126 if (!dev->dma_mask) {
127 dev_warn(dev, "DMA mask not set\n");
Robin Murphy72328882017-08-31 11:32:54 +0100128 dev->dma_mask = &dev->coherent_dma_mask;
Robin Murphy4d8bde82018-07-23 23:16:12 +0100129 }
Robin Murphy72328882017-08-31 11:32:54 +0100130
Robin Murphy4d8bde82018-07-23 23:16:12 +0100131 if (!size && dev->coherent_dma_mask)
Robin Murphy72328882017-08-31 11:32:54 +0100132 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
Robin Murphy4d8bde82018-07-23 23:16:12 +0100133 else if (!size)
134 size = 1ULL << 32;
Robin Murphy72328882017-08-31 11:32:54 +0100135
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600136 /*
137 * Limit coherent and dma mask based on size and default mask
138 * set by the driver.
139 */
Jim Quinlane0d07272020-09-17 18:43:40 +0200140 end = dma_start + size - 1;
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100141 mask = DMA_BIT_MASK(ilog2(end) + 1);
Robin Murphyee7b1f32017-08-11 17:29:56 +0100142 dev->coherent_dma_mask &= mask;
143 *dev->dma_mask &= mask;
Yong Wu89c7cb12021-01-19 18:52:03 +0800144 /* ...but only set bus limit and range map if we found valid dma-ranges earlier */
145 if (!ret) {
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100146 dev->bus_dma_limit = end;
Yong Wu89c7cb12021-01-19 18:52:03 +0800147 dev->dma_range_map = map;
148 }
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600149
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500150 coherent = of_dma_is_coherent(np);
151 dev_dbg(dev, "device is%sdma coherent\n",
152 coherent ? " " : " not ");
153
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +0100154 iommu = of_iommu_configure(dev, np, id);
Jim Quinlane0d07272020-09-17 18:43:40 +0200155 if (PTR_ERR(iommu) == -EPROBE_DEFER) {
Yong Wu89c7cb12021-01-19 18:52:03 +0800156 /* Don't touch range map if it wasn't set from a valid dma-ranges */
157 if (!ret)
158 dev->dma_range_map = NULL;
Jim Quinlane0d07272020-09-17 18:43:40 +0200159 kfree(map);
Sricharan Ra37b19a2017-05-27 19:17:41 +0530160 return -EPROBE_DEFER;
Jim Quinlane0d07272020-09-17 18:43:40 +0200161 }
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530162
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500163 dev_dbg(dev, "device is%sbehind an iommu\n",
164 iommu ? " " : " not ");
165
Jim Quinlane0d07272020-09-17 18:43:40 +0200166 arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530167
168 return 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500169}
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +0100170EXPORT_SYMBOL_GPL(of_dma_configure_id);
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500171
Grant Likely7096d042010-10-20 11:45:13 -0600172int of_device_register(struct platform_device *pdev)
173{
174 device_initialize(&pdev->dev);
175 return of_device_add(pdev);
176}
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000177EXPORT_SYMBOL(of_device_register);
178
Grant Likely94a0cb12010-07-22 13:59:23 -0600179void of_device_unregister(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000180{
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000181 device_unregister(&ofdev->dev);
182}
183EXPORT_SYMBOL(of_device_unregister);
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000184
Joachim Eastwood3386e0f2015-05-06 20:09:09 +0200185const void *of_device_get_match_data(const struct device *dev)
186{
187 const struct of_device_id *match;
188
189 match = of_match_device(dev->driver->of_match_table, dev);
190 if (!match)
191 return NULL;
192
193 return match->data;
194}
195EXPORT_SYMBOL(of_device_get_match_data);
196
Rob Herring0634c292017-03-22 09:16:27 -0500197static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000198{
199 const char *compat;
Rob Herringbc575062017-07-21 15:45:32 -0500200 char *c;
201 struct property *p;
202 ssize_t csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700203 ssize_t tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000204
Zhang Ruib9f73062014-01-14 16:46:38 +0800205 if ((!dev) || (!dev->of_node))
206 return -ENODEV;
207
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000208 /* Name & Type */
Rob Herringa613b262018-08-27 20:00:19 -0500209 /* %p eats all alphanum characters, so %c must be used here */
210 csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
Rob Herringe8b1dee22018-08-29 08:36:12 -0500211 of_node_get_device_type(dev->of_node));
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700212 tsize = csize;
Rob Herringbc575062017-07-21 15:45:32 -0500213 len -= csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700214 if (str)
215 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000216
Rob Herringbc575062017-07-21 15:45:32 -0500217 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700218 csize = strlen(compat) + 1;
219 tsize += csize;
220 if (csize > len)
221 continue;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000222
Rob Herringbc575062017-07-21 15:45:32 -0500223 csize = snprintf(str, len, "C%s", compat);
224 for (c = str; c; ) {
225 c = strchr(c, ' ');
226 if (c)
227 *c++ = '_';
228 }
229 len -= csize;
230 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000231 }
232
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700233 return tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000234}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600235
Stephen Boyd9c829c02016-12-28 14:56:47 -0800236int of_device_request_module(struct device *dev)
237{
238 char *str;
239 ssize_t size;
240 int ret;
241
242 size = of_device_get_modalias(dev, NULL, 0);
243 if (size < 0)
244 return size;
245
246 str = kmalloc(size + 1, GFP_KERNEL);
247 if (!str)
248 return -ENOMEM;
249
250 of_device_get_modalias(dev, str, size);
251 str[size] = '\0';
252 ret = request_module(str);
253 kfree(str);
254
255 return ret;
256}
257EXPORT_SYMBOL_GPL(of_device_request_module);
258
Grant Likelydd27dcd2010-06-08 07:48:12 -0600259/**
Rob Herring0634c292017-03-22 09:16:27 -0500260 * of_device_modalias - Fill buffer with newline terminated modalias string
Lee Jonescb61e9d2021-03-18 10:40:27 +0000261 * @dev: Calling device
262 * @str: Modalias string
263 * @len: Size of @str
Rob Herring0634c292017-03-22 09:16:27 -0500264 */
265ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
266{
267 ssize_t sl = of_device_get_modalias(dev, str, len - 2);
268 if (sl < 0)
269 return sl;
Bjorn Andersson08ab58d2017-08-23 18:04:04 -0700270 if (sl > len - 2)
271 return -ENOMEM;
Rob Herring0634c292017-03-22 09:16:27 -0500272
273 str[sl++] = '\n';
274 str[sl] = 0;
275 return sl;
276}
277EXPORT_SYMBOL_GPL(of_device_modalias);
278
279/**
Grant Likelydd27dcd2010-06-08 07:48:12 -0600280 * of_device_uevent - Display OF related uevent information
Lee Jonescb61e9d2021-03-18 10:40:27 +0000281 * @dev: Device to apply DMA configuration
282 * @env: Kernel object's userspace event reference
Grant Likelydd27dcd2010-06-08 07:48:12 -0600283 */
Grant Likely07d57a32012-02-01 11:22:22 -0700284void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Grant Likelydd27dcd2010-06-08 07:48:12 -0600285{
Rob Herringe8b1dee22018-08-29 08:36:12 -0500286 const char *compat, *type;
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800287 struct alias_prop *app;
Rob Herringbc575062017-07-21 15:45:32 -0500288 struct property *p;
289 int seen = 0;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600290
291 if ((!dev) || (!dev->of_node))
Grant Likely07d57a32012-02-01 11:22:22 -0700292 return;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600293
Rob Herringa613b262018-08-27 20:00:19 -0500294 add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
Rob Herring0d638a02017-06-01 15:50:55 -0500295 add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
Rob Herringe8b1dee22018-08-29 08:36:12 -0500296 type = of_node_get_device_type(dev->of_node);
297 if (type)
298 add_uevent_var(env, "OF_TYPE=%s", type);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600299
300 /* Since the compatible field can contain pretty much anything
301 * it's not really legal to split it out with commas. We split it
302 * up using a number of environment variables instead. */
Rob Herringbc575062017-07-21 15:45:32 -0500303 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Grant Likely07d57a32012-02-01 11:22:22 -0700304 add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600305 seen++;
306 }
Grant Likely07d57a32012-02-01 11:22:22 -0700307 add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800308
309 seen = 0;
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300310 mutex_lock(&of_mutex);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800311 list_for_each_entry(app, &aliases_lookup, link) {
312 if (dev->of_node == app->np) {
313 add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
314 app->alias);
315 seen++;
316 }
317 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300318 mutex_unlock(&of_mutex);
Grant Likely07d57a32012-02-01 11:22:22 -0700319}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600320
Grant Likely07d57a32012-02-01 11:22:22 -0700321int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
322{
323 int sl;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600324
Grant Likely07d57a32012-02-01 11:22:22 -0700325 if ((!dev) || (!dev->of_node))
326 return -ENODEV;
327
328 /* Devicetree modalias is tricky, we add it in 2 steps */
Grant Likelydd27dcd2010-06-08 07:48:12 -0600329 if (add_uevent_var(env, "MODALIAS="))
330 return -ENOMEM;
331
Grant Likely34a1c1e2010-06-08 07:48:13 -0600332 sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
Grant Likelydd27dcd2010-06-08 07:48:12 -0600333 sizeof(env->buf) - env->buflen);
334 if (sl >= (sizeof(env->buf) - env->buflen))
335 return -ENOMEM;
336 env->buflen += sl;
337
338 return 0;
339}
Stephen Boyd7a3b7cd2016-12-28 14:56:48 -0800340EXPORT_SYMBOL_GPL(of_device_uevent_modalias);