blob: 597a1e836f223762da499dc3e1ff3689610a9deb [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
Thomas Gleixner9f083b72015-03-25 13:05:19 +010011#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -080012
Ingo Molnar9eed56e2015-04-02 11:26:23 +020013# include <linux/clocksource.h>
14# include <linux/cpumask.h>
15# include <linux/ktime.h>
16# include <linux/notifier.h>
Thomas Gleixnerd316c572007-02-16 01:28:00 -080017
18struct clock_event_device;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000019struct module;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080020
Viresh Kumar77e32c82015-02-27 17:21:33 +053021/* Clock event mode commands for legacy ->set_mode(): OBSOLETE */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080022enum clock_event_mode {
Ingo Molnar9eed56e2015-04-02 11:26:23 +020023 CLOCK_EVT_MODE_UNUSED,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080024 CLOCK_EVT_MODE_SHUTDOWN,
25 CLOCK_EVT_MODE_PERIODIC,
26 CLOCK_EVT_MODE_ONESHOT,
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070027 CLOCK_EVT_MODE_RESUME,
Viresh Kumar77e32c82015-02-27 17:21:33 +053028};
Viresh Kumarbd624d72015-02-13 08:54:56 +080029
Viresh Kumar77e32c82015-02-27 17:21:33 +053030/*
31 * Possible states of a clock event device.
32 *
33 * DETACHED: Device is not used by clockevents core. Initial state or can be
34 * reached from SHUTDOWN.
35 * SHUTDOWN: Device is powered-off. Can be reached from PERIODIC or ONESHOT.
36 * PERIODIC: Device is programmed to generate events periodically. Can be
37 * reached from DETACHED or SHUTDOWN.
38 * ONESHOT: Device is programmed to generate event only once. Can be reached
39 * from DETACHED or SHUTDOWN.
Viresh Kumar8fff52f2015-04-03 09:04:04 +053040 * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily
41 * stopped.
Viresh Kumar77e32c82015-02-27 17:21:33 +053042 */
43enum clock_event_state {
Ingo Molnar9eed56e2015-04-02 11:26:23 +020044 CLOCK_EVT_STATE_DETACHED,
Viresh Kumar77e32c82015-02-27 17:21:33 +053045 CLOCK_EVT_STATE_SHUTDOWN,
46 CLOCK_EVT_STATE_PERIODIC,
47 CLOCK_EVT_STATE_ONESHOT,
Viresh Kumar8fff52f2015-04-03 09:04:04 +053048 CLOCK_EVT_STATE_ONESHOT_STOPPED,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080049};
50
Thomas Gleixnerd316c572007-02-16 01:28:00 -080051/*
52 * Clock event features
53 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020054# define CLOCK_EVT_FEAT_PERIODIC 0x000001
55# define CLOCK_EVT_FEAT_ONESHOT 0x000002
56# define CLOCK_EVT_FEAT_KTIME 0x000004
57
Thomas Gleixnerd316c572007-02-16 01:28:00 -080058/*
Ingo Molnar9eed56e2015-04-02 11:26:23 +020059 * x86(64) specific (mis)features:
Thomas Gleixnerd316c572007-02-16 01:28:00 -080060 *
61 * - Clockevent source stops in C3 State and needs broadcast support.
62 * - Local APIC timer is used as a dummy device.
63 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020064# define CLOCK_EVT_FEAT_C3STOP 0x000008
65# define CLOCK_EVT_FEAT_DUMMY 0x000010
Thomas Gleixnerd316c572007-02-16 01:28:00 -080066
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010067/*
68 * Core shall set the interrupt affinity dynamically in broadcast mode
69 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020070# define CLOCK_EVT_FEAT_DYNIRQ 0x000020
71# define CLOCK_EVT_FEAT_PERCPU 0x000040
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010072
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053073/*
74 * Clockevent device is based on a hrtimer for broadcast
75 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020076# define CLOCK_EVT_FEAT_HRTIMER 0x000080
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053077
Thomas Gleixnerd316c572007-02-16 01:28:00 -080078/**
79 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000080 * @event_handler: Assigned by the framework to be called by the low
81 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020082 * @set_next_event: set next event function using a clocksource delta
83 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000084 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080085 * @max_delta_ns: maximum delta value in ns
86 * @min_delta_ns: minimum delta value in ns
87 * @mult: nanosecond to cycles multiplier
88 * @shift: nanoseconds to cycles divisor (power of two)
Viresh Kumar77e32c82015-02-27 17:21:33 +053089 * @mode: operating mode, relevant only to ->set_mode(), OBSOLETE
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +020090 * @state_use_accessors:current state of the device, assigned by the core code
Thomas Gleixner847b2f42011-05-18 21:33:41 +000091 * @features: features
92 * @retries: number of forced programming retries
Viresh Kumarbd624d72015-02-13 08:54:56 +080093 * @set_mode: legacy set mode function, only for modes <= CLOCK_EVT_MODE_RESUME.
Viresh Kumar77e32c82015-02-27 17:21:33 +053094 * @set_state_periodic: switch state to periodic, if !set_mode
95 * @set_state_oneshot: switch state to oneshot, if !set_mode
Viresh Kumar8fff52f2015-04-03 09:04:04 +053096 * @set_state_oneshot_stopped: switch state to oneshot_stopped, if !set_mode
Viresh Kumar77e32c82015-02-27 17:21:33 +053097 * @set_state_shutdown: switch state to shutdown, if !set_mode
Viresh Kumar554ef382015-02-27 17:21:32 +053098 * @tick_resume: resume clkevt device, if !set_mode
Thomas Gleixner847b2f42011-05-18 21:33:41 +000099 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000100 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
101 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000102 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800103 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -0700104 * @irq: IRQ number (only for non CPU local devices)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530105 * @bound_on: Bound on CPU
Sergei Shtylyovce0be122007-05-08 00:31:55 -0700106 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800107 * @list: list head for the management code
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000108 * @owner: module reference
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800109 */
110struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000111 void (*event_handler)(struct clock_event_device *);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200112 int (*set_next_event)(unsigned long evt, struct clock_event_device *);
113 int (*set_next_ktime)(ktime_t expires, struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000114 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -0500115 u64 max_delta_ns;
116 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +0000117 u32 mult;
118 u32 shift;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000119 enum clock_event_mode mode;
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200120 enum clock_event_state state_use_accessors;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000121 unsigned int features;
122 unsigned long retries;
123
Viresh Kumarbd624d72015-02-13 08:54:56 +0800124 /*
Viresh Kumar77e32c82015-02-27 17:21:33 +0530125 * State transition callback(s): Only one of the two groups should be
Viresh Kumarbd624d72015-02-13 08:54:56 +0800126 * defined:
127 * - set_mode(), only for modes <= CLOCK_EVT_MODE_RESUME.
Viresh Kumar8fff52f2015-04-03 09:04:04 +0530128 * - set_state_{shutdown|periodic|oneshot|oneshot_stopped}(), tick_resume().
Viresh Kumarbd624d72015-02-13 08:54:56 +0800129 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200130 void (*set_mode)(enum clock_event_mode mode, struct clock_event_device *);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530131 int (*set_state_periodic)(struct clock_event_device *);
132 int (*set_state_oneshot)(struct clock_event_device *);
Viresh Kumar8fff52f2015-04-03 09:04:04 +0530133 int (*set_state_oneshot_stopped)(struct clock_event_device *);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530134 int (*set_state_shutdown)(struct clock_event_device *);
Viresh Kumar554ef382015-02-27 17:21:32 +0530135 int (*tick_resume)(struct clock_event_device *);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800136
137 void (*broadcast)(const struct cpumask *mask);
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200138 void (*suspend)(struct clock_event_device *);
139 void (*resume)(struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000140 unsigned long min_delta_ticks;
141 unsigned long max_delta_ticks;
142
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000143 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800144 int rating;
145 int irq;
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530146 int bound_on;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030147 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800148 struct list_head list;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000149 struct module *owner;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000150} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800151
Viresh Kumar3434d232015-05-21 13:33:45 +0530152/* Helpers to verify state of a clockevent device */
153static inline bool clockevent_state_detached(struct clock_event_device *dev)
154{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200155 return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED;
Viresh Kumar3434d232015-05-21 13:33:45 +0530156}
157
158static inline bool clockevent_state_shutdown(struct clock_event_device *dev)
159{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200160 return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN;
Viresh Kumar3434d232015-05-21 13:33:45 +0530161}
162
163static inline bool clockevent_state_periodic(struct clock_event_device *dev)
164{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200165 return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC;
Viresh Kumar3434d232015-05-21 13:33:45 +0530166}
167
168static inline bool clockevent_state_oneshot(struct clock_event_device *dev)
169{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200170 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT;
Viresh Kumar3434d232015-05-21 13:33:45 +0530171}
172
173static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev)
174{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200175 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED;
Viresh Kumar3434d232015-05-21 13:33:45 +0530176}
177
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800178/*
179 * Calculate a multiplication factor for scaled math, which is used to convert
180 * nanoseconds based values to clock ticks:
181 *
182 * clock_ticks = (nanoseconds * factor) >> shift.
183 *
184 * div_sc is the rearranged equation to calculate a factor from a given clock
185 * ticks / nanoseconds ratio:
186 *
187 * factor = (clock_ticks << shift) / nanoseconds
188 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200189static inline unsigned long
190div_sc(unsigned long ticks, unsigned long nsec, int shift)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800191{
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200192 u64 tmp = ((u64)ticks) << shift;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800193
194 do_div(tmp, nsec);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200195
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800196 return (unsigned long) tmp;
197}
198
199/* Clock event layer functions */
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200200extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800201extern void clockevents_register_device(struct clock_event_device *dev);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000202extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800203
Magnus Damme5400322012-05-09 23:39:34 +0900204extern void clockevents_config(struct clock_event_device *dev, u32 freq);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000205extern void clockevents_config_and_register(struct clock_event_device *dev,
206 u32 freq, unsigned long min_delta,
207 unsigned long max_delta);
208
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000209extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
210
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000211static inline void
212clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
213{
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200214 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, minsec);
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000215}
216
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200217extern void clockevents_suspend(void);
218extern void clockevents_resume(void);
219
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200220# ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
221# ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
Mark Rutland12ad1002013-01-14 17:05:22 +0000222extern void tick_broadcast(const struct cpumask *mask);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200223# else
224# define tick_broadcast NULL
225# endif
Mark Rutland12572db2013-01-14 17:05:21 +0000226extern int tick_receive_broadcast(void);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200227# endif
Mark Rutland12572db2013-01-14 17:05:21 +0000228
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200229# if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530230extern void tick_setup_hrtimer_broadcast(void);
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000231extern int tick_check_broadcast_expired(void);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200232# else
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000233static inline int tick_check_broadcast_expired(void) { return 0; }
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200234static inline void tick_setup_hrtimer_broadcast(void) { }
235# endif
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000236
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530237extern int clockevents_notify(unsigned long reason, void *arg);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200238
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200239#else /* !CONFIG_GENERIC_CLOCKEVENTS: */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800240
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200241static inline void clockevents_suspend(void) { }
242static inline void clockevents_resume(void) { }
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; }
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200245static inline void tick_setup_hrtimer_broadcast(void) { }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800246
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200247#endif /* !CONFIG_GENERIC_CLOCKEVENTS */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800248
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200249#endif /* _LINUX_CLOCKCHIPS_H */