GNU Linux-libre 4.9.284-gnu1
[releases.git] / drivers / acpi / acpica / evrgnini.c
1 /******************************************************************************
2  *
3  * Module Name: evrgnini- ACPI address_space (op_region) init
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2016, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acnamesp.h"
48 #include "acinterp.h"
49
50 #define _COMPONENT          ACPI_EVENTS
51 ACPI_MODULE_NAME("evrgnini")
52
53 /*******************************************************************************
54  *
55  * FUNCTION:    acpi_ev_system_memory_region_setup
56  *
57  * PARAMETERS:  handle              - Region we are interested in
58  *              function            - Start or stop
59  *              handler_context     - Address space handler context
60  *              region_context      - Region specific context
61  *
62  * RETURN:      Status
63  *
64  * DESCRIPTION: Setup a system_memory operation region
65  *
66  ******************************************************************************/
67 acpi_status
68 acpi_ev_system_memory_region_setup(acpi_handle handle,
69                                    u32 function,
70                                    void *handler_context, void **region_context)
71 {
72         union acpi_operand_object *region_desc =
73             (union acpi_operand_object *)handle;
74         struct acpi_mem_space_context *local_region_context;
75
76         ACPI_FUNCTION_TRACE(ev_system_memory_region_setup);
77
78         if (function == ACPI_REGION_DEACTIVATE) {
79                 if (*region_context) {
80                         local_region_context =
81                             (struct acpi_mem_space_context *)*region_context;
82
83                         /* Delete a cached mapping if present */
84
85                         if (local_region_context->mapped_length) {
86                                 acpi_os_unmap_memory(local_region_context->
87                                                      mapped_logical_address,
88                                                      local_region_context->
89                                                      mapped_length);
90                         }
91                         ACPI_FREE(local_region_context);
92                         *region_context = NULL;
93                 }
94                 return_ACPI_STATUS(AE_OK);
95         }
96
97         /* Create a new context */
98
99         local_region_context =
100             ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_mem_space_context));
101         if (!(local_region_context)) {
102                 return_ACPI_STATUS(AE_NO_MEMORY);
103         }
104
105         /* Save the region length and address for use in the handler */
106
107         local_region_context->length = region_desc->region.length;
108         local_region_context->address = region_desc->region.address;
109
110         *region_context = local_region_context;
111         return_ACPI_STATUS(AE_OK);
112 }
113
114 /*******************************************************************************
115  *
116  * FUNCTION:    acpi_ev_io_space_region_setup
117  *
118  * PARAMETERS:  handle              - Region we are interested in
119  *              function            - Start or stop
120  *              handler_context     - Address space handler context
121  *              region_context      - Region specific context
122  *
123  * RETURN:      Status
124  *
125  * DESCRIPTION: Setup a IO operation region
126  *
127  ******************************************************************************/
128
129 acpi_status
130 acpi_ev_io_space_region_setup(acpi_handle handle,
131                               u32 function,
132                               void *handler_context, void **region_context)
133 {
134         ACPI_FUNCTION_TRACE(ev_io_space_region_setup);
135
136         if (function == ACPI_REGION_DEACTIVATE) {
137                 *region_context = NULL;
138         } else {
139                 *region_context = handler_context;
140         }
141
142         return_ACPI_STATUS(AE_OK);
143 }
144
145 /*******************************************************************************
146  *
147  * FUNCTION:    acpi_ev_pci_config_region_setup
148  *
149  * PARAMETERS:  handle              - Region we are interested in
150  *              function            - Start or stop
151  *              handler_context     - Address space handler context
152  *              region_context      - Region specific context
153  *
154  * RETURN:      Status
155  *
156  * DESCRIPTION: Setup a PCI_Config operation region
157  *
158  * MUTEX:       Assumes namespace is not locked
159  *
160  ******************************************************************************/
161
162 acpi_status
163 acpi_ev_pci_config_region_setup(acpi_handle handle,
164                                 u32 function,
165                                 void *handler_context, void **region_context)
166 {
167         acpi_status status = AE_OK;
168         u64 pci_value;
169         struct acpi_pci_id *pci_id = *region_context;
170         union acpi_operand_object *handler_obj;
171         struct acpi_namespace_node *parent_node;
172         struct acpi_namespace_node *pci_root_node;
173         struct acpi_namespace_node *pci_device_node;
174         union acpi_operand_object *region_obj =
175             (union acpi_operand_object *)handle;
176
177         ACPI_FUNCTION_TRACE(ev_pci_config_region_setup);
178
179         handler_obj = region_obj->region.handler;
180         if (!handler_obj) {
181                 /*
182                  * No installed handler. This shouldn't happen because the dispatch
183                  * routine checks before we get here, but we check again just in case.
184                  */
185                 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
186                                   "Attempting to init a region %p, with no handler\n",
187                                   region_obj));
188                 return_ACPI_STATUS(AE_NOT_EXIST);
189         }
190
191         *region_context = NULL;
192         if (function == ACPI_REGION_DEACTIVATE) {
193                 if (pci_id) {
194                         ACPI_FREE(pci_id);
195                 }
196                 return_ACPI_STATUS(status);
197         }
198
199         parent_node = region_obj->region.node->parent;
200
201         /*
202          * Get the _SEG and _BBN values from the device upon which the handler
203          * is installed.
204          *
205          * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
206          * This is the device the handler has been registered to handle.
207          */
208
209         /*
210          * If the address_space.Node is still pointing to the root, we need
211          * to scan upward for a PCI Root bridge and re-associate the op_region
212          * handlers with that device.
213          */
214         if (handler_obj->address_space.node == acpi_gbl_root_node) {
215
216                 /* Start search from the parent object */
217
218                 pci_root_node = parent_node;
219                 while (pci_root_node != acpi_gbl_root_node) {
220
221                         /* Get the _HID/_CID in order to detect a root_bridge */
222
223                         if (acpi_ev_is_pci_root_bridge(pci_root_node)) {
224
225                                 /* Install a handler for this PCI root bridge */
226
227                                 status = acpi_install_address_space_handler((acpi_handle)pci_root_node, ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
228                                 if (ACPI_FAILURE(status)) {
229                                         if (status == AE_SAME_HANDLER) {
230                                                 /*
231                                                  * It is OK if the handler is already installed on the
232                                                  * root bridge. Still need to return a context object
233                                                  * for the new PCI_Config operation region, however.
234                                                  */
235                                                 status = AE_OK;
236                                         } else {
237                                                 ACPI_EXCEPTION((AE_INFO, status,
238                                                                 "Could not install PciConfig handler "
239                                                                 "for Root Bridge %4.4s",
240                                                                 acpi_ut_get_node_name
241                                                                 (pci_root_node)));
242                                         }
243                                 }
244                                 break;
245                         }
246
247                         pci_root_node = pci_root_node->parent;
248                 }
249
250                 /* PCI root bridge not found, use namespace root node */
251         } else {
252                 pci_root_node = handler_obj->address_space.node;
253         }
254
255         /*
256          * If this region is now initialized, we are done.
257          * (install_address_space_handler could have initialized it)
258          */
259         if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
260                 return_ACPI_STATUS(AE_OK);
261         }
262
263         /* Region is still not initialized. Create a new context */
264
265         pci_id = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_pci_id));
266         if (!pci_id) {
267                 return_ACPI_STATUS(AE_NO_MEMORY);
268         }
269
270         /*
271          * For PCI_Config space access, we need the segment, bus, device and
272          * function numbers. Acquire them here.
273          *
274          * Find the parent device object. (This allows the operation region to be
275          * within a subscope under the device, such as a control method.)
276          */
277         pci_device_node = region_obj->region.node;
278         while (pci_device_node && (pci_device_node->type != ACPI_TYPE_DEVICE)) {
279                 pci_device_node = pci_device_node->parent;
280         }
281
282         if (!pci_device_node) {
283                 ACPI_FREE(pci_id);
284                 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
285         }
286
287         /*
288          * Get the PCI device and function numbers from the _ADR object
289          * contained in the parent's scope.
290          */
291         status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR,
292                                                  pci_device_node, &pci_value);
293
294         /*
295          * The default is zero, and since the allocation above zeroed the data,
296          * just do nothing on failure.
297          */
298         if (ACPI_SUCCESS(status)) {
299                 pci_id->device = ACPI_HIWORD(ACPI_LODWORD(pci_value));
300                 pci_id->function = ACPI_LOWORD(ACPI_LODWORD(pci_value));
301         }
302
303         /* The PCI segment number comes from the _SEG method */
304
305         status = acpi_ut_evaluate_numeric_object(METHOD_NAME__SEG,
306                                                  pci_root_node, &pci_value);
307         if (ACPI_SUCCESS(status)) {
308                 pci_id->segment = ACPI_LOWORD(pci_value);
309         }
310
311         /* The PCI bus number comes from the _BBN method */
312
313         status = acpi_ut_evaluate_numeric_object(METHOD_NAME__BBN,
314                                                  pci_root_node, &pci_value);
315         if (ACPI_SUCCESS(status)) {
316                 pci_id->bus = ACPI_LOWORD(pci_value);
317         }
318
319         /* Complete/update the PCI ID for this device */
320
321         status =
322             acpi_hw_derive_pci_id(pci_id, pci_root_node,
323                                   region_obj->region.node);
324         if (ACPI_FAILURE(status)) {
325                 ACPI_FREE(pci_id);
326                 return_ACPI_STATUS(status);
327         }
328
329         *region_context = pci_id;
330         return_ACPI_STATUS(AE_OK);
331 }
332
333 /*******************************************************************************
334  *
335  * FUNCTION:    acpi_ev_is_pci_root_bridge
336  *
337  * PARAMETERS:  node            - Device node being examined
338  *
339  * RETURN:      TRUE if device is a PCI/PCI-Express Root Bridge
340  *
341  * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
342  *              examining the _HID and _CID for the device.
343  *
344  ******************************************************************************/
345
346 u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node)
347 {
348         acpi_status status;
349         struct acpi_pnp_device_id *hid;
350         struct acpi_pnp_device_id_list *cid;
351         u32 i;
352         u8 match;
353
354         /* Get the _HID and check for a PCI Root Bridge */
355
356         status = acpi_ut_execute_HID(node, &hid);
357         if (ACPI_FAILURE(status)) {
358                 return (FALSE);
359         }
360
361         match = acpi_ut_is_pci_root_bridge(hid->string);
362         ACPI_FREE(hid);
363
364         if (match) {
365                 return (TRUE);
366         }
367
368         /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
369
370         status = acpi_ut_execute_CID(node, &cid);
371         if (ACPI_FAILURE(status)) {
372                 return (FALSE);
373         }
374
375         /* Check all _CIDs in the returned list */
376
377         for (i = 0; i < cid->count; i++) {
378                 if (acpi_ut_is_pci_root_bridge(cid->ids[i].string)) {
379                         ACPI_FREE(cid);
380                         return (TRUE);
381                 }
382         }
383
384         ACPI_FREE(cid);
385         return (FALSE);
386 }
387
388 /*******************************************************************************
389  *
390  * FUNCTION:    acpi_ev_pci_bar_region_setup
391  *
392  * PARAMETERS:  handle              - Region we are interested in
393  *              function            - Start or stop
394  *              handler_context     - Address space handler context
395  *              region_context      - Region specific context
396  *
397  * RETURN:      Status
398  *
399  * DESCRIPTION: Setup a pci_BAR operation region
400  *
401  * MUTEX:       Assumes namespace is not locked
402  *
403  ******************************************************************************/
404
405 acpi_status
406 acpi_ev_pci_bar_region_setup(acpi_handle handle,
407                              u32 function,
408                              void *handler_context, void **region_context)
409 {
410         ACPI_FUNCTION_TRACE(ev_pci_bar_region_setup);
411
412         return_ACPI_STATUS(AE_OK);
413 }
414
415 /*******************************************************************************
416  *
417  * FUNCTION:    acpi_ev_cmos_region_setup
418  *
419  * PARAMETERS:  handle              - Region we are interested in
420  *              function            - Start or stop
421  *              handler_context     - Address space handler context
422  *              region_context      - Region specific context
423  *
424  * RETURN:      Status
425  *
426  * DESCRIPTION: Setup a CMOS operation region
427  *
428  * MUTEX:       Assumes namespace is not locked
429  *
430  ******************************************************************************/
431
432 acpi_status
433 acpi_ev_cmos_region_setup(acpi_handle handle,
434                           u32 function,
435                           void *handler_context, void **region_context)
436 {
437         ACPI_FUNCTION_TRACE(ev_cmos_region_setup);
438
439         return_ACPI_STATUS(AE_OK);
440 }
441
442 /*******************************************************************************
443  *
444  * FUNCTION:    acpi_ev_default_region_setup
445  *
446  * PARAMETERS:  handle              - Region we are interested in
447  *              function            - Start or stop
448  *              handler_context     - Address space handler context
449  *              region_context      - Region specific context
450  *
451  * RETURN:      Status
452  *
453  * DESCRIPTION: Default region initialization
454  *
455  ******************************************************************************/
456
457 acpi_status
458 acpi_ev_default_region_setup(acpi_handle handle,
459                              u32 function,
460                              void *handler_context, void **region_context)
461 {
462         ACPI_FUNCTION_TRACE(ev_default_region_setup);
463
464         if (function == ACPI_REGION_DEACTIVATE) {
465                 *region_context = NULL;
466         } else {
467                 *region_context = handler_context;
468         }
469
470         return_ACPI_STATUS(AE_OK);
471 }
472
473 /*******************************************************************************
474  *
475  * FUNCTION:    acpi_ev_initialize_region
476  *
477  * PARAMETERS:  region_obj      - Region we are initializing
478  *              acpi_ns_locked  - Is namespace locked?
479  *
480  * RETURN:      Status
481  *
482  * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
483  *              for execution at a later time
484  *
485  *              Get the appropriate address space handler for a newly
486  *              created region.
487  *
488  *              This also performs address space specific initialization. For
489  *              example, PCI regions must have an _ADR object that contains
490  *              a PCI address in the scope of the definition. This address is
491  *              required to perform an access to PCI config space.
492  *
493  * MUTEX:       Interpreter should be unlocked, because we may run the _REG
494  *              method for this region.
495  *
496  ******************************************************************************/
497
498 acpi_status
499 acpi_ev_initialize_region(union acpi_operand_object *region_obj,
500                           u8 acpi_ns_locked)
501 {
502         union acpi_operand_object *handler_obj;
503         union acpi_operand_object *obj_desc;
504         acpi_adr_space_type space_id;
505         struct acpi_namespace_node *node;
506         acpi_status status;
507
508         ACPI_FUNCTION_TRACE_U32(ev_initialize_region, acpi_ns_locked);
509
510         if (!region_obj) {
511                 return_ACPI_STATUS(AE_BAD_PARAMETER);
512         }
513
514         if (region_obj->common.flags & AOPOBJ_OBJECT_INITIALIZED) {
515                 return_ACPI_STATUS(AE_OK);
516         }
517
518         region_obj->common.flags |= AOPOBJ_OBJECT_INITIALIZED;
519
520         node = region_obj->region.node->parent;
521         space_id = region_obj->region.space_id;
522
523         /*
524          * The following loop depends upon the root Node having no parent
525          * ie: acpi_gbl_root_node->Parent being set to NULL
526          */
527         while (node) {
528
529                 /* Check to see if a handler exists */
530
531                 handler_obj = NULL;
532                 obj_desc = acpi_ns_get_attached_object(node);
533                 if (obj_desc) {
534
535                         /* Can only be a handler if the object exists */
536
537                         switch (node->type) {
538                         case ACPI_TYPE_DEVICE:
539                         case ACPI_TYPE_PROCESSOR:
540                         case ACPI_TYPE_THERMAL:
541
542                                 handler_obj = obj_desc->common_notify.handler;
543                                 break;
544
545                         case ACPI_TYPE_METHOD:
546                                 /*
547                                  * If we are executing module level code, the original
548                                  * Node's object was replaced by this Method object and we
549                                  * saved the handler in the method object.
550                                  *
551                                  * See acpi_ns_exec_module_code
552                                  */
553                                 if (!acpi_gbl_parse_table_as_term_list &&
554                                     obj_desc->method.
555                                     info_flags & ACPI_METHOD_MODULE_LEVEL) {
556                                         handler_obj =
557                                             obj_desc->method.dispatch.handler;
558                                 }
559                                 break;
560
561                         default:
562
563                                 /* Ignore other objects */
564
565                                 break;
566                         }
567
568                         handler_obj =
569                             acpi_ev_find_region_handler(space_id, handler_obj);
570                         if (handler_obj) {
571
572                                 /* Found correct handler */
573
574                                 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
575                                                   "Found handler %p for region %p in obj %p\n",
576                                                   handler_obj, region_obj,
577                                                   obj_desc));
578
579                                 status =
580                                     acpi_ev_attach_region(handler_obj,
581                                                           region_obj,
582                                                           acpi_ns_locked);
583
584                                 /*
585                                  * Tell all users that this region is usable by
586                                  * running the _REG method
587                                  */
588                                 if (acpi_ns_locked) {
589                                         status =
590                                             acpi_ut_release_mutex
591                                             (ACPI_MTX_NAMESPACE);
592                                         if (ACPI_FAILURE(status)) {
593                                                 return_ACPI_STATUS(status);
594                                         }
595                                 }
596
597                                 acpi_ex_exit_interpreter();
598                                 status =
599                                     acpi_ev_execute_reg_method(region_obj,
600                                                                ACPI_REG_CONNECT);
601                                 acpi_ex_enter_interpreter();
602
603                                 if (acpi_ns_locked) {
604                                         status =
605                                             acpi_ut_acquire_mutex
606                                             (ACPI_MTX_NAMESPACE);
607                                         if (ACPI_FAILURE(status)) {
608                                                 return_ACPI_STATUS(status);
609                                         }
610                                 }
611
612                                 return_ACPI_STATUS(AE_OK);
613                         }
614                 }
615
616                 /* This node does not have the handler we need; Pop up one level */
617
618                 node = node->parent;
619         }
620
621         /* If we get here, there is no handler for this region */
622
623         ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
624                           "No handler for RegionType %s(%X) (RegionObj %p)\n",
625                           acpi_ut_get_region_name(space_id), space_id,
626                           region_obj));
627
628         return_ACPI_STATUS(AE_NOT_EXIST);
629 }