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 |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 36 | * @average_bandwidth: Average interconnect bandwidth (KB/second) |
| 37 | * @peak_bandwidth: Peak interconnect bandwidth (KB/second) |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame] | 38 | */ |
| 39 | struct ipa_interconnect { |
| 40 | struct icc_path *path; |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 41 | u32 average_bandwidth; |
| 42 | u32 peak_bandwidth; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /** |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 46 | * struct ipa_clock - IPA clocking information |
| 47 | * @count: Clocking reference count |
Alex Elder | e3eea08 | 2020-07-13 07:24:18 -0500 | [diff] [blame] | 48 | * @mutex: Protects clock enable/disable |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 49 | * @core: IPA core clock |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 50 | * @interconnect_count: Number of elements in interconnect[] |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame] | 51 | * @interconnect: Interconnect array |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 52 | */ |
| 53 | struct ipa_clock { |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 54 | refcount_t count; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 55 | struct mutex mutex; /* protects clock enable/disable */ |
| 56 | struct clk *core; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 57 | u32 interconnect_count; |
| 58 | struct ipa_interconnect *interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 59 | }; |
| 60 | |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 61 | static int ipa_interconnect_init_one(struct device *dev, |
| 62 | struct ipa_interconnect *interconnect, |
| 63 | const struct ipa_interconnect_data *data) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 64 | { |
| 65 | struct icc_path *path; |
| 66 | |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 67 | path = of_icc_get(dev, data->name); |
| 68 | if (IS_ERR(path)) { |
| 69 | int ret = PTR_ERR(path); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 70 | |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 71 | 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 | |
| 84 | static void ipa_interconnect_exit_one(struct ipa_interconnect *interconnect) |
| 85 | { |
| 86 | icc_put(interconnect->path); |
| 87 | memset(interconnect, 0, sizeof(*interconnect)); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* Initialize interconnects required for IPA operation */ |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 91 | static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev, |
| 92 | const struct ipa_interconnect_data *data) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 93 | { |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 94 | struct ipa_interconnect *interconnect; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 95 | u32 count; |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 96 | int ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 97 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 98 | count = clock->interconnect_count; |
| 99 | interconnect = kcalloc(count, sizeof(*interconnect), GFP_KERNEL); |
| 100 | if (!interconnect) |
| 101 | return -ENOMEM; |
| 102 | clock->interconnect = interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 103 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 104 | while (count--) { |
| 105 | ret = ipa_interconnect_init_one(dev, interconnect, data++); |
| 106 | if (ret) |
| 107 | goto out_unwind; |
| 108 | interconnect++; |
| 109 | } |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 113 | out_unwind: |
| 114 | while (interconnect-- > clock->interconnect) |
| 115 | ipa_interconnect_exit_one(interconnect); |
| 116 | kfree(clock->interconnect); |
| 117 | clock->interconnect = NULL; |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 118 | |
| 119 | return ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Inverse of ipa_interconnect_init() */ |
| 123 | static void ipa_interconnect_exit(struct ipa_clock *clock) |
| 124 | { |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 125 | struct ipa_interconnect *interconnect; |
| 126 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 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; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Currently we only use one bandwidth level, so just "enable" interconnects */ |
| 135 | static int ipa_interconnect_enable(struct ipa *ipa) |
| 136 | { |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 137 | struct ipa_interconnect *interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 138 | struct ipa_clock *clock = ipa->clock; |
| 139 | int ret; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 140 | u32 i; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 141 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 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); |
| 147 | if (ret) |
| 148 | goto out_unwind; |
| 149 | interconnect++; |
| 150 | } |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 151 | |
| 152 | return 0; |
| 153 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 154 | out_unwind: |
| 155 | while (interconnect-- > clock->interconnect) |
| 156 | (void)icc_set_bw(interconnect->path, 0, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* To disable an interconnect, we just its bandwidth to 0 */ |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 162 | static void ipa_interconnect_disable(struct ipa *ipa) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 163 | { |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 164 | struct ipa_interconnect *interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 165 | struct ipa_clock *clock = ipa->clock; |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 166 | int result = 0; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 167 | u32 count; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 168 | int ret; |
| 169 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 170 | 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 Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 178 | |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 179 | if (result) |
| 180 | dev_err(&ipa->pdev->dev, |
| 181 | "error %d disabling IPA interconnects\n", ret); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* Turn on IPA clocks, including interconnects */ |
| 185 | static 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() */ |
| 201 | static void ipa_clock_disable(struct ipa *ipa) |
| 202 | { |
| 203 | clk_disable_unprepare(ipa->clock->core); |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 204 | ipa_interconnect_disable(ipa); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 205 | } |
| 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 | */ |
| 211 | bool ipa_clock_get_additional(struct ipa *ipa) |
| 212 | { |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 213 | return refcount_inc_not_zero(&ipa->clock->count); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 214 | } |
| 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 Elder | dc6e607 | 2020-09-17 12:39:22 -0500 | [diff] [blame] | 218 | * under protection of the mutex, and if appropriate the IPA clock |
| 219 | * is enabled. |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 220 | * |
| 221 | * Incrementing the reference count is intentionally deferred until |
| 222 | * after the clock is running and endpoints are resumed. |
| 223 | */ |
| 224 | void 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 Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 246 | refcount_set(&clock->count, 1); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 247 | |
| 248 | out_mutex_unlock: |
| 249 | mutex_unlock(&clock->mutex); |
| 250 | } |
| 251 | |
Alex Elder | dc6e607 | 2020-09-17 12:39:22 -0500 | [diff] [blame] | 252 | /* Attempt to remove an IPA clock reference. If this represents the |
| 253 | * last reference, disable the IPA clock under protection of the mutex. |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 254 | */ |
| 255 | void 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 Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 260 | if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex)) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 261 | return; |
| 262 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 263 | ipa_clock_disable(ipa); |
| 264 | |
| 265 | mutex_unlock(&clock->mutex); |
| 266 | } |
| 267 | |
Alex Elder | 78b348f | 2020-07-03 16:23:34 -0500 | [diff] [blame] | 268 | /* Return the current IPA core clock rate */ |
| 269 | u32 ipa_clock_rate(struct ipa *ipa) |
| 270 | { |
| 271 | return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0; |
| 272 | } |
| 273 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 274 | /* Initialize IPA clocking */ |
Alex Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 275 | struct ipa_clock * |
| 276 | ipa_clock_init(struct device *dev, const struct ipa_clock_data *data) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 277 | { |
| 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 Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 288 | ret = clk_set_rate(clk, data->core_clock_rate); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 289 | if (ret) { |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 290 | dev_err(dev, "error %d setting core clock rate to %u\n", |
| 291 | ret, data->core_clock_rate); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 292 | 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 Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 301 | clock->interconnect_count = data->interconnect_count; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 302 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame^] | 303 | ret = ipa_interconnect_init(clock, dev, data->interconnect_data); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 304 | if (ret) |
| 305 | goto err_kfree; |
| 306 | |
| 307 | mutex_init(&clock->mutex); |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 308 | refcount_set(&clock->count, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 309 | |
| 310 | return clock; |
| 311 | |
| 312 | err_kfree: |
| 313 | kfree(clock); |
| 314 | err_clk_put: |
| 315 | clk_put(clk); |
| 316 | |
| 317 | return ERR_PTR(ret); |
| 318 | } |
| 319 | |
| 320 | /* Inverse of ipa_clock_init() */ |
| 321 | void ipa_clock_exit(struct ipa_clock *clock) |
| 322 | { |
| 323 | struct clk *clk = clock->core; |
| 324 | |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 325 | WARN_ON(refcount_read(&clock->count) != 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 326 | mutex_destroy(&clock->mutex); |
| 327 | ipa_interconnect_exit(clock); |
| 328 | kfree(clock); |
| 329 | clk_put(clk); |
| 330 | } |