blob: 06d036f608f181a792a55c1899e8f820196dde6c [file] [log] [blame]
Paul Zimmerman7359d482013-03-11 17:47:59 -07001/*
2 * hcd_queue.c - DesignWare HS OTG Controller host queuing routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * This file contains the functions to manage Queue Heads and Queue
39 * Transfer Descriptors for Host mode
40 */
Douglas Andersonfb616e32016-01-28 18:20:08 -080041#include <linux/gcd.h>
Paul Zimmerman7359d482013-03-11 17:47:59 -070042#include <linux/kernel.h>
43#include <linux/module.h>
44#include <linux/spinlock.h>
45#include <linux/interrupt.h>
46#include <linux/dma-mapping.h>
47#include <linux/io.h>
48#include <linux/slab.h>
49#include <linux/usb.h>
50
51#include <linux/usb/hcd.h>
52#include <linux/usb/ch11.h>
53
54#include "core.h"
55#include "hcd.h"
56
Douglas Anderson17dd5b62016-01-28 18:19:59 -080057/* Wait this long before releasing periodic reservation */
58#define DWC2_UNRESERVE_DELAY (msecs_to_jiffies(5))
59
60/**
Douglas Andersonb951c6c2016-01-28 18:20:05 -080061 * dwc2_periodic_channel_available() - Checks that a channel is available for a
62 * periodic transfer
63 *
64 * @hsotg: The HCD state structure for the DWC OTG controller
65 *
66 * Return: 0 if successful, negative error code otherwise
67 */
68static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
69{
70 /*
71 * Currently assuming that there is a dedicated host channel for
72 * each periodic transaction plus at least one host channel for
73 * non-periodic transactions
74 */
75 int status;
76 int num_channels;
77
John Younbea8e862016-11-03 17:55:53 -070078 num_channels = hsotg->params.host_channels;
Douglas Andersonb951c6c2016-01-28 18:20:05 -080079 if (hsotg->periodic_channels + hsotg->non_periodic_channels <
80 num_channels
81 && hsotg->periodic_channels < num_channels - 1) {
82 status = 0;
83 } else {
84 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -080085 "%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
86 __func__, num_channels,
Douglas Andersonb951c6c2016-01-28 18:20:05 -080087 hsotg->periodic_channels, hsotg->non_periodic_channels);
88 status = -ENOSPC;
89 }
90
91 return status;
92}
93
94/**
95 * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
96 * for the specified QH in the periodic schedule
97 *
98 * @hsotg: The HCD state structure for the DWC OTG controller
99 * @qh: QH containing periodic bandwidth required
100 *
101 * Return: 0 if successful, negative error code otherwise
102 *
103 * For simplicity, this calculation assumes that all the transfers in the
104 * periodic schedule may occur in the same (micro)frame
105 */
106static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
107 struct dwc2_qh *qh)
108{
109 int status;
110 s16 max_claimed_usecs;
111
112 status = 0;
113
114 if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
115 /*
116 * High speed mode
117 * Max periodic usecs is 80% x 125 usec = 100 usec
118 */
119 max_claimed_usecs = 100 - qh->host_us;
120 } else {
121 /*
122 * Full speed mode
123 * Max periodic usecs is 90% x 1000 usec = 900 usec
124 */
125 max_claimed_usecs = 900 - qh->host_us;
126 }
127
128 if (hsotg->periodic_usecs > max_claimed_usecs) {
129 dev_err(hsotg->dev,
130 "%s: already claimed usecs %d, required usecs %d\n",
131 __func__, hsotg->periodic_usecs, qh->host_us);
132 status = -ENOSPC;
133 }
134
135 return status;
136}
137
138/**
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800139 * pmap_schedule() - Schedule time in a periodic bitmap (pmap).
140 *
141 * @map: The bitmap representing the schedule; will be updated
142 * upon success.
143 * @bits_per_period: The schedule represents several periods. This is how many
144 * bits are in each period. It's assumed that the beginning
145 * of the schedule will repeat after its end.
146 * @periods_in_map: The number of periods in the schedule.
147 * @num_bits: The number of bits we need per period we want to reserve
148 * in this function call.
149 * @interval: How often we need to be scheduled for the reservation this
150 * time. 1 means every period. 2 means every other period.
151 * ...you get the picture?
152 * @start: The bit number to start at. Normally 0. Must be within
153 * the interval or we return failure right away.
154 * @only_one_period: Normally we'll allow picking a start anywhere within the
155 * first interval, since we can still make all repetition
156 * requirements by doing that. However, if you pass true
157 * here then we'll return failure if we can't fit within
158 * the period that "start" is in.
159 *
160 * The idea here is that we want to schedule time for repeating events that all
161 * want the same resource. The resource is divided into fixed-sized periods
162 * and the events want to repeat every "interval" periods. The schedule
163 * granularity is one bit.
164 *
165 * To keep things "simple", we'll represent our schedule with a bitmap that
166 * contains a fixed number of periods. This gets rid of a lot of complexity
167 * but does mean that we need to handle things specially (and non-ideally) if
168 * the number of the periods in the schedule doesn't match well with the
169 * intervals that we're trying to schedule.
170 *
171 * Here's an explanation of the scheme we'll implement, assuming 8 periods.
172 * - If interval is 1, we need to take up space in each of the 8
173 * periods we're scheduling. Easy.
174 * - If interval is 2, we need to take up space in half of the
175 * periods. Again, easy.
176 * - If interval is 3, we actually need to fall back to interval 1.
177 * Why? Because we might need time in any period. AKA for the
178 * first 8 periods, we'll be in slot 0, 3, 6. Then we'll be
179 * in slot 1, 4, 7. Then we'll be in 2, 5. Then we'll be back to
180 * 0, 3, and 6. Since we could be in any frame we need to reserve
181 * for all of them. Sucks, but that's what you gotta do. Note that
182 * if we were instead scheduling 8 * 3 = 24 we'd do much better, but
183 * then we need more memory and time to do scheduling.
184 * - If interval is 4, easy.
185 * - If interval is 5, we again need interval 1. The schedule will be
186 * 0, 5, 2, 7, 4, 1, 6, 3, 0
187 * - If interval is 6, we need interval 2. 0, 6, 4, 2.
188 * - If interval is 7, we need interval 1.
189 * - If interval is 8, we need interval 8.
190 *
191 * If you do the math, you'll see that we need to pretend that interval is
192 * equal to the greatest_common_divisor(interval, periods_in_map).
193 *
194 * Note that at the moment this function tends to front-pack the schedule.
195 * In some cases that's really non-ideal (it's hard to schedule things that
196 * need to repeat every period). In other cases it's perfect (you can easily
197 * schedule bigger, less often repeating things).
198 *
199 * Here's the algorithm in action (8 periods, 5 bits per period):
200 * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0
201 * |*****| ***|*****| ***|*****| ***|*****| ***| OK 3 bits, intv 3 at 2
202 * |*****|* ***|*****| ***|*****|* ***|*****| ***| OK 1 bits, intv 4 at 5
203 * |** |* |** | |** |* |** | | Remv 3 bits, intv 3 at 2
204 * |*** |* |*** | |*** |* |*** | | OK 1 bits, intv 6 at 2
205 * |**** |* * |**** | * |**** |* * |**** | * | OK 1 bits, intv 1 at 3
206 * |**** |**** |**** | *** |**** |**** |**** | *** | OK 2 bits, intv 2 at 6
207 * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 1 at 4
208 * |*****|*****|*****| ****|*****|*****|*****| ****| FAIL 1 bits, intv 1
209 * | ***|*****| ***| ****| ***|*****| ***| ****| Remv 2 bits, intv 2 at 0
210 * | ***| ****| ***| ****| ***| ****| ***| ****| Remv 1 bits, intv 4 at 5
211 * | **| ****| **| ****| **| ****| **| ****| Remv 1 bits, intv 6 at 2
212 * | *| ** *| *| ** *| *| ** *| *| ** *| Remv 1 bits, intv 1 at 3
213 * | *| *| *| *| *| *| *| *| Remv 2 bits, intv 2 at 6
214 * | | | | | | | | | Remv 1 bits, intv 1 at 4
215 * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0
216 * |*** | |** | |*** | |** | | OK 1 bits, intv 4 at 2
217 * |*****| |** **| |*****| |** **| | OK 2 bits, intv 2 at 3
218 * |*****|* |** **| |*****|* |** **| | OK 1 bits, intv 4 at 5
219 * |*****|*** |** **| ** |*****|*** |** **| ** | OK 2 bits, intv 2 at 6
220 * |*****|*****|** **| ****|*****|*****|** **| ****| OK 2 bits, intv 2 at 8
221 * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 4 at 12
222 *
223 * This function is pretty generic and could be easily abstracted if anything
224 * needed similar scheduling.
225 *
226 * Returns either -ENOSPC or a >= 0 start bit which should be passed to the
227 * unschedule routine. The map bitmap will be updated on a non-error result.
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800228 */
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800229static int pmap_schedule(unsigned long *map, int bits_per_period,
230 int periods_in_map, int num_bits,
231 int interval, int start, bool only_one_period)
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800232{
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800233 int interval_bits;
234 int to_reserve;
235 int first_end;
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800236 int i;
237
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800238 if (num_bits > bits_per_period)
239 return -ENOSPC;
240
241 /* Adjust interval as per description */
242 interval = gcd(interval, periods_in_map);
243
244 interval_bits = bits_per_period * interval;
245 to_reserve = periods_in_map / interval;
246
247 /* If start has gotten us past interval then we can't schedule */
248 if (start >= interval_bits)
249 return -ENOSPC;
250
251 if (only_one_period)
252 /* Must fit within same period as start; end at begin of next */
253 first_end = (start / bits_per_period + 1) * bits_per_period;
254 else
255 /* Can fit anywhere in the first interval */
256 first_end = interval_bits;
257
258 /*
259 * We'll try to pick the first repetition, then see if that time
260 * is free for each of the subsequent repetitions. If it's not
261 * we'll adjust the start time for the next search of the first
262 * repetition.
263 */
264 while (start + num_bits <= first_end) {
265 int end;
266
267 /* Need to stay within this period */
268 end = (start / bits_per_period + 1) * bits_per_period;
269
270 /* Look for num_bits us in this microframe starting at start */
271 start = bitmap_find_next_zero_area(map, end, start, num_bits,
272 0);
273
274 /*
275 * We should get start >= end if we fail. We might be
276 * able to check the next microframe depending on the
277 * interval, so continue on (start already updated).
278 */
279 if (start >= end) {
280 start = end;
281 continue;
282 }
283
284 /* At this point we have a valid point for first one */
285 for (i = 1; i < to_reserve; i++) {
286 int ith_start = start + interval_bits * i;
287 int ith_end = end + interval_bits * i;
288 int ret;
289
290 /* Use this as a dumb "check if bits are 0" */
291 ret = bitmap_find_next_zero_area(
292 map, ith_start + num_bits, ith_start, num_bits,
293 0);
294
295 /* We got the right place, continue checking */
296 if (ret == ith_start)
297 continue;
298
299 /* Move start up for next time and exit for loop */
300 ith_start = bitmap_find_next_zero_area(
301 map, ith_end, ith_start, num_bits, 0);
302 if (ith_start >= ith_end)
303 /* Need a while new period next time */
304 start = end;
305 else
306 start = ith_start - interval_bits * i;
307 break;
308 }
309
310 /* If didn't exit the for loop with a break, we have success */
311 if (i == to_reserve)
312 break;
313 }
314
315 if (start + num_bits > first_end)
316 return -ENOSPC;
317
318 for (i = 0; i < to_reserve; i++) {
319 int ith_start = start + interval_bits * i;
320
321 bitmap_set(map, ith_start, num_bits);
322 }
323
324 return start;
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800325}
326
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800327/**
328 * pmap_unschedule() - Undo work done by pmap_schedule()
329 *
330 * @map: See pmap_schedule().
331 * @bits_per_period: See pmap_schedule().
332 * @periods_in_map: See pmap_schedule().
333 * @num_bits: The number of bits that was passed to schedule.
334 * @interval: The interval that was passed to schedule.
335 * @start: The return value from pmap_schedule().
336 */
337static void pmap_unschedule(unsigned long *map, int bits_per_period,
338 int periods_in_map, int num_bits,
339 int interval, int start)
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800340{
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800341 int interval_bits;
342 int to_release;
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800343 int i;
344
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800345 /* Adjust interval as per description in pmap_schedule() */
346 interval = gcd(interval, periods_in_map);
347
348 interval_bits = bits_per_period * interval;
349 to_release = periods_in_map / interval;
350
351 for (i = 0; i < to_release; i++) {
352 int ith_start = start + interval_bits * i;
353
354 bitmap_clear(map, ith_start, num_bits);
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800355 }
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800356}
357
Vardan Mikayelyan3c2203702016-11-10 17:38:21 -0800358/**
359 * dwc2_get_ls_map() - Get the map used for the given qh
360 *
361 * @hsotg: The HCD state structure for the DWC OTG controller.
362 * @qh: QH for the periodic transfer.
363 *
364 * We'll always get the periodic map out of our TT. Note that even if we're
365 * running the host straight in low speed / full speed mode it appears as if
366 * a TT is allocated for us, so we'll use it. If that ever changes we can
367 * add logic here to get a map out of "hsotg" if !qh->do_split.
368 *
369 * Returns: the map or NULL if a map couldn't be found.
370 */
371static unsigned long *dwc2_get_ls_map(struct dwc2_hsotg *hsotg,
372 struct dwc2_qh *qh)
373{
374 unsigned long *map;
375
376 /* Don't expect to be missing a TT and be doing low speed scheduling */
377 if (WARN_ON(!qh->dwc_tt))
378 return NULL;
379
380 /* Get the map and adjust if this is a multi_tt hub */
381 map = qh->dwc_tt->periodic_bitmaps;
382 if (qh->dwc_tt->usb_tt->multi)
383 map += DWC2_ELEMENTS_PER_LS_BITMAP * qh->ttport;
384
385 return map;
386}
387
388#ifdef DWC2_PRINT_SCHEDULE
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800389/*
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800390 * cat_printf() - A printf() + strcat() helper
391 *
392 * This is useful for concatenating a bunch of strings where each string is
393 * constructed using printf.
394 *
395 * @buf: The destination buffer; will be updated to point after the printed
396 * data.
397 * @size: The number of bytes in the buffer (includes space for '\0').
398 * @fmt: The format for printf.
399 * @...: The args for printf.
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800400 */
Nicolas Ioosse135ab72016-06-26 10:12:38 +0200401static __printf(3, 4)
402void cat_printf(char **buf, size_t *size, const char *fmt, ...)
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800403{
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800404 va_list args;
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800405 int i;
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800406
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800407 if (*size == 0)
408 return;
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800409
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800410 va_start(args, fmt);
411 i = vsnprintf(*buf, *size, fmt, args);
412 va_end(args);
413
414 if (i >= *size) {
415 (*buf)[*size - 1] = '\0';
416 *buf += *size;
417 *size = 0;
418 } else {
419 *buf += i;
420 *size -= i;
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800421 }
Douglas Andersonb951c6c2016-01-28 18:20:05 -0800422}
423
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800424/*
425 * pmap_print() - Print the given periodic map
426 *
427 * Will attempt to print out the periodic schedule.
428 *
429 * @map: See pmap_schedule().
430 * @bits_per_period: See pmap_schedule().
431 * @periods_in_map: See pmap_schedule().
432 * @period_name: The name of 1 period, like "uFrame"
433 * @units: The name of the units, like "us".
434 * @print_fn: The function to call for printing.
435 * @print_data: Opaque data to pass to the print function.
436 */
437static void pmap_print(unsigned long *map, int bits_per_period,
438 int periods_in_map, const char *period_name,
439 const char *units,
440 void (*print_fn)(const char *str, void *data),
441 void *print_data)
442{
443 int period;
444
445 for (period = 0; period < periods_in_map; period++) {
446 char tmp[64];
447 char *buf = tmp;
448 size_t buf_size = sizeof(tmp);
449 int period_start = period * bits_per_period;
450 int period_end = period_start + bits_per_period;
451 int start = 0;
452 int count = 0;
453 bool printed = false;
454 int i;
455
456 for (i = period_start; i < period_end + 1; i++) {
457 /* Handle case when ith bit is set */
458 if (i < period_end &&
459 bitmap_find_next_zero_area(map, i + 1,
460 i, 1, 0) != i) {
461 if (count == 0)
462 start = i - period_start;
463 count++;
464 continue;
465 }
466
467 /* ith bit isn't set; don't care if count == 0 */
468 if (count == 0)
469 continue;
470
471 if (!printed)
472 cat_printf(&buf, &buf_size, "%s %d: ",
473 period_name, period);
474 else
475 cat_printf(&buf, &buf_size, ", ");
476 printed = true;
477
478 cat_printf(&buf, &buf_size, "%d %s -%3d %s", start,
479 units, start + count - 1, units);
480 count = 0;
481 }
482
483 if (printed)
484 print_fn(tmp, print_data);
485 }
486}
487
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800488struct dwc2_qh_print_data {
489 struct dwc2_hsotg *hsotg;
490 struct dwc2_qh *qh;
491};
492
493/**
494 * dwc2_qh_print() - Helper function for dwc2_qh_schedule_print()
495 *
496 * @str: The string to print
497 * @data: A pointer to a struct dwc2_qh_print_data
498 */
499static void dwc2_qh_print(const char *str, void *data)
500{
501 struct dwc2_qh_print_data *print_data = data;
502
503 dwc2_sch_dbg(print_data->hsotg, "QH=%p ...%s\n", print_data->qh, str);
504}
505
506/**
507 * dwc2_qh_schedule_print() - Print the periodic schedule
508 *
509 * @hsotg: The HCD state structure for the DWC OTG controller.
510 * @qh: QH to print.
511 */
512static void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
513 struct dwc2_qh *qh)
514{
515 struct dwc2_qh_print_data print_data = { hsotg, qh };
516 int i;
517
518 /*
519 * The printing functions are quite slow and inefficient.
520 * If we don't have tracing turned on, don't run unless the special
521 * define is turned on.
522 */
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800523
524 if (qh->schedule_low_speed) {
525 unsigned long *map = dwc2_get_ls_map(hsotg, qh);
526
527 dwc2_sch_dbg(hsotg, "QH=%p LS/FS trans: %d=>%d us @ %d us",
528 qh, qh->device_us,
529 DWC2_ROUND_US_TO_SLICE(qh->device_us),
530 DWC2_US_PER_SLICE * qh->ls_start_schedule_slice);
531
532 if (map) {
533 dwc2_sch_dbg(hsotg,
534 "QH=%p Whole low/full speed map %p now:\n",
535 qh, map);
536 pmap_print(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
537 DWC2_LS_SCHEDULE_FRAMES, "Frame ", "slices",
538 dwc2_qh_print, &print_data);
539 }
540 }
541
542 for (i = 0; i < qh->num_hs_transfers; i++) {
543 struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + i;
544 int uframe = trans_time->start_schedule_us /
545 DWC2_HS_PERIODIC_US_PER_UFRAME;
546 int rel_us = trans_time->start_schedule_us %
547 DWC2_HS_PERIODIC_US_PER_UFRAME;
548
549 dwc2_sch_dbg(hsotg,
550 "QH=%p HS trans #%d: %d us @ uFrame %d + %d us\n",
551 qh, i, trans_time->duration_us, uframe, rel_us);
552 }
553 if (qh->num_hs_transfers) {
554 dwc2_sch_dbg(hsotg, "QH=%p Whole high speed map now:\n", qh);
555 pmap_print(hsotg->hs_periodic_bitmap,
556 DWC2_HS_PERIODIC_US_PER_UFRAME,
557 DWC2_HS_SCHEDULE_UFRAMES, "uFrame", "us",
558 dwc2_qh_print, &print_data);
559 }
Vardan Mikayelyan3c2203702016-11-10 17:38:21 -0800560 return;
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800561}
Vardan Mikayelyan3c2203702016-11-10 17:38:21 -0800562#else
563static inline void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
564 struct dwc2_qh *qh) {};
565#endif
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800566
567/**
568 * dwc2_ls_pmap_schedule() - Schedule a low speed QH
569 *
570 * @hsotg: The HCD state structure for the DWC OTG controller.
571 * @qh: QH for the periodic transfer.
572 * @search_slice: We'll start trying to schedule at the passed slice.
573 * Remember that slices are the units of the low speed
574 * schedule (think 25us or so).
575 *
576 * Wraps pmap_schedule() with the right parameters for low speed scheduling.
577 *
578 * Normally we schedule low speed devices on the map associated with the TT.
579 *
580 * Returns: 0 for success or an error code.
581 */
582static int dwc2_ls_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
583 int search_slice)
584{
585 int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
586 unsigned long *map = dwc2_get_ls_map(hsotg, qh);
587 int slice;
588
John Youn9da51972017-01-17 20:30:27 -0800589 if (!map)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800590 return -EINVAL;
591
592 /*
593 * Schedule on the proper low speed map with our low speed scheduling
594 * parameters. Note that we use the "device_interval" here since
595 * we want the low speed interval and the only way we'd be in this
596 * function is if the device is low speed.
597 *
598 * If we happen to be doing low speed and high speed scheduling for the
599 * same transaction (AKA we have a split) we always do low speed first.
600 * That means we can always pass "false" for only_one_period (that
601 * parameters is only useful when we're trying to get one schedule to
602 * match what we already planned in the other schedule).
603 */
604 slice = pmap_schedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
605 DWC2_LS_SCHEDULE_FRAMES, slices,
606 qh->device_interval, search_slice, false);
607
608 if (slice < 0)
609 return slice;
610
611 qh->ls_start_schedule_slice = slice;
612 return 0;
613}
614
615/**
616 * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_ls_pmap_schedule()
617 *
618 * @hsotg: The HCD state structure for the DWC OTG controller.
619 * @qh: QH for the periodic transfer.
620 */
621static void dwc2_ls_pmap_unschedule(struct dwc2_hsotg *hsotg,
622 struct dwc2_qh *qh)
623{
624 int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
625 unsigned long *map = dwc2_get_ls_map(hsotg, qh);
626
627 /* Schedule should have failed, so no worries about no error code */
John Youn9da51972017-01-17 20:30:27 -0800628 if (!map)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -0800629 return;
630
631 pmap_unschedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
632 DWC2_LS_SCHEDULE_FRAMES, slices, qh->device_interval,
633 qh->ls_start_schedule_slice);
634}
635
636/**
637 * dwc2_hs_pmap_schedule - Schedule in the main high speed schedule
638 *
639 * This will schedule something on the main dwc2 schedule.
640 *
641 * We'll start looking in qh->hs_transfers[index].start_schedule_us. We'll
642 * update this with the result upon success. We also use the duration from
643 * the same structure.
644 *
645 * @hsotg: The HCD state structure for the DWC OTG controller.
646 * @qh: QH for the periodic transfer.
647 * @only_one_period: If true we will limit ourselves to just looking at
648 * one period (aka one 100us chunk). This is used if we have
649 * already scheduled something on the low speed schedule and
650 * need to find something that matches on the high speed one.
651 * @index: The index into qh->hs_transfers that we're working with.
652 *
653 * Returns: 0 for success or an error code. Upon success the
654 * dwc2_hs_transfer_time specified by "index" will be updated.
655 */
656static int dwc2_hs_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
657 bool only_one_period, int index)
658{
659 struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
660 int us;
661
662 us = pmap_schedule(hsotg->hs_periodic_bitmap,
663 DWC2_HS_PERIODIC_US_PER_UFRAME,
664 DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
665 qh->host_interval, trans_time->start_schedule_us,
666 only_one_period);
667
668 if (us < 0)
669 return us;
670
671 trans_time->start_schedule_us = us;
672 return 0;
673}
674
675/**
676 * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_hs_pmap_schedule()
677 *
678 * @hsotg: The HCD state structure for the DWC OTG controller.
679 * @qh: QH for the periodic transfer.
680 */
681static void dwc2_hs_pmap_unschedule(struct dwc2_hsotg *hsotg,
682 struct dwc2_qh *qh, int index)
683{
684 struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
685
686 pmap_unschedule(hsotg->hs_periodic_bitmap,
687 DWC2_HS_PERIODIC_US_PER_UFRAME,
688 DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
689 qh->host_interval, trans_time->start_schedule_us);
690}
691
692/**
693 * dwc2_uframe_schedule_split - Schedule a QH for a periodic split xfer.
694 *
695 * This is the most complicated thing in USB. We have to find matching time
696 * in both the global high speed schedule for the port and the low speed
697 * schedule for the TT associated with the given device.
698 *
699 * Being here means that the host must be running in high speed mode and the
700 * device is in low or full speed mode (and behind a hub).
701 *
702 * @hsotg: The HCD state structure for the DWC OTG controller.
703 * @qh: QH for the periodic transfer.
704 */
705static int dwc2_uframe_schedule_split(struct dwc2_hsotg *hsotg,
706 struct dwc2_qh *qh)
707{
708 int bytecount = dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
709 int ls_search_slice;
710 int err = 0;
711 int host_interval_in_sched;
712
713 /*
714 * The interval (how often to repeat) in the actual host schedule.
715 * See pmap_schedule() for gcd() explanation.
716 */
717 host_interval_in_sched = gcd(qh->host_interval,
718 DWC2_HS_SCHEDULE_UFRAMES);
719
720 /*
721 * We always try to find space in the low speed schedule first, then
722 * try to find high speed time that matches. If we don't, we'll bump
723 * up the place we start searching in the low speed schedule and try
724 * again. To start we'll look right at the beginning of the low speed
725 * schedule.
726 *
727 * Note that this will tend to front-load the high speed schedule.
728 * We may eventually want to try to avoid this by either considering
729 * both schedules together or doing some sort of round robin.
730 */
731 ls_search_slice = 0;
732
733 while (ls_search_slice < DWC2_LS_SCHEDULE_SLICES) {
734 int start_s_uframe;
735 int ssplit_s_uframe;
736 int second_s_uframe;
737 int rel_uframe;
738 int first_count;
739 int middle_count;
740 int end_count;
741 int first_data_bytes;
742 int other_data_bytes;
743 int i;
744
745 if (qh->schedule_low_speed) {
746 err = dwc2_ls_pmap_schedule(hsotg, qh, ls_search_slice);
747
748 /*
749 * If we got an error here there's no other magic we
750 * can do, so bail. All the looping above is only
751 * helpful to redo things if we got a low speed slot
752 * and then couldn't find a matching high speed slot.
753 */
754 if (err)
755 return err;
756 } else {
757 /* Must be missing the tt structure? Why? */
758 WARN_ON_ONCE(1);
759 }
760
761 /*
762 * This will give us a number 0 - 7 if
763 * DWC2_LS_SCHEDULE_FRAMES == 1, or 0 - 15 if == 2, or ...
764 */
765 start_s_uframe = qh->ls_start_schedule_slice /
766 DWC2_SLICES_PER_UFRAME;
767
768 /* Get a number that's always 0 - 7 */
769 rel_uframe = (start_s_uframe % 8);
770
771 /*
772 * If we were going to start in uframe 7 then we would need to
773 * issue a start split in uframe 6, which spec says is not OK.
774 * Move on to the next full frame (assuming there is one).
775 *
776 * See 11.18.4 Host Split Transaction Scheduling Requirements
777 * bullet 1.
778 */
779 if (rel_uframe == 7) {
780 if (qh->schedule_low_speed)
781 dwc2_ls_pmap_unschedule(hsotg, qh);
782 ls_search_slice =
783 (qh->ls_start_schedule_slice /
784 DWC2_LS_PERIODIC_SLICES_PER_FRAME + 1) *
785 DWC2_LS_PERIODIC_SLICES_PER_FRAME;
786 continue;
787 }
788
789 /*
790 * For ISOC in:
791 * - start split (frame -1)
792 * - complete split w/ data (frame +1)
793 * - complete split w/ data (frame +2)
794 * - ...
795 * - complete split w/ data (frame +num_data_packets)
796 * - complete split w/ data (frame +num_data_packets+1)
797 * - complete split w/ data (frame +num_data_packets+2, max 8)
798 * ...though if frame was "0" then max is 7...
799 *
800 * For ISOC out we might need to do:
801 * - start split w/ data (frame -1)
802 * - start split w/ data (frame +0)
803 * - ...
804 * - start split w/ data (frame +num_data_packets-2)
805 *
806 * For INTERRUPT in we might need to do:
807 * - start split (frame -1)
808 * - complete split w/ data (frame +1)
809 * - complete split w/ data (frame +2)
810 * - complete split w/ data (frame +3, max 8)
811 *
812 * For INTERRUPT out we might need to do:
813 * - start split w/ data (frame -1)
814 * - complete split (frame +1)
815 * - complete split (frame +2)
816 * - complete split (frame +3, max 8)
817 *
818 * Start adjusting!
819 */
820 ssplit_s_uframe = (start_s_uframe +
821 host_interval_in_sched - 1) %
822 host_interval_in_sched;
823 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in)
824 second_s_uframe = start_s_uframe;
825 else
826 second_s_uframe = start_s_uframe + 1;
827
828 /* First data transfer might not be all 188 bytes. */
829 first_data_bytes = 188 -
830 DIV_ROUND_UP(188 * (qh->ls_start_schedule_slice %
831 DWC2_SLICES_PER_UFRAME),
832 DWC2_SLICES_PER_UFRAME);
833 if (first_data_bytes > bytecount)
834 first_data_bytes = bytecount;
835 other_data_bytes = bytecount - first_data_bytes;
836
837 /*
838 * For now, skip OUT xfers where first xfer is partial
839 *
840 * Main dwc2 code assumes:
841 * - INT transfers never get split in two.
842 * - ISOC transfers can always transfer 188 bytes the first
843 * time.
844 *
845 * Until that code is fixed, try again if the first transfer
846 * couldn't transfer everything.
847 *
848 * This code can be removed if/when the rest of dwc2 handles
849 * the above cases. Until it's fixed we just won't be able
850 * to schedule quite as tightly.
851 */
852 if (!qh->ep_is_in &&
853 (first_data_bytes != min_t(int, 188, bytecount))) {
854 dwc2_sch_dbg(hsotg,
855 "QH=%p avoiding broken 1st xfer (%d, %d)\n",
856 qh, first_data_bytes, bytecount);
857 if (qh->schedule_low_speed)
858 dwc2_ls_pmap_unschedule(hsotg, qh);
859 ls_search_slice = (start_s_uframe + 1) *
860 DWC2_SLICES_PER_UFRAME;
861 continue;
862 }
863
864 /* Start by assuming transfers for the bytes */
865 qh->num_hs_transfers = 1 + DIV_ROUND_UP(other_data_bytes, 188);
866
867 /*
868 * Everything except ISOC OUT has extra transfers. Rules are
869 * complicated. See 11.18.4 Host Split Transaction Scheduling
870 * Requirements bullet 3.
871 */
872 if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
873 if (rel_uframe == 6)
874 qh->num_hs_transfers += 2;
875 else
876 qh->num_hs_transfers += 3;
877
878 if (qh->ep_is_in) {
879 /*
880 * First is start split, middle/end is data.
881 * Allocate full data bytes for all data.
882 */
883 first_count = 4;
884 middle_count = bytecount;
885 end_count = bytecount;
886 } else {
887 /*
888 * First is data, middle/end is complete.
889 * First transfer and second can have data.
890 * Rest should just have complete split.
891 */
892 first_count = first_data_bytes;
893 middle_count = max_t(int, 4, other_data_bytes);
894 end_count = 4;
895 }
896 } else {
897 if (qh->ep_is_in) {
898 int last;
899
900 /* Account for the start split */
901 qh->num_hs_transfers++;
902
903 /* Calculate "L" value from spec */
904 last = rel_uframe + qh->num_hs_transfers + 1;
905
906 /* Start with basic case */
907 if (last <= 6)
908 qh->num_hs_transfers += 2;
909 else
910 qh->num_hs_transfers += 1;
911
912 /* Adjust downwards */
913 if (last >= 6 && rel_uframe == 0)
914 qh->num_hs_transfers--;
915
916 /* 1st = start; rest can contain data */
917 first_count = 4;
918 middle_count = min_t(int, 188, bytecount);
919 end_count = middle_count;
920 } else {
921 /* All contain data, last might be smaller */
922 first_count = first_data_bytes;
923 middle_count = min_t(int, 188,
924 other_data_bytes);
925 end_count = other_data_bytes % 188;
926 }
927 }
928
929 /* Assign durations per uFrame */
930 qh->hs_transfers[0].duration_us = HS_USECS_ISO(first_count);
931 for (i = 1; i < qh->num_hs_transfers - 1; i++)
932 qh->hs_transfers[i].duration_us =
933 HS_USECS_ISO(middle_count);
934 if (qh->num_hs_transfers > 1)
935 qh->hs_transfers[qh->num_hs_transfers - 1].duration_us =
936 HS_USECS_ISO(end_count);
937
938 /*
939 * Assign start us. The call below to dwc2_hs_pmap_schedule()
940 * will start with these numbers but may adjust within the same
941 * microframe.
942 */
943 qh->hs_transfers[0].start_schedule_us =
944 ssplit_s_uframe * DWC2_HS_PERIODIC_US_PER_UFRAME;
945 for (i = 1; i < qh->num_hs_transfers; i++)
946 qh->hs_transfers[i].start_schedule_us =
947 ((second_s_uframe + i - 1) %
948 DWC2_HS_SCHEDULE_UFRAMES) *
949 DWC2_HS_PERIODIC_US_PER_UFRAME;
950
951 /* Try to schedule with filled in hs_transfers above */
952 for (i = 0; i < qh->num_hs_transfers; i++) {
953 err = dwc2_hs_pmap_schedule(hsotg, qh, true, i);
954 if (err)
955 break;
956 }
957
958 /* If we scheduled all w/out breaking out then we're all good */
959 if (i == qh->num_hs_transfers)
960 break;
961
962 for (; i >= 0; i--)
963 dwc2_hs_pmap_unschedule(hsotg, qh, i);
964
965 if (qh->schedule_low_speed)
966 dwc2_ls_pmap_unschedule(hsotg, qh);
967
968 /* Try again starting in the next microframe */
969 ls_search_slice = (start_s_uframe + 1) * DWC2_SLICES_PER_UFRAME;
970 }
971
972 if (ls_search_slice >= DWC2_LS_SCHEDULE_SLICES)
973 return -ENOSPC;
974
975 return 0;
976}
977
978/**
979 * dwc2_uframe_schedule_hs - Schedule a QH for a periodic high speed xfer.
980 *
981 * Basically this just wraps dwc2_hs_pmap_schedule() to provide a clean
982 * interface.
983 *
984 * @hsotg: The HCD state structure for the DWC OTG controller.
985 * @qh: QH for the periodic transfer.
986 */
987static int dwc2_uframe_schedule_hs(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
988{
989 /* In non-split host and device time are the same */
990 WARN_ON(qh->host_us != qh->device_us);
991 WARN_ON(qh->host_interval != qh->device_interval);
992 WARN_ON(qh->num_hs_transfers != 1);
993
994 /* We'll have one transfer; init start to 0 before calling scheduler */
995 qh->hs_transfers[0].start_schedule_us = 0;
996 qh->hs_transfers[0].duration_us = qh->host_us;
997
998 return dwc2_hs_pmap_schedule(hsotg, qh, false, 0);
999}
1000
1001/**
1002 * dwc2_uframe_schedule_ls - Schedule a QH for a periodic low/full speed xfer.
1003 *
1004 * Basically this just wraps dwc2_ls_pmap_schedule() to provide a clean
1005 * interface.
1006 *
1007 * @hsotg: The HCD state structure for the DWC OTG controller.
1008 * @qh: QH for the periodic transfer.
1009 */
1010static int dwc2_uframe_schedule_ls(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1011{
1012 /* In non-split host and device time are the same */
1013 WARN_ON(qh->host_us != qh->device_us);
1014 WARN_ON(qh->host_interval != qh->device_interval);
1015 WARN_ON(!qh->schedule_low_speed);
1016
1017 /* Run on the main low speed schedule (no split = no hub = no TT) */
1018 return dwc2_ls_pmap_schedule(hsotg, qh, 0);
1019}
1020
1021/**
1022 * dwc2_uframe_schedule - Schedule a QH for a periodic xfer.
1023 *
1024 * Calls one of the 3 sub-function depending on what type of transfer this QH
1025 * is for. Also adds some printing.
1026 *
1027 * @hsotg: The HCD state structure for the DWC OTG controller.
1028 * @qh: QH for the periodic transfer.
1029 */
1030static int dwc2_uframe_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001031{
1032 int ret;
1033
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001034 if (qh->dev_speed == USB_SPEED_HIGH)
1035 ret = dwc2_uframe_schedule_hs(hsotg, qh);
1036 else if (!qh->do_split)
1037 ret = dwc2_uframe_schedule_ls(hsotg, qh);
1038 else
1039 ret = dwc2_uframe_schedule_split(hsotg, qh);
1040
1041 if (ret)
1042 dwc2_sch_dbg(hsotg, "QH=%p Failed to schedule %d\n", qh, ret);
1043 else
1044 dwc2_qh_schedule_print(hsotg, qh);
1045
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001046 return ret;
1047}
1048
1049/**
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001050 * dwc2_uframe_unschedule - Undoes dwc2_uframe_schedule().
1051 *
1052 * @hsotg: The HCD state structure for the DWC OTG controller.
1053 * @qh: QH for the periodic transfer.
1054 */
1055static void dwc2_uframe_unschedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1056{
1057 int i;
1058
1059 for (i = 0; i < qh->num_hs_transfers; i++)
1060 dwc2_hs_pmap_unschedule(hsotg, qh, i);
1061
1062 if (qh->schedule_low_speed)
1063 dwc2_ls_pmap_unschedule(hsotg, qh);
1064
1065 dwc2_sch_dbg(hsotg, "QH=%p Unscheduled\n", qh);
1066}
1067
1068/**
Douglas Andersonfb616e32016-01-28 18:20:08 -08001069 * dwc2_pick_first_frame() - Choose 1st frame for qh that's already scheduled
1070 *
1071 * Takes a qh that has already been scheduled (which means we know we have the
1072 * bandwdith reserved for us) and set the next_active_frame and the
1073 * start_active_frame.
1074 *
1075 * This is expected to be called on qh's that weren't previously actively
1076 * running. It just picks the next frame that we can fit into without any
1077 * thought about the past.
1078 *
1079 * @hsotg: The HCD state structure for the DWC OTG controller
1080 * @qh: QH for a periodic endpoint
1081 *
1082 */
1083static void dwc2_pick_first_frame(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1084{
1085 u16 frame_number;
1086 u16 earliest_frame;
1087 u16 next_active_frame;
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001088 u16 relative_frame;
Douglas Andersonfb616e32016-01-28 18:20:08 -08001089 u16 interval;
1090
1091 /*
1092 * Use the real frame number rather than the cached value as of the
1093 * last SOF to give us a little extra slop.
1094 */
1095 frame_number = dwc2_hcd_get_frame_number(hsotg);
1096
1097 /*
1098 * We wouldn't want to start any earlier than the next frame just in
1099 * case the frame number ticks as we're doing this calculation.
1100 *
1101 * NOTE: if we could quantify how long till we actually get scheduled
1102 * we might be able to avoid the "+ 1" by looking at the upper part of
1103 * HFNUM (the FRREM field). For now we'll just use the + 1 though.
1104 */
1105 earliest_frame = dwc2_frame_num_inc(frame_number, 1);
1106 next_active_frame = earliest_frame;
1107
1108 /* Get the "no microframe schduler" out of the way... */
John Younbea8e862016-11-03 17:55:53 -07001109 if (hsotg->params.uframe_sched <= 0) {
Douglas Andersonfb616e32016-01-28 18:20:08 -08001110 if (qh->do_split)
1111 /* Splits are active at microframe 0 minus 1 */
1112 next_active_frame |= 0x7;
1113 goto exit;
1114 }
1115
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001116 if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
1117 /*
1118 * We're either at high speed or we're doing a split (which
1119 * means we're talking high speed to a hub). In any case
1120 * the first frame should be based on when the first scheduled
1121 * event is.
1122 */
1123 WARN_ON(qh->num_hs_transfers < 1);
1124
1125 relative_frame = qh->hs_transfers[0].start_schedule_us /
1126 DWC2_HS_PERIODIC_US_PER_UFRAME;
1127
1128 /* Adjust interval as per high speed schedule */
1129 interval = gcd(qh->host_interval, DWC2_HS_SCHEDULE_UFRAMES);
1130
1131 } else {
1132 /*
1133 * Low or full speed directly on dwc2. Just about the same
1134 * as high speed but on a different schedule and with slightly
1135 * different adjustments. Note that this works because when
1136 * the host and device are both low speed then frames in the
1137 * controller tick at low speed.
1138 */
1139 relative_frame = qh->ls_start_schedule_slice /
1140 DWC2_LS_PERIODIC_SLICES_PER_FRAME;
1141 interval = gcd(qh->host_interval, DWC2_LS_SCHEDULE_FRAMES);
1142 }
1143
1144 /* Scheduler messed up if frame is past interval */
1145 WARN_ON(relative_frame >= interval);
Douglas Andersonfb616e32016-01-28 18:20:08 -08001146
1147 /*
1148 * We know interval must divide (HFNUM_MAX_FRNUM + 1) now that we've
1149 * done the gcd(), so it's safe to move to the beginning of the current
1150 * interval like this.
1151 *
1152 * After this we might be before earliest_frame, but don't worry,
1153 * we'll fix it...
1154 */
1155 next_active_frame = (next_active_frame / interval) * interval;
1156
1157 /*
1158 * Actually choose to start at the frame number we've been
1159 * scheduled for.
1160 */
1161 next_active_frame = dwc2_frame_num_inc(next_active_frame,
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001162 relative_frame);
Douglas Andersonfb616e32016-01-28 18:20:08 -08001163
1164 /*
1165 * We actually need 1 frame before since the next_active_frame is
1166 * the frame number we'll be put on the ready list and we won't be on
1167 * the bus until 1 frame later.
1168 */
1169 next_active_frame = dwc2_frame_num_dec(next_active_frame, 1);
1170
1171 /*
1172 * By now we might actually be before the earliest_frame. Let's move
1173 * up intervals until we're not.
1174 */
1175 while (dwc2_frame_num_gt(earliest_frame, next_active_frame))
1176 next_active_frame = dwc2_frame_num_inc(next_active_frame,
1177 interval);
1178
1179exit:
1180 qh->next_active_frame = next_active_frame;
1181 qh->start_active_frame = next_active_frame;
1182
1183 dwc2_sch_vdbg(hsotg, "QH=%p First fn=%04x nxt=%04x\n",
John Youn9da51972017-01-17 20:30:27 -08001184 qh, frame_number, qh->next_active_frame);
Douglas Andersonfb616e32016-01-28 18:20:08 -08001185}
1186
1187/**
Douglas Anderson2d3f1392016-01-28 18:20:06 -08001188 * dwc2_do_reserve() - Make a periodic reservation
1189 *
1190 * Try to allocate space in the periodic schedule. Depending on parameters
1191 * this might use the microframe scheduler or the dumb scheduler.
1192 *
1193 * @hsotg: The HCD state structure for the DWC OTG controller
1194 * @qh: QH for the periodic transfer.
1195 *
1196 * Returns: 0 upon success; error upon failure.
1197 */
1198static int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1199{
1200 int status;
1201
John Younbea8e862016-11-03 17:55:53 -07001202 if (hsotg->params.uframe_sched > 0) {
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001203 status = dwc2_uframe_schedule(hsotg, qh);
Douglas Anderson2d3f1392016-01-28 18:20:06 -08001204 } else {
1205 status = dwc2_periodic_channel_available(hsotg);
1206 if (status) {
1207 dev_info(hsotg->dev,
1208 "%s: No host channel available for periodic transfer\n",
1209 __func__);
1210 return status;
1211 }
1212
1213 status = dwc2_check_periodic_bandwidth(hsotg, qh);
1214 }
1215
1216 if (status) {
1217 dev_dbg(hsotg->dev,
1218 "%s: Insufficient periodic bandwidth for periodic transfer\n",
1219 __func__);
1220 return status;
1221 }
1222
John Younbea8e862016-11-03 17:55:53 -07001223 if (hsotg->params.uframe_sched <= 0)
Douglas Anderson2d3f1392016-01-28 18:20:06 -08001224 /* Reserve periodic channel */
1225 hsotg->periodic_channels++;
1226
1227 /* Update claimed usecs per (micro)frame */
1228 hsotg->periodic_usecs += qh->host_us;
1229
Douglas Andersonfb616e32016-01-28 18:20:08 -08001230 dwc2_pick_first_frame(hsotg, qh);
1231
Douglas Anderson2d3f1392016-01-28 18:20:06 -08001232 return 0;
1233}
1234
1235/**
Douglas Anderson17dd5b62016-01-28 18:19:59 -08001236 * dwc2_do_unreserve() - Actually release the periodic reservation
1237 *
1238 * This function actually releases the periodic bandwidth that was reserved
1239 * by the given qh.
1240 *
1241 * @hsotg: The HCD state structure for the DWC OTG controller
1242 * @qh: QH for the periodic transfer.
1243 */
1244static void dwc2_do_unreserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1245{
1246 assert_spin_locked(&hsotg->lock);
1247
1248 WARN_ON(!qh->unreserve_pending);
1249
1250 /* No more unreserve pending--we're doing it */
1251 qh->unreserve_pending = false;
1252
1253 if (WARN_ON(!list_empty(&qh->qh_list_entry)))
1254 list_del_init(&qh->qh_list_entry);
1255
1256 /* Update claimed usecs per (micro)frame */
Douglas Andersonced9eee2016-01-28 18:20:04 -08001257 hsotg->periodic_usecs -= qh->host_us;
Douglas Anderson17dd5b62016-01-28 18:19:59 -08001258
John Younbea8e862016-11-03 17:55:53 -07001259 if (hsotg->params.uframe_sched > 0) {
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001260 dwc2_uframe_unschedule(hsotg, qh);
Douglas Anderson17dd5b62016-01-28 18:19:59 -08001261 } else {
1262 /* Release periodic channel reservation */
1263 hsotg->periodic_channels--;
1264 }
1265}
1266
1267/**
1268 * dwc2_unreserve_timer_fn() - Timer function to release periodic reservation
1269 *
1270 * According to the kernel doc for usb_submit_urb() (specifically the part about
1271 * "Reserved Bandwidth Transfers"), we need to keep a reservation active as
1272 * long as a device driver keeps submitting. Since we're using HCD_BH to give
1273 * back the URB we need to give the driver a little bit of time before we
1274 * release the reservation. This worker is called after the appropriate
1275 * delay.
1276 *
1277 * @work: Pointer to a qh unreserve_work.
1278 */
1279static void dwc2_unreserve_timer_fn(unsigned long data)
1280{
1281 struct dwc2_qh *qh = (struct dwc2_qh *)data;
1282 struct dwc2_hsotg *hsotg = qh->hsotg;
1283 unsigned long flags;
1284
1285 /*
1286 * Wait for the lock, or for us to be scheduled again. We
1287 * could be scheduled again if:
1288 * - We started executing but didn't get the lock yet.
1289 * - A new reservation came in, but cancel didn't take effect
1290 * because we already started executing.
1291 * - The timer has been kicked again.
1292 * In that case cancel and wait for the next call.
1293 */
1294 while (!spin_trylock_irqsave(&hsotg->lock, flags)) {
1295 if (timer_pending(&qh->unreserve_timer))
1296 return;
1297 }
1298
1299 /*
1300 * Might be no more unreserve pending if:
1301 * - We started executing but didn't get the lock yet.
1302 * - A new reservation came in, but cancel didn't take effect
1303 * because we already started executing.
1304 *
1305 * We can't put this in the loop above because unreserve_pending needs
1306 * to be accessed under lock, so we can only check it once we got the
1307 * lock.
1308 */
1309 if (qh->unreserve_pending)
1310 dwc2_do_unreserve(hsotg, qh);
1311
1312 spin_unlock_irqrestore(&hsotg->lock, flags);
1313}
1314
Paul Zimmerman7359d482013-03-11 17:47:59 -07001315/**
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001316 * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
1317 * host channel is large enough to handle the maximum data transfer in a single
1318 * (micro)frame for a periodic transfer
1319 *
1320 * @hsotg: The HCD state structure for the DWC OTG controller
1321 * @qh: QH for a periodic endpoint
1322 *
1323 * Return: 0 if successful, negative error code otherwise
1324 */
1325static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
1326 struct dwc2_qh *qh)
1327{
1328 u32 max_xfer_size;
1329 u32 max_channel_xfer_size;
1330 int status = 0;
1331
1332 max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
John Younbea8e862016-11-03 17:55:53 -07001333 max_channel_xfer_size = hsotg->params.max_transfer_size;
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001334
1335 if (max_xfer_size > max_channel_xfer_size) {
1336 dev_err(hsotg->dev,
1337 "%s: Periodic xfer length %d > max xfer length for channel %d\n",
1338 __func__, max_xfer_size, max_channel_xfer_size);
1339 status = -ENOSPC;
1340 }
1341
1342 return status;
1343}
1344
1345/**
1346 * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
1347 * the periodic schedule
1348 *
1349 * @hsotg: The HCD state structure for the DWC OTG controller
1350 * @qh: QH for the periodic transfer. The QH should already contain the
1351 * scheduling information.
1352 *
1353 * Return: 0 if successful, negative error code otherwise
1354 */
1355static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1356{
1357 int status;
1358
1359 status = dwc2_check_max_xfer_size(hsotg, qh);
1360 if (status) {
1361 dev_dbg(hsotg->dev,
1362 "%s: Channel max transfer size too small for periodic transfer\n",
1363 __func__);
1364 return status;
1365 }
1366
1367 /* Cancel pending unreserve; if canceled OK, unreserve was pending */
1368 if (del_timer(&qh->unreserve_timer))
1369 WARN_ON(!qh->unreserve_pending);
1370
1371 /*
1372 * Only need to reserve if there's not an unreserve pending, since if an
1373 * unreserve is pending then by definition our old reservation is still
1374 * valid. Unreserve might still be pending even if we didn't cancel if
1375 * dwc2_unreserve_timer_fn() already started. Code in the timer handles
1376 * that case.
1377 */
1378 if (!qh->unreserve_pending) {
Douglas Anderson2d3f1392016-01-28 18:20:06 -08001379 status = dwc2_do_reserve(hsotg, qh);
1380 if (status)
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001381 return status;
Douglas Andersonfb616e32016-01-28 18:20:08 -08001382 } else {
1383 /*
1384 * It might have been a while, so make sure that frame_number
1385 * is still good. Note: we could also try to use the similar
1386 * dwc2_next_periodic_start() but that schedules much more
1387 * tightly and we might need to hurry and queue things up.
1388 */
1389 if (dwc2_frame_num_le(qh->next_active_frame,
1390 hsotg->frame_number))
1391 dwc2_pick_first_frame(hsotg, qh);
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001392 }
1393
1394 qh->unreserve_pending = 0;
1395
John Younbea8e862016-11-03 17:55:53 -07001396 if (hsotg->params.dma_desc_enable > 0)
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001397 /* Don't rely on SOF and start in ready schedule */
1398 list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
1399 else
1400 /* Always start in inactive schedule */
1401 list_add_tail(&qh->qh_list_entry,
1402 &hsotg->periodic_sched_inactive);
1403
Douglas Anderson2d3f1392016-01-28 18:20:06 -08001404 return 0;
Douglas Andersonb951c6c2016-01-28 18:20:05 -08001405}
1406
1407/**
1408 * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
1409 * from the periodic schedule
1410 *
1411 * @hsotg: The HCD state structure for the DWC OTG controller
1412 * @qh: QH for the periodic transfer
1413 */
1414static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
1415 struct dwc2_qh *qh)
1416{
1417 bool did_modify;
1418
1419 assert_spin_locked(&hsotg->lock);
1420
1421 /*
1422 * Schedule the unreserve to happen in a little bit. Cases here:
1423 * - Unreserve worker might be sitting there waiting to grab the lock.
1424 * In this case it will notice it's been schedule again and will
1425 * quit.
1426 * - Unreserve worker might not be scheduled.
1427 *
1428 * We should never already be scheduled since dwc2_schedule_periodic()
1429 * should have canceled the scheduled unreserve timer (hence the
1430 * warning on did_modify).
1431 *
1432 * We add + 1 to the timer to guarantee that at least 1 jiffy has
1433 * passed (otherwise if the jiffy counter might tick right after we
1434 * read it and we'll get no delay).
1435 */
1436 did_modify = mod_timer(&qh->unreserve_timer,
1437 jiffies + DWC2_UNRESERVE_DELAY + 1);
1438 WARN_ON(did_modify);
1439 qh->unreserve_pending = 1;
1440
1441 list_del_init(&qh->qh_list_entry);
1442}
1443
1444/**
Paul Zimmerman7359d482013-03-11 17:47:59 -07001445 * dwc2_qh_init() - Initializes a QH structure
1446 *
1447 * @hsotg: The HCD state structure for the DWC OTG controller
1448 * @qh: The QH to init
1449 * @urb: Holds the information about the device/endpoint needed to initialize
1450 * the QH
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001451 * @mem_flags: Flags for allocating memory.
Paul Zimmerman7359d482013-03-11 17:47:59 -07001452 */
Paul Zimmerman7359d482013-03-11 17:47:59 -07001453static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001454 struct dwc2_hcd_urb *urb, gfp_t mem_flags)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001455{
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001456 int dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1457 u8 ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
1458 bool ep_is_in = !!dwc2_hcd_is_pipe_in(&urb->pipe_info);
1459 bool ep_is_isoc = (ep_type == USB_ENDPOINT_XFER_ISOC);
1460 bool ep_is_int = (ep_type == USB_ENDPOINT_XFER_INT);
1461 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
1462 u32 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1463 bool do_split = (prtspd == HPRT0_SPD_HIGH_SPEED &&
1464 dev_speed != USB_SPEED_HIGH);
1465 int maxp = dwc2_hcd_get_mps(&urb->pipe_info);
1466 int bytecount = dwc2_hb_mult(maxp) * dwc2_max_packet(maxp);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001467 char *speed, *type;
1468
Paul Zimmerman7359d482013-03-11 17:47:59 -07001469 /* Initialize QH */
Douglas Anderson17dd5b62016-01-28 18:19:59 -08001470 qh->hsotg = hsotg;
1471 setup_timer(&qh->unreserve_timer, dwc2_unreserve_timer_fn,
1472 (unsigned long)qh);
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001473 qh->ep_type = ep_type;
1474 qh->ep_is_in = ep_is_in;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001475
1476 qh->data_toggle = DWC2_HC_PID_DATA0;
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001477 qh->maxp = maxp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001478 INIT_LIST_HEAD(&qh->qtd_list);
1479 INIT_LIST_HEAD(&qh->qh_list_entry);
1480
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001481 qh->do_split = do_split;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001482 qh->dev_speed = dev_speed;
1483
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001484 if (ep_is_int || ep_is_isoc) {
1485 /* Compute scheduling parameters once and save them */
1486 int host_speed = do_split ? USB_SPEED_HIGH : dev_speed;
1487 struct dwc2_tt *dwc_tt = dwc2_host_get_tt_info(hsotg, urb->priv,
1488 mem_flags,
1489 &qh->ttport);
1490 int device_ns;
1491
1492 qh->dwc_tt = dwc_tt;
1493
1494 qh->host_us = NS_TO_US(usb_calc_bus_time(host_speed, ep_is_in,
1495 ep_is_isoc, bytecount));
1496 device_ns = usb_calc_bus_time(dev_speed, ep_is_in,
1497 ep_is_isoc, bytecount);
1498
1499 if (do_split && dwc_tt)
1500 device_ns += dwc_tt->usb_tt->think_time;
1501 qh->device_us = NS_TO_US(device_ns);
1502
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001503 qh->device_interval = urb->interval;
1504 qh->host_interval = urb->interval * (do_split ? 8 : 1);
1505
1506 /*
1507 * Schedule low speed if we're running the host in low or
1508 * full speed OR if we've got a "TT" to deal with to access this
1509 * device.
1510 */
1511 qh->schedule_low_speed = prtspd != HPRT0_SPD_HIGH_SPEED ||
1512 dwc_tt;
1513
1514 if (do_split) {
1515 /* We won't know num transfers until we schedule */
1516 qh->num_hs_transfers = -1;
1517 } else if (dev_speed == USB_SPEED_HIGH) {
1518 qh->num_hs_transfers = 1;
1519 } else {
1520 qh->num_hs_transfers = 0;
1521 }
1522
1523 /* We'll schedule later when we have something to do */
1524 }
1525
Paul Zimmerman7359d482013-03-11 17:47:59 -07001526 switch (dev_speed) {
1527 case USB_SPEED_LOW:
1528 speed = "low";
1529 break;
1530 case USB_SPEED_FULL:
1531 speed = "full";
1532 break;
1533 case USB_SPEED_HIGH:
1534 speed = "high";
1535 break;
1536 default:
1537 speed = "?";
1538 break;
1539 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001540
1541 switch (qh->ep_type) {
1542 case USB_ENDPOINT_XFER_ISOC:
1543 type = "isochronous";
1544 break;
1545 case USB_ENDPOINT_XFER_INT:
1546 type = "interrupt";
1547 break;
1548 case USB_ENDPOINT_XFER_CONTROL:
1549 type = "control";
1550 break;
1551 case USB_ENDPOINT_XFER_BULK:
1552 type = "bulk";
1553 break;
1554 default:
1555 type = "?";
1556 break;
1557 }
1558
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001559 dwc2_sch_dbg(hsotg, "QH=%p Init %s, %s speed, %d bytes:\n", qh, type,
1560 speed, bytecount);
1561 dwc2_sch_dbg(hsotg, "QH=%p ...addr=%d, ep=%d, %s\n", qh,
1562 dwc2_hcd_get_dev_addr(&urb->pipe_info),
1563 dwc2_hcd_get_ep_num(&urb->pipe_info),
1564 ep_is_in ? "IN" : "OUT");
1565 if (ep_is_int || ep_is_isoc) {
1566 dwc2_sch_dbg(hsotg,
1567 "QH=%p ...duration: host=%d us, device=%d us\n",
1568 qh, qh->host_us, qh->device_us);
1569 dwc2_sch_dbg(hsotg, "QH=%p ...interval: host=%d, device=%d\n",
1570 qh, qh->host_interval, qh->device_interval);
1571 if (qh->schedule_low_speed)
1572 dwc2_sch_dbg(hsotg, "QH=%p ...low speed schedule=%p\n",
1573 qh, dwc2_get_ls_map(hsotg, qh));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001574 }
1575}
1576
1577/**
1578 * dwc2_hcd_qh_create() - Allocates and initializes a QH
1579 *
1580 * @hsotg: The HCD state structure for the DWC OTG controller
1581 * @urb: Holds the information about the device/endpoint needed
1582 * to initialize the QH
1583 * @atomic_alloc: Flag to do atomic allocation if needed
1584 *
1585 * Return: Pointer to the newly allocated QH, or NULL on error
1586 */
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02001587struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001588 struct dwc2_hcd_urb *urb,
Paul Zimmerman7359d482013-03-11 17:47:59 -07001589 gfp_t mem_flags)
1590{
1591 struct dwc2_qh *qh;
1592
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -07001593 if (!urb->priv)
1594 return NULL;
1595
Paul Zimmerman7359d482013-03-11 17:47:59 -07001596 /* Allocate memory */
1597 qh = kzalloc(sizeof(*qh), mem_flags);
1598 if (!qh)
1599 return NULL;
1600
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001601 dwc2_qh_init(hsotg, qh, urb, mem_flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001602
John Younbea8e862016-11-03 17:55:53 -07001603 if (hsotg->params.dma_desc_enable > 0 &&
Paul Zimmerman7359d482013-03-11 17:47:59 -07001604 dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
1605 dwc2_hcd_qh_free(hsotg, qh);
1606 return NULL;
1607 }
1608
1609 return qh;
1610}
1611
1612/**
1613 * dwc2_hcd_qh_free() - Frees the QH
1614 *
1615 * @hsotg: HCD instance
1616 * @qh: The QH to free
1617 *
1618 * QH should already be removed from the list. QTD list should already be empty
1619 * if called from URB Dequeue.
1620 *
1621 * Must NOT be called with interrupt disabled or spinlock held
1622 */
1623void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1624{
Douglas Anderson17dd5b62016-01-28 18:19:59 -08001625 /* Make sure any unreserve work is finished. */
1626 if (del_timer_sync(&qh->unreserve_timer)) {
1627 unsigned long flags;
1628
1629 spin_lock_irqsave(&hsotg->lock, flags);
1630 dwc2_do_unreserve(hsotg, qh);
1631 spin_unlock_irqrestore(&hsotg->lock, flags);
1632 }
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001633 dwc2_host_put_tt_info(hsotg, qh->dwc_tt);
Douglas Anderson17dd5b62016-01-28 18:19:59 -08001634
Douglas Anderson3bc04e22016-01-28 18:19:53 -08001635 if (qh->desc_list)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001636 dwc2_hcd_qh_free_ddma(hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001637 kfree(qh);
1638}
1639
1640/**
Paul Zimmerman7359d482013-03-11 17:47:59 -07001641 * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
1642 * schedule if it is not already in the schedule. If the QH is already in
1643 * the schedule, no action is taken.
1644 *
1645 * @hsotg: The HCD state structure for the DWC OTG controller
1646 * @qh: The QH to add
1647 *
1648 * Return: 0 if successful, negative error code otherwise
1649 */
1650int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1651{
Dan Carpenterd31e6ca2013-11-25 17:11:29 +03001652 int status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001653 u32 intr_mask;
1654
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001655 if (dbg_qh(qh))
1656 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001657
1658 if (!list_empty(&qh->qh_list_entry))
1659 /* QH already in a schedule */
Dan Carpenterd31e6ca2013-11-25 17:11:29 +03001660 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001661
1662 /* Add the new QH to the appropriate schedule */
1663 if (dwc2_qh_is_non_per(qh)) {
Douglas Andersonfb616e32016-01-28 18:20:08 -08001664 /* Schedule right away */
1665 qh->start_active_frame = hsotg->frame_number;
1666 qh->next_active_frame = qh->start_active_frame;
1667
Paul Zimmerman7359d482013-03-11 17:47:59 -07001668 /* Always start in inactive schedule */
1669 list_add_tail(&qh->qh_list_entry,
1670 &hsotg->non_periodic_sched_inactive);
Dan Carpenter5e128472013-11-25 17:14:14 +03001671 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001672 }
1673
Dan Carpenter5e128472013-11-25 17:14:14 +03001674 status = dwc2_schedule_periodic(hsotg, qh);
1675 if (status)
1676 return status;
1677 if (!hsotg->periodic_qh_count) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001678 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Dan Carpenter5e128472013-11-25 17:14:14 +03001679 intr_mask |= GINTSTS_SOF;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001680 dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
Dan Carpenter5e128472013-11-25 17:14:14 +03001681 }
1682 hsotg->periodic_qh_count++;
1683
Dan Carpenterd31e6ca2013-11-25 17:11:29 +03001684 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001685}
1686
1687/**
1688 * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
1689 * schedule. Memory is not freed.
1690 *
1691 * @hsotg: The HCD state structure
1692 * @qh: QH to remove from schedule
1693 */
1694void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1695{
1696 u32 intr_mask;
1697
1698 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1699
1700 if (list_empty(&qh->qh_list_entry))
1701 /* QH is not in a schedule */
1702 return;
1703
1704 if (dwc2_qh_is_non_per(qh)) {
1705 if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
1706 hsotg->non_periodic_qh_ptr =
1707 hsotg->non_periodic_qh_ptr->next;
1708 list_del_init(&qh->qh_list_entry);
Dan Carpenter5e128472013-11-25 17:14:14 +03001709 return;
1710 }
1711
1712 dwc2_deschedule_periodic(hsotg, qh);
1713 hsotg->periodic_qh_count--;
Sevak Arakelyan907a4442016-04-27 20:20:53 -07001714 if (!hsotg->periodic_qh_count &&
John Younbea8e862016-11-03 17:55:53 -07001715 hsotg->params.dma_desc_enable <= 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001716 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Dan Carpenter5e128472013-11-25 17:14:14 +03001717 intr_mask &= ~GINTSTS_SOF;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001718 dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001719 }
1720}
1721
Douglas Andersonfb616e32016-01-28 18:20:08 -08001722/**
1723 * dwc2_next_for_periodic_split() - Set next_active_frame midway thru a split.
1724 *
1725 * This is called for setting next_active_frame for periodic splits for all but
1726 * the first packet of the split. Confusing? I thought so...
1727 *
1728 * Periodic splits are single low/full speed transfers that we end up splitting
1729 * up into several high speed transfers. They always fit into one full (1 ms)
1730 * frame but might be split over several microframes (125 us each). We to put
1731 * each of the parts on a very specific high speed frame.
1732 *
1733 * This function figures out where the next active uFrame needs to be.
1734 *
1735 * @hsotg: The HCD state structure
1736 * @qh: QH for the periodic transfer.
1737 * @frame_number: The current frame number.
1738 *
1739 * Return: number missed by (or 0 if we didn't miss).
Paul Zimmerman7359d482013-03-11 17:47:59 -07001740 */
Douglas Andersonfb616e32016-01-28 18:20:08 -08001741static int dwc2_next_for_periodic_split(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001742 struct dwc2_qh *qh, u16 frame_number)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001743{
Douglas Andersonced9eee2016-01-28 18:20:04 -08001744 u16 old_frame = qh->next_active_frame;
Douglas Andersonfb616e32016-01-28 18:20:08 -08001745 u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1746 int missed = 0;
1747 u16 incr;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001748
Douglas Andersonfb616e32016-01-28 18:20:08 -08001749 /*
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001750 * See dwc2_uframe_schedule_split() for split scheduling.
1751 *
Douglas Andersonfb616e32016-01-28 18:20:08 -08001752 * Basically: increment 1 normally, but 2 right after the start split
1753 * (except for ISOC out).
1754 */
1755 if (old_frame == qh->start_active_frame &&
1756 !(qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in))
1757 incr = 2;
1758 else
1759 incr = 1;
1760
1761 qh->next_active_frame = dwc2_frame_num_inc(old_frame, incr);
1762
1763 /*
1764 * Note that it's OK for frame_number to be 1 frame past
1765 * next_active_frame. Remember that next_active_frame is supposed to
1766 * be 1 frame _before_ when we want to be scheduled. If we're 1 frame
1767 * past it just means schedule ASAP.
1768 *
1769 * It's _not_ OK, however, if we're more than one frame past.
1770 */
1771 if (dwc2_frame_num_gt(prev_frame_number, qh->next_active_frame)) {
1772 /*
1773 * OOPS, we missed. That's actually pretty bad since
1774 * the hub will be unhappy; try ASAP I guess.
1775 */
1776 missed = dwc2_frame_num_dec(prev_frame_number,
1777 qh->next_active_frame);
Douglas Andersonced9eee2016-01-28 18:20:04 -08001778 qh->next_active_frame = frame_number;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001779 }
Douglas Anderson74fc4a72016-01-28 18:19:58 -08001780
Douglas Andersonfb616e32016-01-28 18:20:08 -08001781 return missed;
1782}
1783
1784/**
1785 * dwc2_next_periodic_start() - Set next_active_frame for next transfer start
1786 *
1787 * This is called for setting next_active_frame for a periodic transfer for
1788 * all cases other than midway through a periodic split. This will also update
1789 * start_active_frame.
1790 *
1791 * Since we _always_ keep start_active_frame as the start of the previous
1792 * transfer this is normally pretty easy: we just add our interval to
1793 * start_active_frame and we've got our answer.
1794 *
1795 * The tricks come into play if we miss. In that case we'll look for the next
1796 * slot we can fit into.
1797 *
1798 * @hsotg: The HCD state structure
1799 * @qh: QH for the periodic transfer.
1800 * @frame_number: The current frame number.
1801 *
1802 * Return: number missed by (or 0 if we didn't miss).
1803 */
1804static int dwc2_next_periodic_start(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001805 struct dwc2_qh *qh, u16 frame_number)
Douglas Andersonfb616e32016-01-28 18:20:08 -08001806{
1807 int missed = 0;
1808 u16 interval = qh->host_interval;
1809 u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1810
1811 qh->start_active_frame = dwc2_frame_num_inc(qh->start_active_frame,
1812 interval);
1813
1814 /*
1815 * The dwc2_frame_num_gt() function used below won't work terribly well
1816 * with if we just incremented by a really large intervals since the
1817 * frame counter only goes to 0x3fff. It's terribly unlikely that we
1818 * will have missed in this case anyway. Just go to exit. If we want
1819 * to try to do better we'll need to keep track of a bigger counter
1820 * somewhere in the driver and handle overflows.
1821 */
1822 if (interval >= 0x1000)
1823 goto exit;
1824
1825 /*
1826 * Test for misses, which is when it's too late to schedule.
1827 *
1828 * A few things to note:
1829 * - We compare against prev_frame_number since start_active_frame
1830 * and next_active_frame are always 1 frame before we want things
1831 * to be active and we assume we can still get scheduled in the
1832 * current frame number.
Douglas Anderson9cf1a602016-01-28 18:20:11 -08001833 * - It's possible for start_active_frame (now incremented) to be
1834 * next_active_frame if we got an EO MISS (even_odd miss) which
1835 * basically means that we detected there wasn't enough time for
1836 * the last packet and dwc2_hc_set_even_odd_frame() rescheduled us
1837 * at the last second. We want to make sure we don't schedule
1838 * another transfer for the same frame. My test webcam doesn't seem
1839 * terribly upset by missing a transfer but really doesn't like when
1840 * we do two transfers in the same frame.
Douglas Andersonfb616e32016-01-28 18:20:08 -08001841 * - Some misses are expected. Specifically, in order to work
1842 * perfectly dwc2 really needs quite spectacular interrupt latency
1843 * requirements. It needs to be able to handle its interrupts
1844 * completely within 125 us of them being asserted. That not only
1845 * means that the dwc2 interrupt handler needs to be fast but it
1846 * means that nothing else in the system has to block dwc2 for a long
1847 * time. We can help with the dwc2 parts of this, but it's hard to
1848 * guarantee that a system will have interrupt latency < 125 us, so
1849 * we have to be robust to some misses.
1850 */
Douglas Anderson9cf1a602016-01-28 18:20:11 -08001851 if (qh->start_active_frame == qh->next_active_frame ||
1852 dwc2_frame_num_gt(prev_frame_number, qh->start_active_frame)) {
Douglas Andersonfb616e32016-01-28 18:20:08 -08001853 u16 ideal_start = qh->start_active_frame;
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001854 int periods_in_map;
Douglas Andersonfb616e32016-01-28 18:20:08 -08001855
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08001856 /*
1857 * Adjust interval as per gcd with map size.
1858 * See pmap_schedule() for more details here.
1859 */
1860 if (qh->do_split || qh->dev_speed == USB_SPEED_HIGH)
1861 periods_in_map = DWC2_HS_SCHEDULE_UFRAMES;
1862 else
1863 periods_in_map = DWC2_LS_SCHEDULE_FRAMES;
1864 interval = gcd(interval, periods_in_map);
Douglas Andersonfb616e32016-01-28 18:20:08 -08001865
1866 do {
1867 qh->start_active_frame = dwc2_frame_num_inc(
1868 qh->start_active_frame, interval);
1869 } while (dwc2_frame_num_gt(prev_frame_number,
1870 qh->start_active_frame));
1871
1872 missed = dwc2_frame_num_dec(qh->start_active_frame,
1873 ideal_start);
1874 }
1875
1876exit:
1877 qh->next_active_frame = qh->start_active_frame;
1878
1879 return missed;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001880}
1881
1882/*
1883 * Deactivates a QH. For non-periodic QHs, removes the QH from the active
1884 * non-periodic schedule. The QH is added to the inactive non-periodic
1885 * schedule if any QTDs are still attached to the QH.
1886 *
1887 * For periodic QHs, the QH is removed from the periodic queued schedule. If
1888 * there are any QTDs still attached to the QH, the QH is added to either the
1889 * periodic inactive schedule or the periodic ready schedule and its next
1890 * scheduled frame is calculated. The QH is placed in the ready schedule if
1891 * the scheduled frame has been reached already. Otherwise it's placed in the
1892 * inactive schedule. If there are no QTDs attached to the QH, the QH is
1893 * completely removed from the periodic schedule.
1894 */
1895void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
1896 int sched_next_periodic_split)
1897{
Douglas Andersonfb616e32016-01-28 18:20:08 -08001898 u16 old_frame = qh->next_active_frame;
Dan Carpenter5e128472013-11-25 17:14:14 +03001899 u16 frame_number;
Douglas Andersonfb616e32016-01-28 18:20:08 -08001900 int missed;
Dan Carpenter5e128472013-11-25 17:14:14 +03001901
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001902 if (dbg_qh(qh))
1903 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001904
1905 if (dwc2_qh_is_non_per(qh)) {
1906 dwc2_hcd_qh_unlink(hsotg, qh);
1907 if (!list_empty(&qh->qtd_list))
1908 /* Add back to inactive non-periodic schedule */
1909 dwc2_hcd_qh_add(hsotg, qh);
Dan Carpenter5e128472013-11-25 17:14:14 +03001910 return;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001911 }
Dan Carpenter5e128472013-11-25 17:14:14 +03001912
Douglas Andersonfb616e32016-01-28 18:20:08 -08001913 /*
1914 * Use the real frame number rather than the cached value as of the
1915 * last SOF just to get us a little closer to reality. Note that
1916 * means we don't actually know if we've already handled the SOF
1917 * interrupt for this frame.
1918 */
Dan Carpenter5e128472013-11-25 17:14:14 +03001919 frame_number = dwc2_hcd_get_frame_number(hsotg);
1920
Douglas Andersonfb616e32016-01-28 18:20:08 -08001921 if (sched_next_periodic_split)
1922 missed = dwc2_next_for_periodic_split(hsotg, qh, frame_number);
1923 else
1924 missed = dwc2_next_periodic_start(hsotg, qh, frame_number);
1925
1926 dwc2_sch_vdbg(hsotg,
John Youn9da51972017-01-17 20:30:27 -08001927 "QH=%p next(%d) fn=%04x, sch=%04x=>%04x (%+d) miss=%d %s\n",
Douglas Andersonfb616e32016-01-28 18:20:08 -08001928 qh, sched_next_periodic_split, frame_number, old_frame,
1929 qh->next_active_frame,
1930 dwc2_frame_num_dec(qh->next_active_frame, old_frame),
1931 missed, missed ? "MISS" : "");
Dan Carpenter5e128472013-11-25 17:14:14 +03001932
1933 if (list_empty(&qh->qtd_list)) {
1934 dwc2_hcd_qh_unlink(hsotg, qh);
1935 return;
1936 }
Douglas Andersonfb616e32016-01-28 18:20:08 -08001937
Dan Carpenter5e128472013-11-25 17:14:14 +03001938 /*
1939 * Remove from periodic_sched_queued and move to
1940 * appropriate queue
Douglas Andersonfb616e32016-01-28 18:20:08 -08001941 *
1942 * Note: we purposely use the frame_number from the "hsotg" structure
1943 * since we know SOF interrupt will handle future frames.
Dan Carpenter5e128472013-11-25 17:14:14 +03001944 */
Douglas Andersonfb616e32016-01-28 18:20:08 -08001945 if (dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number))
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08001946 list_move_tail(&qh->qh_list_entry,
1947 &hsotg->periodic_sched_ready);
Dan Carpenter5e128472013-11-25 17:14:14 +03001948 else
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08001949 list_move_tail(&qh->qh_list_entry,
1950 &hsotg->periodic_sched_inactive);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001951}
1952
1953/**
1954 * dwc2_hcd_qtd_init() - Initializes a QTD structure
1955 *
1956 * @qtd: The QTD to initialize
1957 * @urb: The associated URB
1958 */
1959void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
1960{
1961 qtd->urb = urb;
1962 if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
1963 USB_ENDPOINT_XFER_CONTROL) {
1964 /*
1965 * The only time the QTD data toggle is used is on the data
1966 * phase of control transfers. This phase always starts with
1967 * DATA1.
1968 */
1969 qtd->data_toggle = DWC2_HC_PID_DATA1;
1970 qtd->control_phase = DWC2_CONTROL_SETUP;
1971 }
1972
1973 /* Start split */
1974 qtd->complete_split = 0;
1975 qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
1976 qtd->isoc_split_offset = 0;
1977 qtd->in_process = 0;
1978
1979 /* Store the qtd ptr in the urb to reference the QTD */
1980 urb->qtd = qtd;
1981}
1982
1983/**
1984 * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
Gregory Herrero33ad2612015-04-29 22:09:15 +02001985 * Caller must hold driver lock.
Paul Zimmerman7359d482013-03-11 17:47:59 -07001986 *
1987 * @hsotg: The DWC HCD structure
1988 * @qtd: The QTD to add
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02001989 * @qh: Queue head to add qtd to
Paul Zimmerman7359d482013-03-11 17:47:59 -07001990 *
1991 * Return: 0 if successful, negative error code otherwise
1992 *
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02001993 * If the QH to which the QTD is added is not currently scheduled, it is placed
1994 * into the proper schedule based on its EP type.
Paul Zimmerman7359d482013-03-11 17:47:59 -07001995 */
1996int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02001997 struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001998{
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -07001999 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002000
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002001 if (unlikely(!qh)) {
2002 dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
2003 retval = -EINVAL;
2004 goto fail;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002005 }
2006
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002007 retval = dwc2_hcd_qh_add(hsotg, qh);
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -07002008 if (retval)
2009 goto fail;
2010
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002011 qtd->qh = qh;
2012 list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -07002013
2014 return 0;
Paul Zimmermanb2d6cb52013-07-13 14:53:51 -07002015fail:
Paul Zimmerman7359d482013-03-11 17:47:59 -07002016 return retval;
2017}