mmtimer.c 21 KB

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