xen_snd_front_evtchnl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Xen para-virtual sound device
  4. *
  5. * Copyright (C) 2016-2018 EPAM Systems Inc.
  6. *
  7. * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
  8. */
  9. #include <xen/events.h>
  10. #include <xen/grant_table.h>
  11. #include <xen/xen.h>
  12. #include <xen/xenbus.h>
  13. #include "xen_snd_front.h"
  14. #include "xen_snd_front_alsa.h"
  15. #include "xen_snd_front_cfg.h"
  16. #include "xen_snd_front_evtchnl.h"
  17. static irqreturn_t evtchnl_interrupt_req(int irq, void *dev_id)
  18. {
  19. struct xen_snd_front_evtchnl *channel = dev_id;
  20. struct xen_snd_front_info *front_info = channel->front_info;
  21. struct xensnd_resp *resp;
  22. RING_IDX i, rp;
  23. if (unlikely(channel->state != EVTCHNL_STATE_CONNECTED))
  24. return IRQ_HANDLED;
  25. mutex_lock(&channel->ring_io_lock);
  26. again:
  27. rp = channel->u.req.ring.sring->rsp_prod;
  28. /* Ensure we see queued responses up to rp. */
  29. rmb();
  30. /*
  31. * Assume that the backend is trusted to always write sane values
  32. * to the ring counters, so no overflow checks on frontend side
  33. * are required.
  34. */
  35. for (i = channel->u.req.ring.rsp_cons; i != rp; i++) {
  36. resp = RING_GET_RESPONSE(&channel->u.req.ring, i);
  37. if (resp->id != channel->evt_id)
  38. continue;
  39. switch (resp->operation) {
  40. case XENSND_OP_OPEN:
  41. case XENSND_OP_CLOSE:
  42. case XENSND_OP_READ:
  43. case XENSND_OP_WRITE:
  44. case XENSND_OP_TRIGGER:
  45. channel->u.req.resp_status = resp->status;
  46. complete(&channel->u.req.completion);
  47. break;
  48. case XENSND_OP_HW_PARAM_QUERY:
  49. channel->u.req.resp_status = resp->status;
  50. channel->u.req.resp.hw_param =
  51. resp->resp.hw_param;
  52. complete(&channel->u.req.completion);
  53. break;
  54. default:
  55. dev_err(&front_info->xb_dev->dev,
  56. "Operation %d is not supported\n",
  57. resp->operation);
  58. break;
  59. }
  60. }
  61. channel->u.req.ring.rsp_cons = i;
  62. if (i != channel->u.req.ring.req_prod_pvt) {
  63. int more_to_do;
  64. RING_FINAL_CHECK_FOR_RESPONSES(&channel->u.req.ring,
  65. more_to_do);
  66. if (more_to_do)
  67. goto again;
  68. } else {
  69. channel->u.req.ring.sring->rsp_event = i + 1;
  70. }
  71. mutex_unlock(&channel->ring_io_lock);
  72. return IRQ_HANDLED;
  73. }
  74. static irqreturn_t evtchnl_interrupt_evt(int irq, void *dev_id)
  75. {
  76. struct xen_snd_front_evtchnl *channel = dev_id;
  77. struct xensnd_event_page *page = channel->u.evt.page;
  78. u32 cons, prod;
  79. if (unlikely(channel->state != EVTCHNL_STATE_CONNECTED))
  80. return IRQ_HANDLED;
  81. mutex_lock(&channel->ring_io_lock);
  82. prod = page->in_prod;
  83. /* Ensure we see ring contents up to prod. */
  84. virt_rmb();
  85. if (prod == page->in_cons)
  86. goto out;
  87. /*
  88. * Assume that the backend is trusted to always write sane values
  89. * to the ring counters, so no overflow checks on frontend side
  90. * are required.
  91. */
  92. for (cons = page->in_cons; cons != prod; cons++) {
  93. struct xensnd_evt *event;
  94. event = &XENSND_IN_RING_REF(page, cons);
  95. if (unlikely(event->id != channel->evt_id++))
  96. continue;
  97. switch (event->type) {
  98. case XENSND_EVT_CUR_POS:
  99. xen_snd_front_alsa_handle_cur_pos(channel,
  100. event->op.cur_pos.position);
  101. break;
  102. }
  103. }
  104. page->in_cons = cons;
  105. /* Ensure ring contents. */
  106. virt_wmb();
  107. out:
  108. mutex_unlock(&channel->ring_io_lock);
  109. return IRQ_HANDLED;
  110. }
  111. void xen_snd_front_evtchnl_flush(struct xen_snd_front_evtchnl *channel)
  112. {
  113. int notify;
  114. channel->u.req.ring.req_prod_pvt++;
  115. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&channel->u.req.ring, notify);
  116. if (notify)
  117. notify_remote_via_irq(channel->irq);
  118. }
  119. static void evtchnl_free(struct xen_snd_front_info *front_info,
  120. struct xen_snd_front_evtchnl *channel)
  121. {
  122. unsigned long page = 0;
  123. if (channel->type == EVTCHNL_TYPE_REQ)
  124. page = (unsigned long)channel->u.req.ring.sring;
  125. else if (channel->type == EVTCHNL_TYPE_EVT)
  126. page = (unsigned long)channel->u.evt.page;
  127. if (!page)
  128. return;
  129. channel->state = EVTCHNL_STATE_DISCONNECTED;
  130. if (channel->type == EVTCHNL_TYPE_REQ) {
  131. /* Release all who still waits for response if any. */
  132. channel->u.req.resp_status = -EIO;
  133. complete_all(&channel->u.req.completion);
  134. }
  135. if (channel->irq)
  136. unbind_from_irqhandler(channel->irq, channel);
  137. if (channel->port)
  138. xenbus_free_evtchn(front_info->xb_dev, channel->port);
  139. /* End access and free the page. */
  140. if (channel->gref != GRANT_INVALID_REF)
  141. gnttab_end_foreign_access(channel->gref, 0, page);
  142. else
  143. free_page(page);
  144. memset(channel, 0, sizeof(*channel));
  145. }
  146. void xen_snd_front_evtchnl_free_all(struct xen_snd_front_info *front_info)
  147. {
  148. int i;
  149. if (!front_info->evt_pairs)
  150. return;
  151. for (i = 0; i < front_info->num_evt_pairs; i++) {
  152. evtchnl_free(front_info, &front_info->evt_pairs[i].req);
  153. evtchnl_free(front_info, &front_info->evt_pairs[i].evt);
  154. }
  155. kfree(front_info->evt_pairs);
  156. front_info->evt_pairs = NULL;
  157. }
  158. static int evtchnl_alloc(struct xen_snd_front_info *front_info, int index,
  159. struct xen_snd_front_evtchnl *channel,
  160. enum xen_snd_front_evtchnl_type type)
  161. {
  162. struct xenbus_device *xb_dev = front_info->xb_dev;
  163. unsigned long page;
  164. grant_ref_t gref;
  165. irq_handler_t handler;
  166. char *handler_name = NULL;
  167. int ret;
  168. memset(channel, 0, sizeof(*channel));
  169. channel->type = type;
  170. channel->index = index;
  171. channel->front_info = front_info;
  172. channel->state = EVTCHNL_STATE_DISCONNECTED;
  173. channel->gref = GRANT_INVALID_REF;
  174. page = get_zeroed_page(GFP_KERNEL);
  175. if (!page) {
  176. ret = -ENOMEM;
  177. goto fail;
  178. }
  179. handler_name = kasprintf(GFP_KERNEL, "%s-%s", XENSND_DRIVER_NAME,
  180. type == EVTCHNL_TYPE_REQ ?
  181. XENSND_FIELD_RING_REF :
  182. XENSND_FIELD_EVT_RING_REF);
  183. if (!handler_name) {
  184. ret = -ENOMEM;
  185. goto fail;
  186. }
  187. mutex_init(&channel->ring_io_lock);
  188. if (type == EVTCHNL_TYPE_REQ) {
  189. struct xen_sndif_sring *sring = (struct xen_sndif_sring *)page;
  190. init_completion(&channel->u.req.completion);
  191. mutex_init(&channel->u.req.req_io_lock);
  192. SHARED_RING_INIT(sring);
  193. FRONT_RING_INIT(&channel->u.req.ring, sring, XEN_PAGE_SIZE);
  194. ret = xenbus_grant_ring(xb_dev, sring, 1, &gref);
  195. if (ret < 0) {
  196. channel->u.req.ring.sring = NULL;
  197. goto fail;
  198. }
  199. handler = evtchnl_interrupt_req;
  200. } else {
  201. ret = gnttab_grant_foreign_access(xb_dev->otherend_id,
  202. virt_to_gfn((void *)page), 0);
  203. if (ret < 0)
  204. goto fail;
  205. channel->u.evt.page = (struct xensnd_event_page *)page;
  206. gref = ret;
  207. handler = evtchnl_interrupt_evt;
  208. }
  209. channel->gref = gref;
  210. ret = xenbus_alloc_evtchn(xb_dev, &channel->port);
  211. if (ret < 0)
  212. goto fail;
  213. ret = bind_evtchn_to_irq(channel->port);
  214. if (ret < 0) {
  215. dev_err(&xb_dev->dev,
  216. "Failed to bind IRQ for domid %d port %d: %d\n",
  217. front_info->xb_dev->otherend_id, channel->port, ret);
  218. goto fail;
  219. }
  220. channel->irq = ret;
  221. ret = request_threaded_irq(channel->irq, NULL, handler,
  222. IRQF_ONESHOT, handler_name, channel);
  223. if (ret < 0) {
  224. dev_err(&xb_dev->dev, "Failed to request IRQ %d: %d\n",
  225. channel->irq, ret);
  226. goto fail;
  227. }
  228. kfree(handler_name);
  229. return 0;
  230. fail:
  231. if (page)
  232. free_page(page);
  233. kfree(handler_name);
  234. dev_err(&xb_dev->dev, "Failed to allocate ring: %d\n", ret);
  235. return ret;
  236. }
  237. int xen_snd_front_evtchnl_create_all(struct xen_snd_front_info *front_info,
  238. int num_streams)
  239. {
  240. struct xen_front_cfg_card *cfg = &front_info->cfg;
  241. struct device *dev = &front_info->xb_dev->dev;
  242. int d, ret = 0;
  243. front_info->evt_pairs =
  244. kcalloc(num_streams,
  245. sizeof(struct xen_snd_front_evtchnl_pair),
  246. GFP_KERNEL);
  247. if (!front_info->evt_pairs)
  248. return -ENOMEM;
  249. /* Iterate over devices and their streams and create event channels. */
  250. for (d = 0; d < cfg->num_pcm_instances; d++) {
  251. struct xen_front_cfg_pcm_instance *pcm_instance;
  252. int s, index;
  253. pcm_instance = &cfg->pcm_instances[d];
  254. for (s = 0; s < pcm_instance->num_streams_pb; s++) {
  255. index = pcm_instance->streams_pb[s].index;
  256. ret = evtchnl_alloc(front_info, index,
  257. &front_info->evt_pairs[index].req,
  258. EVTCHNL_TYPE_REQ);
  259. if (ret < 0) {
  260. dev_err(dev, "Error allocating control channel\n");
  261. goto fail;
  262. }
  263. ret = evtchnl_alloc(front_info, index,
  264. &front_info->evt_pairs[index].evt,
  265. EVTCHNL_TYPE_EVT);
  266. if (ret < 0) {
  267. dev_err(dev, "Error allocating in-event channel\n");
  268. goto fail;
  269. }
  270. }
  271. for (s = 0; s < pcm_instance->num_streams_cap; s++) {
  272. index = pcm_instance->streams_cap[s].index;
  273. ret = evtchnl_alloc(front_info, index,
  274. &front_info->evt_pairs[index].req,
  275. EVTCHNL_TYPE_REQ);
  276. if (ret < 0) {
  277. dev_err(dev, "Error allocating control channel\n");
  278. goto fail;
  279. }
  280. ret = evtchnl_alloc(front_info, index,
  281. &front_info->evt_pairs[index].evt,
  282. EVTCHNL_TYPE_EVT);
  283. if (ret < 0) {
  284. dev_err(dev, "Error allocating in-event channel\n");
  285. goto fail;
  286. }
  287. }
  288. }
  289. front_info->num_evt_pairs = num_streams;
  290. return 0;
  291. fail:
  292. xen_snd_front_evtchnl_free_all(front_info);
  293. return ret;
  294. }
  295. static int evtchnl_publish(struct xenbus_transaction xbt,
  296. struct xen_snd_front_evtchnl *channel,
  297. const char *path, const char *node_ring,
  298. const char *node_chnl)
  299. {
  300. struct xenbus_device *xb_dev = channel->front_info->xb_dev;
  301. int ret;
  302. /* Write control channel ring reference. */
  303. ret = xenbus_printf(xbt, path, node_ring, "%u", channel->gref);
  304. if (ret < 0) {
  305. dev_err(&xb_dev->dev, "Error writing ring-ref: %d\n", ret);
  306. return ret;
  307. }
  308. /* Write event channel ring reference. */
  309. ret = xenbus_printf(xbt, path, node_chnl, "%u", channel->port);
  310. if (ret < 0) {
  311. dev_err(&xb_dev->dev, "Error writing event channel: %d\n", ret);
  312. return ret;
  313. }
  314. return 0;
  315. }
  316. int xen_snd_front_evtchnl_publish_all(struct xen_snd_front_info *front_info)
  317. {
  318. struct xen_front_cfg_card *cfg = &front_info->cfg;
  319. struct xenbus_transaction xbt;
  320. int ret, d;
  321. again:
  322. ret = xenbus_transaction_start(&xbt);
  323. if (ret < 0) {
  324. xenbus_dev_fatal(front_info->xb_dev, ret,
  325. "starting transaction");
  326. return ret;
  327. }
  328. for (d = 0; d < cfg->num_pcm_instances; d++) {
  329. struct xen_front_cfg_pcm_instance *pcm_instance;
  330. int s, index;
  331. pcm_instance = &cfg->pcm_instances[d];
  332. for (s = 0; s < pcm_instance->num_streams_pb; s++) {
  333. index = pcm_instance->streams_pb[s].index;
  334. ret = evtchnl_publish(xbt,
  335. &front_info->evt_pairs[index].req,
  336. pcm_instance->streams_pb[s].xenstore_path,
  337. XENSND_FIELD_RING_REF,
  338. XENSND_FIELD_EVT_CHNL);
  339. if (ret < 0)
  340. goto fail;
  341. ret = evtchnl_publish(xbt,
  342. &front_info->evt_pairs[index].evt,
  343. pcm_instance->streams_pb[s].xenstore_path,
  344. XENSND_FIELD_EVT_RING_REF,
  345. XENSND_FIELD_EVT_EVT_CHNL);
  346. if (ret < 0)
  347. goto fail;
  348. }
  349. for (s = 0; s < pcm_instance->num_streams_cap; s++) {
  350. index = pcm_instance->streams_cap[s].index;
  351. ret = evtchnl_publish(xbt,
  352. &front_info->evt_pairs[index].req,
  353. pcm_instance->streams_cap[s].xenstore_path,
  354. XENSND_FIELD_RING_REF,
  355. XENSND_FIELD_EVT_CHNL);
  356. if (ret < 0)
  357. goto fail;
  358. ret = evtchnl_publish(xbt,
  359. &front_info->evt_pairs[index].evt,
  360. pcm_instance->streams_cap[s].xenstore_path,
  361. XENSND_FIELD_EVT_RING_REF,
  362. XENSND_FIELD_EVT_EVT_CHNL);
  363. if (ret < 0)
  364. goto fail;
  365. }
  366. }
  367. ret = xenbus_transaction_end(xbt, 0);
  368. if (ret < 0) {
  369. if (ret == -EAGAIN)
  370. goto again;
  371. xenbus_dev_fatal(front_info->xb_dev, ret,
  372. "completing transaction");
  373. goto fail_to_end;
  374. }
  375. return 0;
  376. fail:
  377. xenbus_transaction_end(xbt, 1);
  378. fail_to_end:
  379. xenbus_dev_fatal(front_info->xb_dev, ret, "writing XenStore");
  380. return ret;
  381. }
  382. void xen_snd_front_evtchnl_pair_set_connected(struct xen_snd_front_evtchnl_pair *evt_pair,
  383. bool is_connected)
  384. {
  385. enum xen_snd_front_evtchnl_state state;
  386. if (is_connected)
  387. state = EVTCHNL_STATE_CONNECTED;
  388. else
  389. state = EVTCHNL_STATE_DISCONNECTED;
  390. mutex_lock(&evt_pair->req.ring_io_lock);
  391. evt_pair->req.state = state;
  392. mutex_unlock(&evt_pair->req.ring_io_lock);
  393. mutex_lock(&evt_pair->evt.ring_io_lock);
  394. evt_pair->evt.state = state;
  395. mutex_unlock(&evt_pair->evt.ring_io_lock);
  396. }
  397. void xen_snd_front_evtchnl_pair_clear(struct xen_snd_front_evtchnl_pair *evt_pair)
  398. {
  399. mutex_lock(&evt_pair->req.ring_io_lock);
  400. evt_pair->req.evt_next_id = 0;
  401. mutex_unlock(&evt_pair->req.ring_io_lock);
  402. mutex_lock(&evt_pair->evt.ring_io_lock);
  403. evt_pair->evt.evt_next_id = 0;
  404. mutex_unlock(&evt_pair->evt.ring_io_lock);
  405. }