ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / parisc / kernel / inventory.c
1 /*
2  * inventory.c
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  *
9  * Copyright (c) 1999 The Puffin Group (David Kennedy and Alex deVries)
10  * Copyright (c) 2001 Matthew Wilcox for Hewlett-Packard
11  *
12  * These are the routines to discover what hardware exists in this box.
13  * This task is complicated by there being 3 different ways of
14  * performing an inventory, depending largely on the age of the box.
15  * The recommended way to do this is to check to see whether the machine
16  * is a `Snake' first, then try System Map, then try PAT.  We try System
17  * Map before checking for a Snake -- this probably doesn't cause any
18  * problems, but...
19  */
20
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <asm/hardware.h>
27 #include <asm/io.h>
28 #include <asm/pdc.h>
29 #include <asm/processor.h>
30 #include <asm/page.h>
31 #include <asm/parisc-device.h>
32
33 /*
34 ** Debug options
35 ** DEBUG_PAT Dump details which PDC PAT provides about ranges/devices.
36 */
37 #undef DEBUG_PAT
38
39 int pdc_type = PDC_TYPE_ILLEGAL;
40
41 void __init setup_pdc(void)
42 {
43         long status;
44         unsigned int bus_id;
45         struct pdc_system_map_mod_info module_result;
46         struct pdc_module_path module_path;
47         struct pdc_model model;
48 #ifdef __LP64__
49         struct pdc_pat_cell_num cell_info;
50 #endif
51
52         /* Determine the pdc "type" used on this machine */
53
54         printk(KERN_INFO "Determining PDC firmware type: ");
55
56         status = pdc_system_map_find_mods(&module_result, &module_path, 0);
57         if (status == PDC_OK) {
58                 pdc_type = PDC_TYPE_SYSTEM_MAP;
59                 printk("System Map.\n");
60                 return;
61         }
62
63         /*
64          * If the machine doesn't support PDC_SYSTEM_MAP then either it
65          * is a pdc pat box, or it is an older box. All 64 bit capable
66          * machines are either pdc pat boxes or they support PDC_SYSTEM_MAP.
67          */
68
69         /*
70          * TODO: We should test for 64 bit capability and give a
71          * clearer message.
72          */
73
74 #ifdef __LP64__
75         status = pdc_pat_cell_get_number(&cell_info);
76         if (status == PDC_OK) {
77                 pdc_type = PDC_TYPE_PAT;
78                 printk("64 bit PAT.\n");
79                 return;
80         }
81 #endif
82
83         /* Check the CPU's bus ID.  There's probably a better test.  */
84
85         status = pdc_model_info(&model);
86
87         bus_id = (model.hversion >> (4 + 7)) & 0x1f;
88
89         switch (bus_id) {
90         case 0x4:               /* 720, 730, 750, 735, 755 */
91         case 0x6:               /* 705, 710 */
92         case 0x7:               /* 715, 725 */
93         case 0x8:               /* 745, 747, 742 */
94         case 0xA:               /* 712 and similiar */
95         case 0xC:               /* 715/64, at least */
96
97                 pdc_type = PDC_TYPE_SNAKE;
98                 printk("Snake.\n");
99                 return;
100
101         default:                /* Everything else */
102
103                 printk("Unsupported.\n");
104                 panic("If this is a 64-bit machine, please try a 64-bit kernel.\n");
105         }
106 }
107
108 #define PDC_PAGE_ADJ_SHIFT (PAGE_SHIFT - 12) /* pdc pages are always 4k */
109
110 static void __init
111 set_pmem_entry(physmem_range_t *pmem_ptr, unsigned long start,
112                unsigned long pages4k)
113 {
114         /* Rather than aligning and potentially throwing away
115          * memory, we'll assume that any ranges are already
116          * nicely aligned with any reasonable page size, and
117          * panic if they are not (it's more likely that the
118          * pdc info is bad in this case).
119          */
120
121         if (   ((start & (PAGE_SIZE - 1)) != 0)
122             || ((pages4k & ((1UL << PDC_PAGE_ADJ_SHIFT) - 1)) != 0) ) {
123
124                 panic("Memory range doesn't align with page size!\n");
125         }
126
127         pmem_ptr->start_pfn = (start >> PAGE_SHIFT);
128         pmem_ptr->pages = (pages4k >> PDC_PAGE_ADJ_SHIFT);
129 }
130
131 static void __init pagezero_memconfig(void)
132 {
133         unsigned long npages;
134
135         /* Use the 32 bit information from page zero to create a single
136          * entry in the pmem_ranges[] table.
137          *
138          * We currently don't support machines with contiguous memory
139          * >= 4 Gb, who report that memory using 64 bit only fields
140          * on page zero. It's not worth doing until it can be tested,
141          * and it is not clear we can support those machines for other
142          * reasons.
143          *
144          * If that support is done in the future, this is where it
145          * should be done.
146          */
147
148         npages = (PAGE_ALIGN(PAGE0->imm_max_mem) >> PAGE_SHIFT);
149         set_pmem_entry(pmem_ranges,0UL,npages);
150         npmem_ranges = 1;
151 }
152
153 #ifdef __LP64__
154
155 /* All of the PDC PAT specific code is 64-bit only */
156
157 /*
158 **  The module object is filled via PDC_PAT_CELL[Return Cell Module].
159 **  If a module is found, register module will get the IODC bytes via
160 **  pdc_iodc_read() using the PA view of conf_base_addr for the hpa parameter.
161 **
162 **  The IO view can be used by PDC_PAT_CELL[Return Cell Module]
163 **  only for SBAs and LBAs.  This view will cause an invalid
164 **  argument error for all other cell module types.
165 **
166 */
167
168 static int __init 
169 pat_query_module(ulong pcell_loc, ulong mod_index)
170 {
171         pdc_pat_cell_mod_maddr_block_t pa_pdc_cell;
172         unsigned long bytecnt;
173         unsigned long temp;     /* 64-bit scratch value */
174         long status;            /* PDC return value status */
175         struct parisc_device *dev;
176
177         /* return cell module (PA or Processor view) */
178         status = pdc_pat_cell_module(&bytecnt, pcell_loc, mod_index,
179                                      PA_VIEW, &pa_pdc_cell);
180
181         if (status != PDC_OK) {
182                 /* no more cell modules or error */
183                 return status;
184         }
185
186         temp = pa_pdc_cell.cba;
187         dev = alloc_pa_dev(PAT_GET_CBA(temp), &pa_pdc_cell.mod_path);
188         if (!dev) {
189                 return PDC_NE_MOD;
190         }
191
192         /* alloc_pa_dev sets dev->hpa */
193
194         /*
195         ** save parameters in the parisc_device
196         ** (The idea being the device driver will call pdc_pat_cell_module()
197         ** and store the results in its own data structure.)
198         */
199         dev->pcell_loc = pcell_loc;
200         dev->mod_index = mod_index;
201
202         /* save generic info returned from the call */
203         /* REVISIT: who is the consumer of this? not sure yet... */
204         dev->mod_info = pa_pdc_cell.mod_info;   /* pass to PAT_GET_ENTITY() */
205         dev->pmod_loc = pa_pdc_cell.mod_location;
206
207         register_parisc_device(dev);    /* advertise device */
208
209 #ifdef DEBUG_PAT
210         pdc_pat_cell_mod_maddr_block_t io_pdc_cell;
211         /* dump what we see so far... */
212         switch (PAT_GET_ENTITY(dev->mod_info)) {
213                 unsigned long i;
214
215         case PAT_ENTITY_PROC:
216                 printk(KERN_DEBUG "PAT_ENTITY_PROC: id_eid 0x%lx\n",
217                         pa_pdc_cell.mod[0]);
218                 break;
219
220         case PAT_ENTITY_MEM:
221                 printk(KERN_DEBUG 
222                         "PAT_ENTITY_MEM: amount 0x%lx min_gni_base 0x%lx min_gni_len 0x%lx\n",
223                         pa_pdc_cell.mod[0], pa_pdc_cell.mod[1], 
224                         pa_pdc_cell.mod[2]);
225                 break;
226         case PAT_ENTITY_CA:
227                 printk(KERN_DEBUG "PAT_ENTITY_CA: %ld\n", pcell_loc);
228                 break;
229
230         case PAT_ENTITY_PBC:
231                 printk(KERN_DEBUG "PAT_ENTITY_PBC: ");
232                 goto print_ranges;
233
234         case PAT_ENTITY_SBA:
235                 printk(KERN_DEBUG "PAT_ENTITY_SBA: ");
236                 goto print_ranges;
237
238         case PAT_ENTITY_LBA:
239                 printk(KERN_DEBUG "PAT_ENTITY_LBA: ");
240
241  print_ranges:
242                 pdc_pat_cell_module(&bytecnt, pcell_loc, mod_index,
243                                     IO_VIEW, &io_pdc_cell);
244                 printk(KERN_DEBUG "ranges %ld\n", pa_pdc_cell.mod[1]);
245                 for (i = 0; i < pa_pdc_cell.mod[1]; i++) {
246                         printk(KERN_DEBUG 
247                                 "  PA_VIEW %ld: 0x%016lx 0x%016lx 0x%016lx\n", 
248                                 i, pa_pdc_cell.mod[2 + i * 3],  /* type */
249                                 pa_pdc_cell.mod[3 + i * 3],     /* start */
250                                 pa_pdc_cell.mod[4 + i * 3]);    /* finish (ie end) */
251                         printk(KERN_DEBUG 
252                                 "  IO_VIEW %ld: 0x%016lx 0x%016lx 0x%016lx\n", 
253                                 i, io_pdc_cell.mod[2 + i * 3],  /* type */
254                                 io_pdc_cell.mod[3 + i * 3],     /* start */
255                                 io_pdc_cell.mod[4 + i * 3]);    /* finish (ie end) */
256                 }
257                 printk(KERN_DEBUG "\n");
258                 break;
259         }
260 #endif /* DEBUG_PAT */
261         return PDC_OK;
262 }
263
264
265 /* pat pdc can return information about a variety of different
266  * types of memory (e.g. firmware,i/o, etc) but we only care about
267  * the usable physical ram right now. Since the firmware specific
268  * information is allocated on the stack, we'll be generous, in
269  * case there is a lot of other information we don't care about.
270  */
271
272 #define PAT_MAX_RANGES (4 * MAX_PHYSMEM_RANGES)
273
274 static void __init pat_memconfig(void)
275 {
276         unsigned long actual_len;
277         struct pdc_pat_pd_addr_map_entry mem_table[PAT_MAX_RANGES+1];
278         struct pdc_pat_pd_addr_map_entry *mtbl_ptr;
279         physmem_range_t *pmem_ptr;
280         long status;
281         int entries;
282         unsigned long length;
283         int i;
284
285         length = (PAT_MAX_RANGES + 1) * sizeof(struct pdc_pat_pd_addr_map_entry);
286
287         status = pdc_pat_pd_get_addr_map(&actual_len, mem_table, length, 0L);
288
289         if ((status != PDC_OK)
290             || ((actual_len % sizeof(struct pdc_pat_pd_addr_map_entry)) != 0)) {
291
292                 /* The above pdc call shouldn't fail, but, just in
293                  * case, just use the PAGE0 info.
294                  */
295
296                 printk("\n\n\n");
297                 printk(KERN_WARNING "WARNING! Could not get full memory configuration. "
298                         "All memory may not be used!\n\n\n");
299                 pagezero_memconfig();
300                 return;
301         }
302
303         entries = actual_len / sizeof(struct pdc_pat_pd_addr_map_entry);
304
305         if (entries > PAT_MAX_RANGES) {
306                 printk(KERN_WARNING "This Machine has more memory ranges than we support!\n");
307                 printk(KERN_WARNING "Some memory may not be used!\n");
308         }
309
310         /* Copy information into the firmware independent pmem_ranges
311          * array, skipping types we don't care about. Notice we said
312          * "may" above. We'll use all the entries that were returned.
313          */
314
315         npmem_ranges = 0;
316         mtbl_ptr = mem_table;
317         pmem_ptr = pmem_ranges; /* Global firmware independent table */
318         for (i = 0; i < entries; i++,mtbl_ptr++) {
319                 if (   (mtbl_ptr->entry_type != PAT_MEMORY_DESCRIPTOR)
320                     || (mtbl_ptr->memory_type != PAT_MEMTYPE_MEMORY)
321                     || (mtbl_ptr->pages == 0)
322                     || (   (mtbl_ptr->memory_usage != PAT_MEMUSE_GENERAL)
323                         && (mtbl_ptr->memory_usage != PAT_MEMUSE_GI)
324                         && (mtbl_ptr->memory_usage != PAT_MEMUSE_GNI) ) ) {
325
326                         continue;
327                 }
328
329                 if (npmem_ranges == MAX_PHYSMEM_RANGES) {
330                         printk(KERN_WARNING "This Machine has more memory ranges than we support!\n");
331                         printk(KERN_WARNING "Some memory will not be used!\n");
332                         break;
333                 }
334
335                 set_pmem_entry(pmem_ptr++,mtbl_ptr->paddr,mtbl_ptr->pages);
336                 npmem_ranges++;
337         }
338 }
339
340 static int __init pat_inventory(void)
341 {
342         int status;
343         ulong mod_index = 0;
344         struct pdc_pat_cell_num cell_info;
345
346         /*
347         ** Note:  Prelude (and it's successors: Lclass, A400/500) only
348         **        implement PDC_PAT_CELL sub-options 0 and 2.
349         */
350         status = pdc_pat_cell_get_number(&cell_info);
351         if (status != PDC_OK) {
352                 return 0;
353         }
354
355 #ifdef DEBUG_PAT
356         printk(KERN_DEBUG "CELL_GET_NUMBER: 0x%lx 0x%lx\n", cell_info.cell_num, 
357                cell_info.cell_loc);
358 #endif
359
360         while (PDC_OK == pat_query_module(cell_info.cell_loc, mod_index)) {
361                 mod_index++;
362         }
363
364         return mod_index;
365 }
366
367 /* We only look for extended memory ranges on a 64 bit capable box */
368 static void __init sprockets_memconfig(void)
369 {
370         struct pdc_memory_table_raddr r_addr;
371         struct pdc_memory_table mem_table[MAX_PHYSMEM_RANGES];
372         struct pdc_memory_table *mtbl_ptr;
373         physmem_range_t *pmem_ptr;
374         long status;
375         int entries;
376         int i;
377
378         status = pdc_mem_mem_table(&r_addr,mem_table,
379                                 (unsigned long)MAX_PHYSMEM_RANGES);
380
381         if (status != PDC_OK) {
382
383                 /* The above pdc call only works on boxes with sprockets
384                  * firmware (newer B,C,J class). Other non PAT PDC machines
385                  * do support more than 3.75 Gb of memory, but we don't
386                  * support them yet.
387                  */
388
389                 pagezero_memconfig();
390                 return;
391         }
392
393         if (r_addr.entries_total > MAX_PHYSMEM_RANGES) {
394                 printk(KERN_WARNING "This Machine has more memory ranges than we support!\n");
395                 printk(KERN_WARNING "Some memory will not be used!\n");
396         }
397
398         entries = (int)r_addr.entries_returned;
399
400         npmem_ranges = 0;
401         mtbl_ptr = mem_table;
402         pmem_ptr = pmem_ranges; /* Global firmware independent table */
403         for (i = 0; i < entries; i++,mtbl_ptr++) {
404                 set_pmem_entry(pmem_ptr++,mtbl_ptr->paddr,mtbl_ptr->pages);
405                 npmem_ranges++;
406         }
407 }
408
409 #else   /* !__LP64__ */
410
411 #define pat_inventory() do { } while (0)
412 #define pat_memconfig() do { } while (0)
413 #define sprockets_memconfig() pagezero_memconfig()
414
415 #endif  /* !__LP64__ */
416
417
418 #ifndef CONFIG_PA20
419
420 /* Code to support Snake machines (7[2350], 7[235]5, 715/Scorpio) */
421
422 static struct parisc_device * __init
423 legacy_create_device(struct pdc_memory_map *r_addr,
424                 struct pdc_module_path *module_path)
425 {
426         struct parisc_device *dev;
427         int status = pdc_mem_map_hpa(r_addr, module_path);
428         if (status != PDC_OK)
429                 return NULL;
430
431         dev = alloc_pa_dev(r_addr->hpa, &module_path->path);
432         if (dev == NULL)
433                 return NULL;
434
435         register_parisc_device(dev);
436         return dev;
437 }
438
439 /**
440  * snake_inventory
441  *
442  * Before PDC_SYSTEM_MAP was invented, the PDC_MEM_MAP call was used.
443  * To use it, we initialise the mod_path.bc to 0xff and try all values of
444  * mod to get the HPA for the top-level devices.  Bus adapters may have
445  * sub-devices which are discovered by setting bc[5] to 0 and bc[4] to the
446  * module, then trying all possible functions.
447  */
448 static void __init snake_inventory(void)
449 {
450         int mod;
451         for (mod = 0; mod < 16; mod++) {
452                 struct parisc_device *dev;
453                 struct pdc_module_path module_path;
454                 struct pdc_memory_map r_addr;
455                 unsigned int func;
456
457                 memset(module_path.path.bc, 0xff, 6);
458                 module_path.path.mod = mod;
459                 dev = legacy_create_device(&r_addr, &module_path);
460                 if ((!dev) || (dev->id.hw_type != HPHW_BA))
461                         continue;
462
463                 memset(module_path.path.bc, 0xff, 4);
464                 module_path.path.bc[4] = mod;
465
466                 for (func = 0; func < 16; func++) {
467                         module_path.path.bc[5] = 0;
468                         module_path.path.mod = func;
469                         legacy_create_device(&r_addr, &module_path);
470                 }
471         }
472 }
473
474 #else /* CONFIG_PA20 */
475 #define snake_inventory() do { } while (0)
476 #endif  /* CONFIG_PA20 */
477
478 /* Common 32/64 bit based code goes here */
479
480 /**
481  * add_system_map_addresses - Add additional addresses to the parisc device.
482  * @dev: The parisc device.
483  * @num_addrs: Then number of addresses to add;
484  * @module_instance: The system_map module instance.
485  *
486  * This function adds any additional addresses reported by the system_map
487  * firmware to the parisc device.
488  */
489 static void __init
490 add_system_map_addresses(struct parisc_device *dev, int num_addrs, 
491                          int module_instance)
492 {
493         int i;
494         long status;
495         struct pdc_system_map_addr_info addr_result;
496
497         dev->addr = kmalloc(num_addrs * sizeof(unsigned long), GFP_KERNEL);
498         if(!dev->addr) {
499                 printk(KERN_ERR "%s %s(): memory allocation failure\n",
500                        __FILE__, __FUNCTION__);
501                 return;
502         }
503
504         for(i = 1; i <= num_addrs; ++i) {
505                 status = pdc_system_map_find_addrs(&addr_result, 
506                                                    module_instance, i);
507                 if(PDC_OK == status) {
508                         dev->addr[dev->num_addrs] = (unsigned long)addr_result.mod_addr;
509                         dev->num_addrs++;
510                 } else {
511                         printk(KERN_WARNING 
512                                "Bad PDC_FIND_ADDRESS status return (%ld) for index %d\n",
513                                status, i);
514                 }
515         }
516 }
517
518 /**
519  * do_system_map_inventory - Retrieve firmware devices via SYSTEM_MAP.
520  *
521  * This function attempts to retrieve and register all the devices firmware
522  * knows about via the SYSTEM_MAP PDC call.
523  */
524 static void __init system_map_inventory(void)
525 {
526         int i;
527         long status = PDC_OK;
528     
529         for (i = 0; status != PDC_BAD_PROC && status != PDC_NE_MOD; i++) {
530                 struct parisc_device *dev;
531                 struct pdc_system_map_mod_info module_result;
532                 struct pdc_module_path module_path;
533
534                 status = pdc_system_map_find_mods(&module_result,
535                                 &module_path, i);
536                 if (status != PDC_OK)
537                         continue;
538                 
539                 dev = alloc_pa_dev(module_result.mod_addr, &module_path.path);
540                 if (!dev)
541                         continue;
542                 
543                 register_parisc_device(dev);
544
545                 /* if available, get the additional addresses for a module */
546                 if (!module_result.add_addrs)
547                         continue;
548
549                 add_system_map_addresses(dev, module_result.add_addrs, i);
550         }
551
552         walk_central_bus();
553         return;
554 }
555
556 void __init do_memory_inventory(void)
557 {
558         switch (pdc_type) {
559
560         case PDC_TYPE_PAT:
561                 pat_memconfig();
562                 break;
563
564         case PDC_TYPE_SYSTEM_MAP:
565                 sprockets_memconfig();
566                 break;
567
568         case PDC_TYPE_SNAKE:
569                 pagezero_memconfig();
570                 return;
571
572         default:
573                 panic("Unknown PDC type!\n");
574         }
575
576         if (npmem_ranges == 0 || pmem_ranges[0].start_pfn != 0) {
577                 printk(KERN_WARNING "Bad memory configuration returned!\n");
578                 printk(KERN_WARNING "Some memory may not be used!\n");
579                 pagezero_memconfig();
580         }
581 }
582
583 void __init do_device_inventory(void)
584 {
585         extern void parisc_generic_device_register(void);
586
587         printk(KERN_INFO "Searching for devices...\n");
588
589         switch (pdc_type) {
590
591         case PDC_TYPE_PAT:
592                 pat_inventory();
593                 break;
594
595         case PDC_TYPE_SYSTEM_MAP:
596                 system_map_inventory();
597                 break;
598
599         case PDC_TYPE_SNAKE:
600                 snake_inventory();
601                 break;
602
603         default:
604                 panic("Unknown PDC type!\n");
605         }
606         parisc_generic_device_register();
607         printk(KERN_INFO "Found devices:\n");
608         print_parisc_devices();
609 }