Skip to contentSkip to conversation

cpufreq: Add sfi based cpufreq driver support

PATCH Superseded Kasagar, Srinidhi 2 participants 14 messages Oct 7, 2014 – Oct 10, 2014 CPU frequency scaling framework

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpufreq: Add sfi based cpufreq driver support

Quoted 1 line

From b3037348db47f0629316dd0027c7f1a1b28be959 Mon Sep 17 00:00:00 2001

From: Srinidhi Kasagar <[email protected]>
Date: Fri, 12 Sep 2014 22:52:37 +0530
Subject: [PATCH] cpufreq: Add sfi based cpufreq driver support

This adds the sfi based cpu freq driver for some of the Intel's Silver Mont based atom architectures like Merrifield and Moorfield (intel-mid)

---
 drivers/cpufreq/Kconfig.x86   |   10 +
 drivers/cpufreq/Makefile      |    1 +
 drivers/cpufreq/sfi-cpufreq.c |  426 +++++++++++++++++++++++++++++++++++++++++
 drivers/cpufreq/sfi-cpufreq.h |   53 +++++
 4 files changed, 490 insertions(+)
 create mode 100644 drivers/cpufreq/sfi-cpufreq.c
 create mode 100644 drivers/cpufreq/sfi-cpufreq.h
drivers/cpufreq/Kconfig.x86 +10 -0
index 89ae88f91895..3d6886b558fa 100644
@@ -57,6 +57,16 @@ config X86_ACPI_CPUFREQ_CPB
 	  By enabling this option the acpi_cpufreq driver provides the old
 	  entry in addition to the new boost ones, for compatibility reasons.
 
+config X86_SFI_CPUFREQ
+	tristate "SFI Processor P-States driver"
+	depends on X86_INTEL_MID && SFI
+	help
+	  This driver adds a CPUFreq driver which utilizes the SFI
+	  Processor Performance States enumeration on some Silver Mont
+	  based Intel Atom architecture (like intel-mid)
+
+	  If in doubt, say N.
+
 config ELAN_CPUFREQ
 	tristate "AMD Elan SC400 and SC410"
 	depends on MELAN
drivers/cpufreq/Makefile +1 -0
index db6d9a2fea4d..c3b51efd4a85 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_X86_P4_CLOCKMOD)		+= p4-clockmod.o
 obj-$(CONFIG_X86_CPUFREQ_NFORCE2)	+= cpufreq-nforce2.o
 obj-$(CONFIG_X86_INTEL_PSTATE)		+= intel_pstate.o
 obj-$(CONFIG_X86_AMD_FREQ_SENSITIVITY)	+= amd_freq_sensitivity.o
+obj-$(CONFIG_X86_SFI_CPUFREQ)		+= sfi-cpufreq.o
 
 ##################################################################################
 # ARM SoC drivers
drivers/cpufreq/sfi-cpufreq.c new file +426 -0

426 lines not on this page · raw message

drivers/cpufreq/sfi-cpufreq.h new file +53 -0

53 lines not on this page · raw message

Signature · 1 line
1.7.9.5
maintainer
#1 lore raw

Adding Dirk as well..

Quoted 4 lines · Srinidhi

From b3037348db47f0629316dd0027c7f1a1b28be959 Mon Sep 17 00:00:00 2001
From: Srinidhi Kasagar <[email protected]>
Date: Fri, 12 Sep 2014 22:52:37 +0530
Subject: [PATCH] cpufreq: Add sfi based cpufreq driver support

You didn't use git send-email ?

Quoted 20 lines

This adds the sfi based cpu freq driver for some of the Intel's Silver Mont based atom architectures like Merrifield and Moorfield (intel-mid)

---
 drivers/cpufreq/Kconfig.x86   |   10 +
 drivers/cpufreq/Makefile      |    1 +
 drivers/cpufreq/sfi-cpufreq.c |  426 +++++++++++++++++++++++++++++++++++++++++
 drivers/cpufreq/sfi-cpufreq.h |   53 +++++
 4 files changed, 490 insertions(+)
 create mode 100644 drivers/cpufreq/sfi-cpufreq.c
 create mode 100644 drivers/cpufreq/sfi-cpufreq.h
drivers/cpufreq/Kconfig.x86 +0 -0
index 89ae88f91895..3d6886b558fa 100644

drivers/cpufreq/Kconfig.x86 config X86_ACPI_CPUFREQ_CPB · line 64

Quoted 8 lines
drivers/cpufreq/Kconfig.x86 +5 -0
@@ -57,6 +57,16 @@ config X86_ACPI_CPUFREQ_CPB
          By enabling this option the acpi_cpufreq driver provides the old
          entry in addition to the new boost ones, for compatibility reasons.
 
+config X86_SFI_CPUFREQ
+       tristate "SFI Processor P-States driver"
+       depends on X86_INTEL_MID && SFI
+       help
+         This driver adds a CPUFreq driver which utilizes the SFI

s/This driver/This/

drivers/cpufreq/Kconfig.x86 config X86_ACPI_CPUFREQ_CPB · line 66

Quoted 2 lines
drivers/cpufreq/Kconfig.x86 +2 -0
+         Processor Performance States enumeration on some Silver Mont
+         based Intel Atom architecture (like intel-mid)

Looks like the sentence isn't complete here, would you like to?

Quoted 24 lines
drivers/cpufreq/Kconfig.x86 +3 -0
+
+         If in doubt, say N.
+
 config ELAN_CPUFREQ
        tristate "AMD Elan SC400 and SC410"
        depends on MELAN
drivers/cpufreq/Makefile +1 -0
index db6d9a2fea4d..c3b51efd4a85 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_X86_P4_CLOCKMOD)         += p4-clockmod.o
 obj-$(CONFIG_X86_CPUFREQ_NFORCE2)      += cpufreq-nforce2.o
 obj-$(CONFIG_X86_INTEL_PSTATE)         += intel_pstate.o
 obj-$(CONFIG_X86_AMD_FREQ_SENSITIVITY) += amd_freq_sensitivity.o
+obj-$(CONFIG_X86_SFI_CPUFREQ)          += sfi-cpufreq.o
 
 ##################################################################################
 # ARM SoC drivers
drivers/cpufreq/sfi-cpufreq.c new file +0 -0
new file mode 100644
index 000000000000..1400e0387528

drivers/cpufreq/sfi-cpufreq.c line 2

