1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * BIOS run time interface routines.
4 *
5 * (C) Copyright 2020 Hewlett Packard Enterprise Development LP
6 * Copyright (C) 2007-2017 Silicon Graphics, Inc. All rights reserved.
7 * Copyright (c) Russ Anderson <rja@sgi.com>
8 */
9
10#include <linux/efi.h>
11#include <linux/export.h>
12#include <linux/slab.h>
13#include <asm/efi.h>
14#include <linux/io.h>
15#include <asm/pgalloc.h>
16#include <asm/uv/bios.h>
17#include <asm/uv/uv_hub.h>
18
19unsigned long uv_systab_phys __ro_after_init = EFI_INVALID_TABLE_ADDR;
20
21struct uv_systab *uv_systab;
22
23static s64 __uv_bios_call(enum uv_bios_cmd which, u64 a1, u64 a2, u64 a3,
24			u64 a4, u64 a5)
25{
26	struct uv_systab *tab = uv_systab;
27	s64 ret;
28
29	if (!tab || !tab->function)
30		/*
31		 * BIOS does not support UV systab
32		 */
33		return BIOS_STATUS_UNIMPLEMENTED;
34
35	ret = efi_call_virt_pointer(tab, function, (u64)which, a1, a2, a3, a4, a5);
36
37	return ret;
38}
39
40static s64 uv_bios_call(enum uv_bios_cmd which, u64 a1, u64 a2, u64 a3, u64 a4,
41		u64 a5)
42{
43	s64 ret;
44
45	if (down_interruptible(&__efi_uv_runtime_lock))
46		return BIOS_STATUS_ABORT;
47
48	ret = __uv_bios_call(which, a1, a2, a3, a4, a5);
49	up(&__efi_uv_runtime_lock);
50
51	return ret;
52}
53
54static s64 uv_bios_call_irqsave(enum uv_bios_cmd which, u64 a1, u64 a2, u64 a3,
55		u64 a4, u64 a5)
56{
57	unsigned long bios_flags;
58	s64 ret;
59
60	if (down_interruptible(&__efi_uv_runtime_lock))
61		return BIOS_STATUS_ABORT;
62
63	local_irq_save(bios_flags);
64	ret = __uv_bios_call(which, a1, a2, a3, a4, a5);
65	local_irq_restore(bios_flags);
66
67	up(&__efi_uv_runtime_lock);
68
69	return ret;
70}
71
72long sn_partition_id;
73EXPORT_SYMBOL_GPL(sn_partition_id);
74long sn_coherency_id;
75long sn_region_size;
76EXPORT_SYMBOL_GPL(sn_region_size);
77long system_serial_number;
78int uv_type;
79
80s64 uv_bios_get_sn_info(int fc, int *uvtype, long *partid, long *coher,
81		long *region, long *ssn)
82{
83	s64 ret;
84	u64 v0, v1;
85	union partition_info_u part;
86
87	ret = uv_bios_call_irqsave(UV_BIOS_GET_SN_INFO, fc,
88				(u64)(&v0), (u64)(&v1), 0, 0);
89	if (ret != BIOS_STATUS_SUCCESS)
90		return ret;
91
92	part.val = v0;
93	if (uvtype)
94		*uvtype = part.hub_version;
95	if (partid)
96		*partid = part.partition_id;
97	if (coher)
98		*coher = part.coherence_id;
99	if (region)
100		*region = part.region_size;
101	if (ssn)
102		*ssn = v1;
103	return ret;
104}
105
106int
107uv_bios_mq_watchlist_alloc(unsigned long addr, unsigned int mq_size,
108			   unsigned long *intr_mmr_offset)
109{
110	u64 watchlist;
111	s64 ret;
112
113	/*
114	 * bios returns watchlist number or negative error number.
115	 */
116	ret = (int)uv_bios_call_irqsave(UV_BIOS_WATCHLIST_ALLOC, addr,
117			mq_size, (u64)intr_mmr_offset,
118			(u64)&watchlist, 0);
119	if (ret < BIOS_STATUS_SUCCESS)
120		return ret;
121
122	return watchlist;
123}
124EXPORT_SYMBOL_GPL(uv_bios_mq_watchlist_alloc);
125
126int
127uv_bios_mq_watchlist_free(int blade, int watchlist_num)
128{
129	return (int)uv_bios_call_irqsave(UV_BIOS_WATCHLIST_FREE,
130				blade, watchlist_num, 0, 0, 0);
131}
132EXPORT_SYMBOL_GPL(uv_bios_mq_watchlist_free);
133
134s64
135uv_bios_change_memprotect(u64 paddr, u64 len, enum uv_memprotect perms)
136{
137	return uv_bios_call_irqsave(UV_BIOS_MEMPROTECT, paddr, len,
138					perms, 0, 0);
139}
140EXPORT_SYMBOL_GPL(uv_bios_change_memprotect);
141
142s64
143uv_bios_reserved_page_pa(u64 buf, u64 *cookie, u64 *addr, u64 *len)
144{
145	return uv_bios_call_irqsave(UV_BIOS_GET_PARTITION_ADDR, (u64)cookie,
146				    (u64)addr, buf, (u64)len, 0);
147}
148EXPORT_SYMBOL_GPL(uv_bios_reserved_page_pa);
149
150s64 uv_bios_freq_base(u64 clock_type, u64 *ticks_per_second)
151{
152	return uv_bios_call(UV_BIOS_FREQ_BASE, clock_type,
153			   (u64)ticks_per_second, 0, 0, 0);
154}
155
156/*
157 * uv_bios_set_legacy_vga_target - Set Legacy VGA I/O Target
158 * @decode: true to enable target, false to disable target
159 * @domain: PCI domain number
160 * @bus: PCI bus number
161 *
162 * Returns:
163 *    0: Success
164 *    -EINVAL: Invalid domain or bus number
165 *    -ENOSYS: Capability not available
166 *    -EBUSY: Legacy VGA I/O cannot be retargeted at this time
167 */
168int uv_bios_set_legacy_vga_target(bool decode, int domain, int bus)
169{
170	return uv_bios_call(UV_BIOS_SET_LEGACY_VGA_TARGET,
171				(u64)decode, (u64)domain, (u64)bus, 0, 0);
172}
173
174unsigned long get_uv_systab_phys(bool msg)
175{
176	if ((uv_systab_phys == EFI_INVALID_TABLE_ADDR) ||
177	    !uv_systab_phys || efi_runtime_disabled()) {
178		if (msg)
179			pr_crit("UV: UVsystab: missing\n");
180		return 0;
181	}
182	return uv_systab_phys;
183}
184
185int uv_bios_init(void)
186{
187	unsigned long uv_systab_phys_addr;
188
189	uv_systab = NULL;
190	uv_systab_phys_addr = get_uv_systab_phys(1);
191	if (!uv_systab_phys_addr)
192		return -EEXIST;
193
194	uv_systab = ioremap(uv_systab_phys_addr, sizeof(struct uv_systab));
195	if (!uv_systab || strncmp(uv_systab->signature, UV_SYSTAB_SIG, 4)) {
196		pr_err("UV: UVsystab: bad signature!\n");
197		iounmap(uv_systab);
198		return -EINVAL;
199	}
200
201	/* Starting with UV4 the UV systab size is variable */
202	if (uv_systab->revision >= UV_SYSTAB_VERSION_UV4) {
203		int size = uv_systab->size;
204
205		iounmap(uv_systab);
206		uv_systab = ioremap(uv_systab_phys_addr, size);
207		if (!uv_systab) {
208			pr_err("UV: UVsystab: ioremap(%d) failed!\n", size);
209			return -EFAULT;
210		}
211	}
212	pr_info("UV: UVsystab: Revision:%x\n", uv_systab->revision);
213	return 0;
214}
215