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
25 /* schedule error type */
26 #define ESCH_SS_Y6 1001
27 #define ESCH_SS_OVERLAP 1002
28 #define ESCH_CS_OVERFLOW 1003
29 #define ESCH_BW_OVERFLOW 1004
30 #define ESCH_FIXME 1005
32 /* mtk scheduler bitmasks */
33 #define EP_BPKTS(p) ((p) & 0x7f)
34 #define EP_BCSCOUNT(p) (((p) & 0x7) << 8)
35 #define EP_BBM(p) ((p) << 11)
36 #define EP_BOFFSET(p) ((p) & 0x3fff)
37 #define EP_BREPEAT(p) (((p) & 0x7fff) << 16)
39 static char *sch_error_string(int err_num)
43 return "Can't schedule Start-Split in Y6";
45 return "Can't find a suitable Start-Split location";
46 case ESCH_CS_OVERFLOW:
47 return "The last Complete-Split is greater than 7";
48 case ESCH_BW_OVERFLOW:
49 return "Bandwidth exceeds the maximum limit";
51 return "FIXME, to be resolved";
57 static int is_fs_or_ls(enum usb_device_speed speed)
59 return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
63 decode_ep(struct usb_host_endpoint *ep, enum usb_device_speed speed)
65 static char buf[DBG_BUF_EN];
66 struct usb_endpoint_descriptor *epd = &ep->desc;
67 unsigned int interval;
70 interval = usb_decode_interval(epd, speed);
71 if (interval % 1000) {
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);
87 static u32 get_bw_boundary(enum usb_device_speed speed)
92 case USB_SPEED_SUPER_PLUS:
93 boundary = SSP_BW_BOUNDARY;
96 boundary = SS_BW_BOUNDARY;
99 boundary = HS_BW_BOUNDARY;
107 * get the bandwidth domain which @ep belongs to.
109 * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
110 * each HS root port is treated as a single bandwidth domain,
111 * but each SS root port is treated as two bandwidth domains, one for IN eps,
113 * @real_port value is defined as follow according to xHCI spec:
114 * 1 for SSport0, ..., N+1 for SSportN, N+2 for HSport0, N+3 for HSport1, etc
115 * so the bandwidth domain array is organized as follow for simplification:
116 * SSport0-OUT, SSport0-IN, ..., SSportX-OUT, SSportX-IN, HSport0, ..., HSportY
118 static struct mu3h_sch_bw_info *
119 get_bw_info(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
120 struct usb_host_endpoint *ep)
122 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
123 struct xhci_virt_device *virt_dev;
126 virt_dev = xhci->devs[udev->slot_id];
127 if (!virt_dev->real_port) {
128 WARN_ONCE(1, "%s invalid real_port\n", dev_name(&udev->dev));
132 if (udev->speed >= USB_SPEED_SUPER) {
133 if (usb_endpoint_dir_out(&ep->desc))
134 bw_index = (virt_dev->real_port - 1) * 2;
136 bw_index = (virt_dev->real_port - 1) * 2 + 1;
138 /* add one more for each SS port */
139 bw_index = virt_dev->real_port + xhci->usb3_rhub.num_ports - 1;
142 return &mtk->sch_array[bw_index];
145 static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
149 esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
150 if (esit > XHCI_MTK_MAX_ESIT)
151 esit = XHCI_MTK_MAX_ESIT;
156 static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
158 struct usb_tt *utt = udev->tt;
159 struct mu3h_sch_tt *tt, **tt_index, **ptt;
160 bool allocated_index = false;
163 return NULL; /* Not below a TT */
166 * Find/create our data structure.
167 * For hubs with a single TT, we get it directly.
168 * For hubs with multiple TTs, there's an extra level of pointers.
172 tt_index = utt->hcpriv;
173 if (!tt_index) { /* Create the index array */
174 tt_index = kcalloc(utt->hub->maxchild,
175 sizeof(*tt_index), GFP_KERNEL);
177 return ERR_PTR(-ENOMEM);
178 utt->hcpriv = tt_index;
179 allocated_index = true;
181 ptt = &tt_index[udev->ttport - 1];
183 ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
187 if (!tt) { /* Create the mu3h_sch_tt */
188 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
190 if (allocated_index) {
194 return ERR_PTR(-ENOMEM);
196 INIT_LIST_HEAD(&tt->ep_list);
203 /* Release the TT above udev, if it's not in use */
204 static void drop_tt(struct usb_device *udev)
206 struct usb_tt *utt = udev->tt;
207 struct mu3h_sch_tt *tt, **tt_index, **ptt;
210 if (!utt || !utt->hcpriv)
211 return; /* Not below a TT, or never allocated */
215 tt_index = utt->hcpriv;
216 ptt = &tt_index[udev->ttport - 1];
217 /* How many entries are left in tt_index? */
218 for (i = 0; i < utt->hub->maxchild; ++i)
219 cnt += !!tt_index[i];
222 ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
226 if (!tt || !list_empty(&tt->ep_list))
227 return; /* never allocated , or still in use*/
238 static struct mu3h_sch_ep_info *
239 create_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
240 struct usb_host_endpoint *ep)
242 struct mu3h_sch_ep_info *sch_ep;
243 struct mu3h_sch_bw_info *bw_info;
244 struct mu3h_sch_tt *tt = NULL;
246 bw_info = get_bw_info(mtk, udev, ep);
248 return ERR_PTR(-ENODEV);
250 sch_ep = kzalloc(sizeof(*sch_ep), GFP_KERNEL);
252 return ERR_PTR(-ENOMEM);
254 if (is_fs_or_ls(udev->speed)) {
258 return ERR_PTR(-ENOMEM);
262 sch_ep->bw_info = bw_info;
265 sch_ep->speed = udev->speed;
266 INIT_LIST_HEAD(&sch_ep->endpoint);
267 INIT_LIST_HEAD(&sch_ep->tt_endpoint);
268 INIT_HLIST_NODE(&sch_ep->hentry);
273 static void setup_sch_info(struct xhci_ep_ctx *ep_ctx,
274 struct mu3h_sch_ep_info *sch_ep)
281 u32 max_esit_payload;
283 ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
284 maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
285 max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
286 mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
288 (CTX_TO_MAX_ESIT_PAYLOAD_HI(
289 le32_to_cpu(ep_ctx->ep_info)) << 16) |
290 CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
292 sch_ep->esit = get_esit(ep_ctx);
293 sch_ep->num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
294 sch_ep->ep_type = ep_type;
295 sch_ep->maxpkt = maxpkt;
297 sch_ep->burst_mode = 0;
300 if (sch_ep->speed == USB_SPEED_HIGH) {
301 sch_ep->cs_count = 0;
304 * usb_20 spec section5.9
305 * a single microframe is enough for HS synchromous endpoints
308 sch_ep->num_budget_microframes = 1;
311 * xHCI spec section6.2.3.4
312 * @max_burst is the number of additional transactions
313 * opportunities per microframe
315 sch_ep->pkts = max_burst + 1;
316 sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
317 } else if (sch_ep->speed >= USB_SPEED_SUPER) {
318 /* usb3_r1 spec section4.4.7 & 4.4.8 */
319 sch_ep->cs_count = 0;
320 sch_ep->burst_mode = 1;
322 * some device's (d)wBytesPerInterval is set as 0,
323 * then max_esit_payload is 0, so evaluate esit_pkts from
326 esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
328 esit_pkts = (mult + 1) * (max_burst + 1);
330 if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
331 sch_ep->pkts = esit_pkts;
332 sch_ep->num_budget_microframes = 1;
335 if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
337 if (sch_ep->esit == 1)
338 sch_ep->pkts = esit_pkts;
339 else if (esit_pkts <= sch_ep->esit)
342 sch_ep->pkts = roundup_pow_of_two(esit_pkts)
345 sch_ep->num_budget_microframes =
346 DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
348 sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
350 sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
351 } else if (is_fs_or_ls(sch_ep->speed)) {
352 sch_ep->pkts = 1; /* at most one packet for each microframe */
355 * num_budget_microframes and cs_count will be updated when
356 * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
358 sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
359 sch_ep->num_budget_microframes = sch_ep->cs_count;
360 sch_ep->bw_cost_per_microframe = min_t(u32, maxpkt, FS_PAYLOAD_MAX);
364 /* Get maximum bandwidth when we schedule at offset slot. */
365 static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
366 struct mu3h_sch_ep_info *sch_ep, u32 offset)
372 for (i = 0; i < sch_ep->num_esit; i++) {
373 u32 base = offset + i * sch_ep->esit;
375 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
376 k = XHCI_MTK_BW_INDEX(base + j);
377 bw = sch_bw->bus_bw[k] + sch_ep->bw_cost_per_microframe;
385 static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
386 struct mu3h_sch_ep_info *sch_ep, bool used)
392 bw_updated = sch_ep->bw_cost_per_microframe * (used ? 1 : -1);
394 for (i = 0; i < sch_ep->num_esit; i++) {
395 base = sch_ep->offset + i * sch_ep->esit;
396 for (j = 0; j < sch_ep->num_budget_microframes; j++)
397 sch_bw->bus_bw[XHCI_MTK_BW_INDEX(base + j)] += bw_updated;
401 static int check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
403 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
408 for (i = 0; i < sch_ep->num_esit; i++) {
409 base = offset + i * sch_ep->esit;
412 * Compared with hs bus, no matter what ep type,
413 * the hub will always delay one uframe to send data
415 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
416 k = XHCI_MTK_BW_INDEX(base + j);
417 tmp = tt->fs_bus_bw[k] + sch_ep->bw_cost_per_microframe;
418 if (tmp > FS_PAYLOAD_MAX)
419 return -ESCH_BW_OVERFLOW;
426 static int check_sch_tt(struct mu3h_sch_ep_info *sch_ep, u32 offset)
428 u32 start_ss, last_ss;
429 u32 start_cs, last_cs;
434 start_ss = offset % 8;
436 if (sch_ep->ep_type == ISOC_OUT_EP) {
437 last_ss = start_ss + sch_ep->cs_count - 1;
440 * usb_20 spec section11.18:
441 * must never schedule Start-Split in Y6
443 if (!(start_ss == 7 || last_ss < 6))
447 u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
450 * usb_20 spec section11.18:
451 * must never schedule Start-Split in Y6
456 /* one uframe for ss + one uframe for idle */
457 start_cs = (start_ss + 2) % 8;
458 last_cs = start_cs + cs_count - 1;
461 return -ESCH_CS_OVERFLOW;
464 cs_count = 7; /* HW limit */
466 sch_ep->cs_count = cs_count;
467 /* ss, idle are ignored */
468 sch_ep->num_budget_microframes = cs_count;
471 * if interval=1, maxp >752, num_budge_micoframe is larger
472 * than sch_ep->esit, will overstep boundary
474 if (sch_ep->num_budget_microframes > sch_ep->esit)
475 sch_ep->num_budget_microframes = sch_ep->esit;
478 return check_fs_bus_bw(sch_ep, offset);
481 static void update_sch_tt(struct mu3h_sch_ep_info *sch_ep, bool used)
483 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
488 bw_updated = sch_ep->bw_cost_per_microframe * (used ? 1 : -1);
490 for (i = 0; i < sch_ep->num_esit; i++) {
491 base = sch_ep->offset + i * sch_ep->esit;
493 for (j = 0; j < sch_ep->num_budget_microframes; j++)
494 tt->fs_bus_bw[XHCI_MTK_BW_INDEX(base + j)] += bw_updated;
498 list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
500 list_del(&sch_ep->tt_endpoint);
503 static int load_ep_bw(struct mu3h_sch_bw_info *sch_bw,
504 struct mu3h_sch_ep_info *sch_ep, bool loaded)
507 update_sch_tt(sch_ep, loaded);
509 /* update bus bandwidth info */
510 update_bus_bw(sch_bw, sch_ep, loaded);
511 sch_ep->allocated = loaded;
516 static int check_sch_bw(struct mu3h_sch_ep_info *sch_ep)
518 struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
519 const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
527 * Search through all possible schedule microframes.
528 * and find a microframe where its worst bandwidth is minimum.
530 for (offset = 0; offset < sch_ep->esit; offset++) {
531 ret = check_sch_tt(sch_ep, offset);
535 worst_bw = get_max_bw(sch_bw, sch_ep, offset);
536 if (worst_bw > bw_boundary)
539 if (min_bw > worst_bw) {
544 /* use first-fit for LS/FS */
545 if (sch_ep->sch_tt && min_index >= 0)
553 return ret ? ret : -ESCH_BW_OVERFLOW;
555 sch_ep->offset = min_index;
557 return load_ep_bw(sch_bw, sch_ep, true);
560 static void destroy_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
561 struct mu3h_sch_ep_info *sch_ep)
563 /* only release ep bw check passed by check_sch_bw() */
564 if (sch_ep->allocated)
565 load_ep_bw(sch_ep->bw_info, sch_ep, false);
570 list_del(&sch_ep->endpoint);
571 hlist_del(&sch_ep->hentry);
575 static bool need_bw_sch(struct usb_device *udev,
576 struct usb_host_endpoint *ep)
578 bool has_tt = udev->tt && udev->tt->hub->parent;
580 /* only for periodic endpoints */
581 if (usb_endpoint_xfer_control(&ep->desc)
582 || usb_endpoint_xfer_bulk(&ep->desc))
586 * for LS & FS periodic endpoints which its device is not behind
587 * a TT are also ignored, root-hub will schedule them directly,
588 * but need set @bpkts field of endpoint context to 1.
590 if (is_fs_or_ls(udev->speed) && !has_tt)
593 /* skip endpoint with zero maxpkt */
594 if (usb_endpoint_maxp(&ep->desc) == 0)
600 int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
602 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
603 struct mu3h_sch_bw_info *sch_array;
606 /* ss IN and OUT are separated */
607 num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
609 sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
610 if (sch_array == NULL)
613 mtk->sch_array = sch_array;
615 INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
616 hash_init(mtk->sch_ep_hash);
621 void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
623 kfree(mtk->sch_array);
626 static int add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
627 struct usb_host_endpoint *ep)
629 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
630 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
631 struct xhci_ep_ctx *ep_ctx;
632 struct xhci_virt_device *virt_dev;
633 struct mu3h_sch_ep_info *sch_ep;
634 unsigned int ep_index;
636 virt_dev = xhci->devs[udev->slot_id];
637 ep_index = xhci_get_endpoint_index(&ep->desc);
638 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
640 if (!need_bw_sch(udev, ep)) {
642 * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
643 * device does not connected through an external HS hub
645 if (usb_endpoint_xfer_int(&ep->desc)
646 || usb_endpoint_xfer_isoc(&ep->desc))
647 ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
652 xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
654 sch_ep = create_sch_ep(mtk, udev, ep);
655 if (IS_ERR_OR_NULL(sch_ep))
658 setup_sch_info(ep_ctx, sch_ep);
660 list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
661 hash_add(mtk->sch_ep_hash, &sch_ep->hentry, (unsigned long)ep);
666 static void drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
667 struct usb_host_endpoint *ep)
669 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
670 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
671 struct mu3h_sch_ep_info *sch_ep;
672 struct hlist_node *hn;
674 if (!need_bw_sch(udev, ep))
677 xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
679 hash_for_each_possible_safe(mtk->sch_ep_hash, sch_ep,
680 hn, hentry, (unsigned long)ep) {
681 if (sch_ep->ep == ep) {
682 destroy_sch_ep(mtk, udev, sch_ep);
688 int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
690 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
691 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
692 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
693 struct mu3h_sch_ep_info *sch_ep;
696 xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
698 list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
699 struct xhci_ep_ctx *ep_ctx;
700 struct usb_host_endpoint *ep = sch_ep->ep;
701 unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
703 ret = check_sch_bw(sch_ep);
705 xhci_err(xhci, "Not enough bandwidth! (%s)\n",
706 sch_error_string(-ret));
710 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
711 ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(sch_ep->pkts)
712 | EP_BCSCOUNT(sch_ep->cs_count)
713 | EP_BBM(sch_ep->burst_mode));
714 ep_ctx->reserved[1] = cpu_to_le32(EP_BOFFSET(sch_ep->offset)
715 | EP_BREPEAT(sch_ep->repeat));
717 xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
718 sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
719 sch_ep->offset, sch_ep->repeat);
722 ret = xhci_check_bandwidth(hcd, udev);
724 list_del_init(&mtk->bw_ep_chk_list);
729 void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
731 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
732 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
733 struct mu3h_sch_ep_info *sch_ep, *tmp;
735 xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
737 list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint)
738 destroy_sch_ep(mtk, udev, sch_ep);
740 xhci_reset_bandwidth(hcd, udev);
743 int xhci_mtk_add_ep(struct usb_hcd *hcd, struct usb_device *udev,
744 struct usb_host_endpoint *ep)
748 ret = xhci_add_endpoint(hcd, udev, ep);
753 ret = add_ep_quirk(hcd, udev, ep);
758 int xhci_mtk_drop_ep(struct usb_hcd *hcd, struct usb_device *udev,
759 struct usb_host_endpoint *ep)
763 ret = xhci_drop_endpoint(hcd, udev, ep);
767 /* needn't check @ep->hcpriv, xhci_endpoint_disable set it NULL */
768 drop_ep_quirk(hcd, udev, ep);