summaryrefslogtreecommitdiff
path: root/drivers/edp/sysfs.c
blob: 8ba5bf360dd913ba35cd1c9ac138384d67d305d6 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Copyright (c) 2012-2013, 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"

static struct kobject edp_kobj;

struct manager_entry {
	struct edp_manager *manager;
	struct kobject kobj;
};

struct manager_attr {
	struct attribute attr;
	ssize_t (*show)(struct edp_manager *, char *);
	ssize_t (*store)(struct edp_manager *, const char *, size_t);
};

static ssize_t cap_show(struct edp_manager *m, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", m->max);
}

static ssize_t remaining_show(struct edp_manager *m, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", m->remaining);
}

static ssize_t manager_governor_show(struct edp_manager *m, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%s\n", m->gov ? m->gov->name : "");
}

static ssize_t manager_governor_store(struct edp_manager *m, const char *s,
		size_t count)
{
	char name[EDP_NAME_LEN];
	struct edp_governor *gov;

	if (!count || count >= sizeof(name))
		return -EINVAL;

	memcpy(name, s, count);
	name[count] = 0;
	strim(name);
	gov = edp_find_governor_unlocked(name);
	if (!gov)
		return -EINVAL;

	return edp_set_governor_unlocked(m, gov) ?: count;
}

struct manager_attr attr_cap = __ATTR_RO(cap);
struct manager_attr attr_remaining = __ATTR_RO(remaining);
struct manager_attr attr_mgr_gov = __ATTR(governor, 0644,
		manager_governor_show, manager_governor_store);

static struct attribute *manager_attrs[] = {
	&attr_cap.attr,
	&attr_remaining.attr,
	&attr_mgr_gov.attr,
	NULL
};

static struct edp_manager *to_manager(struct kobject *kobj)
{
	struct manager_entry *me = container_of(kobj, struct manager_entry,
			kobj);
	return me ? me->manager : NULL;
}

static ssize_t manager_state_show(struct kobject *kobj,
		struct attribute *attr,	char *buf)
{
	ssize_t r;
	struct edp_manager *m;
	struct manager_attr *mattr;

	mutex_lock(&edp_lock);
	m = to_manager(kobj);
	mattr = container_of(attr, struct manager_attr, attr);
	r = m && mattr ? mattr->show(m, buf) : -EINVAL;
	mutex_unlock(&edp_lock);

	return r;
}

static ssize_t manager_state_store(struct kobject *kobj,
		struct attribute *attr,	const char *buf, size_t count)
{
	ssize_t r;
	struct edp_manager *m;
	struct manager_attr *mattr;

	mutex_lock(&edp_lock);
	m = to_manager(kobj);
	mattr = container_of(attr, struct manager_attr, attr);
	r = m && mattr ? mattr->store(m, buf, count) : -EINVAL;
	mutex_unlock(&edp_lock);

	return r;
}

static const struct sysfs_ops manager_sysfs_ops = {
	.show = manager_state_show,
	.store = manager_state_store
};

static struct kobj_type ktype_manager = {
	.sysfs_ops = &manager_sysfs_ops,
	.default_attrs = manager_attrs
};

void edp_manager_add_kobject(struct edp_manager *mgr)
{
	struct manager_entry *me;

	me = kzalloc(sizeof(*me), GFP_KERNEL);
	if (!me) {
		pr_err("%s: failed to alloc sysfs manager entry\n",
				mgr->name);
		return;
	}

	if (kobject_init_and_add(&me->kobj, &ktype_manager, &edp_kobj,
				mgr->name)) {
		pr_err("%s: failed to init & add sysfs manager entry\n",
				mgr->name);
		kfree(me);
		return;
	}

	me->manager = mgr;
	mgr->kobj = &me->kobj;
	kobject_uevent(&me->kobj, KOBJ_ADD);
	return;
}

void edp_manager_remove_kobject(struct edp_manager *mgr)
{
	struct manager_entry *me;

	if (!mgr->kobj)
		return;

	me = container_of(mgr->kobj, struct manager_entry, kobj);
	mgr->kobj = NULL;
	kobject_put(&me->kobj);
	kfree(me);
}

struct client_entry {
	struct edp_client *client;
	struct kobject kobj;
};

struct client_attr {
	struct attribute attr;
	ssize_t (*show)(struct edp_client *, char *);
	ssize_t (*store)(struct edp_client *, const char *);
};

static ssize_t states_show(struct edp_client *c, char *s)
{
	unsigned int i;
	int cnt = 0;
	const int sz = sizeof(*c->states) * 3 + 2;

	for (i = 0; i < c->num_states && (cnt + sz) < PAGE_SIZE; i++)
		cnt += sprintf(s + cnt, "%s%u", i ? " " : "", c->states[i]);

	cnt += sprintf(s + cnt, "\n");
	return cnt;
}

static ssize_t num_states_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", c->num_states);
}

static ssize_t e0_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", c->states[c->e0_index]);
}

static ssize_t max_borrowers_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", c->max_borrowers);
}

static ssize_t priority_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%d\n", c->priority);
}

static ssize_t request_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", req_level(c));
}

/* Allow only updates that are guaranteed to succeed */
static ssize_t request_store(struct edp_client *c, const char *s)
{
	unsigned int id;

	if (sscanf(s, "%u", &id) != 1)
		return -EINVAL;

	if (id >= c->num_states)
		return -EINVAL;

	if (id < c->e0_index && id < req_index(c))
		return -EPERM;

	return edp_update_client_request_unlocked(c, id, NULL);
}

