Skip to contentSkip to conversation

intel_idle: set the state_tables array into __initdata to save mem

PATCH In review Chuansheng Liu 2 participants 13 messages Mar 7, 2013 – Mar 11, 2013 Intel idle driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 intel_idle: set the state_tables array into __initdata to save mem

Currently, in intel_idle.c, there are 5 state_tables array, every array size is sizeof(struct cpuidle_state) * CPUIDLE_STATE_MAX.

But after intel_idle_probe(), just only one array is useful.

Here we can just define one static state_table, and initialize it in intel_idle_probe(), and set other data state_tables as __initdata.

It can save about 3K, which also benefits mobile devices.

---
 drivers/idle/intel_idle.c |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
drivers/idle/intel_idle.c +12 -7
index 5d66750..0642bfe 100644
@@ -99,7 +99,7 @@ static int intel_idle(struct cpuidle_device *dev,
 			struct cpuidle_driver *drv, int index);
 static int intel_idle_cpu_init(int cpu);
 
-static struct cpuidle_state *cpuidle_state_table;
+static struct cpuidle_state cpuidle_state_table[CPUIDLE_STATE_MAX];
 
 /*
  * Set this flag for states where the HW flushes the TLB for us
@@ -124,7 +124,7 @@ static struct cpuidle_state *cpuidle_state_table;
  * which is also the index into the MWAIT hint array.
  * Thus C0 is a dummy.
  */
-static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-NHM",
 		.desc = "MWAIT 0x00",
@@ -157,7 +157,7 @@ static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-SNB",
 		.desc = "MWAIT 0x00",
@@ -197,7 +197,7 @@ static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-IVB",
 		.desc = "MWAIT 0x00",
@@ -237,7 +237,7 @@ static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-HSW",
 		.desc = "MWAIT 0x00",
@@ -277,7 +277,7 @@ static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1E-ATM",
 		.desc = "MWAIT 0x00",
@@ -504,7 +504,12 @@ static int intel_idle_probe(void)
 	pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
 
 	icpu = (const struct idle_cpu *)id->driver_data;
-	cpuidle_state_table = icpu->state_table;
+	/* Copy the icpu->state_table into cpuidle_state_table,
+	 * The pointing array by icpu->state_table is with __initdata,
+	 * which will be freed after kernel init ending.
+	 */
+	memcpy(cpuidle_state_table, icpu->state_table,
+			sizeof(cpuidle_state_table));
 
 	if (boot_cpu_has(X86_FEATURE_ARAT))	/* Always Reliable APIC Timer */
 		lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 75 lines · Chuansheng Liu

Currently, in intel_idle.c, there are 5 state_tables array, every array size is sizeof(struct cpuidle_state) * CPUIDLE_STATE_MAX.

But after intel_idle_probe(), just only one array is useful.

Here we can just define one static state_table, and initialize it in intel_idle_probe(), and set other data state_tables as __initdata.

It can save about 3K, which also benefits mobile devices.

---
 drivers/idle/intel_idle.c |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
drivers/idle/intel_idle.c +6 -6
index 5d66750..0642bfe 100644
@@ -99,7 +99,7 @@ static int intel_idle(struct cpuidle_device *dev,
 			struct cpuidle_driver *drv, int index);
 static int intel_idle_cpu_init(int cpu);
 
-static struct cpuidle_state *cpuidle_state_table;
+static struct cpuidle_state cpuidle_state_table[CPUIDLE_STATE_MAX];
 
 /*
  * Set this flag for states where the HW flushes the TLB for us
@@ -124,7 +124,7 @@ static struct cpuidle_state *cpuidle_state_table;
  * which is also the index into the MWAIT hint array.
  * Thus C0 is a dummy.
  */
-static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-NHM",
 		.desc = "MWAIT 0x00",
@@ -157,7 +157,7 @@ static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-SNB",
 		.desc = "MWAIT 0x00",
@@ -197,7 +197,7 @@ static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-IVB",
 		.desc = "MWAIT 0x00",
@@ -237,7 +237,7 @@ static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-HSW",
 		.desc = "MWAIT 0x00",
@@ -277,7 +277,7 @@ static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1E-ATM",
 		.desc = "MWAIT 0x00",

drivers/idle/intel_idle.c static int intel_idle_probe(void) · line 512

Quoted 10 lines
drivers/idle/intel_idle.c +6 -1
@@ -504,7 +504,12 @@ static int intel_idle_probe(void)
 	pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
 
 	icpu = (const struct idle_cpu *)id->driver_data;
