ACPI: Clean up inclusions of ACPI header files
[linux-block.git] / drivers / xen / xen-acpi-memhotplug.c
1 /*
2  * Copyright (C) 2012 Intel Corporation
3  *    Author: Liu Jinsong <jinsong.liu@intel.com>
4  *    Author: Jiang Yunhong <yunhong.jiang@intel.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14  * NON INFRINGEMENT.  See the GNU General Public License for more
15  * details.
16  */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/acpi.h>
25 #include <xen/acpi.h>
26 #include <xen/interface/platform.h>
27 #include <asm/xen/hypercall.h>
28
29 #define PREFIX "ACPI:xen_memory_hotplug:"
30
31 struct acpi_memory_info {
32         struct list_head list;
33         u64 start_addr;         /* Memory Range start physical addr */
34         u64 length;             /* Memory Range length */
35         unsigned short caching; /* memory cache attribute */
36         unsigned short write_protect;   /* memory read/write attribute */
37                                 /* copied from buffer getting from _CRS */
38         unsigned int enabled:1;
39 };
40
41 struct acpi_memory_device {
42         struct acpi_device *device;
43         struct list_head res_list;
44 };
45
46 static bool acpi_hotmem_initialized __read_mostly;
47
48 static int xen_hotadd_memory(int pxm, struct acpi_memory_info *info)
49 {
50         int rc;
51         struct xen_platform_op op;
52
53         op.cmd = XENPF_mem_hotadd;
54         op.u.mem_add.spfn = info->start_addr >> PAGE_SHIFT;
55         op.u.mem_add.epfn = (info->start_addr + info->length) >> PAGE_SHIFT;
56         op.u.mem_add.pxm = pxm;
57
58         rc = HYPERVISOR_dom0_op(&op);
59         if (rc)
60                 pr_err(PREFIX "Xen Hotplug Memory Add failed on "
61                         "0x%lx -> 0x%lx, _PXM: %d, error: %d\n",
62                         (unsigned long)info->start_addr,
63                         (unsigned long)(info->start_addr + info->length),
64                         pxm, rc);
65
66         return rc;
67 }
68
69 static int xen_acpi_memory_enable_device(struct acpi_memory_device *mem_device)
70 {
71         int pxm, result;
72         int num_enabled = 0;
73         struct acpi_memory_info *info;
74
75         if (!mem_device)
76                 return -EINVAL;
77
78         pxm = xen_acpi_get_pxm(mem_device->device->handle);
79         if (pxm < 0)
80                 return pxm;
81
82         list_for_each_entry(info, &mem_device->res_list, list) {
83                 if (info->enabled) { /* just sanity check...*/
84                         num_enabled++;
85                         continue;
86                 }
87
88                 if (!info->length)
89                         continue;
90
91                 result = xen_hotadd_memory(pxm, info);
92                 if (result)
93                         continue;
94                 info->enabled = 1;
95                 num_enabled++;
96         }
97
98         if (!num_enabled)
99                 return -ENODEV;
100
101         return 0;
102 }
103
104 static acpi_status
105 acpi_memory_get_resource(struct acpi_resource *resource, void *context)
106 {
107         struct acpi_memory_device *mem_device = context;
108         struct acpi_resource_address64 address64;
109         struct acpi_memory_info *info, *new;
110         acpi_status status;
111
112         status = acpi_resource_to_address64(resource, &address64);
113         if (ACPI_FAILURE(status) ||
114             (address64.resource_type != ACPI_MEMORY_RANGE))
115                 return AE_OK;
116
117         list_for_each_entry(info, &mem_device->res_list, list) {
118                 if ((info->caching == address64.info.mem.caching) &&
119                     (info->write_protect == address64.info.mem.write_protect) &&
120                     (info->start_addr + info->length == address64.minimum)) {
121                         info->length += address64.address_length;
122                         return AE_OK;
123                 }
124         }
125
126         new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
127         if (!new)
128                 return AE_ERROR;
129
130         INIT_LIST_HEAD(&new->list);
131         new->caching = address64.info.mem.caching;
132         new->write_protect = address64.info.mem.write_protect;
133         new->start_addr = address64.minimum;
134         new->length = address64.address_length;
135         list_add_tail(&new->list, &mem_device->res_list);
136
137         return AE_OK;
138 }
139
140 static int
141 acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
142 {
143         acpi_status status;
144         struct acpi_memory_info *info, *n;
145
146         if (!list_empty(&mem_device->res_list))
147                 return 0;
148
149         status = acpi_walk_resources(mem_device->device->handle,
150                 METHOD_NAME__CRS, acpi_memory_get_resource, mem_device);
151
152         if (ACPI_FAILURE(status)) {
153                 list_for_each_entry_safe(info, n, &mem_device->res_list, list)
154                         kfree(info);
155                 INIT_LIST_HEAD(&mem_device->res_list);
156                 return -EINVAL;
157         }
158
159         return 0;
160 }
161
162 static int acpi_memory_get_device(acpi_handle handle,
163                                   struct acpi_memory_device **mem_device)
164 {
165         struct acpi_device *device = NULL;
166         int result = 0;
167
168         acpi_scan_lock_acquire();
169
170         acpi_bus_get_device(handle, &device);
171         if (device)
172                 goto end;
173
174         /*
175          * Now add the notified device.  This creates the acpi_device
176          * and invokes .add function
177          */
178         result = acpi_bus_scan(handle);
179         if (result) {
180                 pr_warn(PREFIX "ACPI namespace scan failed\n");
181                 result = -EINVAL;
182                 goto out;
183         }
184         result = acpi_bus_get_device(handle, &device);
185         if (result) {
186                 pr_warn(PREFIX "Missing device object\n");
187                 result = -EINVAL;
188                 goto out;
189         }
190
191 end:
192         *mem_device = acpi_driver_data(device);
193         if (!(*mem_device)) {
194                 pr_err(PREFIX "driver data not found\n");
195                 result = -ENODEV;
196                 goto out;
197         }
198
199 out:
200         acpi_scan_lock_release();
201         return result;
202 }
203
204 static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
205 {
206         unsigned long long current_status;
207
208         /* Get device present/absent information from the _STA */
209         if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
210                                 "_STA", NULL, &current_status)))
211                 return -ENODEV;
212         /*
213          * Check for device status. Device should be
214          * present/enabled/functioning.
215          */
216         if (!((current_status & ACPI_STA_DEVICE_PRESENT)
217               && (current_status & ACPI_STA_DEVICE_ENABLED)
218               && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
219                 return -ENODEV;
220
221         return 0;
222 }
223
224 static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
225 {
226         pr_debug(PREFIX "Xen does not support memory hotremove\n");
227
228         return -ENOSYS;
229 }
230
231 static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
232 {
233         struct acpi_memory_device *mem_device;
234         struct acpi_device *device;
235         u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
236
237         switch (event) {
238         case ACPI_NOTIFY_BUS_CHECK:
239                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
240                         "\nReceived BUS CHECK notification for device\n"));
241                 /* Fall Through */
242         case ACPI_NOTIFY_DEVICE_CHECK:
243                 if (event == ACPI_NOTIFY_DEVICE_CHECK)
244                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
245                         "\nReceived DEVICE CHECK notification for device\n"));
246
247                 if (acpi_memory_get_device(handle, &mem_device)) {
248                         pr_err(PREFIX "Cannot find driver data\n");
249                         break;
250                 }
251
252                 ost_code = ACPI_OST_SC_SUCCESS;
253                 break;
254
255         case ACPI_NOTIFY_EJECT_REQUEST:
256                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
257                         "\nReceived EJECT REQUEST notification for device\n"));
258
259                 acpi_scan_lock_acquire();
260                 if (acpi_bus_get_device(handle, &device)) {
261                         acpi_scan_lock_release();
262                         pr_err(PREFIX "Device doesn't exist\n");
263                         break;
264                 }
265                 mem_device = acpi_driver_data(device);
266                 if (!mem_device) {
267                         acpi_scan_lock_release();
268                         pr_err(PREFIX "Driver Data is NULL\n");
269                         break;
270                 }
271
272                 /*
273                  * TBD: implement acpi_memory_disable_device and invoke
274                  * acpi_bus_remove if Xen support hotremove in the future
275                  */
276                 acpi_memory_disable_device(mem_device);
277                 acpi_scan_lock_release();
278                 break;
279
280         default:
281                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
282                                   "Unsupported event [0x%x]\n", event));
283                 /* non-hotplug event; possibly handled by other handler */
284                 return;
285         }
286
287         (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
288         return;
289 }
290
291 static int xen_acpi_memory_device_add(struct acpi_device *device)
292 {
293         int result;
294         struct acpi_memory_device *mem_device = NULL;
295
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                 kfree(mem_device);
314                 return result;
315         }
316
317         /*
318          * For booting existed memory devices, early boot code has recognized
319          * memory area by EFI/E820. If DSDT shows these memory devices on boot,
320          * hotplug is not necessary for them.
321          * For hot-added memory devices during runtime, it need hypercall to
322          * Xen hypervisor to add memory.
323          */
324         if (!acpi_hotmem_initialized)
325                 return 0;
326
327         if (!acpi_memory_check_device(mem_device))
328                 result = xen_acpi_memory_enable_device(mem_device);
329
330         return result;
331 }
332
333 static int xen_acpi_memory_device_remove(struct acpi_device *device)
334 {
335         struct acpi_memory_device *mem_device = NULL;
336
337         if (!device || !acpi_driver_data(device))
338                 return -EINVAL;
339
340         mem_device = acpi_driver_data(device);
341         kfree(mem_device);
342
343         return 0;
344 }
345
346 /*
347  * Helper function to check for memory device
348  */
349 static acpi_status is_memory_device(acpi_handle handle)
350 {
351         char *hardware_id;
352         acpi_status status;
353         struct acpi_device_info *info;
354
355         status = acpi_get_object_info(handle, &info);
356         if (ACPI_FAILURE(status))
357                 return status;
358
359         if (!(info->valid & ACPI_VALID_HID)) {
360                 kfree(info);
361                 return AE_ERROR;
362         }
363
364         hardware_id = info->hardware_id.string;
365         if ((hardware_id == NULL) ||
366             (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
367                 status = AE_ERROR;
368
369         kfree(info);
370         return status;
371 }
372
373 static acpi_status
374 acpi_memory_register_notify_handler(acpi_handle handle,
375                                     u32 level, void *ctxt, void **retv)
376 {
377         acpi_status status;
378
379         status = is_memory_device(handle);
380         if (ACPI_FAILURE(status))
381                 return AE_OK;   /* continue */
382
383         status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
384                                              acpi_memory_device_notify, NULL);
385         /* continue */
386         return AE_OK;
387 }
388
389 static acpi_status
390 acpi_memory_deregister_notify_handler(acpi_handle handle,
391                                       u32 level, void *ctxt, void **retv)
392 {
393         acpi_status status;
394
395         status = is_memory_device(handle);
396         if (ACPI_FAILURE(status))
397                 return AE_OK;   /* continue */
398
399         status = acpi_remove_notify_handler(handle,
400                                             ACPI_SYSTEM_NOTIFY,
401                                             acpi_memory_device_notify);
402
403         return AE_OK;   /* continue */
404 }
405
406 static const struct acpi_device_id memory_device_ids[] = {
407         {ACPI_MEMORY_DEVICE_HID, 0},
408         {"", 0},
409 };
410 MODULE_DEVICE_TABLE(acpi, memory_device_ids);
411
412 static struct acpi_driver xen_acpi_memory_device_driver = {
413         .name = "acpi_memhotplug",
414         .class = ACPI_MEMORY_DEVICE_CLASS,
415         .ids = memory_device_ids,
416         .ops = {
417                 .add = xen_acpi_memory_device_add,
418                 .remove = xen_acpi_memory_device_remove,
419                 },
420 };
421
422 static int __init xen_acpi_memory_device_init(void)
423 {
424         int result;
425         acpi_status status;
426
427         if (!xen_initial_domain())
428                 return -ENODEV;
429
430         /* unregister the stub which only used to reserve driver space */
431         xen_stub_memory_device_exit();
432
433         result = acpi_bus_register_driver(&xen_acpi_memory_device_driver);
434         if (result < 0) {
435                 xen_stub_memory_device_init();
436                 return -ENODEV;
437         }
438
439         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
440                                      ACPI_UINT32_MAX,
441                                      acpi_memory_register_notify_handler,
442                                      NULL, NULL, NULL);
443
444         if (ACPI_FAILURE(status)) {
445                 pr_warn(PREFIX "walk_namespace failed\n");
446                 acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
447                 xen_stub_memory_device_init();
448                 return -ENODEV;
449         }
450
451         acpi_hotmem_initialized = true;
452         return 0;
453 }
454
455 static void __exit xen_acpi_memory_device_exit(void)
456 {
457         acpi_status status;
458
459         if (!xen_initial_domain())
460                 return;
461
462         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
463                                      ACPI_UINT32_MAX,
464                                      acpi_memory_deregister_notify_handler,
465                                      NULL, NULL, NULL);
466         if (ACPI_FAILURE(status))
467                 pr_warn(PREFIX "walk_namespace failed\n");
468
469         acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
470
471         /*
472          * stub reserve space again to prevent any chance of native
473          * driver loading.
474          */
475         xen_stub_memory_device_init();
476         return;
477 }
478
479 module_init(xen_acpi_memory_device_init);
480 module_exit(xen_acpi_memory_device_exit);
481 ACPI_MODULE_NAME("xen-acpi-memhotplug");
482 MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
483 MODULE_DESCRIPTION("Xen Hotplug Mem Driver");
484 MODULE_LICENSE("GPL");