hmat.c 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2019, Intel Corporation.
 *
 * Heterogeneous Memory Attributes Table (HMAT) representation
 *
 * This program parses and reports the platform's HMAT tables, and registers
 * the applicable attributes with the node's interfaces.
 */

11 12
#define pr_fmt(fmt) "acpi/hmat: " fmt

13 14 15 16 17
#include <linux/acpi.h>
#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/list.h>
18 19
#include <linux/mm.h>
#include <linux/platform_device.h>
20
#include <linux/list_sort.h>
21
#include <linux/memregion.h>
22 23
#include <linux/memory.h>
#include <linux/mutex.h>
24 25
#include <linux/node.h>
#include <linux/sysfs.h>
26
#include <linux/dax.h>
27

28
static u8 hmat_revision;
D
Dan Williams 已提交
29 30 31 32 33 34
static int hmat_disable __initdata;

void __init disable_hmat(void)
{
	hmat_disable = 1;
}
35

36 37 38 39 40
static LIST_HEAD(targets);
static LIST_HEAD(initiators);
static LIST_HEAD(localities);

static DEFINE_MUTEX(target_lock);
41 42 43 44 45 46 47 48 49 50 51 52 53 54

/*
 * The defined enum order is used to prioritize attributes to break ties when
 * selecting the best performing node.
 */
enum locality_types {
	WRITE_LATENCY,
	READ_LATENCY,
	WRITE_BANDWIDTH,
	READ_BANDWIDTH,
};

static struct memory_locality *localities_types[4];

55 56 57 58 59
struct target_cache {
	struct list_head node;
	struct node_cache_attrs cache_attrs;
};

60 61 62 63
struct memory_target {
	struct list_head node;
	unsigned int memory_pxm;
	unsigned int processor_pxm;
64
	struct resource memregions;
65
	struct node_hmem_attrs hmem_attrs[2];
66
	struct list_head caches;
67 68
	struct node_cache_attrs cache_attrs;
	bool registered;
69 70 71 72 73
};

struct memory_initiator {
	struct list_head node;
	unsigned int processor_pxm;
74
	bool has_cpu;
75 76 77 78 79 80 81
};

struct memory_locality {
	struct list_head node;
	struct acpi_hmat_locality *hmat_loc;
};

82
static struct memory_initiator *find_mem_initiator(unsigned int cpu_pxm)
83 84 85 86 87 88 89 90 91
{
	struct memory_initiator *initiator;

	list_for_each_entry(initiator, &initiators, node)
		if (initiator->processor_pxm == cpu_pxm)
			return initiator;
	return NULL;
}

92
static struct memory_target *find_mem_target(unsigned int mem_pxm)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
	struct memory_target *target;

	list_for_each_entry(target, &targets, node)
		if (target->memory_pxm == mem_pxm)
			return target;
	return NULL;
}

static __init void alloc_memory_initiator(unsigned int cpu_pxm)
{
	struct memory_initiator *initiator;

	if (pxm_to_node(cpu_pxm) == NUMA_NO_NODE)
		return;

	initiator = find_mem_initiator(cpu_pxm);
	if (initiator)
		return;

	initiator = kzalloc(sizeof(*initiator), GFP_KERNEL);
	if (!initiator)
		return;

	initiator->processor_pxm = cpu_pxm;
118
	initiator->has_cpu = node_state(pxm_to_node(cpu_pxm), N_CPU);
119 120 121
	list_add_tail(&initiator->node, &initiators);
}

122 123
static __init void alloc_memory_target(unsigned int mem_pxm,
		resource_size_t start, resource_size_t len)
