patch-2_6_7-vs1_9_1_12
[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         /* data holds the pointer to this node's FIT */
232         len = dump_version(page, (unsigned long *)data);
233         len = proc_calc_metrics(page, start, off, count, eof, len);
234         return len;
235 }
236
237 static int
238 read_fit_entry(char *page, char **start, off_t off, int count, int *eof,
239                void *data)
240 {
241         int len = 0;
242
243         /* data holds the pointer to this node's FIT */
244         len = dump_fit(page, (unsigned long *)data);
245         len = proc_calc_metrics(page, start, off, count, eof, len);
246
247         return len;
248 }
249
250 /* this is a fake FIT that's used on the medusa simulator which
251  * doesn't usually run a complete PROM. 
252  */
253 #ifdef CONFIG_IA64_SGI_SN_SIM
254 static unsigned long fakefit[] = {
255         /* this is all we need to satisfy the code below */
256         FIT_SIGNATURE,
257         FIT_ENTRY(FIT_ENTRY_FIT_HEADER, 0x02, 0x60, 2),
258         /* dump something arbitrary for
259          * /proc/sgi_prominfo/nodeX/version */
260         0xbadbeef00fa3ef17ul,
261         FIT_ENTRY(FIT_ENTRY_SAL_A, 0, 0x99, 0x100)
262 };      
263 #endif
264
265 static unsigned long *
266 lookup_fit(int nasid)
267 {
268         unsigned long *fitp;
269         unsigned long fit_paddr;
270         unsigned long *fit_vaddr;
271
272 #ifdef CONFIG_IA64_SGI_SN_SIM
273         if (IS_RUNNING_ON_SIMULATOR())
274                 return fakefit;
275 #endif
276
277         fitp = (void *)GLOBAL_MMR_ADDR(nasid, LB_PROM_SPACE - 32);
278         DPRINTK("pointer to fit at %p\n", (void *)fitp);
279         fit_paddr = readq(fitp);
280         DPRINTK("fit pointer contains %lx\n", fit_paddr);
281         /* snag just the node-relative offset */
282         fit_paddr &= ~0ul >> (63-35);
283         /* the pointer to the FIT is relative to IA-64 compatibility
284          * space.  However, the PROM is mapped at a different offset
285          * in MMR space (both local and global)
286          */
287         fit_paddr += 0x700000000;
288         fit_vaddr = (void *)GLOBAL_MMR_ADDR(nasid, fit_paddr);
289         DPRINTK("fit at %p\n", (void *)fit_vaddr);
290         return fit_vaddr;
291 }
292
293 /* module entry points */
294 int __init prominfo_init(void);
295 void __exit prominfo_exit(void);
296
297 module_init(prominfo_init);
298 module_exit(prominfo_exit);
299
300 static struct proc_dir_entry **proc_entries;
301 static struct proc_dir_entry *sgi_prominfo_entry;
302
303 #define NODE_NAME_LEN 11
304
305 int __init
306 prominfo_init(void)
307 {
308         struct proc_dir_entry **entp;
309         struct proc_dir_entry *p;
310         cnodeid_t cnodeid;
311         nasid_t nasid;
312         char name[NODE_NAME_LEN];
313
314         if (!ia64_platform_is("sn2"))
315                 return 0;
316
317         TRACE();
318
319         DPRINTK("running on cpu %d\n", smp_processor_id());
320         DPRINTK("numnodes %d\n", numnodes);
321
322         proc_entries = kmalloc(numnodes * sizeof(struct proc_dir_entry *),
323                                GFP_KERNEL);
324
325         sgi_prominfo_entry = proc_mkdir("sgi_prominfo", NULL);
326
327         for (cnodeid = 0, entp = proc_entries;
328              cnodeid < numnodes;
329              cnodeid++, entp++) {
330                 sprintf(name, "node%d", cnodeid);
331                 *entp = proc_mkdir(name, sgi_prominfo_entry);
332                 nasid = cnodeid_to_nasid(cnodeid);
333                 p = create_proc_read_entry(
334                         "fit", 0, *entp, read_fit_entry,
335                         lookup_fit(nasid));
336                 if (p)
337                         p->owner = THIS_MODULE;
338                 p = create_proc_read_entry(
339                         "version", 0, *entp, read_version_entry,
340                         lookup_fit(nasid));
341                 if (p)
342                         p->owner = THIS_MODULE;
343         }
344
345         return 0;
346 }
347
348 void __exit
349 prominfo_exit(void)
350 {
351         struct proc_dir_entry **entp;
352         unsigned cnodeid;
353         char name[NODE_NAME_LEN];
354
355         TRACE();
356
357         for (cnodeid = 0, entp = proc_entries;
358              cnodeid < numnodes;
359              cnodeid++, entp++) {
360                 remove_proc_entry("fit", *entp);
361                 remove_proc_entry("version", *entp);
362                 sprintf(name, "node%d", cnodeid);
363                 remove_proc_entry(name, sgi_prominfo_entry);
364         }
365         remove_proc_entry("sgi_prominfo", NULL);
366         kfree(proc_entries);
367 }