xref: /titanic_53/usr/src/uts/i86pc/os/lgrpplat.c (revision 56f33205c9ed776c3c909e07d52e94610a675740)
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 /*
23*56f33205SJonathan Adams  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
272e2c009bSjjc /*
282e2c009bSjjc  * LOCALITY GROUP (LGROUP) PLATFORM SUPPORT FOR X86/AMD64 PLATFORMS
292e2c009bSjjc  * ================================================================
302e2c009bSjjc  * Multiprocessor AMD and Intel systems may have Non Uniform Memory Access
312e2c009bSjjc  * (NUMA).  A NUMA machine consists of one or more "nodes" that each consist of
322e2c009bSjjc  * one or more CPUs and some local memory.  The CPUs in each node can access
332e2c009bSjjc  * the memory in the other nodes but at a higher latency than accessing their
342e2c009bSjjc  * local memory.  Typically, a system with only one node has Uniform Memory
352e2c009bSjjc  * Access (UMA), but it may be possible to have a one node system that has
362e2c009bSjjc  * some global memory outside of the node which is higher latency.
372e2c009bSjjc  *
382e2c009bSjjc  * Module Description
392e2c009bSjjc  * ------------------
402e2c009bSjjc  * This module provides a platform interface for determining which CPUs and
412e2c009bSjjc  * which memory (and how much) are in a NUMA node and how far each node is from
422e2c009bSjjc  * each other.  The interface is used by the Virtual Memory (VM) system and the
432e2c009bSjjc  * common lgroup framework.  The VM system uses the plat_*() routines to fill
442e2c009bSjjc  * in its memory node (memnode) array with the physical address range spanned
452e2c009bSjjc  * by each NUMA node to know which memory belongs to which node, so it can
462e2c009bSjjc  * build and manage a physical page free list for each NUMA node and allocate
472e2c009bSjjc  * local memory from each node as needed.  The common lgroup framework uses the
482e2c009bSjjc  * exported lgrp_plat_*() routines to figure out which CPUs and memory belong
492e2c009bSjjc  * to each node (leaf lgroup) and how far each node is from each other, so it
502e2c009bSjjc  * can build the latency (lgroup) topology for the machine in order to optimize
512e2c009bSjjc  * for locality.  Also, an lgroup platform handle instead of lgroups are used
522e2c009bSjjc  * in the interface with this module, so this module shouldn't need to know
532e2c009bSjjc  * anything about lgroups.  Instead, it just needs to know which CPUs, memory,
542e2c009bSjjc  * etc. are in each NUMA node, how far each node is from each other, and to use
552e2c009bSjjc  * a unique lgroup platform handle to refer to each node through the interface.
562e2c009bSjjc  *
572e2c009bSjjc  * Determining NUMA Configuration
582e2c009bSjjc  * ------------------------------
592e2c009bSjjc  * By default, this module will try to determine the NUMA configuration of the
602e2c009bSjjc  * machine by reading the ACPI System Resource Affinity Table (SRAT) and System
612e2c009bSjjc  * Locality Information Table (SLIT).  The SRAT contains info to tell which
622e2c009bSjjc  * CPUs and memory are local to a given proximity domain (NUMA node).  The SLIT
632e2c009bSjjc  * is a matrix that gives the distance between each system locality (which is
642e2c009bSjjc  * a NUMA node and should correspond to proximity domains in the SRAT).  For
652e2c009bSjjc  * more details on the SRAT and SLIT, please refer to an ACPI 3.0 or newer
662e2c009bSjjc  * specification.
672e2c009bSjjc  *
682e2c009bSjjc  * If the SRAT doesn't exist on a system with AMD Opteron processors, we
692e2c009bSjjc  * examine registers in PCI configuration space to determine how many nodes are
702e2c009bSjjc  * in the system and which CPUs and memory are in each node.
712e2c009bSjjc  * do while booting the kernel.
722e2c009bSjjc  *
732e2c009bSjjc  * NOTE: Using these PCI configuration space registers to determine this
742e2c009bSjjc  *       locality info is not guaranteed to work or be compatible across all
752e2c009bSjjc  *	 Opteron processor families.
762e2c009bSjjc  *
772e2c009bSjjc  * If the SLIT does not exist or look right, the kernel will probe to determine
782e2c009bSjjc  * the distance between nodes as long as the NUMA CPU and memory configuration
792e2c009bSjjc  * has been determined (see lgrp_plat_probe() for details).
802e2c009bSjjc  *
812e2c009bSjjc  * Data Structures
822e2c009bSjjc  * ---------------
832e2c009bSjjc  * The main data structures used by this code are the following:
842e2c009bSjjc  *
85dae2fa37Sjjc  * - lgrp_plat_cpu_node[]		CPU to node ID mapping table indexed by
86dae2fa37Sjjc  *					CPU ID (only used for SRAT)
872e2c009bSjjc  *
882e2c009bSjjc  * - lgrp_plat_lat_stats.latencies[][]	Table of latencies between same and
892e2c009bSjjc  *					different nodes indexed by node ID
902e2c009bSjjc  *
912e2c009bSjjc  * - lgrp_plat_node_cnt			Number of NUMA nodes in system
922e2c009bSjjc  *
932e2c009bSjjc  * - lgrp_plat_node_domain[]		Node ID to proximity domain ID mapping
942e2c009bSjjc  *					table indexed by node ID (only used
952e2c009bSjjc  *					for SRAT)
962e2c009bSjjc  *
972e2c009bSjjc  * - lgrp_plat_node_memory[]		Table with physical address range for
982e2c009bSjjc  *					each node indexed by node ID
992e2c009bSjjc  *
1002e2c009bSjjc  * The code is implemented to make the following always be true:
1012e2c009bSjjc  *
1022e2c009bSjjc  *	lgroup platform handle == node ID == memnode ID
1032e2c009bSjjc  *
1042e2c009bSjjc  * Moreover, it allows for the proximity domain ID to be equal to all of the
1052e2c009bSjjc  * above as long as the proximity domains IDs are numbered from 0 to <number of
1062e2c009bSjjc  * nodes - 1>.  This is done by hashing each proximity domain ID into the range
1072e2c009bSjjc  * from 0 to <number of nodes - 1>.  Then proximity ID N will hash into node ID
1082e2c009bSjjc  * N and proximity domain ID N will be entered into lgrp_plat_node_domain[N]
1092e2c009bSjjc  * and be assigned node ID N.  If the proximity domain IDs aren't numbered
1102e2c009bSjjc  * from 0 to <number of nodes - 1>, then hashing the proximity domain IDs into
1112e2c009bSjjc  * lgrp_plat_node_domain[] will still work for assigning proximity domain IDs
1122e2c009bSjjc  * to node IDs.  However, the proximity domain IDs may not map to the
1132e2c009bSjjc  * equivalent node ID since we want to keep the node IDs numbered from 0 to
1142e2c009bSjjc  * <number of nodes - 1> to minimize cost of searching and potentially space.
11581d9ccb6SJonathan Chew  *
11681d9ccb6SJonathan Chew  * The code below really tries to do the above.  However, the virtual memory
11781d9ccb6SJonathan Chew  * system expects the memnodes which describe the physical address range for
11881d9ccb6SJonathan Chew  * each NUMA node to be arranged in ascending order by physical address.  (:-(
11981d9ccb6SJonathan Chew  * Otherwise, the kernel will panic in different semi-random places in the VM
120cf5755f2SJonathan Chew  * system.
12181d9ccb6SJonathan Chew  *
12281d9ccb6SJonathan Chew  * Consequently, this module has to try to sort the nodes in ascending order by
12381d9ccb6SJonathan Chew  * each node's starting physical address to try to meet this "constraint" in
12481d9ccb6SJonathan Chew  * the VM system (see lgrp_plat_node_sort()).  Also, the lowest numbered
12581d9ccb6SJonathan Chew  * proximity domain ID in the system is deteremined and used to make the lowest
12681d9ccb6SJonathan Chew  * numbered proximity domain map to node 0 in hopes that the proximity domains
12781d9ccb6SJonathan Chew  * are sorted in ascending order by physical address already even if their IDs
12881d9ccb6SJonathan Chew  * don't start at 0 (see NODE_DOMAIN_HASH() and lgrp_plat_srat_domains()).
12981d9ccb6SJonathan Chew  * Finally, it is important to note that these workarounds may not be
13081d9ccb6SJonathan Chew  * sufficient if/when memory hotplugging is supported and the VM system may
13181d9ccb6SJonathan Chew  * ultimately need to be fixed to handle this....
1322e2c009bSjjc  */
1332e2c009bSjjc 
1342e2c009bSjjc 
1357c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>	/* for {in,out}{b,w,l}() */
136dae2fa37Sjjc #include <sys/bootconf.h>
1377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
138f78a91cdSjjc #include <sys/controlregs.h>
1397c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
1407c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
1417c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
1427c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1437c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
1447c478bd9Sstevel@tonic-gate #include <sys/memnode.h>
1457c478bd9Sstevel@tonic-gate #include <sys/mman.h>
146ef50d8c0Sesaxe #include <sys/pci_cfgspace.h>
147ef50d8c0Sesaxe #include <sys/pci_impl.h>
1487c478bd9Sstevel@tonic-gate #include <sys/param.h>
149fb2f18f8Sesaxe #include <sys/pghw.h>
1507c478bd9Sstevel@tonic-gate #include <sys/promif.h>		/* for prom_printf() */
1512e2c009bSjjc #include <sys/sysmacros.h>
1527c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1537c478bd9Sstevel@tonic-gate #include <sys/thread.h>
1547c478bd9Sstevel@tonic-gate #include <sys/types.h>
1557c478bd9Sstevel@tonic-gate #include <sys/var.h>
1567c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>	/* for x86_feature and X86_AMD */
1577c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
1587c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
159affbd3ccSkchow #include <vm/vm_dep.h>
1607c478bd9Sstevel@tonic-gate 
1612e2c009bSjjc #include "acpi_fw.h"		/* for SRAT and SLIT */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #define	MAX_NODES		8
1657c478bd9Sstevel@tonic-gate #define	NLGRP			(MAX_NODES * (MAX_NODES - 1) + 1)
1667c478bd9Sstevel@tonic-gate 
1672e2c009bSjjc /*
1682e2c009bSjjc  * Constants for configuring probing
1692e2c009bSjjc  */
1707c478bd9Sstevel@tonic-gate #define	LGRP_PLAT_PROBE_NROUNDS		64	/* default laps for probing */
1717c478bd9Sstevel@tonic-gate #define	LGRP_PLAT_PROBE_NSAMPLES	1	/* default samples to take */
1728949bcd6Sandrei #define	LGRP_PLAT_PROBE_NREADS		256	/* number of vendor ID reads */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1752e2c009bSjjc  * Flags for probing
1762e2c009bSjjc  */
1772e2c009bSjjc #define	LGRP_PLAT_PROBE_ENABLE		0x1	/* enable probing */
1782e2c009bSjjc #define	LGRP_PLAT_PROBE_PGCPY		0x2	/* probe using page copy */
1792e2c009bSjjc #define	LGRP_PLAT_PROBE_VENDOR		0x4	/* probe vendor ID register */
1802e2c009bSjjc 
1812e2c009bSjjc /*
18281d9ccb6SJonathan Chew  * Hash proximity domain ID into node to domain mapping table "mod" number of
18381d9ccb6SJonathan Chew  * nodes to minimize span of entries used and try to have lowest numbered
18481d9ccb6SJonathan Chew  * proximity domain be node 0
1852e2c009bSjjc  */
18681d9ccb6SJonathan Chew #define	NODE_DOMAIN_HASH(domain, node_cnt) \
18781d9ccb6SJonathan Chew 	((lgrp_plat_prox_domain_min == UINT32_MAX) ? (domain) % node_cnt : \
18881d9ccb6SJonathan Chew 	    ((domain) - lgrp_plat_prox_domain_min) % node_cnt)
1892e2c009bSjjc 
1902e2c009bSjjc 
1912e2c009bSjjc /*
192dae2fa37Sjjc  * CPU to node ID mapping structure (only used with SRAT)
1932e2c009bSjjc  */
1942e2c009bSjjc typedef	struct cpu_node_map {
1952e2c009bSjjc 	int		exists;
1962e2c009bSjjc 	uint_t		node;
1972e2c009bSjjc 	uint32_t	apicid;
1982e2c009bSjjc 	uint32_t	prox_domain;
1992e2c009bSjjc } cpu_node_map_t;
2002e2c009bSjjc 
2012e2c009bSjjc /*
2022e2c009bSjjc  * Latency statistics
2032e2c009bSjjc  */
2042e2c009bSjjc typedef struct lgrp_plat_latency_stats {
2052e2c009bSjjc 	hrtime_t	latencies[MAX_NODES][MAX_NODES];
2062e2c009bSjjc 	hrtime_t	latency_max;
2072e2c009bSjjc 	hrtime_t	latency_min;
2082e2c009bSjjc } lgrp_plat_latency_stats_t;
2092e2c009bSjjc 
2102e2c009bSjjc /*
2112e2c009bSjjc  * Memory configuration for probing
2122e2c009bSjjc  */
2132e2c009bSjjc typedef struct lgrp_plat_probe_mem_config {
2142e2c009bSjjc 	size_t	probe_memsize;		/* how much memory to probe per node */
2152e2c009bSjjc 	caddr_t	probe_va[MAX_NODES];	/* where memory mapped for probing */
2162e2c009bSjjc 	pfn_t	probe_pfn[MAX_NODES];	/* physical pages to map for probing */
2172e2c009bSjjc } lgrp_plat_probe_mem_config_t;
2182e2c009bSjjc 
2192e2c009bSjjc /*
2202e2c009bSjjc  * Statistics kept for probing
2212e2c009bSjjc  */
2222e2c009bSjjc typedef struct lgrp_plat_probe_stats {
2232e2c009bSjjc 	hrtime_t	flush_cost;
2242e2c009bSjjc 	hrtime_t	probe_cost;
2252e2c009bSjjc 	hrtime_t	probe_cost_total;
2262e2c009bSjjc 	hrtime_t	probe_error_code;
2272e2c009bSjjc 	hrtime_t	probe_errors[MAX_NODES][MAX_NODES];
2282e2c009bSjjc 	int		probe_suspect[MAX_NODES][MAX_NODES];
2292e2c009bSjjc 	hrtime_t	probe_max[MAX_NODES][MAX_NODES];
2302e2c009bSjjc 	hrtime_t	probe_min[MAX_NODES][MAX_NODES];
2312e2c009bSjjc } lgrp_plat_probe_stats_t;
2322e2c009bSjjc 
2332e2c009bSjjc /*
2342e2c009bSjjc  * Node to proximity domain ID mapping structure (only used with SRAT)
2352e2c009bSjjc  */
2362e2c009bSjjc typedef	struct node_domain_map {
2372e2c009bSjjc 	int		exists;
2382e2c009bSjjc 	uint32_t	prox_domain;
2392e2c009bSjjc } node_domain_map_t;
2402e2c009bSjjc 
2412e2c009bSjjc /*
2422e2c009bSjjc  * Node ID and starting and ending page for physical memory in node
2432e2c009bSjjc  */
2442e2c009bSjjc typedef	struct node_phys_addr_map {
2452e2c009bSjjc 	pfn_t		start;
2462e2c009bSjjc 	pfn_t		end;
2472e2c009bSjjc 	int		exists;
2482e2c009bSjjc 	uint32_t	prox_domain;
2492e2c009bSjjc } node_phys_addr_map_t;
2502e2c009bSjjc 
251dae2fa37Sjjc /*
252d821f0f0Sjjc  * Number of CPUs for which we got APIC IDs
253dae2fa37Sjjc  */
254d821f0f0Sjjc static int				lgrp_plat_apic_ncpus = 0;
2552e2c009bSjjc 
2562e2c009bSjjc /*
257d5d7cf4eSJonathan Chew  * CPU to node ID mapping table (only used for SRAT) and its max number of
258d5d7cf4eSJonathan Chew  * entries
2592e2c009bSjjc  */
260d5d7cf4eSJonathan Chew static cpu_node_map_t			*lgrp_plat_cpu_node = NULL;
261d5d7cf4eSJonathan Chew static uint_t				lgrp_plat_cpu_node_nentries = 0;
2622e2c009bSjjc 
2632e2c009bSjjc /*
2642e2c009bSjjc  * Latency statistics
2652e2c009bSjjc  */
2662e2c009bSjjc lgrp_plat_latency_stats_t		lgrp_plat_lat_stats;
2672e2c009bSjjc 
2682e2c009bSjjc /*
2692e2c009bSjjc  * Whether memory is interleaved across nodes causing MPO to be disabled
2702e2c009bSjjc  */
2712e2c009bSjjc static int				lgrp_plat_mem_intrlv = 0;
2722e2c009bSjjc 
2732e2c009bSjjc /*
2742e2c009bSjjc  * Node ID to proximity domain ID mapping table (only used for SRAT)
2752e2c009bSjjc  */
2762e2c009bSjjc static node_domain_map_t		lgrp_plat_node_domain[MAX_NODES];
2772e2c009bSjjc 
2782e2c009bSjjc /*
2792e2c009bSjjc  * Physical address range for memory in each node
2802e2c009bSjjc  */
2812e2c009bSjjc static node_phys_addr_map_t		lgrp_plat_node_memory[MAX_NODES];
2822e2c009bSjjc 
2832e2c009bSjjc /*
2842e2c009bSjjc  * Statistics gotten from probing
2852e2c009bSjjc  */
2862e2c009bSjjc static lgrp_plat_probe_stats_t		lgrp_plat_probe_stats;
2872e2c009bSjjc 
2882e2c009bSjjc /*
2892e2c009bSjjc  * Memory configuration for probing
2902e2c009bSjjc  */
2912e2c009bSjjc static lgrp_plat_probe_mem_config_t	lgrp_plat_probe_mem_config;
2922e2c009bSjjc 
2932e2c009bSjjc /*
29481d9ccb6SJonathan Chew  * Lowest proximity domain ID seen in ACPI SRAT
29581d9ccb6SJonathan Chew  */
29681d9ccb6SJonathan Chew static uint32_t				lgrp_plat_prox_domain_min = UINT32_MAX;
29781d9ccb6SJonathan Chew 
29881d9ccb6SJonathan Chew /*
2992e2c009bSjjc  * Error code from processing ACPI SRAT
3002e2c009bSjjc  */
3012e2c009bSjjc static int				lgrp_plat_srat_error = 0;
3022e2c009bSjjc 
3032e2c009bSjjc /*
3042e2c009bSjjc  * Error code from processing ACPI SLIT
3052e2c009bSjjc  */
3062e2c009bSjjc static int				lgrp_plat_slit_error = 0;
3072e2c009bSjjc 
3082e2c009bSjjc /*
3092e2c009bSjjc  * Allocate lgroup array statically
3102e2c009bSjjc  */
3112e2c009bSjjc static lgrp_t				lgrp_space[NLGRP];
3122e2c009bSjjc static int				nlgrps_alloc;
3132e2c009bSjjc 
3142e2c009bSjjc 
3152e2c009bSjjc /*
31681d9ccb6SJonathan Chew  * Enable finding and using minimum proximity domain ID when hashing
31781d9ccb6SJonathan Chew  */
31881d9ccb6SJonathan Chew int			lgrp_plat_domain_min_enable = 1;
31981d9ccb6SJonathan Chew 
32081d9ccb6SJonathan Chew /*
3212e2c009bSjjc  * Number of nodes in system
3222e2c009bSjjc  */
3232e2c009bSjjc uint_t			lgrp_plat_node_cnt = 1;
3242e2c009bSjjc 
3252e2c009bSjjc /*
32681d9ccb6SJonathan Chew  * Enable sorting nodes in ascending order by starting physical address
32781d9ccb6SJonathan Chew  */
32881d9ccb6SJonathan Chew int			lgrp_plat_node_sort_enable = 1;
32981d9ccb6SJonathan Chew 
33081d9ccb6SJonathan Chew /*
3312e2c009bSjjc  * Configuration Parameters for Probing
3322e2c009bSjjc  * - lgrp_plat_probe_flags	Flags to specify enabling probing, probe
3332e2c009bSjjc  *				operation, etc.
3342e2c009bSjjc  * - lgrp_plat_probe_nrounds	How many rounds of probing to do
3352e2c009bSjjc  * - lgrp_plat_probe_nsamples	Number of samples to take when probing each
3362e2c009bSjjc  *				node
3372e2c009bSjjc  * - lgrp_plat_probe_nreads	Number of times to read vendor ID from
3382e2c009bSjjc  *				Northbridge for each probe
3392e2c009bSjjc  */
3402e2c009bSjjc uint_t			lgrp_plat_probe_flags = 0;
3412e2c009bSjjc int			lgrp_plat_probe_nrounds = LGRP_PLAT_PROBE_NROUNDS;
3422e2c009bSjjc int			lgrp_plat_probe_nsamples = LGRP_PLAT_PROBE_NSAMPLES;
3432e2c009bSjjc int			lgrp_plat_probe_nreads = LGRP_PLAT_PROBE_NREADS;
3442e2c009bSjjc 
3452e2c009bSjjc /*
3462e2c009bSjjc  * Enable use of ACPI System Resource Affinity Table (SRAT) and System
3472e2c009bSjjc  * Locality Information Table (SLIT)
3482e2c009bSjjc  */
3492e2c009bSjjc int			lgrp_plat_srat_enable = 1;
3502e2c009bSjjc int			lgrp_plat_slit_enable = 1;
3512e2c009bSjjc 
3522e2c009bSjjc /*
35318968004SKit Chow  * mnode_xwa: set to non-zero value to initiate workaround if large pages are
35418968004SKit Chow  * found to be crossing memory node boundaries. The workaround will eliminate
35518968004SKit Chow  * a base size page at the end of each memory node boundary to ensure that
35618968004SKit Chow  * a large page with constituent pages that span more than 1 memory node
35718968004SKit Chow  * can never be formed.
35818968004SKit Chow  *
35918968004SKit Chow  */
36018968004SKit Chow int	mnode_xwa = 1;
36118968004SKit Chow 
36218968004SKit Chow /*
3632e2c009bSjjc  * Static array to hold lgroup statistics
3642e2c009bSjjc  */
3652e2c009bSjjc struct lgrp_stats	lgrp_stats[NLGRP];
3662e2c009bSjjc 
3672e2c009bSjjc 
3682e2c009bSjjc /*
3692e2c009bSjjc  * Forward declarations of platform interface routines
3702e2c009bSjjc  */
3712e2c009bSjjc void		plat_build_mem_nodes(struct memlist *list);
3722e2c009bSjjc 
3732e2c009bSjjc int		plat_lgrphand_to_mem_node(lgrp_handle_t hand);
3742e2c009bSjjc 
3752e2c009bSjjc lgrp_handle_t	plat_mem_node_to_lgrphand(int mnode);
3762e2c009bSjjc 
3772e2c009bSjjc int		plat_mnode_xcheck(pfn_t pfncnt);
3782e2c009bSjjc 
3792e2c009bSjjc int		plat_pfn_to_mem_node(pfn_t pfn);
3802e2c009bSjjc 
3812e2c009bSjjc /*
3822e2c009bSjjc  * Forward declarations of lgroup platform interface routines
3832e2c009bSjjc  */
3842e2c009bSjjc lgrp_t		*lgrp_plat_alloc(lgrp_id_t lgrpid);
3852e2c009bSjjc 
3862e2c009bSjjc void		lgrp_plat_config(lgrp_config_flag_t flag, uintptr_t arg);
3872e2c009bSjjc 
3882e2c009bSjjc lgrp_handle_t	lgrp_plat_cpu_to_hand(processorid_t id);
3892e2c009bSjjc 
390d5d7cf4eSJonathan Chew void		lgrp_plat_init(lgrp_init_stages_t stage);
3912e2c009bSjjc 
3922e2c009bSjjc int		lgrp_plat_latency(lgrp_handle_t from, lgrp_handle_t to);
3932e2c009bSjjc 
3942e2c009bSjjc int		lgrp_plat_max_lgrps(void);
3952e2c009bSjjc 
3962e2c009bSjjc pgcnt_t		lgrp_plat_mem_size(lgrp_handle_t plathand,
3972e2c009bSjjc     lgrp_mem_query_t query);
3982e2c009bSjjc 
3992e2c009bSjjc lgrp_handle_t	lgrp_plat_pfn_to_hand(pfn_t pfn);
4002e2c009bSjjc 
4012e2c009bSjjc void		lgrp_plat_probe(void);
4022e2c009bSjjc 
4032e2c009bSjjc lgrp_handle_t	lgrp_plat_root_hand(void);
4042e2c009bSjjc 
4052e2c009bSjjc 
4062e2c009bSjjc /*
4072e2c009bSjjc  * Forward declarations of local routines
4082e2c009bSjjc  */
4092e2c009bSjjc static int	is_opteron(void);
4102e2c009bSjjc 
411dae2fa37Sjjc static int	lgrp_plat_cpu_node_update(node_domain_map_t *node_domain,
412d821f0f0Sjjc     int node_cnt, cpu_node_map_t *cpu_node, int nentries, uint32_t apicid,
413d821f0f0Sjjc     uint32_t domain);
414dae2fa37Sjjc 
415d5d7cf4eSJonathan Chew static int	lgrp_plat_cpu_to_node(cpu_t *cp, cpu_node_map_t *cpu_node,
416d5d7cf4eSJonathan Chew     int cpu_node_nentries);
4172e2c009bSjjc 
4182e2c009bSjjc static int	lgrp_plat_domain_to_node(node_domain_map_t *node_domain,
419d821f0f0Sjjc     int node_cnt, uint32_t domain);
4202e2c009bSjjc 
421d5d7cf4eSJonathan Chew static void	lgrp_plat_get_numa_config(void);
422d5d7cf4eSJonathan Chew 
4232e2c009bSjjc static void	lgrp_plat_latency_adjust(node_phys_addr_map_t *node_memory,
4242e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats,
4252e2c009bSjjc     lgrp_plat_probe_stats_t *probe_stats);
4262e2c009bSjjc 
4272e2c009bSjjc static int	lgrp_plat_latency_verify(node_phys_addr_map_t *node_memory,
4282e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats);
4292e2c009bSjjc 
430d5d7cf4eSJonathan Chew static void	lgrp_plat_main_init(void);
431d5d7cf4eSJonathan Chew 
4322e2c009bSjjc static pgcnt_t	lgrp_plat_mem_size_default(lgrp_handle_t, lgrp_mem_query_t);
4332e2c009bSjjc 
4342e2c009bSjjc static int	lgrp_plat_node_domain_update(node_domain_map_t *node_domain,
435d821f0f0Sjjc     int node_cnt, uint32_t domain);
4362e2c009bSjjc 
4372e2c009bSjjc static int	lgrp_plat_node_memory_update(node_domain_map_t *node_domain,
438d821f0f0Sjjc     int node_cnt, node_phys_addr_map_t *node_memory, uint64_t start,
439d821f0f0Sjjc     uint64_t end, uint32_t domain);
4402e2c009bSjjc 
44181d9ccb6SJonathan Chew static void	lgrp_plat_node_sort(node_domain_map_t *node_domain,
44281d9ccb6SJonathan Chew     int node_cnt, cpu_node_map_t *cpu_node, int cpu_count,
44381d9ccb6SJonathan Chew     node_phys_addr_map_t *node_memory);
44481d9ccb6SJonathan Chew 
4452e2c009bSjjc static hrtime_t	lgrp_plat_probe_time(int to, cpu_node_map_t *cpu_node,
446d5d7cf4eSJonathan Chew     int cpu_node_nentries, lgrp_plat_probe_mem_config_t *probe_mem_config,
447d5d7cf4eSJonathan Chew     lgrp_plat_latency_stats_t *lat_stats, lgrp_plat_probe_stats_t *probe_stats);
4482e2c009bSjjc 
449d821f0f0Sjjc static int	lgrp_plat_process_cpu_apicids(cpu_node_map_t *cpu_node);
450dae2fa37Sjjc 
4512e2c009bSjjc static int	lgrp_plat_process_slit(struct slit *tp, uint_t node_cnt,
4522e2c009bSjjc     node_phys_addr_map_t *node_memory, lgrp_plat_latency_stats_t *lat_stats);
4532e2c009bSjjc 
454d821f0f0Sjjc static int	lgrp_plat_process_srat(struct srat *tp,
45581d9ccb6SJonathan Chew     uint32_t *prox_domain_min, node_domain_map_t *node_domain,
45681d9ccb6SJonathan Chew     cpu_node_map_t *cpu_node, int cpu_count,
4572e2c009bSjjc     node_phys_addr_map_t *node_memory);
4582e2c009bSjjc 
459d5d7cf4eSJonathan Chew static void	lgrp_plat_release_bootstrap(void);
460d5d7cf4eSJonathan Chew 
46181d9ccb6SJonathan Chew static int	lgrp_plat_srat_domains(struct srat *tp,
46281d9ccb6SJonathan Chew     uint32_t *prox_domain_min);
4632e2c009bSjjc 
4642e2c009bSjjc static void	lgrp_plat_2level_setup(node_phys_addr_map_t *node_memory,
4652e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats);
4662e2c009bSjjc 
4672e2c009bSjjc static void	opt_get_numa_config(uint_t *node_cnt, int *mem_intrlv,
4682e2c009bSjjc     node_phys_addr_map_t *node_memory);
4692e2c009bSjjc 
4702e2c009bSjjc static hrtime_t	opt_probe_vendor(int dest_node, int nreads);
4712e2c009bSjjc 
4722e2c009bSjjc 
4732e2c009bSjjc /*
4742e2c009bSjjc  * PLATFORM INTERFACE ROUTINES
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4782e2c009bSjjc  * Configure memory nodes for machines with more than one node (ie NUMA)
4792e2c009bSjjc  */
4802e2c009bSjjc void
4812e2c009bSjjc plat_build_mem_nodes(struct memlist *list)
4822e2c009bSjjc {
4832e2c009bSjjc 	pfn_t		cur_start;	/* start addr of subrange */
4842e2c009bSjjc 	pfn_t		cur_end;	/* end addr of subrange */
4852e2c009bSjjc 	pfn_t		start;		/* start addr of whole range */
4862e2c009bSjjc 	pfn_t		end;		/* end addr of whole range */
48718968004SKit Chow 	pgcnt_t		endcnt;		/* pages to sacrifice */
4882e2c009bSjjc 
4892e2c009bSjjc 	/*
4902e2c009bSjjc 	 * Boot install lists are arranged <addr, len>, ...
4912e2c009bSjjc 	 */
4922e2c009bSjjc 	while (list) {
4932e2c009bSjjc 		int	node;
4942e2c009bSjjc 
495*56f33205SJonathan Adams 		start = list->ml_address >> PAGESHIFT;
496*56f33205SJonathan Adams 		end = (list->ml_address + list->ml_size - 1) >> PAGESHIFT;
4972e2c009bSjjc 
4982e2c009bSjjc 		if (start > physmax) {
499*56f33205SJonathan Adams 			list = list->ml_next;
5002e2c009bSjjc 			continue;
5012e2c009bSjjc 		}
5022e2c009bSjjc 		if (end > physmax)
5032e2c009bSjjc 			end = physmax;
5042e2c009bSjjc 
5052e2c009bSjjc 		/*
5062e2c009bSjjc 		 * When there is only one memnode, just add memory to memnode
5072e2c009bSjjc 		 */
5082e2c009bSjjc 		if (max_mem_nodes == 1) {
5092e2c009bSjjc 			mem_node_add_slice(start, end);
510*56f33205SJonathan Adams 			list = list->ml_next;
5112e2c009bSjjc 			continue;
5122e2c009bSjjc 		}
5132e2c009bSjjc 
5142e2c009bSjjc 		/*
5152e2c009bSjjc 		 * mem_node_add_slice() expects to get a memory range that
5162e2c009bSjjc 		 * is within one memnode, so need to split any memory range
5172e2c009bSjjc 		 * that spans multiple memnodes into subranges that are each
5182e2c009bSjjc 		 * contained within one memnode when feeding them to
5192e2c009bSjjc 		 * mem_node_add_slice()
5202e2c009bSjjc 		 */
5212e2c009bSjjc 		cur_start = start;
5222e2c009bSjjc 		do {
5232e2c009bSjjc 			node = plat_pfn_to_mem_node(cur_start);
5242e2c009bSjjc 
5252e2c009bSjjc 			/*
5262e2c009bSjjc 			 * Panic if DRAM address map registers or SRAT say
5272e2c009bSjjc 			 * memory in node doesn't exist or address from
5282e2c009bSjjc 			 * boot installed memory list entry isn't in this node.
5292e2c009bSjjc 			 * This shouldn't happen and rest of code can't deal
5302e2c009bSjjc 			 * with this if it does.
5312e2c009bSjjc 			 */
5322e2c009bSjjc 			if (node < 0 || node >= lgrp_plat_node_cnt ||
5332e2c009bSjjc 			    !lgrp_plat_node_memory[node].exists ||
5342e2c009bSjjc 			    cur_start < lgrp_plat_node_memory[node].start ||
5352e2c009bSjjc 			    cur_start > lgrp_plat_node_memory[node].end) {
5362e2c009bSjjc 				cmn_err(CE_PANIC, "Don't know which memnode "
5372e2c009bSjjc 				    "to add installed memory address 0x%lx\n",
5382e2c009bSjjc 				    cur_start);
5392e2c009bSjjc 			}
5402e2c009bSjjc 
5412e2c009bSjjc 			/*
5422e2c009bSjjc 			 * End of current subrange should not span memnodes
5432e2c009bSjjc 			 */
5442e2c009bSjjc 			cur_end = end;
54518968004SKit Chow 			endcnt = 0;
5462e2c009bSjjc 			if (lgrp_plat_node_memory[node].exists &&
54718968004SKit Chow 			    cur_end > lgrp_plat_node_memory[node].end) {
5482e2c009bSjjc 				cur_end = lgrp_plat_node_memory[node].end;
54918968004SKit Chow 				if (mnode_xwa > 1) {
55018968004SKit Chow 					/*
55118968004SKit Chow 					 * sacrifice the last page in each
55218968004SKit Chow 					 * node to eliminate large pages
55318968004SKit Chow 					 * that span more than 1 memory node.
55418968004SKit Chow 					 */
55518968004SKit Chow 					endcnt = 1;
556bcee7a0bSKit Chow 					physinstalled--;
55718968004SKit Chow 				}
55818968004SKit Chow 			}
5592e2c009bSjjc 
56018968004SKit Chow 			mem_node_add_slice(cur_start, cur_end - endcnt);
5612e2c009bSjjc 
5622e2c009bSjjc 			/*
5632e2c009bSjjc 			 * Next subrange starts after end of current one
5642e2c009bSjjc 			 */
5652e2c009bSjjc 			cur_start = cur_end + 1;
5662e2c009bSjjc 		} while (cur_end < end);
5672e2c009bSjjc 
568*56f33205SJonathan Adams 		list = list->ml_next;
5692e2c009bSjjc 	}
5702e2c009bSjjc 	mem_node_physalign = 0;
5712e2c009bSjjc 	mem_node_pfn_shift = 0;
5722e2c009bSjjc }
5732e2c009bSjjc 
5742e2c009bSjjc 
5752e2c009bSjjc int
5762e2c009bSjjc plat_lgrphand_to_mem_node(lgrp_handle_t hand)
5772e2c009bSjjc {
5782e2c009bSjjc 	if (max_mem_nodes == 1)
5792e2c009bSjjc 		return (0);
5802e2c009bSjjc 
5812e2c009bSjjc 	return ((int)hand);
5822e2c009bSjjc }
5832e2c009bSjjc 
5842e2c009bSjjc 
5852e2c009bSjjc /*
5862e2c009bSjjc  * plat_mnode_xcheck: checks the node memory ranges to see if there is a pfncnt
5872e2c009bSjjc  * range of pages aligned on pfncnt that crosses an node boundary. Returns 1 if
5882e2c009bSjjc  * a crossing is found and returns 0 otherwise.
5892e2c009bSjjc  */
5902e2c009bSjjc int
5912e2c009bSjjc plat_mnode_xcheck(pfn_t pfncnt)
5922e2c009bSjjc {
5932e2c009bSjjc 	int	node, prevnode = -1, basenode;
5942e2c009bSjjc 	pfn_t	ea, sa;
5952e2c009bSjjc 
5962e2c009bSjjc 	for (node = 0; node < lgrp_plat_node_cnt; node++) {
5972e2c009bSjjc 
5982e2c009bSjjc 		if (lgrp_plat_node_memory[node].exists == 0)
5992e2c009bSjjc 			continue;
6002e2c009bSjjc 
6012e2c009bSjjc 		if (prevnode == -1) {
6022e2c009bSjjc 			prevnode = node;
6032e2c009bSjjc 			basenode = node;
6042e2c009bSjjc 			continue;
6052e2c009bSjjc 		}
6062e2c009bSjjc 
6072e2c009bSjjc 		/* assume x86 node pfn ranges are in increasing order */
6082e2c009bSjjc 		ASSERT(lgrp_plat_node_memory[node].start >
6092e2c009bSjjc 		    lgrp_plat_node_memory[prevnode].end);
6102e2c009bSjjc 
6112e2c009bSjjc 		/*
6122e2c009bSjjc 		 * continue if the starting address of node is not contiguous
6132e2c009bSjjc 		 * with the previous node.
6142e2c009bSjjc 		 */
6152e2c009bSjjc 
6162e2c009bSjjc 		if (lgrp_plat_node_memory[node].start !=
6172e2c009bSjjc 		    (lgrp_plat_node_memory[prevnode].end + 1)) {
6182e2c009bSjjc 			basenode = node;
6192e2c009bSjjc 			prevnode = node;
6202e2c009bSjjc 			continue;
6212e2c009bSjjc 		}
6222e2c009bSjjc 
6232e2c009bSjjc 		/* check if the starting address of node is pfncnt aligned */
6242e2c009bSjjc 		if ((lgrp_plat_node_memory[node].start & (pfncnt - 1)) != 0) {
6252e2c009bSjjc 
6262e2c009bSjjc 			/*
6272e2c009bSjjc 			 * at this point, node starts at an unaligned boundary
6282e2c009bSjjc 			 * and is contiguous with the previous node(s) to
6292e2c009bSjjc 			 * basenode. Check if there is an aligned contiguous
6302e2c009bSjjc 			 * range of length pfncnt that crosses this boundary.
6312e2c009bSjjc 			 */
6322e2c009bSjjc 
6332e2c009bSjjc 			sa = P2ALIGN(lgrp_plat_node_memory[prevnode].end,
6342e2c009bSjjc 			    pfncnt);
6352e2c009bSjjc 			ea = P2ROUNDUP((lgrp_plat_node_memory[node].start),
6362e2c009bSjjc 			    pfncnt);
6372e2c009bSjjc 
6382e2c009bSjjc 			ASSERT((ea - sa) == pfncnt);
6392e2c009bSjjc 			if (sa >= lgrp_plat_node_memory[basenode].start &&
64018968004SKit Chow 			    ea <= (lgrp_plat_node_memory[node].end + 1)) {
64118968004SKit Chow 				/*
64218968004SKit Chow 				 * large page found to cross mnode boundary.
64318968004SKit Chow 				 * Return Failure if workaround not enabled.
64418968004SKit Chow 				 */
64518968004SKit Chow 				if (mnode_xwa == 0)
6462e2c009bSjjc 					return (1);
64718968004SKit Chow 				mnode_xwa++;
64818968004SKit Chow 			}
6492e2c009bSjjc 		}
6502e2c009bSjjc 		prevnode = node;
6512e2c009bSjjc 	}
6522e2c009bSjjc 	return (0);
6532e2c009bSjjc }
6542e2c009bSjjc 
6552e2c009bSjjc 
6562e2c009bSjjc lgrp_handle_t
6572e2c009bSjjc plat_mem_node_to_lgrphand(int mnode)
6582e2c009bSjjc {
6592e2c009bSjjc 	if (max_mem_nodes == 1)
6602e2c009bSjjc 		return (LGRP_DEFAULT_HANDLE);
6612e2c009bSjjc 
6622e2c009bSjjc 	return ((lgrp_handle_t)mnode);
6632e2c009bSjjc }
6642e2c009bSjjc 
6652e2c009bSjjc 
6662e2c009bSjjc int
6672e2c009bSjjc plat_pfn_to_mem_node(pfn_t pfn)
6682e2c009bSjjc {
6692e2c009bSjjc 	int	node;
6702e2c009bSjjc 
6712e2c009bSjjc 	if (max_mem_nodes == 1)
6722e2c009bSjjc 		return (0);
6732e2c009bSjjc 
6742e2c009bSjjc 	for (node = 0; node < lgrp_plat_node_cnt; node++) {
6752e2c009bSjjc 		/*
6762e2c009bSjjc 		 * Skip nodes with no memory
6772e2c009bSjjc 		 */
6782e2c009bSjjc 		if (!lgrp_plat_node_memory[node].exists)
6792e2c009bSjjc 			continue;
6802e2c009bSjjc 
6812e2c009bSjjc 		if (pfn >= lgrp_plat_node_memory[node].start &&
6822e2c009bSjjc 		    pfn <= lgrp_plat_node_memory[node].end)
6832e2c009bSjjc 			return (node);
6842e2c009bSjjc 	}
6852e2c009bSjjc 
6862e2c009bSjjc 	/*
6872e2c009bSjjc 	 * Didn't find memnode where this PFN lives which should never happen
6882e2c009bSjjc 	 */
6892e2c009bSjjc 	ASSERT(node < lgrp_plat_node_cnt);
6902e2c009bSjjc 	return (-1);
6912e2c009bSjjc }
6922e2c009bSjjc 
6932e2c009bSjjc 
6942e2c009bSjjc /*
6952e2c009bSjjc  * LGROUP PLATFORM INTERFACE ROUTINES
6962e2c009bSjjc  */
6972e2c009bSjjc 
6982e2c009bSjjc /*
6992e2c009bSjjc  * Allocate additional space for an lgroup.
7002e2c009bSjjc  */
7012e2c009bSjjc /* ARGSUSED */
7022e2c009bSjjc lgrp_t *
7032e2c009bSjjc lgrp_plat_alloc(lgrp_id_t lgrpid)
7042e2c009bSjjc {
7052e2c009bSjjc 	lgrp_t *lgrp;
7062e2c009bSjjc 
7072e2c009bSjjc 	lgrp = &lgrp_space[nlgrps_alloc++];
7082e2c009bSjjc 	if (lgrpid >= NLGRP || nlgrps_alloc > NLGRP)
7092e2c009bSjjc 		return (NULL);
7102e2c009bSjjc 	return (lgrp);
7112e2c009bSjjc }
7122e2c009bSjjc 
7132e2c009bSjjc 
7142e2c009bSjjc /*
7152e2c009bSjjc  * Platform handling for (re)configuration changes
7162e2c009bSjjc  */
7172e2c009bSjjc /* ARGSUSED */
7182e2c009bSjjc void
7192e2c009bSjjc lgrp_plat_config(lgrp_config_flag_t flag, uintptr_t arg)
7202e2c009bSjjc {
7212e2c009bSjjc }
7222e2c009bSjjc 
7232e2c009bSjjc 
7242e2c009bSjjc /*
7252e2c009bSjjc  * Return the platform handle for the lgroup containing the given CPU
7262e2c009bSjjc  */
7272e2c009bSjjc /* ARGSUSED */
7282e2c009bSjjc lgrp_handle_t
7292e2c009bSjjc lgrp_plat_cpu_to_hand(processorid_t id)
7302e2c009bSjjc {
7312e2c009bSjjc 	lgrp_handle_t	hand;
7322e2c009bSjjc 
7332e2c009bSjjc 	if (lgrp_plat_node_cnt == 1)
7342e2c009bSjjc 		return (LGRP_DEFAULT_HANDLE);
7352e2c009bSjjc 
7362e2c009bSjjc 	hand = (lgrp_handle_t)lgrp_plat_cpu_to_node(cpu[id],
737d5d7cf4eSJonathan Chew 	    lgrp_plat_cpu_node, lgrp_plat_cpu_node_nentries);
7382e2c009bSjjc 
7392e2c009bSjjc 	ASSERT(hand != (lgrp_handle_t)-1);
7402e2c009bSjjc 	if (hand == (lgrp_handle_t)-1)
7412e2c009bSjjc 		return (LGRP_NULL_HANDLE);
7422e2c009bSjjc 
7432e2c009bSjjc 	return (hand);
7442e2c009bSjjc }
7452e2c009bSjjc 
7462e2c009bSjjc 
7472e2c009bSjjc /*
7482e2c009bSjjc  * Platform-specific initialization of lgroups
7492e2c009bSjjc  */
7502e2c009bSjjc void
751d5d7cf4eSJonathan Chew lgrp_plat_init(lgrp_init_stages_t stage)
7522e2c009bSjjc {
7532e2c009bSjjc #if defined(__xpv)
754d5d7cf4eSJonathan Chew #else	/* __xpv */
755d5d7cf4eSJonathan Chew 	u_longlong_t	value;
756d5d7cf4eSJonathan Chew #endif	/* __xpv */
757d5d7cf4eSJonathan Chew 
758d5d7cf4eSJonathan Chew 	switch (stage) {
759d5d7cf4eSJonathan Chew 	case LGRP_INIT_STAGE1:
760d5d7cf4eSJonathan Chew #if defined(__xpv)
7612e2c009bSjjc 		/*
7622e2c009bSjjc 		 * XXPV	For now, the hypervisor treats all memory equally.
7632e2c009bSjjc 		 */
7642e2c009bSjjc 		lgrp_plat_node_cnt = max_mem_nodes = 1;
7652e2c009bSjjc #else	/* __xpv */
7662baa66a0SJonathan Chew 		/*
7672baa66a0SJonathan Chew 		 * Get boot property for lgroup topology height limit
7682baa66a0SJonathan Chew 		 */
7692baa66a0SJonathan Chew 		if (bootprop_getval(BP_LGRP_TOPO_LEVELS, &value) == 0)
7702baa66a0SJonathan Chew 			(void) lgrp_topo_ht_limit_set((int)value);
7712baa66a0SJonathan Chew 
7722baa66a0SJonathan Chew 		/*
7732baa66a0SJonathan Chew 		 * Get boot property for enabling/disabling SRAT
7742baa66a0SJonathan Chew 		 */
7752baa66a0SJonathan Chew 		if (bootprop_getval(BP_LGRP_SRAT_ENABLE, &value) == 0)
7762baa66a0SJonathan Chew 			lgrp_plat_srat_enable = (int)value;
7772baa66a0SJonathan Chew 
7782baa66a0SJonathan Chew 		/*
7792baa66a0SJonathan Chew 		 * Get boot property for enabling/disabling SLIT
7802baa66a0SJonathan Chew 		 */
7812baa66a0SJonathan Chew 		if (bootprop_getval(BP_LGRP_SLIT_ENABLE, &value) == 0)
7822baa66a0SJonathan Chew 			lgrp_plat_slit_enable = (int)value;
7832e2c009bSjjc 
7842e2c009bSjjc 		/*
7852e2c009bSjjc 		 * Initialize as a UMA machine
7862e2c009bSjjc 		 */
7872e2c009bSjjc 		if (lgrp_topo_ht_limit() == 1) {
7882e2c009bSjjc 			lgrp_plat_node_cnt = max_mem_nodes = 1;
7892e2c009bSjjc 			return;
7902e2c009bSjjc 		}
7912e2c009bSjjc 
792d5d7cf4eSJonathan Chew 		lgrp_plat_get_numa_config();
7932e2c009bSjjc #endif	/* __xpv */
794d5d7cf4eSJonathan Chew 		break;
795d5d7cf4eSJonathan Chew 
796d5d7cf4eSJonathan Chew 	case LGRP_INIT_STAGE3:
797d5d7cf4eSJonathan Chew 		lgrp_plat_probe();
798d5d7cf4eSJonathan Chew 		lgrp_plat_release_bootstrap();
799d5d7cf4eSJonathan Chew 		break;
800d5d7cf4eSJonathan Chew 
801d5d7cf4eSJonathan Chew 	case LGRP_INIT_STAGE4:
802d5d7cf4eSJonathan Chew 		lgrp_plat_main_init();
803d5d7cf4eSJonathan Chew 		break;
804d5d7cf4eSJonathan Chew 
805d5d7cf4eSJonathan Chew 	default:
806d5d7cf4eSJonathan Chew 		break;
807d5d7cf4eSJonathan Chew 	}
8082e2c009bSjjc }
8092e2c009bSjjc 
8102e2c009bSjjc 
8112e2c009bSjjc /*
8122e2c009bSjjc  * Return latency between "from" and "to" lgroups
8132e2c009bSjjc  *
8142e2c009bSjjc  * This latency number can only be used for relative comparison
8152e2c009bSjjc  * between lgroups on the running system, cannot be used across platforms,
8162e2c009bSjjc  * and may not reflect the actual latency.  It is platform and implementation
8172e2c009bSjjc  * specific, so platform gets to decide its value.  It would be nice if the
8182e2c009bSjjc  * number was at least proportional to make comparisons more meaningful though.
8192e2c009bSjjc  */
8202e2c009bSjjc /* ARGSUSED */
8212e2c009bSjjc int
8222e2c009bSjjc lgrp_plat_latency(lgrp_handle_t from, lgrp_handle_t to)
8232e2c009bSjjc {
8242e2c009bSjjc 	lgrp_handle_t	src, dest;
8252e2c009bSjjc 	int		node;
8262e2c009bSjjc 
8272e2c009bSjjc 	if (max_mem_nodes == 1)
8282e2c009bSjjc 		return (0);
8292e2c009bSjjc 
8302e2c009bSjjc 	/*
8312e2c009bSjjc 	 * Return max latency for root lgroup
8322e2c009bSjjc 	 */
8332e2c009bSjjc 	if (from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE)
8342e2c009bSjjc 		return (lgrp_plat_lat_stats.latency_max);
8352e2c009bSjjc 
8362e2c009bSjjc 	src = from;
8372e2c009bSjjc 	dest = to;
8382e2c009bSjjc 
8392e2c009bSjjc 	/*
8402e2c009bSjjc 	 * Return 0 for nodes (lgroup platform handles) out of range
8412e2c009bSjjc 	 */
8422e2c009bSjjc 	if (src < 0 || src >= MAX_NODES || dest < 0 || dest >= MAX_NODES)
8432e2c009bSjjc 		return (0);
8442e2c009bSjjc 
8452e2c009bSjjc 	/*
8462e2c009bSjjc 	 * Probe from current CPU if its lgroup latencies haven't been set yet
8472e2c009bSjjc 	 * and we are trying to get latency from current CPU to some node
8482e2c009bSjjc 	 */
849d5d7cf4eSJonathan Chew 	node = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node,
850d5d7cf4eSJonathan Chew 	    lgrp_plat_cpu_node_nentries);
8512e2c009bSjjc 	ASSERT(node >= 0 && node < lgrp_plat_node_cnt);
8522e2c009bSjjc 	if (lgrp_plat_lat_stats.latencies[src][src] == 0 && node == src)
8532e2c009bSjjc 		lgrp_plat_probe();
8542e2c009bSjjc 
8552e2c009bSjjc 	return (lgrp_plat_lat_stats.latencies[src][dest]);
8562e2c009bSjjc }
8572e2c009bSjjc 
8582e2c009bSjjc 
8592e2c009bSjjc /*
8602e2c009bSjjc  * Return the maximum number of lgrps supported by the platform.
8612e2c009bSjjc  * Before lgrp topology is known it returns an estimate based on the number of
8622e2c009bSjjc  * nodes. Once topology is known it returns the actual maximim number of lgrps
8632e2c009bSjjc  * created. Since x86/x64 doesn't support Dynamic Reconfiguration (DR) and
8642e2c009bSjjc  * dynamic addition of new nodes, this number may not grow during system
8652e2c009bSjjc  * lifetime (yet).
8662e2c009bSjjc  */
8672e2c009bSjjc int
8682e2c009bSjjc lgrp_plat_max_lgrps(void)
8692e2c009bSjjc {
8702e2c009bSjjc 	return (lgrp_topo_initialized ?
8712e2c009bSjjc 	    lgrp_alloc_max + 1 :
8722e2c009bSjjc 	    lgrp_plat_node_cnt * (lgrp_plat_node_cnt - 1) + 1);
8732e2c009bSjjc }
8742e2c009bSjjc 
8752e2c009bSjjc 
8762e2c009bSjjc /*
8772e2c009bSjjc  * Return the number of free pages in an lgroup.
8782e2c009bSjjc  *
8792e2c009bSjjc  * For query of LGRP_MEM_SIZE_FREE, return the number of base pagesize
8802e2c009bSjjc  * pages on freelists.  For query of LGRP_MEM_SIZE_AVAIL, return the
8812e2c009bSjjc  * number of allocatable base pagesize pages corresponding to the
8822e2c009bSjjc  * lgroup (e.g. do not include page_t's, BOP_ALLOC()'ed memory, ..)
8832e2c009bSjjc  * For query of LGRP_MEM_SIZE_INSTALL, return the amount of physical
8842e2c009bSjjc  * memory installed, regardless of whether or not it's usable.
8852e2c009bSjjc  */
8862e2c009bSjjc pgcnt_t
8872e2c009bSjjc lgrp_plat_mem_size(lgrp_handle_t plathand, lgrp_mem_query_t query)
8882e2c009bSjjc {
8892e2c009bSjjc 	int	mnode;
8902e2c009bSjjc 	pgcnt_t npgs = (pgcnt_t)0;
8912e2c009bSjjc 	extern struct memlist *phys_avail;
8922e2c009bSjjc 	extern struct memlist *phys_install;
8932e2c009bSjjc 
8942e2c009bSjjc 
8952e2c009bSjjc 	if (plathand == LGRP_DEFAULT_HANDLE)
8962e2c009bSjjc 		return (lgrp_plat_mem_size_default(plathand, query));
8972e2c009bSjjc 
8982e2c009bSjjc 	if (plathand != LGRP_NULL_HANDLE) {
8992e2c009bSjjc 		mnode = plat_lgrphand_to_mem_node(plathand);
9002e2c009bSjjc 		if (mnode >= 0 && mem_node_config[mnode].exists) {
9012e2c009bSjjc 			switch (query) {
9022e2c009bSjjc 			case LGRP_MEM_SIZE_FREE:
9032e2c009bSjjc 				npgs = MNODE_PGCNT(mnode);
9042e2c009bSjjc 				break;
9052e2c009bSjjc 			case LGRP_MEM_SIZE_AVAIL:
9062e2c009bSjjc 				npgs = mem_node_memlist_pages(mnode,
9072e2c009bSjjc 				    phys_avail);
9082e2c009bSjjc 				break;
9092e2c009bSjjc 			case LGRP_MEM_SIZE_INSTALL:
9102e2c009bSjjc 				npgs = mem_node_memlist_pages(mnode,
9112e2c009bSjjc 				    phys_install);
9122e2c009bSjjc 				break;
9132e2c009bSjjc 			default:
9142e2c009bSjjc 				break;
9152e2c009bSjjc 			}
9162e2c009bSjjc 		}
9172e2c009bSjjc 	}
9182e2c009bSjjc 	return (npgs);
9192e2c009bSjjc }
9202e2c009bSjjc 
9212e2c009bSjjc 
9222e2c009bSjjc /*
9232e2c009bSjjc  * Return the platform handle of the lgroup that contains the physical memory
9242e2c009bSjjc  * corresponding to the given page frame number
9252e2c009bSjjc  */
9262e2c009bSjjc /* ARGSUSED */
9272e2c009bSjjc lgrp_handle_t
9282e2c009bSjjc lgrp_plat_pfn_to_hand(pfn_t pfn)
9292e2c009bSjjc {
9302e2c009bSjjc 	int	mnode;
9312e2c009bSjjc 
9322e2c009bSjjc 	if (max_mem_nodes == 1)
9332e2c009bSjjc 		return (LGRP_DEFAULT_HANDLE);
9342e2c009bSjjc 
9352e2c009bSjjc 	if (pfn > physmax)
9362e2c009bSjjc 		return (LGRP_NULL_HANDLE);
9372e2c009bSjjc 
9382e2c009bSjjc 	mnode = plat_pfn_to_mem_node(pfn);
9392e2c009bSjjc 	if (mnode < 0)
9402e2c009bSjjc 		return (LGRP_NULL_HANDLE);
9412e2c009bSjjc 
9422e2c009bSjjc 	return (MEM_NODE_2_LGRPHAND(mnode));
9432e2c009bSjjc }
9442e2c009bSjjc 
9452e2c009bSjjc 
9462e2c009bSjjc /*
9472e2c009bSjjc  * Probe memory in each node from current CPU to determine latency topology
9482e2c009bSjjc  *
9492e2c009bSjjc  * The probing code will probe the vendor ID register on the Northbridge of
9502e2c009bSjjc  * Opteron processors and probe memory for other processors by default.
9512e2c009bSjjc  *
9522e2c009bSjjc  * Since probing is inherently error prone, the code takes laps across all the
9532e2c009bSjjc  * nodes probing from each node to each of the other nodes some number of
9542e2c009bSjjc  * times.  Furthermore, each node is probed some number of times before moving
9552e2c009bSjjc  * onto the next one during each lap.  The minimum latency gotten between nodes
9562e2c009bSjjc  * is kept as the latency between the nodes.
9572e2c009bSjjc  *
9582e2c009bSjjc  * After all that,  the probe times are adjusted by normalizing values that are
9592e2c009bSjjc  * close to each other and local latencies are made the same.  Lastly, the
9602e2c009bSjjc  * latencies are verified to make sure that certain conditions are met (eg.
9612e2c009bSjjc  * local < remote, latency(a, b) == latency(b, a), etc.).
9622e2c009bSjjc  *
9632e2c009bSjjc  * If any of the conditions aren't met, the code will export a NUMA
9642e2c009bSjjc  * configuration with the local CPUs and memory given by the SRAT or PCI config
9652e2c009bSjjc  * space registers and one remote memory latency since it can't tell exactly
9662e2c009bSjjc  * how far each node is from each other.
9672e2c009bSjjc  */
9682e2c009bSjjc void
9692e2c009bSjjc lgrp_plat_probe(void)
9702e2c009bSjjc {
9712e2c009bSjjc 	int				from;
9722e2c009bSjjc 	int				i;
9732e2c009bSjjc 	lgrp_plat_latency_stats_t	*lat_stats;
9741ce8847aSJonathan Chew 	boolean_t			probed;
9752e2c009bSjjc 	hrtime_t			probe_time;
9762e2c009bSjjc 	int				to;
9772e2c009bSjjc 
9782e2c009bSjjc 	if (!(lgrp_plat_probe_flags & LGRP_PLAT_PROBE_ENABLE) ||
9792e2c009bSjjc 	    max_mem_nodes == 1 || lgrp_topo_ht_limit() <= 2)
9802e2c009bSjjc 		return;
9812e2c009bSjjc 
9822e2c009bSjjc 	/*
9832e2c009bSjjc 	 * Determine ID of node containing current CPU
9842e2c009bSjjc 	 */
985d5d7cf4eSJonathan Chew 	from = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node,
986d5d7cf4eSJonathan Chew 	    lgrp_plat_cpu_node_nentries);
9872e2c009bSjjc 	ASSERT(from >= 0 && from < lgrp_plat_node_cnt);
9882e2c009bSjjc 	if (srat_ptr && lgrp_plat_srat_enable && !lgrp_plat_srat_error)
9892e2c009bSjjc 		ASSERT(lgrp_plat_node_domain[from].exists);
9902e2c009bSjjc 
9912e2c009bSjjc 	/*
9922e2c009bSjjc 	 * Don't need to probe if got times already
9932e2c009bSjjc 	 */
9942e2c009bSjjc 	lat_stats = &lgrp_plat_lat_stats;
9952e2c009bSjjc 	if (lat_stats->latencies[from][from] != 0)
9962e2c009bSjjc 		return;
9972e2c009bSjjc 
9982e2c009bSjjc 	/*
9992e2c009bSjjc 	 * Read vendor ID in Northbridge or read and write page(s)
10002e2c009bSjjc 	 * in each node from current CPU and remember how long it takes,
10012e2c009bSjjc 	 * so we can build latency topology of machine later.
10022e2c009bSjjc 	 * This should approximate the memory latency between each node.
10032e2c009bSjjc 	 */
10041ce8847aSJonathan Chew 	probed = B_FALSE;
10052e2c009bSjjc 	for (i = 0; i < lgrp_plat_probe_nrounds; i++) {
10062e2c009bSjjc 		for (to = 0; to < lgrp_plat_node_cnt; to++) {
10072e2c009bSjjc 			/*
10081ce8847aSJonathan Chew 			 * Get probe time and skip over any nodes that can't be
10091ce8847aSJonathan Chew 			 * probed yet or don't have memory
10102e2c009bSjjc 			 */
10112e2c009bSjjc 			probe_time = lgrp_plat_probe_time(to,
1012d5d7cf4eSJonathan Chew 			    lgrp_plat_cpu_node, lgrp_plat_cpu_node_nentries,
1013d5d7cf4eSJonathan Chew 			    &lgrp_plat_probe_mem_config, &lgrp_plat_lat_stats,
1014d5d7cf4eSJonathan Chew 			    &lgrp_plat_probe_stats);
10152e2c009bSjjc 			if (probe_time == 0)
10161ce8847aSJonathan Chew 				continue;
10171ce8847aSJonathan Chew 
10181ce8847aSJonathan Chew 			probed = B_TRUE;
10192e2c009bSjjc 
10202e2c009bSjjc 			/*
10212e2c009bSjjc 			 * Keep lowest probe time as latency between nodes
10222e2c009bSjjc 			 */
10232e2c009bSjjc 			if (lat_stats->latencies[from][to] == 0 ||
10242e2c009bSjjc 			    probe_time < lat_stats->latencies[from][to])
10252e2c009bSjjc 				lat_stats->latencies[from][to] = probe_time;
10262e2c009bSjjc 
10272e2c009bSjjc 			/*
10282e2c009bSjjc 			 * Update overall minimum and maximum probe times
10292e2c009bSjjc 			 * across all nodes
10302e2c009bSjjc 			 */
10312e2c009bSjjc 			if (probe_time < lat_stats->latency_min ||
10322e2c009bSjjc 			    lat_stats->latency_min == -1)
10332e2c009bSjjc 				lat_stats->latency_min = probe_time;
10342e2c009bSjjc 			if (probe_time > lat_stats->latency_max)
10352e2c009bSjjc 				lat_stats->latency_max = probe_time;
10362e2c009bSjjc 		}
10372e2c009bSjjc 	}
10382e2c009bSjjc 
10392e2c009bSjjc 	/*
10401ce8847aSJonathan Chew 	 * Bail out if weren't able to probe any nodes from current CPU
10411ce8847aSJonathan Chew 	 */
10421ce8847aSJonathan Chew 	if (probed == B_FALSE)
10431ce8847aSJonathan Chew 		return;
10441ce8847aSJonathan Chew 
10451ce8847aSJonathan Chew 	/*
10462e2c009bSjjc 	 * - Fix up latencies such that local latencies are same,
10472e2c009bSjjc 	 *   latency(i, j) == latency(j, i), etc. (if possible)
10482e2c009bSjjc 	 *
10492e2c009bSjjc 	 * - Verify that latencies look ok
10502e2c009bSjjc 	 *
10512e2c009bSjjc 	 * - Fallback to just optimizing for local and remote if
10522e2c009bSjjc 	 *   latencies didn't look right
10532e2c009bSjjc 	 */
10542e2c009bSjjc 	lgrp_plat_latency_adjust(lgrp_plat_node_memory, &lgrp_plat_lat_stats,
10552e2c009bSjjc 	    &lgrp_plat_probe_stats);
10562e2c009bSjjc 	lgrp_plat_probe_stats.probe_error_code =
10572e2c009bSjjc 	    lgrp_plat_latency_verify(lgrp_plat_node_memory,
10582e2c009bSjjc 	    &lgrp_plat_lat_stats);
10592e2c009bSjjc 	if (lgrp_plat_probe_stats.probe_error_code)
10602e2c009bSjjc 		lgrp_plat_2level_setup(lgrp_plat_node_memory,
10612e2c009bSjjc 		    &lgrp_plat_lat_stats);
10622e2c009bSjjc }
10632e2c009bSjjc 
10642e2c009bSjjc 
10652e2c009bSjjc /*
10662e2c009bSjjc  * Return platform handle for root lgroup
10672e2c009bSjjc  */
10682e2c009bSjjc lgrp_handle_t
10692e2c009bSjjc lgrp_plat_root_hand(void)
10702e2c009bSjjc {
10712e2c009bSjjc 	return (LGRP_DEFAULT_HANDLE);
10722e2c009bSjjc }
10732e2c009bSjjc 
10742e2c009bSjjc 
10752e2c009bSjjc /*
10762e2c009bSjjc  * INTERNAL ROUTINES
10772e2c009bSjjc  */
10782e2c009bSjjc 
10792e2c009bSjjc 
10802e2c009bSjjc /*
10812e2c009bSjjc  * Update CPU to node mapping for given CPU and proximity domain (and returns
10822e2c009bSjjc  * negative numbers for errors and positive ones for success)
10832e2c009bSjjc  */
10842e2c009bSjjc static int
1085d821f0f0Sjjc lgrp_plat_cpu_node_update(node_domain_map_t *node_domain, int node_cnt,
1086dae2fa37Sjjc     cpu_node_map_t *cpu_node, int nentries, uint32_t apicid, uint32_t domain)
10872e2c009bSjjc {
10882e2c009bSjjc 	uint_t	i;
10892e2c009bSjjc 	int	node;
10902e2c009bSjjc 
10912e2c009bSjjc 	/*
10922e2c009bSjjc 	 * Get node number for proximity domain
10932e2c009bSjjc 	 */
1094d821f0f0Sjjc 	node = lgrp_plat_domain_to_node(node_domain, node_cnt, domain);
10952e2c009bSjjc 	if (node == -1) {
1096d821f0f0Sjjc 		node = lgrp_plat_node_domain_update(node_domain, node_cnt,
1097d821f0f0Sjjc 		    domain);
10982e2c009bSjjc 		if (node == -1)
10992e2c009bSjjc 			return (-1);
11002e2c009bSjjc 	}
11012e2c009bSjjc 
11022e2c009bSjjc 	/*
1103dae2fa37Sjjc 	 * Search for entry with given APIC ID and fill in its node and
1104dae2fa37Sjjc 	 * proximity domain IDs (if they haven't been set already)
11052e2c009bSjjc 	 */
1106dae2fa37Sjjc 	for (i = 0; i < nentries; i++) {
11072e2c009bSjjc 		/*
1108dae2fa37Sjjc 		 * Skip nonexistent entries and ones without matching APIC ID
11092e2c009bSjjc 		 */
1110dae2fa37Sjjc 		if (!cpu_node[i].exists || cpu_node[i].apicid != apicid)
1111dae2fa37Sjjc 			continue;
1112dae2fa37Sjjc 
11132e2c009bSjjc 		/*
1114dae2fa37Sjjc 		 * Just return if entry completely and correctly filled in
1115dae2fa37Sjjc 		 * already
11162e2c009bSjjc 		 */
11172e2c009bSjjc 		if (cpu_node[i].prox_domain == domain &&
11182e2c009bSjjc 		    cpu_node[i].node == node)
11192e2c009bSjjc 			return (1);
11202e2c009bSjjc 
11212e2c009bSjjc 		/*
1122dae2fa37Sjjc 		 * Fill in node and proximity domain IDs
11232e2c009bSjjc 		 */
11242e2c009bSjjc 		cpu_node[i].prox_domain = domain;
11252e2c009bSjjc 		cpu_node[i].node = node;
1126dae2fa37Sjjc 
11272e2c009bSjjc 		return (0);
11282e2c009bSjjc 	}
11292e2c009bSjjc 
11302e2c009bSjjc 	/*
1131dae2fa37Sjjc 	 * Return error when entry for APIC ID wasn't found in table
11322e2c009bSjjc 	 */
1133dae2fa37Sjjc 	return (-2);
11342e2c009bSjjc }
11352e2c009bSjjc 
11362e2c009bSjjc 
11372e2c009bSjjc /*
1138dae2fa37Sjjc  * Get node ID for given CPU
11392e2c009bSjjc  */
11402e2c009bSjjc static int
1141d5d7cf4eSJonathan Chew lgrp_plat_cpu_to_node(cpu_t *cp, cpu_node_map_t *cpu_node,
1142d5d7cf4eSJonathan Chew     int cpu_node_nentries)
11432e2c009bSjjc {
1144dae2fa37Sjjc 	processorid_t	cpuid;
11452e2c009bSjjc 
11462e2c009bSjjc 	if (cp == NULL)
11472e2c009bSjjc 		return (-1);
11482e2c009bSjjc 
1149dae2fa37Sjjc 	cpuid = cp->cpu_id;
1150dae2fa37Sjjc 	if (cpuid < 0 || cpuid >= max_ncpus)
1151dae2fa37Sjjc 		return (-1);
1152dae2fa37Sjjc 
11532e2c009bSjjc 	/*
11542e2c009bSjjc 	 * SRAT doesn't exist, isn't enabled, or there was an error processing
11558031591dSSrihari Venkatesan 	 * it, so return node ID for Opteron and -1 otherwise.
11562e2c009bSjjc 	 */
11572e2c009bSjjc 	if (srat_ptr == NULL || !lgrp_plat_srat_enable ||
11582e2c009bSjjc 	    lgrp_plat_srat_error) {
11592e2c009bSjjc 		if (is_opteron())
11608031591dSSrihari Venkatesan 			return (pg_plat_hw_instance_id(cp, PGHW_PROCNODE));
11612e2c009bSjjc 		return (-1);
11622e2c009bSjjc 	}
11632e2c009bSjjc 
11642e2c009bSjjc 	/*
1165dae2fa37Sjjc 	 * Return -1 when CPU to node ID mapping entry doesn't exist for given
1166dae2fa37Sjjc 	 * CPU
11672e2c009bSjjc 	 */
1168d5d7cf4eSJonathan Chew 	if (cpuid >= cpu_node_nentries || !cpu_node[cpuid].exists)
11692e2c009bSjjc 		return (-1);
1170dae2fa37Sjjc 
1171dae2fa37Sjjc 	return (cpu_node[cpuid].node);
11722e2c009bSjjc }
11732e2c009bSjjc 
11742e2c009bSjjc 
11752e2c009bSjjc /*
11762e2c009bSjjc  * Return node number for given proximity domain/system locality
11772e2c009bSjjc  */
11782e2c009bSjjc static int
1179d821f0f0Sjjc lgrp_plat_domain_to_node(node_domain_map_t *node_domain, int node_cnt,
1180d821f0f0Sjjc     uint32_t domain)
11812e2c009bSjjc {
11822e2c009bSjjc 	uint_t	node;
11832e2c009bSjjc 	uint_t	start;
11842e2c009bSjjc 
11852e2c009bSjjc 	/*
11862e2c009bSjjc 	 * Hash proximity domain ID into node to domain mapping table (array),
11872e2c009bSjjc 	 * search for entry with matching proximity domain ID, and return index
11882e2c009bSjjc 	 * of matching entry as node ID.
11892e2c009bSjjc 	 */
1190d821f0f0Sjjc 	node = start = NODE_DOMAIN_HASH(domain, node_cnt);
11912e2c009bSjjc 	do {
11922e2c009bSjjc 		if (node_domain[node].prox_domain == domain &&
11932e2c009bSjjc 		    node_domain[node].exists)
11942e2c009bSjjc 			return (node);
1195cf5755f2SJonathan Chew 		node = (node + 1) % node_cnt;
11962e2c009bSjjc 	} while (node != start);
11972e2c009bSjjc 	return (-1);
11982e2c009bSjjc }
11992e2c009bSjjc 
12002e2c009bSjjc 
12012e2c009bSjjc /*
1202d5d7cf4eSJonathan Chew  * Get NUMA configuration of machine
1203d5d7cf4eSJonathan Chew  */
1204d5d7cf4eSJonathan Chew static void
1205d5d7cf4eSJonathan Chew lgrp_plat_get_numa_config(void)
1206d5d7cf4eSJonathan Chew {
1207d5d7cf4eSJonathan Chew 	uint_t		probe_op;
1208d5d7cf4eSJonathan Chew 
1209d5d7cf4eSJonathan Chew 	/*
1210d5d7cf4eSJonathan Chew 	 * Read boot property with CPU to APIC ID mapping table/array to
1211d5d7cf4eSJonathan Chew 	 * determine number of CPUs
1212d5d7cf4eSJonathan Chew 	 */
1213d5d7cf4eSJonathan Chew 	lgrp_plat_apic_ncpus = lgrp_plat_process_cpu_apicids(NULL);
1214d5d7cf4eSJonathan Chew 
1215d5d7cf4eSJonathan Chew 	/*
1216d5d7cf4eSJonathan Chew 	 * Determine which CPUs and memory are local to each other and number
1217d5d7cf4eSJonathan Chew 	 * of NUMA nodes by reading ACPI System Resource Affinity Table (SRAT)
1218d5d7cf4eSJonathan Chew 	 */
1219d5d7cf4eSJonathan Chew 	if (lgrp_plat_apic_ncpus > 0) {
1220d5d7cf4eSJonathan Chew 		int	retval;
1221d5d7cf4eSJonathan Chew 
1222d5d7cf4eSJonathan Chew 		/*
1223d5d7cf4eSJonathan Chew 		 * Temporarily allocate boot memory to use for CPU to node
1224d5d7cf4eSJonathan Chew 		 * mapping since kernel memory allocator isn't alive yet
1225d5d7cf4eSJonathan Chew 		 */
1226d5d7cf4eSJonathan Chew 		lgrp_plat_cpu_node = (cpu_node_map_t *)BOP_ALLOC(bootops,
1227d5d7cf4eSJonathan Chew 		    NULL, lgrp_plat_apic_ncpus * sizeof (cpu_node_map_t),
1228d5d7cf4eSJonathan Chew 		    sizeof (int));
1229d5d7cf4eSJonathan Chew 
1230d5d7cf4eSJonathan Chew 		ASSERT(lgrp_plat_cpu_node != NULL);
1231d5d7cf4eSJonathan Chew 		if (lgrp_plat_cpu_node) {
1232d5d7cf4eSJonathan Chew 			lgrp_plat_cpu_node_nentries = lgrp_plat_apic_ncpus;
1233d5d7cf4eSJonathan Chew 			bzero(lgrp_plat_cpu_node, lgrp_plat_cpu_node_nentries *
1234d5d7cf4eSJonathan Chew 			    sizeof (cpu_node_map_t));
1235d5d7cf4eSJonathan Chew 		}
1236d5d7cf4eSJonathan Chew 
1237d5d7cf4eSJonathan Chew 		/*
1238d5d7cf4eSJonathan Chew 		 * Fill in CPU to node ID mapping table with APIC ID for each
1239d5d7cf4eSJonathan Chew 		 * CPU
1240d5d7cf4eSJonathan Chew 		 */
1241d5d7cf4eSJonathan Chew 		(void) lgrp_plat_process_cpu_apicids(lgrp_plat_cpu_node);
1242d5d7cf4eSJonathan Chew 
1243d5d7cf4eSJonathan Chew 		retval = lgrp_plat_process_srat(srat_ptr,
1244d5d7cf4eSJonathan Chew 		    &lgrp_plat_prox_domain_min,
1245d5d7cf4eSJonathan Chew 		    lgrp_plat_node_domain, lgrp_plat_cpu_node,
1246d5d7cf4eSJonathan Chew 		    lgrp_plat_apic_ncpus, lgrp_plat_node_memory);
1247d5d7cf4eSJonathan Chew 		if (retval <= 0) {
1248d5d7cf4eSJonathan Chew 			lgrp_plat_srat_error = retval;
1249d5d7cf4eSJonathan Chew 			lgrp_plat_node_cnt = 1;
1250d5d7cf4eSJonathan Chew 		} else {
1251d5d7cf4eSJonathan Chew 			lgrp_plat_srat_error = 0;
1252d5d7cf4eSJonathan Chew 			lgrp_plat_node_cnt = retval;
1253d5d7cf4eSJonathan Chew 		}
1254d5d7cf4eSJonathan Chew 	}
1255d5d7cf4eSJonathan Chew 
1256d5d7cf4eSJonathan Chew 	/*
1257d5d7cf4eSJonathan Chew 	 * Try to use PCI config space registers on Opteron if there's an error
1258d5d7cf4eSJonathan Chew 	 * processing CPU to APIC ID mapping or SRAT
1259d5d7cf4eSJonathan Chew 	 */
1260d5d7cf4eSJonathan Chew 	if ((lgrp_plat_apic_ncpus <= 0 || lgrp_plat_srat_error != 0) &&
1261d5d7cf4eSJonathan Chew 	    is_opteron())
1262d5d7cf4eSJonathan Chew 		opt_get_numa_config(&lgrp_plat_node_cnt, &lgrp_plat_mem_intrlv,
1263d5d7cf4eSJonathan Chew 		    lgrp_plat_node_memory);
1264d5d7cf4eSJonathan Chew 
1265d5d7cf4eSJonathan Chew 	/*
1266d5d7cf4eSJonathan Chew 	 * Don't bother to setup system for multiple lgroups and only use one
1267d5d7cf4eSJonathan Chew 	 * memory node when memory is interleaved between any nodes or there is
1268d5d7cf4eSJonathan Chew 	 * only one NUMA node
1269d5d7cf4eSJonathan Chew 	 *
1270d5d7cf4eSJonathan Chew 	 * NOTE: May need to change this for Dynamic Reconfiguration (DR)
1271d5d7cf4eSJonathan Chew 	 *	 when and if it happens for x86/x64
1272d5d7cf4eSJonathan Chew 	 */
1273d5d7cf4eSJonathan Chew 	if (lgrp_plat_mem_intrlv || lgrp_plat_node_cnt == 1) {
1274d5d7cf4eSJonathan Chew 		lgrp_plat_node_cnt = max_mem_nodes = 1;
1275d5d7cf4eSJonathan Chew 		(void) lgrp_topo_ht_limit_set(1);
1276d5d7cf4eSJonathan Chew 		return;
1277d5d7cf4eSJonathan Chew 	}
1278d5d7cf4eSJonathan Chew 
1279d5d7cf4eSJonathan Chew 	/*
1280d5d7cf4eSJonathan Chew 	 * Leaf lgroups on x86/x64 architectures contain one physical
1281d5d7cf4eSJonathan Chew 	 * processor chip. Tune lgrp_expand_proc_thresh and
1282d5d7cf4eSJonathan Chew 	 * lgrp_expand_proc_diff so that lgrp_choose() will spread
1283d5d7cf4eSJonathan Chew 	 * things out aggressively.
1284d5d7cf4eSJonathan Chew 	 */
1285d5d7cf4eSJonathan Chew 	lgrp_expand_proc_thresh = LGRP_LOADAVG_THREAD_MAX / 2;
1286d5d7cf4eSJonathan Chew 	lgrp_expand_proc_diff = 0;
1287d5d7cf4eSJonathan Chew 
1288d5d7cf4eSJonathan Chew 	/*
1289d5d7cf4eSJonathan Chew 	 * There should be one memnode (physical page free list(s)) for
1290d5d7cf4eSJonathan Chew 	 * each node
1291d5d7cf4eSJonathan Chew 	 */
1292d5d7cf4eSJonathan Chew 	max_mem_nodes = lgrp_plat_node_cnt;
1293d5d7cf4eSJonathan Chew 
1294d5d7cf4eSJonathan Chew 	/*
1295d5d7cf4eSJonathan Chew 	 * Initialize min and max latency before reading SLIT or probing
1296d5d7cf4eSJonathan Chew 	 */
1297d5d7cf4eSJonathan Chew 	lgrp_plat_lat_stats.latency_min = -1;
1298d5d7cf4eSJonathan Chew 	lgrp_plat_lat_stats.latency_max = 0;
1299d5d7cf4eSJonathan Chew 
1300d5d7cf4eSJonathan Chew 	/*
1301d5d7cf4eSJonathan Chew 	 * Determine how far each NUMA node is from each other by
1302d5d7cf4eSJonathan Chew 	 * reading ACPI System Locality Information Table (SLIT) if it
1303d5d7cf4eSJonathan Chew 	 * exists
1304d5d7cf4eSJonathan Chew 	 */
1305d5d7cf4eSJonathan Chew 	lgrp_plat_slit_error = lgrp_plat_process_slit(slit_ptr,
1306d5d7cf4eSJonathan Chew 	    lgrp_plat_node_cnt, lgrp_plat_node_memory,
1307d5d7cf4eSJonathan Chew 	    &lgrp_plat_lat_stats);
1308d5d7cf4eSJonathan Chew 	if (lgrp_plat_slit_error == 0)
1309d5d7cf4eSJonathan Chew 		return;
1310d5d7cf4eSJonathan Chew 
1311d5d7cf4eSJonathan Chew 	/*
1312d5d7cf4eSJonathan Chew 	 * Probe to determine latency between NUMA nodes when SLIT
1313d5d7cf4eSJonathan Chew 	 * doesn't exist or make sense
1314d5d7cf4eSJonathan Chew 	 */
1315d5d7cf4eSJonathan Chew 	lgrp_plat_probe_flags |= LGRP_PLAT_PROBE_ENABLE;
1316d5d7cf4eSJonathan Chew 
1317d5d7cf4eSJonathan Chew 	/*
1318d5d7cf4eSJonathan Chew 	 * Specify whether to probe using vendor ID register or page copy
1319d5d7cf4eSJonathan Chew 	 * if hasn't been specified already or is overspecified
1320d5d7cf4eSJonathan Chew 	 */
1321d5d7cf4eSJonathan Chew 	probe_op = lgrp_plat_probe_flags &
1322d5d7cf4eSJonathan Chew 	    (LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR);
1323d5d7cf4eSJonathan Chew 
1324d5d7cf4eSJonathan Chew 	if (probe_op == 0 ||
1325d5d7cf4eSJonathan Chew 	    probe_op == (LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR)) {
1326d5d7cf4eSJonathan Chew 		lgrp_plat_probe_flags &=
1327d5d7cf4eSJonathan Chew 		    ~(LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR);
1328d5d7cf4eSJonathan Chew 		if (is_opteron())
1329d5d7cf4eSJonathan Chew 			lgrp_plat_probe_flags |=
1330d5d7cf4eSJonathan Chew 			    LGRP_PLAT_PROBE_VENDOR;
1331d5d7cf4eSJonathan Chew 		else
1332d5d7cf4eSJonathan Chew 			lgrp_plat_probe_flags |= LGRP_PLAT_PROBE_PGCPY;
1333d5d7cf4eSJonathan Chew 	}
1334d5d7cf4eSJonathan Chew 
1335d5d7cf4eSJonathan Chew 	/*
1336d5d7cf4eSJonathan Chew 	 * Probing errors can mess up the lgroup topology and
1337d5d7cf4eSJonathan Chew 	 * force us fall back to a 2 level lgroup topology.
1338d5d7cf4eSJonathan Chew 	 * Here we bound how tall the lgroup topology can grow
1339d5d7cf4eSJonathan Chew 	 * in hopes of avoiding any anamolies in probing from
1340d5d7cf4eSJonathan Chew 	 * messing up the lgroup topology by limiting the
1341d5d7cf4eSJonathan Chew 	 * accuracy of the latency topology.
1342d5d7cf4eSJonathan Chew 	 *
1343d5d7cf4eSJonathan Chew 	 * Assume that nodes will at least be configured in a
1344d5d7cf4eSJonathan Chew 	 * ring, so limit height of lgroup topology to be less
1345d5d7cf4eSJonathan Chew 	 * than number of nodes on a system with 4 or more
1346d5d7cf4eSJonathan Chew 	 * nodes
1347d5d7cf4eSJonathan Chew 	 */
1348d5d7cf4eSJonathan Chew 	if (lgrp_plat_node_cnt >= 4 && lgrp_topo_ht_limit() ==
1349d5d7cf4eSJonathan Chew 	    lgrp_topo_ht_limit_default())
1350d5d7cf4eSJonathan Chew 		(void) lgrp_topo_ht_limit_set(lgrp_plat_node_cnt - 1);
1351d5d7cf4eSJonathan Chew }
1352d5d7cf4eSJonathan Chew 
1353d5d7cf4eSJonathan Chew 
1354d5d7cf4eSJonathan Chew /*
13552e2c009bSjjc  * Latencies must be within 1/(2**LGRP_LAT_TOLERANCE_SHIFT) of each other to
13562e2c009bSjjc  * be considered same
13572e2c009bSjjc  */
13582e2c009bSjjc #define	LGRP_LAT_TOLERANCE_SHIFT	4
13592e2c009bSjjc 
13602e2c009bSjjc int	lgrp_plat_probe_lt_shift = LGRP_LAT_TOLERANCE_SHIFT;
13612e2c009bSjjc 
13622e2c009bSjjc 
13632e2c009bSjjc /*
13642e2c009bSjjc  * Adjust latencies between nodes to be symmetric, normalize latencies between
13652e2c009bSjjc  * any nodes that are within some tolerance to be same, and make local
13662e2c009bSjjc  * latencies be same
13672e2c009bSjjc  */
13682e2c009bSjjc static void
13692e2c009bSjjc lgrp_plat_latency_adjust(node_phys_addr_map_t *node_memory,
13702e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats, lgrp_plat_probe_stats_t *probe_stats)
13712e2c009bSjjc {
13722e2c009bSjjc 	int				i;
13732e2c009bSjjc 	int				j;
13742e2c009bSjjc 	int				k;
13752e2c009bSjjc 	int				l;
13762e2c009bSjjc 	u_longlong_t			max;
13772e2c009bSjjc 	u_longlong_t			min;
13782e2c009bSjjc 	u_longlong_t			t;
13792e2c009bSjjc 	u_longlong_t			t1;
13802e2c009bSjjc 	u_longlong_t			t2;
13812e2c009bSjjc 	const lgrp_config_flag_t	cflag = LGRP_CONFIG_LAT_CHANGE_ALL;
13822e2c009bSjjc 	int				lat_corrected[MAX_NODES][MAX_NODES];
13832e2c009bSjjc 
13842e2c009bSjjc 	/*
13852e2c009bSjjc 	 * Nothing to do when this is an UMA machine or don't have args needed
13862e2c009bSjjc 	 */
13872e2c009bSjjc 	if (max_mem_nodes == 1)
13882e2c009bSjjc 		return;
13892e2c009bSjjc 
13902e2c009bSjjc 	ASSERT(node_memory != NULL && lat_stats != NULL &&
13912e2c009bSjjc 	    probe_stats != NULL);
13922e2c009bSjjc 
13932e2c009bSjjc 	/*
13942e2c009bSjjc 	 * Make sure that latencies are symmetric between any two nodes
13952e2c009bSjjc 	 * (ie. latency(node0, node1) == latency(node1, node0))
13962e2c009bSjjc 	 */
13972e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
13982e2c009bSjjc 		if (!node_memory[i].exists)
13992e2c009bSjjc 			continue;
14002e2c009bSjjc 
14012e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
14022e2c009bSjjc 			if (!node_memory[j].exists)
14032e2c009bSjjc 				continue;
14042e2c009bSjjc 
14052e2c009bSjjc 			t1 = lat_stats->latencies[i][j];
14062e2c009bSjjc 			t2 = lat_stats->latencies[j][i];
14072e2c009bSjjc 
14082e2c009bSjjc 			if (t1 == 0 || t2 == 0 || t1 == t2)
14092e2c009bSjjc 				continue;
14102e2c009bSjjc 
14112e2c009bSjjc 			/*
14122e2c009bSjjc 			 * Latencies should be same
14132e2c009bSjjc 			 * - Use minimum of two latencies which should be same
14142e2c009bSjjc 			 * - Track suspect probe times not within tolerance of
14152e2c009bSjjc 			 *   min value
14162e2c009bSjjc 			 * - Remember how much values are corrected by
14172e2c009bSjjc 			 */
14182e2c009bSjjc 			if (t1 > t2) {
14192e2c009bSjjc 				t = t2;
14202e2c009bSjjc 				probe_stats->probe_errors[i][j] += t1 - t2;
14212e2c009bSjjc 				if (t1 - t2 > t2 >> lgrp_plat_probe_lt_shift) {
14222e2c009bSjjc 					probe_stats->probe_suspect[i][j]++;
14232e2c009bSjjc 					probe_stats->probe_suspect[j][i]++;
14242e2c009bSjjc 				}
14252e2c009bSjjc 			} else if (t2 > t1) {
14262e2c009bSjjc 				t = t1;
14272e2c009bSjjc 				probe_stats->probe_errors[j][i] += t2 - t1;
14282e2c009bSjjc 				if (t2 - t1 > t1 >> lgrp_plat_probe_lt_shift) {
14292e2c009bSjjc 					probe_stats->probe_suspect[i][j]++;
14302e2c009bSjjc 					probe_stats->probe_suspect[j][i]++;
14312e2c009bSjjc 				}
14322e2c009bSjjc 			}
14332e2c009bSjjc 
14342e2c009bSjjc 			lat_stats->latencies[i][j] =
14352e2c009bSjjc 			    lat_stats->latencies[j][i] = t;
14362e2c009bSjjc 			lgrp_config(cflag, t1, t);
14372e2c009bSjjc 			lgrp_config(cflag, t2, t);
14382e2c009bSjjc 		}
14392e2c009bSjjc 	}
14402e2c009bSjjc 
14412e2c009bSjjc 	/*
14422e2c009bSjjc 	 * Keep track of which latencies get corrected
14432e2c009bSjjc 	 */
14442e2c009bSjjc 	for (i = 0; i < MAX_NODES; i++)
14452e2c009bSjjc 		for (j = 0; j < MAX_NODES; j++)
14462e2c009bSjjc 			lat_corrected[i][j] = 0;
14472e2c009bSjjc 
14482e2c009bSjjc 	/*
14492e2c009bSjjc 	 * For every two nodes, see whether there is another pair of nodes which
14502e2c009bSjjc 	 * are about the same distance apart and make the latencies be the same
14512e2c009bSjjc 	 * if they are close enough together
14522e2c009bSjjc 	 */
14532e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
14542e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
14552e2c009bSjjc 			if (!node_memory[j].exists)
14562e2c009bSjjc 				continue;
14572e2c009bSjjc 			/*
14582e2c009bSjjc 			 * Pick one pair of nodes (i, j)
14592e2c009bSjjc 			 * and get latency between them
14602e2c009bSjjc 			 */
14612e2c009bSjjc 			t1 = lat_stats->latencies[i][j];
14622e2c009bSjjc 
14632e2c009bSjjc 			/*
14642e2c009bSjjc 			 * Skip this pair of nodes if there isn't a latency
14652e2c009bSjjc 			 * for it yet
14662e2c009bSjjc 			 */
14672e2c009bSjjc 			if (t1 == 0)
14682e2c009bSjjc 				continue;
14692e2c009bSjjc 
14702e2c009bSjjc 			for (k = 0; k < lgrp_plat_node_cnt; k++) {
14712e2c009bSjjc 				for (l = 0; l < lgrp_plat_node_cnt; l++) {
14722e2c009bSjjc 					if (!node_memory[l].exists)
14732e2c009bSjjc 						continue;
14742e2c009bSjjc 					/*
14752e2c009bSjjc 					 * Pick another pair of nodes (k, l)
14762e2c009bSjjc 					 * not same as (i, j) and get latency
14772e2c009bSjjc 					 * between them
14782e2c009bSjjc 					 */
14792e2c009bSjjc 					if (k == i && l == j)
14802e2c009bSjjc 						continue;
14812e2c009bSjjc 
14822e2c009bSjjc 					t2 = lat_stats->latencies[k][l];
14832e2c009bSjjc 
14842e2c009bSjjc 					/*
14852e2c009bSjjc 					 * Skip this pair of nodes if there
14862e2c009bSjjc 					 * isn't a latency for it yet
14872e2c009bSjjc 					 */
14882e2c009bSjjc 
14892e2c009bSjjc 					if (t2 == 0)
14902e2c009bSjjc 						continue;
14912e2c009bSjjc 
14922e2c009bSjjc 					/*
14932e2c009bSjjc 					 * Skip nodes (k, l) if they already
14942e2c009bSjjc 					 * have same latency as (i, j) or
14952e2c009bSjjc 					 * their latency isn't close enough to
14962e2c009bSjjc 					 * be considered/made the same
14972e2c009bSjjc 					 */
14982e2c009bSjjc 					if (t1 == t2 || (t1 > t2 && t1 - t2 >
14992e2c009bSjjc 					    t1 >> lgrp_plat_probe_lt_shift) ||
15002e2c009bSjjc 					    (t2 > t1 && t2 - t1 >
15012e2c009bSjjc 					    t2 >> lgrp_plat_probe_lt_shift))
15022e2c009bSjjc 						continue;
15032e2c009bSjjc 
15042e2c009bSjjc 					/*
15052e2c009bSjjc 					 * Make latency(i, j) same as
15062e2c009bSjjc 					 * latency(k, l), try to use latency
15072e2c009bSjjc 					 * that has been adjusted already to get
15082e2c009bSjjc 					 * more consistency (if possible), and
15092e2c009bSjjc 					 * remember which latencies were
15102e2c009bSjjc 					 * adjusted for next time
15112e2c009bSjjc 					 */
15122e2c009bSjjc 					if (lat_corrected[i][j]) {
15132e2c009bSjjc 						t = t1;
15142e2c009bSjjc 						lgrp_config(cflag, t2, t);
15152e2c009bSjjc 						t2 = t;
15162e2c009bSjjc 					} else if (lat_corrected[k][l]) {
15172e2c009bSjjc 						t = t2;
15182e2c009bSjjc 						lgrp_config(cflag, t1, t);
15192e2c009bSjjc 						t1 = t;
15202e2c009bSjjc 					} else {
15212e2c009bSjjc 						if (t1 > t2)
15222e2c009bSjjc 							t = t2;
15232e2c009bSjjc 						else
15242e2c009bSjjc 							t = t1;
15252e2c009bSjjc 						lgrp_config(cflag, t1, t);
15262e2c009bSjjc 						lgrp_config(cflag, t2, t);
15272e2c009bSjjc 						t1 = t2 = t;
15282e2c009bSjjc 					}
15292e2c009bSjjc 
15302e2c009bSjjc 					lat_stats->latencies[i][j] =
15312e2c009bSjjc 					    lat_stats->latencies[k][l] = t;
15322e2c009bSjjc 
15332e2c009bSjjc 					lat_corrected[i][j] =
15342e2c009bSjjc 					    lat_corrected[k][l] = 1;
15352e2c009bSjjc 				}
15362e2c009bSjjc 			}
15372e2c009bSjjc 		}
15382e2c009bSjjc 	}
15392e2c009bSjjc 
15402e2c009bSjjc 	/*
15412e2c009bSjjc 	 * Local latencies should be same
15422e2c009bSjjc 	 * - Find min and max local latencies
15432e2c009bSjjc 	 * - Make all local latencies be minimum
15442e2c009bSjjc 	 */
15452e2c009bSjjc 	min = -1;
15462e2c009bSjjc 	max = 0;
15472e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
15482e2c009bSjjc 		if (!node_memory[i].exists)
15492e2c009bSjjc 			continue;
15502e2c009bSjjc 		t = lat_stats->latencies[i][i];
15512e2c009bSjjc 		if (t == 0)
15522e2c009bSjjc 			continue;
15532e2c009bSjjc 		if (min == -1 || t < min)
15542e2c009bSjjc 			min = t;
15552e2c009bSjjc 		if (t > max)
15562e2c009bSjjc 			max = t;
15572e2c009bSjjc 	}
15582e2c009bSjjc 	if (min != max) {
15592e2c009bSjjc 		for (i = 0; i < lgrp_plat_node_cnt; i++) {
15602e2c009bSjjc 			int	local;
15612e2c009bSjjc 
15622e2c009bSjjc 			if (!node_memory[i].exists)
15632e2c009bSjjc 				continue;
15642e2c009bSjjc 
15652e2c009bSjjc 			local = lat_stats->latencies[i][i];
15662e2c009bSjjc 			if (local == 0)
15672e2c009bSjjc 				continue;
15682e2c009bSjjc 
15692e2c009bSjjc 			/*
15702e2c009bSjjc 			 * Track suspect probe times that aren't within
15712e2c009bSjjc 			 * tolerance of minimum local latency and how much
15722e2c009bSjjc 			 * probe times are corrected by
15732e2c009bSjjc 			 */
15742e2c009bSjjc 			if (local - min > min >> lgrp_plat_probe_lt_shift)
15752e2c009bSjjc 				probe_stats->probe_suspect[i][i]++;
15762e2c009bSjjc 
15772e2c009bSjjc 			probe_stats->probe_errors[i][i] += local - min;
15782e2c009bSjjc 
15792e2c009bSjjc 			/*
15802e2c009bSjjc 			 * Make local latencies be minimum
15812e2c009bSjjc 			 */
15822e2c009bSjjc 			lgrp_config(LGRP_CONFIG_LAT_CHANGE, i, min);
15832e2c009bSjjc 			lat_stats->latencies[i][i] = min;
15842e2c009bSjjc 		}
15852e2c009bSjjc 	}
15862e2c009bSjjc 
15872e2c009bSjjc 	/*
15882e2c009bSjjc 	 * Determine max probe time again since just adjusted latencies
15892e2c009bSjjc 	 */
15902e2c009bSjjc 	lat_stats->latency_max = 0;
15912e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
15922e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
15932e2c009bSjjc 			if (!node_memory[j].exists)
15942e2c009bSjjc 				continue;
15952e2c009bSjjc 			t = lat_stats->latencies[i][j];
15962e2c009bSjjc 			if (t > lat_stats->latency_max)
15972e2c009bSjjc 				lat_stats->latency_max = t;
15982e2c009bSjjc 		}
15992e2c009bSjjc 	}
16002e2c009bSjjc }
16012e2c009bSjjc 
16022e2c009bSjjc 
16032e2c009bSjjc /*
16042e2c009bSjjc  * Verify following about latencies between nodes:
16052e2c009bSjjc  *
16062e2c009bSjjc  * - Latencies should be symmetric (ie. latency(a, b) == latency(b, a))
16072e2c009bSjjc  * - Local latencies same
16082e2c009bSjjc  * - Local < remote
16092e2c009bSjjc  * - Number of latencies seen is reasonable
16102e2c009bSjjc  * - Number of occurrences of a given latency should be more than 1
16112e2c009bSjjc  *
16122e2c009bSjjc  * Returns:
16132e2c009bSjjc  *	0	Success
16142e2c009bSjjc  *	-1	Not symmetric
16152e2c009bSjjc  *	-2	Local latencies not same
16162e2c009bSjjc  *	-3	Local >= remote
16172e2c009bSjjc  */
16182e2c009bSjjc static int
16192e2c009bSjjc lgrp_plat_latency_verify(node_phys_addr_map_t *node_memory,
16202e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats)
16212e2c009bSjjc {
16222e2c009bSjjc 	int				i;
16232e2c009bSjjc 	int				j;
16242e2c009bSjjc 	u_longlong_t			t1;
16252e2c009bSjjc 	u_longlong_t			t2;
16262e2c009bSjjc 
16272e2c009bSjjc 	ASSERT(node_memory != NULL && lat_stats != NULL);
16282e2c009bSjjc 
16292e2c009bSjjc 	/*
16302e2c009bSjjc 	 * Nothing to do when this is an UMA machine, lgroup topology is
16312e2c009bSjjc 	 * limited to 2 levels, or there aren't any probe times yet
16322e2c009bSjjc 	 */
16332e2c009bSjjc 	if (max_mem_nodes == 1 || lgrp_topo_levels < 2 ||
16342e2c009bSjjc 	    lat_stats->latencies[0][0] == 0)
16352e2c009bSjjc 		return (0);
16362e2c009bSjjc 
16372e2c009bSjjc 	/*
16382e2c009bSjjc 	 * Make sure that latencies are symmetric between any two nodes
16392e2c009bSjjc 	 * (ie. latency(node0, node1) == latency(node1, node0))
16402e2c009bSjjc 	 */
16412e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
16422e2c009bSjjc 		if (!node_memory[i].exists)
16432e2c009bSjjc 			continue;
16442e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
16452e2c009bSjjc 			if (!node_memory[j].exists)
16462e2c009bSjjc 				continue;
16472e2c009bSjjc 			t1 = lat_stats->latencies[i][j];
16482e2c009bSjjc 			t2 = lat_stats->latencies[j][i];
16492e2c009bSjjc 
16502e2c009bSjjc 			if (t1 == 0 || t2 == 0 || t1 == t2)
16512e2c009bSjjc 				continue;
16522e2c009bSjjc 
16532e2c009bSjjc 			return (-1);
16542e2c009bSjjc 		}
16552e2c009bSjjc 	}
16562e2c009bSjjc 
16572e2c009bSjjc 	/*
16582e2c009bSjjc 	 * Local latencies should be same
16592e2c009bSjjc 	 */
16602e2c009bSjjc 	t1 = lat_stats->latencies[0][0];
16612e2c009bSjjc 	for (i = 1; i < lgrp_plat_node_cnt; i++) {
16622e2c009bSjjc 		if (!node_memory[i].exists)
16632e2c009bSjjc 			continue;
16642e2c009bSjjc 
16652e2c009bSjjc 		t2 = lat_stats->latencies[i][i];
16662e2c009bSjjc 		if (t2 == 0)
16672e2c009bSjjc 			continue;
16682e2c009bSjjc 
16692e2c009bSjjc 		if (t1 == 0) {
16702e2c009bSjjc 			t1 = t2;
16712e2c009bSjjc 			continue;
16722e2c009bSjjc 		}
16732e2c009bSjjc 
16742e2c009bSjjc 		if (t1 != t2)
16752e2c009bSjjc 			return (-2);
16762e2c009bSjjc 	}
16772e2c009bSjjc 
16782e2c009bSjjc 	/*
16792e2c009bSjjc 	 * Local latencies should be less than remote
16802e2c009bSjjc 	 */
16812e2c009bSjjc 	if (t1) {
16822e2c009bSjjc 		for (i = 0; i < lgrp_plat_node_cnt; i++) {
16832e2c009bSjjc 			for (j = 0; j < lgrp_plat_node_cnt; j++) {
16842e2c009bSjjc 				if (!node_memory[j].exists)
16852e2c009bSjjc 					continue;
16862e2c009bSjjc 				t2 = lat_stats->latencies[i][j];
16872e2c009bSjjc 				if (i == j || t2 == 0)
16882e2c009bSjjc 					continue;
16892e2c009bSjjc 
16902e2c009bSjjc 				if (t1 >= t2)
16912e2c009bSjjc 					return (-3);
16922e2c009bSjjc 			}
16932e2c009bSjjc 		}
16942e2c009bSjjc 	}
16952e2c009bSjjc 
16962e2c009bSjjc 	return (0);
16972e2c009bSjjc }
16982e2c009bSjjc 
16992e2c009bSjjc 
17002e2c009bSjjc /*
1701d5d7cf4eSJonathan Chew  * Platform-specific initialization
1702d5d7cf4eSJonathan Chew  */
1703d5d7cf4eSJonathan Chew static void
1704d5d7cf4eSJonathan Chew lgrp_plat_main_init(void)
1705d5d7cf4eSJonathan Chew {
1706d5d7cf4eSJonathan Chew 	int	curnode;
1707d5d7cf4eSJonathan Chew 	int	ht_limit;
1708d5d7cf4eSJonathan Chew 	int	i;
1709d5d7cf4eSJonathan Chew 
1710d5d7cf4eSJonathan Chew 	/*
1711d5d7cf4eSJonathan Chew 	 * Print a notice that MPO is disabled when memory is interleaved
1712d5d7cf4eSJonathan Chew 	 * across nodes....Would do this when it is discovered, but can't
1713d5d7cf4eSJonathan Chew 	 * because it happens way too early during boot....
1714d5d7cf4eSJonathan Chew 	 */
1715d5d7cf4eSJonathan Chew 	if (lgrp_plat_mem_intrlv)
1716d5d7cf4eSJonathan Chew 		cmn_err(CE_NOTE,
1717d5d7cf4eSJonathan Chew 		    "MPO disabled because memory is interleaved\n");
1718d5d7cf4eSJonathan Chew 
1719d5d7cf4eSJonathan Chew 	/*
1720d5d7cf4eSJonathan Chew 	 * Don't bother to do any probing if it is disabled, there is only one
1721d5d7cf4eSJonathan Chew 	 * node, or the height of the lgroup topology less than or equal to 2
1722d5d7cf4eSJonathan Chew 	 */
1723d5d7cf4eSJonathan Chew 	ht_limit = lgrp_topo_ht_limit();
1724d5d7cf4eSJonathan Chew 	if (!(lgrp_plat_probe_flags & LGRP_PLAT_PROBE_ENABLE) ||
1725d5d7cf4eSJonathan Chew 	    max_mem_nodes == 1 || ht_limit <= 2) {
1726d5d7cf4eSJonathan Chew 		/*
1727d5d7cf4eSJonathan Chew 		 * Setup lgroup latencies for 2 level lgroup topology
1728d5d7cf4eSJonathan Chew 		 * (ie. local and remote only) if they haven't been set yet
1729d5d7cf4eSJonathan Chew 		 */
1730d5d7cf4eSJonathan Chew 		if (ht_limit == 2 && lgrp_plat_lat_stats.latency_min == -1 &&
1731d5d7cf4eSJonathan Chew 		    lgrp_plat_lat_stats.latency_max == 0)
1732d5d7cf4eSJonathan Chew 			lgrp_plat_2level_setup(lgrp_plat_node_memory,
1733d5d7cf4eSJonathan Chew 			    &lgrp_plat_lat_stats);
1734d5d7cf4eSJonathan Chew 		return;
1735d5d7cf4eSJonathan Chew 	}
1736d5d7cf4eSJonathan Chew 
1737d5d7cf4eSJonathan Chew 	if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_VENDOR) {
1738d5d7cf4eSJonathan Chew 		/*
1739d5d7cf4eSJonathan Chew 		 * Should have been able to probe from CPU 0 when it was added
1740d5d7cf4eSJonathan Chew 		 * to lgroup hierarchy, but may not have been able to then
1741d5d7cf4eSJonathan Chew 		 * because it happens so early in boot that gethrtime() hasn't
1742d5d7cf4eSJonathan Chew 		 * been initialized.  (:-(
1743d5d7cf4eSJonathan Chew 		 */
1744d5d7cf4eSJonathan Chew 		curnode = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node,
1745d5d7cf4eSJonathan Chew 		    lgrp_plat_cpu_node_nentries);
1746d5d7cf4eSJonathan Chew 		ASSERT(curnode >= 0 && curnode < lgrp_plat_node_cnt);
1747d5d7cf4eSJonathan Chew 		if (lgrp_plat_lat_stats.latencies[curnode][curnode] == 0)
1748d5d7cf4eSJonathan Chew 			lgrp_plat_probe();
1749d5d7cf4eSJonathan Chew 
1750d5d7cf4eSJonathan Chew 		return;
1751d5d7cf4eSJonathan Chew 	}
1752d5d7cf4eSJonathan Chew 
1753d5d7cf4eSJonathan Chew 	/*
1754d5d7cf4eSJonathan Chew 	 * When probing memory, use one page for every sample to determine
1755d5d7cf4eSJonathan Chew 	 * lgroup topology and taking multiple samples
1756d5d7cf4eSJonathan Chew 	 */
1757d5d7cf4eSJonathan Chew 	if (lgrp_plat_probe_mem_config.probe_memsize == 0)
1758d5d7cf4eSJonathan Chew 		lgrp_plat_probe_mem_config.probe_memsize = PAGESIZE *
1759d5d7cf4eSJonathan Chew 		    lgrp_plat_probe_nsamples;
1760d5d7cf4eSJonathan Chew 
1761d5d7cf4eSJonathan Chew 	/*
1762d5d7cf4eSJonathan Chew 	 * Map memory in each node needed for probing to determine latency
1763d5d7cf4eSJonathan Chew 	 * topology
1764d5d7cf4eSJonathan Chew 	 */
1765d5d7cf4eSJonathan Chew 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
1766d5d7cf4eSJonathan Chew 		int	mnode;
1767d5d7cf4eSJonathan Chew 
1768d5d7cf4eSJonathan Chew 		/*
1769d5d7cf4eSJonathan Chew 		 * Skip this node and leave its probe page NULL
1770d5d7cf4eSJonathan Chew 		 * if it doesn't have any memory
1771d5d7cf4eSJonathan Chew 		 */
1772d5d7cf4eSJonathan Chew 		mnode = plat_lgrphand_to_mem_node((lgrp_handle_t)i);
1773d5d7cf4eSJonathan Chew 		if (!mem_node_config[mnode].exists) {
1774d5d7cf4eSJonathan Chew 			lgrp_plat_probe_mem_config.probe_va[i] = NULL;
1775d5d7cf4eSJonathan Chew 			continue;
1776d5d7cf4eSJonathan Chew 		}
1777d5d7cf4eSJonathan Chew 
1778d5d7cf4eSJonathan Chew 		/*
1779d5d7cf4eSJonathan Chew 		 * Allocate one kernel virtual page
1780d5d7cf4eSJonathan Chew 		 */
1781d5d7cf4eSJonathan Chew 		lgrp_plat_probe_mem_config.probe_va[i] = vmem_alloc(heap_arena,
1782d5d7cf4eSJonathan Chew 		    lgrp_plat_probe_mem_config.probe_memsize, VM_NOSLEEP);
1783d5d7cf4eSJonathan Chew 		if (lgrp_plat_probe_mem_config.probe_va[i] == NULL) {
1784d5d7cf4eSJonathan Chew 			cmn_err(CE_WARN,
1785d5d7cf4eSJonathan Chew 			    "lgrp_plat_main_init: couldn't allocate memory");
1786d5d7cf4eSJonathan Chew 			return;
1787d5d7cf4eSJonathan Chew 		}
1788d5d7cf4eSJonathan Chew 
1789d5d7cf4eSJonathan Chew 		/*
1790d5d7cf4eSJonathan Chew 		 * Get PFN for first page in each node
1791d5d7cf4eSJonathan Chew 		 */
1792d5d7cf4eSJonathan Chew 		lgrp_plat_probe_mem_config.probe_pfn[i] =
1793d5d7cf4eSJonathan Chew 		    mem_node_config[mnode].physbase;
1794d5d7cf4eSJonathan Chew 
1795d5d7cf4eSJonathan Chew 		/*
1796d5d7cf4eSJonathan Chew 		 * Map virtual page to first page in node
1797d5d7cf4eSJonathan Chew 		 */
1798d5d7cf4eSJonathan Chew 		hat_devload(kas.a_hat, lgrp_plat_probe_mem_config.probe_va[i],
1799d5d7cf4eSJonathan Chew 		    lgrp_plat_probe_mem_config.probe_memsize,
1800d5d7cf4eSJonathan Chew 		    lgrp_plat_probe_mem_config.probe_pfn[i],
1801d5d7cf4eSJonathan Chew 		    PROT_READ | PROT_WRITE | HAT_PLAT_NOCACHE,
1802d5d7cf4eSJonathan Chew 		    HAT_LOAD_NOCONSIST);
1803d5d7cf4eSJonathan Chew 	}
1804d5d7cf4eSJonathan Chew 
1805d5d7cf4eSJonathan Chew 	/*
1806d5d7cf4eSJonathan Chew 	 * Probe from current CPU
1807d5d7cf4eSJonathan Chew 	 */
1808d5d7cf4eSJonathan Chew 	lgrp_plat_probe();
1809d5d7cf4eSJonathan Chew }
1810d5d7cf4eSJonathan Chew 
1811d5d7cf4eSJonathan Chew 
1812d5d7cf4eSJonathan Chew /*
18132e2c009bSjjc  * Return the number of free, allocatable, or installed
18142e2c009bSjjc  * pages in an lgroup
18152e2c009bSjjc  * This is a copy of the MAX_MEM_NODES == 1 version of the routine
18162e2c009bSjjc  * used when MPO is disabled (i.e. single lgroup) or this is the root lgroup
18172e2c009bSjjc  */
18182e2c009bSjjc /* ARGSUSED */
18192e2c009bSjjc static pgcnt_t
18202e2c009bSjjc lgrp_plat_mem_size_default(lgrp_handle_t lgrphand, lgrp_mem_query_t query)
18212e2c009bSjjc {
18222e2c009bSjjc 	struct memlist *mlist;
18232e2c009bSjjc 	pgcnt_t npgs = 0;
18242e2c009bSjjc 	extern struct memlist *phys_avail;
18252e2c009bSjjc 	extern struct memlist *phys_install;
18262e2c009bSjjc 
18272e2c009bSjjc 	switch (query) {
18282e2c009bSjjc 	case LGRP_MEM_SIZE_FREE:
18292e2c009bSjjc 		return ((pgcnt_t)freemem);
18302e2c009bSjjc 	case LGRP_MEM_SIZE_AVAIL:
18312e2c009bSjjc 		memlist_read_lock();
1832*56f33205SJonathan Adams 		for (mlist = phys_avail; mlist; mlist = mlist->ml_next)
1833*56f33205SJonathan Adams 			npgs += btop(mlist->ml_size);
18342e2c009bSjjc 		memlist_read_unlock();
18352e2c009bSjjc 		return (npgs);
18362e2c009bSjjc 	case LGRP_MEM_SIZE_INSTALL:
18372e2c009bSjjc 		memlist_read_lock();
1838*56f33205SJonathan Adams 		for (mlist = phys_install; mlist; mlist = mlist->ml_next)
1839*56f33205SJonathan Adams 			npgs += btop(mlist->ml_size);
18402e2c009bSjjc 		memlist_read_unlock();
18412e2c009bSjjc 		return (npgs);
18422e2c009bSjjc 	default:
18432e2c009bSjjc 		return ((pgcnt_t)0);
18442e2c009bSjjc 	}
18452e2c009bSjjc }
18462e2c009bSjjc 
18472e2c009bSjjc 
18482e2c009bSjjc /*
18492e2c009bSjjc  * Update node to proximity domain mappings for given domain and return node ID
18502e2c009bSjjc  */
18512e2c009bSjjc static int
1852d821f0f0Sjjc lgrp_plat_node_domain_update(node_domain_map_t *node_domain, int node_cnt,
1853d821f0f0Sjjc     uint32_t domain)
18542e2c009bSjjc {
18552e2c009bSjjc 	uint_t	node;
18562e2c009bSjjc 	uint_t	start;
18572e2c009bSjjc 
18582e2c009bSjjc 	/*
18592e2c009bSjjc 	 * Hash proximity domain ID into node to domain mapping table (array)
18602e2c009bSjjc 	 * and add entry for it into first non-existent or matching entry found
18612e2c009bSjjc 	 */
1862d821f0f0Sjjc 	node = start = NODE_DOMAIN_HASH(domain, node_cnt);
18632e2c009bSjjc 	do {
18642e2c009bSjjc 		/*
18652e2c009bSjjc 		 * Entry doesn't exist yet, so create one for this proximity
18662e2c009bSjjc 		 * domain and return node ID which is index into mapping table.
18672e2c009bSjjc 		 */
18682e2c009bSjjc 		if (!node_domain[node].exists) {
18692e2c009bSjjc 			node_domain[node].exists = 1;
18702e2c009bSjjc 			node_domain[node].prox_domain = domain;
18712e2c009bSjjc 			return (node);
18722e2c009bSjjc 		}
18732e2c009bSjjc 
18742e2c009bSjjc 		/*
18752e2c009bSjjc 		 * Entry exists for this proximity domain already, so just
18762e2c009bSjjc 		 * return node ID (index into table).
18772e2c009bSjjc 		 */
18782e2c009bSjjc 		if (node_domain[node].prox_domain == domain)
18792e2c009bSjjc 			return (node);
1880d821f0f0Sjjc 		node = NODE_DOMAIN_HASH(node + 1, node_cnt);
18812e2c009bSjjc 	} while (node != start);
18822e2c009bSjjc 
18832e2c009bSjjc 	/*
18842e2c009bSjjc 	 * Ran out of supported number of entries which shouldn't happen....
18852e2c009bSjjc 	 */
18862e2c009bSjjc 	ASSERT(node != start);
18872e2c009bSjjc 	return (-1);
18882e2c009bSjjc }
18892e2c009bSjjc 
18902e2c009bSjjc 
18912e2c009bSjjc /*
18922e2c009bSjjc  * Update node memory information for given proximity domain with specified
18932e2c009bSjjc  * starting and ending physical address range (and return positive numbers for
18942e2c009bSjjc  * success and negative ones for errors)
18952e2c009bSjjc  */
18962e2c009bSjjc static int
1897d821f0f0Sjjc lgrp_plat_node_memory_update(node_domain_map_t *node_domain, int node_cnt,
1898e9dd3ea3Sjjc     node_phys_addr_map_t *node_memory, uint64_t start, uint64_t end,
18992e2c009bSjjc     uint32_t domain)
19002e2c009bSjjc {
19012e2c009bSjjc 	int	node;
19022e2c009bSjjc 
19032e2c009bSjjc 	/*
19042e2c009bSjjc 	 * Get node number for proximity domain
19052e2c009bSjjc 	 */
1906d821f0f0Sjjc 	node = lgrp_plat_domain_to_node(node_domain, node_cnt, domain);
19072e2c009bSjjc 	if (node == -1) {
1908d821f0f0Sjjc 		node = lgrp_plat_node_domain_update(node_domain, node_cnt,
1909d821f0f0Sjjc 		    domain);
19102e2c009bSjjc 		if (node == -1)
19112e2c009bSjjc 			return (-1);
19122e2c009bSjjc 	}
19132e2c009bSjjc 
19142e2c009bSjjc 	/*
19152e2c009bSjjc 	 * Create entry in table for node if it doesn't exist
19162e2c009bSjjc 	 */
19172e2c009bSjjc 	if (!node_memory[node].exists) {
19182e2c009bSjjc 		node_memory[node].exists = 1;
19192e2c009bSjjc 		node_memory[node].start = btop(start);
19202e2c009bSjjc 		node_memory[node].end = btop(end);
19212e2c009bSjjc 		node_memory[node].prox_domain = domain;
19222e2c009bSjjc 		return (0);
19232e2c009bSjjc 	}
19242e2c009bSjjc 
19252e2c009bSjjc 	/*
19262e2c009bSjjc 	 * Entry already exists for this proximity domain
19272e2c009bSjjc 	 *
19282e2c009bSjjc 	 * There may be more than one SRAT memory entry for a domain, so we may
19292e2c009bSjjc 	 * need to update existing start or end address for the node.
19302e2c009bSjjc 	 */
19312e2c009bSjjc 	if (node_memory[node].prox_domain == domain) {
19322e2c009bSjjc 		if (btop(start) < node_memory[node].start)
19332e2c009bSjjc 			node_memory[node].start = btop(start);
19342e2c009bSjjc 		if (btop(end) > node_memory[node].end)
19352e2c009bSjjc 			node_memory[node].end = btop(end);
19362e2c009bSjjc 		return (1);
19372e2c009bSjjc 	}
19382e2c009bSjjc 	return (-2);
19392e2c009bSjjc }
19402e2c009bSjjc 
19412e2c009bSjjc 
19422e2c009bSjjc /*
194381d9ccb6SJonathan Chew  * Have to sort node by starting physical address because VM system (physical
194481d9ccb6SJonathan Chew  * page free list management) assumes and expects memnodes to be sorted in
194581d9ccb6SJonathan Chew  * ascending order by physical address.  If not, the kernel will panic in
194681d9ccb6SJonathan Chew  * potentially a number of different places.  (:-(
194781d9ccb6SJonathan Chew  * NOTE: This workaround will not be sufficient if/when hotplugging memory is
194881d9ccb6SJonathan Chew  *	 supported on x86/x64.
194981d9ccb6SJonathan Chew  */
195081d9ccb6SJonathan Chew static void
195181d9ccb6SJonathan Chew lgrp_plat_node_sort(node_domain_map_t *node_domain, int node_cnt,
195281d9ccb6SJonathan Chew     cpu_node_map_t *cpu_node, int cpu_count, node_phys_addr_map_t *node_memory)
195381d9ccb6SJonathan Chew {
195481d9ccb6SJonathan Chew 	boolean_t	found;
195581d9ccb6SJonathan Chew 	int		i;
195681d9ccb6SJonathan Chew 	int		j;
195781d9ccb6SJonathan Chew 	int		n;
195881d9ccb6SJonathan Chew 	boolean_t	sorted;
195981d9ccb6SJonathan Chew 	boolean_t	swapped;
196081d9ccb6SJonathan Chew 
196181d9ccb6SJonathan Chew 	if (!lgrp_plat_node_sort_enable || node_cnt <= 1 ||
196281d9ccb6SJonathan Chew 	    node_domain == NULL || node_memory == NULL)
196381d9ccb6SJonathan Chew 		return;
196481d9ccb6SJonathan Chew 
196581d9ccb6SJonathan Chew 	/*
196681d9ccb6SJonathan Chew 	 * Sorted already?
196781d9ccb6SJonathan Chew 	 */
196881d9ccb6SJonathan Chew 	sorted = B_TRUE;
196981d9ccb6SJonathan Chew 	for (i = 0; i < node_cnt - 1; i++) {
197081d9ccb6SJonathan Chew 		/*
197181d9ccb6SJonathan Chew 		 * Skip entries that don't exist
197281d9ccb6SJonathan Chew 		 */
197381d9ccb6SJonathan Chew 		if (!node_memory[i].exists)
197481d9ccb6SJonathan Chew 			continue;
197581d9ccb6SJonathan Chew 
197681d9ccb6SJonathan Chew 		/*
197781d9ccb6SJonathan Chew 		 * Try to find next existing entry to compare against
197881d9ccb6SJonathan Chew 		 */
197981d9ccb6SJonathan Chew 		found = B_FALSE;
198081d9ccb6SJonathan Chew 		for (j = i + 1; j < node_cnt; j++) {
198181d9ccb6SJonathan Chew 			if (node_memory[j].exists) {
198281d9ccb6SJonathan Chew 				found = B_TRUE;
198381d9ccb6SJonathan Chew 				break;
198481d9ccb6SJonathan Chew 			}
198581d9ccb6SJonathan Chew 		}
198681d9ccb6SJonathan Chew 
198781d9ccb6SJonathan Chew 		/*
198881d9ccb6SJonathan Chew 		 * Done if no more existing entries to compare against
198981d9ccb6SJonathan Chew 		 */
199081d9ccb6SJonathan Chew 		if (found == B_FALSE)
199181d9ccb6SJonathan Chew 			break;
199281d9ccb6SJonathan Chew 
199381d9ccb6SJonathan Chew 		/*
199481d9ccb6SJonathan Chew 		 * Not sorted if starting address of current entry is bigger
199581d9ccb6SJonathan Chew 		 * than starting address of next existing entry
199681d9ccb6SJonathan Chew 		 */
199781d9ccb6SJonathan Chew 		if (node_memory[i].start > node_memory[j].start) {
199881d9ccb6SJonathan Chew 			sorted = B_FALSE;
199981d9ccb6SJonathan Chew 			break;
200081d9ccb6SJonathan Chew 		}
200181d9ccb6SJonathan Chew 	}
200281d9ccb6SJonathan Chew 
200381d9ccb6SJonathan Chew 	/*
200481d9ccb6SJonathan Chew 	 * Don't need to sort if sorted already
200581d9ccb6SJonathan Chew 	 */
200681d9ccb6SJonathan Chew 	if (sorted == B_TRUE)
200781d9ccb6SJonathan Chew 		return;
200881d9ccb6SJonathan Chew 
200981d9ccb6SJonathan Chew 	/*
201081d9ccb6SJonathan Chew 	 * Just use bubble sort since number of nodes is small
201181d9ccb6SJonathan Chew 	 */
201281d9ccb6SJonathan Chew 	n = node_cnt;
201381d9ccb6SJonathan Chew 	do {
201481d9ccb6SJonathan Chew 		swapped = B_FALSE;
201581d9ccb6SJonathan Chew 		n--;
201681d9ccb6SJonathan Chew 		for (i = 0; i < n; i++) {
201781d9ccb6SJonathan Chew 			/*
201881d9ccb6SJonathan Chew 			 * Skip entries that don't exist
201981d9ccb6SJonathan Chew 			 */
202081d9ccb6SJonathan Chew 			if (!node_memory[i].exists)
202181d9ccb6SJonathan Chew 				continue;
202281d9ccb6SJonathan Chew 
202381d9ccb6SJonathan Chew 			/*
202481d9ccb6SJonathan Chew 			 * Try to find next existing entry to compare against
202581d9ccb6SJonathan Chew 			 */
202681d9ccb6SJonathan Chew 			found = B_FALSE;
202781d9ccb6SJonathan Chew 			for (j = i + 1; j <= n; j++) {
202881d9ccb6SJonathan Chew 				if (node_memory[j].exists) {
202981d9ccb6SJonathan Chew 					found = B_TRUE;
203081d9ccb6SJonathan Chew 					break;
203181d9ccb6SJonathan Chew 				}
203281d9ccb6SJonathan Chew 			}
203381d9ccb6SJonathan Chew 
203481d9ccb6SJonathan Chew 			/*
203581d9ccb6SJonathan Chew 			 * Done if no more existing entries to compare against
203681d9ccb6SJonathan Chew 			 */
203781d9ccb6SJonathan Chew 			if (found == B_FALSE)
203881d9ccb6SJonathan Chew 				break;
203981d9ccb6SJonathan Chew 
204081d9ccb6SJonathan Chew 			if (node_memory[i].start > node_memory[j].start) {
204181d9ccb6SJonathan Chew 				node_phys_addr_map_t	save_addr;
204281d9ccb6SJonathan Chew 				node_domain_map_t	save_node;
204381d9ccb6SJonathan Chew 
204481d9ccb6SJonathan Chew 				/*
204581d9ccb6SJonathan Chew 				 * Swap node to proxmity domain ID assignments
204681d9ccb6SJonathan Chew 				 */
204781d9ccb6SJonathan Chew 				bcopy(&node_domain[i], &save_node,
204881d9ccb6SJonathan Chew 				    sizeof (node_domain_map_t));
204981d9ccb6SJonathan Chew 				bcopy(&node_domain[j], &node_domain[i],
205081d9ccb6SJonathan Chew 				    sizeof (node_domain_map_t));
205181d9ccb6SJonathan Chew 				bcopy(&save_node, &node_domain[j],
205281d9ccb6SJonathan Chew 				    sizeof (node_domain_map_t));
205381d9ccb6SJonathan Chew 
205481d9ccb6SJonathan Chew 				/*
205581d9ccb6SJonathan Chew 				 * Swap node to physical memory assignments
205681d9ccb6SJonathan Chew 				 */
205781d9ccb6SJonathan Chew 				bcopy(&node_memory[i], &save_addr,
205881d9ccb6SJonathan Chew 				    sizeof (node_phys_addr_map_t));
205981d9ccb6SJonathan Chew 				bcopy(&node_memory[j], &node_memory[i],
206081d9ccb6SJonathan Chew 				    sizeof (node_phys_addr_map_t));
206181d9ccb6SJonathan Chew 				bcopy(&save_addr, &node_memory[j],
206281d9ccb6SJonathan Chew 				    sizeof (node_phys_addr_map_t));
206381d9ccb6SJonathan Chew 				swapped = B_TRUE;
206481d9ccb6SJonathan Chew 			}
206581d9ccb6SJonathan Chew 		}
206681d9ccb6SJonathan Chew 	} while (swapped == B_TRUE);
206781d9ccb6SJonathan Chew 
206881d9ccb6SJonathan Chew 	/*
206981d9ccb6SJonathan Chew 	 * Check to make sure that CPUs assigned to correct node IDs now since
207081d9ccb6SJonathan Chew 	 * node to proximity domain ID assignments may have been changed above
207181d9ccb6SJonathan Chew 	 */
207281d9ccb6SJonathan Chew 	if (n == node_cnt - 1 || cpu_node == NULL || cpu_count < 1)
207381d9ccb6SJonathan Chew 		return;
207481d9ccb6SJonathan Chew 	for (i = 0; i < cpu_count; i++) {
207581d9ccb6SJonathan Chew 		int		node;
207681d9ccb6SJonathan Chew 
207781d9ccb6SJonathan Chew 		node = lgrp_plat_domain_to_node(node_domain, node_cnt,
207881d9ccb6SJonathan Chew 		    cpu_node[i].prox_domain);
207981d9ccb6SJonathan Chew 		if (cpu_node[i].node != node)
208081d9ccb6SJonathan Chew 			cpu_node[i].node = node;
208181d9ccb6SJonathan Chew 	}
208281d9ccb6SJonathan Chew 
208381d9ccb6SJonathan Chew }
208481d9ccb6SJonathan Chew 
208581d9ccb6SJonathan Chew 
208681d9ccb6SJonathan Chew /*
20872e2c009bSjjc  * Return time needed to probe from current CPU to memory in given node
20882e2c009bSjjc  */
20892e2c009bSjjc static hrtime_t
2090d5d7cf4eSJonathan Chew lgrp_plat_probe_time(int to, cpu_node_map_t *cpu_node, int cpu_node_nentries,
20912e2c009bSjjc     lgrp_plat_probe_mem_config_t *probe_mem_config,
20922e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats, lgrp_plat_probe_stats_t *probe_stats)
20932e2c009bSjjc {
20942e2c009bSjjc 	caddr_t			buf;
20952e2c009bSjjc 	hrtime_t		elapsed;
20962e2c009bSjjc 	hrtime_t		end;
20972e2c009bSjjc 	int			from;
20982e2c009bSjjc 	int			i;
20992e2c009bSjjc 	int			ipl;
21002e2c009bSjjc 	hrtime_t		max;
21012e2c009bSjjc 	hrtime_t		min;
21022e2c009bSjjc 	hrtime_t		start;
21032e2c009bSjjc 	extern int		use_sse_pagecopy;
21042e2c009bSjjc 
21052e2c009bSjjc 	/*
21062e2c009bSjjc 	 * Determine ID of node containing current CPU
21072e2c009bSjjc 	 */
2108d5d7cf4eSJonathan Chew 	from = lgrp_plat_cpu_to_node(CPU, cpu_node, cpu_node_nentries);
21092e2c009bSjjc 	ASSERT(from >= 0 && from < lgrp_plat_node_cnt);
21102e2c009bSjjc 
21112e2c009bSjjc 	/*
21122e2c009bSjjc 	 * Do common work for probing main memory
21132e2c009bSjjc 	 */
21142e2c009bSjjc 	if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_PGCPY) {
21152e2c009bSjjc 		/*
21162e2c009bSjjc 		 * Skip probing any nodes without memory and
21172e2c009bSjjc 		 * set probe time to 0
21182e2c009bSjjc 		 */
21192e2c009bSjjc 		if (probe_mem_config->probe_va[to] == NULL) {
21202e2c009bSjjc 			lat_stats->latencies[from][to] = 0;
21212e2c009bSjjc 			return (0);
21222e2c009bSjjc 		}
21232e2c009bSjjc 
21242e2c009bSjjc 		/*
21252e2c009bSjjc 		 * Invalidate caches once instead of once every sample
21262e2c009bSjjc 		 * which should cut cost of probing by a lot
21272e2c009bSjjc 		 */
21282e2c009bSjjc 		probe_stats->flush_cost = gethrtime();
21292e2c009bSjjc 		invalidate_cache();
21302e2c009bSjjc 		probe_stats->flush_cost = gethrtime() -
21312e2c009bSjjc 		    probe_stats->flush_cost;
21322e2c009bSjjc 		probe_stats->probe_cost_total += probe_stats->flush_cost;
21332e2c009bSjjc 	}
21342e2c009bSjjc 
21352e2c009bSjjc 	/*
21362e2c009bSjjc 	 * Probe from current CPU to given memory using specified operation
21372e2c009bSjjc 	 * and take specified number of samples
21382e2c009bSjjc 	 */
21392e2c009bSjjc 	max = 0;
21402e2c009bSjjc 	min = -1;
21412e2c009bSjjc 	for (i = 0; i < lgrp_plat_probe_nsamples; i++) {
21422e2c009bSjjc 		probe_stats->probe_cost = gethrtime();
21432e2c009bSjjc 
21442e2c009bSjjc 		/*
21452e2c009bSjjc 		 * Can't measure probe time if gethrtime() isn't working yet
21462e2c009bSjjc 		 */
21472e2c009bSjjc 		if (probe_stats->probe_cost == 0 && gethrtime() == 0)
21482e2c009bSjjc 			return (0);
21492e2c009bSjjc 
21502e2c009bSjjc 		if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_VENDOR) {
21512e2c009bSjjc 			/*
21522e2c009bSjjc 			 * Measure how long it takes to read vendor ID from
21532e2c009bSjjc 			 * Northbridge
21542e2c009bSjjc 			 */
21552e2c009bSjjc 			elapsed = opt_probe_vendor(to, lgrp_plat_probe_nreads);
21562e2c009bSjjc 		} else {
21572e2c009bSjjc 			/*
21582e2c009bSjjc 			 * Measure how long it takes to copy page
21592e2c009bSjjc 			 * on top of itself
21602e2c009bSjjc 			 */
21612e2c009bSjjc 			buf = probe_mem_config->probe_va[to] + (i * PAGESIZE);
21622e2c009bSjjc 
21632e2c009bSjjc 			kpreempt_disable();
21642e2c009bSjjc 			ipl = splhigh();
21652e2c009bSjjc 			start = gethrtime();
21662e2c009bSjjc 			if (use_sse_pagecopy)
21672e2c009bSjjc 				hwblkpagecopy(buf, buf);
21682e2c009bSjjc 			else
21692e2c009bSjjc 				bcopy(buf, buf, PAGESIZE);
21702e2c009bSjjc 			end = gethrtime();
21712e2c009bSjjc 			elapsed = end - start;
21722e2c009bSjjc 			splx(ipl);
21732e2c009bSjjc 			kpreempt_enable();
21742e2c009bSjjc 		}
21752e2c009bSjjc 
21762e2c009bSjjc 		probe_stats->probe_cost = gethrtime() -
21772e2c009bSjjc 		    probe_stats->probe_cost;
21782e2c009bSjjc 		probe_stats->probe_cost_total += probe_stats->probe_cost;
21792e2c009bSjjc 
21802e2c009bSjjc 		if (min == -1 || elapsed < min)
21812e2c009bSjjc 			min = elapsed;
21822e2c009bSjjc 		if (elapsed > max)
21832e2c009bSjjc 			max = elapsed;
21842e2c009bSjjc 	}
21852e2c009bSjjc 
21862e2c009bSjjc 	/*
21872e2c009bSjjc 	 * Update minimum and maximum probe times between
21882e2c009bSjjc 	 * these two nodes
21892e2c009bSjjc 	 */
21902e2c009bSjjc 	if (min < probe_stats->probe_min[from][to] ||
21912e2c009bSjjc 	    probe_stats->probe_min[from][to] == 0)
21922e2c009bSjjc 		probe_stats->probe_min[from][to] = min;
21932e2c009bSjjc 
21942e2c009bSjjc 	if (max > probe_stats->probe_max[from][to])
21952e2c009bSjjc 		probe_stats->probe_max[from][to] = max;
21962e2c009bSjjc 
21972e2c009bSjjc 	return (min);
21982e2c009bSjjc }
21992e2c009bSjjc 
22002e2c009bSjjc 
22012e2c009bSjjc /*
2202d821f0f0Sjjc  * Read boot property with CPU to APIC ID array, fill in CPU to node ID
2203d5d7cf4eSJonathan Chew  * mapping table with APIC ID for each CPU (if pointer to table isn't NULL),
2204d5d7cf4eSJonathan Chew  * and return number of CPU APIC IDs.
2205dae2fa37Sjjc  *
2206dae2fa37Sjjc  * NOTE: This code assumes that CPU IDs are assigned in order that they appear
2207dae2fa37Sjjc  *       in in cpu_apicid_array boot property which is based on and follows
2208dae2fa37Sjjc  *	 same ordering as processor list in ACPI MADT.  If the code in
2209dae2fa37Sjjc  *	 usr/src/uts/i86pc/io/pcplusmp/apic.c that reads MADT and assigns
2210dae2fa37Sjjc  *	 CPU IDs ever changes, then this code will need to change too....
2211dae2fa37Sjjc  */
2212dae2fa37Sjjc static int
2213d821f0f0Sjjc lgrp_plat_process_cpu_apicids(cpu_node_map_t *cpu_node)
2214dae2fa37Sjjc {
2215d821f0f0Sjjc 	int	boot_prop_len;
2216dae2fa37Sjjc 	char	*boot_prop_name = BP_CPU_APICID_ARRAY;
2217dae2fa37Sjjc 	uint8_t	cpu_apicid_array[UINT8_MAX + 1];
2218dae2fa37Sjjc 	int	i;
2219d821f0f0Sjjc 	int	n;
2220dae2fa37Sjjc 
2221dae2fa37Sjjc 	/*
2222dae2fa37Sjjc 	 * Check length of property value
2223dae2fa37Sjjc 	 */
2224dae2fa37Sjjc 	boot_prop_len = BOP_GETPROPLEN(bootops, boot_prop_name);
2225d821f0f0Sjjc 	if (boot_prop_len <= 0 || boot_prop_len > sizeof (cpu_apicid_array))
2226d5d7cf4eSJonathan Chew 		return (-1);
2227d821f0f0Sjjc 
2228d821f0f0Sjjc 	/*
2229d821f0f0Sjjc 	 * Calculate number of entries in array and return when there's just
2230d821f0f0Sjjc 	 * one CPU since that's not very interesting for NUMA
2231d821f0f0Sjjc 	 */
2232d821f0f0Sjjc 	n = boot_prop_len / sizeof (uint8_t);
2233d821f0f0Sjjc 	if (n == 1)
2234d5d7cf4eSJonathan Chew 		return (-2);
2235dae2fa37Sjjc 
2236dae2fa37Sjjc 	/*
2237dae2fa37Sjjc 	 * Get CPU to APIC ID property value
2238dae2fa37Sjjc 	 */
2239dae2fa37Sjjc 	if (BOP_GETPROP(bootops, boot_prop_name, cpu_apicid_array) < 0)
2240d5d7cf4eSJonathan Chew 		return (-3);
2241d5d7cf4eSJonathan Chew 
2242d5d7cf4eSJonathan Chew 	/*
2243d5d7cf4eSJonathan Chew 	 * Just return number of CPU APIC IDs if CPU to node mapping table is
2244d5d7cf4eSJonathan Chew 	 * NULL
2245d5d7cf4eSJonathan Chew 	 */
2246d5d7cf4eSJonathan Chew 	if (cpu_node == NULL)
2247d5d7cf4eSJonathan Chew 		return (n);
2248dae2fa37Sjjc 
2249dae2fa37Sjjc 	/*
2250dae2fa37Sjjc 	 * Fill in CPU to node ID mapping table with APIC ID for each CPU
2251dae2fa37Sjjc 	 */
2252d821f0f0Sjjc 	for (i = 0; i < n; i++) {
2253dae2fa37Sjjc 		cpu_node[i].exists = 1;
2254dae2fa37Sjjc 		cpu_node[i].apicid = cpu_apicid_array[i];
2255dae2fa37Sjjc 	}
2256dae2fa37Sjjc 
2257d821f0f0Sjjc 	/*
2258d821f0f0Sjjc 	 * Return number of CPUs based on number of APIC IDs
2259d821f0f0Sjjc 	 */
2260d821f0f0Sjjc 	return (n);
2261dae2fa37Sjjc }
2262dae2fa37Sjjc 
2263dae2fa37Sjjc 
2264dae2fa37Sjjc /*
22652e2c009bSjjc  * Read ACPI System Locality Information Table (SLIT) to determine how far each
22662e2c009bSjjc  * NUMA node is from each other
22672e2c009bSjjc  */
22682e2c009bSjjc static int
22692e2c009bSjjc lgrp_plat_process_slit(struct slit *tp, uint_t node_cnt,
22702e2c009bSjjc     node_phys_addr_map_t *node_memory, lgrp_plat_latency_stats_t *lat_stats)
22712e2c009bSjjc {
22722e2c009bSjjc 	int		i;
22732e2c009bSjjc 	int		j;
22742e2c009bSjjc 	int		localities;
22752e2c009bSjjc 	hrtime_t	max;
22762e2c009bSjjc 	hrtime_t	min;
22772e2c009bSjjc 	int		retval;
22782e2c009bSjjc 	uint8_t		*slit_entries;
22792e2c009bSjjc 
22802e2c009bSjjc 	if (tp == NULL || !lgrp_plat_slit_enable)
22812e2c009bSjjc 		return (1);
22822e2c009bSjjc 
22832e2c009bSjjc 	if (lat_stats == NULL)
22842e2c009bSjjc 		return (2);
22852e2c009bSjjc 
22862e2c009bSjjc 	localities = tp->number;
22872e2c009bSjjc 	if (localities != node_cnt)
22882e2c009bSjjc 		return (3);
22892e2c009bSjjc 
22902e2c009bSjjc 	min = lat_stats->latency_min;
22912e2c009bSjjc 	max = lat_stats->latency_max;
22922e2c009bSjjc 
22932e2c009bSjjc 	/*
22942e2c009bSjjc 	 * Fill in latency matrix based on SLIT entries
22952e2c009bSjjc 	 */
22962e2c009bSjjc 	slit_entries = tp->entry;
22972e2c009bSjjc 	for (i = 0; i < localities; i++) {
22982e2c009bSjjc 		for (j = 0; j < localities; j++) {
22992e2c009bSjjc 			uint8_t	latency;
23002e2c009bSjjc 
23012e2c009bSjjc 			latency = slit_entries[(i * localities) + j];
23022e2c009bSjjc 			lat_stats->latencies[i][j] = latency;
23035b7cf7f0Sjjc 			if (latency < min || min == -1)
23042e2c009bSjjc 				min = latency;
23052e2c009bSjjc 			if (latency > max)
23062e2c009bSjjc 				max = latency;
23072e2c009bSjjc 		}
23082e2c009bSjjc 	}
23092e2c009bSjjc 
23102e2c009bSjjc 	/*
23112e2c009bSjjc 	 * Verify that latencies/distances given in SLIT look reasonable
23122e2c009bSjjc 	 */
23132e2c009bSjjc 	retval = lgrp_plat_latency_verify(node_memory, lat_stats);
23142e2c009bSjjc 
23152e2c009bSjjc 	if (retval) {
23162e2c009bSjjc 		/*
23172e2c009bSjjc 		 * Reinitialize (zero) latency table since SLIT doesn't look
23182e2c009bSjjc 		 * right
23192e2c009bSjjc 		 */
23202e2c009bSjjc 		for (i = 0; i < localities; i++) {
23212e2c009bSjjc 			for (j = 0; j < localities; j++)
23222e2c009bSjjc 				lat_stats->latencies[i][j] = 0;
23232e2c009bSjjc 		}
23242e2c009bSjjc 	} else {
23252e2c009bSjjc 		/*
23262e2c009bSjjc 		 * Update min and max latencies seen since SLIT looks valid
23272e2c009bSjjc 		 */
23282e2c009bSjjc 		lat_stats->latency_min = min;
23292e2c009bSjjc 		lat_stats->latency_max = max;
23302e2c009bSjjc 	}
23312e2c009bSjjc 
23322e2c009bSjjc 	return (retval);
23332e2c009bSjjc }
23342e2c009bSjjc 
23352e2c009bSjjc 
23362e2c009bSjjc /*
23372e2c009bSjjc  * Read ACPI System Resource Affinity Table (SRAT) to determine which CPUs
2338d821f0f0Sjjc  * and memory are local to each other in the same NUMA node and return number
2339d821f0f0Sjjc  * of nodes
23402e2c009bSjjc  */
23412e2c009bSjjc static int
234281d9ccb6SJonathan Chew lgrp_plat_process_srat(struct srat *tp, uint32_t *prox_domain_min,
234381d9ccb6SJonathan Chew     node_domain_map_t *node_domain, cpu_node_map_t *cpu_node, int cpu_count,
234481d9ccb6SJonathan Chew     node_phys_addr_map_t *node_memory)
23452e2c009bSjjc {
23465b7cf7f0Sjjc 	struct srat_item	*srat_end;
23472e2c009bSjjc 	int			i;
23482e2c009bSjjc 	struct srat_item	*item;
2349d821f0f0Sjjc 	int			node_cnt;
2350dae2fa37Sjjc 	int			proc_entry_count;
23512e2c009bSjjc 
2352d821f0f0Sjjc 	/*
2353d821f0f0Sjjc 	 * Nothing to do when no SRAT or disabled
2354d821f0f0Sjjc 	 */
23552e2c009bSjjc 	if (tp == NULL || !lgrp_plat_srat_enable)
2356d821f0f0Sjjc 		return (-1);
23572e2c009bSjjc 
23582e2c009bSjjc 	/*
23592e2c009bSjjc 	 * Determine number of nodes by counting number of proximity domains in
2360d821f0f0Sjjc 	 * SRAT and return if number of nodes is 1 or less since don't need to
2361d821f0f0Sjjc 	 * read SRAT then
23622e2c009bSjjc 	 */
236381d9ccb6SJonathan Chew 	node_cnt = lgrp_plat_srat_domains(tp, prox_domain_min);
2364d821f0f0Sjjc 	if (node_cnt == 1)
2365d821f0f0Sjjc 		return (1);
2366d821f0f0Sjjc 	else if (node_cnt <= 0)
2367d821f0f0Sjjc 		return (-2);
23682e2c009bSjjc 
23692e2c009bSjjc 	/*
23702e2c009bSjjc 	 * Walk through SRAT, examining each CPU and memory entry to determine
23712e2c009bSjjc 	 * which CPUs and memory belong to which node.
23722e2c009bSjjc 	 */
23732e2c009bSjjc 	item = tp->list;
23745b7cf7f0Sjjc 	srat_end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp);
2375dae2fa37Sjjc 	proc_entry_count = 0;
23765b7cf7f0Sjjc 	while (item < srat_end) {
23772e2c009bSjjc 		uint32_t	apic_id;
23782e2c009bSjjc 		uint32_t	domain;
23792e2c009bSjjc 		uint64_t	end;
23802e2c009bSjjc 		uint64_t	length;
23812e2c009bSjjc 		uint64_t	start;
23822e2c009bSjjc 
23832e2c009bSjjc 		switch (item->type) {
23842e2c009bSjjc 		case SRAT_PROCESSOR:	/* CPU entry */
23852e2c009bSjjc 			if (!(item->i.p.flags & SRAT_ENABLED) ||
23862e2c009bSjjc 			    cpu_node == NULL)
23872e2c009bSjjc 				break;
23882e2c009bSjjc 
23892e2c009bSjjc 			/*
23902e2c009bSjjc 			 * Calculate domain (node) ID and fill in APIC ID to
23912e2c009bSjjc 			 * domain/node mapping table
23922e2c009bSjjc 			 */
23932e2c009bSjjc 			domain = item->i.p.domain1;
23942e2c009bSjjc 			for (i = 0; i < 3; i++) {
23952e2c009bSjjc 				domain += item->i.p.domain2[i] <<
23962e2c009bSjjc 				    ((i + 1) * 8);
23972e2c009bSjjc 			}
23982e2c009bSjjc 			apic_id = item->i.p.apic_id;
23992e2c009bSjjc 
2400d821f0f0Sjjc 			if (lgrp_plat_cpu_node_update(node_domain, node_cnt,
2401d821f0f0Sjjc 			    cpu_node, cpu_count, apic_id, domain) < 0)
2402d821f0f0Sjjc 				return (-3);
2403dae2fa37Sjjc 
2404dae2fa37Sjjc 			proc_entry_count++;
24052e2c009bSjjc 			break;
24062e2c009bSjjc 
24072e2c009bSjjc 		case SRAT_MEMORY:	/* memory entry */
24082e2c009bSjjc 			if (!(item->i.m.flags & SRAT_ENABLED) ||
24092e2c009bSjjc 			    node_memory == NULL)
24102e2c009bSjjc 				break;
24112e2c009bSjjc 
24122e2c009bSjjc 			/*
24132e2c009bSjjc 			 * Get domain (node) ID and fill in domain/node
24142e2c009bSjjc 			 * to memory mapping table
24152e2c009bSjjc 			 */
24162e2c009bSjjc 			domain = item->i.m.domain;
24172e2c009bSjjc 			start = item->i.m.base_addr;
24182e2c009bSjjc 			length = item->i.m.len;
24192e2c009bSjjc 			end = start + length - 1;
24202e2c009bSjjc 
2421d821f0f0Sjjc 			if (lgrp_plat_node_memory_update(node_domain, node_cnt,
24222e2c009bSjjc 			    node_memory, start, end, domain) < 0)
2423d821f0f0Sjjc 				return (-4);
24242e2c009bSjjc 			break;
2425b6917abeSmishra 		case SRAT_X2APIC:	/* x2apic CPU entry */
2426b6917abeSmishra 			if (!(item->i.xp.flags & SRAT_ENABLED) ||
2427b6917abeSmishra 			    cpu_node == NULL)
2428b6917abeSmishra 				break;
2429b6917abeSmishra 
2430b6917abeSmishra 			/*
2431b6917abeSmishra 			 * Calculate domain (node) ID and fill in APIC ID to
2432b6917abeSmishra 			 * domain/node mapping table
2433b6917abeSmishra 			 */
2434b6917abeSmishra 			domain = item->i.xp.domain;
2435b6917abeSmishra 			apic_id = item->i.xp.x2apic_id;
2436b6917abeSmishra 
2437b6917abeSmishra 			if (lgrp_plat_cpu_node_update(node_domain, node_cnt,
2438b6917abeSmishra 			    cpu_node, cpu_count, apic_id, domain) < 0)
2439b6917abeSmishra 				return (-3);
2440b6917abeSmishra 
2441b6917abeSmishra 			proc_entry_count++;
2442b6917abeSmishra 			break;
24432e2c009bSjjc 
24442e2c009bSjjc 		default:
24452e2c009bSjjc 			break;
24462e2c009bSjjc 		}
24472e2c009bSjjc 
24482e2c009bSjjc 		item = (struct srat_item *)((uintptr_t)item + item->len);
24492e2c009bSjjc 	}
2450dae2fa37Sjjc 
2451dae2fa37Sjjc 	/*
2452dae2fa37Sjjc 	 * Should have seen at least as many SRAT processor entries as CPUs
2453dae2fa37Sjjc 	 */
2454d821f0f0Sjjc 	if (proc_entry_count < cpu_count)
2455d821f0f0Sjjc 		return (-5);
2456dae2fa37Sjjc 
245781d9ccb6SJonathan Chew 	/*
245881d9ccb6SJonathan Chew 	 * Need to sort nodes by starting physical address since VM system
245981d9ccb6SJonathan Chew 	 * assumes and expects memnodes to be sorted in ascending order by
246081d9ccb6SJonathan Chew 	 * physical address
246181d9ccb6SJonathan Chew 	 */
246281d9ccb6SJonathan Chew 	lgrp_plat_node_sort(node_domain, node_cnt, cpu_node, cpu_count,
246381d9ccb6SJonathan Chew 	    node_memory);
246481d9ccb6SJonathan Chew 
2465d821f0f0Sjjc 	return (node_cnt);
24662e2c009bSjjc }
24672e2c009bSjjc 
24682e2c009bSjjc 
24692e2c009bSjjc /*
2470d5d7cf4eSJonathan Chew  * Allocate permanent memory for any temporary memory that we needed to
2471d5d7cf4eSJonathan Chew  * allocate using BOP_ALLOC() before kmem_alloc() and VM system were
2472d5d7cf4eSJonathan Chew  * initialized and copy everything from temporary to permanent memory since
2473d5d7cf4eSJonathan Chew  * temporary boot memory will eventually be released during boot
2474d5d7cf4eSJonathan Chew  */
2475d5d7cf4eSJonathan Chew static void
2476d5d7cf4eSJonathan Chew lgrp_plat_release_bootstrap(void)
2477d5d7cf4eSJonathan Chew {
2478d5d7cf4eSJonathan Chew 	void	*buf;
2479d5d7cf4eSJonathan Chew 	size_t	size;
2480d5d7cf4eSJonathan Chew 
2481d5d7cf4eSJonathan Chew 	if (lgrp_plat_cpu_node_nentries > 0) {
2482d5d7cf4eSJonathan Chew 		size = lgrp_plat_cpu_node_nentries * sizeof (cpu_node_map_t);
2483d5d7cf4eSJonathan Chew 		buf = kmem_alloc(size, KM_SLEEP);
2484d5d7cf4eSJonathan Chew 		bcopy(lgrp_plat_cpu_node, buf, size);
2485d5d7cf4eSJonathan Chew 		lgrp_plat_cpu_node = buf;
2486d5d7cf4eSJonathan Chew 	}
2487d5d7cf4eSJonathan Chew }
2488d5d7cf4eSJonathan Chew 
2489d5d7cf4eSJonathan Chew 
2490d5d7cf4eSJonathan Chew /*
24912e2c009bSjjc  * Return number of proximity domains given in ACPI SRAT
24922e2c009bSjjc  */
24932e2c009bSjjc static int
249481d9ccb6SJonathan Chew lgrp_plat_srat_domains(struct srat *tp, uint32_t *prox_domain_min)
24952e2c009bSjjc {
24962e2c009bSjjc 	int			domain_cnt;
249781d9ccb6SJonathan Chew 	uint32_t		domain_min;
24982e2c009bSjjc 	struct srat_item	*end;
24992e2c009bSjjc 	int			i;
25002e2c009bSjjc 	struct srat_item	*item;
25012e2c009bSjjc 	node_domain_map_t	node_domain[MAX_NODES];
25022e2c009bSjjc 
25032e2c009bSjjc 
25042e2c009bSjjc 	if (tp == NULL || !lgrp_plat_srat_enable)
25052e2c009bSjjc 		return (1);
25062e2c009bSjjc 
25072e2c009bSjjc 	/*
250881d9ccb6SJonathan Chew 	 * Walk through SRAT to find minimum proximity domain ID
250981d9ccb6SJonathan Chew 	 */
251081d9ccb6SJonathan Chew 	domain_min = UINT32_MAX;
251181d9ccb6SJonathan Chew 	item = tp->list;
251281d9ccb6SJonathan Chew 	end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp);
251381d9ccb6SJonathan Chew 	while (item < end) {
251481d9ccb6SJonathan Chew 		uint32_t	domain;
251581d9ccb6SJonathan Chew 
251681d9ccb6SJonathan Chew 		switch (item->type) {
251781d9ccb6SJonathan Chew 		case SRAT_PROCESSOR:	/* CPU entry */
251881d9ccb6SJonathan Chew 			if (!(item->i.p.flags & SRAT_ENABLED)) {
251981d9ccb6SJonathan Chew 				item = (struct srat_item *)((uintptr_t)item +
252081d9ccb6SJonathan Chew 				    item->len);
252181d9ccb6SJonathan Chew 				continue;
252281d9ccb6SJonathan Chew 			}
252381d9ccb6SJonathan Chew 			domain = item->i.p.domain1;
252481d9ccb6SJonathan Chew 			for (i = 0; i < 3; i++) {
252581d9ccb6SJonathan Chew 				domain += item->i.p.domain2[i] <<
252681d9ccb6SJonathan Chew 				    ((i + 1) * 8);
252781d9ccb6SJonathan Chew 			}
252881d9ccb6SJonathan Chew 			break;
252981d9ccb6SJonathan Chew 
253081d9ccb6SJonathan Chew 		case SRAT_MEMORY:	/* memory entry */
253181d9ccb6SJonathan Chew 			if (!(item->i.m.flags & SRAT_ENABLED)) {
253281d9ccb6SJonathan Chew 				item = (struct srat_item *)((uintptr_t)item +
253381d9ccb6SJonathan Chew 				    item->len);
253481d9ccb6SJonathan Chew 				continue;
253581d9ccb6SJonathan Chew 			}
253681d9ccb6SJonathan Chew 			domain = item->i.m.domain;
253781d9ccb6SJonathan Chew 			break;
253881d9ccb6SJonathan Chew 
253981d9ccb6SJonathan Chew 		case SRAT_X2APIC:	/* x2apic CPU entry */
254081d9ccb6SJonathan Chew 			if (!(item->i.xp.flags & SRAT_ENABLED)) {
254181d9ccb6SJonathan Chew 				item = (struct srat_item *)((uintptr_t)item +
254281d9ccb6SJonathan Chew 				    item->len);
254381d9ccb6SJonathan Chew 				continue;
254481d9ccb6SJonathan Chew 			}
254581d9ccb6SJonathan Chew 			domain = item->i.xp.domain;
254681d9ccb6SJonathan Chew 			break;
254781d9ccb6SJonathan Chew 
254881d9ccb6SJonathan Chew 		default:
254981d9ccb6SJonathan Chew 			item = (struct srat_item *)((uintptr_t)item +
255081d9ccb6SJonathan Chew 			    item->len);
255181d9ccb6SJonathan Chew 			continue;
255281d9ccb6SJonathan Chew 		}
255381d9ccb6SJonathan Chew 
255481d9ccb6SJonathan Chew 		/*
255581d9ccb6SJonathan Chew 		 * Keep track of minimum proximity domain ID
255681d9ccb6SJonathan Chew 		 */
255781d9ccb6SJonathan Chew 		if (domain < domain_min)
255881d9ccb6SJonathan Chew 			domain_min = domain;
255981d9ccb6SJonathan Chew 
256081d9ccb6SJonathan Chew 		item = (struct srat_item *)((uintptr_t)item + item->len);
256181d9ccb6SJonathan Chew 	}
256281d9ccb6SJonathan Chew 	if (lgrp_plat_domain_min_enable && prox_domain_min != NULL)
256381d9ccb6SJonathan Chew 		*prox_domain_min = domain_min;
256481d9ccb6SJonathan Chew 
256581d9ccb6SJonathan Chew 	/*
25662e2c009bSjjc 	 * Walk through SRAT, examining each CPU and memory entry to determine
25672e2c009bSjjc 	 * proximity domain ID for each.
25682e2c009bSjjc 	 */
25692e2c009bSjjc 	domain_cnt = 0;
25702e2c009bSjjc 	item = tp->list;
25712e2c009bSjjc 	end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp);
25722e2c009bSjjc 	bzero(node_domain, MAX_NODES * sizeof (node_domain_map_t));
25732e2c009bSjjc 	while (item < end) {
25742e2c009bSjjc 		uint32_t	domain;
25752e2c009bSjjc 		boolean_t	overflow;
25762e2c009bSjjc 		uint_t		start;
25772e2c009bSjjc 
25782e2c009bSjjc 		switch (item->type) {
25792e2c009bSjjc 		case SRAT_PROCESSOR:	/* CPU entry */
258081d9ccb6SJonathan Chew 			if (!(item->i.p.flags & SRAT_ENABLED)) {
258181d9ccb6SJonathan Chew 				item = (struct srat_item *)((uintptr_t)item +
258281d9ccb6SJonathan Chew 				    item->len);
258381d9ccb6SJonathan Chew 				continue;
258481d9ccb6SJonathan Chew 			}
25852e2c009bSjjc 			domain = item->i.p.domain1;
25862e2c009bSjjc 			for (i = 0; i < 3; i++) {
25872e2c009bSjjc 				domain += item->i.p.domain2[i] <<
25882e2c009bSjjc 				    ((i + 1) * 8);
25892e2c009bSjjc 			}
25902e2c009bSjjc 			break;
25912e2c009bSjjc 
25922e2c009bSjjc 		case SRAT_MEMORY:	/* memory entry */
259381d9ccb6SJonathan Chew 			if (!(item->i.m.flags & SRAT_ENABLED)) {
259481d9ccb6SJonathan Chew 				item = (struct srat_item *)((uintptr_t)item +
259581d9ccb6SJonathan Chew 				    item->len);
259681d9ccb6SJonathan Chew 				continue;
259781d9ccb6SJonathan Chew 			}
25982e2c009bSjjc 			domain = item->i.m.domain;
25992e2c009bSjjc 			break;
26002e2c009bSjjc 
2601b6917abeSmishra 		case SRAT_X2APIC:	/* x2apic CPU entry */
260281d9ccb6SJonathan Chew 			if (!(item->i.xp.flags & SRAT_ENABLED)) {
260381d9ccb6SJonathan Chew 				item = (struct srat_item *)((uintptr_t)item +
260481d9ccb6SJonathan Chew 				    item->len);
260581d9ccb6SJonathan Chew 				continue;
260681d9ccb6SJonathan Chew 			}
2607b6917abeSmishra 			domain = item->i.xp.domain;
2608b6917abeSmishra 			break;
2609b6917abeSmishra 
26102e2c009bSjjc 		default:
261181d9ccb6SJonathan Chew 			item = (struct srat_item *)((uintptr_t)item +
261281d9ccb6SJonathan Chew 			    item->len);
261381d9ccb6SJonathan Chew 			continue;
26142e2c009bSjjc 		}
26152e2c009bSjjc 
26162e2c009bSjjc 		/*
26172e2c009bSjjc 		 * Count and keep track of which proximity domain IDs seen
26182e2c009bSjjc 		 */
26192e2c009bSjjc 		start = i = domain % MAX_NODES;
26202e2c009bSjjc 		overflow = B_TRUE;
26212e2c009bSjjc 		do {
26222e2c009bSjjc 			/*
26232e2c009bSjjc 			 * Create entry for proximity domain and increment
26242e2c009bSjjc 			 * count when no entry exists where proximity domain
26252e2c009bSjjc 			 * hashed
26262e2c009bSjjc 			 */
26272e2c009bSjjc 			if (!node_domain[i].exists) {
26282e2c009bSjjc 				node_domain[i].exists = 1;
26292e2c009bSjjc 				node_domain[i].prox_domain = domain;
26302e2c009bSjjc 				domain_cnt++;
26312e2c009bSjjc 				overflow = B_FALSE;
26322e2c009bSjjc 				break;
26332e2c009bSjjc 			}
26342e2c009bSjjc 
26352e2c009bSjjc 			/*
26362e2c009bSjjc 			 * Nothing to do when proximity domain seen already
26372e2c009bSjjc 			 * and its entry exists
26382e2c009bSjjc 			 */
26392e2c009bSjjc 			if (node_domain[i].prox_domain == domain) {
26402e2c009bSjjc 				overflow = B_FALSE;
26412e2c009bSjjc 				break;
26422e2c009bSjjc 			}
26432e2c009bSjjc 
26442e2c009bSjjc 			/*
26452e2c009bSjjc 			 * Entry exists where proximity domain hashed, but for
26462e2c009bSjjc 			 * different proximity domain so keep search for empty
26472e2c009bSjjc 			 * slot to put it or matching entry whichever comes
26482e2c009bSjjc 			 * first.
26492e2c009bSjjc 			 */
26502e2c009bSjjc 			i = (i + 1) % MAX_NODES;
26512e2c009bSjjc 		} while (i != start);
26522e2c009bSjjc 
26532e2c009bSjjc 		/*
26542e2c009bSjjc 		 * Didn't find empty or matching entry which means have more
26552e2c009bSjjc 		 * proximity domains than supported nodes (:-(
26562e2c009bSjjc 		 */
26572e2c009bSjjc 		ASSERT(overflow != B_TRUE);
26582e2c009bSjjc 		if (overflow == B_TRUE)
26592e2c009bSjjc 			return (-1);
26602e2c009bSjjc 
26612e2c009bSjjc 		item = (struct srat_item *)((uintptr_t)item + item->len);
26622e2c009bSjjc 	}
26632e2c009bSjjc 	return (domain_cnt);
26642e2c009bSjjc }
26652e2c009bSjjc 
26662e2c009bSjjc 
26672e2c009bSjjc /*
26682e2c009bSjjc  * Set lgroup latencies for 2 level lgroup topology
26692e2c009bSjjc  */
26702e2c009bSjjc static void
26712e2c009bSjjc lgrp_plat_2level_setup(node_phys_addr_map_t *node_memory,
26722e2c009bSjjc     lgrp_plat_latency_stats_t *lat_stats)
26732e2c009bSjjc {
26742e2c009bSjjc 	int	i;
26752e2c009bSjjc 
26762e2c009bSjjc 	ASSERT(node_memory != NULL && lat_stats != NULL);
26772e2c009bSjjc 
26782e2c009bSjjc 	if (lgrp_plat_node_cnt >= 4)
26792e2c009bSjjc 		cmn_err(CE_NOTE,
26802e2c009bSjjc 		    "MPO only optimizing for local and remote\n");
26812e2c009bSjjc 	for (i = 0; i < lgrp_plat_node_cnt; i++) {
26822e2c009bSjjc 		int	j;
26832e2c009bSjjc 
26842e2c009bSjjc 		if (!node_memory[i].exists)
26852e2c009bSjjc 			continue;
26862e2c009bSjjc 		for (j = 0; j < lgrp_plat_node_cnt; j++) {
26872e2c009bSjjc 			if (!node_memory[j].exists)
26882e2c009bSjjc 				continue;
26892e2c009bSjjc 			if (i == j)
26902e2c009bSjjc 				lat_stats->latencies[i][j] = 2;
26912e2c009bSjjc 			else
26922e2c009bSjjc 				lat_stats->latencies[i][j] = 3;
26932e2c009bSjjc 		}
26942e2c009bSjjc 	}
26952e2c009bSjjc 	lat_stats->latency_min = 2;
26962e2c009bSjjc 	lat_stats->latency_max = 3;
26972e2c009bSjjc 	lgrp_config(LGRP_CONFIG_FLATTEN, 2, 0);
26982e2c009bSjjc }
26992e2c009bSjjc 
27002e2c009bSjjc 
27012e2c009bSjjc /*
27022e2c009bSjjc  * The following Opteron specific constants, macros, types, and routines define
27032e2c009bSjjc  * PCI configuration space registers and how to read them to determine the NUMA
27042e2c009bSjjc  * configuration of *supported* Opteron processors.  They provide the same
27052e2c009bSjjc  * information that may be gotten from the ACPI System Resource Affinity Table
27062e2c009bSjjc  * (SRAT) if it exists on the machine of interest.
27072e2c009bSjjc  *
27082e2c009bSjjc  * The AMD BIOS and Kernel Developer's Guide (BKDG) for the processor family
27092e2c009bSjjc  * of interest describes all of these registers and their contents.  The main
27102e2c009bSjjc  * registers used by this code to determine the NUMA configuration of the
27112e2c009bSjjc  * machine are the node ID register for the number of NUMA nodes and the DRAM
27122e2c009bSjjc  * address map registers for the physical address range of each node.
27132e2c009bSjjc  *
27142e2c009bSjjc  * NOTE: The format and how to determine the NUMA configuration using PCI
27152e2c009bSjjc  *	 config space registers may change or may not be supported in future
27162e2c009bSjjc  *	 Opteron processor families.
27177c478bd9Sstevel@tonic-gate  */
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate /*
27207c478bd9Sstevel@tonic-gate  * How many bits to shift Opteron DRAM Address Map base and limit registers
27217c478bd9Sstevel@tonic-gate  * to get actual value
27227c478bd9Sstevel@tonic-gate  */
2723f78a91cdSjjc #define	OPT_DRAMADDR_HI_LSHIFT_ADDR	40	/* shift left for address */
2724f78a91cdSjjc #define	OPT_DRAMADDR_LO_LSHIFT_ADDR	8	/* shift left for address */
27257c478bd9Sstevel@tonic-gate 
2726f78a91cdSjjc #define	OPT_DRAMADDR_HI_MASK_ADDR	0x000000FF /* address bits 47-40 */
2727f78a91cdSjjc #define	OPT_DRAMADDR_LO_MASK_ADDR	0xFFFF0000 /* address bits 39-24 */
2728f78a91cdSjjc 
2729f78a91cdSjjc #define	OPT_DRAMADDR_LO_MASK_OFF	0xFFFFFF /* offset for address */
2730f78a91cdSjjc 
2731f78a91cdSjjc /*
2732f78a91cdSjjc  * Macros to derive addresses from Opteron DRAM Address Map registers
2733f78a91cdSjjc  */
2734f78a91cdSjjc #define	OPT_DRAMADDR_HI(reg) \
2735f78a91cdSjjc 	(((u_longlong_t)reg & OPT_DRAMADDR_HI_MASK_ADDR) << \
2736f78a91cdSjjc 	    OPT_DRAMADDR_HI_LSHIFT_ADDR)
2737f78a91cdSjjc 
2738f78a91cdSjjc #define	OPT_DRAMADDR_LO(reg) \
2739f78a91cdSjjc 	(((u_longlong_t)reg & OPT_DRAMADDR_LO_MASK_ADDR) << \
2740f78a91cdSjjc 	    OPT_DRAMADDR_LO_LSHIFT_ADDR)
2741f78a91cdSjjc 
2742f78a91cdSjjc #define	OPT_DRAMADDR(high, low) \
2743f78a91cdSjjc 	(OPT_DRAMADDR_HI(high) | OPT_DRAMADDR_LO(low))
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate /*
27467c478bd9Sstevel@tonic-gate  * Bit masks defining what's in Opteron DRAM Address Map base register
27477c478bd9Sstevel@tonic-gate  */
2748f78a91cdSjjc #define	OPT_DRAMBASE_LO_MASK_RE		0x1	/* read enable */
2749f78a91cdSjjc #define	OPT_DRAMBASE_LO_MASK_WE		0x2	/* write enable */
2750f78a91cdSjjc #define	OPT_DRAMBASE_LO_MASK_INTRLVEN	0x700	/* interleave */
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate /*
27537c478bd9Sstevel@tonic-gate  * Bit masks defining what's in Opteron DRAM Address Map limit register
27547c478bd9Sstevel@tonic-gate  */
2755f78a91cdSjjc #define	OPT_DRAMLIMIT_LO_MASK_DSTNODE	0x7		/* destination node */
2756f78a91cdSjjc #define	OPT_DRAMLIMIT_LO_MASK_INTRLVSEL	0x700		/* interleave select */
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate /*
27607c478bd9Sstevel@tonic-gate  * Opteron Node ID register in PCI configuration space contains
27617c478bd9Sstevel@tonic-gate  * number of nodes in system, etc. for Opteron K8.  The following
27627c478bd9Sstevel@tonic-gate  * constants and macros define its contents, structure, and access.
27637c478bd9Sstevel@tonic-gate  */
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate /*
27667c478bd9Sstevel@tonic-gate  * Bit masks defining what's in Opteron Node ID register
27677c478bd9Sstevel@tonic-gate  */
27687c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_ID	0x7	/* node ID */
27697c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_CNT	0x70	/* node count */
27707c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_IONODE	0x700	/* Hypertransport I/O hub node ID */
27717c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_LCKNODE	0x7000	/* lock controller node ID */
27727c478bd9Sstevel@tonic-gate #define	OPT_NODE_MASK_CPUCNT	0xF0000	/* CPUs in system (0 means 1 CPU)  */
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate /*
27757c478bd9Sstevel@tonic-gate  * How many bits in Opteron Node ID register to shift right to get actual value
27767c478bd9Sstevel@tonic-gate  */
27777c478bd9Sstevel@tonic-gate #define	OPT_NODE_RSHIFT_CNT	0x4	/* shift right for node count value */
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate /*
27807c478bd9Sstevel@tonic-gate  * Macros to get values from Opteron Node ID register
27817c478bd9Sstevel@tonic-gate  */
27827c478bd9Sstevel@tonic-gate #define	OPT_NODE_CNT(reg) \
27837c478bd9Sstevel@tonic-gate 	((reg & OPT_NODE_MASK_CNT) >> OPT_NODE_RSHIFT_CNT)
27847c478bd9Sstevel@tonic-gate 
2785f78a91cdSjjc /*
2786f78a91cdSjjc  * Macro to setup PCI Extended Configuration Space (ECS) address to give to
2787f78a91cdSjjc  * "in/out" instructions
2788f78a91cdSjjc  *
2789f78a91cdSjjc  * NOTE: Should only be used in lgrp_plat_init() before MMIO setup because any
2790f78a91cdSjjc  *	 other uses should just do MMIO to access PCI ECS.
2791f78a91cdSjjc  *	 Must enable special bit in Northbridge Configuration Register on
2792f78a91cdSjjc  *	 Greyhound for extended CF8 space access to be able to access PCI ECS
2793f78a91cdSjjc  *	 using "in/out" instructions and restore special bit after done
2794f78a91cdSjjc  *	 accessing PCI ECS.
2795f78a91cdSjjc  */
2796f78a91cdSjjc #define	OPT_PCI_ECS_ADDR(bus, device, function, reg) \
2797f78a91cdSjjc 	(PCI_CONE | (((bus) & 0xff) << 16) | (((device & 0x1f)) << 11)  | \
2798f78a91cdSjjc 	    (((function) & 0x7) << 8) | ((reg) & 0xfc) | \
2799f78a91cdSjjc 	    ((((reg) >> 8) & 0xf) << 24))
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate /*
28027c478bd9Sstevel@tonic-gate  * PCI configuration space registers accessed by specifying
28037c478bd9Sstevel@tonic-gate  * a bus, device, function, and offset.  The following constants
28047c478bd9Sstevel@tonic-gate  * define the values needed to access Opteron K8 configuration
28057c478bd9Sstevel@tonic-gate  * info to determine its node topology
28067c478bd9Sstevel@tonic-gate  */
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate #define	OPT_PCS_BUS_CONFIG	0	/* Hypertransport config space bus */
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate /*
28117c478bd9Sstevel@tonic-gate  * Opteron PCI configuration space register function values
28127c478bd9Sstevel@tonic-gate  */
28137c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_HT		0	/* Hypertransport configuration */
28147c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_ADDRMAP	1	/* Address map configuration */
28157c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_DRAM	2	/* DRAM configuration */
28167c478bd9Sstevel@tonic-gate #define	OPT_PCS_FUNC_MISC	3	/* Miscellaneous configuration */
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate /*
28197c478bd9Sstevel@tonic-gate  * PCI Configuration Space register offsets
28207c478bd9Sstevel@tonic-gate  */
28217c478bd9Sstevel@tonic-gate #define	OPT_PCS_OFF_VENDOR	0x0	/* device/vendor ID register */
2822f78a91cdSjjc #define	OPT_PCS_OFF_DRAMBASE_HI	0x140	/* DRAM Base register (node 0) */
2823f78a91cdSjjc #define	OPT_PCS_OFF_DRAMBASE_LO	0x40	/* DRAM Base register (node 0) */
28247c478bd9Sstevel@tonic-gate #define	OPT_PCS_OFF_NODEID	0x60	/* Node ID register */
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate /*
28277c478bd9Sstevel@tonic-gate  * Opteron PCI Configuration Space device IDs for nodes
28287c478bd9Sstevel@tonic-gate  */
28297c478bd9Sstevel@tonic-gate #define	OPT_PCS_DEV_NODE0		24	/* device number for node 0 */
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate /*
28337c478bd9Sstevel@tonic-gate  * Opteron DRAM address map gives base and limit for physical memory in a node
28347c478bd9Sstevel@tonic-gate  */
28357c478bd9Sstevel@tonic-gate typedef	struct opt_dram_addr_map {
2836f78a91cdSjjc 	uint32_t	base_hi;
2837f78a91cdSjjc 	uint32_t	base_lo;
2838f78a91cdSjjc 	uint32_t	limit_hi;
2839f78a91cdSjjc 	uint32_t	limit_lo;
28407c478bd9Sstevel@tonic-gate } opt_dram_addr_map_t;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate /*
2844f78a91cdSjjc  * Supported AMD processor families
2845f78a91cdSjjc  */
2846f78a91cdSjjc #define	AMD_FAMILY_HAMMER	15
2847f78a91cdSjjc #define	AMD_FAMILY_GREYHOUND	16
28487c478bd9Sstevel@tonic-gate 
2849f78a91cdSjjc /*
28502e2c009bSjjc  * Whether to have is_opteron() return 1 even when processor isn't supported
2851f78a91cdSjjc  */
2852f78a91cdSjjc uint_t	is_opteron_override = 0;
2853f78a91cdSjjc 
2854f78a91cdSjjc /*
2855f78a91cdSjjc  * AMD processor family for current CPU
2856f78a91cdSjjc  */
28577c478bd9Sstevel@tonic-gate uint_t	opt_family = 0;
2858f78a91cdSjjc 
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate /*
2861f78a91cdSjjc  * Determine whether we're running on a supported AMD Opteron since reading
2862f78a91cdSjjc  * node count and DRAM address map registers may have different format or
28632e2c009bSjjc  * may not be supported across processor families
28647c478bd9Sstevel@tonic-gate  */
28652e2c009bSjjc static int
28667c478bd9Sstevel@tonic-gate is_opteron(void)
28677c478bd9Sstevel@tonic-gate {
2868f78a91cdSjjc 
28697c478bd9Sstevel@tonic-gate 	if (x86_vendor != X86_VENDOR_AMD)
28707c478bd9Sstevel@tonic-gate 		return (0);
28717c478bd9Sstevel@tonic-gate 
2872f78a91cdSjjc 	opt_family = cpuid_getfamily(CPU);
2873f78a91cdSjjc 	if (opt_family == AMD_FAMILY_HAMMER ||
2874f78a91cdSjjc 	    opt_family == AMD_FAMILY_GREYHOUND || is_opteron_override)
28757c478bd9Sstevel@tonic-gate 		return (1);
28767c478bd9Sstevel@tonic-gate 	else
28777c478bd9Sstevel@tonic-gate 		return (0);
28787c478bd9Sstevel@tonic-gate }
28797c478bd9Sstevel@tonic-gate 
28802e2c009bSjjc 
28812e2c009bSjjc /*
28822e2c009bSjjc  * Determine NUMA configuration for Opteron from registers that live in PCI
28832e2c009bSjjc  * configuration space
28842e2c009bSjjc  */
28852e2c009bSjjc static void
28862e2c009bSjjc opt_get_numa_config(uint_t *node_cnt, int *mem_intrlv,
28872e2c009bSjjc     node_phys_addr_map_t *node_memory)
28887c478bd9Sstevel@tonic-gate {
28897c478bd9Sstevel@tonic-gate 	uint_t				bus;
28907c478bd9Sstevel@tonic-gate 	uint_t				dev;
28912e2c009bSjjc 	struct opt_dram_addr_map	dram_map[MAX_NODES];
28927c478bd9Sstevel@tonic-gate 	uint_t				node;
28932e2c009bSjjc 	uint_t				node_info[MAX_NODES];
2894f78a91cdSjjc 	uint_t				off_hi;
2895f78a91cdSjjc 	uint_t				off_lo;
2896f78a91cdSjjc 	uint64_t			nb_cfg_reg;
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	/*
28997c478bd9Sstevel@tonic-gate 	 * Read configuration registers from PCI configuration space to
29007c478bd9Sstevel@tonic-gate 	 * determine node information, which memory is in each node, etc.
29017c478bd9Sstevel@tonic-gate 	 *
29027c478bd9Sstevel@tonic-gate 	 * Write to PCI configuration space address register to specify
29037c478bd9Sstevel@tonic-gate 	 * which configuration register to read and read/write PCI
29047c478bd9Sstevel@tonic-gate 	 * configuration space data register to get/set contents
29057c478bd9Sstevel@tonic-gate 	 */
29067c478bd9Sstevel@tonic-gate 	bus = OPT_PCS_BUS_CONFIG;
29077c478bd9Sstevel@tonic-gate 	dev = OPT_PCS_DEV_NODE0;
2908f78a91cdSjjc 	off_hi = OPT_PCS_OFF_DRAMBASE_HI;
2909f78a91cdSjjc 	off_lo = OPT_PCS_OFF_DRAMBASE_LO;
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	/*
29127c478bd9Sstevel@tonic-gate 	 * Read node ID register for node 0 to get node count
29137c478bd9Sstevel@tonic-gate 	 */
29142e2c009bSjjc 	node_info[0] = pci_getl_func(bus, dev, OPT_PCS_FUNC_HT,
2915ef50d8c0Sesaxe 	    OPT_PCS_OFF_NODEID);
29162e2c009bSjjc 	*node_cnt = OPT_NODE_CNT(node_info[0]) + 1;
29172e2c009bSjjc 
29182e2c009bSjjc 	/*
29192e2c009bSjjc 	 * If number of nodes is more than maximum supported, then set node
29202e2c009bSjjc 	 * count to 1 and treat system as UMA instead of NUMA.
29212e2c009bSjjc 	 */
29222e2c009bSjjc 	if (*node_cnt > MAX_NODES) {
29232e2c009bSjjc 		*node_cnt = 1;
29242e2c009bSjjc 		return;
29252e2c009bSjjc 	}
29267c478bd9Sstevel@tonic-gate 
2927f78a91cdSjjc 	/*
2928f78a91cdSjjc 	 * For Greyhound, PCI Extended Configuration Space must be enabled to
2929f78a91cdSjjc 	 * read high DRAM address map base and limit registers
2930f78a91cdSjjc 	 */
2931f78a91cdSjjc 	if (opt_family == AMD_FAMILY_GREYHOUND) {
2932f78a91cdSjjc 		nb_cfg_reg = rdmsr(MSR_AMD_NB_CFG);
2933f78a91cdSjjc 		if ((nb_cfg_reg & AMD_GH_NB_CFG_EN_ECS) == 0)
2934f78a91cdSjjc 			wrmsr(MSR_AMD_NB_CFG,
2935f78a91cdSjjc 			    nb_cfg_reg | AMD_GH_NB_CFG_EN_ECS);
2936f78a91cdSjjc 	}
2937f78a91cdSjjc 
29382e2c009bSjjc 	for (node = 0; node < *node_cnt; node++) {
2939f78a91cdSjjc 		uint32_t	base_hi;
2940f78a91cdSjjc 		uint32_t	base_lo;
2941f78a91cdSjjc 		uint32_t	limit_hi;
2942f78a91cdSjjc 		uint32_t	limit_lo;
2943f78a91cdSjjc 
29447c478bd9Sstevel@tonic-gate 		/*
29457c478bd9Sstevel@tonic-gate 		 * Read node ID register (except for node 0 which we just read)
29467c478bd9Sstevel@tonic-gate 		 */
29477c478bd9Sstevel@tonic-gate 		if (node > 0) {
29482e2c009bSjjc 			node_info[node] = pci_getl_func(bus, dev,
2949ef50d8c0Sesaxe 			    OPT_PCS_FUNC_HT, OPT_PCS_OFF_NODEID);
29507c478bd9Sstevel@tonic-gate 		}
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 		/*
29537c478bd9Sstevel@tonic-gate 		 * Read DRAM base and limit registers which specify
29547c478bd9Sstevel@tonic-gate 		 * physical memory range of each node
29557c478bd9Sstevel@tonic-gate 		 */
2956f78a91cdSjjc 		if (opt_family != AMD_FAMILY_GREYHOUND)
2957f78a91cdSjjc 			base_hi = 0;
2958f78a91cdSjjc 		else {
2959f78a91cdSjjc 			outl(PCI_CONFADD, OPT_PCI_ECS_ADDR(bus, dev,
2960f78a91cdSjjc 			    OPT_PCS_FUNC_ADDRMAP, off_hi));
29612e2c009bSjjc 			base_hi = dram_map[node].base_hi =
2962f78a91cdSjjc 			    inl(PCI_CONFDATA);
2963f78a91cdSjjc 		}
29642e2c009bSjjc 		base_lo = dram_map[node].base_lo = pci_getl_func(bus, dev,
2965f78a91cdSjjc 		    OPT_PCS_FUNC_ADDRMAP, off_lo);
2966f78a91cdSjjc 
29672e2c009bSjjc 		if ((dram_map[node].base_lo & OPT_DRAMBASE_LO_MASK_INTRLVEN) &&
29682e2c009bSjjc 		    mem_intrlv)
29692e2c009bSjjc 			*mem_intrlv = *mem_intrlv + 1;
29707c478bd9Sstevel@tonic-gate 
2971f78a91cdSjjc 		off_hi += 4;	/* high limit register offset */
2972f78a91cdSjjc 		if (opt_family != AMD_FAMILY_GREYHOUND)
2973f78a91cdSjjc 			limit_hi = 0;
2974f78a91cdSjjc 		else {
2975f78a91cdSjjc 			outl(PCI_CONFADD, OPT_PCI_ECS_ADDR(bus, dev,
2976f78a91cdSjjc 			    OPT_PCS_FUNC_ADDRMAP, off_hi));
29772e2c009bSjjc 			limit_hi = dram_map[node].limit_hi =
2978f78a91cdSjjc 			    inl(PCI_CONFDATA);
2979f78a91cdSjjc 		}
2980f78a91cdSjjc 
2981f78a91cdSjjc 		off_lo += 4;	/* low limit register offset */
29822e2c009bSjjc 		limit_lo = dram_map[node].limit_lo = pci_getl_func(bus,
2983f78a91cdSjjc 		    dev, OPT_PCS_FUNC_ADDRMAP, off_lo);
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 		/*
2986f78a91cdSjjc 		 * Increment device number to next node and register offsets
2987f78a91cdSjjc 		 * for DRAM base register of next node
29887c478bd9Sstevel@tonic-gate 		 */
2989f78a91cdSjjc 		off_hi += 4;
2990f78a91cdSjjc 		off_lo += 4;
29917c478bd9Sstevel@tonic-gate 		dev++;
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 		/*
2994a940d195Sjjc 		 * Both read and write enable bits must be enabled in DRAM
2995a940d195Sjjc 		 * address map base register for physical memory to exist in
2996a940d195Sjjc 		 * node
2997a940d195Sjjc 		 */
2998f78a91cdSjjc 		if ((base_lo & OPT_DRAMBASE_LO_MASK_RE) == 0 ||
2999f78a91cdSjjc 		    (base_lo & OPT_DRAMBASE_LO_MASK_WE) == 0) {
3000a940d195Sjjc 			/*
3001a940d195Sjjc 			 * Mark node memory as non-existent and set start and
30022e2c009bSjjc 			 * end addresses to be same in node_memory[]
3003a940d195Sjjc 			 */
30042e2c009bSjjc 			node_memory[node].exists = 0;
30052e2c009bSjjc 			node_memory[node].start = node_memory[node].end =
30062e2c009bSjjc 			    (pfn_t)-1;
3007a940d195Sjjc 			continue;
3008a940d195Sjjc 		}
3009a940d195Sjjc 
3010a940d195Sjjc 		/*
3011a940d195Sjjc 		 * Mark node memory as existing and remember physical address
3012a940d195Sjjc 		 * range of each node for use later
30137c478bd9Sstevel@tonic-gate 		 */
30142e2c009bSjjc 		node_memory[node].exists = 1;
3015f78a91cdSjjc 
30162e2c009bSjjc 		node_memory[node].start = btop(OPT_DRAMADDR(base_hi, base_lo));
3017f78a91cdSjjc 
30182e2c009bSjjc 		node_memory[node].end = btop(OPT_DRAMADDR(limit_hi, limit_lo) |
3019f78a91cdSjjc 		    OPT_DRAMADDR_LO_MASK_OFF);
3020f78a91cdSjjc 	}
3021f78a91cdSjjc 
3022f78a91cdSjjc 	/*
3023f78a91cdSjjc 	 * Restore PCI Extended Configuration Space enable bit
3024f78a91cdSjjc 	 */
3025f78a91cdSjjc 	if (opt_family == AMD_FAMILY_GREYHOUND) {
3026f78a91cdSjjc 		if ((nb_cfg_reg & AMD_GH_NB_CFG_EN_ECS) == 0)
3027f78a91cdSjjc 			wrmsr(MSR_AMD_NB_CFG, nb_cfg_reg);
30287c478bd9Sstevel@tonic-gate 	}
30297c478bd9Sstevel@tonic-gate }
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate /*
30332e2c009bSjjc  * Return average amount of time to read vendor ID register on Northbridge
30342e2c009bSjjc  * N times on specified destination node from current CPU
30357c478bd9Sstevel@tonic-gate  */
30367c478bd9Sstevel@tonic-gate static hrtime_t
30372e2c009bSjjc opt_probe_vendor(int dest_node, int nreads)
30387c478bd9Sstevel@tonic-gate {
30392e2c009bSjjc 	int		cnt;
30407c478bd9Sstevel@tonic-gate 	uint_t		dev;
30417c478bd9Sstevel@tonic-gate 	/* LINTED: set but not used in function */
30427c478bd9Sstevel@tonic-gate 	volatile uint_t	dev_vendor;
30437c478bd9Sstevel@tonic-gate 	hrtime_t	elapsed;
30447c478bd9Sstevel@tonic-gate 	hrtime_t	end;
30457c478bd9Sstevel@tonic-gate 	int		ipl;
30467c478bd9Sstevel@tonic-gate 	hrtime_t	start;
30477c478bd9Sstevel@tonic-gate 
30482e2c009bSjjc 	dev = OPT_PCS_DEV_NODE0 + dest_node;
30497c478bd9Sstevel@tonic-gate 	kpreempt_disable();
30507c478bd9Sstevel@tonic-gate 	ipl = spl8();
30512e2c009bSjjc 	outl(PCI_CONFADD, PCI_CADDR1(0, dev, OPT_PCS_FUNC_DRAM,
30527c478bd9Sstevel@tonic-gate 	    OPT_PCS_OFF_VENDOR));
30537c478bd9Sstevel@tonic-gate 	start = gethrtime();
30542e2c009bSjjc 	for (cnt = 0; cnt < nreads; cnt++)
30557c478bd9Sstevel@tonic-gate 		dev_vendor = inl(PCI_CONFDATA);
30567c478bd9Sstevel@tonic-gate 	end = gethrtime();
30572e2c009bSjjc 	elapsed = (end - start) / nreads;
30587c478bd9Sstevel@tonic-gate 	splx(ipl);
30597c478bd9Sstevel@tonic-gate 	kpreempt_enable();
30602e2c009bSjjc 	return (elapsed);
30617c478bd9Sstevel@tonic-gate }
3062