1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/virtio_pci_modern.h>
4 #include <linux/module.h>
6 #include <linux/delay.h>
9 * vp_modern_map_capability - map a part of virtio pci capability
10 * @mdev: the modern virtio-pci device
11 * @off: offset of the capability
12 * @minlen: minimal length of the capability
13 * @align: align requirement
14 * @start: start from the capability
16 * @len: the length that is actually mapped
17 * @pa: physical address of the capability
19 * Returns the io address of for the part of the capability
22 vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off,
23 size_t minlen, u32 align, u32 start, u32 size,
24 size_t *len, resource_size_t *pa)
26 struct pci_dev *dev = mdev->pci_dev;
31 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
34 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
36 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
39 /* Check if the BAR may have changed since we requested the region. */
40 if (bar >= PCI_STD_NUM_BARS || !(mdev->modern_bars & (1 << bar))) {
42 "virtio_pci: bar unexpectedly changed to %u\n", bar);
46 if (length <= start) {
48 "virtio_pci: bad capability len %u (>%u expected)\n",
53 if (length - start < minlen) {
55 "virtio_pci: bad capability len %u (>=%zu expected)\n",
62 if (start + offset < offset) {
64 "virtio_pci: map wrap-around %u+%u\n",
71 if (offset & (align - 1)) {
73 "virtio_pci: offset %u not aligned to %u\n",
84 if (minlen + offset < minlen ||
85 minlen + offset > pci_resource_len(dev, bar)) {
87 "virtio_pci: map virtio %zu@%u "
88 "out of range on bar %i length %lu\n",
90 bar, (unsigned long)pci_resource_len(dev, bar));
94 p = pci_iomap_range(dev, bar, offset, length);
97 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
100 *pa = pci_resource_start(dev, bar) + offset;
106 * virtio_pci_find_capability - walk capabilities to find device info.
107 * @dev: the pci device
108 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
109 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
110 * @bars: the bitmask of BARs
112 * Returns offset of the capability, or 0.
114 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
115 u32 ioresource_types, int *bars)
119 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
121 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
123 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
126 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
130 /* Ignore structures with reserved BAR values */
131 if (bar >= PCI_STD_NUM_BARS)
134 if (type == cfg_type) {
135 if (pci_resource_len(dev, bar) &&
136 pci_resource_flags(dev, bar) & ioresource_types) {
145 /* This is part of the ABI. Don't screw with it. */
146 static inline void check_offsets(void)
148 /* Note: disk space was harmed in compilation of this function. */
149 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
150 offsetof(struct virtio_pci_cap, cap_vndr));
151 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
152 offsetof(struct virtio_pci_cap, cap_next));
153 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
154 offsetof(struct virtio_pci_cap, cap_len));
155 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
156 offsetof(struct virtio_pci_cap, cfg_type));
157 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
158 offsetof(struct virtio_pci_cap, bar));
159 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
160 offsetof(struct virtio_pci_cap, offset));
161 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
162 offsetof(struct virtio_pci_cap, length));
163 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
164 offsetof(struct virtio_pci_notify_cap,
165 notify_off_multiplier));
166 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
167 offsetof(struct virtio_pci_common_cfg,
168 device_feature_select));
169 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
170 offsetof(struct virtio_pci_common_cfg, device_feature));
171 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
172 offsetof(struct virtio_pci_common_cfg,
173 guest_feature_select));
174 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
175 offsetof(struct virtio_pci_common_cfg, guest_feature));
176 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
177 offsetof(struct virtio_pci_common_cfg, msix_config));
178 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
179 offsetof(struct virtio_pci_common_cfg, num_queues));
180 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
181 offsetof(struct virtio_pci_common_cfg, device_status));
182 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
183 offsetof(struct virtio_pci_common_cfg, config_generation));
184 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
185 offsetof(struct virtio_pci_common_cfg, queue_select));
186 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
187 offsetof(struct virtio_pci_common_cfg, queue_size));
188 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
189 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
190 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
191 offsetof(struct virtio_pci_common_cfg, queue_enable));
192 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
193 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
194 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
195 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
196 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
197 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
198 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
199 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
200 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
201 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
202 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
203 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
204 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
205 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
209 * vp_modern_probe: probe the modern virtio pci device, note that the
210 * caller is required to enable PCI device before calling this function.
211 * @mdev: the modern virtio-pci device
213 * Return 0 on succeed otherwise fail
215 int vp_modern_probe(struct virtio_pci_modern_device *mdev)
217 struct pci_dev *pci_dev = mdev->pci_dev;
218 int err, common, isr, notify, device;
224 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
225 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
228 if (pci_dev->device < 0x1040) {
229 /* Transitional devices: use the PCI subsystem device id as
230 * virtio device id, same as legacy driver always did.
232 mdev->id.device = pci_dev->subsystem_device;
234 /* Modern devices: simply use PCI device id, but start from 0x1040. */
235 mdev->id.device = pci_dev->device - 0x1040;
237 mdev->id.vendor = pci_dev->subsystem_vendor;
239 /* check for a common config: if not, use legacy mode (bar 0). */
240 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
241 IORESOURCE_IO | IORESOURCE_MEM,
244 dev_info(&pci_dev->dev,
245 "virtio_pci: leaving for legacy driver\n");
249 /* If common is there, these should be too... */
250 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
251 IORESOURCE_IO | IORESOURCE_MEM,
253 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
254 IORESOURCE_IO | IORESOURCE_MEM,
256 if (!isr || !notify) {
257 dev_err(&pci_dev->dev,
258 "virtio_pci: missing capabilities %i/%i/%i\n",
259 common, isr, notify);
263 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
265 err = dma_set_mask_and_coherent(&pci_dev->dev,
268 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
270 /* Device capability is only mandatory for devices that have
271 * device-specific configuration.
273 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
274 IORESOURCE_IO | IORESOURCE_MEM,
277 err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
278 "virtio-pci-modern");
283 mdev->common = vp_modern_map_capability(mdev, common,
284 sizeof(struct virtio_pci_common_cfg), 4,
285 0, sizeof(struct virtio_pci_common_cfg),
289 mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
295 /* Read notify_off_multiplier from config space. */
296 pci_read_config_dword(pci_dev,
297 notify + offsetof(struct virtio_pci_notify_cap,
298 notify_off_multiplier),
299 &mdev->notify_offset_multiplier);
300 /* Read notify length and offset from config space. */
301 pci_read_config_dword(pci_dev,
302 notify + offsetof(struct virtio_pci_notify_cap,
306 pci_read_config_dword(pci_dev,
307 notify + offsetof(struct virtio_pci_notify_cap,
311 /* We don't know how many VQs we'll map, ahead of the time.
312 * If notify length is small, map it all now.
313 * Otherwise, map each VQ individually later.
315 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
316 mdev->notify_base = vp_modern_map_capability(mdev, notify,
321 if (!mdev->notify_base)
324 mdev->notify_map_cap = notify;
327 /* Again, we don't know how much we should map, but PAGE_SIZE
328 * is more than enough for all existing devices.
331 mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
342 if (mdev->notify_base)
343 pci_iounmap(pci_dev, mdev->notify_base);
345 pci_iounmap(pci_dev, mdev->isr);
347 pci_iounmap(pci_dev, mdev->common);
349 pci_release_selected_regions(pci_dev, mdev->modern_bars);
352 EXPORT_SYMBOL_GPL(vp_modern_probe);
355 * vp_modern_remove: remove and cleanup the modern virtio pci device
356 * @mdev: the modern virtio-pci device
358 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
360 struct pci_dev *pci_dev = mdev->pci_dev;
363 pci_iounmap(pci_dev, mdev->device);
364 if (mdev->notify_base)
365 pci_iounmap(pci_dev, mdev->notify_base);
366 pci_iounmap(pci_dev, mdev->isr);
367 pci_iounmap(pci_dev, mdev->common);
368 pci_release_selected_regions(pci_dev, mdev->modern_bars);
370 EXPORT_SYMBOL_GPL(vp_modern_remove);
373 * vp_modern_get_features - get features from device
374 * @mdev: the modern virtio-pci device
376 * Returns the features read from the device
378 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
380 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
384 vp_iowrite32(0, &cfg->device_feature_select);
385 features = vp_ioread32(&cfg->device_feature);
386 vp_iowrite32(1, &cfg->device_feature_select);
387 features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
391 EXPORT_SYMBOL_GPL(vp_modern_get_features);
394 * vp_modern_get_driver_features - get driver features from device
395 * @mdev: the modern virtio-pci device
397 * Returns the driver features read from the device
399 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
401 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
405 vp_iowrite32(0, &cfg->guest_feature_select);
406 features = vp_ioread32(&cfg->guest_feature);
407 vp_iowrite32(1, &cfg->guest_feature_select);
408 features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
412 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
415 * vp_modern_set_features - set features to device
416 * @mdev: the modern virtio-pci device
417 * @features: the features set to device
419 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
422 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
424 vp_iowrite32(0, &cfg->guest_feature_select);
425 vp_iowrite32((u32)features, &cfg->guest_feature);
426 vp_iowrite32(1, &cfg->guest_feature_select);
427 vp_iowrite32(features >> 32, &cfg->guest_feature);
429 EXPORT_SYMBOL_GPL(vp_modern_set_features);
432 * vp_modern_generation - get the device genreation
433 * @mdev: the modern virtio-pci device
435 * Returns the genreation read from device
437 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
439 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
441 return vp_ioread8(&cfg->config_generation);
443 EXPORT_SYMBOL_GPL(vp_modern_generation);
446 * vp_modern_get_status - get the device status
447 * @mdev: the modern virtio-pci device
449 * Returns the status read from device
451 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
453 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
455 return vp_ioread8(&cfg->device_status);
457 EXPORT_SYMBOL_GPL(vp_modern_get_status);
460 * vp_modern_set_status - set status to device
461 * @mdev: the modern virtio-pci device
462 * @status: the status set to device
464 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
467 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
470 * Per memory-barriers.txt, wmb() is not needed to guarantee
471 * that the cache coherent memory writes have completed
472 * before writing to the MMIO region.
474 vp_iowrite8(status, &cfg->device_status);
476 EXPORT_SYMBOL_GPL(vp_modern_set_status);
479 * vp_modern_get_queue_reset - get the queue reset status
480 * @mdev: the modern virtio-pci device
481 * @index: queue index
483 int vp_modern_get_queue_reset(struct virtio_pci_modern_device *mdev, u16 index)
485 struct virtio_pci_modern_common_cfg __iomem *cfg;
487 cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
489 vp_iowrite16(index, &cfg->cfg.queue_select);
490 return vp_ioread16(&cfg->queue_reset);
492 EXPORT_SYMBOL_GPL(vp_modern_get_queue_reset);
495 * vp_modern_set_queue_reset - reset the queue
496 * @mdev: the modern virtio-pci device
497 * @index: queue index
499 void vp_modern_set_queue_reset(struct virtio_pci_modern_device *mdev, u16 index)
501 struct virtio_pci_modern_common_cfg __iomem *cfg;
503 cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common;
505 vp_iowrite16(index, &cfg->cfg.queue_select);
506 vp_iowrite16(1, &cfg->queue_reset);
508 while (vp_ioread16(&cfg->queue_reset))
511 while (vp_ioread16(&cfg->cfg.queue_enable))
514 EXPORT_SYMBOL_GPL(vp_modern_set_queue_reset);
517 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
518 * @mdev: the modern virtio-pci device
519 * @index: queue index
520 * @vector: the config vector
522 * Returns the config vector read from the device
524 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
525 u16 index, u16 vector)
527 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
529 vp_iowrite16(index, &cfg->queue_select);
530 vp_iowrite16(vector, &cfg->queue_msix_vector);
531 /* Flush the write out to device */
532 return vp_ioread16(&cfg->queue_msix_vector);
534 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
537 * vp_modern_config_vector - set the vector for config interrupt
538 * @mdev: the modern virtio-pci device
539 * @vector: the config vector
541 * Returns the config vector read from the device
543 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
546 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
548 /* Setup the vector used for configuration events */
549 vp_iowrite16(vector, &cfg->msix_config);
550 /* Verify we had enough resources to assign the vector */
551 /* Will also flush the write out to device */
552 return vp_ioread16(&cfg->msix_config);
554 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
557 * vp_modern_queue_address - set the virtqueue address
558 * @mdev: the modern virtio-pci device
559 * @index: the queue index
560 * @desc_addr: address of the descriptor area
561 * @driver_addr: address of the driver area
562 * @device_addr: address of the device area
564 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
565 u16 index, u64 desc_addr, u64 driver_addr,
568 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
570 vp_iowrite16(index, &cfg->queue_select);
572 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
573 &cfg->queue_desc_hi);
574 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
575 &cfg->queue_avail_hi);
576 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
577 &cfg->queue_used_hi);
579 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
582 * vp_modern_set_queue_enable - enable a virtqueue
583 * @mdev: the modern virtio-pci device
584 * @index: the queue index
585 * @enable: whether the virtqueue is enable or not
587 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
588 u16 index, bool enable)
590 vp_iowrite16(index, &mdev->common->queue_select);
591 vp_iowrite16(enable, &mdev->common->queue_enable);
593 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
596 * vp_modern_get_queue_enable - enable a virtqueue
597 * @mdev: the modern virtio-pci device
598 * @index: the queue index
600 * Returns whether a virtqueue is enabled or not
602 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
605 vp_iowrite16(index, &mdev->common->queue_select);
607 return vp_ioread16(&mdev->common->queue_enable);
609 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
612 * vp_modern_set_queue_size - set size for a virtqueue
613 * @mdev: the modern virtio-pci device
614 * @index: the queue index
615 * @size: the size of the virtqueue
617 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
620 vp_iowrite16(index, &mdev->common->queue_select);
621 vp_iowrite16(size, &mdev->common->queue_size);
624 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
627 * vp_modern_get_queue_size - get size for a virtqueue
628 * @mdev: the modern virtio-pci device
629 * @index: the queue index
631 * Returns the size of the virtqueue
633 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
636 vp_iowrite16(index, &mdev->common->queue_select);
638 return vp_ioread16(&mdev->common->queue_size);
641 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
644 * vp_modern_get_num_queues - get the number of virtqueues
645 * @mdev: the modern virtio-pci device
647 * Returns the number of virtqueues
649 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
651 return vp_ioread16(&mdev->common->num_queues);
653 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
656 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
657 * @mdev: the modern virtio-pci device
658 * @index: the queue index
660 * Returns the notification offset for a virtqueue
662 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
665 vp_iowrite16(index, &mdev->common->queue_select);
667 return vp_ioread16(&mdev->common->queue_notify_off);
671 * vp_modern_map_vq_notify - map notification area for a
673 * @mdev: the modern virtio-pci device
674 * @index: the queue index
675 * @pa: the pointer to the physical address of the nofity area
677 * Returns the address of the notification area
679 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
680 u16 index, resource_size_t *pa)
682 u16 off = vp_modern_get_queue_notify_off(mdev, index);
684 if (mdev->notify_base) {
685 /* offset should not wrap */
686 if ((u64)off * mdev->notify_offset_multiplier + 2
687 > mdev->notify_len) {
688 dev_warn(&mdev->pci_dev->dev,
689 "bad notification offset %u (x %u) "
690 "for queue %u > %zd",
691 off, mdev->notify_offset_multiplier,
692 index, mdev->notify_len);
696 *pa = mdev->notify_pa +
697 off * mdev->notify_offset_multiplier;
698 return mdev->notify_base + off * mdev->notify_offset_multiplier;
700 return vp_modern_map_capability(mdev,
701 mdev->notify_map_cap, 2, 2,
702 off * mdev->notify_offset_multiplier, 2,
706 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
708 MODULE_VERSION("0.1");
709 MODULE_DESCRIPTION("Modern Virtio PCI Device");
711 MODULE_LICENSE("GPL");