clockevent: sun4i: Fix race condition in the probe code
[platform/adaptation/renesas_rcar/renesas_kernel.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 #include <linux/regulator/machine.h>
37 #ifdef CONFIG_X86
38 #include <asm/mpspec.h>
39 #endif
40 #include <linux/pci.h>
41 #include <acpi/apei.h>
42 #include <linux/dmi.h>
43 #include <linux/suspend.h>
44
45 #include "internal.h"
46
47 #define _COMPONENT              ACPI_BUS_COMPONENT
48 ACPI_MODULE_NAME("bus");
49
50 struct acpi_device *acpi_root;
51 struct proc_dir_entry *acpi_root_dir;
52 EXPORT_SYMBOL(acpi_root_dir);
53
54 #ifdef CONFIG_X86
55 #ifdef CONFIG_ACPI_CUSTOM_DSDT
56 static inline int set_copy_dsdt(const struct dmi_system_id *id)
57 {
58         return 0;
59 }
60 #else
61 static int set_copy_dsdt(const struct dmi_system_id *id)
62 {
63         printk(KERN_NOTICE "%s detected - "
64                 "force copy of DSDT to local memory\n", id->ident);
65         acpi_gbl_copy_dsdt_locally = 1;
66         return 0;
67 }
68 #endif
69
70 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
71         /*
72          * Invoke DSDT corruption work-around on all Toshiba Satellite.
73          * https://bugzilla.kernel.org/show_bug.cgi?id=14679
74          */
75         {
76          .callback = set_copy_dsdt,
77          .ident = "TOSHIBA Satellite",
78          .matches = {
79                 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
80                 DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
81                 },
82         },
83         {}
84 };
85 #else
86 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
87         {}
88 };
89 #endif
90
91 /* --------------------------------------------------------------------------
92                                 Device Management
93    -------------------------------------------------------------------------- */
94
95 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
96                                        unsigned long long *sta)
97 {
98         acpi_status status;
99
100         status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
101         if (ACPI_SUCCESS(status))
102                 return AE_OK;
103
104         if (status == AE_NOT_FOUND) {
105                 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
106                        ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
107                 return AE_OK;
108         }
109         return status;
110 }
111
112 int acpi_bus_get_status(struct acpi_device *device)
113 {
114         acpi_status status;
115         unsigned long long sta;
116
117         status = acpi_bus_get_status_handle(device->handle, &sta);
118         if (ACPI_FAILURE(status))
119                 return -ENODEV;
120
121         acpi_set_device_status(device, sta);
122
123         if (device->status.functional && !device->status.present) {
124                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
125                        "functional but not present;\n",
126                         device->pnp.bus_id, (u32)sta));
127         }
128
129         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
130                           device->pnp.bus_id, (u32)sta));
131         return 0;
132 }
133 EXPORT_SYMBOL(acpi_bus_get_status);
134
135 void acpi_bus_private_data_handler(acpi_handle handle,
136                                    void *context)
137 {
138         return;
139 }
140 EXPORT_SYMBOL(acpi_bus_private_data_handler);
141
142 int acpi_bus_get_private_data(acpi_handle handle, void **data)
143 {
144         acpi_status status;
145
146         if (!*data)
147                 return -EINVAL;
148
149         status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
150         if (ACPI_FAILURE(status) || !*data) {
151                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
152                                 handle));
153                 return -ENODEV;
154         }
155
156         return 0;
157 }
158 EXPORT_SYMBOL(acpi_bus_get_private_data);
159
160 void acpi_bus_no_hotplug(acpi_handle handle)
161 {
162         struct acpi_device *adev = NULL;
163
164         acpi_bus_get_device(handle, &adev);
165         if (adev)
166                 adev->flags.no_hotplug = true;
167 }
168 EXPORT_SYMBOL_GPL(acpi_bus_no_hotplug);
169
170 static void acpi_print_osc_error(acpi_handle handle,
171         struct acpi_osc_context *context, char *error)
172 {
173         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
174         int i;
175
176         if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
177                 printk(KERN_DEBUG "%s\n", error);
178         else {
179                 printk(KERN_DEBUG "%s:%s\n", (char *)buffer.pointer, error);
180                 kfree(buffer.pointer);
181         }
182         printk(KERN_DEBUG"_OSC request data:");
183         for (i = 0; i < context->cap.length; i += sizeof(u32))
184                 printk("%x ", *((u32 *)(context->cap.pointer + i)));
185         printk("\n");
186 }
187
188 acpi_status acpi_str_to_uuid(char *str, u8 *uuid)
189 {
190         int i;
191         static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
192                 24, 26, 28, 30, 32, 34};
193
194         if (strlen(str) != 36)
195                 return AE_BAD_PARAMETER;
196         for (i = 0; i < 36; i++) {
197                 if (i == 8 || i == 13 || i == 18 || i == 23) {
198                         if (str[i] != '-')
199                                 return AE_BAD_PARAMETER;
200                 } else if (!isxdigit(str[i]))
201                         return AE_BAD_PARAMETER;
202         }
203         for (i = 0; i < 16; i++) {
204                 uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
205                 uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
206         }
207         return AE_OK;
208 }
209 EXPORT_SYMBOL_GPL(acpi_str_to_uuid);
210
211 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
212 {
213         acpi_status status;
214         struct acpi_object_list input;
215         union acpi_object in_params[4];
216         union acpi_object *out_obj;
217         u8 uuid[16];
218         u32 errors;
219         struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
220
221         if (!context)
222                 return AE_ERROR;
223         if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
224                 return AE_ERROR;
225         context->ret.length = ACPI_ALLOCATE_BUFFER;
226         context->ret.pointer = NULL;
227
228         /* Setting up input parameters */
229         input.count = 4;
230         input.pointer = in_params;
231         in_params[0].type               = ACPI_TYPE_BUFFER;
232         in_params[0].buffer.length      = 16;
233         in_params[0].buffer.pointer     = uuid;
234         in_params[1].type               = ACPI_TYPE_INTEGER;
235         in_params[1].integer.value      = context->rev;
236         in_params[2].type               = ACPI_TYPE_INTEGER;
237         in_params[2].integer.value      = context->cap.length/sizeof(u32);
238         in_params[3].type               = ACPI_TYPE_BUFFER;
239         in_params[3].buffer.length      = context->cap.length;
240         in_params[3].buffer.pointer     = context->cap.pointer;
241
242         status = acpi_evaluate_object(handle, "_OSC", &input, &output);
243         if (ACPI_FAILURE(status))
244                 return status;
245
246         if (!output.length)
247                 return AE_NULL_OBJECT;
248
249         out_obj = output.pointer;
250         if (out_obj->type != ACPI_TYPE_BUFFER
251                 || out_obj->buffer.length != context->cap.length) {
252                 acpi_print_osc_error(handle, context,
253                         "_OSC evaluation returned wrong type");
254                 status = AE_TYPE;
255                 goto out_kfree;
256         }
257         /* Need to ignore the bit0 in result code */
258         errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
259         if (errors) {
260                 if (errors & OSC_REQUEST_ERROR)
261                         acpi_print_osc_error(handle, context,
262                                 "_OSC request failed");
263                 if (errors & OSC_INVALID_UUID_ERROR)
264                         acpi_print_osc_error(handle, context,
265                                 "_OSC invalid UUID");
266                 if (errors & OSC_INVALID_REVISION_ERROR)
267                         acpi_print_osc_error(handle, context,
268                                 "_OSC invalid revision");
269                 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
270                         if (((u32 *)context->cap.pointer)[OSC_QUERY_DWORD]
271                             & OSC_QUERY_ENABLE)
272                                 goto out_success;
273                         status = AE_SUPPORT;
274                         goto out_kfree;
275                 }
276                 status = AE_ERROR;
277                 goto out_kfree;
278         }
279 out_success:
280         context->ret.length = out_obj->buffer.length;
281         context->ret.pointer = kmemdup(out_obj->buffer.pointer,
282                                        context->ret.length, GFP_KERNEL);
283         if (!context->ret.pointer) {
284                 status =  AE_NO_MEMORY;
285                 goto out_kfree;
286         }
287         status =  AE_OK;
288
289 out_kfree:
290         kfree(output.pointer);
291         if (status != AE_OK)
292                 context->ret.pointer = NULL;
293         return status;
294 }
295 EXPORT_SYMBOL(acpi_run_osc);
296
297 bool osc_sb_apei_support_acked;
298 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
299 static void acpi_bus_osc_support(void)
300 {
301         u32 capbuf[2];
302         struct acpi_osc_context context = {
303                 .uuid_str = sb_uuid_str,
304                 .rev = 1,
305                 .cap.length = 8,
306                 .cap.pointer = capbuf,
307         };
308         acpi_handle handle;
309
310         capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
311         capbuf[OSC_SUPPORT_DWORD] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
312 #if defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) ||\
313                         defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
314         capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PAD_SUPPORT;
315 #endif
316
317 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
318         capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_PPC_OST_SUPPORT;
319 #endif
320
321 #ifdef ACPI_HOTPLUG_OST
322         capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_HOTPLUG_OST_SUPPORT;
323 #endif
324
325         if (!ghes_disable)
326                 capbuf[OSC_SUPPORT_DWORD] |= OSC_SB_APEI_SUPPORT;
327         if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
328                 return;
329         if (ACPI_SUCCESS(acpi_run_osc(handle, &context))) {
330                 u32 *capbuf_ret = context.ret.pointer;
331                 if (context.ret.length > OSC_SUPPORT_DWORD)
332                         osc_sb_apei_support_acked =
333                                 capbuf_ret[OSC_SUPPORT_DWORD] & OSC_SB_APEI_SUPPORT;
334                 kfree(context.ret.pointer);
335         }
336         /* do we need to check other returned cap? Sounds no */
337 }
338
339 /* --------------------------------------------------------------------------
340                              Notification Handling
341    -------------------------------------------------------------------------- */
342
343 /**
344  * acpi_bus_notify
345  * ---------------
346  * Callback for all 'system-level' device notifications (values 0x00-0x7F).
347  */
348 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
349 {
350         struct acpi_device *device = NULL;
351         struct acpi_driver *driver;
352
353         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
354                           type, handle));
355
356         switch (type) {
357
358         case ACPI_NOTIFY_BUS_CHECK:
359                 /* TBD */
360                 break;
361
362         case ACPI_NOTIFY_DEVICE_CHECK:
363                 /* TBD */
364                 break;
365
366         case ACPI_NOTIFY_DEVICE_WAKE:
367                 /* TBD */
368                 break;
369
370         case ACPI_NOTIFY_EJECT_REQUEST:
371                 /* TBD */
372                 break;
373
374         case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
375                 /* TBD: Exactly what does 'light' mean? */
376                 break;
377
378         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
379                 /* TBD */
380                 break;
381
382         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
383                 /* TBD */
384                 break;
385
386         case ACPI_NOTIFY_POWER_FAULT:
387                 /* TBD */
388                 break;
389
390         default:
391                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
392                                   "Received unknown/unsupported notification [%08x]\n",
393                                   type));
394                 break;
395         }
396
397         acpi_bus_get_device(handle, &device);
398         if (device) {
399                 driver = device->driver;
400                 if (driver && driver->ops.notify &&
401                     (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
402                         driver->ops.notify(device, type);
403         }
404 }
405
406 /* --------------------------------------------------------------------------
407                              Initialization/Cleanup
408    -------------------------------------------------------------------------- */
409
410 static int __init acpi_bus_init_irq(void)
411 {
412         acpi_status status;
413         char *message = NULL;
414
415
416         /*
417          * Let the system know what interrupt model we are using by
418          * evaluating the \_PIC object, if exists.
419          */
420
421         switch (acpi_irq_model) {
422         case ACPI_IRQ_MODEL_PIC:
423                 message = "PIC";
424                 break;
425         case ACPI_IRQ_MODEL_IOAPIC:
426                 message = "IOAPIC";
427                 break;
428         case ACPI_IRQ_MODEL_IOSAPIC:
429                 message = "IOSAPIC";
430                 break;
431         case ACPI_IRQ_MODEL_PLATFORM:
432                 message = "platform specific model";
433                 break;
434         default:
435                 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
436                 return -ENODEV;
437         }
438
439         printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
440
441         status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
442         if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
443                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
444                 return -ENODEV;
445         }
446
447         return 0;
448 }
449
450 u8 acpi_gbl_permanent_mmap;
451
452
453 void __init acpi_early_init(void)
454 {
455         acpi_status status;
456
457         if (acpi_disabled)
458                 return;
459
460         printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
461
462         /* enable workarounds, unless strict ACPI spec. compliance */
463         if (!acpi_strict)
464                 acpi_gbl_enable_interpreter_slack = TRUE;
465
466         acpi_gbl_permanent_mmap = 1;
467
468         /*
469          * If the machine falls into the DMI check table,
470          * DSDT will be copied to memory
471          */
472         dmi_check_system(dsdt_dmi_table);
473
474         status = acpi_reallocate_root_table();
475         if (ACPI_FAILURE(status)) {
476                 printk(KERN_ERR PREFIX
477                        "Unable to reallocate ACPI tables\n");
478                 goto error0;
479         }
480
481         status = acpi_initialize_subsystem();
482         if (ACPI_FAILURE(status)) {
483                 printk(KERN_ERR PREFIX
484                        "Unable to initialize the ACPI Interpreter\n");
485                 goto error0;
486         }
487
488         status = acpi_load_tables();
489         if (ACPI_FAILURE(status)) {
490                 printk(KERN_ERR PREFIX
491                        "Unable to load the System Description Tables\n");
492                 goto error0;
493         }
494
495 #ifdef CONFIG_X86
496         if (!acpi_ioapic) {
497                 /* compatible (0) means level (3) */
498                 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
499                         acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
500                         acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
501                 }
502                 /* Set PIC-mode SCI trigger type */
503                 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
504                                          (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
505         } else {
506                 /*
507                  * now that acpi_gbl_FADT is initialized,
508                  * update it with result from INT_SRC_OVR parsing
509                  */
510                 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
511         }
512 #endif
513
514         status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
515         if (ACPI_FAILURE(status)) {
516                 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
517                 goto error0;
518         }
519
520         /*
521          * If the system is using ACPI then we can be reasonably
522          * confident that any regulators are managed by the firmware
523          * so tell the regulator core it has everything it needs to
524          * know.
525          */
526         regulator_has_full_constraints();
527
528         return;
529
530       error0:
531         disable_acpi();
532         return;
533 }
534
535 static int __init acpi_bus_init(void)
536 {
537         int result;
538         acpi_status status;
539
540         acpi_os_initialize1();
541
542         status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
543         if (ACPI_FAILURE(status)) {
544                 printk(KERN_ERR PREFIX
545                        "Unable to start the ACPI Interpreter\n");
546                 goto error1;
547         }
548
549         /*
550          * ACPI 2.0 requires the EC driver to be loaded and work before
551          * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
552          * is called).
553          *
554          * This is accomplished by looking for the ECDT table, and getting
555          * the EC parameters out of that.
556          */
557         status = acpi_ec_ecdt_probe();
558         /* Ignore result. Not having an ECDT is not fatal. */
559
560         status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
561         if (ACPI_FAILURE(status)) {
562                 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
563                 goto error1;
564         }
565
566         /*
567          * _OSC method may exist in module level code,
568          * so it must be run after ACPI_FULL_INITIALIZATION
569          */
570         acpi_bus_osc_support();
571
572         /*
573          * _PDC control method may load dynamic SSDT tables,
574          * and we need to install the table handler before that.
575          */
576         acpi_sysfs_init();
577
578         acpi_early_processor_set_pdc();
579
580         /*
581          * Maybe EC region is required at bus_scan/acpi_get_devices. So it
582          * is necessary to enable it as early as possible.
583          */
584         acpi_boot_ec_enable();
585
586         printk(KERN_INFO PREFIX "Interpreter enabled\n");
587
588         /* Initialize sleep structures */
589         acpi_sleep_init();
590
591         /*
592          * Get the system interrupt model and evaluate \_PIC.
593          */
594         result = acpi_bus_init_irq();
595         if (result)
596                 goto error1;
597
598         /*
599          * Register the for all standard device notifications.
600          */
601         status =
602             acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
603                                         &acpi_bus_notify, NULL);
604         if (ACPI_FAILURE(status)) {
605                 printk(KERN_ERR PREFIX
606                        "Unable to register for device notifications\n");
607                 goto error1;
608         }
609
610         /*
611          * Create the top ACPI proc directory
612          */
613         acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
614
615         return 0;
616
617         /* Mimic structured exception handling */
618       error1:
619         acpi_terminate();
620         return -ENODEV;
621 }
622
623 struct kobject *acpi_kobj;
624 EXPORT_SYMBOL_GPL(acpi_kobj);
625
626 static int __init acpi_init(void)
627 {
628         int result;
629
630         if (acpi_disabled) {
631                 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
632                 return -ENODEV;
633         }
634
635         acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
636         if (!acpi_kobj) {
637                 printk(KERN_WARNING "%s: kset create error\n", __func__);
638                 acpi_kobj = NULL;
639         }
640
641         init_acpi_device_notify();
642         result = acpi_bus_init();
643         if (result) {
644                 disable_acpi();
645                 return result;
646         }
647
648         pci_mmcfg_late_init();
649         acpi_scan_init();
650         acpi_ec_init();
651         acpi_debugfs_init();
652         acpi_sleep_proc_init();
653         acpi_wakeup_device_init();
654         return 0;
655 }
656
657 subsys_initcall(acpi_init);