Quoted 2 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
@@ -0,0 +1,426 @@
+/*
+ *  sfi_cpufreq.c - sfi Processor P-States Driver

No need of mentioning file name here.

drivers/cpufreq/sfi-cpufreq.c line 4

Quoted 2 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
+ *  Based on ACPI Processor P-States Driver
+ *

How much different is it from that ? Sorry I haven't checked :(

I mean, will it be possible to make changes to acpi-cpufreq driver or get the common part out?

Quoted 9 lines
drivers/cpufreq/sfi-cpufreq.c new file +9 -0
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *

drivers/cpufreq/sfi-cpufreq.c line 23

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+ *  Author: Vishwesh M Rudramuni <[email protected]>
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/smp.h>
+#include <linux/cpufreq.h>
+#include <linux/slab.h>
+#include <linux/sfi.h>

Keep them in ascending order please.

drivers/cpufreq/sfi-cpufreq.c line 28

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+#include <asm/msr.h>
+#include <asm/processor.h>
+
+#include "sfi-cpufreq.h"

Do you plan to share the definitions here with any other file? If no, please merge the .h file here only.

drivers/cpufreq/sfi-cpufreq.c line 33

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+#define SFI_FREQ_MAX           32
+#define INTEL_MSR_RANGE                0xffff
+#define INTEL_MSR_BUSRATIO_MASK        0xff00
+#define SFI_CPU_MAX            8

Aligning the values in a single column is preferred for readability.

Quoted 176 lines
drivers/cpufreq/sfi-cpufreq.c new file +176 -0
+
+DEFINE_PER_CPU(struct sfi_processor *, sfi_processors);
+
+static DEFINE_MUTEX(performance_mutex);
+static DEFINE_PER_CPU(struct sfi_cpufreq_data *, drv_data);
+struct sfi_freq_table_entry sfi_cpufreq_array[SFI_FREQ_MAX];
+static int sfi_cpufreq_num;
+
+/* sfi_perf_data is a pointer to percpu data. */
+static struct sfi_processor_performance *sfi_perf_data;
+static struct cpufreq_driver sfi_cpufreq_driver;
+
+struct sfi_cpufreq_data {
+       struct sfi_processor_performance *sfi_data;
+       struct cpufreq_frequency_table *freq_table;
+       unsigned int resume;
+};
+
+static int parse_freq(struct sfi_table_header *table)
+{
+       struct sfi_table_simple *sb;
+       struct sfi_freq_table_entry *pentry;
+       int total_len;
+
+       sb = (struct sfi_table_simple *)table;
+       if (!sb) {
+               printk(KERN_WARNING "SFI: Unable to map FREQ\n");
+               return -ENODEV;
+       }
+
+       if (!sfi_cpufreq_num) {
+               sfi_cpufreq_num = SFI_GET_NUM_ENTRIES(sb,
+                        struct sfi_freq_table_entry);
+               pentry = (struct sfi_freq_table_entry *)sb->pentry;
+               total_len = sfi_cpufreq_num * sizeof(*pentry);
+               memcpy(sfi_cpufreq_array, pentry, total_len);
+       }
+
+       return 0;
+}
+
+static int sfi_processor_get_performance_states(struct sfi_processor *pr)
+{
+       int result = 0;
+       int i;
+
+       pr->performance->state_count = sfi_cpufreq_num;
+       pr->performance->states =
+           kmalloc(sizeof(struct sfi_processor_px) * sfi_cpufreq_num,
+                   GFP_KERNEL);
+       if (!pr->performance->states)
+               result = -ENOMEM;
+
+       printk(KERN_INFO "Num p-states %d\n", sfi_cpufreq_num);
+
+       /* Populate the P-states info from the SFI table here */
+       for (i = 0; i < sfi_cpufreq_num; i++) {
+               pr->performance->states[i].core_frequency =
+                       sfi_cpufreq_array[i].freq_mhz;
+               pr->performance->states[i].transition_latency =
+                       sfi_cpufreq_array[i].latency;
+               pr->performance->states[i].control =
+                       sfi_cpufreq_array[i].ctrl_val;
+               printk(KERN_INFO "State [%d]: core_frequency[%d] transition_latency[%d] control[0x%x]\n",
+                       i,
+                       (u32) pr->performance->states[i].core_frequency,
+                       (u32) pr->performance->states[i].transition_latency,
+                       (u32) pr->performance->states[i].control);
+       }
+
+       return result;
+}
+
+static int sfi_processor_register_performance(struct sfi_processor_performance
+                                   *performance, unsigned int cpu)
+{
+       struct sfi_processor *pr;
+
+       mutex_lock(&performance_mutex);
+
+       pr = per_cpu(sfi_processors, cpu);
+       if (!pr) {
+               mutex_unlock(&performance_mutex);
+               return -ENODEV;
+       }
+
+       if (pr->performance) {
+               mutex_unlock(&performance_mutex);
+               return -EBUSY;
+       }
+
+       WARN_ON(!performance);
+
+       pr->performance = performance;
+
+       /* parse the freq table from sfi */
+       sfi_cpufreq_num = 0;
+       sfi_table_parse(SFI_SIG_FREQ, NULL, NULL, parse_freq);
+
+       sfi_processor_get_performance_states(pr);
+
+       mutex_unlock(&performance_mutex);
+       return 0;
+}
+
+void sfi_processor_unregister_performance(struct sfi_processor_performance
+                                     *performance, unsigned int cpu)
+{
+       struct sfi_processor *pr;
+
+       mutex_lock(&performance_mutex);
+
+       pr = per_cpu(sfi_processors, cpu);
+       if (!pr) {
+               mutex_unlock(&performance_mutex);
+               return;
+       }
+
+       if (pr->performance)
+               kfree(pr->performance->states);
+       pr->performance = NULL;
+
+       mutex_unlock(&performance_mutex);
+}
+
+static unsigned extract_freq(u32 msr, struct sfi_cpufreq_data *data)
+{
+       struct cpufreq_frequency_table *pos;
+       struct sfi_processor_performance *perf;
+       u32 sfi_ctrl;
+
+       msr &= INTEL_MSR_BUSRATIO_MASK;
+       perf = data->sfi_data;
+
+       cpufreq_for_each_entry(pos, data->freq_table) {
+               sfi_ctrl = perf->states[pos->driver_data].control
+                       & INTEL_MSR_BUSRATIO_MASK;
+               if (sfi_ctrl == msr)
+                       return pos->frequency;
+       }
+       return data->freq_table[0].frequency;
+}
+
+static u32 get_cur_val(const struct cpumask *mask)
+{
+       u32 val, dummy;
+
+       if (unlikely(cpumask_empty(mask)))
+               return 0;
+
+       rdmsr_on_cpu(cpumask_any(mask), MSR_IA32_PERF_STATUS, &val, &dummy);
+
+       return val;
+}
+
+static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, cpu);
+       unsigned int freq;
+       unsigned int cached_freq;
+
+       pr_debug("get_cur_freq_on_cpu (%d)\n", cpu);
+
+       if (unlikely(data == NULL ||
+               data->sfi_data == NULL || data->freq_table == NULL)) {
+               return 0;
+       }
+
+       cached_freq = data->freq_table[data->sfi_data->state].frequency;
+       freq = extract_freq(get_cur_val(cpumask_of(cpu)), data);
+       if (freq != cached_freq)
+                /* Force the frequency on next target call */
+               data->resume = 1;
+
+       pr_debug("cur freq = %u\n", freq);
+

