clockevents.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * linux/kernel/time/clockevents.c
  3. *
  4. * This file contains functions which manage clock event devices.
  5. *
  6. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  8. * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
  9. *
  10. * This code is licenced under the GPL version 2. For details see
  11. * kernel-base/COPYING.
  12. */
  13. #include <linux/clockchips.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/smp.h>
  18. #include <linux/device.h>
  19. #include "tick-internal.h"
  20. /* The registered clock event devices */
  21. static LIST_HEAD(clockevent_devices);
  22. static LIST_HEAD(clockevents_released);
  23. /* Protection for the above */
  24. static DEFINE_RAW_SPINLOCK(clockevents_lock);
  25. /* Protection for unbind operations */
  26. static DEFINE_MUTEX(clockevents_mutex);
  27. struct ce_unbind {
  28. struct clock_event_device *ce;
  29. int res;
  30. };
  31. static u64 cev_delta2ns(unsigned long latch, struct clock_event_device *evt,
  32. bool ismax)
  33. {
  34. u64 clc = (u64) latch << evt->shift;
  35. u64 rnd;
  36. if (unlikely(!evt->mult)) {
  37. evt->mult = 1;
  38. WARN_ON(1);
  39. }
  40. rnd = (u64) evt->mult - 1;
  41. /*
  42. * Upper bound sanity check. If the backwards conversion is
  43. * not equal latch, we know that the above shift overflowed.
  44. */
  45. if ((clc >> evt->shift) != (u64)latch)
  46. clc = ~0ULL;
  47. /*
  48. * Scaled math oddities:
  49. *
  50. * For mult <= (1 << shift) we can safely add mult - 1 to
  51. * prevent integer rounding loss. So the backwards conversion
  52. * from nsec to device ticks will be correct.
  53. *
  54. * For mult > (1 << shift), i.e. device frequency is > 1GHz we
  55. * need to be careful. Adding mult - 1 will result in a value
  56. * which when converted back to device ticks can be larger
  57. * than latch by up to (mult - 1) >> shift. For the min_delta
  58. * calculation we still want to apply this in order to stay
  59. * above the minimum device ticks limit. For the upper limit
  60. * we would end up with a latch value larger than the upper
  61. * limit of the device, so we omit the add to stay below the
  62. * device upper boundary.
  63. *
  64. * Also omit the add if it would overflow the u64 boundary.
  65. */
  66. if ((~0ULL - clc > rnd) &&
  67. (!ismax || evt->mult <= (1U << evt->shift)))
  68. clc += rnd;
  69. do_div(clc, evt->mult);
  70. /* Deltas less than 1usec are pointless noise */
  71. return clc > 1000 ? clc : 1000;
  72. }
  73. /**
  74. * clockevents_delta2ns - Convert a latch value (device ticks) to nanoseconds
  75. * @latch: value to convert
  76. * @evt: pointer to clock event device descriptor
  77. *
  78. * Math helper, returns latch value converted to nanoseconds (bound checked)
  79. */
  80. u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt)
  81. {
  82. return cev_delta2ns(latch, evt, false);
  83. }
  84. EXPORT_SYMBOL_GPL(clockevent_delta2ns);
  85. /**
  86. * clockevents_set_mode - set the operating mode of a clock event device
  87. * @dev: device to modify
  88. * @mode: new mode
  89. *
  90. * Must be called with interrupts disabled !
  91. */
  92. void clockevents_set_mode(struct clock_event_device *dev,
  93. enum clock_event_mode mode)
  94. {
  95. if (dev->mode != mode) {
  96. dev->set_mode(mode, dev);
  97. dev->mode = mode;
  98. /*
  99. * A nsec2cyc multiplicator of 0 is invalid and we'd crash
  100. * on it, so fix it up and emit a warning:
  101. */
  102. if (mode == CLOCK_EVT_MODE_ONESHOT) {
  103. if (unlikely(!dev->mult)) {
  104. dev->mult = 1;
  105. WARN_ON(1);
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * clockevents_shutdown - shutdown the device and clear next_event
  112. * @dev: device to shutdown
  113. */
  114. void clockevents_shutdown(struct clock_event_device *dev)
  115. {
  116. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  117. dev->next_event.tv64 = KTIME_MAX;
  118. }
  119. #ifdef CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST
  120. /* Limit min_delta to a jiffie */
  121. #define MIN_DELTA_LIMIT (NSEC_PER_SEC / HZ)
  122. /**
  123. * clockevents_increase_min_delta - raise minimum delta of a clock event device
  124. * @dev: device to increase the minimum delta
  125. *
  126. * Returns 0 on success, -ETIME when the minimum delta reached the limit.
  127. */
  128. static int clockevents_increase_min_delta(struct clock_event_device *dev)
  129. {
  130. /* Nothing to do if we already reached the limit */
  131. if (dev->min_delta_ns >= MIN_DELTA_LIMIT) {
  132. printk(KERN_WARNING "CE: Reprogramming failure. Giving up\n");
  133. dev->next_event.tv64 = KTIME_MAX;
  134. return -ETIME;
  135. }
  136. if (dev->min_delta_ns < 5000)
  137. dev->min_delta_ns = 5000;
  138. else
  139. dev->min_delta_ns += dev->min_delta_ns >> 1;
  140. if (dev->min_delta_ns > MIN_DELTA_LIMIT)
  141. dev->min_delta_ns = MIN_DELTA_LIMIT;
  142. printk(KERN_WARNING "CE: %s increased min_delta_ns to %llu nsec\n",
  143. dev->name ? dev->name : "?",
  144. (unsigned long long) dev->min_delta_ns);
  145. return 0;
  146. }
  147. /**
  148. * clockevents_program_min_delta - Set clock event device to the minimum delay.
  149. * @dev: device to program
  150. *
  151. * Returns 0 on success, -ETIME when the retry loop failed.
  152. */
  153. static int clockevents_program_min_delta(struct clock_event_device *dev)
  154. {
  155. unsigned long long clc;
  156. int64_t delta;
  157. int i;
  158. for (i = 0;;) {
  159. delta = dev->min_delta_ns;
  160. dev->next_event = ktime_add_ns(ktime_get(), delta);
  161. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  162. return 0;
  163. dev->retries++;
  164. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  165. if (dev->set_next_event((unsigned long) clc, dev) == 0)
  166. return 0;
  167. if (++i > 2) {
  168. /*
  169. * We tried 3 times to program the device with the
  170. * given min_delta_ns. Try to increase the minimum
  171. * delta, if that fails as well get out of here.
  172. */
  173. if (clockevents_increase_min_delta(dev))
  174. return -ETIME;
  175. i = 0;
  176. }
  177. }
  178. }
  179. #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
  180. /**
  181. * clockevents_program_min_delta - Set clock event device to the minimum delay.
  182. * @dev: device to program
  183. *
  184. * Returns 0 on success, -ETIME when the retry loop failed.
  185. */
  186. static int clockevents_program_min_delta(struct clock_event_device *dev)
  187. {
  188. unsigned long long clc;
  189. int64_t delta;
  190. delta = dev->min_delta_ns;
  191. dev->next_event = ktime_add_ns(ktime_get(), delta);
  192. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  193. return 0;
  194. dev->retries++;
  195. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  196. return dev->set_next_event((unsigned long) clc, dev);
  197. }
  198. #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
  199. /**
  200. * clockevents_program_event - Reprogram the clock event device.
  201. * @dev: device to program
  202. * @expires: absolute expiry time (monotonic clock)
  203. * @force: program minimum delay if expires can not be set
  204. *
  205. * Returns 0 on success, -ETIME when the event is in the past.
  206. */
  207. int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
  208. bool force)
  209. {
  210. unsigned long long clc;
  211. int64_t delta;
  212. int rc;
  213. if (unlikely(expires.tv64 < 0)) {
  214. WARN_ON_ONCE(1);
  215. return -ETIME;
  216. }
  217. dev->next_event = expires;
  218. if (dev->mode == CLOCK_EVT_MODE_SHUTDOWN)
  219. return 0;
  220. /* Shortcut for clockevent devices that can deal with ktime. */
  221. if (dev->features & CLOCK_EVT_FEAT_KTIME)
  222. return dev->set_next_ktime(expires, dev);
  223. delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
  224. if (delta <= 0)
  225. return force ? clockevents_program_min_delta(dev) : -ETIME;
  226. delta = min(delta, (int64_t) dev->max_delta_ns);
  227. delta = max(delta, (int64_t) dev->min_delta_ns);
  228. clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
  229. rc = dev->set_next_event((unsigned long) clc, dev);
  230. return (rc && force) ? clockevents_program_min_delta(dev) : rc;
  231. }
  232. /*
  233. * Called after a notify add to make devices available which were
  234. * released from the notifier call.
  235. */
  236. static void clockevents_notify_released(void)
  237. {
  238. struct clock_event_device *dev;
  239. while (!list_empty(&clockevents_released)) {
  240. dev = list_entry(clockevents_released.next,
  241. struct clock_event_device, list);
  242. list_del(&dev->list);
  243. list_add(&dev->list, &clockevent_devices);
  244. tick_check_new_device(dev);
  245. }
  246. }
  247. /*
  248. * Try to install a replacement clock event device
  249. */
  250. static int clockevents_replace(struct clock_event_device *ced)
  251. {
  252. struct clock_event_device *dev, *newdev = NULL;
  253. list_for_each_entry(dev, &clockevent_devices, list) {
  254. if (dev == ced || dev->mode != CLOCK_EVT_MODE_UNUSED)
  255. continue;
  256. if (!tick_check_replacement(newdev, dev))
  257. continue;
  258. if (!try_module_get(dev->owner))
  259. continue;
  260. if (newdev)
  261. module_put(newdev->owner);
  262. newdev = dev;
  263. }
  264. if (newdev) {
  265. tick_install_replacement(newdev);
  266. list_del_init(&ced->list);
  267. }
  268. return newdev ? 0 : -EBUSY;
  269. }
  270. /*
  271. * Called with clockevents_mutex and clockevents_lock held
  272. */
  273. static int __clockevents_try_unbind(struct clock_event_device *ced, int cpu)
  274. {
  275. /* Fast track. Device is unused */
  276. if (ced->mode == CLOCK_EVT_MODE_UNUSED) {
  277. list_del_init(&ced->list);
  278. return 0;
  279. }
  280. return ced == per_cpu(tick_cpu_device, cpu).evtdev ? -EAGAIN : -EBUSY;
  281. }
  282. /*
  283. * SMP function call to unbind a device
  284. */
  285. static void __clockevents_unbind(void *arg)
  286. {
  287. struct ce_unbind *cu = arg;
  288. int res;
  289. raw_spin_lock(&clockevents_lock);
  290. res = __clockevents_try_unbind(cu->ce, smp_processor_id());
  291. if (res == -EAGAIN)
  292. res = clockevents_replace(cu->ce);
  293. cu->res = res;
  294. raw_spin_unlock(&clockevents_lock);
  295. }
  296. /*
  297. * Issues smp function call to unbind a per cpu device. Called with
  298. * clockevents_mutex held.
  299. */
  300. static int clockevents_unbind(struct clock_event_device *ced, int cpu)
  301. {
  302. struct ce_unbind cu = { .ce = ced, .res = -ENODEV };
  303. smp_call_function_single(cpu, __clockevents_unbind, &cu, 1);
  304. return cu.res;
  305. }
  306. /*
  307. * Unbind a clockevents device.
  308. */
  309. int clockevents_unbind_device(struct clock_event_device *ced, int cpu)
  310. {
  311. int ret;
  312. mutex_lock(&clockevents_mutex);
  313. ret = clockevents_unbind(ced, cpu);
  314. mutex_unlock(&clockevents_mutex);
  315. return ret;
  316. }
  317. EXPORT_SYMBOL_GPL(clockevents_unbind);
  318. /**
  319. * clockevents_register_device - register a clock event device
  320. * @dev: device to register
  321. */
  322. void clockevents_register_device(struct clock_event_device *dev)
  323. {
  324. unsigned long flags;
  325. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  326. if (!dev->cpumask) {
  327. WARN_ON(num_possible_cpus() > 1);
  328. dev->cpumask = cpumask_of(smp_processor_id());
  329. }
  330. raw_spin_lock_irqsave(&clockevents_lock, flags);
  331. list_add(&dev->list, &clockevent_devices);
  332. tick_check_new_device(dev);
  333. clockevents_notify_released();
  334. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  335. }
  336. EXPORT_SYMBOL_GPL(clockevents_register_device);
  337. void clockevents_config(struct clock_event_device *dev, u32 freq)
  338. {
  339. u64 sec;
  340. if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
  341. return;
  342. /*
  343. * Calculate the maximum number of seconds we can sleep. Limit
  344. * to 10 minutes for hardware which can program more than
  345. * 32bit ticks so we still get reasonable conversion values.
  346. */
  347. sec = dev->max_delta_ticks;
  348. do_div(sec, freq);
  349. if (!sec)
  350. sec = 1;
  351. else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
  352. sec = 600;
  353. clockevents_calc_mult_shift(dev, freq, sec);
  354. dev->min_delta_ns = cev_delta2ns(dev->min_delta_ticks, dev, false);
  355. dev->max_delta_ns = cev_delta2ns(dev->max_delta_ticks, dev, true);
  356. }
  357. /**
  358. * clockevents_config_and_register - Configure and register a clock event device
  359. * @dev: device to register
  360. * @freq: The clock frequency
  361. * @min_delta: The minimum clock ticks to program in oneshot mode
  362. * @max_delta: The maximum clock ticks to program in oneshot mode
  363. *
  364. * min/max_delta can be 0 for devices which do not support oneshot mode.
  365. */
  366. void clockevents_config_and_register(struct clock_event_device *dev,
  367. u32 freq, unsigned long min_delta,
  368. unsigned long max_delta)
  369. {
  370. dev->min_delta_ticks = min_delta;
  371. dev->max_delta_ticks = max_delta;
  372. clockevents_config(dev, freq);
  373. clockevents_register_device(dev);
  374. }
  375. EXPORT_SYMBOL_GPL(clockevents_config_and_register);
  376. /**
  377. * clockevents_update_freq - Update frequency and reprogram a clock event device.
  378. * @dev: device to modify
  379. * @freq: new device frequency
  380. *
  381. * Reconfigure and reprogram a clock event device in oneshot
  382. * mode. Must be called on the cpu for which the device delivers per
  383. * cpu timer events with interrupts disabled! Returns 0 on success,
  384. * -ETIME when the event is in the past.
  385. */
  386. int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
  387. {
  388. clockevents_config(dev, freq);
  389. if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
  390. return 0;
  391. return clockevents_program_event(dev, dev->next_event, false);
  392. }
  393. /*
  394. * Noop handler when we shut down an event device
  395. */
  396. void clockevents_handle_noop(struct clock_event_device *dev)
  397. {
  398. }
  399. /**
  400. * clockevents_exchange_device - release and request clock devices
  401. * @old: device to release (can be NULL)
  402. * @new: device to request (can be NULL)
  403. *
  404. * Called from the notifier chain. clockevents_lock is held already
  405. */
  406. void clockevents_exchange_device(struct clock_event_device *old,
  407. struct clock_event_device *new)
  408. {
  409. unsigned long flags;
  410. local_irq_save(flags);
  411. /*
  412. * Caller releases a clock event device. We queue it into the
  413. * released list and do a notify add later.
  414. */
  415. if (old) {
  416. module_put(old->owner);
  417. clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED);
  418. list_del(&old->list);
  419. list_add(&old->list, &clockevents_released);
  420. }
  421. if (new) {
  422. BUG_ON(new->mode != CLOCK_EVT_MODE_UNUSED);
  423. clockevents_shutdown(new);
  424. }
  425. local_irq_restore(flags);
  426. }
  427. /**
  428. * clockevents_suspend - suspend clock devices
  429. */
  430. void clockevents_suspend(void)
  431. {
  432. struct clock_event_device *dev;
  433. list_for_each_entry_reverse(dev, &clockevent_devices, list)
  434. if (dev->suspend)
  435. dev->suspend(dev);
  436. }
  437. /**
  438. * clockevents_resume - resume clock devices
  439. */
  440. void clockevents_resume(void)
  441. {
  442. struct clock_event_device *dev;
  443. list_for_each_entry(dev, &clockevent_devices, list)
  444. if (dev->resume)
  445. dev->resume(dev);
  446. }
  447. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  448. /**
  449. * clockevents_notify - notification about relevant events
  450. */
  451. void clockevents_notify(unsigned long reason, void *arg)
  452. {
  453. struct clock_event_device *dev, *tmp;
  454. unsigned long flags;
  455. int cpu;
  456. raw_spin_lock_irqsave(&clockevents_lock, flags);
  457. switch (reason) {
  458. case CLOCK_EVT_NOTIFY_BROADCAST_ON:
  459. case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
  460. case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
  461. tick_broadcast_on_off(reason, arg);
  462. break;
  463. case CLOCK_EVT_NOTIFY_BROADCAST_ENTER:
  464. case CLOCK_EVT_NOTIFY_BROADCAST_EXIT:
  465. tick_broadcast_oneshot_control(reason);
  466. break;
  467. case CLOCK_EVT_NOTIFY_CPU_DYING:
  468. tick_handover_do_timer(arg);
  469. break;
  470. case CLOCK_EVT_NOTIFY_SUSPEND:
  471. tick_suspend();
  472. tick_suspend_broadcast();
  473. break;
  474. case CLOCK_EVT_NOTIFY_RESUME:
  475. tick_resume();
  476. break;
  477. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  478. tick_shutdown_broadcast_oneshot(arg);
  479. tick_shutdown_broadcast(arg);
  480. tick_shutdown(arg);
  481. /*
  482. * Unregister the clock event devices which were
  483. * released from the users in the notify chain.
  484. */
  485. list_for_each_entry_safe(dev, tmp, &clockevents_released, list)
  486. list_del(&dev->list);
  487. /*
  488. * Now check whether the CPU has left unused per cpu devices
  489. */
  490. cpu = *((int *)arg);
  491. list_for_each_entry_safe(dev, tmp, &clockevent_devices, list) {
  492. if (cpumask_test_cpu(cpu, dev->cpumask) &&
  493. cpumask_weight(dev->cpumask) == 1 &&
  494. !tick_is_broadcast_device(dev)) {
  495. BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
  496. list_del(&dev->list);
  497. }
  498. }
  499. break;
  500. default:
  501. break;
  502. }
  503. raw_spin_unlock_irqrestore(&clockevents_lock, flags);
  504. }
  505. EXPORT_SYMBOL_GPL(clockevents_notify);
  506. #ifdef CONFIG_SYSFS
  507. struct bus_type clockevents_subsys = {
  508. .name = "clockevents",
  509. .dev_name = "clockevent",
  510. };
  511. static DEFINE_PER_CPU(struct device, tick_percpu_dev);
  512. static struct tick_device *tick_get_tick_dev(struct device *dev);
  513. static ssize_t sysfs_show_current_tick_dev(struct device *dev,
  514. struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct tick_device *td;
  518. ssize_t count = 0;
  519. raw_spin_lock_irq(&clockevents_lock);
  520. td = tick_get_tick_dev(dev);
  521. if (td && td->evtdev)
  522. count = snprintf(buf, PAGE_SIZE, "%s\n", td->evtdev->name);
  523. raw_spin_unlock_irq(&clockevents_lock);
  524. return count;
  525. }
  526. static DEVICE_ATTR(current_device, 0444, sysfs_show_current_tick_dev, NULL);
  527. /* We don't support the abomination of removable broadcast devices */
  528. static ssize_t sysfs_unbind_tick_dev(struct device *dev,
  529. struct device_attribute *attr,
  530. const char *buf, size_t count)
  531. {
  532. char name[CS_NAME_LEN];
  533. ssize_t ret = sysfs_get_uname(buf, name, count);
  534. struct clock_event_device *ce;
  535. if (ret < 0)
  536. return ret;
  537. ret = -ENODEV;
  538. mutex_lock(&clockevents_mutex);
  539. raw_spin_lock_irq(&clockevents_lock);
  540. list_for_each_entry(ce, &clockevent_devices, list) {
  541. if (!strcmp(ce->name, name)) {
  542. ret = __clockevents_try_unbind(ce, dev->id);
  543. break;
  544. }
  545. }
  546. raw_spin_unlock_irq(&clockevents_lock);
  547. /*
  548. * We hold clockevents_mutex, so ce can't go away
  549. */
  550. if (ret == -EAGAIN)
  551. ret = clockevents_unbind(ce, dev->id);
  552. mutex_unlock(&clockevents_mutex);
  553. return ret ? ret : count;
  554. }
  555. static DEVICE_ATTR(unbind_device, 0200, NULL, sysfs_unbind_tick_dev);
  556. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  557. static struct device tick_bc_dev = {
  558. .init_name = "broadcast",
  559. .id = 0,
  560. .bus = &clockevents_subsys,
  561. };
  562. static struct tick_device *tick_get_tick_dev(struct device *dev)
  563. {
  564. return dev == &tick_bc_dev ? tick_get_broadcast_device() :
  565. &per_cpu(tick_cpu_device, dev->id);
  566. }
  567. static __init int tick_broadcast_init_sysfs(void)
  568. {
  569. int err = device_register(&tick_bc_dev);
  570. if (!err)
  571. err = device_create_file(&tick_bc_dev, &dev_attr_current_device);
  572. return err;
  573. }
  574. #else
  575. static struct tick_device *tick_get_tick_dev(struct device *dev)
  576. {
  577. return &per_cpu(tick_cpu_device, dev->id);
  578. }
  579. static inline int tick_broadcast_init_sysfs(void) { return 0; }
  580. #endif
  581. static int __init tick_init_sysfs(void)
  582. {
  583. int cpu;
  584. for_each_possible_cpu(cpu) {
  585. struct device *dev = &per_cpu(tick_percpu_dev, cpu);
  586. int err;
  587. dev->id = cpu;
  588. dev->bus = &clockevents_subsys;
  589. err = device_register(dev);
  590. if (!err)
  591. err = device_create_file(dev, &dev_attr_current_device);
  592. if (!err)
  593. err = device_create_file(dev, &dev_attr_unbind_device);
  594. if (err)
  595. return err;
  596. }
  597. return tick_broadcast_init_sysfs();
  598. }
  599. static int __init clockevents_init_sysfs(void)
  600. {
  601. int err = subsys_system_register(&clockevents_subsys, NULL);
  602. if (!err)
  603. err = tick_init_sysfs();
  604. return err;
  605. }
  606. device_initcall(clockevents_init_sysfs);
  607. #endif /* SYSFS */
  608. #endif /* GENERIC_CLOCK_EVENTS */