blob: e20232c3320ab29b64c888ee474a9bc1aec3da66 [file] [log] [blame]
Thomas Gleixnerd316c572007-02-16 01:28:00 -08001/* linux/include/linux/clockchips.h
2 *
3 * This file contains the structure definitions for clockchips.
4 *
5 * If you are not a clockchip, or the time of day code, you should
6 * not be including this file!
7 */
8#ifndef _LINUX_CLOCKCHIPS_H
9#define _LINUX_CLOCKCHIPS_H
10
Daniel Lezcano4dbad812013-03-27 10:22:09 +000011/* Clock event notification values */
12enum clock_event_nofitiers {
13 CLOCK_EVT_NOTIFY_ADD,
14 CLOCK_EVT_NOTIFY_BROADCAST_ON,
15 CLOCK_EVT_NOTIFY_BROADCAST_OFF,
16 CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
17 CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
18 CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
19 CLOCK_EVT_NOTIFY_SUSPEND,
20 CLOCK_EVT_NOTIFY_RESUME,
21 CLOCK_EVT_NOTIFY_CPU_DYING,
22 CLOCK_EVT_NOTIFY_CPU_DEAD,
23};
24
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +020025#ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD
Thomas Gleixnerd316c572007-02-16 01:28:00 -080026
27#include <linux/clocksource.h>
28#include <linux/cpumask.h>
29#include <linux/ktime.h>
30#include <linux/notifier.h>
31
32struct clock_event_device;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000033struct module;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080034
Viresh Kumar77e32c82015-02-27 17:21:33 +053035/* Clock event mode commands for legacy ->set_mode(): OBSOLETE */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080036enum clock_event_mode {
37 CLOCK_EVT_MODE_UNUSED = 0,
38 CLOCK_EVT_MODE_SHUTDOWN,
39 CLOCK_EVT_MODE_PERIODIC,
40 CLOCK_EVT_MODE_ONESHOT,
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070041 CLOCK_EVT_MODE_RESUME,
Viresh Kumar77e32c82015-02-27 17:21:33 +053042};
Viresh Kumarbd624d72015-02-13 08:54:56 +080043
Viresh Kumar77e32c82015-02-27 17:21:33 +053044/*
45 * Possible states of a clock event device.
46 *
47 * DETACHED: Device is not used by clockevents core. Initial state or can be
48 * reached from SHUTDOWN.
49 * SHUTDOWN: Device is powered-off. Can be reached from PERIODIC or ONESHOT.
50 * PERIODIC: Device is programmed to generate events periodically. Can be
51 * reached from DETACHED or SHUTDOWN.
52 * ONESHOT: Device is programmed to generate event only once. Can be reached
53 * from DETACHED or SHUTDOWN.
54 */
55enum clock_event_state {
56 CLOCK_EVT_STATE_DETACHED = 0,
57 CLOCK_EVT_STATE_SHUTDOWN,
58 CLOCK_EVT_STATE_PERIODIC,
59 CLOCK_EVT_STATE_ONESHOT,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080060};
61
Thomas Gleixnerd316c572007-02-16 01:28:00 -080062/*
63 * Clock event features
64 */
65#define CLOCK_EVT_FEAT_PERIODIC 0x000001
66#define CLOCK_EVT_FEAT_ONESHOT 0x000002
Martin Schwidefsky65516f82011-08-23 15:29:43 +020067#define CLOCK_EVT_FEAT_KTIME 0x000004
Thomas Gleixnerd316c572007-02-16 01:28:00 -080068/*
69 * x86(64) specific misfeatures:
70 *
71 * - Clockevent source stops in C3 State and needs broadcast support.
72 * - Local APIC timer is used as a dummy device.
73 */
Martin Schwidefsky65516f82011-08-23 15:29:43 +020074#define CLOCK_EVT_FEAT_C3STOP 0x000008
75#define CLOCK_EVT_FEAT_DUMMY 0x000010
Thomas Gleixnerd316c572007-02-16 01:28:00 -080076
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010077/*
78 * Core shall set the interrupt affinity dynamically in broadcast mode
79 */
80#define CLOCK_EVT_FEAT_DYNIRQ 0x000020
Soren Brinkmann3713c0c2013-09-18 11:48:35 -070081#define CLOCK_EVT_FEAT_PERCPU 0x000040
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010082
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053083/*
84 * Clockevent device is based on a hrtimer for broadcast
85 */
86#define CLOCK_EVT_FEAT_HRTIMER 0x000080
87
Thomas Gleixnerd316c572007-02-16 01:28:00 -080088/**
89 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000090 * @event_handler: Assigned by the framework to be called by the low
91 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020092 * @set_next_event: set next event function using a clocksource delta
93 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000094 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080095 * @max_delta_ns: maximum delta value in ns
96 * @min_delta_ns: minimum delta value in ns
97 * @mult: nanosecond to cycles multiplier
98 * @shift: nanoseconds to cycles divisor (power of two)
Viresh Kumar77e32c82015-02-27 17:21:33 +053099 * @mode: operating mode, relevant only to ->set_mode(), OBSOLETE
100 * @state: current state of the device, assigned by the core code
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000101 * @features: features
102 * @retries: number of forced programming retries
Viresh Kumarbd624d72015-02-13 08:54:56 +0800103 * @set_mode: legacy set mode function, only for modes <= CLOCK_EVT_MODE_RESUME.
Viresh Kumar77e32c82015-02-27 17:21:33 +0530104 * @set_state_periodic: switch state to periodic, if !set_mode
105 * @set_state_oneshot: switch state to oneshot, if !set_mode
106 * @set_state_shutdown: switch state to shutdown, if !set_mode
Viresh Kumar554ef382015-02-27 17:21:32 +0530107 * @tick_resume: resume clkevt device, if !set_mode
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000108 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000109 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
110 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000111 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800112 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -0700113 * @irq: IRQ number (only for non CPU local devices)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530114 * @bound_on: Bound on CPU
Sergei Shtylyovce0be122007-05-08 00:31:55 -0700115 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800116 * @list: list head for the management code
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000117 * @owner: module reference
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800118 */
119struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000120 void (*event_handler)(struct clock_event_device *);
121 int (*set_next_event)(unsigned long evt,
122 struct clock_event_device *);
Martin Schwidefsky65516f82011-08-23 15:29:43 +0200123 int (*set_next_ktime)(ktime_t expires,
124 struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000125 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -0500126 u64 max_delta_ns;
127 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +0000128 u32 mult;
129 u32 shift;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000130 enum clock_event_mode mode;
Viresh Kumar77e32c82015-02-27 17:21:33 +0530131 enum clock_event_state state;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000132 unsigned int features;
133 unsigned long retries;
134
Viresh Kumarbd624d72015-02-13 08:54:56 +0800135 /*
Viresh Kumar77e32c82015-02-27 17:21:33 +0530136 * State transition callback(s): Only one of the two groups should be
Viresh Kumarbd624d72015-02-13 08:54:56 +0800137 * defined:
138 * - set_mode(), only for modes <= CLOCK_EVT_MODE_RESUME.
Viresh Kumar77e32c82015-02-27 17:21:33 +0530139 * - set_state_{shutdown|periodic|oneshot}(), tick_resume().
Viresh Kumarbd624d72015-02-13 08:54:56 +0800140 */
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000141 void (*set_mode)(enum clock_event_mode mode,
142 struct clock_event_device *);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530143 int (*set_state_periodic)(struct clock_event_device *);
144 int (*set_state_oneshot)(struct clock_event_device *);
145 int (*set_state_shutdown)(struct clock_event_device *);
Viresh Kumar554ef382015-02-27 17:21:32 +0530146 int (*tick_resume)(struct clock_event_device *);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800147
148 void (*broadcast)(const struct cpumask *mask);
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200149 void (*suspend)(struct clock_event_device *);
150 void (*resume)(struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000151 unsigned long min_delta_ticks;
152 unsigned long max_delta_ticks;
153
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000154 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800155 int rating;
156 int irq;
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530157 int bound_on;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030158 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800159 struct list_head list;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000160 struct module *owner;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000161} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800162
163/*
164 * Calculate a multiplication factor for scaled math, which is used to convert
165 * nanoseconds based values to clock ticks:
166 *
167 * clock_ticks = (nanoseconds * factor) >> shift.
168 *
169 * div_sc is the rearranged equation to calculate a factor from a given clock
170 * ticks / nanoseconds ratio:
171 *
172 * factor = (clock_ticks << shift) / nanoseconds
173 */
174static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
175 int shift)
176{
177 uint64_t tmp = ((uint64_t)ticks) << shift;
178
179 do_div(tmp, nsec);
180 return (unsigned long) tmp;
181}
182
183/* Clock event layer functions */
Jon Hunter97813f22009-08-18 12:45:11 -0500184extern u64 clockevent_delta2ns(unsigned long latch,
185 struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800186extern void clockevents_register_device(struct clock_event_device *dev);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000187extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800188
Magnus Damme5400322012-05-09 23:39:34 +0900189extern void clockevents_config(struct clock_event_device *dev, u32 freq);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000190extern void clockevents_config_and_register(struct clock_event_device *dev,
191 u32 freq, unsigned long min_delta,
192 unsigned long max_delta);
193
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000194extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
195
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800196extern void clockevents_exchange_device(struct clock_event_device *old,
197 struct clock_event_device *new);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530198extern void clockevents_set_state(struct clock_event_device *dev,
199 enum clock_event_state state);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800200extern int clockevents_program_event(struct clock_event_device *dev,
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200201 ktime_t expires, bool force);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800202
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000203extern void clockevents_handle_noop(struct clock_event_device *dev);
204
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000205static inline void
206clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
207{
208 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
209 freq, minsec);
210}
211
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200212extern void clockevents_suspend(void);
213extern void clockevents_resume(void);
214
Mark Rutland12572db2013-01-14 17:05:21 +0000215#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
Mark Rutland12ad1002013-01-14 17:05:22 +0000216#ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
217extern void tick_broadcast(const struct cpumask *mask);
218#else
219#define tick_broadcast NULL
220#endif
Mark Rutland12572db2013-01-14 17:05:21 +0000221extern int tick_receive_broadcast(void);
222#endif
223
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000224#if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530225extern void tick_setup_hrtimer_broadcast(void);
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000226extern int tick_check_broadcast_expired(void);
227#else
228static inline int tick_check_broadcast_expired(void) { return 0; }
Thomas Gleixnerf1689bb2014-02-07 16:00:46 +0100229static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000230#endif
231
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200232#ifdef CONFIG_GENERIC_CLOCKEVENTS
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530233extern int clockevents_notify(unsigned long reason, void *arg);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800234#else
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530235static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200236#endif
237
238#else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800239
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200240static inline void clockevents_suspend(void) {}
241static inline void clockevents_resume(void) {}
242
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530243static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixner19919222013-03-22 10:48:33 +0100244static inline int tick_check_broadcast_expired(void) { return 0; }
Preeti U Murthy849401b2014-02-09 11:32:22 +0530245static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800246
247#endif
248
249#endif