xref: /haiku/headers/private/virtio/virtio.h (revision cb29eafe2586fdb2d7685afa69fdab5d88a8b576)
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	((1 << 28) - 1)
30 
31 #define VIRTIO_FEATURE_NOTIFY_ON_EMPTY 		(1 << 24)
32 #define VIRTIO_FEATURE_RING_INDIRECT_DESC	(1 << 28)
33 #define VIRTIO_FEATURE_RING_EVENT_IDX		(1 << 29)
34 #define VIRTIO_FEATURE_BAD_FEATURE 			(1 << 30)
35 
36 #define VIRTIO_VIRTQUEUES_MAX_COUNT	8
37 
38 #define VIRTIO_CONFIG_STATUS_RESET	0x00
39 #define VIRTIO_CONFIG_STATUS_ACK	0x01
40 #define VIRTIO_CONFIG_STATUS_DRIVER	0x02
41 #define VIRTIO_CONFIG_STATUS_DRIVER_OK	0x04
42 #define VIRTIO_CONFIG_STATUS_FAILED	0x80
43 
44 // attributes:
45 
46 // node type
47 #define VIRTIO_BUS_TYPE_NAME "bus/virtio/v1"
48 // device type (uint16)
49 #define VIRTIO_DEVICE_TYPE_ITEM "virtio/type"
50 // alignment (uint16)
51 #define VIRTIO_VRING_ALIGNMENT_ITEM "virtio/vring_alignment"
52 
53 // sim cookie, issued by virtio bus manager
54 typedef void* virtio_sim;
55 // device cookie, issued by virtio bus manager
56 typedef void* virtio_device;
57 // queue cookie, issued by virtio bus manager
58 typedef void* virtio_queue;
59 // callback function for requests
60 typedef void (*virtio_callback_func)(void* driverCookie, void* cookie);
61 // callback function for interrupts
62 typedef void (*virtio_intr_func)(void* cookie);
63 
64 #define	VIRTIO_DEVICE_MODULE_NAME "bus_managers/virtio/device/v1"
65 
66 typedef struct {
67 	driver_module_info info;
68 
69 	status_t	(*queue_interrupt_handler)(virtio_sim sim, uint16 queue);
70 	status_t	(*config_interrupt_handler)(virtio_sim sim);
71 } virtio_for_controller_interface;
72 
73 #define VIRTIO_FOR_CONTROLLER_MODULE_NAME "bus_managers/virtio/controller/driver_v1"
74 
75 // Bus manager interface used by Virtio controller drivers.
76 typedef struct {
77 	driver_module_info info;
78 
79 	void (*set_sim)(void* cookie, virtio_sim sim);
80 	status_t (*read_host_features)(void* cookie, uint32* features);
81 	status_t (*write_guest_features)(void* cookie, uint32 features);
82 	uint8 (*get_status)(void* cookie);
83 	void (*set_status)(void* cookie, uint8 status);
84 	status_t (*read_device_config)(void* cookie, uint8 offset, void* buffer,
85 		size_t bufferSize);
86 	status_t (*write_device_config)(void* cookie, uint8 offset,
87 		const void* buffer, size_t bufferSize);
88 
89 	uint16	(*get_queue_ring_size)(void* cookie, uint16 queue);
90 	status_t (*setup_queue)(void* cookie, uint16 queue, phys_addr_t phy);
91 	status_t (*setup_interrupt)(void* cookie, uint16 queueCount);
92 	status_t (*free_interrupt)(void* cookie);
93 	void	(*notify_queue)(void* cookie, uint16 queue);
94 } virtio_sim_interface;
95 
96 
97 // bus manager device interface for peripheral driver
98 typedef struct {
99 	driver_module_info info;
100 
101 	status_t (*negotiate_features)(virtio_device cookie, uint32 supported,
102 		uint32* negotiated, const char* (*get_feature_name)(uint32));
103 
104 	status_t (*read_device_config)(virtio_device cookie, uint8 offset,
105 		void* buffer, size_t bufferSize);
106 	status_t (*write_device_config)(virtio_device cookie, uint8 offset,
107 		const void* buffer, size_t bufferSize);
108 
109 	status_t (*alloc_queues)(virtio_device cookie, size_t count,
110 		virtio_queue* queues);
111 
112 	void (*free_queues)(virtio_device cookie);
113 
114 	status_t (*setup_interrupt)(virtio_device cookie,
115 		virtio_intr_func config_handler, void* driverCookie);
116 
117 	status_t (*free_interrupts)(virtio_device cookie);
118 
119 	status_t (*queue_setup_interrupt)(virtio_queue queue,
120 		virtio_callback_func handler, void* cookie);
121 
122 	status_t (*queue_request)(virtio_queue queue,
123 		const physical_entry* readEntry,
124 		const physical_entry* writtenEntry, void* cookie);
125 
126 	status_t (*queue_request_v)(virtio_queue queue,
127 		const physical_entry* vector,
128 		size_t readVectorCount, size_t writtenVectorCount,
129 		void* cookie);
130 
131 	bool (*queue_is_full)(virtio_queue queue);
132 
133 	bool (*queue_is_empty)(virtio_queue queue);
134 
135 	uint16 (*queue_size)(virtio_queue queue);
136 
137 	bool (*queue_dequeue)(virtio_queue queue, void** _cookie,
138 		uint32* _usedLength);
139 
140 } virtio_device_interface;
141 
142 
143 #endif	/* _VIRTIO_H_ */
144