Loading...
Note: File does not exist in v4.17.
1// 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
7#include <linux/atomic.h>
8#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"
16
17/**
18 * DOC: IPA Clocking
19 *
20 * The "IPA Clock" manages both the IPA core clock and the interconnects
21 * (buses) the IPA depends on as a single logical entity. A reference count
22 * is incremented by "get" operations and decremented by "put" operations.
23 * Transitions of that count from 0 to 1 result in the clock and interconnects
24 * being enabled, and transitions of the count from 1 to 0 cause them to be
25 * disabled. We currently operate the core clock at a fixed clock rate, and
26 * all buses at a fixed average and peak bandwidth. As more advanced IPA
27 * features are enabled, we can make better use of clock and bus scaling.
28 *
29 * An IPA clock reference must be held for any access to IPA hardware.
30 */
31
32#define IPA_CORE_CLOCK_RATE (75UL * 1000 * 1000) /* Hz */
33
34/* Interconnect path bandwidths (each times 1000 bytes per second) */
35#define IPA_MEMORY_AVG (80 * 1000) /* 80 MBps */
36#define IPA_MEMORY_PEAK (600 * 1000)
37
38#define IPA_IMEM_AVG (80 * 1000)
39#define IPA_IMEM_PEAK (350 * 1000)
40
41#define IPA_CONFIG_AVG (40 * 1000)
42#define IPA_CONFIG_PEAK (40 * 1000)
43
44/**
45 * struct ipa_clock - IPA clocking information
46 * @count: Clocking reference count
47 * @mutex: Protects clock enable/disable
48 * @core: IPA core clock
49 * @memory_path: Memory interconnect
50 * @imem_path: Internal memory interconnect
51 * @config_path: Configuration space interconnect
52 */
53struct ipa_clock {
54 atomic_t count;
55 struct mutex mutex; /* protects clock enable/disable */
56 struct clk *core;
57 struct icc_path *memory_path;
58 struct icc_path *imem_path;
59 struct icc_path *config_path;
60};
61
62static struct icc_path *
63ipa_interconnect_init_one(struct device *dev, const char *name)
64{
65 struct icc_path *path;
66
67 path = of_icc_get(dev, name);
68 if (IS_ERR(path))
69 dev_err(dev, "error %ld getting %s interconnect\n",
70 PTR_ERR(path), name);
71
72 return path;
73}
74
75/* Initialize interconnects required for IPA operation */
76static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev)
77{
78 struct icc_path *path;
79
80 path = ipa_interconnect_init_one(dev, "memory");
81 if (IS_ERR(path))
82 goto err_return;
83 clock->memory_path = path;
84
85 path = ipa_interconnect_init_one(dev, "imem");
86 if (IS_ERR(path))
87 goto err_memory_path_put;
88 clock->imem_path = path;
89
90 path = ipa_interconnect_init_one(dev, "config");
91 if (IS_ERR(path))
92 goto err_imem_path_put;
93 clock->config_path = path;
94
95 return 0;
96
97err_imem_path_put:
98 icc_put(clock->imem_path);
99err_memory_path_put:
100 icc_put(clock->memory_path);
101err_return:
102 return PTR_ERR(path);
103}
104
105/* Inverse of ipa_interconnect_init() */
106static void ipa_interconnect_exit(struct ipa_clock *clock)
107{
108 icc_put(clock->config_path);
109 icc_put(clock->imem_path);
110 icc_put(clock->memory_path);
111}
112
113/* Currently we only use one bandwidth level, so just "enable" interconnects */
114static int ipa_interconnect_enable(struct ipa *ipa)
115{
116 struct ipa_clock *clock = ipa->clock;
117 int ret;
118
119 ret = icc_set_bw(clock->memory_path, IPA_MEMORY_AVG, IPA_MEMORY_PEAK);
120 if (ret)
121 return ret;
122
123 ret = icc_set_bw(clock->imem_path, IPA_IMEM_AVG, IPA_IMEM_PEAK);
124 if (ret)
125 goto err_memory_path_disable;
126
127 ret = icc_set_bw(clock->config_path, IPA_CONFIG_AVG, IPA_CONFIG_PEAK);
128 if (ret)
129 goto err_imem_path_disable;
130
131 return 0;
132
133err_imem_path_disable:
134 (void)icc_set_bw(clock->imem_path, 0, 0);
135err_memory_path_disable:
136 (void)icc_set_bw(clock->memory_path, 0, 0);
137
138 return ret;
139}
140
141/* To disable an interconnect, we just its bandwidth to 0 */
142static int ipa_interconnect_disable(struct ipa *ipa)
143{
144 struct ipa_clock *clock = ipa->clock;
145 int ret;
146
147 ret = icc_set_bw(clock->memory_path, 0, 0);
148 if (ret)
149 return ret;
150
151 ret = icc_set_bw(clock->imem_path, 0, 0);
152 if (ret)
153 goto err_memory_path_reenable;
154
155 ret = icc_set_bw(clock->config_path, 0, 0);
156 if (ret)
157 goto err_imem_path_reenable;
158
159 return 0;
160
161err_imem_path_reenable:
162 (void)icc_set_bw(clock->imem_path, IPA_IMEM_AVG, IPA_IMEM_PEAK);
163err_memory_path_reenable:
164 (void)icc_set_bw(clock->memory_path, IPA_MEMORY_AVG, IPA_MEMORY_PEAK);
165
166 return ret;
167}
168
169/* Turn on IPA clocks, including interconnects */
170static int ipa_clock_enable(struct ipa *ipa)
171{
172 int ret;
173
174 ret = ipa_interconnect_enable(ipa);
175 if (ret)
176 return ret;
177
178 ret = clk_prepare_enable(ipa->clock->core);
179 if (ret)
180 ipa_interconnect_disable(ipa);
181
182 return ret;
183}
184
185/* Inverse of ipa_clock_enable() */
186static void ipa_clock_disable(struct ipa *ipa)
187{
188 clk_disable_unprepare(ipa->clock->core);
189 (void)ipa_interconnect_disable(ipa);
190}
191
192/* Get an IPA clock reference, but only if the reference count is
193 * already non-zero. Returns true if the additional reference was
194 * added successfully, or false otherwise.
195 */
196bool ipa_clock_get_additional(struct ipa *ipa)
197{
198 return !!atomic_inc_not_zero(&ipa->clock->count);
199}
200
201/* Get an IPA clock reference. If the reference count is non-zero, it is
202 * incremented and return is immediate. Otherwise it is checked again
203 * under protection of the mutex, and if appropriate the clock (and
204 * interconnects) are enabled suspended endpoints (if any) are resumed
205 * before returning.
206 *
207 * Incrementing the reference count is intentionally deferred until
208 * after the clock is running and endpoints are resumed.
209 */
210void ipa_clock_get(struct ipa *ipa)
211{
212 struct ipa_clock *clock = ipa->clock;
213 int ret;
214
215 /* If the clock is running, just bump the reference count */
216 if (ipa_clock_get_additional(ipa))
217 return;
218
219 /* Otherwise get the mutex and check again */
220 mutex_lock(&clock->mutex);
221
222 /* A reference might have been added before we got the mutex. */
223 if (ipa_clock_get_additional(ipa))
224 goto out_mutex_unlock;
225
226 ret = ipa_clock_enable(ipa);
227 if (ret) {
228 dev_err(&ipa->pdev->dev, "error %d enabling IPA clock\n", ret);
229 goto out_mutex_unlock;
230 }
231
232 ipa_endpoint_resume(ipa);
233
234 atomic_inc(&clock->count);
235
236out_mutex_unlock:
237 mutex_unlock(&clock->mutex);
238}
239
240/* Attempt to remove an IPA clock reference. If this represents the last
241 * reference, suspend endpoints and disable the clock (and interconnects)
242 * under protection of a mutex.
243 */
244void ipa_clock_put(struct ipa *ipa)
245{
246 struct ipa_clock *clock = ipa->clock;
247
248 /* If this is not the last reference there's nothing more to do */
249 if (!atomic_dec_and_mutex_lock(&clock->count, &clock->mutex))
250 return;
251
252 ipa_endpoint_suspend(ipa);
253
254 ipa_clock_disable(ipa);
255
256 mutex_unlock(&clock->mutex);
257}
258
259/* Return the current IPA core clock rate */
260u32 ipa_clock_rate(struct ipa *ipa)
261{
262 return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0;
263}
264
265/* Initialize IPA clocking */
266struct ipa_clock *ipa_clock_init(struct device *dev)
267{
268 struct ipa_clock *clock;
269 struct clk *clk;
270 int ret;
271
272 clk = clk_get(dev, "core");
273 if (IS_ERR(clk)) {
274 dev_err(dev, "error %ld getting core clock\n", PTR_ERR(clk));
275 return ERR_CAST(clk);
276 }
277
278 ret = clk_set_rate(clk, IPA_CORE_CLOCK_RATE);
279 if (ret) {
280 dev_err(dev, "error %d setting core clock rate to %lu\n",
281 ret, IPA_CORE_CLOCK_RATE);
282 goto err_clk_put;
283 }
284
285 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
286 if (!clock) {
287 ret = -ENOMEM;
288 goto err_clk_put;
289 }
290 clock->core = clk;
291
292 ret = ipa_interconnect_init(clock, dev);
293 if (ret)
294 goto err_kfree;
295
296 mutex_init(&clock->mutex);
297 atomic_set(&clock->count, 0);
298
299 return clock;
300
301err_kfree:
302 kfree(clock);
303err_clk_put:
304 clk_put(clk);
305
306 return ERR_PTR(ret);
307}
308
309/* Inverse of ipa_clock_init() */
310void ipa_clock_exit(struct ipa_clock *clock)
311{
312 struct clk *clk = clock->core;
313
314 WARN_ON(atomic_read(&clock->count) != 0);
315 mutex_destroy(&clock->mutex);
316 ipa_interconnect_exit(clock);
317 kfree(clock);
318 clk_put(clk);
319}