Skip to contentSkip to conversation

powerpc: add support for new hcall H_BEST_ENERGY

RFC Superseded Vaidyanathan Srinivasan 3 participants 8 messages Mar 3, 2010 – Apr 7, 2010 PowerPC

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 powerpc: add support for new hcall H_BEST_ENERGY

Hi,

A new hypervisor call H_BEST_ENERGY is supported in IBM pseries ppc64 platform in order to exports platform energy management capabilities to user space administrative tools.

This hypervisor calls provides hints or suggested list of cpus to activate or deactivate for optimized energy management. The following patch add a new pseries specific device driver that will export such platform energy management related information over sysfs interface for use by user space administrative tools.

The device driver exports the following sysfs files:

/sys/device/system/cpu/pseries_(de)activate_hint_list and /sys/device/system/cpu/cpuN/pseries_(de)activate_hint.

Typical usage:

# cat /sys/devices/system/cpu/pseries_activate_hint_list
16,20,24
# cat /sys/devices/system/cpu/pseries_deactivate_hint_list
0,4,8,12

Comma separated list of recommended cpu numbers to activate or deactivate.

cat /sys/device/system/cpu/cpuN/pseries_(de)activate_hint will provide a single integer value returned by the platform for that cpu.

Please let me know your comments and suggestions.

Thanks,
Vaidy
---
powerpc: add support for new hcall H_BEST_ENERGY

Create sysfs interface to export data from H_BEST_ENERGY hcall that can be used by administrative tools on supported pseries
platforms for energy management optimizations.

/sys/device/system/cpu/pseries_(de)activate_hint_list and /sys/device/system/cpu/cpuN/pseries_(de)activate_hint will provide hints for activation and deactivation of cpus respectively.

Added new driver module
arch/powerpc/platforms/pseries/pseries_energy.c
under new config option CONFIG_PSERIES_ENERGY

---
 arch/powerpc/include/asm/hvcall.h               |    3 
 arch/powerpc/kernel/setup-common.c              |    2 
 arch/powerpc/platforms/pseries/Kconfig          |   10 +
 arch/powerpc/platforms/pseries/Makefile         |    1 
 arch/powerpc/platforms/pseries/pseries_energy.c |  246 +++++++++++++++++++++++
 5 files changed, 261 insertions(+), 1 deletions(-)
 create mode 100644 arch/powerpc/platforms/pseries/pseries_energy.c
arch/powerpc/include/asm/hvcall.h +2 -1
index f027581..396599f 100644
@@ -230,7 +230,8 @@
 #define H_ENABLE_CRQ		0x2B0
 #define H_SET_MPP		0x2D0
 #define H_GET_MPP		0x2D4
-#define MAX_HCALL_OPCODE	H_GET_MPP
+#define H_BEST_ENERGY		0x2F4
+#define MAX_HCALL_OPCODE	H_BEST_ENERGY
 
 #ifndef __ASSEMBLY__
 
arch/powerpc/kernel/setup-common.c +2 -0
index 03dd6a2..fbd93e3 100644
@@ -359,6 +359,8 @@ void __init check_for_initrd(void)
 #ifdef CONFIG_SMP
 
 int threads_per_core, threads_shift;
+EXPORT_SYMBOL_GPL(threads_per_core);
+
 cpumask_t threads_core_mask;
 
 static void __init cpu_init_thread_core_maps(int tpc)
arch/powerpc/platforms/pseries/Kconfig +10 -0
index c667f0f..b3dd108 100644
@@ -33,6 +33,16 @@ config PSERIES_MSI
        depends on PCI_MSI && EEH
        default y
 
+config PSERIES_ENERGY
+	tristate "pseries energy management capabilities driver"
+	depends on PPC_PSERIES
+	default y
+	help
+	  Provides interface to platform energy management capabilities
+	  on supported PSERIES platforms.
+	  Provides: /sys/devices/system/cpu/pseries_(de)activation_hint_list
+	  and /sys/devices/system/cpu/cpuN/pseries_(de)activation_hint
+
 config SCANLOG
 	tristate "Scanlog dump interface"
 	depends on RTAS_PROC && PPC_PSERIES