124 125 126 127
{
	struct memory_target *target;

	target = find_mem_target(mem_pxm);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	if (!target) {
		target = kzalloc(sizeof(*target), GFP_KERNEL);
		if (!target)
			return;
		target->memory_pxm = mem_pxm;
		target->processor_pxm = PXM_INVAL;
		target->memregions = (struct resource) {
			.name	= "ACPI mem",
			.start	= 0,
			.end	= -1,
			.flags	= IORESOURCE_MEM,
		};
		list_add_tail(&target->node, &targets);
		INIT_LIST_HEAD(&target->caches);
	}
143

144 145 146 147 148 149 150 151
	/*
	 * There are potentially multiple ranges per PXM, so record each
	 * in the per-target memregions resource tree.
	 */
	if (!__request_region(&target->memregions, start, len, "memory target",
				IORESOURCE_MEM))
		pr_warn("failed to reserve %#llx - %#llx in pxm: %d\n",
				start, start + len, mem_pxm);
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
static __init const char *hmat_data_type(u8 type)
{
	switch (type) {
	case ACPI_HMAT_ACCESS_LATENCY:
		return "Access Latency";
	case ACPI_HMAT_READ_LATENCY:
		return "Read Latency";
	case ACPI_HMAT_WRITE_LATENCY:
		return "Write Latency";
	case ACPI_HMAT_ACCESS_BANDWIDTH:
		return "Access Bandwidth";
	case ACPI_HMAT_READ_BANDWIDTH:
		return "Read Bandwidth";
	case ACPI_HMAT_WRITE_BANDWIDTH:
		return "Write Bandwidth";
	default:
		return "Reserved";
	}
}

static __init const char *hmat_data_type_suffix(u8 type)
{
	switch (type) {
	case ACPI_HMAT_ACCESS_LATENCY:
	case ACPI_HMAT_READ_LATENCY:
	case ACPI_HMAT_WRITE_LATENCY:
		return " nsec";
	case ACPI_HMAT_ACCESS_BANDWIDTH:
	case ACPI_HMAT_READ_BANDWIDTH:
	case ACPI_HMAT_WRITE_BANDWIDTH:
		return " MB/s";
	default:
		return "";
	}
}

190
static u32 hmat_normalize(u16 entry, u64 base, u8 type)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
	u32 value;

	/*
	 * Check for invalid and overflow values
	 */
	if (entry == 0xffff || !entry)
		return 0;
	else if (base > (UINT_MAX / (entry)))
		return 0;

	/*
	 * Divide by the base unit for version 1, convert latency from
	 * picosenonds to nanoseconds if revision 2.
	 */
	value = entry * base;
	if (hmat_revision == 1) {
		if (value < 10)
			return 0;
		value = DIV_ROUND_UP(value, 10);
	} else if (hmat_revision == 2) {
		switch (type) {
		case ACPI_HMAT_ACCESS_LATENCY:
		case ACPI_HMAT_READ_LATENCY:
		case ACPI_HMAT_WRITE_LATENCY:
			value = DIV_ROUND_UP(value, 1000);
			break;
		default:
			break;
		}
	}
	return value;
}

225
static void hmat_update_target_access(struct memory_target *target,
226
				      u8 type, u32 value, int access)
227 228 229
{
	switch (type) {
	case ACPI_HMAT_ACCESS_LATENCY:
230 231
		target->hmem_attrs[access].read_latency = value;
		target->hmem_attrs[access].write_latency = value;
232 233
		break;
	case ACPI_HMAT_READ_LATENCY:
234
		target->hmem_attrs[access].read_latency = value;
235 236
		break;
	case ACPI_HMAT_WRITE_LATENCY:
237
		target->hmem_attrs[access].write_latency = value;
238 239
		break;
	case ACPI_HMAT_ACCESS_BANDWIDTH:
240 241
		target->hmem_attrs[access].read_bandwidth = value;
		target->hmem_attrs[access].write_bandwidth = value;
242 243
		break;
	case ACPI_HMAT_READ_BANDWIDTH:
244
		target->hmem_attrs[access].read_bandwidth = value;
245 246
		break;
	case ACPI_HMAT_WRITE_BANDWIDTH:
247
		target->hmem_attrs[access].write_bandwidth = value;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
		break;
	default:
		break;
	}
}

static __init void hmat_add_locality(struct acpi_hmat_locality *hmat_loc)
{
	struct memory_locality *loc;

	loc = kzalloc(sizeof(*loc), GFP_KERNEL);
	if (!loc) {
		pr_notice_once("Failed to allocate HMAT locality\n");
		return;
	}

	loc->hmat_loc = hmat_loc;
	list_add_tail(&loc->node, &localities);

	switch (hmat_loc->data_type) {
	case ACPI_HMAT_ACCESS_LATENCY:
		localities_types[READ_LATENCY] = loc;
		localities_types[WRITE_LATENCY] = loc;
		break;
	case ACPI_HMAT_READ_LATENCY:
		localities_types[READ_LATENCY] = loc;
		break;
	case ACPI_HMAT_WRITE_LATENCY:
		localities_types[WRITE_LATENCY] = loc;
		break;
	case ACPI_HMAT_ACCESS_BANDWIDTH:
		localities_types[READ_BANDWIDTH] = loc;
		localities_types[WRITE_BANDWIDTH] = loc;
		break;
	case ACPI_HMAT_READ_BANDWIDTH:
		localities_types[READ_BANDWIDTH] = loc;
		break;
	case ACPI_HMAT_WRITE_BANDWIDTH:
		localities_types[WRITE_BANDWIDTH] = loc;
		break;
	default:
		break;
	}
}

