From patchwork Fri Dec 2 16:32:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 9458879 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 B540C60515 for ; Fri, 2 Dec 2016 16:33:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A499828568 for ; Fri, 2 Dec 2016 16:33:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9967028575; Fri, 2 Dec 2016 16:33:13 +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=-4.1 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 35E9528568 for ; Fri, 2 Dec 2016 16:33:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6D3B26E97F; Fri, 2 Dec 2016 16:33:09 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x241.google.com (mail-wm0-x241.google.com [IPv6:2a00:1450:400c:c09::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1261A6E94D for ; Fri, 2 Dec 2016 16:33:06 +0000 (UTC) Received: by mail-wm0-x241.google.com with SMTP id u144so3531151wmu.0 for ; Fri, 02 Dec 2016 08:33:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=B4sc4qT6cH2WejKClFHP9J/B0yVDOQntiuEjY/nnd50=; b=VaOaecP8Q4b5o01pwCDQwg90nVdHJaMm3pFL5o6Fm/oE6pcf0Nv+mOBNFwM3bZVDpL q9Kof5LLRTxCrrnnDhiJA9h6z2ARE78kmKfalOSHHoe1DuKj7VP254M+uPN1yt3WvCiX VNSw62f5FhLCzhB0iUBCx8Y7dj7gHQ/xJKRnfhsLBd2e1Tat0Ri4bQ3bh5DfaUWQCcna 7Da62c0cXoBNMxdjRZXvGLXvt/wSFHCkGo9KdTwy2WPQE2eF3HVGQrgm7Aj77NvYAZnO RQjF6i5Vz63kJL5+TbrmMhNYZMhVAtXessRm2XEdLcr+8NSYSZWLO+7zUSH0wh3qrtyV lZ7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=B4sc4qT6cH2WejKClFHP9J/B0yVDOQntiuEjY/nnd50=; b=UziMb2cyHMsngTDzWTwX1ybcj3bIDiPv2b9gcO8U2jadyIsZ8b7lw5IOp421DjBfFA 56y1Kq5HCmvtF+PUT0cinTfh10HUjm+0DA4b5Hta91OTFnxAsmmktzVz84wcNjz3z6ee jH2rFd5YqEfTRKo3xQ59dDa/Ah2eeznBkCzhnHZ/ekaTeFoSUGPYM7fbYgM7/d8C4aha XfBruf6d9QCGd4JBAshGQJLd4RKiwqg2YeP22O0vcfYn9aCRZFmis+EJABjkH1AkzZEc /km6eaL90BUjkBmMiZDlGAeX4eAITk5Oc+rzUSi4i7PQpj5b9cgaKRXpB0lCOeZic20a fyQA== X-Gm-Message-State: AKaTC02myxW8ATvKOBQYzhOvfQ2hm5uUHo3O7l8J/RMNXZXo4ON9VCShmMEKky14jDFlpA== X-Received: by 10.28.152.79 with SMTP id a76mr3920703wme.47.1480696384378; Fri, 02 Dec 2016 08:33:04 -0800 (PST) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:1098:5:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id b7sm6331517wjm.39.2016.12.02.08.33.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Dec 2016 08:33:02 -0800 (PST) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm 3/5] xf86drm: parse the separate sysfs files for vendor... info Date: Fri, 2 Dec 2016 16:32:36 +0000 Message-Id: <20161202163238.15849-3-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161202163238.15849-1-emil.l.velikov@gmail.com> References: <20161202163238.15849-1-emil.l.velikov@gmail.com> MIME-Version: 1.0 Cc: Mauro Santos , =?UTF-8?q?Michel=20D=C3=A4nzer?= , emil.l.velikov@gmail.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Emil Velikov Up-to recently (patch should land in 4.10) the kernel did not expose the PCI device revision field as a separate sysfs file. Thus one needed too parse the config file to retrieve it. This in itself wakes up the device, which in some cases can be quite slow. To avoid that, just check for the separate files and fall-back to the original if kernel is not new enough. v3: rework alongside drmGetDevice[s]2 Cc: Michel Dänzer Cc: Nicolai Hähnle Cc: Mauro Santos Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=98502 Signed-off-by: Emil Velikov Reviewed-by: Michel Dänzer --- xf86drm.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index ddb8f9f..701cf29 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -2946,11 +2946,49 @@ static int drmGetMaxNodeName(void) 3 /* length of the node number */; } -static int drmParsePciDeviceInfo(int maj, int min, - drmPciDeviceInfoPtr device, - uint32_t flags) -{ #ifdef __linux__ +static int parse_separate_sysfs_files(int maj, int min, + drmPciDeviceInfoPtr device) +{ +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + static const char *attrs[] = { + "revision", /* Older kernels are missing the file, so check for it first */ + "vendor", + "device", + "subsystem_vendor", + "subsystem_device", + }; + char path[PATH_MAX + 1]; + unsigned int data[ARRAY_SIZE(attrs)]; + FILE *fp; + int ret; + + for (unsigned i = 0; i < ARRAY_SIZE(attrs); i++) { + snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/%s", maj, min, + attrs[i]); + fp = fopen(path, "r"); + if (!fp) + return -errno; + + ret = fscanf(fp, "%x", &data[i]); + fclose(fp); + if (ret != 1) + return -errno; + + } + + device->revision_id = data[0] & 0xff; + device->vendor_id = data[1] & 0xffff; + device->device_id = data[2] & 0xffff; + device->subvendor_id = data[3] & 0xffff; + device->subdevice_id = data[4] & 0xffff; + + return 0; +} + +static int parse_config_sysfs_file(int maj, int min, + drmPciDeviceInfoPtr device) +{ char path[PATH_MAX + 1]; unsigned char config[64]; int fd, ret; @@ -2972,6 +3010,18 @@ static int drmParsePciDeviceInfo(int maj, int min, device->subdevice_id = config[46] | (config[47] << 8); return 0; +} +#endif + +static int drmParsePciDeviceInfo(int maj, int min, + drmPciDeviceInfoPtr device, + uint32_t flags) +{ +#ifdef __linux__ + if (parse_separate_sysfs_files(maj, min, device)) + return parse_config_sysfs_file(maj, min, device); + + return 0; #else #warning "Missing implementation of drmParsePciDeviceInfo" return -EINVAL;