All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v3 0/7] The power allocator thermal governor
@ 2014-06-03 10:18 Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 1/7] tracing: Add __bitmask() macro to trace events to cpumasks and other bitmasks Javi Merino
                   ` (6 more replies)
  0 siblings, 7 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel; +Cc: punit.agrawal, Javi Merino

Hi linux-pm,

The power allocator governor allocates device power to control
temperature.  This requires transforming performance requests into
requested power, which we do with the aid of power models.  Patch 5
(thermal: add a basic cpu power actor) implements a simple power model
for cpus.  The division of power between the actors ensures that power
is allocated where it is needed the most, based on the current
workload.

Patches 1 and 2 are not proper parts of these series and can be merged
separately.  Patch 1 (tracing: Add __bitmask() macro to trace events
to cpumasks and other bitmasks) is already in for-next[1].  Patch 2
(thermal: document struct thermal_zone_device and thermal_governor)
has already been submitted to linux-pm[2] and is generic.

[1] https://git.kernel.org/cgit/linux/kernel/git/rostedt/linux-trace.git/commit/?h=for-next&id=4449bf927b61b
db4389393c6fea6837214d1ace7
[2] http://article.gmane.org/gmane.linux.power-management.general/46041

Changes since v2:
  - Changed the PI controller into a PID controller
  - Added static power to the cpu power model
  - tz parameter max_dissipatable_power renamed to sustainable_power
  - Register the cpufreq cooling device as part of the
    power_cpu_actor registration.

Changes since v1:
  - Fixed finding cpufreq cooling devices in cpufreq_frequency_change()
  - Replaced the cooling device interface with a separate power actor
    API
  - Addressed most of Eduardo's comments
  - Incorporated ftrace support for bitmask to trace cpumasks

Todo:
  - Fix kerneldoc strings
  - Use tz->passive
  - Use cpufreq_get() to get the frequency of the cpu instead of
    using the notifiers.
  - Rethink the use of trip points and make it less intrusive
  - Let platforms override the power allocator governor parameters
  - Add more tracing and provide scripts to evaluate the proposal.
  - Tune it to achieve the temperature stability we are aiming for

Cheers,
Javi & Punit

Javi Merino (6):
  thermal: document struct thermal_zone_device and thermal_governor
  thermal: let governors have private data for each thermal zone
  thermal: introduce the Power Actor API
  thermal: add a basic cpu power actor
  thermal: introduce the Power Allocator governor
  thermal: add trace events to the power allocator governor

Steven Rostedt (Red Hat) (1):
  tracing: Add __bitmask() macro to trace events to cpumasks and other
    bitmasks

 Documentation/thermal/power_actor.txt     | 164 ++++++++
 Documentation/thermal/power_allocator.txt |  42 +++
 drivers/thermal/Kconfig                   |  23 ++
 drivers/thermal/Makefile                  |   3 +
 drivers/thermal/power_actor/Kconfig       |   9 +
 drivers/thermal/power_actor/Makefile      |   7 +
 drivers/thermal/power_actor/cpu_actor.c   | 606 ++++++++++++++++++++++++++++++
 drivers/thermal/power_actor/power_actor.c |  64 ++++
 drivers/thermal/power_actor/power_actor.h | 105 ++++++
 drivers/thermal/power_allocator.c         | 465 +++++++++++++++++++++++
 drivers/thermal/thermal_core.c            |  90 ++++-
 drivers/thermal/thermal_core.h            |   8 +
 include/linux/ftrace_event.h              |   3 +
 include/linux/thermal.h                   |  62 ++-
 include/linux/trace_seq.h                 |  10 +
 include/trace/events/thermal.h            |  38 ++
 include/trace/events/thermal_governor.h   |  37 ++
 include/trace/ftrace.h                    |  57 ++-
 kernel/trace/trace_output.c               |  41 ++
 19 files changed, 1822 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/thermal/power_actor.txt
 create mode 100644 Documentation/thermal/power_allocator.txt
 create mode 100644 drivers/thermal/power_actor/Kconfig
 create mode 100644 drivers/thermal/power_actor/Makefile
 create mode 100644 drivers/thermal/power_actor/cpu_actor.c
 create mode 100644 drivers/thermal/power_actor/power_actor.c
 create mode 100644 drivers/thermal/power_actor/power_actor.h
 create mode 100644 drivers/thermal/power_allocator.c
 create mode 100644 include/trace/events/thermal.h
 create mode 100644 include/trace/events/thermal_governor.h

-- 
1.9.1



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RFC PATCH v3 1/7] tracing: Add __bitmask() macro to trace events to cpumasks and other bitmasks
  2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
@ 2014-06-03 10:18 ` Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 2/7] thermal: document struct thermal_zone_device and thermal_governor Javi Merino
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel; +Cc: punit.agrawal, Steven Rostedt (Red Hat)

From: "Steven Rostedt (Red Hat)" <rostedt@goodmis.org>

Being able to show a cpumask of events can be useful as some events
may affect only some CPUs. There is no standard way to record the
cpumask and converting it to a string is rather expensive during
the trace as traces happen in hotpaths. It would be better to record
the raw event mask and be able to parse it at print time.

The following macros were added for use with the TRACE_EVENT() macro:

  __bitmask()
  __assign_bitmask()
  __get_bitmask()

To test this, I added this to the sched_migrate_task event, which
looked like this:

TRACE_EVENT(sched_migrate_task,

	TP_PROTO(struct task_struct *p, int dest_cpu, const struct cpumask *cpus),

	TP_ARGS(p, dest_cpu, cpus),

	TP_STRUCT__entry(
		__array(	char,	comm,	TASK_COMM_LEN	)
		__field(	pid_t,	pid			)
		__field(	int,	prio			)
		__field(	int,	orig_cpu		)
		__field(	int,	dest_cpu		)
		__bitmask(	cpumask, num_possible_cpus()	)
	),

	TP_fast_assign(
		memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
		__entry->pid		= p->pid;
		__entry->prio		= p->prio;
		__entry->orig_cpu	= task_cpu(p);
		__entry->dest_cpu	= dest_cpu;
		__assign_bitmask(cpumask, cpumask_bits(cpus), num_possible_cpus());
	),

	TP_printk("comm=%s pid=%d prio=%d orig_cpu=%d dest_cpu=%d cpumask=%s",
		  __entry->comm, __entry->pid, __entry->prio,
		  __entry->orig_cpu, __entry->dest_cpu,
		  __get_bitmask(cpumask))
);

With the output of:

        ksmtuned-3613  [003] d..2   485.220508: sched_migrate_task: comm=ksmtuned pid=3615 prio=120 orig_cpu=3 dest_cpu=2 cpumask=00000000,0000000f
     migration/1-13    [001] d..5   485.221202: sched_migrate_task: comm=ksmtuned pid=3614 prio=120 orig_cpu=1 dest_cpu=0 cpumask=00000000,0000000f
             awk-3615  [002] d.H5   485.221747: sched_migrate_task: comm=rcu_preempt pid=7 prio=120 orig_cpu=0 dest_cpu=1 cpumask=00000000,000000ff
     migration/2-18    [002] d..5   485.222062: sched_migrate_task: comm=ksmtuned pid=3615 prio=120 orig_cpu=2 dest_cpu=3 cpumask=00000000,0000000f

Link: http://lkml.kernel.org/r/1399377998-14870-6-git-send-email-javi.merino@arm.com
Link: http://lkml.kernel.org/r/20140506132238.22e136d1@gandalf.local.home

Suggested-by: Javi Merino <javi.merino@arm.com>
Tested-by: Javi Merino <javi.merino@arm.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---

Note: When sending the pull request to Linus, state that you
cherry-picked the commit from
https://git.kernel.org/cgit/linux/kernel/git/rostedt/linux-trace.git/commit/?h=for-next&id=4449bf927b61bdb4389393c6fea6837214d1ace7

 include/linux/ftrace_event.h |  3 +++
 include/linux/trace_seq.h    | 10 ++++++++
 include/trace/ftrace.h       | 57 +++++++++++++++++++++++++++++++++++++++++++-
 kernel/trace/trace_output.c  | 41 +++++++++++++++++++++++++++++++
 4 files changed, 110 insertions(+), 1 deletion(-)

diff --git a/include/linux/ftrace_event.h b/include/linux/ftrace_event.h
index d16da3e53bc7..cff3106ffe2c 100644
--- a/include/linux/ftrace_event.h
+++ b/include/linux/ftrace_event.h
@@ -38,6 +38,9 @@ const char *ftrace_print_symbols_seq_u64(struct trace_seq *p,
 								 *symbol_array);
 #endif
 
+const char *ftrace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
+				     unsigned int bitmask_size);
+
 const char *ftrace_print_hex_seq(struct trace_seq *p,
 				 const unsigned char *buf, int len);
 
diff --git a/include/linux/trace_seq.h b/include/linux/trace_seq.h
index a32d86ec8bf2..136116924d8d 100644
--- a/include/linux/trace_seq.h
+++ b/include/linux/trace_seq.h
@@ -46,6 +46,9 @@ extern int trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
 extern void *trace_seq_reserve(struct trace_seq *s, size_t len);
 extern int trace_seq_path(struct trace_seq *s, const struct path *path);
 
+extern int trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
+			     int nmaskbits);
+
 #else /* CONFIG_TRACING */
 static inline int trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
 {
@@ -57,6 +60,13 @@ trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
 	return 0;
 }
 
+static inline int
+trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
+		  int nmaskbits)
+{
+	return 0;
+}
+
 static inline int trace_print_seq(struct seq_file *m, struct trace_seq *s)
 {
 	return 0;
diff --git a/include/trace/ftrace.h b/include/trace/ftrace.h
index 0a1a4f7caf09..9b7a989dcbcc 100644
--- a/include/trace/ftrace.h
+++ b/include/trace/ftrace.h
@@ -53,6 +53,9 @@
 #undef __string
 #define __string(item, src) __dynamic_array(char, item, -1)
 
+#undef __bitmask
+#define __bitmask(item, nr_bits) __dynamic_array(char, item, -1)
+
 #undef TP_STRUCT__entry
 #define TP_STRUCT__entry(args...) args
 
@@ -128,6 +131,9 @@
 #undef __string
 #define __string(item, src) __dynamic_array(char, item, -1)
 
+#undef __bitmask
+#define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
+
 #undef DECLARE_EVENT_CLASS
 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
 	struct ftrace_data_offsets_##call {				\
@@ -200,6 +206,15 @@
 #undef __get_str
 #define __get_str(field) (char *)__get_dynamic_array(field)
 
+#undef __get_bitmask
+#define __get_bitmask(field)						\
+	({								\
+		void *__bitmask = __get_dynamic_array(field);		\
+		unsigned int __bitmask_size;				\
+		__bitmask_size = (__entry->__data_loc_##field >> 16) & 0xffff; \
+		ftrace_print_bitmask_seq(p, __bitmask, __bitmask_size);	\
+	})
+
 #undef __print_flags
 #define __print_flags(flag, delim, flag_array...)			\
 	({								\
@@ -322,6 +337,9 @@ static struct trace_event_functions ftrace_event_type_funcs_##call = {	\
 #undef __string
 #define __string(item, src) __dynamic_array(char, item, -1)
 
+#undef __bitmask
+#define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
+
 #undef DECLARE_EVENT_CLASS
 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print)	\
 static int notrace __init						\
@@ -372,6 +390,29 @@ ftrace_define_fields_##call(struct ftrace_event_call *event_call)	\
 #define __string(item, src) __dynamic_array(char, item,			\
 		    strlen((src) ? (const char *)(src) : "(null)") + 1)
 
+/*
+ * __bitmask_size_in_bytes_raw is the number of bytes needed to hold
+ * num_possible_cpus().
+ */
+#define __bitmask_size_in_bytes_raw(nr_bits)	\
+	(((nr_bits) + 7) / 8)
+
+#define __bitmask_size_in_longs(nr_bits)			\
+	((__bitmask_size_in_bytes_raw(nr_bits) +		\
+	  ((BITS_PER_LONG / 8) - 1)) / (BITS_PER_LONG / 8))
+
+/*
+ * __bitmask_size_in_bytes is the number of bytes needed to hold
+ * num_possible_cpus() padded out to the nearest long. This is what
+ * is saved in the buffer, just to be consistent.
+ */
+#define __bitmask_size_in_bytes(nr_bits)				\
+	(__bitmask_size_in_longs(nr_bits) * (BITS_PER_LONG / 8))
+
+#undef __bitmask
+#define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item,	\
+					 __bitmask_size_in_longs(nr_bits))
+
 #undef DECLARE_EVENT_CLASS
 #define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
 static inline notrace int ftrace_get_offsets_##call(			\
@@ -513,12 +554,22 @@ static inline notrace int ftrace_get_offsets_##call(			\
 	__entry->__data_loc_##item = __data_offsets.item;
 
 #undef __string
-#define __string(item, src) __dynamic_array(char, item, -1)       	\
+#define __string(item, src) __dynamic_array(char, item, -1)
 
 #undef __assign_str
 #define __assign_str(dst, src)						\
 	strcpy(__get_str(dst), (src) ? (const char *)(src) : "(null)");
 
+#undef __bitmask
+#define __bitmask(item, nr_bits) __dynamic_array(unsigned long, item, -1)
+
+#undef __get_bitmask
+#define __get_bitmask(field) (char *)__get_dynamic_array(field)
+
+#undef __assign_bitmask
+#define __assign_bitmask(dst, src, nr_bits)					\
+	memcpy(__get_bitmask(dst), (src), __bitmask_size_in_bytes(nr_bits))
+
 #undef TP_fast_assign
 #define TP_fast_assign(args...) args
 
@@ -586,6 +637,7 @@ static inline void ftrace_test_probe_##call(void)			\
 #undef __print_hex
 #undef __get_dynamic_array
 #undef __get_str
+#undef __get_bitmask
 
 #undef TP_printk
 #define TP_printk(fmt, args...) "\"" fmt "\", "  __stringify(args)
@@ -651,6 +703,9 @@ __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
 #undef __get_str
 #define __get_str(field) (char *)__get_dynamic_array(field)
 
+#undef __get_bitmask
+#define __get_bitmask(field) (char *)__get_dynamic_array(field)
+
 #undef __perf_addr
 #define __perf_addr(a)	(__addr = (a))
 
diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
index a436de18aa99..f3dad80c20b2 100644
--- a/kernel/trace/trace_output.c
+++ b/kernel/trace/trace_output.c
@@ -126,6 +126,34 @@ trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
 EXPORT_SYMBOL_GPL(trace_seq_printf);
 
 /**
+ * trace_seq_bitmask - put a list of longs as a bitmask print output
+ * @s:		trace sequence descriptor
+ * @maskp:	points to an array of unsigned longs that represent a bitmask
+ * @nmaskbits:	The number of bits that are valid in @maskp
+ *
+ * It returns 0 if the trace oversizes the buffer's free
+ * space, 1 otherwise.
+ *
+ * Writes a ASCII representation of a bitmask string into @s.
+ */
+int
+trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
+		  int nmaskbits)
+{
+	int len = (PAGE_SIZE - 1) - s->len;
+	int ret;
+
+	if (s->full || !len)
+		return 0;
+
+	ret = bitmap_scnprintf(s->buffer, len, maskp, nmaskbits);
+	s->len += ret;
+
+	return 1;
+}
+EXPORT_SYMBOL_GPL(trace_seq_bitmask);
+
+/**
  * trace_seq_vprintf - sequence printing of trace information
  * @s: trace sequence descriptor
  * @fmt: printf format string
@@ -399,6 +427,19 @@ EXPORT_SYMBOL(ftrace_print_symbols_seq_u64);
 #endif
 
 const char *
+ftrace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
+			 unsigned int bitmask_size)
+{
+	const char *ret = p->buffer + p->len;
+
+	trace_seq_bitmask(p, bitmask_ptr, bitmask_size * 8);
+	trace_seq_putc(p, 0);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(ftrace_print_bitmask_seq);
+
+const char *
 ftrace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
 {
 	int i;
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RFC PATCH v3 2/7] thermal: document struct thermal_zone_device and thermal_governor
  2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 1/7] tracing: Add __bitmask() macro to trace events to cpumasks and other bitmasks Javi Merino
@ 2014-06-03 10:18 ` Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 3/7] thermal: let governors have private data for each thermal zone Javi Merino
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel
  Cc: punit.agrawal, Javi Merino, Zhang Rui, Eduardo Valentin

Document struct thermal_zone_device and struct thermal_governor fields
and their use by the thermal framework code.

Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Signed-off-by: Javi Merino <javi.merino@arm.com>
---
 include/linux/thermal.h | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index f7e11c7ea7d9..6fca46c82c4d 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -158,6 +158,41 @@ struct thermal_attr {
 	char name[THERMAL_NAME_LENGTH];
 };
 
+/**
+ * struct thermal_zone_device - structure for a thermal zone
+ * @id:		unique id number for each thermal zone
+ * @type:	the thermal zone device type
+ * @device:	struct device for this thermal zone
+ * @trip_temp_attrs:	attributes for trip points for sysfs: trip temperature
+ * @trip_type_attrs:	attributes for trip points for sysfs: trip type
+ * @trip_hyst_attrs:	attributes for trip points for sysfs: trip hysteresis
+ * @devdata:	private pointer for device private data
+ * @trips:	number of trip points the thermal zone supports
+ * @passive_delay:	number of milliseconds to wait between polls when
+ *			performing passive cooling.  Currenty only used by the
+ *			step-wise governor
+ * @polling_delay:	number of milliseconds to wait between polls when
+ *			checking whether trip points have been crossed (0 for
+ *			interrupt driven systems)
+ * @temperature:	current temperature.  This is only for core code,
+ *			drivers should use thermal_zone_get_temp() to get the
+ *			current temperature
+ * @last_temperature:	previous temperature read
+ * @emul_temperature:	emulated temperature when using CONFIG_THERMAL_EMULATION
+ * @passive:		1 if you've crossed a passive trip point, 0 otherwise.
+ *			Currenty only used by the step-wise governor.
+ * @forced_passive:	If > 0, temperature at which to switch on all ACPI
+ *			processor cooling devices.  Currently only used by the
+ *			step-wise governor.
+ * @ops:	operations this thermal_zone_device supports
+ * @tzp:	thermal zone parameters
+ * @governor:	pointer to the governor for this thermal zone
+ * @thermal_instances:	list of struct thermal_instance of this thermal zone
+ * @idr:	struct idr to generate unique id for this zone's cooling devices
+ * @lock:	lock to protect thermal_instances list
+ * @node:	node in thermal_tz_list (in thermal_core.c)
+ * @poll_queue:	delayed work for polling
+ */
 struct thermal_zone_device {
 	int id;
 	char type[THERMAL_NAME_LENGTH];
@@ -179,12 +214,18 @@ struct thermal_zone_device {
 	struct thermal_governor *governor;
 	struct list_head thermal_instances;
 	struct idr idr;
-	struct mutex lock; /* protect thermal_instances list */
+	struct mutex lock;
 	struct list_head node;
 	struct delayed_work poll_queue;
 };
 
-/* Structure that holds thermal governor information */
+/**
+ * struct thermal_governor - structure that holds thermal governor information
+ * @name:	name of the governor
+ * @throttle:	callback called for every trip point even if temperature is
+ *		below the trip point temperature
+ * @governor_list:	node in thermal_governor_list (in thermal_core.c)
+ */
 struct thermal_governor {
 	char name[THERMAL_NAME_LENGTH];
 	int (*throttle)(struct thermal_zone_device *tz, int trip);
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RFC PATCH v3 3/7] thermal: let governors have private data for each thermal zone
  2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 1/7] tracing: Add __bitmask() macro to trace events to cpumasks and other bitmasks Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 2/7] thermal: document struct thermal_zone_device and thermal_governor Javi Merino
