messaging.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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, 1,
  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() - Free 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. transmitting 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;
  103. unsigned long time_left;
  104. /*
  105. * do not vote for runtime-PM if the transactions are part of clock
  106. * pause sequence
  107. */
  108. if (ctrl->sched.clk_state == SLIM_CLK_ENTERING_PAUSE &&
  109. (txn->mt == SLIM_MSG_MT_CORE &&
  110. txn->mc >= SLIM_MSG_MC_BEGIN_RECONFIGURATION &&
  111. txn->mc <= SLIM_MSG_MC_RECONFIGURE_NOW))
  112. clk_pause_msg = true;
  113. if (!clk_pause_msg) {
  114. ret = pm_runtime_get_sync(ctrl->dev);
  115. if (ctrl->sched.clk_state != SLIM_CLK_ACTIVE) {
  116. dev_err(ctrl->dev, "ctrl wrong state:%d, ret:%d\n",
  117. ctrl->sched.clk_state, ret);
  118. goto slim_xfer_err;
  119. }
  120. }
  121. /* Initialize tid to invalid value */
  122. txn->tid = 0;
  123. need_tid = slim_tid_txn(txn->mt, txn->mc);
  124. if (need_tid) {
  125. ret = slim_alloc_txn_tid(ctrl, txn);
  126. if (ret)
  127. return ret;
  128. if (!txn->msg->comp)
  129. txn->comp = &done;
  130. else
  131. txn->comp = txn->comp;
  132. }
  133. ret = ctrl->xfer_msg(ctrl, txn);
  134. if (!ret && need_tid && !txn->msg->comp) {
  135. unsigned long ms = txn->rl + HZ;
  136. time_left = wait_for_completion_timeout(txn->comp,
  137. msecs_to_jiffies(ms));
  138. if (!time_left) {
  139. ret = -ETIMEDOUT;
  140. slim_free_txn_tid(ctrl, txn);
  141. }
  142. }
  143. if (ret)
  144. dev_err(ctrl->dev, "Tx:MT:0x%x, MC:0x%x, LA:0x%x failed:%d\n",
  145. txn->mt, txn->mc, txn->la, ret);
  146. slim_xfer_err:
  147. if (!clk_pause_msg && (txn->tid == 0 || ret == -ETIMEDOUT)) {
  148. /*
  149. * remove runtime-pm vote if this was TX only, or
  150. * if there was error during this transaction
  151. */
  152. pm_runtime_mark_last_busy(ctrl->dev);
  153. pm_runtime_put_autosuspend(ctrl->dev);
  154. }
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(slim_do_transfer);
  158. static int slim_val_inf_sanity(struct slim_controller *ctrl,
  159. struct slim_val_inf *msg, u8 mc)
  160. {
  161. if (!msg || msg->num_bytes > 16 ||
  162. (msg->start_offset + msg->num_bytes) > 0xC00)
  163. goto reterr;
  164. switch (mc) {
  165. case SLIM_MSG_MC_REQUEST_VALUE:
  166. case SLIM_MSG_MC_REQUEST_INFORMATION:
  167. if (msg->rbuf != NULL)
  168. return 0;
  169. break;
  170. case SLIM_MSG_MC_CHANGE_VALUE:
  171. case SLIM_MSG_MC_CLEAR_INFORMATION:
  172. if (msg->wbuf != NULL)
  173. return 0;
  174. break;
  175. case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
  176. case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
  177. if (msg->rbuf != NULL && msg->wbuf != NULL)
  178. return 0;
  179. break;
  180. }
  181. reterr:
  182. if (msg)
  183. dev_err(ctrl->dev, "Sanity check failed:msg:offset:0x%x, mc:%d\n",
  184. msg->start_offset, mc);
  185. return -EINVAL;
  186. }
  187. static u16 slim_slicesize(int code)
  188. {
  189. static const u8 sizetocode[16] = {
  190. 0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7
  191. };
  192. code = clamp(code, 1, (int)ARRAY_SIZE(sizetocode));
  193. return sizetocode[code - 1];
  194. }
  195. /**
  196. * slim_xfer_msg() - Transfer a value info message on slim device
  197. *
  198. * @sbdev: slim device to which this msg has to be transfered
  199. * @msg: value info message pointer
  200. * @mc: message code of the message
  201. *
  202. * Called by drivers which want to transfer a vlaue or info elements.
  203. *
  204. * Return: -ETIMEDOUT: If transmission of this message timed out
  205. */
  206. int slim_xfer_msg(struct slim_device *sbdev, struct slim_val_inf *msg,
  207. u8 mc)
  208. {
  209. DEFINE_SLIM_LDEST_TXN(txn_stack, mc, 6, sbdev->laddr, msg);
  210. struct slim_msg_txn *txn = &txn_stack;
  211. struct slim_controller *ctrl = sbdev->ctrl;
  212. int ret;
  213. u16 sl;
  214. if (!ctrl)
  215. return -EINVAL;
  216. ret = slim_val_inf_sanity(ctrl, msg, mc);
  217. if (ret)
  218. return ret;
  219. sl = slim_slicesize(msg->num_bytes);
  220. dev_dbg(ctrl->dev, "SB xfer msg:os:%x, len:%d, MC:%x, sl:%x\n",
  221. msg->start_offset, msg->num_bytes, mc, sl);
  222. txn->ec = ((sl | (1 << 3)) | ((msg->start_offset & 0xFFF) << 4));
  223. switch (mc) {
  224. case SLIM_MSG_MC_REQUEST_CHANGE_VALUE:
  225. case SLIM_MSG_MC_CHANGE_VALUE:
  226. case SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION:
  227. case SLIM_MSG_MC_CLEAR_INFORMATION:
  228. txn->rl += msg->num_bytes;
  229. break;
  230. default:
  231. break;
  232. }
  233. if (slim_tid_txn(txn->mt, txn->mc))
  234. txn->rl++;
  235. return slim_do_transfer(ctrl, txn);
  236. }
  237. EXPORT_SYMBOL_GPL(slim_xfer_msg);
  238. static void slim_fill_msg(struct slim_val_inf *msg, u32 addr,
  239. size_t count, u8 *rbuf, u8 *wbuf)
  240. {
  241. msg->start_offset = addr;
  242. msg->num_bytes = count;
  243. msg->rbuf = rbuf;
  244. msg->wbuf = wbuf;
  245. msg->comp = NULL;
  246. }
  247. /**
  248. * slim_read() - Read SLIMbus value element
  249. *
  250. * @sdev: client handle.
  251. * @addr: address of value element to read.
  252. * @count: number of bytes to read. Maximum bytes allowed are 16.
  253. * @val: will return what the value element value was
  254. *
  255. * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
  256. * this message timed out (e.g. due to bus lines not being clocked
  257. * or driven by controller)
  258. */
  259. int slim_read(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
  260. {
  261. struct slim_val_inf msg;
  262. slim_fill_msg(&msg, addr, count, val, NULL);
  263. return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_REQUEST_VALUE);
  264. }
  265. EXPORT_SYMBOL_GPL(slim_read);
  266. /**
  267. * slim_readb() - Read byte from SLIMbus value element
  268. *
  269. * @sdev: client handle.
  270. * @addr: address in the value element to read.
  271. *
  272. * Return: byte value of value element.
  273. */
  274. int slim_readb(struct slim_device *sdev, u32 addr)
  275. {
  276. int ret;
  277. u8 buf;
  278. ret = slim_read(sdev, addr, 1, &buf);
  279. if (ret < 0)
  280. return ret;
  281. else
  282. return buf;
  283. }
  284. EXPORT_SYMBOL_GPL(slim_readb);
  285. /**
  286. * slim_write() - Write SLIMbus value element
  287. *
  288. * @sdev: client handle.
  289. * @addr: address in the value element to write.
  290. * @count: number of bytes to write. Maximum bytes allowed are 16.
  291. * @val: value to write to value element
  292. *
  293. * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
  294. * this message timed out (e.g. due to bus lines not being clocked
  295. * or driven by controller)
  296. */
  297. int slim_write(struct slim_device *sdev, u32 addr, size_t count, u8 *val)
  298. {
  299. struct slim_val_inf msg;
  300. slim_fill_msg(&msg, addr, count, NULL, val);
  301. return slim_xfer_msg(sdev, &msg, SLIM_MSG_MC_CHANGE_VALUE);
  302. }
  303. EXPORT_SYMBOL_GPL(slim_write);
  304. /**
  305. * slim_writeb() - Write byte to SLIMbus value element
  306. *
  307. * @sdev: client handle.
  308. * @addr: address of value element to write.
  309. * @value: value to write to value element
  310. *
  311. * Return: -EINVAL for Invalid parameters, -ETIMEDOUT If transmission of
  312. * this message timed out (e.g. due to bus lines not being clocked
  313. * or driven by controller)
  314. *
  315. */
  316. int slim_writeb(struct slim_device *sdev, u32 addr, u8 value)
  317. {
  318. return slim_write(sdev, addr, 1, &value);
  319. }
  320. EXPORT_SYMBOL_GPL(slim_writeb);