/*
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christoph Egger.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static uint32_t nnodes; /* Number of NUMA nodes */
static struct acpisrat_node *node_array; /* Array of NUMA nodes */
static uint32_t ncpus; /* Number of CPUs */
static struct acpisrat_cpu *cpu_array; /* Array of cpus */
static uint32_t nmems; /* Number of Memory ranges */
static struct acpisrat_mem *mem_array;
MEM_FOREACH(tmp) {
if (tmp->mem.nodeid == nodeid)
return tmp;
}
return NULL;
}
/*
* Returns true if ACPI SRAT table is available. If table does not exist, all
* functions below have undefined behaviour.
*/
bool
acpisrat_exist(void)
{
ACPI_TABLE_HEADER *table;
ACPI_STATUS rv;
rv = AcpiGetTable(ACPI_SIG_SRAT, 1, (ACPI_TABLE_HEADER **)&table);
if (ACPI_FAILURE(rv))
return false;
/* Check if header is valid */
if (table == NULL)
return false;
/*
* Deal with holes in the memory nodes. BIOS doesn't enlist memory
* nodes which don't have any memory modules plugged in. This behaviour
* has been observed on AMD machines.
*
* Do that by searching for CPUs in NUMA nodes which don't exist in the
* memory and then insert a zero memory range for the missing node.
*/
CPU_FOREACH(citer) {
mem = mem_get(citer->cpu.nodeid);
if (mem != NULL)
continue;
mem = mem_alloc();
if (mem == NULL)
return ENOMEM;
mem->mem.nodeid = citer->cpu.nodeid;
/* all other fields are already zero filled */
/*
* Initializes parser. Must be the first function being called when table is
* available.
*/
int
acpisrat_init(void)
{
if (!acpisrat_exist())
return EEXIST;
return acpisrat_refresh();
}
/*
* Re-parse ACPI SRAT table. Useful after hotplugging cpu or RAM.
*/
int
acpisrat_refresh(void)
{
int rc, i, j, k;
struct cpulist *citer;
struct memlist *miter;
uint32_t cnodes = 0, mnodes = 0;
void
acpisrat_load_uvm(void)
{
uint32_t i, j, nn, nm;
struct acpisrat_mem m;
nn = acpisrat_nodes();
aprint_debug("SRAT: %u NUMA nodes\n", nn);
for (i = 0; i < nn; i++) {
nm = acpisrat_node_memoryranges(i);
for (j = 0; j < nm; j++) {
acpisrat_mem(i, j, &m);
aprint_debug("SRAT: node %u memory range %u (0x%"
PRIx64" - 0x%"PRIx64" flags %u)\n",
m.nodeid, j, m.baseaddress,
m.baseaddress + m.length, m.flags);
uvm_page_numa_load(trunc_page(m.baseaddress),
trunc_page(m.length), m.nodeid);
}
}
}
/*
* Get number of NUMA nodes.
*/
uint32_t
acpisrat_nodes(void)
{
return nnodes;
}
/*
* Get number of cpus in the node. 0 means, this is a cpu-less node.
*/
uint32_t
acpisrat_node_cpus(acpisrat_nodeid_t nodeid)
{
return node_array[nodeid].ncpus;
}
/*
* Get number of memory ranges in the node 0 means, this node has no RAM.
*/
uint32_t
acpisrat_node_memoryranges(acpisrat_nodeid_t nodeid)
{
return node_array[nodeid].nmems;
}
/*
* Get a node from an APIC id (belonging to a cpu).
*/
struct acpisrat_node *
acpisrat_get_node(uint32_t apicid)
{
struct acpisrat_node *node;
struct acpisrat_cpu *cpu;
size_t i, n;
for (i = 0; i < nnodes; i++) {
node = &node_array[i];
for (n = 0; n < node->ncpus; n++) {
cpu = node->cpu[n];
if (cpu->apicid == apicid) {
return node;
}
}
}