@ 2014-06-03 10:18 ` Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 4/7] thermal: introduce the Power Actor API Javi Merino
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel
  Cc: punit.agrawal, Javi Merino, Zhang Rui, Eduardo Valentin

A governor may need to store its current state between calls to
throttle().  That state depends on the thermal zone, so store it as
private data in struct thermal_zone_device.

The governors may have two new ops: bind_to_tz() and unbind_from_tz().
When provided, these functions let governors do some initialization
and teardown when they are bound/unbound to a tz and possibly store that
information in the governor_data field of the struct
thermal_zone_device.

Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Signed-off-by: Javi Merino <javi.merino@arm.com>
---
 drivers/thermal/thermal_core.c | 83 ++++++++++++++++++++++++++++++++++++++----
 include/linux/thermal.h        |  9 +++++
 2 files changed, 84 insertions(+), 8 deletions(-)

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 71b0ec0c370d..1b13d8e0cfd1 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -72,6 +72,58 @@ static struct thermal_governor *__find_governor(const char *name)
 	return NULL;
 }
 
+/**
+ * bind_previous_governor - bind the previous governor of the thermal zone
+ * @tz:		a valid pointer to a struct thermal_zone_device
+ * @failed_gov_name:	the name of the governor that failed to register
+ *
+ * Register the previous governor of the thermal zone after a new
+ * governor has failed to be bound.
+ */
+static void bind_previous_governor(struct thermal_zone_device *tz,
+				const char *failed_gov_name)
+{
+	if (tz->governor && tz->governor->bind_to_tz) {
+		if (tz->governor->bind_to_tz(tz)) {
+			dev_warn(&tz->device,
+				"governor %s failed to bind and the previous one (%s) failed to register again, thermal zone %s has no governor\n",
+				failed_gov_name, tz->governor->name, tz->type);
+			tz->governor = NULL;
+		}
+	}
+}
+
+/**
+ * thermal_set_governor() - Switch to another governor
+ * @tz:		a valid pointer to a struct thermal_zone_device
+ * @new_gov:	pointer to the new governor
+ *
+ * Change the governor of thermal zone @tz.
+ *
+ * Returns 0 on success, an error if the new governor's bind_to_tz() failed.
+ */
+static int thermal_set_governor(struct thermal_zone_device *tz,
+				struct thermal_governor *new_gov)
+{
+	int ret = 0;
+
+	if (tz->governor && tz->governor->unbind_from_tz)
+		tz->governor->unbind_from_tz(tz);
+
+	if (new_gov && new_gov->bind_to_tz) {
+		ret = new_gov->bind_to_tz(tz);
+		if (ret) {
+			bind_previous_governor(tz, new_gov->name);
+
+			return ret;
+		}
+	}
+
+	tz->governor = new_gov;
+
+	return ret;
+}
+
 int thermal_register_governor(struct thermal_governor *governor)
 {
 	int err;
@@ -104,8 +156,15 @@ int thermal_register_governor(struct thermal_governor *governor)
 
 		name = pos->tzp->governor_name;
 
-		if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
-			pos->governor = governor;
+		if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH)) {
+			int ret;
+
+			ret = thermal_set_governor(pos, governor);
+			if (ret)
+				dev_warn(&pos->device,
+					"Failed to set governor %s for thermal zone %s: %d\n",
+					governor->name, pos->type, ret);
+		}
 	}
 
 	mutex_unlock(&thermal_list_lock);
@@ -131,7 +190,7 @@ void thermal_unregister_governor(struct thermal_governor *governor)
 	list_for_each_entry(pos, &thermal_tz_list, node) {
 		if (!strnicmp(pos->governor->name, governor->name,
 						THERMAL_NAME_LENGTH))
-			pos->governor = NULL;
+			thermal_set_governor(pos, NULL);
 	}
 
 	mutex_unlock(&thermal_list_lock);
@@ -756,8 +815,9 @@ policy_store(struct device *dev, struct device_attribute *attr,
 	if (!gov)
 		goto exit;
 
-	tz->governor = gov;
-	ret = count;
+	ret = thermal_set_governor(tz, gov);
+	if (!ret)
+		ret = count;
 
 exit:
 	mutex_unlock(&thermal_governor_lock);
@@ -1452,6 +1512,7 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
 	int result;
 	int count;
 	int passive = 0;
+	struct thermal_governor *governor;
 
 	if (type && strlen(type) >= THERMAL_NAME_LENGTH)
 		return ERR_PTR(-EINVAL);
@@ -1542,9 +1603,15 @@ struct thermal_zone_device *thermal_zone_device_register(const char *type,
 	mutex_lock(&thermal_governor_lock);
 
 	if (tz->tzp)
-		tz->governor = __find_governor(tz->tzp->governor_name);
+		governor = __find_governor(tz->tzp->governor_name);
 	else
-		tz->governor = def_governor;
+		governor = def_governor;
+
+	result = thermal_set_governor(tz, governor);
+	if (result) {
+		mutex_unlock(&thermal_governor_lock);
+		goto unregister;
+	}
 
 	mutex_unlock(&thermal_governor_lock);
 
@@ -1634,7 +1701,7 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
 		device_remove_file(&tz->device, &dev_attr_mode);
 	device_remove_file(&tz->device, &dev_attr_policy);
 	remove_trip_attrs(tz);
-	tz->governor = NULL;
+	thermal_set_governor(tz, NULL);
 
 	thermal_remove_hwmon_sysfs(tz);
 	release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index 6fca46c82c4d..8babc0a8cce0 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -187,6 +187,7 @@ struct thermal_attr {
  * @ops:	operations this thermal_zone_device supports
  * @tzp:	thermal zone parameters
  * @governor:	pointer to the governor for this thermal zone
+ * @governor_data:	private pointer for governor data
  * @thermal_instances:	list of struct thermal_instance of this thermal zone
  * @idr:	struct idr to generate unique id for this zone's cooling devices
  * @lock:	lock to protect thermal_instances list
@@ -212,6 +213,7 @@ struct thermal_zone_device {
 	struct thermal_zone_device_ops *ops;
 	const struct thermal_zone_params *tzp;
 	struct thermal_governor *governor;
+	void *governor_data;
 	struct list_head thermal_instances;
 	struct idr idr;
 	struct mutex lock;
@@ -222,12 +224,19 @@ struct thermal_zone_device {
 /**
  * struct thermal_governor - structure that holds thermal governor information
  * @name:	name of the governor
+ * @bind_to_tz: callback called when binding to a thermal zone.  If it
+ *		returns 0, the governor is bound to the thermal zone,
+ *		otherwise it fails.
+ * @unbind_from_tz:	callback called when a governor is unbound from a
+ *			thermal zone.
  * @throttle:	callback called for every trip point even if temperature is
  *		below the trip point temperature
  * @governor_list:	node in thermal_governor_list (in thermal_core.c)
  */
 struct thermal_governor {
 	char name[THERMAL_NAME_LENGTH];
+	int (*bind_to_tz)(struct thermal_zone_device *tz);
+	void (*unbind_from_tz)(struct thermal_zone_device *tz);
 	int (*throttle)(struct thermal_zone_device *tz, int trip);
 	struct list_head	governor_list;
 };
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RFC PATCH v3 4/7] thermal: introduce the Power Actor API
  2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
                   ` (2 preceding siblings ...)
  2014-06-03 10:18 ` [RFC PATCH v3 3/7] thermal: let governors have private data for each thermal zone Javi Merino
@ 2014-06-03 10:18 ` Javi Merino
  2014-06-11 11:32   ` Eduardo Valentin
  2014-06-03 10:18 ` [RFC PATCH v3 5/7] thermal: add a basic cpu power actor Javi Merino
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel
  Cc: punit.agrawal, Javi Merino, Zhang Rui, Eduardo Valentin

This patch introduces the Power Actor API in the thermal framework.
With it, devices that can report their power consumption and control
it can be registered.  This base interface is meant to be used to
derive specific power actors, such as a cpu power actor.

Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Signed-off-by: Javi Merino <javi.merino@arm.com>
---
 Documentation/thermal/power_actor.txt     | 38 ++++++++++++++++++
 drivers/thermal/Kconfig                   |  3 ++
 drivers/thermal/Makefile                  |  2 +
 drivers/thermal/power_actor/Makefile      |  5 +++
 drivers/thermal/power_actor/power_actor.c | 64 +++++++++++++++++++++++++++++++
 drivers/thermal/power_actor/power_actor.h | 64 +++++++++++++++++++++++++++++++
 6 files changed, 176 insertions(+)
 create mode 100644 Documentation/thermal/power_actor.txt
 create mode 100644 drivers/thermal/power_actor/Makefile
 create mode 100644 drivers/thermal/power_actor/power_actor.c
 create mode 100644 drivers/thermal/power_actor/power_actor.h

diff --git a/Documentation/thermal/power_actor.txt b/Documentation/thermal/power_actor.txt
new file mode 100644
index 000000000000..5a61f32ec143
--- /dev/null
+++ b/Documentation/thermal/power_actor.txt
@@ -0,0 +1,38 @@
+
+Power Actor API
+===============
+
+The base power actor API is meant to be used to derive specific power
+actors, such as a cpu power actor.  When registering, they should call
+`power_actor_register()` with a unique `enum power_actor_types`.  When
+unregistering, the power actor should call `power_actor_unregister()`
+with the `struct power_actor *` received in the call to
+`power_actor_register()`.
+
+Callbacks
+---------
+
+1. u32 get_req_power(struct power_actor *actor)
+@actor: a valid `struct power_actor *` registered with
+        `power_actor_register()`
+
+`get_req_power()` returns the current requested power in milliwatts.
+
+2. u32 get_max_power(struct power_actor *actor)
+@actor: a valid `struct power_actor *` registered with
+        `power_actor_register()`
+
+`get_max_power()` returns the maximum power that the device could
+consume if it was fully utilized.  It's a function as some devices'
+maximum power consumption can change due to external factors such as
+temperature.
+
+3. int set_power(struct power_actor *actor, u32 power)
+@actor: a valid `struct power_actor *` registered with
+        `power_actor_register()`
+@power: power in milliwatts
+
+`set_power()` should configure the device to consume @power
+milliwatts.
+
+Returns 0 on success, -E* on error.
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 2d51912a6e40..47e2f15537ca 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -89,6 +89,9 @@ config THERMAL_GOV_USER_SPACE
 	help
 	  Enable this to let the user space manage the platform thermals.
 
+config THERMAL_POWER_ACTOR
+	bool
+
 config CPU_THERMAL
 	bool "generic cpu cooling support"
 	depends on CPU_FREQ
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 54e4ec9eb5df..878a02cab7d1 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -14,6 +14,8 @@ thermal_sys-$(CONFIG_THERMAL_GOV_FAIR_SHARE)	+= fair_share.o
 thermal_sys-$(CONFIG_THERMAL_GOV_STEP_WISE)	+= step_wise.o
 thermal_sys-$(CONFIG_THERMAL_GOV_USER_SPACE)	+= user_space.o
 
+obj-$(CONFIG_THERMAL_POWER_ACTOR) += power_actor/
+
 # cpufreq cooling
 thermal_sys-$(CONFIG_CPU_THERMAL)	+= cpu_cooling.o
 
diff --git a/drivers/thermal/power_actor/Makefile b/drivers/thermal/power_actor/Makefile
new file mode 100644
index 000000000000..46478f4928be
--- /dev/null
+++ b/drivers/thermal/power_actor/Makefile
@@ -0,0 +1,5 @@
+#
+# Makefile for the power actors
+#
+
+obj-y += power_actor.o
diff --git a/drivers/thermal/power_actor/power_actor.c b/drivers/thermal/power_actor/power_actor.c
new file mode 100644
index 000000000000..d891deb0e2a1
--- /dev/null
+++ b/drivers/thermal/power_actor/power_actor.c
@@ -0,0 +1,64 @@
+/*
+ * Basic interface for power actors
+ *
+ * Copyright (C) 2014 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed "as is" WITHOUT ANY WARRANTY of any
+ * kind, whether express or implied; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#define pr_fmt(fmt) "Power actor: " fmt
+
+#include <linux/err.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+
+#include "power_actor.h"
+
+LIST_HEAD(actor_list);
+
+/**
+ * power_actor_register - Register an actor in the power actor API
+ * @type:	actor type
+ * @ops:	struct power_actor_ops for this actor
+ * @privdata:	pointer to private data related to the actor
+ *
+ * Returns the struct power_actor * on success, ERR_PTR() on failure
+ */
+struct power_actor *power_actor_register(enum power_actor_types type,
+					struct power_actor_ops *ops,
+					void *privdata)
+{
+	struct power_actor *actor;
+
+	if (!ops->get_req_power || !ops->get_max_power || !ops->set_power)
+		return ERR_PTR(-EINVAL);
+
+	actor = kzalloc(sizeof(*actor), GFP_KERNEL);
+	if (!actor)
+		return ERR_PTR(-ENOMEM);
+
+	actor->type = type;
+	actor->ops = ops;
+	actor->data = privdata;
+
+	list_add(&actor->actor_node, &actor_list);
+
+	return actor;
+}
+
+/**
+ * power_actor_unregister - Unregister an actor
+ * @actor:	the actor to unregister
+ */
+void power_actor_unregister(struct power_actor *actor)
+{
+	list_del(&actor->actor_node);
+	kfree(actor);
+}
diff --git a/drivers/thermal/power_actor/power_actor.h b/drivers/thermal/power_actor/power_actor.h
new file mode 100644
index 000000000000..28098f43630b
--- /dev/null
+++ b/drivers/thermal/power_actor/power_actor.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2014 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __POWER_ACTOR_H__
+#define __POWER_ACTOR_H__
+
+#include <linux/list.h>
+
+#define MAX_NUM_ACTORS 8
+
+enum power_actor_types {
+};
+
+struct power_actor;
+
+/**
+ * struct power_actor_ops - callbacks for power actors
+ * @get_req_power:	return the current requested power in milliwatts
+ * @get_max_power:	return the max power that the device can currently
+ *			consume in milliwatts
+ * @set_power:		configure the device to consume a certain power in
+ *			milliwatts
+ */
+struct power_actor_ops {
+	u32 (*get_req_power)(struct power_actor *);
+	u32 (*get_max_power)(struct power_actor *);
+	int (*set_power)(struct power_actor *, u32);
+};
+
+/**
+ * struct power_actor - structure for a power actor
+ * @type:	the type of power actor.
+ * @ops:	callbacks for the power actor
+ * @data:	a private pointer for type-specific data
+ * @actor_node:	node in actor_list
+ */
+struct power_actor {
+	enum power_actor_types type;
+	struct power_actor_ops *ops;
+	void *data;
+	struct list_head actor_node;
+};
+
+struct power_actor *power_actor_register(enum power_actor_types type,
+					struct power_actor_ops *ops,
+					void *privdata);
+void power_actor_unregister(struct power_actor *actor);
+
+extern struct list_head actor_list;
+
+#endif /* __POWER_ACTOR_H__ */
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RFC PATCH v3 5/7] thermal: add a basic cpu power actor
  2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
                   ` (3 preceding siblings ...)
  2014-06-03 10:18 ` [RFC PATCH v3 4/7] thermal: introduce the Power Actor API Javi Merino
