summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/lib/libvirtio/virtio.c
blob: f9c00a67a89aca1212d478af20880c0109f34530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/******************************************************************************
 * Copyright (c) 2011 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <cpu.h>
#include <cache.h>
#include <byteorder.h>
#include "virtio.h"

/* PCI virtio header offsets */
#define VIRTIOHDR_DEVICE_FEATURES	0
#define VIRTIOHDR_GUEST_FEATURES	4
#define VIRTIOHDR_QUEUE_ADDRESS 	8
#define VIRTIOHDR_QUEUE_SIZE		12
#define VIRTIOHDR_QUEUE_SELECT		14
#define VIRTIOHDR_QUEUE_NOTIFY		16
#define VIRTIOHDR_DEVICE_STATUS 	18
#define VIRTIOHDR_ISR_STATUS		19
#define VIRTIOHDR_DEVICE_CONFIG 	20


/**
 * Calculate ring size according to queue size number
 */
unsigned long virtio_vring_size(unsigned int qsize)
{
	return VQ_ALIGN(sizeof(struct vring_desc) * qsize +
                        sizeof(struct vring_avail) + sizeof(uint16_t) * qsize) +
               VQ_ALIGN(sizeof(struct vring_used) +
                        sizeof(struct vring_used_elem) * qsize);
}


/**
 * Get number of elements in a vring
 * @param   dev  pointer to virtio device information
 * @param   queue virtio queue number
 * @return  number of elements
 */
int virtio_get_qsize(struct virtio_device *dev, int queue)
{
	int size = 0;

	if (dev->type == VIRTIO_TYPE_PCI) {
		ci_write_16(dev->base+VIRTIOHDR_QUEUE_SELECT,
			    cpu_to_le16(queue));
		eieio();
		size = le16_to_cpu(ci_read_16(dev->base+VIRTIOHDR_QUEUE_SIZE));
	}

	return size;
}


/**
 * Get address of descriptor vring
 * @param   dev  pointer to virtio device information
 * @param   queue virtio queue number
 * @return  pointer to the descriptor ring
 */
struct vring_desc *virtio_get_vring_desc(struct virtio_device *dev, int queue)
{
	struct vring_desc *desc = 0;

	if (dev->type == VIRTIO_TYPE_PCI) {
		ci_write_16(dev->base+VIRTIOHDR_QUEUE_SELECT,
			    cpu_to_le16(queue));
		eieio();
		desc = (void*)(4096L *
		   le32_to_cpu(ci_read_32(dev->base+VIRTIOHDR_QUEUE_ADDRESS)));
	}

	return desc;
}


/**
 * Get address of "available" vring
 * @param   dev  pointer to virtio device information
 * @param   queue virtio queue number
 * @return  pointer to the "available" ring
 */
struct vring_avail *virtio_get_vring_avail(struct virtio_device *dev, int queue)
{
	return (void*)((uint64_t)virtio_get_vring_desc(dev, queue)
			+ virtio_get_qsize(dev, queue) * sizeof(struct vring_desc));
}


/**
 * Get address of "used" vring
 * @param   dev  pointer to virtio device information
 * @param   queue virtio queue number
 * @return  pointer to the "used" ring
 */
struct vring_used *virtio_get_vring_used(struct virtio_device *dev, int queue)
{
	return (void*)VQ_ALIGN((uint64_t)virtio_get_vring_avail(dev, queue)
				  + virtio_get_qsize(dev, queue)
				    * sizeof(struct vring_avail));
}


/**
 * Reset virtio device
 */
void virtio_reset_device(struct virtio_device *dev)
{
	if (dev->type == VIRTIO_TYPE_PCI) {
		ci_write_8(dev->base+VIRTIOHDR_DEVICE_STATUS, 0);
	}
}


/**
 * Notify hypervisor about queue update
 */
void virtio_queue_notify(struct virtio_device *dev, int queue)
{
	if (dev->type == VIRTIO_TYPE_PCI) {
		ci_write_16(dev->base+VIRTIOHDR_QUEUE_NOTIFY, cpu_to_le16(queue));
	}
}

/**
 * Set queue address
 */
void virtio_set_qaddr(struct virtio_device *dev, int queue, unsigned int qaddr)
{
        if (dev->type == VIRTIO_TYPE_PCI) {
                uint32_t val = qaddr;
                val = val >> 12;
                ci_write_16(dev->base+VIRTIOHDR_QUEUE_SELECT,
                            cpu_to_le16(queue));
                eieio();
                ci_write_32(dev->base+VIRTIOHDR_QUEUE_ADDRESS,
                            cpu_to_le32(val));
        }
}

/**
 * Set device status bits
 */
void virtio_set_status(struct virtio_device *dev, int status)
{
	if (dev->type == VIRTIO_TYPE_PCI) {
		ci_write_8(dev->base+VIRTIOHDR_DEVICE_STATUS, status);
	}
}


/**
 * Set guest feature bits
 */
void virtio_set_guest_features(struct virtio_device *dev, int features)

{
	if (dev->type == VIRTIO_TYPE_PCI) {
		ci_write_32(dev->base+VIRTIOHDR_GUEST_FEATURES, bswap_32(features));
	}
}

/**
 * Get host feature bits
 */
void virtio_get_host_features(struct virtio_device *dev, int *features)

{
	if (dev->type == VIRTIO_TYPE_PCI && features) {
		*features = bswap_32(ci_read_32(dev->base+VIRTIOHDR_DEVICE_FEATURES));
	}
}


/**
 * Get additional config values
 */
uint64_t virtio_get_config(struct virtio_device *dev, int offset, int size)
{
	uint64_t val = ~0ULL;
	void *confbase;

	switch (dev->type) {
	 case VIRTIO_TYPE_PCI:
		confbase = dev->base+VIRTIOHDR_DEVICE_CONFIG;
		break;
	 default:
		return ~0ULL;
	}
	switch (size) {
	 case 1:
		val = ci_read_8(confbase+offset);
		break;
	 case 2:
		val = ci_read_16(confbase+offset);
		break;
	 case 4:
		val = ci_read_32(confbase+offset);
		break;
	 case 8:
		/* We don't support 8 bytes PIO accesses
		 * in qemu and this is all PIO
		 */
		val = ci_read_32(confbase+offset);
		val <<= 32;
		val |= ci_read_32(confbase+offset+4);
		break;
	}

	return val;
}

/**
 * Get config blob
 */
int __virtio_read_config(struct virtio_device *dev, void *dst,
			  int offset, int len)
{
	void *confbase;
	unsigned char *buf = dst;
	int i;

	switch (dev->type) {
	 case VIRTIO_TYPE_PCI:
		confbase = dev->base+VIRTIOHDR_DEVICE_CONFIG;
		break;
	 default:
		return 0;
	}
	for (i = 0; i < len; i++)
		buf[i] = ci_read_8(confbase + offset + i);
	return len;
}