blob: 5ae6a76beaf09dab2bb62b3debc9ef4717456a84 [file] [log] [blame]
Gleb Natapov89acfa32010-05-10 11:36:37 +03001/* virtio-pci.c - pci interface for virtio interface
2 *
3 * (c) Copyright 2008 Bull S.A.S.
4 *
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
6 *
7 * some parts from Linux Virtio PCI driver
8 *
9 * Copyright IBM Corp. 2007
10 * Authors: Anthony Liguori <aliguori@us.ibm.com>
11 *
12 * Adopted for Seabios: Gleb Natapov <gleb@redhat.com>
13 *
14 * This work is licensed under the terms of the GNU LGPLv3
15 * See the COPYING file in the top-level directory.
16 */
17
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -040018#include "config.h" // CONFIG_DEBUG_LEVEL
Kevin O'Connor9dea5902013-09-14 20:23:54 -040019#include "malloc.h" // free
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "output.h" // dprintf
Sebastian Herbszt70451b62011-11-21 12:23:20 +010021#include "pci.h" // pci_config_readl
22#include "pci_regs.h" // PCI_BASE_ADDRESS_0
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040023#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040024#include "virtio-pci.h"
25#include "virtio-ring.h"
Gleb Natapov89acfa32010-05-10 11:36:37 +030026
Gerd Hoffmann46d17922015-06-25 12:28:46 +020027u64 vp_get_features(struct vp_device *vp)
28{
29 u32 f0, f1;
30
31 if (vp->use_modern) {
32 vp_write(&vp->common, virtio_pci_common_cfg, device_feature_select, 0);
33 f0 = vp_read(&vp->common, virtio_pci_common_cfg, device_feature);
34 vp_write(&vp->common, virtio_pci_common_cfg, device_feature_select, 1);
35 f1 = vp_read(&vp->common, virtio_pci_common_cfg, device_feature);
36 } else {
37 f0 = vp_read(&vp->legacy, virtio_pci_legacy, host_features);
38 f1 = 0;
39 }
40 return ((u64)f1 << 32) | f0;
41}
42
43void vp_set_features(struct vp_device *vp, u64 features)
44{
45 u32 f0, f1;
46
47 f0 = features;
48 f1 = features >> 32;
49
50 if (vp->use_modern) {
51 vp_write(&vp->common, virtio_pci_common_cfg, guest_feature_select, 0);
52 vp_write(&vp->common, virtio_pci_common_cfg, guest_feature, f0);
53 vp_write(&vp->common, virtio_pci_common_cfg, guest_feature_select, 1);
54 vp_write(&vp->common, virtio_pci_common_cfg, guest_feature, f1);
55 } else {
56 vp_write(&vp->legacy, virtio_pci_legacy, guest_features, f0);
57 }
58}
59
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +020060int vp_find_vq(struct vp_device *vp, int queue_index,
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +010061 struct vring_virtqueue **p_vq)
Gleb Natapov89acfa32010-05-10 11:36:37 +030062{
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +020063 int ioaddr = GET_LOWFLAT(vp->ioaddr);
Gleb Natapov89acfa32010-05-10 11:36:37 +030064 u16 num;
65
66 ASSERT32FLAT();
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +010067 struct vring_virtqueue *vq = *p_vq = memalign_low(PAGE_SIZE, sizeof(*vq));
68 if (!vq) {
69 warn_noalloc();
70 goto fail;
71 }
72 memset(vq, 0, sizeof(*vq));
73
Gleb Natapov89acfa32010-05-10 11:36:37 +030074 /* select the queue */
75
76 outw(queue_index, ioaddr + VIRTIO_PCI_QUEUE_SEL);
77
78 /* check if the queue is available */
79
80 num = inw(ioaddr + VIRTIO_PCI_QUEUE_NUM);
81 if (!num) {
82 dprintf(1, "ERROR: queue size is 0\n");
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +010083 goto fail;
Gleb Natapov89acfa32010-05-10 11:36:37 +030084 }
85
86 if (num > MAX_QUEUE_NUM) {
87 dprintf(1, "ERROR: queue size %d > %d\n", num, MAX_QUEUE_NUM);
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +010088 goto fail;
Gleb Natapov89acfa32010-05-10 11:36:37 +030089 }
90
91 /* check if the queue is already active */
92
93 if (inl(ioaddr + VIRTIO_PCI_QUEUE_PFN)) {
94 dprintf(1, "ERROR: queue already active\n");
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +010095 goto fail;
Gleb Natapov89acfa32010-05-10 11:36:37 +030096 }
97
98 vq->queue_index = queue_index;
99
100 /* initialize the queue */
101
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100102 struct vring * vr = &vq->vring;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300103 vring_init(vr, num, (unsigned char*)&vq->queue);
104
105 /* activate the queue
106 *
107 * NOTE: vr->desc is initialized by vring_init()
108 */
109
110 outl((unsigned long)virt_to_phys(vr->desc) >> PAGE_SHIFT,
111 ioaddr + VIRTIO_PCI_QUEUE_PFN);
112
113 return num;
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100114
115fail:
116 free(vq);
117 *p_vq = NULL;
118 return -1;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300119}
Paolo Bonzini4e343322011-11-16 13:02:56 +0100120
Gerd Hoffmann74d0cdc2015-06-25 10:24:27 +0200121void vp_init_simple(struct vp_device *vp, struct pci_device *pci)
Paolo Bonzini4e343322011-11-16 13:02:56 +0100122{
Gerd Hoffmannd8966462015-06-25 16:14:21 +0200123 u8 cap = pci_find_capability(pci, PCI_CAP_ID_VNDR, 0);
124 struct vp_cap *vp_cap;
125 u32 addr, offset;
126 u8 type;
127
128 memset(vp, 0, sizeof(*vp));
129 while (cap != 0) {
130 type = pci_config_readb(pci->bdf, cap +
131 offsetof(struct virtio_pci_cap, cfg_type));
132 switch (type) {
133 case VIRTIO_PCI_CAP_COMMON_CFG:
134 vp_cap = &vp->common;
135 break;
136 case VIRTIO_PCI_CAP_NOTIFY_CFG:
137 vp_cap = &vp->notify;
138 break;
139 case VIRTIO_PCI_CAP_ISR_CFG:
140 vp_cap = &vp->isr;
141 break;
142 case VIRTIO_PCI_CAP_DEVICE_CFG:
143 vp_cap = &vp->device;
144 break;
145 default:
146 vp_cap = NULL;
147 break;
148 }
149 if (vp_cap && !vp_cap->cap) {
150 vp_cap->cap = cap;
151 vp_cap->bar = pci_config_readb(pci->bdf, cap +
152 offsetof(struct virtio_pci_cap, bar));
153 offset = pci_config_readl(pci->bdf, cap +
154 offsetof(struct virtio_pci_cap, offset));
155 addr = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0 + 4 * vp_cap->bar);
156 if (addr & PCI_BASE_ADDRESS_SPACE_IO) {
157 vp_cap->is_io = 1;
158 addr &= PCI_BASE_ADDRESS_IO_MASK;
159 } else {
160 vp_cap->is_io = 0;
161 addr &= PCI_BASE_ADDRESS_MEM_MASK;
162 }
163 vp_cap->addr = addr + offset;
164 dprintf(3, "pci dev %x:%x virtio cap at 0x%x type %d "
165 "bar %d at 0x%08x off +0x%04x [%s]\n",
166 pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf),
167 vp_cap->cap, type, vp_cap->bar, addr, offset,
168 vp_cap->is_io ? "io" : "mmio");
169 }
170
171 cap = pci_find_capability(pci, PCI_CAP_ID_VNDR, cap);
172 }
173
174 if (vp->common.cap && vp->notify.cap && vp->isr.cap && vp->device.cap) {
175 dprintf(1, "pci dev %x:%x supports virtio 1.0\n",
176 pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf));
177 }
178
Gerd Hoffmann0de78912015-06-30 08:50:09 +0200179 vp->legacy.bar = 0;
180 vp->legacy.addr = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0) &
Paolo Bonzini4e343322011-11-16 13:02:56 +0100181 PCI_BASE_ADDRESS_IO_MASK;
Gerd Hoffmann0de78912015-06-30 08:50:09 +0200182 vp->legacy.is_io = 1;
183 vp->ioaddr = vp->legacy.addr; /* temporary */
Paolo Bonzini4e343322011-11-16 13:02:56 +0100184
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200185 vp_reset(vp);
Gerd Hoffmann74d0cdc2015-06-25 10:24:27 +0200186 pci_config_maskw(pci->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200187 vp_set_status(vp, VIRTIO_CONFIG_S_ACKNOWLEDGE |
Paolo Bonzini4e343322011-11-16 13:02:56 +0100188 VIRTIO_CONFIG_S_DRIVER );
Paolo Bonzini4e343322011-11-16 13:02:56 +0100189}