Merge branches 'slab/fixes', 'slob/fixes', 'slub/cleanups' and 'slub/fixes' into...
[sfrench/cifs-2.6.git] / drivers / acpi / acpica / hwgpe.c
1
2 /******************************************************************************
3  *
4  * Module Name: hwgpe - Low level GPE enable/disable/clear functions
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2010, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acevents.h"
48
49 #define _COMPONENT          ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwgpe")
51
52 /* Local prototypes */
53 static acpi_status
54 acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
55                                 struct acpi_gpe_block_info *gpe_block,
56                                 void *context);
57
58 /******************************************************************************
59  *
60  * FUNCTION:    acpi_hw_gpe_register_bit
61  *
62  * PARAMETERS:  gpe_event_info      - Info block for the GPE
63  *              gpe_register_info   - Info block for the GPE register
64  *
65  * RETURN:      Status
66  *
67  * DESCRIPTION: Compute GPE enable mask with one bit corresponding to the given
68  *              GPE set.
69  *
70  ******************************************************************************/
71
72 u32 acpi_hw_gpe_register_bit(struct acpi_gpe_event_info *gpe_event_info,
73                              struct acpi_gpe_register_info *gpe_register_info)
74 {
75         return (u32)1 << (gpe_event_info->gpe_number -
76                                 gpe_register_info->base_gpe_number);
77 }
78
79 /******************************************************************************
80  *
81  * FUNCTION:    acpi_hw_low_set_gpe
82  *
83  * PARAMETERS:  gpe_event_info      - Info block for the GPE to be disabled
84  *              action              - Enable or disable
85  *
86  * RETURN:      Status
87  *
88  * DESCRIPTION: Enable or disable a single GPE in its enable register.
89  *
90  ******************************************************************************/
91
92 acpi_status
93 acpi_hw_low_set_gpe(struct acpi_gpe_event_info *gpe_event_info, u8 action)
94 {
95         struct acpi_gpe_register_info *gpe_register_info;
96         acpi_status status;
97         u32 enable_mask;
98         u32 register_bit;
99
100         ACPI_FUNCTION_ENTRY();
101
102         /* Get the info block for the entire GPE register */
103
104         gpe_register_info = gpe_event_info->register_info;
105         if (!gpe_register_info) {
106                 return (AE_NOT_EXIST);
107         }
108
109         /* Get current value of the enable register that contains this GPE */
110
111         status = acpi_hw_read(&enable_mask, &gpe_register_info->enable_address);
112         if (ACPI_FAILURE(status)) {
113                 return (status);
114         }
115
116         /* Set ot clear just the bit that corresponds to this GPE */
117
118         register_bit = acpi_hw_gpe_register_bit(gpe_event_info,
119                                                 gpe_register_info);
120         switch (action) {
121         case ACPI_GPE_COND_ENABLE:
122                 if (!(register_bit & gpe_register_info->enable_for_run))
123                         return (AE_BAD_PARAMETER);
124
125         case ACPI_GPE_ENABLE:
126                 ACPI_SET_BIT(enable_mask, register_bit);
127                 break;
128
129         case ACPI_GPE_DISABLE:
130                 ACPI_CLEAR_BIT(enable_mask, register_bit);
131                 break;
132
133         default:
134                 ACPI_ERROR((AE_INFO, "Invalid action\n"));
135                 return (AE_BAD_PARAMETER);
136         }
137
138         /* Write the updated enable mask */
139
140         status = acpi_hw_write(enable_mask, &gpe_register_info->enable_address);
141         return (status);
142 }
143
144 /******************************************************************************
145  *
146  * FUNCTION:    acpi_hw_write_gpe_enable_reg
147  *
148  * PARAMETERS:  gpe_event_info      - Info block for the GPE to be enabled
149  *
150  * RETURN:      Status
151  *
152  * DESCRIPTION: Write a GPE enable register.  Note: The bit for this GPE must
153  *              already be cleared or set in the parent register
154  *              enable_for_run mask.
155  *
156  ******************************************************************************/
157
158 acpi_status
159 acpi_hw_write_gpe_enable_reg(struct acpi_gpe_event_info * gpe_event_info)
160 {
161         acpi_status status;
162
163         ACPI_FUNCTION_ENTRY();
164
165         status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_COND_ENABLE);
166         return (status);
167 }
168
169 /******************************************************************************
170  *
171  * FUNCTION:    acpi_hw_clear_gpe
172  *
173  * PARAMETERS:  gpe_event_info      - Info block for the GPE to be cleared
174  *
175  * RETURN:      Status
176  *
177  * DESCRIPTION: Clear the status bit for a single GPE.
178  *
179  ******************************************************************************/
180
181 acpi_status acpi_hw_clear_gpe(struct acpi_gpe_event_info * gpe_event_info)
182 {
183         struct acpi_gpe_register_info *gpe_register_info;
184         acpi_status status;
185         u32 register_bit;
186
187         ACPI_FUNCTION_ENTRY();
188
189         /* Get the info block for the entire GPE register */
190
191         gpe_register_info = gpe_event_info->register_info;
192         if (!gpe_register_info) {
193                 return (AE_NOT_EXIST);
194         }
195
196         register_bit = acpi_hw_gpe_register_bit(gpe_event_info,
197                                                 gpe_register_info);
198
199         /*
200          * Write a one to the appropriate bit in the status register to
201          * clear this GPE.
202          */
203         status = acpi_hw_write(register_bit,
204                                &gpe_register_info->status_address);
205
206         return (status);
207 }
208
209 /******************************************************************************
210  *
211  * FUNCTION:    acpi_hw_get_gpe_status
212  *
213  * PARAMETERS:  gpe_event_info      - Info block for the GPE to queried
214  *              event_status        - Where the GPE status is returned
215  *
216  * RETURN:      Status
217  *
218  * DESCRIPTION: Return the status of a single GPE.
219  *
220  ******************************************************************************/
221
222 acpi_status
223 acpi_hw_get_gpe_status(struct acpi_gpe_event_info * gpe_event_info,
224                        acpi_event_status * event_status)
225 {
226         u32 in_byte;
227         u32 register_bit;
228         struct acpi_gpe_register_info *gpe_register_info;
229         acpi_status status;
230         acpi_event_status local_event_status = 0;
231
232         ACPI_FUNCTION_ENTRY();
233
234         if (!event_status) {
235                 return (AE_BAD_PARAMETER);
236         }
237
238         /* Get the info block for the entire GPE register */
239
240         gpe_register_info = gpe_event_info->register_info;
241
242         /* Get the register bitmask for this GPE */
243
244         register_bit = acpi_hw_gpe_register_bit(gpe_event_info,
245                                                 gpe_register_info);
246
247         /* GPE currently enabled? (enabled for runtime?) */
248
249         if (register_bit & gpe_register_info->enable_for_run) {
250                 local_event_status |= ACPI_EVENT_FLAG_ENABLED;
251         }
252
253         /* GPE enabled for wake? */
254
255         if (register_bit & gpe_register_info->enable_for_wake) {
256                 local_event_status |= ACPI_EVENT_FLAG_WAKE_ENABLED;
257         }
258
259         /* GPE currently active (status bit == 1)? */
260
261         status = acpi_hw_read(&in_byte, &gpe_register_info->status_address);
262         if (ACPI_FAILURE(status)) {
263                 return (status);
264         }
265
266         if (register_bit & in_byte) {
267                 local_event_status |= ACPI_EVENT_FLAG_SET;
268         }
269
270         /* Set return value */
271
272         (*event_status) = local_event_status;
273         return (AE_OK);
274 }
275
276 /******************************************************************************
277  *
278  * FUNCTION:    acpi_hw_disable_gpe_block
279  *
280  * PARAMETERS:  gpe_xrupt_info      - GPE Interrupt info
281  *              gpe_block           - Gpe Block info
282  *
283  * RETURN:      Status
284  *
285  * DESCRIPTION: Disable all GPEs within a single GPE block
286  *
287  ******************************************************************************/
288
289 acpi_status
290 acpi_hw_disable_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
291                           struct acpi_gpe_block_info *gpe_block, void *context)
292 {
293         u32 i;
294         acpi_status status;
295
296         /* Examine each GPE Register within the block */
297
298         for (i = 0; i < gpe_block->register_count; i++) {
299
300                 /* Disable all GPEs in this register */
301
302                 status =
303                     acpi_hw_write(0x00,
304                                   &gpe_block->register_info[i].enable_address);
305                 if (ACPI_FAILURE(status)) {
306                         return (status);
307                 }
308         }
309
310         return (AE_OK);
311 }
312
313 /******************************************************************************
314  *
315  * FUNCTION:    acpi_hw_clear_gpe_block
316  *
317  * PARAMETERS:  gpe_xrupt_info      - GPE Interrupt info
318  *              gpe_block           - Gpe Block info
319  *
320  * RETURN:      Status
321  *
322  * DESCRIPTION: Clear status bits for all GPEs within a single GPE block
323  *
324  ******************************************************************************/
325
326 acpi_status
327 acpi_hw_clear_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
328                         struct acpi_gpe_block_info *gpe_block, void *context)
329 {
330         u32 i;
331         acpi_status status;
332
333         /* Examine each GPE Register within the block */
334
335         for (i = 0; i < gpe_block->register_count; i++) {
336
337                 /* Clear status on all GPEs in this register */
338
339                 status =
340                     acpi_hw_write(0xFF,
341                                   &gpe_block->register_info[i].status_address);
342                 if (ACPI_FAILURE(status)) {
343                         return (status);
344                 }
345         }
346
347         return (AE_OK);
348 }
349
350 /******************************************************************************
351  *
352  * FUNCTION:    acpi_hw_enable_runtime_gpe_block
353  *
354  * PARAMETERS:  gpe_xrupt_info      - GPE Interrupt info
355  *              gpe_block           - Gpe Block info
356  *
357  * RETURN:      Status
358  *
359  * DESCRIPTION: Enable all "runtime" GPEs within a single GPE block. Includes
360  *              combination wake/run GPEs.
361  *
362  ******************************************************************************/
363
364 acpi_status
365 acpi_hw_enable_runtime_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
366                                  struct acpi_gpe_block_info *gpe_block, void *context)
367 {
368         u32 i;
369         acpi_status status;
370
371         /* NOTE: assumes that all GPEs are currently disabled */
372
373         /* Examine each GPE Register within the block */
374
375         for (i = 0; i < gpe_block->register_count; i++) {
376                 if (!gpe_block->register_info[i].enable_for_run) {
377                         continue;
378                 }
379
380                 /* Enable all "runtime" GPEs in this register */
381
382                 status =
383                     acpi_hw_write(gpe_block->register_info[i].enable_for_run,
384                                   &gpe_block->register_info[i].enable_address);
385                 if (ACPI_FAILURE(status)) {
386                         return (status);
387                 }
388         }
389
390         return (AE_OK);
391 }
392
393 /******************************************************************************
394  *
395  * FUNCTION:    acpi_hw_enable_wakeup_gpe_block
396  *
397  * PARAMETERS:  gpe_xrupt_info      - GPE Interrupt info
398  *              gpe_block           - Gpe Block info
399  *
400  * RETURN:      Status
401  *
402  * DESCRIPTION: Enable all "wake" GPEs within a single GPE block. Includes
403  *              combination wake/run GPEs.
404  *
405  ******************************************************************************/
406
407 static acpi_status
408 acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
409                                 struct acpi_gpe_block_info *gpe_block,
410                                 void *context)
411 {
412         u32 i;
413         acpi_status status;
414
415         /* Examine each GPE Register within the block */
416
417         for (i = 0; i < gpe_block->register_count; i++) {
418                 if (!gpe_block->register_info[i].enable_for_wake) {
419                         continue;
420                 }
421
422                 /* Enable all "wake" GPEs in this register */
423
424                 status =
425                     acpi_hw_write(gpe_block->register_info[i].enable_for_wake,
426                                   &gpe_block->register_info[i].enable_address);
427                 if (ACPI_FAILURE(status)) {
428                         return (status);
429                 }
430         }
431
432         return (AE_OK);
433 }
434
435 /******************************************************************************
436  *
437  * FUNCTION:    acpi_hw_disable_all_gpes
438  *
439  * PARAMETERS:  None
440  *
441  * RETURN:      Status
442  *
443  * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
444  *
445  ******************************************************************************/
446
447 acpi_status acpi_hw_disable_all_gpes(void)
448 {
449         acpi_status status;
450
451         ACPI_FUNCTION_TRACE(hw_disable_all_gpes);
452
453         status = acpi_ev_walk_gpe_list(acpi_hw_disable_gpe_block, NULL);
454         status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
455         return_ACPI_STATUS(status);
456 }
457
458 /******************************************************************************
459  *
460  * FUNCTION:    acpi_hw_enable_all_runtime_gpes
461  *
462  * PARAMETERS:  None
463  *
464  * RETURN:      Status
465  *
466  * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
467  *
468  ******************************************************************************/
469
470 acpi_status acpi_hw_enable_all_runtime_gpes(void)
471 {
472         acpi_status status;
473
474         ACPI_FUNCTION_TRACE(hw_enable_all_runtime_gpes);
475
476         status = acpi_ev_walk_gpe_list(acpi_hw_enable_runtime_gpe_block, NULL);
477         return_ACPI_STATUS(status);
478 }
479
480 /******************************************************************************
481  *
482  * FUNCTION:    acpi_hw_enable_all_wakeup_gpes
483  *
484  * PARAMETERS:  None
485  *
486  * RETURN:      Status
487  *
488  * DESCRIPTION: Enable all "wakeup" GPEs, in all GPE blocks
489  *
490  ******************************************************************************/
491
492 acpi_status acpi_hw_enable_all_wakeup_gpes(void)
493 {
494         acpi_status status;
495
496         ACPI_FUNCTION_TRACE(hw_enable_all_wakeup_gpes);
497
498         status = acpi_ev_walk_gpe_list(acpi_hw_enable_wakeup_gpe_block, NULL);
499         return_ACPI_STATUS(status);
500 }