Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/atomic.h> |
| 7 | #include <linux/bug.h> |
| 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/jiffies.h> |
| 10 | #include <linux/kernel.h> |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 11 | #include <linux/list.h> |
Maulik Shah | 985427f | 2020-04-12 20:20:02 +0530 | [diff] [blame] | 12 | #include <linux/lockdep.h> |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/slab.h> |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/wait.h> |
| 20 | |
| 21 | #include <soc/qcom/rpmh.h> |
| 22 | |
| 23 | #include "rpmh-internal.h" |
| 24 | |
| 25 | #define RPMH_TIMEOUT_MS msecs_to_jiffies(10000) |
| 26 | |
Maulik Shah | aff9cc0 | 2020-02-03 19:05:34 +0530 | [diff] [blame] | 27 | #define DEFINE_RPMH_MSG_ONSTACK(device, s, q, name) \ |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 28 | struct rpmh_request name = { \ |
| 29 | .msg = { \ |
| 30 | .state = s, \ |
| 31 | .cmds = name.cmd, \ |
| 32 | .num_cmds = 0, \ |
| 33 | .wait_for_compl = true, \ |
| 34 | }, \ |
| 35 | .cmd = { { 0 } }, \ |
| 36 | .completion = q, \ |
Maulik Shah | aff9cc0 | 2020-02-03 19:05:34 +0530 | [diff] [blame] | 37 | .dev = device, \ |
Lina Iyer | 564b5e2 | 2018-06-20 18:57:05 +0530 | [diff] [blame] | 38 | .needs_free = false, \ |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | #define ctrlr_to_drv(ctrlr) container_of(ctrlr, struct rsc_drv, client) |
| 42 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 43 | /** |
| 44 | * struct cache_req: the request object for caching |
| 45 | * |
| 46 | * @addr: the address of the resource |
| 47 | * @sleep_val: the sleep vote |
| 48 | * @wake_val: the wake vote |
| 49 | * @list: linked list obj |
| 50 | */ |
| 51 | struct cache_req { |
| 52 | u32 addr; |
| 53 | u32 sleep_val; |
| 54 | u32 wake_val; |
| 55 | struct list_head list; |
| 56 | }; |
| 57 | |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 58 | /** |
| 59 | * struct batch_cache_req - An entry in our batch catch |
| 60 | * |
| 61 | * @list: linked list obj |
| 62 | * @count: number of messages |
| 63 | * @rpm_msgs: the messages |
| 64 | */ |
| 65 | |
| 66 | struct batch_cache_req { |
| 67 | struct list_head list; |
| 68 | int count; |
| 69 | struct rpmh_request rpm_msgs[]; |
| 70 | }; |
| 71 | |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 72 | static struct rpmh_ctrlr *get_rpmh_ctrlr(const struct device *dev) |
| 73 | { |
| 74 | struct rsc_drv *drv = dev_get_drvdata(dev->parent); |
| 75 | |
| 76 | return &drv->client; |
| 77 | } |
| 78 | |
| 79 | void rpmh_tx_done(const struct tcs_request *msg, int r) |
| 80 | { |
| 81 | struct rpmh_request *rpm_msg = container_of(msg, struct rpmh_request, |
| 82 | msg); |
| 83 | struct completion *compl = rpm_msg->completion; |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 84 | bool free = rpm_msg->needs_free; |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 85 | |
| 86 | rpm_msg->err = r; |
| 87 | |
| 88 | if (r) |
| 89 | dev_err(rpm_msg->dev, "RPMH TX fail in msg addr=%#x, err=%d\n", |
| 90 | rpm_msg->msg.cmds[0].addr, r); |
| 91 | |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 92 | if (!compl) |
| 93 | goto exit; |
Lina Iyer | 564b5e2 | 2018-06-20 18:57:05 +0530 | [diff] [blame] | 94 | |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 95 | /* Signal the blocking thread we are done */ |
| 96 | complete(compl); |
| 97 | |
| 98 | exit: |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 99 | if (free) |
Lina Iyer | 564b5e2 | 2018-06-20 18:57:05 +0530 | [diff] [blame] | 100 | kfree(rpm_msg); |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 103 | static struct cache_req *__find_req(struct rpmh_ctrlr *ctrlr, u32 addr) |
| 104 | { |
| 105 | struct cache_req *p, *req = NULL; |
| 106 | |
| 107 | list_for_each_entry(p, &ctrlr->cache, list) { |
| 108 | if (p->addr == addr) { |
| 109 | req = p; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return req; |
| 115 | } |
| 116 | |
| 117 | static struct cache_req *cache_rpm_request(struct rpmh_ctrlr *ctrlr, |
| 118 | enum rpmh_state state, |
| 119 | struct tcs_cmd *cmd) |
| 120 | { |
| 121 | struct cache_req *req; |
| 122 | unsigned long flags; |
Maulik Shah | bb70006 | 2020-04-12 20:20:00 +0530 | [diff] [blame] | 123 | u32 old_sleep_val, old_wake_val; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 124 | |
| 125 | spin_lock_irqsave(&ctrlr->cache_lock, flags); |
| 126 | req = __find_req(ctrlr, cmd->addr); |
| 127 | if (req) |
| 128 | goto existing; |
| 129 | |
| 130 | req = kzalloc(sizeof(*req), GFP_ATOMIC); |
| 131 | if (!req) { |
| 132 | req = ERR_PTR(-ENOMEM); |
| 133 | goto unlock; |
| 134 | } |
| 135 | |
| 136 | req->addr = cmd->addr; |
| 137 | req->sleep_val = req->wake_val = UINT_MAX; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 138 | list_add_tail(&req->list, &ctrlr->cache); |
| 139 | |
| 140 | existing: |
Maulik Shah | bb70006 | 2020-04-12 20:20:00 +0530 | [diff] [blame] | 141 | old_sleep_val = req->sleep_val; |
| 142 | old_wake_val = req->wake_val; |
| 143 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 144 | switch (state) { |
| 145 | case RPMH_ACTIVE_ONLY_STATE: |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 146 | case RPMH_WAKE_ONLY_STATE: |
| 147 | req->wake_val = cmd->data; |
| 148 | break; |
| 149 | case RPMH_SLEEP_STATE: |
| 150 | req->sleep_val = cmd->data; |
| 151 | break; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 152 | } |
| 153 | |
Douglas Anderson | 35bb4b2 | 2020-04-17 14:15:47 -0700 | [diff] [blame] | 154 | ctrlr->dirty |= (req->sleep_val != old_sleep_val || |
| 155 | req->wake_val != old_wake_val) && |
| 156 | req->sleep_val != UINT_MAX && |
| 157 | req->wake_val != UINT_MAX; |
Maulik Shah | bb70006 | 2020-04-12 20:20:00 +0530 | [diff] [blame] | 158 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 159 | unlock: |
| 160 | spin_unlock_irqrestore(&ctrlr->cache_lock, flags); |
| 161 | |
| 162 | return req; |
| 163 | } |
| 164 | |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 165 | /** |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 166 | * __rpmh_write: Cache and send the RPMH request |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 167 | * |
| 168 | * @dev: The device making the request |
| 169 | * @state: Active/Sleep request type |
| 170 | * @rpm_msg: The data that needs to be sent (cmds). |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 171 | * |
| 172 | * Cache the RPMH request and send if the state is ACTIVE_ONLY. |
| 173 | * SLEEP/WAKE_ONLY requests are not sent to the controller at |
| 174 | * this time. Use rpmh_flush() to send them to the controller. |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 175 | */ |
| 176 | static int __rpmh_write(const struct device *dev, enum rpmh_state state, |
| 177 | struct rpmh_request *rpm_msg) |
| 178 | { |
| 179 | struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev); |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 180 | int ret = -EINVAL; |
| 181 | struct cache_req *req; |
| 182 | int i; |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 183 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 184 | /* Cache the request in our store and link the payload */ |
| 185 | for (i = 0; i < rpm_msg->msg.num_cmds; i++) { |
| 186 | req = cache_rpm_request(ctrlr, state, &rpm_msg->msg.cmds[i]); |
| 187 | if (IS_ERR(req)) |
| 188 | return PTR_ERR(req); |
| 189 | } |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 190 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 191 | if (state == RPMH_ACTIVE_ONLY_STATE) { |
| 192 | WARN_ON(irqs_disabled()); |
| 193 | ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msg->msg); |
| 194 | } else { |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 195 | /* Clean up our call by spoofing tx_done */ |
Raju P.L.S.S.S.N | 7300241 | 2018-10-23 22:59:08 +0530 | [diff] [blame] | 196 | ret = 0; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 197 | rpmh_tx_done(&rpm_msg->msg, ret); |
| 198 | } |
| 199 | |
| 200 | return ret; |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 201 | } |
| 202 | |
Lina Iyer | 564b5e2 | 2018-06-20 18:57:05 +0530 | [diff] [blame] | 203 | static int __fill_rpmh_msg(struct rpmh_request *req, enum rpmh_state state, |
| 204 | const struct tcs_cmd *cmd, u32 n) |
| 205 | { |
| 206 | if (!cmd || !n || n > MAX_RPMH_PAYLOAD) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | memcpy(req->cmd, cmd, n * sizeof(*cmd)); |
| 210 | |
| 211 | req->msg.state = state; |
| 212 | req->msg.cmds = req->cmd; |
| 213 | req->msg.num_cmds = n; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * rpmh_write_async: Write a set of RPMH commands |
| 220 | * |
| 221 | * @dev: The device making the request |
| 222 | * @state: Active/sleep set |
| 223 | * @cmd: The payload data |
| 224 | * @n: The number of elements in payload |
| 225 | * |
| 226 | * Write a set of RPMH commands, the order of commands is maintained |
| 227 | * and will be sent as a single shot. |
| 228 | */ |
| 229 | int rpmh_write_async(const struct device *dev, enum rpmh_state state, |
| 230 | const struct tcs_cmd *cmd, u32 n) |
| 231 | { |
| 232 | struct rpmh_request *rpm_msg; |
| 233 | int ret; |
| 234 | |
| 235 | rpm_msg = kzalloc(sizeof(*rpm_msg), GFP_ATOMIC); |
| 236 | if (!rpm_msg) |
| 237 | return -ENOMEM; |
| 238 | rpm_msg->needs_free = true; |
| 239 | |
| 240 | ret = __fill_rpmh_msg(rpm_msg, state, cmd, n); |
| 241 | if (ret) { |
| 242 | kfree(rpm_msg); |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | return __rpmh_write(dev, state, rpm_msg); |
| 247 | } |
| 248 | EXPORT_SYMBOL(rpmh_write_async); |
| 249 | |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 250 | /** |
| 251 | * rpmh_write: Write a set of RPMH commands and block until response |
| 252 | * |
Lee Jones | 1894b78 | 2020-11-03 15:28:28 +0000 | [diff] [blame] | 253 | * @dev: The device making the request |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 254 | * @state: Active/sleep set |
| 255 | * @cmd: The payload data |
| 256 | * @n: The number of elements in @cmd |
| 257 | * |
| 258 | * May sleep. Do not call from atomic contexts. |
| 259 | */ |
| 260 | int rpmh_write(const struct device *dev, enum rpmh_state state, |
| 261 | const struct tcs_cmd *cmd, u32 n) |
| 262 | { |
| 263 | DECLARE_COMPLETION_ONSTACK(compl); |
| 264 | DEFINE_RPMH_MSG_ONSTACK(dev, state, &compl, rpm_msg); |
| 265 | int ret; |
| 266 | |
Maulik Shah | 0924dad | 2020-11-26 14:01:30 +0530 | [diff] [blame] | 267 | ret = __fill_rpmh_msg(&rpm_msg, state, cmd, n); |
| 268 | if (ret) |
| 269 | return ret; |
Lina Iyer | c103845 | 2018-06-20 18:57:01 +0530 | [diff] [blame] | 270 | |
| 271 | ret = __rpmh_write(dev, state, &rpm_msg); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | ret = wait_for_completion_timeout(&compl, RPMH_TIMEOUT_MS); |
| 276 | WARN_ON(!ret); |
| 277 | return (ret > 0) ? 0 : -ETIMEDOUT; |
| 278 | } |
| 279 | EXPORT_SYMBOL(rpmh_write); |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 280 | |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 281 | static void cache_batch(struct rpmh_ctrlr *ctrlr, struct batch_cache_req *req) |
| 282 | { |
| 283 | unsigned long flags; |
| 284 | |
| 285 | spin_lock_irqsave(&ctrlr->cache_lock, flags); |
| 286 | list_add_tail(&req->list, &ctrlr->batch_cache); |
Maulik Shah | bb70006 | 2020-04-12 20:20:00 +0530 | [diff] [blame] | 287 | ctrlr->dirty = true; |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 288 | spin_unlock_irqrestore(&ctrlr->cache_lock, flags); |
| 289 | } |
| 290 | |
| 291 | static int flush_batch(struct rpmh_ctrlr *ctrlr) |
| 292 | { |
| 293 | struct batch_cache_req *req; |
| 294 | const struct rpmh_request *rpm_msg; |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 295 | int ret = 0; |
| 296 | int i; |
| 297 | |
| 298 | /* Send Sleep/Wake requests to the controller, expect no response */ |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 299 | list_for_each_entry(req, &ctrlr->batch_cache, list) { |
| 300 | for (i = 0; i < req->count; i++) { |
| 301 | rpm_msg = req->rpm_msgs + i; |
| 302 | ret = rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr), |
| 303 | &rpm_msg->msg); |
| 304 | if (ret) |
| 305 | break; |
| 306 | } |
| 307 | } |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 312 | /** |
| 313 | * rpmh_write_batch: Write multiple sets of RPMH commands and wait for the |
| 314 | * batch to finish. |
| 315 | * |
| 316 | * @dev: the device making the request |
| 317 | * @state: Active/sleep set |
| 318 | * @cmd: The payload data |
| 319 | * @n: The array of count of elements in each batch, 0 terminated. |
| 320 | * |
| 321 | * Write a request to the RSC controller without caching. If the request |
| 322 | * state is ACTIVE, then the requests are treated as completion request |
| 323 | * and sent to the controller immediately. The function waits until all the |
| 324 | * commands are complete. If the request was to SLEEP or WAKE_ONLY, then the |
| 325 | * request is sent as fire-n-forget and no ack is expected. |
| 326 | * |
| 327 | * May sleep. Do not call from atomic contexts for ACTIVE_ONLY requests. |
| 328 | */ |
| 329 | int rpmh_write_batch(const struct device *dev, enum rpmh_state state, |
| 330 | const struct tcs_cmd *cmd, u32 *n) |
| 331 | { |
| 332 | struct batch_cache_req *req; |
| 333 | struct rpmh_request *rpm_msgs; |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 334 | struct completion *compls; |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 335 | struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev); |
| 336 | unsigned long time_left; |
| 337 | int count = 0; |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 338 | int ret, i; |
| 339 | void *ptr; |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 340 | |
| 341 | if (!cmd || !n) |
| 342 | return -EINVAL; |
| 343 | |
| 344 | while (n[count] > 0) |
| 345 | count++; |
| 346 | if (!count) |
| 347 | return -EINVAL; |
| 348 | |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 349 | ptr = kzalloc(sizeof(*req) + |
| 350 | count * (sizeof(req->rpm_msgs[0]) + sizeof(*compls)), |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 351 | GFP_ATOMIC); |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 352 | if (!ptr) |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 353 | return -ENOMEM; |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 354 | |
| 355 | req = ptr; |
| 356 | compls = ptr + sizeof(*req) + count * sizeof(*rpm_msgs); |
| 357 | |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 358 | req->count = count; |
| 359 | rpm_msgs = req->rpm_msgs; |
| 360 | |
| 361 | for (i = 0; i < count; i++) { |
| 362 | __fill_rpmh_msg(rpm_msgs + i, state, cmd, n[i]); |
| 363 | cmd += n[i]; |
| 364 | } |
| 365 | |
| 366 | if (state != RPMH_ACTIVE_ONLY_STATE) { |
| 367 | cache_batch(ctrlr, req); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | for (i = 0; i < count; i++) { |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 372 | struct completion *compl = &compls[i]; |
| 373 | |
| 374 | init_completion(compl); |
| 375 | rpm_msgs[i].completion = compl; |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 376 | ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msgs[i].msg); |
| 377 | if (ret) { |
| 378 | pr_err("Error(%d) sending RPMH message addr=%#x\n", |
| 379 | ret, rpm_msgs[i].msg.cmds[0].addr); |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | time_left = RPMH_TIMEOUT_MS; |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 385 | while (i--) { |
| 386 | time_left = wait_for_completion_timeout(&compls[i], time_left); |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 387 | if (!time_left) { |
| 388 | /* |
| 389 | * Better hope they never finish because they'll signal |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 390 | * the completion that we're going to free once |
| 391 | * we've returned from this function. |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 392 | */ |
| 393 | WARN_ON(1); |
| 394 | ret = -ETIMEDOUT; |
| 395 | goto exit; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | exit: |
Stephen Boyd | baef1c9 | 2019-01-15 14:54:47 -0800 | [diff] [blame] | 400 | kfree(ptr); |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 401 | |
| 402 | return ret; |
| 403 | } |
| 404 | EXPORT_SYMBOL(rpmh_write_batch); |
| 405 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 406 | static int is_req_valid(struct cache_req *req) |
| 407 | { |
| 408 | return (req->sleep_val != UINT_MAX && |
| 409 | req->wake_val != UINT_MAX && |
| 410 | req->sleep_val != req->wake_val); |
| 411 | } |
| 412 | |
Maulik Shah | d5e20507 | 2020-02-03 19:05:35 +0530 | [diff] [blame] | 413 | static int send_single(struct rpmh_ctrlr *ctrlr, enum rpmh_state state, |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 414 | u32 addr, u32 data) |
| 415 | { |
Maulik Shah | d5e20507 | 2020-02-03 19:05:35 +0530 | [diff] [blame] | 416 | DEFINE_RPMH_MSG_ONSTACK(NULL, state, NULL, rpm_msg); |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 417 | |
| 418 | /* Wake sets are always complete and sleep sets are not */ |
| 419 | rpm_msg.msg.wait_for_compl = (state == RPMH_WAKE_ONLY_STATE); |
| 420 | rpm_msg.cmd[0].addr = addr; |
| 421 | rpm_msg.cmd[0].data = data; |
| 422 | rpm_msg.msg.num_cmds = 1; |
| 423 | |
| 424 | return rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr), &rpm_msg.msg); |
| 425 | } |
| 426 | |
| 427 | /** |
Maulik Shah | 985427f | 2020-04-12 20:20:02 +0530 | [diff] [blame] | 428 | * rpmh_flush() - Flushes the buffered sleep and wake sets to TCSes |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 429 | * |
Maulik Shah | 985427f | 2020-04-12 20:20:02 +0530 | [diff] [blame] | 430 | * @ctrlr: Controller making request to flush cached data |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 431 | * |
Maulik Shah | 985427f | 2020-04-12 20:20:02 +0530 | [diff] [blame] | 432 | * Return: |
| 433 | * * 0 - Success |
Maulik Shah | 985427f | 2020-04-12 20:20:02 +0530 | [diff] [blame] | 434 | * * Error code - Otherwise |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 435 | */ |
Maulik Shah | d5e20507 | 2020-02-03 19:05:35 +0530 | [diff] [blame] | 436 | int rpmh_flush(struct rpmh_ctrlr *ctrlr) |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 437 | { |
| 438 | struct cache_req *p; |
Douglas Anderson | d2a8cfc | 2020-05-04 10:50:19 -0700 | [diff] [blame] | 439 | int ret = 0; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 440 | |
Maulik Shah | 985427f | 2020-04-12 20:20:02 +0530 | [diff] [blame] | 441 | lockdep_assert_irqs_disabled(); |
| 442 | |
Douglas Anderson | d2a8cfc | 2020-05-04 10:50:19 -0700 | [diff] [blame] | 443 | /* |
| 444 | * Currently rpmh_flush() is only called when we think we're running |
| 445 | * on the last processor. If the lock is busy it means another |
| 446 | * processor is up and it's better to abort than spin. |
| 447 | */ |
| 448 | if (!spin_trylock(&ctrlr->cache_lock)) |
| 449 | return -EBUSY; |
| 450 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 451 | if (!ctrlr->dirty) { |
| 452 | pr_debug("Skipping flush, TCS has latest data.\n"); |
Douglas Anderson | d2a8cfc | 2020-05-04 10:50:19 -0700 | [diff] [blame] | 453 | goto exit; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 454 | } |
| 455 | |
Maulik Shah | f5ac95f | 2020-04-12 20:20:01 +0530 | [diff] [blame] | 456 | /* Invalidate the TCSes first to avoid stale data */ |
Douglas Anderson | 881808d | 2020-04-13 10:04:14 -0700 | [diff] [blame] | 457 | rpmh_rsc_invalidate(ctrlr_to_drv(ctrlr)); |
Maulik Shah | f5ac95f | 2020-04-12 20:20:01 +0530 | [diff] [blame] | 458 | |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 459 | /* First flush the cached batch requests */ |
| 460 | ret = flush_batch(ctrlr); |
| 461 | if (ret) |
Douglas Anderson | d2a8cfc | 2020-05-04 10:50:19 -0700 | [diff] [blame] | 462 | goto exit; |
Lina Iyer | c8790cb6 | 2018-06-20 18:57:06 +0530 | [diff] [blame] | 463 | |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 464 | list_for_each_entry(p, &ctrlr->cache, list) { |
| 465 | if (!is_req_valid(p)) { |
| 466 | pr_debug("%s: skipping RPMH req: a:%#x s:%#x w:%#x", |
| 467 | __func__, p->addr, p->sleep_val, p->wake_val); |
| 468 | continue; |
| 469 | } |
Maulik Shah | d5e20507 | 2020-02-03 19:05:35 +0530 | [diff] [blame] | 470 | ret = send_single(ctrlr, RPMH_SLEEP_STATE, p->addr, |
| 471 | p->sleep_val); |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 472 | if (ret) |
Douglas Anderson | d2a8cfc | 2020-05-04 10:50:19 -0700 | [diff] [blame] | 473 | goto exit; |
Maulik Shah | d5e20507 | 2020-02-03 19:05:35 +0530 | [diff] [blame] | 474 | ret = send_single(ctrlr, RPMH_WAKE_ONLY_STATE, p->addr, |
| 475 | p->wake_val); |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 476 | if (ret) |
Douglas Anderson | d2a8cfc | 2020-05-04 10:50:19 -0700 | [diff] [blame] | 477 | goto exit; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | ctrlr->dirty = false; |
| 481 | |
Douglas Anderson | d2a8cfc | 2020-05-04 10:50:19 -0700 | [diff] [blame] | 482 | exit: |
| 483 | spin_unlock(&ctrlr->cache_lock); |
| 484 | return ret; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 485 | } |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 486 | |
| 487 | /** |
Maulik Shah | f5ac95f | 2020-04-12 20:20:01 +0530 | [diff] [blame] | 488 | * rpmh_invalidate: Invalidate sleep and wake sets in batch_cache |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 489 | * |
| 490 | * @dev: The device making the request |
| 491 | * |
Maulik Shah | f5ac95f | 2020-04-12 20:20:01 +0530 | [diff] [blame] | 492 | * Invalidate the sleep and wake values in batch_cache. |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 493 | */ |
Maulik Shah | 73edcd3 | 2020-06-18 18:35:53 +0530 | [diff] [blame] | 494 | void rpmh_invalidate(const struct device *dev) |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 495 | { |
| 496 | struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev); |
Maulik Shah | f5ac95f | 2020-04-12 20:20:01 +0530 | [diff] [blame] | 497 | struct batch_cache_req *req, *tmp; |
| 498 | unsigned long flags; |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 499 | |
Maulik Shah | f5ac95f | 2020-04-12 20:20:01 +0530 | [diff] [blame] | 500 | spin_lock_irqsave(&ctrlr->cache_lock, flags); |
| 501 | list_for_each_entry_safe(req, tmp, &ctrlr->batch_cache, list) |
| 502 | kfree(req); |
| 503 | INIT_LIST_HEAD(&ctrlr->batch_cache); |
| 504 | ctrlr->dirty = true; |
| 505 | spin_unlock_irqrestore(&ctrlr->cache_lock, flags); |
Lina Iyer | 600513d | 2018-06-20 18:57:04 +0530 | [diff] [blame] | 506 | } |
| 507 | EXPORT_SYMBOL(rpmh_invalidate); |