Linux Audio

Check our new training course

Loading...
v5.9
  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.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 * @retries:		number of forced programming retries
 84 * @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
 89 * @broadcast:		function to broadcast events
 90 * @min_delta_ticks:	minimum delta value in ticks stored for reconfiguration
 91 * @max_delta_ticks:	maximum delta value in ticks stored for reconfiguration
 92 * @name:		ptr to clock event name
 93 * @rating:		variable to rate clock event devices
 94 * @irq:		IRQ number (only for non CPU local devices)
 95 * @bound_on:		Bound on CPU
 96 * @cpumask:		cpumask to indicate for which CPUs this device works
 97 * @list:		list head for the management code
 98 * @owner:		module reference
 99 */
100struct clock_event_device {
101	void			(*event_handler)(struct clock_event_device *);
102	int			(*set_next_event)(unsigned long evt, struct clock_event_device *);
103	int			(*set_next_ktime)(ktime_t expires, struct clock_event_device *);
 
 
104	ktime_t			next_event;
105	u64			max_delta_ns;
106	u64			min_delta_ns;
107	u32			mult;
108	u32			shift;
109	enum clock_event_state	state_use_accessors;
110	unsigned int		features;
111	unsigned long		retries;
112
113	int			(*set_state_periodic)(struct clock_event_device *);
114	int			(*set_state_oneshot)(struct clock_event_device *);
115	int			(*set_state_oneshot_stopped)(struct clock_event_device *);
116	int			(*set_state_shutdown)(struct clock_event_device *);
117	int			(*tick_resume)(struct clock_event_device *);
118
119	void			(*broadcast)(const struct cpumask *mask);
120	void			(*suspend)(struct clock_event_device *);
121	void			(*resume)(struct clock_event_device *);
122	unsigned long		min_delta_ticks;
123	unsigned long		max_delta_ticks;
124
125	const char		*name;
126	int			rating;
127	int			irq;
128	int			bound_on;
129	const struct cpumask	*cpumask;
130	struct list_head	list;
131	struct module		*owner;
132} ____cacheline_aligned;
133
134/* Helpers to verify state of a clockevent device */
135static inline bool clockevent_state_detached(struct clock_event_device *dev)
136{
137	return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED;
138}
139
140static inline bool clockevent_state_shutdown(struct clock_event_device *dev)
141{
142	return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN;
143}
144
145static inline bool clockevent_state_periodic(struct clock_event_device *dev)
146{
147	return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC;
148}
149
150static inline bool clockevent_state_oneshot(struct clock_event_device *dev)
151{
152	return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT;
153}
154
155static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev)
156{
157	return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED;
158}
159
160/*
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 */
171static inline unsigned long
172div_sc(unsigned long ticks, unsigned long nsec, int shift)
173{
174	u64 tmp = ((u64)ticks) << shift;
175
176	do_div(tmp, nsec);
177
178	return (unsigned long) tmp;
179}
180
181/* Clock event layer functions */
182extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
 
183extern void clockevents_register_device(struct clock_event_device *dev);
184extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
185
 
186extern void clockevents_config_and_register(struct clock_event_device *dev,
187					    u32 freq, unsigned long min_delta,
188					    unsigned long max_delta);
189
190extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
191
 
 
 
 
 
 
 
 
 
 
192static inline void
193clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 maxsec)
194{
195	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, maxsec);
 
196}
197
198extern void clockevents_suspend(void);
199extern void clockevents_resume(void);
 
 
 
 
 
