Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 
0003 /*
0004  * Xen para-virtual sound device
0005  *
0006  * Copyright (C) 2016-2018 EPAM Systems Inc.
0007  *
0008  * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
0009  */
0010 
0011 #include <xen/events.h>
0012 #include <xen/grant_table.h>
0013 #include <xen/xen.h>
0014 #include <xen/xenbus.h>
0015 
0016 #include "xen_snd_front.h"
0017 #include "xen_snd_front_alsa.h"
0018 #include "xen_snd_front_cfg.h"
0019 #include "xen_snd_front_evtchnl.h"
0020 
0021 static irqreturn_t evtchnl_interrupt_req(int irq, void *dev_id)
0022 {
0023     struct xen_snd_front_evtchnl *channel = dev_id;
0024     struct xen_snd_front_info *front_info = channel->front_info;
0025     struct xensnd_resp *resp;
0026     RING_IDX i, rp;
0027 
0028     if (unlikely(channel->state != EVTCHNL_STATE_CONNECTED))
0029         return IRQ_HANDLED;
0030 
0031     mutex_lock(&channel->ring_io_lock);
0032 
0033 again:
0034     rp = channel->u.req.ring.sring->rsp_prod;
0035     /* Ensure we see queued responses up to rp. */
0036     rmb();
0037 
0038     /*
0039      * Assume that the backend is trusted to always write sane values
0040      * to the ring counters, so no overflow checks on frontend side
0041      * are required.
0042      */
0043     for (i = channel->u.req.ring.rsp_cons; i != rp; i++) {
0044         resp = RING_GET_RESPONSE(&channel->u.req.ring, i);
0045         if (resp->id != channel->evt_id)
0046             continue;
0047         switch (resp->operation) {
0048         case XENSND_OP_OPEN:
0049         case XENSND_OP_CLOSE:
0050         case XENSND_OP_READ:
0051         case XENSND_OP_WRITE:
0052         case XENSND_OP_TRIGGER:
0053             channel->u.req.resp_status = resp->status;
0054             complete(&channel->u.req.completion);
0055             break;
0056         case XENSND_OP_HW_PARAM_QUERY:
0057             channel->u.req.resp_status = resp->status;
0058             channel->u.req.resp.hw_param =
0059                     resp->resp.hw_param;
0060             complete(&channel->u.req.completion);
0061             break;
0062 
0063         default:
0064             dev_err(&front_info->xb_dev->dev,
0065                 "Operation %d is not supported\n",
0066                 resp->operation);
0067             break;
0068         }
0069     }
0070 
0071     channel->u.req.ring.rsp_cons = i;
0072     if (i != channel->u.req.ring.req_prod_pvt) {
0073         int more_to_do;
0074 
0075         RING_FINAL_CHECK_FOR_RESPONSES(&channel->u.req.ring,
0076                            more_to_do);
0077         if (more_to_do)
0078             goto again;
0079     } else {
0080         channel->u.req.ring.sring->rsp_event = i + 1;
0081     }
0082 
0083     mutex_unlock(&channel->ring_io_lock);
0084     return IRQ_HANDLED;
0085 }
0086 
0087 static irqreturn_t evtchnl_interrupt_evt(int irq, void *dev_id)
0088 {
0089     struct xen_snd_front_evtchnl *channel = dev_id;
0090     struct xensnd_event_page *page = channel->u.evt.page;
0091     u32 cons, prod;
0092 
0093     if (unlikely(channel->state != EVTCHNL_STATE_CONNECTED))
0094         return IRQ_HANDLED;
0095 
0096     mutex_lock(&channel->ring_io_lock);
0097 
0098     prod = page->in_prod;
0099     /* Ensure we see ring contents up to prod. */
0100     virt_rmb();
0101     if (prod == page->in_cons)
0102         goto out;
0103 
0104     /*
0105      * Assume that the backend is trusted to always write sane values
0106      * to the ring counters, so no overflow checks on frontend side
0107      * are required.
0108      */
0109     for (cons = page->in_cons; cons != prod; cons++) {
0110         struct xensnd_evt *event;
0111 
0112         event = &XENSND_IN_RING_REF(page, cons);
0113         if (unlikely(event->id != channel->evt_id++))
0114             continue;
0115 
0116         switch (event->type) {
0117         case XENSND_EVT_CUR_POS:
0118             xen_snd_front_alsa_handle_cur_pos(channel,
0119                               event->op.cur_pos.position);
0120             break;
0121         }
0122     }
0123 
0124     page->in_cons = cons;
0125     /* Ensure ring contents. */
0126     virt_wmb();
0127 
0128 out:
0129     mutex_unlock(&channel->ring_io_lock);
0130     return IRQ_HANDLED;
0131 }
0132 
0133 void xen_snd_front_evtchnl_flush(struct xen_snd_front_evtchnl *channel)
0134 {
0135     int notify;
0136 
0137     channel->u.req.ring.req_prod_pvt++;
0138     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&channel->u.req.ring, notify);
0139     if (notify)
0140         notify_remote_via_irq(channel->irq);
0141 }
0142 
0143 static void evtchnl_free(struct xen_snd_front_info *front_info,
0144              struct xen_snd_front_evtchnl *channel)
0145 {
0146     void *page = NULL;
0147 
0148     if (channel->type == EVTCHNL_TYPE_REQ)
0149         page = channel->u.req.ring.sring;
0150     else if (channel->type == EVTCHNL_TYPE_EVT)
0151         page = channel->u.evt.page;
0152 
0153     if (!page)
0154         return;
0155 
0156     channel->state = EVTCHNL_STATE_DISCONNECTED;
0157     if (channel->type == EVTCHNL_TYPE_REQ) {
0158         /* Release all who still waits for response if any. */
0159         channel->u.req.resp_status = -EIO;
0160         complete_all(&channel->u.req.completion);
0161     }
0162 
0163     if (channel->irq)
0164         unbind_from_irqhandler(channel->irq, channel);
0165 
0166     if (channel->port)
0167         xenbus_free_evtchn(front_info->xb_dev, channel->port);
0168 
0169     /* End access and free the page. */
0170     xenbus_teardown_ring(&page, 1, &channel->gref);
0171 
0172     memset(channel, 0, sizeof(*channel));
0173 }
0174 
0175 void xen_snd_front_evtchnl_free_all(struct xen_snd_front_info *front_info)
0176 {
0177     int i;
0178 
0179     if (!front_info->evt_pairs)
0180         return;
0181 
0182     for (i = 0; i < front_info->num_evt_pairs; i++) {
0183         evtchnl_free(front_info, &front_info->evt_pairs[i].req);
0184         evtchnl_free(front_info, &front_info->evt_pairs[i].evt);
0185     }
0186 
0187     kfree(front_info->evt_pairs);
0188     front_info->evt_pairs = NULL;
0189 }
0190 
0191 static int evtchnl_alloc(struct xen_snd_front_info *front_info, int index,
0192              struct xen_snd_front_evtchnl *channel,
0193              enum xen_snd_front_evtchnl_type type)
0194 {
0195     struct xenbus_device *xb_dev = front_info->xb_dev;
0196     void *page;
0197     irq_handler_t handler;
0198     char *handler_name = NULL;
0199     int ret;
0200 
0201     memset(channel, 0, sizeof(*channel));
0202     channel->type = type;
0203     channel->index = index;
0204     channel->front_info = front_info;
0205     channel->state = EVTCHNL_STATE_DISCONNECTED;
0206     ret = xenbus_setup_ring(xb_dev, GFP_KERNEL, &page, 1, &channel->gref);
0207     if (ret)
0208         goto fail;
0209 
0210     handler_name = kasprintf(GFP_KERNEL, "%s-%s", XENSND_DRIVER_NAME,
0211                  type == EVTCHNL_TYPE_REQ ?
0212                  XENSND_FIELD_RING_REF :
0213                  XENSND_FIELD_EVT_RING_REF);
0214     if (!handler_name) {
0215         ret = -ENOMEM;
0216         goto fail;
0217     }
0218 
0219     mutex_init(&channel->ring_io_lock);
0220 
0221     if (type == EVTCHNL_TYPE_REQ) {
0222         struct xen_sndif_sring *sring = page;
0223 
0224         init_completion(&channel->u.req.completion);
0225         mutex_init(&channel->u.req.req_io_lock);
0226         XEN_FRONT_RING_INIT(&channel->u.req.ring, sring, XEN_PAGE_SIZE);
0227 
0228         handler = evtchnl_interrupt_req;
0229     } else {
0230         channel->u.evt.page = page;
0231         handler = evtchnl_interrupt_evt;
0232     }
0233 
0234     ret = xenbus_alloc_evtchn(xb_dev, &channel->port);
0235     if (ret < 0)
0236         goto fail;
0237 
0238     ret = bind_evtchn_to_irq(channel->port);
0239     if (ret < 0) {
0240         dev_err(&xb_dev->dev,
0241             "Failed to bind IRQ for domid %d port %d: %d\n",
0242             front_info->xb_dev->otherend_id, channel->port, ret);
0243         goto fail;
0244     }
0245 
0246     channel->irq = ret;
0247 
0248     ret = request_threaded_irq(channel->irq, NULL, handler,
0249                    IRQF_ONESHOT, handler_name, channel);
0250     if (ret < 0) {
0251         dev_err(&xb_dev->dev, "Failed to request IRQ %d: %d\n",
0252             channel->irq, ret);
0253         goto fail;
0254     }
0255 
0256     kfree(handler_name);
0257     return 0;
0258 
0259 fail:
0260     kfree(handler_name);
0261     dev_err(&xb_dev->dev, "Failed to allocate ring: %d\n", ret);
0262     return ret;
0263 }
0264 
0265 int xen_snd_front_evtchnl_create_all(struct xen_snd_front_info *front_info,
0266                      int num_streams)
0267 {
0268     struct xen_front_cfg_card *cfg = &front_info->cfg;
0269     struct device *dev = &front_info->xb_dev->dev;
0270     int d, ret = 0;
0271 
0272     front_info->evt_pairs =
0273             kcalloc(num_streams,
0274                 sizeof(struct xen_snd_front_evtchnl_pair),
0275                 GFP_KERNEL);
0276     if (!front_info->evt_pairs)
0277         return -ENOMEM;
0278 
0279     /* Iterate over devices and their streams and create event channels. */
0280     for (d = 0; d < cfg->num_pcm_instances; d++) {
0281         struct xen_front_cfg_pcm_instance *pcm_instance;
0282         int s, index;
0283 
0284         pcm_instance = &cfg->pcm_instances[d];
0285 
0286         for (s = 0; s < pcm_instance->num_streams_pb; s++) {
0287             index = pcm_instance->streams_pb[s].index;
0288 
0289             ret = evtchnl_alloc(front_info, index,
0290                         &front_info->evt_pairs[index].req,
0291                         EVTCHNL_TYPE_REQ);
0292             if (ret < 0) {
0293                 dev_err(dev, "Error allocating control channel\n");
0294                 goto fail;
0295             }
0296 
0297             ret = evtchnl_alloc(front_info, index,
0298                         &front_info->evt_pairs[index].evt,
0299                         EVTCHNL_TYPE_EVT);
0300             if (ret < 0) {
0301                 dev_err(dev, "Error allocating in-event channel\n");
0302                 goto fail;
0303             }
0304         }
0305 
0306         for (s = 0; s < pcm_instance->num_streams_cap; s++) {
0307             index = pcm_instance->streams_cap[s].index;
0308 
0309             ret = evtchnl_alloc(front_info, index,
0310                         &front_info->evt_pairs[index].req,
0311                         EVTCHNL_TYPE_REQ);
0312             if (ret < 0) {
0313                 dev_err(dev, "Error allocating control channel\n");
0314                 goto fail;
0315             }
0316 
0317             ret = evtchnl_alloc(front_info, index,
0318                         &front_info->evt_pairs[index].evt,
0319                         EVTCHNL_TYPE_EVT);
0320             if (ret < 0) {
0321                 dev_err(dev, "Error allocating in-event channel\n");
0322                 goto fail;
0323             }
0324         }
0325     }
0326 
0327     front_info->num_evt_pairs = num_streams;
0328     return 0;
0329 
0330 fail:
0331     xen_snd_front_evtchnl_free_all(front_info);
0332     return ret;
0333 }
0334 
0335 static int evtchnl_publish(struct xenbus_transaction xbt,
0336                struct xen_snd_front_evtchnl *channel,
0337                const char *path, const char *node_ring,
0338                const char *node_chnl)
0339 {
0340     struct xenbus_device *xb_dev = channel->front_info->xb_dev;
0341     int ret;
0342 
0343     /* Write control channel ring reference. */
0344     ret = xenbus_printf(xbt, path, node_ring, "%u", channel->gref);
0345     if (ret < 0) {
0346         dev_err(&xb_dev->dev, "Error writing ring-ref: %d\n", ret);
0347         return ret;
0348     }
0349 
0350     /* Write event channel ring reference. */
0351     ret = xenbus_printf(xbt, path, node_chnl, "%u", channel->port);
0352     if (ret < 0) {
0353         dev_err(&xb_dev->dev, "Error writing event channel: %d\n", ret);
0354         return ret;
0355     }
0356 
0357     return 0;
0358 }
0359 
0360 int xen_snd_front_evtchnl_publish_all(struct xen_snd_front_info *front_info)
0361 {
0362     struct xen_front_cfg_card *cfg = &front_info->cfg;
0363     struct xenbus_transaction xbt;
0364     int ret, d;
0365 
0366 again:
0367     ret = xenbus_transaction_start(&xbt);
0368     if (ret < 0) {
0369         xenbus_dev_fatal(front_info->xb_dev, ret,
0370                  "starting transaction");
0371         return ret;
0372     }
0373 
0374     for (d = 0; d < cfg->num_pcm_instances; d++) {
0375         struct xen_front_cfg_pcm_instance *pcm_instance;
0376         int s, index;
0377 
0378         pcm_instance = &cfg->pcm_instances[d];
0379 
0380         for (s = 0; s < pcm_instance->num_streams_pb; s++) {
0381             index = pcm_instance->streams_pb[s].index;
0382 
0383             ret = evtchnl_publish(xbt,
0384                           &front_info->evt_pairs[index].req,
0385                           pcm_instance->streams_pb[s].xenstore_path,
0386                           XENSND_FIELD_RING_REF,
0387                           XENSND_FIELD_EVT_CHNL);
0388             if (ret < 0)
0389                 goto fail;
0390 
0391             ret = evtchnl_publish(xbt,
0392                           &front_info->evt_pairs[index].evt,
0393                           pcm_instance->streams_pb[s].xenstore_path,
0394                           XENSND_FIELD_EVT_RING_REF,
0395                           XENSND_FIELD_EVT_EVT_CHNL);
0396             if (ret < 0)
0397                 goto fail;
0398         }
0399 
0400         for (s = 0; s < pcm_instance->num_streams_cap; s++) {
0401             index = pcm_instance->streams_cap[s].index;
0402 
0403             ret = evtchnl_publish(xbt,
0404                           &front_info->evt_pairs[index].req,
0405                           pcm_instance->streams_cap[s].xenstore_path,
0406                           XENSND_FIELD_RING_REF,
0407                           XENSND_FIELD_EVT_CHNL);
0408             if (ret < 0)
0409                 goto fail;
0410 
0411             ret = evtchnl_publish(xbt,
0412                           &front_info->evt_pairs[index].evt,
0413                           pcm_instance->streams_cap[s].xenstore_path,
0414                           XENSND_FIELD_EVT_RING_REF,
0415                           XENSND_FIELD_EVT_EVT_CHNL);
0416             if (ret < 0)
0417                 goto fail;
0418         }
0419     }
0420     ret = xenbus_transaction_end(xbt, 0);
0421     if (ret < 0) {
0422         if (ret == -EAGAIN)
0423             goto again;
0424 
0425         xenbus_dev_fatal(front_info->xb_dev, ret,
0426                  "completing transaction");
0427         goto fail_to_end;
0428     }
0429     return 0;
0430 fail:
0431     xenbus_transaction_end(xbt, 1);
0432 fail_to_end:
0433     xenbus_dev_fatal(front_info->xb_dev, ret, "writing XenStore");
0434     return ret;
0435 }
0436 
0437 void xen_snd_front_evtchnl_pair_set_connected(struct xen_snd_front_evtchnl_pair *evt_pair,
0438                           bool is_connected)
0439 {
0440     enum xen_snd_front_evtchnl_state state;
0441 
0442     if (is_connected)
0443         state = EVTCHNL_STATE_CONNECTED;
0444     else
0445         state = EVTCHNL_STATE_DISCONNECTED;
0446 
0447     mutex_lock(&evt_pair->req.ring_io_lock);
0448     evt_pair->req.state = state;
0449     mutex_unlock(&evt_pair->req.ring_io_lock);
0450 
0451     mutex_lock(&evt_pair->evt.ring_io_lock);
0452     evt_pair->evt.state = state;
0453     mutex_unlock(&evt_pair->evt.ring_io_lock);
0454 }
0455 
0456 void xen_snd_front_evtchnl_pair_clear(struct xen_snd_front_evtchnl_pair *evt_pair)
0457 {
0458     mutex_lock(&evt_pair->req.ring_io_lock);
0459     evt_pair->req.evt_next_id = 0;
0460     mutex_unlock(&evt_pair->req.ring_io_lock);
0461 
0462     mutex_lock(&evt_pair->evt.ring_io_lock);
0463     evt_pair->evt.evt_next_id = 0;
0464     mutex_unlock(&evt_pair->evt.ring_io_lock);
0465 }
0466