From patchwork Tue Mar 8 14:27:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 8534761 Return-Path: X-Original-To: patchwork-xen-devel@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 71F70C0553 for ; Tue, 8 Mar 2016 14:29:45 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 86B0320165 for ; Tue, 8 Mar 2016 14:29:44 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9A9972011B for ; Tue, 8 Mar 2016 14:29:43 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1adIbq-0002XU-68; Tue, 08 Mar 2016 14:27:38 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.84) (envelope-from ) id 1adIbo-0002UH-CB for xen-devel@lists.xensource.com; Tue, 08 Mar 2016 14:27:36 +0000 Received: from [85.158.139.211] by server-1.bemta-5.messagelabs.com id 15/D1-29419-851EED65; Tue, 08 Mar 2016 14:27:36 +0000 X-Env-Sender: kraxel@redhat.com X-Msg-Ref: server-9.tower-206.messagelabs.com!1457447253!27699055!1 X-Originating-IP: [209.132.183.28] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMjA5LjEzMi4xODMuMjggPT4gNTQwNjQ=\n X-StarScan-Received: X-StarScan-Version: 8.11; banners=-,-,- X-VirusChecked: Checked Received: (qmail 40069 invoked from network); 8 Mar 2016 14:27:35 -0000 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by server-9.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 8 Mar 2016 14:27:35 -0000 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 87EEB8DFF1; Tue, 8 Mar 2016 14:27:33 +0000 (UTC) Received: from nilsson.home.kraxel.org (ovpn-116-59.ams2.redhat.com [10.36.116.59]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u28ERWoD008995; Tue, 8 Mar 2016 09:27:32 -0500 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id DDA4E8275B; Tue, 8 Mar 2016 15:27:29 +0100 (CET) From: Gerd Hoffmann To: kevin.tian@intel.com, Alex Williamson , Stefano Stabellini Date: Tue, 8 Mar 2016 15:27:25 +0100 Message-Id: <1457447247-4865-7-git-send-email-kraxel@redhat.com> In-Reply-To: <1457447247-4865-1-git-send-email-kraxel@redhat.com> References: <1457447247-4865-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 Cc: igvt-g@ml01.01.org, xen-devel@lists.xensource.com, Gerd Hoffmann , "open list:All patches CC here" Subject: [Xen-devel] [PATCH v4 6/8] igd: revamp host config read X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, 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 Move all work to the host_pci_config_copy helper function, which we can easily reuse when adding q35 support. Open sysfs file only once for all values. Use pread. Proper error handling. Fix bug: Update config space directly (writing via pci_default_write_config only works for registers whitelisted in wmask). Hmm, this code can hardly ever worked before, /me wonders what test coverage it had. With this patch in place igd-passthru=on actually works, although it still requires root priviledges because linux refuses to allow non-root users access pci config space above offset 0x50. Signed-off-by: Gerd Hoffmann --- hw/pci-host/igd.c | 65 ++++++++++++++++++++++--------------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/hw/pci-host/igd.c b/hw/pci-host/igd.c index 8a8b37b..5c4a008 100644 --- a/hw/pci-host/igd.c +++ b/hw/pci-host/igd.c @@ -20,40 +20,33 @@ static const IGDHostInfo igd_host_bridge_infos[] = { {0xa8, 4}, /* SNB: base of GTT stolen memory */ }; -static int host_pci_config_read(int pos, int len, uint32_t *val) +static void host_pci_config_copy(PCIDevice *guest, const char *host, + const IGDHostInfo *list, int len, Error **errp) { - char path[PATH_MAX]; - int config_fd; - ssize_t size = sizeof(path); - /* Access real host bridge. */ - int rc = snprintf(path, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s", - 0, 0, 0, 0, "config"); - int ret = 0; + char *path; + int config_fd, rc, i; - if (rc >= size || rc < 0) { - return -ENODEV; - } - - config_fd = open(path, O_RDWR); + path = g_strdup_printf("/sys/bus/pci/devices/%s/config", host); + config_fd = open(path, O_RDONLY); if (config_fd < 0) { - return -ENODEV; + error_setg_file_open(errp, errno, path); + goto out_free; } - if (lseek(config_fd, pos, SEEK_SET) != pos) { - ret = -errno; - goto out; + for (i = 0; i < len; i++) { + rc = pread(config_fd, guest->config + list[i].offset, + list[i].len, list[i].offset); + if (rc != list[i].len) { + error_setg_errno(errp, errno, "read %s, offset 0x%x", + path, list[i].offset); + goto out_close; + } } - do { - rc = read(config_fd, (uint8_t *)val, len); - } while (rc < 0 && (errno == EINTR || errno == EAGAIN)); - if (rc != len) { - ret = -errno; - } - -out: +out_close: close(config_fd); - return ret; +out_free: + g_free(path); } #define IGD_PT_I440FX_CLASS(class) \ @@ -72,9 +65,6 @@ static void igd_pt_i440fx_realize(PCIDevice *pci_dev, Error **errp) { IGDPtI440fxClass *k = IGD_PT_I440FX_GET_CLASS(pci_dev); Error *err = NULL; - uint32_t val = 0; - int rc, i, num; - int pos, len; k->parent_realize(pci_dev, &err); if (err != NULL) { @@ -82,16 +72,13 @@ static void igd_pt_i440fx_realize(PCIDevice *pci_dev, Error **errp) return; } - num = ARRAY_SIZE(igd_host_bridge_infos); - for (i = 0; i < num; i++) { - pos = igd_host_bridge_infos[i].offset; - len = igd_host_bridge_infos[i].len; - rc = host_pci_config_read(pos, len, &val); - if (rc) { - error_setg(errp, "failed to read host config"); - return; - } - pci_default_write_config(pci_dev, pos, val, len); + host_pci_config_copy(pci_dev, "0000:00:00.0", + igd_host_bridge_infos, + ARRAY_SIZE(igd_host_bridge_infos), + &err); + if (err != NULL) { + error_propagate(errp, err); + return; } }