xhci-mtk-sch.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 MediaTek Inc.
  4. * Author:
  5. * Zhigang.Wei <zhigang.wei@mediatek.com>
  6. * Chunfeng.Yun <chunfeng.yun@mediatek.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include "xhci.h"
  12. #include "xhci-mtk.h"
  13. #define SSP_BW_BOUNDARY 130000
  14. #define SS_BW_BOUNDARY 51000
  15. /* table 5-5. High-speed Isoc Transaction Limits in usb_20 spec */
  16. #define HS_BW_BOUNDARY 6144
  17. /* usb2 spec section11.18.1: at most 188 FS bytes per microframe */
  18. #define FS_PAYLOAD_MAX 188
  19. #define LS_PAYLOAD_MAX 18
  20. /* section 11.18.1, per fs frame */
  21. #define FS_BW_BOUNDARY 1157
  22. #define LS_BW_BOUNDARY 144
  23. /*
  24. * max number of microframes for split transfer, assume extra-cs budget is 0
  25. * for fs isoc in : 1 ss + 1 idle + 6 cs (roundup(1023/188))
  26. */
  27. #define TT_MICROFRAMES_MAX 8
  28. /* offset from SS for fs/ls isoc/intr ep (ss + idle) */
  29. #define CS_OFFSET 2
  30. #define DBG_BUF_EN 64
  31. /* schedule error type */
  32. #define ESCH_SS_Y6 1001
  33. #define ESCH_SS_OVERLAP 1002
  34. #define ESCH_CS_OVERFLOW 1003
  35. #define ESCH_BW_OVERFLOW 1004
  36. #define ESCH_FIXME 1005
  37. /* mtk scheduler bitmasks */
  38. #define EP_BPKTS(p) ((p) & 0x7f)
  39. #define EP_BCSCOUNT(p) (((p) & 0x7) << 8)
  40. #define EP_BBM(p) ((p) << 11)
  41. #define EP_BOFFSET(p) ((p) & 0x3fff)
  42. #define EP_BREPEAT(p) (((p) & 0x7fff) << 16)
  43. static char *sch_error_string(int err_num)
  44. {
  45. switch (err_num) {
  46. case ESCH_SS_Y6:
  47. return "Can't schedule Start-Split in Y6";
  48. case ESCH_SS_OVERLAP:
  49. return "Can't find a suitable Start-Split location";
  50. case ESCH_CS_OVERFLOW:
  51. return "The last Complete-Split is greater than 7";
  52. case ESCH_BW_OVERFLOW:
  53. return "Bandwidth exceeds the maximum limit";
  54. case ESCH_FIXME:
  55. return "FIXME, to be resolved";
  56. default:
  57. return "Unknown";
  58. }
  59. }
  60. static int is_fs_or_ls(enum usb_device_speed speed)
  61. {
  62. return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
  63. }
  64. static const char *
  65. decode_ep(struct usb_host_endpoint *ep, enum usb_device_speed speed)
  66. {
  67. static char buf[DBG_BUF_EN];
  68. struct usb_endpoint_descriptor *epd = &ep->desc;
  69. unsigned int interval;
  70. const char *unit;
  71. interval = usb_decode_interval(epd, speed);
  72. if (interval % 1000) {
  73. unit = "us";
  74. } else {
  75. unit = "ms";
  76. interval /= 1000;
  77. }
  78. snprintf(buf, DBG_BUF_EN, "%s ep%d%s %s, mpkt:%d, interval:%d/%d%s",
  79. usb_speed_string(speed), usb_endpoint_num(epd),
  80. usb_endpoint_dir_in(epd) ? "in" : "out",
  81. usb_ep_type_string(usb_endpoint_type(epd)),
  82. usb_endpoint_maxp(epd), epd->bInterval, interval, unit);
  83. return buf;
  84. }
  85. static u32 get_bw_boundary(enum usb_device_speed speed)
  86. {
  87. u32 boundary;
  88. switch (speed) {
  89. case USB_SPEED_SUPER_PLUS:
  90. boundary = SSP_BW_BOUNDARY;
  91. break;
  92. case USB_SPEED_SUPER:
  93. boundary = SS_BW_BOUNDARY;
  94. break;
  95. default:
  96. boundary = HS_BW_BOUNDARY;
  97. break;
  98. }
  99. return boundary;
  100. }
  101. /*
  102. * get the bandwidth domain which @ep belongs to.
  103. *
  104. * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
  105. * each HS root port is treated as a single bandwidth domain,
  106. * but each SS root port is treated as two bandwidth domains, one for IN eps,
  107. * one for OUT eps.
  108. */
  109. static struct mu3h_sch_bw_info *
  110. get_bw_info(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
  111. struct usb_host_endpoint *ep)
  112. {
  113. struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
  114. struct xhci_virt_device *virt_dev;
  115. int bw_index;
  116. virt_dev = xhci->devs[udev->slot_id];
  117. if (!virt_dev->rhub_port) {
  118. WARN_ONCE(1, "%s invalid rhub port\n", dev_name(&udev->dev));
  119. return NULL;
  120. }
  121. if (udev->speed >= USB_SPEED_SUPER) {
  122. if (usb_endpoint_dir_out(&ep->desc))
  123. bw_index = (virt_dev->rhub_port->hw_portnum) * 2;
  124. else
  125. bw_index = (virt_dev->rhub_port->hw_portnum) * 2 + 1;
  126. } else {
  127. /* add one more for each SS port */
  128. bw_index = virt_dev->rhub_port->hw_portnum + xhci->usb3_rhub.num_ports;
  129. }
  130. return &mtk->sch_array[bw_index];
  131. }
  132. static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
  133. {
  134. u32 esit;
  135. esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
  136. if (esit > XHCI_MTK_MAX_ESIT)
  137. esit = XHCI_MTK_MAX_ESIT;
  138. return esit;
  139. }
  140. static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
  141. {
  142. struct usb_tt *utt = udev->tt;
  143. struct mu3h_sch_tt *tt, **tt_index, **ptt;
  144. bool allocated_index = false;
  145. if (!utt)
  146. return NULL; /* Not below a TT */
  147. /*
  148. * Find/create our data structure.
  149. * For hubs with a single TT, we get it directly.
  150. * For hubs with multiple TTs, there's an extra level of pointers.
  151. */
  152. tt_index = NULL;
  153. if (utt->multi) {
  154. tt_index = utt->hcpriv;
  155. if (!tt_index) { /* Create the index array */
  156. tt_index = kcalloc(utt->hub->maxchild,
  157. sizeof(*tt_index), GFP_KERNEL);
  158. if (!tt_index)
  159. return ERR_PTR(-ENOMEM);
  160. utt->hcpriv = tt_index;
  161. allocated_index = true;
  162. }
  163. ptt = &tt_index[udev->ttport - 1];
  164. } else {
  165. ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
  166. }
  167. tt = *ptt;
  168. if (!tt) { /* Create the mu3h_sch_tt */
  169. tt = kzalloc(sizeof(*tt), GFP_KERNEL);
  170. if (!tt) {
  171. if (allocated_index) {
  172. utt->hcpriv = NULL;
  173. kfree(tt_index);
  174. }
  175. return ERR_PTR(-ENOMEM);
  176. }
  177. INIT_LIST_HEAD(&tt->ep_list);
  178. *ptt = tt;
  179. }
  180. return tt;
  181. }
  182. /* Release the TT above udev, if it's not in use */
  183. static void drop_tt(struct usb_device *udev)
  184. {
  185. struct usb_tt *utt = udev->tt;
  186. struct mu3h_sch_tt *tt, **tt_index, **ptt;
  187. int i, cnt;
  188. if (!utt || !utt->hcpriv)
  189. return; /* Not below a TT, or never allocated */
  190. cnt = 0;
  191. if (utt->multi) {
  192. tt_index = utt->hcpriv;
  193. ptt = &tt_index[udev->ttport - 1];
  194. /* How many entries are left in tt_index? */
  195. for (i = 0; i < utt->hub->maxchild; ++i)
  196. cnt += !!tt_index[i];
  197. } else {
  198. tt_index = NULL;
  199. ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
  200. }
  201. tt = *ptt;
  202. if (!tt || !list_empty(&tt->ep_list))
  203. return; /* never allocated , or still in use*/
  204. *ptt = NULL;
  205. kfree(tt);
  206. if (cnt == 1) {
  207. utt->hcpriv = NULL;
  208. kfree(tt_index);
  209. }
  210. }
  211. static struct mu3h_sch_ep_info *
  212. create_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
  213. struct usb_host_endpoint *ep, struct xhci_ep_ctx *ep_ctx)
  214. {
  215. struct mu3h_sch_ep_info *sch_ep;
  216. struct mu3h_sch_bw_info *bw_info;
  217. struct mu3h_sch_tt *tt = NULL;
  218. u32 len;
  219. bw_info = get_bw_info(mtk, udev, ep);
  220. if (!bw_info)
  221. return ERR_PTR(-ENODEV);
  222. if (is_fs_or_ls(udev->speed))
  223. len = TT_MICROFRAMES_MAX;
  224. else if ((udev->speed >= USB_SPEED_SUPER) &&
  225. usb_endpoint_xfer_isoc(&ep->desc))
  226. len = get_esit(ep_ctx);
  227. else
  228. len = 1;
  229. sch_ep = kzalloc(struct_size(sch_ep, bw_budget_table, len), GFP_KERNEL);
  230. if (!sch_ep)
  231. return ERR_PTR(-ENOMEM);
  232. if (is_fs_or_ls(udev->speed)) {
  233. tt = find_tt(udev);
  234. if (IS_ERR(tt)) {
  235. kfree(sch_ep);
  236. return ERR_PTR(-ENOMEM);
  237. }
  238. }
  239. sch_ep->bw_info = bw_info;
  240. sch_ep->sch_tt = tt;
  241. sch_ep->ep = ep;
  242. sch_ep->speed = udev->speed;
  243. INIT_LIST_HEAD(&sch_ep->endpoint);
  244. INIT_LIST_HEAD(&sch_ep->tt_endpoint);
  245. INIT_HLIST_NODE(&sch_ep->hentry);
  246. return sch_ep;
  247. }
  248. static void setup_sch_info(struct xhci_ep_ctx *ep_ctx,
  249. struct mu3h_sch_ep_info *sch_ep)
  250. {
  251. u32 ep_type;
  252. u32 maxpkt;
  253. u32 max_burst;
  254. u32 mult;
  255. u32 esit_pkts;
  256. u32 max_esit_payload;
  257. u32 bw_per_microframe;
  258. u32 *bwb_table;
  259. int i;
  260. bwb_table = sch_ep->bw_budget_table;
  261. ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
  262. maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  263. max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
  264. mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
  265. max_esit_payload =
  266. (CTX_TO_MAX_ESIT_PAYLOAD_HI(
  267. le32_to_cpu(ep_ctx->ep_info)) << 16) |
  268. CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
  269. sch_ep->esit = get_esit(ep_ctx);
  270. sch_ep->num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
  271. sch_ep->ep_type = ep_type;
  272. sch_ep->maxpkt = maxpkt;
  273. sch_ep->offset = 0;
  274. sch_ep->burst_mode = 0;
  275. sch_ep->repeat = 0;
  276. if (sch_ep->speed == USB_SPEED_HIGH) {
  277. sch_ep->cs_count = 0;
  278. /*
  279. * usb_20 spec section5.9
  280. * a single microframe is enough for HS synchromous endpoints
  281. * in a interval
  282. */
  283. sch_ep->num_budget_microframes = 1;
  284. /*
  285. * xHCI spec section6.2.3.4
  286. * @max_burst is the number of additional transactions
  287. * opportunities per microframe
  288. */
  289. sch_ep->pkts = max_burst + 1;
  290. bwb_table[0] = maxpkt * sch_ep->pkts;
  291. } else if (sch_ep->speed >= USB_SPEED_SUPER) {
  292. /* usb3_r1 spec section4.4.7 & 4.4.8 */
  293. sch_ep->cs_count = 0;
  294. sch_ep->burst_mode = 1;
  295. /*
  296. * some device's (d)wBytesPerInterval is set as 0,
  297. * then max_esit_payload is 0, so evaluate esit_pkts from
  298. * mult and burst
  299. */
  300. esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
  301. if (esit_pkts == 0)
  302. esit_pkts = (mult + 1) * (max_burst + 1);
  303. if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
  304. sch_ep->pkts = esit_pkts;
  305. sch_ep->num_budget_microframes = 1;
  306. bwb_table[0] = maxpkt * sch_ep->pkts;
  307. }
  308. if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
  309. if (sch_ep->esit == 1)
  310. sch_ep->pkts = esit_pkts;
  311. else if (esit_pkts <= sch_ep->esit)
  312. sch_ep->pkts = 1;
  313. else
  314. sch_ep->pkts = roundup_pow_of_two(esit_pkts)
  315. / sch_ep->esit;
  316. sch_ep->num_budget_microframes =
  317. DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
  318. sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
  319. bw_per_microframe = maxpkt * sch_ep->pkts;
  320. for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
  321. bwb_table[i] = bw_per_microframe;
  322. /* last one <= bw_per_microframe */
  323. bwb_table[i] = maxpkt * esit_pkts - i * bw_per_microframe;
  324. }
  325. } else if (is_fs_or_ls(sch_ep->speed)) {
  326. sch_ep->pkts = 1; /* at most one packet for each microframe */
  327. /*
  328. * @cs_count will be updated to add extra-cs when
  329. * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
  330. * @maxpkt <= 1023;
  331. */
  332. sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
  333. sch_ep->num_budget_microframes = sch_ep->cs_count;
  334. /* init budget table */
  335. if (ep_type == ISOC_OUT_EP) {
  336. for (i = 0; i < sch_ep->cs_count - 1; i++)
  337. bwb_table[i] = FS_PAYLOAD_MAX;
  338. bwb_table[i] = maxpkt - i * FS_PAYLOAD_MAX;
  339. } else if (ep_type == INT_OUT_EP) {
  340. /* only first one used (maxpkt <= 64), others zero */
  341. bwb_table[0] = maxpkt;
  342. } else { /* INT_IN_EP or ISOC_IN_EP */
  343. bwb_table[0] = 0; /* start split */
  344. bwb_table[1] = 0; /* idle */
  345. /*
  346. * @cs_count will be updated according to cs position
  347. * (add 1 or 2 extra-cs), but assume only first
  348. * @num_budget_microframes elements will be used later,
  349. * although in fact it does not (extra-cs budget many receive
  350. * some data for IN ep);
  351. * @cs_count is 1 for INT_IN_EP (maxpkt <= 64);
  352. */
  353. for (i = 0; i < sch_ep->cs_count - 1; i++)
  354. bwb_table[i + CS_OFFSET] = FS_PAYLOAD_MAX;
  355. bwb_table[i + CS_OFFSET] = maxpkt - i * FS_PAYLOAD_MAX;
  356. /* ss + idle */
  357. sch_ep->num_budget_microframes += CS_OFFSET;
  358. }
  359. }
  360. }
  361. /* Get maximum bandwidth when we schedule at offset slot. */
  362. static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
  363. struct mu3h_sch_ep_info *sch_ep, u32 offset)
  364. {
  365. u32 max_bw = 0;
  366. u32 bw;
  367. int i, j, k;
  368. for (i = 0; i < sch_ep->num_esit; i++) {
  369. u32 base = offset + i * sch_ep->esit;
  370. for (j = 0; j < sch_ep->num_budget_microframes; j++) {
  371. k = XHCI_MTK_BW_INDEX(base + j);
  372. bw = sch_bw->bus_bw[k] + sch_ep->bw_budget_table[j];
  373. if (bw > max_bw)
  374. max_bw = bw;
  375. }
  376. }
  377. return max_bw;
  378. }
  379. /*
  380. * for OUT: get first SS consumed bw;
  381. * for IN: get first CS consumed bw;
  382. */
  383. static u16 get_fs_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
  384. {
  385. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  386. u16 fs_bw;
  387. if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
  388. fs_bw = tt->fs_bus_bw_out[XHCI_MTK_BW_INDEX(offset)];
  389. else /* skip ss + idle */
  390. fs_bw = tt->fs_bus_bw_in[XHCI_MTK_BW_INDEX(offset + CS_OFFSET)];
  391. return fs_bw;
  392. }
  393. static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
  394. struct mu3h_sch_ep_info *sch_ep, bool used)
  395. {
  396. u32 base;
  397. int i, j, k;
  398. for (i = 0; i < sch_ep->num_esit; i++) {
  399. base = sch_ep->offset + i * sch_ep->esit;
  400. for (j = 0; j < sch_ep->num_budget_microframes; j++) {
  401. k = XHCI_MTK_BW_INDEX(base + j);
  402. if (used)
  403. sch_bw->bus_bw[k] += sch_ep->bw_budget_table[j];
  404. else
  405. sch_bw->bus_bw[k] -= sch_ep->bw_budget_table[j];
  406. }
  407. }
  408. }
  409. static int check_ls_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
  410. {
  411. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  412. int i;
  413. if (sch_ep->speed != USB_SPEED_LOW)
  414. return 0;
  415. if (sch_ep->ep_type == INT_OUT_EP)
  416. i = XHCI_MTK_BW_INDEX(offset);
  417. else if (sch_ep->ep_type == INT_IN_EP)
  418. i = XHCI_MTK_BW_INDEX(offset + CS_OFFSET); /* skip ss + idle */
  419. else
  420. return -EINVAL;
  421. if (tt->ls_bus_bw[i] + sch_ep->maxpkt > LS_PAYLOAD_MAX)
  422. return -ESCH_BW_OVERFLOW;
  423. return 0;
  424. }
  425. static int check_fs_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
  426. {
  427. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  428. u32 tmp;
  429. int i, k;
  430. /*
  431. * for OUT eps, will transfer exactly assigned length of data,
  432. * so can't allocate more than 188 bytes;
  433. * but it's not for IN eps, usually it can't receive full
  434. * 188 bytes in a uframe, if it not assign full 188 bytes,
  435. * can add another one;
  436. */
  437. for (i = 0; i < sch_ep->num_budget_microframes; i++) {
  438. k = XHCI_MTK_BW_INDEX(offset + i);
  439. if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
  440. tmp = tt->fs_bus_bw_out[k] + sch_ep->bw_budget_table[i];
  441. else /* ep_type : ISOC IN / INTR IN */
  442. tmp = tt->fs_bus_bw_in[k];
  443. if (tmp > FS_PAYLOAD_MAX)
  444. return -ESCH_BW_OVERFLOW;
  445. }
  446. return 0;
  447. }
  448. static int check_fs_budget_frames(struct mu3h_sch_ep_info *sch_ep, int offset)
  449. {
  450. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  451. u32 head, tail;
  452. int i, j, k;
  453. /* bugdet scheduled may cross at most two fs frames */
  454. j = XHCI_MTK_BW_INDEX(offset) / UFRAMES_PER_FRAME;
  455. k = XHCI_MTK_BW_INDEX(offset + sch_ep->num_budget_microframes - 1) / UFRAMES_PER_FRAME;
  456. if (j != k) {
  457. head = tt->fs_frame_bw[j];
  458. tail = tt->fs_frame_bw[k];
  459. } else {
  460. head = tt->fs_frame_bw[j];
  461. tail = 0;
  462. }
  463. j = roundup(offset, UFRAMES_PER_FRAME);
  464. for (i = 0; i < sch_ep->num_budget_microframes; i++) {
  465. if ((offset + i) < j)
  466. head += sch_ep->bw_budget_table[i];
  467. else
  468. tail += sch_ep->bw_budget_table[i];
  469. }
  470. if (head > FS_BW_BOUNDARY || tail > FS_BW_BOUNDARY)
  471. return -ESCH_BW_OVERFLOW;
  472. return 0;
  473. }
  474. static int check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
  475. {
  476. int i, base;
  477. int ret = 0;
  478. for (i = 0; i < sch_ep->num_esit; i++) {
  479. base = offset + i * sch_ep->esit;
  480. ret = check_ls_budget_microframes(sch_ep, base);
  481. if (ret)
  482. goto err;
  483. ret = check_fs_budget_microframes(sch_ep, base);
  484. if (ret)
  485. goto err;
  486. ret = check_fs_budget_frames(sch_ep, base);
  487. if (ret)
  488. goto err;
  489. }
  490. err:
  491. return ret;
  492. }
  493. static int check_ss_and_cs(struct mu3h_sch_ep_info *sch_ep, u32 offset)
  494. {
  495. u32 start_ss, last_ss;
  496. u32 start_cs, last_cs;
  497. start_ss = offset % UFRAMES_PER_FRAME;
  498. if (sch_ep->ep_type == ISOC_OUT_EP) {
  499. last_ss = start_ss + sch_ep->cs_count - 1;
  500. /*
  501. * usb_20 spec section11.18:
  502. * must never schedule Start-Split in Y6
  503. */
  504. if (!(start_ss == 7 || last_ss < 6))
  505. return -ESCH_SS_Y6;
  506. } else {
  507. /* maxpkt <= 1023, cs <= 6 */
  508. u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
  509. /*
  510. * usb_20 spec section11.18:
  511. * must never schedule Start-Split in Y6
  512. */
  513. if (start_ss == 6)
  514. return -ESCH_SS_Y6;
  515. /* one uframe for ss + one uframe for idle */
  516. start_cs = (start_ss + CS_OFFSET) % UFRAMES_PER_FRAME;
  517. last_cs = start_cs + cs_count - 1;
  518. if (last_cs > 7)
  519. return -ESCH_CS_OVERFLOW;
  520. /* add extra-cs */
  521. cs_count += (last_cs == 7) ? 1 : 2;
  522. if (cs_count > 7)
  523. cs_count = 7; /* HW limit */
  524. sch_ep->cs_count = cs_count;
  525. }
  526. return 0;
  527. }
  528. /*
  529. * when isoc-out transfers 188 bytes in a uframe, and send isoc/intr's
  530. * ss token in the uframe, may cause 'bit stuff error' in downstream
  531. * port;
  532. * when isoc-out transfer less than 188 bytes in a uframe, shall send
  533. * isoc-in's ss after isoc-out's ss (but hw can't ensure the sequence,
  534. * so just avoid overlap).
  535. */
  536. static int check_isoc_ss_overlap(struct mu3h_sch_ep_info *sch_ep, u32 offset)
  537. {
  538. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  539. int base;
  540. int i, j, k;
  541. if (!tt)
  542. return 0;
  543. for (i = 0; i < sch_ep->num_esit; i++) {
  544. base = offset + i * sch_ep->esit;
  545. if (sch_ep->ep_type == ISOC_OUT_EP) {
  546. for (j = 0; j < sch_ep->num_budget_microframes; j++) {
  547. k = XHCI_MTK_BW_INDEX(base + j);
  548. if (tt->in_ss_cnt[k])
  549. return -ESCH_SS_OVERLAP;
  550. }
  551. } else if (sch_ep->ep_type == ISOC_IN_EP || sch_ep->ep_type == INT_IN_EP) {
  552. k = XHCI_MTK_BW_INDEX(base);
  553. /* only check IN's ss */
  554. if (tt->fs_bus_bw_out[k])
  555. return -ESCH_SS_OVERLAP;
  556. }
  557. }
  558. return 0;
  559. }
  560. static int check_sch_tt_budget(struct mu3h_sch_ep_info *sch_ep, u32 offset)
  561. {
  562. int ret;
  563. ret = check_ss_and_cs(sch_ep, offset);
  564. if (ret)
  565. return ret;
  566. ret = check_isoc_ss_overlap(sch_ep, offset);
  567. if (ret)
  568. return ret;
  569. return check_fs_bus_bw(sch_ep, offset);
  570. }
  571. /* allocate microframes in the ls/fs frame */
  572. static int alloc_sch_portion_of_frame(struct mu3h_sch_ep_info *sch_ep)
  573. {
  574. struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
  575. const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
  576. u32 bw_max, fs_bw_min;
  577. u32 offset, offset_min;
  578. u16 fs_bw;
  579. int frames;
  580. int i, j;
  581. int ret;
  582. frames = sch_ep->esit / UFRAMES_PER_FRAME;
  583. for (i = 0; i < UFRAMES_PER_FRAME; i++) {
  584. fs_bw_min = FS_PAYLOAD_MAX;
  585. offset_min = XHCI_MTK_MAX_ESIT;
  586. for (j = 0; j < frames; j++) {
  587. offset = (i + j * UFRAMES_PER_FRAME) % sch_ep->esit;
  588. ret = check_sch_tt_budget(sch_ep, offset);
  589. if (ret)
  590. continue;
  591. /* check hs bw domain */
  592. bw_max = get_max_bw(sch_bw, sch_ep, offset);
  593. if (bw_max > bw_boundary) {
  594. ret = -ESCH_BW_OVERFLOW;
  595. continue;
  596. }
  597. /* use best-fit between frames */
  598. fs_bw = get_fs_bw(sch_ep, offset);
  599. if (fs_bw < fs_bw_min) {
  600. fs_bw_min = fs_bw;
  601. offset_min = offset;
  602. }
  603. if (!fs_bw_min)
  604. break;
  605. }
  606. /* use first-fit between microframes in a frame */
  607. if (offset_min < XHCI_MTK_MAX_ESIT)
  608. break;
  609. }
  610. if (offset_min == XHCI_MTK_MAX_ESIT)
  611. return -ESCH_BW_OVERFLOW;
  612. sch_ep->offset = offset_min;
  613. return 0;
  614. }
  615. static void update_sch_tt(struct mu3h_sch_ep_info *sch_ep, bool used)
  616. {
  617. struct mu3h_sch_tt *tt = sch_ep->sch_tt;
  618. u16 *fs_bus_bw;
  619. u32 base;
  620. int i, j, k, f;
  621. if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
  622. fs_bus_bw = tt->fs_bus_bw_out;
  623. else
  624. fs_bus_bw = tt->fs_bus_bw_in;
  625. for (i = 0; i < sch_ep->num_esit; i++) {
  626. base = sch_ep->offset + i * sch_ep->esit;
  627. for (j = 0; j < sch_ep->num_budget_microframes; j++) {
  628. k = XHCI_MTK_BW_INDEX(base + j);
  629. f = k / UFRAMES_PER_FRAME;
  630. if (used) {
  631. if (sch_ep->speed == USB_SPEED_LOW)
  632. tt->ls_bus_bw[k] += (u8)sch_ep->bw_budget_table[j];
  633. fs_bus_bw[k] += (u16)sch_ep->bw_budget_table[j];
  634. tt->fs_frame_bw[f] += (u16)sch_ep->bw_budget_table[j];
  635. } else {
  636. if (sch_ep->speed == USB_SPEED_LOW)
  637. tt->ls_bus_bw[k] -= (u8)sch_ep->bw_budget_table[j];
  638. fs_bus_bw[k] -= (u16)sch_ep->bw_budget_table[j];
  639. tt->fs_frame_bw[f] -= (u16)sch_ep->bw_budget_table[j];
  640. }
  641. }
  642. if (sch_ep->ep_type == ISOC_IN_EP || sch_ep->ep_type == INT_IN_EP) {
  643. k = XHCI_MTK_BW_INDEX(base);
  644. if (used)
  645. tt->in_ss_cnt[k]++;
  646. else
  647. tt->in_ss_cnt[k]--;
  648. }
  649. }
  650. if (used)
  651. list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
  652. else
  653. list_del(&sch_ep->tt_endpoint);
  654. }
  655. static int load_ep_bw(struct mu3h_sch_bw_info *sch_bw,
  656. struct mu3h_sch_ep_info *sch_ep, bool loaded)
  657. {
  658. if (sch_ep->sch_tt)
  659. update_sch_tt(sch_ep, loaded);
  660. /* update bus bandwidth info */
  661. update_bus_bw(sch_bw, sch_ep, loaded);
  662. sch_ep->allocated = loaded;
  663. return 0;
  664. }
  665. /* allocate microframes for hs/ss/ssp */
  666. static int alloc_sch_microframes(struct mu3h_sch_ep_info *sch_ep)
  667. {
  668. struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
  669. const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
  670. u32 offset;
  671. u32 worst_bw;
  672. u32 min_bw = ~0;
  673. int min_index = -1;
  674. /*
  675. * Search through all possible schedule microframes.
  676. * and find a microframe where its worst bandwidth is minimum.
  677. */
  678. for (offset = 0; offset < sch_ep->esit; offset++) {
  679. worst_bw = get_max_bw(sch_bw, sch_ep, offset);
  680. if (worst_bw > bw_boundary)
  681. continue;
  682. if (min_bw > worst_bw) {
  683. min_bw = worst_bw;
  684. min_index = offset;
  685. }
  686. }
  687. if (min_index < 0)
  688. return -ESCH_BW_OVERFLOW;
  689. sch_ep->offset = min_index;
  690. return 0;
  691. }
  692. static int check_sch_bw(struct mu3h_sch_ep_info *sch_ep)
  693. {
  694. int ret;
  695. if (sch_ep->sch_tt)
  696. ret = alloc_sch_portion_of_frame(sch_ep);
  697. else
  698. ret = alloc_sch_microframes(sch_ep);
  699. if (ret)
  700. return ret;
  701. return load_ep_bw(sch_ep->bw_info, sch_ep, true);
  702. }
  703. static void destroy_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
  704. struct mu3h_sch_ep_info *sch_ep)
  705. {
  706. /* only release ep bw check passed by check_sch_bw() */
  707. if (sch_ep->allocated)
  708. load_ep_bw(sch_ep->bw_info, sch_ep, false);
  709. if (sch_ep->sch_tt)
  710. drop_tt(udev);
  711. list_del(&sch_ep->endpoint);
  712. hlist_del(&sch_ep->hentry);
  713. kfree(sch_ep);
  714. }
  715. static bool need_bw_sch(struct usb_device *udev,
  716. struct usb_host_endpoint *ep)
  717. {
  718. bool has_tt = udev->tt && udev->tt->hub->parent;
  719. /* only for periodic endpoints */
  720. if (usb_endpoint_xfer_control(&ep->desc)
  721. || usb_endpoint_xfer_bulk(&ep->desc))
  722. return false;
  723. /*
  724. * for LS & FS periodic endpoints which its device is not behind
  725. * a TT are also ignored, root-hub will schedule them directly,
  726. * but need set @bpkts field of endpoint context to 1.
  727. */
  728. if (is_fs_or_ls(udev->speed) && !has_tt)
  729. return false;
  730. /* skip endpoint with zero maxpkt */
  731. if (usb_endpoint_maxp(&ep->desc) == 0)
  732. return false;
  733. return true;
  734. }
  735. int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
  736. {
  737. struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
  738. struct mu3h_sch_bw_info *sch_array;
  739. int num_usb_bus;
  740. /* ss IN and OUT are separated */
  741. num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
  742. sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
  743. if (sch_array == NULL)
  744. return -ENOMEM;
  745. mtk->sch_array = sch_array;
  746. INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
  747. hash_init(mtk->sch_ep_hash);
  748. return 0;
  749. }
  750. void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
  751. {
  752. kfree(mtk->sch_array);
  753. }
  754. static int add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
  755. struct usb_host_endpoint *ep)
  756. {
  757. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  758. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  759. struct xhci_ep_ctx *ep_ctx;
  760. struct xhci_virt_device *virt_dev;
  761. struct mu3h_sch_ep_info *sch_ep;
  762. unsigned int ep_index;
  763. virt_dev = xhci->devs[udev->slot_id];
  764. ep_index = xhci_get_endpoint_index(&ep->desc);
  765. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  766. if (!need_bw_sch(udev, ep)) {
  767. /*
  768. * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
  769. * device does not connected through an external HS hub
  770. */
  771. if (usb_endpoint_xfer_int(&ep->desc)
  772. || usb_endpoint_xfer_isoc(&ep->desc))
  773. ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
  774. return 0;
  775. }
  776. xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
  777. sch_ep = create_sch_ep(mtk, udev, ep, ep_ctx);
  778. if (IS_ERR_OR_NULL(sch_ep))
  779. return -ENOMEM;
  780. setup_sch_info(ep_ctx, sch_ep);
  781. list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
  782. hash_add(mtk->sch_ep_hash, &sch_ep->hentry, (unsigned long)ep);
  783. return 0;
  784. }
  785. static void drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
  786. struct usb_host_endpoint *ep)
  787. {
  788. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  789. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  790. struct mu3h_sch_ep_info *sch_ep;
  791. struct hlist_node *hn;
  792. if (!need_bw_sch(udev, ep))
  793. return;
  794. xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
  795. hash_for_each_possible_safe(mtk->sch_ep_hash, sch_ep,
  796. hn, hentry, (unsigned long)ep) {
  797. if (sch_ep->ep == ep) {
  798. destroy_sch_ep(mtk, udev, sch_ep);
  799. break;
  800. }
  801. }
  802. }
  803. int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  804. {
  805. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  806. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  807. struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
  808. struct mu3h_sch_ep_info *sch_ep;
  809. int ret;
  810. xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
  811. list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
  812. struct xhci_ep_ctx *ep_ctx;
  813. struct usb_host_endpoint *ep = sch_ep->ep;
  814. unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
  815. ret = check_sch_bw(sch_ep);
  816. if (ret) {
  817. xhci_err(xhci, "Not enough bandwidth! (%s)\n",
  818. sch_error_string(-ret));
  819. return -ENOSPC;
  820. }
  821. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  822. ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(sch_ep->pkts)
  823. | EP_BCSCOUNT(sch_ep->cs_count)
  824. | EP_BBM(sch_ep->burst_mode));
  825. ep_ctx->reserved[1] = cpu_to_le32(EP_BOFFSET(sch_ep->offset)
  826. | EP_BREPEAT(sch_ep->repeat));
  827. xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
  828. sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
  829. sch_ep->offset, sch_ep->repeat);
  830. }
  831. ret = xhci_check_bandwidth(hcd, udev);
  832. if (!ret)
  833. list_del_init(&mtk->bw_ep_chk_list);
  834. return ret;
  835. }
  836. void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  837. {
  838. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  839. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  840. struct mu3h_sch_ep_info *sch_ep, *tmp;
  841. xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
  842. list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint)
  843. destroy_sch_ep(mtk, udev, sch_ep);
  844. xhci_reset_bandwidth(hcd, udev);
  845. }
  846. int xhci_mtk_add_ep(struct usb_hcd *hcd, struct usb_device *udev,
  847. struct usb_host_endpoint *ep)
  848. {
  849. int ret;
  850. ret = xhci_add_endpoint(hcd, udev, ep);
  851. if (ret)
  852. return ret;
  853. if (ep->hcpriv)
  854. ret = add_ep_quirk(hcd, udev, ep);
  855. return ret;
  856. }
  857. int xhci_mtk_drop_ep(struct usb_hcd *hcd, struct usb_device *udev,
  858. struct usb_host_endpoint *ep)
  859. {
  860. int ret;
  861. ret = xhci_drop_endpoint(hcd, udev, ep);
  862. if (ret)
  863. return ret;
  864. /* needn't check @ep->hcpriv, xhci_endpoint_disable set it NULL */
  865. drop_ep_quirk(hcd, udev, ep);
  866. return 0;
  867. }