xhci-mtk-sch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 SS_BW_BOUNDARY 51000
  14. /* table 5-5. High-speed Isoc Transaction Limits in usb_20 spec */
  15. #define HS_BW_BOUNDARY 6144
  16. /* usb2 spec section11.18.1: at most 188 FS bytes per microframe */
  17. #define FS_PAYLOAD_MAX 188
  18. /* mtk scheduler bitmasks */
  19. #define EP_BPKTS(p) ((p) & 0x3f)
  20. #define EP_BCSCOUNT(p) (((p) & 0x7) << 8)
  21. #define EP_BBM(p) ((p) << 11)
  22. #define EP_BOFFSET(p) ((p) & 0x3fff)
  23. #define EP_BREPEAT(p) (((p) & 0x7fff) << 16)
  24. static int is_fs_or_ls(enum usb_device_speed speed)
  25. {
  26. return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
  27. }
  28. /*
  29. * get the index of bandwidth domains array which @ep belongs to.
  30. *
  31. * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
  32. * each HS root port is treated as a single bandwidth domain,
  33. * but each SS root port is treated as two bandwidth domains, one for IN eps,
  34. * one for OUT eps.
  35. * @real_port value is defined as follow according to xHCI spec:
  36. * 1 for SSport0, ..., N+1 for SSportN, N+2 for HSport0, N+3 for HSport1, etc
  37. * so the bandwidth domain array is organized as follow for simplification:
  38. * SSport0-OUT, SSport0-IN, ..., SSportX-OUT, SSportX-IN, HSport0, ..., HSportY
  39. */
  40. static int get_bw_index(struct xhci_hcd *xhci, struct usb_device *udev,
  41. struct usb_host_endpoint *ep)
  42. {
  43. struct xhci_virt_device *virt_dev;
  44. int bw_index;
  45. virt_dev = xhci->devs[udev->slot_id];
  46. if (udev->speed == USB_SPEED_SUPER) {
  47. if (usb_endpoint_dir_out(&ep->desc))
  48. bw_index = (virt_dev->real_port - 1) * 2;
  49. else
  50. bw_index = (virt_dev->real_port - 1) * 2 + 1;
  51. } else {
  52. /* add one more for each SS port */
  53. bw_index = virt_dev->real_port + xhci->usb3_rhub.num_ports - 1;
  54. }
  55. return bw_index;
  56. }
  57. static void setup_sch_info(struct usb_device *udev,
  58. struct xhci_ep_ctx *ep_ctx, struct mu3h_sch_ep_info *sch_ep)
  59. {
  60. u32 ep_type;
  61. u32 ep_interval;
  62. u32 max_packet_size;
  63. u32 max_burst;
  64. u32 mult;
  65. u32 esit_pkts;
  66. ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
  67. ep_interval = CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
  68. max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
  69. max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
  70. mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
  71. sch_ep->esit = 1 << ep_interval;
  72. sch_ep->offset = 0;
  73. sch_ep->burst_mode = 0;
  74. if (udev->speed == USB_SPEED_HIGH) {
  75. sch_ep->cs_count = 0;
  76. /*
  77. * usb_20 spec section5.9
  78. * a single microframe is enough for HS synchromous endpoints
  79. * in a interval
  80. */
  81. sch_ep->num_budget_microframes = 1;
  82. sch_ep->repeat = 0;
  83. /*
  84. * xHCI spec section6.2.3.4
  85. * @max_burst is the number of additional transactions
  86. * opportunities per microframe
  87. */
  88. sch_ep->pkts = max_burst + 1;
  89. sch_ep->bw_cost_per_microframe = max_packet_size * sch_ep->pkts;
  90. } else if (udev->speed == USB_SPEED_SUPER) {
  91. /* usb3_r1 spec section4.4.7 & 4.4.8 */
  92. sch_ep->cs_count = 0;
  93. esit_pkts = (mult + 1) * (max_burst + 1);
  94. if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
  95. sch_ep->pkts = esit_pkts;
  96. sch_ep->num_budget_microframes = 1;
  97. sch_ep->repeat = 0;
  98. }
  99. if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
  100. if (sch_ep->esit == 1)
  101. sch_ep->pkts = esit_pkts;
  102. else if (esit_pkts <= sch_ep->esit)
  103. sch_ep->pkts = 1;
  104. else
  105. sch_ep->pkts = roundup_pow_of_two(esit_pkts)
  106. / sch_ep->esit;
  107. sch_ep->num_budget_microframes =
  108. DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
  109. if (sch_ep->num_budget_microframes > 1)
  110. sch_ep->repeat = 1;
  111. else
  112. sch_ep->repeat = 0;
  113. }
  114. sch_ep->bw_cost_per_microframe = max_packet_size * sch_ep->pkts;
  115. } else if (is_fs_or_ls(udev->speed)) {
  116. /*
  117. * usb_20 spec section11.18.4
  118. * assume worst cases
  119. */
  120. sch_ep->repeat = 0;
  121. sch_ep->pkts = 1; /* at most one packet for each microframe */
  122. if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
  123. sch_ep->cs_count = 3; /* at most need 3 CS*/
  124. /* one for SS and one for budgeted transaction */
  125. sch_ep->num_budget_microframes = sch_ep->cs_count + 2;
  126. sch_ep->bw_cost_per_microframe = max_packet_size;
  127. }
  128. if (ep_type == ISOC_OUT_EP) {
  129. /*
  130. * the best case FS budget assumes that 188 FS bytes
  131. * occur in each microframe
  132. */
  133. sch_ep->num_budget_microframes = DIV_ROUND_UP(
  134. max_packet_size, FS_PAYLOAD_MAX);
  135. sch_ep->bw_cost_per_microframe = FS_PAYLOAD_MAX;
  136. sch_ep->cs_count = sch_ep->num_budget_microframes;
  137. }
  138. if (ep_type == ISOC_IN_EP) {
  139. /* at most need additional two CS. */
  140. sch_ep->cs_count = DIV_ROUND_UP(
  141. max_packet_size, FS_PAYLOAD_MAX) + 2;
  142. sch_ep->num_budget_microframes = sch_ep->cs_count + 2;
  143. sch_ep->bw_cost_per_microframe = FS_PAYLOAD_MAX;
  144. }
  145. }
  146. }
  147. /* Get maximum bandwidth when we schedule at offset slot. */
  148. static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
  149. struct mu3h_sch_ep_info *sch_ep, u32 offset)
  150. {
  151. u32 num_esit;
  152. u32 max_bw = 0;
  153. int i;
  154. int j;
  155. num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
  156. for (i = 0; i < num_esit; i++) {
  157. u32 base = offset + i * sch_ep->esit;
  158. for (j = 0; j < sch_ep->num_budget_microframes; j++) {
  159. if (sch_bw->bus_bw[base + j] > max_bw)
  160. max_bw = sch_bw->bus_bw[base + j];
  161. }
  162. }
  163. return max_bw;
  164. }
  165. static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
  166. struct mu3h_sch_ep_info *sch_ep, int bw_cost)
  167. {
  168. u32 num_esit;
  169. u32 base;
  170. int i;
  171. int j;
  172. num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
  173. for (i = 0; i < num_esit; i++) {
  174. base = sch_ep->offset + i * sch_ep->esit;
  175. for (j = 0; j < sch_ep->num_budget_microframes; j++)
  176. sch_bw->bus_bw[base + j] += bw_cost;
  177. }
  178. }
  179. static int check_sch_bw(struct usb_device *udev,
  180. struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep)
  181. {
  182. u32 offset;
  183. u32 esit;
  184. u32 num_budget_microframes;
  185. u32 min_bw;
  186. u32 min_index;
  187. u32 worst_bw;
  188. u32 bw_boundary;
  189. if (sch_ep->esit > XHCI_MTK_MAX_ESIT)
  190. sch_ep->esit = XHCI_MTK_MAX_ESIT;
  191. esit = sch_ep->esit;
  192. num_budget_microframes = sch_ep->num_budget_microframes;
  193. /*
  194. * Search through all possible schedule microframes.
  195. * and find a microframe where its worst bandwidth is minimum.
  196. */
  197. min_bw = ~0;
  198. min_index = 0;
  199. for (offset = 0; offset < esit; offset++) {
  200. if ((offset + num_budget_microframes) > sch_ep->esit)
  201. break;
  202. /*
  203. * usb_20 spec section11.18:
  204. * must never schedule Start-Split in Y6
  205. */
  206. if (is_fs_or_ls(udev->speed) && (offset % 8 == 6))
  207. continue;
  208. worst_bw = get_max_bw(sch_bw, sch_ep, offset);
  209. if (min_bw > worst_bw) {
  210. min_bw = worst_bw;
  211. min_index = offset;
  212. }
  213. if (min_bw == 0)
  214. break;
  215. }
  216. sch_ep->offset = min_index;
  217. bw_boundary = (udev->speed == USB_SPEED_SUPER)
  218. ? SS_BW_BOUNDARY : HS_BW_BOUNDARY;
  219. /* check bandwidth */
  220. if (min_bw + sch_ep->bw_cost_per_microframe > bw_boundary)
  221. return -ERANGE;
  222. /* update bus bandwidth info */
  223. update_bus_bw(sch_bw, sch_ep, sch_ep->bw_cost_per_microframe);
  224. return 0;
  225. }
  226. static bool need_bw_sch(struct usb_host_endpoint *ep,
  227. enum usb_device_speed speed, int has_tt)
  228. {
  229. /* only for periodic endpoints */
  230. if (usb_endpoint_xfer_control(&ep->desc)
  231. || usb_endpoint_xfer_bulk(&ep->desc))
  232. return false;
  233. /*
  234. * for LS & FS periodic endpoints which its device is not behind
  235. * a TT are also ignored, root-hub will schedule them directly,
  236. * but need set @bpkts field of endpoint context to 1.
  237. */
  238. if (is_fs_or_ls(speed) && !has_tt)
  239. return false;
  240. return true;
  241. }
  242. int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
  243. {
  244. struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
  245. struct mu3h_sch_bw_info *sch_array;
  246. int num_usb_bus;
  247. int i;
  248. /* ss IN and OUT are separated */
  249. num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
  250. sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
  251. if (sch_array == NULL)
  252. return -ENOMEM;
  253. for (i = 0; i < num_usb_bus; i++)
  254. INIT_LIST_HEAD(&sch_array[i].bw_ep_list);
  255. mtk->sch_array = sch_array;
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(xhci_mtk_sch_init);
  259. void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
  260. {
  261. kfree(mtk->sch_array);
  262. }
  263. EXPORT_SYMBOL_GPL(xhci_mtk_sch_exit);
  264. int xhci_mtk_add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
  265. struct usb_host_endpoint *ep)
  266. {
  267. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  268. struct xhci_hcd *xhci;
  269. struct xhci_ep_ctx *ep_ctx;
  270. struct xhci_slot_ctx *slot_ctx;
  271. struct xhci_virt_device *virt_dev;
  272. struct mu3h_sch_bw_info *sch_bw;
  273. struct mu3h_sch_ep_info *sch_ep;
  274. struct mu3h_sch_bw_info *sch_array;
  275. unsigned int ep_index;
  276. int bw_index;
  277. int ret = 0;
  278. xhci = hcd_to_xhci(hcd);
  279. virt_dev = xhci->devs[udev->slot_id];
  280. ep_index = xhci_get_endpoint_index(&ep->desc);
  281. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  282. ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
  283. sch_array = mtk->sch_array;
  284. xhci_dbg(xhci, "%s() type:%d, speed:%d, mpkt:%d, dir:%d, ep:%p\n",
  285. __func__, usb_endpoint_type(&ep->desc), udev->speed,
  286. usb_endpoint_maxp(&ep->desc),
  287. usb_endpoint_dir_in(&ep->desc), ep);
  288. if (!need_bw_sch(ep, udev->speed, slot_ctx->tt_info & TT_SLOT)) {
  289. /*
  290. * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
  291. * device does not connected through an external HS hub
  292. */
  293. if (usb_endpoint_xfer_int(&ep->desc)
  294. || usb_endpoint_xfer_isoc(&ep->desc))
  295. ep_ctx->reserved[0] |= cpu_to_le32(EP_BPKTS(1));
  296. return 0;
  297. }
  298. bw_index = get_bw_index(xhci, udev, ep);
  299. sch_bw = &sch_array[bw_index];
  300. sch_ep = kzalloc(sizeof(struct mu3h_sch_ep_info), GFP_NOIO);
  301. if (!sch_ep)
  302. return -ENOMEM;
  303. setup_sch_info(udev, ep_ctx, sch_ep);
  304. ret = check_sch_bw(udev, sch_bw, sch_ep);
  305. if (ret) {
  306. xhci_err(xhci, "Not enough bandwidth!\n");
  307. kfree(sch_ep);
  308. return -ENOSPC;
  309. }
  310. list_add_tail(&sch_ep->endpoint, &sch_bw->bw_ep_list);
  311. sch_ep->ep = ep;
  312. ep_ctx->reserved[0] |= cpu_to_le32(EP_BPKTS(sch_ep->pkts)
  313. | EP_BCSCOUNT(sch_ep->cs_count) | EP_BBM(sch_ep->burst_mode));
  314. ep_ctx->reserved[1] |= cpu_to_le32(EP_BOFFSET(sch_ep->offset)
  315. | EP_BREPEAT(sch_ep->repeat));
  316. xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
  317. sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
  318. sch_ep->offset, sch_ep->repeat);
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(xhci_mtk_add_ep_quirk);
  322. void xhci_mtk_drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
  323. struct usb_host_endpoint *ep)
  324. {
  325. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  326. struct xhci_hcd *xhci;
  327. struct xhci_slot_ctx *slot_ctx;
  328. struct xhci_virt_device *virt_dev;
  329. struct mu3h_sch_bw_info *sch_array;
  330. struct mu3h_sch_bw_info *sch_bw;
  331. struct mu3h_sch_ep_info *sch_ep;
  332. int bw_index;
  333. xhci = hcd_to_xhci(hcd);
  334. virt_dev = xhci->devs[udev->slot_id];
  335. slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
  336. sch_array = mtk->sch_array;
  337. xhci_dbg(xhci, "%s() type:%d, speed:%d, mpks:%d, dir:%d, ep:%p\n",
  338. __func__, usb_endpoint_type(&ep->desc), udev->speed,
  339. usb_endpoint_maxp(&ep->desc),
  340. usb_endpoint_dir_in(&ep->desc), ep);
  341. if (!need_bw_sch(ep, udev->speed, slot_ctx->tt_info & TT_SLOT))
  342. return;
  343. bw_index = get_bw_index(xhci, udev, ep);
  344. sch_bw = &sch_array[bw_index];
  345. list_for_each_entry(sch_ep, &sch_bw->bw_ep_list, endpoint) {
  346. if (sch_ep->ep == ep) {
  347. update_bus_bw(sch_bw, sch_ep,
  348. -sch_ep->bw_cost_per_microframe);
  349. list_del(&sch_ep->endpoint);
  350. kfree(sch_ep);
  351. break;
  352. }
  353. }
  354. }
  355. EXPORT_SYMBOL_GPL(xhci_mtk_drop_ep_quirk);