From patchwork Sat May 14 19:22:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Wu X-Patchwork-Id: 9095561 Return-Path: X-Original-To: patchwork-dri-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 E00F0BF29F for ; Sat, 14 May 2016 19:55:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AF23A20268 for ; Sat, 14 May 2016 19:55:34 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 72775201CE for ; Sat, 14 May 2016 19:55:33 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 74B476E12F; Sat, 14 May 2016 19:55:32 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail.lekensteyn.nl (mail.lekensteyn.nl [IPv6:2a02:2308::360:1:25]) by gabe.freedesktop.org (Postfix) with ESMTPS id D79166E2D6; Sat, 14 May 2016 19:22:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lekensteyn.nl; s=s2048-2015-q1; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=jzAGVdddWzAF0Z3ZGKzyOCgdp9+Yw0teAop9wRE+Cu4=; b=vNKbEdn6SNC8D55qEsVv0RmspVWQtBxbMa0Wakkgd+P1s0M9hWh5bsJsQk6CpIXnoStKM7EQvwZDuN0eglmXl/HETZKIg8y2zdzoUEiHT0VEmTVlNfZnTXLFEseufIwrOTO23vbWyS9NR3y1sIZREgYSMdoLgr/3yEGqhsoaxFYaRPzABpfIPQZRc2iPpk+i10abMrI1IFF1ju2kplfR1btnp9V9ZV9wKBILoESNUquGtkG+LA5D+s4I46ltBRN4qXtScjAhkARBjLIHZyk4iS5zLpnNWYJahk1OjR8ej1dDgA5n6fxytFM9i+YkcvSe6eQgQISIxGc83rxqQkcOuA==; Received: by lekensteyn.nl with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_CBC_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1b1f8w-00054Q-1S; Sat, 14 May 2016 21:22:42 +0200 From: Peter Wu To: nouveau@lists.freedesktop.org, David Airlie Subject: [PATCH v2] drm/nouveau: check function before using it Date: Sat, 14 May 2016 21:22:05 +0200 Message-Id: <1463253725-5859-1-git-send-email-peter@lekensteyn.nl> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1463244575-3515-1-git-send-email-peter@lekensteyn.nl> References: <1463244575-3515-1-git-send-email-peter@lekensteyn.nl> X-Spam-Score: -0.0 (/) X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Mailman-Approved-At: Sat, 14 May 2016 19:55:31 +0000 Cc: dri-devel@lists.freedesktop.org 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: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Do not unconditionally invoke function 0x1B without checking for its availability, it leads to an infinite loop on some firmware. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=104791 Fixes: 5addcf0a5f0fad ("nouveau: add runtime PM support (v0.9)") Signed-off-by: Peter Wu --- v2: only write optimus_funcs when an Optimus DSM is found. --- drivers/gpu/drm/nouveau/nouveau_acpi.c | 43 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_acpi.c b/drivers/gpu/drm/nouveau/nouveau_acpi.c index cdf5227..009712a 100644 --- a/drivers/gpu/drm/nouveau/nouveau_acpi.c +++ b/drivers/gpu/drm/nouveau/nouveau_acpi.c @@ -45,6 +45,7 @@ static struct nouveau_dsm_priv { bool dsm_detected; bool optimus_detected; + bool flags_func_detected; acpi_handle dhandle; acpi_handle rom_handle; } nouveau_dsm_priv; @@ -58,7 +59,6 @@ bool nouveau_is_v1_dsm(void) { } #define NOUVEAU_DSM_HAS_MUX 0x1 -#define NOUVEAU_DSM_HAS_OPT 0x2 #ifdef CONFIG_VGA_SWITCHEROO static const char nouveau_dsm_muid[] = { @@ -110,9 +110,9 @@ static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t * * requirements on the fourth parameter, so a private implementation * instead of using acpi_check_dsm(). */ -static int nouveau_check_optimus_dsm(acpi_handle handle) +static uint32_t nouveau_check_optimus_dsm(acpi_handle handle) { - int result; + uint32_t result; /* * Function 0 returns a Buffer containing available functions. @@ -123,9 +123,13 @@ static int nouveau_check_optimus_dsm(acpi_handle handle) /* * ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. - * If the n-th bit is enabled, function n is supported + * If the n-th bit is enabled, function n is supported. + * Check for both bit zero and the NOUVEAU_DSM_OPTIMUS_CAPS since + * some implementations return 0x80000001 on invalid parameters. */ - return result & 1 && result & (1 << NOUVEAU_DSM_OPTIMUS_CAPS); + if (result & 1 && result & (1 << NOUVEAU_DSM_OPTIMUS_CAPS)) + return result; + return 0; } static int nouveau_dsm(acpi_handle handle, int func, int arg) @@ -212,10 +216,11 @@ static const struct vga_switcheroo_handler nouveau_dsm_handler = { .get_client_id = nouveau_dsm_get_client_id, }; -static int nouveau_dsm_pci_probe(struct pci_dev *pdev) +static int nouveau_dsm_pci_probe(struct pci_dev *pdev, uint32_t *optimus_funcs) { acpi_handle dhandle; int retval = 0; + uint32_t supported_funcs; dhandle = ACPI_HANDLE(&pdev->dev); if (!dhandle) @@ -228,11 +233,10 @@ static int nouveau_dsm_pci_probe(struct pci_dev *pdev) 1 << NOUVEAU_DSM_POWER)) retval |= NOUVEAU_DSM_HAS_MUX; - if (nouveau_check_optimus_dsm(dhandle)) - retval |= NOUVEAU_DSM_HAS_OPT; - - if (retval & NOUVEAU_DSM_HAS_OPT) { + supported_funcs = nouveau_check_optimus_dsm(dhandle); + if (supported_funcs) { uint32_t result; + *optimus_funcs = supported_funcs; nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0, &result); dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n", @@ -252,7 +256,7 @@ static bool nouveau_dsm_detect(void) struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name}; struct pci_dev *pdev = NULL; int has_dsm = 0; - int has_optimus = 0; + uint32_t optimus_funcs = 0; int vga_count = 0; bool guid_valid; int retval; @@ -268,30 +272,28 @@ static bool nouveau_dsm_detect(void) while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) { vga_count++; - retval = nouveau_dsm_pci_probe(pdev); + retval = nouveau_dsm_pci_probe(pdev, &optimus_funcs); if (retval & NOUVEAU_DSM_HAS_MUX) has_dsm |= 1; - if (retval & NOUVEAU_DSM_HAS_OPT) - has_optimus = 1; } while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) { vga_count++; - retval = nouveau_dsm_pci_probe(pdev); + retval = nouveau_dsm_pci_probe(pdev, &optimus_funcs); if (retval & NOUVEAU_DSM_HAS_MUX) has_dsm |= 1; - if (retval & NOUVEAU_DSM_HAS_OPT) - has_optimus = 1; } /* find the optimus DSM or the old v1 DSM */ - if (has_optimus == 1) { + if (optimus_funcs) { acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, &buffer); printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n", acpi_method_name); nouveau_dsm_priv.optimus_detected = true; + nouveau_dsm_priv.flags_func_detected = + optimus_funcs & (1 << NOUVEAU_DSM_OPTIMUS_FLAGS); ret = true; } else if (vga_count == 2 && has_dsm && guid_valid) { acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, @@ -324,8 +326,9 @@ void nouveau_switcheroo_optimus_dsm(void) if (!nouveau_dsm_priv.optimus_detected) return; - nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS, - 0x3, &result); + if (nouveau_dsm_priv.flags_func_detected) + nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, + NOUVEAU_DSM_OPTIMUS_FLAGS, 0x3, &result); nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);