1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 MediaTek Inc.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
16 #define SSP_BW_BOUNDARY 130000
17 #define SS_BW_BOUNDARY 51000
18 /* table 5-5. High-speed Isoc Transaction Limits in usb_20 spec */
19 #define HS_BW_BOUNDARY 6144
20 /* usb2 spec section11.18.1: at most 188 FS bytes per microframe */
21 #define FS_PAYLOAD_MAX 188
22 #define LS_PAYLOAD_MAX 18
23 /* section 11.18.1, per fs frame */
24 #define FS_BW_BOUNDARY 1157
25 #define LS_BW_BOUNDARY 144
28 * max number of microframes for split transfer, assume extra-cs budget is 0
29 * for fs isoc in : 1 ss + 1 idle + 6 cs (roundup(1023/188))
31 #define TT_MICROFRAMES_MAX 8
32 /* offset from SS for fs/ls isoc/intr ep (ss + idle) */
37 /* schedule error type */
38 #define ESCH_SS_Y6 1001
39 #define ESCH_SS_OVERLAP 1002
40 #define ESCH_CS_OVERFLOW 1003
41 #define ESCH_BW_OVERFLOW 1004
42 #define ESCH_FIXME 1005
44 /* mtk scheduler bitmasks */
45 #define EP_BPKTS(p) ((p) & 0x7f)
46 #define EP_BCSCOUNT(p) (((p) & 0x7) << 8)
47 #define EP_BBM(p) ((p) << 11)
48 #define EP_BOFFSET(p) ((p) & 0x3fff)
49 #define EP_BREPEAT(p) (((p) & 0x7fff) << 16)
51 static char *sch_error_string(int err_num)
55 return "Can't schedule Start-Split in Y6";
57 return "Can't find a suitable Start-Split location";
58 case ESCH_CS_OVERFLOW:
59 return "The last Complete-Split is greater than 7";
60 case ESCH_BW_OVERFLOW:
61 return "Bandwidth exceeds the maximum limit";
63 return "FIXME, to be resolved";
69 static int is_fs_or_ls(enum usb_device_speed speed)
71 return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
75 decode_ep(struct usb_host_endpoint *ep, enum usb_device_speed speed)
77 static char buf[DBG_BUF_EN];
78 struct usb_endpoint_descriptor *epd = &ep->desc;
79 unsigned int interval;
82 interval = usb_decode_interval(epd, speed);
83 if (interval % 1000) {
90 snprintf(buf, DBG_BUF_EN, "%s ep%d%s %s, mpkt:%d, interval:%d/%d%s",
91 usb_speed_string(speed), usb_endpoint_num(epd),
92 usb_endpoint_dir_in(epd) ? "in" : "out",
93 usb_ep_type_string(usb_endpoint_type(epd)),
94 usb_endpoint_maxp(epd), epd->bInterval, interval, unit);
99 static u32 get_bw_boundary(enum usb_device_speed speed)
104 case USB_SPEED_SUPER_PLUS:
105 boundary = SSP_BW_BOUNDARY;
107 case USB_SPEED_SUPER:
108 boundary = SS_BW_BOUNDARY;
111 boundary = HS_BW_BOUNDARY;
119 * get the bandwidth domain which @ep belongs to.
121 * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
122 * each HS root port is treated as a single bandwidth domain,
123 * but each SS root port is treated as two bandwidth domains, one for IN eps,
126 static struct mu3h_sch_bw_info *
127 get_bw_info(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
128 struct usb_host_endpoint *ep)
130 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
131 struct xhci_virt_device *virt_dev;
134 virt_dev = xhci->devs[udev->slot_id];
135 if (!virt_dev->rhub_port) {
136 WARN_ONCE(1, "%s invalid rhub port\n", dev_name(&udev->dev));
140 if (udev->speed >= USB_SPEED_SUPER) {
141 if (usb_endpoint_dir_out(&ep->desc))
142 bw_index = (virt_dev->rhub_port->hw_portnum) * 2;
144 bw_index = (virt_dev->rhub_port->hw_portnum) * 2 + 1;
146 /* add one more for each SS port */
147 bw_index = virt_dev->rhub_port->hw_portnum + xhci->usb3_rhub.num_ports;
150 return &mtk->sch_array[bw_index];
153 static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
157 esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
158 if (esit > XHCI_MTK_MAX_ESIT)
159 esit = XHCI_MTK_MAX_ESIT;
164 static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
166 struct usb_tt *utt = udev->tt;
167 struct mu3h_sch_tt *tt, **tt_index, **ptt;
168 bool allocated_index = false;
171 return NULL; /* Not below a TT */
174 * Find/create our data structure.
175 * For hubs with a single TT, we get it directly.
176 * For hubs with multiple TTs, there's an extra level of pointers.
180 tt_index = utt->hcpriv;
181 if (!tt_index) { /* Create the index array */
182 tt_index = kcalloc(utt->hub->maxchild,
183 sizeof(*tt_index), GFP_KERNEL);
185 return ERR_PTR(-ENOMEM);
186 utt->hcpriv = tt_index;
187 allocated_index = true;
189 ptt = &tt_index[udev->ttport - 1];
191 ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
195 if (!tt) { /* Create the mu3h_sch_tt */
196 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
198 if (allocated_index) {
202 return ERR_PTR(-ENOMEM);
204 INIT_LIST_HEAD(&tt->ep_list);
211 /* Release the TT above udev, if it's not in use */
212 static void drop_tt(struct usb_device *udev)
214 struct usb_tt *utt = udev->tt;
215 struct mu3h_sch_tt *tt, **tt_index, **ptt;
218 if (!utt || !utt->hcpriv)
219 return; /* Not below a TT, or never allocated */
223 tt_index = utt->hcpriv;
224 ptt = &tt_index[udev->ttport - 1];
225 /* How many entries are left in tt_index? */
226 for (i = 0; i < utt->hub->maxchild; ++i)
227 cnt += !!tt_index[i];
230 ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
234 if (!tt || !list_empty(&tt->ep_list))
235 return; /* never allocated , or still in use*/
246 static struct mu3h_sch_ep_info *
247 create_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
248 struct usb_host_endpoint *ep, struct xhci_ep_ctx *ep_ctx)
250 struct mu3h_sch_ep_info *sch_ep;
251 struct mu3h_sch_bw_info *bw_info;
252 struct mu3h_sch_tt *tt = NULL;
255 bw_info = get_bw_info(mtk, udev, ep);
257 return ERR_PTR(-ENODEV);
259 if (is_fs_or_ls(udev->speed))
260 len = TT_MICROFRAMES_MAX;
261 else if ((udev->speed >= USB_SPEED_SUPER) &&
262 usb_endpoint_xfer_isoc(&ep->desc))
263 len = get_esit(ep_ctx);
267 sch_ep = kzalloc(struct_size(sch_ep, bw_budget_table, len), GFP_KERNEL);
269 return ERR_PTR(-ENOMEM);
271 if (is_fs_or_ls(udev->speed)) {
275 return ERR_PTR(-ENOMEM);
279 sch_ep->bw_info = bw_info;
282 sch_ep->speed = udev->speed;
283 INIT_LIST_HEAD(&sch_ep->endpoint);
284 INIT_LIST_HEAD(&sch_ep->tt_endpoint);
285 INIT_HLIST_NODE(&sch_ep->hentry);
290 static void setup_sch_info(struct xhci_ep_ctx *ep_ctx,
291 struct mu3h_sch_ep_info *sch_ep)
298 u32 max_esit_payload;
299 u32 bw_per_microframe;
303 bwb_table = sch_ep->bw_budget_table;
304 ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
305 maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
306 max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
307 mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
309 (CTX_TO_MAX_ESIT_PAYLOAD_HI(
310 le32_to_cpu(ep_ctx->ep_info)) << 16) |
311 CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
313 sch_ep->esit = get_esit(ep_ctx);
314 sch_ep->num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
315 sch_ep->ep_type = ep_type;
316 sch_ep->maxpkt = maxpkt;
318 sch_ep->burst_mode = 0;
321 if (sch_ep->speed == USB_SPEED_HIGH) {
322 sch_ep->cs_count = 0;
325 * usb_20 spec section5.9
326 * a single microframe is enough for HS synchromous endpoints
329 sch_ep->num_budget_microframes = 1;
332 * xHCI spec section6.2.3.4
333 * @max_burst is the number of additional transactions
334 * opportunities per microframe
336 sch_ep->pkts = max_burst + 1;
337 bwb_table[0] = maxpkt * sch_ep->pkts;
338 } else if (sch_ep->speed >= USB_SPEED_SUPER) {
339 /* usb3_r1 spec section4.4.7 & 4.4.8 */
340 sch_ep->cs_count = 0;
341 sch_ep->burst_mode = 1;
343 * some device's (d)wBytesPerInterval is set as 0,
344 * then max_esit_payload is 0, so evaluate esit_pkts from
347 esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
349 esit_pkts = (mult + 1) * (max_burst + 1);
351 if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
352 sch_ep->pkts = esit_pkts;
353 sch_ep->num_budget_microframes = 1;
354 bwb_table[0] = maxpkt * sch_ep->pkts;
357 if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
359 if (sch_ep->esit == 1)
360 sch_ep->pkts = esit_pkts;
361 else if (esit_pkts <= sch_ep->esit)
364 sch_ep->pkts = roundup_pow_of_two(esit_pkts)
367 sch_ep->num_budget_microframes =
368 DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
370 sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
371 bw_per_microframe = maxpkt * sch_ep->pkts;
373 for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
374 bwb_table[i] = bw_per_microframe;
376 /* last one <= bw_per_microframe */
377 bwb_table[i] = maxpkt * esit_pkts - i * bw_per_microframe;
379 } else if (is_fs_or_ls(sch_ep->speed)) {
380 sch_ep->pkts = 1; /* at most one packet for each microframe */
383 * @cs_count will be updated to add extra-cs when
384 * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
387 sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
388 sch_ep->num_budget_microframes = sch_ep->cs_count;
390 /* init budget table */
391 if (ep_type == ISOC_OUT_EP) {
392 for (i = 0; i < sch_ep->cs_count - 1; i++)
393 bwb_table[i] = FS_PAYLOAD_MAX;
395 bwb_table[i] = maxpkt - i * FS_PAYLOAD_MAX;
396 } else if (ep_type == INT_OUT_EP) {
397 /* only first one used (maxpkt <= 64), others zero */
398 bwb_table[0] = maxpkt;
399 } else { /* INT_IN_EP or ISOC_IN_EP */
400 bwb_table[0] = 0; /* start split */
401 bwb_table[1] = 0; /* idle */
403 * @cs_count will be updated according to cs position
404 * (add 1 or 2 extra-cs), but assume only first
405 * @num_budget_microframes elements will be used later,
406 * although in fact it does not (extra-cs budget many receive
407 * some data for IN ep);
408 * @cs_count is 1 for INT_IN_EP (maxpkt <= 64);
410 for (i = 0; i < sch_ep->cs_count - 1; i++)
411 bwb_table[i + CS_OFFSET] = FS_PAYLOAD_MAX;
413 bwb_table[i + CS_OFFSET] = maxpkt - i * FS_PAYLOAD_MAX;
415 sch_ep->num_budget_microframes += CS_OFFSET;
420 /* Get maximum bandwidth when we schedule at offset slot. */
421 static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
422 struct mu3h_sch_ep_info *sch_ep, u32 offset)
428 for (i = 0; i < sch_ep->num_esit; i++) {
429 u32 base = offset + i * sch_ep->esit;
431 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
432 k = XHCI_MTK_BW_INDEX(base + j);
433 bw = sch_bw->bus_bw[k] + sch_ep->bw_budget_table[j];
442 * for OUT: get first SS consumed bw;
443 * for IN: get first CS consumed bw;
445 static u16 get_fs_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
447 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
450 if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
451 fs_bw = tt->fs_bus_bw_out[XHCI_MTK_BW_INDEX(offset)];
452 else /* skip ss + idle */
453 fs_bw = tt->fs_bus_bw_in[XHCI_MTK_BW_INDEX(offset + CS_OFFSET)];
458 static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
459 struct mu3h_sch_ep_info *sch_ep, bool used)
464 for (i = 0; i < sch_ep->num_esit; i++) {
465 base = sch_ep->offset + i * sch_ep->esit;
466 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
467 k = XHCI_MTK_BW_INDEX(base + j);
469 sch_bw->bus_bw[k] += sch_ep->bw_budget_table[j];
471 sch_bw->bus_bw[k] -= sch_ep->bw_budget_table[j];
476 static int check_ls_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
478 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
481 if (sch_ep->speed != USB_SPEED_LOW)
484 if (sch_ep->ep_type == INT_OUT_EP)
485 i = XHCI_MTK_BW_INDEX(offset);
486 else if (sch_ep->ep_type == INT_IN_EP)
487 i = XHCI_MTK_BW_INDEX(offset + CS_OFFSET); /* skip ss + idle */
491 if (tt->ls_bus_bw[i] + sch_ep->maxpkt > LS_PAYLOAD_MAX)
492 return -ESCH_BW_OVERFLOW;
497 static int check_fs_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
499 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
504 * for OUT eps, will transfer exactly assigned length of data,
505 * so can't allocate more than 188 bytes;
506 * but it's not for IN eps, usually it can't receive full
507 * 188 bytes in a uframe, if it not assign full 188 bytes,
508 * can add another one;
510 for (i = 0; i < sch_ep->num_budget_microframes; i++) {
511 k = XHCI_MTK_BW_INDEX(offset + i);
512 if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
513 tmp = tt->fs_bus_bw_out[k] + sch_ep->bw_budget_table[i];
514 else /* ep_type : ISOC IN / INTR IN */
515 tmp = tt->fs_bus_bw_in[k];
517 if (tmp > FS_PAYLOAD_MAX)
518 return -ESCH_BW_OVERFLOW;
524 static int check_fs_budget_frames(struct mu3h_sch_ep_info *sch_ep, int offset)
526 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
530 /* bugdet scheduled may cross at most two fs frames */
531 j = XHCI_MTK_BW_INDEX(offset) / UFRAMES_PER_FRAME;
532 k = XHCI_MTK_BW_INDEX(offset + sch_ep->num_budget_microframes - 1) / UFRAMES_PER_FRAME;
535 head = tt->fs_frame_bw[j];
536 tail = tt->fs_frame_bw[k];
538 head = tt->fs_frame_bw[j];
542 j = roundup(offset, UFRAMES_PER_FRAME);
543 for (i = 0; i < sch_ep->num_budget_microframes; i++) {
544 if ((offset + i) < j)
545 head += sch_ep->bw_budget_table[i];
547 tail += sch_ep->bw_budget_table[i];
550 if (head > FS_BW_BOUNDARY || tail > FS_BW_BOUNDARY)
551 return -ESCH_BW_OVERFLOW;
556 static int check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
561 for (i = 0; i < sch_ep->num_esit; i++) {
562 base = offset + i * sch_ep->esit;
564 ret = check_ls_budget_microframes(sch_ep, base);
568 ret = check_fs_budget_microframes(sch_ep, base);
572 ret = check_fs_budget_frames(sch_ep, base);
581 static int check_ss_and_cs(struct mu3h_sch_ep_info *sch_ep, u32 offset)
583 u32 start_ss, last_ss;
584 u32 start_cs, last_cs;
586 start_ss = offset % UFRAMES_PER_FRAME;
588 if (sch_ep->ep_type == ISOC_OUT_EP) {
589 last_ss = start_ss + sch_ep->cs_count - 1;
592 * usb_20 spec section11.18:
593 * must never schedule Start-Split in Y6
595 if (!(start_ss == 7 || last_ss < 6))
599 /* maxpkt <= 1023, cs <= 6 */
600 u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
603 * usb_20 spec section11.18:
604 * must never schedule Start-Split in Y6
609 /* one uframe for ss + one uframe for idle */
610 start_cs = (start_ss + CS_OFFSET) % UFRAMES_PER_FRAME;
611 last_cs = start_cs + cs_count - 1;
613 return -ESCH_CS_OVERFLOW;
616 cs_count += (last_cs == 7) ? 1 : 2;
618 cs_count = 7; /* HW limit */
620 sch_ep->cs_count = cs_count;
628 * when isoc-out transfers 188 bytes in a uframe, and send isoc/intr's
629 * ss token in the uframe, may cause 'bit stuff error' in downstream
631 * when isoc-out transfer less than 188 bytes in a uframe, shall send
632 * isoc-in's ss after isoc-out's ss (but hw can't ensure the sequence,
633 * so just avoid overlap).
635 static int check_isoc_ss_overlap(struct mu3h_sch_ep_info *sch_ep, u32 offset)
637 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
644 for (i = 0; i < sch_ep->num_esit; i++) {
645 base = offset + i * sch_ep->esit;
647 if (sch_ep->ep_type == ISOC_OUT_EP) {
648 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
649 k = XHCI_MTK_BW_INDEX(base + j);
650 if (tt->in_ss_cnt[k])
651 return -ESCH_SS_OVERLAP;
653 } else if (sch_ep->ep_type == ISOC_IN_EP || sch_ep->ep_type == INT_IN_EP) {
654 k = XHCI_MTK_BW_INDEX(base);
655 /* only check IN's ss */
656 if (tt->fs_bus_bw_out[k])
657 return -ESCH_SS_OVERLAP;
664 static int check_sch_tt_budget(struct mu3h_sch_ep_info *sch_ep, u32 offset)
668 ret = check_ss_and_cs(sch_ep, offset);
672 ret = check_isoc_ss_overlap(sch_ep, offset);
676 return check_fs_bus_bw(sch_ep, offset);
679 /* allocate microframes in the ls/fs frame */
680 static int alloc_sch_portion_of_frame(struct mu3h_sch_ep_info *sch_ep)
682 struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
683 const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
684 u32 bw_max, fs_bw_min;
685 u32 offset, offset_min;
691 frames = sch_ep->esit / UFRAMES_PER_FRAME;
693 for (i = 0; i < UFRAMES_PER_FRAME; i++) {
694 fs_bw_min = FS_PAYLOAD_MAX;
695 offset_min = XHCI_MTK_MAX_ESIT;
697 for (j = 0; j < frames; j++) {
698 offset = (i + j * UFRAMES_PER_FRAME) % sch_ep->esit;
700 ret = check_sch_tt_budget(sch_ep, offset);
704 /* check hs bw domain */
705 bw_max = get_max_bw(sch_bw, sch_ep, offset);
706 if (bw_max > bw_boundary) {
707 ret = -ESCH_BW_OVERFLOW;
711 /* use best-fit between frames */
712 fs_bw = get_fs_bw(sch_ep, offset);
713 if (fs_bw < fs_bw_min) {
722 /* use first-fit between microframes in a frame */
723 if (offset_min < XHCI_MTK_MAX_ESIT)
727 if (offset_min == XHCI_MTK_MAX_ESIT)
728 return -ESCH_BW_OVERFLOW;
730 sch_ep->offset = offset_min;
735 static void update_sch_tt(struct mu3h_sch_ep_info *sch_ep, bool used)
737 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
742 if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
743 fs_bus_bw = tt->fs_bus_bw_out;
745 fs_bus_bw = tt->fs_bus_bw_in;
747 for (i = 0; i < sch_ep->num_esit; i++) {
748 base = sch_ep->offset + i * sch_ep->esit;
750 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
751 k = XHCI_MTK_BW_INDEX(base + j);
752 f = k / UFRAMES_PER_FRAME;
754 if (sch_ep->speed == USB_SPEED_LOW)
755 tt->ls_bus_bw[k] += (u8)sch_ep->bw_budget_table[j];
757 fs_bus_bw[k] += (u16)sch_ep->bw_budget_table[j];
758 tt->fs_frame_bw[f] += (u16)sch_ep->bw_budget_table[j];
760 if (sch_ep->speed == USB_SPEED_LOW)
761 tt->ls_bus_bw[k] -= (u8)sch_ep->bw_budget_table[j];
763 fs_bus_bw[k] -= (u16)sch_ep->bw_budget_table[j];
764 tt->fs_frame_bw[f] -= (u16)sch_ep->bw_budget_table[j];
768 if (sch_ep->ep_type == ISOC_IN_EP || sch_ep->ep_type == INT_IN_EP) {
769 k = XHCI_MTK_BW_INDEX(base);
778 list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
780 list_del(&sch_ep->tt_endpoint);
783 static int load_ep_bw(struct mu3h_sch_bw_info *sch_bw,
784 struct mu3h_sch_ep_info *sch_ep, bool loaded)
787 update_sch_tt(sch_ep, loaded);
789 /* update bus bandwidth info */
790 update_bus_bw(sch_bw, sch_ep, loaded);
791 sch_ep->allocated = loaded;
796 /* allocate microframes for hs/ss/ssp */
797 static int alloc_sch_microframes(struct mu3h_sch_ep_info *sch_ep)
799 struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
800 const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
807 * Search through all possible schedule microframes.
808 * and find a microframe where its worst bandwidth is minimum.
810 for (offset = 0; offset < sch_ep->esit; offset++) {
812 worst_bw = get_max_bw(sch_bw, sch_ep, offset);
813 if (worst_bw > bw_boundary)
816 if (min_bw > worst_bw) {
823 return -ESCH_BW_OVERFLOW;
825 sch_ep->offset = min_index;
830 static int check_sch_bw(struct mu3h_sch_ep_info *sch_ep)
835 ret = alloc_sch_portion_of_frame(sch_ep);
837 ret = alloc_sch_microframes(sch_ep);
842 return load_ep_bw(sch_ep->bw_info, sch_ep, true);
845 static void destroy_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
846 struct mu3h_sch_ep_info *sch_ep)
848 /* only release ep bw check passed by check_sch_bw() */
849 if (sch_ep->allocated)
850 load_ep_bw(sch_ep->bw_info, sch_ep, false);
855 list_del(&sch_ep->endpoint);
856 hlist_del(&sch_ep->hentry);
860 static bool need_bw_sch(struct usb_device *udev,
861 struct usb_host_endpoint *ep)
863 bool has_tt = udev->tt && udev->tt->hub->parent;
865 /* only for periodic endpoints */
866 if (usb_endpoint_xfer_control(&ep->desc)
867 || usb_endpoint_xfer_bulk(&ep->desc))
871 * for LS & FS periodic endpoints which its device is not behind
872 * a TT are also ignored, root-hub will schedule them directly,
873 * but need set @bpkts field of endpoint context to 1.
875 if (is_fs_or_ls(udev->speed) && !has_tt)
878 /* skip endpoint with zero maxpkt */
879 if (usb_endpoint_maxp(&ep->desc) == 0)
885 int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
887 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
888 struct mu3h_sch_bw_info *sch_array;
891 /* ss IN and OUT are separated */
892 num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
894 sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
895 if (sch_array == NULL)
898 mtk->sch_array = sch_array;
900 INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
901 hash_init(mtk->sch_ep_hash);
906 void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
908 kfree(mtk->sch_array);
911 static int add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
912 struct usb_host_endpoint *ep)
914 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
915 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
916 struct xhci_ep_ctx *ep_ctx;
917 struct xhci_virt_device *virt_dev;
918 struct mu3h_sch_ep_info *sch_ep;
919 unsigned int ep_index;
921 virt_dev = xhci->devs[udev->slot_id];
922 ep_index = xhci_get_endpoint_index(&ep->desc);
923 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
925 if (!need_bw_sch(udev, ep)) {
927 * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
928 * device does not connected through an external HS hub
930 if (usb_endpoint_xfer_int(&ep->desc)
931 || usb_endpoint_xfer_isoc(&ep->desc))
932 ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
937 xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
939 sch_ep = create_sch_ep(mtk, udev, ep, ep_ctx);
940 if (IS_ERR_OR_NULL(sch_ep))
943 setup_sch_info(ep_ctx, sch_ep);
945 list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
946 hash_add(mtk->sch_ep_hash, &sch_ep->hentry, (unsigned long)ep);
951 static void drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
952 struct usb_host_endpoint *ep)
954 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
955 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
956 struct mu3h_sch_ep_info *sch_ep;
957 struct hlist_node *hn;
959 if (!need_bw_sch(udev, ep))
962 xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
964 hash_for_each_possible_safe(mtk->sch_ep_hash, sch_ep,
965 hn, hentry, (unsigned long)ep) {
966 if (sch_ep->ep == ep) {
967 destroy_sch_ep(mtk, udev, sch_ep);
973 int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
975 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
976 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
977 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
978 struct mu3h_sch_ep_info *sch_ep;
981 xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
983 list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
984 struct xhci_ep_ctx *ep_ctx;
985 struct usb_host_endpoint *ep = sch_ep->ep;
986 unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
988 ret = check_sch_bw(sch_ep);
990 xhci_err(xhci, "Not enough bandwidth! (%s)\n",
991 sch_error_string(-ret));
995 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
996 ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(sch_ep->pkts)
997 | EP_BCSCOUNT(sch_ep->cs_count)
998 | EP_BBM(sch_ep->burst_mode));
999 ep_ctx->reserved[1] = cpu_to_le32(EP_BOFFSET(sch_ep->offset)
1000 | EP_BREPEAT(sch_ep->repeat));
1002 xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
1003 sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
1004 sch_ep->offset, sch_ep->repeat);
1007 ret = xhci_check_bandwidth(hcd, udev);
1009 list_del_init(&mtk->bw_ep_chk_list);
1014 void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1016 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
1017 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1018 struct mu3h_sch_ep_info *sch_ep, *tmp;
1020 xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
1022 list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint)
1023 destroy_sch_ep(mtk, udev, sch_ep);
1025 xhci_reset_bandwidth(hcd, udev);
1028 int xhci_mtk_add_ep(struct usb_hcd *hcd, struct usb_device *udev,
1029 struct usb_host_endpoint *ep)
1033 ret = xhci_add_endpoint(hcd, udev, ep);
1038 ret = add_ep_quirk(hcd, udev, ep);
1043 int xhci_mtk_drop_ep(struct usb_hcd *hcd, struct usb_device *udev,
1044 struct usb_host_endpoint *ep)
1048 ret = xhci_drop_endpoint(hcd, udev, ep);
1052 /* needn't check @ep->hcpriv, xhci_endpoint_disable set it NULL */
1053 drop_ep_quirk(hcd, udev, ep);