blob: 098c965f6c7866a5ccdf787fa5251913d01c90d4 [file] [log] [blame]
Vladimir Zapolskiyff841362016-10-07 15:39:54 +03001/*
2 * Copyright (C) 2015-2016 Mentor Graphics
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 */
10
11#include <linux/list.h>
12#include <linux/slab.h>
13#include <linux/spinlock.h>
14#include <linux/watchdog.h>
15
16#include "watchdog_pretimeout.h"
17
18/* Default watchdog pretimeout governor */
19static struct watchdog_governor *default_gov;
20
21/* The spinlock protects default_gov, wdd->gov and pretimeout_list */
22static DEFINE_SPINLOCK(pretimeout_lock);
23
24/* List of watchdog devices, which can generate a pretimeout event */
25static LIST_HEAD(pretimeout_list);
26
27struct watchdog_pretimeout {
28 struct watchdog_device *wdd;
29 struct list_head entry;
30};
31
32int watchdog_pretimeout_governor_get(struct watchdog_device *wdd, char *buf)
33{
34 int count = 0;
35
36 spin_lock_irq(&pretimeout_lock);
37 if (wdd->gov)
38 count = sprintf(buf, "%s\n", wdd->gov->name);
39 spin_unlock_irq(&pretimeout_lock);
40
41 return count;
42}
43
44void watchdog_notify_pretimeout(struct watchdog_device *wdd)
45{
46 unsigned long flags;
47
48 spin_lock_irqsave(&pretimeout_lock, flags);
49 if (!wdd->gov) {
50 spin_unlock_irqrestore(&pretimeout_lock, flags);
51 return;
52 }
53
54 wdd->gov->pretimeout(wdd);
55 spin_unlock_irqrestore(&pretimeout_lock, flags);
56}
57EXPORT_SYMBOL_GPL(watchdog_notify_pretimeout);
58
59int watchdog_register_governor(struct watchdog_governor *gov)
60{
61 struct watchdog_pretimeout *p;
62
Vladimir Zapolskiyda0d12f2016-10-07 15:39:56 +030063 if (!strncmp(gov->name, WATCHDOG_PRETIMEOUT_DEFAULT_GOV,
64 WATCHDOG_GOV_NAME_MAXLEN)) {
Vladimir Zapolskiyff841362016-10-07 15:39:54 +030065 spin_lock_irq(&pretimeout_lock);
66 default_gov = gov;
67
68 list_for_each_entry(p, &pretimeout_list, entry)
69 if (!p->wdd->gov)
70 p->wdd->gov = default_gov;
71 spin_unlock_irq(&pretimeout_lock);
72 }
73
74 return 0;
75}
76EXPORT_SYMBOL(watchdog_register_governor);
77
78void watchdog_unregister_governor(struct watchdog_governor *gov)
79{
80 struct watchdog_pretimeout *p;
81
82 spin_lock_irq(&pretimeout_lock);
Vladimir Zapolskiyff841362016-10-07 15:39:54 +030083 list_for_each_entry(p, &pretimeout_list, entry)
84 if (p->wdd->gov == gov)
85 p->wdd->gov = default_gov;
86 spin_unlock_irq(&pretimeout_lock);
87}
88EXPORT_SYMBOL(watchdog_unregister_governor);
89
90int watchdog_register_pretimeout(struct watchdog_device *wdd)
91{
92 struct watchdog_pretimeout *p;
93
94 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
95 return 0;
96
97 p = kzalloc(sizeof(*p), GFP_KERNEL);
98 if (!p)
99 return -ENOMEM;
100
101 spin_lock_irq(&pretimeout_lock);
102 list_add(&p->entry, &pretimeout_list);
103 p->wdd = wdd;
104 wdd->gov = default_gov;
105 spin_unlock_irq(&pretimeout_lock);
106
107 return 0;
108}
109
110void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
111{
112 struct watchdog_pretimeout *p, *t;
113
114 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
115 return;
116
117 spin_lock_irq(&pretimeout_lock);
118 wdd->gov = NULL;
119
120 list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
121 if (p->wdd == wdd) {
122 list_del(&p->entry);
123 break;
124 }
125 }
126 spin_unlock_irq(&pretimeout_lock);
127
128 kfree(p);
129}