@ 2014-06-03 10:18 ` Javi Merino
  2014-06-11 12:05   ` Eduardo Valentin
  2014-06-03 10:18 ` [RFC PATCH v3 6/7] thermal: introduce the Power Allocator governor Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 7/7] thermal: add trace events to the power allocator governor Javi Merino
  6 siblings, 1 reply; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel
  Cc: punit.agrawal, Javi Merino, Zhang Rui, Eduardo Valentin

Introduce a power actor for cpus.  It has a basic power model to get
the current power utilization and uses cpufreq cooling devices to set
the desired power.  It uses the current frequency (as reported by
cpufreq) as well as load and OPPs for the power calculations.  The
cpus must have registered their OPPs in the OPP library.

Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Signed-off-by: Punit Agrawal <punit.agrawal@arm.com>
Signed-off-by: Javi Merino <javi.merino@arm.com>
---
 Documentation/thermal/power_actor.txt     | 126 +++++++
 drivers/thermal/Kconfig                   |   5 +
 drivers/thermal/power_actor/Kconfig       |   9 +
 drivers/thermal/power_actor/Makefile      |   2 +
 drivers/thermal/power_actor/cpu_actor.c   | 601 ++++++++++++++++++++++++++++++
 drivers/thermal/power_actor/power_actor.h |  41 ++
 6 files changed, 784 insertions(+)
 create mode 100644 drivers/thermal/power_actor/Kconfig
 create mode 100644 drivers/thermal/power_actor/cpu_actor.c

diff --git a/Documentation/thermal/power_actor.txt b/Documentation/thermal/power_actor.txt
index 5a61f32ec143..fd51760615bf 100644
--- a/Documentation/thermal/power_actor.txt
+++ b/Documentation/thermal/power_actor.txt
@@ -36,3 +36,129 @@ temperature.
 milliwatts.
 
 Returns 0 on success, -E* on error.
+
+CPU Power Actor API
+===================
+
+A simple power model for CPUs.  The current power is calculated as
+dynamic + (optionally) static power.  This power model requires that
+the operating-points of the CPUs are registered using the kernel's opp
+library and the `cpufreq_frequency_table` is assigned to the `struct
+device` of the cpu.  If you are using the `cpufreq-cpu0.c` driver then
+the `cpufreq_frequency_table` should already be assigned to the cpu
+device.
+
+The `tz` and `plat_static_func` parameters of
+`power_cpu_actor_register()` are optional.  If you don't provide them,
+only dynamic power will be considered.
+
+Dynamic power
+-------------
+
+The dynamic power consumption of a processor depends
+on many factors.  For a given processor implementation the primary
+factors are:
+
+- The time the processor spends running, consuming dynamic power, as
+  compared to the time in idle states where dynamic consumption is
+  negligible.  Herein we refer to this as 'utilisation'.
+- The voltage and frequency levels as a result of DVFS.  The DVFS
+  level is a dominant factor governing power consumption.
+- In running time the 'execution' behaviour (instruction types, memory
+  access patterns and so forth) causes, in most cases, a second order
+  variation.  In pathological cases this variation can be significant,
+  but typically it is of a much lesser impact than the factors above.
+
+A high level dynamic power consumption model may then be represented as:
+
+Pdyn = f(run) * Voltage^2 * Frequency * Utilisation
+
+f(run) here represents the described execution behaviour and its
+result has a units of Watts/Hz/Volt^2 (this often expressed in
+mW/MHz/uVolt^2)
+
+The detailed behaviour for f(run) could be modelled on-line.  However,
+in practice, such an on-line model has dependencies on a number of
+implementation specific processor support and characterisation
+factors.  Therefore, in initial implementation that contribution is
+represented as a constant coefficient.  This is a simplification
+consistent with the relative contribution to overall power variation.
+
+In this simplified representation our model becomes:
+
+Pdyn = Kd * Voltage^2 * Frequency * Utilisation
+
+Where Kd (capacitance) represents an indicative running time dynamic
+power coefficient in fundamental units of mW/MHz/uVolt^2
+
+Static Power
+------------
+
+Static leakage power consumption depends on a number of factors.  For a
+given circuit implementation the primary factors are:
+
+- Time the circuit spends in each 'power state'
+- Temperature
+- Operating voltage
+- Process grade
+
+The time the circuit spends in each 'power state' for a given
+evaluation period at first order means OFF or ON.  However,
+'retention' states can also be supported that reduce power during
+inactive periods without loss of context.
+
+Note: The visibility of state entries to the OS can vary, according to
+platform specifics, and this can then impact the accuracy of a model
+based on OS state information alone.  It might be possible in some
+cases to extract more accurate information from system resources.
+
+The temperature, operating voltage and process 'grade' (slow to fast)
+of the circuit are all significant factors in static leakage power
+consumption.  All of these have complex relationships to static power.
+
+Circuit implementation specific factors include the chosen silicon
+process as well as the type, number and size of transistors in both
+the logic gates and any RAM elements included.
+
+The static power consumption modelling must take into account the
+power managed regions that are implemented.  Taking the example of an
+ARM processor cluster, the modelling would take into account whether
+each CPU can be powered OFF separately or if only a single power
+region is implemented for the complete cluster.
+
+In one view, there are others, a static power consumption model can
+then start from a set of reference values for each power managed
+region (e.g. CPU, Cluster/L2) in each state (e.g. ON, OFF) at an
+arbitrary process grade, voltage and temperature point.  These values
+are then scaled for all of the following: the time in each state, the
+process grade, the current temperature and the operating
+voltage.  However, since both implementation specific and complex
+relationships dominate the estimate, the appropriate interface to the
+model from the cpu power actor is to provide a function callback that
+calculates the static power in this platform.  When registering the
+power cpu actor, pass the thermal zone closest to the cpu (to get the
+temperature) and a function pointer that follows the `get_static_t`
+prototype:
+
+    u32 plat_get_static(cpumask_t *cpumask, unsigned long voltage,
+				unsigned long temperature);
+
+with `cpumask` a cpumask of the cpus involved in the calculation,
+`voltage` the voltage at which they are opperating and `temperature`
+their current temperature.
+
+If both `tz` and `plat_static_func` are NULL when registering the
+power cpu actor, static power is considered to be negligible for this
+platform and only dynamic power is considered.
+
+The platform specific callback can then use any combination of tables
+and/or equations to permute the estimated value.  Process grade
+information is not passed to the model since access to such data, from
+on-chip measurement capability or manufacture time data, is platform
+specific.
+
+Note: the significance of static power for CPUs in comparison to
+dynamic power is highly dependent on implementation.  Given the
+potential complexity in implementation, the importance and accuracy of
+its inclusion when using cpu power actors should be assessed on a case by
+cases basis.
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 47e2f15537ca..1818c4fa60b8 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -92,6 +92,11 @@ config THERMAL_GOV_USER_SPACE
 config THERMAL_POWER_ACTOR
 	bool
 
+menu "Power actors"
+depends on THERMAL_POWER_ACTOR
+source "drivers/thermal/power_actor/Kconfig"
+endmenu
+
 config CPU_THERMAL
 	bool "generic cpu cooling support"
 	depends on CPU_FREQ
diff --git a/drivers/thermal/power_actor/Kconfig b/drivers/thermal/power_actor/Kconfig
new file mode 100644
index 000000000000..fa542ca99cdb
--- /dev/null
+++ b/drivers/thermal/power_actor/Kconfig
@@ -0,0 +1,9 @@
+#
+# Thermal power actor configuration
+#
+
+config THERMAL_POWER_ACTOR_CPU
+	bool
+	prompt "Simple power model for a CPU"
+	help
+	  A simple CPU power model
diff --git a/drivers/thermal/power_actor/Makefile b/drivers/thermal/power_actor/Makefile
index 46478f4928be..6f04b92997e6 100644
--- a/drivers/thermal/power_actor/Makefile
+++ b/drivers/thermal/power_actor/Makefile
@@ -3,3 +3,5 @@
 #
 
 obj-y += power_actor.o
+
+obj-$(CONFIG_THERMAL_POWER_ACTOR_CPU) += cpu_actor.o
diff --git a/drivers/thermal/power_actor/cpu_actor.c b/drivers/thermal/power_actor/cpu_actor.c
new file mode 100644
index 000000000000..6eac80d119a5
--- /dev/null
+++ b/drivers/thermal/power_actor/cpu_actor.c
@@ -0,0 +1,601 @@
+/*
+ * A basic cpu actor
+ *
+ * Copyright (C) 2014 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed "as is" WITHOUT ANY WARRANTY of any
+ * kind, whether express or implied; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#define pr_fmt(fmt) "CPU actor: " fmt
+
+#include <linux/cpu.h>
+#include <linux/cpufreq.h>
+#include <linux/cpumask.h>
+#include <linux/cpu_cooling.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/pm_opp.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+
+#include "power_actor.h"
+
+/**
+ * struct power_table - frequency to power conversion
+ * @frequency:	frequency in KHz
+ * @power:	power in mW
+ *
+ * This structure is built when the cooling device registers and helps
+ * in translating frequency to power and viceversa.
+ */
+struct power_table {
+	u32 frequency;
+	u32 power;
+};
+
+/**
+ * struct cpu_actor - information for each cpu actor
+ * @cpumask:	cpus covered by this actor
+ * @freq:	frequency in KHz of the cpus represented by the cooling device
+ * @last_load:	load measured by the latest call to cpu_get_req_power()
+ * @time_in_idle:	previous reading of the absolute time that this cpu was
+ *			idle
+ * @time_in_idle_timestamp: wall time of the last invocation of
+ *			    get_cpu_idle_time_us()
+ * @dyn_power_table:	array of struct power_table for frequency to power
+ *			conversion
+ * @dyn_power_table_entries: number of entries in the @dyn_power_table array
+ * @cdev:	cpufreq cooling device associated with this actor
+ * @tz:		thermal zone for the sensor closest to the cpus
+ * @plat_get_static_power:	callback to calculate the static power
+ */
+struct cpu_actor {
+	cpumask_t cpumask;
+	u32 freq;
+	u32 last_load;
+	u64 time_in_idle[NR_CPUS];
+	u64 time_in_idle_timestamp[NR_CPUS];
+	struct power_table *dyn_power_table;
+	int dyn_power_table_entries;
+	struct thermal_cooling_device *cdev;
+	struct thermal_zone_device *tz;
+	get_static_t plat_get_static_power;
+};
+
+static DEFINE_MUTEX(cpu_power_actor_lock);
+
+static unsigned int cpu_power_actors_registered;
+
+static u32 cpu_freq_to_power(struct cpu_actor *cpu_actor, u32 freq)
+{
+	int i;
+	struct power_table *pt = cpu_actor->dyn_power_table;
+
+	for (i = 0; i < cpu_actor->dyn_power_table_entries - 1; i++)
+		if (freq <= pt[i].frequency)
+			break;
+
+	return pt[i].power;
+}
+
+static u32 cpu_power_to_freq(struct cpu_actor *cpu_actor, u32 power)
+{
+	int i;
+	struct power_table *pt = cpu_actor->dyn_power_table;
+
+	for (i = 0; i < cpu_actor->dyn_power_table_entries - 1; i++)
+		if (power <= pt[i].power)
+			break;
+
+	return pt[i].frequency;
+}
+
+/**
+ * get_load - get load for a cpu since last updated
+ * @cpu_actor: struct cpu_actor for this actor
+ * @cpu: cpu number
+ *
+ * Return the average load of cpu @cpu in percentage since this
+ * function was last called.
+ */
+static u32 get_load(struct cpu_actor *cpu_actor, int cpu)
+{
+	u32 load;
+	u64 now, now_idle, delta_time, delta_idle;
+
+	now_idle = get_cpu_idle_time(cpu, &now, 0);
+	delta_idle = now_idle - cpu_actor->time_in_idle[cpu];
+	delta_time = now - cpu_actor->time_in_idle_timestamp[cpu];
+
+	if (delta_time <= delta_idle)
+		load = 0;
+	else
+		load = div64_u64(100 * (delta_time - delta_idle), delta_time);
+
+	cpu_actor->time_in_idle[cpu] = now_idle;
+	cpu_actor->time_in_idle_timestamp[cpu] = now;
+
+	return load;
+}
+
+/**
+ * __get_static_power() - calculate the static power consumed by the cpus
+ * @cpu_actor:	&struct cpu_actor for this cpu
+ * @freq:	frequency in KHz
+ *
+ * Calculate the static power consumed by the cpus described by
+ * @cpu_actor running at frequency @freq.  This function relies on a
+ * platform specific function that should have been provided when the
+ * actor was registered.  If it wasn't, the static power is assumed to
+ * be negligible.
+ *
+ * Return: The static power consumed by the cpus.  It returns 0 on
+ * error or if there is no plat_get_static_power().
+ */
+static u32 __get_static_power(struct cpu_actor *cpu_actor, unsigned long freq)
+{
+	int err;
+	struct device *cpu_dev;
+	struct dev_pm_opp *opp;
+	unsigned long voltage, temperature;
+	cpumask_t *cpumask = &cpu_actor->cpumask;
+	unsigned long freq_hz = freq * 1000;
+
+	if (!cpu_actor->plat_get_static_power)
+		return 0;
+
+	if (freq == 0)
+		return 0;
+
+	cpu_dev = get_cpu_device(cpumask_any(cpumask));
+
+	rcu_read_lock();
+
+	opp = dev_pm_opp_find_freq_exact(cpu_dev, freq_hz, true);
+	voltage = dev_pm_opp_get_voltage(opp);
+
+	rcu_read_unlock();
+
+	if (voltage == 0) {
+		dev_warn_ratelimited(cpu_dev,
+				"Failed to get voltage for frequency %lu: %ld\n",
+				freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
+		return 0;
+	}
+
+	err = thermal_zone_get_temp(cpu_actor->tz, &temperature);
+	if (err) {
+		dev_warn(&cpu_actor->tz->device, "Unable to read temperature: %d\n",
+			err);
+		return 0;
+	}
+
+	return cpu_actor->plat_get_static_power(cpumask, voltage, temperature);
+}
+
+static u32 get_static_power(struct cpu_actor *cpu_actor)
+{
+	return __get_static_power(cpu_actor, cpu_actor->freq);
+}
+
+/**
+ * get_dynamic_power - calculate the dynamic power
+ * @cpu_actor: cpu_actor pointer
+ *
+ * Return the dynamic power consumed by the cpus described by
+ * cpu_actor.
+ */
+static u32 get_dynamic_power(struct cpu_actor *cpu_actor)
+{
+	int cpu;
+	u32 power = 0, raw_cpu_power, total_load = 0;
+	raw_cpu_power = cpu_freq_to_power(cpu_actor, cpu_actor->freq);
+
+	for_each_cpu(cpu, &cpu_actor->cpumask) {
+		u32 load;
+
+		if (!cpu_online(cpu))
+			continue;
+
+		load = get_load(cpu_actor, cpu);
+		power += (raw_cpu_power * load) / 100;
+		total_load += load;
+	}
+
+	cpu_actor->last_load = total_load;
+
+	return power;
+}
+
+/**
+ * cpu_get_req_power - get the current power
+ * @actor:	power actor pointer
+ *
+ * Callback for the power actor to return the current power
+ * consumption in milliwatts.
+ */
+static u32 cpu_get_req_power(struct power_actor *actor)
+{
+	u32 static_power, dynamic_power;
+	struct cpu_actor *cpu_actor = actor->data;
+
+	static_power = get_static_power(cpu_actor);
+	dynamic_power = get_dynamic_power(cpu_actor);
+
+	return static_power + dynamic_power;
+}
+
+/**
+ * cpu_get_max_power() - get the maximum power that the cpu could currently consume
+ * @actor:	power actor pointer
+ *
+ * Callback for the power actor to return the maximum power
+ * consumption in milliwatts that the cpu could currently consume.
+ * The static power depends on temperature so the maximum power will
+ * vary over time.
+ */
+static u32 cpu_get_max_power(struct power_actor *actor)
+{
+	u32 max_static_power, max_dyn_power;
+	cpumask_t *cpumask;
+	unsigned int max_freq, last_entry, num_cpus;
+	struct cpu_actor *cpu_actor = actor->data;
+
+	cpumask = &cpu_actor->cpumask;
+	max_freq = cpufreq_quick_get_max(cpumask_any(cpumask));
+	max_static_power = __get_static_power(cpu_actor, max_freq);
+
+	last_entry = cpu_actor->dyn_power_table_entries - 1;
+	num_cpus = cpumask_weight(cpumask);
+	max_dyn_power = cpu_actor->dyn_power_table[last_entry].power * num_cpus;
+
+	return max_static_power + max_dyn_power;
+}
+
+/**
+ * cpu_set_power - set cpufreq cooling device to consume a certain power
+ * @actor: power actor pointer
+ * @power: the power in milliwatts that should be set
+ *
+ * Callback for the power actor to configure the power consumption of
+ * the CPU to be @power milliwatts at most.  This function assumes
+ * that the load will remain constant.  The power is translated into a
+ * cooling state that the cpu cooling device then sets.
+ *
+ * Returns 0 on success, -EINVAL if it couldn't convert the frequency
+ * to a cpufreq cooling device state.
+ */
+static int cpu_set_power(struct power_actor *actor, u32 power)
+{
+	unsigned int cpu, freq;
+	unsigned long cdev_state;
+	u32 dyn_power, normalised_power, last_load;
+	struct thermal_cooling_device *cdev;
+	struct cpu_actor *cpu_actor = actor->data;
+
+	cdev = cpu_actor->cdev;
+
+	dyn_power = power - get_static_power(cpu_actor);
+	cpu = cpumask_any(&cpu_actor->cpumask);
+	last_load = cpu_actor->last_load ? cpu_actor->last_load : 1;
+	normalised_power = (dyn_power * 100) / last_load;
+	freq = cpu_power_to_freq(cpu_actor, normalised_power);
+
+	cdev_state = cpufreq_cooling_get_level(cpu, freq);
+	if (cdev_state == THERMAL_CSTATE_INVALID) {
+		pr_err("Failed to convert %dKHz for cpu %d into a cdev state\n",
+			freq, cpu);
+		return -EINVAL;
+	}
+
+	return cdev->ops->set_cur_state(cdev, cdev_state);
+}
+
+static struct power_actor_ops cpu_actor_ops = {
+	.get_req_power = cpu_get_req_power,
+	.get_max_power = cpu_get_max_power,
+	.set_power = cpu_set_power,
+};
+
+/**
+ * power_cpu_actor_add_static() - add static information to an existing cpu actor
+ * @cpu:	a cpu represented by the cpu power actor
+ * @tz:		pointer to the closest thermal zone to the cpu
+ * @plat_static_func:	function to calculate the static power consumed by these
+ *			cpus
+ *
+ * A function to add static information to an actor after it has
+ * registered.  In order to calculate the static power, we need the to
+ * know the temperature of the cpu, which we get from its closest
+ * thermal zone.  It is possible that when registering the
+ * power_cpu_actor, the thermal zone hasn't been registered yet.  This
+ * function allows you to add that information when the thermal zone
+ * is registered.  @cpu should be one of the cpus represented by the
+ * cpu actor you want to modify
+ *
+ * Return: 0 on success.  -EINVAL if @tz or @plat_static_func are
+ * NULL.  -ENOENT if there's no cpu power actor for @cpu.
+ */
+int power_cpu_actor_add_static(unsigned int cpu, struct thermal_zone_device *tz,
+			get_static_t plat_static_func)
+{
+	struct power_actor *actor;
+
+	if (!plat_static_func || !tz)
+		return -EINVAL;
+
+	list_for_each_entry(actor, &actor_list, actor_node) {
+		struct cpu_actor *cpu_actor;
+
+		if (actor->type != POWER_ACTOR_CPU)
+			continue;
+
+		cpu_actor = actor->data;
+
+		if (cpumask_test_cpu(cpu, &cpu_actor->cpumask)) {
+			cpu_actor->tz = tz;
+			cpu_actor->plat_get_static_power = plat_static_func;
+			return 0;
+		}
+	}
+
+	return -ENOENT;
+}
+
+/**
+ * cpufreq_frequency_change - notifier callback for cpufreq frequency changes
+ * @nb:		struct notifier_block * with callback info
+ * @event:	value showing cpufreq event for which this function invoked
+ * @data:	callback-specific data
+ *
+ * Callback to get notifications of frequency changes.  In the
+ * CPUFREQ_POSTCHANGE @event we store the new frequency so that
+ * cpufreq_get_cur() knows the current frequency and can convert it
+ * into power.
+ */
+static int cpufreq_frequency_change(struct notifier_block *nb,
+				unsigned long event, void *data)
+{
+	struct power_actor *actor;
+	struct cpufreq_freqs *freqs = data;
+
+	/* Only update frequency on postchange */
+	if (event != CPUFREQ_POSTCHANGE)
+		return NOTIFY_DONE;
+
+	list_for_each_entry(actor, &actor_list, actor_node) {
+		struct cpu_actor *cpu_actor;
+
+		if (actor->type != POWER_ACTOR_CPU)
+			continue;
+
+		cpu_actor = actor->data;
+
+		if (cpumask_test_cpu(freqs->cpu, &cpu_actor->cpumask))
+			cpu_actor->freq = freqs->new;
+	}
+
+	return NOTIFY_OK;
+}
+
+struct notifier_block cpufreq_transition_notifier = {
+	.notifier_call = cpufreq_frequency_change,
+};
+
+/**
+ * build_dyn_power_table - create a dynamic power to frequency table
+ * @cpu_actor:	the cpu_actor in which to store the table
+ * @capacitance: dynamic power coefficient for these cpus
+ *
+ * Build a dynamic power to frequency table for this cpu and store it
+ * in @cpu_actor.  This table will be used in cpu_power_to_freq() and
+ * cpu_freq_to_power() to convert between power and frequency
+ * efficiently.  Power is stored in mW, frequency in KHz.  The
+ * resulting table is in ascending order.
+ *
+ * Returns 0 on success, -E* on error.
+ */
+static int build_dyn_power_table(struct cpu_actor *cpu_actor, u32 capacitance)
+{
+	struct power_table *power_table;
+	struct dev_pm_opp *opp;
+	struct device *dev = NULL;
+	int num_opps, cpu, i, ret = 0;
+	unsigned long freq;
+
+	num_opps = 0;
+
+	rcu_read_lock();
+
+	for_each_cpu(cpu, &cpu_actor->cpumask) {
+		dev = get_cpu_device(cpu);
+		if (!dev)
+			continue;
+
+		num_opps = dev_pm_opp_get_opp_count(dev);
+		if (num_opps > 0) {
+			break;
+		} else if (num_opps < 0) {
+			ret = num_opps;
+			goto unlock;
+		}
+	}
+
+	if (num_opps == 0) {
+		ret = -EINVAL;
+		goto unlock;
+	}
+
+	power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
+
+	i = 0;
+	for (freq = 0;
+	     opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
+	     freq++) {
+		u32 freq_mhz, voltage_mv;
+		u64 power;
+
+		freq_mhz = freq / 1000000;
+		voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
+
+		/*
+		 * Do the multiplication with MHz and millivolt so as
+		 * to not overflow.
+		 */
+		power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
+		do_div(power, 1000000000);
+
+		/* frequency is stored in power_table in KHz */
+		power_table[i].frequency = freq / 1000;
+		power_table[i].power = power;
+
+		i++;
+	}
+
+	if (i == 0) {
+		ret = PTR_ERR(opp);
+		goto unlock;
+	}
+
+	cpu_actor->dyn_power_table = power_table;
+	cpu_actor->dyn_power_table_entries = i;
+
+unlock:
+	rcu_read_unlock();
+	return ret;
+}
+
+/**
+ * power_cpu_actor_register - register a cpu_actor within the power actor API
+ * @cpumask:	cpumask of cpus covered by this power_actor
+ * @tz:		thermal zone closest to the cpus (optional)
+ * @capacitance: dynamic power coefficient for these cpus
+ * @plat_static_func:	function to calculate the static power consumed by these
+ *			cpus (optional)
+ *
+ * Create a cpufreq cooling device for the cpus in @cpumask and
+ * register it with the power actor API using a simple cpu power
+ * model.  The cpus must have registered their OPPs in the OPP
+ * library.
+ *
+ * An optional @plat_static_func may be provided to calculate the
+ * static power consumed by these cpus.  If the platform's static
+ * power consumption is unknown or negligible, make it NULL.  If
+ * @plat_static_fun is specified, @tz needs to be specified as well.
+ * @tz should be a pointer to the thermal_zone_device whose sensor is
+ * closer to the cpus in @cpumask.
+ *
+ * Return the power_actor created on success or the corresponding
+ * ERR_PTR() on failure.  This actor should be freed with
+ * power_cpu_actor_unregister() when it's no longer needed.
+ */
+struct power_actor *
+power_cpu_actor_register(cpumask_t *cpumask,
+			struct thermal_zone_device *tz,
+			u32 capacitance,
+			get_static_t plat_static_func)
+{
+	int ret;
+	struct thermal_cooling_device *cdev;
+	struct device *cpu_dev;
+	struct power_actor *actor, *err_ret;
+	struct cpu_actor *cpu_actor;
+
+	if (plat_static_func && !tz) {
+		pr_warn("plat_static_func provided without a thermal zone, ignoring it\n");
+		plat_static_func = NULL;
+	}
+
+	cpu_dev = get_cpu_device(cpumask_any(cpumask));
+	if (!cpu_dev || !cpu_dev->of_node)
+		cdev = cpufreq_cooling_register(cpumask);
+	else
+		cdev = of_cpufreq_cooling_register(cpu_dev->of_node, cpumask);
+
+	if (!cdev)
+		return ERR_PTR(PTR_ERR(cdev));
+
+	cpu_actor = kzalloc(sizeof(*cpu_actor), GFP_KERNEL);
+	if (!cpu_actor) {
+		err_ret = ERR_PTR(-ENOMEM);
+		goto cdev_unregister;
+	}
+
+	cpumask_copy(&cpu_actor->cpumask, cpumask);
+	cpu_actor->cdev = cdev;
+	cpu_actor->tz = tz;
+	cpu_actor->plat_get_static_power = plat_static_func;
+
+	ret = build_dyn_power_table(cpu_actor, capacitance);
+	if (ret) {
+		err_ret = ERR_PTR(ret);
+		goto kfree;
+	}
+
+	actor = power_actor_register(POWER_ACTOR_CPU, &cpu_actor_ops,
+				cpu_actor);
+	if (IS_ERR(actor)) {
+		err_ret = actor;
+		goto kfree;
+	}
+
+	mutex_lock(&cpu_power_actor_lock);
+
+	/*
+	 * You can't register multiple times the same notifier_block.
+	 * The first power actor registered is the only one that
+	 * registers the notifier.
+	 */
+	if (!cpu_power_actors_registered) {
+		ret = cpufreq_register_notifier(&cpufreq_transition_notifier,
+						CPUFREQ_TRANSITION_NOTIFIER);
+		if (ret) {
+			err_ret = ERR_PTR(ret);
+			mutex_unlock(&cpu_power_actor_lock);
+			goto power_actor_unregister;
+		}
+	}
+
+	cpu_power_actors_registered++;
+	mutex_unlock(&cpu_power_actor_lock);
+
+	return actor;
+
+power_actor_unregister:
+	power_actor_unregister(actor);
+kfree:
+	kfree(cpu_actor);
+cdev_unregister:
+	cpufreq_cooling_unregister(cdev);
+
+	return err_ret;
+}
+
+void power_cpu_actor_unregister(struct power_actor *actor)
+{
+	struct cpu_actor *cpu_actor = actor->data;
+
+	kfree(cpu_actor->dyn_power_table);
+
+	mutex_lock(&cpu_power_actor_lock);
+
+	cpu_power_actors_registered--;
+
+	if (!cpu_power_actors_registered)
+		cpufreq_unregister_notifier(&cpufreq_transition_notifier,
+					CPUFREQ_TRANSITION_NOTIFIER);
+
+	mutex_unlock(&cpu_power_actor_lock);
+
+	kfree(cpu_actor);
+	power_actor_unregister(actor);
+}
diff --git a/drivers/thermal/power_actor/power_actor.h b/drivers/thermal/power_actor/power_actor.h
index 28098f43630b..230317c284b2 100644
--- a/drivers/thermal/power_actor/power_actor.h
+++ b/drivers/thermal/power_actor/power_actor.h
@@ -17,11 +17,16 @@
 #ifndef __POWER_ACTOR_H__
 #define __POWER_ACTOR_H__
 
+#include <linux/cpumask.h>
+#include <linux/device.h>
+#include <linux/err.h>
 #include <linux/list.h>
+#include <linux/thermal.h>
 
 #define MAX_NUM_ACTORS 8
 
 enum power_actor_types {
+	POWER_ACTOR_CPU,
 };
 
 struct power_actor;
@@ -59,6 +64,42 @@ struct power_actor *power_actor_register(enum power_actor_types type,
 					void *privdata);
 void power_actor_unregister(struct power_actor *actor);
 
+typedef u32 (*get_static_t)(cpumask_t *cpumask,
+				unsigned long voltage,
+				unsigned long temperature);
+
+#ifdef CONFIG_THERMAL_POWER_ACTOR_CPU
+int power_cpu_actor_add_static(unsigned int cpu,
+			struct thermal_zone_device *tz,
+			get_static_t plat_static_func);
+struct power_actor *
+power_cpu_actor_register(cpumask_t *cpumask,
+			struct thermal_zone_device *tz,
+			u32 capacitance,
+			get_static_t plat_static_func);
+void power_cpu_actor_unregister(struct power_actor *actor);
+#else
+static inline
+int power_cpu_actor_add_static(unsigned int cpu,
+					struct thermal_zone_device *tz,
+					get_static_t plat_static_func)
+{
+	return -ENOSYS;
+}
+static inline
+struct power_actor *
+power_cpu_actor_register(cpumask_t *cpumask,
+			struct thermal_zone_device *tz,
+			u32 capacitance,
+			get_static_t plat_static_func)
+{
+	return ERR_PTR(-ENOSYS);
+}
+static inline void power_cpu_actor_unregister(struct power_actor *actor)
+{
+}
+#endif
+
 extern struct list_head actor_list;
 
 #endif /* __POWER_ACTOR_H__ */
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RFC PATCH v3 6/7] thermal: introduce the Power Allocator governor
  2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
                   ` (4 preceding siblings ...)
  2014-06-03 10:18 ` [RFC PATCH v3 5/7] thermal: add a basic cpu power actor Javi Merino
@ 2014-06-03 10:18 ` Javi Merino
  2014-06-03 10:18 ` [RFC PATCH v3 7/7] thermal: add trace events to the power allocator governor Javi Merino
  6 siblings, 0 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel
  Cc: punit.agrawal, Javi Merino, Zhang Rui, Eduardo Valentin

The power allocator governor is a thermal governor that controls system
and device power allocation to control temperature.  Conceptually, the
implementation divides the sustainable power of a thermal zone among
all the heat sources in that zone.

This governor relies on "power actors", entities that represent heat
sources.  They can report current and maximum power consumption and
can set a given maximum power consumption, usually via a cooling
device.

The governor uses a Proportional Integral Derivative (PID) controller
driven by the temperature of the thermal zone.  The output of the
controller is a power budget that is then allocated to each power
actor that can have bearing on the temperature we are trying to
control.  It decides how much power to give each cooling device based
on the performance they are requesting.  The PID controller ensures
that the total power budget does not exceed the control temperature.

Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Signed-off-by: Punit Agrawal <punit.agrawal@arm.com>
Signed-off-by: Javi Merino <javi.merino@arm.com>
---
 Documentation/thermal/power_allocator.txt |  42 +++
 drivers/thermal/Kconfig                   |  15 +
 drivers/thermal/Makefile                  |   1 +
 drivers/thermal/power_allocator.c         | 455 ++++++++++++++++++++++++++++++
 drivers/thermal/thermal_core.c            |   7 +-
 drivers/thermal/thermal_core.h            |   8 +
 include/linux/thermal.h                   |   8 +
 7 files changed, 535 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/thermal/power_allocator.txt
 create mode 100644 drivers/thermal/power_allocator.c

diff --git a/Documentation/thermal/power_allocator.txt b/Documentation/thermal/power_allocator.txt
new file mode 100644
index 000000000000..cfa66933af86
--- /dev/null
+++ b/Documentation/thermal/power_allocator.txt
@@ -0,0 +1,42 @@
+
+Integration of the power_allocator governor in a platform
+=========================================================
+
+Registering thermal_zone_device
+-------------------------------
+
+An estimate of the sustainable dissipatable power (in mW) should be
+provided while registering the thermal zone.  This is the maximum
+sustained power for allocation at the desired maximum temperature.
+This number can vary for different conditions, but the closed-loop of
+the controller should take care of those variations, the
+`sustainable_power` should be an estimation of it.  Register your
+thermal zone with `thermal_zone_params` that have a
+`sustainable_power`.  If you weren't passing any
+`thermal_zone_params`, then something like this will do:
+
+	static const struct thermal_zone_params tz_params = {
+		.sustainable_power = 3500,
+	};
+
+and then pass `tz_params` as the 5th parameter to
+`thermal_zone_device_register()`
+
+Trip points
+-----------
+
+The governor requires the following two trip points:
+
+1.  "switch on" trip point: temperature above which the governor
+    control loop starts operating
+2.  "desired temperature" trip point: it should be higher than the
+    "switch on" trip point. It is the target temperature the governor
+    is controlling for.
+
+The trip points can be either active or passive.
+
+Power actors
+------------
+
+Devices controlled by this governor must be registered with the power
+actor API.  Read `power_actor.txt` for more information about them.
diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
index 1818c4fa60b8..e5b338a7cab9 100644
--- a/drivers/thermal/Kconfig
+++ b/drivers/thermal/Kconfig
@@ -71,6 +71,14 @@ config THERMAL_DEFAULT_GOV_USER_SPACE
 	  Select this if you want to let the user space manage the
 	  platform thermals.
 
+config THERMAL_DEFAULT_GOV_POWER_ALLOCATOR
+	bool "power_allocator"
+	select THERMAL_GOV_POWER_ALLOCATOR
+	help
+	  Select this if you want to control temperature based on
+	  system and device power allocation. This governor relies on
+	  power actors to operate.
+
 endchoice
 
 config THERMAL_GOV_FAIR_SHARE
@@ -89,6 +97,13 @@ config THERMAL_GOV_USER_SPACE
 	help
 	  Enable this to let the user space manage the platform thermals.
 
+config THERMAL_GOV_POWER_ALLOCATOR
+	bool "Power allocator thermal governor"
+	select THERMAL_POWER_ACTOR
+	help
+	  Enable this to manage platform thermals by dynamically
+	  allocating and limiting power to devices.
+
 config THERMAL_POWER_ACTOR
 	bool
 
diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
index 878a02cab7d1..c5b47f058675 100644
--- a/drivers/thermal/Makefile
+++ b/drivers/thermal/Makefile
@@ -13,6 +13,7 @@ thermal_sys-$(CONFIG_THERMAL_OF)		+= of-thermal.o
 thermal_sys-$(CONFIG_THERMAL_GOV_FAIR_SHARE)	+= fair_share.o
 thermal_sys-$(CONFIG_THERMAL_GOV_STEP_WISE)	+= step_wise.o
 thermal_sys-$(CONFIG_THERMAL_GOV_USER_SPACE)	+= user_space.o
+thermal_sys-$(CONFIG_THERMAL_GOV_POWER_ALLOCATOR)	+= power_allocator.o
 
 obj-$(CONFIG_THERMAL_POWER_ACTOR) += power_actor/
 
diff --git a/drivers/thermal/power_allocator.c b/drivers/thermal/power_allocator.c
new file mode 100644
index 000000000000..9c7e7f212eb6
--- /dev/null
+++ b/drivers/thermal/power_allocator.c
@@ -0,0 +1,455 @@
+/*
+ * A power allocator to manage temperature
+ *
+ * Copyright (C) 2014 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed "as is" WITHOUT ANY WARRANTY of any
+ * kind, whether express or implied; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#define pr_fmt(fmt) "Power allocator: " fmt
+
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/thermal.h>
+
+#include "power_actor/power_actor.h"
+#include "thermal_core.h"
+
+#define FRAC_BITS 8
+#define int_to_frac(x) ((x) << FRAC_BITS)
+#define frac_to_int(x) ((x) >> FRAC_BITS)
+
+/**
+ * mul_frac - multiply two fixed-point numbers
+ * @x:	first multiplicand
+ * @y:	second multiplicand
+ *
+ * Returns the result of multiplying two fixed-point numbers.  The
+ * result is also a fixed-point number.
+ */
+static inline s64 mul_frac(s64 x, s64 y)
+{
+	return (x * y) >> FRAC_BITS;
+}
+
+enum power_allocator_trip_levels {
+	TRIP_SWITCH_ON = 0,	/* Switch on PID controller */
+	TRIP_MAX_DESIRED_TEMPERATURE, /* Temperature we are controlling for */
+};
+
+/**
+ * struct power_allocator_params - parameters for the power allocator governor
+ * @k_po:	Proportional parameter of the PID controller when overshooting
+ *		(i.e., when temperature is below the target)
+ * @k_pi:	Proportional parameter of the PID controller when undershooting
+ * @k_i:	Integral parameter of the PID controller
+ * @k_d:	Derivative parameter of the PID controller
+ * @integral_cutoff:	threshold below which the error is no longer accumulated
+			in the PID controller
+ * @err_integral:	accumulated error in the PID controller.
+ * @prev_err:	error in the previous iteration of the PID controller.
+ *		Used to calculate the derivative term.
+ */
+struct power_allocator_params {
+	s32 k_po;
+	s32 k_pu;
+	s32 k_i;
+	s32 k_d;
+	s32 integral_cutoff;
+	s32 err_integral;
+	s32 prev_err;
+};
+
+/**
+ * pid_controller() - PID controller
+ * @tz:	thermal zone we are operating in
+ * @control_temp:	The target temperature
+ * @max_allocatable_power:	maximum allocatable power for this thermal zone
+ *
+ * This PID controller increases the available power budget so that the
+ * temperature of the thermal zone gets as close as possible to
+ * @control_temp and limits the power if it exceeds it.  k_po is the
+ * proportional term when we are overshooting, k_pu is the
+ * proportional term when we are undershooting.  integral_cutoff is a
+ * threshold below which we stop accumulating the error.  The
+ * accumulated error is only valid if the requested power will make
+ * the system warmer.  If the system is mostly idle, there's no point
+ * in accumulating positive error.
+ *
+ * It returns the power budget for the next period.
+ */
+static u32 pid_controller(struct thermal_zone_device *tz,
+			unsigned long current_temp, unsigned long control_temp,
+			u32 max_allocatable_power)
+{
+	s64 p, i, d, power_range;
+	s32 err;
+	struct power_allocator_params *params = tz->governor_data;
+
+	err = ((s32)control_temp - (s32)current_temp) / 1000;
+	err = int_to_frac(err);
+
+	/* Calculate the proportional term */
+	p = mul_frac(err < 0 ? params->k_po : params->k_pu, err);
+
+	/*
+	 * Calculate the integral term
+	 *
+	 * if the error s less than cut off allow integration (but
+	 * the integral is limited to max power)
+	 */
+	i = mul_frac(params->k_i, params->err_integral);
+
+	if (err < int_to_frac(params->integral_cutoff)) {
+		s64 tmpi = mul_frac(params->k_i, err);
+		tmpi += i;
+		if (tmpi <= int_to_frac(max_allocatable_power)) {
+			i = tmpi;
+			params->err_integral += err;
+		}
+	}
+
+	/* Calculate the derivative term
+	 *
+	 * We do err - prev_err, so with a positive k_d, a decreasing
+	 * error (i.e. driving closer to the line) results in less
+	 * power being applied, slowing down the controller)
+	 */
+	d = mul_frac(params->k_d, err - params->prev_err);
+	params->prev_err = err;
+
+	power_range = p + i + d;
+
+	/* feed-forward the known sustainable dissipatable power */
+	power_range = tz->tzp->sustainable_power + frac_to_int(power_range);
+
+	return clamp(power_range, (s64)0, (s64)max_allocatable_power);
+}
+
+/**
+ * divvy_up_power - divvy the allocated power between the actors
+ * @req_power:	each actor's requested power
+ * @max_power:	each actor's maximum available power
+ * @num_actors:	size of the @req_power, @max_power and @granted_power's array
+ * @total_req_power: sum of @req_power
+ * @power_range:	total allocated power
+ * @granted_power:	ouput array: each actor's granted power
+ *
+ * This function divides the total allocated power (@power_range)
+ * fairly between the actors.  It first tries to give each actor a
+ * share of the @power_range according to how much power it requested
+ * compared to the rest of the actors.  For example, if only one actor
+ * requests power, then it receives all the @power_range.  If
+ * three actors each requests 1mW, each receives a third of the
+ * @power_range.
+ *
+ * If any actor received more than their maximum power, then that
+ * surplus is re-divvied among the actors based on how far they are
+ * from their respective maximums.
+ *
+ * Granted power for each actor is written to @granted_power, which
+ * should've been allocated by the calling function.
+ */
+static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
+			u32 total_req_power, u32 power_range,
+			u32 *granted_power)
+{
+	u32 extra_power, capped_extra_power, extra_actor_power[num_actors];
+	int i;
+
+	if (!total_req_power) {
+		/*
+		 * Nobody requested anything, so just give everybody
+		 * the maximum power
+		 */
+		for (i = 0; i < num_actors; i++)
+			granted_power[i] = max_power[i];
+
+		return;
+	}
+
+	capped_extra_power = 0;
+	extra_power = 0;
+	for (i = 0; i < num_actors; i++) {
+		u64 req_range = req_power[i] * power_range;
+
+		granted_power[i] = div_u64(req_range, total_req_power);
+
+		if (granted_power[i] > max_power[i]) {
+			extra_power += granted_power[i] - max_power[i];
+			granted_power[i] = max_power[i];
+		}
+
+		extra_actor_power[i] = max_power[i] - granted_power[i];
+		capped_extra_power += extra_actor_power[i];
+	}
+
+	if (!extra_power)
+		return;
+
+	/*
+	 * Re-divvy the reclaimed extra among actors based on
+	 * how far they are from the max
+	 */
+	extra_power = min(extra_power, capped_extra_power);
+	if (capped_extra_power > 0)
+		for (i = 0; i < num_actors; i++)
+			granted_power[i] += (extra_actor_power[i] *
+					extra_power) / capped_extra_power;
+}
+
+static int allocate_power(struct thermal_zone_device *tz,
+			unsigned long current_temp, unsigned long control_temp)
+{
+	struct power_actor *actor;
+	u32 *req_power, *max_power, *granted_power;
+	u32 total_req_power, max_allocatable_power;
+	u32 power_range;
+	int i, num_actors, ret = 0;
+
+	mutex_lock(&tz->lock);
+
+	num_actors = 0;
+	list_for_each_entry(actor, &actor_list, actor_node)
+		num_actors++;
+
+	req_power = devm_kcalloc(&tz->device, num_actors, sizeof(*req_power),
+				GFP_KERNEL);
+	if (!req_power) {
+		ret = -ENOMEM;
+		goto unlock;
+	}
+
+	max_power = devm_kcalloc(&tz->device, num_actors, sizeof(*max_power),
+				GFP_KERNEL);
+	if (!max_power) {
+		ret = -ENOMEM;
+		goto free_req_power;
+	}
+
+	granted_power = devm_kcalloc(&tz->device, num_actors,
+				sizeof(*granted_power), GFP_KERNEL);
+	if (!granted_power) {
+		ret = -ENOMEM;
+		goto free_max_power;
+	}
+
+	i = 0;
+	total_req_power = 0;
+	max_allocatable_power = 0;
+
+	list_for_each_entry(actor, &actor_list, actor_node) {
+		req_power[i] = actor->ops->get_req_power(actor);
+		total_req_power += req_power[i];
+
+		max_power[i] = actor->ops->get_max_power(actor);
+		max_allocatable_power += max_power[i];
+
+		i++;
+	}
+
+	power_range = pid_controller(tz, current_temp, control_temp,
+				max_allocatable_power);
+
+	divvy_up_power(req_power, max_power, num_actors, total_req_power,
+		power_range, granted_power);
+
+	i = 0;
+	list_for_each_entry(actor, &actor_list, actor_node) {
+		actor->ops->set_power(actor, granted_power[i]);
+		i++;
+	}
+
+	devm_kfree(&tz->device, granted_power);
+free_max_power:
+	devm_kfree(&tz->device, max_power);
+free_req_power:
+	devm_kfree(&tz->device, req_power);
+unlock:
+	mutex_unlock(&tz->lock);
+
+	return ret;
+}
+
+static int check_trips(struct thermal_zone_device *tz)
+{
+	int ret;
+	enum thermal_trip_type type;
+
+	if (tz->trips < 2)
+		return -EINVAL;
+
+	ret = tz->ops->get_trip_type(tz, TRIP_SWITCH_ON, &type);
+	if (ret)
+		return ret;
+
+	if ((type != THERMAL_TRIP_PASSIVE) && (type != THERMAL_TRIP_ACTIVE))
+		return -EINVAL;
+
+	ret = tz->ops->get_trip_type(tz, TRIP_MAX_DESIRED_TEMPERATURE, &type);
+	if (ret)
+		return ret;
+
+	if ((type != THERMAL_TRIP_PASSIVE) && (type != THERMAL_TRIP_ACTIVE))
+		return -EINVAL;
+
+	return ret;
+}
+
+static void reset_pid_controller(struct power_allocator_params *params)
+{
+	params->err_integral = 0;
+	params->prev_err = 0;
+}
+
+static void allow_maximum_power(void)
+{
+	struct power_actor *actor;
+
+	list_for_each_entry(actor, &actor_list, actor_node) {
+		u32 max_power = actor->ops->get_max_power(actor);
+
+		actor->ops->set_power(actor, max_power);
+	}
+}
+
+/**
+ * power_allocator_bind - bind the power_allocator governor to a thermal zone
+ * @tz:	thermal zone to bind it to
+ *
+ * Check that the thermal zone is valid for this governor: has two
+ * thermal trips.  If so, initialize the PID controller parameters and
+ * bind it to the thermal zone.
+ *
+ * Returns 0 on success, -EINVAL if the trips were invalid or -ENOMEM
+ * if we ran out of memory.
+ */
+static int power_allocator_bind(struct thermal_zone_device *tz)
+{
+	int ret;
+	struct power_allocator_params *params;
+	unsigned long switch_on_temp, control_temp;
+	u32 temperature_threshold;
+
+	ret = check_trips(tz);
+	if (ret) {
+		dev_err(&tz->device,
+			"thermal zone %s has the wrong number of trips for this governor\n",
+			tz->type);
+		return ret;
+	}
+
+	if (!tz->tzp || !tz->tzp->sustainable_power) {
+		dev_err(&tz->device,
+			"power_allocator: missing sustainable_power\n");
+		return -EINVAL;
+	}
+
+	params = devm_kzalloc(&tz->device, sizeof(*params), GFP_KERNEL);
+	if (!params)
+		return -ENOMEM;
+
+	ret = tz->ops->get_trip_temp(tz, TRIP_SWITCH_ON, &switch_on_temp);
+	if (ret)
+		goto free;
+
+	ret = tz->ops->get_trip_temp(tz, TRIP_MAX_DESIRED_TEMPERATURE,
+				&control_temp);
+	if (ret)
+		goto free;
+
+	temperature_threshold = (control_temp - switch_on_temp) / 1000;
+
+	params->k_po = int_to_frac(tz->tzp->sustainable_power) /
+		temperature_threshold;
+	params->k_pu = int_to_frac(2 * tz->tzp->sustainable_power) /
+		temperature_threshold;
+	params->k_i = int_to_frac(10);
+	params->k_d = int_to_frac(0);
+	params->integral_cutoff = 0;
+
+	reset_pid_controller(params);
+
+	tz->governor_data = params;
+
+	return 0;
+
+free:
+	devm_kfree(&tz->device, params);
+	return ret;
+}
+
+static void power_allocator_unbind(struct thermal_zone_device *tz)
+{
+	dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
+	devm_kfree(&tz->device, tz->governor_data);
+	tz->governor_data = NULL;
+}
+
+static int power_allocator_throttle(struct thermal_zone_device *tz, int trip)
+{
+	int ret;
+	unsigned long switch_on_temp, control_temp, current_temp;
+	struct power_allocator_params *params = tz->governor_data;
+
+	/*
+	 * We get called for every trip point but we only need to do
+	 * our calculations once
+	 */
+	if (trip != TRIP_MAX_DESIRED_TEMPERATURE)
+		return 0;
+
+	ret = thermal_zone_get_temp(tz, &current_temp);
+	if (ret) {
+		dev_warn(&tz->device, "Failed to get temperature: %d\n", ret);
+		return ret;
+	}
+
+	ret = tz->ops->get_trip_temp(tz, TRIP_SWITCH_ON, &switch_on_temp);
+	if (ret) {
+		dev_warn(&tz->device,
+			"Failed to get switch on temperature: %d\n", ret);
+		return ret;
+	}
+
+	if (current_temp < switch_on_temp) {
+		reset_pid_controller(params);
+		allow_maximum_power();
+		return 0;
+	}
+
+	ret = tz->ops->get_trip_temp(tz, TRIP_MAX_DESIRED_TEMPERATURE,
+				&control_temp);
+	if (ret) {
+		dev_warn(&tz->device,
+			"Failed to get the maximum desired temperature: %d\n",
+			ret);
+		return ret;
+	}
+
+	return allocate_power(tz, current_temp, control_temp);
+}
+
+static struct thermal_governor thermal_gov_power_allocator = {
+	.name		= "power_allocator",
+	.bind_to_tz	= power_allocator_bind,
+	.unbind_from_tz	= power_allocator_unbind,
+	.throttle	= power_allocator_throttle,
+};
+
+int thermal_gov_power_allocator_register(void)
+{
+	return thermal_register_governor(&thermal_gov_power_allocator);
+}
+
+void thermal_gov_power_allocator_unregister(void)
+{
+	thermal_unregister_governor(&thermal_gov_power_allocator);
+}
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 1b13d8e0cfd1..17257376396b 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -1857,7 +1857,11 @@ static int __init thermal_register_governors(void)
 	if (result)
 		return result;
 
-	return thermal_gov_user_space_register();
+	result = thermal_gov_user_space_register();
+	if (result)
+		return result;
+
+	return thermal_gov_power_allocator_register();
 }
 
 static void thermal_unregister_governors(void)
@@ -1865,6 +1869,7 @@ static void thermal_unregister_governors(void)
 	thermal_gov_step_wise_unregister();
 	thermal_gov_fair_share_unregister();
 	thermal_gov_user_space_unregister();
+	thermal_gov_power_allocator_unregister();
 }
 
 static int __init thermal_init(void)
diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 3db339fb636f..b24cde2c71cc 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -77,6 +77,14 @@ static inline int thermal_gov_user_space_register(void) { return 0; }
 static inline void thermal_gov_user_space_unregister(void) {}
 #endif /* CONFIG_THERMAL_GOV_USER_SPACE */
 
+#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
+int thermal_gov_power_allocator_register(void);
+void thermal_gov_power_allocator_unregister(void);
+#else
+static inline int thermal_gov_power_allocator_register(void) { return 0; }
+static inline void thermal_gov_power_allocator_unregister(void) {}
+#endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
+
 /* device tree support */
 #ifdef CONFIG_THERMAL_OF
 int of_parse_thermal_zones(void);
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index 8babc0a8cce0..526b694767f6 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -57,6 +57,8 @@
 #define DEFAULT_THERMAL_GOVERNOR       "fair_share"
 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
 #define DEFAULT_THERMAL_GOVERNOR       "user_space"
+#elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
+#define DEFAULT_THERMAL_GOVERNOR       "power_allocator"
 #endif
 
 struct thermal_zone_device;
@@ -286,6 +288,12 @@ struct thermal_zone_params {
 
 	int num_tbps;	/* Number of tbp entries */
 	struct thermal_bind_params *tbp;
+
+	/*
+	 * Sustainable power (heat) that this thermal zone can dissipate in
+	 * mW
+	 */
+	u32 sustainable_power;
 };
 
 struct thermal_genl_event {
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RFC PATCH v3 7/7] thermal: add trace events to the power allocator governor
  2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
                   ` (5 preceding siblings ...)
  2014-06-03 10:18 ` [RFC PATCH v3 6/7] thermal: introduce the Power Allocator governor Javi Merino
@ 2014-06-03 10:18 ` Javi Merino
  6 siblings, 0 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-03 10:18 UTC (permalink / raw)
  To: linux-pm, linux-kernel
  Cc: punit.agrawal, Javi Merino, Zhang Rui, Eduardo Valentin,
	Steven Rostedt, Frederic Weisbecker, Ingo Molnar

