blob: 241e33953927d31d56041b7e6cdc8547167c6146 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2 *
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>
31
32/*
33 * CP parameters
34 */
35struct cp_params {
36 unsigned long cp;
37 unsigned long op1;
38 unsigned long op2;
39 unsigned long crn;
40 unsigned long crm;
41 unsigned long write_value;
42 char rw;
43};
44
45static struct semaphore cp_sem;
46static int cpu;
47
48static DEFINE_PER_CPU(struct cp_params, cp_param)
49 = { 15, 0, 0, 0, 0, 0, 'r' };
50
51static struct sysdev_class cpaccess_sysclass = {
52 .name = "cpaccess",
53};
54
55/*
56 * get_asm_value - Dummy fuction
57 * @write_val: Write value incase of a CP register write operation.
58 *
59 * This function is just a placeholder. The first 2 instructions
60 * will be inserted to perform MRC/MCR instruction and a return.
61 * See do_cpregister_rw function. Value passed to function is
62 * accessed from r0 register.
63 */
64static noinline unsigned long cpaccess_dummy(unsigned long write_val)
65{
66 asm("mrc p15, 0, r0, c0, c0, 0\n\t");
67 asm("bx lr\n\t");
68 return 0xBEEF;
69} __attribute__((aligned(32)))
70
71/*
72 * get_asm_value - Read/Write CP registers
73 * @ret: Pointer to return value in case of CP register
74 * read op.
75 *
76 */
77static void get_asm_value(void *ret)
78{
79 *(unsigned long *)ret =
80 cpaccess_dummy(per_cpu(cp_param.write_value, cpu));
81}
82
83/*
84 * dp_cpregister_rw - Read/Write CP registers
85 * @write: 1 for Write and 0 for Read operation
86 *
87 * Returns value read from CP register
88 */
89static unsigned long do_cpregister_rw(int write)
90{
91 unsigned long opcode, ret, *p_opcode;
92
93 /*
94 * Mask the crn, crm, op1, op2 and cp values so they do not
95 * interfer with other fields of the op code.
96 */
97 per_cpu(cp_param.cp, cpu) &= 0xF;
98 per_cpu(cp_param.crn, cpu) &= 0xF;
99 per_cpu(cp_param.crm, cpu) &= 0xF;
100 per_cpu(cp_param.op1, cpu) &= 0x7;
101 per_cpu(cp_param.op2, cpu) &= 0x7;
102
103 /*
104 * Base MRC opcode for MIDR is EE100010,
105 * MCR is 0xEE000010
106 */
107 opcode = (write == 1 ? 0xEE000010 : 0xEE100010);
108 opcode |= (per_cpu(cp_param.crn, cpu)<<16) |
109 (per_cpu(cp_param.crm, cpu)<<0) |
110 (per_cpu(cp_param.op1, cpu)<<21) |
111 (per_cpu(cp_param.op2, cpu)<<5) |
112 (per_cpu(cp_param.cp, cpu) << 8);
113
114 /*
115 * Grab address of the Dummy function, insert MRC/MCR
116 * instruction and a return instruction ("bx lr"). Do
117 * a D cache clean and I cache invalidate after inserting
118 * new code.
119 */
120 p_opcode = (unsigned long *)&cpaccess_dummy;
121 *p_opcode++ = opcode;
122 *p_opcode-- = 0xE12FFF1E;
123 __cpuc_coherent_kern_range((unsigned long)p_opcode,
124 ((unsigned long)p_opcode + (sizeof(long) * 2)));
125
126#ifdef CONFIG_SMP
127 /*
128 * Use smp_call_function_single to do CPU core specific
129 * get_asm_value function call.
130 */
131 if (smp_call_function_single(cpu, get_asm_value, &ret, 1))
132 printk(KERN_ERR "Error cpaccess smp call single\n");
133#else
134 get_asm_value(&ret);
135#endif
136
137 return ret;
138}
139
140/*
141 * cp_register_write_sysfs - sysfs interface for writing to
142 * CP register
143 * @dev: sys device
144 * @attr: device attribute
145 * @buf: write value
146 * @cnt: not used
147 *
148 */
149static ssize_t cp_register_write_sysfs(struct sys_device *dev,
150 struct sysdev_attribute *attr, const char *buf, size_t cnt)
151{
152 unsigned long op1, op2, crn, crm, cp = 15, write_value, ret;
153 char rw;
154 if (down_timeout(&cp_sem, 6000))
155 return -ERESTARTSYS;
156
157 sscanf(buf, "%lu:%lu:%lu:%lu:%lu:%c:%lx:%d", &cp, &op1, &crn,
158 &crm, &op2, &rw, &write_value, &cpu);
159 per_cpu(cp_param.cp, cpu) = cp;
160 per_cpu(cp_param.op1, cpu) = op1;
161 per_cpu(cp_param.crn, cpu) = crn;
162 per_cpu(cp_param.crm, cpu) = crm;
163 per_cpu(cp_param.op2, cpu) = op2;
164 per_cpu(cp_param.rw, cpu) = rw;
165 per_cpu(cp_param.write_value, cpu) = write_value;
166
167 if (per_cpu(cp_param.rw, cpu) == 'w') {
168 do_cpregister_rw(1);
169 ret = cnt;
170 }
171
172 if ((per_cpu(cp_param.rw, cpu) != 'w') &&
173 (per_cpu(cp_param.rw, cpu) != 'r')) {
174 ret = -1;
175 printk(KERN_INFO "Wrong Entry for 'r' or 'w'. \
176 Use cp:op1:crn:crm:op2:r/w:write_value.\n");
177 }
178
179 return cnt;
180}
181
182/*
183 * cp_register_read_sysfs - sysfs interface for reading CP registers
184 * @dev: sys device
185 * @attr: device attribute
186 * @buf: write value
187 *
188 * Code to read in the CPxx crn, crm, op1, op2 variables, or into
189 * the base MRC opcode, store to executable memory, clean/invalidate
190 * caches and then execute the new instruction and provide the
191 * result to the caller.
192 */
193static ssize_t cp_register_read_sysfs(struct sys_device *dev,
194 struct sysdev_attribute *attr, char *buf)
195{
196 int ret;
197 ret = sprintf(buf, "%lx\n", do_cpregister_rw(0));
198
199 if (cp_sem.count <= 0)
200 up(&cp_sem);
201
202 return ret;
203}
204
205/*
206 * Setup sysfs files
207 */
208SYSDEV_ATTR(cp_rw, 0644, cp_register_read_sysfs,
209 cp_register_write_sysfs);
210
211static struct sys_device device_cpaccess = {
212 .id = 0,
213 .cls = &cpaccess_sysclass,
214};
215
216/*
217 * init_cpaccess_sysfs - initialize sys devices
218 */
219static int __init init_cpaccess_sysfs(void)
220{
221 int error = sysdev_class_register(&cpaccess_sysclass);
222
223 if (!error)
224 error = sysdev_register(&device_cpaccess);
225 else
226 printk(KERN_ERR "Error initializing cpaccess \
227 interface\n");
228
229 if (!error)
230 error = sysdev_create_file(&device_cpaccess,
231 &attr_cp_rw);
232 else {
233 printk(KERN_ERR "Error initializing cpaccess \
234 interface\n");
235 sysdev_unregister(&device_cpaccess);
236 sysdev_class_unregister(&cpaccess_sysclass);
237 }
238
239 sema_init(&cp_sem, 1);
240
241 return error;
242}
243
244static void __exit exit_cpaccess_sysfs(void)
245{
246 sysdev_remove_file(&device_cpaccess, &attr_cp_rw);
247 sysdev_unregister(&device_cpaccess);
248 sysdev_class_unregister(&cpaccess_sysclass);
249}
250
251module_init(init_cpaccess_sysfs);
252module_exit(exit_cpaccess_sysfs);
253MODULE_LICENSE("GPL v2");