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