首頁 > 軟體

Linux的WDT(watchdog)驅動

2020-06-16 17:57:09

第一部分: WDT驅動原理
WDT在核心中通常都實現為misc驅動。
WDT介紹
一個Watchdog Timer(WDT)是一個在軟體出錯的時候可以復位計算機系統的硬體電路。
通常一個使用者空間守護行程會在正常的時間間隔內通過/dev/watchdog特殊裝置檔案來通知核心的watchdog驅動,使用者空間仍然正常。當這樣的一個通知發生時,驅動通常會告訴硬體watchdog一切正常,然後watchdog應該再等待一段時間來復位系統。如果使用者空間出問題(RAM錯誤,核心bug等),則通知將會停止,然後硬體watchdog將在超時後復位系統。
Linux的watchdog API是一個相當特別的東西,不同的驅動實現是不同的,而且有時部分是不相容的。這個文件正是要嘗試著去說明已經出現的用法,並且使以後的驅動作者把它作為一份參考。
最簡單的 API:
所有的裝置驅動都支援的基本的操作模式,一旦/dev/watchdog被開啟,則watchdog啟用,並且除非餵狗,否則將在一段時間之後重新啟動,這個時間被稱為timeout或margin。最簡單的餵狗方法就是寫一些資料到裝置。一個非常簡單的watchdog守護行程看起來就像這個檔案這樣:
Documentation/watchdog/src/watchdog-simple.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(void)
{
    int fd = open("/dev/watchdog", O_WRONLY);
    int ret = 0;
    if (fd == -1) {
        perror("watchdog");
        exit(EXIT_FAILURE);
    }
    while (1) {
        ret = write(fd, "", 1);
        if (ret != 1) {
            ret = -1;
            break;
        }
        ret = fsync(fd);
        if (ret)
            break;
        sleep(10);
    }
    close(fd);
    return ret;
}

一個高階一些的驅動在餵狗之前,可能還會做一些其他的事情,比如說檢查HTTP伺服器是否依然可以相應。
當裝置關閉的時候,除非支援"Magic Close"特性。否則watchdog被關閉。這並不總是一個好主意,比如watchdog守護行程出現了bug並且崩潰了,則系統將不會重新啟動。因此,某些驅動支援"Disable watchdog shutdown on close", CONFIG_WATCHDOG_NOWAYOUT設定選項。當編譯核心的時候這個選項被設定為Y,則一旦watchdog被啟動,則將沒有辦法能夠停止。這樣,則當watchdog守護行程崩潰的時候,系統仍將在超時後重新啟動。Watchdog裝置常常也支援nowayout模組引數,這樣這個選項就可以在執行時進行控制。
Magic Close 特性:
如果一個驅動支援"Magic Close",則除非在關閉檔案前,魔幻字元'V'被傳送到/dev/watchdog,驅動將不停止watchdog。如果使用者空間守護行程在關閉檔案前沒有傳送這個字元,則驅動認為使用者空間崩潰,並在關閉watchdog前停止餵狗。
這樣的話,如果沒有在一定的時間內重新開啟watchdog,則將導致一個重新啟動。
ioctl API:
所有標準的驅動也應該支援一個ioctl API。
餵狗使用一個ioctl:
所有的驅動都有一個ioctl介面支援至少一個ioctl命令,KEEPALIVE。這個 ioctl 做的事和一個寫watchdog裝置完全一樣,所以,上面程式的主迴圈可以替換為:
while (1) {

      ioctl(fd, WDIOC_KEEPALIVE, 0);

      sleep(10);

    }

ioctl的引數被忽略。
設定和獲得超時值:
對於某些驅動來說,在上層使用SETTIMEOUT ioctl命令改變watchdog的超時值是可能的,那些驅動在他們的選項與中有WDIOF_SETTIMEOUT標誌。引數是一個代表以秒為單位的超時值,驅動將在同一個變數中返回實際使用的超時值,這個超時值可能由於硬體的限制,而不同於所請求的超時值
    int timeout = 45;
    ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
    printf("The timeout was set to %d secondsn", timeout);
如果裝置的超時值的粒度只能到分鐘,則這個例子可能實際列印"The timeout was set to 60 seconds"。
自從Linux 2.4.18核心,通過GETTIMEOUT ioctl命令查詢當前超時值也是可能的:
    ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
    printf("The timeout was is %d secondsn", timeout);
預處理:
Pretimeouts:
一些watchdog定時器,可以被設定為,在他們實際復位系統前,有一個觸發。這可能通過一個NMI,中斷,或其他機制。這將允許在它復位系統前Linux去記錄一些有用的資訊(比如panic資訊和核心轉儲)。
    pretimeout = 10;
    ioctl(fd, WDIOC_SETPRETIMEOUT, &pretimeout);
