4 * Copyright (C) 2012 VMware, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 and no later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <linux/vmw_vmci_defs.h>
17 #include <linux/vmw_vmci_api.h>
18 #include <linux/moduleparam.h>
19 #include <linux/interrupt.h>
20 #include <linux/highmem.h>
21 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/smp.h>
30 #include <linux/vmalloc.h>
32 #include "vmci_datagram.h"
33 #include "vmci_doorbell.h"
34 #include "vmci_context.h"
35 #include "vmci_driver.h"
36 #include "vmci_event.h"
38 #define PCI_VENDOR_ID_VMWARE 0x15AD
39 #define PCI_DEVICE_ID_VMWARE_VMCI 0x0740
41 #define VMCI_UTIL_NUM_RESOURCES 1
43 static bool vmci_disable_msi;
44 module_param_named(disable_msi, vmci_disable_msi, bool, 0);
45 MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
47 static bool vmci_disable_msix;
48 module_param_named(disable_msix, vmci_disable_msix, bool, 0);
49 MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
51 static u32 ctx_update_sub_id = VMCI_INVALID_ID;
52 static u32 vm_context_id = VMCI_INVALID_ID;
54 struct vmci_guest_device {
55 struct device *dev; /* PCI device we are attached to */
59 unsigned int intr_type;
60 bool exclusive_vectors;
61 struct msix_entry msix_entries[VMCI_MAX_INTRS];
63 struct tasklet_struct datagram_tasklet;
64 struct tasklet_struct bm_tasklet;
67 void *notification_bitmap;
70 /* vmci_dev singleton device and supporting data*/
71 static struct vmci_guest_device *vmci_dev_g;
72 static DEFINE_SPINLOCK(vmci_dev_spinlock);
74 static atomic_t vmci_num_guest_devices = ATOMIC_INIT(0);
76 bool vmci_guest_code_active(void)
78 return atomic_read(&vmci_num_guest_devices) != 0;
81 u32 vmci_get_vm_context_id(void)
83 if (vm_context_id == VMCI_INVALID_ID) {
85 struct vmci_datagram get_cid_msg;
87 vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
89 get_cid_msg.src = VMCI_ANON_SRC_HANDLE;
90 get_cid_msg.payload_size = 0;
91 result = vmci_send_datagram(&get_cid_msg);
93 vm_context_id = result;
99 * VM to hypervisor call mechanism. We use the standard VMware naming
100 * convention since shared code is calling this function as well.
102 int vmci_send_datagram(struct vmci_datagram *dg)
109 return VMCI_ERROR_INVALID_ARGS;
112 * Need to acquire spinlock on the device because the datagram
113 * data may be spread over multiple pages and the monitor may
114 * interleave device user rpc calls from multiple
115 * VCPUs. Acquiring the spinlock precludes that
116 * possibility. Disabling interrupts to avoid incoming
117 * datagrams during a "rep out" and possibly landing up in
120 spin_lock_irqsave(&vmci_dev_spinlock, flags);
123 iowrite8_rep(vmci_dev_g->iobase + VMCI_DATA_OUT_ADDR,
124 dg, VMCI_DG_SIZE(dg));
125 result = ioread32(vmci_dev_g->iobase + VMCI_RESULT_LOW_ADDR);
127 result = VMCI_ERROR_UNAVAILABLE;
130 spin_unlock_irqrestore(&vmci_dev_spinlock, flags);
134 EXPORT_SYMBOL_GPL(vmci_send_datagram);
137 * Gets called with the new context id if updated or resumed.
140 static void vmci_guest_cid_update(u32 sub_id,
141 const struct vmci_event_data *event_data,
144 const struct vmci_event_payld_ctx *ev_payload =
145 vmci_event_data_const_payload(event_data);
147 if (sub_id != ctx_update_sub_id) {
148 pr_devel("Invalid subscriber (ID=0x%x)\n", sub_id);
152 if (!event_data || ev_payload->context_id == VMCI_INVALID_ID) {
153 pr_devel("Invalid event data\n");
157 pr_devel("Updating context from (ID=0x%x) to (ID=0x%x) on event (type=%d)\n",
158 vm_context_id, ev_payload->context_id, event_data->event);
160 vm_context_id = ev_payload->context_id;
164 * Verify that the host supports the hypercalls we need. If it does not,
165 * try to find fallback hypercalls and use those instead. Returns
166 * true if required hypercalls (or fallback hypercalls) are
167 * supported by the host, false otherwise.
169 static bool vmci_check_host_caps(struct pci_dev *pdev)
172 struct vmci_resource_query_msg *msg;
173 u32 msg_size = sizeof(struct vmci_resource_query_hdr) +
174 VMCI_UTIL_NUM_RESOURCES * sizeof(u32);
175 struct vmci_datagram *check_msg;
177 check_msg = kmalloc(msg_size, GFP_KERNEL);
179 dev_err(&pdev->dev, "%s: Insufficient memory\n", __func__);
183 check_msg->dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
184 VMCI_RESOURCES_QUERY);
185 check_msg->src = VMCI_ANON_SRC_HANDLE;
186 check_msg->payload_size = msg_size - VMCI_DG_HEADERSIZE;
187 msg = (struct vmci_resource_query_msg *)VMCI_DG_PAYLOAD(check_msg);
189 msg->num_resources = VMCI_UTIL_NUM_RESOURCES;
190 msg->resources[0] = VMCI_GET_CONTEXT_ID;
192 /* Checks that hyper calls are supported */
193 result = vmci_send_datagram(check_msg) == 0x01;
196 dev_dbg(&pdev->dev, "%s: Host capability check: %s\n",
197 __func__, result ? "PASSED" : "FAILED");
199 /* We need the vector. There are no fallbacks. */
204 * Reads datagrams from the data in port and dispatches them. We
205 * always start reading datagrams into only the first page of the
206 * datagram buffer. If the datagrams don't fit into one page, we
207 * use the maximum datagram buffer size for the remainder of the
208 * invocation. This is a simple heuristic for not penalizing
211 * This function assumes that it has exclusive access to the data
212 * in port for the duration of the call.
214 static void vmci_dispatch_dgs(unsigned long data)
216 struct vmci_guest_device *vmci_dev = (struct vmci_guest_device *)data;
217 u8 *dg_in_buffer = vmci_dev->data_buffer;
218 struct vmci_datagram *dg;
219 size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE;
220 size_t current_dg_in_buffer_size = PAGE_SIZE;
221 size_t remaining_bytes;
223 BUILD_BUG_ON(VMCI_MAX_DG_SIZE < PAGE_SIZE);
225 ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
226 vmci_dev->data_buffer, current_dg_in_buffer_size);
227 dg = (struct vmci_datagram *)dg_in_buffer;
228 remaining_bytes = current_dg_in_buffer_size;
230 while (dg->dst.resource != VMCI_INVALID_ID ||
231 remaining_bytes > PAGE_SIZE) {
235 * When the input buffer spans multiple pages, a datagram can
236 * start on any page boundary in the buffer.
238 if (dg->dst.resource == VMCI_INVALID_ID) {
239 dg = (struct vmci_datagram *)roundup(
240 (uintptr_t)dg + 1, PAGE_SIZE);
242 (size_t)(dg_in_buffer +
243 current_dg_in_buffer_size -
248 dg_in_size = VMCI_DG_SIZE_ALIGNED(dg);
250 if (dg_in_size <= dg_in_buffer_size) {
254 * If the remaining bytes in the datagram
255 * buffer doesn't contain the complete
256 * datagram, we first make sure we have enough
257 * room for it and then we read the reminder
258 * of the datagram and possibly any following
261 if (dg_in_size > remaining_bytes) {
262 if (remaining_bytes !=
263 current_dg_in_buffer_size) {
266 * We move the partial
267 * datagram to the front and
268 * read the reminder of the
269 * datagram and possibly
270 * following calls into the
273 memmove(dg_in_buffer, dg_in_buffer +
274 current_dg_in_buffer_size -
277 dg = (struct vmci_datagram *)
281 if (current_dg_in_buffer_size !=
283 current_dg_in_buffer_size =
286 ioread8_rep(vmci_dev->iobase +
288 vmci_dev->data_buffer +
290 current_dg_in_buffer_size -
295 * We special case event datagrams from the
298 if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
299 dg->dst.resource == VMCI_EVENT_HANDLER) {
300 result = vmci_event_dispatch(dg);
302 result = vmci_datagram_invoke_guest_handler(dg);
304 if (result < VMCI_SUCCESS)
305 dev_dbg(vmci_dev->dev,
306 "Datagram with resource (ID=0x%x) failed (err=%d)\n",
307 dg->dst.resource, result);
309 /* On to the next datagram. */
310 dg = (struct vmci_datagram *)((u8 *)dg +
313 size_t bytes_to_skip;
316 * Datagram doesn't fit in datagram buffer of maximal
319 dev_dbg(vmci_dev->dev,
320 "Failed to receive datagram (size=%u bytes)\n",
323 bytes_to_skip = dg_in_size - remaining_bytes;
324 if (current_dg_in_buffer_size != dg_in_buffer_size)
325 current_dg_in_buffer_size = dg_in_buffer_size;
328 ioread8_rep(vmci_dev->iobase +
330 vmci_dev->data_buffer,
331 current_dg_in_buffer_size);
332 if (bytes_to_skip <= current_dg_in_buffer_size)
335 bytes_to_skip -= current_dg_in_buffer_size;
337 dg = (struct vmci_datagram *)(dg_in_buffer +
342 (size_t) (dg_in_buffer + current_dg_in_buffer_size -
345 if (remaining_bytes < VMCI_DG_HEADERSIZE) {
346 /* Get the next batch of datagrams. */
348 ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
349 vmci_dev->data_buffer,
350 current_dg_in_buffer_size);
351 dg = (struct vmci_datagram *)dg_in_buffer;
352 remaining_bytes = current_dg_in_buffer_size;
358 * Scans the notification bitmap for raised flags, clears them
359 * and handles the notifications.
361 static void vmci_process_bitmap(unsigned long data)
363 struct vmci_guest_device *dev = (struct vmci_guest_device *)data;
365 if (!dev->notification_bitmap) {
366 dev_dbg(dev->dev, "No bitmap present in %s\n", __func__);
370 vmci_dbell_scan_notification_entries(dev->notification_bitmap);
374 * Enable MSI-X. Try exclusive vectors first, then shared vectors.
376 static int vmci_enable_msix(struct pci_dev *pdev,
377 struct vmci_guest_device *vmci_dev)
382 for (i = 0; i < VMCI_MAX_INTRS; ++i) {
383 vmci_dev->msix_entries[i].entry = i;
384 vmci_dev->msix_entries[i].vector = i;
387 result = pci_enable_msix(pdev, vmci_dev->msix_entries, VMCI_MAX_INTRS);
389 vmci_dev->exclusive_vectors = true;
391 result = pci_enable_msix(pdev, vmci_dev->msix_entries, 1);
397 * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
398 * interrupt (vector VMCI_INTR_DATAGRAM).
400 static irqreturn_t vmci_interrupt(int irq, void *_dev)
402 struct vmci_guest_device *dev = _dev;
405 * If we are using MSI-X with exclusive vectors then we simply schedule
406 * the datagram tasklet, since we know the interrupt was meant for us.
407 * Otherwise we must read the ICR to determine what to do.
410 if (dev->intr_type == VMCI_INTR_TYPE_MSIX && dev->exclusive_vectors) {
411 tasklet_schedule(&dev->datagram_tasklet);
415 /* Acknowledge interrupt and determine what needs doing. */
416 icr = ioread32(dev->iobase + VMCI_ICR_ADDR);
417 if (icr == 0 || icr == ~0)
420 if (icr & VMCI_ICR_DATAGRAM) {
421 tasklet_schedule(&dev->datagram_tasklet);
422 icr &= ~VMCI_ICR_DATAGRAM;
425 if (icr & VMCI_ICR_NOTIFICATION) {
426 tasklet_schedule(&dev->bm_tasklet);
427 icr &= ~VMCI_ICR_NOTIFICATION;
432 "Ignoring unknown interrupt cause (%d)\n",
440 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
441 * which is for the notification bitmap. Will only get called if we are
442 * using MSI-X with exclusive vectors.
444 static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
446 struct vmci_guest_device *dev = _dev;
448 /* For MSI-X we can just assume it was meant for us. */
449 tasklet_schedule(&dev->bm_tasklet);
455 * Most of the initialization at module load time is done here.
457 static int vmci_guest_probe_device(struct pci_dev *pdev,
458 const struct pci_device_id *id)
460 struct vmci_guest_device *vmci_dev;
461 void __iomem *iobase;
462 unsigned int capabilities;
467 dev_dbg(&pdev->dev, "Probing for vmci/PCI guest device\n");
469 error = pcim_enable_device(pdev);
472 "Failed to enable VMCI device: %d\n", error);
476 error = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
478 dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
482 iobase = pcim_iomap_table(pdev)[0];
484 dev_info(&pdev->dev, "Found VMCI PCI device at %#lx, irq %u\n",
485 (unsigned long)iobase, pdev->irq);
487 vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
490 "Can't allocate memory for VMCI device\n");
494 vmci_dev->dev = &pdev->dev;
495 vmci_dev->intr_type = VMCI_INTR_TYPE_INTX;
496 vmci_dev->exclusive_vectors = false;
497 vmci_dev->iobase = iobase;
499 tasklet_init(&vmci_dev->datagram_tasklet,
500 vmci_dispatch_dgs, (unsigned long)vmci_dev);
501 tasklet_init(&vmci_dev->bm_tasklet,
502 vmci_process_bitmap, (unsigned long)vmci_dev);
504 vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
505 if (!vmci_dev->data_buffer) {
507 "Can't allocate memory for datagram buffer\n");
511 pci_set_master(pdev); /* To enable queue_pair functionality. */
514 * Verify that the VMCI Device supports the capabilities that
515 * we need. If the device is missing capabilities that we would
516 * like to use, check for fallback capabilities and use those
517 * instead (so we can run a new VM on old hosts). Fail the load if
518 * a required capability is missing and there is no fallback.
520 * Right now, we need datagrams. There are no fallbacks.
522 capabilities = ioread32(vmci_dev->iobase + VMCI_CAPS_ADDR);
523 if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
524 dev_err(&pdev->dev, "Device does not support datagrams\n");
526 goto err_free_data_buffer;
530 * If the hardware supports notifications, we will use that as
533 if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
534 vmci_dev->notification_bitmap = vmalloc(PAGE_SIZE);
535 if (!vmci_dev->notification_bitmap) {
537 "Unable to allocate notification bitmap\n");
539 memset(vmci_dev->notification_bitmap, 0, PAGE_SIZE);
540 capabilities |= VMCI_CAPS_NOTIFICATIONS;
544 dev_info(&pdev->dev, "Using capabilities 0x%x\n", capabilities);
546 /* Let the host know which capabilities we intend to use. */
547 iowrite32(capabilities, vmci_dev->iobase + VMCI_CAPS_ADDR);
549 /* Set up global device so that we can start sending datagrams */
550 spin_lock_irq(&vmci_dev_spinlock);
551 vmci_dev_g = vmci_dev;
552 spin_unlock_irq(&vmci_dev_spinlock);
555 * Register notification bitmap with device if that capability is
558 if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
560 vmalloc_to_page(vmci_dev->notification_bitmap);
561 unsigned long bitmap_ppn = page_to_pfn(page);
562 if (!vmci_dbell_register_notification_bitmap(bitmap_ppn)) {
564 "VMCI device unable to register notification bitmap with PPN 0x%x\n",
566 goto err_remove_vmci_dev_g;
570 /* Check host capabilities. */
571 if (!vmci_check_host_caps(pdev))
572 goto err_remove_bitmap;
577 * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
578 * update the internal context id when needed.
580 vmci_err = vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE,
581 vmci_guest_cid_update, NULL,
583 if (vmci_err < VMCI_SUCCESS)
585 "Failed to subscribe to event (type=%d): %d\n",
586 VMCI_EVENT_CTX_ID_UPDATE, vmci_err);
589 * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
592 if (!vmci_disable_msix && !vmci_enable_msix(pdev, vmci_dev)) {
593 vmci_dev->intr_type = VMCI_INTR_TYPE_MSIX;
594 vmci_dev->irq = vmci_dev->msix_entries[0].vector;
595 } else if (!vmci_disable_msi && !pci_enable_msi(pdev)) {
596 vmci_dev->intr_type = VMCI_INTR_TYPE_MSI;
597 vmci_dev->irq = pdev->irq;
599 vmci_dev->intr_type = VMCI_INTR_TYPE_INTX;
600 vmci_dev->irq = pdev->irq;
604 * Request IRQ for legacy or MSI interrupts, or for first
607 error = request_irq(vmci_dev->irq, vmci_interrupt, IRQF_SHARED,
608 KBUILD_MODNAME, vmci_dev);
610 dev_err(&pdev->dev, "Irq %u in use: %d\n",
611 vmci_dev->irq, error);
612 goto err_disable_msi;
616 * For MSI-X with exclusive vectors we need to request an
617 * interrupt for each vector so that we get a separate
618 * interrupt handler routine. This allows us to distinguish
619 * between the vectors.
621 if (vmci_dev->exclusive_vectors) {
622 error = request_irq(vmci_dev->msix_entries[1].vector,
623 vmci_interrupt_bm, 0, KBUILD_MODNAME,
627 "Failed to allocate irq %u: %d\n",
628 vmci_dev->msix_entries[1].vector, error);
633 dev_dbg(&pdev->dev, "Registered device\n");
635 atomic_inc(&vmci_num_guest_devices);
637 /* Enable specific interrupt bits. */
638 cmd = VMCI_IMR_DATAGRAM;
639 if (capabilities & VMCI_CAPS_NOTIFICATIONS)
640 cmd |= VMCI_IMR_NOTIFICATION;
641 iowrite32(cmd, vmci_dev->iobase + VMCI_IMR_ADDR);
643 /* Enable interrupts. */
644 iowrite32(VMCI_CONTROL_INT_ENABLE,
645 vmci_dev->iobase + VMCI_CONTROL_ADDR);
647 pci_set_drvdata(pdev, vmci_dev);
651 free_irq(vmci_dev->irq, &vmci_dev);
652 tasklet_kill(&vmci_dev->datagram_tasklet);
653 tasklet_kill(&vmci_dev->bm_tasklet);
656 if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSIX)
657 pci_disable_msix(pdev);
658 else if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSI)
659 pci_disable_msi(pdev);
661 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
662 if (vmci_err < VMCI_SUCCESS)
664 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
665 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
668 if (vmci_dev->notification_bitmap) {
669 iowrite32(VMCI_CONTROL_RESET,
670 vmci_dev->iobase + VMCI_CONTROL_ADDR);
671 vfree(vmci_dev->notification_bitmap);
674 err_remove_vmci_dev_g:
675 spin_lock_irq(&vmci_dev_spinlock);
677 spin_unlock_irq(&vmci_dev_spinlock);
679 err_free_data_buffer:
680 vfree(vmci_dev->data_buffer);
682 /* The rest are managed resources and will be freed by PCI core */
686 static void vmci_guest_remove_device(struct pci_dev *pdev)
688 struct vmci_guest_device *vmci_dev = pci_get_drvdata(pdev);
691 dev_dbg(&pdev->dev, "Removing device\n");
693 atomic_dec(&vmci_num_guest_devices);
695 vmci_qp_guest_endpoints_exit();
697 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
698 if (vmci_err < VMCI_SUCCESS)
700 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
701 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
703 spin_lock_irq(&vmci_dev_spinlock);
705 spin_unlock_irq(&vmci_dev_spinlock);
707 dev_dbg(&pdev->dev, "Resetting vmci device\n");
708 iowrite32(VMCI_CONTROL_RESET, vmci_dev->iobase + VMCI_CONTROL_ADDR);
711 * Free IRQ and then disable MSI/MSI-X as appropriate. For
712 * MSI-X, we might have multiple vectors, each with their own
713 * IRQ, which we must free too.
715 free_irq(vmci_dev->irq, vmci_dev);
716 if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSIX) {
717 if (vmci_dev->exclusive_vectors)
718 free_irq(vmci_dev->msix_entries[1].vector, vmci_dev);
719 pci_disable_msix(pdev);
720 } else if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSI) {
721 pci_disable_msi(pdev);
724 tasklet_kill(&vmci_dev->datagram_tasklet);
725 tasklet_kill(&vmci_dev->bm_tasklet);
727 if (vmci_dev->notification_bitmap) {
729 * The device reset above cleared the bitmap state of the
730 * device, so we can safely free it here.
733 vfree(vmci_dev->notification_bitmap);
736 vfree(vmci_dev->data_buffer);
738 /* The rest are managed resources and will be freed by PCI core */
741 static DEFINE_PCI_DEVICE_TABLE(vmci_ids) = {
742 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_VMCI), },
745 MODULE_DEVICE_TABLE(pci, vmci_ids);
747 static struct pci_driver vmci_guest_driver = {
748 .name = KBUILD_MODNAME,
749 .id_table = vmci_ids,
750 .probe = vmci_guest_probe_device,
751 .remove = vmci_guest_remove_device,
754 int __init vmci_guest_init(void)
756 return pci_register_driver(&vmci_guest_driver);
759 void __exit vmci_guest_exit(void)
761 pci_unregister_driver(&vmci_guest_driver);