Thomas Gleixner | 4359375 | 2019-05-19 15:51:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * All rights reserved. |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * ACPI based HotPlug driver that supports Memory Hotplug |
Nick Andrew | c7060d9 | 2009-01-03 18:53:39 +1100 | [diff] [blame] | 10 | * This driver fields notifications from firmware for memory add |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * and remove operations and alerts the VM of the affected memory |
| 12 | * ranges. |
| 13 | */ |
| 14 | |
Toshi Kani | ab6c570 | 2012-11-20 23:42:28 +0000 | [diff] [blame] | 15 | #include <linux/acpi.h> |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 16 | #include <linux/memory.h> |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 17 | #include <linux/memory_hotplug.h> |
| 18 | |
| 19 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define ACPI_MEMORY_DEVICE_CLASS "memory" |
| 22 | #define ACPI_MEMORY_DEVICE_HID "PNP0C80" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device" |
| 24 | |
Rafael J. Wysocki | cccd420 | 2014-05-30 04:29:14 +0200 | [diff] [blame] | 25 | static 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. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 32 | static int acpi_memory_device_add(struct acpi_device *device, |
| 33 | const struct acpi_device_id *not_used); |
| 34 | static void acpi_memory_device_remove(struct acpi_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 36 | static struct acpi_scan_handler memory_device_handler = { |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 37 | .ids = memory_device_ids, |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 38 | .attach = acpi_memory_device_add, |
| 39 | .detach = acpi_memory_device_remove, |
| 40 | .hotplug = { |
| 41 | .enabled = true, |
| 42 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 45 | struct 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | struct acpi_memory_device { |
Hanjun Guo | c18483a | 2020-09-27 17:55:49 +0800 | [diff] [blame] | 55 | struct acpi_device *device; |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 56 | struct list_head res_list; |
David Hildenbrand | 2a15783 | 2021-09-07 19:55:34 -0700 | [diff] [blame] | 57 | int mgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 60 | static acpi_status |
| 61 | acpi_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 Zheng | a45de93 | 2015-01-26 16:58:56 +0800 | [diff] [blame] | 77 | (info->start_addr + info->length == address64.address.minimum)) { |
| 78 | info->length += address64.address.address_length; |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 79 | 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 Zheng | a45de93 | 2015-01-26 16:58:56 +0800 | [diff] [blame] | 90 | new->start_addr = address64.address.minimum; |
| 91 | new->length = address64.address.address_length; |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 92 | list_add_tail(&new->list, &mem_device->res_list); |
| 93 | |
| 94 | return AE_OK; |
| 95 | } |
| 96 | |
Wen Congyang | 386e52b | 2012-11-16 02:06:06 +0100 | [diff] [blame] | 97 | static void |
| 98 | acpi_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static int |
| 108 | acpi_memory_get_device_resources(struct acpi_memory_device *mem_device) |
| 109 | { |
| 110 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
KAMEZAWA Hiroyuki | 5d2870f | 2006-08-05 12:15:04 -0700 | [diff] [blame] | 112 | if (!list_empty(&mem_device->res_list)) |
| 113 | return 0; |
| 114 | |
Patrick Mochel | b863278 | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 115 | status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS, |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 116 | acpi_memory_get_resource, mem_device); |
| 117 | if (ACPI_FAILURE(status)) { |
Wen Congyang | 386e52b | 2012-11-16 02:06:06 +0100 | [diff] [blame] | 118 | acpi_memory_free_device_resources(mem_device); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 119 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 122 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 125 | static int acpi_memory_check_device(struct acpi_memory_device *mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 127 | unsigned long long current_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | /* Get device present/absent information from the _STA */ |
Zhang Yanfei | 16ff816 | 2013-10-02 16:27:37 +0800 | [diff] [blame] | 130 | if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, |
| 131 | METHOD_NAME__STA, NULL, |
| 132 | ¤t_status))) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 133 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Check for device status. Device should be |
| 136 | * present/enabled/functioning. |
| 137 | */ |
Bjorn Helgaas | a0bd4ac | 2007-04-25 14:17:39 -0400 | [diff] [blame] | 138 | if (!((current_status & ACPI_STA_DEVICE_PRESENT) |
| 139 | && (current_status & ACPI_STA_DEVICE_ENABLED) |
| 140 | && (current_status & ACPI_STA_DEVICE_FUNCTIONING))) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 141 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 143 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 146 | static int acpi_bind_memblk(struct memory_block *mem, void *arg) |
| 147 | { |
Rafael J. Wysocki | 24dee1f | 2013-11-29 16:27:43 +0100 | [diff] [blame] | 148 | return acpi_bind_one(&mem->dev, arg); |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int acpi_bind_memory_blocks(struct acpi_memory_info *info, |
Rafael J. Wysocki | 24dee1f | 2013-11-29 16:27:43 +0100 | [diff] [blame] | 152 | struct acpi_device *adev) |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 153 | { |
David Hildenbrand | fbcf73c | 2019-07-18 15:57:46 -0700 | [diff] [blame] | 154 | return walk_memory_blocks(info->start_addr, info->length, adev, |
| 155 | acpi_bind_memblk); |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int acpi_unbind_memblk(struct memory_block *mem, void *arg) |
| 159 | { |
| 160 | acpi_unbind_one(&mem->dev); |
| 161 | return 0; |
| 162 | } |
| 163 | |
Rafael J. Wysocki | 24dee1f | 2013-11-29 16:27:43 +0100 | [diff] [blame] | 164 | static void acpi_unbind_memory_blocks(struct acpi_memory_info *info) |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 165 | { |
David Hildenbrand | fbcf73c | 2019-07-18 15:57:46 -0700 | [diff] [blame] | 166 | walk_memory_blocks(info->start_addr, info->length, NULL, |
| 167 | acpi_unbind_memblk); |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 170 | static int acpi_memory_enable_device(struct acpi_memory_device *mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 172 | acpi_handle handle = mem_device->device->handle; |
David Hildenbrand | 2a15783 | 2021-09-07 19:55:34 -0700 | [diff] [blame] | 173 | mhp_t mhp_flags = MHP_NID_IS_MGID; |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 174 | int result, num_enabled = 0; |
| 175 | struct acpi_memory_info *info; |
David Hildenbrand | 2a15783 | 2021-09-07 19:55:34 -0700 | [diff] [blame] | 176 | u64 total_length = 0; |
| 177 | int node, mgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 179 | node = acpi_get_node(handle); |
David Hildenbrand | 2a15783 | 2021-09-07 19:55:34 -0700 | [diff] [blame] | 180 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* |
| 201 | * Tell the VM there is more memory here... |
| 202 | * Note: Assume that this function returns zero on success |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 203 | * We don't have memory-hot-add rollback function,now. |
| 204 | * (i.e. memory-hot-remove function) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | */ |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 206 | list_for_each_entry(info, &mem_device->res_list, list) { |
Zhao Yakui | 5d2619f | 2009-07-07 10:56:11 +0800 | [diff] [blame] | 207 | /* |
| 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 Mannthey | 8c2676a | 2006-09-30 23:27:07 -0700 | [diff] [blame] | 213 | |
Oscar Salvador | 4a3e5de | 2021-05-04 18:39:45 -0700 | [diff] [blame] | 214 | if (mhp_supports_memmap_on_memory(info->length)) |
| 215 | mhp_flags |= MHP_MEMMAP_ON_MEMORY; |
David Hildenbrand | 2a15783 | 2021-09-07 19:55:34 -0700 | [diff] [blame] | 216 | result = __add_memory(mgid, info->start_addr, info->length, |
Oscar Salvador | 4a3e5de | 2021-05-04 18:39:45 -0700 | [diff] [blame] | 217 | mhp_flags); |
Wen Congyang | 6547947 | 2012-11-16 02:10:37 +0100 | [diff] [blame] | 218 | |
| 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 Ishimatsu | fd4655c | 2013-03-22 01:53:49 +0000 | [diff] [blame] | 224 | if (result && result != -EEXIST) |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 225 | continue; |
Wen Congyang | 6547947 | 2012-11-16 02:10:37 +0100 | [diff] [blame] | 226 | |
Rafael J. Wysocki | 24dee1f | 2013-11-29 16:27:43 +0100 | [diff] [blame] | 227 | result = acpi_bind_memory_blocks(info, mem_device->device); |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 228 | if (result) { |
Rafael J. Wysocki | 24dee1f | 2013-11-29 16:27:43 +0100 | [diff] [blame] | 229 | acpi_unbind_memory_blocks(info); |
Rafael J. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 230 | return -ENODEV; |
| 231 | } |
| 232 | |
Yasuaki Ishimatsu | bb49d82 | 2013-03-21 04:36:12 +0000 | [diff] [blame] | 233 | info->enabled = 1; |
| 234 | |
Wen Congyang | 6547947 | 2012-11-16 02:10:37 +0100 | [diff] [blame] | 235 | /* |
| 236 | * Add num_enable even if add_memory() returns -EEXIST, so the |
| 237 | * device is bound to this driver. |
| 238 | */ |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 239 | num_enabled++; |
| 240 | } |
| 241 | if (!num_enabled) { |
Toshi Kani | ab6c570 | 2012-11-20 23:42:28 +0000 | [diff] [blame] | 242 | dev_err(&mem_device->device->dev, "add_memory failed\n"); |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 243 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
Zhao Yakui | 5d2619f | 2009-07-07 10:56:11 +0800 | [diff] [blame] | 245 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Rafael J. Wysocki | 242831e | 2013-05-27 12:58:46 +0200 | [diff] [blame] | 256 | static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 258 | struct acpi_memory_info *info, *n; |
Tang Chen | 60a5a19 | 2013-02-22 16:33:14 -0800 | [diff] [blame] | 259 | |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 260 | list_for_each_entry_safe(info, n, &mem_device->res_list, list) { |
Wen Congyang | 6547947 | 2012-11-16 02:10:37 +0100 | [diff] [blame] | 261 | if (!info->enabled) |
Yasuaki Ishimatsu | fd4655c | 2013-03-22 01:53:49 +0000 | [diff] [blame] | 262 | continue; |
Wen Congyang | 6547947 | 2012-11-16 02:10:37 +0100 | [diff] [blame] | 263 | |
Rafael J. Wysocki | 24dee1f | 2013-11-29 16:27:43 +0100 | [diff] [blame] | 264 | acpi_unbind_memory_blocks(info); |
David Hildenbrand | e1c158e | 2021-09-07 19:55:09 -0700 | [diff] [blame] | 265 | __remove_memory(info->start_addr, info->length); |
Yasuaki Ishimatsu | 19387b2 | 2012-11-15 06:59:31 +0000 | [diff] [blame] | 266 | list_del(&info->list); |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 267 | kfree(info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Yasuaki Ishimatsu | 19387b2 | 2012-11-15 06:59:31 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Wen Congyang | 386e52b | 2012-11-16 02:06:06 +0100 | [diff] [blame] | 271 | static void acpi_memory_device_free(struct acpi_memory_device *mem_device) |
| 272 | { |
| 273 | if (!mem_device) |
| 274 | return; |
| 275 | |
David Hildenbrand | 2a15783 | 2021-09-07 19:55:34 -0700 | [diff] [blame] | 276 | /* In case we succeeded adding *some* memory, unregistering fails. */ |
| 277 | if (mem_device->mgid >= 0) |
| 278 | memory_group_unregister(mem_device->mgid); |
| 279 | |
Wen Congyang | 386e52b | 2012-11-16 02:06:06 +0100 | [diff] [blame] | 280 | acpi_memory_free_device_resources(mem_device); |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 281 | mem_device->device->driver_data = NULL; |
Wen Congyang | 386e52b | 2012-11-16 02:06:06 +0100 | [diff] [blame] | 282 | kfree(mem_device); |
| 283 | } |
| 284 | |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 285 | static int acpi_memory_device_add(struct acpi_device *device, |
| 286 | const struct acpi_device_id *not_used) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 288 | struct acpi_memory_device *mem_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 292 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 294 | mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (!mem_device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 296 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
KAMEZAWA Hiroyuki | 9ac0239 | 2006-06-27 02:53:27 -0700 | [diff] [blame] | 298 | INIT_LIST_HEAD(&mem_device->res_list); |
Patrick Mochel | 3b74863 | 2006-05-19 16:54:38 -0400 | [diff] [blame] | 299 | mem_device->device = device; |
David Hildenbrand | 2a15783 | 2021-09-07 19:55:34 -0700 | [diff] [blame] | 300 | mem_device->mgid = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME); |
| 302 | sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS); |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 303 | device->driver_data = mem_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | /* Get the range from the _CRS */ |
| 306 | result = acpi_memory_get_device_resources(mem_device); |
| 307 | if (result) { |
Toshi Kani | d19f503 | 2013-07-10 10:47:13 -0600 | [diff] [blame] | 308 | device->driver_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | kfree(mem_device); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 310 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 313 | result = acpi_memory_check_device(mem_device); |
| 314 | if (result) { |
| 315 | acpi_memory_device_free(mem_device); |
| 316 | return 0; |
Bjorn Helgaas | 80f20fe | 2009-06-22 20:41:25 +0000 | [diff] [blame] | 317 | } |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 318 | |
| 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. Wysocki | e2ff394 | 2013-05-08 00:29:49 +0200 | [diff] [blame] | 323 | return result; |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | dev_dbg(&device->dev, "Memory device configured by ACPI\n"); |
| 327 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 330 | static void acpi_memory_device_remove(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 332 | struct acpi_memory_device *mem_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | if (!device || !acpi_driver_data(device)) |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 335 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 337 | mem_device = acpi_driver_data(device); |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 338 | acpi_memory_remove_memory(mem_device); |
Wen Congyang | 386e52b | 2012-11-16 02:06:06 +0100 | [diff] [blame] | 339 | acpi_memory_device_free(mem_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Prarit Bhargava | 00159a2 | 2014-01-14 14:21:13 -0500 | [diff] [blame] | 342 | static bool __initdata acpi_no_memhotplug; |
| 343 | |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 344 | void __init acpi_memory_hotplug_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Rafael J. Wysocki | cccd420 | 2014-05-30 04:29:14 +0200 | [diff] [blame] | 346 | if (acpi_no_memhotplug) { |
| 347 | memory_device_handler.attach = NULL; |
| 348 | acpi_scan_add_handler(&memory_device_handler); |
Prarit Bhargava | 00159a2 | 2014-01-14 14:21:13 -0500 | [diff] [blame] | 349 | return; |
Rafael J. Wysocki | cccd420 | 2014-05-30 04:29:14 +0200 | [diff] [blame] | 350 | } |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 351 | acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
Prarit Bhargava | 00159a2 | 2014-01-14 14:21:13 -0500 | [diff] [blame] | 353 | |
| 354 | static 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. Wysocki | cccd420 | 2014-05-30 04:29:14 +0200 | [diff] [blame] | 360 | |
| 361 | #else |
| 362 | |
| 363 | static struct acpi_scan_handler memory_device_handler = { |
| 364 | .ids = memory_device_ids, |
| 365 | }; |
| 366 | |
| 367 | void __init acpi_memory_hotplug_init(void) |
| 368 | { |
| 369 | acpi_scan_add_handler(&memory_device_handler); |
| 370 | } |
| 371 | |
| 372 | #endif /* CONFIG_ACPI_HOTPLUG_MEMORY */ |