blob: d3d0537d74351008b992ff65d2e44ad7c9b70f27 [file] [log] [blame]
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -04001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/sysrq.h>
16#include <linux/time.h>
17#include <linux/proc_fs.h>
18#include <linux/kernel_stat.h>
19#include <linux/uaccess.h>
20#include <linux/sysdev.h>
21#include <linux/delay.h>
22#include <linux/device.h>
23#include <linux/kernel.h>
24#include <linux/spinlock.h>
25#include <linux/semaphore.h>
26#include <linux/file.h>
27#include <linux/percpu.h>
28#include <linux/string.h>
29#include <linux/smp.h>
30#include <asm/cacheflush.h>
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040031#include <asm/smp_plat.h>
32
33#ifdef CONFIG_ARCH_MSM_KRAIT
34#include <mach/msm-krait-l2-accessors.h>
35#endif
36
37#define TYPE_MAX_CHARACTERS 10
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39/*
40 * CP parameters
41 */
42struct cp_params {
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040043 unsigned long il2index;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044 unsigned long cp;
45 unsigned long op1;
46 unsigned long op2;
47 unsigned long crn;
48 unsigned long crm;
49 unsigned long write_value;
50 char rw;
51};
52
53static struct semaphore cp_sem;
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040054static unsigned long il2_output;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055static int cpu;
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040056char type[TYPE_MAX_CHARACTERS] = "C";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057
58static DEFINE_PER_CPU(struct cp_params, cp_param)
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040059 = { 0, 15, 0, 0, 0, 0, 0, 'r' };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060
61static struct sysdev_class cpaccess_sysclass = {
62 .name = "cpaccess",
63};
64
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040065#ifdef CONFIG_ARCH_MSM_KRAIT
66/*
67 * do_read_il2 - Read indirect L2 registers
68 * @ret: Pointer to return value
69 *
70 */
71static void do_read_il2(void *ret)
72{
73 *(unsigned long *)ret =
74 get_l2_indirect_reg(per_cpu(cp_param.il2index, cpu));
75}
76
77/*
78 * do_write_il2 - Write indirect L2 registers
79 * @ret: Pointer to return value
80 *
81 */
82static void do_write_il2(void *ret)
83{
84 *(unsigned long *)ret =
85 set_get_l2_indirect_reg(per_cpu(cp_param.il2index, cpu),
86 per_cpu(cp_param.write_value, cpu));
87}
88
89/*
90 * do_il2_rw - Call Read/Write indirect L2 register functions
91 * @ret: Pointer to return value in case of CP register
92 *
93 */
94static int do_il2_rw(char *str_tmp)
95{
96 unsigned long write_value, il2index;
97 char rw;
98 int ret = 0;
99
100 il2index = 0;
101 sscanf(str_tmp, "%lx:%c:%lx:%d", &il2index, &rw, &write_value,
102 &cpu);
103 per_cpu(cp_param.il2index, cpu) = il2index;
104 per_cpu(cp_param.rw, cpu) = rw;
105 per_cpu(cp_param.write_value, cpu) = write_value;
106
107 if (per_cpu(cp_param.rw, cpu) == 'r') {
108 if (is_smp()) {
109 if (smp_call_function_single(cpu, do_read_il2,
110 &il2_output, 1))
111 pr_err("Error cpaccess smp call single\n");
112 } else
113 do_read_il2(&il2_output);
114 } else if (per_cpu(cp_param.rw, cpu) == 'w') {
115 if (is_smp()) {
116 if (smp_call_function_single(cpu, do_write_il2,
117 &il2_output, 1))
118 pr_err("Error cpaccess smp call single\n");
119 } else
120 do_write_il2(&il2_output);
121 } else {
122 pr_err("cpaccess: Wrong Entry for 'r' or 'w'.\n");
123 return -EINVAL;
124 }
125 return ret;
126}
127#else
128static void do_il2_rw(char *str_tmp)
129{
130 il2_output = 0;
131}
132#endif
133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134/*
135 * get_asm_value - Dummy fuction
136 * @write_val: Write value incase of a CP register write operation.
137 *
138 * This function is just a placeholder. The first 2 instructions
139 * will be inserted to perform MRC/MCR instruction and a return.
140 * See do_cpregister_rw function. Value passed to function is
141 * accessed from r0 register.
142 */
143static noinline unsigned long cpaccess_dummy(unsigned long write_val)
144{
145 asm("mrc p15, 0, r0, c0, c0, 0\n\t");
146 asm("bx lr\n\t");
147 return 0xBEEF;
148} __attribute__((aligned(32)))
149
150/*
151 * get_asm_value - Read/Write CP registers
152 * @ret: Pointer to return value in case of CP register
153 * read op.
154 *
155 */
156static void get_asm_value(void *ret)
157{
158 *(unsigned long *)ret =
159 cpaccess_dummy(per_cpu(cp_param.write_value, cpu));
160}
161
162/*
163 * dp_cpregister_rw - Read/Write CP registers
164 * @write: 1 for Write and 0 for Read operation
165 *
166 * Returns value read from CP register
167 */
168static unsigned long do_cpregister_rw(int write)
169{
170 unsigned long opcode, ret, *p_opcode;
171
172 /*
173 * Mask the crn, crm, op1, op2 and cp values so they do not
174 * interfer with other fields of the op code.
175 */
176 per_cpu(cp_param.cp, cpu) &= 0xF;
177 per_cpu(cp_param.crn, cpu) &= 0xF;
178 per_cpu(cp_param.crm, cpu) &= 0xF;
179 per_cpu(cp_param.op1, cpu) &= 0x7;
180 per_cpu(cp_param.op2, cpu) &= 0x7;
181
182 /*
183 * Base MRC opcode for MIDR is EE100010,
184 * MCR is 0xEE000010
185 */
186 opcode = (write == 1 ? 0xEE000010 : 0xEE100010);
187 opcode |= (per_cpu(cp_param.crn, cpu)<<16) |
188 (per_cpu(cp_param.crm, cpu)<<0) |
189 (per_cpu(cp_param.op1, cpu)<<21) |
190 (per_cpu(cp_param.op2, cpu)<<5) |
191 (per_cpu(cp_param.cp, cpu) << 8);
192
193 /*
194 * Grab address of the Dummy function, insert MRC/MCR
195 * instruction and a return instruction ("bx lr"). Do
196 * a D cache clean and I cache invalidate after inserting
197 * new code.
198 */
199 p_opcode = (unsigned long *)&cpaccess_dummy;
200 *p_opcode++ = opcode;
201 *p_opcode-- = 0xE12FFF1E;
202 __cpuc_coherent_kern_range((unsigned long)p_opcode,
203 ((unsigned long)p_opcode + (sizeof(long) * 2)));
204
205#ifdef CONFIG_SMP
206 /*
207 * Use smp_call_function_single to do CPU core specific
208 * get_asm_value function call.
209 */
210 if (smp_call_function_single(cpu, get_asm_value, &ret, 1))
211 printk(KERN_ERR "Error cpaccess smp call single\n");
212#else
213 get_asm_value(&ret);
214#endif
215
216 return ret;
217}
218
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400219static int get_register_params(char *str_tmp)
220{
221 unsigned long op1, op2, crn, crm, cp = 15, write_value, il2index;
222 char rw;
223 int cnt = 0;
224
225 il2index = 0;
226 strncpy(type, strsep(&str_tmp, ":"), TYPE_MAX_CHARACTERS);
227
228 if (strncasecmp(type, "C", TYPE_MAX_CHARACTERS) == 0) {
229
230 sscanf(str_tmp, "%lu:%lu:%lu:%lu:%lu:%c:%lx:%d",
231 &cp, &op1, &crn, &crm, &op2, &rw, &write_value, &cpu);
232 per_cpu(cp_param.cp, cpu) = cp;
233 per_cpu(cp_param.op1, cpu) = op1;
234 per_cpu(cp_param.crn, cpu) = crn;
235 per_cpu(cp_param.crm, cpu) = crm;
236 per_cpu(cp_param.op2, cpu) = op2;
237 per_cpu(cp_param.rw, cpu) = rw;
238 per_cpu(cp_param.write_value, cpu) = write_value;
239
240 if ((per_cpu(cp_param.rw, cpu) != 'w') &&
241 (per_cpu(cp_param.rw, cpu) != 'r')) {
242 pr_err("cpaccess: Wrong entry for 'r' or 'w'.\n");
243 return -EINVAL;
244 }
245
246 if (per_cpu(cp_param.rw, cpu) == 'w')
247 do_cpregister_rw(1);
248 } else if (strncasecmp(type, "IL2", TYPE_MAX_CHARACTERS) == 0)
249 do_il2_rw(str_tmp);
250 else {
251 pr_err("cpaccess: Not a valid type. Entered: %s\n", type);
252 return -EINVAL;
253 }
254
255 return cnt;
256}
257
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258/*
259 * cp_register_write_sysfs - sysfs interface for writing to
260 * CP register
261 * @dev: sys device
262 * @attr: device attribute
263 * @buf: write value
264 * @cnt: not used
265 *
266 */
267static ssize_t cp_register_write_sysfs(struct sys_device *dev,
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400268 struct sysdev_attribute *attr, const char *buf, size_t cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269{
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400270 char *str_tmp = (char *)buf;
271
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 if (down_timeout(&cp_sem, 6000))
273 return -ERESTARTSYS;
274
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400275 get_register_params(str_tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276
277 return cnt;
278}
279
280/*
281 * cp_register_read_sysfs - sysfs interface for reading CP registers
282 * @dev: sys device
283 * @attr: device attribute
284 * @buf: write value
285 *
286 * Code to read in the CPxx crn, crm, op1, op2 variables, or into
287 * the base MRC opcode, store to executable memory, clean/invalidate
288 * caches and then execute the new instruction and provide the
289 * result to the caller.
290 */
291static ssize_t cp_register_read_sysfs(struct sys_device *dev,
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400292 struct sysdev_attribute *attr, char *buf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293{
294 int ret;
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400295
296 if (strncasecmp(type, "C", TYPE_MAX_CHARACTERS) == 0)
297 ret = snprintf(buf, TYPE_MAX_CHARACTERS, "%lx\n",
298 do_cpregister_rw(0));
299 else if (strncasecmp(type, "IL2", TYPE_MAX_CHARACTERS) == 0)
300 ret = snprintf(buf, TYPE_MAX_CHARACTERS, "%lx\n", il2_output);
301 else
302 ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303
304 if (cp_sem.count <= 0)
305 up(&cp_sem);
306
307 return ret;
308}
309
310/*
311 * Setup sysfs files
312 */
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400313SYSDEV_ATTR(cp_rw, 0644, cp_register_read_sysfs, cp_register_write_sysfs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314
315static struct sys_device device_cpaccess = {
316 .id = 0,
317 .cls = &cpaccess_sysclass,
318};
319
320/*
321 * init_cpaccess_sysfs - initialize sys devices
322 */
323static int __init init_cpaccess_sysfs(void)
324{
325 int error = sysdev_class_register(&cpaccess_sysclass);
326
327 if (!error)
328 error = sysdev_register(&device_cpaccess);
329 else
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400330 pr_err("Error initializing cpaccess interface\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700331
332 if (!error)
333 error = sysdev_create_file(&device_cpaccess,
334 &attr_cp_rw);
335 else {
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400336 pr_err("Error initializing cpaccess interface\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337 sysdev_unregister(&device_cpaccess);
338 sysdev_class_unregister(&cpaccess_sysclass);
339 }
340
341 sema_init(&cp_sem, 1);
342
343 return error;
344}
345
346static void __exit exit_cpaccess_sysfs(void)
347{
348 sysdev_remove_file(&device_cpaccess, &attr_cp_rw);
349 sysdev_unregister(&device_cpaccess);
350 sysdev_class_unregister(&cpaccess_sysclass);
351}
352
353module_init(init_cpaccess_sysfs);
354module_exit(exit_cpaccess_sysfs);
355MODULE_LICENSE("GPL v2");