注意,預超時值應該是一個相對於超時值提前的秒數。而不是直到預超時的秒數。
比如,如果你設定超時值為60秒,預超時值為10秒,那麼預超時將在50秒後到達。設定為0則是禁用它。預超時還有一個get功能:
    ioctl(fd, WDIOC_GETPRETIMEOUT, &timeout);
    printf("The pretimeout was is %d secondsn", timeout);
不是所有的watchdog驅動都支援一個預超時的。
獲得重新啟動前的秒數
一些watchdog驅動有一個報告在重新啟動前的剩餘時間的功能。WDIOC_GETTIMELEFT就是返回重新啟動前的秒數的ioctl命令。
    ioctl(fd, WDIOC_GETTIMELEFT, &timeleft);
    printf("The timeout was is %d secondsn", timeleft);
環境監視:
Environmental monitoring:
所有的watchdog驅動都被要求返回更多關於系統的資訊,有些返回溫度,風扇和功率水平監測,依稀可以告訴你上一次重新啟動系統的原因。GETSUPPORT ioctl可以用來查詢裝置可以做什麼:
    struct watchdog_info ident;
    ioctl(fd, WDIOC_GETSUPPORT, &ident);
ident結構中返回的欄位是:
        identity    一個標識watchdog驅動的字串
    firmware_version 如果可用的話,就是卡的韌體版本
    options          一個描述裝置支援什麼的標誌
options欄位可以有下面的位集,和描述GET_STATUS 和 GET_BOOT_STATUS ioctls可以返回什麼種類的資訊。
第二部分: WDT驅動原始碼
驅動架構比較簡單,由於kernel啟動時,定義並加入了watchdog的platform_device,所以驅動定義並註冊watchdog 的platform_driver
/* linux/drivers/char/watchdog/s3c2410_wdt.c
 *
 * Copyright (c) 2004 Simtec Electronics
 * Ben Dooks <ben@simtec.co.uk>
 *
 * S3C2410 Watchdog Timer Support
 *
 * Based on, softdog.c by Alan Cox,
 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/timer.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <linux/uaccess.h>
#include <linux/io.h>

#include <mach/map.h>

#undef S3C_VA_WATCHDOG
#define S3C_VA_WATCHDOG (0)

#include <plat/regs-watchdog.h>

#define PFX "s3c2410-wdt: "

#define CONFIG_S3C2410_WATCHDOG_ATBOOT (0)
#define CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME (15)

static int nowayout = WATCHDOG_NOWAYOUT;
static int tmr_margin = CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME;
static int tmr_atboot = CONFIG_S3C2410_WATCHDOG_ATBOOT;
static int soft_noboot;
static int debug;

module_param(tmr_margin, int, 0);
module_param(tmr_atboot, int, 0);
module_param(nowayout, int, 0);
module_param(soft_noboot, int, 0);
module_param(debug, int, 0);

MODULE_PARM_DESC(tmr_margin, "Watchdog tmr_margin in seconds. default="
        __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME) ")");
MODULE_PARM_DESC(tmr_atboot,
        "Watchdog is started at boot time if set to 1, default="
            __MODULE_STRING(CONFIG_S3C2410_WATCHDOG_ATBOOT));
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
            __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
MODULE_PARM_DESC(soft_noboot, "Watchdog action, set to 1 to ignore reboots, "
            "0 to reboot (default depends on ONLY_TESTING)");
MODULE_PARM_DESC(debug, "Watchdog debug, set to >1 for debug, (default 0)");

static unsigned long open_lock;
static struct device *wdt_dev; /* platform device attached to */
static struct resource *wdt_mem;
static struct resource *wdt_irq;
static struct clk *wdt_clock;
static void __iomem *wdt_base;
static unsigned int wdt_count;
static char expect_close;
static DEFINE_SPINLOCK(wdt_lock);

/* watchdog control routines */

#define DBG(msg...) do {
    if (debug)
        printk(KERN_INFO msg);
    } while (0)
/* functions */

static void s3c2410wdt_keepalive(void)
{
    spin_lock(&wdt_lock);
    writel(wdt_count, wdt_base + S3C2410_WTCNT);
    spin_unlock(&wdt_lock);
}

static void __s3c2410wdt_stop(void)
{
    unsigned long wtcon;

    wtcon = readl(wdt_base + S3C2410_WTCON);
    wtcon &= ~(S3C2410_WTCON_ENABLE | S3C2410_WTCON_RSTEN);
    writel(wtcon, wdt_base + S3C2410_WTCON);
}

