Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/jiffies.h> |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 21 | #include <linux/sched.h> |
| 22 | #include <linux/interrupt.h> |
Hanumant Singh | 97cf75a | 2012-08-22 14:07:29 -0700 | [diff] [blame] | 23 | #include <linux/irq.h> |
| 24 | #include <linux/percpu.h> |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/cpu.h> |
| 27 | #include <linux/platform_device.h> |
Hanumant Singh | 15784c8 | 2012-08-02 17:37:04 -0700 | [diff] [blame] | 28 | #include <mach/scm.h> |
| 29 | #include <mach/msm_memory_dump.h> |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 30 | |
| 31 | #define MODULE_NAME "msm_watchdog" |
| 32 | #define WDT0_ACCSCSSNBARK_INT 0 |
| 33 | #define TCSR_WDT_CFG 0x30 |
| 34 | #define WDT0_RST 0x04 |
| 35 | #define WDT0_EN 0x08 |
| 36 | #define WDT0_STS 0x0C |
| 37 | #define WDT0_BARK_TIME 0x10 |
| 38 | #define WDT0_BITE_TIME 0x14 |
| 39 | |
Hanumant Singh | 15784c8 | 2012-08-02 17:37:04 -0700 | [diff] [blame] | 40 | #define MASK_SIZE 32 |
| 41 | #define SCM_SET_REGSAVE_CMD 0x2 |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 42 | #define SCM_SVC_SEC_WDOG_DIS 0x7 |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 43 | |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 44 | static struct workqueue_struct *wdog_wq; |
| 45 | |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 46 | struct msm_watchdog_data { |
| 47 | unsigned int __iomem phys_base; |
| 48 | size_t size; |
| 49 | void __iomem *base; |
| 50 | struct device *dev; |
| 51 | unsigned int pet_time; |
| 52 | unsigned int bark_time; |
| 53 | unsigned int bark_irq; |
| 54 | unsigned int bite_irq; |
Mitchel Humpherys | 1be2380 | 2012-11-16 15:52:32 -0800 | [diff] [blame] | 55 | bool do_ipi_ping; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 56 | unsigned long long last_pet; |
| 57 | unsigned min_slack_ticks; |
| 58 | unsigned long long min_slack_ns; |
Hanumant Singh | 15784c8 | 2012-08-02 17:37:04 -0700 | [diff] [blame] | 59 | void *scm_regsave; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 60 | cpumask_t alive_mask; |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 61 | struct mutex disable_lock; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 62 | struct work_struct init_dogwork_struct; |
| 63 | struct delayed_work dogwork_struct; |
Hanumant Singh | 97cf75a | 2012-08-22 14:07:29 -0700 | [diff] [blame] | 64 | bool irq_ppi; |
| 65 | struct msm_watchdog_data __percpu **wdog_cpu_dd; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 66 | struct notifier_block panic_blk; |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * On the kernel command line specify |
| 71 | * msm_watchdog.enable=1 to enable the watchdog |
| 72 | * By default watchdog is turned on |
| 73 | */ |
| 74 | static int enable = 1; |
| 75 | module_param(enable, int, 0); |
| 76 | |
| 77 | /* |
| 78 | * On the kernel command line specify |
| 79 | * msm_watchdog.WDT_HZ=<clock val in HZ> to set Watchdog |
| 80 | * ticks. By default it is set to 32765. |
| 81 | */ |
| 82 | static long WDT_HZ = 32765; |
| 83 | module_param(WDT_HZ, long, 0); |
| 84 | |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 85 | static void pet_watchdog_work(struct work_struct *work); |
| 86 | static void init_watchdog_work(struct work_struct *work); |
| 87 | |
| 88 | static void dump_cpu_alive_mask(struct msm_watchdog_data *wdog_dd) |
| 89 | { |
| 90 | static char alive_mask_buf[MASK_SIZE]; |
Hanumant Singh | a876ba6 | 2012-08-17 13:46:42 -0700 | [diff] [blame] | 91 | cpulist_scnprintf(alive_mask_buf, MASK_SIZE, |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 92 | &wdog_dd->alive_mask); |
Hanumant Singh | a876ba6 | 2012-08-17 13:46:42 -0700 | [diff] [blame] | 93 | printk(KERN_INFO "cpu alive mask from last pet %s\n", alive_mask_buf); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int msm_watchdog_suspend(struct device *dev) |
| 97 | { |
| 98 | struct msm_watchdog_data *wdog_dd = |
| 99 | (struct msm_watchdog_data *)dev_get_drvdata(dev); |
| 100 | if (!enable) |
| 101 | return 0; |
| 102 | __raw_writel(1, wdog_dd->base + WDT0_RST); |
| 103 | __raw_writel(0, wdog_dd->base + WDT0_EN); |
| 104 | mb(); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int msm_watchdog_resume(struct device *dev) |
| 109 | { |
| 110 | struct msm_watchdog_data *wdog_dd = |
| 111 | (struct msm_watchdog_data *)dev_get_drvdata(dev); |
| 112 | if (!enable) |
| 113 | return 0; |
| 114 | __raw_writel(1, wdog_dd->base + WDT0_EN); |
| 115 | __raw_writel(1, wdog_dd->base + WDT0_RST); |
| 116 | mb(); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int panic_wdog_handler(struct notifier_block *this, |
| 121 | unsigned long event, void *ptr) |
| 122 | { |
| 123 | struct msm_watchdog_data *wdog_dd = container_of(this, |
| 124 | struct msm_watchdog_data, panic_blk); |
| 125 | if (panic_timeout == 0) { |
| 126 | __raw_writel(0, wdog_dd->base + WDT0_EN); |
| 127 | mb(); |
| 128 | } else { |
| 129 | __raw_writel(WDT_HZ * (panic_timeout + 4), |
| 130 | wdog_dd->base + WDT0_BARK_TIME); |
| 131 | __raw_writel(WDT_HZ * (panic_timeout + 4), |
| 132 | wdog_dd->base + WDT0_BITE_TIME); |
| 133 | __raw_writel(1, wdog_dd->base + WDT0_RST); |
| 134 | } |
| 135 | return NOTIFY_DONE; |
| 136 | } |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 137 | |
| 138 | static void wdog_disable(struct msm_watchdog_data *wdog_dd) |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 139 | { |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 140 | __raw_writel(0, wdog_dd->base + WDT0_EN); |
| 141 | mb(); |
| 142 | if (wdog_dd->irq_ppi) { |
| 143 | disable_percpu_irq(wdog_dd->bark_irq); |
| 144 | free_percpu_irq(wdog_dd->bark_irq, wdog_dd->wdog_cpu_dd); |
| 145 | } else |
| 146 | devm_free_irq(wdog_dd->dev, wdog_dd->bark_irq, wdog_dd); |
| 147 | enable = 0; |
| 148 | /*Ensure all cpus see update to enable*/ |
| 149 | smp_mb(); |
| 150 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 151 | &wdog_dd->panic_blk); |
| 152 | cancel_delayed_work_sync(&wdog_dd->dogwork_struct); |
| 153 | /* may be suspended after the first write above */ |
| 154 | __raw_writel(0, wdog_dd->base + WDT0_EN); |
| 155 | mb(); |
| 156 | pr_info("MSM Apps Watchdog deactivated.\n"); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 159 | struct wdog_disable_work_data { |
| 160 | struct work_struct work; |
| 161 | struct completion complete; |
| 162 | struct msm_watchdog_data *wdog_dd; |
| 163 | }; |
| 164 | |
| 165 | static void wdog_disable_work(struct work_struct *work) |
| 166 | { |
| 167 | struct wdog_disable_work_data *work_data = |
| 168 | container_of(work, struct wdog_disable_work_data, work); |
| 169 | wdog_disable(work_data->wdog_dd); |
| 170 | complete(&work_data->complete); |
| 171 | } |
| 172 | |
| 173 | static ssize_t wdog_disable_get(struct device *dev, |
| 174 | struct device_attribute *attr, char *buf) |
| 175 | { |
| 176 | int ret; |
| 177 | struct msm_watchdog_data *wdog_dd = dev_get_drvdata(dev); |
| 178 | |
| 179 | mutex_lock(&wdog_dd->disable_lock); |
| 180 | ret = snprintf(buf, PAGE_SIZE, "%d\n", enable == 0 ? 1 : 0); |
| 181 | mutex_unlock(&wdog_dd->disable_lock); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static ssize_t wdog_disable_set(struct device *dev, |
| 186 | struct device_attribute *attr, |
| 187 | const char *buf, size_t count) |
| 188 | { |
| 189 | int ret; |
| 190 | u8 disable; |
| 191 | struct wdog_disable_work_data work_data; |
| 192 | struct msm_watchdog_data *wdog_dd = dev_get_drvdata(dev); |
| 193 | |
| 194 | ret = kstrtou8(buf, 10, &disable); |
| 195 | if (ret) { |
| 196 | dev_err(wdog_dd->dev, "invalid user input\n"); |
| 197 | return ret; |
| 198 | } |
| 199 | if (disable == 1) { |
| 200 | mutex_lock(&wdog_dd->disable_lock); |
| 201 | if (enable == 0) { |
| 202 | pr_info("MSM Apps Watchdog already disabled\n"); |
| 203 | mutex_unlock(&wdog_dd->disable_lock); |
| 204 | return count; |
| 205 | } |
| 206 | disable = 1; |
| 207 | ret = scm_call(SCM_SVC_BOOT, SCM_SVC_SEC_WDOG_DIS, &disable, |
| 208 | sizeof(disable), NULL, 0); |
| 209 | if (ret) { |
| 210 | dev_err(wdog_dd->dev, |
| 211 | "Failed to deactivate secure wdog\n"); |
| 212 | mutex_unlock(&wdog_dd->disable_lock); |
| 213 | return -EIO; |
| 214 | } |
| 215 | work_data.wdog_dd = wdog_dd; |
| 216 | init_completion(&work_data.complete); |
| 217 | INIT_WORK_ONSTACK(&work_data.work, wdog_disable_work); |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 218 | queue_work_on(0, wdog_wq, &work_data.work); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 219 | wait_for_completion(&work_data.complete); |
| 220 | mutex_unlock(&wdog_dd->disable_lock); |
| 221 | } else { |
| 222 | pr_err("invalid operation, only disable = 1 supported\n"); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | return count; |
| 226 | } |
| 227 | |
| 228 | static DEVICE_ATTR(disable, S_IWUSR | S_IRUSR, wdog_disable_get, |
| 229 | wdog_disable_set); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 230 | |
| 231 | static void pet_watchdog(struct msm_watchdog_data *wdog_dd) |
| 232 | { |
Matt Wagantall | fc10f28 | 2012-10-11 11:39:20 -0700 | [diff] [blame] | 233 | int slack, i, count, prev_count = 0; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 234 | unsigned long long time_ns; |
| 235 | unsigned long long slack_ns; |
| 236 | unsigned long long bark_time_ns = wdog_dd->bark_time * 1000000ULL; |
| 237 | |
Matt Wagantall | fc10f28 | 2012-10-11 11:39:20 -0700 | [diff] [blame] | 238 | for (i = 0; i < 2; i++) { |
| 239 | count = (__raw_readl(wdog_dd->base + WDT0_STS) >> 1) & 0xFFFFF; |
| 240 | if (count != prev_count) { |
| 241 | prev_count = count; |
| 242 | i = 0; |
| 243 | } |
| 244 | } |
| 245 | slack = ((wdog_dd->bark_time * WDT_HZ) / 1000) - count; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 246 | if (slack < wdog_dd->min_slack_ticks) |
| 247 | wdog_dd->min_slack_ticks = slack; |
| 248 | __raw_writel(1, wdog_dd->base + WDT0_RST); |
| 249 | time_ns = sched_clock(); |
| 250 | slack_ns = (wdog_dd->last_pet + bark_time_ns) - time_ns; |
| 251 | if (slack_ns < wdog_dd->min_slack_ns) |
| 252 | wdog_dd->min_slack_ns = slack_ns; |
| 253 | wdog_dd->last_pet = time_ns; |
| 254 | } |
| 255 | |
| 256 | static void keep_alive_response(void *info) |
| 257 | { |
| 258 | int cpu = smp_processor_id(); |
| 259 | struct msm_watchdog_data *wdog_dd = (struct msm_watchdog_data *)info; |
| 260 | cpumask_set_cpu(cpu, &wdog_dd->alive_mask); |
| 261 | smp_mb(); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * If this function does not return, it implies one of the |
| 266 | * other cpu's is not responsive. |
| 267 | */ |
| 268 | static void ping_other_cpus(struct msm_watchdog_data *wdog_dd) |
| 269 | { |
| 270 | int cpu; |
| 271 | cpumask_clear(&wdog_dd->alive_mask); |
| 272 | smp_mb(); |
| 273 | for_each_cpu(cpu, cpu_online_mask) |
| 274 | smp_call_function_single(cpu, keep_alive_response, wdog_dd, 1); |
| 275 | } |
| 276 | |
| 277 | static void pet_watchdog_work(struct work_struct *work) |
| 278 | { |
| 279 | unsigned long delay_time; |
| 280 | struct delayed_work *delayed_work = to_delayed_work(work); |
| 281 | struct msm_watchdog_data *wdog_dd = container_of(delayed_work, |
| 282 | struct msm_watchdog_data, |
| 283 | dogwork_struct); |
| 284 | delay_time = msecs_to_jiffies(wdog_dd->pet_time); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 285 | if (enable) { |
| 286 | if (wdog_dd->do_ipi_ping) |
| 287 | ping_other_cpus(wdog_dd); |
| 288 | pet_watchdog(wdog_dd); |
| 289 | } |
| 290 | /* Check again before scheduling * |
| 291 | * Could have been changed on other cpu */ |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 292 | if (enable) |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 293 | queue_delayed_work_on(0, wdog_wq, |
| 294 | &wdog_dd->dogwork_struct, delay_time); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static int msm_watchdog_remove(struct platform_device *pdev) |
| 298 | { |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 299 | struct wdog_disable_work_data work_data; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 300 | struct msm_watchdog_data *wdog_dd = |
| 301 | (struct msm_watchdog_data *)platform_get_drvdata(pdev); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 302 | |
| 303 | mutex_lock(&wdog_dd->disable_lock); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 304 | if (enable) { |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 305 | work_data.wdog_dd = wdog_dd; |
| 306 | init_completion(&work_data.complete); |
| 307 | INIT_WORK_ONSTACK(&work_data.work, wdog_disable_work); |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 308 | queue_work_on(0, wdog_wq, &work_data.work); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 309 | wait_for_completion(&work_data.complete); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 310 | } |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 311 | mutex_unlock(&wdog_dd->disable_lock); |
| 312 | device_remove_file(wdog_dd->dev, &dev_attr_disable); |
Hanumant Singh | 97cf75a | 2012-08-22 14:07:29 -0700 | [diff] [blame] | 313 | if (wdog_dd->irq_ppi) |
| 314 | free_percpu(wdog_dd->wdog_cpu_dd); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 315 | printk(KERN_INFO "MSM Watchdog Exit - Deactivated\n"); |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 316 | destroy_workqueue(wdog_wq); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 317 | kfree(wdog_dd); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static irqreturn_t wdog_bark_handler(int irq, void *dev_id) |
| 322 | { |
| 323 | struct msm_watchdog_data *wdog_dd = (struct msm_watchdog_data *)dev_id; |
| 324 | unsigned long nanosec_rem; |
| 325 | unsigned long long t = sched_clock(); |
| 326 | |
| 327 | nanosec_rem = do_div(t, 1000000000); |
| 328 | printk(KERN_INFO "Watchdog bark! Now = %lu.%06lu\n", (unsigned long) t, |
| 329 | nanosec_rem / 1000); |
| 330 | |
| 331 | nanosec_rem = do_div(wdog_dd->last_pet, 1000000000); |
| 332 | printk(KERN_INFO "Watchdog last pet at %lu.%06lu\n", (unsigned long) |
| 333 | wdog_dd->last_pet, nanosec_rem / 1000); |
| 334 | if (wdog_dd->do_ipi_ping) |
| 335 | dump_cpu_alive_mask(wdog_dd); |
Pushkar Joshi | e77c087 | 2013-01-17 19:04:37 -0800 | [diff] [blame] | 336 | printk(KERN_INFO "Causing a watchdog bite!"); |
| 337 | __raw_writel(1, wdog_dd->base + WDT0_BITE_TIME); |
| 338 | mb(); |
| 339 | __raw_writel(1, wdog_dd->base + WDT0_RST); |
| 340 | mb(); |
| 341 | /* Delay to make sure bite occurs */ |
| 342 | mdelay(1); |
| 343 | panic("Failed to cause a watchdog bite! - Falling back to kernel panic!"); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 344 | return IRQ_HANDLED; |
| 345 | } |
| 346 | |
Hanumant Singh | 97cf75a | 2012-08-22 14:07:29 -0700 | [diff] [blame] | 347 | static irqreturn_t wdog_ppi_bark(int irq, void *dev_id) |
| 348 | { |
| 349 | struct msm_watchdog_data *wdog_dd = |
| 350 | *(struct msm_watchdog_data **)(dev_id); |
| 351 | return wdog_bark_handler(irq, wdog_dd); |
| 352 | } |
| 353 | |
Hanumant Singh | 15784c8 | 2012-08-02 17:37:04 -0700 | [diff] [blame] | 354 | static void configure_bark_dump(struct msm_watchdog_data *wdog_dd) |
| 355 | { |
| 356 | int ret; |
| 357 | struct msm_client_dump dump_entry; |
| 358 | struct { |
| 359 | unsigned addr; |
| 360 | int len; |
| 361 | } cmd_buf; |
| 362 | |
| 363 | wdog_dd->scm_regsave = (void *)__get_free_page(GFP_KERNEL); |
| 364 | if (wdog_dd->scm_regsave) { |
| 365 | cmd_buf.addr = virt_to_phys(wdog_dd->scm_regsave); |
| 366 | cmd_buf.len = PAGE_SIZE; |
| 367 | ret = scm_call(SCM_SVC_UTIL, SCM_SET_REGSAVE_CMD, |
| 368 | &cmd_buf, sizeof(cmd_buf), NULL, 0); |
| 369 | if (ret) |
| 370 | pr_err("Setting register save address failed.\n" |
| 371 | "Registers won't be dumped on a dog " |
| 372 | "bite\n"); |
| 373 | dump_entry.id = MSM_CPU_CTXT; |
| 374 | dump_entry.start_addr = virt_to_phys(wdog_dd->scm_regsave); |
| 375 | dump_entry.end_addr = dump_entry.start_addr + PAGE_SIZE; |
| 376 | ret = msm_dump_table_register(&dump_entry); |
| 377 | if (ret) |
| 378 | pr_err("Setting cpu dump region failed\n" |
| 379 | "Registers wont be dumped during cpu hang\n"); |
| 380 | } else { |
| 381 | pr_err("Allocating register save space failed\n" |
| 382 | "Registers won't be dumped on a dog bite\n"); |
| 383 | /* |
| 384 | * No need to bail if allocation fails. Simply don't |
| 385 | * send the command, and the secure side will reset |
| 386 | * without saving registers. |
| 387 | */ |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 392 | static void init_watchdog_work(struct work_struct *work) |
| 393 | { |
| 394 | struct msm_watchdog_data *wdog_dd = container_of(work, |
| 395 | struct msm_watchdog_data, |
| 396 | init_dogwork_struct); |
| 397 | unsigned long delay_time; |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 398 | int error; |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 399 | u64 timeout; |
Hanumant Singh | 97cf75a | 2012-08-22 14:07:29 -0700 | [diff] [blame] | 400 | int ret; |
| 401 | |
| 402 | if (wdog_dd->irq_ppi) { |
| 403 | wdog_dd->wdog_cpu_dd = alloc_percpu(struct msm_watchdog_data *); |
| 404 | if (!wdog_dd->wdog_cpu_dd) { |
| 405 | dev_err(wdog_dd->dev, "fail to allocate cpu data\n"); |
| 406 | return; |
| 407 | } |
| 408 | *__this_cpu_ptr(wdog_dd->wdog_cpu_dd) = wdog_dd; |
| 409 | ret = request_percpu_irq(wdog_dd->bark_irq, wdog_ppi_bark, |
| 410 | "apps_wdog_bark", |
| 411 | wdog_dd->wdog_cpu_dd); |
| 412 | if (ret) { |
| 413 | dev_err(wdog_dd->dev, "failed to request bark irq\n"); |
| 414 | free_percpu(wdog_dd->wdog_cpu_dd); |
| 415 | return; |
| 416 | } |
| 417 | } else { |
| 418 | ret = devm_request_irq(wdog_dd->dev, wdog_dd->bark_irq, |
| 419 | wdog_bark_handler, IRQF_TRIGGER_RISING, |
| 420 | "apps_wdog_bark", wdog_dd); |
| 421 | if (ret) { |
| 422 | dev_err(wdog_dd->dev, "failed to request bark irq\n"); |
| 423 | return; |
| 424 | } |
| 425 | } |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 426 | delay_time = msecs_to_jiffies(wdog_dd->pet_time); |
| 427 | wdog_dd->min_slack_ticks = UINT_MAX; |
| 428 | wdog_dd->min_slack_ns = ULLONG_MAX; |
Hanumant Singh | 15784c8 | 2012-08-02 17:37:04 -0700 | [diff] [blame] | 429 | configure_bark_dump(wdog_dd); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 430 | timeout = (wdog_dd->bark_time * WDT_HZ)/1000; |
| 431 | __raw_writel(timeout, wdog_dd->base + WDT0_BARK_TIME); |
| 432 | __raw_writel(timeout + 3*WDT_HZ, wdog_dd->base + WDT0_BITE_TIME); |
| 433 | |
| 434 | wdog_dd->panic_blk.notifier_call = panic_wdog_handler; |
| 435 | atomic_notifier_chain_register(&panic_notifier_list, |
| 436 | &wdog_dd->panic_blk); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 437 | mutex_init(&wdog_dd->disable_lock); |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 438 | queue_delayed_work_on(0, wdog_wq, &wdog_dd->dogwork_struct, |
| 439 | delay_time); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 440 | __raw_writel(1, wdog_dd->base + WDT0_EN); |
| 441 | __raw_writel(1, wdog_dd->base + WDT0_RST); |
| 442 | wdog_dd->last_pet = sched_clock(); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 443 | error = device_create_file(wdog_dd->dev, &dev_attr_disable); |
| 444 | if (error) |
| 445 | dev_err(wdog_dd->dev, "cannot create sysfs attribute\n"); |
Hanumant Singh | 97cf75a | 2012-08-22 14:07:29 -0700 | [diff] [blame] | 446 | if (wdog_dd->irq_ppi) |
| 447 | enable_percpu_irq(wdog_dd->bark_irq, 0); |
Hanumant Singh | 42ffa44 | 2012-08-27 12:26:08 -0700 | [diff] [blame] | 448 | dev_info(wdog_dd->dev, "MSM Watchdog Initialized\n"); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | |
| 452 | static struct of_device_id msm_wdog_match_table[] = { |
| 453 | { .compatible = "qcom,msm-watchdog" }, |
| 454 | {} |
| 455 | }; |
| 456 | |
| 457 | static void __devinit dump_pdata(struct msm_watchdog_data *pdata) |
| 458 | { |
| 459 | dev_dbg(pdata->dev, "wdog bark_time %d", pdata->bark_time); |
| 460 | dev_dbg(pdata->dev, "wdog pet_time %d", pdata->pet_time); |
| 461 | dev_dbg(pdata->dev, "wdog perform ipi ping %d", pdata->do_ipi_ping); |
| 462 | dev_dbg(pdata->dev, "wdog base address is 0x%x\n", (unsigned int) |
| 463 | pdata->base); |
| 464 | } |
| 465 | |
| 466 | static int __devinit msm_wdog_dt_to_pdata(struct platform_device *pdev, |
| 467 | struct msm_watchdog_data *pdata) |
| 468 | { |
| 469 | struct device_node *node = pdev->dev.of_node; |
| 470 | struct resource *wdog_resource; |
| 471 | int ret; |
| 472 | |
| 473 | wdog_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 474 | pdata->size = resource_size(wdog_resource); |
| 475 | pdata->phys_base = wdog_resource->start; |
| 476 | if (unlikely(!(devm_request_region(&pdev->dev, pdata->phys_base, |
| 477 | pdata->size, "msm-watchdog")))) { |
| 478 | dev_err(&pdev->dev, "%s cannot reserve watchdog region\n", |
| 479 | __func__); |
| 480 | return -ENXIO; |
| 481 | } |
| 482 | pdata->base = devm_ioremap(&pdev->dev, pdata->phys_base, |
| 483 | pdata->size); |
| 484 | if (!pdata->base) { |
| 485 | dev_err(&pdev->dev, "%s cannot map wdog register space\n", |
| 486 | __func__); |
| 487 | return -ENXIO; |
| 488 | } |
| 489 | |
| 490 | pdata->bark_irq = platform_get_irq(pdev, 0); |
| 491 | pdata->bite_irq = platform_get_irq(pdev, 1); |
| 492 | ret = of_property_read_u32(node, "qcom,bark-time", &pdata->bark_time); |
| 493 | if (ret) { |
| 494 | dev_err(&pdev->dev, "reading bark time failed\n"); |
| 495 | return -ENXIO; |
| 496 | } |
| 497 | ret = of_property_read_u32(node, "qcom,pet-time", &pdata->pet_time); |
| 498 | if (ret) { |
| 499 | dev_err(&pdev->dev, "reading pet time failed\n"); |
| 500 | return -ENXIO; |
| 501 | } |
Mitchel Humpherys | 1be2380 | 2012-11-16 15:52:32 -0800 | [diff] [blame] | 502 | pdata->do_ipi_ping = of_property_read_bool(node, "qcom,ipi-ping"); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 503 | if (!pdata->bark_time) { |
| 504 | dev_err(&pdev->dev, "%s watchdog bark time not setup\n", |
| 505 | __func__); |
| 506 | return -ENXIO; |
| 507 | } |
| 508 | if (!pdata->pet_time) { |
| 509 | dev_err(&pdev->dev, "%s watchdog pet time not setup\n", |
| 510 | __func__); |
| 511 | return -ENXIO; |
| 512 | } |
Hanumant Singh | 97cf75a | 2012-08-22 14:07:29 -0700 | [diff] [blame] | 513 | pdata->irq_ppi = irq_is_per_cpu(pdata->bark_irq); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 514 | dump_pdata(pdata); |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int __devinit msm_watchdog_probe(struct platform_device *pdev) |
| 519 | { |
| 520 | int ret; |
| 521 | struct msm_watchdog_data *wdog_dd; |
| 522 | |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 523 | wdog_wq = alloc_workqueue("wdog", WQ_HIGHPRI, 0); |
| 524 | if (!wdog_wq) { |
| 525 | pr_err("Failed to allocate watchdog workqueue\n"); |
| 526 | return -EIO; |
| 527 | } |
| 528 | |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 529 | if (!pdev->dev.of_node || !enable) |
| 530 | return -ENODEV; |
| 531 | wdog_dd = kzalloc(sizeof(struct msm_watchdog_data), GFP_KERNEL); |
| 532 | if (!wdog_dd) |
| 533 | return -EIO; |
| 534 | ret = msm_wdog_dt_to_pdata(pdev, wdog_dd); |
| 535 | if (ret) |
| 536 | goto err; |
| 537 | wdog_dd->dev = &pdev->dev; |
| 538 | platform_set_drvdata(pdev, wdog_dd); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 539 | cpumask_clear(&wdog_dd->alive_mask); |
| 540 | INIT_WORK(&wdog_dd->init_dogwork_struct, init_watchdog_work); |
| 541 | INIT_DELAYED_WORK(&wdog_dd->dogwork_struct, pet_watchdog_work); |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 542 | queue_work_on(0, wdog_wq, &wdog_dd->init_dogwork_struct); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 543 | return 0; |
| 544 | err: |
Mitchel Humpherys | 0d89e94 | 2012-12-13 16:56:46 -0800 | [diff] [blame] | 545 | destroy_workqueue(wdog_wq); |
Hanumant | 72aec70 | 2012-06-25 11:51:07 -0700 | [diff] [blame] | 546 | kzfree(wdog_dd); |
| 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | static const struct dev_pm_ops msm_watchdog_dev_pm_ops = { |
| 551 | .suspend_noirq = msm_watchdog_suspend, |
| 552 | .resume_noirq = msm_watchdog_resume, |
| 553 | }; |
| 554 | |
| 555 | static struct platform_driver msm_watchdog_driver = { |
| 556 | .probe = msm_watchdog_probe, |
| 557 | .remove = msm_watchdog_remove, |
| 558 | .driver = { |
| 559 | .name = MODULE_NAME, |
| 560 | .owner = THIS_MODULE, |
| 561 | .pm = &msm_watchdog_dev_pm_ops, |
| 562 | .of_match_table = msm_wdog_match_table, |
| 563 | }, |
| 564 | }; |
| 565 | |
| 566 | static int __devinit init_watchdog(void) |
| 567 | { |
| 568 | return platform_driver_register(&msm_watchdog_driver); |
| 569 | } |
| 570 | |
| 571 | late_initcall(init_watchdog); |
| 572 | MODULE_DESCRIPTION("MSM Watchdog Driver"); |
| 573 | MODULE_LICENSE("GPL v2"); |