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