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