xref: /linux-6.15/include/linux/clockchips.h (revision e9e8bcb8)
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 
18 struct clock_event_device;
19 
20 /* Clock event mode commands */
21 enum 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 */
30 enum 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 /*
49  * x86(64) specific misfeatures:
50  *
51  * - Clockevent source stops in C3 State and needs broadcast support.
52  * - Local APIC timer is used as a dummy device.
53  */
54 #define CLOCK_EVT_FEAT_C3STOP		0x000004
55 #define CLOCK_EVT_FEAT_DUMMY		0x000008
56 
57 /**
58  * struct clock_event_device - clock event device descriptor
59  * @event_handler:	Assigned by the framework to be called by the low
60  *			level handler of the event source
61  * @set_next_event:	set next event function
62  * @next_event:		local storage for the next event in oneshot mode
63  * @max_delta_ns:	maximum delta value in ns
64  * @min_delta_ns:	minimum delta value in ns
65  * @mult:		nanosecond to cycles multiplier
66  * @shift:		nanoseconds to cycles divisor (power of two)
67  * @mode:		operating mode assigned by the management code
68  * @features:		features
69  * @retries:		number of forced programming retries
70  * @set_mode:		set mode function
71  * @broadcast:		function to broadcast events
72  * @min_delta_ticks:	minimum delta value in ticks stored for reconfiguration
73  * @max_delta_ticks:	maximum delta value in ticks stored for reconfiguration
74  * @name:		ptr to clock event name
75  * @rating:		variable to rate clock event devices
76  * @irq:		IRQ number (only for non CPU local devices)
77  * @cpumask:		cpumask to indicate for which CPUs this device works
78  * @list:		list head for the management code
79  */
80 struct clock_event_device {
81 	void			(*event_handler)(struct clock_event_device *);
82 	int			(*set_next_event)(unsigned long evt,
83 						  struct clock_event_device *);
84 	ktime_t			next_event;
85 	u64			max_delta_ns;
86 	u64			min_delta_ns;
87 	u32			mult;
88 	u32			shift;
89 	enum clock_event_mode	mode;
90 	unsigned int		features;
91 	unsigned long		retries;
92 
93 	void			(*broadcast)(const struct cpumask *mask);
94 	void			(*set_mode)(enum clock_event_mode mode,
95 					    struct clock_event_device *);
96 	unsigned long		min_delta_ticks;
97 	unsigned long		max_delta_ticks;
98 
99 	const char		*name;
100 	int			rating;
101 	int			irq;
102 	const struct cpumask	*cpumask;
103 	struct list_head	list;
104 } ____cacheline_aligned;
105 
106 /*
107  * Calculate a multiplication factor for scaled math, which is used to convert
108  * nanoseconds based values to clock ticks:
109  *
110  * clock_ticks = (nanoseconds * factor) >> shift.
111  *
112  * div_sc is the rearranged equation to calculate a factor from a given clock
113  * ticks / nanoseconds ratio:
114  *
115  * factor = (clock_ticks << shift) / nanoseconds
116  */
117 static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
118 				   int shift)
119 {
120 	uint64_t tmp = ((uint64_t)ticks) << shift;
121 
122 	do_div(tmp, nsec);
123 	return (unsigned long) tmp;
124 }
125 
126 /* Clock event layer functions */
127 extern u64 clockevent_delta2ns(unsigned long latch,
128 			       struct clock_event_device *evt);
129 extern void clockevents_register_device(struct clock_event_device *dev);
130 
131 extern void clockevents_config_and_register(struct clock_event_device *dev,
132 					    u32 freq, unsigned long min_delta,
133 					    unsigned long max_delta);
134 
135 extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
136 
137 extern void clockevents_exchange_device(struct clock_event_device *old,
138 					struct clock_event_device *new);
139 extern void clockevents_set_mode(struct clock_event_device *dev,
140 				 enum clock_event_mode mode);
141 extern int clockevents_register_notifier(struct notifier_block *nb);
142 extern int clockevents_program_event(struct clock_event_device *dev,
143 				     ktime_t expires, ktime_t now);
144 
145 extern void clockevents_handle_noop(struct clock_event_device *dev);
146 
147 static inline void
148 clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
149 {
150 	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
151 				      freq, minsec);
152 }
153 
154 #ifdef CONFIG_GENERIC_CLOCKEVENTS
155 extern void clockevents_notify(unsigned long reason, void *arg);
156 #else
157 # define clockevents_notify(reason, arg) do { } while (0)
158 #endif
159 
160 #else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
161 
162 #define clockevents_notify(reason, arg) do { } while (0)
163 
164 #endif
165 
166 #endif
167