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. |
Alex Elder | 4c7ccfc | 2021-02-12 08:33:59 -0600 | [diff] [blame] | 4 | * Copyright (C) 2018-2021 Linaro Ltd. |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 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> |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 12 | #include <linux/pm.h> |
Alex Elder | 2abb0c7 | 2021-08-10 14:27:00 -0500 | [diff] [blame^] | 13 | #include <linux/pm_runtime.h> |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 15 | |
| 16 | #include "ipa.h" |
| 17 | #include "ipa_clock.h" |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 18 | #include "ipa_endpoint.h" |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 19 | #include "ipa_modem.h" |
Alex Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 20 | #include "ipa_data.h" |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * DOC: IPA Clocking |
| 24 | * |
| 25 | * The "IPA Clock" manages both the IPA core clock and the interconnects |
| 26 | * (buses) the IPA depends on as a single logical entity. A reference count |
| 27 | * is incremented by "get" operations and decremented by "put" operations. |
| 28 | * Transitions of that count from 0 to 1 result in the clock and interconnects |
| 29 | * being enabled, and transitions of the count from 1 to 0 cause them to be |
| 30 | * disabled. We currently operate the core clock at a fixed clock rate, and |
| 31 | * all buses at a fixed average and peak bandwidth. As more advanced IPA |
| 32 | * features are enabled, we can make better use of clock and bus scaling. |
| 33 | * |
| 34 | * An IPA clock reference must be held for any access to IPA hardware. |
| 35 | */ |
| 36 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 37 | /** |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame] | 38 | * struct ipa_interconnect - IPA interconnect information |
| 39 | * @path: Interconnect path |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 40 | * @average_bandwidth: Average interconnect bandwidth (KB/second) |
| 41 | * @peak_bandwidth: Peak interconnect bandwidth (KB/second) |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame] | 42 | */ |
| 43 | struct ipa_interconnect { |
| 44 | struct icc_path *path; |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 45 | u32 average_bandwidth; |
| 46 | u32 peak_bandwidth; |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /** |
Alex Elder | afb08b7 | 2021-08-04 10:36:26 -0500 | [diff] [blame] | 50 | * enum ipa_power_flag - IPA power flags |
| 51 | * @IPA_POWER_FLAG_RESUMED: Whether resume from suspend has been signaled |
| 52 | * @IPA_POWER_FLAG_COUNT: Number of defined power flags |
| 53 | */ |
| 54 | enum ipa_power_flag { |
| 55 | IPA_POWER_FLAG_RESUMED, |
| 56 | IPA_POWER_FLAG_COUNT, /* Last; not a flag */ |
| 57 | }; |
| 58 | |
| 59 | /** |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 60 | * struct ipa_clock - IPA clocking information |
| 61 | * @count: Clocking reference count |
Alex Elder | e3eea08 | 2020-07-13 07:24:18 -0500 | [diff] [blame] | 62 | * @mutex: Protects clock enable/disable |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 63 | * @core: IPA core clock |
Alex Elder | afb08b7 | 2021-08-04 10:36:26 -0500 | [diff] [blame] | 64 | * @flags: Boolean state flags |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 65 | * @interconnect_count: Number of elements in interconnect[] |
Alex Elder | 5b40810 | 2021-01-15 06:50:46 -0600 | [diff] [blame] | 66 | * @interconnect: Interconnect array |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 67 | */ |
| 68 | struct ipa_clock { |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 69 | refcount_t count; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 70 | struct mutex mutex; /* protects clock enable/disable */ |
| 71 | struct clk *core; |
Alex Elder | afb08b7 | 2021-08-04 10:36:26 -0500 | [diff] [blame] | 72 | DECLARE_BITMAP(flags, IPA_POWER_FLAG_COUNT); |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 73 | u32 interconnect_count; |
| 74 | struct ipa_interconnect *interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 75 | }; |
| 76 | |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 77 | static int ipa_interconnect_init_one(struct device *dev, |
| 78 | struct ipa_interconnect *interconnect, |
| 79 | const struct ipa_interconnect_data *data) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 80 | { |
| 81 | struct icc_path *path; |
| 82 | |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 83 | path = of_icc_get(dev, data->name); |
| 84 | if (IS_ERR(path)) { |
| 85 | int ret = PTR_ERR(path); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 86 | |
Alex Elder | 4c7ccfc | 2021-02-12 08:33:59 -0600 | [diff] [blame] | 87 | dev_err_probe(dev, ret, "error getting %s interconnect\n", |
| 88 | data->name); |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | interconnect->path = path; |
| 94 | interconnect->average_bandwidth = data->average_bandwidth; |
| 95 | interconnect->peak_bandwidth = data->peak_bandwidth; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static void ipa_interconnect_exit_one(struct ipa_interconnect *interconnect) |
| 101 | { |
| 102 | icc_put(interconnect->path); |
| 103 | memset(interconnect, 0, sizeof(*interconnect)); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* Initialize interconnects required for IPA operation */ |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 107 | static int ipa_interconnect_init(struct ipa_clock *clock, struct device *dev, |
| 108 | const struct ipa_interconnect_data *data) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 109 | { |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 110 | struct ipa_interconnect *interconnect; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 111 | u32 count; |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 112 | int ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 113 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 114 | count = clock->interconnect_count; |
| 115 | interconnect = kcalloc(count, sizeof(*interconnect), GFP_KERNEL); |
| 116 | if (!interconnect) |
| 117 | return -ENOMEM; |
| 118 | clock->interconnect = interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 119 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 120 | while (count--) { |
| 121 | ret = ipa_interconnect_init_one(dev, interconnect, data++); |
| 122 | if (ret) |
| 123 | goto out_unwind; |
| 124 | interconnect++; |
| 125 | } |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 129 | out_unwind: |
| 130 | while (interconnect-- > clock->interconnect) |
| 131 | ipa_interconnect_exit_one(interconnect); |
| 132 | kfree(clock->interconnect); |
| 133 | clock->interconnect = NULL; |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 134 | |
| 135 | return ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* Inverse of ipa_interconnect_init() */ |
| 139 | static void ipa_interconnect_exit(struct ipa_clock *clock) |
| 140 | { |
Alex Elder | 10d0d39 | 2021-01-15 06:50:49 -0600 | [diff] [blame] | 141 | struct ipa_interconnect *interconnect; |
| 142 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 143 | interconnect = clock->interconnect + clock->interconnect_count; |
| 144 | while (interconnect-- > clock->interconnect) |
| 145 | ipa_interconnect_exit_one(interconnect); |
| 146 | kfree(clock->interconnect); |
| 147 | clock->interconnect = NULL; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* Currently we only use one bandwidth level, so just "enable" interconnects */ |
| 151 | static int ipa_interconnect_enable(struct ipa *ipa) |
| 152 | { |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 153 | struct ipa_interconnect *interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 154 | struct ipa_clock *clock = ipa->clock; |
| 155 | int ret; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 156 | u32 i; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 157 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 158 | interconnect = clock->interconnect; |
| 159 | for (i = 0; i < clock->interconnect_count; i++) { |
| 160 | ret = icc_set_bw(interconnect->path, |
| 161 | interconnect->average_bandwidth, |
| 162 | interconnect->peak_bandwidth); |
Alex Elder | 8ee7c40 | 2021-08-04 10:36:23 -0500 | [diff] [blame] | 163 | if (ret) { |
| 164 | dev_err(&ipa->pdev->dev, |
| 165 | "error %d enabling %s interconnect\n", |
| 166 | ret, icc_get_name(interconnect->path)); |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 167 | goto out_unwind; |
Alex Elder | 8ee7c40 | 2021-08-04 10:36:23 -0500 | [diff] [blame] | 168 | } |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 169 | interconnect++; |
| 170 | } |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 174 | out_unwind: |
| 175 | while (interconnect-- > clock->interconnect) |
| 176 | (void)icc_set_bw(interconnect->path, 0, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | /* To disable an interconnect, we just its bandwidth to 0 */ |
Alex Elder | 8ee7c40 | 2021-08-04 10:36:23 -0500 | [diff] [blame] | 182 | static int ipa_interconnect_disable(struct ipa *ipa) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 183 | { |
Alex Elder | db6cd51 | 2021-01-15 06:50:47 -0600 | [diff] [blame] | 184 | struct ipa_interconnect *interconnect; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 185 | struct ipa_clock *clock = ipa->clock; |
Alex Elder | 8ee7c40 | 2021-08-04 10:36:23 -0500 | [diff] [blame] | 186 | struct device *dev = &ipa->pdev->dev; |
Alex Elder | ec0ef6d | 2021-01-15 06:50:45 -0600 | [diff] [blame] | 187 | int result = 0; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 188 | u32 count; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 189 | int ret; |
| 190 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 191 | count = clock->interconnect_count; |
| 192 | interconnect = clock->interconnect + count; |
| 193 | while (count--) { |
| 194 | interconnect--; |
| 195 | ret = icc_set_bw(interconnect->path, 0, 0); |
Alex Elder | 8ee7c40 | 2021-08-04 10:36:23 -0500 | [diff] [blame] | 196 | if (ret) { |
| 197 | dev_err(dev, "error %d disabling %s interconnect\n", |
| 198 | ret, icc_get_name(interconnect->path)); |
| 199 | /* Try to disable all; record only the first error */ |
| 200 | if (!result) |
| 201 | result = ret; |
| 202 | } |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 203 | } |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 204 | |
Alex Elder | 8ee7c40 | 2021-08-04 10:36:23 -0500 | [diff] [blame] | 205 | return result; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* Turn on IPA clocks, including interconnects */ |
| 209 | static int ipa_clock_enable(struct ipa *ipa) |
| 210 | { |
| 211 | int ret; |
| 212 | |
| 213 | ret = ipa_interconnect_enable(ipa); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | ret = clk_prepare_enable(ipa->clock->core); |
Alex Elder | 8ee7c40 | 2021-08-04 10:36:23 -0500 | [diff] [blame] | 218 | if (ret) { |
| 219 | dev_err(&ipa->pdev->dev, "error %d enabling core clock\n", ret); |
| 220 | (void)ipa_interconnect_disable(ipa); |
| 221 | } |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | /* Inverse of ipa_clock_enable() */ |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 227 | static int ipa_clock_disable(struct ipa *ipa) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 228 | { |
| 229 | clk_disable_unprepare(ipa->clock->core); |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 230 | |
| 231 | return ipa_interconnect_disable(ipa); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 232 | } |
| 233 | |
Alex Elder | 2abb0c7 | 2021-08-10 14:27:00 -0500 | [diff] [blame^] | 234 | static int ipa_runtime_suspend(struct device *dev) |
| 235 | { |
| 236 | struct ipa *ipa = dev_get_drvdata(dev); |
| 237 | |
| 238 | /* Endpoints aren't usable until setup is complete */ |
| 239 | if (ipa->setup_complete) { |
| 240 | __clear_bit(IPA_POWER_FLAG_RESUMED, ipa->clock->flags); |
| 241 | ipa_endpoint_suspend(ipa); |
| 242 | gsi_suspend(&ipa->gsi); |
| 243 | } |
| 244 | |
| 245 | return ipa_clock_disable(ipa); |
| 246 | } |
| 247 | |
| 248 | static int ipa_runtime_resume(struct device *dev) |
| 249 | { |
| 250 | struct ipa *ipa = dev_get_drvdata(dev); |
| 251 | int ret; |
| 252 | |
| 253 | ret = ipa_clock_enable(ipa); |
| 254 | if (WARN_ON(ret < 0)) |
| 255 | return ret; |
| 256 | |
| 257 | /* Endpoints aren't usable until setup is complete */ |
| 258 | if (ipa->setup_complete) { |
| 259 | gsi_resume(&ipa->gsi); |
| 260 | ipa_endpoint_resume(ipa); |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 266 | /* Get an IPA clock reference, but only if the reference count is |
| 267 | * already non-zero. Returns true if the additional reference was |
| 268 | * added successfully, or false otherwise. |
| 269 | */ |
| 270 | bool ipa_clock_get_additional(struct ipa *ipa) |
| 271 | { |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 272 | return refcount_inc_not_zero(&ipa->clock->count); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Get an IPA clock reference. If the reference count is non-zero, it is |
| 276 | * incremented and return is immediate. Otherwise it is checked again |
Alex Elder | dc6e607 | 2020-09-17 12:39:22 -0500 | [diff] [blame] | 277 | * under protection of the mutex, and if appropriate the IPA clock |
| 278 | * is enabled. |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 279 | * |
| 280 | * Incrementing the reference count is intentionally deferred until |
| 281 | * after the clock is running and endpoints are resumed. |
| 282 | */ |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 283 | int ipa_clock_get(struct ipa *ipa) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 284 | { |
| 285 | struct ipa_clock *clock = ipa->clock; |
| 286 | int ret; |
| 287 | |
| 288 | /* If the clock is running, just bump the reference count */ |
| 289 | if (ipa_clock_get_additional(ipa)) |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 290 | return 1; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 291 | |
| 292 | /* Otherwise get the mutex and check again */ |
| 293 | mutex_lock(&clock->mutex); |
| 294 | |
| 295 | /* A reference might have been added before we got the mutex. */ |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 296 | if (ipa_clock_get_additional(ipa)) { |
| 297 | ret = 1; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 298 | goto out_mutex_unlock; |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 299 | } |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 300 | |
Alex Elder | 2abb0c7 | 2021-08-10 14:27:00 -0500 | [diff] [blame^] | 301 | ret = ipa_runtime_resume(&ipa->pdev->dev); |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 302 | |
| 303 | refcount_set(&clock->count, 1); |
| 304 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 305 | out_mutex_unlock: |
| 306 | mutex_unlock(&clock->mutex); |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 307 | |
| 308 | return ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 309 | } |
| 310 | |
Alex Elder | dc6e607 | 2020-09-17 12:39:22 -0500 | [diff] [blame] | 311 | /* Attempt to remove an IPA clock reference. If this represents the |
| 312 | * last reference, disable the IPA clock under protection of the mutex. |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 313 | */ |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 314 | int ipa_clock_put(struct ipa *ipa) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 315 | { |
| 316 | struct ipa_clock *clock = ipa->clock; |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 317 | int ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 318 | |
| 319 | /* 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] | 320 | if (!refcount_dec_and_mutex_lock(&clock->count, &clock->mutex)) |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 321 | return 0; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 322 | |
Alex Elder | 2abb0c7 | 2021-08-10 14:27:00 -0500 | [diff] [blame^] | 323 | ret = ipa_runtime_suspend(&ipa->pdev->dev); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 324 | |
| 325 | mutex_unlock(&clock->mutex); |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 326 | |
| 327 | return ret; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 328 | } |
| 329 | |
Alex Elder | 78b348f | 2020-07-03 16:23:34 -0500 | [diff] [blame] | 330 | /* Return the current IPA core clock rate */ |
| 331 | u32 ipa_clock_rate(struct ipa *ipa) |
| 332 | { |
| 333 | return ipa->clock ? (u32)clk_get_rate(ipa->clock->core) : 0; |
| 334 | } |
| 335 | |
Alex Elder | afe1baa | 2021-08-04 10:36:25 -0500 | [diff] [blame] | 336 | /** |
| 337 | * ipa_suspend_handler() - Handle the suspend IPA interrupt |
| 338 | * @ipa: IPA pointer |
| 339 | * @irq_id: IPA interrupt type (unused) |
| 340 | * |
| 341 | * If an RX endpoint is suspended, and the IPA has a packet destined for |
| 342 | * that endpoint, the IPA generates a SUSPEND interrupt to inform the AP |
| 343 | * that it should resume the endpoint. If we get one of these interrupts |
| 344 | * we just wake up the system. |
| 345 | */ |
| 346 | static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id) |
| 347 | { |
| 348 | /* Just report the event, and let system resume handle the rest. |
| 349 | * More than one endpoint could signal this; if so, ignore |
| 350 | * all but the first. |
| 351 | */ |
Alex Elder | afb08b7 | 2021-08-04 10:36:26 -0500 | [diff] [blame] | 352 | if (!test_and_set_bit(IPA_POWER_FLAG_RESUMED, ipa->clock->flags)) |
Alex Elder | afe1baa | 2021-08-04 10:36:25 -0500 | [diff] [blame] | 353 | pm_wakeup_dev_event(&ipa->pdev->dev, 0, true); |
| 354 | |
| 355 | /* Acknowledge/clear the suspend interrupt on all endpoints */ |
| 356 | ipa_interrupt_suspend_clear_all(ipa->interrupt); |
| 357 | } |
| 358 | |
| 359 | void ipa_power_setup(struct ipa *ipa) |
| 360 | { |
| 361 | ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND, |
| 362 | ipa_suspend_handler); |
| 363 | } |
| 364 | |
| 365 | void ipa_power_teardown(struct ipa *ipa) |
| 366 | { |
| 367 | ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND); |
| 368 | } |
| 369 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 370 | /* Initialize IPA clocking */ |
Alex Elder | dfccb8b | 2020-11-19 16:40:39 -0600 | [diff] [blame] | 371 | struct ipa_clock * |
| 372 | ipa_clock_init(struct device *dev, const struct ipa_clock_data *data) |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 373 | { |
| 374 | struct ipa_clock *clock; |
| 375 | struct clk *clk; |
| 376 | int ret; |
| 377 | |
| 378 | clk = clk_get(dev, "core"); |
| 379 | if (IS_ERR(clk)) { |
Alex Elder | 4c7ccfc | 2021-02-12 08:33:59 -0600 | [diff] [blame] | 380 | dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n"); |
| 381 | |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 382 | return ERR_CAST(clk); |
| 383 | } |
| 384 | |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 385 | ret = clk_set_rate(clk, data->core_clock_rate); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 386 | if (ret) { |
Alex Elder | 91d02f9 | 2020-11-19 16:40:41 -0600 | [diff] [blame] | 387 | dev_err(dev, "error %d setting core clock rate to %u\n", |
| 388 | ret, data->core_clock_rate); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 389 | goto err_clk_put; |
| 390 | } |
| 391 | |
| 392 | clock = kzalloc(sizeof(*clock), GFP_KERNEL); |
| 393 | if (!clock) { |
| 394 | ret = -ENOMEM; |
| 395 | goto err_clk_put; |
| 396 | } |
| 397 | clock->core = clk; |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 398 | clock->interconnect_count = data->interconnect_count; |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 399 | |
Alex Elder | ea151e1 | 2021-01-15 06:50:50 -0600 | [diff] [blame] | 400 | ret = ipa_interconnect_init(clock, dev, data->interconnect_data); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 401 | if (ret) |
| 402 | goto err_kfree; |
| 403 | |
| 404 | mutex_init(&clock->mutex); |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 405 | refcount_set(&clock->count, 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 406 | |
| 407 | return clock; |
| 408 | |
| 409 | err_kfree: |
| 410 | kfree(clock); |
| 411 | err_clk_put: |
| 412 | clk_put(clk); |
| 413 | |
| 414 | return ERR_PTR(ret); |
| 415 | } |
| 416 | |
| 417 | /* Inverse of ipa_clock_init() */ |
| 418 | void ipa_clock_exit(struct ipa_clock *clock) |
| 419 | { |
| 420 | struct clk *clk = clock->core; |
| 421 | |
Alex Elder | 0305b70 | 2020-09-17 12:39:20 -0500 | [diff] [blame] | 422 | WARN_ON(refcount_read(&clock->count) != 0); |
Alex Elder | ba764c4 | 2020-03-05 22:28:19 -0600 | [diff] [blame] | 423 | mutex_destroy(&clock->mutex); |
| 424 | ipa_interconnect_exit(clock); |
| 425 | kfree(clock); |
| 426 | clk_put(clk); |
| 427 | } |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 428 | |
| 429 | /** |
| 430 | * ipa_suspend() - Power management system suspend callback |
| 431 | * @dev: IPA device structure |
| 432 | * |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 433 | * Return: 0 on success, or a negative error code |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 434 | * |
| 435 | * Called by the PM framework when a system suspend operation is invoked. |
| 436 | * Suspends endpoints and releases the clock reference held to keep |
| 437 | * the IPA clock running until this point. |
| 438 | */ |
| 439 | static int ipa_suspend(struct device *dev) |
| 440 | { |
Alex Elder | 2abb0c7 | 2021-08-10 14:27:00 -0500 | [diff] [blame^] | 441 | return ipa_runtime_suspend(dev); |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
| 445 | * ipa_resume() - Power management system resume callback |
| 446 | * @dev: IPA device structure |
| 447 | * |
Alex Elder | 7ebd168 | 2021-08-10 14:26:58 -0500 | [diff] [blame] | 448 | * Return: 0 on success, or a negative error code |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 449 | * |
| 450 | * Called by the PM framework when a system resume operation is invoked. |
| 451 | * Takes an IPA clock reference to keep the clock running until suspend, |
| 452 | * and resumes endpoints. |
| 453 | */ |
| 454 | static int ipa_resume(struct device *dev) |
| 455 | { |
Alex Elder | 2abb0c7 | 2021-08-10 14:27:00 -0500 | [diff] [blame^] | 456 | return ipa_runtime_resume(dev); |
Alex Elder | 73ff316 | 2021-08-04 10:36:24 -0500 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | const struct dev_pm_ops ipa_pm_ops = { |
| 460 | .suspend = ipa_suspend, |
| 461 | .resume = ipa_resume, |
| 462 | }; |