aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDouglas Anderson <dianders@chromium.org>2016-01-28 21:20:08 -0500
committerFelipe Balbi <balbi@kernel.org>2016-03-04 08:14:44 -0500
commitfb616e3f837eee20cc0c6d5866983f7d2730d5a3 (patch)
tree1586db4c44fecbe53ea19c54d965de0f1ea793d6
parent483bb2544c470aebdcc41199c0b145d05058a3bd (diff)
usb: dwc2: host: Manage frame nums better in scheduler
The dwc2 scheduler (contained in hcd_queue.c) was a bit confusing in the way it initted / kept track of which frames a QH was going to be active in. Let's clean things up a little bit in preparation for a rewrite of the microframe scheduler. Specifically: * Old code would pick a frame number in dwc2_qh_init() and would try to pick it "in a slightly future (micro)frame". As far as I can tell the reason for this was that there was a delay between dwc2_qh_init() and when we actually wanted to dwc2_hcd_qh_add(). ...but apparently this attempt to be slightly in the future wasn't enough because dwc2_hcd_qh_add() then had code to reset things if the frame _wasn't_ in the future. There's no reason not to just pick the frame later. For non-periodic QH we now pick the frame in dwc2_hcd_qh_add(). For periodic QH we pick the frame at dwc2_schedule_periodic() time. * The old "dwc2_qh_init() actually assigned to "hsotg->frame_number". This doesn't seem like a great idea since that variable is supposed to be used to keep track of which SOF the interrupt handler has seen. Let's be clean: anyone who wants the current frame number (instead of the one as of the last interrupt) should ask for it. * The old code wasn't terribly consistent about trying to use the frame that the microframe scheduler assigned to it. In dwc2_sched_periodic_split() when it was scheduling the first frame it always "ORed" in 0x7 (!). Since the frame goes on the wire 1 uFrame after next_active_frame it meant that the SSPLIT would always try for uFrame 0 and the transaction would happen on the low speed bus during uFrame 1. This is irregardless of what the microframe scheduler said. * The old code assumed it would get called to schedule the next in a periodic split very quickly. That is if next_active_frame was 0 (transfer on wire in uFrame 1) it assumed it was getting called to schedule the next uFrame during uFrame 1 too (so it could queue something up for uFrame 2). It should be possible to actually queue something up for uFrame 2 while in uFrame 2 (AKA queue up ASAP). To do this, code needs to look at the previously scheduled frame when deciding when to next be active, not look at the current frame number. * If there was no microframe scheduler, the old code would check for whether we should be active using "qh->next_active_frame == frame_number". This seemed like a race waiting to happen. ...plus there's no way that you wouldn't want to schedule if next_active_frame was actually less than frame number. Note that this change doesn't make 100% sense on its own since it's expecting some sanity in the frame numbers assigned by the microframe scheduler and (as per the future patch which rewries it) I think that the current microframe scheduler is quite insane. However, it seems like splitting this up from the microframe scheduler patch makes things into smaller chunks and hopefully adds to clarity rather than reduces it. The two patches could certainly be squashed. Not that in the very least, I don't see any obvious bad behavior introduced with just this patch. I've attempted to keep the config parameter to disable the microframe scheduler in tact in this change, though I'm not sure it's worth it. Obviously the code is touched a lot so it's possible I regressed something when the microframe scheduler is disabled, though I did some basic testing and it seemed to work OK. I'm still not 100% sure why you wouldn't want the microframe scheduler (presuming it works), so maybe a future patch (or a future version of this patch?) could remove that parameter. Acked-by: John Youn <johnyoun@synopsys.com> Signed-off-by: Douglas Anderson <dianders@chromium.org> Tested-by: Heiko Stuebner <heiko@sntech.de> Tested-by: Stefan Wahren <stefan.wahren@i2se.com> Signed-off-by: Felipe Balbi <balbi@kernel.org>
-rw-r--r--drivers/usb/dwc2/hcd.h10
-rw-r--r--drivers/usb/dwc2/hcd_queue.c351
2 files changed, 272 insertions, 89 deletions
diff --git a/drivers/usb/dwc2/hcd.h b/drivers/usb/dwc2/hcd.h
index 10c35585a2bd..fd266ac53a28 100644
--- a/drivers/usb/dwc2/hcd.h
+++ b/drivers/usb/dwc2/hcd.h
@@ -244,8 +244,11 @@ enum dwc2_transaction_type {
244 * the bus. We'll move the qh to active here. If the 244 * the bus. We'll move the qh to active here. If the
245 * host is in high speed mode this will be a uframe. If 245 * host is in high speed mode this will be a uframe. If
246 * the host is in low speed mode this will be a full frame. 246 * the host is in low speed mode this will be a full frame.
247 * @start_active_frame: If we are partway through a split transfer, this will be
248 * what next_active_frame was when we started. Otherwise
249 * it should always be the same as next_active_frame.
250 * @assigned_uframe: The uframe (0 -7) assigned by dwc2_find_uframe().
247 * @frame_usecs: Internal variable used by the microframe scheduler 251 * @frame_usecs: Internal variable used by the microframe scheduler
248 * @start_split_frame: (Micro)frame at which last start split was initialized
249 * @ntd: Actual number of transfer descriptors in a list 252 * @ntd: Actual number of transfer descriptors in a list
250 * @qtd_list: List of QTDs for this QH 253 * @qtd_list: List of QTDs for this QH
251 * @channel: Host channel currently processing transfers for this QH 254 * @channel: Host channel currently processing transfers for this QH
@@ -279,8 +282,9 @@ struct dwc2_qh {
279 u16 host_us; 282 u16 host_us;
280 u16 host_interval; 283 u16 host_interval;
281 u16 next_active_frame; 284 u16 next_active_frame;
285 u16 start_active_frame;
286 u16 assigned_uframe;
282 u16 frame_usecs[8]; 287 u16 frame_usecs[8];
283 u16 start_split_frame;
284 u16 ntd; 288 u16 ntd;
285 struct list_head qtd_list; 289 struct list_head qtd_list;
286 struct dwc2_host_chan *channel; 290 struct dwc2_host_chan *channel;
@@ -746,7 +750,7 @@ do { \
746 _qtd_ = list_entry((_qh_)->qtd_list.next, struct dwc2_qtd, \ 750 _qtd_ = list_entry((_qh_)->qtd_list.next, struct dwc2_qtd, \
747 qtd_list_entry); \ 751 qtd_list_entry); \
748 if (usb_pipeint(_qtd_->urb->pipe) && \ 752 if (usb_pipeint(_qtd_->urb->pipe) && \
749 (_qh_)->start_split_frame != 0 && !_qtd_->complete_split) { \ 753 (_qh_)->start_active_frame != 0 && !_qtd_->complete_split) { \
750 _hfnum_.d32 = dwc2_readl((_hcd_)->regs + HFNUM); \ 754 _hfnum_.d32 = dwc2_readl((_hcd_)->regs + HFNUM); \
751 switch (_hfnum_.b.frnum & 0x7) { \ 755 switch (_hfnum_.b.frnum & 0x7) { \
752 case 7: \ 756 case 7: \
diff --git a/drivers/usb/dwc2/hcd_queue.c b/drivers/usb/dwc2/hcd_queue.c
index 9ce407e5017d..9b3c435339ee 100644
--- a/drivers/usb/dwc2/hcd_queue.c
+++ b/drivers/usb/dwc2/hcd_queue.c
@@ -38,6 +38,7 @@
38 * This file contains the functions to manage Queue Heads and Queue 38 * This file contains the functions to manage Queue Heads and Queue
39 * Transfer Descriptors for Host mode 39 * Transfer Descriptors for Host mode
40 */ 40 */
41#include <linux/gcd.h>
41#include <linux/kernel.h> 42#include <linux/kernel.h>
42#include <linux/module.h> 43#include <linux/module.h>
43#include <linux/spinlock.h> 44#include <linux/spinlock.h>
@@ -245,6 +246,96 @@ static int dwc2_find_uframe(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
245} 246}
246 247
247/** 248/**
249 * dwc2_pick_first_frame() - Choose 1st frame for qh that's already scheduled
250 *
251 * Takes a qh that has already been scheduled (which means we know we have the
252 * bandwdith reserved for us) and set the next_active_frame and the
253 * start_active_frame.
254 *
255 * This is expected to be called on qh's that weren't previously actively
256 * running. It just picks the next frame that we can fit into without any
257 * thought about the past.
258 *
259 * @hsotg: The HCD state structure for the DWC OTG controller
260 * @qh: QH for a periodic endpoint
261 *
262 */
263static void dwc2_pick_first_frame(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
264{
265 u16 frame_number;
266 u16 earliest_frame;
267 u16 next_active_frame;
268 u16 interval;
269
270 /*
271 * Use the real frame number rather than the cached value as of the
272 * last SOF to give us a little extra slop.
273 */
274 frame_number = dwc2_hcd_get_frame_number(hsotg);
275
276 /*
277 * We wouldn't want to start any earlier than the next frame just in
278 * case the frame number ticks as we're doing this calculation.
279 *
280 * NOTE: if we could quantify how long till we actually get scheduled
281 * we might be able to avoid the "+ 1" by looking at the upper part of
282 * HFNUM (the FRREM field). For now we'll just use the + 1 though.
283 */
284 earliest_frame = dwc2_frame_num_inc(frame_number, 1);
285 next_active_frame = earliest_frame;
286
287 /* Get the "no microframe schduler" out of the way... */
288 if (hsotg->core_params->uframe_sched <= 0) {
289 if (qh->do_split)
290 /* Splits are active at microframe 0 minus 1 */
291 next_active_frame |= 0x7;
292 goto exit;
293 }
294
295 /* Adjust interval as per high speed schedule which has 8 uFrame */
296 interval = gcd(qh->host_interval, 8);
297
298 /*
299 * We know interval must divide (HFNUM_MAX_FRNUM + 1) now that we've
300 * done the gcd(), so it's safe to move to the beginning of the current
301 * interval like this.
302 *
303 * After this we might be before earliest_frame, but don't worry,
304 * we'll fix it...
305 */
306 next_active_frame = (next_active_frame / interval) * interval;
307
308 /*
309 * Actually choose to start at the frame number we've been
310 * scheduled for.
311 */
312 next_active_frame = dwc2_frame_num_inc(next_active_frame,
313 qh->assigned_uframe);
314
315 /*
316 * We actually need 1 frame before since the next_active_frame is
317 * the frame number we'll be put on the ready list and we won't be on
318 * the bus until 1 frame later.
319 */
320 next_active_frame = dwc2_frame_num_dec(next_active_frame, 1);
321
322 /*
323 * By now we might actually be before the earliest_frame. Let's move
324 * up intervals until we're not.
325 */
326 while (dwc2_frame_num_gt(earliest_frame, next_active_frame))
327 next_active_frame = dwc2_frame_num_inc(next_active_frame,
328 interval);
329
330exit:
331 qh->next_active_frame = next_active_frame;
332 qh->start_active_frame = next_active_frame;
333
334 dwc2_sch_vdbg(hsotg, "QH=%p First fn=%04x nxt=%04x\n",
335 qh, frame_number, qh->next_active_frame);
336}
337
338/**
248 * dwc2_do_reserve() - Make a periodic reservation 339 * dwc2_do_reserve() - Make a periodic reservation
249 * 340 *
250 * Try to allocate space in the periodic schedule. Depending on parameters 341 * Try to allocate space in the periodic schedule. Depending on parameters
@@ -260,25 +351,9 @@ static int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
260 int status; 351 int status;
261 352
262 if (hsotg->core_params->uframe_sched > 0) { 353 if (hsotg->core_params->uframe_sched > 0) {
263 int frame = -1;
264
265 status = dwc2_find_uframe(hsotg, qh); 354 status = dwc2_find_uframe(hsotg, qh);
266 if (status == 0) 355 if (status >= 0)
267 frame = 7; 356 qh->assigned_uframe = status;
268 else if (status > 0)
269 frame = status - 1;
270
271 /* Set the new frame up */
272 if (frame >= 0) {
273 qh->next_active_frame &= ~0x7;
274 qh->next_active_frame |= (frame & 7);
275 dwc2_sch_dbg(hsotg,
276 "QH=%p sched_p nxt=%04x, uf=%d\n",
277 qh, qh->next_active_frame, frame);
278 }
279
280 if (status > 0)
281 status = 0;
282 } else { 357 } else {
283 status = dwc2_periodic_channel_available(hsotg); 358 status = dwc2_periodic_channel_available(hsotg);
284 if (status) { 359 if (status) {
@@ -305,6 +380,8 @@ static int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
305 /* Update claimed usecs per (micro)frame */ 380 /* Update claimed usecs per (micro)frame */
306 hsotg->periodic_usecs += qh->host_us; 381 hsotg->periodic_usecs += qh->host_us;
307 382
383 dwc2_pick_first_frame(hsotg, qh);
384
308 return 0; 385 return 0;
309} 386}
310 387
@@ -460,6 +537,16 @@ static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
460 status = dwc2_do_reserve(hsotg, qh); 537 status = dwc2_do_reserve(hsotg, qh);
461 if (status) 538 if (status)
462 return status; 539 return status;
540 } else {
541 /*
542 * It might have been a while, so make sure that frame_number
543 * is still good. Note: we could also try to use the similar
544 * dwc2_next_periodic_start() but that schedules much more
545 * tightly and we might need to hurry and queue things up.
546 */
547 if (dwc2_frame_num_le(qh->next_active_frame,
548 hsotg->frame_number))
549 dwc2_pick_first_frame(hsotg, qh);
463 } 550 }
464 551
465 qh->unreserve_pending = 0; 552 qh->unreserve_pending = 0;
@@ -520,7 +607,6 @@ static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
520 * @urb: Holds the information about the device/endpoint needed to initialize 607 * @urb: Holds the information about the device/endpoint needed to initialize
521 * the QH 608 * the QH
522 */ 609 */
523#define SCHEDULE_SLOP 10
524static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 610static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
525 struct dwc2_hcd_urb *urb) 611 struct dwc2_hcd_urb *urb)
526{ 612{
@@ -569,11 +655,6 @@ static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
569 qh->ep_type == USB_ENDPOINT_XFER_ISOC, 655 qh->ep_type == USB_ENDPOINT_XFER_ISOC,
570 bytecount)); 656 bytecount));
571 657
572 /* Ensure frame_number corresponds to the reality */
573 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
574 /* Start in a slightly future (micro)frame */
575 qh->next_active_frame = dwc2_frame_num_inc(hsotg->frame_number,
576 SCHEDULE_SLOP);
577 qh->host_interval = urb->interval; 658 qh->host_interval = urb->interval;
578 dwc2_sch_dbg(hsotg, "QH=%p init nxt=%04x, fn=%04x, int=%#x\n", 659 dwc2_sch_dbg(hsotg, "QH=%p init nxt=%04x, fn=%04x, int=%#x\n",
579 qh, qh->next_active_frame, hsotg->frame_number, 660 qh, qh->next_active_frame, hsotg->frame_number,
@@ -589,8 +670,6 @@ static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
589 (dev_speed == USB_SPEED_LOW || 670 (dev_speed == USB_SPEED_LOW ||
590 dev_speed == USB_SPEED_FULL)) { 671 dev_speed == USB_SPEED_FULL)) {
591 qh->host_interval *= 8; 672 qh->host_interval *= 8;
592 qh->next_active_frame |= 0x7;
593 qh->start_split_frame = qh->next_active_frame;
594 dwc2_sch_dbg(hsotg, 673 dwc2_sch_dbg(hsotg,
595 "QH=%p init*8 nxt=%04x, fn=%04x, int=%#x\n", 674 "QH=%p init*8 nxt=%04x, fn=%04x, int=%#x\n",
596 qh, qh->next_active_frame, 675 qh, qh->next_active_frame,
@@ -738,22 +817,12 @@ int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
738 /* QH already in a schedule */ 817 /* QH already in a schedule */
739 return 0; 818 return 0;
740 819
741 if (!dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number) &&
742 !hsotg->frame_number) {
743 u16 new_frame;
744
745 dev_dbg(hsotg->dev,
746 "reset frame number counter\n");
747 new_frame = dwc2_frame_num_inc(hsotg->frame_number,
748 SCHEDULE_SLOP);
749
750 dwc2_sch_vdbg(hsotg, "QH=%p reset nxt=%04x=>%04x\n",
751 qh, qh->next_active_frame, new_frame);
752 qh->next_active_frame = new_frame;
753 }
754
755 /* Add the new QH to the appropriate schedule */ 820 /* Add the new QH to the appropriate schedule */
756 if (dwc2_qh_is_non_per(qh)) { 821 if (dwc2_qh_is_non_per(qh)) {
822 /* Schedule right away */
823 qh->start_active_frame = hsotg->frame_number;
824 qh->next_active_frame = qh->start_active_frame;
825
757 /* Always start in inactive schedule */ 826 /* Always start in inactive schedule */
758 list_add_tail(&qh->qh_list_entry, 827 list_add_tail(&qh->qh_list_entry,
759 &hsotg->non_periodic_sched_inactive); 828 &hsotg->non_periodic_sched_inactive);
@@ -807,46 +876,145 @@ void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
807 } 876 }
808} 877}
809 878
810/* 879/**
811 * Schedule the next continuing periodic split transfer 880 * dwc2_next_for_periodic_split() - Set next_active_frame midway thru a split.
881 *
882 * This is called for setting next_active_frame for periodic splits for all but
883 * the first packet of the split. Confusing? I thought so...
884 *
885 * Periodic splits are single low/full speed transfers that we end up splitting
886 * up into several high speed transfers. They always fit into one full (1 ms)
887 * frame but might be split over several microframes (125 us each). We to put
888 * each of the parts on a very specific high speed frame.
889 *
890 * This function figures out where the next active uFrame needs to be.
891 *
892 * @hsotg: The HCD state structure
893 * @qh: QH for the periodic transfer.
894 * @frame_number: The current frame number.
895 *
896 * Return: number missed by (or 0 if we didn't miss).
812 */ 897 */
813static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg, 898static int dwc2_next_for_periodic_split(struct dwc2_hsotg *hsotg,
814 struct dwc2_qh *qh, u16 frame_number, 899 struct dwc2_qh *qh, u16 frame_number)
815 int sched_next_periodic_split)
816{ 900{
817 u16 incr;
818 u16 old_frame = qh->next_active_frame; 901 u16 old_frame = qh->next_active_frame;
902 u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
903 int missed = 0;
904 u16 incr;
905
906 /*
907 * Basically: increment 1 normally, but 2 right after the start split
908 * (except for ISOC out).
909 */
910 if (old_frame == qh->start_active_frame &&
911 !(qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in))
912 incr = 2;
913 else
914 incr = 1;
915
916 qh->next_active_frame = dwc2_frame_num_inc(old_frame, incr);
819 917
820 if (sched_next_periodic_split) { 918 /*
919 * Note that it's OK for frame_number to be 1 frame past
920 * next_active_frame. Remember that next_active_frame is supposed to
921 * be 1 frame _before_ when we want to be scheduled. If we're 1 frame
922 * past it just means schedule ASAP.
923 *
924 * It's _not_ OK, however, if we're more than one frame past.
925 */
926 if (dwc2_frame_num_gt(prev_frame_number, qh->next_active_frame)) {
927 /*
928 * OOPS, we missed. That's actually pretty bad since
929 * the hub will be unhappy; try ASAP I guess.
930 */
931 missed = dwc2_frame_num_dec(prev_frame_number,
932 qh->next_active_frame);
821 qh->next_active_frame = frame_number; 933 qh->next_active_frame = frame_number;
822 incr = dwc2_frame_num_inc(qh->start_split_frame, 1);
823 if (dwc2_frame_num_le(frame_number, incr)) {
824 /*
825 * Allow one frame to elapse after start split
826 * microframe before scheduling complete split, but
827 * DON'T if we are doing the next start split in the
828 * same frame for an ISOC out
829 */
830 if (qh->ep_type != USB_ENDPOINT_XFER_ISOC ||
831 qh->ep_is_in != 0) {
832 qh->next_active_frame = dwc2_frame_num_inc(
833 qh->next_active_frame, 1);
834 }
835 }
836 } else {
837 qh->next_active_frame =
838 dwc2_frame_num_inc(qh->start_split_frame,
839 qh->host_interval);
840 if (dwc2_frame_num_le(qh->next_active_frame, frame_number))
841 qh->next_active_frame = frame_number;
842 qh->next_active_frame |= 0x7;
843 qh->start_split_frame = qh->next_active_frame;
844 } 934 }
845 935
846 dwc2_sch_vdbg(hsotg, "QH=%p next(%d) fn=%04x, nxt=%04x=>%04x (%+d)\n", 936 return missed;
847 qh, sched_next_periodic_split, frame_number, old_frame, 937}
848 qh->next_active_frame, 938
849 dwc2_frame_num_dec(qh->next_active_frame, old_frame)); 939/**
940 * dwc2_next_periodic_start() - Set next_active_frame for next transfer start
941 *
942 * This is called for setting next_active_frame for a periodic transfer for
943 * all cases other than midway through a periodic split. This will also update
944 * start_active_frame.
945 *
946 * Since we _always_ keep start_active_frame as the start of the previous
947 * transfer this is normally pretty easy: we just add our interval to
948 * start_active_frame and we've got our answer.
949 *
950 * The tricks come into play if we miss. In that case we'll look for the next
951 * slot we can fit into.
952 *
953 * @hsotg: The HCD state structure
954 * @qh: QH for the periodic transfer.
955 * @frame_number: The current frame number.
956 *
957 * Return: number missed by (or 0 if we didn't miss).
958 */
959static int dwc2_next_periodic_start(struct dwc2_hsotg *hsotg,
960 struct dwc2_qh *qh, u16 frame_number)
961{
962 int missed = 0;
963 u16 interval = qh->host_interval;
964 u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
965
966 qh->start_active_frame = dwc2_frame_num_inc(qh->start_active_frame,
967 interval);
968
969 /*
970 * The dwc2_frame_num_gt() function used below won't work terribly well
971 * with if we just incremented by a really large intervals since the
972 * frame counter only goes to 0x3fff. It's terribly unlikely that we
973 * will have missed in this case anyway. Just go to exit. If we want
974 * to try to do better we'll need to keep track of a bigger counter
975 * somewhere in the driver and handle overflows.
976 */
977 if (interval >= 0x1000)
978 goto exit;
979
980 /*
981 * Test for misses, which is when it's too late to schedule.
982 *
983 * A few things to note:
984 * - We compare against prev_frame_number since start_active_frame
985 * and next_active_frame are always 1 frame before we want things
986 * to be active and we assume we can still get scheduled in the
987 * current frame number.
988 * - Some misses are expected. Specifically, in order to work
989 * perfectly dwc2 really needs quite spectacular interrupt latency
990 * requirements. It needs to be able to handle its interrupts
991 * completely within 125 us of them being asserted. That not only
992 * means that the dwc2 interrupt handler needs to be fast but it
993 * means that nothing else in the system has to block dwc2 for a long
994 * time. We can help with the dwc2 parts of this, but it's hard to
995 * guarantee that a system will have interrupt latency < 125 us, so
996 * we have to be robust to some misses.
997 */
998 if (dwc2_frame_num_gt(prev_frame_number, qh->start_active_frame)) {
999 u16 ideal_start = qh->start_active_frame;
1000
1001 /* Adjust interval as per gcd with plan length. */
1002 interval = gcd(interval, 8);
1003
1004 do {
1005 qh->start_active_frame = dwc2_frame_num_inc(
1006 qh->start_active_frame, interval);
1007 } while (dwc2_frame_num_gt(prev_frame_number,
1008 qh->start_active_frame));
1009
1010 missed = dwc2_frame_num_dec(qh->start_active_frame,
1011 ideal_start);
1012 }
1013
1014exit:
1015 qh->next_active_frame = qh->start_active_frame;
1016
1017 return missed;
850} 1018}
851 1019
852/* 1020/*
@@ -865,7 +1033,9 @@ static void dwc2_sched_periodic_split(struct dwc2_hsotg *hsotg,
865void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 1033void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
866 int sched_next_periodic_split) 1034 int sched_next_periodic_split)
867{ 1035{
1036 u16 old_frame = qh->next_active_frame;
868 u16 frame_number; 1037 u16 frame_number;
1038 int missed;
869 1039
870 if (dbg_qh(qh)) 1040 if (dbg_qh(qh))
871 dev_vdbg(hsotg->dev, "%s()\n", __func__); 1041 dev_vdbg(hsotg->dev, "%s()\n", __func__);
@@ -878,30 +1048,39 @@ void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
878 return; 1048 return;
879 } 1049 }
880 1050
1051 /*
1052 * Use the real frame number rather than the cached value as of the
1053 * last SOF just to get us a little closer to reality. Note that
1054 * means we don't actually know if we've already handled the SOF
1055 * interrupt for this frame.
1056 */
881 frame_number = dwc2_hcd_get_frame_number(hsotg); 1057 frame_number = dwc2_hcd_get_frame_number(hsotg);
882 1058
883 if (qh->do_split) { 1059 if (sched_next_periodic_split)
884 dwc2_sched_periodic_split(hsotg, qh, frame_number, 1060 missed = dwc2_next_for_periodic_split(hsotg, qh, frame_number);
885 sched_next_periodic_split); 1061 else
886 } else { 1062 missed = dwc2_next_periodic_start(hsotg, qh, frame_number);
887 qh->next_active_frame = dwc2_frame_num_inc( 1063
888 qh->next_active_frame, qh->host_interval); 1064 dwc2_sch_vdbg(hsotg,
889 if (dwc2_frame_num_le(qh->next_active_frame, frame_number)) 1065 "QH=%p next(%d) fn=%04x, sch=%04x=>%04x (%+d) miss=%d %s\n",
890 qh->next_active_frame = frame_number; 1066 qh, sched_next_periodic_split, frame_number, old_frame,
891 } 1067 qh->next_active_frame,
1068 dwc2_frame_num_dec(qh->next_active_frame, old_frame),
1069 missed, missed ? "MISS" : "");
892 1070
893 if (list_empty(&qh->qtd_list)) { 1071 if (list_empty(&qh->qtd_list)) {
894 dwc2_hcd_qh_unlink(hsotg, qh); 1072 dwc2_hcd_qh_unlink(hsotg, qh);
895 return; 1073 return;
896 } 1074 }
1075
897 /* 1076 /*
898 * Remove from periodic_sched_queued and move to 1077 * Remove from periodic_sched_queued and move to
899 * appropriate queue 1078 * appropriate queue
1079 *
1080 * Note: we purposely use the frame_number from the "hsotg" structure
1081 * since we know SOF interrupt will handle future frames.
900 */ 1082 */
901 if ((hsotg->core_params->uframe_sched > 0 && 1083 if (dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number))
902 dwc2_frame_num_le(qh->next_active_frame, frame_number)) ||
903 (hsotg->core_params->uframe_sched <= 0 &&
904 qh->next_active_frame == frame_number))
905 list_move_tail(&qh->qh_list_entry, 1084 list_move_tail(&qh->qh_list_entry,
906 &hsotg->periodic_sched_ready); 1085 &hsotg->periodic_sched_ready);
907 else 1086 else