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/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/types.h>
18 #include <linux/wait.h>
20 #include <soc/qcom/rpmh.h>
22 #include "rpmh-internal.h"
24 #define RPMH_TIMEOUT_MS msecs_to_jiffies(10000)
26 #define DEFINE_RPMH_MSG_ONSTACK(dev, s, q, name) \
27 struct rpmh_request name = { \
32 .wait_for_compl = true, \
37 .needs_free = false, \
40 #define ctrlr_to_drv(ctrlr) container_of(ctrlr, struct rsc_drv, client)
43 * struct cache_req: the request object for caching
45 * @addr: the address of the resource
46 * @sleep_val: the sleep vote
47 * @wake_val: the wake vote
48 * @list: linked list obj
54 struct list_head list;
58 * struct batch_cache_req - An entry in our batch catch
60 * @list: linked list obj
61 * @count: number of messages
62 * @rpm_msgs: the messages
65 struct batch_cache_req {
66 struct list_head list;
68 struct rpmh_request rpm_msgs[];
71 static struct rpmh_ctrlr *get_rpmh_ctrlr(const struct device *dev)
73 struct rsc_drv *drv = dev_get_drvdata(dev->parent);
78 void rpmh_tx_done(const struct tcs_request *msg, int r)
80 struct rpmh_request *rpm_msg = container_of(msg, struct rpmh_request,
82 struct completion *compl = rpm_msg->completion;
83 bool free = rpm_msg->needs_free;
88 dev_err(rpm_msg->dev, "RPMH TX fail in msg addr=%#x, err=%d\n",
89 rpm_msg->msg.cmds[0].addr, r);
94 /* Signal the blocking thread we are done */
102 static struct cache_req *__find_req(struct rpmh_ctrlr *ctrlr, u32 addr)
104 struct cache_req *p, *req = NULL;
106 list_for_each_entry(p, &ctrlr->cache, list) {
107 if (p->addr == addr) {
116 static struct cache_req *cache_rpm_request(struct rpmh_ctrlr *ctrlr,
117 enum rpmh_state state,
120 struct cache_req *req;
122 u32 old_sleep_val, old_wake_val;
124 spin_lock_irqsave(&ctrlr->cache_lock, flags);
125 req = __find_req(ctrlr, cmd->addr);
129 req = kzalloc(sizeof(*req), GFP_ATOMIC);
131 req = ERR_PTR(-ENOMEM);
135 req->addr = cmd->addr;
136 req->sleep_val = req->wake_val = UINT_MAX;
137 list_add_tail(&req->list, &ctrlr->cache);
140 old_sleep_val = req->sleep_val;
141 old_wake_val = req->wake_val;
144 case RPMH_ACTIVE_ONLY_STATE:
145 case RPMH_WAKE_ONLY_STATE:
146 req->wake_val = cmd->data;
148 case RPMH_SLEEP_STATE:
149 req->sleep_val = cmd->data;
153 ctrlr->dirty |= (req->sleep_val != old_sleep_val ||
154 req->wake_val != old_wake_val) &&
155 req->sleep_val != UINT_MAX &&
156 req->wake_val != UINT_MAX;
159 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
165 * __rpmh_write: Cache and send the RPMH request
167 * @dev: The device making the request
168 * @state: Active/Sleep request type
169 * @rpm_msg: The data that needs to be sent (cmds).
171 * Cache the RPMH request and send if the state is ACTIVE_ONLY.
172 * SLEEP/WAKE_ONLY requests are not sent to the controller at
173 * this time. Use rpmh_flush() to send them to the controller.
175 static int __rpmh_write(const struct device *dev, enum rpmh_state state,
176 struct rpmh_request *rpm_msg)
178 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
180 struct cache_req *req;
183 rpm_msg->msg.state = state;
185 /* Cache the request in our store and link the payload */
186 for (i = 0; i < rpm_msg->msg.num_cmds; i++) {
187 req = cache_rpm_request(ctrlr, state, &rpm_msg->msg.cmds[i]);
192 rpm_msg->msg.state = state;
194 if (state == RPMH_ACTIVE_ONLY_STATE) {
195 WARN_ON(irqs_disabled());
196 ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msg->msg);
198 /* Clean up our call by spoofing tx_done */
200 rpmh_tx_done(&rpm_msg->msg, ret);
206 static int __fill_rpmh_msg(struct rpmh_request *req, enum rpmh_state state,
207 const struct tcs_cmd *cmd, u32 n)
209 if (!cmd || !n || n > MAX_RPMH_PAYLOAD)
212 memcpy(req->cmd, cmd, n * sizeof(*cmd));
214 req->msg.state = state;
215 req->msg.cmds = req->cmd;
216 req->msg.num_cmds = n;
222 * rpmh_write_async: Write a set of RPMH commands
224 * @dev: The device making the request
225 * @state: Active/sleep set
226 * @cmd: The payload data
227 * @n: The number of elements in payload
229 * Write a set of RPMH commands, the order of commands is maintained
230 * and will be sent as a single shot.
232 int rpmh_write_async(const struct device *dev, enum rpmh_state state,
233 const struct tcs_cmd *cmd, u32 n)
235 struct rpmh_request *rpm_msg;
238 rpm_msg = kzalloc(sizeof(*rpm_msg), GFP_ATOMIC);
241 rpm_msg->needs_free = true;
243 ret = __fill_rpmh_msg(rpm_msg, state, cmd, n);
249 return __rpmh_write(dev, state, rpm_msg);
251 EXPORT_SYMBOL(rpmh_write_async);
254 * rpmh_write: Write a set of RPMH commands and block until response
256 * @rc: The RPMH handle got from rpmh_get_client
257 * @state: Active/sleep set
258 * @cmd: The payload data
259 * @n: The number of elements in @cmd
261 * May sleep. Do not call from atomic contexts.
263 int rpmh_write(const struct device *dev, enum rpmh_state state,
264 const struct tcs_cmd *cmd, u32 n)
266 DECLARE_COMPLETION_ONSTACK(compl);
267 DEFINE_RPMH_MSG_ONSTACK(dev, state, &compl, rpm_msg);
270 if (!cmd || !n || n > MAX_RPMH_PAYLOAD)
273 memcpy(rpm_msg.cmd, cmd, n * sizeof(*cmd));
274 rpm_msg.msg.num_cmds = n;
276 ret = __rpmh_write(dev, state, &rpm_msg);
280 ret = wait_for_completion_timeout(&compl, RPMH_TIMEOUT_MS);
282 return (ret > 0) ? 0 : -ETIMEDOUT;
284 EXPORT_SYMBOL(rpmh_write);
286 static void cache_batch(struct rpmh_ctrlr *ctrlr, struct batch_cache_req *req)
290 spin_lock_irqsave(&ctrlr->cache_lock, flags);
291 list_add_tail(&req->list, &ctrlr->batch_cache);
293 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
296 static int flush_batch(struct rpmh_ctrlr *ctrlr)
298 struct batch_cache_req *req;
299 const struct rpmh_request *rpm_msg;
304 /* Send Sleep/Wake requests to the controller, expect no response */
305 spin_lock_irqsave(&ctrlr->cache_lock, flags);
306 list_for_each_entry(req, &ctrlr->batch_cache, list) {
307 for (i = 0; i < req->count; i++) {
308 rpm_msg = req->rpm_msgs + i;
309 ret = rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr),
315 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
321 * rpmh_write_batch: Write multiple sets of RPMH commands and wait for the
324 * @dev: the device making the request
325 * @state: Active/sleep set
326 * @cmd: The payload data
327 * @n: The array of count of elements in each batch, 0 terminated.
329 * Write a request to the RSC controller without caching. If the request
330 * state is ACTIVE, then the requests are treated as completion request
331 * and sent to the controller immediately. The function waits until all the
332 * commands are complete. If the request was to SLEEP or WAKE_ONLY, then the
333 * request is sent as fire-n-forget and no ack is expected.
335 * May sleep. Do not call from atomic contexts for ACTIVE_ONLY requests.
337 int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
338 const struct tcs_cmd *cmd, u32 *n)
340 struct batch_cache_req *req;
341 struct rpmh_request *rpm_msgs;
342 struct completion *compls;
343 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
344 unsigned long time_left;
357 ptr = kzalloc(sizeof(*req) +
358 count * (sizeof(req->rpm_msgs[0]) + sizeof(*compls)),
364 compls = ptr + sizeof(*req) + count * sizeof(*rpm_msgs);
367 rpm_msgs = req->rpm_msgs;
369 for (i = 0; i < count; i++) {
370 __fill_rpmh_msg(rpm_msgs + i, state, cmd, n[i]);
374 if (state != RPMH_ACTIVE_ONLY_STATE) {
375 cache_batch(ctrlr, req);
379 for (i = 0; i < count; i++) {
380 struct completion *compl = &compls[i];
382 init_completion(compl);
383 rpm_msgs[i].completion = compl;
384 ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msgs[i].msg);
386 pr_err("Error(%d) sending RPMH message addr=%#x\n",
387 ret, rpm_msgs[i].msg.cmds[0].addr);
392 time_left = RPMH_TIMEOUT_MS;
394 time_left = wait_for_completion_timeout(&compls[i], time_left);
397 * Better hope they never finish because they'll signal
398 * the completion that we're going to free once
399 * we've returned from this function.
412 EXPORT_SYMBOL(rpmh_write_batch);
414 static int is_req_valid(struct cache_req *req)
416 return (req->sleep_val != UINT_MAX &&
417 req->wake_val != UINT_MAX &&
418 req->sleep_val != req->wake_val);
421 static int send_single(const struct device *dev, enum rpmh_state state,
424 DEFINE_RPMH_MSG_ONSTACK(dev, state, NULL, rpm_msg);
425 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
427 /* Wake sets are always complete and sleep sets are not */
428 rpm_msg.msg.wait_for_compl = (state == RPMH_WAKE_ONLY_STATE);
429 rpm_msg.cmd[0].addr = addr;
430 rpm_msg.cmd[0].data = data;
431 rpm_msg.msg.num_cmds = 1;
433 return rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr), &rpm_msg.msg);
437 * rpmh_flush: Flushes the buffered active and sleep sets to TCS
439 * @dev: The device making the request
441 * Return: -EBUSY if the controller is busy, probably waiting on a response
442 * to a RPMH request sent earlier.
444 * This function is always called from the sleep code from the last CPU
445 * that is powering down the entire system. Since no other RPMH API would be
446 * executing at this time, it is safe to run lockless.
448 int rpmh_flush(const struct device *dev)
451 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
455 pr_debug("Skipping flush, TCS has latest data.\n");
459 /* Invalidate the TCSes first to avoid stale data */
461 ret = rpmh_rsc_invalidate(ctrlr_to_drv(ctrlr));
462 } while (ret == -EAGAIN);
466 /* First flush the cached batch requests */
467 ret = flush_batch(ctrlr);
472 * Nobody else should be calling this function other than system PM,
473 * hence we can run without locks.
475 list_for_each_entry(p, &ctrlr->cache, list) {
476 if (!is_req_valid(p)) {
477 pr_debug("%s: skipping RPMH req: a:%#x s:%#x w:%#x",
478 __func__, p->addr, p->sleep_val, p->wake_val);
481 ret = send_single(dev, RPMH_SLEEP_STATE, p->addr, p->sleep_val);
484 ret = send_single(dev, RPMH_WAKE_ONLY_STATE,
485 p->addr, p->wake_val);
490 ctrlr->dirty = false;
494 EXPORT_SYMBOL(rpmh_flush);
497 * rpmh_invalidate: Invalidate sleep and wake sets in batch_cache
499 * @dev: The device making the request
501 * Invalidate the sleep and wake values in batch_cache.
503 int rpmh_invalidate(const struct device *dev)
505 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
506 struct batch_cache_req *req, *tmp;
509 spin_lock_irqsave(&ctrlr->cache_lock, flags);
510 list_for_each_entry_safe(req, tmp, &ctrlr->batch_cache, list)
512 INIT_LIST_HEAD(&ctrlr->batch_cache);
514 spin_unlock_irqrestore(&ctrlr->cache_lock, flags);
518 EXPORT_SYMBOL(rpmh_invalidate);