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