summaryrefslogtreecommitdiff
path: root/drivers/edp/edp_temporal.c
blob: 89fc0739367cc74342f699ecb81402a26899cd52 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/edp.h>
#include <linux/slab.h>
#include "edp_internal.h"

/*
 * This file implements the (1) LRR: least recently requested (2) MRR:
 * most recently requested and (3) RR: round robin governors.
 *
 * Since they are all based on some timestamps, we use a simple list
 * (the 'temporal list') for ordering the clients according to the main
 * selection criteria. This list is maintained in such a way that
 * throttle-victims appear at the tail and promotions are done from the
 * head.
 */

static void throttle(struct edp_client *client);

/*  Temporal list is manager specific */
static int temporal_start(struct edp_manager *m)
{
	struct list_head *head;
	struct edp_client *c;

	head = kzalloc(sizeof(*head), GFP_KERNEL);
	if (!head)
		return -ENOMEM;

	INIT_LIST_HEAD(head);
	m->gov_data = head;

	list_for_each_entry(c, &m->clients, link) {
		if (req_index(c) < c->e0_index)
			list_add(&c->glnk, head);
	}

	return 0;
}

static void temporal_stop(struct edp_manager *m)
{
	kfree(m->gov_data);
	m->gov_data = NULL;
}

/*
 * We need to remember only those clients that can either be throttled
 * or promoted - this way, we have a smaller list.
 */
static void lrr_update_request(struct edp_client *client,
		const unsigned int *req)
{
	struct list_head *head;

	if (req_index(client) < client->e0_index)
		list_del(&client->glnk);

	edp_default_update_request(client, req, throttle);

	if (req_index(client) < client->e0_index) {
		head = client->manager->gov_data;
		list_add(&client->glnk, head);
	}
}

/*
 * We need to remember only those clients that can either be throttled
 * or promoted - this way, we have a smaller list.
 */
static void mrr_update_request(struct edp_client *client,
		const unsigned int *req)
{
	struct list_head *head;

	if (req_index(client) < client->e0_index)
		list_del(&client->glnk);

	edp_default_update_request(client, req, throttle);

	if (req_index(client) < client->e0_index) {
		head = client->manager->gov_data;
		list_add_tail(&client->glnk, head);
	}
}

static void rr_update_request(struct edp_client *client,
		const unsigned int *req)
{
	struct list_head *head;

	/* new entry */
	if (!client->req) {
		head = client->manager->gov_data;
		list_add(&client->glnk, head);
	}

	edp_default_update_request(client, req, throttle);
}

static void temporal_promote(struct edp_manager *m)
{
	struct list_head *head = m->gov_data;
	struct edp_client *c;
	unsigned int i;

	list_for_each_entry(c, head, glnk) {
		if (req_level(c) <= cur_level(c) || !c->notify_promotion)
			continue;

		i = edp_promotion_point(c, m->remaining);
		if (i == cur_index(c))
			continue;

		m->remaining -= c->states[i] - cur_level(c);
		c->cur = c->states + i;
		if (c->cur == c->req)
			m->num_denied--;

		c->notify_promotion(i);
		if (!m->remaining || !m->num_denied)
			return;
	}
}

#define DEFINE_TEMPORAL_GOV(_gov, _name, _func)	\
	struct edp_governor _gov = {	\
		.name = _name,	\
		.owner = THIS_MODULE,	\
		.start = temporal_start,	\
		.stop = temporal_stop,	\
		.update_request = _func,	\
		.update_loans = edp_default_update_loans,	\
		.promote = temporal_promote	\
	};

static DEFINE_TEMPORAL_GOV(lrr_governor, "least_recent", lrr_update_request);
static DEFINE_TEMPORAL_GOV(mrr_governor, "most_recent", mrr_update_request);
static DEFINE_TEMPORAL_GOV(rr_governor, "round_robin", rr_update_request);

static void throttle(struct edp_client *client)
{
	struct edp_manager *m = client->manager;
	unsigned int required = req_level(client) - cur_level(client);
	struct list_head *head = m->gov_data;
	struct edp_client *n;
	struct edp_client *c;
	unsigned int bal;

	bal = m->remaining;
	n = NULL;

	list_for_each_entry_reverse(c, head, glnk) {
		if (cur_level(c) > e0_level(c) && c != client) {
			c->gwt = edp_throttling_point(c, required - bal);
			bal += cur_level(c) - c->states[c->gwt];
			n = c;
			if (bal >= required)
				break;
		}
	}

	c = n;
	bal = m->remaining;
	if (!c)
		goto finish;

	/* use the safe version as we might be re-arraging the list */
	list_for_each_entry_safe_from(c, n, head, glnk) {
		if (cur_level(c) <= e0_level(c) || c == client ||
				c->gwt == cur_index(c))
			continue;

		c->throttle(c->gwt);
		bal += cur_level(c) - c->states[c->gwt];
		if (c->cur == c->req)
			m->num_denied++;
		c->cur = c->states + c->gwt;

		/* for RR, move this client to the head */
		if (m->gov == &rr_governor)
			list_move(&c->glnk, head);
		if (bal >= required)
			break;
	}

finish:
	m->remaining = bal + cur_level(client);
	client->cur = client->states + edp_promotion_point(client, bal);
	m->remaining -= cur_level(client);
}

static int __init temporal_init(void)
{
	int ret = 0;
	int r;

	r = edp_register_governor(&lrr_governor);
	if (r) {
		pr_err("least_recent governor registration failed: %d\n", r);
		ret = r;
	}

	r = edp_register_governor(&mrr_governor);
	if (r) {
		pr_err("most_recent governor registration failed: %d\n", r);
		ret = r;
	}

	r = edp_register_governor(&rr_governor);
	if (r) {
		pr_err("round_robin governor registration failed: %d\n", r);
		ret = r;
	}

	return ret;
}
postcore_initcall(temporal_init);