blob: 0d442e34c34979a503030214007223742b3bc822 [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/*
22 * Possible states of a clock event device.
23 *
24 * DETACHED: Device is not used by clockevents core. Initial state or can be
25 * reached from SHUTDOWN.
26 * SHUTDOWN: Device is powered-off. Can be reached from PERIODIC or ONESHOT.
27 * PERIODIC: Device is programmed to generate events periodically. Can be
28 * reached from DETACHED or SHUTDOWN.
29 * ONESHOT: Device is programmed to generate event only once. Can be reached
30 * from DETACHED or SHUTDOWN.
Viresh Kumar8fff52f2015-04-03 09:04:04 +053031 * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily
32 * stopped.
Viresh Kumar77e32c82015-02-27 17:21:33 +053033 */
34enum clock_event_state {
Ingo Molnar9eed56e2015-04-02 11:26:23 +020035 CLOCK_EVT_STATE_DETACHED,
Viresh Kumar77e32c82015-02-27 17:21:33 +053036 CLOCK_EVT_STATE_SHUTDOWN,
37 CLOCK_EVT_STATE_PERIODIC,
38 CLOCK_EVT_STATE_ONESHOT,
Viresh Kumar8fff52f2015-04-03 09:04:04 +053039 CLOCK_EVT_STATE_ONESHOT_STOPPED,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080040};
41
Thomas Gleixnerd316c572007-02-16 01:28:00 -080042/*
43 * Clock event features
44 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020045# define CLOCK_EVT_FEAT_PERIODIC 0x000001
46# define CLOCK_EVT_FEAT_ONESHOT 0x000002
47# define CLOCK_EVT_FEAT_KTIME 0x000004
48
Thomas Gleixnerd316c572007-02-16 01:28:00 -080049/*
Ingo Molnar9eed56e2015-04-02 11:26:23 +020050 * x86(64) specific (mis)features:
Thomas Gleixnerd316c572007-02-16 01:28:00 -080051 *
52 * - Clockevent source stops in C3 State and needs broadcast support.
53 * - Local APIC timer is used as a dummy device.
54 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020055# define CLOCK_EVT_FEAT_C3STOP 0x000008
56# define CLOCK_EVT_FEAT_DUMMY 0x000010
Thomas Gleixnerd316c572007-02-16 01:28:00 -080057
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010058/*
59 * Core shall set the interrupt affinity dynamically in broadcast mode
60 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020061# define CLOCK_EVT_FEAT_DYNIRQ 0x000020
62# define CLOCK_EVT_FEAT_PERCPU 0x000040
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010063
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053064/*
65 * Clockevent device is based on a hrtimer for broadcast
66 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020067# define CLOCK_EVT_FEAT_HRTIMER 0x000080
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053068
Thomas Gleixnerd316c572007-02-16 01:28:00 -080069/**
70 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000071 * @event_handler: Assigned by the framework to be called by the low
72 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020073 * @set_next_event: set next event function using a clocksource delta
74 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000075 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080076 * @max_delta_ns: maximum delta value in ns
77 * @min_delta_ns: minimum delta value in ns
78 * @mult: nanosecond to cycles multiplier
79 * @shift: nanoseconds to cycles divisor (power of two)
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +020080 * @state_use_accessors:current state of the device, assigned by the core code
Thomas Gleixner847b2f42011-05-18 21:33:41 +000081 * @features: features
82 * @retries: number of forced programming retries
Viresh Kumareef76352015-09-11 09:34:26 +053083 * @set_state_periodic: switch state to periodic
84 * @set_state_oneshot: switch state to oneshot
85 * @set_state_oneshot_stopped: switch state to oneshot_stopped
86 * @set_state_shutdown: switch state to shutdown
87 * @tick_resume: resume clkevt device
Thomas Gleixner847b2f42011-05-18 21:33:41 +000088 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +000089 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
90 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +000091 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -080092 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -070093 * @irq: IRQ number (only for non CPU local devices)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053094 * @bound_on: Bound on CPU
Sergei Shtylyovce0be122007-05-08 00:31:55 -070095 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -080096 * @list: list head for the management code
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000097 * @owner: module reference
Thomas Gleixnerd316c572007-02-16 01:28:00 -080098 */
99struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000100 void (*event_handler)(struct clock_event_device *);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200101 int (*set_next_event)(unsigned long evt, struct clock_event_device *);
102 int (*set_next_ktime)(ktime_t expires, struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000103 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -0500104 u64 max_delta_ns;
105 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +0000106 u32 mult;
107 u32 shift;
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200108 enum clock_event_state state_use_accessors;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000109 unsigned int features;
110 unsigned long retries;
111
Viresh Kumar77e32c82015-02-27 17:21:33 +0530112 int (*set_state_periodic)(struct clock_event_device *);
113 int (*set_state_oneshot)(struct clock_event_device *);
Viresh Kumar8fff52f2015-04-03 09:04:04 +0530114 int (*set_state_oneshot_stopped)(struct clock_event_device *);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530115 int (*set_state_shutdown)(struct clock_event_device *);
Viresh Kumar554ef382015-02-27 17:21:32 +0530116 int (*tick_resume)(struct clock_event_device *);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800117
118 void (*broadcast)(const struct cpumask *mask);
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200119 void (*suspend)(struct clock_event_device *);
120 void (*resume)(struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000121 unsigned long min_delta_ticks;
122 unsigned long max_delta_ticks;
123
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000124 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800125 int rating;
126 int irq;
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530127 int bound_on;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030128 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800129 struct list_head list;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000130 struct module *owner;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000131} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800132
Viresh Kumar3434d232015-05-21 13:33:45 +0530133/* Helpers to verify state of a clockevent device */
134static inline bool clockevent_state_detached(struct clock_event_device *dev)
135{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200136 return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED;
Viresh Kumar3434d232015-05-21 13:33:45 +0530137}
138
139static inline bool clockevent_state_shutdown(struct clock_event_device *dev)
140{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200141 return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN;
Viresh Kumar3434d232015-05-21 13:33:45 +0530142}
143
144static inline bool clockevent_state_periodic(struct clock_event_device *dev)
145{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200146 return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC;
Viresh Kumar3434d232015-05-21 13:33:45 +0530147}
148
149static inline bool clockevent_state_oneshot(struct clock_event_device *dev)
150{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200151 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT;
Viresh Kumar3434d232015-05-21 13:33:45 +0530152}
153
154static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev)
155{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200156 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED;
Viresh Kumar3434d232015-05-21 13:33:45 +0530157}
158
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800159/*
160 * Calculate a multiplication factor for scaled math, which is used to convert
161 * nanoseconds based values to clock ticks:
162 *
163 * clock_ticks = (nanoseconds * factor) >> shift.
164 *
165 * div_sc is the rearranged equation to calculate a factor from a given clock
166 * ticks / nanoseconds ratio:
167 *
168 * factor = (clock_ticks << shift) / nanoseconds
169 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200170static inline unsigned long
171div_sc(unsigned long ticks, unsigned long nsec, int shift)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800172{
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200173 u64 tmp = ((u64)ticks) << shift;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800174
175 do_div(tmp, nsec);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200176
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800177 return (unsigned long) tmp;
178}
179
180/* Clock event layer functions */
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200181extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800182extern void clockevents_register_device(struct clock_event_device *dev);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000183extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800184
Magnus Damme5400322012-05-09 23:39:34 +0900185extern void clockevents_config(struct clock_event_device *dev, u32 freq);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000186extern void clockevents_config_and_register(struct clock_event_device *dev,
187 u32 freq, unsigned long min_delta,
188 unsigned long max_delta);
189
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000190extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
191
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000192static inline void
Alexander Kuleshovdd42ac8f2015-10-16 15:20:53 +0600193clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 maxsec)
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000194{
Alexander Kuleshovdd42ac8f2015-10-16 15:20:53 +0600195 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, maxsec);
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000196}
197
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200198extern void clockevents_suspend(void);
199extern void clockevents_resume(void);
200
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200201# ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
202# ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
Mark Rutland12ad1002013-01-14 17:05:22 +0000203extern void tick_broadcast(const struct cpumask *mask);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200204# else
205# define tick_broadcast NULL
206# endif
Mark Rutland12572db2013-01-14 17:05:21 +0000207extern int tick_receive_broadcast(void);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200208# endif
Mark Rutland12572db2013-01-14 17:05:21 +0000209
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200210# if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530211extern void tick_setup_hrtimer_broadcast(void);
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000212extern int tick_check_broadcast_expired(void);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200213# else
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000214static inline int tick_check_broadcast_expired(void) { return 0; }
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200215static inline void tick_setup_hrtimer_broadcast(void) { }
216# endif
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000217
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200218#else /* !CONFIG_GENERIC_CLOCKEVENTS: */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800219
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200220static inline void clockevents_suspend(void) { }
221static inline void clockevents_resume(void) { }
Thomas Gleixner19919222013-03-22 10:48:33 +0100222static inline int tick_check_broadcast_expired(void) { return 0; }
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200223static inline void tick_setup_hrtimer_broadcast(void) { }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800224
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200225#endif /* !CONFIG_GENERIC_CLOCKEVENTS */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800226
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200227#endif /* _LINUX_CLOCKCHIPS_H */