xref: /haiku/headers/private/virtio/virtio.h (revision 3634f142352af2428aed187781fc9d75075e9140)
1 /*
2  * Copyright 2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _VIRTIO_H_
6 #define _VIRTIO_H_
7 
8 
9 #include <device_manager.h>
10 #include <KernelExport.h>
11 
12 
13 #define VIRTIO_DEVICE_ID_NETWORK	1
14 #define VIRTIO_DEVICE_ID_BLOCK		2
15 #define VIRTIO_DEVICE_ID_CONSOLE	3
16 #define VIRTIO_DEVICE_ID_ENTROPY	4
17 #define VIRTIO_DEVICE_ID_BALLOON	5
18 #define VIRTIO_DEVICE_ID_IOMEMORY	6
19 #define VIRTIO_DEVICE_ID_RP_MESSAGE	7
20 #define VIRTIO_DEVICE_ID_SCSI		8
21 #define VIRTIO_DEVICE_ID_9P			9
22 #define VIRTIO_DEVICE_ID_RP_SERIAL	11
23 #define VIRTIO_DEVICE_ID_CAIF		12
24 #define VIRTIO_DEVICE_ID_GPU		16
25 #define VIRTIO_DEVICE_ID_INPUT		18
26 #define VIRTIO_DEVICE_ID_VSOCK		19
27 #define VIRTIO_DEVICE_ID_CRYPTO		20
28 
29 #define VIRTIO_FEATURE_TRANSPORT_MASK	((1ULL << 28) - 1)
30 
31 #define VIRTIO_FEATURE_NOTIFY_ON_EMPTY		(1 << 24)
32 #define VIRTIO_FEATURE_ANY_LAYOUT			(1 << 27)
33 #define VIRTIO_FEATURE_RING_INDIRECT_DESC	(1 << 28)
34 #define VIRTIO_FEATURE_RING_EVENT_IDX		(1 << 29)
35 #define VIRTIO_FEATURE_BAD_FEATURE			(1 << 30)
36 #define VIRTIO_FEATURE_VERSION_1			(1ULL << 32)
37 
38 #define VIRTIO_VIRTQUEUES_MAX_COUNT	8
39 
40 #define VIRTIO_CONFIG_STATUS_RESET	0x00
41 #define VIRTIO_CONFIG_STATUS_ACK	0x01
42 #define VIRTIO_CONFIG_STATUS_DRIVER	0x02
43 #define VIRTIO_CONFIG_STATUS_DRIVER_OK	0x04
44 #define VIRTIO_CONFIG_STATUS_FEATURES_OK	0x08
45 #define VIRTIO_CONFIG_STATUS_DEVICE_NEEDS_RESET	0x40
46 #define VIRTIO_CONFIG_STATUS_FAILED	0x80
47 
48 // attributes:
49 
50 // node type
51 #define VIRTIO_BUS_TYPE_NAME "bus/virtio/v1"
52 // device type (uint16)
53 #define VIRTIO_DEVICE_TYPE_ITEM "virtio/type"
54 // alignment (uint16)
55 #define VIRTIO_VRING_ALIGNMENT_ITEM "virtio/vring_alignment"
56 // version (uint8)
57 #define VIRTIO_VERSION_ITEM "virtio/version"
58 
59 // sim cookie, issued by virtio bus manager
60 typedef void* virtio_sim;
61 // device cookie, issued by virtio bus manager
62 typedef void* virtio_device;
63 // queue cookie, issued by virtio bus manager
64 typedef void* virtio_queue;
65 // callback function for requests
66 typedef void (*virtio_callback_func)(void* driverCookie, void* cookie);
67 // callback function for interrupts
68 typedef void (*virtio_intr_func)(void* cookie);
69 
70 #define	VIRTIO_DEVICE_MODULE_NAME "bus_managers/virtio/device/v1"
71 
72 typedef struct {
73 	driver_module_info info;
74 
75 	status_t	(*queue_interrupt_handler)(virtio_sim sim, uint16 queue);
76 	status_t	(*config_interrupt_handler)(virtio_sim sim);
77 } virtio_for_controller_interface;
78 
79 #define VIRTIO_FOR_CONTROLLER_MODULE_NAME "bus_managers/virtio/controller/driver_v1"
80 
81 // Bus manager interface used by Virtio controller drivers.
82 typedef struct {
83 	driver_module_info info;
84 
85 	void (*set_sim)(void* cookie, virtio_sim sim);
86 	status_t (*read_host_features)(void* cookie, uint64* features);
87 	status_t (*write_guest_features)(void* cookie, uint64 features);
88 	uint8 (*get_status)(void* cookie);
89 	void (*set_status)(void* cookie, uint8 status);
90 	status_t (*read_device_config)(void* cookie, uint8 offset, void* buffer,
91 		size_t bufferSize);
92 	status_t (*write_device_config)(void* cookie, uint8 offset,
93 		const void* buffer, size_t bufferSize);
94 
95 	uint16	(*get_queue_ring_size)(void* cookie, uint16 queue);
96 	status_t (*setup_queue)(void* cookie, uint16 queue, phys_addr_t phy, phys_addr_t phyAvail,
97 		phys_addr_t phyUsed);
98 	status_t (*setup_interrupt)(void* cookie, uint16 queueCount);
99 	status_t (*free_interrupt)(void* cookie);
100 	void	(*notify_queue)(void* cookie, uint16 queue);
101 } virtio_sim_interface;
102 
103 
104 // bus manager device interface for peripheral driver
105 typedef struct {
106 	driver_module_info info;
107 
108 	status_t (*negotiate_features)(virtio_device cookie, uint64 supported,
109 		uint64* negotiated, const char* (*get_feature_name)(uint64));
110 
111 	status_t (*clear_feature)(virtio_device cookie, uint64 feature);
112 
113 	status_t (*read_device_config)(virtio_device cookie, uint8 offset,
114 		void* buffer, size_t bufferSize);
115 	status_t (*write_device_config)(virtio_device cookie, uint8 offset,
116 		const void* buffer, size_t bufferSize);
117 
118 	status_t (*alloc_queues)(virtio_device cookie, size_t count,
119 		virtio_queue* queues);
120 
121 	void (*free_queues)(virtio_device cookie);
122 
123 	status_t (*setup_interrupt)(virtio_device cookie,
124 		virtio_intr_func config_handler, void* driverCookie);
125 
126 	status_t (*free_interrupts)(virtio_device cookie);
127 
128 	status_t (*queue_setup_interrupt)(virtio_queue queue,
129 		virtio_callback_func handler, void* cookie);
130 
131 	status_t (*queue_request)(virtio_queue queue,
132 		const physical_entry* readEntry,
133 		const physical_entry* writtenEntry, void* cookie);
134 
135 	status_t (*queue_request_v)(virtio_queue queue,
136 		const physical_entry* vector,
137 		size_t readVectorCount, size_t writtenVectorCount,
138 		void* cookie);
139 
140 	bool (*queue_is_full)(virtio_queue queue);
141 
142 	bool (*queue_is_empty)(virtio_queue queue);
143 
144 	uint16 (*queue_size)(virtio_queue queue);
145 
146 	bool (*queue_dequeue)(virtio_queue queue, void** _cookie,
147 		uint32* _usedLength);
148 
149 } virtio_device_interface;
150 
151 
152 #endif	/* _VIRTIO_H_ */
153