1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2021 Linaro Ltd.
7 #include <linux/refcount.h>
8 #include <linux/mutex.h>
10 #include <linux/device.h>
11 #include <linux/interconnect.h>
14 #include "ipa_clock.h"
15 #include "ipa_modem.h"
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.
30 * An IPA clock reference must be held for any access to IPA hardware.
34 * struct ipa_interconnect - IPA interconnect information
35 * @path: Interconnect path
36 * @average_bandwidth: Average interconnect bandwidth (KB/second)
37 * @peak_bandwidth: Peak interconnect bandwidth (KB/second)
39 struct ipa_interconnect {
40 struct icc_path *path;
41 u32 average_bandwidth;
46 * struct ipa_clock - IPA clocking information
47 * @count: Clocking reference count
48 * @mutex: Protects clock enable/disable
49 * @core: IPA core clock
50 * @interconnect_count: Number of elements in interconnect[]
51 * @interconnect: Interconnect array
55 struct mutex mutex; /* protects clock enable/disable */
57 u32 interconnect_count;
58 struct ipa_interconnect *interconnect;
61 static int ipa_interconnect_init_one(struct device *dev,
62 struct ipa_interconnect *interconnect,
63 const struct ipa_interconnect_data *data)
65 struct icc_path *path;
67 path = of_icc_get(dev, data->name);
69 int ret = PTR_ERR(path);
71 dev_err_probe(dev, ret, "error getting %s interconnect\n",
77 interconnect->path = path;
78 interconnect->average_bandwidth = data->average_bandwidth;
79 interconnect->peak_bandwidth = data->peak_bandwidth;
84 static void ipa_interconnect_exit_one(struct ipa_interconnect *interconnect)
86 icc_put(interconnect->path);
87 memset(interconnect, 0, sizeof(*interconnect));
90 /* Initialize interconnects required for IPA operation */
91 static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev,
92 const struct ipa_interconnect_data *data)
94 struct ipa_interconnect *interconnect;
98 count = clock->interconnect_count;
99 interconnect = kcalloc(count, sizeof(*interconnect), GFP_KERNEL);
102 clock->interconnect = interconnect;
105 ret = ipa_interconnect_init_one(dev, interconnect, data++);
114 while (interconnect-- > clock->interconnect)
115 ipa_interconnect_exit_one(interconnect);
116 kfree(clock->interconnect);
117 clock->interconnect = NULL;
122 /* Inverse of ipa_interconnect_init() */
123 static void ipa_interconnect_exit(struct ipa_clock *clock)
125 struct ipa_interconnect *interconnect;
127 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;
134 /* Currently we only use one bandwidth level, so just "enable" interconnects */
135 static int ipa_interconnect_enable(struct ipa *ipa)
137 struct ipa_interconnect *interconnect;
138 struct ipa_clock *clock = ipa->clock;
142 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);
155 while (interconnect-- > clock->interconnect)
156 (void)icc_set_bw(interconnect->path, 0, 0);
161 /* To disable an interconnect, we just its bandwidth to 0 */
162 static void ipa_interconnect_disable(struct ipa *ipa)
164 struct ipa_interconnect *interconnect;
165 struct ipa_clock *clock = ipa->clock;
170 count = clock->interconnect_count;
171 interconnect = clock->interconnect + count;
174 ret = icc_set_bw(interconnect->path, 0, 0);
180 dev_err(&ipa->pdev->dev,
181 "error %d disabling IPA interconnects\n", ret);
184 /* Turn on IPA clocks, including interconnects */
185 static int ipa_clock_enable(struct ipa *ipa)
189 ret = ipa_interconnect_enable(ipa);
193 ret = clk_prepare_enable(ipa->clock->core);
195 ipa_interconnect_disable(ipa);
200 /* Inverse of ipa_clock_enable() */
201 static void ipa_clock_disable(struct ipa *ipa)
203 clk_disable_unprepare(ipa->clock->core);
204 ipa_interconnect_disable(ipa);
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.
211 bool ipa_clock_get_additional(struct ipa *ipa)
213 return refcount_inc_not_zero(&ipa->clock->count);
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
218 * under protection of the mutex, and if appropriate the IPA clock
221 * Incrementing the reference count is intentionally deferred until
222 * after the clock is running and endpoints are resumed.
224 void ipa_clock_get(struct ipa *ipa)
226 struct ipa_clock *clock = ipa->clock;
229 /* If the clock is running, just bump the reference count */
230 if (ipa_clock_get_additional(ipa))
233 /* Otherwise get the mutex and check again */
234 mutex_lock(&clock->mutex);
236 /* A reference might have been added before we got the mutex. */
237 if (ipa_clock_get_additional(ipa))
238 goto out_mutex_unlock;
240 ret = ipa_clock_enable(ipa);
242 dev_err(&ipa->pdev->dev, "error %d enabling IPA clock\n", ret);
243 goto out_mutex_unlock;
246 refcount_set(&clock->count, 1);
249 mutex_unlock(&clock->mutex);
252 /* Attempt to remove an IPA clock reference. If this represents the
253 * last reference, disable the IPA clock under protection of the mutex.
255 void ipa_clock_put(struct ipa *ipa)
257 struct ipa_clock *clock = ipa->clock;
259 /* If this is not the last reference there's nothing more to do */
260 if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex))
263 ipa_clock_disable(ipa);
265 mutex_unlock(&clock->mutex);
268 /* Return the current IPA core clock rate */
269 u32 ipa_clock_rate(struct ipa *ipa)
271 return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0;
274 /* Initialize IPA clocking */
276 ipa_clock_init(struct device *dev, const struct ipa_clock_data *data)
278 struct ipa_clock *clock;
282 clk = clk_get(dev, "core");
284 dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n");
286 return ERR_CAST(clk);
289 ret = clk_set_rate(clk, data->core_clock_rate);
291 dev_err(dev, "error %d setting core clock rate to %u\n",
292 ret, data->core_clock_rate);
296 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
302 clock->interconnect_count = data->interconnect_count;
304 ret = ipa_interconnect_init(clock, dev, data->interconnect_data);
308 mutex_init(&clock->mutex);
309 refcount_set(&clock->count, 0);
321 /* Inverse of ipa_clock_init() */
322 void ipa_clock_exit(struct ipa_clock *clock)
324 struct clk *clk = clock->core;
326 WARN_ON(refcount_read(&clock->count) != 0);
327 mutex_destroy(&clock->mutex);
328 ipa_interconnect_exit(clock);