293 294 295 296
static __init int hmat_parse_locality(union acpi_subtable_headers *header,
				      const unsigned long end)
{
	struct acpi_hmat_locality *hmat_loc = (void *)header;
297
	struct memory_target *target;
298 299 300
	unsigned int init, targ, total_size, ipds, tpds;
	u32 *inits, *targs, value;
	u16 *entries;
301
	u8 type, mem_hier;
302 303

	if (hmat_loc->header.length < sizeof(*hmat_loc)) {
304
		pr_notice("Unexpected locality header length: %u\n",
305 306 307 308 309
			 hmat_loc->header.length);
		return -EINVAL;
	}

	type = hmat_loc->data_type;
310
	mem_hier = hmat_loc->flags & ACPI_HMAT_MEMORY_HIERARCHY;
311 312 313 314 315
	ipds = hmat_loc->number_of_initiator_Pds;
	tpds = hmat_loc->number_of_target_Pds;
	total_size = sizeof(*hmat_loc) + sizeof(*entries) * ipds * tpds +
		     sizeof(*inits) * ipds + sizeof(*targs) * tpds;
	if (hmat_loc->header.length < total_size) {
316
		pr_notice("Unexpected locality header length:%u, minimum required:%u\n",
317 318 319 320
			 hmat_loc->header.length, total_size);
		return -EINVAL;
	}

321
	pr_info("Locality: Flags:%02x Type:%s Initiator Domains:%u Target Domains:%u Base:%lld\n",
322 323 324 325 326 327 328
		hmat_loc->flags, hmat_data_type(type), ipds, tpds,
		hmat_loc->entry_base_unit);

	inits = (u32 *)(hmat_loc + 1);
	targs = inits + ipds;
	entries = (u16 *)(targs + tpds);
	for (init = 0; init < ipds; init++) {
329
		alloc_memory_initiator(inits[init]);
330 331 332 333
		for (targ = 0; targ < tpds; targ++) {
			value = hmat_normalize(entries[init * tpds + targ],
					       hmat_loc->entry_base_unit,
					       type);
334
			pr_info("  Initiator-Target[%u-%u]:%u%s\n",
335 336
				inits[init], targs[targ], value,
				hmat_data_type_suffix(type));
337 338 339

			if (mem_hier == ACPI_HMAT_MEMORY) {
				target = find_mem_target(targs[targ]);
340 341 342 343 344 345
				if (target && target->processor_pxm == inits[init]) {
					hmat_update_target_access(target, type, value, 0);
					/* If the node has a CPU, update access 1 */
					if (node_state(pxm_to_node(inits[init]), N_CPU))
						hmat_update_target_access(target, type, value, 1);
				}
346
			}
347 348 349
		}
	}

350 351 352
	if (mem_hier == ACPI_HMAT_MEMORY)
		hmat_add_locality(hmat_loc);

353 354 355 356 357 358 359
	return 0;
}