drivers/cpufreq/sfi-cpufreq.c line 219

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+       return freq;
+}
+
+static int sfi_cpufreq_target(struct cpufreq_policy *policy,
+                               unsigned int index)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+       struct sfi_processor_performance *perf;
+       unsigned int next_perf_state = 0; /* Index into perf table */
+       u32 lo, hi;

You meant unsigned int only?

drivers/cpufreq/sfi-cpufreq.c line 224

Quoted 5 lines
drivers/cpufreq/sfi-cpufreq.c new file +5 -0
+
+       if (unlikely(data == NULL ||
+            data->sfi_data == NULL || data->freq_table == NULL)) {
+               return -ENODEV;
+       }

Probably this looks better:

       if (unlikely(!data || !data->sfi_data || !data->freq_table))
               return -ENODEV;

But do we need this check at all ? Also same comments for similar usage in above routines.

drivers/cpufreq/sfi-cpufreq.c line 228

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+
+       perf = data->sfi_data;
+       next_perf_state = data->freq_table[index].driver_data;
+       if (perf->state == next_perf_state) {

Can this happen? Probably cpufreq core will never do it ?

Quoted 18 lines
drivers/cpufreq/sfi-cpufreq.c new file +18 -0
+               if (unlikely(data->resume)) {
+                       pr_debug("Called after resume, resetting to P%d\n",
+                               next_perf_state);
+                       data->resume = 0;
+               } else {
+                       pr_debug("Already at target state (P%d)\n",
+                               next_perf_state);
+                       return 0;
+               }
+       }
+
+       rdmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, &lo, &hi);
+       lo = (lo & ~INTEL_MSR_RANGE) |
+               ((u32) perf->states[next_perf_state].control & INTEL_MSR_RANGE);
+       wrmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, lo, hi);
+
+       perf->state = next_perf_state;
+

drivers/cpufreq/sfi-cpufreq.c line 256

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+       return 0;
+}
+
+static int sfi_cpufreq_cpu_init(struct cpufreq_policy *policy)
+{
+       unsigned int i;
+       unsigned int valid_states = 0;
+       unsigned int cpu = policy->cpu;
+       struct sfi_cpufreq_data *data;
+       unsigned int result = 0;

Can merge all definitions of similar data type in a single line.

drivers/cpufreq/sfi-cpufreq.c line 260

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+       struct sfi_processor_performance *perf;
+       struct sfi_processor *pr;
+
+       pr = kzalloc(sizeof(struct sfi_processor), GFP_KERNEL);

sizeof(*pr)

drivers/cpufreq/sfi-cpufreq.c line 266

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+       if (!pr)
+               return -ENOMEM;
+
+       per_cpu(sfi_processors, cpu) = pr;
+
+       pr_debug("sfi_cpufreq_cpu_init CPU:%d\n", policy->cpu);

I would prefer
pr_debug("%s: CPU:%d\n", __func__, policy->cpu);

drivers/cpufreq/sfi-cpufreq.c line 268

Quoted 2 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
+
+       data = kzalloc(sizeof(struct sfi_cpufreq_data), GFP_KERNEL);

sizeof(*data)

drivers/cpufreq/sfi-cpufreq.c line 270

Quoted 2 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
+       if (!data)
+               return -ENOMEM;

What about freeing pr ?

drivers/cpufreq/sfi-cpufreq.c line 275

Quoted 5 lines
drivers/cpufreq/sfi-cpufreq.c new file +5 -0
+
+       data->sfi_data = per_cpu_ptr(sfi_perf_data, cpu);
+       per_cpu(drv_data, cpu) = data;
+
+       sfi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;

Why here? and not in the structure definition itself..

drivers/cpufreq/sfi-cpufreq.c line 285

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+
+       result = sfi_processor_register_performance(data->sfi_data, cpu);
+       if (result)
+               goto err_free;
+
+       perf = data->sfi_data;
+       policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
+
+       cpumask_set_cpu(policy->cpu, policy->cpus);
+       cpumask_set_cpu(policy->cpu, policy->related_cpus);

You don't have to set related_cpus, its done by core. Also policy->cpus is already set by core to policy->cpu.

drivers/cpufreq/sfi-cpufreq.c line 294

Quoted 9 lines
drivers/cpufreq/sfi-cpufreq.c new file +9 -0
+
+       /* capability check */
+       if (perf->state_count <= 1) {
+               pr_debug("No P-States\n");
+               result = -ENODEV;
+               goto err_unreg;
+       }
+
+       data->freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *

sizeof (*data->freq_table)

Also, you want to do kzalloc? or kmalloc will work as well ?

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+                   (perf->state_count+1), GFP_KERNEL);
+       if (!data->freq_table) {
+               result = -ENOMEM;
+               goto err_unreg;
+       }
+
+       /* detect transition latency */
+       policy->cpuinfo.transition_latency = 0;
+       for (i = 0; i < perf->state_count; i++) {
+               if ((perf->states[i].transition_latency * 1000) >

drivers/cpufreq/sfi-cpufreq.c line 314

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+                   policy->cpuinfo.transition_latency)
+                       policy->cpuinfo.transition_latency =
+                           perf->states[i].transition_latency * 1000;
+       }
+
+       /* initialize the freq table */
+       for (i = 0; i < perf->state_count; i++) {
+               if (i > 0 && perf->states[i].core_frequency >=
+                   data->freq_table[valid_states-1].frequency / 1000)
+                       continue;

What are you doing here ?

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+
+               data->freq_table[valid_states].driver_data = i;
+               data->freq_table[valid_states].frequency =
+                   perf->states[i].core_frequency * 1000;

drivers/cpufreq/sfi-cpufreq.c line 328

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+               valid_states++;
+       }
+       data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
+       perf->state = 0;
+
+       result = cpufreq_table_validate_and_show(policy, data->freq_table);
+       if (result)
+               goto err_freqfree;
+
+       policy->cur = get_cur_freq_on_cpu(cpu);

