mmtimer.c 20 KB

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