From patchwork Mon Jun 18 18:42:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10472551 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 228686029B for ; Mon, 18 Jun 2018 18:43:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1230A2040D for ; Mon, 18 Jun 2018 18:43:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0700326242; Mon, 18 Jun 2018 18:43:32 +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=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, 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 A23212040D for ; Mon, 18 Jun 2018 18:43:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935873AbeFRSnY (ORCPT ); Mon, 18 Jun 2018 14:43:24 -0400 Received: from foss.arm.com ([217.140.101.70]:39210 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935785AbeFRSnT (ORCPT ); Mon, 18 Jun 2018 14:43:19 -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 7B31F80D; Mon, 18 Jun 2018 11:43:19 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.210.39]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 03F403F557; Mon, 18 Jun 2018 11:43:17 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org Cc: kvmarm@lists.cs.columbia.edu, 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 v6 kvmtool 05/13] pci: add capability helpers Date: Mon, 18 Jun 2018 19:42:03 +0100 Message-Id: <20180618184211.43904-6-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180618184211.43904-1-jean-philippe.brucker@arm.com> References: <20180618184211.43904-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. Reviewed-by: Punit Agrawal 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 5d9c0f3b0..01c244bcf 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 5a8c2ef41..689869cb7 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;