This is done by core and so you need to do it.

drivers/cpufreq/sfi-cpufreq.c line 335

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+       pr_debug("CPU%u - SFI performance management activated.\n", cpu);
+       for (i = 0; i < perf->state_count; i++)
+               pr_debug("     %cP%d: %d MHz, %d uS\n",
+                       (i == perf->state ? '*' : ' '), i,
+                       (u32) perf->states[i].core_frequency,
+                       (u32) perf->states[i].transition_latency);

What about printing this in the above for loop only?

drivers/cpufreq/sfi-cpufreq.c line 347

Quoted 12 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+
+       data->resume = 1;
+
+       return result;
+
+err_freqfree:
+       kfree(data->freq_table);
+err_unreg:
+       sfi_processor_unregister_performance(perf, cpu);
+err_free:
+       kfree(data);
+       per_cpu(drv_data, cpu) = NULL;

Free pr ?

drivers/cpufreq/sfi-cpufreq.c line 359

Quoted 12 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+
+       return result;
+}
+
+static int sfi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+       struct sfi_processor *pr = per_cpu(sfi_processors, policy->cpu);
+
+       pr_debug("sfi_cpufreq_cpu_exit\n");
+
+       if (data) {

Why will data be NULL here ?

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+               per_cpu(drv_data, policy->cpu) = NULL;
+               sfi_processor_unregister_performance(data->sfi_data,
+                                               policy->cpu);
+               kfree(data->freq_table);

drivers/cpufreq/sfi-cpufreq.c line 373

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+               kfree(data);
+               kfree(pr);
+       }
+
+       return 0;
+}
+
+static int sfi_cpufreq_resume(struct cpufreq_policy *policy)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);

Your code is dependent on policy->cpu at multiple places and it can change on CPU hotplug. You should worry about it only if there can be more than one CPU in a single policy, which doesn't look like the case.

Also there is a patch from Thomas Petazzoni
[PATCHv2 1/4] cpufreq: allow driver-specific data
https://www.mail-archive.com/[email protected]/msg10696.html

you can use that for data.

drivers/cpufreq/sfi-cpufreq.c line 385

Quoted 12 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+
+       pr_debug("sfi_cpufreq_resume\n");
+
+       data->resume = 1;
+
+       return 0;
+}
+
+static struct freq_attr *sfi_cpufreq_attr[] = {
+       &cpufreq_freq_attr_scaling_available_freqs,
+       NULL,
+};

Can reuse cpufreq_generic_attr ??

Quoted 7 lines
drivers/cpufreq/sfi-cpufreq.c new file +7 -0
+
+static struct cpufreq_driver sfi_cpufreq_driver = {
+       .get = get_cur_freq_on_cpu,
+       .verify = cpufreq_generic_frequency_table_verify,
+       .target_index = sfi_cpufreq_target,
+       .init = sfi_cpufreq_cpu_init,
+       .exit = sfi_cpufreq_cpu_exit,

drivers/cpufreq/sfi-cpufreq.c line 402

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+       .resume = sfi_cpufreq_resume,
+       .name = "sfi-cpufreq",
+       .attr = sfi_cpufreq_attr,
+};
+
+static int __init sfi_cpufreq_init(void)
+{
+       sfi_perf_data = alloc_percpu(struct sfi_processor_performance);
+       if (!sfi_perf_data) {
+               pr_debug("Memory allocation error for sfi_perf_data.\n");

Shouldn't this be pr_err ??

drivers/cpufreq/sfi-cpufreq.c line 414

Quoted 12 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+               return -ENOMEM;
+       }
+
+       return cpufreq_register_driver(&sfi_cpufreq_driver);
+}
+
+static void __exit sfi_cpufreq_exit(void)
+{
+       struct sfi_processor *pr;
+
+       pr = per_cpu(sfi_processors, 0);
+       kfree(pr);

What about: kfree(per_cpu(sfi_processors, 0)); instead of above 4 lines. Also why here when its already done in sfi_cpufreq_cpu_exit ?

drivers/cpufreq/sfi-cpufreq.c line 421

Quoted 7 lines
drivers/cpufreq/sfi-cpufreq.c new file +7 -0
+
+       cpufreq_unregister_driver(&sfi_cpufreq_driver);
+
+       free_percpu(sfi_perf_data);
+}
+late_initcall(sfi_cpufreq_init);
+module_exit(sfi_cpufreq_exit);

Normally we add them just below the respective routines.

drivers/cpufreq/sfi-cpufreq.c line 424

Quoted 3 lines
drivers/cpufreq/sfi-cpufreq.c new file +3 -0
+
+MODULE_ALIAS("sfi");
+MODULE_AUTHOR("Vishwesh Rudramuni");

Can add email address as well..

Quoted 63 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
+MODULE_DESCRIPTION("SFI Processor P-States Driver");
+MODULE_LICENSE("GPL");
drivers/cpufreq/sfi-cpufreq.h new file +53 -1
new file mode 100644
index 000000000000..b24f958cdaa8
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2010, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef __SFI_PROCESSOR_H__
+#define __SFI_PROCESSOR_H__
+
+#include <linux/sfi.h>
+#include <linux/cpuidle.h>
+
+struct sfi_processor_power {
+       struct cpuidle_device dev;
+       u32 default_state;
+       int count;
+       struct cpuidle_state *states;
+       struct sfi_cstate_table_entry *sfi_cstates;
+};
+
+struct sfi_processor_flags {
+       u8 valid;
+       u8 power;
+};
+
+struct sfi_processor {
+       u32 id;
+       struct sfi_processor_flags flags;
+       struct sfi_processor_power power;
+       struct sfi_processor_performance *performance;
+};
+
+/* Performance management */
+struct sfi_processor_px {
+       u32 core_frequency;     /* megahertz */
+       u32 transition_latency; /* microseconds */
+       u32 control;    /* control value */
+};
+
+struct sfi_processor_performance {
+       unsigned int state;
+       unsigned int state_count;
+       struct sfi_processor_px *states;
+};
+
+#endif /*__SFI_PROCESSOR_H__*/
--

1.7.9.5

#2 lore raw

The author is on vacation, let me take some of your comments as i'm in the delivery path..

Quoted 9 lines · Viresh Kumar

Adding Dirk as well..

Quoted 4 lines · Srinidhi

From b3037348db47f0629316dd0027c7f1a1b28be959 Mon Sep 17 00:00:00 2001
From: Srinidhi Kasagar <[email protected]>
Date: Fri, 12 Sep 2014 22:52:37 +0530
Subject: [PATCH] cpufreq: Add sfi based cpufreq driver support

