aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2012-07-11 11:21:54 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-07-16 19:53:16 -0400
commitd58b4bcc6df8046cf9c3c59f9ff84d2cd86b93eb (patch)
treebef387c0c81654a38271f8528bb8b877ac5d3ed7 /drivers/usb
parentc0c53dbc32ea05a1e1dd9dba4772327da9a11750 (diff)
USB: EHCI: introduce high-res timer
This patch (as1572) begins the conversion of ehci-hcd over to using high-resolution timers rather than old-fashioned low-resolution kernel timers. This reduces overhead caused by timer roundoff on systems where HZ is smaller than 1000. Also, the new timer framework introduced here is much more logical and easily extended than the ad-hoc approach ehci-hcd currently uses for timers. An hrtimer structure is added to ehci_hcd, along with a bitflag array and an array of ktime_t values, to keep track of which timing events are pending and what their expiration times are. Only the infrastructure for the timing operations is added in this patch. Later patches will add routines for handling each of the various timing events the driver needs. In some cases the new hrtimer handlers will replace the existing handlers for ehci-hcd's kernel timers; as this happens the old timers will be removed. In other cases the new timing events will replace busy-wait loops. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/host/ehci-hcd.c15
-rw-r--r--drivers/usb/host/ehci-hub.c3
-rw-r--r--drivers/usb/host/ehci-timer.c106
-rw-r--r--drivers/usb/host/ehci.h16
4 files changed, 138 insertions, 2 deletions
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index bc94822f4c5d..f8fed163a23a 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -30,8 +30,7 @@
30#include <linux/vmalloc.h> 30#include <linux/vmalloc.h>
31#include <linux/errno.h> 31#include <linux/errno.h>
32#include <linux/init.h> 32#include <linux/init.h>
33#include <linux/timer.h> 33#include <linux/hrtimer.h>
34#include <linux/ktime.h>
35#include <linux/list.h> 34#include <linux/list.h>
36#include <linux/interrupt.h> 35#include <linux/interrupt.h>
37#include <linux/usb.h> 36#include <linux/usb.h>
@@ -380,6 +379,7 @@ static void ehci_quiesce (struct ehci_hcd *ehci)
380static void end_unlink_async(struct ehci_hcd *ehci); 379static void end_unlink_async(struct ehci_hcd *ehci);
381static void ehci_work(struct ehci_hcd *ehci); 380static void ehci_work(struct ehci_hcd *ehci);
382 381
382#include "ehci-timer.c"
383#include "ehci-hub.c" 383#include "ehci-hub.c"
384#include "ehci-lpm.c" 384#include "ehci-lpm.c"
385#include "ehci-mem.c" 385#include "ehci-mem.c"
@@ -494,7 +494,10 @@ static void ehci_shutdown(struct usb_hcd *hcd)
494 spin_lock_irq(&ehci->lock); 494 spin_lock_irq(&ehci->lock);
495 ehci->rh_state = EHCI_RH_STOPPING; 495 ehci->rh_state = EHCI_RH_STOPPING;
496 ehci_silence_controller(ehci); 496 ehci_silence_controller(ehci);
497 ehci->enabled_hrtimer_events = 0;
497 spin_unlock_irq(&ehci->lock); 498 spin_unlock_irq(&ehci->lock);
499
500 hrtimer_cancel(&ehci->hrtimer);
498} 501}
499 502
500static void ehci_port_power (struct ehci_hcd *ehci, int is_on) 503static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
@@ -561,12 +564,14 @@ static void ehci_stop (struct usb_hcd *hcd)
561 del_timer_sync(&ehci->iaa_watchdog); 564 del_timer_sync(&ehci->iaa_watchdog);
562 565
563 spin_lock_irq(&ehci->lock); 566 spin_lock_irq(&ehci->lock);
567 ehci->enabled_hrtimer_events = 0;
564 ehci_quiesce(ehci); 568 ehci_quiesce(ehci);
565 569
566 ehci_silence_controller(ehci); 570 ehci_silence_controller(ehci);
567 ehci_reset (ehci); 571 ehci_reset (ehci);
568 spin_unlock_irq(&ehci->lock); 572 spin_unlock_irq(&ehci->lock);
569 573
574 hrtimer_cancel(&ehci->hrtimer);
570 remove_sysfs_files(ehci); 575 remove_sysfs_files(ehci);
571 remove_debug_files (ehci); 576 remove_debug_files (ehci);
572 577
@@ -615,6 +620,10 @@ static int ehci_init(struct usb_hcd *hcd)
615 ehci->iaa_watchdog.function = ehci_iaa_watchdog; 620 ehci->iaa_watchdog.function = ehci_iaa_watchdog;
616 ehci->iaa_watchdog.data = (unsigned long) ehci; 621 ehci->iaa_watchdog.data = (unsigned long) ehci;
617 622
623 hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
624 ehci->hrtimer.function = ehci_hrtimer_func;
625 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
626
618 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params); 627 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
619 628
620 /* 629 /*
@@ -954,6 +963,8 @@ static irqreturn_t ehci_irq (struct usb_hcd *hcd)
954 dbg_status(ehci, "fatal", status); 963 dbg_status(ehci, "fatal", status);
955 ehci_halt(ehci); 964 ehci_halt(ehci);
956dead: 965dead:
966 ehci->enabled_hrtimer_events = 0;
967 hrtimer_try_to_cancel(&ehci->hrtimer);
957 ehci_reset(ehci); 968 ehci_reset(ehci);
958 ehci_writel(ehci, 0, &ehci->regs->configured_flag); 969 ehci_writel(ehci, 0, &ehci->regs->configured_flag);
959 usb_hc_died(hcd); 970 usb_hc_died(hcd);
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index fb1b99e74937..25329e4b844f 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -311,12 +311,15 @@ static int ehci_bus_suspend (struct usb_hcd *hcd)
311 ehci_readl(ehci, &ehci->regs->intr_enable); 311 ehci_readl(ehci, &ehci->regs->intr_enable);
312 312
313 ehci->next_statechange = jiffies + msecs_to_jiffies(10); 313 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
314 ehci->enabled_hrtimer_events = 0;
315 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
314 spin_unlock_irq (&ehci->lock); 316 spin_unlock_irq (&ehci->lock);
315 317
316 /* ehci_work() may have re-enabled the watchdog timer, which we do not 318 /* ehci_work() may have re-enabled the watchdog timer, which we do not
317 * want, and so we must delete any pending watchdog timer events. 319 * want, and so we must delete any pending watchdog timer events.
318 */ 320 */
319 del_timer_sync(&ehci->watchdog); 321 del_timer_sync(&ehci->watchdog);
322 hrtimer_cancel(&ehci->hrtimer);
320 return 0; 323 return 0;
321} 324}
322 325
diff --git a/drivers/usb/host/ehci-timer.c b/drivers/usb/host/ehci-timer.c
new file mode 100644
index 000000000000..f6279e2883a8
--- /dev/null
+++ b/drivers/usb/host/ehci-timer.c
@@ -0,0 +1,106 @@
1/*
2 * Copyright (C) 2012 by Alan Stern
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15/* This file is part of ehci-hcd.c */
16
17/*-------------------------------------------------------------------------*/
18
19/*
20 * EHCI timer support... Now using hrtimers.
21 *
22 * Lots of different events are triggered from ehci->hrtimer. Whenever
23 * the timer routine runs, it checks each possible event; events that are
24 * currently enabled and whose expiration time has passed get handled.
25 * The set of enabled events is stored as a collection of bitflags in
26 * ehci->enabled_hrtimer_events, and they are numbered in order of
27 * increasing delay values (ranging between 1 ms and 100 ms).
28 *
29 * Rather than implementing a sorted list or tree of all pending events,
30 * we keep track only of the lowest-numbered pending event, in
31 * ehci->next_hrtimer_event. Whenever ehci->hrtimer gets restarted, its
32 * expiration time is set to the timeout value for this event.
33 *
34 * As a result, events might not get handled right away; the actual delay
35 * could be anywhere up to twice the requested delay. This doesn't
36 * matter, because none of the events are especially time-critical. The
37 * ones that matter most all have a delay of 1 ms, so they will be
38 * handled after 2 ms at most, which is okay. In addition to this, we
39 * allow for an expiration range of 1 ms.
40 */
41
42/*
43 * Delay lengths for the hrtimer event types.
44 * Keep this list sorted by delay length, in the same order as
45 * the event types indexed by enum ehci_hrtimer_event in ehci.h.
46 */
47static unsigned event_delays_ns[] = {
48};
49
50/* Enable a pending hrtimer event */
51static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
52 bool resched)
53{
54 ktime_t *timeout = &ehci->hr_timeouts[event];
55
56 if (resched)
57 *timeout = ktime_add(ktime_get(),
58 ktime_set(0, event_delays_ns[event]));
59 ehci->enabled_hrtimer_events |= (1 << event);
60
61 /* Track only the lowest-numbered pending event */
62 if (event < ehci->next_hrtimer_event) {
63 ehci->next_hrtimer_event = event;
64 hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
65 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
66 }
67}
68
69
70/*
71 * Handler functions for the hrtimer event types.
72 * Keep this array in the same order as the event types indexed by
73 * enum ehci_hrtimer_event in ehci.h.
74 */
75static void (*event_handlers[])(struct ehci_hcd *) = {
76};
77
78static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
79{
80 struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
81 ktime_t now;
82 unsigned long events;
83 unsigned long flags;
84 unsigned e;
85
86 spin_lock_irqsave(&ehci->lock, flags);
87
88 events = ehci->enabled_hrtimer_events;
89 ehci->enabled_hrtimer_events = 0;
90 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
91
92 /*
93 * Check each pending event. If its time has expired, handle
94 * the event; otherwise re-enable it.
95 */
96 now = ktime_get();
97 for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
98 if (now.tv64 >= ehci->hr_timeouts[e].tv64)
99 event_handlers[e](ehci);
100 else
101 ehci_enable_event(ehci, e, false);
102 }
103
104 spin_unlock_irqrestore(&ehci->lock, flags);
105 return HRTIMER_NORESTART;
106}
diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h
index 9e8e82ecce58..070be83028d5 100644
--- a/drivers/usb/host/ehci.h
+++ b/drivers/usb/host/ehci.h
@@ -73,7 +73,23 @@ enum ehci_rh_state {
73 EHCI_RH_STOPPING 73 EHCI_RH_STOPPING
74}; 74};
75 75
76/*
77 * Timer events, ordered by increasing delay length.
78 * Always update event_delays_ns[] and event_handlers[] (defined in
79 * ehci-timer.c) in parallel with this list.
80 */
81enum ehci_hrtimer_event {
82 EHCI_HRTIMER_NUM_EVENTS /* Must come last */
83};
84#define EHCI_HRTIMER_NO_EVENT 99
85
76struct ehci_hcd { /* one per controller */ 86struct ehci_hcd { /* one per controller */
87 /* timing support */
88 enum ehci_hrtimer_event next_hrtimer_event;
89 unsigned enabled_hrtimer_events;
90 ktime_t hr_timeouts[EHCI_HRTIMER_NUM_EVENTS];
91 struct hrtimer hrtimer;
92
77 /* glue to PCI and HCD framework */ 93 /* glue to PCI and HCD framework */
78 struct ehci_caps __iomem *caps; 94 struct ehci_caps __iomem *caps;
79 struct ehci_regs __iomem *regs; 95 struct ehci_regs __iomem *regs;