blob: 72612255fb552b70c9398d7fb7cb26dfbc3f73d8 [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
63 if (!default_gov) {
64 spin_lock_irq(&pretimeout_lock);
65 default_gov = gov;
66
67 list_for_each_entry(p, &pretimeout_list, entry)
68 if (!p->wdd->gov)
69 p->wdd->gov = default_gov;
70 spin_unlock_irq(&pretimeout_lock);
71 }
72
73 return 0;
74}
75EXPORT_SYMBOL(watchdog_register_governor);
76
77void watchdog_unregister_governor(struct watchdog_governor *gov)
78{
79 struct watchdog_pretimeout *p;
80
81 spin_lock_irq(&pretimeout_lock);
82 if (gov == default_gov)
83 default_gov = NULL;
84
85 list_for_each_entry(p, &pretimeout_list, entry)
86 if (p->wdd->gov == gov)
87 p->wdd->gov = default_gov;
88 spin_unlock_irq(&pretimeout_lock);
89}
90EXPORT_SYMBOL(watchdog_unregister_governor);
91
92int watchdog_register_pretimeout(struct watchdog_device *wdd)
93{
94 struct watchdog_pretimeout *p;
95
96 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
97 return 0;
98
99 p = kzalloc(sizeof(*p), GFP_KERNEL);
100 if (!p)
101 return -ENOMEM;
102
103 spin_lock_irq(&pretimeout_lock);
104 list_add(&p->entry, &pretimeout_list);
105 p->wdd = wdd;
106 wdd->gov = default_gov;
107 spin_unlock_irq(&pretimeout_lock);
108
109 return 0;
110}
111
112void watchdog_unregister_pretimeout(struct watchdog_device *wdd)
113{
114 struct watchdog_pretimeout *p, *t;
115
116 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
117 return;
118
119 spin_lock_irq(&pretimeout_lock);
120 wdd->gov = NULL;
121
122 list_for_each_entry_safe(p, t, &pretimeout_list, entry) {
123 if (p->wdd == wdd) {
124 list_del(&p->entry);
125 break;
126 }
127 }
128 spin_unlock_irq(&pretimeout_lock);
129
130 kfree(p);
131}