You didn't use git send-email ?

No, looks like it is broken. Will fix it anyway.

Quoted 24 lines
Quoted 24 lines

This adds the sfi based cpu freq driver for some of the Intel's Silver Mont based atom architectures like Merrifield and Moorfield (intel-mid)

---
 drivers/cpufreq/Kconfig.x86   |   10 +
 drivers/cpufreq/Makefile      |    1 +
 drivers/cpufreq/sfi-cpufreq.c |  426 +++++++++++++++++++++++++++++++++++++++++
 drivers/cpufreq/sfi-cpufreq.h |   53 +++++
 4 files changed, 490 insertions(+)
 create mode 100644 drivers/cpufreq/sfi-cpufreq.c
 create mode 100644 drivers/cpufreq/sfi-cpufreq.h
drivers/cpufreq/Kconfig.x86 +0 -0
index 89ae88f91895..3d6886b558fa 100644
@@ -57,6 +57,16 @@ config X86_ACPI_CPUFREQ_CPB
          By enabling this option the acpi_cpufreq driver provides the old
          entry in addition to the new boost ones, for compatibility reasons.
 
Quoted 7 lines

drivers/cpufreq/Kconfig.x86 config X86_ACPI_CPUFREQ_CPB · line 64

Quoted 5 lines
drivers/cpufreq/Kconfig.x86 +5 -0
+config X86_SFI_CPUFREQ
+       tristate "SFI Processor P-States driver"
+       depends on X86_INTEL_MID && SFI
+       help
+         This driver adds a CPUFreq driver which utilizes the SFI

s/This driver/This/

Thanks, will fix.

Quoted 5 lines

drivers/cpufreq/Kconfig.x86 config X86_ACPI_CPUFREQ_CPB · line 66

Quoted 3 lines
drivers/cpufreq/Kconfig.x86 +2 -0
+         Processor Performance States enumeration on some Silver Mont
+         based Intel Atom architecture (like intel-mid)

Looks like the sentence isn't complete here, would you like to?

This is good enough..let me rephrase it.

Quoted 17 lines
Quoted 17 lines
drivers/cpufreq/Kconfig.x86 +3 -0
+
+         If in doubt, say N.
+
 config ELAN_CPUFREQ
        tristate "AMD Elan SC400 and SC410"
        depends on MELAN
drivers/cpufreq/Makefile +1 -0
index db6d9a2fea4d..c3b51efd4a85 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_X86_P4_CLOCKMOD)         += p4-clockmod.o
 obj-$(CONFIG_X86_CPUFREQ_NFORCE2)      += cpufreq-nforce2.o
 obj-$(CONFIG_X86_INTEL_PSTATE)         += intel_pstate.o
 obj-$(CONFIG_X86_AMD_FREQ_SENSITIVITY) += amd_freq_sensitivity.o
+obj-$(CONFIG_X86_SFI_CPUFREQ)          += sfi-cpufreq.o
 
Quoted 12 lines

drivers/cpufreq/sfi-cpufreq.c line 2

Quoted 10 lines
drivers/cpufreq/Makefile +0 -0
 ##################################################################################
 # ARM SoC drivers
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
new file mode 100644
index 000000000000..1400e0387528
@@ -0,0 +1,426 @@
+/*
+ *  sfi_cpufreq.c - sfi Processor P-States Driver

No need of mentioning file name here.

Thanks for spotting..these are all left over stuffs from the original code. Let me clean up further..

Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 4

Quoted 3 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
+ *  Based on ACPI Processor P-States Driver
+ *

How much different is it from that ? Sorry I haven't checked :(

I mean, will it be possible to make changes to acpi-cpufreq driver or get the common part out?

I dont think so. That is ACPI, this one is SFI. Both are architecturally not aligned.

Quoted 14 lines
Quoted 14 lines
drivers/cpufreq/sfi-cpufreq.c new file +13 -0
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  Author: Vishwesh M Rudramuni <[email protected]>
+ */
+
+#include <linux/kernel.h>
Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 23

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/smp.h>
+#include <linux/cpufreq.h>
+#include <linux/slab.h>
+#include <linux/sfi.h>

Keep them in ascending order please.

Hm..Will fix.

Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 28

Quoted 5 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+#include <asm/msr.h>
+#include <asm/processor.h>
+
+#include "sfi-cpufreq.h"

Do you plan to share the definitions here with any other file? If no, please merge the .h file here only.

No, will fix it.

Quoted 7 lines

drivers/cpufreq/sfi-cpufreq.c line 33

Quoted 5 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+#define SFI_FREQ_MAX           32
+#define INTEL_MSR_RANGE                0xffff
+#define INTEL_MSR_BUSRATIO_MASK        0xff00
+#define SFI_CPU_MAX            8

Aligning the values in a single column is preferred for readability.

I did, not sure how it got realigned. Let me take a look.

[...]

Quoted 11 lines

drivers/cpufreq/sfi-cpufreq.c line 219

Quoted 9 lines
drivers/cpufreq/sfi-cpufreq.c new file +9 -0
+}
+
+static int sfi_cpufreq_target(struct cpufreq_policy *policy,
+                               unsigned int index)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+       struct sfi_processor_performance *perf;
+       unsigned int next_perf_state = 0; /* Index into perf table */
+       u32 lo, hi;

You meant unsigned int only?

Yes.

Quoted 10 lines

drivers/cpufreq/sfi-cpufreq.c line 224

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +5 -0
+
+       if (unlikely(data == NULL ||
+            data->sfi_data == NULL || data->freq_table == NULL)) {
+               return -ENODEV;
+       }

Probably this looks better:

       if (unlikely(!data || !data->sfi_data || !data->freq_table))
               return -ENODEV;

Why not?

Quoted 3 lines

But do we need this check at all ? Also same comments for similar usage in above routines.

Not really..Will remove these stuffs.

Quoted 7 lines

drivers/cpufreq/sfi-cpufreq.c line 228

