xen_snd_front_evtchnl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. guard(mutex)(&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. return IRQ_HANDLED;
  72. }
  73. static irqreturn_t evtchnl_interrupt_evt(int irq, void *dev_id)
  74. {
  75. struct xen_snd_front_evtchnl *channel = dev_id;
  76. struct xensnd_event_page *page = channel->u.evt.page;
  77. u32 cons, prod;
  78. if (unlikely(channel->state != EVTCHNL_STATE_CONNECTED))
  79. return IRQ_HANDLED;
  80. guard(mutex)(&channel->ring_io_lock);
  81. prod = page->in_prod;
  82. /* Ensure we see ring contents up to prod. */
  83. virt_rmb();
  84. if (prod == page->in_cons)
  85. return IRQ_HANDLED;
  86. /*
  87. * Assume that the backend is trusted to always write sane values
  88. * to the ring counters, so no overflow checks on frontend side
  89. * are required.
  90. */
  91. for (cons = page->in_cons; cons != prod; cons++) {
  92. struct xensnd_evt *event;
  93. event = &XENSND_IN_RING_REF(page, cons);
  94. if (unlikely(event->id != channel->evt_id++))
  95. continue;
  96. switch (event->type) {
  97. case XENSND_EVT_CUR_POS:
  98. xen_snd_front_alsa_handle_cur_pos(channel,
  99. event->op.cur_pos.position);
  100. break;
  101. }
  102. }
  103. page->in_cons = cons;
  104. /* Ensure ring contents. */
  105. virt_wmb();
  106. return IRQ_HANDLED;
  107. }
  108. void xen_snd_front_evtchnl_flush(struct xen_snd_front_evtchnl *channel)
  109. {
  110. int notify;
  111. channel->u.req.ring.req_prod_pvt++;
  112. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&channel->u.req.ring, notify);
  113. if (notify)
  114. notify_remote_via_irq(channel->irq);
  115. }
  116. static void evtchnl_free(struct xen_snd_front_info *front_info,
  117. struct xen_snd_front_evtchnl *channel)
  118. {
  119. void *page = NULL;
  120. if (channel->type == EVTCHNL_TYPE_REQ)
  121. page = channel->u.req.ring.sring;
  122. else if (channel->type == EVTCHNL_TYPE_EVT)
  123. page = channel->u.evt.page;
  124. if (!page)
  125. return;
  126. channel->state = EVTCHNL_STATE_DISCONNECTED;
  127. if (channel->type == EVTCHNL_TYPE_REQ) {
  128. /* Release all who still waits for response if any. */
  129. channel->u.req.resp_status = -EIO;
  130. complete_all(&channel->u.req.completion);
  131. }
  132. if (channel->irq)
  133. unbind_from_irqhandler(channel->irq, channel);
  134. if (channel->port)
  135. xenbus_free_evtchn(front_info->xb_dev, channel->port);
  136. /* End access and free the page. */
  137. xenbus_teardown_ring(&page, 1, &channel->gref);
  138. memset(channel, 0, sizeof(*channel));
  139. }
  140. void xen_snd_front_evtchnl_free_all(struct xen_snd_front_info *front_info)
  141. {
  142. int i;
  143. if (!front_info->evt_pairs)
  144. return;
  145. for (i = 0; i < front_info->num_evt_pairs; i++) {
  146. evtchnl_free(front_info, &front_info->evt_pairs[i].req);
  147. evtchnl_free(front_info, &front_info->evt_pairs[i].evt);
  148. }
  149. kfree(front_info->evt_pairs);
  150. front_info->evt_pairs = NULL;
  151. }
  152. static int evtchnl_alloc(struct xen_snd_front_info *front_info, int index,
  153. struct xen_snd_front_evtchnl *channel,
  154. enum xen_snd_front_evtchnl_type type)
  155. {
  156. struct xenbus_device *xb_dev = front_info->xb_dev;
  157. void *page;
  158. irq_handler_t handler;
  159. char *handler_name = NULL;
  160. int ret;
  161. memset(channel, 0, sizeof(*channel));
  162. channel->type = type;
  163. channel->index = index;
  164. channel->front_info = front_info;
  165. channel->state = EVTCHNL_STATE_DISCONNECTED;
  166. ret = xenbus_setup_ring(xb_dev, GFP_KERNEL, &page, 1, &channel->gref);
  167. if (ret)
  168. goto fail;
  169. handler_name = kasprintf(GFP_KERNEL, "%s-%s", XENSND_DRIVER_NAME,
  170. type == EVTCHNL_TYPE_REQ ?
  171. XENSND_FIELD_RING_REF :
  172. XENSND_FIELD_EVT_RING_REF);
  173. if (!handler_name) {
  174. ret = -ENOMEM;
  175. goto fail;
  176. }
  177. mutex_init(&channel->ring_io_lock);
  178. if (type == EVTCHNL_TYPE_REQ) {
  179. struct xen_sndif_sring *sring = page;
  180. init_completion(&channel->u.req.completion);
  181. mutex_init(&channel->u.req.req_io_lock);
  182. XEN_FRONT_RING_INIT(&channel->u.req.ring, sring, XEN_PAGE_SIZE);
  183. handler = evtchnl_interrupt_req;
  184. } else {
  185. channel->u.evt.page = page;
  186. handler = evtchnl_interrupt_evt;
  187. }
  188. ret = xenbus_alloc_evtchn(xb_dev, &channel->port);
  189. if (ret < 0)
  190. goto fail;
  191. ret = bind_evtchn_to_irq(channel->port);
  192. if (ret < 0) {
  193. dev_err(&xb_dev->dev,
  194. "Failed to bind IRQ for domid %d port %d: %d\n",
  195. front_info->xb_dev->otherend_id, channel->port, ret);
  196. goto fail;
  197. }
  198. channel->irq = ret;
  199. ret = request_threaded_irq(channel->irq, NULL, handler,
  200. IRQF_ONESHOT, handler_name, channel);
  201. if (ret < 0) {
  202. dev_err(&xb_dev->dev, "Failed to request IRQ %d: %d\n",
  203. channel->irq, ret);
  204. goto fail;
  205. }
  206. kfree(handler_name);
  207. return 0;
  208. fail:
  209. kfree(handler_name);
  210. dev_err(&xb_dev->dev, "Failed to allocate ring: %d\n", ret);
  211. return ret;
  212. }
  213. int xen_snd_front_evtchnl_create_all(struct xen_snd_front_info *front_info,
  214. int num_streams)
  215. {
  216. struct xen_front_cfg_card *cfg = &front_info->cfg;
  217. struct device *dev = &front_info->xb_dev->dev;
  218. int d, ret = 0;
  219. front_info->evt_pairs =
  220. kzalloc_objs(struct xen_snd_front_evtchnl_pair,
  221. num_streams);
  222. if (!front_info->evt_pairs)
  223. return -ENOMEM;
  224. /* Iterate over devices and their streams and create event channels. */
  225. for (d = 0; d < cfg->num_pcm_instances; d++) {
  226. struct xen_front_cfg_pcm_instance *pcm_instance;
  227. int s, index;
  228. pcm_instance = &cfg->pcm_instances[d];
  229. for (s = 0; s < pcm_instance->num_streams_pb; s++) {
  230. index = pcm_instance->streams_pb[s].index;
  231. ret = evtchnl_alloc(front_info, index,
  232. &front_info->evt_pairs[index].req,
  233. EVTCHNL_TYPE_REQ);
  234. if (ret < 0) {
  235. dev_err(dev, "Error allocating control channel\n");
  236. goto fail;
  237. }
  238. ret = evtchnl_alloc(front_info, index,
  239. &front_info->evt_pairs[index].evt,
  240. EVTCHNL_TYPE_EVT);
  241. if (ret < 0) {
  242. dev_err(dev, "Error allocating in-event channel\n");
  243. goto fail;
  244. }
  245. }
  246. for (s = 0; s < pcm_instance->num_streams_cap; s++) {
  247. index = pcm_instance->streams_cap[s].index;
  248. ret = evtchnl_alloc(front_info, index,
  249. &front_info->evt_pairs[index].req,
  250. EVTCHNL_TYPE_REQ);
  251. if (ret < 0) {
  252. dev_err(dev, "Error allocating control channel\n");
  253. goto fail;
  254. }
  255. ret = evtchnl_alloc(front_info, index,
  256. &front_info->evt_pairs[index].evt,
  257. EVTCHNL_TYPE_EVT);
  258. if (ret < 0) {
  259. dev_err(dev, "Error allocating in-event channel\n");
  260. goto fail;
  261. }
  262. }
  263. }
  264. front_info->num_evt_pairs = num_streams;
  265. return 0;
  266. fail:
  267. xen_snd_front_evtchnl_free_all(front_info);
  268. return ret;
  269. }
  270. static int evtchnl_publish(struct xenbus_transaction xbt,
  271. struct xen_snd_front_evtchnl *channel,
  272. const char *path, const char *node_ring,
  273. const char *node_chnl)
  274. {
  275. struct xenbus_device *xb_dev = channel->front_info->xb_dev;
  276. int ret;
  277. /* Write control channel ring reference. */
  278. ret = xenbus_printf(xbt, path, node_ring, "%u", channel->gref);
  279. if (ret < 0) {
  280. dev_err(&xb_dev->dev, "Error writing ring-ref: %d\n", ret);
  281. return ret;
  282. }
  283. /* Write event channel ring reference. */
  284. ret = xenbus_printf(xbt, path, node_chnl, "%u", channel->port);
  285. if (ret < 0) {
  286. dev_err(&xb_dev->dev, "Error writing event channel: %d\n", ret);
  287. return ret;
  288. }
  289. return 0;
  290. }
  291. int xen_snd_front_evtchnl_publish_all(struct xen_snd_front_info *front_info)
  292. {
  293. struct xen_front_cfg_card *cfg = &front_info->cfg;
  294. struct xenbus_transaction xbt;
  295. int ret, d;
  296. again:
  297. ret = xenbus_transaction_start(&xbt);
  298. if (ret < 0) {
  299. xenbus_dev_fatal(front_info->xb_dev, ret,
  300. "starting transaction");
  301. return ret;
  302. }
  303. for (d = 0; d < cfg->num_pcm_instances; d++) {
  304. struct xen_front_cfg_pcm_instance *pcm_instance;
  305. int s, index;
  306. pcm_instance = &cfg->pcm_instances[d];
  307. for (s = 0; s < pcm_instance->num_streams_pb; s++) {
  308. index = pcm_instance->streams_pb[s].index;
  309. ret = evtchnl_publish(xbt,
  310. &front_info->evt_pairs[index].req,
  311. pcm_instance->streams_pb[s].xenstore_path,
  312. XENSND_FIELD_RING_REF,
  313. XENSND_FIELD_EVT_CHNL);
  314. if (ret < 0)
  315. goto fail;
  316. ret = evtchnl_publish(xbt,
  317. &front_info->evt_pairs[index].evt,
  318. pcm_instance->streams_pb[s].xenstore_path,
  319. XENSND_FIELD_EVT_RING_REF,
  320. XENSND_FIELD_EVT_EVT_CHNL);
  321. if (ret < 0)
  322. goto fail;
  323. }
  324. for (s = 0; s < pcm_instance->num_streams_cap; s++) {
  325. index = pcm_instance->streams_cap[s].index;
  326. ret = evtchnl_publish(xbt,
  327. &front_info->evt_pairs[index].req,
  328. pcm_instance->streams_cap[s].xenstore_path,
  329. XENSND_FIELD_RING_REF,
  330. XENSND_FIELD_EVT_CHNL);
  331. if (ret < 0)
  332. goto fail;
  333. ret = evtchnl_publish(xbt,
  334. &front_info->evt_pairs[index].evt,
  335. pcm_instance->streams_cap[s].xenstore_path,
  336. XENSND_FIELD_EVT_RING_REF,
  337. XENSND_FIELD_EVT_EVT_CHNL);
  338. if (ret < 0)
  339. goto fail;
  340. }
  341. }
  342. ret = xenbus_transaction_end(xbt, 0);
  343. if (ret < 0) {
  344. if (ret == -EAGAIN)
  345. goto again;
  346. xenbus_dev_fatal(front_info->xb_dev, ret,
  347. "completing transaction");
  348. goto fail_to_end;
  349. }
  350. return 0;
  351. fail:
  352. xenbus_transaction_end(xbt, 1);
  353. fail_to_end:
  354. xenbus_dev_fatal(front_info->xb_dev, ret, "writing XenStore");
  355. return ret;
  356. }
  357. void xen_snd_front_evtchnl_pair_set_connected(struct xen_snd_front_evtchnl_pair *evt_pair,
  358. bool is_connected)
  359. {
  360. enum xen_snd_front_evtchnl_state state;
  361. if (is_connected)
  362. state = EVTCHNL_STATE_CONNECTED;
  363. else
  364. state = EVTCHNL_STATE_DISCONNECTED;
  365. scoped_guard(mutex, &evt_pair->req.ring_io_lock) {
  366. evt_pair->req.state = state;
  367. }
  368. scoped_guard(mutex, &evt_pair->evt.ring_io_lock) {
  369. evt_pair->evt.state = state;
  370. }
  371. }
  372. void xen_snd_front_evtchnl_pair_clear(struct xen_snd_front_evtchnl_pair *evt_pair)
  373. {
  374. scoped_guard(mutex, &evt_pair->req.ring_io_lock) {
  375. evt_pair->req.evt_next_id = 0;
  376. }
  377. scoped_guard(mutex, &evt_pair->evt.ring_io_lock) {
  378. evt_pair->evt.evt_next_id = 0;
  379. }
  380. }