summaryrefslogtreecommitdiffstats
path: root/sys/dev/acpi/acpi_event.c
blob: 5f965df58a5b2e67364ebdc4f744f5ff377a6053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*-
 * Copyright (c) 1999 Takanori Watanabe <takawata@shidahara1.planet.sci.kobe-u.ac.jp>
 * Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

#include "opt_acpi.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/ctype.h>
#include <sys/malloc.h>

#include <machine/bus.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>

#include <dev/acpi/aml/aml_env.h>
#include <dev/acpi/aml/aml_evalobj.h>

/* 
 * ACPI events
 */
static void acpi_process_event(acpi_softc_t *sc, u_int32_t status_e,
			       u_int32_t status_0, u_int32_t status_1);

/*
 * ACPI events
 */

static void
acpi_process_event(acpi_softc_t *sc, u_int32_t status_e,
		   u_int32_t status_0, u_int32_t status_1)
{
    int i;
	
    if (status_e & ACPI_PM1_PWRBTN_EN) {
	if (sc->ignore_events & ACPI_PM1_PWRBTN_EN) {
	    ACPI_DEBUGPRINT("PWRBTN event ingnored\n");
	} else {
#if 1
	    acpi_set_sleeping_state(sc, ACPI_S_STATE_S5);
#else
	    /*
	     * If there is ACPI userland daemon,
	     * this event should be passed to it
	     * so that the user can determine power policy.
	     */
	    acpi_queue_event(sc, ACPI_EVENT_TYPE_FIXEDREG, 0);
#endif
	}
    }

    if (status_e & ACPI_PM1_SLPBTN_EN) {
	if (sc->ignore_events & ACPI_PM1_SLPBTN_EN) {
	    ACPI_DEBUGPRINT("SLPBTN event ingnored\n");
	} else {
#if 1
	    acpi_set_sleeping_state(sc, ACPI_S_STATE_S1);
#else
	    acpi_queue_event(sc, ACPI_EVENT_TYPE_FIXEDREG, 1);
#endif
	}
    }
    for(i = 0; i < sc->facp_body->gpe0_len * 4; i++)
	if((status_0 & (1 << i)) && (sc->gpe0_mask & (1 << i)))
	    acpi_queue_event(sc, ACPI_EVENT_TYPE_GPEREG, i);
    for(i = 0; i < sc->facp_body->gpe1_len * 4 ; i++)
	if((status_1 & (1 << i)) && (sc->gpe1_mask & (1 << i)))
	    acpi_queue_event(sc, ACPI_EVENT_TYPE_GPEREG,
			     i + sc->facp_body->gpe1_base);
}

void
acpi_intr(void *data)
{
    u_int32_t	enable;
    u_int32_t	status_e, status_0, status_1;
    u_int32_t	val;
    int		debug;
    acpi_softc_t	*sc;

    sc = (acpi_softc_t *)data;
    debug = acpi_debug;	/* Save debug level */
    acpi_debug = 0;		/* Shut up */

    /* 
     * Power Management 1 Status Registers 
     */
    status_e = enable = 0;
    acpi_io_pm1_status(sc, ACPI_REGISTER_INPUT, &status_e);

    /* 
     * Get current interrupt mask
     */
    acpi_io_pm1_enable(sc, ACPI_REGISTER_INPUT, &enable);

    /* 
     * Disable events and re-enable again
     */
    if ((status_e & enable) != 0) {
	acpi_debug = debug;	/* OK, you can speak */

	ACPI_DEBUGPRINT("pm1_status intr CALLED\n");

	/* Disable all interrupt generation */
	val = enable & (~ACPI_PM1_ALL_ENABLE_BITS);
	acpi_io_pm1_enable(sc, ACPI_REGISTER_OUTPUT, &val);

	/* Clear interrupt status */
	val = enable & ACPI_PM1_ALL_ENABLE_BITS;
	acpi_io_pm1_status(sc, ACPI_REGISTER_OUTPUT, &val);

	/* Re-enable interrupt */
	acpi_io_pm1_enable(sc, ACPI_REGISTER_OUTPUT, &enable);

	acpi_debug = 0;		/* Shut up again */
    }

    /* 
     * General-Purpose Events 0 Status Registers
     */
    status_0 = enable = 0;
    acpi_io_gpe0_status(sc, ACPI_REGISTER_INPUT, &status_0);

    /* 
     * Get current interrupt mask 
     */
    acpi_io_gpe0_enable(sc, ACPI_REGISTER_INPUT, &enable);

    /* 
     * Disable events and re-enable again 
     */
    if ((status_0 & enable) != 0) {
	acpi_debug = debug;	/* OK, you can speak */

	ACPI_DEBUGPRINT("gpe0_status intr CALLED\n");

	/* Disable all interrupt generation */
	val = enable & ~status_0;
#if 0
	/* or should we disable all? */
	val = 0x0;
#endif
	acpi_io_gpe0_enable(sc, ACPI_REGISTER_OUTPUT, &val);
#if 0
	/* Clear interrupt status */
	val = enable;	/* XXX */
	acpi_io_gpe0_status(sc, ACPI_REGISTER_OUTPUT, &val);

	/* Re-enable interrupt */
	acpi_io_gpe0_enable(sc, ACPI_REGISTER_OUTPUT, &enable);

	acpi_debug = 0;		/* Shut up again */
#endif
    }

    /*
     * General-Purpose Events 1 Status Registers
     */
    status_1 = enable = 0;
    acpi_io_gpe1_status(sc, ACPI_REGISTER_INPUT, &status_1);

    /*
      Get current interrupt mask
    */
    acpi_io_gpe1_enable(sc, ACPI_REGISTER_INPUT, &enable);

    /*
     * Disable events and re-enable again
     */
    if ((status_1 & enable) != 0) {
	acpi_debug = debug;	/* OK, you can speak */

	ACPI_DEBUGPRINT("gpe1_status intr CALLED\n");

	/* Disable all interrupt generation */
	val = enable & ~status_1;
#if 0
	/* or should we disable all? */
	val = 0x0;
#endif
	acpi_io_gpe1_enable(sc, ACPI_REGISTER_OUTPUT, &val);

	/* Clear interrupt status */
	val = enable;	/* XXX */
	acpi_io_gpe1_status(sc, ACPI_REGISTER_OUTPUT, &val);

	/* Re-enable interrupt */
	acpi_io_gpe1_enable(sc, ACPI_REGISTER_OUTPUT, &enable);

	acpi_debug = 0;		/* Shut up again */
    }

    acpi_debug = debug;	/* Restore debug level */

    /* do something to handle the events... */
    acpi_process_event(sc, status_e, status_0, status_1);
}

