Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2/* 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
12#ifdef CONFIG_GENERIC_CLOCKEVENTS
13
14# include <linux/clocksource.h>
15# include <linux/cpumask_types.h>
16# include <linux/ktime.h>
17# include <linux/notifier.h>
18
19struct clock_event_device;
20struct module;
21
22/*
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.
32 * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily
33 * stopped.
34 */
35enum clock_event_state {
36 CLOCK_EVT_STATE_DETACHED,
37 CLOCK_EVT_STATE_SHUTDOWN,
38 CLOCK_EVT_STATE_PERIODIC,
39 CLOCK_EVT_STATE_ONESHOT,
40 CLOCK_EVT_STATE_ONESHOT_STOPPED,
41};
42
43/*
44 * Clock event features
45 */
46# define CLOCK_EVT_FEAT_PERIODIC 0x000001
47# define CLOCK_EVT_FEAT_ONESHOT 0x000002
48# define CLOCK_EVT_FEAT_KTIME 0x000004
49
50/*
51 * x86(64) specific (mis)features:
52 *
53 * - Clockevent source stops in C3 State and needs broadcast support.
54 * - Local APIC timer is used as a dummy device.
55 */
56# define CLOCK_EVT_FEAT_C3STOP 0x000008
57# define CLOCK_EVT_FEAT_DUMMY 0x000010
58
59/*
60 * Core shall set the interrupt affinity dynamically in broadcast mode
61 */
62# define CLOCK_EVT_FEAT_DYNIRQ 0x000020
63# define CLOCK_EVT_FEAT_PERCPU 0x000040
64
65/*
66 * Clockevent device is based on a hrtimer for broadcast
67 */
68# define CLOCK_EVT_FEAT_HRTIMER 0x000080
69
70/**
71 * struct clock_event_device - clock event device descriptor
72 * @event_handler: Assigned by the framework to be called by the low
73 * level handler of the event source
74 * @set_next_event: set next event function using a clocksource delta
75 * @set_next_ktime: set next event function using a direct ktime value
76 * @next_event: local storage for the next event in oneshot mode
77 * @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)
81 * @state_use_accessors:current state of the device, assigned by the core code
82 * @features: features
83 * @next_event_forced: True if the last programming was a forced event
84 * @retries: number of forced programming retries
85 * @set_state_periodic: switch state to periodic
86 * @set_state_oneshot: switch state to oneshot
87 * @set_state_oneshot_stopped: switch state to oneshot_stopped
88 * @set_state_shutdown: switch state to shutdown
89 * @tick_resume: resume clkevt device
90 * @broadcast: function to broadcast events
91 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
92 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
93 * @name: ptr to clock event name
94 * @rating: variable to rate clock event devices
95 * @irq: IRQ number (only for non CPU local devices)
96 * @bound_on: Bound on CPU
97 * @cpumask: cpumask to indicate for which CPUs this device works
98 * @list: list head for the management code
99 * @owner: module reference
100 */
101struct clock_event_device {
102 void (*event_handler)(struct clock_event_device *);
103 int (*set_next_event)(unsigned long evt, struct clock_event_device *);
104 int (*set_next_ktime)(ktime_t expires, struct clock_event_device *);
105 ktime_t next_event;
106 u64 max_delta_ns;
107 u64 min_delta_ns;
108 u32 mult;
109 u32 shift;
110 enum clock_event_state state_use_accessors;
111 unsigned int features;
112 unsigned int next_event_forced;
113 unsigned long retries;
114
115 int (*set_state_periodic)(struct clock_event_device *);
116 int (*set_state_oneshot)(struct clock_event_device *);
117 int (*set_state_oneshot_stopped)(struct clock_event_device *);
118 int (*set_state_shutdown)(struct clock_event_device *);
119 int (*tick_resume)(struct clock_event_device *);
120
121 void (*broadcast)(const struct cpumask *mask);
122 void (*suspend)(struct clock_event_device *);
123 void (*resume)(struct clock_event_device *);
124 unsigned long min_delta_ticks;
125 unsigned long max_delta_ticks;
126
127 const char *name;
128 int rating;
129 int irq;
130 int bound_on;
131 const struct cpumask *cpumask;
132 struct list_head list;
133 struct module *owner;
134} ____cacheline_aligned;
135
136/* Helpers to verify state of a clockevent device */
137static inline bool clockevent_state_detached(struct clock_event_device *dev)
138{
139 return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED;
140}
141
142static inline bool clockevent_state_shutdown(struct clock_event_device *dev)
143{
144 return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN;
145}
146
147static inline bool clockevent_state_periodic(struct clock_event_device *dev)
148{
149 return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC;
150}
151
152static inline bool clockevent_state_oneshot(struct clock_event_device *dev)
153{
154 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT;
155}
156
157static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev)
158{
159 return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED;
160}
161
162/*
163 * Calculate a multiplication factor for scaled math, which is used to convert
164 * nanoseconds based values to clock ticks:
165 *
166 * clock_ticks = (nanoseconds * factor) >> shift.
167 *
168 * div_sc is the rearranged equation to calculate a factor from a given clock
169 * ticks / nanoseconds ratio:
170 *
171 * factor = (clock_ticks << shift) / nanoseconds
172 */
173static inline unsigned long
174div_sc(unsigned long ticks, unsigned long nsec, int shift)
175{
176 u64 tmp = ((u64)ticks) << shift;
177
178 do_div(tmp, nsec);
179
180 return (unsigned long) tmp;
181}
182
183/* Clock event layer functions */
184extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
185extern void clockevents_register_device(struct clock_event_device *dev);
186extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
187
188extern void clockevents_config_and_register(struct clock_event_device *dev,
189 u32 freq, unsigned long min_delta,
190 unsigned long max_delta);
191
192extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
193
194static inline void
195clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 maxsec)
196{
197 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, maxsec);
198}
199
200extern void clockevents_suspend(void);
201extern void clockevents_resume(void);
202
203# ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
204# ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
205extern void tick_broadcast(const struct cpumask *mask);
206# else
207# define tick_broadcast NULL
208# endif
209extern int tick_receive_broadcast(void);
210# endif
211
212# if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
213extern void tick_setup_hrtimer_broadcast(void);
214extern int tick_check_broadcast_expired(void);
215# else
216static __always_inline int tick_check_broadcast_expired(void) { return 0; }
217static inline void tick_setup_hrtimer_broadcast(void) { }
218# endif
219
220#else /* !CONFIG_GENERIC_CLOCKEVENTS: */
221
222static inline void clockevents_suspend(void) { }
223static inline void clockevents_resume(void) { }
224static __always_inline int tick_check_broadcast_expired(void) { return 0; }
225static inline void tick_setup_hrtimer_broadcast(void) { }
226
227#endif /* !CONFIG_GENERIC_CLOCKEVENTS */
228
229#endif /* _LINUX_CLOCKCHIPS_H */