hcd_queue.c 62 KB

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