1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20static 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
26 ehci_readl(ehci, &ehci->regs->command);
27}
28
29
30static 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
36 ehci_readl(ehci, &ehci->regs->command);
37}
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69static unsigned event_delays_ns[] = {
70 1 * NSEC_PER_MSEC,
71 1 * NSEC_PER_MSEC,
72 1 * NSEC_PER_MSEC,
73 1125 * NSEC_PER_USEC,
74 2 * NSEC_PER_MSEC,
75 2 * NSEC_PER_MSEC,
76 5 * NSEC_PER_MSEC,
77 6 * NSEC_PER_MSEC,
78 10 * NSEC_PER_MSEC,
79 10 * NSEC_PER_MSEC,
80 15 * NSEC_PER_MSEC,
81 100 * NSEC_PER_MSEC,
82};
83
84
85static void ehci_enable_event(struct ehci_hcd *ehci, unsigned event,
86 bool resched)
87{
88 ktime_t *timeout = &ehci->hr_timeouts[event];
89
90 if (resched)
91 *timeout = ktime_add(ktime_get(),
92 ktime_set(0, event_delays_ns[event]));
93 ehci->enabled_hrtimer_events |= (1 << event);
94
95
96 if (event < ehci->next_hrtimer_event) {
97 ehci->next_hrtimer_event = event;
98 hrtimer_start_range_ns(&ehci->hrtimer, *timeout,
99 NSEC_PER_MSEC, HRTIMER_MODE_ABS);
100 }
101}
102
103
104
105static void ehci_poll_ASS(struct ehci_hcd *ehci)
106{
107 unsigned actual, want;
108
109
110 if (ehci->rh_state != EHCI_RH_RUNNING)
111 return;
112
113 want = (ehci->command & CMD_ASE) ? STS_ASS : 0;
114 actual = ehci_readl(ehci, &ehci->regs->status) & STS_ASS;
115
116 if (want != actual) {
117
118
119 if (ehci->ASS_poll_count++ < 2) {
120 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_ASS, true);
121 return;
122 }
123 ehci_dbg(ehci, "Waited too long for the async schedule status (%x/%x), giving up\n",
124 want, actual);
125 }
126 ehci->ASS_poll_count = 0;
127
128
129 if (want == 0) {
130 if (ehci->async_count > 0)
131 ehci_set_command_bit(ehci, CMD_ASE);
132
133 } else {
134 if (ehci->async_count == 0) {
135
136
137 ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_ASYNC,
138 true);
139 }
140 }
141}
142
143
144static void ehci_disable_ASE(struct ehci_hcd *ehci)
145{
146 ehci_clear_command_bit(ehci, CMD_ASE);
147}
148
149
150
151static void ehci_poll_PSS(struct ehci_hcd *ehci)
152{
153 unsigned actual, want;
154
155
156 if (ehci->rh_state != EHCI_RH_RUNNING)
157 return;
158
159 want = (ehci->command & CMD_PSE) ? STS_PSS : 0;
160 actual = ehci_readl(ehci, &ehci->regs->status) & STS_PSS;
161
162 if (want != actual) {
163
164
165 if (ehci->PSS_poll_count++ < 2) {
166 ehci_enable_event(ehci, EHCI_HRTIMER_POLL_PSS, true);
167 return;
168 }
169 ehci_dbg(ehci, "Waited too long for the periodic schedule status (%x/%x), giving up\n",
170 want, actual);
171 }
172 ehci->PSS_poll_count = 0;
173
174
175 if (want == 0) {
176 if (ehci->periodic_count > 0)
177 ehci_set_command_bit(ehci, CMD_PSE);
178
179 } else {
180 if (ehci->periodic_count == 0) {
181
182
183 ehci_enable_event(ehci, EHCI_HRTIMER_DISABLE_PERIODIC,
184 true);
185 }
186 }
187}
188
189
190static void ehci_disable_PSE(struct ehci_hcd *ehci)
191{
192 ehci_clear_command_bit(ehci, CMD_PSE);
193}
194
195
196
197static void ehci_handle_controller_death(struct ehci_hcd *ehci)
198{
199 if (!(ehci_readl(ehci, &ehci->regs->status) & STS_HALT)) {
200
201
202 if (ehci->died_poll_count++ < 5) {
203
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
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);
215 end_unlink_async(ehci);
216
217
218}
219
220
221static void ehci_handle_start_intr_unlinks(struct ehci_hcd *ehci)
222{
223 bool stopped = (ehci->rh_state < EHCI_RH_RUNNING);
224
225
226
227
228
229
230
231
232 while (!list_empty(&ehci->intr_unlink_wait)) {
233 struct ehci_qh *qh;
234
235 qh = list_first_entry(&ehci->intr_unlink_wait,
236 struct ehci_qh, unlink_node);
237 if (!stopped && (qh->unlink_cycle ==
238 ehci->intr_unlink_wait_cycle))
239 break;
240 list_del_init(&qh->unlink_node);
241 qh->unlink_reason |= QH_UNLINK_QUEUE_EMPTY;
242 start_unlink_intr(ehci, qh);
243 }
244
245
246 if (!list_empty(&ehci->intr_unlink_wait)) {
247 ehci_enable_event(ehci, EHCI_HRTIMER_START_UNLINK_INTR, true);
248 ++ehci->intr_unlink_wait_cycle;
249 }
250}
251
252
253static void ehci_handle_intr_unlinks(struct ehci_hcd *ehci)
254{
255 bool stopped = (ehci->rh_state < EHCI_RH_RUNNING);
256
257
258
259
260
261
262
263
264 ehci->intr_unlinking = true;
265 while (!list_empty(&ehci->intr_unlink)) {
266 struct ehci_qh *qh;
267
268 qh = list_first_entry(&ehci->intr_unlink, struct ehci_qh,
269 unlink_node);
270 if (!stopped && qh->unlink_cycle == ehci->intr_unlink_cycle)
271 break;
272 list_del_init(&qh->unlink_node);
273 end_unlink_intr(ehci, qh);
274 }
275
276
277 if (!list_empty(&ehci->intr_unlink)) {
278 ehci_enable_event(ehci, EHCI_HRTIMER_UNLINK_INTR, true);
279 ++ehci->intr_unlink_cycle;
280 }
281 ehci->intr_unlinking = false;
282}
283
284
285
286static void start_free_itds(struct ehci_hcd *ehci)
287{
288 if (!(ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_FREE_ITDS))) {
289 ehci->last_itd_to_free = list_entry(
290 ehci->cached_itd_list.prev,
291 struct ehci_itd, itd_list);
292 ehci->last_sitd_to_free = list_entry(
293 ehci->cached_sitd_list.prev,
294 struct ehci_sitd, sitd_list);
295 ehci_enable_event(ehci, EHCI_HRTIMER_FREE_ITDS, true);
296 }
297}
298
299
300static void end_free_itds(struct ehci_hcd *ehci)
301{
302 struct ehci_itd *itd, *n;
303 struct ehci_sitd *sitd, *sn;
304
305 if (ehci->rh_state < EHCI_RH_RUNNING) {
306 ehci->last_itd_to_free = NULL;
307 ehci->last_sitd_to_free = NULL;
308 }
309
310 list_for_each_entry_safe(itd, n, &ehci->cached_itd_list, itd_list) {
311 list_del(&itd->itd_list);
312 dma_pool_free(ehci->itd_pool, itd, itd->itd_dma);
313 if (itd == ehci->last_itd_to_free)
314 break;
315 }
316 list_for_each_entry_safe(sitd, sn, &ehci->cached_sitd_list, sitd_list) {
317 list_del(&sitd->sitd_list);
318 dma_pool_free(ehci->sitd_pool, sitd, sitd->sitd_dma);
319 if (sitd == ehci->last_sitd_to_free)
320 break;
321 }
322
323 if (!list_empty(&ehci->cached_itd_list) ||
324 !list_empty(&ehci->cached_sitd_list))
325 start_free_itds(ehci);
326}
327
328
329
330static void ehci_iaa_watchdog(struct ehci_hcd *ehci)
331{
332 u32 cmd, status;
333
334
335
336
337
338
339
340 if (!ehci->iaa_in_progress || ehci->rh_state != EHCI_RH_RUNNING)
341 return;
342
343
344
345
346
347
348
349 cmd = ehci_readl(ehci, &ehci->regs->command);
350
351
352
353
354
355
356
357
358 status = ehci_readl(ehci, &ehci->regs->status);
359 if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
360 COUNT(ehci->stats.lost_iaa);
361 ehci_writel(ehci, STS_IAA, &ehci->regs->status);
362 }
363
364 ehci_dbg(ehci, "IAA watchdog: status %x cmd %x\n", status, cmd);
365 end_iaa_cycle(ehci);
366}
367
368
369
370static void turn_on_io_watchdog(struct ehci_hcd *ehci)
371{
372
373 if (ehci->rh_state != EHCI_RH_RUNNING ||
374 (ehci->enabled_hrtimer_events &
375 BIT(EHCI_HRTIMER_IO_WATCHDOG)))
376 return;
377
378
379
380
381
382 if (ehci->isoc_count > 0 || (ehci->need_io_watchdog &&
383 ehci->async_count + ehci->intr_count > 0))
384 ehci_enable_event(ehci, EHCI_HRTIMER_IO_WATCHDOG, true);
385}
386
387
388
389
390
391
392
393static void (*event_handlers[])(struct ehci_hcd *) = {
394 ehci_poll_ASS,
395 ehci_poll_PSS,
396 ehci_handle_controller_death,
397 ehci_handle_intr_unlinks,
398 end_free_itds,
399 end_unlink_async,
400 ehci_handle_start_intr_unlinks,
401 unlink_empty_async,
402 ehci_iaa_watchdog,
403 ehci_disable_PSE,
404 ehci_disable_ASE,
405 ehci_work,
406};
407
408static enum hrtimer_restart ehci_hrtimer_func(struct hrtimer *t)
409{
410 struct ehci_hcd *ehci = container_of(t, struct ehci_hcd, hrtimer);
411 ktime_t now;
412 unsigned long events;
413 unsigned long flags;
414 unsigned e;
415
416 spin_lock_irqsave(&ehci->lock, flags);
417
418 events = ehci->enabled_hrtimer_events;
419 ehci->enabled_hrtimer_events = 0;
420 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
421
422
423
424
425
426 now = ktime_get();
427 for_each_set_bit(e, &events, EHCI_HRTIMER_NUM_EVENTS) {
428 if (now.tv64 >= ehci->hr_timeouts[e].tv64)
429 event_handlers[e](ehci);
430 else
431 ehci_enable_event(ehci, e, false);
432 }
433
434 spin_unlock_irqrestore(&ehci->lock, flags);
435 return HRTIMER_NORESTART;
436}
437