hcd_queue.c 62 KB

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