mmtimer.c 18 KB

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