-	cpuidle_state_table = icpu->state_table;
+	/* Copy the icpu->state_table into cpuidle_state_table,
+	 * The pointing array by icpu->state_table is with __initdata,
+	 * which will be freed after kernel init ending.
+	 */
+	memcpy(cpuidle_state_table, icpu->state_table,
+			sizeof(cpuidle_state_table));

The idea is good but it could be pushed a bit further.

Instead of changing cpuidle_state_table to an array, change it to an initdata and the functions intel_idle_cpuidle_driver_init, intel_idle_probe to init functions.

That would also be nice to get rid of a global variable to store the current cpu data and let the intel_idle_probe to return the pointer to the right table.

Quoted 3 lines
drivers/idle/intel_idle.c +0 -0
 	if (boot_cpu_has(X86_FEATURE_ARAT))	/* Always Reliable APIC Timer */
 		lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
 
Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#2 lore raw
Quoted 107 lines

-----Original Message-----
From: Daniel Lezcano [mailto:[email protected]]
Sent: Thursday, March 07, 2013 5:49 PM
To: Liu, Chuansheng
Cc: [email protected]; Brown, Len; [email protected]; [email protected]
Subject: Re: [PATCH] intel_idle: set the state_tables array into __initdata to save mem

Quoted 30 lines · Chuansheng Liu

Currently, in intel_idle.c, there are 5 state_tables array, every array size is sizeof(struct cpuidle_state) * CPUIDLE_STATE_MAX.

But after intel_idle_probe(), just only one array is useful.

Here we can just define one static state_table, and initialize it in intel_idle_probe(), and set other data state_tables as __initdata.

It can save about 3K, which also benefits mobile devices.

---
 drivers/idle/intel_idle.c |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
drivers/idle/intel_idle.c +1 -1
index 5d66750..0642bfe 100644
@@ -99,7 +99,7 @@ static int intel_idle(struct cpuidle_device *dev,
 			struct cpuidle_driver *drv, int index);
 static int intel_idle_cpu_init(int cpu);
 
-static struct cpuidle_state *cpuidle_state_table;
+static struct cpuidle_state cpuidle_state_table[CPUIDLE_STATE_MAX];
 
 /*
  * Set this flag for states where the HW flushes the TLB for us

drivers/idle/intel_idle.c static struct cpuidle_state *cpuidle_state_table; · line 127

Quoted 5 lines
drivers/idle/intel_idle.c +1 -1
@@ -124,7 +124,7 @@ static struct cpuidle_state *cpuidle_state_table;
  * which is also the index into the MWAIT hint array.
  * Thus C0 is a dummy.
  */
-static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX]

__initdata = {

drivers/idle/intel_idle.c static struct cpuidle_state

Quoted 4 lines
drivers/idle/intel_idle.c +0 -0
 	{
 		.name = "C1-NHM",
 		.desc = "MWAIT 0x00",
@@ -157,7 +157,7 @@ static struct cpuidle_state

nehalem_cstates[CPUIDLE_STATE_MAX] = {

drivers/idle/intel_idle.c static struct cpuidle_state

Quoted 9 lines
drivers/idle/intel_idle.c +1 -1
 		.enter = NULL }
 };
 
-static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-SNB",
 		.desc = "MWAIT 0x00",
@@ -197,7 +197,7 @@ static struct cpuidle_state

snb_cstates[CPUIDLE_STATE_MAX] = {

drivers/idle/intel_idle.c static struct cpuidle_state

Quoted 9 lines
drivers/idle/intel_idle.c +1 -1
 		.enter = NULL }
 };
 
-static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-IVB",
 		.desc = "MWAIT 0x00",
@@ -237,7 +237,7 @@ static struct cpuidle_state

ivb_cstates[CPUIDLE_STATE_MAX] = {

drivers/idle/intel_idle.c static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {

Quoted 5 lines
drivers/idle/intel_idle.c +1 -1
 		.enter = NULL }
 };
 
-static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] __initdata =

