hpet.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel & MS High Precision Event Timer Implementation.
  4. *
  5. * Copyright (C) 2003 Intel Corporation
  6. * Venki Pallipadi
  7. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  8. * Bob Picco <[email protected]>
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/major.h>
  15. #include <linux/ioport.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/init.h>
  18. #include <linux/io-64-nonatomic-lo-hi.h>
  19. #include <linux/poll.h>
  20. #include <linux/mm.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/wait.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/bcd.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/bitops.h>
  29. #include <linux/compat.h>
  30. #include <linux/clocksource.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/slab.h>
  33. #include <linux/io.h>
  34. #include <linux/acpi.h>
  35. #include <linux/hpet.h>
  36. #include <asm/current.h>
  37. #include <asm/irq.h>
  38. #include <asm/div64.h>
  39. /*
  40. * The High Precision Event Timer driver.
  41. * This driver is closely modelled after the rtc.c driver.
  42. * See HPET spec revision 1.
  43. */
  44. #define HPET_USER_FREQ (64)
  45. #define HPET_DRIFT (500)
  46. #define HPET_RANGE_SIZE 1024 /* from HPET spec */
  47. /* WARNING -- don't get confused. These macros are never used
  48. * to write the (single) counter, and rarely to read it.
  49. * They're badly named; to fix, someday.
  50. */
  51. #if BITS_PER_LONG == 64
  52. #define write_counter(V, MC) writeq(V, MC)
  53. #define read_counter(MC) readq(MC)
  54. #else
  55. #define write_counter(V, MC) writel(V, MC)
  56. #define read_counter(MC) readl(MC)
  57. #endif
  58. static DEFINE_MUTEX(hpet_mutex); /* replaces BKL */
  59. static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
  60. /* This clocksource driver currently only works on ia64 */
  61. #ifdef CONFIG_IA64
  62. static void __iomem *hpet_mctr;
  63. static u64 read_hpet(struct clocksource *cs)
  64. {
  65. return (u64)read_counter((void __iomem *)hpet_mctr);
  66. }
  67. static struct clocksource clocksource_hpet = {
  68. .name = "hpet",
  69. .rating = 250,
  70. .read = read_hpet,
  71. .mask = CLOCKSOURCE_MASK(64),
  72. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  73. };
  74. static struct clocksource *hpet_clocksource;
  75. #endif
  76. /* A lock for concurrent access by app and isr hpet activity. */
  77. static DEFINE_SPINLOCK(hpet_lock);
  78. #define HPET_DEV_NAME (7)
  79. struct hpet_dev {
  80. struct hpets *hd_hpets;
  81. struct hpet __iomem *hd_hpet;
  82. struct hpet_timer __iomem *hd_timer;
  83. unsigned long hd_ireqfreq;
  84. unsigned long hd_irqdata;
  85. wait_queue_head_t hd_waitqueue;
  86. struct fasync_struct *hd_async_queue;
  87. unsigned int hd_flags;
  88. unsigned int hd_irq;
  89. unsigned int hd_hdwirq;
  90. char hd_name[HPET_DEV_NAME];
  91. };
  92. struct hpets {
  93. struct hpets *hp_next;
  94. struct hpet __iomem *hp_hpet;
  95. unsigned long hp_hpet_phys;
  96. struct clocksource *hp_clocksource;
  97. unsigned long long hp_tick_freq;
  98. unsigned long hp_delta;
  99. unsigned int hp_ntimer;
  100. unsigned int hp_which;
  101. struct hpet_dev hp_dev[];
  102. };
  103. static struct hpets *hpets;
  104. #define HPET_OPEN 0x0001
  105. #define HPET_IE 0x0002 /* interrupt enabled */
  106. #define HPET_PERIODIC 0x0004
  107. #define HPET_SHARED_IRQ 0x0008
  108. static irqreturn_t hpet_interrupt(int irq, void *data)
  109. {
  110. struct hpet_dev *devp;
  111. unsigned long isr;
  112. devp = data;
  113. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  114. if ((devp->hd_flags & HPET_SHARED_IRQ) &&
  115. !(isr & readl(&devp->hd_hpet->hpet_isr)))
  116. return IRQ_NONE;
  117. spin_lock(&hpet_lock);
  118. devp->hd_irqdata++;
  119. /*
  120. * For non-periodic timers, increment the accumulator.
  121. * This has the effect of treating non-periodic like periodic.
  122. */
  123. if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
  124. unsigned long t, mc, base, k;
  125. struct hpet __iomem *hpet = devp->hd_hpet;
  126. struct hpets *hpetp = devp->hd_hpets;
  127. t = devp->hd_ireqfreq;
  128. read_counter(&devp->hd_timer->hpet_compare);
  129. mc = read_counter(&hpet->hpet_mc);
  130. /* The time for the next interrupt would logically be t + m,
  131. * however, if we are very unlucky and the interrupt is delayed
  132. * for longer than t then we will completely miss the next
  133. * interrupt if we set t + m and an application will hang.
  134. * Therefore we need to make a more complex computation assuming
  135. * that there exists a k for which the following is true:
  136. * k * t + base < mc + delta
  137. * (k + 1) * t + base > mc + delta
  138. * where t is the interval in hpet ticks for the given freq,
  139. * base is the theoretical start value 0 < base < t,
  140. * mc is the main counter value at the time of the interrupt,
  141. * delta is the time it takes to write the a value to the
  142. * comparator.
  143. * k may then be computed as (mc - base + delta) / t .
  144. */
  145. base = mc % t;
  146. k = (mc - base + hpetp->hp_delta) / t;
  147. write_counter(t * (k + 1) + base,
  148. &devp->hd_timer->hpet_compare);
  149. }
  150. if (devp->hd_flags & HPET_SHARED_IRQ)
  151. writel(isr, &devp->hd_hpet->hpet_isr);
  152. spin_unlock(&hpet_lock);
  153. wake_up_interruptible(&devp->hd_waitqueue);
  154. kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
  155. return IRQ_HANDLED;
  156. }
  157. static void hpet_timer_set_irq(struct hpet_dev *devp)
  158. {
  159. unsigned long v;
  160. int irq, gsi;
  161. struct hpet_timer __iomem *timer;
  162. spin_lock_irq(&hpet_lock);
  163. if (devp->hd_hdwirq) {
  164. spin_unlock_irq(&hpet_lock);
  165. return;
  166. }
  167. timer = devp->hd_timer;
  168. /* we prefer level triggered mode */
  169. v = readl(&timer->hpet_config);
  170. if (!(v & Tn_INT_TYPE_CNF_MASK)) {
  171. v |= Tn_INT_TYPE_CNF_MASK;
  172. writel(v, &timer->hpet_config);
  173. }
  174. spin_unlock_irq(&hpet_lock);
  175. v = (readq(&timer->hpet_config) & Tn_INT_ROUTE_CAP_MASK) >>
  176. Tn_INT_ROUTE_CAP_SHIFT;
  177. /*
  178. * In PIC mode, skip IRQ0-4, IRQ6-9, IRQ12-15 which is always used by
  179. * legacy device. In IO APIC mode, we skip all the legacy IRQS.
  180. */
  181. if (acpi_irq_model == ACPI_IRQ_MODEL_PIC)
  182. v &= ~0xf3df;
  183. else
  184. v &= ~0xffff;
  185. for_each_set_bit(irq, &v, HPET_MAX_IRQ) {
  186. if (irq >= nr_irqs) {
  187. irq = HPET_MAX_IRQ;
  188. break;
  189. }
  190. gsi = acpi_register_gsi(NULL, irq, ACPI_LEVEL_SENSITIVE,
  191. ACPI_ACTIVE_LOW);
  192. if (gsi > 0)
  193. break;
  194. /* FIXME: Setup interrupt source table */
  195. }
  196. if (irq < HPET_MAX_IRQ) {
  197. spin_lock_irq(&hpet_lock);
  198. v = readl(&timer->hpet_config);
  199. v |= irq << Tn_INT_ROUTE_CNF_SHIFT;
  200. writel(v, &timer->hpet_config);
  201. devp->hd_hdwirq = gsi;
  202. spin_unlock_irq(&hpet_lock);
  203. }
  204. return;
  205. }
  206. static int hpet_open(struct inode *inode, struct file *file)
  207. {
  208. struct hpet_dev *devp;
  209. struct hpets *hpetp;
  210. int i;
  211. if (file->f_mode & FMODE_WRITE)
  212. return -EINVAL;
  213. mutex_lock(&hpet_mutex);
  214. spin_lock_irq(&hpet_lock);
  215. for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
  216. for (i = 0; i < hpetp->hp_ntimer; i++)
  217. if (hpetp->hp_dev[i].hd_flags & HPET_OPEN) {
  218. continue;
  219. } else {
  220. devp = &hpetp->hp_dev[i];
  221. break;
  222. }
  223. if (!devp) {
  224. spin_unlock_irq(&hpet_lock);
  225. mutex_unlock(&hpet_mutex);
  226. return -EBUSY;
  227. }
  228. file->private_data = devp;
  229. devp->hd_irqdata = 0;
  230. devp->hd_flags |= HPET_OPEN;
  231. spin_unlock_irq(&hpet_lock);
  232. mutex_unlock(&hpet_mutex);
  233. hpet_timer_set_irq(devp);
  234. return 0;
  235. }
  236. static ssize_t
  237. hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
  238. {
  239. DECLARE_WAITQUEUE(wait, current);
  240. unsigned long data;
  241. ssize_t retval;
  242. struct hpet_dev *devp;
  243. devp = file->private_data;
  244. if (!devp->hd_ireqfreq)
  245. return -EIO;
  246. if (count < sizeof(unsigned long))
  247. return -EINVAL;
  248. add_wait_queue(&devp->hd_waitqueue, &wait);
  249. for ( ; ; ) {
  250. set_current_state(TASK_INTERRUPTIBLE);
  251. spin_lock_irq(&hpet_lock);
  252. data = devp->hd_irqdata;
  253. devp->hd_irqdata = 0;
  254. spin_unlock_irq(&hpet_lock);
  255. if (data) {
  256. break;
  257. } else if (file->f_flags & O_NONBLOCK) {
  258. retval = -EAGAIN;
  259. goto out;
  260. } else if (signal_pending(current)) {
  261. retval = -ERESTARTSYS;
  262. goto out;
  263. }
  264. schedule();
  265. }
  266. retval = put_user(data, (unsigned long __user *)buf);
  267. if (!retval)
  268. retval = sizeof(unsigned long);
  269. out:
  270. __set_current_state(TASK_RUNNING);
  271. remove_wait_queue(&devp->hd_waitqueue, &wait);
  272. return retval;
  273. }
  274. static __poll_t hpet_poll(struct file *file, poll_table * wait)
  275. {
  276. unsigned long v;
  277. struct hpet_dev *devp;
  278. devp = file->private_data;
  279. if (!devp->hd_ireqfreq)
  280. return 0;
  281. poll_wait(file, &devp->hd_waitqueue, wait);
  282. spin_lock_irq(&hpet_lock);
  283. v = devp->hd_irqdata;
  284. spin_unlock_irq(&hpet_lock);
  285. if (v != 0)
  286. return EPOLLIN | EPOLLRDNORM;
  287. return 0;
  288. }
  289. #ifdef CONFIG_HPET_MMAP
  290. #ifdef CONFIG_HPET_MMAP_DEFAULT
  291. static int hpet_mmap_enabled = 1;
  292. #else
  293. static int hpet_mmap_enabled = 0;
  294. #endif
  295. static __init int hpet_mmap_enable(char *str)
  296. {
  297. get_option(&str, &hpet_mmap_enabled);
  298. pr_info("HPET mmap %s\n", hpet_mmap_enabled ? "enabled" : "disabled");
  299. return 1;
  300. }
  301. __setup("hpet_mmap=", hpet_mmap_enable);
  302. static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
  303. {
  304. struct hpet_dev *devp;
  305. unsigned long addr;
  306. if (!hpet_mmap_enabled)
  307. return -EACCES;
  308. devp = file->private_data;
  309. addr = devp->hd_hpets->hp_hpet_phys;
  310. if (addr & (PAGE_SIZE - 1))
  311. return -ENOSYS;
  312. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  313. return vm_iomap_memory(vma, addr, PAGE_SIZE);
  314. }
  315. #else
  316. static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
  317. {
  318. return -ENOSYS;
  319. }
  320. #endif
  321. static int hpet_fasync(int fd, struct file *file, int on)
  322. {
  323. struct hpet_dev *devp;
  324. devp = file->private_data;
  325. if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
  326. return 0;
  327. else
  328. return -EIO;
  329. }
  330. static int hpet_release(struct inode *inode, struct file *file)
  331. {
  332. struct hpet_dev *devp;
  333. struct hpet_timer __iomem *timer;
  334. int irq = 0;
  335. devp = file->private_data;
  336. timer = devp->hd_timer;
  337. spin_lock_irq(&hpet_lock);
  338. writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
  339. &timer->hpet_config);
  340. irq = devp->hd_irq;
  341. devp->hd_irq = 0;
  342. devp->hd_ireqfreq = 0;
  343. if (devp->hd_flags & HPET_PERIODIC
  344. && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
  345. unsigned long v;
  346. v = readq(&timer->hpet_config);
  347. v ^= Tn_TYPE_CNF_MASK;
  348. writeq(v, &timer->hpet_config);
  349. }
  350. devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
  351. spin_unlock_irq(&hpet_lock);
  352. if (irq)
  353. free_irq(irq, devp);
  354. file->private_data = NULL;
  355. return 0;
  356. }
  357. static int hpet_ioctl_ieon(struct hpet_dev *devp)
  358. {
  359. struct hpet_timer __iomem *timer;
  360. struct hpet __iomem *hpet;
  361. struct hpets *hpetp;
  362. int irq;
  363. unsigned long g, v, t, m;
  364. unsigned long flags, isr;
  365. timer = devp->hd_timer;
  366. hpet = devp->hd_hpet;
  367. hpetp = devp->hd_hpets;
  368. if (!devp->hd_ireqfreq)
  369. return -EIO;
  370. spin_lock_irq(&hpet_lock);
  371. if (devp->hd_flags & HPET_IE) {
  372. spin_unlock_irq(&hpet_lock);
  373. return -EBUSY;
  374. }
  375. devp->hd_flags |= HPET_IE;
  376. if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
  377. devp->hd_flags |= HPET_SHARED_IRQ;
  378. spin_unlock_irq(&hpet_lock);
  379. irq = devp->hd_hdwirq;
  380. if (irq) {
  381. unsigned long irq_flags;
  382. if (devp->hd_flags & HPET_SHARED_IRQ) {
  383. /*
  384. * To prevent the interrupt handler from seeing an
  385. * unwanted interrupt status bit, program the timer
  386. * so that it will not fire in the near future ...
  387. */
  388. writel(readl(&timer->hpet_config) & ~Tn_TYPE_CNF_MASK,
  389. &timer->hpet_config);
  390. write_counter(read_counter(&hpet->hpet_mc),
  391. &timer->hpet_compare);
  392. /* ... and clear any left-over status. */
  393. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  394. writel(isr, &hpet->hpet_isr);
  395. }
  396. sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
  397. irq_flags = devp->hd_flags & HPET_SHARED_IRQ ? IRQF_SHARED : 0;
  398. if (request_irq(irq, hpet_interrupt, irq_flags,
  399. devp->hd_name, (void *)devp)) {
  400. printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
  401. irq = 0;
  402. }
  403. }
  404. if (irq == 0) {
  405. spin_lock_irq(&hpet_lock);
  406. devp->hd_flags ^= HPET_IE;
  407. spin_unlock_irq(&hpet_lock);
  408. return -EIO;
  409. }
  410. devp->hd_irq = irq;
  411. t = devp->hd_ireqfreq;
  412. v = readq(&timer->hpet_config);
  413. /* 64-bit comparators are not yet supported through the ioctls,
  414. * so force this into 32-bit mode if it supports both modes
  415. */
  416. g = v | Tn_32MODE_CNF_MASK | Tn_INT_ENB_CNF_MASK;
  417. if (devp->hd_flags & HPET_PERIODIC) {
  418. g |= Tn_TYPE_CNF_MASK;
  419. v |= Tn_TYPE_CNF_MASK | Tn_VAL_SET_CNF_MASK;
  420. writeq(v, &timer->hpet_config);
  421. local_irq_save(flags);
  422. /*
  423. * NOTE: First we modify the hidden accumulator
  424. * register supported by periodic-capable comparators.
  425. * We never want to modify the (single) counter; that
  426. * would affect all the comparators. The value written
  427. * is the counter value when the first interrupt is due.
  428. */
  429. m = read_counter(&hpet->hpet_mc);
  430. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  431. /*
  432. * Then we modify the comparator, indicating the period
  433. * for subsequent interrupt.
  434. */
  435. write_counter(t, &timer->hpet_compare);
  436. } else {
  437. local_irq_save(flags);
  438. m = read_counter(&hpet->hpet_mc);
  439. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  440. }
  441. if (devp->hd_flags & HPET_SHARED_IRQ) {
  442. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  443. writel(isr, &hpet->hpet_isr);
  444. }
  445. writeq(g, &timer->hpet_config);
  446. local_irq_restore(flags);
  447. return 0;
  448. }
  449. /* converts Hz to number of timer ticks */
  450. static inline unsigned long hpet_time_div(struct hpets *hpets,
  451. unsigned long dis)
  452. {
  453. unsigned long long m;
  454. m = hpets->hp_tick_freq + (dis >> 1);
  455. return div64_ul(m, dis);
  456. }
  457. static int
  458. hpet_ioctl_common(struct hpet_dev *devp, unsigned int cmd, unsigned long arg,
  459. struct hpet_info *info)
  460. {
  461. struct hpet_timer __iomem *timer;
  462. struct hpets *hpetp;
  463. int err;
  464. unsigned long v;
  465. switch (cmd) {
  466. case HPET_IE_OFF:
  467. case HPET_INFO:
  468. case HPET_EPI:
  469. case HPET_DPI:
  470. case HPET_IRQFREQ:
  471. timer = devp->hd_timer;
  472. hpetp = devp->hd_hpets;
  473. break;
  474. case HPET_IE_ON:
  475. return hpet_ioctl_ieon(devp);
  476. default:
  477. return -EINVAL;
  478. }
  479. err = 0;
  480. switch (cmd) {
  481. case HPET_IE_OFF:
  482. if ((devp->hd_flags & HPET_IE) == 0)
  483. break;
  484. v = readq(&timer->hpet_config);
  485. v &= ~Tn_INT_ENB_CNF_MASK;
  486. writeq(v, &timer->hpet_config);
  487. if (devp->hd_irq) {
  488. free_irq(devp->hd_irq, devp);
  489. devp->hd_irq = 0;
  490. }
  491. devp->hd_flags ^= HPET_IE;
  492. break;
  493. case HPET_INFO:
  494. {
  495. memset(info, 0, sizeof(*info));
  496. if (devp->hd_ireqfreq)
  497. info->hi_ireqfreq =
  498. hpet_time_div(hpetp, devp->hd_ireqfreq);
  499. info->hi_flags =
  500. readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
  501. info->hi_hpet = hpetp->hp_which;
  502. info->hi_timer = devp - hpetp->hp_dev;
  503. break;
  504. }
  505. case HPET_EPI:
  506. v = readq(&timer->hpet_config);
  507. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  508. err = -ENXIO;
  509. break;
  510. }
  511. devp->hd_flags |= HPET_PERIODIC;
  512. break;
  513. case HPET_DPI:
  514. v = readq(&timer->hpet_config);
  515. if ((v & Tn_PER_INT_CAP_MASK) == 0) {
  516. err = -ENXIO;
  517. break;
  518. }
  519. if (devp->hd_flags & HPET_PERIODIC &&
  520. readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
  521. v = readq(&timer->hpet_config);
  522. v ^= Tn_TYPE_CNF_MASK;
  523. writeq(v, &timer->hpet_config);
  524. }
  525. devp->hd_flags &= ~HPET_PERIODIC;
  526. break;
  527. case HPET_IRQFREQ:
  528. if ((arg > hpet_max_freq) &&
  529. !capable(CAP_SYS_RESOURCE)) {
  530. err = -EACCES;
  531. break;
  532. }
  533. if (!arg) {
  534. err = -EINVAL;
  535. break;
  536. }
  537. devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
  538. }
  539. return err;
  540. }
  541. static long
  542. hpet_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  543. {
  544. struct hpet_info info;
  545. int err;
  546. mutex_lock(&hpet_mutex);
  547. err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
  548. mutex_unlock(&hpet_mutex);
  549. if ((cmd == HPET_INFO) && !err &&
  550. (copy_to_user((void __user *)arg, &info, sizeof(info))))
  551. err = -EFAULT;
  552. return err;
  553. }
  554. #ifdef CONFIG_COMPAT
  555. struct compat_hpet_info {
  556. compat_ulong_t hi_ireqfreq; /* Hz */
  557. compat_ulong_t hi_flags; /* information */
  558. unsigned short hi_hpet;
  559. unsigned short hi_timer;
  560. };
  561. static long
  562. hpet_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  563. {
  564. struct hpet_info info;
  565. int err;
  566. mutex_lock(&hpet_mutex);
  567. err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
  568. mutex_unlock(&hpet_mutex);
  569. if ((cmd == HPET_INFO) && !err) {
  570. struct compat_hpet_info __user *u = compat_ptr(arg);
  571. if (put_user(info.hi_ireqfreq, &u->hi_ireqfreq) ||
  572. put_user(info.hi_flags, &u->hi_flags) ||
  573. put_user(info.hi_hpet, &u->hi_hpet) ||
  574. put_user(info.hi_timer, &u->hi_timer))
  575. err = -EFAULT;
  576. }
  577. return err;
  578. }
  579. #endif
  580. static const struct file_operations hpet_fops = {
  581. .owner = THIS_MODULE,
  582. .llseek = no_llseek,
  583. .read = hpet_read,
  584. .poll = hpet_poll,
  585. .unlocked_ioctl = hpet_ioctl,
  586. #ifdef CONFIG_COMPAT
  587. .compat_ioctl = hpet_compat_ioctl,
  588. #endif
  589. .open = hpet_open,
  590. .release = hpet_release,
  591. .fasync = hpet_fasync,
  592. .mmap = hpet_mmap,
  593. };
  594. static int hpet_is_known(struct hpet_data *hdp)
  595. {
  596. struct hpets *hpetp;
  597. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  598. if (hpetp->hp_hpet_phys == hdp->hd_phys_address)
  599. return 1;
  600. return 0;
  601. }
  602. static struct ctl_table hpet_table[] = {
  603. {
  604. .procname = "max-user-freq",
  605. .data = &hpet_max_freq,
  606. .maxlen = sizeof(int),
  607. .mode = 0644,
  608. .proc_handler = proc_dointvec,
  609. },
  610. {}
  611. };
  612. static struct ctl_table_header *sysctl_header;
  613. /*
  614. * Adjustment for when arming the timer with
  615. * initial conditions. That is, main counter
  616. * ticks expired before interrupts are enabled.
  617. */
  618. #define TICK_CALIBRATE (1000UL)
  619. static unsigned long __hpet_calibrate(struct hpets *hpetp)
  620. {
  621. struct hpet_timer __iomem *timer = NULL;
  622. unsigned long t, m, count, i, flags, start;
  623. struct hpet_dev *devp;
  624. int j;
  625. struct hpet __iomem *hpet;
  626. for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
  627. if ((devp->hd_flags & HPET_OPEN) == 0) {
  628. timer = devp->hd_timer;
  629. break;
  630. }
  631. if (!timer)
  632. return 0;
  633. hpet = hpetp->hp_hpet;
  634. t = read_counter(&timer->hpet_compare);
  635. i = 0;
  636. count = hpet_time_div(hpetp, TICK_CALIBRATE);
  637. local_irq_save(flags);
  638. start = read_counter(&hpet->hpet_mc);
  639. do {
  640. m = read_counter(&hpet->hpet_mc);
  641. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  642. } while (i++, (m - start) < count);
  643. local_irq_restore(flags);
  644. return (m - start) / i;
  645. }
  646. static unsigned long hpet_calibrate(struct hpets *hpetp)
  647. {
  648. unsigned long ret = ~0UL;
  649. unsigned long tmp;
  650. /*
  651. * Try to calibrate until return value becomes stable small value.
  652. * If SMI interruption occurs in calibration loop, the return value
  653. * will be big. This avoids its impact.
  654. */
  655. for ( ; ; ) {
  656. tmp = __hpet_calibrate(hpetp);
  657. if (ret <= tmp)
  658. break;
  659. ret = tmp;
  660. }
  661. return ret;
  662. }
  663. int hpet_alloc(struct hpet_data *hdp)
  664. {
  665. u64 cap, mcfg;
  666. struct hpet_dev *devp;
  667. u32 i, ntimer;
  668. struct hpets *hpetp;
  669. struct hpet __iomem *hpet;
  670. static struct hpets *last;
  671. unsigned long period;
  672. unsigned long long temp;
  673. u32 remainder;
  674. /*
  675. * hpet_alloc can be called by platform dependent code.
  676. * If platform dependent code has allocated the hpet that
  677. * ACPI has also reported, then we catch it here.
  678. */
  679. if (hpet_is_known(hdp)) {
  680. printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
  681. __func__);
  682. return 0;
  683. }
  684. hpetp = kzalloc(struct_size(hpetp, hp_dev, hdp->hd_nirqs),
  685. GFP_KERNEL);
  686. if (!hpetp)
  687. return -ENOMEM;
  688. hpetp->hp_which = hpet_nhpet++;
  689. hpetp->hp_hpet = hdp->hd_address;
  690. hpetp->hp_hpet_phys = hdp->hd_phys_address;
  691. hpetp->hp_ntimer = hdp->hd_nirqs;
  692. for (i = 0; i < hdp->hd_nirqs; i++)
  693. hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
  694. hpet = hpetp->hp_hpet;
  695. cap = readq(&hpet->hpet_cap);
  696. ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
  697. if (hpetp->hp_ntimer != ntimer) {
  698. printk(KERN_WARNING "hpet: number irqs doesn't agree"
  699. " with number of timers\n");
  700. kfree(hpetp);
  701. return -ENODEV;
  702. }
  703. if (last)
  704. last->hp_next = hpetp;
  705. else
  706. hpets = hpetp;
  707. last = hpetp;
  708. period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
  709. HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
  710. temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
  711. temp += period >> 1; /* round */
  712. do_div(temp, period);
  713. hpetp->hp_tick_freq = temp; /* ticks per second */
  714. printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
  715. hpetp->hp_which, hdp->hd_phys_address,
  716. hpetp->hp_ntimer > 1 ? "s" : "");
  717. for (i = 0; i < hpetp->hp_ntimer; i++)
  718. printk(KERN_CONT "%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
  719. printk(KERN_CONT "\n");
  720. temp = hpetp->hp_tick_freq;
  721. remainder = do_div(temp, 1000000);
  722. printk(KERN_INFO
  723. "hpet%u: %u comparators, %d-bit %u.%06u MHz counter\n",
  724. hpetp->hp_which, hpetp->hp_ntimer,
  725. cap & HPET_COUNTER_SIZE_MASK ? 64 : 32,
  726. (unsigned) temp, remainder);
  727. mcfg = readq(&hpet->hpet_config);
  728. if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
  729. write_counter(0L, &hpet->hpet_mc);
  730. mcfg |= HPET_ENABLE_CNF_MASK;
  731. writeq(mcfg, &hpet->hpet_config);
  732. }
  733. for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
  734. struct hpet_timer __iomem *timer;
  735. timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
  736. devp->hd_hpets = hpetp;
  737. devp->hd_hpet = hpet;
  738. devp->hd_timer = timer;
  739. /*
  740. * If the timer was reserved by platform code,
  741. * then make timer unavailable for opens.
  742. */
  743. if (hdp->hd_state & (1 << i)) {
  744. devp->hd_flags = HPET_OPEN;
  745. continue;
  746. }
  747. init_waitqueue_head(&devp->hd_waitqueue);
  748. }
  749. hpetp->hp_delta = hpet_calibrate(hpetp);
  750. /* This clocksource driver currently only works on ia64 */
  751. #ifdef CONFIG_IA64
  752. if (!hpet_clocksource) {
  753. hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc;
  754. clocksource_hpet.archdata.fsys_mmio = hpet_mctr;
  755. clocksource_register_hz(&clocksource_hpet, hpetp->hp_tick_freq);
  756. hpetp->hp_clocksource = &clocksource_hpet;
  757. hpet_clocksource = &clocksource_hpet;
  758. }
  759. #endif
  760. return 0;
  761. }
  762. static acpi_status hpet_resources(struct acpi_resource *res, void *data)
  763. {
  764. struct hpet_data *hdp;
  765. acpi_status status;
  766. struct acpi_resource_address64 addr;
  767. hdp = data;
  768. status = acpi_resource_to_address64(res, &addr);
  769. if (ACPI_SUCCESS(status)) {
  770. hdp->hd_phys_address = addr.address.minimum;
  771. hdp->hd_address = ioremap(addr.address.minimum, addr.address.address_length);
  772. if (!hdp->hd_address)
  773. return AE_ERROR;
  774. if (hpet_is_known(hdp)) {
  775. iounmap(hdp->hd_address);
  776. return AE_ALREADY_EXISTS;
  777. }
  778. } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  779. struct acpi_resource_fixed_memory32 *fixmem32;
  780. fixmem32 = &res->data.fixed_memory32;
  781. hdp->hd_phys_address = fixmem32->address;
  782. hdp->hd_address = ioremap(fixmem32->address,
  783. HPET_RANGE_SIZE);
  784. if (!hdp->hd_address)
  785. return AE_ERROR;
  786. if (hpet_is_known(hdp)) {
  787. iounmap(hdp->hd_address);
  788. return AE_ALREADY_EXISTS;
  789. }
  790. } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  791. struct acpi_resource_extended_irq *irqp;
  792. int i, irq;
  793. irqp = &res->data.extended_irq;
  794. for (i = 0; i < irqp->interrupt_count; i++) {
  795. if (hdp->hd_nirqs >= HPET_MAX_TIMERS)
  796. break;
  797. irq = acpi_register_gsi(NULL, irqp->interrupts[i],
  798. irqp->triggering,
  799. irqp->polarity);
  800. if (irq < 0)
  801. return AE_ERROR;
  802. hdp->hd_irq[hdp->hd_nirqs] = irq;
  803. hdp->hd_nirqs++;
  804. }
  805. }
  806. return AE_OK;
  807. }
  808. static int hpet_acpi_add(struct acpi_device *device)
  809. {
  810. acpi_status result;
  811. struct hpet_data data;
  812. memset(&data, 0, sizeof(data));
  813. result =
  814. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  815. hpet_resources, &data);
  816. if (ACPI_FAILURE(result))
  817. return -ENODEV;
  818. if (!data.hd_address || !data.hd_nirqs) {
  819. if (data.hd_address)
  820. iounmap(data.hd_address);
  821. printk("%s: no address or irqs in _CRS\n", __func__);
  822. return -ENODEV;
  823. }
  824. return hpet_alloc(&data);
  825. }
  826. static const struct acpi_device_id hpet_device_ids[] = {
  827. {"PNP0103", 0},
  828. {"", 0},
  829. };
  830. static struct acpi_driver hpet_acpi_driver = {
  831. .name = "hpet",
  832. .ids = hpet_device_ids,
  833. .ops = {
  834. .add = hpet_acpi_add,
  835. },
  836. };
  837. static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
  838. static int __init hpet_init(void)
  839. {
  840. int result;
  841. result = misc_register(&hpet_misc);
  842. if (result < 0)
  843. return -ENODEV;
  844. sysctl_header = register_sysctl("dev/hpet", hpet_table);
  845. result = acpi_bus_register_driver(&hpet_acpi_driver);
  846. if (result < 0) {
  847. if (sysctl_header)
  848. unregister_sysctl_table(sysctl_header);
  849. misc_deregister(&hpet_misc);
  850. return result;
  851. }
  852. return 0;
  853. }
  854. device_initcall(hpet_init);
  855. /*
  856. MODULE_AUTHOR("Bob Picco <[email protected]>");
  857. MODULE_LICENSE("GPL");
  858. */