From patchwork Thu Feb 16 12:23:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 58009 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp275162wrn; Thu, 16 Feb 2023 04:38:07 -0800 (PST) X-Google-Smtp-Source: AK7set/dBoBPzVaeCybPJUJcitWJL8dWazjZtU405Fj69X0Cm2ZTLv4pSjpG/0SfBssUI5ey0R+9 X-Received: by 2002:a05:6402:201:b0:4ad:152c:865c with SMTP id t1-20020a056402020100b004ad152c865cmr3167018edv.9.1676551087224; Thu, 16 Feb 2023 04:38:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676551087; cv=none; d=google.com; s=arc-20160816; b=lyaPrEfraddOZbE7rhBrCY6peBwtSbBXddjuFGYqHCIyAuHnesar7ji6TPJ8qMmdLK FbMF1grM3+0A21ygzuX3AwZBRA4UUa7v4RyUASf9fSvOQAn++6sYVdbNw/+Jdurj08cv axcd9RJHMEcQdjgp8RIcoDv1pBcQZsSpcp+1oMAL3DlRxCcwwV9IzsWt1tzboEeMwV+g 8/dh/8QaQS9W+w9/7Gq86j5EzRbgd2+eTs/T/cx7kR2KaV5xmgo8XWL3GIZ25yuqAddH Ks3cdbRg3koPJS2B0rfpz0suIsNAm/1CveRSuIVUjJLuki9i8pfyjyYZy8UQ06Snevm5 sMRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IE7WxnAiIB7i2KDtpHqG2refvq1iHi/wQrOL9j6i0yk=; b=fPza0xyCpPksxP8TbVD5sBfQzyam/6T9LrQQ4psitDS1VIiAH9Vv21+bgdZAOGuqgH 6jMFjiegUB0mj7mZZeGrSMQZN6T4JneNgV/SGZgjjjqTUO9HTUWrLRu3nkY5Fi0vttrI Rvi6neP+bN/BIk4S/iPiSTBG516l7YOp2OAn1X+p4H3hUcLebhQIis54fHE32ESb5H7P 55nZt1eGBfCcwBZw4udEmxYXCu7ZSKdqU5RC8nl1rfClbH/7hIpyeWB478iP+/oodFP5 vz5r0u2/N620mpWazOvuglW6JtJCZiKEDNxWjOArFmIFcywgo8TSSEdSg7RjMcX03QsL rG0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=QfHdsO9C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v18-20020aa7cd52000000b004acdd3b83d2si1927032edw.14.2023.02.16.04.37.43; Thu, 16 Feb 2023 04:38:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=QfHdsO9C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230156AbjBPMZW (ORCPT + 99 others); Thu, 16 Feb 2023 07:25:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230149AbjBPMZP (ORCPT ); Thu, 16 Feb 2023 07:25:15 -0500 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B6F92CFD7; Thu, 16 Feb 2023 04:25:14 -0800 (PST) Received: by mail-pl1-x633.google.com with SMTP id e12so1849606plh.6; Thu, 16 Feb 2023 04:25:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IE7WxnAiIB7i2KDtpHqG2refvq1iHi/wQrOL9j6i0yk=; b=QfHdsO9CHPLRt+AgAY/jYgiWxy8Hl5uOAkVSHXo2QyUIV/hw+BnYYGFLEKFCwj+pCd 80SLOOxz5oNpIZ52Tvm4edbNWQUEU3Lb0Rq3eZcBpArguzKWa0yYUBr2BENRJvFMWGM3 QSte7j3/Z+nU3uGcZFiIaGB9HXCZxgs2BZp0WJgKaZ2abNMziwgq/Q2FGC5CclEsGfSl RsHuQc5ldLjijn344nzdQHZBj66gLS8mc6UTOHc9ri2+gqs2M3BMM/luYz3n2AXDPuJ4 7f5Ha5J1pEAfZcmH5PtqYofp+78w8vG6aUQIJ4DTvH5/K35xs7WkKr2NPAipCVIGzamX OxaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IE7WxnAiIB7i2KDtpHqG2refvq1iHi/wQrOL9j6i0yk=; b=tvFubqhCfHlgISMMhh0p6BR6MFFIf3bc8kjFuvqaB/1cKB6KlV7hXSY8eoIh/mU967 vPllcamtQp17P121GrEZ60sJP+/TlqX04s0WX5GZPjzH/rb16aSBEYvk5eQB23cB7ffd bwv12cA2YjZRdVikshYC/o8lQHd3AFigEayN5YkC2fesKMpyFmuaSSfqV+goTJn5HaHk +pr2DfI3Y3M5IP48dcf71ioAWLrcw39zwhrsWEn/F78EPsWyP4+eij/OjztGqz1DW2wX q1YfgqL4BBpzPQzrsXTlx6YvS8//kqIWm+um2B+nqzCYLgHz3v+5PfurFKI+uiG9W9P/ DlEQ== X-Gm-Message-State: AO0yUKX1Dk8jtx3F248t4Y7manJBBtDktM34L8cRGT4ndmFpGO/HsB5u S70SfodahJ4XCHIFGcZlqQY= X-Received: by 2002:a05:6a20:3b9b:b0:c7:3aa1:d528 with SMTP id b27-20020a056a203b9b00b000c73aa1d528mr773706pzh.37.1676550314089; Thu, 16 Feb 2023 04:25:14 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id c10-20020aa781ca000000b005a909290425sm1224055pfn.172.2023.02.16.04.25.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Feb 2023 04:25:13 -0800 (PST) From: Orlando Chamberlain To: Hans de Goede Cc: Mark Gross , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Lukas Wunner , Seth Forshee , Aditya Garg , Aun-Ali Zaidi , Kerem Karabay , Orlando Chamberlain Subject: [PATCH v2 1/5] apple-gmux: use first bit to check switch state Date: Thu, 16 Feb 2023 23:23:39 +1100 Message-Id: <20230216122342.5918-2-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230216122342.5918-1-orlandoch.dev@gmail.com> References: <20230216122342.5918-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757991233046252237?= X-GMAIL-MSGID: =?utf-8?q?1757991233046252237?= On T2 Macs with MMIO gmux, when GMUX_PORT_SWITCH_DISPLAY is read, it can have values of 2, 3, 4, and 5. Odd values correspond to the discrete gpu, and even values correspond to the integrated gpu. The current logic is that only 2 corresponds to IGD, but this doesn't work for T2 Macs. Instead, check the first bit to determine the connected gpu. As T2 Macs with gmux only can switch the internal display, it is untested if this change (or a similar change) would be applicable to GMUX_PORT_SWITCH_DDC and GMUX_PORT_SWITCH_EXTERNAL. Signed-off-by: Orlando Chamberlain --- v1->v2: no change drivers/platform/x86/apple-gmux.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index 9333f82cfa8a..ec99e05e532c 100644 --- a/drivers/platform/x86/apple-gmux.c +++ b/drivers/platform/x86/apple-gmux.c @@ -346,10 +346,10 @@ static void gmux_read_switch_state(struct apple_gmux_data *gmux_data) else gmux_data->switch_state_ddc = VGA_SWITCHEROO_DIS; - if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) == 2) - gmux_data->switch_state_display = VGA_SWITCHEROO_IGD; - else + if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_DISPLAY) & 1) gmux_data->switch_state_display = VGA_SWITCHEROO_DIS; + else + gmux_data->switch_state_display = VGA_SWITCHEROO_IGD; if (gmux_read8(gmux_data, GMUX_PORT_SWITCH_EXTERNAL) == 2) gmux_data->switch_state_external = VGA_SWITCHEROO_IGD; From patchwork Thu Feb 16 12:23:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 58011 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp275288wrn; Thu, 16 Feb 2023 04:38:23 -0800 (PST) X-Google-Smtp-Source: AK7set8cmXKI3ws4P4gomb2B6QqkA1VD+XDMMjHEZ7TBHTSNBLbHXiWffXFnLog4T6GOoMlC82I7 X-Received: by 2002:a17:906:a38e:b0:887:ebaa:7adb with SMTP id k14-20020a170906a38e00b00887ebaa7adbmr6272316ejz.12.1676551103672; Thu, 16 Feb 2023 04:38:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676551103; cv=none; d=google.com; s=arc-20160816; b=x2wfuTav10n5qAMN8BFLX04lfeDO3hwahXDWd138UBlGv91KMOW6O9csgpDVPaCflw TcSnx1ISWuirRziML+Qc19hu5rdxJPX09IO7DSMkD3vu0SIVgpfpEEqsn2ExoGftT8fA G++/0YIXiP3kY3EYOrlCER8B72q6fIEAnN/Oi1T4bXfcCzjvMnpKRiTWzncU/AYSPZHl HotBPrm3CLo/6GacK0NCSN/eU/4E9otbS4DMGKuZGxy9I1QwiS15YGaYi88EHmV3/oY4 LmbWQiN3S7vDX0z94vASkjHwYv+75Vy+piAspHugFuc0oRDh6GN0ulVD+2FQ/PdWa5lu w5CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XNM4GN7Ssv1yEmEmtO2H5I4AwlrGE/CL3Qb+Ouh4gek=; b=OMHwzTHqXGySuSShwKCbKRxqnxSjl9szSmLYjGK0kpGpE09UDmY4oxmm6NsiBdXcL1 laz8SCoEDMCOR65PKTI1OwMYAnM2khJIbDrzz4BaVDUcg0KQWP3sPbwkT+QT/jlelgtx i+2i3lULfIbgMK+WYZLbkypNb78zPJaNbK4VAZVb4YbvCTnrRrXRJZ5NB+HIgNZU0A4B jPWvYod3PAMHDLia0w57EvLLOAMjRhE2cjxXU4hjM9khKbdEB2icOJVFD0mSh23tkcRy +Bf67vzL3LN/+hfrTa2RYGIrNgxH/avHv7teTsCSRTi+Pmy/CDTqrHweYogh+0mI/RWq QKjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gBGm9QiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j23-20020a170906105700b008b149e22ef5si2260434ejj.430.2023.02.16.04.38.00; Thu, 16 Feb 2023 04:38:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=gBGm9QiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230185AbjBPMZZ (ORCPT + 99 others); Thu, 16 Feb 2023 07:25:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230172AbjBPMZV (ORCPT ); Thu, 16 Feb 2023 07:25:21 -0500 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B37D4A1FF; Thu, 16 Feb 2023 04:25:18 -0800 (PST) Received: by mail-pf1-x429.google.com with SMTP id bd35so1335560pfb.6; Thu, 16 Feb 2023 04:25:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XNM4GN7Ssv1yEmEmtO2H5I4AwlrGE/CL3Qb+Ouh4gek=; b=gBGm9QiL699dPCxZRgQ1UTNR+udBf7ihQmPiLF2dxHNaprBmtCm1OR/lxzTE8yEkBm Kiv8QjFODzjmYzpUbaY8Eztge5PdgDjes4WELprUdJLrJusaEnN+fuPxz44JW8n/IZUE g/r+7OU3KqpDgmlod24LBJlZ6XFVnzKMTGkO0QBiIb3XR982BIsnkxkkGaWP9MAFzLrp r5CRArRXLwCq//w6bXMjndyqiiw6h8Zo1fuca3ohXGY1cdp2865qhv7Mr/zRziN+VC56 9m6lU3B+ioTivSHEoJ2u6zq6ut1oAjTadLl++WH2yz5QYSGAXP2GzIQeE4QH39au1QaD fp5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XNM4GN7Ssv1yEmEmtO2H5I4AwlrGE/CL3Qb+Ouh4gek=; b=zrK1hE3jJqOhGQlm1S0g22wYBXL0jJIBNQ882DHHokjMBDuGdQ39cglQDxchHj35DQ BEkxiGeFaau79A9tSVue/4ZzaREVf5HyM8k8vA+o54lvnMBGyBrPsSzlIDOE7a1bUDLT GxDxCWOG88eofRiZinUS6d2JkXm8wuhyiB1cL/7Y/dd6wLHSwaJqDLLPoil0d7UXrttm z8PvJ1OvlHD6iEhu/5r/MiH48MAk/z2WpAKfE/iYiwP9vDHodHKwzZSampzeoU4Iny+k fhT0sz4mYXevwc8YmrM0VfsZS3WI+5186ihcqCUiEVW6ewEAZFTrzzbDmH3S/qoAE9NE uCFA== X-Gm-Message-State: AO0yUKWzitdSLO0sazFEM8GxZLuFNp+jr7CbGpoYfCE0Slth/ghNbWWr hV46G72K3n1L1vuClLbT7q0= X-Received: by 2002:aa7:9782:0:b0:5a8:aabf:46f4 with SMTP id o2-20020aa79782000000b005a8aabf46f4mr5365555pfp.8.1676550318017; Thu, 16 Feb 2023 04:25:18 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id c10-20020aa781ca000000b005a909290425sm1224055pfn.172.2023.02.16.04.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Feb 2023 04:25:17 -0800 (PST) From: Orlando Chamberlain To: Hans de Goede Cc: Mark Gross , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Lukas Wunner , Seth Forshee , Aditya Garg , Aun-Ali Zaidi , Kerem Karabay , Orlando Chamberlain Subject: [PATCH v2 2/5] apple-gmux: refactor gmux types Date: Thu, 16 Feb 2023 23:23:40 +1100 Message-Id: <20230216122342.5918-3-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230216122342.5918-1-orlandoch.dev@gmail.com> References: <20230216122342.5918-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757991250140607549?= X-GMAIL-MSGID: =?utf-8?q?1757991250140607549?= Add apple_gmux_config struct containing operations and data specific to each mux type. This is in preparation for adding a third, MMIO based, gmux type. Signed-off-by: Orlando Chamberlain --- v1->v2: Handle the two ways of reading the version as part of this type system (read_version_as_u32). drivers/platform/x86/apple-gmux.c | 93 ++++++++++++++++++++----------- include/linux/apple-gmux.h | 18 ++++-- 2 files changed, 74 insertions(+), 37 deletions(-) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index ec99e05e532c..36208e93d745 100644 --- a/drivers/platform/x86/apple-gmux.c +++ b/drivers/platform/x86/apple-gmux.c @@ -5,6 +5,7 @@ * Copyright (C) Canonical Ltd. * Copyright (C) 2010-2012 Andreas Heider * Copyright (C) 2015 Lukas Wunner + * Copyright (C) 2023 Orlando Chamberlain */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt @@ -43,10 +44,12 @@ * http://www.renesas.com/products/mpumcu/h8s/h8s2100/h8s2113/index.jsp */ +struct apple_gmux_config; + struct apple_gmux_data { unsigned long iostart; unsigned long iolen; - bool indexed; + const struct apple_gmux_config *config; struct mutex index_lock; struct backlight_device *bdev; @@ -64,6 +67,18 @@ struct apple_gmux_data { static struct apple_gmux_data *apple_gmux_data; +struct apple_gmux_config { + u8 (*read8)(struct apple_gmux_data *gmux_data, int port); + void (*write8)(struct apple_gmux_data *gmux_data, int port, u8 val); + u32 (*read32)(struct apple_gmux_data *gmux_data, int port); + void (*write32)(struct apple_gmux_data *gmux_data, int port, u32 val); + const struct vga_switcheroo_handler *gmux_handler; + enum vga_switcheroo_handler_flags_t handler_flags; + unsigned long resource_type; + bool read_version_as_u32; + char *name; +}; + #define GMUX_INTERRUPT_ENABLE 0xff #define GMUX_INTERRUPT_DISABLE 0x00 @@ -195,35 +210,23 @@ static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port, static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port) { - if (gmux_data->indexed) - return gmux_index_read8(gmux_data, port); - else - return gmux_pio_read8(gmux_data, port); + return gmux_data->config->read8(gmux_data, port); } static void gmux_write8(struct apple_gmux_data *gmux_data, int port, u8 val) { - if (gmux_data->indexed) - gmux_index_write8(gmux_data, port, val); - else - gmux_pio_write8(gmux_data, port, val); + return gmux_data->config->write8(gmux_data, port, val); } static u32 gmux_read32(struct apple_gmux_data *gmux_data, int port) { - if (gmux_data->indexed) - return gmux_index_read32(gmux_data, port); - else - return gmux_pio_read32(gmux_data, port); + return gmux_data->config->read32(gmux_data, port); } static void gmux_write32(struct apple_gmux_data *gmux_data, int port, u32 val) { - if (gmux_data->indexed) - gmux_index_write32(gmux_data, port, val); - else - gmux_pio_write32(gmux_data, port, val); + return gmux_data->config->write32(gmux_data, port, val); } /** @@ -463,19 +466,43 @@ static enum vga_switcheroo_client_id gmux_get_client_id(struct pci_dev *pdev) return VGA_SWITCHEROO_DIS; } -static const struct vga_switcheroo_handler gmux_handler_indexed = { +static const struct vga_switcheroo_handler gmux_handler_no_ddc = { .switchto = gmux_switchto, .power_state = gmux_set_power_state, .get_client_id = gmux_get_client_id, }; -static const struct vga_switcheroo_handler gmux_handler_classic = { +static const struct vga_switcheroo_handler gmux_handler_ddc = { .switchto = gmux_switchto, .switch_ddc = gmux_switch_ddc, .power_state = gmux_set_power_state, .get_client_id = gmux_get_client_id, }; +static const struct apple_gmux_config apple_gmux_pio = { + .read8 = &gmux_pio_read8, + .write8 = &gmux_pio_write8, + .read32 = &gmux_pio_read32, + .write32 = &gmux_pio_write32, + .gmux_handler = &gmux_handler_ddc, + .handler_flags = VGA_SWITCHEROO_CAN_SWITCH_DDC, + .resource_type = IORESOURCE_IO, + .read_version_as_u32 = false, + .name = "classic" +}; + +static const struct apple_gmux_config apple_gmux_index = { + .read8 = &gmux_index_read8, + .write8 = &gmux_index_write8, + .read32 = &gmux_index_read32, + .write32 = &gmux_index_write32, + .gmux_handler = &gmux_handler_no_ddc, + .handler_flags = VGA_SWITCHEROO_NEEDS_EDP_CONFIG, + .resource_type = IORESOURCE_IO, + .read_version_as_u32 = true, + .name = "indexed" +}; + /** * DOC: Interrupt * @@ -565,13 +592,13 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) int ret = -ENXIO; acpi_status status; unsigned long long gpe; - bool indexed = false; + enum apple_gmux_type type; u32 version; if (apple_gmux_data) return -EBUSY; - if (!apple_gmux_detect(pnp, &indexed)) { + if (!apple_gmux_detect(pnp, &type)) { pr_info("gmux device not present\n"); return -ENODEV; } @@ -581,6 +608,16 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) return -ENOMEM; pnp_set_drvdata(pnp, gmux_data); + switch (type) { + case APPLE_GMUX_TYPE_INDEXED: + gmux_data->config = &apple_gmux_index; + mutex_init(&gmux_data->index_lock); + break; + case APPLE_GMUX_TYPE_PIO: + gmux_data->config = &apple_gmux_pio; + break; + } + res = pnp_get_resource(pnp, IORESOURCE_IO, 0); gmux_data->iostart = res->start; gmux_data->iolen = resource_size(res); @@ -591,9 +628,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) goto err_free; } - if (indexed) { - mutex_init(&gmux_data->index_lock); - gmux_data->indexed = true; + if (gmux_data->config->read_version_as_u32) { version = gmux_read32(gmux_data, GMUX_PORT_VERSION_MAJOR); ver_major = (version >> 24) & 0xff; ver_minor = (version >> 16) & 0xff; @@ -604,7 +639,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) ver_release = gmux_read8(gmux_data, GMUX_PORT_VERSION_RELEASE); } pr_info("Found gmux version %d.%d.%d [%s]\n", ver_major, ver_minor, - ver_release, (gmux_data->indexed ? "indexed" : "classic")); + ver_release, gmux_data->config->name); memset(&props, 0, sizeof(props)); props.type = BACKLIGHT_PLATFORM; @@ -694,12 +729,8 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) * * Pre-retina MacBook Pros can switch the panel's DDC separately. */ - if (gmux_data->indexed) - ret = vga_switcheroo_register_handler(&gmux_handler_indexed, - VGA_SWITCHEROO_NEEDS_EDP_CONFIG); - else - ret = vga_switcheroo_register_handler(&gmux_handler_classic, - VGA_SWITCHEROO_CAN_SWITCH_DDC); + ret = vga_switcheroo_register_handler(gmux_data->config->gmux_handler, + gmux_data->config->handler_flags); if (ret) { pr_err("Failed to register vga_switcheroo handler\n"); goto err_register_handler; diff --git a/include/linux/apple-gmux.h b/include/linux/apple-gmux.h index 1f68b49bcd68..5f658439f7f8 100644 --- a/include/linux/apple-gmux.h +++ b/include/linux/apple-gmux.h @@ -36,6 +36,11 @@ #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4) +enum apple_gmux_type { + APPLE_GMUX_TYPE_PIO, + APPLE_GMUX_TYPE_INDEXED +}; + #if IS_ENABLED(CONFIG_APPLE_GMUX) static inline bool apple_gmux_is_indexed(unsigned long iostart) { @@ -65,13 +70,13 @@ static inline bool apple_gmux_is_indexed(unsigned long iostart) * Return: %true if a supported gmux ACPI device is detected and the kernel * was configured with CONFIG_APPLE_GMUX, %false otherwise. */ -static inline bool apple_gmux_detect(struct pnp_dev *pnp_dev, bool *indexed_ret) +static inline bool apple_gmux_detect(struct pnp_dev *pnp_dev, enum apple_gmux_type *type_ret) { u8 ver_major, ver_minor, ver_release; struct device *dev = NULL; struct acpi_device *adev; struct resource *res; - bool indexed = false; + enum apple_gmux_type type = APPLE_GMUX_TYPE_PIO; bool ret = false; if (!pnp_dev) { @@ -99,13 +104,14 @@ static inline bool apple_gmux_detect(struct pnp_dev *pnp_dev, bool *indexed_ret) ver_minor = inb(res->start + GMUX_PORT_VERSION_MINOR); ver_release = inb(res->start + GMUX_PORT_VERSION_RELEASE); if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) { - indexed = apple_gmux_is_indexed(res->start); - if (!indexed) + if (apple_gmux_is_indexed(res->start)) + type = APPLE_GMUX_TYPE_INDEXED; + else goto out; } - if (indexed_ret) - *indexed_ret = indexed; + if (type_ret) + *type_ret = type; ret = true; out: From patchwork Thu Feb 16 12:23:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 58012 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp275317wrn; Thu, 16 Feb 2023 04:38:28 -0800 (PST) X-Google-Smtp-Source: AK7set+0HT1ni7kGdl31HZ34BOuKky2cVinX59slbYIsrRkz/OqofKVdGL+7xLa3xbjM+9sXuVvV X-Received: by 2002:a17:906:5946:b0:8b1:3038:e81f with SMTP id g6-20020a170906594600b008b13038e81fmr5897549ejr.50.1676551108223; Thu, 16 Feb 2023 04:38:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676551108; cv=none; d=google.com; s=arc-20160816; b=xi4M/apTKKzZ/3uVCdCzUr1jgV9wo0ZW0slxyoV5tBntmhLdln4rVfEUP2aAwVTaRH z42daji+nnBEJcfZlrcbtSxcg00mkA8HFk1MNrRTOVxEM8qd2pHmxXeybHSWWPkX9P0s DswRwWMwcbe+fOlEwU2JFVTiJe02T1EKCNNxhOiH3WC24c2DmpzjMoH1dIjsm6qTfMQe CfSU9bxVUbzSVaDGmNZZ3i+JTpLAlI3gPnK+2P1YlsdM1spNS3c5ba9MeIt99DDhuIR9 BHuQopRpMuWXaHnMxmk6lOc8zWNV8YyXc0HOW5ex2rUVC2TKXjfnqgPk3fHoPxPU524O XcdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cV5gSZbAk+OK4o2s0sGW4RLDMGE24woGtYndT0R/1so=; b=HqJHMjr/8n9dhoCHAqx0QmUNzd83mOdEgZuK9cJYDXUqhTALXuI9uWIp7IY6lI8fAs aP49HdbgQMdFpwzFNy5CxikClUpBlbSiPkaW6gxH3h6yE3a4sgtTZGvzwtzJh/VzTVOW HP1J+816LgT7siTRlJXmAsal14166bhxjZs0yYIvTOFiK9XauuVzBpDp5UaT2gpzYdsd FnKe/NFEao+qBmlN9gAxflmVWOPHWF9G6zoDYFCmmTNkSNTtxNaQxsYqLaiKGhgclg+H ldxeCbQvAIiZsiDFljtcy/nZJDUgVUBA30Mehqz7vtDgJCWGf7X8TcENu4As+ZFdi2IF BaAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=aiP8Gui+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id uj28-20020a170907c99c00b0088bba5ae982si1818142ejc.140.2023.02.16.04.38.05; Thu, 16 Feb 2023 04:38:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=aiP8Gui+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230204AbjBPMZ2 (ORCPT + 99 others); Thu, 16 Feb 2023 07:25:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230166AbjBPMZX (ORCPT ); Thu, 16 Feb 2023 07:25:23 -0500 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 439A92D150; Thu, 16 Feb 2023 04:25:22 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id ja21so1814624plb.13; Thu, 16 Feb 2023 04:25:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cV5gSZbAk+OK4o2s0sGW4RLDMGE24woGtYndT0R/1so=; b=aiP8Gui+ZfrGDh/k74j4XykYop7KbIq6K+9dHNcBJ0e+9iMDfxlkDF+31txhH6GLxY iQujF3TsQgE9ve0hvj0GX+bF+ncNsIDFx18qNQfm0S+ldzG341W17q1pm+IvCghRNzWp KzTDhMWO6WTO9ooW8CKJfo/yC5ESUD76658oGpEPXHQLq/6EdwktEyvZK1nxEhg//rcz vHiJmUOdBI41ZxL2PTrJtkDcMkQMnVme7poxaq/UEFjLiPGVHiI59LBQgiCe+f/jWRXj zUpo7snaDK81yK5vlTWoB5LccLGcIGN1o4DTGA9E3wzidcJTOBA1m98HB67zw47jlx9I PcVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cV5gSZbAk+OK4o2s0sGW4RLDMGE24woGtYndT0R/1so=; b=QNxKU6QtCZjzXXUxeQv01w8/RpJad2GXtGylldod60HdCmjWKYBPBHpl77Wf9r0kUK sL41XVnyndxbBost1YdKcjoQFUcbuU7/snH4HHosTS35/ZE/7RX5VASZD9vXLU9BJBeU 4M94OVTxKUju8Cme6qWJbptGtuEBtFj4eyQZnCyGWSZVJXYEJikaYjH7cgF6DqZnHfMo LVKJuS2BT8sW2LUtUXAvfNv4tESQwYYOksDhZUZyOVmJCP4nsRiAI995A3muHbXkVVRe tuQlwZsR7DARHctKgFaVl/7UjU2rJ0CwdvLhbA39xtGUuhaHITvoteTYKgHR16xSSfMv rsnQ== X-Gm-Message-State: AO0yUKUgoHt0bX41HYdOAmBjE7bYdKIuUPUFWM9Nt4q+tNGn4scGyTrA f+RGQ4i4YHE/hUDAj83KA8w= X-Received: by 2002:a05:6a20:728f:b0:bc:ce49:798d with SMTP id o15-20020a056a20728f00b000bcce49798dmr6571360pzk.41.1676550321820; Thu, 16 Feb 2023 04:25:21 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id c10-20020aa781ca000000b005a909290425sm1224055pfn.172.2023.02.16.04.25.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Feb 2023 04:25:21 -0800 (PST) From: Orlando Chamberlain To: Hans de Goede Cc: Mark Gross , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Lukas Wunner , Seth Forshee , Aditya Garg , Aun-Ali Zaidi , Kerem Karabay , Orlando Chamberlain Subject: [PATCH v2 3/5] apple-gmux: Use GMSP acpi method for interrupt clear Date: Thu, 16 Feb 2023 23:23:41 +1100 Message-Id: <20230216122342.5918-4-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230216122342.5918-1-orlandoch.dev@gmail.com> References: <20230216122342.5918-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757991255120079749?= X-GMAIL-MSGID: =?utf-8?q?1757991255120079749?= This is needed for interrupts to be cleared correctly on MMIO based gmux's. It is untested if this helps/hinders other gmux types, so currently this is only enabled for the MMIO gmux's. There is also a "GMLV" acpi method, and the "GMSP" method can be called with 1 as its argument, but the purposes of these aren't known and they don't seem to be needed. Signed-off-by: Orlando Chamberlain --- v1->v2: Only enable this on MMIO gmux's drivers/platform/x86/apple-gmux.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index 36208e93d745..12a93fc49c36 100644 --- a/drivers/platform/x86/apple-gmux.c +++ b/drivers/platform/x86/apple-gmux.c @@ -76,6 +76,7 @@ struct apple_gmux_config { enum vga_switcheroo_handler_flags_t handler_flags; unsigned long resource_type; bool read_version_as_u32; + bool use_acpi_gmsp; char *name; }; @@ -488,6 +489,7 @@ static const struct apple_gmux_config apple_gmux_pio = { .handler_flags = VGA_SWITCHEROO_CAN_SWITCH_DDC, .resource_type = IORESOURCE_IO, .read_version_as_u32 = false, + .use_acpi_gmsp = false, .name = "classic" }; @@ -500,6 +502,7 @@ static const struct apple_gmux_config apple_gmux_index = { .handler_flags = VGA_SWITCHEROO_NEEDS_EDP_CONFIG, .resource_type = IORESOURCE_IO, .read_version_as_u32 = true, + .use_acpi_gmsp = false, .name = "indexed" }; @@ -511,8 +514,29 @@ static const struct apple_gmux_config apple_gmux_index = { * MCP79, on all following generations it's GPIO pin 6 of the Intel PCH. * The GPE merely signals that an interrupt occurred, the actual type of event * is identified by reading a gmux register. + * + * On MMIO gmux's, we also need to call the acpi method GMSP to properly clear + * interrupts. */ +static int gmux_call_acpi_gmsp(struct apple_gmux_data *gmux_data, int arg) +{ + acpi_status status = AE_OK; + union acpi_object arg0 = { ACPI_TYPE_INTEGER }; + struct acpi_object_list arg_list = { 1, &arg0 }; + + arg0.integer.value = arg; + + status = acpi_evaluate_object(gmux_data->dhandle, "GMSP", &arg_list, NULL); + if (ACPI_FAILURE(status)) { + pr_err("GMSP call failed: %s\n", + acpi_format_exception(status)); + return -ENODEV; + } + + return 0; +} + static inline void gmux_disable_interrupts(struct apple_gmux_data *gmux_data) { gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_ENABLE, @@ -536,7 +560,11 @@ static void gmux_clear_interrupts(struct apple_gmux_data *gmux_data) /* to clear interrupts write back current status */ status = gmux_interrupt_get_status(gmux_data); - gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status); + if (status) { + gmux_write8(gmux_data, GMUX_PORT_INTERRUPT_STATUS, status); + if (gmux_data->config->use_acpi_gmsp) + gmux_call_acpi_gmsp(gmux_data, 0); + } } static void gmux_notify_handler(acpi_handle device, u32 value, void *context) From patchwork Thu Feb 16 12:23:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 58007 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp271449wrn; Thu, 16 Feb 2023 04:30:36 -0800 (PST) X-Google-Smtp-Source: AK7set8942Q2PYRyRZS4cG3AQWRFTgAv0sca9M2IiG0qF8AnKdKbL4TwvYrIM/O9xTZOveLS8uf6 X-Received: by 2002:a17:90b:3b4d:b0:234:a299:b50a with SMTP id ot13-20020a17090b3b4d00b00234a299b50amr1022596pjb.2.1676550635878; Thu, 16 Feb 2023 04:30:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676550635; cv=none; d=google.com; s=arc-20160816; b=gzj7hEVqbJ7U+gVg7E5hpDVBD3+s2S/KHyyrrR5VHVG2XYX+sC3iKlpJts0o+SKMlK ry9s8dGQelegR2bWoav+Z7ND1zeHsU4m3nCWIeZFD8zpXguLekmJ4pst720NI+5txxHp Sn3M2dJ7Q/GkQO9f85N9ovTQgNs42pX9I/hKeeTKw7Y8bNl+sZO069PXKRRRm6gvSyhx UqBEpBZz0DX/3L+W1ExPKE3hQ0WJPVgBopwFNOC8mRghIHktqjzPxFdlm3hju4Ujjpk8 Y4j7g9yIUVaxStBzL17KQlo54EjRVYXL+3yU3mYMucEXH3ITnL1sFwCBArDm6u7lrr3O aSXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=n2AvMjaSjaeMLAOmSRivE95PUspyvUwC1tbdchn+oFU=; b=E0IhRZ/8/XKMx/4JDC4HcxR2Dun37U55idARj2GCvVe/F/Rx8yW9jIVzCjiP6NvNaa hrpfNqQA/PL/Y5mjEDGzh38I5KHbNczT754YOImL1baDk4J34XhA4EaUqoWCygM5UPKy gCYmHwdwZPL/VeS7gXpzgJNf8RbXIcku9dTKk7M5SPak8ZEQkRE//MAmS5HiANeJrSjq 5n22UcVqRSQCFRuZwHVb5gTUmFZbjW1gMZL+THvpZrTGhm0pQiUYYeV9AgmrCvWddoGk FAtvvERHJ1LBw0uZrGFAnVxD3Y7IzwQp6L8+ogexiLYr48j7W3H+FgSqHUEqxnWA1zMX 9epA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="mn9w/Vo9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e7-20020a17090a630700b002333adf6321si4683524pjj.47.2023.02.16.04.30.22; Thu, 16 Feb 2023 04:30:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="mn9w/Vo9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230229AbjBPMZk (ORCPT + 99 others); Thu, 16 Feb 2023 07:25:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230227AbjBPMZd (ORCPT ); Thu, 16 Feb 2023 07:25:33 -0500 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EFF357774; Thu, 16 Feb 2023 04:25:26 -0800 (PST) Received: by mail-pf1-x432.google.com with SMTP id r3so1339857pfh.4; Thu, 16 Feb 2023 04:25:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n2AvMjaSjaeMLAOmSRivE95PUspyvUwC1tbdchn+oFU=; b=mn9w/Vo93CWNQGIZzLIYp25j8xvCDa+5o4BDRmpm1EKFxSwOhSBmkNZzg22msNe82C H0UOKJ/dAcfSORpOkqdg5YgqOF3wHy/PABxrpzXJFAstvcHLWy8ey33tRDKFU5NO8+KN 4+FtL4bo0U4sAoH6FNka/U//56nXRUjLQo81JUxrhNbndc1qFyuR13SkgH3xw+wSSXtZ DpVAnTUsgmJjholFDFDxaZmg51S4MIErg3EtgxfDkY24SJ5yrGiNkoKpUrqC4OqjcWvN 8F8ynhlIBQztYY8/CLn1ybFnNHGDUHbnXHbQbu+JZT6sDXbwEMAFTbghtxbVBN7nFY6h 22bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n2AvMjaSjaeMLAOmSRivE95PUspyvUwC1tbdchn+oFU=; b=wSJCZHxP3eEtKw07WcY7tFvES7ppQQsAtLcES613fdUwCDjlPYgxfK0eYDk7SoD+U2 x0xkHtAXJfU2UvGEtCkwws4cjEeGud29Uaq+t49jsoGQIASzX9Bj4WRhA00+umybL2Kc za5mnKJh5ilZSlqEJLYgwgidtOQj4w9pYLSn77QFmeUX1wB48ILqvG62eOD+uugUcs32 P1QxnhJga8nupgWuzGefWw08nKwnWP6PmLAOEfsIS5WMOssydU+zfSeqHy1JmZQ7BzbV rpg743kaXGTaTrsR26Yx44k0MgZpjf0ajbMarERyZMgDhFYb1UjjL4pGMhzYdodB+qvG oheQ== X-Gm-Message-State: AO0yUKWcZqZoRPAnwdHgbrhu01LJqq7HuXWBQliz3wV3t6py8IXS9rmx uOZeb5XFUZ/m4idjke3ffSY= X-Received: by 2002:a05:6a00:348d:b0:5a8:ac15:534d with SMTP id cp13-20020a056a00348d00b005a8ac15534dmr1735784pfb.13.1676550325747; Thu, 16 Feb 2023 04:25:25 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id c10-20020aa781ca000000b005a909290425sm1224055pfn.172.2023.02.16.04.25.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Feb 2023 04:25:25 -0800 (PST) From: Orlando Chamberlain To: Hans de Goede Cc: Mark Gross , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Lukas Wunner , Seth Forshee , Aditya Garg , Aun-Ali Zaidi , Kerem Karabay , Orlando Chamberlain Subject: [PATCH v2 4/5] apple-gmux: support MMIO gmux on T2 Macs Date: Thu, 16 Feb 2023 23:23:42 +1100 Message-Id: <20230216122342.5918-5-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230216122342.5918-1-orlandoch.dev@gmail.com> References: <20230216122342.5918-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757990759581549695?= X-GMAIL-MSGID: =?utf-8?q?1757990759581549695?= In some newer dual gpu MacBooks, gmux is controlled by the T2 security chip, and acessed with MMIO. Add support for these gmux controllers Interestingly, the ACPI table only allocates 8 bytes for GMUX, but we actually need 16, and as such we request 16 with request_mem_region. Reading and writing from ports: 16 bytes from 0xfe0b0200 are used. 0x0 to 0x4 are where data to read appears, and where data to write goes. Writing to 0xe sets the gmux port being accessed, and writing to 0xf sends commands. These commands are 0x40 & data_length for write, and data_length for read, where data_length is 1, 2 or 4. Once byte base+0xf is 0, the command is done. Issues: As with other retina models, we can't switch DDC lines so switching at runtime doesn't work if the inactive gpu driver already disabled eDP due to it not being connected when that driver loaded. Additionally, turning on the dgpu back on the MacBookPro16,1 does not work. Signed-off-by: Orlando Chamberlain --- v1->v2: Document some chips present, and clarify which chips aren't present on MMIO gmux laptops. drivers/platform/x86/apple-gmux.c | 142 +++++++++++++++++++++++++++--- include/linux/apple-gmux.h | 40 ++++++--- 2 files changed, 158 insertions(+), 26 deletions(-) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index 12a93fc49c36..5bac6dcfada0 100644 --- a/drivers/platform/x86/apple-gmux.c +++ b/drivers/platform/x86/apple-gmux.c @@ -28,15 +28,17 @@ * DOC: Overview * * gmux is a microcontroller built into the MacBook Pro to support dual GPUs: - * A `Lattice XP2`_ on pre-retinas, a `Renesas R4F2113`_ on retinas. + * A `Lattice XP2`_ on pre-retinas, a `Renesas R4F2113`_ on pre-T2 retinas. + * The chip used on T2 Macs is not known. * * (The MacPro6,1 2013 also has a gmux, however it is unclear why since it has * dual GPUs but no built-in display.) * * gmux is connected to the LPC bus of the southbridge. Its I/O ports are * accessed differently depending on the microcontroller: Driver functions - * to access a pre-retina gmux are infixed ``_pio_``, those for a retina gmux - * are infixed ``_index_``. + * to access a pre-retina gmux are infixed ``_pio_``, those for a pre-T2 + * retina gmux are infixed ``_index_``, and those on T2 Macs are infixed + * with ``_mmio_``. * * .. _Lattice XP2: * http://www.latticesemi.com/en/Products/FPGAandCPLD/LatticeXP2.aspx @@ -47,6 +49,7 @@ struct apple_gmux_config; struct apple_gmux_data { + u8 *__iomem iomem_base; unsigned long iostart; unsigned long iolen; const struct apple_gmux_config *config; @@ -209,6 +212,79 @@ static void gmux_index_write32(struct apple_gmux_data *gmux_data, int port, mutex_unlock(&gmux_data->index_lock); } +static int gmux_mmio_wait(struct apple_gmux_data *gmux_data) +{ + int i = 200; + u8 gwr = ioread8(gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND); + + while (i && gwr) { + gwr = ioread8(gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND); + udelay(100); + i--; + } + + return !!i; +} + +static u8 gmux_mmio_read8(struct apple_gmux_data *gmux_data, int port) +{ + u8 val; + + mutex_lock(&gmux_data->index_lock); + gmux_mmio_wait(gmux_data); + iowrite8((port & 0xff), gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT); + iowrite8(GMUX_MMIO_READ | sizeof(val), + gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND); + gmux_mmio_wait(gmux_data); + val = ioread8(gmux_data->iomem_base); + mutex_unlock(&gmux_data->index_lock); + + return val; +} + +static void gmux_mmio_write8(struct apple_gmux_data *gmux_data, int port, + u8 val) +{ + mutex_lock(&gmux_data->index_lock); + gmux_mmio_wait(gmux_data); + iowrite8(val, gmux_data->iomem_base); + + iowrite8(port & 0xff, gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT); + iowrite8(GMUX_MMIO_WRITE | sizeof(val), + gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND); + + gmux_mmio_wait(gmux_data); + mutex_unlock(&gmux_data->index_lock); +} + +static u32 gmux_mmio_read32(struct apple_gmux_data *gmux_data, int port) +{ + u32 val; + + mutex_lock(&gmux_data->index_lock); + gmux_mmio_wait(gmux_data); + iowrite8((port & 0xff), gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT); + iowrite8(GMUX_MMIO_READ | sizeof(val), + gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND); + gmux_mmio_wait(gmux_data); + val = be32_to_cpu(ioread32(gmux_data->iomem_base)); + mutex_unlock(&gmux_data->index_lock); + + return val; +} + +static void gmux_mmio_write32(struct apple_gmux_data *gmux_data, int port, + u32 val) +{ + mutex_lock(&gmux_data->index_lock); + iowrite32(cpu_to_be32(val), gmux_data->iomem_base); + iowrite8(port & 0xff, gmux_data->iomem_base + GMUX_MMIO_PORT_SELECT); + iowrite8(GMUX_MMIO_WRITE | sizeof(val), + gmux_data->iomem_base + GMUX_MMIO_COMMAND_SEND); + gmux_mmio_wait(gmux_data); + mutex_unlock(&gmux_data->index_lock); +} + static u8 gmux_read8(struct apple_gmux_data *gmux_data, int port) { return gmux_data->config->read8(gmux_data, port); @@ -237,8 +313,8 @@ static void gmux_write32(struct apple_gmux_data *gmux_data, int port, * the GPU. On dual GPU MacBook Pros by contrast, either GPU may be suspended * to conserve energy. Hence the PWM signal needs to be generated by a separate * backlight driver which is controlled by gmux. The earliest generation - * MBP5 2008/09 uses a `TI LP8543`_ backlight driver. All newer models - * use a `TI LP8545`_. + * MBP5 2008/09 uses a `TI LP8543`_ backlight driver. Newer models + * use a `TI LP8545`_ or a TI LP8548. * * .. _TI LP8543: https://www.ti.com/lit/ds/symlink/lp8543.pdf * .. _TI LP8545: https://www.ti.com/lit/ds/symlink/lp8545.pdf @@ -302,8 +378,8 @@ static const struct backlight_ops gmux_bl_ops = { * connecting it either to the discrete GPU or the Thunderbolt controller. * Oddly enough, while the full port is no longer switchable, AUX and HPD * are still switchable by way of an `NXP CBTL03062`_ (on pre-retinas - * MBP8 2011 and MBP9 2012) or two `TI TS3DS10224`_ (on retinas) under the - * control of gmux. Since the integrated GPU is missing the main link, + * MBP8 2011 and MBP9 2012) or two `TI TS3DS10224`_ (on pre-t2 retinas) under + * the control of gmux. Since the integrated GPU is missing the main link, * external displays appear to it as phantoms which fail to link-train. * * gmux receives the HPD signal of all display connectors and sends an @@ -506,6 +582,20 @@ static const struct apple_gmux_config apple_gmux_index = { .name = "indexed" }; +static const struct apple_gmux_config apple_gmux_mmio = { + .read8 = &gmux_mmio_read8, + .write8 = &gmux_mmio_write8, + .read32 = &gmux_mmio_read32, + .write32 = &gmux_mmio_write32, + .gmux_handler = &gmux_handler_no_ddc, + .handler_flags = VGA_SWITCHEROO_NEEDS_EDP_CONFIG, + .resource_type = IORESOURCE_MEM, + .read_version_as_u32 = true, + .use_acpi_gmsp = true, + .name = "T2" +}; + + /** * DOC: Interrupt * @@ -637,6 +727,25 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) pnp_set_drvdata(pnp, gmux_data); switch (type) { + case APPLE_GMUX_TYPE_MMIO: + gmux_data->config = &apple_gmux_mmio; + mutex_init(&gmux_data->index_lock); + + res = pnp_get_resource(pnp, IORESOURCE_MEM, 0); + gmux_data->iostart = res->start; + /* Although the ACPI table only allocates 8 bytes, we need 16. */ + gmux_data->iolen = 16; + if (!request_mem_region(gmux_data->iostart, gmux_data->iolen, + "Apple gmux")) { + pr_err("gmux I/O already in use\n"); + goto err_free; + } + gmux_data->iomem_base = ioremap(gmux_data->iostart, gmux_data->iolen); + if (!gmux_data->iomem_base) { + pr_err("couldn't remap gmux mmio region"); + goto err_release; + } + goto get_version; case APPLE_GMUX_TYPE_INDEXED: gmux_data->config = &apple_gmux_index; mutex_init(&gmux_data->index_lock); @@ -656,6 +765,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) goto err_free; } +get_version: if (gmux_data->config->read_version_as_u32) { version = gmux_read32(gmux_data, GMUX_PORT_VERSION_MAJOR); ver_major = (version >> 24) & 0xff; @@ -686,7 +796,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) gmux_data, &gmux_bl_ops, &props); if (IS_ERR(bdev)) { ret = PTR_ERR(bdev); - goto err_release; + goto err_unmap; } gmux_data->bdev = bdev; @@ -753,7 +863,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) /* * Retina MacBook Pros cannot switch the panel's AUX separately * and need eDP pre-calibration. They are distinguishable from - * pre-retinas by having an "indexed" gmux. + * pre-retinas by having an "indexed" or "T2" gmux. * * Pre-retina MacBook Pros can switch the panel's DDC separately. */ @@ -778,8 +888,14 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) &gmux_notify_handler); err_notify: backlight_device_unregister(bdev); +err_unmap: + if (gmux_data->iomem_base) + iounmap(gmux_data->iomem_base); err_release: - release_region(gmux_data->iostart, gmux_data->iolen); + if (gmux_data->config->resource_type == IORESOURCE_MEM) + release_mem_region(gmux_data->iostart, gmux_data->iolen); + else + release_region(gmux_data->iostart, gmux_data->iolen); err_free: kfree(gmux_data); return ret; @@ -800,7 +916,11 @@ static void gmux_remove(struct pnp_dev *pnp) backlight_device_unregister(gmux_data->bdev); - release_region(gmux_data->iostart, gmux_data->iolen); + if (gmux_data->iomem_base) { + iounmap(gmux_data->iomem_base); + release_mem_region(gmux_data->iostart, gmux_data->iolen); + } else + release_region(gmux_data->iostart, gmux_data->iolen); apple_gmux_data = NULL; kfree(gmux_data); diff --git a/include/linux/apple-gmux.h b/include/linux/apple-gmux.h index 5f658439f7f8..b7532f26b756 100644 --- a/include/linux/apple-gmux.h +++ b/include/linux/apple-gmux.h @@ -34,11 +34,18 @@ #define GMUX_PORT_READ 0xd0 #define GMUX_PORT_WRITE 0xd4 +#define GMUX_MMIO_PORT_SELECT 0x0e +#define GMUX_MMIO_COMMAND_SEND 0x0f + +#define GMUX_MMIO_READ 0x00 +#define GMUX_MMIO_WRITE 0x40 + #define GMUX_MIN_IO_LEN (GMUX_PORT_BRIGHTNESS + 4) enum apple_gmux_type { APPLE_GMUX_TYPE_PIO, - APPLE_GMUX_TYPE_INDEXED + APPLE_GMUX_TYPE_INDEXED, + APPLE_GMUX_TYPE_MMIO }; #if IS_ENABLED(CONFIG_APPLE_GMUX) @@ -93,19 +100,24 @@ static inline bool apple_gmux_detect(struct pnp_dev *pnp_dev, enum apple_gmux_ty } res = pnp_get_resource(pnp_dev, IORESOURCE_IO, 0); - if (!res || resource_size(res) < GMUX_MIN_IO_LEN) - goto out; - - /* - * Invalid version information may indicate either that the gmux - * device isn't present or that it's a new one that uses indexed io. - */ - ver_major = inb(res->start + GMUX_PORT_VERSION_MAJOR); - ver_minor = inb(res->start + GMUX_PORT_VERSION_MINOR); - ver_release = inb(res->start + GMUX_PORT_VERSION_RELEASE); - if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) { - if (apple_gmux_is_indexed(res->start)) - type = APPLE_GMUX_TYPE_INDEXED; + if (res && resource_size(res) >= GMUX_MIN_IO_LEN) { + /* + * Invalid version information may indicate either that the gmux + * device isn't present or that it's a new one that uses indexed io. + */ + ver_major = inb(res->start + GMUX_PORT_VERSION_MAJOR); + ver_minor = inb(res->start + GMUX_PORT_VERSION_MINOR); + ver_release = inb(res->start + GMUX_PORT_VERSION_RELEASE); + if (ver_major == 0xff && ver_minor == 0xff && ver_release == 0xff) { + if (apple_gmux_is_indexed(res->start)) + type = APPLE_GMUX_TYPE_INDEXED; + else + goto out; + } + } else { + res = pnp_get_resource(pnp_dev, IORESOURCE_MEM, 0); + if (res) + type = APPLE_GMUX_TYPE_MMIO; else goto out; } From patchwork Thu Feb 16 12:23:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 58010 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp275223wrn; Thu, 16 Feb 2023 04:38:14 -0800 (PST) X-Google-Smtp-Source: AK7set8eayrX2P4rdkZE1CINLNBWgfan09H8cDdVzN1jLEaA5wNDpOGL7dGBha2FmMYtyFdWCBLM X-Received: by 2002:a17:907:a484:b0:8b1:438e:9b3f with SMTP id vp4-20020a170907a48400b008b1438e9b3fmr1802959ejc.20.1676551094732; Thu, 16 Feb 2023 04:38:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676551094; cv=none; d=google.com; s=arc-20160816; b=DH3vILb2qlIoq/Gw9NG9v87CkdcxCE3u3jJzKDw57qecKk29s/qn9U9X5ktkAWdhUm O04MzJBK5ItFcXjgfEEMwbT/FZBD/z/+JW6OIBTXXhAdU09LXHDmbvFAZjDEghaocKiF hJ82estEe9fikXDoxW/KJh44N2khE7grOFoYwfz4HBVC96Vv+D9XRDVs1V0qNoQJoJWu lEDSWvt/QoE6Li0C0tNKisWTDgR4tnPyFAnpd8HQDlHW2Hrtn7K/9uR+MdBcToU0hRe3 11UmZPMAwXsGpA00P1aWGNFJD79ekY01uMArzZh4hkJUcWffjEnGJVdZSizOsCYebFeV h28Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Am8BGSEip5nNE8Zh0hm60KhhWsRjuiVkZed/6iLfSSg=; b=gkFmsBbAc+YGpFZ3irbMRyDhh3hcj7E2TbVMusAQKXJx1Sbn+ZbqByXtvWnOgPfo4X gUQY2gGSEn0wg2g3LTEF/JRq6VKCWxYDIrCED3jYPBWCfHqAD7/Hfp3pvJCzmHar562I xzd0FT0PangzVRXIlw6iczXsRcJennb1gHHSgvA3i6mQ9JHbFB1reg4Sjpjd7eTNoYEw keZw7h24lnuXn43PlYDKono4V8ppGG9DJeRxzsxKxqR/3hMhjrqRVGiDqqOzk+wTbbln 93sZbQR/r7S4yzMnDYDvxyQKKJcVP7hwV2d77HdLFPFdvyxq3NdSmySh4j8Nq9mnca/N oKxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kuj5ZFPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mf21-20020a1709071a5500b008b1318e9b71si1889534ejc.519.2023.02.16.04.37.51; Thu, 16 Feb 2023 04:38:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kuj5ZFPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230287AbjBPMZo (ORCPT + 99 others); Thu, 16 Feb 2023 07:25:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35456 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230246AbjBPMZi (ORCPT ); Thu, 16 Feb 2023 07:25:38 -0500 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BB545776D; Thu, 16 Feb 2023 04:25:30 -0800 (PST) Received: by mail-pf1-x434.google.com with SMTP id n2so1345727pfo.3; Thu, 16 Feb 2023 04:25:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Am8BGSEip5nNE8Zh0hm60KhhWsRjuiVkZed/6iLfSSg=; b=kuj5ZFPccKNgGnLvhSCdjjLGRHp/Cg168G+FmzCHapcrrSBsK3Zq2++X4KBN/kfm2N /YvqnFNfz8wJq8hQUrRRx0qDaQ9e4Ix/LnT2/oD+P6L9iDzb4nS/q0JD7HxyjYvKeksL brv5h9SOUs/hfcUBWzikgIBVekWlqRnR3jK1XCC59m/1Seon8DSfXHvzPn5RhLZU2Wcs ag9Fg+2U4RX4jkrm3jNql2ihlc3eN6CTD1wvVWv3I+JllMKVAg5YaXHtEs2NT7Jp/9tm AUoF8g9dNGGJUfraAGSG0M4WJnqrt13qgU2lgPX5RHrjeS7Ok5frtdfoqVoyEsSyq8mL QRNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Am8BGSEip5nNE8Zh0hm60KhhWsRjuiVkZed/6iLfSSg=; b=ee30K0aBXI6gdfGrDc+nBUKNKKC9tnDY1Q0xNX1VhiLIwltRy5cI6jJXrxzTUDlUf2 mJc/w265f6Db6iDltxCvAS/g25XslJ9tRHhPaqpdOz45T4cOOMQckO5AZtZFnPF0wz+c S/Imhz8BCradViYHEGBryHfZyhCxnXDzE1zDPSiXNPonbu3FjZfQDN7se9S8kILkCww6 VqW+kK12b6Vfdp6c1HeWZoWoX57lobcKVoksd8R4p5n2xumfQ/mK3+xKqChE2IbGuDsX vUebPL51xsUZZhDCQD+xHujehHl9VEDIKImMgOazr2zsg+wArpT2bPhKg25ExybU+DZY LoSw== X-Gm-Message-State: AO0yUKUUQ867heqLs/ixiyBSkZ68P9Oq3MtjhZiRDPr+n6UomfhkGjF+ YPwZ8piwnzpJCLRnfE2us5s= X-Received: by 2002:aa7:978e:0:b0:592:5276:95df with SMTP id o14-20020aa7978e000000b00592527695dfmr4534640pfp.2.1676550329599; Thu, 16 Feb 2023 04:25:29 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id c10-20020aa781ca000000b005a909290425sm1224055pfn.172.2023.02.16.04.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Feb 2023 04:25:29 -0800 (PST) From: Orlando Chamberlain To: Hans de Goede Cc: Mark Gross , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Lukas Wunner , Seth Forshee , Aditya Garg , Aun-Ali Zaidi , Kerem Karabay , Orlando Chamberlain Subject: [PATCH v2 5/5] apple-gmux: add debugfs interface Date: Thu, 16 Feb 2023 23:23:43 +1100 Message-Id: <20230216122342.5918-6-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230216122342.5918-1-orlandoch.dev@gmail.com> References: <20230216122342.5918-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757991240607681186?= X-GMAIL-MSGID: =?utf-8?q?1757991240607681186?= Allow reading and writing gmux ports from userspace. For example: echo 4 > /sys/kernel/debug/apple_gmux/selected_port cat /sys/kernel/debug/apple_gmux/selected_port_data | xxd -p Will show the gmux version information (00000005 in this case) Signed-off-by: Orlando Chamberlain --- v1->v2: Use debugfs instead of sysfs. drivers/platform/x86/apple-gmux.c | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index 5bac6dcfada0..e8a35d98b113 100644 --- a/drivers/platform/x86/apple-gmux.c +++ b/drivers/platform/x86/apple-gmux.c @@ -22,6 +22,7 @@ #include #include #include +#include #include /** @@ -66,6 +67,10 @@ struct apple_gmux_data { enum vga_switcheroo_client_id switch_state_external; enum vga_switcheroo_state power_state; struct completion powerchange_done; + + /* debugfs data */ + u8 selected_port; + struct dentry *debug_dentry; }; static struct apple_gmux_data *apple_gmux_data; @@ -674,6 +679,87 @@ static void gmux_notify_handler(acpi_handle device, u32 value, void *context) complete(&gmux_data->powerchange_done); } +/** + * DOC: Debugfs Interface + * + * gmux ports can be accessed from userspace as a debugfs interface. For example: + * + * # echo 4 > /sys/kernel/debug/apple_gmux/selected_port + * # cat /sys/kernel/debug/apple_gmux/selected_port_data | xxd -p + * 00000005 + * + * Reads 4 bytes from port 4 (GMUX_PORT_VERSION_MAJOR). + * + * 1 and 4 byte writes are also allowed. + */ + +static ssize_t gmux_selected_port_data_write(struct file *file, + const char __user *userbuf, size_t count, loff_t *ppos) +{ + struct apple_gmux_data *gmux_data = file->private_data; + int ret; + + if (*ppos) + return -EINVAL; + + if (count == 1) { + u8 data; + + ret = copy_from_user(&data, userbuf, 1); + if (ret) + return ret; + gmux_write8(gmux_data, gmux_data->selected_port, data); + } else if (count == 4) { + u32 data; + + ret = copy_from_user(&data, userbuf, 4); + if (ret) + return ret; + gmux_write32(gmux_data, gmux_data->selected_port, data); + } else + return -EINVAL; + + return count; +} + +static ssize_t gmux_selected_port_data_read(struct file *file, + char __user *userbuf, size_t count, loff_t *ppos) +{ + struct apple_gmux_data *gmux_data = file->private_data; + u32 data; + + data = gmux_read32(gmux_data, gmux_data->selected_port); + + return simple_read_from_buffer(userbuf, count, ppos, &data, sizeof(data)); +} + +static const struct file_operations gmux_port_data_ops = { + .open = simple_open, + .write = gmux_selected_port_data_write, + .read = gmux_selected_port_data_read +}; + +static void gmux_init_debugfs(struct apple_gmux_data *gmux_data) +{ + struct dentry *debug_dentry; + + debug_dentry = debugfs_create_dir(KBUILD_MODNAME, NULL); + + if (IS_ERR(debug_dentry)) + return; + + gmux_data->debug_dentry = debug_dentry; + + debugfs_create_u8("selected_port", 0644, debug_dentry, &gmux_data->selected_port); + debugfs_create_file("selected_port_data", 0644, debug_dentry, + gmux_data, &gmux_port_data_ops); +} + +static void gmux_fini_debugfs(struct apple_gmux_data *gmux_data) +{ + debugfs_remove_recursive(gmux_data->debug_dentry); +} + static int gmux_suspend(struct device *dev) { struct pnp_dev *pnp = to_pnp_dev(dev); @@ -874,6 +960,7 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) goto err_register_handler; } + gmux_init_debugfs(gmux_data); return 0; err_register_handler: @@ -905,6 +992,7 @@ static void gmux_remove(struct pnp_dev *pnp) { struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp); + gmux_fini_debugfs(gmux_data); vga_switcheroo_unregister_handler(); gmux_disable_interrupts(gmux_data); if (gmux_data->gpe >= 0) {