hcd_queue.c 62 KB

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