ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / parisc / kernel / firmware.c
1 /*
2  * arch/parisc/kernel/firmware.c  - safe PDC access routines
3  *
4  *      PDC == Processor Dependent Code
5  *
6  * See http://www.parisc-linux.org/documentation/index.html
7  * for documentation describing the entry points and calling
8  * conventions defined below.
9  *
10  * Copyright 1999 SuSE GmbH Nuernberg (Philipp Rumpf, prumpf@tux.org)
11  * Copyright 1999 The Puffin Group, (Alex deVries, David Kennedy)
12  * Copyright 2003 Grant Grundler <grundler parisc-linux org>
13  *
14  *    This program is free software; you can redistribute it and/or modify
15  *    it under the terms of the GNU General Public License as published by
16  *    the Free Software Foundation; either version 2 of the License, or
17  *    (at your option) any later version.
18  *
19  */
20
21 /*      I think it would be in everyone's best interest to follow this
22  *      guidelines when writing PDC wrappers:
23  *
24  *       - the name of the pdc wrapper should match one of the macros
25  *         used for the first two arguments
26  *       - don't use caps for random parts of the name
27  *       - use the static PDC result buffers and "copyout" to structs
28  *         supplied by the caller to encapsulate alignment restrictions
29  *       - hold pdc_lock while in PDC or using static result buffers
30  *       - use __pa() to convert virtual (kernel) pointers to physical
31  *         ones.
32  *       - the name of the struct used for pdc return values should equal
33  *         one of the macros used for the first two arguments to the
34  *         corresponding PDC call
35  *       - keep the order of arguments
36  *       - don't be smart (setting trailing NUL bytes for strings, return
37  *         something useful even if the call failed) unless you are sure
38  *         it's not going to affect functionality or performance
39  *
40  *      Example:
41  *      int pdc_cache_info(struct pdc_cache_info *cache_info )
42  *      {
43  *              int retval;
44  *
45  *              spin_lock_irq(&pdc_lock);
46  *              retval = mem_pdc_call(PDC_CACHE,PDC_CACHE_INFO,__pa(cache_info),0);
47  *              convert_to_wide(pdc_result);
48  *              memcpy(cache_info, pdc_result, sizeof(*cache_info));
49  *              spin_unlock_irq(&pdc_lock);
50  *
51  *              return retval;
52  *      }
53  *                                      prumpf  991016  
54  */
55
56 #include <stdarg.h>
57
58 #include <linux/delay.h>
59 #include <linux/init.h>
60 #include <linux/kernel.h>
61 #include <linux/module.h>
62 #include <linux/string.h>
63 #include <linux/spinlock.h>
64
65 #include <asm/page.h>
66 #include <asm/pdc.h>
67 #include <asm/system.h>
68 #include <asm/processor.h>      /* for boot_cpu_data */
69
70 static spinlock_t pdc_lock = SPIN_LOCK_UNLOCKED;
71 static unsigned long pdc_result[32] __attribute__ ((aligned (8)));
72 static unsigned long pdc_result2[32] __attribute__ ((aligned (8)));
73
74 /* on all currently-supported platforms, IODC I/O calls are always
75  * 32-bit calls, and MEM_PDC calls are always the same width as the OS.
76  * This means Cxxx boxes can't run wide kernels right now. -PB
77  *
78  * CONFIG_PDC_NARROW has been added to allow 64-bit kernels to run on
79  * systems with 32-bit MEM_PDC calls. This will allow wide kernels to
80  * run on Cxxx boxes now. -RB
81  *
82  * Note that some PAT boxes may have 64-bit IODC I/O...
83  */
84
85 #ifdef __LP64__
86 long real64_call(unsigned long function, ...);
87 #endif
88 long real32_call(unsigned long function, ...);
89
90 #if defined(__LP64__) && ! defined(CONFIG_PDC_NARROW)
91 #define MEM_PDC (unsigned long)(PAGE0->mem_pdc_hi) << 32 | PAGE0->mem_pdc
92 #   define mem_pdc_call(args...) real64_call(MEM_PDC, args)
93 #else
94 #define MEM_PDC (unsigned long)PAGE0->mem_pdc
95 #   define mem_pdc_call(args...) real32_call(MEM_PDC, args)
96 #endif
97
98
99 /**
100  * f_extend - Convert PDC addresses to kernel addresses.
101  * @address: Address returned from PDC.
102  *
103  * This function is used to convert PDC addresses into kernel addresses
104  * when the PDC address size and kernel address size are different.
105  */
106 static unsigned long f_extend(unsigned long address)
107 {
108 #ifdef CONFIG_PDC_NARROW
109         if((address & 0xff000000) == 0xf0000000)
110                 return 0xf0f0f0f000000000 | (u32)address;
111
112         if((address & 0xf0000000) == 0xf0000000)
113                 return 0xffffffff00000000 | (u32)address;
114 #endif
115         return address;
116 }
117
118 /**
119  * convert_to_wide - Convert the return buffer addresses into kernel addresses.
120  * @address: The return buffer from PDC.
121  *
122  * This function is used to convert the return buffer addresses retrieved from PDC
123  * into kernel addresses when the PDC address size and kernel address size are
124  * different.
125  */
126 static void convert_to_wide(unsigned long *addr)
127 {
128 #ifdef CONFIG_PDC_NARROW
129         int i;
130         unsigned *p = (unsigned int *)addr;
131         for(i = 31; i >= 0; --i)
132                 addr[i] = p[i];
133 #endif
134 }
135
136 /**
137  * pdc_emergency_unlock - Unlock the linux pdc lock
138  *
139  * This call unlocks the linux pdc lock in case we need some PDC functions
140  * (like pdc_add_valid) during kernel stack dump.
141  */
142 void pdc_emergency_unlock(void)
143 {
144         spin_unlock(&pdc_lock);
145 }
146
147
148 /**
149  * pdc_add_valid - Verify address can be accessed without causing a HPMC.
150  * @address: Address to be verified.
151  *
152  * This PDC call attempts to read from the specified address and verifies
153  * if the address is valid.
154  * 
155  * The return value is PDC_OK (0) in case accessing this address is valid.
156  */
157 int pdc_add_valid(unsigned long address)
158 {
159         int retval;
160
161         spin_lock_irq(&pdc_lock);
162         retval = mem_pdc_call(PDC_ADD_VALID, PDC_ADD_VALID_VERIFY, address);
163         spin_unlock_irq(&pdc_lock);
164
165         return retval;
166 }
167 EXPORT_SYMBOL(pdc_add_valid);
168
169 /**
170  * pdc_chassis_info - Return chassis information.
171  * @result: The return buffer.
172  * @chassis_info: The memory buffer address.
173  * @len: The size of the memory buffer address.
174  *
175  * An HVERSION dependent call for returning the chassis information.
176  */
177 int __init pdc_chassis_info(struct pdc_chassis_info *chassis_info, void *led_info, unsigned long len)
178 {
179         int retval;
180
181         spin_lock_irq(&pdc_lock);
182         memcpy(&pdc_result, chassis_info, sizeof(*chassis_info));
183         memcpy(&pdc_result2, led_info, len);
184         retval = mem_pdc_call(PDC_CHASSIS, PDC_RETURN_CHASSIS_INFO,
185                               __pa(pdc_result), __pa(pdc_result2), len);
186         memcpy(chassis_info, pdc_result, sizeof(*chassis_info));
187         memcpy(led_info, pdc_result2, len);
188         spin_unlock_irq(&pdc_lock);
189
190         return retval;
191 }
192
193 /**
194  * pdc_pat_chassis_send_log - Sends a PDC PAT CHASSIS log message.
195  * @retval: -1 on error, 0 on success. Other value are PDC errors
196  * 
197  * Must be correctly formatted or expect system crash
198  */
199 #ifdef __LP64__
200 int pdc_pat_chassis_send_log(unsigned long state, unsigned long data)
201 {
202         if (!is_pdc_pat())
203                 return -1;
204
205         int retval = 0;
206
207         spin_lock_irq(&pdc_lock);
208         retval = mem_pdc_call(PDC_PAT_CHASSIS_LOG, PDC_PAT_CHASSIS_WRITE_LOG, __pa(&state), __pa(&data));
209         spin_unlock_irq(&pdc_lock);
210
211         return retval;
212 }
213 #endif
214
215 /**
216  * pdc_chassis_disp - Updates display
217  * @retval: -1 on error, 0 on success
218  *
219  * Works on old PDC only (E class, others?)
220  */
221 int pdc_chassis_disp(unsigned long disp)
222 {
223         int retval = 0;
224
225         spin_lock_irq(&pdc_lock);
226         retval = mem_pdc_call(PDC_CHASSIS, PDC_CHASSIS_DISP, disp);
227         spin_unlock_irq(&pdc_lock);
228
229         return retval;
230 }
231
232 /**
233  * pdc_coproc_cfg - To identify coprocessors attached to the processor.
234  * @pdc_coproc_info: Return buffer address.
235  *
236  * This PDC call returns the presence and status of all the coprocessors
237  * attached to the processor.
238  */
239 int __init pdc_coproc_cfg(struct pdc_coproc_cfg *pdc_coproc_info)
240 {
241         int retval;
242
243         spin_lock_irq(&pdc_lock);
244         retval = mem_pdc_call(PDC_COPROC, PDC_COPROC_CFG, __pa(pdc_result));
245         convert_to_wide(pdc_result);
246         pdc_coproc_info->ccr_functional = pdc_result[0];
247         pdc_coproc_info->ccr_present = pdc_result[1];
248         pdc_coproc_info->revision = pdc_result[17];
249         pdc_coproc_info->model = pdc_result[18];
250         spin_unlock_irq(&pdc_lock);
251
252         return retval;
253 }
254
255 /**
256  * pdc_iodc_read - Read data from the modules IODC.
257  * @actcnt: The actual number of bytes.
258  * @hpa: The HPA of the module for the iodc read.
259  * @index: The iodc entry point.
260  * @iodc_data: A buffer memory for the iodc options.
261  * @iodc_data_size: Size of the memory buffer.
262  *
263  * This PDC call reads from the IODC of the module specified by the hpa
264  * argument.
265  */
266 int pdc_iodc_read(unsigned long *actcnt, unsigned long hpa, unsigned int index,
267                   void *iodc_data, unsigned int iodc_data_size)
268 {
269         int retval;
270
271         spin_lock_irq(&pdc_lock);
272         retval = mem_pdc_call(PDC_IODC, PDC_IODC_READ, __pa(pdc_result), hpa, 
273                               index, __pa(pdc_result2), iodc_data_size);
274         convert_to_wide(pdc_result);
275         *actcnt = pdc_result[0];
276         memcpy(iodc_data, pdc_result2, iodc_data_size);
277         spin_unlock_irq(&pdc_lock);
278
279         return retval;
280 }
281 EXPORT_SYMBOL(pdc_iodc_read);
282
283 /**
284  * pdc_system_map_find_mods - Locate unarchitected modules.
285  * @pdc_mod_info: Return buffer address.
286  * @mod_path: pointer to dev path structure.
287  * @mod_index: fixed address module index.
288  *
289  * To locate and identify modules which reside at fixed I/O addresses, which
290  * do not self-identify via architected bus walks.
291  */
292 int pdc_system_map_find_mods(struct pdc_system_map_mod_info *pdc_mod_info,
293                              struct pdc_module_path *mod_path, long mod_index)
294 {
295         int retval;
296
297         spin_lock_irq(&pdc_lock);
298         retval = mem_pdc_call(PDC_SYSTEM_MAP, PDC_FIND_MODULE, __pa(pdc_result), 
299                               __pa(pdc_result2), mod_index);
300         convert_to_wide(pdc_result);
301         memcpy(pdc_mod_info, pdc_result, sizeof(*pdc_mod_info));
302         memcpy(mod_path, pdc_result2, sizeof(*mod_path));
303         spin_unlock_irq(&pdc_lock);
304
305         pdc_mod_info->mod_addr = f_extend(pdc_mod_info->mod_addr);
306         return retval;
307 }
308
309 /**
310  * pdc_system_map_find_addrs - Retrieve additional address ranges.
311  * @pdc_addr_info: Return buffer address.
312  * @mod_index: Fixed address module index.
313  * @addr_index: Address range index.
314  * 
315  * Retrieve additional information about subsequent address ranges for modules
316  * with multiple address ranges.  
317  */
318 int pdc_system_map_find_addrs(struct pdc_system_map_addr_info *pdc_addr_info, 
319                               long mod_index, long addr_index)
320 {
321         int retval;
322
323         spin_lock_irq(&pdc_lock);
324         retval = mem_pdc_call(PDC_SYSTEM_MAP, PDC_FIND_ADDRESS, __pa(pdc_result),
325                               mod_index, addr_index);
326         convert_to_wide(pdc_result);
327         memcpy(pdc_addr_info, pdc_result, sizeof(*pdc_addr_info));
328         spin_unlock_irq(&pdc_lock);
329
330         pdc_addr_info->mod_addr = f_extend(pdc_addr_info->mod_addr);
331         return retval;
332 }
333
334 /**
335  * pdc_model_info - Return model information about the processor.
336  * @model: The return buffer.
337  *
338  * Returns the version numbers, identifiers, and capabilities from the processor module.
339  */
340 int pdc_model_info(struct pdc_model *model) 
341 {
342         int retval;
343
344         spin_lock_irq(&pdc_lock);
345         retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_INFO, __pa(pdc_result), 0);
346         convert_to_wide(pdc_result);
347         memcpy(model, pdc_result, sizeof(*model));
348         spin_unlock_irq(&pdc_lock);
349
350         return retval;
351 }
352
353 /**
354  * pdc_model_sysmodel - Get the system model name.
355  * @name: A char array of at least 81 characters.
356  *
357  * Get system model name from PDC ROM (e.g. 9000/715 or 9000/778/B160L)
358  */
359 int pdc_model_sysmodel(char *name)
360 {
361         int retval;
362
363         spin_lock_irq(&pdc_lock);
364         retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_SYSMODEL, __pa(pdc_result),
365                               OS_ID_HPUX, __pa(name));
366         convert_to_wide(pdc_result);
367
368         if (retval == PDC_OK) {
369                 name[pdc_result[0]] = '\0'; /* add trailing '\0' */
370         } else {
371                 name[0] = 0;
372         }
373         spin_unlock_irq(&pdc_lock);
374
375         return retval;
376 }
377
378 /**
379  * pdc_model_versions - Identify the version number of each processor.
380  * @cpu_id: The return buffer.
381  * @id: The id of the processor to check.
382  *
383  * Returns the version number for each processor component.
384  *
385  * This comment was here before, but I do not know what it means :( -RB
386  * id: 0 = cpu revision, 1 = boot-rom-version
387  */
388 int pdc_model_versions(unsigned long *versions, int id)
389 {
390         int retval;
391
392         spin_lock_irq(&pdc_lock);
393         retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_VERSIONS, __pa(pdc_result), id);
394         convert_to_wide(pdc_result);
395         *versions = pdc_result[0];
396         spin_unlock_irq(&pdc_lock);
397
398         return retval;
399 }
400
401 /**
402  * pdc_model_cpuid - Returns the CPU_ID.
403  * @cpu_id: The return buffer.
404  *
405  * Returns the CPU_ID value which uniquely identifies the cpu portion of
406  * the processor module.
407  */
408 int pdc_model_cpuid(unsigned long *cpu_id)
409 {
410         int retval;
411
412         spin_lock_irq(&pdc_lock);
413         pdc_result[0] = 0; /* preset zero (call may not be implemented!) */
414         retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_CPU_ID, __pa(pdc_result), 0);
415         convert_to_wide(pdc_result);
416         *cpu_id = pdc_result[0];
417         spin_unlock_irq(&pdc_lock);
418
419         return retval;
420 }
421
422 /**
423  * pdc_model_capabilities - Returns the platform capabilities.
424  * @capabilities: The return buffer.
425  *
426  * Returns information about platform support for 32- and/or 64-bit
427  * OSes, IO-PDIR coherency, and virtual aliasing.
428  */
429 int pdc_model_capabilities(unsigned long *capabilities)
430 {
431         int retval;
432
433         spin_lock_irq(&pdc_lock);
434         pdc_result[0] = 0; /* preset zero (call may not be implemented!) */
435         retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_CAPABILITIES, __pa(pdc_result), 0);
436         convert_to_wide(pdc_result);
437         *capabilities = pdc_result[0];
438         spin_unlock_irq(&pdc_lock);
439
440         return retval;
441 }
442
443 /**
444  * pdc_cache_info - Return cache and TLB information.
445  * @cache_info: The return buffer.
446  *
447  * Returns information about the processor's cache and TLB.
448  */
449 int pdc_cache_info(struct pdc_cache_info *cache_info)
450 {
451         int retval;
452
453         spin_lock_irq(&pdc_lock);
454         retval = mem_pdc_call(PDC_CACHE, PDC_CACHE_INFO, __pa(pdc_result), 0);
455         convert_to_wide(pdc_result);
456         memcpy(cache_info, pdc_result, sizeof(*cache_info));
457         spin_unlock_irq(&pdc_lock);
458
459         return retval;
460 }
461
462 #ifndef CONFIG_PA20
463 /**
464  * pdc_btlb_info - Return block TLB information.
465  * @btlb: The return buffer.
466  *
467  * Returns information about the hardware Block TLB.
468  */
469 int pdc_btlb_info(struct pdc_btlb_info *btlb) 
470 {
471         int retval;
472
473         spin_lock_irq(&pdc_lock);
474         retval = mem_pdc_call(PDC_BLOCK_TLB, PDC_BTLB_INFO, __pa(pdc_result), 0);
475         memcpy(btlb, pdc_result, sizeof(*btlb));
476         spin_unlock_irq(&pdc_lock);
477
478         if(retval < 0) {
479                 btlb->max_size = 0;
480         }
481         return retval;
482 }
483
484 /**
485  * pdc_mem_map_hpa - Find fixed module information.  
486  * @address: The return buffer
487  * @mod_path: pointer to dev path structure.
488  *
489  * This call was developed for S700 workstations to allow the kernel to find
490  * the I/O devices (Core I/O). In the future (Kittyhawk and beyond) this
491  * call will be replaced (on workstations) by the architected PDC_SYSTEM_MAP
492  * call.
493  *
494  * This call is supported by all existing S700 workstations (up to  Gecko).
495  */
496 int pdc_mem_map_hpa(struct pdc_memory_map *address,
497                 struct pdc_module_path *mod_path)
498 {
499         int retval;
500
501         spin_lock_irq(&pdc_lock);
502         memcpy(pdc_result2, mod_path, sizeof(*mod_path));
503         retval = mem_pdc_call(PDC_MEM_MAP, PDC_MEM_MAP_HPA, __pa(pdc_result),
504                                 __pa(pdc_result2));
505         memcpy(address, pdc_result, sizeof(*address));
506         spin_unlock_irq(&pdc_lock);
507
508         return retval;
509 }
510 #endif  /* !CONFIG_PA20 */
511
512 /**
513  * pdc_lan_station_id - Get the LAN address.
514  * @lan_addr: The return buffer.
515  * @hpa: The network device HPA.
516  *
517  * Get the LAN station address when it is not directly available from the LAN hardware.
518  */
519 int pdc_lan_station_id(char *lan_addr, unsigned long hpa)
520 {
521         int retval;
522
523         spin_lock_irq(&pdc_lock);
524         retval = mem_pdc_call(PDC_LAN_STATION_ID, PDC_LAN_STATION_ID_READ,
525                         __pa(pdc_result), hpa);
526         if (retval < 0) {
527                 /* FIXME: else read MAC from NVRAM */
528                 memset(lan_addr, 0, PDC_LAN_STATION_ID_SIZE);
529         } else {
530                 memcpy(lan_addr, pdc_result, PDC_LAN_STATION_ID_SIZE);
531         }
532         spin_unlock_irq(&pdc_lock);
533
534         return retval;
535 }
536 EXPORT_SYMBOL(pdc_lan_station_id);
537
538
539 /**
540  * pdc_get_initiator - Get the SCSI Interface Card params (SCSI ID, SDTR, SE or LVD)
541  * @hwpath: fully bc.mod style path to the device.
542  * @scsi_id: what someone told firmware the ID should be.
543  * @period: time in cycles 
544  * @width: 8 or 16-bit wide bus
545  * @mode: 0,1,2 -> SE,HVD,LVD signalling mode
546  *
547  * Get the SCSI operational parameters from PDC.
548  * Needed since HPUX never used BIOS or symbios card NVRAM.
549  * Most ncr/sym cards won't have an entry and just use whatever
550  * capabilities of the card are (eg Ultra, LVD). But there are
551  * several cases where it's useful:
552  *    o set SCSI id for Multi-initiator clusters,
553  *    o cable too long (ie SE scsi 10Mhz won't support 6m length),
554  *    o bus width exported is less than what the interface chip supports.
555  */
556 int pdc_get_initiator(struct hardware_path *hwpath, unsigned char *scsi_id,
557                 unsigned long *period, char *width, char *mode)
558 {
559         int retval;
560
561         spin_lock_irq(&pdc_lock);
562
563 /* BCJ-XXXX series boxes. E.G. "9000/785/C3000" */
564 #define IS_SPROCKETS() (strlen(boot_cpu_data.pdc.sys_model_name) == 14 && \
565         strncmp(boot_cpu_data.pdc.sys_model_name, "9000/785", 8) == 0)
566
567         retval = mem_pdc_call(PDC_INITIATOR, PDC_GET_INITIATOR, 
568                               __pa(pdc_result), __pa(hwpath));
569
570         if (retval < PDC_OK)
571                 goto fail;
572
573         *scsi_id = (unsigned char) pdc_result[0];
574
575         /* convert Bus speed in Mhz to period (in 1/10 ns) */
576         switch (pdc_result[1]) {
577                 /*
578                  * case  0:   driver determines rate
579                  * case -1:   Settings are uninitialized.
580                  */
581                 case  5:  *period = 2000; break;
582                 case 10:  *period = 1000; break;
583                 case 20:  *period = 500; break;
584                 case 40:  *period = 250; break;
585                 default: /* Do nothing */ break;
586         }
587
588         /* 
589          * pdc_result[2]        PDC suggested SCSI id
590          * pdc_result[3]        PDC suggested SCSI rate
591          */
592
593         if (IS_SPROCKETS()) {
594                 /* 0 == 8-bit, 1 == 16-bit */
595                 *width = (char) pdc_result[4];
596
597                 /* ...in case someone needs it in the future.
598                  * sym53c8xx.c comments say it can't autodetect
599                  * for 825/825A/875 chips.
600                  *      0 == SE, 1 == HVD, 2 == LVD
601                  */
602                 *mode = (char) pdc_result[5]; 
603         }
604
605  fail:
606         spin_unlock_irq(&pdc_lock);
607         return (retval >= PDC_OK);
608 }
609 EXPORT_SYMBOL(pdc_get_initiator);
610
611
612 /**
613  * pdc_pci_irt_size - Get the number of entries in the interrupt routing table.
614  * @num_entries: The return value.
615  * @hpa: The HPA for the device.
616  *
617  * This PDC function returns the number of entries in the specified cell's
618  * interrupt table.
619  * Similar to PDC_PAT stuff - but added for Forte/Allegro boxes
620  */ 
621 int pdc_pci_irt_size(unsigned long *num_entries, unsigned long hpa)
622 {
623         int retval;
624
625         spin_lock_irq(&pdc_lock);
626         retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_GET_INT_TBL_SIZE, 
627                               __pa(pdc_result), hpa);
628         convert_to_wide(pdc_result);
629         *num_entries = pdc_result[0];
630         spin_unlock_irq(&pdc_lock);
631
632         return retval;
633 }
634
635 /** 
636  * pdc_pci_irt - Get the PCI interrupt routing table.
637  * @num_entries: The number of entries in the table.
638  * @hpa: The Hard Physical Address of the device.
639  * @tbl: 
640  *
641  * Get the PCI interrupt routing table for the device at the given HPA.
642  * Similar to PDC_PAT stuff - but added for Forte/Allegro boxes
643  */
644 int pdc_pci_irt(unsigned long num_entries, unsigned long hpa, void *tbl)
645 {
646         int retval;
647
648         spin_lock_irq(&pdc_lock);
649         pdc_result[0] = num_entries;
650         retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_GET_INT_TBL, 
651                               __pa(pdc_result), hpa, __pa(tbl));
652         spin_unlock_irq(&pdc_lock);
653
654         return retval;
655 }
656
657
658 #if 0   /* UNTEST CODE - left here in case someone needs it */
659
660 /** 
661  * pdc_pci_config_read - read PCI config space.
662  * @hpa         token from PDC to indicate which PCI device
663  * @pci_addr    configuration space address to read from
664  *
665  * Read PCI Configuration space *before* linux PCI subsystem is running.
666  */
667 unsigned int pdc_pci_config_read(void *hpa, unsigned long cfg_addr)
668 {
669         int retval;
670         spin_lock_irq(&pdc_lock);
671         pdc_result[0] = 0;
672         pdc_result[1] = 0;
673         retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_READ_CONFIG, 
674                               __pa(pdc_result), hpa, cfg_addr&~3UL, 4UL);
675         spin_unlock_irq(&pdc_lock);
676         return retval ? ~0 : (unsigned int) pdc_result[0];
677 }
678
679
680 /** 
681  * pdc_pci_config_write - read PCI config space.
682  * @hpa         token from PDC to indicate which PCI device
683  * @pci_addr    configuration space address to write
684  * @val         value we want in the 32-bit register
685  *
686  * Write PCI Configuration space *before* linux PCI subsystem is running.
687  */
688 void pdc_pci_config_write(void *hpa, unsigned long cfg_addr, unsigned int val)
689 {
690         int retval;
691         spin_lock_irq(&pdc_lock);
692         pdc_result[0] = 0;
693         retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_WRITE_CONFIG, 
694                               __pa(pdc_result), hpa,
695                               cfg_addr&~3UL, 4UL, (unsigned long) val);
696         spin_unlock_irq(&pdc_lock);
697         return retval;
698 }
699 #endif /* UNTESTED CODE */
700
701 /**
702  * pdc_tod_read - Read the Time-Of-Day clock.
703  * @tod: The return buffer:
704  *
705  * Read the Time-Of-Day clock
706  */
707 int pdc_tod_read(struct pdc_tod *tod)
708 {
709         int retval;
710
711         spin_lock_irq(&pdc_lock);
712         retval = mem_pdc_call(PDC_TOD, PDC_TOD_READ, __pa(pdc_result), 0);
713         convert_to_wide(pdc_result);
714         memcpy(tod, pdc_result, sizeof(*tod));
715         spin_unlock_irq(&pdc_lock);
716
717         return retval;
718 }
719 EXPORT_SYMBOL(pdc_tod_read);
720
721 /**
722  * pdc_tod_set - Set the Time-Of-Day clock.
723  * @sec: The number of seconds since epoch.
724  * @usec: The number of micro seconds.
725  *
726  * Set the Time-Of-Day clock.
727  */ 
728 int pdc_tod_set(unsigned long sec, unsigned long usec)
729 {
730         int retval;
731
732         spin_lock_irq(&pdc_lock);
733         retval = mem_pdc_call(PDC_TOD, PDC_TOD_WRITE, sec, usec);
734         spin_unlock_irq(&pdc_lock);
735
736         return retval;
737 }
738 EXPORT_SYMBOL(pdc_tod_set);
739
740 #ifdef __LP64__
741 int pdc_mem_mem_table(struct pdc_memory_table_raddr *r_addr,
742                 struct pdc_memory_table *tbl, unsigned long entries)
743 {
744         int retval;
745
746         spin_lock_irq(&pdc_lock);
747         retval = mem_pdc_call(PDC_MEM, PDC_MEM_TABLE, __pa(pdc_result), __pa(pdc_result2), entries);
748         convert_to_wide(pdc_result);
749         memcpy(r_addr, pdc_result, sizeof(*r_addr));
750         memcpy(tbl, pdc_result2, entries * sizeof(*tbl));
751         spin_unlock_irq(&pdc_lock);
752
753         return retval;
754 }
755 #endif /* __LP64__ */
756
757 /* FIXME: Is this pdc used?  I could not find type reference to ftc_bitmap
758  * so I guessed at unsigned long.  Someone who knows what this does, can fix
759  * it later. :)
760  */
761 int pdc_do_firm_test_reset(unsigned long ftc_bitmap)
762 {
763         int retval;
764
765         spin_lock_irq(&pdc_lock);
766         retval = mem_pdc_call(PDC_BROADCAST_RESET, PDC_DO_FIRM_TEST_RESET,
767                               PDC_FIRM_TEST_MAGIC, ftc_bitmap);
768         spin_unlock_irq(&pdc_lock);
769
770         return retval;
771 }
772
773 /*
774  * pdc_do_reset - Reset the system.
775  *
776  * Reset the system.
777  */
778 int pdc_do_reset()
779 {
780         int retval;
781
782         spin_lock_irq(&pdc_lock);
783         retval = mem_pdc_call(PDC_BROADCAST_RESET, PDC_DO_RESET);
784         spin_unlock_irq(&pdc_lock);
785
786         return retval;
787 }
788
789 /*
790  * pdc_soft_power_info - Enable soft power switch.
791  * @power_reg: address of soft power register
792  *
793  * Return the absolute address of the soft power switch register
794  */
795 int __init pdc_soft_power_info(unsigned long *power_reg)
796 {
797         int retval;
798
799         *power_reg = (unsigned long) (-1);
800         
801         spin_lock_irq(&pdc_lock);
802         retval = mem_pdc_call(PDC_SOFT_POWER, PDC_SOFT_POWER_INFO, __pa(pdc_result), 0);
803         if (retval == PDC_OK) {
804                 convert_to_wide(pdc_result);
805                 *power_reg = f_extend(pdc_result[0]);
806         }
807         spin_unlock_irq(&pdc_lock);
808
809         return retval;
810 }
811
812 /*
813  * pdc_soft_power_button - Control the soft power button behaviour
814  * @sw_control: 0 for hardware control, 1 for software control 
815  *
816  *
817  * This PDC function places the soft power button under software or
818  * hardware control.
819  * Under software control the OS may control to when to allow to shut 
820  * down the system. Under hardware control pressing the power button 
821  * powers off the system immediately.
822  */
823 int pdc_soft_power_button(int sw_control)
824 {
825         int retval;
826         spin_lock_irq(&pdc_lock);
827         retval = mem_pdc_call(PDC_SOFT_POWER, PDC_SOFT_POWER_ENABLE, __pa(pdc_result), sw_control);
828         spin_unlock_irq(&pdc_lock);
829         return retval;
830 }
831
832 /*
833  * pdc_io_reset - Hack to avoid overlapping range registers of Bridges devices.
834  * Primarily a problem on T600 (which parisc-linux doesn't support) but
835  * who knows what other platform firmware might do with this OS "hook".
836  */
837 void pdc_io_reset(void)
838 {
839         spin_lock_irq(&pdc_lock);  
840         mem_pdc_call(PDC_IO, PDC_IO_RESET, 0);
841         spin_unlock_irq(&pdc_lock);
842 }
843
844 /*
845  * pdc_io_reset_devices - Hack to Stop USB controller
846  *
847  * If PDC used the usb controller, the usb controller
848  * is still running and will crash the machines during iommu 
849  * setup, because of still running DMA. This PDC call
850  * stops the USB controller.
851  * Normally called after calling pdc_io_reset().
852  */
853 void pdc_io_reset_devices(void)
854 {
855         spin_lock_irq(&pdc_lock);  
856         mem_pdc_call(PDC_IO, PDC_IO_RESET_DEVICES, 0);
857         spin_unlock_irq(&pdc_lock);
858 }
859
860
861 /**
862  * pdc_iodc_putc - Console character print using IODC.
863  * @c: the character to output.
864  *
865  * Note that only these special chars are architected for console IODC io:
866  * BEL, BS, CR, and LF. Others are passed through.
867  * Since the HP console requires CR+LF to perform a 'newline', we translate
868  * "\n" to "\r\n".
869  */
870 void pdc_iodc_putc(unsigned char c)
871 {
872         /* XXX Should we spinlock posx usage */
873         static int posx;        /* for simple TAB-Simulation... */
874         static int __attribute__((aligned(8)))   iodc_retbuf[32];
875         static char __attribute__((aligned(64))) iodc_dbuf[4096];
876         unsigned int n;
877         unsigned int flags;
878
879         switch (c) {
880         case '\n':
881                 iodc_dbuf[0] = '\r';
882                 iodc_dbuf[1] = '\n';
883                 n = 2;
884                 posx = 0;
885                 break;
886         case '\t':
887                 pdc_iodc_putc(' ');
888                 while (posx & 7)        /* expand TAB */
889                         pdc_iodc_putc(' ');
890                 return;         /* return since IODC can't handle this */
891         case '\b':
892                 posx-=2;                /* BS */
893         default:
894                 iodc_dbuf[0] = c;
895                 n = 1;
896                 posx++;
897                 break;
898         }
899
900         spin_lock_irqsave(&pdc_lock, flags);
901         real32_call(PAGE0->mem_cons.iodc_io,
902                     (unsigned long)PAGE0->mem_cons.hpa, ENTRY_IO_COUT,
903                     PAGE0->mem_cons.spa, __pa(PAGE0->mem_cons.dp.layers),
904                     __pa(iodc_retbuf), 0, __pa(iodc_dbuf), n, 0);
905         spin_unlock_irqrestore(&pdc_lock, flags);
906 }
907
908 /**
909  * pdc_iodc_outc - Console character print using IODC (without conversions).
910  * @c: the character to output.
911  *
912  * Write the character directly to the IODC console.
913  */
914 void pdc_iodc_outc(unsigned char c)
915 {
916         unsigned int n, flags;
917
918         /* fill buffer with one caracter and print it */
919         static int __attribute__((aligned(8)))   iodc_retbuf[32];
920         static char __attribute__((aligned(64))) iodc_dbuf[4096];
921
922         n = 1;
923         iodc_dbuf[0] = c;
924
925         spin_lock_irqsave(&pdc_lock, flags);
926         real32_call(PAGE0->mem_cons.iodc_io,
927                     (unsigned long)PAGE0->mem_cons.hpa, ENTRY_IO_COUT,
928                     PAGE0->mem_cons.spa, __pa(PAGE0->mem_cons.dp.layers),
929                     __pa(iodc_retbuf), 0, __pa(iodc_dbuf), n, 0);
930         spin_unlock_irqrestore(&pdc_lock, flags);
931 }
932
933 /**
934  * pdc_iodc_getc - Read a character (non-blocking) from the PDC console.
935  *
936  * Read a character (non-blocking) from the PDC console, returns -1 if
937  * key is not present.
938  */
939 int pdc_iodc_getc(void)
940 {
941         unsigned int flags;
942         static int __attribute__((aligned(8)))   iodc_retbuf[32];
943         static char __attribute__((aligned(64))) iodc_dbuf[4096];
944         int ch;
945         int status;
946
947         /* Bail if no console input device. */
948         if (!PAGE0->mem_kbd.iodc_io)
949                 return 0;
950         
951         /* wait for a keyboard (rs232)-input */
952         spin_lock_irqsave(&pdc_lock, flags);
953         real32_call(PAGE0->mem_kbd.iodc_io,
954                     (unsigned long)PAGE0->mem_kbd.hpa, ENTRY_IO_CIN,
955                     PAGE0->mem_kbd.spa, __pa(PAGE0->mem_kbd.dp.layers), 
956                     __pa(iodc_retbuf), 0, __pa(iodc_dbuf), 1, 0);
957
958         ch = *iodc_dbuf;
959         status = *iodc_retbuf;
960         spin_unlock_irqrestore(&pdc_lock, flags);
961
962         if (status == 0)
963             return -1;
964         
965         return ch;
966 }
967
968 int pdc_sti_call(unsigned long func, unsigned long flags,
969                  unsigned long inptr, unsigned long outputr,
970                  unsigned long glob_cfg)
971 {
972         int retval;
973
974         spin_lock_irq(&pdc_lock);  
975         retval = real32_call(func, flags, inptr, outputr, glob_cfg);
976         spin_unlock_irq(&pdc_lock);
977
978         return retval;
979 }
980 EXPORT_SYMBOL(pdc_sti_call);
981
982 #ifdef __LP64__
983 /**
984  * pdc_pat_cell_get_number - Returns the cell number.
985  * @cell_info: The return buffer.
986  *
987  * This PDC call returns the cell number of the cell from which the call
988  * is made.
989  */
990 int pdc_pat_cell_get_number(struct pdc_pat_cell_num *cell_info)
991 {
992         int retval;
993
994         spin_lock_irq(&pdc_lock);
995         retval = mem_pdc_call(PDC_PAT_CELL, PDC_PAT_CELL_GET_NUMBER, __pa(pdc_result));
996         memcpy(cell_info, pdc_result, sizeof(*cell_info));
997         spin_unlock_irq(&pdc_lock);
998
999         return retval;
1000 }
1001
1002 /**
1003  * pdc_pat_cell_module - Retrieve the cell's module information.
1004  * @actcnt: The number of bytes written to mem_addr.
1005  * @ploc: The physical location.
1006  * @mod: The module index.
1007  * @view_type: The view of the address type.
1008  * @mem_addr: The return buffer.
1009  *
1010  * This PDC call returns information about each module attached to the cell
1011  * at the specified location.
1012  */
1013 int pdc_pat_cell_module(unsigned long *actcnt, unsigned long ploc, unsigned long mod,
1014                         unsigned long view_type, void *mem_addr)
1015 {
1016         int retval;
1017         static struct pdc_pat_cell_mod_maddr_block result __attribute__ ((aligned (8)));
1018
1019         spin_lock_irq(&pdc_lock);
1020         retval = mem_pdc_call(PDC_PAT_CELL, PDC_PAT_CELL_MODULE, __pa(pdc_result), 
1021                               ploc, mod, view_type, __pa(&result));
1022         if(!retval) {
1023                 *actcnt = pdc_result[0];
1024                 memcpy(mem_addr, &result, *actcnt);
1025         }
1026         spin_unlock_irq(&pdc_lock);
1027
1028         return retval;
1029 }
1030
1031 /**
1032  * pdc_pat_cpu_get_number - Retrieve the cpu number.
1033  * @cpu_info: The return buffer.
1034  * @hpa: The Hard Physical Address of the CPU.
1035  *
1036  * Retrieve the cpu number for the cpu at the specified HPA.
1037  */
1038 int pdc_pat_cpu_get_number(struct pdc_pat_cpu_num *cpu_info, void *hpa)
1039 {
1040         int retval;
1041
1042         spin_lock_irq(&pdc_lock);
1043         retval = mem_pdc_call(PDC_PAT_CPU, PDC_PAT_CPU_GET_NUMBER,
1044                               __pa(&pdc_result), hpa);
1045         memcpy(cpu_info, pdc_result, sizeof(*cpu_info));
1046         spin_unlock_irq(&pdc_lock);
1047
1048         return retval;
1049 }
1050
1051 /**
1052  * pdc_pat_get_irt_size - Retrieve the number of entries in the cell's interrupt table.
1053  * @num_entries: The return value.
1054  * @cell_num: The target cell.
1055  *
1056  * This PDC function returns the number of entries in the specified cell's
1057  * interrupt table.
1058  */
1059 int pdc_pat_get_irt_size(unsigned long *num_entries, unsigned long cell_num)
1060 {
1061         int retval;
1062
1063         spin_lock_irq(&pdc_lock);
1064         retval = mem_pdc_call(PDC_PAT_IO, PDC_PAT_IO_GET_PCI_ROUTING_TABLE_SIZE,
1065                               __pa(pdc_result), cell_num);
1066         *num_entries = pdc_result[0];
1067         spin_unlock_irq(&pdc_lock);
1068
1069         return retval;
1070 }
1071
1072 /**
1073  * pdc_pat_get_irt - Retrieve the cell's interrupt table.
1074  * @r_addr: The return buffer.
1075  * @cell_num: The target cell.
1076  *
1077  * This PDC function returns the actual interrupt table for the specified cell.
1078  */
1079 int pdc_pat_get_irt(void *r_addr, unsigned long cell_num)
1080 {
1081         int retval;
1082
1083         spin_lock_irq(&pdc_lock);
1084         retval = mem_pdc_call(PDC_PAT_IO, PDC_PAT_IO_GET_PCI_ROUTING_TABLE,
1085                               __pa(r_addr), cell_num);
1086         spin_unlock_irq(&pdc_lock);
1087
1088         return retval;
1089 }
1090
1091 /**
1092  * pdc_pat_pd_get_addr_map - Retrieve information about memory address ranges.
1093  * @actlen: The return buffer.
1094  * @mem_addr: Pointer to the memory buffer.
1095  * @count: The number of bytes to read from the buffer.
1096  * @offset: The offset with respect to the beginning of the buffer.
1097  *
1098  */
1099 int pdc_pat_pd_get_addr_map(unsigned long *actual_len, void *mem_addr, 
1100                             unsigned long count, unsigned long offset)
1101 {
1102         int retval;
1103
1104         spin_lock_irq(&pdc_lock);
1105         retval = mem_pdc_call(PDC_PAT_PD, PDC_PAT_PD_GET_ADDR_MAP, __pa(pdc_result), 
1106                               __pa(pdc_result2), count, offset);
1107         *actual_len = pdc_result[0];
1108         memcpy(mem_addr, pdc_result2, *actual_len);
1109         spin_unlock_irq(&pdc_lock);
1110
1111         return retval;
1112 }
1113 #endif /* __LP64__ */
1114
1115
1116 /***************** 32-bit real-mode calls ***********/
1117 /* The struct below is used
1118  * to overlay real_stack (real2.S), preparing a 32-bit call frame.
1119  * real32_call_asm() then uses this stack in narrow real mode
1120  */
1121
1122 struct narrow_stack {
1123         /* use int, not long which is 64 bits */
1124         unsigned int arg13;
1125         unsigned int arg12;
1126         unsigned int arg11;
1127         unsigned int arg10;
1128         unsigned int arg9;
1129         unsigned int arg8;
1130         unsigned int arg7;
1131         unsigned int arg6;
1132         unsigned int arg5;
1133         unsigned int arg4;
1134         unsigned int arg3;
1135         unsigned int arg2;
1136         unsigned int arg1;
1137         unsigned int arg0;
1138         unsigned int frame_marker[8];
1139         unsigned int sp;
1140         /* in reality, there's nearly 8k of stack after this */
1141 };
1142
1143 long real32_call(unsigned long fn, ...)
1144 {
1145         va_list args;
1146         extern struct narrow_stack real_stack;
1147         extern unsigned long real32_call_asm(unsigned int *,
1148                                              unsigned int *, 
1149                                              unsigned int);
1150         
1151         va_start(args, fn);
1152         real_stack.arg0 = va_arg(args, unsigned int);
1153         real_stack.arg1 = va_arg(args, unsigned int);
1154         real_stack.arg2 = va_arg(args, unsigned int);
1155         real_stack.arg3 = va_arg(args, unsigned int);
1156         real_stack.arg4 = va_arg(args, unsigned int);
1157         real_stack.arg5 = va_arg(args, unsigned int);
1158         real_stack.arg6 = va_arg(args, unsigned int);
1159         real_stack.arg7 = va_arg(args, unsigned int);
1160         real_stack.arg8 = va_arg(args, unsigned int);
1161         real_stack.arg9 = va_arg(args, unsigned int);
1162         real_stack.arg10 = va_arg(args, unsigned int);
1163         real_stack.arg11 = va_arg(args, unsigned int);
1164         real_stack.arg12 = va_arg(args, unsigned int);
1165         real_stack.arg13 = va_arg(args, unsigned int);
1166         va_end(args);
1167         
1168         return real32_call_asm(&real_stack.sp, &real_stack.arg0, fn);
1169 }
1170
1171 #ifdef __LP64__
1172 /***************** 64-bit real-mode calls ***********/
1173
1174 struct wide_stack {
1175         unsigned long arg0;
1176         unsigned long arg1;
1177         unsigned long arg2;
1178         unsigned long arg3;
1179         unsigned long arg4;
1180         unsigned long arg5;
1181         unsigned long arg6;
1182         unsigned long arg7;
1183         unsigned long arg8;
1184         unsigned long arg9;
1185         unsigned long arg10;
1186         unsigned long arg11;
1187         unsigned long arg12;
1188         unsigned long arg13;
1189         unsigned long frame_marker[2];  /* rp, previous sp */
1190         unsigned long sp;
1191         /* in reality, there's nearly 8k of stack after this */
1192 };
1193
1194 long real64_call(unsigned long fn, ...)
1195 {
1196         va_list args;
1197         extern struct wide_stack real_stack;
1198         extern unsigned long real64_call_asm(unsigned long *,
1199                                              unsigned long *, 
1200                                              unsigned long);
1201     
1202         va_start(args, fn);
1203         real_stack.arg0 = va_arg(args, unsigned long);
1204         real_stack.arg1 = va_arg(args, unsigned long);
1205         real_stack.arg2 = va_arg(args, unsigned long);
1206         real_stack.arg3 = va_arg(args, unsigned long);
1207         real_stack.arg4 = va_arg(args, unsigned long);
1208         real_stack.arg5 = va_arg(args, unsigned long);
1209         real_stack.arg6 = va_arg(args, unsigned long);
1210         real_stack.arg7 = va_arg(args, unsigned long);
1211         real_stack.arg8 = va_arg(args, unsigned long);
1212         real_stack.arg9 = va_arg(args, unsigned long);
1213         real_stack.arg10 = va_arg(args, unsigned long);
1214         real_stack.arg11 = va_arg(args, unsigned long);
1215         real_stack.arg12 = va_arg(args, unsigned long);
1216         real_stack.arg13 = va_arg(args, unsigned long);
1217         va_end(args);
1218         
1219         return real64_call_asm(&real_stack.sp, &real_stack.arg0, fn);
1220 }
1221
1222 #endif /* __LP64__ */
1223