From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753270AbbJFQSF (ORCPT ); Tue, 6 Oct 2015 12:18:05 -0400 Received: from mail-wi0-f171.google.com ([209.85.212.171]:35425 "EHLO mail-wi0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753251AbbJFQSA (ORCPT ); Tue, 6 Oct 2015 12:18:00 -0400 From: Vlad Zolotarov To: linux-kernel@vger.kernel.org, mst@redhat.com, hjk@hansjkoch.de, corbet@lwn.net, gregkh@linuxfoundation.org Cc: bruce.richardson@intel.com, avi@cloudius-systems.com, gleb@cloudius-systems.com, stephen@networkplumber.org, alexander.duyck@gmail.com, Vlad Zolotarov Subject: [PATCH v4 4/4] Documentation: update uio-howto Date: Tue, 6 Oct 2015 19:17:46 +0300 Message-Id: <1444148266-21972-5-git-send-email-vladz@cloudius-systems.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1444148266-21972-1-git-send-email-vladz@cloudius-systems.com> References: <1444148266-21972-1-git-send-email-vladz@cloudius-systems.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Change the chapters related to uio_pci_generic that refer interrupt mode. Add the relevant explanation regarding MSI and MSI-X interrupt modes support. Signed-off-by: Vlad Zolotarov --- New in v4: - Update uio_pci_generic example in uio-howto.tmpl --- Documentation/DocBook/uio-howto.tmpl | 133 ++++++++++++++++++++++++++++++----- 1 file changed, 116 insertions(+), 17 deletions(-) diff --git a/Documentation/DocBook/uio-howto.tmpl b/Documentation/DocBook/uio-howto.tmpl index cd0e452..507b2ca 100644 --- a/Documentation/DocBook/uio-howto.tmpl +++ b/Documentation/DocBook/uio-howto.tmpl @@ -46,6 +46,12 @@ GPL version 2. + 0.10 + 2015-10-04 + vz + Added MSI and MSI-X support to uio_pci_generic. + + 0.9 2009-07-16 mst @@ -935,15 +941,32 @@ and look in the output for failure reasons Things to know about uio_pci_generic -Interrupts are handled using the Interrupt Disable bit in the PCI command +Interrupts are handled either as MSI-X or MSI interrupts (if the device supports it) or +as legacy INTx interrupts. By default INTx interrupts are used. + + +uio_pci_generic automatically configures a device to use INTx interrupt for backward +compatibility. If INTx are not available MSI-X interrupts will be used if the device +supports it and if not MSI interrupts are going to be used. If none of the interrupts +modes is supported probe() will fail. + + +To get the used interrupt mode application has to use UIO_PCI_GENERIC_INT_MODE_GET ioctl +command. +UIO_PCI_GENERIC_IRQ_NUM_GET ioctl command may be used to get the total number of IRQs. +Then UIO_PCI_GENERIC_IRQ_SET ioctl command may be used to bind a specific eventfd to a specific +IRQ vector. + + +Legacy interrupts are handled using the Interrupt Disable bit in the PCI command register and Interrupt Status bit in the PCI status register. All devices compliant to PCI 2.3 (circa 2002) and all compliant PCI Express devices should support these bits. uio_pci_generic detects this support, and won't bind to devices which do not support the Interrupt Disable Bit in the command register. -On each interrupt, uio_pci_generic sets the Interrupt Disable bit. -This prevents the device from generating further interrupts +If legacy interrupts are used, uio_pci_generic sets the Interrupt Disable bit on +each interrupt. This prevents the device from generating further interrupts until the bit is cleared. The userspace driver should clear this bit before blocking and waiting for more interrupts. @@ -966,17 +989,23 @@ Here is some sample userspace driver code using uio_pci_generic: #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> +#include <linux/uio_pci_generic.h> #include <fcntl.h> #include <errno.h> +#include <sys/eventfd.h> +#include <sys/ioctl.h> int main() { - int uiofd; + int uiofd, event_fd; int configfd; int err; int i; unsigned icount; + __u64 read_buf; unsigned char command_high; + __u32 int_mode, num_irqs; + int bytes_to_read; uiofd = open("/dev/uio0", O_RDONLY); if (uiofd < 0) { @@ -989,13 +1018,65 @@ int main() return errno; } - /* Read and cache command value */ - err = pread(configfd, &command_high, 1, 5); - if (err != 1) { - perror("command config read:"); + /* Get the interrupt mode */ + if (ioctl(uiofd, UIO_PCI_GENERIC_INT_MODE_GET, &int_mode) < 0) + { perror("getting interrupt mode"); return errno; + } + + /* Read the number of available IRQs - for INT#x and MSI 1 (one) will be returned */ + if (ioctl(uiofd, UIO_PCI_GENERIC_IRQ_NUM_GET, &num_irqs) < 0) { + perror("getting IRQs number"); return errno; } - command_high &= ~0x4; + + /* if interrupt mode is MSI-X - allocate eventfd file descriptor and bind it to + * the IRQ you need. We'll bind it to the first available IRQ in this example. + */ + if (int_mode == UIO_INT_MODE_MSIX) { + struct uio_pci_generic_irq_set irq_set; + + if (num_irqs < 1) { + printf("Hmmm... Zero IRQ numbers. Something wrong with MSI-X configuration\n"); + return -1; + } + + printf("Interrupt mode is MSI-X, we are going to use IRQ[0]\n"); + + /* set up an eventfd for an interrupt */ + event_fd = eventfd(0, EFD_CLOEXEC); + if (event_fd < 0) { + perror("cannot create irq eventfd"); + return errno; + } + + /* connect to the first IRQ */ + irq_set.vec = 0; + irq_set.fd = event_fd; + + if (ioctl(uiofd, UIO_PCI_GENERIC_IRQ_SET, &irq_set) < 0) { + perror("binding the eventfd descriptor to IRQ[0]"); + return errno; + } + + /* eventfd read() requires to read 8 bytes, while UIO - requires 4 bytes */ + bytes_to_read = 8; + } else if (int_mode == UIO_INT_MODE_MSI || int_mode == UIO_INT_MODE_INTX) { + event_fd = uiofd; + bytes_to_read = 4; + + if (int_mode == UIO_INT_MODE_INTX) { + /* Read and cache command value */ + err = pread(configfd, &command_high, 1, 5); + if (err != 1) { + perror("command config read:"); + return errno; + } + command_high &= ~0x4; + } + } else { + printf("Interrupts are not supported\n"); + return -1; + } for(i = 0;; ++i) { /* Print out a message, for debugging. */ @@ -1006,24 +1087,42 @@ int main() /****************************************/ /* Here we got an interrupt from the - device. Do something to it. */ + device. Do something to it and handle the HW + interrupt state machine if needed. */ /****************************************/ - /* Re-enable interrupts. */ - err = pwrite(configfd, &command_high, 1, 5); - if (err != 1) { - perror("config write:"); - break; + if (int_mode == UIO_INT_MODE_INTX) { + /* Re-enable interrupts. */ + err = pwrite(configfd, &command_high, 1, 5); + if (err != 1) { + perror("config write:"); + break; + } } /* Wait for next interrupt. */ - err = read(uiofd, &icount, 4); - if (err != 4) { + err = read(event_fd, &read_buf, bytes_to_read); + if (err != bytes_to_read) { perror("uio read:"); break; } + icount++; } + + /* optional: unbind the eventfd from the IRQ */ + if (int_mode == UIO_INT_MODE_MSIX) { + struct uio_pci_generic_irq_set irq_set = { + .vec = 0, + .fd = -1 + }; + + if (ioctl(uiofd, UIO_PCI_GENERIC_IRQ_SET, &irq_set) < 0) { + perror("unbinding the eventfd descriptor from IRQ[0]"); + return errno; + } + } + return errno; } -- 2.1.0