{

drivers/idle/intel_idle.c static struct cpuidle_state

Quoted 4 lines
drivers/idle/intel_idle.c +0 -0
 	{
 		.name = "C1-HSW",
 		.desc = "MWAIT 0x00",
@@ -277,7 +277,7 @@ static struct cpuidle_state

hsw_cstates[CPUIDLE_STATE_MAX] = {

drivers/idle/intel_idle.c static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {

Quoted 5 lines
drivers/idle/intel_idle.c +1 -1
 		.enter = NULL }
 };
 
-static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] __initdata

= {

Quoted 4 lines
drivers/idle/intel_idle.c +0 -0
 	{
 		.name = "C1E-ATM",
 		.desc = "MWAIT 0x00",

drivers/idle/intel_idle.c static int intel_idle_probe(void) · line 512

Quoted 10 lines
drivers/idle/intel_idle.c +6 -1
@@ -504,7 +504,12 @@ static int intel_idle_probe(void)
 	pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
 
 	icpu = (const struct idle_cpu *)id->driver_data;
-	cpuidle_state_table = icpu->state_table;
+	/* Copy the icpu->state_table into cpuidle_state_table,
+	 * The pointing array by icpu->state_table is with __initdata,
+	 * which will be freed after kernel init ending.
+	 */
+	memcpy(cpuidle_state_table, icpu->state_table,
+			sizeof(cpuidle_state_table));

The idea is good but it could be pushed a bit further.

Quoted 3 lines

Instead of changing cpuidle_state_table to an array, change it to an initdata and the functions intel_idle_cpuidle_driver_init, intel_idle_probe to init functions.

You are right. The global variable intel_idle_driver will has the copy of cpuidle_state_table[], will update the new patch. Thanks.

Quoted 4 lines

That would also be nice to get rid of a global variable to store the current cpu data and let the intel_idle_probe to return the pointer to the right table.

Sounds reasonable, will have a try also. Thanks again.

Quoted 13 lines

drivers/idle/intel_idle.c static int intel_idle_probe(void)

Quoted 5 lines
drivers/idle/intel_idle.c +0 -0
 	if (boot_cpu_has(X86_FEATURE_ARAT))	/* Always Reliable APIC Timer */
 		lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
 

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

#3 lore raw

cover intel_idle: set the state_tables array into __initdata to save mem

In reply to Chuansheng Liu

As Daniel suggested, I did some cleanup before setting the state_tables array into __initdata.

Thanks your help to review them.

[PATCH 1/3] intel_idle: changing the continue to break in intel_idle_cpu_init() [PATCH 2/3] intel_idle: Removing the redundant calculating for dev->state_count [PATCH 3/3] intel_idle: set the state_tables array as __initdata to save mem

#4 lore raw

PATCH 1/3 intel_idle: changing the continue to break in intel_idle_cpu_init()

According to commit e022e7eb9, the .enter == NULL is the last one in state_tables[].

So just like intel_idle_cpuidle_driver_init(), in case of .enter == NULL, breaking the for(;;) loop directly.

---
 drivers/idle/intel_idle.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/idle/intel_idle.c +1 -1
index 5d66750..17c9cf9 100644
@@ -610,7 +610,7 @@ static int intel_idle_cpu_init(int cpu)
 		int num_substates, mwait_hint, mwait_cstate, mwait_substate;
 
 		if (cpuidle_state_table[cstate].enter == NULL)
-			continue;
+			break;
 
 		if (cstate + 1 > max_cstate) {
 			printk(PREFIX "max_cstate %d reached\n", max_cstate);
Signature · 1 line
1.7.0.4
Acked-by on v1 1/3
#5 lore raw

Re: [PATCH 1/3] intel_idle: changing the continue to break in intel_idle_cpu_init()

Quoted 9 lines · Chuansheng Liu

According to commit e022e7eb9, the .enter == NULL is the last one in state_tables[].

So just like intel_idle_cpuidle_driver_init(), in case of .enter == NULL, breaking the for(;;) loop directly.

---

Sounds good.

Quoted 17 lines
 drivers/idle/intel_idle.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/idle/intel_idle.c +1 -1
index 5d66750..17c9cf9 100644
@@ -610,7 +610,7 @@ static int intel_idle_cpu_init(int cpu)
 		int num_substates, mwait_hint, mwait_cstate, mwait_substate;
 
 		if (cpuidle_state_table[cstate].enter == NULL)
-			continue;
+			break;
 
 		if (cstate + 1 > max_cstate) {
 			printk(PREFIX "max_cstate %d reached\n", max_cstate);
Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#6 lore raw

PATCH 2/3 intel_idle: Removing the redundant calculating for dev->state_count

In reply to Chuansheng Liu

In function intel_idle_cpu_init() and intel_idle_cpuidle_driver_init(), they are having the same for(;;) loop.

Here in intel_idle_cpu_init(), the dev->state_count can be assigned by drv->state_count directly.

---
 drivers/idle/intel_idle.c |   30 ++----------------------------
 1 files changed, 2 insertions(+), 28 deletions(-)
drivers/idle/intel_idle.c +2 -28
index 17c9cf9..503b401 100644
@@ -599,38 +599,12 @@ static int intel_idle_cpuidle_driver_init(void)
  */
 static int intel_idle_cpu_init(int cpu)
 {
-	int cstate;
 	struct cpuidle_device *dev;
+	struct cpuidle_driver *drv = &intel_idle_driver;
 
 	dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
 
-	dev->state_count = 1;
-
-	for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
-		int num_substates, mwait_hint, mwait_cstate, mwait_substate;
-
-		if (cpuidle_state_table[cstate].enter == NULL)
-			break;
-
-		if (cstate + 1 > max_cstate) {
-			printk(PREFIX "max_cstate %d reached\n", max_cstate);
-			break;
-		}
-
-		mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
-		mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
-		mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
-
-		/* does the state exist in CPUID.MWAIT? */
-		num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
-					& MWAIT_SUBSTATE_MASK;
-
-		/* if sub-state in table is not enumerated by CPUID */
-		if ((mwait_substate + 1) > num_substates)
-			continue;
-
-		dev->state_count += 1;
-	}
+	dev->state_count = drv->state_count;
 
 	dev->cpu = cpu;
 
Signature · 1 line
1.7.0.4
#7 lore raw

Re: [PATCH 2/3] intel_idle: Removing the redundant calculating for dev->state_count

Quoted 44 lines · Chuansheng Liu

In function intel_idle_cpu_init() and intel_idle_cpuidle_driver_init(), they are having the same for(;;) loop.

Here in intel_idle_cpu_init(), the dev->state_count can be assigned by drv->state_count directly.

---
 drivers/idle/intel_idle.c |   30 ++----------------------------
 1 files changed, 2 insertions(+), 28 deletions(-)
drivers/idle/intel_idle.c +1 -19
index 17c9cf9..503b401 100644
@@ -599,38 +599,12 @@ static int intel_idle_cpuidle_driver_init(void)
  */
 static int intel_idle_cpu_init(int cpu)
 {
-	int cstate;
 	struct cpuidle_device *dev;
+	struct cpuidle_driver *drv = &intel_idle_driver;
 
 	dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
 
-	dev->state_count = 1;
-
-	for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
-		int num_substates, mwait_hint, mwait_cstate, mwait_substate;
-
-		if (cpuidle_state_table[cstate].enter == NULL)
-			break;
-
-		if (cstate + 1 > max_cstate) {
-			printk(PREFIX "max_cstate %d reached\n", max_cstate);
-			break;
-		}
-
-		mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
-		mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
-		mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
-
-		/* does the state exist in CPUID.MWAIT? */

drivers/idle/intel_idle.c static int intel_idle_cpuidle_driver_init(void) · line 607

Quoted 10 lines
drivers/idle/intel_idle.c +1 -9
-		num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
-					& MWAIT_SUBSTATE_MASK;
-
-		/* if sub-state in table is not enumerated by CPUID */
-		if ((mwait_substate + 1) > num_substates)
-			continue;
-
-		dev->state_count += 1;
-	}
+	dev->state_count = drv->state_count;

The cpuidle_register_device function already does this initialization.

Probably you can get rid of this initialization and certainly factor out a bit the code in this case.

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#8 lore raw

RE: [PATCH 2/3] intel_idle: Removing the redundant calculating for dev->state_count

Quoted 60 lines

-----Original Message-----
From: Daniel Lezcano [mailto:[email protected]]
Sent: Saturday, March 09, 2013 11:00 AM
To: Liu, Chuansheng
Cc: [email protected]; Brown, Len; [email protected]; [email protected]
Subject: Re: [PATCH 2/3] intel_idle: Removing the redundant calculating for dev->state_count

Quoted 48 lines · Chuansheng Liu

In function intel_idle_cpu_init() and intel_idle_cpuidle_driver_init(), they are having the same for(;;) loop.

Here in intel_idle_cpu_init(), the dev->state_count can be assigned by drv->state_count directly.

---
 drivers/idle/intel_idle.c |   30 ++----------------------------
 1 files changed, 2 insertions(+), 28 deletions(-)
drivers/idle/intel_idle.c +1 -23
index 17c9cf9..503b401 100644
@@ -599,38 +599,12 @@ static int intel_idle_cpuidle_driver_init(void)
  */
 static int intel_idle_cpu_init(int cpu)
 {
-	int cstate;
 	struct cpuidle_device *dev;
+	struct cpuidle_driver *drv = &intel_idle_driver;
 
 	dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
 
-	dev->state_count = 1;
-
-	for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
-		int num_substates, mwait_hint, mwait_cstate, mwait_substate;
-
-		if (cpuidle_state_table[cstate].enter == NULL)
-			break;
-
-		if (cstate + 1 > max_cstate) {
-			printk(PREFIX "max_cstate %d reached\n", max_cstate);
-			break;
-		}
-
-		mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
-		mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
-		mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
-
-		/* does the state exist in CPUID.MWAIT? */
-		num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
-					& MWAIT_SUBSTATE_MASK;
-
-		/* if sub-state in table is not enumerated by CPUID */
Quoted 8 lines

drivers/idle/intel_idle.c static int intel_idle_cpuidle_driver_init(void) · line 607

Quoted 6 lines
drivers/idle/intel_idle.c +1 -5
-		if ((mwait_substate + 1) > num_substates)
-			continue;
-
-		dev->state_count += 1;
-	}
+	dev->state_count = drv->state_count;

The cpuidle_register_device function already does this initialization.

Thanks your remind, will send patch V2 to remove it.

Quoted 11 lines

Probably you can get rid of this initialization and certainly factor out a bit the code in this case.

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

#9 lore raw

PATCH v2 2/3 intel_idle: Removing the redundant calculating for dev->state_count

In reply to Chuansheng Liu

In function intel_idle_cpu_init() and intel_idle_cpuidle_driver_init(), they are having the same for(;;) loop to count the ->state_count.

Although intel_idle_cpu_init() can be called at runtime CPU HOTPLUG case, but max_cstate can not be changed at runtime.

So the dev->state_count should be == drv->state_count, in the function cpuidle_register_device() has done the initialization.

Here we can clean up these pieces of code.

---
 drivers/idle/intel_idle.c |   29 -----------------------------
 1 files changed, 0 insertions(+), 29 deletions(-)
drivers/idle/intel_idle.c +0 -29
index 17c9cf9..bb0ae0b 100644
@@ -599,39 +599,10 @@ static int intel_idle_cpuidle_driver_init(void)
  */
 static int intel_idle_cpu_init(int cpu)
 {
-	int cstate;
 	struct cpuidle_device *dev;
 
 	dev = per_cpu_ptr(intel_idle_cpuidle_devices, cpu);
 
-	dev->state_count = 1;
-
-	for (cstate = 0; cstate < CPUIDLE_STATE_MAX; ++cstate) {
-		int num_substates, mwait_hint, mwait_cstate, mwait_substate;
-
-		if (cpuidle_state_table[cstate].enter == NULL)
-			break;
-
-		if (cstate + 1 > max_cstate) {
-			printk(PREFIX "max_cstate %d reached\n", max_cstate);
-			break;
-		}
-
-		mwait_hint = flg2MWAIT(cpuidle_state_table[cstate].flags);
-		mwait_cstate = MWAIT_HINT2CSTATE(mwait_hint);
-		mwait_substate = MWAIT_HINT2SUBSTATE(mwait_hint);
-
-		/* does the state exist in CPUID.MWAIT? */
-		num_substates = (mwait_substates >> ((mwait_cstate + 1) * 4))
-					& MWAIT_SUBSTATE_MASK;
-
-		/* if sub-state in table is not enumerated by CPUID */
-		if ((mwait_substate + 1) > num_substates)
-			continue;
-
-		dev->state_count += 1;
-	}
-
 	dev->cpu = cpu;
 
 	if (cpuidle_register_device(dev)) {
Signature · 1 line
1.7.0.4
Acked-by on v2 2/3
#10 lore raw

Re: [PATCH 2/3 V2] intel_idle: Removing the redundant calculating for dev->state_count

Quoted 7 lines · Chuansheng Liu

In function intel_idle_cpu_init() and intel_idle_cpuidle_driver_init(), they are having the same for(;;) loop to count the ->state_count.

Although intel_idle_cpu_init() can be called at runtime CPU HOTPLUG case, but max_cstate can not be changed at runtime.

Quoted 7 lines

So the dev->state_count should be == drv->state_count, in the function cpuidle_register_device() has done the initialization.

Here we can clean up these pieces of code.

---

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#11 lore raw

PATCH 3/3 intel_idle: set the state_tables array as __initdata to save mem

In reply to Chuansheng Liu

Currently, in intel_idle.c, there are 5 state_tables array, every array size is sizeof(struct cpuidle_state) * CPUIDLE_STATE_MAX.

As in intel_idle_cpuidle_driver_init(), we have copied the data into intel_idle_driver->state[], so do not need to keep state_tables[] there any more after system init.

It will save about 3~4k memory, also benefits mobile devices. Here changing them as __initdata, also removing global var cpuidle_state_table pointer.

---
 drivers/idle/intel_idle.c |   18 ++++++++----------
 1 files changed, 8 insertions(+), 10 deletions(-)
drivers/idle/intel_idle.c +8 -10
index 503b401..cbdc952 100644
@@ -99,8 +99,6 @@ static int intel_idle(struct cpuidle_device *dev,
 			struct cpuidle_driver *drv, int index);
 static int intel_idle_cpu_init(int cpu);
 
-static struct cpuidle_state *cpuidle_state_table;
-
 /*
  * Set this flag for states where the HW flushes the TLB for us
  * and so we don't need cross-calls to keep it consistent.
@@ -124,7 +122,7 @@ static struct cpuidle_state *cpuidle_state_table;
  * which is also the index into the MWAIT hint array.
  * Thus C0 is a dummy.
  */
-static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-NHM",
 		.desc = "MWAIT 0x00",
@@ -157,7 +155,7 @@ static struct cpuidle_state nehalem_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-SNB",
 		.desc = "MWAIT 0x00",
@@ -197,7 +195,7 @@ static struct cpuidle_state snb_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-IVB",
 		.desc = "MWAIT 0x00",
@@ -237,7 +235,7 @@ static struct cpuidle_state ivb_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1-HSW",
 		.desc = "MWAIT 0x00",
@@ -277,7 +275,7 @@ static struct cpuidle_state hsw_cstates[CPUIDLE_STATE_MAX] = {
 		.enter = NULL }
 };
 
-static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] = {
+static struct cpuidle_state atom_cstates[CPUIDLE_STATE_MAX] __initdata = {
 	{
 		.name = "C1E-ATM",
 		.desc = "MWAIT 0x00",
@@ -472,7 +470,7 @@ MODULE_DEVICE_TABLE(x86cpu, intel_idle_ids);
 /*
  * intel_idle_probe()
  */
-static int intel_idle_probe(void)
+static int __init intel_idle_probe(void)
 {
 	unsigned int eax, ebx, ecx;
 	const struct x86_cpu_id *id;
@@ -504,7 +502,6 @@ static int intel_idle_probe(void)
 	pr_debug(PREFIX "MWAIT substates: 0x%x\n", mwait_substates);
 
 	icpu = (const struct idle_cpu *)id->driver_data;
-	cpuidle_state_table = icpu->state_table;
 
 	if (boot_cpu_has(X86_FEATURE_ARAT))	/* Always Reliable APIC Timer */
 		lapic_timer_reliable_states = LAPIC_TIMER_ALWAYS_RELIABLE;
@@ -540,10 +537,11 @@ static void intel_idle_cpuidle_devices_uninit(void)
  * intel_idle_cpuidle_driver_init()
  * allocate, initialize cpuidle_states
  */
-static int intel_idle_cpuidle_driver_init(void)
+static int __init intel_idle_cpuidle_driver_init(void)
 {
 	int cstate;
 	struct cpuidle_driver *drv = &intel_idle_driver;
+	struct cpuidle_state *cpuidle_state_table = icpu->state_table;
 
 	drv->state_count = 1;
 
Signature · 1 line
1.7.0.4
Acked-by on v1 3/3
#12 lore raw

Re: [PATCH 3/3] intel_idle: set the state_tables array as __initdata to save mem

Quoted 8 lines · Chuansheng Liu

Currently, in intel_idle.c, there are 5 state_tables array, every array size is sizeof(struct cpuidle_state) * CPUIDLE_STATE_MAX.

As in intel_idle_cpuidle_driver_init(), we have copied the data into intel_idle_driver->state[], so do not need to keep state_tables[] there any more after system init.

Quoted 6 lines

It will save about 3~4k memory, also benefits mobile devices. Here changing them as __initdata, also removing global var cpuidle_state_table pointer.

---

Signature · 5 lines
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog