1 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 * Xen para-virtual sound device
6 * Copyright (C) 2016-2018 EPAM Systems Inc.
11 #include <xen/events.h>
12 #include <xen/grant_table.h>
14 #include <xen/xenbus.h>
16 #include "xen_snd_front.h"
17 #include "xen_snd_front_alsa.h"
18 #include "xen_snd_front_cfg.h"
19 #include "xen_snd_front_evtchnl.h"
21 static irqreturn_t evtchnl_interrupt_req(int irq, void *dev_id)
23 struct xen_snd_front_evtchnl *channel = dev_id;
24 struct xen_snd_front_info *front_info = channel->front_info;
25 struct xensnd_resp *resp;
28 if (unlikely(channel->state != EVTCHNL_STATE_CONNECTED))
31 mutex_lock(&channel->ring_io_lock);
34 rp = channel->u.req.ring.sring->rsp_prod;
35 /* Ensure we see queued responses up to rp. */
39 * Assume that the backend is trusted to always write sane values
40 * to the ring counters, so no overflow checks on frontend side
43 for (i = channel->u.req.ring.rsp_cons; i != rp; i++) {
44 resp = RING_GET_RESPONSE(&channel->u.req.ring, i);
45 if (resp->id != channel->evt_id)
47 switch (resp->operation) {
52 case XENSND_OP_TRIGGER:
53 channel->u.req.resp_status = resp->status;
54 complete(&channel->u.req.completion);
56 case XENSND_OP_HW_PARAM_QUERY:
57 channel->u.req.resp_status = resp->status;
58 channel->u.req.resp.hw_param =
60 complete(&channel->u.req.completion);
64 dev_err(&front_info->xb_dev->dev,
65 "Operation %d is not supported\n",
71 channel->u.req.ring.rsp_cons = i;
72 if (i != channel->u.req.ring.req_prod_pvt) {
75 RING_FINAL_CHECK_FOR_RESPONSES(&channel->u.req.ring,
80 channel->u.req.ring.sring->rsp_event = i + 1;
83 mutex_unlock(&channel->ring_io_lock);
87 static irqreturn_t evtchnl_interrupt_evt(int irq, void *dev_id)
89 struct xen_snd_front_evtchnl *channel = dev_id;
90 struct xensnd_event_page *page = channel->u.evt.page;
93 if (unlikely(channel->state != EVTCHNL_STATE_CONNECTED))
96 mutex_lock(&channel->ring_io_lock);
99 /* Ensure we see ring contents up to prod. */
101 if (prod == page->in_cons)
105 * Assume that the backend is trusted to always write sane values
106 * to the ring counters, so no overflow checks on frontend side
109 for (cons = page->in_cons; cons != prod; cons++) {
110 struct xensnd_evt *event;
112 event = &XENSND_IN_RING_REF(page, cons);
113 if (unlikely(event->id != channel->evt_id++))
116 switch (event->type) {
117 case XENSND_EVT_CUR_POS:
118 xen_snd_front_alsa_handle_cur_pos(channel,
119 event->op.cur_pos.position);
124 page->in_cons = cons;
125 /* Ensure ring contents. */
129 mutex_unlock(&channel->ring_io_lock);
133 void xen_snd_front_evtchnl_flush(struct xen_snd_front_evtchnl *channel)
137 channel->u.req.ring.req_prod_pvt++;
138 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&channel->u.req.ring, notify);
140 notify_remote_via_irq(channel->irq);
143 static void evtchnl_free(struct xen_snd_front_info *front_info,
144 struct xen_snd_front_evtchnl *channel)
148 if (channel->type == EVTCHNL_TYPE_REQ)
149 page = channel->u.req.ring.sring;
150 else if (channel->type == EVTCHNL_TYPE_EVT)
151 page = channel->u.evt.page;
156 channel->state = EVTCHNL_STATE_DISCONNECTED;
157 if (channel->type == EVTCHNL_TYPE_REQ) {
158 /* Release all who still waits for response if any. */
159 channel->u.req.resp_status = -EIO;
160 complete_all(&channel->u.req.completion);
164 unbind_from_irqhandler(channel->irq, channel);
167 xenbus_free_evtchn(front_info->xb_dev, channel->port);
169 /* End access and free the page. */
170 xenbus_teardown_ring(&page, 1, &channel->gref);
172 memset(channel, 0, sizeof(*channel));
175 void xen_snd_front_evtchnl_free_all(struct xen_snd_front_info *front_info)
179 if (!front_info->evt_pairs)
182 for (i = 0; i < front_info->num_evt_pairs; i++) {
183 evtchnl_free(front_info, &front_info->evt_pairs[i].req);
184 evtchnl_free(front_info, &front_info->evt_pairs[i].evt);
187 kfree(front_info->evt_pairs);
188 front_info->evt_pairs = NULL;
191 static int evtchnl_alloc(struct xen_snd_front_info *front_info, int index,
192 struct xen_snd_front_evtchnl *channel,
193 enum xen_snd_front_evtchnl_type type)
195 struct xenbus_device *xb_dev = front_info->xb_dev;
197 irq_handler_t handler;
198 char *handler_name = NULL;
201 memset(channel, 0, sizeof(*channel));
202 channel->type = type;
203 channel->index = index;
204 channel->front_info = front_info;
205 channel->state = EVTCHNL_STATE_DISCONNECTED;
206 ret = xenbus_setup_ring(xb_dev, GFP_KERNEL, &page, 1, &channel->gref);
210 handler_name = kasprintf(GFP_KERNEL, "%s-%s", XENSND_DRIVER_NAME,
211 type == EVTCHNL_TYPE_REQ ?
212 XENSND_FIELD_RING_REF :
213 XENSND_FIELD_EVT_RING_REF);
219 mutex_init(&channel->ring_io_lock);
221 if (type == EVTCHNL_TYPE_REQ) {
222 struct xen_sndif_sring *sring = page;
224 init_completion(&channel->u.req.completion);
225 mutex_init(&channel->u.req.req_io_lock);
226 XEN_FRONT_RING_INIT(&channel->u.req.ring, sring, XEN_PAGE_SIZE);
228 handler = evtchnl_interrupt_req;
230 channel->u.evt.page = page;
231 handler = evtchnl_interrupt_evt;
234 ret = xenbus_alloc_evtchn(xb_dev, &channel->port);
238 ret = bind_evtchn_to_irq(channel->port);
240 dev_err(&xb_dev->dev,
241 "Failed to bind IRQ for domid %d port %d: %d\n",
242 front_info->xb_dev->otherend_id, channel->port, ret);
248 ret = request_threaded_irq(channel->irq, NULL, handler,
249 IRQF_ONESHOT, handler_name, channel);
251 dev_err(&xb_dev->dev, "Failed to request IRQ %d: %d\n",
261 dev_err(&xb_dev->dev, "Failed to allocate ring: %d\n", ret);
265 int xen_snd_front_evtchnl_create_all(struct xen_snd_front_info *front_info,
268 struct xen_front_cfg_card *cfg = &front_info->cfg;
269 struct device *dev = &front_info->xb_dev->dev;
272 front_info->evt_pairs =
274 sizeof(struct xen_snd_front_evtchnl_pair),
276 if (!front_info->evt_pairs)
279 /* Iterate over devices and their streams and create event channels. */
280 for (d = 0; d < cfg->num_pcm_instances; d++) {
281 struct xen_front_cfg_pcm_instance *pcm_instance;
284 pcm_instance = &cfg->pcm_instances[d];
286 for (s = 0; s < pcm_instance->num_streams_pb; s++) {
287 index = pcm_instance->streams_pb[s].index;
289 ret = evtchnl_alloc(front_info, index,
290 &front_info->evt_pairs[index].req,
293 dev_err(dev, "Error allocating control channel\n");
297 ret = evtchnl_alloc(front_info, index,
298 &front_info->evt_pairs[index].evt,
301 dev_err(dev, "Error allocating in-event channel\n");
306 for (s = 0; s < pcm_instance->num_streams_cap; s++) {
307 index = pcm_instance->streams_cap[s].index;
309 ret = evtchnl_alloc(front_info, index,
310 &front_info->evt_pairs[index].req,
313 dev_err(dev, "Error allocating control channel\n");
317 ret = evtchnl_alloc(front_info, index,
318 &front_info->evt_pairs[index].evt,
321 dev_err(dev, "Error allocating in-event channel\n");
327 front_info->num_evt_pairs = num_streams;
331 xen_snd_front_evtchnl_free_all(front_info);
335 static int evtchnl_publish(struct xenbus_transaction xbt,
336 struct xen_snd_front_evtchnl *channel,
337 const char *path, const char *node_ring,
338 const char *node_chnl)
340 struct xenbus_device *xb_dev = channel->front_info->xb_dev;
343 /* Write control channel ring reference. */
344 ret = xenbus_printf(xbt, path, node_ring, "%u", channel->gref);
346 dev_err(&xb_dev->dev, "Error writing ring-ref: %d\n", ret);
350 /* Write event channel ring reference. */
351 ret = xenbus_printf(xbt, path, node_chnl, "%u", channel->port);
353 dev_err(&xb_dev->dev, "Error writing event channel: %d\n", ret);
360 int xen_snd_front_evtchnl_publish_all(struct xen_snd_front_info *front_info)
362 struct xen_front_cfg_card *cfg = &front_info->cfg;
363 struct xenbus_transaction xbt;
367 ret = xenbus_transaction_start(&xbt);
369 xenbus_dev_fatal(front_info->xb_dev, ret,
370 "starting transaction");
374 for (d = 0; d < cfg->num_pcm_instances; d++) {
375 struct xen_front_cfg_pcm_instance *pcm_instance;
378 pcm_instance = &cfg->pcm_instances[d];
380 for (s = 0; s < pcm_instance->num_streams_pb; s++) {
381 index = pcm_instance->streams_pb[s].index;
383 ret = evtchnl_publish(xbt,
384 &front_info->evt_pairs[index].req,
385 pcm_instance->streams_pb[s].xenstore_path,
386 XENSND_FIELD_RING_REF,
387 XENSND_FIELD_EVT_CHNL);
391 ret = evtchnl_publish(xbt,
392 &front_info->evt_pairs[index].evt,
393 pcm_instance->streams_pb[s].xenstore_path,
394 XENSND_FIELD_EVT_RING_REF,
395 XENSND_FIELD_EVT_EVT_CHNL);
400 for (s = 0; s < pcm_instance->num_streams_cap; s++) {
401 index = pcm_instance->streams_cap[s].index;
403 ret = evtchnl_publish(xbt,
404 &front_info->evt_pairs[index].req,
405 pcm_instance->streams_cap[s].xenstore_path,
406 XENSND_FIELD_RING_REF,
407 XENSND_FIELD_EVT_CHNL);
411 ret = evtchnl_publish(xbt,
412 &front_info->evt_pairs[index].evt,
413 pcm_instance->streams_cap[s].xenstore_path,
414 XENSND_FIELD_EVT_RING_REF,
415 XENSND_FIELD_EVT_EVT_CHNL);
420 ret = xenbus_transaction_end(xbt, 0);
425 xenbus_dev_fatal(front_info->xb_dev, ret,
426 "completing transaction");
431 xenbus_transaction_end(xbt, 1);
433 xenbus_dev_fatal(front_info->xb_dev, ret, "writing XenStore");
437 void xen_snd_front_evtchnl_pair_set_connected(struct xen_snd_front_evtchnl_pair *evt_pair,
440 enum xen_snd_front_evtchnl_state state;
443 state = EVTCHNL_STATE_CONNECTED;
445 state = EVTCHNL_STATE_DISCONNECTED;
447 mutex_lock(&evt_pair->req.ring_io_lock);
448 evt_pair->req.state = state;
449 mutex_unlock(&evt_pair->req.ring_io_lock);
451 mutex_lock(&evt_pair->evt.ring_io_lock);
452 evt_pair->evt.state = state;
453 mutex_unlock(&evt_pair->evt.ring_io_lock);
456 void xen_snd_front_evtchnl_pair_clear(struct xen_snd_front_evtchnl_pair *evt_pair)
458 mutex_lock(&evt_pair->req.ring_io_lock);
459 evt_pair->req.evt_next_id = 0;
460 mutex_unlock(&evt_pair->req.ring_io_lock);
462 mutex_lock(&evt_pair->evt.ring_io_lock);
463 evt_pair->evt.evt_next_id = 0;
464 mutex_unlock(&evt_pair->evt.ring_io_lock);