Add trace events for the power allocator governor and the power actor
interface of the cpu cooling device.

Cc: Zhang Rui <rui.zhang@intel.com>
Cc: Eduardo Valentin <edubezval@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Signed-off-by: Javi Merino <javi.merino@arm.com>

---

trace-cmd needs the patched attached in
http://article.gmane.org/gmane.linux.kernel/1704423 for this to work.

 drivers/thermal/power_actor/cpu_actor.c |  5 +++++
 drivers/thermal/power_allocator.c       | 12 ++++++++++-
 include/trace/events/thermal.h          | 38 +++++++++++++++++++++++++++++++++
 include/trace/events/thermal_governor.h | 37 ++++++++++++++++++++++++++++++++
 4 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 include/trace/events/thermal.h
 create mode 100644 include/trace/events/thermal_governor.h

diff --git a/drivers/thermal/power_actor/cpu_actor.c b/drivers/thermal/power_actor/cpu_actor.c
index 6eac80d119a5..396689b8fd40 100644
--- a/drivers/thermal/power_actor/cpu_actor.c
+++ b/drivers/thermal/power_actor/cpu_actor.c
@@ -27,6 +27,9 @@
 #include <linux/printk.h>
 #include <linux/slab.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/thermal.h>
+
 #include "power_actor.h"
 
 /**
@@ -297,6 +300,8 @@ static int cpu_set_power(struct power_actor *actor, u32 power)
 		return -EINVAL;
 	}
 
+	trace_thermal_power_limit(&cpu_actor->cpumask, freq, cdev_state, power);
+
 	return cdev->ops->set_cur_state(cdev, cdev_state);
 }
 
diff --git a/drivers/thermal/power_allocator.c b/drivers/thermal/power_allocator.c
index 9c7e7f212eb6..1e57a810903d 100644
--- a/drivers/thermal/power_allocator.c
+++ b/drivers/thermal/power_allocator.c
@@ -19,6 +19,9 @@
 #include <linux/slab.h>
 #include <linux/thermal.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/thermal_governor.h>
+
 #include "power_actor/power_actor.h"
 #include "thermal_core.h"
 
@@ -130,7 +133,14 @@ static u32 pid_controller(struct thermal_zone_device *tz,
 	/* feed-forward the known sustainable dissipatable power */
 	power_range = tz->tzp->sustainable_power + frac_to_int(power_range);
 
