| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * linux/kernel/time/tick-oneshot.c | 
 | 3 |  * | 
 | 4 |  * This file contains functions which manage high resolution tick | 
 | 5 |  * related events. | 
 | 6 |  * | 
 | 7 |  * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> | 
 | 8 |  * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar | 
 | 9 |  * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner | 
 | 10 |  * | 
 | 11 |  * This code is licenced under the GPL version 2. For details see | 
 | 12 |  * kernel-base/COPYING. | 
 | 13 |  */ | 
 | 14 | #include <linux/cpu.h> | 
 | 15 | #include <linux/err.h> | 
 | 16 | #include <linux/hrtimer.h> | 
| Russell King | d7b9068 | 2008-04-17 07:46:24 +0200 | [diff] [blame] | 17 | #include <linux/interrupt.h> | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 18 | #include <linux/percpu.h> | 
 | 19 | #include <linux/profile.h> | 
 | 20 | #include <linux/sched.h> | 
 | 21 | #include <linux/tick.h> | 
 | 22 |  | 
 | 23 | #include "tick-internal.h" | 
 | 24 |  | 
| Thomas Gleixner | 80a05b9 | 2010-03-12 17:34:14 +0100 | [diff] [blame] | 25 | /* Limit min_delta to a jiffie */ | 
 | 26 | #define MIN_DELTA_LIMIT		(NSEC_PER_SEC / HZ) | 
 | 27 |  | 
 | 28 | static int tick_increase_min_delta(struct clock_event_device *dev) | 
 | 29 | { | 
 | 30 | 	/* Nothing to do if we already reached the limit */ | 
 | 31 | 	if (dev->min_delta_ns >= MIN_DELTA_LIMIT) | 
 | 32 | 		return -ETIME; | 
 | 33 |  | 
 | 34 | 	if (dev->min_delta_ns < 5000) | 
 | 35 | 		dev->min_delta_ns = 5000; | 
 | 36 | 	else | 
 | 37 | 		dev->min_delta_ns += dev->min_delta_ns >> 1; | 
 | 38 |  | 
 | 39 | 	if (dev->min_delta_ns > MIN_DELTA_LIMIT) | 
 | 40 | 		dev->min_delta_ns = MIN_DELTA_LIMIT; | 
 | 41 |  | 
 | 42 | 	printk(KERN_WARNING "CE: %s increased min_delta_ns to %llu nsec\n", | 
 | 43 | 	       dev->name ? dev->name : "?", | 
 | 44 | 	       (unsigned long long) dev->min_delta_ns); | 
 | 45 | 	return 0; | 
 | 46 | } | 
 | 47 |  | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 48 | /** | 
| Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 49 |  * tick_program_event internal worker function | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 50 |  */ | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 51 | int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires, | 
 | 52 | 			   int force) | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 53 | { | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 54 | 	ktime_t now = ktime_get(); | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 55 | 	int i; | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 56 |  | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 57 | 	for (i = 0;;) { | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 58 | 		int ret = clockevents_program_event(dev, expires, now); | 
 | 59 |  | 
 | 60 | 		if (!ret || !force) | 
 | 61 | 			return ret; | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 62 |  | 
| Thomas Gleixner | 80a05b9 | 2010-03-12 17:34:14 +0100 | [diff] [blame] | 63 | 		dev->retries++; | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 64 | 		/* | 
| Thomas Gleixner | 80a05b9 | 2010-03-12 17:34:14 +0100 | [diff] [blame] | 65 | 		 * We tried 3 times to program the device with the given | 
 | 66 | 		 * min_delta_ns. If that's not working then we increase it | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 67 | 		 * and emit a warning. | 
 | 68 | 		 */ | 
 | 69 | 		if (++i > 2) { | 
| Thomas Gleixner | 61c22c3 | 2008-09-09 21:38:57 +0200 | [diff] [blame] | 70 | 			/* Increase the min. delta and try again */ | 
| Thomas Gleixner | 80a05b9 | 2010-03-12 17:34:14 +0100 | [diff] [blame] | 71 | 			if (tick_increase_min_delta(dev)) { | 
 | 72 | 				/* | 
 | 73 | 				 * Get out of the loop if min_delta_ns | 
 | 74 | 				 * hit the limit already. That's | 
 | 75 | 				 * better than staying here forever. | 
 | 76 | 				 * | 
 | 77 | 				 * We clear next_event so we have a | 
 | 78 | 				 * chance that the box survives. | 
 | 79 | 				 */ | 
 | 80 | 				printk(KERN_WARNING | 
 | 81 | 				       "CE: Reprogramming failure. Giving up\n"); | 
 | 82 | 				dev->next_event.tv64 = KTIME_MAX; | 
 | 83 | 				return -ETIME; | 
 | 84 | 			} | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 85 | 			i = 0; | 
 | 86 | 		} | 
 | 87 |  | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 88 | 		now = ktime_get(); | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 89 | 		expires = ktime_add_ns(now, dev->min_delta_ns); | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 90 | 	} | 
 | 91 | } | 
 | 92 |  | 
 | 93 | /** | 
| Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 94 |  * tick_program_event | 
 | 95 |  */ | 
 | 96 | int tick_program_event(ktime_t expires, int force) | 
 | 97 | { | 
 | 98 | 	struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; | 
 | 99 |  | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 100 | 	return tick_dev_program_event(dev, expires, force); | 
| Thomas Gleixner | 7205656 | 2008-09-03 21:37:03 +0000 | [diff] [blame] | 101 | } | 
 | 102 |  | 
 | 103 | /** | 
| Thomas Gleixner | cd05a1f | 2007-03-17 00:25:52 +0100 | [diff] [blame] | 104 |  * tick_resume_onshot - resume oneshot mode | 
 | 105 |  */ | 
 | 106 | void tick_resume_oneshot(void) | 
 | 107 | { | 
 | 108 | 	struct tick_device *td = &__get_cpu_var(tick_cpu_device); | 
 | 109 | 	struct clock_event_device *dev = td->evtdev; | 
 | 110 |  | 
 | 111 | 	clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); | 
 | 112 | 	tick_program_event(ktime_get(), 1); | 
 | 113 | } | 
 | 114 |  | 
 | 115 | /** | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 116 |  * tick_setup_oneshot - setup the event device for oneshot mode (hres or nohz) | 
 | 117 |  */ | 
 | 118 | void tick_setup_oneshot(struct clock_event_device *newdev, | 
 | 119 | 			void (*handler)(struct clock_event_device *), | 
 | 120 | 			ktime_t next_event) | 
 | 121 | { | 
 | 122 | 	newdev->event_handler = handler; | 
 | 123 | 	clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT); | 
