xen_snd_front_evtchnl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 <[email protected]>
  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. void *page = NULL;
  123. if (channel->type == EVTCHNL_TYPE_REQ)
  124. page = channel->u.req.ring.sring;
  125. else if (channel->type == EVTCHNL_TYPE_EVT)
  126. page = 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. xenbus_teardown_ring(&page, 1, &channel->gref);
  141. memset(channel, 0, sizeof(*channel));
  142. }
  143. void xen_snd_front_evtchnl_free_all(struct xen_snd_front_info *front_info)
  144. {
  145. int i;
  146. if (!front_info->evt_pairs)
  147. return;
  148. for (i = 0; i < front_info->num_evt_pairs; i++) {
  149. evtchnl_free(front_info, &front_info->evt_pairs[i].req);
  150. evtchnl_free(front_info, &front_info->evt_pairs[i].evt);
  151. }
  152. kfree(front_info->evt_pairs);
  153. front_info->evt_pairs = NULL;
  154. }
  155. static int evtchnl_alloc(struct xen_snd_front_info *front_info, int index,
  156. struct xen_snd_front_evtchnl *channel,
  157. enum xen_snd_front_evtchnl_type type)
  158. {
  159. struct xenbus_device *xb_dev = front_info->xb_dev;
  160. void *page;
  161. irq_handler_t handler;
  162. char *handler_name = NULL;
  163. int ret;
  164. memset(channel, 0, sizeof(*channel));
  165. channel->type = type;
  166. channel->index = index;
  167. channel->front_info = front_info;
  168. channel->state = EVTCHNL_STATE_DISCONNECTED;
  169. ret = xenbus_setup_ring(xb_dev, GFP_KERNEL, &page, 1, &channel->gref);
  170. if (ret)
  171. goto fail;
  172. handler_name = kasprintf(GFP_KERNEL, "%s-%s", XENSND_DRIVER_NAME,
  173. type == EVTCHNL_TYPE_REQ ?
  174. XENSND_FIELD_RING_REF :
  175. XENSND_FIELD_EVT_RING_REF);
  176. if (!handler_name) {
  177. ret = -ENOMEM;
  178. goto fail;
  179. }
  180. mutex_init(&channel->ring_io_lock);
  181. if (type == EVTCHNL_TYPE_REQ) {
  182. struct xen_sndif_sring *sring = page;
  183. init_completion(&channel->u.req.completion);
  184. mutex_init(&channel->u.req.req_io_lock);
  185. XEN_FRONT_RING_INIT(&channel->u.req.ring, sring, XEN_PAGE_SIZE);
  186. handler = evtchnl_interrupt_req;
  187. } else {
  188. channel->u.evt.page = page;
  189. handler = evtchnl_interrupt_evt;
  190. }
  191. ret = xenbus_alloc_evtchn(xb_dev, &channel->port);
  192. if (ret < 0)
  193. goto fail;
  194. ret = bind_evtchn_to_irq(channel->port);
  195. if (ret < 0) {
  196. dev_err(&xb_dev->dev,
  197. "Failed to bind IRQ for domid %d port %d: %d\n",
  198. front_info->xb_dev->otherend_id, channel->port, ret);
  199. goto fail;
  200. }
  201. channel->irq = ret;
  202. ret = request_threaded_irq(channel->irq, NULL, handler,
  203. IRQF_ONESHOT, handler_name, channel);
  204. if (ret < 0) {
  205. dev_err(&xb_dev->dev, "Failed to request IRQ %d: %d\n",
  206. channel->irq, ret);
  207. goto fail;
  208. }
  209. kfree(handler_name);
  210. return 0;
  211. fail:
  212. kfree(handler_name);
  213. dev_err(&xb_dev->dev, "Failed to allocate ring: %d\n", ret);
  214. return ret;
  215. }
  216. int xen_snd_front_evtchnl_create_all(struct xen_snd_front_info *front_info,
  217. int num_streams)
  218. {
  219. struct xen_front_cfg_card *cfg = &front_info->cfg;
  220. struct device *dev = &front_info->xb_dev->dev;
  221. int d, ret = 0;
  222. front_info->evt_pairs =
  223. kcalloc(num_streams,
  224. sizeof(struct xen_snd_front_evtchnl_pair),
  225. GFP_KERNEL);
  226. if (!front_info->evt_pairs)
  227. return -ENOMEM;
  228. /* Iterate over devices and their streams and create event channels. */
  229. for (d = 0; d < cfg->num_pcm_instances; d++) {
  230. struct xen_front_cfg_pcm_instance *pcm_instance;
  231. int s, index;
  232. pcm_instance = &cfg->pcm_instances[d];
  233. for (s = 0; s < pcm_instance->num_streams_pb; s++) {
  234. index = pcm_instance->streams_pb[s].index;
  235. ret = evtchnl_alloc(front_info, index,
  236. &front_info->evt_pairs[index].req,
  237. EVTCHNL_TYPE_REQ);
  238. if (ret < 0) {
  239. dev_err(dev, "Error allocating control channel\n");
  240. goto fail;
  241. }
  242. ret = evtchnl_alloc(front_info, index,
  243. &front_info->evt_pairs[index].evt,
  244. EVTCHNL_TYPE_EVT);
  245. if (ret < 0) {
  246. dev_err(dev, "Error allocating in-event channel\n");
  247. goto fail;
  248. }
  249. }
  250. for (s = 0; s < pcm_instance->num_streams_cap; s++) {
  251. index = pcm_instance->streams_cap[s].index;
  252. ret = evtchnl_alloc(front_info, index,
  253. &front_info->evt_pairs[index].req,
  254. EVTCHNL_TYPE_REQ);
  255. if (ret < 0) {
  256. dev_err(dev, "Error allocating control channel\n");
  257. goto fail;
  258. }
  259. ret = evtchnl_alloc(front_info, index,
  260. &front_info->evt_pairs[index].evt,
  261. EVTCHNL_TYPE_EVT);
  262. if (ret < 0) {
  263. dev_err(dev, "Error allocating in-event channel\n");
  264. goto fail;
  265. }
  266. }
  267. }
  268. front_info->num_evt_pairs = num_streams;
  269. return 0;
  270. fail:
  271. xen_snd_front_evtchnl_free_all(front_info);
  272. return ret;
  273. }
  274. static int evtchnl_publish(struct xenbus_transaction xbt,
  275. struct xen_snd_front_evtchnl *channel,
  276. const char *path, const char *node_ring,
  277. const char *node_chnl)
  278. {
  279. struct xenbus_device *xb_dev = channel->front_info->xb_dev;
  280. int ret;
  281. /* Write control channel ring reference. */
  282. ret = xenbus_printf(xbt, path, node_ring, "%u", channel->gref);
  283. if (ret < 0) {
  284. dev_err(&xb_dev->dev, "Error writing ring-ref: %d\n", ret);
  285. return ret;
  286. }
  287. /* Write event channel ring reference. */
  288. ret = xenbus_printf(xbt, path, node_chnl, "%u", channel->port);
  289. if (ret < 0) {
  290. dev_err(&xb_dev->dev, "Error writing event channel: %d\n", ret);
  291. return ret;
  292. }
  293. return 0;
  294. }
  295. int xen_snd_front_evtchnl_publish_all(struct xen_snd_front_info *front_info)
  296. {
  297. struct xen_front_cfg_card *cfg = &front_info->cfg;
  298. struct xenbus_transaction xbt;
  299. int ret, d;
  300. again:
  301. ret = xenbus_transaction_start(&xbt);
  302. if (ret < 0) {
  303. xenbus_dev_fatal(front_info->xb_dev, ret,
  304. "starting transaction");
  305. return ret;
  306. }
  307. for (d = 0; d < cfg->num_pcm_instances; d++) {
  308. struct xen_front_cfg_pcm_instance *pcm_instance;
  309. int s, index;
  310. pcm_instance = &cfg->pcm_instances[d];
  311. for (s = 0; s < pcm_instance->num_streams_pb; s++) {
  312. index = pcm_instance->streams_pb[s].index;
  313. ret = evtchnl_publish(xbt,
  314. &front_info->evt_pairs[index].req,
  315. pcm_instance->streams_pb[s].xenstore_path,
  316. XENSND_FIELD_RING_REF,
  317. XENSND_FIELD_EVT_CHNL);
  318. if (ret < 0)
  319. goto fail;
  320. ret = evtchnl_publish(xbt,
  321. &front_info->evt_pairs[index].evt,
  322. pcm_instance->streams_pb[s].xenstore_path,
  323. XENSND_FIELD_EVT_RING_REF,
  324. XENSND_FIELD_EVT_EVT_CHNL);
  325. if (ret < 0)
  326. goto fail;
  327. }
  328. for (s = 0; s < pcm_instance->num_streams_cap; s++) {
  329. index = pcm_instance->streams_cap[s].index;
  330. ret = evtchnl_publish(xbt,
  331. &front_info->evt_pairs[index].req,
  332. pcm_instance->streams_cap[s].xenstore_path,
  333. XENSND_FIELD_RING_REF,
  334. XENSND_FIELD_EVT_CHNL);
  335. if (ret < 0)
  336. goto fail;
  337. ret = evtchnl_publish(xbt,
  338. &front_info->evt_pairs[index].evt,
  339. pcm_instance->streams_cap[s].xenstore_path,
  340. XENSND_FIELD_EVT_RING_REF,
  341. XENSND_FIELD_EVT_EVT_CHNL);
  342. if (ret < 0)
  343. goto fail;
  344. }
  345. }
  346. ret = xenbus_transaction_end(xbt, 0);
  347. if (ret < 0) {
  348. if (ret == -EAGAIN)
  349. goto again;
  350. xenbus_dev_fatal(front_info->xb_dev, ret,
  351. "completing transaction");
  352. goto fail_to_end;
  353. }
  354. return 0;
  355. fail:
  356. xenbus_transaction_end(xbt, 1);
  357. fail_to_end:
  358. xenbus_dev_fatal(front_info->xb_dev, ret, "writing XenStore");
  359. return ret;
  360. }
  361. void xen_snd_front_evtchnl_pair_set_connected(struct xen_snd_front_evtchnl_pair *evt_pair,
  362. bool is_connected)
  363. {
  364. enum xen_snd_front_evtchnl_state state;
  365. if (is_connected)
  366. state = EVTCHNL_STATE_CONNECTED;
  367. else
  368. state = EVTCHNL_STATE_DISCONNECTED;
  369. mutex_lock(&evt_pair->req.ring_io_lock);
  370. evt_pair->req.state = state;
  371. mutex_unlock(&evt_pair->req.ring_io_lock);
  372. mutex_lock(&evt_pair->evt.ring_io_lock);
  373. evt_pair->evt.state = state;
  374. mutex_unlock(&evt_pair->evt.ring_io_lock);
  375. }
  376. void xen_snd_front_evtchnl_pair_clear(struct xen_snd_front_evtchnl_pair *evt_pair)
  377. {
  378. mutex_lock(&evt_pair->req.ring_io_lock);
  379. evt_pair->req.evt_next_id = 0;
  380. mutex_unlock(&evt_pair->req.ring_io_lock);
  381. mutex_lock(&evt_pair->evt.ring_io_lock);
  382. evt_pair->evt.evt_next_id = 0;
  383. mutex_unlock(&evt_pair->evt.ring_io_lock);
  384. }