-	return clamp(power_range, (s64)0, (s64)max_allocatable_power);
+	power_range = clamp(power_range, (s64)0, (s64)max_allocatable_power);
+
+	trace_thermal_power_allocator_pid(frac_to_int(err),
+					frac_to_int(params->err_integral),
+					frac_to_int(p), frac_to_int(i),
+					frac_to_int(d), power_range);
+
+	return power_range;
 }
 
 /**
diff --git a/include/trace/events/thermal.h b/include/trace/events/thermal.h
new file mode 100644
index 000000000000..ce326e5cfe90
--- /dev/null
+++ b/include/trace/events/thermal.h
@@ -0,0 +1,38 @@
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM thermal
+
+#if !defined(_TRACE_THERMAL_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_THERMAL_H
+
+#include <linux/tracepoint.h>
+
+TRACE_EVENT(thermal_power_limit,
+	TP_PROTO(const struct cpumask *cpus, unsigned int freq,
+		unsigned long cdev_state, u32 power),
+
+	TP_ARGS(cpus, freq, cdev_state, power),
+
+	TP_STRUCT__entry(
+		__bitmask(cpumask, num_possible_cpus())
+		__field(unsigned int,  freq      )
+		__field(unsigned long, cdev_state)
+		__field(u32,           power     )
+	),
+
+	TP_fast_assign(
+		__assign_bitmask(cpumask, cpumask_bits(cpus),
+				num_possible_cpus());
+		__entry->freq = freq;
+		__entry->cdev_state = cdev_state;
+		__entry->power = power;
+	),
+
+	TP_printk("cpus=%s freq=%u cdev_state=%lu power=%u",
+		__get_bitmask(cpumask), __entry->freq, __entry->cdev_state,
+		__entry->power)
+);
+
+#endif /* _TRACE_THERMAL_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
diff --git a/include/trace/events/thermal_governor.h b/include/trace/events/thermal_governor.h
new file mode 100644
index 000000000000..5e5b25a41a6b
--- /dev/null
+++ b/include/trace/events/thermal_governor.h
@@ -0,0 +1,37 @@
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM thermal_governor
+
+#if !defined(_TRACE_THERMAL_GOVERNOR_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_THERMAL_GOVERNOR_H
+
+#include <linux/tracepoint.h>
+
+TRACE_EVENT(thermal_power_allocator_pid,
+	TP_PROTO(s32 err, s32 err_integral, s64 p, s64 i, s64 d, s32 output),
+	TP_ARGS(err, err_integral, p, i, d, output),
+	TP_STRUCT__entry(
+		__field(s32, err         )
+		__field(s32, err_integral)
+		__field(s64, p           )
+		__field(s64, i           )
+		__field(s64, d           )
+		__field(s32, output      )
+	),
+	TP_fast_assign(
+		__entry->err = err;
+		__entry->err_integral = err_integral;
+		__entry->p = p;
+		__entry->i = i;
+		__entry->d = d;
+		__entry->output = output;
+	),
+
+	TP_printk("err=%d err_integral=%d p=%lld i=%lld d=%lld output=%d",
+		__entry->err, __entry->err_integral,
+		__entry->p, __entry->i, __entry->d, __entry->output)
+);
+
+#endif /* _TRACE_THERMAL_GOVERNOR_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
-- 
1.9.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [RFC PATCH v3 4/7] thermal: introduce the Power Actor API
  2014-06-03 10:18 ` [RFC PATCH v3 4/7] thermal: introduce the Power Actor API Javi Merino
@ 2014-06-11 11:32   ` Eduardo Valentin
  2014-06-12 13:45     ` Javi Merino
  0 siblings, 1 reply; 13+ messages in thread
From: Eduardo Valentin @ 2014-06-11 11:32 UTC (permalink / raw)
  To: Javi Merino; +Cc: linux-pm, linux-kernel, punit.agrawal, Zhang Rui

Hello Javi,

On Tue, Jun 03, 2014 at 11:18:32AM +0100, Javi Merino wrote:
> This patch introduces the Power Actor API in the thermal framework.
> With it, devices that can report their power consumption and control
> it can be registered.  This base interface is meant to be used to
> derive specific power actors, such as a cpu power actor.
> 
> Cc: Zhang Rui <rui.zhang@intel.com>
> Cc: Eduardo Valentin <edubezval@gmail.com>
> Signed-off-by: Javi Merino <javi.merino@arm.com>
> ---
>  Documentation/thermal/power_actor.txt     | 38 ++++++++++++++++++
>  drivers/thermal/Kconfig                   |  3 ++
>  drivers/thermal/Makefile                  |  2 +
>  drivers/thermal/power_actor/Makefile      |  5 +++
>  drivers/thermal/power_actor/power_actor.c | 64 +++++++++++++++++++++++++++++++
>  drivers/thermal/power_actor/power_actor.h | 64 +++++++++++++++++++++++++++++++

Do you think this API may have other users other than thermal?

If yes, I propose moving it somewhere else, such as driver/base/power.



>  6 files changed, 176 insertions(+)
>  create mode 100644 Documentation/thermal/power_actor.txt
>  create mode 100644 drivers/thermal/power_actor/Makefile
>  create mode 100644 drivers/thermal/power_actor/power_actor.c
>  create mode 100644 drivers/thermal/power_actor/power_actor.h
> 
> diff --git a/Documentation/thermal/power_actor.txt b/Documentation/thermal/power_actor.txt
> new file mode 100644
> index 000000000000..5a61f32ec143
> --- /dev/null
> +++ b/Documentation/thermal/power_actor.txt
> @@ -0,0 +1,38 @@
> +
> +Power Actor API
> +===============
> +
> +The base power actor API is meant to be used to derive specific power


How about having a deeper explanation? Maybe including one or two
paragraphs explaning why and when using this API makes sense.

One or two scenario explanation also would help.

An explanation of the difference to cooling API is also good to have.

> +actors, such as a cpu power actor.  When registering, they should call
> +`power_actor_register()` with a unique `enum power_actor_types`.  When

Why this enum? Maybe it is a design copy of PM QoS? But to me this is
bound to struct device, no?

> +unregistering, the power actor should call `power_actor_unregister()`
> +with the `struct power_actor *` received in the call to
> +`power_actor_register()`.
> +
> +Callbacks
> +---------
> +
> +1. u32 get_req_power(struct power_actor *actor)
> +@actor: a valid `struct power_actor *` registered with
> +        `power_actor_register()`
> +
> +`get_req_power()` returns the current requested power in milliwatts.
> +
> +2. u32 get_max_power(struct power_actor *actor)
> +@actor: a valid `struct power_actor *` registered with
> +        `power_actor_register()`
> +
> +`get_max_power()` returns the maximum power that the device could
> +consume if it was fully utilized.  It's a function as some devices'
> +maximum power consumption can change due to external factors such as
> +temperature.
> +
> +3. int set_power(struct power_actor *actor, u32 power)
> +@actor: a valid `struct power_actor *` registered with
> +        `power_actor_register()`
> +@power: power in milliwatts
> +
> +`set_power()` should configure the device to consume @power
> +milliwatts.
> +
> +Returns 0 on success, -E* on error.
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index 2d51912a6e40..47e2f15537ca 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -89,6 +89,9 @@ config THERMAL_GOV_USER_SPACE
>  	help
>  	  Enable this to let the user space manage the platform thermals.
>  
> +config THERMAL_POWER_ACTOR
> +	bool
> +

Why empty description/help?

>  config CPU_THERMAL
>  	bool "generic cpu cooling support"
>  	depends on CPU_FREQ
> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> index 54e4ec9eb5df..878a02cab7d1 100644
> --- a/drivers/thermal/Makefile
> +++ b/drivers/thermal/Makefile
> @@ -14,6 +14,8 @@ thermal_sys-$(CONFIG_THERMAL_GOV_FAIR_SHARE)	+= fair_share.o
>  thermal_sys-$(CONFIG_THERMAL_GOV_STEP_WISE)	+= step_wise.o
>  thermal_sys-$(CONFIG_THERMAL_GOV_USER_SPACE)	+= user_space.o
>  
> +obj-$(CONFIG_THERMAL_POWER_ACTOR) += power_actor/
> +
>  # cpufreq cooling
>  thermal_sys-$(CONFIG_CPU_THERMAL)	+= cpu_cooling.o
>  
> diff --git a/drivers/thermal/power_actor/Makefile b/drivers/thermal/power_actor/Makefile
> new file mode 100644
> index 000000000000..46478f4928be
> --- /dev/null
> +++ b/drivers/thermal/power_actor/Makefile
> @@ -0,0 +1,5 @@
> +#
> +# Makefile for the power actors
> +#
> +
> +obj-y += power_actor.o

Why a new directory? Maybe using parent dir is enough?

> diff --git a/drivers/thermal/power_actor/power_actor.c b/drivers/thermal/power_actor/power_actor.c
> new file mode 100644
> index 000000000000..d891deb0e2a1
> --- /dev/null
> +++ b/drivers/thermal/power_actor/power_actor.c
> @@ -0,0 +1,64 @@
> +/*
> + * Basic interface for power actors
> + *
> + * Copyright (C) 2014 ARM Ltd.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed "as is" WITHOUT ANY WARRANTY of any
> + * kind, whether express or implied; without even the implied warranty
> + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + */
> +
> +#define pr_fmt(fmt) "Power actor: " fmt
> +
> +#include <linux/err.h>
> +#include <linux/list.h>
> +#include <linux/slab.h>
> +
> +#include "power_actor.h"
> +
> +LIST_HEAD(actor_list);
> +
> +/**
> + * power_actor_register - Register an actor in the power actor API
> + * @type:	actor type
> + * @ops:	struct power_actor_ops for this actor
> + * @privdata:	pointer to private data related to the actor
> + *
> + * Returns the struct power_actor * on success, ERR_PTR() on failure
> + */
> +struct power_actor *power_actor_register(enum power_actor_types type,
> +					struct power_actor_ops *ops,
> +					void *privdata)
> +{
> +	struct power_actor *actor;
> +
> +	if (!ops->get_req_power || !ops->get_max_power || !ops->set_power)
> +		return ERR_PTR(-EINVAL);
> +
> +	actor = kzalloc(sizeof(*actor), GFP_KERNEL);

devm_?

> +	if (!actor)
> +		return ERR_PTR(-ENOMEM);
> +
> +	actor->type = type;
> +	actor->ops = ops;
> +	actor->data = privdata;
> +
> +	list_add(&actor->actor_node, &actor_list);
> +

Do you need to protect this one?

> +	return actor;
> +}
> +
> +/**
> + * power_actor_unregister - Unregister an actor
> + * @actor:	the actor to unregister
> + */
> +void power_actor_unregister(struct power_actor *actor)
> +{
> +	list_del(&actor->actor_node);
> +	kfree(actor);
> +}
> diff --git a/drivers/thermal/power_actor/power_actor.h b/drivers/thermal/power_actor/power_actor.h
> new file mode 100644
> index 000000000000..28098f43630b
> --- /dev/null
> +++ b/drivers/thermal/power_actor/power_actor.h
> @@ -0,0 +1,64 @@
> +/*
> + * Copyright (C) 2014 ARM Ltd.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * 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.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef __POWER_ACTOR_H__
> +#define __POWER_ACTOR_H__
> +
> +#include <linux/list.h>
> +
> +#define MAX_NUM_ACTORS 8
> +

unused define

> +enum power_actor_types {
> +};

I still don't get this enum.

> +
> +struct power_actor;
> +
> +/**
> + * struct power_actor_ops - callbacks for power actors
> + * @get_req_power:	return the current requested power in milliwatts
> + * @get_max_power:	return the max power that the device can currently
> + *			consume in milliwatts
> + * @set_power:		configure the device to consume a certain power in
> + *			milliwatts
> + */
> +struct power_actor_ops {
> +	u32 (*get_req_power)(struct power_actor *);
> +	u32 (*get_max_power)(struct power_actor *);
> +	int (*set_power)(struct power_actor *, u32);
> +};
> +
> +/**
> + * struct power_actor - structure for a power actor
> + * @type:	the type of power actor.
> + * @ops:	callbacks for the power actor
> + * @data:	a private pointer for type-specific data
> + * @actor_node:	node in actor_list
> + */
> +struct power_actor {
> +	enum power_actor_types type;
> +	struct power_actor_ops *ops;
> +	void *data;
> +	struct list_head actor_node;
> +};
> +
> +struct power_actor *power_actor_register(enum power_actor_types type,
> +					struct power_actor_ops *ops,
> +					void *privdata);
> +void power_actor_unregister(struct power_actor *actor);
> +
> +extern struct list_head actor_list;
> +
> +#endif /* __POWER_ACTOR_H__ */
> -- 
> 1.9.1
> 
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFC PATCH v3 5/7] thermal: add a basic cpu power actor
  2014-06-03 10:18 ` [RFC PATCH v3 5/7] thermal: add a basic cpu power actor Javi Merino
@ 2014-06-11 12:05   ` Eduardo Valentin
  2014-06-12 14:26     ` Javi Merino
  0 siblings, 1 reply; 13+ messages in thread
From: Eduardo Valentin @ 2014-06-11 12:05 UTC (permalink / raw)
  To: Javi Merino; +Cc: linux-pm, linux-kernel, punit.agrawal, Zhang Rui

Hello Javi,


On Tue, Jun 03, 2014 at 11:18:33AM +0100, Javi Merino wrote:
> Introduce a power actor for cpus.  It has a basic power model to get
> the current power utilization and uses cpufreq cooling devices to set
> the desired power.  It uses the current frequency (as reported by
> cpufreq) as well as load and OPPs for the power calculations.  The
> cpus must have registered their OPPs in the OPP library.
> 
> Cc: Zhang Rui <rui.zhang@intel.com>
> Cc: Eduardo Valentin <edubezval@gmail.com>
> Signed-off-by: Punit Agrawal <punit.agrawal@arm.com>
> Signed-off-by: Javi Merino <javi.merino@arm.com>
> ---
>  Documentation/thermal/power_actor.txt     | 126 +++++++
>  drivers/thermal/Kconfig                   |   5 +
>  drivers/thermal/power_actor/Kconfig       |   9 +
>  drivers/thermal/power_actor/Makefile      |   2 +
>  drivers/thermal/power_actor/cpu_actor.c   | 601 ++++++++++++++++++++++++++++++
>  drivers/thermal/power_actor/power_actor.h |  41 ++
>  6 files changed, 784 insertions(+)
>  create mode 100644 drivers/thermal/power_actor/Kconfig
>  create mode 100644 drivers/thermal/power_actor/cpu_actor.c
> 
> diff --git a/Documentation/thermal/power_actor.txt b/Documentation/thermal/power_actor.txt
> index 5a61f32ec143..fd51760615bf 100644
> --- a/Documentation/thermal/power_actor.txt
> +++ b/Documentation/thermal/power_actor.txt
> @@ -36,3 +36,129 @@ temperature.
>  milliwatts.
>  
>  Returns 0 on success, -E* on error.
> +
> +CPU Power Actor API
> +===================
> +
> +A simple power model for CPUs.  The current power is calculated as
> +dynamic + (optionally) static power.  This power model requires that
> +the operating-points of the CPUs are registered using the kernel's opp
> +library and the `cpufreq_frequency_table` is assigned to the `struct
> +device` of the cpu.  If you are using the `cpufreq-cpu0.c` driver then
> +the `cpufreq_frequency_table` should already be assigned to the cpu
> +device.
> +
> +The `tz` and `plat_static_func` parameters of
> +`power_cpu_actor_register()` are optional.  If you don't provide them,
> +only dynamic power will be considered.
> +
> +Dynamic power
> +-------------
> +
> +The dynamic power consumption of a processor depends
> +on many factors.  For a given processor implementation the primary
> +factors are:
> +
> +- The time the processor spends running, consuming dynamic power, as
> +  compared to the time in idle states where dynamic consumption is
> +  negligible.  Herein we refer to this as 'utilisation'.
> +- The voltage and frequency levels as a result of DVFS.  The DVFS
> +  level is a dominant factor governing power consumption.
> +- In running time the 'execution' behaviour (instruction types, memory
> +  access patterns and so forth) causes, in most cases, a second order
> +  variation.  In pathological cases this variation can be significant,
> +  but typically it is of a much lesser impact than the factors above.
> +
> +A high level dynamic power consumption model may then be represented as:
> +
> +Pdyn = f(run) * Voltage^2 * Frequency * Utilisation
> +
> +f(run) here represents the described execution behaviour and its
> +result has a units of Watts/Hz/Volt^2 (this often expressed in
> +mW/MHz/uVolt^2)
> +
> +The detailed behaviour for f(run) could be modelled on-line.  However,
> +in practice, such an on-line model has dependencies on a number of
> +implementation specific processor support and characterisation
> +factors.  Therefore, in initial implementation that contribution is
> +represented as a constant coefficient.  This is a simplification
> +consistent with the relative contribution to overall power variation.
> +
> +In this simplified representation our model becomes:
> +
> +Pdyn = Kd * Voltage^2 * Frequency * Utilisation
> +
> +Where Kd (capacitance) represents an indicative running time dynamic
> +power coefficient in fundamental units of mW/MHz/uVolt^2
> +
> +Static Power
> +------------
> +
> +Static leakage power consumption depends on a number of factors.  For a
> +given circuit implementation the primary factors are:
> +
> +- Time the circuit spends in each 'power state'
> +- Temperature
> +- Operating voltage
> +- Process grade
> +
> +The time the circuit spends in each 'power state' for a given
> +evaluation period at first order means OFF or ON.  However,
> +'retention' states can also be supported that reduce power during
> +inactive periods without loss of context.
> +
> +Note: The visibility of state entries to the OS can vary, according to
> +platform specifics, and this can then impact the accuracy of a model
> +based on OS state information alone.  It might be possible in some
> +cases to extract more accurate information from system resources.
> +
> +The temperature, operating voltage and process 'grade' (slow to fast)
> +of the circuit are all significant factors in static leakage power
> +consumption.  All of these have complex relationships to static power.
> +
> +Circuit implementation specific factors include the chosen silicon
> +process as well as the type, number and size of transistors in both
> +the logic gates and any RAM elements included.
> +
> +The static power consumption modelling must take into account the
> +power managed regions that are implemented.  Taking the example of an
> +ARM processor cluster, the modelling would take into account whether
> +each CPU can be powered OFF separately or if only a single power
> +region is implemented for the complete cluster.
> +
> +In one view, there are others, a static power consumption model can
> +then start from a set of reference values for each power managed
> +region (e.g. CPU, Cluster/L2) in each state (e.g. ON, OFF) at an
> +arbitrary process grade, voltage and temperature point.  These values
> +are then scaled for all of the following: the time in each state, the
> +process grade, the current temperature and the operating
> +voltage.  However, since both implementation specific and complex
> +relationships dominate the estimate, the appropriate interface to the
> +model from the cpu power actor is to provide a function callback that
> +calculates the static power in this platform.  When registering the
> +power cpu actor, pass the thermal zone closest to the cpu (to get the
> +temperature) and a function pointer that follows the `get_static_t`
> +prototype:
> +
> +    u32 plat_get_static(cpumask_t *cpumask, unsigned long voltage,
> +				unsigned long temperature);
> +
> +with `cpumask` a cpumask of the cpus involved in the calculation,
> +`voltage` the voltage at which they are opperating and `temperature`
> +their current temperature.
> +
> +If both `tz` and `plat_static_func` are NULL when registering the
> +power cpu actor, static power is considered to be negligible for this
> +platform and only dynamic power is considered.
> +
> +The platform specific callback can then use any combination of tables
> +and/or equations to permute the estimated value.  Process grade
> +information is not passed to the model since access to such data, from
> +on-chip measurement capability or manufacture time data, is platform
> +specific.
> +
> +Note: the significance of static power for CPUs in comparison to
> +dynamic power is highly dependent on implementation.  Given the
> +potential complexity in implementation, the importance and accuracy of
> +its inclusion when using cpu power actors should be assessed on a case by
> +cases basis.

In one way or another, we are dealing with models here. Even the above
dynamic power model is still a model and has errors from reality. The
point is providing a good default model that works across platforms.

> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index 47e2f15537ca..1818c4fa60b8 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -92,6 +92,11 @@ config THERMAL_GOV_USER_SPACE
>  config THERMAL_POWER_ACTOR
>  	bool
>  
> +menu "Power actors"
> +depends on THERMAL_POWER_ACTOR
> +source "drivers/thermal/power_actor/Kconfig"
> +endmenu
> +
>  config CPU_THERMAL
>  	bool "generic cpu cooling support"
>  	depends on CPU_FREQ
> diff --git a/drivers/thermal/power_actor/Kconfig b/drivers/thermal/power_actor/Kconfig
> new file mode 100644
> index 000000000000..fa542ca99cdb
> --- /dev/null
> +++ b/drivers/thermal/power_actor/Kconfig
> @@ -0,0 +1,9 @@
> +#
> +# Thermal power actor configuration
> +#
> +
> +config THERMAL_POWER_ACTOR_CPU
> +	bool
> +	prompt "Simple power model for a CPU"
> +	help
> +	  A simple CPU power model

A better help is always welcome.

> diff --git a/drivers/thermal/power_actor/Makefile b/drivers/thermal/power_actor/Makefile
> index 46478f4928be..6f04b92997e6 100644
> --- a/drivers/thermal/power_actor/Makefile
> +++ b/drivers/thermal/power_actor/Makefile
> @@ -3,3 +3,5 @@
>  #
>  
>  obj-y += power_actor.o
> +
> +obj-$(CONFIG_THERMAL_POWER_ACTOR_CPU) += cpu_actor.o
> diff --git a/drivers/thermal/power_actor/cpu_actor.c b/drivers/thermal/power_actor/cpu_actor.c
> new file mode 100644
> index 000000000000..6eac80d119a5
> --- /dev/null
> +++ b/drivers/thermal/power_actor/cpu_actor.c
> @@ -0,0 +1,601 @@
> +/*
> + * A basic cpu actor
> + *
> + * Copyright (C) 2014 ARM Ltd.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * This program is distributed "as is" WITHOUT ANY WARRANTY of any
> + * kind, whether express or implied; without even the implied warranty
> + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + */
> +
> +#define pr_fmt(fmt) "CPU actor: " fmt
> +
> +#include <linux/cpu.h>
> +#include <linux/cpufreq.h>
> +#include <linux/cpumask.h>
> +#include <linux/cpu_cooling.h>
> +#include <linux/device.h>
> +#include <linux/err.h>
> +#include <linux/init.h>
> +#include <linux/list.h>
> +#include <linux/pm_opp.h>
> +#include <linux/printk.h>
> +#include <linux/slab.h>
> +
> +#include "power_actor.h"
> +
> +/**
> + * struct power_table - frequency to power conversion
> + * @frequency:	frequency in KHz
> + * @power:	power in mW
> + *
> + * This structure is built when the cooling device registers and helps
> + * in translating frequency to power and viceversa.
> + */
> +struct power_table {
> +	u32 frequency;
> +	u32 power;
> +};
> +
> +/**
> + * struct cpu_actor - information for each cpu actor
> + * @cpumask:	cpus covered by this actor
> + * @freq:	frequency in KHz of the cpus represented by the cooling device
> + * @last_load:	load measured by the latest call to cpu_get_req_power()
> + * @time_in_idle:	previous reading of the absolute time that this cpu was
> + *			idle
> + * @time_in_idle_timestamp: wall time of the last invocation of
> + *			    get_cpu_idle_time_us()
> + * @dyn_power_table:	array of struct power_table for frequency to power
> + *			conversion
> + * @dyn_power_table_entries: number of entries in the @dyn_power_table array
> + * @cdev:	cpufreq cooling device associated with this actor
> + * @tz:		thermal zone for the sensor closest to the cpus
> + * @plat_get_static_power:	callback to calculate the static power
> + */
> +struct cpu_actor {
> +	cpumask_t cpumask;
> +	u32 freq;
> +	u32 last_load;
> +	u64 time_in_idle[NR_CPUS];
> +	u64 time_in_idle_timestamp[NR_CPUS];
> +	struct power_table *dyn_power_table;
> +	int dyn_power_table_entries;
> +	struct thermal_cooling_device *cdev;
> +	struct thermal_zone_device *tz;
> +	get_static_t plat_get_static_power;
> +};
> +
> +static DEFINE_MUTEX(cpu_power_actor_lock);
> +
> +static unsigned int cpu_power_actors_registered;
> +
> +static u32 cpu_freq_to_power(struct cpu_actor *cpu_actor, u32 freq)
> +{
> +	int i;
> +	struct power_table *pt = cpu_actor->dyn_power_table;
> +
> +	for (i = 0; i < cpu_actor->dyn_power_table_entries - 1; i++)
> +		if (freq <= pt[i].frequency)
> +			break;
> +
> +	return pt[i].power;
> +}
> +
> +static u32 cpu_power_to_freq(struct cpu_actor *cpu_actor, u32 power)
> +{
> +	int i;
> +	struct power_table *pt = cpu_actor->dyn_power_table;
> +
> +	for (i = 0; i < cpu_actor->dyn_power_table_entries - 1; i++)
> +		if (power <= pt[i].power)
> +			break;
> +
> +	return pt[i].frequency;
> +}
> +
> +/**
> + * get_load - get load for a cpu since last updated
> + * @cpu_actor: struct cpu_actor for this actor
> + * @cpu: cpu number
> + *
> + * Return the average load of cpu @cpu in percentage since this
> + * function was last called.
> + */
> +static u32 get_load(struct cpu_actor *cpu_actor, int cpu)
> +{
> +	u32 load;
> +	u64 now, now_idle, delta_time, delta_idle;
> +
> +	now_idle = get_cpu_idle_time(cpu, &now, 0);
> +	delta_idle = now_idle - cpu_actor->time_in_idle[cpu];
> +	delta_time = now - cpu_actor->time_in_idle_timestamp[cpu];
> +
> +	if (delta_time <= delta_idle)
> +		load = 0;
> +	else
> +		load = div64_u64(100 * (delta_time - delta_idle), delta_time);
> +
> +	cpu_actor->time_in_idle[cpu] = now_idle;
> +	cpu_actor->time_in_idle_timestamp[cpu] = now;
> +
> +	return load;
> +}
> +
> +/**
> + * __get_static_power() - calculate the static power consumed by the cpus
> + * @cpu_actor:	&struct cpu_actor for this cpu
> + * @freq:	frequency in KHz
> + *
> + * Calculate the static power consumed by the cpus described by
> + * @cpu_actor running at frequency @freq.  This function relies on a
> + * platform specific function that should have been provided when the
> + * actor was registered.  If it wasn't, the static power is assumed to
> + * be negligible.
> + *
> + * Return: The static power consumed by the cpus.  It returns 0 on
> + * error or if there is no plat_get_static_power().
> + */
> +static u32 __get_static_power(struct cpu_actor *cpu_actor, unsigned long freq)
> +{
> +	int err;
> +	struct device *cpu_dev;
> +	struct dev_pm_opp *opp;
> +	unsigned long voltage, temperature;
> +	cpumask_t *cpumask = &cpu_actor->cpumask;
> +	unsigned long freq_hz = freq * 1000;
> +
> +	if (!cpu_actor->plat_get_static_power)
> +		return 0;
> +
> +	if (freq == 0)
> +		return 0;
> +
> +	cpu_dev = get_cpu_device(cpumask_any(cpumask));
> +
> +	rcu_read_lock();
> +
> +	opp = dev_pm_opp_find_freq_exact(cpu_dev, freq_hz, true);
> +	voltage = dev_pm_opp_get_voltage(opp);
> +
> +	rcu_read_unlock();
> +
> +	if (voltage == 0) {
> +		dev_warn_ratelimited(cpu_dev,
> +				"Failed to get voltage for frequency %lu: %ld\n",
> +				freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
> +		return 0;
> +	}
> +
> +	err = thermal_zone_get_temp(cpu_actor->tz, &temperature);
> +	if (err) {
> +		dev_warn(&cpu_actor->tz->device, "Unable to read temperature: %d\n",
> +			err);
> +		return 0;
> +	}
> +
> +	return cpu_actor->plat_get_static_power(cpumask, voltage, temperature);

How about if this could be optional? That is, something like:


+	if (cpu_actor->plat_get_static_power)
+		return cpu_actor->plat_get_static_power(cpumask, voltage, temperature);
+	else
+		return default_static_power(cpumask, voltage, temperature);

> +}
> +
> +static u32 get_static_power(struct cpu_actor *cpu_actor)
> +{
> +	return __get_static_power(cpu_actor, cpu_actor->freq);
> +}
> +
> +/**
> + * get_dynamic_power - calculate the dynamic power
> + * @cpu_actor: cpu_actor pointer
> + *
> + * Return the dynamic power consumed by the cpus described by
> + * cpu_actor.
> + */
> +static u32 get_dynamic_power(struct cpu_actor *cpu_actor)
> +{
> +	int cpu;
> +	u32 power = 0, raw_cpu_power, total_load = 0;
> +	raw_cpu_power = cpu_freq_to_power(cpu_actor, cpu_actor->freq);
> +
> +	for_each_cpu(cpu, &cpu_actor->cpumask) {
> +		u32 load;
> +
> +		if (!cpu_online(cpu))
> +			continue;
> +
> +		load = get_load(cpu_actor, cpu);
> +		power += (raw_cpu_power * load) / 100;
> +		total_load += load;
> +	}
> +
> +	cpu_actor->last_load = total_load;
> +
> +	return power;
> +}
> +
> +/**
> + * cpu_get_req_power - get the current power
> + * @actor:	power actor pointer
> + *
> + * Callback for the power actor to return the current power
> + * consumption in milliwatts.
> + */
> +static u32 cpu_get_req_power(struct power_actor *actor)
> +{
> +	u32 static_power, dynamic_power;
> +	struct cpu_actor *cpu_actor = actor->data;
> +
> +	static_power = get_static_power(cpu_actor);
> +	dynamic_power = get_dynamic_power(cpu_actor);
> +
> +	return static_power + dynamic_power;
> +}
> +
> +/**
> + * cpu_get_max_power() - get the maximum power that the cpu could currently consume
> + * @actor:	power actor pointer
> + *
> + * Callback for the power actor to return the maximum power
> + * consumption in milliwatts that the cpu could currently consume.
> + * The static power depends on temperature so the maximum power will
> + * vary over time.
> + */
> +static u32 cpu_get_max_power(struct power_actor *actor)
> +{
> +	u32 max_static_power, max_dyn_power;
> +	cpumask_t *cpumask;
> +	unsigned int max_freq, last_entry, num_cpus;
> +	struct cpu_actor *cpu_actor = actor->data;
> +
> +	cpumask = &cpu_actor->cpumask;
> +	max_freq = cpufreq_quick_get_max(cpumask_any(cpumask));
> +	max_static_power = __get_static_power(cpu_actor, max_freq);
> +
> +	last_entry = cpu_actor->dyn_power_table_entries - 1;
> +	num_cpus = cpumask_weight(cpumask);
> +	max_dyn_power = cpu_actor->dyn_power_table[last_entry].power * num_cpus;
> +
> +	return max_static_power + max_dyn_power;
> +}
> +
> +/**
> + * cpu_set_power - set cpufreq cooling device to consume a certain power
> + * @actor: power actor pointer
> + * @power: the power in milliwatts that should be set
> + *
> + * Callback for the power actor to configure the power consumption of
> + * the CPU to be @power milliwatts at most.  This function assumes
> + * that the load will remain constant.  The power is translated into a
> + * cooling state that the cpu cooling device then sets.
> + *
> + * Returns 0 on success, -EINVAL if it couldn't convert the frequency
> + * to a cpufreq cooling device state.
> + */
> +static int cpu_set_power(struct power_actor *actor, u32 power)
> +{
> +	unsigned int cpu, freq;
> +	unsigned long cdev_state;
> +	u32 dyn_power, normalised_power, last_load;
> +	struct thermal_cooling_device *cdev;
> +	struct cpu_actor *cpu_actor = actor->data;
> +
> +	cdev = cpu_actor->cdev;
> +
> +	dyn_power = power - get_static_power(cpu_actor);
> +	cpu = cpumask_any(&cpu_actor->cpumask);
> +	last_load = cpu_actor->last_load ? cpu_actor->last_load : 1;
> +	normalised_power = (dyn_power * 100) / last_load;
> +	freq = cpu_power_to_freq(cpu_actor, normalised_power);
> +
> +	cdev_state = cpufreq_cooling_get_level(cpu, freq);
> +	if (cdev_state == THERMAL_CSTATE_INVALID) {
> +		pr_err("Failed to convert %dKHz for cpu %d into a cdev state\n",
> +			freq, cpu);
> +		return -EINVAL;
> +	}
> +
> +	return cdev->ops->set_cur_state(cdev, cdev_state);
> +}
> +
> +static struct power_actor_ops cpu_actor_ops = {
> +	.get_req_power = cpu_get_req_power,
> +	.get_max_power = cpu_get_max_power,
> +	.set_power = cpu_set_power,
> +};

Why the above cannot be done with current thermal API?

> +
> +/**
> + * power_cpu_actor_add_static() - add static information to an existing cpu actor
> + * @cpu:	a cpu represented by the cpu power actor
> + * @tz:		pointer to the closest thermal zone to the cpu
> + * @plat_static_func:	function to calculate the static power consumed by these
> + *			cpus
> + *
> + * A function to add static information to an actor after it has
> + * registered.  In order to calculate the static power, we need the to
> + * know the temperature of the cpu, which we get from its closest
> + * thermal zone.  It is possible that when registering the
> + * power_cpu_actor, the thermal zone hasn't been registered yet.  This
> + * function allows you to add that information when the thermal zone
> + * is registered.  @cpu should be one of the cpus represented by the
> + * cpu actor you want to modify
> + *
> + * Return: 0 on success.  -EINVAL if @tz or @plat_static_func are
> + * NULL.  -ENOENT if there's no cpu power actor for @cpu.
> + */
> +int power_cpu_actor_add_static(unsigned int cpu, struct thermal_zone_device *tz,
> +			get_static_t plat_static_func)
> +{
> +	struct power_actor *actor;
> +
> +	if (!plat_static_func || !tz)
> +		return -EINVAL;
> +
> +	list_for_each_entry(actor, &actor_list, actor_node) {
> +		struct cpu_actor *cpu_actor;
> +
> +		if (actor->type != POWER_ACTOR_CPU)
> +			continue;
> +
> +		cpu_actor = actor->data;
> +
> +		if (cpumask_test_cpu(cpu, &cpu_actor->cpumask)) {
> +			cpu_actor->tz = tz;
> +			cpu_actor->plat_get_static_power = plat_static_func;
> +			return 0;
> +		}
> +	}
> +
> +	return -ENOENT;
> +}
> +
> +/**
> + * cpufreq_frequency_change - notifier callback for cpufreq frequency changes
> + * @nb:		struct notifier_block * with callback info
> + * @event:	value showing cpufreq event for which this function invoked
> + * @data:	callback-specific data
> + *
> + * Callback to get notifications of frequency changes.  In the
> + * CPUFREQ_POSTCHANGE @event we store the new frequency so that
> + * cpufreq_get_cur() knows the current frequency and can convert it
> + * into power.
> + */
> +static int cpufreq_frequency_change(struct notifier_block *nb,
> +				unsigned long event, void *data)
> +{
> +	struct power_actor *actor;
> +	struct cpufreq_freqs *freqs = data;
> +
> +	/* Only update frequency on postchange */
> +	if (event != CPUFREQ_POSTCHANGE)
> +		return NOTIFY_DONE;
> +
> +	list_for_each_entry(actor, &actor_list, actor_node) {
> +		struct cpu_actor *cpu_actor;
> +
> +		if (actor->type != POWER_ACTOR_CPU)
> +			continue;
> +
> +		cpu_actor = actor->data;
> +
> +		if (cpumask_test_cpu(freqs->cpu, &cpu_actor->cpumask))
> +			cpu_actor->freq = freqs->new;
> +	}
> +
> +	return NOTIFY_OK;
> +}
> +
> +struct notifier_block cpufreq_transition_notifier = {
> +	.notifier_call = cpufreq_frequency_change,
> +};
> +
> +/**
> + * build_dyn_power_table - create a dynamic power to frequency table
> + * @cpu_actor:	the cpu_actor in which to store the table
> + * @capacitance: dynamic power coefficient for these cpus
> + *
> + * Build a dynamic power to frequency table for this cpu and store it
> + * in @cpu_actor.  This table will be used in cpu_power_to_freq() and
> + * cpu_freq_to_power() to convert between power and frequency
> + * efficiently.  Power is stored in mW, frequency in KHz.  The
> + * resulting table is in ascending order.
> + *
> + * Returns 0 on success, -E* on error.
> + */
> +static int build_dyn_power_table(struct cpu_actor *cpu_actor, u32 capacitance)
> +{
> +	struct power_table *power_table;
> +	struct dev_pm_opp *opp;
> +	struct device *dev = NULL;
> +	int num_opps, cpu, i, ret = 0;
> +	unsigned long freq;
> +
> +	num_opps = 0;
> +
> +	rcu_read_lock();
> +
> +	for_each_cpu(cpu, &cpu_actor->cpumask) {
> +		dev = get_cpu_device(cpu);
> +		if (!dev)
> +			continue;
> +
> +		num_opps = dev_pm_opp_get_opp_count(dev);
> +		if (num_opps > 0) {
> +			break;
> +		} else if (num_opps < 0) {
> +			ret = num_opps;
> +			goto unlock;
> +		}
> +	}
> +
> +	if (num_opps == 0) {
> +		ret = -EINVAL;
> +		goto unlock;
> +	}
> +
> +	power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
> +
> +	i = 0;
> +	for (freq = 0;
> +	     opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
> +	     freq++) {
> +		u32 freq_mhz, voltage_mv;
> +		u64 power;
> +
> +		freq_mhz = freq / 1000000;
> +		voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
> +
> +		/*
> +		 * Do the multiplication with MHz and millivolt so as
> +		 * to not overflow.
> +		 */
> +		power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
> +		do_div(power, 1000000000);
> +
> +		/* frequency is stored in power_table in KHz */
> +		power_table[i].frequency = freq / 1000;
> +		power_table[i].power = power;
> +
> +		i++;
> +	}
> +
> +	if (i == 0) {
> +		ret = PTR_ERR(opp);
> +		goto unlock;
> +	}
> +
> +	cpu_actor->dyn_power_table = power_table;
> +	cpu_actor->dyn_power_table_entries = i;
> +
> +unlock:
> +	rcu_read_unlock();
> +	return ret;
> +}
> +
> +/**
> + * power_cpu_actor_register - register a cpu_actor within the power actor API
> + * @cpumask:	cpumask of cpus covered by this power_actor
> + * @tz:		thermal zone closest to the cpus (optional)
> + * @capacitance: dynamic power coefficient for these cpus
> + * @plat_static_func:	function to calculate the static power consumed by these
> + *			cpus (optional)
> + *
> + * Create a cpufreq cooling device for the cpus in @cpumask and
> + * register it with the power actor API using a simple cpu power
> + * model.  The cpus must have registered their OPPs in the OPP
> + * library.
> + *
> + * An optional @plat_static_func may be provided to calculate the
> + * static power consumed by these cpus.  If the platform's static
> + * power consumption is unknown or negligible, make it NULL.  If
> + * @plat_static_fun is specified, @tz needs to be specified as well.
> + * @tz should be a pointer to the thermal_zone_device whose sensor is
> + * closer to the cpus in @cpumask.
> + *
> + * Return the power_actor created on success or the corresponding
> + * ERR_PTR() on failure.  This actor should be freed with
> + * power_cpu_actor_unregister() when it's no longer needed.
> + */
> +struct power_actor *
> +power_cpu_actor_register(cpumask_t *cpumask,
> +			struct thermal_zone_device *tz,
> +			u32 capacitance,
> +			get_static_t plat_static_func)
> +{
> +	int ret;
> +	struct thermal_cooling_device *cdev;
> +	struct device *cpu_dev;
> +	struct power_actor *actor, *err_ret;
> +	struct cpu_actor *cpu_actor;
> +
> +	if (plat_static_func && !tz) {
> +		pr_warn("plat_static_func provided without a thermal zone, ignoring it\n");
> +		plat_static_func = NULL;


You either make this one hard requirement (and do not fail gracefuly
like above) or you make it really optional and provide a good default
formula (which is preferrable).

> +	}
> +
> +	cpu_dev = get_cpu_device(cpumask_any(cpumask));
> +	if (!cpu_dev || !cpu_dev->of_node)
> +		cdev = cpufreq_cooling_register(cpumask);
> +	else
> +		cdev = of_cpufreq_cooling_register(cpu_dev->of_node, cpumask);
> +
> +	if (!cdev)
> +		return ERR_PTR(PTR_ERR(cdev));
> +
> +	cpu_actor = kzalloc(sizeof(*cpu_actor), GFP_KERNEL);
> +	if (!cpu_actor) {
> +		err_ret = ERR_PTR(-ENOMEM);
> +		goto cdev_unregister;
> +	}
> +
> +	cpumask_copy(&cpu_actor->cpumask, cpumask);
> +	cpu_actor->cdev = cdev;
> +	cpu_actor->tz = tz;
> +	cpu_actor->plat_get_static_power = plat_static_func;
> +
> +	ret = build_dyn_power_table(cpu_actor, capacitance);
> +	if (ret) {
> +		err_ret = ERR_PTR(ret);
> +		goto kfree;
> +	}
> +
> +	actor = power_actor_register(POWER_ACTOR_CPU, &cpu_actor_ops,
> +				cpu_actor);
> +	if (IS_ERR(actor)) {
> +		err_ret = actor;
> +		goto kfree;
> +	}
> +
> +	mutex_lock(&cpu_power_actor_lock);
> +
> +	/*
> +	 * You can't register multiple times the same notifier_block.
> +	 * The first power actor registered is the only one that
> +	 * registers the notifier.
> +	 */
> +	if (!cpu_power_actors_registered) {
> +		ret = cpufreq_register_notifier(&cpufreq_transition_notifier,
> +						CPUFREQ_TRANSITION_NOTIFIER);
> +		if (ret) {
> +			err_ret = ERR_PTR(ret);
> +			mutex_unlock(&cpu_power_actor_lock);
> +			goto power_actor_unregister;
> +		}
> +	}
> +
> +	cpu_power_actors_registered++;
> +	mutex_unlock(&cpu_power_actor_lock);
> +
> +	return actor;
> +
> +power_actor_unregister:
> +	power_actor_unregister(actor);
> +kfree:
> +	kfree(cpu_actor);
> +cdev_unregister:
> +	cpufreq_cooling_unregister(cdev);
> +
> +	return err_ret;
> +}
> +
> +void power_cpu_actor_unregister(struct power_actor *actor)
> +{
> +	struct cpu_actor *cpu_actor = actor->data;
> +
> +	kfree(cpu_actor->dyn_power_table);
> +
> +	mutex_lock(&cpu_power_actor_lock);
> +
> +	cpu_power_actors_registered--;
> +
> +	if (!cpu_power_actors_registered)
> +		cpufreq_unregister_notifier(&cpufreq_transition_notifier,
> +					CPUFREQ_TRANSITION_NOTIFIER);
> +
> +	mutex_unlock(&cpu_power_actor_lock);
> +
> +	kfree(cpu_actor);
> +	power_actor_unregister(actor);
> +}
> diff --git a/drivers/thermal/power_actor/power_actor.h b/drivers/thermal/power_actor/power_actor.h
> index 28098f43630b..230317c284b2 100644
> --- a/drivers/thermal/power_actor/power_actor.h
> +++ b/drivers/thermal/power_actor/power_actor.h
> @@ -17,11 +17,16 @@
>  #ifndef __POWER_ACTOR_H__
>  #define __POWER_ACTOR_H__
>  
> +#include <linux/cpumask.h>
> +#include <linux/device.h>
> +#include <linux/err.h>
>  #include <linux/list.h>
> +#include <linux/thermal.h>
>  
>  #define MAX_NUM_ACTORS 8
>  
>  enum power_actor_types {
> +	POWER_ACTOR_CPU,

Using struct device is more scalable no? What if we want to provide a
power actor for a specific bus, or device, or coprocessor? Are we going
to maintain this enum for every single new user?

Another point to think is if we are having more than one user of power
actor per device (type). 

>  };
>  
>  struct power_actor;
> @@ -59,6 +64,42 @@ struct power_actor *power_actor_register(enum power_actor_types type,
>  					void *privdata);
>  void power_actor_unregister(struct power_actor *actor);
>  
> +typedef u32 (*get_static_t)(cpumask_t *cpumask,
> +				unsigned long voltage,
> +				unsigned long temperature);
> +
> +#ifdef CONFIG_THERMAL_POWER_ACTOR_CPU
> +int power_cpu_actor_add_static(unsigned int cpu,
> +			struct thermal_zone_device *tz,
> +			get_static_t plat_static_func);
> +struct power_actor *
> +power_cpu_actor_register(cpumask_t *cpumask,
> +			struct thermal_zone_device *tz,
> +			u32 capacitance,
> +			get_static_t plat_static_func);
> +void power_cpu_actor_unregister(struct power_actor *actor);
> +#else
> +static inline
> +int power_cpu_actor_add_static(unsigned int cpu,
> +					struct thermal_zone_device *tz,
> +					get_static_t plat_static_func)
> +{
> +	return -ENOSYS;
> +}
> +static inline
> +struct power_actor *
> +power_cpu_actor_register(cpumask_t *cpumask,
> +			struct thermal_zone_device *tz,
> +			u32 capacitance,
> +			get_static_t plat_static_func)
> +{
> +	return ERR_PTR(-ENOSYS);
> +}
> +static inline void power_cpu_actor_unregister(struct power_actor *actor)
> +{
> +}
> +#endif
> +
>  extern struct list_head actor_list;
>  
>  #endif /* __POWER_ACTOR_H__ */
> -- 
> 1.9.1
> 
> 


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFC PATCH v3 4/7] thermal: introduce the Power Actor API
  2014-06-11 11:32   ` Eduardo Valentin
