xref: /openbmc/linux/drivers/acpi/acpica/evgpeinit.c (revision 9d749629)
1 /******************************************************************************
2  *
3  * Module Name: evgpeinit - System GPE initialization and update
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, 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 
49 #define _COMPONENT          ACPI_EVENTS
50 ACPI_MODULE_NAME("evgpeinit")
51 #if (!ACPI_REDUCED_HARDWARE)	/* Entire module */
52 /*
53  * Note: History of _PRW support in ACPICA
54  *
55  * Originally (2000 - 2010), the GPE initialization code performed a walk of
56  * the entire namespace to execute the _PRW methods and detect all GPEs
57  * capable of waking the system.
58  *
59  * As of 10/2010, the _PRW method execution has been removed since it is
60  * actually unnecessary. The host OS must in fact execute all _PRW methods
61  * in order to identify the device/power-resource dependencies. We now put
62  * the onus on the host OS to identify the wake GPEs as part of this process
63  * and to inform ACPICA of these GPEs via the acpi_setup_gpe_for_wake interface. This
64  * not only reduces the complexity of the ACPICA initialization code, but in
65  * some cases (on systems with very large namespaces) it should reduce the
66  * kernel boot time as well.
67  */
68 
69 /*******************************************************************************
70  *
71  * FUNCTION:    acpi_ev_gpe_initialize
72  *
73  * PARAMETERS:  None
74  *
75  * RETURN:      Status
76  *
77  * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
78  *
79  ******************************************************************************/
80 acpi_status acpi_ev_gpe_initialize(void)
81 {
82 	u32 register_count0 = 0;
83 	u32 register_count1 = 0;
84 	u32 gpe_number_max = 0;
85 	acpi_status status;
86 
87 	ACPI_FUNCTION_TRACE(ev_gpe_initialize);
88 
89 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
90 			      "Initializing General Purpose Events (GPEs):\n"));
91 
92 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
93 	if (ACPI_FAILURE(status)) {
94 		return_ACPI_STATUS(status);
95 	}
96 
97 	/*
98 	 * Initialize the GPE Block(s) defined in the FADT
99 	 *
100 	 * Why the GPE register block lengths are divided by 2:  From the ACPI
101 	 * Spec, section "General-Purpose Event Registers", we have:
102 	 *
103 	 * "Each register block contains two registers of equal length
104 	 *  GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
105 	 *  GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
106 	 *  The length of the GPE1_STS and GPE1_EN registers is equal to
107 	 *  half the GPE1_LEN. If a generic register block is not supported
108 	 *  then its respective block pointer and block length values in the
109 	 *  FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
110 	 *  to be the same size."
111 	 */
112 
113 	/*
114 	 * Determine the maximum GPE number for this machine.
115 	 *
116 	 * Note: both GPE0 and GPE1 are optional, and either can exist without
117 	 * the other.
118 	 *
119 	 * If EITHER the register length OR the block address are zero, then that
120 	 * particular block is not supported.
121 	 */
122 	if (acpi_gbl_FADT.gpe0_block_length &&
123 	    acpi_gbl_FADT.xgpe0_block.address) {
124 
125 		/* GPE block 0 exists (has both length and address > 0) */
126 
127 		register_count0 = (u16)(acpi_gbl_FADT.gpe0_block_length / 2);
128 
129 		gpe_number_max =
130 		    (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
131 
132 		/* Install GPE Block 0 */
133 
134 		status = acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
135 						  &acpi_gbl_FADT.xgpe0_block,
136 						  register_count0, 0,
137 						  acpi_gbl_FADT.sci_interrupt,
138 						  &acpi_gbl_gpe_fadt_blocks[0]);
139 
140 		if (ACPI_FAILURE(status)) {
141 			ACPI_EXCEPTION((AE_INFO, status,
142 					"Could not create GPE Block 0"));
143 		}
144 	}
145 
146 	if (acpi_gbl_FADT.gpe1_block_length &&
147 	    acpi_gbl_FADT.xgpe1_block.address) {
148 
149 		/* GPE block 1 exists (has both length and address > 0) */
150 
151 		register_count1 = (u16)(acpi_gbl_FADT.gpe1_block_length / 2);
152 
153 		/* Check for GPE0/GPE1 overlap (if both banks exist) */
154 
155 		if ((register_count0) &&
156 		    (gpe_number_max >= acpi_gbl_FADT.gpe1_base)) {
157 			ACPI_ERROR((AE_INFO,
158 				    "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
159 				    "(GPE %u to %u) - Ignoring GPE1",
160 				    gpe_number_max, acpi_gbl_FADT.gpe1_base,
161 				    acpi_gbl_FADT.gpe1_base +
162 				    ((register_count1 *
163 				      ACPI_GPE_REGISTER_WIDTH) - 1)));
164 
165 			/* Ignore GPE1 block by setting the register count to zero */
166 
167 			register_count1 = 0;
168 		} else {
169 			/* Install GPE Block 1 */
170 
171 			status =
172 			    acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
173 						     &acpi_gbl_FADT.xgpe1_block,
174 						     register_count1,
175 						     acpi_gbl_FADT.gpe1_base,
176 						     acpi_gbl_FADT.
177 						     sci_interrupt,
178 						     &acpi_gbl_gpe_fadt_blocks
179 						     [1]);
180 
181 			if (ACPI_FAILURE(status)) {
182 				ACPI_EXCEPTION((AE_INFO, status,
183 						"Could not create GPE Block 1"));
184 			}
185 
186 			/*
187 			 * GPE0 and GPE1 do not have to be contiguous in the GPE number
188 			 * space. However, GPE0 always starts at GPE number zero.
189 			 */
190 			gpe_number_max = acpi_gbl_FADT.gpe1_base +
191 			    ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1);
192 		}
193 	}
194 
195 	/* Exit if there are no GPE registers */
196 
197 	if ((register_count0 + register_count1) == 0) {
198 
199 		/* GPEs are not required by ACPI, this is OK */
200 
201 		ACPI_DEBUG_PRINT((ACPI_DB_INIT,
202 				  "There are no GPE blocks defined in the FADT\n"));
203 		status = AE_OK;
204 		goto cleanup;
205 	}
206 
207 	/* Check for Max GPE number out-of-range */
208 
209 	if (gpe_number_max > ACPI_GPE_MAX) {
210 		ACPI_ERROR((AE_INFO,
211 			    "Maximum GPE number from FADT is too large: 0x%X",
212 			    gpe_number_max));
213 		status = AE_BAD_VALUE;
214 		goto cleanup;
215 	}
216 
217       cleanup:
218 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
219 	return_ACPI_STATUS(AE_OK);
220 }
221 
222 /*******************************************************************************
223  *
224  * FUNCTION:    acpi_ev_update_gpes
225  *
226  * PARAMETERS:  table_owner_id      - ID of the newly-loaded ACPI table
227  *
228  * RETURN:      None
229  *
230  * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
231  *              result of a Load() or load_table() operation. If new GPE
232  *              methods have been installed, register the new methods.
233  *
234  ******************************************************************************/
235 
236 void acpi_ev_update_gpes(acpi_owner_id table_owner_id)
237 {
238 	struct acpi_gpe_xrupt_info *gpe_xrupt_info;
239 	struct acpi_gpe_block_info *gpe_block;
240 	struct acpi_gpe_walk_info walk_info;
241 	acpi_status status = AE_OK;
242 
243 	/*
244 	 * Find any _Lxx/_Exx GPE methods that have just been loaded.
245 	 *
246 	 * Any GPEs that correspond to new _Lxx/_Exx methods are immediately
247 	 * enabled.
248 	 *
249 	 * Examine the namespace underneath each gpe_device within the
250 	 * gpe_block lists.
251 	 */
252 	status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
253 	if (ACPI_FAILURE(status)) {
254 		return;
255 	}
256 
257 	walk_info.count = 0;
258 	walk_info.owner_id = table_owner_id;
259 	walk_info.execute_by_owner_id = TRUE;
260 
261 	/* Walk the interrupt level descriptor list */
262 
263 	gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
264 	while (gpe_xrupt_info) {
265 
266 		/* Walk all Gpe Blocks attached to this interrupt level */
267 
268 		gpe_block = gpe_xrupt_info->gpe_block_list_head;
269 		while (gpe_block) {
270 			walk_info.gpe_block = gpe_block;
271 			walk_info.gpe_device = gpe_block->node;
272 
273 			status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD,
274 							walk_info.gpe_device,
275 							ACPI_UINT32_MAX,
276 							ACPI_NS_WALK_NO_UNLOCK,
277 							acpi_ev_match_gpe_method,
278 							NULL, &walk_info, NULL);
279 			if (ACPI_FAILURE(status)) {
280 				ACPI_EXCEPTION((AE_INFO, status,
281 						"While decoding _Lxx/_Exx methods"));
282 			}
283 
284 			gpe_block = gpe_block->next;
285 		}
286 
287 		gpe_xrupt_info = gpe_xrupt_info->next;
288 	}
289 
290 	if (walk_info.count) {
291 		ACPI_INFO((AE_INFO, "Enabled %u new GPEs", walk_info.count));
292 	}
293 
294 	(void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
295 	return;
296 }
297 
298 /*******************************************************************************
299  *
300  * FUNCTION:    acpi_ev_match_gpe_method
301  *
302  * PARAMETERS:  Callback from walk_namespace
303  *
304  * RETURN:      Status
305  *
306  * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
307  *              control method under the _GPE portion of the namespace.
308  *              Extract the name and GPE type from the object, saving this
309  *              information for quick lookup during GPE dispatch. Allows a
310  *              per-owner_id evaluation if execute_by_owner_id is TRUE in the
311  *              walk_info parameter block.
312  *
313  *              The name of each GPE control method is of the form:
314  *              "_Lxx" or "_Exx", where:
315  *                  L      - means that the GPE is level triggered
316  *                  E      - means that the GPE is edge triggered
317  *                  xx     - is the GPE number [in HEX]
318  *
319  * If walk_info->execute_by_owner_id is TRUE, we only execute examine GPE methods
320  * with that owner.
321  *
322  ******************************************************************************/
323 
324 acpi_status
325 acpi_ev_match_gpe_method(acpi_handle obj_handle,
326 			 u32 level, void *context, void **return_value)
327 {
328 	struct acpi_namespace_node *method_node =
329 	    ACPI_CAST_PTR(struct acpi_namespace_node, obj_handle);
330 	struct acpi_gpe_walk_info *walk_info =
331 	    ACPI_CAST_PTR(struct acpi_gpe_walk_info, context);
332 	struct acpi_gpe_event_info *gpe_event_info;
333 	u32 gpe_number;
334 	char name[ACPI_NAME_SIZE + 1];
335 	u8 type;
336 
337 	ACPI_FUNCTION_TRACE(ev_match_gpe_method);
338 
339 	/* Check if requested owner_id matches this owner_id */
340 
341 	if ((walk_info->execute_by_owner_id) &&
342 	    (method_node->owner_id != walk_info->owner_id)) {
343 		return_ACPI_STATUS(AE_OK);
344 	}
345 
346 	/*
347 	 * Match and decode the _Lxx and _Exx GPE method names
348 	 *
349 	 * 1) Extract the method name and null terminate it
350 	 */
351 	ACPI_MOVE_32_TO_32(name, &method_node->name.integer);
352 	name[ACPI_NAME_SIZE] = 0;
353 
354 	/* 2) Name must begin with an underscore */
355 
356 	if (name[0] != '_') {
357 		return_ACPI_STATUS(AE_OK);	/* Ignore this method */
358 	}
359 
360 	/*
361 	 * 3) Edge/Level determination is based on the 2nd character
362 	 *    of the method name
363 	 */
364 	switch (name[1]) {
365 	case 'L':
366 		type = ACPI_GPE_LEVEL_TRIGGERED;
367 		break;
368 
369 	case 'E':
370 		type = ACPI_GPE_EDGE_TRIGGERED;
371 		break;
372 
373 	default:
374 		/* Unknown method type, just ignore it */
375 
376 		ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
377 				  "Ignoring unknown GPE method type: %s "
378 				  "(name not of form _Lxx or _Exx)", name));
379 		return_ACPI_STATUS(AE_OK);
380 	}
381 
382 	/* 4) The last two characters of the name are the hex GPE Number */
383 
384 	gpe_number = ACPI_STRTOUL(&name[2], NULL, 16);
385 	if (gpe_number == ACPI_UINT32_MAX) {
386 
387 		/* Conversion failed; invalid method, just ignore it */
388 
389 		ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
390 				  "Could not extract GPE number from name: %s "
391 				  "(name is not of form _Lxx or _Exx)", name));
392 		return_ACPI_STATUS(AE_OK);
393 	}
394 
395 	/* Ensure that we have a valid GPE number for this GPE block */
396 
397 	gpe_event_info =
398 	    acpi_ev_low_get_gpe_info(gpe_number, walk_info->gpe_block);
399 	if (!gpe_event_info) {
400 		/*
401 		 * This gpe_number is not valid for this GPE block, just ignore it.
402 		 * However, it may be valid for a different GPE block, since GPE0
403 		 * and GPE1 methods both appear under \_GPE.
404 		 */
405 		return_ACPI_STATUS(AE_OK);
406 	}
407 
408 	if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
409 	    ACPI_GPE_DISPATCH_HANDLER) {
410 
411 		/* If there is already a handler, ignore this GPE method */
412 
413 		return_ACPI_STATUS(AE_OK);
414 	}
415 
416 	if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
417 	    ACPI_GPE_DISPATCH_METHOD) {
418 		/*
419 		 * If there is already a method, ignore this method. But check
420 		 * for a type mismatch (if both the _Lxx AND _Exx exist)
421 		 */
422 		if (type != (gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK)) {
423 			ACPI_ERROR((AE_INFO,
424 				    "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
425 				    gpe_number, gpe_number, gpe_number));
426 		}
427 		return_ACPI_STATUS(AE_OK);
428 	}
429 
430 	/* Disable the GPE in case it's been enabled already. */
431 	(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
432 
433 	/*
434 	 * Add the GPE information from above to the gpe_event_info block for
435 	 * use during dispatch of this GPE.
436 	 */
437 	gpe_event_info->flags &= ~(ACPI_GPE_DISPATCH_MASK);
438 	gpe_event_info->flags |= (u8)(type | ACPI_GPE_DISPATCH_METHOD);
439 	gpe_event_info->dispatch.method_node = method_node;
440 
441 	ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
442 			  "Registered GPE method %s as GPE number 0x%.2X\n",
443 			  name, gpe_number));
444 	return_ACPI_STATUS(AE_OK);
445 }
446 
447 #endif				/* !ACPI_REDUCED_HARDWARE */
448