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 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
224 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
227 if (pci_dev->device < 0x1040) {
228 /* Transitional devices: use the PCI subsystem device id as
229 * virtio device id, same as legacy driver always did.
231 mdev->id.device = pci_dev->subsystem_device;
233 /* Modern devices: simply use PCI device id, but start from 0x1040. */
234 mdev->id.device = pci_dev->device - 0x1040;
236 mdev->id.vendor = pci_dev->subsystem_vendor;
238 /* check for a common config: if not, use legacy mode (bar 0). */
239 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
240 IORESOURCE_IO | IORESOURCE_MEM,
243 dev_info(&pci_dev->dev,
244 "virtio_pci: leaving for legacy driver\n");
248 /* If common is there, these should be too... */
249 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
250 IORESOURCE_IO | IORESOURCE_MEM,
252 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
253 IORESOURCE_IO | IORESOURCE_MEM,
255 if (!isr || !notify) {
256 dev_err(&pci_dev->dev,
257 "virtio_pci: missing capabilities %i/%i/%i\n",
258 common, isr, notify);
262 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
264 err = dma_set_mask_and_coherent(&pci_dev->dev,
267 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
269 /* Device capability is only mandatory for devices that have
270 * device-specific configuration.
272 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
273 IORESOURCE_IO | IORESOURCE_MEM,
276 err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
277 "virtio-pci-modern");
282 mdev->common = vp_modern_map_capability(mdev, common,
283 sizeof(struct virtio_pci_common_cfg), 4,
284 0, sizeof(struct virtio_pci_common_cfg),
288 mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
294 /* Read notify_off_multiplier from config space. */
295 pci_read_config_dword(pci_dev,
296 notify + offsetof(struct virtio_pci_notify_cap,
297 notify_off_multiplier),
298 &mdev->notify_offset_multiplier);
299 /* Read notify length and offset from config space. */
300 pci_read_config_dword(pci_dev,
301 notify + offsetof(struct virtio_pci_notify_cap,
305 pci_read_config_dword(pci_dev,
306 notify + offsetof(struct virtio_pci_notify_cap,
310 /* We don't know how many VQs we'll map, ahead of the time.
311 * If notify length is small, map it all now.
312 * Otherwise, map each VQ individually later.
314 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
315 mdev->notify_base = vp_modern_map_capability(mdev, notify,
320 if (!mdev->notify_base)
323 mdev->notify_map_cap = notify;
326 /* Again, we don't know how much we should map, but PAGE_SIZE
327 * is more than enough for all existing devices.
330 mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
341 if (mdev->notify_base)
342 pci_iounmap(pci_dev, mdev->notify_base);
344 pci_iounmap(pci_dev, mdev->isr);
346 pci_iounmap(pci_dev, mdev->common);
348 pci_release_selected_regions(pci_dev, mdev->modern_bars);
351 EXPORT_SYMBOL_GPL(vp_modern_probe);
354 * vp_modern_remove: remove and cleanup the modern virtio pci device
355 * @mdev: the modern virtio-pci device
357 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
359 struct pci_dev *pci_dev = mdev->pci_dev;
362 pci_iounmap(pci_dev, mdev->device);
363 if (mdev->notify_base)
364 pci_iounmap(pci_dev, mdev->notify_base);
365 pci_iounmap(pci_dev, mdev->isr);
366 pci_iounmap(pci_dev, mdev->common);
367 pci_release_selected_regions(pci_dev, mdev->modern_bars);
369 EXPORT_SYMBOL_GPL(vp_modern_remove);
372 * vp_modern_get_features - get features from device
373 * @mdev: the modern virtio-pci device
375 * Returns the features read from the device
377 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
379 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
383 vp_iowrite32(0, &cfg->device_feature_select);
384 features = vp_ioread32(&cfg->device_feature);
385 vp_iowrite32(1, &cfg->device_feature_select);
386 features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
390 EXPORT_SYMBOL_GPL(vp_modern_get_features);
393 * vp_modern_get_driver_features - get driver features from device
394 * @mdev: the modern virtio-pci device
396 * Returns the driver features read from the device
398 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
400 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
404 vp_iowrite32(0, &cfg->guest_feature_select);
405 features = vp_ioread32(&cfg->guest_feature);
406 vp_iowrite32(1, &cfg->guest_feature_select);
407 features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
411 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
414 * vp_modern_set_features - set features to device
415 * @mdev: the modern virtio-pci device
416 * @features: the features set to device
418 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
421 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
423 vp_iowrite32(0, &cfg->guest_feature_select);
424 vp_iowrite32((u32)features, &cfg->guest_feature);
425 vp_iowrite32(1, &cfg->guest_feature_select);
426 vp_iowrite32(features >> 32, &cfg->guest_feature);
428 EXPORT_SYMBOL_GPL(vp_modern_set_features);
431 * vp_modern_generation - get the device genreation
432 * @mdev: the modern virtio-pci device
434 * Returns the genreation read from device
436 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
438 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
440 return vp_ioread8(&cfg->config_generation);
442 EXPORT_SYMBOL_GPL(vp_modern_generation);
445 * vp_modern_get_status - get the device status
446 * @mdev: the modern virtio-pci device
448 * Returns the status read from device
450 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
452 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
454 return vp_ioread8(&cfg->device_status);
456 EXPORT_SYMBOL_GPL(vp_modern_get_status);
459 * vp_modern_set_status - set status to device
460 * @mdev: the modern virtio-pci device
461 * @status: the status set to device
463 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
466 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
469 * Per memory-barriers.txt, wmb() is not needed to guarantee
470 * that the cache coherent memory writes have completed
471 * before writing to the MMIO region.
473 vp_iowrite8(status, &cfg->device_status);
475 EXPORT_SYMBOL_GPL(vp_modern_set_status);
478 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
479 * @mdev: the modern virtio-pci device
480 * @index: queue index
481 * @vector: the config vector
483 * Returns the config vector read from the device
485 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
486 u16 index, u16 vector)
488 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
490 vp_iowrite16(index, &cfg->queue_select);
491 vp_iowrite16(vector, &cfg->queue_msix_vector);
492 /* Flush the write out to device */
493 return vp_ioread16(&cfg->queue_msix_vector);
495 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
498 * vp_modern_config_vector - set the vector for config interrupt
499 * @mdev: the modern virtio-pci device
500 * @vector: the config vector
502 * Returns the config vector read from the device
504 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
507 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
509 /* Setup the vector used for configuration events */
510 vp_iowrite16(vector, &cfg->msix_config);
511 /* Verify we had enough resources to assign the vector */
512 /* Will also flush the write out to device */
513 return vp_ioread16(&cfg->msix_config);
515 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
518 * vp_modern_queue_address - set the virtqueue address
519 * @mdev: the modern virtio-pci device
520 * @index: the queue index
521 * @desc_addr: address of the descriptor area
522 * @driver_addr: address of the driver area
523 * @device_addr: address of the device area
525 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
526 u16 index, u64 desc_addr, u64 driver_addr,
529 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
531 vp_iowrite16(index, &cfg->queue_select);
533 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
534 &cfg->queue_desc_hi);
535 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
536 &cfg->queue_avail_hi);
537 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
538 &cfg->queue_used_hi);
540 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
543 * vp_modern_set_queue_enable - enable a virtqueue
544 * @mdev: the modern virtio-pci device
545 * @index: the queue index
546 * @enable: whether the virtqueue is enable or not
548 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
549 u16 index, bool enable)
551 vp_iowrite16(index, &mdev->common->queue_select);
552 vp_iowrite16(enable, &mdev->common->queue_enable);
554 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
557 * vp_modern_get_queue_enable - enable a virtqueue
558 * @mdev: the modern virtio-pci device
559 * @index: the queue index
561 * Returns whether a virtqueue is enabled or not
563 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
566 vp_iowrite16(index, &mdev->common->queue_select);
568 return vp_ioread16(&mdev->common->queue_enable);
570 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
573 * vp_modern_set_queue_size - set size for a virtqueue
574 * @mdev: the modern virtio-pci device
575 * @index: the queue index
576 * @size: the size of the virtqueue
578 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
581 vp_iowrite16(index, &mdev->common->queue_select);
582 vp_iowrite16(size, &mdev->common->queue_size);
585 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
588 * vp_modern_get_queue_size - get size for a virtqueue
589 * @mdev: the modern virtio-pci device
590 * @index: the queue index
592 * Returns the size of the virtqueue
594 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
597 vp_iowrite16(index, &mdev->common->queue_select);
599 return vp_ioread16(&mdev->common->queue_size);
602 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
605 * vp_modern_get_num_queues - get the number of virtqueues
606 * @mdev: the modern virtio-pci device
608 * Returns the number of virtqueues
610 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
612 return vp_ioread16(&mdev->common->num_queues);
614 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
617 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
618 * @mdev: the modern virtio-pci device
619 * @index: the queue index
621 * Returns the notification offset for a virtqueue
623 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
626 vp_iowrite16(index, &mdev->common->queue_select);
628 return vp_ioread16(&mdev->common->queue_notify_off);
632 * vp_modern_map_vq_notify - map notification area for a
634 * @mdev: the modern virtio-pci device
635 * @index: the queue index
636 * @pa: the pointer to the physical address of the nofity area
638 * Returns the address of the notification area
640 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
641 u16 index, resource_size_t *pa)
643 u16 off = vp_modern_get_queue_notify_off(mdev, index);
645 if (mdev->notify_base) {
646 /* offset should not wrap */
647 if ((u64)off * mdev->notify_offset_multiplier + 2
648 > mdev->notify_len) {
649 dev_warn(&mdev->pci_dev->dev,
650 "bad notification offset %u (x %u) "
651 "for queue %u > %zd",
652 off, mdev->notify_offset_multiplier,
653 index, mdev->notify_len);
657 *pa = mdev->notify_pa +
658 off * mdev->notify_offset_multiplier;
659 return mdev->notify_base + off * mdev->notify_offset_multiplier;
661 return vp_modern_map_capability(mdev,
662 mdev->notify_map_cap, 2, 2,
663 off * mdev->notify_offset_multiplier, 2,
667 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
669 MODULE_VERSION("0.1");
670 MODULE_DESCRIPTION("Modern Virtio PCI Device");
671 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
672 MODULE_LICENSE("GPL");