Quoted 5 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+
+       perf = data->sfi_data;
+       next_perf_state = data->freq_table[index].driver_data;
+       if (perf->state == next_perf_state) {

Can this happen? Probably cpufreq core will never do it ?

I think it did. Let me keep this.

Quoted 23 lines
Quoted 23 lines
drivers/cpufreq/sfi-cpufreq.c new file +22 -0
+               if (unlikely(data->resume)) {
+                       pr_debug("Called after resume, resetting to P%d\n",
+                               next_perf_state);
+                       data->resume = 0;
+               } else {
+                       pr_debug("Already at target state (P%d)\n",
+                               next_perf_state);
+                       return 0;
+               }
+       }
+
+       rdmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, &lo, &hi);
+       lo = (lo & ~INTEL_MSR_RANGE) |
+               ((u32) perf->states[next_perf_state].control & INTEL_MSR_RANGE);
+       wrmsr_on_cpu(policy->cpu, MSR_IA32_PERF_CTL, lo, hi);
+
+       perf->state = next_perf_state;
+
+       return 0;
+}
+
+static int sfi_cpufreq_cpu_init(struct cpufreq_policy *policy)
Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 256

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+{
+       unsigned int i;
+       unsigned int valid_states = 0;
+       unsigned int cpu = policy->cpu;
+       struct sfi_cpufreq_data *data;
+       unsigned int result = 0;

Can merge all definitions of similar data type in a single line.

Why not?

Quoted 7 lines

drivers/cpufreq/sfi-cpufreq.c line 260

Quoted 5 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+       struct sfi_processor_performance *perf;
+       struct sfi_processor *pr;
+
+       pr = kzalloc(sizeof(struct sfi_processor), GFP_KERNEL);

sizeof(*pr)

Style issues. Will fix.

Quoted 10 lines

drivers/cpufreq/sfi-cpufreq.c line 266

Quoted 7 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+       if (!pr)
+               return -ENOMEM;
+
+       per_cpu(sfi_processors, cpu) = pr;
+
+       pr_debug("sfi_cpufreq_cpu_init CPU:%d\n", policy->cpu);

I would prefer
pr_debug("%s: CPU:%d\n", __func__, policy->cpu);

Me too. Will fix.

Quoted 5 lines

drivers/cpufreq/sfi-cpufreq.c line 268

Quoted 3 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
+
+       data = kzalloc(sizeof(struct sfi_cpufreq_data), GFP_KERNEL);

sizeof(*data)

ditto..

Quoted 5 lines

drivers/cpufreq/sfi-cpufreq.c line 270

Quoted 3 lines
drivers/cpufreq/sfi-cpufreq.c new file +2 -0
+       if (!data)
+               return -ENOMEM;

What about freeing pr ?

My bad. Will fix.

Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 275

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +5 -0
+
+       data->sfi_data = per_cpu_ptr(sfi_perf_data, cpu);
+       per_cpu(drv_data, cpu) = data;
+
+       sfi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;

Why here? and not in the structure definition itself..

Hmm..another legacy style. Will add it in the definition rather..Thanks.

Quoted 14 lines

drivers/cpufreq/sfi-cpufreq.c line 285

Quoted 11 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+
+       result = sfi_processor_register_performance(data->sfi_data, cpu);
+       if (result)
+               goto err_free;
+
+       perf = data->sfi_data;
+       policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
+
+       cpumask_set_cpu(policy->cpu, policy->cpus);
+       cpumask_set_cpu(policy->cpu, policy->related_cpus);

You don't have to set related_cpus, its done by core. Also policy->cpus is already set by core to policy->cpu.

Yes, Will fix.

Quoted 12 lines

drivers/cpufreq/sfi-cpufreq.c line 294

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +9 -0
+
+       /* capability check */
+       if (perf->state_count <= 1) {
+               pr_debug("No P-States\n");
+               result = -ENODEV;
+               goto err_unreg;
+       }
+
+       data->freq_table = kzalloc(sizeof(struct cpufreq_frequency_table) *

sizeof (*data->freq_table)

ok..

Quoted 2 lines

Also, you want to do kzalloc? or kmalloc will work as well ?

kmalloc makes my table corrupted, perhaps the addition of new flag variable. Let me keep kzalloc.

Quoted 15 lines
Quoted 15 lines
drivers/cpufreq/sfi-cpufreq.c new file +14 -0
+                   (perf->state_count+1), GFP_KERNEL);
+       if (!data->freq_table) {
+               result = -ENOMEM;
+               goto err_unreg;
+       }
+
+       /* detect transition latency */
+       policy->cpuinfo.transition_latency = 0;
+       for (i = 0; i < perf->state_count; i++) {
+               if ((perf->states[i].transition_latency * 1000) >
+                   policy->cpuinfo.transition_latency)
+                       policy->cpuinfo.transition_latency =
+                           perf->states[i].transition_latency * 1000;
+       }
Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 314

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+
+       /* initialize the freq table */
+       for (i = 0; i < perf->state_count; i++) {
+               if (i > 0 && perf->states[i].core_frequency >=
+                   data->freq_table[valid_states-1].frequency / 1000)
+                       continue;

What are you doing here ?

This is unncessary check, not needed. Again a left over stuff. Will clean it.

Quoted 17 lines
Quoted 5 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+
+               data->freq_table[valid_states].driver_data = i;
+               data->freq_table[valid_states].frequency =
+                   perf->states[i].core_frequency * 1000;

drivers/cpufreq/sfi-cpufreq.c line 328

Quoted 10 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+               valid_states++;
+       }
+       data->freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
+       perf->state = 0;
+
+       result = cpufreq_table_validate_and_show(policy, data->freq_table);
+       if (result)
+               goto err_freqfree;
+
+       policy->cur = get_cur_freq_on_cpu(cpu);

This is done by core and so you need to do it.

Agree. Will remove.

Quoted 9 lines

drivers/cpufreq/sfi-cpufreq.c line 335

Quoted 7 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+       pr_debug("CPU%u - SFI performance management activated.\n", cpu);
+       for (i = 0; i < perf->state_count; i++)
+               pr_debug("     %cP%d: %d MHz, %d uS\n",
+                       (i == perf->state ? '*' : ' '), i,
+                       (u32) perf->states[i].core_frequency,
+                       (u32) perf->states[i].transition_latency);

What about printing this in the above for loop only?

Why not?

Quoted 15 lines

drivers/cpufreq/sfi-cpufreq.c line 347

Quoted 13 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+
+       data->resume = 1;
+
+       return result;
+
+err_freqfree:
+       kfree(data->freq_table);
+err_unreg:
+       sfi_processor_unregister_performance(perf, cpu);
+err_free:
+       kfree(data);
+       per_cpu(drv_data, cpu) = NULL;

Free pr ?

My bad. Wilf fix

Quoted 15 lines

drivers/cpufreq/sfi-cpufreq.c line 359

Quoted 13 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+
+       return result;
+}
+
+static int sfi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+       struct sfi_processor *pr = per_cpu(sfi_processors, policy->cpu);
+
+       pr_debug("sfi_cpufreq_cpu_exit\n");
+
+       if (data) {

Why will data be NULL here ?

Perhaps to satisfy the static code check tools. Will remove.

Quoted 11 lines
Quoted 11 lines
drivers/cpufreq/sfi-cpufreq.c new file +10 -0
+               per_cpu(drv_data, policy->cpu) = NULL;
+               sfi_processor_unregister_performance(data->sfi_data,
+                                               policy->cpu);
+               kfree(data->freq_table);
+               kfree(data);
+               kfree(pr);
+       }
+
+       return 0;
+}
Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 373

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +4 -0
+
+static int sfi_cpufreq_resume(struct cpufreq_policy *policy)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);

