xref: /openbmc/linux/arch/x86/include/asm/uv/uv_hub.h (revision 5f40f7d9)
1bb898558SAl Viro /*
2bb898558SAl Viro  * This file is subject to the terms and conditions of the GNU General Public
3bb898558SAl Viro  * License.  See the file "COPYING" in the main directory of this archive
4bb898558SAl Viro  * for more details.
5bb898558SAl Viro  *
6bb898558SAl Viro  * SGI UV architectural definitions
7bb898558SAl Viro  *
85f40f7d9SDimitri Sivanich  * Copyright (C) 2007-2014 Silicon Graphics, Inc. All rights reserved.
9bb898558SAl Viro  */
10bb898558SAl Viro 
1105e4d316SH. Peter Anvin #ifndef _ASM_X86_UV_UV_HUB_H
1205e4d316SH. Peter Anvin #define _ASM_X86_UV_UV_HUB_H
13bb898558SAl Viro 
14bc5d9940SJack Steiner #ifdef CONFIG_X86_64
15bb898558SAl Viro #include <linux/numa.h>
16bb898558SAl Viro #include <linux/percpu.h>
17c08b6accSMike Travis #include <linux/timer.h>
188dc579e8SJack Steiner #include <linux/io.h>
19bb898558SAl Viro #include <asm/types.h>
20bb898558SAl Viro #include <asm/percpu.h>
2166666e50SJack Steiner #include <asm/uv/uv_mmrs.h>
2202dd0a06SRobin Holt #include <asm/irq_vectors.h>
2302dd0a06SRobin Holt #include <asm/io_apic.h>
24bb898558SAl Viro 
25bb898558SAl Viro 
26bb898558SAl Viro /*
27bb898558SAl Viro  * Addressing Terminology
28bb898558SAl Viro  *
29bb898558SAl Viro  *	M       - The low M bits of a physical address represent the offset
30bb898558SAl Viro  *		  into the blade local memory. RAM memory on a blade is physically
31bb898558SAl Viro  *		  contiguous (although various IO spaces may punch holes in
32bb898558SAl Viro  *		  it)..
33bb898558SAl Viro  *
34bb898558SAl Viro  *	N	- Number of bits in the node portion of a socket physical
35bb898558SAl Viro  *		  address.
36bb898558SAl Viro  *
37bb898558SAl Viro  *	NASID   - network ID of a router, Mbrick or Cbrick. Nasid values of
38bb898558SAl Viro  *		  routers always have low bit of 1, C/MBricks have low bit
39bb898558SAl Viro  *		  equal to 0. Most addressing macros that target UV hub chips
40bb898558SAl Viro  *		  right shift the NASID by 1 to exclude the always-zero bit.
41bb898558SAl Viro  *		  NASIDs contain up to 15 bits.
42bb898558SAl Viro  *
43bb898558SAl Viro  *	GNODE   - NASID right shifted by 1 bit. Most mmrs contain gnodes instead
44bb898558SAl Viro  *		  of nasids.
45bb898558SAl Viro  *
46bb898558SAl Viro  *	PNODE   - the low N bits of the GNODE. The PNODE is the most useful variant
47bb898558SAl Viro  *		  of the nasid for socket usage.
48bb898558SAl Viro  *
496a469e46SJack Steiner  *	GPA	- (global physical address) a socket physical address converted
506a469e46SJack Steiner  *		  so that it can be used by the GRU as a global address. Socket
516a469e46SJack Steiner  *		  physical addresses 1) need additional NASID (node) bits added
526a469e46SJack Steiner  *		  to the high end of the address, and 2) unaliased if the
536a469e46SJack Steiner  *		  partition does not have a physical address 0. In addition, on
546a469e46SJack Steiner  *		  UV2 rev 1, GPAs need the gnode left shifted to bits 39 or 40.
556a469e46SJack Steiner  *
56bb898558SAl Viro  *
57bb898558SAl Viro  *  NumaLink Global Physical Address Format:
58bb898558SAl Viro  *  +--------------------------------+---------------------+
59bb898558SAl Viro  *  |00..000|      GNODE             |      NodeOffset     |
60bb898558SAl Viro  *  +--------------------------------+---------------------+
61bb898558SAl Viro  *          |<-------53 - M bits --->|<--------M bits ----->
62bb898558SAl Viro  *
63bb898558SAl Viro  *	M - number of node offset bits (35 .. 40)
64bb898558SAl Viro  *
65bb898558SAl Viro  *
66bb898558SAl Viro  *  Memory/UV-HUB Processor Socket Address Format:
67bb898558SAl Viro  *  +----------------+---------------+---------------------+
68bb898558SAl Viro  *  |00..000000000000|   PNODE       |      NodeOffset     |
69bb898558SAl Viro  *  +----------------+---------------+---------------------+
70bb898558SAl Viro  *                   <--- N bits --->|<--------M bits ----->
71bb898558SAl Viro  *
72bb898558SAl Viro  *	M - number of node offset bits (35 .. 40)
73bb898558SAl Viro  *	N - number of PNODE bits (0 .. 10)
74bb898558SAl Viro  *
75bb898558SAl Viro  *		Note: M + N cannot currently exceed 44 (x86_64) or 46 (IA64).
76bb898558SAl Viro  *		The actual values are configuration dependent and are set at
77bb898558SAl Viro  *		boot time. M & N values are set by the hardware/BIOS at boot.
78bb898558SAl Viro  *
79bb898558SAl Viro  *
80bb898558SAl Viro  * APICID format
81bb898558SAl Viro  *	NOTE!!!!!! This is the current format of the APICID. However, code
82bb898558SAl Viro  *	should assume that this will change in the future. Use functions
83bb898558SAl Viro  *	in this file for all APICID bit manipulations and conversion.
84bb898558SAl Viro  *
85bb898558SAl Viro  *		1111110000000000
86bb898558SAl Viro  *		5432109876543210
872a919596SJack Steiner  *		pppppppppplc0cch	Nehalem-EX (12 bits in hdw reg)
882a919596SJack Steiner  *		ppppppppplcc0cch	Westmere-EX (12 bits in hdw reg)
892a919596SJack Steiner  *		pppppppppppcccch	SandyBridge (15 bits in hdw reg)
90bb898558SAl Viro  *		sssssssssss
91bb898558SAl Viro  *
92bb898558SAl Viro  *			p  = pnode bits
93bb898558SAl Viro  *			l =  socket number on board
94bb898558SAl Viro  *			c  = core
95bb898558SAl Viro  *			h  = hyperthread
96bb898558SAl Viro  *			s  = bits that are in the SOCKET_ID CSR
97bb898558SAl Viro  *
982a919596SJack Steiner  *	Note: Processor may support fewer bits in the APICID register. The ACPI
99bb898558SAl Viro  *	      tables hold all 16 bits. Software needs to be aware of this.
100bb898558SAl Viro  *
101bb898558SAl Viro  *	      Unless otherwise specified, all references to APICID refer to
102bb898558SAl Viro  *	      the FULL value contained in ACPI tables, not the subset in the
103bb898558SAl Viro  *	      processor APICID register.
104bb898558SAl Viro  */
105bb898558SAl Viro 
106bb898558SAl Viro 
107bb898558SAl Viro /*
108bb898558SAl Viro  * Maximum number of bricks in all partitions and in all coherency domains.
109bb898558SAl Viro  * This is the total number of bricks accessible in the numalink fabric. It
110bb898558SAl Viro  * includes all C & M bricks. Routers are NOT included.
111bb898558SAl Viro  *
112bb898558SAl Viro  * This value is also the value of the maximum number of non-router NASIDs
113bb898558SAl Viro  * in the numalink fabric.
114bb898558SAl Viro  *
115bb898558SAl Viro  * NOTE: a brick may contain 1 or 2 OS nodes. Don't get these confused.
116bb898558SAl Viro  */
117bb898558SAl Viro #define UV_MAX_NUMALINK_BLADES	16384
118bb898558SAl Viro 
119bb898558SAl Viro /*
120bb898558SAl Viro  * Maximum number of C/Mbricks within a software SSI (hardware may support
121bb898558SAl Viro  * more).
122bb898558SAl Viro  */
123bb898558SAl Viro #define UV_MAX_SSI_BLADES	256
124bb898558SAl Viro 
125bb898558SAl Viro /*
126bb898558SAl Viro  * The largest possible NASID of a C or M brick (+ 2)
127bb898558SAl Viro  */
1281d21e6e3SRobin Holt #define UV_MAX_NASID_VALUE	(UV_MAX_NUMALINK_BLADES * 2)
129bb898558SAl Viro 
1307f1baa06SMike Travis struct uv_scir_s {
1317f1baa06SMike Travis 	struct timer_list timer;
1327f1baa06SMike Travis 	unsigned long	offset;
1337f1baa06SMike Travis 	unsigned long	last;
1347f1baa06SMike Travis 	unsigned long	idle_on;
1357f1baa06SMike Travis 	unsigned long	idle_off;
1367f1baa06SMike Travis 	unsigned char	state;
1377f1baa06SMike Travis 	unsigned char	enabled;
1387f1baa06SMike Travis };
1397f1baa06SMike Travis 
140bb898558SAl Viro /*
141bb898558SAl Viro  * The following defines attributes of the HUB chip. These attributes are
142bb898558SAl Viro  * frequently referenced and are kept in the per-cpu data areas of each cpu.
143bb898558SAl Viro  * They are kept together in a struct to minimize cache misses.
144bb898558SAl Viro  */
145bb898558SAl Viro struct uv_hub_info_s {
146bb898558SAl Viro 	unsigned long		global_mmr_base;
147bb898558SAl Viro 	unsigned long		gpa_mask;
148c4ed3f04SJack Steiner 	unsigned int		gnode_extra;
1492a919596SJack Steiner 	unsigned char		hub_revision;
1502a919596SJack Steiner 	unsigned char		apic_pnode_shift;
1516a469e46SJack Steiner 	unsigned char		m_shift;
1526a469e46SJack Steiner 	unsigned char		n_lshift;
153bb898558SAl Viro 	unsigned long		gnode_upper;
154bb898558SAl Viro 	unsigned long		lowmem_remap_top;
155bb898558SAl Viro 	unsigned long		lowmem_remap_base;
156bb898558SAl Viro 	unsigned short		pnode;
157bb898558SAl Viro 	unsigned short		pnode_mask;
158bb898558SAl Viro 	unsigned short		coherency_domain_number;
159bb898558SAl Viro 	unsigned short		numa_blade_id;
160bb898558SAl Viro 	unsigned char		blade_processor_id;
161bb898558SAl Viro 	unsigned char		m_val;
162bb898558SAl Viro 	unsigned char		n_val;
1637f1baa06SMike Travis 	struct uv_scir_s	scir;
164bb898558SAl Viro };
1657f1baa06SMike Travis 
166bb898558SAl Viro DECLARE_PER_CPU(struct uv_hub_info_s, __uv_hub_info);
167bb898558SAl Viro #define uv_hub_info		(&__get_cpu_var(__uv_hub_info))
168bb898558SAl Viro #define uv_cpu_hub_info(cpu)	(&per_cpu(__uv_hub_info, cpu))
169bb898558SAl Viro 
1702a919596SJack Steiner /*
1712a919596SJack Steiner  * Hub revisions less than UV2_HUB_REVISION_BASE are UV1 hubs. All UV2
1722a919596SJack Steiner  * hubs have revision numbers greater than or equal to UV2_HUB_REVISION_BASE.
1732a919596SJack Steiner  * This is a software convention - NOT the hardware revision numbers in
1742a919596SJack Steiner  * the hub chip.
1752a919596SJack Steiner  */
1762a919596SJack Steiner #define UV1_HUB_REVISION_BASE		1
1772a919596SJack Steiner #define UV2_HUB_REVISION_BASE		3
1786edbd471SMike Travis #define UV3_HUB_REVISION_BASE		5
1792a919596SJack Steiner 
1802a919596SJack Steiner static inline int is_uv1_hub(void)
1812a919596SJack Steiner {
1822a919596SJack Steiner 	return uv_hub_info->hub_revision < UV2_HUB_REVISION_BASE;
1832a919596SJack Steiner }
1842a919596SJack Steiner 
1852a919596SJack Steiner static inline int is_uv2_hub(void)
1862a919596SJack Steiner {
1876edbd471SMike Travis 	return ((uv_hub_info->hub_revision >= UV2_HUB_REVISION_BASE) &&
1886edbd471SMike Travis 		(uv_hub_info->hub_revision < UV3_HUB_REVISION_BASE));
1896edbd471SMike Travis }
1906edbd471SMike Travis 
1916edbd471SMike Travis static inline int is_uv3_hub(void)
1926edbd471SMike Travis {
1936edbd471SMike Travis 	return uv_hub_info->hub_revision >= UV3_HUB_REVISION_BASE;
1946edbd471SMike Travis }
1956edbd471SMike Travis 
1966edbd471SMike Travis static inline int is_uv_hub(void)
1976edbd471SMike Travis {
1986edbd471SMike Travis 	return uv_hub_info->hub_revision;
1996edbd471SMike Travis }
2006edbd471SMike Travis 
2016edbd471SMike Travis /* code common to uv2 and uv3 only */
2026edbd471SMike Travis static inline int is_uvx_hub(void)
2036edbd471SMike Travis {
2042a919596SJack Steiner 	return uv_hub_info->hub_revision >= UV2_HUB_REVISION_BASE;
2052a919596SJack Steiner }
2062a919596SJack Steiner 
207c8f730b1SRuss Anderson union uvh_apicid {
208c8f730b1SRuss Anderson     unsigned long       v;
209c8f730b1SRuss Anderson     struct uvh_apicid_s {
210c8f730b1SRuss Anderson         unsigned long   local_apic_mask  : 24;
211c8f730b1SRuss Anderson         unsigned long   local_apic_shift :  5;
212c8f730b1SRuss Anderson         unsigned long   unused1          :  3;
213c8f730b1SRuss Anderson         unsigned long   pnode_mask       : 24;
214c8f730b1SRuss Anderson         unsigned long   pnode_shift      :  5;
215c8f730b1SRuss Anderson         unsigned long   unused2          :  3;
216c8f730b1SRuss Anderson     } s;
217c8f730b1SRuss Anderson };
218c8f730b1SRuss Anderson 
219bb898558SAl Viro /*
220bb898558SAl Viro  * Local & Global MMR space macros.
221bb898558SAl Viro  *	Note: macros are intended to be used ONLY by inline functions
222bb898558SAl Viro  *	in this file - not by other kernel code.
223bb898558SAl Viro  *		n -  NASID (full 15-bit global nasid)
224bb898558SAl Viro  *		g -  GNODE (full 15-bit global nasid, right shifted 1)
225bb898558SAl Viro  *		p -  PNODE (local part of nsids, right shifted 1)
226bb898558SAl Viro  */
227bb898558SAl Viro #define UV_NASID_TO_PNODE(n)		(((n) >> 1) & uv_hub_info->pnode_mask)
228c4ed3f04SJack Steiner #define UV_PNODE_TO_GNODE(p)		((p) |uv_hub_info->gnode_extra)
229c4ed3f04SJack Steiner #define UV_PNODE_TO_NASID(p)		(UV_PNODE_TO_GNODE(p) << 1)
230bb898558SAl Viro 
2312a919596SJack Steiner #define UV1_LOCAL_MMR_BASE		0xf4000000UL
2322a919596SJack Steiner #define UV1_GLOBAL_MMR32_BASE		0xf8000000UL
2332a919596SJack Steiner #define UV1_LOCAL_MMR_SIZE		(64UL * 1024 * 1024)
2342a919596SJack Steiner #define UV1_GLOBAL_MMR32_SIZE		(64UL * 1024 * 1024)
2352a919596SJack Steiner 
2362a919596SJack Steiner #define UV2_LOCAL_MMR_BASE		0xfa000000UL
2372a919596SJack Steiner #define UV2_GLOBAL_MMR32_BASE		0xfc000000UL
2382a919596SJack Steiner #define UV2_LOCAL_MMR_SIZE		(32UL * 1024 * 1024)
2392a919596SJack Steiner #define UV2_GLOBAL_MMR32_SIZE		(32UL * 1024 * 1024)
2402a919596SJack Steiner 
2416edbd471SMike Travis #define UV3_LOCAL_MMR_BASE		0xfa000000UL
2426edbd471SMike Travis #define UV3_GLOBAL_MMR32_BASE		0xfc000000UL
2436edbd471SMike Travis #define UV3_LOCAL_MMR_SIZE		(32UL * 1024 * 1024)
2446edbd471SMike Travis #define UV3_GLOBAL_MMR32_SIZE		(32UL * 1024 * 1024)
2456edbd471SMike Travis 
2466edbd471SMike Travis #define UV_LOCAL_MMR_BASE		(is_uv1_hub() ? UV1_LOCAL_MMR_BASE : \
2476edbd471SMike Travis 					(is_uv2_hub() ? UV2_LOCAL_MMR_BASE : \
2486edbd471SMike Travis 							UV3_LOCAL_MMR_BASE))
2496edbd471SMike Travis #define UV_GLOBAL_MMR32_BASE		(is_uv1_hub() ? UV1_GLOBAL_MMR32_BASE :\
2506edbd471SMike Travis 					(is_uv2_hub() ? UV2_GLOBAL_MMR32_BASE :\
2516edbd471SMike Travis 							UV3_GLOBAL_MMR32_BASE))
2522a919596SJack Steiner #define UV_LOCAL_MMR_SIZE		(is_uv1_hub() ? UV1_LOCAL_MMR_SIZE : \
2536edbd471SMike Travis 					(is_uv2_hub() ? UV2_LOCAL_MMR_SIZE : \
2546edbd471SMike Travis 							UV3_LOCAL_MMR_SIZE))
2552a919596SJack Steiner #define UV_GLOBAL_MMR32_SIZE		(is_uv1_hub() ? UV1_GLOBAL_MMR32_SIZE :\
2566edbd471SMike Travis 					(is_uv2_hub() ? UV2_GLOBAL_MMR32_SIZE :\
2576edbd471SMike Travis 							UV3_GLOBAL_MMR32_SIZE))
258bb898558SAl Viro #define UV_GLOBAL_MMR64_BASE		(uv_hub_info->global_mmr_base)
259bb898558SAl Viro 
26056abcf24SJack Steiner #define UV_GLOBAL_GRU_MMR_BASE		0x4000000
26156abcf24SJack Steiner 
262bb898558SAl Viro #define UV_GLOBAL_MMR32_PNODE_SHIFT	15
263bb898558SAl Viro #define UV_GLOBAL_MMR64_PNODE_SHIFT	26
264bb898558SAl Viro 
265bb898558SAl Viro #define UV_GLOBAL_MMR32_PNODE_BITS(p)	((p) << (UV_GLOBAL_MMR32_PNODE_SHIFT))
266bb898558SAl Viro 
267bb898558SAl Viro #define UV_GLOBAL_MMR64_PNODE_BITS(p)					\
26867e83f30SJack Steiner 	(((unsigned long)(p)) << UV_GLOBAL_MMR64_PNODE_SHIFT)
269bb898558SAl Viro 
270c8f730b1SRuss Anderson #define UVH_APICID		0x002D0E00L
271bb898558SAl Viro #define UV_APIC_PNODE_SHIFT	6
272bb898558SAl Viro 
2738191c9f6SDimitri Sivanich #define UV_APICID_HIBIT_MASK	0xffff0000
2748191c9f6SDimitri Sivanich 
2757f1baa06SMike Travis /* Local Bus from cpu's perspective */
2767f1baa06SMike Travis #define LOCAL_BUS_BASE		0x1c00000
2777f1baa06SMike Travis #define LOCAL_BUS_SIZE		(4 * 1024 * 1024)
2787f1baa06SMike Travis 
2797f1baa06SMike Travis /*
2807f1baa06SMike Travis  * System Controller Interface Reg
2817f1baa06SMike Travis  *
2827f1baa06SMike Travis  * Note there are NO leds on a UV system.  This register is only
2837f1baa06SMike Travis  * used by the system controller to monitor system-wide operation.
2847f1baa06SMike Travis  * There are 64 regs per node.  With Nahelem cpus (2 cores per node,
2857f1baa06SMike Travis  * 8 cpus per core, 2 threads per cpu) there are 32 cpu threads on
2867f1baa06SMike Travis  * a node.
2877f1baa06SMike Travis  *
2887f1baa06SMike Travis  * The window is located at top of ACPI MMR space
2897f1baa06SMike Travis  */
2907f1baa06SMike Travis #define SCIR_WINDOW_COUNT	64
2917f1baa06SMike Travis #define SCIR_LOCAL_MMR_BASE	(LOCAL_BUS_BASE + \
2927f1baa06SMike Travis 				 LOCAL_BUS_SIZE - \
2937f1baa06SMike Travis 				 SCIR_WINDOW_COUNT)
2947f1baa06SMike Travis 
2957f1baa06SMike Travis #define SCIR_CPU_HEARTBEAT	0x01	/* timer interrupt */
2967f1baa06SMike Travis #define SCIR_CPU_ACTIVITY	0x02	/* not idle */
2977f1baa06SMike Travis #define SCIR_CPU_HB_INTERVAL	(HZ)	/* once per second */
2987f1baa06SMike Travis 
2998661984fSDimitri Sivanich /* Loop through all installed blades */
3008661984fSDimitri Sivanich #define for_each_possible_blade(bid)		\
3018661984fSDimitri Sivanich 	for ((bid) = 0; (bid) < uv_num_possible_blades(); (bid)++)
3028661984fSDimitri Sivanich 
303bb898558SAl Viro /*
304bb898558SAl Viro  * Macros for converting between kernel virtual addresses, socket local physical
305bb898558SAl Viro  * addresses, and UV global physical addresses.
306bb898558SAl Viro  *	Note: use the standard __pa() & __va() macros for converting
307bb898558SAl Viro  *	      between socket virtual and socket physical addresses.
308bb898558SAl Viro  */
309bb898558SAl Viro 
310bb898558SAl Viro /* socket phys RAM --> UV global physical address */
311bb898558SAl Viro static inline unsigned long uv_soc_phys_ram_to_gpa(unsigned long paddr)
312bb898558SAl Viro {
313bb898558SAl Viro 	if (paddr < uv_hub_info->lowmem_remap_top)
314189f67c4SJack Steiner 		paddr |= uv_hub_info->lowmem_remap_base;
3156a469e46SJack Steiner 	paddr |= uv_hub_info->gnode_upper;
3166a469e46SJack Steiner 	paddr = ((paddr << uv_hub_info->m_shift) >> uv_hub_info->m_shift) |
3176a469e46SJack Steiner 		((paddr >> uv_hub_info->m_val) << uv_hub_info->n_lshift);
3186a469e46SJack Steiner 	return paddr;
319bb898558SAl Viro }
320bb898558SAl Viro 
321bb898558SAl Viro 
322bb898558SAl Viro /* socket virtual --> UV global physical address */
323bb898558SAl Viro static inline unsigned long uv_gpa(void *v)
324bb898558SAl Viro {
325189f67c4SJack Steiner 	return uv_soc_phys_ram_to_gpa(__pa(v));
326bb898558SAl Viro }
327bb898558SAl Viro 
328fae419f2SRobin Holt /* Top two bits indicate the requested address is in MMR space.  */
329fae419f2SRobin Holt static inline int
330fae419f2SRobin Holt uv_gpa_in_mmr_space(unsigned long gpa)
331fae419f2SRobin Holt {
332fae419f2SRobin Holt 	return (gpa >> 62) == 0x3UL;
333fae419f2SRobin Holt }
334fae419f2SRobin Holt 
335729d69e6SRobin Holt /* UV global physical address --> socket phys RAM */
336729d69e6SRobin Holt static inline unsigned long uv_gpa_to_soc_phys_ram(unsigned long gpa)
337729d69e6SRobin Holt {
3385a51467bSRuss Anderson 	unsigned long paddr;
339729d69e6SRobin Holt 	unsigned long remap_base = uv_hub_info->lowmem_remap_base;
340729d69e6SRobin Holt 	unsigned long remap_top =  uv_hub_info->lowmem_remap_top;
341729d69e6SRobin Holt 
3426a469e46SJack Steiner 	gpa = ((gpa << uv_hub_info->m_shift) >> uv_hub_info->m_shift) |
3436a469e46SJack Steiner 		((gpa >> uv_hub_info->n_lshift) << uv_hub_info->m_val);
3445a51467bSRuss Anderson 	paddr = gpa & uv_hub_info->gpa_mask;
345729d69e6SRobin Holt 	if (paddr >= remap_base && paddr < remap_base + remap_top)
346729d69e6SRobin Holt 		paddr -= remap_base;
347729d69e6SRobin Holt 	return paddr;
348729d69e6SRobin Holt }
349729d69e6SRobin Holt 
350729d69e6SRobin Holt 
3516a469e46SJack Steiner /* gpa -> pnode */
3521d21e6e3SRobin Holt static inline unsigned long uv_gpa_to_gnode(unsigned long gpa)
3531d21e6e3SRobin Holt {
3546a469e46SJack Steiner 	return gpa >> uv_hub_info->n_lshift;
3551d21e6e3SRobin Holt }
3561d21e6e3SRobin Holt 
3571d21e6e3SRobin Holt /* gpa -> pnode */
3581d21e6e3SRobin Holt static inline int uv_gpa_to_pnode(unsigned long gpa)
3591d21e6e3SRobin Holt {
3601d21e6e3SRobin Holt 	unsigned long n_mask = (1UL << uv_hub_info->n_val) - 1;
3611d21e6e3SRobin Holt 
3621d21e6e3SRobin Holt 	return uv_gpa_to_gnode(gpa) & n_mask;
3631d21e6e3SRobin Holt }
3641d21e6e3SRobin Holt 
3656a469e46SJack Steiner /* gpa -> node offset*/
3666a469e46SJack Steiner static inline unsigned long uv_gpa_to_offset(unsigned long gpa)
3676a469e46SJack Steiner {
3686a469e46SJack Steiner 	return (gpa << uv_hub_info->m_shift) >> uv_hub_info->m_shift;
3696a469e46SJack Steiner }
3706a469e46SJack Steiner 
371bb898558SAl Viro /* pnode, offset --> socket virtual */
372bb898558SAl Viro static inline void *uv_pnode_offset_to_vaddr(int pnode, unsigned long offset)
373bb898558SAl Viro {
374bb898558SAl Viro 	return __va(((unsigned long)pnode << uv_hub_info->m_val) | offset);
375bb898558SAl Viro }
376bb898558SAl Viro 
377bb898558SAl Viro 
378bb898558SAl Viro /*
379bb898558SAl Viro  * Extract a PNODE from an APICID (full apicid, not processor subset)
380bb898558SAl Viro  */
381bb898558SAl Viro static inline int uv_apicid_to_pnode(int apicid)
382bb898558SAl Viro {
383c8f730b1SRuss Anderson 	return (apicid >> uv_hub_info->apic_pnode_shift);
384bb898558SAl Viro }
385bb898558SAl Viro 
386bb898558SAl Viro /*
3872a919596SJack Steiner  * Convert an apicid to the socket number on the blade
3882a919596SJack Steiner  */
3892a919596SJack Steiner static inline int uv_apicid_to_socket(int apicid)
3902a919596SJack Steiner {
3912a919596SJack Steiner 	if (is_uv1_hub())
3922a919596SJack Steiner 		return (apicid >> (uv_hub_info->apic_pnode_shift - 1)) & 1;
3932a919596SJack Steiner 	else
3942a919596SJack Steiner 		return 0;
3952a919596SJack Steiner }
3962a919596SJack Steiner 
3972a919596SJack Steiner /*
398bb898558SAl Viro  * Access global MMRs using the low memory MMR32 space. This region supports
399bb898558SAl Viro  * faster MMR access but not all MMRs are accessible in this space.
400bb898558SAl Viro  */
40139d30770SMike Travis static inline unsigned long *uv_global_mmr32_address(int pnode, unsigned long offset)
402bb898558SAl Viro {
403bb898558SAl Viro 	return __va(UV_GLOBAL_MMR32_BASE |
404bb898558SAl Viro 		       UV_GLOBAL_MMR32_PNODE_BITS(pnode) | offset);
405bb898558SAl Viro }
406bb898558SAl Viro 
40739d30770SMike Travis static inline void uv_write_global_mmr32(int pnode, unsigned long offset, unsigned long val)
408bb898558SAl Viro {
4098dc579e8SJack Steiner 	writeq(val, uv_global_mmr32_address(pnode, offset));
410bb898558SAl Viro }
411bb898558SAl Viro 
41239d30770SMike Travis static inline unsigned long uv_read_global_mmr32(int pnode, unsigned long offset)
413bb898558SAl Viro {
4148dc579e8SJack Steiner 	return readq(uv_global_mmr32_address(pnode, offset));
415bb898558SAl Viro }
416bb898558SAl Viro 
417bb898558SAl Viro /*
418bb898558SAl Viro  * Access Global MMR space using the MMR space located at the top of physical
419bb898558SAl Viro  * memory.
420bb898558SAl Viro  */
421a289cc7cSRandy Dunlap static inline volatile void __iomem *uv_global_mmr64_address(int pnode, unsigned long offset)
422bb898558SAl Viro {
423bb898558SAl Viro 	return __va(UV_GLOBAL_MMR64_BASE |
424bb898558SAl Viro 		    UV_GLOBAL_MMR64_PNODE_BITS(pnode) | offset);
425bb898558SAl Viro }
426bb898558SAl Viro 
42739d30770SMike Travis static inline void uv_write_global_mmr64(int pnode, unsigned long offset, unsigned long val)
428bb898558SAl Viro {
4298dc579e8SJack Steiner 	writeq(val, uv_global_mmr64_address(pnode, offset));
430bb898558SAl Viro }
431bb898558SAl Viro 
43239d30770SMike Travis static inline unsigned long uv_read_global_mmr64(int pnode, unsigned long offset)
433bb898558SAl Viro {
4348dc579e8SJack Steiner 	return readq(uv_global_mmr64_address(pnode, offset));
435bb898558SAl Viro }
436bb898558SAl Viro 
437bb898558SAl Viro /*
43856abcf24SJack Steiner  * Global MMR space addresses when referenced by the GRU. (GRU does
43956abcf24SJack Steiner  * NOT use socket addressing).
44056abcf24SJack Steiner  */
44156abcf24SJack Steiner static inline unsigned long uv_global_gru_mmr_address(int pnode, unsigned long offset)
44256abcf24SJack Steiner {
443e1e0138dSJack Steiner 	return UV_GLOBAL_GRU_MMR_BASE | offset |
444e1e0138dSJack Steiner 		((unsigned long)pnode << uv_hub_info->m_val);
44556abcf24SJack Steiner }
44656abcf24SJack Steiner 
44739d30770SMike Travis static inline void uv_write_global_mmr8(int pnode, unsigned long offset, unsigned char val)
44839d30770SMike Travis {
44939d30770SMike Travis 	writeb(val, uv_global_mmr64_address(pnode, offset));
45039d30770SMike Travis }
45139d30770SMike Travis 
45239d30770SMike Travis static inline unsigned char uv_read_global_mmr8(int pnode, unsigned long offset)
45339d30770SMike Travis {
45439d30770SMike Travis 	return readb(uv_global_mmr64_address(pnode, offset));
45539d30770SMike Travis }
45639d30770SMike Travis 
45756abcf24SJack Steiner /*
458bb898558SAl Viro  * Access hub local MMRs. Faster than using global space but only local MMRs
459bb898558SAl Viro  * are accessible.
460bb898558SAl Viro  */
461bb898558SAl Viro static inline unsigned long *uv_local_mmr_address(unsigned long offset)
462bb898558SAl Viro {
463bb898558SAl Viro 	return __va(UV_LOCAL_MMR_BASE | offset);
464bb898558SAl Viro }
465bb898558SAl Viro 
466bb898558SAl Viro static inline unsigned long uv_read_local_mmr(unsigned long offset)
467bb898558SAl Viro {
4688dc579e8SJack Steiner 	return readq(uv_local_mmr_address(offset));
469bb898558SAl Viro }
470bb898558SAl Viro 
471bb898558SAl Viro static inline void uv_write_local_mmr(unsigned long offset, unsigned long val)
472bb898558SAl Viro {
4738dc579e8SJack Steiner 	writeq(val, uv_local_mmr_address(offset));
474bb898558SAl Viro }
475bb898558SAl Viro 
4767f1baa06SMike Travis static inline unsigned char uv_read_local_mmr8(unsigned long offset)
4777f1baa06SMike Travis {
4788dc579e8SJack Steiner 	return readb(uv_local_mmr_address(offset));
4797f1baa06SMike Travis }
4807f1baa06SMike Travis 
4817f1baa06SMike Travis static inline void uv_write_local_mmr8(unsigned long offset, unsigned char val)
4827f1baa06SMike Travis {
4838dc579e8SJack Steiner 	writeb(val, uv_local_mmr_address(offset));
4847f1baa06SMike Travis }
4857f1baa06SMike Travis 
486bb898558SAl Viro /*
487bb898558SAl Viro  * Structures and definitions for converting between cpu, node, pnode, and blade
488bb898558SAl Viro  * numbers.
489bb898558SAl Viro  */
490bb898558SAl Viro struct uv_blade_info {
491bb898558SAl Viro 	unsigned short	nr_possible_cpus;
492bb898558SAl Viro 	unsigned short	nr_online_cpus;
493bb898558SAl Viro 	unsigned short	pnode;
4946c7184b7SJack Steiner 	short		memory_nid;
4950d12ef0cSMike Travis 	spinlock_t	nmi_lock;	/* obsolete, see uv_hub_nmi */
4960d12ef0cSMike Travis 	unsigned long	nmi_count;	/* obsolete, see uv_hub_nmi */
497bb898558SAl Viro };
498bb898558SAl Viro extern struct uv_blade_info *uv_blade_info;
499bb898558SAl Viro extern short *uv_node_to_blade;
500bb898558SAl Viro extern short *uv_cpu_to_blade;
501bb898558SAl Viro extern short uv_possible_blades;
502bb898558SAl Viro 
503bb898558SAl Viro /* Blade-local cpu number of current cpu. Numbered 0 .. <# cpus on the blade> */
504bb898558SAl Viro static inline int uv_blade_processor_id(void)
505bb898558SAl Viro {
506bb898558SAl Viro 	return uv_hub_info->blade_processor_id;
507bb898558SAl Viro }
508bb898558SAl Viro 
509bb898558SAl Viro /* Blade number of current cpu. Numnbered 0 .. <#blades -1> */
510bb898558SAl Viro static inline int uv_numa_blade_id(void)
511bb898558SAl Viro {
512bb898558SAl Viro 	return uv_hub_info->numa_blade_id;
513bb898558SAl Viro }
514bb898558SAl Viro 
515bb898558SAl Viro /* Convert a cpu number to the the UV blade number */
516bb898558SAl Viro static inline int uv_cpu_to_blade_id(int cpu)
517bb898558SAl Viro {
518bb898558SAl Viro 	return uv_cpu_to_blade[cpu];
519bb898558SAl Viro }
520bb898558SAl Viro 
521bb898558SAl Viro /* Convert linux node number to the UV blade number */
522bb898558SAl Viro static inline int uv_node_to_blade_id(int nid)
523bb898558SAl Viro {
524bb898558SAl Viro 	return uv_node_to_blade[nid];
525bb898558SAl Viro }
526bb898558SAl Viro 
527bb898558SAl Viro /* Convert a blade id to the PNODE of the blade */
528bb898558SAl Viro static inline int uv_blade_to_pnode(int bid)
529bb898558SAl Viro {
530bb898558SAl Viro 	return uv_blade_info[bid].pnode;
531bb898558SAl Viro }
532bb898558SAl Viro 
5336c7184b7SJack Steiner /* Nid of memory node on blade. -1 if no blade-local memory */
5346c7184b7SJack Steiner static inline int uv_blade_to_memory_nid(int bid)
5356c7184b7SJack Steiner {
5366c7184b7SJack Steiner 	return uv_blade_info[bid].memory_nid;
5376c7184b7SJack Steiner }
5386c7184b7SJack Steiner 
539bb898558SAl Viro /* Determine the number of possible cpus on a blade */
540bb898558SAl Viro static inline int uv_blade_nr_possible_cpus(int bid)
541bb898558SAl Viro {
542bb898558SAl Viro 	return uv_blade_info[bid].nr_possible_cpus;
543bb898558SAl Viro }
544bb898558SAl Viro 
545bb898558SAl Viro /* Determine the number of online cpus on a blade */
546bb898558SAl Viro static inline int uv_blade_nr_online_cpus(int bid)
547bb898558SAl Viro {
548bb898558SAl Viro 	return uv_blade_info[bid].nr_online_cpus;
549bb898558SAl Viro }
550bb898558SAl Viro 
551bb898558SAl Viro /* Convert a cpu id to the PNODE of the blade containing the cpu */
552bb898558SAl Viro static inline int uv_cpu_to_pnode(int cpu)
553bb898558SAl Viro {
554bb898558SAl Viro 	return uv_blade_info[uv_cpu_to_blade_id(cpu)].pnode;
555bb898558SAl Viro }
556bb898558SAl Viro 
557bb898558SAl Viro /* Convert a linux node number to the PNODE of the blade */
558bb898558SAl Viro static inline int uv_node_to_pnode(int nid)
559bb898558SAl Viro {
560bb898558SAl Viro 	return uv_blade_info[uv_node_to_blade_id(nid)].pnode;
561bb898558SAl Viro }
562bb898558SAl Viro 
563bb898558SAl Viro /* Maximum possible number of blades */
564bb898558SAl Viro static inline int uv_num_possible_blades(void)
565bb898558SAl Viro {
566bb898558SAl Viro 	return uv_possible_blades;
567bb898558SAl Viro }
568bb898558SAl Viro 
5690d12ef0cSMike Travis /* Per Hub NMI support */
5700d12ef0cSMike Travis extern void uv_nmi_setup(void);
5710d12ef0cSMike Travis 
5720d12ef0cSMike Travis /* BMC sets a bit this MMR non-zero before sending an NMI */
5730d12ef0cSMike Travis #define UVH_NMI_MMR		UVH_SCRATCH5
5740d12ef0cSMike Travis #define UVH_NMI_MMR_CLEAR	UVH_SCRATCH5_ALIAS
5750d12ef0cSMike Travis #define UVH_NMI_MMR_SHIFT	63
5760d12ef0cSMike Travis #define	UVH_NMI_MMR_TYPE	"SCRATCH5"
5770d12ef0cSMike Travis 
5780d12ef0cSMike Travis /* Newer SMM NMI handler, not present in all systems */
5790d12ef0cSMike Travis #define UVH_NMI_MMRX		UVH_EVENT_OCCURRED0
5800d12ef0cSMike Travis #define UVH_NMI_MMRX_CLEAR	UVH_EVENT_OCCURRED0_ALIAS
5810d12ef0cSMike Travis #define UVH_NMI_MMRX_SHIFT	(is_uv1_hub() ? \
5820d12ef0cSMike Travis 					UV1H_EVENT_OCCURRED0_EXTIO_INT0_SHFT :\
5830d12ef0cSMike Travis 					UVXH_EVENT_OCCURRED0_EXTIO_INT0_SHFT)
5840d12ef0cSMike Travis #define	UVH_NMI_MMRX_TYPE	"EXTIO_INT0"
5850d12ef0cSMike Travis 
5860d12ef0cSMike Travis /* Non-zero indicates newer SMM NMI handler present */
5870d12ef0cSMike Travis #define UVH_NMI_MMRX_SUPPORTED	UVH_EXTIO_INT0_BROADCAST
5880d12ef0cSMike Travis 
5890d12ef0cSMike Travis /* Indicates to BIOS that we want to use the newer SMM NMI handler */
5900d12ef0cSMike Travis #define UVH_NMI_MMRX_REQ	UVH_SCRATCH5_ALIAS_2
5910d12ef0cSMike Travis #define UVH_NMI_MMRX_REQ_SHIFT	62
5920d12ef0cSMike Travis 
5930d12ef0cSMike Travis struct uv_hub_nmi_s {
5940d12ef0cSMike Travis 	raw_spinlock_t	nmi_lock;
5950d12ef0cSMike Travis 	atomic_t	in_nmi;		/* flag this node in UV NMI IRQ */
5960d12ef0cSMike Travis 	atomic_t	cpu_owner;	/* last locker of this struct */
5970d12ef0cSMike Travis 	atomic_t	read_mmr_count;	/* count of MMR reads */
5980d12ef0cSMike Travis 	atomic_t	nmi_count;	/* count of true UV NMIs */
5990d12ef0cSMike Travis 	unsigned long	nmi_value;	/* last value read from NMI MMR */
6000d12ef0cSMike Travis };
6010d12ef0cSMike Travis 
6020d12ef0cSMike Travis struct uv_cpu_nmi_s {
6030d12ef0cSMike Travis 	struct uv_hub_nmi_s	*hub;
6040d12ef0cSMike Travis 	atomic_t		state;
6050d12ef0cSMike Travis 	atomic_t		pinging;
6060d12ef0cSMike Travis 	int			queries;
6070d12ef0cSMike Travis 	int			pings;
6080d12ef0cSMike Travis };
6090d12ef0cSMike Travis 
6100d12ef0cSMike Travis DECLARE_PER_CPU(struct uv_cpu_nmi_s, __uv_cpu_nmi);
6110d12ef0cSMike Travis #define uv_cpu_nmi			(__get_cpu_var(__uv_cpu_nmi))
6120d12ef0cSMike Travis #define uv_hub_nmi			(uv_cpu_nmi.hub)
6130d12ef0cSMike Travis #define uv_cpu_nmi_per(cpu)		(per_cpu(__uv_cpu_nmi, cpu))
6140d12ef0cSMike Travis #define uv_hub_nmi_per(cpu)		(uv_cpu_nmi_per(cpu).hub)
6150d12ef0cSMike Travis 
6160d12ef0cSMike Travis /* uv_cpu_nmi_states */
6170d12ef0cSMike Travis #define	UV_NMI_STATE_OUT		0
6180d12ef0cSMike Travis #define	UV_NMI_STATE_IN			1
6190d12ef0cSMike Travis #define	UV_NMI_STATE_DUMP		2
6200d12ef0cSMike Travis #define	UV_NMI_STATE_DUMP_DONE		3
6210d12ef0cSMike Travis 
6227f1baa06SMike Travis /* Update SCIR state */
6237f1baa06SMike Travis static inline void uv_set_scir_bits(unsigned char value)
6247f1baa06SMike Travis {
6257f1baa06SMike Travis 	if (uv_hub_info->scir.state != value) {
6267f1baa06SMike Travis 		uv_hub_info->scir.state = value;
6277f1baa06SMike Travis 		uv_write_local_mmr8(uv_hub_info->scir.offset, value);
6287f1baa06SMike Travis 	}
6297f1baa06SMike Travis }
63066666e50SJack Steiner 
63139d30770SMike Travis static inline unsigned long uv_scir_offset(int apicid)
63239d30770SMike Travis {
63339d30770SMike Travis 	return SCIR_LOCAL_MMR_BASE | (apicid & 0x3f);
63439d30770SMike Travis }
63539d30770SMike Travis 
6367f1baa06SMike Travis static inline void uv_set_cpu_scir_bits(int cpu, unsigned char value)
6377f1baa06SMike Travis {
6387f1baa06SMike Travis 	if (uv_cpu_hub_info(cpu)->scir.state != value) {
63939d30770SMike Travis 		uv_write_global_mmr8(uv_cpu_to_pnode(cpu),
64039d30770SMike Travis 				uv_cpu_hub_info(cpu)->scir.offset, value);
6417f1baa06SMike Travis 		uv_cpu_hub_info(cpu)->scir.state = value;
6427f1baa06SMike Travis 	}
6437f1baa06SMike Travis }
644bb898558SAl Viro 
6458191c9f6SDimitri Sivanich extern unsigned int uv_apicid_hibits;
64656abcf24SJack Steiner static unsigned long uv_hub_ipi_value(int apicid, int vector, int mode)
64756abcf24SJack Steiner {
6488191c9f6SDimitri Sivanich 	apicid |= uv_apicid_hibits;
64956abcf24SJack Steiner 	return (1UL << UVH_IPI_INT_SEND_SHFT) |
65056abcf24SJack Steiner 			((apicid) << UVH_IPI_INT_APIC_ID_SHFT) |
65156abcf24SJack Steiner 			(mode << UVH_IPI_INT_DELIVERY_MODE_SHFT) |
65256abcf24SJack Steiner 			(vector << UVH_IPI_INT_VECTOR_SHFT);
65356abcf24SJack Steiner }
65456abcf24SJack Steiner 
65566666e50SJack Steiner static inline void uv_hub_send_ipi(int pnode, int apicid, int vector)
65666666e50SJack Steiner {
65766666e50SJack Steiner 	unsigned long val;
65802dd0a06SRobin Holt 	unsigned long dmode = dest_Fixed;
65902dd0a06SRobin Holt 
66002dd0a06SRobin Holt 	if (vector == NMI_VECTOR)
66102dd0a06SRobin Holt 		dmode = dest_NMI;
66266666e50SJack Steiner 
66356abcf24SJack Steiner 	val = uv_hub_ipi_value(apicid, vector, dmode);
66466666e50SJack Steiner 	uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
66566666e50SJack Steiner }
66666666e50SJack Steiner 
6677a1110e8SJack Steiner /*
6687a1110e8SJack Steiner  * Get the minimum revision number of the hub chips within the partition.
6692a919596SJack Steiner  *     1 - UV1 rev 1.0 initial silicon
6702a919596SJack Steiner  *     2 - UV1 rev 2.0 production silicon
6712a919596SJack Steiner  *     3 - UV2 rev 1.0 initial silicon
6726edbd471SMike Travis  *     5 - UV3 rev 1.0 initial silicon
6737a1110e8SJack Steiner  */
6747a1110e8SJack Steiner static inline int uv_get_min_hub_revision_id(void)
6757a1110e8SJack Steiner {
6762a919596SJack Steiner 	return uv_hub_info->hub_revision;
6777a1110e8SJack Steiner }
6787a1110e8SJack Steiner 
679bc5d9940SJack Steiner #endif /* CONFIG_X86_64 */
6807f1baa06SMike Travis #endif /* _ASM_X86_UV_UV_HUB_H */
681