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