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