/* * max number of microframes for split transfer, assume extra-cs budget is 0 * for fs isoc in : 1 ss + 1 idle + 6 cs (roundup(1023/188))
*/ #define TT_MICROFRAMES_MAX 8 /* offset from SS for fs/ls isoc/intr ep (ss + idle) */ #define CS_OFFSET 2
staticchar *sch_error_string(int err_num)
{ switch (err_num) { case ESCH_SS_Y6: return"Can't schedule Start-Split in Y6"; case ESCH_SS_OVERLAP: return"Can't find a suitable Start-Split location"; case ESCH_CS_OVERFLOW: return"The last Complete-Split is greater than 7"; case ESCH_BW_OVERFLOW: return"Bandwidth exceeds the maximum limit"; case ESCH_FIXME: return"FIXME, to be resolved"; default: return"Unknown";
}
}
/* * get the bandwidth domain which @ep belongs to. * * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk, * each HS root port is treated as a single bandwidth domain, * but each SS root port is treated as two bandwidth domains, one for IN eps, * one for OUT eps.
*/ staticstruct mu3h_sch_bw_info *
get_bw_info(struct xhci_hcd_mtk *mtk, struct usb_device *udev, struct usb_host_endpoint *ep)
{ struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); struct xhci_virt_device *virt_dev; int bw_index;
if (udev->speed >= USB_SPEED_SUPER) { if (usb_endpoint_dir_out(&ep->desc))
bw_index = (virt_dev->rhub_port->hw_portnum) * 2; else
bw_index = (virt_dev->rhub_port->hw_portnum) * 2 + 1;
} else { /* add one more for each SS port */
bw_index = virt_dev->rhub_port->hw_portnum + xhci->usb3_rhub.num_ports;
}
/* * Find/create our data structure. * For hubs with a single TT, we get it directly. * For hubs with multiple TTs, there's an extra level of pointers.
*/
tt_index = NULL; if (utt->multi) {
tt_index = utt->hcpriv; if (!tt_index) { /* Create the index array */
tt_index = kcalloc(utt->hub->maxchild, sizeof(*tt_index), GFP_KERNEL); if (!tt_index) return ERR_PTR(-ENOMEM);
utt->hcpriv = tt_index;
allocated_index = true;
}
ptt = &tt_index[udev->ttport - 1];
} else {
ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
}
tt = *ptt; if (!tt) { /* Create the mu3h_sch_tt */
tt = kzalloc(sizeof(*tt), GFP_KERNEL); if (!tt) { if (allocated_index) {
utt->hcpriv = NULL;
kfree(tt_index);
} return ERR_PTR(-ENOMEM);
}
INIT_LIST_HEAD(&tt->ep_list);
*ptt = tt;
}
return tt;
}
/* Release the TT above udev, if it's not in use */ staticvoid drop_tt(struct usb_device *udev)
{ struct usb_tt *utt = udev->tt; struct mu3h_sch_tt *tt, **tt_index, **ptt; int i, cnt;
if (!utt || !utt->hcpriv) return; /* Not below a TT, or never allocated */
cnt = 0; if (utt->multi) {
tt_index = utt->hcpriv;
ptt = &tt_index[udev->ttport - 1]; /* How many entries are left in tt_index? */ for (i = 0; i < utt->hub->maxchild; ++i)
cnt += !!tt_index[i];
} else {
tt_index = NULL;
ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
}
tt = *ptt; if (!tt || !list_empty(&tt->ep_list)) return; /* never allocated , or still in use*/
bw_info = get_bw_info(mtk, udev, ep); if (!bw_info) return ERR_PTR(-ENODEV);
if (is_fs_or_ls(udev->speed))
len = TT_MICROFRAMES_MAX; elseif ((udev->speed >= USB_SPEED_SUPER) &&
usb_endpoint_xfer_isoc(&ep->desc))
len = get_esit(ep_ctx); else
len = 1;
sch_ep = kzalloc(struct_size(sch_ep, bw_budget_table, len), GFP_KERNEL); if (!sch_ep) return ERR_PTR(-ENOMEM);
if (is_fs_or_ls(udev->speed)) {
tt = find_tt(udev); if (IS_ERR(tt)) {
kfree(sch_ep); return ERR_PTR(-ENOMEM);
}
}
for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
bwb_table[i] = bw_per_microframe;
/* last one <= bw_per_microframe */
bwb_table[i] = maxpkt * esit_pkts - i * bw_per_microframe;
}
} elseif (is_fs_or_ls(sch_ep->speed)) {
sch_ep->pkts = 1; /* at most one packet for each microframe */
/* * @cs_count will be updated to add extra-cs when * check TT for INT_OUT_EP, ISOC/INT_IN_EP type * @maxpkt <= 1023;
*/
sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
sch_ep->num_budget_microframes = sch_ep->cs_count;
/* init budget table */ if (ep_type == ISOC_OUT_EP) { for (i = 0; i < sch_ep->cs_count - 1; i++)
bwb_table[i] = FS_PAYLOAD_MAX;
bwb_table[i] = maxpkt - i * FS_PAYLOAD_MAX;
} elseif (ep_type == INT_OUT_EP) { /* only first one used (maxpkt <= 64), others zero */
bwb_table[0] = maxpkt;
} else { /* INT_IN_EP or ISOC_IN_EP */
bwb_table[0] = 0; /* start split */
bwb_table[1] = 0; /* idle */ /* * @cs_count will be updated according to cs position * (add 1 or 2 extra-cs), but assume only first * @num_budget_microframes elements will be used later, * although in fact it does not (extra-cs budget many receive * some data for IN ep); * @cs_count is 1 for INT_IN_EP (maxpkt <= 64);
*/ for (i = 0; i < sch_ep->cs_count - 1; i++)
bwb_table[i + CS_OFFSET] = FS_PAYLOAD_MAX;
bwb_table[i + CS_OFFSET] = maxpkt - i * FS_PAYLOAD_MAX; /* ss + idle */
sch_ep->num_budget_microframes += CS_OFFSET;
}
}
}
/* Get maximum bandwidth when we schedule at offset slot. */ static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep, u32 offset)
{
u32 max_bw = 0;
u32 bw; int i, j, k;
for (i = 0; i < sch_ep->num_esit; i++) {
u32 base = offset + i * sch_ep->esit;
/* * for OUT: get first SS consumed bw; * for IN: get first CS consumed bw;
*/ static u16 get_fs_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
{ struct mu3h_sch_tt *tt = sch_ep->sch_tt;
u16 fs_bw;
staticvoid update_bus_bw(struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep, bool used)
{
u32 base; int i, j, k;
for (i = 0; i < sch_ep->num_esit; i++) {
base = sch_ep->offset + i * sch_ep->esit; for (j = 0; j < sch_ep->num_budget_microframes; j++) {
k = XHCI_MTK_BW_INDEX(base + j); if (used)
sch_bw->bus_bw[k] += sch_ep->bw_budget_table[j]; else
sch_bw->bus_bw[k] -= sch_ep->bw_budget_table[j];
}
}
}
staticint check_ls_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
{ struct mu3h_sch_tt *tt = sch_ep->sch_tt; int i;
if (sch_ep->speed != USB_SPEED_LOW) return 0;
if (sch_ep->ep_type == INT_OUT_EP)
i = XHCI_MTK_BW_INDEX(offset); elseif (sch_ep->ep_type == INT_IN_EP)
i = XHCI_MTK_BW_INDEX(offset + CS_OFFSET); /* skip ss + idle */ else return -EINVAL;
if (tt->ls_bus_bw[i] + sch_ep->maxpkt > LS_PAYLOAD_MAX) return -ESCH_BW_OVERFLOW;
return 0;
}
staticint check_fs_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
{ struct mu3h_sch_tt *tt = sch_ep->sch_tt;
u32 tmp; int i, k;
/* * for OUT eps, will transfer exactly assigned length of data, * so can't allocate more than 188 bytes; * but it's not for IN eps, usually it can't receive full * 188 bytes in a uframe, if it not assign full 188 bytes, * can add another one;
*/ for (i = 0; i < sch_ep->num_budget_microframes; i++) {
k = XHCI_MTK_BW_INDEX(offset + i); if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
tmp = tt->fs_bus_bw_out[k] + sch_ep->bw_budget_table[i]; else/* ep_type : ISOC IN / INTR IN */
tmp = tt->fs_bus_bw_in[k];
if (tmp > FS_PAYLOAD_MAX) return -ESCH_BW_OVERFLOW;
}
return 0;
}
staticint check_fs_budget_frames(struct mu3h_sch_ep_info *sch_ep, int offset)
{ struct mu3h_sch_tt *tt = sch_ep->sch_tt;
u32 head, tail; int i, j, k;
/* bugdet scheduled may cross at most two fs frames */
j = XHCI_MTK_BW_INDEX(offset) / UFRAMES_PER_FRAME;
k = XHCI_MTK_BW_INDEX(offset + sch_ep->num_budget_microframes - 1) / UFRAMES_PER_FRAME;
if (j != k) {
head = tt->fs_frame_bw[j];
tail = tt->fs_frame_bw[k];
} else {
head = tt->fs_frame_bw[j];
tail = 0;
}
j = roundup(offset, UFRAMES_PER_FRAME); for (i = 0; i < sch_ep->num_budget_microframes; i++) { if ((offset + i) < j)
head += sch_ep->bw_budget_table[i]; else
tail += sch_ep->bw_budget_table[i];
}
if (head > FS_BW_BOUNDARY || tail > FS_BW_BOUNDARY) return -ESCH_BW_OVERFLOW;
return 0;
}
staticint check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
{ int i, base; int ret = 0;
for (i = 0; i < sch_ep->num_esit; i++) {
base = offset + i * sch_ep->esit;
ret = check_ls_budget_microframes(sch_ep, base); if (ret) goto err;
ret = check_fs_budget_microframes(sch_ep, base); if (ret) goto err;
ret = check_fs_budget_frames(sch_ep, base); if (ret) goto err;
}
/* * when isoc-out transfers 188 bytes in a uframe, and send isoc/intr's * ss token in the uframe, may cause 'bit stuff error' in downstream * port; * when isoc-out transfer less than 188 bytes in a uframe, shall send * isoc-in's ss after isoc-out's ss (but hw can't ensure the sequence, * so just avoid overlap).
*/ staticint check_isoc_ss_overlap(struct mu3h_sch_ep_info *sch_ep, u32 offset)
{ struct mu3h_sch_tt *tt = sch_ep->sch_tt; int base; int i, j, k;
if (!tt) return 0;
for (i = 0; i < sch_ep->num_esit; i++) {
base = offset + i * sch_ep->esit;
if (sch_ep->ep_type == ISOC_OUT_EP) { for (j = 0; j < sch_ep->num_budget_microframes; j++) {
k = XHCI_MTK_BW_INDEX(base + j); if (tt->in_ss_cnt[k]) return -ESCH_SS_OVERLAP;
}
} elseif (sch_ep->ep_type == ISOC_IN_EP || sch_ep->ep_type == INT_IN_EP) {
k = XHCI_MTK_BW_INDEX(base); /* only check IN's ss */ if (tt->fs_bus_bw_out[k]) return -ESCH_SS_OVERLAP;
}
}
return 0;
}
staticint check_sch_tt_budget(struct mu3h_sch_ep_info *sch_ep, u32 offset)
{ int ret;
ret = check_ss_and_cs(sch_ep, offset); if (ret) return ret;
ret = check_isoc_ss_overlap(sch_ep, offset); if (ret) return ret;
return check_fs_bus_bw(sch_ep, offset);
}
/* allocate microframes in the ls/fs frame */ staticint alloc_sch_portion_of_frame(struct mu3h_sch_ep_info *sch_ep)
{ struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info; const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
u32 bw_max, fs_bw_min;
u32 offset, offset_min;
u16 fs_bw; int frames; int i, j; int ret;
frames = sch_ep->esit / UFRAMES_PER_FRAME;
for (i = 0; i < UFRAMES_PER_FRAME; i++) {
fs_bw_min = FS_PAYLOAD_MAX;
offset_min = XHCI_MTK_MAX_ESIT;
for (j = 0; j < frames; j++) {
offset = (i + j * UFRAMES_PER_FRAME) % sch_ep->esit;
ret = check_sch_tt_budget(sch_ep, offset); if (ret) continue;
for (i = 0; i < sch_ep->num_esit; i++) {
base = sch_ep->offset + i * sch_ep->esit;
for (j = 0; j < sch_ep->num_budget_microframes; j++) {
k = XHCI_MTK_BW_INDEX(base + j);
f = k / UFRAMES_PER_FRAME; if (used) { if (sch_ep->speed == USB_SPEED_LOW)
tt->ls_bus_bw[k] += (u8)sch_ep->bw_budget_table[j];
/* * Search through all possible schedule microframes. * and find a microframe where its worst bandwidth is minimum.
*/ for (offset = 0; offset < sch_ep->esit; offset++) {
worst_bw = get_max_bw(sch_bw, sch_ep, offset); if (worst_bw > bw_boundary) continue;
/* only for periodic endpoints */ if (usb_endpoint_xfer_control(&ep->desc)
|| usb_endpoint_xfer_bulk(&ep->desc)) returnfalse;
/* * for LS & FS periodic endpoints which its device is not behind * a TT are also ignored, root-hub will schedule them directly, * but need set @bpkts field of endpoint context to 1.
*/ if (is_fs_or_ls(udev->speed) && !has_tt) returnfalse;
/* skip endpoint with zero maxpkt */ if (usb_endpoint_maxp(&ep->desc) == 0) returnfalse;
returntrue;
}
int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
{ struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); struct mu3h_sch_bw_info *sch_array; int num_usb_bus;
/* ss IN and OUT are separated */
num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
if (!need_bw_sch(udev, ep)) { /* * set @bpkts to 1 if it is LS or FS periodic endpoint, and its * device does not connected through an external HS hub
*/ if (usb_endpoint_xfer_int(&ep->desc)
|| usb_endpoint_xfer_isoc(&ep->desc))
ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.