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 if (length <= start) {
40 "virtio_pci: bad capability len %u (>%u expected)\n",
45 if (length - start < minlen) {
47 "virtio_pci: bad capability len %u (>=%zu expected)\n",
54 if (start + offset < offset) {
56 "virtio_pci: map wrap-around %u+%u\n",
63 if (offset & (align - 1)) {
65 "virtio_pci: offset %u not aligned to %u\n",
76 if (minlen + offset < minlen ||
77 minlen + offset > pci_resource_len(dev, bar)) {
79 "virtio_pci: map virtio %zu@%u "
80 "out of range on bar %i length %lu\n",
82 bar, (unsigned long)pci_resource_len(dev, bar));
86 p = pci_iomap_range(dev, bar, offset, length);
89 "virtio_pci: unable to map virtio %u@%u on bar %i\n",
92 *pa = pci_resource_start(dev, bar) + offset;
98 * virtio_pci_find_capability - walk capabilities to find device info.
99 * @dev: the pci device
100 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
101 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
102 * @bars: the bitmask of BARs
104 * Returns offset of the capability, or 0.
106 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
107 u32 ioresource_types, int *bars)
111 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
113 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
115 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
118 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
122 /* Ignore structures with reserved BAR values */
126 if (type == cfg_type) {
127 if (pci_resource_len(dev, bar) &&
128 pci_resource_flags(dev, bar) & ioresource_types) {
137 /* This is part of the ABI. Don't screw with it. */
138 static inline void check_offsets(void)
140 /* Note: disk space was harmed in compilation of this function. */
141 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
142 offsetof(struct virtio_pci_cap, cap_vndr));
143 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
144 offsetof(struct virtio_pci_cap, cap_next));
145 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
146 offsetof(struct virtio_pci_cap, cap_len));
147 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
148 offsetof(struct virtio_pci_cap, cfg_type));
149 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
150 offsetof(struct virtio_pci_cap, bar));
151 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
152 offsetof(struct virtio_pci_cap, offset));
153 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
154 offsetof(struct virtio_pci_cap, length));
155 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
156 offsetof(struct virtio_pci_notify_cap,
157 notify_off_multiplier));
158 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
159 offsetof(struct virtio_pci_common_cfg,
160 device_feature_select));
161 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
162 offsetof(struct virtio_pci_common_cfg, device_feature));
163 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
164 offsetof(struct virtio_pci_common_cfg,
165 guest_feature_select));
166 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
167 offsetof(struct virtio_pci_common_cfg, guest_feature));
168 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
169 offsetof(struct virtio_pci_common_cfg, msix_config));
170 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
171 offsetof(struct virtio_pci_common_cfg, num_queues));
172 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
173 offsetof(struct virtio_pci_common_cfg, device_status));
174 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
175 offsetof(struct virtio_pci_common_cfg, config_generation));
176 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
177 offsetof(struct virtio_pci_common_cfg, queue_select));
178 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
179 offsetof(struct virtio_pci_common_cfg, queue_size));
180 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
181 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
182 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
183 offsetof(struct virtio_pci_common_cfg, queue_enable));
184 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
185 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
186 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
187 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
188 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
189 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
190 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
191 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
192 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
193 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
194 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
195 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
196 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
197 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
201 * vp_modern_probe: probe the modern virtio pci device, note that the
202 * caller is required to enable PCI device before calling this function.
203 * @mdev: the modern virtio-pci device
205 * Return 0 on succeed otherwise fail
207 int vp_modern_probe(struct virtio_pci_modern_device *mdev)
209 struct pci_dev *pci_dev = mdev->pci_dev;
210 int err, common, isr, notify, device;
216 mdev->pci_dev = pci_dev;
218 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
219 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
222 if (pci_dev->device < 0x1040) {
223 /* Transitional devices: use the PCI subsystem device id as
224 * virtio device id, same as legacy driver always did.
226 mdev->id.device = pci_dev->subsystem_device;
228 /* Modern devices: simply use PCI device id, but start from 0x1040. */
229 mdev->id.device = pci_dev->device - 0x1040;
231 mdev->id.vendor = pci_dev->subsystem_vendor;
233 /* check for a common config: if not, use legacy mode (bar 0). */
234 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
235 IORESOURCE_IO | IORESOURCE_MEM,
238 dev_info(&pci_dev->dev,
239 "virtio_pci: leaving for legacy driver\n");
243 /* If common is there, these should be too... */
244 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
245 IORESOURCE_IO | IORESOURCE_MEM,
247 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
248 IORESOURCE_IO | IORESOURCE_MEM,
250 if (!isr || !notify) {
251 dev_err(&pci_dev->dev,
252 "virtio_pci: missing capabilities %i/%i/%i\n",
253 common, isr, notify);
257 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
259 err = dma_set_mask_and_coherent(&pci_dev->dev,
262 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
264 /* Device capability is only mandatory for devices that have
265 * device-specific configuration.
267 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
268 IORESOURCE_IO | IORESOURCE_MEM,
271 err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
272 "virtio-pci-modern");
277 mdev->common = vp_modern_map_capability(mdev, common,
278 sizeof(struct virtio_pci_common_cfg), 4,
279 0, sizeof(struct virtio_pci_common_cfg),
283 mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
289 /* Read notify_off_multiplier from config space. */
290 pci_read_config_dword(pci_dev,
291 notify + offsetof(struct virtio_pci_notify_cap,
292 notify_off_multiplier),
293 &mdev->notify_offset_multiplier);
294 /* Read notify length and offset from config space. */
295 pci_read_config_dword(pci_dev,
296 notify + offsetof(struct virtio_pci_notify_cap,
300 pci_read_config_dword(pci_dev,
301 notify + offsetof(struct virtio_pci_notify_cap,
305 /* We don't know how many VQs we'll map, ahead of the time.
306 * If notify length is small, map it all now.
307 * Otherwise, map each VQ individually later.
309 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
310 mdev->notify_base = vp_modern_map_capability(mdev, notify,
315 if (!mdev->notify_base)
318 mdev->notify_map_cap = notify;
321 /* Again, we don't know how much we should map, but PAGE_SIZE
322 * is more than enough for all existing devices.
325 mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
336 if (mdev->notify_base)
337 pci_iounmap(pci_dev, mdev->notify_base);
339 pci_iounmap(pci_dev, mdev->isr);
341 pci_iounmap(pci_dev, mdev->common);
343 pci_release_selected_regions(pci_dev, mdev->modern_bars);
346 EXPORT_SYMBOL_GPL(vp_modern_probe);
349 * vp_modern_probe: remove and cleanup the modern virtio pci device
350 * @mdev: the modern virtio-pci device
352 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
354 struct pci_dev *pci_dev = mdev->pci_dev;
357 pci_iounmap(pci_dev, mdev->device);
358 if (mdev->notify_base)
359 pci_iounmap(pci_dev, mdev->notify_base);
360 pci_iounmap(pci_dev, mdev->isr);
361 pci_iounmap(pci_dev, mdev->common);
362 pci_release_selected_regions(pci_dev, mdev->modern_bars);
364 EXPORT_SYMBOL_GPL(vp_modern_remove);
367 * vp_modern_get_features - get features from device
368 * @mdev: the modern virtio-pci device
370 * Returns the features read from the device
372 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
374 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
378 vp_iowrite32(0, &cfg->device_feature_select);
379 features = vp_ioread32(&cfg->device_feature);
380 vp_iowrite32(1, &cfg->device_feature_select);
381 features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
385 EXPORT_SYMBOL_GPL(vp_modern_get_features);
388 * vp_modern_get_driver_features - get driver features from device
389 * @mdev: the modern virtio-pci device
391 * Returns the driver features read from the device
393 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
395 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
399 vp_iowrite32(0, &cfg->guest_feature_select);
400 features = vp_ioread32(&cfg->guest_feature);
401 vp_iowrite32(1, &cfg->guest_feature_select);
402 features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
406 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
409 * vp_modern_set_features - set features to device
410 * @mdev: the modern virtio-pci device
411 * @features: the features set to device
413 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
416 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
418 vp_iowrite32(0, &cfg->guest_feature_select);
419 vp_iowrite32((u32)features, &cfg->guest_feature);
420 vp_iowrite32(1, &cfg->guest_feature_select);
421 vp_iowrite32(features >> 32, &cfg->guest_feature);
423 EXPORT_SYMBOL_GPL(vp_modern_set_features);
426 * vp_modern_generation - get the device genreation
427 * @mdev: the modern virtio-pci device
429 * Returns the genreation read from device
431 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
433 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
435 return vp_ioread8(&cfg->config_generation);
437 EXPORT_SYMBOL_GPL(vp_modern_generation);
440 * vp_modern_get_status - get the device status
441 * @mdev: the modern virtio-pci device
443 * Returns the status read from device
445 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
447 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
449 return vp_ioread8(&cfg->device_status);
451 EXPORT_SYMBOL_GPL(vp_modern_get_status);
454 * vp_modern_set_status - set status to device
455 * @mdev: the modern virtio-pci device
456 * @status: the status set to device
458 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
461 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
463 vp_iowrite8(status, &cfg->device_status);
465 EXPORT_SYMBOL_GPL(vp_modern_set_status);
468 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
469 * @mdev: the modern virtio-pci device
470 * @index: queue index
471 * @vector: the config vector
473 * Returns the config vector read from the device
475 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
476 u16 index, u16 vector)
478 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
480 vp_iowrite16(index, &cfg->queue_select);
481 vp_iowrite16(vector, &cfg->queue_msix_vector);
482 /* Flush the write out to device */
483 return vp_ioread16(&cfg->queue_msix_vector);
485 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
488 * vp_modern_config_vector - set the vector for config interrupt
489 * @mdev: the modern virtio-pci device
490 * @vector: the config vector
492 * Returns the config vector read from the device
494 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
497 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
499 /* Setup the vector used for configuration events */
500 vp_iowrite16(vector, &cfg->msix_config);
501 /* Verify we had enough resources to assign the vector */
502 /* Will also flush the write out to device */
503 return vp_ioread16(&cfg->msix_config);
505 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
508 * vp_modern_queue_address - set the virtqueue address
509 * @mdev: the modern virtio-pci device
510 * @index: the queue index
511 * @desc_addr: address of the descriptor area
512 * @driver_addr: address of the driver area
513 * @device_addr: address of the device area
515 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
516 u16 index, u64 desc_addr, u64 driver_addr,
519 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
521 vp_iowrite16(index, &cfg->queue_select);
523 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
524 &cfg->queue_desc_hi);
525 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
526 &cfg->queue_avail_hi);
527 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
528 &cfg->queue_used_hi);
530 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
533 * vp_modern_set_queue_enable - enable a virtqueue
534 * @mdev: the modern virtio-pci device
535 * @index: the queue index
536 * @enable: whether the virtqueue is enable or not
538 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
539 u16 index, bool enable)
541 vp_iowrite16(index, &mdev->common->queue_select);
542 vp_iowrite16(enable, &mdev->common->queue_enable);
544 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
547 * vp_modern_get_queue_enable - enable a virtqueue
548 * @mdev: the modern virtio-pci device
549 * @index: the queue index
551 * Returns whether a virtqueue is enabled or not
553 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
556 vp_iowrite16(index, &mdev->common->queue_select);
558 return vp_ioread16(&mdev->common->queue_enable);
560 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
563 * vp_modern_set_queue_size - set size for a virtqueue
564 * @mdev: the modern virtio-pci device
565 * @index: the queue index
566 * @size: the size of the virtqueue
568 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
571 vp_iowrite16(index, &mdev->common->queue_select);
572 vp_iowrite16(size, &mdev->common->queue_size);
575 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
578 * vp_modern_get_queue_size - get size for a virtqueue
579 * @mdev: the modern virtio-pci device
580 * @index: the queue index
582 * Returns the size of the virtqueue
584 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
587 vp_iowrite16(index, &mdev->common->queue_select);
589 return vp_ioread16(&mdev->common->queue_size);
592 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
595 * vp_modern_get_num_queues - get the number of virtqueues
596 * @mdev: the modern virtio-pci device
598 * Returns the number of virtqueues
600 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
602 return vp_ioread16(&mdev->common->num_queues);
604 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
607 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
608 * @mdev: the modern virtio-pci device
609 * @index: the queue index
611 * Returns the notification offset for a virtqueue
613 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
616 vp_iowrite16(index, &mdev->common->queue_select);
618 return vp_ioread16(&mdev->common->queue_notify_off);
622 * vp_modern_map_vq_notify - map notification area for a
624 * @mdev: the modern virtio-pci device
625 * @index: the queue index
626 * @pa: the pointer to the physical address of the nofity area
628 * Returns the address of the notification area
630 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
631 u16 index, resource_size_t *pa)
633 u16 off = vp_modern_get_queue_notify_off(mdev, index);
635 if (mdev->notify_base) {
636 /* offset should not wrap */
637 if ((u64)off * mdev->notify_offset_multiplier + 2
638 > mdev->notify_len) {
639 dev_warn(&mdev->pci_dev->dev,
640 "bad notification offset %u (x %u) "
641 "for queue %u > %zd",
642 off, mdev->notify_offset_multiplier,
643 index, mdev->notify_len);
647 *pa = mdev->notify_pa +
648 off * mdev->notify_offset_multiplier;
649 return mdev->notify_base + off * mdev->notify_offset_multiplier;
651 return vp_modern_map_capability(mdev,
652 mdev->notify_map_cap, 2, 2,
653 off * mdev->notify_offset_multiplier, 2,
657 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
659 MODULE_VERSION("0.1");
660 MODULE_DESCRIPTION("Modern Virtio PCI Device");
661 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
662 MODULE_LICENSE("GPL");