arch/powerpc/platforms/pseries/Makefile +1 -0
index 0ff5174..3813977 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_EEH)	+= eeh.o eeh_cache.o eeh_driver.o eeh_event.o eeh_sysfs.o
 obj-$(CONFIG_KEXEC)	+= kexec.o
 obj-$(CONFIG_PCI)	+= pci.o pci_dlpar.o
 obj-$(CONFIG_PSERIES_MSI)	+= msi.o
+obj-$(CONFIG_PSERIES_ENERGY)	+= pseries_energy.o
 
 obj-$(CONFIG_HOTPLUG_CPU)	+= hotplug-cpu.o
 obj-$(CONFIG_MEMORY_HOTPLUG)	+= hotplug-memory.o
arch/powerpc/platforms/pseries/pseries_energy.c new file +246 -0

246 lines not on this page · raw message

#1 lore raw
Quoted 22 lines · Vaidyanathan Srinivasan

static void __init cpu_init_thread_core_maps(int tpc)

arch/powerpc/platforms/pseries/Kconfig +10 -0
index c667f0f..b3dd108 100644
@@ -33,6 +33,16 @@ config PSERIES_MSI
        depends on PCI_MSI && EEH
        default y
 
+config PSERIES_ENERGY
+	tristate "pseries energy management capabilities driver"
+	depends on PPC_PSERIES
+	default y
+	help
+	  Provides interface to platform energy management capabilities
+	  on supported PSERIES platforms.
+	  Provides: /sys/devices/system/cpu/pseries_(de)activation_hint_list
+	  and /sys/devices/system/cpu/cpuN/pseries_(de)activation_hint
+
 config SCANLOG
 	tristate "Scanlog dump interface"
 	depends on RTAS_PROC && PPC_PSERIES

.....

Quoted 16 lines
arch/powerpc/platforms/pseries/Kconfig +16 -0
+static int __init pseries_energy_init(void)
+{
+	int cpu, err;
+	struct sys_device *cpu_sys_dev;
+
+	/* Create the sysfs files */
+	err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_activate_hint_list.attr);
+	if (!err)
+		err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_deactivate_hint_list.attr);
+
+	for_each_possible_cpu(cpu) {
+		cpu_sys_dev = get_cpu_sysdev(cpu);
+		err = sysfs_create_file(&cpu_sys_dev->kobj,
+				&attr_percpu_activate_hint.attr);

arch/powerpc/platforms/pseries/Kconfig

Quoted 10 lines
arch/powerpc/platforms/pseries/Kconfig +10 -0
+		if (err)
+			break;
+		err = sysfs_create_file(&cpu_sys_dev->kobj,
+				&attr_percpu_deactivate_hint.attr);
+		if (err)
+			break;
+	}
+	return err;
+
+}

Shouldn't we create this only for supported platforms ?

Thanks
Dipankar

#2 lore raw
Quoted 46 lines · Dipankar Sarma
Quoted 22 lines · Vaidyanathan Srinivasan

static void __init cpu_init_thread_core_maps(int tpc)

arch/powerpc/platforms/pseries/Kconfig +10 -0
index c667f0f..b3dd108 100644
@@ -33,6 +33,16 @@ config PSERIES_MSI
        depends on PCI_MSI && EEH
        default y
 
+config PSERIES_ENERGY
+	tristate "pseries energy management capabilities driver"
+	depends on PPC_PSERIES
+	default y
+	help
+	  Provides interface to platform energy management capabilities
+	  on supported PSERIES platforms.
+	  Provides: /sys/devices/system/cpu/pseries_(de)activation_hint_list
+	  and /sys/devices/system/cpu/cpuN/pseries_(de)activation_hint
+
 config SCANLOG
 	tristate "Scanlog dump interface"
 	depends on RTAS_PROC && PPC_PSERIES

.....

