blob: e71e318576a4dbb25e41167ff7965f99b8b6f221 [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>
Neil Leeder7673a872011-11-07 11:36:39 -050032#include <asm/mmu_writeable.h>
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040033
34#ifdef CONFIG_ARCH_MSM_KRAIT
35#include <mach/msm-krait-l2-accessors.h>
36#endif
37
38#define TYPE_MAX_CHARACTERS 10
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40/*
41 * CP parameters
42 */
43struct cp_params {
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040044 unsigned long il2index;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045 unsigned long cp;
46 unsigned long op1;
47 unsigned long op2;
48 unsigned long crn;
49 unsigned long crm;
50 unsigned long write_value;
51 char rw;
52};
53
54static struct semaphore cp_sem;
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040055static unsigned long il2_output;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056static int cpu;
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040057char type[TYPE_MAX_CHARACTERS] = "C";
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058
59static DEFINE_PER_CPU(struct cp_params, cp_param)
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040060 = { 0, 15, 0, 0, 0, 0, 0, 'r' };
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061
62static struct sysdev_class cpaccess_sysclass = {
63 .name = "cpaccess",
64};
65
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -040066#ifdef CONFIG_ARCH_MSM_KRAIT
67/*
68 * do_read_il2 - Read indirect L2 registers
69 * @ret: Pointer to return value
70 *
71 */
72static void do_read_il2(void *ret)
73{
74 *(unsigned long *)ret =
75 get_l2_indirect_reg(per_cpu(cp_param.il2index, cpu));
76}
77
78/*
79 * do_write_il2 - Write indirect L2 registers
80 * @ret: Pointer to return value
81 *
82 */
83static void do_write_il2(void *ret)
84{
85 *(unsigned long *)ret =
86 set_get_l2_indirect_reg(per_cpu(cp_param.il2index, cpu),
87 per_cpu(cp_param.write_value, cpu));
88}
89
90/*
91 * do_il2_rw - Call Read/Write indirect L2 register functions
92 * @ret: Pointer to return value in case of CP register
93 *
94 */
95static int do_il2_rw(char *str_tmp)
96{
97 unsigned long write_value, il2index;
98 char rw;
99 int ret = 0;
100
101 il2index = 0;
102 sscanf(str_tmp, "%lx:%c:%lx:%d", &il2index, &rw, &write_value,
103 &cpu);
104 per_cpu(cp_param.il2index, cpu) = il2index;
105 per_cpu(cp_param.rw, cpu) = rw;
106 per_cpu(cp_param.write_value, cpu) = write_value;
107
108 if (per_cpu(cp_param.rw, cpu) == 'r') {
109 if (is_smp()) {
110 if (smp_call_function_single(cpu, do_read_il2,
111 &il2_output, 1))
112 pr_err("Error cpaccess smp call single\n");
113 } else
114 do_read_il2(&il2_output);
115 } else if (per_cpu(cp_param.rw, cpu) == 'w') {
116 if (is_smp()) {
117 if (smp_call_function_single(cpu, do_write_il2,
118 &il2_output, 1))
119 pr_err("Error cpaccess smp call single\n");
120 } else
121 do_write_il2(&il2_output);
122 } else {
123 pr_err("cpaccess: Wrong Entry for 'r' or 'w'.\n");
124 return -EINVAL;
125 }
126 return ret;
127}
128#else
129static void do_il2_rw(char *str_tmp)
130{
131 il2_output = 0;
132}
133#endif
134
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135/*
136 * get_asm_value - Dummy fuction
137 * @write_val: Write value incase of a CP register write operation.
138 *
139 * This function is just a placeholder. The first 2 instructions
140 * will be inserted to perform MRC/MCR instruction and a return.
141 * See do_cpregister_rw function. Value passed to function is
142 * accessed from r0 register.
143 */
144static noinline unsigned long cpaccess_dummy(unsigned long write_val)
145{
146 asm("mrc p15, 0, r0, c0, c0, 0\n\t");
147 asm("bx lr\n\t");
148 return 0xBEEF;
149} __attribute__((aligned(32)))
150
151/*
152 * get_asm_value - Read/Write CP registers
153 * @ret: Pointer to return value in case of CP register
154 * read op.
155 *
156 */
157static void get_asm_value(void *ret)
158{
159 *(unsigned long *)ret =
160 cpaccess_dummy(per_cpu(cp_param.write_value, cpu));
161}
162
163/*
164 * dp_cpregister_rw - Read/Write CP registers
165 * @write: 1 for Write and 0 for Read operation
166 *
167 * Returns value read from CP register
168 */
169static unsigned long do_cpregister_rw(int write)
170{
171 unsigned long opcode, ret, *p_opcode;
172
173 /*
174 * Mask the crn, crm, op1, op2 and cp values so they do not
175 * interfer with other fields of the op code.
176 */
177 per_cpu(cp_param.cp, cpu) &= 0xF;
178 per_cpu(cp_param.crn, cpu) &= 0xF;
179 per_cpu(cp_param.crm, cpu) &= 0xF;
180 per_cpu(cp_param.op1, cpu) &= 0x7;
181 per_cpu(cp_param.op2, cpu) &= 0x7;
182
183 /*
184 * Base MRC opcode for MIDR is EE100010,
185 * MCR is 0xEE000010
186 */
187 opcode = (write == 1 ? 0xEE000010 : 0xEE100010);
188 opcode |= (per_cpu(cp_param.crn, cpu)<<16) |
189 (per_cpu(cp_param.crm, cpu)<<0) |
190 (per_cpu(cp_param.op1, cpu)<<21) |
191 (per_cpu(cp_param.op2, cpu)<<5) |
192 (per_cpu(cp_param.cp, cpu) << 8);
193
194 /*
Neil Leeder7673a872011-11-07 11:36:39 -0500195 * Grab address of the Dummy function, write the MRC/MCR
196 * instruction, ensuring cache coherency.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197 */
198 p_opcode = (unsigned long *)&cpaccess_dummy;
Neil Leeder7673a872011-11-07 11:36:39 -0500199 mem_text_write_kernel_word(p_opcode, opcode);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200
201#ifdef CONFIG_SMP
202 /*
203 * Use smp_call_function_single to do CPU core specific
204 * get_asm_value function call.
205 */
206 if (smp_call_function_single(cpu, get_asm_value, &ret, 1))
207 printk(KERN_ERR "Error cpaccess smp call single\n");
208#else
209 get_asm_value(&ret);
210#endif
211
212 return ret;
213}
214
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400215static int get_register_params(char *str_tmp)
216{
217 unsigned long op1, op2, crn, crm, cp = 15, write_value, il2index;
218 char rw;
219 int cnt = 0;
220
221 il2index = 0;
222 strncpy(type, strsep(&str_tmp, ":"), TYPE_MAX_CHARACTERS);
223
224 if (strncasecmp(type, "C", TYPE_MAX_CHARACTERS) == 0) {
225
226 sscanf(str_tmp, "%lu:%lu:%lu:%lu:%lu:%c:%lx:%d",
227 &cp, &op1, &crn, &crm, &op2, &rw, &write_value, &cpu);
228 per_cpu(cp_param.cp, cpu) = cp;
229 per_cpu(cp_param.op1, cpu) = op1;
230 per_cpu(cp_param.crn, cpu) = crn;
231 per_cpu(cp_param.crm, cpu) = crm;
232 per_cpu(cp_param.op2, cpu) = op2;
233 per_cpu(cp_param.rw, cpu) = rw;
234 per_cpu(cp_param.write_value, cpu) = write_value;
235
236 if ((per_cpu(cp_param.rw, cpu) != 'w') &&
237 (per_cpu(cp_param.rw, cpu) != 'r')) {
238 pr_err("cpaccess: Wrong entry for 'r' or 'w'.\n");
239 return -EINVAL;
240 }
241
242 if (per_cpu(cp_param.rw, cpu) == 'w')
243 do_cpregister_rw(1);
244 } else if (strncasecmp(type, "IL2", TYPE_MAX_CHARACTERS) == 0)
245 do_il2_rw(str_tmp);
246 else {
247 pr_err("cpaccess: Not a valid type. Entered: %s\n", type);
248 return -EINVAL;
249 }
250
251 return cnt;
252}
253
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254/*
255 * cp_register_write_sysfs - sysfs interface for writing to
256 * CP register
257 * @dev: sys device
258 * @attr: device attribute
259 * @buf: write value
260 * @cnt: not used
261 *
262 */
263static ssize_t cp_register_write_sysfs(struct sys_device *dev,
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400264 struct sysdev_attribute *attr, const char *buf, size_t cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265{
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400266 char *str_tmp = (char *)buf;
267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 if (down_timeout(&cp_sem, 6000))
269 return -ERESTARTSYS;
270
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400271 get_register_params(str_tmp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272
273 return cnt;
274}
275
276/*
277 * cp_register_read_sysfs - sysfs interface for reading CP registers
278 * @dev: sys device
279 * @attr: device attribute
280 * @buf: write value
281 *
282 * Code to read in the CPxx crn, crm, op1, op2 variables, or into
283 * the base MRC opcode, store to executable memory, clean/invalidate
284 * caches and then execute the new instruction and provide the
285 * result to the caller.
286 */
287static ssize_t cp_register_read_sysfs(struct sys_device *dev,
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400288 struct sysdev_attribute *attr, char *buf)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289{
290 int ret;
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400291
292 if (strncasecmp(type, "C", TYPE_MAX_CHARACTERS) == 0)
293 ret = snprintf(buf, TYPE_MAX_CHARACTERS, "%lx\n",
294 do_cpregister_rw(0));
295 else if (strncasecmp(type, "IL2", TYPE_MAX_CHARACTERS) == 0)
296 ret = snprintf(buf, TYPE_MAX_CHARACTERS, "%lx\n", il2_output);
297 else
298 ret = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299
300 if (cp_sem.count <= 0)
301 up(&cp_sem);
302
303 return ret;
304}
305
306/*
307 * Setup sysfs files
308 */
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400309SYSDEV_ATTR(cp_rw, 0644, cp_register_read_sysfs, cp_register_write_sysfs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310
311static struct sys_device device_cpaccess = {
312 .id = 0,
313 .cls = &cpaccess_sysclass,
314};
315
316/*
317 * init_cpaccess_sysfs - initialize sys devices
318 */
319static int __init init_cpaccess_sysfs(void)
320{
321 int error = sysdev_class_register(&cpaccess_sysclass);
322
323 if (!error)
324 error = sysdev_register(&device_cpaccess);
325 else
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400326 pr_err("Error initializing cpaccess interface\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
328 if (!error)
329 error = sysdev_create_file(&device_cpaccess,
330 &attr_cp_rw);
331 else {
Suren Eda Naarayana Kulothungan4c317fb2011-04-20 21:12:01 -0400332 pr_err("Error initializing cpaccess interface\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 sysdev_unregister(&device_cpaccess);
334 sysdev_class_unregister(&cpaccess_sysclass);
335 }
336
337 sema_init(&cp_sem, 1);
338
339 return error;
340}
341
342static void __exit exit_cpaccess_sysfs(void)
343{
344 sysdev_remove_file(&device_cpaccess, &attr_cp_rw);
345 sysdev_unregister(&device_cpaccess);
346 sysdev_class_unregister(&cpaccess_sysclass);
347}
348
349module_init(init_cpaccess_sysfs);
350module_exit(exit_cpaccess_sysfs);
351MODULE_LICENSE("GPL v2");