2 * wARM - exporting ARM processor specific privileged services to userspace
5 * Author: GraÅžvydas "notaz" Ignotas
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/proc_fs.h>
15 #include <linux/delay.h>
17 #include <linux/seq_file.h>
19 #include <linux/version.h>
20 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
21 #include <linux/uaccess.h>
23 #include <linux/init.h>
24 #include <linux/moduleparam.h>
25 #include <asm/uaccess.h>
27 #define unlocked_ioctl ioctl
34 #ifndef CONFIG_PROC_FS
41 #define WARM_INFO(fmt, ...) \
43 pr_info(PFX fmt, ##__VA_ARGS__)
45 #define MAX_CACHEOP_RANGE 16384
47 /* assume RAM starts at phys addr 0 (this is really machine specific) */
48 #define RAM_PHYS_START 0
49 #define RAM_MAX_SIZE 0x10000000 /* 256M, try to be future proof */
52 #if defined(CONFIG_CPU_ARM926T)
53 #define EXPECTED_ID 0x069260
54 #elif defined(CONFIG_CPU_ARM920T)
55 #define EXPECTED_ID 0x029200
57 #error "unsupported CPU"
60 extern unsigned long max_mapnr;
62 /* "upper" physical memory, not seen by Linux and to be mmap'ed */
63 static u32 uppermem_start;
64 static u32 uppermem_end;
68 static u32 *get_pgtable(void)
72 /* get the pointer to the translation table base... */
73 asm ("mrc p15, 0, %0, c2, c0, 0" : "=r"(ttb));
75 return __va((ttb) & 0xffffc000);
78 static int do_set_cb_uppermem(int in_cb, int is_set)
84 if (uppermem_end <= uppermem_start)
87 if (in_cb & WCB_C_BIT)
89 if (in_cb & WCB_B_BIT)
92 pgtable = get_pgtable();
94 for (i = 0; i < 4096; i++)
96 if ((pgtable[i] & 3) != 1)
97 /* must be coarse page table */
100 cpt = __va(pgtable[i] & 0xfffffc00);
102 for (j = 0; j < 256; j++)
111 addr = entry & 0xfffff000;
112 if (uppermem_start <= addr && addr < uppermem_end)
114 pr_debug(PFX "%s C&B bits %08x\n",
115 is_set ? "set" : "clear", entry);
122 /* need to also set AP bits (so that no faults
123 * happen and kernel doesn't touch this after us) */
124 if ((entry & 3) == 3)
125 entry |= 0x030; /* tiny page */
136 warm_drain_wb_inval_tlb();
138 WARM_INFO("%c%c bit(s) %s for phys %08x-%08x (%d pages)\n",
139 bits & 8 ? 'c' : ' ', bits & 4 ? 'b' : ' ',
140 is_set ? "set" : "cleared",
141 uppermem_start, uppermem_end - 1, count);
146 static int do_set_cb_virt(int in_cb, int is_set, u32 addr, u32 size)
148 int count = 0, bits = 0;
153 if (in_cb & WCB_C_BIT)
155 if (in_cb & WCB_B_BIT)
158 size += addr & (PAGE_SIZE - 1);
159 size = ALIGN(size, PAGE_SIZE);
161 addr &= ~(PAGE_SIZE - 1);
165 pgtable = get_pgtable();
167 for (; addr < end; addr += PAGE_SIZE)
169 desc1 = pgtable[addr >> 20];
171 if ((desc1 & 3) != 1) {
172 printk(KERN_WARNING PFX "not coarse table? %08x %08x\n", desc1, addr);
176 cpt = __va(desc1 & 0xfffffc00);
177 desc2 = cpt[(addr >> 12) & 0xff];
179 if ((desc2 & 3) != 2) {
180 printk(KERN_WARNING PFX "not small page? %08x %08x\n", desc2, addr);
190 cpt[(addr >> 12) & 0xff] = desc2;
195 warm_drain_wb_inval_tlb();
197 WARM_INFO("%c%c bit(s) %s virt %08x-%08x (%d pages)\n",
198 bits & 8 ? 'c' : ' ', bits & 4 ? 'b' : ' ',
199 is_set ? "set" : "cleared", start, end - 1, count);
204 static int do_virt2phys(unsigned long *_addr)
210 pgtable = get_pgtable();
211 desc1 = pgtable[addr >> 20];
214 case 1: /* coarse table */
215 cpt = __va(desc1 & 0xfffffc00);
216 desc2 = cpt[(addr >> 12) & 0xff];
218 case 2: /* 1MB section */
219 *_addr = (desc1 & 0xfff00000) | (addr & 0xfffff);
221 case 3: /* fine table */
222 cpt = __va(desc1 & 0xfffff000);
223 desc2 = cpt[(addr >> 10) & 0x3ff];
230 case 1: /* large page */
231 *_addr = (desc2 & 0xffff0000) | (addr & 0xffff);
233 case 2: /* small page */
234 *_addr = (desc2 & 0xfffff000) | (addr & 0x0fff);
236 case 3: /* tiny page */
237 *_addr = (desc2 & 0xfffffc00) | (addr & 0x03ff);
246 static int do_cache_ops_whole(int ops)
248 if ((ops & (WOP_D_CLEAN|WOP_D_INVALIDATE)) == (WOP_D_CLEAN|WOP_D_INVALIDATE))
249 warm_cop_clean_inval_d();
251 else if (ops & WOP_D_CLEAN)
254 else if (ops & WOP_D_INVALIDATE) {
255 printk(KERN_WARNING PFX "invalidate without clean is dangerous!\n");
259 if (ops & WOP_I_INVALIDATE)
266 static int do_cache_ops(int ops, u32 addr, u32 size)
274 case WOP_D_CLEAN|WOP_D_INVALIDATE|WOP_I_INVALIDATE:
275 warm_cop_r_clean_d_inval_di(addr, size);
277 case WOP_D_CLEAN|WOP_D_INVALIDATE:
278 warm_cop_r_clean_d_inval_d(addr, size);
280 case WOP_D_CLEAN|WOP_I_INVALIDATE:
281 warm_cop_r_clean_d_inval_i(addr, size);
284 warm_cop_r_clean_d(addr, size);
286 case WOP_D_INVALIDATE|WOP_I_INVALIDATE:
287 warm_cop_r_inval_di(addr, size);
289 case WOP_D_INVALIDATE:
290 warm_cop_r_inval_d(addr, size);
292 case WOP_I_INVALIDATE:
293 warm_cop_r_inval_i(addr, size);
304 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
305 static long warm_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
307 static int warm_ioctl(struct inode *inode, struct file *file,
308 unsigned int cmd, unsigned long __arg)
311 void __user *arg = (void __user *) __arg;
313 struct warm_cache_op wcop;
314 struct warm_change_cb ccb;
321 if (copy_from_user(&u.wcop, arg, sizeof(u.wcop)))
323 if (u.wcop.ops & ~(WOP_D_CLEAN|WOP_D_INVALIDATE|WOP_I_INVALIDATE))
325 if (u.wcop.size == (unsigned long)-1 ||
326 (u.wcop.size > MAX_CACHEOP_RANGE && !(u.wcop.ops & WOP_D_INVALIDATE)))
327 ret = do_cache_ops_whole(u.wcop.ops);
329 ret = do_cache_ops(u.wcop.ops, u.wcop.addr, u.wcop.size);
331 case WARMC_CHANGE_CB:
332 if (copy_from_user(&u.ccb, arg, sizeof(u.ccb)))
334 if (u.ccb.cb & ~(WCB_C_BIT|WCB_B_BIT))
336 if (u.ccb.addr == 0 && u.ccb.size == 0)
337 ret = do_set_cb_uppermem(u.ccb.cb, u.ccb.is_set);
339 ret = do_set_cb_virt(u.ccb.cb, u.ccb.is_set, u.ccb.addr, u.ccb.size);
341 case WARMC_VIRT2PHYS:
342 if (copy_from_user(&u.addr, arg, sizeof(u.addr)))
344 ret = do_virt2phys(&u.addr);
345 if (copy_to_user(arg, &u.addr, sizeof(u.addr)))
356 static const char *warm_implementor_name(char code)
366 return "Motorola - Freescale";
373 static const char *warm_arch_name(int code)
394 static int warm_cache_size(int code, int m)
402 static int warm_cache_assoc(int code, int m)
409 return base << (code - 1);
412 static int warm_cache_line(int code)
417 static int warm_seq_show(struct seq_file *seq, void *offset)
421 seq_printf(seq, "wARM: " WARM_VER "\n");
424 asm ("mrc p15, 0, %0, c0, c0, 0" : "=r"(tmp));
425 seq_printf(seq, "ID: %08x\n", tmp);
427 /* revised format, not yet documented */
428 } else if ((tmp & 0xf000) == 0) {
430 seq_printf(seq, "Architecture: %d\n",
431 (tmp & 0xf00) == 0x600 ? 3 : 2);
432 seq_printf(seq, "Variant: %d%d0\n", (tmp & 0xf00) >> 8,
435 seq_printf(seq, "Implementor: %c (%s)\n", tmp >> 24,
436 warm_implementor_name(tmp >> 24));
437 if ((tmp & 0xf000) == 7) {
438 seq_printf(seq, "Architecture: %s\n",
439 tmp & 0x800000 ? "4T" : "3");
440 seq_printf(seq, "Variant: 0x%x\n", (tmp & 0x7f0000) >> 16);
442 seq_printf(seq, "Architecture: %s\n",
443 warm_arch_name((tmp & 0x0f0000) >> 16));
444 seq_printf(seq, "Variant: 0x%x\n", (tmp & 0xf00000) >> 20);
446 seq_printf(seq, "Part number: 0x%x\n", (tmp & 0xfff0) >> 4);
448 seq_printf(seq, "Revision: 0x%x\n", tmp & 0xf);
451 asm ("mrc p15, 0, %0, c0, c0, 1" : "=r"(tmp));
452 seq_printf(seq, "Cache ctype: 0x%x\n", (tmp & 0x1e000000) >> 25);
453 seq_printf(seq, "Cache unified: %s\n", (tmp & 0x01000000) ? "no" : "yes");
454 seq_printf(seq, "DCache size: %d\n",
455 warm_cache_size((tmp >> (6+12)) & 0xf, (tmp >> (2+12)) & 1));
456 seq_printf(seq, "DCache associativity: %d\n",
457 warm_cache_assoc((tmp >> (3+12)) & 7, (tmp >> (2+12)) & 1));
458 seq_printf(seq, "DCache line size: %d\n",
459 warm_cache_line((tmp >> (0+12)) & 3));
460 seq_printf(seq, "ICache size: %d\n",
461 warm_cache_size((tmp >> 6) & 0xf, (tmp >> 2) & 1));
462 seq_printf(seq, "ICache associativity: %d\n",
463 warm_cache_assoc((tmp >> 3) & 7, (tmp >> 2) & 1));
464 seq_printf(seq, "ICache line size: %d\n",
465 warm_cache_line((tmp >> 0) & 3));
470 static int warm_open(struct inode *inode, struct file *file)
472 return single_open(file, warm_seq_show, NULL);
475 static int warm_close(struct inode *ino, struct file *file)
477 return single_release(ino, file);
480 static const struct file_operations warm_fops = {
481 .owner = THIS_MODULE,
485 .unlocked_ioctl = warm_ioctl,
486 .release = warm_close,
489 static int __init warm_module_init(void)
491 struct proc_dir_entry *pret;
494 asm ("mrc p15, 0, %0, c0, c0, 0" : "=r"(cpuid));
495 if ((cpuid & 0x0ffff0) != EXPECTED_ID) {
496 printk(KERN_ERR PFX "module was compiled for different CPU, aborting\n");
500 pret = create_proc_entry("warm", S_IWUGO | S_IRUGO, NULL);
502 printk(KERN_ERR PFX "can't create proc entry\n");
506 pret->owner = THIS_MODULE;
507 pret->proc_fops = &warm_fops;
509 uppermem_start = RAM_PHYS_START + (max_mapnr << PAGE_SHIFT);
510 uppermem_end = RAM_PHYS_START + RAM_MAX_SIZE;
512 pr_info(PFX WARM_VER " loaded, ");
513 if (uppermem_end <= uppermem_start)
514 printk("no upper mem");
516 printk("upper mem %08x-%08x", uppermem_start, uppermem_end - 1);
519 /* give time for /proc node to appear */
525 static void __exit warm_module_exit(void)
527 remove_proc_entry("warm", NULL);
529 pr_info(PFX "unloaded.\n");
532 module_init(warm_module_init);
533 module_exit(warm_module_exit);
535 module_param(verbose, int, 0644);
537 MODULE_LICENSE("GPL");
538 MODULE_DESCRIPTION("ARM processor services");
539 MODULE_AUTHOR("Grazvydas Ignotas");