| Thomas Gleixner | 1fb9b7d | 2008-09-03 21:37:14 +0000 | [diff] [blame] | 124 | 	tick_dev_program_event(newdev, next_event, 1); | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 125 | } | 
 | 126 |  | 
 | 127 | /** | 
 | 128 |  * tick_switch_to_oneshot - switch to oneshot mode | 
 | 129 |  */ | 
 | 130 | int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *)) | 
 | 131 | { | 
 | 132 | 	struct tick_device *td = &__get_cpu_var(tick_cpu_device); | 
 | 133 | 	struct clock_event_device *dev = td->evtdev; | 
 | 134 |  | 
 | 135 | 	if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT) || | 
| Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 136 | 		    !tick_device_is_functional(dev)) { | 
 | 137 |  | 
 | 138 | 		printk(KERN_INFO "Clockevents: " | 
 | 139 | 		       "could not switch to one-shot mode:"); | 
 | 140 | 		if (!dev) { | 
 | 141 | 			printk(" no tick device\n"); | 
 | 142 | 		} else { | 
 | 143 | 			if (!tick_device_is_functional(dev)) | 
 | 144 | 				printk(" %s is not functional.\n", dev->name); | 
 | 145 | 			else | 
 | 146 | 				printk(" %s does not support one-shot mode.\n", | 
 | 147 | 				       dev->name); | 
 | 148 | 		} | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 149 | 		return -EINVAL; | 
| Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 150 | 	} | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 151 |  | 
 | 152 | 	td->mode = TICKDEV_MODE_ONESHOT; | 
 | 153 | 	dev->event_handler = handler; | 
 | 154 | 	clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT); | 
 | 155 | 	tick_broadcast_switch_to_oneshot(); | 
 | 156 | 	return 0; | 
 | 157 | } | 
 | 158 |  | 
| Thomas Gleixner | cd6d95d | 2009-06-12 11:29:27 +0200 | [diff] [blame] | 159 | /** | 
 | 160 |  * tick_check_oneshot_mode - check whether the system is in oneshot mode | 
 | 161 |  * | 
 | 162 |  * returns 1 when either nohz or highres are enabled. otherwise 0. | 
 | 163 |  */ | 
 | 164 | int tick_oneshot_mode_active(void) | 
 | 165 | { | 
 | 166 | 	unsigned long flags; | 
 | 167 | 	int ret; | 
 | 168 |  | 
 | 169 | 	local_irq_save(flags); | 
 | 170 | 	ret = __get_cpu_var(tick_cpu_device).mode == TICKDEV_MODE_ONESHOT; | 
 | 171 | 	local_irq_restore(flags); | 
 | 172 |  | 
 | 173 | 	return ret; | 
 | 174 | } | 
 | 175 |  | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 176 | #ifdef CONFIG_HIGH_RES_TIMERS | 
 | 177 | /** | 
 | 178 |  * tick_init_highres - switch to high resolution mode | 
 | 179 |  * | 
 | 180 |  * Called with interrupts disabled. | 
 | 181 |  */ | 
 | 182 | int tick_init_highres(void) | 
 | 183 | { | 
 | 184 | 	return tick_switch_to_oneshot(hrtimer_interrupt); | 
 | 185 | } | 
 | 186 | #endif |