static __init int hmat_parse_cache(union acpi_subtable_headers *header,
				   const unsigned long end)
{
	struct acpi_hmat_cache *cache = (void *)header;
360 361
	struct memory_target *target;
	struct target_cache *tcache;
362 363 364
	u32 attrs;

	if (cache->header.length < sizeof(*cache)) {
365
		pr_notice("Unexpected cache header length: %u\n",
366 367 368 369 370
			 cache->header.length);
		return -EINVAL;
	}

	attrs = cache->cache_attributes;
371
	pr_info("Cache: Domain:%u Size:%llu Attrs:%08x SMBIOS Handles:%d\n",
372 373 374
		cache->memory_PD, cache->cache_size, attrs,
		cache->number_of_SMBIOShandles);

375 376 377 378 379 380 381 382 383 384 385 386 387
	target = find_mem_target(cache->memory_PD);
	if (!target)
		return 0;

	tcache = kzalloc(sizeof(*tcache), GFP_KERNEL);
	if (!tcache) {
		pr_notice_once("Failed to allocate HMAT cache info\n");
		return 0;
	}

	tcache->cache_attrs.size = cache->cache_size;
	tcache->cache_attrs.level = (attrs & ACPI_HMAT_CACHE_LEVEL) >> 4;
	tcache->cache_attrs.line_size = (attrs & ACPI_HMAT_CACHE_LINE_SIZE) >> 16;
388 389 390

	switch ((attrs & ACPI_HMAT_CACHE_ASSOCIATIVITY) >> 8) {
	case ACPI_HMAT_CA_DIRECT_MAPPED:
391
		tcache->cache_attrs.indexing = NODE_CACHE_DIRECT_MAP;
392 393
		break;
	case ACPI_HMAT_CA_COMPLEX_CACHE_INDEXING:
394
		tcache->cache_attrs.indexing = NODE_CACHE_INDEXED;
395 396 397
		break;
	case ACPI_HMAT_CA_NONE:
	default:
398
		tcache->cache_attrs.indexing = NODE_CACHE_OTHER;
399 400 401 402 403
		break;
	}

	switch ((attrs & ACPI_HMAT_WRITE_POLICY) >> 12) {
	case ACPI_HMAT_CP_WB:
404
		tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_BACK;
405 406
		break;
	case ACPI_HMAT_CP_WT:
407
		tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_THROUGH;
408 409 410
		break;
	case ACPI_HMAT_CP_NONE:
	default:
411
		tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_OTHER;
412 413
		break;
	}
414
	list_add_tail(&tcache->node, &target->caches);
415

416 417 418 419 420 421 422
	return 0;
}

static int __init hmat_parse_proximity_domain(union acpi_subtable_headers *header,
					      const unsigned long end)
{
	struct acpi_hmat_proximity_domain *p = (void *)header;
423
	struct memory_target *target = NULL;
424 425

	if (p->header.length != sizeof(*p)) {
426
		pr_notice("Unexpected address range header length: %u\n",
427 428 429 430 431
			 p->header.length);
		return -EINVAL;
	}

	if (hmat_revision == 1)
432
		pr_info("Memory (%#llx length %#llx) Flags:%04x Processor Domain:%u Memory Domain:%u\n",
433 434 435
			p->reserved3, p->reserved4, p->flags, p->processor_PD,
			p->memory_PD);
	else
436
		pr_info("Memory Flags:%04x Processor Domain:%u Memory Domain:%u\n",
437 438
			p->flags, p->processor_PD, p->memory_PD);

439 440
	if ((hmat_revision == 1 && p->flags & ACPI_HMAT_MEMORY_PD_VALID) ||
	    hmat_revision > 1) {
441 442
		target = find_mem_target(p->memory_PD);
		if (!target) {
443
			pr_debug("Memory Domain missing from SRAT\n");
444 445 446 447 448 449 450
			return -EINVAL;
		}
	}
	if (target && p->flags & ACPI_HMAT_PROCESSOR_PD_VALID) {
		int p_node = pxm_to_node(p->processor_PD);

		if (p_node == NUMA_NO_NODE) {
451
			pr_debug("Invalid Processor Domain\n");
452 453
			return -EINVAL;
		}
454
		target->processor_pxm = p->processor_PD;
455 456
	}

457 458 459 460 461 462 463 464 465 466 467 468
	return 0;
}

static int __init hmat_parse_subtable(union acpi_subtable_headers *header,
				      const unsigned long end)
{
	struct acpi_hmat_structure *hdr = (void *)header;

	if (!hdr)
		return -EINVAL;

	switch (hdr->type) {
469
	case ACPI_HMAT_TYPE_PROXIMITY:
470 471 472 473 474 475 476 477 478 479
		return hmat_parse_proximity_domain(header, end);
	case ACPI_HMAT_TYPE_LOCALITY:
		return hmat_parse_locality(header, end);
	case ACPI_HMAT_TYPE_CACHE:
		return hmat_parse_cache(header, end);
	default:
		return -EINVAL;
	}
}

480 481 482 483 484 485 486 487 488
static __init int srat_parse_mem_affinity(union acpi_subtable_headers *header,
					  const unsigned long end)
{
	struct acpi_srat_mem_affinity *ma = (void *)header;

	if (!ma)
		return -EINVAL;
	if (!(ma->flags & ACPI_SRAT_MEM_ENABLED))
		return 0;
489
	alloc_memory_target(ma->proximity_domain, ma->base_address, ma->length);
490 491 492
	return 0;
}

