Blame view
drivers/watchdog/wdat_wdt.c
13.2 KB
d2912cb15 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
058dfc767 ACPI / watchdog: ... |
2 3 4 5 6 |
/* * ACPI Hardware Watchdog (WDAT) driver. * * Copyright (C) 2016, Intel Corporation * Author: Mika Westerberg <mika.westerberg@linux.intel.com> |
058dfc767 ACPI / watchdog: ... |
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 |
*/ #include <linux/acpi.h> #include <linux/ioport.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/pm.h> #include <linux/watchdog.h> #define MAX_WDAT_ACTIONS ACPI_WDAT_ACTION_RESERVED /** * struct wdat_instruction - Single ACPI WDAT instruction * @entry: Copy of the ACPI table instruction * @reg: Register the instruction is accessing * @node: Next instruction in action sequence */ struct wdat_instruction { struct acpi_wdat_entry entry; void __iomem *reg; struct list_head node; }; /** * struct wdat_wdt - ACPI WDAT watchdog device * @pdev: Parent platform device * @wdd: Watchdog core device * @period: How long is one watchdog period in ms * @stopped_in_sleep: Is this watchdog stopped by the firmware in S1-S5 * @stopped: Was the watchdog stopped by the driver in suspend * @actions: An array of instruction lists indexed by an action number from * the WDAT table. There can be %NULL entries for not implemented * actions. */ struct wdat_wdt { struct platform_device *pdev; struct watchdog_device wdd; unsigned int period; bool stopped_in_sleep; bool stopped; struct list_head *instructions[MAX_WDAT_ACTIONS]; }; #define to_wdat_wdt(wdd) container_of(wdd, struct wdat_wdt, wdd) static bool nowayout = WATCHDOG_NOWAYOUT; module_param(nowayout, bool, 0); MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
87bd74b77 ACPI: watchdog: S... |
56 57 58 59 60 61 |
#define WDAT_DEFAULT_TIMEOUT 30 static int timeout = WDAT_DEFAULT_TIMEOUT; module_param(timeout, int, 0); MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default=" __MODULE_STRING(WDAT_DEFAULT_TIMEOUT) ")"); |
058dfc767 ACPI / watchdog: ... |
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 |
static int wdat_wdt_read(struct wdat_wdt *wdat, const struct wdat_instruction *instr, u32 *value) { const struct acpi_generic_address *gas = &instr->entry.register_region; switch (gas->access_width) { case 1: *value = ioread8(instr->reg); break; case 2: *value = ioread16(instr->reg); break; case 3: *value = ioread32(instr->reg); break; default: return -EINVAL; } dev_dbg(&wdat->pdev->dev, "Read %#x from 0x%08llx ", *value, gas->address); return 0; } static int wdat_wdt_write(struct wdat_wdt *wdat, const struct wdat_instruction *instr, u32 value) { const struct acpi_generic_address *gas = &instr->entry.register_region; switch (gas->access_width) { case 1: iowrite8((u8)value, instr->reg); break; case 2: iowrite16((u16)value, instr->reg); break; case 3: iowrite32(value, instr->reg); break; default: return -EINVAL; } dev_dbg(&wdat->pdev->dev, "Wrote %#x to 0x%08llx ", value, gas->address); return 0; } static int wdat_wdt_run_action(struct wdat_wdt *wdat, unsigned int action, u32 param, u32 *retval) { struct wdat_instruction *instr; if (action >= ARRAY_SIZE(wdat->instructions)) return -EINVAL; if (!wdat->instructions[action]) return -EOPNOTSUPP; dev_dbg(&wdat->pdev->dev, "Running action %#x ", action); /* Run each instruction sequentially */ list_for_each_entry(instr, wdat->instructions[action], node) { const struct acpi_wdat_entry *entry = &instr->entry; const struct acpi_generic_address *gas; u32 flags, value, mask, x, y; bool preserve; int ret; gas = &entry->register_region; preserve = entry->instruction & ACPI_WDAT_PRESERVE_REGISTER; flags = entry->instruction & ~ACPI_WDAT_PRESERVE_REGISTER; value = entry->value; mask = entry->mask; switch (flags) { case ACPI_WDAT_READ_VALUE: ret = wdat_wdt_read(wdat, instr, &x); if (ret) return ret; x >>= gas->bit_offset; x &= mask; if (retval) *retval = x == value; break; case ACPI_WDAT_READ_COUNTDOWN: ret = wdat_wdt_read(wdat, instr, &x); if (ret) return ret; x >>= gas->bit_offset; x &= mask; if (retval) *retval = x; break; case ACPI_WDAT_WRITE_VALUE: x = value & mask; x <<= gas->bit_offset; if (preserve) { ret = wdat_wdt_read(wdat, instr, &y); if (ret) return ret; y = y & ~(mask << gas->bit_offset); x |= y; } ret = wdat_wdt_write(wdat, instr, x); if (ret) return ret; break; case ACPI_WDAT_WRITE_COUNTDOWN: x = param; x &= mask; x <<= gas->bit_offset; if (preserve) { ret = wdat_wdt_read(wdat, instr, &y); if (ret) return ret; y = y & ~(mask << gas->bit_offset); x |= y; } ret = wdat_wdt_write(wdat, instr, x); if (ret) return ret; break; default: dev_err(&wdat->pdev->dev, "Unknown instruction: %u ", flags); return -EINVAL; } } return 0; } static int wdat_wdt_enable_reboot(struct wdat_wdt *wdat) { int ret; /* * WDAT specification says that the watchdog is required to reboot * the system when it fires. However, it also states that it is * recommeded to make it configurable through hardware register. We * enable reboot now if it is configrable, just in case. */ |
cda3b9178 watchdog: wdat_wd... |
216 |
ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_REBOOT, 0, NULL); |
058dfc767 ACPI / watchdog: ... |
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 |
if (ret && ret != -EOPNOTSUPP) { dev_err(&wdat->pdev->dev, "Failed to enable reboot when watchdog triggers "); return ret; } return 0; } static void wdat_wdt_boot_status(struct wdat_wdt *wdat) { u32 boot_status = 0; int ret; ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_STATUS, 0, &boot_status); if (ret && ret != -EOPNOTSUPP) { dev_err(&wdat->pdev->dev, "Failed to read boot status "); return; } if (boot_status) wdat->wdd.bootstatus = WDIOF_CARDRESET; /* Clear the boot status in case BIOS did not do it */ |
cda3b9178 watchdog: wdat_wd... |
243 |
ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_STATUS, 0, NULL); |
058dfc767 ACPI / watchdog: ... |
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 |
if (ret && ret != -EOPNOTSUPP) dev_err(&wdat->pdev->dev, "Failed to clear boot status "); } static void wdat_wdt_set_running(struct wdat_wdt *wdat) { u32 running = 0; int ret; ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_RUNNING_STATE, 0, &running); if (ret && ret != -EOPNOTSUPP) dev_err(&wdat->pdev->dev, "Failed to read running state "); if (running) set_bit(WDOG_HW_RUNNING, &wdat->wdd.status); } static int wdat_wdt_start(struct watchdog_device *wdd) { return wdat_wdt_run_action(to_wdat_wdt(wdd), ACPI_WDAT_SET_RUNNING_STATE, 0, NULL); } static int wdat_wdt_stop(struct watchdog_device *wdd) { return wdat_wdt_run_action(to_wdat_wdt(wdd), ACPI_WDAT_SET_STOPPED_STATE, 0, NULL); } static int wdat_wdt_ping(struct watchdog_device *wdd) { return wdat_wdt_run_action(to_wdat_wdt(wdd), ACPI_WDAT_RESET, 0, NULL); } static int wdat_wdt_set_timeout(struct watchdog_device *wdd, unsigned int timeout) { struct wdat_wdt *wdat = to_wdat_wdt(wdd); unsigned int periods; int ret; periods = timeout * 1000 / wdat->period; ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_COUNTDOWN, periods, NULL); if (!ret) wdd->timeout = timeout; return ret; } static unsigned int wdat_wdt_get_timeleft(struct watchdog_device *wdd) { struct wdat_wdt *wdat = to_wdat_wdt(wdd); u32 periods = 0; |
0a48f239b watchdog: wdat_wd... |
299 |
wdat_wdt_run_action(wdat, ACPI_WDAT_GET_CURRENT_COUNTDOWN, 0, &periods); |
058dfc767 ACPI / watchdog: ... |
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
return periods * wdat->period / 1000; } static const struct watchdog_info wdat_wdt_info = { .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, .firmware_version = 0, .identity = "wdat_wdt", }; static const struct watchdog_ops wdat_wdt_ops = { .owner = THIS_MODULE, .start = wdat_wdt_start, .stop = wdat_wdt_stop, .ping = wdat_wdt_ping, .set_timeout = wdat_wdt_set_timeout, .get_timeleft = wdat_wdt_get_timeleft, }; static int wdat_wdt_probe(struct platform_device *pdev) { |
edaa35b55 watchdog: wdat_wd... |
320 |
struct device *dev = &pdev->dev; |
058dfc767 ACPI / watchdog: ... |
321 322 323 324 325 326 327 328 329 330 331 332 |
const struct acpi_wdat_entry *entries; const struct acpi_table_wdat *tbl; struct wdat_wdt *wdat; struct resource *res; void __iomem **regs; acpi_status status; int i, ret; status = acpi_get_table(ACPI_SIG_WDAT, 0, (struct acpi_table_header **)&tbl); if (ACPI_FAILURE(status)) return -ENODEV; |
edaa35b55 watchdog: wdat_wd... |
333 |
wdat = devm_kzalloc(dev, sizeof(*wdat), GFP_KERNEL); |
058dfc767 ACPI / watchdog: ... |
334 335 |
if (!wdat) return -ENOMEM; |
edaa35b55 watchdog: wdat_wd... |
336 |
regs = devm_kcalloc(dev, pdev->num_resources, sizeof(*regs), |
058dfc767 ACPI / watchdog: ... |
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
GFP_KERNEL); if (!regs) return -ENOMEM; /* WDAT specification wants to have >= 1ms period */ if (tbl->timer_period < 1) return -EINVAL; if (tbl->min_count > tbl->max_count) return -EINVAL; wdat->period = tbl->timer_period; wdat->wdd.min_hw_heartbeat_ms = wdat->period * tbl->min_count; wdat->wdd.max_hw_heartbeat_ms = wdat->period * tbl->max_count; wdat->stopped_in_sleep = tbl->flags & ACPI_WDAT_STOPPED; wdat->wdd.info = &wdat_wdt_info; wdat->wdd.ops = &wdat_wdt_ops; wdat->pdev = pdev; /* Request and map all resources */ for (i = 0; i < pdev->num_resources; i++) { void __iomem *reg; res = &pdev->resource[i]; if (resource_type(res) == IORESOURCE_MEM) { |
edaa35b55 watchdog: wdat_wd... |
361 |
reg = devm_ioremap_resource(dev, res); |
356ed0435 watchdog: wdat_wd... |
362 363 |
if (IS_ERR(reg)) return PTR_ERR(reg); |
058dfc767 ACPI / watchdog: ... |
364 |
} else if (resource_type(res) == IORESOURCE_IO) { |
edaa35b55 watchdog: wdat_wd... |
365 |
reg = devm_ioport_map(dev, res->start, 1); |
356ed0435 watchdog: wdat_wd... |
366 367 |
if (!reg) return -ENOMEM; |
058dfc767 ACPI / watchdog: ... |
368 |
} else { |
edaa35b55 watchdog: wdat_wd... |
369 370 |
dev_err(dev, "Unsupported resource "); |
058dfc767 ACPI / watchdog: ... |
371 372 |
return -EINVAL; } |
058dfc767 ACPI / watchdog: ... |
373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
regs[i] = reg; } entries = (struct acpi_wdat_entry *)(tbl + 1); for (i = 0; i < tbl->entries; i++) { const struct acpi_generic_address *gas; struct wdat_instruction *instr; struct list_head *instructions; unsigned int action; struct resource r; int j; action = entries[i].action; if (action >= MAX_WDAT_ACTIONS) { |
edaa35b55 watchdog: wdat_wd... |
387 388 |
dev_dbg(dev, "Skipping unknown action: %u ", action); |
058dfc767 ACPI / watchdog: ... |
389 390 |
continue; } |
edaa35b55 watchdog: wdat_wd... |
391 |
instr = devm_kzalloc(dev, sizeof(*instr), GFP_KERNEL); |
058dfc767 ACPI / watchdog: ... |
392 393 394 395 396 397 398 399 400 401 |
if (!instr) return -ENOMEM; INIT_LIST_HEAD(&instr->node); instr->entry = entries[i]; gas = &entries[i].register_region; memset(&r, 0, sizeof(r)); r.start = gas->address; |
07fec9a84 ACPI: watchdog: F... |
402 |
r.end = r.start + ACPI_ACCESS_BYTE_WIDTH(gas->access_width) - 1; |
058dfc767 ACPI / watchdog: ... |
403 404 405 406 407 |
if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) { r.flags = IORESOURCE_MEM; } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) { r.flags = IORESOURCE_IO; } else { |
edaa35b55 watchdog: wdat_wd... |
408 409 |
dev_dbg(dev, "Unsupported address space: %d ", |
058dfc767 ACPI / watchdog: ... |
410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
gas->space_id); continue; } /* Find the matching resource */ for (j = 0; j < pdev->num_resources; j++) { res = &pdev->resource[j]; if (resource_contains(res, &r)) { instr->reg = regs[j] + r.start - res->start; break; } } if (!instr->reg) { |
edaa35b55 watchdog: wdat_wd... |
424 425 |
dev_err(dev, "I/O resource not found "); |
058dfc767 ACPI / watchdog: ... |
426 427 428 429 430 |
return -EINVAL; } instructions = wdat->instructions[action]; if (!instructions) { |
edaa35b55 watchdog: wdat_wd... |
431 432 433 |
instructions = devm_kzalloc(dev, sizeof(*instructions), GFP_KERNEL); |
058dfc767 ACPI / watchdog: ... |
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
if (!instructions) return -ENOMEM; INIT_LIST_HEAD(instructions); wdat->instructions[action] = instructions; } list_add_tail(&instr->node, instructions); } wdat_wdt_boot_status(wdat); wdat_wdt_set_running(wdat); ret = wdat_wdt_enable_reboot(wdat); if (ret) return ret; platform_set_drvdata(pdev, wdat); |
87bd74b77 ACPI: watchdog: S... |
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
/* * Set initial timeout so that userspace has time to configure the * watchdog properly after it has opened the device. In some cases * the BIOS default is too short and causes immediate reboot. */ if (timeout * 1000 < wdat->wdd.min_hw_heartbeat_ms || timeout * 1000 > wdat->wdd.max_hw_heartbeat_ms) { dev_warn(dev, "Invalid timeout %d given, using %d ", timeout, WDAT_DEFAULT_TIMEOUT); timeout = WDAT_DEFAULT_TIMEOUT; } ret = wdat_wdt_set_timeout(&wdat->wdd, timeout); if (ret) return ret; |
058dfc767 ACPI / watchdog: ... |
468 |
watchdog_set_nowayout(&wdat->wdd, nowayout); |
edaa35b55 watchdog: wdat_wd... |
469 |
return devm_watchdog_register_device(dev, &wdat->wdd); |
058dfc767 ACPI / watchdog: ... |
470 471 472 473 474 |
} #ifdef CONFIG_PM_SLEEP static int wdat_wdt_suspend_noirq(struct device *dev) { |
20745634c watchdog: simplif... |
475 |
struct wdat_wdt *wdat = dev_get_drvdata(dev); |
058dfc767 ACPI / watchdog: ... |
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 |
int ret; if (!watchdog_active(&wdat->wdd)) return 0; /* * We need to stop the watchdog if firmare is not doing it or if we * are going suspend to idle (where firmware is not involved). If * firmware is stopping the watchdog we kick it here one more time * to give it some time. */ wdat->stopped = false; if (acpi_target_system_state() == ACPI_STATE_S0 || !wdat->stopped_in_sleep) { ret = wdat_wdt_stop(&wdat->wdd); if (!ret) wdat->stopped = true; } else { ret = wdat_wdt_ping(&wdat->wdd); } return ret; } static int wdat_wdt_resume_noirq(struct device *dev) { |
20745634c watchdog: simplif... |
502 |
struct wdat_wdt *wdat = dev_get_drvdata(dev); |
058dfc767 ACPI / watchdog: ... |
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
int ret; if (!watchdog_active(&wdat->wdd)) return 0; if (!wdat->stopped) { /* * Looks like the boot firmware reinitializes the watchdog * before it hands off to the OS on resume from sleep so we * stop and reprogram the watchdog here. */ ret = wdat_wdt_stop(&wdat->wdd); if (ret) return ret; ret = wdat_wdt_set_timeout(&wdat->wdd, wdat->wdd.timeout); if (ret) return ret; ret = wdat_wdt_enable_reboot(wdat); if (ret) return ret; |
28e3d7002 watchdog: wdat_wd... |
525 526 527 528 |
ret = wdat_wdt_ping(&wdat->wdd); if (ret) return ret; |
058dfc767 ACPI / watchdog: ... |
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 |
} return wdat_wdt_start(&wdat->wdd); } #endif static const struct dev_pm_ops wdat_wdt_pm_ops = { SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(wdat_wdt_suspend_noirq, wdat_wdt_resume_noirq) }; static struct platform_driver wdat_wdt_driver = { .probe = wdat_wdt_probe, .driver = { .name = "wdat_wdt", .pm = &wdat_wdt_pm_ops, }, }; module_platform_driver(wdat_wdt_driver); MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); MODULE_DESCRIPTION("ACPI Hardware Watchdog (WDAT) driver"); MODULE_LICENSE("GPL v2"); MODULE_ALIAS("platform:wdat_wdt"); |