static ssize_t current_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", cur_level(c));
}

static ssize_t threshold_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", c->ithreshold);
}

static ssize_t threshold_store(struct edp_client *c, const char *s)
{
	unsigned int tv;

	if (sscanf(s, "%u", &tv) != 1)
		return -EINVAL;

	return edp_update_loan_threshold_unlocked(c, tv);
}

static ssize_t borrowers_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", c->num_borrowers);
}

static ssize_t loans_show(struct edp_client *c, char *s)
{
	return scnprintf(s, PAGE_SIZE, "%u\n", c->num_loans);
}

struct client_attr attr_states = __ATTR_RO(states);
struct client_attr attr_num_states = __ATTR_RO(num_states);
struct client_attr attr_e0 = __ATTR_RO(e0);
struct client_attr attr_max_borrowers = __ATTR_RO(max_borrowers);
struct client_attr attr_priority = __ATTR_RO(priority);
struct client_attr attr_request = __ATTR(request, 0644, request_show,
		request_store);
struct client_attr attr_threshold = __ATTR(threshold, 0644, threshold_show,
		threshold_store);
struct client_attr attr_borrowers = __ATTR_RO(borrowers);
struct client_attr attr_loans = __ATTR_RO(loans);
struct client_attr attr_current = {
	.attr = { .name = "current", .mode = 0444 },
	.show = current_show
};

static struct attribute *client_attrs[] = {
	&attr_states.attr,
	&attr_num_states.attr,
	&attr_e0.attr,
	&attr_max_borrowers.attr,
	&attr_priority.attr,
	&attr_request.attr,
	&attr_current.attr,
	&attr_threshold.attr,
	&attr_borrowers.attr,
	&attr_loans.attr,
	NULL
};

static struct edp_client *to_client(struct kobject *kobj)
{
	struct client_entry *ce = container_of(kobj, struct client_entry,
			kobj);
	return ce ? ce->client : NULL;
}

static ssize_t client_state_show(struct kobject *kobj,
		struct attribute *attr,	char *buf)
{
	ssize_t r;
	struct edp_client *c;
	struct client_attr *cattr;

	mutex_lock(&edp_lock);
	c = to_client(kobj);
	cattr = container_of(attr, struct client_attr, attr);
	r = c && cattr ? cattr->show(c, buf) : -EINVAL;
	mutex_unlock(&edp_lock);

	return r;
}

static ssize_t client_state_store(struct kobject *kobj,
		struct attribute *attr, const char *buf, size_t count)
{
	ssize_t r = -EINVAL;
	struct edp_client *c;
	struct client_attr *cattr;

	mutex_lock(&edp_lock);

	c = to_client(kobj);
	cattr = container_of(attr, struct client_attr, attr);
	if (c && cattr) {
		if (cattr->store)
			r = cattr->store(c, buf);
	}

	mutex_unlock(&edp_lock);

	return r ?: count;
}

static const struct sysfs_ops client_sysfs_ops = {
	.show = client_state_show,
	.store = client_state_store
};

static struct kobj_type ktype_client = {
	.sysfs_ops = &client_sysfs_ops,
	.default_attrs = client_attrs
};

void edp_client_add_kobject(struct edp_client *client)
{
	struct client_entry *ce;
	struct kobject *parent = client->manager->kobj;

	if (!parent)
		return;

	ce = kzalloc(sizeof(*ce), GFP_KERNEL);
	if (!ce) {
		pr_err("%s: failed to alloc sysfs client entry\n",
				client->name);
		return;
	}

	if (kobject_init_and_add(&ce->kobj, &ktype_client, parent,
				client->name)) {
		pr_err("%s: failed to init & add sysfs client entry\n",
				client->name);
		kfree(ce);
		return;
	}

	ce->client = client;
	client->kobj = &ce->kobj;
	kobject_uevent(&ce->kobj, KOBJ_ADD);
	return;
}

void edp_client_remove_kobject(struct edp_client *client)
{
	struct client_entry *ce;

	if (!client->kobj)
		return;

	ce = container_of(client->kobj, struct client_entry, kobj);
	client->kobj = NULL;
	kobject_put(&ce->kobj);
	kfree(ce);
}

static ssize_t governors_show(struct kobject *kobj, struct attribute *attr,
		char *s)
{
	struct edp_governor *g;
	int cnt = 0;

	mutex_lock(&edp_lock);

	list_for_each_entry(g, &edp_governors, link) {
		if (cnt + EDP_NAME_LEN + 2 >= PAGE_SIZE)
			break;
		cnt += sprintf(s + cnt, "%s%s", cnt ? " " : "", g->name);
	}

	cnt += sprintf(s + cnt, "\n");

	mutex_unlock(&edp_lock);

	return cnt;
}

static const struct sysfs_ops edp_sysfs_ops = {
	.show = governors_show
};

static struct attribute attr_governors = {
	.name = "governors",
	.mode = 0444
};

static struct attribute *edp_attrs[] = {
	&attr_governors,
	NULL
};

static struct kobj_type ktype_edp = {
	.sysfs_ops = &edp_sysfs_ops,
	.default_attrs = edp_attrs
};

static int __init edp_sysfs_init(void)
{
	struct kobject *parent = NULL;

#ifdef CONFIG_PM
	parent = power_kobj;
#endif

	return kobject_init_and_add(&edp_kobj, &ktype_edp, parent, "edp");
}
postcore_initcall(edp_sysfs_init);