493
static u32 hmat_initiator_perf(struct memory_target *target,
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
			       struct memory_initiator *initiator,
			       struct acpi_hmat_locality *hmat_loc)
{
	unsigned int ipds, tpds, i, idx = 0, tdx = 0;
	u32 *inits, *targs;
	u16 *entries;

	ipds = hmat_loc->number_of_initiator_Pds;
	tpds = hmat_loc->number_of_target_Pds;
	inits = (u32 *)(hmat_loc + 1);
	targs = inits + ipds;
	entries = (u16 *)(targs + tpds);

	for (i = 0; i < ipds; i++) {
		if (inits[i] == initiator->processor_pxm) {
			idx = i;
			break;
		}
	}

	if (i == ipds)
		return 0;

	for (i = 0; i < tpds; i++) {
		if (targs[i] == target->memory_pxm) {
			tdx = i;
			break;
		}
	}
	if (i == tpds)
		return 0;

	return hmat_normalize(entries[idx * tpds + tdx],
			      hmat_loc->entry_base_unit,
			      hmat_loc->data_type);
}

531
static bool hmat_update_best(u8 type, u32 value, u32 *best)
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
{
	bool updated = false;

	if (!value)
		return false;

	switch (type) {
	case ACPI_HMAT_ACCESS_LATENCY:
	case ACPI_HMAT_READ_LATENCY:
	case ACPI_HMAT_WRITE_LATENCY:
		if (!*best || *best > value) {
			*best = value;
			updated = true;
		}
		break;
	case ACPI_HMAT_ACCESS_BANDWIDTH:
	case ACPI_HMAT_READ_BANDWIDTH:
	case ACPI_HMAT_WRITE_BANDWIDTH:
		if (!*best || *best < value) {
			*best = value;
			updated = true;
		}
		break;
	}

	return updated;
}

560 561
static int initiator_cmp(void *priv, const struct list_head *a,
			 const struct list_head *b)
562 563 564 565 566 567 568 569 570 571
{
	struct memory_initiator *ia;
	struct memory_initiator *ib;

	ia = list_entry(a, struct memory_initiator, node);
	ib = list_entry(b, struct memory_initiator, node);

	return ia->processor_pxm - ib->processor_pxm;
}

572 573 574 575 576 577 578 579 580 581 582 583 584
static int initiators_to_nodemask(unsigned long *p_nodes)
{
	struct memory_initiator *initiator;

	if (list_empty(&initiators))
		return -ENXIO;

	list_for_each_entry(initiator, &initiators, node)
		set_bit(initiator->processor_pxm, p_nodes);

	return 0;
}

