delta-ipc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2015
  4. * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
  5. */
  6. #include <linux/rpmsg.h>
  7. #include "delta.h"
  8. #include "delta-ipc.h"
  9. #include "delta-mem.h"
  10. #define IPC_TIMEOUT 100
  11. #define IPC_SANITY_TAG 0xDEADBEEF
  12. enum delta_ipc_fw_command {
  13. DELTA_IPC_OPEN,
  14. DELTA_IPC_SET_STREAM,
  15. DELTA_IPC_DECODE,
  16. DELTA_IPC_CLOSE
  17. };
  18. #define to_rpmsg_driver(__drv) container_of(__drv, struct rpmsg_driver, drv)
  19. #define to_delta(__d) container_of(__d, struct delta_dev, rpmsg_driver)
  20. #define to_ctx(hdl) ((struct delta_ipc_ctx *)hdl)
  21. #define to_pctx(ctx) container_of(ctx, struct delta_ctx, ipc_ctx)
  22. struct delta_ipc_header_msg {
  23. u32 tag;
  24. void *host_hdl;
  25. u32 copro_hdl;
  26. u32 command;
  27. };
  28. #define to_host_hdl(ctx) ((void *)ctx)
  29. #define msg_to_ctx(msg) ((struct delta_ipc_ctx *)(msg)->header.host_hdl)
  30. #define msg_to_copro_hdl(msg) ((msg)->header.copro_hdl)
  31. static inline dma_addr_t to_paddr(struct delta_ipc_ctx *ctx, void *vaddr)
  32. {
  33. return (ctx->ipc_buf->paddr + (vaddr - ctx->ipc_buf->vaddr));
  34. }
  35. static inline bool is_valid_data(struct delta_ipc_ctx *ctx,
  36. void *data, u32 size)
  37. {
  38. return ((data >= ctx->ipc_buf->vaddr) &&
  39. ((data + size) <= (ctx->ipc_buf->vaddr + ctx->ipc_buf->size)));
  40. }
  41. /*
  42. * IPC shared memory (@ipc_buf_size, @ipc_buf_paddr) is sent to copro
  43. * at each instance opening. This memory is allocated by IPC client
  44. * and given through delta_ipc_open(). All messages parameters
  45. * (open, set_stream, decode) will have their phy address within
  46. * this IPC shared memory, avoiding de-facto recopies inside delta-ipc.
  47. * All the below messages structures are used on both host and firmware
  48. * side and are packed (use only of 32 bits size fields in messages
  49. * structures to ensure packing):
  50. * - struct delta_ipc_open_msg
  51. * - struct delta_ipc_set_stream_msg
  52. * - struct delta_ipc_decode_msg
  53. * - struct delta_ipc_close_msg
  54. * - struct delta_ipc_cb_msg
  55. */
  56. struct delta_ipc_open_msg {
  57. struct delta_ipc_header_msg header;
  58. u32 ipc_buf_size;
  59. dma_addr_t ipc_buf_paddr;
  60. char name[32];
  61. u32 param_size;
  62. dma_addr_t param_paddr;
  63. };
  64. struct delta_ipc_set_stream_msg {
  65. struct delta_ipc_header_msg header;
  66. u32 param_size;
  67. dma_addr_t param_paddr;
  68. };
  69. struct delta_ipc_decode_msg {
  70. struct delta_ipc_header_msg header;
  71. u32 param_size;
  72. dma_addr_t param_paddr;
  73. u32 status_size;
  74. dma_addr_t status_paddr;
  75. };
  76. struct delta_ipc_close_msg {
  77. struct delta_ipc_header_msg header;
  78. };
  79. struct delta_ipc_cb_msg {
  80. struct delta_ipc_header_msg header;
  81. int err;
  82. };
  83. static void build_msg_header(struct delta_ipc_ctx *ctx,
  84. enum delta_ipc_fw_command command,
  85. struct delta_ipc_header_msg *header)
  86. {
  87. header->tag = IPC_SANITY_TAG;
  88. header->host_hdl = to_host_hdl(ctx);
  89. header->copro_hdl = ctx->copro_hdl;
  90. header->command = command;
  91. }
  92. int delta_ipc_open(struct delta_ctx *pctx, const char *name,
  93. struct delta_ipc_param *param, u32 ipc_buf_size,
  94. struct delta_buf **ipc_buf, void **hdl)
  95. {
  96. struct delta_dev *delta = pctx->dev;
  97. struct rpmsg_device *rpmsg_device = delta->rpmsg_device;
  98. struct delta_ipc_ctx *ctx = &pctx->ipc_ctx;
  99. struct delta_ipc_open_msg msg;
  100. struct delta_buf *buf = &ctx->ipc_buf_struct;
  101. int ret;
  102. if (!rpmsg_device) {
  103. dev_err(delta->dev,
  104. "%s ipc: failed to open, rpmsg is not initialized\n",
  105. pctx->name);
  106. pctx->sys_errors++;
  107. return -EINVAL;
  108. }
  109. if (!name) {
  110. dev_err(delta->dev,
  111. "%s ipc: failed to open, no name given\n",
  112. pctx->name);
  113. return -EINVAL;
  114. }
  115. if (!param || !param->data || !param->size) {
  116. dev_err(delta->dev,
  117. "%s ipc: failed to open, empty parameter\n",
  118. pctx->name);
  119. return -EINVAL;
  120. }
  121. if (!ipc_buf_size) {
  122. dev_err(delta->dev,
  123. "%s ipc: failed to open, no size given for ipc buffer\n",
  124. pctx->name);
  125. return -EINVAL;
  126. }
  127. if (param->size > ipc_buf_size) {
  128. dev_err(delta->dev,
  129. "%s ipc: failed to open, too large ipc parameter (%d bytes while max %d expected)\n",
  130. pctx->name,
  131. param->size, ctx->ipc_buf->size);
  132. return -EINVAL;
  133. }
  134. /* init */
  135. init_completion(&ctx->done);
  136. /*
  137. * allocation of contiguous buffer for
  138. * data of commands exchanged between
  139. * host and firmware coprocessor
  140. */
  141. ret = hw_alloc(pctx, ipc_buf_size,
  142. "ipc data buffer", buf);
  143. if (ret)
  144. return ret;
  145. ctx->ipc_buf = buf;
  146. /* build rpmsg message */
  147. build_msg_header(ctx, DELTA_IPC_OPEN, &msg.header);
  148. msg.ipc_buf_size = ipc_buf_size;
  149. msg.ipc_buf_paddr = ctx->ipc_buf->paddr;
  150. memcpy(msg.name, name, sizeof(msg.name));
  151. msg.name[sizeof(msg.name) - 1] = 0;
  152. msg.param_size = param->size;
  153. memcpy(ctx->ipc_buf->vaddr, param->data, msg.param_size);
  154. msg.param_paddr = ctx->ipc_buf->paddr;
  155. /* send it */
  156. ret = rpmsg_send(rpmsg_device->ept, &msg, sizeof(msg));
  157. if (ret) {
  158. dev_err(delta->dev,
  159. "%s ipc: failed to open, rpmsg_send failed (%d) for DELTA_IPC_OPEN (name=%s, size=%d, data=%p)\n",
  160. pctx->name,
  161. ret, name, param->size, param->data);
  162. goto err;
  163. }
  164. /* wait for acknowledge */
  165. if (!wait_for_completion_timeout
  166. (&ctx->done, msecs_to_jiffies(IPC_TIMEOUT))) {
  167. dev_err(delta->dev,
  168. "%s ipc: failed to open, timeout waiting for DELTA_IPC_OPEN callback (name=%s, size=%d, data=%p)\n",
  169. pctx->name,
  170. name, param->size, param->data);
  171. ret = -ETIMEDOUT;
  172. goto err;
  173. }
  174. /* command completed, check error */
  175. if (ctx->cb_err) {
  176. dev_err(delta->dev,
  177. "%s ipc: failed to open, DELTA_IPC_OPEN completed but with error (%d) (name=%s, size=%d, data=%p)\n",
  178. pctx->name,
  179. ctx->cb_err, name, param->size, param->data);
  180. ret = -EIO;
  181. goto err;
  182. }
  183. *ipc_buf = ctx->ipc_buf;
  184. *hdl = (void *)ctx;
  185. return 0;
  186. err:
  187. pctx->sys_errors++;
  188. if (ctx->ipc_buf) {
  189. hw_free(pctx, ctx->ipc_buf);
  190. ctx->ipc_buf = NULL;
  191. }
  192. return ret;
  193. };
  194. int delta_ipc_set_stream(void *hdl, struct delta_ipc_param *param)
  195. {
  196. struct delta_ipc_ctx *ctx = to_ctx(hdl);
  197. struct delta_ctx *pctx = to_pctx(ctx);
  198. struct delta_dev *delta = pctx->dev;
  199. struct rpmsg_device *rpmsg_device = delta->rpmsg_device;
  200. struct delta_ipc_set_stream_msg msg;
  201. int ret;
  202. if (!hdl) {
  203. dev_err(delta->dev,
  204. "%s ipc: failed to set stream, invalid ipc handle\n",
  205. pctx->name);
  206. return -EINVAL;
  207. }
  208. if (!rpmsg_device) {
  209. dev_err(delta->dev,
  210. "%s ipc: failed to set stream, rpmsg is not initialized\n",
  211. pctx->name);
  212. return -EINVAL;
  213. }
  214. if (!param || !param->data || !param->size) {
  215. dev_err(delta->dev,
  216. "%s ipc: failed to set stream, empty parameter\n",
  217. pctx->name);
  218. return -EINVAL;
  219. }
  220. if (param->size > ctx->ipc_buf->size) {
  221. dev_err(delta->dev,
  222. "%s ipc: failed to set stream, too large ipc parameter(%d bytes while max %d expected)\n",
  223. pctx->name,
  224. param->size, ctx->ipc_buf->size);
  225. return -EINVAL;
  226. }
  227. if (!is_valid_data(ctx, param->data, param->size)) {
  228. dev_err(delta->dev,
  229. "%s ipc: failed to set stream, parameter is not in expected address range (size=%d, data=%p not in %p..%p)\n",
  230. pctx->name,
  231. param->size,
  232. param->data,
  233. ctx->ipc_buf->vaddr,
  234. ctx->ipc_buf->vaddr + ctx->ipc_buf->size - 1);
  235. return -EINVAL;
  236. }
  237. /* build rpmsg message */
  238. build_msg_header(ctx, DELTA_IPC_SET_STREAM, &msg.header);
  239. msg.param_size = param->size;
  240. msg.param_paddr = to_paddr(ctx, param->data);
  241. /* send it */
  242. ret = rpmsg_send(rpmsg_device->ept, &msg, sizeof(msg));
  243. if (ret) {
  244. dev_err(delta->dev,
  245. "%s ipc: failed to set stream, rpmsg_send failed (%d) for DELTA_IPC_SET_STREAM (size=%d, data=%p)\n",
  246. pctx->name,
  247. ret, param->size, param->data);
  248. pctx->sys_errors++;
  249. return ret;
  250. }
  251. /* wait for acknowledge */
  252. if (!wait_for_completion_timeout
  253. (&ctx->done, msecs_to_jiffies(IPC_TIMEOUT))) {
  254. dev_err(delta->dev,
  255. "%s ipc: failed to set stream, timeout waiting for DELTA_IPC_SET_STREAM callback (size=%d, data=%p)\n",
  256. pctx->name,
  257. param->size, param->data);
  258. pctx->sys_errors++;
  259. return -ETIMEDOUT;
  260. }
  261. /* command completed, check status */
  262. if (ctx->cb_err) {
  263. dev_err(delta->dev,
  264. "%s ipc: failed to set stream, DELTA_IPC_SET_STREAM completed but with error (%d) (size=%d, data=%p)\n",
  265. pctx->name,
  266. ctx->cb_err, param->size, param->data);
  267. pctx->sys_errors++;
  268. return -EIO;
  269. }
  270. return 0;
  271. }
  272. int delta_ipc_decode(void *hdl, struct delta_ipc_param *param,
  273. struct delta_ipc_param *status)
  274. {
  275. struct delta_ipc_ctx *ctx = to_ctx(hdl);
  276. struct delta_ctx *pctx = to_pctx(ctx);
  277. struct delta_dev *delta = pctx->dev;
  278. struct rpmsg_device *rpmsg_device = delta->rpmsg_device;
  279. struct delta_ipc_decode_msg msg;
  280. int ret;
  281. if (!hdl) {
  282. dev_err(delta->dev,
  283. "%s ipc: failed to decode, invalid ipc handle\n",
  284. pctx->name);
  285. return -EINVAL;
  286. }
  287. if (!rpmsg_device) {
  288. dev_err(delta->dev,
  289. "%s ipc: failed to decode, rpmsg is not initialized\n",
  290. pctx->name);
  291. return -EINVAL;
  292. }
  293. if (!param || !param->data || !param->size) {
  294. dev_err(delta->dev,
  295. "%s ipc: failed to decode, empty parameter\n",
  296. pctx->name);
  297. return -EINVAL;
  298. }
  299. if (!status || !status->data || !status->size) {
  300. dev_err(delta->dev,
  301. "%s ipc: failed to decode, empty status\n",
  302. pctx->name);
  303. return -EINVAL;
  304. }
  305. if (param->size + status->size > ctx->ipc_buf->size) {
  306. dev_err(delta->dev,
  307. "%s ipc: failed to decode, too large ipc parameter (%d bytes (param) + %d bytes (status) while max %d expected)\n",
  308. pctx->name,
  309. param->size,
  310. status->size,
  311. ctx->ipc_buf->size);
  312. return -EINVAL;
  313. }
  314. if (!is_valid_data(ctx, param->data, param->size)) {
  315. dev_err(delta->dev,
  316. "%s ipc: failed to decode, parameter is not in expected address range (size=%d, data=%p not in %p..%p)\n",
  317. pctx->name,
  318. param->size,
  319. param->data,
  320. ctx->ipc_buf->vaddr,
  321. ctx->ipc_buf->vaddr + ctx->ipc_buf->size - 1);
  322. return -EINVAL;
  323. }
  324. if (!is_valid_data(ctx, status->data, status->size)) {
  325. dev_err(delta->dev,
  326. "%s ipc: failed to decode, status is not in expected address range (size=%d, data=%p not in %p..%p)\n",
  327. pctx->name,
  328. status->size,
  329. status->data,
  330. ctx->ipc_buf->vaddr,
  331. ctx->ipc_buf->vaddr + ctx->ipc_buf->size - 1);
  332. return -EINVAL;
  333. }
  334. /* build rpmsg message */
  335. build_msg_header(ctx, DELTA_IPC_DECODE, &msg.header);
  336. msg.param_size = param->size;
  337. msg.param_paddr = to_paddr(ctx, param->data);
  338. msg.status_size = status->size;
  339. msg.status_paddr = to_paddr(ctx, status->data);
  340. /* send it */
  341. ret = rpmsg_send(rpmsg_device->ept, &msg, sizeof(msg));
  342. if (ret) {
  343. dev_err(delta->dev,
  344. "%s ipc: failed to decode, rpmsg_send failed (%d) for DELTA_IPC_DECODE (size=%d, data=%p)\n",
  345. pctx->name,
  346. ret, param->size, param->data);
  347. pctx->sys_errors++;
  348. return ret;
  349. }
  350. /* wait for acknowledge */
  351. if (!wait_for_completion_timeout
  352. (&ctx->done, msecs_to_jiffies(IPC_TIMEOUT))) {
  353. dev_err(delta->dev,
  354. "%s ipc: failed to decode, timeout waiting for DELTA_IPC_DECODE callback (size=%d, data=%p)\n",
  355. pctx->name,
  356. param->size, param->data);
  357. pctx->sys_errors++;
  358. return -ETIMEDOUT;
  359. }
  360. /* command completed, check status */
  361. if (ctx->cb_err) {
  362. dev_err(delta->dev,
  363. "%s ipc: failed to decode, DELTA_IPC_DECODE completed but with error (%d) (size=%d, data=%p)\n",
  364. pctx->name,
  365. ctx->cb_err, param->size, param->data);
  366. pctx->sys_errors++;
  367. return -EIO;
  368. }
  369. return 0;
  370. };
  371. void delta_ipc_close(void *hdl)
  372. {
  373. struct delta_ipc_ctx *ctx = to_ctx(hdl);
  374. struct delta_ctx *pctx = to_pctx(ctx);
  375. struct delta_dev *delta = pctx->dev;
  376. struct rpmsg_device *rpmsg_device = delta->rpmsg_device;
  377. struct delta_ipc_close_msg msg;
  378. int ret;
  379. if (!hdl) {
  380. dev_err(delta->dev,
  381. "%s ipc: failed to close, invalid ipc handle\n",
  382. pctx->name);
  383. return;
  384. }
  385. if (ctx->ipc_buf) {
  386. hw_free(pctx, ctx->ipc_buf);
  387. ctx->ipc_buf = NULL;
  388. }
  389. if (!rpmsg_device) {
  390. dev_err(delta->dev,
  391. "%s ipc: failed to close, rpmsg is not initialized\n",
  392. pctx->name);
  393. return;
  394. }
  395. /* build rpmsg message */
  396. build_msg_header(ctx, DELTA_IPC_CLOSE, &msg.header);
  397. /* send it */
  398. ret = rpmsg_send(rpmsg_device->ept, &msg, sizeof(msg));
  399. if (ret) {
  400. dev_err(delta->dev,
  401. "%s ipc: failed to close, rpmsg_send failed (%d) for DELTA_IPC_CLOSE\n",
  402. pctx->name, ret);
  403. pctx->sys_errors++;
  404. return;
  405. }
  406. /* wait for acknowledge */
  407. if (!wait_for_completion_timeout
  408. (&ctx->done, msecs_to_jiffies(IPC_TIMEOUT))) {
  409. dev_err(delta->dev,
  410. "%s ipc: failed to close, timeout waiting for DELTA_IPC_CLOSE callback\n",
  411. pctx->name);
  412. pctx->sys_errors++;
  413. return;
  414. }
  415. /* command completed, check status */
  416. if (ctx->cb_err) {
  417. dev_err(delta->dev,
  418. "%s ipc: failed to close, DELTA_IPC_CLOSE completed but with error (%d)\n",
  419. pctx->name, ctx->cb_err);
  420. pctx->sys_errors++;
  421. }
  422. };
  423. static int delta_ipc_cb(struct rpmsg_device *rpdev, void *data,
  424. int len, void *priv, u32 src)
  425. {
  426. struct delta_ipc_ctx *ctx;
  427. struct delta_ipc_cb_msg *msg;
  428. /* sanity check */
  429. if (!rpdev) {
  430. dev_err(NULL, "rpdev is NULL\n");
  431. return -EINVAL;
  432. }
  433. if (!data || !len) {
  434. dev_err(&rpdev->dev,
  435. "unexpected empty message received from src=%d\n", src);
  436. return -EINVAL;
  437. }
  438. if (len != sizeof(*msg)) {
  439. dev_err(&rpdev->dev,
  440. "unexpected message length received from src=%d (received %d bytes while %zu bytes expected)\n",
  441. len, src, sizeof(*msg));
  442. return -EINVAL;
  443. }
  444. msg = (struct delta_ipc_cb_msg *)data;
  445. if (msg->header.tag != IPC_SANITY_TAG) {
  446. dev_err(&rpdev->dev,
  447. "unexpected message tag received from src=%d (received %x tag while %x expected)\n",
  448. src, msg->header.tag, IPC_SANITY_TAG);
  449. return -EINVAL;
  450. }
  451. ctx = msg_to_ctx(msg);
  452. if (!ctx) {
  453. dev_err(&rpdev->dev,
  454. "unexpected message with NULL host_hdl received from src=%d\n",
  455. src);
  456. return -EINVAL;
  457. }
  458. /*
  459. * if not already known, save copro instance context
  460. * to ensure re-entrance on copro side
  461. */
  462. if (!ctx->copro_hdl)
  463. ctx->copro_hdl = msg_to_copro_hdl(msg);
  464. /*
  465. * all is fine,
  466. * update status & complete command
  467. */
  468. ctx->cb_err = msg->err;
  469. complete(&ctx->done);
  470. return 0;
  471. }
  472. static int delta_ipc_probe(struct rpmsg_device *rpmsg_device)
  473. {
  474. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpmsg_device->dev.driver);
  475. struct delta_dev *delta = to_delta(rpdrv);
  476. delta->rpmsg_device = rpmsg_device;
  477. return 0;
  478. }
  479. static void delta_ipc_remove(struct rpmsg_device *rpmsg_device)
  480. {
  481. struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpmsg_device->dev.driver);
  482. struct delta_dev *delta = to_delta(rpdrv);
  483. delta->rpmsg_device = NULL;
  484. }
  485. static struct rpmsg_device_id delta_ipc_device_id_table[] = {
  486. {.name = "rpmsg-delta"},
  487. {},
  488. };
  489. static struct rpmsg_driver delta_rpmsg_driver = {
  490. .drv = {.name = KBUILD_MODNAME},
  491. .id_table = delta_ipc_device_id_table,
  492. .probe = delta_ipc_probe,
  493. .callback = delta_ipc_cb,
  494. .remove = delta_ipc_remove,
  495. };
  496. int delta_ipc_init(struct delta_dev *delta)
  497. {
  498. delta->rpmsg_driver = delta_rpmsg_driver;
  499. return register_rpmsg_driver(&delta->rpmsg_driver);
  500. }
  501. void delta_ipc_exit(struct delta_dev *delta)
  502. {
  503. unregister_rpmsg_driver(&delta->rpmsg_driver);
  504. }