patch-2_6_7-vs1_9_1_12
[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 #if defined(CONFIG_PPC_STD_MMU) && !defined(CONFIG_PPC64BRIDGE)
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 #ifndef CONFIG_PPC64BRIDGE
139         for (ptr = Hash; ptr < Hash_end; ptr++) {
140                 unsigned int mctx, vsid;
141
142                 if (!ptr->v)
143                         continue;
144                 /* undo the esid skew */
145                 vsid = ptr->vsid;
146                 mctx = ((vsid - (vsid & 0xf) * 0x111) >> 4) & 0xfffff;
147                 if (mctx == 0)
148                         kptes++;
149                 else
150                         uptes++;
151         }
152 #endif
153
154         n += sprintf( buffer + n,
155                       "PTE Hash Table Information\n"
156                       "Size\t\t: %luKb\n"
157                       "Buckets\t\t: %lu\n"
158                       "Address\t\t: %08lx\n"
159                       "Entries\t\t: %lu\n"
160 #ifndef CONFIG_PPC64BRIDGE
161                       "User ptes\t: %u\n"
162                       "Kernel ptes\t: %u\n"
163                       "Percent full\t: %lu%%\n"
164 #endif
165                       , (unsigned long)(Hash_size>>10),
166                       (Hash_size/(sizeof(PTE)*8)),
167                       (unsigned long)Hash,
168                       Hash_size/sizeof(PTE)
169 #ifndef CONFIG_PPC64BRIDGE
170                       , uptes,
171                       kptes,
172                       ((kptes+uptes)*100) / (Hash_size/sizeof(PTE))
173 #endif
174                 );
175
176         n += sprintf( buffer + n,
177                       "Reloads\t\t: %lu\n"
178                       "Preloads\t: %lu\n"
179                       "Searches\t: %u\n"
180                       "Overflows\t: %u\n"
181                       "Evicts\t\t: %lu\n",
182                       htab_reloads, htab_preloads, htab_hash_searches,
183                       primary_pteg_full, htab_evicts);
184 return_string:
185 #endif /* CONFIG_PPC_STD_MMU */
186
187         n += sprintf( buffer + n,
188                       "Non-error misses: %lu\n"
189                       "Error misses\t: %lu\n",
190                       pte_misses, pte_errors);
191         if (*ppos >= strlen(buffer))
192                 return 0;
193         if (n > strlen(buffer) - *ppos)
194                 n = strlen(buffer) - *ppos;
195         if (n > count)
196                 n = count;
197         if (copy_to_user(buf, buffer + *ppos, n))
198                 return -EFAULT;
199         *ppos += n;
200         return n;
201 }
202
203 /*
204  * Allow user to define performance counters and resize the hash table
205  */
206 static ssize_t ppc_htab_write(struct file * file, const char __user * ubuffer,
207                               size_t count, loff_t *ppos)
208 {
209 #ifdef CONFIG_PPC_STD_MMU
210         unsigned long tmp;
211         char buffer[16];
212
213         if ( current->uid != 0 )
214                 return -EACCES;
215         if (strncpy_from_user(buffer, ubuffer, 15))
216                 return -EFAULT;
217         buffer[15] = 0;
218
219         /* don't set the htab size for now */
220         if ( !strncmp( buffer, "size ", 5) )
221                 return -EBUSY;
222
223         if ( !strncmp( buffer, "reset", 5) )
224         {
225                 if (cur_cpu_spec[0]->cpu_features & CPU_FTR_604_PERF_MON) {
226                         /* reset PMC1 and PMC2 */
227                         mtspr(SPRN_PMC1, 0);
228                         mtspr(SPRN_PMC2, 0);
229                 }
230                 htab_reloads = 0;
231                 htab_evicts = 0;
232                 pte_misses = 0;
233                 pte_errors = 0;
234         }
235
236         /* Everything below here requires the performance monitor feature. */
237         if ( !cur_cpu_spec[0]->cpu_features & CPU_FTR_604_PERF_MON )
238                 return count;
239
240         /* turn off performance monitoring */
241         if ( !strncmp( buffer, "off", 3) )
242         {
243                 mtspr(SPRN_MMCR0, 0);
244                 mtspr(SPRN_PMC1, 0);
245                 mtspr(SPRN_PMC2, 0);
246         }
247
248         if ( !strncmp( buffer, "user", 4) )
249         {
250                 /* setup mmcr0 and clear the correct pmc */
251                 tmp = (mfspr(SPRN_MMCR0) & ~(0x60000000)) | 0x20000000;
252                 mtspr(SPRN_MMCR0, tmp);
253                 mtspr(SPRN_PMC1, 0);
254                 mtspr(SPRN_PMC2, 0);
255         }
256
257         if ( !strncmp( buffer, "kernel", 6) )
258         {
259                 /* setup mmcr0 and clear the correct pmc */
260                 tmp = (mfspr(SPRN_MMCR0) & ~(0x60000000)) | 0x40000000;
261                 mtspr(SPRN_MMCR0, tmp);
262                 mtspr(SPRN_PMC1, 0);
263                 mtspr(SPRN_PMC2, 0);
264         }
265
266         /* PMC1 values */
267         if ( !strncmp( buffer, "dtlb", 4) )
268         {
269                 /* setup mmcr0 and clear the correct pmc */
270                 tmp = (mfspr(SPRN_MMCR0) & ~(0x7F << 7)) | MMCR0_PMC1_DTLB;
271                 mtspr(SPRN_MMCR0, tmp);
272                 mtspr(SPRN_PMC1, 0);
273         }
274
275         if ( !strncmp( buffer, "ic miss", 7) )
276         {
277                 /* setup mmcr0 and clear the correct pmc */
278                 tmp = (mfspr(SPRN_MMCR0) & ~(0x7F<<7)) | MMCR0_PMC1_ICACHEMISS;
279                 mtspr(SPRN_MMCR0, tmp);
280                 mtspr(SPRN_PMC1, 0);
281         }
282
283         /* PMC2 values */
284         if ( !strncmp( buffer, "load miss time", 14) )
285         {
286                 /* setup mmcr0 and clear the correct pmc */
287                asm volatile(
288                        "mfspr %0,%1\n\t"     /* get current mccr0 */
289                        "rlwinm %0,%0,0,0,31-6\n\t"  /* clear bits [26-31] */
290                        "ori   %0,%0,%2 \n\t" /* or in mmcr0 settings */
291                        "mtspr %1,%0 \n\t"    /* set new mccr0 */
292                        "mtspr %3,%4 \n\t"    /* reset the pmc */
293                        : "=r" (tmp)
294                        : "i" (SPRN_MMCR0),
295                        "i" (MMCR0_PMC2_LOADMISSTIME),
296                        "i" (SPRN_PMC2),  "r" (0) );
297         }
298
299         if ( !strncmp( buffer, "itlb", 4) )
300         {
301                 /* setup mmcr0 and clear the correct pmc */
302                asm volatile(
303                        "mfspr %0,%1\n\t"     /* get current mccr0 */
304                        "rlwinm %0,%0,0,0,31-6\n\t"  /* clear bits [26-31] */
305                        "ori   %0,%0,%2 \n\t" /* or in mmcr0 settings */
306                        "mtspr %1,%0 \n\t"    /* set new mccr0 */
307                        "mtspr %3,%4 \n\t"    /* reset the pmc */
308                        : "=r" (tmp)
309                        : "i" (SPRN_MMCR0), "i" (MMCR0_PMC2_ITLB),
310                        "i" (SPRN_PMC2),  "r" (0) );
311         }
312
313         if ( !strncmp( buffer, "dc miss", 7) )
314         {
315                 /* setup mmcr0 and clear the correct pmc */
316                asm volatile(
317                        "mfspr %0,%1\n\t"     /* get current mccr0 */
318                        "rlwinm %0,%0,0,0,31-6\n\t"  /* clear bits [26-31] */
319                        "ori   %0,%0,%2 \n\t" /* or in mmcr0 settings */
320                        "mtspr %1,%0 \n\t"    /* set new mccr0 */
321                        "mtspr %3,%4 \n\t"    /* reset the pmc */
322                        : "=r" (tmp)
323                        : "i" (SPRN_MMCR0), "i" (MMCR0_PMC2_DCACHEMISS),
324                        "i" (SPRN_PMC2),  "r" (0) );
325         }
326
327         return count;
328 #else /* CONFIG_PPC_STD_MMU */
329         return 0;
330 #endif /* CONFIG_PPC_STD_MMU */
331 }
332
333
334 static long long
335 ppc_htab_lseek(struct file * file, loff_t offset, int orig)
336 {
337     long long ret = -EINVAL;
338
339     lock_kernel();
340     switch (orig) {
341     case 0:
342         file->f_pos = offset;
343         ret = file->f_pos;
344         break;
345     case 1:
346         file->f_pos += offset;
347         ret = file->f_pos;
348     }
349     unlock_kernel();
350     return ret;
351 }
352
353 int proc_dol2crvec(ctl_table *table, int write, struct file *filp,
354                   void __user *buffer_arg, size_t *lenp)
355 {
356         int vleft, first=1, len, left, val;
357         char __user *buffer = (char __user *) buffer_arg;
358         #define TMPBUFLEN 256
359         char buf[TMPBUFLEN], *p;
360         static const char *sizestrings[4] = {
361                 "2MB", "256KB", "512KB", "1MB"
362         };
363         static const char *clockstrings[8] = {
364                 "clock disabled", "+1 clock", "+1.5 clock", "reserved(3)",
365                 "+2 clock", "+2.5 clock", "+3 clock", "reserved(7)"
366         };
367         static const char *typestrings[4] = {
368                 "flow-through burst SRAM", "reserved SRAM",
369                 "pipelined burst SRAM", "pipelined late-write SRAM"
370         };
371         static const char *holdstrings[4] = {
372                 "0.5", "1.0", "(reserved2)", "(reserved3)"
373         };
374
375         if (!(cur_cpu_spec[0]->cpu_features & CPU_FTR_L2CR))
376                 return -EFAULT;
377
378         if ( /*!table->maxlen ||*/ (filp->f_pos && !write)) {
379                 *lenp = 0;
380                 return 0;
381         }
382
383         vleft = table->maxlen / sizeof(int);
384         left = *lenp;
385
386         for (; left /*&& vleft--*/; first=0) {
387                 if (write) {
388                         while (left) {
389                                 char c;
390                                 if(get_user(c, buffer))
391                                         return -EFAULT;
392                                 if (!isspace(c))
393                                         break;
394                                 left--;
395                                 buffer++;
396                         }
397                         if (!left)
398                                 break;
399                         len = left;
400                         if (len > TMPBUFLEN-1)
401                                 len = TMPBUFLEN-1;
402                         if(copy_from_user(buf, buffer, len))
403                                 return -EFAULT;
404                         buf[len] = 0;
405                         p = buf;
406                         if (*p < '0' || *p > '9')
407                                 break;
408                         val = simple_strtoul(p, &p, 0);
409                         len = p-buf;
410                         if ((len < left) && *p && !isspace(*p))
411                                 break;
412                         buffer += len;
413                         left -= len;
414                         _set_L2CR(val);
415                 } else {
416                         p = buf;
417                         if (!first)
418                                 *p++ = '\t';
419                         val = _get_L2CR();
420                         p += sprintf(p, "0x%08x: ", val);
421                         p += sprintf(p, " %s", (val >> 31) & 1 ? "enabled" :
422                                         "disabled");
423                         p += sprintf(p, ", %sparity", (val>>30)&1 ? "" : "no ");
424                         p += sprintf(p, ", %s", sizestrings[(val >> 28) & 3]);
425                         p += sprintf(p, ", %s", clockstrings[(val >> 25) & 7]);
426                         p += sprintf(p, ", %s", typestrings[(val >> 23) & 2]);
427                         p += sprintf(p, "%s", (val>>22)&1 ? ", data only" : "");
428                         p += sprintf(p, "%s", (val>>20)&1 ? ", ZZ enabled": "");
429                         p += sprintf(p, ", %s", (val>>19)&1 ? "write-through" :
430                                         "copy-back");
431                         p += sprintf(p, "%s", (val>>18)&1 ? ", testing" : "");
432                         p += sprintf(p, ", %sns hold",holdstrings[(val>>16)&3]);
433                         p += sprintf(p, "%s", (val>>15)&1 ? ", DLL slow" : "");
434                         p += sprintf(p, "%s", (val>>14)&1 ? ", diff clock" :"");
435                         p += sprintf(p, "%s", (val>>13)&1 ? ", DLL bypass" :"");
436
437                         p += sprintf(p,"\n");
438
439                         len = strlen(buf);
440                         if (len > left)
441                                 len = left;
442                         if (copy_to_user(buffer, buf, len))
443                                 return -EFAULT;
444                         left -= len;
445                         buffer += len;
446                         break;
447                 }
448         }
449
450         if (!write && !first && left) {
451                 if(put_user('\n', (char *) buffer))
452                         return -EFAULT;
453                 left--, buffer++;
454         }
455         if (write) {
456                 p = (char *) buffer;
457                 while (left) {
458                         char c;
459                         if(get_user(c, p++))
460                                 return -EFAULT;
461                         if (!isspace(c))
462                                 break;
463                         left--;
464                 }
465         }
466         if (write && first)
467                 return -EINVAL;
468         *lenp -= left;
469         filp->f_pos += *lenp;
470         return 0;
471 }