Quoted 20 lines
arch/powerpc/platforms/pseries/Kconfig +20 -0
+static int __init pseries_energy_init(void)
+{
+	int cpu, err;
+	struct sys_device *cpu_sys_dev;
+
+	/* Create the sysfs files */
+	err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_activate_hint_list.attr);
+	if (!err)
+		err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_deactivate_hint_list.attr);
+
+	for_each_possible_cpu(cpu) {
+		cpu_sys_dev = get_cpu_sysdev(cpu);
+		err = sysfs_create_file(&cpu_sys_dev->kobj,
+				&attr_percpu_activate_hint.attr);
+		if (err)
+			break;
+		err = sysfs_create_file(&cpu_sys_dev->kobj,
+				&attr_percpu_deactivate_hint.attr);
Quoted 8 lines

arch/powerpc/platforms/pseries/Kconfig

Quoted 6 lines
arch/powerpc/platforms/pseries/Kconfig +6 -0
+		if (err)
+			break;
+	}
+	return err;
+
+}

Shouldn't we create this only for supported platforms ?

Hi Dipankar,

Yes we will need a check like firmware_has_feature(FW_FEATURE_BEST_ENERGY) to avoid sysfs files in unsupported platforms. I will add that check in the next iteration.

Thanks,
Vaidy

#3 lore raw
Quoted 9 lines · Vaidyanathan Srinivasan
Quoted 1 line · Dipankar Sarma

Shouldn't we create this only for supported platforms ?

Hi Dipankar,

Yes we will need a check like firmware_has_feature(FW_FEATURE_BEST_ENERGY) to avoid sysfs files in unsupported platforms. I will add that check in the next iteration.

Also, given that this module isn't likely to provide anything on older platforms, it should get loaded only on newer platforms.

Thanks
Dipankar

#4 lore raw
Quoted 5 lines · Dipankar Sarma
Quoted 4 lines · Vaidyanathan Srinivasan
Quoted 1 line · Dipankar Sarma

Shouldn't we create this only for supported platforms ?

Quoted 8 lines
Quoted 5 lines

Hi Dipankar,

Yes we will need a check like firmware_has_feature(FW_FEATURE_BEST_ENERGY) to avoid sysfs files in unsupported platforms. I will add that check in the next iteration.

Also, given that this module isn't likely to provide anything on older platforms, it should get loaded only on newer platforms.

Yes, I will explore methods to achieve this. I could not yet find a trigger to load modules based on firmware feature.

--Vaidy

#5 lore raw
In reply to Vaidyanathan Srinivasan

arch/powerpc/kernel/setup-common.c void __init check_for_initrd(void) · line 362

Quoted 11 lines · Vaidyanathan Srinivasan

Hi,

arch/powerpc/kernel/setup-common.c +1 -0
index 03dd6a2..fbd93e3 100644
@@ -359,6 +359,8 @@ void __init check_for_initrd(void)
 #ifdef CONFIG_SMP
 
 int threads_per_core, threads_shift;
+EXPORT_SYMBOL_GPL(threads_per_core);

While I agree it should be exported for the APIs in cputhread.h to be usable from a module, this variable shouldn't be -used- directly, but only via the API functions in there.

.../...

arch/powerpc/kernel/setup-common.c void __init check_for_initrd(void)

