mmtimer.c 20 KB

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