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);
345 EXPORT_SYMBOL_GPL(vp_modern_probe);
348 * vp_modern_probe: remove and cleanup the modern virtio pci device
349 * @mdev: the modern virtio-pci device
351 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
353 struct pci_dev *pci_dev = mdev->pci_dev;
356 pci_iounmap(pci_dev, mdev->device);
357 if (mdev->notify_base)
358 pci_iounmap(pci_dev, mdev->notify_base);
359 pci_iounmap(pci_dev, mdev->isr);
360 pci_iounmap(pci_dev, mdev->common);
361 pci_release_selected_regions(pci_dev, mdev->modern_bars);
363 EXPORT_SYMBOL_GPL(vp_modern_remove);
366 * vp_modern_get_features - get features from device
367 * @mdev: the modern virtio-pci device
369 * Returns the features read from the device
371 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
373 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
377 vp_iowrite32(0, &cfg->device_feature_select);
378 features = vp_ioread32(&cfg->device_feature);
379 vp_iowrite32(1, &cfg->device_feature_select);
380 features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
384 EXPORT_SYMBOL_GPL(vp_modern_get_features);
387 * vp_modern_get_driver_features - get driver features from device
388 * @mdev: the modern virtio-pci device
390 * Returns the driver features read from the device
392 u64 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
394 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
398 vp_iowrite32(0, &cfg->guest_feature_select);
399 features = vp_ioread32(&cfg->guest_feature);
400 vp_iowrite32(1, &cfg->guest_feature_select);
401 features |= ((u64)vp_ioread32(&cfg->guest_feature) << 32);
405 EXPORT_SYMBOL_GPL(vp_modern_get_driver_features);
408 * vp_modern_set_features - set features to device
409 * @mdev: the modern virtio-pci device
410 * @features: the features set to device
412 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
415 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
417 vp_iowrite32(0, &cfg->guest_feature_select);
418 vp_iowrite32((u32)features, &cfg->guest_feature);
419 vp_iowrite32(1, &cfg->guest_feature_select);
420 vp_iowrite32(features >> 32, &cfg->guest_feature);
422 EXPORT_SYMBOL_GPL(vp_modern_set_features);
425 * vp_modern_generation - get the device genreation
426 * @mdev: the modern virtio-pci device
428 * Returns the genreation read from device
430 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
432 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
434 return vp_ioread8(&cfg->config_generation);
436 EXPORT_SYMBOL_GPL(vp_modern_generation);
439 * vp_modern_get_status - get the device status
440 * @mdev: the modern virtio-pci device
442 * Returns the status read from device
444 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
446 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
448 return vp_ioread8(&cfg->device_status);
450 EXPORT_SYMBOL_GPL(vp_modern_get_status);
453 * vp_modern_set_status - set status to device
454 * @mdev: the modern virtio-pci device
455 * @status: the status set to device
457 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
460 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
462 vp_iowrite8(status, &cfg->device_status);
464 EXPORT_SYMBOL_GPL(vp_modern_set_status);
467 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
468 * @mdev: the modern virtio-pci device
469 * @index: queue index
470 * @vector: the config vector
472 * Returns the config vector read from the device
474 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
475 u16 index, u16 vector)
477 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
479 vp_iowrite16(index, &cfg->queue_select);
480 vp_iowrite16(vector, &cfg->queue_msix_vector);
481 /* Flush the write out to device */
482 return vp_ioread16(&cfg->queue_msix_vector);
484 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
487 * vp_modern_config_vector - set the vector for config interrupt
488 * @mdev: the modern virtio-pci device
489 * @vector: the config vector
491 * Returns the config vector read from the device
493 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
496 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
498 /* Setup the vector used for configuration events */
499 vp_iowrite16(vector, &cfg->msix_config);
500 /* Verify we had enough resources to assign the vector */
501 /* Will also flush the write out to device */
502 return vp_ioread16(&cfg->msix_config);
504 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
507 * vp_modern_queue_address - set the virtqueue address
508 * @mdev: the modern virtio-pci device
509 * @index: the queue index
510 * @desc_addr: address of the descriptor area
511 * @driver_addr: address of the driver area
512 * @device_addr: address of the device area
514 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
515 u16 index, u64 desc_addr, u64 driver_addr,
518 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
520 vp_iowrite16(index, &cfg->queue_select);
522 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
523 &cfg->queue_desc_hi);
524 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
525 &cfg->queue_avail_hi);
526 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
527 &cfg->queue_used_hi);
529 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
532 * vp_modern_set_queue_enable - enable a virtqueue
533 * @mdev: the modern virtio-pci device
534 * @index: the queue index
535 * @enable: whether the virtqueue is enable or not
537 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
538 u16 index, bool enable)
540 vp_iowrite16(index, &mdev->common->queue_select);
541 vp_iowrite16(enable, &mdev->common->queue_enable);
543 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
546 * vp_modern_get_queue_enable - enable a virtqueue
547 * @mdev: the modern virtio-pci device
548 * @index: the queue index
550 * Returns whether a virtqueue is enabled or not
552 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
555 vp_iowrite16(index, &mdev->common->queue_select);
557 return vp_ioread16(&mdev->common->queue_enable);
559 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
562 * vp_modern_set_queue_size - set size for a virtqueue
563 * @mdev: the modern virtio-pci device
564 * @index: the queue index
565 * @size: the size of the virtqueue
567 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
570 vp_iowrite16(index, &mdev->common->queue_select);
571 vp_iowrite16(size, &mdev->common->queue_size);
574 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
577 * vp_modern_get_queue_size - get size for a virtqueue
578 * @mdev: the modern virtio-pci device
579 * @index: the queue index
581 * Returns the size of the virtqueue
583 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
586 vp_iowrite16(index, &mdev->common->queue_select);
588 return vp_ioread16(&mdev->common->queue_size);
591 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
594 * vp_modern_get_num_queues - get the number of virtqueues
595 * @mdev: the modern virtio-pci device
597 * Returns the number of virtqueues
599 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
601 return vp_ioread16(&mdev->common->num_queues);
603 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
606 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
607 * @mdev: the modern virtio-pci device
608 * @index: the queue index
610 * Returns the notification offset for a virtqueue
612 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
615 vp_iowrite16(index, &mdev->common->queue_select);
617 return vp_ioread16(&mdev->common->queue_notify_off);
621 * vp_modern_map_vq_notify - map notification area for a
623 * @mdev: the modern virtio-pci device
624 * @index: the queue index
625 * @pa: the pointer to the physical address of the nofity area
627 * Returns the address of the notification area
629 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
630 u16 index, resource_size_t *pa)
632 u16 off = vp_modern_get_queue_notify_off(mdev, index);
634 if (mdev->notify_base) {
635 /* offset should not wrap */
636 if ((u64)off * mdev->notify_offset_multiplier + 2
637 > mdev->notify_len) {
638 dev_warn(&mdev->pci_dev->dev,
639 "bad notification offset %u (x %u) "
640 "for queue %u > %zd",
641 off, mdev->notify_offset_multiplier,
642 index, mdev->notify_len);
646 *pa = mdev->notify_pa +
647 off * mdev->notify_offset_multiplier;
648 return mdev->notify_base + off * mdev->notify_offset_multiplier;
650 return vp_modern_map_capability(mdev,
651 mdev->notify_map_cap, 2, 2,
652 off * mdev->notify_offset_multiplier, 2,
656 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
658 MODULE_VERSION("0.1");
659 MODULE_DESCRIPTION("Modern Virtio PCI Device");
660 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
661 MODULE_LICENSE("GPL");