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
17 * Returns the io address of for the part of the capability
19 void __iomem *vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off,
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",
93 EXPORT_SYMBOL_GPL(vp_modern_map_capability);
96 * virtio_pci_find_capability - walk capabilities to find device info.
97 * @dev: the pci device
98 * @cfg_type: the VIRTIO_PCI_CAP_* value we seek
99 * @ioresource_types: IORESOURCE_MEM and/or IORESOURCE_IO.
100 * @bars: the bitmask of BARs
102 * Returns offset of the capability, or 0.
104 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
105 u32 ioresource_types, int *bars)
109 for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
111 pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
113 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
116 pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
120 /* Ignore structures with reserved BAR values */
124 if (type == cfg_type) {
125 if (pci_resource_len(dev, bar) &&
126 pci_resource_flags(dev, bar) & ioresource_types) {
135 /* This is part of the ABI. Don't screw with it. */
136 static inline void check_offsets(void)
138 /* Note: disk space was harmed in compilation of this function. */
139 BUILD_BUG_ON(VIRTIO_PCI_CAP_VNDR !=
140 offsetof(struct virtio_pci_cap, cap_vndr));
141 BUILD_BUG_ON(VIRTIO_PCI_CAP_NEXT !=
142 offsetof(struct virtio_pci_cap, cap_next));
143 BUILD_BUG_ON(VIRTIO_PCI_CAP_LEN !=
144 offsetof(struct virtio_pci_cap, cap_len));
145 BUILD_BUG_ON(VIRTIO_PCI_CAP_CFG_TYPE !=
146 offsetof(struct virtio_pci_cap, cfg_type));
147 BUILD_BUG_ON(VIRTIO_PCI_CAP_BAR !=
148 offsetof(struct virtio_pci_cap, bar));
149 BUILD_BUG_ON(VIRTIO_PCI_CAP_OFFSET !=
150 offsetof(struct virtio_pci_cap, offset));
151 BUILD_BUG_ON(VIRTIO_PCI_CAP_LENGTH !=
152 offsetof(struct virtio_pci_cap, length));
153 BUILD_BUG_ON(VIRTIO_PCI_NOTIFY_CAP_MULT !=
154 offsetof(struct virtio_pci_notify_cap,
155 notify_off_multiplier));
156 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DFSELECT !=
157 offsetof(struct virtio_pci_common_cfg,
158 device_feature_select));
159 BUILD_BUG_ON(VIRTIO_PCI_COMMON_DF !=
160 offsetof(struct virtio_pci_common_cfg, device_feature));
161 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GFSELECT !=
162 offsetof(struct virtio_pci_common_cfg,
163 guest_feature_select));
164 BUILD_BUG_ON(VIRTIO_PCI_COMMON_GF !=
165 offsetof(struct virtio_pci_common_cfg, guest_feature));
166 BUILD_BUG_ON(VIRTIO_PCI_COMMON_MSIX !=
167 offsetof(struct virtio_pci_common_cfg, msix_config));
168 BUILD_BUG_ON(VIRTIO_PCI_COMMON_NUMQ !=
169 offsetof(struct virtio_pci_common_cfg, num_queues));
170 BUILD_BUG_ON(VIRTIO_PCI_COMMON_STATUS !=
171 offsetof(struct virtio_pci_common_cfg, device_status));
172 BUILD_BUG_ON(VIRTIO_PCI_COMMON_CFGGENERATION !=
173 offsetof(struct virtio_pci_common_cfg, config_generation));
174 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SELECT !=
175 offsetof(struct virtio_pci_common_cfg, queue_select));
176 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_SIZE !=
177 offsetof(struct virtio_pci_common_cfg, queue_size));
178 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_MSIX !=
179 offsetof(struct virtio_pci_common_cfg, queue_msix_vector));
180 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_ENABLE !=
181 offsetof(struct virtio_pci_common_cfg, queue_enable));
182 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_NOFF !=
183 offsetof(struct virtio_pci_common_cfg, queue_notify_off));
184 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCLO !=
185 offsetof(struct virtio_pci_common_cfg, queue_desc_lo));
186 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_DESCHI !=
187 offsetof(struct virtio_pci_common_cfg, queue_desc_hi));
188 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILLO !=
189 offsetof(struct virtio_pci_common_cfg, queue_avail_lo));
190 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_AVAILHI !=
191 offsetof(struct virtio_pci_common_cfg, queue_avail_hi));
192 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDLO !=
193 offsetof(struct virtio_pci_common_cfg, queue_used_lo));
194 BUILD_BUG_ON(VIRTIO_PCI_COMMON_Q_USEDHI !=
195 offsetof(struct virtio_pci_common_cfg, queue_used_hi));
199 * vp_modern_probe: probe the modern virtio pci device, note that the
200 * caller is required to enable PCI device before calling this function.
201 * @mdev: the modern virtio-pci device
203 * Return 0 on succeed otherwise fail
205 int vp_modern_probe(struct virtio_pci_modern_device *mdev)
207 struct pci_dev *pci_dev = mdev->pci_dev;
208 int err, common, isr, notify, device;
214 mdev->pci_dev = pci_dev;
216 /* We only own devices >= 0x1000 and <= 0x107f: leave the rest. */
217 if (pci_dev->device < 0x1000 || pci_dev->device > 0x107f)
220 if (pci_dev->device < 0x1040) {
221 /* Transitional devices: use the PCI subsystem device id as
222 * virtio device id, same as legacy driver always did.
224 mdev->id.device = pci_dev->subsystem_device;
226 /* Modern devices: simply use PCI device id, but start from 0x1040. */
227 mdev->id.device = pci_dev->device - 0x1040;
229 mdev->id.vendor = pci_dev->subsystem_vendor;
231 /* check for a common config: if not, use legacy mode (bar 0). */
232 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG,
233 IORESOURCE_IO | IORESOURCE_MEM,
236 dev_info(&pci_dev->dev,
237 "virtio_pci: leaving for legacy driver\n");
241 /* If common is there, these should be too... */
242 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG,
243 IORESOURCE_IO | IORESOURCE_MEM,
245 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG,
246 IORESOURCE_IO | IORESOURCE_MEM,
248 if (!isr || !notify) {
249 dev_err(&pci_dev->dev,
250 "virtio_pci: missing capabilities %i/%i/%i\n",
251 common, isr, notify);
255 err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64));
257 err = dma_set_mask_and_coherent(&pci_dev->dev,
260 dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
262 /* Device capability is only mandatory for devices that have
263 * device-specific configuration.
265 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG,
266 IORESOURCE_IO | IORESOURCE_MEM,
269 err = pci_request_selected_regions(pci_dev, mdev->modern_bars,
270 "virtio-pci-modern");
275 mdev->common = vp_modern_map_capability(mdev, common,
276 sizeof(struct virtio_pci_common_cfg), 4,
277 0, sizeof(struct virtio_pci_common_cfg),
281 mdev->isr = vp_modern_map_capability(mdev, isr, sizeof(u8), 1,
287 /* Read notify_off_multiplier from config space. */
288 pci_read_config_dword(pci_dev,
289 notify + offsetof(struct virtio_pci_notify_cap,
290 notify_off_multiplier),
291 &mdev->notify_offset_multiplier);
292 /* Read notify length and offset from config space. */
293 pci_read_config_dword(pci_dev,
294 notify + offsetof(struct virtio_pci_notify_cap,
298 pci_read_config_dword(pci_dev,
299 notify + offsetof(struct virtio_pci_notify_cap,
303 /* We don't know how many VQs we'll map, ahead of the time.
304 * If notify length is small, map it all now.
305 * Otherwise, map each VQ individually later.
307 if ((u64)notify_length + (notify_offset % PAGE_SIZE) <= PAGE_SIZE) {
308 mdev->notify_base = vp_modern_map_capability(mdev, notify,
312 if (!mdev->notify_base)
315 mdev->notify_map_cap = notify;
318 /* Again, we don't know how much we should map, but PAGE_SIZE
319 * is more than enough for all existing devices.
322 mdev->device = vp_modern_map_capability(mdev, device, 0, 4,
332 if (mdev->notify_base)
333 pci_iounmap(pci_dev, mdev->notify_base);
335 pci_iounmap(pci_dev, mdev->isr);
337 pci_iounmap(pci_dev, mdev->common);
341 EXPORT_SYMBOL_GPL(vp_modern_probe);
344 * vp_modern_probe: remove and cleanup the modern virtio pci device
345 * @mdev: the modern virtio-pci device
347 void vp_modern_remove(struct virtio_pci_modern_device *mdev)
349 struct pci_dev *pci_dev = mdev->pci_dev;
352 pci_iounmap(pci_dev, mdev->device);
353 if (mdev->notify_base)
354 pci_iounmap(pci_dev, mdev->notify_base);
355 pci_iounmap(pci_dev, mdev->isr);
356 pci_iounmap(pci_dev, mdev->common);
357 pci_release_selected_regions(pci_dev, mdev->modern_bars);
359 EXPORT_SYMBOL_GPL(vp_modern_remove);
362 * vp_modern_get_features - get features from device
363 * @mdev: the modern virtio-pci device
365 * Returns the features read from the device
367 u64 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
369 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
373 vp_iowrite32(0, &cfg->device_feature_select);
374 features = vp_ioread32(&cfg->device_feature);
375 vp_iowrite32(1, &cfg->device_feature_select);
376 features |= ((u64)vp_ioread32(&cfg->device_feature) << 32);
380 EXPORT_SYMBOL_GPL(vp_modern_get_features);
383 * vp_modern_set_features - set features to device
384 * @mdev: the modern virtio-pci device
385 * @features: the features set to device
387 void vp_modern_set_features(struct virtio_pci_modern_device *mdev,
390 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
392 vp_iowrite32(0, &cfg->guest_feature_select);
393 vp_iowrite32((u32)features, &cfg->guest_feature);
394 vp_iowrite32(1, &cfg->guest_feature_select);
395 vp_iowrite32(features >> 32, &cfg->guest_feature);
397 EXPORT_SYMBOL_GPL(vp_modern_set_features);
400 * vp_modern_generation - get the device genreation
401 * @mdev: the modern virtio-pci device
403 * Returns the genreation read from device
405 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev)
407 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
409 return vp_ioread8(&cfg->config_generation);
411 EXPORT_SYMBOL_GPL(vp_modern_generation);
414 * vp_modern_get_status - get the device status
415 * @mdev: the modern virtio-pci device
417 * Returns the status read from device
419 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev)
421 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
423 return vp_ioread8(&cfg->device_status);
425 EXPORT_SYMBOL_GPL(vp_modern_get_status);
428 * vp_modern_set_status - set status to device
429 * @mdev: the modern virtio-pci device
430 * @status: the status set to device
432 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
435 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
437 vp_iowrite8(status, &cfg->device_status);
439 EXPORT_SYMBOL_GPL(vp_modern_set_status);
442 * vp_modern_queue_vector - set the MSIX vector for a specific virtqueue
443 * @mdev: the modern virtio-pci device
444 * @index: queue index
445 * @vector: the config vector
447 * Returns the config vector read from the device
449 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
450 u16 index, u16 vector)
452 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
454 vp_iowrite16(index, &cfg->queue_select);
455 vp_iowrite16(vector, &cfg->queue_msix_vector);
456 /* Flush the write out to device */
457 return vp_ioread16(&cfg->queue_msix_vector);
459 EXPORT_SYMBOL_GPL(vp_modern_queue_vector);
462 * vp_modern_config_vector - set the vector for config interrupt
463 * @mdev: the modern virtio-pci device
464 * @vector: the config vector
466 * Returns the config vector read from the device
468 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
471 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
473 /* Setup the vector used for configuration events */
474 vp_iowrite16(vector, &cfg->msix_config);
475 /* Verify we had enough resources to assign the vector */
476 /* Will also flush the write out to device */
477 return vp_ioread16(&cfg->msix_config);
479 EXPORT_SYMBOL_GPL(vp_modern_config_vector);
482 * vp_modern_queue_address - set the virtqueue address
483 * @mdev: the modern virtio-pci device
484 * @index: the queue index
485 * @desc_addr: address of the descriptor area
486 * @driver_addr: address of the driver area
487 * @device_addr: address of the device area
489 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
490 u16 index, u64 desc_addr, u64 driver_addr,
493 struct virtio_pci_common_cfg __iomem *cfg = mdev->common;
495 vp_iowrite16(index, &cfg->queue_select);
497 vp_iowrite64_twopart(desc_addr, &cfg->queue_desc_lo,
498 &cfg->queue_desc_hi);
499 vp_iowrite64_twopart(driver_addr, &cfg->queue_avail_lo,
500 &cfg->queue_avail_hi);
501 vp_iowrite64_twopart(device_addr, &cfg->queue_used_lo,
502 &cfg->queue_used_hi);
504 EXPORT_SYMBOL_GPL(vp_modern_queue_address);
507 * vp_modern_set_queue_enable - enable a virtqueue
508 * @mdev: the modern virtio-pci device
509 * @index: the queue index
510 * @enable: whether the virtqueue is enable or not
512 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
513 u16 index, bool enable)
515 vp_iowrite16(index, &mdev->common->queue_select);
516 vp_iowrite16(enable, &mdev->common->queue_enable);
518 EXPORT_SYMBOL_GPL(vp_modern_set_queue_enable);
521 * vp_modern_get_queue_enable - enable a virtqueue
522 * @mdev: the modern virtio-pci device
523 * @index: the queue index
525 * Returns whether a virtqueue is enabled or not
527 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
530 vp_iowrite16(index, &mdev->common->queue_select);
532 return vp_ioread16(&mdev->common->queue_enable);
534 EXPORT_SYMBOL_GPL(vp_modern_get_queue_enable);
537 * vp_modern_set_queue_size - set size for a virtqueue
538 * @mdev: the modern virtio-pci device
539 * @index: the queue index
540 * @size: the size of the virtqueue
542 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
545 vp_iowrite16(index, &mdev->common->queue_select);
546 vp_iowrite16(size, &mdev->common->queue_size);
549 EXPORT_SYMBOL_GPL(vp_modern_set_queue_size);
552 * vp_modern_get_queue_size - get size for a virtqueue
553 * @mdev: the modern virtio-pci device
554 * @index: the queue index
556 * Returns the size of the virtqueue
558 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
561 vp_iowrite16(index, &mdev->common->queue_select);
563 return vp_ioread16(&mdev->common->queue_size);
566 EXPORT_SYMBOL_GPL(vp_modern_get_queue_size);
569 * vp_modern_get_num_queues - get the number of virtqueues
570 * @mdev: the modern virtio-pci device
572 * Returns the number of virtqueues
574 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev)
576 return vp_ioread16(&mdev->common->num_queues);
578 EXPORT_SYMBOL_GPL(vp_modern_get_num_queues);
581 * vp_modern_get_queue_notify_off - get notification offset for a virtqueue
582 * @mdev: the modern virtio-pci device
583 * @index: the queue index
585 * Returns the notification offset for a virtqueue
587 u16 vp_modern_get_queue_notify_off(struct virtio_pci_modern_device *mdev,
590 vp_iowrite16(index, &mdev->common->queue_select);
592 return vp_ioread16(&mdev->common->queue_notify_off);
594 EXPORT_SYMBOL_GPL(vp_modern_get_queue_notify_off);
596 MODULE_VERSION("0.1");
597 MODULE_DESCRIPTION("Modern Virtio PCI Device");
599 MODULE_LICENSE("GPL");