clockevent: sun4i: Fix race condition in the probe code
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / acpi / utils.c
1 /*
2  *  acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/hardirq.h>
32 #include <linux/acpi.h>
33 #include <linux/dynamic_debug.h>
34
35 #include "internal.h"
36
37 #define _COMPONENT              ACPI_BUS_COMPONENT
38 ACPI_MODULE_NAME("utils");
39
40 /* --------------------------------------------------------------------------
41                             Object Evaluation Helpers
42    -------------------------------------------------------------------------- */
43 static void
44 acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
45 {
46 #ifdef ACPI_DEBUG_OUTPUT
47         char prefix[80] = {'\0'};
48         struct acpi_buffer buffer = {sizeof(prefix), prefix};
49         acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
50         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
51                 (char *) prefix, p, acpi_format_exception(s)));
52 #else
53         return;
54 #endif
55 }
56
57 acpi_status
58 acpi_extract_package(union acpi_object *package,
59                      struct acpi_buffer *format, struct acpi_buffer *buffer)
60 {
61         u32 size_required = 0;
62         u32 tail_offset = 0;
63         char *format_string = NULL;
64         u32 format_count = 0;
65         u32 i = 0;
66         u8 *head = NULL;
67         u8 *tail = NULL;
68
69
70         if (!package || (package->type != ACPI_TYPE_PACKAGE)
71             || (package->package.count < 1)) {
72                 printk(KERN_WARNING PREFIX "Invalid package argument\n");
73                 return AE_BAD_PARAMETER;
74         }
75
76         if (!format || !format->pointer || (format->length < 1)) {
77                 printk(KERN_WARNING PREFIX "Invalid format argument\n");
78                 return AE_BAD_PARAMETER;
79         }
80
81         if (!buffer) {
82                 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
83                 return AE_BAD_PARAMETER;
84         }
85
86         format_count = (format->length / sizeof(char)) - 1;
87         if (format_count > package->package.count) {
88                 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
89                               " than exist in package [%d].\n",
90                               format_count, package->package.count);
91                 return AE_BAD_DATA;
92         }
93
94         format_string = format->pointer;
95
96         /*
97          * Calculate size_required.
98          */
99         for (i = 0; i < format_count; i++) {
100
101                 union acpi_object *element = &(package->package.elements[i]);
102
103                 switch (element->type) {
104
105                 case ACPI_TYPE_INTEGER:
106                         switch (format_string[i]) {
107                         case 'N':
108                                 size_required += sizeof(u64);
109                                 tail_offset += sizeof(u64);
110                                 break;
111                         case 'S':
112                                 size_required +=
113                                     sizeof(char *) + sizeof(u64) +
114                                     sizeof(char);
115                                 tail_offset += sizeof(char *);
116                                 break;
117                         default:
118                                 printk(KERN_WARNING PREFIX "Invalid package element"
119                                               " [%d]: got number, expecting"
120                                               " [%c]\n",
121                                               i, format_string[i]);
122                                 return AE_BAD_DATA;
123                                 break;
124                         }
125                         break;
126
127                 case ACPI_TYPE_STRING:
128                 case ACPI_TYPE_BUFFER:
129                         switch (format_string[i]) {
130                         case 'S':
131                                 size_required +=
132                                     sizeof(char *) +
133                                     (element->string.length * sizeof(char)) +
134                                     sizeof(char);
135                                 tail_offset += sizeof(char *);
136                                 break;
137                         case 'B':
138                                 size_required +=
139                                     sizeof(u8 *) +
140                                     (element->buffer.length * sizeof(u8));
141                                 tail_offset += sizeof(u8 *);
142                                 break;
143                         default:
144                                 printk(KERN_WARNING PREFIX "Invalid package element"
145                                               " [%d] got string/buffer,"
146                                               " expecting [%c]\n",
147                                               i, format_string[i]);
148                                 return AE_BAD_DATA;
149                                 break;
150                         }
151                         break;
152
153                 case ACPI_TYPE_PACKAGE:
154                 default:
155                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
156                                           "Found unsupported element at index=%d\n",
157                                           i));
158                         /* TBD: handle nested packages... */
159                         return AE_SUPPORT;
160                         break;
161                 }
162         }
163
164         /*
165          * Validate output buffer.
166          */
167         if (buffer->length == ACPI_ALLOCATE_BUFFER) {
168                 buffer->pointer = ACPI_ALLOCATE(size_required);
169                 if (!buffer->pointer)
170                         return AE_NO_MEMORY;
171                 buffer->length = size_required;
172                 memset(buffer->pointer, 0, size_required);
173         } else {
174                 if (buffer->length < size_required) {
175                         buffer->length = size_required;
176                         return AE_BUFFER_OVERFLOW;
177                 } else if (buffer->length != size_required ||
178                            !buffer->pointer) {
179                         return AE_BAD_PARAMETER;
180                 }
181         }
182
183         head = buffer->pointer;
184         tail = buffer->pointer + tail_offset;
185
186         /*
187          * Extract package data.
188          */
189         for (i = 0; i < format_count; i++) {
190
191                 u8 **pointer = NULL;
192                 union acpi_object *element = &(package->package.elements[i]);
193
194                 if (!element) {
195                         return AE_BAD_DATA;
196                 }
197
198                 switch (element->type) {
199
200                 case ACPI_TYPE_INTEGER:
201                         switch (format_string[i]) {
202                         case 'N':
203                                 *((u64 *) head) =
204                                     element->integer.value;
205                                 head += sizeof(u64);
206                                 break;
207                         case 'S':
208                                 pointer = (u8 **) head;
209                                 *pointer = tail;
210                                 *((u64 *) tail) =
211                                     element->integer.value;
212                                 head += sizeof(u64 *);
213                                 tail += sizeof(u64);
214                                 /* NULL terminate string */
215                                 *tail = (char)0;
216                                 tail += sizeof(char);
217                                 break;
218                         default:
219                                 /* Should never get here */
220                                 break;
221                         }
222                         break;
223
224                 case ACPI_TYPE_STRING:
225                 case ACPI_TYPE_BUFFER:
226                         switch (format_string[i]) {
227                         case 'S':
228                                 pointer = (u8 **) head;
229                                 *pointer = tail;
230                                 memcpy(tail, element->string.pointer,
231                                        element->string.length);
232                                 head += sizeof(char *);
233                                 tail += element->string.length * sizeof(char);
234                                 /* NULL terminate string */
235                                 *tail = (char)0;
236                                 tail += sizeof(char);
237                                 break;
238                         case 'B':
239                                 pointer = (u8 **) head;
240                                 *pointer = tail;
241                                 memcpy(tail, element->buffer.pointer,
242                                        element->buffer.length);
243                                 head += sizeof(u8 *);
244                                 tail += element->buffer.length * sizeof(u8);
245                                 break;
246                         default:
247                                 /* Should never get here */
248                                 break;
249                         }
250                         break;
251
252                 case ACPI_TYPE_PACKAGE:
253                         /* TBD: handle nested packages... */
254                 default:
255                         /* Should never get here */
256                         break;
257                 }
258         }
259
260         return AE_OK;
261 }
262
263 EXPORT_SYMBOL(acpi_extract_package);
264
265 acpi_status
266 acpi_evaluate_integer(acpi_handle handle,
267                       acpi_string pathname,
268                       struct acpi_object_list *arguments, unsigned long long *data)
269 {
270         acpi_status status = AE_OK;
271         union acpi_object element;
272         struct acpi_buffer buffer = { 0, NULL };
273
274         if (!data)
275                 return AE_BAD_PARAMETER;
276
277         buffer.length = sizeof(union acpi_object);
278         buffer.pointer = &element;
279         status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
280         if (ACPI_FAILURE(status)) {
281                 acpi_util_eval_error(handle, pathname, status);
282                 return status;
283         }
284
285         if (element.type != ACPI_TYPE_INTEGER) {
286                 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
287                 return AE_BAD_DATA;
288         }
289
290         *data = element.integer.value;
291
292         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
293
294         return AE_OK;
295 }
296
297 EXPORT_SYMBOL(acpi_evaluate_integer);
298
299 acpi_status
300 acpi_evaluate_reference(acpi_handle handle,
301                         acpi_string pathname,
302                         struct acpi_object_list *arguments,
303                         struct acpi_handle_list *list)
304 {
305         acpi_status status = AE_OK;
306         union acpi_object *package = NULL;
307         union acpi_object *element = NULL;
308         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
309         u32 i = 0;
310
311
312         if (!list) {
313                 return AE_BAD_PARAMETER;
314         }
315
316         /* Evaluate object. */
317
318         status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
319         if (ACPI_FAILURE(status))
320                 goto end;
321
322         package = buffer.pointer;
323
324         if ((buffer.length == 0) || !package) {
325                 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
326                             (unsigned)buffer.length, package);
327                 status = AE_BAD_DATA;
328                 acpi_util_eval_error(handle, pathname, status);
329                 goto end;
330         }
331         if (package->type != ACPI_TYPE_PACKAGE) {
332                 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
333                             package->type);
334                 status = AE_BAD_DATA;
335                 acpi_util_eval_error(handle, pathname, status);
336                 goto end;
337         }
338         if (!package->package.count) {
339                 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
340                             package);
341                 status = AE_BAD_DATA;
342                 acpi_util_eval_error(handle, pathname, status);
343                 goto end;
344         }
345
346         if (package->package.count > ACPI_MAX_HANDLES) {
347                 return AE_NO_MEMORY;
348         }
349         list->count = package->package.count;
350
351         /* Extract package data. */
352
353         for (i = 0; i < list->count; i++) {
354
355                 element = &(package->package.elements[i]);
356
357                 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
358                         status = AE_BAD_DATA;
359                         printk(KERN_ERR PREFIX
360                                     "Expecting a [Reference] package element, found type %X\n",
361                                     element->type);
362                         acpi_util_eval_error(handle, pathname, status);
363                         break;
364                 }
365
366                 if (!element->reference.handle) {
367                         printk(KERN_WARNING PREFIX "Invalid reference in"
368                                " package %s\n", pathname);
369                         status = AE_NULL_ENTRY;
370                         break;
371                 }
372                 /* Get the  acpi_handle. */
373
374                 list->handles[i] = element->reference.handle;
375                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
376                                   list->handles[i]));
377         }
378
379       end:
380         if (ACPI_FAILURE(status)) {
381                 list->count = 0;
382                 //kfree(list->handles);
383         }
384
385         kfree(buffer.pointer);
386
387         return status;
388 }
389
390 EXPORT_SYMBOL(acpi_evaluate_reference);
391
392 acpi_status
393 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
394 {
395         acpi_status status;
396         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
397         union acpi_object *output;
398
399         status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
400
401         if (ACPI_FAILURE(status))
402                 return status;
403
404         output = buffer.pointer;
405
406         if (!output || output->type != ACPI_TYPE_PACKAGE
407             || !output->package.count
408             || output->package.elements[0].type != ACPI_TYPE_BUFFER
409             || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
410                 status = AE_TYPE;
411                 goto out;
412         }
413
414         status = acpi_decode_pld_buffer(
415                         output->package.elements[0].buffer.pointer,
416                         output->package.elements[0].buffer.length,
417                         pld);
418
419 out:
420         kfree(buffer.pointer);
421         return status;
422 }
423 EXPORT_SYMBOL(acpi_get_physical_device_location);
424
425 /**
426  * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations
427  * @handle: ACPI device handle
428  * @source_event: source event code
429  * @status_code: status code
430  * @status_buf: optional detailed information (NULL if none)
431  *
432  * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
433  * must call this function when evaluating _OST for hotplug operations.
434  * When the platform does not support _OST, this function has no effect.
435  */
436 acpi_status
437 acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event,
438                 u32 status_code, struct acpi_buffer *status_buf)
439 {
440 #ifdef ACPI_HOTPLUG_OST
441         union acpi_object params[3] = {
442                 {.type = ACPI_TYPE_INTEGER,},
443                 {.type = ACPI_TYPE_INTEGER,},
444                 {.type = ACPI_TYPE_BUFFER,}
445         };
446         struct acpi_object_list arg_list = {3, params};
447         acpi_status status;
448
449         params[0].integer.value = source_event;
450         params[1].integer.value = status_code;
451         if (status_buf != NULL) {
452                 params[2].buffer.pointer = status_buf->pointer;
453                 params[2].buffer.length = status_buf->length;
454         } else {
455                 params[2].buffer.pointer = NULL;
456                 params[2].buffer.length = 0;
457         }
458
459         status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
460         return status;
461 #else
462         return AE_OK;
463 #endif
464 }
465 EXPORT_SYMBOL(acpi_evaluate_hotplug_ost);
466
467 /**
468  * acpi_handle_path: Return the object path of handle
469  *
470  * Caller must free the returned buffer
471  */
472 static char *acpi_handle_path(acpi_handle handle)
473 {
474         struct acpi_buffer buffer = {
475                 .length = ACPI_ALLOCATE_BUFFER,
476                 .pointer = NULL
477         };
478
479         if (in_interrupt() ||
480             acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
481                 return NULL;
482         return buffer.pointer;
483 }
484
485 /**
486  * acpi_handle_printk: Print message with ACPI prefix and object path
487  *
488  * This function is called through acpi_handle_<level> macros and prints
489  * a message with ACPI prefix and object path.  This function acquires
490  * the global namespace mutex to obtain an object path.  In interrupt
491  * context, it shows the object path as <n/a>.
492  */
493 void
494 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
495 {
496         struct va_format vaf;
497         va_list args;
498         const char *path;
499
500         va_start(args, fmt);
501         vaf.fmt = fmt;
502         vaf.va = &args;
503
504         path = acpi_handle_path(handle);
505         printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
506
507         va_end(args);
508         kfree(path);
509 }
510 EXPORT_SYMBOL(acpi_handle_printk);
511
512 #if defined(CONFIG_DYNAMIC_DEBUG)
513 /**
514  * __acpi_handle_debug: pr_debug with ACPI prefix and object path
515  *
516  * This function is called through acpi_handle_debug macro and debug
517  * prints a message with ACPI prefix and object path. This function
518  * acquires the global namespace mutex to obtain an object path.  In
519  * interrupt context, it shows the object path as <n/a>.
520  */
521 void
522 __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
523                     const char *fmt, ...)
524 {
525         struct va_format vaf;
526         va_list args;
527         const char *path;
528
529         va_start(args, fmt);
530         vaf.fmt = fmt;
531         vaf.va = &args;
532
533         path = acpi_handle_path(handle);
534         __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
535
536         va_end(args);
537         kfree(path);
538 }
539 EXPORT_SYMBOL(__acpi_handle_debug);
540 #endif
541
542 /**
543  * acpi_has_method: Check whether @handle has a method named @name
544  * @handle: ACPI device handle
545  * @name: name of object or method
546  *
547  * Check whether @handle has a method named @name.
548  */
549 bool acpi_has_method(acpi_handle handle, char *name)
550 {
551         acpi_handle tmp;
552
553         return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
554 }
555 EXPORT_SYMBOL(acpi_has_method);
556
557 acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
558                                        u64 arg)
559 {
560         union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
561         struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
562
563         obj.integer.value = arg;
564
565         return acpi_evaluate_object(handle, method, &arg_list, NULL);
566 }
567 EXPORT_SYMBOL(acpi_execute_simple_method);
568
569 /**
570  * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
571  * @handle: ACPI device handle
572  *
573  * Evaluate device's _EJ0 method for hotplug operations.
574  */
575 acpi_status acpi_evaluate_ej0(acpi_handle handle)
576 {
577         acpi_status status;
578
579         status = acpi_execute_simple_method(handle, "_EJ0", 1);
580         if (status == AE_NOT_FOUND)
581                 acpi_handle_warn(handle, "No _EJ0 support for device\n");
582         else if (ACPI_FAILURE(status))
583                 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
584
585         return status;
586 }
587
588 /**
589  * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
590  * @handle: ACPI device handle
591  * @lock: lock device if non-zero, otherwise unlock device
592  *
593  * Evaluate device's _LCK method if present to lock/unlock device
594  */
595 acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
596 {
597         acpi_status status;
598
599         status = acpi_execute_simple_method(handle, "_LCK", !!lock);
600         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
601                 if (lock)
602                         acpi_handle_warn(handle,
603                                 "Locking device failed (0x%x)\n", status);
604                 else
605                         acpi_handle_warn(handle,
606                                 "Unlocking device failed (0x%x)\n", status);
607         }
608
609         return status;
610 }
611
612 /**
613  * acpi_evaluate_dsm - evaluate device's _DSM method
614  * @handle: ACPI device handle
615  * @uuid: UUID of requested functions, should be 16 bytes
616  * @rev: revision number of requested function
617  * @func: requested function number
618  * @argv4: the function specific parameter
619  *
620  * Evaluate device's _DSM method with specified UUID, revision id and
621  * function number. Caller needs to free the returned object.
622  *
623  * Though ACPI defines the fourth parameter for _DSM should be a package,
624  * some old BIOSes do expect a buffer or an integer etc.
625  */
626 union acpi_object *
627 acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
628                   union acpi_object *argv4)
629 {
630         acpi_status ret;
631         struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
632         union acpi_object params[4];
633         struct acpi_object_list input = {
634                 .count = 4,
635                 .pointer = params,
636         };
637
638         params[0].type = ACPI_TYPE_BUFFER;
639         params[0].buffer.length = 16;
640         params[0].buffer.pointer = (char *)uuid;
641         params[1].type = ACPI_TYPE_INTEGER;
642         params[1].integer.value = rev;
643         params[2].type = ACPI_TYPE_INTEGER;
644         params[2].integer.value = func;
645         if (argv4) {
646                 params[3] = *argv4;
647         } else {
648                 params[3].type = ACPI_TYPE_PACKAGE;
649                 params[3].package.count = 0;
650                 params[3].package.elements = NULL;
651         }
652
653         ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
654         if (ACPI_SUCCESS(ret))
655                 return (union acpi_object *)buf.pointer;
656
657         if (ret != AE_NOT_FOUND)
658                 acpi_handle_warn(handle,
659                                 "failed to evaluate _DSM (0x%x)\n", ret);
660
661         return NULL;
662 }
663 EXPORT_SYMBOL(acpi_evaluate_dsm);
664
665 /**
666  * acpi_check_dsm - check if _DSM method supports requested functions.
667  * @handle: ACPI device handle
668  * @uuid: UUID of requested functions, should be 16 bytes at least
669  * @rev: revision number of requested functions
670  * @funcs: bitmap of requested functions
671  * @exclude: excluding special value, used to support i915 and nouveau
672  *
673  * Evaluate device's _DSM method to check whether it supports requested
674  * functions. Currently only support 64 functions at maximum, should be
675  * enough for now.
676  */
677 bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
678 {
679         int i;
680         u64 mask = 0;
681         union acpi_object *obj;
682
683         if (funcs == 0)
684                 return false;
685
686         obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
687         if (!obj)
688                 return false;
689
690         /* For compatibility, old BIOSes may return an integer */
691         if (obj->type == ACPI_TYPE_INTEGER)
692                 mask = obj->integer.value;
693         else if (obj->type == ACPI_TYPE_BUFFER)
694                 for (i = 0; i < obj->buffer.length && i < 8; i++)
695                         mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
696         ACPI_FREE(obj);
697
698         /*
699          * Bit 0 indicates whether there's support for any functions other than
700          * function 0 for the specified UUID and revision.
701          */
702         if ((mask & 0x1) && (mask & funcs) == funcs)
703                 return true;
704
705         return false;
706 }
707 EXPORT_SYMBOL(acpi_check_dsm);