mmtimer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Timer device implementation for SGI SN platforms.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (c) 2001-2006 Silicon Graphics, Inc. All rights reserved.
  9. *
  10. * This driver exports an API that should be supportable by any HPET or IA-PC
  11. * multimedia timer. The code below is currently specific to the SGI Altix
  12. * SHub RTC, however.
  13. *
  14. * 11/01/01 - jbarnes - initial revision
  15. * 9/10/04 - Christoph Lameter - remove interrupt support for kernel inclusion
  16. * 10/1/04 - Christoph Lameter - provide posix clock CLOCK_SGI_CYCLE
  17. * 10/13/04 - Christoph Lameter, Dimitri Sivanich - provide timer interrupt
  18. * support via the posix timer interface
  19. */
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/ioctl.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/mm.h>
  27. #include <linux/fs.h>
  28. #include <linux/mmtimer.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/posix-timers.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/time.h>
  33. #include <linux/math64.h>
  34. #include <linux/smp_lock.h>
  35. #include <linux/slab.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/sn/addrs.h>
  38. #include <asm/sn/intr.h>
  39. #include <asm/sn/shub_mmr.h>
  40. #include <asm/sn/nodepda.h>
  41. #include <asm/sn/shubio.h>
  42. MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
  43. MODULE_DESCRIPTION("SGI Altix RTC Timer");
  44. MODULE_LICENSE("GPL");
  45. /* name of the device, usually in /dev */
  46. #define MMTIMER_NAME "mmtimer"
  47. #define MMTIMER_DESC "SGI Altix RTC Timer"
  48. #define MMTIMER_VERSION "2.1"
  49. #define RTC_BITS 55 /* 55 bits for this implementation */
  50. extern unsigned long sn_rtc_cycles_per_second;
  51. #define RTC_COUNTER_ADDR ((long *)LOCAL_MMR_ADDR(SH_RTC))
  52. #define rtc_time() (*RTC_COUNTER_ADDR)
  53. static long mmtimer_ioctl(struct file *file, unsigned int cmd,
  54. unsigned long arg);
  55. static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma);
  56. /*
  57. * Period in femtoseconds (10^-15 s)
  58. */
  59. static unsigned long mmtimer_femtoperiod = 0;
  60. static const struct file_operations mmtimer_fops = {
  61. .owner = THIS_MODULE,
  62. .mmap = mmtimer_mmap,
  63. .unlocked_ioctl = mmtimer_ioctl,
  64. .llseek = noop_llseek,
  65. };
  66. /*
  67. * We only have comparison registers RTC1-4 currently available per
  68. * node. RTC0 is used by SAL.
  69. */
  70. /* Check for an RTC interrupt pending */
  71. static int mmtimer_int_pending(int comparator)
  72. {
  73. if (HUB_L((unsigned long *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED)) &
  74. SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator)
  75. return 1;
  76. else
  77. return 0;
  78. }
  79. /* Clear the RTC interrupt pending bit */
  80. static void mmtimer_clr_int_pending(int comparator)
  81. {
  82. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED_ALIAS),
  83. SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator);
  84. }
  85. /* Setup timer on comparator RTC1 */
  86. static void mmtimer_setup_int_0(int cpu, u64 expires)
  87. {
  88. u64 val;
  89. /* Disable interrupt */
  90. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 0UL);
  91. /* Initialize comparator value */
  92. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), -1L);
  93. /* Clear pending bit */
  94. mmtimer_clr_int_pending(0);
  95. val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC1_INT_CONFIG_IDX_SHFT) |
  96. ((u64)cpu_physical_id(cpu) <<
  97. SH_RTC1_INT_CONFIG_PID_SHFT);
  98. /* Set configuration */
  99. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_CONFIG), val);
  100. /* Enable RTC interrupts */
  101. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 1UL);
  102. /* Initialize comparator value */
  103. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), expires);
  104. }
  105. /* Setup timer on comparator RTC2 */
  106. static void mmtimer_setup_int_1(int cpu, u64 expires)
  107. {
  108. u64 val;
  109. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 0UL);
  110. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), -1L);
  111. mmtimer_clr_int_pending(1);
  112. val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC2_INT_CONFIG_IDX_SHFT) |
  113. ((u64)cpu_physical_id(cpu) <<
  114. SH_RTC2_INT_CONFIG_PID_SHFT);
  115. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_CONFIG), val);
  116. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 1UL);
  117. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), expires);
  118. }
  119. /* Setup timer on comparator RTC3 */
  120. static void mmtimer_setup_int_2(int cpu, u64 expires)
  121. {
  122. u64 val;
  123. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 0UL);
  124. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), -1L);
  125. mmtimer_clr_int_pending(2);
  126. val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC3_INT_CONFIG_IDX_SHFT) |
  127. ((u64)cpu_physical_id(cpu) <<
  128. SH_RTC3_INT_CONFIG_PID_SHFT);
  129. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_CONFIG), val);
  130. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 1UL);
  131. HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), expires);
  132. }
  133. /*
  134. * This function must be called with interrupts disabled and preemption off
  135. * in order to insure that the setup succeeds in a deterministic time frame.
  136. * It will check if the interrupt setup succeeded.
  137. */
  138. static int mmtimer_setup(int cpu, int comparator, unsigned long expires)
  139. {
  140. switch (comparator) {
  141. case 0:
  142. mmtimer_setup_int_0(cpu, expires);
  143. break;
  144. case 1:
  145. mmtimer_setup_int_1(cpu, expires);
  146. break;
  147. case 2:
  148. mmtimer_setup_int_2(cpu, expires);
  149. break;
  150. }
  151. /* We might've missed our expiration time */
  152. if (rtc_time() <= expires)
  153. return 1;
  154. /*
  155. * If an interrupt is already pending then its okay
  156. * if not then we failed
  157. */
  158. return mmtimer_int_pending(comparator);
  159. }
  160. static int mmtimer_disable_int(long nasid, int comparator)
  161. {
  162. switch (comparator) {
  163. case 0:
  164. nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE),
  165. 0UL) : REMOTE_HUB_S(nasid, SH_RTC1_INT_ENABLE, 0UL);
  166. break;
  167. case 1:
  168. nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE),
  169. 0UL) : REMOTE_HUB_S(nasid, SH_RTC2_INT_ENABLE, 0UL);
  170. break;
  171. case 2:
  172. nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE),
  173. 0UL) : REMOTE_HUB_S(nasid, SH_RTC3_INT_ENABLE, 0UL);
  174. break;
  175. default:
  176. return -EFAULT;
  177. }
  178. return 0;
  179. }
  180. #define COMPARATOR 1 /* The comparator to use */
  181. #define TIMER_OFF 0xbadcabLL /* Timer is not setup */
  182. #define TIMER_SET 0 /* Comparator is set for this timer */
  183. /* There is one of these for each timer */
  184. struct mmtimer {
  185. struct rb_node list;
  186. struct k_itimer *timer;
  187. int cpu;
  188. };
  189. struct mmtimer_node {
  190. spinlock_t lock ____cacheline_aligned;
  191. struct rb_root timer_head;
  192. struct rb_node *next;
  193. struct tasklet_struct tasklet;
  194. };
  195. static struct mmtimer_node *timers;
  196. /*
  197. * Add a new mmtimer struct to the node's mmtimer list.
  198. * This function assumes the struct mmtimer_node is locked.
  199. */
  200. static void mmtimer_add_list(struct mmtimer *n)
  201. {
  202. int nodeid = n->timer->it.mmtimer.node;
  203. unsigned long expires = n->timer->it.mmtimer.expires;
  204. struct rb_node **link = &timers[nodeid].timer_head.rb_node;
  205. struct rb_node *parent = NULL;
  206. struct mmtimer *x;
  207. /*
  208. * Find the right place in the rbtree:
  209. */
  210. while (*link) {
  211. parent = *link;
  212. x = rb_entry(parent, struct mmtimer, list);
  213. if (expires < x->timer->it.mmtimer.expires)
  214. link = &(*link)->rb_left;
  215. else
  216. link = &(*link)->rb_right;
  217. }
  218. /*
  219. * Insert the timer to the rbtree and check whether it
  220. * replaces the first pending timer
  221. */
  222. rb_link_node(&n->list, parent, link);
  223. rb_insert_color(&n->list, &timers[nodeid].timer_head);
  224. if (!timers[nodeid].next || expires < rb_entry(timers[nodeid].next,
  225. struct mmtimer, list)->timer->it.mmtimer.expires)
  226. timers[nodeid].next = &n->list;
  227. }
  228. /*
  229. * Set the comparator for the next timer.
  230. * This function assumes the struct mmtimer_node is locked.
  231. */
  232. static void mmtimer_set_next_timer(int nodeid)
  233. {
  234. struct mmtimer_node *n = &timers[nodeid];
  235. struct mmtimer *x;
  236. struct k_itimer *t;
  237. int o;
  238. restart:
  239. if (n->next == NULL)
  240. return;
  241. x = rb_entry(n->next, struct mmtimer, list);
  242. t = x->timer;
  243. if (!t->it.mmtimer.incr) {
  244. /* Not an interval timer */
  245. if (!mmtimer_setup(x->cpu, COMPARATOR,
  246. t->it.mmtimer.expires)) {
  247. /* Late setup, fire now */
  248. tasklet_schedule(&n->tasklet);
  249. }
  250. return;
  251. }
  252. /* Interval timer */
  253. o = 0;
  254. while (!mmtimer_setup(x->cpu, COMPARATOR, t->it.mmtimer.expires)) {
  255. unsigned long e, e1;
  256. struct rb_node *next;
  257. t->it.mmtimer.expires += t->it.mmtimer.incr << o;
  258. t->it_overrun += 1 << o;
  259. o++;
  260. if (o > 20) {
  261. printk(KERN_ALERT "mmtimer: cannot reschedule timer\n");
  262. t->it.mmtimer.clock = TIMER_OFF;
  263. n->next = rb_next(&x->list);
  264. rb_erase(&x->list, &n->timer_head);
  265. kfree(x);
  266. goto restart;
  267. }
  268. e = t->it.mmtimer.expires;
  269. next = rb_next(&x->list);
  270. if (next == NULL)
  271. continue;
  272. e1 = rb_entry(next, struct mmtimer, list)->
  273. timer->it.mmtimer.expires;
  274. if (e > e1) {
  275. n->next = next;
  276. rb_erase(&x->list, &n->timer_head);
  277. mmtimer_add_list(x);
  278. goto restart;
  279. }
  280. }
  281. }
  282. /**
  283. * mmtimer_ioctl - ioctl interface for /dev/mmtimer
  284. * @file: file structure for the device
  285. * @cmd: command to execute
  286. * @arg: optional argument to command
  287. *
  288. * Executes the command specified by @cmd. Returns 0 for success, < 0 for
  289. * failure.
  290. *
  291. * Valid commands:
  292. *
  293. * %MMTIMER_GETOFFSET - Should return the offset (relative to the start
  294. * of the page where the registers are mapped) for the counter in question.
  295. *
  296. * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
  297. * seconds
  298. *
  299. * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
  300. * specified by @arg
  301. *
  302. * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
  303. *
  304. * %MMTIMER_MMAPAVAIL - Returns 1 if the registers can be mmap'd into userspace
  305. *
  306. * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
  307. * in the address specified by @arg.
  308. */
  309. static long mmtimer_ioctl(struct file *file, unsigned int cmd,
  310. unsigned long arg)
  311. {
  312. int ret = 0;
  313. lock_kernel();
  314. switch (cmd) {
  315. case MMTIMER_GETOFFSET: /* offset of the counter */
  316. /*
  317. * SN RTC registers are on their own 64k page
  318. */
  319. if(PAGE_SIZE <= (1 << 16))
  320. ret = (((long)RTC_COUNTER_ADDR) & (PAGE_SIZE-1)) / 8;
  321. else
  322. ret = -ENOSYS;
  323. break;
  324. case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */
  325. if(copy_to_user((unsigned long __user *)arg,
  326. &mmtimer_femtoperiod, sizeof(unsigned long)))
  327. ret = -EFAULT;
  328. break;
  329. case MMTIMER_GETFREQ: /* frequency in Hz */
  330. if(copy_to_user((unsigned long __user *)arg,
  331. &sn_rtc_cycles_per_second,
  332. sizeof(unsigned long)))
  333. ret = -EFAULT;
  334. break;
  335. case MMTIMER_GETBITS: /* number of bits in the clock */
  336. ret = RTC_BITS;
  337. break;
  338. case MMTIMER_MMAPAVAIL: /* can we mmap the clock into userspace? */
  339. ret = (PAGE_SIZE <= (1 << 16)) ? 1 : 0;
  340. break;
  341. case MMTIMER_GETCOUNTER:
  342. if(copy_to_user((unsigned long __user *)arg,
  343. RTC_COUNTER_ADDR, sizeof(unsigned long)))
  344. ret = -EFAULT;
  345. break;
  346. default:
  347. ret = -ENOTTY;
  348. break;
  349. }
  350. unlock_kernel();
  351. return ret;
  352. }
  353. /**
  354. * mmtimer_mmap - maps the clock's registers into userspace
  355. * @file: file structure for the device
  356. * @vma: VMA to map the registers into
  357. *
  358. * Calls remap_pfn_range() to map the clock's registers into
  359. * the calling process' address space.
  360. */
  361. static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
  362. {
  363. unsigned long mmtimer_addr;
  364. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  365. return -EINVAL;
  366. if (vma->vm_flags & VM_WRITE)
  367. return -EPERM;
  368. if (PAGE_SIZE > (1 << 16))
  369. return -ENOSYS;
  370. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  371. mmtimer_addr = __pa(RTC_COUNTER_ADDR);
  372. mmtimer_addr &= ~(PAGE_SIZE - 1);
  373. mmtimer_addr &= 0xfffffffffffffffUL;
  374. if (remap_pfn_range(vma, vma->vm_start, mmtimer_addr >> PAGE_SHIFT,
  375. PAGE_SIZE, vma->vm_page_prot)) {
  376. printk(KERN_ERR "remap_pfn_range failed in mmtimer.c\n");
  377. return -EAGAIN;
  378. }
  379. return 0;
  380. }
  381. static struct miscdevice mmtimer_miscdev = {
  382. SGI_MMTIMER,
  383. MMTIMER_NAME,
  384. &mmtimer_fops
  385. };
  386. static struct timespec sgi_clock_offset;
  387. static int sgi_clock_period;
  388. /*
  389. * Posix Timer Interface
  390. */
  391. static struct timespec sgi_clock_offset;
  392. static int sgi_clock_period;
  393. static int sgi_clock_get(clockid_t clockid, struct timespec *tp)
  394. {
  395. u64 nsec;
  396. nsec = rtc_time() * sgi_clock_period
  397. + sgi_clock_offset.tv_nsec;
  398. *tp = ns_to_timespec(nsec);
  399. tp->tv_sec += sgi_clock_offset.tv_sec;
  400. return 0;
  401. };
  402. static int sgi_clock_set(clockid_t clockid, struct timespec *tp)
  403. {
  404. u64 nsec;
  405. u32 rem;
  406. nsec = rtc_time() * sgi_clock_period;
  407. sgi_clock_offset.tv_sec = tp->tv_sec - div_u64_rem(nsec, NSEC_PER_SEC, &rem);
  408. if (rem <= tp->tv_nsec)
  409. sgi_clock_offset.tv_nsec = tp->tv_sec - rem;
  410. else {
  411. sgi_clock_offset.tv_nsec = tp->tv_sec + NSEC_PER_SEC - rem;
  412. sgi_clock_offset.tv_sec--;
  413. }
  414. return 0;
  415. }
  416. /**
  417. * mmtimer_interrupt - timer interrupt handler
  418. * @irq: irq received
  419. * @dev_id: device the irq came from
  420. *
  421. * Called when one of the comarators matches the counter, This
  422. * routine will send signals to processes that have requested
  423. * them.
  424. *
  425. * This interrupt is run in an interrupt context
  426. * by the SHUB. It is therefore safe to locally access SHub
  427. * registers.
  428. */
  429. static irqreturn_t
  430. mmtimer_interrupt(int irq, void *dev_id)
  431. {
  432. unsigned long expires = 0;
  433. int result = IRQ_NONE;
  434. unsigned indx = cpu_to_node(smp_processor_id());
  435. struct mmtimer *base;
  436. spin_lock(&timers[indx].lock);
  437. base = rb_entry(timers[indx].next, struct mmtimer, list);
  438. if (base == NULL) {
  439. spin_unlock(&timers[indx].lock);
  440. return result;
  441. }
  442. if (base->cpu == smp_processor_id()) {
  443. if (base->timer)
  444. expires = base->timer->it.mmtimer.expires;
  445. /* expires test won't work with shared irqs */
  446. if ((mmtimer_int_pending(COMPARATOR) > 0) ||
  447. (expires && (expires <= rtc_time()))) {
  448. mmtimer_clr_int_pending(COMPARATOR);
  449. tasklet_schedule(&timers[indx].tasklet);
  450. result = IRQ_HANDLED;
  451. }
  452. }
  453. spin_unlock(&timers[indx].lock);
  454. return result;
  455. }
  456. static void mmtimer_tasklet(unsigned long data)
  457. {
  458. int nodeid = data;
  459. struct mmtimer_node *mn = &timers[nodeid];
  460. struct mmtimer *x;
  461. struct k_itimer *t;
  462. unsigned long flags;
  463. /* Send signal and deal with periodic signals */
  464. spin_lock_irqsave(&mn->lock, flags);
  465. if (!mn->next)
  466. goto out;
  467. x = rb_entry(mn->next, struct mmtimer, list);
  468. t = x->timer;
  469. if (t->it.mmtimer.clock == TIMER_OFF)
  470. goto out;
  471. t->it_overrun = 0;
  472. mn->next = rb_next(&x->list);
  473. rb_erase(&x->list, &mn->timer_head);
  474. if (posix_timer_event(t, 0) != 0)
  475. t->it_overrun++;
  476. if(t->it.mmtimer.incr) {
  477. t->it.mmtimer.expires += t->it.mmtimer.incr;
  478. mmtimer_add_list(x);
  479. } else {
  480. /* Ensure we don't false trigger in mmtimer_interrupt */
  481. t->it.mmtimer.clock = TIMER_OFF;
  482. t->it.mmtimer.expires = 0;
  483. kfree(x);
  484. }
  485. /* Set comparator for next timer, if there is one */
  486. mmtimer_set_next_timer(nodeid);
  487. t->it_overrun_last = t->it_overrun;
  488. out:
  489. spin_unlock_irqrestore(&mn->lock, flags);
  490. }
  491. static int sgi_timer_create(struct k_itimer *timer)
  492. {
  493. /* Insure that a newly created timer is off */
  494. timer->it.mmtimer.clock = TIMER_OFF;
  495. return 0;
  496. }
  497. /* This does not really delete a timer. It just insures
  498. * that the timer is not active
  499. *
  500. * Assumption: it_lock is already held with irq's disabled
  501. */
  502. static int sgi_timer_del(struct k_itimer *timr)
  503. {
  504. cnodeid_t nodeid = timr->it.mmtimer.node;
  505. unsigned long irqflags;
  506. spin_lock_irqsave(&timers[nodeid].lock, irqflags);
  507. if (timr->it.mmtimer.clock != TIMER_OFF) {
  508. unsigned long expires = timr->it.mmtimer.expires;
  509. struct rb_node *n = timers[nodeid].timer_head.rb_node;
  510. struct mmtimer *uninitialized_var(t);
  511. int r = 0;
  512. timr->it.mmtimer.clock = TIMER_OFF;
  513. timr->it.mmtimer.expires = 0;
  514. while (n) {
  515. t = rb_entry(n, struct mmtimer, list);
  516. if (t->timer == timr)
  517. break;
  518. if (expires < t->timer->it.mmtimer.expires)
  519. n = n->rb_left;
  520. else
  521. n = n->rb_right;
  522. }
  523. if (!n) {
  524. spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
  525. return 0;
  526. }
  527. if (timers[nodeid].next == n) {
  528. timers[nodeid].next = rb_next(n);
  529. r = 1;
  530. }
  531. rb_erase(n, &timers[nodeid].timer_head);
  532. kfree(t);
  533. if (r) {
  534. mmtimer_disable_int(cnodeid_to_nasid(nodeid),
  535. COMPARATOR);
  536. mmtimer_set_next_timer(nodeid);
  537. }
  538. }
  539. spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
  540. return 0;
  541. }
  542. /* Assumption: it_lock is already held with irq's disabled */
  543. static void sgi_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting)
  544. {
  545. if (timr->it.mmtimer.clock == TIMER_OFF) {
  546. cur_setting->it_interval.tv_nsec = 0;
  547. cur_setting->it_interval.tv_sec = 0;
  548. cur_setting->it_value.tv_nsec = 0;
  549. cur_setting->it_value.tv_sec =0;
  550. return;
  551. }
  552. cur_setting->it_interval = ns_to_timespec(timr->it.mmtimer.incr * sgi_clock_period);
  553. cur_setting->it_value = ns_to_timespec((timr->it.mmtimer.expires - rtc_time()) * sgi_clock_period);
  554. }
  555. static int sgi_timer_set(struct k_itimer *timr, int flags,
  556. struct itimerspec * new_setting,
  557. struct itimerspec * old_setting)
  558. {
  559. unsigned long when, period, irqflags;
  560. int err = 0;
  561. cnodeid_t nodeid;
  562. struct mmtimer *base;
  563. struct rb_node *n;
  564. if (old_setting)
  565. sgi_timer_get(timr, old_setting);
  566. sgi_timer_del(timr);
  567. when = timespec_to_ns(&new_setting->it_value);
  568. period = timespec_to_ns(&new_setting->it_interval);
  569. if (when == 0)
  570. /* Clear timer */
  571. return 0;
  572. base = kmalloc(sizeof(struct mmtimer), GFP_KERNEL);
  573. if (base == NULL)
  574. return -ENOMEM;
  575. if (flags & TIMER_ABSTIME) {
  576. struct timespec n;
  577. unsigned long now;
  578. getnstimeofday(&n);
  579. now = timespec_to_ns(&n);
  580. if (when > now)
  581. when -= now;
  582. else
  583. /* Fire the timer immediately */
  584. when = 0;
  585. }
  586. /*
  587. * Convert to sgi clock period. Need to keep rtc_time() as near as possible
  588. * to getnstimeofday() in order to be as faithful as possible to the time
  589. * specified.
  590. */
  591. when = (when + sgi_clock_period - 1) / sgi_clock_period + rtc_time();
  592. period = (period + sgi_clock_period - 1) / sgi_clock_period;
  593. /*
  594. * We are allocating a local SHub comparator. If we would be moved to another
  595. * cpu then another SHub may be local to us. Prohibit that by switching off
  596. * preemption.
  597. */
  598. preempt_disable();
  599. nodeid = cpu_to_node(smp_processor_id());
  600. /* Lock the node timer structure */
  601. spin_lock_irqsave(&timers[nodeid].lock, irqflags);
  602. base->timer = timr;
  603. base->cpu = smp_processor_id();
  604. timr->it.mmtimer.clock = TIMER_SET;
  605. timr->it.mmtimer.node = nodeid;
  606. timr->it.mmtimer.incr = period;
  607. timr->it.mmtimer.expires = when;
  608. n = timers[nodeid].next;
  609. /* Add the new struct mmtimer to node's timer list */
  610. mmtimer_add_list(base);
  611. if (timers[nodeid].next == n) {
  612. /* No need to reprogram comparator for now */
  613. spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
  614. preempt_enable();
  615. return err;
  616. }
  617. /* We need to reprogram the comparator */
  618. if (n)
  619. mmtimer_disable_int(cnodeid_to_nasid(nodeid), COMPARATOR);
  620. mmtimer_set_next_timer(nodeid);
  621. /* Unlock the node timer structure */
  622. spin_unlock_irqrestore(&timers[nodeid].lock, irqflags);
  623. preempt_enable();
  624. return err;
  625. }
  626. static struct k_clock sgi_clock = {
  627. .res = 0,
  628. .clock_set = sgi_clock_set,
  629. .clock_get = sgi_clock_get,
  630. .timer_create = sgi_timer_create,
  631. .nsleep = do_posix_clock_nonanosleep,
  632. .timer_set = sgi_timer_set,
  633. .timer_del = sgi_timer_del,
  634. .timer_get = sgi_timer_get
  635. };
  636. /**
  637. * mmtimer_init - device initialization routine
  638. *
  639. * Does initial setup for the mmtimer device.
  640. */
  641. static int __init mmtimer_init(void)
  642. {
  643. cnodeid_t node, maxn = -1;
  644. if (!ia64_platform_is("sn2"))
  645. return 0;
  646. /*
  647. * Sanity check the cycles/sec variable
  648. */
  649. if (sn_rtc_cycles_per_second < 100000) {
  650. printk(KERN_ERR "%s: unable to determine clock frequency\n",
  651. MMTIMER_NAME);
  652. goto out1;
  653. }
  654. mmtimer_femtoperiod = ((unsigned long)1E15 + sn_rtc_cycles_per_second /
  655. 2) / sn_rtc_cycles_per_second;
  656. if (request_irq(SGI_MMTIMER_VECTOR, mmtimer_interrupt, IRQF_PERCPU, MMTIMER_NAME, NULL)) {
  657. printk(KERN_WARNING "%s: unable to allocate interrupt.",
  658. MMTIMER_NAME);
  659. goto out1;
  660. }
  661. if (misc_register(&mmtimer_miscdev)) {
  662. printk(KERN_ERR "%s: failed to register device\n",
  663. MMTIMER_NAME);
  664. goto out2;
  665. }
  666. /* Get max numbered node, calculate slots needed */
  667. for_each_online_node(node) {
  668. maxn = node;
  669. }
  670. maxn++;
  671. /* Allocate list of node ptrs to mmtimer_t's */
  672. timers = kzalloc(sizeof(struct mmtimer_node)*maxn, GFP_KERNEL);
  673. if (timers == NULL) {
  674. printk(KERN_ERR "%s: failed to allocate memory for device\n",
  675. MMTIMER_NAME);
  676. goto out3;
  677. }
  678. /* Initialize struct mmtimer's for each online node */
  679. for_each_online_node(node) {
  680. spin_lock_init(&timers[node].lock);
  681. tasklet_init(&timers[node].tasklet, mmtimer_tasklet,
  682. (unsigned long) node);
  683. }
  684. sgi_clock_period = sgi_clock.res = NSEC_PER_SEC / sn_rtc_cycles_per_second;
  685. register_posix_clock(CLOCK_SGI_CYCLE, &sgi_clock);
  686. printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION,
  687. sn_rtc_cycles_per_second/(unsigned long)1E6);
  688. return 0;
  689. out3:
  690. kfree(timers);
  691. misc_deregister(&mmtimer_miscdev);
  692. out2:
  693. free_irq(SGI_MMTIMER_VECTOR, NULL);
  694. out1:
  695. return -1;
  696. }
  697. module_init(mmtimer_init);