1*550435edSBin Meng // SPDX-License-Identifier: GPL-2.0+
2*550435edSBin Meng /*
3*550435edSBin Meng * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4*550435edSBin Meng *
5*550435edSBin Meng * VirtIO PCI bus transport driver
6*550435edSBin Meng * Ported from Linux drivers/virtio/virtio_pci*.c
7*550435edSBin Meng */
8*550435edSBin Meng
9*550435edSBin Meng #include <common.h>
10*550435edSBin Meng #include <dm.h>
11*550435edSBin Meng #include <virtio_types.h>
12*550435edSBin Meng #include <virtio.h>
13*550435edSBin Meng #include <virtio_ring.h>
14*550435edSBin Meng #include <dm/device.h>
15*550435edSBin Meng #include <linux/compat.h>
16*550435edSBin Meng #include <linux/io.h>
17*550435edSBin Meng #include "virtio_pci.h"
18*550435edSBin Meng
19*550435edSBin Meng #define VIRTIO_PCI_DRV_NAME "virtio-pci.m"
20*550435edSBin Meng
21*550435edSBin Meng /* PCI device ID in the range 0x1040 to 0x107f */
22*550435edSBin Meng #define VIRTIO_PCI_VENDOR_ID 0x1af4
23*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID00 0x1040
24*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID01 0x1041
25*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID02 0x1042
26*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID03 0x1043
27*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID04 0x1044
28*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID05 0x1045
29*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID06 0x1046
30*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID07 0x1047
31*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID08 0x1048
32*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID09 0x1049
33*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID0A 0x104a
34*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID0B 0x104b
35*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID0C 0x104c
36*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID0D 0x104d
37*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID0E 0x104e
38*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID0F 0x104f
39*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID10 0x1050
40*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID11 0x1051
41*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID12 0x1052
42*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID13 0x1053
43*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID14 0x1054
44*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID15 0x1055
45*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID16 0x1056
46*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID17 0x1057
47*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID18 0x1058
48*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID19 0x1059
49*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID1A 0x105a
50*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID1B 0x105b
51*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID1C 0x105c
52*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID1D 0x105d
53*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID1E 0x105e
54*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID1F 0x105f
55*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID20 0x1060
56*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID21 0x1061
57*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID22 0x1062
58*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID23 0x1063
59*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID24 0x1064
60*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID25 0x1065
61*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID26 0x1066
62*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID27 0x1067
63*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID28 0x1068
64*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID29 0x1069
65*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID2A 0x106a
66*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID2B 0x106b
67*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID2C 0x106c
68*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID2D 0x106d
69*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID2E 0x106e
70*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID2F 0x106f
71*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID30 0x1070
72*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID31 0x1071
73*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID32 0x1072
74*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID33 0x1073
75*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID34 0x1074
76*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID35 0x1075
77*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID36 0x1076
78*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID37 0x1077
79*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID38 0x1078
80*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID39 0x1079
81*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID3A 0x107a
82*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID3B 0x107b
83*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID3C 0x107c
84*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID3D 0x107d
85*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID3E 0x107e
86*550435edSBin Meng #define VIRTIO_PCI_DEVICE_ID3F 0x107f
87*550435edSBin Meng
88*550435edSBin Meng /**
89*550435edSBin Meng * virtio pci transport driver private data
90*550435edSBin Meng *
91*550435edSBin Meng * @common: pci transport device common register block base
92*550435edSBin Meng * @notify_base: pci transport device notify register block base
93*550435edSBin Meng * @device: pci transport device device-specific register block base
94*550435edSBin Meng * @device_len: pci transport device device-specific register block length
95*550435edSBin Meng * @notify_offset_multiplier: multiply queue_notify_off by this value
96*550435edSBin Meng */
97*550435edSBin Meng struct virtio_pci_priv {
98*550435edSBin Meng struct virtio_pci_common_cfg __iomem *common;
99*550435edSBin Meng void __iomem *notify_base;
100*550435edSBin Meng void __iomem *device;
101*550435edSBin Meng u32 device_len;
102*550435edSBin Meng u32 notify_offset_multiplier;
103*550435edSBin Meng };
104*550435edSBin Meng
virtio_pci_get_config(struct udevice * udev,unsigned int offset,void * buf,unsigned int len)105*550435edSBin Meng static int virtio_pci_get_config(struct udevice *udev, unsigned int offset,
106*550435edSBin Meng void *buf, unsigned int len)
107*550435edSBin Meng {
108*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
109*550435edSBin Meng u8 b;
110*550435edSBin Meng __le16 w;
111*550435edSBin Meng __le32 l;
112*550435edSBin Meng
113*550435edSBin Meng WARN_ON(offset + len > priv->device_len);
114*550435edSBin Meng
115*550435edSBin Meng switch (len) {
116*550435edSBin Meng case 1:
117*550435edSBin Meng b = ioread8(priv->device + offset);
118*550435edSBin Meng memcpy(buf, &b, sizeof(b));
119*550435edSBin Meng break;
120*550435edSBin Meng case 2:
121*550435edSBin Meng w = cpu_to_le16(ioread16(priv->device + offset));
122*550435edSBin Meng memcpy(buf, &w, sizeof(w));
123*550435edSBin Meng break;
124*550435edSBin Meng case 4:
125*550435edSBin Meng l = cpu_to_le32(ioread32(priv->device + offset));
126*550435edSBin Meng memcpy(buf, &l, sizeof(l));
127*550435edSBin Meng break;
128*550435edSBin Meng case 8:
129*550435edSBin Meng l = cpu_to_le32(ioread32(priv->device + offset));
130*550435edSBin Meng memcpy(buf, &l, sizeof(l));
131*550435edSBin Meng l = cpu_to_le32(ioread32(priv->device + offset + sizeof(l)));
132*550435edSBin Meng memcpy(buf + sizeof(l), &l, sizeof(l));
133*550435edSBin Meng break;
134*550435edSBin Meng default:
135*550435edSBin Meng WARN_ON(true);
136*550435edSBin Meng }
137*550435edSBin Meng
138*550435edSBin Meng return 0;
139*550435edSBin Meng }
140*550435edSBin Meng
virtio_pci_set_config(struct udevice * udev,unsigned int offset,const void * buf,unsigned int len)141*550435edSBin Meng static int virtio_pci_set_config(struct udevice *udev, unsigned int offset,
142*550435edSBin Meng const void *buf, unsigned int len)
143*550435edSBin Meng {
144*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
145*550435edSBin Meng u8 b;
146*550435edSBin Meng __le16 w;
147*550435edSBin Meng __le32 l;
148*550435edSBin Meng
149*550435edSBin Meng WARN_ON(offset + len > priv->device_len);
150*550435edSBin Meng
151*550435edSBin Meng switch (len) {
152*550435edSBin Meng case 1:
153*550435edSBin Meng memcpy(&b, buf, sizeof(b));
154*550435edSBin Meng iowrite8(b, priv->device + offset);
155*550435edSBin Meng break;
156*550435edSBin Meng case 2:
157*550435edSBin Meng memcpy(&w, buf, sizeof(w));
158*550435edSBin Meng iowrite16(le16_to_cpu(w), priv->device + offset);
159*550435edSBin Meng break;
160*550435edSBin Meng case 4:
161*550435edSBin Meng memcpy(&l, buf, sizeof(l));
162*550435edSBin Meng iowrite32(le32_to_cpu(l), priv->device + offset);
163*550435edSBin Meng break;
164*550435edSBin Meng case 8:
165*550435edSBin Meng memcpy(&l, buf, sizeof(l));
166*550435edSBin Meng iowrite32(le32_to_cpu(l), priv->device + offset);
167*550435edSBin Meng memcpy(&l, buf + sizeof(l), sizeof(l));
168*550435edSBin Meng iowrite32(le32_to_cpu(l), priv->device + offset + sizeof(l));
169*550435edSBin Meng break;
170*550435edSBin Meng default:
171*550435edSBin Meng WARN_ON(true);
172*550435edSBin Meng }
173*550435edSBin Meng
174*550435edSBin Meng return 0;
175*550435edSBin Meng }
176*550435edSBin Meng
virtio_pci_generation(struct udevice * udev,u32 * counter)177*550435edSBin Meng static int virtio_pci_generation(struct udevice *udev, u32 *counter)
178*550435edSBin Meng {
179*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
180*550435edSBin Meng
181*550435edSBin Meng *counter = ioread8(&priv->common->config_generation);
182*550435edSBin Meng
183*550435edSBin Meng return 0;
184*550435edSBin Meng }
185*550435edSBin Meng
virtio_pci_get_status(struct udevice * udev,u8 * status)186*550435edSBin Meng static int virtio_pci_get_status(struct udevice *udev, u8 *status)
187*550435edSBin Meng {
188*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
189*550435edSBin Meng
190*550435edSBin Meng *status = ioread8(&priv->common->device_status);
191*550435edSBin Meng
192*550435edSBin Meng return 0;
193*550435edSBin Meng }
194*550435edSBin Meng
virtio_pci_set_status(struct udevice * udev,u8 status)195*550435edSBin Meng static int virtio_pci_set_status(struct udevice *udev, u8 status)
196*550435edSBin Meng {
197*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
198*550435edSBin Meng
199*550435edSBin Meng /* We should never be setting status to 0 */
200*550435edSBin Meng WARN_ON(status == 0);
201*550435edSBin Meng
202*550435edSBin Meng iowrite8(status, &priv->common->device_status);
203*550435edSBin Meng
204*550435edSBin Meng return 0;
205*550435edSBin Meng }
206*550435edSBin Meng
virtio_pci_reset(struct udevice * udev)207*550435edSBin Meng static int virtio_pci_reset(struct udevice *udev)
208*550435edSBin Meng {
209*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
210*550435edSBin Meng
211*550435edSBin Meng /* 0 status means a reset */
212*550435edSBin Meng iowrite8(0, &priv->common->device_status);
213*550435edSBin Meng
214*550435edSBin Meng /*
215*550435edSBin Meng * After writing 0 to device_status, the driver MUST wait for a read
216*550435edSBin Meng * of device_status to return 0 before reinitializing the device.
217*550435edSBin Meng * This will flush out the status write, and flush in device writes,
218*550435edSBin Meng * including MSI-X interrupts, if any.
219*550435edSBin Meng */
220*550435edSBin Meng while (ioread8(&priv->common->device_status))
221*550435edSBin Meng udelay(1000);
222*550435edSBin Meng
223*550435edSBin Meng return 0;
224*550435edSBin Meng }
225*550435edSBin Meng
virtio_pci_get_features(struct udevice * udev,u64 * features)226*550435edSBin Meng static int virtio_pci_get_features(struct udevice *udev, u64 *features)
227*550435edSBin Meng {
228*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
229*550435edSBin Meng
230*550435edSBin Meng iowrite32(0, &priv->common->device_feature_select);
231*550435edSBin Meng *features = ioread32(&priv->common->device_feature);
232*550435edSBin Meng iowrite32(1, &priv->common->device_feature_select);
233*550435edSBin Meng *features |= ((u64)ioread32(&priv->common->device_feature) << 32);
234*550435edSBin Meng
235*550435edSBin Meng return 0;
236*550435edSBin Meng }
237*550435edSBin Meng
virtio_pci_set_features(struct udevice * udev)238*550435edSBin Meng static int virtio_pci_set_features(struct udevice *udev)
239*550435edSBin Meng {
240*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
241*550435edSBin Meng struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
242*550435edSBin Meng
243*550435edSBin Meng if (!__virtio_test_bit(udev, VIRTIO_F_VERSION_1)) {
244*550435edSBin Meng debug("virtio: device uses modern interface but does not have VIRTIO_F_VERSION_1\n");
245*550435edSBin Meng return -EINVAL;
246*550435edSBin Meng }
247*550435edSBin Meng
248*550435edSBin Meng iowrite32(0, &priv->common->guest_feature_select);
249*550435edSBin Meng iowrite32((u32)uc_priv->features, &priv->common->guest_feature);
250*550435edSBin Meng iowrite32(1, &priv->common->guest_feature_select);
251*550435edSBin Meng iowrite32(uc_priv->features >> 32, &priv->common->guest_feature);
252*550435edSBin Meng
253*550435edSBin Meng return 0;
254*550435edSBin Meng }
255*550435edSBin Meng
virtio_pci_setup_vq(struct udevice * udev,unsigned int index)256*550435edSBin Meng static struct virtqueue *virtio_pci_setup_vq(struct udevice *udev,
257*550435edSBin Meng unsigned int index)
258*550435edSBin Meng {
259*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
260*550435edSBin Meng struct virtio_pci_common_cfg __iomem *cfg = priv->common;
261*550435edSBin Meng struct virtqueue *vq;
262*550435edSBin Meng u16 num;
263*550435edSBin Meng u64 addr;
264*550435edSBin Meng int err;
265*550435edSBin Meng
266*550435edSBin Meng if (index >= ioread16(&cfg->num_queues))
267*550435edSBin Meng return ERR_PTR(-ENOENT);
268*550435edSBin Meng
269*550435edSBin Meng /* Select the queue we're interested in */
270*550435edSBin Meng iowrite16(index, &cfg->queue_select);
271*550435edSBin Meng
272*550435edSBin Meng /* Check if queue is either not available or already active */
273*550435edSBin Meng num = ioread16(&cfg->queue_size);
274*550435edSBin Meng if (!num || ioread16(&cfg->queue_enable))
275*550435edSBin Meng return ERR_PTR(-ENOENT);
276*550435edSBin Meng
277*550435edSBin Meng if (num & (num - 1)) {
278*550435edSBin Meng printf("(%s): bad queue size %u", udev->name, num);
279*550435edSBin Meng return ERR_PTR(-EINVAL);
280*550435edSBin Meng }
281*550435edSBin Meng
282*550435edSBin Meng /* Create the vring */
283*550435edSBin Meng vq = vring_create_virtqueue(index, num, VIRTIO_PCI_VRING_ALIGN, udev);
284*550435edSBin Meng if (!vq) {
285*550435edSBin Meng err = -ENOMEM;
286*550435edSBin Meng goto error_available;
287*550435edSBin Meng }
288*550435edSBin Meng
289*550435edSBin Meng /* Activate the queue */
290*550435edSBin Meng iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size);
291*550435edSBin Meng
292*550435edSBin Meng addr = virtqueue_get_desc_addr(vq);
293*550435edSBin Meng iowrite32((u32)addr, &cfg->queue_desc_lo);
294*550435edSBin Meng iowrite32(addr >> 32, &cfg->queue_desc_hi);
295*550435edSBin Meng
296*550435edSBin Meng addr = virtqueue_get_avail_addr(vq);
297*550435edSBin Meng iowrite32((u32)addr, &cfg->queue_avail_lo);
298*550435edSBin Meng iowrite32(addr >> 32, &cfg->queue_avail_hi);
299*550435edSBin Meng
300*550435edSBin Meng addr = virtqueue_get_used_addr(vq);
301*550435edSBin Meng iowrite32((u32)addr, &cfg->queue_used_lo);
302*550435edSBin Meng iowrite32(addr >> 32, &cfg->queue_used_hi);
303*550435edSBin Meng
304*550435edSBin Meng iowrite16(1, &cfg->queue_enable);
305*550435edSBin Meng
306*550435edSBin Meng return vq;
307*550435edSBin Meng
308*550435edSBin Meng error_available:
309*550435edSBin Meng return ERR_PTR(err);
310*550435edSBin Meng }
311*550435edSBin Meng
virtio_pci_del_vq(struct virtqueue * vq)312*550435edSBin Meng static void virtio_pci_del_vq(struct virtqueue *vq)
313*550435edSBin Meng {
314*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(vq->vdev);
315*550435edSBin Meng unsigned int index = vq->index;
316*550435edSBin Meng
317*550435edSBin Meng iowrite16(index, &priv->common->queue_select);
318*550435edSBin Meng
319*550435edSBin Meng /* Select and deactivate the queue */
320*550435edSBin Meng iowrite16(0, &priv->common->queue_enable);
321*550435edSBin Meng
322*550435edSBin Meng vring_del_virtqueue(vq);
323*550435edSBin Meng }
324*550435edSBin Meng
virtio_pci_del_vqs(struct udevice * udev)325*550435edSBin Meng static int virtio_pci_del_vqs(struct udevice *udev)
326*550435edSBin Meng {
327*550435edSBin Meng struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
328*550435edSBin Meng struct virtqueue *vq, *n;
329*550435edSBin Meng
330*550435edSBin Meng list_for_each_entry_safe(vq, n, &uc_priv->vqs, list)
331*550435edSBin Meng virtio_pci_del_vq(vq);
332*550435edSBin Meng
333*550435edSBin Meng return 0;
334*550435edSBin Meng }
335*550435edSBin Meng
virtio_pci_find_vqs(struct udevice * udev,unsigned int nvqs,struct virtqueue * vqs[])336*550435edSBin Meng static int virtio_pci_find_vqs(struct udevice *udev, unsigned int nvqs,
337*550435edSBin Meng struct virtqueue *vqs[])
338*550435edSBin Meng {
339*550435edSBin Meng int i;
340*550435edSBin Meng
341*550435edSBin Meng for (i = 0; i < nvqs; ++i) {
342*550435edSBin Meng vqs[i] = virtio_pci_setup_vq(udev, i);
343*550435edSBin Meng if (IS_ERR(vqs[i])) {
344*550435edSBin Meng virtio_pci_del_vqs(udev);
345*550435edSBin Meng return PTR_ERR(vqs[i]);
346*550435edSBin Meng }
347*550435edSBin Meng }
348*550435edSBin Meng
349*550435edSBin Meng return 0;
350*550435edSBin Meng }
351*550435edSBin Meng
virtio_pci_notify(struct udevice * udev,struct virtqueue * vq)352*550435edSBin Meng static int virtio_pci_notify(struct udevice *udev, struct virtqueue *vq)
353*550435edSBin Meng {
354*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
355*550435edSBin Meng u16 off;
356*550435edSBin Meng
357*550435edSBin Meng /* Select the queue we're interested in */
358*550435edSBin Meng iowrite16(vq->index, &priv->common->queue_select);
359*550435edSBin Meng
360*550435edSBin Meng /* get offset of notification word for this vq */
361*550435edSBin Meng off = ioread16(&priv->common->queue_notify_off);
362*550435edSBin Meng
363*550435edSBin Meng /*
364*550435edSBin Meng * We write the queue's selector into the notification register
365*550435edSBin Meng * to signal the other end
366*550435edSBin Meng */
367*550435edSBin Meng iowrite16(vq->index,
368*550435edSBin Meng priv->notify_base + off * priv->notify_offset_multiplier);
369*550435edSBin Meng
370*550435edSBin Meng return 0;
371*550435edSBin Meng }
372*550435edSBin Meng
373*550435edSBin Meng /**
374*550435edSBin Meng * virtio_pci_find_capability - walk capabilities to find device info
375*550435edSBin Meng *
376*550435edSBin Meng * @udev: the transport device
377*550435edSBin Meng * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
378*550435edSBin Meng *
379*550435edSBin Meng * @return offset of the configuration structure
380*550435edSBin Meng */
virtio_pci_find_capability(struct udevice * udev,u8 cfg_type)381*550435edSBin Meng static int virtio_pci_find_capability(struct udevice *udev, u8 cfg_type)
382*550435edSBin Meng {
383*550435edSBin Meng int pos;
384*550435edSBin Meng int offset;
385*550435edSBin Meng u8 type, bar;
386*550435edSBin Meng
387*550435edSBin Meng for (pos = dm_pci_find_capability(udev, PCI_CAP_ID_VNDR);
388*550435edSBin Meng pos > 0;
389*550435edSBin Meng pos = dm_pci_find_next_capability(udev, pos, PCI_CAP_ID_VNDR)) {
390*550435edSBin Meng offset = pos + offsetof(struct virtio_pci_cap, cfg_type);
391*550435edSBin Meng dm_pci_read_config8(udev, offset, &type);
392*550435edSBin Meng offset = pos + offsetof(struct virtio_pci_cap, bar);
393*550435edSBin Meng dm_pci_read_config8(udev, offset, &bar);
394*550435edSBin Meng
395*550435edSBin Meng /* Ignore structures with reserved BAR values */
396*550435edSBin Meng if (bar > 0x5)
397*550435edSBin Meng continue;
398*550435edSBin Meng
399*550435edSBin Meng if (type == cfg_type)
400*550435edSBin Meng return pos;
401*550435edSBin Meng }
402*550435edSBin Meng
403*550435edSBin Meng return 0;
404*550435edSBin Meng }
405*550435edSBin Meng
406*550435edSBin Meng /**
407*550435edSBin Meng * virtio_pci_map_capability - map base address of the capability
408*550435edSBin Meng *
409*550435edSBin Meng * @udev: the transport device
410*550435edSBin Meng * @off: offset of the configuration structure
411*550435edSBin Meng *
412*550435edSBin Meng * @return base address of the capability
413*550435edSBin Meng */
virtio_pci_map_capability(struct udevice * udev,int off)414*550435edSBin Meng static void __iomem *virtio_pci_map_capability(struct udevice *udev, int off)
415*550435edSBin Meng {
416*550435edSBin Meng u8 bar;
417*550435edSBin Meng u32 offset;
418*550435edSBin Meng ulong base;
419*550435edSBin Meng void __iomem *p;
420*550435edSBin Meng
421*550435edSBin Meng if (!off)
422*550435edSBin Meng return NULL;
423*550435edSBin Meng
424*550435edSBin Meng offset = off + offsetof(struct virtio_pci_cap, bar);
425*550435edSBin Meng dm_pci_read_config8(udev, offset, &bar);
426*550435edSBin Meng offset = off + offsetof(struct virtio_pci_cap, offset);
427*550435edSBin Meng dm_pci_read_config32(udev, offset, &offset);
428*550435edSBin Meng
429*550435edSBin Meng /*
430*550435edSBin Meng * TODO: adding 64-bit BAR support
431*550435edSBin Meng *
432*550435edSBin Meng * Per spec, the BAR is permitted to be either 32-bit or 64-bit.
433*550435edSBin Meng * For simplicity, only read the BAR address as 32-bit.
434*550435edSBin Meng */
435*550435edSBin Meng base = dm_pci_read_bar32(udev, bar);
436*550435edSBin Meng p = (void __iomem *)base + offset;
437*550435edSBin Meng
438*550435edSBin Meng return p;
439*550435edSBin Meng }
440*550435edSBin Meng
virtio_pci_bind(struct udevice * udev)441*550435edSBin Meng static int virtio_pci_bind(struct udevice *udev)
442*550435edSBin Meng {
443*550435edSBin Meng static int num_devs;
444*550435edSBin Meng char name[20];
445*550435edSBin Meng
446*550435edSBin Meng /* Create a unique device name */
447*550435edSBin Meng sprintf(name, "%s#%u", VIRTIO_PCI_DRV_NAME, num_devs++);
448*550435edSBin Meng device_set_name(udev, name);
449*550435edSBin Meng
450*550435edSBin Meng return 0;
451*550435edSBin Meng }
452*550435edSBin Meng
virtio_pci_probe(struct udevice * udev)453*550435edSBin Meng static int virtio_pci_probe(struct udevice *udev)
454*550435edSBin Meng {
455*550435edSBin Meng struct pci_child_platdata *pplat = dev_get_parent_platdata(udev);
456*550435edSBin Meng struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
457*550435edSBin Meng struct virtio_pci_priv *priv = dev_get_priv(udev);
458*550435edSBin Meng u16 subvendor;
459*550435edSBin Meng u8 revision;
460*550435edSBin Meng int common, notify, device;
461*550435edSBin Meng int offset;
462*550435edSBin Meng
463*550435edSBin Meng /* We only own devices >= 0x1040 and <= 0x107f: leave the rest. */
464*550435edSBin Meng if (pplat->device < 0x1040 || pplat->device > 0x107f)
465*550435edSBin Meng return -ENODEV;
466*550435edSBin Meng
467*550435edSBin Meng /* Transitional devices must not have a PCI revision ID of 0 */
468*550435edSBin Meng dm_pci_read_config8(udev, PCI_REVISION_ID, &revision);
469*550435edSBin Meng
470*550435edSBin Meng /* Modern devices: simply use PCI device id, but start from 0x1040. */
471*550435edSBin Meng uc_priv->device = pplat->device - 0x1040;
472*550435edSBin Meng dm_pci_read_config16(udev, PCI_SUBSYSTEM_VENDOR_ID, &subvendor);
473*550435edSBin Meng uc_priv->vendor = subvendor;
474*550435edSBin Meng
475*550435edSBin Meng /* Check for a common config: if not, use legacy mode (bar 0) */
476*550435edSBin Meng common = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_COMMON_CFG);
477*550435edSBin Meng if (!common) {
478*550435edSBin Meng printf("(%s): leaving for legacy driver\n", udev->name);
479*550435edSBin Meng return -ENODEV;
480*550435edSBin Meng }
481*550435edSBin Meng
482*550435edSBin Meng /* If common is there, notify should be too */
483*550435edSBin Meng notify = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_NOTIFY_CFG);
484*550435edSBin Meng if (!notify) {
485*550435edSBin Meng printf("(%s): missing capabilities %i/%i\n", udev->name,
486*550435edSBin Meng common, notify);
487*550435edSBin Meng return -EINVAL;
488*550435edSBin Meng }
489*550435edSBin Meng
490*550435edSBin Meng /*
491*550435edSBin Meng * Device capability is only mandatory for devices that have
492*550435edSBin Meng * device-specific configuration.
493*550435edSBin Meng */
494*550435edSBin Meng device = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_DEVICE_CFG);
495*550435edSBin Meng if (device) {
496*550435edSBin Meng offset = notify + offsetof(struct virtio_pci_cap, length);
497*550435edSBin Meng dm_pci_read_config32(udev, offset, &priv->device_len);
498*550435edSBin Meng }
499*550435edSBin Meng
500*550435edSBin Meng /* Map configuration structures */
501*550435edSBin Meng priv->common = virtio_pci_map_capability(udev, common);
502*550435edSBin Meng priv->notify_base = virtio_pci_map_capability(udev, notify);
503*550435edSBin Meng priv->device = virtio_pci_map_capability(udev, device);
504*550435edSBin Meng debug("(%p): common @ %p, notify base @ %p, device @ %p\n",
505*550435edSBin Meng udev, priv->common, priv->notify_base, priv->device);
506*550435edSBin Meng
507*550435edSBin Meng /* Read notify_off_multiplier from config space */
508*550435edSBin Meng offset = notify + offsetof(struct virtio_pci_notify_cap,
509*550435edSBin Meng notify_off_multiplier);
510*550435edSBin Meng dm_pci_read_config32(udev, offset, &priv->notify_offset_multiplier);
511*550435edSBin Meng
512*550435edSBin Meng debug("(%s): device (%d) vendor (%08x) version (%d)\n", udev->name,
513*550435edSBin Meng uc_priv->device, uc_priv->vendor, revision);
514*550435edSBin Meng
515*550435edSBin Meng return 0;
516*550435edSBin Meng }
517*550435edSBin Meng
518*550435edSBin Meng static const struct dm_virtio_ops virtio_pci_ops = {
519*550435edSBin Meng .get_config = virtio_pci_get_config,
520*550435edSBin Meng .set_config = virtio_pci_set_config,
521*550435edSBin Meng .generation = virtio_pci_generation,
522*550435edSBin Meng .get_status = virtio_pci_get_status,
523*550435edSBin Meng .set_status = virtio_pci_set_status,
524*550435edSBin Meng .reset = virtio_pci_reset,
525*550435edSBin Meng .get_features = virtio_pci_get_features,
526*550435edSBin Meng .set_features = virtio_pci_set_features,
527*550435edSBin Meng .find_vqs = virtio_pci_find_vqs,
528*550435edSBin Meng .del_vqs = virtio_pci_del_vqs,
529*550435edSBin Meng .notify = virtio_pci_notify,
530*550435edSBin Meng };
531*550435edSBin Meng
532*550435edSBin Meng U_BOOT_DRIVER(virtio_pci_modern) = {
533*550435edSBin Meng .name = VIRTIO_PCI_DRV_NAME,
534*550435edSBin Meng .id = UCLASS_VIRTIO,
535*550435edSBin Meng .ops = &virtio_pci_ops,
536*550435edSBin Meng .bind = virtio_pci_bind,
537*550435edSBin Meng .probe = virtio_pci_probe,
538*550435edSBin Meng .priv_auto_alloc_size = sizeof(struct virtio_pci_priv),
539*550435edSBin Meng };
540*550435edSBin Meng
541*550435edSBin Meng static struct pci_device_id virtio_pci_supported[] = {
542*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID00) },
543*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID01) },
544*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID02) },
545*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID03) },
546*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID04) },
547*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID05) },
548*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID06) },
549*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID07) },
550*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID08) },
551*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID09) },
552*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0A) },
553*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0B) },
554*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0C) },
555*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0D) },
556*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0E) },
557*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0F) },
558*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID10) },
559*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID11) },
560*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID12) },
561*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID13) },
562*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID14) },
563*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID15) },
564*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID16) },
565*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID17) },
566*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID18) },
567*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID19) },
568*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1A) },
569*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1B) },
570*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1C) },
571*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1D) },
572*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1E) },
573*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1F) },
574*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID20) },
575*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID21) },
576*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID22) },
577*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID23) },
578*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID24) },
579*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID25) },
580*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID26) },
581*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID27) },
582*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID28) },
583*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID29) },
584*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2A) },
585*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2B) },
586*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2C) },
587*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2D) },
588*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2E) },
589*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2F) },
590*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID30) },
591*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID31) },
592*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID32) },
593*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID33) },
594*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID34) },
595*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID35) },
596*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID36) },
597*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID37) },
598*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID38) },
599*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID39) },
600*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3A) },
601*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3B) },
602*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3C) },
603*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3D) },
604*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3E) },
605*550435edSBin Meng { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3F) },
606*550435edSBin Meng {},
607*550435edSBin Meng };
608*550435edSBin Meng
609*550435edSBin Meng U_BOOT_PCI_DEVICE(virtio_pci_modern, virtio_pci_supported);
610