162306a36Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * hcd_queue.c - DesignWare HS OTG Controller host queuing routines 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2004-2013 Synopsys, Inc. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci/* 962306a36Sopenharmony_ci * This file contains the functions to manage Queue Heads and Queue 1062306a36Sopenharmony_ci * Transfer Descriptors for Host mode 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci#include <linux/gcd.h> 1362306a36Sopenharmony_ci#include <linux/kernel.h> 1462306a36Sopenharmony_ci#include <linux/module.h> 1562306a36Sopenharmony_ci#include <linux/spinlock.h> 1662306a36Sopenharmony_ci#include <linux/interrupt.h> 1762306a36Sopenharmony_ci#include <linux/dma-mapping.h> 1862306a36Sopenharmony_ci#include <linux/io.h> 1962306a36Sopenharmony_ci#include <linux/slab.h> 2062306a36Sopenharmony_ci#include <linux/usb.h> 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#include <linux/usb/hcd.h> 2362306a36Sopenharmony_ci#include <linux/usb/ch11.h> 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#include "core.h" 2662306a36Sopenharmony_ci#include "hcd.h" 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/* Wait this long before releasing periodic reservation */ 2962306a36Sopenharmony_ci#define DWC2_UNRESERVE_DELAY (msecs_to_jiffies(5)) 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* If we get a NAK, wait this long before retrying */ 3262306a36Sopenharmony_ci#define DWC2_RETRY_WAIT_DELAY (1 * NSEC_PER_MSEC) 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci/** 3562306a36Sopenharmony_ci * dwc2_periodic_channel_available() - Checks that a channel is available for a 3662306a36Sopenharmony_ci * periodic transfer 3762306a36Sopenharmony_ci * 3862306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 3962306a36Sopenharmony_ci * 4062306a36Sopenharmony_ci * Return: 0 if successful, negative error code otherwise 4162306a36Sopenharmony_ci */ 4262306a36Sopenharmony_cistatic int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg) 4362306a36Sopenharmony_ci{ 4462306a36Sopenharmony_ci /* 4562306a36Sopenharmony_ci * Currently assuming that there is a dedicated host channel for 4662306a36Sopenharmony_ci * each periodic transaction plus at least one host channel for 4762306a36Sopenharmony_ci * non-periodic transactions 4862306a36Sopenharmony_ci */ 4962306a36Sopenharmony_ci int status; 5062306a36Sopenharmony_ci int num_channels; 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci num_channels = hsotg->params.host_channels; 5362306a36Sopenharmony_ci if ((hsotg->periodic_channels + hsotg->non_periodic_channels < 5462306a36Sopenharmony_ci num_channels) && (hsotg->periodic_channels < num_channels - 1)) { 5562306a36Sopenharmony_ci status = 0; 5662306a36Sopenharmony_ci } else { 5762306a36Sopenharmony_ci dev_dbg(hsotg->dev, 5862306a36Sopenharmony_ci "%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n", 5962306a36Sopenharmony_ci __func__, num_channels, 6062306a36Sopenharmony_ci hsotg->periodic_channels, hsotg->non_periodic_channels); 6162306a36Sopenharmony_ci status = -ENOSPC; 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci return status; 6562306a36Sopenharmony_ci} 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci/** 6862306a36Sopenharmony_ci * dwc2_check_periodic_bandwidth() - Checks that there is sufficient bandwidth 6962306a36Sopenharmony_ci * for the specified QH in the periodic schedule 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 7262306a36Sopenharmony_ci * @qh: QH containing periodic bandwidth required 7362306a36Sopenharmony_ci * 7462306a36Sopenharmony_ci * Return: 0 if successful, negative error code otherwise 7562306a36Sopenharmony_ci * 7662306a36Sopenharmony_ci * For simplicity, this calculation assumes that all the transfers in the 7762306a36Sopenharmony_ci * periodic schedule may occur in the same (micro)frame 7862306a36Sopenharmony_ci */ 7962306a36Sopenharmony_cistatic int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg, 8062306a36Sopenharmony_ci struct dwc2_qh *qh) 8162306a36Sopenharmony_ci{ 8262306a36Sopenharmony_ci int status; 8362306a36Sopenharmony_ci s16 max_claimed_usecs; 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci status = 0; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) { 8862306a36Sopenharmony_ci /* 8962306a36Sopenharmony_ci * High speed mode 9062306a36Sopenharmony_ci * Max periodic usecs is 80% x 125 usec = 100 usec 9162306a36Sopenharmony_ci */ 9262306a36Sopenharmony_ci max_claimed_usecs = 100 - qh->host_us; 9362306a36Sopenharmony_ci } else { 9462306a36Sopenharmony_ci /* 9562306a36Sopenharmony_ci * Full speed mode 9662306a36Sopenharmony_ci * Max periodic usecs is 90% x 1000 usec = 900 usec 9762306a36Sopenharmony_ci */ 9862306a36Sopenharmony_ci max_claimed_usecs = 900 - qh->host_us; 9962306a36Sopenharmony_ci } 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci if (hsotg->periodic_usecs > max_claimed_usecs) { 10262306a36Sopenharmony_ci dev_err(hsotg->dev, 10362306a36Sopenharmony_ci "%s: already claimed usecs %d, required usecs %d\n", 10462306a36Sopenharmony_ci __func__, hsotg->periodic_usecs, qh->host_us); 10562306a36Sopenharmony_ci status = -ENOSPC; 10662306a36Sopenharmony_ci } 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci return status; 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci/** 11262306a36Sopenharmony_ci * pmap_schedule() - Schedule time in a periodic bitmap (pmap). 11362306a36Sopenharmony_ci * 11462306a36Sopenharmony_ci * @map: The bitmap representing the schedule; will be updated 11562306a36Sopenharmony_ci * upon success. 11662306a36Sopenharmony_ci * @bits_per_period: The schedule represents several periods. This is how many 11762306a36Sopenharmony_ci * bits are in each period. It's assumed that the beginning 11862306a36Sopenharmony_ci * of the schedule will repeat after its end. 11962306a36Sopenharmony_ci * @periods_in_map: The number of periods in the schedule. 12062306a36Sopenharmony_ci * @num_bits: The number of bits we need per period we want to reserve 12162306a36Sopenharmony_ci * in this function call. 12262306a36Sopenharmony_ci * @interval: How often we need to be scheduled for the reservation this 12362306a36Sopenharmony_ci * time. 1 means every period. 2 means every other period. 12462306a36Sopenharmony_ci * ...you get the picture? 12562306a36Sopenharmony_ci * @start: The bit number to start at. Normally 0. Must be within 12662306a36Sopenharmony_ci * the interval or we return failure right away. 12762306a36Sopenharmony_ci * @only_one_period: Normally we'll allow picking a start anywhere within the 12862306a36Sopenharmony_ci * first interval, since we can still make all repetition 12962306a36Sopenharmony_ci * requirements by doing that. However, if you pass true 13062306a36Sopenharmony_ci * here then we'll return failure if we can't fit within 13162306a36Sopenharmony_ci * the period that "start" is in. 13262306a36Sopenharmony_ci * 13362306a36Sopenharmony_ci * The idea here is that we want to schedule time for repeating events that all 13462306a36Sopenharmony_ci * want the same resource. The resource is divided into fixed-sized periods 13562306a36Sopenharmony_ci * and the events want to repeat every "interval" periods. The schedule 13662306a36Sopenharmony_ci * granularity is one bit. 13762306a36Sopenharmony_ci * 13862306a36Sopenharmony_ci * To keep things "simple", we'll represent our schedule with a bitmap that 13962306a36Sopenharmony_ci * contains a fixed number of periods. This gets rid of a lot of complexity 14062306a36Sopenharmony_ci * but does mean that we need to handle things specially (and non-ideally) if 14162306a36Sopenharmony_ci * the number of the periods in the schedule doesn't match well with the 14262306a36Sopenharmony_ci * intervals that we're trying to schedule. 14362306a36Sopenharmony_ci * 14462306a36Sopenharmony_ci * Here's an explanation of the scheme we'll implement, assuming 8 periods. 14562306a36Sopenharmony_ci * - If interval is 1, we need to take up space in each of the 8 14662306a36Sopenharmony_ci * periods we're scheduling. Easy. 14762306a36Sopenharmony_ci * - If interval is 2, we need to take up space in half of the 14862306a36Sopenharmony_ci * periods. Again, easy. 14962306a36Sopenharmony_ci * - If interval is 3, we actually need to fall back to interval 1. 15062306a36Sopenharmony_ci * Why? Because we might need time in any period. AKA for the 15162306a36Sopenharmony_ci * first 8 periods, we'll be in slot 0, 3, 6. Then we'll be 15262306a36Sopenharmony_ci * in slot 1, 4, 7. Then we'll be in 2, 5. Then we'll be back to 15362306a36Sopenharmony_ci * 0, 3, and 6. Since we could be in any frame we need to reserve 15462306a36Sopenharmony_ci * for all of them. Sucks, but that's what you gotta do. Note that 15562306a36Sopenharmony_ci * if we were instead scheduling 8 * 3 = 24 we'd do much better, but 15662306a36Sopenharmony_ci * then we need more memory and time to do scheduling. 15762306a36Sopenharmony_ci * - If interval is 4, easy. 15862306a36Sopenharmony_ci * - If interval is 5, we again need interval 1. The schedule will be 15962306a36Sopenharmony_ci * 0, 5, 2, 7, 4, 1, 6, 3, 0 16062306a36Sopenharmony_ci * - If interval is 6, we need interval 2. 0, 6, 4, 2. 16162306a36Sopenharmony_ci * - If interval is 7, we need interval 1. 16262306a36Sopenharmony_ci * - If interval is 8, we need interval 8. 16362306a36Sopenharmony_ci * 16462306a36Sopenharmony_ci * If you do the math, you'll see that we need to pretend that interval is 16562306a36Sopenharmony_ci * equal to the greatest_common_divisor(interval, periods_in_map). 16662306a36Sopenharmony_ci * 16762306a36Sopenharmony_ci * Note that at the moment this function tends to front-pack the schedule. 16862306a36Sopenharmony_ci * In some cases that's really non-ideal (it's hard to schedule things that 16962306a36Sopenharmony_ci * need to repeat every period). In other cases it's perfect (you can easily 17062306a36Sopenharmony_ci * schedule bigger, less often repeating things). 17162306a36Sopenharmony_ci * 17262306a36Sopenharmony_ci * Here's the algorithm in action (8 periods, 5 bits per period): 17362306a36Sopenharmony_ci * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0 17462306a36Sopenharmony_ci * |*****| ***|*****| ***|*****| ***|*****| ***| OK 3 bits, intv 3 at 2 17562306a36Sopenharmony_ci * |*****|* ***|*****| ***|*****|* ***|*****| ***| OK 1 bits, intv 4 at 5 17662306a36Sopenharmony_ci * |** |* |** | |** |* |** | | Remv 3 bits, intv 3 at 2 17762306a36Sopenharmony_ci * |*** |* |*** | |*** |* |*** | | OK 1 bits, intv 6 at 2 17862306a36Sopenharmony_ci * |**** |* * |**** | * |**** |* * |**** | * | OK 1 bits, intv 1 at 3 17962306a36Sopenharmony_ci * |**** |**** |**** | *** |**** |**** |**** | *** | OK 2 bits, intv 2 at 6 18062306a36Sopenharmony_ci * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 1 at 4 18162306a36Sopenharmony_ci * |*****|*****|*****| ****|*****|*****|*****| ****| FAIL 1 bits, intv 1 18262306a36Sopenharmony_ci * | ***|*****| ***| ****| ***|*****| ***| ****| Remv 2 bits, intv 2 at 0 18362306a36Sopenharmony_ci * | ***| ****| ***| ****| ***| ****| ***| ****| Remv 1 bits, intv 4 at 5 18462306a36Sopenharmony_ci * | **| ****| **| ****| **| ****| **| ****| Remv 1 bits, intv 6 at 2 18562306a36Sopenharmony_ci * | *| ** *| *| ** *| *| ** *| *| ** *| Remv 1 bits, intv 1 at 3 18662306a36Sopenharmony_ci * | *| *| *| *| *| *| *| *| Remv 2 bits, intv 2 at 6 18762306a36Sopenharmony_ci * | | | | | | | | | Remv 1 bits, intv 1 at 4 18862306a36Sopenharmony_ci * |** | |** | |** | |** | | OK 2 bits, intv 2 at 0 18962306a36Sopenharmony_ci * |*** | |** | |*** | |** | | OK 1 bits, intv 4 at 2 19062306a36Sopenharmony_ci * |*****| |** **| |*****| |** **| | OK 2 bits, intv 2 at 3 19162306a36Sopenharmony_ci * |*****|* |** **| |*****|* |** **| | OK 1 bits, intv 4 at 5 19262306a36Sopenharmony_ci * |*****|*** |** **| ** |*****|*** |** **| ** | OK 2 bits, intv 2 at 6 19362306a36Sopenharmony_ci * |*****|*****|** **| ****|*****|*****|** **| ****| OK 2 bits, intv 2 at 8 19462306a36Sopenharmony_ci * |*****|*****|*****| ****|*****|*****|*****| ****| OK 1 bits, intv 4 at 12 19562306a36Sopenharmony_ci * 19662306a36Sopenharmony_ci * This function is pretty generic and could be easily abstracted if anything 19762306a36Sopenharmony_ci * needed similar scheduling. 19862306a36Sopenharmony_ci * 19962306a36Sopenharmony_ci * Returns either -ENOSPC or a >= 0 start bit which should be passed to the 20062306a36Sopenharmony_ci * unschedule routine. The map bitmap will be updated on a non-error result. 20162306a36Sopenharmony_ci */ 20262306a36Sopenharmony_cistatic int pmap_schedule(unsigned long *map, int bits_per_period, 20362306a36Sopenharmony_ci int periods_in_map, int num_bits, 20462306a36Sopenharmony_ci int interval, int start, bool only_one_period) 20562306a36Sopenharmony_ci{ 20662306a36Sopenharmony_ci int interval_bits; 20762306a36Sopenharmony_ci int to_reserve; 20862306a36Sopenharmony_ci int first_end; 20962306a36Sopenharmony_ci int i; 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci if (num_bits > bits_per_period) 21262306a36Sopenharmony_ci return -ENOSPC; 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci /* Adjust interval as per description */ 21562306a36Sopenharmony_ci interval = gcd(interval, periods_in_map); 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci interval_bits = bits_per_period * interval; 21862306a36Sopenharmony_ci to_reserve = periods_in_map / interval; 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci /* If start has gotten us past interval then we can't schedule */ 22162306a36Sopenharmony_ci if (start >= interval_bits) 22262306a36Sopenharmony_ci return -ENOSPC; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci if (only_one_period) 22562306a36Sopenharmony_ci /* Must fit within same period as start; end at begin of next */ 22662306a36Sopenharmony_ci first_end = (start / bits_per_period + 1) * bits_per_period; 22762306a36Sopenharmony_ci else 22862306a36Sopenharmony_ci /* Can fit anywhere in the first interval */ 22962306a36Sopenharmony_ci first_end = interval_bits; 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci /* 23262306a36Sopenharmony_ci * We'll try to pick the first repetition, then see if that time 23362306a36Sopenharmony_ci * is free for each of the subsequent repetitions. If it's not 23462306a36Sopenharmony_ci * we'll adjust the start time for the next search of the first 23562306a36Sopenharmony_ci * repetition. 23662306a36Sopenharmony_ci */ 23762306a36Sopenharmony_ci while (start + num_bits <= first_end) { 23862306a36Sopenharmony_ci int end; 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci /* Need to stay within this period */ 24162306a36Sopenharmony_ci end = (start / bits_per_period + 1) * bits_per_period; 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci /* Look for num_bits us in this microframe starting at start */ 24462306a36Sopenharmony_ci start = bitmap_find_next_zero_area(map, end, start, num_bits, 24562306a36Sopenharmony_ci 0); 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci /* 24862306a36Sopenharmony_ci * We should get start >= end if we fail. We might be 24962306a36Sopenharmony_ci * able to check the next microframe depending on the 25062306a36Sopenharmony_ci * interval, so continue on (start already updated). 25162306a36Sopenharmony_ci */ 25262306a36Sopenharmony_ci if (start >= end) { 25362306a36Sopenharmony_ci start = end; 25462306a36Sopenharmony_ci continue; 25562306a36Sopenharmony_ci } 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ci /* At this point we have a valid point for first one */ 25862306a36Sopenharmony_ci for (i = 1; i < to_reserve; i++) { 25962306a36Sopenharmony_ci int ith_start = start + interval_bits * i; 26062306a36Sopenharmony_ci int ith_end = end + interval_bits * i; 26162306a36Sopenharmony_ci int ret; 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci /* Use this as a dumb "check if bits are 0" */ 26462306a36Sopenharmony_ci ret = bitmap_find_next_zero_area( 26562306a36Sopenharmony_ci map, ith_start + num_bits, ith_start, num_bits, 26662306a36Sopenharmony_ci 0); 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci /* We got the right place, continue checking */ 26962306a36Sopenharmony_ci if (ret == ith_start) 27062306a36Sopenharmony_ci continue; 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci /* Move start up for next time and exit for loop */ 27362306a36Sopenharmony_ci ith_start = bitmap_find_next_zero_area( 27462306a36Sopenharmony_ci map, ith_end, ith_start, num_bits, 0); 27562306a36Sopenharmony_ci if (ith_start >= ith_end) 27662306a36Sopenharmony_ci /* Need a while new period next time */ 27762306a36Sopenharmony_ci start = end; 27862306a36Sopenharmony_ci else 27962306a36Sopenharmony_ci start = ith_start - interval_bits * i; 28062306a36Sopenharmony_ci break; 28162306a36Sopenharmony_ci } 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci /* If didn't exit the for loop with a break, we have success */ 28462306a36Sopenharmony_ci if (i == to_reserve) 28562306a36Sopenharmony_ci break; 28662306a36Sopenharmony_ci } 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci if (start + num_bits > first_end) 28962306a36Sopenharmony_ci return -ENOSPC; 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci for (i = 0; i < to_reserve; i++) { 29262306a36Sopenharmony_ci int ith_start = start + interval_bits * i; 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci bitmap_set(map, ith_start, num_bits); 29562306a36Sopenharmony_ci } 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci return start; 29862306a36Sopenharmony_ci} 29962306a36Sopenharmony_ci 30062306a36Sopenharmony_ci/** 30162306a36Sopenharmony_ci * pmap_unschedule() - Undo work done by pmap_schedule() 30262306a36Sopenharmony_ci * 30362306a36Sopenharmony_ci * @map: See pmap_schedule(). 30462306a36Sopenharmony_ci * @bits_per_period: See pmap_schedule(). 30562306a36Sopenharmony_ci * @periods_in_map: See pmap_schedule(). 30662306a36Sopenharmony_ci * @num_bits: The number of bits that was passed to schedule. 30762306a36Sopenharmony_ci * @interval: The interval that was passed to schedule. 30862306a36Sopenharmony_ci * @start: The return value from pmap_schedule(). 30962306a36Sopenharmony_ci */ 31062306a36Sopenharmony_cistatic void pmap_unschedule(unsigned long *map, int bits_per_period, 31162306a36Sopenharmony_ci int periods_in_map, int num_bits, 31262306a36Sopenharmony_ci int interval, int start) 31362306a36Sopenharmony_ci{ 31462306a36Sopenharmony_ci int interval_bits; 31562306a36Sopenharmony_ci int to_release; 31662306a36Sopenharmony_ci int i; 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci /* Adjust interval as per description in pmap_schedule() */ 31962306a36Sopenharmony_ci interval = gcd(interval, periods_in_map); 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci interval_bits = bits_per_period * interval; 32262306a36Sopenharmony_ci to_release = periods_in_map / interval; 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ci for (i = 0; i < to_release; i++) { 32562306a36Sopenharmony_ci int ith_start = start + interval_bits * i; 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ci bitmap_clear(map, ith_start, num_bits); 32862306a36Sopenharmony_ci } 32962306a36Sopenharmony_ci} 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci/** 33262306a36Sopenharmony_ci * dwc2_get_ls_map() - Get the map used for the given qh 33362306a36Sopenharmony_ci * 33462306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 33562306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 33662306a36Sopenharmony_ci * 33762306a36Sopenharmony_ci * We'll always get the periodic map out of our TT. Note that even if we're 33862306a36Sopenharmony_ci * running the host straight in low speed / full speed mode it appears as if 33962306a36Sopenharmony_ci * a TT is allocated for us, so we'll use it. If that ever changes we can 34062306a36Sopenharmony_ci * add logic here to get a map out of "hsotg" if !qh->do_split. 34162306a36Sopenharmony_ci * 34262306a36Sopenharmony_ci * Returns: the map or NULL if a map couldn't be found. 34362306a36Sopenharmony_ci */ 34462306a36Sopenharmony_cistatic unsigned long *dwc2_get_ls_map(struct dwc2_hsotg *hsotg, 34562306a36Sopenharmony_ci struct dwc2_qh *qh) 34662306a36Sopenharmony_ci{ 34762306a36Sopenharmony_ci unsigned long *map; 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci /* Don't expect to be missing a TT and be doing low speed scheduling */ 35062306a36Sopenharmony_ci if (WARN_ON(!qh->dwc_tt)) 35162306a36Sopenharmony_ci return NULL; 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ci /* Get the map and adjust if this is a multi_tt hub */ 35462306a36Sopenharmony_ci map = qh->dwc_tt->periodic_bitmaps; 35562306a36Sopenharmony_ci if (qh->dwc_tt->usb_tt->multi) 35662306a36Sopenharmony_ci map += DWC2_ELEMENTS_PER_LS_BITMAP * (qh->ttport - 1); 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci return map; 35962306a36Sopenharmony_ci} 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ci#ifdef DWC2_PRINT_SCHEDULE 36262306a36Sopenharmony_ci/* 36362306a36Sopenharmony_ci * cat_printf() - A printf() + strcat() helper 36462306a36Sopenharmony_ci * 36562306a36Sopenharmony_ci * This is useful for concatenating a bunch of strings where each string is 36662306a36Sopenharmony_ci * constructed using printf. 36762306a36Sopenharmony_ci * 36862306a36Sopenharmony_ci * @buf: The destination buffer; will be updated to point after the printed 36962306a36Sopenharmony_ci * data. 37062306a36Sopenharmony_ci * @size: The number of bytes in the buffer (includes space for '\0'). 37162306a36Sopenharmony_ci * @fmt: The format for printf. 37262306a36Sopenharmony_ci * @...: The args for printf. 37362306a36Sopenharmony_ci */ 37462306a36Sopenharmony_cistatic __printf(3, 4) 37562306a36Sopenharmony_civoid cat_printf(char **buf, size_t *size, const char *fmt, ...) 37662306a36Sopenharmony_ci{ 37762306a36Sopenharmony_ci va_list args; 37862306a36Sopenharmony_ci int i; 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci if (*size == 0) 38162306a36Sopenharmony_ci return; 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_ci va_start(args, fmt); 38462306a36Sopenharmony_ci i = vsnprintf(*buf, *size, fmt, args); 38562306a36Sopenharmony_ci va_end(args); 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci if (i >= *size) { 38862306a36Sopenharmony_ci (*buf)[*size - 1] = '\0'; 38962306a36Sopenharmony_ci *buf += *size; 39062306a36Sopenharmony_ci *size = 0; 39162306a36Sopenharmony_ci } else { 39262306a36Sopenharmony_ci *buf += i; 39362306a36Sopenharmony_ci *size -= i; 39462306a36Sopenharmony_ci } 39562306a36Sopenharmony_ci} 39662306a36Sopenharmony_ci 39762306a36Sopenharmony_ci/* 39862306a36Sopenharmony_ci * pmap_print() - Print the given periodic map 39962306a36Sopenharmony_ci * 40062306a36Sopenharmony_ci * Will attempt to print out the periodic schedule. 40162306a36Sopenharmony_ci * 40262306a36Sopenharmony_ci * @map: See pmap_schedule(). 40362306a36Sopenharmony_ci * @bits_per_period: See pmap_schedule(). 40462306a36Sopenharmony_ci * @periods_in_map: See pmap_schedule(). 40562306a36Sopenharmony_ci * @period_name: The name of 1 period, like "uFrame" 40662306a36Sopenharmony_ci * @units: The name of the units, like "us". 40762306a36Sopenharmony_ci * @print_fn: The function to call for printing. 40862306a36Sopenharmony_ci * @print_data: Opaque data to pass to the print function. 40962306a36Sopenharmony_ci */ 41062306a36Sopenharmony_cistatic void pmap_print(unsigned long *map, int bits_per_period, 41162306a36Sopenharmony_ci int periods_in_map, const char *period_name, 41262306a36Sopenharmony_ci const char *units, 41362306a36Sopenharmony_ci void (*print_fn)(const char *str, void *data), 41462306a36Sopenharmony_ci void *print_data) 41562306a36Sopenharmony_ci{ 41662306a36Sopenharmony_ci int period; 41762306a36Sopenharmony_ci 41862306a36Sopenharmony_ci for (period = 0; period < periods_in_map; period++) { 41962306a36Sopenharmony_ci char tmp[64]; 42062306a36Sopenharmony_ci char *buf = tmp; 42162306a36Sopenharmony_ci size_t buf_size = sizeof(tmp); 42262306a36Sopenharmony_ci int period_start = period * bits_per_period; 42362306a36Sopenharmony_ci int period_end = period_start + bits_per_period; 42462306a36Sopenharmony_ci int start = 0; 42562306a36Sopenharmony_ci int count = 0; 42662306a36Sopenharmony_ci bool printed = false; 42762306a36Sopenharmony_ci int i; 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci for (i = period_start; i < period_end + 1; i++) { 43062306a36Sopenharmony_ci /* Handle case when ith bit is set */ 43162306a36Sopenharmony_ci if (i < period_end && 43262306a36Sopenharmony_ci bitmap_find_next_zero_area(map, i + 1, 43362306a36Sopenharmony_ci i, 1, 0) != i) { 43462306a36Sopenharmony_ci if (count == 0) 43562306a36Sopenharmony_ci start = i - period_start; 43662306a36Sopenharmony_ci count++; 43762306a36Sopenharmony_ci continue; 43862306a36Sopenharmony_ci } 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci /* ith bit isn't set; don't care if count == 0 */ 44162306a36Sopenharmony_ci if (count == 0) 44262306a36Sopenharmony_ci continue; 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci if (!printed) 44562306a36Sopenharmony_ci cat_printf(&buf, &buf_size, "%s %d: ", 44662306a36Sopenharmony_ci period_name, period); 44762306a36Sopenharmony_ci else 44862306a36Sopenharmony_ci cat_printf(&buf, &buf_size, ", "); 44962306a36Sopenharmony_ci printed = true; 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci cat_printf(&buf, &buf_size, "%d %s -%3d %s", start, 45262306a36Sopenharmony_ci units, start + count - 1, units); 45362306a36Sopenharmony_ci count = 0; 45462306a36Sopenharmony_ci } 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_ci if (printed) 45762306a36Sopenharmony_ci print_fn(tmp, print_data); 45862306a36Sopenharmony_ci } 45962306a36Sopenharmony_ci} 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_cistruct dwc2_qh_print_data { 46262306a36Sopenharmony_ci struct dwc2_hsotg *hsotg; 46362306a36Sopenharmony_ci struct dwc2_qh *qh; 46462306a36Sopenharmony_ci}; 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci/** 46762306a36Sopenharmony_ci * dwc2_qh_print() - Helper function for dwc2_qh_schedule_print() 46862306a36Sopenharmony_ci * 46962306a36Sopenharmony_ci * @str: The string to print 47062306a36Sopenharmony_ci * @data: A pointer to a struct dwc2_qh_print_data 47162306a36Sopenharmony_ci */ 47262306a36Sopenharmony_cistatic void dwc2_qh_print(const char *str, void *data) 47362306a36Sopenharmony_ci{ 47462306a36Sopenharmony_ci struct dwc2_qh_print_data *print_data = data; 47562306a36Sopenharmony_ci 47662306a36Sopenharmony_ci dwc2_sch_dbg(print_data->hsotg, "QH=%p ...%s\n", print_data->qh, str); 47762306a36Sopenharmony_ci} 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ci/** 48062306a36Sopenharmony_ci * dwc2_qh_schedule_print() - Print the periodic schedule 48162306a36Sopenharmony_ci * 48262306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 48362306a36Sopenharmony_ci * @qh: QH to print. 48462306a36Sopenharmony_ci */ 48562306a36Sopenharmony_cistatic void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg, 48662306a36Sopenharmony_ci struct dwc2_qh *qh) 48762306a36Sopenharmony_ci{ 48862306a36Sopenharmony_ci struct dwc2_qh_print_data print_data = { hsotg, qh }; 48962306a36Sopenharmony_ci int i; 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ci /* 49262306a36Sopenharmony_ci * The printing functions are quite slow and inefficient. 49362306a36Sopenharmony_ci * If we don't have tracing turned on, don't run unless the special 49462306a36Sopenharmony_ci * define is turned on. 49562306a36Sopenharmony_ci */ 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_ci if (qh->schedule_low_speed) { 49862306a36Sopenharmony_ci unsigned long *map = dwc2_get_ls_map(hsotg, qh); 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p LS/FS trans: %d=>%d us @ %d us", 50162306a36Sopenharmony_ci qh, qh->device_us, 50262306a36Sopenharmony_ci DWC2_ROUND_US_TO_SLICE(qh->device_us), 50362306a36Sopenharmony_ci DWC2_US_PER_SLICE * qh->ls_start_schedule_slice); 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci if (map) { 50662306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, 50762306a36Sopenharmony_ci "QH=%p Whole low/full speed map %p now:\n", 50862306a36Sopenharmony_ci qh, map); 50962306a36Sopenharmony_ci pmap_print(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME, 51062306a36Sopenharmony_ci DWC2_LS_SCHEDULE_FRAMES, "Frame ", "slices", 51162306a36Sopenharmony_ci dwc2_qh_print, &print_data); 51262306a36Sopenharmony_ci } 51362306a36Sopenharmony_ci } 51462306a36Sopenharmony_ci 51562306a36Sopenharmony_ci for (i = 0; i < qh->num_hs_transfers; i++) { 51662306a36Sopenharmony_ci struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + i; 51762306a36Sopenharmony_ci int uframe = trans_time->start_schedule_us / 51862306a36Sopenharmony_ci DWC2_HS_PERIODIC_US_PER_UFRAME; 51962306a36Sopenharmony_ci int rel_us = trans_time->start_schedule_us % 52062306a36Sopenharmony_ci DWC2_HS_PERIODIC_US_PER_UFRAME; 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, 52362306a36Sopenharmony_ci "QH=%p HS trans #%d: %d us @ uFrame %d + %d us\n", 52462306a36Sopenharmony_ci qh, i, trans_time->duration_us, uframe, rel_us); 52562306a36Sopenharmony_ci } 52662306a36Sopenharmony_ci if (qh->num_hs_transfers) { 52762306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p Whole high speed map now:\n", qh); 52862306a36Sopenharmony_ci pmap_print(hsotg->hs_periodic_bitmap, 52962306a36Sopenharmony_ci DWC2_HS_PERIODIC_US_PER_UFRAME, 53062306a36Sopenharmony_ci DWC2_HS_SCHEDULE_UFRAMES, "uFrame", "us", 53162306a36Sopenharmony_ci dwc2_qh_print, &print_data); 53262306a36Sopenharmony_ci } 53362306a36Sopenharmony_ci} 53462306a36Sopenharmony_ci#else 53562306a36Sopenharmony_cistatic inline void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg, 53662306a36Sopenharmony_ci struct dwc2_qh *qh) {}; 53762306a36Sopenharmony_ci#endif 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ci/** 54062306a36Sopenharmony_ci * dwc2_ls_pmap_schedule() - Schedule a low speed QH 54162306a36Sopenharmony_ci * 54262306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 54362306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 54462306a36Sopenharmony_ci * @search_slice: We'll start trying to schedule at the passed slice. 54562306a36Sopenharmony_ci * Remember that slices are the units of the low speed 54662306a36Sopenharmony_ci * schedule (think 25us or so). 54762306a36Sopenharmony_ci * 54862306a36Sopenharmony_ci * Wraps pmap_schedule() with the right parameters for low speed scheduling. 54962306a36Sopenharmony_ci * 55062306a36Sopenharmony_ci * Normally we schedule low speed devices on the map associated with the TT. 55162306a36Sopenharmony_ci * 55262306a36Sopenharmony_ci * Returns: 0 for success or an error code. 55362306a36Sopenharmony_ci */ 55462306a36Sopenharmony_cistatic int dwc2_ls_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 55562306a36Sopenharmony_ci int search_slice) 55662306a36Sopenharmony_ci{ 55762306a36Sopenharmony_ci int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE); 55862306a36Sopenharmony_ci unsigned long *map = dwc2_get_ls_map(hsotg, qh); 55962306a36Sopenharmony_ci int slice; 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci if (!map) 56262306a36Sopenharmony_ci return -EINVAL; 56362306a36Sopenharmony_ci 56462306a36Sopenharmony_ci /* 56562306a36Sopenharmony_ci * Schedule on the proper low speed map with our low speed scheduling 56662306a36Sopenharmony_ci * parameters. Note that we use the "device_interval" here since 56762306a36Sopenharmony_ci * we want the low speed interval and the only way we'd be in this 56862306a36Sopenharmony_ci * function is if the device is low speed. 56962306a36Sopenharmony_ci * 57062306a36Sopenharmony_ci * If we happen to be doing low speed and high speed scheduling for the 57162306a36Sopenharmony_ci * same transaction (AKA we have a split) we always do low speed first. 57262306a36Sopenharmony_ci * That means we can always pass "false" for only_one_period (that 57362306a36Sopenharmony_ci * parameters is only useful when we're trying to get one schedule to 57462306a36Sopenharmony_ci * match what we already planned in the other schedule). 57562306a36Sopenharmony_ci */ 57662306a36Sopenharmony_ci slice = pmap_schedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME, 57762306a36Sopenharmony_ci DWC2_LS_SCHEDULE_FRAMES, slices, 57862306a36Sopenharmony_ci qh->device_interval, search_slice, false); 57962306a36Sopenharmony_ci 58062306a36Sopenharmony_ci if (slice < 0) 58162306a36Sopenharmony_ci return slice; 58262306a36Sopenharmony_ci 58362306a36Sopenharmony_ci qh->ls_start_schedule_slice = slice; 58462306a36Sopenharmony_ci return 0; 58562306a36Sopenharmony_ci} 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ci/** 58862306a36Sopenharmony_ci * dwc2_ls_pmap_unschedule() - Undo work done by dwc2_ls_pmap_schedule() 58962306a36Sopenharmony_ci * 59062306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 59162306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 59262306a36Sopenharmony_ci */ 59362306a36Sopenharmony_cistatic void dwc2_ls_pmap_unschedule(struct dwc2_hsotg *hsotg, 59462306a36Sopenharmony_ci struct dwc2_qh *qh) 59562306a36Sopenharmony_ci{ 59662306a36Sopenharmony_ci int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE); 59762306a36Sopenharmony_ci unsigned long *map = dwc2_get_ls_map(hsotg, qh); 59862306a36Sopenharmony_ci 59962306a36Sopenharmony_ci /* Schedule should have failed, so no worries about no error code */ 60062306a36Sopenharmony_ci if (!map) 60162306a36Sopenharmony_ci return; 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci pmap_unschedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME, 60462306a36Sopenharmony_ci DWC2_LS_SCHEDULE_FRAMES, slices, qh->device_interval, 60562306a36Sopenharmony_ci qh->ls_start_schedule_slice); 60662306a36Sopenharmony_ci} 60762306a36Sopenharmony_ci 60862306a36Sopenharmony_ci/** 60962306a36Sopenharmony_ci * dwc2_hs_pmap_schedule - Schedule in the main high speed schedule 61062306a36Sopenharmony_ci * 61162306a36Sopenharmony_ci * This will schedule something on the main dwc2 schedule. 61262306a36Sopenharmony_ci * 61362306a36Sopenharmony_ci * We'll start looking in qh->hs_transfers[index].start_schedule_us. We'll 61462306a36Sopenharmony_ci * update this with the result upon success. We also use the duration from 61562306a36Sopenharmony_ci * the same structure. 61662306a36Sopenharmony_ci * 61762306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 61862306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 61962306a36Sopenharmony_ci * @only_one_period: If true we will limit ourselves to just looking at 62062306a36Sopenharmony_ci * one period (aka one 100us chunk). This is used if we have 62162306a36Sopenharmony_ci * already scheduled something on the low speed schedule and 62262306a36Sopenharmony_ci * need to find something that matches on the high speed one. 62362306a36Sopenharmony_ci * @index: The index into qh->hs_transfers that we're working with. 62462306a36Sopenharmony_ci * 62562306a36Sopenharmony_ci * Returns: 0 for success or an error code. Upon success the 62662306a36Sopenharmony_ci * dwc2_hs_transfer_time specified by "index" will be updated. 62762306a36Sopenharmony_ci */ 62862306a36Sopenharmony_cistatic int dwc2_hs_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 62962306a36Sopenharmony_ci bool only_one_period, int index) 63062306a36Sopenharmony_ci{ 63162306a36Sopenharmony_ci struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index; 63262306a36Sopenharmony_ci int us; 63362306a36Sopenharmony_ci 63462306a36Sopenharmony_ci us = pmap_schedule(hsotg->hs_periodic_bitmap, 63562306a36Sopenharmony_ci DWC2_HS_PERIODIC_US_PER_UFRAME, 63662306a36Sopenharmony_ci DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us, 63762306a36Sopenharmony_ci qh->host_interval, trans_time->start_schedule_us, 63862306a36Sopenharmony_ci only_one_period); 63962306a36Sopenharmony_ci 64062306a36Sopenharmony_ci if (us < 0) 64162306a36Sopenharmony_ci return us; 64262306a36Sopenharmony_ci 64362306a36Sopenharmony_ci trans_time->start_schedule_us = us; 64462306a36Sopenharmony_ci return 0; 64562306a36Sopenharmony_ci} 64662306a36Sopenharmony_ci 64762306a36Sopenharmony_ci/** 64862306a36Sopenharmony_ci * dwc2_hs_pmap_unschedule() - Undo work done by dwc2_hs_pmap_schedule() 64962306a36Sopenharmony_ci * 65062306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 65162306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 65262306a36Sopenharmony_ci * @index: Transfer index 65362306a36Sopenharmony_ci */ 65462306a36Sopenharmony_cistatic void dwc2_hs_pmap_unschedule(struct dwc2_hsotg *hsotg, 65562306a36Sopenharmony_ci struct dwc2_qh *qh, int index) 65662306a36Sopenharmony_ci{ 65762306a36Sopenharmony_ci struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index; 65862306a36Sopenharmony_ci 65962306a36Sopenharmony_ci pmap_unschedule(hsotg->hs_periodic_bitmap, 66062306a36Sopenharmony_ci DWC2_HS_PERIODIC_US_PER_UFRAME, 66162306a36Sopenharmony_ci DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us, 66262306a36Sopenharmony_ci qh->host_interval, trans_time->start_schedule_us); 66362306a36Sopenharmony_ci} 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci/** 66662306a36Sopenharmony_ci * dwc2_uframe_schedule_split - Schedule a QH for a periodic split xfer. 66762306a36Sopenharmony_ci * 66862306a36Sopenharmony_ci * This is the most complicated thing in USB. We have to find matching time 66962306a36Sopenharmony_ci * in both the global high speed schedule for the port and the low speed 67062306a36Sopenharmony_ci * schedule for the TT associated with the given device. 67162306a36Sopenharmony_ci * 67262306a36Sopenharmony_ci * Being here means that the host must be running in high speed mode and the 67362306a36Sopenharmony_ci * device is in low or full speed mode (and behind a hub). 67462306a36Sopenharmony_ci * 67562306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 67662306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 67762306a36Sopenharmony_ci */ 67862306a36Sopenharmony_cistatic int dwc2_uframe_schedule_split(struct dwc2_hsotg *hsotg, 67962306a36Sopenharmony_ci struct dwc2_qh *qh) 68062306a36Sopenharmony_ci{ 68162306a36Sopenharmony_ci int bytecount = qh->maxp_mult * qh->maxp; 68262306a36Sopenharmony_ci int ls_search_slice; 68362306a36Sopenharmony_ci int err = 0; 68462306a36Sopenharmony_ci int host_interval_in_sched; 68562306a36Sopenharmony_ci 68662306a36Sopenharmony_ci /* 68762306a36Sopenharmony_ci * The interval (how often to repeat) in the actual host schedule. 68862306a36Sopenharmony_ci * See pmap_schedule() for gcd() explanation. 68962306a36Sopenharmony_ci */ 69062306a36Sopenharmony_ci host_interval_in_sched = gcd(qh->host_interval, 69162306a36Sopenharmony_ci DWC2_HS_SCHEDULE_UFRAMES); 69262306a36Sopenharmony_ci 69362306a36Sopenharmony_ci /* 69462306a36Sopenharmony_ci * We always try to find space in the low speed schedule first, then 69562306a36Sopenharmony_ci * try to find high speed time that matches. If we don't, we'll bump 69662306a36Sopenharmony_ci * up the place we start searching in the low speed schedule and try 69762306a36Sopenharmony_ci * again. To start we'll look right at the beginning of the low speed 69862306a36Sopenharmony_ci * schedule. 69962306a36Sopenharmony_ci * 70062306a36Sopenharmony_ci * Note that this will tend to front-load the high speed schedule. 70162306a36Sopenharmony_ci * We may eventually want to try to avoid this by either considering 70262306a36Sopenharmony_ci * both schedules together or doing some sort of round robin. 70362306a36Sopenharmony_ci */ 70462306a36Sopenharmony_ci ls_search_slice = 0; 70562306a36Sopenharmony_ci 70662306a36Sopenharmony_ci while (ls_search_slice < DWC2_LS_SCHEDULE_SLICES) { 70762306a36Sopenharmony_ci int start_s_uframe; 70862306a36Sopenharmony_ci int ssplit_s_uframe; 70962306a36Sopenharmony_ci int second_s_uframe; 71062306a36Sopenharmony_ci int rel_uframe; 71162306a36Sopenharmony_ci int first_count; 71262306a36Sopenharmony_ci int middle_count; 71362306a36Sopenharmony_ci int end_count; 71462306a36Sopenharmony_ci int first_data_bytes; 71562306a36Sopenharmony_ci int other_data_bytes; 71662306a36Sopenharmony_ci int i; 71762306a36Sopenharmony_ci 71862306a36Sopenharmony_ci if (qh->schedule_low_speed) { 71962306a36Sopenharmony_ci err = dwc2_ls_pmap_schedule(hsotg, qh, ls_search_slice); 72062306a36Sopenharmony_ci 72162306a36Sopenharmony_ci /* 72262306a36Sopenharmony_ci * If we got an error here there's no other magic we 72362306a36Sopenharmony_ci * can do, so bail. All the looping above is only 72462306a36Sopenharmony_ci * helpful to redo things if we got a low speed slot 72562306a36Sopenharmony_ci * and then couldn't find a matching high speed slot. 72662306a36Sopenharmony_ci */ 72762306a36Sopenharmony_ci if (err) 72862306a36Sopenharmony_ci return err; 72962306a36Sopenharmony_ci } else { 73062306a36Sopenharmony_ci /* Must be missing the tt structure? Why? */ 73162306a36Sopenharmony_ci WARN_ON_ONCE(1); 73262306a36Sopenharmony_ci } 73362306a36Sopenharmony_ci 73462306a36Sopenharmony_ci /* 73562306a36Sopenharmony_ci * This will give us a number 0 - 7 if 73662306a36Sopenharmony_ci * DWC2_LS_SCHEDULE_FRAMES == 1, or 0 - 15 if == 2, or ... 73762306a36Sopenharmony_ci */ 73862306a36Sopenharmony_ci start_s_uframe = qh->ls_start_schedule_slice / 73962306a36Sopenharmony_ci DWC2_SLICES_PER_UFRAME; 74062306a36Sopenharmony_ci 74162306a36Sopenharmony_ci /* Get a number that's always 0 - 7 */ 74262306a36Sopenharmony_ci rel_uframe = (start_s_uframe % 8); 74362306a36Sopenharmony_ci 74462306a36Sopenharmony_ci /* 74562306a36Sopenharmony_ci * If we were going to start in uframe 7 then we would need to 74662306a36Sopenharmony_ci * issue a start split in uframe 6, which spec says is not OK. 74762306a36Sopenharmony_ci * Move on to the next full frame (assuming there is one). 74862306a36Sopenharmony_ci * 74962306a36Sopenharmony_ci * See 11.18.4 Host Split Transaction Scheduling Requirements 75062306a36Sopenharmony_ci * bullet 1. 75162306a36Sopenharmony_ci */ 75262306a36Sopenharmony_ci if (rel_uframe == 7) { 75362306a36Sopenharmony_ci if (qh->schedule_low_speed) 75462306a36Sopenharmony_ci dwc2_ls_pmap_unschedule(hsotg, qh); 75562306a36Sopenharmony_ci ls_search_slice = 75662306a36Sopenharmony_ci (qh->ls_start_schedule_slice / 75762306a36Sopenharmony_ci DWC2_LS_PERIODIC_SLICES_PER_FRAME + 1) * 75862306a36Sopenharmony_ci DWC2_LS_PERIODIC_SLICES_PER_FRAME; 75962306a36Sopenharmony_ci continue; 76062306a36Sopenharmony_ci } 76162306a36Sopenharmony_ci 76262306a36Sopenharmony_ci /* 76362306a36Sopenharmony_ci * For ISOC in: 76462306a36Sopenharmony_ci * - start split (frame -1) 76562306a36Sopenharmony_ci * - complete split w/ data (frame +1) 76662306a36Sopenharmony_ci * - complete split w/ data (frame +2) 76762306a36Sopenharmony_ci * - ... 76862306a36Sopenharmony_ci * - complete split w/ data (frame +num_data_packets) 76962306a36Sopenharmony_ci * - complete split w/ data (frame +num_data_packets+1) 77062306a36Sopenharmony_ci * - complete split w/ data (frame +num_data_packets+2, max 8) 77162306a36Sopenharmony_ci * ...though if frame was "0" then max is 7... 77262306a36Sopenharmony_ci * 77362306a36Sopenharmony_ci * For ISOC out we might need to do: 77462306a36Sopenharmony_ci * - start split w/ data (frame -1) 77562306a36Sopenharmony_ci * - start split w/ data (frame +0) 77662306a36Sopenharmony_ci * - ... 77762306a36Sopenharmony_ci * - start split w/ data (frame +num_data_packets-2) 77862306a36Sopenharmony_ci * 77962306a36Sopenharmony_ci * For INTERRUPT in we might need to do: 78062306a36Sopenharmony_ci * - start split (frame -1) 78162306a36Sopenharmony_ci * - complete split w/ data (frame +1) 78262306a36Sopenharmony_ci * - complete split w/ data (frame +2) 78362306a36Sopenharmony_ci * - complete split w/ data (frame +3, max 8) 78462306a36Sopenharmony_ci * 78562306a36Sopenharmony_ci * For INTERRUPT out we might need to do: 78662306a36Sopenharmony_ci * - start split w/ data (frame -1) 78762306a36Sopenharmony_ci * - complete split (frame +1) 78862306a36Sopenharmony_ci * - complete split (frame +2) 78962306a36Sopenharmony_ci * - complete split (frame +3, max 8) 79062306a36Sopenharmony_ci * 79162306a36Sopenharmony_ci * Start adjusting! 79262306a36Sopenharmony_ci */ 79362306a36Sopenharmony_ci ssplit_s_uframe = (start_s_uframe + 79462306a36Sopenharmony_ci host_interval_in_sched - 1) % 79562306a36Sopenharmony_ci host_interval_in_sched; 79662306a36Sopenharmony_ci if (qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in) 79762306a36Sopenharmony_ci second_s_uframe = start_s_uframe; 79862306a36Sopenharmony_ci else 79962306a36Sopenharmony_ci second_s_uframe = start_s_uframe + 1; 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci /* First data transfer might not be all 188 bytes. */ 80262306a36Sopenharmony_ci first_data_bytes = 188 - 80362306a36Sopenharmony_ci DIV_ROUND_UP(188 * (qh->ls_start_schedule_slice % 80462306a36Sopenharmony_ci DWC2_SLICES_PER_UFRAME), 80562306a36Sopenharmony_ci DWC2_SLICES_PER_UFRAME); 80662306a36Sopenharmony_ci if (first_data_bytes > bytecount) 80762306a36Sopenharmony_ci first_data_bytes = bytecount; 80862306a36Sopenharmony_ci other_data_bytes = bytecount - first_data_bytes; 80962306a36Sopenharmony_ci 81062306a36Sopenharmony_ci /* 81162306a36Sopenharmony_ci * For now, skip OUT xfers where first xfer is partial 81262306a36Sopenharmony_ci * 81362306a36Sopenharmony_ci * Main dwc2 code assumes: 81462306a36Sopenharmony_ci * - INT transfers never get split in two. 81562306a36Sopenharmony_ci * - ISOC transfers can always transfer 188 bytes the first 81662306a36Sopenharmony_ci * time. 81762306a36Sopenharmony_ci * 81862306a36Sopenharmony_ci * Until that code is fixed, try again if the first transfer 81962306a36Sopenharmony_ci * couldn't transfer everything. 82062306a36Sopenharmony_ci * 82162306a36Sopenharmony_ci * This code can be removed if/when the rest of dwc2 handles 82262306a36Sopenharmony_ci * the above cases. Until it's fixed we just won't be able 82362306a36Sopenharmony_ci * to schedule quite as tightly. 82462306a36Sopenharmony_ci */ 82562306a36Sopenharmony_ci if (!qh->ep_is_in && 82662306a36Sopenharmony_ci (first_data_bytes != min_t(int, 188, bytecount))) { 82762306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, 82862306a36Sopenharmony_ci "QH=%p avoiding broken 1st xfer (%d, %d)\n", 82962306a36Sopenharmony_ci qh, first_data_bytes, bytecount); 83062306a36Sopenharmony_ci if (qh->schedule_low_speed) 83162306a36Sopenharmony_ci dwc2_ls_pmap_unschedule(hsotg, qh); 83262306a36Sopenharmony_ci ls_search_slice = (start_s_uframe + 1) * 83362306a36Sopenharmony_ci DWC2_SLICES_PER_UFRAME; 83462306a36Sopenharmony_ci continue; 83562306a36Sopenharmony_ci } 83662306a36Sopenharmony_ci 83762306a36Sopenharmony_ci /* Start by assuming transfers for the bytes */ 83862306a36Sopenharmony_ci qh->num_hs_transfers = 1 + DIV_ROUND_UP(other_data_bytes, 188); 83962306a36Sopenharmony_ci 84062306a36Sopenharmony_ci /* 84162306a36Sopenharmony_ci * Everything except ISOC OUT has extra transfers. Rules are 84262306a36Sopenharmony_ci * complicated. See 11.18.4 Host Split Transaction Scheduling 84362306a36Sopenharmony_ci * Requirements bullet 3. 84462306a36Sopenharmony_ci */ 84562306a36Sopenharmony_ci if (qh->ep_type == USB_ENDPOINT_XFER_INT) { 84662306a36Sopenharmony_ci if (rel_uframe == 6) 84762306a36Sopenharmony_ci qh->num_hs_transfers += 2; 84862306a36Sopenharmony_ci else 84962306a36Sopenharmony_ci qh->num_hs_transfers += 3; 85062306a36Sopenharmony_ci 85162306a36Sopenharmony_ci if (qh->ep_is_in) { 85262306a36Sopenharmony_ci /* 85362306a36Sopenharmony_ci * First is start split, middle/end is data. 85462306a36Sopenharmony_ci * Allocate full data bytes for all data. 85562306a36Sopenharmony_ci */ 85662306a36Sopenharmony_ci first_count = 4; 85762306a36Sopenharmony_ci middle_count = bytecount; 85862306a36Sopenharmony_ci end_count = bytecount; 85962306a36Sopenharmony_ci } else { 86062306a36Sopenharmony_ci /* 86162306a36Sopenharmony_ci * First is data, middle/end is complete. 86262306a36Sopenharmony_ci * First transfer and second can have data. 86362306a36Sopenharmony_ci * Rest should just have complete split. 86462306a36Sopenharmony_ci */ 86562306a36Sopenharmony_ci first_count = first_data_bytes; 86662306a36Sopenharmony_ci middle_count = max_t(int, 4, other_data_bytes); 86762306a36Sopenharmony_ci end_count = 4; 86862306a36Sopenharmony_ci } 86962306a36Sopenharmony_ci } else { 87062306a36Sopenharmony_ci if (qh->ep_is_in) { 87162306a36Sopenharmony_ci int last; 87262306a36Sopenharmony_ci 87362306a36Sopenharmony_ci /* Account for the start split */ 87462306a36Sopenharmony_ci qh->num_hs_transfers++; 87562306a36Sopenharmony_ci 87662306a36Sopenharmony_ci /* Calculate "L" value from spec */ 87762306a36Sopenharmony_ci last = rel_uframe + qh->num_hs_transfers + 1; 87862306a36Sopenharmony_ci 87962306a36Sopenharmony_ci /* Start with basic case */ 88062306a36Sopenharmony_ci if (last <= 6) 88162306a36Sopenharmony_ci qh->num_hs_transfers += 2; 88262306a36Sopenharmony_ci else 88362306a36Sopenharmony_ci qh->num_hs_transfers += 1; 88462306a36Sopenharmony_ci 88562306a36Sopenharmony_ci /* Adjust downwards */ 88662306a36Sopenharmony_ci if (last >= 6 && rel_uframe == 0) 88762306a36Sopenharmony_ci qh->num_hs_transfers--; 88862306a36Sopenharmony_ci 88962306a36Sopenharmony_ci /* 1st = start; rest can contain data */ 89062306a36Sopenharmony_ci first_count = 4; 89162306a36Sopenharmony_ci middle_count = min_t(int, 188, bytecount); 89262306a36Sopenharmony_ci end_count = middle_count; 89362306a36Sopenharmony_ci } else { 89462306a36Sopenharmony_ci /* All contain data, last might be smaller */ 89562306a36Sopenharmony_ci first_count = first_data_bytes; 89662306a36Sopenharmony_ci middle_count = min_t(int, 188, 89762306a36Sopenharmony_ci other_data_bytes); 89862306a36Sopenharmony_ci end_count = other_data_bytes % 188; 89962306a36Sopenharmony_ci } 90062306a36Sopenharmony_ci } 90162306a36Sopenharmony_ci 90262306a36Sopenharmony_ci /* Assign durations per uFrame */ 90362306a36Sopenharmony_ci qh->hs_transfers[0].duration_us = HS_USECS_ISO(first_count); 90462306a36Sopenharmony_ci for (i = 1; i < qh->num_hs_transfers - 1; i++) 90562306a36Sopenharmony_ci qh->hs_transfers[i].duration_us = 90662306a36Sopenharmony_ci HS_USECS_ISO(middle_count); 90762306a36Sopenharmony_ci if (qh->num_hs_transfers > 1) 90862306a36Sopenharmony_ci qh->hs_transfers[qh->num_hs_transfers - 1].duration_us = 90962306a36Sopenharmony_ci HS_USECS_ISO(end_count); 91062306a36Sopenharmony_ci 91162306a36Sopenharmony_ci /* 91262306a36Sopenharmony_ci * Assign start us. The call below to dwc2_hs_pmap_schedule() 91362306a36Sopenharmony_ci * will start with these numbers but may adjust within the same 91462306a36Sopenharmony_ci * microframe. 91562306a36Sopenharmony_ci */ 91662306a36Sopenharmony_ci qh->hs_transfers[0].start_schedule_us = 91762306a36Sopenharmony_ci ssplit_s_uframe * DWC2_HS_PERIODIC_US_PER_UFRAME; 91862306a36Sopenharmony_ci for (i = 1; i < qh->num_hs_transfers; i++) 91962306a36Sopenharmony_ci qh->hs_transfers[i].start_schedule_us = 92062306a36Sopenharmony_ci ((second_s_uframe + i - 1) % 92162306a36Sopenharmony_ci DWC2_HS_SCHEDULE_UFRAMES) * 92262306a36Sopenharmony_ci DWC2_HS_PERIODIC_US_PER_UFRAME; 92362306a36Sopenharmony_ci 92462306a36Sopenharmony_ci /* Try to schedule with filled in hs_transfers above */ 92562306a36Sopenharmony_ci for (i = 0; i < qh->num_hs_transfers; i++) { 92662306a36Sopenharmony_ci err = dwc2_hs_pmap_schedule(hsotg, qh, true, i); 92762306a36Sopenharmony_ci if (err) 92862306a36Sopenharmony_ci break; 92962306a36Sopenharmony_ci } 93062306a36Sopenharmony_ci 93162306a36Sopenharmony_ci /* If we scheduled all w/out breaking out then we're all good */ 93262306a36Sopenharmony_ci if (i == qh->num_hs_transfers) 93362306a36Sopenharmony_ci break; 93462306a36Sopenharmony_ci 93562306a36Sopenharmony_ci for (; i >= 0; i--) 93662306a36Sopenharmony_ci dwc2_hs_pmap_unschedule(hsotg, qh, i); 93762306a36Sopenharmony_ci 93862306a36Sopenharmony_ci if (qh->schedule_low_speed) 93962306a36Sopenharmony_ci dwc2_ls_pmap_unschedule(hsotg, qh); 94062306a36Sopenharmony_ci 94162306a36Sopenharmony_ci /* Try again starting in the next microframe */ 94262306a36Sopenharmony_ci ls_search_slice = (start_s_uframe + 1) * DWC2_SLICES_PER_UFRAME; 94362306a36Sopenharmony_ci } 94462306a36Sopenharmony_ci 94562306a36Sopenharmony_ci if (ls_search_slice >= DWC2_LS_SCHEDULE_SLICES) 94662306a36Sopenharmony_ci return -ENOSPC; 94762306a36Sopenharmony_ci 94862306a36Sopenharmony_ci return 0; 94962306a36Sopenharmony_ci} 95062306a36Sopenharmony_ci 95162306a36Sopenharmony_ci/** 95262306a36Sopenharmony_ci * dwc2_uframe_schedule_hs - Schedule a QH for a periodic high speed xfer. 95362306a36Sopenharmony_ci * 95462306a36Sopenharmony_ci * Basically this just wraps dwc2_hs_pmap_schedule() to provide a clean 95562306a36Sopenharmony_ci * interface. 95662306a36Sopenharmony_ci * 95762306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 95862306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 95962306a36Sopenharmony_ci */ 96062306a36Sopenharmony_cistatic int dwc2_uframe_schedule_hs(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 96162306a36Sopenharmony_ci{ 96262306a36Sopenharmony_ci /* In non-split host and device time are the same */ 96362306a36Sopenharmony_ci WARN_ON(qh->host_us != qh->device_us); 96462306a36Sopenharmony_ci WARN_ON(qh->host_interval != qh->device_interval); 96562306a36Sopenharmony_ci WARN_ON(qh->num_hs_transfers != 1); 96662306a36Sopenharmony_ci 96762306a36Sopenharmony_ci /* We'll have one transfer; init start to 0 before calling scheduler */ 96862306a36Sopenharmony_ci qh->hs_transfers[0].start_schedule_us = 0; 96962306a36Sopenharmony_ci qh->hs_transfers[0].duration_us = qh->host_us; 97062306a36Sopenharmony_ci 97162306a36Sopenharmony_ci return dwc2_hs_pmap_schedule(hsotg, qh, false, 0); 97262306a36Sopenharmony_ci} 97362306a36Sopenharmony_ci 97462306a36Sopenharmony_ci/** 97562306a36Sopenharmony_ci * dwc2_uframe_schedule_ls - Schedule a QH for a periodic low/full speed xfer. 97662306a36Sopenharmony_ci * 97762306a36Sopenharmony_ci * Basically this just wraps dwc2_ls_pmap_schedule() to provide a clean 97862306a36Sopenharmony_ci * interface. 97962306a36Sopenharmony_ci * 98062306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 98162306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 98262306a36Sopenharmony_ci */ 98362306a36Sopenharmony_cistatic int dwc2_uframe_schedule_ls(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 98462306a36Sopenharmony_ci{ 98562306a36Sopenharmony_ci /* In non-split host and device time are the same */ 98662306a36Sopenharmony_ci WARN_ON(qh->host_us != qh->device_us); 98762306a36Sopenharmony_ci WARN_ON(qh->host_interval != qh->device_interval); 98862306a36Sopenharmony_ci WARN_ON(!qh->schedule_low_speed); 98962306a36Sopenharmony_ci 99062306a36Sopenharmony_ci /* Run on the main low speed schedule (no split = no hub = no TT) */ 99162306a36Sopenharmony_ci return dwc2_ls_pmap_schedule(hsotg, qh, 0); 99262306a36Sopenharmony_ci} 99362306a36Sopenharmony_ci 99462306a36Sopenharmony_ci/** 99562306a36Sopenharmony_ci * dwc2_uframe_schedule - Schedule a QH for a periodic xfer. 99662306a36Sopenharmony_ci * 99762306a36Sopenharmony_ci * Calls one of the 3 sub-function depending on what type of transfer this QH 99862306a36Sopenharmony_ci * is for. Also adds some printing. 99962306a36Sopenharmony_ci * 100062306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 100162306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 100262306a36Sopenharmony_ci */ 100362306a36Sopenharmony_cistatic int dwc2_uframe_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 100462306a36Sopenharmony_ci{ 100562306a36Sopenharmony_ci int ret; 100662306a36Sopenharmony_ci 100762306a36Sopenharmony_ci if (qh->dev_speed == USB_SPEED_HIGH) 100862306a36Sopenharmony_ci ret = dwc2_uframe_schedule_hs(hsotg, qh); 100962306a36Sopenharmony_ci else if (!qh->do_split) 101062306a36Sopenharmony_ci ret = dwc2_uframe_schedule_ls(hsotg, qh); 101162306a36Sopenharmony_ci else 101262306a36Sopenharmony_ci ret = dwc2_uframe_schedule_split(hsotg, qh); 101362306a36Sopenharmony_ci 101462306a36Sopenharmony_ci if (ret) 101562306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p Failed to schedule %d\n", qh, ret); 101662306a36Sopenharmony_ci else 101762306a36Sopenharmony_ci dwc2_qh_schedule_print(hsotg, qh); 101862306a36Sopenharmony_ci 101962306a36Sopenharmony_ci return ret; 102062306a36Sopenharmony_ci} 102162306a36Sopenharmony_ci 102262306a36Sopenharmony_ci/** 102362306a36Sopenharmony_ci * dwc2_uframe_unschedule - Undoes dwc2_uframe_schedule(). 102462306a36Sopenharmony_ci * 102562306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller. 102662306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 102762306a36Sopenharmony_ci */ 102862306a36Sopenharmony_cistatic void dwc2_uframe_unschedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 102962306a36Sopenharmony_ci{ 103062306a36Sopenharmony_ci int i; 103162306a36Sopenharmony_ci 103262306a36Sopenharmony_ci for (i = 0; i < qh->num_hs_transfers; i++) 103362306a36Sopenharmony_ci dwc2_hs_pmap_unschedule(hsotg, qh, i); 103462306a36Sopenharmony_ci 103562306a36Sopenharmony_ci if (qh->schedule_low_speed) 103662306a36Sopenharmony_ci dwc2_ls_pmap_unschedule(hsotg, qh); 103762306a36Sopenharmony_ci 103862306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p Unscheduled\n", qh); 103962306a36Sopenharmony_ci} 104062306a36Sopenharmony_ci 104162306a36Sopenharmony_ci/** 104262306a36Sopenharmony_ci * dwc2_pick_first_frame() - Choose 1st frame for qh that's already scheduled 104362306a36Sopenharmony_ci * 104462306a36Sopenharmony_ci * Takes a qh that has already been scheduled (which means we know we have the 104562306a36Sopenharmony_ci * bandwdith reserved for us) and set the next_active_frame and the 104662306a36Sopenharmony_ci * start_active_frame. 104762306a36Sopenharmony_ci * 104862306a36Sopenharmony_ci * This is expected to be called on qh's that weren't previously actively 104962306a36Sopenharmony_ci * running. It just picks the next frame that we can fit into without any 105062306a36Sopenharmony_ci * thought about the past. 105162306a36Sopenharmony_ci * 105262306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 105362306a36Sopenharmony_ci * @qh: QH for a periodic endpoint 105462306a36Sopenharmony_ci * 105562306a36Sopenharmony_ci */ 105662306a36Sopenharmony_cistatic void dwc2_pick_first_frame(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 105762306a36Sopenharmony_ci{ 105862306a36Sopenharmony_ci u16 frame_number; 105962306a36Sopenharmony_ci u16 earliest_frame; 106062306a36Sopenharmony_ci u16 next_active_frame; 106162306a36Sopenharmony_ci u16 relative_frame; 106262306a36Sopenharmony_ci u16 interval; 106362306a36Sopenharmony_ci 106462306a36Sopenharmony_ci /* 106562306a36Sopenharmony_ci * Use the real frame number rather than the cached value as of the 106662306a36Sopenharmony_ci * last SOF to give us a little extra slop. 106762306a36Sopenharmony_ci */ 106862306a36Sopenharmony_ci frame_number = dwc2_hcd_get_frame_number(hsotg); 106962306a36Sopenharmony_ci 107062306a36Sopenharmony_ci /* 107162306a36Sopenharmony_ci * We wouldn't want to start any earlier than the next frame just in 107262306a36Sopenharmony_ci * case the frame number ticks as we're doing this calculation. 107362306a36Sopenharmony_ci * 107462306a36Sopenharmony_ci * NOTE: if we could quantify how long till we actually get scheduled 107562306a36Sopenharmony_ci * we might be able to avoid the "+ 1" by looking at the upper part of 107662306a36Sopenharmony_ci * HFNUM (the FRREM field). For now we'll just use the + 1 though. 107762306a36Sopenharmony_ci */ 107862306a36Sopenharmony_ci earliest_frame = dwc2_frame_num_inc(frame_number, 1); 107962306a36Sopenharmony_ci next_active_frame = earliest_frame; 108062306a36Sopenharmony_ci 108162306a36Sopenharmony_ci /* Get the "no microframe scheduler" out of the way... */ 108262306a36Sopenharmony_ci if (!hsotg->params.uframe_sched) { 108362306a36Sopenharmony_ci if (qh->do_split) 108462306a36Sopenharmony_ci /* Splits are active at microframe 0 minus 1 */ 108562306a36Sopenharmony_ci next_active_frame |= 0x7; 108662306a36Sopenharmony_ci goto exit; 108762306a36Sopenharmony_ci } 108862306a36Sopenharmony_ci 108962306a36Sopenharmony_ci if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) { 109062306a36Sopenharmony_ci /* 109162306a36Sopenharmony_ci * We're either at high speed or we're doing a split (which 109262306a36Sopenharmony_ci * means we're talking high speed to a hub). In any case 109362306a36Sopenharmony_ci * the first frame should be based on when the first scheduled 109462306a36Sopenharmony_ci * event is. 109562306a36Sopenharmony_ci */ 109662306a36Sopenharmony_ci WARN_ON(qh->num_hs_transfers < 1); 109762306a36Sopenharmony_ci 109862306a36Sopenharmony_ci relative_frame = qh->hs_transfers[0].start_schedule_us / 109962306a36Sopenharmony_ci DWC2_HS_PERIODIC_US_PER_UFRAME; 110062306a36Sopenharmony_ci 110162306a36Sopenharmony_ci /* Adjust interval as per high speed schedule */ 110262306a36Sopenharmony_ci interval = gcd(qh->host_interval, DWC2_HS_SCHEDULE_UFRAMES); 110362306a36Sopenharmony_ci 110462306a36Sopenharmony_ci } else { 110562306a36Sopenharmony_ci /* 110662306a36Sopenharmony_ci * Low or full speed directly on dwc2. Just about the same 110762306a36Sopenharmony_ci * as high speed but on a different schedule and with slightly 110862306a36Sopenharmony_ci * different adjustments. Note that this works because when 110962306a36Sopenharmony_ci * the host and device are both low speed then frames in the 111062306a36Sopenharmony_ci * controller tick at low speed. 111162306a36Sopenharmony_ci */ 111262306a36Sopenharmony_ci relative_frame = qh->ls_start_schedule_slice / 111362306a36Sopenharmony_ci DWC2_LS_PERIODIC_SLICES_PER_FRAME; 111462306a36Sopenharmony_ci interval = gcd(qh->host_interval, DWC2_LS_SCHEDULE_FRAMES); 111562306a36Sopenharmony_ci } 111662306a36Sopenharmony_ci 111762306a36Sopenharmony_ci /* Scheduler messed up if frame is past interval */ 111862306a36Sopenharmony_ci WARN_ON(relative_frame >= interval); 111962306a36Sopenharmony_ci 112062306a36Sopenharmony_ci /* 112162306a36Sopenharmony_ci * We know interval must divide (HFNUM_MAX_FRNUM + 1) now that we've 112262306a36Sopenharmony_ci * done the gcd(), so it's safe to move to the beginning of the current 112362306a36Sopenharmony_ci * interval like this. 112462306a36Sopenharmony_ci * 112562306a36Sopenharmony_ci * After this we might be before earliest_frame, but don't worry, 112662306a36Sopenharmony_ci * we'll fix it... 112762306a36Sopenharmony_ci */ 112862306a36Sopenharmony_ci next_active_frame = (next_active_frame / interval) * interval; 112962306a36Sopenharmony_ci 113062306a36Sopenharmony_ci /* 113162306a36Sopenharmony_ci * Actually choose to start at the frame number we've been 113262306a36Sopenharmony_ci * scheduled for. 113362306a36Sopenharmony_ci */ 113462306a36Sopenharmony_ci next_active_frame = dwc2_frame_num_inc(next_active_frame, 113562306a36Sopenharmony_ci relative_frame); 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_ci /* 113862306a36Sopenharmony_ci * We actually need 1 frame before since the next_active_frame is 113962306a36Sopenharmony_ci * the frame number we'll be put on the ready list and we won't be on 114062306a36Sopenharmony_ci * the bus until 1 frame later. 114162306a36Sopenharmony_ci */ 114262306a36Sopenharmony_ci next_active_frame = dwc2_frame_num_dec(next_active_frame, 1); 114362306a36Sopenharmony_ci 114462306a36Sopenharmony_ci /* 114562306a36Sopenharmony_ci * By now we might actually be before the earliest_frame. Let's move 114662306a36Sopenharmony_ci * up intervals until we're not. 114762306a36Sopenharmony_ci */ 114862306a36Sopenharmony_ci while (dwc2_frame_num_gt(earliest_frame, next_active_frame)) 114962306a36Sopenharmony_ci next_active_frame = dwc2_frame_num_inc(next_active_frame, 115062306a36Sopenharmony_ci interval); 115162306a36Sopenharmony_ci 115262306a36Sopenharmony_ciexit: 115362306a36Sopenharmony_ci qh->next_active_frame = next_active_frame; 115462306a36Sopenharmony_ci qh->start_active_frame = next_active_frame; 115562306a36Sopenharmony_ci 115662306a36Sopenharmony_ci dwc2_sch_vdbg(hsotg, "QH=%p First fn=%04x nxt=%04x\n", 115762306a36Sopenharmony_ci qh, frame_number, qh->next_active_frame); 115862306a36Sopenharmony_ci} 115962306a36Sopenharmony_ci 116062306a36Sopenharmony_ci/** 116162306a36Sopenharmony_ci * dwc2_do_reserve() - Make a periodic reservation 116262306a36Sopenharmony_ci * 116362306a36Sopenharmony_ci * Try to allocate space in the periodic schedule. Depending on parameters 116462306a36Sopenharmony_ci * this might use the microframe scheduler or the dumb scheduler. 116562306a36Sopenharmony_ci * 116662306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 116762306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 116862306a36Sopenharmony_ci * 116962306a36Sopenharmony_ci * Returns: 0 upon success; error upon failure. 117062306a36Sopenharmony_ci */ 117162306a36Sopenharmony_cistatic int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 117262306a36Sopenharmony_ci{ 117362306a36Sopenharmony_ci int status; 117462306a36Sopenharmony_ci 117562306a36Sopenharmony_ci if (hsotg->params.uframe_sched) { 117662306a36Sopenharmony_ci status = dwc2_uframe_schedule(hsotg, qh); 117762306a36Sopenharmony_ci } else { 117862306a36Sopenharmony_ci status = dwc2_periodic_channel_available(hsotg); 117962306a36Sopenharmony_ci if (status) { 118062306a36Sopenharmony_ci dev_info(hsotg->dev, 118162306a36Sopenharmony_ci "%s: No host channel available for periodic transfer\n", 118262306a36Sopenharmony_ci __func__); 118362306a36Sopenharmony_ci return status; 118462306a36Sopenharmony_ci } 118562306a36Sopenharmony_ci 118662306a36Sopenharmony_ci status = dwc2_check_periodic_bandwidth(hsotg, qh); 118762306a36Sopenharmony_ci } 118862306a36Sopenharmony_ci 118962306a36Sopenharmony_ci if (status) { 119062306a36Sopenharmony_ci dev_dbg(hsotg->dev, 119162306a36Sopenharmony_ci "%s: Insufficient periodic bandwidth for periodic transfer\n", 119262306a36Sopenharmony_ci __func__); 119362306a36Sopenharmony_ci return status; 119462306a36Sopenharmony_ci } 119562306a36Sopenharmony_ci 119662306a36Sopenharmony_ci if (!hsotg->params.uframe_sched) 119762306a36Sopenharmony_ci /* Reserve periodic channel */ 119862306a36Sopenharmony_ci hsotg->periodic_channels++; 119962306a36Sopenharmony_ci 120062306a36Sopenharmony_ci /* Update claimed usecs per (micro)frame */ 120162306a36Sopenharmony_ci hsotg->periodic_usecs += qh->host_us; 120262306a36Sopenharmony_ci 120362306a36Sopenharmony_ci dwc2_pick_first_frame(hsotg, qh); 120462306a36Sopenharmony_ci 120562306a36Sopenharmony_ci return 0; 120662306a36Sopenharmony_ci} 120762306a36Sopenharmony_ci 120862306a36Sopenharmony_ci/** 120962306a36Sopenharmony_ci * dwc2_do_unreserve() - Actually release the periodic reservation 121062306a36Sopenharmony_ci * 121162306a36Sopenharmony_ci * This function actually releases the periodic bandwidth that was reserved 121262306a36Sopenharmony_ci * by the given qh. 121362306a36Sopenharmony_ci * 121462306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 121562306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 121662306a36Sopenharmony_ci */ 121762306a36Sopenharmony_cistatic void dwc2_do_unreserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 121862306a36Sopenharmony_ci{ 121962306a36Sopenharmony_ci assert_spin_locked(&hsotg->lock); 122062306a36Sopenharmony_ci 122162306a36Sopenharmony_ci WARN_ON(!qh->unreserve_pending); 122262306a36Sopenharmony_ci 122362306a36Sopenharmony_ci /* No more unreserve pending--we're doing it */ 122462306a36Sopenharmony_ci qh->unreserve_pending = false; 122562306a36Sopenharmony_ci 122662306a36Sopenharmony_ci if (WARN_ON(!list_empty(&qh->qh_list_entry))) 122762306a36Sopenharmony_ci list_del_init(&qh->qh_list_entry); 122862306a36Sopenharmony_ci 122962306a36Sopenharmony_ci /* Update claimed usecs per (micro)frame */ 123062306a36Sopenharmony_ci hsotg->periodic_usecs -= qh->host_us; 123162306a36Sopenharmony_ci 123262306a36Sopenharmony_ci if (hsotg->params.uframe_sched) { 123362306a36Sopenharmony_ci dwc2_uframe_unschedule(hsotg, qh); 123462306a36Sopenharmony_ci } else { 123562306a36Sopenharmony_ci /* Release periodic channel reservation */ 123662306a36Sopenharmony_ci hsotg->periodic_channels--; 123762306a36Sopenharmony_ci } 123862306a36Sopenharmony_ci} 123962306a36Sopenharmony_ci 124062306a36Sopenharmony_ci/** 124162306a36Sopenharmony_ci * dwc2_unreserve_timer_fn() - Timer function to release periodic reservation 124262306a36Sopenharmony_ci * 124362306a36Sopenharmony_ci * According to the kernel doc for usb_submit_urb() (specifically the part about 124462306a36Sopenharmony_ci * "Reserved Bandwidth Transfers"), we need to keep a reservation active as 124562306a36Sopenharmony_ci * long as a device driver keeps submitting. Since we're using HCD_BH to give 124662306a36Sopenharmony_ci * back the URB we need to give the driver a little bit of time before we 124762306a36Sopenharmony_ci * release the reservation. This worker is called after the appropriate 124862306a36Sopenharmony_ci * delay. 124962306a36Sopenharmony_ci * 125062306a36Sopenharmony_ci * @t: Address to a qh unreserve_work. 125162306a36Sopenharmony_ci */ 125262306a36Sopenharmony_cistatic void dwc2_unreserve_timer_fn(struct timer_list *t) 125362306a36Sopenharmony_ci{ 125462306a36Sopenharmony_ci struct dwc2_qh *qh = from_timer(qh, t, unreserve_timer); 125562306a36Sopenharmony_ci struct dwc2_hsotg *hsotg = qh->hsotg; 125662306a36Sopenharmony_ci unsigned long flags; 125762306a36Sopenharmony_ci 125862306a36Sopenharmony_ci /* 125962306a36Sopenharmony_ci * Wait for the lock, or for us to be scheduled again. We 126062306a36Sopenharmony_ci * could be scheduled again if: 126162306a36Sopenharmony_ci * - We started executing but didn't get the lock yet. 126262306a36Sopenharmony_ci * - A new reservation came in, but cancel didn't take effect 126362306a36Sopenharmony_ci * because we already started executing. 126462306a36Sopenharmony_ci * - The timer has been kicked again. 126562306a36Sopenharmony_ci * In that case cancel and wait for the next call. 126662306a36Sopenharmony_ci */ 126762306a36Sopenharmony_ci while (!spin_trylock_irqsave(&hsotg->lock, flags)) { 126862306a36Sopenharmony_ci if (timer_pending(&qh->unreserve_timer)) 126962306a36Sopenharmony_ci return; 127062306a36Sopenharmony_ci } 127162306a36Sopenharmony_ci 127262306a36Sopenharmony_ci /* 127362306a36Sopenharmony_ci * Might be no more unreserve pending if: 127462306a36Sopenharmony_ci * - We started executing but didn't get the lock yet. 127562306a36Sopenharmony_ci * - A new reservation came in, but cancel didn't take effect 127662306a36Sopenharmony_ci * because we already started executing. 127762306a36Sopenharmony_ci * 127862306a36Sopenharmony_ci * We can't put this in the loop above because unreserve_pending needs 127962306a36Sopenharmony_ci * to be accessed under lock, so we can only check it once we got the 128062306a36Sopenharmony_ci * lock. 128162306a36Sopenharmony_ci */ 128262306a36Sopenharmony_ci if (qh->unreserve_pending) 128362306a36Sopenharmony_ci dwc2_do_unreserve(hsotg, qh); 128462306a36Sopenharmony_ci 128562306a36Sopenharmony_ci spin_unlock_irqrestore(&hsotg->lock, flags); 128662306a36Sopenharmony_ci} 128762306a36Sopenharmony_ci 128862306a36Sopenharmony_ci/** 128962306a36Sopenharmony_ci * dwc2_check_max_xfer_size() - Checks that the max transfer size allowed in a 129062306a36Sopenharmony_ci * host channel is large enough to handle the maximum data transfer in a single 129162306a36Sopenharmony_ci * (micro)frame for a periodic transfer 129262306a36Sopenharmony_ci * 129362306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 129462306a36Sopenharmony_ci * @qh: QH for a periodic endpoint 129562306a36Sopenharmony_ci * 129662306a36Sopenharmony_ci * Return: 0 if successful, negative error code otherwise 129762306a36Sopenharmony_ci */ 129862306a36Sopenharmony_cistatic int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg, 129962306a36Sopenharmony_ci struct dwc2_qh *qh) 130062306a36Sopenharmony_ci{ 130162306a36Sopenharmony_ci u32 max_xfer_size; 130262306a36Sopenharmony_ci u32 max_channel_xfer_size; 130362306a36Sopenharmony_ci int status = 0; 130462306a36Sopenharmony_ci 130562306a36Sopenharmony_ci max_xfer_size = qh->maxp * qh->maxp_mult; 130662306a36Sopenharmony_ci max_channel_xfer_size = hsotg->params.max_transfer_size; 130762306a36Sopenharmony_ci 130862306a36Sopenharmony_ci if (max_xfer_size > max_channel_xfer_size) { 130962306a36Sopenharmony_ci dev_err(hsotg->dev, 131062306a36Sopenharmony_ci "%s: Periodic xfer length %d > max xfer length for channel %d\n", 131162306a36Sopenharmony_ci __func__, max_xfer_size, max_channel_xfer_size); 131262306a36Sopenharmony_ci status = -ENOSPC; 131362306a36Sopenharmony_ci } 131462306a36Sopenharmony_ci 131562306a36Sopenharmony_ci return status; 131662306a36Sopenharmony_ci} 131762306a36Sopenharmony_ci 131862306a36Sopenharmony_ci/** 131962306a36Sopenharmony_ci * dwc2_schedule_periodic() - Schedules an interrupt or isochronous transfer in 132062306a36Sopenharmony_ci * the periodic schedule 132162306a36Sopenharmony_ci * 132262306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 132362306a36Sopenharmony_ci * @qh: QH for the periodic transfer. The QH should already contain the 132462306a36Sopenharmony_ci * scheduling information. 132562306a36Sopenharmony_ci * 132662306a36Sopenharmony_ci * Return: 0 if successful, negative error code otherwise 132762306a36Sopenharmony_ci */ 132862306a36Sopenharmony_cistatic int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 132962306a36Sopenharmony_ci{ 133062306a36Sopenharmony_ci int status; 133162306a36Sopenharmony_ci 133262306a36Sopenharmony_ci status = dwc2_check_max_xfer_size(hsotg, qh); 133362306a36Sopenharmony_ci if (status) { 133462306a36Sopenharmony_ci dev_dbg(hsotg->dev, 133562306a36Sopenharmony_ci "%s: Channel max transfer size too small for periodic transfer\n", 133662306a36Sopenharmony_ci __func__); 133762306a36Sopenharmony_ci return status; 133862306a36Sopenharmony_ci } 133962306a36Sopenharmony_ci 134062306a36Sopenharmony_ci /* Cancel pending unreserve; if canceled OK, unreserve was pending */ 134162306a36Sopenharmony_ci if (del_timer(&qh->unreserve_timer)) 134262306a36Sopenharmony_ci WARN_ON(!qh->unreserve_pending); 134362306a36Sopenharmony_ci 134462306a36Sopenharmony_ci /* 134562306a36Sopenharmony_ci * Only need to reserve if there's not an unreserve pending, since if an 134662306a36Sopenharmony_ci * unreserve is pending then by definition our old reservation is still 134762306a36Sopenharmony_ci * valid. Unreserve might still be pending even if we didn't cancel if 134862306a36Sopenharmony_ci * dwc2_unreserve_timer_fn() already started. Code in the timer handles 134962306a36Sopenharmony_ci * that case. 135062306a36Sopenharmony_ci */ 135162306a36Sopenharmony_ci if (!qh->unreserve_pending) { 135262306a36Sopenharmony_ci status = dwc2_do_reserve(hsotg, qh); 135362306a36Sopenharmony_ci if (status) 135462306a36Sopenharmony_ci return status; 135562306a36Sopenharmony_ci } else { 135662306a36Sopenharmony_ci /* 135762306a36Sopenharmony_ci * It might have been a while, so make sure that frame_number 135862306a36Sopenharmony_ci * is still good. Note: we could also try to use the similar 135962306a36Sopenharmony_ci * dwc2_next_periodic_start() but that schedules much more 136062306a36Sopenharmony_ci * tightly and we might need to hurry and queue things up. 136162306a36Sopenharmony_ci */ 136262306a36Sopenharmony_ci if (dwc2_frame_num_le(qh->next_active_frame, 136362306a36Sopenharmony_ci hsotg->frame_number)) 136462306a36Sopenharmony_ci dwc2_pick_first_frame(hsotg, qh); 136562306a36Sopenharmony_ci } 136662306a36Sopenharmony_ci 136762306a36Sopenharmony_ci qh->unreserve_pending = 0; 136862306a36Sopenharmony_ci 136962306a36Sopenharmony_ci if (hsotg->params.dma_desc_enable) 137062306a36Sopenharmony_ci /* Don't rely on SOF and start in ready schedule */ 137162306a36Sopenharmony_ci list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready); 137262306a36Sopenharmony_ci else 137362306a36Sopenharmony_ci /* Always start in inactive schedule */ 137462306a36Sopenharmony_ci list_add_tail(&qh->qh_list_entry, 137562306a36Sopenharmony_ci &hsotg->periodic_sched_inactive); 137662306a36Sopenharmony_ci 137762306a36Sopenharmony_ci return 0; 137862306a36Sopenharmony_ci} 137962306a36Sopenharmony_ci 138062306a36Sopenharmony_ci/** 138162306a36Sopenharmony_ci * dwc2_deschedule_periodic() - Removes an interrupt or isochronous transfer 138262306a36Sopenharmony_ci * from the periodic schedule 138362306a36Sopenharmony_ci * 138462306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 138562306a36Sopenharmony_ci * @qh: QH for the periodic transfer 138662306a36Sopenharmony_ci */ 138762306a36Sopenharmony_cistatic void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg, 138862306a36Sopenharmony_ci struct dwc2_qh *qh) 138962306a36Sopenharmony_ci{ 139062306a36Sopenharmony_ci bool did_modify; 139162306a36Sopenharmony_ci 139262306a36Sopenharmony_ci assert_spin_locked(&hsotg->lock); 139362306a36Sopenharmony_ci 139462306a36Sopenharmony_ci /* 139562306a36Sopenharmony_ci * Schedule the unreserve to happen in a little bit. Cases here: 139662306a36Sopenharmony_ci * - Unreserve worker might be sitting there waiting to grab the lock. 139762306a36Sopenharmony_ci * In this case it will notice it's been schedule again and will 139862306a36Sopenharmony_ci * quit. 139962306a36Sopenharmony_ci * - Unreserve worker might not be scheduled. 140062306a36Sopenharmony_ci * 140162306a36Sopenharmony_ci * We should never already be scheduled since dwc2_schedule_periodic() 140262306a36Sopenharmony_ci * should have canceled the scheduled unreserve timer (hence the 140362306a36Sopenharmony_ci * warning on did_modify). 140462306a36Sopenharmony_ci * 140562306a36Sopenharmony_ci * We add + 1 to the timer to guarantee that at least 1 jiffy has 140662306a36Sopenharmony_ci * passed (otherwise if the jiffy counter might tick right after we 140762306a36Sopenharmony_ci * read it and we'll get no delay). 140862306a36Sopenharmony_ci */ 140962306a36Sopenharmony_ci did_modify = mod_timer(&qh->unreserve_timer, 141062306a36Sopenharmony_ci jiffies + DWC2_UNRESERVE_DELAY + 1); 141162306a36Sopenharmony_ci WARN_ON(did_modify); 141262306a36Sopenharmony_ci qh->unreserve_pending = 1; 141362306a36Sopenharmony_ci 141462306a36Sopenharmony_ci list_del_init(&qh->qh_list_entry); 141562306a36Sopenharmony_ci} 141662306a36Sopenharmony_ci 141762306a36Sopenharmony_ci/** 141862306a36Sopenharmony_ci * dwc2_wait_timer_fn() - Timer function to re-queue after waiting 141962306a36Sopenharmony_ci * 142062306a36Sopenharmony_ci * As per the spec, a NAK indicates that "a function is temporarily unable to 142162306a36Sopenharmony_ci * transmit or receive data, but will eventually be able to do so without need 142262306a36Sopenharmony_ci * of host intervention". 142362306a36Sopenharmony_ci * 142462306a36Sopenharmony_ci * That means that when we encounter a NAK we're supposed to retry. 142562306a36Sopenharmony_ci * 142662306a36Sopenharmony_ci * ...but if we retry right away (from the interrupt handler that saw the NAK) 142762306a36Sopenharmony_ci * then we can end up with an interrupt storm (if the other side keeps NAKing 142862306a36Sopenharmony_ci * us) because on slow enough CPUs it could take us longer to get out of the 142962306a36Sopenharmony_ci * interrupt routine than it takes for the device to send another NAK. That 143062306a36Sopenharmony_ci * leads to a constant stream of NAK interrupts and the CPU locks. 143162306a36Sopenharmony_ci * 143262306a36Sopenharmony_ci * ...so instead of retrying right away in the case of a NAK we'll set a timer 143362306a36Sopenharmony_ci * to retry some time later. This function handles that timer and moves the 143462306a36Sopenharmony_ci * qh back to the "inactive" list, then queues transactions. 143562306a36Sopenharmony_ci * 143662306a36Sopenharmony_ci * @t: Pointer to wait_timer in a qh. 143762306a36Sopenharmony_ci * 143862306a36Sopenharmony_ci * Return: HRTIMER_NORESTART to not automatically restart this timer. 143962306a36Sopenharmony_ci */ 144062306a36Sopenharmony_cistatic enum hrtimer_restart dwc2_wait_timer_fn(struct hrtimer *t) 144162306a36Sopenharmony_ci{ 144262306a36Sopenharmony_ci struct dwc2_qh *qh = container_of(t, struct dwc2_qh, wait_timer); 144362306a36Sopenharmony_ci struct dwc2_hsotg *hsotg = qh->hsotg; 144462306a36Sopenharmony_ci unsigned long flags; 144562306a36Sopenharmony_ci 144662306a36Sopenharmony_ci spin_lock_irqsave(&hsotg->lock, flags); 144762306a36Sopenharmony_ci 144862306a36Sopenharmony_ci /* 144962306a36Sopenharmony_ci * We'll set wait_timer_cancel to true if we want to cancel this 145062306a36Sopenharmony_ci * operation in dwc2_hcd_qh_unlink(). 145162306a36Sopenharmony_ci */ 145262306a36Sopenharmony_ci if (!qh->wait_timer_cancel) { 145362306a36Sopenharmony_ci enum dwc2_transaction_type tr_type; 145462306a36Sopenharmony_ci 145562306a36Sopenharmony_ci qh->want_wait = false; 145662306a36Sopenharmony_ci 145762306a36Sopenharmony_ci list_move(&qh->qh_list_entry, 145862306a36Sopenharmony_ci &hsotg->non_periodic_sched_inactive); 145962306a36Sopenharmony_ci 146062306a36Sopenharmony_ci tr_type = dwc2_hcd_select_transactions(hsotg); 146162306a36Sopenharmony_ci if (tr_type != DWC2_TRANSACTION_NONE) 146262306a36Sopenharmony_ci dwc2_hcd_queue_transactions(hsotg, tr_type); 146362306a36Sopenharmony_ci } 146462306a36Sopenharmony_ci 146562306a36Sopenharmony_ci spin_unlock_irqrestore(&hsotg->lock, flags); 146662306a36Sopenharmony_ci return HRTIMER_NORESTART; 146762306a36Sopenharmony_ci} 146862306a36Sopenharmony_ci 146962306a36Sopenharmony_ci/** 147062306a36Sopenharmony_ci * dwc2_qh_init() - Initializes a QH structure 147162306a36Sopenharmony_ci * 147262306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 147362306a36Sopenharmony_ci * @qh: The QH to init 147462306a36Sopenharmony_ci * @urb: Holds the information about the device/endpoint needed to initialize 147562306a36Sopenharmony_ci * the QH 147662306a36Sopenharmony_ci * @mem_flags: Flags for allocating memory. 147762306a36Sopenharmony_ci */ 147862306a36Sopenharmony_cistatic void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 147962306a36Sopenharmony_ci struct dwc2_hcd_urb *urb, gfp_t mem_flags) 148062306a36Sopenharmony_ci{ 148162306a36Sopenharmony_ci int dev_speed = dwc2_host_get_speed(hsotg, urb->priv); 148262306a36Sopenharmony_ci u8 ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info); 148362306a36Sopenharmony_ci bool ep_is_in = !!dwc2_hcd_is_pipe_in(&urb->pipe_info); 148462306a36Sopenharmony_ci bool ep_is_isoc = (ep_type == USB_ENDPOINT_XFER_ISOC); 148562306a36Sopenharmony_ci bool ep_is_int = (ep_type == USB_ENDPOINT_XFER_INT); 148662306a36Sopenharmony_ci u32 hprt = dwc2_readl(hsotg, HPRT0); 148762306a36Sopenharmony_ci u32 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT; 148862306a36Sopenharmony_ci bool do_split = (prtspd == HPRT0_SPD_HIGH_SPEED && 148962306a36Sopenharmony_ci dev_speed != USB_SPEED_HIGH); 149062306a36Sopenharmony_ci int maxp = dwc2_hcd_get_maxp(&urb->pipe_info); 149162306a36Sopenharmony_ci int maxp_mult = dwc2_hcd_get_maxp_mult(&urb->pipe_info); 149262306a36Sopenharmony_ci int bytecount = maxp_mult * maxp; 149362306a36Sopenharmony_ci char *speed, *type; 149462306a36Sopenharmony_ci 149562306a36Sopenharmony_ci /* Initialize QH */ 149662306a36Sopenharmony_ci qh->hsotg = hsotg; 149762306a36Sopenharmony_ci timer_setup(&qh->unreserve_timer, dwc2_unreserve_timer_fn, 0); 149862306a36Sopenharmony_ci hrtimer_init(&qh->wait_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 149962306a36Sopenharmony_ci qh->wait_timer.function = &dwc2_wait_timer_fn; 150062306a36Sopenharmony_ci qh->ep_type = ep_type; 150162306a36Sopenharmony_ci qh->ep_is_in = ep_is_in; 150262306a36Sopenharmony_ci 150362306a36Sopenharmony_ci qh->data_toggle = DWC2_HC_PID_DATA0; 150462306a36Sopenharmony_ci qh->maxp = maxp; 150562306a36Sopenharmony_ci qh->maxp_mult = maxp_mult; 150662306a36Sopenharmony_ci INIT_LIST_HEAD(&qh->qtd_list); 150762306a36Sopenharmony_ci INIT_LIST_HEAD(&qh->qh_list_entry); 150862306a36Sopenharmony_ci 150962306a36Sopenharmony_ci qh->do_split = do_split; 151062306a36Sopenharmony_ci qh->dev_speed = dev_speed; 151162306a36Sopenharmony_ci 151262306a36Sopenharmony_ci if (ep_is_int || ep_is_isoc) { 151362306a36Sopenharmony_ci /* Compute scheduling parameters once and save them */ 151462306a36Sopenharmony_ci int host_speed = do_split ? USB_SPEED_HIGH : dev_speed; 151562306a36Sopenharmony_ci struct dwc2_tt *dwc_tt = dwc2_host_get_tt_info(hsotg, urb->priv, 151662306a36Sopenharmony_ci mem_flags, 151762306a36Sopenharmony_ci &qh->ttport); 151862306a36Sopenharmony_ci int device_ns; 151962306a36Sopenharmony_ci 152062306a36Sopenharmony_ci qh->dwc_tt = dwc_tt; 152162306a36Sopenharmony_ci 152262306a36Sopenharmony_ci qh->host_us = NS_TO_US(usb_calc_bus_time(host_speed, ep_is_in, 152362306a36Sopenharmony_ci ep_is_isoc, bytecount)); 152462306a36Sopenharmony_ci device_ns = usb_calc_bus_time(dev_speed, ep_is_in, 152562306a36Sopenharmony_ci ep_is_isoc, bytecount); 152662306a36Sopenharmony_ci 152762306a36Sopenharmony_ci if (do_split && dwc_tt) 152862306a36Sopenharmony_ci device_ns += dwc_tt->usb_tt->think_time; 152962306a36Sopenharmony_ci qh->device_us = NS_TO_US(device_ns); 153062306a36Sopenharmony_ci 153162306a36Sopenharmony_ci qh->device_interval = urb->interval; 153262306a36Sopenharmony_ci qh->host_interval = urb->interval * (do_split ? 8 : 1); 153362306a36Sopenharmony_ci 153462306a36Sopenharmony_ci /* 153562306a36Sopenharmony_ci * Schedule low speed if we're running the host in low or 153662306a36Sopenharmony_ci * full speed OR if we've got a "TT" to deal with to access this 153762306a36Sopenharmony_ci * device. 153862306a36Sopenharmony_ci */ 153962306a36Sopenharmony_ci qh->schedule_low_speed = prtspd != HPRT0_SPD_HIGH_SPEED || 154062306a36Sopenharmony_ci dwc_tt; 154162306a36Sopenharmony_ci 154262306a36Sopenharmony_ci if (do_split) { 154362306a36Sopenharmony_ci /* We won't know num transfers until we schedule */ 154462306a36Sopenharmony_ci qh->num_hs_transfers = -1; 154562306a36Sopenharmony_ci } else if (dev_speed == USB_SPEED_HIGH) { 154662306a36Sopenharmony_ci qh->num_hs_transfers = 1; 154762306a36Sopenharmony_ci } else { 154862306a36Sopenharmony_ci qh->num_hs_transfers = 0; 154962306a36Sopenharmony_ci } 155062306a36Sopenharmony_ci 155162306a36Sopenharmony_ci /* We'll schedule later when we have something to do */ 155262306a36Sopenharmony_ci } 155362306a36Sopenharmony_ci 155462306a36Sopenharmony_ci switch (dev_speed) { 155562306a36Sopenharmony_ci case USB_SPEED_LOW: 155662306a36Sopenharmony_ci speed = "low"; 155762306a36Sopenharmony_ci break; 155862306a36Sopenharmony_ci case USB_SPEED_FULL: 155962306a36Sopenharmony_ci speed = "full"; 156062306a36Sopenharmony_ci break; 156162306a36Sopenharmony_ci case USB_SPEED_HIGH: 156262306a36Sopenharmony_ci speed = "high"; 156362306a36Sopenharmony_ci break; 156462306a36Sopenharmony_ci default: 156562306a36Sopenharmony_ci speed = "?"; 156662306a36Sopenharmony_ci break; 156762306a36Sopenharmony_ci } 156862306a36Sopenharmony_ci 156962306a36Sopenharmony_ci switch (qh->ep_type) { 157062306a36Sopenharmony_ci case USB_ENDPOINT_XFER_ISOC: 157162306a36Sopenharmony_ci type = "isochronous"; 157262306a36Sopenharmony_ci break; 157362306a36Sopenharmony_ci case USB_ENDPOINT_XFER_INT: 157462306a36Sopenharmony_ci type = "interrupt"; 157562306a36Sopenharmony_ci break; 157662306a36Sopenharmony_ci case USB_ENDPOINT_XFER_CONTROL: 157762306a36Sopenharmony_ci type = "control"; 157862306a36Sopenharmony_ci break; 157962306a36Sopenharmony_ci case USB_ENDPOINT_XFER_BULK: 158062306a36Sopenharmony_ci type = "bulk"; 158162306a36Sopenharmony_ci break; 158262306a36Sopenharmony_ci default: 158362306a36Sopenharmony_ci type = "?"; 158462306a36Sopenharmony_ci break; 158562306a36Sopenharmony_ci } 158662306a36Sopenharmony_ci 158762306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p Init %s, %s speed, %d bytes:\n", qh, type, 158862306a36Sopenharmony_ci speed, bytecount); 158962306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p ...addr=%d, ep=%d, %s\n", qh, 159062306a36Sopenharmony_ci dwc2_hcd_get_dev_addr(&urb->pipe_info), 159162306a36Sopenharmony_ci dwc2_hcd_get_ep_num(&urb->pipe_info), 159262306a36Sopenharmony_ci ep_is_in ? "IN" : "OUT"); 159362306a36Sopenharmony_ci if (ep_is_int || ep_is_isoc) { 159462306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, 159562306a36Sopenharmony_ci "QH=%p ...duration: host=%d us, device=%d us\n", 159662306a36Sopenharmony_ci qh, qh->host_us, qh->device_us); 159762306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p ...interval: host=%d, device=%d\n", 159862306a36Sopenharmony_ci qh, qh->host_interval, qh->device_interval); 159962306a36Sopenharmony_ci if (qh->schedule_low_speed) 160062306a36Sopenharmony_ci dwc2_sch_dbg(hsotg, "QH=%p ...low speed schedule=%p\n", 160162306a36Sopenharmony_ci qh, dwc2_get_ls_map(hsotg, qh)); 160262306a36Sopenharmony_ci } 160362306a36Sopenharmony_ci} 160462306a36Sopenharmony_ci 160562306a36Sopenharmony_ci/** 160662306a36Sopenharmony_ci * dwc2_hcd_qh_create() - Allocates and initializes a QH 160762306a36Sopenharmony_ci * 160862306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 160962306a36Sopenharmony_ci * @urb: Holds the information about the device/endpoint needed 161062306a36Sopenharmony_ci * to initialize the QH 161162306a36Sopenharmony_ci * @mem_flags: Flags for allocating memory. 161262306a36Sopenharmony_ci * 161362306a36Sopenharmony_ci * Return: Pointer to the newly allocated QH, or NULL on error 161462306a36Sopenharmony_ci */ 161562306a36Sopenharmony_cistruct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg, 161662306a36Sopenharmony_ci struct dwc2_hcd_urb *urb, 161762306a36Sopenharmony_ci gfp_t mem_flags) 161862306a36Sopenharmony_ci{ 161962306a36Sopenharmony_ci struct dwc2_qh *qh; 162062306a36Sopenharmony_ci 162162306a36Sopenharmony_ci if (!urb->priv) 162262306a36Sopenharmony_ci return NULL; 162362306a36Sopenharmony_ci 162462306a36Sopenharmony_ci /* Allocate memory */ 162562306a36Sopenharmony_ci qh = kzalloc(sizeof(*qh), mem_flags); 162662306a36Sopenharmony_ci if (!qh) 162762306a36Sopenharmony_ci return NULL; 162862306a36Sopenharmony_ci 162962306a36Sopenharmony_ci dwc2_qh_init(hsotg, qh, urb, mem_flags); 163062306a36Sopenharmony_ci 163162306a36Sopenharmony_ci if (hsotg->params.dma_desc_enable && 163262306a36Sopenharmony_ci dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) { 163362306a36Sopenharmony_ci dwc2_hcd_qh_free(hsotg, qh); 163462306a36Sopenharmony_ci return NULL; 163562306a36Sopenharmony_ci } 163662306a36Sopenharmony_ci 163762306a36Sopenharmony_ci return qh; 163862306a36Sopenharmony_ci} 163962306a36Sopenharmony_ci 164062306a36Sopenharmony_ci/** 164162306a36Sopenharmony_ci * dwc2_hcd_qh_free() - Frees the QH 164262306a36Sopenharmony_ci * 164362306a36Sopenharmony_ci * @hsotg: HCD instance 164462306a36Sopenharmony_ci * @qh: The QH to free 164562306a36Sopenharmony_ci * 164662306a36Sopenharmony_ci * QH should already be removed from the list. QTD list should already be empty 164762306a36Sopenharmony_ci * if called from URB Dequeue. 164862306a36Sopenharmony_ci * 164962306a36Sopenharmony_ci * Must NOT be called with interrupt disabled or spinlock held 165062306a36Sopenharmony_ci */ 165162306a36Sopenharmony_civoid dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 165262306a36Sopenharmony_ci{ 165362306a36Sopenharmony_ci /* Make sure any unreserve work is finished. */ 165462306a36Sopenharmony_ci if (del_timer_sync(&qh->unreserve_timer)) { 165562306a36Sopenharmony_ci unsigned long flags; 165662306a36Sopenharmony_ci 165762306a36Sopenharmony_ci spin_lock_irqsave(&hsotg->lock, flags); 165862306a36Sopenharmony_ci dwc2_do_unreserve(hsotg, qh); 165962306a36Sopenharmony_ci spin_unlock_irqrestore(&hsotg->lock, flags); 166062306a36Sopenharmony_ci } 166162306a36Sopenharmony_ci 166262306a36Sopenharmony_ci /* 166362306a36Sopenharmony_ci * We don't have the lock so we can safely wait until the wait timer 166462306a36Sopenharmony_ci * finishes. Of course, at this point in time we'd better have set 166562306a36Sopenharmony_ci * wait_timer_active to false so if this timer was still pending it 166662306a36Sopenharmony_ci * won't do anything anyway, but we want it to finish before we free 166762306a36Sopenharmony_ci * memory. 166862306a36Sopenharmony_ci */ 166962306a36Sopenharmony_ci hrtimer_cancel(&qh->wait_timer); 167062306a36Sopenharmony_ci 167162306a36Sopenharmony_ci dwc2_host_put_tt_info(hsotg, qh->dwc_tt); 167262306a36Sopenharmony_ci 167362306a36Sopenharmony_ci if (qh->desc_list) 167462306a36Sopenharmony_ci dwc2_hcd_qh_free_ddma(hsotg, qh); 167562306a36Sopenharmony_ci else if (hsotg->unaligned_cache && qh->dw_align_buf) 167662306a36Sopenharmony_ci kmem_cache_free(hsotg->unaligned_cache, qh->dw_align_buf); 167762306a36Sopenharmony_ci 167862306a36Sopenharmony_ci kfree(qh); 167962306a36Sopenharmony_ci} 168062306a36Sopenharmony_ci 168162306a36Sopenharmony_ci/** 168262306a36Sopenharmony_ci * dwc2_hcd_qh_add() - Adds a QH to either the non periodic or periodic 168362306a36Sopenharmony_ci * schedule if it is not already in the schedule. If the QH is already in 168462306a36Sopenharmony_ci * the schedule, no action is taken. 168562306a36Sopenharmony_ci * 168662306a36Sopenharmony_ci * @hsotg: The HCD state structure for the DWC OTG controller 168762306a36Sopenharmony_ci * @qh: The QH to add 168862306a36Sopenharmony_ci * 168962306a36Sopenharmony_ci * Return: 0 if successful, negative error code otherwise 169062306a36Sopenharmony_ci */ 169162306a36Sopenharmony_ciint dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 169262306a36Sopenharmony_ci{ 169362306a36Sopenharmony_ci int status; 169462306a36Sopenharmony_ci u32 intr_mask; 169562306a36Sopenharmony_ci ktime_t delay; 169662306a36Sopenharmony_ci 169762306a36Sopenharmony_ci if (dbg_qh(qh)) 169862306a36Sopenharmony_ci dev_vdbg(hsotg->dev, "%s()\n", __func__); 169962306a36Sopenharmony_ci 170062306a36Sopenharmony_ci if (!list_empty(&qh->qh_list_entry)) 170162306a36Sopenharmony_ci /* QH already in a schedule */ 170262306a36Sopenharmony_ci return 0; 170362306a36Sopenharmony_ci 170462306a36Sopenharmony_ci /* Add the new QH to the appropriate schedule */ 170562306a36Sopenharmony_ci if (dwc2_qh_is_non_per(qh)) { 170662306a36Sopenharmony_ci /* Schedule right away */ 170762306a36Sopenharmony_ci qh->start_active_frame = hsotg->frame_number; 170862306a36Sopenharmony_ci qh->next_active_frame = qh->start_active_frame; 170962306a36Sopenharmony_ci 171062306a36Sopenharmony_ci if (qh->want_wait) { 171162306a36Sopenharmony_ci list_add_tail(&qh->qh_list_entry, 171262306a36Sopenharmony_ci &hsotg->non_periodic_sched_waiting); 171362306a36Sopenharmony_ci qh->wait_timer_cancel = false; 171462306a36Sopenharmony_ci delay = ktime_set(0, DWC2_RETRY_WAIT_DELAY); 171562306a36Sopenharmony_ci hrtimer_start(&qh->wait_timer, delay, HRTIMER_MODE_REL); 171662306a36Sopenharmony_ci } else { 171762306a36Sopenharmony_ci list_add_tail(&qh->qh_list_entry, 171862306a36Sopenharmony_ci &hsotg->non_periodic_sched_inactive); 171962306a36Sopenharmony_ci } 172062306a36Sopenharmony_ci return 0; 172162306a36Sopenharmony_ci } 172262306a36Sopenharmony_ci 172362306a36Sopenharmony_ci status = dwc2_schedule_periodic(hsotg, qh); 172462306a36Sopenharmony_ci if (status) 172562306a36Sopenharmony_ci return status; 172662306a36Sopenharmony_ci if (!hsotg->periodic_qh_count) { 172762306a36Sopenharmony_ci intr_mask = dwc2_readl(hsotg, GINTMSK); 172862306a36Sopenharmony_ci intr_mask |= GINTSTS_SOF; 172962306a36Sopenharmony_ci dwc2_writel(hsotg, intr_mask, GINTMSK); 173062306a36Sopenharmony_ci } 173162306a36Sopenharmony_ci hsotg->periodic_qh_count++; 173262306a36Sopenharmony_ci 173362306a36Sopenharmony_ci return 0; 173462306a36Sopenharmony_ci} 173562306a36Sopenharmony_ci 173662306a36Sopenharmony_ci/** 173762306a36Sopenharmony_ci * dwc2_hcd_qh_unlink() - Removes a QH from either the non-periodic or periodic 173862306a36Sopenharmony_ci * schedule. Memory is not freed. 173962306a36Sopenharmony_ci * 174062306a36Sopenharmony_ci * @hsotg: The HCD state structure 174162306a36Sopenharmony_ci * @qh: QH to remove from schedule 174262306a36Sopenharmony_ci */ 174362306a36Sopenharmony_civoid dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh) 174462306a36Sopenharmony_ci{ 174562306a36Sopenharmony_ci u32 intr_mask; 174662306a36Sopenharmony_ci 174762306a36Sopenharmony_ci dev_vdbg(hsotg->dev, "%s()\n", __func__); 174862306a36Sopenharmony_ci 174962306a36Sopenharmony_ci /* If the wait_timer is pending, this will stop it from acting */ 175062306a36Sopenharmony_ci qh->wait_timer_cancel = true; 175162306a36Sopenharmony_ci 175262306a36Sopenharmony_ci if (list_empty(&qh->qh_list_entry)) 175362306a36Sopenharmony_ci /* QH is not in a schedule */ 175462306a36Sopenharmony_ci return; 175562306a36Sopenharmony_ci 175662306a36Sopenharmony_ci if (dwc2_qh_is_non_per(qh)) { 175762306a36Sopenharmony_ci if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry) 175862306a36Sopenharmony_ci hsotg->non_periodic_qh_ptr = 175962306a36Sopenharmony_ci hsotg->non_periodic_qh_ptr->next; 176062306a36Sopenharmony_ci list_del_init(&qh->qh_list_entry); 176162306a36Sopenharmony_ci return; 176262306a36Sopenharmony_ci } 176362306a36Sopenharmony_ci 176462306a36Sopenharmony_ci dwc2_deschedule_periodic(hsotg, qh); 176562306a36Sopenharmony_ci hsotg->periodic_qh_count--; 176662306a36Sopenharmony_ci if (!hsotg->periodic_qh_count && 176762306a36Sopenharmony_ci !hsotg->params.dma_desc_enable) { 176862306a36Sopenharmony_ci intr_mask = dwc2_readl(hsotg, GINTMSK); 176962306a36Sopenharmony_ci intr_mask &= ~GINTSTS_SOF; 177062306a36Sopenharmony_ci dwc2_writel(hsotg, intr_mask, GINTMSK); 177162306a36Sopenharmony_ci } 177262306a36Sopenharmony_ci} 177362306a36Sopenharmony_ci 177462306a36Sopenharmony_ci/** 177562306a36Sopenharmony_ci * dwc2_next_for_periodic_split() - Set next_active_frame midway thru a split. 177662306a36Sopenharmony_ci * 177762306a36Sopenharmony_ci * This is called for setting next_active_frame for periodic splits for all but 177862306a36Sopenharmony_ci * the first packet of the split. Confusing? I thought so... 177962306a36Sopenharmony_ci * 178062306a36Sopenharmony_ci * Periodic splits are single low/full speed transfers that we end up splitting 178162306a36Sopenharmony_ci * up into several high speed transfers. They always fit into one full (1 ms) 178262306a36Sopenharmony_ci * frame but might be split over several microframes (125 us each). We to put 178362306a36Sopenharmony_ci * each of the parts on a very specific high speed frame. 178462306a36Sopenharmony_ci * 178562306a36Sopenharmony_ci * This function figures out where the next active uFrame needs to be. 178662306a36Sopenharmony_ci * 178762306a36Sopenharmony_ci * @hsotg: The HCD state structure 178862306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 178962306a36Sopenharmony_ci * @frame_number: The current frame number. 179062306a36Sopenharmony_ci * 179162306a36Sopenharmony_ci * Return: number missed by (or 0 if we didn't miss). 179262306a36Sopenharmony_ci */ 179362306a36Sopenharmony_cistatic int dwc2_next_for_periodic_split(struct dwc2_hsotg *hsotg, 179462306a36Sopenharmony_ci struct dwc2_qh *qh, u16 frame_number) 179562306a36Sopenharmony_ci{ 179662306a36Sopenharmony_ci u16 old_frame = qh->next_active_frame; 179762306a36Sopenharmony_ci u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1); 179862306a36Sopenharmony_ci int missed = 0; 179962306a36Sopenharmony_ci u16 incr; 180062306a36Sopenharmony_ci 180162306a36Sopenharmony_ci /* 180262306a36Sopenharmony_ci * See dwc2_uframe_schedule_split() for split scheduling. 180362306a36Sopenharmony_ci * 180462306a36Sopenharmony_ci * Basically: increment 1 normally, but 2 right after the start split 180562306a36Sopenharmony_ci * (except for ISOC out). 180662306a36Sopenharmony_ci */ 180762306a36Sopenharmony_ci if (old_frame == qh->start_active_frame && 180862306a36Sopenharmony_ci !(qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in)) 180962306a36Sopenharmony_ci incr = 2; 181062306a36Sopenharmony_ci else 181162306a36Sopenharmony_ci incr = 1; 181262306a36Sopenharmony_ci 181362306a36Sopenharmony_ci qh->next_active_frame = dwc2_frame_num_inc(old_frame, incr); 181462306a36Sopenharmony_ci 181562306a36Sopenharmony_ci /* 181662306a36Sopenharmony_ci * Note that it's OK for frame_number to be 1 frame past 181762306a36Sopenharmony_ci * next_active_frame. Remember that next_active_frame is supposed to 181862306a36Sopenharmony_ci * be 1 frame _before_ when we want to be scheduled. If we're 1 frame 181962306a36Sopenharmony_ci * past it just means schedule ASAP. 182062306a36Sopenharmony_ci * 182162306a36Sopenharmony_ci * It's _not_ OK, however, if we're more than one frame past. 182262306a36Sopenharmony_ci */ 182362306a36Sopenharmony_ci if (dwc2_frame_num_gt(prev_frame_number, qh->next_active_frame)) { 182462306a36Sopenharmony_ci /* 182562306a36Sopenharmony_ci * OOPS, we missed. That's actually pretty bad since 182662306a36Sopenharmony_ci * the hub will be unhappy; try ASAP I guess. 182762306a36Sopenharmony_ci */ 182862306a36Sopenharmony_ci missed = dwc2_frame_num_dec(prev_frame_number, 182962306a36Sopenharmony_ci qh->next_active_frame); 183062306a36Sopenharmony_ci qh->next_active_frame = frame_number; 183162306a36Sopenharmony_ci } 183262306a36Sopenharmony_ci 183362306a36Sopenharmony_ci return missed; 183462306a36Sopenharmony_ci} 183562306a36Sopenharmony_ci 183662306a36Sopenharmony_ci/** 183762306a36Sopenharmony_ci * dwc2_next_periodic_start() - Set next_active_frame for next transfer start 183862306a36Sopenharmony_ci * 183962306a36Sopenharmony_ci * This is called for setting next_active_frame for a periodic transfer for 184062306a36Sopenharmony_ci * all cases other than midway through a periodic split. This will also update 184162306a36Sopenharmony_ci * start_active_frame. 184262306a36Sopenharmony_ci * 184362306a36Sopenharmony_ci * Since we _always_ keep start_active_frame as the start of the previous 184462306a36Sopenharmony_ci * transfer this is normally pretty easy: we just add our interval to 184562306a36Sopenharmony_ci * start_active_frame and we've got our answer. 184662306a36Sopenharmony_ci * 184762306a36Sopenharmony_ci * The tricks come into play if we miss. In that case we'll look for the next 184862306a36Sopenharmony_ci * slot we can fit into. 184962306a36Sopenharmony_ci * 185062306a36Sopenharmony_ci * @hsotg: The HCD state structure 185162306a36Sopenharmony_ci * @qh: QH for the periodic transfer. 185262306a36Sopenharmony_ci * @frame_number: The current frame number. 185362306a36Sopenharmony_ci * 185462306a36Sopenharmony_ci * Return: number missed by (or 0 if we didn't miss). 185562306a36Sopenharmony_ci */ 185662306a36Sopenharmony_cistatic int dwc2_next_periodic_start(struct dwc2_hsotg *hsotg, 185762306a36Sopenharmony_ci struct dwc2_qh *qh, u16 frame_number) 185862306a36Sopenharmony_ci{ 185962306a36Sopenharmony_ci int missed = 0; 186062306a36Sopenharmony_ci u16 interval = qh->host_interval; 186162306a36Sopenharmony_ci u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1); 186262306a36Sopenharmony_ci 186362306a36Sopenharmony_ci qh->start_active_frame = dwc2_frame_num_inc(qh->start_active_frame, 186462306a36Sopenharmony_ci interval); 186562306a36Sopenharmony_ci 186662306a36Sopenharmony_ci /* 186762306a36Sopenharmony_ci * The dwc2_frame_num_gt() function used below won't work terribly well 186862306a36Sopenharmony_ci * with if we just incremented by a really large intervals since the 186962306a36Sopenharmony_ci * frame counter only goes to 0x3fff. It's terribly unlikely that we 187062306a36Sopenharmony_ci * will have missed in this case anyway. Just go to exit. If we want 187162306a36Sopenharmony_ci * to try to do better we'll need to keep track of a bigger counter 187262306a36Sopenharmony_ci * somewhere in the driver and handle overflows. 187362306a36Sopenharmony_ci */ 187462306a36Sopenharmony_ci if (interval >= 0x1000) 187562306a36Sopenharmony_ci goto exit; 187662306a36Sopenharmony_ci 187762306a36Sopenharmony_ci /* 187862306a36Sopenharmony_ci * Test for misses, which is when it's too late to schedule. 187962306a36Sopenharmony_ci * 188062306a36Sopenharmony_ci * A few things to note: 188162306a36Sopenharmony_ci * - We compare against prev_frame_number since start_active_frame 188262306a36Sopenharmony_ci * and next_active_frame are always 1 frame before we want things 188362306a36Sopenharmony_ci * to be active and we assume we can still get scheduled in the 188462306a36Sopenharmony_ci * current frame number. 188562306a36Sopenharmony_ci * - It's possible for start_active_frame (now incremented) to be 188662306a36Sopenharmony_ci * next_active_frame if we got an EO MISS (even_odd miss) which 188762306a36Sopenharmony_ci * basically means that we detected there wasn't enough time for 188862306a36Sopenharmony_ci * the last packet and dwc2_hc_set_even_odd_frame() rescheduled us 188962306a36Sopenharmony_ci * at the last second. We want to make sure we don't schedule 189062306a36Sopenharmony_ci * another transfer for the same frame. My test webcam doesn't seem 189162306a36Sopenharmony_ci * terribly upset by missing a transfer but really doesn't like when 189262306a36Sopenharmony_ci * we do two transfers in the same frame. 189362306a36Sopenharmony_ci * - Some misses are expected. Specifically, in order to work 189462306a36Sopenharmony_ci * perfectly dwc2 really needs quite spectacular interrupt latency 189562306a36Sopenharmony_ci * requirements. It needs to be able to handle its interrupts 189662306a36Sopenharmony_ci * completely within 125 us of them being asserted. That not only 189762306a36Sopenharmony_ci * means that the dwc2 interrupt handler needs to be fast but it 189862306a36Sopenharmony_ci * means that nothing else in the system has to block dwc2 for a long 189962306a36Sopenharmony_ci * time. We can help with the dwc2 parts of this, but it's hard to 190062306a36Sopenharmony_ci * guarantee that a system will have interrupt latency < 125 us, so 190162306a36Sopenharmony_ci * we have to be robust to some misses. 190262306a36Sopenharmony_ci */ 190362306a36Sopenharmony_ci if (qh->start_active_frame == qh->next_active_frame || 190462306a36Sopenharmony_ci dwc2_frame_num_gt(prev_frame_number, qh->start_active_frame)) { 190562306a36Sopenharmony_ci u16 ideal_start = qh->start_active_frame; 190662306a36Sopenharmony_ci int periods_in_map; 190762306a36Sopenharmony_ci 190862306a36Sopenharmony_ci /* 190962306a36Sopenharmony_ci * Adjust interval as per gcd with map size. 191062306a36Sopenharmony_ci * See pmap_schedule() for more details here. 191162306a36Sopenharmony_ci */ 191262306a36Sopenharmony_ci if (qh->do_split || qh->dev_speed == USB_SPEED_HIGH) 191362306a36Sopenharmony_ci periods_in_map = DWC2_HS_SCHEDULE_UFRAMES; 191462306a36Sopenharmony_ci else 191562306a36Sopenharmony_ci periods_in_map = DWC2_LS_SCHEDULE_FRAMES; 191662306a36Sopenharmony_ci interval = gcd(interval, periods_in_map); 191762306a36Sopenharmony_ci 191862306a36Sopenharmony_ci do { 191962306a36Sopenharmony_ci qh->start_active_frame = dwc2_frame_num_inc( 192062306a36Sopenharmony_ci qh->start_active_frame, interval); 192162306a36Sopenharmony_ci } while (dwc2_frame_num_gt(prev_frame_number, 192262306a36Sopenharmony_ci qh->start_active_frame)); 192362306a36Sopenharmony_ci 192462306a36Sopenharmony_ci missed = dwc2_frame_num_dec(qh->start_active_frame, 192562306a36Sopenharmony_ci ideal_start); 192662306a36Sopenharmony_ci } 192762306a36Sopenharmony_ci 192862306a36Sopenharmony_ciexit: 192962306a36Sopenharmony_ci qh->next_active_frame = qh->start_active_frame; 193062306a36Sopenharmony_ci 193162306a36Sopenharmony_ci return missed; 193262306a36Sopenharmony_ci} 193362306a36Sopenharmony_ci 193462306a36Sopenharmony_ci/* 193562306a36Sopenharmony_ci * Deactivates a QH. For non-periodic QHs, removes the QH from the active 193662306a36Sopenharmony_ci * non-periodic schedule. The QH is added to the inactive non-periodic 193762306a36Sopenharmony_ci * schedule if any QTDs are still attached to the QH. 193862306a36Sopenharmony_ci * 193962306a36Sopenharmony_ci * For periodic QHs, the QH is removed from the periodic queued schedule. If 194062306a36Sopenharmony_ci * there are any QTDs still attached to the QH, the QH is added to either the 194162306a36Sopenharmony_ci * periodic inactive schedule or the periodic ready schedule and its next 194262306a36Sopenharmony_ci * scheduled frame is calculated. The QH is placed in the ready schedule if 194362306a36Sopenharmony_ci * the scheduled frame has been reached already. Otherwise it's placed in the 194462306a36Sopenharmony_ci * inactive schedule. If there are no QTDs attached to the QH, the QH is 194562306a36Sopenharmony_ci * completely removed from the periodic schedule. 194662306a36Sopenharmony_ci */ 194762306a36Sopenharmony_civoid dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 194862306a36Sopenharmony_ci int sched_next_periodic_split) 194962306a36Sopenharmony_ci{ 195062306a36Sopenharmony_ci u16 old_frame = qh->next_active_frame; 195162306a36Sopenharmony_ci u16 frame_number; 195262306a36Sopenharmony_ci int missed; 195362306a36Sopenharmony_ci 195462306a36Sopenharmony_ci if (dbg_qh(qh)) 195562306a36Sopenharmony_ci dev_vdbg(hsotg->dev, "%s()\n", __func__); 195662306a36Sopenharmony_ci 195762306a36Sopenharmony_ci if (dwc2_qh_is_non_per(qh)) { 195862306a36Sopenharmony_ci dwc2_hcd_qh_unlink(hsotg, qh); 195962306a36Sopenharmony_ci if (!list_empty(&qh->qtd_list)) 196062306a36Sopenharmony_ci /* Add back to inactive/waiting non-periodic schedule */ 196162306a36Sopenharmony_ci dwc2_hcd_qh_add(hsotg, qh); 196262306a36Sopenharmony_ci return; 196362306a36Sopenharmony_ci } 196462306a36Sopenharmony_ci 196562306a36Sopenharmony_ci /* 196662306a36Sopenharmony_ci * Use the real frame number rather than the cached value as of the 196762306a36Sopenharmony_ci * last SOF just to get us a little closer to reality. Note that 196862306a36Sopenharmony_ci * means we don't actually know if we've already handled the SOF 196962306a36Sopenharmony_ci * interrupt for this frame. 197062306a36Sopenharmony_ci */ 197162306a36Sopenharmony_ci frame_number = dwc2_hcd_get_frame_number(hsotg); 197262306a36Sopenharmony_ci 197362306a36Sopenharmony_ci if (sched_next_periodic_split) 197462306a36Sopenharmony_ci missed = dwc2_next_for_periodic_split(hsotg, qh, frame_number); 197562306a36Sopenharmony_ci else 197662306a36Sopenharmony_ci missed = dwc2_next_periodic_start(hsotg, qh, frame_number); 197762306a36Sopenharmony_ci 197862306a36Sopenharmony_ci dwc2_sch_vdbg(hsotg, 197962306a36Sopenharmony_ci "QH=%p next(%d) fn=%04x, sch=%04x=>%04x (%+d) miss=%d %s\n", 198062306a36Sopenharmony_ci qh, sched_next_periodic_split, frame_number, old_frame, 198162306a36Sopenharmony_ci qh->next_active_frame, 198262306a36Sopenharmony_ci dwc2_frame_num_dec(qh->next_active_frame, old_frame), 198362306a36Sopenharmony_ci missed, missed ? "MISS" : ""); 198462306a36Sopenharmony_ci 198562306a36Sopenharmony_ci if (list_empty(&qh->qtd_list)) { 198662306a36Sopenharmony_ci dwc2_hcd_qh_unlink(hsotg, qh); 198762306a36Sopenharmony_ci return; 198862306a36Sopenharmony_ci } 198962306a36Sopenharmony_ci 199062306a36Sopenharmony_ci /* 199162306a36Sopenharmony_ci * Remove from periodic_sched_queued and move to 199262306a36Sopenharmony_ci * appropriate queue 199362306a36Sopenharmony_ci * 199462306a36Sopenharmony_ci * Note: we purposely use the frame_number from the "hsotg" structure 199562306a36Sopenharmony_ci * since we know SOF interrupt will handle future frames. 199662306a36Sopenharmony_ci */ 199762306a36Sopenharmony_ci if (dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number)) 199862306a36Sopenharmony_ci list_move_tail(&qh->qh_list_entry, 199962306a36Sopenharmony_ci &hsotg->periodic_sched_ready); 200062306a36Sopenharmony_ci else 200162306a36Sopenharmony_ci list_move_tail(&qh->qh_list_entry, 200262306a36Sopenharmony_ci &hsotg->periodic_sched_inactive); 200362306a36Sopenharmony_ci} 200462306a36Sopenharmony_ci 200562306a36Sopenharmony_ci/** 200662306a36Sopenharmony_ci * dwc2_hcd_qtd_init() - Initializes a QTD structure 200762306a36Sopenharmony_ci * 200862306a36Sopenharmony_ci * @qtd: The QTD to initialize 200962306a36Sopenharmony_ci * @urb: The associated URB 201062306a36Sopenharmony_ci */ 201162306a36Sopenharmony_civoid dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb) 201262306a36Sopenharmony_ci{ 201362306a36Sopenharmony_ci qtd->urb = urb; 201462306a36Sopenharmony_ci if (dwc2_hcd_get_pipe_type(&urb->pipe_info) == 201562306a36Sopenharmony_ci USB_ENDPOINT_XFER_CONTROL) { 201662306a36Sopenharmony_ci /* 201762306a36Sopenharmony_ci * The only time the QTD data toggle is used is on the data 201862306a36Sopenharmony_ci * phase of control transfers. This phase always starts with 201962306a36Sopenharmony_ci * DATA1. 202062306a36Sopenharmony_ci */ 202162306a36Sopenharmony_ci qtd->data_toggle = DWC2_HC_PID_DATA1; 202262306a36Sopenharmony_ci qtd->control_phase = DWC2_CONTROL_SETUP; 202362306a36Sopenharmony_ci } 202462306a36Sopenharmony_ci 202562306a36Sopenharmony_ci /* Start split */ 202662306a36Sopenharmony_ci qtd->complete_split = 0; 202762306a36Sopenharmony_ci qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL; 202862306a36Sopenharmony_ci qtd->isoc_split_offset = 0; 202962306a36Sopenharmony_ci qtd->in_process = 0; 203062306a36Sopenharmony_ci 203162306a36Sopenharmony_ci /* Store the qtd ptr in the urb to reference the QTD */ 203262306a36Sopenharmony_ci urb->qtd = qtd; 203362306a36Sopenharmony_ci} 203462306a36Sopenharmony_ci 203562306a36Sopenharmony_ci/** 203662306a36Sopenharmony_ci * dwc2_hcd_qtd_add() - Adds a QTD to the QTD-list of a QH 203762306a36Sopenharmony_ci * Caller must hold driver lock. 203862306a36Sopenharmony_ci * 203962306a36Sopenharmony_ci * @hsotg: The DWC HCD structure 204062306a36Sopenharmony_ci * @qtd: The QTD to add 204162306a36Sopenharmony_ci * @qh: Queue head to add qtd to 204262306a36Sopenharmony_ci * 204362306a36Sopenharmony_ci * Return: 0 if successful, negative error code otherwise 204462306a36Sopenharmony_ci * 204562306a36Sopenharmony_ci * If the QH to which the QTD is added is not currently scheduled, it is placed 204662306a36Sopenharmony_ci * into the proper schedule based on its EP type. 204762306a36Sopenharmony_ci */ 204862306a36Sopenharmony_ciint dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd, 204962306a36Sopenharmony_ci struct dwc2_qh *qh) 205062306a36Sopenharmony_ci{ 205162306a36Sopenharmony_ci int retval; 205262306a36Sopenharmony_ci 205362306a36Sopenharmony_ci if (unlikely(!qh)) { 205462306a36Sopenharmony_ci dev_err(hsotg->dev, "%s: Invalid QH\n", __func__); 205562306a36Sopenharmony_ci retval = -EINVAL; 205662306a36Sopenharmony_ci goto fail; 205762306a36Sopenharmony_ci } 205862306a36Sopenharmony_ci 205962306a36Sopenharmony_ci retval = dwc2_hcd_qh_add(hsotg, qh); 206062306a36Sopenharmony_ci if (retval) 206162306a36Sopenharmony_ci goto fail; 206262306a36Sopenharmony_ci 206362306a36Sopenharmony_ci qtd->qh = qh; 206462306a36Sopenharmony_ci list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list); 206562306a36Sopenharmony_ci 206662306a36Sopenharmony_ci return 0; 206762306a36Sopenharmony_cifail: 206862306a36Sopenharmony_ci return retval; 206962306a36Sopenharmony_ci} 2070