messaging.c 9.1 KB

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