summaryrefslogtreecommitdiff
path: root/drivers/cpuidle/governors/irq.c
blob: a5a322f051b4d7b457bb2ad3b43f2bad5c7e0569 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * drivers/cpuidle/governors/irq.c - the irq governor
 *
 * Copyright (C) 2016, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
 *
 * 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.
 *
 */
#include <linux/cpuidle.h>
#include <linux/interrupt.h>
#include <linux/ktime.h>
#include <linux/pm_qos.h>
#include <linux/sched.h>
#include <linux/tick.h>

#include "../cpuidle.h"

static int irq_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
{
	int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
	u64 now = local_clock();
	u64 next_irq_event = irq_timings_next_event(now);
	s64 next_timer_event = ktime_to_us(tick_nohz_get_sleep_length());
	s64 next_event;
	s64 diff = S64_MAX;
	int i, state = CPUIDLE_DRIVER_STATE_START;

	/*
	 * If there are no interrupt supposed to happen on this CPU,
	 * then we rely on the timer expiration.
	 *
	 * Otherwise, irqt_get_next_prediction() returns when is
	 * supposed to happen the next event in absolute time, so we
	 * have to substract the current time to have the duration of
	 * the sleep and convert it in usec.
	 */
	if (next_irq_event != U64_MAX) {

		diff = next_irq_event - now;

		/*
		 * The event already happen, we can't fail to select a
		 * state because the returned value is used as an
		 * index. Return the shallowest state and let the
		 * cpuidle code to check a need_resched() before
		 * entering idle.
		 */
		if (diff < 0)
			return CPUIDLE_DRIVER_STATE_START;

		/*
		 * Convert into microsecond, shifting by 10 (div by 1024)
		 * is enough precise for our purpose.
		 */
		diff >>= 10;
	}

	next_event = min(next_timer_event, diff);

	/*
	 * Find the idle state with the lowest power while satisfying
	 * our constraints. Never, never, default to the polling state
	 * (x86 specific), the shallowest state's exit latency is
	 * small enough to give fast response. The polling state must
	 * be selected before based on deterministic information
	 * (timer expiration or high irq rate).
	 */
	for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) {
		struct cpuidle_state *s = &drv->states[i];
		struct cpuidle_state_usage *su = &dev->states_usage[i];

		if (s->disabled || su->disable)
			continue;
		if (s->target_residency > next_event)
			continue;
		if (s->exit_latency > latency_req)
			continue;

		state = i;
	}

	return state;
}

static int irq_enable(struct cpuidle_driver *drv,
		      struct cpuidle_device *dev)
{
	irq_timings_enable();

	return 0;
}

static void irq_disable(struct cpuidle_driver *drv,
			struct cpuidle_device *dev)
{
	irq_timings_disable();
}

static struct cpuidle_governor irq_governor = {
	.name   = "irq",
	.rating = 10,
	.select = irq_select,
	.enable = irq_enable,
	.disable = irq_disable,
};

static int __init irq_init(void)
{
	return cpuidle_register_governor(&irq_governor);
}

postcore_initcall(irq_init);