]>
Commit | Line | Data |
---|---|---|
95857638 | 1 | // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 |
1da177e4 LT |
2 | /****************************************************************************** |
3 | * | |
42f8fb75 | 4 | * Module Name: evregion - Operation Region support |
1da177e4 | 5 | * |
612c2932 | 6 | * Copyright (C) 2000 - 2023, Intel Corp. |
1da177e4 | 7 | * |
95857638 | 8 | *****************************************************************************/ |
1da177e4 | 9 | |
1da177e4 | 10 | #include <acpi/acpi.h> |
e2f7a777 LB |
11 | #include "accommon.h" |
12 | #include "acevents.h" | |
13 | #include "acnamesp.h" | |
14 | #include "acinterp.h" | |
1da177e4 LT |
15 | |
16 | #define _COMPONENT ACPI_EVENTS | |
4be44fcd | 17 | ACPI_MODULE_NAME("evregion") |
1da177e4 | 18 | |
42f8fb75 BM |
19 | extern u8 acpi_gbl_default_address_spaces[]; |
20 | ||
44f6c012 | 21 | /* Local prototypes */ |
74d3ec77 | 22 | |
779bac99 RW |
23 | static void |
24 | acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node, | |
25 | acpi_adr_space_type space_id); | |
26 | ||
44f6c012 | 27 | static acpi_status |
4be44fcd LB |
28 | acpi_ev_reg_run(acpi_handle obj_handle, |
29 | u32 level, void *context, void **return_value); | |
44f6c012 | 30 | |
1da177e4 LT |
31 | /******************************************************************************* |
32 | * | |
33 | * FUNCTION: acpi_ev_initialize_op_regions | |
34 | * | |
35 | * PARAMETERS: None | |
36 | * | |
37 | * RETURN: Status | |
38 | * | |
39 | * DESCRIPTION: Execute _REG methods for all Operation Regions that have | |
40 | * an installed default region handler. | |
41 | * | |
42 | ******************************************************************************/ | |
43 | ||
4be44fcd | 44 | acpi_status acpi_ev_initialize_op_regions(void) |
1da177e4 | 45 | { |
4be44fcd | 46 | acpi_status status; |
67a119f9 | 47 | u32 i; |
1da177e4 | 48 | |
b229cf92 | 49 | ACPI_FUNCTION_TRACE(ev_initialize_op_regions); |
1da177e4 | 50 | |
4be44fcd LB |
51 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
52 | if (ACPI_FAILURE(status)) { | |
53 | return_ACPI_STATUS(status); | |
1da177e4 LT |
54 | } |
55 | ||
9f15fc66 | 56 | /* Run the _REG methods for op_regions in each default address space */ |
52fc0b02 | 57 | |
9f15fc66 BM |
58 | for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) { |
59 | /* | |
74d3ec77 LM |
60 | * Make sure the installed handler is the DEFAULT handler. If not the |
61 | * default, the _REG methods will have already been run (when the | |
62 | * handler was installed) | |
1da177e4 | 63 | */ |
74d3ec77 LM |
64 | if (acpi_ev_has_default_handler(acpi_gbl_root_node, |
65 | acpi_gbl_default_address_spaces | |
66 | [i])) { | |
d815346f | 67 | acpi_ev_execute_reg_methods(acpi_gbl_root_node, |
cdf65d73 | 68 | ACPI_UINT32_MAX, |
d815346f LZ |
69 | acpi_gbl_default_address_spaces |
70 | [i], ACPI_REG_CONNECT); | |
74d3ec77 | 71 | } |
1da177e4 LT |
72 | } |
73 | ||
4be44fcd LB |
74 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); |
75 | return_ACPI_STATUS(status); | |
1da177e4 LT |
76 | } |
77 | ||
1da177e4 LT |
78 | /******************************************************************************* |
79 | * | |
80 | * FUNCTION: acpi_ev_address_space_dispatch | |
81 | * | |
82 | * PARAMETERS: region_obj - Internal region object | |
9ce81784 | 83 | * field_obj - Corresponding field. Can be NULL. |
ba494bee | 84 | * function - Read or Write operation |
f5407af3 | 85 | * region_offset - Where in the region to read or write |
1da177e4 | 86 | * bit_width - Field width in bits (8, 16, 32, or 64) |
ba494bee | 87 | * value - Pointer to in or out value, must be |
5df7e6cb | 88 | * a full 64-bit integer |
1da177e4 LT |
89 | * |
90 | * RETURN: Status | |
91 | * | |
92 | * DESCRIPTION: Dispatch an address space or operation region access to | |
93 | * a previously installed handler. | |
94 | * | |
7b738064 BM |
95 | * NOTE: During early initialization, we always install the default region |
96 | * handlers for Memory, I/O and PCI_Config. This ensures that these operation | |
97 | * region address spaces are always available as per the ACPI specification. | |
98 | * This is especially needed in order to support the execution of | |
99 | * module-level AML code during loading of the ACPI tables. | |
100 | * | |
1da177e4 LT |
101 | ******************************************************************************/ |
102 | ||
103 | acpi_status | |
4be44fcd | 104 | acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj, |
9ce81784 | 105 | union acpi_operand_object *field_obj, |
4be44fcd | 106 | u32 function, |
5df7e6cb | 107 | u32 region_offset, u32 bit_width, u64 *value) |
1da177e4 | 108 | { |
4be44fcd | 109 | acpi_status status; |
4be44fcd LB |
110 | acpi_adr_space_handler handler; |
111 | acpi_adr_space_setup region_setup; | |
112 | union acpi_operand_object *handler_desc; | |
113 | union acpi_operand_object *region_obj2; | |
114 | void *region_context = NULL; | |
9ce81784 | 115 | struct acpi_connection_info *context; |
c27f3d01 HG |
116 | acpi_mutex context_mutex; |
117 | u8 context_locked; | |
75ec6e55 | 118 | acpi_physical_address address; |
1da177e4 | 119 | |
b229cf92 | 120 | ACPI_FUNCTION_TRACE(ev_address_space_dispatch); |
1da177e4 | 121 | |
4be44fcd | 122 | region_obj2 = acpi_ns_get_secondary_object(region_obj); |
1da177e4 | 123 | if (!region_obj2) { |
4be44fcd | 124 | return_ACPI_STATUS(AE_NOT_EXIST); |
1da177e4 LT |
125 | } |
126 | ||
127 | /* Ensure that there is a handler associated with this region */ | |
128 | ||
129 | handler_desc = region_obj->region.handler; | |
130 | if (!handler_desc) { | |
b8e4d893 BM |
131 | ACPI_ERROR((AE_INFO, |
132 | "No handler for Region [%4.4s] (%p) [%s]", | |
133 | acpi_ut_get_node_name(region_obj->region.node), | |
134 | region_obj, | |
135 | acpi_ut_get_region_name(region_obj->region. | |
136 | space_id))); | |
4be44fcd LB |
137 | |
138 | return_ACPI_STATUS(AE_NOT_EXIST); | |
1da177e4 LT |
139 | } |
140 | ||
9ce81784 | 141 | context = handler_desc->address_space.context; |
c27f3d01 HG |
142 | context_mutex = handler_desc->address_space.context_mutex; |
143 | context_locked = FALSE; | |
9ce81784 | 144 | |
1da177e4 | 145 | /* |
9f15fc66 | 146 | * It may be the case that the region has never been initialized. |
1da177e4 LT |
147 | * Some types of regions require special init code |
148 | */ | |
149 | if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) { | |
9f15fc66 BM |
150 | |
151 | /* This region has not been initialized yet, do it */ | |
152 | ||
1da177e4 LT |
153 | region_setup = handler_desc->address_space.setup; |
154 | if (!region_setup) { | |
52fc0b02 | 155 | |
1da177e4 LT |
156 | /* No initialization routine, exit with error */ |
157 | ||
b8e4d893 BM |
158 | ACPI_ERROR((AE_INFO, |
159 | "No init routine for region(%p) [%s]", | |
160 | region_obj, | |
161 | acpi_ut_get_region_name(region_obj->region. | |
162 | space_id))); | |
4be44fcd | 163 | return_ACPI_STATUS(AE_NOT_EXIST); |
1da177e4 LT |
164 | } |
165 | ||
0acf24ad SH |
166 | if (region_obj->region.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) { |
167 | struct acpi_pcc_info *ctx = | |
168 | handler_desc->address_space.context; | |
169 | ||
170 | ctx->internal_buffer = | |
171 | field_obj->field.internal_pcc_buffer; | |
b70d6f07 BM |
172 | ctx->length = (u16)region_obj->region.length; |
173 | ctx->subspace_id = (u8)region_obj->region.address; | |
0acf24ad SH |
174 | } |
175 | ||
ee64b827 SH |
176 | if (region_obj->region.space_id == |
177 | ACPI_ADR_SPACE_FIXED_HARDWARE) { | |
178 | struct acpi_ffh_info *ctx = | |
179 | handler_desc->address_space.context; | |
180 | ||
181 | ctx->length = region_obj->region.length; | |
182 | ctx->offset = region_obj->region.address; | |
183 | } | |
184 | ||
1da177e4 | 185 | /* |
9f15fc66 BM |
186 | * We must exit the interpreter because the region setup will |
187 | * potentially execute control methods (for example, the _REG method | |
188 | * for this region) | |
1da177e4 | 189 | */ |
014d433f | 190 | acpi_ex_exit_interpreter(); |
1da177e4 | 191 | |
4be44fcd | 192 | status = region_setup(region_obj, ACPI_REGION_ACTIVATE, |
9ce81784 | 193 | context, ®ion_context); |
1da177e4 LT |
194 | |
195 | /* Re-enter the interpreter */ | |
196 | ||
014d433f | 197 | acpi_ex_enter_interpreter(); |
1da177e4 LT |
198 | |
199 | /* Check for failure of the Region Setup */ | |
200 | ||
4be44fcd | 201 | if (ACPI_FAILURE(status)) { |
b8e4d893 BM |
202 | ACPI_EXCEPTION((AE_INFO, status, |
203 | "During region initialization: [%s]", | |
204 | acpi_ut_get_region_name(region_obj-> | |
205 | region. | |
206 | space_id))); | |
4be44fcd | 207 | return_ACPI_STATUS(status); |
1da177e4 LT |
208 | } |
209 | ||
9f15fc66 BM |
210 | /* Region initialization may have been completed by region_setup */ |
211 | ||
1da177e4 LT |
212 | if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) { |
213 | region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE; | |
214 | ||
d2e7d079 DB |
215 | /* |
216 | * Save the returned context for use in all accesses to | |
217 | * the handler for this particular region | |
218 | */ | |
219 | if (!(region_obj2->extra.region_context)) { | |
4be44fcd LB |
220 | region_obj2->extra.region_context = |
221 | region_context; | |
1da177e4 LT |
222 | } |
223 | } | |
224 | } | |
225 | ||
226 | /* We have everything we need, we can invoke the address space handler */ | |
227 | ||
228 | handler = handler_desc->address_space.handler; | |
75ec6e55 | 229 | address = (region_obj->region.address + region_offset); |
1da177e4 | 230 | |
c27f3d01 HG |
231 | ACPI_DEBUG_PRINT((ACPI_DB_OPREGION, |
232 | "Handler %p (@%p) Address %8.8X%8.8X [%s]\n", | |
233 | ®ion_obj->region.handler->address_space, handler, | |
234 | ACPI_FORMAT_UINT64(address), | |
235 | acpi_ut_get_region_name(region_obj->region. | |
236 | space_id))); | |
237 | ||
238 | if (!(handler_desc->address_space.handler_flags & | |
239 | ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) { | |
240 | /* | |
241 | * For handlers other than the default (supplied) handlers, we must | |
242 | * exit the interpreter because the handler *might* block -- we don't | |
243 | * know what it will do, so we can't hold the lock on the interpreter. | |
244 | */ | |
245 | acpi_ex_exit_interpreter(); | |
246 | } | |
247 | ||
9ce81784 BM |
248 | /* |
249 | * Special handling for generic_serial_bus and general_purpose_io: | |
250 | * There are three extra parameters that must be passed to the | |
251 | * handler via the context: | |
75ec6e55 BM |
252 | * 1) Connection buffer, a resource template from Connection() op |
253 | * 2) Length of the above buffer | |
254 | * 3) Actual access length from the access_as() op | |
255 | * | |
c27f3d01 HG |
256 | * Since we pass these extra parameters via the context, which is |
257 | * shared between threads, we must lock the context to avoid these | |
258 | * parameters being changed from another thread before the handler | |
259 | * has completed running. | |
260 | * | |
75ec6e55 BM |
261 | * In addition, for general_purpose_io, the Address and bit_width fields |
262 | * are defined as follows: | |
263 | * 1) Address is the pin number index of the field (bit offset from | |
264 | * the previous Connection) | |
265 | * 2) bit_width is the actual bit length of the field (number of pins) | |
9ce81784 | 266 | */ |
8f6493d1 HG |
267 | if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS || |
268 | region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) && | |
75ec6e55 BM |
269 | context && field_obj) { |
270 | ||
c27f3d01 HG |
271 | status = |
272 | acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER); | |
273 | if (ACPI_FAILURE(status)) { | |
274 | goto re_enter_interpreter; | |
275 | } | |
276 | ||
277 | context_locked = TRUE; | |
278 | ||
75ec6e55 BM |
279 | /* Get the Connection (resource_template) buffer */ |
280 | ||
281 | context->connection = field_obj->field.resource_buffer; | |
282 | context->length = field_obj->field.resource_length; | |
283 | context->access_length = field_obj->field.access_length; | |
9ce81784 | 284 | |
8f6493d1 HG |
285 | if (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) { |
286 | address = field_obj->field.pin_number_index; | |
287 | bit_width = field_obj->field.bit_length; | |
c27f3d01 | 288 | } |
9ce81784 BM |
289 | } |
290 | ||
1da177e4 LT |
291 | /* Call the handler */ |
292 | ||
75ec6e55 | 293 | status = handler(function, address, bit_width, value, context, |
1da177e4 LT |
294 | region_obj2->extra.region_context); |
295 | ||
c27f3d01 HG |
296 | if (context_locked) { |
297 | acpi_os_release_mutex(context_mutex); | |
298 | } | |
299 | ||
4be44fcd | 300 | if (ACPI_FAILURE(status)) { |
b8e4d893 BM |
301 | ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]", |
302 | acpi_ut_get_region_name(region_obj->region. | |
303 | space_id))); | |
53ae81e1 BM |
304 | |
305 | /* | |
306 | * Special case for an EC timeout. These are seen so frequently | |
307 | * that an additional error message is helpful | |
308 | */ | |
309 | if ((region_obj->region.space_id == ACPI_ADR_SPACE_EC) && | |
310 | (status == AE_TIME)) { | |
311 | ACPI_ERROR((AE_INFO, | |
312 | "Timeout from EC hardware or EC device driver")); | |
313 | } | |
1da177e4 LT |
314 | } |
315 | ||
c27f3d01 | 316 | re_enter_interpreter: |
61686124 BM |
317 | if (!(handler_desc->address_space.handler_flags & |
318 | ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) { | |
1da177e4 LT |
319 | /* |
320 | * We just returned from a non-default handler, we must re-enter the | |
321 | * interpreter | |
322 | */ | |
014d433f | 323 | acpi_ex_enter_interpreter(); |
1da177e4 LT |
324 | } |
325 | ||
4be44fcd | 326 | return_ACPI_STATUS(status); |
1da177e4 LT |
327 | } |
328 | ||
1da177e4 LT |
329 | /******************************************************************************* |
330 | * | |
331 | * FUNCTION: acpi_ev_detach_region | |
332 | * | |
333 | * PARAMETERS: region_obj - Region Object | |
334 | * acpi_ns_is_locked - Namespace Region Already Locked? | |
335 | * | |
336 | * RETURN: None | |
337 | * | |
338 | * DESCRIPTION: Break the association between the handler and the region | |
339 | * this is a two way association. | |
340 | * | |
341 | ******************************************************************************/ | |
342 | ||
343 | void | |
4be44fcd LB |
344 | acpi_ev_detach_region(union acpi_operand_object *region_obj, |
345 | u8 acpi_ns_is_locked) | |
1da177e4 | 346 | { |
4be44fcd LB |
347 | union acpi_operand_object *handler_obj; |
348 | union acpi_operand_object *obj_desc; | |
f953529f | 349 | union acpi_operand_object *start_desc; |
4be44fcd LB |
350 | union acpi_operand_object **last_obj_ptr; |
351 | acpi_adr_space_setup region_setup; | |
352 | void **region_context; | |
353 | union acpi_operand_object *region_obj2; | |
354 | acpi_status status; | |
1da177e4 | 355 | |
b229cf92 | 356 | ACPI_FUNCTION_TRACE(ev_detach_region); |
1da177e4 | 357 | |
4be44fcd | 358 | region_obj2 = acpi_ns_get_secondary_object(region_obj); |
1da177e4 LT |
359 | if (!region_obj2) { |
360 | return_VOID; | |
361 | } | |
362 | region_context = ®ion_obj2->extra.region_context; | |
363 | ||
364 | /* Get the address handler from the region object */ | |
365 | ||
366 | handler_obj = region_obj->region.handler; | |
367 | if (!handler_obj) { | |
52fc0b02 | 368 | |
1da177e4 LT |
369 | /* This region has no handler, all done */ |
370 | ||
371 | return_VOID; | |
372 | } | |
373 | ||
374 | /* Find this region in the handler's list */ | |
375 | ||
376 | obj_desc = handler_obj->address_space.region_list; | |
f953529f | 377 | start_desc = obj_desc; |
1da177e4 LT |
378 | last_obj_ptr = &handler_obj->address_space.region_list; |
379 | ||
380 | while (obj_desc) { | |
52fc0b02 | 381 | |
1da177e4 LT |
382 | /* Is this the correct Region? */ |
383 | ||
384 | if (obj_desc == region_obj) { | |
4be44fcd LB |
385 | ACPI_DEBUG_PRINT((ACPI_DB_OPREGION, |
386 | "Removing Region %p from address handler %p\n", | |
387 | region_obj, handler_obj)); | |
1da177e4 LT |
388 | |
389 | /* This is it, remove it from the handler's list */ | |
390 | ||
391 | *last_obj_ptr = obj_desc->region.next; | |
4be44fcd | 392 | obj_desc->region.next = NULL; /* Must clear field */ |
1da177e4 LT |
393 | |
394 | if (acpi_ns_is_locked) { | |
4be44fcd LB |
395 | status = |
396 | acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); | |
397 | if (ACPI_FAILURE(status)) { | |
1da177e4 LT |
398 | return_VOID; |
399 | } | |
400 | } | |
401 | ||
402 | /* Now stop region accesses by executing the _REG method */ | |
403 | ||
e2066ca1 BM |
404 | status = |
405 | acpi_ev_execute_reg_method(region_obj, | |
406 | ACPI_REG_DISCONNECT); | |
4be44fcd | 407 | if (ACPI_FAILURE(status)) { |
b8e4d893 BM |
408 | ACPI_EXCEPTION((AE_INFO, status, |
409 | "from region _REG, [%s]", | |
410 | acpi_ut_get_region_name | |
411 | (region_obj->region.space_id))); | |
1da177e4 LT |
412 | } |
413 | ||
414 | if (acpi_ns_is_locked) { | |
4be44fcd LB |
415 | status = |
416 | acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); | |
417 | if (ACPI_FAILURE(status)) { | |
1da177e4 LT |
418 | return_VOID; |
419 | } | |
420 | } | |
421 | ||
f6dd9221 | 422 | /* |
9f15fc66 BM |
423 | * If the region has been activated, call the setup handler with |
424 | * the deactivate notification | |
f6dd9221 BM |
425 | */ |
426 | if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) { | |
427 | region_setup = handler_obj->address_space.setup; | |
428 | status = | |
429 | region_setup(region_obj, | |
430 | ACPI_REGION_DEACTIVATE, | |
431 | handler_obj->address_space. | |
432 | context, region_context); | |
1da177e4 | 433 | |
d2e7d079 DB |
434 | /* |
435 | * region_context should have been released by the deactivate | |
436 | * operation. We don't need access to it anymore here. | |
437 | */ | |
438 | if (region_context) { | |
439 | *region_context = NULL; | |
440 | } | |
441 | ||
f6dd9221 | 442 | /* Init routine may fail, Just ignore errors */ |
1da177e4 | 443 | |
f6dd9221 BM |
444 | if (ACPI_FAILURE(status)) { |
445 | ACPI_EXCEPTION((AE_INFO, status, | |
446 | "from region handler - deactivate, [%s]", | |
447 | acpi_ut_get_region_name | |
448 | (region_obj->region. | |
449 | space_id))); | |
450 | } | |
1da177e4 | 451 | |
f6dd9221 BM |
452 | region_obj->region.flags &= |
453 | ~(AOPOBJ_SETUP_COMPLETE); | |
1da177e4 LT |
454 | } |
455 | ||
1da177e4 LT |
456 | /* |
457 | * Remove handler reference in the region | |
458 | * | |
f6dd9221 BM |
459 | * NOTE: this doesn't mean that the region goes away, the region |
460 | * is just inaccessible as indicated to the _REG method | |
1da177e4 | 461 | * |
f6dd9221 BM |
462 | * If the region is on the handler's list, this must be the |
463 | * region's handler | |
1da177e4 LT |
464 | */ |
465 | region_obj->region.handler = NULL; | |
4be44fcd | 466 | acpi_ut_remove_reference(handler_obj); |
1da177e4 LT |
467 | |
468 | return_VOID; | |
469 | } | |
470 | ||
471 | /* Walk the linked list of handlers */ | |
472 | ||
473 | last_obj_ptr = &obj_desc->region.next; | |
474 | obj_desc = obj_desc->region.next; | |
f953529f BM |
475 | |
476 | /* Prevent infinite loop if list is corrupted */ | |
477 | ||
478 | if (obj_desc == start_desc) { | |
479 | ACPI_ERROR((AE_INFO, | |
480 | "Circular handler list in region object %p", | |
481 | region_obj)); | |
482 | return_VOID; | |
483 | } | |
1da177e4 LT |
484 | } |
485 | ||
486 | /* If we get here, the region was not in the handler's region list */ | |
487 | ||
4be44fcd LB |
488 | ACPI_DEBUG_PRINT((ACPI_DB_OPREGION, |
489 | "Cannot remove region %p from address handler %p\n", | |
490 | region_obj, handler_obj)); | |
1da177e4 LT |
491 | |
492 | return_VOID; | |
493 | } | |
494 | ||
1da177e4 LT |
495 | /******************************************************************************* |
496 | * | |
497 | * FUNCTION: acpi_ev_attach_region | |
498 | * | |
499 | * PARAMETERS: handler_obj - Handler Object | |
500 | * region_obj - Region Object | |
501 | * acpi_ns_is_locked - Namespace Region Already Locked? | |
502 | * | |
503 | * RETURN: None | |
504 | * | |
505 | * DESCRIPTION: Create the association between the handler and the region | |
506 | * this is a two way association. | |
507 | * | |
508 | ******************************************************************************/ | |
509 | ||
510 | acpi_status | |
4be44fcd LB |
511 | acpi_ev_attach_region(union acpi_operand_object *handler_obj, |
512 | union acpi_operand_object *region_obj, | |
513 | u8 acpi_ns_is_locked) | |
1da177e4 LT |
514 | { |
515 | ||
b229cf92 | 516 | ACPI_FUNCTION_TRACE(ev_attach_region); |
1da177e4 | 517 | |
1d65d9a7 LZ |
518 | /* Install the region's handler */ |
519 | ||
520 | if (region_obj->region.handler) { | |
521 | return_ACPI_STATUS(AE_ALREADY_EXISTS); | |
522 | } | |
523 | ||
4be44fcd LB |
524 | ACPI_DEBUG_PRINT((ACPI_DB_OPREGION, |
525 | "Adding Region [%4.4s] %p to address handler %p [%s]\n", | |
526 | acpi_ut_get_node_name(region_obj->region.node), | |
527 | region_obj, handler_obj, | |
528 | acpi_ut_get_region_name(region_obj->region. | |
529 | space_id))); | |
1da177e4 LT |
530 | |
531 | /* Link this region to the front of the handler's list */ | |
532 | ||
533 | region_obj->region.next = handler_obj->address_space.region_list; | |
534 | handler_obj->address_space.region_list = region_obj; | |
1da177e4 | 535 | region_obj->region.handler = handler_obj; |
4be44fcd | 536 | acpi_ut_add_reference(handler_obj); |
1da177e4 | 537 | |
4be44fcd | 538 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
539 | } |
540 | ||
849c2571 LZ |
541 | /******************************************************************************* |
542 | * | |
d1461a1b | 543 | * FUNCTION: acpi_ev_execute_reg_method |
849c2571 LZ |
544 | * |
545 | * PARAMETERS: region_obj - Region object | |
d1461a1b | 546 | * function - Passed to _REG: On (1) or Off (0) |
849c2571 LZ |
547 | * |
548 | * RETURN: Status | |
549 | * | |
d1461a1b | 550 | * DESCRIPTION: Execute _REG method for a region |
849c2571 LZ |
551 | * |
552 | ******************************************************************************/ | |
553 | ||
d1461a1b LZ |
554 | acpi_status |
555 | acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function) | |
849c2571 | 556 | { |
d1461a1b LZ |
557 | struct acpi_evaluate_info *info; |
558 | union acpi_operand_object *args[3]; | |
559 | union acpi_operand_object *region_obj2; | |
0dfaaa3d BM |
560 | const acpi_name *reg_name_ptr = |
561 | ACPI_CAST_PTR(acpi_name, METHOD_NAME__REG); | |
849c2571 LZ |
562 | struct acpi_namespace_node *method_node; |
563 | struct acpi_namespace_node *node; | |
849c2571 LZ |
564 | acpi_status status; |
565 | ||
d1461a1b LZ |
566 | ACPI_FUNCTION_TRACE(ev_execute_reg_method); |
567 | ||
568 | if (!acpi_gbl_namespace_initialized || | |
569 | region_obj->region.handler == NULL) { | |
570 | return_ACPI_STATUS(AE_OK); | |
571 | } | |
849c2571 LZ |
572 | |
573 | region_obj2 = acpi_ns_get_secondary_object(region_obj); | |
574 | if (!region_obj2) { | |
d1461a1b | 575 | return_ACPI_STATUS(AE_NOT_EXIST); |
849c2571 LZ |
576 | } |
577 | ||
d1461a1b LZ |
578 | /* |
579 | * Find any "_REG" method associated with this region definition. | |
580 | * The method should always be updated as this function may be | |
581 | * invoked after a namespace change. | |
582 | */ | |
849c2571 | 583 | node = region_obj->region.node->parent; |
849c2571 LZ |
584 | status = |
585 | acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD, | |
586 | &method_node); | |
587 | if (ACPI_SUCCESS(status)) { | |
588 | /* | |
d1461a1b LZ |
589 | * The _REG method is optional and there can be only one per |
590 | * region definition. This will be executed when the handler is | |
591 | * attached or removed. | |
849c2571 LZ |
592 | */ |
593 | region_obj2->extra.method_REG = method_node; | |
594 | } | |
d1461a1b | 595 | if (region_obj2->extra.method_REG == NULL) { |
efaed9be LZ |
596 | return_ACPI_STATUS(AE_OK); |
597 | } | |
598 | ||
599 | /* _REG(DISCONNECT) should be paired with _REG(CONNECT) */ | |
600 | ||
601 | if ((function == ACPI_REG_CONNECT && | |
602 | region_obj->common.flags & AOPOBJ_REG_CONNECTED) || | |
603 | (function == ACPI_REG_DISCONNECT && | |
604 | !(region_obj->common.flags & AOPOBJ_REG_CONNECTED))) { | |
42f8fb75 | 605 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
606 | } |
607 | ||
42f8fb75 | 608 | /* Allocate and initialize the evaluation information block */ |
1da177e4 | 609 | |
42f8fb75 BM |
610 | info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info)); |
611 | if (!info) { | |
612 | return_ACPI_STATUS(AE_NO_MEMORY); | |
1da177e4 LT |
613 | } |
614 | ||
42f8fb75 | 615 | info->prefix_node = region_obj2->extra.method_REG; |
29a241cc | 616 | info->relative_pathname = NULL; |
42f8fb75 BM |
617 | info->parameters = args; |
618 | info->flags = ACPI_IGNORE_RETURN_VALUE; | |
1da177e4 LT |
619 | |
620 | /* | |
42f8fb75 BM |
621 | * The _REG method has two arguments: |
622 | * | |
623 | * arg0 - Integer: | |
624 | * Operation region space ID Same value as region_obj->Region.space_id | |
1da177e4 | 625 | * |
42f8fb75 BM |
626 | * arg1 - Integer: |
627 | * connection status 1 for connecting the handler, 0 for disconnecting | |
628 | * the handler (Passed as a parameter) | |
1da177e4 | 629 | */ |
42f8fb75 BM |
630 | args[0] = |
631 | acpi_ut_create_integer_object((u64)region_obj->region.space_id); | |
632 | if (!args[0]) { | |
1da177e4 | 633 | status = AE_NO_MEMORY; |
42f8fb75 | 634 | goto cleanup1; |
1da177e4 LT |
635 | } |
636 | ||
42f8fb75 BM |
637 | args[1] = acpi_ut_create_integer_object((u64)function); |
638 | if (!args[1]) { | |
639 | status = AE_NO_MEMORY; | |
640 | goto cleanup2; | |
641 | } | |
1da177e4 | 642 | |
42f8fb75 | 643 | args[2] = NULL; /* Terminate list */ |
1da177e4 | 644 | |
42f8fb75 | 645 | /* Execute the method, no return value */ |
1da177e4 | 646 | |
42f8fb75 BM |
647 | ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname |
648 | (ACPI_TYPE_METHOD, info->prefix_node, NULL)); | |
1da177e4 | 649 | |
42f8fb75 BM |
650 | status = acpi_ns_evaluate(info); |
651 | acpi_ut_remove_reference(args[1]); | |
1da177e4 | 652 | |
efaed9be LZ |
653 | if (ACPI_FAILURE(status)) { |
654 | goto cleanup2; | |
655 | } | |
656 | ||
657 | if (function == ACPI_REG_CONNECT) { | |
658 | region_obj->common.flags |= AOPOBJ_REG_CONNECTED; | |
659 | } else { | |
660 | region_obj->common.flags &= ~AOPOBJ_REG_CONNECTED; | |
661 | } | |
662 | ||
10622bf8 | 663 | cleanup2: |
42f8fb75 | 664 | acpi_ut_remove_reference(args[0]); |
1da177e4 | 665 | |
10622bf8 | 666 | cleanup1: |
42f8fb75 | 667 | ACPI_FREE(info); |
4be44fcd | 668 | return_ACPI_STATUS(status); |
1da177e4 LT |
669 | } |
670 | ||
1da177e4 LT |
671 | /******************************************************************************* |
672 | * | |
673 | * FUNCTION: acpi_ev_execute_reg_methods | |
674 | * | |
ba494bee | 675 | * PARAMETERS: node - Namespace node for the device |
cdf65d73 | 676 | * max_depth - Depth to which search for _REG |
1da177e4 | 677 | * space_id - The address space ID |
d815346f | 678 | * function - Passed to _REG: On (1) or Off (0) |
1da177e4 | 679 | * |
d815346f | 680 | * RETURN: None |
1da177e4 LT |
681 | * |
682 | * DESCRIPTION: Run all _REG methods for the input Space ID; | |
683 | * Note: assumes namespace is locked, or system init time. | |
684 | * | |
685 | ******************************************************************************/ | |
686 | ||
d815346f | 687 | void |
cdf65d73 | 688 | acpi_ev_execute_reg_methods(struct acpi_namespace_node *node, u32 max_depth, |
d815346f | 689 | acpi_adr_space_type space_id, u32 function) |
1da177e4 | 690 | { |
25823e78 | 691 | struct acpi_reg_walk_info info; |
1da177e4 | 692 | |
b229cf92 | 693 | ACPI_FUNCTION_TRACE(ev_execute_reg_methods); |
1da177e4 | 694 | |
8b1cafdc BM |
695 | /* |
696 | * These address spaces do not need a call to _REG, since the ACPI | |
697 | * specification defines them as: "must always be accessible". Since | |
698 | * they never change state (never become unavailable), no need to ever | |
699 | * call _REG on them. Also, a data_table is not a "real" address space, | |
700 | * so do not call _REG. September 2018. | |
701 | */ | |
702 | if ((space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) || | |
703 | (space_id == ACPI_ADR_SPACE_SYSTEM_IO) || | |
704 | (space_id == ACPI_ADR_SPACE_DATA_TABLE)) { | |
705 | return_VOID; | |
706 | } | |
707 | ||
25823e78 | 708 | info.space_id = space_id; |
d815346f | 709 | info.function = function; |
25823e78 BM |
710 | info.reg_run_count = 0; |
711 | ||
712 | ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES, | |
713 | " Running _REG methods for SpaceId %s\n", | |
714 | acpi_ut_get_region_name(info.space_id))); | |
715 | ||
1da177e4 | 716 | /* |
9f15fc66 BM |
717 | * Run all _REG methods for all Operation Regions for this space ID. This |
718 | * is a separate walk in order to handle any interdependencies between | |
719 | * regions and _REG methods. (i.e. handlers must be installed for all | |
720 | * regions of this Space ID before we can run any _REG methods) | |
1da177e4 | 721 | */ |
cdf65d73 | 722 | (void)acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, max_depth, |
d815346f LZ |
723 | ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run, NULL, |
724 | &info, NULL); | |
1da177e4 | 725 | |
0306f035 HG |
726 | /* |
727 | * Special case for EC and GPIO: handle "orphan" _REG methods with | |
728 | * no region. | |
729 | */ | |
730 | if (space_id == ACPI_ADR_SPACE_EC || space_id == ACPI_ADR_SPACE_GPIO) { | |
731 | acpi_ev_execute_orphan_reg_method(node, space_id); | |
e2066ca1 BM |
732 | } |
733 | ||
25823e78 BM |
734 | ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES, |
735 | " Executed %u _REG methods for SpaceId %s\n", | |
736 | info.reg_run_count, | |
737 | acpi_ut_get_region_name(info.space_id))); | |
738 | ||
d815346f | 739 | return_VOID; |
1da177e4 LT |
740 | } |
741 | ||
1da177e4 LT |
742 | /******************************************************************************* |
743 | * | |
744 | * FUNCTION: acpi_ev_reg_run | |
745 | * | |
746 | * PARAMETERS: walk_namespace callback | |
747 | * | |
ba494bee | 748 | * DESCRIPTION: Run _REG method for region objects of the requested spaceID |
1da177e4 LT |
749 | * |
750 | ******************************************************************************/ | |
751 | ||
44f6c012 | 752 | static acpi_status |
4be44fcd LB |
753 | acpi_ev_reg_run(acpi_handle obj_handle, |
754 | u32 level, void *context, void **return_value) | |
1da177e4 | 755 | { |
4be44fcd LB |
756 | union acpi_operand_object *obj_desc; |
757 | struct acpi_namespace_node *node; | |
4be44fcd | 758 | acpi_status status; |
25823e78 | 759 | struct acpi_reg_walk_info *info; |
1da177e4 | 760 | |
25823e78 | 761 | info = ACPI_CAST_PTR(struct acpi_reg_walk_info, context); |
1da177e4 LT |
762 | |
763 | /* Convert and validate the device handle */ | |
764 | ||
f24b664d | 765 | node = acpi_ns_validate_handle(obj_handle); |
1da177e4 LT |
766 | if (!node) { |
767 | return (AE_BAD_PARAMETER); | |
768 | } | |
769 | ||
770 | /* | |
8b1cafdc BM |
771 | * We only care about regions and objects that are allowed to have |
772 | * address space handlers | |
1da177e4 | 773 | */ |
4be44fcd | 774 | if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) { |
1da177e4 LT |
775 | return (AE_OK); |
776 | } | |
777 | ||
778 | /* Check for an existing internal object */ | |
779 | ||
4be44fcd | 780 | obj_desc = acpi_ns_get_attached_object(node); |
1da177e4 | 781 | if (!obj_desc) { |
52fc0b02 | 782 | |
1da177e4 LT |
783 | /* No object, just exit */ |
784 | ||
785 | return (AE_OK); | |
786 | } | |
787 | ||
788 | /* Object is a Region */ | |
789 | ||
25823e78 | 790 | if (obj_desc->region.space_id != info->space_id) { |
9f15fc66 BM |
791 | |
792 | /* This region is for a different address space, just ignore it */ | |
793 | ||
1da177e4 LT |
794 | return (AE_OK); |
795 | } | |
796 | ||
25823e78 | 797 | info->reg_run_count++; |
d815346f | 798 | status = acpi_ev_execute_reg_method(obj_desc, info->function); |
1da177e4 LT |
799 | return (status); |
800 | } | |
e2066ca1 BM |
801 | |
802 | /******************************************************************************* | |
803 | * | |
0306f035 | 804 | * FUNCTION: acpi_ev_execute_orphan_reg_method |
e2066ca1 | 805 | * |
0306f035 HG |
806 | * PARAMETERS: device_node - Namespace node for an ACPI device |
807 | * space_id - The address space ID | |
e2066ca1 BM |
808 | * |
809 | * RETURN: None | |
810 | * | |
0306f035 | 811 | * DESCRIPTION: Execute an "orphan" _REG method that appears under an ACPI |
e2066ca1 | 812 | * device. This is a _REG method that has no corresponding region |
0306f035 HG |
813 | * within the device's scope. ACPI tables depending on these |
814 | * "orphan" _REG methods have been seen for both EC and GPIO | |
815 | * Operation Regions. Presumably the Windows ACPI implementation | |
816 | * always calls the _REG method independent of the presence of | |
817 | * an actual Operation Region with the correct address space ID. | |
8f4f5e78 ZR |
818 | * |
819 | * MUTEX: Assumes the namespace is locked | |
e2066ca1 BM |
820 | * |
821 | ******************************************************************************/ | |
822 | ||
779bac99 | 823 | static void |
0306f035 HG |
824 | acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node, |
825 | acpi_adr_space_type space_id) | |
e2066ca1 | 826 | { |
8f4f5e78 ZR |
827 | acpi_handle reg_method; |
828 | struct acpi_namespace_node *next_node; | |
e2066ca1 BM |
829 | acpi_status status; |
830 | struct acpi_object_list args; | |
831 | union acpi_object objects[2]; | |
e2066ca1 | 832 | |
0306f035 | 833 | ACPI_FUNCTION_TRACE(ev_execute_orphan_reg_method); |
e2066ca1 | 834 | |
0306f035 | 835 | if (!device_node) { |
e2066ca1 BM |
836 | return_VOID; |
837 | } | |
838 | ||
839 | /* Namespace is currently locked, must release */ | |
840 | ||
841 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); | |
842 | ||
e2066ca1 BM |
843 | /* Get a handle to a _REG method immediately under the EC device */ |
844 | ||
0306f035 | 845 | status = acpi_get_handle(device_node, METHOD_NAME__REG, ®_method); |
e2066ca1 | 846 | if (ACPI_FAILURE(status)) { |
8f4f5e78 | 847 | goto exit; /* There is no _REG method present */ |
e2066ca1 BM |
848 | } |
849 | ||
850 | /* | |
851 | * Execute the _REG method only if there is no Operation Region in | |
852 | * this scope with the Embedded Controller space ID. Otherwise, it | |
853 | * will already have been executed. Note, this allows for Regions | |
854 | * with other space IDs to be present; but the code below will then | |
8f4f5e78 | 855 | * execute the _REG method with the embedded_control space_ID argument. |
e2066ca1 | 856 | */ |
0306f035 | 857 | next_node = acpi_ns_get_next_node(device_node, NULL); |
e2066ca1 BM |
858 | while (next_node) { |
859 | if ((next_node->type == ACPI_TYPE_REGION) && | |
860 | (next_node->object) && | |
0306f035 | 861 | (next_node->object->region.space_id == space_id)) { |
8f4f5e78 | 862 | goto exit; /* Do not execute the _REG */ |
e2066ca1 | 863 | } |
8f4f5e78 | 864 | |
0306f035 | 865 | next_node = acpi_ns_get_next_node(device_node, next_node); |
e2066ca1 BM |
866 | } |
867 | ||
0306f035 | 868 | /* Evaluate the _REG(space_id,Connect) method */ |
e2066ca1 BM |
869 | |
870 | args.count = 2; | |
871 | args.pointer = objects; | |
872 | objects[0].type = ACPI_TYPE_INTEGER; | |
0306f035 | 873 | objects[0].integer.value = space_id; |
e2066ca1 BM |
874 | objects[1].type = ACPI_TYPE_INTEGER; |
875 | objects[1].integer.value = ACPI_REG_CONNECT; | |
876 | ||
edc5935e | 877 | (void)acpi_evaluate_object(reg_method, NULL, &args, NULL); |
e2066ca1 | 878 | |
10622bf8 | 879 | exit: |
e2066ca1 BM |
880 | /* We ignore all errors from above, don't care */ |
881 | ||
edc5935e | 882 | (void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); |
e2066ca1 BM |
883 | return_VOID; |
884 | } |