smp2p_loopback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* arch/arm/mach-msm/smp2p_loopback.c
  2. *
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/debugfs.h>
  15. #include <linux/list.h>
  16. #include <linux/ctype.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/completion.h>
  21. #include <linux/termios.h>
  22. #include <linux/module.h>
  23. #include <linux/remote_spinlock.h>
  24. #include "smem_private.h"
  25. #include "smp2p_private.h"
  26. /**
  27. * struct smp2p_loopback_ctx - Representation of remote loopback object.
  28. *
  29. * @proc_id: Processor id of the processor that sends the loopback commands.
  30. * @out: Handle to the smem entry structure for providing the response.
  31. * @out_nb: Notifies the opening of local entry.
  32. * @out_is_active: Outbound entry events should be processed.
  33. * @in_nb: Notifies changes in the remote entry.
  34. * @in_is_active: Inbound entry events should be processed.
  35. * @rmt_lpb_work: Work item that handles the incoming loopback commands.
  36. * @rmt_cmd: Structure that holds the current and previous value of the entry.
  37. */
  38. struct smp2p_loopback_ctx {
  39. int proc_id;
  40. struct msm_smp2p_out *out;
  41. struct notifier_block out_nb;
  42. bool out_is_active;
  43. struct notifier_block in_nb;
  44. bool in_is_active;
  45. struct work_struct rmt_lpb_work;
  46. struct msm_smp2p_update_notif rmt_cmd;
  47. };
  48. static struct smp2p_loopback_ctx remote_loopback[SMP2P_NUM_PROCS];
  49. static struct msm_smp2p_remote_mock remote_mock;
  50. /**
  51. * remote_spinlock_test - Handles remote spinlock test.
  52. *
  53. * @ctx: Loopback context
  54. */
  55. static void remote_spinlock_test(struct smp2p_loopback_ctx *ctx)
  56. {
  57. uint32_t test_request;
  58. uint32_t test_response;
  59. unsigned long flags;
  60. int n;
  61. unsigned lock_count = 0;
  62. remote_spinlock_t *smem_spinlock;
  63. test_request = 0x0;
  64. SMP2P_SET_RMT_CMD_TYPE_REQ(test_request);
  65. smem_spinlock = smem_get_remote_spinlock();
  66. if (!smem_spinlock) {
  67. pr_err("%s: unable to get remote spinlock\n", __func__);
  68. return;
  69. }
  70. for (;;) {
  71. remote_spin_lock_irqsave(smem_spinlock, flags);
  72. ++lock_count;
  73. SMP2P_SET_RMT_CMD(test_request, SMP2P_LB_CMD_RSPIN_LOCKED);
  74. (void)msm_smp2p_out_write(ctx->out, test_request);
  75. for (n = 0; n < 10000; ++n) {
  76. (void)msm_smp2p_in_read(ctx->proc_id,
  77. "smp2p", &test_response);
  78. test_response = SMP2P_GET_RMT_CMD(test_response);
  79. if (test_response == SMP2P_LB_CMD_RSPIN_END)
  80. break;
  81. if (test_response != SMP2P_LB_CMD_RSPIN_UNLOCKED)
  82. SMP2P_ERR("%s: invalid spinlock command %x\n",
  83. __func__, test_response);
  84. }
  85. if (test_response == SMP2P_LB_CMD_RSPIN_END) {
  86. SMP2P_SET_RMT_CMD_TYPE_RESP(test_request);
  87. SMP2P_SET_RMT_CMD(test_request,
  88. SMP2P_LB_CMD_RSPIN_END);
  89. SMP2P_SET_RMT_DATA(test_request, lock_count);
  90. (void)msm_smp2p_out_write(ctx->out, test_request);
  91. break;
  92. }
  93. SMP2P_SET_RMT_CMD(test_request, SMP2P_LB_CMD_RSPIN_UNLOCKED);
  94. (void)msm_smp2p_out_write(ctx->out, test_request);
  95. remote_spin_unlock_irqrestore(smem_spinlock, flags);
  96. }
  97. remote_spin_unlock_irqrestore(smem_spinlock, flags);
  98. }
  99. /**
  100. * smp2p_rmt_lpb_worker - Handles incoming remote loopback commands.
  101. *
  102. * @work: Work Item scheduled to handle the incoming commands.
  103. */
  104. static void smp2p_rmt_lpb_worker(struct work_struct *work)
  105. {
  106. struct smp2p_loopback_ctx *ctx;
  107. int lpb_cmd;
  108. int lpb_cmd_type;
  109. int lpb_data;
  110. ctx = container_of(work, struct smp2p_loopback_ctx, rmt_lpb_work);
  111. if (!ctx->in_is_active || !ctx->out_is_active)
  112. return;
  113. if (ctx->rmt_cmd.previous_value == ctx->rmt_cmd.current_value)
  114. return;
  115. lpb_cmd_type = SMP2P_GET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value);
  116. lpb_cmd = SMP2P_GET_RMT_CMD(ctx->rmt_cmd.current_value);
  117. lpb_data = SMP2P_GET_RMT_DATA(ctx->rmt_cmd.current_value);
  118. if (lpb_cmd & SMP2P_RLPB_IGNORE)
  119. return;
  120. switch (lpb_cmd) {
  121. case SMP2P_LB_CMD_NOOP:
  122. /* Do nothing */
  123. break;
  124. case SMP2P_LB_CMD_ECHO:
  125. SMP2P_SET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value, 0);
  126. SMP2P_SET_RMT_DATA(ctx->rmt_cmd.current_value,
  127. lpb_data);
  128. (void)msm_smp2p_out_write(ctx->out,
  129. ctx->rmt_cmd.current_value);
  130. break;
  131. case SMP2P_LB_CMD_CLEARALL:
  132. ctx->rmt_cmd.current_value = 0;
  133. (void)msm_smp2p_out_write(ctx->out,
  134. ctx->rmt_cmd.current_value);
  135. break;
  136. case SMP2P_LB_CMD_PINGPONG:
  137. SMP2P_SET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value, 0);
  138. if (lpb_data) {
  139. lpb_data--;
  140. SMP2P_SET_RMT_DATA(ctx->rmt_cmd.current_value,
  141. lpb_data);
  142. (void)msm_smp2p_out_write(ctx->out,
  143. ctx->rmt_cmd.current_value);
  144. }
  145. break;
  146. case SMP2P_LB_CMD_RSPIN_START:
  147. remote_spinlock_test(ctx);
  148. break;
  149. case SMP2P_LB_CMD_RSPIN_LOCKED:
  150. case SMP2P_LB_CMD_RSPIN_UNLOCKED:
  151. case SMP2P_LB_CMD_RSPIN_END:
  152. /* not used for remote spinlock test */
  153. break;
  154. default:
  155. SMP2P_DBG("%s: Unknown loopback command %x\n",
  156. __func__, lpb_cmd);
  157. break;
  158. }
  159. }
  160. /**
  161. * smp2p_rmt_in_edge_notify - Schedules a work item to handle the commands.
  162. *
  163. * @nb: Notifier block, this is called when the value in remote entry changes.
  164. * @event: Takes value SMP2P_ENTRY_UPDATE or SMP2P_OPEN based on the event.
  165. * @data: Consists of previous and current value in case of entry update.
  166. * @returns: 0 for success (return value required for notifier chains).
  167. */
  168. static int smp2p_rmt_in_edge_notify(struct notifier_block *nb,
  169. unsigned long event, void *data)
  170. {
  171. struct smp2p_loopback_ctx *ctx;
  172. if (!(event == SMP2P_ENTRY_UPDATE || event == SMP2P_OPEN))
  173. return 0;
  174. ctx = container_of(nb, struct smp2p_loopback_ctx, in_nb);
  175. if (data && ctx->in_is_active) {
  176. ctx->rmt_cmd =
  177. *(struct msm_smp2p_update_notif *)data;
  178. schedule_work(&ctx->rmt_lpb_work);
  179. }
  180. return 0;
  181. }
  182. /**
  183. * smp2p_rmt_out_edge_notify - Notifies on the opening of the outbound entry.
  184. *
  185. * @nb: Notifier block, this is called when the local entry is open.
  186. * @event: Takes on value SMP2P_OPEN when the local entry is open.
  187. * @data: Consist of current value of the remote entry, if entry is open.
  188. * @returns: 0 for success (return value required for notifier chains).
  189. */
  190. static int smp2p_rmt_out_edge_notify(struct notifier_block *nb,
  191. unsigned long event, void *data)
  192. {
  193. struct smp2p_loopback_ctx *ctx;
  194. ctx = container_of(nb, struct smp2p_loopback_ctx, out_nb);
  195. if (event == SMP2P_OPEN)
  196. SMP2P_DBG("%s: 'smp2p':%d opened\n", __func__,
  197. ctx->proc_id);
  198. return 0;
  199. }
  200. /**
  201. * msm_smp2p_init_rmt_lpb - Initializes the remote loopback object.
  202. *
  203. * @ctx: Pointer to remote loopback object that needs to be initialized.
  204. * @pid: Processor id of the processor that is sending the commands.
  205. * @entry: Name of the entry that needs to be opened locally.
  206. * @returns: 0 on success, standard Linux error code otherwise.
  207. */
  208. static int msm_smp2p_init_rmt_lpb(struct smp2p_loopback_ctx *ctx,
  209. int pid, const char *entry)
  210. {
  211. int ret = 0;
  212. int tmp;
  213. if (!ctx || !entry || pid > SMP2P_NUM_PROCS)
  214. return -EINVAL;
  215. ctx->in_nb.notifier_call = smp2p_rmt_in_edge_notify;
  216. ctx->out_nb.notifier_call = smp2p_rmt_out_edge_notify;
  217. ctx->proc_id = pid;
  218. ctx->in_is_active = true;
  219. ctx->out_is_active = true;
  220. tmp = msm_smp2p_out_open(pid, entry, &ctx->out_nb,
  221. &ctx->out);
  222. if (tmp) {
  223. SMP2P_ERR("%s: open failed outbound entry '%s':%d - ret %d\n",
  224. __func__, entry, pid, tmp);
  225. ret = tmp;
  226. }
  227. tmp = msm_smp2p_in_register(ctx->proc_id,
  228. SMP2P_RLPB_ENTRY_NAME,
  229. &ctx->in_nb);
  230. if (tmp) {
  231. SMP2P_ERR("%s: unable to open inbound entry '%s':%d - ret %d\n",
  232. __func__, entry, pid, tmp);
  233. ret = tmp;
  234. }
  235. return ret;
  236. }
  237. /**
  238. * msm_smp2p_init_rmt_lpb_proc - Wrapper over msm_smp2p_init_rmt_lpb
  239. *
  240. * @remote_pid: Processor ID of the processor that sends loopback command.
  241. * @returns: Pointer to outbound entry handle.
  242. */
  243. void *msm_smp2p_init_rmt_lpb_proc(int remote_pid)
  244. {
  245. int tmp;
  246. void *ret = NULL;
  247. tmp = msm_smp2p_init_rmt_lpb(&remote_loopback[remote_pid],
  248. remote_pid, SMP2P_RLPB_ENTRY_NAME);
  249. if (!tmp)
  250. ret = remote_loopback[remote_pid].out;
  251. return ret;
  252. }
  253. EXPORT_SYMBOL(msm_smp2p_init_rmt_lpb_proc);
  254. /**
  255. * msm_smp2p_deinit_rmt_lpb_proc - Unregister support for remote processor.
  256. *
  257. * @remote_pid: Processor ID of the remote system.
  258. * @returns: 0 on success, standard Linux error code otherwise.
  259. *
  260. * Unregister loopback support for remote processor.
  261. */
  262. int msm_smp2p_deinit_rmt_lpb_proc(int remote_pid)
  263. {
  264. int ret = 0;
  265. int tmp;
  266. struct smp2p_loopback_ctx *ctx;
  267. if (remote_pid >= SMP2P_NUM_PROCS)
  268. return -EINVAL;
  269. ctx = &remote_loopback[remote_pid];
  270. /* abort any pending notifications */
  271. remote_loopback[remote_pid].out_is_active = false;
  272. remote_loopback[remote_pid].in_is_active = false;
  273. flush_work(&ctx->rmt_lpb_work);
  274. /* unregister entries */
  275. tmp = msm_smp2p_out_close(&remote_loopback[remote_pid].out);
  276. remote_loopback[remote_pid].out = NULL;
  277. if (tmp) {
  278. SMP2P_ERR("%s: outbound 'smp2p':%d close failed %d\n",
  279. __func__, remote_pid, tmp);
  280. ret = tmp;
  281. }
  282. tmp = msm_smp2p_in_unregister(remote_pid,
  283. SMP2P_RLPB_ENTRY_NAME, &remote_loopback[remote_pid].in_nb);
  284. if (tmp) {
  285. SMP2P_ERR("%s: inbound 'smp2p':%d close failed %d\n",
  286. __func__, remote_pid, tmp);
  287. ret = tmp;
  288. }
  289. return ret;
  290. }
  291. EXPORT_SYMBOL(msm_smp2p_deinit_rmt_lpb_proc);
  292. /**
  293. * msm_smp2p_set_remote_mock_exists - Sets the remote mock configuration.
  294. *
  295. * @item_exists: true = Remote mock SMEM item exists
  296. *
  297. * This is used in the testing environment to simulate the existence of the
  298. * remote smem item in order to test the negotiation algorithm.
  299. */
  300. void msm_smp2p_set_remote_mock_exists(bool item_exists)
  301. {
  302. remote_mock.item_exists = item_exists;
  303. }
  304. EXPORT_SYMBOL(msm_smp2p_set_remote_mock_exists);
  305. /**
  306. * msm_smp2p_get_remote_mock - Get remote mock object.
  307. *
  308. * @returns: Point to the remote mock object.
  309. */
  310. void *msm_smp2p_get_remote_mock(void)
  311. {
  312. return &remote_mock;
  313. }
  314. EXPORT_SYMBOL(msm_smp2p_get_remote_mock);
  315. /**
  316. * msm_smp2p_get_remote_mock_smem_item - Returns a pointer to remote item.
  317. *
  318. * @size: Size of item.
  319. * @returns: Pointer to mock remote smem item.
  320. */
  321. void *msm_smp2p_get_remote_mock_smem_item(uint32_t *size)
  322. {
  323. void *ptr = NULL;
  324. if (remote_mock.item_exists) {
  325. *size = sizeof(remote_mock.remote_item);
  326. ptr = &(remote_mock.remote_item);
  327. }
  328. return ptr;
  329. }
  330. EXPORT_SYMBOL(msm_smp2p_get_remote_mock_smem_item);
  331. /**
  332. * smp2p_remote_mock_rx_interrupt - Triggers receive interrupt for mock proc.
  333. *
  334. * @returns: 0 for success
  335. *
  336. * This function simulates the receiving of interrupt by the mock remote
  337. * processor in a testing environment.
  338. */
  339. int smp2p_remote_mock_rx_interrupt(void)
  340. {
  341. remote_mock.rx_interrupt_count++;
  342. if (remote_mock.initialized)
  343. complete(&remote_mock.cb_completion);
  344. return 0;
  345. }
  346. EXPORT_SYMBOL(smp2p_remote_mock_rx_interrupt);
  347. /**
  348. * smp2p_remote_mock_tx_interrupt - Calls the SMP2P interrupt handler.
  349. *
  350. * This function calls the interrupt handler of the Apps processor to simulate
  351. * receiving interrupts from a remote processor.
  352. */
  353. static void smp2p_remote_mock_tx_interrupt(void)
  354. {
  355. msm_smp2p_interrupt_handler(SMP2P_REMOTE_MOCK_PROC);
  356. }
  357. /**
  358. * smp2p_remote_mock_init - Initialize the remote mock and loopback objects.
  359. *
  360. * @returns: 0 for success
  361. */
  362. static int __init smp2p_remote_mock_init(void)
  363. {
  364. int i;
  365. smp2p_init_header(&remote_mock.remote_item.header,
  366. SMP2P_REMOTE_MOCK_PROC, SMP2P_APPS_PROC,
  367. 0, 0);
  368. remote_mock.rx_interrupt_count = 0;
  369. remote_mock.rx_interrupt = smp2p_remote_mock_rx_interrupt;
  370. remote_mock.tx_interrupt = smp2p_remote_mock_tx_interrupt;
  371. remote_mock.item_exists = false;
  372. init_completion(&remote_mock.cb_completion);
  373. remote_mock.initialized = true;
  374. for (i = 0; i < SMP2P_NUM_PROCS; i++) {
  375. INIT_WORK(&(remote_loopback[i].rmt_lpb_work),
  376. smp2p_rmt_lpb_worker);
  377. if (i == SMP2P_REMOTE_MOCK_PROC)
  378. /* do not register loopback for remote mock proc */
  379. continue;
  380. msm_smp2p_init_rmt_lpb(&remote_loopback[i],
  381. i, SMP2P_RLPB_ENTRY_NAME);
  382. }
  383. return 0;
  384. }
  385. module_init(smp2p_remote_mock_init);