From patchwork Thu Aug 27 08:52:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 7081931 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 40C5ABEEC1 for ; Thu, 27 Aug 2015 08:53:02 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 54AEB208F6 for ; Thu, 27 Aug 2015 08:53:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6765C208C9 for ; Thu, 27 Aug 2015 08:52:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751881AbbH0Iwz (ORCPT ); Thu, 27 Aug 2015 04:52:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41667 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750802AbbH0Iwx (ORCPT ); Thu, 27 Aug 2015 04:52:53 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 9EE938E76C; Thu, 27 Aug 2015 08:52:53 +0000 (UTC) Received: from redhat.com (ovpn-116-110.ams2.redhat.com [10.36.116.110]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id t7R8qpb9001608; Thu, 27 Aug 2015 04:52:52 -0400 Date: Thu, 27 Aug 2015 11:52:51 +0300 From: "Michael S. Tsirkin" To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, qemu-devel@nongnu.org Subject: [PATCH 1/2] pci: add bar sizing Message-ID: <1440665434-2893-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Will be used for pci-testdev. Signed-off-by: Michael S. Tsirkin --- lib/x86/pci.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/x86/pci.c b/lib/x86/pci.c index 231668a..5116dac 100644 --- a/lib/x86/pci.c +++ b/lib/x86/pci.c @@ -19,6 +19,13 @@ static uint32_t pci_config_read(pcidevaddr_t dev, uint8_t reg) return inl(0xCFC); } +static uint32_t pci_config_write(pcidevaddr_t dev, uint8_t reg, uint32_t val) +{ + uint32_t index = reg | (dev << 8) | (0x1 << 31); + outl(0xCF8, index); + outl(0xCFC, val); +} + /* Scan bus look for a specific device. Only bus 0 scanned for now. */ pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id) { @@ -42,6 +49,22 @@ unsigned long pci_bar_addr(pcidevaddr_t dev, int bar_num) } } +unsigned long pci_bar_size(pcidevaddr_t dev, int bar_num) +{ + uint32_t bar = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4); + uint32_t mask; + + pci_config_write(dev, PCI_BASE_ADDRESS_0 + bar_num * 4, 0xffffffff); + mask = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4); + pci_config_write(dev, PCI_BASE_ADDRESS_0 + bar_num * 4, bar); + + if (bar & PCI_BASE_ADDRESS_SPACE_IO) { + return ~(mask & PCI_BASE_ADDRESS_IO_MASK) + 1; + } else { + return ~(mask & PCI_BASE_ADDRESS_MEM_MASK) + 1; + } +} + bool pci_bar_is_memory(pcidevaddr_t dev, int bar_num) { uint32_t bar = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);