Quoted 12 lines
arch/powerpc/kernel/setup-common.c +12 -0
+
+#define MODULE_VERS "1.0"
+#define MODULE_NAME "pseries_energy"
+
+/* Helper Routines to convert between drc_index to cpu numbers */
+
+static u32 cpu_to_drc_index(int cpu)
+{
+	struct device_node *dn = NULL;
+	const int *indexes;
+	int i;
+	dn = of_find_node_by_name(dn, "cpus");

Check the result. Also that's not a nice way to do that, you should look for /cpus by path I reckon.

arch/powerpc/kernel/setup-common.c

Quoted 1 line
arch/powerpc/kernel/setup-common.c +1 -0
+	indexes = of_get_property(dn, "ibm,drc-indexes", NULL);

Check the result here too.

arch/powerpc/kernel/setup-common.c

Quoted 2 lines
arch/powerpc/kernel/setup-common.c +2 -0
+	/* Convert logical cpu number to core number */
+	i = cpu / threads_per_core;

Don't use that variable as I said earlier. Use cpu_thread_to_core()

Quoted 6 lines
arch/powerpc/kernel/setup-common.c +6 -0
+	/*
+	 * The first element indexes[0] is the number of drc_indexes
+	 * returned in the list.  Hence i+1 will get the drc_index
+	 * corresponding to core number i.
+	 */
+	WARN_ON(i > indexes[0]);

arch/powerpc/kernel/setup-common.c

Quoted 10 lines
arch/powerpc/kernel/setup-common.c +10 -0
+	return indexes[i + 1];
+}
+
+static int drc_index_to_cpu(u32 drc_index)
+{
+	struct device_node *dn = NULL;
+	const int *indexes;
+	int i, cpu;
+	dn = of_find_node_by_name(dn, "cpus");
+	indexes = of_get_property(dn, "ibm,drc-indexes", NULL);

Same comments, check results and use /cpus path

arch/powerpc/kernel/setup-common.c

Quoted 11 lines
arch/powerpc/kernel/setup-common.c +11 -0
+	/*
+	 * First element in the array is the number of drc_indexes
+	 * returned.  Search through the list to find the matching
+	 * drc_index and get the core number
+	 */
+	for (i = 0; i < indexes[0]; i++) {
+		if (indexes[i + 1] == drc_index)
+			break;
+	}
+	/* Convert core number to logical cpu number */
+	cpu = i * threads_per_core;

Here's more annoying as we don't have an API in cputhread.h for that.

In fact, we have a confusion in there since cpu_first_thread_in_core() doesn't actually take a core number ...

I'm going to recommend a complicated approach but that's the best in the long run:

- First do a patch that renames cpu_first_thread_in_core() to something
clearer like cpu_first_thread_in_same_core() or cpu_leftmost_sibling() (I think I prefer the later). Rename the few users in arch/powerpc/mm/mmu_context_nohash.c and arch/powerpc/kernel/smp.c

- Then do a patch that adds a cpu_first_thread_of_core() that takes a
core number, basically does:

static inline int cpu_first_thread_of_core(int core)

{
	return core << threads_shift;
}

- Then add your modified H_BEST_ENERGY patch on top of it using the
above two as pre-reqs :-)

Quoted 13 lines
arch/powerpc/kernel/setup-common.c +13 -0
+	return cpu;
+}
+
+/*
+ * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
+ * preferred logical cpus to activate or deactivate for optimized
+ * energy consumption.
+ */
+
+#define FLAGS_MODE1	0x004E200000080E01
+#define FLAGS_MODE2	0x004E200000080401
+#define FLAGS_ACTIVATE  0x100
+

arch/powerpc/kernel/setup-common.c

Quoted 10 lines
arch/powerpc/kernel/setup-common.c +10 -0
+static ssize_t get_best_energy_list(char *page, int activate)
+{
+	int rc, cnt, i, cpu;
+	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
+	unsigned long flags = 0;
+	u32 *buf_page;
+	char *s = page;
+
+	buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
+

Why that blank line ?

arch/powerpc/kernel/setup-common.c

Quoted 2 lines
arch/powerpc/kernel/setup-common.c +2 -0
+	if (!buf_page)
+		return 0;

So here you return 0 instead of -ENOMEM

arch/powerpc/kernel/setup-common.c

Quoted 8 lines
arch/powerpc/kernel/setup-common.c +8 -0
+	flags = FLAGS_MODE1;
+	if (activate)
+		flags |= FLAGS_ACTIVATE;
+
+	rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
+				0, 0, 0, 0, 0, 0);
+
+	

Again, no need for a blank line before xx = foo() and if (xx)

arch/powerpc/kernel/setup-common.c

Quoted 4 lines

if (rc != H_SUCCESS) {

arch/powerpc/kernel/setup-common.c +3 -0
+		free_page((unsigned long) buf_page);
+		return -EINVAL;
+	}

And here you return an error code. Which one is right ?