static int
acpi_set_gpe_bits(struct aml_name *name, va_list ap)
{
    struct acpi_softc	*sc = va_arg(ap, struct acpi_softc *);
    int			*gpemask0 = va_arg(ap, int *);
    int			*gpemask1 = va_arg(ap, int *);
    int			gpenum;

#define XDIGITTONUM(c) ((isdigit(c)) ? ((c) - '0') : ('A' <= (c)&& (c) <= 'F') ? ((c) - 'A' + 10) : 0)

    if (isxdigit(name->name[2]) && isxdigit(name->name[3])) {
	gpenum = XDIGITTONUM(name->name[2]) * 16 + 
	    XDIGITTONUM(name->name[3]);
	ACPI_DEBUGPRINT("GPENUM %d %d \n", gpenum, sc->facp_body->gpe0_len * 4);
	if (gpenum < (sc->facp_body->gpe0_len * 4)) {
	    *gpemask0 |= (1 << gpenum);
	} else {
	    *gpemask1 |= (1 << (gpenum - sc->facp_body->gpe1_base));
	}
    }
    ACPI_DEBUGPRINT("GPEMASK %x %x\n", *gpemask0, *gpemask1);
    return 0;
}

void
acpi_enable_events(acpi_softc_t *sc)
{
    u_int32_t	status;
    u_int32_t	mask0, mask1;
    u_int32_t	flags;

    /*
     * Setup PM1 Enable Registers Fixed Feature Enable Bits (4.7.3.1.2)
     * based on flags field of Fixed ACPI Description Table (5.2.5).
     */
    acpi_io_pm1_enable(sc, ACPI_REGISTER_INPUT, &status);
    flags = sc->facp_body->flags;
    if ((flags & ACPI_FACP_FLAG_PWR_BUTTON) == 0) {
	status |= ACPI_PM1_PWRBTN_EN;
    }
    if ((flags & ACPI_FACP_FLAG_SLP_BUTTON) == 0) {
	status |= ACPI_PM1_SLPBTN_EN;
    }
    acpi_io_pm1_enable(sc, ACPI_REGISTER_OUTPUT, &status);

#if 1
    /*
     * XXX
     * This should be done based on level event handlers in
     * \_GPE scope (4.7.2.2.1.2).
     */

    mask0 = mask1 = 0;
    aml_apply_foreach_found_objects(NULL, "\\_GPE._L", acpi_set_gpe_bits,
				    sc, &mask0, &mask1);	/* XXX correct? */
    sc->gpe0_mask = mask0;
    sc->gpe1_mask = mask1;

    acpi_io_gpe0_enable(sc, ACPI_REGISTER_OUTPUT, &mask0);
    acpi_io_gpe1_enable(sc, ACPI_REGISTER_OUTPUT, &mask1);
#endif

    /* print all event status for debugging */
    acpi_io_pm1_status(sc, ACPI_REGISTER_INPUT, &status);
    acpi_io_pm1_enable(sc, ACPI_REGISTER_INPUT,  &status);
    acpi_io_gpe0_status(sc, ACPI_REGISTER_INPUT, &status);
    acpi_io_gpe0_enable(sc, ACPI_REGISTER_INPUT, &status);
    acpi_io_gpe1_status(sc, ACPI_REGISTER_INPUT, &status);
    acpi_io_gpe1_enable(sc, ACPI_REGISTER_INPUT, &status);
    acpi_io_pm1_control(sc, ACPI_REGISTER_INPUT,  &mask0, &mask1);
    acpi_io_pm2_control(sc, ACPI_REGISTER_INPUT,  &status);
    acpi_io_pm_timer(sc, ACPI_REGISTER_INPUT,  &status);
}

