messaging.c 8.4 KB

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