From: Ben-Ami Yassour Subject: RE: [PATCH 1/2] KVM/userspace: Support for assigning PCI devices to guest Date: Tue, 22 Jul 2008 15:28:37 +0300 Message-ID: <1216729717.31546.378.camel@cluwyn.haifa.ibm.com> References: <1216214225-18030-9-git-send-email-benami@il.ibm.com> <1216214475-18117-1-git-send-email-benami@il.ibm.com> <1216214475-18117-2-git-send-email-benami@il.ibm.com> <0122C7C995D32147B66BF4F440D30163015DCCD1@pdsmsx415.ccr.corp.intel.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit Cc: amit.shah@qumranet.com, kvm@vger.kernel.org, Muli Ben-Yehuda , anthony@codemonkey.ws, Or Sagi To: "Han, Weidong" Return-path: Received: from mtagate5.de.ibm.com ([195.212.29.154]:10945 "EHLO mtagate5.de.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751690AbYGVM3c (ORCPT ); Tue, 22 Jul 2008 08:29:32 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate5.de.ibm.com (8.13.8/8.13.8) with ESMTP id m6MCSeqK545272 for ; Tue, 22 Jul 2008 12:28:40 GMT Received: from d12av01.megacenter.de.ibm.com (d12av01.megacenter.de.ibm.com [9.149.165.212]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v9.0) with ESMTP id m6MCSdnY3498120 for ; Tue, 22 Jul 2008 14:28:40 +0200 Received: from d12av01.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av01.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id m6MCSdJC025010 for ; Tue, 22 Jul 2008 14:28:39 +0200 In-Reply-To: <0122C7C995D32147B66BF4F440D30163015DCCD1@pdsmsx415.ccr.corp.intel.com> Sender: kvm-owner@vger.kernel.org List-ID: On Thu, 2008-07-17 at 15:52 +0800, Han, Weidong wrote: > Ben-Ami Yassour wrote: > > From: Or Sagi > > > > From: Nir Peleg > > From: Amit Shah > > From: Glauber de Oliveira Costa > > > > We can assign a device from the host machine to a guest. The > > original code comes from Neocleus. > > > > A new command-line option, -pcidevice is added. > > For example, to invoke it for an Ethernet device sitting at > > PCI bus:dev.fn 04:08.0 with host IRQ 18, use this: > > > > -pcidevice Ethernet/04:08.0 > > > > The host ethernet driver is to be removed before doing the > > passthrough. If not, the device assignment fails but the > > guest continues without the assignment. > > > > If kvm uses the in-kernel irqchip, interrupts are routed to the > > guest via the kvm module (accompanied kernel changes are necessary). > > > > If -no-kvm-irqchip is used, the 'irqhook' module, available > > separately, is to be used for interrupt injection into the guest. In > > this case, an extra parameter, - is to be appended to > > the above-mentioned pcidevice parameter. > > > > Signed-off-by: Amit Shah > > --- > > +static pt_dev_t *register_real_device(PCIBus *e_bus, const char > > *e_dev_name, + int e_devfn, > uint8_t r_bus, uint8_t r_dev, > > + uint8_t r_func) > > +{ > > + int rc; > > + pt_dev_t *pci_dev; > > + uint8_t e_device, e_intx; > > + > > + DEBUG("register_real_device: Registering real physical " > > + "device %s (devfn=0x%x)\n", e_dev_name, e_devfn); > > + > > + pci_dev = (pt_dev_t *) pci_register_device(e_bus, e_dev_name, > > + sizeof(pt_dev_t), > e_devfn, > > + pt_pci_read_config, > > + pt_pci_write_config); > > + > > + if (NULL == pci_dev) { > > + fprintf(stderr, "register_real_device: Error: Couldn't " > > + "register real device %s\n", e_dev_name); > > + return NULL; > > + } > > + if (pt_get_real_device(pci_dev, r_bus, r_dev, r_func)) { > > + fprintf(stderr, "register_real_device: Error: Couldn't > get " > > + "real device (%s)!\n", e_dev_name); > > + return NULL; > > + } > > + > > + /* handle real device's MMIO/PIO BARs */ > > + if (pt_register_regions(pci_dev->real_device.regions, > > + pci_dev->real_device.region_number, > pci_dev)) > > + return NULL; > > + > > + /* handle interrupt routing */ > > + e_device = (pci_dev->dev.devfn >> 3) & 0x1f; > > + e_intx = pci_dev->dev.config[0x3d] - 1; > > + pci_dev->intpin = e_intx; > > + pci_dev->run = 0; > > + pci_dev->girq = 0; > > + pci_dev->h_busnr = r_bus; > > + pci_dev->h_devfn = PCI_DEVFN(r_dev, r_func); > > + > > +#ifdef KVM_CAP_PCI_PASSTHROUGH > > + if (kvm_enabled()) { > > + struct kvm_pci_passthrough_dev pci_pt_dev; > > + > > + memset(&pci_pt_dev, 0, sizeof(pci_pt_dev)); > > + pci_pt_dev.guest.busnr = pci_bus_num(e_bus); > > + pci_pt_dev.guest.devfn = PCI_DEVFN(e_device, r_func); > > Why combine e_device and r_func? The guest devfn is e_devfn. the current assumption is that e_func and r_func are identical. > > > + pci_pt_dev.host.busnr = pci_dev->h_busnr; > > + pci_pt_dev.host.devfn = pci_dev->h_devfn; > > + > > + /* We'll set the value of the guest irq as and when > > + * the piix config gets updated. See pci_pt_update_irq. > > + * The host irq field never gets used anyway > > + */ > > + > > + rc = kvm_update_pci_pt_device(kvm_context, &pci_pt_dev); > > + if (rc < 0) { > > + fprintf(stderr, "Could not notify kernel about " > > + "passthrough device\n"); > > + perror("pt-ioctl"); > > + return NULL; > > + } > > + } > > +#endif > > + > > + fprintf(logfile, "Registered host PCI device %02x:%02x.%1x " > > + "as guest device %02x:%02x.%1x\n", > > + r_bus, r_dev, r_func, > > + pci_bus_num(e_bus), e_device, r_func); > > Second "r_func" should replaced by e_devfn & 0x7. same as above. > > Randy (Weidong) >