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