xref: /openbmc/linux/drivers/interconnect/imx/imx.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1f0d80485SLeonard Crestez // SPDX-License-Identifier: GPL-2.0
2f0d80485SLeonard Crestez /*
3f0d80485SLeonard Crestez  * Interconnect framework driver for i.MX SoC
4f0d80485SLeonard Crestez  *
5f0d80485SLeonard Crestez  * Copyright (c) 2019, BayLibre
6f0d80485SLeonard Crestez  * Copyright (c) 2019-2020, NXP
7f0d80485SLeonard Crestez  * Author: Alexandre Bailon <abailon@baylibre.com>
8f0d80485SLeonard Crestez  * Author: Leonard Crestez <leonard.crestez@nxp.com>
9f0d80485SLeonard Crestez  */
10f0d80485SLeonard Crestez 
11f0d80485SLeonard Crestez #include <linux/device.h>
12f0d80485SLeonard Crestez #include <linux/interconnect-provider.h>
137980d85aSPeng Fan #include <linux/io.h>
14f0d80485SLeonard Crestez #include <linux/module.h>
15f0d80485SLeonard Crestez #include <linux/of.h>
16f0d80485SLeonard Crestez #include <linux/of_platform.h>
17f0d80485SLeonard Crestez #include <linux/platform_device.h>
18f0d80485SLeonard Crestez #include <linux/pm_qos.h>
19f0d80485SLeonard Crestez 
20f0d80485SLeonard Crestez #include "imx.h"
21f0d80485SLeonard Crestez 
22f0d80485SLeonard Crestez /* private icc_node data */
23f0d80485SLeonard Crestez struct imx_icc_node {
24f0d80485SLeonard Crestez 	const struct imx_icc_node_desc *desc;
257980d85aSPeng Fan 	const struct imx_icc_noc_setting *setting;
26f0d80485SLeonard Crestez 	struct device *qos_dev;
27f0d80485SLeonard Crestez 	struct dev_pm_qos_request qos_req;
287980d85aSPeng Fan 	struct imx_icc_provider *imx_provider;
29f0d80485SLeonard Crestez };
30f0d80485SLeonard Crestez 
imx_icc_get_bw(struct icc_node * node,u32 * avg,u32 * peak)319d6c7ee7SAbel Vesa static int imx_icc_get_bw(struct icc_node *node, u32 *avg, u32 *peak)
329d6c7ee7SAbel Vesa {
339d6c7ee7SAbel Vesa 	*avg = 0;
349d6c7ee7SAbel Vesa 	*peak = 0;
359d6c7ee7SAbel Vesa 
369d6c7ee7SAbel Vesa 	return 0;
379d6c7ee7SAbel Vesa }
389d6c7ee7SAbel Vesa 
imx_icc_node_set(struct icc_node * node)39f0d80485SLeonard Crestez static int imx_icc_node_set(struct icc_node *node)
40f0d80485SLeonard Crestez {
41f0d80485SLeonard Crestez 	struct device *dev = node->provider->dev;
42f0d80485SLeonard Crestez 	struct imx_icc_node *node_data = node->data;
437980d85aSPeng Fan 	void __iomem *base;
447980d85aSPeng Fan 	u32 prio;
45f0d80485SLeonard Crestez 	u64 freq;
46f0d80485SLeonard Crestez 
477980d85aSPeng Fan 	if (node_data->setting && node->peak_bw) {
487980d85aSPeng Fan 		base = node_data->setting->reg + node_data->imx_provider->noc_base;
497980d85aSPeng Fan 		if (node_data->setting->mode == IMX_NOC_MODE_FIXED) {
507980d85aSPeng Fan 			prio = node_data->setting->prio_level;
517980d85aSPeng Fan 			prio = PRIORITY_COMP_MARK | (prio << 8) | prio;
527980d85aSPeng Fan 			writel(prio, base + IMX_NOC_PRIO_REG);
537980d85aSPeng Fan 			writel(node_data->setting->mode, base + IMX_NOC_MODE_REG);
547980d85aSPeng Fan 			writel(node_data->setting->ext_control, base + IMX_NOC_EXT_CTL_REG);
557980d85aSPeng Fan 			dev_dbg(dev, "%s: mode: 0x%x, prio: 0x%x, ext_control: 0x%x\n",
567980d85aSPeng Fan 				node_data->desc->name, node_data->setting->mode, prio,
577980d85aSPeng Fan 				node_data->setting->ext_control);
587980d85aSPeng Fan 		} else if (node_data->setting->mode == IMX_NOC_MODE_UNCONFIGURED) {
597980d85aSPeng Fan 			dev_dbg(dev, "%s: mode not unconfigured\n", node_data->desc->name);
607980d85aSPeng Fan 		} else {
617980d85aSPeng Fan 			dev_info(dev, "%s: mode: %d not supported\n",
627980d85aSPeng Fan 				 node_data->desc->name, node_data->setting->mode);
637980d85aSPeng Fan 			return -EOPNOTSUPP;
647980d85aSPeng Fan 		}
657980d85aSPeng Fan 	}
667980d85aSPeng Fan 
67f0d80485SLeonard Crestez 	if (!node_data->qos_dev)
68f0d80485SLeonard Crestez 		return 0;
69f0d80485SLeonard Crestez 
70f0d80485SLeonard Crestez 	freq = (node->avg_bw + node->peak_bw) * node_data->desc->adj->bw_mul;
71f0d80485SLeonard Crestez 	do_div(freq, node_data->desc->adj->bw_div);
72f0d80485SLeonard Crestez 	dev_dbg(dev, "node %s device %s avg_bw %ukBps peak_bw %ukBps min_freq %llukHz\n",
73f0d80485SLeonard Crestez 		node->name, dev_name(node_data->qos_dev),
74f0d80485SLeonard Crestez 		node->avg_bw, node->peak_bw, freq);
75f0d80485SLeonard Crestez 
76f0d80485SLeonard Crestez 	if (freq > S32_MAX) {
77f0d80485SLeonard Crestez 		dev_err(dev, "%s can't request more than S32_MAX freq\n",
78f0d80485SLeonard Crestez 				node->name);
79f0d80485SLeonard Crestez 		return -ERANGE;
80f0d80485SLeonard Crestez 	}
81f0d80485SLeonard Crestez 
82f0d80485SLeonard Crestez 	dev_pm_qos_update_request(&node_data->qos_req, freq);
83f0d80485SLeonard Crestez 
84f0d80485SLeonard Crestez 	return 0;
85f0d80485SLeonard Crestez }
86f0d80485SLeonard Crestez 
imx_icc_set(struct icc_node * src,struct icc_node * dst)87f0d80485SLeonard Crestez static int imx_icc_set(struct icc_node *src, struct icc_node *dst)
88f0d80485SLeonard Crestez {
896eeaf28cSPeng Fan 	int ret;
906eeaf28cSPeng Fan 
916eeaf28cSPeng Fan 	ret = imx_icc_node_set(src);
926eeaf28cSPeng Fan 	if (ret)
936eeaf28cSPeng Fan 		return ret;
946eeaf28cSPeng Fan 
95f0d80485SLeonard Crestez 	return imx_icc_node_set(dst);
96f0d80485SLeonard Crestez }
97f0d80485SLeonard Crestez 
98f0d80485SLeonard Crestez /* imx_icc_node_destroy() - Destroy an imx icc_node, including private data */
imx_icc_node_destroy(struct icc_node * node)99f0d80485SLeonard Crestez static void imx_icc_node_destroy(struct icc_node *node)
100f0d80485SLeonard Crestez {
101f0d80485SLeonard Crestez 	struct imx_icc_node *node_data = node->data;
102f0d80485SLeonard Crestez 	int ret;
103f0d80485SLeonard Crestez 
104f0d80485SLeonard Crestez 	if (dev_pm_qos_request_active(&node_data->qos_req)) {
105f0d80485SLeonard Crestez 		ret = dev_pm_qos_remove_request(&node_data->qos_req);
106f0d80485SLeonard Crestez 		if (ret)
107f0d80485SLeonard Crestez 			dev_warn(node->provider->dev,
108f0d80485SLeonard Crestez 				 "failed to remove qos request for %s\n",
109f0d80485SLeonard Crestez 				 dev_name(node_data->qos_dev));
110f0d80485SLeonard Crestez 	}
111f0d80485SLeonard Crestez 
112f0d80485SLeonard Crestez 	put_device(node_data->qos_dev);
113f0d80485SLeonard Crestez 	icc_node_del(node);
114f0d80485SLeonard Crestez 	icc_node_destroy(node->id);
115f0d80485SLeonard Crestez }
116f0d80485SLeonard Crestez 
imx_icc_node_init_qos(struct icc_provider * provider,struct icc_node * node)117f0d80485SLeonard Crestez static int imx_icc_node_init_qos(struct icc_provider *provider,
118f0d80485SLeonard Crestez 				 struct icc_node *node)
119f0d80485SLeonard Crestez {
120f0d80485SLeonard Crestez 	struct imx_icc_node *node_data = node->data;
121f0d80485SLeonard Crestez 	const struct imx_icc_node_adj_desc *adj = node_data->desc->adj;
122f0d80485SLeonard Crestez 	struct device *dev = provider->dev;
123f0d80485SLeonard Crestez 	struct device_node *dn = NULL;
124f0d80485SLeonard Crestez 	struct platform_device *pdev;
125f0d80485SLeonard Crestez 
126f0d80485SLeonard Crestez 	if (adj->main_noc) {
127f0d80485SLeonard Crestez 		node_data->qos_dev = dev;
128f0d80485SLeonard Crestez 		dev_dbg(dev, "icc node %s[%d] is main noc itself\n",
129f0d80485SLeonard Crestez 			node->name, node->id);
130f0d80485SLeonard Crestez 	} else {
131f0d80485SLeonard Crestez 		dn = of_parse_phandle(dev->of_node, adj->phandle_name, 0);
132360a1028SWei Yongjun 		if (!dn) {
133360a1028SWei Yongjun 			dev_warn(dev, "Failed to parse %s\n",
134360a1028SWei Yongjun 				 adj->phandle_name);
135360a1028SWei Yongjun 			return -ENODEV;
136f0d80485SLeonard Crestez 		}
137f0d80485SLeonard Crestez 		/* Allow scaling to be disabled on a per-node basis */
1386414b79dSChristophe JAILLET 		if (!of_device_is_available(dn)) {
139f0d80485SLeonard Crestez 			dev_warn(dev, "Missing property %s, skip scaling %s\n",
140f0d80485SLeonard Crestez 				 adj->phandle_name, node->name);
141c6174c0eSChristophe JAILLET 			of_node_put(dn);
142f0d80485SLeonard Crestez 			return 0;
143f0d80485SLeonard Crestez 		}
144f0d80485SLeonard Crestez 
145f0d80485SLeonard Crestez 		pdev = of_find_device_by_node(dn);
146f0d80485SLeonard Crestez 		of_node_put(dn);
147f0d80485SLeonard Crestez 		if (!pdev) {
148f0d80485SLeonard Crestez 			dev_warn(dev, "node %s[%d] missing device for %pOF\n",
149f0d80485SLeonard Crestez 				 node->name, node->id, dn);
150f0d80485SLeonard Crestez 			return -EPROBE_DEFER;
151f0d80485SLeonard Crestez 		}
152f0d80485SLeonard Crestez 		node_data->qos_dev = &pdev->dev;
153f0d80485SLeonard Crestez 		dev_dbg(dev, "node %s[%d] has device node %pOF\n",
154f0d80485SLeonard Crestez 			node->name, node->id, dn);
155f0d80485SLeonard Crestez 	}
156f0d80485SLeonard Crestez 
157f0d80485SLeonard Crestez 	return dev_pm_qos_add_request(node_data->qos_dev,
158f0d80485SLeonard Crestez 				      &node_data->qos_req,
159f0d80485SLeonard Crestez 				      DEV_PM_QOS_MIN_FREQUENCY, 0);
160f0d80485SLeonard Crestez }
161f0d80485SLeonard Crestez 
imx_icc_node_add(struct imx_icc_provider * imx_provider,const struct imx_icc_node_desc * node_desc,const struct imx_icc_noc_setting * setting)16212db59e8SPeng Fan static struct icc_node *imx_icc_node_add(struct imx_icc_provider *imx_provider,
1637980d85aSPeng Fan 					 const struct imx_icc_node_desc *node_desc,
1647980d85aSPeng Fan 					 const struct imx_icc_noc_setting *setting)
165f0d80485SLeonard Crestez {
16612db59e8SPeng Fan 	struct icc_provider *provider = &imx_provider->provider;
167f0d80485SLeonard Crestez 	struct device *dev = provider->dev;
168f0d80485SLeonard Crestez 	struct imx_icc_node *node_data;
169f0d80485SLeonard Crestez 	struct icc_node *node;
170f0d80485SLeonard Crestez 	int ret;
171f0d80485SLeonard Crestez 
172f0d80485SLeonard Crestez 	node = icc_node_create(node_desc->id);
173f0d80485SLeonard Crestez 	if (IS_ERR(node)) {
174f0d80485SLeonard Crestez 		dev_err(dev, "failed to create node %d\n", node_desc->id);
175f0d80485SLeonard Crestez 		return node;
176f0d80485SLeonard Crestez 	}
177f0d80485SLeonard Crestez 
178f0d80485SLeonard Crestez 	if (node->data) {
179f0d80485SLeonard Crestez 		dev_err(dev, "already created node %s id=%d\n",
180f0d80485SLeonard Crestez 			node_desc->name, node_desc->id);
181f0d80485SLeonard Crestez 		return ERR_PTR(-EEXIST);
182f0d80485SLeonard Crestez 	}
183f0d80485SLeonard Crestez 
184f0d80485SLeonard Crestez 	node_data = devm_kzalloc(dev, sizeof(*node_data), GFP_KERNEL);
185f0d80485SLeonard Crestez 	if (!node_data) {
186f0d80485SLeonard Crestez 		icc_node_destroy(node->id);
187f0d80485SLeonard Crestez 		return ERR_PTR(-ENOMEM);
188f0d80485SLeonard Crestez 	}
189f0d80485SLeonard Crestez 
190f0d80485SLeonard Crestez 	node->name = node_desc->name;
191f0d80485SLeonard Crestez 	node->data = node_data;
192f0d80485SLeonard Crestez 	node_data->desc = node_desc;
1937980d85aSPeng Fan 	node_data->setting = setting;
1947980d85aSPeng Fan 	node_data->imx_provider = imx_provider;
195f0d80485SLeonard Crestez 	icc_node_add(node, provider);
196f0d80485SLeonard Crestez 
197f0d80485SLeonard Crestez 	if (node_desc->adj) {
198f0d80485SLeonard Crestez 		ret = imx_icc_node_init_qos(provider, node);
199f0d80485SLeonard Crestez 		if (ret < 0) {
200f0d80485SLeonard Crestez 			imx_icc_node_destroy(node);
201f0d80485SLeonard Crestez 			return ERR_PTR(ret);
202f0d80485SLeonard Crestez 		}
203f0d80485SLeonard Crestez 	}
204f0d80485SLeonard Crestez 
205f0d80485SLeonard Crestez 	return node;
206f0d80485SLeonard Crestez }
207f0d80485SLeonard Crestez 
imx_icc_unregister_nodes(struct icc_provider * provider)208f0d80485SLeonard Crestez static void imx_icc_unregister_nodes(struct icc_provider *provider)
209f0d80485SLeonard Crestez {
210f0d80485SLeonard Crestez 	struct icc_node *node, *tmp;
211f0d80485SLeonard Crestez 
212f0d80485SLeonard Crestez 	list_for_each_entry_safe(node, tmp, &provider->nodes, node_list)
213f0d80485SLeonard Crestez 		imx_icc_node_destroy(node);
214f0d80485SLeonard Crestez }
215f0d80485SLeonard Crestez 
imx_icc_register_nodes(struct imx_icc_provider * imx_provider,const struct imx_icc_node_desc * descs,int count,const struct imx_icc_noc_setting * settings)21612db59e8SPeng Fan static int imx_icc_register_nodes(struct imx_icc_provider *imx_provider,
217f0d80485SLeonard Crestez 				  const struct imx_icc_node_desc *descs,
2187980d85aSPeng Fan 				  int count,
2197980d85aSPeng Fan 				  const struct imx_icc_noc_setting *settings)
220f0d80485SLeonard Crestez {
22112db59e8SPeng Fan 	struct icc_provider *provider = &imx_provider->provider;
222f0d80485SLeonard Crestez 	struct icc_onecell_data *provider_data = provider->data;
223f0d80485SLeonard Crestez 	int ret;
224f0d80485SLeonard Crestez 	int i;
225f0d80485SLeonard Crestez 
226f0d80485SLeonard Crestez 	for (i = 0; i < count; i++) {
227f0d80485SLeonard Crestez 		struct icc_node *node;
228f0d80485SLeonard Crestez 		const struct imx_icc_node_desc *node_desc = &descs[i];
229f0d80485SLeonard Crestez 		size_t j;
230f0d80485SLeonard Crestez 
2317980d85aSPeng Fan 		node = imx_icc_node_add(imx_provider, node_desc,
2327980d85aSPeng Fan 					settings ? &settings[node_desc->id] : NULL);
233f0d80485SLeonard Crestez 		if (IS_ERR(node)) {
234e0cbf2f0SKrzysztof Kozlowski 			ret = dev_err_probe(provider->dev, PTR_ERR(node),
235e0cbf2f0SKrzysztof Kozlowski 					    "failed to add %s\n", node_desc->name);
236f0d80485SLeonard Crestez 			goto err;
237f0d80485SLeonard Crestez 		}
238f0d80485SLeonard Crestez 		provider_data->nodes[node->id] = node;
239f0d80485SLeonard Crestez 
240f0d80485SLeonard Crestez 		for (j = 0; j < node_desc->num_links; j++) {
241f0d80485SLeonard Crestez 			ret = icc_link_create(node, node_desc->links[j]);
242f0d80485SLeonard Crestez 			if (ret) {
243f0d80485SLeonard Crestez 				dev_err(provider->dev, "failed to link node %d to %d: %d\n",
244f0d80485SLeonard Crestez 					node->id, node_desc->links[j], ret);
245f0d80485SLeonard Crestez 				goto err;
246f0d80485SLeonard Crestez 			}
247f0d80485SLeonard Crestez 		}
248f0d80485SLeonard Crestez 	}
249f0d80485SLeonard Crestez 
250f0d80485SLeonard Crestez 	return 0;
251f0d80485SLeonard Crestez 
252f0d80485SLeonard Crestez err:
253f0d80485SLeonard Crestez 	imx_icc_unregister_nodes(provider);
254f0d80485SLeonard Crestez 
255f0d80485SLeonard Crestez 	return ret;
256f0d80485SLeonard Crestez }
257f0d80485SLeonard Crestez 
get_max_node_id(struct imx_icc_node_desc * nodes,int nodes_count)258f0d80485SLeonard Crestez static int get_max_node_id(struct imx_icc_node_desc *nodes, int nodes_count)
259f0d80485SLeonard Crestez {
260f0d80485SLeonard Crestez 	int i, ret = 0;
261f0d80485SLeonard Crestez 
262f0d80485SLeonard Crestez 	for (i = 0; i < nodes_count; ++i)
263f0d80485SLeonard Crestez 		if (nodes[i].id > ret)
264f0d80485SLeonard Crestez 			ret = nodes[i].id;
265f0d80485SLeonard Crestez 
266f0d80485SLeonard Crestez 	return ret;
267f0d80485SLeonard Crestez }
268f0d80485SLeonard Crestez 
imx_icc_register(struct platform_device * pdev,struct imx_icc_node_desc * nodes,int nodes_count,struct imx_icc_noc_setting * settings)269f0d80485SLeonard Crestez int imx_icc_register(struct platform_device *pdev,
2707980d85aSPeng Fan 		     struct imx_icc_node_desc *nodes, int nodes_count,
2717980d85aSPeng Fan 		     struct imx_icc_noc_setting *settings)
272f0d80485SLeonard Crestez {
273f0d80485SLeonard Crestez 	struct device *dev = &pdev->dev;
274f0d80485SLeonard Crestez 	struct icc_onecell_data *data;
27512db59e8SPeng Fan 	struct imx_icc_provider *imx_provider;
276f0d80485SLeonard Crestez 	struct icc_provider *provider;
277bd734481SPeng Fan 	int num_nodes;
278f0d80485SLeonard Crestez 	int ret;
279f0d80485SLeonard Crestez 
280f0d80485SLeonard Crestez 	/* icc_onecell_data is indexed by node_id, unlike nodes param */
281bd734481SPeng Fan 	num_nodes = get_max_node_id(nodes, nodes_count) + 1;
282bd734481SPeng Fan 	data = devm_kzalloc(dev, struct_size(data, nodes, num_nodes),
283f0d80485SLeonard Crestez 			    GFP_KERNEL);
284f0d80485SLeonard Crestez 	if (!data)
285f0d80485SLeonard Crestez 		return -ENOMEM;
286bd734481SPeng Fan 	data->num_nodes = num_nodes;
287f0d80485SLeonard Crestez 
28812db59e8SPeng Fan 	imx_provider = devm_kzalloc(dev, sizeof(*imx_provider), GFP_KERNEL);
28912db59e8SPeng Fan 	if (!imx_provider)
290f0d80485SLeonard Crestez 		return -ENOMEM;
29112db59e8SPeng Fan 	provider = &imx_provider->provider;
292f0d80485SLeonard Crestez 	provider->set = imx_icc_set;
2939d6c7ee7SAbel Vesa 	provider->get_bw = imx_icc_get_bw;
294f0d80485SLeonard Crestez 	provider->aggregate = icc_std_aggregate;
295f0d80485SLeonard Crestez 	provider->xlate = of_icc_xlate_onecell;
296f0d80485SLeonard Crestez 	provider->data = data;
297f0d80485SLeonard Crestez 	provider->dev = dev->parent;
298*9fbd3552SJohan Hovold 
299*9fbd3552SJohan Hovold 	icc_provider_init(provider);
300*9fbd3552SJohan Hovold 
30112db59e8SPeng Fan 	platform_set_drvdata(pdev, imx_provider);
302f0d80485SLeonard Crestez 
3037980d85aSPeng Fan 	if (settings) {
3047980d85aSPeng Fan 		imx_provider->noc_base = devm_of_iomap(dev, provider->dev->of_node, 0, NULL);
3057980d85aSPeng Fan 		if (IS_ERR(imx_provider->noc_base)) {
3067980d85aSPeng Fan 			ret = PTR_ERR(imx_provider->noc_base);
3077980d85aSPeng Fan 			dev_err(dev, "Error mapping NoC: %d\n", ret);
3087980d85aSPeng Fan 			return ret;
3097980d85aSPeng Fan 		}
3107980d85aSPeng Fan 	}
3117980d85aSPeng Fan 
3127980d85aSPeng Fan 	ret = imx_icc_register_nodes(imx_provider, nodes, nodes_count, settings);
313f0d80485SLeonard Crestez 	if (ret)
314*9fbd3552SJohan Hovold 		return ret;
315*9fbd3552SJohan Hovold 
316*9fbd3552SJohan Hovold 	ret = icc_provider_register(provider);
317*9fbd3552SJohan Hovold 	if (ret)
318*9fbd3552SJohan Hovold 		goto err_unregister_nodes;
319f0d80485SLeonard Crestez 
320f0d80485SLeonard Crestez 	return 0;
321f0d80485SLeonard Crestez 
322*9fbd3552SJohan Hovold err_unregister_nodes:
323*9fbd3552SJohan Hovold 	imx_icc_unregister_nodes(&imx_provider->provider);
324f0d80485SLeonard Crestez 	return ret;
325f0d80485SLeonard Crestez }
326f0d80485SLeonard Crestez EXPORT_SYMBOL_GPL(imx_icc_register);
327f0d80485SLeonard Crestez 
imx_icc_unregister(struct platform_device * pdev)328f62e3f59SUwe Kleine-König void imx_icc_unregister(struct platform_device *pdev)
329f0d80485SLeonard Crestez {
33012db59e8SPeng Fan 	struct imx_icc_provider *imx_provider = platform_get_drvdata(pdev);
331f0d80485SLeonard Crestez 
332*9fbd3552SJohan Hovold 	icc_provider_deregister(&imx_provider->provider);
33312db59e8SPeng Fan 	imx_icc_unregister_nodes(&imx_provider->provider);
334f0d80485SLeonard Crestez }
335f0d80485SLeonard Crestez EXPORT_SYMBOL_GPL(imx_icc_unregister);
336f0d80485SLeonard Crestez 
337f0d80485SLeonard Crestez MODULE_LICENSE("GPL v2");
338