ARM: tegra14: clock: Convert BB EMC user
[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
33 static struct emc_iso_usage emc_usage_table[TEGRA_EMC_ISO_USE_CASES_MAX_NUM];
34
35
36 void __init tegra_emc_iso_usage_table_init(struct emc_iso_usage *table,
37                                            int size)
38 {
39         size = min(size, TEGRA_EMC_ISO_USE_CASES_MAX_NUM);
40
41         if (size && table)
42                 memcpy(emc_usage_table, table,
43                        size * sizeof(struct emc_iso_usage));
44 }
45
46 static u8 tegra_emc_get_iso_share(u32 usage_flags)
47 {
48         int i;
49         u8 iso_share = 100;
50
51         if (usage_flags) {
52                 for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
53                         struct emc_iso_usage *iso_usage = &emc_usage_table[i];
54                         u32 flags = iso_usage->emc_usage_flags;
55                         u8 share = iso_usage->iso_usage_share;
56
57                         if (!flags)
58                                 continue;
59                         if (!share) {
60                                 WARN(1, "%s: entry %d: iso_share 0\n",
61                                      __func__, i);
62                                 continue;
63                         }
64
65                         if ((flags & usage_flags) == flags)
66                                 iso_share = min(iso_share,
67                                                 iso_usage->iso_usage_share);
68                 }
69         }
70         emc_iso_share = iso_share;
71         return iso_share;
72 }
73
74 unsigned long tegra_emc_apply_efficiency(unsigned long total_bw,
75         unsigned long iso_bw, unsigned long max_rate, u32 usage_flags)
76 {
77         u8 efficiency = tegra_emc_get_iso_share(usage_flags);
78         if (iso_bw && efficiency && (efficiency < 100)) {
79                 iso_bw /= efficiency;
80                 iso_bw = (iso_bw < max_rate / 100) ?
81                                 (iso_bw * 100) : max_rate;
82         }
83
84         efficiency = tegra_emc_bw_efficiency;
85         if (total_bw && efficiency && (efficiency < 100)) {
86                 total_bw = total_bw / efficiency;
87                 total_bw = (total_bw < max_rate / 100) ?
88                                 (total_bw * 100) : max_rate;
89         }
90         return max(total_bw, iso_bw);
91 }
92
93 #ifdef CONFIG_DEBUG_FS
94
95 #define USER_NAME(module) \
96 [EMC_USER_##module] = #module
97
98 static const char *emc_user_names[EMC_USER_NUM] = {
99         USER_NAME(DC),
100         USER_NAME(VI),
101         USER_NAME(MSENC),
102         USER_NAME(2D),
103         USER_NAME(3D),
104         USER_NAME(BB),
105 };
106
107 static int emc_usage_table_show(struct seq_file *s, void *data)
108 {
109         int i, j;
110
111         seq_printf(s, "EMC USAGE\t\tISO SHARE %%\n");
112
113         for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
114                 u32 flags = emc_usage_table[i].emc_usage_flags;
115                 u8 share = emc_usage_table[i].iso_usage_share;
116                 bool first = false;
117
118                 seq_printf(s, "[%d]: ", i);
119                 if (!flags) {
120                         seq_printf(s, "reserved\n");
121                         continue;
122                 }
123
124                 for (j = 0; j < EMC_USER_NUM; j++) {
125                         u32 mask = 0x1 << j;
126                         if (!(flags & mask))
127                                 continue;
128                         seq_printf(s, "%s%s", first ? "+" : "",
129                                    emc_user_names[j]);
130                         first = true;
131                 }
132                 seq_printf(s, "\r\t\t\t= %d\n", share);
133         }
134         return 0;
135 }
136
137 static int emc_usage_table_open(struct inode *inode, struct file *file)
138 {
139         return single_open(file, emc_usage_table_show, inode->i_private);
140 }
141
142 static const struct file_operations emc_usage_table_fops = {
143         .open           = emc_usage_table_open,
144         .read           = seq_read,
145         .llseek         = seq_lseek,
146         .release        = single_release,
147 };
148
149 int __init tegra_emc_iso_usage_debugfs_init(struct dentry *emc_debugfs_root)
150 {
151         struct dentry *d;
152
153         d = debugfs_create_file("emc_usage_table", S_IRUGO, emc_debugfs_root,
154                 NULL, &emc_usage_table_fops);
155         if (!d)
156                 return -ENOMEM;
157
158         d = debugfs_create_u8("emc_iso_share", S_IRUGO, emc_debugfs_root,
159                               &emc_iso_share);
160         if (!d)
161                 return -ENOMEM;
162
163         return 0;
164 }
165 #endif