hcd_queue.c 62 KB

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