xref: /titanic_53/usr/src/uts/i86pc/os/lgrpplat.c (revision 2e2c009b1ed162f14fb834101a3ce2b3ee5fe2a7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c39996a7Sstevel  * Common Development and Distribution License (the "License").
6c39996a7Sstevel  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21c39996a7Sstevel 
227c478bd9Sstevel@tonic-gate /*
23472714d6Skchow  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
30*2e2c009bSjjc /*
31*2e2c009bSjjc  * LOCALITY GROUP (LGROUP) PLATFORM SUPPORT FOR X86/AMD64 PLATFORMS
32*2e2c009bSjjc  * ================================================================
33*2e2c009bSjjc  * Multiprocessor AMD and Intel systems may have Non Uniform Memory Access
34*2e2c009bSjjc  * (NUMA).  A NUMA machine consists of one or more "nodes" that each consist of
35*2e2c009bSjjc  * one or more CPUs and some local memory.  The CPUs in each node can access
36*2e2c009bSjjc  * the memory in the other nodes but at a higher latency than accessing their
37*2e2c009bSjjc  * local memory.  Typically, a system with only one node has Uniform Memory
38*2e2c009bSjjc  * Access (UMA), but it may be possible to have a one node system that has
39*2e2c009bSjjc  * some global memory outside of the node which is higher latency.
40*2e2c009bSjjc  *
41*2e2c009bSjjc  * Module Description
42*2e2c009bSjjc  * ------------------
43*2e2c009bSjjc  * This module provides a platform interface for determining which CPUs and
44*2e2c009bSjjc  * which memory (and how much) are in a NUMA node and how far each node is from
45*2e2c009bSjjc  * each other.  The interface is used by the Virtual Memory (VM) system and the
46*2e2c009bSjjc  * common lgroup framework.  The VM system uses the plat_*() routines to fill
47*2e2c009bSjjc  * in its memory node (memnode) array with the physical address range spanned
48*2e2c009bSjjc  * by each NUMA node to know which memory belongs to which node, so it can
49*2e2c009bSjjc  * build and manage a physical page free list for each NUMA node and allocate
50*2e2c009bSjjc  * local memory from each node as needed.  The common lgroup framework uses the
51*2e2c009bSjjc  * exported lgrp_plat_*() routines to figure out which CPUs and memory belong
52*2e2c009bSjjc  * to each node (leaf lgroup) and how far each node is from each other, so it
53*2e2c009bSjjc  * can build the latency (lgroup) topology for the machine in order to optimize
54*2e2c009bSjjc  * for locality.  Also, an lgroup platform handle instead of lgroups are used
55*2e2c009bSjjc  * in the interface with this module, so this module shouldn't need to know
56*2e2c009bSjjc  * anything about lgroups.  Instead, it just needs to know which CPUs, memory,
57*2e2c009bSjjc  * etc. are in each NUMA node, how far each node is from each other, and to use
58*2e2c009bSjjc  * a unique lgroup platform handle to refer to each node through the interface.
59*2e2c009bSjjc  *
60*2e2c009bSjjc  * Determining NUMA Configuration
61*2e2c009bSjjc  * ------------------------------
62*2e2c009bSjjc  * By default, this module will try to determine the NUMA configuration of the
63*2e2c009bSjjc  * machine by reading the ACPI System Resource Affinity Table (SRAT) and System
64*2e2c009bSjjc  * Locality Information Table (SLIT).  The SRAT contains info to tell which
65*2e2c009bSjjc  * CPUs and memory are local to a given proximity domain (NUMA node).  The SLIT
66*2e2c009bSjjc  * is a matrix that gives the distance between each system locality (which is
67*2e2c009bSjjc  * a NUMA node and should correspond to proximity domains in the SRAT).  For
68*2e2c009bSjjc  * more details on the SRAT and SLIT, please refer to an ACPI 3.0 or newer
69*2e2c009bSjjc  * specification.
70*2e2c009bSjjc  *
71*2e2c009bSjjc  * If the SRAT doesn't exist on a system with AMD Opteron processors, we
72*2e2c009bSjjc  * examine registers in PCI configuration space to determine how many nodes are
73*2e2c009bSjjc  * in the system and which CPUs and memory are in each node.
74*2e2c009bSjjc  * do while booting the kernel.
75*2e2c009bSjjc  *
76*2e2c009bSjjc  * NOTE: Using these PCI configuration space registers to determine this
77*2e2c009bSjjc  *       locality info is not guaranteed to work or be compatible across all
78*2e2c009bSjjc  *	 Opteron processor families.
79*2e2c009bSjjc  *
80*2e2c009bSjjc  * If the SLIT does not exist or look right, the kernel will probe to determine
81*2e2c009bSjjc  * the distance between nodes as long as the NUMA CPU and memory configuration
82*2e2c009bSjjc  * has been determined (see lgrp_plat_probe() for details).
83*2e2c009bSjjc  *
84*2e2c009bSjjc  * Data Structures
85*2e2c009bSjjc  * ---------------
86*2e2c009bSjjc  * The main data structures used by this code are the following:
87*2e2c009bSjjc  *
88*2e2c009bSjjc  * - lgrp_plat_cpu_node[]		APIC ID to node ID mapping table
89*2e2c009bSjjc  *					indexed by hashed APIC ID (only used
90*2e2c009bSjjc  *					for SRAT)
91*2e2c009bSjjc  *
92*2e2c009bSjjc  * - lgrp_plat_lat_stats.latencies[][]	Table of latencies between same and
93*2e2c009bSjjc  *					different nodes indexed by node ID
94*2e2c009bSjjc  *
95*2e2c009bSjjc  * - lgrp_plat_node_cnt			Number of NUMA nodes in system
96*2e2c009bSjjc  *
97*2e2c009bSjjc  * - lgrp_plat_node_domain[]		Node ID to proximity domain ID mapping
98*2e2c009bSjjc  *					table indexed by node ID (only used
99*2e2c009bSjjc  *					for SRAT)
100*2e2c009bSjjc  *
101*2e2c009bSjjc  * - lgrp_plat_node_memory[]		Table with physical address range for
102*2e2c009bSjjc  *					each node indexed by node ID
103*2e2c009bSjjc  *
104*2e2c009bSjjc  * The code is implemented to make the following always be true:
105*2e2c009bSjjc  *
106*2e2c009bSjjc  *	lgroup platform handle == node ID == memnode ID
107*2e2c009bSjjc  *
108*2e2c009bSjjc  * Moreover, it allows for the proximity domain ID to be equal to all of the
109*2e2c009bSjjc  * above as long as the proximity domains IDs are numbered from 0 to <number of
110*2e2c009bSjjc  * nodes - 1>.  This is done by hashing each proximity domain ID into the range
111*2e2c009bSjjc  * from 0 to <number of nodes - 1>.  Then proximity ID N will hash into node ID
112*2e2c009bSjjc  * N and proximity domain ID N will be entered into lgrp_plat_node_domain[N]
113*2e2c009bSjjc  * and be assigned node ID N.  If the proximity domain IDs aren't numbered
114*2e2c009bSjjc  * from 0 to <number of nodes - 1>, then hashing the proximity domain IDs into
115*2e2c009bSjjc  * lgrp_plat_node_domain[] will still work for assigning proximity domain IDs
116*2e2c009bSjjc  * to node IDs.  However, the proximity domain IDs may not map to the
117*2e2c009bSjjc  * equivalent node ID since we want to keep the node IDs numbered from 0 to
118*2e2c009bSjjc  * <number of nodes - 1> to minimize cost of searching and potentially space.
119*2e2c009bSjjc  */
120*2e2c009bSjjc 
121*2e2c009bSjjc 
1227c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>	/* for {in,out}{b,w,l}() */
1237c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
124f78a91cdSjjc #include <sys/controlregs.h>
1257c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
1267c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
1277c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
1287c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1297c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
1307c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
1317c478bd9Sstevel@tonic-gate #include <sys/mman.h>
132ef50d8c0Sesaxe #include <sys/pci_cfgspace.h>
133ef50d8c0Sesaxe #include <sys/pci_impl.h>
1347c478bd9Sstevel@tonic-gate #include <sys/param.h>
135fb2f18f8Sesaxe #include <sys/pghw.h>
1367c478bd9Sstevel@tonic-gate #include <sys/promif.h>		/* for prom_printf() */
137*2e2c009bSjjc #include <sys/sysmacros.h>
1387c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1397c478bd9Sstevel@tonic-gate #include <sys/thread.h>
1407c478bd9Sstevel@tonic-gate #include <sys/types.h>
1417c478bd9Sstevel@tonic-gate #include <sys/var.h>
1427c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>	/* for x86_feature and X86_AMD */
1437c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
1447c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
145affbd3ccSkchow #include <vm/vm_dep.h>
1467c478bd9Sstevel@tonic-gate 
147*2e2c009bSjjc #include "acpi_fw.h"		/* for SRAT and SLIT */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #define	MAX_NODES		8
1517c478bd9Sstevel@tonic-gate #define	NLGRP			(MAX_NODES * (MAX_NODES - 1) + 1)
1527c478bd9Sstevel@tonic-gate 
153*2e2c009bSjjc /*
154*2e2c009bSjjc  * Constants for configuring probing
155*2e2c009bSjjc  */
1567c478bd9Sstevel@tonic-gate #define	LGRP_PLAT_PROBE_NROUNDS		64	/* default laps for probing */
1577c478bd9Sstevel@tonic-gate #define	LGRP_PLAT_PROBE_NSAMPLES	1	/* default samples to take */
1588949bcd6Sandrei #define	LGRP_PLAT_PROBE_NREADS		256	/* number of vendor ID reads */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
161*2e2c009bSjjc  * Flags for probing
162*2e2c009bSjjc  */
163*2e2c009bSjjc #define	LGRP_PLAT_PROBE_ENABLE		0x1	/* enable probing */
164*2e2c009bSjjc #define	LGRP_PLAT_PROBE_PGCPY		0x2	/* probe using page copy */
165*2e2c009bSjjc #define	LGRP_PLAT_PROBE_VENDOR		0x4	/* probe vendor ID register */
166*2e2c009bSjjc 
167*2e2c009bSjjc /*
168*2e2c009bSjjc  * Hash CPU APIC ID into CPU to node mapping table using max_ncpus
169*2e2c009bSjjc  * to minimize span of entries used
170*2e2c009bSjjc  */
171*2e2c009bSjjc #define	CPU_NODE_HASH(apicid)		((apicid) % max_ncpus)
172*2e2c009bSjjc 
173*2e2c009bSjjc /*
174*2e2c009bSjjc  * Hash proximity domain ID into node to domain mapping table using to minimize
175*2e2c009bSjjc  * span of entries used
176*2e2c009bSjjc  */
177*2e2c009bSjjc #define	NODE_DOMAIN_HASH(domain)	((domain) % lgrp_plat_node_cnt)
178*2e2c009bSjjc 
179*2e2c009bSjjc 
180*2e2c009bSjjc /*
181*2e2c009bSjjc  * CPU APIC ID to node ID mapping structure (only used with SRAT)
182*2e2c009bSjjc  */
183*2e2c009bSjjc typedef	struct cpu_node_map {
184*2e2c009bSjjc 	int		exists;
185*2e2c009bSjjc 	uint_t		node;
186*2e2c009bSjjc 	uint32_t	apicid;
187*2e2c009bSjjc 	uint32_t	prox_domain;
188*2e2c009bSjjc } cpu_node_map_t;
189*2e2c009bSjjc 
190*2e2c009bSjjc /*
191*2e2c009bSjjc  * Latency statistics
192*2e2c009bSjjc  */
193*2e2c009bSjjc typedef struct lgrp_plat_latency_stats {
194*2e2c009bSjjc 	hrtime_t	latencies[MAX_NODES][MAX_NODES];
195*2e2c009bSjjc 	hrtime_t	latency_max;
196*2e2c009bSjjc 	hrtime_t	latency_min;
197*2e2c009bSjjc } lgrp_plat_latency_stats_t;
198*2e2c009bSjjc 
199*2e2c009bSjjc /*
200*2e2c009bSjjc  * Memory configuration for probing
201*2e2c009bSjjc  */
202*2e2c009bSjjc typedef struct lgrp_plat_probe_mem_config {
203*2e2c009bSjjc 	size_t	probe_memsize;		/* how much memory to probe per node */
204*2e2c009bSjjc 	caddr_t	probe_va[MAX_NODES];	/* where memory mapped for probing */
205*2e2c009bSjjc 	pfn_t	probe_pfn[MAX_NODES];	/* physical pages to map for probing */
206*2e2c009bSjjc } lgrp_plat_probe_mem_config_t;
207*2e2c009bSjjc 
208*2e2c009bSjjc /*
209*2e2c009bSjjc  * Statistics kept for probing
210*2e2c009bSjjc  */
211*2e2c009bSjjc typedef struct lgrp_plat_probe_stats {
212*2e2c009bSjjc 	hrtime_t	flush_cost;
213*2e2c009bSjjc 	hrtime_t	probe_cost;
214*2e2c009bSjjc 	hrtime_t	probe_cost_total;
215*2e2c009bSjjc 	hrtime_t	probe_error_code;
216*2e2c009bSjjc 	hrtime_t	probe_errors[MAX_NODES][MAX_NODES];
217*2e2c009bSjjc 	int		probe_suspect[MAX_NODES][MAX_NODES];
218*2e2c009bSjjc 	hrtime_t	probe_max[MAX_NODES][MAX_NODES];
219*2e2c009bSjjc 	hrtime_t	probe_min[MAX_NODES][MAX_NODES];
220*2e2c009bSjjc } lgrp_plat_probe_stats_t;
221*2e2c009bSjjc 
222*2e2c009bSjjc /*
223*2e2c009bSjjc  * Node to proximity domain ID mapping structure (only used with SRAT)
224*2e2c009bSjjc  */
225*2e2c009bSjjc typedef	struct node_domain_map {
226*2e2c009bSjjc 	int		exists;
227*2e2c009bSjjc 	uint32_t	prox_domain;
228*2e2c009bSjjc } node_domain_map_t;
229*2e2c009bSjjc 
230*2e2c009bSjjc /*
231*2e2c009bSjjc  * Node ID and starting and ending page for physical memory in node
232*2e2c009bSjjc  */
233*2e2c009bSjjc typedef	struct node_phys_addr_map {
234*2e2c009bSjjc 	pfn_t		start;
235*2e2c009bSjjc 	pfn_t		end;
236*2e2c009bSjjc 	int		exists;
237*2e2c009bSjjc 	uint32_t	prox_domain;
238*2e2c009bSjjc } node_phys_addr_map_t;
239*2e2c009bSjjc 
240*2e2c009bSjjc 
241*2e2c009bSjjc /*
242*2e2c009bSjjc  * CPU APIC ID to node ID mapping table (only used for SRAT)
243*2e2c009bSjjc  */
244*2e2c009bSjjc static cpu_node_map_t			lgrp_plat_cpu_node[NCPU];
245*2e2c009bSjjc 
246*2e2c009bSjjc /*
247*2e2c009bSjjc  * Latency statistics
248*2e2c009bSjjc  */
249*2e2c009bSjjc lgrp_plat_latency_stats_t		lgrp_plat_lat_stats;
250*2e2c009bSjjc 
251*2e2c009bSjjc /*
252*2e2c009bSjjc  * Whether memory is interleaved across nodes causing MPO to be disabled
253*2e2c009bSjjc  */
254*2e2c009bSjjc static int				lgrp_plat_mem_intrlv = 0;
255*2e2c009bSjjc 
256*2e2c009bSjjc /*
257*2e2c009bSjjc  * Node ID to proximity domain ID mapping table (only used for SRAT)
258*2e2c009bSjjc  */
259*2e2c009bSjjc static node_domain_map_t		lgrp_plat_node_domain[MAX_NODES];
260*2e2c009bSjjc 
261*2e2c009bSjjc /*
262*2e2c009bSjjc  * Physical address range for memory in each node
263*2e2c009bSjjc  */
264*2e2c009bSjjc static node_phys_addr_map_t		lgrp_plat_node_memory[MAX_NODES];
265*2e2c009bSjjc 
266*2e2c009bSjjc /*
267*2e2c009bSjjc  * Statistics gotten from probing
268*2e2c009bSjjc  */
269*2e2c009bSjjc static lgrp_plat_probe_stats_t		lgrp_plat_probe_stats;
270*2e2c009bSjjc 
271*2e2c009bSjjc /*
272*2e2c009bSjjc  * Memory configuration for probing
273*2e2c009bSjjc  */
274*2e2c009bSjjc static lgrp_plat_probe_mem_config_t	lgrp_plat_probe_mem_config;
275*2e2c009bSjjc 
276*2e2c009bSjjc /*
277*2e2c009bSjjc  * Error code from processing ACPI SRAT
278*2e2c009bSjjc  */
279*2e2c009bSjjc static int				lgrp_plat_srat_error = 0;
280*2e2c009bSjjc 
281*2e2c009bSjjc /*
282*2e2c009bSjjc  * Error code from processing ACPI SLIT
283*2e2c009bSjjc  */
284*2e2c009bSjjc static int				lgrp_plat_slit_error = 0;
285*2e2c009bSjjc 
286*2e2c009bSjjc /*
287*2e2c009bSjjc  * Allocate lgroup array statically
288*2e2c009bSjjc  */
289*2e2c009bSjjc static lgrp_t				lgrp_space[NLGRP];
290*2e2c009bSjjc static int				nlgrps_alloc;
291*2e2c009bSjjc 
292*2e2c009bSjjc 
293*2e2c009bSjjc /*
294*2e2c009bSjjc  * Number of nodes in system
295*2e2c009bSjjc  */
296*2e2c009bSjjc uint_t			lgrp_plat_node_cnt = 1;
297*2e2c009bSjjc 
298*2e2c009bSjjc /*
299*2e2c009bSjjc  * Configuration Parameters for Probing
300*2e2c009bSjjc  * - lgrp_plat_probe_flags	Flags to specify enabling probing, probe
301*2e2c009bSjjc  *				operation, etc.
302*2e2c009bSjjc  * - lgrp_plat_probe_nrounds	How many rounds of probing to do
303*2e2c009bSjjc  * - lgrp_plat_probe_nsamples	Number of samples to take when probing each
304*2e2c009bSjjc  *				node
305*2e2c009bSjjc  * - lgrp_plat_probe_nreads	Number of times to read vendor ID from
306*2e2c009bSjjc  *				Northbridge for each probe
307*2e2c009bSjjc  */
308*2e2c009bSjjc uint_t			lgrp_plat_probe_flags = 0;
309*2e2c009bSjjc int			lgrp_plat_probe_nrounds = LGRP_PLAT_PROBE_NROUNDS;
310*2e2c009bSjjc int			lgrp_plat_probe_nsamples = LGRP_PLAT_PROBE_NSAMPLES;
311*2e2c009bSjjc int			lgrp_plat_probe_nreads = LGRP_PLAT_PROBE_NREADS;
312*2e2c009bSjjc 
313*2e2c009bSjjc /*
314*2e2c009bSjjc  * Enable use of ACPI System Resource Affinity Table (SRAT) and System
315*2e2c009bSjjc  * Locality Information Table (SLIT)
316*2e2c009bSjjc  */
317*2e2c009bSjjc int			lgrp_plat_srat_enable = 1;
318*2e2c009bSjjc int			lgrp_plat_slit_enable = 1;
319*2e2c009bSjjc 
320*2e2c009bSjjc /*
321*2e2c009bSjjc  * Static array to hold lgroup statistics
322*2e2c009bSjjc  */
323*2e2c009bSjjc struct lgrp_stats	lgrp_stats[NLGRP];
324*2e2c009bSjjc 
325*2e2c009bSjjc 
326*2e2c009bSjjc /*
327*2e2c009bSjjc  * Forward declarations of platform interface routines
328*2e2c009bSjjc  */
329*2e2c009bSjjc void		plat_build_mem_nodes(struct memlist *list);
330*2e2c009bSjjc 
331*2e2c009bSjjc int		plat_lgrphand_to_mem_node(lgrp_handle_t hand);
332*2e2c009bSjjc 
333*2e2c009bSjjc lgrp_handle_t	plat_mem_node_to_lgrphand(int mnode);
334*2e2c009bSjjc 
335*2e2c009bSjjc int		plat_mnode_xcheck(pfn_t pfncnt);
336*2e2c009bSjjc 
337*2e2c009bSjjc int		plat_pfn_to_mem_node(pfn_t pfn);
338*2e2c009bSjjc 
339*2e2c009bSjjc /*
340*2e2c009bSjjc  * Forward declarations of lgroup platform interface routines
341*2e2c009bSjjc  */
342*2e2c009bSjjc lgrp_t		*lgrp_plat_alloc(lgrp_id_t lgrpid);
343*2e2c009bSjjc 
344*2e2c009bSjjc void		lgrp_plat_config(lgrp_config_flag_t flag, uintptr_t arg);
345*2e2c009bSjjc 
346*2e2c009bSjjc lgrp_handle_t	lgrp_plat_cpu_to_hand(processorid_t id);
347*2e2c009bSjjc 
348*2e2c009bSjjc void		lgrp_plat_init(void);
349*2e2c009bSjjc 
350*2e2c009bSjjc int		lgrp_plat_latency(lgrp_handle_t from, lgrp_handle_t to);
351*2e2c009bSjjc 
352*2e2c009bSjjc void		lgrp_plat_main_init(void);
353*2e2c009bSjjc 
354*2e2c009bSjjc int		lgrp_plat_max_lgrps(void);
355*2e2c009bSjjc 
356*2e2c009bSjjc pgcnt_t		lgrp_plat_mem_size(lgrp_handle_t plathand,
357*2e2c009bSjjc     lgrp_mem_query_t query);
358*2e2c009bSjjc 
359*2e2c009bSjjc lgrp_handle_t	lgrp_plat_pfn_to_hand(pfn_t pfn);
360*2e2c009bSjjc 
361*2e2c009bSjjc void		lgrp_plat_probe(void);
362*2e2c009bSjjc 
363*2e2c009bSjjc lgrp_handle_t	lgrp_plat_root_hand(void);
364*2e2c009bSjjc 
365*2e2c009bSjjc 
366*2e2c009bSjjc /*
367*2e2c009bSjjc  * Forward declarations of local routines
368*2e2c009bSjjc  */
369*2e2c009bSjjc static int	is_opteron(void);
370*2e2c009bSjjc 
371*2e2c009bSjjc static int	lgrp_plat_cpu_to_node(cpu_t *cp, cpu_node_map_t *cpu_node);
372*2e2c009bSjjc 
373*2e2c009bSjjc static int	lgrp_plat_domain_to_node(node_domain_map_t *node_domain,
374*2e2c009bSjjc     uint32_t domain);
375*2e2c009bSjjc 
376*2e2c009bSjjc static void	lgrp_plat_latency_adjust(node_phys_addr_map_t *node_memory,
377*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats,
378*2e2c009bSjjc     lgrp_plat_probe_stats_t *probe_stats);
379*2e2c009bSjjc 
380*2e2c009bSjjc static int	lgrp_plat_latency_verify(node_phys_addr_map_t *node_memory,
381*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats);
382*2e2c009bSjjc 
383*2e2c009bSjjc static pgcnt_t	lgrp_plat_mem_size_default(lgrp_handle_t, lgrp_mem_query_t);
384*2e2c009bSjjc 
385*2e2c009bSjjc static int	lgrp_plat_node_domain_update(node_domain_map_t *node_domain,
386*2e2c009bSjjc     uint32_t domain);
387*2e2c009bSjjc 
388*2e2c009bSjjc static int	lgrp_plat_node_memory_update(node_domain_map_t *node_domain,
389*2e2c009bSjjc     node_phys_addr_map_t *node_memory, uintptr_t start, uintptr_t end,
390*2e2c009bSjjc     uint32_t domain);
391*2e2c009bSjjc 
392*2e2c009bSjjc static hrtime_t	lgrp_plat_probe_time(int to, cpu_node_map_t *cpu_node,
393*2e2c009bSjjc     lgrp_plat_probe_mem_config_t *probe_mem_config,
394*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats,
395*2e2c009bSjjc     lgrp_plat_probe_stats_t *probe_stats);
396*2e2c009bSjjc 
397*2e2c009bSjjc static int	lgrp_plat_process_slit(struct slit *tp, uint_t node_cnt,
398*2e2c009bSjjc     node_phys_addr_map_t *node_memory, lgrp_plat_latency_stats_t *lat_stats);
399*2e2c009bSjjc 
400*2e2c009bSjjc static int	lgrp_plat_process_srat(struct srat *tp, uint_t *node_cnt,
401*2e2c009bSjjc     node_domain_map_t *node_domain, cpu_node_map_t *cpu_node,
402*2e2c009bSjjc     node_phys_addr_map_t *node_memory);
403*2e2c009bSjjc 
404*2e2c009bSjjc static int	lgrp_plat_srat_domains(struct srat *tp);
405*2e2c009bSjjc 
406*2e2c009bSjjc static void	lgrp_plat_2level_setup(node_phys_addr_map_t *node_memory,
407*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats);
408*2e2c009bSjjc 
409*2e2c009bSjjc static void	opt_get_numa_config(uint_t *node_cnt, int *mem_intrlv,
410*2e2c009bSjjc     node_phys_addr_map_t *node_memory);
411*2e2c009bSjjc 
412*2e2c009bSjjc static hrtime_t	opt_probe_vendor(int dest_node, int nreads);
413*2e2c009bSjjc 
414*2e2c009bSjjc 
415*2e2c009bSjjc /*
416*2e2c009bSjjc  * PLATFORM INTERFACE ROUTINES
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*
420*2e2c009bSjjc  * Configure memory nodes for machines with more than one node (ie NUMA)
421*2e2c009bSjjc  */
422*2e2c009bSjjc void
423*2e2c009bSjjc plat_build_mem_nodes(struct memlist *list)
424*2e2c009bSjjc {
425*2e2c009bSjjc 	pfn_t		cur_start;	/* start addr of subrange */
426*2e2c009bSjjc 	pfn_t		cur_end;	/* end addr of subrange */
427*2e2c009bSjjc 	pfn_t		start;		/* start addr of whole range */
428*2e2c009bSjjc 	pfn_t		end;		/* end addr of whole range */
429*2e2c009bSjjc 
430*2e2c009bSjjc 	/*
431*2e2c009bSjjc 	 * Boot install lists are arranged <addr, len>, ...
432*2e2c009bSjjc 	 */
433*2e2c009bSjjc 	while (list) {
434*2e2c009bSjjc 		int	node;
435*2e2c009bSjjc 
436*2e2c009bSjjc 		start = list->address >> PAGESHIFT;
437*2e2c009bSjjc 		end = (list->address + list->size - 1) >> PAGESHIFT;
438*2e2c009bSjjc 
439*2e2c009bSjjc 		if (start > physmax) {
440*2e2c009bSjjc 			list = list->next;
441*2e2c009bSjjc 			continue;
442*2e2c009bSjjc 		}
443*2e2c009bSjjc 		if (end > physmax)
444*2e2c009bSjjc 			end = physmax;
445*2e2c009bSjjc 
446*2e2c009bSjjc 		/*
447*2e2c009bSjjc 		 * When there is only one memnode, just add memory to memnode
448*2e2c009bSjjc 		 */
449*2e2c009bSjjc 		if (max_mem_nodes == 1) {
450*2e2c009bSjjc 			mem_node_add_slice(start, end);
451*2e2c009bSjjc 			list = list->next;
452*2e2c009bSjjc 			continue;
453*2e2c009bSjjc 		}
454*2e2c009bSjjc 
455*2e2c009bSjjc 		/*
456*2e2c009bSjjc 		 * mem_node_add_slice() expects to get a memory range that
457*2e2c009bSjjc 		 * is within one memnode, so need to split any memory range
458*2e2c009bSjjc 		 * that spans multiple memnodes into subranges that are each
459*2e2c009bSjjc 		 * contained within one memnode when feeding them to
460*2e2c009bSjjc 		 * mem_node_add_slice()
461*2e2c009bSjjc 		 */
462*2e2c009bSjjc 		cur_start = start;
463*2e2c009bSjjc 		do {
464*2e2c009bSjjc 			node = plat_pfn_to_mem_node(cur_start);
465*2e2c009bSjjc 
466*2e2c009bSjjc 			/*
467*2e2c009bSjjc 			 * Panic if DRAM address map registers or SRAT say
468*2e2c009bSjjc 			 * memory in node doesn't exist or address from
469*2e2c009bSjjc 			 * boot installed memory list entry isn't in this node.
470*2e2c009bSjjc 			 * This shouldn't happen and rest of code can't deal
471*2e2c009bSjjc 			 * with this if it does.
472*2e2c009bSjjc 			 */
473*2e2c009bSjjc 			if (node < 0 || node >= lgrp_plat_node_cnt ||
474*2e2c009bSjjc 			    !lgrp_plat_node_memory[node].exists ||
475*2e2c009bSjjc 			    cur_start < lgrp_plat_node_memory[node].start ||
476*2e2c009bSjjc 			    cur_start > lgrp_plat_node_memory[node].end) {
477*2e2c009bSjjc 				cmn_err(CE_PANIC, "Don't know which memnode "
478*2e2c009bSjjc 				    "to add installed memory address 0x%lx\n",
479*2e2c009bSjjc 				    cur_start);
480*2e2c009bSjjc 			}
481*2e2c009bSjjc 
482*2e2c009bSjjc 			/*
483*2e2c009bSjjc 			 * End of current subrange should not span memnodes
484*2e2c009bSjjc 			 */
485*2e2c009bSjjc 			cur_end = end;
486*2e2c009bSjjc 			if (lgrp_plat_node_memory[node].exists &&
487*2e2c009bSjjc 			    cur_end > lgrp_plat_node_memory[node].end)
488*2e2c009bSjjc 				cur_end = lgrp_plat_node_memory[node].end;
489*2e2c009bSjjc 
490*2e2c009bSjjc 			mem_node_add_slice(cur_start, cur_end);
491*2e2c009bSjjc 
492*2e2c009bSjjc 			/*
493*2e2c009bSjjc 			 * Next subrange starts after end of current one
494*2e2c009bSjjc 			 */
495*2e2c009bSjjc 			cur_start = cur_end + 1;
496*2e2c009bSjjc 		} while (cur_end < end);
497*2e2c009bSjjc 
498*2e2c009bSjjc 		list = list->next;
499*2e2c009bSjjc 	}
500*2e2c009bSjjc 	mem_node_physalign = 0;
501*2e2c009bSjjc 	mem_node_pfn_shift = 0;
502*2e2c009bSjjc }
503*2e2c009bSjjc 
504*2e2c009bSjjc 
505*2e2c009bSjjc int
506*2e2c009bSjjc plat_lgrphand_to_mem_node(lgrp_handle_t hand)
507*2e2c009bSjjc {
508*2e2c009bSjjc 	if (max_mem_nodes == 1)
509*2e2c009bSjjc 		return (0);
510*2e2c009bSjjc 
511*2e2c009bSjjc 	return ((int)hand);
512*2e2c009bSjjc }
513*2e2c009bSjjc 
514*2e2c009bSjjc 
515*2e2c009bSjjc /*
516*2e2c009bSjjc  * plat_mnode_xcheck: checks the node memory ranges to see if there is a pfncnt
517*2e2c009bSjjc  * range of pages aligned on pfncnt that crosses an node boundary. Returns 1 if
518*2e2c009bSjjc  * a crossing is found and returns 0 otherwise.
519*2e2c009bSjjc  */
520*2e2c009bSjjc int
521*2e2c009bSjjc plat_mnode_xcheck(pfn_t pfncnt)
522*2e2c009bSjjc {
523*2e2c009bSjjc 	int	node, prevnode = -1, basenode;
524*2e2c009bSjjc 	pfn_t	ea, sa;
525*2e2c009bSjjc 
526*2e2c009bSjjc 	for (node = 0; node < lgrp_plat_node_cnt; node++) {
527*2e2c009bSjjc 
528*2e2c009bSjjc 		if (lgrp_plat_node_memory[node].exists == 0)
529*2e2c009bSjjc 			continue;
530*2e2c009bSjjc 
531*2e2c009bSjjc 		if (prevnode == -1) {
532*2e2c009bSjjc 			prevnode = node;
533*2e2c009bSjjc 			basenode = node;
534*2e2c009bSjjc 			continue;
535*2e2c009bSjjc 		}
536*2e2c009bSjjc 
537*2e2c009bSjjc 		/* assume x86 node pfn ranges are in increasing order */
538*2e2c009bSjjc 		ASSERT(lgrp_plat_node_memory[node].start >
539*2e2c009bSjjc 		    lgrp_plat_node_memory[prevnode].end);
540*2e2c009bSjjc 
541*2e2c009bSjjc 		/*
542*2e2c009bSjjc 		 * continue if the starting address of node is not contiguous
543*2e2c009bSjjc 		 * with the previous node.
544*2e2c009bSjjc 		 */
545*2e2c009bSjjc 
546*2e2c009bSjjc 		if (lgrp_plat_node_memory[node].start !=
547*2e2c009bSjjc 		    (lgrp_plat_node_memory[prevnode].end + 1)) {
548*2e2c009bSjjc 			basenode = node;
549*2e2c009bSjjc 			prevnode = node;
550*2e2c009bSjjc 			continue;
551*2e2c009bSjjc 		}
552*2e2c009bSjjc 
553*2e2c009bSjjc 		/* check if the starting address of node is pfncnt aligned */
554*2e2c009bSjjc 		if ((lgrp_plat_node_memory[node].start & (pfncnt - 1)) != 0) {
555*2e2c009bSjjc 
556*2e2c009bSjjc 			/*
557*2e2c009bSjjc 			 * at this point, node starts at an unaligned boundary
558*2e2c009bSjjc 			 * and is contiguous with the previous node(s) to
559*2e2c009bSjjc 			 * basenode. Check if there is an aligned contiguous
560*2e2c009bSjjc 			 * range of length pfncnt that crosses this boundary.
561*2e2c009bSjjc 			 */
562*2e2c009bSjjc 
563*2e2c009bSjjc 			sa = P2ALIGN(lgrp_plat_node_memory[prevnode].end,
564*2e2c009bSjjc 			    pfncnt);
565*2e2c009bSjjc 			ea = P2ROUNDUP((lgrp_plat_node_memory[node].start),
566*2e2c009bSjjc 			    pfncnt);
567*2e2c009bSjjc 
568*2e2c009bSjjc 			ASSERT((ea - sa) == pfncnt);
569*2e2c009bSjjc 			if (sa >= lgrp_plat_node_memory[basenode].start &&
570*2e2c009bSjjc 			    ea <= (lgrp_plat_node_memory[node].end + 1))
571*2e2c009bSjjc 				return (1);
572*2e2c009bSjjc 		}
573*2e2c009bSjjc 		prevnode = node;
574*2e2c009bSjjc 	}
575*2e2c009bSjjc 	return (0);
576*2e2c009bSjjc }
577*2e2c009bSjjc 
578*2e2c009bSjjc 
579*2e2c009bSjjc lgrp_handle_t
580*2e2c009bSjjc plat_mem_node_to_lgrphand(int mnode)
581*2e2c009bSjjc {
582*2e2c009bSjjc 	if (max_mem_nodes == 1)
583*2e2c009bSjjc 		return (LGRP_DEFAULT_HANDLE);
584*2e2c009bSjjc 
585*2e2c009bSjjc 	return ((lgrp_handle_t)mnode);
586*2e2c009bSjjc }
587*2e2c009bSjjc 
588*2e2c009bSjjc 
589*2e2c009bSjjc int
590*2e2c009bSjjc plat_pfn_to_mem_node(pfn_t pfn)
591*2e2c009bSjjc {
592*2e2c009bSjjc 	int	node;
593*2e2c009bSjjc 
594*2e2c009bSjjc 	if (max_mem_nodes == 1)
595*2e2c009bSjjc 		return (0);
596*2e2c009bSjjc 
597*2e2c009bSjjc 	for (node = 0; node < lgrp_plat_node_cnt; node++) {
598*2e2c009bSjjc 		/*
599*2e2c009bSjjc 		 * Skip nodes with no memory
600*2e2c009bSjjc 		 */
601*2e2c009bSjjc 		if (!lgrp_plat_node_memory[node].exists)
602*2e2c009bSjjc 			continue;
603*2e2c009bSjjc 
604*2e2c009bSjjc 		if (pfn >= lgrp_plat_node_memory[node].start &&
605*2e2c009bSjjc 		    pfn <= lgrp_plat_node_memory[node].end)
606*2e2c009bSjjc 			return (node);
607*2e2c009bSjjc 	}
608*2e2c009bSjjc 
609*2e2c009bSjjc 	/*
610*2e2c009bSjjc 	 * Didn't find memnode where this PFN lives which should never happen
611*2e2c009bSjjc 	 */
612*2e2c009bSjjc 	ASSERT(node < lgrp_plat_node_cnt);
613*2e2c009bSjjc 	return (-1);
614*2e2c009bSjjc }
615*2e2c009bSjjc 
616*2e2c009bSjjc 
617*2e2c009bSjjc /*
618*2e2c009bSjjc  * LGROUP PLATFORM INTERFACE ROUTINES
619*2e2c009bSjjc  */
620*2e2c009bSjjc 
621*2e2c009bSjjc /*
622*2e2c009bSjjc  * Allocate additional space for an lgroup.
623*2e2c009bSjjc  */
624*2e2c009bSjjc /* ARGSUSED */
625*2e2c009bSjjc lgrp_t *
626*2e2c009bSjjc lgrp_plat_alloc(lgrp_id_t lgrpid)
627*2e2c009bSjjc {
628*2e2c009bSjjc 	lgrp_t *lgrp;
629*2e2c009bSjjc 
630*2e2c009bSjjc 	lgrp = &lgrp_space[nlgrps_alloc++];
631*2e2c009bSjjc 	if (lgrpid >= NLGRP || nlgrps_alloc > NLGRP)
632*2e2c009bSjjc 		return (NULL);
633*2e2c009bSjjc 	return (lgrp);
634*2e2c009bSjjc }
635*2e2c009bSjjc 
636*2e2c009bSjjc 
637*2e2c009bSjjc /*
638*2e2c009bSjjc  * Platform handling for (re)configuration changes
639*2e2c009bSjjc  */
640*2e2c009bSjjc /* ARGSUSED */
641*2e2c009bSjjc void
642*2e2c009bSjjc lgrp_plat_config(lgrp_config_flag_t flag, uintptr_t arg)
643*2e2c009bSjjc {
644*2e2c009bSjjc }
645*2e2c009bSjjc 
646*2e2c009bSjjc 
647*2e2c009bSjjc /*
648*2e2c009bSjjc  * Return the platform handle for the lgroup containing the given CPU
649*2e2c009bSjjc  */
650*2e2c009bSjjc /* ARGSUSED */
651*2e2c009bSjjc lgrp_handle_t
652*2e2c009bSjjc lgrp_plat_cpu_to_hand(processorid_t id)
653*2e2c009bSjjc {
654*2e2c009bSjjc 	lgrp_handle_t	hand;
655*2e2c009bSjjc 
656*2e2c009bSjjc 	if (lgrp_plat_node_cnt == 1)
657*2e2c009bSjjc 		return (LGRP_DEFAULT_HANDLE);
658*2e2c009bSjjc 
659*2e2c009bSjjc 	hand = (lgrp_handle_t)lgrp_plat_cpu_to_node(cpu[id],
660*2e2c009bSjjc 	    lgrp_plat_cpu_node);
661*2e2c009bSjjc 
662*2e2c009bSjjc 	ASSERT(hand != (lgrp_handle_t)-1);
663*2e2c009bSjjc 	if (hand == (lgrp_handle_t)-1)
664*2e2c009bSjjc 		return (LGRP_NULL_HANDLE);
665*2e2c009bSjjc 
666*2e2c009bSjjc 	return (hand);
667*2e2c009bSjjc }
668*2e2c009bSjjc 
669*2e2c009bSjjc 
670*2e2c009bSjjc /*
671*2e2c009bSjjc  * Platform-specific initialization of lgroups
672*2e2c009bSjjc  */
673*2e2c009bSjjc void
674*2e2c009bSjjc lgrp_plat_init(void)
675*2e2c009bSjjc {
676*2e2c009bSjjc #if defined(__xpv)
677*2e2c009bSjjc 	/*
678*2e2c009bSjjc 	 * XXPV	For now, the hypervisor treats all memory equally.
679*2e2c009bSjjc 	 */
680*2e2c009bSjjc 	lgrp_plat_node_cnt = max_mem_nodes = 1;
681*2e2c009bSjjc #else	/* __xpv */
682*2e2c009bSjjc 	uint_t	probe_op;
683*2e2c009bSjjc 
684*2e2c009bSjjc 	/*
685*2e2c009bSjjc 	 * Initialize as a UMA machine
686*2e2c009bSjjc 	 */
687*2e2c009bSjjc 	if (lgrp_topo_ht_limit() == 1) {
688*2e2c009bSjjc 		lgrp_plat_node_cnt = max_mem_nodes = 1;
689*2e2c009bSjjc 		return;
690*2e2c009bSjjc 	}
691*2e2c009bSjjc 
692*2e2c009bSjjc 	/*
693*2e2c009bSjjc 	 * Determine which CPUs and memory are local to each other and number
694*2e2c009bSjjc 	 * of NUMA nodes by reading ACPI System Resource Affinity Table (SRAT)
695*2e2c009bSjjc 	 */
696*2e2c009bSjjc 	lgrp_plat_srat_error = lgrp_plat_process_srat(srat_ptr,
697*2e2c009bSjjc 	    &lgrp_plat_node_cnt, lgrp_plat_node_domain, lgrp_plat_cpu_node,
698*2e2c009bSjjc 	    lgrp_plat_node_memory);
699*2e2c009bSjjc 
700*2e2c009bSjjc 	/*
701*2e2c009bSjjc 	 * Try to use PCI config space registers on Opteron if SRAT doesn't
702*2e2c009bSjjc 	 * exist or there is some error processing the SRAT
703*2e2c009bSjjc 	 */
704*2e2c009bSjjc 	if (lgrp_plat_srat_error != 0 && is_opteron())
705*2e2c009bSjjc 		opt_get_numa_config(&lgrp_plat_node_cnt, &lgrp_plat_mem_intrlv,
706*2e2c009bSjjc 		    lgrp_plat_node_memory);
707*2e2c009bSjjc 
708*2e2c009bSjjc 	/*
709*2e2c009bSjjc 	 * Don't bother to setup system for multiple lgroups and only use one
710*2e2c009bSjjc 	 * memory node when memory is interleaved between any nodes or there is
711*2e2c009bSjjc 	 * only one NUMA node
712*2e2c009bSjjc 	 *
713*2e2c009bSjjc 	 * NOTE: May need to change this for Dynamic Reconfiguration (DR)
714*2e2c009bSjjc 	 *	 when and if it happens for x86/x64
715*2e2c009bSjjc 	 */
716*2e2c009bSjjc 	if (lgrp_plat_mem_intrlv || lgrp_plat_node_cnt == 1) {
717*2e2c009bSjjc 		lgrp_plat_node_cnt = max_mem_nodes = 1;
718*2e2c009bSjjc 		(void) lgrp_topo_ht_limit_set(1);
719*2e2c009bSjjc 		return;
720*2e2c009bSjjc 	}
721*2e2c009bSjjc 
722*2e2c009bSjjc 	/*
723*2e2c009bSjjc 	 * Leaf lgroups on x86/x64 architectures contain one physical
724*2e2c009bSjjc 	 * processor chip. Tune lgrp_expand_proc_thresh and
725*2e2c009bSjjc 	 * lgrp_expand_proc_diff so that lgrp_choose() will spread
726*2e2c009bSjjc 	 * things out aggressively.
727*2e2c009bSjjc 	 */
728*2e2c009bSjjc 	lgrp_expand_proc_thresh = LGRP_LOADAVG_THREAD_MAX / 2;
729*2e2c009bSjjc 	lgrp_expand_proc_diff = 0;
730*2e2c009bSjjc 
731*2e2c009bSjjc 	/*
732*2e2c009bSjjc 	 * There should be one memnode (physical page free list(s)) for
733*2e2c009bSjjc 	 * each node
734*2e2c009bSjjc 	 */
735*2e2c009bSjjc 	max_mem_nodes = lgrp_plat_node_cnt;
736*2e2c009bSjjc 
737*2e2c009bSjjc 	/*
738*2e2c009bSjjc 	 * Determine how far each NUMA node is from each other by
739*2e2c009bSjjc 	 * reading ACPI System Locality Information Table (SLIT) if it
740*2e2c009bSjjc 	 * exists
741*2e2c009bSjjc 	 */
742*2e2c009bSjjc 	lgrp_plat_slit_error = lgrp_plat_process_slit(slit_ptr,
743*2e2c009bSjjc 	    lgrp_plat_node_cnt, lgrp_plat_node_memory,
744*2e2c009bSjjc 	    &lgrp_plat_lat_stats);
745*2e2c009bSjjc 	if (lgrp_plat_slit_error == 0)
746*2e2c009bSjjc 		return;
747*2e2c009bSjjc 
748*2e2c009bSjjc 	/*
749*2e2c009bSjjc 	 * Probe to determine latency between NUMA nodes when SLIT
750*2e2c009bSjjc 	 * doesn't exist or make sense
751*2e2c009bSjjc 	 */
752*2e2c009bSjjc 	lgrp_plat_probe_flags |= LGRP_PLAT_PROBE_ENABLE;
753*2e2c009bSjjc 
754*2e2c009bSjjc 	/*
755*2e2c009bSjjc 	 * Specify whether to probe using vendor ID register or page copy
756*2e2c009bSjjc 	 * if hasn't been specified already or is overspecified
757*2e2c009bSjjc 	 */
758*2e2c009bSjjc 	probe_op = lgrp_plat_probe_flags &
759*2e2c009bSjjc 	    (LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR);
760*2e2c009bSjjc 
761*2e2c009bSjjc 	if (probe_op == 0 ||
762*2e2c009bSjjc 	    probe_op == (LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR)) {
763*2e2c009bSjjc 		lgrp_plat_probe_flags &=
764*2e2c009bSjjc 		    ~(LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR);
765*2e2c009bSjjc 		if (is_opteron())
766*2e2c009bSjjc 			lgrp_plat_probe_flags |=
767*2e2c009bSjjc 			    LGRP_PLAT_PROBE_VENDOR;
768*2e2c009bSjjc 		else
769*2e2c009bSjjc 			lgrp_plat_probe_flags |= LGRP_PLAT_PROBE_PGCPY;
770*2e2c009bSjjc 	}
771*2e2c009bSjjc 
772*2e2c009bSjjc 	/*
773*2e2c009bSjjc 	 * Probing errors can mess up the lgroup topology and
774*2e2c009bSjjc 	 * force us fall back to a 2 level lgroup topology.
775*2e2c009bSjjc 	 * Here we bound how tall the lgroup topology can grow
776*2e2c009bSjjc 	 * in hopes of avoiding any anamolies in probing from
777*2e2c009bSjjc 	 * messing up the lgroup topology by limiting the
778*2e2c009bSjjc 	 * accuracy of the latency topology.
779*2e2c009bSjjc 	 *
780*2e2c009bSjjc 	 * Assume that nodes will at least be configured in a
781*2e2c009bSjjc 	 * ring, so limit height of lgroup topology to be less
782*2e2c009bSjjc 	 * than number of nodes on a system with 4 or more
783*2e2c009bSjjc 	 * nodes
784*2e2c009bSjjc 	 */
785*2e2c009bSjjc 	if (lgrp_plat_node_cnt >= 4 && lgrp_topo_ht_limit() ==
786*2e2c009bSjjc 	    lgrp_topo_ht_limit_default())
787*2e2c009bSjjc 		(void) lgrp_topo_ht_limit_set(lgrp_plat_node_cnt - 1);
788*2e2c009bSjjc #endif	/* __xpv */
789*2e2c009bSjjc }
790*2e2c009bSjjc 
791*2e2c009bSjjc 
792*2e2c009bSjjc /*
793*2e2c009bSjjc  * Return latency between "from" and "to" lgroups
794*2e2c009bSjjc  *
795*2e2c009bSjjc  * This latency number can only be used for relative comparison
796*2e2c009bSjjc  * between lgroups on the running system, cannot be used across platforms,
797*2e2c009bSjjc  * and may not reflect the actual latency.  It is platform and implementation
798*2e2c009bSjjc  * specific, so platform gets to decide its value.  It would be nice if the
799*2e2c009bSjjc  * number was at least proportional to make comparisons more meaningful though.
800*2e2c009bSjjc  */
801*2e2c009bSjjc /* ARGSUSED */
802*2e2c009bSjjc int
803*2e2c009bSjjc lgrp_plat_latency(lgrp_handle_t from, lgrp_handle_t to)
804*2e2c009bSjjc {
805*2e2c009bSjjc 	lgrp_handle_t	src, dest;
806*2e2c009bSjjc 	int		node;
807*2e2c009bSjjc 
808*2e2c009bSjjc 	if (max_mem_nodes == 1)
809*2e2c009bSjjc 		return (0);
810*2e2c009bSjjc 
811*2e2c009bSjjc 	/*
812*2e2c009bSjjc 	 * Return max latency for root lgroup
813*2e2c009bSjjc 	 */
814*2e2c009bSjjc 	if (from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE)
815*2e2c009bSjjc 		return (lgrp_plat_lat_stats.latency_max);
816*2e2c009bSjjc 
817*2e2c009bSjjc 	src = from;
818*2e2c009bSjjc 	dest = to;
819*2e2c009bSjjc 
820*2e2c009bSjjc 	/*
821*2e2c009bSjjc 	 * Return 0 for nodes (lgroup platform handles) out of range
822*2e2c009bSjjc 	 */
823*2e2c009bSjjc 	if (src < 0 || src >= MAX_NODES || dest < 0 || dest >= MAX_NODES)
824*2e2c009bSjjc 		return (0);
825*2e2c009bSjjc 
826*2e2c009bSjjc 	/*
827*2e2c009bSjjc 	 * Probe from current CPU if its lgroup latencies haven't been set yet
828*2e2c009bSjjc 	 * and we are trying to get latency from current CPU to some node
829*2e2c009bSjjc 	 */
830*2e2c009bSjjc 	node = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node);
831*2e2c009bSjjc 	ASSERT(node >= 0 && node < lgrp_plat_node_cnt);
832*2e2c009bSjjc 	if (lgrp_plat_lat_stats.latencies[src][src] == 0 && node == src)
833*2e2c009bSjjc 		lgrp_plat_probe();
834*2e2c009bSjjc 
835*2e2c009bSjjc 	return (lgrp_plat_lat_stats.latencies[src][dest]);
836*2e2c009bSjjc }
837*2e2c009bSjjc 
838*2e2c009bSjjc 
839*2e2c009bSjjc /*
840*2e2c009bSjjc  * Platform-specific initialization
841*2e2c009bSjjc  */
842*2e2c009bSjjc void
843*2e2c009bSjjc lgrp_plat_main_init(void)
844*2e2c009bSjjc {
845*2e2c009bSjjc 	int	curnode;
846*2e2c009bSjjc 	int	ht_limit;
847*2e2c009bSjjc 	int	i;
848*2e2c009bSjjc 
849*2e2c009bSjjc 	/*
850*2e2c009bSjjc 	 * Print a notice that MPO is disabled when memory is interleaved
851*2e2c009bSjjc 	 * across nodes....Would do this when it is discovered, but can't
852*2e2c009bSjjc 	 * because it happens way too early during boot....
853*2e2c009bSjjc 	 */
854*2e2c009bSjjc 	if (lgrp_plat_mem_intrlv)
855*2e2c009bSjjc 		cmn_err(CE_NOTE,
856*2e2c009bSjjc 		    "MPO disabled because memory is interleaved\n");
857*2e2c009bSjjc 
858*2e2c009bSjjc 	/*
859*2e2c009bSjjc 	 * Don't bother to do any probing if it is disabled, there is only one
860*2e2c009bSjjc 	 * node, or the height of the lgroup topology less than or equal to 2
861*2e2c009bSjjc 	 */
862*2e2c009bSjjc 	ht_limit = lgrp_topo_ht_limit();
863*2e2c009bSjjc 	if (!(lgrp_plat_probe_flags & LGRP_PLAT_PROBE_ENABLE) ||
864*2e2c009bSjjc 	    max_mem_nodes == 1 || ht_limit <= 2) {
865*2e2c009bSjjc 		/*
866*2e2c009bSjjc 		 * Setup lgroup latencies for 2 level lgroup topology
867*2e2c009bSjjc 		 * (ie. local and remote only) if they haven't been set yet
868*2e2c009bSjjc 		 */
869*2e2c009bSjjc 		if (ht_limit == 2 && lgrp_plat_lat_stats.latency_min == -1 &&
870*2e2c009bSjjc 		    lgrp_plat_lat_stats.latency_max == 0)
871*2e2c009bSjjc 			lgrp_plat_2level_setup(lgrp_plat_node_memory,
872*2e2c009bSjjc 			    &lgrp_plat_lat_stats);
873*2e2c009bSjjc 		return;
874*2e2c009bSjjc 	}
875*2e2c009bSjjc 
876*2e2c009bSjjc 	if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_VENDOR) {
877*2e2c009bSjjc 		/*
878*2e2c009bSjjc 		 * Should have been able to probe from CPU 0 when it was added
879*2e2c009bSjjc 		 * to lgroup hierarchy, but may not have been able to then
880*2e2c009bSjjc 		 * because it happens so early in boot that gethrtime() hasn't
881*2e2c009bSjjc 		 * been initialized.  (:-(
882*2e2c009bSjjc 		 */
883*2e2c009bSjjc 		curnode = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node);
884*2e2c009bSjjc 		ASSERT(curnode >= 0 && curnode < lgrp_plat_node_cnt);
885*2e2c009bSjjc 		if (lgrp_plat_lat_stats.latencies[curnode][curnode] == 0)
886*2e2c009bSjjc 			lgrp_plat_probe();
887*2e2c009bSjjc 
888*2e2c009bSjjc 		return;
889*2e2c009bSjjc 	}
890*2e2c009bSjjc 
891*2e2c009bSjjc 	/*
892*2e2c009bSjjc 	 * When probing memory, use one page for every sample to determine
893*2e2c009bSjjc 	 * lgroup topology and taking multiple samples
894*2e2c009bSjjc 	 */
895*2e2c009bSjjc 	if (lgrp_plat_probe_mem_config.probe_memsize == 0)
896*2e2c009bSjjc 		lgrp_plat_probe_mem_config.probe_memsize = PAGESIZE *
897*2e2c009bSjjc 		    lgrp_plat_probe_nsamples;
898*2e2c009bSjjc 
899*2e2c009bSjjc 	/*
900*2e2c009bSjjc 	 * Map memory in each node needed for probing to determine latency
901*2e2c009bSjjc 	 * topology
902*2e2c009bSjjc 	 */
903*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
904*2e2c009bSjjc 		int	mnode;
905*2e2c009bSjjc 
906*2e2c009bSjjc 		/*
907*2e2c009bSjjc 		 * Skip this node and leave its probe page NULL
908*2e2c009bSjjc 		 * if it doesn't have any memory
909*2e2c009bSjjc 		 */
910*2e2c009bSjjc 		mnode = plat_lgrphand_to_mem_node((lgrp_handle_t)i);
911*2e2c009bSjjc 		if (!mem_node_config[mnode].exists) {
912*2e2c009bSjjc 			lgrp_plat_probe_mem_config.probe_va[i] = NULL;
913*2e2c009bSjjc 			continue;
914*2e2c009bSjjc 		}
915*2e2c009bSjjc 
916*2e2c009bSjjc 		/*
917*2e2c009bSjjc 		 * Allocate one kernel virtual page
918*2e2c009bSjjc 		 */
919*2e2c009bSjjc 		lgrp_plat_probe_mem_config.probe_va[i] = vmem_alloc(heap_arena,
920*2e2c009bSjjc 		    lgrp_plat_probe_mem_config.probe_memsize, VM_NOSLEEP);
921*2e2c009bSjjc 		if (lgrp_plat_probe_mem_config.probe_va[i] == NULL) {
922*2e2c009bSjjc 			cmn_err(CE_WARN,
923*2e2c009bSjjc 			    "lgrp_plat_main_init: couldn't allocate memory");
924*2e2c009bSjjc 			return;
925*2e2c009bSjjc 		}
926*2e2c009bSjjc 
927*2e2c009bSjjc 		/*
928*2e2c009bSjjc 		 * Get PFN for first page in each node
929*2e2c009bSjjc 		 */
930*2e2c009bSjjc 		lgrp_plat_probe_mem_config.probe_pfn[i] =
931*2e2c009bSjjc 		    mem_node_config[mnode].physbase;
932*2e2c009bSjjc 
933*2e2c009bSjjc 		/*
934*2e2c009bSjjc 		 * Map virtual page to first page in node
935*2e2c009bSjjc 		 */
936*2e2c009bSjjc 		hat_devload(kas.a_hat, lgrp_plat_probe_mem_config.probe_va[i],
937*2e2c009bSjjc 		    lgrp_plat_probe_mem_config.probe_memsize,
938*2e2c009bSjjc 		    lgrp_plat_probe_mem_config.probe_pfn[i],
939*2e2c009bSjjc 		    PROT_READ | PROT_WRITE | HAT_PLAT_NOCACHE,
940*2e2c009bSjjc 		    HAT_LOAD_NOCONSIST);
941*2e2c009bSjjc 	}
942*2e2c009bSjjc 
943*2e2c009bSjjc 	/*
944*2e2c009bSjjc 	 * Probe from current CPU
945*2e2c009bSjjc 	 */
946*2e2c009bSjjc 	lgrp_plat_probe();
947*2e2c009bSjjc }
948*2e2c009bSjjc 
949*2e2c009bSjjc 
950*2e2c009bSjjc /*
951*2e2c009bSjjc  * Return the maximum number of lgrps supported by the platform.
952*2e2c009bSjjc  * Before lgrp topology is known it returns an estimate based on the number of
953*2e2c009bSjjc  * nodes. Once topology is known it returns the actual maximim number of lgrps
954*2e2c009bSjjc  * created. Since x86/x64 doesn't support Dynamic Reconfiguration (DR) and
955*2e2c009bSjjc  * dynamic addition of new nodes, this number may not grow during system
956*2e2c009bSjjc  * lifetime (yet).
957*2e2c009bSjjc  */
958*2e2c009bSjjc int
959*2e2c009bSjjc lgrp_plat_max_lgrps(void)
960*2e2c009bSjjc {
961*2e2c009bSjjc 	return (lgrp_topo_initialized ?
962*2e2c009bSjjc 	    lgrp_alloc_max + 1 :
963*2e2c009bSjjc 	    lgrp_plat_node_cnt * (lgrp_plat_node_cnt - 1) + 1);
964*2e2c009bSjjc }
965*2e2c009bSjjc 
966*2e2c009bSjjc 
967*2e2c009bSjjc /*
968*2e2c009bSjjc  * Return the number of free pages in an lgroup.
969*2e2c009bSjjc  *
970*2e2c009bSjjc  * For query of LGRP_MEM_SIZE_FREE, return the number of base pagesize
971*2e2c009bSjjc  * pages on freelists.  For query of LGRP_MEM_SIZE_AVAIL, return the
972*2e2c009bSjjc  * number of allocatable base pagesize pages corresponding to the
973*2e2c009bSjjc  * lgroup (e.g. do not include page_t's, BOP_ALLOC()'ed memory, ..)
974*2e2c009bSjjc  * For query of LGRP_MEM_SIZE_INSTALL, return the amount of physical
975*2e2c009bSjjc  * memory installed, regardless of whether or not it's usable.
976*2e2c009bSjjc  */
977*2e2c009bSjjc pgcnt_t
978*2e2c009bSjjc lgrp_plat_mem_size(lgrp_handle_t plathand, lgrp_mem_query_t query)
979*2e2c009bSjjc {
980*2e2c009bSjjc 	int	mnode;
981*2e2c009bSjjc 	pgcnt_t npgs = (pgcnt_t)0;
982*2e2c009bSjjc 	extern struct memlist *phys_avail;
983*2e2c009bSjjc 	extern struct memlist *phys_install;
984*2e2c009bSjjc 
985*2e2c009bSjjc 
986*2e2c009bSjjc 	if (plathand == LGRP_DEFAULT_HANDLE)
987*2e2c009bSjjc 		return (lgrp_plat_mem_size_default(plathand, query));
988*2e2c009bSjjc 
989*2e2c009bSjjc 	if (plathand != LGRP_NULL_HANDLE) {
990*2e2c009bSjjc 		mnode = plat_lgrphand_to_mem_node(plathand);
991*2e2c009bSjjc 		if (mnode >= 0 && mem_node_config[mnode].exists) {
992*2e2c009bSjjc 			switch (query) {
993*2e2c009bSjjc 			case LGRP_MEM_SIZE_FREE:
994*2e2c009bSjjc 				npgs = MNODE_PGCNT(mnode);
995*2e2c009bSjjc 				break;
996*2e2c009bSjjc 			case LGRP_MEM_SIZE_AVAIL:
997*2e2c009bSjjc 				npgs = mem_node_memlist_pages(mnode,
998*2e2c009bSjjc 				    phys_avail);
999*2e2c009bSjjc 				break;
1000*2e2c009bSjjc 			case LGRP_MEM_SIZE_INSTALL:
1001*2e2c009bSjjc 				npgs = mem_node_memlist_pages(mnode,
1002*2e2c009bSjjc 				    phys_install);
1003*2e2c009bSjjc 				break;
1004*2e2c009bSjjc 			default:
1005*2e2c009bSjjc 				break;
1006*2e2c009bSjjc 			}
1007*2e2c009bSjjc 		}
1008*2e2c009bSjjc 	}
1009*2e2c009bSjjc 	return (npgs);
1010*2e2c009bSjjc }
1011*2e2c009bSjjc 
1012*2e2c009bSjjc 
1013*2e2c009bSjjc /*
1014*2e2c009bSjjc  * Return the platform handle of the lgroup that contains the physical memory
1015*2e2c009bSjjc  * corresponding to the given page frame number
1016*2e2c009bSjjc  */
1017*2e2c009bSjjc /* ARGSUSED */
1018*2e2c009bSjjc lgrp_handle_t
1019*2e2c009bSjjc lgrp_plat_pfn_to_hand(pfn_t pfn)
1020*2e2c009bSjjc {
1021*2e2c009bSjjc 	int	mnode;
1022*2e2c009bSjjc 
1023*2e2c009bSjjc 	if (max_mem_nodes == 1)
1024*2e2c009bSjjc 		return (LGRP_DEFAULT_HANDLE);
1025*2e2c009bSjjc 
1026*2e2c009bSjjc 	if (pfn > physmax)
1027*2e2c009bSjjc 		return (LGRP_NULL_HANDLE);
1028*2e2c009bSjjc 
1029*2e2c009bSjjc 	mnode = plat_pfn_to_mem_node(pfn);
1030*2e2c009bSjjc 	if (mnode < 0)
1031*2e2c009bSjjc 		return (LGRP_NULL_HANDLE);
1032*2e2c009bSjjc 
1033*2e2c009bSjjc 	return (MEM_NODE_2_LGRPHAND(mnode));
1034*2e2c009bSjjc }
1035*2e2c009bSjjc 
1036*2e2c009bSjjc 
1037*2e2c009bSjjc /*
1038*2e2c009bSjjc  * Probe memory in each node from current CPU to determine latency topology
1039*2e2c009bSjjc  *
1040*2e2c009bSjjc  * The probing code will probe the vendor ID register on the Northbridge of
1041*2e2c009bSjjc  * Opteron processors and probe memory for other processors by default.
1042*2e2c009bSjjc  *
1043*2e2c009bSjjc  * Since probing is inherently error prone, the code takes laps across all the
1044*2e2c009bSjjc  * nodes probing from each node to each of the other nodes some number of
1045*2e2c009bSjjc  * times.  Furthermore, each node is probed some number of times before moving
1046*2e2c009bSjjc  * onto the next one during each lap.  The minimum latency gotten between nodes
1047*2e2c009bSjjc  * is kept as the latency between the nodes.
1048*2e2c009bSjjc  *
1049*2e2c009bSjjc  * After all that,  the probe times are adjusted by normalizing values that are
1050*2e2c009bSjjc  * close to each other and local latencies are made the same.  Lastly, the
1051*2e2c009bSjjc  * latencies are verified to make sure that certain conditions are met (eg.
1052*2e2c009bSjjc  * local < remote, latency(a, b) == latency(b, a), etc.).
1053*2e2c009bSjjc  *
1054*2e2c009bSjjc  * If any of the conditions aren't met, the code will export a NUMA
1055*2e2c009bSjjc  * configuration with the local CPUs and memory given by the SRAT or PCI config
1056*2e2c009bSjjc  * space registers and one remote memory latency since it can't tell exactly
1057*2e2c009bSjjc  * how far each node is from each other.
1058*2e2c009bSjjc  */
1059*2e2c009bSjjc void
1060*2e2c009bSjjc lgrp_plat_probe(void)
1061*2e2c009bSjjc {
1062*2e2c009bSjjc 	int				from;
1063*2e2c009bSjjc 	int				i;
1064*2e2c009bSjjc 	lgrp_plat_latency_stats_t	*lat_stats;
1065*2e2c009bSjjc 	hrtime_t			probe_time;
1066*2e2c009bSjjc 	int				to;
1067*2e2c009bSjjc 
1068*2e2c009bSjjc 	if (!(lgrp_plat_probe_flags & LGRP_PLAT_PROBE_ENABLE) ||
1069*2e2c009bSjjc 	    max_mem_nodes == 1 || lgrp_topo_ht_limit() <= 2)
1070*2e2c009bSjjc 		return;
1071*2e2c009bSjjc 
1072*2e2c009bSjjc 	/*
1073*2e2c009bSjjc 	 * Determine ID of node containing current CPU
1074*2e2c009bSjjc 	 */
1075*2e2c009bSjjc 	from = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node);
1076*2e2c009bSjjc 	ASSERT(from >= 0 && from < lgrp_plat_node_cnt);
1077*2e2c009bSjjc 	if (srat_ptr && lgrp_plat_srat_enable && !lgrp_plat_srat_error)
1078*2e2c009bSjjc 		ASSERT(lgrp_plat_node_domain[from].exists);
1079*2e2c009bSjjc 
1080*2e2c009bSjjc 	/*
1081*2e2c009bSjjc 	 * Don't need to probe if got times already
1082*2e2c009bSjjc 	 */
1083*2e2c009bSjjc 	lat_stats = &lgrp_plat_lat_stats;
1084*2e2c009bSjjc 	if (lat_stats->latencies[from][from] != 0)
1085*2e2c009bSjjc 		return;
1086*2e2c009bSjjc 
1087*2e2c009bSjjc 	/*
1088*2e2c009bSjjc 	 * Read vendor ID in Northbridge or read and write page(s)
1089*2e2c009bSjjc 	 * in each node from current CPU and remember how long it takes,
1090*2e2c009bSjjc 	 * so we can build latency topology of machine later.
1091*2e2c009bSjjc 	 * This should approximate the memory latency between each node.
1092*2e2c009bSjjc 	 */
1093*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_probe_nrounds; i++) {
1094*2e2c009bSjjc 		for (to = 0; to < lgrp_plat_node_cnt; to++) {
1095*2e2c009bSjjc 			/*
1096*2e2c009bSjjc 			 * Get probe time and bail out if can't get it yet
1097*2e2c009bSjjc 			 */
1098*2e2c009bSjjc 			probe_time = lgrp_plat_probe_time(to,
1099*2e2c009bSjjc 			    lgrp_plat_cpu_node, &lgrp_plat_probe_mem_config,
1100*2e2c009bSjjc 			    &lgrp_plat_lat_stats, &lgrp_plat_probe_stats);
1101*2e2c009bSjjc 			if (probe_time == 0)
1102*2e2c009bSjjc 				return;
1103*2e2c009bSjjc 
1104*2e2c009bSjjc 			/*
1105*2e2c009bSjjc 			 * Keep lowest probe time as latency between nodes
1106*2e2c009bSjjc 			 */
1107*2e2c009bSjjc 			if (lat_stats->latencies[from][to] == 0 ||
1108*2e2c009bSjjc 			    probe_time < lat_stats->latencies[from][to])
1109*2e2c009bSjjc 				lat_stats->latencies[from][to] = probe_time;
1110*2e2c009bSjjc 
1111*2e2c009bSjjc 			/*
1112*2e2c009bSjjc 			 * Update overall minimum and maximum probe times
1113*2e2c009bSjjc 			 * across all nodes
1114*2e2c009bSjjc 			 */
1115*2e2c009bSjjc 			if (probe_time < lat_stats->latency_min ||
1116*2e2c009bSjjc 			    lat_stats->latency_min == -1)
1117*2e2c009bSjjc 				lat_stats->latency_min = probe_time;
1118*2e2c009bSjjc 			if (probe_time > lat_stats->latency_max)
1119*2e2c009bSjjc 				lat_stats->latency_max = probe_time;
1120*2e2c009bSjjc 		}
1121*2e2c009bSjjc 	}
1122*2e2c009bSjjc 
1123*2e2c009bSjjc 	/*
1124*2e2c009bSjjc 	 * - Fix up latencies such that local latencies are same,
1125*2e2c009bSjjc 	 *   latency(i, j) == latency(j, i), etc. (if possible)
1126*2e2c009bSjjc 	 *
1127*2e2c009bSjjc 	 * - Verify that latencies look ok
1128*2e2c009bSjjc 	 *
1129*2e2c009bSjjc 	 * - Fallback to just optimizing for local and remote if
1130*2e2c009bSjjc 	 *   latencies didn't look right
1131*2e2c009bSjjc 	 */
1132*2e2c009bSjjc 	lgrp_plat_latency_adjust(lgrp_plat_node_memory, &lgrp_plat_lat_stats,
1133*2e2c009bSjjc 	    &lgrp_plat_probe_stats);
1134*2e2c009bSjjc 	lgrp_plat_probe_stats.probe_error_code =
1135*2e2c009bSjjc 	    lgrp_plat_latency_verify(lgrp_plat_node_memory,
1136*2e2c009bSjjc 	    &lgrp_plat_lat_stats);
1137*2e2c009bSjjc 	if (lgrp_plat_probe_stats.probe_error_code)
1138*2e2c009bSjjc 		lgrp_plat_2level_setup(lgrp_plat_node_memory,
1139*2e2c009bSjjc 		    &lgrp_plat_lat_stats);
1140*2e2c009bSjjc }
1141*2e2c009bSjjc 
1142*2e2c009bSjjc 
1143*2e2c009bSjjc /*
1144*2e2c009bSjjc  * Return platform handle for root lgroup
1145*2e2c009bSjjc  */
1146*2e2c009bSjjc lgrp_handle_t
1147*2e2c009bSjjc lgrp_plat_root_hand(void)
1148*2e2c009bSjjc {
1149*2e2c009bSjjc 	return (LGRP_DEFAULT_HANDLE);
1150*2e2c009bSjjc }
1151*2e2c009bSjjc 
1152*2e2c009bSjjc 
1153*2e2c009bSjjc /*
1154*2e2c009bSjjc  * INTERNAL ROUTINES
1155*2e2c009bSjjc  */
1156*2e2c009bSjjc 
1157*2e2c009bSjjc 
1158*2e2c009bSjjc /*
1159*2e2c009bSjjc  * Update CPU to node mapping for given CPU and proximity domain (and returns
1160*2e2c009bSjjc  * negative numbers for errors and positive ones for success)
1161*2e2c009bSjjc  */
1162*2e2c009bSjjc static int
1163*2e2c009bSjjc lgrp_plat_cpu_node_update(node_domain_map_t *node_domain,
1164*2e2c009bSjjc     cpu_node_map_t *cpu_node, uint32_t apicid, uint32_t domain)
1165*2e2c009bSjjc {
1166*2e2c009bSjjc 	uint_t	i;
1167*2e2c009bSjjc 	uint_t	start;
1168*2e2c009bSjjc 	int	node;
1169*2e2c009bSjjc 
1170*2e2c009bSjjc 	/*
1171*2e2c009bSjjc 	 * Get node number for proximity domain
1172*2e2c009bSjjc 	 */
1173*2e2c009bSjjc 	node = lgrp_plat_domain_to_node(node_domain, domain);
1174*2e2c009bSjjc 	if (node == -1) {
1175*2e2c009bSjjc 		node = lgrp_plat_node_domain_update(node_domain, domain);
1176*2e2c009bSjjc 		if (node == -1)
1177*2e2c009bSjjc 			return (-1);
1178*2e2c009bSjjc 	}
1179*2e2c009bSjjc 
1180*2e2c009bSjjc 	/*
1181*2e2c009bSjjc 	 * Hash given CPU APIC ID into CPU to node mapping table/array and
1182*2e2c009bSjjc 	 * enter it and its corresponding node and proximity domain IDs into
1183*2e2c009bSjjc 	 * first non-existent or matching entry
1184*2e2c009bSjjc 	 */
1185*2e2c009bSjjc 	i = start = CPU_NODE_HASH(apicid);
1186*2e2c009bSjjc 	do {
1187*2e2c009bSjjc 		if (cpu_node[i].exists) {
1188*2e2c009bSjjc 			/*
1189*2e2c009bSjjc 			 * Update already existing entry for CPU
1190*2e2c009bSjjc 			 */
1191*2e2c009bSjjc 			if (cpu_node[i].apicid == apicid) {
1192*2e2c009bSjjc 				/*
1193*2e2c009bSjjc 				 * Just return when everything same
1194*2e2c009bSjjc 				 */
1195*2e2c009bSjjc 				if (cpu_node[i].prox_domain == domain &&
1196*2e2c009bSjjc 				    cpu_node[i].node == node)
1197*2e2c009bSjjc 					return (1);
1198*2e2c009bSjjc 
1199*2e2c009bSjjc 				/*
1200*2e2c009bSjjc 				 * Assert that proximity domain and node IDs
1201*2e2c009bSjjc 				 * should be same and return error on non-debug
1202*2e2c009bSjjc 				 * kernel
1203*2e2c009bSjjc 				 */
1204*2e2c009bSjjc 				ASSERT(cpu_node[i].prox_domain == domain &&
1205*2e2c009bSjjc 				    cpu_node[i].node == node);
1206*2e2c009bSjjc 				return (-1);
1207*2e2c009bSjjc 			}
1208*2e2c009bSjjc 		} else {
1209*2e2c009bSjjc 			/*
1210*2e2c009bSjjc 			 * Create new entry for CPU
1211*2e2c009bSjjc 			 */
1212*2e2c009bSjjc 			cpu_node[i].exists = 1;
1213*2e2c009bSjjc 			cpu_node[i].apicid = apicid;
1214*2e2c009bSjjc 			cpu_node[i].prox_domain = domain;
1215*2e2c009bSjjc 			cpu_node[i].node = node;
1216*2e2c009bSjjc 			return (0);
1217*2e2c009bSjjc 		}
1218*2e2c009bSjjc 		i = CPU_NODE_HASH(i + 1);
1219*2e2c009bSjjc 	} while (i != start);
1220*2e2c009bSjjc 
1221*2e2c009bSjjc 	/*
1222*2e2c009bSjjc 	 * Ran out of supported number of entries which shouldn't happen....
1223*2e2c009bSjjc 	 */
1224*2e2c009bSjjc 	ASSERT(i != start);
1225*2e2c009bSjjc 	return (-1);
1226*2e2c009bSjjc }
1227*2e2c009bSjjc 
1228*2e2c009bSjjc 
1229*2e2c009bSjjc /*
1230*2e2c009bSjjc  * Get node ID for given CPU ID
1231*2e2c009bSjjc  */
1232*2e2c009bSjjc static int
1233*2e2c009bSjjc lgrp_plat_cpu_to_node(cpu_t *cp, cpu_node_map_t *cpu_node)
1234*2e2c009bSjjc {
1235*2e2c009bSjjc 	uint32_t	apicid;
1236*2e2c009bSjjc 	uint_t		i;
1237*2e2c009bSjjc 	uint_t		start;
1238*2e2c009bSjjc 
1239*2e2c009bSjjc 	if (cp == NULL)
1240*2e2c009bSjjc 		return (-1);
1241*2e2c009bSjjc 
1242*2e2c009bSjjc 	/*
1243*2e2c009bSjjc 	 * SRAT doesn't exist, isn't enabled, or there was an error processing
1244*2e2c009bSjjc 	 * it, so return chip ID for Opteron and -1 otherwise.
1245*2e2c009bSjjc 	 */
1246*2e2c009bSjjc 	if (srat_ptr == NULL || !lgrp_plat_srat_enable ||
1247*2e2c009bSjjc 	    lgrp_plat_srat_error) {
1248*2e2c009bSjjc 		if (is_opteron())
1249*2e2c009bSjjc 			return (pg_plat_hw_instance_id(cp, PGHW_CHIP));
1250*2e2c009bSjjc 		return (-1);
1251*2e2c009bSjjc 	}
1252*2e2c009bSjjc 
1253*2e2c009bSjjc 	/*
1254*2e2c009bSjjc 	 * SRAT does exist, so get APIC ID for given CPU and map that to its
1255*2e2c009bSjjc 	 * node ID
1256*2e2c009bSjjc 	 */
1257*2e2c009bSjjc 	apicid = cpuid_get_apicid(cp);
1258*2e2c009bSjjc 	i = start = CPU_NODE_HASH(apicid);
1259*2e2c009bSjjc 	do {
1260*2e2c009bSjjc 		if (cpu_node[i].apicid == apicid && cpu_node[i].exists)
1261*2e2c009bSjjc 			return (cpu_node[i].node);
1262*2e2c009bSjjc 		i = CPU_NODE_HASH(i + 1);
1263*2e2c009bSjjc 	} while (i != start);
1264*2e2c009bSjjc 	return (-1);
1265*2e2c009bSjjc }
1266*2e2c009bSjjc 
1267*2e2c009bSjjc 
1268*2e2c009bSjjc /*
1269*2e2c009bSjjc  * Return node number for given proximity domain/system locality
1270*2e2c009bSjjc  */
1271*2e2c009bSjjc static int
1272*2e2c009bSjjc lgrp_plat_domain_to_node(node_domain_map_t *node_domain, uint32_t domain)
1273*2e2c009bSjjc {
1274*2e2c009bSjjc 	uint_t	node;
1275*2e2c009bSjjc 	uint_t	start;
1276*2e2c009bSjjc 
1277*2e2c009bSjjc 	/*
1278*2e2c009bSjjc 	 * Hash proximity domain ID into node to domain mapping table (array),
1279*2e2c009bSjjc 	 * search for entry with matching proximity domain ID, and return index
1280*2e2c009bSjjc 	 * of matching entry as node ID.
1281*2e2c009bSjjc 	 */
1282*2e2c009bSjjc 	node = start = NODE_DOMAIN_HASH(domain);
1283*2e2c009bSjjc 	do {
1284*2e2c009bSjjc 		if (node_domain[node].prox_domain == domain &&
1285*2e2c009bSjjc 		    node_domain[node].exists)
1286*2e2c009bSjjc 			return (node);
1287*2e2c009bSjjc 		node = NODE_DOMAIN_HASH(node + 1);
1288*2e2c009bSjjc 	} while (node != start);
1289*2e2c009bSjjc 	return (-1);
1290*2e2c009bSjjc }
1291*2e2c009bSjjc 
1292*2e2c009bSjjc 
1293*2e2c009bSjjc /*
1294*2e2c009bSjjc  * Latencies must be within 1/(2**LGRP_LAT_TOLERANCE_SHIFT) of each other to
1295*2e2c009bSjjc  * be considered same
1296*2e2c009bSjjc  */
1297*2e2c009bSjjc #define	LGRP_LAT_TOLERANCE_SHIFT	4
1298*2e2c009bSjjc 
1299*2e2c009bSjjc int	lgrp_plat_probe_lt_shift = LGRP_LAT_TOLERANCE_SHIFT;
1300*2e2c009bSjjc 
1301*2e2c009bSjjc 
1302*2e2c009bSjjc /*
1303*2e2c009bSjjc  * Adjust latencies between nodes to be symmetric, normalize latencies between
1304*2e2c009bSjjc  * any nodes that are within some tolerance to be same, and make local
1305*2e2c009bSjjc  * latencies be same
1306*2e2c009bSjjc  */
1307*2e2c009bSjjc static void
1308*2e2c009bSjjc lgrp_plat_latency_adjust(node_phys_addr_map_t *node_memory,
1309*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats, lgrp_plat_probe_stats_t *probe_stats)
1310*2e2c009bSjjc {
1311*2e2c009bSjjc 	int				i;
1312*2e2c009bSjjc 	int				j;
1313*2e2c009bSjjc 	int				k;
1314*2e2c009bSjjc 	int				l;
1315*2e2c009bSjjc 	u_longlong_t			max;
1316*2e2c009bSjjc 	u_longlong_t			min;
1317*2e2c009bSjjc 	u_longlong_t			t;
1318*2e2c009bSjjc 	u_longlong_t			t1;
1319*2e2c009bSjjc 	u_longlong_t			t2;
1320*2e2c009bSjjc 	const lgrp_config_flag_t	cflag = LGRP_CONFIG_LAT_CHANGE_ALL;
1321*2e2c009bSjjc 	int				lat_corrected[MAX_NODES][MAX_NODES];
1322*2e2c009bSjjc 
1323*2e2c009bSjjc 	/*
1324*2e2c009bSjjc 	 * Nothing to do when this is an UMA machine or don't have args needed
1325*2e2c009bSjjc 	 */
1326*2e2c009bSjjc 	if (max_mem_nodes == 1)
1327*2e2c009bSjjc 		return;
1328*2e2c009bSjjc 
1329*2e2c009bSjjc 	ASSERT(node_memory != NULL && lat_stats != NULL &&
1330*2e2c009bSjjc 	    probe_stats != NULL);
1331*2e2c009bSjjc 
1332*2e2c009bSjjc 	/*
1333*2e2c009bSjjc 	 * Make sure that latencies are symmetric between any two nodes
1334*2e2c009bSjjc 	 * (ie. latency(node0, node1) == latency(node1, node0))
1335*2e2c009bSjjc 	 */
1336*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
1337*2e2c009bSjjc 		if (!node_memory[i].exists)
1338*2e2c009bSjjc 			continue;
1339*2e2c009bSjjc 
1340*2e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
1341*2e2c009bSjjc 			if (!node_memory[j].exists)
1342*2e2c009bSjjc 				continue;
1343*2e2c009bSjjc 
1344*2e2c009bSjjc 			t1 = lat_stats->latencies[i][j];
1345*2e2c009bSjjc 			t2 = lat_stats->latencies[j][i];
1346*2e2c009bSjjc 
1347*2e2c009bSjjc 			if (t1 == 0 || t2 == 0 || t1 == t2)
1348*2e2c009bSjjc 				continue;
1349*2e2c009bSjjc 
1350*2e2c009bSjjc 			/*
1351*2e2c009bSjjc 			 * Latencies should be same
1352*2e2c009bSjjc 			 * - Use minimum of two latencies which should be same
1353*2e2c009bSjjc 			 * - Track suspect probe times not within tolerance of
1354*2e2c009bSjjc 			 *   min value
1355*2e2c009bSjjc 			 * - Remember how much values are corrected by
1356*2e2c009bSjjc 			 */
1357*2e2c009bSjjc 			if (t1 > t2) {
1358*2e2c009bSjjc 				t = t2;
1359*2e2c009bSjjc 				probe_stats->probe_errors[i][j] += t1 - t2;
1360*2e2c009bSjjc 				if (t1 - t2 > t2 >> lgrp_plat_probe_lt_shift) {
1361*2e2c009bSjjc 					probe_stats->probe_suspect[i][j]++;
1362*2e2c009bSjjc 					probe_stats->probe_suspect[j][i]++;
1363*2e2c009bSjjc 				}
1364*2e2c009bSjjc 			} else if (t2 > t1) {
1365*2e2c009bSjjc 				t = t1;
1366*2e2c009bSjjc 				probe_stats->probe_errors[j][i] += t2 - t1;
1367*2e2c009bSjjc 				if (t2 - t1 > t1 >> lgrp_plat_probe_lt_shift) {
1368*2e2c009bSjjc 					probe_stats->probe_suspect[i][j]++;
1369*2e2c009bSjjc 					probe_stats->probe_suspect[j][i]++;
1370*2e2c009bSjjc 				}
1371*2e2c009bSjjc 			}
1372*2e2c009bSjjc 
1373*2e2c009bSjjc 			lat_stats->latencies[i][j] =
1374*2e2c009bSjjc 			    lat_stats->latencies[j][i] = t;
1375*2e2c009bSjjc 			lgrp_config(cflag, t1, t);
1376*2e2c009bSjjc 			lgrp_config(cflag, t2, t);
1377*2e2c009bSjjc 		}
1378*2e2c009bSjjc 	}
1379*2e2c009bSjjc 
1380*2e2c009bSjjc 	/*
1381*2e2c009bSjjc 	 * Keep track of which latencies get corrected
1382*2e2c009bSjjc 	 */
1383*2e2c009bSjjc 	for (i = 0; i < MAX_NODES; i++)
1384*2e2c009bSjjc 		for (j = 0; j < MAX_NODES; j++)
1385*2e2c009bSjjc 			lat_corrected[i][j] = 0;
1386*2e2c009bSjjc 
1387*2e2c009bSjjc 	/*
1388*2e2c009bSjjc 	 * For every two nodes, see whether there is another pair of nodes which
1389*2e2c009bSjjc 	 * are about the same distance apart and make the latencies be the same
1390*2e2c009bSjjc 	 * if they are close enough together
1391*2e2c009bSjjc 	 */
1392*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
1393*2e2c009bSjjc 		if (!node_memory[i].exists)
1394*2e2c009bSjjc 			continue;
1395*2e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
1396*2e2c009bSjjc 			if (!node_memory[j].exists)
1397*2e2c009bSjjc 				continue;
1398*2e2c009bSjjc 			/*
1399*2e2c009bSjjc 			 * Pick one pair of nodes (i, j)
1400*2e2c009bSjjc 			 * and get latency between them
1401*2e2c009bSjjc 			 */
1402*2e2c009bSjjc 			t1 = lat_stats->latencies[i][j];
1403*2e2c009bSjjc 
1404*2e2c009bSjjc 			/*
1405*2e2c009bSjjc 			 * Skip this pair of nodes if there isn't a latency
1406*2e2c009bSjjc 			 * for it yet
1407*2e2c009bSjjc 			 */
1408*2e2c009bSjjc 			if (t1 == 0)
1409*2e2c009bSjjc 				continue;
1410*2e2c009bSjjc 
1411*2e2c009bSjjc 			for (k = 0; k < lgrp_plat_node_cnt; k++) {
1412*2e2c009bSjjc 				if (!node_memory[k].exists)
1413*2e2c009bSjjc 					continue;
1414*2e2c009bSjjc 				for (l = 0; l < lgrp_plat_node_cnt; l++) {
1415*2e2c009bSjjc 					if (!node_memory[l].exists)
1416*2e2c009bSjjc 						continue;
1417*2e2c009bSjjc 					/*
1418*2e2c009bSjjc 					 * Pick another pair of nodes (k, l)
1419*2e2c009bSjjc 					 * not same as (i, j) and get latency
1420*2e2c009bSjjc 					 * between them
1421*2e2c009bSjjc 					 */
1422*2e2c009bSjjc 					if (k == i && l == j)
1423*2e2c009bSjjc 						continue;
1424*2e2c009bSjjc 
1425*2e2c009bSjjc 					t2 = lat_stats->latencies[k][l];
1426*2e2c009bSjjc 
1427*2e2c009bSjjc 					/*
1428*2e2c009bSjjc 					 * Skip this pair of nodes if there
1429*2e2c009bSjjc 					 * isn't a latency for it yet
1430*2e2c009bSjjc 					 */
1431*2e2c009bSjjc 
1432*2e2c009bSjjc 					if (t2 == 0)
1433*2e2c009bSjjc 						continue;
1434*2e2c009bSjjc 
1435*2e2c009bSjjc 					/*
1436*2e2c009bSjjc 					 * Skip nodes (k, l) if they already
1437*2e2c009bSjjc 					 * have same latency as (i, j) or
1438*2e2c009bSjjc 					 * their latency isn't close enough to
1439*2e2c009bSjjc 					 * be considered/made the same
1440*2e2c009bSjjc 					 */
1441*2e2c009bSjjc 					if (t1 == t2 || (t1 > t2 && t1 - t2 >
1442*2e2c009bSjjc 					    t1 >> lgrp_plat_probe_lt_shift) ||
1443*2e2c009bSjjc 					    (t2 > t1 && t2 - t1 >
1444*2e2c009bSjjc 					    t2 >> lgrp_plat_probe_lt_shift))
1445*2e2c009bSjjc 						continue;
1446*2e2c009bSjjc 
1447*2e2c009bSjjc 					/*
1448*2e2c009bSjjc 					 * Make latency(i, j) same as
1449*2e2c009bSjjc 					 * latency(k, l), try to use latency
1450*2e2c009bSjjc 					 * that has been adjusted already to get
1451*2e2c009bSjjc 					 * more consistency (if possible), and
1452*2e2c009bSjjc 					 * remember which latencies were
1453*2e2c009bSjjc 					 * adjusted for next time
1454*2e2c009bSjjc 					 */
1455*2e2c009bSjjc 					if (lat_corrected[i][j]) {
1456*2e2c009bSjjc 						t = t1;
1457*2e2c009bSjjc 						lgrp_config(cflag, t2, t);
1458*2e2c009bSjjc 						t2 = t;
1459*2e2c009bSjjc 					} else if (lat_corrected[k][l]) {
1460*2e2c009bSjjc 						t = t2;
1461*2e2c009bSjjc 						lgrp_config(cflag, t1, t);
1462*2e2c009bSjjc 						t1 = t;
1463*2e2c009bSjjc 					} else {
1464*2e2c009bSjjc 						if (t1 > t2)
1465*2e2c009bSjjc 							t = t2;
1466*2e2c009bSjjc 						else
1467*2e2c009bSjjc 							t = t1;
1468*2e2c009bSjjc 						lgrp_config(cflag, t1, t);
1469*2e2c009bSjjc 						lgrp_config(cflag, t2, t);
1470*2e2c009bSjjc 						t1 = t2 = t;
1471*2e2c009bSjjc 					}
1472*2e2c009bSjjc 
1473*2e2c009bSjjc 					lat_stats->latencies[i][j] =
1474*2e2c009bSjjc 					    lat_stats->latencies[k][l] = t;
1475*2e2c009bSjjc 
1476*2e2c009bSjjc 					lat_corrected[i][j] =
1477*2e2c009bSjjc 					    lat_corrected[k][l] = 1;
1478*2e2c009bSjjc 				}
1479*2e2c009bSjjc 			}
1480*2e2c009bSjjc 		}
1481*2e2c009bSjjc 	}
1482*2e2c009bSjjc 
1483*2e2c009bSjjc 	/*
1484*2e2c009bSjjc 	 * Local latencies should be same
1485*2e2c009bSjjc 	 * - Find min and max local latencies
1486*2e2c009bSjjc 	 * - Make all local latencies be minimum
1487*2e2c009bSjjc 	 */
1488*2e2c009bSjjc 	min = -1;
1489*2e2c009bSjjc 	max = 0;
1490*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
1491*2e2c009bSjjc 		if (!node_memory[i].exists)
1492*2e2c009bSjjc 			continue;
1493*2e2c009bSjjc 		t = lat_stats->latencies[i][i];
1494*2e2c009bSjjc 		if (t == 0)
1495*2e2c009bSjjc 			continue;
1496*2e2c009bSjjc 		if (min == -1 || t < min)
1497*2e2c009bSjjc 			min = t;
1498*2e2c009bSjjc 		if (t > max)
1499*2e2c009bSjjc 			max = t;
1500*2e2c009bSjjc 	}
1501*2e2c009bSjjc 	if (min != max) {
1502*2e2c009bSjjc 		for (i = 0; i < lgrp_plat_node_cnt; i++) {
1503*2e2c009bSjjc 			int	local;
1504*2e2c009bSjjc 
1505*2e2c009bSjjc 			if (!node_memory[i].exists)
1506*2e2c009bSjjc 				continue;
1507*2e2c009bSjjc 
1508*2e2c009bSjjc 			local = lat_stats->latencies[i][i];
1509*2e2c009bSjjc 			if (local == 0)
1510*2e2c009bSjjc 				continue;
1511*2e2c009bSjjc 
1512*2e2c009bSjjc 			/*
1513*2e2c009bSjjc 			 * Track suspect probe times that aren't within
1514*2e2c009bSjjc 			 * tolerance of minimum local latency and how much
1515*2e2c009bSjjc 			 * probe times are corrected by
1516*2e2c009bSjjc 			 */
1517*2e2c009bSjjc 			if (local - min > min >> lgrp_plat_probe_lt_shift)
1518*2e2c009bSjjc 				probe_stats->probe_suspect[i][i]++;
1519*2e2c009bSjjc 
1520*2e2c009bSjjc 			probe_stats->probe_errors[i][i] += local - min;
1521*2e2c009bSjjc 
1522*2e2c009bSjjc 			/*
1523*2e2c009bSjjc 			 * Make local latencies be minimum
1524*2e2c009bSjjc 			 */
1525*2e2c009bSjjc 			lgrp_config(LGRP_CONFIG_LAT_CHANGE, i, min);
1526*2e2c009bSjjc 			lat_stats->latencies[i][i] = min;
1527*2e2c009bSjjc 		}
1528*2e2c009bSjjc 	}
1529*2e2c009bSjjc 
1530*2e2c009bSjjc 	/*
1531*2e2c009bSjjc 	 * Determine max probe time again since just adjusted latencies
1532*2e2c009bSjjc 	 */
1533*2e2c009bSjjc 	lat_stats->latency_max = 0;
1534*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
1535*2e2c009bSjjc 		if (!node_memory[i].exists)
1536*2e2c009bSjjc 			continue;
1537*2e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
1538*2e2c009bSjjc 			if (!node_memory[j].exists)
1539*2e2c009bSjjc 				continue;
1540*2e2c009bSjjc 			t = lat_stats->latencies[i][j];
1541*2e2c009bSjjc 			if (t > lat_stats->latency_max)
1542*2e2c009bSjjc 				lat_stats->latency_max = t;
1543*2e2c009bSjjc 		}
1544*2e2c009bSjjc 	}
1545*2e2c009bSjjc }
1546*2e2c009bSjjc 
1547*2e2c009bSjjc 
1548*2e2c009bSjjc /*
1549*2e2c009bSjjc  * Verify following about latencies between nodes:
1550*2e2c009bSjjc  *
1551*2e2c009bSjjc  * - Latencies should be symmetric (ie. latency(a, b) == latency(b, a))
1552*2e2c009bSjjc  * - Local latencies same
1553*2e2c009bSjjc  * - Local < remote
1554*2e2c009bSjjc  * - Number of latencies seen is reasonable
1555*2e2c009bSjjc  * - Number of occurrences of a given latency should be more than 1
1556*2e2c009bSjjc  *
1557*2e2c009bSjjc  * Returns:
1558*2e2c009bSjjc  *	0	Success
1559*2e2c009bSjjc  *	-1	Not symmetric
1560*2e2c009bSjjc  *	-2	Local latencies not same
1561*2e2c009bSjjc  *	-3	Local >= remote
1562*2e2c009bSjjc  */
1563*2e2c009bSjjc static int
1564*2e2c009bSjjc lgrp_plat_latency_verify(node_phys_addr_map_t *node_memory,
1565*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats)
1566*2e2c009bSjjc {
1567*2e2c009bSjjc 	int				i;
1568*2e2c009bSjjc 	int				j;
1569*2e2c009bSjjc 	u_longlong_t			t1;
1570*2e2c009bSjjc 	u_longlong_t			t2;
1571*2e2c009bSjjc 
1572*2e2c009bSjjc 	ASSERT(node_memory != NULL && lat_stats != NULL);
1573*2e2c009bSjjc 
1574*2e2c009bSjjc 	/*
1575*2e2c009bSjjc 	 * Nothing to do when this is an UMA machine, lgroup topology is
1576*2e2c009bSjjc 	 * limited to 2 levels, or there aren't any probe times yet
1577*2e2c009bSjjc 	 */
1578*2e2c009bSjjc 	if (max_mem_nodes == 1 || lgrp_topo_levels < 2 ||
1579*2e2c009bSjjc 	    lat_stats->latencies[0][0] == 0)
1580*2e2c009bSjjc 		return (0);
1581*2e2c009bSjjc 
1582*2e2c009bSjjc 	/*
1583*2e2c009bSjjc 	 * Make sure that latencies are symmetric between any two nodes
1584*2e2c009bSjjc 	 * (ie. latency(node0, node1) == latency(node1, node0))
1585*2e2c009bSjjc 	 */
1586*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
1587*2e2c009bSjjc 		if (!node_memory[i].exists)
1588*2e2c009bSjjc 			continue;
1589*2e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
1590*2e2c009bSjjc 			if (!node_memory[j].exists)
1591*2e2c009bSjjc 				continue;
1592*2e2c009bSjjc 			t1 = lat_stats->latencies[i][j];
1593*2e2c009bSjjc 			t2 = lat_stats->latencies[j][i];
1594*2e2c009bSjjc 
1595*2e2c009bSjjc 			if (t1 == 0 || t2 == 0 || t1 == t2)
1596*2e2c009bSjjc 				continue;
1597*2e2c009bSjjc 
1598*2e2c009bSjjc 			return (-1);
1599*2e2c009bSjjc 		}
1600*2e2c009bSjjc 	}
1601*2e2c009bSjjc 
1602*2e2c009bSjjc 	/*
1603*2e2c009bSjjc 	 * Local latencies should be same
1604*2e2c009bSjjc 	 */
1605*2e2c009bSjjc 	t1 = lat_stats->latencies[0][0];
1606*2e2c009bSjjc 	for (i = 1; i < lgrp_plat_node_cnt; i++) {
1607*2e2c009bSjjc 		if (!node_memory[i].exists)
1608*2e2c009bSjjc 			continue;
1609*2e2c009bSjjc 
1610*2e2c009bSjjc 		t2 = lat_stats->latencies[i][i];
1611*2e2c009bSjjc 		if (t2 == 0)
1612*2e2c009bSjjc 			continue;
1613*2e2c009bSjjc 
1614*2e2c009bSjjc 		if (t1 == 0) {
1615*2e2c009bSjjc 			t1 = t2;
1616*2e2c009bSjjc 			continue;
1617*2e2c009bSjjc 		}
1618*2e2c009bSjjc 
1619*2e2c009bSjjc 		if (t1 != t2)
1620*2e2c009bSjjc 			return (-2);
1621*2e2c009bSjjc 	}
1622*2e2c009bSjjc 
1623*2e2c009bSjjc 	/*
1624*2e2c009bSjjc 	 * Local latencies should be less than remote
1625*2e2c009bSjjc 	 */
1626*2e2c009bSjjc 	if (t1) {
1627*2e2c009bSjjc 		for (i = 0; i < lgrp_plat_node_cnt; i++) {
1628*2e2c009bSjjc 			if (!node_memory[i].exists)
1629*2e2c009bSjjc 				continue;
1630*2e2c009bSjjc 			for (j = 0; j < lgrp_plat_node_cnt; j++) {
1631*2e2c009bSjjc 				if (!node_memory[j].exists)
1632*2e2c009bSjjc 					continue;
1633*2e2c009bSjjc 				t2 = lat_stats->latencies[i][j];
1634*2e2c009bSjjc 				if (i == j || t2 == 0)
1635*2e2c009bSjjc 					continue;
1636*2e2c009bSjjc 
1637*2e2c009bSjjc 				if (t1 >= t2)
1638*2e2c009bSjjc 					return (-3);
1639*2e2c009bSjjc 			}
1640*2e2c009bSjjc 		}
1641*2e2c009bSjjc 	}
1642*2e2c009bSjjc 
1643*2e2c009bSjjc 	return (0);
1644*2e2c009bSjjc }
1645*2e2c009bSjjc 
1646*2e2c009bSjjc 
1647*2e2c009bSjjc /*
1648*2e2c009bSjjc  * Return the number of free, allocatable, or installed
1649*2e2c009bSjjc  * pages in an lgroup
1650*2e2c009bSjjc  * This is a copy of the MAX_MEM_NODES == 1 version of the routine
1651*2e2c009bSjjc  * used when MPO is disabled (i.e. single lgroup) or this is the root lgroup
1652*2e2c009bSjjc  */
1653*2e2c009bSjjc /* ARGSUSED */
1654*2e2c009bSjjc static pgcnt_t
1655*2e2c009bSjjc lgrp_plat_mem_size_default(lgrp_handle_t lgrphand, lgrp_mem_query_t query)
1656*2e2c009bSjjc {
1657*2e2c009bSjjc 	struct memlist *mlist;
1658*2e2c009bSjjc 	pgcnt_t npgs = 0;
1659*2e2c009bSjjc 	extern struct memlist *phys_avail;
1660*2e2c009bSjjc 	extern struct memlist *phys_install;
1661*2e2c009bSjjc 
1662*2e2c009bSjjc 	switch (query) {
1663*2e2c009bSjjc 	case LGRP_MEM_SIZE_FREE:
1664*2e2c009bSjjc 		return ((pgcnt_t)freemem);
1665*2e2c009bSjjc 	case LGRP_MEM_SIZE_AVAIL:
1666*2e2c009bSjjc 		memlist_read_lock();
1667*2e2c009bSjjc 		for (mlist = phys_avail; mlist; mlist = mlist->next)
1668*2e2c009bSjjc 			npgs += btop(mlist->size);
1669*2e2c009bSjjc 		memlist_read_unlock();
1670*2e2c009bSjjc 		return (npgs);
1671*2e2c009bSjjc 	case LGRP_MEM_SIZE_INSTALL:
1672*2e2c009bSjjc 		memlist_read_lock();
1673*2e2c009bSjjc 		for (mlist = phys_install; mlist; mlist = mlist->next)
1674*2e2c009bSjjc 			npgs += btop(mlist->size);
1675*2e2c009bSjjc 		memlist_read_unlock();
1676*2e2c009bSjjc 		return (npgs);
1677*2e2c009bSjjc 	default:
1678*2e2c009bSjjc 		return ((pgcnt_t)0);
1679*2e2c009bSjjc 	}
1680*2e2c009bSjjc }
1681*2e2c009bSjjc 
1682*2e2c009bSjjc 
1683*2e2c009bSjjc /*
1684*2e2c009bSjjc  * Update node to proximity domain mappings for given domain and return node ID
1685*2e2c009bSjjc  */
1686*2e2c009bSjjc static int
1687*2e2c009bSjjc lgrp_plat_node_domain_update(node_domain_map_t *node_domain, uint32_t domain)
1688*2e2c009bSjjc {
1689*2e2c009bSjjc 	uint_t	node;
1690*2e2c009bSjjc 	uint_t	start;
1691*2e2c009bSjjc 
1692*2e2c009bSjjc 	/*
1693*2e2c009bSjjc 	 * Hash proximity domain ID into node to domain mapping table (array)
1694*2e2c009bSjjc 	 * and add entry for it into first non-existent or matching entry found
1695*2e2c009bSjjc 	 */
1696*2e2c009bSjjc 	node = start = NODE_DOMAIN_HASH(domain);
1697*2e2c009bSjjc 	do {
1698*2e2c009bSjjc 		/*
1699*2e2c009bSjjc 		 * Entry doesn't exist yet, so create one for this proximity
1700*2e2c009bSjjc 		 * domain and return node ID which is index into mapping table.
1701*2e2c009bSjjc 		 */
1702*2e2c009bSjjc 		if (!node_domain[node].exists) {
1703*2e2c009bSjjc 			node_domain[node].exists = 1;
1704*2e2c009bSjjc 			node_domain[node].prox_domain = domain;
1705*2e2c009bSjjc 			return (node);
1706*2e2c009bSjjc 		}
1707*2e2c009bSjjc 
1708*2e2c009bSjjc 		/*
1709*2e2c009bSjjc 		 * Entry exists for this proximity domain already, so just
1710*2e2c009bSjjc 		 * return node ID (index into table).
1711*2e2c009bSjjc 		 */
1712*2e2c009bSjjc 		if (node_domain[node].prox_domain == domain)
1713*2e2c009bSjjc 			return (node);
1714*2e2c009bSjjc 		node = NODE_DOMAIN_HASH(node + 1);
1715*2e2c009bSjjc 	} while (node != start);
1716*2e2c009bSjjc 
1717*2e2c009bSjjc 	/*
1718*2e2c009bSjjc 	 * Ran out of supported number of entries which shouldn't happen....
1719*2e2c009bSjjc 	 */
1720*2e2c009bSjjc 	ASSERT(node != start);
1721*2e2c009bSjjc 	return (-1);
1722*2e2c009bSjjc }
1723*2e2c009bSjjc 
1724*2e2c009bSjjc 
1725*2e2c009bSjjc /*
1726*2e2c009bSjjc  * Update node memory information for given proximity domain with specified
1727*2e2c009bSjjc  * starting and ending physical address range (and return positive numbers for
1728*2e2c009bSjjc  * success and negative ones for errors)
1729*2e2c009bSjjc  */
1730*2e2c009bSjjc static int
1731*2e2c009bSjjc lgrp_plat_node_memory_update(node_domain_map_t *node_domain,
1732*2e2c009bSjjc     node_phys_addr_map_t *node_memory, uintptr_t start, uintptr_t end,
1733*2e2c009bSjjc     uint32_t domain)
1734*2e2c009bSjjc {
1735*2e2c009bSjjc 	int	node;
1736*2e2c009bSjjc 
1737*2e2c009bSjjc 	/*
1738*2e2c009bSjjc 	 * Get node number for proximity domain
1739*2e2c009bSjjc 	 */
1740*2e2c009bSjjc 	node = lgrp_plat_domain_to_node(node_domain, domain);
1741*2e2c009bSjjc 	if (node == -1) {
1742*2e2c009bSjjc 		node = lgrp_plat_node_domain_update(node_domain, domain);
1743*2e2c009bSjjc 		if (node == -1)
1744*2e2c009bSjjc 			return (-1);
1745*2e2c009bSjjc 	}
1746*2e2c009bSjjc 
1747*2e2c009bSjjc 	/*
1748*2e2c009bSjjc 	 * Create entry in table for node if it doesn't exist
1749*2e2c009bSjjc 	 */
1750*2e2c009bSjjc 	if (!node_memory[node].exists) {
1751*2e2c009bSjjc 		node_memory[node].exists = 1;
1752*2e2c009bSjjc 		node_memory[node].start = btop(start);
1753*2e2c009bSjjc 		node_memory[node].end = btop(end);
1754*2e2c009bSjjc 		node_memory[node].prox_domain = domain;
1755*2e2c009bSjjc 		return (0);
1756*2e2c009bSjjc 	}
1757*2e2c009bSjjc 
1758*2e2c009bSjjc 	/*
1759*2e2c009bSjjc 	 * Entry already exists for this proximity domain
1760*2e2c009bSjjc 	 *
1761*2e2c009bSjjc 	 * There may be more than one SRAT memory entry for a domain, so we may
1762*2e2c009bSjjc 	 * need to update existing start or end address for the node.
1763*2e2c009bSjjc 	 */
1764*2e2c009bSjjc 	if (node_memory[node].prox_domain == domain) {
1765*2e2c009bSjjc 		if (btop(start) < node_memory[node].start)
1766*2e2c009bSjjc 			node_memory[node].start = btop(start);
1767*2e2c009bSjjc 		if (btop(end) > node_memory[node].end)
1768*2e2c009bSjjc 			node_memory[node].end = btop(end);
1769*2e2c009bSjjc 		return (1);
1770*2e2c009bSjjc 	}
1771*2e2c009bSjjc 	return (-2);
1772*2e2c009bSjjc }
1773*2e2c009bSjjc 
1774*2e2c009bSjjc 
1775*2e2c009bSjjc /*
1776*2e2c009bSjjc  * Return time needed to probe from current CPU to memory in given node
1777*2e2c009bSjjc  */
1778*2e2c009bSjjc static hrtime_t
1779*2e2c009bSjjc lgrp_plat_probe_time(int to, cpu_node_map_t *cpu_node,
1780*2e2c009bSjjc     lgrp_plat_probe_mem_config_t *probe_mem_config,
1781*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats, lgrp_plat_probe_stats_t *probe_stats)
1782*2e2c009bSjjc {
1783*2e2c009bSjjc 	caddr_t			buf;
1784*2e2c009bSjjc 	hrtime_t		elapsed;
1785*2e2c009bSjjc 	hrtime_t		end;
1786*2e2c009bSjjc 	int			from;
1787*2e2c009bSjjc 	int			i;
1788*2e2c009bSjjc 	int			ipl;
1789*2e2c009bSjjc 	hrtime_t		max;
1790*2e2c009bSjjc 	hrtime_t		min;
1791*2e2c009bSjjc 	hrtime_t		start;
1792*2e2c009bSjjc 	extern int		use_sse_pagecopy;
1793*2e2c009bSjjc 
1794*2e2c009bSjjc 	/*
1795*2e2c009bSjjc 	 * Determine ID of node containing current CPU
1796*2e2c009bSjjc 	 */
1797*2e2c009bSjjc 	from = lgrp_plat_cpu_to_node(CPU, cpu_node);
1798*2e2c009bSjjc 	ASSERT(from >= 0 && from < lgrp_plat_node_cnt);
1799*2e2c009bSjjc 
1800*2e2c009bSjjc 	/*
1801*2e2c009bSjjc 	 * Do common work for probing main memory
1802*2e2c009bSjjc 	 */
1803*2e2c009bSjjc 	if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_PGCPY) {
1804*2e2c009bSjjc 		/*
1805*2e2c009bSjjc 		 * Skip probing any nodes without memory and
1806*2e2c009bSjjc 		 * set probe time to 0
1807*2e2c009bSjjc 		 */
1808*2e2c009bSjjc 		if (probe_mem_config->probe_va[to] == NULL) {
1809*2e2c009bSjjc 			lat_stats->latencies[from][to] = 0;
1810*2e2c009bSjjc 			return (0);
1811*2e2c009bSjjc 		}
1812*2e2c009bSjjc 
1813*2e2c009bSjjc 		/*
1814*2e2c009bSjjc 		 * Invalidate caches once instead of once every sample
1815*2e2c009bSjjc 		 * which should cut cost of probing by a lot
1816*2e2c009bSjjc 		 */
1817*2e2c009bSjjc 		probe_stats->flush_cost = gethrtime();
1818*2e2c009bSjjc 		invalidate_cache();
1819*2e2c009bSjjc 		probe_stats->flush_cost = gethrtime() -
1820*2e2c009bSjjc 		    probe_stats->flush_cost;
1821*2e2c009bSjjc 		probe_stats->probe_cost_total += probe_stats->flush_cost;
1822*2e2c009bSjjc 	}
1823*2e2c009bSjjc 
1824*2e2c009bSjjc 	/*
1825*2e2c009bSjjc 	 * Probe from current CPU to given memory using specified operation
1826*2e2c009bSjjc 	 * and take specified number of samples
1827*2e2c009bSjjc 	 */
1828*2e2c009bSjjc 	max = 0;
1829*2e2c009bSjjc 	min = -1;
1830*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_probe_nsamples; i++) {
1831*2e2c009bSjjc 		probe_stats->probe_cost = gethrtime();
1832*2e2c009bSjjc 
1833*2e2c009bSjjc 		/*
1834*2e2c009bSjjc 		 * Can't measure probe time if gethrtime() isn't working yet
1835*2e2c009bSjjc 		 */
1836*2e2c009bSjjc 		if (probe_stats->probe_cost == 0 && gethrtime() == 0)
1837*2e2c009bSjjc 			return (0);
1838*2e2c009bSjjc 
1839*2e2c009bSjjc 		if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_VENDOR) {
1840*2e2c009bSjjc 			/*
1841*2e2c009bSjjc 			 * Measure how long it takes to read vendor ID from
1842*2e2c009bSjjc 			 * Northbridge
1843*2e2c009bSjjc 			 */
1844*2e2c009bSjjc 			elapsed = opt_probe_vendor(to, lgrp_plat_probe_nreads);
1845*2e2c009bSjjc 		} else {
1846*2e2c009bSjjc 			/*
1847*2e2c009bSjjc 			 * Measure how long it takes to copy page
1848*2e2c009bSjjc 			 * on top of itself
1849*2e2c009bSjjc 			 */
1850*2e2c009bSjjc 			buf = probe_mem_config->probe_va[to] + (i * PAGESIZE);
1851*2e2c009bSjjc 
1852*2e2c009bSjjc 			kpreempt_disable();
1853*2e2c009bSjjc 			ipl = splhigh();
1854*2e2c009bSjjc 			start = gethrtime();
1855*2e2c009bSjjc 			if (use_sse_pagecopy)
1856*2e2c009bSjjc 				hwblkpagecopy(buf, buf);
1857*2e2c009bSjjc 			else
1858*2e2c009bSjjc 				bcopy(buf, buf, PAGESIZE);
1859*2e2c009bSjjc 			end = gethrtime();
1860*2e2c009bSjjc 			elapsed = end - start;
1861*2e2c009bSjjc 			splx(ipl);
1862*2e2c009bSjjc 			kpreempt_enable();
1863*2e2c009bSjjc 		}
1864*2e2c009bSjjc 
1865*2e2c009bSjjc 		probe_stats->probe_cost = gethrtime() -
1866*2e2c009bSjjc 		    probe_stats->probe_cost;
1867*2e2c009bSjjc 		probe_stats->probe_cost_total += probe_stats->probe_cost;
1868*2e2c009bSjjc 
1869*2e2c009bSjjc 		if (min == -1 || elapsed < min)
1870*2e2c009bSjjc 			min = elapsed;
1871*2e2c009bSjjc 		if (elapsed > max)
1872*2e2c009bSjjc 			max = elapsed;
1873*2e2c009bSjjc 	}
1874*2e2c009bSjjc 
1875*2e2c009bSjjc 	/*
1876*2e2c009bSjjc 	 * Update minimum and maximum probe times between
1877*2e2c009bSjjc 	 * these two nodes
1878*2e2c009bSjjc 	 */
1879*2e2c009bSjjc 	if (min < probe_stats->probe_min[from][to] ||
1880*2e2c009bSjjc 	    probe_stats->probe_min[from][to] == 0)
1881*2e2c009bSjjc 		probe_stats->probe_min[from][to] = min;
1882*2e2c009bSjjc 
1883*2e2c009bSjjc 	if (max > probe_stats->probe_max[from][to])
1884*2e2c009bSjjc 		probe_stats->probe_max[from][to] = max;
1885*2e2c009bSjjc 
1886*2e2c009bSjjc 	return (min);
1887*2e2c009bSjjc }
1888*2e2c009bSjjc 
1889*2e2c009bSjjc 
1890*2e2c009bSjjc /*
1891*2e2c009bSjjc  * Read ACPI System Locality Information Table (SLIT) to determine how far each
1892*2e2c009bSjjc  * NUMA node is from each other
1893*2e2c009bSjjc  */
1894*2e2c009bSjjc static int
1895*2e2c009bSjjc lgrp_plat_process_slit(struct slit *tp, uint_t node_cnt,
1896*2e2c009bSjjc     node_phys_addr_map_t *node_memory, lgrp_plat_latency_stats_t *lat_stats)
1897*2e2c009bSjjc {
1898*2e2c009bSjjc 	int		i;
1899*2e2c009bSjjc 	int		j;
1900*2e2c009bSjjc 	int		localities;
1901*2e2c009bSjjc 	hrtime_t	max;
1902*2e2c009bSjjc 	hrtime_t	min;
1903*2e2c009bSjjc 	int		retval;
1904*2e2c009bSjjc 	uint8_t		*slit_entries;
1905*2e2c009bSjjc 
1906*2e2c009bSjjc 	if (tp == NULL || !lgrp_plat_slit_enable)
1907*2e2c009bSjjc 		return (1);
1908*2e2c009bSjjc 
1909*2e2c009bSjjc 	if (lat_stats == NULL)
1910*2e2c009bSjjc 		return (2);
1911*2e2c009bSjjc 
1912*2e2c009bSjjc 	localities = tp->number;
1913*2e2c009bSjjc 	if (localities != node_cnt)
1914*2e2c009bSjjc 		return (3);
1915*2e2c009bSjjc 
1916*2e2c009bSjjc 	min = lat_stats->latency_min;
1917*2e2c009bSjjc 	max = lat_stats->latency_max;
1918*2e2c009bSjjc 
1919*2e2c009bSjjc 	/*
1920*2e2c009bSjjc 	 * Fill in latency matrix based on SLIT entries
1921*2e2c009bSjjc 	 */
1922*2e2c009bSjjc 	slit_entries = tp->entry;
1923*2e2c009bSjjc 	for (i = 0; i < localities; i++) {
1924*2e2c009bSjjc 		for (j = 0; j < localities; j++) {
1925*2e2c009bSjjc 			uint8_t	latency;
1926*2e2c009bSjjc 
1927*2e2c009bSjjc 			latency = slit_entries[(i * localities) + j];
1928*2e2c009bSjjc 			lat_stats->latencies[i][j] = latency;
1929*2e2c009bSjjc 			if (latency < min)
1930*2e2c009bSjjc 				min = latency;
1931*2e2c009bSjjc 			if (latency > max)
1932*2e2c009bSjjc 				max = latency;
1933*2e2c009bSjjc 		}
1934*2e2c009bSjjc 	}
1935*2e2c009bSjjc 
1936*2e2c009bSjjc 	/*
1937*2e2c009bSjjc 	 * Verify that latencies/distances given in SLIT look reasonable
1938*2e2c009bSjjc 	 */
1939*2e2c009bSjjc 	retval = lgrp_plat_latency_verify(node_memory, lat_stats);
1940*2e2c009bSjjc 
1941*2e2c009bSjjc 	if (retval) {
1942*2e2c009bSjjc 		/*
1943*2e2c009bSjjc 		 * Reinitialize (zero) latency table since SLIT doesn't look
1944*2e2c009bSjjc 		 * right
1945*2e2c009bSjjc 		 */
1946*2e2c009bSjjc 		for (i = 0; i < localities; i++) {
1947*2e2c009bSjjc 			for (j = 0; j < localities; j++)
1948*2e2c009bSjjc 				lat_stats->latencies[i][j] = 0;
1949*2e2c009bSjjc 		}
1950*2e2c009bSjjc 	} else {
1951*2e2c009bSjjc 		/*
1952*2e2c009bSjjc 		 * Update min and max latencies seen since SLIT looks valid
1953*2e2c009bSjjc 		 */
1954*2e2c009bSjjc 		lat_stats->latency_min = min;
1955*2e2c009bSjjc 		lat_stats->latency_max = max;
1956*2e2c009bSjjc 	}
1957*2e2c009bSjjc 
1958*2e2c009bSjjc 	return (retval);
1959*2e2c009bSjjc }
1960*2e2c009bSjjc 
1961*2e2c009bSjjc 
1962*2e2c009bSjjc /*
1963*2e2c009bSjjc  * Read ACPI System Resource Affinity Table (SRAT) to determine which CPUs
1964*2e2c009bSjjc  * and memory are local to each other in the same NUMA node
1965*2e2c009bSjjc  */
1966*2e2c009bSjjc static int
1967*2e2c009bSjjc lgrp_plat_process_srat(struct srat *tp, uint_t *node_cnt,
1968*2e2c009bSjjc     node_domain_map_t *node_domain, cpu_node_map_t *cpu_node,
1969*2e2c009bSjjc     node_phys_addr_map_t *node_memory)
1970*2e2c009bSjjc {
1971*2e2c009bSjjc 	struct srat_item	*end;
1972*2e2c009bSjjc 	int			i;
1973*2e2c009bSjjc 	struct srat_item	*item;
1974*2e2c009bSjjc 
1975*2e2c009bSjjc 	if (tp == NULL || !lgrp_plat_srat_enable)
1976*2e2c009bSjjc 		return (1);
1977*2e2c009bSjjc 
1978*2e2c009bSjjc 	/*
1979*2e2c009bSjjc 	 * Determine number of nodes by counting number of proximity domains in
1980*2e2c009bSjjc 	 * SRAT
1981*2e2c009bSjjc 	 */
1982*2e2c009bSjjc 	if (node_cnt) {
1983*2e2c009bSjjc 		int	nodes;
1984*2e2c009bSjjc 
1985*2e2c009bSjjc 		nodes = lgrp_plat_srat_domains(tp);
1986*2e2c009bSjjc 		if (nodes < 0) {
1987*2e2c009bSjjc 			*node_cnt = 1;
1988*2e2c009bSjjc 			return (2);
1989*2e2c009bSjjc 		}
1990*2e2c009bSjjc 		*node_cnt = nodes;
1991*2e2c009bSjjc 	}
1992*2e2c009bSjjc 
1993*2e2c009bSjjc 	/*
1994*2e2c009bSjjc 	 * Walk through SRAT, examining each CPU and memory entry to determine
1995*2e2c009bSjjc 	 * which CPUs and memory belong to which node.
1996*2e2c009bSjjc 	 */
1997*2e2c009bSjjc 	item = tp->list;
1998*2e2c009bSjjc 	end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp);
1999*2e2c009bSjjc 	while (item < end) {
2000*2e2c009bSjjc 		uint32_t	apic_id;
2001*2e2c009bSjjc 		uint32_t	domain;
2002*2e2c009bSjjc 		uint64_t	end;
2003*2e2c009bSjjc 		uint64_t	length;
2004*2e2c009bSjjc 		uint64_t	start;
2005*2e2c009bSjjc 
2006*2e2c009bSjjc 		switch (item->type) {
2007*2e2c009bSjjc 		case SRAT_PROCESSOR:	/* CPU entry */
2008*2e2c009bSjjc 			if (!(item->i.p.flags & SRAT_ENABLED) ||
2009*2e2c009bSjjc 			    cpu_node == NULL)
2010*2e2c009bSjjc 				break;
2011*2e2c009bSjjc 
2012*2e2c009bSjjc 			/*
2013*2e2c009bSjjc 			 * Calculate domain (node) ID and fill in APIC ID to
2014*2e2c009bSjjc 			 * domain/node mapping table
2015*2e2c009bSjjc 			 */
2016*2e2c009bSjjc 			domain = item->i.p.domain1;
2017*2e2c009bSjjc 			for (i = 0; i < 3; i++) {
2018*2e2c009bSjjc 				domain += item->i.p.domain2[i] <<
2019*2e2c009bSjjc 				    ((i + 1) * 8);
2020*2e2c009bSjjc 			}
2021*2e2c009bSjjc 			apic_id = item->i.p.apic_id;
2022*2e2c009bSjjc 
2023*2e2c009bSjjc 			if (lgrp_plat_cpu_node_update(node_domain, cpu_node,
2024*2e2c009bSjjc 			    apic_id, domain) < 0)
2025*2e2c009bSjjc 				return (3);
2026*2e2c009bSjjc 			break;
2027*2e2c009bSjjc 
2028*2e2c009bSjjc 		case SRAT_MEMORY:	/* memory entry */
2029*2e2c009bSjjc 			if (!(item->i.m.flags & SRAT_ENABLED) ||
2030*2e2c009bSjjc 			    node_memory == NULL)
2031*2e2c009bSjjc 				break;
2032*2e2c009bSjjc 
2033*2e2c009bSjjc 			/*
2034*2e2c009bSjjc 			 * Get domain (node) ID and fill in domain/node
2035*2e2c009bSjjc 			 * to memory mapping table
2036*2e2c009bSjjc 			 */
2037*2e2c009bSjjc 			domain = item->i.m.domain;
2038*2e2c009bSjjc 			start = item->i.m.base_addr;
2039*2e2c009bSjjc 			length = item->i.m.len;
2040*2e2c009bSjjc 			end = start + length - 1;
2041*2e2c009bSjjc 
2042*2e2c009bSjjc 			if (lgrp_plat_node_memory_update(node_domain,
2043*2e2c009bSjjc 			    node_memory, start, end, domain) < 0)
2044*2e2c009bSjjc 				return (4);
2045*2e2c009bSjjc 			break;
2046*2e2c009bSjjc 
2047*2e2c009bSjjc 		default:
2048*2e2c009bSjjc 			break;
2049*2e2c009bSjjc 		}
2050*2e2c009bSjjc 
2051*2e2c009bSjjc 		item = (struct srat_item *)((uintptr_t)item + item->len);
2052*2e2c009bSjjc 	}
2053*2e2c009bSjjc 	return (0);
2054*2e2c009bSjjc }
2055*2e2c009bSjjc 
2056*2e2c009bSjjc 
2057*2e2c009bSjjc /*
2058*2e2c009bSjjc  * Return number of proximity domains given in ACPI SRAT
2059*2e2c009bSjjc  */
2060*2e2c009bSjjc static int
2061*2e2c009bSjjc lgrp_plat_srat_domains(struct srat *tp)
2062*2e2c009bSjjc {
2063*2e2c009bSjjc 	int			domain_cnt;
2064*2e2c009bSjjc 	struct srat_item	*end;
2065*2e2c009bSjjc 	int			i;
2066*2e2c009bSjjc 	struct srat_item	*item;
2067*2e2c009bSjjc 	node_domain_map_t	node_domain[MAX_NODES];
2068*2e2c009bSjjc 
2069*2e2c009bSjjc 
2070*2e2c009bSjjc 	if (tp == NULL || !lgrp_plat_srat_enable)
2071*2e2c009bSjjc 		return (1);
2072*2e2c009bSjjc 
2073*2e2c009bSjjc 	/*
2074*2e2c009bSjjc 	 * Walk through SRAT, examining each CPU and memory entry to determine
2075*2e2c009bSjjc 	 * proximity domain ID for each.
2076*2e2c009bSjjc 	 */
2077*2e2c009bSjjc 	domain_cnt = 0;
2078*2e2c009bSjjc 	item = tp->list;
2079*2e2c009bSjjc 	end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp);
2080*2e2c009bSjjc 	bzero(node_domain, MAX_NODES * sizeof (node_domain_map_t));
2081*2e2c009bSjjc 	while (item < end) {
2082*2e2c009bSjjc 		uint32_t	domain;
2083*2e2c009bSjjc 		boolean_t	overflow;
2084*2e2c009bSjjc 		uint_t		start;
2085*2e2c009bSjjc 
2086*2e2c009bSjjc 		switch (item->type) {
2087*2e2c009bSjjc 		case SRAT_PROCESSOR:	/* CPU entry */
2088*2e2c009bSjjc 			if (!(item->i.p.flags & SRAT_ENABLED))
2089*2e2c009bSjjc 				break;
2090*2e2c009bSjjc 			domain = item->i.p.domain1;
2091*2e2c009bSjjc 			for (i = 0; i < 3; i++) {
2092*2e2c009bSjjc 				domain += item->i.p.domain2[i] <<
2093*2e2c009bSjjc 				    ((i + 1) * 8);
2094*2e2c009bSjjc 			}
2095*2e2c009bSjjc 			break;
2096*2e2c009bSjjc 
2097*2e2c009bSjjc 		case SRAT_MEMORY:	/* memory entry */
2098*2e2c009bSjjc 			if (!(item->i.m.flags & SRAT_ENABLED))
2099*2e2c009bSjjc 				break;
2100*2e2c009bSjjc 			domain = item->i.m.domain;
2101*2e2c009bSjjc 			break;
2102*2e2c009bSjjc 
2103*2e2c009bSjjc 		default:
2104*2e2c009bSjjc 			break;
2105*2e2c009bSjjc 		}
2106*2e2c009bSjjc 
2107*2e2c009bSjjc 		/*
2108*2e2c009bSjjc 		 * Count and keep track of which proximity domain IDs seen
2109*2e2c009bSjjc 		 */
2110*2e2c009bSjjc 		start = i = domain % MAX_NODES;
2111*2e2c009bSjjc 		overflow = B_TRUE;
2112*2e2c009bSjjc 		do {
2113*2e2c009bSjjc 			/*
2114*2e2c009bSjjc 			 * Create entry for proximity domain and increment
2115*2e2c009bSjjc 			 * count when no entry exists where proximity domain
2116*2e2c009bSjjc 			 * hashed
2117*2e2c009bSjjc 			 */
2118*2e2c009bSjjc 			if (!node_domain[i].exists) {
2119*2e2c009bSjjc 				node_domain[i].exists = 1;
2120*2e2c009bSjjc 				node_domain[i].prox_domain = domain;
2121*2e2c009bSjjc 				domain_cnt++;
2122*2e2c009bSjjc 				overflow = B_FALSE;
2123*2e2c009bSjjc 				break;
2124*2e2c009bSjjc 			}
2125*2e2c009bSjjc 
2126*2e2c009bSjjc 			/*
2127*2e2c009bSjjc 			 * Nothing to do when proximity domain seen already
2128*2e2c009bSjjc 			 * and its entry exists
2129*2e2c009bSjjc 			 */
2130*2e2c009bSjjc 			if (node_domain[i].prox_domain == domain) {
2131*2e2c009bSjjc 				overflow = B_FALSE;
2132*2e2c009bSjjc 				break;
2133*2e2c009bSjjc 			}
2134*2e2c009bSjjc 
2135*2e2c009bSjjc 			/*
2136*2e2c009bSjjc 			 * Entry exists where proximity domain hashed, but for
2137*2e2c009bSjjc 			 * different proximity domain so keep search for empty
2138*2e2c009bSjjc 			 * slot to put it or matching entry whichever comes
2139*2e2c009bSjjc 			 * first.
2140*2e2c009bSjjc 			 */
2141*2e2c009bSjjc 			i = (i + 1) % MAX_NODES;
2142*2e2c009bSjjc 		} while (i != start);
2143*2e2c009bSjjc 
2144*2e2c009bSjjc 		/*
2145*2e2c009bSjjc 		 * Didn't find empty or matching entry which means have more
2146*2e2c009bSjjc 		 * proximity domains than supported nodes (:-(
2147*2e2c009bSjjc 		 */
2148*2e2c009bSjjc 		ASSERT(overflow != B_TRUE);
2149*2e2c009bSjjc 		if (overflow == B_TRUE)
2150*2e2c009bSjjc 			return (-1);
2151*2e2c009bSjjc 
2152*2e2c009bSjjc 		item = (struct srat_item *)((uintptr_t)item + item->len);
2153*2e2c009bSjjc 	}
2154*2e2c009bSjjc 	return (domain_cnt);
2155*2e2c009bSjjc }
2156*2e2c009bSjjc 
2157*2e2c009bSjjc 
2158*2e2c009bSjjc /*
2159*2e2c009bSjjc  * Set lgroup latencies for 2 level lgroup topology
2160*2e2c009bSjjc  */
2161*2e2c009bSjjc static void
2162*2e2c009bSjjc lgrp_plat_2level_setup(node_phys_addr_map_t *node_memory,
2163*2e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats)
2164*2e2c009bSjjc {
2165*2e2c009bSjjc 	int	i;
2166*2e2c009bSjjc 
2167*2e2c009bSjjc 	ASSERT(node_memory != NULL && lat_stats != NULL);
2168*2e2c009bSjjc 
2169*2e2c009bSjjc 	if (lgrp_plat_node_cnt >= 4)
2170*2e2c009bSjjc 		cmn_err(CE_NOTE,
2171*2e2c009bSjjc 		    "MPO only optimizing for local and remote\n");
2172*2e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
2173*2e2c009bSjjc 		int	j;
2174*2e2c009bSjjc 
2175*2e2c009bSjjc 		if (!node_memory[i].exists)
2176*2e2c009bSjjc 			continue;
2177*2e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
2178*2e2c009bSjjc 			if (!node_memory[j].exists)
2179*2e2c009bSjjc 				continue;
2180*2e2c009bSjjc 			if (i == j)
2181*2e2c009bSjjc 				lat_stats->latencies[i][j] = 2;
2182*2e2c009bSjjc 			else
2183*2e2c009bSjjc 				lat_stats->latencies[i][j] = 3;
2184*2e2c009bSjjc 		}
2185*2e2c009bSjjc 	}
2186*2e2c009bSjjc 	lat_stats->latency_min = 2;
2187*2e2c009bSjjc 	lat_stats->latency_max = 3;
2188*2e2c009bSjjc 	lgrp_config(LGRP_CONFIG_FLATTEN, 2, 0);
2189*2e2c009bSjjc }
2190*2e2c009bSjjc 
2191*2e2c009bSjjc 
2192*2e2c009bSjjc /*
2193*2e2c009bSjjc  * The following Opteron specific constants, macros, types, and routines define
2194*2e2c009bSjjc  * PCI configuration space registers and how to read them to determine the NUMA
2195*2e2c009bSjjc  * configuration of *supported* Opteron processors.  They provide the same
2196*2e2c009bSjjc  * information that may be gotten from the ACPI System Resource Affinity Table
2197*2e2c009bSjjc  * (SRAT) if it exists on the machine of interest.
2198*2e2c009bSjjc  *
2199*2e2c009bSjjc  * The AMD BIOS and Kernel Developer's Guide (BKDG) for the processor family
2200*2e2c009bSjjc  * of interest describes all of these registers and their contents.  The main
2201*2e2c009bSjjc  * registers used by this code to determine the NUMA configuration of the
2202*2e2c009bSjjc  * machine are the node ID register for the number of NUMA nodes and the DRAM
2203*2e2c009bSjjc  * address map registers for the physical address range of each node.
2204*2e2c009bSjjc  *
2205*2e2c009bSjjc  * NOTE: The format and how to determine the NUMA configuration using PCI
2206*2e2c009bSjjc  *	 config space registers may change or may not be supported in future
2207*2e2c009bSjjc  *	 Opteron processor families.
22087c478bd9Sstevel@tonic-gate  */
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate /*
22117c478bd9Sstevel@tonic-gate  * How many bits to shift Opteron DRAM Address Map base and limit registers
22127c478bd9Sstevel@tonic-gate  * to get actual value
22137c478bd9Sstevel@tonic-gate  */
2214f78a91cdSjjc #define	OPT_DRAMADDR_HI_LSHIFT_ADDR	40	/* shift left for address */
2215f78a91cdSjjc #define	OPT_DRAMADDR_LO_LSHIFT_ADDR	8	/* shift left for address */
22167c478bd9Sstevel@tonic-gate 
2217f78a91cdSjjc #define	OPT_DRAMADDR_HI_MASK_ADDR	0x000000FF /* address bits 47-40 */
2218f78a91cdSjjc #define	OPT_DRAMADDR_LO_MASK_ADDR	0xFFFF0000 /* address bits 39-24 */
2219f78a91cdSjjc 
2220f78a91cdSjjc #define	OPT_DRAMADDR_LO_MASK_OFF	0xFFFFFF /* offset for address */
2221f78a91cdSjjc 
2222f78a91cdSjjc /*
2223f78a91cdSjjc  * Macros to derive addresses from Opteron DRAM Address Map registers
2224f78a91cdSjjc  */
2225f78a91cdSjjc #define	OPT_DRAMADDR_HI(reg) \
2226f78a91cdSjjc 	(((u_longlong_t)reg & OPT_DRAMADDR_HI_MASK_ADDR) << \
2227f78a91cdSjjc 	    OPT_DRAMADDR_HI_LSHIFT_ADDR)
2228f78a91cdSjjc 
2229f78a91cdSjjc #define	OPT_DRAMADDR_LO(reg) \
2230f78a91cdSjjc 	(((u_longlong_t)reg & OPT_DRAMADDR_LO_MASK_ADDR) << \
2231f78a91cdSjjc 	    OPT_DRAMADDR_LO_LSHIFT_ADDR)
2232f78a91cdSjjc 
2233f78a91cdSjjc #define	OPT_DRAMADDR(high, low) \
2234f78a91cdSjjc 	(OPT_DRAMADDR_HI(high) | OPT_DRAMADDR_LO(low))
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate /*
22377c478bd9Sstevel@tonic-gate  * Bit masks defining what's in Opteron DRAM Address Map base register
22387c478bd9Sstevel@tonic-gate  */
2239f78a91cdSjjc #define	OPT_DRAMBASE_LO_MASK_RE		0x1	/* read enable */
2240f78a91cdSjjc #define	OPT_DRAMBASE_LO_MASK_WE		0x2	/* write enable */
2241f78a91cdSjjc #define	OPT_DRAMBASE_LO_MASK_INTRLVEN	0x700	/* interleave */
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate /*
22447c478bd9Sstevel@tonic-gate  * Bit masks defining what's in Opteron DRAM Address Map limit register
22457c478bd9Sstevel@tonic-gate  */
2246f78a91cdSjjc #define	OPT_DRAMLIMIT_LO_MASK_DSTNODE	0x7		/* destination node */
2247f78a91cdSjjc #define	OPT_DRAMLIMIT_LO_MASK_INTRLVSEL	0x700		/* interleave select */
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate /*
22517c478bd9Sstevel@tonic-gate  * Opteron Node ID register in PCI configuration space contains
22527c478bd9Sstevel@tonic-gate  * number of nodes in system, etc. for Opteron K8.  The following
22537c478bd9Sstevel@tonic-gate  * constants and macros define its contents, structure, and access.
22547c478bd9Sstevel@tonic-gate  */
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate /*
22577c478bd9Sstevel@tonic-gate  * Bit masks defining what's in Opteron Node ID register
22587c478bd9Sstevel@tonic-gate  */
22597c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_ID	0x7	/* node ID */
22607c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_CNT	0x70	/* node count */
22617c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_IONODE	0x700	/* Hypertransport I/O hub node ID */
22627c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_LCKNODE	0x7000	/* lock controller node ID */
22637c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_CPUCNT	0xF0000	/* CPUs in system (0 means 1 CPU)  */
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate /*
22667c478bd9Sstevel@tonic-gate  * How many bits in Opteron Node ID register to shift right to get actual value
22677c478bd9Sstevel@tonic-gate  */
22687c478bd9Sstevel@tonic-gate #define	OPT_NODE_RSHIFT_CNT	0x4	/* shift right for node count value */
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate /*
22717c478bd9Sstevel@tonic-gate  * Macros to get values from Opteron Node ID register
22727c478bd9Sstevel@tonic-gate  */
22737c478bd9Sstevel@tonic-gate #define	OPT_NODE_CNT(reg) \
22747c478bd9Sstevel@tonic-gate 	((reg & OPT_NODE_MASK_CNT) >> OPT_NODE_RSHIFT_CNT)
22757c478bd9Sstevel@tonic-gate 
2276f78a91cdSjjc /*
2277f78a91cdSjjc  * Macro to setup PCI Extended Configuration Space (ECS) address to give to
2278f78a91cdSjjc  * "in/out" instructions
2279f78a91cdSjjc  *
2280f78a91cdSjjc  * NOTE: Should only be used in lgrp_plat_init() before MMIO setup because any
2281f78a91cdSjjc  *	 other uses should just do MMIO to access PCI ECS.
2282f78a91cdSjjc  *	 Must enable special bit in Northbridge Configuration Register on
2283f78a91cdSjjc  *	 Greyhound for extended CF8 space access to be able to access PCI ECS
2284f78a91cdSjjc  *	 using "in/out" instructions and restore special bit after done
2285f78a91cdSjjc  *	 accessing PCI ECS.
2286f78a91cdSjjc  */
2287f78a91cdSjjc #define	OPT_PCI_ECS_ADDR(bus, device, function, reg) \
2288f78a91cdSjjc 	(PCI_CONE | (((bus) & 0xff) << 16) | (((device & 0x1f)) << 11)  | \
2289f78a91cdSjjc 	    (((function) & 0x7) << 8) | ((reg) & 0xfc) | \
2290f78a91cdSjjc 	    ((((reg) >> 8) & 0xf) << 24))
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate /*
22937c478bd9Sstevel@tonic-gate  * PCI configuration space registers accessed by specifying
22947c478bd9Sstevel@tonic-gate  * a bus, device, function, and offset.  The following constants
22957c478bd9Sstevel@tonic-gate  * define the values needed to access Opteron K8 configuration
22967c478bd9Sstevel@tonic-gate  * info to determine its node topology
22977c478bd9Sstevel@tonic-gate  */
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate #define	OPT_PCS_BUS_CONFIG	0	/* Hypertransport config space bus */
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate /*
23027c478bd9Sstevel@tonic-gate  * Opteron PCI configuration space register function values
23037c478bd9Sstevel@tonic-gate  */
23047c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_HT		0	/* Hypertransport configuration */
23057c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_ADDRMAP	1	/* Address map configuration */
23067c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_DRAM	2	/* DRAM configuration */
23077c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_MISC	3	/* Miscellaneous configuration */
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate /*
23107c478bd9Sstevel@tonic-gate  * PCI Configuration Space register offsets
23117c478bd9Sstevel@tonic-gate  */
23127c478bd9Sstevel@tonic-gate #define	OPT_PCS_OFF_VENDOR	0x0	/* device/vendor ID register */
2313f78a91cdSjjc #define	OPT_PCS_OFF_DRAMBASE_HI	0x140	/* DRAM Base register (node 0) */
2314f78a91cdSjjc #define	OPT_PCS_OFF_DRAMBASE_LO	0x40	/* DRAM Base register (node 0) */
23157c478bd9Sstevel@tonic-gate #define	OPT_PCS_OFF_NODEID	0x60	/* Node ID register */
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate /*
23187c478bd9Sstevel@tonic-gate  * Opteron PCI Configuration Space device IDs for nodes
23197c478bd9Sstevel@tonic-gate  */
23207c478bd9Sstevel@tonic-gate #define	OPT_PCS_DEV_NODE0		24	/* device number for node 0 */
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate /*
23247c478bd9Sstevel@tonic-gate  * Opteron DRAM address map gives base and limit for physical memory in a node
23257c478bd9Sstevel@tonic-gate  */
23267c478bd9Sstevel@tonic-gate typedef	struct opt_dram_addr_map {
2327f78a91cdSjjc 	uint32_t	base_hi;
2328f78a91cdSjjc 	uint32_t	base_lo;
2329f78a91cdSjjc 	uint32_t	limit_hi;
2330f78a91cdSjjc 	uint32_t	limit_lo;
23317c478bd9Sstevel@tonic-gate } opt_dram_addr_map_t;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate /*
2335f78a91cdSjjc  * Supported AMD processor families
2336f78a91cdSjjc  */
2337f78a91cdSjjc #define	AMD_FAMILY_HAMMER	15
2338f78a91cdSjjc #define	AMD_FAMILY_GREYHOUND	16
23397c478bd9Sstevel@tonic-gate 
2340f78a91cdSjjc /*
2341*2e2c009bSjjc  * Whether to have is_opteron() return 1 even when processor isn't supported
2342f78a91cdSjjc  */
2343f78a91cdSjjc uint_t	is_opteron_override = 0;
2344f78a91cdSjjc 
2345f78a91cdSjjc /*
2346f78a91cdSjjc  * AMD processor family for current CPU
2347f78a91cdSjjc  */
23487c478bd9Sstevel@tonic-gate uint_t	opt_family = 0;
2349f78a91cdSjjc 
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate /*
2352f78a91cdSjjc  * Determine whether we're running on a supported AMD Opteron since reading
2353f78a91cdSjjc  * node count and DRAM address map registers may have different format or
2354*2e2c009bSjjc  * may not be supported across processor families
23557c478bd9Sstevel@tonic-gate  */
2356*2e2c009bSjjc static int
23577c478bd9Sstevel@tonic-gate is_opteron(void)
23587c478bd9Sstevel@tonic-gate {
2359f78a91cdSjjc 
23607c478bd9Sstevel@tonic-gate 	if (x86_vendor != X86_VENDOR_AMD)
23617c478bd9Sstevel@tonic-gate 		return (0);
23627c478bd9Sstevel@tonic-gate 
2363f78a91cdSjjc 	opt_family = cpuid_getfamily(CPU);
2364f78a91cdSjjc 	if (opt_family == AMD_FAMILY_HAMMER ||
2365f78a91cdSjjc 	    opt_family == AMD_FAMILY_GREYHOUND || is_opteron_override)
23667c478bd9Sstevel@tonic-gate 		return (1);
23677c478bd9Sstevel@tonic-gate 	else
23687c478bd9Sstevel@tonic-gate 		return (0);
23697c478bd9Sstevel@tonic-gate }
23707c478bd9Sstevel@tonic-gate 
2371*2e2c009bSjjc 
2372*2e2c009bSjjc /*
2373*2e2c009bSjjc  * Determine NUMA configuration for Opteron from registers that live in PCI
2374*2e2c009bSjjc  * configuration space
2375*2e2c009bSjjc  */
2376*2e2c009bSjjc static void
2377*2e2c009bSjjc opt_get_numa_config(uint_t *node_cnt, int *mem_intrlv,
2378*2e2c009bSjjc     node_phys_addr_map_t *node_memory)
23797c478bd9Sstevel@tonic-gate {
23807c478bd9Sstevel@tonic-gate 	uint_t				bus;
23817c478bd9Sstevel@tonic-gate 	uint_t				dev;
2382*2e2c009bSjjc 	struct opt_dram_addr_map	dram_map[MAX_NODES];
23837c478bd9Sstevel@tonic-gate 	uint_t				node;
2384*2e2c009bSjjc 	uint_t				node_info[MAX_NODES];
2385f78a91cdSjjc 	uint_t				off_hi;
2386f78a91cdSjjc 	uint_t				off_lo;
2387f78a91cdSjjc 	uint64_t			nb_cfg_reg;
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	/*
23907c478bd9Sstevel@tonic-gate 	 * Read configuration registers from PCI configuration space to
23917c478bd9Sstevel@tonic-gate 	 * determine node information, which memory is in each node, etc.
23927c478bd9Sstevel@tonic-gate 	 *
23937c478bd9Sstevel@tonic-gate 	 * Write to PCI configuration space address register to specify
23947c478bd9Sstevel@tonic-gate 	 * which configuration register to read and read/write PCI
23957c478bd9Sstevel@tonic-gate 	 * configuration space data register to get/set contents
23967c478bd9Sstevel@tonic-gate 	 */
23977c478bd9Sstevel@tonic-gate 	bus = OPT_PCS_BUS_CONFIG;
23987c478bd9Sstevel@tonic-gate 	dev = OPT_PCS_DEV_NODE0;
2399f78a91cdSjjc 	off_hi = OPT_PCS_OFF_DRAMBASE_HI;
2400f78a91cdSjjc 	off_lo = OPT_PCS_OFF_DRAMBASE_LO;
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	/*
24037c478bd9Sstevel@tonic-gate 	 * Read node ID register for node 0 to get node count
24047c478bd9Sstevel@tonic-gate 	 */
2405*2e2c009bSjjc 	node_info[0] = pci_getl_func(bus, dev, OPT_PCS_FUNC_HT,
2406ef50d8c0Sesaxe 	    OPT_PCS_OFF_NODEID);
2407*2e2c009bSjjc 	*node_cnt = OPT_NODE_CNT(node_info[0]) + 1;
2408*2e2c009bSjjc 
2409*2e2c009bSjjc 	/*
2410*2e2c009bSjjc 	 * If number of nodes is more than maximum supported, then set node
2411*2e2c009bSjjc 	 * count to 1 and treat system as UMA instead of NUMA.
2412*2e2c009bSjjc 	 */
2413*2e2c009bSjjc 	if (*node_cnt > MAX_NODES) {
2414*2e2c009bSjjc 		*node_cnt = 1;
2415*2e2c009bSjjc 		return;
2416*2e2c009bSjjc 	}
24177c478bd9Sstevel@tonic-gate 
2418f78a91cdSjjc 	/*
2419f78a91cdSjjc 	 * For Greyhound, PCI Extended Configuration Space must be enabled to
2420f78a91cdSjjc 	 * read high DRAM address map base and limit registers
2421f78a91cdSjjc 	 */
2422f78a91cdSjjc 	if (opt_family == AMD_FAMILY_GREYHOUND) {
2423f78a91cdSjjc 		nb_cfg_reg = rdmsr(MSR_AMD_NB_CFG);
2424f78a91cdSjjc 		if ((nb_cfg_reg & AMD_GH_NB_CFG_EN_ECS) == 0)
2425f78a91cdSjjc 			wrmsr(MSR_AMD_NB_CFG,
2426f78a91cdSjjc 			    nb_cfg_reg | AMD_GH_NB_CFG_EN_ECS);
2427f78a91cdSjjc 	}
2428f78a91cdSjjc 
2429*2e2c009bSjjc 	for (node = 0; node < *node_cnt; node++) {
2430f78a91cdSjjc 		uint32_t	base_hi;
2431f78a91cdSjjc 		uint32_t	base_lo;
2432f78a91cdSjjc 		uint32_t	limit_hi;
2433f78a91cdSjjc 		uint32_t	limit_lo;
2434f78a91cdSjjc 
24357c478bd9Sstevel@tonic-gate 		/*
24367c478bd9Sstevel@tonic-gate 		 * Read node ID register (except for node 0 which we just read)
24377c478bd9Sstevel@tonic-gate 		 */
24387c478bd9Sstevel@tonic-gate 		if (node > 0) {
2439*2e2c009bSjjc 			node_info[node] = pci_getl_func(bus, dev,
2440ef50d8c0Sesaxe 			    OPT_PCS_FUNC_HT, OPT_PCS_OFF_NODEID);
24417c478bd9Sstevel@tonic-gate 		}
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 		/*
24447c478bd9Sstevel@tonic-gate 		 * Read DRAM base and limit registers which specify
24457c478bd9Sstevel@tonic-gate 		 * physical memory range of each node
24467c478bd9Sstevel@tonic-gate 		 */
2447f78a91cdSjjc 		if (opt_family != AMD_FAMILY_GREYHOUND)
2448f78a91cdSjjc 			base_hi = 0;
2449f78a91cdSjjc 		else {
2450f78a91cdSjjc 			outl(PCI_CONFADD, OPT_PCI_ECS_ADDR(bus, dev,
2451f78a91cdSjjc 			    OPT_PCS_FUNC_ADDRMAP, off_hi));
2452*2e2c009bSjjc 			base_hi = dram_map[node].base_hi =
2453f78a91cdSjjc 			    inl(PCI_CONFDATA);
2454f78a91cdSjjc 		}
2455*2e2c009bSjjc 		base_lo = dram_map[node].base_lo = pci_getl_func(bus, dev,
2456f78a91cdSjjc 		    OPT_PCS_FUNC_ADDRMAP, off_lo);
2457f78a91cdSjjc 
2458*2e2c009bSjjc 		if ((dram_map[node].base_lo & OPT_DRAMBASE_LO_MASK_INTRLVEN) &&
2459*2e2c009bSjjc 		    mem_intrlv)
2460*2e2c009bSjjc 			*mem_intrlv = *mem_intrlv + 1;
24617c478bd9Sstevel@tonic-gate 
2462f78a91cdSjjc 		off_hi += 4;	/* high limit register offset */
2463f78a91cdSjjc 		if (opt_family != AMD_FAMILY_GREYHOUND)
2464f78a91cdSjjc 			limit_hi = 0;
2465f78a91cdSjjc 		else {
2466f78a91cdSjjc 			outl(PCI_CONFADD, OPT_PCI_ECS_ADDR(bus, dev,
2467f78a91cdSjjc 			    OPT_PCS_FUNC_ADDRMAP, off_hi));
2468*2e2c009bSjjc 			limit_hi = dram_map[node].limit_hi =
2469f78a91cdSjjc 			    inl(PCI_CONFDATA);
2470f78a91cdSjjc 		}
2471f78a91cdSjjc 
2472f78a91cdSjjc 		off_lo += 4;	/* low limit register offset */
2473*2e2c009bSjjc 		limit_lo = dram_map[node].limit_lo = pci_getl_func(bus,
2474f78a91cdSjjc 		    dev, OPT_PCS_FUNC_ADDRMAP, off_lo);
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 		/*
2477f78a91cdSjjc 		 * Increment device number to next node and register offsets
2478f78a91cdSjjc 		 * for DRAM base register of next node
24797c478bd9Sstevel@tonic-gate 		 */
2480f78a91cdSjjc 		off_hi += 4;
2481f78a91cdSjjc 		off_lo += 4;
24827c478bd9Sstevel@tonic-gate 		dev++;
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 		/*
2485a940d195Sjjc 		 * Both read and write enable bits must be enabled in DRAM
2486a940d195Sjjc 		 * address map base register for physical memory to exist in
2487a940d195Sjjc 		 * node
2488a940d195Sjjc 		 */
2489f78a91cdSjjc 		if ((base_lo & OPT_DRAMBASE_LO_MASK_RE) == 0 ||
2490f78a91cdSjjc 		    (base_lo & OPT_DRAMBASE_LO_MASK_WE) == 0) {
2491a940d195Sjjc 			/*
2492a940d195Sjjc 			 * Mark node memory as non-existent and set start and
2493*2e2c009bSjjc 			 * end addresses to be same in node_memory[]
2494a940d195Sjjc 			 */
2495*2e2c009bSjjc 			node_memory[node].exists = 0;
2496*2e2c009bSjjc 			node_memory[node].start = node_memory[node].end =
2497*2e2c009bSjjc 			    (pfn_t)-1;
2498a940d195Sjjc 			continue;
2499a940d195Sjjc 		}
2500a940d195Sjjc 
2501a940d195Sjjc 		/*
2502a940d195Sjjc 		 * Mark node memory as existing and remember physical address
2503a940d195Sjjc 		 * range of each node for use later
25047c478bd9Sstevel@tonic-gate 		 */
2505*2e2c009bSjjc 		node_memory[node].exists = 1;
2506f78a91cdSjjc 
2507*2e2c009bSjjc 		node_memory[node].start = btop(OPT_DRAMADDR(base_hi, base_lo));
2508f78a91cdSjjc 
2509*2e2c009bSjjc 		node_memory[node].end = btop(OPT_DRAMADDR(limit_hi, limit_lo) |
2510f78a91cdSjjc 		    OPT_DRAMADDR_LO_MASK_OFF);
2511f78a91cdSjjc 	}
2512f78a91cdSjjc 
2513f78a91cdSjjc 	/*
2514f78a91cdSjjc 	 * Restore PCI Extended Configuration Space enable bit
2515f78a91cdSjjc 	 */
2516f78a91cdSjjc 	if (opt_family == AMD_FAMILY_GREYHOUND) {
2517f78a91cdSjjc 		if ((nb_cfg_reg & AMD_GH_NB_CFG_EN_ECS) == 0)
2518f78a91cdSjjc 			wrmsr(MSR_AMD_NB_CFG, nb_cfg_reg);
25197c478bd9Sstevel@tonic-gate 	}
25207c478bd9Sstevel@tonic-gate }
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate /*
2524*2e2c009bSjjc  * Return average amount of time to read vendor ID register on Northbridge
2525*2e2c009bSjjc  * N times on specified destination node from current CPU
25267c478bd9Sstevel@tonic-gate  */
25277c478bd9Sstevel@tonic-gate static hrtime_t
2528*2e2c009bSjjc opt_probe_vendor(int dest_node, int nreads)
25297c478bd9Sstevel@tonic-gate {
2530*2e2c009bSjjc 	int		cnt;
25317c478bd9Sstevel@tonic-gate 	uint_t		dev;
25327c478bd9Sstevel@tonic-gate 	/* LINTED: set but not used in function */
25337c478bd9Sstevel@tonic-gate 	volatile uint_t	dev_vendor;
25347c478bd9Sstevel@tonic-gate 	hrtime_t	elapsed;
25357c478bd9Sstevel@tonic-gate 	hrtime_t	end;
25367c478bd9Sstevel@tonic-gate 	int		ipl;
25377c478bd9Sstevel@tonic-gate 	hrtime_t	start;
25387c478bd9Sstevel@tonic-gate 
2539*2e2c009bSjjc 	dev = OPT_PCS_DEV_NODE0 + dest_node;
25407c478bd9Sstevel@tonic-gate 	kpreempt_disable();
25417c478bd9Sstevel@tonic-gate 	ipl = spl8();
2542*2e2c009bSjjc 	outl(PCI_CONFADD, PCI_CADDR1(0, dev, OPT_PCS_FUNC_DRAM,
25437c478bd9Sstevel@tonic-gate 	    OPT_PCS_OFF_VENDOR));
25447c478bd9Sstevel@tonic-gate 	start = gethrtime();
2545*2e2c009bSjjc 	for (cnt = 0; cnt < nreads; cnt++)
25467c478bd9Sstevel@tonic-gate 		dev_vendor = inl(PCI_CONFDATA);
25477c478bd9Sstevel@tonic-gate 	end = gethrtime();
2548*2e2c009bSjjc 	elapsed = (end - start) / nreads;
25497c478bd9Sstevel@tonic-gate 	splx(ipl);
25507c478bd9Sstevel@tonic-gate 	kpreempt_enable();
2551*2e2c009bSjjc 	return (elapsed);
25527c478bd9Sstevel@tonic-gate }
2553