@ 2014-06-12 13:45     ` Javi Merino
  0 siblings, 0 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-12 13:45 UTC (permalink / raw)
  To: Eduardo Valentin; +Cc: linux-pm, linux-kernel, Punit Agrawal, Zhang Rui

On Wed, Jun 11, 2014 at 12:32:54PM +0100, Eduardo Valentin wrote:
> Hello Javi,
> 
> On Tue, Jun 03, 2014 at 11:18:32AM +0100, Javi Merino wrote:
> > This patch introduces the Power Actor API in the thermal framework.
> > With it, devices that can report their power consumption and control
> > it can be registered.  This base interface is meant to be used to
> > derive specific power actors, such as a cpu power actor.
> > 
> > Cc: Zhang Rui <rui.zhang@intel.com>
> > Cc: Eduardo Valentin <edubezval@gmail.com>
> > Signed-off-by: Javi Merino <javi.merino@arm.com>
> > ---
> >  Documentation/thermal/power_actor.txt     | 38 ++++++++++++++++++
> >  drivers/thermal/Kconfig                   |  3 ++
> >  drivers/thermal/Makefile                  |  2 +
> >  drivers/thermal/power_actor/Makefile      |  5 +++
> >  drivers/thermal/power_actor/power_actor.c | 64 +++++++++++++++++++++++++++++++
> >  drivers/thermal/power_actor/power_actor.h | 64 +++++++++++++++++++++++++++++++
> 
> Do you think this API may have other users other than thermal?
> 
> If yes, I propose moving it somewhere else, such as driver/base/power.
> 

It could be used by others, but I think we should move it when those
other users appear.

> >  6 files changed, 176 insertions(+)
> >  create mode 100644 Documentation/thermal/power_actor.txt
> >  create mode 100644 drivers/thermal/power_actor/Makefile
> >  create mode 100644 drivers/thermal/power_actor/power_actor.c
> >  create mode 100644 drivers/thermal/power_actor/power_actor.h
> > 
> > diff --git a/Documentation/thermal/power_actor.txt b/Documentation/thermal/power_actor.txt
> > new file mode 100644
> > index 000000000000..5a61f32ec143
> > --- /dev/null
> > +++ b/Documentation/thermal/power_actor.txt
> > @@ -0,0 +1,38 @@
> > +
> > +Power Actor API
> > +===============
> > +
> > +The base power actor API is meant to be used to derive specific power
> 
> 
> How about having a deeper explanation? Maybe including one or two
> paragraphs explaning why and when using this API makes sense.
> 
> One or two scenario explanation also would help.

Ok, I'll add it.

> An explanation of the difference to cooling API is also good to have.
> 
> > +actors, such as a cpu power actor.  When registering, they should call
> > +`power_actor_register()` with a unique `enum power_actor_types`.  When
> 
> Why this enum? Maybe it is a design copy of PM QoS?

To be able to tell different power actors apart.  I haven't looked
into PM QoS.

> But to me this is bound to struct device, no?

You're suggesting that we should add this as a field to struct device?
Could do.

> > +unregistering, the power actor should call `power_actor_unregister()`
> > +with the `struct power_actor *` received in the call to
> > +`power_actor_register()`.
> > +
> > +Callbacks
> > +---------
> > +
> > +1. u32 get_req_power(struct power_actor *actor)
> > +@actor: a valid `struct power_actor *` registered with
> > +        `power_actor_register()`
> > +
> > +`get_req_power()` returns the current requested power in milliwatts.
> > +
> > +2. u32 get_max_power(struct power_actor *actor)
> > +@actor: a valid `struct power_actor *` registered with
> > +        `power_actor_register()`
> > +
> > +`get_max_power()` returns the maximum power that the device could
> > +consume if it was fully utilized.  It's a function as some devices'
> > +maximum power consumption can change due to external factors such as
> > +temperature.
> > +
> > +3. int set_power(struct power_actor *actor, u32 power)
> > +@actor: a valid `struct power_actor *` registered with
> > +        `power_actor_register()`
> > +@power: power in milliwatts
> > +
> > +`set_power()` should configure the device to consume @power
> > +milliwatts.
> > +
> > +Returns 0 on success, -E* on error.
> > diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> > index 2d51912a6e40..47e2f15537ca 100644
> > --- a/drivers/thermal/Kconfig
> > +++ b/drivers/thermal/Kconfig
> > @@ -89,6 +89,9 @@ config THERMAL_GOV_USER_SPACE
> >  	help
> >  	  Enable this to let the user space manage the platform thermals.
> >  
> > +config THERMAL_POWER_ACTOR
> > +	bool
> > +
> 
> Why empty description/help?

Because it's not an option that users can select.

> >  config CPU_THERMAL
> >  	bool "generic cpu cooling support"
> >  	depends on CPU_FREQ
> > diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> > index 54e4ec9eb5df..878a02cab7d1 100644
> > --- a/drivers/thermal/Makefile
> > +++ b/drivers/thermal/Makefile
> > @@ -14,6 +14,8 @@ thermal_sys-$(CONFIG_THERMAL_GOV_FAIR_SHARE)	+= fair_share.o
> >  thermal_sys-$(CONFIG_THERMAL_GOV_STEP_WISE)	+= step_wise.o
> >  thermal_sys-$(CONFIG_THERMAL_GOV_USER_SPACE)	+= user_space.o
> >  
> > +obj-$(CONFIG_THERMAL_POWER_ACTOR) += power_actor/
> > +
> >  # cpufreq cooling
> >  thermal_sys-$(CONFIG_CPU_THERMAL)	+= cpu_cooling.o
> >  
> > diff --git a/drivers/thermal/power_actor/Makefile b/drivers/thermal/power_actor/Makefile
> > new file mode 100644
> > index 000000000000..46478f4928be
> > --- /dev/null
> > +++ b/drivers/thermal/power_actor/Makefile
> > @@ -0,0 +1,5 @@
> > +#
> > +# Makefile for the power actors
> > +#
> > +
> > +obj-y += power_actor.o
> 
> Why a new directory? Maybe using parent dir is enough?

Ok, I'll move it to the parent directory.

> > diff --git a/drivers/thermal/power_actor/power_actor.c b/drivers/thermal/power_actor/power_actor.c
> > new file mode 100644
> > index 000000000000..d891deb0e2a1
> > --- /dev/null
> > +++ b/drivers/thermal/power_actor/power_actor.c
> > @@ -0,0 +1,64 @@
> > +/*
> > + * Basic interface for power actors
> > + *
> > + * Copyright (C) 2014 ARM Ltd.
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License version 2 as
> > + * published by the Free Software Foundation.
> > + *
> > + * This program is distributed "as is" WITHOUT ANY WARRANTY of any
> > + * kind, whether express or implied; without even the implied warranty
> > + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> > + * GNU General Public License for more details.
> > + */
> > +
> > +#define pr_fmt(fmt) "Power actor: " fmt
> > +
> > +#include <linux/err.h>
> > +#include <linux/list.h>
> > +#include <linux/slab.h>
> > +
> > +#include "power_actor.h"
> > +
> > +LIST_HEAD(actor_list);
> > +
> > +/**
> > + * power_actor_register - Register an actor in the power actor API
> > + * @type:	actor type
> > + * @ops:	struct power_actor_ops for this actor
> > + * @privdata:	pointer to private data related to the actor
> > + *
> > + * Returns the struct power_actor * on success, ERR_PTR() on failure
> > + */
> > +struct power_actor *power_actor_register(enum power_actor_types type,
> > +					struct power_actor_ops *ops,
> > +					void *privdata)
> > +{
> > +	struct power_actor *actor;
> > +
> > +	if (!ops->get_req_power || !ops->get_max_power || !ops->set_power)
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	actor = kzalloc(sizeof(*actor), GFP_KERNEL);
> 
> devm_?

I can't, there's no device.

> > +	if (!actor)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	actor->type = type;
> > +	actor->ops = ops;
> > +	actor->data = privdata;
> > +
> > +	list_add(&actor->actor_node, &actor_list);
> > +
> 
> Do you need to protect this one?

Yes, it needs protection.

> > +	return actor;
> > +}
> > +
> > +/**
> > + * power_actor_unregister - Unregister an actor
> > + * @actor:	the actor to unregister
> > + */
> > +void power_actor_unregister(struct power_actor *actor)
> > +{
> > +	list_del(&actor->actor_node);
> > +	kfree(actor);
> > +}
> > diff --git a/drivers/thermal/power_actor/power_actor.h b/drivers/thermal/power_actor/power_actor.h
> > new file mode 100644
> > index 000000000000..28098f43630b
> > --- /dev/null
> > +++ b/drivers/thermal/power_actor/power_actor.h
> > @@ -0,0 +1,64 @@
> > +/*
> > + * Copyright (C) 2014 ARM Ltd.
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License version 2 as
> > + * published by the Free Software Foundation.
> > + *
> > + * 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.
> > + *
> > + * You should have received a copy of the GNU General Public License
> > + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> > + */
> > +
> > +#ifndef __POWER_ACTOR_H__
> > +#define __POWER_ACTOR_H__
> > +
> > +#include <linux/list.h>
> > +
> > +#define MAX_NUM_ACTORS 8
> > +
> 
> unused define

True, it's a left over, I'll delete it.

> > +enum power_actor_types {
> > +};
> 
> I still don't get this enum.

You will when you look at the next patch ;)

Cheers,
Javi


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFC PATCH v3 5/7] thermal: add a basic cpu power actor
  2014-06-11 12:05   ` Eduardo Valentin
