17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5c39996a7Sstevel * Common Development and Distribution License (the "License"). 6c39996a7Sstevel * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21c39996a7Sstevel 227c478bd9Sstevel@tonic-gate /* 23472714d6Skchow * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate 302e2c009bSjjc /* 312e2c009bSjjc * LOCALITY GROUP (LGROUP) PLATFORM SUPPORT FOR X86/AMD64 PLATFORMS 322e2c009bSjjc * ================================================================ 332e2c009bSjjc * Multiprocessor AMD and Intel systems may have Non Uniform Memory Access 342e2c009bSjjc * (NUMA). A NUMA machine consists of one or more "nodes" that each consist of 352e2c009bSjjc * one or more CPUs and some local memory. The CPUs in each node can access 362e2c009bSjjc * the memory in the other nodes but at a higher latency than accessing their 372e2c009bSjjc * local memory. Typically, a system with only one node has Uniform Memory 382e2c009bSjjc * Access (UMA), but it may be possible to have a one node system that has 392e2c009bSjjc * some global memory outside of the node which is higher latency. 402e2c009bSjjc * 412e2c009bSjjc * Module Description 422e2c009bSjjc * ------------------ 432e2c009bSjjc * This module provides a platform interface for determining which CPUs and 442e2c009bSjjc * which memory (and how much) are in a NUMA node and how far each node is from 452e2c009bSjjc * each other. The interface is used by the Virtual Memory (VM) system and the 462e2c009bSjjc * common lgroup framework. The VM system uses the plat_*() routines to fill 472e2c009bSjjc * in its memory node (memnode) array with the physical address range spanned 482e2c009bSjjc * by each NUMA node to know which memory belongs to which node, so it can 492e2c009bSjjc * build and manage a physical page free list for each NUMA node and allocate 502e2c009bSjjc * local memory from each node as needed. The common lgroup framework uses the 512e2c009bSjjc * exported lgrp_plat_*() routines to figure out which CPUs and memory belong 522e2c009bSjjc * to each node (leaf lgroup) and how far each node is from each other, so it 532e2c009bSjjc * can build the latency (lgroup) topology for the machine in order to optimize 542e2c009bSjjc * for locality. Also, an lgroup platform handle instead of lgroups are used 552e2c009bSjjc * in the interface with this module, so this module shouldn't need to know 562e2c009bSjjc * anything about lgroups. Instead, it just needs to know which CPUs, memory, 572e2c009bSjjc * etc. are in each NUMA node, how far each node is from each other, and to use 582e2c009bSjjc * a unique lgroup platform handle to refer to each node through the interface. 592e2c009bSjjc * 602e2c009bSjjc * Determining NUMA Configuration 612e2c009bSjjc * ------------------------------ 622e2c009bSjjc * By default, this module will try to determine the NUMA configuration of the 632e2c009bSjjc * machine by reading the ACPI System Resource Affinity Table (SRAT) and System 642e2c009bSjjc * Locality Information Table (SLIT). The SRAT contains info to tell which 652e2c009bSjjc * CPUs and memory are local to a given proximity domain (NUMA node). The SLIT 662e2c009bSjjc * is a matrix that gives the distance between each system locality (which is 672e2c009bSjjc * a NUMA node and should correspond to proximity domains in the SRAT). For 682e2c009bSjjc * more details on the SRAT and SLIT, please refer to an ACPI 3.0 or newer 692e2c009bSjjc * specification. 702e2c009bSjjc * 712e2c009bSjjc * If the SRAT doesn't exist on a system with AMD Opteron processors, we 722e2c009bSjjc * examine registers in PCI configuration space to determine how many nodes are 732e2c009bSjjc * in the system and which CPUs and memory are in each node. 742e2c009bSjjc * do while booting the kernel. 752e2c009bSjjc * 762e2c009bSjjc * NOTE: Using these PCI configuration space registers to determine this 772e2c009bSjjc * locality info is not guaranteed to work or be compatible across all 782e2c009bSjjc * Opteron processor families. 792e2c009bSjjc * 802e2c009bSjjc * If the SLIT does not exist or look right, the kernel will probe to determine 812e2c009bSjjc * the distance between nodes as long as the NUMA CPU and memory configuration 822e2c009bSjjc * has been determined (see lgrp_plat_probe() for details). 832e2c009bSjjc * 842e2c009bSjjc * Data Structures 852e2c009bSjjc * --------------- 862e2c009bSjjc * The main data structures used by this code are the following: 872e2c009bSjjc * 882e2c009bSjjc * - lgrp_plat_cpu_node[] APIC ID to node ID mapping table 892e2c009bSjjc * indexed by hashed APIC ID (only used 902e2c009bSjjc * for SRAT) 912e2c009bSjjc * 922e2c009bSjjc * - lgrp_plat_lat_stats.latencies[][] Table of latencies between same and 932e2c009bSjjc * different nodes indexed by node ID 942e2c009bSjjc * 952e2c009bSjjc * - lgrp_plat_node_cnt Number of NUMA nodes in system 962e2c009bSjjc * 972e2c009bSjjc * - lgrp_plat_node_domain[] Node ID to proximity domain ID mapping 982e2c009bSjjc * table indexed by node ID (only used 992e2c009bSjjc * for SRAT) 1002e2c009bSjjc * 1012e2c009bSjjc * - lgrp_plat_node_memory[] Table with physical address range for 1022e2c009bSjjc * each node indexed by node ID 1032e2c009bSjjc * 1042e2c009bSjjc * The code is implemented to make the following always be true: 1052e2c009bSjjc * 1062e2c009bSjjc * lgroup platform handle == node ID == memnode ID 1072e2c009bSjjc * 1082e2c009bSjjc * Moreover, it allows for the proximity domain ID to be equal to all of the 1092e2c009bSjjc * above as long as the proximity domains IDs are numbered from 0 to <number of 1102e2c009bSjjc * nodes - 1>. This is done by hashing each proximity domain ID into the range 1112e2c009bSjjc * from 0 to <number of nodes - 1>. Then proximity ID N will hash into node ID 1122e2c009bSjjc * N and proximity domain ID N will be entered into lgrp_plat_node_domain[N] 1132e2c009bSjjc * and be assigned node ID N. If the proximity domain IDs aren't numbered 1142e2c009bSjjc * from 0 to <number of nodes - 1>, then hashing the proximity domain IDs into 1152e2c009bSjjc * lgrp_plat_node_domain[] will still work for assigning proximity domain IDs 1162e2c009bSjjc * to node IDs. However, the proximity domain IDs may not map to the 1172e2c009bSjjc * equivalent node ID since we want to keep the node IDs numbered from 0 to 1182e2c009bSjjc * <number of nodes - 1> to minimize cost of searching and potentially space. 1192e2c009bSjjc */ 1202e2c009bSjjc 1212e2c009bSjjc 1227c478bd9Sstevel@tonic-gate #include <sys/archsystm.h> /* for {in,out}{b,w,l}() */ 1237c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 124f78a91cdSjjc #include <sys/controlregs.h> 1257c478bd9Sstevel@tonic-gate #include <sys/cpupart.h> 1267c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 1277c478bd9Sstevel@tonic-gate #include <sys/lgrp.h> 1287c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 1297c478bd9Sstevel@tonic-gate #include <sys/memlist.h> 1307c478bd9Sstevel@tonic-gate #include <sys/memnode.h> 1317c478bd9Sstevel@tonic-gate #include <sys/mman.h> 132ef50d8c0Sesaxe #include <sys/pci_cfgspace.h> 133ef50d8c0Sesaxe #include <sys/pci_impl.h> 1347c478bd9Sstevel@tonic-gate #include <sys/param.h> 135fb2f18f8Sesaxe #include <sys/pghw.h> 1367c478bd9Sstevel@tonic-gate #include <sys/promif.h> /* for prom_printf() */ 1372e2c009bSjjc #include <sys/sysmacros.h> 1387c478bd9Sstevel@tonic-gate #include <sys/systm.h> 1397c478bd9Sstevel@tonic-gate #include <sys/thread.h> 1407c478bd9Sstevel@tonic-gate #include <sys/types.h> 1417c478bd9Sstevel@tonic-gate #include <sys/var.h> 1427c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h> /* for x86_feature and X86_AMD */ 1437c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h> 1447c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h> 145affbd3ccSkchow #include <vm/vm_dep.h> 1467c478bd9Sstevel@tonic-gate 1472e2c009bSjjc #include "acpi_fw.h" /* for SRAT and SLIT */ 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate #define MAX_NODES 8 1517c478bd9Sstevel@tonic-gate #define NLGRP (MAX_NODES * (MAX_NODES - 1) + 1) 1527c478bd9Sstevel@tonic-gate 1532e2c009bSjjc /* 1542e2c009bSjjc * Constants for configuring probing 1552e2c009bSjjc */ 1567c478bd9Sstevel@tonic-gate #define LGRP_PLAT_PROBE_NROUNDS 64 /* default laps for probing */ 1577c478bd9Sstevel@tonic-gate #define LGRP_PLAT_PROBE_NSAMPLES 1 /* default samples to take */ 1588949bcd6Sandrei #define LGRP_PLAT_PROBE_NREADS 256 /* number of vendor ID reads */ 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* 1612e2c009bSjjc * Flags for probing 1622e2c009bSjjc */ 1632e2c009bSjjc #define LGRP_PLAT_PROBE_ENABLE 0x1 /* enable probing */ 1642e2c009bSjjc #define LGRP_PLAT_PROBE_PGCPY 0x2 /* probe using page copy */ 1652e2c009bSjjc #define LGRP_PLAT_PROBE_VENDOR 0x4 /* probe vendor ID register */ 1662e2c009bSjjc 1672e2c009bSjjc /* 1682e2c009bSjjc * Hash CPU APIC ID into CPU to node mapping table using max_ncpus 1692e2c009bSjjc * to minimize span of entries used 1702e2c009bSjjc */ 1712e2c009bSjjc #define CPU_NODE_HASH(apicid) ((apicid) % max_ncpus) 1722e2c009bSjjc 1732e2c009bSjjc /* 1742e2c009bSjjc * Hash proximity domain ID into node to domain mapping table using to minimize 1752e2c009bSjjc * span of entries used 1762e2c009bSjjc */ 1772e2c009bSjjc #define NODE_DOMAIN_HASH(domain) ((domain) % lgrp_plat_node_cnt) 1782e2c009bSjjc 1792e2c009bSjjc 1802e2c009bSjjc /* 1812e2c009bSjjc * CPU APIC ID to node ID mapping structure (only used with SRAT) 1822e2c009bSjjc */ 1832e2c009bSjjc typedef struct cpu_node_map { 1842e2c009bSjjc int exists; 1852e2c009bSjjc uint_t node; 1862e2c009bSjjc uint32_t apicid; 1872e2c009bSjjc uint32_t prox_domain; 1882e2c009bSjjc } cpu_node_map_t; 1892e2c009bSjjc 1902e2c009bSjjc /* 1912e2c009bSjjc * Latency statistics 1922e2c009bSjjc */ 1932e2c009bSjjc typedef struct lgrp_plat_latency_stats { 1942e2c009bSjjc hrtime_t latencies[MAX_NODES][MAX_NODES]; 1952e2c009bSjjc hrtime_t latency_max; 1962e2c009bSjjc hrtime_t latency_min; 1972e2c009bSjjc } lgrp_plat_latency_stats_t; 1982e2c009bSjjc 1992e2c009bSjjc /* 2002e2c009bSjjc * Memory configuration for probing 2012e2c009bSjjc */ 2022e2c009bSjjc typedef struct lgrp_plat_probe_mem_config { 2032e2c009bSjjc size_t probe_memsize; /* how much memory to probe per node */ 2042e2c009bSjjc caddr_t probe_va[MAX_NODES]; /* where memory mapped for probing */ 2052e2c009bSjjc pfn_t probe_pfn[MAX_NODES]; /* physical pages to map for probing */ 2062e2c009bSjjc } lgrp_plat_probe_mem_config_t; 2072e2c009bSjjc 2082e2c009bSjjc /* 2092e2c009bSjjc * Statistics kept for probing 2102e2c009bSjjc */ 2112e2c009bSjjc typedef struct lgrp_plat_probe_stats { 2122e2c009bSjjc hrtime_t flush_cost; 2132e2c009bSjjc hrtime_t probe_cost; 2142e2c009bSjjc hrtime_t probe_cost_total; 2152e2c009bSjjc hrtime_t probe_error_code; 2162e2c009bSjjc hrtime_t probe_errors[MAX_NODES][MAX_NODES]; 2172e2c009bSjjc int probe_suspect[MAX_NODES][MAX_NODES]; 2182e2c009bSjjc hrtime_t probe_max[MAX_NODES][MAX_NODES]; 2192e2c009bSjjc hrtime_t probe_min[MAX_NODES][MAX_NODES]; 2202e2c009bSjjc } lgrp_plat_probe_stats_t; 2212e2c009bSjjc 2222e2c009bSjjc /* 2232e2c009bSjjc * Node to proximity domain ID mapping structure (only used with SRAT) 2242e2c009bSjjc */ 2252e2c009bSjjc typedef struct node_domain_map { 2262e2c009bSjjc int exists; 2272e2c009bSjjc uint32_t prox_domain; 2282e2c009bSjjc } node_domain_map_t; 2292e2c009bSjjc 2302e2c009bSjjc /* 2312e2c009bSjjc * Node ID and starting and ending page for physical memory in node 2322e2c009bSjjc */ 2332e2c009bSjjc typedef struct node_phys_addr_map { 2342e2c009bSjjc pfn_t start; 2352e2c009bSjjc pfn_t end; 2362e2c009bSjjc int exists; 2372e2c009bSjjc uint32_t prox_domain; 2382e2c009bSjjc } node_phys_addr_map_t; 2392e2c009bSjjc 2402e2c009bSjjc 2412e2c009bSjjc /* 2422e2c009bSjjc * CPU APIC ID to node ID mapping table (only used for SRAT) 2432e2c009bSjjc */ 2442e2c009bSjjc static cpu_node_map_t lgrp_plat_cpu_node[NCPU]; 2452e2c009bSjjc 2462e2c009bSjjc /* 2472e2c009bSjjc * Latency statistics 2482e2c009bSjjc */ 2492e2c009bSjjc lgrp_plat_latency_stats_t lgrp_plat_lat_stats; 2502e2c009bSjjc 2512e2c009bSjjc /* 2522e2c009bSjjc * Whether memory is interleaved across nodes causing MPO to be disabled 2532e2c009bSjjc */ 2542e2c009bSjjc static int lgrp_plat_mem_intrlv = 0; 2552e2c009bSjjc 2562e2c009bSjjc /* 2572e2c009bSjjc * Node ID to proximity domain ID mapping table (only used for SRAT) 2582e2c009bSjjc */ 2592e2c009bSjjc static node_domain_map_t lgrp_plat_node_domain[MAX_NODES]; 2602e2c009bSjjc 2612e2c009bSjjc /* 2622e2c009bSjjc * Physical address range for memory in each node 2632e2c009bSjjc */ 2642e2c009bSjjc static node_phys_addr_map_t lgrp_plat_node_memory[MAX_NODES]; 2652e2c009bSjjc 2662e2c009bSjjc /* 2672e2c009bSjjc * Statistics gotten from probing 2682e2c009bSjjc */ 2692e2c009bSjjc static lgrp_plat_probe_stats_t lgrp_plat_probe_stats; 2702e2c009bSjjc 2712e2c009bSjjc /* 2722e2c009bSjjc * Memory configuration for probing 2732e2c009bSjjc */ 2742e2c009bSjjc static lgrp_plat_probe_mem_config_t lgrp_plat_probe_mem_config; 2752e2c009bSjjc 2762e2c009bSjjc /* 2772e2c009bSjjc * Error code from processing ACPI SRAT 2782e2c009bSjjc */ 2792e2c009bSjjc static int lgrp_plat_srat_error = 0; 2802e2c009bSjjc 2812e2c009bSjjc /* 2822e2c009bSjjc * Error code from processing ACPI SLIT 2832e2c009bSjjc */ 2842e2c009bSjjc static int lgrp_plat_slit_error = 0; 2852e2c009bSjjc 2862e2c009bSjjc /* 2872e2c009bSjjc * Allocate lgroup array statically 2882e2c009bSjjc */ 2892e2c009bSjjc static lgrp_t lgrp_space[NLGRP]; 2902e2c009bSjjc static int nlgrps_alloc; 2912e2c009bSjjc 2922e2c009bSjjc 2932e2c009bSjjc /* 2942e2c009bSjjc * Number of nodes in system 2952e2c009bSjjc */ 2962e2c009bSjjc uint_t lgrp_plat_node_cnt = 1; 2972e2c009bSjjc 2982e2c009bSjjc /* 2992e2c009bSjjc * Configuration Parameters for Probing 3002e2c009bSjjc * - lgrp_plat_probe_flags Flags to specify enabling probing, probe 3012e2c009bSjjc * operation, etc. 3022e2c009bSjjc * - lgrp_plat_probe_nrounds How many rounds of probing to do 3032e2c009bSjjc * - lgrp_plat_probe_nsamples Number of samples to take when probing each 3042e2c009bSjjc * node 3052e2c009bSjjc * - lgrp_plat_probe_nreads Number of times to read vendor ID from 3062e2c009bSjjc * Northbridge for each probe 3072e2c009bSjjc */ 3082e2c009bSjjc uint_t lgrp_plat_probe_flags = 0; 3092e2c009bSjjc int lgrp_plat_probe_nrounds = LGRP_PLAT_PROBE_NROUNDS; 3102e2c009bSjjc int lgrp_plat_probe_nsamples = LGRP_PLAT_PROBE_NSAMPLES; 3112e2c009bSjjc int lgrp_plat_probe_nreads = LGRP_PLAT_PROBE_NREADS; 3122e2c009bSjjc 3132e2c009bSjjc /* 3142e2c009bSjjc * Enable use of ACPI System Resource Affinity Table (SRAT) and System 3152e2c009bSjjc * Locality Information Table (SLIT) 3162e2c009bSjjc */ 3172e2c009bSjjc int lgrp_plat_srat_enable = 1; 3182e2c009bSjjc int lgrp_plat_slit_enable = 1; 3192e2c009bSjjc 3202e2c009bSjjc /* 3212e2c009bSjjc * Static array to hold lgroup statistics 3222e2c009bSjjc */ 3232e2c009bSjjc struct lgrp_stats lgrp_stats[NLGRP]; 3242e2c009bSjjc 3252e2c009bSjjc 3262e2c009bSjjc /* 3272e2c009bSjjc * Forward declarations of platform interface routines 3282e2c009bSjjc */ 3292e2c009bSjjc void plat_build_mem_nodes(struct memlist *list); 3302e2c009bSjjc 3312e2c009bSjjc int plat_lgrphand_to_mem_node(lgrp_handle_t hand); 3322e2c009bSjjc 3332e2c009bSjjc lgrp_handle_t plat_mem_node_to_lgrphand(int mnode); 3342e2c009bSjjc 3352e2c009bSjjc int plat_mnode_xcheck(pfn_t pfncnt); 3362e2c009bSjjc 3372e2c009bSjjc int plat_pfn_to_mem_node(pfn_t pfn); 3382e2c009bSjjc 3392e2c009bSjjc /* 3402e2c009bSjjc * Forward declarations of lgroup platform interface routines 3412e2c009bSjjc */ 3422e2c009bSjjc lgrp_t *lgrp_plat_alloc(lgrp_id_t lgrpid); 3432e2c009bSjjc 3442e2c009bSjjc void lgrp_plat_config(lgrp_config_flag_t flag, uintptr_t arg); 3452e2c009bSjjc 3462e2c009bSjjc lgrp_handle_t lgrp_plat_cpu_to_hand(processorid_t id); 3472e2c009bSjjc 3482e2c009bSjjc void lgrp_plat_init(void); 3492e2c009bSjjc 3502e2c009bSjjc int lgrp_plat_latency(lgrp_handle_t from, lgrp_handle_t to); 3512e2c009bSjjc 3522e2c009bSjjc void lgrp_plat_main_init(void); 3532e2c009bSjjc 3542e2c009bSjjc int lgrp_plat_max_lgrps(void); 3552e2c009bSjjc 3562e2c009bSjjc pgcnt_t lgrp_plat_mem_size(lgrp_handle_t plathand, 3572e2c009bSjjc lgrp_mem_query_t query); 3582e2c009bSjjc 3592e2c009bSjjc lgrp_handle_t lgrp_plat_pfn_to_hand(pfn_t pfn); 3602e2c009bSjjc 3612e2c009bSjjc void lgrp_plat_probe(void); 3622e2c009bSjjc 3632e2c009bSjjc lgrp_handle_t lgrp_plat_root_hand(void); 3642e2c009bSjjc 3652e2c009bSjjc 3662e2c009bSjjc /* 3672e2c009bSjjc * Forward declarations of local routines 3682e2c009bSjjc */ 3692e2c009bSjjc static int is_opteron(void); 3702e2c009bSjjc 3712e2c009bSjjc static int lgrp_plat_cpu_to_node(cpu_t *cp, cpu_node_map_t *cpu_node); 3722e2c009bSjjc 3732e2c009bSjjc static int lgrp_plat_domain_to_node(node_domain_map_t *node_domain, 3742e2c009bSjjc uint32_t domain); 3752e2c009bSjjc 3762e2c009bSjjc static void lgrp_plat_latency_adjust(node_phys_addr_map_t *node_memory, 3772e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats, 3782e2c009bSjjc lgrp_plat_probe_stats_t *probe_stats); 3792e2c009bSjjc 3802e2c009bSjjc static int lgrp_plat_latency_verify(node_phys_addr_map_t *node_memory, 3812e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats); 3822e2c009bSjjc 3832e2c009bSjjc static pgcnt_t lgrp_plat_mem_size_default(lgrp_handle_t, lgrp_mem_query_t); 3842e2c009bSjjc 3852e2c009bSjjc static int lgrp_plat_node_domain_update(node_domain_map_t *node_domain, 3862e2c009bSjjc uint32_t domain); 3872e2c009bSjjc 3882e2c009bSjjc static int lgrp_plat_node_memory_update(node_domain_map_t *node_domain, 389*e9dd3ea3Sjjc node_phys_addr_map_t *node_memory, uint64_t start, uint64_t end, 3902e2c009bSjjc uint32_t domain); 3912e2c009bSjjc 3922e2c009bSjjc static hrtime_t lgrp_plat_probe_time(int to, cpu_node_map_t *cpu_node, 3932e2c009bSjjc lgrp_plat_probe_mem_config_t *probe_mem_config, 3942e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats, 3952e2c009bSjjc lgrp_plat_probe_stats_t *probe_stats); 3962e2c009bSjjc 3972e2c009bSjjc static int lgrp_plat_process_slit(struct slit *tp, uint_t node_cnt, 3982e2c009bSjjc node_phys_addr_map_t *node_memory, lgrp_plat_latency_stats_t *lat_stats); 3992e2c009bSjjc 4002e2c009bSjjc static int lgrp_plat_process_srat(struct srat *tp, uint_t *node_cnt, 4012e2c009bSjjc node_domain_map_t *node_domain, cpu_node_map_t *cpu_node, 4022e2c009bSjjc node_phys_addr_map_t *node_memory); 4032e2c009bSjjc 4042e2c009bSjjc static int lgrp_plat_srat_domains(struct srat *tp); 4052e2c009bSjjc 4062e2c009bSjjc static void lgrp_plat_2level_setup(node_phys_addr_map_t *node_memory, 4072e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats); 4082e2c009bSjjc 4092e2c009bSjjc static void opt_get_numa_config(uint_t *node_cnt, int *mem_intrlv, 4102e2c009bSjjc node_phys_addr_map_t *node_memory); 4112e2c009bSjjc 4122e2c009bSjjc static hrtime_t opt_probe_vendor(int dest_node, int nreads); 4132e2c009bSjjc 4142e2c009bSjjc 4152e2c009bSjjc /* 4162e2c009bSjjc * PLATFORM INTERFACE ROUTINES 4177c478bd9Sstevel@tonic-gate */ 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate /* 4202e2c009bSjjc * Configure memory nodes for machines with more than one node (ie NUMA) 4212e2c009bSjjc */ 4222e2c009bSjjc void 4232e2c009bSjjc plat_build_mem_nodes(struct memlist *list) 4242e2c009bSjjc { 4252e2c009bSjjc pfn_t cur_start; /* start addr of subrange */ 4262e2c009bSjjc pfn_t cur_end; /* end addr of subrange */ 4272e2c009bSjjc pfn_t start; /* start addr of whole range */ 4282e2c009bSjjc pfn_t end; /* end addr of whole range */ 4292e2c009bSjjc 4302e2c009bSjjc /* 4312e2c009bSjjc * Boot install lists are arranged <addr, len>, ... 4322e2c009bSjjc */ 4332e2c009bSjjc while (list) { 4342e2c009bSjjc int node; 4352e2c009bSjjc 4362e2c009bSjjc start = list->address >> PAGESHIFT; 4372e2c009bSjjc end = (list->address + list->size - 1) >> PAGESHIFT; 4382e2c009bSjjc 4392e2c009bSjjc if (start > physmax) { 4402e2c009bSjjc list = list->next; 4412e2c009bSjjc continue; 4422e2c009bSjjc } 4432e2c009bSjjc if (end > physmax) 4442e2c009bSjjc end = physmax; 4452e2c009bSjjc 4462e2c009bSjjc /* 4472e2c009bSjjc * When there is only one memnode, just add memory to memnode 4482e2c009bSjjc */ 4492e2c009bSjjc if (max_mem_nodes == 1) { 4502e2c009bSjjc mem_node_add_slice(start, end); 4512e2c009bSjjc list = list->next; 4522e2c009bSjjc continue; 4532e2c009bSjjc } 4542e2c009bSjjc 4552e2c009bSjjc /* 4562e2c009bSjjc * mem_node_add_slice() expects to get a memory range that 4572e2c009bSjjc * is within one memnode, so need to split any memory range 4582e2c009bSjjc * that spans multiple memnodes into subranges that are each 4592e2c009bSjjc * contained within one memnode when feeding them to 4602e2c009bSjjc * mem_node_add_slice() 4612e2c009bSjjc */ 4622e2c009bSjjc cur_start = start; 4632e2c009bSjjc do { 4642e2c009bSjjc node = plat_pfn_to_mem_node(cur_start); 4652e2c009bSjjc 4662e2c009bSjjc /* 4672e2c009bSjjc * Panic if DRAM address map registers or SRAT say 4682e2c009bSjjc * memory in node doesn't exist or address from 4692e2c009bSjjc * boot installed memory list entry isn't in this node. 4702e2c009bSjjc * This shouldn't happen and rest of code can't deal 4712e2c009bSjjc * with this if it does. 4722e2c009bSjjc */ 4732e2c009bSjjc if (node < 0 || node >= lgrp_plat_node_cnt || 4742e2c009bSjjc !lgrp_plat_node_memory[node].exists || 4752e2c009bSjjc cur_start < lgrp_plat_node_memory[node].start || 4762e2c009bSjjc cur_start > lgrp_plat_node_memory[node].end) { 4772e2c009bSjjc cmn_err(CE_PANIC, "Don't know which memnode " 4782e2c009bSjjc "to add installed memory address 0x%lx\n", 4792e2c009bSjjc cur_start); 4802e2c009bSjjc } 4812e2c009bSjjc 4822e2c009bSjjc /* 4832e2c009bSjjc * End of current subrange should not span memnodes 4842e2c009bSjjc */ 4852e2c009bSjjc cur_end = end; 4862e2c009bSjjc if (lgrp_plat_node_memory[node].exists && 4872e2c009bSjjc cur_end > lgrp_plat_node_memory[node].end) 4882e2c009bSjjc cur_end = lgrp_plat_node_memory[node].end; 4892e2c009bSjjc 4902e2c009bSjjc mem_node_add_slice(cur_start, cur_end); 4912e2c009bSjjc 4922e2c009bSjjc /* 4932e2c009bSjjc * Next subrange starts after end of current one 4942e2c009bSjjc */ 4952e2c009bSjjc cur_start = cur_end + 1; 4962e2c009bSjjc } while (cur_end < end); 4972e2c009bSjjc 4982e2c009bSjjc list = list->next; 4992e2c009bSjjc } 5002e2c009bSjjc mem_node_physalign = 0; 5012e2c009bSjjc mem_node_pfn_shift = 0; 5022e2c009bSjjc } 5032e2c009bSjjc 5042e2c009bSjjc 5052e2c009bSjjc int 5062e2c009bSjjc plat_lgrphand_to_mem_node(lgrp_handle_t hand) 5072e2c009bSjjc { 5082e2c009bSjjc if (max_mem_nodes == 1) 5092e2c009bSjjc return (0); 5102e2c009bSjjc 5112e2c009bSjjc return ((int)hand); 5122e2c009bSjjc } 5132e2c009bSjjc 5142e2c009bSjjc 5152e2c009bSjjc /* 5162e2c009bSjjc * plat_mnode_xcheck: checks the node memory ranges to see if there is a pfncnt 5172e2c009bSjjc * range of pages aligned on pfncnt that crosses an node boundary. Returns 1 if 5182e2c009bSjjc * a crossing is found and returns 0 otherwise. 5192e2c009bSjjc */ 5202e2c009bSjjc int 5212e2c009bSjjc plat_mnode_xcheck(pfn_t pfncnt) 5222e2c009bSjjc { 5232e2c009bSjjc int node, prevnode = -1, basenode; 5242e2c009bSjjc pfn_t ea, sa; 5252e2c009bSjjc 5262e2c009bSjjc for (node = 0; node < lgrp_plat_node_cnt; node++) { 5272e2c009bSjjc 5282e2c009bSjjc if (lgrp_plat_node_memory[node].exists == 0) 5292e2c009bSjjc continue; 5302e2c009bSjjc 5312e2c009bSjjc if (prevnode == -1) { 5322e2c009bSjjc prevnode = node; 5332e2c009bSjjc basenode = node; 5342e2c009bSjjc continue; 5352e2c009bSjjc } 5362e2c009bSjjc 5372e2c009bSjjc /* assume x86 node pfn ranges are in increasing order */ 5382e2c009bSjjc ASSERT(lgrp_plat_node_memory[node].start > 5392e2c009bSjjc lgrp_plat_node_memory[prevnode].end); 5402e2c009bSjjc 5412e2c009bSjjc /* 5422e2c009bSjjc * continue if the starting address of node is not contiguous 5432e2c009bSjjc * with the previous node. 5442e2c009bSjjc */ 5452e2c009bSjjc 5462e2c009bSjjc if (lgrp_plat_node_memory[node].start != 5472e2c009bSjjc (lgrp_plat_node_memory[prevnode].end + 1)) { 5482e2c009bSjjc basenode = node; 5492e2c009bSjjc prevnode = node; 5502e2c009bSjjc continue; 5512e2c009bSjjc } 5522e2c009bSjjc 5532e2c009bSjjc /* check if the starting address of node is pfncnt aligned */ 5542e2c009bSjjc if ((lgrp_plat_node_memory[node].start & (pfncnt - 1)) != 0) { 5552e2c009bSjjc 5562e2c009bSjjc /* 5572e2c009bSjjc * at this point, node starts at an unaligned boundary 5582e2c009bSjjc * and is contiguous with the previous node(s) to 5592e2c009bSjjc * basenode. Check if there is an aligned contiguous 5602e2c009bSjjc * range of length pfncnt that crosses this boundary. 5612e2c009bSjjc */ 5622e2c009bSjjc 5632e2c009bSjjc sa = P2ALIGN(lgrp_plat_node_memory[prevnode].end, 5642e2c009bSjjc pfncnt); 5652e2c009bSjjc ea = P2ROUNDUP((lgrp_plat_node_memory[node].start), 5662e2c009bSjjc pfncnt); 5672e2c009bSjjc 5682e2c009bSjjc ASSERT((ea - sa) == pfncnt); 5692e2c009bSjjc if (sa >= lgrp_plat_node_memory[basenode].start && 5702e2c009bSjjc ea <= (lgrp_plat_node_memory[node].end + 1)) 5712e2c009bSjjc return (1); 5722e2c009bSjjc } 5732e2c009bSjjc prevnode = node; 5742e2c009bSjjc } 5752e2c009bSjjc return (0); 5762e2c009bSjjc } 5772e2c009bSjjc 5782e2c009bSjjc 5792e2c009bSjjc lgrp_handle_t 5802e2c009bSjjc plat_mem_node_to_lgrphand(int mnode) 5812e2c009bSjjc { 5822e2c009bSjjc if (max_mem_nodes == 1) 5832e2c009bSjjc return (LGRP_DEFAULT_HANDLE); 5842e2c009bSjjc 5852e2c009bSjjc return ((lgrp_handle_t)mnode); 5862e2c009bSjjc } 5872e2c009bSjjc 5882e2c009bSjjc 5892e2c009bSjjc int 5902e2c009bSjjc plat_pfn_to_mem_node(pfn_t pfn) 5912e2c009bSjjc { 5922e2c009bSjjc int node; 5932e2c009bSjjc 5942e2c009bSjjc if (max_mem_nodes == 1) 5952e2c009bSjjc return (0); 5962e2c009bSjjc 5972e2c009bSjjc for (node = 0; node < lgrp_plat_node_cnt; node++) { 5982e2c009bSjjc /* 5992e2c009bSjjc * Skip nodes with no memory 6002e2c009bSjjc */ 6012e2c009bSjjc if (!lgrp_plat_node_memory[node].exists) 6022e2c009bSjjc continue; 6032e2c009bSjjc 6042e2c009bSjjc if (pfn >= lgrp_plat_node_memory[node].start && 6052e2c009bSjjc pfn <= lgrp_plat_node_memory[node].end) 6062e2c009bSjjc return (node); 6072e2c009bSjjc } 6082e2c009bSjjc 6092e2c009bSjjc /* 6102e2c009bSjjc * Didn't find memnode where this PFN lives which should never happen 6112e2c009bSjjc */ 6122e2c009bSjjc ASSERT(node < lgrp_plat_node_cnt); 6132e2c009bSjjc return (-1); 6142e2c009bSjjc } 6152e2c009bSjjc 6162e2c009bSjjc 6172e2c009bSjjc /* 6182e2c009bSjjc * LGROUP PLATFORM INTERFACE ROUTINES 6192e2c009bSjjc */ 6202e2c009bSjjc 6212e2c009bSjjc /* 6222e2c009bSjjc * Allocate additional space for an lgroup. 6232e2c009bSjjc */ 6242e2c009bSjjc /* ARGSUSED */ 6252e2c009bSjjc lgrp_t * 6262e2c009bSjjc lgrp_plat_alloc(lgrp_id_t lgrpid) 6272e2c009bSjjc { 6282e2c009bSjjc lgrp_t *lgrp; 6292e2c009bSjjc 6302e2c009bSjjc lgrp = &lgrp_space[nlgrps_alloc++]; 6312e2c009bSjjc if (lgrpid >= NLGRP || nlgrps_alloc > NLGRP) 6322e2c009bSjjc return (NULL); 6332e2c009bSjjc return (lgrp); 6342e2c009bSjjc } 6352e2c009bSjjc 6362e2c009bSjjc 6372e2c009bSjjc /* 6382e2c009bSjjc * Platform handling for (re)configuration changes 6392e2c009bSjjc */ 6402e2c009bSjjc /* ARGSUSED */ 6412e2c009bSjjc void 6422e2c009bSjjc lgrp_plat_config(lgrp_config_flag_t flag, uintptr_t arg) 6432e2c009bSjjc { 6442e2c009bSjjc } 6452e2c009bSjjc 6462e2c009bSjjc 6472e2c009bSjjc /* 6482e2c009bSjjc * Return the platform handle for the lgroup containing the given CPU 6492e2c009bSjjc */ 6502e2c009bSjjc /* ARGSUSED */ 6512e2c009bSjjc lgrp_handle_t 6522e2c009bSjjc lgrp_plat_cpu_to_hand(processorid_t id) 6532e2c009bSjjc { 6542e2c009bSjjc lgrp_handle_t hand; 6552e2c009bSjjc 6562e2c009bSjjc if (lgrp_plat_node_cnt == 1) 6572e2c009bSjjc return (LGRP_DEFAULT_HANDLE); 6582e2c009bSjjc 6592e2c009bSjjc hand = (lgrp_handle_t)lgrp_plat_cpu_to_node(cpu[id], 6602e2c009bSjjc lgrp_plat_cpu_node); 6612e2c009bSjjc 6622e2c009bSjjc ASSERT(hand != (lgrp_handle_t)-1); 6632e2c009bSjjc if (hand == (lgrp_handle_t)-1) 6642e2c009bSjjc return (LGRP_NULL_HANDLE); 6652e2c009bSjjc 6662e2c009bSjjc return (hand); 6672e2c009bSjjc } 6682e2c009bSjjc 6692e2c009bSjjc 6702e2c009bSjjc /* 6712e2c009bSjjc * Platform-specific initialization of lgroups 6722e2c009bSjjc */ 6732e2c009bSjjc void 6742e2c009bSjjc lgrp_plat_init(void) 6752e2c009bSjjc { 6762e2c009bSjjc #if defined(__xpv) 6772e2c009bSjjc /* 6782e2c009bSjjc * XXPV For now, the hypervisor treats all memory equally. 6792e2c009bSjjc */ 6802e2c009bSjjc lgrp_plat_node_cnt = max_mem_nodes = 1; 6812e2c009bSjjc #else /* __xpv */ 6822e2c009bSjjc uint_t probe_op; 6832e2c009bSjjc 6842e2c009bSjjc /* 6852e2c009bSjjc * Initialize as a UMA machine 6862e2c009bSjjc */ 6872e2c009bSjjc if (lgrp_topo_ht_limit() == 1) { 6882e2c009bSjjc lgrp_plat_node_cnt = max_mem_nodes = 1; 6892e2c009bSjjc return; 6902e2c009bSjjc } 6912e2c009bSjjc 6922e2c009bSjjc /* 6932e2c009bSjjc * Determine which CPUs and memory are local to each other and number 6942e2c009bSjjc * of NUMA nodes by reading ACPI System Resource Affinity Table (SRAT) 6952e2c009bSjjc */ 6962e2c009bSjjc lgrp_plat_srat_error = lgrp_plat_process_srat(srat_ptr, 6972e2c009bSjjc &lgrp_plat_node_cnt, lgrp_plat_node_domain, lgrp_plat_cpu_node, 6982e2c009bSjjc lgrp_plat_node_memory); 6992e2c009bSjjc 7002e2c009bSjjc /* 7012e2c009bSjjc * Try to use PCI config space registers on Opteron if SRAT doesn't 7022e2c009bSjjc * exist or there is some error processing the SRAT 7032e2c009bSjjc */ 7042e2c009bSjjc if (lgrp_plat_srat_error != 0 && is_opteron()) 7052e2c009bSjjc opt_get_numa_config(&lgrp_plat_node_cnt, &lgrp_plat_mem_intrlv, 7062e2c009bSjjc lgrp_plat_node_memory); 7072e2c009bSjjc 7082e2c009bSjjc /* 7092e2c009bSjjc * Don't bother to setup system for multiple lgroups and only use one 7102e2c009bSjjc * memory node when memory is interleaved between any nodes or there is 7112e2c009bSjjc * only one NUMA node 7122e2c009bSjjc * 7132e2c009bSjjc * NOTE: May need to change this for Dynamic Reconfiguration (DR) 7142e2c009bSjjc * when and if it happens for x86/x64 7152e2c009bSjjc */ 7162e2c009bSjjc if (lgrp_plat_mem_intrlv || lgrp_plat_node_cnt == 1) { 7172e2c009bSjjc lgrp_plat_node_cnt = max_mem_nodes = 1; 7182e2c009bSjjc (void) lgrp_topo_ht_limit_set(1); 7192e2c009bSjjc return; 7202e2c009bSjjc } 7212e2c009bSjjc 7222e2c009bSjjc /* 7232e2c009bSjjc * Leaf lgroups on x86/x64 architectures contain one physical 7242e2c009bSjjc * processor chip. Tune lgrp_expand_proc_thresh and 7252e2c009bSjjc * lgrp_expand_proc_diff so that lgrp_choose() will spread 7262e2c009bSjjc * things out aggressively. 7272e2c009bSjjc */ 7282e2c009bSjjc lgrp_expand_proc_thresh = LGRP_LOADAVG_THREAD_MAX / 2; 7292e2c009bSjjc lgrp_expand_proc_diff = 0; 7302e2c009bSjjc 7312e2c009bSjjc /* 7322e2c009bSjjc * There should be one memnode (physical page free list(s)) for 7332e2c009bSjjc * each node 7342e2c009bSjjc */ 7352e2c009bSjjc max_mem_nodes = lgrp_plat_node_cnt; 7362e2c009bSjjc 7372e2c009bSjjc /* 7382e2c009bSjjc * Determine how far each NUMA node is from each other by 7392e2c009bSjjc * reading ACPI System Locality Information Table (SLIT) if it 7402e2c009bSjjc * exists 7412e2c009bSjjc */ 7422e2c009bSjjc lgrp_plat_slit_error = lgrp_plat_process_slit(slit_ptr, 7432e2c009bSjjc lgrp_plat_node_cnt, lgrp_plat_node_memory, 7442e2c009bSjjc &lgrp_plat_lat_stats); 7452e2c009bSjjc if (lgrp_plat_slit_error == 0) 7462e2c009bSjjc return; 7472e2c009bSjjc 7482e2c009bSjjc /* 7492e2c009bSjjc * Probe to determine latency between NUMA nodes when SLIT 7502e2c009bSjjc * doesn't exist or make sense 7512e2c009bSjjc */ 7522e2c009bSjjc lgrp_plat_probe_flags |= LGRP_PLAT_PROBE_ENABLE; 7532e2c009bSjjc 7542e2c009bSjjc /* 7552e2c009bSjjc * Specify whether to probe using vendor ID register or page copy 7562e2c009bSjjc * if hasn't been specified already or is overspecified 7572e2c009bSjjc */ 7582e2c009bSjjc probe_op = lgrp_plat_probe_flags & 7592e2c009bSjjc (LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR); 7602e2c009bSjjc 7612e2c009bSjjc if (probe_op == 0 || 7622e2c009bSjjc probe_op == (LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR)) { 7632e2c009bSjjc lgrp_plat_probe_flags &= 7642e2c009bSjjc ~(LGRP_PLAT_PROBE_PGCPY|LGRP_PLAT_PROBE_VENDOR); 7652e2c009bSjjc if (is_opteron()) 7662e2c009bSjjc lgrp_plat_probe_flags |= 7672e2c009bSjjc LGRP_PLAT_PROBE_VENDOR; 7682e2c009bSjjc else 7692e2c009bSjjc lgrp_plat_probe_flags |= LGRP_PLAT_PROBE_PGCPY; 7702e2c009bSjjc } 7712e2c009bSjjc 7722e2c009bSjjc /* 7732e2c009bSjjc * Probing errors can mess up the lgroup topology and 7742e2c009bSjjc * force us fall back to a 2 level lgroup topology. 7752e2c009bSjjc * Here we bound how tall the lgroup topology can grow 7762e2c009bSjjc * in hopes of avoiding any anamolies in probing from 7772e2c009bSjjc * messing up the lgroup topology by limiting the 7782e2c009bSjjc * accuracy of the latency topology. 7792e2c009bSjjc * 7802e2c009bSjjc * Assume that nodes will at least be configured in a 7812e2c009bSjjc * ring, so limit height of lgroup topology to be less 7822e2c009bSjjc * than number of nodes on a system with 4 or more 7832e2c009bSjjc * nodes 7842e2c009bSjjc */ 7852e2c009bSjjc if (lgrp_plat_node_cnt >= 4 && lgrp_topo_ht_limit() == 7862e2c009bSjjc lgrp_topo_ht_limit_default()) 7872e2c009bSjjc (void) lgrp_topo_ht_limit_set(lgrp_plat_node_cnt - 1); 7882e2c009bSjjc #endif /* __xpv */ 7892e2c009bSjjc } 7902e2c009bSjjc 7912e2c009bSjjc 7922e2c009bSjjc /* 7932e2c009bSjjc * Return latency between "from" and "to" lgroups 7942e2c009bSjjc * 7952e2c009bSjjc * This latency number can only be used for relative comparison 7962e2c009bSjjc * between lgroups on the running system, cannot be used across platforms, 7972e2c009bSjjc * and may not reflect the actual latency. It is platform and implementation 7982e2c009bSjjc * specific, so platform gets to decide its value. It would be nice if the 7992e2c009bSjjc * number was at least proportional to make comparisons more meaningful though. 8002e2c009bSjjc */ 8012e2c009bSjjc /* ARGSUSED */ 8022e2c009bSjjc int 8032e2c009bSjjc lgrp_plat_latency(lgrp_handle_t from, lgrp_handle_t to) 8042e2c009bSjjc { 8052e2c009bSjjc lgrp_handle_t src, dest; 8062e2c009bSjjc int node; 8072e2c009bSjjc 8082e2c009bSjjc if (max_mem_nodes == 1) 8092e2c009bSjjc return (0); 8102e2c009bSjjc 8112e2c009bSjjc /* 8122e2c009bSjjc * Return max latency for root lgroup 8132e2c009bSjjc */ 8142e2c009bSjjc if (from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE) 8152e2c009bSjjc return (lgrp_plat_lat_stats.latency_max); 8162e2c009bSjjc 8172e2c009bSjjc src = from; 8182e2c009bSjjc dest = to; 8192e2c009bSjjc 8202e2c009bSjjc /* 8212e2c009bSjjc * Return 0 for nodes (lgroup platform handles) out of range 8222e2c009bSjjc */ 8232e2c009bSjjc if (src < 0 || src >= MAX_NODES || dest < 0 || dest >= MAX_NODES) 8242e2c009bSjjc return (0); 8252e2c009bSjjc 8262e2c009bSjjc /* 8272e2c009bSjjc * Probe from current CPU if its lgroup latencies haven't been set yet 8282e2c009bSjjc * and we are trying to get latency from current CPU to some node 8292e2c009bSjjc */ 8302e2c009bSjjc node = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node); 8312e2c009bSjjc ASSERT(node >= 0 && node < lgrp_plat_node_cnt); 8322e2c009bSjjc if (lgrp_plat_lat_stats.latencies[src][src] == 0 && node == src) 8332e2c009bSjjc lgrp_plat_probe(); 8342e2c009bSjjc 8352e2c009bSjjc return (lgrp_plat_lat_stats.latencies[src][dest]); 8362e2c009bSjjc } 8372e2c009bSjjc 8382e2c009bSjjc 8392e2c009bSjjc /* 8402e2c009bSjjc * Platform-specific initialization 8412e2c009bSjjc */ 8422e2c009bSjjc void 8432e2c009bSjjc lgrp_plat_main_init(void) 8442e2c009bSjjc { 8452e2c009bSjjc int curnode; 8462e2c009bSjjc int ht_limit; 8472e2c009bSjjc int i; 8482e2c009bSjjc 8492e2c009bSjjc /* 8502e2c009bSjjc * Print a notice that MPO is disabled when memory is interleaved 8512e2c009bSjjc * across nodes....Would do this when it is discovered, but can't 8522e2c009bSjjc * because it happens way too early during boot.... 8532e2c009bSjjc */ 8542e2c009bSjjc if (lgrp_plat_mem_intrlv) 8552e2c009bSjjc cmn_err(CE_NOTE, 8562e2c009bSjjc "MPO disabled because memory is interleaved\n"); 8572e2c009bSjjc 8582e2c009bSjjc /* 8592e2c009bSjjc * Don't bother to do any probing if it is disabled, there is only one 8602e2c009bSjjc * node, or the height of the lgroup topology less than or equal to 2 8612e2c009bSjjc */ 8622e2c009bSjjc ht_limit = lgrp_topo_ht_limit(); 8632e2c009bSjjc if (!(lgrp_plat_probe_flags & LGRP_PLAT_PROBE_ENABLE) || 8642e2c009bSjjc max_mem_nodes == 1 || ht_limit <= 2) { 8652e2c009bSjjc /* 8662e2c009bSjjc * Setup lgroup latencies for 2 level lgroup topology 8672e2c009bSjjc * (ie. local and remote only) if they haven't been set yet 8682e2c009bSjjc */ 8692e2c009bSjjc if (ht_limit == 2 && lgrp_plat_lat_stats.latency_min == -1 && 8702e2c009bSjjc lgrp_plat_lat_stats.latency_max == 0) 8712e2c009bSjjc lgrp_plat_2level_setup(lgrp_plat_node_memory, 8722e2c009bSjjc &lgrp_plat_lat_stats); 8732e2c009bSjjc return; 8742e2c009bSjjc } 8752e2c009bSjjc 8762e2c009bSjjc if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_VENDOR) { 8772e2c009bSjjc /* 8782e2c009bSjjc * Should have been able to probe from CPU 0 when it was added 8792e2c009bSjjc * to lgroup hierarchy, but may not have been able to then 8802e2c009bSjjc * because it happens so early in boot that gethrtime() hasn't 8812e2c009bSjjc * been initialized. (:-( 8822e2c009bSjjc */ 8832e2c009bSjjc curnode = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node); 8842e2c009bSjjc ASSERT(curnode >= 0 && curnode < lgrp_plat_node_cnt); 8852e2c009bSjjc if (lgrp_plat_lat_stats.latencies[curnode][curnode] == 0) 8862e2c009bSjjc lgrp_plat_probe(); 8872e2c009bSjjc 8882e2c009bSjjc return; 8892e2c009bSjjc } 8902e2c009bSjjc 8912e2c009bSjjc /* 8922e2c009bSjjc * When probing memory, use one page for every sample to determine 8932e2c009bSjjc * lgroup topology and taking multiple samples 8942e2c009bSjjc */ 8952e2c009bSjjc if (lgrp_plat_probe_mem_config.probe_memsize == 0) 8962e2c009bSjjc lgrp_plat_probe_mem_config.probe_memsize = PAGESIZE * 8972e2c009bSjjc lgrp_plat_probe_nsamples; 8982e2c009bSjjc 8992e2c009bSjjc /* 9002e2c009bSjjc * Map memory in each node needed for probing to determine latency 9012e2c009bSjjc * topology 9022e2c009bSjjc */ 9032e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 9042e2c009bSjjc int mnode; 9052e2c009bSjjc 9062e2c009bSjjc /* 9072e2c009bSjjc * Skip this node and leave its probe page NULL 9082e2c009bSjjc * if it doesn't have any memory 9092e2c009bSjjc */ 9102e2c009bSjjc mnode = plat_lgrphand_to_mem_node((lgrp_handle_t)i); 9112e2c009bSjjc if (!mem_node_config[mnode].exists) { 9122e2c009bSjjc lgrp_plat_probe_mem_config.probe_va[i] = NULL; 9132e2c009bSjjc continue; 9142e2c009bSjjc } 9152e2c009bSjjc 9162e2c009bSjjc /* 9172e2c009bSjjc * Allocate one kernel virtual page 9182e2c009bSjjc */ 9192e2c009bSjjc lgrp_plat_probe_mem_config.probe_va[i] = vmem_alloc(heap_arena, 9202e2c009bSjjc lgrp_plat_probe_mem_config.probe_memsize, VM_NOSLEEP); 9212e2c009bSjjc if (lgrp_plat_probe_mem_config.probe_va[i] == NULL) { 9222e2c009bSjjc cmn_err(CE_WARN, 9232e2c009bSjjc "lgrp_plat_main_init: couldn't allocate memory"); 9242e2c009bSjjc return; 9252e2c009bSjjc } 9262e2c009bSjjc 9272e2c009bSjjc /* 9282e2c009bSjjc * Get PFN for first page in each node 9292e2c009bSjjc */ 9302e2c009bSjjc lgrp_plat_probe_mem_config.probe_pfn[i] = 9312e2c009bSjjc mem_node_config[mnode].physbase; 9322e2c009bSjjc 9332e2c009bSjjc /* 9342e2c009bSjjc * Map virtual page to first page in node 9352e2c009bSjjc */ 9362e2c009bSjjc hat_devload(kas.a_hat, lgrp_plat_probe_mem_config.probe_va[i], 9372e2c009bSjjc lgrp_plat_probe_mem_config.probe_memsize, 9382e2c009bSjjc lgrp_plat_probe_mem_config.probe_pfn[i], 9392e2c009bSjjc PROT_READ | PROT_WRITE | HAT_PLAT_NOCACHE, 9402e2c009bSjjc HAT_LOAD_NOCONSIST); 9412e2c009bSjjc } 9422e2c009bSjjc 9432e2c009bSjjc /* 9442e2c009bSjjc * Probe from current CPU 9452e2c009bSjjc */ 9462e2c009bSjjc lgrp_plat_probe(); 9472e2c009bSjjc } 9482e2c009bSjjc 9492e2c009bSjjc 9502e2c009bSjjc /* 9512e2c009bSjjc * Return the maximum number of lgrps supported by the platform. 9522e2c009bSjjc * Before lgrp topology is known it returns an estimate based on the number of 9532e2c009bSjjc * nodes. Once topology is known it returns the actual maximim number of lgrps 9542e2c009bSjjc * created. Since x86/x64 doesn't support Dynamic Reconfiguration (DR) and 9552e2c009bSjjc * dynamic addition of new nodes, this number may not grow during system 9562e2c009bSjjc * lifetime (yet). 9572e2c009bSjjc */ 9582e2c009bSjjc int 9592e2c009bSjjc lgrp_plat_max_lgrps(void) 9602e2c009bSjjc { 9612e2c009bSjjc return (lgrp_topo_initialized ? 9622e2c009bSjjc lgrp_alloc_max + 1 : 9632e2c009bSjjc lgrp_plat_node_cnt * (lgrp_plat_node_cnt - 1) + 1); 9642e2c009bSjjc } 9652e2c009bSjjc 9662e2c009bSjjc 9672e2c009bSjjc /* 9682e2c009bSjjc * Return the number of free pages in an lgroup. 9692e2c009bSjjc * 9702e2c009bSjjc * For query of LGRP_MEM_SIZE_FREE, return the number of base pagesize 9712e2c009bSjjc * pages on freelists. For query of LGRP_MEM_SIZE_AVAIL, return the 9722e2c009bSjjc * number of allocatable base pagesize pages corresponding to the 9732e2c009bSjjc * lgroup (e.g. do not include page_t's, BOP_ALLOC()'ed memory, ..) 9742e2c009bSjjc * For query of LGRP_MEM_SIZE_INSTALL, return the amount of physical 9752e2c009bSjjc * memory installed, regardless of whether or not it's usable. 9762e2c009bSjjc */ 9772e2c009bSjjc pgcnt_t 9782e2c009bSjjc lgrp_plat_mem_size(lgrp_handle_t plathand, lgrp_mem_query_t query) 9792e2c009bSjjc { 9802e2c009bSjjc int mnode; 9812e2c009bSjjc pgcnt_t npgs = (pgcnt_t)0; 9822e2c009bSjjc extern struct memlist *phys_avail; 9832e2c009bSjjc extern struct memlist *phys_install; 9842e2c009bSjjc 9852e2c009bSjjc 9862e2c009bSjjc if (plathand == LGRP_DEFAULT_HANDLE) 9872e2c009bSjjc return (lgrp_plat_mem_size_default(plathand, query)); 9882e2c009bSjjc 9892e2c009bSjjc if (plathand != LGRP_NULL_HANDLE) { 9902e2c009bSjjc mnode = plat_lgrphand_to_mem_node(plathand); 9912e2c009bSjjc if (mnode >= 0 && mem_node_config[mnode].exists) { 9922e2c009bSjjc switch (query) { 9932e2c009bSjjc case LGRP_MEM_SIZE_FREE: 9942e2c009bSjjc npgs = MNODE_PGCNT(mnode); 9952e2c009bSjjc break; 9962e2c009bSjjc case LGRP_MEM_SIZE_AVAIL: 9972e2c009bSjjc npgs = mem_node_memlist_pages(mnode, 9982e2c009bSjjc phys_avail); 9992e2c009bSjjc break; 10002e2c009bSjjc case LGRP_MEM_SIZE_INSTALL: 10012e2c009bSjjc npgs = mem_node_memlist_pages(mnode, 10022e2c009bSjjc phys_install); 10032e2c009bSjjc break; 10042e2c009bSjjc default: 10052e2c009bSjjc break; 10062e2c009bSjjc } 10072e2c009bSjjc } 10082e2c009bSjjc } 10092e2c009bSjjc return (npgs); 10102e2c009bSjjc } 10112e2c009bSjjc 10122e2c009bSjjc 10132e2c009bSjjc /* 10142e2c009bSjjc * Return the platform handle of the lgroup that contains the physical memory 10152e2c009bSjjc * corresponding to the given page frame number 10162e2c009bSjjc */ 10172e2c009bSjjc /* ARGSUSED */ 10182e2c009bSjjc lgrp_handle_t 10192e2c009bSjjc lgrp_plat_pfn_to_hand(pfn_t pfn) 10202e2c009bSjjc { 10212e2c009bSjjc int mnode; 10222e2c009bSjjc 10232e2c009bSjjc if (max_mem_nodes == 1) 10242e2c009bSjjc return (LGRP_DEFAULT_HANDLE); 10252e2c009bSjjc 10262e2c009bSjjc if (pfn > physmax) 10272e2c009bSjjc return (LGRP_NULL_HANDLE); 10282e2c009bSjjc 10292e2c009bSjjc mnode = plat_pfn_to_mem_node(pfn); 10302e2c009bSjjc if (mnode < 0) 10312e2c009bSjjc return (LGRP_NULL_HANDLE); 10322e2c009bSjjc 10332e2c009bSjjc return (MEM_NODE_2_LGRPHAND(mnode)); 10342e2c009bSjjc } 10352e2c009bSjjc 10362e2c009bSjjc 10372e2c009bSjjc /* 10382e2c009bSjjc * Probe memory in each node from current CPU to determine latency topology 10392e2c009bSjjc * 10402e2c009bSjjc * The probing code will probe the vendor ID register on the Northbridge of 10412e2c009bSjjc * Opteron processors and probe memory for other processors by default. 10422e2c009bSjjc * 10432e2c009bSjjc * Since probing is inherently error prone, the code takes laps across all the 10442e2c009bSjjc * nodes probing from each node to each of the other nodes some number of 10452e2c009bSjjc * times. Furthermore, each node is probed some number of times before moving 10462e2c009bSjjc * onto the next one during each lap. The minimum latency gotten between nodes 10472e2c009bSjjc * is kept as the latency between the nodes. 10482e2c009bSjjc * 10492e2c009bSjjc * After all that, the probe times are adjusted by normalizing values that are 10502e2c009bSjjc * close to each other and local latencies are made the same. Lastly, the 10512e2c009bSjjc * latencies are verified to make sure that certain conditions are met (eg. 10522e2c009bSjjc * local < remote, latency(a, b) == latency(b, a), etc.). 10532e2c009bSjjc * 10542e2c009bSjjc * If any of the conditions aren't met, the code will export a NUMA 10552e2c009bSjjc * configuration with the local CPUs and memory given by the SRAT or PCI config 10562e2c009bSjjc * space registers and one remote memory latency since it can't tell exactly 10572e2c009bSjjc * how far each node is from each other. 10582e2c009bSjjc */ 10592e2c009bSjjc void 10602e2c009bSjjc lgrp_plat_probe(void) 10612e2c009bSjjc { 10622e2c009bSjjc int from; 10632e2c009bSjjc int i; 10642e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats; 10652e2c009bSjjc hrtime_t probe_time; 10662e2c009bSjjc int to; 10672e2c009bSjjc 10682e2c009bSjjc if (!(lgrp_plat_probe_flags & LGRP_PLAT_PROBE_ENABLE) || 10692e2c009bSjjc max_mem_nodes == 1 || lgrp_topo_ht_limit() <= 2) 10702e2c009bSjjc return; 10712e2c009bSjjc 10722e2c009bSjjc /* 10732e2c009bSjjc * Determine ID of node containing current CPU 10742e2c009bSjjc */ 10752e2c009bSjjc from = lgrp_plat_cpu_to_node(CPU, lgrp_plat_cpu_node); 10762e2c009bSjjc ASSERT(from >= 0 && from < lgrp_plat_node_cnt); 10772e2c009bSjjc if (srat_ptr && lgrp_plat_srat_enable && !lgrp_plat_srat_error) 10782e2c009bSjjc ASSERT(lgrp_plat_node_domain[from].exists); 10792e2c009bSjjc 10802e2c009bSjjc /* 10812e2c009bSjjc * Don't need to probe if got times already 10822e2c009bSjjc */ 10832e2c009bSjjc lat_stats = &lgrp_plat_lat_stats; 10842e2c009bSjjc if (lat_stats->latencies[from][from] != 0) 10852e2c009bSjjc return; 10862e2c009bSjjc 10872e2c009bSjjc /* 10882e2c009bSjjc * Read vendor ID in Northbridge or read and write page(s) 10892e2c009bSjjc * in each node from current CPU and remember how long it takes, 10902e2c009bSjjc * so we can build latency topology of machine later. 10912e2c009bSjjc * This should approximate the memory latency between each node. 10922e2c009bSjjc */ 10932e2c009bSjjc for (i = 0; i < lgrp_plat_probe_nrounds; i++) { 10942e2c009bSjjc for (to = 0; to < lgrp_plat_node_cnt; to++) { 10952e2c009bSjjc /* 10962e2c009bSjjc * Get probe time and bail out if can't get it yet 10972e2c009bSjjc */ 10982e2c009bSjjc probe_time = lgrp_plat_probe_time(to, 10992e2c009bSjjc lgrp_plat_cpu_node, &lgrp_plat_probe_mem_config, 11002e2c009bSjjc &lgrp_plat_lat_stats, &lgrp_plat_probe_stats); 11012e2c009bSjjc if (probe_time == 0) 11022e2c009bSjjc return; 11032e2c009bSjjc 11042e2c009bSjjc /* 11052e2c009bSjjc * Keep lowest probe time as latency between nodes 11062e2c009bSjjc */ 11072e2c009bSjjc if (lat_stats->latencies[from][to] == 0 || 11082e2c009bSjjc probe_time < lat_stats->latencies[from][to]) 11092e2c009bSjjc lat_stats->latencies[from][to] = probe_time; 11102e2c009bSjjc 11112e2c009bSjjc /* 11122e2c009bSjjc * Update overall minimum and maximum probe times 11132e2c009bSjjc * across all nodes 11142e2c009bSjjc */ 11152e2c009bSjjc if (probe_time < lat_stats->latency_min || 11162e2c009bSjjc lat_stats->latency_min == -1) 11172e2c009bSjjc lat_stats->latency_min = probe_time; 11182e2c009bSjjc if (probe_time > lat_stats->latency_max) 11192e2c009bSjjc lat_stats->latency_max = probe_time; 11202e2c009bSjjc } 11212e2c009bSjjc } 11222e2c009bSjjc 11232e2c009bSjjc /* 11242e2c009bSjjc * - Fix up latencies such that local latencies are same, 11252e2c009bSjjc * latency(i, j) == latency(j, i), etc. (if possible) 11262e2c009bSjjc * 11272e2c009bSjjc * - Verify that latencies look ok 11282e2c009bSjjc * 11292e2c009bSjjc * - Fallback to just optimizing for local and remote if 11302e2c009bSjjc * latencies didn't look right 11312e2c009bSjjc */ 11322e2c009bSjjc lgrp_plat_latency_adjust(lgrp_plat_node_memory, &lgrp_plat_lat_stats, 11332e2c009bSjjc &lgrp_plat_probe_stats); 11342e2c009bSjjc lgrp_plat_probe_stats.probe_error_code = 11352e2c009bSjjc lgrp_plat_latency_verify(lgrp_plat_node_memory, 11362e2c009bSjjc &lgrp_plat_lat_stats); 11372e2c009bSjjc if (lgrp_plat_probe_stats.probe_error_code) 11382e2c009bSjjc lgrp_plat_2level_setup(lgrp_plat_node_memory, 11392e2c009bSjjc &lgrp_plat_lat_stats); 11402e2c009bSjjc } 11412e2c009bSjjc 11422e2c009bSjjc 11432e2c009bSjjc /* 11442e2c009bSjjc * Return platform handle for root lgroup 11452e2c009bSjjc */ 11462e2c009bSjjc lgrp_handle_t 11472e2c009bSjjc lgrp_plat_root_hand(void) 11482e2c009bSjjc { 11492e2c009bSjjc return (LGRP_DEFAULT_HANDLE); 11502e2c009bSjjc } 11512e2c009bSjjc 11522e2c009bSjjc 11532e2c009bSjjc /* 11542e2c009bSjjc * INTERNAL ROUTINES 11552e2c009bSjjc */ 11562e2c009bSjjc 11572e2c009bSjjc 11582e2c009bSjjc /* 11592e2c009bSjjc * Update CPU to node mapping for given CPU and proximity domain (and returns 11602e2c009bSjjc * negative numbers for errors and positive ones for success) 11612e2c009bSjjc */ 11622e2c009bSjjc static int 11632e2c009bSjjc lgrp_plat_cpu_node_update(node_domain_map_t *node_domain, 11642e2c009bSjjc cpu_node_map_t *cpu_node, uint32_t apicid, uint32_t domain) 11652e2c009bSjjc { 11662e2c009bSjjc uint_t i; 11672e2c009bSjjc uint_t start; 11682e2c009bSjjc int node; 11692e2c009bSjjc 11702e2c009bSjjc /* 11712e2c009bSjjc * Get node number for proximity domain 11722e2c009bSjjc */ 11732e2c009bSjjc node = lgrp_plat_domain_to_node(node_domain, domain); 11742e2c009bSjjc if (node == -1) { 11752e2c009bSjjc node = lgrp_plat_node_domain_update(node_domain, domain); 11762e2c009bSjjc if (node == -1) 11772e2c009bSjjc return (-1); 11782e2c009bSjjc } 11792e2c009bSjjc 11802e2c009bSjjc /* 11812e2c009bSjjc * Hash given CPU APIC ID into CPU to node mapping table/array and 11822e2c009bSjjc * enter it and its corresponding node and proximity domain IDs into 11832e2c009bSjjc * first non-existent or matching entry 11842e2c009bSjjc */ 11852e2c009bSjjc i = start = CPU_NODE_HASH(apicid); 11862e2c009bSjjc do { 11872e2c009bSjjc if (cpu_node[i].exists) { 11882e2c009bSjjc /* 11892e2c009bSjjc * Update already existing entry for CPU 11902e2c009bSjjc */ 11912e2c009bSjjc if (cpu_node[i].apicid == apicid) { 11922e2c009bSjjc /* 11932e2c009bSjjc * Just return when everything same 11942e2c009bSjjc */ 11952e2c009bSjjc if (cpu_node[i].prox_domain == domain && 11962e2c009bSjjc cpu_node[i].node == node) 11972e2c009bSjjc return (1); 11982e2c009bSjjc 11992e2c009bSjjc /* 12002e2c009bSjjc * Assert that proximity domain and node IDs 12012e2c009bSjjc * should be same and return error on non-debug 12022e2c009bSjjc * kernel 12032e2c009bSjjc */ 12042e2c009bSjjc ASSERT(cpu_node[i].prox_domain == domain && 12052e2c009bSjjc cpu_node[i].node == node); 12062e2c009bSjjc return (-1); 12072e2c009bSjjc } 12082e2c009bSjjc } else { 12092e2c009bSjjc /* 12102e2c009bSjjc * Create new entry for CPU 12112e2c009bSjjc */ 12122e2c009bSjjc cpu_node[i].exists = 1; 12132e2c009bSjjc cpu_node[i].apicid = apicid; 12142e2c009bSjjc cpu_node[i].prox_domain = domain; 12152e2c009bSjjc cpu_node[i].node = node; 12162e2c009bSjjc return (0); 12172e2c009bSjjc } 12182e2c009bSjjc i = CPU_NODE_HASH(i + 1); 12192e2c009bSjjc } while (i != start); 12202e2c009bSjjc 12212e2c009bSjjc /* 12222e2c009bSjjc * Ran out of supported number of entries which shouldn't happen.... 12232e2c009bSjjc */ 12242e2c009bSjjc ASSERT(i != start); 12252e2c009bSjjc return (-1); 12262e2c009bSjjc } 12272e2c009bSjjc 12282e2c009bSjjc 12292e2c009bSjjc /* 12302e2c009bSjjc * Get node ID for given CPU ID 12312e2c009bSjjc */ 12322e2c009bSjjc static int 12332e2c009bSjjc lgrp_plat_cpu_to_node(cpu_t *cp, cpu_node_map_t *cpu_node) 12342e2c009bSjjc { 12352e2c009bSjjc uint32_t apicid; 12362e2c009bSjjc uint_t i; 12372e2c009bSjjc uint_t start; 12382e2c009bSjjc 12392e2c009bSjjc if (cp == NULL) 12402e2c009bSjjc return (-1); 12412e2c009bSjjc 12422e2c009bSjjc /* 12432e2c009bSjjc * SRAT doesn't exist, isn't enabled, or there was an error processing 12442e2c009bSjjc * it, so return chip ID for Opteron and -1 otherwise. 12452e2c009bSjjc */ 12462e2c009bSjjc if (srat_ptr == NULL || !lgrp_plat_srat_enable || 12472e2c009bSjjc lgrp_plat_srat_error) { 12482e2c009bSjjc if (is_opteron()) 12492e2c009bSjjc return (pg_plat_hw_instance_id(cp, PGHW_CHIP)); 12502e2c009bSjjc return (-1); 12512e2c009bSjjc } 12522e2c009bSjjc 12532e2c009bSjjc /* 12542e2c009bSjjc * SRAT does exist, so get APIC ID for given CPU and map that to its 12552e2c009bSjjc * node ID 12562e2c009bSjjc */ 12572e2c009bSjjc apicid = cpuid_get_apicid(cp); 12582e2c009bSjjc i = start = CPU_NODE_HASH(apicid); 12592e2c009bSjjc do { 12602e2c009bSjjc if (cpu_node[i].apicid == apicid && cpu_node[i].exists) 12612e2c009bSjjc return (cpu_node[i].node); 12622e2c009bSjjc i = CPU_NODE_HASH(i + 1); 12632e2c009bSjjc } while (i != start); 12642e2c009bSjjc return (-1); 12652e2c009bSjjc } 12662e2c009bSjjc 12672e2c009bSjjc 12682e2c009bSjjc /* 12692e2c009bSjjc * Return node number for given proximity domain/system locality 12702e2c009bSjjc */ 12712e2c009bSjjc static int 12722e2c009bSjjc lgrp_plat_domain_to_node(node_domain_map_t *node_domain, uint32_t domain) 12732e2c009bSjjc { 12742e2c009bSjjc uint_t node; 12752e2c009bSjjc uint_t start; 12762e2c009bSjjc 12772e2c009bSjjc /* 12782e2c009bSjjc * Hash proximity domain ID into node to domain mapping table (array), 12792e2c009bSjjc * search for entry with matching proximity domain ID, and return index 12802e2c009bSjjc * of matching entry as node ID. 12812e2c009bSjjc */ 12822e2c009bSjjc node = start = NODE_DOMAIN_HASH(domain); 12832e2c009bSjjc do { 12842e2c009bSjjc if (node_domain[node].prox_domain == domain && 12852e2c009bSjjc node_domain[node].exists) 12862e2c009bSjjc return (node); 12872e2c009bSjjc node = NODE_DOMAIN_HASH(node + 1); 12882e2c009bSjjc } while (node != start); 12892e2c009bSjjc return (-1); 12902e2c009bSjjc } 12912e2c009bSjjc 12922e2c009bSjjc 12932e2c009bSjjc /* 12942e2c009bSjjc * Latencies must be within 1/(2**LGRP_LAT_TOLERANCE_SHIFT) of each other to 12952e2c009bSjjc * be considered same 12962e2c009bSjjc */ 12972e2c009bSjjc #define LGRP_LAT_TOLERANCE_SHIFT 4 12982e2c009bSjjc 12992e2c009bSjjc int lgrp_plat_probe_lt_shift = LGRP_LAT_TOLERANCE_SHIFT; 13002e2c009bSjjc 13012e2c009bSjjc 13022e2c009bSjjc /* 13032e2c009bSjjc * Adjust latencies between nodes to be symmetric, normalize latencies between 13042e2c009bSjjc * any nodes that are within some tolerance to be same, and make local 13052e2c009bSjjc * latencies be same 13062e2c009bSjjc */ 13072e2c009bSjjc static void 13082e2c009bSjjc lgrp_plat_latency_adjust(node_phys_addr_map_t *node_memory, 13092e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats, lgrp_plat_probe_stats_t *probe_stats) 13102e2c009bSjjc { 13112e2c009bSjjc int i; 13122e2c009bSjjc int j; 13132e2c009bSjjc int k; 13142e2c009bSjjc int l; 13152e2c009bSjjc u_longlong_t max; 13162e2c009bSjjc u_longlong_t min; 13172e2c009bSjjc u_longlong_t t; 13182e2c009bSjjc u_longlong_t t1; 13192e2c009bSjjc u_longlong_t t2; 13202e2c009bSjjc const lgrp_config_flag_t cflag = LGRP_CONFIG_LAT_CHANGE_ALL; 13212e2c009bSjjc int lat_corrected[MAX_NODES][MAX_NODES]; 13222e2c009bSjjc 13232e2c009bSjjc /* 13242e2c009bSjjc * Nothing to do when this is an UMA machine or don't have args needed 13252e2c009bSjjc */ 13262e2c009bSjjc if (max_mem_nodes == 1) 13272e2c009bSjjc return; 13282e2c009bSjjc 13292e2c009bSjjc ASSERT(node_memory != NULL && lat_stats != NULL && 13302e2c009bSjjc probe_stats != NULL); 13312e2c009bSjjc 13322e2c009bSjjc /* 13332e2c009bSjjc * Make sure that latencies are symmetric between any two nodes 13342e2c009bSjjc * (ie. latency(node0, node1) == latency(node1, node0)) 13352e2c009bSjjc */ 13362e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 13372e2c009bSjjc if (!node_memory[i].exists) 13382e2c009bSjjc continue; 13392e2c009bSjjc 13402e2c009bSjjc for (j = 0; j < lgrp_plat_node_cnt; j++) { 13412e2c009bSjjc if (!node_memory[j].exists) 13422e2c009bSjjc continue; 13432e2c009bSjjc 13442e2c009bSjjc t1 = lat_stats->latencies[i][j]; 13452e2c009bSjjc t2 = lat_stats->latencies[j][i]; 13462e2c009bSjjc 13472e2c009bSjjc if (t1 == 0 || t2 == 0 || t1 == t2) 13482e2c009bSjjc continue; 13492e2c009bSjjc 13502e2c009bSjjc /* 13512e2c009bSjjc * Latencies should be same 13522e2c009bSjjc * - Use minimum of two latencies which should be same 13532e2c009bSjjc * - Track suspect probe times not within tolerance of 13542e2c009bSjjc * min value 13552e2c009bSjjc * - Remember how much values are corrected by 13562e2c009bSjjc */ 13572e2c009bSjjc if (t1 > t2) { 13582e2c009bSjjc t = t2; 13592e2c009bSjjc probe_stats->probe_errors[i][j] += t1 - t2; 13602e2c009bSjjc if (t1 - t2 > t2 >> lgrp_plat_probe_lt_shift) { 13612e2c009bSjjc probe_stats->probe_suspect[i][j]++; 13622e2c009bSjjc probe_stats->probe_suspect[j][i]++; 13632e2c009bSjjc } 13642e2c009bSjjc } else if (t2 > t1) { 13652e2c009bSjjc t = t1; 13662e2c009bSjjc probe_stats->probe_errors[j][i] += t2 - t1; 13672e2c009bSjjc if (t2 - t1 > t1 >> lgrp_plat_probe_lt_shift) { 13682e2c009bSjjc probe_stats->probe_suspect[i][j]++; 13692e2c009bSjjc probe_stats->probe_suspect[j][i]++; 13702e2c009bSjjc } 13712e2c009bSjjc } 13722e2c009bSjjc 13732e2c009bSjjc lat_stats->latencies[i][j] = 13742e2c009bSjjc lat_stats->latencies[j][i] = t; 13752e2c009bSjjc lgrp_config(cflag, t1, t); 13762e2c009bSjjc lgrp_config(cflag, t2, t); 13772e2c009bSjjc } 13782e2c009bSjjc } 13792e2c009bSjjc 13802e2c009bSjjc /* 13812e2c009bSjjc * Keep track of which latencies get corrected 13822e2c009bSjjc */ 13832e2c009bSjjc for (i = 0; i < MAX_NODES; i++) 13842e2c009bSjjc for (j = 0; j < MAX_NODES; j++) 13852e2c009bSjjc lat_corrected[i][j] = 0; 13862e2c009bSjjc 13872e2c009bSjjc /* 13882e2c009bSjjc * For every two nodes, see whether there is another pair of nodes which 13892e2c009bSjjc * are about the same distance apart and make the latencies be the same 13902e2c009bSjjc * if they are close enough together 13912e2c009bSjjc */ 13922e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 13932e2c009bSjjc if (!node_memory[i].exists) 13942e2c009bSjjc continue; 13952e2c009bSjjc for (j = 0; j < lgrp_plat_node_cnt; j++) { 13962e2c009bSjjc if (!node_memory[j].exists) 13972e2c009bSjjc continue; 13982e2c009bSjjc /* 13992e2c009bSjjc * Pick one pair of nodes (i, j) 14002e2c009bSjjc * and get latency between them 14012e2c009bSjjc */ 14022e2c009bSjjc t1 = lat_stats->latencies[i][j]; 14032e2c009bSjjc 14042e2c009bSjjc /* 14052e2c009bSjjc * Skip this pair of nodes if there isn't a latency 14062e2c009bSjjc * for it yet 14072e2c009bSjjc */ 14082e2c009bSjjc if (t1 == 0) 14092e2c009bSjjc continue; 14102e2c009bSjjc 14112e2c009bSjjc for (k = 0; k < lgrp_plat_node_cnt; k++) { 14122e2c009bSjjc if (!node_memory[k].exists) 14132e2c009bSjjc continue; 14142e2c009bSjjc for (l = 0; l < lgrp_plat_node_cnt; l++) { 14152e2c009bSjjc if (!node_memory[l].exists) 14162e2c009bSjjc continue; 14172e2c009bSjjc /* 14182e2c009bSjjc * Pick another pair of nodes (k, l) 14192e2c009bSjjc * not same as (i, j) and get latency 14202e2c009bSjjc * between them 14212e2c009bSjjc */ 14222e2c009bSjjc if (k == i && l == j) 14232e2c009bSjjc continue; 14242e2c009bSjjc 14252e2c009bSjjc t2 = lat_stats->latencies[k][l]; 14262e2c009bSjjc 14272e2c009bSjjc /* 14282e2c009bSjjc * Skip this pair of nodes if there 14292e2c009bSjjc * isn't a latency for it yet 14302e2c009bSjjc */ 14312e2c009bSjjc 14322e2c009bSjjc if (t2 == 0) 14332e2c009bSjjc continue; 14342e2c009bSjjc 14352e2c009bSjjc /* 14362e2c009bSjjc * Skip nodes (k, l) if they already 14372e2c009bSjjc * have same latency as (i, j) or 14382e2c009bSjjc * their latency isn't close enough to 14392e2c009bSjjc * be considered/made the same 14402e2c009bSjjc */ 14412e2c009bSjjc if (t1 == t2 || (t1 > t2 && t1 - t2 > 14422e2c009bSjjc t1 >> lgrp_plat_probe_lt_shift) || 14432e2c009bSjjc (t2 > t1 && t2 - t1 > 14442e2c009bSjjc t2 >> lgrp_plat_probe_lt_shift)) 14452e2c009bSjjc continue; 14462e2c009bSjjc 14472e2c009bSjjc /* 14482e2c009bSjjc * Make latency(i, j) same as 14492e2c009bSjjc * latency(k, l), try to use latency 14502e2c009bSjjc * that has been adjusted already to get 14512e2c009bSjjc * more consistency (if possible), and 14522e2c009bSjjc * remember which latencies were 14532e2c009bSjjc * adjusted for next time 14542e2c009bSjjc */ 14552e2c009bSjjc if (lat_corrected[i][j]) { 14562e2c009bSjjc t = t1; 14572e2c009bSjjc lgrp_config(cflag, t2, t); 14582e2c009bSjjc t2 = t; 14592e2c009bSjjc } else if (lat_corrected[k][l]) { 14602e2c009bSjjc t = t2; 14612e2c009bSjjc lgrp_config(cflag, t1, t); 14622e2c009bSjjc t1 = t; 14632e2c009bSjjc } else { 14642e2c009bSjjc if (t1 > t2) 14652e2c009bSjjc t = t2; 14662e2c009bSjjc else 14672e2c009bSjjc t = t1; 14682e2c009bSjjc lgrp_config(cflag, t1, t); 14692e2c009bSjjc lgrp_config(cflag, t2, t); 14702e2c009bSjjc t1 = t2 = t; 14712e2c009bSjjc } 14722e2c009bSjjc 14732e2c009bSjjc lat_stats->latencies[i][j] = 14742e2c009bSjjc lat_stats->latencies[k][l] = t; 14752e2c009bSjjc 14762e2c009bSjjc lat_corrected[i][j] = 14772e2c009bSjjc lat_corrected[k][l] = 1; 14782e2c009bSjjc } 14792e2c009bSjjc } 14802e2c009bSjjc } 14812e2c009bSjjc } 14822e2c009bSjjc 14832e2c009bSjjc /* 14842e2c009bSjjc * Local latencies should be same 14852e2c009bSjjc * - Find min and max local latencies 14862e2c009bSjjc * - Make all local latencies be minimum 14872e2c009bSjjc */ 14882e2c009bSjjc min = -1; 14892e2c009bSjjc max = 0; 14902e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 14912e2c009bSjjc if (!node_memory[i].exists) 14922e2c009bSjjc continue; 14932e2c009bSjjc t = lat_stats->latencies[i][i]; 14942e2c009bSjjc if (t == 0) 14952e2c009bSjjc continue; 14962e2c009bSjjc if (min == -1 || t < min) 14972e2c009bSjjc min = t; 14982e2c009bSjjc if (t > max) 14992e2c009bSjjc max = t; 15002e2c009bSjjc } 15012e2c009bSjjc if (min != max) { 15022e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 15032e2c009bSjjc int local; 15042e2c009bSjjc 15052e2c009bSjjc if (!node_memory[i].exists) 15062e2c009bSjjc continue; 15072e2c009bSjjc 15082e2c009bSjjc local = lat_stats->latencies[i][i]; 15092e2c009bSjjc if (local == 0) 15102e2c009bSjjc continue; 15112e2c009bSjjc 15122e2c009bSjjc /* 15132e2c009bSjjc * Track suspect probe times that aren't within 15142e2c009bSjjc * tolerance of minimum local latency and how much 15152e2c009bSjjc * probe times are corrected by 15162e2c009bSjjc */ 15172e2c009bSjjc if (local - min > min >> lgrp_plat_probe_lt_shift) 15182e2c009bSjjc probe_stats->probe_suspect[i][i]++; 15192e2c009bSjjc 15202e2c009bSjjc probe_stats->probe_errors[i][i] += local - min; 15212e2c009bSjjc 15222e2c009bSjjc /* 15232e2c009bSjjc * Make local latencies be minimum 15242e2c009bSjjc */ 15252e2c009bSjjc lgrp_config(LGRP_CONFIG_LAT_CHANGE, i, min); 15262e2c009bSjjc lat_stats->latencies[i][i] = min; 15272e2c009bSjjc } 15282e2c009bSjjc } 15292e2c009bSjjc 15302e2c009bSjjc /* 15312e2c009bSjjc * Determine max probe time again since just adjusted latencies 15322e2c009bSjjc */ 15332e2c009bSjjc lat_stats->latency_max = 0; 15342e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 15352e2c009bSjjc if (!node_memory[i].exists) 15362e2c009bSjjc continue; 15372e2c009bSjjc for (j = 0; j < lgrp_plat_node_cnt; j++) { 15382e2c009bSjjc if (!node_memory[j].exists) 15392e2c009bSjjc continue; 15402e2c009bSjjc t = lat_stats->latencies[i][j]; 15412e2c009bSjjc if (t > lat_stats->latency_max) 15422e2c009bSjjc lat_stats->latency_max = t; 15432e2c009bSjjc } 15442e2c009bSjjc } 15452e2c009bSjjc } 15462e2c009bSjjc 15472e2c009bSjjc 15482e2c009bSjjc /* 15492e2c009bSjjc * Verify following about latencies between nodes: 15502e2c009bSjjc * 15512e2c009bSjjc * - Latencies should be symmetric (ie. latency(a, b) == latency(b, a)) 15522e2c009bSjjc * - Local latencies same 15532e2c009bSjjc * - Local < remote 15542e2c009bSjjc * - Number of latencies seen is reasonable 15552e2c009bSjjc * - Number of occurrences of a given latency should be more than 1 15562e2c009bSjjc * 15572e2c009bSjjc * Returns: 15582e2c009bSjjc * 0 Success 15592e2c009bSjjc * -1 Not symmetric 15602e2c009bSjjc * -2 Local latencies not same 15612e2c009bSjjc * -3 Local >= remote 15622e2c009bSjjc */ 15632e2c009bSjjc static int 15642e2c009bSjjc lgrp_plat_latency_verify(node_phys_addr_map_t *node_memory, 15652e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats) 15662e2c009bSjjc { 15672e2c009bSjjc int i; 15682e2c009bSjjc int j; 15692e2c009bSjjc u_longlong_t t1; 15702e2c009bSjjc u_longlong_t t2; 15712e2c009bSjjc 15722e2c009bSjjc ASSERT(node_memory != NULL && lat_stats != NULL); 15732e2c009bSjjc 15742e2c009bSjjc /* 15752e2c009bSjjc * Nothing to do when this is an UMA machine, lgroup topology is 15762e2c009bSjjc * limited to 2 levels, or there aren't any probe times yet 15772e2c009bSjjc */ 15782e2c009bSjjc if (max_mem_nodes == 1 || lgrp_topo_levels < 2 || 15792e2c009bSjjc lat_stats->latencies[0][0] == 0) 15802e2c009bSjjc return (0); 15812e2c009bSjjc 15822e2c009bSjjc /* 15832e2c009bSjjc * Make sure that latencies are symmetric between any two nodes 15842e2c009bSjjc * (ie. latency(node0, node1) == latency(node1, node0)) 15852e2c009bSjjc */ 15862e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 15872e2c009bSjjc if (!node_memory[i].exists) 15882e2c009bSjjc continue; 15892e2c009bSjjc for (j = 0; j < lgrp_plat_node_cnt; j++) { 15902e2c009bSjjc if (!node_memory[j].exists) 15912e2c009bSjjc continue; 15922e2c009bSjjc t1 = lat_stats->latencies[i][j]; 15932e2c009bSjjc t2 = lat_stats->latencies[j][i]; 15942e2c009bSjjc 15952e2c009bSjjc if (t1 == 0 || t2 == 0 || t1 == t2) 15962e2c009bSjjc continue; 15972e2c009bSjjc 15982e2c009bSjjc return (-1); 15992e2c009bSjjc } 16002e2c009bSjjc } 16012e2c009bSjjc 16022e2c009bSjjc /* 16032e2c009bSjjc * Local latencies should be same 16042e2c009bSjjc */ 16052e2c009bSjjc t1 = lat_stats->latencies[0][0]; 16062e2c009bSjjc for (i = 1; i < lgrp_plat_node_cnt; i++) { 16072e2c009bSjjc if (!node_memory[i].exists) 16082e2c009bSjjc continue; 16092e2c009bSjjc 16102e2c009bSjjc t2 = lat_stats->latencies[i][i]; 16112e2c009bSjjc if (t2 == 0) 16122e2c009bSjjc continue; 16132e2c009bSjjc 16142e2c009bSjjc if (t1 == 0) { 16152e2c009bSjjc t1 = t2; 16162e2c009bSjjc continue; 16172e2c009bSjjc } 16182e2c009bSjjc 16192e2c009bSjjc if (t1 != t2) 16202e2c009bSjjc return (-2); 16212e2c009bSjjc } 16222e2c009bSjjc 16232e2c009bSjjc /* 16242e2c009bSjjc * Local latencies should be less than remote 16252e2c009bSjjc */ 16262e2c009bSjjc if (t1) { 16272e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 16282e2c009bSjjc if (!node_memory[i].exists) 16292e2c009bSjjc continue; 16302e2c009bSjjc for (j = 0; j < lgrp_plat_node_cnt; j++) { 16312e2c009bSjjc if (!node_memory[j].exists) 16322e2c009bSjjc continue; 16332e2c009bSjjc t2 = lat_stats->latencies[i][j]; 16342e2c009bSjjc if (i == j || t2 == 0) 16352e2c009bSjjc continue; 16362e2c009bSjjc 16372e2c009bSjjc if (t1 >= t2) 16382e2c009bSjjc return (-3); 16392e2c009bSjjc } 16402e2c009bSjjc } 16412e2c009bSjjc } 16422e2c009bSjjc 16432e2c009bSjjc return (0); 16442e2c009bSjjc } 16452e2c009bSjjc 16462e2c009bSjjc 16472e2c009bSjjc /* 16482e2c009bSjjc * Return the number of free, allocatable, or installed 16492e2c009bSjjc * pages in an lgroup 16502e2c009bSjjc * This is a copy of the MAX_MEM_NODES == 1 version of the routine 16512e2c009bSjjc * used when MPO is disabled (i.e. single lgroup) or this is the root lgroup 16522e2c009bSjjc */ 16532e2c009bSjjc /* ARGSUSED */ 16542e2c009bSjjc static pgcnt_t 16552e2c009bSjjc lgrp_plat_mem_size_default(lgrp_handle_t lgrphand, lgrp_mem_query_t query) 16562e2c009bSjjc { 16572e2c009bSjjc struct memlist *mlist; 16582e2c009bSjjc pgcnt_t npgs = 0; 16592e2c009bSjjc extern struct memlist *phys_avail; 16602e2c009bSjjc extern struct memlist *phys_install; 16612e2c009bSjjc 16622e2c009bSjjc switch (query) { 16632e2c009bSjjc case LGRP_MEM_SIZE_FREE: 16642e2c009bSjjc return ((pgcnt_t)freemem); 16652e2c009bSjjc case LGRP_MEM_SIZE_AVAIL: 16662e2c009bSjjc memlist_read_lock(); 16672e2c009bSjjc for (mlist = phys_avail; mlist; mlist = mlist->next) 16682e2c009bSjjc npgs += btop(mlist->size); 16692e2c009bSjjc memlist_read_unlock(); 16702e2c009bSjjc return (npgs); 16712e2c009bSjjc case LGRP_MEM_SIZE_INSTALL: 16722e2c009bSjjc memlist_read_lock(); 16732e2c009bSjjc for (mlist = phys_install; mlist; mlist = mlist->next) 16742e2c009bSjjc npgs += btop(mlist->size); 16752e2c009bSjjc memlist_read_unlock(); 16762e2c009bSjjc return (npgs); 16772e2c009bSjjc default: 16782e2c009bSjjc return ((pgcnt_t)0); 16792e2c009bSjjc } 16802e2c009bSjjc } 16812e2c009bSjjc 16822e2c009bSjjc 16832e2c009bSjjc /* 16842e2c009bSjjc * Update node to proximity domain mappings for given domain and return node ID 16852e2c009bSjjc */ 16862e2c009bSjjc static int 16872e2c009bSjjc lgrp_plat_node_domain_update(node_domain_map_t *node_domain, uint32_t domain) 16882e2c009bSjjc { 16892e2c009bSjjc uint_t node; 16902e2c009bSjjc uint_t start; 16912e2c009bSjjc 16922e2c009bSjjc /* 16932e2c009bSjjc * Hash proximity domain ID into node to domain mapping table (array) 16942e2c009bSjjc * and add entry for it into first non-existent or matching entry found 16952e2c009bSjjc */ 16962e2c009bSjjc node = start = NODE_DOMAIN_HASH(domain); 16972e2c009bSjjc do { 16982e2c009bSjjc /* 16992e2c009bSjjc * Entry doesn't exist yet, so create one for this proximity 17002e2c009bSjjc * domain and return node ID which is index into mapping table. 17012e2c009bSjjc */ 17022e2c009bSjjc if (!node_domain[node].exists) { 17032e2c009bSjjc node_domain[node].exists = 1; 17042e2c009bSjjc node_domain[node].prox_domain = domain; 17052e2c009bSjjc return (node); 17062e2c009bSjjc } 17072e2c009bSjjc 17082e2c009bSjjc /* 17092e2c009bSjjc * Entry exists for this proximity domain already, so just 17102e2c009bSjjc * return node ID (index into table). 17112e2c009bSjjc */ 17122e2c009bSjjc if (node_domain[node].prox_domain == domain) 17132e2c009bSjjc return (node); 17142e2c009bSjjc node = NODE_DOMAIN_HASH(node + 1); 17152e2c009bSjjc } while (node != start); 17162e2c009bSjjc 17172e2c009bSjjc /* 17182e2c009bSjjc * Ran out of supported number of entries which shouldn't happen.... 17192e2c009bSjjc */ 17202e2c009bSjjc ASSERT(node != start); 17212e2c009bSjjc return (-1); 17222e2c009bSjjc } 17232e2c009bSjjc 17242e2c009bSjjc 17252e2c009bSjjc /* 17262e2c009bSjjc * Update node memory information for given proximity domain with specified 17272e2c009bSjjc * starting and ending physical address range (and return positive numbers for 17282e2c009bSjjc * success and negative ones for errors) 17292e2c009bSjjc */ 17302e2c009bSjjc static int 17312e2c009bSjjc lgrp_plat_node_memory_update(node_domain_map_t *node_domain, 1732*e9dd3ea3Sjjc node_phys_addr_map_t *node_memory, uint64_t start, uint64_t end, 17332e2c009bSjjc uint32_t domain) 17342e2c009bSjjc { 17352e2c009bSjjc int node; 17362e2c009bSjjc 17372e2c009bSjjc /* 17382e2c009bSjjc * Get node number for proximity domain 17392e2c009bSjjc */ 17402e2c009bSjjc node = lgrp_plat_domain_to_node(node_domain, domain); 17412e2c009bSjjc if (node == -1) { 17422e2c009bSjjc node = lgrp_plat_node_domain_update(node_domain, domain); 17432e2c009bSjjc if (node == -1) 17442e2c009bSjjc return (-1); 17452e2c009bSjjc } 17462e2c009bSjjc 17472e2c009bSjjc /* 17482e2c009bSjjc * Create entry in table for node if it doesn't exist 17492e2c009bSjjc */ 17502e2c009bSjjc if (!node_memory[node].exists) { 17512e2c009bSjjc node_memory[node].exists = 1; 17522e2c009bSjjc node_memory[node].start = btop(start); 17532e2c009bSjjc node_memory[node].end = btop(end); 17542e2c009bSjjc node_memory[node].prox_domain = domain; 17552e2c009bSjjc return (0); 17562e2c009bSjjc } 17572e2c009bSjjc 17582e2c009bSjjc /* 17592e2c009bSjjc * Entry already exists for this proximity domain 17602e2c009bSjjc * 17612e2c009bSjjc * There may be more than one SRAT memory entry for a domain, so we may 17622e2c009bSjjc * need to update existing start or end address for the node. 17632e2c009bSjjc */ 17642e2c009bSjjc if (node_memory[node].prox_domain == domain) { 17652e2c009bSjjc if (btop(start) < node_memory[node].start) 17662e2c009bSjjc node_memory[node].start = btop(start); 17672e2c009bSjjc if (btop(end) > node_memory[node].end) 17682e2c009bSjjc node_memory[node].end = btop(end); 17692e2c009bSjjc return (1); 17702e2c009bSjjc } 17712e2c009bSjjc return (-2); 17722e2c009bSjjc } 17732e2c009bSjjc 17742e2c009bSjjc 17752e2c009bSjjc /* 17762e2c009bSjjc * Return time needed to probe from current CPU to memory in given node 17772e2c009bSjjc */ 17782e2c009bSjjc static hrtime_t 17792e2c009bSjjc lgrp_plat_probe_time(int to, cpu_node_map_t *cpu_node, 17802e2c009bSjjc lgrp_plat_probe_mem_config_t *probe_mem_config, 17812e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats, lgrp_plat_probe_stats_t *probe_stats) 17822e2c009bSjjc { 17832e2c009bSjjc caddr_t buf; 17842e2c009bSjjc hrtime_t elapsed; 17852e2c009bSjjc hrtime_t end; 17862e2c009bSjjc int from; 17872e2c009bSjjc int i; 17882e2c009bSjjc int ipl; 17892e2c009bSjjc hrtime_t max; 17902e2c009bSjjc hrtime_t min; 17912e2c009bSjjc hrtime_t start; 17922e2c009bSjjc extern int use_sse_pagecopy; 17932e2c009bSjjc 17942e2c009bSjjc /* 17952e2c009bSjjc * Determine ID of node containing current CPU 17962e2c009bSjjc */ 17972e2c009bSjjc from = lgrp_plat_cpu_to_node(CPU, cpu_node); 17982e2c009bSjjc ASSERT(from >= 0 && from < lgrp_plat_node_cnt); 17992e2c009bSjjc 18002e2c009bSjjc /* 18012e2c009bSjjc * Do common work for probing main memory 18022e2c009bSjjc */ 18032e2c009bSjjc if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_PGCPY) { 18042e2c009bSjjc /* 18052e2c009bSjjc * Skip probing any nodes without memory and 18062e2c009bSjjc * set probe time to 0 18072e2c009bSjjc */ 18082e2c009bSjjc if (probe_mem_config->probe_va[to] == NULL) { 18092e2c009bSjjc lat_stats->latencies[from][to] = 0; 18102e2c009bSjjc return (0); 18112e2c009bSjjc } 18122e2c009bSjjc 18132e2c009bSjjc /* 18142e2c009bSjjc * Invalidate caches once instead of once every sample 18152e2c009bSjjc * which should cut cost of probing by a lot 18162e2c009bSjjc */ 18172e2c009bSjjc probe_stats->flush_cost = gethrtime(); 18182e2c009bSjjc invalidate_cache(); 18192e2c009bSjjc probe_stats->flush_cost = gethrtime() - 18202e2c009bSjjc probe_stats->flush_cost; 18212e2c009bSjjc probe_stats->probe_cost_total += probe_stats->flush_cost; 18222e2c009bSjjc } 18232e2c009bSjjc 18242e2c009bSjjc /* 18252e2c009bSjjc * Probe from current CPU to given memory using specified operation 18262e2c009bSjjc * and take specified number of samples 18272e2c009bSjjc */ 18282e2c009bSjjc max = 0; 18292e2c009bSjjc min = -1; 18302e2c009bSjjc for (i = 0; i < lgrp_plat_probe_nsamples; i++) { 18312e2c009bSjjc probe_stats->probe_cost = gethrtime(); 18322e2c009bSjjc 18332e2c009bSjjc /* 18342e2c009bSjjc * Can't measure probe time if gethrtime() isn't working yet 18352e2c009bSjjc */ 18362e2c009bSjjc if (probe_stats->probe_cost == 0 && gethrtime() == 0) 18372e2c009bSjjc return (0); 18382e2c009bSjjc 18392e2c009bSjjc if (lgrp_plat_probe_flags & LGRP_PLAT_PROBE_VENDOR) { 18402e2c009bSjjc /* 18412e2c009bSjjc * Measure how long it takes to read vendor ID from 18422e2c009bSjjc * Northbridge 18432e2c009bSjjc */ 18442e2c009bSjjc elapsed = opt_probe_vendor(to, lgrp_plat_probe_nreads); 18452e2c009bSjjc } else { 18462e2c009bSjjc /* 18472e2c009bSjjc * Measure how long it takes to copy page 18482e2c009bSjjc * on top of itself 18492e2c009bSjjc */ 18502e2c009bSjjc buf = probe_mem_config->probe_va[to] + (i * PAGESIZE); 18512e2c009bSjjc 18522e2c009bSjjc kpreempt_disable(); 18532e2c009bSjjc ipl = splhigh(); 18542e2c009bSjjc start = gethrtime(); 18552e2c009bSjjc if (use_sse_pagecopy) 18562e2c009bSjjc hwblkpagecopy(buf, buf); 18572e2c009bSjjc else 18582e2c009bSjjc bcopy(buf, buf, PAGESIZE); 18592e2c009bSjjc end = gethrtime(); 18602e2c009bSjjc elapsed = end - start; 18612e2c009bSjjc splx(ipl); 18622e2c009bSjjc kpreempt_enable(); 18632e2c009bSjjc } 18642e2c009bSjjc 18652e2c009bSjjc probe_stats->probe_cost = gethrtime() - 18662e2c009bSjjc probe_stats->probe_cost; 18672e2c009bSjjc probe_stats->probe_cost_total += probe_stats->probe_cost; 18682e2c009bSjjc 18692e2c009bSjjc if (min == -1 || elapsed < min) 18702e2c009bSjjc min = elapsed; 18712e2c009bSjjc if (elapsed > max) 18722e2c009bSjjc max = elapsed; 18732e2c009bSjjc } 18742e2c009bSjjc 18752e2c009bSjjc /* 18762e2c009bSjjc * Update minimum and maximum probe times between 18772e2c009bSjjc * these two nodes 18782e2c009bSjjc */ 18792e2c009bSjjc if (min < probe_stats->probe_min[from][to] || 18802e2c009bSjjc probe_stats->probe_min[from][to] == 0) 18812e2c009bSjjc probe_stats->probe_min[from][to] = min; 18822e2c009bSjjc 18832e2c009bSjjc if (max > probe_stats->probe_max[from][to]) 18842e2c009bSjjc probe_stats->probe_max[from][to] = max; 18852e2c009bSjjc 18862e2c009bSjjc return (min); 18872e2c009bSjjc } 18882e2c009bSjjc 18892e2c009bSjjc 18902e2c009bSjjc /* 18912e2c009bSjjc * Read ACPI System Locality Information Table (SLIT) to determine how far each 18922e2c009bSjjc * NUMA node is from each other 18932e2c009bSjjc */ 18942e2c009bSjjc static int 18952e2c009bSjjc lgrp_plat_process_slit(struct slit *tp, uint_t node_cnt, 18962e2c009bSjjc node_phys_addr_map_t *node_memory, lgrp_plat_latency_stats_t *lat_stats) 18972e2c009bSjjc { 18982e2c009bSjjc int i; 18992e2c009bSjjc int j; 19002e2c009bSjjc int localities; 19012e2c009bSjjc hrtime_t max; 19022e2c009bSjjc hrtime_t min; 19032e2c009bSjjc int retval; 19042e2c009bSjjc uint8_t *slit_entries; 19052e2c009bSjjc 19062e2c009bSjjc if (tp == NULL || !lgrp_plat_slit_enable) 19072e2c009bSjjc return (1); 19082e2c009bSjjc 19092e2c009bSjjc if (lat_stats == NULL) 19102e2c009bSjjc return (2); 19112e2c009bSjjc 19122e2c009bSjjc localities = tp->number; 19132e2c009bSjjc if (localities != node_cnt) 19142e2c009bSjjc return (3); 19152e2c009bSjjc 19162e2c009bSjjc min = lat_stats->latency_min; 19172e2c009bSjjc max = lat_stats->latency_max; 19182e2c009bSjjc 19192e2c009bSjjc /* 19202e2c009bSjjc * Fill in latency matrix based on SLIT entries 19212e2c009bSjjc */ 19222e2c009bSjjc slit_entries = tp->entry; 19232e2c009bSjjc for (i = 0; i < localities; i++) { 19242e2c009bSjjc for (j = 0; j < localities; j++) { 19252e2c009bSjjc uint8_t latency; 19262e2c009bSjjc 19272e2c009bSjjc latency = slit_entries[(i * localities) + j]; 19282e2c009bSjjc lat_stats->latencies[i][j] = latency; 19292e2c009bSjjc if (latency < min) 19302e2c009bSjjc min = latency; 19312e2c009bSjjc if (latency > max) 19322e2c009bSjjc max = latency; 19332e2c009bSjjc } 19342e2c009bSjjc } 19352e2c009bSjjc 19362e2c009bSjjc /* 19372e2c009bSjjc * Verify that latencies/distances given in SLIT look reasonable 19382e2c009bSjjc */ 19392e2c009bSjjc retval = lgrp_plat_latency_verify(node_memory, lat_stats); 19402e2c009bSjjc 19412e2c009bSjjc if (retval) { 19422e2c009bSjjc /* 19432e2c009bSjjc * Reinitialize (zero) latency table since SLIT doesn't look 19442e2c009bSjjc * right 19452e2c009bSjjc */ 19462e2c009bSjjc for (i = 0; i < localities; i++) { 19472e2c009bSjjc for (j = 0; j < localities; j++) 19482e2c009bSjjc lat_stats->latencies[i][j] = 0; 19492e2c009bSjjc } 19502e2c009bSjjc } else { 19512e2c009bSjjc /* 19522e2c009bSjjc * Update min and max latencies seen since SLIT looks valid 19532e2c009bSjjc */ 19542e2c009bSjjc lat_stats->latency_min = min; 19552e2c009bSjjc lat_stats->latency_max = max; 19562e2c009bSjjc } 19572e2c009bSjjc 19582e2c009bSjjc return (retval); 19592e2c009bSjjc } 19602e2c009bSjjc 19612e2c009bSjjc 19622e2c009bSjjc /* 19632e2c009bSjjc * Read ACPI System Resource Affinity Table (SRAT) to determine which CPUs 19642e2c009bSjjc * and memory are local to each other in the same NUMA node 19652e2c009bSjjc */ 19662e2c009bSjjc static int 19672e2c009bSjjc lgrp_plat_process_srat(struct srat *tp, uint_t *node_cnt, 19682e2c009bSjjc node_domain_map_t *node_domain, cpu_node_map_t *cpu_node, 19692e2c009bSjjc node_phys_addr_map_t *node_memory) 19702e2c009bSjjc { 19712e2c009bSjjc struct srat_item *end; 19722e2c009bSjjc int i; 19732e2c009bSjjc struct srat_item *item; 19742e2c009bSjjc 19752e2c009bSjjc if (tp == NULL || !lgrp_plat_srat_enable) 19762e2c009bSjjc return (1); 19772e2c009bSjjc 19782e2c009bSjjc /* 19792e2c009bSjjc * Determine number of nodes by counting number of proximity domains in 19802e2c009bSjjc * SRAT 19812e2c009bSjjc */ 19822e2c009bSjjc if (node_cnt) { 19832e2c009bSjjc int nodes; 19842e2c009bSjjc 19852e2c009bSjjc nodes = lgrp_plat_srat_domains(tp); 19862e2c009bSjjc if (nodes < 0) { 19872e2c009bSjjc *node_cnt = 1; 19882e2c009bSjjc return (2); 19892e2c009bSjjc } 19902e2c009bSjjc *node_cnt = nodes; 19912e2c009bSjjc } 19922e2c009bSjjc 19932e2c009bSjjc /* 19942e2c009bSjjc * Walk through SRAT, examining each CPU and memory entry to determine 19952e2c009bSjjc * which CPUs and memory belong to which node. 19962e2c009bSjjc */ 19972e2c009bSjjc item = tp->list; 19982e2c009bSjjc end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp); 19992e2c009bSjjc while (item < end) { 20002e2c009bSjjc uint32_t apic_id; 20012e2c009bSjjc uint32_t domain; 20022e2c009bSjjc uint64_t end; 20032e2c009bSjjc uint64_t length; 20042e2c009bSjjc uint64_t start; 20052e2c009bSjjc 20062e2c009bSjjc switch (item->type) { 20072e2c009bSjjc case SRAT_PROCESSOR: /* CPU entry */ 20082e2c009bSjjc if (!(item->i.p.flags & SRAT_ENABLED) || 20092e2c009bSjjc cpu_node == NULL) 20102e2c009bSjjc break; 20112e2c009bSjjc 20122e2c009bSjjc /* 20132e2c009bSjjc * Calculate domain (node) ID and fill in APIC ID to 20142e2c009bSjjc * domain/node mapping table 20152e2c009bSjjc */ 20162e2c009bSjjc domain = item->i.p.domain1; 20172e2c009bSjjc for (i = 0; i < 3; i++) { 20182e2c009bSjjc domain += item->i.p.domain2[i] << 20192e2c009bSjjc ((i + 1) * 8); 20202e2c009bSjjc } 20212e2c009bSjjc apic_id = item->i.p.apic_id; 20222e2c009bSjjc 20232e2c009bSjjc if (lgrp_plat_cpu_node_update(node_domain, cpu_node, 20242e2c009bSjjc apic_id, domain) < 0) 20252e2c009bSjjc return (3); 20262e2c009bSjjc break; 20272e2c009bSjjc 20282e2c009bSjjc case SRAT_MEMORY: /* memory entry */ 20292e2c009bSjjc if (!(item->i.m.flags & SRAT_ENABLED) || 20302e2c009bSjjc node_memory == NULL) 20312e2c009bSjjc break; 20322e2c009bSjjc 20332e2c009bSjjc /* 20342e2c009bSjjc * Get domain (node) ID and fill in domain/node 20352e2c009bSjjc * to memory mapping table 20362e2c009bSjjc */ 20372e2c009bSjjc domain = item->i.m.domain; 20382e2c009bSjjc start = item->i.m.base_addr; 20392e2c009bSjjc length = item->i.m.len; 20402e2c009bSjjc end = start + length - 1; 20412e2c009bSjjc 20422e2c009bSjjc if (lgrp_plat_node_memory_update(node_domain, 20432e2c009bSjjc node_memory, start, end, domain) < 0) 20442e2c009bSjjc return (4); 20452e2c009bSjjc break; 20462e2c009bSjjc 20472e2c009bSjjc default: 20482e2c009bSjjc break; 20492e2c009bSjjc } 20502e2c009bSjjc 20512e2c009bSjjc item = (struct srat_item *)((uintptr_t)item + item->len); 20522e2c009bSjjc } 20532e2c009bSjjc return (0); 20542e2c009bSjjc } 20552e2c009bSjjc 20562e2c009bSjjc 20572e2c009bSjjc /* 20582e2c009bSjjc * Return number of proximity domains given in ACPI SRAT 20592e2c009bSjjc */ 20602e2c009bSjjc static int 20612e2c009bSjjc lgrp_plat_srat_domains(struct srat *tp) 20622e2c009bSjjc { 20632e2c009bSjjc int domain_cnt; 20642e2c009bSjjc struct srat_item *end; 20652e2c009bSjjc int i; 20662e2c009bSjjc struct srat_item *item; 20672e2c009bSjjc node_domain_map_t node_domain[MAX_NODES]; 20682e2c009bSjjc 20692e2c009bSjjc 20702e2c009bSjjc if (tp == NULL || !lgrp_plat_srat_enable) 20712e2c009bSjjc return (1); 20722e2c009bSjjc 20732e2c009bSjjc /* 20742e2c009bSjjc * Walk through SRAT, examining each CPU and memory entry to determine 20752e2c009bSjjc * proximity domain ID for each. 20762e2c009bSjjc */ 20772e2c009bSjjc domain_cnt = 0; 20782e2c009bSjjc item = tp->list; 20792e2c009bSjjc end = (struct srat_item *)(tp->hdr.len + (uintptr_t)tp); 20802e2c009bSjjc bzero(node_domain, MAX_NODES * sizeof (node_domain_map_t)); 20812e2c009bSjjc while (item < end) { 20822e2c009bSjjc uint32_t domain; 20832e2c009bSjjc boolean_t overflow; 20842e2c009bSjjc uint_t start; 20852e2c009bSjjc 20862e2c009bSjjc switch (item->type) { 20872e2c009bSjjc case SRAT_PROCESSOR: /* CPU entry */ 20882e2c009bSjjc if (!(item->i.p.flags & SRAT_ENABLED)) 20892e2c009bSjjc break; 20902e2c009bSjjc domain = item->i.p.domain1; 20912e2c009bSjjc for (i = 0; i < 3; i++) { 20922e2c009bSjjc domain += item->i.p.domain2[i] << 20932e2c009bSjjc ((i + 1) * 8); 20942e2c009bSjjc } 20952e2c009bSjjc break; 20962e2c009bSjjc 20972e2c009bSjjc case SRAT_MEMORY: /* memory entry */ 20982e2c009bSjjc if (!(item->i.m.flags & SRAT_ENABLED)) 20992e2c009bSjjc break; 21002e2c009bSjjc domain = item->i.m.domain; 21012e2c009bSjjc break; 21022e2c009bSjjc 21032e2c009bSjjc default: 21042e2c009bSjjc break; 21052e2c009bSjjc } 21062e2c009bSjjc 21072e2c009bSjjc /* 21082e2c009bSjjc * Count and keep track of which proximity domain IDs seen 21092e2c009bSjjc */ 21102e2c009bSjjc start = i = domain % MAX_NODES; 21112e2c009bSjjc overflow = B_TRUE; 21122e2c009bSjjc do { 21132e2c009bSjjc /* 21142e2c009bSjjc * Create entry for proximity domain and increment 21152e2c009bSjjc * count when no entry exists where proximity domain 21162e2c009bSjjc * hashed 21172e2c009bSjjc */ 21182e2c009bSjjc if (!node_domain[i].exists) { 21192e2c009bSjjc node_domain[i].exists = 1; 21202e2c009bSjjc node_domain[i].prox_domain = domain; 21212e2c009bSjjc domain_cnt++; 21222e2c009bSjjc overflow = B_FALSE; 21232e2c009bSjjc break; 21242e2c009bSjjc } 21252e2c009bSjjc 21262e2c009bSjjc /* 21272e2c009bSjjc * Nothing to do when proximity domain seen already 21282e2c009bSjjc * and its entry exists 21292e2c009bSjjc */ 21302e2c009bSjjc if (node_domain[i].prox_domain == domain) { 21312e2c009bSjjc overflow = B_FALSE; 21322e2c009bSjjc break; 21332e2c009bSjjc } 21342e2c009bSjjc 21352e2c009bSjjc /* 21362e2c009bSjjc * Entry exists where proximity domain hashed, but for 21372e2c009bSjjc * different proximity domain so keep search for empty 21382e2c009bSjjc * slot to put it or matching entry whichever comes 21392e2c009bSjjc * first. 21402e2c009bSjjc */ 21412e2c009bSjjc i = (i + 1) % MAX_NODES; 21422e2c009bSjjc } while (i != start); 21432e2c009bSjjc 21442e2c009bSjjc /* 21452e2c009bSjjc * Didn't find empty or matching entry which means have more 21462e2c009bSjjc * proximity domains than supported nodes (:-( 21472e2c009bSjjc */ 21482e2c009bSjjc ASSERT(overflow != B_TRUE); 21492e2c009bSjjc if (overflow == B_TRUE) 21502e2c009bSjjc return (-1); 21512e2c009bSjjc 21522e2c009bSjjc item = (struct srat_item *)((uintptr_t)item + item->len); 21532e2c009bSjjc } 21542e2c009bSjjc return (domain_cnt); 21552e2c009bSjjc } 21562e2c009bSjjc 21572e2c009bSjjc 21582e2c009bSjjc /* 21592e2c009bSjjc * Set lgroup latencies for 2 level lgroup topology 21602e2c009bSjjc */ 21612e2c009bSjjc static void 21622e2c009bSjjc lgrp_plat_2level_setup(node_phys_addr_map_t *node_memory, 21632e2c009bSjjc lgrp_plat_latency_stats_t *lat_stats) 21642e2c009bSjjc { 21652e2c009bSjjc int i; 21662e2c009bSjjc 21672e2c009bSjjc ASSERT(node_memory != NULL && lat_stats != NULL); 21682e2c009bSjjc 21692e2c009bSjjc if (lgrp_plat_node_cnt >= 4) 21702e2c009bSjjc cmn_err(CE_NOTE, 21712e2c009bSjjc "MPO only optimizing for local and remote\n"); 21722e2c009bSjjc for (i = 0; i < lgrp_plat_node_cnt; i++) { 21732e2c009bSjjc int j; 21742e2c009bSjjc 21752e2c009bSjjc if (!node_memory[i].exists) 21762e2c009bSjjc continue; 21772e2c009bSjjc for (j = 0; j < lgrp_plat_node_cnt; j++) { 21782e2c009bSjjc if (!node_memory[j].exists) 21792e2c009bSjjc continue; 21802e2c009bSjjc if (i == j) 21812e2c009bSjjc lat_stats->latencies[i][j] = 2; 21822e2c009bSjjc else 21832e2c009bSjjc lat_stats->latencies[i][j] = 3; 21842e2c009bSjjc } 21852e2c009bSjjc } 21862e2c009bSjjc lat_stats->latency_min = 2; 21872e2c009bSjjc lat_stats->latency_max = 3; 21882e2c009bSjjc lgrp_config(LGRP_CONFIG_FLATTEN, 2, 0); 21892e2c009bSjjc } 21902e2c009bSjjc 21912e2c009bSjjc 21922e2c009bSjjc /* 21932e2c009bSjjc * The following Opteron specific constants, macros, types, and routines define 21942e2c009bSjjc * PCI configuration space registers and how to read them to determine the NUMA 21952e2c009bSjjc * configuration of *supported* Opteron processors. They provide the same 21962e2c009bSjjc * information that may be gotten from the ACPI System Resource Affinity Table 21972e2c009bSjjc * (SRAT) if it exists on the machine of interest. 21982e2c009bSjjc * 21992e2c009bSjjc * The AMD BIOS and Kernel Developer's Guide (BKDG) for the processor family 22002e2c009bSjjc * of interest describes all of these registers and their contents. The main 22012e2c009bSjjc * registers used by this code to determine the NUMA configuration of the 22022e2c009bSjjc * machine are the node ID register for the number of NUMA nodes and the DRAM 22032e2c009bSjjc * address map registers for the physical address range of each node. 22042e2c009bSjjc * 22052e2c009bSjjc * NOTE: The format and how to determine the NUMA configuration using PCI 22062e2c009bSjjc * config space registers may change or may not be supported in future 22072e2c009bSjjc * Opteron processor families. 22087c478bd9Sstevel@tonic-gate */ 22097c478bd9Sstevel@tonic-gate 22107c478bd9Sstevel@tonic-gate /* 22117c478bd9Sstevel@tonic-gate * How many bits to shift Opteron DRAM Address Map base and limit registers 22127c478bd9Sstevel@tonic-gate * to get actual value 22137c478bd9Sstevel@tonic-gate */ 2214f78a91cdSjjc #define OPT_DRAMADDR_HI_LSHIFT_ADDR 40 /* shift left for address */ 2215f78a91cdSjjc #define OPT_DRAMADDR_LO_LSHIFT_ADDR 8 /* shift left for address */ 22167c478bd9Sstevel@tonic-gate 2217f78a91cdSjjc #define OPT_DRAMADDR_HI_MASK_ADDR 0x000000FF /* address bits 47-40 */ 2218f78a91cdSjjc #define OPT_DRAMADDR_LO_MASK_ADDR 0xFFFF0000 /* address bits 39-24 */ 2219f78a91cdSjjc 2220f78a91cdSjjc #define OPT_DRAMADDR_LO_MASK_OFF 0xFFFFFF /* offset for address */ 2221f78a91cdSjjc 2222f78a91cdSjjc /* 2223f78a91cdSjjc * Macros to derive addresses from Opteron DRAM Address Map registers 2224f78a91cdSjjc */ 2225f78a91cdSjjc #define OPT_DRAMADDR_HI(reg) \ 2226f78a91cdSjjc (((u_longlong_t)reg & OPT_DRAMADDR_HI_MASK_ADDR) << \ 2227f78a91cdSjjc OPT_DRAMADDR_HI_LSHIFT_ADDR) 2228f78a91cdSjjc 2229f78a91cdSjjc #define OPT_DRAMADDR_LO(reg) \ 2230f78a91cdSjjc (((u_longlong_t)reg & OPT_DRAMADDR_LO_MASK_ADDR) << \ 2231f78a91cdSjjc OPT_DRAMADDR_LO_LSHIFT_ADDR) 2232f78a91cdSjjc 2233f78a91cdSjjc #define OPT_DRAMADDR(high, low) \ 2234f78a91cdSjjc (OPT_DRAMADDR_HI(high) | OPT_DRAMADDR_LO(low)) 22357c478bd9Sstevel@tonic-gate 22367c478bd9Sstevel@tonic-gate /* 22377c478bd9Sstevel@tonic-gate * Bit masks defining what's in Opteron DRAM Address Map base register 22387c478bd9Sstevel@tonic-gate */ 2239f78a91cdSjjc #define OPT_DRAMBASE_LO_MASK_RE 0x1 /* read enable */ 2240f78a91cdSjjc #define OPT_DRAMBASE_LO_MASK_WE 0x2 /* write enable */ 2241f78a91cdSjjc #define OPT_DRAMBASE_LO_MASK_INTRLVEN 0x700 /* interleave */ 22427c478bd9Sstevel@tonic-gate 22437c478bd9Sstevel@tonic-gate /* 22447c478bd9Sstevel@tonic-gate * Bit masks defining what's in Opteron DRAM Address Map limit register 22457c478bd9Sstevel@tonic-gate */ 2246f78a91cdSjjc #define OPT_DRAMLIMIT_LO_MASK_DSTNODE 0x7 /* destination node */ 2247f78a91cdSjjc #define OPT_DRAMLIMIT_LO_MASK_INTRLVSEL 0x700 /* interleave select */ 22487c478bd9Sstevel@tonic-gate 22497c478bd9Sstevel@tonic-gate 22507c478bd9Sstevel@tonic-gate /* 22517c478bd9Sstevel@tonic-gate * Opteron Node ID register in PCI configuration space contains 22527c478bd9Sstevel@tonic-gate * number of nodes in system, etc. for Opteron K8. The following 22537c478bd9Sstevel@tonic-gate * constants and macros define its contents, structure, and access. 22547c478bd9Sstevel@tonic-gate */ 22557c478bd9Sstevel@tonic-gate 22567c478bd9Sstevel@tonic-gate /* 22577c478bd9Sstevel@tonic-gate * Bit masks defining what's in Opteron Node ID register 22587c478bd9Sstevel@tonic-gate */ 22597c478bd9Sstevel@tonic-gate #define OPT_NODE_MASK_ID 0x7 /* node ID */ 22607c478bd9Sstevel@tonic-gate #define OPT_NODE_MASK_CNT 0x70 /* node count */ 22617c478bd9Sstevel@tonic-gate #define OPT_NODE_MASK_IONODE 0x700 /* Hypertransport I/O hub node ID */ 22627c478bd9Sstevel@tonic-gate #define OPT_NODE_MASK_LCKNODE 0x7000 /* lock controller node ID */ 22637c478bd9Sstevel@tonic-gate #define OPT_NODE_MASK_CPUCNT 0xF0000 /* CPUs in system (0 means 1 CPU) */ 22647c478bd9Sstevel@tonic-gate 22657c478bd9Sstevel@tonic-gate /* 22667c478bd9Sstevel@tonic-gate * How many bits in Opteron Node ID register to shift right to get actual value 22677c478bd9Sstevel@tonic-gate */ 22687c478bd9Sstevel@tonic-gate #define OPT_NODE_RSHIFT_CNT 0x4 /* shift right for node count value */ 22697c478bd9Sstevel@tonic-gate 22707c478bd9Sstevel@tonic-gate /* 22717c478bd9Sstevel@tonic-gate * Macros to get values from Opteron Node ID register 22727c478bd9Sstevel@tonic-gate */ 22737c478bd9Sstevel@tonic-gate #define OPT_NODE_CNT(reg) \ 22747c478bd9Sstevel@tonic-gate ((reg & OPT_NODE_MASK_CNT) >> OPT_NODE_RSHIFT_CNT) 22757c478bd9Sstevel@tonic-gate 2276f78a91cdSjjc /* 2277f78a91cdSjjc * Macro to setup PCI Extended Configuration Space (ECS) address to give to 2278f78a91cdSjjc * "in/out" instructions 2279f78a91cdSjjc * 2280f78a91cdSjjc * NOTE: Should only be used in lgrp_plat_init() before MMIO setup because any 2281f78a91cdSjjc * other uses should just do MMIO to access PCI ECS. 2282f78a91cdSjjc * Must enable special bit in Northbridge Configuration Register on 2283f78a91cdSjjc * Greyhound for extended CF8 space access to be able to access PCI ECS 2284f78a91cdSjjc * using "in/out" instructions and restore special bit after done 2285f78a91cdSjjc * accessing PCI ECS. 2286f78a91cdSjjc */ 2287f78a91cdSjjc #define OPT_PCI_ECS_ADDR(bus, device, function, reg) \ 2288f78a91cdSjjc (PCI_CONE | (((bus) & 0xff) << 16) | (((device & 0x1f)) << 11) | \ 2289f78a91cdSjjc (((function) & 0x7) << 8) | ((reg) & 0xfc) | \ 2290f78a91cdSjjc ((((reg) >> 8) & 0xf) << 24)) 22917c478bd9Sstevel@tonic-gate 22927c478bd9Sstevel@tonic-gate /* 22937c478bd9Sstevel@tonic-gate * PCI configuration space registers accessed by specifying 22947c478bd9Sstevel@tonic-gate * a bus, device, function, and offset. The following constants 22957c478bd9Sstevel@tonic-gate * define the values needed to access Opteron K8 configuration 22967c478bd9Sstevel@tonic-gate * info to determine its node topology 22977c478bd9Sstevel@tonic-gate */ 22987c478bd9Sstevel@tonic-gate 22997c478bd9Sstevel@tonic-gate #define OPT_PCS_BUS_CONFIG 0 /* Hypertransport config space bus */ 23007c478bd9Sstevel@tonic-gate 23017c478bd9Sstevel@tonic-gate /* 23027c478bd9Sstevel@tonic-gate * Opteron PCI configuration space register function values 23037c478bd9Sstevel@tonic-gate */ 23047c478bd9Sstevel@tonic-gate #define OPT_PCS_FUNC_HT 0 /* Hypertransport configuration */ 23057c478bd9Sstevel@tonic-gate #define OPT_PCS_FUNC_ADDRMAP 1 /* Address map configuration */ 23067c478bd9Sstevel@tonic-gate #define OPT_PCS_FUNC_DRAM 2 /* DRAM configuration */ 23077c478bd9Sstevel@tonic-gate #define OPT_PCS_FUNC_MISC 3 /* Miscellaneous configuration */ 23087c478bd9Sstevel@tonic-gate 23097c478bd9Sstevel@tonic-gate /* 23107c478bd9Sstevel@tonic-gate * PCI Configuration Space register offsets 23117c478bd9Sstevel@tonic-gate */ 23127c478bd9Sstevel@tonic-gate #define OPT_PCS_OFF_VENDOR 0x0 /* device/vendor ID register */ 2313f78a91cdSjjc #define OPT_PCS_OFF_DRAMBASE_HI 0x140 /* DRAM Base register (node 0) */ 2314f78a91cdSjjc #define OPT_PCS_OFF_DRAMBASE_LO 0x40 /* DRAM Base register (node 0) */ 23157c478bd9Sstevel@tonic-gate #define OPT_PCS_OFF_NODEID 0x60 /* Node ID register */ 23167c478bd9Sstevel@tonic-gate 23177c478bd9Sstevel@tonic-gate /* 23187c478bd9Sstevel@tonic-gate * Opteron PCI Configuration Space device IDs for nodes 23197c478bd9Sstevel@tonic-gate */ 23207c478bd9Sstevel@tonic-gate #define OPT_PCS_DEV_NODE0 24 /* device number for node 0 */ 23217c478bd9Sstevel@tonic-gate 23227c478bd9Sstevel@tonic-gate 23237c478bd9Sstevel@tonic-gate /* 23247c478bd9Sstevel@tonic-gate * Opteron DRAM address map gives base and limit for physical memory in a node 23257c478bd9Sstevel@tonic-gate */ 23267c478bd9Sstevel@tonic-gate typedef struct opt_dram_addr_map { 2327f78a91cdSjjc uint32_t base_hi; 2328f78a91cdSjjc uint32_t base_lo; 2329f78a91cdSjjc uint32_t limit_hi; 2330f78a91cdSjjc uint32_t limit_lo; 23317c478bd9Sstevel@tonic-gate } opt_dram_addr_map_t; 23327c478bd9Sstevel@tonic-gate 23337c478bd9Sstevel@tonic-gate 23347c478bd9Sstevel@tonic-gate /* 2335f78a91cdSjjc * Supported AMD processor families 2336f78a91cdSjjc */ 2337f78a91cdSjjc #define AMD_FAMILY_HAMMER 15 2338f78a91cdSjjc #define AMD_FAMILY_GREYHOUND 16 23397c478bd9Sstevel@tonic-gate 2340f78a91cdSjjc /* 23412e2c009bSjjc * Whether to have is_opteron() return 1 even when processor isn't supported 2342f78a91cdSjjc */ 2343f78a91cdSjjc uint_t is_opteron_override = 0; 2344f78a91cdSjjc 2345f78a91cdSjjc /* 2346f78a91cdSjjc * AMD processor family for current CPU 2347f78a91cdSjjc */ 23487c478bd9Sstevel@tonic-gate uint_t opt_family = 0; 2349f78a91cdSjjc 23507c478bd9Sstevel@tonic-gate 23517c478bd9Sstevel@tonic-gate /* 2352f78a91cdSjjc * Determine whether we're running on a supported AMD Opteron since reading 2353f78a91cdSjjc * node count and DRAM address map registers may have different format or 23542e2c009bSjjc * may not be supported across processor families 23557c478bd9Sstevel@tonic-gate */ 23562e2c009bSjjc static int 23577c478bd9Sstevel@tonic-gate is_opteron(void) 23587c478bd9Sstevel@tonic-gate { 2359f78a91cdSjjc 23607c478bd9Sstevel@tonic-gate if (x86_vendor != X86_VENDOR_AMD) 23617c478bd9Sstevel@tonic-gate return (0); 23627c478bd9Sstevel@tonic-gate 2363f78a91cdSjjc opt_family = cpuid_getfamily(CPU); 2364f78a91cdSjjc if (opt_family == AMD_FAMILY_HAMMER || 2365f78a91cdSjjc opt_family == AMD_FAMILY_GREYHOUND || is_opteron_override) 23667c478bd9Sstevel@tonic-gate return (1); 23677c478bd9Sstevel@tonic-gate else 23687c478bd9Sstevel@tonic-gate return (0); 23697c478bd9Sstevel@tonic-gate } 23707c478bd9Sstevel@tonic-gate 23712e2c009bSjjc 23722e2c009bSjjc /* 23732e2c009bSjjc * Determine NUMA configuration for Opteron from registers that live in PCI 23742e2c009bSjjc * configuration space 23752e2c009bSjjc */ 23762e2c009bSjjc static void 23772e2c009bSjjc opt_get_numa_config(uint_t *node_cnt, int *mem_intrlv, 23782e2c009bSjjc node_phys_addr_map_t *node_memory) 23797c478bd9Sstevel@tonic-gate { 23807c478bd9Sstevel@tonic-gate uint_t bus; 23817c478bd9Sstevel@tonic-gate uint_t dev; 23822e2c009bSjjc struct opt_dram_addr_map dram_map[MAX_NODES]; 23837c478bd9Sstevel@tonic-gate uint_t node; 23842e2c009bSjjc uint_t node_info[MAX_NODES]; 2385f78a91cdSjjc uint_t off_hi; 2386f78a91cdSjjc uint_t off_lo; 2387f78a91cdSjjc uint64_t nb_cfg_reg; 23887c478bd9Sstevel@tonic-gate 23897c478bd9Sstevel@tonic-gate /* 23907c478bd9Sstevel@tonic-gate * Read configuration registers from PCI configuration space to 23917c478bd9Sstevel@tonic-gate * determine node information, which memory is in each node, etc. 23927c478bd9Sstevel@tonic-gate * 23937c478bd9Sstevel@tonic-gate * Write to PCI configuration space address register to specify 23947c478bd9Sstevel@tonic-gate * which configuration register to read and read/write PCI 23957c478bd9Sstevel@tonic-gate * configuration space data register to get/set contents 23967c478bd9Sstevel@tonic-gate */ 23977c478bd9Sstevel@tonic-gate bus = OPT_PCS_BUS_CONFIG; 23987c478bd9Sstevel@tonic-gate dev = OPT_PCS_DEV_NODE0; 2399f78a91cdSjjc off_hi = OPT_PCS_OFF_DRAMBASE_HI; 2400f78a91cdSjjc off_lo = OPT_PCS_OFF_DRAMBASE_LO; 24017c478bd9Sstevel@tonic-gate 24027c478bd9Sstevel@tonic-gate /* 24037c478bd9Sstevel@tonic-gate * Read node ID register for node 0 to get node count 24047c478bd9Sstevel@tonic-gate */ 24052e2c009bSjjc node_info[0] = pci_getl_func(bus, dev, OPT_PCS_FUNC_HT, 2406ef50d8c0Sesaxe OPT_PCS_OFF_NODEID); 24072e2c009bSjjc *node_cnt = OPT_NODE_CNT(node_info[0]) + 1; 24082e2c009bSjjc 24092e2c009bSjjc /* 24102e2c009bSjjc * If number of nodes is more than maximum supported, then set node 24112e2c009bSjjc * count to 1 and treat system as UMA instead of NUMA. 24122e2c009bSjjc */ 24132e2c009bSjjc if (*node_cnt > MAX_NODES) { 24142e2c009bSjjc *node_cnt = 1; 24152e2c009bSjjc return; 24162e2c009bSjjc } 24177c478bd9Sstevel@tonic-gate 2418f78a91cdSjjc /* 2419f78a91cdSjjc * For Greyhound, PCI Extended Configuration Space must be enabled to 2420f78a91cdSjjc * read high DRAM address map base and limit registers 2421f78a91cdSjjc */ 2422f78a91cdSjjc if (opt_family == AMD_FAMILY_GREYHOUND) { 2423f78a91cdSjjc nb_cfg_reg = rdmsr(MSR_AMD_NB_CFG); 2424f78a91cdSjjc if ((nb_cfg_reg & AMD_GH_NB_CFG_EN_ECS) == 0) 2425f78a91cdSjjc wrmsr(MSR_AMD_NB_CFG, 2426f78a91cdSjjc nb_cfg_reg | AMD_GH_NB_CFG_EN_ECS); 2427f78a91cdSjjc } 2428f78a91cdSjjc 24292e2c009bSjjc for (node = 0; node < *node_cnt; node++) { 2430f78a91cdSjjc uint32_t base_hi; 2431f78a91cdSjjc uint32_t base_lo; 2432f78a91cdSjjc uint32_t limit_hi; 2433f78a91cdSjjc uint32_t limit_lo; 2434f78a91cdSjjc 24357c478bd9Sstevel@tonic-gate /* 24367c478bd9Sstevel@tonic-gate * Read node ID register (except for node 0 which we just read) 24377c478bd9Sstevel@tonic-gate */ 24387c478bd9Sstevel@tonic-gate if (node > 0) { 24392e2c009bSjjc node_info[node] = pci_getl_func(bus, dev, 2440ef50d8c0Sesaxe OPT_PCS_FUNC_HT, OPT_PCS_OFF_NODEID); 24417c478bd9Sstevel@tonic-gate } 24427c478bd9Sstevel@tonic-gate 24437c478bd9Sstevel@tonic-gate /* 24447c478bd9Sstevel@tonic-gate * Read DRAM base and limit registers which specify 24457c478bd9Sstevel@tonic-gate * physical memory range of each node 24467c478bd9Sstevel@tonic-gate */ 2447f78a91cdSjjc if (opt_family != AMD_FAMILY_GREYHOUND) 2448f78a91cdSjjc base_hi = 0; 2449f78a91cdSjjc else { 2450f78a91cdSjjc outl(PCI_CONFADD, OPT_PCI_ECS_ADDR(bus, dev, 2451f78a91cdSjjc OPT_PCS_FUNC_ADDRMAP, off_hi)); 24522e2c009bSjjc base_hi = dram_map[node].base_hi = 2453f78a91cdSjjc inl(PCI_CONFDATA); 2454f78a91cdSjjc } 24552e2c009bSjjc base_lo = dram_map[node].base_lo = pci_getl_func(bus, dev, 2456f78a91cdSjjc OPT_PCS_FUNC_ADDRMAP, off_lo); 2457f78a91cdSjjc 24582e2c009bSjjc if ((dram_map[node].base_lo & OPT_DRAMBASE_LO_MASK_INTRLVEN) && 24592e2c009bSjjc mem_intrlv) 24602e2c009bSjjc *mem_intrlv = *mem_intrlv + 1; 24617c478bd9Sstevel@tonic-gate 2462f78a91cdSjjc off_hi += 4; /* high limit register offset */ 2463f78a91cdSjjc if (opt_family != AMD_FAMILY_GREYHOUND) 2464f78a91cdSjjc limit_hi = 0; 2465f78a91cdSjjc else { 2466f78a91cdSjjc outl(PCI_CONFADD, OPT_PCI_ECS_ADDR(bus, dev, 2467f78a91cdSjjc OPT_PCS_FUNC_ADDRMAP, off_hi)); 24682e2c009bSjjc limit_hi = dram_map[node].limit_hi = 2469f78a91cdSjjc inl(PCI_CONFDATA); 2470f78a91cdSjjc } 2471f78a91cdSjjc 2472f78a91cdSjjc off_lo += 4; /* low limit register offset */ 24732e2c009bSjjc limit_lo = dram_map[node].limit_lo = pci_getl_func(bus, 2474f78a91cdSjjc dev, OPT_PCS_FUNC_ADDRMAP, off_lo); 24757c478bd9Sstevel@tonic-gate 24767c478bd9Sstevel@tonic-gate /* 2477f78a91cdSjjc * Increment device number to next node and register offsets 2478f78a91cdSjjc * for DRAM base register of next node 24797c478bd9Sstevel@tonic-gate */ 2480f78a91cdSjjc off_hi += 4; 2481f78a91cdSjjc off_lo += 4; 24827c478bd9Sstevel@tonic-gate dev++; 24837c478bd9Sstevel@tonic-gate 24847c478bd9Sstevel@tonic-gate /* 2485a940d195Sjjc * Both read and write enable bits must be enabled in DRAM 2486a940d195Sjjc * address map base register for physical memory to exist in 2487a940d195Sjjc * node 2488a940d195Sjjc */ 2489f78a91cdSjjc if ((base_lo & OPT_DRAMBASE_LO_MASK_RE) == 0 || 2490f78a91cdSjjc (base_lo & OPT_DRAMBASE_LO_MASK_WE) == 0) { 2491a940d195Sjjc /* 2492a940d195Sjjc * Mark node memory as non-existent and set start and 24932e2c009bSjjc * end addresses to be same in node_memory[] 2494a940d195Sjjc */ 24952e2c009bSjjc node_memory[node].exists = 0; 24962e2c009bSjjc node_memory[node].start = node_memory[node].end = 24972e2c009bSjjc (pfn_t)-1; 2498a940d195Sjjc continue; 2499a940d195Sjjc } 2500a940d195Sjjc 2501a940d195Sjjc /* 2502a940d195Sjjc * Mark node memory as existing and remember physical address 2503a940d195Sjjc * range of each node for use later 25047c478bd9Sstevel@tonic-gate */ 25052e2c009bSjjc node_memory[node].exists = 1; 2506f78a91cdSjjc 25072e2c009bSjjc node_memory[node].start = btop(OPT_DRAMADDR(base_hi, base_lo)); 2508f78a91cdSjjc 25092e2c009bSjjc node_memory[node].end = btop(OPT_DRAMADDR(limit_hi, limit_lo) | 2510f78a91cdSjjc OPT_DRAMADDR_LO_MASK_OFF); 2511f78a91cdSjjc } 2512f78a91cdSjjc 2513f78a91cdSjjc /* 2514f78a91cdSjjc * Restore PCI Extended Configuration Space enable bit 2515f78a91cdSjjc */ 2516f78a91cdSjjc if (opt_family == AMD_FAMILY_GREYHOUND) { 2517f78a91cdSjjc if ((nb_cfg_reg & AMD_GH_NB_CFG_EN_ECS) == 0) 2518f78a91cdSjjc wrmsr(MSR_AMD_NB_CFG, nb_cfg_reg); 25197c478bd9Sstevel@tonic-gate } 25207c478bd9Sstevel@tonic-gate } 25217c478bd9Sstevel@tonic-gate 25227c478bd9Sstevel@tonic-gate 25237c478bd9Sstevel@tonic-gate /* 25242e2c009bSjjc * Return average amount of time to read vendor ID register on Northbridge 25252e2c009bSjjc * N times on specified destination node from current CPU 25267c478bd9Sstevel@tonic-gate */ 25277c478bd9Sstevel@tonic-gate static hrtime_t 25282e2c009bSjjc opt_probe_vendor(int dest_node, int nreads) 25297c478bd9Sstevel@tonic-gate { 25302e2c009bSjjc int cnt; 25317c478bd9Sstevel@tonic-gate uint_t dev; 25327c478bd9Sstevel@tonic-gate /* LINTED: set but not used in function */ 25337c478bd9Sstevel@tonic-gate volatile uint_t dev_vendor; 25347c478bd9Sstevel@tonic-gate hrtime_t elapsed; 25357c478bd9Sstevel@tonic-gate hrtime_t end; 25367c478bd9Sstevel@tonic-gate int ipl; 25377c478bd9Sstevel@tonic-gate hrtime_t start; 25387c478bd9Sstevel@tonic-gate 25392e2c009bSjjc dev = OPT_PCS_DEV_NODE0 + dest_node; 25407c478bd9Sstevel@tonic-gate kpreempt_disable(); 25417c478bd9Sstevel@tonic-gate ipl = spl8(); 25422e2c009bSjjc outl(PCI_CONFADD, PCI_CADDR1(0, dev, OPT_PCS_FUNC_DRAM, 25437c478bd9Sstevel@tonic-gate OPT_PCS_OFF_VENDOR)); 25447c478bd9Sstevel@tonic-gate start = gethrtime(); 25452e2c009bSjjc for (cnt = 0; cnt < nreads; cnt++) 25467c478bd9Sstevel@tonic-gate dev_vendor = inl(PCI_CONFDATA); 25477c478bd9Sstevel@tonic-gate end = gethrtime(); 25482e2c009bSjjc elapsed = (end - start) / nreads; 25497c478bd9Sstevel@tonic-gate splx(ipl); 25507c478bd9Sstevel@tonic-gate kpreempt_enable(); 25512e2c009bSjjc return (elapsed); 25527c478bd9Sstevel@tonic-gate } 2553