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 ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msg->msg);
188 /* Clean up our call by spoofing tx_done */
190 rpmh_tx_done(&rpm_msg->msg);
196 static int __fill_rpmh_msg(struct rpmh_request *req, enum rpmh_state state,
197 const struct tcs_cmd *cmd, u32 n)
199 if (!cmd || !n || n > MAX_RPMH_PAYLOAD)
202 memcpy(req->cmd, cmd, n * sizeof(*cmd));
204 req->msg.state = state;
205 req->msg.cmds = req->cmd;
206 req->msg.num_cmds = n;
212 * rpmh_write_async: Write a set of RPMH commands
214 * @dev: The device making the request
215 * @state: Active/sleep set
216 * @cmd: The payload data
217 * @n: The number of elements in payload
219 * Write a set of RPMH commands, the order of commands is maintained
220 * and will be sent as a single shot.
222 int rpmh_write_async(const struct device *dev, enum rpmh_state state,
223 const struct tcs_cmd *cmd, u32 n)
225 struct rpmh_request *rpm_msg;
228 rpm_msg = kzalloc(sizeof(*rpm_msg), GFP_ATOMIC);
231 rpm_msg->needs_free = true;
233 ret = __fill_rpmh_msg(rpm_msg, state, cmd, n);
239 return __rpmh_write(dev, state, rpm_msg);
241 EXPORT_SYMBOL_GPL(rpmh_write_async);
244 * rpmh_write: Write a set of RPMH commands and block until response
246 * @dev: The device making the request
247 * @state: Active/sleep set
248 * @cmd: The payload data
249 * @n: The number of elements in @cmd
251 * May sleep. Do not call from atomic contexts.
253 int rpmh_write(const struct device *dev, enum rpmh_state state,
254 const struct tcs_cmd *cmd, u32 n)
256 DECLARE_COMPLETION_ONSTACK(compl);
257 DEFINE_RPMH_MSG_ONSTACK(dev, state, &compl, rpm_msg);
260 ret = __fill_rpmh_msg(&rpm_msg, state, cmd, n);
264 ret = __rpmh_write(dev, state, &rpm_msg);
268 ret = wait_for_completion_timeout(&compl, RPMH_TIMEOUT_MS);
270 return (ret > 0) ? 0 : -ETIMEDOUT;
272 EXPORT_SYMBOL_GPL(rpmh_write);
274 static void cache_batch(struct rpmh_ctrlr *ctrlr, struct batch_cache_req *req)
278 spin_lock_irqsave(&ctrlr->cache_lock, flags);
279 list_add_tail(&req->list, &ctrlr->batch_cache);
281 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
284 static int flush_batch(struct rpmh_ctrlr *ctrlr)
286 struct batch_cache_req *req;
287 const struct rpmh_request *rpm_msg;
291 /* Send Sleep/Wake requests to the controller, expect no response */
292 list_for_each_entry(req, &ctrlr->batch_cache, list) {
293 for (i = 0; i < req->count; i++) {
294 rpm_msg = req->rpm_msgs + i;
295 ret = rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr),
306 * rpmh_write_batch: Write multiple sets of RPMH commands and wait for the
309 * @dev: the device making the request
310 * @state: Active/sleep set
311 * @cmd: The payload data
312 * @n: The array of count of elements in each batch, 0 terminated.
314 * Write a request to the RSC controller without caching. If the request
315 * state is ACTIVE, then the requests are treated as completion request
316 * and sent to the controller immediately. The function waits until all the
317 * commands are complete. If the request was to SLEEP or WAKE_ONLY, then the
318 * request is sent as fire-n-forget and no ack is expected.
320 * May sleep. Do not call from atomic contexts for ACTIVE_ONLY requests.
322 int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
323 const struct tcs_cmd *cmd, u32 *n)
325 struct batch_cache_req *req;
326 struct rpmh_request *rpm_msgs;
327 struct completion *compls;
328 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
329 unsigned long time_left;
342 ptr = kzalloc(sizeof(*req) +
343 count * (sizeof(req->rpm_msgs[0]) + sizeof(*compls)),
349 compls = ptr + sizeof(*req) + count * sizeof(*rpm_msgs);
352 rpm_msgs = req->rpm_msgs;
354 for (i = 0; i < count; i++) {
355 __fill_rpmh_msg(rpm_msgs + i, state, cmd, n[i]);
359 if (state != RPMH_ACTIVE_ONLY_STATE) {
360 cache_batch(ctrlr, req);
364 for (i = 0; i < count; i++) {
365 struct completion *compl = &compls[i];
367 init_completion(compl);
368 rpm_msgs[i].completion = compl;
369 ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msgs[i].msg);
371 pr_err("Error(%d) sending RPMH message addr=%#x\n",
372 ret, rpm_msgs[i].msg.cmds[0].addr);
377 time_left = RPMH_TIMEOUT_MS;
379 time_left = wait_for_completion_timeout(&compls[i], time_left);
382 * Better hope they never finish because they'll signal
383 * the completion that we're going to free once
384 * we've returned from this function.
397 EXPORT_SYMBOL_GPL(rpmh_write_batch);
399 static int is_req_valid(struct cache_req *req)
401 return (req->sleep_val != UINT_MAX &&
402 req->wake_val != UINT_MAX &&
403 req->sleep_val != req->wake_val);
406 static int send_single(struct rpmh_ctrlr *ctrlr, enum rpmh_state state,
409 DEFINE_RPMH_MSG_ONSTACK(NULL, state, NULL, rpm_msg);
411 /* Wake sets are always complete and sleep sets are not */
412 rpm_msg.msg.wait_for_compl = (state == RPMH_WAKE_ONLY_STATE);
413 rpm_msg.cmd[0].addr = addr;
414 rpm_msg.cmd[0].data = data;
415 rpm_msg.msg.num_cmds = 1;
417 return rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr), &rpm_msg.msg);
421 * rpmh_flush() - Flushes the buffered sleep and wake sets to TCSes
423 * @ctrlr: Controller making request to flush cached data
427 * * Error code - Otherwise
429 int rpmh_flush(struct rpmh_ctrlr *ctrlr)
434 lockdep_assert_irqs_disabled();
437 * Currently rpmh_flush() is only called when we think we're running
438 * on the last processor. If the lock is busy it means another
439 * processor is up and it's better to abort than spin.
441 if (!spin_trylock(&ctrlr->cache_lock))
445 pr_debug("Skipping flush, TCS has latest data.\n");
446 goto write_next_wakeup;
449 /* Invalidate the TCSes first to avoid stale data */
450 rpmh_rsc_invalidate(ctrlr_to_drv(ctrlr));
452 /* First flush the cached batch requests */
453 ret = flush_batch(ctrlr);
457 list_for_each_entry(p, &ctrlr->cache, list) {
458 if (!is_req_valid(p)) {
459 pr_debug("%s: skipping RPMH req: a:%#x s:%#x w:%#x",
460 __func__, p->addr, p->sleep_val, p->wake_val);
463 ret = send_single(ctrlr, RPMH_SLEEP_STATE, p->addr,
467 ret = send_single(ctrlr, RPMH_WAKE_ONLY_STATE, p->addr,
473 ctrlr->dirty = false;
476 rpmh_rsc_write_next_wakeup(ctrlr_to_drv(ctrlr));
478 spin_unlock(&ctrlr->cache_lock);
483 * rpmh_invalidate: Invalidate sleep and wake sets in batch_cache
485 * @dev: The device making the request
487 * Invalidate the sleep and wake values in batch_cache.
489 void rpmh_invalidate(const struct device *dev)
491 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
492 struct batch_cache_req *req, *tmp;
495 spin_lock_irqsave(&ctrlr->cache_lock, flags);
496 list_for_each_entry_safe(req, tmp, &ctrlr->batch_cache, list)
498 INIT_LIST_HEAD(&ctrlr->batch_cache);
500 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
502 EXPORT_SYMBOL_GPL(rpmh_invalidate);