These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / SLOF / lib / libvirtio / virtio.c
1 /******************************************************************************
2  * Copyright (c) 2011 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stddef.h>
18 #include <cpu.h>
19 #include <cache.h>
20 #include <byteorder.h>
21 #include "virtio.h"
22 #include "helpers.h"
23
24 /* PCI virtio header offsets */
25 #define VIRTIOHDR_DEVICE_FEATURES       0
26 #define VIRTIOHDR_GUEST_FEATURES        4
27 #define VIRTIOHDR_QUEUE_ADDRESS         8
28 #define VIRTIOHDR_QUEUE_SIZE            12
29 #define VIRTIOHDR_QUEUE_SELECT          14
30 #define VIRTIOHDR_QUEUE_NOTIFY          16
31 #define VIRTIOHDR_DEVICE_STATUS         18
32 #define VIRTIOHDR_ISR_STATUS            19
33 #define VIRTIOHDR_DEVICE_CONFIG         20
34
35 /* PCI defines */
36 #define PCI_BASE_ADDR_SPACE_IO  0x01
37 #define PCI_BASE_ADDR_SPACE_64BIT 0x04
38 #define PCI_BASE_ADDR_MEM_MASK  (~0x0fUL)
39 #define PCI_BASE_ADDR_IO_MASK   (~0x03UL)
40
41 #define PCI_BASE_ADDR_REG_0     0x10
42 #define PCI_CONFIG_CAP_REG      0x34
43
44 #define PCI_CAP_ID_VNDR         0x9
45
46 /* Common configuration */
47 #define VIRTIO_PCI_CAP_COMMON_CFG       1
48 /* Notifications */
49 #define VIRTIO_PCI_CAP_NOTIFY_CFG       2
50 /* ISR access */
51 #define VIRTIO_PCI_CAP_ISR_CFG          3
52 /* Device specific configuration */
53 #define VIRTIO_PCI_CAP_DEVICE_CFG       4
54 /* PCI configuration access */
55 #define VIRTIO_PCI_CAP_PCI_CFG          5
56
57 #define VIRTIO_PCI_CAP_VNDR     0         /* Generic PCI field: PCI_CAP_ID_VNDR */
58 #define VIRTIO_PCI_CAP_NEXT     1         /* Generic PCI field: next ptr. */
59 #define VIRTIO_PCI_CAP_LEN      2         /* Generic PCI field: capability length */
60 #define VIRTIO_PCI_CAP_CFG_TYPE 3         /* Identifies the structure. */
61 #define VIRTIO_PCI_CAP_BAR      4         /* Where to find it. */
62 #define VIRTIO_PCI_CAP_OFFSET   8         /* Offset within bar. */
63 #define VIRTIO_PCI_CAP_LENGTH  12         /* Length of the structure, in bytes. */
64
65 struct virtio_dev_common {
66         le32 dev_features_sel;
67         le32 dev_features;
68         le32 drv_features_sel;
69         le32 drv_features;
70         le16 msix_config;
71         le16 num_queues;
72         uint8_t dev_status;
73         uint8_t cfg_generation;
74
75         le16 q_select;
76         le16 q_size;
77         le16 q_msix_vec;
78         le16 q_enable;
79         le16 q_notify_off;
80         le64 q_desc;
81         le64 q_avail;
82         le64 q_used;
83 } __attribute__ ((packed));
84
85 /* virtio 1.0 Spec: 4.1.3 PCI Device Layout
86  *
87  * Fields of different sizes are present in the device configuration regions.
88  * All 64-bit, 32-bit and 16-bit fields are little-endian. 64-bit fields are to
89  * be treated as two 32-bit fields, with low 32 bit part followed by the high 32
90  * bit part.
91  */
92 static void virtio_pci_write64(void *addr, uint64_t val)
93 {
94         uint32_t hi = (val >> 32) & 0xFFFFFFFF;
95         uint32_t lo = val & 0xFFFFFFFF;
96
97         ci_write_32(addr, cpu_to_le32(lo));
98         ci_write_32(addr + 4, cpu_to_le32(hi));
99 }
100
101 static uint64_t virtio_pci_read64(void *addr)
102 {
103         uint64_t hi, lo;
104
105         lo = le32_to_cpu(ci_read_32(addr));
106         hi = le32_to_cpu(ci_read_32(addr + 4));
107         return (hi << 32) | lo;
108 }
109
110 static void virtio_cap_set_base_addr(struct virtio_cap *cap, uint32_t offset)
111 {
112         uint64_t addr;
113
114         addr = SLOF_pci_config_read32(PCI_BASE_ADDR_REG_0 + 4 * cap->bar);
115         if (addr & PCI_BASE_ADDR_SPACE_IO) {
116                 addr = addr & PCI_BASE_ADDR_IO_MASK;
117                 cap->is_io = 1;
118         } else {
119                 if (addr & PCI_BASE_ADDR_SPACE_64BIT)
120                         addr |= SLOF_pci_config_read32(PCI_BASE_ADDR_REG_0 + 4 * (cap->bar + 1)) << 32;
121                 addr = addr & PCI_BASE_ADDR_MEM_MASK;
122                 cap->is_io = 0;
123         }
124         addr = (uint64_t)SLOF_translate_my_address((void *)addr);
125         cap->addr = (void *)addr + offset;
126 }
127
128 static void virtio_process_cap(struct virtio_device *dev, uint8_t cap_ptr)
129 {
130         struct virtio_cap *cap;
131         uint8_t cfg_type, bar;
132         uint32_t offset;
133
134         cfg_type = SLOF_pci_config_read8(cap_ptr + VIRTIO_PCI_CAP_CFG_TYPE);
135         bar = SLOF_pci_config_read8(cap_ptr + VIRTIO_PCI_CAP_BAR);
136         offset = SLOF_pci_config_read32(cap_ptr + VIRTIO_PCI_CAP_OFFSET);
137
138         switch(cfg_type) {
139         case VIRTIO_PCI_CAP_COMMON_CFG:
140                 cap = &dev->common;
141                 break;
142         case VIRTIO_PCI_CAP_NOTIFY_CFG:
143                 cap = &dev->notify;
144                 dev->notify_off_mul = SLOF_pci_config_read32(cap_ptr + sizeof(struct virtio_cap));
145                 break;
146         case VIRTIO_PCI_CAP_ISR_CFG:
147                 cap = &dev->isr;
148                 break;
149         case VIRTIO_PCI_CAP_DEVICE_CFG:
150                 cap = &dev->device;
151                 break;
152         default:
153                 return;
154         }
155
156         cap->bar = bar;
157         virtio_cap_set_base_addr(cap, offset);
158         cap->cap_id = cfg_type;
159 }
160
161 /**
162  * Reads the virtio device capabilities, gets called from SLOF routines The
163  * function determines legacy or modern device and sets up driver registers
164  */
165 struct virtio_device *virtio_setup_vd(void)
166 {
167         uint8_t cap_ptr, cap_vndr;
168         struct virtio_device *dev;
169
170         dev = SLOF_alloc_mem(sizeof(struct virtio_device));
171         if (!dev) {
172                 printf("Failed to allocate memory");
173                 return NULL;
174         }
175
176         cap_ptr = SLOF_pci_config_read8(PCI_CONFIG_CAP_REG);
177         while (cap_ptr != 0) {
178                 cap_vndr = SLOF_pci_config_read8(cap_ptr + VIRTIO_PCI_CAP_VNDR);
179                 if (cap_vndr == PCI_CAP_ID_VNDR)
180                         virtio_process_cap(dev, cap_ptr);
181                 cap_ptr = SLOF_pci_config_read8(cap_ptr+VIRTIO_PCI_CAP_NEXT);
182         }
183
184         if (dev->common.cap_id && dev->notify.cap_id &&
185             dev->isr.cap_id && dev->device.cap_id) {
186                 dev->is_modern = 1;
187         } else {
188                 dev->is_modern = 0;
189                 dev->legacy.cap_id = 0;
190                 dev->legacy.bar = 0;
191                 virtio_cap_set_base_addr(&dev->legacy, 0);
192         }
193         return dev;
194 }
195
196 /**
197  * Calculate ring size according to queue size number
198  */
199 unsigned long virtio_vring_size(unsigned int qsize)
200 {
201         return VQ_ALIGN(sizeof(struct vring_desc) * qsize +
202                         sizeof(struct vring_avail) + sizeof(uint16_t) * qsize) +
203                 VQ_ALIGN(sizeof(struct vring_used) +
204                          sizeof(struct vring_used_elem) * qsize);
205 }
206
207
208 /**
209  * Get number of elements in a vring
210  * @param   dev  pointer to virtio device information
211  * @param   queue virtio queue number
212  * @return  number of elements
213  */
214 unsigned int virtio_get_qsize(struct virtio_device *dev, int queue)
215 {
216         unsigned int size = 0;
217
218         if (dev->is_modern) {
219                 void *addr = dev->common.addr + offset_of(struct virtio_dev_common, q_select);
220                 ci_write_16(addr, cpu_to_le16(queue));
221                 eieio();
222                 addr = dev->common.addr + offset_of(struct virtio_dev_common, q_size);
223                 size = le16_to_cpu(ci_read_16(addr));
224         }
225         else {
226                 ci_write_16(dev->legacy.addr+VIRTIOHDR_QUEUE_SELECT,
227                             cpu_to_le16(queue));
228                 eieio();
229                 size = le16_to_cpu(ci_read_16(dev->legacy.addr+VIRTIOHDR_QUEUE_SIZE));
230         }
231
232         return size;
233 }
234
235
236 /**
237  * Get address of descriptor vring
238  * @param   dev  pointer to virtio device information
239  * @param   queue virtio queue number
240  * @return  pointer to the descriptor ring
241  */
242 struct vring_desc *virtio_get_vring_desc(struct virtio_device *dev, int queue)
243 {
244         struct vring_desc *desc = 0;
245
246         if (dev->is_modern) {
247                 void *q_sel = dev->common.addr + offset_of(struct virtio_dev_common, q_select);
248                 void *q_desc = dev->common.addr + offset_of(struct virtio_dev_common, q_desc);
249
250                 ci_write_16(q_sel, cpu_to_le16(queue));
251                 eieio();
252                 desc = (void *)(virtio_pci_read64(q_desc));
253         } else {
254                 ci_write_16(dev->legacy.addr+VIRTIOHDR_QUEUE_SELECT,
255                             cpu_to_le16(queue));
256                 eieio();
257                 desc = (void*)(4096L *
258                                le32_to_cpu(ci_read_32(dev->legacy.addr+VIRTIOHDR_QUEUE_ADDRESS)));
259         }
260
261         return desc;
262 }
263
264
265 /**
266  * Get address of "available" vring
267  * @param   dev  pointer to virtio device information
268  * @param   queue virtio queue number
269  * @return  pointer to the "available" ring
270  */
271 struct vring_avail *virtio_get_vring_avail(struct virtio_device *dev, int queue)
272 {
273         if (dev->is_modern) {
274                 void *q_sel = dev->common.addr + offset_of(struct virtio_dev_common, q_select);
275                 void *q_avail = dev->common.addr + offset_of(struct virtio_dev_common, q_avail);
276
277                 ci_write_16(q_sel, cpu_to_le16(queue));
278                 eieio();
279                 return (void *)(virtio_pci_read64(q_avail));
280         }
281         else {
282                 return (void*)((uint64_t)virtio_get_vring_desc(dev, queue) +
283                                virtio_get_qsize(dev, queue) * sizeof(struct vring_desc));
284         }
285 }
286
287
288 /**
289  * Get address of "used" vring
290  * @param   dev  pointer to virtio device information
291  * @param   queue virtio queue number
292  * @return  pointer to the "used" ring
293  */
294 struct vring_used *virtio_get_vring_used(struct virtio_device *dev, int queue)
295 {
296         if (dev->is_modern) {
297                 void *q_sel = dev->common.addr + offset_of(struct virtio_dev_common, q_select);
298                 void *q_used = dev->common.addr + offset_of(struct virtio_dev_common, q_used);
299
300                 ci_write_16(q_sel, cpu_to_le16(queue));
301                 eieio();
302                 return (void *)(virtio_pci_read64(q_used));
303         } else {
304                 return (void*)VQ_ALIGN((uint64_t)virtio_get_vring_avail(dev, queue)
305                                        + virtio_get_qsize(dev, queue)
306                                        * sizeof(struct vring_avail));
307         }
308 }
309
310 /**
311  * Fill the virtio ring descriptor depending on the legacy mode or virtio 1.0
312  */
313 void virtio_fill_desc(struct vring_desc *desc, bool is_modern,
314                       uint64_t addr, uint32_t len,
315                       uint16_t flags, uint16_t next)
316 {
317         if (is_modern) {
318                 desc->addr = cpu_to_le64(addr);
319                 desc->len = cpu_to_le32(len);
320                 desc->flags = cpu_to_le16(flags);
321                 desc->next = cpu_to_le16(next);
322         } else {
323                 desc->addr = addr;
324                 desc->len = len;
325                 desc->flags = flags;
326                 desc->next = next;
327         }
328 }
329
330 /**
331  * Reset virtio device
332  */
333 void virtio_reset_device(struct virtio_device *dev)
334 {
335         virtio_set_status(dev, 0);
336 }
337
338
339 /**
340  * Notify hypervisor about queue update
341  */
342 void virtio_queue_notify(struct virtio_device *dev, int queue)
343 {
344         if (dev->is_modern) {
345                 void *q_sel = dev->common.addr + offset_of(struct virtio_dev_common, q_select);
346                 void *q_ntfy = dev->common.addr + offset_of(struct virtio_dev_common, q_notify_off);
347                 void *addr;
348                 uint16_t q_notify_off;
349
350                 ci_write_16(q_sel, cpu_to_le16(queue));
351                 eieio();
352                 q_notify_off = le16_to_cpu(ci_read_16(q_ntfy));
353                 addr = dev->notify.addr + q_notify_off * dev->notify_off_mul;
354                 ci_write_16(addr, cpu_to_le16(queue));
355         } else {
356                 ci_write_16(dev->legacy.addr+VIRTIOHDR_QUEUE_NOTIFY, cpu_to_le16(queue));
357         }
358 }
359
360 /**
361  * Set queue address
362  */
363 void virtio_set_qaddr(struct virtio_device *dev, int queue, unsigned long qaddr)
364 {
365         if (dev->is_modern) {
366                 uint64_t q_desc = qaddr;
367                 uint64_t q_avail;
368                 uint64_t q_used;
369                 uint32_t q_size = virtio_get_qsize(dev, queue);
370
371                 virtio_pci_write64(dev->common.addr + offset_of(struct virtio_dev_common, q_desc), q_desc);
372                 q_avail = q_desc + q_size * sizeof(struct vring_desc);
373                 virtio_pci_write64(dev->common.addr + offset_of(struct virtio_dev_common, q_avail), q_avail);
374                 q_used = VQ_ALIGN(q_avail + sizeof(struct vring_avail) + sizeof(uint16_t) * q_size);
375                 virtio_pci_write64(dev->common.addr + offset_of(struct virtio_dev_common, q_used), q_used);
376                 ci_write_16(dev->common.addr + offset_of(struct virtio_dev_common, q_enable), cpu_to_le16(1));
377         } else {
378                 uint32_t val = qaddr;
379                 val = val >> 12;
380                 ci_write_16(dev->legacy.addr+VIRTIOHDR_QUEUE_SELECT,
381                             cpu_to_le16(queue));
382                 eieio();
383                 ci_write_32(dev->legacy.addr+VIRTIOHDR_QUEUE_ADDRESS,
384                             cpu_to_le32(val));
385         }
386 }
387
388 int virtio_queue_init_vq(struct virtio_device *dev, struct vqs *vq, unsigned int id)
389 {
390         vq->size = virtio_get_qsize(dev, id);
391         vq->desc = SLOF_alloc_mem_aligned(virtio_vring_size(vq->size), 4096);
392         if (!vq->desc) {
393                 printf("memory allocation failed!\n");
394                 return -1;
395         }
396         memset(vq->desc, 0, virtio_vring_size(vq->size));
397         virtio_set_qaddr(dev, id, (unsigned long)vq->desc);
398         vq->avail = virtio_get_vring_avail(dev, id);
399         vq->used = virtio_get_vring_used(dev, id);
400         vq->id = id;
401         return 0;
402 }
403
404 /**
405  * Set device status bits
406  */
407 void virtio_set_status(struct virtio_device *dev, int status)
408 {
409         if (dev->is_modern) {
410                 ci_write_8(dev->common.addr +
411                            offset_of(struct virtio_dev_common, dev_status), status);
412         } else {
413                 ci_write_8(dev->legacy.addr+VIRTIOHDR_DEVICE_STATUS, status);
414         }
415 }
416
417 /**
418  * Get device status bits
419  */
420 void virtio_get_status(struct virtio_device *dev, int *status)
421 {
422         if (dev->is_modern) {
423                 *status = ci_read_8(dev->common.addr +
424                                     offset_of(struct virtio_dev_common, dev_status));
425         } else {
426                 *status = ci_read_8(dev->legacy.addr+VIRTIOHDR_DEVICE_STATUS);
427         }
428 }
429
430 /**
431  * Set guest feature bits
432  */
433 void virtio_set_guest_features(struct virtio_device *dev, uint64_t features)
434
435 {
436         if (dev->is_modern) {
437                 uint32_t f1 = (features >> 32) & 0xFFFFFFFF;
438                 uint32_t f0 = features & 0xFFFFFFFF;
439                 void *addr = dev->common.addr;
440
441                 ci_write_32(addr + offset_of(struct virtio_dev_common, drv_features_sel),
442                             cpu_to_le32(1));
443                 ci_write_32(addr + offset_of(struct virtio_dev_common, drv_features),
444                             cpu_to_le32(f1));
445
446                 ci_write_32(addr + offset_of(struct virtio_dev_common, drv_features_sel),
447                             cpu_to_le32(0));
448                 ci_write_32(addr + offset_of(struct virtio_dev_common, drv_features),
449                             cpu_to_le32(f0));
450         } else {
451                 ci_write_32(dev->legacy.addr+VIRTIOHDR_GUEST_FEATURES, cpu_to_le32(features));
452         }
453 }
454
455 /**
456  * Get host feature bits
457  */
458 uint64_t virtio_get_host_features(struct virtio_device *dev)
459
460 {
461         uint64_t features = 0;
462         if (dev->is_modern) {
463                 uint32_t f0 = 0, f1 = 0;
464                 void *addr = dev->common.addr;
465
466                 ci_write_32(addr + offset_of(struct virtio_dev_common, dev_features_sel),
467                             cpu_to_le32(1));
468                 f1 = ci_read_32(addr +
469                                 offset_of(struct virtio_dev_common, dev_features));
470                 ci_write_32(addr + offset_of(struct virtio_dev_common, dev_features_sel),
471                             cpu_to_le32(0));
472                 f0 = ci_read_32(addr +
473                                 offset_of(struct virtio_dev_common, dev_features));
474
475                 features = ((uint64_t)le32_to_cpu(f1) << 32) | le32_to_cpu(f0);
476         } else {
477                 features = le32_to_cpu(ci_read_32(dev->legacy.addr+VIRTIOHDR_DEVICE_FEATURES));
478         }
479         return features;
480 }
481
482 int virtio_negotiate_guest_features(struct virtio_device *dev, uint64_t features)
483 {
484         uint64_t host_features = 0;
485         int status;
486
487         /* Negotiate features */
488         host_features = virtio_get_host_features(dev);
489         if (!(host_features & VIRTIO_F_VERSION_1)) {
490                 fprintf(stderr, "Device does not support virtio 1.0 %llx\n", host_features);
491                 return -1;
492         }
493
494         virtio_set_guest_features(dev,  features);
495         host_features = virtio_get_host_features(dev);
496         if ((host_features & features) != features) {
497                 fprintf(stderr, "Features error %llx\n", features);
498                 return -1;
499         }
500
501         virtio_get_status(dev, &status);
502         status |= VIRTIO_STAT_FEATURES_OK;
503         virtio_set_status(dev, status);
504
505         /* Read back to verify the FEATURES_OK bit */
506         virtio_get_status(dev, &status);
507         if ((status & VIRTIO_STAT_FEATURES_OK) != VIRTIO_STAT_FEATURES_OK)
508                 return -1;
509
510         return 0;
511 }
512
513 /**
514  * Get additional config values
515  */
516 uint64_t virtio_get_config(struct virtio_device *dev, int offset, int size)
517 {
518         uint64_t val = ~0ULL;
519         uint32_t hi, lo;
520         void *confbase;
521
522         if (dev->is_modern)
523                 confbase = dev->device.addr;
524         else
525                 confbase = dev->legacy.addr+VIRTIOHDR_DEVICE_CONFIG;
526
527         switch (size) {
528         case 1:
529                 val = ci_read_8(confbase+offset);
530                 break;
531         case 2:
532                 val = ci_read_16(confbase+offset);
533                 if (dev->is_modern)
534                         val = le16_to_cpu(val);
535                 break;
536         case 4:
537                 val = ci_read_32(confbase+offset);
538                 if (dev->is_modern)
539                         val = le32_to_cpu(val);
540                 break;
541         case 8:
542                 /* We don't support 8 bytes PIO accesses
543                  * in qemu and this is all PIO
544                  */
545                 lo = ci_read_32(confbase+offset);
546                 hi = ci_read_32(confbase+offset+4);
547                 if (dev->is_modern)
548                         val = (uint64_t)le32_to_cpu(hi) << 32 | le32_to_cpu(lo);
549                 else
550                         val = (uint64_t)hi << 32 | lo;
551                 break;
552         }
553
554         return val;
555 }
556
557 /**
558  * Get config blob
559  */
560 int __virtio_read_config(struct virtio_device *dev, void *dst,
561                          int offset, int len)
562 {
563         void *confbase;
564         unsigned char *buf = dst;
565         int i;
566
567         if (dev->is_modern)
568                 confbase = dev->device.addr;
569         else
570                 confbase = dev->legacy.addr+VIRTIOHDR_DEVICE_CONFIG;
571
572         for (i = 0; i < len; i++)
573                 buf[i] = ci_read_8(confbase + offset + i);
574
575         return len;
576 }