1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
6 #include <linux/atomic.h>
8 #include <linux/interrupt.h>
9 #include <linux/jiffies.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/lockdep.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/types.h>
19 #include <linux/wait.h>
21 #include <soc/qcom/rpmh.h>
23 #include "rpmh-internal.h"
25 #define RPMH_TIMEOUT_MS msecs_to_jiffies(10000)
27 #define DEFINE_RPMH_MSG_ONSTACK(device, s, q, name) \
28 struct rpmh_request name = { \
33 .wait_for_compl = true, \
38 .needs_free = false, \
41 #define ctrlr_to_drv(ctrlr) container_of(ctrlr, struct rsc_drv, client)
44 * struct cache_req: the request object for caching
46 * @addr: the address of the resource
47 * @sleep_val: the sleep vote
48 * @wake_val: the wake vote
49 * @list: linked list obj
55 struct list_head list;
59 * struct batch_cache_req - An entry in our batch catch
61 * @list: linked list obj
62 * @count: number of messages
63 * @rpm_msgs: the messages
66 struct batch_cache_req {
67 struct list_head list;
69 struct rpmh_request rpm_msgs[];
72 static struct rpmh_ctrlr *get_rpmh_ctrlr(const struct device *dev)
74 struct rsc_drv *drv = dev_get_drvdata(dev->parent);
79 void rpmh_tx_done(const struct tcs_request *msg)
81 struct rpmh_request *rpm_msg = container_of(msg, struct rpmh_request,
83 struct completion *compl = rpm_msg->completion;
84 bool free = rpm_msg->needs_free;
89 /* Signal the blocking thread we are done */
97 static struct cache_req *__find_req(struct rpmh_ctrlr *ctrlr, u32 addr)
99 struct cache_req *p, *req = NULL;
101 list_for_each_entry(p, &ctrlr->cache, list) {
102 if (p->addr == addr) {
111 static struct cache_req *cache_rpm_request(struct rpmh_ctrlr *ctrlr,
112 enum rpmh_state state,
115 struct cache_req *req;
117 u32 old_sleep_val, old_wake_val;
119 spin_lock_irqsave(&ctrlr->cache_lock, flags);
120 req = __find_req(ctrlr, cmd->addr);
124 req = kzalloc(sizeof(*req), GFP_ATOMIC);
126 req = ERR_PTR(-ENOMEM);
130 req->addr = cmd->addr;
131 req->sleep_val = req->wake_val = UINT_MAX;
132 list_add_tail(&req->list, &ctrlr->cache);
135 old_sleep_val = req->sleep_val;
136 old_wake_val = req->wake_val;
139 case RPMH_ACTIVE_ONLY_STATE:
140 case RPMH_WAKE_ONLY_STATE:
141 req->wake_val = cmd->data;
143 case RPMH_SLEEP_STATE:
144 req->sleep_val = cmd->data;
148 ctrlr->dirty |= (req->sleep_val != old_sleep_val ||
149 req->wake_val != old_wake_val) &&
150 req->sleep_val != UINT_MAX &&
151 req->wake_val != UINT_MAX;
154 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
160 * __rpmh_write: Cache and send the RPMH request
162 * @dev: The device making the request
163 * @state: Active/Sleep request type
164 * @rpm_msg: The data that needs to be sent (cmds).
166 * Cache the RPMH request and send if the state is ACTIVE_ONLY.
167 * SLEEP/WAKE_ONLY requests are not sent to the controller at
168 * this time. Use rpmh_flush() to send them to the controller.
170 static int __rpmh_write(const struct device *dev, enum rpmh_state state,
171 struct rpmh_request *rpm_msg)
173 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
175 struct cache_req *req;
178 /* Cache the request in our store and link the payload */
179 for (i = 0; i < rpm_msg->msg.num_cmds; i++) {
180 req = cache_rpm_request(ctrlr, state, &rpm_msg->msg.cmds[i]);
185 if (state == RPMH_ACTIVE_ONLY_STATE) {
186 WARN_ON(irqs_disabled());
187 ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msg->msg);
189 /* Clean up our call by spoofing tx_done */
191 rpmh_tx_done(&rpm_msg->msg);
197 static int __fill_rpmh_msg(struct rpmh_request *req, enum rpmh_state state,
198 const struct tcs_cmd *cmd, u32 n)
200 if (!cmd || !n || n > MAX_RPMH_PAYLOAD)
203 memcpy(req->cmd, cmd, n * sizeof(*cmd));
205 req->msg.state = state;
206 req->msg.cmds = req->cmd;
207 req->msg.num_cmds = n;
213 * rpmh_write_async: Write a set of RPMH commands
215 * @dev: The device making the request
216 * @state: Active/sleep set
217 * @cmd: The payload data
218 * @n: The number of elements in payload
220 * Write a set of RPMH commands, the order of commands is maintained
221 * and will be sent as a single shot.
223 int rpmh_write_async(const struct device *dev, enum rpmh_state state,
224 const struct tcs_cmd *cmd, u32 n)
226 struct rpmh_request *rpm_msg;
229 rpm_msg = kzalloc(sizeof(*rpm_msg), GFP_ATOMIC);
232 rpm_msg->needs_free = true;
234 ret = __fill_rpmh_msg(rpm_msg, state, cmd, n);
240 return __rpmh_write(dev, state, rpm_msg);
242 EXPORT_SYMBOL_GPL(rpmh_write_async);
245 * rpmh_write: Write a set of RPMH commands and block until response
247 * @dev: The device making the request
248 * @state: Active/sleep set
249 * @cmd: The payload data
250 * @n: The number of elements in @cmd
252 * May sleep. Do not call from atomic contexts.
254 int rpmh_write(const struct device *dev, enum rpmh_state state,
255 const struct tcs_cmd *cmd, u32 n)
257 DECLARE_COMPLETION_ONSTACK(compl);
258 DEFINE_RPMH_MSG_ONSTACK(dev, state, &compl, rpm_msg);
261 ret = __fill_rpmh_msg(&rpm_msg, state, cmd, n);
265 ret = __rpmh_write(dev, state, &rpm_msg);
269 ret = wait_for_completion_timeout(&compl, RPMH_TIMEOUT_MS);
271 return (ret > 0) ? 0 : -ETIMEDOUT;
273 EXPORT_SYMBOL_GPL(rpmh_write);
275 static void cache_batch(struct rpmh_ctrlr *ctrlr, struct batch_cache_req *req)
279 spin_lock_irqsave(&ctrlr->cache_lock, flags);
280 list_add_tail(&req->list, &ctrlr->batch_cache);
282 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
285 static int flush_batch(struct rpmh_ctrlr *ctrlr)
287 struct batch_cache_req *req;
288 const struct rpmh_request *rpm_msg;
292 /* Send Sleep/Wake requests to the controller, expect no response */
293 list_for_each_entry(req, &ctrlr->batch_cache, list) {
294 for (i = 0; i < req->count; i++) {
295 rpm_msg = req->rpm_msgs + i;
296 ret = rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr),
307 * rpmh_write_batch: Write multiple sets of RPMH commands and wait for the
310 * @dev: the device making the request
311 * @state: Active/sleep set
312 * @cmd: The payload data
313 * @n: The array of count of elements in each batch, 0 terminated.
315 * Write a request to the RSC controller without caching. If the request
316 * state is ACTIVE, then the requests are treated as completion request
317 * and sent to the controller immediately. The function waits until all the
318 * commands are complete. If the request was to SLEEP or WAKE_ONLY, then the
319 * request is sent as fire-n-forget and no ack is expected.
321 * May sleep. Do not call from atomic contexts for ACTIVE_ONLY requests.
323 int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
324 const struct tcs_cmd *cmd, u32 *n)
326 struct batch_cache_req *req;
327 struct rpmh_request *rpm_msgs;
328 struct completion *compls;
329 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
330 unsigned long time_left;
343 ptr = kzalloc(sizeof(*req) +
344 count * (sizeof(req->rpm_msgs[0]) + sizeof(*compls)),
350 compls = ptr + sizeof(*req) + count * sizeof(*rpm_msgs);
353 rpm_msgs = req->rpm_msgs;
355 for (i = 0; i < count; i++) {
356 __fill_rpmh_msg(rpm_msgs + i, state, cmd, n[i]);
360 if (state != RPMH_ACTIVE_ONLY_STATE) {
361 cache_batch(ctrlr, req);
365 for (i = 0; i < count; i++) {
366 struct completion *compl = &compls[i];
368 init_completion(compl);
369 rpm_msgs[i].completion = compl;
370 ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msgs[i].msg);
372 pr_err("Error(%d) sending RPMH message addr=%#x\n",
373 ret, rpm_msgs[i].msg.cmds[0].addr);
378 time_left = RPMH_TIMEOUT_MS;
380 time_left = wait_for_completion_timeout(&compls[i], time_left);
383 * Better hope they never finish because they'll signal
384 * the completion that we're going to free once
385 * we've returned from this function.
398 EXPORT_SYMBOL_GPL(rpmh_write_batch);
400 static int is_req_valid(struct cache_req *req)
402 return (req->sleep_val != UINT_MAX &&
403 req->wake_val != UINT_MAX &&
404 req->sleep_val != req->wake_val);
407 static int send_single(struct rpmh_ctrlr *ctrlr, enum rpmh_state state,
410 DEFINE_RPMH_MSG_ONSTACK(NULL, state, NULL, rpm_msg);
412 /* Wake sets are always complete and sleep sets are not */
413 rpm_msg.msg.wait_for_compl = (state == RPMH_WAKE_ONLY_STATE);
414 rpm_msg.cmd[0].addr = addr;
415 rpm_msg.cmd[0].data = data;
416 rpm_msg.msg.num_cmds = 1;
418 return rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr), &rpm_msg.msg);
422 * rpmh_flush() - Flushes the buffered sleep and wake sets to TCSes
424 * @ctrlr: Controller making request to flush cached data
428 * * Error code - Otherwise
430 int rpmh_flush(struct rpmh_ctrlr *ctrlr)
435 lockdep_assert_irqs_disabled();
438 * Currently rpmh_flush() is only called when we think we're running
439 * on the last processor. If the lock is busy it means another
440 * processor is up and it's better to abort than spin.
442 if (!spin_trylock(&ctrlr->cache_lock))
446 pr_debug("Skipping flush, TCS has latest data.\n");
447 goto write_next_wakeup;
450 /* Invalidate the TCSes first to avoid stale data */
451 rpmh_rsc_invalidate(ctrlr_to_drv(ctrlr));
453 /* First flush the cached batch requests */
454 ret = flush_batch(ctrlr);
458 list_for_each_entry(p, &ctrlr->cache, list) {
459 if (!is_req_valid(p)) {
460 pr_debug("%s: skipping RPMH req: a:%#x s:%#x w:%#x",
461 __func__, p->addr, p->sleep_val, p->wake_val);
464 ret = send_single(ctrlr, RPMH_SLEEP_STATE, p->addr,
468 ret = send_single(ctrlr, RPMH_WAKE_ONLY_STATE, p->addr,
474 ctrlr->dirty = false;
477 rpmh_rsc_write_next_wakeup(ctrlr_to_drv(ctrlr));
479 spin_unlock(&ctrlr->cache_lock);
484 * rpmh_invalidate: Invalidate sleep and wake sets in batch_cache
486 * @dev: The device making the request
488 * Invalidate the sleep and wake values in batch_cache.
490 void rpmh_invalidate(const struct device *dev)
492 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
493 struct batch_cache_req *req, *tmp;
496 spin_lock_irqsave(&ctrlr->cache_lock, flags);
497 list_for_each_entry_safe(req, tmp, &ctrlr->batch_cache, list)
499 INIT_LIST_HEAD(&ctrlr->batch_cache);
501 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
503 EXPORT_SYMBOL_GPL(rpmh_invalidate);