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