static void s3c2410wdt_stop(void)
{
    spin_lock(&wdt_lock);
    __s3c2410wdt_stop();
    spin_unlock(&wdt_lock);
}

static void s3c2410wdt_start(void)
{
    unsigned long wtcon;

    spin_lock(&wdt_lock);

    __s3c2410wdt_stop();

    wtcon = readl(wdt_base + S3C2410_WTCON);
    wtcon |= S3C2410_WTCON_ENABLE | S3C2410_WTCON_DIV128;

    if (soft_noboot) {
        wtcon |= S3C2410_WTCON_INTEN;
        wtcon &= ~S3C2410_WTCON_RSTEN;
    } else {
        wtcon &= ~S3C2410_WTCON_INTEN;
        wtcon |= S3C2410_WTCON_RSTEN;
    }

    DBG("%s: wdt_count=0x%08x, wtcon=%08lxn",
        __func__, wdt_count, wtcon);

    writel(wdt_count, wdt_base + S3C2410_WTDAT);
    writel(wdt_count, wdt_base + S3C2410_WTCNT);
    writel(wtcon, wdt_base + S3C2410_WTCON);
    spin_unlock(&wdt_lock);
}

static int s3c2410wdt_set_heartbeat(int timeout)
{
    unsigned int freq = clk_get_rate(wdt_clock);
    unsigned int count;
    unsigned int divisor = 1;
    unsigned long wtcon;
    if (timeout < 1)
        return -EINVAL;

    freq /= 128;
    count = timeout * freq;

    DBG("%s: count=%d, timeout=%d, freq=%dn",
        __func__, count, timeout, freq);

    /* if the count is bigger than the watchdog register,
      then work out what we need to do (and if) we can
      actually make this value
    */

    if (count >= 0x10000) {
        for (divisor = 1; divisor <= 0x100; divisor++) {
            if ((count / divisor) < 0x10000)
                break;
        }

        if ((count / divisor) >= 0x10000) {
            dev_err(wdt_dev, "timeout %d too bign", timeout);
            return -EINVAL;
        }
    }

    tmr_margin = timeout;

    DBG("%s: timeout=%d, divisor=%d, count=%d (%08x)n",
        __func__, timeout, divisor, count, count/divisor);

    count /= divisor;
    wdt_count = count;

    /* update the pre-scaler */
    wtcon = readl(wdt_base + S3C2410_WTCON);
    wtcon &= ~S3C2410_WTCON_PRESCALE_MASK;
    wtcon |= S3C2410_WTCON_PRESCALE(divisor-1);

    writel(count, wdt_base + S3C2410_WTDAT);
    writel(wtcon, wdt_base + S3C2410_WTCON);

    return 0;
}

/*
 * /dev/watchdog handling
 */

static int s3c2410wdt_open(struct inode *inode, struct file *file)
{
    if (test_and_set_bit(0, &open_lock))
        return -EBUSY;

    if (nowayout)
        __module_get(THIS_MODULE);

    expect_close = 0;

    /* start the timer */
    s3c2410wdt_start();
    return nonseekable_open(inode, file);
}

static int s3c2410wdt_release(struct inode *inode, struct file *file)
{
    /*
    * Shut off the timer.
    * Lock it in if it's a module and we set nowayout
    */

    if (expect_close == 42)
        s3c2410wdt_stop();
    else {
        dev_err(wdt_dev, "Unexpected close, not stopping watchdogn");
        s3c2410wdt_keepalive();
    }
    expect_close = 0;
    clear_bit(0, &open_lock);
    return 0;
}

static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
                size_t len, loff_t *ppos)
{
    /*
    * Refresh the timer.
    */
    if (len) {
        if (!nowayout) {
            size_t i;

            /* In case it was set long ago */
            expect_close = 0;

            for (i = 0; i != len; i++) {
                char c;

                if (get_user(c, data + i))
                    return -EFAULT;
                if (c == 'V')
                    expect_close = 42;
            }
        }
        s3c2410wdt_keepalive();
    }
    return len;
}

#define OPTIONS (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)

static const struct watchdog_info s3c2410_wdt_ident = {
    .options = OPTIONS,
    .firmware_version = 0,
    .identity = "S3C2410 Watchdog",
};


