Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/* $NetBSD: acpi_wdrt.c,v 1.4 2015/04/23 23:23:00 pgoyette Exp $ */

/*
 * Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca>
 * 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. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
 */

/*
 * ACPI "WDRT" watchdog support, based on:
 *
 * Watchdog Timer Hardware Requirements for Windows Server 2003, version 1.01
 * http://www.microsoft.com/whdc/system/sysinternals/watchdog.mspx
 */

/* #define ACPIWDRT_DEBUG */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi_wdrt.c,v 1.4 2015/04/23 23:23:00 pgoyette Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/module.h>

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

#include <dev/sysmon/sysmonvar.h>

#define _COMPONENT	ACPI_RESOURCE_COMPONENT
ACPI_MODULE_NAME	("acpi_wdrt")

#ifdef ACPIWDRT_DEBUG
#define DPRINTF(x)	printf x
#else
#define	DPRINTF(x)	do { } while (0)
#endif

/* Watchdog Control/Status Register (32 bits) */
#define	ACPI_WDRT_CSR_RUNSTOP		(1 << 0)	/* rw */
#define	 ACPI_WDRT_RUNSTOP_STOPPED	 (0 << 0)
#define	 ACPI_WDRT_RUNSTOP_RUNNING	 (1 << 0)
#define	ACPI_WDRT_CSR_FIRED		(1 << 1)	/* rw */
#define	ACPI_WDRT_CSR_ACTION		(1 << 2)	/* rw */
#define	 ACPI_WDRT_ACTION_RESET		 (0 << 2)
#define	 ACPI_WDRT_ACTION_POWEROFF	 (1 << 2)
#define	ACPI_WDRT_CSR_WDOGEN		(1 << 3)	/* ro */
#define	 ACPI_WDRT_WDOGEN_ENABLE	 (0 << 3)
#define	 ACPI_WDRT_WDOGEN_DISABLE	 (1 << 3)
#define	ACPI_WDRT_CSR_TRIGGER		(1 << 7)	/* wo */
/* Watchdog Count Register (32 bits) */
#define	ACPI_WDRT_COUNT_DATA_MASK	0xffff

/* Watchdog Resource Table - Units */
#define	ACPI_WDRT_UNITS_1S		0x0
#define	ACPI_WDRT_UNITS_100MS		0x1
#define	ACPI_WDRT_UNITS_10MS		0x2

struct acpi_wdrt_softc {
	device_t		sc_dev;

	bus_space_tag_t		sc_memt;
	bus_space_handle_t	sc_memh;

	struct sysmon_wdog	sc_smw;
	bool			sc_smw_valid;

	uint32_t		sc_max_period;
	unsigned int		sc_period_scale;

	ACPI_GENERIC_ADDRESS	sc_control_reg;
	ACPI_GENERIC_ADDRESS	sc_count_reg;
};

static int	acpi_wdrt_match(device_t, cfdata_t, void *);
static void	acpi_wdrt_attach(device_t, device_t, void *);
static int	acpi_wdrt_detach(device_t, int);
static bool	acpi_wdrt_suspend(device_t, const pmf_qual_t *);

static int	acpi_wdrt_setmode(struct sysmon_wdog *);
static int	acpi_wdrt_tickle(struct sysmon_wdog *);

static ACPI_STATUS acpi_wdrt_read_control(struct acpi_wdrt_softc *, uint64_t *);
static ACPI_STATUS acpi_wdrt_write_control(struct acpi_wdrt_softc *, uint64_t);
#if 0
static ACPI_STATUS acpi_wdrt_read_count(struct acpi_wdrt_softc *, uint32_t *);
#endif
static ACPI_STATUS acpi_wdrt_write_count(struct acpi_wdrt_softc *, uint32_t);

CFATTACH_DECL_NEW(
    acpiwdrt,
    sizeof(struct acpi_wdrt_softc),
    acpi_wdrt_match,
    acpi_wdrt_attach,
    acpi_wdrt_detach,
    NULL
);

static int
acpi_wdrt_match(device_t parent, cfdata_t match, void *opaque)
{
	ACPI_TABLE_WDRT *wdrt;
	ACPI_STATUS rv;
	uint64_t val;

	rv = AcpiGetTable(ACPI_SIG_WDRT, 1, (ACPI_TABLE_HEADER **)&wdrt);
	if (ACPI_FAILURE(rv))
		return 0;

	/* Only system memory address spaces are allowed */
	if (wdrt->ControlRegister.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY ||
	    wdrt->CountRegister.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
		return 0;
	}
	/* Sanity check control & count register addresses */
	if (wdrt->ControlRegister.Address == 0 ||
	    wdrt->ControlRegister.Address == 0xffffffff ||
	    wdrt->ControlRegister.Address == 0xffffffffffffffff ||
	    wdrt->CountRegister.Address == 0 ||
	    wdrt->CountRegister.Address == 0xffffffff ||
	    wdrt->CountRegister.Address == 0xffffffffffffffff) {
		return 0;
	}

	/* Read control regster */
	rv = AcpiOsReadMemory(wdrt->ControlRegister.Address, &val,
	    wdrt->ControlRegister.BitWidth);
	if (ACPI_FAILURE(rv))
		return 0;

	/* Make sure the hardware watchdog is enabled */
	if ((val & ACPI_WDRT_CSR_WDOGEN) == ACPI_WDRT_WDOGEN_DISABLE)
		return 0;

	return 1;
}

