Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 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 | |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 7 | #include <linux/refcount.h> |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 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" |
Alex Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 16 | #include "ipa_data.h" |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 17 | |
| 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 Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 33 | /** |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 34 | * struct ipa_interconnect - IPA interconnect information |
| 35 | * @path: Interconnect path |
| 36 | */ |
| 37 | struct ipa_interconnect { |
| 38 | struct icc_path *path; |
| 39 | }; |
| 40 | |
| 41 | /** |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 42 | * struct ipa_clock - IPA clocking information |
| 43 | * @count: Clocking reference count |
Alex Elder | e3eea08 | 2020-07-13 07:24:18 -0500 | [diff] [blame] | 44 | * @mutex: Protects clock enable/disable |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 45 | * @core: IPA core clock |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 46 | * @interconnect: Interconnect array |
Alex Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 47 | * @interconnect_data: Interconnect configuration data |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 48 | */ |
| 49 | struct ipa_clock { |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 50 | refcount_t count; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 51 | struct mutex mutex; /* protects clock enable/disable */ |
| 52 | struct clk *core; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 53 | struct ipa_interconnect *interconnect[IPA_INTERCONNECT_COUNT]; |
Alex Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 54 | const struct ipa_interconnect_data *interconnect_data; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static struct icc_path * |
| 58 | ipa_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 Wenhu | 0715396 | 2020-05-24 23:29:51 -0700 | [diff] [blame] | 64 | dev_err(dev, "error %ld getting %s interconnect\n", |
| 65 | PTR_ERR(path), name); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 66 | |
| 67 | return path; |
| 68 | } |
| 69 | |
| 70 | /* Initialize interconnects required for IPA operation */ |
| 71 | static 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 Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 78 | clock->interconnect[IPA_INTERCONNECT_MEMORY]->path = path; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 79 | |
| 80 | path = ipa_interconnect_init_one(dev, "imem"); |
| 81 | if (IS_ERR(path)) |
| 82 | goto err_memory_path_put; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 83 | clock->interconnect[IPA_INTERCONNECT_IMEM]->path = path; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 84 | |
| 85 | path = ipa_interconnect_init_one(dev, "config"); |
| 86 | if (IS_ERR(path)) |
| 87 | goto err_imem_path_put; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 88 | clock->interconnect[IPA_INTERCONNECT_CONFIG]->path = path; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | |
| 92 | err_imem_path_put: |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 93 | icc_put(clock->interconnect[IPA_INTERCONNECT_IMEM]->path); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 94 | err_memory_path_put: |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 95 | icc_put(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 96 | err_return: |
| 97 | return PTR_ERR(path); |
| 98 | } |
| 99 | |
| 100 | /* Inverse of ipa_interconnect_init() */ |
| 101 | static void ipa_interconnect_exit(struct ipa_clock *clock) |
| 102 | { |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 103 | 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 Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* Currently we only use one bandwidth level, so just "enable" interconnects */ |
| 109 | static int ipa_interconnect_enable(struct ipa *ipa) |
| 110 | { |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 111 | const struct ipa_interconnect_data *data; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 112 | struct ipa_clock *clock = ipa->clock; |
| 113 | int ret; |
| 114 | |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 115 | data = &clock->interconnect_data[IPA_INTERCONNECT_MEMORY]; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 116 | ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path, |
| 117 | data->average_bandwidth, data->peak_bandwidth); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 118 | if (ret) |
| 119 | return ret; |
| 120 | |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 121 | data = &clock->interconnect_data[IPA_INTERCONNECT_IMEM]; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 122 | ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_IMEM]->path, |
| 123 | data->average_bandwidth, data->peak_bandwidth); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 124 | if (ret) |
| 125 | goto err_memory_path_disable; |
| 126 | |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 127 | data = &clock->interconnect_data[IPA_INTERCONNECT_CONFIG]; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 128 | ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_CONFIG]->path, |
| 129 | data->average_bandwidth, data->peak_bandwidth); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 130 | if (ret) |
| 131 | goto err_imem_path_disable; |
| 132 | |
| 133 | return 0; |
| 134 | |
| 135 | err_imem_path_disable: |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 136 | (void)icc_set_bw(clock->interconnect[IPA_INTERCONNECT_IMEM]->path, |
| 137 | 0, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 138 | err_memory_path_disable: |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 139 | (void)icc_set_bw(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path, |
| 140 | 0, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | /* To disable an interconnect, we just its bandwidth to 0 */ |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 146 | static void ipa_interconnect_disable(struct ipa *ipa) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 147 | { |
| 148 | struct ipa_clock *clock = ipa->clock; |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 149 | int result = 0; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 150 | int ret; |
| 151 | |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 152 | ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_MEMORY]->path, |
| 153 | 0, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 154 | if (ret) |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 155 | result = ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 156 | |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 157 | ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_IMEM]->path, |
| 158 | 0, 0); |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 159 | if (ret && !result) |
| 160 | result = ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 161 | |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame^] | 162 | ret = icc_set_bw(clock->interconnect[IPA_INTERCONNECT_CONFIG]->path, |
| 163 | 0, 0); |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 164 | if (ret && !result) |
| 165 | result = ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 166 | |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 167 | if (result) |
| 168 | dev_err(&ipa->pdev->dev, |
| 169 | "error %d disabling IPA interconnects\n", ret); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Turn on IPA clocks, including interconnects */ |
| 173 | static 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() */ |
| 189 | static void ipa_clock_disable(struct ipa *ipa) |
| 190 | { |
| 191 | clk_disable_unprepare(ipa->clock->core); |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 192 | ipa_interconnect_disable(ipa); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 193 | } |
| 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 | */ |
| 199 | bool ipa_clock_get_additional(struct ipa *ipa) |
| 200 | { |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 201 | return refcount_inc_not_zero(&ipa->clock->count); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 202 | } |
| 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 Elder | dc6e607 | 2020-09-17 12:39:22 -0500 | [diff] [blame] | 206 | * under protection of the mutex, and if appropriate the IPA clock |
| 207 | * is enabled. |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 208 | * |
| 209 | * Incrementing the reference count is intentionally deferred until |
| 210 | * after the clock is running and endpoints are resumed. |
| 211 | */ |
| 212 | void 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 Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 234 | refcount_set(&clock->count, 1); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 235 | |
| 236 | out_mutex_unlock: |
| 237 | mutex_unlock(&clock->mutex); |
| 238 | } |
| 239 | |
Alex Elder | dc6e607 | 2020-09-17 12:39:22 -0500 | [diff] [blame] | 240 | /* Attempt to remove an IPA clock reference. If this represents the |
| 241 | * last reference, disable the IPA clock under protection of the mutex. |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 242 | */ |
| 243 | void 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 Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 248 | if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex)) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 249 | return; |
| 250 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 251 | ipa_clock_disable(ipa); |
| 252 | |
| 253 | mutex_unlock(&clock->mutex); |
| 254 | } |
| 255 | |
Alex Elder | 78b348f | 2020-07-03 16:23:34 -0500 | [diff] [blame] | 256 | /* Return the current IPA core clock rate */ |
| 257 | u32 ipa_clock_rate(struct ipa *ipa) |
| 258 | { |
| 259 | return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0; |
| 260 | } |
| 261 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 262 | /* Initialize IPA clocking */ |
Alex Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 263 | struct ipa_clock * |
| 264 | ipa_clock_init(struct device *dev, const struct ipa_clock_data *data) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 265 | { |
| 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 Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 276 | ret = clk_set_rate(clk, data->core_clock_rate); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 277 | if (ret) { |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 278 | dev_err(dev, "error %d setting core clock rate to %u\n", |
| 279 | ret, data->core_clock_rate); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 280 | 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 Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 289 | clock->interconnect_data = data->interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 290 | |
| 291 | ret = ipa_interconnect_init(clock, dev); |
| 292 | if (ret) |
| 293 | goto err_kfree; |
| 294 | |
| 295 | mutex_init(&clock->mutex); |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 296 | refcount_set(&clock->count, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 297 | |
| 298 | return clock; |
| 299 | |
| 300 | err_kfree: |
| 301 | kfree(clock); |
| 302 | err_clk_put: |
| 303 | clk_put(clk); |
| 304 | |
| 305 | return ERR_PTR(ret); |
| 306 | } |
| 307 | |
| 308 | /* Inverse of ipa_clock_init() */ |
| 309 | void ipa_clock_exit(struct ipa_clock *clock) |
| 310 | { |
| 311 | struct clk *clk = clock->core; |
| 312 | |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 313 | WARN_ON(refcount_read(&clock->count) != 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 314 | mutex_destroy(&clock->mutex); |
| 315 | ipa_interconnect_exit(clock); |
| 316 | kfree(clock); |
| 317 | clk_put(clk); |
| 318 | } |