summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/tegra_emc.c
blob: 7f92dd6da344c771e9b4ce84575f118c3b114ce8 (plain)
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
/*
 * arch/arm/mach-tegra/tegra_emc.c
 *
 * Copyright (c) 2013-2014, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/tegra-soc.h>

#include <mach/tegra_emc.h>

#include "tegra12_emc.h"

u8 tegra_emc_iso_share = 100;
static unsigned long emc_iso_allocation;
static unsigned long last_iso_bw;

static struct emc_iso_usage emc_usage_table[TEGRA_EMC_ISO_USE_CASES_MAX_NUM];


u32 tegra_get_dvfs_clk_change_latency_nsec(unsigned long emc_freq_khz)
{
	switch (tegra_get_chipid()) {
	case TEGRA_CHIPID_TEGRA12:
	case TEGRA_CHIPID_TEGRA13:
		return tegra12_get_dvfs_clk_change_latency_nsec(emc_freq_khz);
	default:
		return 0;
	}
}

void __init tegra_emc_iso_usage_table_init(struct emc_iso_usage *table,
					   int size)
{
	size = min(size, TEGRA_EMC_ISO_USE_CASES_MAX_NUM);

	if (size && table)
		memcpy(emc_usage_table, table,
		       size * sizeof(struct emc_iso_usage));
}

static u8 tegra_emc_get_iso_share(u32 usage_flags, unsigned long iso_bw)
{
	int i;
	u8 iso_share = 100;

	if (usage_flags) {
		for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
			struct emc_iso_usage *iso_usage = &emc_usage_table[i];
			u32 flags = iso_usage->emc_usage_flags;
			u8 share = iso_usage->iso_usage_share;

			if (!flags)
				continue;

			if (iso_usage->iso_share_calculator)
				share = iso_usage->iso_share_calculator(iso_bw);
			if (!share) {
				WARN(1, "%s: entry %d: iso_share 0\n",
				     __func__, i);
				continue;
			}

			if ((flags & usage_flags) == flags)
				iso_share = min(iso_share, share);
		}
	}
	last_iso_bw = iso_bw;
	tegra_emc_iso_share = iso_share;
	return iso_share;
}

unsigned long tegra_emc_apply_efficiency(unsigned long total_bw,
	unsigned long iso_bw, unsigned long max_rate, u32 usage_flags,
	unsigned long *iso_bw_min)
{
	u8 efficiency = tegra_emc_get_iso_share(usage_flags, iso_bw);
	if (iso_bw && efficiency && (efficiency < 100)) {
		iso_bw /= efficiency;
		iso_bw = (iso_bw < max_rate / 100) ?
				(iso_bw * 100) : max_rate;
	}
	emc_iso_allocation = iso_bw;
	if (iso_bw_min)
		*iso_bw_min = iso_bw;

	efficiency = tegra_emc_bw_efficiency;
	if (total_bw && efficiency && (efficiency < 100)) {
		total_bw = total_bw / efficiency;
		total_bw = (total_bw < max_rate / 100) ?
				(total_bw * 100) : max_rate;
	}
	return max(total_bw, iso_bw);
}

#ifdef CONFIG_DEBUG_FS

#define USER_NAME(module) \
[EMC_USER_##module] = #module

static const char *emc_user_names[EMC_USER_NUM] = {
	USER_NAME(DC1),
	USER_NAME(DC2),
	USER_NAME(VI),
	USER_NAME(MSENC),
	USER_NAME(2D),
	USER_NAME(3D),
	USER_NAME(BB),
};

static int emc_usage_table_show(struct seq_file *s, void *data)
{
	int i, j;

	seq_printf(s, "EMC USAGE\t\tISO SHARE %% @ last bw %lu\n", last_iso_bw);

	for (i = 0; i < TEGRA_EMC_ISO_USE_CASES_MAX_NUM; i++) {
		u32 flags = emc_usage_table[i].emc_usage_flags;
		u8 share = emc_usage_table[i].iso_usage_share;
		bool fixed_share = true;
		bool first = false;

		if (emc_usage_table[i].iso_share_calculator) {
			share = emc_usage_table[i].iso_share_calculator(
				last_iso_bw);
			fixed_share = false;
		}

		seq_printf(s, "[%d]: ", i);
		if (!flags) {
			seq_printf(s, "reserved\n");
			continue;
		}

		for (j = 0; j < EMC_USER_NUM; j++) {
			u32 mask = 0x1 << j;
			if (!(flags & mask))
				continue;
			seq_printf(s, "%s%s", first ? "+" : "",
				   emc_user_names[j]);
			first = true;
		}
		seq_printf(s, "\r\t\t\t= %d(%s across bw)\n",
			   share, fixed_share ? "fixed" : "vary");
	}
	return 0;
}

static int emc_usage_table_open(struct inode *inode, struct file *file)
{
	return single_open(file, emc_usage_table_show, inode->i_private);
}

static const struct file_operations emc_usage_table_fops = {
	.open		= emc_usage_table_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

int __init tegra_emc_iso_usage_debugfs_init(struct dentry *emc_debugfs_root)
{
	struct dentry *d;

	d = debugfs_create_file("emc_usage_table", S_IRUGO, emc_debugfs_root,
		NULL, &emc_usage_table_fops);
	if (!d)
		return -ENOMEM;

	d = debugfs_create_u8("emc_iso_share", S_IRUGO, emc_debugfs_root,
			      &tegra_emc_iso_share);
	if (!d)
		return -ENOMEM;

	d = debugfs_create_u32("emc_iso_allocation", S_IRUGO, emc_debugfs_root,
			      (u32 *)&emc_iso_allocation);
	if (!d)
		return -ENOMEM;

	return 0;
}
#endif