Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp4792188rwb; Tue, 17 Jan 2023 05:41:09 -0800 (PST) X-Google-Smtp-Source: AMrXdXsWe2chaZ3MRBHTPWSv8gAxokmvTBpc8s59cZ/IJKNZnUK+53JAbiASnfeHoAJ2AZzdwFq+ X-Received: by 2002:a05:6402:400a:b0:496:bdb5:572f with SMTP id d10-20020a056402400a00b00496bdb5572fmr3833288eda.31.1673962869422; Tue, 17 Jan 2023 05:41:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1673962869; cv=pass; d=google.com; s=arc-20160816; b=SzpOSoPQxAa56bK0gSoxSaf8vEQtaTdvnoTxsLGpgMl4lt11yPjClA6kKDnmFOWHeN ljui9sX5/RbsQTZQbj5aBBSqhssbBgHTuSjgSZCnPx2n/wuPCHEpdh6H6J1bMo8LoL2E TRf8UkftTueig0llXdSXXkEylCfBJdZ+woZDXu4Ic1vIjsNqKp44rwLYKqrAyLu9nG6Q e1gW/MjWSu/JlOpostr7u3uMK+bbENn9IPyFKV7KkIqXFF+kAoycMj2RckaaoFCZ1WyK gfVCDi7yP+HJi3z7fEn/A/13+LqdoBMfCvMnjLhWDaEBDU7sb7ngVSGPUWhymf6+jOOl Exew== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:in-reply-to:content-disposition :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tnCJvgqItxXDyV6XF5HpF4DnztjAAvS/il1ItqCxsqw=; b=ERWBg6HFsbgH0ryO1wCZcL7ZzPk4usk2Yt7NVC0n8tAasdLYI0SOkNGF8D9R3nNILl iPOMyYtkDFiQtePcvy39d4rPIcNUOCsE1GVtPtL9jkAARdK/QSkZ3YX1iMr+JZ+YL8Dw cta3pgyUgwToGVqQJhcKWxGkgQ03pzto/BGSXDVfUc/Be4TEAyujArPIMhD5ukcmFnFY 7xJaAdb/sOXkfQoZIxQlbLqQKjrFGX/WVYZVLJFoRHP16fgywEMT8ci/Sa97bNazapqF t5bS+5Yn6xkafhjVbmyJYNGJguHPhRnXslpaG1pIHzv2oZL+p4F/fM8r4/ZO9wWvH9ZL EOyQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=Y6L33EjP; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b12-20020a056402350c00b0049e15ded603si9107750edd.279.2023.01.17.05.40.56; Tue, 17 Jan 2023 05:41:09 -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=@amd.com header.s=selector1 header.b=Y6L33EjP; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230123AbjAQNfd (ORCPT + 50 others); Tue, 17 Jan 2023 08:35:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229502AbjAQNf0 (ORCPT ); Tue, 17 Jan 2023 08:35:26 -0500 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2084.outbound.protection.outlook.com [40.107.243.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24E4023102; Tue, 17 Jan 2023 05:35:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U14nNwC7xKKrtuBYvGhb4/Icxz83DUtt9xZVCJKrNnOpbMfjOm8CKrHh+FWSFlKRRXH4kQkKg3Vi/ebhOWoquFn4G5IVQQZpOGnLgEacWNYqCYuxbV5oJwXbAwInKoQvtDYyNts/ukOHlzAIG1DJ0zrD+Hr2/ifE8hUF2m91AJ76vF/8drcG4t23Py0ydKK2jiNHzfBMeDdEnPn8Z4DQ7KsXnicP2G2WJSyuJoj9CybWfiv1kcHJYICrNhAoRNIEyvJOVcuOA6hcpd13mY0Xg6yRJiP9WOFBG9K42BrS13T+je3EyrN1Lqi3rVC4HffVgcWNJyuW4sPYZgUFRDoDFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=tnCJvgqItxXDyV6XF5HpF4DnztjAAvS/il1ItqCxsqw=; b=VOWOYUakJKTChwLavdvl+ECYdGQu4Mzm5Fsyo1bY2tv+n+6M7xwE5VgC12SLYUdBp7RiW/tDaSog/nkxf2oBOUx4O6A9dL7iGRnYGdUZTPme4Xm/Kzqj4SYy8jfRUhQb/oId9AOS/dzNxa7XqtoqgKU8Exx0t+OEktpasVxCV8f8jt+rV9QbuVgyBK/FC+pMac8uVVGVObvvDAdTPb+UfJfAZsaebPHhUSfNX1EHbsB3BMvDAtnpKwl0czTXmLCE8QbhYsQ6Wjm1QBeDcMyERkTX7BLmQaZi7we5dSZ8PT7yTC6LXm/Zfm21FRhGm6rMZYpAllcPbF478M5egIk4ag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tnCJvgqItxXDyV6XF5HpF4DnztjAAvS/il1ItqCxsqw=; b=Y6L33EjPtbVgvO0oaP2I2X8W7ejkFARS68soCal9o8nO8vVJ/sEA/lwZ2uDvUJnOXuiyz0yP3s97GBHBnrIvP9Rn7tx/RZgsH8fnkGFPJvlutMKbd8b0IspdEF41Syic2pfi0zAfdaJgx09EjLgrBQ7muftUf00q0KvJn7JC2+8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from DM5PR12MB2504.namprd12.prod.outlook.com (2603:10b6:4:b5::19) by CY5PR12MB6346.namprd12.prod.outlook.com (2603:10b6:930:21::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5986.23; Tue, 17 Jan 2023 13:35:22 +0000 Received: from DM5PR12MB2504.namprd12.prod.outlook.com ([fe80::d17b:f929:12fd:8ae]) by DM5PR12MB2504.namprd12.prod.outlook.com ([fe80::d17b:f929:12fd:8ae%4]) with mapi id 15.20.6002.013; Tue, 17 Jan 2023 13:35:22 +0000 Date: Tue, 17 Jan 2023 21:34:58 +0800 From: Huang Rui To: "Yuan, Perry" Cc: "rafael.j.wysocki@intel.com" , "Limonciello, Mario" , "viresh.kumar@linaro.org" , "Sharma, Deepak" , "Fontenot, Nathan" , "Deucher, Alexander" , "Huang, Shimmer" , "Du, Xiaojian" , "Meng, Li (Jassmine)" , "Karny, Wyes" , "linux-pm@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v10 05/12] cpufreq: amd-pstate: implement Pstate EPP support for the AMD processors Message-ID: References: <20230106061420.95715-1-perry.yuan@amd.com> <20230106061420.95715-6-perry.yuan@amd.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230106061420.95715-6-perry.yuan@amd.com> X-ClientProxiedBy: SI2PR06CA0007.apcprd06.prod.outlook.com (2603:1096:4:186::9) To DM5PR12MB2504.namprd12.prod.outlook.com (2603:10b6:4:b5::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM5PR12MB2504:EE_|CY5PR12MB6346:EE_ X-MS-Office365-Filtering-Correlation-Id: f7c1f050-67ca-40cf-4a65-08daf88fae48 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yN4k0zHF5/IrTnm5MvX5tAHUgXwhSGMhG2RdpN/IjzyhG8LX+SZqgdpPgqchLBj51brRw0c+66Y8eccYNrj7CFjgtok+r9JS4hi3QfHJJuhqIvLKjljh4FnMH2BgTd4mWMkg91vMTiE1rL2Yk4T27PSA1O9+8nken8a3S+zSAdiiAmhwG9ocZH6BLKXjSWpUIG97igtErTlvquAC8kZ/BD4Zaf6t3vFc32PMlPHLcQvVJGfYDeEkXz/A20qpZ3PeOiYiS6/uGGvY+UHE1Ms7PLTy0DQuwWRIhfVqAlRS0D1GrDMLZl6sRjJoiA4RlQKbi5cs7oHr+tIxpon6ISGV+3Yu9J/olWA94og3nGhTUufIhgo82URIlkoLY59anGvFDjJLHqD8nPVPjP/XE6j5ozrZQiwltkUvMQZy3BAzZOqgY63kLMQQ0nYJcZ33aizrnMRZGOsfxwqRXgYVPkngRvaVX52RA1uyUU0Wo3T95Fy6tq0gCvPBRvlX6/ZbR+saLySHgrYT7cxsrOCn3WIfak5L+1Uu59eVGkjsUQkFh40qmmbDH5N+tX1ouhsU6nqU3GyQcAuTZgaCzJ2LBN7gkbpx7CruxREX7uR9/sq4zIIa5uZtonuRBzOPKVFX7XAOIWG7XExwKp72iA6/8y8q1g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB2504.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(136003)(376002)(366004)(346002)(451199015)(186003)(478600001)(26005)(6512007)(6666004)(6506007)(83380400001)(6486002)(86362001)(36756003)(2616005)(316002)(54906003)(66556008)(66476007)(66946007)(6636002)(37006003)(8676002)(4326008)(41300700001)(8936002)(6862004)(38100700002)(30864003)(5660300002)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?4xBeAGU1yKWxthDseBVswjEkUzwDWZDf+xw+L4yTxeK1L879VJrMLLaJRqog?= =?us-ascii?Q?s1RchCh4L08rZ2CEJ7WIMhmEUH4W6o7aK9NC6Bk9WQogAPYxOn5CdC/t9Z+V?= =?us-ascii?Q?Urq9ddunbjPSuyvBl4qBAfG946uxWOJEEWnV+B12BOoNnT9UiK52Mk0+AC6C?= =?us-ascii?Q?m4rg6vd/Nh1/ZAB+fgPFbX4l+UtJWwNkFOM/WwksGuMXgssCDOLbmiM4XkBu?= =?us-ascii?Q?lTkuzI/nBM4Q2XHaOZEp8T3xo1wEVkvDloK3Aw69k2emGIlytU4KzeZaExjn?= =?us-ascii?Q?NCB0ttcWVYtp5fszQMxxd57xuvHk6gO7RrRAZzfn8gN4U8hiQJtEBdFvK6nP?= =?us-ascii?Q?ZADda4CzcNit89FjxL8HZ9i5l48IgTjpKzLlOwbO/wTVQs7skFgKZWkdybAE?= =?us-ascii?Q?RxiWlLp+M+VG2QkNQGjfpg1QhoR8SlTdBfar/0wiiik+FJJ2n0JilhfoWRiI?= =?us-ascii?Q?GGgoNyutUmm8jDFmpdl/PBJn3RYVkXcwW+RNIDKGuvn1fzHM5n5vLvv8OpO0?= =?us-ascii?Q?GNCmrTSTrgp0bNt3yenGcTKTjRWzOg0HE2zrmJCzufi/gvcqPNtsVYIhbNET?= =?us-ascii?Q?MSt/SmWar5obKDJ/KuVKhnGgwt3uESlPBQnIjjpxPv7fc7m3A8q1wJTu9ugX?= =?us-ascii?Q?YJPfofrp8uCtZsiapSYADhY1wFLq7XsjsfQ5Ik/CaOv0WNyeCg/oQLeG4k1f?= =?us-ascii?Q?KcaDEtkyl2N0fHC2mGpsN2eiv5zx1mhI1etBenFYrbpxLCNQFEi+akef6Gx2?= =?us-ascii?Q?SdcwT0QywZTCcHTKFfYIZ/8NErZiOVSZqV5pRLIvGHYS5R6T+oekvf7XgFje?= =?us-ascii?Q?beXUzDP7+MXLY1XTGr3qCOmFek5gBh7qYS6L2CQRHxJMoTxQIJmTXUmPjPfQ?= =?us-ascii?Q?mWJBUQDl4ijOq/LP95Eu6HKgonJfMS5VWJTgWZ9VEzcBSETBo9UAhPpXgrPy?= =?us-ascii?Q?+pdJIa4AQsFhH6T7WGIFmIxiTqjumTtSZ0xT6M1Q3thvwdV1GHWn2xz0DtnH?= =?us-ascii?Q?mX/x5mFspkPxA1Mu5IdvCxrfPU2PPIhuc0iXJdqY1GIO8NMZh2FwUXMV3VsQ?= =?us-ascii?Q?tkLLtxRGeOOq3PHGqQlItG3LdFO2dy/NPbghl41PWBhlY93k1HuGIPrJFCOL?= =?us-ascii?Q?Ls4LikafTNaTy1saOMRinhAGVAo6lB/DyXEzV5o7gQjVQJT6IPlSI9rB0R3E?= =?us-ascii?Q?zDR4bvxRY2SUEzMi2JqeCmIVuVsLDjWD8mSEaoYsUhZf++/4PBA4fpbfyLlL?= =?us-ascii?Q?OWD78LWIXD2WyB/5XPW5TSYORdw5oj+DxyNLyTxOZ/5RJjHZ/ptEjwVX29j8?= =?us-ascii?Q?uGUR/PHHKW6AU8XmdEHRjDyLdSPH0XSvY2WilBuejIzqJptsgKdpkWI1xwJ+?= =?us-ascii?Q?4Tb0ZqKLsz1G3ePNwyTuixM/9YwMFFVhsy5kQT6bqwIOgUFgznYowKxdAiuP?= =?us-ascii?Q?8wUFl1fSAEXfV3dWFNfiiFsd2azO16qhJxpL/b1iGmDMg5RXpOY4HsyFSSOj?= =?us-ascii?Q?BzxbS+m46aDUrspIByE5fokMVxAIbZhW5KKq73OZteO7XMEEyNu7FCE/XkXp?= =?us-ascii?Q?L/EobW62fqnx4C7JKVPNo5ss8WTsToFR5u9mNtWC?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f7c1f050-67ca-40cf-4a65-08daf88fae48 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB2504.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jan 2023 13:35:21.8549 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 2S/q+dFOwJCSBXX1dORJ2ibncWyarUtOg+4Z26+PxxaDxpoSuWMq1s09mfgZkGvNhiVfBCGEcRg+bw/XizjPTA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6346 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,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 On Fri, Jan 06, 2023 at 02:14:13PM +0800, Yuan, Perry wrote: > From: Perry Yuan > > Add EPP driver support for AMD SoCs which support a dedicated MSR for > CPPC. EPP is used by the DPM controller to configure the frequency that > a core operates at during short periods of activity. > > The SoC EPP targets are configured on a scale from 0 to 255 where 0 > represents maximum performance and 255 represents maximum efficiency. > > The amd-pstate driver exports profile string names to userspace that are > tied to specific EPP values. > > The balance_performance string (0x80) provides the best balance for > efficiency versus power on most systems, but users can choose other > strings to meet their needs as well. > > $ cat /sys/devices/system/cpu/cpufreq/policy0/energy_performance_available_preferences > default performance balance_performance balance_power power > > $ cat /sys/devices/system/cpu/cpufreq/policy0/energy_performance_preference > balance_performance > > To enable the driver,it needs to add `amd_pstate=active` to kernel > command line and kernel will load the active mode epp driver > > Signed-off-by: Perry Yuan > --- > drivers/cpufreq/amd-pstate.c | 374 ++++++++++++++++++++++++++++++++++- > include/linux/amd-pstate.h | 10 + > 2 files changed, 378 insertions(+), 6 deletions(-) > > diff --git a/drivers/cpufreq/amd-pstate.c b/drivers/cpufreq/amd-pstate.c > index 1a8b31277620..9a92c71af80f 100644 > --- a/drivers/cpufreq/amd-pstate.c > +++ b/drivers/cpufreq/amd-pstate.c > @@ -59,7 +59,9 @@ > * we disable it by default to go acpi-cpufreq on these processors and add a > * module parameter to be able to enable it manually for debugging. > */ > +static struct cpufreq_driver *current_pstate_driver; > static struct cpufreq_driver amd_pstate_driver; > +static struct cpufreq_driver amd_pstate_epp_driver; > static int cppc_state = AMD_PSTATE_DISABLE; > > static inline int get_mode_idx_from_str(const char *str, size_t size) > @@ -73,6 +75,114 @@ static inline int get_mode_idx_from_str(const char *str, size_t size) > return -EINVAL; > } > > +static DEFINE_MUTEX(amd_pstate_limits_lock); > +static DEFINE_MUTEX(amd_pstate_driver_lock); > + > +static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached) > +{ > + u64 epp; > + int ret; > + > + if (boot_cpu_has(X86_FEATURE_CPPC)) { > + if (!cppc_req_cached) { > + epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, > + &cppc_req_cached); > + if (epp) > + return epp; > + } > + epp = (cppc_req_cached >> 24) & 0xFF; > + } else { > + ret = cppc_get_epp_perf(cpudata->cpu, &epp); > + if (ret < 0) { > + pr_debug("Could not retrieve energy perf value (%d)\n", ret); > + return -EIO; > + } > + } > + > + return (s16)(epp & 0xff); > +} > + > +static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata) > +{ > + s16 epp; > + int index = -EINVAL; > + > + epp = amd_pstate_get_epp(cpudata, 0); > + if (epp < 0) > + return epp; > + > + switch (epp) { > + case HWP_EPP_PERFORMANCE: > + index = EPP_INDEX_PERFORMANCE; > + break; > + case HWP_EPP_BALANCE_PERFORMANCE: > + index = EPP_INDEX_BALANCE_PERFORMANCE; > + break; > + case HWP_EPP_BALANCE_POWERSAVE: > + index = EPP_INDEX_BALANCE_POWERSAVE; > + break; > + case HWP_EPP_POWERSAVE: > + index = EPP_INDEX_POWERSAVE; > + break; > + default: > + break; > + } > + > + return index; > +} > + > +static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp) > +{ > + int ret; > + struct cppc_perf_ctrls perf_ctrls; > + > + if (boot_cpu_has(X86_FEATURE_CPPC)) { > + u64 value = READ_ONCE(cpudata->cppc_req_cached); > + > + value &= ~GENMASK_ULL(31, 24); > + value |= (u64)epp << 24; > + WRITE_ONCE(cpudata->cppc_req_cached, value); > + > + ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value); > + if (!ret) > + cpudata->epp_cached = epp; > + } else { > + perf_ctrls.energy_perf = epp; > + ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1); > + if (ret) { > + pr_debug("failed to set energy perf value (%d)\n", ret); > + return ret; > + } > + cpudata->epp_cached = epp; > + } > + > + return ret; > +} > + > +static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata, > + int pref_index) > +{ > + int epp = -EINVAL; > + int ret; > + > + if (!pref_index) { > + pr_debug("EPP pref_index is invalid\n"); > + return -EINVAL; > + } > + > + if (epp == -EINVAL) > + epp = epp_values[pref_index]; > + > + if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) { > + pr_debug("EPP cannot be set under performance policy\n"); > + return -EBUSY; > + } > + > + ret = amd_pstate_set_epp(cpudata, epp); > + > + return ret; > +} > + > static inline int pstate_enable(bool enable) > { > return wrmsrl_safe(MSR_AMD_CPPC_ENABLE, enable); > @@ -81,11 +191,21 @@ static inline int pstate_enable(bool enable) > static int cppc_enable(bool enable) > { > int cpu, ret = 0; > + struct cppc_perf_ctrls perf_ctrls; > > for_each_present_cpu(cpu) { > ret = cppc_set_enable(cpu, enable); > if (ret) > return ret; > + > + /* Enable autonomous mode for EPP */ > + if (cppc_state == AMD_PSTATE_ACTIVE) { > + /* Set desired perf as zero to allow EPP firmware control */ > + perf_ctrls.desired_perf = 0; > + ret = cppc_set_perf(cpu, &perf_ctrls); > + if (ret) > + return ret; > + } > } > > return ret; > @@ -428,7 +548,7 @@ static void amd_pstate_boost_init(struct amd_cpudata *cpudata) > return; > > cpudata->boost_supported = true; > - amd_pstate_driver.boost_enabled = true; > + current_pstate_driver->boost_enabled = true; > } > > static void amd_perf_ctl_reset(unsigned int cpu) > @@ -602,10 +722,61 @@ static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy, > return sprintf(&buf[0], "%u\n", perf); > } > > +static ssize_t show_energy_performance_available_preferences( > + struct cpufreq_policy *policy, char *buf) > +{ > + int i = 0; > + int offset = 0; > + > + while (energy_perf_strings[i] != NULL) > + offset += sysfs_emit_at(buf, offset, "%s ", energy_perf_strings[i++]); > + > + sysfs_emit_at(buf, offset, "\n"); > + > + return offset; > +} > + > +static ssize_t store_energy_performance_preference( > + struct cpufreq_policy *policy, const char *buf, size_t count) > +{ > + struct amd_cpudata *cpudata = policy->driver_data; > + char str_preference[21]; > + ssize_t ret; > + > + ret = sscanf(buf, "%20s", str_preference); > + if (ret != 1) > + return -EINVAL; > + > + ret = match_string(energy_perf_strings, -1, str_preference); > + if (ret < 0) > + return -EINVAL; > + > + mutex_lock(&amd_pstate_limits_lock); > + ret = amd_pstate_set_energy_pref_index(cpudata, ret); > + mutex_unlock(&amd_pstate_limits_lock); > + > + return ret ?: count; > +} > + > +static ssize_t show_energy_performance_preference( > + struct cpufreq_policy *policy, char *buf) > +{ > + struct amd_cpudata *cpudata = policy->driver_data; > + int preference; > + > + preference = amd_pstate_get_energy_pref_index(cpudata); > + if (preference < 0) > + return preference; > + > + return sysfs_emit(buf, "%s\n", energy_perf_strings[preference]); > +} > + > cpufreq_freq_attr_ro(amd_pstate_max_freq); > cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq); > > cpufreq_freq_attr_ro(amd_pstate_highest_perf); > +cpufreq_freq_attr_rw(energy_performance_preference); > +cpufreq_freq_attr_ro(energy_performance_available_preferences); > > static struct freq_attr *amd_pstate_attr[] = { > &amd_pstate_max_freq, > @@ -614,6 +785,181 @@ static struct freq_attr *amd_pstate_attr[] = { > NULL, > }; > > +static struct freq_attr *amd_pstate_epp_attr[] = { > + &amd_pstate_max_freq, > + &amd_pstate_lowest_nonlinear_freq, > + &amd_pstate_highest_perf, > + &energy_performance_preference, > + &energy_performance_available_preferences, > + NULL, > +}; > + > +static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy) > +{ > + int min_freq, max_freq, nominal_freq, lowest_nonlinear_freq, ret; > + struct amd_cpudata *cpudata; > + struct device *dev; > + int rc; > + u64 value; > + > + dev = get_cpu_device(policy->cpu); > + if (!dev) > + goto free_cpudata1; > + > + cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL); > + if (!cpudata) > + return -ENOMEM; > + > + cpudata->cpu = policy->cpu; > + cpudata->epp_policy = 0; > + > + rc = amd_pstate_init_perf(cpudata); > + if (rc) > + goto free_cpudata1; > + > + min_freq = amd_get_min_freq(cpudata); > + max_freq = amd_get_max_freq(cpudata); > + nominal_freq = amd_get_nominal_freq(cpudata); > + lowest_nonlinear_freq = amd_get_lowest_nonlinear_freq(cpudata); > + if (min_freq < 0 || max_freq < 0 || min_freq > max_freq) { > + dev_err(dev, "min_freq(%d) or max_freq(%d) value is incorrect\n", > + min_freq, max_freq); > + ret = -EINVAL; > + goto free_cpudata1; > + } > + > + policy->cpuinfo.min_freq = min_freq; > + policy->cpuinfo.max_freq = max_freq; > + /* It will be updated by governor */ > + policy->cur = policy->cpuinfo.min_freq; > + > + /* Initial processor data capability frequencies */ > + cpudata->max_freq = max_freq; > + cpudata->min_freq = min_freq; > + cpudata->nominal_freq = nominal_freq; > + cpudata->lowest_nonlinear_freq = lowest_nonlinear_freq; > + > + policy->driver_data = cpudata; > + > + cpudata->epp_cached = amd_pstate_get_epp(cpudata, value); As the warning which reminded by 0day, should be amd_pstate_get_epp(cpudata, 0). Others look good for me, with those fixed, please feel free to add Acked-by: Huang Rui > + > + policy->min = policy->cpuinfo.min_freq; > + policy->max = policy->cpuinfo.max_freq; > + > + /* > + * Set the policy to powersave to provide a valid fallback value in case > + * the default cpufreq governor is neither powersave nor performance. > + */ > + policy->policy = CPUFREQ_POLICY_POWERSAVE; > + > + if (boot_cpu_has(X86_FEATURE_CPPC)) { > + policy->fast_switch_possible = true; > + ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value); > + if (ret) > + return ret; > + WRITE_ONCE(cpudata->cppc_req_cached, value); > + > + ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value); > + if (ret) > + return ret; > + WRITE_ONCE(cpudata->cppc_cap1_cached, value); > + } > + amd_pstate_boost_init(cpudata); > + > + return 0; > + > +free_cpudata1: > + kfree(cpudata); > + return ret; > +} > + > +static int amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy) > +{ > + pr_debug("CPU %d exiting\n", policy->cpu); > + policy->fast_switch_possible = false; > + return 0; > +} > + > +static void amd_pstate_epp_init(unsigned int cpu) > +{ > + struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); > + struct amd_cpudata *cpudata = policy->driver_data; > + u32 max_perf, min_perf; > + u64 value; > + s16 epp; > + > + max_perf = READ_ONCE(cpudata->highest_perf); > + min_perf = READ_ONCE(cpudata->lowest_perf); > + > + value = READ_ONCE(cpudata->cppc_req_cached); > + > + if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) > + min_perf = max_perf; > + > + /* Initial min/max values for CPPC Performance Controls Register */ > + value &= ~AMD_CPPC_MIN_PERF(~0L); > + value |= AMD_CPPC_MIN_PERF(min_perf); > + > + value &= ~AMD_CPPC_MAX_PERF(~0L); > + value |= AMD_CPPC_MAX_PERF(max_perf); > + > + /* CPPC EPP feature require to set zero to the desire perf bit */ > + value &= ~AMD_CPPC_DES_PERF(~0L); > + value |= AMD_CPPC_DES_PERF(0); > + > + if (cpudata->epp_policy == cpudata->policy) > + goto skip_epp; > + > + cpudata->epp_policy = cpudata->policy; > + > + if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) { > + epp = amd_pstate_get_epp(cpudata, value); > + if (epp < 0) > + goto skip_epp; > + /* force the epp value to be zero for performance policy */ > + epp = 0; > + } else { > + /* Get BIOS pre-defined epp value */ > + epp = amd_pstate_get_epp(cpudata, value); > + if (epp) > + goto skip_epp; > + } > + /* Set initial EPP value */ > + if (boot_cpu_has(X86_FEATURE_CPPC)) { > + value &= ~GENMASK_ULL(31, 24); > + value |= (u64)epp << 24; > + } > + > +skip_epp: > + WRITE_ONCE(cpudata->cppc_req_cached, value); > + amd_pstate_set_epp(cpudata, epp); > + cpufreq_cpu_put(policy); > +} > + > +static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy) > +{ > + struct amd_cpudata *cpudata = policy->driver_data; > + > + if (!policy->cpuinfo.max_freq) > + return -ENODEV; > + > + pr_debug("set_policy: cpuinfo.max %u policy->max %u\n", > + policy->cpuinfo.max_freq, policy->max); > + > + cpudata->policy = policy->policy; > + > + amd_pstate_epp_init(policy->cpu); > + > + return 0; > +} > + > +static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy) > +{ > + cpufreq_verify_within_cpu_limits(policy); > + pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min); > + return 0; > +} > + > static struct cpufreq_driver amd_pstate_driver = { > .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS, > .verify = amd_pstate_verify, > @@ -627,6 +973,16 @@ static struct cpufreq_driver amd_pstate_driver = { > .attr = amd_pstate_attr, > }; > > +static struct cpufreq_driver amd_pstate_epp_driver = { > + .flags = CPUFREQ_CONST_LOOPS, > + .verify = amd_pstate_epp_verify_policy, > + .setpolicy = amd_pstate_epp_set_policy, > + .init = amd_pstate_epp_cpu_init, > + .exit = amd_pstate_epp_cpu_exit, > + .name = "amd_pstate_epp", > + .attr = amd_pstate_epp_attr, > +}; > + > static int __init amd_pstate_init(void) > { > int ret; > @@ -655,7 +1011,8 @@ static int __init amd_pstate_init(void) > /* capability check */ > if (boot_cpu_has(X86_FEATURE_CPPC)) { > pr_debug("AMD CPPC MSR based functionality is supported\n"); > - amd_pstate_driver.adjust_perf = amd_pstate_adjust_perf; > + if (cppc_state == AMD_PSTATE_PASSIVE) > + current_pstate_driver->adjust_perf = amd_pstate_adjust_perf; > } else { > pr_debug("AMD CPPC shared memory based functionality is supported\n"); > static_call_update(amd_pstate_enable, cppc_enable); > @@ -666,14 +1023,13 @@ static int __init amd_pstate_init(void) > /* enable amd pstate feature */ > ret = amd_pstate_enable(true); > if (ret) { > - pr_err("failed to enable amd-pstate with return %d\n", ret); > + pr_err("failed to enable with return %d\n", ret); > return ret; > } > > - ret = cpufreq_register_driver(&amd_pstate_driver); > + ret = cpufreq_register_driver(current_pstate_driver); > if (ret) > - pr_err("failed to register amd_pstate_driver with return %d\n", > - ret); > + pr_err("failed to register with return %d\n", ret); > > return ret; > } > @@ -695,6 +1051,12 @@ static int __init amd_pstate_param(char *str) > if (cppc_state == AMD_PSTATE_DISABLE) > pr_info("driver is explicitly disabled\n"); > > + if (cppc_state == AMD_PSTATE_ACTIVE) > + current_pstate_driver = &amd_pstate_epp_driver; > + > + if (cppc_state == AMD_PSTATE_PASSIVE) > + current_pstate_driver = &amd_pstate_driver; > + > return 0; > } > > diff --git a/include/linux/amd-pstate.h b/include/linux/amd-pstate.h > index dae2ce0f6735..8341a2a2948a 100644 > --- a/include/linux/amd-pstate.h > +++ b/include/linux/amd-pstate.h > @@ -47,6 +47,10 @@ struct amd_aperf_mperf { > * @prev: Last Aperf/Mperf/tsc count value read from register > * @freq: current cpu frequency value > * @boost_supported: check whether the Processor or SBIOS supports boost mode > + * @epp_policy: Last saved policy used to set energy-performance preference > + * @epp_cached: Cached CPPC energy-performance preference value > + * @policy: Cpufreq policy value > + * @cppc_cap1_cached Cached MSR_AMD_CPPC_CAP1 register value > * > * The amd_cpudata is key private data for each CPU thread in AMD P-State, and > * represents all the attributes and goals that AMD P-State requests at runtime. > @@ -72,6 +76,12 @@ struct amd_cpudata { > > u64 freq; > bool boost_supported; > + > + /* EPP feature related attributes*/ > + s16 epp_policy; > + s16 epp_cached; > + u32 policy; > + u64 cppc_cap1_cached; > }; > > /* > -- > 2.34.1 >