blob: 3ac7e2d90374fd5f88f8a4130fa2aa7b827c20c4 [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,
Daniel Lezcano4dbad812013-03-27 10:22:09 +000019 CLOCK_EVT_NOTIFY_CPU_DYING,
20 CLOCK_EVT_NOTIFY_CPU_DEAD,
21};
22
Thomas Gleixner9f083b72015-03-25 13:05:19 +010023#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -080024
25#include <linux/clocksource.h>
26#include <linux/cpumask.h>
27#include <linux/ktime.h>
28#include <linux/notifier.h>
29
30struct clock_event_device;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000031struct module;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080032
Viresh Kumar77e32c82015-02-27 17:21:33 +053033/* Clock event mode commands for legacy ->set_mode(): OBSOLETE */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080034enum clock_event_mode {
35 CLOCK_EVT_MODE_UNUSED = 0,
36 CLOCK_EVT_MODE_SHUTDOWN,
37 CLOCK_EVT_MODE_PERIODIC,
38 CLOCK_EVT_MODE_ONESHOT,
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070039 CLOCK_EVT_MODE_RESUME,
Viresh Kumar77e32c82015-02-27 17:21:33 +053040};
Viresh Kumarbd624d72015-02-13 08:54:56 +080041
Viresh Kumar77e32c82015-02-27 17:21:33 +053042/*
43 * Possible states of a clock event device.
44 *
45 * DETACHED: Device is not used by clockevents core. Initial state or can be
46 * reached from SHUTDOWN.
47 * SHUTDOWN: Device is powered-off. Can be reached from PERIODIC or ONESHOT.
48 * PERIODIC: Device is programmed to generate events periodically. Can be
49 * reached from DETACHED or SHUTDOWN.
50 * ONESHOT: Device is programmed to generate event only once. Can be reached
51 * from DETACHED or SHUTDOWN.
52 */
53enum clock_event_state {
54 CLOCK_EVT_STATE_DETACHED = 0,
55 CLOCK_EVT_STATE_SHUTDOWN,
56 CLOCK_EVT_STATE_PERIODIC,
57 CLOCK_EVT_STATE_ONESHOT,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080058};
59
Thomas Gleixnerd316c572007-02-16 01:28:00 -080060/*
61 * Clock event features
62 */
63#define CLOCK_EVT_FEAT_PERIODIC 0x000001
64#define CLOCK_EVT_FEAT_ONESHOT 0x000002
Martin Schwidefsky65516f82011-08-23 15:29:43 +020065#define CLOCK_EVT_FEAT_KTIME 0x000004
Thomas Gleixnerd316c572007-02-16 01:28:00 -080066/*
67 * x86(64) specific misfeatures:
68 *
69 * - Clockevent source stops in C3 State and needs broadcast support.
70 * - Local APIC timer is used as a dummy device.
71 */
Martin Schwidefsky65516f82011-08-23 15:29:43 +020072#define CLOCK_EVT_FEAT_C3STOP 0x000008
73#define CLOCK_EVT_FEAT_DUMMY 0x000010
Thomas Gleixnerd316c572007-02-16 01:28:00 -080074
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010075/*
76 * Core shall set the interrupt affinity dynamically in broadcast mode
77 */
78#define CLOCK_EVT_FEAT_DYNIRQ 0x000020
Soren Brinkmann3713c0c2013-09-18 11:48:35 -070079#define CLOCK_EVT_FEAT_PERCPU 0x000040
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010080
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053081/*
82 * Clockevent device is based on a hrtimer for broadcast
83 */
84#define CLOCK_EVT_FEAT_HRTIMER 0x000080
85
Thomas Gleixnerd316c572007-02-16 01:28:00 -080086/**
87 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000088 * @event_handler: Assigned by the framework to be called by the low
89 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020090 * @set_next_event: set next event function using a clocksource delta
91 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000092 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080093 * @max_delta_ns: maximum delta value in ns
94 * @min_delta_ns: minimum delta value in ns
95 * @mult: nanosecond to cycles multiplier
96 * @shift: nanoseconds to cycles divisor (power of two)
Viresh Kumar77e32c82015-02-27 17:21:33 +053097 * @mode: operating mode, relevant only to ->set_mode(), OBSOLETE
98 * @state: current state of the device, assigned by the core code
Thomas Gleixner847b2f42011-05-18 21:33:41 +000099 * @features: features
100 * @retries: number of forced programming retries
Viresh Kumarbd624d72015-02-13 08:54:56 +0800101 * @set_mode: legacy set mode function, only for modes <= CLOCK_EVT_MODE_RESUME.
Viresh Kumar77e32c82015-02-27 17:21:33 +0530102 * @set_state_periodic: switch state to periodic, if !set_mode
103 * @set_state_oneshot: switch state to oneshot, if !set_mode
104 * @set_state_shutdown: switch state to shutdown, if !set_mode
Viresh Kumar554ef382015-02-27 17:21:32 +0530105 * @tick_resume: resume clkevt device, if !set_mode
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000106 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000107 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
108 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000109 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800110 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -0700111 * @irq: IRQ number (only for non CPU local devices)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530112 * @bound_on: Bound on CPU
Sergei Shtylyovce0be122007-05-08 00:31:55 -0700113 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800114 * @list: list head for the management code
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000115 * @owner: module reference
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800116 */
117struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000118 void (*event_handler)(struct clock_event_device *);
119 int (*set_next_event)(unsigned long evt,
120 struct clock_event_device *);
Martin Schwidefsky65516f82011-08-23 15:29:43 +0200121 int (*set_next_ktime)(ktime_t expires,
122 struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000123 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -0500124 u64 max_delta_ns;
125 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +0000126 u32 mult;
127 u32 shift;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000128 enum clock_event_mode mode;
Viresh Kumar77e32c82015-02-27 17:21:33 +0530129 enum clock_event_state state;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000130 unsigned int features;
131 unsigned long retries;
132
Viresh Kumarbd624d72015-02-13 08:54:56 +0800133 /*
Viresh Kumar77e32c82015-02-27 17:21:33 +0530134 * State transition callback(s): Only one of the two groups should be
Viresh Kumarbd624d72015-02-13 08:54:56 +0800135 * defined:
136 * - set_mode(), only for modes <= CLOCK_EVT_MODE_RESUME.
Viresh Kumar77e32c82015-02-27 17:21:33 +0530137 * - set_state_{shutdown|periodic|oneshot}(), tick_resume().
Viresh Kumarbd624d72015-02-13 08:54:56 +0800138 */
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000139 void (*set_mode)(enum clock_event_mode mode,
140 struct clock_event_device *);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530141 int (*set_state_periodic)(struct clock_event_device *);
142 int (*set_state_oneshot)(struct clock_event_device *);
143 int (*set_state_shutdown)(struct clock_event_device *);
Viresh Kumar554ef382015-02-27 17:21:32 +0530144 int (*tick_resume)(struct clock_event_device *);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800145
146 void (*broadcast)(const struct cpumask *mask);
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200147 void (*suspend)(struct clock_event_device *);
148 void (*resume)(struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000149 unsigned long min_delta_ticks;
150 unsigned long max_delta_ticks;
151
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000152 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800153 int rating;
154 int irq;
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530155 int bound_on;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030156 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800157 struct list_head list;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000158 struct module *owner;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000159} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800160
161/*
162 * Calculate a multiplication factor for scaled math, which is used to convert
163 * nanoseconds based values to clock ticks:
164 *
165 * clock_ticks = (nanoseconds * factor) >> shift.
166 *
167 * div_sc is the rearranged equation to calculate a factor from a given clock
168 * ticks / nanoseconds ratio:
169 *
170 * factor = (clock_ticks << shift) / nanoseconds
171 */
172static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
173 int shift)
174{
175 uint64_t tmp = ((uint64_t)ticks) << shift;
176
177 do_div(tmp, nsec);
178 return (unsigned long) tmp;
179}
180
181/* Clock event layer functions */
Jon Hunter97813f22009-08-18 12:45:11 -0500182extern u64 clockevent_delta2ns(unsigned long latch,
183 struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800184extern void clockevents_register_device(struct clock_event_device *dev);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000185extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800186
Magnus Damme5400322012-05-09 23:39:34 +0900187extern void clockevents_config(struct clock_event_device *dev, u32 freq);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000188extern void clockevents_config_and_register(struct clock_event_device *dev,
189 u32 freq, unsigned long min_delta,
190 unsigned long max_delta);
191
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000192extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
193
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000194static inline void
195clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
196{
197 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
198 freq, minsec);
199}
200
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200201extern void clockevents_suspend(void);
202extern void clockevents_resume(void);
203
Mark Rutland12572db2013-01-14 17:05:21 +0000204#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
Mark Rutland12ad1002013-01-14 17:05:22 +0000205#ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
206extern void tick_broadcast(const struct cpumask *mask);
207#else
208#define tick_broadcast NULL
209#endif
Mark Rutland12572db2013-01-14 17:05:21 +0000210extern int tick_receive_broadcast(void);
211#endif
212
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000213#if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530214extern void tick_setup_hrtimer_broadcast(void);
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000215extern int tick_check_broadcast_expired(void);
216#else
217static inline int tick_check_broadcast_expired(void) { return 0; }
Thomas Gleixnerf1689bb2014-02-07 16:00:46 +0100218static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000219#endif
220
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530221extern int clockevents_notify(unsigned long reason, void *arg);
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200222
Thomas Gleixner9f083b72015-03-25 13:05:19 +0100223#else /* CONFIG_GENERIC_CLOCKEVENTS */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800224
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200225static inline void clockevents_suspend(void) {}
226static inline void clockevents_resume(void) {}
227
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530228static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixner19919222013-03-22 10:48:33 +0100229static inline int tick_check_broadcast_expired(void) { return 0; }
Preeti U Murthy849401b2014-02-09 11:32:22 +0530230static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixner9f083b72015-03-25 13:05:19 +0100231static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800232
233#endif
234
235#endif