nv_tco.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * nv_tco 0.01: TCO timer driver for NV chipsets
  4. *
  5. * (c) Copyright 2005 Google Inc., All Rights Reserved.
  6. *
  7. * Based off i8xx_tco.c:
  8. * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights
  9. * Reserved.
  10. * http://www.kernelconcepts.de
  11. *
  12. * TCO timer driver for NV chipsets
  13. * based on softdog.c by Alan Cox <alan@redhat.com>
  14. */
  15. /*
  16. * Includes, defines, variables, module parameters, ...
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/types.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/watchdog.h>
  24. #include <linux/init.h>
  25. #include <linux/fs.h>
  26. #include <linux/pci.h>
  27. #include <linux/ioport.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/io.h>
  32. #include "nv_tco.h"
  33. /* Module and version information */
  34. #define TCO_VERSION "0.01"
  35. #define TCO_MODULE_NAME "NV_TCO"
  36. #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
  37. /* internal variables */
  38. static unsigned int tcobase;
  39. static DEFINE_SPINLOCK(tco_lock); /* Guards the hardware */
  40. static unsigned long timer_alive;
  41. static char tco_expect_close;
  42. static struct pci_dev *tco_pci;
  43. /* the watchdog platform device */
  44. static struct platform_device *nv_tco_platform_device;
  45. /* module parameters */
  46. #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat (2<heartbeat<39) */
  47. static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
  48. module_param(heartbeat, int, 0);
  49. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<heartbeat<39, "
  50. "default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  51. static bool nowayout = WATCHDOG_NOWAYOUT;
  52. module_param(nowayout, bool, 0);
  53. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
  54. " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  55. /*
  56. * Some TCO specific functions
  57. */
  58. static inline unsigned char seconds_to_ticks(int seconds)
  59. {
  60. /* the internal timer is stored as ticks which decrement
  61. * every 0.6 seconds */
  62. return (seconds * 10) / 6;
  63. }
  64. static void tco_timer_start(void)
  65. {
  66. u32 val;
  67. unsigned long flags;
  68. spin_lock_irqsave(&tco_lock, flags);
  69. val = inl(TCO_CNT(tcobase));
  70. val &= ~TCO_CNT_TCOHALT;
  71. outl(val, TCO_CNT(tcobase));
  72. spin_unlock_irqrestore(&tco_lock, flags);
  73. }
  74. static void tco_timer_stop(void)
  75. {
  76. u32 val;
  77. unsigned long flags;
  78. spin_lock_irqsave(&tco_lock, flags);
  79. val = inl(TCO_CNT(tcobase));
  80. val |= TCO_CNT_TCOHALT;
  81. outl(val, TCO_CNT(tcobase));
  82. spin_unlock_irqrestore(&tco_lock, flags);
  83. }
  84. static void tco_timer_keepalive(void)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&tco_lock, flags);
  88. outb(0x01, TCO_RLD(tcobase));
  89. spin_unlock_irqrestore(&tco_lock, flags);
  90. }
  91. static int tco_timer_set_heartbeat(int t)
  92. {
  93. int ret = 0;
  94. unsigned char tmrval;
  95. unsigned long flags;
  96. u8 val;
  97. /*
  98. * note seconds_to_ticks(t) > t, so if t > 0x3f, so is
  99. * tmrval=seconds_to_ticks(t). Check that the count in seconds isn't
  100. * out of range on it's own (to avoid overflow in tmrval).
  101. */
  102. if (t < 0 || t > 0x3f)
  103. return -EINVAL;
  104. tmrval = seconds_to_ticks(t);
  105. /* "Values of 0h-3h are ignored and should not be attempted" */
  106. if (tmrval > 0x3f || tmrval < 0x04)
  107. return -EINVAL;
  108. /* Write new heartbeat to watchdog */
  109. spin_lock_irqsave(&tco_lock, flags);
  110. val = inb(TCO_TMR(tcobase));
  111. val &= 0xc0;
  112. val |= tmrval;
  113. outb(val, TCO_TMR(tcobase));
  114. val = inb(TCO_TMR(tcobase));
  115. if ((val & 0x3f) != tmrval)
  116. ret = -EINVAL;
  117. spin_unlock_irqrestore(&tco_lock, flags);
  118. if (ret)
  119. return ret;
  120. heartbeat = t;
  121. return 0;
  122. }
  123. /*
  124. * /dev/watchdog handling
  125. */
  126. static int nv_tco_open(struct inode *inode, struct file *file)
  127. {
  128. /* /dev/watchdog can only be opened once */
  129. if (test_and_set_bit(0, &timer_alive))
  130. return -EBUSY;
  131. /* Reload and activate timer */
  132. tco_timer_keepalive();
  133. tco_timer_start();
  134. return stream_open(inode, file);
  135. }
  136. static int nv_tco_release(struct inode *inode, struct file *file)
  137. {
  138. /* Shut off the timer */
  139. if (tco_expect_close == 42) {
  140. tco_timer_stop();
  141. } else {
  142. pr_crit("Unexpected close, not stopping watchdog!\n");
  143. tco_timer_keepalive();
  144. }
  145. clear_bit(0, &timer_alive);
  146. tco_expect_close = 0;
  147. return 0;
  148. }
  149. static ssize_t nv_tco_write(struct file *file, const char __user *data,
  150. size_t len, loff_t *ppos)
  151. {
  152. /* See if we got the magic character 'V' and reload the timer */
  153. if (len) {
  154. if (!nowayout) {
  155. size_t i;
  156. /*
  157. * note: just in case someone wrote the magic character
  158. * five months ago...
  159. */
  160. tco_expect_close = 0;
  161. /*
  162. * scan to see whether or not we got the magic
  163. * character
  164. */
  165. for (i = 0; i != len; i++) {
  166. char c;
  167. if (get_user(c, data + i))
  168. return -EFAULT;
  169. if (c == 'V')
  170. tco_expect_close = 42;
  171. }
  172. }
  173. /* someone wrote to us, we should reload the timer */
  174. tco_timer_keepalive();
  175. }
  176. return len;
  177. }
  178. static long nv_tco_ioctl(struct file *file, unsigned int cmd,
  179. unsigned long arg)
  180. {
  181. int new_options, retval = -EINVAL;
  182. int new_heartbeat;
  183. void __user *argp = (void __user *)arg;
  184. int __user *p = argp;
  185. static const struct watchdog_info ident = {
  186. .options = WDIOF_SETTIMEOUT |
  187. WDIOF_KEEPALIVEPING |
  188. WDIOF_MAGICCLOSE,
  189. .firmware_version = 0,
  190. .identity = TCO_MODULE_NAME,
  191. };
  192. switch (cmd) {
  193. case WDIOC_GETSUPPORT:
  194. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  195. case WDIOC_GETSTATUS:
  196. case WDIOC_GETBOOTSTATUS:
  197. return put_user(0, p);
  198. case WDIOC_SETOPTIONS:
  199. if (get_user(new_options, p))
  200. return -EFAULT;
  201. if (new_options & WDIOS_DISABLECARD) {
  202. tco_timer_stop();
  203. retval = 0;
  204. }
  205. if (new_options & WDIOS_ENABLECARD) {
  206. tco_timer_keepalive();
  207. tco_timer_start();
  208. retval = 0;
  209. }
  210. return retval;
  211. case WDIOC_KEEPALIVE:
  212. tco_timer_keepalive();
  213. return 0;
  214. case WDIOC_SETTIMEOUT:
  215. if (get_user(new_heartbeat, p))
  216. return -EFAULT;
  217. if (tco_timer_set_heartbeat(new_heartbeat))
  218. return -EINVAL;
  219. tco_timer_keepalive();
  220. /* Fall through */
  221. case WDIOC_GETTIMEOUT:
  222. return put_user(heartbeat, p);
  223. default:
  224. return -ENOTTY;
  225. }
  226. }
  227. /*
  228. * Kernel Interfaces
  229. */
  230. static const struct file_operations nv_tco_fops = {
  231. .owner = THIS_MODULE,
  232. .llseek = no_llseek,
  233. .write = nv_tco_write,
  234. .unlocked_ioctl = nv_tco_ioctl,
  235. .open = nv_tco_open,
  236. .release = nv_tco_release,
  237. };
  238. static struct miscdevice nv_tco_miscdev = {
  239. .minor = WATCHDOG_MINOR,
  240. .name = "watchdog",
  241. .fops = &nv_tco_fops,
  242. };
  243. /*
  244. * Data for PCI driver interface
  245. *
  246. * This data only exists for exporting the supported
  247. * PCI ids via MODULE_DEVICE_TABLE. We do not actually
  248. * register a pci_driver, because someone else might one day
  249. * want to register another driver on the same PCI id.
  250. */
  251. static const struct pci_device_id tco_pci_tbl[] = {
  252. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SMBUS,
  253. PCI_ANY_ID, PCI_ANY_ID, },
  254. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SMBUS,
  255. PCI_ANY_ID, PCI_ANY_ID, },
  256. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP78S_SMBUS,
  257. PCI_ANY_ID, PCI_ANY_ID, },
  258. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP79_SMBUS,
  259. PCI_ANY_ID, PCI_ANY_ID, },
  260. { 0, }, /* End of list */
  261. };
  262. MODULE_DEVICE_TABLE(pci, tco_pci_tbl);
  263. /*
  264. * Init & exit routines
  265. */
  266. static unsigned char nv_tco_getdevice(void)
  267. {
  268. struct pci_dev *dev = NULL;
  269. u32 val;
  270. /* Find the PCI device */
  271. for_each_pci_dev(dev) {
  272. if (pci_match_id(tco_pci_tbl, dev) != NULL) {
  273. tco_pci = dev;
  274. break;
  275. }
  276. }
  277. if (!tco_pci)
  278. return 0;
  279. /* Find the base io port */
  280. pci_read_config_dword(tco_pci, 0x64, &val);
  281. val &= 0xffff;
  282. if (val == 0x0001 || val == 0x0000) {
  283. /* Something is wrong here, bar isn't setup */
  284. pr_err("failed to get tcobase address\n");
  285. return 0;
  286. }
  287. val &= 0xff00;
  288. tcobase = val + 0x40;
  289. if (!request_region(tcobase, 0x10, "NV TCO")) {
  290. pr_err("I/O address 0x%04x already in use\n", tcobase);
  291. return 0;
  292. }
  293. /* Set a reasonable heartbeat before we stop the timer */
  294. tco_timer_set_heartbeat(30);
  295. /*
  296. * Stop the TCO before we change anything so we don't race with
  297. * a zeroed timer.
  298. */
  299. tco_timer_keepalive();
  300. tco_timer_stop();
  301. /* Disable SMI caused by TCO */
  302. if (!request_region(MCP51_SMI_EN(tcobase), 4, "NV TCO")) {
  303. pr_err("I/O address 0x%04x already in use\n",
  304. MCP51_SMI_EN(tcobase));
  305. goto out;
  306. }
  307. val = inl(MCP51_SMI_EN(tcobase));
  308. val &= ~MCP51_SMI_EN_TCO;
  309. outl(val, MCP51_SMI_EN(tcobase));
  310. val = inl(MCP51_SMI_EN(tcobase));
  311. release_region(MCP51_SMI_EN(tcobase), 4);
  312. if (val & MCP51_SMI_EN_TCO) {
  313. pr_err("Could not disable SMI caused by TCO\n");
  314. goto out;
  315. }
  316. /* Check chipset's NO_REBOOT bit */
  317. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  318. val |= MCP51_SMBUS_SETUP_B_TCO_REBOOT;
  319. pci_write_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, val);
  320. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  321. if (!(val & MCP51_SMBUS_SETUP_B_TCO_REBOOT)) {
  322. pr_err("failed to reset NO_REBOOT flag, reboot disabled by hardware\n");
  323. goto out;
  324. }
  325. return 1;
  326. out:
  327. release_region(tcobase, 0x10);
  328. return 0;
  329. }
  330. static int nv_tco_init(struct platform_device *dev)
  331. {
  332. int ret;
  333. /* Check whether or not the hardware watchdog is there */
  334. if (!nv_tco_getdevice())
  335. return -ENODEV;
  336. /* Check to see if last reboot was due to watchdog timeout */
  337. pr_info("Watchdog reboot %sdetected\n",
  338. inl(TCO_STS(tcobase)) & TCO_STS_TCO2TO_STS ? "" : "not ");
  339. /* Clear out the old status */
  340. outl(TCO_STS_RESET, TCO_STS(tcobase));
  341. /*
  342. * Check that the heartbeat value is within it's range.
  343. * If not, reset to the default.
  344. */
  345. if (tco_timer_set_heartbeat(heartbeat)) {
  346. heartbeat = WATCHDOG_HEARTBEAT;
  347. tco_timer_set_heartbeat(heartbeat);
  348. pr_info("heartbeat value must be 2<heartbeat<39, using %d\n",
  349. heartbeat);
  350. }
  351. ret = misc_register(&nv_tco_miscdev);
  352. if (ret != 0) {
  353. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  354. WATCHDOG_MINOR, ret);
  355. goto unreg_region;
  356. }
  357. clear_bit(0, &timer_alive);
  358. tco_timer_stop();
  359. pr_info("initialized (0x%04x). heartbeat=%d sec (nowayout=%d)\n",
  360. tcobase, heartbeat, nowayout);
  361. return 0;
  362. unreg_region:
  363. release_region(tcobase, 0x10);
  364. return ret;
  365. }
  366. static void nv_tco_cleanup(void)
  367. {
  368. u32 val;
  369. /* Stop the timer before we leave */
  370. if (!nowayout)
  371. tco_timer_stop();
  372. /* Set the NO_REBOOT bit to prevent later reboots, just for sure */
  373. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  374. val &= ~MCP51_SMBUS_SETUP_B_TCO_REBOOT;
  375. pci_write_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, val);
  376. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  377. if (val & MCP51_SMBUS_SETUP_B_TCO_REBOOT) {
  378. pr_crit("Couldn't unset REBOOT bit. Machine may soon reset\n");
  379. }
  380. /* Deregister */
  381. misc_deregister(&nv_tco_miscdev);
  382. release_region(tcobase, 0x10);
  383. }
  384. static int nv_tco_remove(struct platform_device *dev)
  385. {
  386. if (tcobase)
  387. nv_tco_cleanup();
  388. return 0;
  389. }
  390. static void nv_tco_shutdown(struct platform_device *dev)
  391. {
  392. u32 val;
  393. tco_timer_stop();
  394. /* Some BIOSes fail the POST (once) if the NO_REBOOT flag is not
  395. * unset during shutdown. */
  396. pci_read_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, &val);
  397. val &= ~MCP51_SMBUS_SETUP_B_TCO_REBOOT;
  398. pci_write_config_dword(tco_pci, MCP51_SMBUS_SETUP_B, val);
  399. }
  400. static struct platform_driver nv_tco_driver = {
  401. .probe = nv_tco_init,
  402. .remove = nv_tco_remove,
  403. .shutdown = nv_tco_shutdown,
  404. .driver = {
  405. .name = TCO_MODULE_NAME,
  406. },
  407. };
  408. static int __init nv_tco_init_module(void)
  409. {
  410. int err;
  411. pr_info("NV TCO WatchDog Timer Driver v%s\n", TCO_VERSION);
  412. err = platform_driver_register(&nv_tco_driver);
  413. if (err)
  414. return err;
  415. nv_tco_platform_device = platform_device_register_simple(
  416. TCO_MODULE_NAME, -1, NULL, 0);
  417. if (IS_ERR(nv_tco_platform_device)) {
  418. err = PTR_ERR(nv_tco_platform_device);
  419. goto unreg_platform_driver;
  420. }
  421. return 0;
  422. unreg_platform_driver:
  423. platform_driver_unregister(&nv_tco_driver);
  424. return err;
  425. }
  426. static void __exit nv_tco_cleanup_module(void)
  427. {
  428. platform_device_unregister(nv_tco_platform_device);
  429. platform_driver_unregister(&nv_tco_driver);
  430. pr_info("NV TCO Watchdog Module Unloaded\n");
  431. }
  432. module_init(nv_tco_init_module);
  433. module_exit(nv_tco_cleanup_module);
  434. MODULE_AUTHOR("Mike Waychison");
  435. MODULE_DESCRIPTION("TCO timer driver for NV chipsets");
  436. MODULE_LICENSE("GPL");