xref: /openbmc/linux/drivers/acpi/numa/hmat.c (revision 85716a80)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019, Intel Corporation.
4  *
5  * Heterogeneous Memory Attributes Table (HMAT) representation
6  *
7  * This program parses and reports the platform's HMAT tables, and registers
8  * the applicable attributes with the node's interfaces.
9  */
10 
11 #define pr_fmt(fmt) "acpi/hmat: " fmt
12 
13 #include <linux/acpi.h>
14 #include <linux/bitops.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/mm.h>
19 #include <linux/platform_device.h>
20 #include <linux/list_sort.h>
21 #include <linux/memregion.h>
22 #include <linux/memory.h>
23 #include <linux/mutex.h>
24 #include <linux/node.h>
25 #include <linux/sysfs.h>
26 #include <linux/dax.h>
27 
28 static u8 hmat_revision;
29 static int hmat_disable __initdata;
30 
31 void __init disable_hmat(void)
32 {
33 	hmat_disable = 1;
34 }
35 
36 static LIST_HEAD(targets);
37 static LIST_HEAD(initiators);
38 static LIST_HEAD(localities);
39 
40 static DEFINE_MUTEX(target_lock);
41 
42 /*
43  * The defined enum order is used to prioritize attributes to break ties when
44  * selecting the best performing node.
45  */
46 enum locality_types {
47 	WRITE_LATENCY,
48 	READ_LATENCY,
49 	WRITE_BANDWIDTH,
50 	READ_BANDWIDTH,
51 };
52 
53 static struct memory_locality *localities_types[4];
54 
55 struct target_cache {
56 	struct list_head node;
57 	struct node_cache_attrs cache_attrs;
58 };
59 
60 struct memory_target {
61 	struct list_head node;
62 	unsigned int memory_pxm;
63 	unsigned int processor_pxm;
64 	struct resource memregions;
65 	struct node_hmem_attrs hmem_attrs[2];
66 	struct list_head caches;
67 	struct node_cache_attrs cache_attrs;
68 	bool registered;
69 };
70 
71 struct memory_initiator {
72 	struct list_head node;
73 	unsigned int processor_pxm;
74 	bool has_cpu;
75 };
76 
77 struct memory_locality {
78 	struct list_head node;
79 	struct acpi_hmat_locality *hmat_loc;
80 };
81 
82 static struct memory_initiator *find_mem_initiator(unsigned int cpu_pxm)
83 {
84 	struct memory_initiator *initiator;
85 
86 	list_for_each_entry(initiator, &initiators, node)
87 		if (initiator->processor_pxm == cpu_pxm)
88 			return initiator;
89 	return NULL;
90 }
91 
92 static struct memory_target *find_mem_target(unsigned int mem_pxm)
93 {
94 	struct memory_target *target;
95 
96 	list_for_each_entry(target, &targets, node)
97 		if (target->memory_pxm == mem_pxm)
98 			return target;
99 	return NULL;
100 }
101 
102 static __init void alloc_memory_initiator(unsigned int cpu_pxm)
103 {
104 	struct memory_initiator *initiator;
105 
106 	if (pxm_to_node(cpu_pxm) == NUMA_NO_NODE)
107 		return;
108 
109 	initiator = find_mem_initiator(cpu_pxm);
110 	if (initiator)
111 		return;
112 
113 	initiator = kzalloc(sizeof(*initiator), GFP_KERNEL);
114 	if (!initiator)
115 		return;
116 
117 	initiator->processor_pxm = cpu_pxm;
118 	initiator->has_cpu = node_state(pxm_to_node(cpu_pxm), N_CPU);
119 	list_add_tail(&initiator->node, &initiators);
120 }
121 
122 static __init void alloc_memory_target(unsigned int mem_pxm,
123 		resource_size_t start, resource_size_t len)
124 {
125 	struct memory_target *target;
126 
127 	target = find_mem_target(mem_pxm);
128 	if (!target) {
129 		target = kzalloc(sizeof(*target), GFP_KERNEL);
130 		if (!target)
131 			return;
132 		target->memory_pxm = mem_pxm;
133 		target->processor_pxm = PXM_INVAL;
134 		target->memregions = (struct resource) {
135 			.name	= "ACPI mem",
136 			.start	= 0,
137 			.end	= -1,
138 			.flags	= IORESOURCE_MEM,
139 		};
140 		list_add_tail(&target->node, &targets);
141 		INIT_LIST_HEAD(&target->caches);
142 	}
143 
144 	/*
145 	 * There are potentially multiple ranges per PXM, so record each
146 	 * in the per-target memregions resource tree.
147 	 */
148 	if (!__request_region(&target->memregions, start, len, "memory target",
149 				IORESOURCE_MEM))
150 		pr_warn("failed to reserve %#llx - %#llx in pxm: %d\n",
151 				start, start + len, mem_pxm);
152 }
153 
154 static __init const char *hmat_data_type(u8 type)
155 {
156 	switch (type) {
157 	case ACPI_HMAT_ACCESS_LATENCY:
158 		return "Access Latency";
159 	case ACPI_HMAT_READ_LATENCY:
160 		return "Read Latency";
161 	case ACPI_HMAT_WRITE_LATENCY:
162 		return "Write Latency";
163 	case ACPI_HMAT_ACCESS_BANDWIDTH:
164 		return "Access Bandwidth";
165 	case ACPI_HMAT_READ_BANDWIDTH:
166 		return "Read Bandwidth";
167 	case ACPI_HMAT_WRITE_BANDWIDTH:
168 		return "Write Bandwidth";
169 	default:
170 		return "Reserved";
171 	}
172 }
173 
174 static __init const char *hmat_data_type_suffix(u8 type)
175 {
176 	switch (type) {
177 	case ACPI_HMAT_ACCESS_LATENCY:
178 	case ACPI_HMAT_READ_LATENCY:
179 	case ACPI_HMAT_WRITE_LATENCY:
180 		return " nsec";
181 	case ACPI_HMAT_ACCESS_BANDWIDTH:
182 	case ACPI_HMAT_READ_BANDWIDTH:
183 	case ACPI_HMAT_WRITE_BANDWIDTH:
184 		return " MB/s";
185 	default:
186 		return "";
187 	}
188 }
189 
190 static u32 hmat_normalize(u16 entry, u64 base, u8 type)
191 {
192 	u32 value;
193 
194 	/*
195 	 * Check for invalid and overflow values
196 	 */
197 	if (entry == 0xffff || !entry)
198 		return 0;
199 	else if (base > (UINT_MAX / (entry)))
200 		return 0;
201 
202 	/*
203 	 * Divide by the base unit for version 1, convert latency from
204 	 * picosenonds to nanoseconds if revision 2.
205 	 */
206 	value = entry * base;
207 	if (hmat_revision == 1) {
208 		if (value < 10)
209 			return 0;
210 		value = DIV_ROUND_UP(value, 10);
211 	} else if (hmat_revision == 2) {
212 		switch (type) {
213 		case ACPI_HMAT_ACCESS_LATENCY:
214 		case ACPI_HMAT_READ_LATENCY:
215 		case ACPI_HMAT_WRITE_LATENCY:
216 			value = DIV_ROUND_UP(value, 1000);
217 			break;
218 		default:
219 			break;
220 		}
221 	}
222 	return value;
223 }
224 
225 static void hmat_update_target_access(struct memory_target *target,
226 				      u8 type, u32 value, int access)
227 {
228 	switch (type) {
229 	case ACPI_HMAT_ACCESS_LATENCY:
230 		target->hmem_attrs[access].read_latency = value;
231 		target->hmem_attrs[access].write_latency = value;
232 		break;
233 	case ACPI_HMAT_READ_LATENCY:
234 		target->hmem_attrs[access].read_latency = value;
235 		break;
236 	case ACPI_HMAT_WRITE_LATENCY:
237 		target->hmem_attrs[access].write_latency = value;
238 		break;
239 	case ACPI_HMAT_ACCESS_BANDWIDTH:
240 		target->hmem_attrs[access].read_bandwidth = value;
241 		target->hmem_attrs[access].write_bandwidth = value;
242 		break;
243 	case ACPI_HMAT_READ_BANDWIDTH:
244 		target->hmem_attrs[access].read_bandwidth = value;
245 		break;
246 	case ACPI_HMAT_WRITE_BANDWIDTH:
247 		target->hmem_attrs[access].write_bandwidth = value;
248 		break;
249 	default:
250 		break;
251 	}
252 }
253 
254 static __init void hmat_add_locality(struct acpi_hmat_locality *hmat_loc)
255 {
256 	struct memory_locality *loc;
257 
258 	loc = kzalloc(sizeof(*loc), GFP_KERNEL);
259 	if (!loc) {
260 		pr_notice_once("Failed to allocate HMAT locality\n");
261 		return;
262 	}
263 
264 	loc->hmat_loc = hmat_loc;
265 	list_add_tail(&loc->node, &localities);
266 
267 	switch (hmat_loc->data_type) {
268 	case ACPI_HMAT_ACCESS_LATENCY:
269 		localities_types[READ_LATENCY] = loc;
270 		localities_types[WRITE_LATENCY] = loc;
271 		break;
272 	case ACPI_HMAT_READ_LATENCY:
273 		localities_types[READ_LATENCY] = loc;
274 		break;
275 	case ACPI_HMAT_WRITE_LATENCY:
276 		localities_types[WRITE_LATENCY] = loc;
277 		break;
278 	case ACPI_HMAT_ACCESS_BANDWIDTH:
279 		localities_types[READ_BANDWIDTH] = loc;
280 		localities_types[WRITE_BANDWIDTH] = loc;
281 		break;
282 	case ACPI_HMAT_READ_BANDWIDTH:
283 		localities_types[READ_BANDWIDTH] = loc;
284 		break;
285 	case ACPI_HMAT_WRITE_BANDWIDTH:
286 		localities_types[WRITE_BANDWIDTH] = loc;
287 		break;
288 	default:
289 		break;
290 	}
291 }
292 
293 static __init int hmat_parse_locality(union acpi_subtable_headers *header,
294 				      const unsigned long end)
295 {
296 	struct acpi_hmat_locality *hmat_loc = (void *)header;
297 	struct memory_target *target;
298 	unsigned int init, targ, total_size, ipds, tpds;
299 	u32 *inits, *targs, value;
300 	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 			 hmat_loc->header.length);
306 		return -EINVAL;
307 	}
308 
309 	type = hmat_loc->data_type;
310 	mem_hier = hmat_loc->flags & ACPI_HMAT_MEMORY_HIERARCHY;
311 	ipds = hmat_loc->number_of_initiator_Pds;
312 	tpds = hmat_loc->number_of_target_Pds;
313 	total_size = sizeof(*hmat_loc) + sizeof(*entries) * ipds * tpds +
314 		     sizeof(*inits) * ipds + sizeof(*targs) * tpds;
315 	if (hmat_loc->header.length < total_size) {
316 		pr_notice("Unexpected locality header length:%u, minimum required:%u\n",
317 			 hmat_loc->header.length, total_size);
318 		return -EINVAL;
319 	}
320 
321 	pr_info("Locality: Flags:%02x Type:%s Initiator Domains:%u Target Domains:%u Base:%lld\n",
322 		hmat_loc->flags, hmat_data_type(type), ipds, tpds,
323 		hmat_loc->entry_base_unit);
324 
325 	inits = (u32 *)(hmat_loc + 1);
326 	targs = inits + ipds;
327 	entries = (u16 *)(targs + tpds);
328 	for (init = 0; init < ipds; init++) {
329 		alloc_memory_initiator(inits[init]);
330 		for (targ = 0; targ < tpds; targ++) {
331 			value = hmat_normalize(entries[init * tpds + targ],
332 					       hmat_loc->entry_base_unit,
333 					       type);
334 			pr_info("  Initiator-Target[%u-%u]:%u%s\n",
335 				inits[init], targs[targ], value,
336 				hmat_data_type_suffix(type));
337 
338 			if (mem_hier == ACPI_HMAT_MEMORY) {
339 				target = find_mem_target(targs[targ]);
340 				if (target && target->processor_pxm == inits[init]) {
341 					hmat_update_target_access(target, type, value, 0);
342 					/* If the node has a CPU, update access 1 */
343 					if (node_state(pxm_to_node(inits[init]), N_CPU))
344 						hmat_update_target_access(target, type, value, 1);
345 				}
346 			}
347 		}
348 	}
349 
350 	if (mem_hier == ACPI_HMAT_MEMORY)
351 		hmat_add_locality(hmat_loc);
352 
353 	return 0;
354 }
355 
356 static __init int hmat_parse_cache(union acpi_subtable_headers *header,
357 				   const unsigned long end)
358 {
359 	struct acpi_hmat_cache *cache = (void *)header;
360 	struct memory_target *target;
361 	struct target_cache *tcache;
362 	u32 attrs;
363 
364 	if (cache->header.length < sizeof(*cache)) {
365 		pr_notice("Unexpected cache header length: %u\n",
366 			 cache->header.length);
367 		return -EINVAL;
368 	}
369 
370 	attrs = cache->cache_attributes;
371 	pr_info("Cache: Domain:%u Size:%llu Attrs:%08x SMBIOS Handles:%d\n",
372 		cache->memory_PD, cache->cache_size, attrs,
373 		cache->number_of_SMBIOShandles);
374 
375 	target = find_mem_target(cache->memory_PD);
376 	if (!target)
377 		return 0;
378 
379 	tcache = kzalloc(sizeof(*tcache), GFP_KERNEL);
380 	if (!tcache) {
381 		pr_notice_once("Failed to allocate HMAT cache info\n");
382 		return 0;
383 	}
384 
385 	tcache->cache_attrs.size = cache->cache_size;
386 	tcache->cache_attrs.level = (attrs & ACPI_HMAT_CACHE_LEVEL) >> 4;
387 	tcache->cache_attrs.line_size = (attrs & ACPI_HMAT_CACHE_LINE_SIZE) >> 16;
388 
389 	switch ((attrs & ACPI_HMAT_CACHE_ASSOCIATIVITY) >> 8) {
390 	case ACPI_HMAT_CA_DIRECT_MAPPED:
391 		tcache->cache_attrs.indexing = NODE_CACHE_DIRECT_MAP;
392 		break;
393 	case ACPI_HMAT_CA_COMPLEX_CACHE_INDEXING:
394 		tcache->cache_attrs.indexing = NODE_CACHE_INDEXED;
395 		break;
396 	case ACPI_HMAT_CA_NONE:
397 	default:
398 		tcache->cache_attrs.indexing = NODE_CACHE_OTHER;
399 		break;
400 	}
401 
402 	switch ((attrs & ACPI_HMAT_WRITE_POLICY) >> 12) {
403 	case ACPI_HMAT_CP_WB:
404 		tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_BACK;
405 		break;
406 	case ACPI_HMAT_CP_WT:
407 		tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_THROUGH;
408 		break;
409 	case ACPI_HMAT_CP_NONE:
410 	default:
411 		tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_OTHER;
412 		break;
413 	}
414 	list_add_tail(&tcache->node, &target->caches);
415 
416 	return 0;
417 }
418 
419 static int __init hmat_parse_proximity_domain(union acpi_subtable_headers *header,
420 					      const unsigned long end)
421 {
422 	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 			 p->header.length);
428 		return -EINVAL;
429 	}
430 
431 	if (hmat_revision == 1)
432 		pr_info("Memory (%#llx length %#llx) Flags:%04x Processor Domain:%u Memory Domain:%u\n",
433 			p->reserved3, p->reserved4, p->flags, p->processor_PD,
434 			p->memory_PD);
435 	else
436 		pr_info("Memory Flags:%04x Processor Domain:%u Memory Domain:%u\n",
437 			p->flags, p->processor_PD, p->memory_PD);
438 
439 	if ((hmat_revision == 1 && p->flags & ACPI_HMAT_MEMORY_PD_VALID) ||
440 	    hmat_revision > 1) {
441 		target = find_mem_target(p->memory_PD);
442 		if (!target) {
443 			pr_debug("Memory Domain missing from SRAT\n");
444 			return -EINVAL;
445 		}
446 	}
447 	if (target && p->flags & ACPI_HMAT_PROCESSOR_PD_VALID) {
448 		int p_node = pxm_to_node(p->processor_PD);
449 
450 		if (p_node == NUMA_NO_NODE) {
451 			pr_debug("Invalid Processor Domain\n");
452 			return -EINVAL;
453 		}
454 		target->processor_pxm = p->processor_PD;
455 	}
456 
457 	return 0;
458 }
459 
460 static int __init hmat_parse_subtable(union acpi_subtable_headers *header,
461 				      const unsigned long end)
462 {
463 	struct acpi_hmat_structure *hdr = (void *)header;
464 
465 	if (!hdr)
466 		return -EINVAL;
467 
468 	switch (hdr->type) {
469 	case ACPI_HMAT_TYPE_PROXIMITY:
470 		return hmat_parse_proximity_domain(header, end);
471 	case ACPI_HMAT_TYPE_LOCALITY:
472 		return hmat_parse_locality(header, end);
473 	case ACPI_HMAT_TYPE_CACHE:
474 		return hmat_parse_cache(header, end);
475 	default:
476 		return -EINVAL;
477 	}
478 }
479 
480 static __init int srat_parse_mem_affinity(union acpi_subtable_headers *header,
481 					  const unsigned long end)
482 {
483 	struct acpi_srat_mem_affinity *ma = (void *)header;
484 
485 	if (!ma)
486 		return -EINVAL;
487 	if (!(ma->flags & ACPI_SRAT_MEM_ENABLED))
488 		return 0;
489 	alloc_memory_target(ma->proximity_domain, ma->base_address, ma->length);
490 	return 0;
491 }
492 
493 static u32 hmat_initiator_perf(struct memory_target *target,
494 			       struct memory_initiator *initiator,
495 			       struct acpi_hmat_locality *hmat_loc)
496 {
497 	unsigned int ipds, tpds, i, idx = 0, tdx = 0;
498 	u32 *inits, *targs;
499 	u16 *entries;
500 
501 	ipds = hmat_loc->number_of_initiator_Pds;
502 	tpds = hmat_loc->number_of_target_Pds;
503 	inits = (u32 *)(hmat_loc + 1);
504 	targs = inits + ipds;
505 	entries = (u16 *)(targs + tpds);
506 
507 	for (i = 0; i < ipds; i++) {
508 		if (inits[i] == initiator->processor_pxm) {
509 			idx = i;
510 			break;
511 		}
512 	}
513 
514 	if (i == ipds)
515 		return 0;
516 
517 	for (i = 0; i < tpds; i++) {
518 		if (targs[i] == target->memory_pxm) {
519 			tdx = i;
520 			break;
521 		}
522 	}
523 	if (i == tpds)
524 		return 0;
525 
526 	return hmat_normalize(entries[idx * tpds + tdx],
527 			      hmat_loc->entry_base_unit,
528 			      hmat_loc->data_type);
529 }
530 
531 static bool hmat_update_best(u8 type, u32 value, u32 *best)
532 {
533 	bool updated = false;
534 
535 	if (!value)
536 		return false;
537 
538 	switch (type) {
539 	case ACPI_HMAT_ACCESS_LATENCY:
540 	case ACPI_HMAT_READ_LATENCY:
541 	case ACPI_HMAT_WRITE_LATENCY:
542 		if (!*best || *best > value) {
543 			*best = value;
544 			updated = true;
545 		}
546 		break;
547 	case ACPI_HMAT_ACCESS_BANDWIDTH:
548 	case ACPI_HMAT_READ_BANDWIDTH:
549 	case ACPI_HMAT_WRITE_BANDWIDTH:
550 		if (!*best || *best < value) {
551 			*best = value;
552 			updated = true;
553 		}
554 		break;
555 	}
556 
557 	return updated;
558 }
559 
560 static int initiator_cmp(void *priv, const struct list_head *a,
561 			 const struct list_head *b)
562 {
563 	struct memory_initiator *ia;
564 	struct memory_initiator *ib;
565 	unsigned long *p_nodes = priv;
566 
567 	ia = list_entry(a, struct memory_initiator, node);
568 	ib = list_entry(b, struct memory_initiator, node);
569 
570 	set_bit(ia->processor_pxm, p_nodes);
571 	set_bit(ib->processor_pxm, p_nodes);
572 
573 	return ia->processor_pxm - ib->processor_pxm;
574 }
575 
576 static void hmat_register_target_initiators(struct memory_target *target)
577 {
578 	static DECLARE_BITMAP(p_nodes, MAX_NUMNODES);
579 	struct memory_initiator *initiator;
580 	unsigned int mem_nid, cpu_nid;
581 	struct memory_locality *loc = NULL;
582 	u32 best = 0;
583 	bool access0done = false;
584 	int i;
585 
586 	mem_nid = pxm_to_node(target->memory_pxm);
587 	/*
588 	 * If the Address Range Structure provides a local processor pxm, link
589 	 * only that one. Otherwise, find the best performance attributes and
590 	 * register all initiators that match.
591 	 */
592 	if (target->processor_pxm != PXM_INVAL) {
593 		cpu_nid = pxm_to_node(target->processor_pxm);
594 		register_memory_node_under_compute_node(mem_nid, cpu_nid, 0);
595 		access0done = true;
596 		if (node_state(cpu_nid, N_CPU)) {
597 			register_memory_node_under_compute_node(mem_nid, cpu_nid, 1);
598 			return;
599 		}
600 	}
601 
602 	if (list_empty(&localities))
603 		return;
604 
605 	/*
606 	 * We need the initiator list sorted so we can use bitmap_clear for
607 	 * previously set initiators when we find a better memory accessor.
608 	 * We'll also use the sorting to prime the candidate nodes with known
609 	 * initiators.
610 	 */
611 	bitmap_zero(p_nodes, MAX_NUMNODES);
612 	list_sort(p_nodes, &initiators, initiator_cmp);
613 	if (!access0done) {
614 		for (i = WRITE_LATENCY; i <= READ_BANDWIDTH; i++) {
615 			loc = localities_types[i];
616 			if (!loc)
617 				continue;
618 
619 			best = 0;
620 			list_for_each_entry(initiator, &initiators, node) {
621 				u32 value;
622 
623 				if (!test_bit(initiator->processor_pxm, p_nodes))
624 					continue;
625 
626 				value = hmat_initiator_perf(target, initiator,
627 							    loc->hmat_loc);
628 				if (hmat_update_best(loc->hmat_loc->data_type, value, &best))
629 					bitmap_clear(p_nodes, 0, initiator->processor_pxm);
630 				if (value != best)
631 					clear_bit(initiator->processor_pxm, p_nodes);
632 			}
633 			if (best)
634 				hmat_update_target_access(target, loc->hmat_loc->data_type,
635 							  best, 0);
636 		}
637 
638 		for_each_set_bit(i, p_nodes, MAX_NUMNODES) {
639 			cpu_nid = pxm_to_node(i);
640 			register_memory_node_under_compute_node(mem_nid, cpu_nid, 0);
641 		}
642 	}
643 
644 	/* Access 1 ignores Generic Initiators */
645 	bitmap_zero(p_nodes, MAX_NUMNODES);
646 	list_sort(p_nodes, &initiators, initiator_cmp);
647 	best = 0;
648 	for (i = WRITE_LATENCY; i <= READ_BANDWIDTH; i++) {
649 		loc = localities_types[i];
650 		if (!loc)
651 			continue;
652 
653 		best = 0;
654 		list_for_each_entry(initiator, &initiators, node) {
655 			u32 value;
656 
657 			if (!initiator->has_cpu) {
658 				clear_bit(initiator->processor_pxm, p_nodes);
659 				continue;
660 			}
661 			if (!test_bit(initiator->processor_pxm, p_nodes))
662 				continue;
663 
664 			value = hmat_initiator_perf(target, initiator, loc->hmat_loc);
665 			if (hmat_update_best(loc->hmat_loc->data_type, value, &best))
666 				bitmap_clear(p_nodes, 0, initiator->processor_pxm);
667 			if (value != best)
668 				clear_bit(initiator->processor_pxm, p_nodes);
669 		}
670 		if (best)
671 			hmat_update_target_access(target, loc->hmat_loc->data_type, best, 1);
672 	}
673 	for_each_set_bit(i, p_nodes, MAX_NUMNODES) {
674 		cpu_nid = pxm_to_node(i);
675 		register_memory_node_under_compute_node(mem_nid, cpu_nid, 1);
676 	}
677 }
678 
679 static void hmat_register_target_cache(struct memory_target *target)
680 {
681 	unsigned mem_nid = pxm_to_node(target->memory_pxm);
682 	struct target_cache *tcache;
683 
684 	list_for_each_entry(tcache, &target->caches, node)
685 		node_add_cache(mem_nid, &tcache->cache_attrs);
686 }
687 
688 static void hmat_register_target_perf(struct memory_target *target, int access)
689 {
690 	unsigned mem_nid = pxm_to_node(target->memory_pxm);
691 	node_set_perf_attrs(mem_nid, &target->hmem_attrs[access], access);
692 }
693 
694 static void hmat_register_target_devices(struct memory_target *target)
695 {
696 	struct resource *res;
697 
698 	/*
699 	 * Do not bother creating devices if no driver is available to
700 	 * consume them.
701 	 */
702 	if (!IS_ENABLED(CONFIG_DEV_DAX_HMEM))
703 		return;
704 
705 	for (res = target->memregions.child; res; res = res->sibling) {
706 		int target_nid = pxm_to_node(target->memory_pxm);
707 
708 		hmem_register_device(target_nid, res);
709 	}
710 }
711 
712 static void hmat_register_target(struct memory_target *target)
713 {
714 	int nid = pxm_to_node(target->memory_pxm);
715 
716 	/*
717 	 * Devices may belong to either an offline or online
718 	 * node, so unconditionally add them.
719 	 */
720 	hmat_register_target_devices(target);
721 
722 	/*
723 	 * Skip offline nodes. This can happen when memory
724 	 * marked EFI_MEMORY_SP, "specific purpose", is applied
725 	 * to all the memory in a proximity domain leading to
726 	 * the node being marked offline / unplugged, or if
727 	 * memory-only "hotplug" node is offline.
728 	 */
729 	if (nid == NUMA_NO_NODE || !node_online(nid))
730 		return;
731 
732 	mutex_lock(&target_lock);
733 	if (!target->registered) {
734 		hmat_register_target_initiators(target);
735 		hmat_register_target_cache(target);
736 		hmat_register_target_perf(target, 0);
737 		hmat_register_target_perf(target, 1);
738 		target->registered = true;
739 	}
740 	mutex_unlock(&target_lock);
741 }
742 
743 static void hmat_register_targets(void)
744 {
745 	struct memory_target *target;
746 
747 	list_for_each_entry(target, &targets, node)
748 		hmat_register_target(target);
749 }
750 
751 static int hmat_callback(struct notifier_block *self,
752 			 unsigned long action, void *arg)
753 {
754 	struct memory_target *target;
755 	struct memory_notify *mnb = arg;
756 	int pxm, nid = mnb->status_change_nid;
757 
758 	if (nid == NUMA_NO_NODE || action != MEM_ONLINE)
759 		return NOTIFY_OK;
760 
761 	pxm = node_to_pxm(nid);
762 	target = find_mem_target(pxm);
763 	if (!target)
764 		return NOTIFY_OK;
765 
766 	hmat_register_target(target);
767 	return NOTIFY_OK;
768 }
769 
770 static __init void hmat_free_structures(void)
771 {
772 	struct memory_target *target, *tnext;
773 	struct memory_locality *loc, *lnext;
774 	struct memory_initiator *initiator, *inext;
775 	struct target_cache *tcache, *cnext;
776 
777 	list_for_each_entry_safe(target, tnext, &targets, node) {
778 		struct resource *res, *res_next;
779 
780 		list_for_each_entry_safe(tcache, cnext, &target->caches, node) {
781 			list_del(&tcache->node);
782 			kfree(tcache);
783 		}
784 
785 		list_del(&target->node);
786 		res = target->memregions.child;
787 		while (res) {
788 			res_next = res->sibling;
789 			__release_region(&target->memregions, res->start,
790 					resource_size(res));
791 			res = res_next;
792 		}
793 		kfree(target);
794 	}
795 
796 	list_for_each_entry_safe(initiator, inext, &initiators, node) {
797 		list_del(&initiator->node);
798 		kfree(initiator);
799 	}
800 
801 	list_for_each_entry_safe(loc, lnext, &localities, node) {
802 		list_del(&loc->node);
803 		kfree(loc);
804 	}
805 }
806 
807 static __init int hmat_init(void)
808 {
809 	struct acpi_table_header *tbl;
810 	enum acpi_hmat_type i;
811 	acpi_status status;
812 
813 	if (srat_disabled() || hmat_disable)
814 		return 0;
815 
816 	status = acpi_get_table(ACPI_SIG_SRAT, 0, &tbl);
817 	if (ACPI_FAILURE(status))
818 		return 0;
819 
820 	if (acpi_table_parse_entries(ACPI_SIG_SRAT,
821 				sizeof(struct acpi_table_srat),
822 				ACPI_SRAT_TYPE_MEMORY_AFFINITY,
823 				srat_parse_mem_affinity, 0) < 0)
824 		goto out_put;
825 	acpi_put_table(tbl);
826 
827 	status = acpi_get_table(ACPI_SIG_HMAT, 0, &tbl);
828 	if (ACPI_FAILURE(status))
829 		goto out_put;
830 
831 	hmat_revision = tbl->revision;
832 	switch (hmat_revision) {
833 	case 1:
834 	case 2:
835 		break;
836 	default:
837 		pr_notice("Ignoring: Unknown revision:%d\n", hmat_revision);
838 		goto out_put;
839 	}
840 
841 	for (i = ACPI_HMAT_TYPE_PROXIMITY; i < ACPI_HMAT_TYPE_RESERVED; i++) {
842 		if (acpi_table_parse_entries(ACPI_SIG_HMAT,
843 					     sizeof(struct acpi_table_hmat), i,
844 					     hmat_parse_subtable, 0) < 0) {
845 			pr_notice("Ignoring: Invalid table");
846 			goto out_put;
847 		}
848 	}
849 	hmat_register_targets();
850 
851 	/* Keep the table and structures if the notifier may use them */
852 	if (!hotplug_memory_notifier(hmat_callback, HMAT_CALLBACK_PRI))
853 		return 0;
854 out_put:
855 	hmat_free_structures();
856 	acpi_put_table(tbl);
857 	return 0;
858 }
859 device_initcall(hmat_init);
860