18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Memory subsystem support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
68c2ecf20Sopenharmony_ci *            Dave Hansen <haveblue@us.ibm.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This file provides the necessary infrastructure to represent
98c2ecf20Sopenharmony_ci * a SPARSEMEM-memory-model system's physical memory in /sysfs.
108c2ecf20Sopenharmony_ci * All arch-independent code that assumes MEMORY_HOTPLUG requires
118c2ecf20Sopenharmony_ci * SPARSEMEM should be contained here, or in mm/memory_hotplug.c.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/topology.h>
178c2ecf20Sopenharmony_ci#include <linux/capability.h>
188c2ecf20Sopenharmony_ci#include <linux/device.h>
198c2ecf20Sopenharmony_ci#include <linux/memory.h>
208c2ecf20Sopenharmony_ci#include <linux/memory_hotplug.h>
218c2ecf20Sopenharmony_ci#include <linux/mm.h>
228c2ecf20Sopenharmony_ci#include <linux/stat.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include <linux/xarray.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <linux/atomic.h>
278c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define MEMORY_CLASS_NAME	"memory"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic const char *const online_type_to_str[] = {
328c2ecf20Sopenharmony_ci	[MMOP_OFFLINE] = "offline",
338c2ecf20Sopenharmony_ci	[MMOP_ONLINE] = "online",
348c2ecf20Sopenharmony_ci	[MMOP_ONLINE_KERNEL] = "online_kernel",
358c2ecf20Sopenharmony_ci	[MMOP_ONLINE_MOVABLE] = "online_movable",
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciint memhp_online_type_from_str(const char *str)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	int i;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(online_type_to_str); i++) {
438c2ecf20Sopenharmony_ci		if (sysfs_streq(str, online_type_to_str[i]))
448c2ecf20Sopenharmony_ci			return i;
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci	return -EINVAL;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define to_memory_block(dev) container_of(dev, struct memory_block, dev)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int sections_per_block;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic inline unsigned long memory_block_id(unsigned long section_nr)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	return section_nr / sections_per_block;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline unsigned long pfn_to_block_id(unsigned long pfn)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return memory_block_id(pfn_to_section_nr(pfn));
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic inline unsigned long phys_to_block_id(unsigned long phys)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	return pfn_to_block_id(PFN_DOWN(phys));
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int memory_subsys_online(struct device *dev);
698c2ecf20Sopenharmony_cistatic int memory_subsys_offline(struct device *dev);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic struct bus_type memory_subsys = {
728c2ecf20Sopenharmony_ci	.name = MEMORY_CLASS_NAME,
738c2ecf20Sopenharmony_ci	.dev_name = MEMORY_CLASS_NAME,
748c2ecf20Sopenharmony_ci	.online = memory_subsys_online,
758c2ecf20Sopenharmony_ci	.offline = memory_subsys_offline,
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci * Memory blocks are cached in a local radix tree to avoid
808c2ecf20Sopenharmony_ci * a costly linear search for the corresponding device on
818c2ecf20Sopenharmony_ci * the subsystem bus.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_cistatic DEFINE_XARRAY(memory_blocks);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic BLOCKING_NOTIFIER_HEAD(memory_chain);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciint register_memory_notifier(struct notifier_block *nb)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	return blocking_notifier_chain_register(&memory_chain, nb);
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ciEXPORT_SYMBOL(register_memory_notifier);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_civoid unregister_memory_notifier(struct notifier_block *nb)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	blocking_notifier_chain_unregister(&memory_chain, nb);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unregister_memory_notifier);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic void memory_block_release(struct device *dev)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	kfree(mem);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciunsigned long __weak memory_block_size_bytes(void)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	return MIN_MEMORY_BLOCK_SIZE;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(memory_block_size_bytes);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/*
1138c2ecf20Sopenharmony_ci * Show the first physical section index (number) of this memory block.
1148c2ecf20Sopenharmony_ci */
1158c2ecf20Sopenharmony_cistatic ssize_t phys_index_show(struct device *dev,
1168c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
1198c2ecf20Sopenharmony_ci	unsigned long phys_index;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	phys_index = mem->start_section_nr / sections_per_block;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%08lx\n", phys_index);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/*
1278c2ecf20Sopenharmony_ci * Legacy interface that we cannot remove. Always indicate "removable"
1288c2ecf20Sopenharmony_ci * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_cistatic ssize_t removable_show(struct device *dev, struct device_attribute *attr,
1318c2ecf20Sopenharmony_ci			      char *buf)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/*
1378c2ecf20Sopenharmony_ci * online, offline, going offline, etc.
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_cistatic ssize_t state_show(struct device *dev, struct device_attribute *attr,
1408c2ecf20Sopenharmony_ci			  char *buf)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
1438c2ecf20Sopenharmony_ci	const char *output;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * We can probably put these states in a nice little array
1478c2ecf20Sopenharmony_ci	 * so that they're not open-coded
1488c2ecf20Sopenharmony_ci	 */
1498c2ecf20Sopenharmony_ci	switch (mem->state) {
1508c2ecf20Sopenharmony_ci	case MEM_ONLINE:
1518c2ecf20Sopenharmony_ci		output = "online";
1528c2ecf20Sopenharmony_ci		break;
1538c2ecf20Sopenharmony_ci	case MEM_OFFLINE:
1548c2ecf20Sopenharmony_ci		output = "offline";
1558c2ecf20Sopenharmony_ci		break;
1568c2ecf20Sopenharmony_ci	case MEM_GOING_OFFLINE:
1578c2ecf20Sopenharmony_ci		output = "going-offline";
1588c2ecf20Sopenharmony_ci		break;
1598c2ecf20Sopenharmony_ci	default:
1608c2ecf20Sopenharmony_ci		WARN_ON(1);
1618c2ecf20Sopenharmony_ci		return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state);
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%s\n", output);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciint memory_notify(unsigned long val, void *v)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	return blocking_notifier_call_chain(&memory_chain, val, v);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/*
1738c2ecf20Sopenharmony_ci * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
1748c2ecf20Sopenharmony_ci * OK to have direct references to sparsemem variables in here.
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistatic int
1778c2ecf20Sopenharmony_cimemory_block_action(unsigned long start_section_nr, unsigned long action,
1788c2ecf20Sopenharmony_ci		    int online_type, int nid)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	unsigned long start_pfn;
1818c2ecf20Sopenharmony_ci	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
1828c2ecf20Sopenharmony_ci	int ret;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	start_pfn = section_nr_to_pfn(start_section_nr);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	switch (action) {
1878c2ecf20Sopenharmony_ci	case MEM_ONLINE:
1888c2ecf20Sopenharmony_ci		ret = online_pages(start_pfn, nr_pages, online_type, nid);
1898c2ecf20Sopenharmony_ci		break;
1908c2ecf20Sopenharmony_ci	case MEM_OFFLINE:
1918c2ecf20Sopenharmony_ci		ret = offline_pages(start_pfn, nr_pages);
1928c2ecf20Sopenharmony_ci		break;
1938c2ecf20Sopenharmony_ci	default:
1948c2ecf20Sopenharmony_ci		WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
1958c2ecf20Sopenharmony_ci		     "%ld\n", __func__, start_section_nr, action, action);
1968c2ecf20Sopenharmony_ci		ret = -EINVAL;
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	return ret;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic int memory_block_change_state(struct memory_block *mem,
2038c2ecf20Sopenharmony_ci		unsigned long to_state, unsigned long from_state_req)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	int ret = 0;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (mem->state != from_state_req)
2088c2ecf20Sopenharmony_ci		return -EINVAL;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (to_state == MEM_OFFLINE)
2118c2ecf20Sopenharmony_ci		mem->state = MEM_GOING_OFFLINE;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	ret = memory_block_action(mem->start_section_nr, to_state,
2148c2ecf20Sopenharmony_ci				  mem->online_type, mem->nid);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	mem->state = ret ? from_state_req : to_state;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	return ret;
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/* The device lock serializes operations on memory_subsys_[online|offline] */
2228c2ecf20Sopenharmony_cistatic int memory_subsys_online(struct device *dev)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
2258c2ecf20Sopenharmony_ci	int ret;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (mem->state == MEM_ONLINE)
2288c2ecf20Sopenharmony_ci		return 0;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/*
2318c2ecf20Sopenharmony_ci	 * When called via device_online() without configuring the online_type,
2328c2ecf20Sopenharmony_ci	 * we want to default to MMOP_ONLINE.
2338c2ecf20Sopenharmony_ci	 */
2348c2ecf20Sopenharmony_ci	if (mem->online_type == MMOP_OFFLINE)
2358c2ecf20Sopenharmony_ci		mem->online_type = MMOP_ONLINE;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
2388c2ecf20Sopenharmony_ci	mem->online_type = MMOP_OFFLINE;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	return ret;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic int memory_subsys_offline(struct device *dev)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (mem->state == MEM_OFFLINE)
2488c2ecf20Sopenharmony_ci		return 0;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic ssize_t state_store(struct device *dev, struct device_attribute *attr,
2548c2ecf20Sopenharmony_ci			   const char *buf, size_t count)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	const int online_type = memhp_online_type_from_str(buf);
2578c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
2588c2ecf20Sopenharmony_ci	int ret;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	if (online_type < 0)
2618c2ecf20Sopenharmony_ci		return -EINVAL;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	ret = lock_device_hotplug_sysfs();
2648c2ecf20Sopenharmony_ci	if (ret)
2658c2ecf20Sopenharmony_ci		return ret;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	switch (online_type) {
2688c2ecf20Sopenharmony_ci	case MMOP_ONLINE_KERNEL:
2698c2ecf20Sopenharmony_ci	case MMOP_ONLINE_MOVABLE:
2708c2ecf20Sopenharmony_ci	case MMOP_ONLINE:
2718c2ecf20Sopenharmony_ci		/* mem->online_type is protected by device_hotplug_lock */
2728c2ecf20Sopenharmony_ci		mem->online_type = online_type;
2738c2ecf20Sopenharmony_ci		ret = device_online(&mem->dev);
2748c2ecf20Sopenharmony_ci		break;
2758c2ecf20Sopenharmony_ci	case MMOP_OFFLINE:
2768c2ecf20Sopenharmony_ci		ret = device_offline(&mem->dev);
2778c2ecf20Sopenharmony_ci		break;
2788c2ecf20Sopenharmony_ci	default:
2798c2ecf20Sopenharmony_ci		ret = -EINVAL; /* should never happen */
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	unlock_device_hotplug();
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (ret < 0)
2858c2ecf20Sopenharmony_ci		return ret;
2868c2ecf20Sopenharmony_ci	if (ret)
2878c2ecf20Sopenharmony_ci		return -EINVAL;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return count;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/*
2938c2ecf20Sopenharmony_ci * Legacy interface that we cannot remove: s390x exposes the storage increment
2948c2ecf20Sopenharmony_ci * covered by a memory block, allowing for identifying which memory blocks
2958c2ecf20Sopenharmony_ci * comprise a storage increment. Since a memory block spans complete
2968c2ecf20Sopenharmony_ci * storage increments nowadays, this interface is basically unused. Other
2978c2ecf20Sopenharmony_ci * archs never exposed != 0.
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistatic ssize_t phys_device_show(struct device *dev,
3008c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
3038c2ecf20Sopenharmony_ci	unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%d\n",
3068c2ecf20Sopenharmony_ci			  arch_get_memory_phys_device(start_pfn));
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMORY_HOTREMOVE
3108c2ecf20Sopenharmony_cistatic int print_allowed_zone(char *buf, int len, int nid,
3118c2ecf20Sopenharmony_ci			      unsigned long start_pfn, unsigned long nr_pages,
3128c2ecf20Sopenharmony_ci			      int online_type, struct zone *default_zone)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	struct zone *zone;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
3178c2ecf20Sopenharmony_ci	if (zone == default_zone)
3188c2ecf20Sopenharmony_ci		return 0;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return sysfs_emit_at(buf, len, " %s", zone->name);
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic ssize_t valid_zones_show(struct device *dev,
3248c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
3278c2ecf20Sopenharmony_ci	unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
3288c2ecf20Sopenharmony_ci	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
3298c2ecf20Sopenharmony_ci	struct zone *default_zone;
3308c2ecf20Sopenharmony_ci	int len = 0;
3318c2ecf20Sopenharmony_ci	int nid;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/*
3348c2ecf20Sopenharmony_ci	 * Check the existing zone. Make sure that we do that only on the
3358c2ecf20Sopenharmony_ci	 * online nodes otherwise the page_zone is not reliable
3368c2ecf20Sopenharmony_ci	 */
3378c2ecf20Sopenharmony_ci	if (mem->state == MEM_ONLINE) {
3388c2ecf20Sopenharmony_ci		/*
3398c2ecf20Sopenharmony_ci		 * The block contains more than one zone can not be offlined.
3408c2ecf20Sopenharmony_ci		 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
3418c2ecf20Sopenharmony_ci		 */
3428c2ecf20Sopenharmony_ci		default_zone = test_pages_in_a_zone(start_pfn,
3438c2ecf20Sopenharmony_ci						    start_pfn + nr_pages);
3448c2ecf20Sopenharmony_ci		if (!default_zone)
3458c2ecf20Sopenharmony_ci			return sysfs_emit(buf, "%s\n", "none");
3468c2ecf20Sopenharmony_ci		len += sysfs_emit_at(buf, len, "%s", default_zone->name);
3478c2ecf20Sopenharmony_ci		goto out;
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	nid = mem->nid;
3518c2ecf20Sopenharmony_ci	default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, start_pfn,
3528c2ecf20Sopenharmony_ci					  nr_pages);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	len += sysfs_emit_at(buf, len, "%s", default_zone->name);
3558c2ecf20Sopenharmony_ci	len += print_allowed_zone(buf, len, nid, start_pfn, nr_pages,
3568c2ecf20Sopenharmony_ci				  MMOP_ONLINE_KERNEL, default_zone);
3578c2ecf20Sopenharmony_ci	len += print_allowed_zone(buf, len, nid, start_pfn, nr_pages,
3588c2ecf20Sopenharmony_ci				  MMOP_ONLINE_MOVABLE, default_zone);
3598c2ecf20Sopenharmony_ciout:
3608c2ecf20Sopenharmony_ci	len += sysfs_emit_at(buf, len, "\n");
3618c2ecf20Sopenharmony_ci	return len;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(valid_zones);
3648c2ecf20Sopenharmony_ci#endif
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(phys_index);
3678c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(state);
3688c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(phys_device);
3698c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(removable);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci/*
3728c2ecf20Sopenharmony_ci * Show the memory block size (shared by all memory blocks).
3738c2ecf20Sopenharmony_ci */
3748c2ecf20Sopenharmony_cistatic ssize_t block_size_bytes_show(struct device *dev,
3758c2ecf20Sopenharmony_ci				     struct device_attribute *attr, char *buf)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%lx\n", memory_block_size_bytes());
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(block_size_bytes);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/*
3838c2ecf20Sopenharmony_ci * Memory auto online policy.
3848c2ecf20Sopenharmony_ci */
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic ssize_t auto_online_blocks_show(struct device *dev,
3878c2ecf20Sopenharmony_ci				       struct device_attribute *attr, char *buf)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%s\n",
3908c2ecf20Sopenharmony_ci			  online_type_to_str[memhp_default_online_type]);
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic ssize_t auto_online_blocks_store(struct device *dev,
3948c2ecf20Sopenharmony_ci					struct device_attribute *attr,
3958c2ecf20Sopenharmony_ci					const char *buf, size_t count)
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	const int online_type = memhp_online_type_from_str(buf);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (online_type < 0)
4008c2ecf20Sopenharmony_ci		return -EINVAL;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	memhp_default_online_type = online_type;
4038c2ecf20Sopenharmony_ci	return count;
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(auto_online_blocks);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci/*
4098c2ecf20Sopenharmony_ci * Some architectures will have custom drivers to do this, and
4108c2ecf20Sopenharmony_ci * will not need to do it from userspace.  The fake hot-add code
4118c2ecf20Sopenharmony_ci * as well as ppc64 will do all of their discovery in userspace
4128c2ecf20Sopenharmony_ci * and will require this interface.
4138c2ecf20Sopenharmony_ci */
4148c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_MEMORY_PROBE
4158c2ecf20Sopenharmony_cistatic ssize_t probe_store(struct device *dev, struct device_attribute *attr,
4168c2ecf20Sopenharmony_ci			   const char *buf, size_t count)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	u64 phys_addr;
4198c2ecf20Sopenharmony_ci	int nid, ret;
4208c2ecf20Sopenharmony_ci	unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	ret = kstrtoull(buf, 0, &phys_addr);
4238c2ecf20Sopenharmony_ci	if (ret)
4248c2ecf20Sopenharmony_ci		return ret;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
4278c2ecf20Sopenharmony_ci		return -EINVAL;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	ret = lock_device_hotplug_sysfs();
4308c2ecf20Sopenharmony_ci	if (ret)
4318c2ecf20Sopenharmony_ci		return ret;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	nid = memory_add_physaddr_to_nid(phys_addr);
4348c2ecf20Sopenharmony_ci	ret = __add_memory(nid, phys_addr,
4358c2ecf20Sopenharmony_ci			   MIN_MEMORY_BLOCK_SIZE * sections_per_block,
4368c2ecf20Sopenharmony_ci			   MHP_NONE);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (ret)
4398c2ecf20Sopenharmony_ci		goto out;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	ret = count;
4428c2ecf20Sopenharmony_ciout:
4438c2ecf20Sopenharmony_ci	unlock_device_hotplug();
4448c2ecf20Sopenharmony_ci	return ret;
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(probe);
4488c2ecf20Sopenharmony_ci#endif
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMORY_FAILURE
4518c2ecf20Sopenharmony_ci/*
4528c2ecf20Sopenharmony_ci * Support for offlining pages of memory
4538c2ecf20Sopenharmony_ci */
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci/* Soft offline a page */
4568c2ecf20Sopenharmony_cistatic ssize_t soft_offline_page_store(struct device *dev,
4578c2ecf20Sopenharmony_ci				       struct device_attribute *attr,
4588c2ecf20Sopenharmony_ci				       const char *buf, size_t count)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	int ret;
4618c2ecf20Sopenharmony_ci	u64 pfn;
4628c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN))
4638c2ecf20Sopenharmony_ci		return -EPERM;
4648c2ecf20Sopenharmony_ci	if (kstrtoull(buf, 0, &pfn) < 0)
4658c2ecf20Sopenharmony_ci		return -EINVAL;
4668c2ecf20Sopenharmony_ci	pfn >>= PAGE_SHIFT;
4678c2ecf20Sopenharmony_ci	ret = soft_offline_page(pfn, 0);
4688c2ecf20Sopenharmony_ci	return ret == 0 ? count : ret;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci/* Forcibly offline a page, including killing processes. */
4728c2ecf20Sopenharmony_cistatic ssize_t hard_offline_page_store(struct device *dev,
4738c2ecf20Sopenharmony_ci				       struct device_attribute *attr,
4748c2ecf20Sopenharmony_ci				       const char *buf, size_t count)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	int ret;
4778c2ecf20Sopenharmony_ci	u64 pfn;
4788c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN))
4798c2ecf20Sopenharmony_ci		return -EPERM;
4808c2ecf20Sopenharmony_ci	if (kstrtoull(buf, 0, &pfn) < 0)
4818c2ecf20Sopenharmony_ci		return -EINVAL;
4828c2ecf20Sopenharmony_ci	pfn >>= PAGE_SHIFT;
4838c2ecf20Sopenharmony_ci	ret = memory_failure(pfn, 0);
4848c2ecf20Sopenharmony_ci	return ret ? ret : count;
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(soft_offline_page);
4888c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(hard_offline_page);
4898c2ecf20Sopenharmony_ci#endif
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci/* See phys_device_show(). */
4928c2ecf20Sopenharmony_ciint __weak arch_get_memory_phys_device(unsigned long start_pfn)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	return 0;
4958c2ecf20Sopenharmony_ci}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci/*
4988c2ecf20Sopenharmony_ci * A reference for the returned memory block device is acquired.
4998c2ecf20Sopenharmony_ci *
5008c2ecf20Sopenharmony_ci * Called under device_hotplug_lock.
5018c2ecf20Sopenharmony_ci */
5028c2ecf20Sopenharmony_cistatic struct memory_block *find_memory_block_by_id(unsigned long block_id)
5038c2ecf20Sopenharmony_ci{
5048c2ecf20Sopenharmony_ci	struct memory_block *mem;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	mem = xa_load(&memory_blocks, block_id);
5078c2ecf20Sopenharmony_ci	if (mem)
5088c2ecf20Sopenharmony_ci		get_device(&mem->dev);
5098c2ecf20Sopenharmony_ci	return mem;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci/*
5138c2ecf20Sopenharmony_ci * Called under device_hotplug_lock.
5148c2ecf20Sopenharmony_ci */
5158c2ecf20Sopenharmony_cistruct memory_block *find_memory_block(struct mem_section *section)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	unsigned long block_id = memory_block_id(__section_nr(section));
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	return find_memory_block_by_id(block_id);
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic struct attribute *memory_memblk_attrs[] = {
5238c2ecf20Sopenharmony_ci	&dev_attr_phys_index.attr,
5248c2ecf20Sopenharmony_ci	&dev_attr_state.attr,
5258c2ecf20Sopenharmony_ci	&dev_attr_phys_device.attr,
5268c2ecf20Sopenharmony_ci	&dev_attr_removable.attr,
5278c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMORY_HOTREMOVE
5288c2ecf20Sopenharmony_ci	&dev_attr_valid_zones.attr,
5298c2ecf20Sopenharmony_ci#endif
5308c2ecf20Sopenharmony_ci	NULL
5318c2ecf20Sopenharmony_ci};
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic struct attribute_group memory_memblk_attr_group = {
5348c2ecf20Sopenharmony_ci	.attrs = memory_memblk_attrs,
5358c2ecf20Sopenharmony_ci};
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic const struct attribute_group *memory_memblk_attr_groups[] = {
5388c2ecf20Sopenharmony_ci	&memory_memblk_attr_group,
5398c2ecf20Sopenharmony_ci	NULL,
5408c2ecf20Sopenharmony_ci};
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci/*
5438c2ecf20Sopenharmony_ci * register_memory - Setup a sysfs device for a memory block
5448c2ecf20Sopenharmony_ci */
5458c2ecf20Sopenharmony_cistatic
5468c2ecf20Sopenharmony_ciint register_memory(struct memory_block *memory)
5478c2ecf20Sopenharmony_ci{
5488c2ecf20Sopenharmony_ci	int ret;
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	memory->dev.bus = &memory_subsys;
5518c2ecf20Sopenharmony_ci	memory->dev.id = memory->start_section_nr / sections_per_block;
5528c2ecf20Sopenharmony_ci	memory->dev.release = memory_block_release;
5538c2ecf20Sopenharmony_ci	memory->dev.groups = memory_memblk_attr_groups;
5548c2ecf20Sopenharmony_ci	memory->dev.offline = memory->state == MEM_OFFLINE;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	ret = device_register(&memory->dev);
5578c2ecf20Sopenharmony_ci	if (ret) {
5588c2ecf20Sopenharmony_ci		put_device(&memory->dev);
5598c2ecf20Sopenharmony_ci		return ret;
5608c2ecf20Sopenharmony_ci	}
5618c2ecf20Sopenharmony_ci	ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
5628c2ecf20Sopenharmony_ci			      GFP_KERNEL));
5638c2ecf20Sopenharmony_ci	if (ret)
5648c2ecf20Sopenharmony_ci		device_unregister(&memory->dev);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	return ret;
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic int init_memory_block(unsigned long block_id, unsigned long state)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	struct memory_block *mem;
5728c2ecf20Sopenharmony_ci	int ret = 0;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	mem = find_memory_block_by_id(block_id);
5758c2ecf20Sopenharmony_ci	if (mem) {
5768c2ecf20Sopenharmony_ci		put_device(&mem->dev);
5778c2ecf20Sopenharmony_ci		return -EEXIST;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
5808c2ecf20Sopenharmony_ci	if (!mem)
5818c2ecf20Sopenharmony_ci		return -ENOMEM;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	mem->start_section_nr = block_id * sections_per_block;
5848c2ecf20Sopenharmony_ci	mem->state = state;
5858c2ecf20Sopenharmony_ci	mem->nid = NUMA_NO_NODE;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	ret = register_memory(mem);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	return ret;
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic int add_memory_block(unsigned long base_section_nr)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	int section_count = 0;
5958c2ecf20Sopenharmony_ci	unsigned long nr;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
5988c2ecf20Sopenharmony_ci	     nr++)
5998c2ecf20Sopenharmony_ci		if (present_section_nr(nr))
6008c2ecf20Sopenharmony_ci			section_count++;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	if (section_count == 0)
6038c2ecf20Sopenharmony_ci		return 0;
6048c2ecf20Sopenharmony_ci	return init_memory_block(memory_block_id(base_section_nr),
6058c2ecf20Sopenharmony_ci				 MEM_ONLINE);
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic void unregister_memory(struct memory_block *memory)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
6118c2ecf20Sopenharmony_ci		return;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	/* drop the ref. we got via find_memory_block() */
6168c2ecf20Sopenharmony_ci	put_device(&memory->dev);
6178c2ecf20Sopenharmony_ci	device_unregister(&memory->dev);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci/*
6218c2ecf20Sopenharmony_ci * Create memory block devices for the given memory area. Start and size
6228c2ecf20Sopenharmony_ci * have to be aligned to memory block granularity. Memory block devices
6238c2ecf20Sopenharmony_ci * will be initialized as offline.
6248c2ecf20Sopenharmony_ci *
6258c2ecf20Sopenharmony_ci * Called under device_hotplug_lock.
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_ciint create_memory_block_devices(unsigned long start, unsigned long size)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
6308c2ecf20Sopenharmony_ci	unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
6318c2ecf20Sopenharmony_ci	struct memory_block *mem;
6328c2ecf20Sopenharmony_ci	unsigned long block_id;
6338c2ecf20Sopenharmony_ci	int ret = 0;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
6368c2ecf20Sopenharmony_ci			 !IS_ALIGNED(size, memory_block_size_bytes())))
6378c2ecf20Sopenharmony_ci		return -EINVAL;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	for (block_id = start_block_id; block_id != end_block_id; block_id++) {
6408c2ecf20Sopenharmony_ci		ret = init_memory_block(block_id, MEM_OFFLINE);
6418c2ecf20Sopenharmony_ci		if (ret)
6428c2ecf20Sopenharmony_ci			break;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci	if (ret) {
6458c2ecf20Sopenharmony_ci		end_block_id = block_id;
6468c2ecf20Sopenharmony_ci		for (block_id = start_block_id; block_id != end_block_id;
6478c2ecf20Sopenharmony_ci		     block_id++) {
6488c2ecf20Sopenharmony_ci			mem = find_memory_block_by_id(block_id);
6498c2ecf20Sopenharmony_ci			if (WARN_ON_ONCE(!mem))
6508c2ecf20Sopenharmony_ci				continue;
6518c2ecf20Sopenharmony_ci			unregister_memory(mem);
6528c2ecf20Sopenharmony_ci		}
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci	return ret;
6558c2ecf20Sopenharmony_ci}
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci/*
6588c2ecf20Sopenharmony_ci * Remove memory block devices for the given memory area. Start and size
6598c2ecf20Sopenharmony_ci * have to be aligned to memory block granularity. Memory block devices
6608c2ecf20Sopenharmony_ci * have to be offline.
6618c2ecf20Sopenharmony_ci *
6628c2ecf20Sopenharmony_ci * Called under device_hotplug_lock.
6638c2ecf20Sopenharmony_ci */
6648c2ecf20Sopenharmony_civoid remove_memory_block_devices(unsigned long start, unsigned long size)
6658c2ecf20Sopenharmony_ci{
6668c2ecf20Sopenharmony_ci	const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
6678c2ecf20Sopenharmony_ci	const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
6688c2ecf20Sopenharmony_ci	struct memory_block *mem;
6698c2ecf20Sopenharmony_ci	unsigned long block_id;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
6728c2ecf20Sopenharmony_ci			 !IS_ALIGNED(size, memory_block_size_bytes())))
6738c2ecf20Sopenharmony_ci		return;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	for (block_id = start_block_id; block_id != end_block_id; block_id++) {
6768c2ecf20Sopenharmony_ci		mem = find_memory_block_by_id(block_id);
6778c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(!mem))
6788c2ecf20Sopenharmony_ci			continue;
6798c2ecf20Sopenharmony_ci		unregister_memory_block_under_nodes(mem);
6808c2ecf20Sopenharmony_ci		unregister_memory(mem);
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci/* return true if the memory block is offlined, otherwise, return false */
6858c2ecf20Sopenharmony_cibool is_memblock_offlined(struct memory_block *mem)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	return mem->state == MEM_OFFLINE;
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_cistatic struct attribute *memory_root_attrs[] = {
6918c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_MEMORY_PROBE
6928c2ecf20Sopenharmony_ci	&dev_attr_probe.attr,
6938c2ecf20Sopenharmony_ci#endif
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMORY_FAILURE
6968c2ecf20Sopenharmony_ci	&dev_attr_soft_offline_page.attr,
6978c2ecf20Sopenharmony_ci	&dev_attr_hard_offline_page.attr,
6988c2ecf20Sopenharmony_ci#endif
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	&dev_attr_block_size_bytes.attr,
7018c2ecf20Sopenharmony_ci	&dev_attr_auto_online_blocks.attr,
7028c2ecf20Sopenharmony_ci	NULL
7038c2ecf20Sopenharmony_ci};
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic struct attribute_group memory_root_attr_group = {
7068c2ecf20Sopenharmony_ci	.attrs = memory_root_attrs,
7078c2ecf20Sopenharmony_ci};
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic const struct attribute_group *memory_root_attr_groups[] = {
7108c2ecf20Sopenharmony_ci	&memory_root_attr_group,
7118c2ecf20Sopenharmony_ci	NULL,
7128c2ecf20Sopenharmony_ci};
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci/*
7158c2ecf20Sopenharmony_ci * Initialize the sysfs support for memory devices. At the time this function
7168c2ecf20Sopenharmony_ci * is called, we cannot have concurrent creation/deletion of memory block
7178c2ecf20Sopenharmony_ci * devices, the device_hotplug_lock is not needed.
7188c2ecf20Sopenharmony_ci */
7198c2ecf20Sopenharmony_civoid __init memory_dev_init(void)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	int ret;
7228c2ecf20Sopenharmony_ci	unsigned long block_sz, nr;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	/* Validate the configured memory block size */
7258c2ecf20Sopenharmony_ci	block_sz = memory_block_size_bytes();
7268c2ecf20Sopenharmony_ci	if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
7278c2ecf20Sopenharmony_ci		panic("Memory block size not suitable: 0x%lx\n", block_sz);
7288c2ecf20Sopenharmony_ci	sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
7318c2ecf20Sopenharmony_ci	if (ret)
7328c2ecf20Sopenharmony_ci		panic("%s() failed to register subsystem: %d\n", __func__, ret);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	/*
7358c2ecf20Sopenharmony_ci	 * Create entries for memory sections that were found
7368c2ecf20Sopenharmony_ci	 * during boot and have been initialized
7378c2ecf20Sopenharmony_ci	 */
7388c2ecf20Sopenharmony_ci	for (nr = 0; nr <= __highest_present_section_nr;
7398c2ecf20Sopenharmony_ci	     nr += sections_per_block) {
7408c2ecf20Sopenharmony_ci		ret = add_memory_block(nr);
7418c2ecf20Sopenharmony_ci		if (ret)
7428c2ecf20Sopenharmony_ci			panic("%s() failed to add memory block: %d\n", __func__,
7438c2ecf20Sopenharmony_ci			      ret);
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci/**
7488c2ecf20Sopenharmony_ci * walk_memory_blocks - walk through all present memory blocks overlapped
7498c2ecf20Sopenharmony_ci *			by the range [start, start + size)
7508c2ecf20Sopenharmony_ci *
7518c2ecf20Sopenharmony_ci * @start: start address of the memory range
7528c2ecf20Sopenharmony_ci * @size: size of the memory range
7538c2ecf20Sopenharmony_ci * @arg: argument passed to func
7548c2ecf20Sopenharmony_ci * @func: callback for each memory section walked
7558c2ecf20Sopenharmony_ci *
7568c2ecf20Sopenharmony_ci * This function walks through all present memory blocks overlapped by the
7578c2ecf20Sopenharmony_ci * range [start, start + size), calling func on each memory block.
7588c2ecf20Sopenharmony_ci *
7598c2ecf20Sopenharmony_ci * In case func() returns an error, walking is aborted and the error is
7608c2ecf20Sopenharmony_ci * returned.
7618c2ecf20Sopenharmony_ci *
7628c2ecf20Sopenharmony_ci * Called under device_hotplug_lock.
7638c2ecf20Sopenharmony_ci */
7648c2ecf20Sopenharmony_ciint walk_memory_blocks(unsigned long start, unsigned long size,
7658c2ecf20Sopenharmony_ci		       void *arg, walk_memory_blocks_func_t func)
7668c2ecf20Sopenharmony_ci{
7678c2ecf20Sopenharmony_ci	const unsigned long start_block_id = phys_to_block_id(start);
7688c2ecf20Sopenharmony_ci	const unsigned long end_block_id = phys_to_block_id(start + size - 1);
7698c2ecf20Sopenharmony_ci	struct memory_block *mem;
7708c2ecf20Sopenharmony_ci	unsigned long block_id;
7718c2ecf20Sopenharmony_ci	int ret = 0;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	if (!size)
7748c2ecf20Sopenharmony_ci		return 0;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
7778c2ecf20Sopenharmony_ci		mem = find_memory_block_by_id(block_id);
7788c2ecf20Sopenharmony_ci		if (!mem)
7798c2ecf20Sopenharmony_ci			continue;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci		ret = func(mem, arg);
7828c2ecf20Sopenharmony_ci		put_device(&mem->dev);
7838c2ecf20Sopenharmony_ci		if (ret)
7848c2ecf20Sopenharmony_ci			break;
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci	return ret;
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_cistruct for_each_memory_block_cb_data {
7908c2ecf20Sopenharmony_ci	walk_memory_blocks_func_t func;
7918c2ecf20Sopenharmony_ci	void *arg;
7928c2ecf20Sopenharmony_ci};
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_cistatic int for_each_memory_block_cb(struct device *dev, void *data)
7958c2ecf20Sopenharmony_ci{
7968c2ecf20Sopenharmony_ci	struct memory_block *mem = to_memory_block(dev);
7978c2ecf20Sopenharmony_ci	struct for_each_memory_block_cb_data *cb_data = data;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	return cb_data->func(mem, cb_data->arg);
8008c2ecf20Sopenharmony_ci}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci/**
8038c2ecf20Sopenharmony_ci * for_each_memory_block - walk through all present memory blocks
8048c2ecf20Sopenharmony_ci *
8058c2ecf20Sopenharmony_ci * @arg: argument passed to func
8068c2ecf20Sopenharmony_ci * @func: callback for each memory block walked
8078c2ecf20Sopenharmony_ci *
8088c2ecf20Sopenharmony_ci * This function walks through all present memory blocks, calling func on
8098c2ecf20Sopenharmony_ci * each memory block.
8108c2ecf20Sopenharmony_ci *
8118c2ecf20Sopenharmony_ci * In case func() returns an error, walking is aborted and the error is
8128c2ecf20Sopenharmony_ci * returned.
8138c2ecf20Sopenharmony_ci */
8148c2ecf20Sopenharmony_ciint for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
8158c2ecf20Sopenharmony_ci{
8168c2ecf20Sopenharmony_ci	struct for_each_memory_block_cb_data cb_data = {
8178c2ecf20Sopenharmony_ci		.func = func,
8188c2ecf20Sopenharmony_ci		.arg = arg,
8198c2ecf20Sopenharmony_ci	};
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
8228c2ecf20Sopenharmony_ci				for_each_memory_block_cb);
8238c2ecf20Sopenharmony_ci}
824