blob: 6e3ae7ebc33eeb7bc6edc75677563bdb6cb37445 [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 */
Stephen Rothwellf85ff302007-05-01 16:40:36 +10009#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/mod_devicetable.h>
12#include <linux/slab.h>
Robin Murphy72328882017-08-31 11:32:54 +010013#include <linux/platform_device.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +100014
15#include <asm/errno.h>
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080016#include "of_private.h"
Stephen Rothwellf85ff302007-05-01 16:40:36 +100017
18/**
Grant Likely44504b22010-04-13 16:13:22 -070019 * of_match_device - Tell if a struct device matches an of_device_id list
Jojo Zeng8e94fd32019-04-25 09:45:54 +080020 * @matches: array of of device match structures to search in
Stephen Rothwellf85ff302007-05-01 16:40:36 +100021 * @dev: the of device structure to match against
22 *
Grant Likely2dc11582010-08-06 09:25:50 -060023 * Used by a driver to check whether an platform_device present in the
Stephen Rothwellf85ff302007-05-01 16:40:36 +100024 * system is in its list of supported devices.
25 */
26const struct of_device_id *of_match_device(const struct of_device_id *matches,
Grant Likely44504b22010-04-13 16:13:22 -070027 const struct device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100028{
Grant Likely8cec0e72010-06-08 07:48:17 -060029 if ((!matches) || (!dev->of_node))
Stephen Rothwellf85ff302007-05-01 16:40:36 +100030 return NULL;
Grant Likely44504b22010-04-13 16:13:22 -070031 return of_match_node(matches, dev->of_node);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100032}
33EXPORT_SYMBOL(of_match_device);
34
Grant Likely94a0cb12010-07-22 13:59:23 -060035struct platform_device *of_dev_get(struct platform_device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100036{
37 struct device *tmp;
38
39 if (!dev)
40 return NULL;
41 tmp = get_device(&dev->dev);
42 if (tmp)
Grant Likely94a0cb12010-07-22 13:59:23 -060043 return to_platform_device(tmp);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100044 else
45 return NULL;
46}
47EXPORT_SYMBOL(of_dev_get);
48
Grant Likely94a0cb12010-07-22 13:59:23 -060049void of_dev_put(struct platform_device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100050{
51 if (dev)
52 put_device(&dev->dev);
53}
54EXPORT_SYMBOL(of_dev_put);
55
Grant Likely7096d042010-10-20 11:45:13 -060056int of_device_add(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100057{
Grant Likely61c7a082010-04-13 16:12:29 -070058 BUG_ON(ofdev->dev.of_node == NULL);
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000059
Grant Likelyeca39302010-06-08 07:48:21 -060060 /* name and id have to be set so that the platform bus doesn't get
61 * confused on matching */
62 ofdev->name = dev_name(&ofdev->dev);
Andy Shevchenko6d7e3bf82017-08-25 18:33:13 +030063 ofdev->id = PLATFORM_DEVID_NONE;
Grant Likelyeca39302010-06-08 07:48:21 -060064
Zhen Lei56f2de82015-08-25 12:08:22 +080065 /*
66 * If this device has not binding numa node in devicetree, that is
67 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
68 * device is on the same node as the parent.
69 */
70 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000071
72 return device_add(&ofdev->dev);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100073}
Grant Likely7096d042010-10-20 11:45:13 -060074
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050075/**
76 * of_dma_configure - Setup DMA configuration
77 * @dev: Device to apply DMA configuration
78 * @np: Pointer to OF node having DMA configuration
Christoph Hellwig3d6ce862018-05-03 16:25:08 +020079 * @force_dma: Whether device is to be set up by of_dma_configure() even if
80 * DMA capability is not explicitly described by firmware.
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +010081 * @id: Optional const pointer value input id
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050082 *
83 * Try to get devices's DMA configuration from DT and update it
84 * accordingly.
85 *
86 * If platform code needs to use its own special DMA configuration, it
87 * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
88 * to fix up DMA configuration.
89 */
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +010090int of_dma_configure_id(struct device *dev, struct device_node *np,
91 bool force_dma, const u32 *id)
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050092{
Robin Murphy53c92d72016-04-07 18:42:05 +010093 const struct iommu_ops *iommu;
Jim Quinlane0d07272020-09-17 18:43:40 +020094 const struct bus_dma_region *map = NULL;
95 dma_addr_t dma_start = 0;
96 u64 mask, end, size = 0;
97 bool coherent;
98 int ret;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050099
Jim Quinlane0d07272020-09-17 18:43:40 +0200100 ret = of_dma_get_range(np, &map);
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500101 if (ret < 0) {
Robin Murphy72328882017-08-31 11:32:54 +0100102 /*
103 * For legacy reasons, we have to assume some devices need
104 * DMA configuration regardless of whether "dma-ranges" is
105 * correctly specified or not.
106 */
Christoph Hellwig3d6ce862018-05-03 16:25:08 +0200107 if (!force_dma)
Robin Murphy72328882017-08-31 11:32:54 +0100108 return ret == -ENODEV ? 0 : ret;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500109 } else {
Jim Quinlane0d07272020-09-17 18:43:40 +0200110 const struct bus_dma_region *r = map;
111 dma_addr_t dma_end = 0;
112
113 /* Determine the overall bounds of all DMA regions */
114 for (dma_start = ~(dma_addr_t)0; r->size; r++) {
115 /* Take lower and upper limits */
116 if (r->dma_start < dma_start)
117 dma_start = r->dma_start;
118 if (r->dma_start + r->size > dma_end)
119 dma_end = r->dma_start + r->size;
120 }
121 size = dma_end - dma_start;
Murali Karicheri0c79c812015-03-03 12:52:10 -0500122
123 /*
124 * Add a work around to treat the size as mask + 1 in case
125 * it is defined in DT as a mask.
126 */
127 if (size & 1) {
Jim Quinlane0d07272020-09-17 18:43:40 +0200128 dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
Murali Karicheri0c79c812015-03-03 12:52:10 -0500129 size);
130 size = size + 1;
131 }
132
133 if (!size) {
134 dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
Jim Quinlane0d07272020-09-17 18:43:40 +0200135 kfree(map);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530136 return -EINVAL;
Murali Karicheri0c79c812015-03-03 12:52:10 -0500137 }
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500138 }
139
Robin Murphy72328882017-08-31 11:32:54 +0100140 /*
Robin Murphy4d8bde82018-07-23 23:16:12 +0100141 * If @dev is expected to be DMA-capable then the bus code that created
142 * it should have initialised its dma_mask pointer by this point. For
143 * now, we'll continue the legacy behaviour of coercing it to the
144 * coherent mask if not, but we'll no longer do so quietly.
Robin Murphy72328882017-08-31 11:32:54 +0100145 */
Robin Murphy4d8bde82018-07-23 23:16:12 +0100146 if (!dev->dma_mask) {
147 dev_warn(dev, "DMA mask not set\n");
Robin Murphy72328882017-08-31 11:32:54 +0100148 dev->dma_mask = &dev->coherent_dma_mask;
Robin Murphy4d8bde82018-07-23 23:16:12 +0100149 }
Robin Murphy72328882017-08-31 11:32:54 +0100150
Robin Murphy4d8bde82018-07-23 23:16:12 +0100151 if (!size && dev->coherent_dma_mask)
Robin Murphy72328882017-08-31 11:32:54 +0100152 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
Robin Murphy4d8bde82018-07-23 23:16:12 +0100153 else if (!size)
154 size = 1ULL << 32;
Robin Murphy72328882017-08-31 11:32:54 +0100155
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600156 /*
157 * Limit coherent and dma mask based on size and default mask
158 * set by the driver.
159 */
Jim Quinlane0d07272020-09-17 18:43:40 +0200160 end = dma_start + size - 1;
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100161 mask = DMA_BIT_MASK(ilog2(end) + 1);
Robin Murphyee7b1f32017-08-11 17:29:56 +0100162 dev->coherent_dma_mask &= mask;
163 *dev->dma_mask &= mask;
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100164 /* ...but only set bus limit if we found valid dma-ranges earlier */
Robin Murphy6778be42018-11-07 16:30:32 +0000165 if (!ret)
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100166 dev->bus_dma_limit = end;
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600167
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500168 coherent = of_dma_is_coherent(np);
169 dev_dbg(dev, "device is%sdma coherent\n",
170 coherent ? " " : " not ");
171
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +0100172 iommu = of_iommu_configure(dev, np, id);
Jim Quinlane0d07272020-09-17 18:43:40 +0200173 if (PTR_ERR(iommu) == -EPROBE_DEFER) {
174 kfree(map);
Sricharan Ra37b19a2017-05-27 19:17:41 +0530175 return -EPROBE_DEFER;
Jim Quinlane0d07272020-09-17 18:43:40 +0200176 }
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530177
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500178 dev_dbg(dev, "device is%sbehind an iommu\n",
179 iommu ? " " : " not ");
180
Jim Quinlane0d07272020-09-17 18:43:40 +0200181 arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530182
Jim Quinlane0d07272020-09-17 18:43:40 +0200183 dev->dma_range_map = map;
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530184 return 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500185}
Lorenzo Pieralisia081bd42020-06-19 09:20:08 +0100186EXPORT_SYMBOL_GPL(of_dma_configure_id);
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500187
Grant Likely7096d042010-10-20 11:45:13 -0600188int of_device_register(struct platform_device *pdev)
189{
190 device_initialize(&pdev->dev);
191 return of_device_add(pdev);
192}
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000193EXPORT_SYMBOL(of_device_register);
194
Grant Likely94a0cb12010-07-22 13:59:23 -0600195void of_device_unregister(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000196{
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000197 device_unregister(&ofdev->dev);
198}
199EXPORT_SYMBOL(of_device_unregister);
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000200
Joachim Eastwood3386e0f2015-05-06 20:09:09 +0200201const void *of_device_get_match_data(const struct device *dev)
202{
203 const struct of_device_id *match;
204
205 match = of_match_device(dev->driver->of_match_table, dev);
206 if (!match)
207 return NULL;
208
209 return match->data;
210}
211EXPORT_SYMBOL(of_device_get_match_data);
212
Rob Herring0634c292017-03-22 09:16:27 -0500213static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000214{
215 const char *compat;
Rob Herringbc575062017-07-21 15:45:32 -0500216 char *c;
217 struct property *p;
218 ssize_t csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700219 ssize_t tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000220
Zhang Ruib9f73062014-01-14 16:46:38 +0800221 if ((!dev) || (!dev->of_node))
222 return -ENODEV;
223
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000224 /* Name & Type */
Rob Herringa613b262018-08-27 20:00:19 -0500225 /* %p eats all alphanum characters, so %c must be used here */
226 csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
Rob Herringe8b1dee22018-08-29 08:36:12 -0500227 of_node_get_device_type(dev->of_node));
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700228 tsize = csize;
Rob Herringbc575062017-07-21 15:45:32 -0500229 len -= csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700230 if (str)
231 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000232
Rob Herringbc575062017-07-21 15:45:32 -0500233 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700234 csize = strlen(compat) + 1;
235 tsize += csize;
236 if (csize > len)
237 continue;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000238
Rob Herringbc575062017-07-21 15:45:32 -0500239 csize = snprintf(str, len, "C%s", compat);
240 for (c = str; c; ) {
241 c = strchr(c, ' ');
242 if (c)
243 *c++ = '_';
244 }
245 len -= csize;
246 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000247 }
248
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700249 return tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000250}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600251
Stephen Boyd9c829c02016-12-28 14:56:47 -0800252int of_device_request_module(struct device *dev)
253{
254 char *str;
255 ssize_t size;
256 int ret;
257
258 size = of_device_get_modalias(dev, NULL, 0);
259 if (size < 0)
260 return size;
261
262 str = kmalloc(size + 1, GFP_KERNEL);
263 if (!str)
264 return -ENOMEM;
265
266 of_device_get_modalias(dev, str, size);
267 str[size] = '\0';
268 ret = request_module(str);
269 kfree(str);
270
271 return ret;
272}
273EXPORT_SYMBOL_GPL(of_device_request_module);
274
Grant Likelydd27dcd2010-06-08 07:48:12 -0600275/**
Rob Herring0634c292017-03-22 09:16:27 -0500276 * of_device_modalias - Fill buffer with newline terminated modalias string
277 */
278ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
279{
280 ssize_t sl = of_device_get_modalias(dev, str, len - 2);
281 if (sl < 0)
282 return sl;
Bjorn Andersson08ab58d2017-08-23 18:04:04 -0700283 if (sl > len - 2)
284 return -ENOMEM;
Rob Herring0634c292017-03-22 09:16:27 -0500285
286 str[sl++] = '\n';
287 str[sl] = 0;
288 return sl;
289}
290EXPORT_SYMBOL_GPL(of_device_modalias);
291
292/**
Grant Likelydd27dcd2010-06-08 07:48:12 -0600293 * of_device_uevent - Display OF related uevent information
294 */
Grant Likely07d57a32012-02-01 11:22:22 -0700295void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Grant Likelydd27dcd2010-06-08 07:48:12 -0600296{
Rob Herringe8b1dee22018-08-29 08:36:12 -0500297 const char *compat, *type;
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800298 struct alias_prop *app;
Rob Herringbc575062017-07-21 15:45:32 -0500299 struct property *p;
300 int seen = 0;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600301
302 if ((!dev) || (!dev->of_node))
Grant Likely07d57a32012-02-01 11:22:22 -0700303 return;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600304
Rob Herringa613b262018-08-27 20:00:19 -0500305 add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
Rob Herring0d638a02017-06-01 15:50:55 -0500306 add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
Rob Herringe8b1dee22018-08-29 08:36:12 -0500307 type = of_node_get_device_type(dev->of_node);
308 if (type)
309 add_uevent_var(env, "OF_TYPE=%s", type);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600310
311 /* Since the compatible field can contain pretty much anything
312 * it's not really legal to split it out with commas. We split it
313 * up using a number of environment variables instead. */
Rob Herringbc575062017-07-21 15:45:32 -0500314 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Grant Likely07d57a32012-02-01 11:22:22 -0700315 add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600316 seen++;
317 }
Grant Likely07d57a32012-02-01 11:22:22 -0700318 add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800319
320 seen = 0;
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300321 mutex_lock(&of_mutex);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800322 list_for_each_entry(app, &aliases_lookup, link) {
323 if (dev->of_node == app->np) {
324 add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
325 app->alias);
326 seen++;
327 }
328 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300329 mutex_unlock(&of_mutex);
Grant Likely07d57a32012-02-01 11:22:22 -0700330}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600331
Grant Likely07d57a32012-02-01 11:22:22 -0700332int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
333{
334 int sl;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600335
Grant Likely07d57a32012-02-01 11:22:22 -0700336 if ((!dev) || (!dev->of_node))
337 return -ENODEV;
338
339 /* Devicetree modalias is tricky, we add it in 2 steps */
Grant Likelydd27dcd2010-06-08 07:48:12 -0600340 if (add_uevent_var(env, "MODALIAS="))
341 return -ENOMEM;
342
Grant Likely34a1c1e2010-06-08 07:48:13 -0600343 sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
Grant Likelydd27dcd2010-06-08 07:48:12 -0600344 sizeof(env->buf) - env->buflen);
345 if (sl >= (sizeof(env->buf) - env->buflen))
346 return -ENOMEM;
347 env->buflen += sl;
348
349 return 0;
350}
Stephen Boyd7a3b7cd2016-12-28 14:56:48 -0800351EXPORT_SYMBOL_GPL(of_device_uevent_modalias);