From patchwork Sat Feb 18 13:20:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 13145565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC93AC636CC for ; Sat, 18 Feb 2023 13:20:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229763AbjBRNUv (ORCPT ); Sat, 18 Feb 2023 08:20:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229796AbjBRNUr (ORCPT ); Sat, 18 Feb 2023 08:20:47 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAB4518AA9; Sat, 18 Feb 2023 05:20:46 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id j1so282110pfu.5; Sat, 18 Feb 2023 05:20:46 -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=YNMRgH3NmhW4em9tRT4KeD9GEuYej7dmXs7hwuKnpeQ=; b=gpl97i2XWkDu/oiU+nMsVB8xOckNetjRCCk1kIih1uGK4CJ9dAQ4Ngr8WlMGC8EGHY uxkoGxQLhD30r3xK12g1egnxfht3eZwAClTcyuqhDxpwnLy7aMSGud2KonjHXgKzl23k wcMY6wBFdNeSHZv0jG5JGC1z72k4bCEVd7oa3kVgGZuoV69BPiztxx32IYEWYp9IXrmA ey7e9BeYcl41n6+8I3+XaQt8p6oP9fRHm+Yvvx8J7CvYS95qBzClkEG/FnBjAa6oqWIK 7RYzEE1K3XRciQYQWep/eOw8MWr7fr1qzP9od0sJYdlP5x2zO7UGZ0uGzpg+Qyxb4fvU GyZw== 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=YNMRgH3NmhW4em9tRT4KeD9GEuYej7dmXs7hwuKnpeQ=; b=G0vR3dcAgHnV4lBge+U9ppHUJEcFtMHQN8ouAVnGazX+PGzdQLC/YH2YYwxiBJAMcP wErdioK5e3tLi4uUIpP9tUREGg34qbN1va+ilzTdFrRnX/zj3ZT6L+juk2eP1V2udKho xEOmSknVnGY8ErxNjQQyOnQYLllHX26rIQTQ3SfKJKIcrnq1CUxg7QpY/hlBCXgP/3f6 l4I/XYuQPuUucSZ3FXHQdYQ8bWX6xwvl6AwgKdLrepPmWPQaJJK8HVjCmf1Y2Gij0qPf MfgdsKTo1Jh1PDISFpjztPKZXYf3oRDxuPWXwLO73sbu0V55hZeBkMZdIn8Oi1Be+iET q4qQ== X-Gm-Message-State: AO0yUKWi4eGJYfnP6lm787JSwWiBWweIlxVrLxZyfR+UYt317Q5NZd5/ bXQsabhvPVP0spfRDgC04wQ= X-Google-Smtp-Source: AK7set+dZDl6noBNz/ZY6LMvFwirLRo2JsGKWqlOCQE/DMGWw1ZRaxjIFAqxPsmFQr+nlaAhgQiqIQ== X-Received: by 2002:aa7:96c9:0:b0:5a9:b4eb:d262 with SMTP id h9-20020aa796c9000000b005a9b4ebd262mr7651302pfq.1.1676726446099; Sat, 18 Feb 2023 05:20:46 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id v19-20020aa78093000000b0058e24050648sm4769448pff.12.2023.02.18.05.20.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 05:20:45 -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 v3 1/5] apple-gmux: use first bit to check switch state Date: Sun, 19 Feb 2023 00:20:03 +1100 Message-Id: <20230218132007.3350-2-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230218132007.3350-1-orlandoch.dev@gmail.com> References: <20230218132007.3350-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org 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 --- 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 Sat Feb 18 13:20:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 13145566 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 858BEC64ED6 for ; Sat, 18 Feb 2023 13:20:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229798AbjBRNUx (ORCPT ); Sat, 18 Feb 2023 08:20:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229799AbjBRNUw (ORCPT ); Sat, 18 Feb 2023 08:20:52 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F16018B00; Sat, 18 Feb 2023 05:20:50 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id g2so735400pjp.2; Sat, 18 Feb 2023 05:20:50 -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=ZCWQ17D4Y5CKu1gM+7TRPxzDj5IX3tuwwnau96cBtVo=; b=LypYViB2+XpyyBH/4qUSbZmHsASA+ge+1pYhoxV39r4rIAcQVwyJFFVG1AjNvaD2qz 0KVR0KL/KSDck5OZ0u0LMp/yDhS7bBNLF7Nt3Kxpwik5ndLYuuhm+Pm+Q1/T0fDSJX6V PdZGrHGRTxxUvi3SGVdgEuYFhW0X6hpyLEf8chwzt6ySRot8CafT3UplpW/PiHbUEzy1 08AjYAQ/htmSWh+b48vwIPR4XdL06VGJl1F+o0eDU4hY/VtToz3N9hY6jTcWi96zMN5y zl+ruW5ieweIxV8V/Z5QkUbQlzZrhVo7/uLiwVuKO/MbjsLyLl41ToORyby3wKlkzlUd Rv8w== 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=ZCWQ17D4Y5CKu1gM+7TRPxzDj5IX3tuwwnau96cBtVo=; b=GbL+pS6U+Xw848y9ArlSJXGMvE8l2EdWUDsHUK+Vv6Ngslkg/O0dpLFc2Srj6BB+0J BW+6qLKpsJE6dqCYf9X1hcMNJLf50h6MI7kfTKan71KyfuvM9pXByHf4WcWfH09xYDpW EkPNbfe0WYO0mOuI7VMLRV+km+l8jId6HBzYqY9ONqYwMbzBKmKUedinwPMoxqdh0MqF Vbk79gSk5VhcMm1DuLyCsqVu+Up+y2xFjCTBlwZFQnT7ZG0clhNO5HgjguGuqkRltx5q WZ+LyMogdvnas0o2TWklEopOgBQ37Z5NZL3mYZTOi0+ET8jBRSC45DKjUQ2QSBjxSJVY u+FQ== X-Gm-Message-State: AO0yUKWjyndg3uh0QxLC8o4yk8OKA2n26AY9e6386uhL5MGFzKf78ySt l1E12DjDr+Pup0pIfnDoF1E= X-Google-Smtp-Source: AK7set+Ms47ENVmB5mpKsTJJJ/7y4At6JaAqOjWHqRavk3e0T54J9O/UGSU74LLvdtHoZ2PXpZoXPQ== X-Received: by 2002:a05:6a21:360b:b0:c7:1bf1:4b28 with SMTP id yg11-20020a056a21360b00b000c71bf14b28mr7250330pzb.12.1676726449995; Sat, 18 Feb 2023 05:20:49 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id v19-20020aa78093000000b0058e24050648sm4769448pff.12.2023.02.18.05.20.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 05:20:49 -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 v3 2/5] apple-gmux: refactor gmux types Date: Sun, 19 Feb 2023 00:20:04 +1100 Message-Id: <20230218132007.3350-3-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230218132007.3350-1-orlandoch.dev@gmail.com> References: <20230218132007.3350-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org 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 --- v2->v3: add comma to last item in enum apple_gmux_type 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..147dc1c52e08 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 Sat Feb 18 13:20:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 13145567 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2E4CC6379F for ; Sat, 18 Feb 2023 13:20:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229799AbjBRNU6 (ORCPT ); Sat, 18 Feb 2023 08:20:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229820AbjBRNUz (ORCPT ); Sat, 18 Feb 2023 08:20:55 -0500 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57C3F1968D; Sat, 18 Feb 2023 05:20:54 -0800 (PST) Received: by mail-pl1-x634.google.com with SMTP id 19so750084plo.7; Sat, 18 Feb 2023 05:20:54 -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=DV93rUPqERTzCGzgF+Nyoaubtpk03qL36hyj85V+RLA=; b=KA/EY+98GP20sHiGHDsgKCCYvqBnXkPPOBIZxF6NB78aXWWIpH5MWuseuao1oqew6L E7WmnieEoombFqBZU3btW8QK4vbpSFiwVQfGm4OB+tUNC5xicpQEFdcMIpnEYUNQTWAf B0Nt7klX04OhPmxwrsBbfCqVH+9uRe9Mg3wmqxPaA38O2cEIpowOH9fQl2qeW6wX+sNd 49OGJY7ivMJI+ck4MNiQ2k5ajrEk8xQViKbEwi7EICK+JR0XfhK2oyyHFjQTAiLhF88t T30X8LKfzmhGaZAqAQ+nGMIvbNDSS+Cp9UJ2Xnqv+2a5BgakMTFTav/0wePzHD840B2f 636w== 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=DV93rUPqERTzCGzgF+Nyoaubtpk03qL36hyj85V+RLA=; b=Bs3PH854BVyZ8Y5RPg4ttqF3zVfaDbrDmnAz0xEbDtTbnXLtBV5dEjZhQ515+m7v4e r3pAaGcaNphF5IhjIkDytVo0H9oT7A7QfyWJulhorWPbtVXye7jfo+xsjiuXH8fbMLCa n4lGnO9O6nj0enhzFrCm69M+Sh3y9cbFV13audqEHL3LdcAmBg+Lhsabi+mC/jSAwhgf hGmyWsHj0IJpByCLE24AlE2Hm4yHnl23nEN584WtwtZhEXYnV83zObiTQDftGwg93egl 4jBJ+fMdxxmgFdtOZ62ZRv0PkyRTZ+2pu+V4XHQYCKLMeGyFvabwDgvRG+CS1MMl7YSM UZhg== X-Gm-Message-State: AO0yUKW/zsKL07y1u12MuzPBaT+2PXx7wvUDX8vIZ+PyuPzmMxN7O7Qo p3Yc+s/whMaQHK4inP1VV0s= X-Google-Smtp-Source: AK7set/bM2vLZGaeUiVgMOOszovguu9KlevXt8hDj8+lIN3R8WzAywv4emrtHGN6RLiCjFsErfbB9Q== X-Received: by 2002:a05:6a20:9390:b0:c7:7c12:638e with SMTP id x16-20020a056a20939000b000c77c12638emr5303286pzh.9.1676726453840; Sat, 18 Feb 2023 05:20:53 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id v19-20020aa78093000000b0058e24050648sm4769448pff.12.2023.02.18.05.20.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 05:20:53 -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 v3 3/5] apple-gmux: Use GMSP acpi method for interrupt clear Date: Sun, 19 Feb 2023 00:20:05 +1100 Message-Id: <20230218132007.3350-4-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230218132007.3350-1-orlandoch.dev@gmail.com> References: <20230218132007.3350-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org 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 --- v2->v3: remove status != 0 check drivers/platform/x86/apple-gmux.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index 36208e93d745..8dfa1c02be64 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, @@ -537,6 +561,8 @@ 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 (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 Sat Feb 18 13:20:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 13145568 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B8B6C636CC for ; Sat, 18 Feb 2023 13:21:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229824AbjBRNVM (ORCPT ); Sat, 18 Feb 2023 08:21:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229536AbjBRNVG (ORCPT ); Sat, 18 Feb 2023 08:21:06 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B9B41A947; Sat, 18 Feb 2023 05:20:58 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id t15so745977plg.6; Sat, 18 Feb 2023 05:20:58 -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=cF5Y4pPUSGrVbM8YYaWz/D49GVkjD7CQeblpzVtmdXQ=; b=E5elhLX0SZROI5MP2sO68SJW/tDAvmXHNZBQQVene/D+vzshFRmW16ExTkcKH0dmNE rRSD0w1Jh8Pw6qQh1TQM5p9tTF0wgFoR2JI6CdcKK4/NmArIiSuq2AC5lwIZveaWoLE/ 2ncfFwe/Ylfyp+C/TMcdw35lhpvwLzLgZ9BeNd0RYb41SSzt/u947R1qVoNSXP5gX1a/ EtVAW5VyE97jx0dHu9M7CbTZI3uSJWuKH2S9fTEIgIX0ld8EsCRb3qGm6STySw9h1r4O kF7+y8KSIXD9oBpuhwOW5jhMZtwIIAWTFLODPyGWy7LMiKo5uAxKjcuXFGxLZ6eIDOr6 O1eA== 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=cF5Y4pPUSGrVbM8YYaWz/D49GVkjD7CQeblpzVtmdXQ=; b=rMmD1ozlNXpjYjiSESGQRd/jv9lhZFrTFgG+l8tyMyBpbhx2q+Qg4lT+CjdMsDaBc2 N1ld3VDLS8OKHgiHrhRvddc2gm/HzT5vvQy9BAq4qTUfC3520pSHaT4dto/vGv/DgmjM a988L40epDD4Lqg+YCvIPz+SIi9TdoxMZJLP4H2+HaJoB/Ce3bdK7prSGERYp+cRdVhM fK6obNeGKTcZx8J292nRzGo3xESKRbG/hPzdHPeFj+jKJGQHYdYgEV6n5pR2zsmLJ2pM MxeyBhWTbtPhCaBzVBFgijRijO26wfG1WVSTUmOs/ZmXvIisKzFNlTOkJrgk5iY+k+3F xCmQ== X-Gm-Message-State: AO0yUKWYmT38Y2gIAKNp2L9KDlSmCmCafP8mBF8ZiVbpeteWlNv4FrxO fU7FFlGS6OfkehUhP8hJIr4= X-Google-Smtp-Source: AK7set8RYvBtmGjuPx2plM/zu/+YuArghOoOnRdYFaZMaRl9jRBfjwQQsAtJcXQs9G3nof8CnRILDw== X-Received: by 2002:a05:6a20:144a:b0:be:a7f8:78a6 with SMTP id a10-20020a056a20144a00b000bea7f878a6mr4074692pzi.42.1676726457763; Sat, 18 Feb 2023 05:20:57 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id v19-20020aa78093000000b0058e24050648sm4769448pff.12.2023.02.18.05.20.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 05:20:57 -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 v3 4/5] apple-gmux: support MMIO gmux on T2 Macs Date: Sun, 19 Feb 2023 00:20:06 +1100 Message-Id: <20230218132007.3350-5-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230218132007.3350-1-orlandoch.dev@gmail.com> References: <20230218132007.3350-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org 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 --- v2->v3: add comma after last item in apple_gmux_type enum v2->v3: check for not present mmio gmux drivers/platform/x86/apple-gmux.c | 142 +++++++++++++++++++++++++++--- include/linux/apple-gmux.h | 56 +++++++++--- 2 files changed, 174 insertions(+), 24 deletions(-) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index 8dfa1c02be64..01e7b1939916 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 * @@ -635,6 +725,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); @@ -654,6 +763,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; @@ -684,7 +794,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; @@ -751,7 +861,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. */ @@ -776,8 +886,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; @@ -798,7 +914,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 147dc1c52e08..0bcb331550a4 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_MMIO, }; #if IS_ENABLED(CONFIG_APPLE_GMUX) @@ -57,6 +64,24 @@ static inline bool apple_gmux_is_indexed(unsigned long iostart) return false; } +static inline bool apple_gmux_is_mmio(unsigned long iostart) +{ + u8 *__iomem iomem_base = ioremap(iostart, 16); + u8 val; + + if (!iomem_base) + return false; + + /* + * If this is 0xff, then gmux must not be present, as the gmux would + * reset it to 0x00, or it would be one of 0x1, 0x4, 0x41, 0x44 if a + * command is currently being processed. + */ + val = ioread8(iomem_base + GMUX_MMIO_COMMAND_SEND); + iounmap(iomem_base); + return (val != 0xff); +} + /** * apple_gmux_detect() - detect if gmux is built into the machine * @@ -93,19 +118,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 && apple_gmux_is_mmio(res->start)) + type = APPLE_GMUX_TYPE_MMIO; else goto out; } From patchwork Sat Feb 18 13:20:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 13145569 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B18F7C636CC for ; Sat, 18 Feb 2023 13:21:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229851AbjBRNV1 (ORCPT ); Sat, 18 Feb 2023 08:21:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229829AbjBRNVZ (ORCPT ); Sat, 18 Feb 2023 08:21:25 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32B451ABEE; Sat, 18 Feb 2023 05:21:02 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id t15so746064plg.6; Sat, 18 Feb 2023 05:21:02 -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=azOKFY8r4ZFN0TrySiezIiphodtx+QxBgOOIAouGT3U=; b=MnJA+KLdTx4gr6/1UI87VTaCM/r/SCkxaid7ehbgvA2XdJbatfS8WtigKdgPjirBux zt6CqL+N0rE10MK51ntQNMSqRmETKowp7/avz6bMn5o8SFfePnPnbhTifQoNt/iYDyWK XjuFwzO2vJHJ+zCwbUZp7JoMN3HNRBQeBxyS9qBOvsoqLeSXSX1vhooGO9wgoiMp8qbz 8TWKSSbfrZboDA886/cD8MhhggUsxdoeWLEWa1K/OkeqbpxTa61ssRF5ykCNIcNeaZeb oNMv/8pG7C2f463Nt8Tjha0x3OGzhmnqU3sAS81R9k8emfW4ms1SP0rYwhE96PfHG/jo aEsw== 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=azOKFY8r4ZFN0TrySiezIiphodtx+QxBgOOIAouGT3U=; b=Uze9lxrC6/bwTFd9R5CrP/ZjYdWfplsL563m13+gOv+YoNn/sGQkJCUudwS7YxlxLc tOhBaAGc6DLr2PJNWbFqkB53yE4jFBziPqI0qlO8Q2qaXIp5scnt1LjQZj1CajhyWMOT QT4FLNT9xh/ktSm9HhTdErg0mEMYgJx1/B0tXu7Tf0C460Zcniwc8XuZoEarTtFxUCH/ nUG3kMRIvO9+duqYtTvcSR3WIJyxFV5WHG3qQ9eDgbbu6sLrjJEbYo02ElATrGluWk3A pmU4rX7oz3B68HWf1xBeXMd7l5K4z1fBXuuuQyS8nPUSBnpUxmcXhC9WJUgFqBYhv50r CtuQ== X-Gm-Message-State: AO0yUKXA5lqYd2GH1XQ+aeLze4q42UC7OlJwFYXqC25aAL9CZ40bfP5P IbFsekQ+e2Xh3Zx7XOVGLZA= X-Google-Smtp-Source: AK7set87id1WVpQZurYteFJVdAd+GwKFsyIlbtAXj7tEbjColPRwPc6X8uMszpcrNoC7WD3jObruBw== X-Received: by 2002:a05:6a20:8f0e:b0:bc:8b91:69a3 with SMTP id b14-20020a056a208f0e00b000bc8b9169a3mr4237797pzk.46.1676726461649; Sat, 18 Feb 2023 05:21:01 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id v19-20020aa78093000000b0058e24050648sm4769448pff.12.2023.02.18.05.20.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 05:21:01 -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 v3 5/5] apple-gmux: add debugfs interface Date: Sun, 19 Feb 2023 00:20:07 +1100 Message-Id: <20230218132007.3350-6-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230218132007.3350-1-orlandoch.dev@gmail.com> References: <20230218132007.3350-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org 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 --- v2->v3: don't check if the folder failed to be created drivers/platform/x86/apple-gmux.c | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index 01e7b1939916..4cbdc9f9bd10 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; @@ -672,6 +677,81 @@ 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) +{ + gmux_data->debug_dentry = debugfs_create_dir(KBUILD_MODNAME, NULL); + + debugfs_create_u8("selected_port", 0644, gmux_data->debug_dentry, + &gmux_data->selected_port); + debugfs_create_file("selected_port_data", 0644, gmux_data->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); @@ -872,6 +952,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: @@ -903,6 +984,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) {