ACPI / PM: Common string representations of device power states
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / bus.c
1 /*
2  *  acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
3  *
4  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
5  *
6  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.  See the GNU
16  *  General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23  */
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
30 #include <linux/sched.h>
31 #include <linux/pm.h>
32 #include <linux/device.h>
33 #include <linux/proc_fs.h>
34 #include <linux/acpi.h>
35 #include <linux/slab.h>
36 #ifdef CONFIG_X86
37 #include <asm/mpspec.h>
38 #endif
39 #include <linux/pci.h>
40 #include <acpi/acpi_bus.h>
41 #include <acpi/acpi_drivers.h>
42 #include <acpi/apei.h>
43 #include <linux/dmi.h>
44 #include <linux/suspend.h>
45
46 #include "internal.h"
47
48 #define _COMPONENT              ACPI_BUS_COMPONENT
49 ACPI_MODULE_NAME("bus");
50
51 struct acpi_device *acpi_root;
52 struct proc_dir_entry *acpi_root_dir;
53 EXPORT_SYMBOL(acpi_root_dir);
54
55 #define STRUCT_TO_INT(s)        (*((int*)&s))
56
57
58 #ifdef CONFIG_X86
59 static int set_copy_dsdt(const struct dmi_system_id *id)
60 {
61         printk(KERN_NOTICE "%s detected - "
62                 "force copy of DSDT to local memory\n", id->ident);
63         acpi_gbl_copy_dsdt_locally = 1;
64         return 0;
65 }
66
67 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
68         /*
69          * Invoke DSDT corruption work-around on all Toshiba Satellite.
70          * https://bugzilla.kernel.org/show_bug.cgi?id=14679
71          */
72         {
73          .callback = set_copy_dsdt,
74          .ident = "TOSHIBA Satellite",
75          .matches = {
76                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
77                 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
78                 },
79         },
80         {}
81 };
82 #else
83 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
84         {}
85 };
86 #endif
87
88 /* --------------------------------------------------------------------------
89                                 Device Management
90    -------------------------------------------------------------------------- */
91
92 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
93 {
94         acpi_status status = AE_OK;
95
96
97         if (!device)
98                 return -EINVAL;
99
100         /* TBD: Support fixed-feature devices */
101
102         status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
103         if (ACPI_FAILURE(status) || !*device) {
104                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
105                                   handle));
106                 return -ENODEV;
107         }
108
109         return 0;
110 }
111
112 EXPORT_SYMBOL(acpi_bus_get_device);
113
114 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
115                                        unsigned long long *sta)
116 {
117         acpi_status status;
118
119         status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
120         if (ACPI_SUCCESS(status))
121                 return AE_OK;
122
123         if (status == AE_NOT_FOUND) {
124                 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
125                        ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
126                 return AE_OK;
127         }
128         return status;
129 }
130
131 int acpi_bus_get_status(struct acpi_device *device)
132 {
133         acpi_status status;
134         unsigned long long sta;
135
136         status = acpi_bus_get_status_handle(device->handle, &sta);
137         if (ACPI_FAILURE(status))
138                 return -ENODEV;
139
140         STRUCT_TO_INT(device->status) = (int) sta;
141
142         if (device->status.functional && !device->status.present) {
143                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
144                        "functional but not present;\n",
145                         device->pnp.bus_id,
146                         (u32) STRUCT_TO_INT(device->status)));
147         }
148
149         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
150                           device->pnp.bus_id,
151                           (u32) STRUCT_TO_INT(device->status)));
152         return 0;
153 }
154 EXPORT_SYMBOL(acpi_bus_get_status);
155
156 void acpi_bus_private_data_handler(acpi_handle handle,
157                                    void *context)
158 {
159         return;
160 }
161 EXPORT_SYMBOL(acpi_bus_private_data_handler);
162
163 int acpi_bus_get_private_data(acpi_handle handle, void **data)
164 {
165         acpi_status status = AE_OK;
166
167         if (!*data)
168                 return -EINVAL;
169
170         status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
171         if (ACPI_FAILURE(status) || !*data) {
172                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
173                                 handle));
174                 return -ENODEV;
175         }
176
177         return 0;
178 }
179 EXPORT_SYMBOL(acpi_bus_get_private_data);
180
181 /* --------------------------------------------------------------------------
182                                  Power Management
183    -------------------------------------------------------------------------- */
184
185 /**
186  * acpi_power_state_string - String representation of ACPI device power state.
187  * @state: ACPI device power state to return the string representation of.
188  */
189 const char *acpi_power_state_string(int state)
190 {
191         switch (state) {
192         case ACPI_STATE_D0:
193                 return "D0";
194         case ACPI_STATE_D1:
195                 return "D1";
196         case ACPI_STATE_D2:
197                 return "D2";
198         case ACPI_STATE_D3_HOT:
199                 return "D3hot";
200         case ACPI_STATE_D3_COLD:
201                 return "D3";
202         default:
203                 return "(unknown)";
204         }
205 }
206
207 /**
208  * acpi_device_get_power - Get power state of an ACPI device.
209  * @device: Device to get the power state of.
210  * @state: Place to store the power state of the device.
211  *
212  * This function does not update the device's power.state field, but it may
213  * update its parent's power.state field (when the parent's power state is
214  * unknown and the device's power state turns out to be D0).
215  */
216 int acpi_device_get_power(struct acpi_device *device, int *state)
217 {
218         int result = ACPI_STATE_UNKNOWN;
219
220         if (!device || !state)
221                 return -EINVAL;
222
223         if (!device->flags.power_manageable) {
224                 /* TBD: Non-recursive algorithm for walking up hierarchy. */
225                 *state = device->parent ?
226                         device->parent->power.state : ACPI_STATE_D0;
227                 goto out;
228         }
229
230         /*
231          * Get the device's power state either directly (via _PSC) or
232          * indirectly (via power resources).
233          */
234         if (device->power.flags.explicit_get) {
235                 unsigned long long psc;
236                 acpi_status status = acpi_evaluate_integer(device->handle,
237                                                            "_PSC", NULL, &psc);
238                 if (ACPI_FAILURE(status))
239                         return -ENODEV;
240
241                 result = psc;
242         }
243         /* The test below covers ACPI_STATE_UNKNOWN too. */
244         if (result <= ACPI_STATE_D2) {
245           ; /* Do nothing. */
246         } else if (device->power.flags.power_resources) {
247                 int error = acpi_power_get_inferred_state(device, &result);
248                 if (error)
249                         return error;
250         } else if (result == ACPI_STATE_D3_HOT) {
251                 result = ACPI_STATE_D3;
252         }
253
254         /*
255          * If we were unsure about the device parent's power state up to this
256          * point, the fact that the device is in D0 implies that the parent has
257          * to be in D0 too.
258          */
259         if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN
260             && result == ACPI_STATE_D0)
261                 device->parent->power.state = ACPI_STATE_D0;
262
263         *state = result;
264
265  out:
266         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
267                           device->pnp.bus_id, acpi_power_state_string(*state)));
268
269         return 0;
270 }
271
272
273 /**
274  * acpi_device_set_power - Set power state of an ACPI device.
275  * @device: Device to set the power state of.
276  * @state: New power state to set.
277  *
278  * Callers must ensure that the device is power manageable before using this
279  * function.
280  */
281 int acpi_device_set_power(struct acpi_device *device, int state)
282 {
283         int result = 0;
284         acpi_status status = AE_OK;
285         char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
286         bool cut_power = false;
287
288         if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
289                 return -EINVAL;
290
291         /* Make sure this is a valid target state */
292
293         if (state == device->power.state) {
294                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n",
295                                   acpi_power_state_string(state)));
296                 return 0;
297         }
298
299         if (!device->power.states[state].flags.valid) {
300                 printk(KERN_WARNING PREFIX "Device does not support %s\n",
301                        acpi_power_state_string(state));
302                 return -ENODEV;
303         }
304         if (device->parent && (state < device->parent->power.state)) {
305                 printk(KERN_WARNING PREFIX
306                               "Cannot set device to a higher-powered"
307                               " state than parent\n");
308                 return -ENODEV;
309         }
310
311         /* For D3cold we should first transition into D3hot. */
312         if (state == ACPI_STATE_D3_COLD
313             && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) {
314                 state = ACPI_STATE_D3_HOT;
315                 object_name[3] = '3';
316                 cut_power = true;
317         }
318
319         /*
320          * Transition Power
321          * ----------------
322          * On transitions to a high-powered state we first apply power (via
323          * power resources) then evalute _PSx.  Conversly for transitions to
324          * a lower-powered state.
325          */
326         if (state < device->power.state) {
327                 if (device->power.state >= ACPI_STATE_D3_HOT &&
328                     state != ACPI_STATE_D0) {
329                         printk(KERN_WARNING PREFIX
330                               "Cannot transition to non-D0 state from D3\n");
331                         return -ENODEV;
332                 }
333                 if (device->power.flags.power_resources) {
334                         result = acpi_power_transition(device, state);
335                         if (result)
336                                 goto end;
337                 }
338                 if (device->power.states[state].flags.explicit_set) {
339                         status = acpi_evaluate_object(device->handle,
340                                                       object_name, NULL, NULL);
341                         if (ACPI_FAILURE(status)) {
342                                 result = -ENODEV;
343                                 goto end;
344                         }
345                 }
346         } else {
347                 if (device->power.states[state].flags.explicit_set) {
348                         status = acpi_evaluate_object(device->handle,
349                                                       object_name, NULL, NULL);
350                         if (ACPI_FAILURE(status)) {
351                                 result = -ENODEV;
352                                 goto end;
353                         }
354                 }
355                 if (device->power.flags.power_resources) {
356                         result = acpi_power_transition(device, state);
357                         if (result)
358                                 goto end;
359                 }
360         }
361
362         if (cut_power)
363                 result = acpi_power_transition(device, ACPI_STATE_D3_COLD);
364
365       end:
366         if (result)
367                 printk(KERN_WARNING PREFIX
368                               "Device [%s] failed to transition to %s\n",
369                               device->pnp.bus_id,
370                               acpi_power_state_string(state));
371         else {
372                 device->power.state = state;
373                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
374                                   "Device [%s] transitioned to %s\n",
375                                   device->pnp.bus_id,
376                                   acpi_power_state_string(state)));
377         }
378
379         return result;
380 }
381 EXPORT_SYMBOL(acpi_device_set_power);
382
383
384 int acpi_bus_set_power(acpi_handle handle, int state)
385 {
386         struct acpi_device *device;
387         int result;
388
389         result = acpi_bus_get_device(handle, &device);
390         if (result)
391                 return result;
392
393         if (!device->flags.power_manageable) {
394                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
395                                 "Device [%s] is not power manageable\n",
396                                 dev_name(&device->dev)));
397                 return -ENODEV;
398         }
399
400         return acpi_device_set_power(device, state);
401 }
402 EXPORT_SYMBOL(acpi_bus_set_power);
403
404
405 int acpi_bus_init_power(struct acpi_device *device)
406 {
407         int state;
408         int result;
409
410         if (!device)
411                 return -EINVAL;
412
413         device->power.state = ACPI_STATE_UNKNOWN;
414
415         result = acpi_device_get_power(device, &state);
416         if (result)
417                 return result;
418
419         if (device->power.flags.power_resources)
420                 result = acpi_power_on_resources(device, state);
421
422         if (!result)
423                 device->power.state = state;
424
425         return result;
426 }
427
428
429 int acpi_bus_update_power(acpi_handle handle, int *state_p)
430 {
431         struct acpi_device *device;
432         int state;
433         int result;
434
435         result = acpi_bus_get_device(handle, &device);
436         if (result)
437                 return result;
438
439         result = acpi_device_get_power(device, &state);
440         if (result)
441                 return result;
442
443         result = acpi_device_set_power(device, state);
444         if (!result && state_p)
445                 *state_p = state;
446
447         return result;
448 }
449 EXPORT_SYMBOL_GPL(acpi_bus_update_power);
450
451
452 bool acpi_bus_power_manageable(acpi_handle handle)
453 {
454         struct acpi_device *device;
455         int result;
456
457         result = acpi_bus_get_device(handle, &device);
458         return result ? false : device->flags.power_manageable;
459 }
460
461 EXPORT_SYMBOL(acpi_bus_power_manageable);
462
463 bool acpi_bus_can_wakeup(acpi_handle handle)
464 {
465         struct acpi_device *device;
466         int result;
467
468         result = acpi_bus_get_device(handle, &device);
469         return result ? false : device->wakeup.flags.valid;
470 }
471
472 EXPORT_SYMBOL(acpi_bus_can_wakeup);
473
474 static void acpi_print_osc_error(acpi_handle handle,
475         struct acpi_osc_context *context, char *error)
476 {
477         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
478         int i;
479
480         if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
481                 printk(KERN_DEBUG "%s\n", error);
482         else {
483                 printk(KERN_DEBUG "%s:%s\n", (char *)buffer.pointer, error);
484                 kfree(buffer.pointer);
485         }
486         printk(KERN_DEBUG"_OSC request data:");
487         for (i = 0; i < context->cap.length; i += sizeof(u32))
488                 printk("%x ", *((u32 *)(context->cap.pointer + i)));
489         printk("\n");
490 }
491
492 static acpi_status acpi_str_to_uuid(char *str, u8 *uuid)
493 {
494         int i;
495         static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
496                 24, 26, 28, 30, 32, 34};
497
498         if (strlen(str) != 36)
499                 return AE_BAD_PARAMETER;
500         for (i = 0; i < 36; i++) {
501                 if (i == 8 || i == 13 || i == 18 || i == 23) {
502                         if (str[i] != '-')
503                                 return AE_BAD_PARAMETER;
504                 } else if (!isxdigit(str[i]))
505                         return AE_BAD_PARAMETER;
506         }
507         for (i = 0; i < 16; i++) {
508                 uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
509                 uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
510         }
511         return AE_OK;
512 }
513
514 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
515 {
516         acpi_status status;
517         struct acpi_object_list input;
518         union acpi_object in_params[4];
519         union acpi_object *out_obj;
520         u8 uuid[16];
521         u32 errors;
522         struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
523
524         if (!context)
525                 return AE_ERROR;
526         if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
527                 return AE_ERROR;
528         context->ret.length = ACPI_ALLOCATE_BUFFER;
529         context->ret.pointer = NULL;
530
531         /* Setting up input parameters */
532         input.count = 4;
533         input.pointer = in_params;
534         in_params[0].type               = ACPI_TYPE_BUFFER;
535         in_params[0].buffer.length      = 16;
536         in_params[0].buffer.pointer     = uuid;
537         in_params[1].type               = ACPI_TYPE_INTEGER;
538         in_params[1].integer.value      = context->rev;
539         in_params[2].type               = ACPI_TYPE_INTEGER;
540         in_params[2].integer.value      = context->cap.length/sizeof(u32);
541         in_params[3].type               = ACPI_TYPE_BUFFER;
542         in_params[3].buffer.length      = context->cap.length;
543         in_params[3].buffer.pointer     = context->cap.pointer;
544
545         status = acpi_evaluate_object(handle, "_OSC", &input, &output);
546         if (ACPI_FAILURE(status))
547                 return status;
548
549         if (!output.length)
550                 return AE_NULL_OBJECT;
551
552         out_obj = output.pointer;
553         if (out_obj->type != ACPI_TYPE_BUFFER
554                 || out_obj->buffer.length != context->cap.length) {
555                 acpi_print_osc_error(handle, context,
556                         "_OSC evaluation returned wrong type");
557                 status = AE_TYPE;
558                 goto out_kfree;
559         }
560         /* Need to ignore the bit0 in result code */
561         errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
562         if (errors) {
563                 if (errors & OSC_REQUEST_ERROR)
564                         acpi_print_osc_error(handle, context,
565                                 "_OSC request failed");
566                 if (errors & OSC_INVALID_UUID_ERROR)
567                         acpi_print_osc_error(handle, context,
568                                 "_OSC invalid UUID");
569                 if (errors & OSC_INVALID_REVISION_ERROR)
570                         acpi_print_osc_error(handle, context,
571                                 "_OSC invalid revision");
572                 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
573                         if (((u32 *)context->cap.pointer)[OSC_QUERY_TYPE]
574                             & OSC_QUERY_ENABLE)
575                                 goto out_success;
576                         status = AE_SUPPORT;
577                         goto out_kfree;
578                 }
579                 status = AE_ERROR;
580                 goto out_kfree;
581         }
582 out_success:
583         context->ret.length = out_obj->buffer.length;
584         context->ret.pointer = kmalloc(context->ret.length, GFP_KERNEL);
585         if (!context->ret.pointer) {
586                 status =  AE_NO_MEMORY;
587                 goto out_kfree;
588         }
589         memcpy(context->ret.pointer, out_obj->buffer.pointer,
590                 context->ret.length);
591         status =  AE_OK;
592
593 out_kfree:
594         kfree(output.pointer);
595         if (status != AE_OK)
596                 context->ret.pointer = NULL;
597         return status;
598 }
599 EXPORT_SYMBOL(acpi_run_osc);
600
601 bool osc_sb_apei_support_acked;
602 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
603 static void acpi_bus_osc_support(void)
604 {
605         u32 capbuf[2];
606         struct acpi_osc_context context = {
607                 .uuid_str = sb_uuid_str,
608                 .rev = 1,
609                 .cap.length = 8,
610                 .cap.pointer = capbuf,
611         };
612         acpi_handle handle;
613
614         capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
615         capbuf[OSC_SUPPORT_TYPE] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
616 #if defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) ||\
617                         defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
618         capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PAD_SUPPORT;
619 #endif
620
621 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
622         capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PPC_OST_SUPPORT;
623 #endif
624
625 #ifdef ACPI_HOTPLUG_OST
626         capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_HOTPLUG_OST_SUPPORT;
627 #endif
628
629         if (!ghes_disable)
630                 capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_APEI_SUPPORT;
631         if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
632                 return;
633         if (ACPI_SUCCESS(acpi_run_osc(handle, &context))) {
634                 u32 *capbuf_ret = context.ret.pointer;
635                 if (context.ret.length > OSC_SUPPORT_TYPE)
636                         osc_sb_apei_support_acked =
637                                 capbuf_ret[OSC_SUPPORT_TYPE] & OSC_SB_APEI_SUPPORT;
638                 kfree(context.ret.pointer);
639         }
640         /* do we need to check other returned cap? Sounds no */
641 }
642
643 /* --------------------------------------------------------------------------
644                                 Event Management
645    -------------------------------------------------------------------------- */
646
647 #ifdef CONFIG_ACPI_PROC_EVENT
648 static DEFINE_SPINLOCK(acpi_bus_event_lock);
649
650 LIST_HEAD(acpi_bus_event_list);
651 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
652
653 extern int event_is_open;
654
655 int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
656 {
657         struct acpi_bus_event *event;
658         unsigned long flags = 0;
659
660         /* drop event on the floor if no one's listening */
661         if (!event_is_open)
662                 return 0;
663
664         event = kzalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
665         if (!event)
666                 return -ENOMEM;
667
668         strcpy(event->device_class, device_class);
669         strcpy(event->bus_id, bus_id);
670         event->type = type;
671         event->data = data;
672
673         spin_lock_irqsave(&acpi_bus_event_lock, flags);
674         list_add_tail(&event->node, &acpi_bus_event_list);
675         spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
676
677         wake_up_interruptible(&acpi_bus_event_queue);
678
679         return 0;
680
681 }
682
683 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
684
685 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
686 {
687         if (!device)
688                 return -EINVAL;
689         return acpi_bus_generate_proc_event4(device->pnp.device_class,
690                                              device->pnp.bus_id, type, data);
691 }
692
693 EXPORT_SYMBOL(acpi_bus_generate_proc_event);
694
695 int acpi_bus_receive_event(struct acpi_bus_event *event)
696 {
697         unsigned long flags = 0;
698         struct acpi_bus_event *entry = NULL;
699
700         DECLARE_WAITQUEUE(wait, current);
701
702
703         if (!event)
704                 return -EINVAL;
705
706         if (list_empty(&acpi_bus_event_list)) {
707
708                 set_current_state(TASK_INTERRUPTIBLE);
709                 add_wait_queue(&acpi_bus_event_queue, &wait);
710
711                 if (list_empty(&acpi_bus_event_list))
712                         schedule();
713
714                 remove_wait_queue(&acpi_bus_event_queue, &wait);
715                 set_current_state(TASK_RUNNING);
716
717                 if (signal_pending(current))
718                         return -ERESTARTSYS;
719         }
720
721         spin_lock_irqsave(&acpi_bus_event_lock, flags);
722         if (!list_empty(&acpi_bus_event_list)) {
723                 entry = list_entry(acpi_bus_event_list.next,
724                                    struct acpi_bus_event, node);
725                 list_del(&entry->node);
726         }
727         spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
728
729         if (!entry)
730                 return -ENODEV;
731
732         memcpy(event, entry, sizeof(struct acpi_bus_event));
733
734         kfree(entry);
735
736         return 0;
737 }
738
739 #endif  /* CONFIG_ACPI_PROC_EVENT */
740
741 /* --------------------------------------------------------------------------
742                              Notification Handling
743    -------------------------------------------------------------------------- */
744
745 static void acpi_bus_check_device(acpi_handle handle)
746 {
747         struct acpi_device *device;
748         acpi_status status;
749         struct acpi_device_status old_status;
750
751         if (acpi_bus_get_device(handle, &device))
752                 return;
753         if (!device)
754                 return;
755
756         old_status = device->status;
757
758         /*
759          * Make sure this device's parent is present before we go about
760          * messing with the device.
761          */
762         if (device->parent && !device->parent->status.present) {
763                 device->status = device->parent->status;
764                 return;
765         }
766
767         status = acpi_bus_get_status(device);
768         if (ACPI_FAILURE(status))
769                 return;
770
771         if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
772                 return;
773
774         /*
775          * Device Insertion/Removal
776          */
777         if ((device->status.present) && !(old_status.present)) {
778                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
779                 /* TBD: Handle device insertion */
780         } else if (!(device->status.present) && (old_status.present)) {
781                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
782                 /* TBD: Handle device removal */
783         }
784 }
785
786 static void acpi_bus_check_scope(acpi_handle handle)
787 {
788         /* Status Change? */
789         acpi_bus_check_device(handle);
790
791         /*
792          * TBD: Enumerate child devices within this device's scope and
793          *       run acpi_bus_check_device()'s on them.
794          */
795 }
796
797 static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list);
798 int register_acpi_bus_notifier(struct notifier_block *nb)
799 {
800         return blocking_notifier_chain_register(&acpi_bus_notify_list, nb);
801 }
802 EXPORT_SYMBOL_GPL(register_acpi_bus_notifier);
803
804 void unregister_acpi_bus_notifier(struct notifier_block *nb)
805 {
806         blocking_notifier_chain_unregister(&acpi_bus_notify_list, nb);
807 }
808 EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier);
809
810 /**
811  * acpi_bus_notify
812  * ---------------
813  * Callback for all 'system-level' device notifications (values 0x00-0x7F).
814  */
815 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
816 {
817         struct acpi_device *device = NULL;
818         struct acpi_driver *driver;
819
820         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
821                           type, handle));
822
823         blocking_notifier_call_chain(&acpi_bus_notify_list,
824                 type, (void *)handle);
825
826         switch (type) {
827
828         case ACPI_NOTIFY_BUS_CHECK:
829                 acpi_bus_check_scope(handle);
830                 /*
831                  * TBD: We'll need to outsource certain events to non-ACPI
832                  *      drivers via the device manager (device.c).
833                  */
834                 break;
835
836         case ACPI_NOTIFY_DEVICE_CHECK:
837                 acpi_bus_check_device(handle);
838                 /*
839                  * TBD: We'll need to outsource certain events to non-ACPI
840                  *      drivers via the device manager (device.c).
841                  */
842                 break;
843
844         case ACPI_NOTIFY_DEVICE_WAKE:
845                 /* TBD */
846                 break;
847
848         case ACPI_NOTIFY_EJECT_REQUEST:
849                 /* TBD */
850                 break;
851
852         case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
853                 /* TBD: Exactly what does 'light' mean? */
854                 break;
855
856         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
857                 /* TBD */
858                 break;
859
860         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
861                 /* TBD */
862                 break;
863
864         case ACPI_NOTIFY_POWER_FAULT:
865                 /* TBD */
866                 break;
867
868         default:
869                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
870                                   "Received unknown/unsupported notification [%08x]\n",
871                                   type));
872                 break;
873         }
874
875         acpi_bus_get_device(handle, &device);
876         if (device) {
877                 driver = device->driver;
878                 if (driver && driver->ops.notify &&
879                     (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
880                         driver->ops.notify(device, type);
881         }
882 }
883
884 /* --------------------------------------------------------------------------
885                              Initialization/Cleanup
886    -------------------------------------------------------------------------- */
887
888 static int __init acpi_bus_init_irq(void)
889 {
890         acpi_status status = AE_OK;
891         union acpi_object arg = { ACPI_TYPE_INTEGER };
892         struct acpi_object_list arg_list = { 1, &arg };
893         char *message = NULL;
894
895
896         /*
897          * Let the system know what interrupt model we are using by
898          * evaluating the \_PIC object, if exists.
899          */
900
901         switch (acpi_irq_model) {
902         case ACPI_IRQ_MODEL_PIC:
903                 message = "PIC";
904                 break;
905         case ACPI_IRQ_MODEL_IOAPIC:
906                 message = "IOAPIC";
907                 break;
908         case ACPI_IRQ_MODEL_IOSAPIC:
909                 message = "IOSAPIC";
910                 break;
911         case ACPI_IRQ_MODEL_PLATFORM:
912                 message = "platform specific model";
913                 break;
914         default:
915                 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
916                 return -ENODEV;
917         }
918
919         printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
920
921         arg.integer.value = acpi_irq_model;
922
923         status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
924         if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
925                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
926                 return -ENODEV;
927         }
928
929         return 0;
930 }
931
932 u8 acpi_gbl_permanent_mmap;
933
934
935 void __init acpi_early_init(void)
936 {
937         acpi_status status = AE_OK;
938
939         if (acpi_disabled)
940                 return;
941
942         printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
943
944         /* enable workarounds, unless strict ACPI spec. compliance */
945         if (!acpi_strict)
946                 acpi_gbl_enable_interpreter_slack = TRUE;
947
948         acpi_gbl_permanent_mmap = 1;
949
950         /*
951          * If the machine falls into the DMI check table,
952          * DSDT will be copied to memory
953          */
954         dmi_check_system(dsdt_dmi_table);
955
956         status = acpi_reallocate_root_table();
957         if (ACPI_FAILURE(status)) {
958                 printk(KERN_ERR PREFIX
959                        "Unable to reallocate ACPI tables\n");
960                 goto error0;
961         }
962
963         status = acpi_initialize_subsystem();
964         if (ACPI_FAILURE(status)) {
965                 printk(KERN_ERR PREFIX
966                        "Unable to initialize the ACPI Interpreter\n");
967                 goto error0;
968         }
969
970         status = acpi_load_tables();
971         if (ACPI_FAILURE(status)) {
972                 printk(KERN_ERR PREFIX
973                        "Unable to load the System Description Tables\n");
974                 goto error0;
975         }
976
977 #ifdef CONFIG_X86
978         if (!acpi_ioapic) {
979                 /* compatible (0) means level (3) */
980                 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
981                         acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
982                         acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
983                 }
984                 /* Set PIC-mode SCI trigger type */
985                 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
986                                          (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
987         } else {
988                 /*
989                  * now that acpi_gbl_FADT is initialized,
990                  * update it with result from INT_SRC_OVR parsing
991                  */
992                 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
993         }
994 #endif
995
996         status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
997         if (ACPI_FAILURE(status)) {
998                 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
999                 goto error0;
1000         }
1001
1002         return;
1003
1004       error0:
1005         disable_acpi();
1006         return;
1007 }
1008
1009 static int __init acpi_bus_init(void)
1010 {
1011         int result = 0;
1012         acpi_status status = AE_OK;
1013         extern acpi_status acpi_os_initialize1(void);
1014
1015         acpi_os_initialize1();
1016
1017         status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1018         if (ACPI_FAILURE(status)) {
1019                 printk(KERN_ERR PREFIX
1020                        "Unable to start the ACPI Interpreter\n");
1021                 goto error1;
1022         }
1023
1024         /*
1025          * ACPI 2.0 requires the EC driver to be loaded and work before
1026          * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
1027          * is called).
1028          *
1029          * This is accomplished by looking for the ECDT table, and getting
1030          * the EC parameters out of that.
1031          */
1032         status = acpi_ec_ecdt_probe();
1033         /* Ignore result. Not having an ECDT is not fatal. */
1034
1035         status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1036         if (ACPI_FAILURE(status)) {
1037                 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
1038                 goto error1;
1039         }
1040
1041         /*
1042          * _OSC method may exist in module level code,
1043          * so it must be run after ACPI_FULL_INITIALIZATION
1044          */
1045         acpi_bus_osc_support();
1046
1047         /*
1048          * _PDC control method may load dynamic SSDT tables,
1049          * and we need to install the table handler before that.
1050          */
1051         acpi_sysfs_init();
1052
1053         acpi_early_processor_set_pdc();
1054
1055         /*
1056          * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1057          * is necessary to enable it as early as possible.
1058          */
1059         acpi_boot_ec_enable();
1060
1061         printk(KERN_INFO PREFIX "Interpreter enabled\n");
1062
1063         /* Initialize sleep structures */
1064         acpi_sleep_init();
1065
1066         /*
1067          * Get the system interrupt model and evaluate \_PIC.
1068          */
1069         result = acpi_bus_init_irq();
1070         if (result)
1071                 goto error1;
1072
1073         /*
1074          * Register the for all standard device notifications.
1075          */
1076         status =
1077             acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1078                                         &acpi_bus_notify, NULL);
1079         if (ACPI_FAILURE(status)) {
1080                 printk(KERN_ERR PREFIX
1081                        "Unable to register for device notifications\n");
1082                 goto error1;
1083         }
1084
1085         /*
1086          * Create the top ACPI proc directory
1087          */
1088         acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1089
1090         return 0;
1091
1092         /* Mimic structured exception handling */
1093       error1:
1094         acpi_terminate();
1095         return -ENODEV;
1096 }
1097
1098 struct kobject *acpi_kobj;
1099 EXPORT_SYMBOL_GPL(acpi_kobj);
1100
1101 static int __init acpi_init(void)
1102 {
1103         int result;
1104
1105         if (acpi_disabled) {
1106                 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
1107                 return -ENODEV;
1108         }
1109
1110         acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1111         if (!acpi_kobj) {
1112                 printk(KERN_WARNING "%s: kset create error\n", __func__);
1113                 acpi_kobj = NULL;
1114         }
1115
1116         init_acpi_device_notify();
1117         result = acpi_bus_init();
1118         if (result) {
1119                 disable_acpi();
1120                 return result;
1121         }
1122
1123         pci_mmcfg_late_init();
1124         acpi_scan_init();
1125         acpi_ec_init();
1126         acpi_debugfs_init();
1127         acpi_sleep_proc_init();
1128         acpi_wakeup_device_init();
1129         return 0;
1130 }
1131
1132 subsys_initcall(acpi_init);