hpet.c 23 KB

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