static void
acpi_wdrt_attach(device_t parent, device_t self, void *opaque)
{
	struct acpi_wdrt_softc *sc = device_private(self);
	ACPI_TABLE_WDRT *wdrt;
	ACPI_STATUS rv;

	sc->sc_dev = self;

	pmf_device_register(self, acpi_wdrt_suspend, NULL);

	rv = AcpiGetTable(ACPI_SIG_WDRT, 1, (ACPI_TABLE_HEADER **)&wdrt);
	if (ACPI_FAILURE(rv)) {
		aprint_error(": couldn't get WDRT (%s)\n",
		    AcpiFormatException(rv));
		return;
	}

	/* Maximum counter value must be 511 - 65535 */
	if (wdrt->MaxCount < 511) {
		aprint_error(": maximum counter value out of range (%d)\n",
		    wdrt->MaxCount);
		return;
	}
	/* Counter units can be 1s, 100ms, or 10ms */
	switch (wdrt->Units) {
	case ACPI_WDRT_UNITS_1S:
	case ACPI_WDRT_UNITS_100MS:
	case ACPI_WDRT_UNITS_10MS:
		break;
	default:
		aprint_error(": units not supported (0x%x)\n", wdrt->Units);
		return;
	}

	sc->sc_control_reg = wdrt->ControlRegister;
	sc->sc_count_reg = wdrt->CountRegister;

	aprint_naive("\n");
	aprint_normal(": mem 0x%" PRIx64 ",0x%" PRIx64 "\n",
	    sc->sc_control_reg.Address, sc->sc_count_reg.Address);

	if (wdrt->PciVendorId != 0xffff && wdrt->PciDeviceId != 0xffff) {
		aprint_verbose_dev(sc->sc_dev, "PCI %u:%03u:%02u:%01u",
		    wdrt->PciSegment, wdrt->PciBus, wdrt->PciDevice,
		    wdrt->PciFunction);
		aprint_verbose(" vendor 0x%04x product 0x%04x\n",
		    wdrt->PciVendorId, wdrt->PciDeviceId);
	}

	sc->sc_max_period = wdrt->MaxCount;
	sc->sc_period_scale = 1;
	if (wdrt->Units == ACPI_WDRT_UNITS_100MS)
		sc->sc_period_scale = 10;
	if (wdrt->Units == ACPI_WDRT_UNITS_10MS)
		sc->sc_period_scale = 100;
	sc->sc_max_period /= sc->sc_period_scale;
	aprint_normal_dev(self, "watchdog interval 1-%d sec.\n",
	    sc->sc_max_period);

	sc->sc_smw.smw_name = device_xname(self);
	sc->sc_smw.smw_cookie = sc;
	sc->sc_smw.smw_setmode = acpi_wdrt_setmode;
	sc->sc_smw.smw_tickle = acpi_wdrt_tickle;
	sc->sc_smw.smw_period = sc->sc_max_period;

	if (sysmon_wdog_register(&sc->sc_smw))
		aprint_error_dev(self, "couldn't register with sysmon\n");
	else
		sc->sc_smw_valid = true;
}

