rpmh.c 12 KB

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