Merge ../linus
[pandora-kernel.git] / arch / frv / kernel / sysctl.c
1 /* sysctl.c: implementation of /proc/sys files relating to FRV specifically
2  *
3  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/config.h>
13 #include <linux/slab.h>
14 #include <linux/sysctl.h>
15 #include <linux/proc_fs.h>
16 #include <linux/init.h>
17 #include <asm/uaccess.h>
18
19 static const char frv_cache_wback[] = "wback";
20 static const char frv_cache_wthru[] = "wthru";
21
22 static void frv_change_dcache_mode(unsigned long newmode)
23 {
24         unsigned long flags, hsr0;
25
26         local_irq_save(flags);
27
28         hsr0 = __get_HSR(0);
29         hsr0 &= ~HSR0_DCE;
30         __set_HSR(0, hsr0);
31
32         asm volatile("  dcef    @(gr0,gr0),#1   \n"
33                      "  membar                  \n"
34                      : : : "memory"
35                      );
36
37         hsr0 = (hsr0 & ~HSR0_CBM) | newmode;
38         __set_HSR(0, hsr0);
39         hsr0 |= HSR0_DCE;
40         __set_HSR(0, hsr0);
41
42         local_irq_restore(flags);
43
44         //printk("HSR0 now %08lx\n", hsr0);
45 }
46
47 /*****************************************************************************/
48 /*
49  * handle requests to dynamically switch the write caching mode delivered by /proc
50  */
51 static int procctl_frv_cachemode(ctl_table *table, int write, struct file *filp,
52                                  void __user *buffer, size_t *lenp, loff_t *ppos)
53 {
54         unsigned long hsr0;
55         char buff[8];
56         int len;
57
58         len = *lenp;
59
60         if (write) {
61                 /* potential state change */
62                 if (len <= 1 || len > sizeof(buff) - 1)
63                         return -EINVAL;
64
65                 if (copy_from_user(buff, buffer, len) != 0)
66                         return -EFAULT;
67
68                 if (buff[len - 1] == '\n')
69                         buff[len - 1] = '\0';
70                 else
71                         buff[len] = '\0';
72
73                 if (strcmp(buff, frv_cache_wback) == 0) {
74                         /* switch dcache into write-back mode */
75                         frv_change_dcache_mode(HSR0_CBM_COPY_BACK);
76                         return 0;
77                 }
78
79                 if (strcmp(buff, frv_cache_wthru) == 0) {
80                         /* switch dcache into write-through mode */
81                         frv_change_dcache_mode(HSR0_CBM_WRITE_THRU);
82                         return 0;
83                 }
84
85                 return -EINVAL;
86         }
87
88         /* read the state */
89         if (filp->f_pos > 0) {
90                 *lenp = 0;
91                 return 0;
92         }
93
94         hsr0 = __get_HSR(0);
95         switch (hsr0 & HSR0_CBM) {
96         case HSR0_CBM_WRITE_THRU:
97                 memcpy(buff, frv_cache_wthru, sizeof(frv_cache_wthru) - 1);
98                 buff[sizeof(frv_cache_wthru) - 1] = '\n';
99                 len = sizeof(frv_cache_wthru);
100                 break;
101         default:
102                 memcpy(buff, frv_cache_wback, sizeof(frv_cache_wback) - 1);
103                 buff[sizeof(frv_cache_wback) - 1] = '\n';
104                 len = sizeof(frv_cache_wback);
105                 break;
106         }
107
108         if (len > *lenp)
109                 len = *lenp;
110
111         if (copy_to_user(buffer, buff, len) != 0)
112                 return -EFAULT;
113
114         *lenp = len;
115         filp->f_pos = len;
116         return 0;
117
118 } /* end procctl_frv_cachemode() */
119
120 /*****************************************************************************/
121 /*
122  * permit the mm_struct the nominated process is using have its MMU context ID pinned
123  */
124 #ifdef CONFIG_MMU
125 static int procctl_frv_pin_cxnr(ctl_table *table, int write, struct file *filp,
126                                 void __user *buffer, size_t *lenp, loff_t *ppos)
127 {
128         pid_t pid;
129         char buff[16], *p;
130         int len;
131
132         len = *lenp;
133
134         if (write) {
135                 /* potential state change */
136                 if (len <= 1 || len > sizeof(buff) - 1)
137                         return -EINVAL;
138
139                 if (copy_from_user(buff, buffer, len) != 0)
140                         return -EFAULT;
141
142                 if (buff[len - 1] == '\n')
143                         buff[len - 1] = '\0';
144                 else
145                         buff[len] = '\0';
146
147                 pid = simple_strtoul(buff, &p, 10);
148                 if (*p)
149                         return -EINVAL;
150
151                 return cxn_pin_by_pid(pid);
152         }
153
154         /* read the currently pinned CXN */
155         if (filp->f_pos > 0) {
156                 *lenp = 0;
157                 return 0;
158         }
159
160         len = snprintf(buff, sizeof(buff), "%d\n", cxn_pinned);
161         if (len > *lenp)
162                 len = *lenp;
163
164         if (copy_to_user(buffer, buff, len) != 0)
165                 return -EFAULT;
166
167         *lenp = len;
168         filp->f_pos = len;
169         return 0;
170
171 } /* end procctl_frv_pin_cxnr() */
172 #endif
173
174 /*
175  * FR-V specific sysctls
176  */
177 static struct ctl_table frv_table[] =
178 {
179         { 1, "cache-mode",      NULL, 0, 0644, NULL, &procctl_frv_cachemode },
180 #ifdef CONFIG_MMU
181         { 2, "pin-cxnr",        NULL, 0, 0644, NULL, &procctl_frv_pin_cxnr },
182 #endif
183         { 0 }
184 };
185
186 /*
187  * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
188  * when all the PM interfaces exist nicely.
189  */
190 #define CTL_FRV 9898
191 static struct ctl_table frv_dir_table[] =
192 {
193         {CTL_FRV, "frv", NULL, 0, 0555, frv_table},
194         {0}
195 };
196
197 /*
198  * Initialize power interface
199  */
200 static int __init frv_sysctl_init(void)
201 {
202         register_sysctl_table(frv_dir_table, 1);
203         return 0;
204 }
205
206 __initcall(frv_sysctl_init);