xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpi_memhotplug.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2004, 2013 Intel Corporation
4*4882a593Smuzhiyun  * Author: Naveen B S <naveen.b.s@intel.com>
5*4882a593Smuzhiyun  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * All rights reserved.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * ACPI based HotPlug driver that supports Memory Hotplug
10*4882a593Smuzhiyun  * This driver fields notifications from firmware for memory add
11*4882a593Smuzhiyun  * and remove operations and alerts the VM of the affected memory
12*4882a593Smuzhiyun  * ranges.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/acpi.h>
16*4882a593Smuzhiyun #include <linux/memory.h>
17*4882a593Smuzhiyun #include <linux/memory_hotplug.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "internal.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define ACPI_MEMORY_DEVICE_CLASS		"memory"
22*4882a593Smuzhiyun #define ACPI_MEMORY_DEVICE_HID			"PNP0C80"
23*4882a593Smuzhiyun #define ACPI_MEMORY_DEVICE_NAME			"Hotplug Mem Device"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun static const struct acpi_device_id memory_device_ids[] = {
26*4882a593Smuzhiyun 	{ACPI_MEMORY_DEVICE_HID, 0},
27*4882a593Smuzhiyun 	{"", 0},
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #ifdef CONFIG_ACPI_HOTPLUG_MEMORY
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun static int acpi_memory_device_add(struct acpi_device *device,
33*4882a593Smuzhiyun 				  const struct acpi_device_id *not_used);
34*4882a593Smuzhiyun static void acpi_memory_device_remove(struct acpi_device *device);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun static struct acpi_scan_handler memory_device_handler = {
37*4882a593Smuzhiyun 	.ids = memory_device_ids,
38*4882a593Smuzhiyun 	.attach = acpi_memory_device_add,
39*4882a593Smuzhiyun 	.detach = acpi_memory_device_remove,
40*4882a593Smuzhiyun 	.hotplug = {
41*4882a593Smuzhiyun 		.enabled = true,
42*4882a593Smuzhiyun 	},
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun struct acpi_memory_info {
46*4882a593Smuzhiyun 	struct list_head list;
47*4882a593Smuzhiyun 	u64 start_addr;		/* Memory Range start physical addr */
48*4882a593Smuzhiyun 	u64 length;		/* Memory Range length */
49*4882a593Smuzhiyun 	unsigned short caching;	/* memory cache attribute */
50*4882a593Smuzhiyun 	unsigned short write_protect;	/* memory read/write attribute */
51*4882a593Smuzhiyun 	unsigned int enabled:1;
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun struct acpi_memory_device {
55*4882a593Smuzhiyun 	struct acpi_device *device;
56*4882a593Smuzhiyun 	struct list_head res_list;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun static acpi_status
acpi_memory_get_resource(struct acpi_resource * resource,void * context)60*4882a593Smuzhiyun acpi_memory_get_resource(struct acpi_resource *resource, void *context)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct acpi_memory_device *mem_device = context;
63*4882a593Smuzhiyun 	struct acpi_resource_address64 address64;
64*4882a593Smuzhiyun 	struct acpi_memory_info *info, *new;
65*4882a593Smuzhiyun 	acpi_status status;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	status = acpi_resource_to_address64(resource, &address64);
68*4882a593Smuzhiyun 	if (ACPI_FAILURE(status) ||
69*4882a593Smuzhiyun 	    (address64.resource_type != ACPI_MEMORY_RANGE))
70*4882a593Smuzhiyun 		return AE_OK;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	list_for_each_entry(info, &mem_device->res_list, list) {
73*4882a593Smuzhiyun 		/* Can we combine the resource range information? */
74*4882a593Smuzhiyun 		if ((info->caching == address64.info.mem.caching) &&
75*4882a593Smuzhiyun 		    (info->write_protect == address64.info.mem.write_protect) &&
76*4882a593Smuzhiyun 		    (info->start_addr + info->length == address64.address.minimum)) {
77*4882a593Smuzhiyun 			info->length += address64.address.address_length;
78*4882a593Smuzhiyun 			return AE_OK;
79*4882a593Smuzhiyun 		}
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
83*4882a593Smuzhiyun 	if (!new)
84*4882a593Smuzhiyun 		return AE_ERROR;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	INIT_LIST_HEAD(&new->list);
87*4882a593Smuzhiyun 	new->caching = address64.info.mem.caching;
88*4882a593Smuzhiyun 	new->write_protect = address64.info.mem.write_protect;
89*4882a593Smuzhiyun 	new->start_addr = address64.address.minimum;
90*4882a593Smuzhiyun 	new->length = address64.address.address_length;
91*4882a593Smuzhiyun 	list_add_tail(&new->list, &mem_device->res_list);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return AE_OK;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun static void
acpi_memory_free_device_resources(struct acpi_memory_device * mem_device)97*4882a593Smuzhiyun acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct acpi_memory_info *info, *n;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	list_for_each_entry_safe(info, n, &mem_device->res_list, list)
102*4882a593Smuzhiyun 		kfree(info);
103*4882a593Smuzhiyun 	INIT_LIST_HEAD(&mem_device->res_list);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun static int
acpi_memory_get_device_resources(struct acpi_memory_device * mem_device)107*4882a593Smuzhiyun acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	acpi_status status;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (!list_empty(&mem_device->res_list))
112*4882a593Smuzhiyun 		return 0;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
115*4882a593Smuzhiyun 				     acpi_memory_get_resource, mem_device);
116*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
117*4882a593Smuzhiyun 		acpi_memory_free_device_resources(mem_device);
118*4882a593Smuzhiyun 		return -EINVAL;
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return 0;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
acpi_memory_check_device(struct acpi_memory_device * mem_device)124*4882a593Smuzhiyun static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	unsigned long long current_status;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* Get device present/absent information from the _STA */
129*4882a593Smuzhiyun 	if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
130*4882a593Smuzhiyun 					       METHOD_NAME__STA, NULL,
131*4882a593Smuzhiyun 					       &current_status)))
132*4882a593Smuzhiyun 		return -ENODEV;
133*4882a593Smuzhiyun 	/*
134*4882a593Smuzhiyun 	 * Check for device status. Device should be
135*4882a593Smuzhiyun 	 * present/enabled/functioning.
136*4882a593Smuzhiyun 	 */
137*4882a593Smuzhiyun 	if (!((current_status & ACPI_STA_DEVICE_PRESENT)
138*4882a593Smuzhiyun 	      && (current_status & ACPI_STA_DEVICE_ENABLED)
139*4882a593Smuzhiyun 	      && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
140*4882a593Smuzhiyun 		return -ENODEV;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return 0;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
acpi_bind_memblk(struct memory_block * mem,void * arg)145*4882a593Smuzhiyun static int acpi_bind_memblk(struct memory_block *mem, void *arg)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	return acpi_bind_one(&mem->dev, arg);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
acpi_bind_memory_blocks(struct acpi_memory_info * info,struct acpi_device * adev)150*4882a593Smuzhiyun static int acpi_bind_memory_blocks(struct acpi_memory_info *info,
151*4882a593Smuzhiyun 				   struct acpi_device *adev)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	return walk_memory_blocks(info->start_addr, info->length, adev,
154*4882a593Smuzhiyun 				  acpi_bind_memblk);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
acpi_unbind_memblk(struct memory_block * mem,void * arg)157*4882a593Smuzhiyun static int acpi_unbind_memblk(struct memory_block *mem, void *arg)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	acpi_unbind_one(&mem->dev);
160*4882a593Smuzhiyun 	return 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
acpi_unbind_memory_blocks(struct acpi_memory_info * info)163*4882a593Smuzhiyun static void acpi_unbind_memory_blocks(struct acpi_memory_info *info)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	walk_memory_blocks(info->start_addr, info->length, NULL,
166*4882a593Smuzhiyun 			   acpi_unbind_memblk);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
acpi_memory_enable_device(struct acpi_memory_device * mem_device)169*4882a593Smuzhiyun static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	acpi_handle handle = mem_device->device->handle;
172*4882a593Smuzhiyun 	int result, num_enabled = 0;
173*4882a593Smuzhiyun 	struct acpi_memory_info *info;
174*4882a593Smuzhiyun 	int node;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	node = acpi_get_node(handle);
177*4882a593Smuzhiyun 	/*
178*4882a593Smuzhiyun 	 * Tell the VM there is more memory here...
179*4882a593Smuzhiyun 	 * Note: Assume that this function returns zero on success
180*4882a593Smuzhiyun 	 * We don't have memory-hot-add rollback function,now.
181*4882a593Smuzhiyun 	 * (i.e. memory-hot-remove function)
182*4882a593Smuzhiyun 	 */
183*4882a593Smuzhiyun 	list_for_each_entry(info, &mem_device->res_list, list) {
184*4882a593Smuzhiyun 		if (info->enabled) { /* just sanity check...*/
185*4882a593Smuzhiyun 			num_enabled++;
186*4882a593Smuzhiyun 			continue;
187*4882a593Smuzhiyun 		}
188*4882a593Smuzhiyun 		/*
189*4882a593Smuzhiyun 		 * If the memory block size is zero, please ignore it.
190*4882a593Smuzhiyun 		 * Don't try to do the following memory hotplug flowchart.
191*4882a593Smuzhiyun 		 */
192*4882a593Smuzhiyun 		if (!info->length)
193*4882a593Smuzhiyun 			continue;
194*4882a593Smuzhiyun 		if (node < 0)
195*4882a593Smuzhiyun 			node = memory_add_physaddr_to_nid(info->start_addr);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 		result = __add_memory(node, info->start_addr, info->length,
198*4882a593Smuzhiyun 				      MHP_NONE);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		/*
201*4882a593Smuzhiyun 		 * If the memory block has been used by the kernel, add_memory()
202*4882a593Smuzhiyun 		 * returns -EEXIST. If add_memory() returns the other error, it
203*4882a593Smuzhiyun 		 * means that this memory block is not used by the kernel.
204*4882a593Smuzhiyun 		 */
205*4882a593Smuzhiyun 		if (result && result != -EEXIST)
206*4882a593Smuzhiyun 			continue;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 		result = acpi_bind_memory_blocks(info, mem_device->device);
209*4882a593Smuzhiyun 		if (result) {
210*4882a593Smuzhiyun 			acpi_unbind_memory_blocks(info);
211*4882a593Smuzhiyun 			return -ENODEV;
212*4882a593Smuzhiyun 		}
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 		info->enabled = 1;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 		/*
217*4882a593Smuzhiyun 		 * Add num_enable even if add_memory() returns -EEXIST, so the
218*4882a593Smuzhiyun 		 * device is bound to this driver.
219*4882a593Smuzhiyun 		 */
220*4882a593Smuzhiyun 		num_enabled++;
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 	if (!num_enabled) {
223*4882a593Smuzhiyun 		dev_err(&mem_device->device->dev, "add_memory failed\n");
224*4882a593Smuzhiyun 		return -EINVAL;
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun 	/*
227*4882a593Smuzhiyun 	 * Sometimes the memory device will contain several memory blocks.
228*4882a593Smuzhiyun 	 * When one memory block is hot-added to the system memory, it will
229*4882a593Smuzhiyun 	 * be regarded as a success.
230*4882a593Smuzhiyun 	 * Otherwise if the last memory block can't be hot-added to the system
231*4882a593Smuzhiyun 	 * memory, it will be failure and the memory device can't be bound with
232*4882a593Smuzhiyun 	 * driver.
233*4882a593Smuzhiyun 	 */
234*4882a593Smuzhiyun 	return 0;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
acpi_memory_remove_memory(struct acpi_memory_device * mem_device)237*4882a593Smuzhiyun static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	acpi_handle handle = mem_device->device->handle;
240*4882a593Smuzhiyun 	struct acpi_memory_info *info, *n;
241*4882a593Smuzhiyun 	int nid = acpi_get_node(handle);
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
244*4882a593Smuzhiyun 		if (!info->enabled)
245*4882a593Smuzhiyun 			continue;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		if (nid == NUMA_NO_NODE)
248*4882a593Smuzhiyun 			nid = memory_add_physaddr_to_nid(info->start_addr);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 		acpi_unbind_memory_blocks(info);
251*4882a593Smuzhiyun 		__remove_memory(nid, info->start_addr, info->length);
252*4882a593Smuzhiyun 		list_del(&info->list);
253*4882a593Smuzhiyun 		kfree(info);
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
acpi_memory_device_free(struct acpi_memory_device * mem_device)257*4882a593Smuzhiyun static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	if (!mem_device)
260*4882a593Smuzhiyun 		return;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	acpi_memory_free_device_resources(mem_device);
263*4882a593Smuzhiyun 	mem_device->device->driver_data = NULL;
264*4882a593Smuzhiyun 	kfree(mem_device);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun 
acpi_memory_device_add(struct acpi_device * device,const struct acpi_device_id * not_used)267*4882a593Smuzhiyun static int acpi_memory_device_add(struct acpi_device *device,
268*4882a593Smuzhiyun 				  const struct acpi_device_id *not_used)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	struct acpi_memory_device *mem_device;
271*4882a593Smuzhiyun 	int result;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	if (!device)
274*4882a593Smuzhiyun 		return -EINVAL;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
277*4882a593Smuzhiyun 	if (!mem_device)
278*4882a593Smuzhiyun 		return -ENOMEM;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	INIT_LIST_HEAD(&mem_device->res_list);
281*4882a593Smuzhiyun 	mem_device->device = device;
282*4882a593Smuzhiyun 	sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
283*4882a593Smuzhiyun 	sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
284*4882a593Smuzhiyun 	device->driver_data = mem_device;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	/* Get the range from the _CRS */
287*4882a593Smuzhiyun 	result = acpi_memory_get_device_resources(mem_device);
288*4882a593Smuzhiyun 	if (result) {
289*4882a593Smuzhiyun 		device->driver_data = NULL;
290*4882a593Smuzhiyun 		kfree(mem_device);
291*4882a593Smuzhiyun 		return result;
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	result = acpi_memory_check_device(mem_device);
295*4882a593Smuzhiyun 	if (result) {
296*4882a593Smuzhiyun 		acpi_memory_device_free(mem_device);
297*4882a593Smuzhiyun 		return 0;
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	result = acpi_memory_enable_device(mem_device);
301*4882a593Smuzhiyun 	if (result) {
302*4882a593Smuzhiyun 		dev_err(&device->dev, "acpi_memory_enable_device() error\n");
303*4882a593Smuzhiyun 		acpi_memory_device_free(mem_device);
304*4882a593Smuzhiyun 		return result;
305*4882a593Smuzhiyun 	}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	dev_dbg(&device->dev, "Memory device configured by ACPI\n");
308*4882a593Smuzhiyun 	return 1;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
acpi_memory_device_remove(struct acpi_device * device)311*4882a593Smuzhiyun static void acpi_memory_device_remove(struct acpi_device *device)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	struct acpi_memory_device *mem_device;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	if (!device || !acpi_driver_data(device))
316*4882a593Smuzhiyun 		return;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	mem_device = acpi_driver_data(device);
319*4882a593Smuzhiyun 	acpi_memory_remove_memory(mem_device);
320*4882a593Smuzhiyun 	acpi_memory_device_free(mem_device);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun static bool __initdata acpi_no_memhotplug;
324*4882a593Smuzhiyun 
acpi_memory_hotplug_init(void)325*4882a593Smuzhiyun void __init acpi_memory_hotplug_init(void)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	if (acpi_no_memhotplug) {
328*4882a593Smuzhiyun 		memory_device_handler.attach = NULL;
329*4882a593Smuzhiyun 		acpi_scan_add_handler(&memory_device_handler);
330*4882a593Smuzhiyun 		return;
331*4882a593Smuzhiyun 	}
332*4882a593Smuzhiyun 	acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
disable_acpi_memory_hotplug(char * str)335*4882a593Smuzhiyun static int __init disable_acpi_memory_hotplug(char *str)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	acpi_no_memhotplug = true;
338*4882a593Smuzhiyun 	return 1;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun __setup("acpi_no_memhotplug", disable_acpi_memory_hotplug);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun #else
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun static struct acpi_scan_handler memory_device_handler = {
345*4882a593Smuzhiyun 	.ids = memory_device_ids,
346*4882a593Smuzhiyun };
347*4882a593Smuzhiyun 
acpi_memory_hotplug_init(void)348*4882a593Smuzhiyun void __init acpi_memory_hotplug_init(void)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	acpi_scan_add_handler(&memory_device_handler);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun #endif /* CONFIG_ACPI_HOTPLUG_MEMORY */
354