Merge branch 'core-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / acpica / nsxfname.c
1 /******************************************************************************
2  *
3  * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4  *                         ACPI Namespace oriented interfaces
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2015, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45 #define EXPORT_ACPI_INTERFACES
46
47 #include <acpi/acpi.h>
48 #include "accommon.h"
49 #include "acnamesp.h"
50 #include "acparser.h"
51 #include "amlcode.h"
52
53 #define _COMPONENT          ACPI_NAMESPACE
54 ACPI_MODULE_NAME("nsxfname")
55
56 /* Local prototypes */
57 static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
58                                     struct acpi_pnp_device_id *source,
59                                     char *string_area);
60
61 /******************************************************************************
62  *
63  * FUNCTION:    acpi_get_handle
64  *
65  * PARAMETERS:  parent          - Object to search under (search scope).
66  *              pathname        - Pointer to an asciiz string containing the
67  *                                name
68  *              ret_handle      - Where the return handle is returned
69  *
70  * RETURN:      Status
71  *
72  * DESCRIPTION: This routine will search for a caller specified name in the
73  *              name space. The caller can restrict the search region by
74  *              specifying a non NULL parent. The parent value is itself a
75  *              namespace handle.
76  *
77  ******************************************************************************/
78
79 acpi_status
80 acpi_get_handle(acpi_handle parent,
81                 acpi_string pathname, acpi_handle * ret_handle)
82 {
83         acpi_status status;
84         struct acpi_namespace_node *node = NULL;
85         struct acpi_namespace_node *prefix_node = NULL;
86
87         ACPI_FUNCTION_ENTRY();
88
89         /* Parameter Validation */
90
91         if (!ret_handle || !pathname) {
92                 return (AE_BAD_PARAMETER);
93         }
94
95         /* Convert a parent handle to a prefix node */
96
97         if (parent) {
98                 prefix_node = acpi_ns_validate_handle(parent);
99                 if (!prefix_node) {
100                         return (AE_BAD_PARAMETER);
101                 }
102         }
103
104         /*
105          * Valid cases are:
106          * 1) Fully qualified pathname
107          * 2) Parent + Relative pathname
108          *
109          * Error for <null Parent + relative path>
110          */
111         if (ACPI_IS_ROOT_PREFIX(pathname[0])) {
112
113                 /* Pathname is fully qualified (starts with '\') */
114
115                 /* Special case for root-only, since we can't search for it */
116
117                 if (!strcmp(pathname, ACPI_NS_ROOT_PATH)) {
118                         *ret_handle =
119                             ACPI_CAST_PTR(acpi_handle, acpi_gbl_root_node);
120                         return (AE_OK);
121                 }
122         } else if (!prefix_node) {
123
124                 /* Relative path with null prefix is disallowed */
125
126                 return (AE_BAD_PARAMETER);
127         }
128
129         /* Find the Node and convert to a handle */
130
131         status =
132             acpi_ns_get_node(prefix_node, pathname, ACPI_NS_NO_UPSEARCH, &node);
133         if (ACPI_SUCCESS(status)) {
134                 *ret_handle = ACPI_CAST_PTR(acpi_handle, node);
135         }
136
137         return (status);
138 }
139
140 ACPI_EXPORT_SYMBOL(acpi_get_handle)
141
142 /******************************************************************************
143  *
144  * FUNCTION:    acpi_get_name
145  *
146  * PARAMETERS:  handle          - Handle to be converted to a pathname
147  *              name_type       - Full pathname or single segment
148  *              buffer          - Buffer for returned path
149  *
150  * RETURN:      Pointer to a string containing the fully qualified Name.
151  *
152  * DESCRIPTION: This routine returns the fully qualified name associated with
153  *              the Handle parameter. This and the acpi_pathname_to_handle are
154  *              complementary functions.
155  *
156  ******************************************************************************/
157 acpi_status
158 acpi_get_name(acpi_handle handle, u32 name_type, struct acpi_buffer * buffer)
159 {
160         acpi_status status;
161         struct acpi_namespace_node *node;
162         char *node_name;
163
164         /* Parameter validation */
165
166         if (name_type > ACPI_NAME_TYPE_MAX) {
167                 return (AE_BAD_PARAMETER);
168         }
169
170         status = acpi_ut_validate_buffer(buffer);
171         if (ACPI_FAILURE(status)) {
172                 return (status);
173         }
174
175         if (name_type == ACPI_FULL_PATHNAME) {
176
177                 /* Get the full pathname (From the namespace root) */
178
179                 status = acpi_ns_handle_to_pathname(handle, buffer);
180                 return (status);
181         }
182
183         /*
184          * Wants the single segment ACPI name.
185          * Validate handle and convert to a namespace Node
186          */
187         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
188         if (ACPI_FAILURE(status)) {
189                 return (status);
190         }
191
192         node = acpi_ns_validate_handle(handle);
193         if (!node) {
194                 status = AE_BAD_PARAMETER;
195                 goto unlock_and_exit;
196         }
197
198         /* Validate/Allocate/Clear caller buffer */
199
200         status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
201         if (ACPI_FAILURE(status)) {
202                 goto unlock_and_exit;
203         }
204
205         /* Just copy the ACPI name from the Node and zero terminate it */
206
207         node_name = acpi_ut_get_node_name(node);
208         ACPI_MOVE_NAME(buffer->pointer, node_name);
209         ((char *)buffer->pointer)[ACPI_NAME_SIZE] = 0;
210         status = AE_OK;
211
212 unlock_and_exit:
213
214         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
215         return (status);
216 }
217
218 ACPI_EXPORT_SYMBOL(acpi_get_name)
219
220 /******************************************************************************
221  *
222  * FUNCTION:    acpi_ns_copy_device_id
223  *
224  * PARAMETERS:  dest                - Pointer to the destination PNP_DEVICE_ID
225  *              source              - Pointer to the source PNP_DEVICE_ID
226  *              string_area         - Pointer to where to copy the dest string
227  *
228  * RETURN:      Pointer to the next string area
229  *
230  * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
231  *
232  ******************************************************************************/
233 static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
234                                     struct acpi_pnp_device_id *source,
235                                     char *string_area)
236 {
237
238         /* Create the destination PNP_DEVICE_ID */
239
240         dest->string = string_area;
241         dest->length = source->length;
242
243         /* Copy actual string and return a pointer to the next string area */
244
245         memcpy(string_area, source->string, source->length);
246         return (string_area + source->length);
247 }
248
249 /******************************************************************************
250  *
251  * FUNCTION:    acpi_get_object_info
252  *
253  * PARAMETERS:  handle              - Object Handle
254  *              return_buffer       - Where the info is returned
255  *
256  * RETURN:      Status
257  *
258  * DESCRIPTION: Returns information about an object as gleaned from the
259  *              namespace node and possibly by running several standard
260  *              control methods (Such as in the case of a device.)
261  *
262  * For Device and Processor objects, run the Device _HID, _UID, _CID, _SUB,
263  * _CLS, _STA, _ADR, _sx_w, and _sx_d methods.
264  *
265  * Note: Allocates the return buffer, must be freed by the caller.
266  *
267  ******************************************************************************/
268
269 acpi_status
270 acpi_get_object_info(acpi_handle handle,
271                      struct acpi_device_info **return_buffer)
272 {
273         struct acpi_namespace_node *node;
274         struct acpi_device_info *info;
275         struct acpi_pnp_device_id_list *cid_list = NULL;
276         struct acpi_pnp_device_id *hid = NULL;
277         struct acpi_pnp_device_id *uid = NULL;
278         struct acpi_pnp_device_id *sub = NULL;
279         struct acpi_pnp_device_id *cls = NULL;
280         char *next_id_string;
281         acpi_object_type type;
282         acpi_name name;
283         u8 param_count = 0;
284         u16 valid = 0;
285         u32 info_size;
286         u32 i;
287         acpi_status status;
288
289         /* Parameter validation */
290
291         if (!handle || !return_buffer) {
292                 return (AE_BAD_PARAMETER);
293         }
294
295         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
296         if (ACPI_FAILURE(status)) {
297                 return (status);
298         }
299
300         node = acpi_ns_validate_handle(handle);
301         if (!node) {
302                 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
303                 return (AE_BAD_PARAMETER);
304         }
305
306         /* Get the namespace node data while the namespace is locked */
307
308         info_size = sizeof(struct acpi_device_info);
309         type = node->type;
310         name = node->name.integer;
311
312         if (node->type == ACPI_TYPE_METHOD) {
313                 param_count = node->object->method.param_count;
314         }
315
316         status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
317         if (ACPI_FAILURE(status)) {
318                 return (status);
319         }
320
321         if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
322                 /*
323                  * Get extra info for ACPI Device/Processor objects only:
324                  * Run the Device _HID, _UID, _SUB, _CID, and _CLS methods.
325                  *
326                  * Note: none of these methods are required, so they may or may
327                  * not be present for this device. The Info->Valid bitfield is used
328                  * to indicate which methods were found and run successfully.
329                  */
330
331                 /* Execute the Device._HID method */
332
333                 status = acpi_ut_execute_HID(node, &hid);
334                 if (ACPI_SUCCESS(status)) {
335                         info_size += hid->length;
336                         valid |= ACPI_VALID_HID;
337                 }
338
339                 /* Execute the Device._UID method */
340
341                 status = acpi_ut_execute_UID(node, &uid);
342                 if (ACPI_SUCCESS(status)) {
343                         info_size += uid->length;
344                         valid |= ACPI_VALID_UID;
345                 }
346
347                 /* Execute the Device._SUB method */
348
349                 status = acpi_ut_execute_SUB(node, &sub);
350                 if (ACPI_SUCCESS(status)) {
351                         info_size += sub->length;
352                         valid |= ACPI_VALID_SUB;
353                 }
354
355                 /* Execute the Device._CID method */
356
357                 status = acpi_ut_execute_CID(node, &cid_list);
358                 if (ACPI_SUCCESS(status)) {
359
360                         /* Add size of CID strings and CID pointer array */
361
362                         info_size +=
363                             (cid_list->list_size -
364                              sizeof(struct acpi_pnp_device_id_list));
365                         valid |= ACPI_VALID_CID;
366                 }
367
368                 /* Execute the Device._CLS method */
369
370                 status = acpi_ut_execute_CLS(node, &cls);
371                 if (ACPI_SUCCESS(status)) {
372                         info_size += cls->length;
373                         valid |= ACPI_VALID_CLS;
374                 }
375         }
376
377         /*
378          * Now that we have the variable-length data, we can allocate the
379          * return buffer
380          */
381         info = ACPI_ALLOCATE_ZEROED(info_size);
382         if (!info) {
383                 status = AE_NO_MEMORY;
384                 goto cleanup;
385         }
386
387         /* Get the fixed-length data */
388
389         if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
390                 /*
391                  * Get extra info for ACPI Device/Processor objects only:
392                  * Run the _STA, _ADR and, sx_w, and _sx_d methods.
393                  *
394                  * Notes: none of these methods are required, so they may or may
395                  * not be present for this device. The Info->Valid bitfield is used
396                  * to indicate which methods were found and run successfully.
397                  *
398                  * For _STA, if the method does not exist, then (as per the ACPI
399                  * specification), the returned current_status flags will indicate
400                  * that the device is present/functional/enabled. Otherwise, the
401                  * current_status flags reflect the value returned from _STA.
402                  */
403
404                 /* Execute the Device._STA method */
405
406                 status = acpi_ut_execute_STA(node, &info->current_status);
407                 if (ACPI_SUCCESS(status)) {
408                         valid |= ACPI_VALID_STA;
409                 }
410
411                 /* Execute the Device._ADR method */
412
413                 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR, node,
414                                                          &info->address);
415                 if (ACPI_SUCCESS(status)) {
416                         valid |= ACPI_VALID_ADR;
417                 }
418
419                 /* Execute the Device._sx_w methods */
420
421                 status = acpi_ut_execute_power_methods(node,
422                                                        acpi_gbl_lowest_dstate_names,
423                                                        ACPI_NUM_sx_w_METHODS,
424                                                        info->lowest_dstates);
425                 if (ACPI_SUCCESS(status)) {
426                         valid |= ACPI_VALID_SXWS;
427                 }
428
429                 /* Execute the Device._sx_d methods */
430
431                 status = acpi_ut_execute_power_methods(node,
432                                                        acpi_gbl_highest_dstate_names,
433                                                        ACPI_NUM_sx_d_METHODS,
434                                                        info->highest_dstates);
435                 if (ACPI_SUCCESS(status)) {
436                         valid |= ACPI_VALID_SXDS;
437                 }
438         }
439
440         /*
441          * Create a pointer to the string area of the return buffer.
442          * Point to the end of the base struct acpi_device_info structure.
443          */
444         next_id_string = ACPI_CAST_PTR(char, info->compatible_id_list.ids);
445         if (cid_list) {
446
447                 /* Point past the CID PNP_DEVICE_ID array */
448
449                 next_id_string +=
450                     ((acpi_size) cid_list->count *
451                      sizeof(struct acpi_pnp_device_id));
452         }
453
454         /*
455          * Copy the HID, UID, SUB, and CIDs to the return buffer.
456          * The variable-length strings are copied to the reserved area
457          * at the end of the buffer.
458          *
459          * For HID and CID, check if the ID is a PCI Root Bridge.
460          */
461         if (hid) {
462                 next_id_string = acpi_ns_copy_device_id(&info->hardware_id,
463                                                         hid, next_id_string);
464
465                 if (acpi_ut_is_pci_root_bridge(hid->string)) {
466                         info->flags |= ACPI_PCI_ROOT_BRIDGE;
467                 }
468         }
469
470         if (uid) {
471                 next_id_string = acpi_ns_copy_device_id(&info->unique_id,
472                                                         uid, next_id_string);
473         }
474
475         if (sub) {
476                 next_id_string = acpi_ns_copy_device_id(&info->subsystem_id,
477                                                         sub, next_id_string);
478         }
479
480         if (cid_list) {
481                 info->compatible_id_list.count = cid_list->count;
482                 info->compatible_id_list.list_size = cid_list->list_size;
483
484                 /* Copy each CID */
485
486                 for (i = 0; i < cid_list->count; i++) {
487                         next_id_string =
488                             acpi_ns_copy_device_id(&info->compatible_id_list.
489                                                    ids[i], &cid_list->ids[i],
490                                                    next_id_string);
491
492                         if (acpi_ut_is_pci_root_bridge(cid_list->ids[i].string)) {
493                                 info->flags |= ACPI_PCI_ROOT_BRIDGE;
494                         }
495                 }
496         }
497
498         if (cls) {
499                 next_id_string = acpi_ns_copy_device_id(&info->class_code,
500                                                         cls, next_id_string);
501         }
502
503         /* Copy the fixed-length data */
504
505         info->info_size = info_size;
506         info->type = type;
507         info->name = name;
508         info->param_count = param_count;
509         info->valid = valid;
510
511         *return_buffer = info;
512         status = AE_OK;
513
514 cleanup:
515         if (hid) {
516                 ACPI_FREE(hid);
517         }
518         if (uid) {
519                 ACPI_FREE(uid);
520         }
521         if (sub) {
522                 ACPI_FREE(sub);
523         }
524         if (cid_list) {
525                 ACPI_FREE(cid_list);
526         }
527         if (cls) {
528                 ACPI_FREE(cls);
529         }
530         return (status);
531 }
532
533 ACPI_EXPORT_SYMBOL(acpi_get_object_info)
534
535 /******************************************************************************
536  *
537  * FUNCTION:    acpi_install_method
538  *
539  * PARAMETERS:  buffer         - An ACPI table containing one control method
540  *
541  * RETURN:      Status
542  *
543  * DESCRIPTION: Install a control method into the namespace. If the method
544  *              name already exists in the namespace, it is overwritten. The
545  *              input buffer must contain a valid DSDT or SSDT containing a
546  *              single control method.
547  *
548  ******************************************************************************/
549 acpi_status acpi_install_method(u8 *buffer)
550 {
551         struct acpi_table_header *table =
552             ACPI_CAST_PTR(struct acpi_table_header, buffer);
553         u8 *aml_buffer;
554         u8 *aml_start;
555         char *path;
556         struct acpi_namespace_node *node;
557         union acpi_operand_object *method_obj;
558         struct acpi_parse_state parser_state;
559         u32 aml_length;
560         u16 opcode;
561         u8 method_flags;
562         acpi_status status;
563
564         /* Parameter validation */
565
566         if (!buffer) {
567                 return (AE_BAD_PARAMETER);
568         }
569
570         /* Table must be a DSDT or SSDT */
571
572         if (!ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) &&
573             !ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
574                 return (AE_BAD_HEADER);
575         }
576
577         /* First AML opcode in the table must be a control method */
578
579         parser_state.aml = buffer + sizeof(struct acpi_table_header);
580         opcode = acpi_ps_peek_opcode(&parser_state);
581         if (opcode != AML_METHOD_OP) {
582                 return (AE_BAD_PARAMETER);
583         }
584
585         /* Extract method information from the raw AML */
586
587         parser_state.aml += acpi_ps_get_opcode_size(opcode);
588         parser_state.pkg_end = acpi_ps_get_next_package_end(&parser_state);
589         path = acpi_ps_get_next_namestring(&parser_state);
590         method_flags = *parser_state.aml++;
591         aml_start = parser_state.aml;
592         aml_length = ACPI_PTR_DIFF(parser_state.pkg_end, aml_start);
593
594         /*
595          * Allocate resources up-front. We don't want to have to delete a new
596          * node from the namespace if we cannot allocate memory.
597          */
598         aml_buffer = ACPI_ALLOCATE(aml_length);
599         if (!aml_buffer) {
600                 return (AE_NO_MEMORY);
601         }
602
603         method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
604         if (!method_obj) {
605                 ACPI_FREE(aml_buffer);
606                 return (AE_NO_MEMORY);
607         }
608
609         /* Lock namespace for acpi_ns_lookup, we may be creating a new node */
610
611         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
612         if (ACPI_FAILURE(status)) {
613                 goto error_exit;
614         }
615
616         /* The lookup either returns an existing node or creates a new one */
617
618         status =
619             acpi_ns_lookup(NULL, path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
620                            ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND,
621                            NULL, &node);
622
623         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
624
625         if (ACPI_FAILURE(status)) {     /* ns_lookup */
626                 if (status != AE_ALREADY_EXISTS) {
627                         goto error_exit;
628                 }
629
630                 /* Node existed previously, make sure it is a method node */
631
632                 if (node->type != ACPI_TYPE_METHOD) {
633                         status = AE_TYPE;
634                         goto error_exit;
635                 }
636         }
637
638         /* Copy the method AML to the local buffer */
639
640         memcpy(aml_buffer, aml_start, aml_length);
641
642         /* Initialize the method object with the new method's information */
643
644         method_obj->method.aml_start = aml_buffer;
645         method_obj->method.aml_length = aml_length;
646
647         method_obj->method.param_count = (u8)
648             (method_flags & AML_METHOD_ARG_COUNT);
649
650         if (method_flags & AML_METHOD_SERIALIZED) {
651                 method_obj->method.info_flags = ACPI_METHOD_SERIALIZED;
652
653                 method_obj->method.sync_level = (u8)
654                     ((method_flags & AML_METHOD_SYNC_LEVEL) >> 4);
655         }
656
657         /*
658          * Now that it is complete, we can attach the new method object to
659          * the method Node (detaches/deletes any existing object)
660          */
661         status = acpi_ns_attach_object(node, method_obj, ACPI_TYPE_METHOD);
662
663         /*
664          * Flag indicates AML buffer is dynamic, must be deleted later.
665          * Must be set only after attach above.
666          */
667         node->flags |= ANOBJ_ALLOCATED_BUFFER;
668
669         /* Remove local reference to the method object */
670
671         acpi_ut_remove_reference(method_obj);
672         return (status);
673
674 error_exit:
675
676         ACPI_FREE(aml_buffer);
677         ACPI_FREE(method_obj);
678         return (status);
679 }
680 ACPI_EXPORT_SYMBOL(acpi_install_method)