xref: /openbmc/linux/drivers/usb/host/xhci-mtk-sch.c (revision 6197e5b7)
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 
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 */
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 
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 
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 */
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 
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 
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. */
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 
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 
381 static int check_sch_tt(struct usb_device *udev,
382 	struct mu3h_sch_ep_info *sch_ep, u32 offset)
383 {
384 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
385 	u32 extra_cs_count;
386 	u32 fs_budget_start;
387 	u32 start_ss, last_ss;
388 	u32 start_cs, last_cs;
389 	int i;
390 
391 	start_ss = offset % 8;
392 	fs_budget_start = (start_ss + 1) % 8;
393 
394 	if (sch_ep->ep_type == ISOC_OUT_EP) {
395 		last_ss = start_ss + sch_ep->cs_count - 1;
396 
397 		/*
398 		 * usb_20 spec section11.18:
399 		 * must never schedule Start-Split in Y6
400 		 */
401 		if (!(start_ss == 7 || last_ss < 6))
402 			return -ERANGE;
403 
404 		for (i = 0; i < sch_ep->cs_count; i++)
405 			if (test_bit(offset + i, tt->split_bit_map))
406 				return -ERANGE;
407 
408 	} else {
409 		u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
410 
411 		/*
412 		 * usb_20 spec section11.18:
413 		 * must never schedule Start-Split in Y6
414 		 */
415 		if (start_ss == 6)
416 			return -ERANGE;
417 
418 		/* one uframe for ss + one uframe for idle */
419 		start_cs = (start_ss + 2) % 8;
420 		last_cs = start_cs + cs_count - 1;
421 
422 		if (last_cs > 7)
423 			return -ERANGE;
424 
425 		if (sch_ep->ep_type == ISOC_IN_EP)
426 			extra_cs_count = (last_cs == 7) ? 1 : 2;
427 		else /*  ep_type : INTR IN / INTR OUT */
428 			extra_cs_count = (fs_budget_start == 6) ? 1 : 2;
429 
430 		cs_count += extra_cs_count;
431 		if (cs_count > 7)
432 			cs_count = 7; /* HW limit */
433 
434 		for (i = 0; i < cs_count + 2; i++) {
435 			if (test_bit(offset + i, tt->split_bit_map))
436 				return -ERANGE;
437 		}
438 
439 		sch_ep->cs_count = cs_count;
440 		/* one for ss, the other for idle */
441 		sch_ep->num_budget_microframes = cs_count + 2;
442 
443 		/*
444 		 * if interval=1, maxp >752, num_budge_micoframe is larger
445 		 * than sch_ep->esit, will overstep boundary
446 		 */
447 		if (sch_ep->num_budget_microframes > sch_ep->esit)
448 			sch_ep->num_budget_microframes = sch_ep->esit;
449 	}
450 
451 	return 0;
452 }
453 
454 static void update_sch_tt(struct usb_device *udev,
455 	struct mu3h_sch_ep_info *sch_ep)
456 {
457 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
458 	u32 base, num_esit;
459 	int i, j;
460 
461 	num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
462 	for (i = 0; i < num_esit; i++) {
463 		base = sch_ep->offset + i * sch_ep->esit;
464 		for (j = 0; j < sch_ep->num_budget_microframes; j++)
465 			set_bit(base + j, tt->split_bit_map);
466 	}
467 
468 	list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
469 }
470 
471 static int check_sch_bw(struct usb_device *udev,
472 	struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep)
473 {
474 	u32 offset;
475 	u32 esit;
476 	u32 min_bw;
477 	u32 min_index;
478 	u32 worst_bw;
479 	u32 bw_boundary;
480 	u32 min_num_budget;
481 	u32 min_cs_count;
482 	bool tt_offset_ok = false;
483 	int ret;
484 
485 	esit = sch_ep->esit;
486 
487 	/*
488 	 * Search through all possible schedule microframes.
489 	 * and find a microframe where its worst bandwidth is minimum.
490 	 */
491 	min_bw = ~0;
492 	min_index = 0;
493 	min_cs_count = sch_ep->cs_count;
494 	min_num_budget = sch_ep->num_budget_microframes;
495 	for (offset = 0; offset < esit; offset++) {
496 		if (is_fs_or_ls(udev->speed)) {
497 			ret = check_sch_tt(udev, sch_ep, offset);
498 			if (ret)
499 				continue;
500 			else
501 				tt_offset_ok = true;
502 		}
503 
504 		if ((offset + sch_ep->num_budget_microframes) > sch_ep->esit)
505 			break;
506 
507 		worst_bw = get_max_bw(sch_bw, sch_ep, offset);
508 		if (min_bw > worst_bw) {
509 			min_bw = worst_bw;
510 			min_index = offset;
511 			min_cs_count = sch_ep->cs_count;
512 			min_num_budget = sch_ep->num_budget_microframes;
513 		}
514 		if (min_bw == 0)
515 			break;
516 	}
517 
518 	if (udev->speed == USB_SPEED_SUPER_PLUS)
519 		bw_boundary = SSP_BW_BOUNDARY;
520 	else if (udev->speed == USB_SPEED_SUPER)
521 		bw_boundary = SS_BW_BOUNDARY;
522 	else
523 		bw_boundary = HS_BW_BOUNDARY;
524 
525 	/* check bandwidth */
526 	if (min_bw > bw_boundary)
527 		return -ERANGE;
528 
529 	sch_ep->offset = min_index;
530 	sch_ep->cs_count = min_cs_count;
531 	sch_ep->num_budget_microframes = min_num_budget;
532 
533 	if (is_fs_or_ls(udev->speed)) {
534 		/* all offset for tt is not ok*/
535 		if (!tt_offset_ok)
536 			return -ERANGE;
537 
538 		update_sch_tt(udev, sch_ep);
539 	}
540 
541 	/* update bus bandwidth info */
542 	update_bus_bw(sch_bw, sch_ep, 1);
543 
544 	return 0;
545 }
546 
547 static void destroy_sch_ep(struct usb_device *udev,
548 	struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep)
549 {
550 	/* only release ep bw check passed by check_sch_bw() */
551 	if (sch_ep->allocated)
552 		update_bus_bw(sch_bw, sch_ep, 0);
553 
554 	list_del(&sch_ep->endpoint);
555 
556 	if (sch_ep->sch_tt) {
557 		list_del(&sch_ep->tt_endpoint);
558 		drop_tt(udev);
559 	}
560 	kfree(sch_ep);
561 }
562 
563 static bool need_bw_sch(struct usb_host_endpoint *ep,
564 	enum usb_device_speed speed, int has_tt)
565 {
566 	/* only for periodic endpoints */
567 	if (usb_endpoint_xfer_control(&ep->desc)
568 		|| usb_endpoint_xfer_bulk(&ep->desc))
569 		return false;
570 
571 	/*
572 	 * for LS & FS periodic endpoints which its device is not behind
573 	 * a TT are also ignored, root-hub will schedule them directly,
574 	 * but need set @bpkts field of endpoint context to 1.
575 	 */
576 	if (is_fs_or_ls(speed) && !has_tt)
577 		return false;
578 
579 	/* skip endpoint with zero maxpkt */
580 	if (usb_endpoint_maxp(&ep->desc) == 0)
581 		return false;
582 
583 	return true;
584 }
585 
586 int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
587 {
588 	struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
589 	struct mu3h_sch_bw_info *sch_array;
590 	int num_usb_bus;
591 	int i;
592 
593 	/* ss IN and OUT are separated */
594 	num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
595 
596 	sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
597 	if (sch_array == NULL)
598 		return -ENOMEM;
599 
600 	for (i = 0; i < num_usb_bus; i++)
601 		INIT_LIST_HEAD(&sch_array[i].bw_ep_list);
602 
603 	mtk->sch_array = sch_array;
604 
605 	INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
606 
607 	return 0;
608 }
609 EXPORT_SYMBOL_GPL(xhci_mtk_sch_init);
610 
611 void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
612 {
613 	kfree(mtk->sch_array);
614 }
615 EXPORT_SYMBOL_GPL(xhci_mtk_sch_exit);
616 
617 int xhci_mtk_add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
618 		struct usb_host_endpoint *ep)
619 {
620 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
621 	struct xhci_hcd *xhci;
622 	struct xhci_ep_ctx *ep_ctx;
623 	struct xhci_slot_ctx *slot_ctx;
624 	struct xhci_virt_device *virt_dev;
625 	struct mu3h_sch_ep_info *sch_ep;
626 	unsigned int ep_index;
627 
628 	xhci = hcd_to_xhci(hcd);
629 	virt_dev = xhci->devs[udev->slot_id];
630 	ep_index = xhci_get_endpoint_index(&ep->desc);
631 	slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
632 	ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
633 
634 	xhci_dbg(xhci, "%s() type:%d, speed:%d, mpkt:%d, dir:%d, ep:%p\n",
635 		__func__, usb_endpoint_type(&ep->desc), udev->speed,
636 		usb_endpoint_maxp(&ep->desc),
637 		usb_endpoint_dir_in(&ep->desc), ep);
638 
639 	if (!need_bw_sch(ep, udev->speed, slot_ctx->tt_info & TT_SLOT)) {
640 		/*
641 		 * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
642 		 * device does not connected through an external HS hub
643 		 */
644 		if (usb_endpoint_xfer_int(&ep->desc)
645 			|| usb_endpoint_xfer_isoc(&ep->desc))
646 			ep_ctx->reserved[0] |= cpu_to_le32(EP_BPKTS(1));
647 
648 		return 0;
649 	}
650 
651 	sch_ep = create_sch_ep(udev, ep, ep_ctx);
652 	if (IS_ERR_OR_NULL(sch_ep))
653 		return -ENOMEM;
654 
655 	setup_sch_info(udev, ep_ctx, sch_ep);
656 
657 	list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
658 
659 	return 0;
660 }
661 EXPORT_SYMBOL_GPL(xhci_mtk_add_ep_quirk);
662 
663 void xhci_mtk_drop_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_slot_ctx *slot_ctx;
669 	struct xhci_virt_device *virt_dev;
670 	struct mu3h_sch_bw_info *sch_array;
671 	struct mu3h_sch_bw_info *sch_bw;
672 	struct mu3h_sch_ep_info *sch_ep, *tmp;
673 	int bw_index;
674 
675 	xhci = hcd_to_xhci(hcd);
676 	virt_dev = xhci->devs[udev->slot_id];
677 	slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
678 	sch_array = mtk->sch_array;
679 
680 	xhci_dbg(xhci, "%s() type:%d, speed:%d, mpks:%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 		return;
687 
688 	bw_index = get_bw_index(xhci, udev, ep);
689 	sch_bw = &sch_array[bw_index];
690 
691 	list_for_each_entry_safe(sch_ep, tmp, &sch_bw->bw_ep_list, endpoint) {
692 		if (sch_ep->ep == ep) {
693 			destroy_sch_ep(udev, sch_bw, sch_ep);
694 			break;
695 		}
696 	}
697 }
698 EXPORT_SYMBOL_GPL(xhci_mtk_drop_ep_quirk);
699 
700 int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
701 {
702 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
703 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
704 	struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
705 	struct mu3h_sch_bw_info *sch_bw;
706 	struct mu3h_sch_ep_info *sch_ep, *tmp;
707 	int bw_index, ret;
708 
709 	xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
710 
711 	list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
712 		bw_index = get_bw_index(xhci, udev, sch_ep->ep);
713 		sch_bw = &mtk->sch_array[bw_index];
714 
715 		ret = check_sch_bw(udev, sch_bw, sch_ep);
716 		if (ret) {
717 			xhci_err(xhci, "Not enough bandwidth!\n");
718 			return -ENOSPC;
719 		}
720 	}
721 
722 	list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint) {
723 		struct xhci_ep_ctx *ep_ctx;
724 		struct usb_host_endpoint *ep = sch_ep->ep;
725 		unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
726 
727 		bw_index = get_bw_index(xhci, udev, ep);
728 		sch_bw = &mtk->sch_array[bw_index];
729 
730 		list_move_tail(&sch_ep->endpoint, &sch_bw->bw_ep_list);
731 
732 		ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
733 		ep_ctx->reserved[0] |= cpu_to_le32(EP_BPKTS(sch_ep->pkts)
734 			| EP_BCSCOUNT(sch_ep->cs_count)
735 			| EP_BBM(sch_ep->burst_mode));
736 		ep_ctx->reserved[1] |= cpu_to_le32(EP_BOFFSET(sch_ep->offset)
737 			| EP_BREPEAT(sch_ep->repeat));
738 
739 		xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
740 			sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
741 			sch_ep->offset, sch_ep->repeat);
742 	}
743 
744 	return xhci_check_bandwidth(hcd, udev);
745 }
746 EXPORT_SYMBOL_GPL(xhci_mtk_check_bandwidth);
747 
748 void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
749 {
750 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
751 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
752 	struct mu3h_sch_bw_info *sch_bw;
753 	struct mu3h_sch_ep_info *sch_ep, *tmp;
754 	int bw_index;
755 
756 	xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
757 
758 	list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint) {
759 		bw_index = get_bw_index(xhci, udev, sch_ep->ep);
760 		sch_bw = &mtk->sch_array[bw_index];
761 		destroy_sch_ep(udev, sch_bw, sch_ep);
762 	}
763 
764 	xhci_reset_bandwidth(hcd, udev);
765 }
766 EXPORT_SYMBOL_GPL(xhci_mtk_reset_bandwidth);
767