rpmh.c 12 KB

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