From patchwork Wed Nov 22 18:58:16 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: 10070755 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 7F7EF601D5 for ; Wed, 22 Nov 2017 18:56:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7257029D74 for ; Wed, 22 Nov 2017 18:56:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6733729D96; Wed, 22 Nov 2017 18:56:35 +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 0BCD329D74 for ; Wed, 22 Nov 2017 18:56:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752143AbdKVS4d (ORCPT ); Wed, 22 Nov 2017 13:56:33 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:55020 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752115AbdKVS4b (ORCPT ); Wed, 22 Nov 2017 13:56:31 -0500 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 3B9351435; Wed, 22 Nov 2017 10:56:31 -0800 (PST) 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 D9BDD3F487; Wed, 22 Nov 2017 10:56:29 -0800 (PST) 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, alex.williamson@redhat.com Subject: [PATCH v4 kvmtool 05/12] pci: add capability helpers Date: Wed, 22 Nov 2017 18:58:16 +0000 Message-Id: <20171122185823.7765-6-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171122185823.7765-1-jean-philippe.brucker@arm.com> References: <20171122185823.7765-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;