~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/ia64/kernel/time.c

Version: ~ [ linux-5.2-rc1 ] ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * linux/arch/ia64/kernel/time.c
  3  *
  4  * Copyright (C) 1998-2003 Hewlett-Packard Co
  5  *      Stephane Eranian <eranian@hpl.hp.com>
  6  *      David Mosberger <davidm@hpl.hp.com>
  7  * Copyright (C) 1999 Don Dugger <don.dugger@intel.com>
  8  * Copyright (C) 1999-2000 VA Linux Systems
  9  * Copyright (C) 1999-2000 Walt Drummond <drummond@valinux.com>
 10  */
 11 #include <linux/config.h>
 12 
 13 #include <linux/init.h>
 14 #include <linux/kernel.h>
 15 #include <linux/module.h>
 16 #include <linux/profile.h>
 17 #include <linux/sched.h>
 18 #include <linux/time.h>
 19 #include <linux/interrupt.h>
 20 #include <linux/efi.h>
 21 #include <linux/profile.h>
 22 #include <linux/timex.h>
 23 
 24 #include <asm/delay.h>
 25 #include <asm/hw_irq.h>
 26 #include <asm/ptrace.h>
 27 #include <asm/sal.h>
 28 #include <asm/sections.h>
 29 #include <asm/system.h>
 30 
 31 extern unsigned long wall_jiffies;
 32 
 33 u64 jiffies_64 = INITIAL_JIFFIES;
 34 
 35 EXPORT_SYMBOL(jiffies_64);
 36 
 37 #define TIME_KEEPER_ID  0       /* smp_processor_id() of time-keeper */
 38 
 39 #ifdef CONFIG_IA64_DEBUG_IRQ
 40 
 41 unsigned long last_cli_ip;
 42 
 43 #endif
 44 
 45 unsigned long long
 46 sched_clock (void)
 47 {
 48         unsigned long offset = ia64_get_itc();
 49 
 50         return (offset * local_cpu_data->nsec_per_cyc) >> IA64_NSEC_PER_CYC_SHIFT;
 51 }
 52 
 53 static void
 54 itc_reset (void)
 55 {
 56 }
 57 
 58 /*
 59  * Adjust for the fact that xtime has been advanced by delta_nsec (may be negative and/or
 60  * larger than NSEC_PER_SEC.
 61  */
 62 static void
 63 itc_update (long delta_nsec)
 64 {
 65 }
 66 
 67 /*
 68  * Return the number of nano-seconds that elapsed since the last
 69  * update to jiffy.  It is quite possible that the timer interrupt
 70  * will interrupt this and result in a race for any of jiffies,
 71  * wall_jiffies or itm_next.  Thus, the xtime_lock must be at least
 72  * read synchronised when calling this routine (see do_gettimeofday()
 73  * below for an example).
 74  */
 75 unsigned long
 76 itc_get_offset (void)
 77 {
 78         unsigned long elapsed_cycles, lost = jiffies - wall_jiffies;
 79         unsigned long now = ia64_get_itc(), last_tick;
 80 
 81         last_tick = (cpu_data(TIME_KEEPER_ID)->itm_next
 82                      - (lost + 1)*cpu_data(TIME_KEEPER_ID)->itm_delta);
 83 
 84         elapsed_cycles = now - last_tick;
 85         return (elapsed_cycles*local_cpu_data->nsec_per_cyc) >> IA64_NSEC_PER_CYC_SHIFT;
 86 }
 87 
 88 static struct time_interpolator itc_interpolator = {
 89         .get_offset =   itc_get_offset,
 90         .update =       itc_update,
 91         .reset =        itc_reset
 92 };
 93 
 94 int
 95 do_settimeofday (struct timespec *tv)
 96 {
 97         time_t wtm_sec, sec = tv->tv_sec;
 98         long wtm_nsec, nsec = tv->tv_nsec;
 99 
100         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
101                 return -EINVAL;
102 
103         write_seqlock_irq(&xtime_lock);
104         {
105                 /*
106                  * This is revolting. We need to set "xtime" correctly. However, the value
107                  * in this location is the value at the most recent update of wall time.
108                  * Discover what correction gettimeofday would have done, and then undo
109                  * it!
110                  */
111                 nsec -= time_interpolator_get_offset();
112 
113                 wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
114                 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
115 
116                 set_normalized_timespec(&xtime, sec, nsec);
117                 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
118 
119                 time_adjust = 0;                /* stop active adjtime() */
120                 time_status |= STA_UNSYNC;
121                 time_maxerror = NTP_PHASE_LIMIT;
122                 time_esterror = NTP_PHASE_LIMIT;
123                 time_interpolator_reset();
124         }
125         write_sequnlock_irq(&xtime_lock);
126         clock_was_set();
127         return 0;
128 }
129 
130 EXPORT_SYMBOL(do_settimeofday);
131 
132 void
133 do_gettimeofday (struct timeval *tv)
134 {
135         unsigned long seq, nsec, usec, sec, old, offset;
136 
137         while (1) {
138                 seq = read_seqbegin(&xtime_lock);
139                 {
140                         old = last_nsec_offset;
141                         offset = time_interpolator_get_offset();
142                         sec = xtime.tv_sec;
143                         nsec = xtime.tv_nsec;
144                 }
145                 if (unlikely(read_seqretry(&xtime_lock, seq)))
146                         continue;
147                 /*
148                  * Ensure that for any pair of causally ordered gettimeofday() calls, time
149                  * never goes backwards (even when ITC on different CPUs are not perfectly
150                  * synchronized).  (A pair of concurrent calls to gettimeofday() is by
151                  * definition non-causal and hence it makes no sense to talk about
152                  * time-continuity for such calls.)
153                  *
154                  * Doing this in a lock-free and race-free manner is tricky.  Here is why
155                  * it works (most of the time): read_seqretry() just succeeded, which
156                  * implies we calculated a consistent (valid) value for "offset".  If the
157                  * cmpxchg() below succeeds, we further know that last_nsec_offset still
158                  * has the same value as at the beginning of the loop, so there was
159                  * presumably no timer-tick or other updates to last_nsec_offset in the
160                  * meantime.  This isn't 100% true though: there _is_ a possibility of a
161                  * timer-tick occurring right right after read_seqretry() and then getting
162                  * zero or more other readers which will set last_nsec_offset to the same
163                  * value as the one we read at the beginning of the loop.  If this
164                  * happens, we'll end up returning a slightly newer time than we ought to
165                  * (the jump forward is at most "offset" nano-seconds).  There is no
166                  * danger of causing time to go backwards, though, so we are safe in that
167                  * sense.  We could make the probability of this unlucky case occurring
168                  * arbitrarily small by encoding a version number in last_nsec_offset, but
169                  * even without versioning, the probability of this unlucky case should be
170                  * so small that we won't worry about it.
171                  */
172                 if (offset <= old) {
173                         offset = old;
174                         break;
175                 } else if (likely(cmpxchg(&last_nsec_offset, old, offset) == old))
176                         break;
177 
178                 /* someone else beat us to updating last_nsec_offset; try again */
179         }
180 
181         usec = (nsec + offset) / 1000;
182 
183         while (unlikely(usec >= USEC_PER_SEC)) {
184                 usec -= USEC_PER_SEC;
185                 ++sec;
186         }
187 
188         tv->tv_sec = sec;
189         tv->tv_usec = usec;
190 }
191 
192 EXPORT_SYMBOL(do_gettimeofday);
193 
194 /*
195  * The profiling function is SMP safe. (nothing can mess
196  * around with "current", and the profiling counters are
197  * updated with atomic operations). This is especially
198  * useful with a profiling multiplier != 1
199  */
200 static inline void
201 ia64_do_profile (struct pt_regs * regs)
202 {
203         unsigned long ip, slot;
204         extern cpumask_t prof_cpu_mask;
205 
206         profile_hook(regs);
207 
208         if (user_mode(regs))
209                 return;
210 
211         if (!prof_buffer)
212                 return;
213 
214         ip = instruction_pointer(regs);
215         /* Conserve space in histogram by encoding slot bits in address
216          * bits 2 and 3 rather than bits 0 and 1.
217          */
218         slot = ip & 3;
219         ip = (ip & ~3UL) + 4*slot;
220 
221         /*
222          * Only measure the CPUs specified by /proc/irq/prof_cpu_mask.
223          * (default is all CPUs.)
224          */
225         if (!cpu_isset(smp_processor_id(), prof_cpu_mask))
226                 return;
227 
228         ip -= (unsigned long) &_stext;
229         ip >>= prof_shift;
230         /*
231          * Don't ignore out-of-bounds IP values silently,
232          * put them into the last histogram slot, so if
233          * present, they will show up as a sharp peak.
234          */
235         if (ip > prof_len-1)
236                 ip = prof_len-1;
237         atomic_inc((atomic_t *)&prof_buffer[ip]);
238 }
239 
240 static irqreturn_t
241 timer_interrupt (int irq, void *dev_id, struct pt_regs *regs)
242 {
243         unsigned long new_itm;
244 
245         new_itm = local_cpu_data->itm_next;
246 
247         if (!time_after(ia64_get_itc(), new_itm))
248                 printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
249                        ia64_get_itc(), new_itm);
250 
251         ia64_do_profile(regs);
252 
253         while (1) {
254 
255 #ifdef CONFIG_SMP
256                 smp_do_timer(regs);
257 #endif
258                 new_itm += local_cpu_data->itm_delta;
259 
260                 if (smp_processor_id() == TIME_KEEPER_ID) {
261                         /*
262                          * Here we are in the timer irq handler. We have irqs locally
263                          * disabled, but we don't know if the timer_bh is running on
264                          * another CPU. We need to avoid to SMP race by acquiring the
265                          * xtime_lock.
266                          */
267                         write_seqlock(&xtime_lock);
268                         do_timer(regs);
269                         local_cpu_data->itm_next = new_itm;
270                         write_sequnlock(&xtime_lock);
271                 } else
272                         local_cpu_data->itm_next = new_itm;
273 
274                 if (time_after(new_itm, ia64_get_itc()))
275                         break;
276         }
277 
278         do {
279             /*
280              * If we're too close to the next clock tick for comfort, we increase the
281              * safety margin by intentionally dropping the next tick(s).  We do NOT update
282              * itm.next because that would force us to call do_timer() which in turn would
283              * let our clock run too fast (with the potentially devastating effect of
284              * losing monotony of time).
285              */
286             while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
287               new_itm += local_cpu_data->itm_delta;
288             ia64_set_itm(new_itm);
289             /* double check, in case we got hit by a (slow) PMI: */
290         } while (time_after_eq(ia64_get_itc(), new_itm));
291         return IRQ_HANDLED;
292 }
293 
294 /*
295  * Encapsulate access to the itm structure for SMP.
296  */
297 void
298 ia64_cpu_local_tick (void)
299 {
300         int cpu = smp_processor_id();
301         unsigned long shift = 0, delta;
302 
303         /* arrange for the cycle counter to generate a timer interrupt: */
304         ia64_set_itv(IA64_TIMER_VECTOR);
305 
306         delta = local_cpu_data->itm_delta;
307         /*
308          * Stagger the timer tick for each CPU so they don't occur all at (almost) the
309          * same time:
310          */
311         if (cpu) {
312                 unsigned long hi = 1UL << ia64_fls(cpu);
313                 shift = (2*(cpu - hi) + 1) * delta/hi/2;
314         }
315         local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
316         ia64_set_itm(local_cpu_data->itm_next);
317 }
318 
319 void __init
320 ia64_init_itm (void)
321 {
322         unsigned long platform_base_freq, itc_freq;
323         struct pal_freq_ratio itc_ratio, proc_ratio;
324         long status, platform_base_drift, itc_drift;
325 
326         /*
327          * According to SAL v2.6, we need to use a SAL call to determine the platform base
328          * frequency and then a PAL call to determine the frequency ratio between the ITC
329          * and the base frequency.
330          */
331         status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
332                                     &platform_base_freq, &platform_base_drift);
333         if (status != 0) {
334                 printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
335         } else {
336                 status = ia64_pal_freq_ratios(&proc_ratio, 0, &itc_ratio);
337                 if (status != 0)
338                         printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
339         }
340         if (status != 0) {
341                 /* invent "random" values */
342                 printk(KERN_ERR
343                        "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
344                 platform_base_freq = 100000000;
345                 platform_base_drift = -1;       /* no drift info */
346                 itc_ratio.num = 3;
347                 itc_ratio.den = 1;
348         }
349         if (platform_base_freq < 40000000) {
350                 printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
351                        platform_base_freq);
352                 platform_base_freq = 75000000;
353                 platform_base_drift = -1;
354         }
355         if (!proc_ratio.den)
356                 proc_ratio.den = 1;     /* avoid division by zero */
357         if (!itc_ratio.den)
358                 itc_ratio.den = 1;      /* avoid division by zero */
359 
360         itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
361         if (platform_base_drift != -1)
362                 itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
363         else
364                 itc_drift = -1;
365 
366         local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
367         printk(KERN_INFO "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%lu/%lu, "
368                "ITC freq=%lu.%03luMHz+/-%ldppm\n", smp_processor_id(),
369                platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
370                itc_ratio.num, itc_ratio.den, itc_freq / 1000000, (itc_freq / 1000) % 1000,
371                itc_drift);
372 
373         local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
374         local_cpu_data->itc_freq = itc_freq;
375         local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
376         local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
377                                         + itc_freq/2)/itc_freq;
378 
379         if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
380                 itc_interpolator.frequency = local_cpu_data->itc_freq;
381                 itc_interpolator.drift = itc_drift;
382                 register_time_interpolator(&itc_interpolator);
383         }
384 
385         /* Setup the CPU local timer tick */
386         ia64_cpu_local_tick();
387 }
388 
389 static struct irqaction timer_irqaction = {
390         .handler =      timer_interrupt,
391         .flags =        SA_INTERRUPT,
392         .name =         "timer"
393 };
394 
395 void __init
396 time_init (void)
397 {
398         register_percpu_irq(IA64_TIMER_VECTOR, &timer_irqaction);
399         efi_gettimeofday(&xtime);
400         ia64_init_itm();
401 
402         /*
403          * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
404          * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
405          */
406         set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
407 }
408 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp