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