blob: 6b0d3ef7309cb710a63b38848ad35b770fed3c1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002 * Copyright (C) 2004, 2013 Intel Corporation
3 * Author: Naveen B S <naveen.b.s@intel.com>
4 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * ACPI based HotPlug driver that supports Memory Hotplug
Nick Andrewc7060d92009-01-03 18:53:39 +110020 * This driver fields notifications from firmware for memory add
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * and remove operations and alerts the VM of the affected memory
22 * ranges.
23 */
24
Toshi Kaniab6c5702012-11-20 23:42:28 +000025#include <linux/acpi.h>
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +020026#include <linux/memory.h>
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010027#include <linux/memory_hotplug.h>
28
29#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define ACPI_MEMORY_DEVICE_CLASS "memory"
32#define ACPI_MEMORY_DEVICE_HID "PNP0C80"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
34
35#define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
36
Zhao Yakuiaa7b2b22009-07-03 10:49:03 +080037#undef PREFIX
38#define PREFIX "ACPI:memory_hp:"
39
Len Brownf52fd662007-02-12 22:42:12 -050040ACPI_MODULE_NAME("acpi_memhotplug");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Rafael J. Wysockicccd4202014-05-30 04:29:14 +020042static const struct acpi_device_id memory_device_ids[] = {
43 {ACPI_MEMORY_DEVICE_HID, 0},
44 {"", 0},
45};
46
47#ifdef CONFIG_ACPI_HOTPLUG_MEMORY
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* Memory Device States */
50#define MEMORY_INVALID_STATE 0
51#define MEMORY_POWER_ON_STATE 1
52#define MEMORY_POWER_OFF_STATE 2
53
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010054static int acpi_memory_device_add(struct acpi_device *device,
55 const struct acpi_device_id *not_used);
56static void acpi_memory_device_remove(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010058static struct acpi_scan_handler memory_device_handler = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020059 .ids = memory_device_ids,
Rafael J. Wysocki0a347642013-03-03 23:18:03 +010060 .attach = acpi_memory_device_add,
61 .detach = acpi_memory_device_remove,
62 .hotplug = {
63 .enabled = true,
64 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070067struct acpi_memory_info {
68 struct list_head list;
69 u64 start_addr; /* Memory Range start physical addr */
70 u64 length; /* Memory Range length */
71 unsigned short caching; /* memory cache attribute */
72 unsigned short write_protect; /* memory read/write attribute */
73 unsigned int enabled:1;
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076struct acpi_memory_device {
Patrick Mochel3b748632006-05-19 16:54:38 -040077 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040078 unsigned int state; /* State of the memory device */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070079 struct list_head res_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -070082static acpi_status
83acpi_memory_get_resource(struct acpi_resource *resource, void *context)
84{
85 struct acpi_memory_device *mem_device = context;
86 struct acpi_resource_address64 address64;
87 struct acpi_memory_info *info, *new;
88 acpi_status status;
89
90 status = acpi_resource_to_address64(resource, &address64);
91 if (ACPI_FAILURE(status) ||
92 (address64.resource_type != ACPI_MEMORY_RANGE))
93 return AE_OK;
94
95 list_for_each_entry(info, &mem_device->res_list, list) {
96 /* Can we combine the resource range information? */
97 if ((info->caching == address64.info.mem.caching) &&
98 (info->write_protect == address64.info.mem.write_protect) &&
Lv Zhenga45de932015-01-26 16:58:56 +080099 (info->start_addr + info->length == address64.address.minimum)) {
100 info->length += address64.address.address_length;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700101 return AE_OK;
102 }
103 }
104
105 new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
106 if (!new)
107 return AE_ERROR;
108
109 INIT_LIST_HEAD(&new->list);
110 new->caching = address64.info.mem.caching;
111 new->write_protect = address64.info.mem.write_protect;
Lv Zhenga45de932015-01-26 16:58:56 +0800112 new->start_addr = address64.address.minimum;
113 new->length = address64.address.address_length;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700114 list_add_tail(&new->list, &mem_device->res_list);
115
116 return AE_OK;
117}
118
Wen Congyang386e52b2012-11-16 02:06:06 +0100119static void
120acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
121{
122 struct acpi_memory_info *info, *n;
123
124 list_for_each_entry_safe(info, n, &mem_device->res_list, list)
125 kfree(info);
126 INIT_LIST_HEAD(&mem_device->res_list);
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int
130acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
131{
132 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
KAMEZAWA Hiroyuki5d2870f2006-08-05 12:15:04 -0700134 if (!list_empty(&mem_device->res_list))
135 return 0;
136
Patrick Mochelb8632782006-05-19 16:54:41 -0400137 status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700138 acpi_memory_get_resource, mem_device);
139 if (ACPI_FAILURE(status)) {
Wen Congyang386e52b2012-11-16 02:06:06 +0100140 acpi_memory_free_device_resources(mem_device);
Patrick Mocheld550d982006-06-27 00:41:40 -0400141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
Patrick Mocheld550d982006-06-27 00:41:40 -0400144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Len Brown4be44fc2005-08-05 00:44:28 -0400147static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400149 unsigned long long current_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* Get device present/absent information from the _STA */
Zhang Yanfei16ff816d2013-10-02 16:27:37 +0800152 if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
153 METHOD_NAME__STA, NULL,
154 &current_status)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400155 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
157 * Check for device status. Device should be
158 * present/enabled/functioning.
159 */
Bjorn Helgaasa0bd4ac2007-04-25 14:17:39 -0400160 if (!((current_status & ACPI_STA_DEVICE_PRESENT)
161 && (current_status & ACPI_STA_DEVICE_ENABLED)
162 && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400163 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200168static unsigned long acpi_meminfo_start_pfn(struct acpi_memory_info *info)
169{
170 return PFN_DOWN(info->start_addr);
171}
172
173static unsigned long acpi_meminfo_end_pfn(struct acpi_memory_info *info)
174{
175 return PFN_UP(info->start_addr + info->length-1);
176}
177
178static int acpi_bind_memblk(struct memory_block *mem, void *arg)
179{
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100180 return acpi_bind_one(&mem->dev, arg);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200181}
182
183static int acpi_bind_memory_blocks(struct acpi_memory_info *info,
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100184 struct acpi_device *adev)
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200185{
186 return walk_memory_range(acpi_meminfo_start_pfn(info),
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100187 acpi_meminfo_end_pfn(info), adev,
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200188 acpi_bind_memblk);
189}
190
191static int acpi_unbind_memblk(struct memory_block *mem, void *arg)
192{
193 acpi_unbind_one(&mem->dev);
194 return 0;
195}
196
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100197static void acpi_unbind_memory_blocks(struct acpi_memory_info *info)
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200198{
199 walk_memory_range(acpi_meminfo_start_pfn(info),
200 acpi_meminfo_end_pfn(info), NULL, acpi_unbind_memblk);
201}
202
Len Brown4be44fc2005-08-05 00:44:28 -0400203static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200205 acpi_handle handle = mem_device->device->handle;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700206 int result, num_enabled = 0;
207 struct acpi_memory_info *info;
Yasunori Goto1e3590e2006-06-27 02:53:31 -0700208 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200210 node = acpi_get_node(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
212 * Tell the VM there is more memory here...
213 * Note: Assume that this function returns zero on success
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700214 * We don't have memory-hot-add rollback function,now.
215 * (i.e. memory-hot-remove function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700217 list_for_each_entry(info, &mem_device->res_list, list) {
KAMEZAWA Hiroyukifa25d8d2006-08-05 12:15:02 -0700218 if (info->enabled) { /* just sanity check...*/
Yasunori Gotodd56a8e2006-06-27 02:53:29 -0700219 num_enabled++;
220 continue;
221 }
Zhao Yakui5d2619f2009-07-07 10:56:11 +0800222 /*
223 * If the memory block size is zero, please ignore it.
224 * Don't try to do the following memory hotplug flowchart.
225 */
226 if (!info->length)
227 continue;
Keith Mannthey8c2676a2006-09-30 23:27:07 -0700228 if (node < 0)
229 node = memory_add_physaddr_to_nid(info->start_addr);
230
Yasunori Gotobc02af92006-06-27 02:53:30 -0700231 result = add_memory(node, info->start_addr, info->length);
Wen Congyang65479472012-11-16 02:10:37 +0100232
233 /*
234 * If the memory block has been used by the kernel, add_memory()
235 * returns -EEXIST. If add_memory() returns the other error, it
236 * means that this memory block is not used by the kernel.
237 */
Yasuaki Ishimatsufd4655c2013-03-22 01:53:49 +0000238 if (result && result != -EEXIST)
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700239 continue;
Wen Congyang65479472012-11-16 02:10:37 +0100240
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100241 result = acpi_bind_memory_blocks(info, mem_device->device);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200242 if (result) {
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100243 acpi_unbind_memory_blocks(info);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200244 return -ENODEV;
245 }
246
Yasuaki Ishimatsubb49d822013-03-21 04:36:12 +0000247 info->enabled = 1;
248
Wen Congyang65479472012-11-16 02:10:37 +0100249 /*
250 * Add num_enable even if add_memory() returns -EEXIST, so the
251 * device is bound to this driver.
252 */
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700253 num_enabled++;
254 }
255 if (!num_enabled) {
Toshi Kaniab6c5702012-11-20 23:42:28 +0000256 dev_err(&mem_device->device->dev, "add_memory failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 mem_device->state = MEMORY_INVALID_STATE;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Zhao Yakui5d2619f2009-07-07 10:56:11 +0800260 /*
261 * Sometimes the memory device will contain several memory blocks.
262 * When one memory block is hot-added to the system memory, it will
263 * be regarded as a success.
264 * Otherwise if the last memory block can't be hot-added to the system
265 * memory, it will be failure and the memory device can't be bound with
266 * driver.
267 */
268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Rafael J. Wysocki242831e2013-05-27 12:58:46 +0200271static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200273 acpi_handle handle = mem_device->device->handle;
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700274 struct acpi_memory_info *info, *n;
Rafael J. Wysocki242831e2013-05-27 12:58:46 +0200275 int nid = acpi_get_node(handle);
Tang Chen60a5a192013-02-22 16:33:14 -0800276
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700277 list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
Wen Congyang65479472012-11-16 02:10:37 +0100278 if (!info->enabled)
Yasuaki Ishimatsufd4655c2013-03-22 01:53:49 +0000279 continue;
Wen Congyang65479472012-11-16 02:10:37 +0100280
Jianguo Wu1bb25df2013-08-30 09:25:40 +0800281 if (nid == NUMA_NO_NODE)
Tang Chen60a5a192013-02-22 16:33:14 -0800282 nid = memory_add_physaddr_to_nid(info->start_addr);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200283
Rafael J. Wysocki24dee1f2013-11-29 16:27:43 +0100284 acpi_unbind_memory_blocks(info);
Rafael J. Wysocki242831e2013-05-27 12:58:46 +0200285 remove_memory(nid, info->start_addr, info->length);
Yasuaki Ishimatsu19387b22012-11-15 06:59:31 +0000286 list_del(&info->list);
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700287 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Yasuaki Ishimatsu19387b22012-11-15 06:59:31 +0000289}
290
Wen Congyang386e52b2012-11-16 02:06:06 +0100291static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
292{
293 if (!mem_device)
294 return;
295
296 acpi_memory_free_device_resources(mem_device);
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100297 mem_device->device->driver_data = NULL;
Wen Congyang386e52b2012-11-16 02:06:06 +0100298 kfree(mem_device);
299}
300
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100301static int acpi_memory_device_add(struct acpi_device *device,
302 const struct acpi_device_id *not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100304 struct acpi_memory_device *mem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Burman Yan36bcbec2006-12-19 12:56:11 -0800310 mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!mem_device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
KAMEZAWA Hiroyuki9ac02392006-06-27 02:53:27 -0700314 INIT_LIST_HEAD(&mem_device->res_list);
Patrick Mochel3b748632006-05-19 16:54:38 -0400315 mem_device->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
317 sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700318 device->driver_data = mem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* Get the range from the _CRS */
321 result = acpi_memory_get_device_resources(mem_device);
322 if (result) {
Toshi Kanid19f5032013-07-10 10:47:13 -0600323 device->driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 kfree(mem_device);
Patrick Mocheld550d982006-06-27 00:41:40 -0400325 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 /* Set the device state */
329 mem_device->state = MEMORY_POWER_ON_STATE;
330
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100331 result = acpi_memory_check_device(mem_device);
332 if (result) {
333 acpi_memory_device_free(mem_device);
334 return 0;
Bjorn Helgaas80f20fe2009-06-22 20:41:25 +0000335 }
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100336
337 result = acpi_memory_enable_device(mem_device);
338 if (result) {
339 dev_err(&device->dev, "acpi_memory_enable_device() error\n");
340 acpi_memory_device_free(mem_device);
Rafael J. Wysockie2ff3942013-05-08 00:29:49 +0200341 return result;
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100342 }
343
344 dev_dbg(&device->dev, "Memory device configured by ACPI\n");
345 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100348static void acpi_memory_device_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100350 struct acpi_memory_device *mem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!device || !acpi_driver_data(device))
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100353 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200355 mem_device = acpi_driver_data(device);
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100356 acpi_memory_remove_memory(mem_device);
Wen Congyang386e52b2012-11-16 02:06:06 +0100357 acpi_memory_device_free(mem_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Prarit Bhargava00159a22014-01-14 14:21:13 -0500360static bool __initdata acpi_no_memhotplug;
361
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100362void __init acpi_memory_hotplug_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Rafael J. Wysockicccd4202014-05-30 04:29:14 +0200364 if (acpi_no_memhotplug) {
365 memory_device_handler.attach = NULL;
366 acpi_scan_add_handler(&memory_device_handler);
Prarit Bhargava00159a22014-01-14 14:21:13 -0500367 return;
Rafael J. Wysockicccd4202014-05-30 04:29:14 +0200368 }
Rafael J. Wysocki0a347642013-03-03 23:18:03 +0100369 acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
Prarit Bhargava00159a22014-01-14 14:21:13 -0500371
372static int __init disable_acpi_memory_hotplug(char *str)
373{
374 acpi_no_memhotplug = true;
375 return 1;
376}
377__setup("acpi_no_memhotplug", disable_acpi_memory_hotplug);
Rafael J. Wysockicccd4202014-05-30 04:29:14 +0200378
379#else
380
381static struct acpi_scan_handler memory_device_handler = {
382 .ids = memory_device_ids,
383};
384
385void __init acpi_memory_hotplug_init(void)
386{
387 acpi_scan_add_handler(&memory_device_handler);
388}
389
390#endif /* CONFIG_ACPI_HOTPLUG_MEMORY */