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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022 Tetsuya Uemura <t_uemura@macome.co.jp>
*
* 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.
*/
#include <sys/cdefs.h>
#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/callout.h>
#include <sys/eventhandler.h>
#include <sys/interrupt.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/queue.h>
#include <sys/rman.h>
#include <sys/sysctl.h>
#include <sys/watchdog.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <contrib/dev/acpica/include/acpi.h>
#include <contrib/dev/acpica/include/accommon.h>
#include <contrib/dev/acpica/include/aclocal.h>
#include <contrib/dev/acpica/include/actables.h>
#include <dev/acpica/acpivar.h>
/*
* Resource entry. Every instruction has the corresponding ACPI GAS but two or
* more instructions may access the same or adjacent register region(s). So we
* need to merge all the specified resources.
*
* res Resource when allocated.
* start Region start address.
* end Region end address + 1.
* rid Resource rid assigned when allocated.
* type ACPI resource type, SYS_RES_IOPORT or SYS_RES_MEMORY.
* link Next/previous resource entry.
*/
struct wdat_res {
struct resource *res;
uint64_t start;
uint64_t end;
int rid;
int type;
TAILQ_ENTRY(wdat_res) link;
};
/*
* Instruction entry. Every instruction itself is actually a single register
* read or write (and subsequent bit operation(s)).
* 0 or more instructions are tied to every watchdog action and once an action
* is kicked, the corresponding entries run sequentially.
*
* entry Permanent copy of ACPI_WDAT_ENTRY entry (sub-table).
* next Next instruction entry.
*/
struct wdat_instr {
ACPI_WDAT_ENTRY entry;
STAILQ_ENTRY(wdat_instr) next;
};
/*
* dev Watchdog device.
* wdat ACPI WDAT table, can be accessed until AcpiPutTable().
* default_timeout BIOS configured watchdog ticks to fire.
* timeout User configured timeout in millisecond or 0 if isn't set.
* max Max. supported watchdog ticks to be set.
* min Min. supported watchdog ticks to be set.
* period Milliseconds per watchdog tick.
* running True if this watchdog is running or false if stopped.
* stop_in_sleep False if this watchdog keeps counting down during sleep.
* ev_tag Tag for EVENTHANDLER_*().
* action Array of watchdog instruction sets, each indexed by action.
*/
struct wdatwd_softc {
device_t dev;
ACPI_TABLE_WDAT *wdat;
uint64_t default_timeout;
uint64_t timeout;
u_int max;
u_int min;
u_int period;
bool running;
bool stop_in_sleep;
eventhandler_tag ev_tag;
STAILQ_HEAD(, wdat_instr) action[ACPI_WDAT_ACTION_RESERVED];
TAILQ_HEAD(res_head, wdat_res) res;
};
#define WDATWD_VERBOSE_PRINTF(dev, ...) \
do { \
if (bootverbose) \
device_printf(dev, __VA_ARGS__); \
} while (0)
/*
* Do requested action.
*/
static int
wdatwd_action(const struct wdatwd_softc *sc, const u_int action, const uint64_t val, uint64_t *ret)
{
struct wdat_instr *wdat;
const char *rw = NULL;
ACPI_STATUS status;
if (STAILQ_EMPTY(&sc->action[action])) {
WDATWD_VERBOSE_PRINTF(sc->dev,
"action not supported: 0x%02x\n", action);
return (EOPNOTSUPP);
}
STAILQ_FOREACH(wdat, &sc->action[action], next) {
ACPI_GENERIC_ADDRESS *gas = &wdat->entry.RegisterRegion;
uint64_t x, y;
switch (wdat->entry.Instruction
& ~ACPI_WDAT_PRESERVE_REGISTER) {
case ACPI_WDAT_READ_VALUE:
status = AcpiRead(&x, gas);
if (ACPI_FAILURE(status)) {
rw = "AcpiRead";
goto fail;
}
x >>= gas->BitOffset;
x &= wdat->entry.Mask;
*ret = (x == wdat->entry.Value) ? 1 : 0;
break;
case ACPI_WDAT_READ_COUNTDOWN:
status = AcpiRead(&x, gas);
if (ACPI_FAILURE(status)) {
rw = "AcpiRead";
goto fail;
}
x >>= gas->BitOffset;
x &= wdat->entry.Mask;
*ret = x;
break;
case ACPI_WDAT_WRITE_VALUE:
x = wdat->entry.Value & wdat->entry.Mask;
x <<= gas->BitOffset;
if (wdat->entry.Instruction
& ACPI_WDAT_PRESERVE_REGISTER) {
status = AcpiRead(&y, gas);
if (ACPI_FAILURE(status)) {
rw = "AcpiRead";
goto fail;
}
y &= ~(wdat->entry.Mask << gas->BitOffset);
x |= y;
}
status = AcpiWrite(x, gas);
if (ACPI_FAILURE(status)) {
rw = "AcpiWrite";
goto fail;
}
break;
case ACPI_WDAT_WRITE_COUNTDOWN:
x = val & wdat->entry.Mask;
x <<= gas->BitOffset;
if (wdat->entry.Instruction
& ACPI_WDAT_PRESERVE_REGISTER) {
status = AcpiRead(&y, gas);
if (ACPI_FAILURE(status)) {
rw = "AcpiRead";
goto fail;
}
y &= ~(wdat->entry.Mask << gas->BitOffset);
x |= y;
}
status = AcpiWrite(x, gas);
if (ACPI_FAILURE(status)) {
rw = "AcpiWrite";
goto fail;
}
break;
default:
return (EINVAL);
}
}
return (0);
fail:
device_printf(sc->dev, "action: 0x%02x, %s() returned: %d\n",
action, rw, status);
return (ENXIO);
}
/*
* Reset the watchdog countdown.
*/
static int
wdatwd_reset_countdown(const struct wdatwd_softc *sc)
{
return wdatwd_action(sc, ACPI_WDAT_RESET, 0, NULL);
}
/*
* Set the watchdog countdown value. In WDAT specification, this is optional.
*/
static int
wdatwd_set_countdown(struct wdatwd_softc *sc, u_int cmd)
{
uint64_t timeout;
int e;
cmd &= WD_INTERVAL;
timeout = ((uint64_t) 1 << cmd) / 1000000 / sc->period;
if (timeout > sc->max)
timeout = sc->max;
else if (timeout < sc->min)
timeout = sc->min;
e = wdatwd_action(sc, ACPI_WDAT_SET_COUNTDOWN, timeout, NULL);
if (e == 0)
sc->timeout = timeout * sc->period;
return (e);
}
/*
* Get the watchdog current countdown value.
*/
static int
wdatwd_get_current_countdown(const struct wdatwd_softc *sc, uint64_t *timeout)
{
return wdatwd_action(sc, ACPI_WDAT_GET_CURRENT_COUNTDOWN, 0, timeout);
}
/*
* Get the watchdog countdown value the watchdog is configured to fire.
*/
static int
wdatwd_get_countdown(const struct wdatwd_softc *sc, uint64_t *timeout)
{
return wdatwd_action(sc, ACPI_WDAT_GET_COUNTDOWN, 0, timeout);
}
/*
* Set the watchdog to running state.
*/
static int
wdatwd_set_running(struct wdatwd_softc *sc)
{
int e;
e = wdatwd_action(sc, ACPI_WDAT_SET_RUNNING_STATE, 0, NULL);
if (e == 0)
sc->running = true;
return (e);
}
/*
* Set the watchdog to stopped state.
*/
static int
wdatwd_set_stop(struct wdatwd_softc *sc)
{
int e;
e = wdatwd_action(sc, ACPI_WDAT_SET_STOPPED_STATE, 0, NULL);
if (e == 0)
sc->running = false;
return (e);
}
/*
* Clear the watchdog's boot status if the current boot was caused by the
* watchdog firing.
*/
static int
wdatwd_clear_status(const struct wdatwd_softc *sc)
{
return wdatwd_action(sc, ACPI_WDAT_SET_STATUS, 0, NULL);
}
/*
* Set the watchdog to reboot when it is fired.
*/
static int
wdatwd_set_reboot(const struct wdatwd_softc *sc)
{
return wdatwd_action(sc, ACPI_WDAT_SET_REBOOT, 0, NULL);
}
/*
* Watchdog event handler.
*/
static void
wdatwd_event(void *private, u_int cmd, int *error)
{
struct wdatwd_softc *sc = private;
uint64_t cur[2], cnt[2];
bool run[2];
if (bootverbose) {
run[0] = sc->running;
if (wdatwd_get_countdown(sc, &cnt[0]) != 0)
cnt[0] = 0;
if (wdatwd_get_current_countdown(sc, &cur[0]) != 0)
cur[0] = 0;
}
if ((cmd & WD_INTERVAL) == 0)
wdatwd_set_stop(sc);
else {
if (!sc->running) {
/* ACPI_WDAT_SET_COUNTDOWN may not be implemented. */
wdatwd_set_countdown(sc, cmd);
wdatwd_set_running(sc);
/*
* In the first wdatwd_event() call, it sets the
* watchdog timeout to a considerably larger value such
* as 137 seconds, then kicks the watchdog to start
* counting down. Weirdly though, on a Dell R210 BIOS
* 1.12.0, a supplemental reset action must be
* triggered for the newly set timeout value to take
* effect. Without it, the watchdog fires 2.4 seconds
* after starting, where 2.4 seconds is its initially
* set timeout. This failure scenario is seen by first
* starting watchdogd(8) without wdatwd registered then
* kldload it. In steady state, watchdogd pats the
* watchdog every 10 or so seconds which is much longer
* than 2.4 seconds timeout.
*/
}
wdatwd_reset_countdown(sc);
}
if (bootverbose) {
run[1] = sc->running;
if (wdatwd_get_countdown(sc, &cnt[1]) != 0)
cnt[1] = 0;
if (wdatwd_get_current_countdown(sc, &cur[1]) != 0)
cur[1] = 0;
WDATWD_VERBOSE_PRINTF(sc->dev, "cmd: %u, sc->running: "
"%d -> %d, cnt: %llu -> %llu, cur: %llu -> %llu\n", cmd,
run[0], run[1],
(unsigned long long) cnt[0],
(unsigned long long) cnt[1],
(unsigned long long)cur[0],
(unsigned long long)cur[1]);
}
return;
}
static ssize_t
wdat_set_action(struct wdatwd_softc *sc, ACPI_WDAT_ENTRY *addr, ssize_t remaining)
{
ACPI_WDAT_ENTRY *entry = addr;
struct wdat_instr *wdat;
if (remaining < sizeof(ACPI_WDAT_ENTRY))
return (-EINVAL);
/* Skip actions beyond specification. */
if (entry->Action < nitems(sc->action)) {
wdat = malloc(sizeof(*wdat), M_DEVBUF, M_WAITOK | M_ZERO);
wdat->entry = *entry;
STAILQ_INSERT_TAIL(&sc->action[entry->Action], wdat, next);
}
return sizeof(ACPI_WDAT_ENTRY);
}
/*
* Transform every ACPI_WDAT_ENTRY to wdat_instr by calling wdat_set_action().
*/
static void
wdat_parse_action_table(struct wdatwd_softc *sc)
{
ACPI_TABLE_WDAT *wdat = sc->wdat;
ssize_t remaining, consumed;
char *cp;
remaining = wdat->Header.Length - sizeof(ACPI_TABLE_WDAT);
while (remaining > 0) {
cp = (char *)wdat + wdat->Header.Length - remaining;
consumed = wdat_set_action(sc, (ACPI_WDAT_ENTRY *)cp,
remaining);
if (consumed < 0) {
device_printf(sc->dev, "inconsistent WDAT table.\n");
break;
}
remaining -= consumed;
}
}
/*
* Decode the given GAS rr and set its type, start and end (actually end + 1)
* in the newly malloc()'ed res.
*/
static struct wdat_res *
wdat_alloc_region(ACPI_GENERIC_ADDRESS *rr)
{
struct wdat_res *res;
if (rr->AccessWidth < 1 || rr->AccessWidth > 4)
return (NULL);
res = malloc(sizeof(*res),
M_DEVBUF, M_WAITOK | M_ZERO);
if (res != NULL) {
res->start = rr->Address;
res->end = res->start + (1 << (rr->AccessWidth - 1));
res->type = rr->SpaceId;
}
return (res);
}
#define OVERLAP_NONE 0x0 // no overlap.
#define OVERLAP_SUBSET 0x1 // res2 is fully covered by res1.
#define OVERLAP_START 0x2 // the start of res2 is overlaped.
#define OVERLAP_END 0x4 // the end of res2 is overlapped.
/*
* Compare the given res1 and res2, and one of the above OVERLAP_* constant, or
* in case res2 is larger than res1 at both the start and the end,
* OVERLAP_START | OVERLAP_END, is returned.
*/
static int
wdat_compare_region(const struct wdat_res *res1, const struct wdat_res *res2)
{
int overlap;
/*
* a) both have different resource type. == OVERLAP_NONE
* b) res2 and res1 have no overlap. == OVERLAP_NONE
* c) res2 is fully covered by res1. == OVERLAP_SUBSET
* d) res2 and res1 overlap partially. == OVERLAP_START or
* OVERLAP_END
* e) res2 fully covers res1. == OVERLAP_START | OVERLAP_END
*/
overlap = 0;
if (res1->type != res2->type || res1->start > res2->end
|| res1->end < res2->start)
overlap |= OVERLAP_NONE;
else {
if (res1->start <= res2->start && res1->end >= res2->end)
overlap |= OVERLAP_SUBSET;
if (res1->start > res2->start)
overlap |= OVERLAP_START;
if (res1->end < res2->end)
overlap |= OVERLAP_END;
}
return (overlap);
}
/*
* Try to merge the given newres with the existing sc->res.
*/
static void
wdat_merge_region(struct wdatwd_softc *sc, struct wdat_res *newres)
{
struct wdat_res *res1, *res2, *res_safe, *res_itr;
int overlap;
if (TAILQ_EMPTY(&sc->res)) {
TAILQ_INSERT_HEAD(&sc->res, newres, link);
return;
}
overlap = OVERLAP_NONE;
TAILQ_FOREACH_SAFE(res1, &sc->res, link, res_safe) {
overlap = wdat_compare_region(res1, newres);
/* Try next res if newres isn't mergeable. */
if (overlap == OVERLAP_NONE)
continue;
/* This res fully covers newres. */
if (overlap == OVERLAP_SUBSET)
break;
/* Newres extends the existing res res1 to lower. */
if ((overlap & OVERLAP_START)) {
res1->start = newres->start;
res_itr = res1;
/* Try to merge more res if possible. */
while ((res2 = TAILQ_PREV(res_itr, res_head, link))) {
if (res1->type != res2->type) {
res_itr = res2;
continue;
} else if (res1->start <= res2->end) {
res1->start = res2->start;
TAILQ_REMOVE(&sc->res, res2, link);
free(res2, M_DEVBUF);
} else
break;
}
}
/* Newres extends the existing res res1 to upper. */
if ((overlap & OVERLAP_END)) {
res1->end = newres->end;
res_itr = res1;
/* Try to merge more res if possible. */
while ((res2 = TAILQ_NEXT(res_itr, link))) {
if (res1->type != res2->type) {
res_itr = res2;
continue;
} else if (res1->end >= res2->start) {
res1->end = res2->end;
TAILQ_REMOVE(&sc->res, res2, link);
free(res2, M_DEVBUF);
} else
break;
}
}
break;
}
/*
* If newres extends the existing res, newres must be free()'ed.
* Otherwise insert newres into sc->res at appropriate position
* (the lowest address region appears first).
*/
if (overlap > OVERLAP_NONE)
free(newres, M_DEVBUF);
else {
TAILQ_FOREACH(res1, &sc->res, link) {
if (newres->type != res1->type)
continue;
if (newres->start < res1->start) {
TAILQ_INSERT_BEFORE(res1, newres, link);
break;
}
}
if (res1 == NULL)
TAILQ_INSERT_TAIL(&sc->res, newres, link);
}
}
/*
* Release the already allocated resource.
*/
static void
wdat_release_resource(device_t dev)
{
struct wdatwd_softc *sc;
struct wdat_instr *wdat;
struct wdat_res *res;
int i;
sc = device_get_softc(dev);
TAILQ_FOREACH(res, &sc->res, link)
if (res->res != NULL) {
bus_release_resource(dev, res->type,
res->rid, res->res);
bus_delete_resource(dev, res->type, res->rid);
res->res = NULL;
}
for (i = 0; i < nitems(sc->action); ++i)
while (!STAILQ_EMPTY(&sc->action[i])) {
wdat = STAILQ_FIRST(&sc->action[i]);
STAILQ_REMOVE_HEAD(&sc->action[i], next);
free(wdat, M_DEVBUF);
}
while (!TAILQ_EMPTY(&sc->res)) {
res = TAILQ_FIRST(&sc->res);
TAILQ_REMOVE(&sc->res, res, link);
free(res, M_DEVBUF);
}
}
static int
wdatwd_probe(device_t dev)
{
ACPI_TABLE_WDAT *wdat;
ACPI_STATUS status;
/* Without WDAT table we have nothing to do. */
status = AcpiGetTable(ACPI_SIG_WDAT, 0, (ACPI_TABLE_HEADER **)&wdat);
if (ACPI_FAILURE(status))
return (ENXIO);
/* Try to allocate one resource and assume wdatwd is already attached
* if it fails. */
{
int type, rid = 0;
struct resource *res;
if (acpi_bus_alloc_gas(dev, &type, &rid,
&((ACPI_WDAT_ENTRY *)(wdat + 1))->RegisterRegion,
&res, 0))
return (ENXIO);
bus_release_resource(dev, type, rid, res);
bus_delete_resource(dev, type, rid);
}
WDATWD_VERBOSE_PRINTF(dev, "Flags: 0x%x, TimerPeriod: %d ms/cnt, "
"MaxCount: %d cnt (%d ms), MinCount: %d cnt (%d ms)\n",
(int)wdat->Flags, (int)wdat->TimerPeriod,
(int)wdat->MaxCount, (int)(wdat->MaxCount * wdat->TimerPeriod),
(int)wdat->MinCount, (int)(wdat->MinCount * wdat->TimerPeriod));
/* WDAT timer consistency. */
if ((wdat->TimerPeriod < 1) || (wdat->MinCount > wdat->MaxCount)) {
device_printf(dev, "inconsistent timer variables.\n");
return (EINVAL);
}
AcpiPutTable((ACPI_TABLE_HEADER *)wdat);
device_set_desc(dev, "ACPI WDAT Watchdog Interface");
return (BUS_PROBE_DEFAULT);
}
static int
wdatwd_attach(device_t dev)
{
struct wdatwd_softc *sc;
struct wdat_instr *wdat;
struct wdat_res *res;
struct sysctl_ctx_list *sctx;
struct sysctl_oid *soid;
ACPI_STATUS status;
int e, i, rid;
sc = device_get_softc(dev);
sc->dev = dev;
for (i = 0; i < nitems(sc->action); ++i)
STAILQ_INIT(&sc->action[i]);
/* Search and parse WDAT table. */
status = AcpiGetTable(ACPI_SIG_WDAT, 0,
(ACPI_TABLE_HEADER **)&sc->wdat);
if (ACPI_FAILURE(status))
return (ENXIO);
/* Parse watchdog variables. */
sc->period = sc->wdat->TimerPeriod;
sc->max = sc->wdat->MaxCount;
sc->min = sc->wdat->MinCount;
sc->stop_in_sleep = (sc->wdat->Flags & ACPI_WDAT_STOPPED)
? true : false;
/* Parse defined watchdog actions. */
wdat_parse_action_table(sc);
AcpiPutTable((ACPI_TABLE_HEADER *)sc->wdat);
/* Verbose logging. */
if (bootverbose) {
for (i = 0; i < nitems(sc->action); ++i)
STAILQ_FOREACH(wdat, &sc->action[i], next) {
WDATWD_VERBOSE_PRINTF(dev, "action: 0x%02x, "
"%s %s at 0x%llx (%d bit(s), offset %d bit(s))\n",
i,
wdat->entry.RegisterRegion.SpaceId
== ACPI_ADR_SPACE_SYSTEM_MEMORY
? "mem"
: wdat->entry.RegisterRegion.SpaceId
== ACPI_ADR_SPACE_SYSTEM_IO
? "io "
: "???",
wdat->entry.RegisterRegion.AccessWidth == 1
? "byte "
: wdat->entry.RegisterRegion.AccessWidth == 2
? "word "
: wdat->entry.RegisterRegion.AccessWidth == 3
? "dword"
: wdat->entry.RegisterRegion.AccessWidth == 4
? "qword"
: "undef",
(unsigned long long )
wdat->entry.RegisterRegion.Address,
wdat->entry.RegisterRegion.BitWidth,
wdat->entry.RegisterRegion.BitOffset);
}
}
/* Canonicalize the requested resources. */
TAILQ_INIT(&sc->res);
for (i = 0; i < nitems(sc->action); ++i)
STAILQ_FOREACH(wdat, &sc->action[i], next) {
res = wdat_alloc_region(&wdat->entry.RegisterRegion);
if (res == NULL)
goto fail;
wdat_merge_region(sc, res);
}
/* Resource allocation. */
rid = 0;
TAILQ_FOREACH(res, &sc->res, link) {
switch (res->type) {
case ACPI_ADR_SPACE_SYSTEM_MEMORY:
res->type = SYS_RES_MEMORY;
break;
case ACPI_ADR_SPACE_SYSTEM_IO:
res->type = SYS_RES_IOPORT;
break;
default:
goto fail;
}
res->rid = rid++;
bus_set_resource(dev, res->type, res->rid,
res->start, res->end - res->start);
res->res = bus_alloc_resource_any(
dev, res->type, &res->rid, RF_ACTIVE);
if (res->res == NULL) {
bus_delete_resource(dev, res->type, res->rid);
device_printf(dev, "%s at 0x%llx (%lld byte(s)): "
"alloc' failed\n",
res->type == SYS_RES_MEMORY ? "mem" : "io ",
(unsigned long long )res->start,
(unsigned long long )(res->end - res->start));
goto fail;
}
WDATWD_VERBOSE_PRINTF(dev, "%s at 0x%llx (%lld byte(s)): "
"alloc'ed\n",
res->type == SYS_RES_MEMORY ? "mem" : "io ",
(unsigned long long )res->start,
(unsigned long long) (res->end - res->start));
}
/* Initialize the watchdog hardware. */
if (wdatwd_set_stop(sc) != 0)
goto fail;
if ((e = wdatwd_clear_status(sc)) && e != EOPNOTSUPP)
goto fail;
if ((e = wdatwd_set_reboot(sc)) && e != EOPNOTSUPP)
goto fail;
if ((e = wdatwd_get_countdown(sc, &sc->default_timeout))
&& e != EOPNOTSUPP)
goto fail;
WDATWD_VERBOSE_PRINTF(dev, "initialized.\n");
/* Some sysctls. Most of them should go to WDATWD_VERBOSE_PRINTF(). */
sctx = device_get_sysctl_ctx(dev);
soid = device_get_sysctl_tree(dev);
SYSCTL_ADD_U64(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
"timeout_default", CTLFLAG_RD, SYSCTL_NULL_U64_PTR,
sc->default_timeout * sc->period,
"The default watchdog timeout in millisecond.");
SYSCTL_ADD_BOOL(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
"timeout_configurable", CTLFLAG_RD, SYSCTL_NULL_BOOL_PTR,
STAILQ_EMPTY(&sc->action[ACPI_WDAT_SET_COUNTDOWN]) ? false : true,
"Whether the watchdog timeout is configurable or not.");
SYSCTL_ADD_U64(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
"timeout", CTLFLAG_RD, &sc->timeout, 0,
"The current watchdog timeout in millisecond. "
"If 0, the default timeout is used.");
SYSCTL_ADD_BOOL(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
"running", CTLFLAG_RD, &sc->running, 0,
"Whether the watchdog timer is running or not.");
sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, wdatwd_event, sc,
EVENTHANDLER_PRI_ANY);
WDATWD_VERBOSE_PRINTF(dev, "watchdog registered.\n");
return (0);
fail:
wdat_release_resource(dev);
return (ENXIO);
}
static int
wdatwd_detach(device_t dev)
{
struct wdatwd_softc *sc;
int e;
sc = device_get_softc(dev);
EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
e = wdatwd_set_stop(sc);
wdat_release_resource(dev);
return (e);
}
static int
wdatwd_suspend(device_t dev)
{
struct wdatwd_softc *sc;
sc = device_get_softc(dev);
if (!sc->stop_in_sleep)
return (0);
return wdatwd_set_stop(sc);
}
static int
wdatwd_resume(device_t dev)
{
struct wdatwd_softc *sc;
sc = device_get_softc(dev);
if (!sc->stop_in_sleep)
return (0);
return (wdatwd_reset_countdown(sc) || wdatwd_set_running(sc));
}
static device_method_t wdatwd_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, wdatwd_probe),
DEVMETHOD(device_attach, wdatwd_attach),
DEVMETHOD(device_detach, wdatwd_detach),
DEVMETHOD(device_shutdown, wdatwd_detach),
DEVMETHOD(device_suspend, wdatwd_suspend),
DEVMETHOD(device_resume, wdatwd_resume),
DEVMETHOD_END
};
static driver_t wdatwd_driver = {
"wdatwd",
wdatwd_methods,
sizeof(struct wdatwd_softc),
};
DRIVER_MODULE(wdatwd, acpi, wdatwd_driver, 0, 0);
MODULE_DEPEND(wdatwd, acpi, 1, 1, 1);
|