hcd_queue.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  1. /*
  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. * This file contains the functions to manage Queue Heads and Queue
  38. * Transfer Descriptors for Host mode
  39. */
  40. #include <linux/gcd.h>
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/io.h>
  47. #include <linux/slab.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/hcd.h>
  50. #include <linux/usb/ch11.h>
  51. #include "core.h"
  52. #include "hcd.h"
  53. /* Wait this long before releasing periodic reservation */
  54. #define DWC2_UNRESERVE_DELAY (msecs_to_jiffies(5))
  55. /**
  56. * dwc2_periodic_channel_available() - Checks that a channel is available for a
  57. * periodic transfer
  58. *
  59. * @hsotg: The HCD state structure for the DWC OTG controller
  60. *
  61. * Return: 0 if successful, negative error code otherwise
  62. */
  63. static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
  64. {
  65. /*
  66. * Currently assuming that there is a dedicated host channel for
  67. * each periodic transaction plus at least one host channel for
  68. * non-periodic transactions
  69. */
  70. int status;
  71. int num_channels;
  72. num_channels = hsotg->params.host_channels;
  73. if ((hsotg->periodic_channels + hsotg->non_periodic_channels <
  74. num_channels) && (hsotg->periodic_channels < num_channels - 1)) {
  75. status = 0;
  76. } else {
  77. dev_dbg(hsotg->dev,
  78. "%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  79. __func__, num_channels,
  80. hsotg->periodic_channels, hsotg->non_periodic_channels);
  81. status = -ENOSPC;
  82. }
  83. return status;
  84. }
  85. /**
  86. * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth
  87. * for the specified QH in the periodic schedule
  88. *
  89. * @hsotg: The HCD state structure for the DWC OTG controller
  90. * @qh: QH containing periodic bandwidth required
  91. *
  92. * Return: 0 if successful, negative error code otherwise
  93. *
  94. * For simplicity, this calculation assumes that all the transfers in the
  95. * periodic schedule may occur in the same (micro)frame
  96. */
  97. static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
  98. struct dwc2_qh *qh)
  99. {
  100. int status;
  101. s16 max_claimed_usecs;
  102. status = 0;
  103. if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
  104. /*
  105. * High speed mode
  106. * Max periodic usecs is 80% x 125 usec = 100 usec
  107. */
  108. max_claimed_usecs = 100 - qh->host_us;
  109. } else {
  110. /*
  111. * Full speed mode
  112. * Max periodic usecs is 90% x 1000 usec = 900 usec
  113. */
  114. max_claimed_usecs = 900 - qh->host_us;
  115. }
  116. if (hsotg->periodic_usecs > max_claimed_usecs) {
  117. dev_err(hsotg->dev,
  118. "%s: already claimed usecs %d, required usecs %d\n",
  119. __func__, hsotg->periodic_usecs, qh->host_us);
  120. status = -ENOSPC;
  121. }
  122. return status;
  123. }
  124. /**
  125. * pmap_schedule() - Schedule time in a periodic bitmap (pmap).
  126. *
  127. * @map: The bitmap representing the schedule; will be updated
  128. * upon success.
  129. * @bits_per_period: The schedule represents several periods. This is how many
  130. * bits are in each period. It's assumed that the beginning
  131. * of the schedule will repeat after its end.
  132. * @periods_in_map: The number of periods in the schedule.
  133. * @num_bits: The number of bits we need per period we want to reserve
  134. * in this function call.
  135. * @interval: How often we need to be scheduled for the reservation this
  136. * time. 1 means every period. 2 means every other period.
  137. * ...you get the picture?
  138. * @start: The bit number to start at. Normally 0. Must be within
  139. * the interval or we return failure right away.
  140. * @only_one_period: Normally we'll allow picking a start anywhere within the
  141. * first interval, since we can still make all repetition
  142. * requirements by doing that. However, if you pass true
  143. * here then we'll return failure if we can't fit within
  144. * the period that "start" is in.
  145. *
  146. * The idea here is that we want to schedule time for repeating events that all
  147. * want the same resource. The resource is divided into fixed-sized periods
  148. * and the events want to repeat every "interval" periods. The schedule
  149. * granularity is one bit.
  150. *
  151. * To keep things "simple", we'll represent our schedule with a bitmap that
  152. * contains a fixed number of periods. This gets rid of a lot of complexity
  153. * but does mean that we need to handle things specially (and non-ideally) if
  154. * the number of the periods in the schedule doesn't match well with the
  155. * intervals that we're trying to schedule.
  156. *
  157. * Here's an explanation of the scheme we'll implement, assuming 8 periods.
  158. * - If interval is 1, we need to take up space in each of the 8
  159. * periods we're scheduling. Easy.
  160. * - If interval is 2, we need to take up space in half of the
  161. * periods. Again, easy.
  162. * - If interval is 3, we actually need to fall back to interval 1.
  163. * Why? Because we might need time in any period. AKA for the
  164. * first 8 periods, we'll be in slot 0, 3, 6. Then we'll be
  165. * in slot 1, 4, 7. Then we'll be in 2, 5. Then we'll be back to
  166. * 0, 3, and 6. Since we could be in any frame we need to reserve
  167. * for all of them. Sucks, but that's what you gotta do. Note that
  168. * if we were instead scheduling 8 * 3 = 24 we'd do much better, but
  169. * then we need more memory and time to do scheduling.
  170. * - If interval is 4, easy.
  171. * - If interval is 5, we again need interval 1. The schedule will be
  172. * 0, 5, 2, 7, 4, 1, 6, 3, 0
  173. * - If interval is 6, we need interval 2. 0, 6, 4, 2.
  174. * - If interval is 7, we need interval 1.
  175. * - If interval is 8, we need interval 8.
  176. *
  177. * If you do the math, you'll see that we need to pretend that interval is
  178. * equal to the greatest_common_divisor(interval, periods_in_map).
  179. *
  180. * Note that at the moment this function tends to front-pack the schedule.
  181. * In some cases that's really non-ideal (it's hard to schedule things that
  182. * need to repeat every period). In other cases it's perfect (you can easily
  183. * schedule bigger, less often repeating things).
  184. *
  185. * Here's the algorithm in action (8 periods, 5 bits per period):
  186. * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0
  187. * |*****| ***|*****| ***|*****| ***|*****| ***| OK 3 bits, intv 3 at 2
  188. * |*****|* ***|*****| ***|*****|* ***|*****| ***| OK 1 bits, intv 4 at 5
  189. * |** |* |** | |** |* |** | | Remv 3 bits, intv 3 at 2
  190. * |*** |* |*** | |*** |* |*** | | OK 1 bits, intv 6 at 2
  191. * |**** |* * |**** | * |**** |* * |**** | * | OK 1 bits, intv 1 at 3
  192. * |**** |**** |**** | *** |**** |**** |**** | *** | OK 2 bits, intv 2 at 6
  193. * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 1 at 4
  194. * |*****|*****|*****| ****|*****|*****|*****| ****| FAIL 1 bits, intv 1
  195. * | ***|*****| ***| ****| ***|*****| ***| ****| Remv 2 bits, intv 2 at 0
  196. * | ***| ****| ***| ****| ***| ****| ***| ****| Remv 1 bits, intv 4 at 5
  197. * | **| ****| **| ****| **| ****| **| ****| Remv 1 bits, intv 6 at 2
  198. * | *| ** *| *| ** *| *| ** *| *| ** *| Remv 1 bits, intv 1 at 3
  199. * | *| *| *| *| *| *| *| *| Remv 2 bits, intv 2 at 6
  200. * | | | | | | | | | Remv 1 bits, intv 1 at 4
  201. * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0
  202. * |*** | |** | |*** | |** | | OK 1 bits, intv 4 at 2
  203. * |*****| |** **| |*****| |** **| | OK 2 bits, intv 2 at 3
  204. * |*****|* |** **| |*****|* |** **| | OK 1 bits, intv 4 at 5
  205. * |*****|*** |** **| ** |*****|*** |** **| ** | OK 2 bits, intv 2 at 6
  206. * |*****|*****|** **| ****|*****|*****|** **| ****| OK 2 bits, intv 2 at 8
  207. * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 4 at 12
  208. *
  209. * This function is pretty generic and could be easily abstracted if anything
  210. * needed similar scheduling.
  211. *
  212. * Returns either -ENOSPC or a >= 0 start bit which should be passed to the
  213. * unschedule routine. The map bitmap will be updated on a non-error result.
  214. */
  215. static int pmap_schedule(unsigned long *map, int bits_per_period,
  216. int periods_in_map, int num_bits,
  217. int interval, int start, bool only_one_period)
  218. {
  219. int interval_bits;
  220. int to_reserve;
  221. int first_end;
  222. int i;
  223. if (num_bits > bits_per_period)
  224. return -ENOSPC;
  225. /* Adjust interval as per description */
  226. interval = gcd(interval, periods_in_map);
  227. interval_bits = bits_per_period * interval;
  228. to_reserve = periods_in_map / interval;
  229. /* If start has gotten us past interval then we can't schedule */
  230. if (start >= interval_bits)
  231. return -ENOSPC;
  232. if (only_one_period)
  233. /* Must fit within same period as start; end at begin of next */
  234. first_end = (start / bits_per_period + 1) * bits_per_period;
  235. else
  236. /* Can fit anywhere in the first interval */
  237. first_end = interval_bits;
  238. /*
  239. * We'll try to pick the first repetition, then see if that time
  240. * is free for each of the subsequent repetitions. If it's not
  241. * we'll adjust the start time for the next search of the first
  242. * repetition.
  243. */
  244. while (start + num_bits <= first_end) {
  245. int end;
  246. /* Need to stay within this period */
  247. end = (start / bits_per_period + 1) * bits_per_period;
  248. /* Look for num_bits us in this microframe starting at start */
  249. start = bitmap_find_next_zero_area(map, end, start, num_bits,
  250. 0);
  251. /*
  252. * We should get start >= end if we fail. We might be
  253. * able to check the next microframe depending on the
  254. * interval, so continue on (start already updated).
  255. */
  256. if (start >= end) {
  257. start = end;
  258. continue;
  259. }
  260. /* At this point we have a valid point for first one */
  261. for (i = 1; i < to_reserve; i++) {
  262. int ith_start = start + interval_bits * i;
  263. int ith_end = end + interval_bits * i;
  264. int ret;
  265. /* Use this as a dumb "check if bits are 0" */
  266. ret = bitmap_find_next_zero_area(
  267. map, ith_start + num_bits, ith_start, num_bits,
  268. 0);
  269. /* We got the right place, continue checking */
  270. if (ret == ith_start)
  271. continue;
  272. /* Move start up for next time and exit for loop */
  273. ith_start = bitmap_find_next_zero_area(
  274. map, ith_end, ith_start, num_bits, 0);
  275. if (ith_start >= ith_end)
  276. /* Need a while new period next time */
  277. start = end;
  278. else
  279. start = ith_start - interval_bits * i;
  280. break;
  281. }
  282. /* If didn't exit the for loop with a break, we have success */
  283. if (i == to_reserve)
  284. break;
  285. }
  286. if (start + num_bits > first_end)
  287. return -ENOSPC;
  288. for (i = 0; i < to_reserve; i++) {
  289. int ith_start = start + interval_bits * i;
  290. bitmap_set(map, ith_start, num_bits);
  291. }
  292. return start;
  293. }
  294. /**
  295. * pmap_unschedule() - Undo work done by pmap_schedule()
  296. *
  297. * @map: See pmap_schedule().
  298. * @bits_per_period: See pmap_schedule().
  299. * @periods_in_map: See pmap_schedule().
  300. * @num_bits: The number of bits that was passed to schedule.
  301. * @interval: The interval that was passed to schedule.
  302. * @start: The return value from pmap_schedule().
  303. */
  304. static void pmap_unschedule(unsigned long *map, int bits_per_period,
  305. int periods_in_map, int num_bits,
  306. int interval, int start)
  307. {
  308. int interval_bits;
  309. int to_release;
  310. int i;
  311. /* Adjust interval as per description in pmap_schedule() */
  312. interval = gcd(interval, periods_in_map);
  313. interval_bits = bits_per_period * interval;
  314. to_release = periods_in_map / interval;
  315. for (i = 0; i < to_release; i++) {
  316. int ith_start = start + interval_bits * i;
  317. bitmap_clear(map, ith_start, num_bits);
  318. }
  319. }
  320. /**
  321. * dwc2_get_ls_map() - Get the map used for the given qh
  322. *
  323. * @hsotg: The HCD state structure for the DWC OTG controller.
  324. * @qh: QH for the periodic transfer.
  325. *
  326. * We'll always get the periodic map out of our TT. Note that even if we're
  327. * running the host straight in low speed / full speed mode it appears as if
  328. * a TT is allocated for us, so we'll use it. If that ever changes we can
  329. * add logic here to get a map out of "hsotg" if !qh->do_split.
  330. *
  331. * Returns: the map or NULL if a map couldn't be found.
  332. */
  333. static unsigned long *dwc2_get_ls_map(struct dwc2_hsotg *hsotg,
  334. struct dwc2_qh *qh)
  335. {
  336. unsigned long *map;
  337. /* Don't expect to be missing a TT and be doing low speed scheduling */
  338. if (WARN_ON(!qh->dwc_tt))
  339. return NULL;
  340. /* Get the map and adjust if this is a multi_tt hub */
  341. map = qh->dwc_tt->periodic_bitmaps;
  342. if (qh->dwc_tt->usb_tt->multi)
  343. map += DWC2_ELEMENTS_PER_LS_BITMAP * qh->ttport;
  344. return map;
  345. }
  346. #ifdef DWC2_PRINT_SCHEDULE
  347. /*
  348. * cat_printf() - A printf() + strcat() helper
  349. *
  350. * This is useful for concatenating a bunch of strings where each string is
  351. * constructed using printf.
  352. *
  353. * @buf: The destination buffer; will be updated to point after the printed
  354. * data.
  355. * @size: The number of bytes in the buffer (includes space for '\0').
  356. * @fmt: The format for printf.
  357. * @...: The args for printf.
  358. */
  359. static __printf(3, 4)
  360. void cat_printf(char **buf, size_t *size, const char *fmt, ...)
  361. {
  362. va_list args;
  363. int i;
  364. if (*size == 0)
  365. return;
  366. va_start(args, fmt);
  367. i = vsnprintf(*buf, *size, fmt, args);
  368. va_end(args);
  369. if (i >= *size) {
  370. (*buf)[*size - 1] = '\0';
  371. *buf += *size;
  372. *size = 0;
  373. } else {
  374. *buf += i;
  375. *size -= i;
  376. }
  377. }
  378. /*
  379. * pmap_print() - Print the given periodic map
  380. *
  381. * Will attempt to print out the periodic schedule.
  382. *
  383. * @map: See pmap_schedule().
  384. * @bits_per_period: See pmap_schedule().
  385. * @periods_in_map: See pmap_schedule().
  386. * @period_name: The name of 1 period, like "uFrame"
  387. * @units: The name of the units, like "us".
  388. * @print_fn: The function to call for printing.
  389. * @print_data: Opaque data to pass to the print function.
  390. */
  391. static void pmap_print(unsigned long *map, int bits_per_period,
  392. int periods_in_map, const char *period_name,
  393. const char *units,
  394. void (*print_fn)(const char *str, void *data),
  395. void *print_data)
  396. {
  397. int period;
  398. for (period = 0; period < periods_in_map; period++) {
  399. char tmp[64];
  400. char *buf = tmp;
  401. size_t buf_size = sizeof(tmp);
  402. int period_start = period * bits_per_period;
  403. int period_end = period_start + bits_per_period;
  404. int start = 0;
  405. int count = 0;
  406. bool printed = false;
  407. int i;
  408. for (i = period_start; i < period_end + 1; i++) {
  409. /* Handle case when ith bit is set */
  410. if (i < period_end &&
  411. bitmap_find_next_zero_area(map, i + 1,
  412. i, 1, 0) != i) {
  413. if (count == 0)
  414. start = i - period_start;
  415. count++;
  416. continue;
  417. }
  418. /* ith bit isn't set; don't care if count == 0 */
  419. if (count == 0)
  420. continue;
  421. if (!printed)
  422. cat_printf(&buf, &buf_size, "%s %d: ",
  423. period_name, period);
  424. else
  425. cat_printf(&buf, &buf_size, ", ");
  426. printed = true;
  427. cat_printf(&buf, &buf_size, "%d %s -%3d %s", start,
  428. units, start + count - 1, units);
  429. count = 0;
  430. }
  431. if (printed)
  432. print_fn(tmp, print_data);
  433. }
  434. }
  435. struct dwc2_qh_print_data {
  436. struct dwc2_hsotg *hsotg;
  437. struct dwc2_qh *qh;
  438. };
  439. /**
  440. * dwc2_qh_print() - Helper function for dwc2_qh_schedule_print()
  441. *
  442. * @str: The string to print
  443. * @data: A pointer to a struct dwc2_qh_print_data
  444. */
  445. static void dwc2_qh_print(const char *str, void *data)
  446. {
  447. struct dwc2_qh_print_data *print_data = data;
  448. dwc2_sch_dbg(print_data->hsotg, "QH=%p ...%s\n", print_data->qh, str);
  449. }
  450. /**
  451. * dwc2_qh_schedule_print() - Print the periodic schedule
  452. *
  453. * @hsotg: The HCD state structure for the DWC OTG controller.
  454. * @qh: QH to print.
  455. */
  456. static void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
  457. struct dwc2_qh *qh)
  458. {
  459. struct dwc2_qh_print_data print_data = { hsotg, qh };
  460. int i;
  461. /*
  462. * The printing functions are quite slow and inefficient.
  463. * If we don't have tracing turned on, don't run unless the special
  464. * define is turned on.
  465. */
  466. if (qh->schedule_low_speed) {
  467. unsigned long *map = dwc2_get_ls_map(hsotg, qh);
  468. dwc2_sch_dbg(hsotg, "QH=%p LS/FS trans: %d=>%d us @ %d us",
  469. qh, qh->device_us,
  470. DWC2_ROUND_US_TO_SLICE(qh->device_us),
  471. DWC2_US_PER_SLICE * qh->ls_start_schedule_slice);
  472. if (map) {
  473. dwc2_sch_dbg(hsotg,
  474. "QH=%p Whole low/full speed map %p now:\n",
  475. qh, map);
  476. pmap_print(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
  477. DWC2_LS_SCHEDULE_FRAMES, "Frame ", "slices",
  478. dwc2_qh_print, &print_data);
  479. }
  480. }
  481. for (i = 0; i < qh->num_hs_transfers; i++) {
  482. struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + i;
  483. int uframe = trans_time->start_schedule_us /
  484. DWC2_HS_PERIODIC_US_PER_UFRAME;
  485. int rel_us = trans_time->start_schedule_us %
  486. DWC2_HS_PERIODIC_US_PER_UFRAME;
  487. dwc2_sch_dbg(hsotg,
  488. "QH=%p HS trans #%d: %d us @ uFrame %d + %d us\n",
  489. qh, i, trans_time->duration_us, uframe, rel_us);
  490. }
  491. if (qh->num_hs_transfers) {
  492. dwc2_sch_dbg(hsotg, "QH=%p Whole high speed map now:\n", qh);
  493. pmap_print(hsotg->hs_periodic_bitmap,
  494. DWC2_HS_PERIODIC_US_PER_UFRAME,
  495. DWC2_HS_SCHEDULE_UFRAMES, "uFrame", "us",
  496. dwc2_qh_print, &print_data);
  497. }
  498. return;
  499. }
  500. #else
  501. static inline void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
  502. struct dwc2_qh *qh) {};
  503. #endif
  504. /**
  505. * dwc2_ls_pmap_schedule() - Schedule a low speed QH
  506. *
  507. * @hsotg: The HCD state structure for the DWC OTG controller.
  508. * @qh: QH for the periodic transfer.
  509. * @search_slice: We'll start trying to schedule at the passed slice.
  510. * Remember that slices are the units of the low speed
  511. * schedule (think 25us or so).
  512. *
  513. * Wraps pmap_schedule() with the right parameters for low speed scheduling.
  514. *
  515. * Normally we schedule low speed devices on the map associated with the TT.
  516. *
  517. * Returns: 0 for success or an error code.
  518. */
  519. static int dwc2_ls_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  520. int search_slice)
  521. {
  522. int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
  523. unsigned long *map = dwc2_get_ls_map(hsotg, qh);
  524. int slice;
  525. if (!map)
  526. return -EINVAL;
  527. /*
  528. * Schedule on the proper low speed map with our low speed scheduling
  529. * parameters. Note that we use the "device_interval" here since
  530. * we want the low speed interval and the only way we'd be in this
  531. * function is if the device is low speed.
  532. *
  533. * If we happen to be doing low speed and high speed scheduling for the
  534. * same transaction (AKA we have a split) we always do low speed first.
  535. * That means we can always pass "false" for only_one_period (that
  536. * parameters is only useful when we're trying to get one schedule to
  537. * match what we already planned in the other schedule).
  538. */
  539. slice = pmap_schedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
  540. DWC2_LS_SCHEDULE_FRAMES, slices,
  541. qh->device_interval, search_slice, false);
  542. if (slice < 0)
  543. return slice;
  544. qh->ls_start_schedule_slice = slice;
  545. return 0;
  546. }
  547. /**
  548. * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_ls_pmap_schedule()
  549. *
  550. * @hsotg: The HCD state structure for the DWC OTG controller.
  551. * @qh: QH for the periodic transfer.
  552. */
  553. static void dwc2_ls_pmap_unschedule(struct dwc2_hsotg *hsotg,
  554. struct dwc2_qh *qh)
  555. {
  556. int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
  557. unsigned long *map = dwc2_get_ls_map(hsotg, qh);
  558. /* Schedule should have failed, so no worries about no error code */
  559. if (!map)
  560. return;
  561. pmap_unschedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
  562. DWC2_LS_SCHEDULE_FRAMES, slices, qh->device_interval,
  563. qh->ls_start_schedule_slice);
  564. }
  565. /**
  566. * dwc2_hs_pmap_schedule - Schedule in the main high speed schedule
  567. *
  568. * This will schedule something on the main dwc2 schedule.
  569. *
  570. * We'll start looking in qh->hs_transfers[index].start_schedule_us. We'll
  571. * update this with the result upon success. We also use the duration from
  572. * the same structure.
  573. *
  574. * @hsotg: The HCD state structure for the DWC OTG controller.
  575. * @qh: QH for the periodic transfer.
  576. * @only_one_period: If true we will limit ourselves to just looking at
  577. * one period (aka one 100us chunk). This is used if we have
  578. * already scheduled something on the low speed schedule and
  579. * need to find something that matches on the high speed one.
  580. * @index: The index into qh->hs_transfers that we're working with.
  581. *
  582. * Returns: 0 for success or an error code. Upon success the
  583. * dwc2_hs_transfer_time specified by "index" will be updated.
  584. */
  585. static int dwc2_hs_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  586. bool only_one_period, int index)
  587. {
  588. struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
  589. int us;
  590. us = pmap_schedule(hsotg->hs_periodic_bitmap,
  591. DWC2_HS_PERIODIC_US_PER_UFRAME,
  592. DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
  593. qh->host_interval, trans_time->start_schedule_us,
  594. only_one_period);
  595. if (us < 0)
  596. return us;
  597. trans_time->start_schedule_us = us;
  598. return 0;
  599. }
  600. /**
  601. * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_hs_pmap_schedule()
  602. *
  603. * @hsotg: The HCD state structure for the DWC OTG controller.
  604. * @qh: QH for the periodic transfer.
  605. */
  606. static void dwc2_hs_pmap_unschedule(struct dwc2_hsotg *hsotg,
  607. struct dwc2_qh *qh, int index)
  608. {
  609. struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
  610. pmap_unschedule(hsotg->hs_periodic_bitmap,
  611. DWC2_HS_PERIODIC_US_PER_UFRAME,
  612. DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
  613. qh->host_interval, trans_time->start_schedule_us);
  614. }
  615. /**
  616. * dwc2_uframe_schedule_split - Schedule a QH for a periodic split xfer.
  617. *
  618. * This is the most complicated thing in USB. We have to find matching time
  619. * in both the global high speed schedule for the port and the low speed
  620. * schedule for the TT associated with the given device.
  621. *
  622. * Being here means that the host must be running in high speed mode and the
  623. * device is in low or full speed mode (and behind a hub).
  624. *
  625. * @hsotg: The HCD state structure for the DWC OTG controller.
  626. * @qh: QH for the periodic transfer.
  627. */
  628. static int dwc2_uframe_schedule_split(struct dwc2_hsotg *hsotg,
  629. struct dwc2_qh *qh)
  630. {
  631. int bytecount = dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
  632. int ls_search_slice;
  633. int err = 0;
  634. int host_interval_in_sched;
  635. /*
  636. * The interval (how often to repeat) in the actual host schedule.
  637. * See pmap_schedule() for gcd() explanation.
  638. */
  639. host_interval_in_sched = gcd(qh->host_interval,
  640. DWC2_HS_SCHEDULE_UFRAMES);
  641. /*
  642. * We always try to find space in the low speed schedule first, then
  643. * try to find high speed time that matches. If we don't, we'll bump
  644. * up the place we start searching in the low speed schedule and try
  645. * again. To start we'll look right at the beginning of the low speed
  646. * schedule.
  647. *
  648. * Note that this will tend to front-load the high speed schedule.
  649. * We may eventually want to try to avoid this by either considering
  650. * both schedules together or doing some sort of round robin.
  651. */
  652. ls_search_slice = 0;
  653. while (ls_search_slice < DWC2_LS_SCHEDULE_SLICES) {
  654. int start_s_uframe;
  655. int ssplit_s_uframe;
  656. int second_s_uframe;
  657. int rel_uframe;
  658. int first_count;
  659. int middle_count;
  660. int end_count;
  661. int first_data_bytes;
  662. int other_data_bytes;
  663. int i;
  664. if (qh->schedule_low_speed) {
  665. err = dwc2_ls_pmap_schedule(hsotg, qh, ls_search_slice);
  666. /*
  667. * If we got an error here there's no other magic we
  668. * can do, so bail. All the looping above is only
  669. * helpful to redo things if we got a low speed slot
  670. * and then couldn't find a matching high speed slot.
  671. */
  672. if (err)
  673. return err;
  674. } else {
  675. /* Must be missing the tt structure? Why? */
  676. WARN_ON_ONCE(1);
  677. }
  678. /*
  679. * This will give us a number 0 - 7 if
  680. * DWC2_LS_SCHEDULE_FRAMES == 1, or 0 - 15 if == 2, or ...
  681. */
  682. start_s_uframe = qh->ls_start_schedule_slice /
  683. DWC2_SLICES_PER_UFRAME;
  684. /* Get a number that's always 0 - 7 */
  685. rel_uframe = (start_s_uframe % 8);
  686. /*
  687. * If we were going to start in uframe 7 then we would need to
  688. * issue a start split in uframe 6, which spec says is not OK.
  689. * Move on to the next full frame (assuming there is one).
  690. *
  691. * See 11.18.4 Host Split Transaction Scheduling Requirements
  692. * bullet 1.
  693. */
  694. if (rel_uframe == 7) {
  695. if (qh->schedule_low_speed)
  696. dwc2_ls_pmap_unschedule(hsotg, qh);
  697. ls_search_slice =
  698. (qh->ls_start_schedule_slice /
  699. DWC2_LS_PERIODIC_SLICES_PER_FRAME + 1) *
  700. DWC2_LS_PERIODIC_SLICES_PER_FRAME;
  701. continue;
  702. }
  703. /*
  704. * For ISOC in:
  705. * - start split (frame -1)
  706. * - complete split w/ data (frame +1)
  707. * - complete split w/ data (frame +2)
  708. * - ...
  709. * - complete split w/ data (frame +num_data_packets)
  710. * - complete split w/ data (frame +num_data_packets+1)
  711. * - complete split w/ data (frame +num_data_packets+2, max 8)
  712. * ...though if frame was "0" then max is 7...
  713. *
  714. * For ISOC out we might need to do:
  715. * - start split w/ data (frame -1)
  716. * - start split w/ data (frame +0)
  717. * - ...
  718. * - start split w/ data (frame +num_data_packets-2)
  719. *
  720. * For INTERRUPT in we might need to do:
  721. * - start split (frame -1)
  722. * - complete split w/ data (frame +1)
  723. * - complete split w/ data (frame +2)
  724. * - complete split w/ data (frame +3, max 8)
  725. *
  726. * For INTERRUPT out we might need to do:
  727. * - start split w/ data (frame -1)
  728. * - complete split (frame +1)
  729. * - complete split (frame +2)
  730. * - complete split (frame +3, max 8)
  731. *
  732. * Start adjusting!
  733. */
  734. ssplit_s_uframe = (start_s_uframe +
  735. host_interval_in_sched - 1) %
  736. host_interval_in_sched;
  737. if (qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in)
  738. second_s_uframe = start_s_uframe;
  739. else
  740. second_s_uframe = start_s_uframe + 1;
  741. /* First data transfer might not be all 188 bytes. */
  742. first_data_bytes = 188 -
  743. DIV_ROUND_UP(188 * (qh->ls_start_schedule_slice %
  744. DWC2_SLICES_PER_UFRAME),
  745. DWC2_SLICES_PER_UFRAME);
  746. if (first_data_bytes > bytecount)
  747. first_data_bytes = bytecount;
  748. other_data_bytes = bytecount - first_data_bytes;
  749. /*
  750. * For now, skip OUT xfers where first xfer is partial
  751. *
  752. * Main dwc2 code assumes:
  753. * - INT transfers never get split in two.
  754. * - ISOC transfers can always transfer 188 bytes the first
  755. * time.
  756. *
  757. * Until that code is fixed, try again if the first transfer
  758. * couldn't transfer everything.
  759. *
  760. * This code can be removed if/when the rest of dwc2 handles
  761. * the above cases. Until it's fixed we just won't be able
  762. * to schedule quite as tightly.
  763. */
  764. if (!qh->ep_is_in &&
  765. (first_data_bytes != min_t(int, 188, bytecount))) {
  766. dwc2_sch_dbg(hsotg,
  767. "QH=%p avoiding broken 1st xfer (%d, %d)\n",
  768. qh, first_data_bytes, bytecount);
  769. if (qh->schedule_low_speed)
  770. dwc2_ls_pmap_unschedule(hsotg, qh);
  771. ls_search_slice = (start_s_uframe + 1) *
  772. DWC2_SLICES_PER_UFRAME;
  773. continue;
  774. }
  775. /* Start by assuming transfers for the bytes */
  776. qh->num_hs_transfers = 1 + DIV_ROUND_UP(other_data_bytes, 188);
  777. /*
  778. * Everything except ISOC OUT has extra transfers. Rules are
  779. * complicated. See 11.18.4 Host Split Transaction Scheduling
  780. * Requirements bullet 3.
  781. */
  782. if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
  783. if (rel_uframe == 6)
  784. qh->num_hs_transfers += 2;
  785. else
  786. qh->num_hs_transfers += 3;
  787. if (qh->ep_is_in) {
  788. /*
  789. * First is start split, middle/end is data.
  790. * Allocate full data bytes for all data.
  791. */
  792. first_count = 4;
  793. middle_count = bytecount;
  794. end_count = bytecount;
  795. } else {
  796. /*
  797. * First is data, middle/end is complete.
  798. * First transfer and second can have data.
  799. * Rest should just have complete split.
  800. */
  801. first_count = first_data_bytes;
  802. middle_count = max_t(int, 4, other_data_bytes);
  803. end_count = 4;
  804. }
  805. } else {
  806. if (qh->ep_is_in) {
  807. int last;
  808. /* Account for the start split */
  809. qh->num_hs_transfers++;
  810. /* Calculate "L" value from spec */
  811. last = rel_uframe + qh->num_hs_transfers + 1;
  812. /* Start with basic case */
  813. if (last <= 6)
  814. qh->num_hs_transfers += 2;
  815. else
  816. qh->num_hs_transfers += 1;
  817. /* Adjust downwards */
  818. if (last >= 6 && rel_uframe == 0)
  819. qh->num_hs_transfers--;
  820. /* 1st = start; rest can contain data */
  821. first_count = 4;
  822. middle_count = min_t(int, 188, bytecount);
  823. end_count = middle_count;
  824. } else {
  825. /* All contain data, last might be smaller */
  826. first_count = first_data_bytes;
  827. middle_count = min_t(int, 188,
  828. other_data_bytes);
  829. end_count = other_data_bytes % 188;
  830. }
  831. }
  832. /* Assign durations per uFrame */
  833. qh->hs_transfers[0].duration_us = HS_USECS_ISO(first_count);
  834. for (i = 1; i < qh->num_hs_transfers - 1; i++)
  835. qh->hs_transfers[i].duration_us =
  836. HS_USECS_ISO(middle_count);
  837. if (qh->num_hs_transfers > 1)
  838. qh->hs_transfers[qh->num_hs_transfers - 1].duration_us =
  839. HS_USECS_ISO(end_count);
  840. /*
  841. * Assign start us. The call below to dwc2_hs_pmap_schedule()
  842. * will start with these numbers but may adjust within the same
  843. * microframe.
  844. */
  845. qh->hs_transfers[0].start_schedule_us =
  846. ssplit_s_uframe * DWC2_HS_PERIODIC_US_PER_UFRAME;
  847. for (i = 1; i < qh->num_hs_transfers; i++)
  848. qh->hs_transfers[i].start_schedule_us =
  849. ((second_s_uframe + i - 1) %
  850. DWC2_HS_SCHEDULE_UFRAMES) *
  851. DWC2_HS_PERIODIC_US_PER_UFRAME;
  852. /* Try to schedule with filled in hs_transfers above */
  853. for (i = 0; i < qh->num_hs_transfers; i++) {
  854. err = dwc2_hs_pmap_schedule(hsotg, qh, true, i);
  855. if (err)
  856. break;
  857. }
  858. /* If we scheduled all w/out breaking out then we're all good */
  859. if (i == qh->num_hs_transfers)
  860. break;
  861. for (; i >= 0; i--)
  862. dwc2_hs_pmap_unschedule(hsotg, qh, i);
  863. if (qh->schedule_low_speed)
  864. dwc2_ls_pmap_unschedule(hsotg, qh);
  865. /* Try again starting in the next microframe */
  866. ls_search_slice = (start_s_uframe + 1) * DWC2_SLICES_PER_UFRAME;
  867. }
  868. if (ls_search_slice >= DWC2_LS_SCHEDULE_SLICES)
  869. return -ENOSPC;
  870. return 0;
  871. }
  872. /**
  873. * dwc2_uframe_schedule_hs - Schedule a QH for a periodic high speed xfer.
  874. *
  875. * Basically this just wraps dwc2_hs_pmap_schedule() to provide a clean
  876. * interface.
  877. *
  878. * @hsotg: The HCD state structure for the DWC OTG controller.
  879. * @qh: QH for the periodic transfer.
  880. */
  881. static int dwc2_uframe_schedule_hs(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  882. {
  883. /* In non-split host and device time are the same */
  884. WARN_ON(qh->host_us != qh->device_us);
  885. WARN_ON(qh->host_interval != qh->device_interval);
  886. WARN_ON(qh->num_hs_transfers != 1);
  887. /* We'll have one transfer; init start to 0 before calling scheduler */
  888. qh->hs_transfers[0].start_schedule_us = 0;
  889. qh->hs_transfers[0].duration_us = qh->host_us;
  890. return dwc2_hs_pmap_schedule(hsotg, qh, false, 0);
  891. }
  892. /**
  893. * dwc2_uframe_schedule_ls - Schedule a QH for a periodic low/full speed xfer.
  894. *
  895. * Basically this just wraps dwc2_ls_pmap_schedule() to provide a clean
  896. * interface.
  897. *
  898. * @hsotg: The HCD state structure for the DWC OTG controller.
  899. * @qh: QH for the periodic transfer.
  900. */
  901. static int dwc2_uframe_schedule_ls(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  902. {
  903. /* In non-split host and device time are the same */
  904. WARN_ON(qh->host_us != qh->device_us);
  905. WARN_ON(qh->host_interval != qh->device_interval);
  906. WARN_ON(!qh->schedule_low_speed);
  907. /* Run on the main low speed schedule (no split = no hub = no TT) */
  908. return dwc2_ls_pmap_schedule(hsotg, qh, 0);
  909. }
  910. /**
  911. * dwc2_uframe_schedule - Schedule a QH for a periodic xfer.
  912. *
  913. * Calls one of the 3 sub-function depending on what type of transfer this QH
  914. * is for. Also adds some printing.
  915. *
  916. * @hsotg: The HCD state structure for the DWC OTG controller.
  917. * @qh: QH for the periodic transfer.
  918. */
  919. static int dwc2_uframe_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  920. {
  921. int ret;
  922. if (qh->dev_speed == USB_SPEED_HIGH)
  923. ret = dwc2_uframe_schedule_hs(hsotg, qh);
  924. else if (!qh->do_split)
  925. ret = dwc2_uframe_schedule_ls(hsotg, qh);
  926. else
  927. ret = dwc2_uframe_schedule_split(hsotg, qh);
  928. if (ret)
  929. dwc2_sch_dbg(hsotg, "QH=%p Failed to schedule %d\n", qh, ret);
  930. else
  931. dwc2_qh_schedule_print(hsotg, qh);
  932. return ret;
  933. }
  934. /**
  935. * dwc2_uframe_unschedule - Undoes dwc2_uframe_schedule().
  936. *
  937. * @hsotg: The HCD state structure for the DWC OTG controller.
  938. * @qh: QH for the periodic transfer.
  939. */
  940. static void dwc2_uframe_unschedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  941. {
  942. int i;
  943. for (i = 0; i < qh->num_hs_transfers; i++)
  944. dwc2_hs_pmap_unschedule(hsotg, qh, i);
  945. if (qh->schedule_low_speed)
  946. dwc2_ls_pmap_unschedule(hsotg, qh);
  947. dwc2_sch_dbg(hsotg, "QH=%p Unscheduled\n", qh);
  948. }
  949. /**
  950. * dwc2_pick_first_frame() - Choose 1st frame for qh that's already scheduled
  951. *
  952. * Takes a qh that has already been scheduled (which means we know we have the
  953. * bandwdith reserved for us) and set the next_active_frame and the
  954. * start_active_frame.
  955. *
  956. * This is expected to be called on qh's that weren't previously actively
  957. * running. It just picks the next frame that we can fit into without any
  958. * thought about the past.
  959. *
  960. * @hsotg: The HCD state structure for the DWC OTG controller
  961. * @qh: QH for a periodic endpoint
  962. *
  963. */
  964. static void dwc2_pick_first_frame(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  965. {
  966. u16 frame_number;
  967. u16 earliest_frame;
  968. u16 next_active_frame;
  969. u16 relative_frame;
  970. u16 interval;
  971. /*
  972. * Use the real frame number rather than the cached value as of the
  973. * last SOF to give us a little extra slop.
  974. */
  975. frame_number = dwc2_hcd_get_frame_number(hsotg);
  976. /*
  977. * We wouldn't want to start any earlier than the next frame just in
  978. * case the frame number ticks as we're doing this calculation.
  979. *
  980. * NOTE: if we could quantify how long till we actually get scheduled
  981. * we might be able to avoid the "+ 1" by looking at the upper part of
  982. * HFNUM (the FRREM field). For now we'll just use the + 1 though.
  983. */
  984. earliest_frame = dwc2_frame_num_inc(frame_number, 1);
  985. next_active_frame = earliest_frame;
  986. /* Get the "no microframe schduler" out of the way... */
  987. if (hsotg->params.uframe_sched <= 0) {
  988. if (qh->do_split)
  989. /* Splits are active at microframe 0 minus 1 */
  990. next_active_frame |= 0x7;
  991. goto exit;
  992. }
  993. if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
  994. /*
  995. * We're either at high speed or we're doing a split (which
  996. * means we're talking high speed to a hub). In any case
  997. * the first frame should be based on when the first scheduled
  998. * event is.
  999. */
  1000. WARN_ON(qh->num_hs_transfers < 1);
  1001. relative_frame = qh->hs_transfers[0].start_schedule_us /
  1002. DWC2_HS_PERIODIC_US_PER_UFRAME;
  1003. /* Adjust interval as per high speed schedule */
  1004. interval = gcd(qh->host_interval, DWC2_HS_SCHEDULE_UFRAMES);
  1005. } else {
  1006. /*
  1007. * Low or full speed directly on dwc2. Just about the same
  1008. * as high speed but on a different schedule and with slightly
  1009. * different adjustments. Note that this works because when
  1010. * the host and device are both low speed then frames in the
  1011. * controller tick at low speed.
  1012. */
  1013. relative_frame = qh->ls_start_schedule_slice /
  1014. DWC2_LS_PERIODIC_SLICES_PER_FRAME;
  1015. interval = gcd(qh->host_interval, DWC2_LS_SCHEDULE_FRAMES);
  1016. }
  1017. /* Scheduler messed up if frame is past interval */
  1018. WARN_ON(relative_frame >= interval);
  1019. /*
  1020. * We know interval must divide (HFNUM_MAX_FRNUM + 1) now that we've
  1021. * done the gcd(), so it's safe to move to the beginning of the current
  1022. * interval like this.
  1023. *
  1024. * After this we might be before earliest_frame, but don't worry,
  1025. * we'll fix it...
  1026. */
  1027. next_active_frame = (next_active_frame / interval) * interval;
  1028. /*
  1029. * Actually choose to start at the frame number we've been
  1030. * scheduled for.
  1031. */
  1032. next_active_frame = dwc2_frame_num_inc(next_active_frame,
  1033. relative_frame);
  1034. /*
  1035. * We actually need 1 frame before since the next_active_frame is
  1036. * the frame number we'll be put on the ready list and we won't be on
  1037. * the bus until 1 frame later.
  1038. */
  1039. next_active_frame = dwc2_frame_num_dec(next_active_frame, 1);
  1040. /*
  1041. * By now we might actually be before the earliest_frame. Let's move
  1042. * up intervals until we're not.
  1043. */
  1044. while (dwc2_frame_num_gt(earliest_frame, next_active_frame))
  1045. next_active_frame = dwc2_frame_num_inc(next_active_frame,
  1046. interval);
  1047. exit:
  1048. qh->next_active_frame = next_active_frame;
  1049. qh->start_active_frame = next_active_frame;
  1050. dwc2_sch_vdbg(hsotg, "QH=%p First fn=%04x nxt=%04x\n",
  1051. qh, frame_number, qh->next_active_frame);
  1052. }
  1053. /**
  1054. * dwc2_do_reserve() - Make a periodic reservation
  1055. *
  1056. * Try to allocate space in the periodic schedule. Depending on parameters
  1057. * this might use the microframe scheduler or the dumb scheduler.
  1058. *
  1059. * @hsotg: The HCD state structure for the DWC OTG controller
  1060. * @qh: QH for the periodic transfer.
  1061. *
  1062. * Returns: 0 upon success; error upon failure.
  1063. */
  1064. static int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  1065. {
  1066. int status;
  1067. if (hsotg->params.uframe_sched > 0) {
  1068. status = dwc2_uframe_schedule(hsotg, qh);
  1069. } else {
  1070. status = dwc2_periodic_channel_available(hsotg);
  1071. if (status) {
  1072. dev_info(hsotg->dev,
  1073. "%s: No host channel available for periodic transfer\n",
  1074. __func__);
  1075. return status;
  1076. }
  1077. status = dwc2_check_periodic_bandwidth(hsotg, qh);
  1078. }
  1079. if (status) {
  1080. dev_dbg(hsotg->dev,
  1081. "%s: Insufficient periodic bandwidth for periodic transfer\n",
  1082. __func__);
  1083. return status;
  1084. }
  1085. if (hsotg->params.uframe_sched <= 0)
  1086. /* Reserve periodic channel */
  1087. hsotg->periodic_channels++;
  1088. /* Update claimed usecs per (micro)frame */
  1089. hsotg->periodic_usecs += qh->host_us;
  1090. dwc2_pick_first_frame(hsotg, qh);
  1091. return 0;
  1092. }
  1093. /**
  1094. * dwc2_do_unreserve() - Actually release the periodic reservation
  1095. *
  1096. * This function actually releases the periodic bandwidth that was reserved
  1097. * by the given qh.
  1098. *
  1099. * @hsotg: The HCD state structure for the DWC OTG controller
  1100. * @qh: QH for the periodic transfer.
  1101. */
  1102. static void dwc2_do_unreserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  1103. {
  1104. assert_spin_locked(&hsotg->lock);
  1105. WARN_ON(!qh->unreserve_pending);
  1106. /* No more unreserve pending--we're doing it */
  1107. qh->unreserve_pending = false;
  1108. if (WARN_ON(!list_empty(&qh->qh_list_entry)))
  1109. list_del_init(&qh->qh_list_entry);
  1110. /* Update claimed usecs per (micro)frame */
  1111. hsotg->periodic_usecs -= qh->host_us;
  1112. if (hsotg->params.uframe_sched > 0) {
  1113. dwc2_uframe_unschedule(hsotg, qh);
  1114. } else {
  1115. /* Release periodic channel reservation */
  1116. hsotg->periodic_channels--;
  1117. }
  1118. }
  1119. /**
  1120. * dwc2_unreserve_timer_fn() - Timer function to release periodic reservation
  1121. *
  1122. * According to the kernel doc for usb_submit_urb() (specifically the part about
  1123. * "Reserved Bandwidth Transfers"), we need to keep a reservation active as
  1124. * long as a device driver keeps submitting. Since we're using HCD_BH to give
  1125. * back the URB we need to give the driver a little bit of time before we
  1126. * release the reservation. This worker is called after the appropriate
  1127. * delay.
  1128. *
  1129. * @work: Pointer to a qh unreserve_work.
  1130. */
  1131. static void dwc2_unreserve_timer_fn(unsigned long data)
  1132. {
  1133. struct dwc2_qh *qh = (struct dwc2_qh *)data;
  1134. struct dwc2_hsotg *hsotg = qh->hsotg;
  1135. unsigned long flags;
  1136. /*
  1137. * Wait for the lock, or for us to be scheduled again. We
  1138. * could be scheduled again if:
  1139. * - We started executing but didn't get the lock yet.
  1140. * - A new reservation came in, but cancel didn't take effect
  1141. * because we already started executing.
  1142. * - The timer has been kicked again.
  1143. * In that case cancel and wait for the next call.
  1144. */
  1145. while (!spin_trylock_irqsave(&hsotg->lock, flags)) {
  1146. if (timer_pending(&qh->unreserve_timer))
  1147. return;
  1148. }
  1149. /*
  1150. * Might be no more unreserve pending if:
  1151. * - We started executing but didn't get the lock yet.
  1152. * - A new reservation came in, but cancel didn't take effect
  1153. * because we already started executing.
  1154. *
  1155. * We can't put this in the loop above because unreserve_pending needs
  1156. * to be accessed under lock, so we can only check it once we got the
  1157. * lock.
  1158. */
  1159. if (qh->unreserve_pending)
  1160. dwc2_do_unreserve(hsotg, qh);
  1161. spin_unlock_irqrestore(&hsotg->lock, flags);
  1162. }
  1163. /**
  1164. * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a
  1165. * host channel is large enough to handle the maximum data transfer in a single
  1166. * (micro)frame for a periodic transfer
  1167. *
  1168. * @hsotg: The HCD state structure for the DWC OTG controller
  1169. * @qh: QH for a periodic endpoint
  1170. *
  1171. * Return: 0 if successful, negative error code otherwise
  1172. */
  1173. static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
  1174. struct dwc2_qh *qh)
  1175. {
  1176. u32 max_xfer_size;
  1177. u32 max_channel_xfer_size;
  1178. int status = 0;
  1179. max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
  1180. max_channel_xfer_size = hsotg->params.max_transfer_size;
  1181. if (max_xfer_size > max_channel_xfer_size) {
  1182. dev_err(hsotg->dev,
  1183. "%s: Periodic xfer length %d > max xfer length for channel %d\n",
  1184. __func__, max_xfer_size, max_channel_xfer_size);
  1185. status = -ENOSPC;
  1186. }
  1187. return status;
  1188. }
  1189. /**
  1190. * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in
  1191. * the periodic schedule
  1192. *
  1193. * @hsotg: The HCD state structure for the DWC OTG controller
  1194. * @qh: QH for the periodic transfer. The QH should already contain the
  1195. * scheduling information.
  1196. *
  1197. * Return: 0 if successful, negative error code otherwise
  1198. */
  1199. static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  1200. {
  1201. int status;
  1202. status = dwc2_check_max_xfer_size(hsotg, qh);
  1203. if (status) {
  1204. dev_dbg(hsotg->dev,
  1205. "%s: Channel max transfer size too small for periodic transfer\n",
  1206. __func__);
  1207. return status;
  1208. }
  1209. /* Cancel pending unreserve; if canceled OK, unreserve was pending */
  1210. if (del_timer(&qh->unreserve_timer))
  1211. WARN_ON(!qh->unreserve_pending);
  1212. /*
  1213. * Only need to reserve if there's not an unreserve pending, since if an
  1214. * unreserve is pending then by definition our old reservation is still
  1215. * valid. Unreserve might still be pending even if we didn't cancel if
  1216. * dwc2_unreserve_timer_fn() already started. Code in the timer handles
  1217. * that case.
  1218. */
  1219. if (!qh->unreserve_pending) {
  1220. status = dwc2_do_reserve(hsotg, qh);
  1221. if (status)
  1222. return status;
  1223. } else {
  1224. /*
  1225. * It might have been a while, so make sure that frame_number
  1226. * is still good. Note: we could also try to use the similar
  1227. * dwc2_next_periodic_start() but that schedules much more
  1228. * tightly and we might need to hurry and queue things up.
  1229. */
  1230. if (dwc2_frame_num_le(qh->next_active_frame,
  1231. hsotg->frame_number))
  1232. dwc2_pick_first_frame(hsotg, qh);
  1233. }
  1234. qh->unreserve_pending = 0;
  1235. if (hsotg->params.dma_desc_enable > 0)
  1236. /* Don't rely on SOF and start in ready schedule */
  1237. list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
  1238. else
  1239. /* Always start in inactive schedule */
  1240. list_add_tail(&qh->qh_list_entry,
  1241. &hsotg->periodic_sched_inactive);
  1242. return 0;
  1243. }
  1244. /**
  1245. * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer
  1246. * from the periodic schedule
  1247. *
  1248. * @hsotg: The HCD state structure for the DWC OTG controller
  1249. * @qh: QH for the periodic transfer
  1250. */
  1251. static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
  1252. struct dwc2_qh *qh)
  1253. {
  1254. bool did_modify;
  1255. assert_spin_locked(&hsotg->lock);
  1256. /*
  1257. * Schedule the unreserve to happen in a little bit. Cases here:
  1258. * - Unreserve worker might be sitting there waiting to grab the lock.
  1259. * In this case it will notice it's been schedule again and will
  1260. * quit.
  1261. * - Unreserve worker might not be scheduled.
  1262. *
  1263. * We should never already be scheduled since dwc2_schedule_periodic()
  1264. * should have canceled the scheduled unreserve timer (hence the
  1265. * warning on did_modify).
  1266. *
  1267. * We add + 1 to the timer to guarantee that at least 1 jiffy has
  1268. * passed (otherwise if the jiffy counter might tick right after we
  1269. * read it and we'll get no delay).
  1270. */
  1271. did_modify = mod_timer(&qh->unreserve_timer,
  1272. jiffies + DWC2_UNRESERVE_DELAY + 1);
  1273. WARN_ON(did_modify);
  1274. qh->unreserve_pending = 1;
  1275. list_del_init(&qh->qh_list_entry);
  1276. }
  1277. /**
  1278. * dwc2_qh_init() - Initializes a QH structure
  1279. *
  1280. * @hsotg: The HCD state structure for the DWC OTG controller
  1281. * @qh: The QH to init
  1282. * @urb: Holds the information about the device/endpoint needed to initialize
  1283. * the QH
  1284. * @mem_flags: Flags for allocating memory.
  1285. */
  1286. static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  1287. struct dwc2_hcd_urb *urb, gfp_t mem_flags)
  1288. {
  1289. int dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
  1290. u8 ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
  1291. bool ep_is_in = !!dwc2_hcd_is_pipe_in(&urb->pipe_info);
  1292. bool ep_is_isoc = (ep_type == USB_ENDPOINT_XFER_ISOC);
  1293. bool ep_is_int = (ep_type == USB_ENDPOINT_XFER_INT);
  1294. u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
  1295. u32 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
  1296. bool do_split = (prtspd == HPRT0_SPD_HIGH_SPEED &&
  1297. dev_speed != USB_SPEED_HIGH);
  1298. int maxp = dwc2_hcd_get_mps(&urb->pipe_info);
  1299. int bytecount = dwc2_hb_mult(maxp) * dwc2_max_packet(maxp);
  1300. char *speed, *type;
  1301. /* Initialize QH */
  1302. qh->hsotg = hsotg;
  1303. setup_timer(&qh->unreserve_timer, dwc2_unreserve_timer_fn,
  1304. (unsigned long)qh);
  1305. qh->ep_type = ep_type;
  1306. qh->ep_is_in = ep_is_in;
  1307. qh->data_toggle = DWC2_HC_PID_DATA0;
  1308. qh->maxp = maxp;
  1309. INIT_LIST_HEAD(&qh->qtd_list);
  1310. INIT_LIST_HEAD(&qh->qh_list_entry);
  1311. qh->do_split = do_split;
  1312. qh->dev_speed = dev_speed;
  1313. if (ep_is_int || ep_is_isoc) {
  1314. /* Compute scheduling parameters once and save them */
  1315. int host_speed = do_split ? USB_SPEED_HIGH : dev_speed;
  1316. struct dwc2_tt *dwc_tt = dwc2_host_get_tt_info(hsotg, urb->priv,
  1317. mem_flags,
  1318. &qh->ttport);
  1319. int device_ns;
  1320. qh->dwc_tt = dwc_tt;
  1321. qh->host_us = NS_TO_US(usb_calc_bus_time(host_speed, ep_is_in,
  1322. ep_is_isoc, bytecount));
  1323. device_ns = usb_calc_bus_time(dev_speed, ep_is_in,
  1324. ep_is_isoc, bytecount);
  1325. if (do_split && dwc_tt)
  1326. device_ns += dwc_tt->usb_tt->think_time;
  1327. qh->device_us = NS_TO_US(device_ns);
  1328. qh->device_interval = urb->interval;
  1329. qh->host_interval = urb->interval * (do_split ? 8 : 1);
  1330. /*
  1331. * Schedule low speed if we're running the host in low or
  1332. * full speed OR if we've got a "TT" to deal with to access this
  1333. * device.
  1334. */
  1335. qh->schedule_low_speed = prtspd != HPRT0_SPD_HIGH_SPEED ||
  1336. dwc_tt;
  1337. if (do_split) {
  1338. /* We won't know num transfers until we schedule */
  1339. qh->num_hs_transfers = -1;
  1340. } else if (dev_speed == USB_SPEED_HIGH) {
  1341. qh->num_hs_transfers = 1;
  1342. } else {
  1343. qh->num_hs_transfers = 0;
  1344. }
  1345. /* We'll schedule later when we have something to do */
  1346. }
  1347. switch (dev_speed) {
  1348. case USB_SPEED_LOW:
  1349. speed = "low";
  1350. break;
  1351. case USB_SPEED_FULL:
  1352. speed = "full";
  1353. break;
  1354. case USB_SPEED_HIGH:
  1355. speed = "high";
  1356. break;
  1357. default:
  1358. speed = "?";
  1359. break;
  1360. }
  1361. switch (qh->ep_type) {
  1362. case USB_ENDPOINT_XFER_ISOC:
  1363. type = "isochronous";
  1364. break;
  1365. case USB_ENDPOINT_XFER_INT:
  1366. type = "interrupt";
  1367. break;
  1368. case USB_ENDPOINT_XFER_CONTROL:
  1369. type = "control";
  1370. break;
  1371. case USB_ENDPOINT_XFER_BULK:
  1372. type = "bulk";
  1373. break;
  1374. default:
  1375. type = "?";
  1376. break;
  1377. }
  1378. dwc2_sch_dbg(hsotg, "QH=%p Init %s, %s speed, %d bytes:\n", qh, type,
  1379. speed, bytecount);
  1380. dwc2_sch_dbg(hsotg, "QH=%p ...addr=%d, ep=%d, %s\n", qh,
  1381. dwc2_hcd_get_dev_addr(&urb->pipe_info),
  1382. dwc2_hcd_get_ep_num(&urb->pipe_info),
  1383. ep_is_in ? "IN" : "OUT");
  1384. if (ep_is_int || ep_is_isoc) {
  1385. dwc2_sch_dbg(hsotg,
  1386. "QH=%p ...duration: host=%d us, device=%d us\n",
  1387. qh, qh->host_us, qh->device_us);
  1388. dwc2_sch_dbg(hsotg, "QH=%p ...interval: host=%d, device=%d\n",
  1389. qh, qh->host_interval, qh->device_interval);
  1390. if (qh->schedule_low_speed)
  1391. dwc2_sch_dbg(hsotg, "QH=%p ...low speed schedule=%p\n",
  1392. qh, dwc2_get_ls_map(hsotg, qh));
  1393. }
  1394. }
  1395. /**
  1396. * dwc2_hcd_qh_create() - Allocates and initializes a QH
  1397. *
  1398. * @hsotg: The HCD state structure for the DWC OTG controller
  1399. * @urb: Holds the information about the device/endpoint needed
  1400. * to initialize the QH
  1401. * @atomic_alloc: Flag to do atomic allocation if needed
  1402. *
  1403. * Return: Pointer to the newly allocated QH, or NULL on error
  1404. */
  1405. struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
  1406. struct dwc2_hcd_urb *urb,
  1407. gfp_t mem_flags)
  1408. {
  1409. struct dwc2_qh *qh;
  1410. if (!urb->priv)
  1411. return NULL;
  1412. /* Allocate memory */
  1413. qh = kzalloc(sizeof(*qh), mem_flags);
  1414. if (!qh)
  1415. return NULL;
  1416. dwc2_qh_init(hsotg, qh, urb, mem_flags);
  1417. if (hsotg->params.dma_desc_enable > 0 &&
  1418. dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
  1419. dwc2_hcd_qh_free(hsotg, qh);
  1420. return NULL;
  1421. }
  1422. return qh;
  1423. }
  1424. /**
  1425. * dwc2_hcd_qh_free() - Frees the QH
  1426. *
  1427. * @hsotg: HCD instance
  1428. * @qh: The QH to free
  1429. *
  1430. * QH should already be removed from the list. QTD list should already be empty
  1431. * if called from URB Dequeue.
  1432. *
  1433. * Must NOT be called with interrupt disabled or spinlock held
  1434. */
  1435. void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  1436. {
  1437. /* Make sure any unreserve work is finished. */
  1438. if (del_timer_sync(&qh->unreserve_timer)) {
  1439. unsigned long flags;
  1440. spin_lock_irqsave(&hsotg->lock, flags);
  1441. dwc2_do_unreserve(hsotg, qh);
  1442. spin_unlock_irqrestore(&hsotg->lock, flags);
  1443. }
  1444. dwc2_host_put_tt_info(hsotg, qh->dwc_tt);
  1445. if (qh->desc_list)
  1446. dwc2_hcd_qh_free_ddma(hsotg, qh);
  1447. kfree(qh);
  1448. }
  1449. /**
  1450. * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic
  1451. * schedule if it is not already in the schedule. If the QH is already in
  1452. * the schedule, no action is taken.
  1453. *
  1454. * @hsotg: The HCD state structure for the DWC OTG controller
  1455. * @qh: The QH to add
  1456. *
  1457. * Return: 0 if successful, negative error code otherwise
  1458. */
  1459. int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  1460. {
  1461. int status;
  1462. u32 intr_mask;
  1463. if (dbg_qh(qh))
  1464. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1465. if (!list_empty(&qh->qh_list_entry))
  1466. /* QH already in a schedule */
  1467. return 0;
  1468. /* Add the new QH to the appropriate schedule */
  1469. if (dwc2_qh_is_non_per(qh)) {
  1470. /* Schedule right away */
  1471. qh->start_active_frame = hsotg->frame_number;
  1472. qh->next_active_frame = qh->start_active_frame;
  1473. /* Always start in inactive schedule */
  1474. list_add_tail(&qh->qh_list_entry,
  1475. &hsotg->non_periodic_sched_inactive);
  1476. return 0;
  1477. }
  1478. status = dwc2_schedule_periodic(hsotg, qh);
  1479. if (status)
  1480. return status;
  1481. if (!hsotg->periodic_qh_count) {
  1482. intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
  1483. intr_mask |= GINTSTS_SOF;
  1484. dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
  1485. }
  1486. hsotg->periodic_qh_count++;
  1487. return 0;
  1488. }
  1489. /**
  1490. * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic
  1491. * schedule. Memory is not freed.
  1492. *
  1493. * @hsotg: The HCD state structure
  1494. * @qh: QH to remove from schedule
  1495. */
  1496. void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
  1497. {
  1498. u32 intr_mask;
  1499. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1500. if (list_empty(&qh->qh_list_entry))
  1501. /* QH is not in a schedule */
  1502. return;
  1503. if (dwc2_qh_is_non_per(qh)) {
  1504. if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
  1505. hsotg->non_periodic_qh_ptr =
  1506. hsotg->non_periodic_qh_ptr->next;
  1507. list_del_init(&qh->qh_list_entry);
  1508. return;
  1509. }
  1510. dwc2_deschedule_periodic(hsotg, qh);
  1511. hsotg->periodic_qh_count--;
  1512. if (!hsotg->periodic_qh_count &&
  1513. hsotg->params.dma_desc_enable <= 0) {
  1514. intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
  1515. intr_mask &= ~GINTSTS_SOF;
  1516. dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
  1517. }
  1518. }
  1519. /**
  1520. * dwc2_next_for_periodic_split() - Set next_active_frame midway thru a split.
  1521. *
  1522. * This is called for setting next_active_frame for periodic splits for all but
  1523. * the first packet of the split. Confusing? I thought so...
  1524. *
  1525. * Periodic splits are single low/full speed transfers that we end up splitting
  1526. * up into several high speed transfers. They always fit into one full (1 ms)
  1527. * frame but might be split over several microframes (125 us each). We to put
  1528. * each of the parts on a very specific high speed frame.
  1529. *
  1530. * This function figures out where the next active uFrame needs to be.
  1531. *
  1532. * @hsotg: The HCD state structure
  1533. * @qh: QH for the periodic transfer.
  1534. * @frame_number: The current frame number.
  1535. *
  1536. * Return: number missed by (or 0 if we didn't miss).
  1537. */
  1538. static int dwc2_next_for_periodic_split(struct dwc2_hsotg *hsotg,
  1539. struct dwc2_qh *qh, u16 frame_number)
  1540. {
  1541. u16 old_frame = qh->next_active_frame;
  1542. u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
  1543. int missed = 0;
  1544. u16 incr;
  1545. /*
  1546. * See dwc2_uframe_schedule_split() for split scheduling.
  1547. *
  1548. * Basically: increment 1 normally, but 2 right after the start split
  1549. * (except for ISOC out).
  1550. */
  1551. if (old_frame == qh->start_active_frame &&
  1552. !(qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in))
  1553. incr = 2;
  1554. else
  1555. incr = 1;
  1556. qh->next_active_frame = dwc2_frame_num_inc(old_frame, incr);
  1557. /*
  1558. * Note that it's OK for frame_number to be 1 frame past
  1559. * next_active_frame. Remember that next_active_frame is supposed to
  1560. * be 1 frame _before_ when we want to be scheduled. If we're 1 frame
  1561. * past it just means schedule ASAP.
  1562. *
  1563. * It's _not_ OK, however, if we're more than one frame past.
  1564. */
  1565. if (dwc2_frame_num_gt(prev_frame_number, qh->next_active_frame)) {
  1566. /*
  1567. * OOPS, we missed. That's actually pretty bad since
  1568. * the hub will be unhappy; try ASAP I guess.
  1569. */
  1570. missed = dwc2_frame_num_dec(prev_frame_number,
  1571. qh->next_active_frame);
  1572. qh->next_active_frame = frame_number;
  1573. }
  1574. return missed;
  1575. }
  1576. /**
  1577. * dwc2_next_periodic_start() - Set next_active_frame for next transfer start
  1578. *
  1579. * This is called for setting next_active_frame for a periodic transfer for
  1580. * all cases other than midway through a periodic split. This will also update
  1581. * start_active_frame.
  1582. *
  1583. * Since we _always_ keep start_active_frame as the start of the previous
  1584. * transfer this is normally pretty easy: we just add our interval to
  1585. * start_active_frame and we've got our answer.
  1586. *
  1587. * The tricks come into play if we miss. In that case we'll look for the next
  1588. * slot we can fit into.
  1589. *
  1590. * @hsotg: The HCD state structure
  1591. * @qh: QH for the periodic transfer.
  1592. * @frame_number: The current frame number.
  1593. *
  1594. * Return: number missed by (or 0 if we didn't miss).
  1595. */
  1596. static int dwc2_next_periodic_start(struct dwc2_hsotg *hsotg,
  1597. struct dwc2_qh *qh, u16 frame_number)
  1598. {
  1599. int missed = 0;
  1600. u16 interval = qh->host_interval;
  1601. u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
  1602. qh->start_active_frame = dwc2_frame_num_inc(qh->start_active_frame,
  1603. interval);
  1604. /*
  1605. * The dwc2_frame_num_gt() function used below won't work terribly well
  1606. * with if we just incremented by a really large intervals since the
  1607. * frame counter only goes to 0x3fff. It's terribly unlikely that we
  1608. * will have missed in this case anyway. Just go to exit. If we want
  1609. * to try to do better we'll need to keep track of a bigger counter
  1610. * somewhere in the driver and handle overflows.
  1611. */
  1612. if (interval >= 0x1000)
  1613. goto exit;
  1614. /*
  1615. * Test for misses, which is when it's too late to schedule.
  1616. *
  1617. * A few things to note:
  1618. * - We compare against prev_frame_number since start_active_frame
  1619. * and next_active_frame are always 1 frame before we want things
  1620. * to be active and we assume we can still get scheduled in the
  1621. * current frame number.
  1622. * - It's possible for start_active_frame (now incremented) to be
  1623. * next_active_frame if we got an EO MISS (even_odd miss) which
  1624. * basically means that we detected there wasn't enough time for
  1625. * the last packet and dwc2_hc_set_even_odd_frame() rescheduled us
  1626. * at the last second. We want to make sure we don't schedule
  1627. * another transfer for the same frame. My test webcam doesn't seem
  1628. * terribly upset by missing a transfer but really doesn't like when
  1629. * we do two transfers in the same frame.
  1630. * - Some misses are expected. Specifically, in order to work
  1631. * perfectly dwc2 really needs quite spectacular interrupt latency
  1632. * requirements. It needs to be able to handle its interrupts
  1633. * completely within 125 us of them being asserted. That not only
  1634. * means that the dwc2 interrupt handler needs to be fast but it
  1635. * means that nothing else in the system has to block dwc2 for a long
  1636. * time. We can help with the dwc2 parts of this, but it's hard to
  1637. * guarantee that a system will have interrupt latency < 125 us, so
  1638. * we have to be robust to some misses.
  1639. */
  1640. if (qh->start_active_frame == qh->next_active_frame ||
  1641. dwc2_frame_num_gt(prev_frame_number, qh->start_active_frame)) {
  1642. u16 ideal_start = qh->start_active_frame;
  1643. int periods_in_map;
  1644. /*
  1645. * Adjust interval as per gcd with map size.
  1646. * See pmap_schedule() for more details here.
  1647. */
  1648. if (qh->do_split || qh->dev_speed == USB_SPEED_HIGH)
  1649. periods_in_map = DWC2_HS_SCHEDULE_UFRAMES;
  1650. else
  1651. periods_in_map = DWC2_LS_SCHEDULE_FRAMES;
  1652. interval = gcd(interval, periods_in_map);
  1653. do {
  1654. qh->start_active_frame = dwc2_frame_num_inc(
  1655. qh->start_active_frame, interval);
  1656. } while (dwc2_frame_num_gt(prev_frame_number,
  1657. qh->start_active_frame));
  1658. missed = dwc2_frame_num_dec(qh->start_active_frame,
  1659. ideal_start);
  1660. }
  1661. exit:
  1662. qh->next_active_frame = qh->start_active_frame;
  1663. return missed;
  1664. }
  1665. /*
  1666. * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  1667. * non-periodic schedule. The QH is added to the inactive non-periodic
  1668. * schedule if any QTDs are still attached to the QH.
  1669. *
  1670. * For periodic QHs, the QH is removed from the periodic queued schedule. If
  1671. * there are any QTDs still attached to the QH, the QH is added to either the
  1672. * periodic inactive schedule or the periodic ready schedule and its next
  1673. * scheduled frame is calculated. The QH is placed in the ready schedule if
  1674. * the scheduled frame has been reached already. Otherwise it's placed in the
  1675. * inactive schedule. If there are no QTDs attached to the QH, the QH is
  1676. * completely removed from the periodic schedule.
  1677. */
  1678. void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
  1679. int sched_next_periodic_split)
  1680. {
  1681. u16 old_frame = qh->next_active_frame;
  1682. u16 frame_number;
  1683. int missed;
  1684. if (dbg_qh(qh))
  1685. dev_vdbg(hsotg->dev, "%s()\n", __func__);
  1686. if (dwc2_qh_is_non_per(qh)) {
  1687. dwc2_hcd_qh_unlink(hsotg, qh);
  1688. if (!list_empty(&qh->qtd_list))
  1689. /* Add back to inactive non-periodic schedule */
  1690. dwc2_hcd_qh_add(hsotg, qh);
  1691. return;
  1692. }
  1693. /*
  1694. * Use the real frame number rather than the cached value as of the
  1695. * last SOF just to get us a little closer to reality. Note that
  1696. * means we don't actually know if we've already handled the SOF
  1697. * interrupt for this frame.
  1698. */
  1699. frame_number = dwc2_hcd_get_frame_number(hsotg);
  1700. if (sched_next_periodic_split)
  1701. missed = dwc2_next_for_periodic_split(hsotg, qh, frame_number);
  1702. else
  1703. missed = dwc2_next_periodic_start(hsotg, qh, frame_number);
  1704. dwc2_sch_vdbg(hsotg,
  1705. "QH=%p next(%d) fn=%04x, sch=%04x=>%04x (%+d) miss=%d %s\n",
  1706. qh, sched_next_periodic_split, frame_number, old_frame,
  1707. qh->next_active_frame,
  1708. dwc2_frame_num_dec(qh->next_active_frame, old_frame),
  1709. missed, missed ? "MISS" : "");
  1710. if (list_empty(&qh->qtd_list)) {
  1711. dwc2_hcd_qh_unlink(hsotg, qh);
  1712. return;
  1713. }
  1714. /*
  1715. * Remove from periodic_sched_queued and move to
  1716. * appropriate queue
  1717. *
  1718. * Note: we purposely use the frame_number from the "hsotg" structure
  1719. * since we know SOF interrupt will handle future frames.
  1720. */
  1721. if (dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number))
  1722. list_move_tail(&qh->qh_list_entry,
  1723. &hsotg->periodic_sched_ready);
  1724. else
  1725. list_move_tail(&qh->qh_list_entry,
  1726. &hsotg->periodic_sched_inactive);
  1727. }
  1728. /**
  1729. * dwc2_hcd_qtd_init() - Initializes a QTD structure
  1730. *
  1731. * @qtd: The QTD to initialize
  1732. * @urb: The associated URB
  1733. */
  1734. void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
  1735. {
  1736. qtd->urb = urb;
  1737. if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
  1738. USB_ENDPOINT_XFER_CONTROL) {
  1739. /*
  1740. * The only time the QTD data toggle is used is on the data
  1741. * phase of control transfers. This phase always starts with
  1742. * DATA1.
  1743. */
  1744. qtd->data_toggle = DWC2_HC_PID_DATA1;
  1745. qtd->control_phase = DWC2_CONTROL_SETUP;
  1746. }
  1747. /* Start split */
  1748. qtd->complete_split = 0;
  1749. qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
  1750. qtd->isoc_split_offset = 0;
  1751. qtd->in_process = 0;
  1752. /* Store the qtd ptr in the urb to reference the QTD */
  1753. urb->qtd = qtd;
  1754. }
  1755. /**
  1756. * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH
  1757. * Caller must hold driver lock.
  1758. *
  1759. * @hsotg: The DWC HCD structure
  1760. * @qtd: The QTD to add
  1761. * @qh: Queue head to add qtd to
  1762. *
  1763. * Return: 0 if successful, negative error code otherwise
  1764. *
  1765. * If the QH to which the QTD is added is not currently scheduled, it is placed
  1766. * into the proper schedule based on its EP type.
  1767. */
  1768. int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
  1769. struct dwc2_qh *qh)
  1770. {
  1771. int retval;
  1772. if (unlikely(!qh)) {
  1773. dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
  1774. retval = -EINVAL;
  1775. goto fail;
  1776. }
  1777. retval = dwc2_hcd_qh_add(hsotg, qh);
  1778. if (retval)
  1779. goto fail;
  1780. qtd->qh = qh;
  1781. list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
  1782. return 0;
  1783. fail:
  1784. return retval;
  1785. }