static long s3c2410wdt_ioctl(struct file *file, unsigned int cmd,
                            unsigned long arg)
{
    void __user *argp = (void __user *)arg;
    int __user *p = argp;
    int new_margin;

    switch (cmd) {
    case WDIOC_GETSUPPORT:
        return copy_to_user(argp, &s3c2410_wdt_ident,
            sizeof(s3c2410_wdt_ident)) ? -EFAULT : 0;
    case WDIOC_GETSTATUS:
    case WDIOC_GETBOOTSTATUS:
        return put_user(0, p);
    case WDIOC_KEEPALIVE:
        s3c2410wdt_keepalive();
        return 0;
    case WDIOC_SETTIMEOUT:
        if (get_user(new_margin, p))
            return -EFAULT;
        if (s3c2410wdt_set_heartbeat(new_margin))
            return -EINVAL;
        s3c2410wdt_keepalive();
        return put_user(tmr_margin, p);
    case WDIOC_GETTIMEOUT:
        return put_user(tmr_margin, p);
    default:
        return -ENOTTY;
    }
}

/* kernel interface */

static const struct file_operations s3c2410wdt_fops = {
    .owner = THIS_MODULE,
    .llseek = no_llseek,
    .write = s3c2410wdt_write,
    .unlocked_ioctl = s3c2410wdt_ioctl,
    .open = s3c2410wdt_open,
    .release = s3c2410wdt_release,
};

static struct miscdevice s3c2410wdt_miscdev = {
    .minor = WATCHDOG_MINOR,
    .name = "watchdog",
    .fops = &s3c2410wdt_fops,
};

/* interrupt handler code */

static irqreturn_t s3c2410wdt_irq(int irqno, void *param)
{
    dev_info(wdt_dev, "watchdog timer expired (irq)n");

    s3c2410wdt_keepalive();
    return IRQ_HANDLED;
}
/* device interface */