@ 2014-06-12 14:26     ` Javi Merino
  2014-06-13 17:01       ` Javi Merino
  0 siblings, 1 reply; 13+ messages in thread
From: Javi Merino @ 2014-06-12 14:26 UTC (permalink / raw)
  To: Eduardo Valentin; +Cc: linux-pm, linux-kernel, Punit Agrawal, Zhang Rui

On Wed, Jun 11, 2014 at 01:05:37PM +0100, Eduardo Valentin wrote:
> Hello Javi,
> 
> On Tue, Jun 03, 2014 at 11:18:33AM +0100, Javi Merino wrote:
> > Introduce a power actor for cpus.  It has a basic power model to get
> > the current power utilization and uses cpufreq cooling devices to set
> > the desired power.  It uses the current frequency (as reported by
> > cpufreq) as well as load and OPPs for the power calculations.  The
> > cpus must have registered their OPPs in the OPP library.
> >
> > Cc: Zhang Rui <rui.zhang@intel.com>
> > Cc: Eduardo Valentin <edubezval@gmail.com>
> > Signed-off-by: Punit Agrawal <punit.agrawal@arm.com>
> > Signed-off-by: Javi Merino <javi.merino@arm.com>
> > ---
> >  Documentation/thermal/power_actor.txt     | 126 +++++++
> >  drivers/thermal/Kconfig                   |   5 +
> >  drivers/thermal/power_actor/Kconfig       |   9 +
> >  drivers/thermal/power_actor/Makefile      |   2 +
> >  drivers/thermal/power_actor/cpu_actor.c   | 601 ++++++++++++++++++++++++++++++
> >  drivers/thermal/power_actor/power_actor.h |  41 ++
> >  6 files changed, 784 insertions(+)
> >  create mode 100644 drivers/thermal/power_actor/Kconfig
> >  create mode 100644 drivers/thermal/power_actor/cpu_actor.c
> >
> > diff --git a/Documentation/thermal/power_actor.txt b/Documentation/thermal/power_actor.txt
> > index 5a61f32ec143..fd51760615bf 100644
> > --- a/Documentation/thermal/power_actor.txt
> > +++ b/Documentation/thermal/power_actor.txt
> > @@ -36,3 +36,129 @@ temperature.
> >  milliwatts.
> >
> >  Returns 0 on success, -E* on error.
> > +
> > +CPU Power Actor API
> > +===================
> > +
> > +A simple power model for CPUs.  The current power is calculated as
> > +dynamic + (optionally) static power.  This power model requires that
> > +the operating-points of the CPUs are registered using the kernel's opp
> > +library and the `cpufreq_frequency_table` is assigned to the `struct
> > +device` of the cpu.  If you are using the `cpufreq-cpu0.c` driver then
> > +the `cpufreq_frequency_table` should already be assigned to the cpu
> > +device.
> > +
> > +The `tz` and `plat_static_func` parameters of
> > +`power_cpu_actor_register()` are optional.  If you don't provide them,
> > +only dynamic power will be considered.
> > +
> > +Dynamic power
> > +-------------
> > +
> > +The dynamic power consumption of a processor depends
> > +on many factors.  For a given processor implementation the primary
> > +factors are:
> > +
> > +- The time the processor spends running, consuming dynamic power, as
> > +  compared to the time in idle states where dynamic consumption is
> > +  negligible.  Herein we refer to this as 'utilisation'.
> > +- The voltage and frequency levels as a result of DVFS.  The DVFS
> > +  level is a dominant factor governing power consumption.
> > +- In running time the 'execution' behaviour (instruction types, memory
> > +  access patterns and so forth) causes, in most cases, a second order
> > +  variation.  In pathological cases this variation can be significant,
> > +  but typically it is of a much lesser impact than the factors above.
> > +
> > +A high level dynamic power consumption model may then be represented as:
> > +
> > +Pdyn = f(run) * Voltage^2 * Frequency * Utilisation
> > +
> > +f(run) here represents the described execution behaviour and its
> > +result has a units of Watts/Hz/Volt^2 (this often expressed in
> > +mW/MHz/uVolt^2)
> > +
> > +The detailed behaviour for f(run) could be modelled on-line.  However,
> > +in practice, such an on-line model has dependencies on a number of
> > +implementation specific processor support and characterisation
> > +factors.  Therefore, in initial implementation that contribution is
> > +represented as a constant coefficient.  This is a simplification
> > +consistent with the relative contribution to overall power variation.
> > +
> > +In this simplified representation our model becomes:
> > +
> > +Pdyn = Kd * Voltage^2 * Frequency * Utilisation
> > +
> > +Where Kd (capacitance) represents an indicative running time dynamic
> > +power coefficient in fundamental units of mW/MHz/uVolt^2
> > +
> > +Static Power
> > +------------
> > +
> > +Static leakage power consumption depends on a number of factors.  For a
> > +given circuit implementation the primary factors are:
> > +
> > +- Time the circuit spends in each 'power state'
> > +- Temperature
> > +- Operating voltage
> > +- Process grade
> > +
> > +The time the circuit spends in each 'power state' for a given
> > +evaluation period at first order means OFF or ON.  However,
> > +'retention' states can also be supported that reduce power during
> > +inactive periods without loss of context.
> > +
> > +Note: The visibility of state entries to the OS can vary, according to
> > +platform specifics, and this can then impact the accuracy of a model
> > +based on OS state information alone.  It might be possible in some
> > +cases to extract more accurate information from system resources.
> > +
> > +The temperature, operating voltage and process 'grade' (slow to fast)
> > +of the circuit are all significant factors in static leakage power
> > +consumption.  All of these have complex relationships to static power.
> > +
> > +Circuit implementation specific factors include the chosen silicon
> > +process as well as the type, number and size of transistors in both
> > +the logic gates and any RAM elements included.
> > +
> > +The static power consumption modelling must take into account the
> > +power managed regions that are implemented.  Taking the example of an
> > +ARM processor cluster, the modelling would take into account whether
> > +each CPU can be powered OFF separately or if only a single power
> > +region is implemented for the complete cluster.
> > +
> > +In one view, there are others, a static power consumption model can
> > +then start from a set of reference values for each power managed
> > +region (e.g. CPU, Cluster/L2) in each state (e.g. ON, OFF) at an
> > +arbitrary process grade, voltage and temperature point.  These values
> > +are then scaled for all of the following: the time in each state, the
> > +process grade, the current temperature and the operating
> > +voltage.  However, since both implementation specific and complex
> > +relationships dominate the estimate, the appropriate interface to the
> > +model from the cpu power actor is to provide a function callback that
> > +calculates the static power in this platform.  When registering the
> > +power cpu actor, pass the thermal zone closest to the cpu (to get the
> > +temperature) and a function pointer that follows the `get_static_t`
> > +prototype:
> > +
> > +    u32 plat_get_static(cpumask_t *cpumask, unsigned long voltage,
> > +                             unsigned long temperature);
> > +
> > +with `cpumask` a cpumask of the cpus involved in the calculation,
> > +`voltage` the voltage at which they are opperating and `temperature`
> > +their current temperature.
> > +
> > +If both `tz` and `plat_static_func` are NULL when registering the
> > +power cpu actor, static power is considered to be negligible for this
> > +platform and only dynamic power is considered.
> > +
> > +The platform specific callback can then use any combination of tables
> > +and/or equations to permute the estimated value.  Process grade
> > +information is not passed to the model since access to such data, from
> > +on-chip measurement capability or manufacture time data, is platform
> > +specific.
> > +
> > +Note: the significance of static power for CPUs in comparison to
> > +dynamic power is highly dependent on implementation.  Given the
> > +potential complexity in implementation, the importance and accuracy of
> > +its inclusion when using cpu power actors should be assessed on a case by
> > +cases basis.
> 
> In one way or another, we are dealing with models here. Even the above
> dynamic power model is still a model and has errors from reality. The
> point is providing a good default model that works across platforms.

I know, but what we have been told is that there is no formula for
static power that works across platforms.  If you know one, please let
us know and we will add it.
 
> > diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> > index 47e2f15537ca..1818c4fa60b8 100644
> > --- a/drivers/thermal/Kconfig
> > +++ b/drivers/thermal/Kconfig
> > @@ -92,6 +92,11 @@ config THERMAL_GOV_USER_SPACE
> >  config THERMAL_POWER_ACTOR
> >       bool
> >
> > +menu "Power actors"
> > +depends on THERMAL_POWER_ACTOR
> > +source "drivers/thermal/power_actor/Kconfig"
> > +endmenu
> > +
> >  config CPU_THERMAL
> >       bool "generic cpu cooling support"
> >       depends on CPU_FREQ
> > diff --git a/drivers/thermal/power_actor/Kconfig b/drivers/thermal/power_actor/Kconfig
> > new file mode 100644
> > index 000000000000..fa542ca99cdb
> > --- /dev/null
> > +++ b/drivers/thermal/power_actor/Kconfig
> > @@ -0,0 +1,9 @@
> > +#
> > +# Thermal power actor configuration
> > +#
> > +
> > +config THERMAL_POWER_ACTOR_CPU
> > +     bool
> > +     prompt "Simple power model for a CPU"
> > +     help
> > +       A simple CPU power model
> 
> A better help is always welcome.

I guess I can repeat some of the text in the Documentation here.

> > diff --git a/drivers/thermal/power_actor/Makefile b/drivers/thermal/power_actor/Makefile
> > index 46478f4928be..6f04b92997e6 100644
> > --- a/drivers/thermal/power_actor/Makefile
> > +++ b/drivers/thermal/power_actor/Makefile
> > @@ -3,3 +3,5 @@
> >  #
> >
> >  obj-y += power_actor.o
> > +
> > +obj-$(CONFIG_THERMAL_POWER_ACTOR_CPU) += cpu_actor.o
> > diff --git a/drivers/thermal/power_actor/cpu_actor.c b/drivers/thermal/power_actor/cpu_actor.c
> > new file mode 100644
> > index 000000000000..6eac80d119a5
> > --- /dev/null
> > +++ b/drivers/thermal/power_actor/cpu_actor.c
> > @@ -0,0 +1,601 @@
> > +/*
> > + * A basic cpu actor
> > + *
> > + * Copyright (C) 2014 ARM Ltd.
> > + *
> > + * This program is free software; you can redistribute it and/or modify
> > + * it under the terms of the GNU General Public License version 2 as
> > + * published by the Free Software Foundation.
> > + *
> > + * This program is distributed "as is" WITHOUT ANY WARRANTY of any
> > + * kind, whether express or implied; without even the implied warranty
> > + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> > + * GNU General Public License for more details.
> > + */
> > +
> > +#define pr_fmt(fmt) "CPU actor: " fmt
> > +
> > +#include <linux/cpu.h>
> > +#include <linux/cpufreq.h>
> > +#include <linux/cpumask.h>
> > +#include <linux/cpu_cooling.h>
> > +#include <linux/device.h>
> > +#include <linux/err.h>
> > +#include <linux/init.h>
> > +#include <linux/list.h>
> > +#include <linux/pm_opp.h>
> > +#include <linux/printk.h>
> > +#include <linux/slab.h>
> > +
> > +#include "power_actor.h"
> > +
> > +/**
> > + * struct power_table - frequency to power conversion
> > + * @frequency:       frequency in KHz
> > + * @power:   power in mW
> > + *
> > + * This structure is built when the cooling device registers and helps
> > + * in translating frequency to power and viceversa.
> > + */
> > +struct power_table {
> > +     u32 frequency;
> > +     u32 power;
> > +};
> > +
> > +/**
> > + * struct cpu_actor - information for each cpu actor
> > + * @cpumask: cpus covered by this actor
> > + * @freq:    frequency in KHz of the cpus represented by the cooling device
> > + * @last_load:       load measured by the latest call to cpu_get_req_power()
> > + * @time_in_idle:    previous reading of the absolute time that this cpu was
> > + *                   idle
> > + * @time_in_idle_timestamp: wall time of the last invocation of
> > + *                       get_cpu_idle_time_us()
> > + * @dyn_power_table: array of struct power_table for frequency to power
> > + *                   conversion
> > + * @dyn_power_table_entries: number of entries in the @dyn_power_table array
> > + * @cdev:    cpufreq cooling device associated with this actor
> > + * @tz:              thermal zone for the sensor closest to the cpus
> > + * @plat_get_static_power:   callback to calculate the static power
> > + */
> > +struct cpu_actor {
> > +     cpumask_t cpumask;
> > +     u32 freq;
> > +     u32 last_load;
> > +     u64 time_in_idle[NR_CPUS];
> > +     u64 time_in_idle_timestamp[NR_CPUS];
> > +     struct power_table *dyn_power_table;
> > +     int dyn_power_table_entries;
> > +     struct thermal_cooling_device *cdev;
> > +     struct thermal_zone_device *tz;
> > +     get_static_t plat_get_static_power;
> > +};
> > +
> > +static DEFINE_MUTEX(cpu_power_actor_lock);
> > +
> > +static unsigned int cpu_power_actors_registered;
> > +
> > +static u32 cpu_freq_to_power(struct cpu_actor *cpu_actor, u32 freq)
> > +{
> > +     int i;
> > +     struct power_table *pt = cpu_actor->dyn_power_table;
> > +
> > +     for (i = 0; i < cpu_actor->dyn_power_table_entries - 1; i++)
> > +             if (freq <= pt[i].frequency)
> > +                     break;
> > +
> > +     return pt[i].power;
> > +}
> > +
> > +static u32 cpu_power_to_freq(struct cpu_actor *cpu_actor, u32 power)
> > +{
> > +     int i;
> > +     struct power_table *pt = cpu_actor->dyn_power_table;
> > +
> > +     for (i = 0; i < cpu_actor->dyn_power_table_entries - 1; i++)
> > +             if (power <= pt[i].power)
> > +                     break;
> > +
> > +     return pt[i].frequency;
> > +}
> > +
> > +/**
> > + * get_load - get load for a cpu since last updated
> > + * @cpu_actor: struct cpu_actor for this actor
> > + * @cpu: cpu number
> > + *
> > + * Return the average load of cpu @cpu in percentage since this
> > + * function was last called.
> > + */
> > +static u32 get_load(struct cpu_actor *cpu_actor, int cpu)
> > +{
> > +     u32 load;
> > +     u64 now, now_idle, delta_time, delta_idle;
> > +
> > +     now_idle = get_cpu_idle_time(cpu, &now, 0);
> > +     delta_idle = now_idle - cpu_actor->time_in_idle[cpu];
> > +     delta_time = now - cpu_actor->time_in_idle_timestamp[cpu];
> > +
> > +     if (delta_time <= delta_idle)
> > +             load = 0;
> > +     else
> > +             load = div64_u64(100 * (delta_time - delta_idle), delta_time);
> > +
> > +     cpu_actor->time_in_idle[cpu] = now_idle;
> > +     cpu_actor->time_in_idle_timestamp[cpu] = now;
> > +
> > +     return load;
> > +}
> > +
> > +/**
> > + * __get_static_power() - calculate the static power consumed by the cpus
> > + * @cpu_actor:       &struct cpu_actor for this cpu
> > + * @freq:    frequency in KHz
> > + *
> > + * Calculate the static power consumed by the cpus described by
> > + * @cpu_actor running at frequency @freq.  This function relies on a
> > + * platform specific function that should have been provided when the
> > + * actor was registered.  If it wasn't, the static power is assumed to
> > + * be negligible.
> > + *
> > + * Return: The static power consumed by the cpus.  It returns 0 on
> > + * error or if there is no plat_get_static_power().
> > + */
> > +static u32 __get_static_power(struct cpu_actor *cpu_actor, unsigned long freq)
> > +{
> > +     int err;
> > +     struct device *cpu_dev;
> > +     struct dev_pm_opp *opp;
> > +     unsigned long voltage, temperature;
> > +     cpumask_t *cpumask = &cpu_actor->cpumask;
> > +     unsigned long freq_hz = freq * 1000;
> > +
> > +     if (!cpu_actor->plat_get_static_power)
> > +             return 0;
> > +
> > +     if (freq == 0)
> > +             return 0;
> > +
> > +     cpu_dev = get_cpu_device(cpumask_any(cpumask));
> > +
> > +     rcu_read_lock();
> > +
> > +     opp = dev_pm_opp_find_freq_exact(cpu_dev, freq_hz, true);
> > +     voltage = dev_pm_opp_get_voltage(opp);
> > +
> > +     rcu_read_unlock();
> > +
> > +     if (voltage == 0) {
> > +             dev_warn_ratelimited(cpu_dev,
> > +                             "Failed to get voltage for frequency %lu: %ld\n",
> > +                             freq_hz, IS_ERR(opp) ? PTR_ERR(opp) : 0);
> > +             return 0;
> > +     }
> > +
> > +     err = thermal_zone_get_temp(cpu_actor->tz, &temperature);
> > +     if (err) {
> > +             dev_warn(&cpu_actor->tz->device, "Unable to read temperature: %d\n",
> > +                     err);
> > +             return 0;
> > +     }
> > +
> > +     return cpu_actor->plat_get_static_power(cpumask, voltage, temperature);
> 
> How about if this could be optional? That is, something like:

It's already covered, see the begginning of the function, we return 0
if the cpu_actor->plat_get_static_power is NULL.

> +       if (cpu_actor->plat_get_static_power)
> +               return cpu_actor->plat_get_static_power(cpumask, voltage, temperature);
> +       else
> +               return default_static_power(cpumask, voltage, temperature);
> 
> > +}
> > +
> > +static u32 get_static_power(struct cpu_actor *cpu_actor)
> > +{
> > +     return __get_static_power(cpu_actor, cpu_actor->freq);
> > +}
> > +
> > +/**
> > + * get_dynamic_power - calculate the dynamic power
> > + * @cpu_actor: cpu_actor pointer
> > + *
> > + * Return the dynamic power consumed by the cpus described by
> > + * cpu_actor.
> > + */
> > +static u32 get_dynamic_power(struct cpu_actor *cpu_actor)
> > +{
> > +     int cpu;
> > +     u32 power = 0, raw_cpu_power, total_load = 0;
> > +     raw_cpu_power = cpu_freq_to_power(cpu_actor, cpu_actor->freq);
> > +
> > +     for_each_cpu(cpu, &cpu_actor->cpumask) {
> > +             u32 load;
> > +
> > +             if (!cpu_online(cpu))
> > +                     continue;
> > +
> > +             load = get_load(cpu_actor, cpu);
> > +             power += (raw_cpu_power * load) / 100;
> > +             total_load += load;
> > +     }
> > +
> > +     cpu_actor->last_load = total_load;
> > +
> > +     return power;
> > +}
> > +
> > +/**
> > + * cpu_get_req_power - get the current power
> > + * @actor:   power actor pointer
> > + *
> > + * Callback for the power actor to return the current power
> > + * consumption in milliwatts.
> > + */
> > +static u32 cpu_get_req_power(struct power_actor *actor)
> > +{
> > +     u32 static_power, dynamic_power;
> > +     struct cpu_actor *cpu_actor = actor->data;
> > +
> > +     static_power = get_static_power(cpu_actor);
> > +     dynamic_power = get_dynamic_power(cpu_actor);
> > +
> > +     return static_power + dynamic_power;
> > +}
> > +
> > +/**
> > + * cpu_get_max_power() - get the maximum power that the cpu could currently consume
> > + * @actor:   power actor pointer
> > + *
> > + * Callback for the power actor to return the maximum power
> > + * consumption in milliwatts that the cpu could currently consume.
> > + * The static power depends on temperature so the maximum power will
> > + * vary over time.
> > + */
> > +static u32 cpu_get_max_power(struct power_actor *actor)
> > +{
> > +     u32 max_static_power, max_dyn_power;
> > +     cpumask_t *cpumask;
> > +     unsigned int max_freq, last_entry, num_cpus;
> > +     struct cpu_actor *cpu_actor = actor->data;
> > +
> > +     cpumask = &cpu_actor->cpumask;
> > +     max_freq = cpufreq_quick_get_max(cpumask_any(cpumask));
> > +     max_static_power = __get_static_power(cpu_actor, max_freq);
> > +
> > +     last_entry = cpu_actor->dyn_power_table_entries - 1;
> > +     num_cpus = cpumask_weight(cpumask);
> > +     max_dyn_power = cpu_actor->dyn_power_table[last_entry].power * num_cpus;
> > +
> > +     return max_static_power + max_dyn_power;
> > +}
> > +
> > +/**
> > + * cpu_set_power - set cpufreq cooling device to consume a certain power
> > + * @actor: power actor pointer
> > + * @power: the power in milliwatts that should be set
> > + *
> > + * Callback for the power actor to configure the power consumption of
> > + * the CPU to be @power milliwatts at most.  This function assumes
> > + * that the load will remain constant.  The power is translated into a
> > + * cooling state that the cpu cooling device then sets.
> > + *
> > + * Returns 0 on success, -EINVAL if it couldn't convert the frequency
> > + * to a cpufreq cooling device state.
> > + */
> > +static int cpu_set_power(struct power_actor *actor, u32 power)
> > +{
> > +     unsigned int cpu, freq;
> > +     unsigned long cdev_state;
> > +     u32 dyn_power, normalised_power, last_load;
> > +     struct thermal_cooling_device *cdev;
> > +     struct cpu_actor *cpu_actor = actor->data;
> > +
> > +     cdev = cpu_actor->cdev;
> > +
> > +     dyn_power = power - get_static_power(cpu_actor);
> > +     cpu = cpumask_any(&cpu_actor->cpumask);
> > +     last_load = cpu_actor->last_load ? cpu_actor->last_load : 1;
> > +     normalised_power = (dyn_power * 100) / last_load;
> > +     freq = cpu_power_to_freq(cpu_actor, normalised_power);
> > +
> > +     cdev_state = cpufreq_cooling_get_level(cpu, freq);
> > +     if (cdev_state == THERMAL_CSTATE_INVALID) {
> > +             pr_err("Failed to convert %dKHz for cpu %d into a cdev state\n",
> > +                     freq, cpu);
> > +             return -EINVAL;
> > +     }
> > +
> > +     return cdev->ops->set_cur_state(cdev, cdev_state);
> > +}
> > +
> > +static struct power_actor_ops cpu_actor_ops = {
> > +     .get_req_power = cpu_get_req_power,
> > +     .get_max_power = cpu_get_max_power,
> > +     .set_power = cpu_set_power,
> > +};
> 
> Why the above cannot be done with current thermal API?

