1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2020 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"
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.
29 * An IPA clock reference must be held for any access to IPA hardware.
32 #define IPA_CORE_CLOCK_RATE (75UL * 1000 * 1000) /* Hz */
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)
38 #define IPA_IMEM_AVG (80 * 1000)
39 #define IPA_IMEM_PEAK (350 * 1000)
41 #define IPA_CONFIG_AVG (40 * 1000)
42 #define IPA_CONFIG_PEAK (40 * 1000)
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
55 struct mutex mutex; /* protects clock enable/disable */
57 struct icc_path *memory_path;
58 struct icc_path *imem_path;
59 struct icc_path *config_path;
62 static struct icc_path *
63 ipa_interconnect_init_one(struct device *dev, const char *name)
65 struct icc_path *path;
67 path = of_icc_get(dev, name);
69 dev_err(dev, "error %ld getting %s interconnect\n",
75 /* Initialize interconnects required for IPA operation */
76 static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev)
78 struct icc_path *path;
80 path = ipa_interconnect_init_one(dev, "memory");
83 clock->memory_path = path;
85 path = ipa_interconnect_init_one(dev, "imem");
87 goto err_memory_path_put;
88 clock->imem_path = path;
90 path = ipa_interconnect_init_one(dev, "config");
92 goto err_imem_path_put;
93 clock->config_path = path;
98 icc_put(clock->imem_path);
100 icc_put(clock->memory_path);
102 return PTR_ERR(path);
105 /* Inverse of ipa_interconnect_init() */
106 static void ipa_interconnect_exit(struct ipa_clock *clock)
108 icc_put(clock->config_path);
109 icc_put(clock->imem_path);
110 icc_put(clock->memory_path);
113 /* Currently we only use one bandwidth level, so just "enable" interconnects */
114 static int ipa_interconnect_enable(struct ipa *ipa)
116 struct ipa_clock *clock = ipa->clock;
119 ret = icc_set_bw(clock->memory_path, IPA_MEMORY_AVG, IPA_MEMORY_PEAK);
123 ret = icc_set_bw(clock->imem_path, IPA_IMEM_AVG, IPA_IMEM_PEAK);
125 goto err_memory_path_disable;
127 ret = icc_set_bw(clock->config_path, IPA_CONFIG_AVG, IPA_CONFIG_PEAK);
129 goto err_imem_path_disable;
133 err_imem_path_disable:
134 (void)icc_set_bw(clock->imem_path, 0, 0);
135 err_memory_path_disable:
136 (void)icc_set_bw(clock->memory_path, 0, 0);
141 /* To disable an interconnect, we just its bandwidth to 0 */
142 static int ipa_interconnect_disable(struct ipa *ipa)
144 struct ipa_clock *clock = ipa->clock;
147 ret = icc_set_bw(clock->memory_path, 0, 0);
151 ret = icc_set_bw(clock->imem_path, 0, 0);
153 goto err_memory_path_reenable;
155 ret = icc_set_bw(clock->config_path, 0, 0);
157 goto err_imem_path_reenable;
161 err_imem_path_reenable:
162 (void)icc_set_bw(clock->imem_path, IPA_IMEM_AVG, IPA_IMEM_PEAK);
163 err_memory_path_reenable:
164 (void)icc_set_bw(clock->memory_path, IPA_MEMORY_AVG, IPA_MEMORY_PEAK);
169 /* Turn on IPA clocks, including interconnects */
170 static int ipa_clock_enable(struct ipa *ipa)
174 ret = ipa_interconnect_enable(ipa);
178 ret = clk_prepare_enable(ipa->clock->core);
180 ipa_interconnect_disable(ipa);
185 /* Inverse of ipa_clock_enable() */
186 static void ipa_clock_disable(struct ipa *ipa)
188 clk_disable_unprepare(ipa->clock->core);
189 (void)ipa_interconnect_disable(ipa);
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.
196 bool ipa_clock_get_additional(struct ipa *ipa)
198 return refcount_inc_not_zero(&ipa->clock->count);
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 IPA clock
206 * Incrementing the reference count is intentionally deferred until
207 * after the clock is running and endpoints are resumed.
209 void ipa_clock_get(struct ipa *ipa)
211 struct ipa_clock *clock = ipa->clock;
214 /* If the clock is running, just bump the reference count */
215 if (ipa_clock_get_additional(ipa))
218 /* Otherwise get the mutex and check again */
219 mutex_lock(&clock->mutex);
221 /* A reference might have been added before we got the mutex. */
222 if (ipa_clock_get_additional(ipa))
223 goto out_mutex_unlock;
225 ret = ipa_clock_enable(ipa);
227 dev_err(&ipa->pdev->dev, "error %d enabling IPA clock\n", ret);
228 goto out_mutex_unlock;
231 refcount_set(&clock->count, 1);
234 mutex_unlock(&clock->mutex);
237 /* Attempt to remove an IPA clock reference. If this represents the
238 * last reference, disable the IPA clock under protection of the mutex.
240 void ipa_clock_put(struct ipa *ipa)
242 struct ipa_clock *clock = ipa->clock;
244 /* If this is not the last reference there's nothing more to do */
245 if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex))
248 ipa_clock_disable(ipa);
250 mutex_unlock(&clock->mutex);
253 /* Return the current IPA core clock rate */
254 u32 ipa_clock_rate(struct ipa *ipa)
256 return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0;
259 /* Initialize IPA clocking */
260 struct ipa_clock *ipa_clock_init(struct device *dev)
262 struct ipa_clock *clock;
266 clk = clk_get(dev, "core");
268 dev_err(dev, "error %ld getting core clock\n", PTR_ERR(clk));
269 return ERR_CAST(clk);
272 ret = clk_set_rate(clk, IPA_CORE_CLOCK_RATE);
274 dev_err(dev, "error %d setting core clock rate to %lu\n",
275 ret, IPA_CORE_CLOCK_RATE);
279 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
286 ret = ipa_interconnect_init(clock, dev);
290 mutex_init(&clock->mutex);
291 refcount_set(&clock->count, 0);
303 /* Inverse of ipa_clock_init() */
304 void ipa_clock_exit(struct ipa_clock *clock)
306 struct clk *clk = clock->core;
308 WARN_ON(refcount_read(&clock->count) != 0);
309 mutex_destroy(&clock->mutex);
310 ipa_interconnect_exit(clock);