Home | History | Annotate | Line # | Download | only in pci
virtiovar.h revision 1.22
      1  1.22  yamaguch /*	$NetBSD: virtiovar.h,v 1.22 2021/10/21 05:37:43 yamaguchi Exp $	*/
      2   1.1   hannken 
      3   1.1   hannken /*
      4   1.1   hannken  * Copyright (c) 2010 Minoura Makoto.
      5   1.1   hannken  * All rights reserved.
      6   1.1   hannken  *
      7   1.1   hannken  * Redistribution and use in source and binary forms, with or without
      8   1.1   hannken  * modification, are permitted provided that the following conditions
      9   1.1   hannken  * are met:
     10   1.1   hannken  * 1. Redistributions of source code must retain the above copyright
     11   1.1   hannken  *    notice, this list of conditions and the following disclaimer.
     12   1.1   hannken  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   hannken  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   hannken  *    documentation and/or other materials provided with the distribution.
     15   1.1   hannken  *
     16   1.1   hannken  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1   hannken  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1   hannken  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1   hannken  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1   hannken  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1   hannken  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1   hannken  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1   hannken  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1   hannken  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1   hannken  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1   hannken  */
     27   1.1   hannken 
     28   1.1   hannken /*
     29   1.1   hannken  * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT'
     30   1.1   hannken  * Appendix A.
     31   1.1   hannken  */
     32   1.1   hannken /* An interface for efficient virtio implementation.
     33   1.1   hannken  *
     34   1.1   hannken  * This header is BSD licensed so anyone can use the definitions
     35   1.1   hannken  * to implement compatible drivers/servers.
     36   1.1   hannken  *
     37   1.1   hannken  * Copyright 2007, 2009, IBM Corporation
     38   1.1   hannken  * All rights reserved.
     39   1.1   hannken  *
     40   1.1   hannken  * Redistribution and use in source and binary forms, with or without
     41   1.1   hannken  * modification, are permitted provided that the following conditions
     42   1.1   hannken  * are met:
     43   1.1   hannken  * 1. Redistributions of source code must retain the above copyright
     44   1.1   hannken  *    notice, this list of conditions and the following disclaimer.
     45   1.1   hannken  * 2. Redistributions in binary form must reproduce the above copyright
     46   1.1   hannken  *    notice, this list of conditions and the following disclaimer in the
     47   1.1   hannken  *    documentation and/or other materials provided with the distribution.
     48   1.1   hannken  * 3. Neither the name of IBM nor the names of its contributors
     49   1.1   hannken  *    may be used to endorse or promote products derived from this software
     50   1.1   hannken  *    without specific prior written permission.
     51   1.1   hannken  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
     52   1.1   hannken  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53   1.1   hannken  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54   1.1   hannken  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
     55   1.1   hannken  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56   1.1   hannken  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57   1.1   hannken  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58   1.1   hannken  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59   1.1   hannken  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60   1.1   hannken  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61   1.1   hannken  * SUCH DAMAGE.
     62   1.1   hannken  */
     63   1.1   hannken 
     64   1.1   hannken 
     65   1.1   hannken #ifndef _DEV_PCI_VIRTIOVAR_H_
     66   1.1   hannken #define	_DEV_PCI_VIRTIOVAR_H_
     67   1.1   hannken 
     68   1.1   hannken #include <sys/types.h>
     69   1.1   hannken #include <sys/queue.h>
     70   1.1   hannken #include <sys/bus.h>
     71   1.1   hannken #include <dev/pci/virtioreg.h>
     72   1.1   hannken 
     73  1.17   reinoud 
     74   1.1   hannken struct vq_entry {
     75   1.1   hannken 	SIMPLEQ_ENTRY(vq_entry)	qe_list; /* free list */
     76   1.1   hannken 	uint16_t		qe_index; /* index in vq_desc array */
     77   1.1   hannken 	/* followings are used only when it is the `head' entry */
     78   1.1   hannken 	int16_t			qe_next;     /* next enq slot */
     79   1.1   hannken 	bool			qe_indirect; /* 1 if using indirect */
     80   1.1   hannken 	struct vring_desc	*qe_desc_base;
     81   1.1   hannken };
     82   1.1   hannken 
     83   1.1   hannken struct virtqueue {
     84   1.1   hannken 	struct virtio_softc	*vq_owner;
     85   1.1   hannken         unsigned int		vq_num; /* queue size (# of entries) */
     86   1.1   hannken 	int			vq_index; /* queue number (0, 1, ...) */
     87   1.1   hannken 
     88   1.1   hannken 	/* vring pointers (KVA) */
     89   1.1   hannken         struct vring_desc	*vq_desc;
     90   1.1   hannken         struct vring_avail	*vq_avail;
     91   1.1   hannken         struct vring_used	*vq_used;
     92   1.1   hannken 	void			*vq_indirect;
     93  1.17   reinoud 	uint16_t		*vq_used_event;		/* trails avail */
     94  1.17   reinoud 	uint16_t		*vq_avail_event;	/* trails used  */
     95   1.1   hannken 
     96   1.1   hannken 	/* virtqueue allocation info */
     97   1.1   hannken 	void			*vq_vaddr;
     98   1.1   hannken 	int			vq_availoffset;
     99   1.1   hannken 	int			vq_usedoffset;
    100   1.1   hannken 	int			vq_indirectoffset;
    101   1.1   hannken 	bus_dma_segment_t	vq_segs[1];
    102   1.1   hannken 	unsigned int		vq_bytesize;
    103   1.1   hannken 	bus_dmamap_t		vq_dmamap;
    104   1.1   hannken 
    105   1.1   hannken 	int			vq_maxsegsize;
    106   1.1   hannken 	int			vq_maxnsegs;
    107   1.1   hannken 
    108   1.1   hannken 	/* free entry management */
    109   1.1   hannken 	struct vq_entry		*vq_entries;
    110   1.1   hannken 	SIMPLEQ_HEAD(, vq_entry) vq_freelist;
    111   1.1   hannken 	kmutex_t		vq_freelist_lock;
    112   1.1   hannken 
    113   1.1   hannken 	/* enqueue/dequeue status */
    114   1.1   hannken 	uint16_t		vq_avail_idx;
    115   1.1   hannken 	uint16_t		vq_used_idx;
    116   1.1   hannken 	int			vq_queued;
    117   1.1   hannken 	kmutex_t		vq_aring_lock;
    118   1.1   hannken 	kmutex_t		vq_uring_lock;
    119   1.1   hannken 
    120   1.1   hannken 	/* interrupt handler */
    121   1.1   hannken 	int			(*vq_done)(struct virtqueue*);
    122  1.15  yamaguch 	int			(*vq_intrhand)(void *);
    123  1.16  yamaguch 	void			*vq_intrhand_arg;
    124  1.17   reinoud 
    125  1.17   reinoud 	/* for 1.0 */
    126  1.17   reinoud 	uint32_t		vq_notify_off;
    127   1.1   hannken };
    128   1.1   hannken 
    129   1.7  jdolecek struct virtio_attach_args {
    130   1.7  jdolecek 	int			sc_childdevid;
    131   1.7  jdolecek };
    132   1.7  jdolecek 
    133   1.7  jdolecek typedef int (*virtio_callback)(struct virtio_softc*);
    134   1.7  jdolecek 
    135   1.7  jdolecek #ifdef VIRTIO_PRIVATE
    136  1.11  jakllsch struct virtio_ops {
    137  1.11  jakllsch 	void		(*kick)(struct virtio_softc *, uint16_t);
    138  1.11  jakllsch 	uint16_t	(*read_queue_size)(struct virtio_softc *, uint16_t);
    139  1.17   reinoud 	void		(*setup_queue)(struct virtio_softc *, uint16_t, uint64_t);
    140  1.11  jakllsch 	void		(*set_status)(struct virtio_softc *, int);
    141  1.17   reinoud 	void		(*neg_features)(struct virtio_softc *, uint64_t);
    142  1.21  yamaguch 	int		(*alloc_interrupts)(struct virtio_softc *);
    143  1.21  yamaguch 	void		(*free_interrupts)(struct virtio_softc *);
    144  1.22  yamaguch 	int		(*setup_interrupts)(struct virtio_softc *, int);
    145  1.11  jakllsch };
    146  1.11  jakllsch 
    147   1.1   hannken struct virtio_softc {
    148   1.1   hannken 	device_t		sc_dev;
    149  1.11  jakllsch 	const struct virtio_ops *sc_ops;
    150   1.1   hannken 	bus_dma_tag_t		sc_dmat;
    151   1.1   hannken 
    152  1.19   reinoud 	int			sc_bus_endian;
    153  1.19   reinoud 	int			sc_struct_endian;
    154  1.19   reinoud 
    155  1.17   reinoud 	bus_space_tag_t		sc_devcfg_iot;
    156  1.17   reinoud 	bus_space_handle_t	sc_devcfg_ioh;
    157  1.17   reinoud 	bus_size_t		sc_devcfg_iosize;
    158  1.17   reinoud 
    159   1.1   hannken 	int			sc_ipl; /* set by child */
    160   1.4     ozaki 	void			*sc_soft_ih;
    161   1.1   hannken 
    162   1.3     ozaki 	int			sc_flags; /* set by child */
    163   1.3     ozaki 
    164  1.17   reinoud 	uint64_t		sc_active_features;
    165   1.1   hannken 	bool			sc_indirect;
    166  1.17   reinoud 	bool			sc_version_1;
    167  1.18   reinoud 	bool			sc_finished_called;
    168   1.1   hannken 
    169   1.1   hannken 	int			sc_nvqs; /* set by child */
    170   1.1   hannken 	struct virtqueue	*sc_vqs; /* set by child */
    171   1.1   hannken 
    172   1.1   hannken 	int			sc_childdevid;
    173   1.7  jdolecek 	device_t		sc_child; 		/* set by child */
    174  1.13  yamaguch 	bool			sc_child_mq;
    175   1.7  jdolecek 	virtio_callback		sc_config_change; 	/* set by child */
    176   1.7  jdolecek 	virtio_callback		sc_intrhand;		/* set by child */
    177   1.1   hannken };
    178   1.7  jdolecek #else
    179   1.7  jdolecek struct virtio_softc;
    180   1.7  jdolecek #endif
    181   1.1   hannken 
    182  1.17   reinoud 
    183  1.17   reinoud /* interupt types, stored in virtio_softc->sc_flags */
    184  1.17   reinoud #define VIRTIO_F_INTR_MPSAFE	(1 << 0)
    185  1.17   reinoud #define VIRTIO_F_INTR_SOFTINT	(1 << 1)
    186  1.17   reinoud #define VIRTIO_F_INTR_MSIX	(1 << 2)
    187  1.17   reinoud 
    188   1.3     ozaki 
    189   1.7  jdolecek #define	VIRTIO_CHILD_FAILED		((void *)1)
    190   1.7  jdolecek 
    191   1.1   hannken /* public interface */
    192  1.17   reinoud void virtio_negotiate_features(struct virtio_softc*, uint64_t);
    193   1.1   hannken 
    194   1.1   hannken uint8_t virtio_read_device_config_1(struct virtio_softc *, int);
    195   1.1   hannken uint16_t virtio_read_device_config_2(struct virtio_softc *, int);
    196   1.1   hannken uint32_t virtio_read_device_config_4(struct virtio_softc *, int);
    197   1.1   hannken uint64_t virtio_read_device_config_8(struct virtio_softc *, int);
    198  1.17   reinoud uint16_t virtio_read_device_config_le_2(struct virtio_softc *, int);
    199  1.17   reinoud uint32_t virtio_read_device_config_le_4(struct virtio_softc *, int);
    200   1.1   hannken void virtio_write_device_config_1(struct virtio_softc *, int, uint8_t);
    201   1.1   hannken void virtio_write_device_config_2(struct virtio_softc *, int, uint16_t);
    202   1.1   hannken void virtio_write_device_config_4(struct virtio_softc *, int, uint32_t);
    203   1.1   hannken void virtio_write_device_config_8(struct virtio_softc *, int, uint64_t);
    204  1.17   reinoud void virtio_write_device_config_le_2(struct virtio_softc *, int, uint16_t);
    205  1.17   reinoud void virtio_write_device_config_le_4(struct virtio_softc *, int, uint32_t);
    206   1.1   hannken 
    207   1.1   hannken int virtio_alloc_vq(struct virtio_softc*, struct virtqueue*, int, int, int,
    208   1.1   hannken 		    const char*);
    209   1.1   hannken int virtio_free_vq(struct virtio_softc*, struct virtqueue*);
    210   1.1   hannken void virtio_reset(struct virtio_softc *);
    211   1.1   hannken void virtio_reinit_start(struct virtio_softc *);
    212   1.1   hannken void virtio_reinit_end(struct virtio_softc *);
    213   1.7  jdolecek void virtio_child_attach_start(struct virtio_softc *, device_t, int,
    214   1.7  jdolecek                     struct virtqueue *,
    215   1.7  jdolecek                     virtio_callback, virtio_callback, int,
    216   1.7  jdolecek 		    int, const char *);
    217  1.13  yamaguch void virtio_child_attach_set_vqs(struct virtio_softc *,
    218  1.13  yamaguch                     struct virtqueue *, int);
    219   1.7  jdolecek int virtio_child_attach_finish(struct virtio_softc *);
    220   1.7  jdolecek void virtio_child_attach_failed(struct virtio_softc *);
    221   1.7  jdolecek void virtio_child_detach(struct virtio_softc *);
    222   1.1   hannken 
    223   1.1   hannken int virtio_enqueue_prep(struct virtio_softc*, struct virtqueue*, int*);
    224   1.1   hannken int virtio_enqueue_reserve(struct virtio_softc*, struct virtqueue*, int, int);
    225   1.1   hannken int virtio_enqueue(struct virtio_softc*, struct virtqueue*, int,
    226   1.1   hannken 		   bus_dmamap_t, bool);
    227   1.1   hannken int virtio_enqueue_p(struct virtio_softc*, struct virtqueue*, int,
    228   1.1   hannken 		     bus_dmamap_t, bus_addr_t, bus_size_t, bool);
    229   1.1   hannken int virtio_enqueue_commit(struct virtio_softc*, struct virtqueue*, int, bool);
    230   1.1   hannken int virtio_enqueue_abort(struct virtio_softc*, struct virtqueue*, int);
    231   1.1   hannken 
    232   1.1   hannken int virtio_dequeue(struct virtio_softc*, struct virtqueue*, int *, int *);
    233   1.1   hannken int virtio_dequeue_commit(struct virtio_softc*, struct virtqueue*, int);
    234   1.1   hannken 
    235  1.16  yamaguch bool virtio_vq_is_enqueued(struct virtio_softc *, struct virtqueue *);
    236   1.1   hannken int virtio_vq_intr(struct virtio_softc *);
    237  1.16  yamaguch int virtio_vq_intrhand(struct virtio_softc *);
    238  1.17   reinoud int virtio_postpone_intr(struct virtio_softc *sc, struct virtqueue *vq,
    239  1.17   reinoud 		uint16_t nslots);
    240  1.17   reinoud int virtio_postpone_intr_smart(struct virtio_softc *sc, struct virtqueue *vq);
    241  1.17   reinoud int virtio_postpone_intr_far(struct virtio_softc *sc, struct virtqueue *vq);
    242   1.1   hannken void virtio_stop_vq_intr(struct virtio_softc *, struct virtqueue *);
    243  1.17   reinoud int virtio_start_vq_intr(struct virtio_softc *, struct virtqueue *);
    244   1.1   hannken 
    245   1.7  jdolecek /* encapsulation */
    246   1.7  jdolecek bus_dma_tag_t	virtio_dmat(struct virtio_softc *);
    247   1.7  jdolecek device_t	virtio_child(struct virtio_softc *);
    248   1.7  jdolecek int		virtio_intrhand(struct virtio_softc *);
    249  1.17   reinoud uint64_t	virtio_features(struct virtio_softc *);
    250   1.7  jdolecek 
    251  1.10    cherry /* autoconf(9) common */
    252  1.10    cherry void virtio_set_status(struct virtio_softc *, int);
    253  1.17   reinoud void virtio_print_device_type(device_t, int, int);
    254  1.17   reinoud int virtio_attach_failed(struct virtio_softc *);
    255  1.10    cherry 
    256  1.10    cherry #define virtio_device_reset(sc)	virtio_set_status((sc), 0)
    257  1.10    cherry 
    258  1.17   reinoud /* endian conversion */
    259  1.17   reinoud 
    260  1.17   reinoud /*
    261  1.17   reinoud  * Virtio structures are read/written in host endian for 0.9 but little endian
    262  1.17   reinoud  * for 1.0. One notable exception is AArch BE that always uses little endian.
    263  1.17   reinoud  */
    264  1.17   reinoud uint16_t virtio_rw16(struct virtio_softc *sc, uint16_t val);
    265  1.17   reinoud uint32_t virtio_rw32(struct virtio_softc *sc, uint32_t val);
    266  1.17   reinoud uint64_t virtio_rw64(struct virtio_softc *sc, uint64_t val);
    267  1.17   reinoud 
    268   1.1   hannken #endif /* _DEV_PCI_VIRTIOVAR_H_ */
    269