Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp28430ybj; Wed, 6 May 2020 10:47:49 -0700 (PDT) X-Google-Smtp-Source: APiQypImlcWQaOz+w4y4gUo4wW8JGSgB3d+jCsGfNK+iouXinYqq7YuwXCRk8n3hOuwybsm3zFMx X-Received: by 2002:aa7:ce0f:: with SMTP id d15mr8142915edv.327.1588787269762; Wed, 06 May 2020 10:47:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588787269; cv=none; d=google.com; s=arc-20160816; b=pMU82s+L/sQV3mC9Xq9klPSSvhmeMkcdWWZzCiozKBzcrMPlpoIeqAidHUSZXm2f0x u15uIdueImO3rFMmYVNL0WsNazGNljjCjkaitUS6HgkydJt2YvkXWT8GonnxitvIqq/C oiIpW0lDd/KRAdlpt4piz/XaINnjzy9wXitzSyCOYL01x6qdX9NhN53WCX23dL+w+xE7 Nk9W0tPNxvtVCstRIA6u3/h2UxdmBPJujvoq5uESyNqISad4gjlruDROREZLvvw645vz nu7vRxP6vBHVDpM4hYXarcaM+Ni18DTwXYkREXvzT8YFp2wfxUlwSEqC9o7lWZYwq4uI 0wiA== 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; bh=Loty5fSkr4J1+imkrjLx4bXdzxK+sYS+csyKwjwuAGg=; b=Y6Yqhi/QK5CChD7u6JLXPLte3AMFM09CEQ/lyF72/+ruOmIjD9qulayVC1KOfNoA6Q 7BTRl40e1tUqy7LbqiBcErHv8WBtbUwdK9Kuhz+5MVRm6te96t59tGOvhLNfASe+zfbv DsKOPMifSur2+NsKjNlqE2d8n86qOGrxMn0/7te+y2JEURLi27yTecQIdlCVk4WfATnN NL7okzptZI6Svl5/l0mnEjkpSyCm/zLy45xdzYMfSt+ZrLSlFzU5VOs+asWJgVaD8iMu FZvj+4eHR8O+oNA+iYWU+r9zk1Sx2CcozkuthOB1cgeOhsSH9ZoZt2845VT7OCKBuGPJ JH3g== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id by15si1652100ejb.229.2020.05.06.10.47.26; Wed, 06 May 2020 10:47:49 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730483AbgEFRoz (ORCPT + 99 others); Wed, 6 May 2020 13:44:55 -0400 Received: from mail.baikalelectronics.com ([87.245.175.226]:32936 "EHLO mail.baikalelectronics.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730294AbgEFRoa (ORCPT ); Wed, 6 May 2020 13:44:30 -0400 Received: from localhost (unknown [127.0.0.1]) by mail.baikalelectronics.ru (Postfix) with ESMTP id 26C9D8000BA8; Wed, 6 May 2020 17:44:24 +0000 (UTC) X-Virus-Scanned: amavisd-new at baikalelectronics.ru Received: from mail.baikalelectronics.ru ([127.0.0.1]) by localhost (mail.baikalelectronics.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3k9N9oB7K8-w; Wed, 6 May 2020 20:44:23 +0300 (MSK) From: To: Thomas Bogendoerfer CC: Serge Semin , Serge Semin , Alexey Malahov , Paul Burton , Ralf Baechle , Arnd Bergmann , Rob Herring , , , Greg Kroah-Hartman , Thomas Gleixner , Allison Randal , , Subject: [PATCH v2 17/20] mips: Add udelay lpj numbers adjustment Date: Wed, 6 May 2020 20:42:35 +0300 Message-ID: <20200506174238.15385-18-Sergey.Semin@baikalelectronics.ru> In-Reply-To: <20200506174238.15385-1-Sergey.Semin@baikalelectronics.ru> References: <20200306124807.3596F80307C2@mail.baikalelectronics.ru> <20200506174238.15385-1-Sergey.Semin@baikalelectronics.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-ClientProxiedBy: MAIL.baikal.int (192.168.51.25) To mail (192.168.51.25) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Serge Semin 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 Cc: Thomas Bogendoerfer Cc: Paul Burton Cc: Ralf Baechle Cc: Arnd Bergmann Cc: Rob Herring Cc: linux-pm@vger.kernel.org Cc: devicetree@vger.kernel.org --- 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 37e9413a393d..ce89e18af024 100644 --- a/arch/mips/kernel/time.c +++ b/arch/mips/kernel/time.c @@ -18,12 +18,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