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