blob: cbd667496bb16aed3b4d2386736f8cfabd36b35b [file] [log] [blame]
Jason Wangfd502722021-01-04 14:55:00 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include <linux/virtio_pci_modern.h>
4#include <linux/module.h>
5#include <linux/pci.h>
6
7/*
8 * vp_modern_map_capability - map a part of virtio pci capability
9 * @mdev: the modern virtio-pci device
10 * @off: offset of the capability
11 * @minlen: minimal length of the capability
12 * @align: align requirement
13 * @start: start from the capability
14 * @size: map size
15 * @len: the length that is actually mapped
16 *
17 * Returns the io address of for the part of the capability
18 */
19void __iomem *vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off,
20 size_t minlen,
21 u32 align,
22 u32 start, u32 size,
23 size_t *len)
24{
25 struct pci_dev *dev = mdev->pci_dev;
26 u8 bar;
27 u32 offset, length;
28 void __iomem *p;
29
30 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
31 bar),
32 &bar);
33 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
34 &offset);
35 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
36 &length);
37
38 if (length <= start) {
39 dev_err(&dev->dev,
40 "virtio_pci: bad capability len %u (>%u expected)\n",
41 length, start);
42 return NULL;
43 }
44
45 if (length - start < minlen) {
46 dev_err(&dev->dev,
47 "virtio_pci: bad capability len %u (>=%zu expected)\n",
48 length, minlen);
49 return NULL;
50 }
51
52 length -= start;
53
54 if (start + offset < offset) {
55 dev_err(&dev->dev,
56 "virtio_pci: map wrap-around %u+%u\n",
57 start, offset);
58 return NULL;
59 }
60
61 offset += start;
62
63 if (offset & (align - 1)) {
64 dev_err(&dev->dev,
65 "virtio_pci: offset %u not aligned to %u\n",
66 offset, align);
67 return NULL;
68 }
69
70 if (length > size)
71 length = size;
72
73 if (len)
74 *len = length;
75
76 if (minlen + offset < minlen ||
77 minlen + offset > pci_resource_len(dev, bar)) {
78 dev_err(&dev->dev,
79 "virtio_pci: map virtio %zu@%u "
80 "out of range on bar %i length %lu\n",
81 minlen, offset,
82 bar, (unsigned long)pci_resource_len(dev, bar));
83 return NULL;
84 }
85
86 p = pci_iomap_range(dev, bar, offset, length);
87 if (!p)
88 dev_err(&dev->dev,
89 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
90 length, offset, bar);
91 return p;
92}
93EXPORT_SYMBOL_GPL(vp_modern_map_capability);
94
95/**
96 * virtio_pci_find_capability - walk capabilities to find device info.
97 * @dev: the pci device
98 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
99 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
100 * @bars: the bitmask of BARs
101 *
102 * Returns offset of the capability, or 0.
103 */
104static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
105 u32 ioresource_types, int *bars)
106{
107 int pos;
108
109 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
110 pos > 0;
111 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
112 u8 type, bar;
113 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
114 cfg_type),
115 &type);
116 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
117 bar),
118 &bar);
119
120 /* Ignore structures with reserved BAR values */
121 if (bar > 0x5)
122 continue;
123
124 if (type == cfg_type) {
125 if (pci_resource_len(dev, bar) &&
126 pci_resource_flags(dev, bar) & ioresource_types) {
127 *bars |= (1 << bar);
128 return pos;
129 }
130 }
131 }
132 return 0;
133}
134
135/* This is part of the ABI. Don't screw with it. */
136static inline void check_offsets(void)
137{
138 /* Note: disk space was harmed in compilation of this function. */
139 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
140 offsetof(struct virtio_pci_cap, cap_vndr));
141 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
142 offsetof(struct virtio_pci_cap, cap_next));
143 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
144 offsetof(struct virtio_pci_cap, cap_len));
145 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
146 offsetof(struct virtio_pci_cap, cfg_type));
147 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
148 offsetof(struct virtio_pci_cap, bar));
149 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
150 offsetof(struct virtio_pci_cap, offset));
151 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
152 offsetof(struct virtio_pci_cap, length));
153 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
154 offsetof(struct virtio_pci_notify_cap,
155 notify_off_multiplier));
156 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
157 offsetof(struct virtio_pci_common_cfg,
158 device_feature_select));
159 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
160 offsetof(struct virtio_pci_common_cfg, device_feature));
161 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
162 offsetof(struct virtio_pci_common_cfg,
163 guest_feature_select));
164 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
165 offsetof(struct virtio_pci_common_cfg, guest_feature));
166 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
167 offsetof(struct virtio_pci_common_cfg, msix_config));
168 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
169 offsetof(struct virtio_pci_common_cfg, num_queues));
170 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
171 offsetof(struct virtio_pci_common_cfg, device_status));
172 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
173 offsetof(struct virtio_pci_common_cfg, config_generation));
174 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
175 offsetof(struct virtio_pci_common_cfg, queue_select));
176 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
177 offsetof(struct virtio_pci_common_cfg, queue_size));
178 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
179 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
180 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
181 offsetof(struct virtio_pci_common_cfg, queue_enable));
182 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
183 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
184 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
185 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
186 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
187 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
188 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
189 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
190 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
191 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
192 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
193 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
194 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
195 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
196}
197
198/*
199 * vp_modern_probe: probe the modern virtio pci device, note that the
200 * caller is required to enable PCI device before calling this function.
201 * @mdev: the modern virtio-pci device
202 *
203 * Return 0 on succeed otherwise fail
204 */
205int vp_modern_probe(struct virtio_pci_modern_device *mdev)
206{
207 struct pci_dev *pci_dev = mdev->pci_dev;
208 int err, common, isr, notify, device;
209 u32 notify_length;
210 u32 notify_offset;
211
212 check_offsets();
213
214 mdev->pci_dev = pci_dev;
215
216 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
217 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
218 return -ENODEV;
219
220 if (pci_dev->device < 0x1040) {
221 /* Transitional devices: use the PCI subsystem device id as
222 * virtio device id, same as legacy driver always did.
223 */
224 mdev->id.device = pci_dev->subsystem_device;
225 } else {
226 /* Modern devices: simply use PCI device id, but start from 0x1040. */
227 mdev->id.device = pci_dev->device - 0x1040;
228 }
229 mdev->id.vendor = pci_dev->subsystem_vendor;
230
231 /* check for a common config: if not, use legacy mode (bar 0). */
232 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
233 IORESOURCE_IO | IORESOURCE_MEM,
234 &mdev->modern_bars);
235 if (!common) {
236 dev_info(&pci_dev->dev,
237 "virtio_pci: leaving for legacy driver\n");
238 return -ENODEV;
239 }
240
241 /* If common is there, these should be too... */
242 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
243 IORESOURCE_IO | IORESOURCE_MEM,
244 &mdev->modern_bars);
245 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
246 IORESOURCE_IO | IORESOURCE_MEM,
247 &mdev->modern_bars);
248 if (!isr || !notify) {
249 dev_err(&pci_dev->dev,
250 "virtio_pci: missing capabilities %i/%i/%i\n",
251 common, isr, notify);
252 return -EINVAL;
253 }
254
255 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
256 if (err)
257 err = dma_set_mask_and_coherent(&pci_dev->dev,
258 DMA_BIT_MASK(32));
259 if (err)
260 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
261
262 /* Device capability is only mandatory for devices that have
263 * device-specific configuration.
264 */
265 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
266 IORESOURCE_IO | IORESOURCE_MEM,
267 &mdev->modern_bars);
268
269 err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
270 "virtio-pci-modern");
271 if (err)
272 return err;
273
274 err = -EINVAL;
275 mdev->common = vp_modern_map_capability(mdev, common,
276 sizeof(struct virtio_pci_common_cfg), 4,
277 0, sizeof(struct virtio_pci_common_cfg),
278 NULL);
279 if (!mdev->common)
280 goto err_map_common;
281 mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
282 0, 1,
283 NULL);
284 if (!mdev->isr)
285 goto err_map_isr;
286
287 /* Read notify_off_multiplier from config space. */
288 pci_read_config_dword(pci_dev,
289 notify + offsetof(struct virtio_pci_notify_cap,
290 notify_off_multiplier),
291 &mdev->notify_offset_multiplier);
292 /* Read notify length and offset from config space. */
293 pci_read_config_dword(pci_dev,
294 notify + offsetof(struct virtio_pci_notify_cap,
295 cap.length),
296 &notify_length);
297
298 pci_read_config_dword(pci_dev,
299 notify + offsetof(struct virtio_pci_notify_cap,
300 cap.offset),
301 &notify_offset);
302
303 /* We don't know how many VQs we'll map, ahead of the time.
304 * If notify length is small, map it all now.
305 * Otherwise, map each VQ individually later.
306 */
307 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
308 mdev->notify_base = vp_modern_map_capability(mdev, notify,
309 2, 2,
310 0, notify_length,
311 &mdev->notify_len);
312 if (!mdev->notify_base)
313 goto err_map_notify;
314 } else {
315 mdev->notify_map_cap = notify;
316 }
317
318 /* Again, we don't know how much we should map, but PAGE_SIZE
319 * is more than enough for all existing devices.
320 */
321 if (device) {
322 mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
323 0, PAGE_SIZE,
324 &mdev->device_len);
325 if (!mdev->device)
326 goto err_map_device;
327 }
328
329 return 0;
330
331err_map_device:
332 if (mdev->notify_base)
333 pci_iounmap(pci_dev, mdev->notify_base);
334err_map_notify:
335 pci_iounmap(pci_dev, mdev->isr);
336err_map_isr:
337 pci_iounmap(pci_dev, mdev->common);
338err_map_common:
339 return err;
340}
341EXPORT_SYMBOL_GPL(vp_modern_probe);
342
343/*
344 * vp_modern_probe: remove and cleanup the modern virtio pci device
345 * @mdev: the modern virtio-pci device
346 */
347void vp_modern_remove(struct virtio_pci_modern_device *mdev)
348{
349 struct pci_dev *pci_dev = mdev->pci_dev;
350
351 if (mdev->device)
352 pci_iounmap(pci_dev, mdev->device);
353 if (mdev->notify_base)
354 pci_iounmap(pci_dev, mdev->notify_base);
355 pci_iounmap(pci_dev, mdev->isr);
356 pci_iounmap(pci_dev, mdev->common);
357 pci_release_selected_regions(pci_dev, mdev->modern_bars);
358}
359EXPORT_SYMBOL_GPL(vp_modern_remove);
360
361/*
362 * vp_modern_get_features - get features from device
363 * @mdev: the modern virtio-pci device
364 *
365 * Returns the features read from the device
366 */
367u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
368{
369 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
370
371 u64 features;
372
373 vp_iowrite32(0, &cfg->device_feature_select);
374 features = vp_ioread32(&cfg->device_feature);
375 vp_iowrite32(1, &cfg->device_feature_select);
376 features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
377
378 return features;
379}
380EXPORT_SYMBOL_GPL(vp_modern_get_features);
381
382/*
383 * vp_modern_set_features - set features to device
384 * @mdev: the modern virtio-pci device
385 * @features: the features set to device
386 */
387void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
388 u64 features)
389{
390 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
391
392 vp_iowrite32(0, &cfg->guest_feature_select);
393 vp_iowrite32((u32)features, &cfg->guest_feature);
394 vp_iowrite32(1, &cfg->guest_feature_select);
395 vp_iowrite32(features >> 32, &cfg->guest_feature);
396}
397EXPORT_SYMBOL_GPL(vp_modern_set_features);
398
399/*
400 * vp_modern_generation - get the device genreation
401 * @mdev: the modern virtio-pci device
402 *
403 * Returns the genreation read from device
404 */
405u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
406{
407 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
408
409 return vp_ioread8(&cfg->config_generation);
410}
411EXPORT_SYMBOL_GPL(vp_modern_generation);
412
413/*
414 * vp_modern_get_status - get the device status
415 * @mdev: the modern virtio-pci device
416 *
417 * Returns the status read from device
418 */
419u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
420{
421 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
422
423 return vp_ioread8(&cfg->device_status);
424}
425EXPORT_SYMBOL_GPL(vp_modern_get_status);
426
427/*
428 * vp_modern_set_status - set status to device
429 * @mdev: the modern virtio-pci device
430 * @status: the status set to device
431 */
432void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
433 u8 status)
434{
435 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
436
437 vp_iowrite8(status, &cfg->device_status);
438}
439EXPORT_SYMBOL_GPL(vp_modern_set_status);
440
441/*
442 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
443 * @mdev: the modern virtio-pci device
444 * @index: queue index
445 * @vector: the config vector
446 *
447 * Returns the config vector read from the device
448 */
449u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
450 u16 index, u16 vector)
451{
452 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
453
454 vp_iowrite16(index, &cfg->queue_select);
455 vp_iowrite16(vector, &cfg->queue_msix_vector);
456 /* Flush the write out to device */
457 return vp_ioread16(&cfg->queue_msix_vector);
458}
459EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
460
461/*
462 * vp_modern_config_vector - set the vector for config interrupt
463 * @mdev: the modern virtio-pci device
464 * @vector: the config vector
465 *
466 * Returns the config vector read from the device
467 */
468u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
469 u16 vector)
470{
471 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
472
473 /* Setup the vector used for configuration events */
474 vp_iowrite16(vector, &cfg->msix_config);
475 /* Verify we had enough resources to assign the vector */
476 /* Will also flush the write out to device */
477 return vp_ioread16(&cfg->msix_config);
478}
479EXPORT_SYMBOL_GPL(vp_modern_config_vector);
480
481/*
482 * vp_modern_queue_address - set the virtqueue address
483 * @mdev: the modern virtio-pci device
484 * @index: the queue index
485 * @desc_addr: address of the descriptor area
486 * @driver_addr: address of the driver area
487 * @device_addr: address of the device area
488 */
489void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
490 u16 index, u64 desc_addr, u64 driver_addr,
491 u64 device_addr)
492{
493 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
494
495 vp_iowrite16(index, &cfg->queue_select);
496
497 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
498 &cfg->queue_desc_hi);
499 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
500 &cfg->queue_avail_hi);
501 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
502 &cfg->queue_used_hi);
503}
504EXPORT_SYMBOL_GPL(vp_modern_queue_address);
505
506/*
507 * vp_modern_set_queue_enable - enable a virtqueue
508 * @mdev: the modern virtio-pci device
509 * @index: the queue index
510 * @enable: whether the virtqueue is enable or not
511 */
512void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
513 u16 index, bool enable)
514{
515 vp_iowrite16(index, &mdev->common->queue_select);
516 vp_iowrite16(enable, &mdev->common->queue_enable);
517}
518EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
519
520/*
521 * vp_modern_get_queue_enable - enable a virtqueue
522 * @mdev: the modern virtio-pci device
523 * @index: the queue index
524 *
525 * Returns whether a virtqueue is enabled or not
526 */
527bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
528 u16 index)
529{
530 vp_iowrite16(index, &mdev->common->queue_select);
531
532 return vp_ioread16(&mdev->common->queue_enable);
533}
534EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
535
536/*
537 * vp_modern_set_queue_size - set size for a virtqueue
538 * @mdev: the modern virtio-pci device
539 * @index: the queue index
540 * @size: the size of the virtqueue
541 */
542void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
543 u16 index, u16 size)
544{
545 vp_iowrite16(index, &mdev->common->queue_select);
546 vp_iowrite16(size, &mdev->common->queue_size);
547
548}
549EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
550
551/*
552 * vp_modern_get_queue_size - get size for a virtqueue
553 * @mdev: the modern virtio-pci device
554 * @index: the queue index
555 *
556 * Returns the size of the virtqueue
557 */
558u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
559 u16 index)
560{
561 vp_iowrite16(index, &mdev->common->queue_select);
562
563 return vp_ioread16(&mdev->common->queue_size);
564
565}
566EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
567
568/*
569 * vp_modern_get_num_queues - get the number of virtqueues
570 * @mdev: the modern virtio-pci device
571 *
572 * Returns the number of virtqueues
573 */
574u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
575{
576 return vp_ioread16(&mdev->common->num_queues);
577}
578EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
579
580/*
581 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
582 * @mdev: the modern virtio-pci device
583 * @index: the queue index
584 *
585 * Returns the notification offset for a virtqueue
586 */
587u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
588 u16 index)
589{
590 vp_iowrite16(index, &mdev->common->queue_select);
591
592 return vp_ioread16(&mdev->common->queue_notify_off);
593}
594EXPORT_SYMBOL_GPL(vp_modern_get_queue_notify_off);
595
596MODULE_VERSION("0.1");
597MODULE_DESCRIPTION("Modern Virtio PCI Device");
598MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
599MODULE_LICENSE("GPL");