From patchwork Wed Nov 9 18:08:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 9419969 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 2D31C6048E for ; Wed, 9 Nov 2016 18:19:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 24E1128B67 for ; Wed, 9 Nov 2016 18:19:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 191232936B; Wed, 9 Nov 2016 18:19:31 +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=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, 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 7C09828B67 for ; Wed, 9 Nov 2016 18:19:30 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D9DAD6E0BE; Wed, 9 Nov 2016 18:19:27 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x242.google.com (mail-wm0-x242.google.com [IPv6:2a00:1450:400c:c09::242]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0850A6E0BE for ; Wed, 9 Nov 2016 18:19:26 +0000 (UTC) Received: by mail-wm0-x242.google.com with SMTP id u144so30882497wmu.0 for ; Wed, 09 Nov 2016 10:19:25 -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=AXzRRmPp7ukI7O+9UaNQVBwxx3WtiuW27PTEveAP3tk=; b=InEd6ftBuRyEPnbTGpIZ++s+wGstYiQdWj7a6AR9y+BtAlYWJHppz1WQ+Uw9C1xJ2f H9FxpBkS7vvZMxkZEceeaW+DsFx3zuU9l2hxhI/wtIQlI99ysmfp/hzbjViLXWszft4Z o9+D4krlye5pg9gBVQywpjZvtJNX87Zr47eYQdb5ow9e5EENJsKb/+y5ToG0ExHZaSye r08LIPhlPng6y8ZgcYcj1Z3SI9vrdtgw8tvXBJosf8J5gGorbTIVSSSzwX8ZyDGXfT1L cIhH0W6GDSYjjUL85C7zNz9bEpudwso/HcZxJ2kvDV2CZGMG15ZYtvluYFaAQl0kLivk 5agw== 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=AXzRRmPp7ukI7O+9UaNQVBwxx3WtiuW27PTEveAP3tk=; b=Gd1JRzzknibnXDZGCiKlzO1C7gR5MAB8zwryE7WjkiQmoJ1cJMYQWyLHXKKeTGDzbN 2h1raE7vlschkXsTQTPv00Ga2WZgzG3RELqX3+7Dky8QQ/dGByh28vXjShxocy2RAR7l hCiUY+RwqY+23+nRROuiq0nWAADrcdLniwDG9IiBnNeVNneYoX86O1CCtRvWUaNzEDN9 093GYykfbjjjJWXYo8r5WuBuD66P4BAdrRps2m67fOa/jjp18MzGPRUckiet4r04ACn4 Mxp4iEcHJzaEuM8NJokOtxd9rv0Tj7mKuB4bi0UdbEVZ3gUK/3Li7yY8T7eTea3y5bI4 6qKQ== X-Gm-Message-State: ABUngvdc6qmYYoLgKs/piGj+fuw+jERlh2aMa8Io0Oth27uDMjIDd57cj4ubzoFuzTInKw== X-Received: by 10.194.55.199 with SMTP id u7mr885053wjp.72.1478715564531; Wed, 09 Nov 2016 10:19:24 -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 w7sm8316533wmd.24.2016.11.09.10.19.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Nov 2016 10:19:23 -0800 (PST) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm v2] xf86drm: Parse the separate files to retrieve the vendor, ... info Date: Wed, 9 Nov 2016 18:08:50 +0000 Message-Id: <20161109180850.6012-1-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161101181334.8225-1-emil.l.velikov@gmail.com> References: <20161101181334.8225-1-emil.l.velikov@gmail.com> 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 that is about to change. Since returning 0 when one might expect a valid value is a no-go add a workaround drmDeviceUseRevisionFile() which one can use to say "I don't care if the revision file returns 0." v2: Complete rework - add new API to control the method, instead of changing it underneat the users' feet. Cc: Michel Dänzer Cc: Mauro Santos Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=98502 Signed-off-by: Emil Velikov --- I don't have a strong preference for/against this or v1. With this Mesa will require a 2 line patch. With v1 things will get fixed magically, when rebuilt against newer libdrm. Mauro I'll send the mesa patch in a second. Feel free to give it a spin. Thanks --- xf86drm.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- xf86drm.h | 11 ++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index 52add5e..676effc 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -113,6 +113,13 @@ void drmSetServerInfo(drmServerInfoPtr info) drm_server_info = info; } +static bool use_revision_file; + +void drmDeviceUseRevisionFile(void) +{ + use_revision_file = true; +} + /** * Output a message to stderr. * @@ -2946,10 +2953,56 @@ static int drmGetMaxNodeName(void) 3 /* length of the node number */; } -static int drmParsePciDeviceInfo(const char *d_name, - drmPciDeviceInfoPtr device) -{ #ifdef __linux__ +static int parse_separate_sysfs_files(const char *d_name, + drmPciDeviceInfoPtr device) +{ +#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 int data[ARRAY_SIZE(attrs)]; + FILE *fp; + int ret; + + 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 = 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(const char *d_name, + drmPciDeviceInfoPtr device) +{ char path[PATH_MAX + 1]; unsigned char config[64]; int fd, ret; @@ -2971,6 +3024,17 @@ static int drmParsePciDeviceInfo(const char *d_name, device->subdevice_id = config[46] | (config[47] << 8); return 0; +} +#endif + +static int drmParsePciDeviceInfo(const char *d_name, + drmPciDeviceInfoPtr device) +{ +#ifdef __linux__ + if (use_revision_file) + return parse_separate_sysfs_files(d_name, device); + + return parse_config_sysfs_file(d_name, device); #else #warning "Missing implementation of drmParsePciDeviceInfo" return -EINVAL; diff --git a/xf86drm.h b/xf86drm.h index 481d882..d1ebc32 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -796,6 +796,17 @@ extern void drmFreeDevice(drmDevicePtr *device); extern int drmGetDevices(drmDevicePtr devices[], int max_devices); extern void drmFreeDevices(drmDevicePtr devices[], int count); + +/** + * Force any sequential calls to drmGetDevice/drmGetDevices to use the + * revision sysfs file instead of config one. The latter wakes up the device + * if powered off. + * + * A value of 0 will be returned for the revision_id field if the sysfs file + * is missing. DO NOT USE THIS if you depend on a correct revision_id. + */ +extern void drmDeviceUseRevisionFile(void); + #if defined(__cplusplus) } #endif