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_set_features - set features to device
388 * @mdev: the modern virtio-pci device
389 * @features: the features set to device
391 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
394 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
396 vp_iowrite32(0, &cfg->guest_feature_select);
397 vp_iowrite32((u32)features, &cfg->guest_feature);
398 vp_iowrite32(1, &cfg->guest_feature_select);
399 vp_iowrite32(features >> 32, &cfg->guest_feature);
401 EXPORT_SYMBOL_GPL(vp_modern_set_features);
404 * vp_modern_generation - get the device genreation
405 * @mdev: the modern virtio-pci device
407 * Returns the genreation read from device
409 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
411 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
413 return vp_ioread8(&cfg->config_generation);
415 EXPORT_SYMBOL_GPL(vp_modern_generation);
418 * vp_modern_get_status - get the device status
419 * @mdev: the modern virtio-pci device
421 * Returns the status read from device
423 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
425 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
427 return vp_ioread8(&cfg->device_status);
429 EXPORT_SYMBOL_GPL(vp_modern_get_status);
432 * vp_modern_set_status - set status to device
433 * @mdev: the modern virtio-pci device
434 * @status: the status set to device
436 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
439 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
441 vp_iowrite8(status, &cfg->device_status);
443 EXPORT_SYMBOL_GPL(vp_modern_set_status);
446 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
447 * @mdev: the modern virtio-pci device
448 * @index: queue index
449 * @vector: the config vector
451 * Returns the config vector read from the device
453 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
454 u16 index, u16 vector)
456 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
458 vp_iowrite16(index, &cfg->queue_select);
459 vp_iowrite16(vector, &cfg->queue_msix_vector);
460 /* Flush the write out to device */
461 return vp_ioread16(&cfg->queue_msix_vector);
463 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
466 * vp_modern_config_vector - set the vector for config interrupt
467 * @mdev: the modern virtio-pci device
468 * @vector: the config vector
470 * Returns the config vector read from the device
472 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
475 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
477 /* Setup the vector used for configuration events */
478 vp_iowrite16(vector, &cfg->msix_config);
479 /* Verify we had enough resources to assign the vector */
480 /* Will also flush the write out to device */
481 return vp_ioread16(&cfg->msix_config);
483 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
486 * vp_modern_queue_address - set the virtqueue address
487 * @mdev: the modern virtio-pci device
488 * @index: the queue index
489 * @desc_addr: address of the descriptor area
490 * @driver_addr: address of the driver area
491 * @device_addr: address of the device area
493 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
494 u16 index, u64 desc_addr, u64 driver_addr,
497 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
499 vp_iowrite16(index, &cfg->queue_select);
501 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
502 &cfg->queue_desc_hi);
503 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
504 &cfg->queue_avail_hi);
505 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
506 &cfg->queue_used_hi);
508 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
511 * vp_modern_set_queue_enable - enable a virtqueue
512 * @mdev: the modern virtio-pci device
513 * @index: the queue index
514 * @enable: whether the virtqueue is enable or not
516 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
517 u16 index, bool enable)
519 vp_iowrite16(index, &mdev->common->queue_select);
520 vp_iowrite16(enable, &mdev->common->queue_enable);
522 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
525 * vp_modern_get_queue_enable - enable a virtqueue
526 * @mdev: the modern virtio-pci device
527 * @index: the queue index
529 * Returns whether a virtqueue is enabled or not
531 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
534 vp_iowrite16(index, &mdev->common->queue_select);
536 return vp_ioread16(&mdev->common->queue_enable);
538 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
541 * vp_modern_set_queue_size - set size for a virtqueue
542 * @mdev: the modern virtio-pci device
543 * @index: the queue index
544 * @size: the size of the virtqueue
546 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
549 vp_iowrite16(index, &mdev->common->queue_select);
550 vp_iowrite16(size, &mdev->common->queue_size);
553 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
556 * vp_modern_get_queue_size - get size for a virtqueue
557 * @mdev: the modern virtio-pci device
558 * @index: the queue index
560 * Returns the size of the virtqueue
562 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
565 vp_iowrite16(index, &mdev->common->queue_select);
567 return vp_ioread16(&mdev->common->queue_size);
570 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
573 * vp_modern_get_num_queues - get the number of virtqueues
574 * @mdev: the modern virtio-pci device
576 * Returns the number of virtqueues
578 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
580 return vp_ioread16(&mdev->common->num_queues);
582 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
585 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
586 * @mdev: the modern virtio-pci device
587 * @index: the queue index
589 * Returns the notification offset for a virtqueue
591 static u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
594 vp_iowrite16(index, &mdev->common->queue_select);
596 return vp_ioread16(&mdev->common->queue_notify_off);
600 * vp_modern_map_vq_notify - map notification area for a
602 * @mdev: the modern virtio-pci device
603 * @index: the queue index
604 * @pa: the pointer to the physical address of the nofity area
606 * Returns the address of the notification area
608 void __iomem *vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
609 u16 index, resource_size_t *pa)
611 u16 off = vp_modern_get_queue_notify_off(mdev, index);
613 if (mdev->notify_base) {
614 /* offset should not wrap */
615 if ((u64)off * mdev->notify_offset_multiplier + 2
616 > mdev->notify_len) {
617 dev_warn(&mdev->pci_dev->dev,
618 "bad notification offset %u (x %u) "
619 "for queue %u > %zd",
620 off, mdev->notify_offset_multiplier,
621 index, mdev->notify_len);
625 *pa = mdev->notify_pa +
626 off * mdev->notify_offset_multiplier;
627 return mdev->notify_base + off * mdev->notify_offset_multiplier;
629 return vp_modern_map_capability(mdev,
630 mdev->notify_map_cap, 2, 2,
631 off * mdev->notify_offset_multiplier, 2,
635 EXPORT_SYMBOL_GPL(vp_modern_map_vq_notify);
637 MODULE_VERSION("0.1");
638 MODULE_DESCRIPTION("Modern Virtio PCI Device");
639 MODULE_AUTHOR("Jason Wang <jasowang@redhat.com>");
640 MODULE_LICENSE("GPL");