ec2d2ce1596c2695b67c50b899a87e21fc99f28d
[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         unsigned long *iso_bw_min)
78 {
79         u8 efficiency = tegra_emc_get_iso_share(usage_flags);
80         if (iso_bw && efficiency && (efficiency < 100)) {
81                 iso_bw /= efficiency;
82                 iso_bw = (iso_bw < max_rate / 100) ?
83                                 (iso_bw * 100) : max_rate;
84         }
85         emc_iso_allocation = iso_bw;
86         if (iso_bw_min)
87                 *iso_bw_min = iso_bw;
88
89         efficiency = tegra_emc_bw_efficiency;
90         if (total_bw && efficiency && (efficiency < 100)) {
91                 total_bw = total_bw / efficiency;
92                 total_bw = (total_bw < max_rate / 100) ?
93                                 (total_bw * 100) : max_rate;
94         }
95         return max(total_bw, iso_bw);
96 }
97
98 #ifdef CONFIG_DEBUG_FS
99
100 #define USER_NAME(module) \
101 [EMC_USER_##module] = #module
102
103 static const char *emc_user_names[EMC_USER_NUM] = {
104         USER_NAME(DC),
105         USER_NAME(VI),
106         USER_NAME(MSENC),
107         USER_NAME(2D),
108         USER_NAME(3D),
109         USER_NAME(BB),
110 };
111
112 static int emc_usage_table_show(struct seq_file *s, void *data)
113 {
114         int i, j;
115
116         seq_printf(s, "EMC USAGE\t\tISO SHARE %%\n");
117
118         for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
119                 u32 flags = emc_usage_table[i].emc_usage_flags;
120                 u8 share = emc_usage_table[i].iso_usage_share;
121                 bool first = false;
122
123                 seq_printf(s, "[%d]: ", i);
124                 if (!flags) {
125                         seq_printf(s, "reserved\n");
126                         continue;
127                 }
128
129                 for (j = 0; j < EMC_USER_NUM; j++) {
130                         u32 mask = 0x1 << j;
131                         if (!(flags & mask))
132                                 continue;
133                         seq_printf(s, "%s%s", first ? "+" : "",
134                                    emc_user_names[j]);
135                         first = true;
136                 }
137                 seq_printf(s, "\r\t\t\t= %d\n", share);
138         }
139         return 0;
140 }
141
142 static int emc_usage_table_open(struct inode *inode, struct file *file)
143 {
144         return single_open(file, emc_usage_table_show, inode->i_private);
145 }
146
147 static const struct file_operations emc_usage_table_fops = {
148         .open           = emc_usage_table_open,
149         .read           = seq_read,
150         .llseek         = seq_lseek,
151         .release        = single_release,
152 };
153
154 int __init tegra_emc_iso_usage_debugfs_init(struct dentry *emc_debugfs_root)
155 {
156         struct dentry *d;
157
158         d = debugfs_create_file("emc_usage_table", S_IRUGO, emc_debugfs_root,
159                 NULL, &emc_usage_table_fops);
160         if (!d)
161                 return -ENOMEM;
162
163         d = debugfs_create_u8("emc_iso_share", S_IRUGO, emc_debugfs_root,
164                               &emc_iso_share);
165         if (!d)
166                 return -ENOMEM;
167
168         d = debugfs_create_u32("emc_iso_allocation", S_IRUGO, emc_debugfs_root,
169                               (u32 *)&emc_iso_allocation);
170         if (!d)
171                 return -ENOMEM;
172
173         return 0;
174 }
175 #endif