blob: a41749543d487bd16463c8103dcd93f4e8a29627 [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
35/* Clock event mode commands */
36enum 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 Kumarbd624d72015-02-13 08:54:56 +080042
43 /* Legacy ->set_mode() callback doesn't support below modes */
Thomas Gleixnerd316c572007-02-16 01:28:00 -080044};
45
Thomas Gleixnerd316c572007-02-16 01:28:00 -080046/*
47 * Clock event features
48 */
49#define CLOCK_EVT_FEAT_PERIODIC 0x000001
50#define CLOCK_EVT_FEAT_ONESHOT 0x000002
Martin Schwidefsky65516f82011-08-23 15:29:43 +020051#define CLOCK_EVT_FEAT_KTIME 0x000004
Thomas Gleixnerd316c572007-02-16 01:28:00 -080052/*
53 * x86(64) specific misfeatures:
54 *
55 * - Clockevent source stops in C3 State and needs broadcast support.
56 * - Local APIC timer is used as a dummy device.
57 */
Martin Schwidefsky65516f82011-08-23 15:29:43 +020058#define CLOCK_EVT_FEAT_C3STOP 0x000008
59#define CLOCK_EVT_FEAT_DUMMY 0x000010
Thomas Gleixnerd316c572007-02-16 01:28:00 -080060
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010061/*
62 * Core shall set the interrupt affinity dynamically in broadcast mode
63 */
64#define CLOCK_EVT_FEAT_DYNIRQ 0x000020
Soren Brinkmann3713c0c2013-09-18 11:48:35 -070065#define CLOCK_EVT_FEAT_PERCPU 0x000040
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010066
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053067/*
68 * Clockevent device is based on a hrtimer for broadcast
69 */
70#define CLOCK_EVT_FEAT_HRTIMER 0x000080
71
Thomas Gleixnerd316c572007-02-16 01:28:00 -080072/**
73 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000074 * @event_handler: Assigned by the framework to be called by the low
75 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020076 * @set_next_event: set next event function using a clocksource delta
77 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000078 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080079 * @max_delta_ns: maximum delta value in ns
80 * @min_delta_ns: minimum delta value in ns
81 * @mult: nanosecond to cycles multiplier
82 * @shift: nanoseconds to cycles divisor (power of two)
Thomas Gleixner847b2f42011-05-18 21:33:41 +000083 * @mode: operating mode assigned by the management code
84 * @features: features
85 * @retries: number of forced programming retries
Viresh Kumarbd624d72015-02-13 08:54:56 +080086 * @set_mode: legacy set mode function, only for modes <= CLOCK_EVT_MODE_RESUME.
87 * @set_mode_periodic: switch mode to periodic, if !set_mode
88 * @set_mode_oneshot: switch mode to oneshot, if !set_mode
89 * @set_mode_shutdown: switch mode to shutdown, if !set_mode
Viresh Kumar554ef382015-02-27 17:21:32 +053090 * @tick_resume: resume clkevt device, if !set_mode
Thomas Gleixner847b2f42011-05-18 21:33:41 +000091 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +000092 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
93 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +000094 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -080095 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -070096 * @irq: IRQ number (only for non CPU local devices)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053097 * @bound_on: Bound on CPU
Sergei Shtylyovce0be122007-05-08 00:31:55 -070098 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -080099 * @list: list head for the management code
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000100 * @owner: module reference
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800101 */
102struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000103 void (*event_handler)(struct clock_event_device *);
104 int (*set_next_event)(unsigned long evt,
105 struct clock_event_device *);
Martin Schwidefsky65516f82011-08-23 15:29:43 +0200106 int (*set_next_ktime)(ktime_t expires,
107 struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000108 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -0500109 u64 max_delta_ns;
110 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +0000111 u32 mult;
112 u32 shift;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000113 enum clock_event_mode mode;
114 unsigned int features;
115 unsigned long retries;
116
Viresh Kumarbd624d72015-02-13 08:54:56 +0800117 /*
118 * Mode transition callback(s): Only one of the two groups should be
119 * defined:
120 * - set_mode(), only for modes <= CLOCK_EVT_MODE_RESUME.
121 * - set_mode_{shutdown|periodic|oneshot|resume}().
122 */
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000123 void (*set_mode)(enum clock_event_mode mode,
124 struct clock_event_device *);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800125 int (*set_mode_periodic)(struct clock_event_device *);
126 int (*set_mode_oneshot)(struct clock_event_device *);
127 int (*set_mode_shutdown)(struct clock_event_device *);
Viresh Kumar554ef382015-02-27 17:21:32 +0530128 int (*tick_resume)(struct clock_event_device *);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800129
130 void (*broadcast)(const struct cpumask *mask);
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200131 void (*suspend)(struct clock_event_device *);
132 void (*resume)(struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000133 unsigned long min_delta_ticks;
134 unsigned long max_delta_ticks;
135
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000136 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800137 int rating;
138 int irq;
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530139 int bound_on;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030140 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800141 struct list_head list;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000142 struct module *owner;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000143} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800144
145/*
146 * Calculate a multiplication factor for scaled math, which is used to convert
147 * nanoseconds based values to clock ticks:
148 *
149 * clock_ticks = (nanoseconds * factor) >> shift.
150 *
151 * div_sc is the rearranged equation to calculate a factor from a given clock
152 * ticks / nanoseconds ratio:
153 *
154 * factor = (clock_ticks << shift) / nanoseconds
155 */
156static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
157 int shift)
158{
159 uint64_t tmp = ((uint64_t)ticks) << shift;
160
161 do_div(tmp, nsec);
162 return (unsigned long) tmp;
163}
164
165/* Clock event layer functions */
Jon Hunter97813f22009-08-18 12:45:11 -0500166extern u64 clockevent_delta2ns(unsigned long latch,
167 struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800168extern void clockevents_register_device(struct clock_event_device *dev);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000169extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800170
Magnus Damme5400322012-05-09 23:39:34 +0900171extern void clockevents_config(struct clock_event_device *dev, u32 freq);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000172extern void clockevents_config_and_register(struct clock_event_device *dev,
173 u32 freq, unsigned long min_delta,
174 unsigned long max_delta);
175
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000176extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
177
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800178extern void clockevents_exchange_device(struct clock_event_device *old,
179 struct clock_event_device *new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800180extern void clockevents_set_mode(struct clock_event_device *dev,
181 enum clock_event_mode mode);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800182extern int clockevents_program_event(struct clock_event_device *dev,
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200183 ktime_t expires, bool force);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800184
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000185extern void clockevents_handle_noop(struct clock_event_device *dev);
186
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000187static inline void
188clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
189{
190 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
191 freq, minsec);
192}
193
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200194extern void clockevents_suspend(void);
195extern void clockevents_resume(void);
196
Mark Rutland12572db2013-01-14 17:05:21 +0000197#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
Mark Rutland12ad1002013-01-14 17:05:22 +0000198#ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
199extern void tick_broadcast(const struct cpumask *mask);
200#else
201#define tick_broadcast NULL
202#endif
Mark Rutland12572db2013-01-14 17:05:21 +0000203extern int tick_receive_broadcast(void);
204#endif
205
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000206#if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530207extern void tick_setup_hrtimer_broadcast(void);
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000208extern int tick_check_broadcast_expired(void);
209#else
210static inline int tick_check_broadcast_expired(void) { return 0; }
Thomas Gleixnerf1689bb2014-02-07 16:00:46 +0100211static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixnereaa907c2013-03-06 11:18:36 +0000212#endif
213
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200214#ifdef CONFIG_GENERIC_CLOCKEVENTS
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530215extern int clockevents_notify(unsigned long reason, void *arg);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800216#else
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530217static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200218#endif
219
220#else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800221
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200222static inline void clockevents_suspend(void) {}
223static inline void clockevents_resume(void) {}
224
Preeti U Murthyda7e6f42014-02-07 13:36:06 +0530225static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
Thomas Gleixner19919222013-03-22 10:48:33 +0100226static inline int tick_check_broadcast_expired(void) { return 0; }
Preeti U Murthy849401b2014-02-09 11:32:22 +0530227static inline void tick_setup_hrtimer_broadcast(void) {};
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800228
229#endif
230
231#endif