hcd_queue.c 63 KB

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