blob: 354675a643db5193f97044ffd8230a182dd01bf3 [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
Alex Elderdb6cd512021-01-15 06:50:47 -060036 * @average_bandwidth: Average interconnect bandwidth (KB/second)
37 * @peak_bandwidth: Peak interconnect bandwidth (KB/second)
Alex Elder5b408102021-01-15 06:50:46 -060038 */
39struct ipa_interconnect {
40 struct icc_path *path;
Alex Elderdb6cd512021-01-15 06:50:47 -060041 u32 average_bandwidth;
42 u32 peak_bandwidth;
Alex Elder5b408102021-01-15 06:50:46 -060043};
44
45/**
Alex Elderba764c42020-03-05 22:28:19 -060046 * struct ipa_clock - IPA clocking information
47 * @count: Clocking reference count
Alex Eldere3eea082020-07-13 07:24:18 -050048 * @mutex: Protects clock enable/disable
Alex Elderba764c42020-03-05 22:28:19 -060049 * @core: IPA core clock
Alex Elderea151e12021-01-15 06:50:50 -060050 * @interconnect_count: Number of elements in interconnect[]
Alex Elder5b408102021-01-15 06:50:46 -060051 * @interconnect: Interconnect array
Alex Elderba764c42020-03-05 22:28:19 -060052 */
53struct ipa_clock {
Alex Elder0305b702020-09-17 12:39:20 -050054 refcount_t count;
Alex Elderba764c42020-03-05 22:28:19 -060055 struct mutex mutex; /* protects clock enable/disable */
56 struct clk *core;
Alex Elderea151e12021-01-15 06:50:50 -060057 u32 interconnect_count;
58 struct ipa_interconnect *interconnect;
Alex Elderba764c42020-03-05 22:28:19 -060059};
60
Alex Elder10d0d392021-01-15 06:50:49 -060061static int ipa_interconnect_init_one(struct device *dev,
62 struct ipa_interconnect *interconnect,
63 const struct ipa_interconnect_data *data)
Alex Elderba764c42020-03-05 22:28:19 -060064{
65 struct icc_path *path;
66
Alex Elder10d0d392021-01-15 06:50:49 -060067 path = of_icc_get(dev, data->name);
68 if (IS_ERR(path)) {
69 int ret = PTR_ERR(path);
Alex Elderba764c42020-03-05 22:28:19 -060070
Alex Elder10d0d392021-01-15 06:50:49 -060071 dev_err(dev, "error %d getting %s interconnect\n", ret,
72 data->name);
73
74 return ret;
75 }
76
77 interconnect->path = path;
78 interconnect->average_bandwidth = data->average_bandwidth;
79 interconnect->peak_bandwidth = data->peak_bandwidth;
80
81 return 0;
82}
83
84static void ipa_interconnect_exit_one(struct ipa_interconnect *interconnect)
85{
86 icc_put(interconnect->path);
87 memset(interconnect, 0, sizeof(*interconnect));
Alex Elderba764c42020-03-05 22:28:19 -060088}
89
90/* Initialize interconnects required for IPA operation */
Alex Elderdb6cd512021-01-15 06:50:47 -060091static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev,
92 const struct ipa_interconnect_data *data)
Alex Elderba764c42020-03-05 22:28:19 -060093{
Alex Elderdb6cd512021-01-15 06:50:47 -060094 struct ipa_interconnect *interconnect;
Alex Elderea151e12021-01-15 06:50:50 -060095 u32 count;
Alex Elder10d0d392021-01-15 06:50:49 -060096 int ret;
Alex Elderba764c42020-03-05 22:28:19 -060097
Alex Elderea151e12021-01-15 06:50:50 -060098 count = clock->interconnect_count;
99 interconnect = kcalloc(count, sizeof(*interconnect), GFP_KERNEL);
100 if (!interconnect)
101 return -ENOMEM;
102 clock->interconnect = interconnect;
Alex Elderba764c42020-03-05 22:28:19 -0600103
Alex Elderea151e12021-01-15 06:50:50 -0600104 while (count--) {
105 ret = ipa_interconnect_init_one(dev, interconnect, data++);
106 if (ret)
107 goto out_unwind;
108 interconnect++;
109 }
Alex Elderba764c42020-03-05 22:28:19 -0600110
111 return 0;
112
Alex Elderea151e12021-01-15 06:50:50 -0600113out_unwind:
114 while (interconnect-- > clock->interconnect)
115 ipa_interconnect_exit_one(interconnect);
116 kfree(clock->interconnect);
117 clock->interconnect = NULL;
Alex Elder10d0d392021-01-15 06:50:49 -0600118
119 return ret;
Alex Elderba764c42020-03-05 22:28:19 -0600120}
121
122/* Inverse of ipa_interconnect_init() */
123static void ipa_interconnect_exit(struct ipa_clock *clock)
124{
Alex Elder10d0d392021-01-15 06:50:49 -0600125 struct ipa_interconnect *interconnect;
126
Alex Elderea151e12021-01-15 06:50:50 -0600127 interconnect = clock->interconnect + clock->interconnect_count;
128 while (interconnect-- > clock->interconnect)
129 ipa_interconnect_exit_one(interconnect);
130 kfree(clock->interconnect);
131 clock->interconnect = NULL;
Alex Elderba764c42020-03-05 22:28:19 -0600132}
133
134/* Currently we only use one bandwidth level, so just "enable" interconnects */
135static int ipa_interconnect_enable(struct ipa *ipa)
136{
Alex Elderdb6cd512021-01-15 06:50:47 -0600137 struct ipa_interconnect *interconnect;
Alex Elderba764c42020-03-05 22:28:19 -0600138 struct ipa_clock *clock = ipa->clock;
139 int ret;
Alex Elderea151e12021-01-15 06:50:50 -0600140 u32 i;
Alex Elderba764c42020-03-05 22:28:19 -0600141
Alex Elderea151e12021-01-15 06:50:50 -0600142 interconnect = clock->interconnect;
143 for (i = 0; i < clock->interconnect_count; i++) {
144 ret = icc_set_bw(interconnect->path,
145 interconnect->average_bandwidth,
146 interconnect->peak_bandwidth);
147 if (ret)
148 goto out_unwind;
149 interconnect++;
150 }
Alex Elderba764c42020-03-05 22:28:19 -0600151
152 return 0;
153
Alex Elderea151e12021-01-15 06:50:50 -0600154out_unwind:
155 while (interconnect-- > clock->interconnect)
156 (void)icc_set_bw(interconnect->path, 0, 0);
Alex Elderba764c42020-03-05 22:28:19 -0600157
158 return ret;
159}
160
161/* To disable an interconnect, we just its bandwidth to 0 */
Alex Elderec0ef6d2021-01-15 06:50:45 -0600162static void ipa_interconnect_disable(struct ipa *ipa)
Alex Elderba764c42020-03-05 22:28:19 -0600163{
Alex Elderdb6cd512021-01-15 06:50:47 -0600164 struct ipa_interconnect *interconnect;
Alex Elderba764c42020-03-05 22:28:19 -0600165 struct ipa_clock *clock = ipa->clock;
Alex Elderec0ef6d2021-01-15 06:50:45 -0600166 int result = 0;
Alex Elderea151e12021-01-15 06:50:50 -0600167 u32 count;
Alex Elderba764c42020-03-05 22:28:19 -0600168 int ret;
169
Alex Elderea151e12021-01-15 06:50:50 -0600170 count = clock->interconnect_count;
171 interconnect = clock->interconnect + count;
172 while (count--) {
173 interconnect--;
174 ret = icc_set_bw(interconnect->path, 0, 0);
175 if (ret && !result)
176 result = ret;
177 }
Alex Elderba764c42020-03-05 22:28:19 -0600178
Alex Elderec0ef6d2021-01-15 06:50:45 -0600179 if (result)
180 dev_err(&ipa->pdev->dev,
181 "error %d disabling IPA interconnects\n", ret);
Alex Elderba764c42020-03-05 22:28:19 -0600182}
183
184/* Turn on IPA clocks, including interconnects */
185static int ipa_clock_enable(struct ipa *ipa)
186{
187 int ret;
188
189 ret = ipa_interconnect_enable(ipa);
190 if (ret)
191 return ret;
192
193 ret = clk_prepare_enable(ipa->clock->core);
194 if (ret)
195 ipa_interconnect_disable(ipa);
196
197 return ret;
198}
199
200/* Inverse of ipa_clock_enable() */
201static void ipa_clock_disable(struct ipa *ipa)
202{
203 clk_disable_unprepare(ipa->clock->core);
Alex Elderec0ef6d2021-01-15 06:50:45 -0600204 ipa_interconnect_disable(ipa);
Alex Elderba764c42020-03-05 22:28:19 -0600205}
206
207/* Get an IPA clock reference, but only if the reference count is
208 * already non-zero. Returns true if the additional reference was
209 * added successfully, or false otherwise.
210 */
211bool ipa_clock_get_additional(struct ipa *ipa)
212{
Alex Elder0305b702020-09-17 12:39:20 -0500213 return refcount_inc_not_zero(&ipa->clock->count);
Alex Elderba764c42020-03-05 22:28:19 -0600214}
215
216/* Get an IPA clock reference. If the reference count is non-zero, it is
217 * incremented and return is immediate. Otherwise it is checked again
Alex Elderdc6e6072020-09-17 12:39:22 -0500218 * under protection of the mutex, and if appropriate the IPA clock
219 * is enabled.
Alex Elderba764c42020-03-05 22:28:19 -0600220 *
221 * Incrementing the reference count is intentionally deferred until
222 * after the clock is running and endpoints are resumed.
223 */
224void ipa_clock_get(struct ipa *ipa)
225{
226 struct ipa_clock *clock = ipa->clock;
227 int ret;
228
229 /* If the clock is running, just bump the reference count */
230 if (ipa_clock_get_additional(ipa))
231 return;
232
233 /* Otherwise get the mutex and check again */
234 mutex_lock(&clock->mutex);
235
236 /* A reference might have been added before we got the mutex. */
237 if (ipa_clock_get_additional(ipa))
238 goto out_mutex_unlock;
239
240 ret = ipa_clock_enable(ipa);
241 if (ret) {
242 dev_err(&ipa->pdev->dev, "error %d enabling IPA clock\n", ret);
243 goto out_mutex_unlock;
244 }
245
Alex Elder0305b702020-09-17 12:39:20 -0500246 refcount_set(&clock->count, 1);
Alex Elderba764c42020-03-05 22:28:19 -0600247
248out_mutex_unlock:
249 mutex_unlock(&clock->mutex);
250}
251
Alex Elderdc6e6072020-09-17 12:39:22 -0500252/* Attempt to remove an IPA clock reference. If this represents the
253 * last reference, disable the IPA clock under protection of the mutex.
Alex Elderba764c42020-03-05 22:28:19 -0600254 */
255void ipa_clock_put(struct ipa *ipa)
256{
257 struct ipa_clock *clock = ipa->clock;
258
259 /* If this is not the last reference there's nothing more to do */
Alex Elder0305b702020-09-17 12:39:20 -0500260 if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex))
Alex Elderba764c42020-03-05 22:28:19 -0600261 return;
262
Alex Elderba764c42020-03-05 22:28:19 -0600263 ipa_clock_disable(ipa);
264
265 mutex_unlock(&clock->mutex);
266}
267
Alex Elder78b348f2020-07-03 16:23:34 -0500268/* Return the current IPA core clock rate */
269u32 ipa_clock_rate(struct ipa *ipa)
270{
271 return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0;
272}
273
Alex Elderba764c42020-03-05 22:28:19 -0600274/* Initialize IPA clocking */
Alex Elderdfccb8b2020-11-19 16:40:39 -0600275struct ipa_clock *
276ipa_clock_init(struct device *dev, const struct ipa_clock_data *data)
Alex Elderba764c42020-03-05 22:28:19 -0600277{
278 struct ipa_clock *clock;
279 struct clk *clk;
280 int ret;
281
282 clk = clk_get(dev, "core");
283 if (IS_ERR(clk)) {
284 dev_err(dev, "error %ld getting core clock\n", PTR_ERR(clk));
285 return ERR_CAST(clk);
286 }
287
Alex Elder91d02f92020-11-19 16:40:41 -0600288 ret = clk_set_rate(clk, data->core_clock_rate);
Alex Elderba764c42020-03-05 22:28:19 -0600289 if (ret) {
Alex Elder91d02f92020-11-19 16:40:41 -0600290 dev_err(dev, "error %d setting core clock rate to %u\n",
291 ret, data->core_clock_rate);
Alex Elderba764c42020-03-05 22:28:19 -0600292 goto err_clk_put;
293 }
294
295 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
296 if (!clock) {
297 ret = -ENOMEM;
298 goto err_clk_put;
299 }
300 clock->core = clk;
Alex Elderea151e12021-01-15 06:50:50 -0600301 clock->interconnect_count = data->interconnect_count;
Alex Elderba764c42020-03-05 22:28:19 -0600302
Alex Elderea151e12021-01-15 06:50:50 -0600303 ret = ipa_interconnect_init(clock, dev, data->interconnect_data);
Alex Elderba764c42020-03-05 22:28:19 -0600304 if (ret)
305 goto err_kfree;
306
307 mutex_init(&clock->mutex);
Alex Elder0305b702020-09-17 12:39:20 -0500308 refcount_set(&clock->count, 0);
Alex Elderba764c42020-03-05 22:28:19 -0600309
310 return clock;
311
312err_kfree:
313 kfree(clock);
314err_clk_put:
315 clk_put(clk);
316
317 return ERR_PTR(ret);
318}
319
320/* Inverse of ipa_clock_init() */
321void ipa_clock_exit(struct ipa_clock *clock)
322{
323 struct clk *clk = clock->core;
324
Alex Elder0305b702020-09-17 12:39:20 -0500325 WARN_ON(refcount_read(&clock->count) != 0);
Alex Elderba764c42020-03-05 22:28:19 -0600326 mutex_destroy(&clock->mutex);
327 ipa_interconnect_exit(clock);
328 kfree(clock);
329 clk_put(clk);
330}