evtchn_dev.c revision b26a64ae582e72d0b4c710cd8eba9c4afd4a9fdd
843e19887f64dde75055cf8842fc4db2171eff45johnlev * CDDL HEADER START
843e19887f64dde75055cf8842fc4db2171eff45johnlev * The contents of this file are subject to the terms of the
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Common Development and Distribution License (the "License").
843e19887f64dde75055cf8842fc4db2171eff45johnlev * You may not use this file except in compliance with the License.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
843e19887f64dde75055cf8842fc4db2171eff45johnlev * See the License for the specific language governing permissions
843e19887f64dde75055cf8842fc4db2171eff45johnlev * and limitations under the License.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * When distributing Covered Code, include this CDDL HEADER in each
843e19887f64dde75055cf8842fc4db2171eff45johnlev * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * If applicable, add the following below this CDDL HEADER, with the
843e19887f64dde75055cf8842fc4db2171eff45johnlev * fields enclosed by brackets "[]" replaced with your own identifying
843e19887f64dde75055cf8842fc4db2171eff45johnlev * information: Portions Copyright [yyyy] [name of copyright owner]
843e19887f64dde75055cf8842fc4db2171eff45johnlev * CDDL HEADER END
b26a64ae582e72d0b4c710cd8eba9c4afd4a9fddjohnlev * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Use is subject to license terms.
843e19887f64dde75055cf8842fc4db2171eff45johnlev#pragma ident "%Z%%M% %I% %E% SMI"
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Driver for receiving and demuxing event-channel signals.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Copyright (c) 2004-2005, K A Fraser
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Multi-process extensions Copyright (c) 2004, Steven Smith
843e19887f64dde75055cf8842fc4db2171eff45johnlev * This file may be distributed separately from the Linux kernel, or
843e19887f64dde75055cf8842fc4db2171eff45johnlev * incorporated into other software packages, subject to the following license:
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Permission is hereby granted, free of charge, to any person obtaining a copy
843e19887f64dde75055cf8842fc4db2171eff45johnlev * of this source file (the "Software"), to deal in the Software without
843e19887f64dde75055cf8842fc4db2171eff45johnlev * restriction, including without limitation the rights to use, copy, modify,
843e19887f64dde75055cf8842fc4db2171eff45johnlev * merge, publish, distribute, sublicense, and/or sell copies of the Software,
843e19887f64dde75055cf8842fc4db2171eff45johnlev * and to permit persons to whom the Software is furnished to do so, subject to
843e19887f64dde75055cf8842fc4db2171eff45johnlev * the following conditions:
843e19887f64dde75055cf8842fc4db2171eff45johnlev * The above copyright notice and this permission notice shall be included in
843e19887f64dde75055cf8842fc4db2171eff45johnlev * all copies or substantial portions of the Software.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
843e19887f64dde75055cf8842fc4db2171eff45johnlev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
843e19887f64dde75055cf8842fc4db2171eff45johnlev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
843e19887f64dde75055cf8842fc4db2171eff45johnlev * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
843e19887f64dde75055cf8842fc4db2171eff45johnlev * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
843e19887f64dde75055cf8842fc4db2171eff45johnlev * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
843e19887f64dde75055cf8842fc4db2171eff45johnlev * IN THE SOFTWARE.
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* Some handy macros */
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* Soft state data structure for evtchn driver */
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* Notification ring, accessed via /dev/xen/evtchn. */
843e19887f64dde75055cf8842fc4db2171eff45johnlev#define EVTCHN_RING_SIZE (PAGESIZE / sizeof (evtchn_port_t))
843e19887f64dde75055cf8842fc4db2171eff45johnlev#define EVTCHN_RING_MASK(_i) ((_i) & (EVTCHN_RING_SIZE - 1))
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* Processes wait on this queue when ring is empty. */
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* last pid to bind to this event channel. debug aid. */
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic int evtchndrv_detach(dev_info_t *, ddi_detach_cmd_t);
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* Who's bound to each port? */
843e19887f64dde75055cf8842fc4db2171eff45johnlevstatic struct evtsoftdata *port_user[NR_EVENT_CHANNELS];
843e19887f64dde75055cf8842fc4db2171eff45johnlev * This is quite gross, we had to leave the evtchn that led to this
843e19887f64dde75055cf8842fc4db2171eff45johnlev * invocation in a global mailbox, retrieve it now.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * We do this because the interface doesn't offer us a way to pass
843e19887f64dde75055cf8842fc4db2171eff45johnlev * a dynamic argument up through the generic interrupt service layer.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * The mailbox is safe since we either run with interrupts disabled or
843e19887f64dde75055cf8842fc4db2171eff45johnlev * non-preemptable till we reach here.
843e19887f64dde75055cf8842fc4db2171eff45johnlev if ((ep->ring_prod - ep->ring_cons) < EVTCHN_RING_SIZE) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Wake up reader when ring goes non-empty
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* ARGSUSED */
b26a64ae582e72d0b4c710cd8eba9c4afd4a9fddjohnlevevtchndrv_read(dev_t dev, struct uio *uio, cred_t *cr)
843e19887f64dde75055cf8842fc4db2171eff45johnlev ep = EVTCHNDRV_INST2SOFTS(EVTCHNDRV_MINOR2INST(minor));
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* Whole number of ports. */
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (0);
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (cv_wait_sig(&ep->evtchn_wait, &ep->evtchn_lock) == 0) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev bytes2 = EVTCHN_RING_MASK(p) * sizeof (evtchn_port_t);
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* Truncate chunks according to caller's maximum byte count. */
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (uiomove(&ep->ring[EVTCHN_RING_MASK(c)], bytes1, UIO_READ, uio) ||
843e19887f64dde75055cf8842fc4db2171eff45johnlev ((bytes2 != 0) && uiomove(&ep->ring[0], bytes2, UIO_READ, uio))) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev ep->ring_cons += (bytes1 + bytes2) / sizeof (evtchn_port_t);
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* ARGSUSED */
b26a64ae582e72d0b4c710cd8eba9c4afd4a9fddjohnlevevtchndrv_write(dev_t dev, struct uio *uio, cred_t *cr)
843e19887f64dde75055cf8842fc4db2171eff45johnlev ep = EVTCHNDRV_INST2SOFTS(EVTCHNDRV_MINOR2INST(minor));
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* Whole number of ports. */
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (count == 0) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev if ((rc = uiomove(kbuf, count, UIO_WRITE, uio)) != 0)
843e19887f64dde75055cf8842fc4db2171eff45johnlev for (i = 0; i < (count / sizeof (evtchn_port_t)); i++)
843e19887f64dde75055cf8842fc4db2171eff45johnlev * save away the PID of the last process to bind to this event channel.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Useful for debugging.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * It is possible the event is in transit to us.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * If it is already in the ring buffer, then a client may
843e19887f64dde75055cf8842fc4db2171eff45johnlev * get a spurious event notification on the next read of
843e19887f64dde75055cf8842fc4db2171eff45johnlev * of the evtchn device. Clients will need to be able to
843e19887f64dde75055cf8842fc4db2171eff45johnlev * handle getting a spurious event notification.
843e19887f64dde75055cf8842fc4db2171eff45johnlev * The event is masked and should stay so, clean it up.
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* ARGSUSED */
b26a64ae582e72d0b4c710cd8eba9c4afd4a9fddjohnlevevtchndrv_ioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cr,
843e19887f64dde75055cf8842fc4db2171eff45johnlev ep = EVTCHNDRV_INST2SOFTS(EVTCHNDRV_MINOR2INST(minor));
843e19887f64dde75055cf8842fc4db2171eff45johnlev if ((err = xen_bind_virq(bind.virq, 0, rvalp)) != 0)
843e19887f64dde75055cf8842fc4db2171eff45johnlev if ((err = xen_alloc_unbound_evtchn(bind.remote_domain,
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (copyin((void *)data, &unbind, sizeof (unbind))) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev if (copyin((void *)data, ¬ify, sizeof (notify))) {
843e19887f64dde75055cf8842fc4db2171eff45johnlevevtchndrv_poll(dev_t dev, short ev, int anyyet, short *revp, pollhead_t **phpp)
843e19887f64dde75055cf8842fc4db2171eff45johnlev ep = EVTCHNDRV_INST2SOFTS(EVTCHNDRV_MINOR2INST(minor));
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (0);
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* ARGSUSED */
843e19887f64dde75055cf8842fc4db2171eff45johnlevevtchndrv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
843e19887f64dde75055cf8842fc4db2171eff45johnlev * only allow open on minor = 0 - the clone device
843e19887f64dde75055cf8842fc4db2171eff45johnlev * find a free slot and grab it
843e19887f64dde75055cf8842fc4db2171eff45johnlev for (minor = 1; minor < evtchndrv_nclones; minor++) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* Allocate softstate structure */
843e19887f64dde75055cf8842fc4db2171eff45johnlev ep = EVTCHNDRV_INST2SOFTS(EVTCHNDRV_MINOR2INST(minor));
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* ... and init it */
843e19887f64dde75055cf8842fc4db2171eff45johnlev mutex_init(&ep->evtchn_lock, NULL, MUTEX_DEFAULT, NULL);
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* clone driver */
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (0);
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* ARGSUSED */
843e19887f64dde75055cf8842fc4db2171eff45johnlevevtchndrv_close(dev_t dev, int flag, int otyp, struct cred *credp)
843e19887f64dde75055cf8842fc4db2171eff45johnlev ep = EVTCHNDRV_INST2SOFTS(EVTCHNDRV_MINOR2INST(minor));
843e19887f64dde75055cf8842fc4db2171eff45johnlev for (i = 0; i < NR_EVENT_CHANNELS; i++) {
843e19887f64dde75055cf8842fc4db2171eff45johnlev ddi_soft_state_free(evtchndrv_statep, EVTCHNDRV_MINOR2INST(minor));
843e19887f64dde75055cf8842fc4db2171eff45johnlev * free clone tab slot
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (0);
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* ARGSUSED */
843e19887f64dde75055cf8842fc4db2171eff45johnlevevtchndrv_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
843e19887f64dde75055cf8842fc4db2171eff45johnlev *result = (void *)0;
843e19887f64dde75055cf8842fc4db2171eff45johnlevevtchndrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
843e19887f64dde75055cf8842fc4db2171eff45johnlev cmn_err(CE_WARN, "evtchn_attach: unknown cmd 0x%x\n", cmd);
843e19887f64dde75055cf8842fc4db2171eff45johnlev /* DDI_ATTACH */
843e19887f64dde75055cf8842fc4db2171eff45johnlev * only one instance - but we clone using the open routine
843e19887f64dde75055cf8842fc4db2171eff45johnlev mutex_init(&evtchndrv_clone_tab_mutex, NULL, MUTEX_DRIVER,
843e19887f64dde75055cf8842fc4db2171eff45johnlev error = ddi_create_minor_node(dip, "evtchn", S_IFCHR, unit,
843e19887f64dde75055cf8842fc4db2171eff45johnlev * save dip for getinfo
843e19887f64dde75055cf8842fc4db2171eff45johnlev mutex_init(&port_user_lock, NULL, MUTEX_DRIVER, NULL);
843e19887f64dde75055cf8842fc4db2171eff45johnlev (void) add_avintr(NULL, IPL_EVTCHN, (avfunc)evtchn_device_upcall,
843e19887f64dde75055cf8842fc4db2171eff45johnlev "evtchn_driver", ec_dev_irq, NULL, NULL, NULL, dip);
843e19887f64dde75055cf8842fc4db2171eff45johnlev/*ARGSUSED*/
843e19887f64dde75055cf8842fc4db2171eff45johnlevevtchndrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
843e19887f64dde75055cf8842fc4db2171eff45johnlev * Don't allow detach for now.
843e19887f64dde75055cf8842fc4db2171eff45johnlev/* Solaris driver framework */
843e19887f64dde75055cf8842fc4db2171eff45johnlev 0, /* cb_stream */
843e19887f64dde75055cf8842fc4db2171eff45johnlev 0, /* devo_refcnt */
843e19887f64dde75055cf8842fc4db2171eff45johnlev &mod_driverops, /* Type of module. This one is a driver */
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (e);
843e19887f64dde75055cf8842fc4db2171eff45johnlev return (0);