blob: 24f662d8bd39a68ce27b9b5e9493f945ebc06b34 [file] [log] [blame]
Thomas Gleixner43593752019-05-19 15:51:42 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01003 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * All rights reserved.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * ACPI based HotPlug driver that supports Memory Hotplug
Nick Andrewc7060d92009-01-03 18:53:39 +110010 * This driver fields notifications from firmware for memory add
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * and remove operations and alerts the VM of the affected memory
12 * ranges.
13 */
14
Toshi Kaniab6c5702012-11-20 23:42:28 +000015#include <linux/acpi.h>
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +020016#include <linux/memory.h>
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010017#include <linux/memory_hotplug.h>
18
19#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define ACPI_MEMORY_DEVICE_CLASS "memory"
22#define ACPI_MEMORY_DEVICE_HID "PNP0C80"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
24
Rafael J. Wysockicccd4202014-05-30 04:29:14 +020025static const struct acpi_device_id memory_device_ids[] = {
26 {ACPI_MEMORY_DEVICE_HID, 0},
27 {"", 0},
28};
29
30#ifdef CONFIG_ACPI_HOTPLUG_MEMORY
31
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010032static int acpi_memory_device_add(struct acpi_device *device,
33 const struct acpi_device_id *not_used);
34static void acpi_memory_device_remove(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010036static struct acpi_scan_handler memory_device_handler = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020037 .ids = memory_device_ids,
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010038 .attach = acpi_memory_device_add,
39 .detach = acpi_memory_device_remove,
40 .hotplug = {
41 .enabled = true,
42 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070045struct acpi_memory_info {
46 struct list_head list;
47 u64 start_addr; /* Memory Range start physical addr */
48 u64 length; /* Memory Range length */
49 unsigned short caching; /* memory cache attribute */
50 unsigned short write_protect; /* memory read/write attribute */
51 unsigned int enabled:1;
52};
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054struct acpi_memory_device {
Hanjun Guoc18483a2020-09-27 17:55:49 +080055 struct acpi_device *device;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070056 struct list_head res_list;
David Hildenbrand2a157832021-09-07 19:55:34 -070057 int mgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070060static acpi_status
61acpi_memory_get_resource(struct acpi_resource *resource, void *context)
62{
63 struct acpi_memory_device *mem_device = context;
64 struct acpi_resource_address64 address64;
65 struct acpi_memory_info *info, *new;
66 acpi_status status;
67
68 status = acpi_resource_to_address64(resource, &address64);
69 if (ACPI_FAILURE(status) ||
70 (address64.resource_type != ACPI_MEMORY_RANGE))
71 return AE_OK;
72
73 list_for_each_entry(info, &mem_device->res_list, list) {
74 /* Can we combine the resource range information? */
75 if ((info->caching == address64.info.mem.caching) &&
76 (info->write_protect == address64.info.mem.write_protect) &&
Lv Zhenga45de932015-01-26 16:58:56 +080077 (info->start_addr + info->length == address64.address.minimum)) {
78 info->length += address64.address.address_length;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070079 return AE_OK;
80 }
81 }
82
83 new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
84 if (!new)
85 return AE_ERROR;
86
87 INIT_LIST_HEAD(&new->list);
88 new->caching = address64.info.mem.caching;
89 new->write_protect = address64.info.mem.write_protect;
Lv Zhenga45de932015-01-26 16:58:56 +080090 new->start_addr = address64.address.minimum;
91 new->length = address64.address.address_length;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070092 list_add_tail(&new->list, &mem_device->res_list);
93
94 return AE_OK;
95}
96
Wen Congyang386e52b2012-11-16 02:06:06 +010097static void
98acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
99{
100 struct acpi_memory_info *info, *n;
101
102 list_for_each_entry_safe(info, n, &mem_device->res_list, list)
103 kfree(info);
104 INIT_LIST_HEAD(&mem_device->res_list);
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int
108acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
109{
110 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
KAMEZAWA Hiroyuki5d2870f2006-08-05 12:15:04 -0700112 if (!list_empty(&mem_device->res_list))
113 return 0;
114
Patrick Mochelb8632782006-05-19 16:54:41 -0400115 status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700116 acpi_memory_get_resource, mem_device);
117 if (ACPI_FAILURE(status)) {
Wen Congyang386e52b2012-11-16 02:06:06 +0100118 acpi_memory_free_device_resources(mem_device);
Patrick Mocheld550d982006-06-27 00:41:40 -0400119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Len Brown4be44fc2005-08-05 00:44:28 -0400125static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400127 unsigned long long current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* Get device present/absent information from the _STA */
Zhang Yanfei16ff8162013-10-02 16:27:37 +0800130 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
131 METHOD_NAME__STA, NULL,
132 &current_status)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400133 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * Check for device status. Device should be
136 * present/enabled/functioning.
137 */
Bjorn Helgaasa0bd4ac2007-04-25 14:17:39 -0400138 if (!((current_status & ACPI_STA_DEVICE_PRESENT)
139 && (current_status & ACPI_STA_DEVICE_ENABLED)
140 && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400141 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Patrick Mocheld550d982006-06-27 00:41:40 -0400143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200146static int acpi_bind_memblk(struct memory_block *mem, void *arg)
147{
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100148 return acpi_bind_one(&mem->dev, arg);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200149}
150
151static int acpi_bind_memory_blocks(struct acpi_memory_info *info,
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100152 struct acpi_device *adev)
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200153{
David Hildenbrandfbcf73c2019-07-18 15:57:46 -0700154 return walk_memory_blocks(info->start_addr, info->length, adev,
155 acpi_bind_memblk);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200156}
157
158static int acpi_unbind_memblk(struct memory_block *mem, void *arg)
159{
160 acpi_unbind_one(&mem->dev);
161 return 0;
162}
163
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100164static void acpi_unbind_memory_blocks(struct acpi_memory_info *info)
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200165{
David Hildenbrandfbcf73c2019-07-18 15:57:46 -0700166 walk_memory_blocks(info->start_addr, info->length, NULL,
167 acpi_unbind_memblk);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200168}
169
Len Brown4be44fc2005-08-05 00:44:28 -0400170static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200172 acpi_handle handle = mem_device->device->handle;
David Hildenbrand2a157832021-09-07 19:55:34 -0700173 mhp_t mhp_flags = MHP_NID_IS_MGID;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700174 int result, num_enabled = 0;
175 struct acpi_memory_info *info;
David Hildenbrand2a157832021-09-07 19:55:34 -0700176 u64 total_length = 0;
177 int node, mgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200179 node = acpi_get_node(handle);
David Hildenbrand2a157832021-09-07 19:55:34 -0700180
181 list_for_each_entry(info, &mem_device->res_list, list) {
182 if (!info->length)
183 continue;
184 /* We want a single node for the whole memory group */
185 if (node < 0)
186 node = memory_add_physaddr_to_nid(info->start_addr);
187 total_length += info->length;
188 }
189
190 if (!total_length) {
191 dev_err(&mem_device->device->dev, "device is empty\n");
192 return -EINVAL;
193 }
194
195 mgid = memory_group_register_static(node, PFN_UP(total_length));
196 if (mgid < 0)
197 return mgid;
198 mem_device->mgid = mgid;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * Tell the VM there is more memory here...
202 * Note: Assume that this function returns zero on success
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700203 * We don't have memory-hot-add rollback function,now.
204 * (i.e. memory-hot-remove function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700206 list_for_each_entry(info, &mem_device->res_list, list) {
Zhao Yakui5d2619f2009-07-07 10:56:11 +0800207 /*
208 * If the memory block size is zero, please ignore it.
209 * Don't try to do the following memory hotplug flowchart.
210 */
211 if (!info->length)
212 continue;
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700213
Oscar Salvador4a3e5de2021-05-04 18:39:45 -0700214 if (mhp_supports_memmap_on_memory(info->length))
215 mhp_flags |= MHP_MEMMAP_ON_MEMORY;
David Hildenbrand2a157832021-09-07 19:55:34 -0700216 result = __add_memory(mgid, info->start_addr, info->length,
Oscar Salvador4a3e5de2021-05-04 18:39:45 -0700217 mhp_flags);
Wen Congyang65479472012-11-16 02:10:37 +0100218
219 /*
220 * If the memory block has been used by the kernel, add_memory()
221 * returns -EEXIST. If add_memory() returns the other error, it
222 * means that this memory block is not used by the kernel.
223 */
Yasuaki Ishimatsufd4655c2013-03-22 01:53:49 +0000224 if (result && result != -EEXIST)
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700225 continue;
Wen Congyang65479472012-11-16 02:10:37 +0100226
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100227 result = acpi_bind_memory_blocks(info, mem_device->device);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200228 if (result) {
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100229 acpi_unbind_memory_blocks(info);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200230 return -ENODEV;
231 }
232
Yasuaki Ishimatsubb49d822013-03-21 04:36:12 +0000233 info->enabled = 1;
234
Wen Congyang65479472012-11-16 02:10:37 +0100235 /*
236 * Add num_enable even if add_memory() returns -EEXIST, so the
237 * device is bound to this driver.
238 */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700239 num_enabled++;
240 }
241 if (!num_enabled) {
Toshi Kaniab6c5702012-11-20 23:42:28 +0000242 dev_err(&mem_device->device->dev, "add_memory failed\n");
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700243 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Zhao Yakui5d2619f2009-07-07 10:56:11 +0800245 /*
246 * Sometimes the memory device will contain several memory blocks.
247 * When one memory block is hot-added to the system memory, it will
248 * be regarded as a success.
249 * Otherwise if the last memory block can't be hot-added to the system
250 * memory, it will be failure and the memory device can't be bound with
251 * driver.
252 */
253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Rafael J. Wysocki242831e2013-05-27 12:58:46 +0200256static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700258 struct acpi_memory_info *info, *n;
Tang Chen60a5a192013-02-22 16:33:14 -0800259
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700260 list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
Wen Congyang65479472012-11-16 02:10:37 +0100261 if (!info->enabled)
Yasuaki Ishimatsufd4655c2013-03-22 01:53:49 +0000262 continue;
Wen Congyang65479472012-11-16 02:10:37 +0100263
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100264 acpi_unbind_memory_blocks(info);
David Hildenbrande1c158e2021-09-07 19:55:09 -0700265 __remove_memory(info->start_addr, info->length);
Yasuaki Ishimatsu19387b22012-11-15 06:59:31 +0000266 list_del(&info->list);
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700267 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Yasuaki Ishimatsu19387b22012-11-15 06:59:31 +0000269}
270
Wen Congyang386e52b2012-11-16 02:06:06 +0100271static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
272{
273 if (!mem_device)
274 return;
275
David Hildenbrand2a157832021-09-07 19:55:34 -0700276 /* In case we succeeded adding *some* memory, unregistering fails. */
277 if (mem_device->mgid >= 0)
278 memory_group_unregister(mem_device->mgid);
279
Wen Congyang386e52b2012-11-16 02:06:06 +0100280 acpi_memory_free_device_resources(mem_device);
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100281 mem_device->device->driver_data = NULL;
Wen Congyang386e52b2012-11-16 02:06:06 +0100282 kfree(mem_device);
283}
284
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100285static int acpi_memory_device_add(struct acpi_device *device,
286 const struct acpi_device_id *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100288 struct acpi_memory_device *mem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400292 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Burman Yan36bcbec2006-12-19 12:56:11 -0800294 mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!mem_device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400296 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700298 INIT_LIST_HEAD(&mem_device->res_list);
Patrick Mochel3b748632006-05-19 16:54:38 -0400299 mem_device->device = device;
David Hildenbrand2a157832021-09-07 19:55:34 -0700300 mem_device->mgid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
302 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700303 device->driver_data = mem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Get the range from the _CRS */
306 result = acpi_memory_get_device_resources(mem_device);
307 if (result) {
Toshi Kanid19f5032013-07-10 10:47:13 -0600308 device->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 kfree(mem_device);
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100313 result = acpi_memory_check_device(mem_device);
314 if (result) {
315 acpi_memory_device_free(mem_device);
316 return 0;
Bjorn Helgaas80f20fe2009-06-22 20:41:25 +0000317 }
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100318
319 result = acpi_memory_enable_device(mem_device);
320 if (result) {
321 dev_err(&device->dev, "acpi_memory_enable_device() error\n");
322 acpi_memory_device_free(mem_device);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200323 return result;
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100324 }
325
326 dev_dbg(&device->dev, "Memory device configured by ACPI\n");
327 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100330static void acpi_memory_device_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100332 struct acpi_memory_device *mem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (!device || !acpi_driver_data(device))
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100335 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200337 mem_device = acpi_driver_data(device);
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100338 acpi_memory_remove_memory(mem_device);
Wen Congyang386e52b2012-11-16 02:06:06 +0100339 acpi_memory_device_free(mem_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Prarit Bhargava00159a22014-01-14 14:21:13 -0500342static bool __initdata acpi_no_memhotplug;
343
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100344void __init acpi_memory_hotplug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Rafael J. Wysockicccd4202014-05-30 04:29:14 +0200346 if (acpi_no_memhotplug) {
347 memory_device_handler.attach = NULL;
348 acpi_scan_add_handler(&memory_device_handler);
Prarit Bhargava00159a22014-01-14 14:21:13 -0500349 return;
Rafael J. Wysockicccd4202014-05-30 04:29:14 +0200350 }
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100351 acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
Prarit Bhargava00159a22014-01-14 14:21:13 -0500353
354static int __init disable_acpi_memory_hotplug(char *str)
355{
356 acpi_no_memhotplug = true;
357 return 1;
358}
359__setup("acpi_no_memhotplug", disable_acpi_memory_hotplug);
Rafael J. Wysockicccd4202014-05-30 04:29:14 +0200360
361#else
362
363static struct acpi_scan_handler memory_device_handler = {
364 .ids = memory_device_ids,
365};
366
367void __init acpi_memory_hotplug_init(void)
368{
369 acpi_scan_add_handler(&memory_device_handler);
370}
371
372#endif /* CONFIG_ACPI_HOTPLUG_MEMORY */