585
static void hmat_register_target_initiators(struct memory_target *target)
586 587 588 589 590 591
{
	static DECLARE_BITMAP(p_nodes, MAX_NUMNODES);
	struct memory_initiator *initiator;
	unsigned int mem_nid, cpu_nid;
	struct memory_locality *loc = NULL;
	u32 best = 0;
592
	bool access0done = false;
593 594 595 596 597 598 599 600 601 602 603
	int i;

	mem_nid = pxm_to_node(target->memory_pxm);
	/*
	 * If the Address Range Structure provides a local processor pxm, link
	 * only that one. Otherwise, find the best performance attributes and
	 * register all initiators that match.
	 */
	if (target->processor_pxm != PXM_INVAL) {
		cpu_nid = pxm_to_node(target->processor_pxm);
		register_memory_node_under_compute_node(mem_nid, cpu_nid, 0);
604 605 606 607 608
		access0done = true;
		if (node_state(cpu_nid, N_CPU)) {
			register_memory_node_under_compute_node(mem_nid, cpu_nid, 1);
			return;
		}
609 610 611 612 613 614 615 616 617 618 619 620
	}

	if (list_empty(&localities))
		return;

	/*
	 * We need the initiator list sorted so we can use bitmap_clear for
	 * previously set initiators when we find a better memory accessor.
	 * We'll also use the sorting to prime the candidate nodes with known
	 * initiators.
	 */
	bitmap_zero(p_nodes, MAX_NUMNODES);
621 622 623 624
	list_sort(NULL, &initiators, initiator_cmp);
	if (initiators_to_nodemask(p_nodes) < 0)
		return;

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	if (!access0done) {
		for (i = WRITE_LATENCY; i <= READ_BANDWIDTH; i++) {
			loc = localities_types[i];
			if (!loc)
				continue;

			best = 0;
			list_for_each_entry(initiator, &initiators, node) {
				u32 value;

				if (!test_bit(initiator->processor_pxm, p_nodes))
					continue;

				value = hmat_initiator_perf(target, initiator,
							    loc->hmat_loc);
				if (hmat_update_best(loc->hmat_loc->data_type, value, &best))
					bitmap_clear(p_nodes, 0, initiator->processor_pxm);
				if (value != best)
					clear_bit(initiator->processor_pxm, p_nodes);
			}
			if (best)
				hmat_update_target_access(target, loc->hmat_loc->data_type,
							  best, 0);
		}

		for_each_set_bit(i, p_nodes, MAX_NUMNODES) {
			cpu_nid = pxm_to_node(i);
			register_memory_node_under_compute_node(mem_nid, cpu_nid, 0);
		}
	}

	/* Access 1 ignores Generic Initiators */
	bitmap_zero(p_nodes, MAX_NUMNODES);
658 659 660
	if (initiators_to_nodemask(p_nodes) < 0)
		return;

661 662 663 664 665 666 667 668 669
	for (i = WRITE_LATENCY; i <= READ_BANDWIDTH; i++) {
		loc = localities_types[i];
		if (!loc)
			continue;

		best = 0;
		list_for_each_entry(initiator, &initiators, node) {
			u32 value;

670 671 672 673
			if (!initiator->has_cpu) {
				clear_bit(initiator->processor_pxm, p_nodes);
				continue;
			}
674 675 676 677 678 679 680 681 682 683
			if (!test_bit(initiator->processor_pxm, p_nodes))
				continue;

			value = hmat_initiator_perf(target, initiator, loc->hmat_loc);
			if (hmat_update_best(loc->hmat_loc->data_type, value, &best))
				bitmap_clear(p_nodes, 0, initiator->processor_pxm);
			if (value != best)
				clear_bit(initiator->processor_pxm, p_nodes);
		}
		if (best)
684
			hmat_update_target_access(target, loc->hmat_loc->data_type, best, 1);
685 686 687
	}
	for_each_set_bit(i, p_nodes, MAX_NUMNODES) {
		cpu_nid = pxm_to_node(i);
688
		register_memory_node_under_compute_node(mem_nid, cpu_nid, 1);
689 690 691
	}
}

692
static void hmat_register_target_cache(struct memory_target *target)
693 694 695 696 697 698 699 700
{
	unsigned mem_nid = pxm_to_node(target->memory_pxm);
	struct target_cache *tcache;

	list_for_each_entry(tcache, &target->caches, node)
		node_add_cache(mem_nid, &tcache->cache_attrs);
}

701
static void hmat_register_target_perf(struct memory_target *target, int access)
702 703
{
	unsigned mem_nid = pxm_to_node(target->memory_pxm);
704
	node_set_perf_attrs(mem_nid, &target->hmem_attrs[access], access);
705 706
}

707
static void hmat_register_target_devices(struct memory_target *target)
708 709 710 711 712 713 714 715 716 717
{
	struct resource *res;

	/*
	 * Do not bother creating devices if no driver is available to
	 * consume them.
	 */
	if (!IS_ENABLED(CONFIG_DEV_DAX_HMEM))
		return;

718
	for (res = target->memregions.child; res; res = res->sibling) {
719
		int target_nid = pxm_to_node(target->memory_pxm);
720 721 722

		hmem_register_device(target_nid, res);
	}
723 724
}

725
static void hmat_register_target(struct memory_target *target)
726
{
727 728
	int nid = pxm_to_node(target->memory_pxm);

729 730 731 732 733 734
	/*
	 * Devices may belong to either an offline or online
	 * node, so unconditionally add them.
	 */
	hmat_register_target_devices(target);

735 736 737
	/*
	 * Skip offline nodes. This can happen when memory
	 * marked EFI_MEMORY_SP, "specific purpose", is applied
T
Tom Saeger 已提交
738
	 * to all the memory in a proximity domain leading to
739 740 741 742
	 * the node being marked offline / unplugged, or if
	 * memory-only "hotplug" node is offline.
	 */
	if (nid == NUMA_NO_NODE || !node_online(nid))
743 744
		return;

745 746 747 748
	mutex_lock(&target_lock);
	if (!target->registered) {
		hmat_register_target_initiators(target);
		hmat_register_target_cache(target);
749 750
		hmat_register_target_perf(target, 0);
		hmat_register_target_perf(target, 1);
751 752 753
		target->registered = true;
	}
	mutex_unlock(&target_lock);
754 755
}

