hcd_queue.c 62 KB

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