static int
acpi_wdrt_detach(device_t self, int flags)
{
	struct acpi_wdrt_softc *sc = device_private(self);

	/* Don't allow detach if watchdog is armed */
	if (sc->sc_smw_valid &&
	    (sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
		return EBUSY;

	if (sc->sc_smw_valid) {
		sysmon_wdog_unregister(&sc->sc_smw);
		sc->sc_smw_valid = false;
	}

	pmf_device_deregister(self);

	return 0;
}

static bool
acpi_wdrt_suspend(device_t self, const pmf_qual_t *qual)
{
	struct acpi_wdrt_softc *sc = device_private(self);

	if (sc->sc_smw_valid == false)
		return true;

	/* Don't allow suspend if watchdog is armed */
	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
		return false;

	return true;
}

static int
acpi_wdrt_setmode(struct sysmon_wdog *smw)
{
	struct acpi_wdrt_softc *sc = smw->smw_cookie;
	uint64_t val;

	DPRINTF(("%s: %s mode 0x%x period %u\n", device_xname(sc->sc_dev),
	    __func__, smw->smw_mode, smw->smw_period));

	switch (smw->smw_mode & WDOG_MODE_MASK) {
	case WDOG_MODE_DISARMED:
		/* Disable watchdog timer */
		if (ACPI_FAILURE(acpi_wdrt_read_control(sc, &val)))
			goto failed;
		val &= ~ACPI_WDRT_CSR_RUNSTOP;
		val |= ACPI_WDRT_RUNSTOP_STOPPED;
		if (ACPI_FAILURE(acpi_wdrt_write_control(sc, val)))
			goto failed;
		break;
	default:
		if (smw->smw_period == 0)
			return EINVAL;
		if (smw->smw_period > sc->sc_max_period)
			smw->smw_period = sc->sc_max_period;

		/* Enable watchdog timer */
		if (ACPI_FAILURE(acpi_wdrt_read_control(sc, &val)))
			goto failed;
		val &= ~ACPI_WDRT_CSR_RUNSTOP;
		val &= ~ACPI_WDRT_CSR_ACTION;
		val |= ACPI_WDRT_ACTION_RESET;
		if (ACPI_FAILURE(acpi_wdrt_write_control(sc, val)))
			goto failed;
		/* Reset count register */
		if (acpi_wdrt_tickle(smw))
			goto failed;
		val |= ACPI_WDRT_RUNSTOP_RUNNING;
		if (ACPI_FAILURE(acpi_wdrt_write_control(sc, val)))
			goto failed;
		break;
	}

	return 0;

failed:
	return EIO;
}

static int
acpi_wdrt_tickle(struct sysmon_wdog *smw)
{
	struct acpi_wdrt_softc *sc = smw->smw_cookie;
	uint64_t val;
	
	DPRINTF(("%s: %s mode 0x%x period %u\n", device_xname(sc->sc_dev),
	    __func__, smw->smw_mode, smw->smw_period));

	/* Reset count register */
	val = smw->smw_period * sc->sc_period_scale;
	if (ACPI_FAILURE(acpi_wdrt_write_count(sc, val)))
		return EIO;

	/* Trigger new count interval */
	if (ACPI_FAILURE(acpi_wdrt_read_control(sc, &val)))
		return EIO;
	val |= ACPI_WDRT_CSR_TRIGGER;
	if (ACPI_FAILURE(acpi_wdrt_write_control(sc, val)))
		return EIO;

	return 0;
}

static ACPI_STATUS
acpi_wdrt_read_control(struct acpi_wdrt_softc *sc, uint64_t *val)
{
	ACPI_STATUS rv;

	KASSERT(sc->sc_smw_valid == true);

	rv = AcpiOsReadMemory(sc->sc_control_reg.Address,
	    val, sc->sc_control_reg.BitWidth);

	DPRINTF(("%s: %s 0x%" PRIx64 "/%u 0x%08x (%u)\n",
	    device_xname(sc->sc_dev),
	    __func__, sc->sc_control_reg.Address, sc->sc_control_reg.BitWidth,
	    *val, rv));

	return rv;
}

static ACPI_STATUS
acpi_wdrt_write_control(struct acpi_wdrt_softc *sc, uint64_t val)
{
	ACPI_STATUS rv;

	KASSERT(sc->sc_smw_valid == true);

	rv = AcpiOsWriteMemory(sc->sc_control_reg.Address,
	    val, sc->sc_control_reg.BitWidth);

	DPRINTF(("%s: %s 0x%" PRIx64 "/%u 0x%08x (%u)\n",
	    device_xname(sc->sc_dev),
	    __func__, sc->sc_control_reg.Address, sc->sc_control_reg.BitWidth,
	    val, rv));

	return rv;
}

#if 0
static ACPI_STATUS
acpi_wdrt_read_count(struct acpi_wdrt_softc *sc, uint32_t *val)
{
	ACPI_STATUS rv;

	KASSERT(sc->sc_smw_valid == true);

	rv = AcpiOsReadMemory(sc->sc_count_reg.Address,
	    val, sc->sc_count_reg.BitWidth);

	DPRINTF(("%s: %s 0x%" PRIx64 "/%u 0x%08x (%u)\n",
	    device_xname(sc->sc_dev),
	    __func__, sc->sc_count_reg.Address, sc->sc_count_reg.BitWidth,
	    *val, rv));

	return rv;
}
#endif

static ACPI_STATUS
acpi_wdrt_write_count(struct acpi_wdrt_softc *sc, uint32_t val)
{
	ACPI_STATUS rv;

	KASSERT(sc->sc_smw_valid == true);

	rv = AcpiOsWriteMemory(sc->sc_count_reg.Address,
	    val, sc->sc_count_reg.BitWidth);

	DPRINTF(("%s: %s 0x%" PRIx64 "/%u 0x%08x (%u)\n",
	    device_xname(sc->sc_dev),
	    __func__, sc->sc_count_reg.Address, sc->sc_count_reg.BitWidth,
	    val, rv));

	return rv;
}

MODULE(MODULE_CLASS_DRIVER, acpiwdrt, "sysmon_wdog");

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
acpiwdrt_modcmd(modcmd_t cmd, void *opaque)
{
	int error = 0;

	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		error = config_init_component(cfdriver_ioconf_acpiwdrt,
		    cfattach_ioconf_acpiwdrt, cfdata_ioconf_acpiwdrt);
#endif
		return error;
	case MODULE_CMD_FINI:
#ifdef _MODULE
		error = config_fini_component(cfdriver_ioconf_acpiwdrt,
		    cfattach_ioconf_acpiwdrt, cfdata_ioconf_acpiwdrt);
#endif
		return error;
	default:
		return ENOTTY;
	}
}