ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / kernel / ppc_htab.c
1 /*
2  * PowerPC hash table management proc entry.  Will show information
3  * about the current hash table and will allow changes to it.
4  *
5  * Written by Cort Dougan (cort@cs.nmt.edu)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/proc_fs.h>
17 #include <linux/stat.h>
18 #include <linux/sysctl.h>
19 #include <linux/ctype.h>
20 #include <linux/threads.h>
21 #include <linux/smp_lock.h>
22
23 #include <asm/uaccess.h>
24 #include <asm/bitops.h>
25 #include <asm/mmu.h>
26 #include <asm/residual.h>
27 #include <asm/io.h>
28 #include <asm/pgtable.h>
29 #include <asm/cputable.h>
30 #include <asm/system.h>
31 #include <asm/reg.h>
32
33 static ssize_t ppc_htab_read(struct file * file, char __user * buf,
34                              size_t count, loff_t *ppos);
35 static ssize_t ppc_htab_write(struct file * file, const char __user * buffer,
36                               size_t count, loff_t *ppos);
37 static long long ppc_htab_lseek(struct file * file, loff_t offset, int orig);
38 int proc_dol2crvec(ctl_table *table, int write, struct file *filp,
39                   void __user *buffer, size_t *lenp);
40
41 extern PTE *Hash, *Hash_end;
42 extern unsigned long Hash_size, Hash_mask;
43 extern unsigned long _SDR1;
44 extern unsigned long htab_reloads;
45 extern unsigned long htab_preloads;
46 extern unsigned long htab_evicts;
47 extern unsigned long pte_misses;
48 extern unsigned long pte_errors;
49 extern unsigned int primary_pteg_full;
50 extern unsigned int htab_hash_searches;
51
52 struct file_operations ppc_htab_operations = {
53         .llseek =       ppc_htab_lseek,
54         .read =         ppc_htab_read,
55         .write =        ppc_htab_write,
56 };
57
58 static char *pmc1_lookup(unsigned long mmcr0)
59 {
60         switch ( mmcr0 & (0x7f<<7) )
61         {
62         case 0x0:
63                 return "none";
64         case MMCR0_PMC1_CYCLES:
65                 return "cycles";
66         case MMCR0_PMC1_ICACHEMISS:
67                 return "ic miss";
68         case MMCR0_PMC1_DTLB:
69                 return "dtlb miss";
70         default:
71                 return "unknown";
72         }
73 }
74
75 static char *pmc2_lookup(unsigned long mmcr0)
76 {
77         switch ( mmcr0 & 0x3f )
78         {
79         case 0x0:
80                 return "none";
81         case MMCR0_PMC2_CYCLES:
82                 return "cycles";
83         case MMCR0_PMC2_DCACHEMISS:
84                 return "dc miss";
85         case MMCR0_PMC2_ITLB:
86                 return "itlb miss";
87         case MMCR0_PMC2_LOADMISSTIME:
88                 return "load miss time";
89         default:
90                 return "unknown";
91         }
92 }
93
94 /*
95  * print some useful info about the hash table.  This function
96  * is _REALLY_ slow (see the nested for loops below) but nothing
97  * in here should be really timing critical. -- Cort
98  */
99 static ssize_t ppc_htab_read(struct file * file, char __user * buf,
100                              size_t count, loff_t *ppos)
101 {
102         unsigned long mmcr0 = 0, pmc1 = 0, pmc2 = 0;
103         int n = 0;
104 #ifdef CONFIG_PPC_STD_MMU
105         unsigned int kptes = 0, uptes = 0;
106         PTE *ptr;
107 #endif /* CONFIG_PPC_STD_MMU */
108         char buffer[512];
109
110         if (count < 0)
111                 return -EINVAL;
112
113         if (cur_cpu_spec[0]->cpu_features & CPU_FTR_604_PERF_MON) {
114                 mmcr0 = mfspr(SPRN_MMCR0);
115                 pmc1 = mfspr(SPRN_PMC1);
116                 pmc2 = mfspr(SPRN_PMC2);
117                 n += sprintf( buffer + n,
118                               "604 Performance Monitoring\n"
119                               "MMCR0\t\t: %08lx %s%s ",
120                               mmcr0,
121                               ( mmcr0>>28 & 0x2 ) ? "(user mode counted)" : "",
122                               ( mmcr0>>28 & 0x4 ) ? "(kernel mode counted)" : "");
123                 n += sprintf( buffer + n,
124                               "\nPMC1\t\t: %08lx (%s)\n"
125                               "PMC2\t\t: %08lx (%s)\n",
126                               pmc1, pmc1_lookup(mmcr0),
127                               pmc2, pmc2_lookup(mmcr0));
128         }
129
130 #ifdef CONFIG_PPC_STD_MMU
131         /* if we don't have a htab */
132         if ( Hash_size == 0 )
133         {
134                 n += sprintf( buffer + n, "No Hash Table used\n");
135                 goto return_string;
136         }
137
138         for (ptr = Hash; ptr < Hash_end; ptr++) {
139                 unsigned int mctx, vsid;
140
141                 if (!ptr->v)
142                         continue;
143                 /* undo the esid skew */
144                 vsid = ptr->vsid;
145                 mctx = ((vsid - (vsid & 0xf) * 0x111) >> 4) & 0xfffff;
146                 if (mctx == 0)
147                         kptes++;
148                 else
149                         uptes++;
150         }
151
152         n += sprintf( buffer + n,
153                       "PTE Hash Table Information\n"
154                       "Size\t\t: %luKb\n"
155                       "Buckets\t\t: %lu\n"
156                       "Address\t\t: %08lx\n"
157                       "Entries\t\t: %lu\n"
158                       "User ptes\t: %u\n"
159                       "Kernel ptes\t: %u\n"
160                       "Percent full\t: %lu%%\n",
161                       (unsigned long)(Hash_size>>10),
162                       (Hash_size/(sizeof(PTE)*8)),
163                       (unsigned long)Hash,
164                       Hash_size/sizeof(PTE),
165                       uptes,
166                       kptes,
167                       ((kptes+uptes)*100) / (Hash_size/sizeof(PTE))
168                 );
169
170         n += sprintf( buffer + n,
171                       "Reloads\t\t: %lu\n"
172                       "Preloads\t: %lu\n"
173                       "Searches\t: %u\n"
174                       "Overflows\t: %u\n"
175                       "Evicts\t\t: %lu\n",
176                       htab_reloads, htab_preloads, htab_hash_searches,
177                       primary_pteg_full, htab_evicts);
178 return_string:
179 #endif /* CONFIG_PPC_STD_MMU */
180
181         n += sprintf( buffer + n,
182                       "Non-error misses: %lu\n"
183                       "Error misses\t: %lu\n",
184                       pte_misses, pte_errors);
185         if (*ppos >= strlen(buffer))
186                 return 0;
187         if (n > strlen(buffer) - *ppos)
188                 n = strlen(buffer) - *ppos;
189         if (n > count)
190                 n = count;
191         if (copy_to_user(buf, buffer + *ppos, n))
192                 return -EFAULT;
193         *ppos += n;
194         return n;
195 }
196
197 /*
198  * Allow user to define performance counters and resize the hash table
199  */
200 static ssize_t ppc_htab_write(struct file * file, const char __user * ubuffer,
201                               size_t count, loff_t *ppos)
202 {
203 #ifdef CONFIG_PPC_STD_MMU
204         unsigned long tmp;
205         char buffer[16];
206
207         if ( current->uid != 0 )
208                 return -EACCES;
209         if (strncpy_from_user(buffer, ubuffer, 15))
210                 return -EFAULT;
211         buffer[15] = 0;
212
213         /* don't set the htab size for now */
214         if ( !strncmp( buffer, "size ", 5) )
215                 return -EBUSY;
216
217         if ( !strncmp( buffer, "reset", 5) )
218         {
219                 if (cur_cpu_spec[0]->cpu_features & CPU_FTR_604_PERF_MON) {
220                         /* reset PMC1 and PMC2 */
221                         mtspr(SPRN_PMC1, 0);
222                         mtspr(SPRN_PMC2, 0);
223                 }
224                 htab_reloads = 0;
225                 htab_evicts = 0;
226                 pte_misses = 0;
227                 pte_errors = 0;
228         }
229
230         /* Everything below here requires the performance monitor feature. */
231         if ( !cur_cpu_spec[0]->cpu_features & CPU_FTR_604_PERF_MON )
232                 return count;
233
234         /* turn off performance monitoring */
235         if ( !strncmp( buffer, "off", 3) )
236         {
237                 mtspr(SPRN_MMCR0, 0);
238                 mtspr(SPRN_PMC1, 0);
239                 mtspr(SPRN_PMC2, 0);
240         }
241
242         if ( !strncmp( buffer, "user", 4) )
243         {
244                 /* setup mmcr0 and clear the correct pmc */
245                 tmp = (mfspr(SPRN_MMCR0) & ~(0x60000000)) | 0x20000000;
246                 mtspr(SPRN_MMCR0, tmp);
247                 mtspr(SPRN_PMC1, 0);
248                 mtspr(SPRN_PMC2, 0);
249         }
250
251         if ( !strncmp( buffer, "kernel", 6) )
252         {
253                 /* setup mmcr0 and clear the correct pmc */
254                 tmp = (mfspr(SPRN_MMCR0) & ~(0x60000000)) | 0x40000000;
255                 mtspr(SPRN_MMCR0, tmp);
256                 mtspr(SPRN_PMC1, 0);
257                 mtspr(SPRN_PMC2, 0);
258         }
259
260         /* PMC1 values */
261         if ( !strncmp( buffer, "dtlb", 4) )
262         {
263                 /* setup mmcr0 and clear the correct pmc */
264                 tmp = (mfspr(SPRN_MMCR0) & ~(0x7F << 7)) | MMCR0_PMC1_DTLB;
265                 mtspr(SPRN_MMCR0, tmp);
266                 mtspr(SPRN_PMC1, 0);
267         }
268
269         if ( !strncmp( buffer, "ic miss", 7) )
270         {
271                 /* setup mmcr0 and clear the correct pmc */
272                 tmp = (mfspr(SPRN_MMCR0) & ~(0x7F<<7)) | MMCR0_PMC1_ICACHEMISS;
273                 mtspr(SPRN_MMCR0, tmp);
274                 mtspr(SPRN_PMC1, 0);
275         }
276
277         /* PMC2 values */
278         if ( !strncmp( buffer, "load miss time", 14) )
279         {
280                 /* setup mmcr0 and clear the correct pmc */
281                asm volatile(
282                        "mfspr %0,%1\n\t"     /* get current mccr0 */
283                        "rlwinm %0,%0,0,0,31-6\n\t"  /* clear bits [26-31] */
284                        "ori   %0,%0,%2 \n\t" /* or in mmcr0 settings */
285                        "mtspr %1,%0 \n\t"    /* set new mccr0 */
286                        "mtspr %3,%4 \n\t"    /* reset the pmc */
287                        : "=r" (tmp)
288                        : "i" (SPRN_MMCR0),
289                        "i" (MMCR0_PMC2_LOADMISSTIME),
290                        "i" (SPRN_PMC2),  "r" (0) );
291         }
292
293         if ( !strncmp( buffer, "itlb", 4) )
294         {
295                 /* setup mmcr0 and clear the correct pmc */
296                asm volatile(
297                        "mfspr %0,%1\n\t"     /* get current mccr0 */
298                        "rlwinm %0,%0,0,0,31-6\n\t"  /* clear bits [26-31] */
299                        "ori   %0,%0,%2 \n\t" /* or in mmcr0 settings */
300                        "mtspr %1,%0 \n\t"    /* set new mccr0 */
301                        "mtspr %3,%4 \n\t"    /* reset the pmc */
302                        : "=r" (tmp)
303                        : "i" (SPRN_MMCR0), "i" (MMCR0_PMC2_ITLB),
304                        "i" (SPRN_PMC2),  "r" (0) );
305         }
306
307         if ( !strncmp( buffer, "dc miss", 7) )
308         {
309                 /* setup mmcr0 and clear the correct pmc */
310                asm volatile(
311                        "mfspr %0,%1\n\t"     /* get current mccr0 */
312                        "rlwinm %0,%0,0,0,31-6\n\t"  /* clear bits [26-31] */
313                        "ori   %0,%0,%2 \n\t" /* or in mmcr0 settings */
314                        "mtspr %1,%0 \n\t"    /* set new mccr0 */
315                        "mtspr %3,%4 \n\t"    /* reset the pmc */
316                        : "=r" (tmp)
317                        : "i" (SPRN_MMCR0), "i" (MMCR0_PMC2_DCACHEMISS),
318                        "i" (SPRN_PMC2),  "r" (0) );
319         }
320
321         return count;
322 #else /* CONFIG_PPC_STD_MMU */
323         return 0;
324 #endif /* CONFIG_PPC_STD_MMU */
325 }
326
327
328 static long long
329 ppc_htab_lseek(struct file * file, loff_t offset, int orig)
330 {
331     long long ret = -EINVAL;
332
333     lock_kernel();
334     switch (orig) {
335     case 0:
336         file->f_pos = offset;
337         ret = file->f_pos;
338         break;
339     case 1:
340         file->f_pos += offset;
341         ret = file->f_pos;
342     }
343     unlock_kernel();
344     return ret;
345 }
346
347 int proc_dol2crvec(ctl_table *table, int write, struct file *filp,
348                   void __user *buffer_arg, size_t *lenp)
349 {
350         int vleft, first=1, len, left, val;
351         char __user *buffer = (char __user *) buffer_arg;
352         #define TMPBUFLEN 256
353         char buf[TMPBUFLEN], *p;
354         static const char *sizestrings[4] = {
355                 "2MB", "256KB", "512KB", "1MB"
356         };
357         static const char *clockstrings[8] = {
358                 "clock disabled", "+1 clock", "+1.5 clock", "reserved(3)",
359                 "+2 clock", "+2.5 clock", "+3 clock", "reserved(7)"
360         };
361         static const char *typestrings[4] = {
362                 "flow-through burst SRAM", "reserved SRAM",
363                 "pipelined burst SRAM", "pipelined late-write SRAM"
364         };
365         static const char *holdstrings[4] = {
366                 "0.5", "1.0", "(reserved2)", "(reserved3)"
367         };
368
369         if (!(cur_cpu_spec[0]->cpu_features & CPU_FTR_L2CR))
370                 return -EFAULT;
371
372         if ( /*!table->maxlen ||*/ (filp->f_pos && !write)) {
373                 *lenp = 0;
374                 return 0;
375         }
376
377         vleft = table->maxlen / sizeof(int);
378         left = *lenp;
379
380         for (; left /*&& vleft--*/; first=0) {
381                 if (write) {
382                         while (left) {
383                                 char c;
384                                 if(get_user(c, buffer))
385                                         return -EFAULT;
386                                 if (!isspace(c))
387                                         break;
388                                 left--;
389                                 buffer++;
390                         }
391                         if (!left)
392                                 break;
393                         len = left;
394                         if (len > TMPBUFLEN-1)
395                                 len = TMPBUFLEN-1;
396                         if(copy_from_user(buf, buffer, len))
397                                 return -EFAULT;
398                         buf[len] = 0;
399                         p = buf;
400                         if (*p < '0' || *p > '9')
401                                 break;
402                         val = simple_strtoul(p, &p, 0);
403                         len = p-buf;
404                         if ((len < left) && *p && !isspace(*p))
405                                 break;
406                         buffer += len;
407                         left -= len;
408                         _set_L2CR(val);
409                 } else {
410                         p = buf;
411                         if (!first)
412                                 *p++ = '\t';
413                         val = _get_L2CR();
414                         p += sprintf(p, "0x%08x: ", val);
415                         p += sprintf(p, " %s", (val >> 31) & 1 ? "enabled" :
416                                         "disabled");
417                         p += sprintf(p, ", %sparity", (val>>30)&1 ? "" : "no ");
418                         p += sprintf(p, ", %s", sizestrings[(val >> 28) & 3]);
419                         p += sprintf(p, ", %s", clockstrings[(val >> 25) & 7]);
420                         p += sprintf(p, ", %s", typestrings[(val >> 23) & 2]);
421                         p += sprintf(p, "%s", (val>>22)&1 ? ", data only" : "");
422                         p += sprintf(p, "%s", (val>>20)&1 ? ", ZZ enabled": "");
423                         p += sprintf(p, ", %s", (val>>19)&1 ? "write-through" :
424                                         "copy-back");
425                         p += sprintf(p, "%s", (val>>18)&1 ? ", testing" : "");
426                         p += sprintf(p, ", %sns hold",holdstrings[(val>>16)&3]);
427                         p += sprintf(p, "%s", (val>>15)&1 ? ", DLL slow" : "");
428                         p += sprintf(p, "%s", (val>>14)&1 ? ", diff clock" :"");
429                         p += sprintf(p, "%s", (val>>13)&1 ? ", DLL bypass" :"");
430
431                         p += sprintf(p,"\n");
432
433                         len = strlen(buf);
434                         if (len > left)
435                                 len = left;
436                         if (copy_to_user(buffer, buf, len))
437                                 return -EFAULT;
438                         left -= len;
439                         buffer += len;
440                         break;
441                 }
442         }
443
444         if (!write && !first && left) {
445                 if(put_user('\n', (char *) buffer))
446                         return -EFAULT;
447                 left--, buffer++;
448         }
449         if (write) {
450                 p = (char *) buffer;
451                 while (left) {
452                         char c;
453                         if(get_user(c, p++))
454                                 return -EFAULT;
455                         if (!isspace(c))
456                                 break;
457                         left--;
458                 }
459         }
460         if (write && first)
461                 return -EINVAL;
462         *lenp -= left;
463         filp->f_pos += *lenp;
464         return 0;
465 }