Quoted 133 lines
arch/powerpc/kernel/setup-common.c +132 -0
+	cnt = retbuf[0];
+	for (i = 0; i < cnt; i++) {
+		cpu = drc_index_to_cpu(buf_page[2*i+1]);
+		if ((cpu_online(cpu) && !activate) ||
+		    (!cpu_online(cpu) && activate))
+			s += sprintf(s, "%d,", cpu);
+	}
+	if (s > page) { /* Something to show */
+		s--; /* Suppress last comma */
+		s += sprintf(s, "\n");
+	}
+
+	free_page((unsigned long) buf_page);
+	return s-page;
+}
+
+static ssize_t get_best_energy_data(struct sys_device *dev,
+					char *page, int activate)
+{
+	int rc;
+	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
+	unsigned long flags = 0;
+
+	flags = FLAGS_MODE2;
+	if (activate)
+		flags |= FLAGS_ACTIVATE;
+
+	rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags,
+				cpu_to_drc_index(dev->id),
+				0, 0, 0, 0, 0, 0, 0);
+
+	if (rc != H_SUCCESS)
+		return -EINVAL;
+
+	return sprintf(page, "%lu\n", retbuf[1] >> 32);
+}
+
+/* Wrapper functions */
+
+static ssize_t cpu_activate_hint_list_show(struct sysdev_class *class,
+						char *page)
+{
+	return get_best_energy_list(page, 1);
+}
+
+static ssize_t cpu_deactivate_hint_list_show(struct sysdev_class *class,
+						char *page)
+{
+	return get_best_energy_list(page, 0);
+}
+
+static ssize_t percpu_activate_hint_show(struct sys_device *dev,
+			struct sysdev_attribute *attr, char *page)
+{
+	return get_best_energy_data(dev, page, 1);
+}
+
+static ssize_t percpu_deactivate_hint_show(struct sys_device *dev,
+			struct sysdev_attribute *attr, char *page)
+{
+	return get_best_energy_data(dev, page, 0);
+}
+
+/*
+ * Create sysfs interface:
+ * /sys/devices/system/cpu/pseries_activate_hint_list
+ * /sys/devices/system/cpu/pseries_deactivate_hint_list
+ * 	Comma separated list of cpus to activate or deactivate
+ * /sys/devices/system/cpu/cpuN/pseries_activate_hint
+ * /sys/devices/system/cpu/cpuN/pseries_deactivate_hint
+ *	Per-cpu value of the hint
+ */
+
+struct sysdev_class_attribute attr_cpu_activate_hint_list =
+		_SYSDEV_CLASS_ATTR(pseries_activate_hint_list, 0444,
+		cpu_activate_hint_list_show, NULL);
+
+struct sysdev_class_attribute attr_cpu_deactivate_hint_list =
+		_SYSDEV_CLASS_ATTR(pseries_deactivate_hint_list, 0444,
+		cpu_deactivate_hint_list_show, NULL);
+
+struct sysdev_attribute attr_percpu_activate_hint =
+		_SYSDEV_ATTR(pseries_activate_hint, 0444,
+		percpu_activate_hint_show, NULL);
+
+struct sysdev_attribute attr_percpu_deactivate_hint =
+		_SYSDEV_ATTR(pseries_deactivate_hint, 0444,
+		percpu_deactivate_hint_show, NULL);
+
+static int __init pseries_energy_init(void)
+{
+	int cpu, err;
+	struct sys_device *cpu_sys_dev;
+
+	/* Create the sysfs files */
+	err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_activate_hint_list.attr);
+	if (!err)
+		err = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_deactivate_hint_list.attr);
+
+	for_each_possible_cpu(cpu) {
+		cpu_sys_dev = get_cpu_sysdev(cpu);
+		err = sysfs_create_file(&cpu_sys_dev->kobj,
+				&attr_percpu_activate_hint.attr);
+		if (err)
+			break;
+		err = sysfs_create_file(&cpu_sys_dev->kobj,
+				&attr_percpu_deactivate_hint.attr);
+		if (err)
+			break;
+	}
+	return err;
+
+}
+
+static void __exit pseries_energy_cleanup(void)
+{
+	int cpu;
+	struct sys_device *cpu_sys_dev;
+
+	/* Remove the sysfs files */
+	sysfs_remove_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_activate_hint_list.attr);
+
+	sysfs_remove_file(&cpu_sysdev_class.kset.kobj,
+				&attr_cpu_deactivate_hint_list.attr);
+
+	for_each_possible_cpu(cpu) {
+		cpu_sys_dev = get_cpu_sysdev(cpu);
+		sysfs_remove_file(&cpu_sys_dev->kobj,
+				&attr_percpu_activate_hint.attr);

arch/powerpc/kernel/setup-common.c

Quoted 10 lines
arch/powerpc/kernel/setup-common.c +10 -0
+		sysfs_remove_file(&cpu_sys_dev->kobj,
+				&attr_percpu_deactivate_hint.attr);
+	}
+}
+
+module_init(pseries_energy_init);
+module_exit(pseries_energy_cleanup);
+MODULE_DESCRIPTION("Driver for pseries platform energy management");
+MODULE_AUTHOR("Vaidyanathan Srinivasan");
+MODULE_LICENSE("GPL");