200
201# ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
202#  ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
203extern void tick_broadcast(const struct cpumask *mask);
204#  else
205#   define tick_broadcast	NULL
206#  endif
207extern int tick_receive_broadcast(void);
208# endif
209
210# if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
211extern void tick_setup_hrtimer_broadcast(void);
212extern int tick_check_broadcast_expired(void);
213# else
214static inline int tick_check_broadcast_expired(void) { return 0; }
215static inline void tick_setup_hrtimer_broadcast(void) { }
216# endif
217
218#else /* !CONFIG_GENERIC_CLOCKEVENTS: */
219
220static inline void clockevents_suspend(void) { }
221static inline void clockevents_resume(void) { }
222static inline int tick_check_broadcast_expired(void) { return 0; }
223static inline void tick_setup_hrtimer_broadcast(void) { }
224
225#endif /* !CONFIG_GENERIC_CLOCKEVENTS */
226
227#endif /* _LINUX_CLOCKCHIPS_H */
v3.5.6
 
  1/*  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
 11#ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD
 12
 13#include <linux/clocksource.h>
 14#include <linux/cpumask.h>
 15#include <linux/ktime.h>
 16#include <linux/notifier.h>
 17
 18struct clock_event_device;
 
 19
 20/* Clock event mode commands */
 21enum clock_event_mode {
 22	CLOCK_EVT_MODE_UNUSED = 0,
 23	CLOCK_EVT_MODE_SHUTDOWN,
 24	CLOCK_EVT_MODE_PERIODIC,
 25	CLOCK_EVT_MODE_ONESHOT,
 26	CLOCK_EVT_MODE_RESUME,
 27};
 28
 29/* Clock event notification values */
 30enum clock_event_nofitiers {
 31	CLOCK_EVT_NOTIFY_ADD,
 32	CLOCK_EVT_NOTIFY_BROADCAST_ON,
 33	CLOCK_EVT_NOTIFY_BROADCAST_OFF,
 34	CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
 35	CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
 36	CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
 37	CLOCK_EVT_NOTIFY_SUSPEND,
 38	CLOCK_EVT_NOTIFY_RESUME,
 39	CLOCK_EVT_NOTIFY_CPU_DYING,
 40	CLOCK_EVT_NOTIFY_CPU_DEAD,
 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 * 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 */
 55#define CLOCK_EVT_FEAT_C3STOP		0x000008
 56#define CLOCK_EVT_FEAT_DUMMY		0x000010
 
 
 
 
 
 
 
 
 
 
 
 57
 58/**
 59 * struct clock_event_device - clock event device descriptor
 60 * @event_handler:	Assigned by the framework to be called by the low
 61 *			level handler of the event source
 62 * @set_next_event:	set next event function using a clocksource delta
 63 * @set_next_ktime:	set next event function using a direct ktime value
 64 * @next_event:		local storage for the next event in oneshot mode
 65 * @max_delta_ns:	maximum delta value in ns
 66 * @min_delta_ns:	minimum delta value in ns
 67 * @mult:		nanosecond to cycles multiplier
 68 * @shift:		nanoseconds to cycles divisor (power of two)
 69 * @mode:		operating mode assigned by the management code
 70 * @features:		features
 71 * @retries:		number of forced programming retries
 72 * @set_mode:		set mode function
 
 
 
 
 73 * @broadcast:		function to broadcast events
 74 * @min_delta_ticks:	minimum delta value in ticks stored for reconfiguration
 75 * @max_delta_ticks:	maximum delta value in ticks stored for reconfiguration
 76 * @name:		ptr to clock event name
 77 * @rating:		variable to rate clock event devices
 78 * @irq:		IRQ number (only for non CPU local devices)
 
 79 * @cpumask:		cpumask to indicate for which CPUs this device works
 80 * @list:		list head for the management code
 
 81 */
 82struct clock_event_device {
 83	void			(*event_handler)(struct clock_event_device *);
 84	int			(*set_next_event)(unsigned long evt,
 85						  struct clock_event_device *);
 86	int			(*set_next_ktime)(ktime_t expires,
 87						  struct clock_event_device *);
 88	ktime_t			next_event;
 89	u64			max_delta_ns;
 90	u64			min_delta_ns;
 91	u32			mult;
 92	u32			shift;
 93	enum clock_event_mode	mode;
 94	unsigned int		features;
 95	unsigned long		retries;
 96
 
 
 
 
 
 
 97	void			(*broadcast)(const struct cpumask *mask);
 98	void			(*set_mode)(enum clock_event_mode mode,
 99					    struct clock_event_device *);
100	unsigned long		min_delta_ticks;
101	unsigned long		max_delta_ticks;
102
103	const char		*name;
104	int			rating;
105	int			irq;
 
106	const struct cpumask	*cpumask;
107	struct list_head	list;
 
108} ____cacheline_aligned;
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110/*
111 * Calculate a multiplication factor for scaled math, which is used to convert
112 * nanoseconds based values to clock ticks:
113 *
114 * clock_ticks = (nanoseconds * factor) >> shift.
115 *
116 * div_sc is the rearranged equation to calculate a factor from a given clock
117 * ticks / nanoseconds ratio:
118 *
119 * factor = (clock_ticks << shift) / nanoseconds
120 */
121static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
122				   int shift)
123{
124	uint64_t tmp = ((uint64_t)ticks) << shift;
125
126	do_div(tmp, nsec);
 
127	return (unsigned long) tmp;
128}
129
130/* Clock event layer functions */
131extern u64 clockevent_delta2ns(unsigned long latch,
132			       struct clock_event_device *evt);
133extern void clockevents_register_device(struct clock_event_device *dev);
 
134
135extern void clockevents_config(struct clock_event_device *dev, u32 freq);
136extern void clockevents_config_and_register(struct clock_event_device *dev,
137					    u32 freq, unsigned long min_delta,
138					    unsigned long max_delta);
139
140extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
141
142extern void clockevents_exchange_device(struct clock_event_device *old,
143					struct clock_event_device *new);
144extern void clockevents_set_mode(struct clock_event_device *dev,
145				 enum clock_event_mode mode);
146extern int clockevents_register_notifier(struct notifier_block *nb);
147extern int clockevents_program_event(struct clock_event_device *dev,
148				     ktime_t expires, bool force);
149
150extern void clockevents_handle_noop(struct clock_event_device *dev);
151
152static inline void
153clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
154{
155	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
156				      freq, minsec);
157}
158
159#ifdef CONFIG_GENERIC_CLOCKEVENTS
160extern void clockevents_notify(unsigned long reason, void *arg);
161#else
162# define clockevents_notify(reason, arg) do { } while (0)
163#endif
164
165#else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
166
167#define clockevents_notify(reason, arg) do { } while (0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
169#endif
170
171#endif