vmitime.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * VMI paravirtual timer support routines.
  3. *
  4. * Copyright (C) 2005, VMware, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more
  15. * details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * Send feedback to dhecht@vmware.com
  22. *
  23. */
  24. /*
  25. * Portions of this code from arch/i386/kernel/timers/timer_tsc.c.
  26. * Portions of the CONFIG_NO_IDLE_HZ code from arch/s390/kernel/time.c.
  27. * See comments there for proper credits.
  28. */
  29. #include <linux/spinlock.h>
  30. #include <linux/init.h>
  31. #include <linux/errno.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/kernel_stat.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/clocksource.h>
  37. #include <asm/timer.h>
  38. #include <asm/io.h>
  39. #include <asm/apic.h>
  40. #include <asm/div64.h>
  41. #include <asm/timer.h>
  42. #include <asm/desc.h>
  43. #include <asm/vmi.h>
  44. #include <asm/vmi_time.h>
  45. #include <mach_timer.h>
  46. #include <io_ports.h>
  47. #ifdef CONFIG_X86_LOCAL_APIC
  48. #define VMI_ALARM_WIRING VMI_ALARM_WIRED_LVTT
  49. #else
  50. #define VMI_ALARM_WIRING VMI_ALARM_WIRED_IRQ0
  51. #endif
  52. /* Cached VMI operations */
  53. struct vmi_timer_ops vmi_timer_ops;
  54. #ifdef CONFIG_NO_IDLE_HZ
  55. /* /proc/sys/kernel/hz_timer state. */
  56. int sysctl_hz_timer;
  57. /* Some stats */
  58. static DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_irqs);
  59. static DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_jiffies);
  60. static DEFINE_PER_CPU(unsigned long, idle_start_jiffies);
  61. #endif /* CONFIG_NO_IDLE_HZ */
  62. /* Number of alarms per second. By default this is CONFIG_VMI_ALARM_HZ. */
  63. static int alarm_hz = CONFIG_VMI_ALARM_HZ;
  64. /* Cache of the value get_cycle_frequency / HZ. */
  65. static signed long long cycles_per_jiffy;
  66. /* Cache of the value get_cycle_frequency / alarm_hz. */
  67. static signed long long cycles_per_alarm;
  68. /* The number of cycles accounted for by the 'jiffies'/'xtime' count.
  69. * Protected by xtime_lock. */
  70. static unsigned long long real_cycles_accounted_system;
  71. /* The number of cycles accounted for by update_process_times(), per cpu. */
  72. static DEFINE_PER_CPU(unsigned long long, process_times_cycles_accounted_cpu);
  73. /* The number of stolen cycles accounted, per cpu. */
  74. static DEFINE_PER_CPU(unsigned long long, stolen_cycles_accounted_cpu);
  75. /* Clock source. */
  76. static cycle_t read_real_cycles(void)
  77. {
  78. return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_REAL);
  79. }
  80. static cycle_t read_available_cycles(void)
  81. {
  82. return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_AVAILABLE);
  83. }
  84. #if 0
  85. static cycle_t read_stolen_cycles(void)
  86. {
  87. return vmi_timer_ops.get_cycle_counter(VMI_CYCLES_STOLEN);
  88. }
  89. #endif /* 0 */
  90. static struct clocksource clocksource_vmi = {
  91. .name = "vmi-timer",
  92. .rating = 450,
  93. .read = read_real_cycles,
  94. .mask = CLOCKSOURCE_MASK(64),
  95. .mult = 0, /* to be set */
  96. .shift = 22,
  97. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  98. };
  99. /* Timer interrupt handler. */
  100. static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id);
  101. static struct irqaction vmi_timer_irq = {
  102. vmi_timer_interrupt,
  103. SA_INTERRUPT,
  104. CPU_MASK_NONE,
  105. "VMI-alarm",
  106. NULL,
  107. NULL
  108. };
  109. /* Alarm rate */
  110. static int __init vmi_timer_alarm_rate_setup(char* str)
  111. {
  112. int alarm_rate;
  113. if (get_option(&str, &alarm_rate) == 1 && alarm_rate > 0) {
  114. alarm_hz = alarm_rate;
  115. printk(KERN_WARNING "VMI timer alarm HZ set to %d\n", alarm_hz);
  116. }
  117. return 1;
  118. }
  119. __setup("vmi_timer_alarm_hz=", vmi_timer_alarm_rate_setup);
  120. /* Initialization */
  121. static void vmi_get_wallclock_ts(struct timespec *ts)
  122. {
  123. unsigned long long wallclock;
  124. wallclock = vmi_timer_ops.get_wallclock(); // nsec units
  125. ts->tv_nsec = do_div(wallclock, 1000000000);
  126. ts->tv_sec = wallclock;
  127. }
  128. unsigned long vmi_get_wallclock(void)
  129. {
  130. struct timespec ts;
  131. vmi_get_wallclock_ts(&ts);
  132. return ts.tv_sec;
  133. }
  134. int vmi_set_wallclock(unsigned long now)
  135. {
  136. return -1;
  137. }
  138. unsigned long long vmi_get_sched_cycles(void)
  139. {
  140. return read_available_cycles();
  141. }
  142. unsigned long vmi_cpu_khz(void)
  143. {
  144. unsigned long long khz;
  145. khz = vmi_timer_ops.get_cycle_frequency();
  146. (void)do_div(khz, 1000);
  147. return khz;
  148. }
  149. void __init vmi_time_init(void)
  150. {
  151. unsigned long long cycles_per_sec, cycles_per_msec;
  152. unsigned long flags;
  153. local_irq_save(flags);
  154. setup_irq(0, &vmi_timer_irq);
  155. #ifdef CONFIG_X86_LOCAL_APIC
  156. set_intr_gate(LOCAL_TIMER_VECTOR, apic_vmi_timer_interrupt);
  157. #endif
  158. real_cycles_accounted_system = read_real_cycles();
  159. per_cpu(process_times_cycles_accounted_cpu, 0) = read_available_cycles();
  160. cycles_per_sec = vmi_timer_ops.get_cycle_frequency();
  161. cycles_per_jiffy = cycles_per_sec;
  162. (void)do_div(cycles_per_jiffy, HZ);
  163. cycles_per_alarm = cycles_per_sec;
  164. (void)do_div(cycles_per_alarm, alarm_hz);
  165. cycles_per_msec = cycles_per_sec;
  166. (void)do_div(cycles_per_msec, 1000);
  167. printk(KERN_WARNING "VMI timer cycles/sec = %llu ; cycles/jiffy = %llu ;"
  168. "cycles/alarm = %llu\n", cycles_per_sec, cycles_per_jiffy,
  169. cycles_per_alarm);
  170. clocksource_vmi.mult = clocksource_khz2mult(cycles_per_msec,
  171. clocksource_vmi.shift);
  172. if (clocksource_register(&clocksource_vmi))
  173. printk(KERN_WARNING "Error registering VMITIME clocksource.");
  174. /* Disable PIT. */
  175. outb_p(0x3a, PIT_MODE); /* binary, mode 5, LSB/MSB, ch 0 */
  176. /* schedule the alarm. do this in phase with process_times_cycles_accounted_cpu
  177. * reduce the latency calling update_process_times. */
  178. vmi_timer_ops.set_alarm(
  179. VMI_ALARM_WIRED_IRQ0 | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
  180. per_cpu(process_times_cycles_accounted_cpu, 0) + cycles_per_alarm,
  181. cycles_per_alarm);
  182. local_irq_restore(flags);
  183. }
  184. #ifdef CONFIG_X86_LOCAL_APIC
  185. void __init vmi_timer_setup_boot_alarm(void)
  186. {
  187. local_irq_disable();
  188. /* Route the interrupt to the correct vector. */
  189. apic_write_around(APIC_LVTT, LOCAL_TIMER_VECTOR);
  190. /* Cancel the IRQ0 wired alarm, and setup the LVTT alarm. */
  191. vmi_timer_ops.cancel_alarm(VMI_CYCLES_AVAILABLE);
  192. vmi_timer_ops.set_alarm(
  193. VMI_ALARM_WIRED_LVTT | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
  194. per_cpu(process_times_cycles_accounted_cpu, 0) + cycles_per_alarm,
  195. cycles_per_alarm);
  196. local_irq_enable();
  197. }
  198. /* Initialize the time accounting variables for an AP on an SMP system.
  199. * Also, set the local alarm for the AP. */
  200. void __devinit vmi_timer_setup_secondary_alarm(void)
  201. {
  202. int cpu = smp_processor_id();
  203. /* Route the interrupt to the correct vector. */
  204. apic_write_around(APIC_LVTT, LOCAL_TIMER_VECTOR);
  205. per_cpu(process_times_cycles_accounted_cpu, cpu) = read_available_cycles();
  206. vmi_timer_ops.set_alarm(
  207. VMI_ALARM_WIRED_LVTT | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
  208. per_cpu(process_times_cycles_accounted_cpu, cpu) + cycles_per_alarm,
  209. cycles_per_alarm);
  210. }
  211. #endif
  212. /* Update system wide (real) time accounting (e.g. jiffies, xtime). */
  213. static void vmi_account_real_cycles(unsigned long long cur_real_cycles)
  214. {
  215. long long cycles_not_accounted;
  216. write_seqlock(&xtime_lock);
  217. cycles_not_accounted = cur_real_cycles - real_cycles_accounted_system;
  218. while (cycles_not_accounted >= cycles_per_jiffy) {
  219. /* systems wide jiffies. */
  220. do_timer(1);
  221. cycles_not_accounted -= cycles_per_jiffy;
  222. real_cycles_accounted_system += cycles_per_jiffy;
  223. }
  224. write_sequnlock(&xtime_lock);
  225. }
  226. /* Update per-cpu process times. */
  227. static void vmi_account_process_times_cycles(struct pt_regs *regs, int cpu,
  228. unsigned long long cur_process_times_cycles)
  229. {
  230. long long cycles_not_accounted;
  231. cycles_not_accounted = cur_process_times_cycles -
  232. per_cpu(process_times_cycles_accounted_cpu, cpu);
  233. while (cycles_not_accounted >= cycles_per_jiffy) {
  234. /* Account time to the current process. This includes
  235. * calling into the scheduler to decrement the timeslice
  236. * and possibly reschedule.*/
  237. update_process_times(user_mode(regs));
  238. /* XXX handle /proc/profile multiplier. */
  239. profile_tick(CPU_PROFILING);
  240. cycles_not_accounted -= cycles_per_jiffy;
  241. per_cpu(process_times_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
  242. }
  243. }
  244. #ifdef CONFIG_NO_IDLE_HZ
  245. /* Update per-cpu idle times. Used when a no-hz halt is ended. */
  246. static void vmi_account_no_hz_idle_cycles(int cpu,
  247. unsigned long long cur_process_times_cycles)
  248. {
  249. long long cycles_not_accounted;
  250. unsigned long no_idle_hz_jiffies = 0;
  251. cycles_not_accounted = cur_process_times_cycles -
  252. per_cpu(process_times_cycles_accounted_cpu, cpu);
  253. while (cycles_not_accounted >= cycles_per_jiffy) {
  254. no_idle_hz_jiffies++;
  255. cycles_not_accounted -= cycles_per_jiffy;
  256. per_cpu(process_times_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
  257. }
  258. /* Account time to the idle process. */
  259. account_steal_time(idle_task(cpu), jiffies_to_cputime(no_idle_hz_jiffies));
  260. }
  261. #endif
  262. /* Update per-cpu stolen time. */
  263. static void vmi_account_stolen_cycles(int cpu,
  264. unsigned long long cur_real_cycles,
  265. unsigned long long cur_avail_cycles)
  266. {
  267. long long stolen_cycles_not_accounted;
  268. unsigned long stolen_jiffies = 0;
  269. if (cur_real_cycles < cur_avail_cycles)
  270. return;
  271. stolen_cycles_not_accounted = cur_real_cycles - cur_avail_cycles -
  272. per_cpu(stolen_cycles_accounted_cpu, cpu);
  273. while (stolen_cycles_not_accounted >= cycles_per_jiffy) {
  274. stolen_jiffies++;
  275. stolen_cycles_not_accounted -= cycles_per_jiffy;
  276. per_cpu(stolen_cycles_accounted_cpu, cpu) += cycles_per_jiffy;
  277. }
  278. /* HACK: pass NULL to force time onto cpustat->steal. */
  279. account_steal_time(NULL, jiffies_to_cputime(stolen_jiffies));
  280. }
  281. /* Body of either IRQ0 interrupt handler (UP no local-APIC) or
  282. * local-APIC LVTT interrupt handler (UP & local-APIC or SMP). */
  283. static void vmi_local_timer_interrupt(int cpu)
  284. {
  285. unsigned long long cur_real_cycles, cur_process_times_cycles;
  286. cur_real_cycles = read_real_cycles();
  287. cur_process_times_cycles = read_available_cycles();
  288. /* Update system wide (real) time state (xtime, jiffies). */
  289. vmi_account_real_cycles(cur_real_cycles);
  290. /* Update per-cpu process times. */
  291. vmi_account_process_times_cycles(get_irq_regs(), cpu, cur_process_times_cycles);
  292. /* Update time stolen from this cpu by the hypervisor. */
  293. vmi_account_stolen_cycles(cpu, cur_real_cycles, cur_process_times_cycles);
  294. }
  295. #ifdef CONFIG_NO_IDLE_HZ
  296. /* Must be called only from idle loop, with interrupts disabled. */
  297. int vmi_stop_hz_timer(void)
  298. {
  299. /* Note that cpu_set, cpu_clear are (SMP safe) atomic on x86. */
  300. unsigned long seq, next;
  301. unsigned long long real_cycles_expiry;
  302. int cpu = smp_processor_id();
  303. BUG_ON(!irqs_disabled());
  304. if (sysctl_hz_timer != 0)
  305. return 0;
  306. cpu_set(cpu, nohz_cpu_mask);
  307. smp_mb();
  308. if (rcu_needs_cpu(cpu) || local_softirq_pending() ||
  309. (next = next_timer_interrupt(),
  310. time_before_eq(next, jiffies + HZ/CONFIG_VMI_ALARM_HZ))) {
  311. cpu_clear(cpu, nohz_cpu_mask);
  312. return 0;
  313. }
  314. /* Convert jiffies to the real cycle counter. */
  315. do {
  316. seq = read_seqbegin(&xtime_lock);
  317. real_cycles_expiry = real_cycles_accounted_system +
  318. (long)(next - jiffies) * cycles_per_jiffy;
  319. } while (read_seqretry(&xtime_lock, seq));
  320. /* This cpu is going idle. Disable the periodic alarm. */
  321. vmi_timer_ops.cancel_alarm(VMI_CYCLES_AVAILABLE);
  322. per_cpu(idle_start_jiffies, cpu) = jiffies;
  323. /* Set the real time alarm to expire at the next event. */
  324. vmi_timer_ops.set_alarm(
  325. VMI_ALARM_WIRING | VMI_ALARM_IS_ONESHOT | VMI_CYCLES_REAL,
  326. real_cycles_expiry, 0);
  327. return 1;
  328. }
  329. static void vmi_reenable_hz_timer(int cpu)
  330. {
  331. /* For /proc/vmi/info idle_hz stat. */
  332. per_cpu(vmi_idle_no_hz_jiffies, cpu) += jiffies - per_cpu(idle_start_jiffies, cpu);
  333. per_cpu(vmi_idle_no_hz_irqs, cpu)++;
  334. /* Don't bother explicitly cancelling the one-shot alarm -- at
  335. * worse we will receive a spurious timer interrupt. */
  336. vmi_timer_ops.set_alarm(
  337. VMI_ALARM_WIRING | VMI_ALARM_IS_PERIODIC | VMI_CYCLES_AVAILABLE,
  338. per_cpu(process_times_cycles_accounted_cpu, cpu) + cycles_per_alarm,
  339. cycles_per_alarm);
  340. /* Indicate this cpu is no longer nohz idle. */
  341. cpu_clear(cpu, nohz_cpu_mask);
  342. }
  343. /* Called from interrupt handlers when (local) HZ timer is disabled. */
  344. void vmi_account_time_restart_hz_timer(void)
  345. {
  346. unsigned long long cur_real_cycles, cur_process_times_cycles;
  347. int cpu = smp_processor_id();
  348. BUG_ON(!irqs_disabled());
  349. /* Account the time during which the HZ timer was disabled. */
  350. cur_real_cycles = read_real_cycles();
  351. cur_process_times_cycles = read_available_cycles();
  352. /* Update system wide (real) time state (xtime, jiffies). */
  353. vmi_account_real_cycles(cur_real_cycles);
  354. /* Update per-cpu idle times. */
  355. vmi_account_no_hz_idle_cycles(cpu, cur_process_times_cycles);
  356. /* Update time stolen from this cpu by the hypervisor. */
  357. vmi_account_stolen_cycles(cpu, cur_real_cycles, cur_process_times_cycles);
  358. /* Reenable the hz timer. */
  359. vmi_reenable_hz_timer(cpu);
  360. }
  361. #endif /* CONFIG_NO_IDLE_HZ */
  362. /* UP (and no local-APIC) VMI-timer alarm interrupt handler.
  363. * Handler for IRQ0. Not used when SMP or X86_LOCAL_APIC after
  364. * APIC setup and setup_boot_vmi_alarm() is called. */
  365. static irqreturn_t vmi_timer_interrupt(int irq, void *dev_id)
  366. {
  367. vmi_local_timer_interrupt(smp_processor_id());
  368. return IRQ_HANDLED;
  369. }
  370. #ifdef CONFIG_X86_LOCAL_APIC
  371. /* SMP VMI-timer alarm interrupt handler. Handler for LVTT vector.
  372. * Also used in UP when CONFIG_X86_LOCAL_APIC.
  373. * The wrapper code is from arch/i386/kernel/apic.c#smp_apic_timer_interrupt. */
  374. void smp_apic_vmi_timer_interrupt(struct pt_regs *regs)
  375. {
  376. struct pt_regs *old_regs = set_irq_regs(regs);
  377. int cpu = smp_processor_id();
  378. /*
  379. * the NMI deadlock-detector uses this.
  380. */
  381. per_cpu(irq_stat,cpu).apic_timer_irqs++;
  382. /*
  383. * NOTE! We'd better ACK the irq immediately,
  384. * because timer handling can be slow.
  385. */
  386. ack_APIC_irq();
  387. /*
  388. * update_process_times() expects us to have done irq_enter().
  389. * Besides, if we don't timer interrupts ignore the global
  390. * interrupt lock, which is the WrongThing (tm) to do.
  391. */
  392. irq_enter();
  393. vmi_local_timer_interrupt(cpu);
  394. irq_exit();
  395. set_irq_regs(old_regs);
  396. }
  397. #endif /* CONFIG_X86_LOCAL_APIC */