xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/hardware/hwgpe.c (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
1 /******************************************************************************
2  *
3  * Module Name: hwgpe - Low level GPE enable/disable/clear functions
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115 
116 #include "acpi.h"
117 #include "accommon.h"
118 #include "acevents.h"
119 
120 #define _COMPONENT          ACPI_HARDWARE
121         ACPI_MODULE_NAME    ("hwgpe")
122 
123 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
124 
125 /* Local prototypes */
126 
127 static ACPI_STATUS
128 AcpiHwEnableWakeupGpeBlock (
129     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo,
130     ACPI_GPE_BLOCK_INFO     *GpeBlock,
131     void                    *Context);
132 
133 static ACPI_STATUS
134 AcpiHwGpeEnableWrite (
135     UINT8                   EnableMask,
136     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo);
137 
138 
139 /******************************************************************************
140  *
141  * FUNCTION:    AcpiHwGetGpeRegisterBit
142  *
143  * PARAMETERS:  GpeEventInfo        - Info block for the GPE
144  *
145  * RETURN:      Register mask with a one in the GPE bit position
146  *
147  * DESCRIPTION: Compute the register mask for this GPE. One bit is set in the
148  *              correct position for the input GPE.
149  *
150  ******************************************************************************/
151 
152 UINT32
153 AcpiHwGetGpeRegisterBit (
154     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
155 {
156 
157     return ((UINT32) 1 <<
158         (GpeEventInfo->GpeNumber - GpeEventInfo->RegisterInfo->BaseGpeNumber));
159 }
160 
161 
162 /******************************************************************************
163  *
164  * FUNCTION:    AcpiHwLowSetGpe
165  *
166  * PARAMETERS:  GpeEventInfo        - Info block for the GPE to be disabled
167  *              Action              - Enable or disable
168  *
169  * RETURN:      Status
170  *
171  * DESCRIPTION: Enable or disable a single GPE in the parent enable register.
172  *              The EnableMask field of the involved GPE register must be
173  *              updated by the caller if necessary.
174  *
175  ******************************************************************************/
176 
177 ACPI_STATUS
178 AcpiHwLowSetGpe (
179     ACPI_GPE_EVENT_INFO     *GpeEventInfo,
180     UINT32                  Action)
181 {
182     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
183     ACPI_STATUS             Status;
184     UINT32                  EnableMask;
185     UINT32                  RegisterBit;
186 
187 
188     ACPI_FUNCTION_ENTRY ();
189 
190 
191     /* Get the info block for the entire GPE register */
192 
193     GpeRegisterInfo = GpeEventInfo->RegisterInfo;
194     if (!GpeRegisterInfo)
195     {
196         return (AE_NOT_EXIST);
197     }
198 
199     /* Get current value of the enable register that contains this GPE */
200 
201     Status = AcpiHwRead (&EnableMask, &GpeRegisterInfo->EnableAddress);
202     if (ACPI_FAILURE (Status))
203     {
204         return (Status);
205     }
206 
207     /* Set or clear just the bit that corresponds to this GPE */
208 
209     RegisterBit = AcpiHwGetGpeRegisterBit (GpeEventInfo);
210     switch (Action)
211     {
212     case ACPI_GPE_CONDITIONAL_ENABLE:
213 
214         /* Only enable if the corresponding EnableMask bit is set */
215 
216         if (!(RegisterBit & GpeRegisterInfo->EnableMask))
217         {
218             return (AE_BAD_PARAMETER);
219         }
220 
221         /*lint -fallthrough */
222 
223     case ACPI_GPE_ENABLE:
224 
225         ACPI_SET_BIT (EnableMask, RegisterBit);
226         break;
227 
228     case ACPI_GPE_DISABLE:
229 
230         ACPI_CLEAR_BIT (EnableMask, RegisterBit);
231         break;
232 
233     default:
234 
235         ACPI_ERROR ((AE_INFO, "Invalid GPE Action, %u", Action));
236         return (AE_BAD_PARAMETER);
237     }
238 
239     /* Write the updated enable mask */
240 
241     Status = AcpiHwWrite (EnableMask, &GpeRegisterInfo->EnableAddress);
242     return (Status);
243 }
244 
245 
246 /******************************************************************************
247  *
248  * FUNCTION:    AcpiHwClearGpe
249  *
250  * PARAMETERS:  GpeEventInfo        - Info block for the GPE to be cleared
251  *
252  * RETURN:      Status
253  *
254  * DESCRIPTION: Clear the status bit for a single GPE.
255  *
256  ******************************************************************************/
257 
258 ACPI_STATUS
259 AcpiHwClearGpe (
260     ACPI_GPE_EVENT_INFO     *GpeEventInfo)
261 {
262     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
263     ACPI_STATUS             Status;
264     UINT32                  RegisterBit;
265 
266 
267     ACPI_FUNCTION_ENTRY ();
268 
269     /* Get the info block for the entire GPE register */
270 
271     GpeRegisterInfo = GpeEventInfo->RegisterInfo;
272     if (!GpeRegisterInfo)
273     {
274         return (AE_NOT_EXIST);
275     }
276 
277     /*
278      * Write a one to the appropriate bit in the status register to
279      * clear this GPE.
280      */
281     RegisterBit = AcpiHwGetGpeRegisterBit (GpeEventInfo);
282 
283     Status = AcpiHwWrite (RegisterBit, &GpeRegisterInfo->StatusAddress);
284     return (Status);
285 }
286 
287 
288 /******************************************************************************
289  *
290  * FUNCTION:    AcpiHwGetGpeStatus
291  *
292  * PARAMETERS:  GpeEventInfo        - Info block for the GPE to queried
293  *              EventStatus         - Where the GPE status is returned
294  *
295  * RETURN:      Status
296  *
297  * DESCRIPTION: Return the status of a single GPE.
298  *
299  ******************************************************************************/
300 
301 ACPI_STATUS
302 AcpiHwGetGpeStatus (
303     ACPI_GPE_EVENT_INFO     *GpeEventInfo,
304     ACPI_EVENT_STATUS       *EventStatus)
305 {
306     UINT32                  InByte;
307     UINT32                  RegisterBit;
308     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
309     ACPI_EVENT_STATUS       LocalEventStatus = 0;
310     ACPI_STATUS             Status;
311 
312 
313     ACPI_FUNCTION_ENTRY ();
314 
315 
316     if (!EventStatus)
317     {
318         return (AE_BAD_PARAMETER);
319     }
320 
321     /* GPE currently handled? */
322 
323     if (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) !=
324         ACPI_GPE_DISPATCH_NONE)
325     {
326         LocalEventStatus |= ACPI_EVENT_FLAG_HAS_HANDLER;
327     }
328 
329     /* Get the info block for the entire GPE register */
330 
331     GpeRegisterInfo = GpeEventInfo->RegisterInfo;
332 
333     /* Get the register bitmask for this GPE */
334 
335     RegisterBit = AcpiHwGetGpeRegisterBit (GpeEventInfo);
336 
337     /* GPE currently enabled? (enabled for runtime?) */
338 
339     if (RegisterBit & GpeRegisterInfo->EnableForRun)
340     {
341         LocalEventStatus |= ACPI_EVENT_FLAG_ENABLED;
342     }
343 
344     /* GPE enabled for wake? */
345 
346     if (RegisterBit & GpeRegisterInfo->EnableForWake)
347     {
348         LocalEventStatus |= ACPI_EVENT_FLAG_WAKE_ENABLED;
349     }
350 
351     /* GPE currently enabled (enable bit == 1)? */
352 
353     Status = AcpiHwRead (&InByte, &GpeRegisterInfo->EnableAddress);
354     if (ACPI_FAILURE (Status))
355     {
356         return (Status);
357     }
358 
359     if (RegisterBit & InByte)
360     {
361         LocalEventStatus |= ACPI_EVENT_FLAG_ENABLE_SET;
362     }
363 
364     /* GPE currently active (status bit == 1)? */
365 
366     Status = AcpiHwRead (&InByte, &GpeRegisterInfo->StatusAddress);
367     if (ACPI_FAILURE (Status))
368     {
369         return (Status);
370     }
371 
372     if (RegisterBit & InByte)
373     {
374         LocalEventStatus |= ACPI_EVENT_FLAG_STATUS_SET;
375     }
376 
377     /* Set return value */
378 
379     (*EventStatus) = LocalEventStatus;
380     return (AE_OK);
381 }
382 
383 
384 /******************************************************************************
385  *
386  * FUNCTION:    AcpiHwGpeEnableWrite
387  *
388  * PARAMETERS:  EnableMask          - Bit mask to write to the GPE register
389  *              GpeRegisterInfo     - Gpe Register info
390  *
391  * RETURN:      Status
392  *
393  * DESCRIPTION: Write the enable mask byte to the given GPE register.
394  *
395  ******************************************************************************/
396 
397 static ACPI_STATUS
398 AcpiHwGpeEnableWrite (
399     UINT8                   EnableMask,
400     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo)
401 {
402     ACPI_STATUS             Status;
403 
404 
405     GpeRegisterInfo->EnableMask = EnableMask;
406 
407     Status = AcpiHwWrite (EnableMask, &GpeRegisterInfo->EnableAddress);
408     return (Status);
409 }
410 
411 
412 /******************************************************************************
413  *
414  * FUNCTION:    AcpiHwDisableGpeBlock
415  *
416  * PARAMETERS:  GpeXruptInfo        - GPE Interrupt info
417  *              GpeBlock            - Gpe Block info
418  *
419  * RETURN:      Status
420  *
421  * DESCRIPTION: Disable all GPEs within a single GPE block
422  *
423  ******************************************************************************/
424 
425 ACPI_STATUS
426 AcpiHwDisableGpeBlock (
427     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo,
428     ACPI_GPE_BLOCK_INFO     *GpeBlock,
429     void                    *Context)
430 {
431     UINT32                  i;
432     ACPI_STATUS             Status;
433 
434 
435     /* Examine each GPE Register within the block */
436 
437     for (i = 0; i < GpeBlock->RegisterCount; i++)
438     {
439         /* Disable all GPEs in this register */
440 
441         Status = AcpiHwGpeEnableWrite (0x00, &GpeBlock->RegisterInfo[i]);
442         if (ACPI_FAILURE (Status))
443         {
444             return (Status);
445         }
446     }
447 
448     return (AE_OK);
449 }
450 
451 
452 /******************************************************************************
453  *
454  * FUNCTION:    AcpiHwClearGpeBlock
455  *
456  * PARAMETERS:  GpeXruptInfo        - GPE Interrupt info
457  *              GpeBlock            - Gpe Block info
458  *
459  * RETURN:      Status
460  *
461  * DESCRIPTION: Clear status bits for all GPEs within a single GPE block
462  *
463  ******************************************************************************/
464 
465 ACPI_STATUS
466 AcpiHwClearGpeBlock (
467     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo,
468     ACPI_GPE_BLOCK_INFO     *GpeBlock,
469     void                    *Context)
470 {
471     UINT32                  i;
472     ACPI_STATUS             Status;
473 
474 
475     /* Examine each GPE Register within the block */
476 
477     for (i = 0; i < GpeBlock->RegisterCount; i++)
478     {
479         /* Clear status on all GPEs in this register */
480 
481         Status = AcpiHwWrite (0xFF, &GpeBlock->RegisterInfo[i].StatusAddress);
482         if (ACPI_FAILURE (Status))
483         {
484             return (Status);
485         }
486     }
487 
488     return (AE_OK);
489 }
490 
491 
492 /******************************************************************************
493  *
494  * FUNCTION:    AcpiHwEnableRuntimeGpeBlock
495  *
496  * PARAMETERS:  GpeXruptInfo        - GPE Interrupt info
497  *              GpeBlock            - Gpe Block info
498  *
499  * RETURN:      Status
500  *
501  * DESCRIPTION: Enable all "runtime" GPEs within a single GPE block. Includes
502  *              combination wake/run GPEs.
503  *
504  ******************************************************************************/
505 
506 ACPI_STATUS
507 AcpiHwEnableRuntimeGpeBlock (
508     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo,
509     ACPI_GPE_BLOCK_INFO     *GpeBlock,
510     void                    *Context)
511 {
512     UINT32                  i;
513     ACPI_STATUS             Status;
514     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
515 
516 
517     /* NOTE: assumes that all GPEs are currently disabled */
518 
519     /* Examine each GPE Register within the block */
520 
521     for (i = 0; i < GpeBlock->RegisterCount; i++)
522     {
523         GpeRegisterInfo = &GpeBlock->RegisterInfo[i];
524         if (!GpeRegisterInfo->EnableForRun)
525         {
526             continue;
527         }
528 
529         /* Enable all "runtime" GPEs in this register */
530 
531         Status = AcpiHwGpeEnableWrite (GpeRegisterInfo->EnableForRun,
532             GpeRegisterInfo);
533         if (ACPI_FAILURE (Status))
534         {
535             return (Status);
536         }
537     }
538 
539     return (AE_OK);
540 }
541 
542 
543 /******************************************************************************
544  *
545  * FUNCTION:    AcpiHwEnableWakeupGpeBlock
546  *
547  * PARAMETERS:  GpeXruptInfo        - GPE Interrupt info
548  *              GpeBlock            - Gpe Block info
549  *
550  * RETURN:      Status
551  *
552  * DESCRIPTION: Enable all "wake" GPEs within a single GPE block. Includes
553  *              combination wake/run GPEs.
554  *
555  ******************************************************************************/
556 
557 static ACPI_STATUS
558 AcpiHwEnableWakeupGpeBlock (
559     ACPI_GPE_XRUPT_INFO     *GpeXruptInfo,
560     ACPI_GPE_BLOCK_INFO     *GpeBlock,
561     void                    *Context)
562 {
563     UINT32                  i;
564     ACPI_STATUS             Status;
565     ACPI_GPE_REGISTER_INFO  *GpeRegisterInfo;
566 
567 
568     /* Examine each GPE Register within the block */
569 
570     for (i = 0; i < GpeBlock->RegisterCount; i++)
571     {
572         GpeRegisterInfo = &GpeBlock->RegisterInfo[i];
573 
574         /*
575          * Enable all "wake" GPEs in this register and disable the
576          * remaining ones.
577          */
578         Status = AcpiHwGpeEnableWrite (GpeRegisterInfo->EnableForWake,
579             GpeRegisterInfo);
580         if (ACPI_FAILURE (Status))
581         {
582             return (Status);
583         }
584     }
585 
586     return (AE_OK);
587 }
588 
589 
590 /******************************************************************************
591  *
592  * FUNCTION:    AcpiHwDisableAllGpes
593  *
594  * PARAMETERS:  None
595  *
596  * RETURN:      Status
597  *
598  * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
599  *
600  ******************************************************************************/
601 
602 ACPI_STATUS
603 AcpiHwDisableAllGpes (
604     void)
605 {
606     ACPI_STATUS             Status;
607 
608 
609     ACPI_FUNCTION_TRACE (HwDisableAllGpes);
610 
611 
612     Status = AcpiEvWalkGpeList (AcpiHwDisableGpeBlock, NULL);
613     Status = AcpiEvWalkGpeList (AcpiHwClearGpeBlock, NULL);
614     return_ACPI_STATUS (Status);
615 }
616 
617 
618 /******************************************************************************
619  *
620  * FUNCTION:    AcpiHwEnableAllRuntimeGpes
621  *
622  * PARAMETERS:  None
623  *
624  * RETURN:      Status
625  *
626  * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
627  *
628  ******************************************************************************/
629 
630 ACPI_STATUS
631 AcpiHwEnableAllRuntimeGpes (
632     void)
633 {
634     ACPI_STATUS             Status;
635 
636 
637     ACPI_FUNCTION_TRACE (HwEnableAllRuntimeGpes);
638 
639 
640     Status = AcpiEvWalkGpeList (AcpiHwEnableRuntimeGpeBlock, NULL);
641     return_ACPI_STATUS (Status);
642 }
643 
644 
645 /******************************************************************************
646  *
647  * FUNCTION:    AcpiHwEnableAllWakeupGpes
648  *
649  * PARAMETERS:  None
650  *
651  * RETURN:      Status
652  *
653  * DESCRIPTION: Enable all "wakeup" GPEs, in all GPE blocks
654  *
655  ******************************************************************************/
656 
657 ACPI_STATUS
658 AcpiHwEnableAllWakeupGpes (
659     void)
660 {
661     ACPI_STATUS             Status;
662 
663 
664     ACPI_FUNCTION_TRACE (HwEnableAllWakeupGpes);
665 
666 
667     Status = AcpiEvWalkGpeList (AcpiHwEnableWakeupGpeBlock, NULL);
668     return_ACPI_STATUS (Status);
669 }
670 
671 #endif /* !ACPI_REDUCED_HARDWARE */
672