ARM: tegra: clock: Make EMC BW request calculation common
[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 };
105
106 static int emc_usage_table_show(struct seq_file *s, void *data)
107 {
108         int i, j;
109
110         seq_printf(s, "EMC USAGE\t\tISO SHARE %%\n");
111
112         for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
113                 u32 flags = emc_usage_table[i].emc_usage_flags;
114                 u8 share = emc_usage_table[i].iso_usage_share;
115                 bool first = false;
116
117                 seq_printf(s, "[%d]: ", i);
118                 if (!flags) {
119                         seq_printf(s, "reserved\n");
120                         continue;
121                 }
122
123                 for (j = 0; j < EMC_USER_NUM; j++) {
124                         u32 mask = 0x1 << j;
125                         if (!(flags & mask))
126                                 continue;
127                         seq_printf(s, "%s%s", first ? "+" : "",
128                                    emc_user_names[j]);
129                         first = true;
130                 }
131                 seq_printf(s, "\r\t\t\t= %d\n", share);
132         }
133         return 0;
134 }
135
136 static int emc_usage_table_open(struct inode *inode, struct file *file)
137 {
138         return single_open(file, emc_usage_table_show, inode->i_private);
139 }
140
141 static const struct file_operations emc_usage_table_fops = {
142         .open           = emc_usage_table_open,
143         .read           = seq_read,
144         .llseek         = seq_lseek,
145         .release        = single_release,
146 };
147
148 int __init tegra_emc_iso_usage_debugfs_init(struct dentry *emc_debugfs_root)
149 {
150         struct dentry *d;
151
152         d = debugfs_create_file("emc_usage_table", S_IRUGO, emc_debugfs_root,
153                 NULL, &emc_usage_table_fops);
154         if (!d)
155                 return -ENOMEM;
156
157         d = debugfs_create_u8("emc_iso_share", S_IRUGO, emc_debugfs_root,
158                               &emc_iso_share);
159         if (!d)
160                 return -ENOMEM;
161
162         return 0;
163 }
164 #endif