Commit | Line | Data |
---|---|---|
d58b4bcc AS |
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 | ||
3ca9aeba AS |
19 | /* Set a bit in the USBCMD register */ |
20 | static void ehci_set_command_bit(struct ehci_hcd *ehci, u32 bit) | |
21 | { | |
22 | ehci->command |= bit; | |
23 | ehci_writel(ehci, ehci->command, &ehci->regs->command); | |
24 | ||
25 | /* unblock posted write */ | |
26 | ehci_readl(ehci, &ehci->regs->command); | |
27 | } | |
28 | ||
29 | /* Clear a bit in the USBCMD register */ | |
30 | static void ehci_clear_command_bit(struct ehci_hcd *ehci, u32 bit) | |
31 | { | |
32 | ehci->command &= ~bit; | |
33 | ehci_writel(ehci, ehci->command, &ehci->regs->command); | |
34 | ||
35 | /* unblock posted write */ | |
36 | ehci_readl(ehci, &ehci->regs->command); | |
37 | } | |
38 | ||
39 | /*-------------------------------------------------------------------------*/ | |
40 | ||
d58b4bcc AS |
41 | /* |
42 | * EHCI timer support... Now using hrtimers. | |
43 | * | |
44 | * Lots of different events are triggered from ehci->hrtimer. Whenever | |
45 | * the timer routine runs, it checks each possible event; events that are | |
46 | * currently enabled and whose expiration time has passed get handled. | |
47 | * The set of enabled events is stored as a collection of bitflags in | |
48 | * ehci->enabled_hrtimer_events, and they are numbered in order of | |
49 | * increasing delay values (ranging between 1 ms and 100 ms). | |
50 | * | |
51 | * Rather than implementing a sorted list or tree of all pending events, | |
52 | * we keep track only of the lowest-numbered pending event, in | |
53 | * ehci->next_hrtimer_event. Whenever ehci->hrtimer gets restarted, its | |
54 | * expiration time is set to the timeout value for this event. | |
55 | * | |
56 | * As a result, events might not get handled right away; the actual delay | |
57 | * could be anywhere up to twice the requested delay. This doesn't | |
58 | * matter, because none of the events are especially time-critical. The | |
59 | * ones that matter most all have a delay of 1 ms, so they will be | |
60 | * handled after 2 ms at most, which is okay. In addition to this, we | |
61 | * allow for an expiration range of 1 ms. | |
62 | */ | |
63 | ||
64 | /* | |
65 | * Delay lengths for the hrtimer event types. | |
66 | * Keep this list sorted by delay length, in the same order as | |
67 | * the event types indexed by enum ehci_hrtimer_event in ehci.h. | |
68 | */ | |
69 | static unsigned event_delays_ns[] = { | |
31446610 | 70 | 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_ASS */ |
3ca9aeba | 71 | 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_PSS */ |
bf6387bc | 72 | 1 * NSEC_PER_MSEC, /* EHCI_HRTIMER_POLL_DEAD */ |
df202255 | 73 | 1125 * NSEC_PER_USEC, /* EHCI_HRTIMER_UNLINK_INTR */ |
55934eb3 | 74 | 2 * NSEC_PER_MSEC, /* EHCI_HRTIMER_FREE_ITDS */ |
32830f20 | 75 | 6 * NSEC_PER_MSEC, /* EHCI_HRTIMER_ASYNC_UNLINKS */ |
9d938747 | 76 | 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_IAA_WATCHDOG */ |
3ca9aeba | 77 | 10 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_PERIODIC */ |
31446610 | 78 | 15 * NSEC_PER_MSEC, /* EHCI_HRTIMER_DISABLE_ASYNC */ |
d58b4bcc AS |
79 | }; |
80 | ||
81 | /* Enable a pending hrtimer event */ | |
82 | static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event, | |
83 | bool resched) | |
84 | { | |
85 | ktime_t *timeout = &ehci->hr_timeouts[event]; | |
86 | ||
87 | if (resched) | |
88 | *timeout = ktime_add(ktime_get(), | |
89 | ktime_set(0, event_delays_ns[event])); | |
90 | ehci->enabled_hrtimer_events |= (1 << event); | |
91 | ||
92 | /* Track only the lowest-numbered pending event */ | |
93 | if (event < ehci->next_hrtimer_event) { | |
94 | ehci->next_hrtimer_event = event; | |
95 | hrtimer_start_range_ns(&ehci->hrtimer, *timeout, | |
96 | NSEC_PER_MSEC, HRTIMER_MODE_ABS); | |
97 | } | |
98 | } | |
99 | ||
100 | ||
31446610 AS |
101 | /* Poll the STS_ASS status bit; see when it agrees with CMD_ASE */ |
102 | static void ehci_poll_ASS(struct ehci_hcd *ehci) | |
103 | { | |
104 | unsigned actual, want; | |
105 | ||
106 | /* Don't enable anything if the controller isn't running (e.g., died) */ | |
107 | if (ehci->rh_state != EHCI_RH_RUNNING) | |
108 | return; | |
109 | ||
110 | want = (ehci->command & CMD_ASE) ? STS_ASS : 0; | |
111 | actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS; | |
112 | ||
113 | if (want != actual) { | |
114 | ||
115 | /* Poll again later, but give up after about 20 ms */ | |
116 | if (ehci->ASS_poll_count++ < 20) { | |
117 | ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true); | |
118 | return; | |
119 | } | |
120 | ehci_warn(ehci, "Waited too long for the async schedule status, giving up\n"); | |
121 | } | |
122 | ehci->ASS_poll_count = 0; | |
123 | ||
124 | /* The status is up-to-date; restart or stop the schedule as needed */ | |
125 | if (want == 0) { /* Stopped */ | |
126 | if (ehci->async_count > 0) | |
127 | ehci_set_command_bit(ehci, CMD_ASE); | |
128 | ||
129 | } else { /* Running */ | |
130 | if (ehci->async_count == 0) { | |
131 | ||
132 | /* Turn off the schedule after a while */ | |
133 | ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC, | |
134 | true); | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | /* Turn off the async schedule after a brief delay */ | |
140 | static void ehci_disable_ASE(struct ehci_hcd *ehci) | |
141 | { | |
142 | ehci_clear_command_bit(ehci, CMD_ASE); | |
143 | } | |
144 | ||
145 | ||
3ca9aeba AS |
146 | /* Poll the STS_PSS status bit; see when it agrees with CMD_PSE */ |
147 | static void ehci_poll_PSS(struct ehci_hcd *ehci) | |
148 | { | |
149 | unsigned actual, want; | |
150 | ||
151 | /* Don't do anything if the controller isn't running (e.g., died) */ | |
152 | if (ehci->rh_state != EHCI_RH_RUNNING) | |
153 | return; | |
154 | ||
155 | want = (ehci->command & CMD_PSE) ? STS_PSS : 0; | |
156 | actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS; | |
157 | ||
158 | if (want != actual) { | |
159 | ||
160 | /* Poll again later, but give up after about 20 ms */ | |
161 | if (ehci->PSS_poll_count++ < 20) { | |
162 | ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true); | |
163 | return; | |
164 | } | |
165 | ehci_warn(ehci, "Waited too long for the periodic schedule status, giving up\n"); | |
166 | } | |
167 | ehci->PSS_poll_count = 0; | |
168 | ||
169 | /* The status is up-to-date; restart or stop the schedule as needed */ | |
170 | if (want == 0) { /* Stopped */ | |
3ca9aeba AS |
171 | if (ehci->periodic_count > 0) { |
172 | ||
173 | /* make sure ehci_work scans these */ | |
174 | ehci->next_uframe = ehci_read_frame_index(ehci) | |
175 | & ((ehci->periodic_size << 3) - 1); | |
176 | ehci_set_command_bit(ehci, CMD_PSE); | |
177 | } | |
178 | ||
179 | } else { /* Running */ | |
180 | if (ehci->periodic_count == 0) { | |
181 | ||
182 | /* Turn off the schedule after a while */ | |
183 | ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC, | |
184 | true); | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | /* Turn off the periodic schedule after a brief delay */ | |
190 | static void ehci_disable_PSE(struct ehci_hcd *ehci) | |
191 | { | |
192 | ehci_clear_command_bit(ehci, CMD_PSE); | |
3ca9aeba AS |
193 | } |
194 | ||
195 | ||
bf6387bc AS |
196 | /* Poll the STS_HALT status bit; see when a dead controller stops */ |
197 | static void ehci_handle_controller_death(struct ehci_hcd *ehci) | |
198 | { | |
199 | if (!(ehci_readl(ehci, &ehci->regs->status) & STS_HALT)) { | |
200 | ||
201 | /* Give up after a few milliseconds */ | |
202 | if (ehci->died_poll_count++ < 5) { | |
203 | /* Try again later */ | |
204 | ehci_enable_event(ehci, EHCI_HRTIMER_POLL_DEAD, true); | |
205 | return; | |
206 | } | |
207 | ehci_warn(ehci, "Waited too long for the controller to stop, giving up\n"); | |
208 | } | |
209 | ||
210 | /* Clean up the mess */ | |
211 | ehci->rh_state = EHCI_RH_HALTED; | |
212 | ehci_writel(ehci, 0, &ehci->regs->configured_flag); | |
213 | ehci_writel(ehci, 0, &ehci->regs->intr_enable); | |
214 | ehci_work(ehci); | |
3c273a05 | 215 | end_unlink_async(ehci); |
bf6387bc AS |
216 | |
217 | /* Not in process context, so don't try to reset the controller */ | |
218 | } | |
219 | ||
220 | ||
df202255 AS |
221 | /* Handle unlinked interrupt QHs once they are gone from the hardware */ |
222 | static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci) | |
223 | { | |
224 | bool stopped = (ehci->rh_state < EHCI_RH_RUNNING); | |
225 | ||
226 | /* | |
227 | * Process all the QHs on the intr_unlink list that were added | |
228 | * before the current unlink cycle began. The list is in | |
229 | * temporal order, so stop when we reach the first entry in the | |
230 | * current cycle. But if the root hub isn't running then | |
231 | * process all the QHs on the list. | |
232 | */ | |
233 | ehci->intr_unlinking = true; | |
234 | while (ehci->intr_unlink) { | |
235 | struct ehci_qh *qh = ehci->intr_unlink; | |
236 | ||
237 | if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle) | |
238 | break; | |
239 | ehci->intr_unlink = qh->unlink_next; | |
240 | qh->unlink_next = NULL; | |
241 | end_unlink_intr(ehci, qh); | |
242 | } | |
243 | ||
244 | /* Handle remaining entries later */ | |
245 | if (ehci->intr_unlink) { | |
246 | ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true); | |
247 | ++ehci->intr_unlink_cycle; | |
248 | } | |
249 | ehci->intr_unlinking = false; | |
250 | } | |
251 | ||
252 | ||
55934eb3 AS |
253 | /* Start another free-iTDs/siTDs cycle */ |
254 | static void start_free_itds(struct ehci_hcd *ehci) | |
255 | { | |
256 | if (!(ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_FREE_ITDS))) { | |
257 | ehci->last_itd_to_free = list_entry( | |
258 | ehci->cached_itd_list.prev, | |
259 | struct ehci_itd, itd_list); | |
260 | ehci->last_sitd_to_free = list_entry( | |
261 | ehci->cached_sitd_list.prev, | |
262 | struct ehci_sitd, sitd_list); | |
263 | ehci_enable_event(ehci, EHCI_HRTIMER_FREE_ITDS, true); | |
264 | } | |
265 | } | |
266 | ||
267 | /* Wait for controller to stop using old iTDs and siTDs */ | |
268 | static void end_free_itds(struct ehci_hcd *ehci) | |
269 | { | |
270 | struct ehci_itd *itd, *n; | |
271 | struct ehci_sitd *sitd, *sn; | |
272 | ||
273 | if (ehci->rh_state < EHCI_RH_RUNNING) { | |
274 | ehci->last_itd_to_free = NULL; | |
275 | ehci->last_sitd_to_free = NULL; | |
276 | } | |
277 | ||
278 | list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) { | |
279 | list_del(&itd->itd_list); | |
280 | dma_pool_free(ehci->itd_pool, itd, itd->itd_dma); | |
281 | if (itd == ehci->last_itd_to_free) | |
282 | break; | |
283 | } | |
284 | list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) { | |
285 | list_del(&sitd->sitd_list); | |
286 | dma_pool_free(ehci->sitd_pool, sitd, sitd->sitd_dma); | |
287 | if (sitd == ehci->last_sitd_to_free) | |
288 | break; | |
289 | } | |
290 | ||
291 | if (!list_empty(&ehci->cached_itd_list) || | |
292 | !list_empty(&ehci->cached_sitd_list)) | |
293 | start_free_itds(ehci); | |
294 | } | |
295 | ||
296 | ||
9d938747 AS |
297 | /* Handle lost (or very late) IAA interrupts */ |
298 | static void ehci_iaa_watchdog(struct ehci_hcd *ehci) | |
299 | { | |
300 | if (ehci->rh_state != EHCI_RH_RUNNING) | |
301 | return; | |
302 | ||
303 | /* | |
304 | * Lost IAA irqs wedge things badly; seen first with a vt8235. | |
305 | * So we need this watchdog, but must protect it against both | |
306 | * (a) SMP races against real IAA firing and retriggering, and | |
307 | * (b) clean HC shutdown, when IAA watchdog was pending. | |
308 | */ | |
3c273a05 | 309 | if (ehci->async_iaa) { |
9d938747 AS |
310 | u32 cmd, status; |
311 | ||
312 | /* If we get here, IAA is *REALLY* late. It's barely | |
313 | * conceivable that the system is so busy that CMD_IAAD | |
314 | * is still legitimately set, so let's be sure it's | |
315 | * clear before we read STS_IAA. (The HC should clear | |
316 | * CMD_IAAD when it sets STS_IAA.) | |
317 | */ | |
318 | cmd = ehci_readl(ehci, &ehci->regs->command); | |
319 | ||
320 | /* | |
321 | * If IAA is set here it either legitimately triggered | |
322 | * after the watchdog timer expired (_way_ late, so we'll | |
323 | * still count it as lost) ... or a silicon erratum: | |
324 | * - VIA seems to set IAA without triggering the IRQ; | |
325 | * - IAAD potentially cleared without setting IAA. | |
326 | */ | |
327 | status = ehci_readl(ehci, &ehci->regs->status); | |
328 | if ((status & STS_IAA) || !(cmd & CMD_IAAD)) { | |
329 | COUNT(ehci->stats.lost_iaa); | |
330 | ehci_writel(ehci, STS_IAA, &ehci->regs->status); | |
331 | } | |
332 | ||
333 | ehci_vdbg(ehci, "IAA watchdog: status %x cmd %x\n", | |
334 | status, cmd); | |
335 | end_unlink_async(ehci); | |
336 | } | |
337 | } | |
338 | ||
339 | ||
d58b4bcc AS |
340 | /* |
341 | * Handler functions for the hrtimer event types. | |
342 | * Keep this array in the same order as the event types indexed by | |
343 | * enum ehci_hrtimer_event in ehci.h. | |
344 | */ | |
345 | static void (*event_handlers[])(struct ehci_hcd *) = { | |
31446610 | 346 | ehci_poll_ASS, /* EHCI_HRTIMER_POLL_ASS */ |
3ca9aeba | 347 | ehci_poll_PSS, /* EHCI_HRTIMER_POLL_PSS */ |
bf6387bc | 348 | ehci_handle_controller_death, /* EHCI_HRTIMER_POLL_DEAD */ |
df202255 | 349 | ehci_handle_intr_unlinks, /* EHCI_HRTIMER_UNLINK_INTR */ |
55934eb3 | 350 | end_free_itds, /* EHCI_HRTIMER_FREE_ITDS */ |
32830f20 | 351 | unlink_empty_async, /* EHCI_HRTIMER_ASYNC_UNLINKS */ |
9d938747 | 352 | ehci_iaa_watchdog, /* EHCI_HRTIMER_IAA_WATCHDOG */ |
3ca9aeba | 353 | ehci_disable_PSE, /* EHCI_HRTIMER_DISABLE_PERIODIC */ |
31446610 | 354 | ehci_disable_ASE, /* EHCI_HRTIMER_DISABLE_ASYNC */ |
d58b4bcc AS |
355 | }; |
356 | ||
357 | static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t) | |
358 | { | |
359 | struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer); | |
360 | ktime_t now; | |
361 | unsigned long events; | |
362 | unsigned long flags; | |
363 | unsigned e; | |
364 | ||
365 | spin_lock_irqsave(&ehci->lock, flags); | |
366 | ||
367 | events = ehci->enabled_hrtimer_events; | |
368 | ehci->enabled_hrtimer_events = 0; | |
369 | ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT; | |
370 | ||
371 | /* | |
372 | * Check each pending event. If its time has expired, handle | |
373 | * the event; otherwise re-enable it. | |
374 | */ | |
375 | now = ktime_get(); | |
376 | for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) { | |
377 | if (now.tv64 >= ehci->hr_timeouts[e].tv64) | |
378 | event_handlers[e](ehci); | |
379 | else | |
380 | ehci_enable_event(ehci, e, false); | |
381 | } | |
382 | ||
383 | spin_unlock_irqrestore(&ehci->lock, flags); | |
384 | return HRTIMER_NORESTART; | |
385 | } |