static int __devinit s3c2410wdt_probe(struct platform_device *pdev)
{
    struct resource *res;
    struct device *dev;
    unsigned int wtcon;
    int started = 0;
    int ret;
    int size;

    DBG("%s: probe=%pn", __func__, pdev);

    dev = &pdev->dev;
    wdt_dev = &pdev->dev;

    /* get the memory region for the watchdog timer -- flags is IORESOURCE_MEM */
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (res == NULL) {
        dev_err(dev, "no memory resource specifiedn");
        return -ENOENT;
    }

    size = (res->end - res->start) + 1;

    //請求分配指定的I/O記憶體資源
    wdt_mem = request_mem_region(res->start, size, pdev->name);
    if (wdt_mem == NULL) {
        dev_err(dev, "failed to get memory regionn");
        ret = -ENOENT;
        goto err_req;
    }

    //將一個IO地址空間對映到核心的虛擬地址空間上去,便於存取
    wdt_base = ioremap(res->start, size);
    if (wdt_base == NULL) {
        dev_err(dev, "failed to ioremap() regionn");
        ret = -EINVAL;
        goto err_req;
    }

    DBG("probe: mapped wdt_base=%pn", wdt_base);

    /* get the memory region for the watchdog timer -- flags is IORESOURCE_IRQ */
    wdt_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
    if (wdt_irq == NULL) {
        dev_err(dev, "no irq resource specifiedn");
        ret = -ENOENT;
        goto err_map;
    }

    //註冊中斷服務函數s3c2410wdt_irq()
    ret = request_irq(wdt_irq->start, s3c2410wdt_irq, 0, pdev->name, pdev);
    if (ret != 0) {
        dev_err(dev, "failed to install irq (%d)n", ret);
        goto err_map;
    }

    //從平台時鐘佇列中獲取clk
    wdt_clock = clk_get(&pdev->dev, "watchdog");
    if (IS_ERR(wdt_clock)) {
        dev_err(dev, "failed to find watchdog clock sourcen");
        ret = PTR_ERR(wdt_clock);
        goto err_irq;
    }

    //inform the system when the clock source should be running
    clk_enable(wdt_clock);

    /* see if we can actually set the requested timer margin, and if
    * not, try the default value */

    if (s3c2410wdt_set_heartbeat(tmr_margin)) {
        started = s3c2410wdt_set_heartbeat(
                    CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);

        if (started == 0)
            dev_info(dev,
              "tmr_margin value out of range, default %d usedn",
                  CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
        else
            dev_info(dev, "default timer value is out of range, "
                            "cannot startn");
    }

    ret = misc_register(&s3c2410wdt_miscdev);
    if (ret) {
        dev_err(dev, "cannot register miscdev on minor=%d (%d)n",
            WATCHDOG_MINOR, ret);
        goto err_clk;
    }

    if (tmr_atboot && started == 0) {
        dev_info(dev, "starting watchdog timern");
        s3c2410wdt_start();
    } else if (!tmr_atboot) {
        /* if we're not enabling the watchdog, then ensure it is
        * disabled if it has been left running from the bootloader
        * or other source */

        s3c2410wdt_stop();
    }

    /* print out a statement of readiness */

    wtcon = readl(wdt_base + S3C2410_WTCON);

    dev_info(dev, "watchdog %sactive, reset %sabled, irq %sabledn",
        (wtcon & S3C2410_WTCON_ENABLE) ? "" : "in",
        (wtcon & S3C2410_WTCON_RSTEN) ? "" : "dis",
        (wtcon & S3C2410_WTCON_INTEN) ? "" : "en");

    return 0;

 err_clk:
    clk_disable(wdt_clock);
    clk_put(wdt_clock);

 err_irq:
    free_irq(wdt_irq->start, pdev);

 err_map:
    iounmap(wdt_base);

 err_req:
    release_resource(wdt_mem);
    kfree(wdt_mem);

    return ret;
}

static int __devexit s3c2410wdt_remove(struct platform_device *dev)
{
    release_resource(wdt_mem);
    kfree(wdt_mem);
    wdt_mem = NULL;

    free_irq(wdt_irq->start, dev);
    wdt_irq = NULL;

    clk_disable(wdt_clock);
    clk_put(wdt_clock);
    wdt_clock = NULL;

    iounmap(wdt_base);
    misc_deregister(&s3c2410wdt_miscdev);
    return 0;
}

static void s3c2410wdt_shutdown(struct platform_device *dev)
{
    s3c2410wdt_stop();
}

#ifdef CONFIG_PM

static unsigned long wtcon_save;
static unsigned long wtdat_save;

static int s3c2410wdt_suspend(struct platform_device *dev, pm_message_t state)
{
    /* Save watchdog state, and turn it off. */
    wtcon_save = readl(wdt_base + S3C2410_WTCON);
    wtdat_save = readl(wdt_base + S3C2410_WTDAT);

    /* Note that WTCNT doesn't need to be saved. */
    s3c2410wdt_stop();

    return 0;
}

static int s3c2410wdt_resume(struct platform_device *dev)
{
    /* Restore watchdog state. */

    writel(wtdat_save, wdt_base + S3C2410_WTDAT);
    writel(wtdat_save, wdt_base + S3C2410_WTCNT); /* Reset count */
    writel(wtcon_save, wdt_base + S3C2410_WTCON);

    printk(KERN_INFO PFX "watchdog %sabledn",
          (wtcon_save & S3C2410_WTCON_ENABLE) ? "en" : "dis");

    return 0;
}
#else
#define s3c2410wdt_suspend NULL
#define s3c2410wdt_resume NULL
#endif /* CONFIG_PM */


/*
 *platform_driver s3c2410wdt_driver 與 platform_device s3c_device_wdt 對應
 *s3c_device_wdt 在arch/arm/plat-s3c24xx/devs.c中定義
 *兩者的工作順序是先定義platform_device -> 註冊 platform_device->
 *在mini2440_machine_init()中完成
 *再定義 platform_driver-> 註冊 platform_driver
 */
static struct platform_driver s3c2410wdt_driver = {
    .probe = s3c2410wdt_probe, //裝置的檢測,所以需要先註冊裝置
    .remove = __devexit_p(s3c2410wdt_remove), //刪除該裝置
    .shutdown = s3c2410wdt_shutdown, //關閉該裝置
    .suspend = s3c2410wdt_suspend,
    .resume = s3c2410wdt_resume,
    .driver = { //裝置驅動
        .owner = THIS_MODULE,
        /*
        *對應 struct platform_device s3c_device_wdt = {
        *    .name        = "s3c2410-wdt",
        *      ...
        *    };
        */
        .name = "s3c2410-wdt",
    },
};


static char banner[] __initdata =
    KERN_INFO "S3C2410 Watchdog Timer, (c) 2004 Simtec Electronicsn";

static int __init watchdog_init(void) //模組初始化
{
    printk(banner); //列印資訊
    return platform_driver_register(&s3c2410wdt_driver); //註冊裝置的驅動程式
}

static void __exit watchdog_exit(void) //移除模組
{
    platform_driver_unregister(&s3c2410wdt_driver); //unregister a driver for platform-level devices
}

module_init(watchdog_init);
module_exit(watchdog_exit);

MODULE_AUTHOR("Ben Dooks , "
          "Dimitry Andric ");
MODULE_DESCRIPTION("S3C2410 Watchdog Device Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
MODULE_ALIAS("platform:s3c2410-wdt");

本文永久更新連結地址http://www.linuxidc.com/Linux/2015-07/120374.htm


IT145.com E-mail:sddin#qq.com