xen_snd_front_evtchnl.c 12 KB

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