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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
/*
* omap-pm.c - OMAP power management interface
*
* Copyright (C) 2008-2010 Texas Instruments, Inc.
* Copyright (C) 2008-2009 Nokia Corporation
* Vishwanath BS
*
* This code is based on plat-omap/omap-pm-noop.c.
*
* Interface developed by (in alphabetical order):
* Karthik Dasu, Tony Lindgren, Rajendra Nayak, Sakari Poussa, Veeramanikandan
* Raju, Anand Sawant, Igor Stoppa, Paul Walmsley, Richard Woodruff
*/
#undef DEBUG
#include <linux/init.h>
#include <linux/cpufreq.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/list.h>
/* Interface documentation is in mach/omap-pm.h */
#include <plat/omap-pm.h>
#include <plat/omap_device.h>
#include <plat/common.h>
#include "../mach-omap2/powerdomain.h"
struct omap_opp *dsp_opps;
struct omap_opp *mpu_opps;
struct omap_opp *l3_opps;
static DEFINE_MUTEX(bus_tput_mutex);
static DEFINE_MUTEX(mpu_tput_mutex);
static DEFINE_MUTEX(mpu_lat_mutex);
static bool off_mode_enabled;
/* Used to model a Interconnect Throughput */
static struct interconnect_tput {
/* Total no of users at any point of interconnect */
u8 no_of_users;
/* List of all the current users for interconnect */
struct list_head users_list;
struct list_head node;
/* Protect interconnect throughput */
struct mutex throughput_mutex;
/* Target level for interconnect throughput */
unsigned long target_level;
} *bus_tput;
/* Used to represent a user of a interconnect throughput */
struct users {
/* Device pointer used to uniquely identify the user */
struct device *dev;
struct list_head node;
/* Current level as requested for interconnect throughput by the user */
u32 level;
};
/* Private/Internal Functions */
/**
* user_lookup - look up a user by its device pointer, return a pointer
* @dev: The device to be looked up
*
* Looks for a interconnect user by its device pointer. Returns a
* pointer to
* the struct users if found, else returns NULL.
**/
static struct users *user_lookup(struct device *dev)
{
struct users *usr, *tmp_usr;
usr = NULL;
list_for_each_entry(tmp_usr, &bus_tput->users_list, node) {
if (tmp_usr->dev == dev) {
usr = tmp_usr;
break;
}
}
return usr;
}
/**
* get_user - gets a new users_list struct dynamically
*
* This function allocates dynamcially the user node
* Returns a pointer to users struct on success. On dynamic allocation
* failure
* returns a ERR_PTR(-ENOMEM).
**/
static struct users *get_user(void)
{
struct users *user;
user = kmalloc(sizeof(struct users), GFP_KERNEL);
if (!user) {
pr_err("%s FATAL ERROR: kmalloc "
"failed\n", __func__);
return ERR_PTR(-ENOMEM);
}
return user;
}
/**
* omap_bus_tput_init - Initializes the interconnect throughput
* userlist
* Allocates memory for global throughput variable dynamically.
* Intializes Userlist, no. of users and throughput target level.
* Returns 0 on sucess, else returns EINVAL if memory
* allocation fails.
*/
int omap_bus_tput_init(void)
{
bus_tput = kmalloc(sizeof(struct interconnect_tput), GFP_KERNEL);
if (!bus_tput) {
pr_err("%s FATAL ERROR: kmalloc failed\n", __func__);
return -EINVAL;
}
INIT_LIST_HEAD(&bus_tput->users_list);
mutex_init(&bus_tput->throughput_mutex);
bus_tput->no_of_users = 0;
bus_tput->target_level = 0;
return 0;
}
/**
* add_req_tput - Request for a required level by a device
* @dev: Uniquely identifes the caller
* @level: The requested level for the interconnect bandwidth in KiB/s
*
* This function recomputes the target level of the interconnect
* bandwidth
* based on the level requested by all the users.
* Multiple calls to this function by the same device will
* replace the previous level requested
* Returns the updated level of interconnect throughput.
* In case of Invalid dev or user pointer, it returns 0.
*/
static unsigned long add_req_tput(struct device *dev, unsigned long level)
{
int ret;
struct users *user;
if (!dev) {
pr_err("Invalid dev pointer\n");
ret = 0;
}
mutex_lock(&bus_tput->throughput_mutex);
user = user_lookup(dev);
if (user == NULL) {
user = get_user();
if (IS_ERR(user)) {
pr_err("Couldn't get user from the list to"
"add new throughput constraint");
ret = 0;
goto unlock;
}
bus_tput->target_level += level;
bus_tput->no_of_users++;
user->dev = dev;
list_add(&user->node, &bus_tput->users_list);
user->level = level;
} else {
bus_tput->target_level -= user->level;
bus_tput->target_level += level;
user->level = level;
}
ret = bus_tput->target_level;
unlock:
mutex_unlock(&bus_tput->throughput_mutex);
return ret;
}
/**
* remove_req_tput - Release a previously requested level of
* a throughput level for interconnect
* @dev: Device pointer to dev
*
* This function recomputes the target level of the interconnect
* throughput after removing
* the level requested by the user.
* Returns 0, if the dev structure is invalid
* else returns modified interconnect throughput rate.
*/
static unsigned long remove_req_tput(struct device *dev)
{
struct users *user;
int found = 0;
int ret;
mutex_lock(&bus_tput->throughput_mutex);
list_for_each_entry(user, &bus_tput->users_list, node) {
if (user->dev == dev) {
found = 1;
break;
}
}
if (!found) {
/* No such user exists */
pr_err("Invalid Device Structure\n");
ret = 0;
goto unlock;
}
bus_tput->target_level -= user->level;
bus_tput->no_of_users--;
list_del(&user->node);
kfree(user);
ret = bus_tput->target_level;
unlock:
mutex_unlock(&bus_tput->throughput_mutex);
return ret;
}
/*
* Device-driver-originated constraints (via board-*.c files)
*/
int omap_pm_set_max_mpu_wakeup_lat(struct pm_qos_request_list **qos_request,
long t)
{
if (!qos_request || t < -1) {
WARN(1, "OMAP PM: %s: invalid parameter(s)", __func__);
return -EINVAL;
};
mutex_lock(&mpu_lat_mutex);
if (t == -1) {
pm_qos_remove_request(*qos_request);
kfree(*qos_request);
*qos_request = NULL;
} else if (*qos_request == NULL) {
*qos_request = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL);
pm_qos_add_request(*qos_request, PM_QOS_CPU_DMA_LATENCY, t);
} else
pm_qos_update_request(*qos_request, t);
mutex_unlock(&mpu_lat_mutex);
return 0;
}
int omap_pm_set_min_bus_tput(struct device *dev, u8 agent_id, long r)
{
int ret;
struct device *l3_dev;
static struct device dummy_l3_dev;
unsigned long target_level = 0;
if (!dev || (agent_id != OCP_INITIATOR_AGENT &&
agent_id != OCP_TARGET_AGENT)) {
WARN(1, "OMAP PM: %s: invalid parameter(s)", __func__);
return -EINVAL;
};
mutex_lock(&bus_tput_mutex);
l3_dev = omap2_get_l3_device();
if (!l3_dev) {
pr_err("Unable to get l3 device pointer");
ret = -EINVAL;
goto unlock;
}
if (r == -1) {
pr_debug("OMAP PM: remove min bus tput constraint for: "
"interconnect dev %s for agent_id %d\n", dev_name(dev),
agent_id);
target_level = remove_req_tput(dev);
} else {
pr_debug("OMAP PM: add min bus tput constraint for: "
"interconnect dev %s for agent_id %d: rate %ld KiB\n",
dev_name(dev), agent_id, r);
target_level = add_req_tput(dev, r);
}
/* Convert the throughput(in KiB/s) into Hz. */
target_level = (target_level * 1000)/4;
WARN(1, "OMAP PM: %s: constraint not called, needs DVFS", __func__);
#if 0
ret = omap_device_scale(&dummy_l3_dev, l3_dev, target_level);
#endif
if (ret)
pr_err("Unable to change level for interconnect bandwidth to %ld\n",
target_level);
unlock:
mutex_unlock(&bus_tput_mutex);
return ret;
}
int omap_pm_set_max_dev_wakeup_lat(struct device *req_dev, struct device *dev,
long t)
{
struct omap_device *odev;
struct powerdomain *pwrdm_dev;
struct platform_device *pdev;
int ret = 0;
if (!req_dev || !dev || t < -1) {
WARN(1, "OMAP PM: %s: invalid parameter(s)", __func__);
return -EINVAL;
};
/* Look for the devices Power Domain */
pdev = container_of(dev, struct platform_device, dev);
/* Try to catch non platform devices. */
if (pdev->name == NULL) {
pr_err("OMAP-PM: Error: platform device not valid\n");
return -EINVAL;
}
odev = to_omap_device(pdev);
if (odev) {
pwrdm_dev = omap_device_get_pwrdm(odev);
} else {
pr_err("OMAP-PM: Error: Could not find omap_device "
"for %s\n", pdev->name);
return -EINVAL;
}
/* Catch devices with undefined powerdomains. */
if (!pwrdm_dev) {
pr_err("OMAP-PM: Error: could not find parent "
"powerdomain for %s\n", pdev->name);
return -EINVAL;
}
if (t == -1) {
pr_debug("OMAP PM: remove max device latency constraint: "
"dev %s, pwrdm %s, req by %s\n", dev_name(dev),
pwrdm_dev->name, dev_name(req_dev));
ret = pwrdm_wakeuplat_release_constraint(pwrdm_dev, req_dev);
} else {
pr_debug("OMAP PM: add max device latency constraint: "
"dev %s, t = %ld usec, pwrdm %s, req by %s\n",
dev_name(dev), t, pwrdm_dev->name, dev_name(req_dev));
ret = pwrdm_wakeuplat_set_constraint(pwrdm_dev, req_dev, t);
}
/*
* For current Linux, this needs to map the device to a
* powerdomain, then go through the list of current max lat
* constraints on that powerdomain and find the smallest. If
* the latency constraint has changed, the code should
* recompute the state to enter for the next powerdomain
* state. Conceivably, this code should also determine
* whether to actually disable the device clocks or not,
* depending on how long it takes to re-enable the clocks.
*
* TI CDP code can call constraint_set here.
*/
return ret;
}
int omap_pm_set_max_sdma_lat(struct pm_qos_request_list **qos_request,
long t)
{
if (!qos_request || t < -1) {
WARN(1, "OMAP PM: %s: invalid parameter(s)", __func__);
return -EINVAL;
};
if (t == -1) {
pm_qos_remove_request(*qos_request);
kfree(*qos_request);
*qos_request = NULL;
} else if (*qos_request == NULL) {
*qos_request = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL);
pm_qos_add_request(*qos_request, PM_QOS_CPU_DMA_LATENCY, t);
} else
pm_qos_update_request(*qos_request, t);
return 0;
}
int omap_pm_set_min_clk_rate(struct device *dev, struct clk *c, long r)
{
if (!dev || !c || r < 0) {
WARN(1, "OMAP PM: %s: invalid parameter(s)", __func__);
return -EINVAL;
}
if (r == 0)
pr_debug("OMAP PM: remove min clk rate constraint: "
"dev %s\n", dev_name(dev));
else
pr_debug("OMAP PM: add min clk rate constraint: "
"dev %s, rate = %ld Hz\n", dev_name(dev), r);
/*
* Code in a real implementation should keep track of these
* constraints on the clock, and determine the highest minimum
* clock rate. It should iterate over each OPP and determine
* whether the OPP will result in a clock rate that would
* satisfy this constraint (and any other PM constraint in effect
* at that time). Once it finds the lowest-voltage OPP that
* meets those conditions, it should switch to it, or return
* an error if the code is not capable of doing so.
*/
return 0;
}
/*
* DSP Bridge-specific constraints
*/
const struct omap_opp *omap_pm_dsp_get_opp_table(void)
{
pr_debug("OMAP PM: DSP request for OPP table\n");
/*
* Return DSP frequency table here: The final item in the
* array should have .rate = .opp_id = 0.
*/
return NULL;
}
void omap_pm_dsp_set_min_opp(u8 opp_id)
{
if (opp_id == 0) {
WARN_ON(1);
return;
}
pr_debug("OMAP PM: DSP requests minimum VDD1 OPP to be %d\n", opp_id);
/*
*
* For l-o dev tree, our VDD1 clk is keyed on OPP ID, so we
* can just test to see which is higher, the CPU's desired OPP
* ID or the DSP's desired OPP ID, and use whichever is
* highest.
*
* In CDP12.14+, the VDD1 OPP custom clock that controls the DSP
* rate is keyed on MPU speed, not the OPP ID. So we need to
* map the OPP ID to the MPU speed for use with clk_set_rate()
* if it is higher than the current OPP clock rate.
*
*/
}
u8 omap_pm_dsp_get_opp(void)
{
pr_debug("OMAP PM: DSP requests current DSP OPP ID\n");
/*
* For l-o dev tree, call clk_get_rate() on VDD1 OPP clock
*
* CDP12.14+:
* Call clk_get_rate() on the OPP custom clock, map that to an
* OPP ID using the tables defined in board-*.c/chip-*.c files.
*/
return 0;
}
/*
* CPUFreq-originated constraint
*
* In the future, this should be handled by custom OPP clocktype
* functions.
*/
struct cpufreq_frequency_table **omap_pm_cpu_get_freq_table(void)
{
pr_debug("OMAP PM: CPUFreq request for frequency table\n");
/*
* Return CPUFreq frequency table here: loop over
* all VDD1 clkrates, pull out the mpu_ck frequencies, build
* table
*/
return NULL;
}
void omap_pm_cpu_set_freq(unsigned long f)
{
if (f == 0) {
WARN_ON(1);
return;
}
pr_debug("OMAP PM: CPUFreq requests CPU frequency to be set to %lu\n",
f);
/*
* For l-o dev tree, determine whether MPU freq or DSP OPP id
* freq is higher. Find the OPP ID corresponding to the
* higher frequency. Call clk_round_rate() and clk_set_rate()
* on the OPP custom clock.
*
* CDP should just be able to set the VDD1 OPP clock rate here.
*/
}
unsigned long omap_pm_cpu_get_freq(void)
{
pr_debug("OMAP PM: CPUFreq requests current CPU frequency\n");
/*
* Call clk_get_rate() on the mpu_ck.
*/
return 0;
}
/**
* omap_pm_enable_off_mode - notify OMAP PM that off-mode is enabled
*
* Intended for use only by OMAP PM core code to notify this layer
* that off mode has been enabled.
*/
void omap_pm_enable_off_mode(void)
{
off_mode_enabled = true;
}
/**
* omap_pm_disable_off_mode - notify OMAP PM that off-mode is disabled
*
* Intended for use only by OMAP PM core code to notify this layer
* that off mode has been disabled.
*/
void omap_pm_disable_off_mode(void)
{
off_mode_enabled = false;
}
/*
* Device context loss tracking
*/
int omap_pm_get_dev_context_loss_count(struct device *dev)
{
static u32 counter = 1;
if (!dev) {
WARN_ON(1);
return -EINVAL;
};
pr_debug("OMAP PM: returning context loss count for dev %s\n",
dev_name(dev));
/*
* Map the device to the powerdomain. Return the powerdomain
* off counter.
*/
/* Let the counter roll-over: its for test only */
return counter++;
}
/* Should be called before clk framework init */
int __init omap_pm_if_early_init()
{
return 0;
}
/* Must be called after clock framework is initialized */
int __init omap_pm_if_init(void)
{
int ret;
ret = omap_bus_tput_init();
if (ret)
pr_err("Failed to initialize interconnect"
" bandwidth users list\n");
return ret;
}
void omap_pm_if_exit(void)
{
/* Deallocate CPUFreq frequency table here */
}
int omap_pm_set_min_mpu_freq(struct device *dev, unsigned long f)
{
int ret = 0;
struct device *mpu_dev;
if (!dev) {
WARN(1, "OMAP PM: %s: invalid parameter(s)", __func__);
return -EINVAL;
}
mutex_lock(&mpu_tput_mutex);
mpu_dev = omap2_get_mpuss_device();
if (!mpu_dev) {
pr_err("Unable to get MPU device pointer");
ret = -EINVAL;
goto unlock;
}
/* Rescale the frequency if a change is detected with
* the new constraint.
*/
WARN(1, "OMAP PM: %s: constraint not called, needs DVFS", __func__);
#if 0
ret = omap_device_set_rate(dev, mpu_dev, f);
#endif
if (ret)
pr_err("Unable to set MPU frequency to %ld\n", f);
unlock:
mutex_unlock(&mpu_tput_mutex);
return ret;
}
EXPORT_SYMBOL(omap_pm_set_min_mpu_freq);
|