xref: /openbmc/linux/drivers/of/device.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1af6074fcSRob Herring // SPDX-License-Identifier: GPL-2.0
2f85ff305SStephen Rothwell #include <linux/kernel.h>
3f85ff305SStephen Rothwell #include <linux/of.h>
4f898f8dbSStephen Rothwell #include <linux/of_device.h>
51f5c69aaSMurali Karicheri #include <linux/of_address.h>
61f5c69aaSMurali Karicheri #include <linux/of_iommu.h>
7ce5cb67cSWill Deacon #include <linux/of_reserved_mem.h>
8e0d07278SJim Quinlan #include <linux/dma-direct.h> /* for bus_dma_region */
90a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
10f85ff305SStephen Rothwell #include <linux/init.h>
11f85ff305SStephen Rothwell #include <linux/mod_devicetable.h>
12f85ff305SStephen Rothwell #include <linux/slab.h>
1372328883SRobin Murphy #include <linux/platform_device.h>
14f85ff305SStephen Rothwell 
15f85ff305SStephen Rothwell #include <asm/errno.h>
16ced4eec9SStepan Moskovchenko #include "of_private.h"
17f85ff305SStephen Rothwell 
18f85ff305SStephen Rothwell /**
1944504b2bSGrant Likely  * of_match_device - Tell if a struct device matches an of_device_id list
208e94fd36SJojo Zeng  * @matches: array of of device match structures to search in
21f85ff305SStephen Rothwell  * @dev: the of device structure to match against
22f85ff305SStephen Rothwell  *
232dc11581SGrant Likely  * Used by a driver to check whether an platform_device present in the
24f85ff305SStephen Rothwell  * system is in its list of supported devices.
25f85ff305SStephen Rothwell  */
of_match_device(const struct of_device_id * matches,const struct device * dev)26f85ff305SStephen Rothwell const struct of_device_id *of_match_device(const struct of_device_id *matches,
2744504b2bSGrant Likely 					   const struct device *dev)
28f85ff305SStephen Rothwell {
299b22c17aSRob Herring 	if (!matches || !dev->of_node || dev->of_node_reused)
30f85ff305SStephen Rothwell 		return NULL;
3144504b2bSGrant Likely 	return of_match_node(matches, dev->of_node);
32f85ff305SStephen Rothwell }
33f85ff305SStephen Rothwell EXPORT_SYMBOL(of_match_device);
34f85ff305SStephen Rothwell 
35f3cfd136SWill Deacon static void
of_dma_set_restricted_buffer(struct device * dev,struct device_node * np)36ce5cb67cSWill Deacon of_dma_set_restricted_buffer(struct device *dev, struct device_node *np)
37ce5cb67cSWill Deacon {
38ce5cb67cSWill Deacon 	struct device_node *node, *of_node = dev->of_node;
39ce5cb67cSWill Deacon 	int count, i;
40ce5cb67cSWill Deacon 
41f3cfd136SWill Deacon 	if (!IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL))
42f3cfd136SWill Deacon 		return;
43f3cfd136SWill Deacon 
44ce5cb67cSWill Deacon 	count = of_property_count_elems_of_size(of_node, "memory-region",
45ce5cb67cSWill Deacon 						sizeof(u32));
46ce5cb67cSWill Deacon 	/*
47ce5cb67cSWill Deacon 	 * If dev->of_node doesn't exist or doesn't contain memory-region, try
48ce5cb67cSWill Deacon 	 * the OF node having DMA configuration.
49ce5cb67cSWill Deacon 	 */
50ce5cb67cSWill Deacon 	if (count <= 0) {
51ce5cb67cSWill Deacon 		of_node = np;
52ce5cb67cSWill Deacon 		count = of_property_count_elems_of_size(
53ce5cb67cSWill Deacon 			of_node, "memory-region", sizeof(u32));
54ce5cb67cSWill Deacon 	}
55ce5cb67cSWill Deacon 
56ce5cb67cSWill Deacon 	for (i = 0; i < count; i++) {
57ce5cb67cSWill Deacon 		node = of_parse_phandle(of_node, "memory-region", i);
58ce5cb67cSWill Deacon 		/*
59ce5cb67cSWill Deacon 		 * There might be multiple memory regions, but only one
60ce5cb67cSWill Deacon 		 * restricted-dma-pool region is allowed.
61ce5cb67cSWill Deacon 		 */
62ce5cb67cSWill Deacon 		if (of_device_is_compatible(node, "restricted-dma-pool") &&
63d17e37c4SLiang He 		    of_device_is_available(node)) {
64d17e37c4SLiang He 			of_node_put(node);
65f3cfd136SWill Deacon 			break;
66ce5cb67cSWill Deacon 		}
67d17e37c4SLiang He 		of_node_put(node);
68d17e37c4SLiang He 	}
69ce5cb67cSWill Deacon 
7031c8025fSDavid Brazdil 	/*
7131c8025fSDavid Brazdil 	 * Attempt to initialize a restricted-dma-pool region if one was found.
7231c8025fSDavid Brazdil 	 * Note that count can hold a negative error code.
7331c8025fSDavid Brazdil 	 */
7431c8025fSDavid Brazdil 	if (i < count && of_reserved_mem_device_init_by_idx(dev, of_node, i))
75f3cfd136SWill Deacon 		dev_warn(dev, "failed to initialise \"restricted-dma-pool\" memory node\n");
76ce5cb67cSWill Deacon }
77ce5cb67cSWill Deacon 
781f5c69aaSMurali Karicheri /**
79cb61e9dbSLee Jones  * of_dma_configure_id - Setup DMA configuration
801f5c69aaSMurali Karicheri  * @dev:	Device to apply DMA configuration
811f5c69aaSMurali Karicheri  * @np:		Pointer to OF node having DMA configuration
823d6ce86eSChristoph Hellwig  * @force_dma:  Whether device is to be set up by of_dma_configure() even if
833d6ce86eSChristoph Hellwig  *		DMA capability is not explicitly described by firmware.
84a081bd4aSLorenzo Pieralisi  * @id:		Optional const pointer value input id
851f5c69aaSMurali Karicheri  *
861f5c69aaSMurali Karicheri  * Try to get devices's DMA configuration from DT and update it
871f5c69aaSMurali Karicheri  * accordingly.
881f5c69aaSMurali Karicheri  *
891f5c69aaSMurali Karicheri  * If platform code needs to use its own special DMA configuration, it
901f5c69aaSMurali Karicheri  * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
911f5c69aaSMurali Karicheri  * to fix up DMA configuration.
921f5c69aaSMurali Karicheri  */
of_dma_configure_id(struct device * dev,struct device_node * np,bool force_dma,const u32 * id)93a081bd4aSLorenzo Pieralisi int of_dma_configure_id(struct device *dev, struct device_node *np,
94a081bd4aSLorenzo Pieralisi 			bool force_dma, const u32 *id)
951f5c69aaSMurali Karicheri {
9653c92d79SRobin Murphy 	const struct iommu_ops *iommu;
97e0d07278SJim Quinlan 	const struct bus_dma_region *map = NULL;
98f1ad5338SRobin Murphy 	struct device_node *bus_np;
9948ab6d5dSGeert Uytterhoeven 	u64 dma_start = 0;
100e0d07278SJim Quinlan 	u64 mask, end, size = 0;
101e0d07278SJim Quinlan 	bool coherent;
102e0d07278SJim Quinlan 	int ret;
1031f5c69aaSMurali Karicheri 
104f1ad5338SRobin Murphy 	if (np == dev->of_node)
105f1ad5338SRobin Murphy 		bus_np = __of_get_dma_parent(np);
106f1ad5338SRobin Murphy 	else
107f1ad5338SRobin Murphy 		bus_np = of_node_get(np);
108f1ad5338SRobin Murphy 
109f1ad5338SRobin Murphy 	ret = of_dma_get_range(bus_np, &map);
110f1ad5338SRobin Murphy 	of_node_put(bus_np);
1111f5c69aaSMurali Karicheri 	if (ret < 0) {
11272328883SRobin Murphy 		/*
11372328883SRobin Murphy 		 * For legacy reasons, we have to assume some devices need
11472328883SRobin Murphy 		 * DMA configuration regardless of whether "dma-ranges" is
11572328883SRobin Murphy 		 * correctly specified or not.
11672328883SRobin Murphy 		 */
1173d6ce86eSChristoph Hellwig 		if (!force_dma)
11872328883SRobin Murphy 			return ret == -ENODEV ? 0 : ret;
1191f5c69aaSMurali Karicheri 	} else {
120e0d07278SJim Quinlan 		const struct bus_dma_region *r = map;
12148ab6d5dSGeert Uytterhoeven 		u64 dma_end = 0;
122e0d07278SJim Quinlan 
123e0d07278SJim Quinlan 		/* Determine the overall bounds of all DMA regions */
124495023e4SGeert Uytterhoeven 		for (dma_start = ~0; r->size; r++) {
125e0d07278SJim Quinlan 			/* Take lower and upper limits */
126e0d07278SJim Quinlan 			if (r->dma_start < dma_start)
127e0d07278SJim Quinlan 				dma_start = r->dma_start;
128e0d07278SJim Quinlan 			if (r->dma_start + r->size > dma_end)
129e0d07278SJim Quinlan 				dma_end = r->dma_start + r->size;
130e0d07278SJim Quinlan 		}
131e0d07278SJim Quinlan 		size = dma_end - dma_start;
1320c79c81cSMurali Karicheri 
1330c79c81cSMurali Karicheri 		/*
1340c79c81cSMurali Karicheri 		 * Add a work around to treat the size as mask + 1 in case
1350c79c81cSMurali Karicheri 		 * it is defined in DT as a mask.
1360c79c81cSMurali Karicheri 		 */
1370c79c81cSMurali Karicheri 		if (size & 1) {
138e0d07278SJim Quinlan 			dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
1390c79c81cSMurali Karicheri 				 size);
1400c79c81cSMurali Karicheri 			size = size + 1;
1410c79c81cSMurali Karicheri 		}
1420c79c81cSMurali Karicheri 
1430c79c81cSMurali Karicheri 		if (!size) {
1440c79c81cSMurali Karicheri 			dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
145e0d07278SJim Quinlan 			kfree(map);
1467b07cbefSLaurent Pinchart 			return -EINVAL;
1470c79c81cSMurali Karicheri 		}
1481f5c69aaSMurali Karicheri 	}
1491f5c69aaSMurali Karicheri 
15072328883SRobin Murphy 	/*
1514d8bde88SRobin Murphy 	 * If @dev is expected to be DMA-capable then the bus code that created
1524d8bde88SRobin Murphy 	 * it should have initialised its dma_mask pointer by this point. For
1534d8bde88SRobin Murphy 	 * now, we'll continue the legacy behaviour of coercing it to the
1544d8bde88SRobin Murphy 	 * coherent mask if not, but we'll no longer do so quietly.
15572328883SRobin Murphy 	 */
1564d8bde88SRobin Murphy 	if (!dev->dma_mask) {
1574d8bde88SRobin Murphy 		dev_warn(dev, "DMA mask not set\n");
15872328883SRobin Murphy 		dev->dma_mask = &dev->coherent_dma_mask;
1594d8bde88SRobin Murphy 	}
16072328883SRobin Murphy 
1614d8bde88SRobin Murphy 	if (!size && dev->coherent_dma_mask)
16272328883SRobin Murphy 		size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1634d8bde88SRobin Murphy 	else if (!size)
1644d8bde88SRobin Murphy 		size = 1ULL << 32;
16572328883SRobin Murphy 
1669a6d7298SMurali Karicheri 	/*
1679a6d7298SMurali Karicheri 	 * Limit coherent and dma mask based on size and default mask
1689a6d7298SMurali Karicheri 	 * set by the driver.
1699a6d7298SMurali Karicheri 	 */
170e0d07278SJim Quinlan 	end = dma_start + size - 1;
171a7ba70f1SNicolas Saenz Julienne 	mask = DMA_BIT_MASK(ilog2(end) + 1);
172ee7b1f31SRobin Murphy 	dev->coherent_dma_mask &= mask;
173ee7b1f31SRobin Murphy 	*dev->dma_mask &= mask;
17489c7cb16SYong Wu 	/* ...but only set bus limit and range map if we found valid dma-ranges earlier */
17589c7cb16SYong Wu 	if (!ret) {
176a7ba70f1SNicolas Saenz Julienne 		dev->bus_dma_limit = end;
17789c7cb16SYong Wu 		dev->dma_range_map = map;
17889c7cb16SYong Wu 	}
1799a6d7298SMurali Karicheri 
1801f5c69aaSMurali Karicheri 	coherent = of_dma_is_coherent(np);
1811f5c69aaSMurali Karicheri 	dev_dbg(dev, "device is%sdma coherent\n",
1821f5c69aaSMurali Karicheri 		coherent ? " " : " not ");
1831f5c69aaSMurali Karicheri 
184a081bd4aSLorenzo Pieralisi 	iommu = of_iommu_configure(dev, np, id);
185e0d07278SJim Quinlan 	if (PTR_ERR(iommu) == -EPROBE_DEFER) {
18689c7cb16SYong Wu 		/* Don't touch range map if it wasn't set from a valid dma-ranges */
18789c7cb16SYong Wu 		if (!ret)
18889c7cb16SYong Wu 			dev->dma_range_map = NULL;
189e0d07278SJim Quinlan 		kfree(map);
190a37b19a3SSricharan R 		return -EPROBE_DEFER;
191e0d07278SJim Quinlan 	}
1927b07cbefSLaurent Pinchart 
1931f5c69aaSMurali Karicheri 	dev_dbg(dev, "device is%sbehind an iommu\n",
1941f5c69aaSMurali Karicheri 		iommu ? " " : " not ");
1951f5c69aaSMurali Karicheri 
196e0d07278SJim Quinlan 	arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
1977b07cbefSLaurent Pinchart 
198fec9b625SClaire Chang 	if (!iommu)
199f3cfd136SWill Deacon 		of_dma_set_restricted_buffer(dev, np);
200fec9b625SClaire Chang 
2017b07cbefSLaurent Pinchart 	return 0;
2021f5c69aaSMurali Karicheri }
203a081bd4aSLorenzo Pieralisi EXPORT_SYMBOL_GPL(of_dma_configure_id);
2041f5c69aaSMurali Karicheri 
of_device_get_match_data(const struct device * dev)2053386e0faSJoachim Eastwood const void *of_device_get_match_data(const struct device *dev)
2063386e0faSJoachim Eastwood {
2073386e0faSJoachim Eastwood 	const struct of_device_id *match;
2083386e0faSJoachim Eastwood 
2093386e0faSJoachim Eastwood 	match = of_match_device(dev->driver->of_match_table, dev);
2103386e0faSJoachim Eastwood 	if (!match)
2113386e0faSJoachim Eastwood 		return NULL;
2123386e0faSJoachim Eastwood 
2133386e0faSJoachim Eastwood 	return match->data;
2143386e0faSJoachim Eastwood }
2153386e0faSJoachim Eastwood EXPORT_SYMBOL(of_device_get_match_data);
2163386e0faSJoachim Eastwood 
217dd27dcdaSGrant Likely /**
2180634c295SRob Herring  * of_device_modalias - Fill buffer with newline terminated modalias string
219cb61e9dbSLee Jones  * @dev:	Calling device
220cb61e9dbSLee Jones  * @str:	Modalias string
221cb61e9dbSLee Jones  * @len:	Size of @str
2220634c295SRob Herring  */
of_device_modalias(struct device * dev,char * str,ssize_t len)2230634c295SRob Herring ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
2240634c295SRob Herring {
2255c3d15e1SMiquel Raynal 	ssize_t sl;
2265c3d15e1SMiquel Raynal 
2275c3d15e1SMiquel Raynal 	if (!dev || !dev->of_node || dev->of_node_reused)
2285c3d15e1SMiquel Raynal 		return -ENODEV;
2295c3d15e1SMiquel Raynal 
2305c3d15e1SMiquel Raynal 	sl = of_modalias(dev->of_node, str, len - 2);
2310634c295SRob Herring 	if (sl < 0)
2320634c295SRob Herring 		return sl;
23308ab58d9SBjorn Andersson 	if (sl > len - 2)
23408ab58d9SBjorn Andersson 		return -ENOMEM;
2350634c295SRob Herring 
2360634c295SRob Herring 	str[sl++] = '\n';
2370634c295SRob Herring 	str[sl] = 0;
2380634c295SRob Herring 	return sl;
2390634c295SRob Herring }
2400634c295SRob Herring EXPORT_SYMBOL_GPL(of_device_modalias);
2410634c295SRob Herring 
2420634c295SRob Herring /**
243dd27dcdaSGrant Likely  * of_device_uevent - Display OF related uevent information
2449f041c5dSGreg Kroah-Hartman  * @dev:	Device to display the uevent information for
2459f041c5dSGreg Kroah-Hartman  * @env:	Kernel object's userspace event reference to fill up
246dd27dcdaSGrant Likely  */
of_device_uevent(const struct device * dev,struct kobj_uevent_env * env)2479f041c5dSGreg Kroah-Hartman void of_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
248dd27dcdaSGrant Likely {
249e8b1dee2SRob Herring 	const char *compat, *type;
250ced4eec9SStepan Moskovchenko 	struct alias_prop *app;
251bc575064SRob Herring 	struct property *p;
252bc575064SRob Herring 	int seen = 0;
253dd27dcdaSGrant Likely 
254dd27dcdaSGrant Likely 	if ((!dev) || (!dev->of_node))
25507d57a32SGrant Likely 		return;
256dd27dcdaSGrant Likely 
257a613b26aSRob Herring 	add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
2580d638a07SRob Herring 	add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
259e8b1dee2SRob Herring 	type = of_node_get_device_type(dev->of_node);
260e8b1dee2SRob Herring 	if (type)
261e8b1dee2SRob Herring 		add_uevent_var(env, "OF_TYPE=%s", type);
262dd27dcdaSGrant Likely 
263dd27dcdaSGrant Likely 	/* Since the compatible field can contain pretty much anything
264dd27dcdaSGrant Likely 	 * it's not really legal to split it out with commas. We split it
265dd27dcdaSGrant Likely 	 * up using a number of environment variables instead. */
266bc575064SRob Herring 	of_property_for_each_string(dev->of_node, "compatible", p, compat) {
26707d57a32SGrant Likely 		add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
268dd27dcdaSGrant Likely 		seen++;
269dd27dcdaSGrant Likely 	}
27007d57a32SGrant Likely 	add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
271ced4eec9SStepan Moskovchenko 
272ced4eec9SStepan Moskovchenko 	seen = 0;
273c05aba2bSPantelis Antoniou 	mutex_lock(&of_mutex);
274ced4eec9SStepan Moskovchenko 	list_for_each_entry(app, &aliases_lookup, link) {
275ced4eec9SStepan Moskovchenko 		if (dev->of_node == app->np) {
276ced4eec9SStepan Moskovchenko 			add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
277ced4eec9SStepan Moskovchenko 				       app->alias);
278ced4eec9SStepan Moskovchenko 			seen++;
279ced4eec9SStepan Moskovchenko 		}
280ced4eec9SStepan Moskovchenko 	}
281c05aba2bSPantelis Antoniou 	mutex_unlock(&of_mutex);
28207d57a32SGrant Likely }
283*97c23217SMiquel Raynal EXPORT_SYMBOL_GPL(of_device_uevent);
284dd27dcdaSGrant Likely 
of_device_uevent_modalias(const struct device * dev,struct kobj_uevent_env * env)285a77ad4bfSGreg Kroah-Hartman int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *env)
28607d57a32SGrant Likely {
28707d57a32SGrant Likely 	int sl;
288dd27dcdaSGrant Likely 
289553bd297SAlexander Stein 	if ((!dev) || (!dev->of_node) || dev->of_node_reused)
29007d57a32SGrant Likely 		return -ENODEV;
29107d57a32SGrant Likely 
29207d57a32SGrant Likely 	/* Devicetree modalias is tricky, we add it in 2 steps */
293dd27dcdaSGrant Likely 	if (add_uevent_var(env, "MODALIAS="))
294dd27dcdaSGrant Likely 		return -ENOMEM;
295dd27dcdaSGrant Likely 
2965c3d15e1SMiquel Raynal 	sl = of_modalias(dev->of_node, &env->buf[env->buflen-1],
297dd27dcdaSGrant Likely 			 sizeof(env->buf) - env->buflen);
2982295bed9SAlexander Stein 	if (sl < 0)
2992295bed9SAlexander Stein 		return sl;
300dd27dcdaSGrant Likely 	if (sl >= (sizeof(env->buf) - env->buflen))
301dd27dcdaSGrant Likely 		return -ENOMEM;
302dd27dcdaSGrant Likely 	env->buflen += sl;
303dd27dcdaSGrant Likely 
304dd27dcdaSGrant Likely 	return 0;
305dd27dcdaSGrant Likely }
3067a3b7cd3SStephen Boyd EXPORT_SYMBOL_GPL(of_device_uevent_modalias);
307