wdat_wdt.c 13 KB

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