blob: 2bf5af6823d8cf2a576c3abf75abff36e8e07833 [file] [log] [blame]
Alex Elderba764c42020-03-05 22:28:19 -06001// SPDX-License-Identifier: GPL-2.0
2
3/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2020 Linaro Ltd.
5 */
6
Alex Elder0305b702020-09-17 12:39:20 -05007#include <linux/refcount.h>
Alex Elderba764c42020-03-05 22:28:19 -06008#include <linux/mutex.h>
9#include <linux/clk.h>
10#include <linux/device.h>
11#include <linux/interconnect.h>
12
13#include "ipa.h"
14#include "ipa_clock.h"
15#include "ipa_modem.h"
Alex Elderdfccb8b2020-11-19 16:40:39 -060016#include "ipa_data.h"
Alex Elderba764c42020-03-05 22:28:19 -060017
18/**
19 * DOC: IPA Clocking
20 *
21 * The "IPA Clock" manages both the IPA core clock and the interconnects
22 * (buses) the IPA depends on as a single logical entity. A reference count
23 * is incremented by "get" operations and decremented by "put" operations.
24 * Transitions of that count from 0 to 1 result in the clock and interconnects
25 * being enabled, and transitions of the count from 1 to 0 cause them to be
26 * disabled. We currently operate the core clock at a fixed clock rate, and
27 * all buses at a fixed average and peak bandwidth. As more advanced IPA
28 * features are enabled, we can make better use of clock and bus scaling.
29 *
30 * An IPA clock reference must be held for any access to IPA hardware.
31 */
32
Alex Elderba764c42020-03-05 22:28:19 -060033/**
Alex Elder5b408102021-01-15 06:50:46 -060034 * struct ipa_interconnect - IPA interconnect information
35 * @path: Interconnect path
36 */
37struct ipa_interconnect {
38 struct icc_path *path;
39};
40
41/**
Alex Elderba764c42020-03-05 22:28:19 -060042 * struct ipa_clock - IPA clocking information
43 * @count: Clocking reference count
Alex Eldere3eea082020-07-13 07:24:18 -050044 * @mutex: Protects clock enable/disable
Alex Elderba764c42020-03-05 22:28:19 -060045 * @core: IPA core clock
Alex Elder5b408102021-01-15 06:50:46 -060046 * @interconnect: Interconnect array
Alex Elderdfccb8b2020-11-19 16:40:39 -060047 * @interconnect_data: Interconnect configuration data
Alex Elderba764c42020-03-05 22:28:19 -060048 */
49struct ipa_clock {
Alex Elder0305b702020-09-17 12:39:20 -050050 refcount_t count;
Alex Elderba764c42020-03-05 22:28:19 -060051 struct mutex mutex; /* protects clock enable/disable */
52 struct clk *core;
Alex Elder5b408102021-01-15 06:50:46 -060053 struct ipa_interconnect *interconnect[IPA_INTERCONNECT_COUNT];
Alex Elderdfccb8b2020-11-19 16:40:39 -060054 const struct ipa_interconnect_data *interconnect_data;
Alex Elderba764c42020-03-05 22:28:19 -060055};
56
57static struct icc_path *
58ipa_interconnect_init_one(struct device *dev, const char *name)
59{
60 struct icc_path *path;
61
62 path = of_icc_get(dev, name);
63 if (IS_ERR(path))
Wang Wenhu07153962020-05-24 23:29:51 -070064 dev_err(dev, "error %ld getting %s interconnect\n",
65 PTR_ERR(path), name);
Alex Elderba764c42020-03-05 22:28:19 -060066
67 return path;
68}
69
70/* Initialize interconnects required for IPA operation */
71static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev)
72{
73 struct icc_path *path;
74
75 path = ipa_interconnect_init_one(dev, "memory");
76 if (IS_ERR(path))
77 goto err_return;
Alex Elder5b408102021-01-15 06:50:46 -060078 clock->interconnect[IPA_INTERCONNECT_MEMORY]->path = path;
Alex Elderba764c42020-03-05 22:28:19 -060079
80 path = ipa_interconnect_init_one(dev, "imem");
81 if (IS_ERR(path))
82 goto err_memory_path_put;
Alex Elder5b408102021-01-15 06:50:46 -060083 clock->interconnect[IPA_INTERCONNECT_IMEM]->path = path;
Alex Elderba764c42020-03-05 22:28:19 -060084
85 path = ipa_interconnect_init_one(dev, "config");
86 if (IS_ERR(path))
87 goto err_imem_path_put;
Alex Elder5b408102021-01-15 06:50:46 -060088 clock->interconnect[IPA_INTERCONNECT_CONFIG]->path = path;
Alex Elderba764c42020-03-05 22:28:19 -060089
90 return 0;
91
92err_imem_path_put:
Alex Elder5b408102021-01-15 06:50:46 -060093 icc_put(clock->interconnect[IPA_INTERCONNECT_IMEM]->path);
Alex Elderba764c42020-03-05 22:28:19 -060094err_memory_path_put:
Alex Elder5b408102021-01-15 06:50:46 -060095 icc_put(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path);
Alex Elderba764c42020-03-05 22:28:19 -060096err_return:
97 return PTR_ERR(path);
98}
99
100/* Inverse of ipa_interconnect_init() */
101static void ipa_interconnect_exit(struct ipa_clock *clock)
102{
Alex Elder5b408102021-01-15 06:50:46 -0600103 icc_put(clock->interconnect[IPA_INTERCONNECT_CONFIG]->path);
104 icc_put(clock->interconnect[IPA_INTERCONNECT_IMEM]->path);
105 icc_put(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path);
Alex Elderba764c42020-03-05 22:28:19 -0600106}
107
108/* Currently we only use one bandwidth level, so just "enable" interconnects */
109static int ipa_interconnect_enable(struct ipa *ipa)
110{
Alex Elder91d02f92020-11-19 16:40:41 -0600111 const struct ipa_interconnect_data *data;
Alex Elderba764c42020-03-05 22:28:19 -0600112 struct ipa_clock *clock = ipa->clock;
113 int ret;
114
Alex Elder91d02f92020-11-19 16:40:41 -0600115 data = &clock->interconnect_data[IPA_INTERCONNECT_MEMORY];
Alex Elder5b408102021-01-15 06:50:46 -0600116 ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path,
117 data->average_bandwidth, data->peak_bandwidth);
Alex Elderba764c42020-03-05 22:28:19 -0600118 if (ret)
119 return ret;
120
Alex Elder91d02f92020-11-19 16:40:41 -0600121 data = &clock->interconnect_data[IPA_INTERCONNECT_IMEM];
Alex Elder5b408102021-01-15 06:50:46 -0600122 ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_IMEM]->path,
123 data->average_bandwidth, data->peak_bandwidth);
Alex Elderba764c42020-03-05 22:28:19 -0600124 if (ret)
125 goto err_memory_path_disable;
126
Alex Elder91d02f92020-11-19 16:40:41 -0600127 data = &clock->interconnect_data[IPA_INTERCONNECT_CONFIG];
Alex Elder5b408102021-01-15 06:50:46 -0600128 ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_CONFIG]->path,
129 data->average_bandwidth, data->peak_bandwidth);
Alex Elderba764c42020-03-05 22:28:19 -0600130 if (ret)
131 goto err_imem_path_disable;
132
133 return 0;
134
135err_imem_path_disable:
Alex Elder5b408102021-01-15 06:50:46 -0600136 (void)icc_set_bw(clock->interconnect[IPA_INTERCONNECT_IMEM]->path,
137 0, 0);
Alex Elderba764c42020-03-05 22:28:19 -0600138err_memory_path_disable:
Alex Elder5b408102021-01-15 06:50:46 -0600139 (void)icc_set_bw(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path,
140 0, 0);
Alex Elderba764c42020-03-05 22:28:19 -0600141
142 return ret;
143}
144
145/* To disable an interconnect, we just its bandwidth to 0 */
Alex Elderec0ef6d2021-01-15 06:50:45 -0600146static void ipa_interconnect_disable(struct ipa *ipa)
Alex Elderba764c42020-03-05 22:28:19 -0600147{
148 struct ipa_clock *clock = ipa->clock;
Alex Elderec0ef6d2021-01-15 06:50:45 -0600149 int result = 0;
Alex Elderba764c42020-03-05 22:28:19 -0600150 int ret;
151
Alex Elder5b408102021-01-15 06:50:46 -0600152 ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path,
153 0, 0);
Alex Elderba764c42020-03-05 22:28:19 -0600154 if (ret)
Alex Elderec0ef6d2021-01-15 06:50:45 -0600155 result = ret;
Alex Elderba764c42020-03-05 22:28:19 -0600156
Alex Elder5b408102021-01-15 06:50:46 -0600157 ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_IMEM]->path,
158 0, 0);
Alex Elderec0ef6d2021-01-15 06:50:45 -0600159 if (ret && !result)
160 result = ret;
Alex Elderba764c42020-03-05 22:28:19 -0600161
Alex Elder5b408102021-01-15 06:50:46 -0600162 ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_CONFIG]->path,
163 0, 0);
Alex Elderec0ef6d2021-01-15 06:50:45 -0600164 if (ret && !result)
165 result = ret;
Alex Elderba764c42020-03-05 22:28:19 -0600166
Alex Elderec0ef6d2021-01-15 06:50:45 -0600167 if (result)
168 dev_err(&ipa->pdev->dev,
169 "error %d disabling IPA interconnects\n", ret);
Alex Elderba764c42020-03-05 22:28:19 -0600170}
171
172/* Turn on IPA clocks, including interconnects */
173static int ipa_clock_enable(struct ipa *ipa)
174{
175 int ret;
176
177 ret = ipa_interconnect_enable(ipa);
178 if (ret)
179 return ret;
180
181 ret = clk_prepare_enable(ipa->clock->core);
182 if (ret)
183 ipa_interconnect_disable(ipa);
184
185 return ret;
186}
187
188/* Inverse of ipa_clock_enable() */
189static void ipa_clock_disable(struct ipa *ipa)
190{
191 clk_disable_unprepare(ipa->clock->core);
Alex Elderec0ef6d2021-01-15 06:50:45 -0600192 ipa_interconnect_disable(ipa);
Alex Elderba764c42020-03-05 22:28:19 -0600193}
194
195/* Get an IPA clock reference, but only if the reference count is
196 * already non-zero. Returns true if the additional reference was
197 * added successfully, or false otherwise.
198 */
199bool ipa_clock_get_additional(struct ipa *ipa)
200{
Alex Elder0305b702020-09-17 12:39:20 -0500201 return refcount_inc_not_zero(&ipa->clock->count);
Alex Elderba764c42020-03-05 22:28:19 -0600202}
203
204/* Get an IPA clock reference. If the reference count is non-zero, it is
205 * incremented and return is immediate. Otherwise it is checked again
Alex Elderdc6e6072020-09-17 12:39:22 -0500206 * under protection of the mutex, and if appropriate the IPA clock
207 * is enabled.
Alex Elderba764c42020-03-05 22:28:19 -0600208 *
209 * Incrementing the reference count is intentionally deferred until
210 * after the clock is running and endpoints are resumed.
211 */
212void ipa_clock_get(struct ipa *ipa)
213{
214 struct ipa_clock *clock = ipa->clock;
215 int ret;
216
217 /* If the clock is running, just bump the reference count */
218 if (ipa_clock_get_additional(ipa))
219 return;
220
221 /* Otherwise get the mutex and check again */
222 mutex_lock(&clock->mutex);
223
224 /* A reference might have been added before we got the mutex. */
225 if (ipa_clock_get_additional(ipa))
226 goto out_mutex_unlock;
227
228 ret = ipa_clock_enable(ipa);
229 if (ret) {
230 dev_err(&ipa->pdev->dev, "error %d enabling IPA clock\n", ret);
231 goto out_mutex_unlock;
232 }
233
Alex Elder0305b702020-09-17 12:39:20 -0500234 refcount_set(&clock->count, 1);
Alex Elderba764c42020-03-05 22:28:19 -0600235
236out_mutex_unlock:
237 mutex_unlock(&clock->mutex);
238}
239
Alex Elderdc6e6072020-09-17 12:39:22 -0500240/* Attempt to remove an IPA clock reference. If this represents the
241 * last reference, disable the IPA clock under protection of the mutex.
Alex Elderba764c42020-03-05 22:28:19 -0600242 */
243void ipa_clock_put(struct ipa *ipa)
244{
245 struct ipa_clock *clock = ipa->clock;
246
247 /* If this is not the last reference there's nothing more to do */
Alex Elder0305b702020-09-17 12:39:20 -0500248 if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex))
Alex Elderba764c42020-03-05 22:28:19 -0600249 return;
250
Alex Elderba764c42020-03-05 22:28:19 -0600251 ipa_clock_disable(ipa);
252
253 mutex_unlock(&clock->mutex);
254}
255
Alex Elder78b348f2020-07-03 16:23:34 -0500256/* Return the current IPA core clock rate */
257u32 ipa_clock_rate(struct ipa *ipa)
258{
259 return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0;
260}
261
Alex Elderba764c42020-03-05 22:28:19 -0600262/* Initialize IPA clocking */
Alex Elderdfccb8b2020-11-19 16:40:39 -0600263struct ipa_clock *
264ipa_clock_init(struct device *dev, const struct ipa_clock_data *data)
Alex Elderba764c42020-03-05 22:28:19 -0600265{
266 struct ipa_clock *clock;
267 struct clk *clk;
268 int ret;
269
270 clk = clk_get(dev, "core");
271 if (IS_ERR(clk)) {
272 dev_err(dev, "error %ld getting core clock\n", PTR_ERR(clk));
273 return ERR_CAST(clk);
274 }
275
Alex Elder91d02f92020-11-19 16:40:41 -0600276 ret = clk_set_rate(clk, data->core_clock_rate);
Alex Elderba764c42020-03-05 22:28:19 -0600277 if (ret) {
Alex Elder91d02f92020-11-19 16:40:41 -0600278 dev_err(dev, "error %d setting core clock rate to %u\n",
279 ret, data->core_clock_rate);
Alex Elderba764c42020-03-05 22:28:19 -0600280 goto err_clk_put;
281 }
282
283 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
284 if (!clock) {
285 ret = -ENOMEM;
286 goto err_clk_put;
287 }
288 clock->core = clk;
Alex Elderdfccb8b2020-11-19 16:40:39 -0600289 clock->interconnect_data = data->interconnect;
Alex Elderba764c42020-03-05 22:28:19 -0600290
291 ret = ipa_interconnect_init(clock, dev);
292 if (ret)
293 goto err_kfree;
294
295 mutex_init(&clock->mutex);
Alex Elder0305b702020-09-17 12:39:20 -0500296 refcount_set(&clock->count, 0);
Alex Elderba764c42020-03-05 22:28:19 -0600297
298 return clock;
299
300err_kfree:
301 kfree(clock);
302err_clk_put:
303 clk_put(clk);
304
305 return ERR_PTR(ret);
306}
307
308/* Inverse of ipa_clock_init() */
309void ipa_clock_exit(struct ipa_clock *clock)
310{
311 struct clk *clk = clock->core;
312
Alex Elder0305b702020-09-17 12:39:20 -0500313 WARN_ON(refcount_read(&clock->count) != 0);
Alex Elderba764c42020-03-05 22:28:19 -0600314 mutex_destroy(&clock->mutex);
315 ipa_interconnect_exit(clock);
316 kfree(clock);
317 clk_put(clk);
318}