xref: /linux/drivers/usb/host/xhci-mtk-sch.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
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 #define LS_PAYLOAD_MAX 18
23 /* section 11.18.1, per fs frame */
24 #define FS_BW_BOUNDARY	1157
25 #define LS_BW_BOUNDARY	144
26 
27 /*
28  * max number of microframes for split transfer, assume extra-cs budget is 0
29  * for fs isoc in : 1 ss + 1 idle + 6 cs (roundup(1023/188))
30  */
31 #define TT_MICROFRAMES_MAX	8
32 /* offset from SS for fs/ls isoc/intr ep (ss + idle) */
33 #define CS_OFFSET	2
34 
35 #define DBG_BUF_EN	64
36 
37 /* schedule error type */
38 #define ESCH_SS_Y6		1001
39 #define ESCH_SS_OVERLAP		1002
40 #define ESCH_CS_OVERFLOW	1003
41 #define ESCH_BW_OVERFLOW	1004
42 #define ESCH_FIXME		1005
43 
44 /* mtk scheduler bitmasks */
45 #define EP_BPKTS(p)	((p) & 0x7f)
46 #define EP_BCSCOUNT(p)	(((p) & 0x7) << 8)
47 #define EP_BBM(p)	((p) << 11)
48 #define EP_BOFFSET(p)	((p) & 0x3fff)
49 #define EP_BREPEAT(p)	(((p) & 0x7fff) << 16)
50 
sch_error_string(int err_num)51 static char *sch_error_string(int err_num)
52 {
53 	switch (err_num) {
54 	case ESCH_SS_Y6:
55 		return "Can't schedule Start-Split in Y6";
56 	case ESCH_SS_OVERLAP:
57 		return "Can't find a suitable Start-Split location";
58 	case ESCH_CS_OVERFLOW:
59 		return "The last Complete-Split is greater than 7";
60 	case ESCH_BW_OVERFLOW:
61 		return "Bandwidth exceeds the maximum limit";
62 	case ESCH_FIXME:
63 		return "FIXME, to be resolved";
64 	default:
65 		return "Unknown";
66 	}
67 }
68 
is_fs_or_ls(enum usb_device_speed speed)69 static int is_fs_or_ls(enum usb_device_speed speed)
70 {
71 	return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
72 }
73 
74 static const char *
decode_ep(struct usb_host_endpoint * ep,enum usb_device_speed speed)75 decode_ep(struct usb_host_endpoint *ep, enum usb_device_speed speed)
76 {
77 	static char buf[DBG_BUF_EN];
78 	struct usb_endpoint_descriptor *epd = &ep->desc;
79 	unsigned int interval;
80 	const char *unit;
81 
82 	interval = usb_decode_interval(epd, speed);
83 	if (interval % 1000) {
84 		unit = "us";
85 	} else {
86 		unit = "ms";
87 		interval /= 1000;
88 	}
89 
90 	snprintf(buf, DBG_BUF_EN, "%s ep%d%s %s, mpkt:%d, interval:%d/%d%s",
91 		 usb_speed_string(speed), usb_endpoint_num(epd),
92 		 usb_endpoint_dir_in(epd) ? "in" : "out",
93 		 usb_ep_type_string(usb_endpoint_type(epd)),
94 		 usb_endpoint_maxp(epd), epd->bInterval, interval, unit);
95 
96 	return buf;
97 }
98 
get_bw_boundary(enum usb_device_speed speed)99 static u32 get_bw_boundary(enum usb_device_speed speed)
100 {
101 	u32 boundary;
102 
103 	switch (speed) {
104 	case USB_SPEED_SUPER_PLUS:
105 		boundary = SSP_BW_BOUNDARY;
106 		break;
107 	case USB_SPEED_SUPER:
108 		boundary = SS_BW_BOUNDARY;
109 		break;
110 	default:
111 		boundary = HS_BW_BOUNDARY;
112 		break;
113 	}
114 
115 	return boundary;
116 }
117 
118 /*
119 * get the bandwidth domain which @ep belongs to.
120 *
121 * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
122 * each HS root port is treated as a single bandwidth domain,
123 * but each SS root port is treated as two bandwidth domains, one for IN eps,
124 * one for OUT eps.
125 */
126 static struct mu3h_sch_bw_info *
get_bw_info(struct xhci_hcd_mtk * mtk,struct usb_device * udev,struct usb_host_endpoint * ep)127 get_bw_info(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
128 	    struct usb_host_endpoint *ep)
129 {
130 	struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
131 	struct xhci_virt_device *virt_dev;
132 	int bw_index;
133 
134 	virt_dev = xhci->devs[udev->slot_id];
135 	if (!virt_dev->rhub_port) {
136 		WARN_ONCE(1, "%s invalid rhub port\n", dev_name(&udev->dev));
137 		return NULL;
138 	}
139 
140 	if (udev->speed >= USB_SPEED_SUPER) {
141 		if (usb_endpoint_dir_out(&ep->desc))
142 			bw_index = (virt_dev->rhub_port->hw_portnum) * 2;
143 		else
144 			bw_index = (virt_dev->rhub_port->hw_portnum) * 2 + 1;
145 	} else {
146 		/* add one more for each SS port */
147 		bw_index = virt_dev->rhub_port->hw_portnum + xhci->usb3_rhub.num_ports;
148 	}
149 
150 	return &mtk->sch_array[bw_index];
151 }
152 
get_esit(struct xhci_ep_ctx * ep_ctx)153 static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
154 {
155 	u32 esit;
156 
157 	esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
158 	if (esit > XHCI_MTK_MAX_ESIT)
159 		esit = XHCI_MTK_MAX_ESIT;
160 
161 	return esit;
162 }
163 
find_tt(struct usb_device * udev)164 static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
165 {
166 	struct usb_tt *utt = udev->tt;
167 	struct mu3h_sch_tt *tt, **tt_index, **ptt;
168 	bool allocated_index = false;
169 
170 	if (!utt)
171 		return NULL;	/* Not below a TT */
172 
173 	/*
174 	 * Find/create our data structure.
175 	 * For hubs with a single TT, we get it directly.
176 	 * For hubs with multiple TTs, there's an extra level of pointers.
177 	 */
178 	tt_index = NULL;
179 	if (utt->multi) {
180 		tt_index = utt->hcpriv;
181 		if (!tt_index) {	/* Create the index array */
182 			tt_index = kzalloc_objs(*tt_index, utt->hub->maxchild);
183 			if (!tt_index)
184 				return ERR_PTR(-ENOMEM);
185 			utt->hcpriv = tt_index;
186 			allocated_index = true;
187 		}
188 		ptt = &tt_index[udev->ttport - 1];
189 	} else {
190 		ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
191 	}
192 
193 	tt = *ptt;
194 	if (!tt) {	/* Create the mu3h_sch_tt */
195 		tt = kzalloc_obj(*tt);
196 		if (!tt) {
197 			if (allocated_index) {
198 				utt->hcpriv = NULL;
199 				kfree(tt_index);
200 			}
201 			return ERR_PTR(-ENOMEM);
202 		}
203 		INIT_LIST_HEAD(&tt->ep_list);
204 		*ptt = tt;
205 	}
206 
207 	return tt;
208 }
209 
210 /* Release the TT above udev, if it's not in use */
drop_tt(struct usb_device * udev)211 static void drop_tt(struct usb_device *udev)
212 {
213 	struct usb_tt *utt = udev->tt;
214 	struct mu3h_sch_tt *tt, **tt_index, **ptt;
215 	int i, cnt;
216 
217 	if (!utt || !utt->hcpriv)
218 		return;		/* Not below a TT, or never allocated */
219 
220 	cnt = 0;
221 	if (utt->multi) {
222 		tt_index = utt->hcpriv;
223 		ptt = &tt_index[udev->ttport - 1];
224 		/*  How many entries are left in tt_index? */
225 		for (i = 0; i < utt->hub->maxchild; ++i)
226 			cnt += !!tt_index[i];
227 	} else {
228 		tt_index = NULL;
229 		ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
230 	}
231 
232 	tt = *ptt;
233 	if (!tt || !list_empty(&tt->ep_list))
234 		return;		/* never allocated , or still in use*/
235 
236 	*ptt = NULL;
237 	kfree(tt);
238 
239 	if (cnt == 1) {
240 		utt->hcpriv = NULL;
241 		kfree(tt_index);
242 	}
243 }
244 
245 static struct mu3h_sch_ep_info *
create_sch_ep(struct xhci_hcd_mtk * mtk,struct usb_device * udev,struct usb_host_endpoint * ep,struct xhci_ep_ctx * ep_ctx)246 create_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
247 	      struct usb_host_endpoint *ep, struct xhci_ep_ctx *ep_ctx)
248 {
249 	struct mu3h_sch_ep_info *sch_ep;
250 	struct mu3h_sch_bw_info *bw_info;
251 	struct mu3h_sch_tt *tt = NULL;
252 	u32 len;
253 
254 	bw_info = get_bw_info(mtk, udev, ep);
255 	if (!bw_info)
256 		return ERR_PTR(-ENODEV);
257 
258 	if (is_fs_or_ls(udev->speed))
259 		len = TT_MICROFRAMES_MAX;
260 	else if ((udev->speed >= USB_SPEED_SUPER) &&
261 		 usb_endpoint_xfer_isoc(&ep->desc))
262 		len = get_esit(ep_ctx);
263 	else
264 		len = 1;
265 
266 	sch_ep = kzalloc_flex(*sch_ep, bw_budget_table, len);
267 	if (!sch_ep)
268 		return ERR_PTR(-ENOMEM);
269 
270 	if (is_fs_or_ls(udev->speed)) {
271 		tt = find_tt(udev);
272 		if (IS_ERR(tt)) {
273 			kfree(sch_ep);
274 			return ERR_PTR(-ENOMEM);
275 		}
276 	}
277 
278 	sch_ep->bw_info = bw_info;
279 	sch_ep->sch_tt = tt;
280 	sch_ep->ep = ep;
281 	sch_ep->speed = udev->speed;
282 	INIT_LIST_HEAD(&sch_ep->endpoint);
283 	INIT_LIST_HEAD(&sch_ep->tt_endpoint);
284 	INIT_HLIST_NODE(&sch_ep->hentry);
285 
286 	return sch_ep;
287 }
288 
setup_sch_info(struct xhci_ep_ctx * ep_ctx,struct mu3h_sch_ep_info * sch_ep)289 static void setup_sch_info(struct xhci_ep_ctx *ep_ctx,
290 			   struct mu3h_sch_ep_info *sch_ep)
291 {
292 	u32 ep_type;
293 	u32 maxpkt;
294 	u32 max_burst;
295 	u32 mult;
296 	u32 esit_pkts;
297 	u32 max_esit_payload;
298 	u32 bw_per_microframe;
299 	u32 *bwb_table;
300 	int i;
301 
302 	bwb_table = sch_ep->bw_budget_table;
303 	ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
304 	maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
305 	max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
306 	mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
307 	max_esit_payload =
308 		(CTX_TO_MAX_ESIT_PAYLOAD_HI(
309 			le32_to_cpu(ep_ctx->ep_info)) << 16) |
310 		 CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
311 
312 	sch_ep->esit = get_esit(ep_ctx);
313 	sch_ep->num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
314 	sch_ep->ep_type = ep_type;
315 	sch_ep->maxpkt = maxpkt;
316 	sch_ep->offset = 0;
317 	sch_ep->burst_mode = 0;
318 	sch_ep->repeat = 0;
319 
320 	if (sch_ep->speed == USB_SPEED_HIGH) {
321 		sch_ep->cs_count = 0;
322 
323 		/*
324 		 * usb_20 spec section5.9
325 		 * a single microframe is enough for HS synchromous endpoints
326 		 * in a interval
327 		 */
328 		sch_ep->num_budget_microframes = 1;
329 
330 		/*
331 		 * xHCI spec section6.2.3.4
332 		 * @max_burst is the number of additional transactions
333 		 * opportunities per microframe
334 		 */
335 		sch_ep->pkts = max_burst + 1;
336 		bwb_table[0] = maxpkt * sch_ep->pkts;
337 	} else if (sch_ep->speed >= USB_SPEED_SUPER) {
338 		/* usb3_r1 spec section4.4.7 & 4.4.8 */
339 		sch_ep->cs_count = 0;
340 		sch_ep->burst_mode = 1;
341 		/*
342 		 * some device's (d)wBytesPerInterval is set as 0,
343 		 * then max_esit_payload is 0, so evaluate esit_pkts from
344 		 * mult and burst
345 		 */
346 		esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
347 		if (esit_pkts == 0)
348 			esit_pkts = (mult + 1) * (max_burst + 1);
349 
350 		if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
351 			sch_ep->pkts = esit_pkts;
352 			sch_ep->num_budget_microframes = 1;
353 			bwb_table[0] = maxpkt * sch_ep->pkts;
354 		}
355 
356 		if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
357 
358 			if (sch_ep->esit == 1)
359 				sch_ep->pkts = esit_pkts;
360 			else if (esit_pkts <= sch_ep->esit)
361 				sch_ep->pkts = 1;
362 			else
363 				sch_ep->pkts = roundup_pow_of_two(esit_pkts)
364 					/ sch_ep->esit;
365 
366 			sch_ep->num_budget_microframes =
367 				DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
368 
369 			sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
370 			bw_per_microframe = maxpkt * sch_ep->pkts;
371 
372 			for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
373 				bwb_table[i] = bw_per_microframe;
374 
375 			/* last one <= bw_per_microframe */
376 			bwb_table[i] = maxpkt * esit_pkts - i * bw_per_microframe;
377 		}
378 	} else if (is_fs_or_ls(sch_ep->speed)) {
379 		sch_ep->pkts = 1; /* at most one packet for each microframe */
380 
381 		/*
382 		 * @cs_count will be updated to add extra-cs when
383 		 * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
384 		 * @maxpkt <= 1023;
385 		 */
386 		sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
387 		sch_ep->num_budget_microframes = sch_ep->cs_count;
388 
389 		/* init budget table */
390 		if (ep_type == ISOC_OUT_EP) {
391 			for (i = 0; i < sch_ep->cs_count - 1; i++)
392 				bwb_table[i] = FS_PAYLOAD_MAX;
393 
394 			bwb_table[i] = maxpkt - i * FS_PAYLOAD_MAX;
395 		} else if (ep_type == INT_OUT_EP) {
396 			/* only first one used (maxpkt <= 64), others zero */
397 			bwb_table[0] = maxpkt;
398 		} else { /* INT_IN_EP or ISOC_IN_EP */
399 			bwb_table[0] = 0; /* start split */
400 			bwb_table[1] = 0; /* idle */
401 			/*
402 			 * @cs_count will be updated according to cs position
403 			 * (add 1 or 2 extra-cs), but assume only first
404 			 * @num_budget_microframes elements will be used later,
405 			 * although in fact it does not (extra-cs budget many receive
406 			 * some data for IN ep);
407 			 * @cs_count is 1 for INT_IN_EP (maxpkt <= 64);
408 			 */
409 			for (i = 0; i < sch_ep->cs_count - 1; i++)
410 				bwb_table[i + CS_OFFSET] = FS_PAYLOAD_MAX;
411 
412 			bwb_table[i + CS_OFFSET] = maxpkt - i * FS_PAYLOAD_MAX;
413 			/* ss + idle */
414 			sch_ep->num_budget_microframes += CS_OFFSET;
415 		}
416 	}
417 }
418 
419 /* 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)420 static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
421 	struct mu3h_sch_ep_info *sch_ep, u32 offset)
422 {
423 	u32 max_bw = 0;
424 	u32 bw;
425 	int i, j, k;
426 
427 	for (i = 0; i < sch_ep->num_esit; i++) {
428 		u32 base = offset + i * sch_ep->esit;
429 
430 		for (j = 0; j < sch_ep->num_budget_microframes; j++) {
431 			k = XHCI_MTK_BW_INDEX(base + j);
432 			bw = sch_bw->bus_bw[k] + sch_ep->bw_budget_table[j];
433 			if (bw > max_bw)
434 				max_bw = bw;
435 		}
436 	}
437 	return max_bw;
438 }
439 
440 /*
441  * for OUT: get first SS consumed bw;
442  * for IN: get first CS consumed bw;
443  */
get_fs_bw(struct mu3h_sch_ep_info * sch_ep,int offset)444 static u16 get_fs_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
445 {
446 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
447 	u16 fs_bw;
448 
449 	if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
450 		fs_bw = tt->fs_bus_bw_out[XHCI_MTK_BW_INDEX(offset)];
451 	else	/* skip ss + idle */
452 		fs_bw = tt->fs_bus_bw_in[XHCI_MTK_BW_INDEX(offset + CS_OFFSET)];
453 
454 	return fs_bw;
455 }
456 
update_bus_bw(struct mu3h_sch_bw_info * sch_bw,struct mu3h_sch_ep_info * sch_ep,bool used)457 static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
458 	struct mu3h_sch_ep_info *sch_ep, bool used)
459 {
460 	u32 base;
461 	int i, j, k;
462 
463 	for (i = 0; i < sch_ep->num_esit; i++) {
464 		base = sch_ep->offset + i * sch_ep->esit;
465 		for (j = 0; j < sch_ep->num_budget_microframes; j++) {
466 			k = XHCI_MTK_BW_INDEX(base + j);
467 			if (used)
468 				sch_bw->bus_bw[k] += sch_ep->bw_budget_table[j];
469 			else
470 				sch_bw->bus_bw[k] -= sch_ep->bw_budget_table[j];
471 		}
472 	}
473 }
474 
check_ls_budget_microframes(struct mu3h_sch_ep_info * sch_ep,int offset)475 static int check_ls_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
476 {
477 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
478 	int i;
479 
480 	if (sch_ep->speed != USB_SPEED_LOW)
481 		return 0;
482 
483 	if (sch_ep->ep_type == INT_OUT_EP)
484 		i = XHCI_MTK_BW_INDEX(offset);
485 	else if (sch_ep->ep_type == INT_IN_EP)
486 		i = XHCI_MTK_BW_INDEX(offset + CS_OFFSET); /* skip ss + idle */
487 	else
488 		return -EINVAL;
489 
490 	if (tt->ls_bus_bw[i] + sch_ep->maxpkt > LS_PAYLOAD_MAX)
491 		return -ESCH_BW_OVERFLOW;
492 
493 	return 0;
494 }
495 
check_fs_budget_microframes(struct mu3h_sch_ep_info * sch_ep,int offset)496 static int check_fs_budget_microframes(struct mu3h_sch_ep_info *sch_ep, int offset)
497 {
498 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
499 	u32 tmp;
500 	int i, k;
501 
502 	/*
503 	 * for OUT eps, will transfer exactly assigned length of data,
504 	 * so can't allocate more than 188 bytes;
505 	 * but it's not for IN eps, usually it can't receive full
506 	 * 188 bytes in a uframe, if it not assign full 188 bytes,
507 	 * can add another one;
508 	 */
509 	for (i = 0; i < sch_ep->num_budget_microframes; i++) {
510 		k = XHCI_MTK_BW_INDEX(offset + i);
511 		if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
512 			tmp = tt->fs_bus_bw_out[k] + sch_ep->bw_budget_table[i];
513 		else /* ep_type : ISOC IN / INTR IN */
514 			tmp = tt->fs_bus_bw_in[k];
515 
516 		if (tmp > FS_PAYLOAD_MAX)
517 			return -ESCH_BW_OVERFLOW;
518 	}
519 
520 	return 0;
521 }
522 
check_fs_budget_frames(struct mu3h_sch_ep_info * sch_ep,int offset)523 static int check_fs_budget_frames(struct mu3h_sch_ep_info *sch_ep, int offset)
524 {
525 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
526 	u32 head, tail;
527 	int i, j, k;
528 
529 	/* bugdet scheduled may cross at most two fs frames */
530 	j = XHCI_MTK_BW_INDEX(offset) / UFRAMES_PER_FRAME;
531 	k = XHCI_MTK_BW_INDEX(offset + sch_ep->num_budget_microframes - 1) / UFRAMES_PER_FRAME;
532 
533 	if (j != k) {
534 		head = tt->fs_frame_bw[j];
535 		tail = tt->fs_frame_bw[k];
536 	} else {
537 		head = tt->fs_frame_bw[j];
538 		tail = 0;
539 	}
540 
541 	j = roundup(offset, UFRAMES_PER_FRAME);
542 	for (i = 0; i < sch_ep->num_budget_microframes; i++) {
543 		if ((offset + i) < j)
544 			head += sch_ep->bw_budget_table[i];
545 		else
546 			tail += sch_ep->bw_budget_table[i];
547 	}
548 
549 	if (head > FS_BW_BOUNDARY || tail > FS_BW_BOUNDARY)
550 		return -ESCH_BW_OVERFLOW;
551 
552 	return 0;
553 }
554 
check_fs_bus_bw(struct mu3h_sch_ep_info * sch_ep,int offset)555 static int check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
556 {
557 	int i, base;
558 	int ret = 0;
559 
560 	for (i = 0; i < sch_ep->num_esit; i++) {
561 		base = offset + i * sch_ep->esit;
562 
563 		ret = check_ls_budget_microframes(sch_ep, base);
564 		if (ret)
565 			goto err;
566 
567 		ret = check_fs_budget_microframes(sch_ep, base);
568 		if (ret)
569 			goto err;
570 
571 		ret = check_fs_budget_frames(sch_ep, base);
572 		if (ret)
573 			goto err;
574 	}
575 
576 err:
577 	return ret;
578 }
579 
check_ss_and_cs(struct mu3h_sch_ep_info * sch_ep,u32 offset)580 static int check_ss_and_cs(struct mu3h_sch_ep_info *sch_ep, u32 offset)
581 {
582 	u32 start_ss, last_ss;
583 	u32 start_cs, last_cs;
584 
585 	start_ss = offset % UFRAMES_PER_FRAME;
586 
587 	if (sch_ep->ep_type == ISOC_OUT_EP) {
588 		last_ss = start_ss + sch_ep->cs_count - 1;
589 
590 		/*
591 		 * usb_20 spec section11.18:
592 		 * must never schedule Start-Split in Y6
593 		 */
594 		if (!(start_ss == 7 || last_ss < 6))
595 			return -ESCH_SS_Y6;
596 
597 	} else {
598 		/* maxpkt <= 1023, cs <= 6 */
599 		u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
600 
601 		/*
602 		 * usb_20 spec section11.18:
603 		 * must never schedule Start-Split in Y6
604 		 */
605 		if (start_ss == 6)
606 			return -ESCH_SS_Y6;
607 
608 		/* one uframe for ss + one uframe for idle */
609 		start_cs = (start_ss + CS_OFFSET) % UFRAMES_PER_FRAME;
610 		last_cs = start_cs + cs_count - 1;
611 		if (last_cs > 7)
612 			return -ESCH_CS_OVERFLOW;
613 
614 		/* add extra-cs */
615 		cs_count += (last_cs == 7) ? 1 : 2;
616 		if (cs_count > 7)
617 			cs_count = 7; /* HW limit */
618 
619 		sch_ep->cs_count = cs_count;
620 
621 	}
622 
623 	return 0;
624 }
625 
626 /*
627  * when isoc-out transfers 188 bytes in a uframe, and send isoc/intr's
628  * ss token in the uframe, may cause 'bit stuff error' in downstream
629  * port;
630  * when isoc-out transfer less than 188 bytes in a uframe, shall send
631  * isoc-in's ss after isoc-out's ss (but hw can't ensure the sequence,
632  * so just avoid overlap).
633  */
check_isoc_ss_overlap(struct mu3h_sch_ep_info * sch_ep,u32 offset)634 static int check_isoc_ss_overlap(struct mu3h_sch_ep_info *sch_ep, u32 offset)
635 {
636 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
637 	int base;
638 	int i, j, k;
639 
640 	if (!tt)
641 		return 0;
642 
643 	for (i = 0; i < sch_ep->num_esit; i++) {
644 		base = offset + i * sch_ep->esit;
645 
646 		if (sch_ep->ep_type == ISOC_OUT_EP) {
647 			for (j = 0; j < sch_ep->num_budget_microframes; j++) {
648 				k = XHCI_MTK_BW_INDEX(base + j);
649 				if (tt->in_ss_cnt[k])
650 					return -ESCH_SS_OVERLAP;
651 			}
652 		} else if (sch_ep->ep_type == ISOC_IN_EP || sch_ep->ep_type == INT_IN_EP) {
653 			k = XHCI_MTK_BW_INDEX(base);
654 			/* only check IN's ss */
655 			if (tt->fs_bus_bw_out[k])
656 				return -ESCH_SS_OVERLAP;
657 		}
658 	}
659 
660 	return 0;
661 }
662 
check_sch_tt_budget(struct mu3h_sch_ep_info * sch_ep,u32 offset)663 static int check_sch_tt_budget(struct mu3h_sch_ep_info *sch_ep, u32 offset)
664 {
665 	int ret;
666 
667 	ret = check_ss_and_cs(sch_ep, offset);
668 	if (ret)
669 		return ret;
670 
671 	ret = check_isoc_ss_overlap(sch_ep, offset);
672 	if (ret)
673 		return ret;
674 
675 	return check_fs_bus_bw(sch_ep, offset);
676 }
677 
678 /* allocate microframes in the ls/fs frame */
alloc_sch_portion_of_frame(struct mu3h_sch_ep_info * sch_ep)679 static int alloc_sch_portion_of_frame(struct mu3h_sch_ep_info *sch_ep)
680 {
681 	struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
682 	const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
683 	u32 bw_max, fs_bw_min;
684 	u32 offset, offset_min;
685 	u16 fs_bw;
686 	int frames;
687 	int i, j;
688 	int ret;
689 
690 	frames = sch_ep->esit / UFRAMES_PER_FRAME;
691 
692 	for (i = 0; i < UFRAMES_PER_FRAME; i++) {
693 		fs_bw_min = FS_PAYLOAD_MAX;
694 		offset_min = XHCI_MTK_MAX_ESIT;
695 
696 		for (j = 0; j < frames; j++) {
697 			offset = (i + j * UFRAMES_PER_FRAME) % sch_ep->esit;
698 
699 			ret = check_sch_tt_budget(sch_ep, offset);
700 			if (ret)
701 				continue;
702 
703 			/* check hs bw domain */
704 			bw_max = get_max_bw(sch_bw, sch_ep, offset);
705 			if (bw_max > bw_boundary) {
706 				ret = -ESCH_BW_OVERFLOW;
707 				continue;
708 			}
709 
710 			/* use best-fit between frames */
711 			fs_bw = get_fs_bw(sch_ep, offset);
712 			if (fs_bw < fs_bw_min) {
713 				fs_bw_min = fs_bw;
714 				offset_min = offset;
715 			}
716 
717 			if (!fs_bw_min)
718 				break;
719 		}
720 
721 		/* use first-fit between microframes in a frame */
722 		if (offset_min < XHCI_MTK_MAX_ESIT)
723 			break;
724 	}
725 
726 	if (offset_min == XHCI_MTK_MAX_ESIT)
727 		return -ESCH_BW_OVERFLOW;
728 
729 	sch_ep->offset = offset_min;
730 
731 	return 0;
732 }
733 
update_sch_tt(struct mu3h_sch_ep_info * sch_ep,bool used)734 static void update_sch_tt(struct mu3h_sch_ep_info *sch_ep, bool used)
735 {
736 	struct mu3h_sch_tt *tt = sch_ep->sch_tt;
737 	u16 *fs_bus_bw;
738 	u32 base;
739 	int i, j, k, f;
740 
741 	if (sch_ep->ep_type == ISOC_OUT_EP || sch_ep->ep_type == INT_OUT_EP)
742 		fs_bus_bw = tt->fs_bus_bw_out;
743 	else
744 		fs_bus_bw = tt->fs_bus_bw_in;
745 
746 	for (i = 0; i < sch_ep->num_esit; i++) {
747 		base = sch_ep->offset + i * sch_ep->esit;
748 
749 		for (j = 0; j < sch_ep->num_budget_microframes; j++) {
750 			k = XHCI_MTK_BW_INDEX(base + j);
751 			f = k / UFRAMES_PER_FRAME;
752 			if (used) {
753 				if (sch_ep->speed == USB_SPEED_LOW)
754 					tt->ls_bus_bw[k] += (u8)sch_ep->bw_budget_table[j];
755 
756 				fs_bus_bw[k] += (u16)sch_ep->bw_budget_table[j];
757 				tt->fs_frame_bw[f] += (u16)sch_ep->bw_budget_table[j];
758 			} else {
759 				if (sch_ep->speed == USB_SPEED_LOW)
760 					tt->ls_bus_bw[k] -= (u8)sch_ep->bw_budget_table[j];
761 
762 				fs_bus_bw[k] -= (u16)sch_ep->bw_budget_table[j];
763 				tt->fs_frame_bw[f] -= (u16)sch_ep->bw_budget_table[j];
764 			}
765 		}
766 
767 		if (sch_ep->ep_type == ISOC_IN_EP || sch_ep->ep_type == INT_IN_EP) {
768 			k = XHCI_MTK_BW_INDEX(base);
769 			if (used)
770 				tt->in_ss_cnt[k]++;
771 			else
772 				tt->in_ss_cnt[k]--;
773 		}
774 	}
775 
776 	if (used)
777 		list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
778 	else
779 		list_del(&sch_ep->tt_endpoint);
780 }
781 
load_ep_bw(struct mu3h_sch_bw_info * sch_bw,struct mu3h_sch_ep_info * sch_ep,bool loaded)782 static int load_ep_bw(struct mu3h_sch_bw_info *sch_bw,
783 		      struct mu3h_sch_ep_info *sch_ep, bool loaded)
784 {
785 	if (sch_ep->sch_tt)
786 		update_sch_tt(sch_ep, loaded);
787 
788 	/* update bus bandwidth info */
789 	update_bus_bw(sch_bw, sch_ep, loaded);
790 	sch_ep->allocated = loaded;
791 
792 	return 0;
793 }
794 
795 /* allocate microframes for hs/ss/ssp */
alloc_sch_microframes(struct mu3h_sch_ep_info * sch_ep)796 static int alloc_sch_microframes(struct mu3h_sch_ep_info *sch_ep)
797 {
798 	struct mu3h_sch_bw_info *sch_bw = sch_ep->bw_info;
799 	const u32 bw_boundary = get_bw_boundary(sch_ep->speed);
800 	u32 offset;
801 	u32 worst_bw;
802 	u32 min_bw = ~0;
803 	int min_index = -1;
804 
805 	/*
806 	 * Search through all possible schedule microframes.
807 	 * and find a microframe where its worst bandwidth is minimum.
808 	 */
809 	for (offset = 0; offset < sch_ep->esit; offset++) {
810 
811 		worst_bw = get_max_bw(sch_bw, sch_ep, offset);
812 		if (worst_bw > bw_boundary)
813 			continue;
814 
815 		if (min_bw > worst_bw) {
816 			min_bw = worst_bw;
817 			min_index = offset;
818 		}
819 	}
820 
821 	if (min_index < 0)
822 		return -ESCH_BW_OVERFLOW;
823 
824 	sch_ep->offset = min_index;
825 
826 	return 0;
827 }
828 
check_sch_bw(struct mu3h_sch_ep_info * sch_ep)829 static int check_sch_bw(struct mu3h_sch_ep_info *sch_ep)
830 {
831 	int ret;
832 
833 	if (sch_ep->sch_tt)
834 		ret = alloc_sch_portion_of_frame(sch_ep);
835 	else
836 		ret = alloc_sch_microframes(sch_ep);
837 
838 	if (ret)
839 		return ret;
840 
841 	return load_ep_bw(sch_ep->bw_info, sch_ep, true);
842 }
843 
destroy_sch_ep(struct xhci_hcd_mtk * mtk,struct usb_device * udev,struct mu3h_sch_ep_info * sch_ep)844 static void destroy_sch_ep(struct xhci_hcd_mtk *mtk, struct usb_device *udev,
845 			   struct mu3h_sch_ep_info *sch_ep)
846 {
847 	/* only release ep bw check passed by check_sch_bw() */
848 	if (sch_ep->allocated)
849 		load_ep_bw(sch_ep->bw_info, sch_ep, false);
850 
851 	if (sch_ep->sch_tt)
852 		drop_tt(udev);
853 
854 	list_del(&sch_ep->endpoint);
855 	hlist_del(&sch_ep->hentry);
856 	kfree(sch_ep);
857 }
858 
need_bw_sch(struct usb_device * udev,struct usb_host_endpoint * ep)859 static bool need_bw_sch(struct usb_device *udev,
860 			struct usb_host_endpoint *ep)
861 {
862 	bool has_tt = udev->tt && udev->tt->hub->parent;
863 
864 	/* only for periodic endpoints */
865 	if (usb_endpoint_xfer_control(&ep->desc)
866 		|| usb_endpoint_xfer_bulk(&ep->desc))
867 		return false;
868 
869 	/*
870 	 * for LS & FS periodic endpoints which its device is not behind
871 	 * a TT are also ignored, root-hub will schedule them directly,
872 	 * but need set @bpkts field of endpoint context to 1.
873 	 */
874 	if (is_fs_or_ls(udev->speed) && !has_tt)
875 		return false;
876 
877 	/* skip endpoint with zero maxpkt */
878 	if (usb_endpoint_maxp(&ep->desc) == 0)
879 		return false;
880 
881 	return true;
882 }
883 
xhci_mtk_sch_init(struct xhci_hcd_mtk * mtk)884 int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
885 {
886 	struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
887 	struct mu3h_sch_bw_info *sch_array;
888 	int num_usb_bus;
889 
890 	/* ss IN and OUT are separated */
891 	num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
892 
893 	sch_array = kzalloc_objs(*sch_array, num_usb_bus);
894 	if (sch_array == NULL)
895 		return -ENOMEM;
896 
897 	mtk->sch_array = sch_array;
898 
899 	INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
900 	hash_init(mtk->sch_ep_hash);
901 
902 	return 0;
903 }
904 
xhci_mtk_sch_exit(struct xhci_hcd_mtk * mtk)905 void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
906 {
907 	kfree(mtk->sch_array);
908 }
909 
add_ep_quirk(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint * ep)910 static int add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
911 			struct usb_host_endpoint *ep)
912 {
913 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
914 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
915 	struct xhci_ep_ctx *ep_ctx;
916 	struct xhci_virt_device *virt_dev;
917 	struct mu3h_sch_ep_info *sch_ep;
918 	unsigned int ep_index;
919 
920 	virt_dev = xhci->devs[udev->slot_id];
921 	ep_index = xhci_get_endpoint_index(&ep->desc);
922 	ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
923 
924 	if (!need_bw_sch(udev, ep)) {
925 		/*
926 		 * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
927 		 * device does not connected through an external HS hub
928 		 */
929 		if (usb_endpoint_xfer_int(&ep->desc)
930 			|| usb_endpoint_xfer_isoc(&ep->desc))
931 			ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
932 
933 		return 0;
934 	}
935 
936 	xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
937 
938 	sch_ep = create_sch_ep(mtk, udev, ep, ep_ctx);
939 	if (IS_ERR_OR_NULL(sch_ep))
940 		return -ENOMEM;
941 
942 	setup_sch_info(ep_ctx, sch_ep);
943 
944 	list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
945 	hash_add(mtk->sch_ep_hash, &sch_ep->hentry, (unsigned long)ep);
946 
947 	return 0;
948 }
949 
drop_ep_quirk(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint * ep)950 static void drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
951 			  struct usb_host_endpoint *ep)
952 {
953 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
954 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
955 	struct mu3h_sch_ep_info *sch_ep;
956 	struct hlist_node *hn;
957 
958 	if (!need_bw_sch(udev, ep))
959 		return;
960 
961 	xhci_dbg(xhci, "%s %s\n", __func__, decode_ep(ep, udev->speed));
962 
963 	hash_for_each_possible_safe(mtk->sch_ep_hash, sch_ep,
964 				    hn, hentry, (unsigned long)ep) {
965 		if (sch_ep->ep == ep) {
966 			destroy_sch_ep(mtk, udev, sch_ep);
967 			break;
968 		}
969 	}
970 }
971 
xhci_mtk_check_bandwidth(struct usb_hcd * hcd,struct usb_device * udev)972 int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
973 {
974 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
975 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
976 	struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
977 	struct mu3h_sch_ep_info *sch_ep;
978 	int ret;
979 
980 	xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
981 
982 	list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
983 		struct xhci_ep_ctx *ep_ctx;
984 		struct usb_host_endpoint *ep = sch_ep->ep;
985 		unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
986 
987 		ret = check_sch_bw(sch_ep);
988 		if (ret) {
989 			xhci_err(xhci, "Not enough bandwidth! (%s)\n",
990 				 sch_error_string(-ret));
991 			return -ENOSPC;
992 		}
993 
994 		ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
995 		ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(sch_ep->pkts)
996 			| EP_BCSCOUNT(sch_ep->cs_count)
997 			| EP_BBM(sch_ep->burst_mode));
998 		ep_ctx->reserved[1] = cpu_to_le32(EP_BOFFSET(sch_ep->offset)
999 			| EP_BREPEAT(sch_ep->repeat));
1000 
1001 		xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
1002 			sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
1003 			sch_ep->offset, sch_ep->repeat);
1004 	}
1005 
1006 	ret = xhci_check_bandwidth(hcd, udev);
1007 	if (!ret)
1008 		list_del_init(&mtk->bw_ep_chk_list);
1009 
1010 	return ret;
1011 }
1012 
xhci_mtk_reset_bandwidth(struct usb_hcd * hcd,struct usb_device * udev)1013 void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1014 {
1015 	struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
1016 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1017 	struct mu3h_sch_ep_info *sch_ep, *tmp;
1018 
1019 	xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
1020 
1021 	list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint)
1022 		destroy_sch_ep(mtk, udev, sch_ep);
1023 
1024 	xhci_reset_bandwidth(hcd, udev);
1025 }
1026 
xhci_mtk_add_ep(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint * ep)1027 int xhci_mtk_add_ep(struct usb_hcd *hcd, struct usb_device *udev,
1028 		    struct usb_host_endpoint *ep)
1029 {
1030 	int ret;
1031 
1032 	ret = xhci_add_endpoint(hcd, udev, ep);
1033 	if (ret)
1034 		return ret;
1035 
1036 	if (ep->hcpriv)
1037 		ret = add_ep_quirk(hcd, udev, ep);
1038 
1039 	return ret;
1040 }
1041 
xhci_mtk_drop_ep(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint * ep)1042 int xhci_mtk_drop_ep(struct usb_hcd *hcd, struct usb_device *udev,
1043 		     struct usb_host_endpoint *ep)
1044 {
1045 	int ret;
1046 
1047 	ret = xhci_drop_endpoint(hcd, udev, ep);
1048 	if (ret)
1049 		return ret;
1050 
1051 	/* needn't check @ep->hcpriv, xhci_endpoint_disable set it NULL */
1052 	drop_ep_quirk(hcd, udev, ep);
1053 
1054 	return 0;
1055 }
1056