1 /*
2 * Copyright (c) 2014 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #pragma once
9
10 #include <lk/compiler.h>
11 #include <assert.h>
12 #include <lk/list.h>
13 #include <sys/types.h>
14 #include <dev/virtio/virtio_ring.h>
15
16 /* detect a virtio mmio hardware block
17 * returns number of devices found */
18 int virtio_mmio_detect(void *ptr, uint count, const uint irqs[], size_t stride);
19
20 #define MAX_VIRTIO_RINGS 4
21
22 struct virtio_mmio_config;
23
24 struct virtio_device {
25 bool valid;
26
27 uint index;
28 uint irq;
29
30 volatile struct virtio_mmio_config *mmio_config;
31 void *config_ptr;
32
33 void *priv; /* a place for the driver to put private data */
34
35 enum handler_return (*irq_driver_callback)(struct virtio_device *dev, uint ring, const struct vring_used_elem *e);
36 enum handler_return (*config_change_callback)(struct virtio_device *dev);
37
38 /* virtio rings */
39 uint32_t active_rings_bitmap;
40 struct vring ring[MAX_VIRTIO_RINGS];
41 };
42
43 void virtio_reset_device(struct virtio_device *dev);
44 void virtio_status_acknowledge_driver(struct virtio_device *dev);
45 void virtio_status_driver_ok(struct virtio_device *dev);
46
47 /* api used by devices to interact with the virtio bus */
48 status_t virtio_alloc_ring(struct virtio_device *dev, uint index, uint16_t len) __NONNULL();
49
50 /* add a descriptor at index desc_index to the free list on ring_index */
51 void virtio_free_desc(struct virtio_device *dev, uint ring_index, uint16_t desc_index);
52
53 /* allocate a descriptor off the free list, 0xffff is error */
54 uint16_t virtio_alloc_desc(struct virtio_device *dev, uint ring_index);
55
56 /* allocate a descriptor chain the free list */
57 struct vring_desc *virtio_alloc_desc_chain(struct virtio_device *dev, uint ring_index, size_t count, uint16_t *start_index);
58
virtio_desc_index_to_desc(struct virtio_device * dev,uint ring_index,uint16_t desc_index)59 static inline struct vring_desc *virtio_desc_index_to_desc(struct virtio_device *dev, uint ring_index, uint16_t desc_index) {
60 DEBUG_ASSERT(desc_index != 0xffff);
61 return &dev->ring[ring_index].desc[desc_index];
62 }
63
64 void virtio_dump_desc(const struct vring_desc *desc);
65
66 /* submit a chain to the avail list */
67 void virtio_submit_chain(struct virtio_device *dev, uint ring_index, uint16_t desc_index);
68
69 void virtio_kick(struct virtio_device *dev, uint ring_idnex);
70
71
72