ARM: tegra: clock: Add emc iso allocation to debugfs
[linux-3.10.git] / arch / arm / mach-tegra / tegra_emc.c
1 /*
2  * arch/arm/mach-tegra/tegra_emc.c
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/err.h>
23 #include <linux/io.h>
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28
29 #include "tegra_emc.h"
30
31 static u8 emc_iso_share = 100;
32 static unsigned long emc_iso_allocation;
33
34 static struct emc_iso_usage emc_usage_table[TEGRA_EMC_ISO_USE_CASES_MAX_NUM];
35
36
37 void __init tegra_emc_iso_usage_table_init(struct emc_iso_usage *table,
38                                            int size)
39 {
40         size = min(size, TEGRA_EMC_ISO_USE_CASES_MAX_NUM);
41
42         if (size && table)
43                 memcpy(emc_usage_table, table,
44                        size * sizeof(struct emc_iso_usage));
45 }
46
47 static u8 tegra_emc_get_iso_share(u32 usage_flags)
48 {
49         int i;
50         u8 iso_share = 100;
51
52         if (usage_flags) {
53                 for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
54                         struct emc_iso_usage *iso_usage = &emc_usage_table[i];
55                         u32 flags = iso_usage->emc_usage_flags;
56                         u8 share = iso_usage->iso_usage_share;
57
58                         if (!flags)
59                                 continue;
60                         if (!share) {
61                                 WARN(1, "%s: entry %d: iso_share 0\n",
62                                      __func__, i);
63                                 continue;
64                         }
65
66                         if ((flags & usage_flags) == flags)
67                                 iso_share = min(iso_share,
68                                                 iso_usage->iso_usage_share);
69                 }
70         }
71         emc_iso_share = iso_share;
72         return iso_share;
73 }
74
75 unsigned long tegra_emc_apply_efficiency(unsigned long total_bw,
76         unsigned long iso_bw, unsigned long max_rate, u32 usage_flags)
77 {
78         u8 efficiency = tegra_emc_get_iso_share(usage_flags);
79         if (iso_bw && efficiency && (efficiency < 100)) {
80                 iso_bw /= efficiency;
81                 iso_bw = (iso_bw < max_rate / 100) ?
82                                 (iso_bw * 100) : max_rate;
83         }
84         emc_iso_allocation = iso_bw;
85
86         efficiency = tegra_emc_bw_efficiency;
87         if (total_bw && efficiency && (efficiency < 100)) {
88                 total_bw = total_bw / efficiency;
89                 total_bw = (total_bw < max_rate / 100) ?
90                                 (total_bw * 100) : max_rate;
91         }
92         return max(total_bw, iso_bw);
93 }
94
95 #ifdef CONFIG_DEBUG_FS
96
97 #define USER_NAME(module) \
98 [EMC_USER_##module] = #module
99
100 static const char *emc_user_names[EMC_USER_NUM] = {
101         USER_NAME(DC),
102         USER_NAME(VI),
103         USER_NAME(MSENC),
104         USER_NAME(2D),
105         USER_NAME(3D),
106         USER_NAME(BB),
107 };
108
109 static int emc_usage_table_show(struct seq_file *s, void *data)
110 {
111         int i, j;
112
113         seq_printf(s, "EMC USAGE\t\tISO SHARE %%\n");
114
115         for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
116                 u32 flags = emc_usage_table[i].emc_usage_flags;
117                 u8 share = emc_usage_table[i].iso_usage_share;
118                 bool first = false;
119
120                 seq_printf(s, "[%d]: ", i);
121                 if (!flags) {
122                         seq_printf(s, "reserved\n");
123                         continue;
124                 }
125
126                 for (j = 0; j < EMC_USER_NUM; j++) {
127                         u32 mask = 0x1 << j;
128                         if (!(flags & mask))
129                                 continue;
130                         seq_printf(s, "%s%s", first ? "+" : "",
131                                    emc_user_names[j]);
132                         first = true;
133                 }
134                 seq_printf(s, "\r\t\t\t= %d\n", share);
135         }
136         return 0;
137 }
138
139 static int emc_usage_table_open(struct inode *inode, struct file *file)
140 {
141         return single_open(file, emc_usage_table_show, inode->i_private);
142 }
143
144 static const struct file_operations emc_usage_table_fops = {
145         .open           = emc_usage_table_open,
146         .read           = seq_read,
147         .llseek         = seq_lseek,
148         .release        = single_release,
149 };
150
151 int __init tegra_emc_iso_usage_debugfs_init(struct dentry *emc_debugfs_root)
152 {
153         struct dentry *d;
154
155         d = debugfs_create_file("emc_usage_table", S_IRUGO, emc_debugfs_root,
156                 NULL, &emc_usage_table_fops);
157         if (!d)
158                 return -ENOMEM;
159
160         d = debugfs_create_u8("emc_iso_share", S_IRUGO, emc_debugfs_root,
161                               &emc_iso_share);
162         d = debugfs_create_u32("emc_iso_allocation", S_IRUGO, emc_debugfs_root,
163                               (u32 *)&emc_iso_allocation);
164         if (!d)
165                 return -ENOMEM;
166
167         return 0;
168 }
169 #endif