From patchwork Tue Oct 31 19:14:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10035253 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 129D1602B9 for ; Tue, 31 Oct 2017 19:16:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0530828AA7 for ; Tue, 31 Oct 2017 19:16:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EE75528AFB; Tue, 31 Oct 2017 19:16:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A01EB28AF6 for ; Tue, 31 Oct 2017 19:16:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933074AbdJaTQM (ORCPT ); Tue, 31 Oct 2017 15:16:12 -0400 Received: from foss.arm.com ([217.140.101.70]:40870 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933072AbdJaTQL (ORCPT ); Tue, 31 Oct 2017 15:16:11 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0487E1435; Tue, 31 Oct 2017 12:16:11 -0700 (PDT) Received: from e106794-lin.cambridge.arm.com (e106794-lin.cambridge.arm.com [10.1.210.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id C64A03F3E1; Tue, 31 Oct 2017 12:16:09 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org Cc: will.deacon@arm.com, robin.murphy@arm.com, lorenzo.pieralisi@arm.com, marc.zyngier@arm.com, punit.agrawal@arm.com Subject: [PATCH v3 kvmtool 05/11] pci: add capability helpers Date: Tue, 31 Oct 2017 19:14:43 +0000 Message-Id: <20171031191449.1950-6-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171031191449.1950-1-jean-philippe.brucker@arm.com> References: <20171031191449.1950-1-jean-philippe.brucker@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a way to iterate over all capabilities in a config space. Add a search function for getting a specific capability. Signed-off-by: Jean-Philippe Brucker --- include/kvm/pci.h | 14 ++++++++++++++ pci.c | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/kvm/pci.h b/include/kvm/pci.h index 5d9c0f3b045f..01c244bcfb7f 100644 --- a/include/kvm/pci.h +++ b/include/kvm/pci.h @@ -58,6 +58,11 @@ struct msix_cap { u32 pba_offset; }; +struct pci_cap_hdr { + u8 type; + u8 next; +}; + #define PCI_BAR_OFFSET(b) (offsetof(struct pci_device_header, bar[b])) #define PCI_DEV_CFG_SIZE 256 #define PCI_DEV_CFG_MASK (PCI_DEV_CFG_SIZE - 1) @@ -113,6 +118,13 @@ struct pci_device_header { enum irq_type irq_type; }; +#define PCI_CAP(pci_hdr, pos) ((void *)(pci_hdr) + (pos)) + +#define pci_for_each_cap(pos, cap, hdr) \ + for ((pos) = (hdr)->capabilities & ~3; \ + (cap) = PCI_CAP(hdr, pos), (pos) != 0; \ + (pos) = ((struct pci_cap_hdr *)(cap))->next & ~3) + int pci__init(struct kvm *kvm); int pci__exit(struct kvm *kvm); struct pci_device_header *pci__find_dev(u8 dev_num); @@ -121,4 +133,6 @@ void pci__assign_irq(struct device_header *dev_hdr); void pci__config_wr(struct kvm *kvm, union pci_config_address addr, void *data, int size); void pci__config_rd(struct kvm *kvm, union pci_config_address addr, void *data, int size); +void *pci_find_cap(struct pci_device_header *hdr, u8 cap_type); + #endif /* KVM__PCI_H */ diff --git a/pci.c b/pci.c index 5a8c2ef41e93..689869cb79a3 100644 --- a/pci.c +++ b/pci.c @@ -27,6 +27,19 @@ u32 pci_get_io_space_block(u32 size) return block; } +void *pci_find_cap(struct pci_device_header *hdr, u8 cap_type) +{ + u8 pos; + struct pci_cap_hdr *cap; + + pci_for_each_cap(pos, cap, hdr) { + if (cap->type == cap_type) + return cap; + } + + return NULL; +} + void pci__assign_irq(struct device_header *dev_hdr) { struct pci_device_header *pci_hdr = dev_hdr->data;