1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/virtio_pci_modern.h>
4 #include <linux/module.h>
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
15 * @len: the length that is actually mapped
16 * @pa: physical address of the capability
18 * Returns the io address of for the part of the capability
21 vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off,
22 size_t minlen, u32 align, u32 start, u32 size,
23 size_t *len, resource_size_t *pa)
25 struct pci_dev *dev = mdev->pci_dev;
30 pci_read_config_byte(dev, off + offsetof(struct virtio_pci_cap,
33 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, offset),
35 pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
38 /* Check if the BAR may have changed since we requested the region. */
39 if (bar >= PCI_STD_NUM_BARS || !(mdev->modern_bars & (1 << bar))) {
41 "virtio_pci: bar unexpectedly changed to %u\n", bar);
45 if (length <= start) {
47 "virtio_pci: bad capability len %u (>%u expected)\n",
52 if (length - start < minlen) {
54 "virtio_pci: bad capability len %u (>=%zu expected)\n",
61 if (start + offset < offset) {
63 "virtio_pci: map wrap-around %u+%u\n",
70 if (offset & (align - 1)) {
72 "virtio_pci: offset %u not aligned to %u\n",
83 if (minlen + offset < minlen ||
84 minlen + offset > pci_resource_len(dev, bar)) {
86 "virtio_pci: map virtio %zu@%u "
87 "out of range on bar %i length %lu\n",
89 bar, (unsigned long)pci_resource_len(dev, bar));
93 p = pci_iomap_range(dev, bar, offset, length);
96 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
99 *pa = pci_resource_start(dev, bar) + offset;
105 * virtio_pci_find_capability - walk capabilities to find device info.
106 * @dev: the pci device
107 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
108 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
109 * @bars: the bitmask of BARs
111 * Returns offset of the capability, or 0.
113 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
114 u32 ioresource_types, int *bars)
118 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
120 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
122 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
125 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
129 /* Ignore structures with reserved BAR values */
130 if (bar >= PCI_STD_NUM_BARS)
133 if (type == cfg_type) {
134 if (pci_resource_len(dev, bar) &&
135 pci_resource_flags(dev, bar) & ioresource_types) {
144 /* This is part of the ABI. Don't screw with it. */
145 static inline void check_offsets(void)
147 /* Note: disk space was harmed in compilation of this function. */
148 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
149 offsetof(struct virtio_pci_cap, cap_vndr));
150 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
151 offsetof(struct virtio_pci_cap, cap_next));
152 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
153 offsetof(struct virtio_pci_cap, cap_len));
154 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
155 offsetof(struct virtio_pci_cap, cfg_type));
156 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
157 offsetof(struct virtio_pci_cap, bar));
158 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
159 offsetof(struct virtio_pci_cap, offset));
160 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
161 offsetof(struct virtio_pci_cap, length));
162 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
163 offsetof(struct virtio_pci_notify_cap,
164 notify_off_multiplier));
165 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
166 offsetof(struct virtio_pci_common_cfg,
167 device_feature_select));
168 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
169 offsetof(struct virtio_pci_common_cfg, device_feature));
170 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
171 offsetof(struct virtio_pci_common_cfg,
172 guest_feature_select));
173 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
174 offsetof(struct virtio_pci_common_cfg, guest_feature));
175 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
176 offsetof(struct virtio_pci_common_cfg, msix_config));
177 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
178 offsetof(struct virtio_pci_common_cfg, num_queues));
179 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
180 offsetof(struct virtio_pci_common_cfg, device_status));
181 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
182 offsetof(struct virtio_pci_common_cfg, config_generation));
183 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
184 offsetof(struct virtio_pci_common_cfg, queue_select));
185 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
186 offsetof(struct virtio_pci_common_cfg, queue_size));
187 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
188 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
189 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
190 offsetof(struct virtio_pci_common_cfg, queue_enable));
191 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
192 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
193 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
194 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
195 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
196 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
197 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
198 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
199 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
200 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
201 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
202 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
203 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
204 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
208 * vp_modern_probe: probe the modern virtio pci device, note that the
209 * caller is required to enable PCI device before calling this function.
210 * @mdev: the modern virtio-pci device
212 * Return 0 on succeed otherwise fail
214 int vp_modern_probe(struct virtio_pci_modern_device *mdev)
216 struct pci_dev *pci_dev = mdev->pci_dev;
217 int err, common, isr, notify, device;
223 mdev->pci_dev = pci_dev;
225 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
226 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
229 if (pci_dev->device < 0x1040) {
230 /* Transitional devices: use the PCI subsystem device id as
231 * virtio device id, same as legacy driver always did.
233 mdev->id.device = pci_dev->subsystem_device;
235 /* Modern devices: simply use PCI device id, but start from 0x1040. */
236 mdev->id.device = pci_dev->device - 0x1040;
238 mdev->id.vendor = pci_dev->subsystem_vendor;
240 /* check for a common config: if not, use legacy mode (bar 0). */
241 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
242 IORESOURCE_IO | IORESOURCE_MEM,
245 dev_info(&pci_dev->dev,
246 "virtio_pci: leaving for legacy driver\n");
250 /* If common is there, these should be too... */
251 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
252 IORESOURCE_IO | IORESOURCE_MEM,
254 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
255 IORESOURCE_IO | IORESOURCE_MEM,
257 if (!isr || !notify) {
258 dev_err(&pci_dev->dev,
259 "virtio_pci: missing capabilities %i/%i/%i\n",
260 common, isr, notify);
264 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
266 err = dma_set_mask_and_coherent(&pci_dev->dev,
269 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
271 /* Device capability is only mandatory for devices that have
272 * device-specific configuration.
274 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
275 IORESOURCE_IO | IORESOURCE_MEM,
278 err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
279 "virtio-pci-modern");
284 mdev->common = vp_modern_map_capability(mdev, common,
285 sizeof(struct virtio_pci_common_cfg), 4,
286 0, sizeof(struct virtio_pci_common_cfg),
290 mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
296 /* Read notify_off_multiplier from config space. */
297 pci_read_config_dword(pci_dev,
298 notify + offsetof(struct virtio_pci_notify_cap,
299 notify_off_multiplier),
300 &mdev->notify_offset_multiplier);
301 /* Read notify length and offset from config space. */
302 pci_read_config_dword(pci_dev,
303 notify + offsetof(struct virtio_pci_notify_cap,
307 pci_read_config_dword(pci_dev,
308 notify + offsetof(struct virtio_pci_notify_cap,
312 /* We don't know how many VQs we'll map, ahead of the time.
313 * If notify length is small, map it all now.
314 * Otherwise, map each VQ individually later.
316 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
317 mdev->notify_base = vp_modern_map_capability(mdev, notify,
322 if (!mdev->notify_base)
325 mdev->notify_map_cap = notify;
328 /* Again, we don't know how much we should map, but PAGE_SIZE
329 * is more than enough for all existing devices.
332 mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
343 if (mdev->notify_base)
344 pci_iounmap(pci_dev, mdev->notify_base);
346 pci_iounmap(pci_dev, mdev->isr);
348 pci_iounmap(pci_dev, mdev->common);
350 pci_release_selected_regions(pci_dev, mdev->modern_bars);
353 EXPORT_SYMBOL_GPL(vp_modern_probe);
356 * vp_modern_remove: remove and cleanup the modern virtio pci device
357 * @mdev: the modern virtio-pci device
359 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
361 struct pci_dev *pci_dev = mdev->pci_dev;
364 pci_iounmap(pci_dev, mdev->device);
365 if (mdev->notify_base)
366 pci_iounmap(pci_dev, mdev->notify_base);
367 pci_iounmap(pci_dev, mdev->isr);
368 pci_iounmap(pci_dev, mdev->common);
369 pci_release_selected_regions(pci_dev, mdev->modern_bars);
371 EXPORT_SYMBOL_GPL(vp_modern_remove);
374 * vp_modern_get_features - get features from device
375 * @mdev: the modern virtio-pci device
377 * Returns the features read from the device
379 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
381 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
385 vp_iowrite32(0, &cfg->device_feature_select);
386 features = vp_ioread32(&cfg->device_feature);
387 vp_iowrite32(1, &cfg->device_feature_select);
388 features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
392 EXPORT_SYMBOL_GPL(vp_modern_get_features);
395 * vp_modern_get_driver_features - get driver features from device
396 * @mdev: the modern virtio-pci device
398 * Returns the driver features read from the device
400 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
402 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
406 vp_iowrite32(0, &cfg->guest_feature_select);
407 features = vp_ioread32(&cfg->guest_feature);
408 vp_iowrite32(1, &cfg->guest_feature_select);
409 features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
413 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
416 * vp_modern_set_features - set features to device
417 * @mdev: the modern virtio-pci device
418 * @features: the features set to device
420 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
423 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
425 vp_iowrite32(0, &cfg->guest_feature_select);
426 vp_iowrite32((u32)features, &cfg->guest_feature);
427 vp_iowrite32(1, &cfg->guest_feature_select);
428 vp_iowrite32(features >> 32, &cfg->guest_feature);
430 EXPORT_SYMBOL_GPL(vp_modern_set_features);
433 * vp_modern_generation - get the device genreation
434 * @mdev: the modern virtio-pci device
436 * Returns the genreation read from device
438 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
440 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
442 return vp_ioread8(&cfg->config_generation);
444 EXPORT_SYMBOL_GPL(vp_modern_generation);
447 * vp_modern_get_status - get the device status
448 * @mdev: the modern virtio-pci device
450 * Returns the status read from device
452 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
454 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
456 return vp_ioread8(&cfg->device_status);
458 EXPORT_SYMBOL_GPL(vp_modern_get_status);
461 * vp_modern_set_status - set status to device
462 * @mdev: the modern virtio-pci device
463 * @status: the status set to device
465 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
468 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
471 * Per memory-barriers.txt, wmb() is not needed to guarantee
472 * that the cache coherent memory writes have completed
473 * before writing to the MMIO region.
475 vp_iowrite8(status, &cfg->device_status);
477 EXPORT_SYMBOL_GPL(vp_modern_set_status);
480 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
481 * @mdev: the modern virtio-pci device
482 * @index: queue index
483 * @vector: the config vector
485 * Returns the config vector read from the device
487 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
488 u16 index, u16 vector)
490 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
492 vp_iowrite16(index, &cfg->queue_select);
493 vp_iowrite16(vector, &cfg->queue_msix_vector);
494 /* Flush the write out to device */
495 return vp_ioread16(&cfg->queue_msix_vector);
497 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
500 * vp_modern_config_vector - set the vector for config interrupt
501 * @mdev: the modern virtio-pci device
502 * @vector: the config vector
504 * Returns the config vector read from the device
506 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
509 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
511 /* Setup the vector used for configuration events */
512 vp_iowrite16(vector, &cfg->msix_config);
513 /* Verify we had enough resources to assign the vector */
514 /* Will also flush the write out to device */
515 return vp_ioread16(&cfg->msix_config);
517 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
520 * vp_modern_queue_address - set the virtqueue address
521 * @mdev: the modern virtio-pci device
522 * @index: the queue index
523 * @desc_addr: address of the descriptor area
524 * @driver_addr: address of the driver area
525 * @device_addr: address of the device area
527 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
528 u16 index, u64 desc_addr, u64 driver_addr,
531 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
533 vp_iowrite16(index, &cfg->queue_select);
535 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
536 &cfg->queue_desc_hi);
537 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
538 &cfg->queue_avail_hi);
539 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
540 &cfg->queue_used_hi);
542 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
545 * vp_modern_set_queue_enable - enable a virtqueue
546 * @mdev: the modern virtio-pci device
547 * @index: the queue index
548 * @enable: whether the virtqueue is enable or not
550 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
551 u16 index, bool enable)
553 vp_iowrite16(index, &mdev->common->queue_select);
554 vp_iowrite16(enable, &mdev->common->queue_enable);
556 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
559 * vp_modern_get_queue_enable - enable a virtqueue
560 * @mdev: the modern virtio-pci device
561 * @index: the queue index
563 * Returns whether a virtqueue is enabled or not
565 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
568 vp_iowrite16(index, &mdev->common->queue_select);
570 return vp_ioread16(&mdev->common->queue_enable);
572 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
575 * vp_modern_set_queue_size - set size for a virtqueue
576 * @mdev: the modern virtio-pci device
577 * @index: the queue index
578 * @size: the size of the virtqueue
580 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
583 vp_iowrite16(index, &mdev->common->queue_select);
584 vp_iowrite16(size, &mdev->common->queue_size);
587 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
590 * vp_modern_get_queue_size - get size for a virtqueue
591 * @mdev: the modern virtio-pci device
592 * @index: the queue index
594 * Returns the size of the virtqueue
596 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
599 vp_iowrite16(index, &mdev->common->queue_select);
601 return vp_ioread16(&mdev->common->queue_size);
604 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
607 * vp_modern_get_num_queues - get the number of virtqueues
608 * @mdev: the modern virtio-pci device
610 * Returns the number of virtqueues
612 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
614 return vp_ioread16(&mdev->common->num_queues);
616 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
619 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
620 * @mdev: the modern virtio-pci device
621 * @index: the queue index
623 * Returns the notification offset for a virtqueue
625 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
628 vp_iowrite16(index, &mdev->common->queue_select);
630 return vp_ioread16(&mdev->common->queue_notify_off);
634 * vp_modern_map_vq_notify - map notification area for a
636 * @mdev: the modern virtio-pci device
637 * @index: the queue index
638 * @pa: the pointer to the physical address of the nofity area
640 * Returns the address of the notification area
642 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
643 u16 index, resource_size_t *pa)
645 u16 off = vp_modern_get_queue_notify_off(mdev, index);
647 if (mdev->notify_base) {
648 /* offset should not wrap */
649 if ((u64)off * mdev->notify_offset_multiplier + 2
650 > mdev->notify_len) {
651 dev_warn(&mdev->pci_dev->dev,
652 "bad notification offset %u (x %u) "
653 "for queue %u > %zd",
654 off, mdev->notify_offset_multiplier,
655 index, mdev->notify_len);
659 *pa = mdev->notify_pa +
660 off * mdev->notify_offset_multiplier;
661 return mdev->notify_base + off * mdev->notify_offset_multiplier;
663 return vp_modern_map_capability(mdev,
664 mdev->notify_map_cap, 2, 2,
665 off * mdev->notify_offset_multiplier, 2,
669 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
671 MODULE_VERSION("0.1");
672 MODULE_DESCRIPTION("Modern Virtio PCI Device");
673 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
674 MODULE_LICENSE("GPL");