1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019, Intel Corporation.
5 * Heterogeneous Memory Attributes Table (HMAT) representation
7 * This program parses and reports the platform's HMAT tables, and registers
8 * the applicable attributes with the node's interfaces.
11 #include <linux/acpi.h>
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/list_sort.h>
17 #include <linux/memory.h>
18 #include <linux/mutex.h>
19 #include <linux/node.h>
20 #include <linux/sysfs.h>
22 static u8 hmat_revision;
24 static LIST_HEAD(targets);
25 static LIST_HEAD(initiators);
26 static LIST_HEAD(localities);
28 static DEFINE_MUTEX(target_lock);
31 * The defined enum order is used to prioritize attributes to break ties when
32 * selecting the best performing node.
41 static struct memory_locality *localities_types[4];
44 struct list_head node;
45 struct node_cache_attrs cache_attrs;
48 struct memory_target {
49 struct list_head node;
50 unsigned int memory_pxm;
51 unsigned int processor_pxm;
52 struct node_hmem_attrs hmem_attrs;
53 struct list_head caches;
54 struct node_cache_attrs cache_attrs;
58 struct memory_initiator {
59 struct list_head node;
60 unsigned int processor_pxm;
63 struct memory_locality {
64 struct list_head node;
65 struct acpi_hmat_locality *hmat_loc;
68 static struct memory_initiator *find_mem_initiator(unsigned int cpu_pxm)
70 struct memory_initiator *initiator;
72 list_for_each_entry(initiator, &initiators, node)
73 if (initiator->processor_pxm == cpu_pxm)
78 static struct memory_target *find_mem_target(unsigned int mem_pxm)
80 struct memory_target *target;
82 list_for_each_entry(target, &targets, node)
83 if (target->memory_pxm == mem_pxm)
88 static __init void alloc_memory_initiator(unsigned int cpu_pxm)
90 struct memory_initiator *initiator;
92 if (pxm_to_node(cpu_pxm) == NUMA_NO_NODE)
95 initiator = find_mem_initiator(cpu_pxm);
99 initiator = kzalloc(sizeof(*initiator), GFP_KERNEL);
103 initiator->processor_pxm = cpu_pxm;
104 list_add_tail(&initiator->node, &initiators);
107 static __init void alloc_memory_target(unsigned int mem_pxm)
109 struct memory_target *target;
111 target = find_mem_target(mem_pxm);
115 target = kzalloc(sizeof(*target), GFP_KERNEL);
119 target->memory_pxm = mem_pxm;
120 target->processor_pxm = PXM_INVAL;
121 list_add_tail(&target->node, &targets);
122 INIT_LIST_HEAD(&target->caches);
125 static __init const char *hmat_data_type(u8 type)
128 case ACPI_HMAT_ACCESS_LATENCY:
129 return "Access Latency";
130 case ACPI_HMAT_READ_LATENCY:
131 return "Read Latency";
132 case ACPI_HMAT_WRITE_LATENCY:
133 return "Write Latency";
134 case ACPI_HMAT_ACCESS_BANDWIDTH:
135 return "Access Bandwidth";
136 case ACPI_HMAT_READ_BANDWIDTH:
137 return "Read Bandwidth";
138 case ACPI_HMAT_WRITE_BANDWIDTH:
139 return "Write Bandwidth";
145 static __init const char *hmat_data_type_suffix(u8 type)
148 case ACPI_HMAT_ACCESS_LATENCY:
149 case ACPI_HMAT_READ_LATENCY:
150 case ACPI_HMAT_WRITE_LATENCY:
152 case ACPI_HMAT_ACCESS_BANDWIDTH:
153 case ACPI_HMAT_READ_BANDWIDTH:
154 case ACPI_HMAT_WRITE_BANDWIDTH:
161 static u32 hmat_normalize(u16 entry, u64 base, u8 type)
166 * Check for invalid and overflow values
168 if (entry == 0xffff || !entry)
170 else if (base > (UINT_MAX / (entry)))
174 * Divide by the base unit for version 1, convert latency from
175 * picosenonds to nanoseconds if revision 2.
177 value = entry * base;
178 if (hmat_revision == 1) {
181 value = DIV_ROUND_UP(value, 10);
182 } else if (hmat_revision == 2) {
184 case ACPI_HMAT_ACCESS_LATENCY:
185 case ACPI_HMAT_READ_LATENCY:
186 case ACPI_HMAT_WRITE_LATENCY:
187 value = DIV_ROUND_UP(value, 1000);
196 static void hmat_update_target_access(struct memory_target *target,
200 case ACPI_HMAT_ACCESS_LATENCY:
201 target->hmem_attrs.read_latency = value;
202 target->hmem_attrs.write_latency = value;
204 case ACPI_HMAT_READ_LATENCY:
205 target->hmem_attrs.read_latency = value;
207 case ACPI_HMAT_WRITE_LATENCY:
208 target->hmem_attrs.write_latency = value;
210 case ACPI_HMAT_ACCESS_BANDWIDTH:
211 target->hmem_attrs.read_bandwidth = value;
212 target->hmem_attrs.write_bandwidth = value;
214 case ACPI_HMAT_READ_BANDWIDTH:
215 target->hmem_attrs.read_bandwidth = value;
217 case ACPI_HMAT_WRITE_BANDWIDTH:
218 target->hmem_attrs.write_bandwidth = value;
225 static __init void hmat_add_locality(struct acpi_hmat_locality *hmat_loc)
227 struct memory_locality *loc;
229 loc = kzalloc(sizeof(*loc), GFP_KERNEL);
231 pr_notice_once("Failed to allocate HMAT locality\n");
235 loc->hmat_loc = hmat_loc;
236 list_add_tail(&loc->node, &localities);
238 switch (hmat_loc->data_type) {
239 case ACPI_HMAT_ACCESS_LATENCY:
240 localities_types[READ_LATENCY] = loc;
241 localities_types[WRITE_LATENCY] = loc;
243 case ACPI_HMAT_READ_LATENCY:
244 localities_types[READ_LATENCY] = loc;
246 case ACPI_HMAT_WRITE_LATENCY:
247 localities_types[WRITE_LATENCY] = loc;
249 case ACPI_HMAT_ACCESS_BANDWIDTH:
250 localities_types[READ_BANDWIDTH] = loc;
251 localities_types[WRITE_BANDWIDTH] = loc;
253 case ACPI_HMAT_READ_BANDWIDTH:
254 localities_types[READ_BANDWIDTH] = loc;
256 case ACPI_HMAT_WRITE_BANDWIDTH:
257 localities_types[WRITE_BANDWIDTH] = loc;
264 static __init int hmat_parse_locality(union acpi_subtable_headers *header,
265 const unsigned long end)
267 struct acpi_hmat_locality *hmat_loc = (void *)header;
268 struct memory_target *target;
269 unsigned int init, targ, total_size, ipds, tpds;
270 u32 *inits, *targs, value;
274 if (hmat_loc->header.length < sizeof(*hmat_loc)) {
275 pr_notice("HMAT: Unexpected locality header length: %d\n",
276 hmat_loc->header.length);
280 type = hmat_loc->data_type;
281 mem_hier = hmat_loc->flags & ACPI_HMAT_MEMORY_HIERARCHY;
282 ipds = hmat_loc->number_of_initiator_Pds;
283 tpds = hmat_loc->number_of_target_Pds;
284 total_size = sizeof(*hmat_loc) + sizeof(*entries) * ipds * tpds +
285 sizeof(*inits) * ipds + sizeof(*targs) * tpds;
286 if (hmat_loc->header.length < total_size) {
287 pr_notice("HMAT: Unexpected locality header length:%d, minimum required:%d\n",
288 hmat_loc->header.length, total_size);
292 pr_info("HMAT: Locality: Flags:%02x Type:%s Initiator Domains:%d Target Domains:%d Base:%lld\n",
293 hmat_loc->flags, hmat_data_type(type), ipds, tpds,
294 hmat_loc->entry_base_unit);
296 inits = (u32 *)(hmat_loc + 1);
297 targs = inits + ipds;
298 entries = (u16 *)(targs + tpds);
299 for (init = 0; init < ipds; init++) {
300 alloc_memory_initiator(inits[init]);
301 for (targ = 0; targ < tpds; targ++) {
302 value = hmat_normalize(entries[init * tpds + targ],
303 hmat_loc->entry_base_unit,
305 pr_info(" Initiator-Target[%d-%d]:%d%s\n",
306 inits[init], targs[targ], value,
307 hmat_data_type_suffix(type));
309 if (mem_hier == ACPI_HMAT_MEMORY) {
310 target = find_mem_target(targs[targ]);
311 if (target && target->processor_pxm == inits[init])
312 hmat_update_target_access(target, type, value);
317 if (mem_hier == ACPI_HMAT_MEMORY)
318 hmat_add_locality(hmat_loc);
323 static __init int hmat_parse_cache(union acpi_subtable_headers *header,
324 const unsigned long end)
326 struct acpi_hmat_cache *cache = (void *)header;
327 struct memory_target *target;
328 struct target_cache *tcache;
331 if (cache->header.length < sizeof(*cache)) {
332 pr_notice("HMAT: Unexpected cache header length: %d\n",
333 cache->header.length);
337 attrs = cache->cache_attributes;
338 pr_info("HMAT: Cache: Domain:%d Size:%llu Attrs:%08x SMBIOS Handles:%d\n",
339 cache->memory_PD, cache->cache_size, attrs,
340 cache->number_of_SMBIOShandles);
342 target = find_mem_target(cache->memory_PD);
346 tcache = kzalloc(sizeof(*tcache), GFP_KERNEL);
348 pr_notice_once("Failed to allocate HMAT cache info\n");
352 tcache->cache_attrs.size = cache->cache_size;
353 tcache->cache_attrs.level = (attrs & ACPI_HMAT_CACHE_LEVEL) >> 4;
354 tcache->cache_attrs.line_size = (attrs & ACPI_HMAT_CACHE_LINE_SIZE) >> 16;
356 switch ((attrs & ACPI_HMAT_CACHE_ASSOCIATIVITY) >> 8) {
357 case ACPI_HMAT_CA_DIRECT_MAPPED:
358 tcache->cache_attrs.indexing = NODE_CACHE_DIRECT_MAP;
360 case ACPI_HMAT_CA_COMPLEX_CACHE_INDEXING:
361 tcache->cache_attrs.indexing = NODE_CACHE_INDEXED;
363 case ACPI_HMAT_CA_NONE:
365 tcache->cache_attrs.indexing = NODE_CACHE_OTHER;
369 switch ((attrs & ACPI_HMAT_WRITE_POLICY) >> 12) {
370 case ACPI_HMAT_CP_WB:
371 tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_BACK;
373 case ACPI_HMAT_CP_WT:
374 tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_THROUGH;
376 case ACPI_HMAT_CP_NONE:
378 tcache->cache_attrs.write_policy = NODE_CACHE_WRITE_OTHER;
381 list_add_tail(&tcache->node, &target->caches);
386 static int __init hmat_parse_proximity_domain(union acpi_subtable_headers *header,
387 const unsigned long end)
389 struct acpi_hmat_proximity_domain *p = (void *)header;
390 struct memory_target *target = NULL;
392 if (p->header.length != sizeof(*p)) {
393 pr_notice("HMAT: Unexpected address range header length: %d\n",
398 if (hmat_revision == 1)
399 pr_info("HMAT: Memory (%#llx length %#llx) Flags:%04x Processor Domain:%d Memory Domain:%d\n",
400 p->reserved3, p->reserved4, p->flags, p->processor_PD,
403 pr_info("HMAT: Memory Flags:%04x Processor Domain:%d Memory Domain:%d\n",
404 p->flags, p->processor_PD, p->memory_PD);
406 if (p->flags & ACPI_HMAT_MEMORY_PD_VALID && hmat_revision == 1) {
407 target = find_mem_target(p->memory_PD);
409 pr_debug("HMAT: Memory Domain missing from SRAT\n");
413 if (target && p->flags & ACPI_HMAT_PROCESSOR_PD_VALID) {
414 int p_node = pxm_to_node(p->processor_PD);
416 if (p_node == NUMA_NO_NODE) {
417 pr_debug("HMAT: Invalid Processor Domain\n");
420 target->processor_pxm = p_node;
426 static int __init hmat_parse_subtable(union acpi_subtable_headers *header,
427 const unsigned long end)
429 struct acpi_hmat_structure *hdr = (void *)header;
435 case ACPI_HMAT_TYPE_PROXIMITY:
436 return hmat_parse_proximity_domain(header, end);
437 case ACPI_HMAT_TYPE_LOCALITY:
438 return hmat_parse_locality(header, end);
439 case ACPI_HMAT_TYPE_CACHE:
440 return hmat_parse_cache(header, end);
446 static __init int srat_parse_mem_affinity(union acpi_subtable_headers *header,
447 const unsigned long end)
449 struct acpi_srat_mem_affinity *ma = (void *)header;
453 if (!(ma->flags & ACPI_SRAT_MEM_ENABLED))
455 alloc_memory_target(ma->proximity_domain);
459 static u32 hmat_initiator_perf(struct memory_target *target,
460 struct memory_initiator *initiator,
461 struct acpi_hmat_locality *hmat_loc)
463 unsigned int ipds, tpds, i, idx = 0, tdx = 0;
467 ipds = hmat_loc->number_of_initiator_Pds;
468 tpds = hmat_loc->number_of_target_Pds;
469 inits = (u32 *)(hmat_loc + 1);
470 targs = inits + ipds;
471 entries = (u16 *)(targs + tpds);
473 for (i = 0; i < ipds; i++) {
474 if (inits[i] == initiator->processor_pxm) {
483 for (i = 0; i < tpds; i++) {
484 if (targs[i] == target->memory_pxm) {
492 return hmat_normalize(entries[idx * tpds + tdx],
493 hmat_loc->entry_base_unit,
494 hmat_loc->data_type);
497 static bool hmat_update_best(u8 type, u32 value, u32 *best)
499 bool updated = false;
505 case ACPI_HMAT_ACCESS_LATENCY:
506 case ACPI_HMAT_READ_LATENCY:
507 case ACPI_HMAT_WRITE_LATENCY:
508 if (!*best || *best > value) {
513 case ACPI_HMAT_ACCESS_BANDWIDTH:
514 case ACPI_HMAT_READ_BANDWIDTH:
515 case ACPI_HMAT_WRITE_BANDWIDTH:
516 if (!*best || *best < value) {
526 static int initiator_cmp(void *priv, struct list_head *a, struct list_head *b)
528 struct memory_initiator *ia;
529 struct memory_initiator *ib;
530 unsigned long *p_nodes = priv;
532 ia = list_entry(a, struct memory_initiator, node);
533 ib = list_entry(b, struct memory_initiator, node);
535 set_bit(ia->processor_pxm, p_nodes);
536 set_bit(ib->processor_pxm, p_nodes);
538 return ia->processor_pxm - ib->processor_pxm;
541 static void hmat_register_target_initiators(struct memory_target *target)
543 static DECLARE_BITMAP(p_nodes, MAX_NUMNODES);
544 struct memory_initiator *initiator;
545 unsigned int mem_nid, cpu_nid;
546 struct memory_locality *loc = NULL;
550 mem_nid = pxm_to_node(target->memory_pxm);
552 * If the Address Range Structure provides a local processor pxm, link
553 * only that one. Otherwise, find the best performance attributes and
554 * register all initiators that match.
556 if (target->processor_pxm != PXM_INVAL) {
557 cpu_nid = pxm_to_node(target->processor_pxm);
558 register_memory_node_under_compute_node(mem_nid, cpu_nid, 0);
562 if (list_empty(&localities))
566 * We need the initiator list sorted so we can use bitmap_clear for
567 * previously set initiators when we find a better memory accessor.
568 * We'll also use the sorting to prime the candidate nodes with known
571 bitmap_zero(p_nodes, MAX_NUMNODES);
572 list_sort(p_nodes, &initiators, initiator_cmp);
573 for (i = WRITE_LATENCY; i <= READ_BANDWIDTH; i++) {
574 loc = localities_types[i];
579 list_for_each_entry(initiator, &initiators, node) {
582 if (!test_bit(initiator->processor_pxm, p_nodes))
585 value = hmat_initiator_perf(target, initiator, loc->hmat_loc);
586 if (hmat_update_best(loc->hmat_loc->data_type, value, &best))
587 bitmap_clear(p_nodes, 0, initiator->processor_pxm);
589 clear_bit(initiator->processor_pxm, p_nodes);
592 hmat_update_target_access(target, loc->hmat_loc->data_type, best);
595 for_each_set_bit(i, p_nodes, MAX_NUMNODES) {
596 cpu_nid = pxm_to_node(i);
597 register_memory_node_under_compute_node(mem_nid, cpu_nid, 0);
601 static void hmat_register_target_cache(struct memory_target *target)
603 unsigned mem_nid = pxm_to_node(target->memory_pxm);
604 struct target_cache *tcache;
606 list_for_each_entry(tcache, &target->caches, node)
607 node_add_cache(mem_nid, &tcache->cache_attrs);
610 static void hmat_register_target_perf(struct memory_target *target)
612 unsigned mem_nid = pxm_to_node(target->memory_pxm);
613 node_set_perf_attrs(mem_nid, &target->hmem_attrs, 0);
616 static void hmat_register_target(struct memory_target *target)
618 int nid = pxm_to_node(target->memory_pxm);
621 * Skip offline nodes. This can happen when memory
622 * marked EFI_MEMORY_SP, "specific purpose", is applied
623 * to all the memory in a promixity domain leading to
624 * the node being marked offline / unplugged, or if
625 * memory-only "hotplug" node is offline.
627 if (nid == NUMA_NO_NODE || !node_online(nid))
630 mutex_lock(&target_lock);
631 if (!target->registered) {
632 hmat_register_target_initiators(target);
633 hmat_register_target_cache(target);
634 hmat_register_target_perf(target);
635 target->registered = true;
637 mutex_unlock(&target_lock);
640 static void hmat_register_targets(void)
642 struct memory_target *target;
644 list_for_each_entry(target, &targets, node)
645 hmat_register_target(target);
648 static int hmat_callback(struct notifier_block *self,
649 unsigned long action, void *arg)
651 struct memory_target *target;
652 struct memory_notify *mnb = arg;
653 int pxm, nid = mnb->status_change_nid;
655 if (nid == NUMA_NO_NODE || action != MEM_ONLINE)
658 pxm = node_to_pxm(nid);
659 target = find_mem_target(pxm);
663 hmat_register_target(target);
667 static struct notifier_block hmat_callback_nb = {
668 .notifier_call = hmat_callback,
672 static __init void hmat_free_structures(void)
674 struct memory_target *target, *tnext;
675 struct memory_locality *loc, *lnext;
676 struct memory_initiator *initiator, *inext;
677 struct target_cache *tcache, *cnext;
679 list_for_each_entry_safe(target, tnext, &targets, node) {
680 list_for_each_entry_safe(tcache, cnext, &target->caches, node) {
681 list_del(&tcache->node);
684 list_del(&target->node);
688 list_for_each_entry_safe(initiator, inext, &initiators, node) {
689 list_del(&initiator->node);
693 list_for_each_entry_safe(loc, lnext, &localities, node) {
694 list_del(&loc->node);
699 static __init int hmat_init(void)
701 struct acpi_table_header *tbl;
702 enum acpi_hmat_type i;
708 status = acpi_get_table(ACPI_SIG_SRAT, 0, &tbl);
709 if (ACPI_FAILURE(status))
712 if (acpi_table_parse_entries(ACPI_SIG_SRAT,
713 sizeof(struct acpi_table_srat),
714 ACPI_SRAT_TYPE_MEMORY_AFFINITY,
715 srat_parse_mem_affinity, 0) < 0)
719 status = acpi_get_table(ACPI_SIG_HMAT, 0, &tbl);
720 if (ACPI_FAILURE(status))
723 hmat_revision = tbl->revision;
724 switch (hmat_revision) {
729 pr_notice("Ignoring HMAT: Unknown revision:%d\n", hmat_revision);
733 for (i = ACPI_HMAT_TYPE_PROXIMITY; i < ACPI_HMAT_TYPE_RESERVED; i++) {
734 if (acpi_table_parse_entries(ACPI_SIG_HMAT,
735 sizeof(struct acpi_table_hmat), i,
736 hmat_parse_subtable, 0) < 0) {
737 pr_notice("Ignoring HMAT: Invalid table");
741 hmat_register_targets();
743 /* Keep the table and structures if the notifier may use them */
744 if (!register_hotmemory_notifier(&hmat_callback_nb))
747 hmat_free_structures();
751 subsys_initcall(hmat_init);