Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 1 | /* |
| 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 */ |
| 19 | static struct watchdog_governor *default_gov; |
| 20 | |
| 21 | /* The spinlock protects default_gov, wdd->gov and pretimeout_list */ |
| 22 | static DEFINE_SPINLOCK(pretimeout_lock); |
| 23 | |
| 24 | /* List of watchdog devices, which can generate a pretimeout event */ |
| 25 | static LIST_HEAD(pretimeout_list); |
| 26 | |
| 27 | struct watchdog_pretimeout { |
| 28 | struct watchdog_device *wdd; |
| 29 | struct list_head entry; |
| 30 | }; |
| 31 | |
| 32 | int 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 | |
| 44 | void 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 | } |
| 57 | EXPORT_SYMBOL_GPL(watchdog_notify_pretimeout); |
| 58 | |
| 59 | int watchdog_register_governor(struct watchdog_governor *gov) |
| 60 | { |
| 61 | struct watchdog_pretimeout *p; |
| 62 | |
Vladimir Zapolskiy | da0d12f | 2016-10-07 15:39:56 +0300 | [diff] [blame^] | 63 | if (!strncmp(gov->name, WATCHDOG_PRETIMEOUT_DEFAULT_GOV, |
| 64 | WATCHDOG_GOV_NAME_MAXLEN)) { |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 65 | 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 | } |
| 76 | EXPORT_SYMBOL(watchdog_register_governor); |
| 77 | |
| 78 | void watchdog_unregister_governor(struct watchdog_governor *gov) |
| 79 | { |
| 80 | struct watchdog_pretimeout *p; |
| 81 | |
| 82 | spin_lock_irq(&pretimeout_lock); |
Vladimir Zapolskiy | ff84136 | 2016-10-07 15:39:54 +0300 | [diff] [blame] | 83 | 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 | } |
| 88 | EXPORT_SYMBOL(watchdog_unregister_governor); |
| 89 | |
| 90 | int 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 | |
| 110 | void 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 | } |