blob: 20b84465a09d32d0f8618bffe102b40d2a9a2491 [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
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +020011#ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD
Thomas Gleixnerd316c572007-02-16 01:28:00 -080012
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,
Thomas Gleixner18de5bc2007-07-21 04:37:34 -070026 CLOCK_EVT_MODE_RESUME,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080027};
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,
Thomas Gleixner1595f452007-10-14 22:57:45 +020034 CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080035 CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
36 CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
37 CLOCK_EVT_NOTIFY_SUSPEND,
38 CLOCK_EVT_NOTIFY_RESUME,
Sebastien Dugue94df7de2008-12-01 14:09:07 +010039 CLOCK_EVT_NOTIFY_CPU_DYING,
Thomas Gleixnerd316c572007-02-16 01:28:00 -080040 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
Martin Schwidefsky65516f82011-08-23 15:29:43 +020048#define CLOCK_EVT_FEAT_KTIME 0x000004
Thomas Gleixnerd316c572007-02-16 01:28:00 -080049/*
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 */
Martin Schwidefsky65516f82011-08-23 15:29:43 +020055#define CLOCK_EVT_FEAT_C3STOP 0x000008
56#define CLOCK_EVT_FEAT_DUMMY 0x000010
Thomas Gleixnerd316c572007-02-16 01:28:00 -080057
Daniel Lezcano38b17152013-03-02 11:10:11 +010058/*
59 * Core shall set the interrupt affinity dynamically in broadcast mode
60 */
61#define CLOCK_EVT_FEAT_DYNIRQ 0x000020
62
Thomas Gleixnerd316c572007-02-16 01:28:00 -080063/**
64 * struct clock_event_device - clock event device descriptor
Thomas Gleixner847b2f42011-05-18 21:33:41 +000065 * @event_handler: Assigned by the framework to be called by the low
66 * level handler of the event source
Martin Schwidefsky65516f82011-08-23 15:29:43 +020067 * @set_next_event: set next event function using a clocksource delta
68 * @set_next_ktime: set next event function using a direct ktime value
Thomas Gleixner847b2f42011-05-18 21:33:41 +000069 * @next_event: local storage for the next event in oneshot mode
Thomas Gleixnerd316c572007-02-16 01:28:00 -080070 * @max_delta_ns: maximum delta value in ns
71 * @min_delta_ns: minimum delta value in ns
72 * @mult: nanosecond to cycles multiplier
73 * @shift: nanoseconds to cycles divisor (power of two)
Thomas Gleixner847b2f42011-05-18 21:33:41 +000074 * @mode: operating mode assigned by the management code
75 * @features: features
76 * @retries: number of forced programming retries
77 * @set_mode: set mode function
78 * @broadcast: function to broadcast events
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +000079 * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
80 * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
Thomas Gleixner847b2f42011-05-18 21:33:41 +000081 * @name: ptr to clock event name
Thomas Gleixnerd316c572007-02-16 01:28:00 -080082 * @rating: variable to rate clock event devices
Sergei Shtylyovce0be122007-05-08 00:31:55 -070083 * @irq: IRQ number (only for non CPU local devices)
84 * @cpumask: cpumask to indicate for which CPUs this device works
Thomas Gleixnerd316c572007-02-16 01:28:00 -080085 * @list: list head for the management code
Thomas Gleixnerd316c572007-02-16 01:28:00 -080086 */
87struct clock_event_device {
Thomas Gleixner847b2f42011-05-18 21:33:41 +000088 void (*event_handler)(struct clock_event_device *);
89 int (*set_next_event)(unsigned long evt,
90 struct clock_event_device *);
Martin Schwidefsky65516f82011-08-23 15:29:43 +020091 int (*set_next_ktime)(ktime_t expires,
92 struct clock_event_device *);
Thomas Gleixner847b2f42011-05-18 21:33:41 +000093 ktime_t next_event;
Jon Hunter97813f22009-08-18 12:45:11 -050094 u64 max_delta_ns;
95 u64 min_delta_ns;
Thomas Gleixner23af3682009-11-11 14:05:25 +000096 u32 mult;
97 u32 shift;
Thomas Gleixner847b2f42011-05-18 21:33:41 +000098 enum clock_event_mode mode;
99 unsigned int features;
100 unsigned long retries;
101
102 void (*broadcast)(const struct cpumask *mask);
103 void (*set_mode)(enum clock_event_mode mode,
104 struct clock_event_device *);
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000105 unsigned long min_delta_ticks;
106 unsigned long max_delta_ticks;
107
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000108 const char *name;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800109 int rating;
110 int irq;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030111 const struct cpumask *cpumask;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800112 struct list_head list;
Thomas Gleixner847b2f42011-05-18 21:33:41 +0000113} ____cacheline_aligned;
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800114
115/*
116 * Calculate a multiplication factor for scaled math, which is used to convert
117 * nanoseconds based values to clock ticks:
118 *
119 * clock_ticks = (nanoseconds * factor) >> shift.
120 *
121 * div_sc is the rearranged equation to calculate a factor from a given clock
122 * ticks / nanoseconds ratio:
123 *
124 * factor = (clock_ticks << shift) / nanoseconds
125 */
126static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
127 int shift)
128{
129 uint64_t tmp = ((uint64_t)ticks) << shift;
130
131 do_div(tmp, nsec);
132 return (unsigned long) tmp;
133}
134
135/* Clock event layer functions */
Jon Hunter97813f22009-08-18 12:45:11 -0500136extern u64 clockevent_delta2ns(unsigned long latch,
137 struct clock_event_device *evt);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800138extern void clockevents_register_device(struct clock_event_device *dev);
139
Thomas Gleixner57f0fcb2011-05-18 21:33:41 +0000140extern void clockevents_config_and_register(struct clock_event_device *dev,
141 u32 freq, unsigned long min_delta,
142 unsigned long max_delta);
143
Thomas Gleixner80b816b2011-05-18 21:33:42 +0000144extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
145
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800146extern void clockevents_exchange_device(struct clock_event_device *old,
147 struct clock_event_device *new);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800148extern void clockevents_set_mode(struct clock_event_device *dev,
149 enum clock_event_mode mode);
150extern int clockevents_register_notifier(struct notifier_block *nb);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800151extern int clockevents_program_event(struct clock_event_device *dev,
Martin Schwidefskyd1748302011-08-23 15:29:42 +0200152 ktime_t expires, bool force);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800153
Venkatesh Pallipadi7c1e7682008-09-03 21:36:50 +0000154extern void clockevents_handle_noop(struct clock_event_device *dev);
155
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000156static inline void
157clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
158{
159 return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
160 freq, minsec);
161}
162
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200163#ifdef CONFIG_GENERIC_CLOCKEVENTS
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800164extern void clockevents_notify(unsigned long reason, void *arg);
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800165#else
Thomas Gleixnerde68d9b2007-10-12 23:04:05 +0200166# define clockevents_notify(reason, arg) do { } while (0)
167#endif
168
169#else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800170
Thomas Gleixnerd316c572007-02-16 01:28:00 -0800171#define clockevents_notify(reason, arg) do { } while (0)
172
173#endif
174
175#endif