mmtimer.c 21 KB

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