wdat_wdt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ACPI Hardware Watchdog (WDAT) driver.
  4. *
  5. * Copyright (C) 2016, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/ioport.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm.h>
  13. #include <linux/watchdog.h>
  14. #define MAX_WDAT_ACTIONS ACPI_WDAT_ACTION_RESERVED
  15. /**
  16. * struct wdat_instruction - Single ACPI WDAT instruction
  17. * @entry: Copy of the ACPI table instruction
  18. * @reg: Register the instruction is accessing
  19. * @node: Next instruction in action sequence
  20. */
  21. struct wdat_instruction {
  22. struct acpi_wdat_entry entry;
  23. void __iomem *reg;
  24. struct list_head node;
  25. };
  26. /**
  27. * struct wdat_wdt - ACPI WDAT watchdog device
  28. * @pdev: Parent platform device
  29. * @wdd: Watchdog core device
  30. * @period: How long is one watchdog period in ms
  31. * @stopped_in_sleep: Is this watchdog stopped by the firmware in S1-S5
  32. * @stopped: Was the watchdog stopped by the driver in suspend
  33. * @actions: An array of instruction lists indexed by an action number from
  34. * the WDAT table. There can be %NULL entries for not implemented
  35. * actions.
  36. */
  37. struct wdat_wdt {
  38. struct platform_device *pdev;
  39. struct watchdog_device wdd;
  40. unsigned int period;
  41. bool stopped_in_sleep;
  42. bool stopped;
  43. struct list_head *instructions[MAX_WDAT_ACTIONS];
  44. };
  45. #define to_wdat_wdt(wdd) container_of(wdd, struct wdat_wdt, wdd)
  46. static bool nowayout = WATCHDOG_NOWAYOUT;
  47. module_param(nowayout, bool, 0);
  48. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  49. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  50. #define WDAT_DEFAULT_TIMEOUT 30
  51. static int timeout = WDAT_DEFAULT_TIMEOUT;
  52. module_param(timeout, int, 0);
  53. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
  54. __MODULE_STRING(WDAT_DEFAULT_TIMEOUT) ")");
  55. static int wdat_wdt_read(struct wdat_wdt *wdat,
  56. const struct wdat_instruction *instr, u32 *value)
  57. {
  58. const struct acpi_generic_address *gas = &instr->entry.register_region;
  59. switch (gas->access_width) {
  60. case 1:
  61. *value = ioread8(instr->reg);
  62. break;
  63. case 2:
  64. *value = ioread16(instr->reg);
  65. break;
  66. case 3:
  67. *value = ioread32(instr->reg);
  68. break;
  69. default:
  70. return -EINVAL;
  71. }
  72. dev_dbg(&wdat->pdev->dev, "Read %#x from 0x%08llx\n", *value,
  73. gas->address);
  74. return 0;
  75. }
  76. static int wdat_wdt_write(struct wdat_wdt *wdat,
  77. const struct wdat_instruction *instr, u32 value)
  78. {
  79. const struct acpi_generic_address *gas = &instr->entry.register_region;
  80. switch (gas->access_width) {
  81. case 1:
  82. iowrite8((u8)value, instr->reg);
  83. break;
  84. case 2:
  85. iowrite16((u16)value, instr->reg);
  86. break;
  87. case 3:
  88. iowrite32(value, instr->reg);
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. dev_dbg(&wdat->pdev->dev, "Wrote %#x to 0x%08llx\n", value,
  94. gas->address);
  95. return 0;
  96. }
  97. static int wdat_wdt_run_action(struct wdat_wdt *wdat, unsigned int action,
  98. u32 param, u32 *retval)
  99. {
  100. struct wdat_instruction *instr;
  101. if (action >= ARRAY_SIZE(wdat->instructions))
  102. return -EINVAL;
  103. if (!wdat->instructions[action])
  104. return -EOPNOTSUPP;
  105. dev_dbg(&wdat->pdev->dev, "Running action %#x\n", action);
  106. /* Run each instruction sequentially */
  107. list_for_each_entry(instr, wdat->instructions[action], node) {
  108. const struct acpi_wdat_entry *entry = &instr->entry;
  109. const struct acpi_generic_address *gas;
  110. u32 flags, value, mask, x, y;
  111. bool preserve;
  112. int ret;
  113. gas = &entry->register_region;
  114. preserve = entry->instruction & ACPI_WDAT_PRESERVE_REGISTER;
  115. flags = entry->instruction & ~ACPI_WDAT_PRESERVE_REGISTER;
  116. value = entry->value;
  117. mask = entry->mask;
  118. switch (flags) {
  119. case ACPI_WDAT_READ_VALUE:
  120. ret = wdat_wdt_read(wdat, instr, &x);
  121. if (ret)
  122. return ret;
  123. x >>= gas->bit_offset;
  124. x &= mask;
  125. if (retval)
  126. *retval = x == value;
  127. break;
  128. case ACPI_WDAT_READ_COUNTDOWN:
  129. ret = wdat_wdt_read(wdat, instr, &x);
  130. if (ret)
  131. return ret;
  132. x >>= gas->bit_offset;
  133. x &= mask;
  134. if (retval)
  135. *retval = x;
  136. break;
  137. case ACPI_WDAT_WRITE_VALUE:
  138. x = value & mask;
  139. x <<= gas->bit_offset;
  140. if (preserve) {
  141. ret = wdat_wdt_read(wdat, instr, &y);
  142. if (ret)
  143. return ret;
  144. y = y & ~(mask << gas->bit_offset);
  145. x |= y;
  146. }
  147. ret = wdat_wdt_write(wdat, instr, x);
  148. if (ret)
  149. return ret;
  150. break;
  151. case ACPI_WDAT_WRITE_COUNTDOWN:
  152. x = param;
  153. x &= mask;
  154. x <<= gas->bit_offset;
  155. if (preserve) {
  156. ret = wdat_wdt_read(wdat, instr, &y);
  157. if (ret)
  158. return ret;
  159. y = y & ~(mask << gas->bit_offset);
  160. x |= y;
  161. }
  162. ret = wdat_wdt_write(wdat, instr, x);
  163. if (ret)
  164. return ret;
  165. break;
  166. default:
  167. dev_err(&wdat->pdev->dev, "Unknown instruction: %u\n",
  168. flags);
  169. return -EINVAL;
  170. }
  171. }
  172. return 0;
  173. }
  174. static int wdat_wdt_enable_reboot(struct wdat_wdt *wdat)
  175. {
  176. int ret;
  177. /*
  178. * WDAT specification says that the watchdog is required to reboot
  179. * the system when it fires. However, it also states that it is
  180. * recommeded to make it configurable through hardware register. We
  181. * enable reboot now if it is configrable, just in case.
  182. */
  183. ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_REBOOT, 0, NULL);
  184. if (ret && ret != -EOPNOTSUPP) {
  185. dev_err(&wdat->pdev->dev,
  186. "Failed to enable reboot when watchdog triggers\n");
  187. return ret;
  188. }
  189. return 0;
  190. }
  191. static void wdat_wdt_boot_status(struct wdat_wdt *wdat)
  192. {
  193. u32 boot_status = 0;
  194. int ret;
  195. ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_STATUS, 0, &boot_status);
  196. if (ret && ret != -EOPNOTSUPP) {
  197. dev_err(&wdat->pdev->dev, "Failed to read boot status\n");
  198. return;
  199. }
  200. if (boot_status)
  201. wdat->wdd.bootstatus = WDIOF_CARDRESET;
  202. /* Clear the boot status in case BIOS did not do it */
  203. ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_STATUS, 0, NULL);
  204. if (ret && ret != -EOPNOTSUPP)
  205. dev_err(&wdat->pdev->dev, "Failed to clear boot status\n");
  206. }
  207. static void wdat_wdt_set_running(struct wdat_wdt *wdat)
  208. {
  209. u32 running = 0;
  210. int ret;
  211. ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_RUNNING_STATE, 0,
  212. &running);
  213. if (ret && ret != -EOPNOTSUPP)
  214. dev_err(&wdat->pdev->dev, "Failed to read running state\n");
  215. if (running)
  216. set_bit(WDOG_HW_RUNNING, &wdat->wdd.status);
  217. }
  218. static int wdat_wdt_start(struct watchdog_device *wdd)
  219. {
  220. return wdat_wdt_run_action(to_wdat_wdt(wdd),
  221. ACPI_WDAT_SET_RUNNING_STATE, 0, NULL);
  222. }
  223. static int wdat_wdt_stop(struct watchdog_device *wdd)
  224. {
  225. return wdat_wdt_run_action(to_wdat_wdt(wdd),
  226. ACPI_WDAT_SET_STOPPED_STATE, 0, NULL);
  227. }
  228. static int wdat_wdt_ping(struct watchdog_device *wdd)
  229. {
  230. return wdat_wdt_run_action(to_wdat_wdt(wdd), ACPI_WDAT_RESET, 0, NULL);
  231. }
  232. static int wdat_wdt_set_timeout(struct watchdog_device *wdd,
  233. unsigned int timeout)
  234. {
  235. struct wdat_wdt *wdat = to_wdat_wdt(wdd);
  236. unsigned int periods;
  237. int ret;
  238. periods = timeout * 1000 / wdat->period;
  239. ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_COUNTDOWN, periods, NULL);
  240. if (!ret)
  241. wdd->timeout = timeout;
  242. return ret;
  243. }
  244. static unsigned int wdat_wdt_get_timeleft(struct watchdog_device *wdd)
  245. {
  246. struct wdat_wdt *wdat = to_wdat_wdt(wdd);
  247. u32 periods = 0;
  248. wdat_wdt_run_action(wdat, ACPI_WDAT_GET_CURRENT_COUNTDOWN, 0, &periods);
  249. return periods * wdat->period / 1000;
  250. }
  251. static const struct watchdog_info wdat_wdt_info = {
  252. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  253. .firmware_version = 0,
  254. .identity = "wdat_wdt",
  255. };
  256. static const struct watchdog_ops wdat_wdt_ops = {
  257. .owner = THIS_MODULE,
  258. .start = wdat_wdt_start,
  259. .stop = wdat_wdt_stop,
  260. .ping = wdat_wdt_ping,
  261. .set_timeout = wdat_wdt_set_timeout,
  262. .get_timeleft = wdat_wdt_get_timeleft,
  263. };
  264. static int wdat_wdt_probe(struct platform_device *pdev)
  265. {
  266. struct device *dev = &pdev->dev;
  267. const struct acpi_wdat_entry *entries;
  268. const struct acpi_table_wdat *tbl;
  269. struct wdat_wdt *wdat;
  270. struct resource *res;
  271. void __iomem **regs;
  272. acpi_status status;
  273. int i, ret;
  274. status = acpi_get_table(ACPI_SIG_WDAT, 0,
  275. (struct acpi_table_header **)&tbl);
  276. if (ACPI_FAILURE(status))
  277. return -ENODEV;
  278. wdat = devm_kzalloc(dev, sizeof(*wdat), GFP_KERNEL);
  279. if (!wdat)
  280. return -ENOMEM;
  281. regs = devm_kcalloc(dev, pdev->num_resources, sizeof(*regs),
  282. GFP_KERNEL);
  283. if (!regs)
  284. return -ENOMEM;
  285. /* WDAT specification wants to have >= 1ms period */
  286. if (tbl->timer_period < 1)
  287. return -EINVAL;
  288. if (tbl->min_count > tbl->max_count)
  289. return -EINVAL;
  290. wdat->period = tbl->timer_period;
  291. wdat->wdd.min_hw_heartbeat_ms = wdat->period * tbl->min_count;
  292. wdat->wdd.max_hw_heartbeat_ms = wdat->period * tbl->max_count;
  293. wdat->stopped_in_sleep = tbl->flags & ACPI_WDAT_STOPPED;
  294. wdat->wdd.info = &wdat_wdt_info;
  295. wdat->wdd.ops = &wdat_wdt_ops;
  296. wdat->pdev = pdev;
  297. /* Request and map all resources */
  298. for (i = 0; i < pdev->num_resources; i++) {
  299. void __iomem *reg;
  300. res = &pdev->resource[i];
  301. if (resource_type(res) == IORESOURCE_MEM) {
  302. reg = devm_ioremap_resource(dev, res);
  303. if (IS_ERR(reg))
  304. return PTR_ERR(reg);
  305. } else if (resource_type(res) == IORESOURCE_IO) {
  306. reg = devm_ioport_map(dev, res->start, 1);
  307. if (!reg)
  308. return -ENOMEM;
  309. } else {
  310. dev_err(dev, "Unsupported resource\n");
  311. return -EINVAL;
  312. }
  313. regs[i] = reg;
  314. }
  315. entries = (struct acpi_wdat_entry *)(tbl + 1);
  316. for (i = 0; i < tbl->entries; i++) {
  317. const struct acpi_generic_address *gas;
  318. struct wdat_instruction *instr;
  319. struct list_head *instructions;
  320. unsigned int action;
  321. struct resource r;
  322. int j;
  323. action = entries[i].action;
  324. if (action >= MAX_WDAT_ACTIONS) {
  325. dev_dbg(dev, "Skipping unknown action: %u\n", action);
  326. continue;
  327. }
  328. instr = devm_kzalloc(dev, sizeof(*instr), GFP_KERNEL);
  329. if (!instr)
  330. return -ENOMEM;
  331. INIT_LIST_HEAD(&instr->node);
  332. instr->entry = entries[i];
  333. gas = &entries[i].register_region;
  334. memset(&r, 0, sizeof(r));
  335. r.start = gas->address;
  336. r.end = r.start + ACPI_ACCESS_BYTE_WIDTH(gas->access_width) - 1;
  337. if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  338. r.flags = IORESOURCE_MEM;
  339. } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  340. r.flags = IORESOURCE_IO;
  341. } else {
  342. dev_dbg(dev, "Unsupported address space: %d\n",
  343. gas->space_id);
  344. continue;
  345. }
  346. /* Find the matching resource */
  347. for (j = 0; j < pdev->num_resources; j++) {
  348. res = &pdev->resource[j];
  349. if (resource_contains(res, &r)) {
  350. instr->reg = regs[j] + r.start - res->start;
  351. break;
  352. }
  353. }
  354. if (!instr->reg) {
  355. dev_err(dev, "I/O resource not found\n");
  356. return -EINVAL;
  357. }
  358. instructions = wdat->instructions[action];
  359. if (!instructions) {
  360. instructions = devm_kzalloc(dev,
  361. sizeof(*instructions),
  362. GFP_KERNEL);
  363. if (!instructions)
  364. return -ENOMEM;
  365. INIT_LIST_HEAD(instructions);
  366. wdat->instructions[action] = instructions;
  367. }
  368. list_add_tail(&instr->node, instructions);
  369. }
  370. wdat_wdt_boot_status(wdat);
  371. wdat_wdt_set_running(wdat);
  372. ret = wdat_wdt_enable_reboot(wdat);
  373. if (ret)
  374. return ret;
  375. platform_set_drvdata(pdev, wdat);
  376. /*
  377. * Set initial timeout so that userspace has time to configure the
  378. * watchdog properly after it has opened the device. In some cases
  379. * the BIOS default is too short and causes immediate reboot.
  380. */
  381. if (timeout * 1000 < wdat->wdd.min_hw_heartbeat_ms ||
  382. timeout * 1000 > wdat->wdd.max_hw_heartbeat_ms) {
  383. dev_warn(dev, "Invalid timeout %d given, using %d\n",
  384. timeout, WDAT_DEFAULT_TIMEOUT);
  385. timeout = WDAT_DEFAULT_TIMEOUT;
  386. }
  387. ret = wdat_wdt_set_timeout(&wdat->wdd, timeout);
  388. if (ret)
  389. return ret;
  390. watchdog_set_nowayout(&wdat->wdd, nowayout);
  391. return devm_watchdog_register_device(dev, &wdat->wdd);
  392. }
  393. #ifdef CONFIG_PM_SLEEP
  394. static int wdat_wdt_suspend_noirq(struct device *dev)
  395. {
  396. struct wdat_wdt *wdat = dev_get_drvdata(dev);
  397. int ret;
  398. if (!watchdog_active(&wdat->wdd))
  399. return 0;
  400. /*
  401. * We need to stop the watchdog if firmare is not doing it or if we
  402. * are going suspend to idle (where firmware is not involved). If
  403. * firmware is stopping the watchdog we kick it here one more time
  404. * to give it some time.
  405. */
  406. wdat->stopped = false;
  407. if (acpi_target_system_state() == ACPI_STATE_S0 ||
  408. !wdat->stopped_in_sleep) {
  409. ret = wdat_wdt_stop(&wdat->wdd);
  410. if (!ret)
  411. wdat->stopped = true;
  412. } else {
  413. ret = wdat_wdt_ping(&wdat->wdd);
  414. }
  415. return ret;
  416. }
  417. static int wdat_wdt_resume_noirq(struct device *dev)
  418. {
  419. struct wdat_wdt *wdat = dev_get_drvdata(dev);
  420. int ret;
  421. if (!watchdog_active(&wdat->wdd))
  422. return 0;
  423. if (!wdat->stopped) {
  424. /*
  425. * Looks like the boot firmware reinitializes the watchdog
  426. * before it hands off to the OS on resume from sleep so we
  427. * stop and reprogram the watchdog here.
  428. */
  429. ret = wdat_wdt_stop(&wdat->wdd);
  430. if (ret)
  431. return ret;
  432. ret = wdat_wdt_set_timeout(&wdat->wdd, wdat->wdd.timeout);
  433. if (ret)
  434. return ret;
  435. ret = wdat_wdt_enable_reboot(wdat);
  436. if (ret)
  437. return ret;
  438. ret = wdat_wdt_ping(&wdat->wdd);
  439. if (ret)
  440. return ret;
  441. }
  442. return wdat_wdt_start(&wdat->wdd);
  443. }
  444. #endif
  445. static const struct dev_pm_ops wdat_wdt_pm_ops = {
  446. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(wdat_wdt_suspend_noirq,
  447. wdat_wdt_resume_noirq)
  448. };
  449. static struct platform_driver wdat_wdt_driver = {
  450. .probe = wdat_wdt_probe,
  451. .driver = {
  452. .name = "wdat_wdt",
  453. .pm = &wdat_wdt_pm_ops,
  454. },
  455. };
  456. module_platform_driver(wdat_wdt_driver);
  457. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  458. MODULE_DESCRIPTION("ACPI Hardware Watchdog (WDAT) driver");
  459. MODULE_LICENSE("GPL v2");
  460. MODULE_ALIAS("platform:wdat_wdt");