From patchwork Tue Nov 1 18:13:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 9407839 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 46FA1600C8 for ; Tue, 1 Nov 2016 18:23:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3A9DD2995E for ; Tue, 1 Nov 2016 18:23:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2DFF529A41; Tue, 1 Nov 2016 18:23:54 +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 903052995E for ; Tue, 1 Nov 2016 18:23:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C0A186E280; Tue, 1 Nov 2016 18:23:51 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x244.google.com (mail-wm0-x244.google.com [IPv6:2a00:1450:400c:c09::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7D6296E280 for ; Tue, 1 Nov 2016 18:23:50 +0000 (UTC) Received: by mail-wm0-x244.google.com with SMTP id c17so23782793wmc.3 for ; Tue, 01 Nov 2016 11:23:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zElvrVXA3O72v7sOGJHXS9s5PQWrKRPirvC122nfuiE=; b=lf3NGpbAJhidTLm+qu1epg5zIB/Hr8TONEFAqGlpyNk9gwiWUiYyX8wTHzYb1HAVAN CI4SnO1psHsdbNZVi0/e3pmFSKkGpS3y2skO0pMQH02rLxX+XhQ9OXqte6uHsQmzVkvp 6/lK1Ehmwyek4vHFghe0kbMDDkkt9lO/wGkEqZkFN+dlELUHADdXYZkk45jl88DmuSOE AsjaYD+Y8w6omvHf9PSPLGQ3CmXX4rpvyypRINx8FzI5Z5Q6IRB0JxinGhNBxf9CliHV esHFPU6N59VSei4WGoMeuemqJEyAE6sAokRafjSs2sHq11MnWhRd1J4S21l6b/7HK9jJ oOhA== 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:mime-version :content-transfer-encoding; bh=zElvrVXA3O72v7sOGJHXS9s5PQWrKRPirvC122nfuiE=; b=V3vZMNiR7zoZf8HTRnrgwykksB5SKlHhOsQzCLyDNnMQKXwRTCu0GOXVWvw/3qpjdH gRLaN/FKJSxK9qDIj4Vf24lVBBgYV+xsLmz8b+wQQY7hO61i2Yd8uOnc4ODxBXyODqGB L22IRTbK7CmVyJ5NbUmaCaudb0eNkyC3dpqKuvzJG2vMetm1AwFGM3dSmPGoB9MewGQG hV100D5u/5Qio/Alupxdq/qMJ1OYtFQj0/eMZuVhD6mTgMjf7WXUDWrGJoBjlwxzbzQL M5rbVkUYM3Q35ilHxvPzVrj0Cch1gW4C+MtImmv4Pb2IvAj2P7F8KcVuz5lZ5z+LBQJv R49g== X-Gm-Message-State: ABUngveFNfDOlwZoOM2L1u6C2l8YhzJqIWyOtkJT3hVum2INPdsm2ommZzK3b1pwhaH7og== X-Received: by 10.194.243.167 with SMTP id wz7mr211704wjc.65.1478024629102; Tue, 01 Nov 2016 11:23:49 -0700 (PDT) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:1098:5:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id io3sm36984657wjb.24.2016.11.01.11.23.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Nov 2016 11:23:48 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm] xf86drm: Parse the separate files to retrieve the vendor/device info Date: Tue, 1 Nov 2016 18:13:34 +0000 Message-Id: <20161101181334.8225-1-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.10.0 MIME-Version: 1.0 Cc: =?UTF-8?q?Michel=20D=C3=A4nzer?= , emil.l.velikov@gmail.com, Mauro Santos 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 Parsing config sysfs file wakes up the device. The latter of which may be slow and isn't required to begin with. Reading through config is/was required since the revision is not available by other means, although with a kernel patch in the way we can 'cheat' temporarily. That should be fine, since no open-source project has ever used the value. Cc: Michel Dänzer Cc: Mauro Santos Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=98502 Signed-off-by: Emil Velikov --- Mauro can you apply this against libdrm and rebuild it. You do _not_ need to rebuild mesa afterwords. Thanks --- xf86drm.c | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index 52add5e..5a5100c 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -2950,25 +2950,45 @@ static int drmParsePciDeviceInfo(const char *d_name, drmPciDeviceInfoPtr device) { #ifdef __linux__ +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + static const char *attrs[] = { + "revision", /* XXX: make sure it's always first, see note below */ + "vendor", + "device", + "subsystem_vendor", + "subsystem_device", + }; char path[PATH_MAX + 1]; - unsigned char config[64]; - int fd, ret; + unsigned int data[ARRAY_SIZE(attrs)]; + FILE *fp; + int ret; - snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config", d_name); - fd = open(path, O_RDONLY); - if (fd < 0) - return -errno; + for (unsigned i = 0; i < ARRAY_SIZE(attrs); i++) { + snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/%s", + d_name, attrs[i]); + fp = fopen(path, "r"); + if (!fp) { + /* Note: First we check the revision, since older kernels + * may not have it. Default to zero in such cases. */ + if (i == 0) { + data[i] = 0; + continue; + } + return -errno; + } - ret = read(fd, config, sizeof(config)); - close(fd); - if (ret < 0) - return -errno; + ret = fscanf(fp, "%x", &data[i]); + fclose(fp); + if (ret != 1) + return -errno; + + } - device->vendor_id = config[0] | (config[1] << 8); - device->device_id = config[2] | (config[3] << 8); - device->revision_id = config[8]; - device->subvendor_id = config[44] | (config[45] << 8); - device->subdevice_id = config[46] | (config[47] << 8); + 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; #else