aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/l2c_share/l2c_share_normal.c
blob: 0b38f04452d9b128140b36692d49cfd55186ecca (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/cpu.h>
#include <linux/sched.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <mach/mt_hotplug_strategy.h>
#include <mach/mt_spm_mtcmos.h>
#include <mach/mt_secure_api.h>
#include "l2c_share.h"


static struct device_driver mt_l2c_drv =
{
    .name = "l2c_share",
    .bus = &platform_bus_type,
    .owner = THIS_MODULE,
};


static char *log[] = {
    "borrow L2$",
    "return L2$",   
    "wrong option"
};


void __iomem *mp0_cache_config;
void __iomem *mp1_cache_config;

l2c_share_info g_l2c_share_info;
int is_l2_borrowed;


int IS_L2_BORROWED(void)
{
    return is_l2_borrowed;
}

static int enable_secondary_clusters_pwr(void)
{
	int err = 0;
	
	if(g_l2c_share_info.share_cluster_num == 1)
	{
		pr_notice("L2$ share cluster num is only 1, no needs to enable other cluster's pwr.\n");
	}
	else if(g_l2c_share_info.share_cluster_num == 2)
	{
		spm_mtcmos_ctrl_cpusys1(STA_POWER_ON, 1);
	}
	//else if(TBD...)
	else
	{
		pr_err("[ERROR] Inllegal L2$ share_cluster_num!\n");
		err = -1;
	}

	return err;
}

int disable_secondary_clusters_pwr(void)
{
	int err = 0;
	
	if(g_l2c_share_info.share_cluster_num == 1)
	{
		pr_notice("L2$ share cluster num is only 1, no needs to disable other cluster's pwr.\n");
	}
	else if(g_l2c_share_info.share_cluster_num == 2)
	{
		spm_mtcmos_ctrl_cpusys1(STA_POWER_DOWN, 1);
	}
	//else if(TBD...)
	else
	{
		pr_err("[ERROR] Inllegal L2$ share_cluster_num!\n");
		err = -1;
	}

	return err;
}

int config_L2_size(enum options option)
{
	int ret = 0;
	unsigned long flag;
	
	local_irq_save(flag);
	ret = mt_secure_call(MTK_SIP_KERNEL_L2_SHARING, option, g_l2c_share_info.share_cluster_num, 
		(g_l2c_share_info.cluster_borrow<<16) | (g_l2c_share_info.cluster_return));
	local_irq_restore(flag);

	return ret;
}

int switch_L2(enum options option)
{
	int i, cpu;
	int err = 0;
	int retry=0;    
	u64 t1;
	u64 t2;
	unsigned long mask = (1<<0);

	if(option >= BORROW_NONE) {
		pr_err("wrong option %d\n", option);
		return -1;
	}

	t1 = sched_clock();

	/* bind this process to main cpu */    
	while(sched_setaffinity(0, (struct cpumask*) &mask) < 0)
	{
		pr_err("Could not set cpu 0 affinity for current process(%d).\n", retry);
		retry++;
		if(retry > 100)
		{
			return -1;
		}
	}

	/*disable hot-plug*/
	hps_set_enabled(0);

	is_l2_borrowed = 0;

	for(i=1; i<NR_CPUS; i++)
	{
		if(cpu_online(i))
		{
			err = cpu_down(i);
			if(err < 0)
			{
				pr_err("[L2$ sharing] disable cpu %d failed!\n", i);
				
				hps_set_enabled(1);
				return -1;
			}
		}
	}

	/* disable preemption */
	cpu = get_cpu();
	
	/* enable other clusters' power */
	enable_secondary_clusters_pwr();

	config_L2_size(option);

	if(option == BORROW_L2)
	{
		is_l2_borrowed = 1;        
	}
	else // if(option == RETURN_L2)
	{
		is_l2_borrowed = 0;
		/* Disable other clusters' power */
		disable_secondary_clusters_pwr();
	}

	/*enable hot-plug*/
	hps_set_enabled(1);	
	put_cpu();

	t2 = sched_clock();
    
	if(option == BORROW_L2)
	{
		pr_notice("[%s]: borrow L2$ cost %llu ns\n", __func__, t2 - t1);
	}
	else
	{
		pr_notice("[%s]: return L2$ cost %llu ns\n", __func__, t2 - t1);
	}

	return err;
}

static ssize_t cur_l2c_show(struct device_driver *driver, char *buf)
{
	return 0;
}

static ssize_t cur_l2c_store(struct device_driver *driver, const char *buf,
			     size_t count)
{
	char *p = (char *)buf;
	int option, ret;

	option = simple_strtoul(p, &p, 10);

	if(option >= BORROW_NONE) {
		pr_err("wrong option %d\n", option);
		return count;
	}

	pr_notice("config L2 option: %s\n", log[option]);

	ret = switch_L2(option);

	if (ret < 0)
		pr_err("Config L2 error ret:%d by %s\n", ret, log[option]);
	return count;
}

DRIVER_ATTR(current_l2c, 0644, cur_l2c_show, cur_l2c_store);

/*
 * mt_l2c_init: initialize l2c driver.
 * Always return 0.
 */
int mt_l2c_init(void)
{
	struct device_node *node;
	int ret = 0;
    
	node = of_find_compatible_node(NULL, NULL, "mediatek,l2c_share");
	if(node)
	{
		mp0_cache_config = of_iomap(node, 0);
		mp1_cache_config = of_iomap(node, 1);
		of_property_read_u32(node, "mediatek,l2c-share-cluster-num", &g_l2c_share_info.share_cluster_num);
		of_property_read_u32(node, "mediatek,l2c-cluster-borrow", &g_l2c_share_info.cluster_borrow);
		of_property_read_u32(node, "mediatek,l2c-cluster-return", &g_l2c_share_info.cluster_return);

		ret = driver_register(&mt_l2c_drv);
		if (ret){
			printk("fail to register mt_l2c_drv\n");
		}

		ret = driver_create_file(&mt_l2c_drv, &driver_attr_current_l2c);
		if (ret) {
			pr_err("fail to create l2$ sharing sysfs files\n");
		}
	}
	else
	{
		pr_err("[L2$ sharing] failed to find l2$ node in DT!\n");
		ret =  -1;
	}

	return ret;
}

module_init(mt_l2c_init);

EXPORT_SYMBOL(switch_L2);
EXPORT_SYMBOL(IS_L2_BORROWED);