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