1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * arch/parisc/kernel/firmware.c  - safe PDC access routines
4 *
5 *	PDC == Processor Dependent Code
6 *
7 * See PDC documentation at
8 * https://parisc.wiki.kernel.org/index.php/Technical_Documentation
9 * for documentation describing the entry points and calling
10 * conventions defined below.
11 *
12 * Copyright 1999 SuSE GmbH Nuernberg (Philipp Rumpf, prumpf@tux.org)
13 * Copyright 1999 The Puffin Group, (Alex deVries, David Kennedy)
14 * Copyright 2003 Grant Grundler <grundler parisc-linux org>
15 * Copyright 2003,2004 Ryan Bradetich <rbrad@parisc-linux.org>
16 * Copyright 2004,2006 Thibaut VARENE <varenet@parisc-linux.org>
17 */
18
19/*	I think it would be in everyone's best interest to follow this
20 *	guidelines when writing PDC wrappers:
21 *
22 *	 - the name of the pdc wrapper should match one of the macros
23 *	   used for the first two arguments
24 *	 - don't use caps for random parts of the name
25 *	 - use the static PDC result buffers and "copyout" to structs
26 *	   supplied by the caller to encapsulate alignment restrictions
27 *	 - hold pdc_lock while in PDC or using static result buffers
28 *	 - use __pa() to convert virtual (kernel) pointers to physical
29 *	   ones.
30 *	 - the name of the struct used for pdc return values should equal
31 *	   one of the macros used for the first two arguments to the
32 *	   corresponding PDC call
33 *	 - keep the order of arguments
34 *	 - don't be smart (setting trailing NUL bytes for strings, return
35 *	   something useful even if the call failed) unless you are sure
36 *	   it's not going to affect functionality or performance
37 *
38 *	Example:
39 *	int pdc_cache_info(struct pdc_cache_info *cache_info )
40 *	{
41 *		int retval;
42 *
43 *		spin_lock_irq(&pdc_lock);
44 *		retval = mem_pdc_call(PDC_CACHE,PDC_CACHE_INFO,__pa(cache_info),0);
45 *		convert_to_wide(pdc_result);
46 *		memcpy(cache_info, pdc_result, sizeof(*cache_info));
47 *		spin_unlock_irq(&pdc_lock);
48 *
49 *		return retval;
50 *	}
51 *					prumpf	991016
52 */
53
54#include <linux/stdarg.h>
55
56#include <linux/delay.h>
57#include <linux/init.h>
58#include <linux/kernel.h>
59#include <linux/module.h>
60#include <linux/string.h>
61#include <linux/spinlock.h>
62
63#include <asm/page.h>
64#include <asm/pdc.h>
65#include <asm/pdcpat.h>
66#include <asm/processor.h>	/* for boot_cpu_data */
67
68#if defined(BOOTLOADER)
69# undef  spin_lock_irqsave
70# define spin_lock_irqsave(a, b) { b = 1; }
71# undef  spin_unlock_irqrestore
72# define spin_unlock_irqrestore(a, b)
73#else
74static DEFINE_SPINLOCK(pdc_lock);
75#endif
76
77static unsigned long pdc_result[NUM_PDC_RESULT]  __aligned(8);
78static unsigned long pdc_result2[NUM_PDC_RESULT] __aligned(8);
79
80#ifdef CONFIG_64BIT
81#define WIDE_FIRMWARE		PDC_MODEL_OS64
82#define NARROW_FIRMWARE		PDC_MODEL_OS32
83
84/* Firmware needs to be initially set to narrow to determine the
85 * actual firmware width. */
86int parisc_narrow_firmware __ro_after_init = NARROW_FIRMWARE;
87#endif
88
89/* On most currently-supported platforms, IODC I/O calls are 32-bit calls
90 * and MEM_PDC calls are always the same width as the OS.
91 * Some PAT boxes may have 64-bit IODC I/O.
92 *
93 * Ryan Bradetich added the now obsolete CONFIG_PDC_NARROW to allow
94 * 64-bit kernels to run on systems with 32-bit MEM_PDC calls.
95 * This allowed wide kernels to run on Cxxx boxes.
96 * We now detect 32-bit-only PDC and dynamically switch to 32-bit mode
97 * when running a 64-bit kernel on such boxes (e.g. C200 or C360).
98 */
99
100#ifdef CONFIG_64BIT
101long real64_call(unsigned long function, ...);
102#endif
103long real32_call(unsigned long function, ...);
104
105#ifdef CONFIG_64BIT
106#   define MEM_PDC (unsigned long)(PAGE0->mem_pdc_hi) << 32 | PAGE0->mem_pdc
107#   define mem_pdc_call(args...) unlikely(parisc_narrow_firmware) ? real32_call(MEM_PDC, args) : real64_call(MEM_PDC, args)
108#else
109#   define MEM_PDC (unsigned long)PAGE0->mem_pdc
110#   define mem_pdc_call(args...) real32_call(MEM_PDC, args)
111#endif
112
113
114/**
115 * f_extend - Convert PDC addresses to kernel addresses.
116 * @address: Address returned from PDC.
117 *
118 * This function is used to convert PDC addresses into kernel addresses
119 * when the PDC address size and kernel address size are different.
120 */
121static unsigned long f_extend(unsigned long address)
122{
123#ifdef CONFIG_64BIT
124	if(unlikely(parisc_narrow_firmware)) {
125		if((address & 0xff000000) == 0xf0000000)
126			return (0xfffffff0UL << 32) | (u32)address;
127
128		if((address & 0xf0000000) == 0xf0000000)
129			return (0xffffffffUL << 32) | (u32)address;
130	}
131#endif
132	return address;
133}
134
135/**
136 * convert_to_wide - Convert the return buffer addresses into kernel addresses.
137 * @addr: The return buffer from PDC.
138 *
139 * This function is used to convert the return buffer addresses retrieved from PDC
140 * into kernel addresses when the PDC address size and kernel address size are
141 * different.
142 */
143static void convert_to_wide(unsigned long *addr)
144{
145#ifdef CONFIG_64BIT
146	int i;
147	unsigned int *p = (unsigned int *)addr;
148
149	if (unlikely(parisc_narrow_firmware)) {
150		for (i = (NUM_PDC_RESULT-1); i >= 0; --i)
151			addr[i] = p[i];
152	}
153#endif
154}
155
156#ifdef CONFIG_64BIT
157void set_firmware_width_unlocked(void)
158{
159	int ret;
160
161	ret = mem_pdc_call(PDC_MODEL, PDC_MODEL_CAPABILITIES,
162		__pa(pdc_result), 0);
163	if (ret < 0)
164		return;
165	convert_to_wide(pdc_result);
166	if (pdc_result[0] != NARROW_FIRMWARE)
167		parisc_narrow_firmware = 0;
168}
169
170/**
171 * set_firmware_width - Determine if the firmware is wide or narrow.
172 *
173 * This function must be called before any pdc_* function that uses the
174 * convert_to_wide function.
175 */
176void set_firmware_width(void)
177{
178	unsigned long flags;
179
180	/* already initialized? */
181	if (parisc_narrow_firmware != NARROW_FIRMWARE)
182		return;
183
184	spin_lock_irqsave(&pdc_lock, flags);
185	set_firmware_width_unlocked();
186	spin_unlock_irqrestore(&pdc_lock, flags);
187}
188#else
189void set_firmware_width_unlocked(void)
190{
191	return;
192}
193
194void set_firmware_width(void)
195{
196	return;
197}
198#endif /*CONFIG_64BIT*/
199
200
201#if !defined(BOOTLOADER)
202/**
203 * pdc_emergency_unlock - Unlock the linux pdc lock
204 *
205 * This call unlocks the linux pdc lock in case we need some PDC functions
206 * (like pdc_add_valid) during kernel stack dump.
207 */
208void pdc_emergency_unlock(void)
209{
210 	/* Spinlock DEBUG code freaks out if we unconditionally unlock */
211        if (spin_is_locked(&pdc_lock))
212		spin_unlock(&pdc_lock);
213}
214
215
216/**
217 * pdc_add_valid - Verify address can be accessed without causing a HPMC.
218 * @address: Address to be verified.
219 *
220 * This PDC call attempts to read from the specified address and verifies
221 * if the address is valid.
222 *
223 * The return value is PDC_OK (0) in case accessing this address is valid.
224 */
225int pdc_add_valid(unsigned long address)
226{
227        int retval;
228	unsigned long flags;
229
230        spin_lock_irqsave(&pdc_lock, flags);
231        retval = mem_pdc_call(PDC_ADD_VALID, PDC_ADD_VALID_VERIFY, address);
232        spin_unlock_irqrestore(&pdc_lock, flags);
233
234        return retval;
235}
236EXPORT_SYMBOL(pdc_add_valid);
237
238/**
239 * pdc_instr - Get instruction that invokes PDCE_CHECK in HPMC handler.
240 * @instr: Pointer to variable which will get instruction opcode.
241 *
242 * The return value is PDC_OK (0) in case call succeeded.
243 */
244int __init pdc_instr(unsigned int *instr)
245{
246	int retval;
247	unsigned long flags;
248
249	spin_lock_irqsave(&pdc_lock, flags);
250	retval = mem_pdc_call(PDC_INSTR, 0UL, __pa(pdc_result));
251	convert_to_wide(pdc_result);
252	*instr = pdc_result[0];
253	spin_unlock_irqrestore(&pdc_lock, flags);
254
255	return retval;
256}
257
258/**
259 * pdc_chassis_info - Return chassis information.
260 * @chassis_info: The memory buffer address.
261 * @led_info: The size of the memory buffer address.
262 * @len: The size of the memory buffer address.
263 *
264 * An HVERSION dependent call for returning the chassis information.
265 */
266int __init pdc_chassis_info(struct pdc_chassis_info *chassis_info, void *led_info, unsigned long len)
267{
268        int retval;
269	unsigned long flags;
270
271        spin_lock_irqsave(&pdc_lock, flags);
272        memcpy(&pdc_result, chassis_info, sizeof(*chassis_info));
273        memcpy(&pdc_result2, led_info, len);
274        retval = mem_pdc_call(PDC_CHASSIS, PDC_RETURN_CHASSIS_INFO,
275                              __pa(pdc_result), __pa(pdc_result2), len);
276        memcpy(chassis_info, pdc_result, sizeof(*chassis_info));
277        memcpy(led_info, pdc_result2, len);
278        spin_unlock_irqrestore(&pdc_lock, flags);
279
280        return retval;
281}
282
283/**
284 * pdc_pat_chassis_send_log - Sends a PDC PAT CHASSIS log message.
285 * @state: state of the machine
286 * @data: value for that state
287 *
288 * Must be correctly formatted or expect system crash
289 */
290#ifdef CONFIG_64BIT
291int pdc_pat_chassis_send_log(unsigned long state, unsigned long data)
292{
293	int retval = 0;
294	unsigned long flags;
295
296	if (!is_pdc_pat())
297		return -1;
298
299	spin_lock_irqsave(&pdc_lock, flags);
300	retval = mem_pdc_call(PDC_PAT_CHASSIS_LOG, PDC_PAT_CHASSIS_WRITE_LOG, __pa(&state), __pa(&data));
301	spin_unlock_irqrestore(&pdc_lock, flags);
302
303	return retval;
304}
305#endif
306
307/**
308 * pdc_chassis_disp - Updates chassis code
309 * @disp: value to show on display
310 */
311int pdc_chassis_disp(unsigned long disp)
312{
313	int retval = 0;
314	unsigned long flags;
315
316	spin_lock_irqsave(&pdc_lock, flags);
317	retval = mem_pdc_call(PDC_CHASSIS, PDC_CHASSIS_DISP, disp);
318	spin_unlock_irqrestore(&pdc_lock, flags);
319
320	return retval;
321}
322
323/**
324 * __pdc_cpu_rendezvous - Stop currently executing CPU and do not return.
325 */
326int __pdc_cpu_rendezvous(void)
327{
328	if (is_pdc_pat())
329		return mem_pdc_call(PDC_PAT_CPU, PDC_PAT_CPU_RENDEZVOUS);
330	else
331		return mem_pdc_call(PDC_PROC, 1, 0);
332}
333
334/**
335 * pdc_cpu_rendezvous_lock - Lock PDC while transitioning to rendezvous state
336 */
337void pdc_cpu_rendezvous_lock(void) __acquires(&pdc_lock)
338{
339	spin_lock(&pdc_lock);
340}
341
342/**
343 * pdc_cpu_rendezvous_unlock - Unlock PDC after reaching rendezvous state
344 */
345void pdc_cpu_rendezvous_unlock(void) __releases(&pdc_lock)
346{
347	spin_unlock(&pdc_lock);
348}
349
350/**
351 * pdc_pat_get_PDC_entrypoint - Get PDC entry point for current CPU
352 * @pdc_entry: pointer to where the PDC entry point should be stored
353 */
354int pdc_pat_get_PDC_entrypoint(unsigned long *pdc_entry)
355{
356	int retval = 0;
357	unsigned long flags;
358
359	if (!IS_ENABLED(CONFIG_SMP) || !is_pdc_pat()) {
360		*pdc_entry = MEM_PDC;
361		return 0;
362	}
363
364	spin_lock_irqsave(&pdc_lock, flags);
365	retval = mem_pdc_call(PDC_PAT_CPU, PDC_PAT_CPU_GET_PDC_ENTRYPOINT,
366			__pa(pdc_result));
367	*pdc_entry = pdc_result[0];
368	spin_unlock_irqrestore(&pdc_lock, flags);
369
370	return retval;
371}
372/**
373 * pdc_chassis_warn - Fetches chassis warnings
374 * @warn: The warning value to be shown
375 */
376int pdc_chassis_warn(unsigned long *warn)
377{
378	int retval = 0;
379	unsigned long flags;
380
381	spin_lock_irqsave(&pdc_lock, flags);
382	retval = mem_pdc_call(PDC_CHASSIS, PDC_CHASSIS_WARN, __pa(pdc_result));
383	*warn = pdc_result[0];
384	spin_unlock_irqrestore(&pdc_lock, flags);
385
386	return retval;
387}
388
389int pdc_coproc_cfg_unlocked(struct pdc_coproc_cfg *pdc_coproc_info)
390{
391	int ret;
392
393	ret = mem_pdc_call(PDC_COPROC, PDC_COPROC_CFG, __pa(pdc_result));
394	convert_to_wide(pdc_result);
395	pdc_coproc_info->ccr_functional = pdc_result[0];
396	pdc_coproc_info->ccr_present = pdc_result[1];
397	pdc_coproc_info->revision = pdc_result[17];
398	pdc_coproc_info->model = pdc_result[18];
399
400	return ret;
401}
402
403/**
404 * pdc_coproc_cfg - To identify coprocessors attached to the processor.
405 * @pdc_coproc_info: Return buffer address.
406 *
407 * This PDC call returns the presence and status of all the coprocessors
408 * attached to the processor.
409 */
410int pdc_coproc_cfg(struct pdc_coproc_cfg *pdc_coproc_info)
411{
412	int ret;
413	unsigned long flags;
414
415	spin_lock_irqsave(&pdc_lock, flags);
416	ret = pdc_coproc_cfg_unlocked(pdc_coproc_info);
417	spin_unlock_irqrestore(&pdc_lock, flags);
418
419	return ret;
420}
421
422/**
423 * pdc_iodc_read - Read data from the modules IODC.
424 * @actcnt: The actual number of bytes.
425 * @hpa: The HPA of the module for the iodc read.
426 * @index: The iodc entry point.
427 * @iodc_data: A buffer memory for the iodc options.
428 * @iodc_data_size: Size of the memory buffer.
429 *
430 * This PDC call reads from the IODC of the module specified by the hpa
431 * argument.
432 */
433int pdc_iodc_read(unsigned long *actcnt, unsigned long hpa, unsigned int index,
434		  void *iodc_data, unsigned int iodc_data_size)
435{
436	int retval;
437	unsigned long flags;
438
439	spin_lock_irqsave(&pdc_lock, flags);
440	retval = mem_pdc_call(PDC_IODC, PDC_IODC_READ, __pa(pdc_result), hpa,
441			      index, __pa(pdc_result2), iodc_data_size);
442	convert_to_wide(pdc_result);
443	*actcnt = pdc_result[0];
444	memcpy(iodc_data, pdc_result2, iodc_data_size);
445	spin_unlock_irqrestore(&pdc_lock, flags);
446
447	return retval;
448}
449EXPORT_SYMBOL(pdc_iodc_read);
450
451/**
452 * pdc_system_map_find_mods - Locate unarchitected modules.
453 * @pdc_mod_info: Return buffer address.
454 * @mod_path: pointer to dev path structure.
455 * @mod_index: fixed address module index.
456 *
457 * To locate and identify modules which reside at fixed I/O addresses, which
458 * do not self-identify via architected bus walks.
459 */
460int pdc_system_map_find_mods(struct pdc_system_map_mod_info *pdc_mod_info,
461			     struct pdc_module_path *mod_path, long mod_index)
462{
463	int retval;
464	unsigned long flags;
465
466	spin_lock_irqsave(&pdc_lock, flags);
467	retval = mem_pdc_call(PDC_SYSTEM_MAP, PDC_FIND_MODULE, __pa(pdc_result),
468			      __pa(pdc_result2), mod_index);
469	convert_to_wide(pdc_result);
470	memcpy(pdc_mod_info, pdc_result, sizeof(*pdc_mod_info));
471	memcpy(mod_path, pdc_result2, sizeof(*mod_path));
472	spin_unlock_irqrestore(&pdc_lock, flags);
473
474	pdc_mod_info->mod_addr = f_extend(pdc_mod_info->mod_addr);
475	return retval;
476}
477
478/**
479 * pdc_system_map_find_addrs - Retrieve additional address ranges.
480 * @pdc_addr_info: Return buffer address.
481 * @mod_index: Fixed address module index.
482 * @addr_index: Address range index.
483 *
484 * Retrieve additional information about subsequent address ranges for modules
485 * with multiple address ranges.
486 */
487int pdc_system_map_find_addrs(struct pdc_system_map_addr_info *pdc_addr_info,
488			      long mod_index, long addr_index)
489{
490	int retval;
491	unsigned long flags;
492
493	spin_lock_irqsave(&pdc_lock, flags);
494	retval = mem_pdc_call(PDC_SYSTEM_MAP, PDC_FIND_ADDRESS, __pa(pdc_result),
495			      mod_index, addr_index);
496	convert_to_wide(pdc_result);
497	memcpy(pdc_addr_info, pdc_result, sizeof(*pdc_addr_info));
498	spin_unlock_irqrestore(&pdc_lock, flags);
499
500	pdc_addr_info->mod_addr = f_extend(pdc_addr_info->mod_addr);
501	return retval;
502}
503
504/**
505 * pdc_model_info - Return model information about the processor.
506 * @model: The return buffer.
507 *
508 * Returns the version numbers, identifiers, and capabilities from the processor module.
509 */
510int pdc_model_info(struct pdc_model *model)
511{
512	int retval;
513	unsigned long flags;
514
515	spin_lock_irqsave(&pdc_lock, flags);
516	retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_INFO, __pa(pdc_result), 0);
517	convert_to_wide(pdc_result);
518	memcpy(model, pdc_result, sizeof(*model));
519	spin_unlock_irqrestore(&pdc_lock, flags);
520
521	return retval;
522}
523
524/**
525 * pdc_model_sysmodel - Get the system model name.
526 * @os_id: The operating system ID asked for (an OS_ID_* value)
527 * @name: A char array of at least 81 characters.
528 *
529 * Get system model name from PDC ROM (e.g. 9000/715 or 9000/778/B160L).
530 * Using OS_ID_HPUX will return the equivalent of the 'modelname' command
531 * on HP/UX.
532 */
533int pdc_model_sysmodel(unsigned int os_id, char *name)
534{
535        int retval;
536	unsigned long flags;
537
538        spin_lock_irqsave(&pdc_lock, flags);
539        retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_SYSMODEL, __pa(pdc_result),
540                              os_id, __pa(name));
541        convert_to_wide(pdc_result);
542
543        if (retval == PDC_OK) {
544                name[pdc_result[0]] = '\0'; /* add trailing '\0' */
545        } else {
546                name[0] = 0;
547        }
548        spin_unlock_irqrestore(&pdc_lock, flags);
549
550        return retval;
551}
552
553/**
554 * pdc_model_versions - Identify the version number of each processor.
555 * @versions: The return buffer.
556 * @id: The id of the processor to check.
557 *
558 * Returns the version number for each processor component.
559 *
560 * This comment was here before, but I do not know what it means :( -RB
561 * id: 0 = cpu revision, 1 = boot-rom-version
562 */
563int pdc_model_versions(unsigned long *versions, int id)
564{
565        int retval;
566	unsigned long flags;
567
568        spin_lock_irqsave(&pdc_lock, flags);
569        retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_VERSIONS, __pa(pdc_result), id);
570        convert_to_wide(pdc_result);
571        *versions = pdc_result[0];
572        spin_unlock_irqrestore(&pdc_lock, flags);
573
574        return retval;
575}
576
577/**
578 * pdc_model_cpuid - Returns the CPU_ID.
579 * @cpu_id: The return buffer.
580 *
581 * Returns the CPU_ID value which uniquely identifies the cpu portion of
582 * the processor module.
583 */
584int pdc_model_cpuid(unsigned long *cpu_id)
585{
586        int retval;
587	unsigned long flags;
588
589        spin_lock_irqsave(&pdc_lock, flags);
590        pdc_result[0] = 0; /* preset zero (call may not be implemented!) */
591        retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_CPU_ID, __pa(pdc_result), 0);
592        convert_to_wide(pdc_result);
593        *cpu_id = pdc_result[0];
594        spin_unlock_irqrestore(&pdc_lock, flags);
595
596        return retval;
597}
598
599/**
600 * pdc_model_capabilities - Returns the platform capabilities.
601 * @capabilities: The return buffer.
602 *
603 * Returns information about platform support for 32- and/or 64-bit
604 * OSes, IO-PDIR coherency, and virtual aliasing.
605 */
606int pdc_model_capabilities(unsigned long *capabilities)
607{
608        int retval;
609	unsigned long flags;
610
611        spin_lock_irqsave(&pdc_lock, flags);
612        pdc_result[0] = 0; /* preset zero (call may not be implemented!) */
613        retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_CAPABILITIES, __pa(pdc_result), 0);
614        convert_to_wide(pdc_result);
615        if (retval == PDC_OK) {
616                *capabilities = pdc_result[0];
617        } else {
618                *capabilities = PDC_MODEL_OS32;
619        }
620        spin_unlock_irqrestore(&pdc_lock, flags);
621
622        return retval;
623}
624
625/**
626 * pdc_model_platform_info - Returns machine product and serial number.
627 * @orig_prod_num: Return buffer for original product number.
628 * @current_prod_num: Return buffer for current product number.
629 * @serial_no: Return buffer for serial number.
630 *
631 * Returns strings containing the original and current product numbers and the
632 * serial number of the system.
633 */
634int pdc_model_platform_info(char *orig_prod_num, char *current_prod_num,
635		char *serial_no)
636{
637	int retval;
638	unsigned long flags;
639
640	spin_lock_irqsave(&pdc_lock, flags);
641	retval = mem_pdc_call(PDC_MODEL, PDC_MODEL_GET_PLATFORM_INFO,
642		__pa(orig_prod_num), __pa(current_prod_num), __pa(serial_no));
643	convert_to_wide(pdc_result);
644	spin_unlock_irqrestore(&pdc_lock, flags);
645
646	return retval;
647}
648
649/**
650 * pdc_cache_info - Return cache and TLB information.
651 * @cache_info: The return buffer.
652 *
653 * Returns information about the processor's cache and TLB.
654 */
655int pdc_cache_info(struct pdc_cache_info *cache_info)
656{
657        int retval;
658	unsigned long flags;
659
660        spin_lock_irqsave(&pdc_lock, flags);
661        retval = mem_pdc_call(PDC_CACHE, PDC_CACHE_INFO, __pa(pdc_result), 0);
662        convert_to_wide(pdc_result);
663        memcpy(cache_info, pdc_result, sizeof(*cache_info));
664        spin_unlock_irqrestore(&pdc_lock, flags);
665
666        return retval;
667}
668
669/**
670 * pdc_spaceid_bits - Return whether Space ID hashing is turned on.
671 * @space_bits: Should be 0, if not, bad mojo!
672 *
673 * Returns information about Space ID hashing.
674 */
675int pdc_spaceid_bits(unsigned long *space_bits)
676{
677	int retval;
678	unsigned long flags;
679
680	spin_lock_irqsave(&pdc_lock, flags);
681	pdc_result[0] = 0;
682	retval = mem_pdc_call(PDC_CACHE, PDC_CACHE_RET_SPID, __pa(pdc_result), 0);
683	convert_to_wide(pdc_result);
684	*space_bits = pdc_result[0];
685	spin_unlock_irqrestore(&pdc_lock, flags);
686
687	return retval;
688}
689
690/**
691 * pdc_btlb_info - Return block TLB information.
692 * @btlb: The return buffer.
693 *
694 * Returns information about the hardware Block TLB.
695 */
696int pdc_btlb_info(struct pdc_btlb_info *btlb)
697{
698	int retval;
699	unsigned long flags;
700
701	if (IS_ENABLED(CONFIG_PA20))
702		return PDC_BAD_PROC;
703
704	spin_lock_irqsave(&pdc_lock, flags);
705	retval = mem_pdc_call(PDC_BLOCK_TLB, PDC_BTLB_INFO, __pa(pdc_result), 0);
706	memcpy(btlb, pdc_result, sizeof(*btlb));
707	spin_unlock_irqrestore(&pdc_lock, flags);
708
709	if(retval < 0) {
710		btlb->max_size = 0;
711	}
712	return retval;
713}
714
715int pdc_btlb_insert(unsigned long long vpage, unsigned long physpage, unsigned long len,
716		    unsigned long entry_info, unsigned long slot)
717{
718	int retval;
719	unsigned long flags;
720
721	if (IS_ENABLED(CONFIG_PA20))
722		return PDC_BAD_PROC;
723
724	spin_lock_irqsave(&pdc_lock, flags);
725	retval = mem_pdc_call(PDC_BLOCK_TLB, PDC_BTLB_INSERT, (unsigned long) (vpage >> 32),
726			      (unsigned long) vpage, physpage, len, entry_info, slot);
727	spin_unlock_irqrestore(&pdc_lock, flags);
728	return retval;
729}
730
731int pdc_btlb_purge_all(void)
732{
733	int retval;
734	unsigned long flags;
735
736	if (IS_ENABLED(CONFIG_PA20))
737		return PDC_BAD_PROC;
738
739	spin_lock_irqsave(&pdc_lock, flags);
740	retval = mem_pdc_call(PDC_BLOCK_TLB, PDC_BTLB_PURGE_ALL);
741	spin_unlock_irqrestore(&pdc_lock, flags);
742	return retval;
743}
744
745/**
746 * pdc_mem_map_hpa - Find fixed module information.
747 * @address: The return buffer
748 * @mod_path: pointer to dev path structure.
749 *
750 * This call was developed for S700 workstations to allow the kernel to find
751 * the I/O devices (Core I/O). In the future (Kittyhawk and beyond) this
752 * call will be replaced (on workstations) by the architected PDC_SYSTEM_MAP
753 * call.
754 *
755 * This call is supported by all existing S700 workstations (up to  Gecko).
756 */
757int pdc_mem_map_hpa(struct pdc_memory_map *address,
758		struct pdc_module_path *mod_path)
759{
760        int retval;
761	unsigned long flags;
762
763	if (IS_ENABLED(CONFIG_PA20))
764		return PDC_BAD_PROC;
765
766        spin_lock_irqsave(&pdc_lock, flags);
767        memcpy(pdc_result2, mod_path, sizeof(*mod_path));
768        retval = mem_pdc_call(PDC_MEM_MAP, PDC_MEM_MAP_HPA, __pa(pdc_result),
769				__pa(pdc_result2));
770        memcpy(address, pdc_result, sizeof(*address));
771        spin_unlock_irqrestore(&pdc_lock, flags);
772
773        return retval;
774}
775
776/**
777 * pdc_lan_station_id - Get the LAN address.
778 * @lan_addr: The return buffer.
779 * @hpa: The network device HPA.
780 *
781 * Get the LAN station address when it is not directly available from the LAN hardware.
782 */
783int pdc_lan_station_id(char *lan_addr, unsigned long hpa)
784{
785	int retval;
786	unsigned long flags;
787
788	spin_lock_irqsave(&pdc_lock, flags);
789	retval = mem_pdc_call(PDC_LAN_STATION_ID, PDC_LAN_STATION_ID_READ,
790			__pa(pdc_result), hpa);
791	if (retval < 0) {
792		/* FIXME: else read MAC from NVRAM */
793		memset(lan_addr, 0, PDC_LAN_STATION_ID_SIZE);
794	} else {
795		memcpy(lan_addr, pdc_result, PDC_LAN_STATION_ID_SIZE);
796	}
797	spin_unlock_irqrestore(&pdc_lock, flags);
798
799	return retval;
800}
801EXPORT_SYMBOL(pdc_lan_station_id);
802
803/**
804 * pdc_stable_read - Read data from Stable Storage.
805 * @staddr: Stable Storage address to access.
806 * @memaddr: The memory address where Stable Storage data shall be copied.
807 * @count: number of bytes to transfer. count is multiple of 4.
808 *
809 * This PDC call reads from the Stable Storage address supplied in staddr
810 * and copies count bytes to the memory address memaddr.
811 * The call will fail if staddr+count > PDC_STABLE size.
812 */
813int pdc_stable_read(unsigned long staddr, void *memaddr, unsigned long count)
814{
815       int retval;
816	unsigned long flags;
817
818       spin_lock_irqsave(&pdc_lock, flags);
819       retval = mem_pdc_call(PDC_STABLE, PDC_STABLE_READ, staddr,
820               __pa(pdc_result), count);
821       convert_to_wide(pdc_result);
822       memcpy(memaddr, pdc_result, count);
823       spin_unlock_irqrestore(&pdc_lock, flags);
824
825       return retval;
826}
827EXPORT_SYMBOL(pdc_stable_read);
828
829/**
830 * pdc_stable_write - Write data to Stable Storage.
831 * @staddr: Stable Storage address to access.
832 * @memaddr: The memory address where Stable Storage data shall be read from.
833 * @count: number of bytes to transfer. count is multiple of 4.
834 *
835 * This PDC call reads count bytes from the supplied memaddr address,
836 * and copies count bytes to the Stable Storage address staddr.
837 * The call will fail if staddr+count > PDC_STABLE size.
838 */
839int pdc_stable_write(unsigned long staddr, void *memaddr, unsigned long count)
840{
841       int retval;
842	unsigned long flags;
843
844       spin_lock_irqsave(&pdc_lock, flags);
845       memcpy(pdc_result, memaddr, count);
846       convert_to_wide(pdc_result);
847       retval = mem_pdc_call(PDC_STABLE, PDC_STABLE_WRITE, staddr,
848               __pa(pdc_result), count);
849       spin_unlock_irqrestore(&pdc_lock, flags);
850
851       return retval;
852}
853EXPORT_SYMBOL(pdc_stable_write);
854
855/**
856 * pdc_stable_get_size - Get Stable Storage size in bytes.
857 * @size: pointer where the size will be stored.
858 *
859 * This PDC call returns the number of bytes in the processor's Stable
860 * Storage, which is the number of contiguous bytes implemented in Stable
861 * Storage starting from staddr=0. size in an unsigned 64-bit integer
862 * which is a multiple of four.
863 */
864int pdc_stable_get_size(unsigned long *size)
865{
866       int retval;
867	unsigned long flags;
868
869       spin_lock_irqsave(&pdc_lock, flags);
870       retval = mem_pdc_call(PDC_STABLE, PDC_STABLE_RETURN_SIZE, __pa(pdc_result));
871       *size = pdc_result[0];
872       spin_unlock_irqrestore(&pdc_lock, flags);
873
874       return retval;
875}
876EXPORT_SYMBOL(pdc_stable_get_size);
877
878/**
879 * pdc_stable_verify_contents - Checks that Stable Storage contents are valid.
880 *
881 * This PDC call is meant to be used to check the integrity of the current
882 * contents of Stable Storage.
883 */
884int pdc_stable_verify_contents(void)
885{
886       int retval;
887	unsigned long flags;
888
889       spin_lock_irqsave(&pdc_lock, flags);
890       retval = mem_pdc_call(PDC_STABLE, PDC_STABLE_VERIFY_CONTENTS);
891       spin_unlock_irqrestore(&pdc_lock, flags);
892
893       return retval;
894}
895EXPORT_SYMBOL(pdc_stable_verify_contents);
896
897/**
898 * pdc_stable_initialize - Sets Stable Storage contents to zero and initialize
899 * the validity indicator.
900 *
901 * This PDC call will erase all contents of Stable Storage. Use with care!
902 */
903int pdc_stable_initialize(void)
904{
905       int retval;
906	unsigned long flags;
907
908       spin_lock_irqsave(&pdc_lock, flags);
909       retval = mem_pdc_call(PDC_STABLE, PDC_STABLE_INITIALIZE);
910       spin_unlock_irqrestore(&pdc_lock, flags);
911
912       return retval;
913}
914EXPORT_SYMBOL(pdc_stable_initialize);
915
916/**
917 * pdc_get_initiator - Get the SCSI Interface Card params (SCSI ID, SDTR, SE or LVD)
918 * @hwpath: fully bc.mod style path to the device.
919 * @initiator: the array to return the result into
920 *
921 * Get the SCSI operational parameters from PDC.
922 * Needed since HPUX never used BIOS or symbios card NVRAM.
923 * Most ncr/sym cards won't have an entry and just use whatever
924 * capabilities of the card are (eg Ultra, LVD). But there are
925 * several cases where it's useful:
926 *    o set SCSI id for Multi-initiator clusters,
927 *    o cable too long (ie SE scsi 10Mhz won't support 6m length),
928 *    o bus width exported is less than what the interface chip supports.
929 */
930int pdc_get_initiator(struct hardware_path *hwpath, struct pdc_initiator *initiator)
931{
932	int retval;
933	unsigned long flags;
934
935	spin_lock_irqsave(&pdc_lock, flags);
936
937/* BCJ-XXXX series boxes. E.G. "9000/785/C3000" */
938#define IS_SPROCKETS() (strlen(boot_cpu_data.pdc.sys_model_name) == 14 && \
939	strncmp(boot_cpu_data.pdc.sys_model_name, "9000/785", 8) == 0)
940
941	retval = mem_pdc_call(PDC_INITIATOR, PDC_GET_INITIATOR,
942			      __pa(pdc_result), __pa(hwpath));
943	if (retval < PDC_OK)
944		goto out;
945
946	if (pdc_result[0] < 16) {
947		initiator->host_id = pdc_result[0];
948	} else {
949		initiator->host_id = -1;
950	}
951
952	/*
953	 * Sprockets and Piranha return 20 or 40 (MT/s).  Prelude returns
954	 * 1, 2, 5 or 10 for 5, 10, 20 or 40 MT/s, respectively
955	 */
956	switch (pdc_result[1]) {
957		case  1: initiator->factor = 50; break;
958		case  2: initiator->factor = 25; break;
959		case  5: initiator->factor = 12; break;
960		case 25: initiator->factor = 10; break;
961		case 20: initiator->factor = 12; break;
962		case 40: initiator->factor = 10; break;
963		default: initiator->factor = -1; break;
964	}
965
966	if (IS_SPROCKETS()) {
967		initiator->width = pdc_result[4];
968		initiator->mode = pdc_result[5];
969	} else {
970		initiator->width = -1;
971		initiator->mode = -1;
972	}
973
974 out:
975	spin_unlock_irqrestore(&pdc_lock, flags);
976
977	return (retval >= PDC_OK);
978}
979EXPORT_SYMBOL(pdc_get_initiator);
980
981
982/**
983 * pdc_pci_irt_size - Get the number of entries in the interrupt routing table.
984 * @num_entries: The return value.
985 * @hpa: The HPA for the device.
986 *
987 * This PDC function returns the number of entries in the specified cell's
988 * interrupt table.
989 * Similar to PDC_PAT stuff - but added for Forte/Allegro boxes
990 */
991int pdc_pci_irt_size(unsigned long *num_entries, unsigned long hpa)
992{
993	int retval;
994	unsigned long flags;
995
996	spin_lock_irqsave(&pdc_lock, flags);
997	retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_GET_INT_TBL_SIZE,
998			      __pa(pdc_result), hpa);
999	convert_to_wide(pdc_result);
1000	*num_entries = pdc_result[0];
1001	spin_unlock_irqrestore(&pdc_lock, flags);
1002
1003	return retval;
1004}
1005
1006/**
1007 * pdc_pci_irt - Get the PCI interrupt routing table.
1008 * @num_entries: The number of entries in the table.
1009 * @hpa: The Hard Physical Address of the device.
1010 * @tbl:
1011 *
1012 * Get the PCI interrupt routing table for the device at the given HPA.
1013 * Similar to PDC_PAT stuff - but added for Forte/Allegro boxes
1014 */
1015int pdc_pci_irt(unsigned long num_entries, unsigned long hpa, void *tbl)
1016{
1017	int retval;
1018	unsigned long flags;
1019
1020	BUG_ON((unsigned long)tbl & 0x7);
1021
1022	spin_lock_irqsave(&pdc_lock, flags);
1023	pdc_result[0] = num_entries;
1024	retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_GET_INT_TBL,
1025			      __pa(pdc_result), hpa, __pa(tbl));
1026	spin_unlock_irqrestore(&pdc_lock, flags);
1027
1028	return retval;
1029}
1030
1031
1032#if 0	/* UNTEST CODE - left here in case someone needs it */
1033
1034/**
1035 * pdc_pci_config_read - read PCI config space.
1036 * @hpa: Token from PDC to indicate which PCI device
1037 * @cfg_addr: Configuration space address to read from
1038 *
1039 * Read PCI Configuration space *before* linux PCI subsystem is running.
1040 */
1041unsigned int pdc_pci_config_read(void *hpa, unsigned long cfg_addr)
1042{
1043	int retval;
1044	unsigned long flags;
1045
1046	spin_lock_irqsave(&pdc_lock, flags);
1047	pdc_result[0] = 0;
1048	pdc_result[1] = 0;
1049	retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_READ_CONFIG,
1050			      __pa(pdc_result), hpa, cfg_addr&~3UL, 4UL);
1051	spin_unlock_irqrestore(&pdc_lock, flags);
1052
1053	return retval ? ~0 : (unsigned int) pdc_result[0];
1054}
1055
1056
1057/**
1058 * pdc_pci_config_write - read PCI config space.
1059 * @hpa: Token from PDC to indicate which PCI device
1060 * @cfg_addr: Configuration space address to write
1061 * @val: Value we want in the 32-bit register
1062 *
1063 * Write PCI Configuration space *before* linux PCI subsystem is running.
1064 */
1065void pdc_pci_config_write(void *hpa, unsigned long cfg_addr, unsigned int val)
1066{
1067	int retval;
1068	unsigned long flags;
1069
1070	spin_lock_irqsave(&pdc_lock, flags);
1071	pdc_result[0] = 0;
1072	retval = mem_pdc_call(PDC_PCI_INDEX, PDC_PCI_WRITE_CONFIG,
1073			      __pa(pdc_result), hpa,
1074			      cfg_addr&~3UL, 4UL, (unsigned long) val);
1075	spin_unlock_irqrestore(&pdc_lock, flags);
1076
1077	return retval;
1078}
1079#endif /* UNTESTED CODE */
1080
1081/**
1082 * pdc_tod_read - Read the Time-Of-Day clock.
1083 * @tod: The return buffer:
1084 *
1085 * Read the Time-Of-Day clock
1086 */
1087int pdc_tod_read(struct pdc_tod *tod)
1088{
1089        int retval;
1090	unsigned long flags;
1091
1092        spin_lock_irqsave(&pdc_lock, flags);
1093        retval = mem_pdc_call(PDC_TOD, PDC_TOD_READ, __pa(pdc_result), 0);
1094        convert_to_wide(pdc_result);
1095        memcpy(tod, pdc_result, sizeof(*tod));
1096        spin_unlock_irqrestore(&pdc_lock, flags);
1097
1098        return retval;
1099}
1100EXPORT_SYMBOL(pdc_tod_read);
1101
1102int pdc_mem_pdt_info(struct pdc_mem_retinfo *rinfo)
1103{
1104	int retval;
1105	unsigned long flags;
1106
1107	spin_lock_irqsave(&pdc_lock, flags);
1108	retval = mem_pdc_call(PDC_MEM, PDC_MEM_MEMINFO, __pa(pdc_result), 0);
1109	convert_to_wide(pdc_result);
1110	memcpy(rinfo, pdc_result, sizeof(*rinfo));
1111	spin_unlock_irqrestore(&pdc_lock, flags);
1112
1113	return retval;
1114}
1115
1116int pdc_mem_pdt_read_entries(struct pdc_mem_read_pdt *pret,
1117		unsigned long *pdt_entries_ptr)
1118{
1119	int retval;
1120	unsigned long flags;
1121
1122	spin_lock_irqsave(&pdc_lock, flags);
1123	retval = mem_pdc_call(PDC_MEM, PDC_MEM_READ_PDT, __pa(pdc_result),
1124			__pa(pdt_entries_ptr));
1125	if (retval == PDC_OK) {
1126		convert_to_wide(pdc_result);
1127		memcpy(pret, pdc_result, sizeof(*pret));
1128	}
1129	spin_unlock_irqrestore(&pdc_lock, flags);
1130
1131#ifdef CONFIG_64BIT
1132	/*
1133	 * 64-bit kernels should not call this PDT function in narrow mode.
1134	 * The pdt_entries_ptr array above will now contain 32-bit values
1135	 */
1136	if (WARN_ON_ONCE((retval == PDC_OK) && parisc_narrow_firmware))
1137		return PDC_ERROR;
1138#endif
1139
1140	return retval;
1141}
1142
1143/**
1144 * pdc_pim_toc11 - Fetch TOC PIM 1.1 data from firmware.
1145 * @ret: pointer to return buffer
1146 */
1147int pdc_pim_toc11(struct pdc_toc_pim_11 *ret)
1148{
1149	int retval;
1150	unsigned long flags;
1151
1152	spin_lock_irqsave(&pdc_lock, flags);
1153	retval = mem_pdc_call(PDC_PIM, PDC_PIM_TOC, __pa(pdc_result),
1154			      __pa(ret), sizeof(*ret));
1155	spin_unlock_irqrestore(&pdc_lock, flags);
1156	return retval;
1157}
1158
1159/**
1160 * pdc_pim_toc20 - Fetch TOC PIM 2.0 data from firmware.
1161 * @ret: pointer to return buffer
1162 */
1163int pdc_pim_toc20(struct pdc_toc_pim_20 *ret)
1164{
1165	int retval;
1166	unsigned long flags;
1167
1168	spin_lock_irqsave(&pdc_lock, flags);
1169	retval = mem_pdc_call(PDC_PIM, PDC_PIM_TOC, __pa(pdc_result),
1170			      __pa(ret), sizeof(*ret));
1171	spin_unlock_irqrestore(&pdc_lock, flags);
1172	return retval;
1173}
1174
1175/**
1176 * pdc_tod_set - Set the Time-Of-Day clock.
1177 * @sec: The number of seconds since epoch.
1178 * @usec: The number of micro seconds.
1179 *
1180 * Set the Time-Of-Day clock.
1181 */
1182int pdc_tod_set(unsigned long sec, unsigned long usec)
1183{
1184        int retval;
1185	unsigned long flags;
1186
1187        spin_lock_irqsave(&pdc_lock, flags);
1188        retval = mem_pdc_call(PDC_TOD, PDC_TOD_WRITE, sec, usec);
1189        spin_unlock_irqrestore(&pdc_lock, flags);
1190
1191        return retval;
1192}
1193EXPORT_SYMBOL(pdc_tod_set);
1194
1195#ifdef CONFIG_64BIT
1196int pdc_mem_mem_table(struct pdc_memory_table_raddr *r_addr,
1197		struct pdc_memory_table *tbl, unsigned long entries)
1198{
1199	int retval;
1200	unsigned long flags;
1201
1202	spin_lock_irqsave(&pdc_lock, flags);
1203	retval = mem_pdc_call(PDC_MEM, PDC_MEM_TABLE, __pa(pdc_result), __pa(pdc_result2), entries);
1204	convert_to_wide(pdc_result);
1205	memcpy(r_addr, pdc_result, sizeof(*r_addr));
1206	memcpy(tbl, pdc_result2, entries * sizeof(*tbl));
1207	spin_unlock_irqrestore(&pdc_lock, flags);
1208
1209	return retval;
1210}
1211#endif /* CONFIG_64BIT */
1212
1213/* FIXME: Is this pdc used?  I could not find type reference to ftc_bitmap
1214 * so I guessed at unsigned long.  Someone who knows what this does, can fix
1215 * it later. :)
1216 */
1217int pdc_do_firm_test_reset(unsigned long ftc_bitmap)
1218{
1219        int retval;
1220	unsigned long flags;
1221
1222        spin_lock_irqsave(&pdc_lock, flags);
1223        retval = mem_pdc_call(PDC_BROADCAST_RESET, PDC_DO_FIRM_TEST_RESET,
1224                              PDC_FIRM_TEST_MAGIC, ftc_bitmap);
1225        spin_unlock_irqrestore(&pdc_lock, flags);
1226
1227        return retval;
1228}
1229
1230/*
1231 * pdc_do_reset - Reset the system.
1232 *
1233 * Reset the system.
1234 */
1235int pdc_do_reset(void)
1236{
1237        int retval;
1238	unsigned long flags;
1239
1240        spin_lock_irqsave(&pdc_lock, flags);
1241        retval = mem_pdc_call(PDC_BROADCAST_RESET, PDC_DO_RESET);
1242        spin_unlock_irqrestore(&pdc_lock, flags);
1243
1244        return retval;
1245}
1246
1247/*
1248 * pdc_soft_power_info - Enable soft power switch.
1249 * @power_reg: address of soft power register
1250 *
1251 * Return the absolute address of the soft power switch register
1252 */
1253int __init pdc_soft_power_info(unsigned long *power_reg)
1254{
1255	int retval;
1256	unsigned long flags;
1257
1258	*power_reg = (unsigned long) (-1);
1259
1260	spin_lock_irqsave(&pdc_lock, flags);
1261	retval = mem_pdc_call(PDC_SOFT_POWER, PDC_SOFT_POWER_INFO, __pa(pdc_result), 0);
1262	if (retval == PDC_OK) {
1263                convert_to_wide(pdc_result);
1264                *power_reg = f_extend(pdc_result[0]);
1265	}
1266	spin_unlock_irqrestore(&pdc_lock, flags);
1267
1268	return retval;
1269}
1270
1271/*
1272 * pdc_soft_power_button{_panic} - Control the soft power button behaviour
1273 * @sw_control: 0 for hardware control, 1 for software control
1274 *
1275 *
1276 * This PDC function places the soft power button under software or
1277 * hardware control.
1278 * Under software control the OS may control to when to allow to shut
1279 * down the system. Under hardware control pressing the power button
1280 * powers off the system immediately.
1281 *
1282 * The _panic version relies on spin_trylock to prevent deadlock
1283 * on panic path.
1284 */
1285int pdc_soft_power_button(int sw_control)
1286{
1287	int retval;
1288	unsigned long flags;
1289
1290	spin_lock_irqsave(&pdc_lock, flags);
1291	retval = mem_pdc_call(PDC_SOFT_POWER, PDC_SOFT_POWER_ENABLE, __pa(pdc_result), sw_control);
1292	spin_unlock_irqrestore(&pdc_lock, flags);
1293
1294	return retval;
1295}
1296
1297int pdc_soft_power_button_panic(int sw_control)
1298{
1299	int retval;
1300	unsigned long flags;
1301
1302	if (!spin_trylock_irqsave(&pdc_lock, flags)) {
1303		pr_emerg("Couldn't enable soft power button\n");
1304		return -EBUSY; /* ignored by the panic notifier */
1305	}
1306
1307	retval = mem_pdc_call(PDC_SOFT_POWER, PDC_SOFT_POWER_ENABLE, __pa(pdc_result), sw_control);
1308	spin_unlock_irqrestore(&pdc_lock, flags);
1309
1310	return retval;
1311}
1312
1313/*
1314 * pdc_io_reset - Hack to avoid overlapping range registers of Bridges devices.
1315 * Primarily a problem on T600 (which parisc-linux doesn't support) but
1316 * who knows what other platform firmware might do with this OS "hook".
1317 */
1318void pdc_io_reset(void)
1319{
1320	unsigned long flags;
1321
1322	spin_lock_irqsave(&pdc_lock, flags);
1323	mem_pdc_call(PDC_IO, PDC_IO_RESET, 0);
1324	spin_unlock_irqrestore(&pdc_lock, flags);
1325}
1326
1327/*
1328 * pdc_io_reset_devices - Hack to Stop USB controller
1329 *
1330 * If PDC used the usb controller, the usb controller
1331 * is still running and will crash the machines during iommu
1332 * setup, because of still running DMA. This PDC call
1333 * stops the USB controller.
1334 * Normally called after calling pdc_io_reset().
1335 */
1336void pdc_io_reset_devices(void)
1337{
1338	unsigned long flags;
1339
1340	spin_lock_irqsave(&pdc_lock, flags);
1341	mem_pdc_call(PDC_IO, PDC_IO_RESET_DEVICES, 0);
1342	spin_unlock_irqrestore(&pdc_lock, flags);
1343}
1344
1345#endif /* defined(BOOTLOADER) */
1346
1347/* locked by pdc_lock */
1348static char iodc_dbuf[4096] __page_aligned_bss;
1349
1350/**
1351 * pdc_iodc_print - Console print using IODC.
1352 * @str: the string to output.
1353 * @count: length of str
1354 *
1355 * Note that only these special chars are architected for console IODC io:
1356 * BEL, BS, CR, and LF. Others are passed through.
1357 * Since the HP console requires CR+LF to perform a 'newline', we translate
1358 * "\n" to "\r\n".
1359 */
1360int pdc_iodc_print(const unsigned char *str, unsigned count)
1361{
1362	unsigned int i, found = 0;
1363	unsigned long flags;
1364
1365	count = min_t(unsigned int, count, sizeof(iodc_dbuf));
1366
1367	spin_lock_irqsave(&pdc_lock, flags);
1368	for (i = 0; i < count;) {
1369		switch(str[i]) {
1370		case '\n':
1371			iodc_dbuf[i+0] = '\r';
1372			iodc_dbuf[i+1] = '\n';
1373			i += 2;
1374			found = 1;
1375			goto print;
1376		default:
1377			iodc_dbuf[i] = str[i];
1378			i++;
1379			break;
1380		}
1381	}
1382
1383print:
1384	real32_call(PAGE0->mem_cons.iodc_io,
1385		(unsigned long)PAGE0->mem_cons.hpa, ENTRY_IO_COUT,
1386		PAGE0->mem_cons.spa, __pa(PAGE0->mem_cons.dp.layers),
1387		__pa(pdc_result), 0, __pa(iodc_dbuf), i, 0);
1388	spin_unlock_irqrestore(&pdc_lock, flags);
1389
1390	return i - found;
1391}
1392
1393#if !defined(BOOTLOADER)
1394/**
1395 * pdc_iodc_getc - Read a character (non-blocking) from the PDC console.
1396 *
1397 * Read a character (non-blocking) from the PDC console, returns -1 if
1398 * key is not present.
1399 */
1400int pdc_iodc_getc(void)
1401{
1402	int ch;
1403	int status;
1404	unsigned long flags;
1405
1406	/* Bail if no console input device. */
1407	if (!PAGE0->mem_kbd.iodc_io)
1408		return 0;
1409
1410	/* wait for a keyboard (rs232)-input */
1411	spin_lock_irqsave(&pdc_lock, flags);
1412	real32_call(PAGE0->mem_kbd.iodc_io,
1413		    (unsigned long)PAGE0->mem_kbd.hpa, ENTRY_IO_CIN,
1414		    PAGE0->mem_kbd.spa, __pa(PAGE0->mem_kbd.dp.layers),
1415		    __pa(pdc_result), 0, __pa(iodc_dbuf), 1, 0);
1416
1417	ch = *iodc_dbuf;
1418	/* like convert_to_wide() but for first return value only: */
1419	status = *(int *)&pdc_result;
1420	spin_unlock_irqrestore(&pdc_lock, flags);
1421
1422	if (status == 0)
1423	    return -1;
1424
1425	return ch;
1426}
1427
1428int pdc_sti_call(unsigned long func, unsigned long flags,
1429		unsigned long inptr, unsigned long outputr,
1430		unsigned long glob_cfg, int do_call64)
1431{
1432	int retval = 0;
1433	unsigned long irqflags;
1434
1435	spin_lock_irqsave(&pdc_lock, irqflags);
1436	if (IS_ENABLED(CONFIG_64BIT) && do_call64) {
1437#ifdef CONFIG_64BIT
1438		retval = real64_call(func, flags, inptr, outputr, glob_cfg);
1439#else
1440		WARN_ON(1);
1441#endif
1442	} else {
1443		retval = real32_call(func, flags, inptr, outputr, glob_cfg);
1444	}
1445	spin_unlock_irqrestore(&pdc_lock, irqflags);
1446
1447	return retval;
1448}
1449EXPORT_SYMBOL(pdc_sti_call);
1450
1451#ifdef CONFIG_64BIT
1452/**
1453 * pdc_pat_cell_get_number - Returns the cell number.
1454 * @cell_info: The return buffer.
1455 *
1456 * This PDC call returns the cell number of the cell from which the call
1457 * is made.
1458 */
1459int pdc_pat_cell_get_number(struct pdc_pat_cell_num *cell_info)
1460{
1461	int retval;
1462	unsigned long flags;
1463
1464	spin_lock_irqsave(&pdc_lock, flags);
1465	retval = mem_pdc_call(PDC_PAT_CELL, PDC_PAT_CELL_GET_NUMBER, __pa(pdc_result));
1466	memcpy(cell_info, pdc_result, sizeof(*cell_info));
1467	spin_unlock_irqrestore(&pdc_lock, flags);
1468
1469	return retval;
1470}
1471
1472/**
1473 * pdc_pat_cell_module - Retrieve the cell's module information.
1474 * @actcnt: The number of bytes written to mem_addr.
1475 * @ploc: The physical location.
1476 * @mod: The module index.
1477 * @view_type: The view of the address type.
1478 * @mem_addr: The return buffer.
1479 *
1480 * This PDC call returns information about each module attached to the cell
1481 * at the specified location.
1482 */
1483int pdc_pat_cell_module(unsigned long *actcnt, unsigned long ploc, unsigned long mod,
1484			unsigned long view_type, void *mem_addr)
1485{
1486	int retval;
1487	unsigned long flags;
1488	static struct pdc_pat_cell_mod_maddr_block result __attribute__ ((aligned (8)));
1489
1490	spin_lock_irqsave(&pdc_lock, flags);
1491	retval = mem_pdc_call(PDC_PAT_CELL, PDC_PAT_CELL_MODULE, __pa(pdc_result),
1492			      ploc, mod, view_type, __pa(&result));
1493	if(!retval) {
1494		*actcnt = pdc_result[0];
1495		memcpy(mem_addr, &result, *actcnt);
1496	}
1497	spin_unlock_irqrestore(&pdc_lock, flags);
1498
1499	return retval;
1500}
1501
1502/**
1503 * pdc_pat_cell_info - Retrieve the cell's information.
1504 * @info: The pointer to a struct pdc_pat_cell_info_rtn_block.
1505 * @actcnt: The number of bytes which should be written to info.
1506 * @offset: offset of the structure.
1507 * @cell_number: The cell number which should be asked, or -1 for current cell.
1508 *
1509 * This PDC call returns information about the given cell (or all cells).
1510 */
1511int pdc_pat_cell_info(struct pdc_pat_cell_info_rtn_block *info,
1512		unsigned long *actcnt, unsigned long offset,
1513		unsigned long cell_number)
1514{
1515	int retval;
1516	unsigned long flags;
1517	struct pdc_pat_cell_info_rtn_block result;
1518
1519	spin_lock_irqsave(&pdc_lock, flags);
1520	retval = mem_pdc_call(PDC_PAT_CELL, PDC_PAT_CELL_GET_INFO,
1521			__pa(pdc_result), __pa(&result), *actcnt,
1522			offset, cell_number);
1523	if (!retval) {
1524		*actcnt = pdc_result[0];
1525		memcpy(info, &result, *actcnt);
1526	}
1527	spin_unlock_irqrestore(&pdc_lock, flags);
1528
1529	return retval;
1530}
1531
1532/**
1533 * pdc_pat_cpu_get_number - Retrieve the cpu number.
1534 * @cpu_info: The return buffer.
1535 * @hpa: The Hard Physical Address of the CPU.
1536 *
1537 * Retrieve the cpu number for the cpu at the specified HPA.
1538 */
1539int pdc_pat_cpu_get_number(struct pdc_pat_cpu_num *cpu_info, unsigned long hpa)
1540{
1541	int retval;
1542	unsigned long flags;
1543
1544	spin_lock_irqsave(&pdc_lock, flags);
1545	retval = mem_pdc_call(PDC_PAT_CPU, PDC_PAT_CPU_GET_NUMBER,
1546			      __pa(&pdc_result), hpa);
1547	memcpy(cpu_info, pdc_result, sizeof(*cpu_info));
1548	spin_unlock_irqrestore(&pdc_lock, flags);
1549
1550	return retval;
1551}
1552
1553/**
1554 * pdc_pat_get_irt_size - Retrieve the number of entries in the cell's interrupt table.
1555 * @num_entries: The return value.
1556 * @cell_num: The target cell.
1557 *
1558 * This PDC function returns the number of entries in the specified cell's
1559 * interrupt table.
1560 */
1561int pdc_pat_get_irt_size(unsigned long *num_entries, unsigned long cell_num)
1562{
1563	int retval;
1564	unsigned long flags;
1565
1566	spin_lock_irqsave(&pdc_lock, flags);
1567	retval = mem_pdc_call(PDC_PAT_IO, PDC_PAT_IO_GET_PCI_ROUTING_TABLE_SIZE,
1568			      __pa(pdc_result), cell_num);
1569	*num_entries = pdc_result[0];
1570	spin_unlock_irqrestore(&pdc_lock, flags);
1571
1572	return retval;
1573}
1574
1575/**
1576 * pdc_pat_get_irt - Retrieve the cell's interrupt table.
1577 * @r_addr: The return buffer.
1578 * @cell_num: The target cell.
1579 *
1580 * This PDC function returns the actual interrupt table for the specified cell.
1581 */
1582int pdc_pat_get_irt(void *r_addr, unsigned long cell_num)
1583{
1584	int retval;
1585	unsigned long flags;
1586
1587	spin_lock_irqsave(&pdc_lock, flags);
1588	retval = mem_pdc_call(PDC_PAT_IO, PDC_PAT_IO_GET_PCI_ROUTING_TABLE,
1589			      __pa(r_addr), cell_num);
1590	spin_unlock_irqrestore(&pdc_lock, flags);
1591
1592	return retval;
1593}
1594
1595/**
1596 * pdc_pat_pd_get_addr_map - Retrieve information about memory address ranges.
1597 * @actual_len: The return buffer.
1598 * @mem_addr: Pointer to the memory buffer.
1599 * @count: The number of bytes to read from the buffer.
1600 * @offset: The offset with respect to the beginning of the buffer.
1601 *
1602 */
1603int pdc_pat_pd_get_addr_map(unsigned long *actual_len, void *mem_addr,
1604			    unsigned long count, unsigned long offset)
1605{
1606	int retval;
1607	unsigned long flags;
1608
1609	spin_lock_irqsave(&pdc_lock, flags);
1610	retval = mem_pdc_call(PDC_PAT_PD, PDC_PAT_PD_GET_ADDR_MAP, __pa(pdc_result),
1611			      __pa(pdc_result2), count, offset);
1612	*actual_len = pdc_result[0];
1613	memcpy(mem_addr, pdc_result2, *actual_len);
1614	spin_unlock_irqrestore(&pdc_lock, flags);
1615
1616	return retval;
1617}
1618
1619/**
1620 * pdc_pat_pd_get_pdc_revisions - Retrieve PDC interface revisions.
1621 * @legacy_rev: The legacy revision.
1622 * @pat_rev: The PAT revision.
1623 * @pdc_cap: The PDC capabilities.
1624 *
1625 */
1626int pdc_pat_pd_get_pdc_revisions(unsigned long *legacy_rev,
1627		unsigned long *pat_rev, unsigned long *pdc_cap)
1628{
1629	int retval;
1630	unsigned long flags;
1631
1632	spin_lock_irqsave(&pdc_lock, flags);
1633	retval = mem_pdc_call(PDC_PAT_PD, PDC_PAT_PD_GET_PDC_INTERF_REV,
1634				__pa(pdc_result));
1635	if (retval == PDC_OK) {
1636		*legacy_rev = pdc_result[0];
1637		*pat_rev = pdc_result[1];
1638		*pdc_cap = pdc_result[2];
1639	}
1640	spin_unlock_irqrestore(&pdc_lock, flags);
1641
1642	return retval;
1643}
1644
1645
1646/**
1647 * pdc_pat_io_pci_cfg_read - Read PCI configuration space.
1648 * @pci_addr: PCI configuration space address for which the read request is being made.
1649 * @pci_size: Size of read in bytes. Valid values are 1, 2, and 4.
1650 * @mem_addr: Pointer to return memory buffer.
1651 *
1652 */
1653int pdc_pat_io_pci_cfg_read(unsigned long pci_addr, int pci_size, u32 *mem_addr)
1654{
1655	int retval;
1656	unsigned long flags;
1657
1658	spin_lock_irqsave(&pdc_lock, flags);
1659	retval = mem_pdc_call(PDC_PAT_IO, PDC_PAT_IO_PCI_CONFIG_READ,
1660					__pa(pdc_result), pci_addr, pci_size);
1661	switch(pci_size) {
1662		case 1: *(u8 *) mem_addr =  (u8)  pdc_result[0]; break;
1663		case 2: *(u16 *)mem_addr =  (u16) pdc_result[0]; break;
1664		case 4: *(u32 *)mem_addr =  (u32) pdc_result[0]; break;
1665	}
1666	spin_unlock_irqrestore(&pdc_lock, flags);
1667
1668	return retval;
1669}
1670
1671/**
1672 * pdc_pat_io_pci_cfg_write - Retrieve information about memory address ranges.
1673 * @pci_addr: PCI configuration space address for which the write  request is being made.
1674 * @pci_size: Size of write in bytes. Valid values are 1, 2, and 4.
1675 * @val: Pointer to 1, 2, or 4 byte value in low order end of argument to be
1676 *         written to PCI Config space.
1677 *
1678 */
1679int pdc_pat_io_pci_cfg_write(unsigned long pci_addr, int pci_size, u32 val)
1680{
1681	int retval;
1682	unsigned long flags;
1683
1684	spin_lock_irqsave(&pdc_lock, flags);
1685	retval = mem_pdc_call(PDC_PAT_IO, PDC_PAT_IO_PCI_CONFIG_WRITE,
1686				pci_addr, pci_size, val);
1687	spin_unlock_irqrestore(&pdc_lock, flags);
1688
1689	return retval;
1690}
1691
1692/**
1693 * pdc_pat_mem_pdt_info - Retrieve information about page deallocation table
1694 * @rinfo: memory pdt information
1695 *
1696 */
1697int pdc_pat_mem_pdt_info(struct pdc_pat_mem_retinfo *rinfo)
1698{
1699	int retval;
1700	unsigned long flags;
1701
1702	spin_lock_irqsave(&pdc_lock, flags);
1703	retval = mem_pdc_call(PDC_PAT_MEM, PDC_PAT_MEM_PD_INFO,
1704			__pa(&pdc_result));
1705	if (retval == PDC_OK)
1706		memcpy(rinfo, &pdc_result, sizeof(*rinfo));
1707	spin_unlock_irqrestore(&pdc_lock, flags);
1708
1709	return retval;
1710}
1711
1712/**
1713 * pdc_pat_mem_pdt_cell_info - Retrieve information about page deallocation
1714 *				table of a cell
1715 * @rinfo: memory pdt information
1716 * @cell: cell number
1717 *
1718 */
1719int pdc_pat_mem_pdt_cell_info(struct pdc_pat_mem_cell_pdt_retinfo *rinfo,
1720		unsigned long cell)
1721{
1722	int retval;
1723	unsigned long flags;
1724
1725	spin_lock_irqsave(&pdc_lock, flags);
1726	retval = mem_pdc_call(PDC_PAT_MEM, PDC_PAT_MEM_CELL_INFO,
1727			__pa(&pdc_result), cell);
1728	if (retval == PDC_OK)
1729		memcpy(rinfo, &pdc_result, sizeof(*rinfo));
1730	spin_unlock_irqrestore(&pdc_lock, flags);
1731
1732	return retval;
1733}
1734
1735/**
1736 * pdc_pat_mem_read_cell_pdt - Read PDT entries from (old) PAT firmware
1737 * @pret: array of PDT entries
1738 * @pdt_entries_ptr: ptr to hold number of PDT entries
1739 * @max_entries: maximum number of entries to be read
1740 *
1741 */
1742int pdc_pat_mem_read_cell_pdt(struct pdc_pat_mem_read_pd_retinfo *pret,
1743		unsigned long *pdt_entries_ptr, unsigned long max_entries)
1744{
1745	int retval;
1746	unsigned long flags, entries;
1747
1748	spin_lock_irqsave(&pdc_lock, flags);
1749	/* PDC_PAT_MEM_CELL_READ is available on early PAT machines only */
1750	retval = mem_pdc_call(PDC_PAT_MEM, PDC_PAT_MEM_CELL_READ,
1751			__pa(&pdc_result), parisc_cell_num,
1752			__pa(pdt_entries_ptr));
1753
1754	if (retval == PDC_OK) {
1755		/* build up return value as for PDC_PAT_MEM_PD_READ */
1756		entries = min(pdc_result[0], max_entries);
1757		pret->pdt_entries = entries;
1758		pret->actual_count_bytes = entries * sizeof(unsigned long);
1759	}
1760
1761	spin_unlock_irqrestore(&pdc_lock, flags);
1762	WARN_ON(retval == PDC_OK && pdc_result[0] > max_entries);
1763
1764	return retval;
1765}
1766/**
1767 * pdc_pat_mem_read_pd_pdt - Read PDT entries from (newer) PAT firmware
1768 * @pret: array of PDT entries
1769 * @pdt_entries_ptr: ptr to hold number of PDT entries
1770 * @count: number of bytes to read
1771 * @offset: offset to start (in bytes)
1772 *
1773 */
1774int pdc_pat_mem_read_pd_pdt(struct pdc_pat_mem_read_pd_retinfo *pret,
1775		unsigned long *pdt_entries_ptr, unsigned long count,
1776		unsigned long offset)
1777{
1778	int retval;
1779	unsigned long flags, entries;
1780
1781	spin_lock_irqsave(&pdc_lock, flags);
1782	retval = mem_pdc_call(PDC_PAT_MEM, PDC_PAT_MEM_PD_READ,
1783		__pa(&pdc_result), __pa(pdt_entries_ptr),
1784		count, offset);
1785
1786	if (retval == PDC_OK) {
1787		entries = min(pdc_result[0], count);
1788		pret->actual_count_bytes = entries;
1789		pret->pdt_entries = entries / sizeof(unsigned long);
1790	}
1791
1792	spin_unlock_irqrestore(&pdc_lock, flags);
1793
1794	return retval;
1795}
1796
1797/**
1798 * pdc_pat_mem_get_dimm_phys_location - Get physical DIMM slot via PAT firmware
1799 * @pret: ptr to hold returned information
1800 * @phys_addr: physical address to examine
1801 *
1802 */
1803int pdc_pat_mem_get_dimm_phys_location(
1804		struct pdc_pat_mem_phys_mem_location *pret,
1805		unsigned long phys_addr)
1806{
1807	int retval;
1808	unsigned long flags;
1809
1810	spin_lock_irqsave(&pdc_lock, flags);
1811	retval = mem_pdc_call(PDC_PAT_MEM, PDC_PAT_MEM_ADDRESS,
1812		__pa(&pdc_result), phys_addr);
1813
1814	if (retval == PDC_OK)
1815		memcpy(pret, &pdc_result, sizeof(*pret));
1816
1817	spin_unlock_irqrestore(&pdc_lock, flags);
1818
1819	return retval;
1820}
1821#endif /* CONFIG_64BIT */
1822#endif /* defined(BOOTLOADER) */
1823
1824
1825/***************** 32-bit real-mode calls ***********/
1826/* The struct below is used
1827 * to overlay real_stack (real2.S), preparing a 32-bit call frame.
1828 * real32_call_asm() then uses this stack in narrow real mode
1829 */
1830
1831struct narrow_stack {
1832	/* use int, not long which is 64 bits */
1833	unsigned int arg13;
1834	unsigned int arg12;
1835	unsigned int arg11;
1836	unsigned int arg10;
1837	unsigned int arg9;
1838	unsigned int arg8;
1839	unsigned int arg7;
1840	unsigned int arg6;
1841	unsigned int arg5;
1842	unsigned int arg4;
1843	unsigned int arg3;
1844	unsigned int arg2;
1845	unsigned int arg1;
1846	unsigned int arg0;
1847	unsigned int frame_marker[8];
1848	unsigned int sp;
1849	/* in reality, there's nearly 8k of stack after this */
1850};
1851
1852long real32_call(unsigned long fn, ...)
1853{
1854	va_list args;
1855	extern struct narrow_stack real_stack;
1856	extern unsigned long real32_call_asm(unsigned int *,
1857					     unsigned int *,
1858					     unsigned int);
1859
1860	va_start(args, fn);
1861	real_stack.arg0 = va_arg(args, unsigned int);
1862	real_stack.arg1 = va_arg(args, unsigned int);
1863	real_stack.arg2 = va_arg(args, unsigned int);
1864	real_stack.arg3 = va_arg(args, unsigned int);
1865	real_stack.arg4 = va_arg(args, unsigned int);
1866	real_stack.arg5 = va_arg(args, unsigned int);
1867	real_stack.arg6 = va_arg(args, unsigned int);
1868	real_stack.arg7 = va_arg(args, unsigned int);
1869	real_stack.arg8 = va_arg(args, unsigned int);
1870	real_stack.arg9 = va_arg(args, unsigned int);
1871	real_stack.arg10 = va_arg(args, unsigned int);
1872	real_stack.arg11 = va_arg(args, unsigned int);
1873	real_stack.arg12 = va_arg(args, unsigned int);
1874	real_stack.arg13 = va_arg(args, unsigned int);
1875	va_end(args);
1876
1877	return real32_call_asm(&real_stack.sp, &real_stack.arg0, fn);
1878}
1879
1880#ifdef CONFIG_64BIT
1881/***************** 64-bit real-mode calls ***********/
1882
1883struct wide_stack {
1884	unsigned long arg0;
1885	unsigned long arg1;
1886	unsigned long arg2;
1887	unsigned long arg3;
1888	unsigned long arg4;
1889	unsigned long arg5;
1890	unsigned long arg6;
1891	unsigned long arg7;
1892	unsigned long arg8;
1893	unsigned long arg9;
1894	unsigned long arg10;
1895	unsigned long arg11;
1896	unsigned long arg12;
1897	unsigned long arg13;
1898	unsigned long frame_marker[2];	/* rp, previous sp */
1899	unsigned long sp;
1900	/* in reality, there's nearly 8k of stack after this */
1901};
1902
1903long real64_call(unsigned long fn, ...)
1904{
1905	va_list args;
1906	extern struct wide_stack real64_stack;
1907	extern unsigned long real64_call_asm(unsigned long *,
1908					     unsigned long *,
1909					     unsigned long);
1910
1911	va_start(args, fn);
1912	real64_stack.arg0 = va_arg(args, unsigned long);
1913	real64_stack.arg1 = va_arg(args, unsigned long);
1914	real64_stack.arg2 = va_arg(args, unsigned long);
1915	real64_stack.arg3 = va_arg(args, unsigned long);
1916	real64_stack.arg4 = va_arg(args, unsigned long);
1917	real64_stack.arg5 = va_arg(args, unsigned long);
1918	real64_stack.arg6 = va_arg(args, unsigned long);
1919	real64_stack.arg7 = va_arg(args, unsigned long);
1920	real64_stack.arg8 = va_arg(args, unsigned long);
1921	real64_stack.arg9 = va_arg(args, unsigned long);
1922	real64_stack.arg10 = va_arg(args, unsigned long);
1923	real64_stack.arg11 = va_arg(args, unsigned long);
1924	real64_stack.arg12 = va_arg(args, unsigned long);
1925	real64_stack.arg13 = va_arg(args, unsigned long);
1926	va_end(args);
1927
1928	return real64_call_asm(&real64_stack.sp, &real64_stack.arg0, fn);
1929}
1930
1931#endif /* CONFIG_64BIT */
1932