Your code is dependent on policy->cpu at multiple places and it can change on CPU hotplug. You should worry about it only if there can be more than one CPU in a single policy, which doesn't look like the case.

Not sure what do you mean by not using policy->cpu. I need all of these to support hotplug. Can you elaborate if you mean otherwise please?

Quoted 7 lines

Also there is a patch from Thomas Petazzoni
[PATCHv2 1/4] cpufreq: allow driver-specific data
https://www.mail-archive.com/[email protected]/msg10696.html

you can use that for data.

Quoted 14 lines

drivers/cpufreq/sfi-cpufreq.c line 385

Quoted 12 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+
+       pr_debug("sfi_cpufreq_resume\n");
+
+       data->resume = 1;
+
+       return 0;
+}
+
+static struct freq_attr *sfi_cpufreq_attr[] = {
+       &cpufreq_freq_attr_scaling_available_freqs,
+       NULL,
+};

Can reuse cpufreq_generic_attr ??

We should be using that. Will fix.

Quoted 12 lines
Quoted 12 lines
drivers/cpufreq/sfi-cpufreq.c new file +11 -0
+
+static struct cpufreq_driver sfi_cpufreq_driver = {
+       .get = get_cur_freq_on_cpu,
+       .verify = cpufreq_generic_frequency_table_verify,
+       .target_index = sfi_cpufreq_target,
+       .init = sfi_cpufreq_cpu_init,
+       .exit = sfi_cpufreq_cpu_exit,
+       .resume = sfi_cpufreq_resume,
+       .name = "sfi-cpufreq",
+       .attr = sfi_cpufreq_attr,
+};
Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 402

Quoted 6 lines
drivers/cpufreq/sfi-cpufreq.c new file +6 -0
+
+static int __init sfi_cpufreq_init(void)
+{
+       sfi_perf_data = alloc_percpu(struct sfi_processor_performance);
+       if (!sfi_perf_data) {
+               pr_debug("Memory allocation error for sfi_perf_data.\n");

Shouldn't this be pr_err ??

Yes. Will fix.

Quoted 16 lines

drivers/cpufreq/sfi-cpufreq.c line 414

Quoted 13 lines
drivers/cpufreq/sfi-cpufreq.c new file +12 -0
+               return -ENOMEM;
+       }
+
+       return cpufreq_register_driver(&sfi_cpufreq_driver);
+}
+
+static void __exit sfi_cpufreq_exit(void)
+{
+       struct sfi_processor *pr;
+
+       pr = per_cpu(sfi_processors, 0);
+       kfree(pr);

What about: kfree(per_cpu(sfi_processors, 0)); instead of above 4 lines. Also why here when its already done in sfi_cpufreq_cpu_exit ?

I think it is not required. Will remove them.

Quoted 10 lines

drivers/cpufreq/sfi-cpufreq.c line 421

Quoted 8 lines
drivers/cpufreq/sfi-cpufreq.c new file +7 -0
+
+       cpufreq_unregister_driver(&sfi_cpufreq_driver);
+
+       free_percpu(sfi_perf_data);
+}
+late_initcall(sfi_cpufreq_init);
+module_exit(sfi_cpufreq_exit);

Normally we add them just below the respective routines.

Some prefer like this :)

Quoted 6 lines

drivers/cpufreq/sfi-cpufreq.c line 424

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +3 -0
+
+MODULE_ALIAS("sfi");
+MODULE_AUTHOR("Vishwesh Rudramuni");

Can add email address as well..

Yes..Will add.

Srinidhi

maintainer
#3 lore raw
Quoted 11 lines · Srinidhi
Quoted 9 lines · Viresh Kumar

drivers/cpufreq/sfi-cpufreq.c line 219

Quoted 7 lines
drivers/cpufreq/sfi-cpufreq.c new file +7 -0
+static int sfi_cpufreq_target(struct cpufreq_policy *policy,
+                               unsigned int index)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+       struct sfi_processor_performance *perf;
+       unsigned int next_perf_state = 0; /* Index into perf table */
+       u32 lo, hi;

You meant unsigned int only?

Quoted 1 line

Yes.

Then use that only to be aligned with other data types.

Quoted 7 lines
Quoted 5 lines

drivers/cpufreq/sfi-cpufreq.c line 228

Quoted 3 lines
drivers/cpufreq/sfi-cpufreq.c new file +3 -0
+       perf = data->sfi_data;
+       next_perf_state = data->freq_table[index].driver_data;
+       if (perf->state == next_perf_state) {

Can this happen? Probably cpufreq core will never do it ?

I think it did. Let me keep this.

Okay, but how? The cpufreq core doesn't propagate call to ->target_index() if the freq isn't changing.. Please check if this is getting hit at all or not.

Quoted 10 lines
Quoted 7 lines

drivers/cpufreq/sfi-cpufreq.c line 373

Quoted 3 lines
drivers/cpufreq/sfi-cpufreq.c new file +3 -0
+static int sfi_cpufreq_resume(struct cpufreq_policy *policy)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);

Your code is dependent on policy->cpu at multiple places and it can change on CPU hotplug. You should worry about it only if there can be more than one CPU in a single policy, which doesn't look like the case.

Not sure what do you mean by not using policy->cpu. I need all of these to support hotplug. Can you elaborate if you mean otherwise please?

So you are using policy->cpu to set/get a value out of a per-cpu variable. Now if a policy has 4 cpus then per-cpu variable will only be initialized for policy->cpu and not others.

But now if we do hotplug of that cpu, policy->cpu will move to next cpu. In this case accessing the per-cpu variable will not be a good idea :)

#4 lore raw
Quoted 12 lines · Viresh Kumar
Quoted 11 lines · Srinidhi
Quoted 9 lines · Viresh Kumar

drivers/cpufreq/sfi-cpufreq.c line 219

Quoted 7 lines
drivers/cpufreq/sfi-cpufreq.c new file +7 -0
+static int sfi_cpufreq_target(struct cpufreq_policy *policy,
+                               unsigned int index)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);
+       struct sfi_processor_performance *perf;
+       unsigned int next_perf_state = 0; /* Index into perf table */
+       u32 lo, hi;

