messaging.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2017, The Linux Foundation
  4. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/pm_runtime.h>
  8. #include "slimbus.h"
  9. #include <linux/io.h>
  10. /**
  11. * slim_msg_response() - Deliver Message response received from a device to the
  12. * framework.
  13. *
  14. * @ctrl: Controller handle
  15. * @reply: Reply received from the device
  16. * @len: Length of the reply
  17. * @tid: Transaction ID received with which framework can associate reply.
  18. *
  19. * Called by controller to inform framework about the response received.
  20. * This helps in making the API asynchronous, and controller-driver doesn't need
  21. * to manage 1 more table other than the one managed by framework mapping TID
  22. * with buffers
  23. */
  24. void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 len)
  25. {
  26. struct slim_msg_txn *txn;
  27. struct slim_val_inf *msg;
  28. unsigned long flags;
  29. spin_lock_irqsave(&ctrl->txn_lock, flags);
  30. txn = idr_find(&ctrl->tid_idr, tid);
  31. spin_unlock_irqrestore(&ctrl->txn_lock, flags);
  32. if (txn == NULL)
  33. return;
  34. msg = txn->msg;
  35. if (msg == NULL || msg->rbuf == NULL) {
  36. dev_err(ctrl->dev, "Got response to invalid TID:%d, len:%d\n",
  37. tid, len);
  38. return;
  39. }
  40. slim_free_txn_tid(ctrl, txn);
  41. memcpy_fromio(msg->rbuf, reply, len);
  42. if (txn->comp)
  43. complete(txn->comp);
  44. /* Remove runtime-pm vote now that response was received for TID txn */
  45. pm_runtime_mark_last_busy(ctrl->dev);
  46. pm_runtime_put_autosuspend(ctrl->dev);
  47. }
  48. EXPORT_SYMBOL_GPL(slim_msg_response);
  49. /**
  50. * slim_alloc_txn_tid() - Allocate a tid to txn
  51. *
  52. * @ctrl: Controller handle
  53. * @txn: transaction to be allocated with tid.
  54. *
  55. * Return: zero on success with valid txn->tid and error code on failures.
  56. */
  57. int slim_alloc_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn)
  58. {
  59. unsigned long flags;
  60. int ret = 0;
  61. spin_lock_irqsave(&ctrl->txn_lock, flags);
  62. ret = idr_alloc_cyclic(&ctrl->tid_idr, txn, 1,
  63. SLIM_MAX_TIDS, GFP_ATOMIC);
  64. if (ret < 0) {
  65. dev_err(ctrl->dev, "%s: idr_alloc_cyclic ret %d\n",
  66. __func__, ret);
  67. spin_unlock_irqrestore(&ctrl->txn_lock, flags);
  68. return ret;
  69. }
  70. txn->tid = ret;
  71. spin_unlock_irqrestore(&ctrl->txn_lock, flags);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(slim_alloc_txn_tid);
  75. /**
  76. * slim_free_txn_tid() - Free tid of txn
  77. *
  78. * @ctrl: Controller handle
  79. * @txn: transaction whose tid should be freed
  80. */
  81. void slim_free_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn)
  82. {
  83. unsigned long flags;
  84. spin_lock_irqsave(&ctrl->txn_lock, flags);
  85. idr_remove(&ctrl->tid_idr, txn->tid);
  86. spin_unlock_irqrestore(&ctrl->txn_lock, flags);
  87. }
  88. EXPORT_SYMBOL_GPL(slim_free_txn_tid);
  89. /**
  90. * slim_do_transfer() - Process a SLIMbus-messaging transaction
  91. *
  92. * @ctrl: Controller handle
  93. * @txn: Transaction to be sent over SLIMbus
  94. *
  95. * Called by controller to transmit messaging transactions not dealing with
  96. * Interface/Value elements. (e.g. transmitting a message to assign logical
  97. * address to a slave device
  98. *
  99. * Return: -ETIMEDOUT: If transmission of this message timed out
  100. * (e.g. due to bus lines not being clocked or driven by controller)
  101. */
  102. int slim_do_transfer(struct slim_controller *ctrl, struct slim_msg_txn *txn)
  103. {
  104. DECLARE_COMPLETION_ONSTACK(done);
  105. bool need_tid = false, clk_pause_msg = false;
  106. int ret, timeout;
  107. struct completion *comp;
  108. /*
  109. * do not vote for runtime-PM if the transactions are part of clock
  110. * pause sequence
  111. */
  112. if (ctrl->sched.clk_state == SLIM_CLK_ENTERING_PAUSE &&
  113. (txn->mt == SLIM_MSG_MT_CORE &&
  114. txn->mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
  115. txn->mc <= SLIM_MSG_MC_RECONFIGURE_NOW))
  116. clk_pause_msg = true;
  117. /* Initialize tid to invalid value */
  118. txn->tid = 0;
  119. need_tid = slim_tid_txn(txn->mt, txn->mc);
  120. if (need_tid) {
  121. ret = slim_alloc_txn_tid(ctrl, txn);
  122. if (ret) {
  123. dev_err(ctrl->dev, "%s: slim_alloc_txn_tid ret %d\n",
  124. __func__, ret);
  125. return ret;
  126. }
  127. if (!txn->msg->comp) {
  128. comp = txn->comp;
  129. txn->comp = &done;
  130. }
  131. }
  132. if (!clk_pause_msg) {
  133. ret = pm_runtime_get_sync(ctrl->dev);
  134. if (ret < 0) {
  135. dev_err(ctrl->dev, "%s: pm_runtime_get_sync failed: %d\n",
  136. __func__, ret);
  137. slim_free_txn_tid(ctrl, txn);
  138. pm_runtime_put_noidle(ctrl->dev);
  139. /* Set device in suspended since resume failed */
  140. pm_runtime_set_suspended(ctrl->dev);
  141. if (need_tid && !txn->msg->comp)
  142. txn->comp = comp;
  143. return ret;
  144. }
  145. if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
  146. dev_err(ctrl->dev, "%s: ctrl wrong state:%d, ret:%d\n",
  147. __func__, ctrl->sched.clk_state, ret);
  148. if (need_tid && !txn->msg->comp)
  149. txn->comp = comp;
  150. goto slim_xfer_err;
  151. }
  152. }
  153. ret = ctrl->xfer_msg(ctrl, txn);
  154. if (!ret && need_tid && !txn->msg->comp) {
  155. unsigned long ms = txn->rl + HZ;
  156. timeout = wait_for_completion_timeout(txn->comp,
  157. msecs_to_jiffies(ms));
  158. if (!timeout) {
  159. ret = -ETIMEDOUT;
  160. dev_err(ctrl->dev, "%s: Timed out %d\n", __func__, ret);
  161. txn->comp = NULL;
  162. slim_free_txn_tid(ctrl, txn);
  163. }
  164. }
  165. if (ret)
  166. dev_err(ctrl->dev, "Tx:MT:0x%x, MC:0x%x, LA:0x%x failed:%d\n",
  167. txn->mt, txn->mc, txn->la, ret);
  168. slim_xfer_err:
  169. if (!clk_pause_msg && (txn->tid == 0 || ret == -ETIMEDOUT)) {
  170. /*
  171. * remove runtime-pm vote if this was TX only, or
  172. * if there was error during this transaction
  173. */
  174. pm_runtime_mark_last_busy(ctrl->dev);
  175. pm_runtime_put_autosuspend(ctrl->dev);
  176. }
  177. return ret;
  178. }
  179. EXPORT_SYMBOL_GPL(slim_do_transfer);
  180. static int slim_val_inf_sanity(struct slim_controller *ctrl,
  181. struct slim_val_inf *msg, u8 mc)
  182. {
  183. if (!msg || msg->num_bytes > 16 ||
  184. (msg->start_offset + msg->num_bytes) > 0xC00)
  185. goto reterr;
  186. switch (mc) {
  187. case SLIM_MSG_MC_REQUEST_VALUE:
  188. case SLIM_MSG_MC_REQUEST_INFORMATION:
  189. if (msg->rbuf != NULL)
  190. return 0;
  191. break;
  192. case SLIM_MSG_MC_CHANGE_VALUE:
  193. case SLIM_MSG_MC_CLEAR_INFORMATION:
  194. if (msg->wbuf != NULL)
  195. return 0;
  196. break;
  197. case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
  198. case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
  199. if (msg->rbuf != NULL && msg->wbuf != NULL)
  200. return 0;
  201. break;
  202. }
  203. reterr:
  204. if (msg)
  205. dev_err(ctrl->dev, "Sanity check failed:msg:offset:0x%x, mc:%d\n",
  206. msg->start_offset, mc);
  207. return -EINVAL;
  208. }
  209. static u16 slim_slicesize(int code)
  210. {
  211. static const u8 sizetocode[16] = {
  212. 0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7
  213. };
  214. code = clamp(code, 1, (int)ARRAY_SIZE(sizetocode));
  215. return sizetocode[code - 1];
  216. }
  217. /**
  218. * slim_xfer_msg() - Transfer a value info message on slim device
  219. *
  220. * @sbdev: slim device to which this msg has to be transfered
  221. * @msg: value info message pointer
  222. * @mc: message code of the message
  223. *
  224. * Called by drivers which want to transfer a vlaue or info elements.
  225. *
  226. * Return: -ETIMEDOUT: If transmission of this message timed out
  227. */
  228. int slim_xfer_msg(struct slim_device *sbdev, struct slim_val_inf *msg,
  229. u8 mc)
  230. {
  231. DEFINE_SLIM_LDEST_TXN(txn_stack, mc, 6, sbdev->laddr, msg);
  232. struct slim_msg_txn *txn = &txn_stack;
  233. struct slim_controller *ctrl = sbdev->ctrl;
  234. int ret;
  235. u16 sl;
  236. if (!ctrl)
  237. return -EINVAL;
  238. ret = slim_val_inf_sanity(ctrl, msg, mc);
  239. if (ret)
  240. return ret;
  241. sl = slim_slicesize(msg->num_bytes);
  242. dev_dbg(ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
  243. msg->start_offset, msg->num_bytes, mc, sl);
  244. txn->ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
  245. switch (mc) {
  246. case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
  247. case SLIM_MSG_MC_CHANGE_VALUE:
  248. case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
  249. case SLIM_MSG_MC_CLEAR_INFORMATION:
  250. txn->rl += msg->num_bytes;
  251. break;
  252. default:
  253. break;
  254. }
  255. if (slim_tid_txn(txn->mt, txn->mc))
  256. txn->rl++;
  257. return slim_do_transfer(ctrl, txn);
  258. }
  259. EXPORT_SYMBOL_GPL(slim_xfer_msg);
  260. static void slim_fill_msg(struct slim_val_inf *msg, u32 addr,
  261. size_t count, u8 *rbuf, u8 *wbuf)
  262. {
  263. msg->start_offset = addr;
  264. msg->num_bytes = count;
  265. msg->rbuf = rbuf;
  266. msg->wbuf = wbuf;
  267. msg->comp = NULL;
  268. }
  269. /**
  270. * slim_read() - Read SLIMbus value element
  271. *
  272. * @sdev: client handle.
  273. * @addr: address of value element to read.
  274. * @count: number of bytes to read. Maximum bytes allowed are 16.
  275. * @val: will return what the value element value was
  276. *
  277. * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
  278. * this message timed out (e.g. due to bus lines not being clocked
  279. * or driven by controller)
  280. */
  281. int slim_read(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
  282. {
  283. struct slim_val_inf msg;
  284. slim_fill_msg(&msg, addr, count, val, NULL);
  285. return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_REQUEST_VALUE);
  286. }
  287. EXPORT_SYMBOL_GPL(slim_read);
  288. /**
  289. * slim_readb() - Read byte from SLIMbus value element
  290. *
  291. * @sdev: client handle.
  292. * @addr: address in the value element to read.
  293. *
  294. * Return: byte value of value element.
  295. */
  296. int slim_readb(struct slim_device *sdev, u32 addr)
  297. {
  298. int ret;
  299. u8 buf;
  300. ret = slim_read(sdev, addr, 1, &buf);
  301. if (ret < 0)
  302. return ret;
  303. else
  304. return buf;
  305. }
  306. EXPORT_SYMBOL_GPL(slim_readb);
  307. /**
  308. * slim_write() - Write SLIMbus value element
  309. *
  310. * @sdev: client handle.
  311. * @addr: address in the value element to write.
  312. * @count: number of bytes to write. Maximum bytes allowed are 16.
  313. * @val: value to write to value element
  314. *
  315. * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
  316. * this message timed out (e.g. due to bus lines not being clocked
  317. * or driven by controller)
  318. */
  319. int slim_write(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
  320. {
  321. struct slim_val_inf msg;
  322. slim_fill_msg(&msg, addr, count, NULL, val);
  323. return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_CHANGE_VALUE);
  324. }
  325. EXPORT_SYMBOL_GPL(slim_write);
  326. /**
  327. * slim_writeb() - Write byte to SLIMbus value element
  328. *
  329. * @sdev: client handle.
  330. * @addr: address of value element to write.
  331. * @value: value to write to value element
  332. *
  333. * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
  334. * this message timed out (e.g. due to bus lines not being clocked
  335. * or driven by controller)
  336. *
  337. */
  338. int slim_writeb(struct slim_device *sdev, u32 addr, u8 value)
  339. {
  340. return slim_write(sdev, addr, 1, &value);
  341. }
  342. EXPORT_SYMBOL_GPL(slim_writeb);