void
acpi_disable_events(acpi_softc_t *sc)
{
    u_int32_t	zero;

    if (sc->enabled) {
	zero = 0;
	acpi_io_pm1_enable(sc, ACPI_REGISTER_OUTPUT, &zero);
	acpi_io_gpe0_enable(sc, ACPI_REGISTER_OUTPUT, &zero);
	acpi_io_gpe1_enable(sc, ACPI_REGISTER_OUTPUT, &zero);
    }
}

void
acpi_clear_ignore_events(void *arg)
{
    ((acpi_softc_t *)arg)->ignore_events = 0;
    ACPI_DEBUGPRINT("ignore events cleared\n");
}

/*
 * Transition the rest of the system through state changes.
 */
int
acpi_send_pm_event(acpi_softc_t *sc, u_int8_t state)
{
    int	error;

    error = 0;
    switch (state) {
    case ACPI_S_STATE_S0:
	if (sc->system_state != ACPI_S_STATE_S0) {
	    DEVICE_RESUME(root_bus);
	}
	break;
    case ACPI_S_STATE_S1:
    case ACPI_S_STATE_S2:
    case ACPI_S_STATE_S3:
    case ACPI_S_STATE_S4:
	error = DEVICE_SUSPEND(root_bus);
	break;
    default:
	break;
    }

    return (error);
}

/*
 * Event-handler thread.
 */
void 
acpi_queue_event(acpi_softc_t *sc, int type, int arg)
{
    struct acpi_event	*ae;
    int			s;

    ae = malloc(sizeof(*ae), M_TEMP, M_NOWAIT);
    if(ae == NULL)
	panic("acpi_queue_event: can't allocate event");
	
    ae->ae_type = type;
    ae->ae_arg = arg;
    s = splhigh();
    STAILQ_INSERT_TAIL(&sc->event, ae, ae_q);
    splx(s);
    wakeup(&sc->event);
}

void 
acpi_event_thread(void *arg)
{
    acpi_softc_t	*sc = arg;
    int			s , gpe1_base = sc->facp_body->gpe1_base;
    u_int32_t		status,bit;
    struct acpi_event	*ae;
    const char		numconv[] = {'0','1','2','3','4','5','6','7',
				     '8','9','A','B','C','D','E','F',-1};
    char		gpemethod[] = "\\_GPE._LXX";
    union aml_object	argv;	/* Dummy*/

    while(1) {
	s = splhigh();
	if ((ae = STAILQ_FIRST(&sc->event)) == NULL) {
	    splx(s);
	    tsleep(&sc->event, PWAIT, "acpiev", 0);
	    continue;
	} else {
	    splx(s);
	}
	s = splhigh();
	STAILQ_REMOVE_HEAD_UNTIL(&sc->event, ae, ae_q);
	splx(s);
	switch(ae->ae_type) {
	case ACPI_EVENT_TYPE_GPEREG:
	    sprintf(gpemethod, "\\_GPE._L%c%c",
		    numconv[(ae->ae_arg / 0x10) & 0xf],
		    numconv[ae->ae_arg & 0xf]);
	    aml_invoke_method_by_name(gpemethod, 0, &argv);
	    sprintf(gpemethod, "\\_GPE._E%c%c",
		    numconv[(ae->ae_arg / 0x10) & 0xf],
		    numconv[ae->ae_arg & 0xf]);
	    aml_invoke_method_by_name(gpemethod, 0, &argv);
	    s=splhigh();
	    if((ae->ae_arg < gpe1_base) || (gpe1_base == 0)){
		bit = 1 << ae->ae_arg;
		ACPI_DEBUGPRINT("GPE0%x\n", bit);
		acpi_io_gpe0_status(sc, ACPI_REGISTER_OUTPUT,
				    &bit);
		acpi_io_gpe0_enable(sc, ACPI_REGISTER_INPUT,
				    &status);
		ACPI_DEBUGPRINT("GPE0%x\n", status);
		status |= bit;
		acpi_io_gpe0_enable(sc, ACPI_REGISTER_OUTPUT,
				    &status);
	    } else {
		bit = 1 << (ae->ae_arg - sc->facp_body->gpe1_base);
		acpi_io_gpe1_status(sc, ACPI_REGISTER_OUTPUT,
				    &bit);
		acpi_io_gpe1_enable(sc, ACPI_REGISTER_INPUT,
				    &status);
		status |= bit;
		acpi_io_gpe1_enable(sc, ACPI_REGISTER_OUTPUT,
				    &status);
	    }
	    splx(s);
	    break;
	}
	free(ae, M_TEMP);
    }
    ACPI_DEVPRINTF("????\n");
}
OpenPOWER on IntegriCloud