Cheers,
Ben.

#6 lore raw
Quoted 10 lines · Benjamin Herrenschmidt
Quoted 9 lines · Vaidyanathan Srinivasan

Hi,

arch/powerpc/kernel/setup-common.c +0 -0
index 03dd6a2..fbd93e3 100644
@@ -359,6 +359,8 @@ void __init check_for_initrd(void)
 #ifdef CONFIG_SMP
 
Quoted 6 lines

arch/powerpc/kernel/setup-common.c void __init check_for_initrd(void) · line 362

Quoted 2 lines
arch/powerpc/kernel/setup-common.c +1 -0
 int threads_per_core, threads_shift;
+EXPORT_SYMBOL_GPL(threads_per_core);

While I agree it should be exported for the APIs in cputhread.h to be usable from a module, this variable shouldn't be -used- directly, but only via the API functions in there.

Ok agreed. This will be the first module to use this and hence will have to implement the API and export the function.

Quoted 17 lines

.../...

arch/powerpc/kernel/setup-common.c void __init check_for_initrd(void)

Quoted 12 lines
arch/powerpc/kernel/setup-common.c +12 -0
+
+#define MODULE_VERS "1.0"
+#define MODULE_NAME "pseries_energy"
+
+/* Helper Routines to convert between drc_index to cpu numbers */
+
+static u32 cpu_to_drc_index(int cpu)
+{
+	struct device_node *dn = NULL;
+	const int *indexes;
+	int i;
+	dn = of_find_node_by_name(dn, "cpus");

Check the result. Also that's not a nice way to do that, you should look for /cpus by path I reckon.

I will check the return code, but why do you feel getting the node by path is better? Is there any race, or we may have duplicate "cpus" node?

Quoted 3 lines

arch/powerpc/kernel/setup-common.c

Quoted 1 line
arch/powerpc/kernel/setup-common.c +1 -0
+	indexes = of_get_property(dn, "ibm,drc-indexes", NULL);

Check the result here too.

Yes, will do.

Quoted 4 lines

arch/powerpc/kernel/setup-common.c

Quoted 2 lines
arch/powerpc/kernel/setup-common.c +2 -0
+	/* Convert logical cpu number to core number */
+	i = cpu / threads_per_core;

Don't use that variable as I said earlier. Use cpu_thread_to_core()

Ack

Quoted 10 lines
Quoted 10 lines
arch/powerpc/kernel/setup-common.c +10 -0
+	/*
+	 * The first element indexes[0] is the number of drc_indexes
+	 * returned in the list.  Hence i+1 will get the drc_index
+	 * corresponding to core number i.
+	 */
+	WARN_ON(i > indexes[0]);
+	return indexes[i + 1];
+}
+
+static int drc_index_to_cpu(u32 drc_index)
Quoted 8 lines

arch/powerpc/kernel/setup-common.c

