USB: iowarrior: fix NULL-deref in write
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / xen / xen-acpi-cpuhotplug.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 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/cpu.h>
23 #include <linux/acpi.h>
24 #include <linux/uaccess.h>
25 #include <acpi/acpi_bus.h>
26 #include <acpi/acpi_drivers.h>
27 #include <acpi/processor.h>
28
29 #include <xen/acpi.h>
30 #include <xen/interface/platform.h>
31 #include <asm/xen/hypercall.h>
32
33 #define PREFIX "ACPI:xen_cpu_hotplug:"
34
35 #define INSTALL_NOTIFY_HANDLER 0
36 #define UNINSTALL_NOTIFY_HANDLER 1
37
38 static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr);
39
40 /* --------------------------------------------------------------------------
41 Driver Interface
42 -------------------------------------------------------------------------- */
43
44 static int xen_acpi_processor_enable(struct acpi_device *device)
45 {
46 acpi_status status = 0;
47 unsigned long long value;
48 union acpi_object object = { 0 };
49 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
50 struct acpi_processor *pr;
51
52 pr = acpi_driver_data(device);
53 if (!pr) {
54 pr_err(PREFIX "Cannot find driver data\n");
55 return -EINVAL;
56 }
57
58 if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
59 /* Declared with "Processor" statement; match ProcessorID */
60 status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
61 if (ACPI_FAILURE(status)) {
62 pr_err(PREFIX "Evaluating processor object\n");
63 return -ENODEV;
64 }
65
66 pr->acpi_id = object.processor.proc_id;
67 } else {
68 /* Declared with "Device" statement; match _UID */
69 status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
70 NULL, &value);
71 if (ACPI_FAILURE(status)) {
72 pr_err(PREFIX "Evaluating processor _UID\n");
73 return -ENODEV;
74 }
75
76 pr->acpi_id = value;
77 }
78
79 pr->id = xen_pcpu_id(pr->acpi_id);
80
81 if ((int)pr->id < 0)
82 /* This cpu is not presented at hypervisor, try to hotadd it */
83 if (ACPI_FAILURE(xen_acpi_cpu_hotadd(pr))) {
84 pr_err(PREFIX "Hotadd CPU (acpi_id = %d) failed.\n",
85 pr->acpi_id);
86 return -ENODEV;
87 }
88
89 return 0;
90 }
91
92 static int __cpuinit xen_acpi_processor_add(struct acpi_device *device)
93 {
94 int ret;
95 struct acpi_processor *pr;
96
97 if (!device)
98 return -EINVAL;
99
100 pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
101 if (!pr)
102 return -ENOMEM;
103
104 pr->handle = device->handle;
105 strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
106 strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
107 device->driver_data = pr;
108
109 ret = xen_acpi_processor_enable(device);
110 if (ret)
111 pr_err(PREFIX "Error when enabling Xen processor\n");
112
113 return ret;
114 }
115
116 static int xen_acpi_processor_remove(struct acpi_device *device)
117 {
118 struct acpi_processor *pr;
119
120 if (!device)
121 return -EINVAL;
122
123 pr = acpi_driver_data(device);
124 if (!pr)
125 return -EINVAL;
126
127 kfree(pr);
128 return 0;
129 }
130
131 /*--------------------------------------------------------------
132 Acpi processor hotplug support
133 --------------------------------------------------------------*/
134
135 static int is_processor_present(acpi_handle handle)
136 {
137 acpi_status status;
138 unsigned long long sta = 0;
139
140
141 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
142
143 if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
144 return 1;
145
146 /*
147 * _STA is mandatory for a processor that supports hot plug
148 */
149 if (status == AE_NOT_FOUND)
150 pr_info(PREFIX "Processor does not support hot plug\n");
151 else
152 pr_info(PREFIX "Processor Device is not present");
153 return 0;
154 }
155
156 static int xen_apic_id(acpi_handle handle)
157 {
158 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
159 union acpi_object *obj;
160 struct acpi_madt_local_apic *lapic;
161 int apic_id;
162
163 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
164 return -EINVAL;
165
166 if (!buffer.length || !buffer.pointer)
167 return -EINVAL;
168
169 obj = buffer.pointer;
170 if (obj->type != ACPI_TYPE_BUFFER ||
171 obj->buffer.length < sizeof(*lapic)) {
172 kfree(buffer.pointer);
173 return -EINVAL;
174 }
175
176 lapic = (struct acpi_madt_local_apic *)obj->buffer.pointer;
177
178 if (lapic->header.type != ACPI_MADT_TYPE_LOCAL_APIC ||
179 !(lapic->lapic_flags & ACPI_MADT_ENABLED)) {
180 kfree(buffer.pointer);
181 return -EINVAL;
182 }
183
184 apic_id = (uint32_t)lapic->id;
185 kfree(buffer.pointer);
186 buffer.length = ACPI_ALLOCATE_BUFFER;
187 buffer.pointer = NULL;
188
189 return apic_id;
190 }
191
192 static int xen_hotadd_cpu(struct acpi_processor *pr)
193 {
194 int cpu_id, apic_id, pxm;
195 struct xen_platform_op op;
196
197 apic_id = xen_apic_id(pr->handle);
198 if (apic_id < 0) {
199 pr_err(PREFIX "Failed to get apic_id for acpi_id %d\n",
200 pr->acpi_id);
201 return -ENODEV;
202 }
203
204 pxm = xen_acpi_get_pxm(pr->handle);
205 if (pxm < 0) {
206 pr_err(PREFIX "Failed to get _PXM for acpi_id %d\n",
207 pr->acpi_id);
208 return pxm;
209 }
210
211 op.cmd = XENPF_cpu_hotadd;
212 op.u.cpu_add.apic_id = apic_id;
213 op.u.cpu_add.acpi_id = pr->acpi_id;
214 op.u.cpu_add.pxm = pxm;
215
216 cpu_id = HYPERVISOR_dom0_op(&op);
217 if (cpu_id < 0)
218 pr_err(PREFIX "Failed to hotadd CPU for acpi_id %d\n",
219 pr->acpi_id);
220
221 return cpu_id;
222 }
223
224 static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr)
225 {
226 if (!is_processor_present(pr->handle))
227 return AE_ERROR;
228
229 pr->id = xen_hotadd_cpu(pr);
230 if ((int)pr->id < 0)
231 return AE_ERROR;
232
233 /*
234 * Sync with Xen hypervisor, providing new /sys/.../xen_cpuX
235 * interface after cpu hotadded.
236 */
237 xen_pcpu_hotplug_sync();
238
239 return AE_OK;
240 }
241
242 static int acpi_processor_device_remove(struct acpi_device *device)
243 {
244 pr_debug(PREFIX "Xen does not support CPU hotremove\n");
245
246 return -ENOSYS;
247 }
248
249 static void acpi_processor_hotplug_notify(acpi_handle handle,
250 u32 event, void *data)
251 {
252 struct acpi_processor *pr;
253 struct acpi_device *device = NULL;
254 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
255 int result;
256
257 acpi_scan_lock_acquire();
258
259 switch (event) {
260 case ACPI_NOTIFY_BUS_CHECK:
261 case ACPI_NOTIFY_DEVICE_CHECK:
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
263 "Processor driver received %s event\n",
264 (event == ACPI_NOTIFY_BUS_CHECK) ?
265 "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
266
267 if (!is_processor_present(handle))
268 break;
269
270 if (!acpi_bus_get_device(handle, &device))
271 break;
272
273 result = acpi_bus_scan(handle);
274 if (result) {
275 pr_err(PREFIX "Unable to add the device\n");
276 break;
277 }
278 result = acpi_bus_get_device(handle, &device);
279 if (result) {
280 pr_err(PREFIX "Missing device object\n");
281 break;
282 }
283 ost_code = ACPI_OST_SC_SUCCESS;
284 break;
285
286 case ACPI_NOTIFY_EJECT_REQUEST:
287 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
288 "received ACPI_NOTIFY_EJECT_REQUEST\n"));
289
290 if (acpi_bus_get_device(handle, &device)) {
291 pr_err(PREFIX "Device don't exist, dropping EJECT\n");
292 break;
293 }
294 pr = acpi_driver_data(device);
295 if (!pr) {
296 pr_err(PREFIX "Driver data is NULL, dropping EJECT\n");
297 break;
298 }
299
300 /*
301 * TBD: implement acpi_processor_device_remove if Xen support
302 * CPU hotremove in the future.
303 */
304 acpi_processor_device_remove(device);
305 break;
306
307 default:
308 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
309 "Unsupported event [0x%x]\n", event));
310
311 /* non-hotplug event; possibly handled by other handler */
312 goto out;
313 }
314
315 (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
316
317 out:
318 acpi_scan_lock_release();
319 }
320
321 static acpi_status is_processor_device(acpi_handle handle)
322 {
323 struct acpi_device_info *info;
324 char *hid;
325 acpi_status status;
326
327 status = acpi_get_object_info(handle, &info);
328 if (ACPI_FAILURE(status))
329 return status;
330
331 if (info->type == ACPI_TYPE_PROCESSOR) {
332 kfree(info);
333 return AE_OK; /* found a processor object */
334 }
335
336 if (!(info->valid & ACPI_VALID_HID)) {
337 kfree(info);
338 return AE_ERROR;
339 }
340
341 hid = info->hardware_id.string;
342 if ((hid == NULL) || strcmp(hid, ACPI_PROCESSOR_DEVICE_HID)) {
343 kfree(info);
344 return AE_ERROR;
345 }
346
347 kfree(info);
348 return AE_OK; /* found a processor device object */
349 }
350
351 static acpi_status
352 processor_walk_namespace_cb(acpi_handle handle,
353 u32 lvl, void *context, void **rv)
354 {
355 acpi_status status;
356 int *action = context;
357
358 status = is_processor_device(handle);
359 if (ACPI_FAILURE(status))
360 return AE_OK; /* not a processor; continue to walk */
361
362 switch (*action) {
363 case INSTALL_NOTIFY_HANDLER:
364 acpi_install_notify_handler(handle,
365 ACPI_SYSTEM_NOTIFY,
366 acpi_processor_hotplug_notify,
367 NULL);
368 break;
369 case UNINSTALL_NOTIFY_HANDLER:
370 acpi_remove_notify_handler(handle,
371 ACPI_SYSTEM_NOTIFY,
372 acpi_processor_hotplug_notify);
373 break;
374 default:
375 break;
376 }
377
378 /* found a processor; skip walking underneath */
379 return AE_CTRL_DEPTH;
380 }
381
382 static
383 void acpi_processor_install_hotplug_notify(void)
384 {
385 int action = INSTALL_NOTIFY_HANDLER;
386 acpi_walk_namespace(ACPI_TYPE_ANY,
387 ACPI_ROOT_OBJECT,
388 ACPI_UINT32_MAX,
389 processor_walk_namespace_cb, NULL, &action, NULL);
390 }
391
392 static
393 void acpi_processor_uninstall_hotplug_notify(void)
394 {
395 int action = UNINSTALL_NOTIFY_HANDLER;
396 acpi_walk_namespace(ACPI_TYPE_ANY,
397 ACPI_ROOT_OBJECT,
398 ACPI_UINT32_MAX,
399 processor_walk_namespace_cb, NULL, &action, NULL);
400 }
401
402 static const struct acpi_device_id processor_device_ids[] = {
403 {ACPI_PROCESSOR_OBJECT_HID, 0},
404 {ACPI_PROCESSOR_DEVICE_HID, 0},
405 {"", 0},
406 };
407 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
408
409 static struct acpi_driver xen_acpi_processor_driver = {
410 .name = "processor",
411 .class = ACPI_PROCESSOR_CLASS,
412 .ids = processor_device_ids,
413 .ops = {
414 .add = xen_acpi_processor_add,
415 .remove = xen_acpi_processor_remove,
416 },
417 };
418
419 static int __init xen_acpi_processor_init(void)
420 {
421 int result = 0;
422
423 if (!xen_initial_domain())
424 return -ENODEV;
425
426 /* unregister the stub which only used to reserve driver space */
427 xen_stub_processor_exit();
428
429 result = acpi_bus_register_driver(&xen_acpi_processor_driver);
430 if (result < 0) {
431 xen_stub_processor_init();
432 return result;
433 }
434
435 acpi_processor_install_hotplug_notify();
436 return 0;
437 }
438
439 static void __exit xen_acpi_processor_exit(void)
440 {
441 if (!xen_initial_domain())
442 return;
443
444 acpi_processor_uninstall_hotplug_notify();
445
446 acpi_bus_unregister_driver(&xen_acpi_processor_driver);
447
448 /*
449 * stub reserve space again to prevent any chance of native
450 * driver loading.
451 */
452 xen_stub_processor_init();
453 return;
454 }
455
456 module_init(xen_acpi_processor_init);
457 module_exit(xen_acpi_processor_exit);
458 ACPI_MODULE_NAME("xen-acpi-cpuhotplug");
459 MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
460 MODULE_DESCRIPTION("Xen Hotplug CPU Driver");
461 MODULE_LICENSE("GPL");