You meant unsigned int only?

Quoted 3 lines
Quoted 1 line

Yes.

Then use that only to be aligned with other data types.

Sorry, I meant it should be u32 as they are register reads.

Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

drivers/cpufreq/sfi-cpufreq.c line 228

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +3 -0
+       perf = data->sfi_data;
+       next_perf_state = data->freq_table[index].driver_data;
+       if (perf->state == next_perf_state) {

Can this happen? Probably cpufreq core will never do it ?

I think it did. Let me keep this.

Okay, but how? The cpufreq core doesn't propagate call to ->target_index() if the freq isn't changing.. Please check if this is getting hit at all or not.

I guess if you offline a cpu and then bring it online, then even though there is no change in frequency, the core calls ->target_index().

am i missing something?

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

drivers/cpufreq/sfi-cpufreq.c line 373

Quoted 4 lines
drivers/cpufreq/sfi-cpufreq.c new file +3 -0
+static int sfi_cpufreq_resume(struct cpufreq_policy *policy)
+{
+       struct sfi_cpufreq_data *data = per_cpu(drv_data, policy->cpu);

Your code is dependent on policy->cpu at multiple places and it can change on CPU hotplug. You should worry about it only if there can be more than one CPU in a single policy, which doesn't look like the case.

Not sure what do you mean by not using policy->cpu. I need all of these to support hotplug. Can you elaborate if you mean otherwise please?

Quoted 6 lines

So you are using policy->cpu to set/get a value out of a per-cpu variable. Now if a policy has 4 cpus then per-cpu variable will only be initialized for policy->cpu and not others.

But now if we do hotplug of that cpu, policy->cpu will move to next cpu. In this case accessing the per-cpu variable will not be a good idea :)

Not sure what I can do with this. Let me have a look.

Srinidhi

maintainer
#5 lore raw
Quoted 2 lines · Srinidhi

I guess if you offline a cpu and then bring it online, then even though there is no change in frequency, the core calls ->target_index().

No.

Quoted 1 line

am i missing something?

Yes.

#6 lore raw
Quoted 5 lines · Viresh Kumar
Quoted 2 lines · Srinidhi

I guess if you offline a cpu and then bring it online, then even though there is no change in frequency, the core calls ->target_index().

No.

Hmm..I noticed later that I was doing cpumask_copy for my internal experiments. Even in that case, as you pointed, the ->target_index() was not called for the onlined CPU, but for the other related CPU. I should have debug printed policy->cpu in my ->target() implementation..:(

Anyway, thanks much for it. Will fix that useless check in the next version of my patch.

Srinidhi

maintainer
#7 lore raw
Quoted 3 lines · Srinidhi

Hmm..I noticed later that I was doing cpumask_copy for my internal experiments. Even in that case, as you pointed, the ->target_index() was not called for the onlined CPU, but for the other related CPU.

But why is it getting called for even that ?

#8 lore raw
Quoted 6 lines · Viresh Kumar
Quoted 3 lines · Srinidhi

Hmm..I noticed later that I was doing cpumask_copy for my internal experiments. Even in that case, as you pointed, the ->target_index() was not called for the onlined CPU, but for the other related CPU.

But why is it getting called for even that ?

I have not debugged that. However, to double check the same I have rebased to the clean upstream tree and I don't see ->target_index() is called for any other CPUs during the offline-online path..

Srinidhi

maintainer
#9 lore raw
Quoted 3 lines · Srinidhi

I have not debugged that. However, to double check the same I have rebased to the clean upstream tree and I don't see ->target_index() is called for any other CPUs during the offline-online path..

OKay, this is getting a bit confusing.

->target_index() is called for which CPU?

#10 lore raw
Quoted 8 lines · Viresh Kumar
Quoted 3 lines · Srinidhi

I have not debugged that. However, to double check the same I have rebased to the clean upstream tree and I don't see ->target_index() is called for any other CPUs during the offline-online path..

OKay, this is getting a bit confusing.

->target_index() is called for which CPU?

It was called for the sibling cpu of the onlined one.

Srinidhi

maintainer
#11 lore raw
Quoted 1 line · Srinidhi

It was called for the sibling cpu of the onlined one.

Still not clear. Can you give me output of this:

cat /sys/devices/system/cpu/cpu*/cpufreq/affected_cpus and cat /sys/devices/system/cpu/cpu*/cpufreq/related_cpus

And then also tell me which CPU are you trying to remove ?

#12 lore raw
Quoted 7 lines · Viresh Kumar
Quoted 1 line · Srinidhi

It was called for the sibling cpu of the onlined one.

Still not clear. Can you give me output of this:

cat /sys/devices/system/cpu/cpu*/cpufreq/affected_cpus and cat /sys/devices/system/cpu/cpu*/cpufreq/related_cpus

# cat /sys/devices/system/cpu/cpu*/cpufreq/affected_cpus      
0 1
0 1
2 3
2 3
# cat /sys/devices/system/cpu/cpu*/cpufreq/related_cpus
0 1
0 1
2 3
2 3
Quoted 2 lines

And then also tell me which CPU are you trying to remove ?

cpu1

BTW, I had to switch to my own tree to get the issue back (where I have my legacy custom cpufreq/core changes. So I suspect my changes not the upstream tree)

Srinidhi

maintainer
#13 lore raw
Quoted 13 lines · Srinidhi
# cat /sys/devices/system/cpu/cpu*/cpufreq/affected_cpus
0 1
0 1
2 3
2 3
# cat /sys/devices/system/cpu/cpu*/cpufreq/related_cpus
0 1
0 1
2 3
2 3
Quoted 1 line
Quoted 3 lines
Quoted 1 line

And then also tell me which CPU are you trying to remove ?

cpu1

So when to remove CPU1, you get a call for freq change for CPU0. Right ?

There are few things worth noticing here:
- Freq change is only requested for policy->cpu. In your case there will be two policies present in system. For the first one policy->cpu will be 0, for the second
one 2.
- So target_index() will always be called for 0 and 2. Unless policy->cpu is changed
by hotplugging out policy->cpu, i.e. 0 or 2.
- This whole thread started because you said target_index() is called for policy->cur
frequency. So even if ->target_index() is called (which might happen due to governor
requesting a new freq), it shouldn't be called for the same freq.

Quoted 3 lines

BTW, I had to switch to my own tree to get the issue back (where I have my legacy custom cpufreq/core changes. So I suspect my changes not the upstream tree)

That's another side of the story :)