Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp78357ybg; Mon, 8 Jun 2020 16:58:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxC0ZPYkeue1oth5nEfP03Zfn4uTXeM2fVMviiWa8AtN6YtsgAjKVzMUTkVjG1ukA20usMD X-Received: by 2002:a17:906:d93c:: with SMTP id rn28mr24366542ejb.190.1591660722009; Mon, 08 Jun 2020 16:58:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591660722; cv=none; d=google.com; s=arc-20160816; b=D5YsfaSCrF3vQGM7IEhVLNKWITgpGLrB5Yv206BHhUngA5WxTBM6zaaWlZlrXK6GYP eyDVnLtTCO34GfiApijepPQP4P6mYGNJwONN5cBDFKjfOxEvaKMlG48GrVsaJVH+VgPS OedEHIFcb2K6ysjzbh0nzxd8Hjz/nOCspoJE6x1wsFmPw0pRInvuESLD2yEowoFfASu1 Jpeqb8ot25UMRtqIFHOZSMPmvM03R2rSv3M5gR91wNEAzXuFU/6LGOGAUiXePyfm4dXH OMv0fpSEYZo0JT5AMU7SbRLYO0fy6+3ZCgo/IGnS7LnKAvJWreNJfqspv5+IhC1l3qIg 7i4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=A10jC15dN5yB86BPC+JNaqFFI606QvARt8DuP3Ed+zw=; b=stJKK4rFKRnP5PxCNozeduJMZFDIWZxm0PZOLcW8auFnQgtLCm5AcCheZ4p0xEPB0h JeFa11DT2yo4ZVHI7z1OdoiFPn0P+9NihdyJcwitdf451YkFXCzEzMy28MRmRc0pbXYX W0Yto1ZgKFTZtlYbaWZkgDgiROb9V+ejj6BxHWKCKCULrRcFsjlBTdD4g1FY2ZMNV5st 2pETMULRb7d+xhPb25+1cyvHuJF3/2oWujX98FaYqpqwhpw/WKTQdbAsleCXbPnzlzGp Nl1QoWNMuFJvBolG6leQPm1p8/N/oPUDy9HGe0b7teyvQQBa3YnFMnyVATmCpWqcUkoF w2gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=DmEsOlH1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d5si9777924ejw.224.2020.06.08.16.58.19; Mon, 08 Jun 2020 16:58:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=DmEsOlH1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732677AbgFHXxh (ORCPT + 99 others); Mon, 8 Jun 2020 19:53:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:50446 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729935AbgFHXYZ (ORCPT ); Mon, 8 Jun 2020 19:24:25 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9859120CC7; Mon, 8 Jun 2020 23:24:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1591658664; bh=rCFqFPN/S5hYmSyfe+QjOdpNml/We5jXqm0Wk0oj8e8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DmEsOlH11NRPEXAnGxujjcGfsUWtpA5MyacxSf+yi6FV6zM6+5cY94AAZO+bPy5YV reQ7xwSaR9/gP+lWxH5pwxBnwLrurmClsA2ce6xq39L4dZDvFPDjI8pkNeT91OtKXJ Di+62KKPiIW3QGi+UR+ge9RRZswI+RsEW1Tk3GZQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Serge Semin , Alexey Malahov , Jiaxun Yang , Thomas Bogendoerfer , Paul Burton , Ralf Baechle , Arnd Bergmann , Rob Herring , devicetree@vger.kernel.org, Sasha Levin , linux-mips@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 080/106] mips: Add udelay lpj numbers adjustment Date: Mon, 8 Jun 2020 19:22:12 -0400 Message-Id: <20200608232238.3368589-80-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200608232238.3368589-1-sashal@kernel.org> References: <20200608232238.3368589-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Serge Semin [ Upstream commit ed26aacfb5f71eecb20a51c4467da440cb719d66 ] Loops-per-jiffies is a special number which represents a number of noop-loop cycles per CPU-scheduler quantum - jiffies. As you understand aside from CPU-specific implementation it depends on the CPU frequency. So when a platform has the CPU frequency fixed, we have no problem and the current udelay interface will work just fine. But as soon as CPU-freq driver is enabled and the cores frequency changes, we'll end up with distorted udelay's. In order to fix this we have to accordinly adjust the per-CPU udelay_val (the same as the global loops_per_jiffy) number. This can be done in the CPU-freq transition event handler. We subscribe to that event in the MIPS arch time-inititalization method. Co-developed-by: Alexey Malahov Signed-off-by: Alexey Malahov Signed-off-by: Serge Semin Reviewed-by: Jiaxun Yang Cc: Thomas Bogendoerfer Cc: Paul Burton Cc: Ralf Baechle Cc: Arnd Bergmann Cc: Rob Herring Cc: devicetree@vger.kernel.org Signed-off-by: Thomas Bogendoerfer Signed-off-by: Sasha Levin --- arch/mips/kernel/time.c | 70 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/arch/mips/kernel/time.c b/arch/mips/kernel/time.c index bfe02ded25d1..1e631a484ddf 100644 --- a/arch/mips/kernel/time.c +++ b/arch/mips/kernel/time.c @@ -22,12 +22,82 @@ #include #include #include +#include +#include #include #include #include #include +#ifdef CONFIG_CPU_FREQ + +static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref); +static DEFINE_PER_CPU(unsigned long, pcp_lpj_ref_freq); +static unsigned long glb_lpj_ref; +static unsigned long glb_lpj_ref_freq; + +static int cpufreq_callback(struct notifier_block *nb, + unsigned long val, void *data) +{ + struct cpufreq_freqs *freq = data; + struct cpumask *cpus = freq->policy->cpus; + unsigned long lpj; + int cpu; + + /* + * Skip lpj numbers adjustment if the CPU-freq transition is safe for + * the loops delay. (Is this possible?) + */ + if (freq->flags & CPUFREQ_CONST_LOOPS) + return NOTIFY_OK; + + /* Save the initial values of the lpjes for future scaling. */ + if (!glb_lpj_ref) { + glb_lpj_ref = boot_cpu_data.udelay_val; + glb_lpj_ref_freq = freq->old; + + for_each_online_cpu(cpu) { + per_cpu(pcp_lpj_ref, cpu) = + cpu_data[cpu].udelay_val; + per_cpu(pcp_lpj_ref_freq, cpu) = freq->old; + } + } + + /* + * Adjust global lpj variable and per-CPU udelay_val number in + * accordance with the new CPU frequency. + */ + if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) || + (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) { + loops_per_jiffy = cpufreq_scale(glb_lpj_ref, + glb_lpj_ref_freq, + freq->new); + + for_each_cpu(cpu, cpus) { + lpj = cpufreq_scale(per_cpu(pcp_lpj_ref, cpu), + per_cpu(pcp_lpj_ref_freq, cpu), + freq->new); + cpu_data[cpu].udelay_val = (unsigned int)lpj; + } + } + + return NOTIFY_OK; +} + +static struct notifier_block cpufreq_notifier = { + .notifier_call = cpufreq_callback, +}; + +static int __init register_cpufreq_notifier(void) +{ + return cpufreq_register_notifier(&cpufreq_notifier, + CPUFREQ_TRANSITION_NOTIFIER); +} +core_initcall(register_cpufreq_notifier); + +#endif /* CONFIG_CPU_FREQ */ + /* * forward reference */ -- 2.25.1