blob: a4520e27f8bda52a4ade98618dd7c6944edd5142 [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 Lezcano4dbad8162013-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;
33
34/* Clock event mode commands */
35enum clock_event_mode {
36 CLOCK_EVT_MODE_UNUSED = 0,
37 CLOCK_EVT_MODE_SHUTDOWN,
38 CLOCK_EVT_MODE_PERIODIC,
39 CLOCK_EVT_MODE_ONESHOT,
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070040 CLOCK_EVT_MODE_RESUME,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080041};
42
Thomas Gleixnerd316c572007-02-16 01:28:00 -080043/*
44 * Clock event features
45 */
46#define CLOCK_EVT_FEAT_PERIODIC 0x000001
47#define CLOCK_EVT_FEAT_ONESHOT 0x000002
Martin Schwidefsky65516f82011-08-23 15:29:43 +020048#define CLOCK_EVT_FEAT_KTIME 0x000004
Thomas Gleixnerd316c572007-02-16 01:28:00 -080049/*
50 * x86(64) specific misfeatures:
51 *
52 * - Clockevent source stops in C3 State and needs broadcast support.
53 * - Local APIC timer is used as a dummy device.
54 */
Martin Schwidefsky65516f82011-08-23 15:29:43 +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 */
61#define CLOCK_EVT_FEAT_DYNIRQ 0x000020
62
Preeti U Murthyfe20b8eb2014-02-07 13:36:32 +053063/*
64 * Clockevent device is based on a hrtimer for broadcast
65 */
66#define CLOCK_EVT_FEAT_HRTIMER 0x000080
67
Thomas Gleixnerd316c572007-02-16 01:28:00 -080068/**
69 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000070 * @event_handler: Assigned by the framework to be called by the low
71 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020072 * @set_next_event: set next event function using a clocksource delta
73 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000074 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080075 * @max_delta_ns: maximum delta value in ns
76 * @min_delta_ns: minimum delta value in ns
77 * @mult: nanosecond to cycles multiplier
78 * @shift: nanoseconds to cycles divisor (power of two)
Thomas Gleixner847b2f42011-05-18 21:33:41 +000079 * @mode: operating mode assigned by the management code
80 * @features: features
81 * @retries: number of forced programming retries
82 * @set_mode: set mode function
83 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +000084 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
85 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +000086 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -080087 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -070088 * @irq: IRQ number (only for non CPU local devices)
Preeti U Murthyfe20b8eb2014-02-07 13:36:32 +053089 * @bound_on: Bound on CPU
Sergei Shtylyovce0be122007-05-08 00:31:55 -070090 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -080091 * @list: list head for the management code
Thomas Gleixnerd316c572007-02-16 01:28:00 -080092 */
93struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +000094 void (*event_handler)(struct clock_event_device *);
95 int (*set_next_event)(unsigned long evt,
96 struct clock_event_device *);
Martin Schwidefsky65516f82011-08-23 15:29:43 +020097 int (*set_next_ktime)(ktime_t expires,
98 struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +000099 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -0500100 u64 max_delta_ns;
101 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +0000102 u32 mult;
103 u32 shift;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000104 enum clock_event_mode mode;
105 unsigned int features;
106 unsigned long retries;
107
108 void (*broadcast)(const struct cpumask *mask);
109 void (*set_mode)(enum clock_event_mode mode,
110 struct clock_event_device *);
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200111 void (*suspend)(struct clock_event_device *);
112 void (*resume)(struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000113 unsigned long min_delta_ticks;
114 unsigned long max_delta_ticks;
115
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000116 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800117 int rating;
118 int irq;
Preeti U Murthyfe20b8eb2014-02-07 13:36:32 +0530119 int bound_on;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030120 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800121 struct list_head list;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000122} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800123
124/*
125 * Calculate a multiplication factor for scaled math, which is used to convert
126 * nanoseconds based values to clock ticks:
127 *
128 * clock_ticks = (nanoseconds * factor) >> shift.
129 *
130 * div_sc is the rearranged equation to calculate a factor from a given clock
131 * ticks / nanoseconds ratio:
132 *
133 * factor = (clock_ticks << shift) / nanoseconds
134 */
135static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
136 int shift)
137{
138 uint64_t tmp = ((uint64_t)ticks) << shift;
139
140 do_div(tmp, nsec);
141 return (unsigned long) tmp;
142}
143
144/* Clock event layer functions */
Jon Hunter97813f22009-08-18 12:45:11 -0500145extern u64 clockevent_delta2ns(unsigned long latch,
146 struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800147extern void clockevents_register_device(struct clock_event_device *dev);
148
Magnus Damme5400322012-05-09 23:39:34 +0900149extern void clockevents_config(struct clock_event_device *dev, u32 freq);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000150extern void clockevents_config_and_register(struct clock_event_device *dev,
151 u32 freq, unsigned long min_delta,
152 unsigned long max_delta);
153
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000154extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
155
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800156extern void clockevents_exchange_device(struct clock_event_device *old,
157 struct clock_event_device *new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800158extern void clockevents_set_mode(struct clock_event_device *dev,
159 enum clock_event_mode mode);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800160extern int clockevents_program_event(struct clock_event_device *dev,
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200161 ktime_t expires, bool force);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800162
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000163extern void clockevents_handle_noop(struct clock_event_device *dev);
164
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000165static inline void
166clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
167{
168 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
169 freq, minsec);
170}
171
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200172extern void clockevents_suspend(void);
173extern void clockevents_resume(void);
174
Mark Rutland12572db2013-01-14 17:05:21 +0000175#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
Mark Rutland12ad1002013-01-14 17:05:22 +0000176#ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
177extern void tick_broadcast(const struct cpumask *mask);
178#else
179#define tick_broadcast NULL
180#endif
Mark Rutland12572db2013-01-14 17:05:21 +0000181extern int tick_receive_broadcast(void);
182#endif
183
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000184#if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
Preeti U Murthyfe20b8eb2014-02-07 13:36:32 +0530185extern void tick_setup_hrtimer_broadcast(void);
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000186extern int tick_check_broadcast_expired(void);
187#else
188static inline int tick_check_broadcast_expired(void) { return 0; }
Thomas Gleixnera8db4f02014-02-07 16:00:46 +0100189static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000190#endif
191
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200192#ifdef CONFIG_GENERIC_CLOCKEVENTS
Preeti U Murthy05c26712014-02-07 13:36:06 +0530193extern int clockevents_notify(unsigned long reason, void *arg);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800194#else
Preeti U Murthy05c26712014-02-07 13:36:06 +0530195static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200196#endif
197
198#else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800199
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200200static inline void clockevents_suspend(void) {}
201static inline void clockevents_resume(void) {}
202
Preeti U Murthy05c26712014-02-07 13:36:06 +0530203static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixner19919222013-03-22 10:48:33 +0100204static inline int tick_check_broadcast_expired(void) { return 0; }
Preeti U Murthyeab6f412014-02-09 11:32:22 +0530205static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800206
207#endif
208
209#endif