Our v1 of the patches used the cooling device API by adding "units" to
it and we ended up stretching the abstraction too much.  The two main
problems are:

1.  get_max_state() gives you the maximum cooling state which, for
    passive devices, is the minimum performance (frequency in case of
    cpufreq cdev).  get_max_power() gives you the maximum power, which
    gives you the maximum performance (frequency in the case of CPUs,
    GPUs and buses)

2.  Some power actors need additional parameters.  For example, to
    calculate the static power consumed by the cpu you need to know
    its temperature.  You can do that by registering the thermal zone
    device that's closest to the cpu (what we're doing now) or by
    passing the thermal zone device in every call to get_req_power(),
    get_max_power() and set_power().  I'm unsure whether the latter
    would be better, but again is something that the cooling device
    API doesn't allow us to do.

> > +
> > +/**
> > + * power_cpu_actor_add_static() - add static information to an existing cpu actor
> > + * @cpu:     a cpu represented by the cpu power actor
> > + * @tz:              pointer to the closest thermal zone to the cpu
> > + * @plat_static_func:        function to calculate the static power consumed by these
> > + *                   cpus
> > + *
> > + * A function to add static information to an actor after it has
> > + * registered.  In order to calculate the static power, we need the to
> > + * know the temperature of the cpu, which we get from its closest
> > + * thermal zone.  It is possible that when registering the
> > + * power_cpu_actor, the thermal zone hasn't been registered yet.  This
> > + * function allows you to add that information when the thermal zone
> > + * is registered.  @cpu should be one of the cpus represented by the
> > + * cpu actor you want to modify
> > + *
> > + * Return: 0 on success.  -EINVAL if @tz or @plat_static_func are
> > + * NULL.  -ENOENT if there's no cpu power actor for @cpu.
> > + */
> > +int power_cpu_actor_add_static(unsigned int cpu, struct thermal_zone_device *tz,
> > +                     get_static_t plat_static_func)
> > +{
> > +     struct power_actor *actor;
> > +
> > +     if (!plat_static_func || !tz)
> > +             return -EINVAL;
> > +
> > +     list_for_each_entry(actor, &actor_list, actor_node) {
> > +             struct cpu_actor *cpu_actor;
> > +
> > +             if (actor->type != POWER_ACTOR_CPU)
> > +                     continue;
> > +
> > +             cpu_actor = actor->data;
> > +
> > +             if (cpumask_test_cpu(cpu, &cpu_actor->cpumask)) {
> > +                     cpu_actor->tz = tz;
> > +                     cpu_actor->plat_get_static_power = plat_static_func;
> > +                     return 0;
> > +             }
> > +     }
> > +
> > +     return -ENOENT;
> > +}
> > +
> > +/**
> > + * cpufreq_frequency_change - notifier callback for cpufreq frequency changes
> > + * @nb:              struct notifier_block * with callback info
> > + * @event:   value showing cpufreq event for which this function invoked
> > + * @data:    callback-specific data
> > + *
> > + * Callback to get notifications of frequency changes.  In the
> > + * CPUFREQ_POSTCHANGE @event we store the new frequency so that
> > + * cpufreq_get_cur() knows the current frequency and can convert it
> > + * into power.
> > + */
> > +static int cpufreq_frequency_change(struct notifier_block *nb,
> > +                             unsigned long event, void *data)
> > +{
> > +     struct power_actor *actor;
> > +     struct cpufreq_freqs *freqs = data;
> > +
> > +     /* Only update frequency on postchange */
> > +     if (event != CPUFREQ_POSTCHANGE)
> > +             return NOTIFY_DONE;
> > +
> > +     list_for_each_entry(actor, &actor_list, actor_node) {
> > +             struct cpu_actor *cpu_actor;
> > +
> > +             if (actor->type != POWER_ACTOR_CPU)
> > +                     continue;
> > +
> > +             cpu_actor = actor->data;
> > +
> > +             if (cpumask_test_cpu(freqs->cpu, &cpu_actor->cpumask))
> > +                     cpu_actor->freq = freqs->new;
> > +     }
> > +
> > +     return NOTIFY_OK;
> > +}
> > +
> > +struct notifier_block cpufreq_transition_notifier = {
> > +     .notifier_call = cpufreq_frequency_change,
> > +};
> > +
> > +/**
> > + * build_dyn_power_table - create a dynamic power to frequency table
> > + * @cpu_actor:       the cpu_actor in which to store the table
> > + * @capacitance: dynamic power coefficient for these cpus
> > + *
> > + * Build a dynamic power to frequency table for this cpu and store it
> > + * in @cpu_actor.  This table will be used in cpu_power_to_freq() and
> > + * cpu_freq_to_power() to convert between power and frequency
> > + * efficiently.  Power is stored in mW, frequency in KHz.  The
> > + * resulting table is in ascending order.
> > + *
> > + * Returns 0 on success, -E* on error.
> > + */
> > +static int build_dyn_power_table(struct cpu_actor *cpu_actor, u32 capacitance)
> > +{
> > +     struct power_table *power_table;
> > +     struct dev_pm_opp *opp;
> > +     struct device *dev = NULL;
> > +     int num_opps, cpu, i, ret = 0;
> > +     unsigned long freq;
> > +
> > +     num_opps = 0;
> > +
> > +     rcu_read_lock();
> > +
> > +     for_each_cpu(cpu, &cpu_actor->cpumask) {
> > +             dev = get_cpu_device(cpu);
> > +             if (!dev)
> > +                     continue;
> > +
> > +             num_opps = dev_pm_opp_get_opp_count(dev);
> > +             if (num_opps > 0) {
> > +                     break;
> > +             } else if (num_opps < 0) {
> > +                     ret = num_opps;
> > +                     goto unlock;
> > +             }
> > +     }
> > +
> > +     if (num_opps == 0) {
> > +             ret = -EINVAL;
> > +             goto unlock;
> > +     }
> > +
> > +     power_table = kcalloc(num_opps, sizeof(*power_table), GFP_KERNEL);
> > +
> > +     i = 0;
> > +     for (freq = 0;
> > +          opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp);
> > +          freq++) {
> > +             u32 freq_mhz, voltage_mv;
> > +             u64 power;
> > +
> > +             freq_mhz = freq / 1000000;
> > +             voltage_mv = dev_pm_opp_get_voltage(opp) / 1000;
> > +
> > +             /*
> > +              * Do the multiplication with MHz and millivolt so as
> > +              * to not overflow.
> > +              */
> > +             power = (u64)capacitance * freq_mhz * voltage_mv * voltage_mv;
> > +             do_div(power, 1000000000);
> > +
> > +             /* frequency is stored in power_table in KHz */
> > +             power_table[i].frequency = freq / 1000;
> > +             power_table[i].power = power;
> > +
> > +             i++;
> > +     }
> > +
> > +     if (i == 0) {
> > +             ret = PTR_ERR(opp);
> > +             goto unlock;
> > +     }
> > +
> > +     cpu_actor->dyn_power_table = power_table;
> > +     cpu_actor->dyn_power_table_entries = i;
> > +
> > +unlock:
> > +     rcu_read_unlock();
> > +     return ret;
> > +}
> > +
> > +/**
> > + * power_cpu_actor_register - register a cpu_actor within the power actor API
> > + * @cpumask: cpumask of cpus covered by this power_actor
> > + * @tz:              thermal zone closest to the cpus (optional)
> > + * @capacitance: dynamic power coefficient for these cpus
> > + * @plat_static_func:        function to calculate the static power consumed by these
> > + *                   cpus (optional)
> > + *
> > + * Create a cpufreq cooling device for the cpus in @cpumask and
> > + * register it with the power actor API using a simple cpu power
> > + * model.  The cpus must have registered their OPPs in the OPP
> > + * library.
> > + *
> > + * An optional @plat_static_func may be provided to calculate the
> > + * static power consumed by these cpus.  If the platform's static
> > + * power consumption is unknown or negligible, make it NULL.  If
> > + * @plat_static_fun is specified, @tz needs to be specified as well.
> > + * @tz should be a pointer to the thermal_zone_device whose sensor is
> > + * closer to the cpus in @cpumask.
> > + *
> > + * Return the power_actor created on success or the corresponding
> > + * ERR_PTR() on failure.  This actor should be freed with
> > + * power_cpu_actor_unregister() when it's no longer needed.
> > + */
> > +struct power_actor *
> > +power_cpu_actor_register(cpumask_t *cpumask,
> > +                     struct thermal_zone_device *tz,
> > +                     u32 capacitance,
> > +                     get_static_t plat_static_func)
> > +{
> > +     int ret;
> > +     struct thermal_cooling_device *cdev;
> > +     struct device *cpu_dev;
> > +     struct power_actor *actor, *err_ret;
> > +     struct cpu_actor *cpu_actor;
> > +
> > +     if (plat_static_func && !tz) {
> > +             pr_warn("plat_static_func provided without a thermal zone, ignoring it\n");
> > +             plat_static_func = NULL;
> 
> 
> You either make this one hard requirement (and do not fail gracefuly
> like above) or you make it really optional and provide a good default
> formula (which is preferrable).

It's really optional.  If the function is NULL, the platform's static
power consumption is assumed to be negligible.

> > +     }
> > +
> > +     cpu_dev = get_cpu_device(cpumask_any(cpumask));
> > +     if (!cpu_dev || !cpu_dev->of_node)
> > +             cdev = cpufreq_cooling_register(cpumask);
> > +     else
> > +             cdev = of_cpufreq_cooling_register(cpu_dev->of_node, cpumask);
> > +
> > +     if (!cdev)
> > +             return ERR_PTR(PTR_ERR(cdev));
> > +
> > +     cpu_actor = kzalloc(sizeof(*cpu_actor), GFP_KERNEL);
> > +     if (!cpu_actor) {
> > +             err_ret = ERR_PTR(-ENOMEM);
> > +             goto cdev_unregister;
> > +     }
> > +
> > +     cpumask_copy(&cpu_actor->cpumask, cpumask);
> > +     cpu_actor->cdev = cdev;
> > +     cpu_actor->tz = tz;
> > +     cpu_actor->plat_get_static_power = plat_static_func;
> > +
> > +     ret = build_dyn_power_table(cpu_actor, capacitance);
> > +     if (ret) {
> > +             err_ret = ERR_PTR(ret);
> > +             goto kfree;
> > +     }
> > +
> > +     actor = power_actor_register(POWER_ACTOR_CPU, &cpu_actor_ops,
> > +                             cpu_actor);
> > +     if (IS_ERR(actor)) {
> > +             err_ret = actor;
> > +             goto kfree;
> > +     }
> > +
> > +     mutex_lock(&cpu_power_actor_lock);
> > +
> > +     /*
> > +      * You can't register multiple times the same notifier_block.
> > +      * The first power actor registered is the only one that
> > +      * registers the notifier.
> > +      */
> > +     if (!cpu_power_actors_registered) {
> > +             ret = cpufreq_register_notifier(&cpufreq_transition_notifier,
> > +                                             CPUFREQ_TRANSITION_NOTIFIER);
> > +             if (ret) {
> > +                     err_ret = ERR_PTR(ret);
> > +                     mutex_unlock(&cpu_power_actor_lock);
> > +                     goto power_actor_unregister;
> > +             }
> > +     }
> > +
> > +     cpu_power_actors_registered++;
> > +     mutex_unlock(&cpu_power_actor_lock);
> > +
> > +     return actor;
> > +
> > +power_actor_unregister:
> > +     power_actor_unregister(actor);
> > +kfree:
> > +     kfree(cpu_actor);
> > +cdev_unregister:
> > +     cpufreq_cooling_unregister(cdev);
> > +
> > +     return err_ret;
> > +}
> > +
> > +void power_cpu_actor_unregister(struct power_actor *actor)
> > +{
> > +     struct cpu_actor *cpu_actor = actor->data;
> > +
> > +     kfree(cpu_actor->dyn_power_table);
> > +
> > +     mutex_lock(&cpu_power_actor_lock);
> > +
> > +     cpu_power_actors_registered--;
> > +
> > +     if (!cpu_power_actors_registered)
> > +             cpufreq_unregister_notifier(&cpufreq_transition_notifier,
> > +                                     CPUFREQ_TRANSITION_NOTIFIER);
> > +
> > +     mutex_unlock(&cpu_power_actor_lock);
> > +
> > +     kfree(cpu_actor);
> > +     power_actor_unregister(actor);
> > +}
> > diff --git a/drivers/thermal/power_actor/power_actor.h b/drivers/thermal/power_actor/power_actor.h
> > index 28098f43630b..230317c284b2 100644
> > --- a/drivers/thermal/power_actor/power_actor.h
> > +++ b/drivers/thermal/power_actor/power_actor.h
> > @@ -17,11 +17,16 @@
> >  #ifndef __POWER_ACTOR_H__
> >  #define __POWER_ACTOR_H__
> >
> > +#include <linux/cpumask.h>
> > +#include <linux/device.h>
> > +#include <linux/err.h>
> >  #include <linux/list.h>
> > +#include <linux/thermal.h>
> >
> >  #define MAX_NUM_ACTORS 8
> >
> >  enum power_actor_types {
> > +     POWER_ACTOR_CPU,
> 
> Using struct device is more scalable no? What if we want to provide a
> power actor for a specific bus, or device, or coprocessor? Are we going
> to maintain this enum for every single new user?

The counterargument would be, does every single device need to carry
this?  Anyway, I'll see if we can add the power actor to the "struct
device".

> Another point to think is if we are having more than one user of power
> actor per device (type).

Some of our testing is done on big.LITTLE systems, which have two cpu
actors: one for the big CPUs and one for the little ones.  So this is
already covered.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RFC PATCH v3 5/7] thermal: add a basic cpu power actor
  2014-06-12 14:26     ` Javi Merino
@ 2014-06-13 17:01       ` Javi Merino
  0 siblings, 0 replies; 13+ messages in thread
From: Javi Merino @ 2014-06-13 17:01 UTC (permalink / raw)
  To: Eduardo Valentin; +Cc: linux-pm, linux-kernel, Punit Agrawal, Zhang Rui

On Thu, Jun 12, 2014 at 03:26:50PM +0100, Javi Merino wrote:
> On Wed, Jun 11, 2014 at 01:05:37PM +0100, Eduardo Valentin wrote:
> > On Tue, Jun 03, 2014 at 11:18:33AM +0100, Javi Merino wrote:
> > > Introduce a power actor for cpus.  It has a basic power model to get
> > > the current power utilization and uses cpufreq cooling devices to set
> > > the desired power.  It uses the current frequency (as reported by
> > > cpufreq) as well as load and OPPs for the power calculations.  The
> > > cpus must have registered their OPPs in the OPP library.
> > >
> > > Cc: Zhang Rui <rui.zhang@intel.com>
> > > Cc: Eduardo Valentin <edubezval@gmail.com>
> > > Signed-off-by: Punit Agrawal <punit.agrawal@arm.com>
> > > Signed-off-by: Javi Merino <javi.merino@arm.com>
> > > ---
> > >  Documentation/thermal/power_actor.txt     | 126 +++++++
> > >  drivers/thermal/Kconfig                   |   5 +
> > >  drivers/thermal/power_actor/Kconfig       |   9 +
> > >  drivers/thermal/power_actor/Makefile      |   2 +
> > >  drivers/thermal/power_actor/cpu_actor.c   | 601 ++++++++++++++++++++++++++++++
> > >  drivers/thermal/power_actor/power_actor.h |  41 ++
> > >  6 files changed, 784 insertions(+)
> > >  create mode 100644 drivers/thermal/power_actor/Kconfig
> > >  create mode 100644 drivers/thermal/power_actor/cpu_actor.c
> > >
[...]
> > > diff --git a/drivers/thermal/power_actor/Kconfig b/drivers/thermal/power_actor/Kconfig
> > > new file mode 100644
> > > index 000000000000..fa542ca99cdb
> > > --- /dev/null
> > > +++ b/drivers/thermal/power_actor/Kconfig
> > > @@ -0,0 +1,9 @@
> > > +#
> > > +# Thermal power actor configuration
> > > +#
> > > +
> > > +config THERMAL_POWER_ACTOR_CPU
> > > +     bool
> > > +     prompt "Simple power model for a CPU"
> > > +     help
> > > +       A simple CPU power model
> >
> > A better help is always welcome.
> 
> I guess I can repeat some of the text in the Documentation here.

I've been thinking about it and I'd rather remove the prompt
altogether.  There's no point in asking the user about this, drivers
that use this API can select THERMAL_POWER_ACTOR_CPU themselves.  So
I'll remove the help text.

[...]
> > > diff --git a/drivers/thermal/power_actor/power_actor.h b/drivers/thermal/power_actor/power_actor.h
> > > index 28098f43630b..230317c284b2 100644
> > > --- a/drivers/thermal/power_actor/power_actor.h
> > > +++ b/drivers/thermal/power_actor/power_actor.h
> > > @@ -17,11 +17,16 @@
> > >  #ifndef __POWER_ACTOR_H__
> > >  #define __POWER_ACTOR_H__
> > >
> > > +#include <linux/cpumask.h>
> > > +#include <linux/device.h>
> > > +#include <linux/err.h>
> > >  #include <linux/list.h>
> > > +#include <linux/thermal.h>
> > >
> > >  #define MAX_NUM_ACTORS 8
> > >
> > >  enum power_actor_types {
> > > +     POWER_ACTOR_CPU,
> >
> > Using struct device is more scalable no? What if we want to provide a
> > power actor for a specific bus, or device, or coprocessor? Are we going
> > to maintain this enum for every single new user?
> 
> The counterargument would be, does every single device need to carry
> this?  Anyway, I'll see if we can add the power actor to the "struct
> device".

I've decided to remove the only user of the power_actor_types enum and
remove this field as well.  It's simplifies things which is always
good.

Cheers,
Javi


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-06-13 17:01 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-03 10:18 [RFC PATCH v3 0/7] The power allocator thermal governor Javi Merino
2014-06-03 10:18 ` [RFC PATCH v3 1/7] tracing: Add __bitmask() macro to trace events to cpumasks and other bitmasks Javi Merino
2014-06-03 10:18 ` [RFC PATCH v3 2/7] thermal: document struct thermal_zone_device and thermal_governor Javi Merino
2014-06-03 10:18 ` [RFC PATCH v3 3/7] thermal: let governors have private data for each thermal zone Javi Merino
2014-06-03 10:18 ` [RFC PATCH v3 4/7] thermal: introduce the Power Actor API Javi Merino
2014-06-11 11:32   ` Eduardo Valentin
2014-06-12 13:45     ` Javi Merino
2014-06-03 10:18 ` [RFC PATCH v3 5/7] thermal: add a basic cpu power actor Javi Merino
2014-06-11 12:05   ` Eduardo Valentin
2014-06-12 14:26     ` Javi Merino
2014-06-13 17:01       ` Javi Merino
2014-06-03 10:18 ` [RFC PATCH v3 6/7] thermal: introduce the Power Allocator governor Javi Merino
2014-06-03 10:18 ` [RFC PATCH v3 7/7] thermal: add trace events to the power allocator governor Javi Merino

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.