Quoted 6 lines
arch/powerpc/kernel/setup-common.c +6 -0
+{
+	struct device_node *dn = NULL;
+	const int *indexes;
+	int i, cpu;
+	dn = of_find_node_by_name(dn, "cpus");
+	indexes = of_get_property(dn, "ibm,drc-indexes", NULL);

Same comments, check results and use /cpus path

Sure. Will do.

Quoted 28 lines

arch/powerpc/kernel/setup-common.c

Quoted 11 lines
arch/powerpc/kernel/setup-common.c +11 -0
+	/*
+	 * First element in the array is the number of drc_indexes
+	 * returned.  Search through the list to find the matching
+	 * drc_index and get the core number
+	 */
+	for (i = 0; i < indexes[0]; i++) {
+		if (indexes[i + 1] == drc_index)
+			break;
+	}
+	/* Convert core number to logical cpu number */
+	cpu = i * threads_per_core;

Here's more annoying as we don't have an API in cputhread.h for that.

In fact, we have a confusion in there since cpu_first_thread_in_core() doesn't actually take a core number ...

I'm going to recommend a complicated approach but that's the best in the long run:

- First do a patch that renames cpu_first_thread_in_core() to something
clearer like cpu_first_thread_in_same_core() or cpu_leftmost_sibling() (I think I prefer the later). Rename the few users in arch/powerpc/mm/mmu_context_nohash.c and arch/powerpc/kernel/smp.c

- Then do a patch that adds a cpu_first_thread_of_core() that takes a
core number, basically does:

Quoted 7 lines

static inline int cpu_first_thread_of_core(int core)

{
	return core << threads_shift;
}

- Then add your modified H_BEST_ENERGY patch on top of it using the
above two as pre-reqs :-)

Ok, I can do that change and then base this patch on the new API.

Quoted 16 lines
Quoted 16 lines
arch/powerpc/kernel/setup-common.c +16 -0
+	return cpu;
+}
+
+/*
+ * pseries hypervisor call H_BEST_ENERGY provides hints to OS on
+ * preferred logical cpus to activate or deactivate for optimized
+ * energy consumption.
+ */
+
+#define FLAGS_MODE1	0x004E200000080E01
+#define FLAGS_MODE2	0x004E200000080401
+#define FLAGS_ACTIVATE  0x100
+
+static ssize_t get_best_energy_list(char *page, int activate)
+{
+	int rc, cnt, i, cpu;
Quoted 8 lines

arch/powerpc/kernel/setup-common.c

Quoted 7 lines
arch/powerpc/kernel/setup-common.c +7 -0
+	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
+	unsigned long flags = 0;
+	u32 *buf_page;
+	char *s = page;
+
+	buf_page = (u32 *) get_zeroed_page(GFP_KERNEL);
+

Why that blank line ?

I will remove them

Quoted 4 lines

arch/powerpc/kernel/setup-common.c

Quoted 2 lines
arch/powerpc/kernel/setup-common.c +2 -0
+	if (!buf_page)
+		return 0;

So here you return 0 instead of -ENOMEM

Will fix. -ENOMEM is correct.

Quoted 10 lines

arch/powerpc/kernel/setup-common.c

Quoted 8 lines
arch/powerpc/kernel/setup-common.c +8 -0
+	flags = FLAGS_MODE1;
+	if (activate)
+		flags |= FLAGS_ACTIVATE;
+
+	rc = plpar_hcall9(H_BEST_ENERGY, retbuf, flags, 0, __pa(buf_page),
+				0, 0, 0, 0, 0, 0);
+
+	

Again, no need for a blank line before xx = foo() and if (xx)

Ack.

Quoted 6 lines

arch/powerpc/kernel/setup-common.c

Quoted 4 lines

if (rc != H_SUCCESS) {

arch/powerpc/kernel/setup-common.c +3 -0
+		free_page((unsigned long) buf_page);
+		return -EINVAL;
+	}

And here you return an error code. Which one is right ?

Returning an error code is correct to user space. The call will check for return value on read while getting the list.

Thanks for the detailed review. I will fix the issues that you pointed out and post the next version.

--Vaidy

#7 lore raw
Quoted 6 lines · Vaidyanathan Srinivasan
Quoted 2 lines

Check the result. Also that's not a nice way to do that, you should look for /cpus by path I reckon.

I will check the return code, but why do you feel getting the node by path is better? Is there any race, or we may have duplicate "cpus" node?

Well, you never really know what might be down the device-tree :-) /cpus is well defined where it is, while god knows what would happen if somebody had the weird idea to have something called "cpus" below some device driver for whatever reason they might even.

Cheers,
Ben.