From patchwork Wed Nov 30 20:35:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 9454881 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 BF7126074E for ; Wed, 30 Nov 2016 20:36:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B47AB284A3 for ; Wed, 30 Nov 2016 20:36:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A8D94284A2; Wed, 30 Nov 2016 20:36:03 +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 4FB84284A2 for ; Wed, 30 Nov 2016 20:36:03 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2BE616E547; Wed, 30 Nov 2016 20:36:01 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wj0-x244.google.com (mail-wj0-x244.google.com [IPv6:2a00:1450:400c:c01::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id D45E36E522 for ; Wed, 30 Nov 2016 20:35:35 +0000 (UTC) Received: by mail-wj0-x244.google.com with SMTP id kp2so23890028wjc.0 for ; Wed, 30 Nov 2016 12:35:35 -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=ph+21MLzRHcVZgTAj3EII+SmK5yvX4d3e3nEMMmyDD4=; b=BMEchjLfVu2vdmz15fPAoYtpFPTc8uzEaZD1EA91qAu7vBZFb4Ytf/N6hldua2iESa dYe0T0Lp7nSs+hDnsFseDNfXIUp5QHQ/W8SYtoujvIdI58rI7kEIEMfbuFM8q684yHHY a2De0JwPhqlFSkyftBlq1NWh428epMfesKoW9Ia9ohU+PnyInCY0T/dufFjTVyE4FA8d tEOhPOe8H7cX48QtYWLdjDnlv9xdNLHGi3UlkA9VZkfLQBTNnHJxjzRscfNJeqieATpf YJabcdbxBItztuVWudd5ah6/X/nmHgkuVV2wHH2T2zwAo6z4cU5Bya3mxE4qviQE+q0u UemA== 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=ph+21MLzRHcVZgTAj3EII+SmK5yvX4d3e3nEMMmyDD4=; b=KDCxrEmNHNnUY34nDKo2zeM0T6S8q3iw/LM6ahmVqmz4/NED7n2j3t9zgWsh6aFFXy VWJ8KXVSi1e6tqkWY7sg3g8gY0ySSaaXoo/gr+G3HVwBRigjq4a2121mJEV5ICtBvppV rVYzqaBFSh8kfgryX9UxUYVLHeG5asPrvbjjLRrp3S3/mpSeZo8hha4MvBoYe9DEQ9qA YiEayhEzv03WTegPCY7f4kRsl5J/EDPCYzkkYg1r00qrPadUZcDJiK53sbcf4ppYStm4 qUOpNCfABzQdmq3c5FALBg7gxoUt7kl0SjTPjfHDfMaYyKAp7pXPZmWmEI2q94WUYPEM QbCQ== X-Gm-Message-State: AKaTC02VXKhgvi1/GgpOt53FA2IdBF60QYbCq7leFUOgO6zgigkcxU1UrMc3m+VzC6a5bg== X-Received: by 10.194.96.135 with SMTP id ds7mr30360436wjb.29.1480538134168; Wed, 30 Nov 2016 12:35:34 -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 ei2sm74572607wjd.47.2016.11.30.12.35.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Nov 2016 12:35:33 -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: Wed, 30 Nov 2016 20:35:09 +0000 Message-Id: <20161130203511.18910-3-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161130203511.18910-1-emil.l.velikov@gmail.com> References: <20161130203511.18910-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 --- 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;