blob: 8ae9a95ebf5b5053e576a5995fa4d58b57f20bb5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Gleixnerd316c572007-02-16 01:28:00 -08002/* linux/include/linux/clockchips.h
3 *
4 * This file contains the structure definitions for clockchips.
5 *
6 * If you are not a clockchip, or the time of day code, you should
7 * not be including this file!
8 */
9#ifndef _LINUX_CLOCKCHIPS_H
10#define _LINUX_CLOCKCHIPS_H
11
Thomas Gleixner9f083b72015-03-25 13:05:19 +010012#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -080013
Ingo Molnar9eed56e2015-04-02 11:26:23 +020014# include <linux/clocksource.h>
15# include <linux/cpumask.h>
16# include <linux/ktime.h>
17# include <linux/notifier.h>
Thomas Gleixnerd316c572007-02-16 01:28:00 -080018
19struct clock_event_device;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000020struct module;
Thomas Gleixnerd316c572007-02-16 01:28:00 -080021
Viresh Kumar77e32c82015-02-27 17:21:33 +053022/*
23 * Possible states of a clock event device.
24 *
25 * DETACHED: Device is not used by clockevents core. Initial state or can be
26 * reached from SHUTDOWN.
27 * SHUTDOWN: Device is powered-off. Can be reached from PERIODIC or ONESHOT.
28 * PERIODIC: Device is programmed to generate events periodically. Can be
29 * reached from DETACHED or SHUTDOWN.
30 * ONESHOT: Device is programmed to generate event only once. Can be reached
31 * from DETACHED or SHUTDOWN.
Viresh Kumar8fff52f2015-04-03 09:04:04 +053032 * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily
33 * stopped.
Viresh Kumar77e32c82015-02-27 17:21:33 +053034 */
35enum clock_event_state {
Ingo Molnar9eed56e2015-04-02 11:26:23 +020036 CLOCK_EVT_STATE_DETACHED,
Viresh Kumar77e32c82015-02-27 17:21:33 +053037 CLOCK_EVT_STATE_SHUTDOWN,
38 CLOCK_EVT_STATE_PERIODIC,
39 CLOCK_EVT_STATE_ONESHOT,
Viresh Kumar8fff52f2015-04-03 09:04:04 +053040 CLOCK_EVT_STATE_ONESHOT_STOPPED,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080041};
42
Thomas Gleixnerd316c572007-02-16 01:28:00 -080043/*
44 * Clock event features
45 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020046# define CLOCK_EVT_FEAT_PERIODIC 0x000001
47# define CLOCK_EVT_FEAT_ONESHOT 0x000002
48# define CLOCK_EVT_FEAT_KTIME 0x000004
49
Thomas Gleixnerd316c572007-02-16 01:28:00 -080050/*
Ingo Molnar9eed56e2015-04-02 11:26:23 +020051 * x86(64) specific (mis)features:
Thomas Gleixnerd316c572007-02-16 01:28:00 -080052 *
53 * - Clockevent source stops in C3 State and needs broadcast support.
54 * - Local APIC timer is used as a dummy device.
55 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020056# define CLOCK_EVT_FEAT_C3STOP 0x000008
57# define CLOCK_EVT_FEAT_DUMMY 0x000010
Thomas Gleixnerd316c572007-02-16 01:28:00 -080058
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010059/*
60 * Core shall set the interrupt affinity dynamically in broadcast mode
61 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020062# define CLOCK_EVT_FEAT_DYNIRQ 0x000020
63# define CLOCK_EVT_FEAT_PERCPU 0x000040
Daniel Lezcanod2348fb2013-03-02 11:10:11 +010064
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053065/*
66 * Clockevent device is based on a hrtimer for broadcast
67 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +020068# define CLOCK_EVT_FEAT_HRTIMER 0x000080
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053069
Thomas Gleixnerd316c572007-02-16 01:28:00 -080070/**
71 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000072 * @event_handler: Assigned by the framework to be called by the low
73 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020074 * @set_next_event: set next event function using a clocksource delta
75 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000076 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080077 * @max_delta_ns: maximum delta value in ns
78 * @min_delta_ns: minimum delta value in ns
79 * @mult: nanosecond to cycles multiplier
80 * @shift: nanoseconds to cycles divisor (power of two)
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +020081 * @state_use_accessors:current state of the device, assigned by the core code
Thomas Gleixner847b2f42011-05-18 21:33:41 +000082 * @features: features
83 * @retries: number of forced programming retries
Viresh Kumareef76352015-09-11 09:34:26 +053084 * @set_state_periodic: switch state to periodic
85 * @set_state_oneshot: switch state to oneshot
86 * @set_state_oneshot_stopped: switch state to oneshot_stopped
87 * @set_state_shutdown: switch state to shutdown
88 * @tick_resume: resume clkevt device
Thomas Gleixner847b2f42011-05-18 21:33:41 +000089 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +000090 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
91 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +000092 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -080093 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -070094 * @irq: IRQ number (only for non CPU local devices)
Preeti U Murthy5d1638a2014-02-07 13:36:32 +053095 * @bound_on: Bound on CPU
Sergei Shtylyovce0be122007-05-08 00:31:55 -070096 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -080097 * @list: list head for the management code
Thomas Gleixnerccf33d62013-04-25 20:31:49 +000098 * @owner: module reference
Thomas Gleixnerd316c572007-02-16 01:28:00 -080099 */
100struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000101 void (*event_handler)(struct clock_event_device *);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200102 int (*set_next_event)(unsigned long evt, struct clock_event_device *);
103 int (*set_next_ktime)(ktime_t expires, struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000104 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -0500105 u64 max_delta_ns;
106 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +0000107 u32 mult;
108 u32 shift;
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200109 enum clock_event_state state_use_accessors;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000110 unsigned int features;
111 unsigned long retries;
112
Viresh Kumar77e32c82015-02-27 17:21:33 +0530113 int (*set_state_periodic)(struct clock_event_device *);
114 int (*set_state_oneshot)(struct clock_event_device *);
Viresh Kumar8fff52f2015-04-03 09:04:04 +0530115 int (*set_state_oneshot_stopped)(struct clock_event_device *);
Viresh Kumar77e32c82015-02-27 17:21:33 +0530116 int (*set_state_shutdown)(struct clock_event_device *);
Viresh Kumar554ef382015-02-27 17:21:32 +0530117 int (*tick_resume)(struct clock_event_device *);
Viresh Kumarbd624d72015-02-13 08:54:56 +0800118
119 void (*broadcast)(const struct cpumask *mask);
Rafael J. Wysockiadc78e62012-08-06 01:40:41 +0200120 void (*suspend)(struct clock_event_device *);
121 void (*resume)(struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000122 unsigned long min_delta_ticks;
123 unsigned long max_delta_ticks;
124
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000125 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800126 int rating;
127 int irq;
Preeti U Murthy5d1638a2014-02-07 13:36:32 +0530128 int bound_on;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030129 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800130 struct list_head list;
Thomas Gleixnerccf33d62013-04-25 20:31:49 +0000131 struct module *owner;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000132} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800133
Viresh Kumar3434d232015-05-21 13:33:45 +0530134/* Helpers to verify state of a clockevent device */
135static inline bool clockevent_state_detached(struct clock_event_device *dev)
136{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200137 return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED;
Viresh Kumar3434d232015-05-21 13:33:45 +0530138}
139
140static inline bool clockevent_state_shutdown(struct clock_event_device *dev)
141{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200142 return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN;
Viresh Kumar3434d232015-05-21 13:33:45 +0530143}
144
145static inline bool clockevent_state_periodic(struct clock_event_device *dev)
146{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200147 return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC;
Viresh Kumar3434d232015-05-21 13:33:45 +0530148}
149
150static inline bool clockevent_state_oneshot(struct clock_event_device *dev)
151{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200152 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT;
Viresh Kumar3434d232015-05-21 13:33:45 +0530153}
154
155static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev)
156{
Thomas Gleixnerbe3ef762015-06-02 14:30:11 +0200157 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED;
Viresh Kumar3434d232015-05-21 13:33:45 +0530158}
159
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800160/*
161 * Calculate a multiplication factor for scaled math, which is used to convert
162 * nanoseconds based values to clock ticks:
163 *
164 * clock_ticks = (nanoseconds * factor) >> shift.
165 *
166 * div_sc is the rearranged equation to calculate a factor from a given clock
167 * ticks / nanoseconds ratio:
168 *
169 * factor = (clock_ticks << shift) / nanoseconds
170 */
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200171static inline unsigned long
172div_sc(unsigned long ticks, unsigned long nsec, int shift)
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800173{
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200174 u64 tmp = ((u64)ticks) << shift;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800175
176 do_div(tmp, nsec);
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200177
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800178 return (unsigned long) tmp;
179}
180
181/* Clock event layer functions */
Ingo Molnar9eed56e2015-04-02 11:26:23 +0200182extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800183extern void clockevents_register_device(struct clock_event_device *dev);
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000184extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800185
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 */