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