messaging.c 9.2 KB

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