ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ia64 / sn / kernel / sn2 / prominfo_proc.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1999,2001-2003 Silicon Graphics, Inc.  All Rights Reserved.
7  *
8  * Module to export the system's Firmware Interface Tables, including
9  * PROM revision numbers, in /proc
10  */
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/proc_fs.h>
15 #include <asm/system.h>
16 #include <asm/io.h>
17 #include <asm/sn/sn2/addrs.h>
18 #include <asm/sn/simulator.h>
19
20 /* to lookup nasids */
21 #include <asm/sn/sn_cpuid.h>
22
23 MODULE_DESCRIPTION("PROM version reporting for /proc");
24 MODULE_AUTHOR("Chad Talbott");
25 MODULE_LICENSE("GPL");
26
27 #undef DEBUG_PROMINFO
28
29 #define TRACE_PROMINFO
30
31 #if defined(DEBUG_PROMINFO)
32 #  define DPRINTK(x...) printk(KERN_DEBUG x)
33 #else
34 #  define DPRINTK(x...)
35 #endif
36
37 #if defined(TRACE_PROMINFO) && defined(DEBUG_PROMINFO)
38 #  if defined(__GNUC__)
39 #    define TRACE()     printk(KERN_DEBUG "%s:%d:%s\n", \
40                                __FILE__, __LINE__, __FUNCTION__)
41 #  else
42 #    define TRACE()     printk(KERN_DEBUG "%s:%d\n", __LINE__, __FILE__)
43 #  endif
44 #else
45 #  define TRACE()
46 #endif
47
48 /* Sub-regions determined by bits in Node Offset */
49 #define LB_PROM_SPACE           0x0000000700000000ul /* Local LB PROM */
50
51 #define FIT_SIGNATURE           0x2020205f5449465ful
52 /* Standard Intel FIT entry types */
53 #define FIT_ENTRY_FIT_HEADER    0x00    /* FIT header entry */
54 #define FIT_ENTRY_PAL_B         0x01    /* PAL_B entry */
55 /* Entries 0x02 through 0x0D reserved by Intel */
56 #define FIT_ENTRY_PAL_A_PROC    0x0E    /* Processor-specific PAL_A entry */
57 #define FIT_ENTRY_PAL_A         0x0F    /* PAL_A entry, same as... */
58 #define FIT_ENTRY_PAL_A_GEN     0x0F    /* ...Generic PAL_A entry */
59 #define FIT_ENTRY_UNUSED        0x7F    /* Unused (reserved by Intel?) */
60 /* OEM-defined entries range from 0x10 to 0x7E. */
61 #define FIT_ENTRY_SAL_A         0x10    /* SAL_A entry */
62 #define FIT_ENTRY_SAL_B         0x11    /* SAL_B entry */
63 #define FIT_ENTRY_SALRUNTIME    0x12    /* SAL runtime entry */
64 #define FIT_ENTRY_EFI           0x1F    /* EFI entry */
65 #define FIT_ENTRY_FPSWA         0x20    /* embedded fpswa entry */
66 #define FIT_ENTRY_VMLINUX       0x21    /* embedded vmlinux entry */
67
68 #define FIT_MAJOR_SHIFT (32 + 8)
69 #define FIT_MAJOR_MASK  ((1 << 8) - 1)
70 #define FIT_MINOR_SHIFT 32
71 #define FIT_MINOR_MASK  ((1 << 8) - 1)
72
73 #define FIT_MAJOR(q)    \
74         ((unsigned) ((q) >> FIT_MAJOR_SHIFT) & FIT_MAJOR_MASK)
75 #define FIT_MINOR(q)    \
76         ((unsigned) ((q) >> FIT_MINOR_SHIFT) & FIT_MINOR_MASK)
77
78 #define FIT_TYPE_SHIFT  (32 + 16)
79 #define FIT_TYPE_MASK   ((1 << 7) - 1)
80
81 #define FIT_TYPE(q)     \
82         ((unsigned) ((q) >> FIT_TYPE_SHIFT) & FIT_TYPE_MASK)
83
84 #define FIT_ENTRY(type, maj, min, size)                                 \
85         ((((unsigned long)(maj) & FIT_MAJOR_MASK) << FIT_MAJOR_SHIFT) | \
86          (((unsigned long)(min) & FIT_MINOR_MASK) << FIT_MINOR_SHIFT) | \
87          (((unsigned long)(type) & FIT_TYPE_MASK) << FIT_TYPE_SHIFT) |  \
88          (size))
89
90 struct fit_type_map_t {
91         unsigned char   type;
92         const char      *name;
93 };
94
95 static const struct fit_type_map_t fit_entry_types[] = {
96         { FIT_ENTRY_FIT_HEADER, "FIT Header" },
97         { FIT_ENTRY_PAL_A_GEN,  "Generic PAL_A" },
98         { FIT_ENTRY_PAL_A_PROC, "Processor-specific PAL_A" },
99         { FIT_ENTRY_PAL_A,      "PAL_A" },
100         { FIT_ENTRY_PAL_B,      "PAL_B" },
101         { FIT_ENTRY_SAL_A,      "SAL_A" },
102         { FIT_ENTRY_SAL_B,      "SAL_B" },
103         { FIT_ENTRY_SALRUNTIME, "SAL runtime" },
104         { FIT_ENTRY_EFI,        "EFI" },
105         { FIT_ENTRY_VMLINUX,    "Embedded Linux" },
106         { FIT_ENTRY_FPSWA,      "Embedded FPSWA" },
107         { FIT_ENTRY_UNUSED,     "Unused" },
108         { 0xff,                 "Error" },
109 };
110
111 static const char *
112 fit_type_name(unsigned char type)
113 {
114         struct fit_type_map_t const*mapp;
115
116         for (mapp = fit_entry_types; mapp->type != 0xff; mapp++)
117                 if (type == mapp->type)
118                         return mapp->name;
119
120         if ((type > FIT_ENTRY_PAL_A) && (type < FIT_ENTRY_UNUSED))
121                 return "OEM type";
122         if ((type > FIT_ENTRY_PAL_B) && (type < FIT_ENTRY_PAL_A))
123                 return "Reserved";
124
125         return "Unknown type";
126 }
127
128 /* These two routines read the FIT table directly from the FLASH PROM
129  * on a specific node.  The PROM can only be accessed using aligned 64
130  * bit reads, so we do that and then shift and mask the result to get
131  * at each field.
132  */
133 static int
134 dump_fit_entry(char *page, unsigned long *fentry)
135 {
136         unsigned long q1, q2;
137         unsigned type;
138
139         TRACE();
140
141         q1 = readq(fentry);
142         q2 = readq(fentry + 1);
143         type = FIT_TYPE(q2);
144         return sprintf(page, "%02x %-25s %x.%02x %016lx %u\n",
145                        type,
146                        fit_type_name(type),
147                        FIT_MAJOR(q2), FIT_MINOR(q2),
148                        q1,
149                        /* mult by sixteen to get size in bytes */
150                        (unsigned)q2 * 16);
151 }
152
153 /* We assume that the fit table will be small enough that we can print
154  * the whole thing into one page.  (This is true for our default 16kB
155  * pages -- each entry is about 60 chars wide when printed.)  I read
156  * somewhere that the maximum size of the FIT is 128 entries, so we're
157  * OK except for 4kB pages (and no one is going to do that on SN
158  * anyway).
159  */
160 static int
161 dump_fit(char *page, unsigned long *fit)
162 {
163         unsigned long qw;
164         int nentries;
165         int fentry;
166         char *p;
167
168         TRACE();
169
170         DPRINTK("dumping fit from %p\n", (void *)fit);
171
172         qw = readq(fit);
173         DPRINTK("FIT signature: %016lx (%.8s)\n", qw, (char *)&qw);
174         if (qw != FIT_SIGNATURE)
175                 printk(KERN_WARNING "Unrecognized FIT signature");
176
177         qw = readq(fit + 1);
178         nentries = (unsigned)qw;
179         DPRINTK("number of fit entries: %u\n", nentries);
180         /* check that we won't overflow the page -- see comment above */
181         BUG_ON(nentries * 60 > PAGE_SIZE);
182
183         p = page;
184         for (fentry = 0; fentry < nentries; fentry++)
185                 /* each FIT entry is two 64 bit words */
186                 p += dump_fit_entry(p, fit + 2 * fentry);
187
188         return p - page;
189 }
190
191 static int
192 dump_version(char *page, unsigned long *fit)
193 {
194         int nentries;
195         int fentry;
196         unsigned long qw;
197
198         TRACE();
199
200         nentries = (unsigned)readq(fit + 1);
201         BUG_ON(nentries * 60 > PAGE_SIZE);
202
203         for (fentry = 0; fentry < nentries; fentry++) {
204                 qw = readq(fit + 2 * fentry + 1);
205                 if (FIT_TYPE(qw) == FIT_ENTRY_SAL_A)
206                         return sprintf(page, "%x.%02x\n",
207                                        FIT_MAJOR(qw), FIT_MINOR(qw));
208         }
209         return 0;
210 }
211
212 /* same as in proc_misc.c */
213 static int
214 proc_calc_metrics(char *page, char **start, off_t off, int count, int *eof,
215                   int len)
216 {
217         if (len <= off+count) *eof = 1;
218         *start = page + off;
219         len -= off;
220         if (len>count) len = count;
221         if (len<0) len = 0;
222         return len;
223 }
224
225 static int
226 read_version_entry(char *page, char **start, off_t off, int count, int *eof,
227                    void *data)
228 {
229         int len = 0;
230
231         MOD_INC_USE_COUNT;
232         /* data holds the pointer to this node's FIT */
233         len = dump_version(page, (unsigned long *)data);
234         len = proc_calc_metrics(page, start, off, count, eof, len);
235         MOD_DEC_USE_COUNT;
236         return len;
237 }
238
239 static int
240 read_fit_entry(char *page, char **start, off_t off, int count, int *eof,
241                void *data)
242 {
243         int len = 0;
244
245         MOD_INC_USE_COUNT;
246         /* data holds the pointer to this node's FIT */
247         len = dump_fit(page, (unsigned long *)data);
248         len = proc_calc_metrics(page, start, off, count, eof, len);
249         MOD_DEC_USE_COUNT;
250
251         return len;
252 }
253
254 /* this is a fake FIT that's used on the medusa simulator which
255  * doesn't usually run a complete PROM. 
256  */
257 #ifdef CONFIG_IA64_SGI_SN_SIM
258 static unsigned long fakefit[] = {
259         /* this is all we need to satisfy the code below */
260         FIT_SIGNATURE,
261         FIT_ENTRY(FIT_ENTRY_FIT_HEADER, 0x02, 0x60, 2),
262         /* dump something arbitrary for
263          * /proc/sgi_prominfo/nodeX/version */
264         0xbadbeef00fa3ef17ul,
265         FIT_ENTRY(FIT_ENTRY_SAL_A, 0, 0x99, 0x100)
266 };      
267 #endif
268
269 static unsigned long *
270 lookup_fit(int nasid)
271 {
272         unsigned long *fitp;
273         unsigned long fit_paddr;
274         unsigned long *fit_vaddr;
275
276 #ifdef CONFIG_IA64_SGI_SN_SIM
277         if (IS_RUNNING_ON_SIMULATOR())
278                 return fakefit;
279 #endif
280
281         fitp = (void *)GLOBAL_MMR_ADDR(nasid, LB_PROM_SPACE - 32);
282         DPRINTK("pointer to fit at %p\n", (void *)fitp);
283         fit_paddr = readq(fitp);
284         DPRINTK("fit pointer contains %lx\n", fit_paddr);
285         /* snag just the node-relative offset */
286         fit_paddr &= ~0ul >> (63-35);
287         /* the pointer to the FIT is relative to IA-64 compatibility
288          * space.  However, the PROM is mapped at a different offset
289          * in MMR space (both local and global)
290          */
291         fit_paddr += 0x700000000;
292         fit_vaddr = (void *)GLOBAL_MMR_ADDR(nasid, fit_paddr);
293         DPRINTK("fit at %p\n", (void *)fit_vaddr);
294         return fit_vaddr;
295 }
296
297 /* module entry points */
298 int __init prominfo_init(void);
299 void __exit prominfo_exit(void);
300
301 module_init(prominfo_init);
302 module_exit(prominfo_exit);
303
304 static struct proc_dir_entry **proc_entries;
305 static struct proc_dir_entry *sgi_prominfo_entry;
306
307 #define NODE_NAME_LEN 11
308
309 int __init
310 prominfo_init(void)
311 {
312         struct proc_dir_entry **entp;
313         cnodeid_t cnodeid;
314         nasid_t nasid;
315         char name[NODE_NAME_LEN];
316
317         if (!ia64_platform_is("sn2"))
318                 return 0;
319
320         TRACE();
321
322         DPRINTK("running on cpu %d\n", smp_processor_id());
323         DPRINTK("numnodes %d\n", numnodes);
324
325         proc_entries = kmalloc(numnodes * sizeof(struct proc_dir_entry *),
326                                GFP_KERNEL);
327
328         sgi_prominfo_entry = proc_mkdir("sgi_prominfo", NULL);
329
330         for (cnodeid = 0, entp = proc_entries;
331              cnodeid < numnodes;
332              cnodeid++, entp++) {
333                 sprintf(name, "node%d", cnodeid);
334                 *entp = proc_mkdir(name, sgi_prominfo_entry);
335                 nasid = cnodeid_to_nasid(cnodeid);
336                 create_proc_read_entry(
337                         "fit", 0, *entp, read_fit_entry,
338                         lookup_fit(nasid));
339                 create_proc_read_entry(
340                         "version", 0, *entp, read_version_entry,
341                         lookup_fit(nasid));
342         }
343
344         return 0;
345 }
346
347 void __exit
348 prominfo_exit(void)
349 {
350         struct proc_dir_entry **entp;
351         unsigned cnodeid;
352         char name[NODE_NAME_LEN];
353
354         TRACE();
355
356         for (cnodeid = 0, entp = proc_entries;
357              cnodeid < numnodes;
358              cnodeid++, entp++) {
359                 remove_proc_entry("fit", *entp);
360                 remove_proc_entry("version", *entp);
361                 sprintf(name, "node%d", cnodeid);
362                 remove_proc_entry(name, sgi_prominfo_entry);
363         }
364         remove_proc_entry("sgi_prominfo", NULL);
365         kfree(proc_entries);
366 }