uv_time.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * SGI RTC clock/timer routines.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Copyright (c) 2009 Silicon Graphics, Inc. All Rights Reserved.
  19. * Copyright (c) Dimitri Sivanich
  20. */
  21. #include <linux/clockchips.h>
  22. #include <linux/slab.h>
  23. #include <asm/uv/uv_mmrs.h>
  24. #include <asm/uv/uv_hub.h>
  25. #include <asm/uv/bios.h>
  26. #include <asm/uv/uv.h>
  27. #include <asm/apic.h>
  28. #include <asm/cpu.h>
  29. #define RTC_NAME "sgi_rtc"
  30. static cycle_t uv_read_rtc(struct clocksource *cs);
  31. static int uv_rtc_next_event(unsigned long, struct clock_event_device *);
  32. static void uv_rtc_timer_setup(enum clock_event_mode,
  33. struct clock_event_device *);
  34. static struct clocksource clocksource_uv = {
  35. .name = RTC_NAME,
  36. .rating = 400,
  37. .read = uv_read_rtc,
  38. .mask = (cycle_t)UVH_RTC_REAL_TIME_CLOCK_MASK,
  39. .shift = 10,
  40. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  41. };
  42. static struct clock_event_device clock_event_device_uv = {
  43. .name = RTC_NAME,
  44. .features = CLOCK_EVT_FEAT_ONESHOT,
  45. .shift = 20,
  46. .rating = 400,
  47. .irq = -1,
  48. .set_next_event = uv_rtc_next_event,
  49. .set_mode = uv_rtc_timer_setup,
  50. .event_handler = NULL,
  51. };
  52. static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  53. /* There is one of these allocated per node */
  54. struct uv_rtc_timer_head {
  55. spinlock_t lock;
  56. /* next cpu waiting for timer, local node relative: */
  57. int next_cpu;
  58. /* number of cpus on this node: */
  59. int ncpus;
  60. struct {
  61. int lcpu; /* systemwide logical cpu number */
  62. u64 expires; /* next timer expiration for this cpu */
  63. } cpu[1];
  64. };
  65. /*
  66. * Access to uv_rtc_timer_head via blade id.
  67. */
  68. static struct uv_rtc_timer_head **blade_info __read_mostly;
  69. static int uv_rtc_evt_enable;
  70. /*
  71. * Hardware interface routines
  72. */
  73. /* Send IPIs to another node */
  74. static void uv_rtc_send_IPI(int cpu)
  75. {
  76. unsigned long apicid, val;
  77. int pnode;
  78. apicid = cpu_physical_id(cpu);
  79. pnode = uv_apicid_to_pnode(apicid);
  80. val = (1UL << UVH_IPI_INT_SEND_SHFT) |
  81. (apicid << UVH_IPI_INT_APIC_ID_SHFT) |
  82. (X86_PLATFORM_IPI_VECTOR << UVH_IPI_INT_VECTOR_SHFT);
  83. uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
  84. }
  85. /* Check for an RTC interrupt pending */
  86. static int uv_intr_pending(int pnode)
  87. {
  88. return uv_read_global_mmr64(pnode, UVH_EVENT_OCCURRED0) &
  89. UVH_EVENT_OCCURRED0_RTC1_MASK;
  90. }
  91. /* Setup interrupt and return non-zero if early expiration occurred. */
  92. static int uv_setup_intr(int cpu, u64 expires)
  93. {
  94. u64 val;
  95. int pnode = uv_cpu_to_pnode(cpu);
  96. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
  97. UVH_RTC1_INT_CONFIG_M_MASK);
  98. uv_write_global_mmr64(pnode, UVH_INT_CMPB, -1L);
  99. uv_write_global_mmr64(pnode, UVH_EVENT_OCCURRED0_ALIAS,
  100. UVH_EVENT_OCCURRED0_RTC1_MASK);
  101. val = (X86_PLATFORM_IPI_VECTOR << UVH_RTC1_INT_CONFIG_VECTOR_SHFT) |
  102. ((u64)cpu_physical_id(cpu) << UVH_RTC1_INT_CONFIG_APIC_ID_SHFT);
  103. /* Set configuration */
  104. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG, val);
  105. /* Initialize comparator value */
  106. uv_write_global_mmr64(pnode, UVH_INT_CMPB, expires);
  107. if (uv_read_rtc(NULL) <= expires)
  108. return 0;
  109. return !uv_intr_pending(pnode);
  110. }
  111. /*
  112. * Per-cpu timer tracking routines
  113. */
  114. static __init void uv_rtc_deallocate_timers(void)
  115. {
  116. int bid;
  117. for_each_possible_blade(bid) {
  118. kfree(blade_info[bid]);
  119. }
  120. kfree(blade_info);
  121. }
  122. /* Allocate per-node list of cpu timer expiration times. */
  123. static __init int uv_rtc_allocate_timers(void)
  124. {
  125. int cpu;
  126. blade_info = kmalloc(uv_possible_blades * sizeof(void *), GFP_KERNEL);
  127. if (!blade_info)
  128. return -ENOMEM;
  129. memset(blade_info, 0, uv_possible_blades * sizeof(void *));
  130. for_each_present_cpu(cpu) {
  131. int nid = cpu_to_node(cpu);
  132. int bid = uv_cpu_to_blade_id(cpu);
  133. int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
  134. struct uv_rtc_timer_head *head = blade_info[bid];
  135. if (!head) {
  136. head = kmalloc_node(sizeof(struct uv_rtc_timer_head) +
  137. (uv_blade_nr_possible_cpus(bid) *
  138. 2 * sizeof(u64)),
  139. GFP_KERNEL, nid);
  140. if (!head) {
  141. uv_rtc_deallocate_timers();
  142. return -ENOMEM;
  143. }
  144. spin_lock_init(&head->lock);
  145. head->ncpus = uv_blade_nr_possible_cpus(bid);
  146. head->next_cpu = -1;
  147. blade_info[bid] = head;
  148. }
  149. head->cpu[bcpu].lcpu = cpu;
  150. head->cpu[bcpu].expires = ULLONG_MAX;
  151. }
  152. return 0;
  153. }
  154. /* Find and set the next expiring timer. */
  155. static void uv_rtc_find_next_timer(struct uv_rtc_timer_head *head, int pnode)
  156. {
  157. u64 lowest = ULLONG_MAX;
  158. int c, bcpu = -1;
  159. head->next_cpu = -1;
  160. for (c = 0; c < head->ncpus; c++) {
  161. u64 exp = head->cpu[c].expires;
  162. if (exp < lowest) {
  163. bcpu = c;
  164. lowest = exp;
  165. }
  166. }
  167. if (bcpu >= 0) {
  168. head->next_cpu = bcpu;
  169. c = head->cpu[bcpu].lcpu;
  170. if (uv_setup_intr(c, lowest))
  171. /* If we didn't set it up in time, trigger */
  172. uv_rtc_send_IPI(c);
  173. } else {
  174. uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
  175. UVH_RTC1_INT_CONFIG_M_MASK);
  176. }
  177. }
  178. /*
  179. * Set expiration time for current cpu.
  180. *
  181. * Returns 1 if we missed the expiration time.
  182. */
  183. static int uv_rtc_set_timer(int cpu, u64 expires)
  184. {
  185. int pnode = uv_cpu_to_pnode(cpu);
  186. int bid = uv_cpu_to_blade_id(cpu);
  187. struct uv_rtc_timer_head *head = blade_info[bid];
  188. int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
  189. u64 *t = &head->cpu[bcpu].expires;
  190. unsigned long flags;
  191. int next_cpu;
  192. spin_lock_irqsave(&head->lock, flags);
  193. next_cpu = head->next_cpu;
  194. *t = expires;
  195. /* Will this one be next to go off? */
  196. if (next_cpu < 0 || bcpu == next_cpu ||
  197. expires < head->cpu[next_cpu].expires) {
  198. head->next_cpu = bcpu;
  199. if (uv_setup_intr(cpu, expires)) {
  200. *t = ULLONG_MAX;
  201. uv_rtc_find_next_timer(head, pnode);
  202. spin_unlock_irqrestore(&head->lock, flags);
  203. return -ETIME;
  204. }
  205. }
  206. spin_unlock_irqrestore(&head->lock, flags);
  207. return 0;
  208. }
  209. /*
  210. * Unset expiration time for current cpu.
  211. *
  212. * Returns 1 if this timer was pending.
  213. */
  214. static int uv_rtc_unset_timer(int cpu, int force)
  215. {
  216. int pnode = uv_cpu_to_pnode(cpu);
  217. int bid = uv_cpu_to_blade_id(cpu);
  218. struct uv_rtc_timer_head *head = blade_info[bid];
  219. int bcpu = uv_cpu_hub_info(cpu)->blade_processor_id;
  220. u64 *t = &head->cpu[bcpu].expires;
  221. unsigned long flags;
  222. int rc = 0;
  223. spin_lock_irqsave(&head->lock, flags);
  224. if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
  225. rc = 1;
  226. if (rc) {
  227. *t = ULLONG_MAX;
  228. /* Was the hardware setup for this timer? */
  229. if (head->next_cpu == bcpu)
  230. uv_rtc_find_next_timer(head, pnode);
  231. }
  232. spin_unlock_irqrestore(&head->lock, flags);
  233. return rc;
  234. }
  235. /*
  236. * Kernel interface routines.
  237. */
  238. /*
  239. * Read the RTC.
  240. *
  241. * Starting with HUB rev 2.0, the UV RTC register is replicated across all
  242. * cachelines of it's own page. This allows faster simultaneous reads
  243. * from a given socket.
  244. */
  245. static cycle_t uv_read_rtc(struct clocksource *cs)
  246. {
  247. unsigned long offset;
  248. if (uv_get_min_hub_revision_id() == 1)
  249. offset = 0;
  250. else
  251. offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
  252. return (cycle_t)uv_read_local_mmr(UVH_RTC | offset);
  253. }
  254. /*
  255. * Program the next event, relative to now
  256. */
  257. static int uv_rtc_next_event(unsigned long delta,
  258. struct clock_event_device *ced)
  259. {
  260. int ced_cpu = cpumask_first(ced->cpumask);
  261. return uv_rtc_set_timer(ced_cpu, delta + uv_read_rtc(NULL));
  262. }
  263. /*
  264. * Setup the RTC timer in oneshot mode
  265. */
  266. static void uv_rtc_timer_setup(enum clock_event_mode mode,
  267. struct clock_event_device *evt)
  268. {
  269. int ced_cpu = cpumask_first(evt->cpumask);
  270. switch (mode) {
  271. case CLOCK_EVT_MODE_PERIODIC:
  272. case CLOCK_EVT_MODE_ONESHOT:
  273. case CLOCK_EVT_MODE_RESUME:
  274. /* Nothing to do here yet */
  275. break;
  276. case CLOCK_EVT_MODE_UNUSED:
  277. case CLOCK_EVT_MODE_SHUTDOWN:
  278. uv_rtc_unset_timer(ced_cpu, 1);
  279. break;
  280. }
  281. }
  282. static void uv_rtc_interrupt(void)
  283. {
  284. int cpu = smp_processor_id();
  285. struct clock_event_device *ced = &per_cpu(cpu_ced, cpu);
  286. if (!ced || !ced->event_handler)
  287. return;
  288. if (uv_rtc_unset_timer(cpu, 0) != 1)
  289. return;
  290. ced->event_handler(ced);
  291. }
  292. static int __init uv_enable_evt_rtc(char *str)
  293. {
  294. uv_rtc_evt_enable = 1;
  295. return 1;
  296. }
  297. __setup("uvrtcevt", uv_enable_evt_rtc);
  298. static __init void uv_rtc_register_clockevents(struct work_struct *dummy)
  299. {
  300. struct clock_event_device *ced = &__get_cpu_var(cpu_ced);
  301. *ced = clock_event_device_uv;
  302. ced->cpumask = cpumask_of(smp_processor_id());
  303. clockevents_register_device(ced);
  304. }
  305. static __init int uv_rtc_setup_clock(void)
  306. {
  307. int rc;
  308. if (!is_uv_system())
  309. return -ENODEV;
  310. clocksource_uv.mult = clocksource_hz2mult(sn_rtc_cycles_per_second,
  311. clocksource_uv.shift);
  312. /* If single blade, prefer tsc */
  313. if (uv_num_possible_blades() == 1)
  314. clocksource_uv.rating = 250;
  315. rc = clocksource_register(&clocksource_uv);
  316. if (rc)
  317. printk(KERN_INFO "UV RTC clocksource failed rc %d\n", rc);
  318. else
  319. printk(KERN_INFO "UV RTC clocksource registered freq %lu MHz\n",
  320. sn_rtc_cycles_per_second/(unsigned long)1E6);
  321. if (rc || !uv_rtc_evt_enable || x86_platform_ipi_callback)
  322. return rc;
  323. /* Setup and register clockevents */
  324. rc = uv_rtc_allocate_timers();
  325. if (rc)
  326. goto error;
  327. x86_platform_ipi_callback = uv_rtc_interrupt;
  328. clock_event_device_uv.mult = div_sc(sn_rtc_cycles_per_second,
  329. NSEC_PER_SEC, clock_event_device_uv.shift);
  330. clock_event_device_uv.min_delta_ns = NSEC_PER_SEC /
  331. sn_rtc_cycles_per_second;
  332. clock_event_device_uv.max_delta_ns = clocksource_uv.mask *
  333. (NSEC_PER_SEC / sn_rtc_cycles_per_second);
  334. rc = schedule_on_each_cpu(uv_rtc_register_clockevents);
  335. if (rc) {
  336. x86_platform_ipi_callback = NULL;
  337. uv_rtc_deallocate_timers();
  338. goto error;
  339. }
  340. printk(KERN_INFO "UV RTC clockevents registered\n");
  341. return 0;
  342. error:
  343. clocksource_unregister(&clocksource_uv);
  344. printk(KERN_INFO "UV RTC clockevents failed rc %d\n", rc);
  345. return rc;
  346. }
  347. arch_initcall(uv_rtc_setup_clock);