756
static void hmat_register_targets(void)
757 758 759
{
	struct memory_target *target;

760 761
	list_for_each_entry(target, &targets, node)
		hmat_register_target(target);
762 763
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
static int hmat_callback(struct notifier_block *self,
			 unsigned long action, void *arg)
{
	struct memory_target *target;
	struct memory_notify *mnb = arg;
	int pxm, nid = mnb->status_change_nid;

	if (nid == NUMA_NO_NODE || action != MEM_ONLINE)
		return NOTIFY_OK;

	pxm = node_to_pxm(nid);
	target = find_mem_target(pxm);
	if (!target)
		return NOTIFY_OK;

	hmat_register_target(target);
	return NOTIFY_OK;
}

static struct notifier_block hmat_callback_nb = {
	.notifier_call = hmat_callback,
	.priority = 2,
};

788 789 790 791 792
static __init void hmat_free_structures(void)
{
	struct memory_target *target, *tnext;
	struct memory_locality *loc, *lnext;
	struct memory_initiator *initiator, *inext;
793
	struct target_cache *tcache, *cnext;
794 795

	list_for_each_entry_safe(target, tnext, &targets, node) {
796 797
		struct resource *res, *res_next;

798 799 800 801
		list_for_each_entry_safe(tcache, cnext, &target->caches, node) {
			list_del(&tcache->node);
			kfree(tcache);
		}
802

803
		list_del(&target->node);
804 805 806 807 808 809 810
		res = target->memregions.child;
		while (res) {
			res_next = res->sibling;
			__release_region(&target->memregions, res->start,
					resource_size(res));
			res = res_next;
		}
811 812 813 814 815 816 817 818 819 820 821 822 823 824
		kfree(target);
	}

	list_for_each_entry_safe(initiator, inext, &initiators, node) {
		list_del(&initiator->node);
		kfree(initiator);
	}

	list_for_each_entry_safe(loc, lnext, &localities, node) {
		list_del(&loc->node);
		kfree(loc);
	}
}

825 826 827 828 829 830
static __init int hmat_init(void)
{
	struct acpi_table_header *tbl;
	enum acpi_hmat_type i;
	acpi_status status;

D
Dan Williams 已提交
831
	if (srat_disabled() || hmat_disable)
832 833
		return 0;

834 835 836 837 838 839 840 841 842 843 844
	status = acpi_get_table(ACPI_SIG_SRAT, 0, &tbl);
	if (ACPI_FAILURE(status))
		return 0;

	if (acpi_table_parse_entries(ACPI_SIG_SRAT,
				sizeof(struct acpi_table_srat),
				ACPI_SRAT_TYPE_MEMORY_AFFINITY,
				srat_parse_mem_affinity, 0) < 0)
		goto out_put;
	acpi_put_table(tbl);

845 846
	status = acpi_get_table(ACPI_SIG_HMAT, 0, &tbl);
	if (ACPI_FAILURE(status))
847
		goto out_put;
848 849 850 851 852 853 854

	hmat_revision = tbl->revision;
	switch (hmat_revision) {
	case 1:
	case 2:
		break;
	default:
855
		pr_notice("Ignoring: Unknown revision:%d\n", hmat_revision);
856 857 858
		goto out_put;
	}

859
	for (i = ACPI_HMAT_TYPE_PROXIMITY; i < ACPI_HMAT_TYPE_RESERVED; i++) {
860 861 862
		if (acpi_table_parse_entries(ACPI_SIG_HMAT,
					     sizeof(struct acpi_table_hmat), i,
					     hmat_parse_subtable, 0) < 0) {
863
			pr_notice("Ignoring: Invalid table");
864 865 866
			goto out_put;
		}
	}
867
	hmat_register_targets();
868 869 870 871

	/* Keep the table and structures if the notifier may use them */
	if (!register_hotmemory_notifier(&hmat_callback_nb))
		return 0;
872
out_put:
873
	hmat_free_structures();
874 875 876
	acpi_put_table(tbl);
	return 0;
}
877
device_initcall(hmat_init);