1 /*
2 * Copyright (c) 2014 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #pragma once
24
25 #include <compiler.h>
26 #include <assert.h>
27 #include <list.h>
28 #include <sys/types.h>
29 #include <dev/virtio/virtio_ring.h>
30
31 /* detect a virtio mmio hardware block
32 * returns number of devices found */
33 int virtio_mmio_detect(void *ptr, uint count, const uint irqs[]);
34
35 #define MAX_VIRTIO_RINGS 4
36
37 struct virtio_mmio_config;
38
39 struct virtio_device {
40 bool valid;
41
42 uint index;
43 uint irq;
44
45 volatile struct virtio_mmio_config *mmio_config;
46 void *config_ptr;
47
48 void *priv; /* a place for the driver to put private data */
49
50 enum handler_return (*irq_driver_callback)(struct virtio_device *dev, uint ring, const struct vring_used_elem *e);
51 enum handler_return (*config_change_callback)(struct virtio_device *dev);
52
53 /* virtio rings */
54 uint32_t active_rings_bitmap;
55 struct vring ring[MAX_VIRTIO_RINGS];
56 };
57
58 void virtio_reset_device(struct virtio_device *dev);
59 void virtio_status_acknowledge_driver(struct virtio_device *dev);
60 void virtio_status_driver_ok(struct virtio_device *dev);
61
62 /* api used by devices to interact with the virtio bus */
63 status_t virtio_alloc_ring(struct virtio_device *dev, uint index, uint16_t len) __NONNULL();
64
65 /* add a descriptor at index desc_index to the free list on ring_index */
66 void virtio_free_desc(struct virtio_device *dev, uint ring_index, uint16_t desc_index);
67
68 /* allocate a descriptor off the free list, 0xffff is error */
69 uint16_t virtio_alloc_desc(struct virtio_device *dev, uint ring_index);
70
71 /* allocate a descriptor chain the free list */
72 struct vring_desc *virtio_alloc_desc_chain(struct virtio_device *dev, uint ring_index, size_t count, uint16_t *start_index);
73
virtio_desc_index_to_desc(struct virtio_device * dev,uint ring_index,uint16_t desc_index)74 static inline struct vring_desc *virtio_desc_index_to_desc(struct virtio_device *dev, uint ring_index, uint16_t desc_index)
75 {
76 DEBUG_ASSERT(desc_index != 0xffff);
77 return &dev->ring[ring_index].desc[desc_index];
78 }
79
80 void virtio_dump_desc(const struct vring_desc *desc);
81
82 /* submit a chain to the avail list */
83 void virtio_submit_chain(struct virtio_device *dev, uint ring_index, uint16_t desc_index);
84
85 void virtio_kick(struct virtio_device *dev, uint ring_idnex);
86
87
88