tb.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - bus logic (NHI independent)
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2019, Intel Corporation
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/errno.h>
  10. #include <linux/delay.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/platform_data/x86/apple.h>
  13. #include "tb.h"
  14. #include "tb_regs.h"
  15. #include "tunnel.h"
  16. #define TB_TIMEOUT 100 /* ms */
  17. #define TB_RELEASE_BW_TIMEOUT 10000 /* ms */
  18. /*
  19. * How many time bandwidth allocation request from graphics driver is
  20. * retried if the DP tunnel is still activating.
  21. */
  22. #define TB_BW_ALLOC_RETRIES 3
  23. /*
  24. * Minimum bandwidth (in Mb/s) that is needed in the single transmitter/receiver
  25. * direction. This is 40G - 10% guard band bandwidth.
  26. */
  27. #define TB_ASYM_MIN (40000 * 90 / 100)
  28. /*
  29. * Threshold bandwidth (in Mb/s) that is used to switch the links to
  30. * asymmetric and back. This is selected as 45G which means when the
  31. * request is higher than this, we switch the link to asymmetric, and
  32. * when it is less than this we switch it back. The 45G is selected so
  33. * that we still have 27G (of the total 72G) for bulk PCIe traffic when
  34. * switching back to symmetric.
  35. */
  36. #define TB_ASYM_THRESHOLD 45000
  37. #define MAX_GROUPS 7 /* max Group_ID is 7 */
  38. static unsigned int asym_threshold = TB_ASYM_THRESHOLD;
  39. module_param_named(asym_threshold, asym_threshold, uint, 0444);
  40. MODULE_PARM_DESC(asym_threshold,
  41. "threshold (Mb/s) when to Gen 4 switch link symmetry. 0 disables. (default: "
  42. __MODULE_STRING(TB_ASYM_THRESHOLD) ")");
  43. /**
  44. * struct tb_cm - Simple Thunderbolt connection manager
  45. * @tunnel_list: List of active tunnels
  46. * @dp_resources: List of available DP resources for DP tunneling
  47. * @hotplug_active: tb_handle_hotplug will stop progressing plug
  48. * events and exit if this is not set (it needs to
  49. * acquire the lock one more time). Used to drain wq
  50. * after cfg has been paused.
  51. * @remove_work: Work used to remove any unplugged routers after
  52. * runtime resume
  53. * @groups: Bandwidth groups used in this domain.
  54. */
  55. struct tb_cm {
  56. struct list_head tunnel_list;
  57. struct list_head dp_resources;
  58. bool hotplug_active;
  59. struct delayed_work remove_work;
  60. struct tb_bandwidth_group groups[MAX_GROUPS];
  61. };
  62. static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
  63. {
  64. return ((void *)tcm - sizeof(struct tb));
  65. }
  66. struct tb_hotplug_event {
  67. struct delayed_work work;
  68. struct tb *tb;
  69. u64 route;
  70. u8 port;
  71. bool unplug;
  72. int retry;
  73. };
  74. static void tb_scan_port(struct tb_port *port);
  75. static void tb_handle_hotplug(struct work_struct *work);
  76. static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port,
  77. const char *reason);
  78. static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port,
  79. int retry, unsigned long delay);
  80. static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
  81. {
  82. struct tb_hotplug_event *ev;
  83. ev = kmalloc_obj(*ev);
  84. if (!ev)
  85. return;
  86. ev->tb = tb;
  87. ev->route = route;
  88. ev->port = port;
  89. ev->unplug = unplug;
  90. INIT_DELAYED_WORK(&ev->work, tb_handle_hotplug);
  91. queue_delayed_work(tb->wq, &ev->work, 0);
  92. }
  93. /* enumeration & hot plug handling */
  94. static void tb_add_dp_resources(struct tb_switch *sw)
  95. {
  96. struct tb_cm *tcm = tb_priv(sw->tb);
  97. struct tb_port *port;
  98. tb_switch_for_each_port(sw, port) {
  99. if (!tb_port_is_dpin(port))
  100. continue;
  101. if (!tb_switch_query_dp_resource(sw, port))
  102. continue;
  103. /*
  104. * If DP IN on device router exist, position it at the
  105. * beginning of the DP resources list, so that it is used
  106. * before DP IN of the host router. This way external GPU(s)
  107. * will be prioritized when pairing DP IN to a DP OUT.
  108. */
  109. if (tb_route(sw))
  110. list_add(&port->list, &tcm->dp_resources);
  111. else
  112. list_add_tail(&port->list, &tcm->dp_resources);
  113. tb_port_dbg(port, "DP IN resource available\n");
  114. }
  115. }
  116. static void tb_remove_dp_resources(struct tb_switch *sw)
  117. {
  118. struct tb_cm *tcm = tb_priv(sw->tb);
  119. struct tb_port *port, *tmp;
  120. /* Clear children resources first */
  121. tb_switch_for_each_port(sw, port) {
  122. if (tb_port_has_remote(port))
  123. tb_remove_dp_resources(port->remote->sw);
  124. }
  125. list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
  126. if (port->sw == sw) {
  127. tb_port_dbg(port, "DP OUT resource unavailable\n");
  128. list_del_init(&port->list);
  129. }
  130. }
  131. }
  132. static void tb_discover_dp_resource(struct tb *tb, struct tb_port *port)
  133. {
  134. struct tb_cm *tcm = tb_priv(tb);
  135. struct tb_port *p;
  136. list_for_each_entry(p, &tcm->dp_resources, list) {
  137. if (p == port)
  138. return;
  139. }
  140. tb_port_dbg(port, "DP %s resource available discovered\n",
  141. tb_port_is_dpin(port) ? "IN" : "OUT");
  142. list_add_tail(&port->list, &tcm->dp_resources);
  143. }
  144. static void tb_discover_dp_resources(struct tb *tb)
  145. {
  146. struct tb_cm *tcm = tb_priv(tb);
  147. struct tb_tunnel *tunnel;
  148. list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
  149. if (tb_tunnel_is_dp(tunnel))
  150. tb_discover_dp_resource(tb, tunnel->dst_port);
  151. }
  152. }
  153. /* Enables CL states up to host router */
  154. static int tb_enable_clx(struct tb_switch *sw)
  155. {
  156. struct tb_cm *tcm = tb_priv(sw->tb);
  157. unsigned int clx = TB_CL0S | TB_CL1;
  158. const struct tb_tunnel *tunnel;
  159. int ret;
  160. /*
  161. * Currently only enable CLx for the first link. This is enough
  162. * to allow the CPU to save energy at least on Intel hardware
  163. * and makes it slightly simpler to implement. We may change
  164. * this in the future to cover the whole topology if it turns
  165. * out to be beneficial.
  166. */
  167. while (sw && tb_switch_depth(sw) > 1)
  168. sw = tb_switch_parent(sw);
  169. if (!sw)
  170. return 0;
  171. if (tb_switch_depth(sw) != 1)
  172. return 0;
  173. /*
  174. * If we are re-enabling then check if there is an active DMA
  175. * tunnel and in that case bail out.
  176. */
  177. list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
  178. if (tb_tunnel_is_dma(tunnel)) {
  179. if (tb_tunnel_port_on_path(tunnel, tb_upstream_port(sw)))
  180. return 0;
  181. }
  182. }
  183. /*
  184. * Initially try with CL2. If that's not supported by the
  185. * topology try with CL0s and CL1 and then give up.
  186. */
  187. ret = tb_switch_clx_enable(sw, clx | TB_CL2);
  188. if (ret == -EOPNOTSUPP)
  189. ret = tb_switch_clx_enable(sw, clx);
  190. return ret == -EOPNOTSUPP ? 0 : ret;
  191. }
  192. /*
  193. * Disables CL states from @sw up to the host router.
  194. *
  195. * This can be used to figure out whether the link was setup by us or the
  196. * boot firmware so we don't accidentally enable them if they were not
  197. * enabled during discovery.
  198. */
  199. static bool tb_disable_clx(struct tb_switch *sw)
  200. {
  201. bool disabled = false;
  202. do {
  203. int ret;
  204. ret = tb_switch_clx_disable(sw);
  205. if (ret > 0)
  206. disabled = true;
  207. else if (ret < 0)
  208. tb_sw_warn(sw, "failed to disable CL states\n");
  209. sw = tb_switch_parent(sw);
  210. } while (sw);
  211. return disabled;
  212. }
  213. static int tb_increase_switch_tmu_accuracy(struct device *dev, void *data)
  214. {
  215. struct tb_switch *sw;
  216. sw = tb_to_switch(dev);
  217. if (!sw)
  218. return 0;
  219. if (tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_LOWRES)) {
  220. enum tb_switch_tmu_mode mode;
  221. int ret;
  222. if (tb_switch_clx_is_enabled(sw, TB_CL1))
  223. mode = TB_SWITCH_TMU_MODE_HIFI_UNI;
  224. else
  225. mode = TB_SWITCH_TMU_MODE_HIFI_BI;
  226. ret = tb_switch_tmu_configure(sw, mode);
  227. if (ret)
  228. return ret;
  229. return tb_switch_tmu_enable(sw);
  230. }
  231. return 0;
  232. }
  233. static void tb_increase_tmu_accuracy(struct tb_tunnel *tunnel)
  234. {
  235. struct tb_switch *sw;
  236. if (!tunnel)
  237. return;
  238. /*
  239. * Once first DP tunnel is established we change the TMU
  240. * accuracy of first depth child routers (and the host router)
  241. * to the highest. This is needed for the DP tunneling to work
  242. * but also allows CL0s.
  243. *
  244. * If both routers are v2 then we don't need to do anything as
  245. * they are using enhanced TMU mode that allows all CLx.
  246. */
  247. sw = tunnel->tb->root_switch;
  248. device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy);
  249. }
  250. static int tb_switch_tmu_hifi_uni_required(struct device *dev, void *not_used)
  251. {
  252. struct tb_switch *sw = tb_to_switch(dev);
  253. if (sw && tb_switch_tmu_is_enabled(sw) &&
  254. tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_HIFI_UNI))
  255. return 1;
  256. return device_for_each_child(dev, NULL,
  257. tb_switch_tmu_hifi_uni_required);
  258. }
  259. static bool tb_tmu_hifi_uni_required(struct tb *tb)
  260. {
  261. return device_for_each_child(&tb->dev, NULL,
  262. tb_switch_tmu_hifi_uni_required) == 1;
  263. }
  264. static int tb_enable_tmu(struct tb_switch *sw)
  265. {
  266. int ret;
  267. /*
  268. * If both routers at the end of the link are v2 we simply
  269. * enable the enhanced uni-directional mode. That covers all
  270. * the CL states. For v1 and before we need to use the normal
  271. * rate to allow CL1 (when supported). Otherwise we keep the TMU
  272. * running at the highest accuracy.
  273. */
  274. ret = tb_switch_tmu_configure(sw,
  275. TB_SWITCH_TMU_MODE_MEDRES_ENHANCED_UNI);
  276. if (ret == -EOPNOTSUPP) {
  277. if (tb_switch_clx_is_enabled(sw, TB_CL1)) {
  278. /*
  279. * Figure out uni-directional HiFi TMU requirements
  280. * currently in the domain. If there are no
  281. * uni-directional HiFi requirements we can put the TMU
  282. * into LowRes mode.
  283. *
  284. * Deliberately skip bi-directional HiFi links
  285. * as these work independently of other links
  286. * (and they do not allow any CL states anyway).
  287. */
  288. if (tb_tmu_hifi_uni_required(sw->tb))
  289. ret = tb_switch_tmu_configure(sw,
  290. TB_SWITCH_TMU_MODE_HIFI_UNI);
  291. else
  292. ret = tb_switch_tmu_configure(sw,
  293. TB_SWITCH_TMU_MODE_LOWRES);
  294. } else {
  295. ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI);
  296. }
  297. /* If not supported, fallback to bi-directional HiFi */
  298. if (ret == -EOPNOTSUPP)
  299. ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI);
  300. }
  301. if (ret)
  302. return ret;
  303. /* If it is already enabled in correct mode, don't touch it */
  304. if (tb_switch_tmu_is_enabled(sw))
  305. return 0;
  306. ret = tb_switch_tmu_disable(sw);
  307. if (ret)
  308. return ret;
  309. ret = tb_switch_tmu_post_time(sw);
  310. if (ret)
  311. return ret;
  312. return tb_switch_tmu_enable(sw);
  313. }
  314. static void tb_switch_discover_tunnels(struct tb_switch *sw,
  315. struct list_head *list,
  316. bool alloc_hopids)
  317. {
  318. struct tb *tb = sw->tb;
  319. struct tb_port *port;
  320. tb_switch_for_each_port(sw, port) {
  321. struct tb_tunnel *tunnel = NULL;
  322. switch (port->config.type) {
  323. case TB_TYPE_DP_HDMI_IN:
  324. tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids);
  325. tb_increase_tmu_accuracy(tunnel);
  326. break;
  327. case TB_TYPE_PCIE_DOWN:
  328. tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
  329. break;
  330. case TB_TYPE_USB3_DOWN:
  331. tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
  332. break;
  333. default:
  334. break;
  335. }
  336. if (tunnel)
  337. list_add_tail(&tunnel->list, list);
  338. }
  339. tb_switch_for_each_port(sw, port) {
  340. if (tb_port_has_remote(port)) {
  341. tb_switch_discover_tunnels(port->remote->sw, list,
  342. alloc_hopids);
  343. }
  344. }
  345. }
  346. static int tb_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
  347. {
  348. if (tb_switch_is_usb4(port->sw))
  349. return usb4_port_configure_xdomain(port, xd);
  350. return tb_lc_configure_xdomain(port);
  351. }
  352. static void tb_port_unconfigure_xdomain(struct tb_port *port)
  353. {
  354. if (tb_switch_is_usb4(port->sw))
  355. usb4_port_unconfigure_xdomain(port);
  356. else
  357. tb_lc_unconfigure_xdomain(port);
  358. }
  359. static void tb_scan_xdomain(struct tb_port *port)
  360. {
  361. struct tb_switch *sw = port->sw;
  362. struct tb *tb = sw->tb;
  363. struct tb_xdomain *xd;
  364. u64 route;
  365. if (!tb_is_xdomain_enabled())
  366. return;
  367. route = tb_downstream_route(port);
  368. xd = tb_xdomain_find_by_route(tb, route);
  369. if (xd) {
  370. tb_xdomain_put(xd);
  371. return;
  372. }
  373. xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
  374. NULL);
  375. if (xd) {
  376. tb_port_at(route, sw)->xdomain = xd;
  377. tb_port_configure_xdomain(port, xd);
  378. tb_xdomain_add(xd);
  379. }
  380. }
  381. /*
  382. * Returns the first inactive port on @sw.
  383. */
  384. static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
  385. enum tb_port_type type)
  386. {
  387. struct tb_port *port;
  388. tb_switch_for_each_port(sw, port) {
  389. if (tb_is_upstream_port(port))
  390. continue;
  391. if (port->config.type != type)
  392. continue;
  393. if (!port->cap_adap)
  394. continue;
  395. if (tb_port_is_enabled(port))
  396. continue;
  397. return port;
  398. }
  399. return NULL;
  400. }
  401. static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
  402. const struct tb_port *port)
  403. {
  404. struct tb_port *down;
  405. down = usb4_switch_map_usb3_down(sw, port);
  406. if (down && !tb_usb3_port_is_enabled(down))
  407. return down;
  408. return NULL;
  409. }
  410. static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
  411. struct tb_port *src_port,
  412. struct tb_port *dst_port)
  413. {
  414. struct tb_cm *tcm = tb_priv(tb);
  415. struct tb_tunnel *tunnel;
  416. list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
  417. if (tunnel->type == type &&
  418. ((src_port && src_port == tunnel->src_port) ||
  419. (dst_port && dst_port == tunnel->dst_port))) {
  420. return tunnel;
  421. }
  422. }
  423. return NULL;
  424. }
  425. static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
  426. struct tb_port *src_port,
  427. struct tb_port *dst_port)
  428. {
  429. struct tb_port *port, *usb3_down;
  430. struct tb_switch *sw;
  431. /* Pick the router that is deepest in the topology */
  432. if (tb_port_path_direction_downstream(src_port, dst_port))
  433. sw = dst_port->sw;
  434. else
  435. sw = src_port->sw;
  436. /* Can't be the host router */
  437. if (sw == tb->root_switch)
  438. return NULL;
  439. /* Find the downstream USB4 port that leads to this router */
  440. port = tb_port_at(tb_route(sw), tb->root_switch);
  441. /* Find the corresponding host router USB3 downstream port */
  442. usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
  443. if (!usb3_down)
  444. return NULL;
  445. return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
  446. }
  447. /**
  448. * tb_consumed_usb3_pcie_bandwidth() - Consumed USB3/PCIe bandwidth over a single link
  449. * @tb: Domain structure
  450. * @src_port: Source protocol adapter
  451. * @dst_port: Destination protocol adapter
  452. * @port: USB4 port the consumed bandwidth is calculated
  453. * @consumed_up: Consumed upstream bandwidth (Mb/s)
  454. * @consumed_down: Consumed downstream bandwidth (Mb/s)
  455. *
  456. * Calculates consumed USB3 and PCIe bandwidth at @port between path
  457. * from @src_port to @dst_port. Does not take USB3 tunnel starting from
  458. * @src_port and ending on @src_port into account because that bandwidth is
  459. * already included in as part of the "first hop" USB3 tunnel.
  460. *
  461. * Return: %0 on success, negative errno otherwise.
  462. */
  463. static int tb_consumed_usb3_pcie_bandwidth(struct tb *tb,
  464. struct tb_port *src_port,
  465. struct tb_port *dst_port,
  466. struct tb_port *port,
  467. int *consumed_up,
  468. int *consumed_down)
  469. {
  470. int pci_consumed_up, pci_consumed_down;
  471. struct tb_tunnel *tunnel;
  472. *consumed_up = *consumed_down = 0;
  473. tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
  474. if (tunnel && !tb_port_is_usb3_down(src_port) &&
  475. !tb_port_is_usb3_up(dst_port)) {
  476. int ret;
  477. ret = tb_tunnel_consumed_bandwidth(tunnel, consumed_up,
  478. consumed_down);
  479. if (ret)
  480. return ret;
  481. }
  482. /*
  483. * If there is anything reserved for PCIe bulk traffic take it
  484. * into account here too.
  485. */
  486. if (tb_tunnel_reserved_pci(port, &pci_consumed_up, &pci_consumed_down)) {
  487. *consumed_up += pci_consumed_up;
  488. *consumed_down += pci_consumed_down;
  489. }
  490. return 0;
  491. }
  492. /**
  493. * tb_consumed_dp_bandwidth() - Consumed DP bandwidth over a single link
  494. * @tb: Domain structure
  495. * @src_port: Source protocol adapter
  496. * @dst_port: Destination protocol adapter
  497. * @port: USB4 port the consumed bandwidth is calculated
  498. * @consumed_up: Consumed upstream bandwidth (Mb/s)
  499. * @consumed_down: Consumed downstream bandwidth (Mb/s)
  500. *
  501. * Calculates consumed DP bandwidth at @port between path from @src_port
  502. * to @dst_port. Does not take tunnel starting from @src_port and ending
  503. * from @src_port into account.
  504. *
  505. * If there is bandwidth reserved for any of the groups between
  506. * @src_port and @dst_port (but not yet used) that is also taken into
  507. * account in the returned consumed bandwidth.
  508. *
  509. * Return: %0 on success, negative errno otherwise.
  510. */
  511. static int tb_consumed_dp_bandwidth(struct tb *tb,
  512. struct tb_port *src_port,
  513. struct tb_port *dst_port,
  514. struct tb_port *port,
  515. int *consumed_up,
  516. int *consumed_down)
  517. {
  518. int group_reserved[MAX_GROUPS] = {};
  519. struct tb_cm *tcm = tb_priv(tb);
  520. struct tb_tunnel *tunnel;
  521. bool downstream;
  522. int i, ret;
  523. *consumed_up = *consumed_down = 0;
  524. /*
  525. * Find all DP tunnels that cross the port and reduce
  526. * their consumed bandwidth from the available.
  527. */
  528. list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
  529. const struct tb_bandwidth_group *group;
  530. int dp_consumed_up, dp_consumed_down;
  531. if (tb_tunnel_is_invalid(tunnel))
  532. continue;
  533. if (!tb_tunnel_is_dp(tunnel))
  534. continue;
  535. if (!tb_tunnel_port_on_path(tunnel, port))
  536. continue;
  537. /*
  538. * Calculate what is reserved for groups crossing the
  539. * same ports only once (as that is reserved for all the
  540. * tunnels in the group).
  541. */
  542. group = tunnel->src_port->group;
  543. if (group && group->reserved && !group_reserved[group->index])
  544. group_reserved[group->index] = group->reserved;
  545. /*
  546. * Ignore the DP tunnel between src_port and dst_port
  547. * because it is the same tunnel and we may be
  548. * re-calculating estimated bandwidth.
  549. */
  550. if (tunnel->src_port == src_port &&
  551. tunnel->dst_port == dst_port)
  552. continue;
  553. ret = tb_tunnel_consumed_bandwidth(tunnel, &dp_consumed_up,
  554. &dp_consumed_down);
  555. if (ret)
  556. return ret;
  557. *consumed_up += dp_consumed_up;
  558. *consumed_down += dp_consumed_down;
  559. }
  560. downstream = tb_port_path_direction_downstream(src_port, dst_port);
  561. for (i = 0; i < ARRAY_SIZE(group_reserved); i++) {
  562. if (downstream)
  563. *consumed_down += group_reserved[i];
  564. else
  565. *consumed_up += group_reserved[i];
  566. }
  567. return 0;
  568. }
  569. static bool tb_asym_supported(struct tb_port *src_port, struct tb_port *dst_port,
  570. struct tb_port *port)
  571. {
  572. bool downstream = tb_port_path_direction_downstream(src_port, dst_port);
  573. enum tb_link_width width;
  574. if (tb_is_upstream_port(port))
  575. width = downstream ? TB_LINK_WIDTH_ASYM_RX : TB_LINK_WIDTH_ASYM_TX;
  576. else
  577. width = downstream ? TB_LINK_WIDTH_ASYM_TX : TB_LINK_WIDTH_ASYM_RX;
  578. return tb_port_width_supported(port, width);
  579. }
  580. /**
  581. * tb_maximum_bandwidth() - Maximum bandwidth over a single link
  582. * @tb: Domain structure
  583. * @src_port: Source protocol adapter
  584. * @dst_port: Destination protocol adapter
  585. * @port: USB4 port the total bandwidth is calculated
  586. * @max_up: Maximum upstream bandwidth (Mb/s)
  587. * @max_down: Maximum downstream bandwidth (Mb/s)
  588. * @include_asym: Include bandwidth if the link is switched from
  589. * symmetric to asymmetric
  590. *
  591. * Returns maximum possible bandwidth in @max_up and @max_down over a
  592. * single link at @port. If @include_asym is set then includes the
  593. * additional banwdith if the links are transitioned into asymmetric to
  594. * direction from @src_port to @dst_port.
  595. *
  596. * Return: %0 on success, negative errno otherwise.
  597. */
  598. static int tb_maximum_bandwidth(struct tb *tb, struct tb_port *src_port,
  599. struct tb_port *dst_port, struct tb_port *port,
  600. int *max_up, int *max_down, bool include_asym)
  601. {
  602. bool downstream = tb_port_path_direction_downstream(src_port, dst_port);
  603. int link_speed, link_width, up_bw, down_bw;
  604. /*
  605. * Can include asymmetric, only if it is actually supported by
  606. * the lane adapter.
  607. */
  608. if (!tb_asym_supported(src_port, dst_port, port))
  609. include_asym = false;
  610. if (tb_is_upstream_port(port)) {
  611. link_speed = port->sw->link_speed;
  612. /*
  613. * sw->link_width is from upstream perspective so we use
  614. * the opposite for downstream of the host router.
  615. */
  616. if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) {
  617. up_bw = link_speed * 3 * 1000;
  618. down_bw = link_speed * 1 * 1000;
  619. } else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) {
  620. up_bw = link_speed * 1 * 1000;
  621. down_bw = link_speed * 3 * 1000;
  622. } else if (include_asym) {
  623. /*
  624. * The link is symmetric at the moment but we
  625. * can switch it to asymmetric as needed. Report
  626. * this bandwidth as available (even though it
  627. * is not yet enabled).
  628. */
  629. if (downstream) {
  630. up_bw = link_speed * 1 * 1000;
  631. down_bw = link_speed * 3 * 1000;
  632. } else {
  633. up_bw = link_speed * 3 * 1000;
  634. down_bw = link_speed * 1 * 1000;
  635. }
  636. } else {
  637. up_bw = link_speed * port->sw->link_width * 1000;
  638. down_bw = up_bw;
  639. }
  640. } else {
  641. link_speed = tb_port_get_link_speed(port);
  642. if (link_speed < 0)
  643. return link_speed;
  644. link_width = tb_port_get_link_width(port);
  645. if (link_width < 0)
  646. return link_width;
  647. if (link_width == TB_LINK_WIDTH_ASYM_TX) {
  648. up_bw = link_speed * 1 * 1000;
  649. down_bw = link_speed * 3 * 1000;
  650. } else if (link_width == TB_LINK_WIDTH_ASYM_RX) {
  651. up_bw = link_speed * 3 * 1000;
  652. down_bw = link_speed * 1 * 1000;
  653. } else if (include_asym) {
  654. /*
  655. * The link is symmetric at the moment but we
  656. * can switch it to asymmetric as needed. Report
  657. * this bandwidth as available (even though it
  658. * is not yet enabled).
  659. */
  660. if (downstream) {
  661. up_bw = link_speed * 1 * 1000;
  662. down_bw = link_speed * 3 * 1000;
  663. } else {
  664. up_bw = link_speed * 3 * 1000;
  665. down_bw = link_speed * 1 * 1000;
  666. }
  667. } else {
  668. up_bw = link_speed * link_width * 1000;
  669. down_bw = up_bw;
  670. }
  671. }
  672. /* Leave 10% guard band */
  673. *max_up = up_bw - up_bw / 10;
  674. *max_down = down_bw - down_bw / 10;
  675. tb_port_dbg(port, "link maximum bandwidth %d/%d Mb/s\n", *max_up, *max_down);
  676. return 0;
  677. }
  678. /**
  679. * tb_available_bandwidth() - Available bandwidth for tunneling
  680. * @tb: Domain structure
  681. * @src_port: Source protocol adapter
  682. * @dst_port: Destination protocol adapter
  683. * @available_up: Available bandwidth upstream (Mb/s)
  684. * @available_down: Available bandwidth downstream (Mb/s)
  685. * @include_asym: Include bandwidth if the link is switched from
  686. * symmetric to asymmetric
  687. *
  688. * Calculates maximum available bandwidth for protocol tunneling between
  689. * @src_port and @dst_port at the moment. This is minimum of maximum
  690. * link bandwidth across all links reduced by currently consumed
  691. * bandwidth on that link.
  692. *
  693. * If @include_asym is true then includes also bandwidth that can be
  694. * added when the links are transitioned into asymmetric (but does not
  695. * transition the links).
  696. *
  697. * Return: %0 on success, negative errno otherwise.
  698. */
  699. static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
  700. struct tb_port *dst_port, int *available_up,
  701. int *available_down, bool include_asym)
  702. {
  703. struct tb_port *port;
  704. int ret;
  705. /* Maximum possible bandwidth asymmetric Gen 4 link is 120 Gb/s */
  706. *available_up = *available_down = 120000;
  707. /* Find the minimum available bandwidth over all links */
  708. tb_for_each_port_on_path(src_port, dst_port, port) {
  709. int max_up, max_down, consumed_up, consumed_down;
  710. if (!tb_port_is_null(port))
  711. continue;
  712. ret = tb_maximum_bandwidth(tb, src_port, dst_port, port,
  713. &max_up, &max_down, include_asym);
  714. if (ret)
  715. return ret;
  716. ret = tb_consumed_usb3_pcie_bandwidth(tb, src_port, dst_port,
  717. port, &consumed_up,
  718. &consumed_down);
  719. if (ret)
  720. return ret;
  721. max_up -= consumed_up;
  722. max_down -= consumed_down;
  723. ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, port,
  724. &consumed_up, &consumed_down);
  725. if (ret)
  726. return ret;
  727. max_up -= consumed_up;
  728. max_down -= consumed_down;
  729. if (max_up < *available_up)
  730. *available_up = max_up;
  731. if (max_down < *available_down)
  732. *available_down = max_down;
  733. }
  734. if (*available_up < 0)
  735. *available_up = 0;
  736. if (*available_down < 0)
  737. *available_down = 0;
  738. return 0;
  739. }
  740. static int tb_release_unused_usb3_bandwidth(struct tb *tb,
  741. struct tb_port *src_port,
  742. struct tb_port *dst_port)
  743. {
  744. struct tb_tunnel *tunnel;
  745. tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
  746. return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
  747. }
  748. static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
  749. struct tb_port *dst_port)
  750. {
  751. int ret, available_up, available_down;
  752. struct tb_tunnel *tunnel;
  753. tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
  754. if (!tunnel)
  755. return;
  756. tb_tunnel_dbg(tunnel, "reclaiming unused bandwidth\n");
  757. /*
  758. * Calculate available bandwidth for the first hop USB3 tunnel.
  759. * That determines the whole USB3 bandwidth for this branch.
  760. */
  761. ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
  762. &available_up, &available_down, false);
  763. if (ret) {
  764. tb_tunnel_warn(tunnel, "failed to calculate available bandwidth\n");
  765. return;
  766. }
  767. tb_tunnel_dbg(tunnel, "available bandwidth %d/%d Mb/s\n", available_up,
  768. available_down);
  769. tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
  770. }
  771. static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
  772. {
  773. struct tb_switch *parent = tb_switch_parent(sw);
  774. int ret, available_up, available_down;
  775. struct tb_port *up, *down, *port;
  776. struct tb_cm *tcm = tb_priv(tb);
  777. struct tb_tunnel *tunnel;
  778. if (!tb_acpi_may_tunnel_usb3()) {
  779. tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
  780. return 0;
  781. }
  782. up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
  783. if (!up)
  784. return 0;
  785. if (!sw->link_usb4)
  786. return 0;
  787. /*
  788. * Look up available down port. Since we are chaining it should
  789. * be found right above this switch.
  790. */
  791. port = tb_switch_downstream_port(sw);
  792. down = tb_find_usb3_down(parent, port);
  793. if (!down)
  794. return 0;
  795. if (tb_route(parent)) {
  796. struct tb_port *parent_up;
  797. /*
  798. * Check first that the parent switch has its upstream USB3
  799. * port enabled. Otherwise the chain is not complete and
  800. * there is no point setting up a new tunnel.
  801. */
  802. parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
  803. if (!parent_up || !tb_port_is_enabled(parent_up))
  804. return 0;
  805. /* Make all unused bandwidth available for the new tunnel */
  806. ret = tb_release_unused_usb3_bandwidth(tb, down, up);
  807. if (ret)
  808. return ret;
  809. }
  810. ret = tb_available_bandwidth(tb, down, up, &available_up, &available_down,
  811. false);
  812. if (ret)
  813. goto err_reclaim;
  814. tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
  815. available_up, available_down);
  816. /*
  817. * If the available bandwidth is less than 1.5 Gb/s notify
  818. * userspace that the connected isochronous device may not work
  819. * properly.
  820. */
  821. if (available_up < 1500 || available_down < 1500)
  822. tb_tunnel_event(tb, TB_TUNNEL_LOW_BANDWIDTH, TB_TUNNEL_USB3,
  823. down, up);
  824. tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
  825. available_down);
  826. if (!tunnel) {
  827. ret = -ENOMEM;
  828. goto err_reclaim;
  829. }
  830. if (tb_tunnel_activate(tunnel)) {
  831. tb_port_info(up,
  832. "USB3 tunnel activation failed, aborting\n");
  833. ret = -EIO;
  834. goto err_free;
  835. }
  836. list_add_tail(&tunnel->list, &tcm->tunnel_list);
  837. if (tb_route(parent))
  838. tb_reclaim_usb3_bandwidth(tb, down, up);
  839. return 0;
  840. err_free:
  841. tb_tunnel_put(tunnel);
  842. err_reclaim:
  843. if (tb_route(parent))
  844. tb_reclaim_usb3_bandwidth(tb, down, up);
  845. return ret;
  846. }
  847. static int tb_create_usb3_tunnels(struct tb_switch *sw)
  848. {
  849. struct tb_port *port;
  850. int ret;
  851. if (!tb_acpi_may_tunnel_usb3())
  852. return 0;
  853. if (tb_route(sw)) {
  854. ret = tb_tunnel_usb3(sw->tb, sw);
  855. if (ret)
  856. return ret;
  857. }
  858. tb_switch_for_each_port(sw, port) {
  859. if (!tb_port_has_remote(port))
  860. continue;
  861. ret = tb_create_usb3_tunnels(port->remote->sw);
  862. if (ret)
  863. return ret;
  864. }
  865. return 0;
  866. }
  867. /**
  868. * tb_configure_asym() - Transition links to asymmetric if needed
  869. * @tb: Domain structure
  870. * @src_port: Source adapter to start the transition
  871. * @dst_port: Destination adapter
  872. * @requested_up: Additional bandwidth (Mb/s) required upstream
  873. * @requested_down: Additional bandwidth (Mb/s) required downstream
  874. *
  875. * Transition links between @src_port and @dst_port into asymmetric, with
  876. * three lanes in the direction from @src_port towards @dst_port and one lane
  877. * in the opposite direction, if the bandwidth requirements
  878. * (requested + currently consumed) on that link exceed @asym_threshold.
  879. *
  880. * Must be called with available >= requested over all links.
  881. *
  882. * Return: %0 on success, negative errno otherwise.
  883. */
  884. static int tb_configure_asym(struct tb *tb, struct tb_port *src_port,
  885. struct tb_port *dst_port, int requested_up,
  886. int requested_down)
  887. {
  888. bool clx = false, clx_disabled = false, downstream;
  889. struct tb_switch *sw;
  890. struct tb_port *up;
  891. int ret = 0;
  892. if (!asym_threshold)
  893. return 0;
  894. downstream = tb_port_path_direction_downstream(src_port, dst_port);
  895. /* Pick up router deepest in the hierarchy */
  896. if (downstream)
  897. sw = dst_port->sw;
  898. else
  899. sw = src_port->sw;
  900. tb_for_each_upstream_port_on_path(src_port, dst_port, up) {
  901. struct tb_port *down = tb_switch_downstream_port(up->sw);
  902. enum tb_link_width width_up, width_down;
  903. int consumed_up, consumed_down;
  904. ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, up,
  905. &consumed_up, &consumed_down);
  906. if (ret)
  907. break;
  908. if (downstream) {
  909. /*
  910. * Downstream so make sure upstream is within the 36G
  911. * (40G - guard band 10%), and the requested is above
  912. * what the threshold is.
  913. */
  914. if (consumed_up + requested_up >= TB_ASYM_MIN) {
  915. ret = -ENOBUFS;
  916. break;
  917. }
  918. /* Does consumed + requested exceed the threshold */
  919. if (consumed_down + requested_down < asym_threshold)
  920. continue;
  921. width_up = TB_LINK_WIDTH_ASYM_RX;
  922. width_down = TB_LINK_WIDTH_ASYM_TX;
  923. } else {
  924. /* Upstream, the opposite of above */
  925. if (consumed_down + requested_down >= TB_ASYM_MIN) {
  926. ret = -ENOBUFS;
  927. break;
  928. }
  929. if (consumed_up + requested_up < asym_threshold)
  930. continue;
  931. width_up = TB_LINK_WIDTH_ASYM_TX;
  932. width_down = TB_LINK_WIDTH_ASYM_RX;
  933. }
  934. if (up->sw->link_width == width_up)
  935. continue;
  936. if (!tb_port_width_supported(up, width_up) ||
  937. !tb_port_width_supported(down, width_down))
  938. continue;
  939. /*
  940. * Disable CL states before doing any transitions. We
  941. * delayed it until now that we know there is a real
  942. * transition taking place.
  943. */
  944. if (!clx_disabled) {
  945. clx = tb_disable_clx(sw);
  946. clx_disabled = true;
  947. }
  948. tb_sw_dbg(up->sw, "configuring asymmetric link\n");
  949. /*
  950. * Here requested + consumed > threshold so we need to
  951. * transition the link into asymmetric now.
  952. */
  953. ret = tb_switch_set_link_width(up->sw, width_up);
  954. if (ret) {
  955. tb_sw_warn(up->sw, "failed to set link width\n");
  956. break;
  957. }
  958. }
  959. /* Re-enable CL states if they were previosly enabled */
  960. if (clx)
  961. tb_enable_clx(sw);
  962. return ret;
  963. }
  964. /**
  965. * tb_configure_sym() - Transition links to symmetric if possible
  966. * @tb: Domain structure
  967. * @src_port: Source adapter to start the transition
  968. * @dst_port: Destination adapter
  969. * @keep_asym: Keep asymmetric link if preferred
  970. *
  971. * Goes over each link from @src_port to @dst_port and tries to
  972. * transition the link to symmetric if the currently consumed bandwidth
  973. * allows and link asymmetric preference is ignored (if @keep_asym is %false).
  974. *
  975. * Return: %0 on success, negative errno otherwise.
  976. */
  977. static int tb_configure_sym(struct tb *tb, struct tb_port *src_port,
  978. struct tb_port *dst_port, bool keep_asym)
  979. {
  980. bool clx = false, clx_disabled = false, downstream;
  981. struct tb_switch *sw;
  982. struct tb_port *up;
  983. int ret = 0;
  984. if (!asym_threshold)
  985. return 0;
  986. downstream = tb_port_path_direction_downstream(src_port, dst_port);
  987. /* Pick up router deepest in the hierarchy */
  988. if (downstream)
  989. sw = dst_port->sw;
  990. else
  991. sw = src_port->sw;
  992. tb_for_each_upstream_port_on_path(src_port, dst_port, up) {
  993. int consumed_up, consumed_down;
  994. /* Already symmetric */
  995. if (up->sw->link_width <= TB_LINK_WIDTH_DUAL)
  996. continue;
  997. /* Unplugged, no need to switch */
  998. if (up->sw->is_unplugged)
  999. continue;
  1000. ret = tb_consumed_dp_bandwidth(tb, src_port, dst_port, up,
  1001. &consumed_up, &consumed_down);
  1002. if (ret)
  1003. break;
  1004. if (downstream) {
  1005. /*
  1006. * Downstream so we want the consumed_down < threshold.
  1007. * Upstream traffic should be less than 36G (40G
  1008. * guard band 10%) as the link was configured asymmetric
  1009. * already.
  1010. */
  1011. if (consumed_down >= asym_threshold)
  1012. continue;
  1013. } else {
  1014. if (consumed_up >= asym_threshold)
  1015. continue;
  1016. }
  1017. if (up->sw->link_width == TB_LINK_WIDTH_DUAL)
  1018. continue;
  1019. /*
  1020. * Here consumed < threshold so we can transition the
  1021. * link to symmetric.
  1022. *
  1023. * However, if the router prefers asymmetric link we
  1024. * honor that (unless @keep_asym is %false).
  1025. */
  1026. if (keep_asym &&
  1027. up->sw->preferred_link_width > TB_LINK_WIDTH_DUAL) {
  1028. tb_sw_dbg(up->sw, "keeping preferred asymmetric link\n");
  1029. continue;
  1030. }
  1031. /* Disable CL states before doing any transitions */
  1032. if (!clx_disabled) {
  1033. clx = tb_disable_clx(sw);
  1034. clx_disabled = true;
  1035. }
  1036. tb_sw_dbg(up->sw, "configuring symmetric link\n");
  1037. ret = tb_switch_set_link_width(up->sw, TB_LINK_WIDTH_DUAL);
  1038. if (ret) {
  1039. tb_sw_warn(up->sw, "failed to set link width\n");
  1040. break;
  1041. }
  1042. }
  1043. /* Re-enable CL states if they were previosly enabled */
  1044. if (clx)
  1045. tb_enable_clx(sw);
  1046. return ret;
  1047. }
  1048. static void tb_configure_link(struct tb_port *down, struct tb_port *up,
  1049. struct tb_switch *sw)
  1050. {
  1051. struct tb *tb = sw->tb;
  1052. /* Link the routers using both links if available */
  1053. down->remote = up;
  1054. up->remote = down;
  1055. if (down->dual_link_port && up->dual_link_port) {
  1056. down->dual_link_port->remote = up->dual_link_port;
  1057. up->dual_link_port->remote = down->dual_link_port;
  1058. }
  1059. /*
  1060. * Enable lane bonding if the link is currently two single lane
  1061. * links.
  1062. */
  1063. if (sw->link_width < TB_LINK_WIDTH_DUAL)
  1064. tb_switch_set_link_width(sw, TB_LINK_WIDTH_DUAL);
  1065. /*
  1066. * Device router that comes up as symmetric link is
  1067. * connected deeper in the hierarchy, we transition the links
  1068. * above into symmetric if bandwidth allows.
  1069. */
  1070. if (tb_switch_depth(sw) > 1 &&
  1071. tb_port_get_link_generation(up) >= 4 &&
  1072. up->sw->link_width == TB_LINK_WIDTH_DUAL) {
  1073. struct tb_port *host_port;
  1074. host_port = tb_port_at(tb_route(sw), tb->root_switch);
  1075. tb_configure_sym(tb, host_port, up, false);
  1076. }
  1077. /* Set the link configured */
  1078. tb_switch_configure_link(sw);
  1079. }
  1080. /*
  1081. * tb_scan_switch() - scan for and initialize downstream switches
  1082. */
  1083. static void tb_scan_switch(struct tb_switch *sw)
  1084. {
  1085. struct tb_port *port;
  1086. pm_runtime_get_sync(&sw->dev);
  1087. tb_switch_for_each_port(sw, port)
  1088. tb_scan_port(port);
  1089. pm_runtime_mark_last_busy(&sw->dev);
  1090. pm_runtime_put_autosuspend(&sw->dev);
  1091. }
  1092. /*
  1093. * tb_scan_port() - check for and initialize switches below port
  1094. */
  1095. static void tb_scan_port(struct tb_port *port)
  1096. {
  1097. struct tb_cm *tcm = tb_priv(port->sw->tb);
  1098. struct tb_port *upstream_port;
  1099. bool discovery = false;
  1100. struct tb_switch *sw;
  1101. if (tb_is_upstream_port(port))
  1102. return;
  1103. if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
  1104. !tb_dp_port_is_enabled(port)) {
  1105. tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
  1106. tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
  1107. false);
  1108. return;
  1109. }
  1110. if (port->config.type != TB_TYPE_PORT)
  1111. return;
  1112. if (port->dual_link_port && port->link_nr)
  1113. return; /*
  1114. * Downstream switch is reachable through two ports.
  1115. * Only scan on the primary port (link_nr == 0).
  1116. */
  1117. if (port->usb4)
  1118. pm_runtime_get_sync(&port->usb4->dev);
  1119. if (tb_wait_for_port(port, false) <= 0)
  1120. goto out_rpm_put;
  1121. if (port->remote) {
  1122. tb_port_dbg(port, "port already has a remote\n");
  1123. goto out_rpm_put;
  1124. }
  1125. sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
  1126. tb_downstream_route(port));
  1127. if (IS_ERR(sw)) {
  1128. /*
  1129. * Make the downstream retimers available even if there
  1130. * is no router connected.
  1131. */
  1132. tb_retimer_scan(port, true);
  1133. /*
  1134. * If there is an error accessing the connected switch
  1135. * it may be connected to another domain. Also we allow
  1136. * the other domain to be connected to a max depth switch.
  1137. */
  1138. if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
  1139. tb_scan_xdomain(port);
  1140. goto out_rpm_put;
  1141. }
  1142. if (tb_switch_configure(sw)) {
  1143. tb_switch_put(sw);
  1144. goto out_rpm_put;
  1145. }
  1146. /*
  1147. * If there was previously another domain connected remove it
  1148. * first.
  1149. */
  1150. if (port->xdomain) {
  1151. tb_xdomain_remove(port->xdomain);
  1152. tb_port_unconfigure_xdomain(port);
  1153. port->xdomain = NULL;
  1154. }
  1155. /*
  1156. * Do not send uevents until we have discovered all existing
  1157. * tunnels and know which switches were authorized already by
  1158. * the boot firmware.
  1159. */
  1160. if (!tcm->hotplug_active) {
  1161. dev_set_uevent_suppress(&sw->dev, true);
  1162. discovery = true;
  1163. }
  1164. /*
  1165. * At the moment Thunderbolt 2 and beyond (devices with LC) we
  1166. * can support runtime PM.
  1167. */
  1168. sw->rpm = sw->generation > 1;
  1169. if (tb_switch_add(sw)) {
  1170. tb_switch_put(sw);
  1171. goto out_rpm_put;
  1172. }
  1173. upstream_port = tb_upstream_port(sw);
  1174. tb_configure_link(port, upstream_port, sw);
  1175. /*
  1176. * Scan for downstream retimers. We only scan them after the
  1177. * router has been enumerated to avoid issues with certain
  1178. * Pluggable devices that expect the host to enumerate them
  1179. * within certain timeout.
  1180. */
  1181. tb_retimer_scan(port, true);
  1182. /*
  1183. * CL0s and CL1 are enabled and supported together.
  1184. * Silently ignore CLx enabling in case CLx is not supported.
  1185. */
  1186. if (discovery)
  1187. tb_sw_dbg(sw, "discovery, not touching CL states\n");
  1188. else if (tb_enable_clx(sw))
  1189. tb_sw_warn(sw, "failed to enable CL states\n");
  1190. if (tb_enable_tmu(sw))
  1191. tb_sw_warn(sw, "failed to enable TMU\n");
  1192. /*
  1193. * Configuration valid needs to be set after the TMU has been
  1194. * enabled for the upstream port of the router so we do it here.
  1195. */
  1196. tb_switch_configuration_valid(sw);
  1197. /* Scan upstream retimers */
  1198. tb_retimer_scan(upstream_port, true);
  1199. /*
  1200. * Create USB 3.x tunnels only when the switch is plugged to the
  1201. * domain. This is because we scan the domain also during discovery
  1202. * and want to discover existing USB 3.x tunnels before we create
  1203. * any new.
  1204. */
  1205. if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
  1206. tb_sw_warn(sw, "USB3 tunnel creation failed\n");
  1207. tb_add_dp_resources(sw);
  1208. tb_scan_switch(sw);
  1209. out_rpm_put:
  1210. if (port->usb4) {
  1211. pm_runtime_mark_last_busy(&port->usb4->dev);
  1212. pm_runtime_put_autosuspend(&port->usb4->dev);
  1213. }
  1214. }
  1215. static void
  1216. tb_recalc_estimated_bandwidth_for_group(struct tb_bandwidth_group *group)
  1217. {
  1218. struct tb_tunnel *first_tunnel;
  1219. struct tb *tb = group->tb;
  1220. struct tb_port *in;
  1221. int ret;
  1222. tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n",
  1223. group->index);
  1224. first_tunnel = NULL;
  1225. list_for_each_entry(in, &group->ports, group_list) {
  1226. int estimated_bw, estimated_up, estimated_down;
  1227. struct tb_tunnel *tunnel;
  1228. struct tb_port *out;
  1229. if (!usb4_dp_port_bandwidth_mode_enabled(in))
  1230. continue;
  1231. tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
  1232. if (WARN_ON(!tunnel))
  1233. break;
  1234. if (!first_tunnel) {
  1235. /*
  1236. * Since USB3 bandwidth is shared by all DP
  1237. * tunnels under the host router USB4 port, even
  1238. * if they do not begin from the host router, we
  1239. * can release USB3 bandwidth just once and not
  1240. * for each tunnel separately.
  1241. */
  1242. first_tunnel = tunnel;
  1243. ret = tb_release_unused_usb3_bandwidth(tb,
  1244. first_tunnel->src_port, first_tunnel->dst_port);
  1245. if (ret) {
  1246. tb_tunnel_warn(tunnel,
  1247. "failed to release unused bandwidth\n");
  1248. break;
  1249. }
  1250. }
  1251. out = tunnel->dst_port;
  1252. ret = tb_available_bandwidth(tb, in, out, &estimated_up,
  1253. &estimated_down, true);
  1254. if (ret) {
  1255. tb_tunnel_warn(tunnel,
  1256. "failed to re-calculate estimated bandwidth\n");
  1257. break;
  1258. }
  1259. /*
  1260. * Estimated bandwidth includes:
  1261. * - already allocated bandwidth for the DP tunnel
  1262. * - available bandwidth along the path
  1263. * - bandwidth allocated for USB 3.x but not used.
  1264. */
  1265. if (tb_tunnel_direction_downstream(tunnel))
  1266. estimated_bw = estimated_down;
  1267. else
  1268. estimated_bw = estimated_up;
  1269. /*
  1270. * If there is reserved bandwidth for the group that is
  1271. * not yet released we report that too.
  1272. */
  1273. tb_tunnel_dbg(tunnel,
  1274. "re-calculated estimated bandwidth %u (+ %u reserved) = %u Mb/s\n",
  1275. estimated_bw, group->reserved,
  1276. estimated_bw + group->reserved);
  1277. if (usb4_dp_port_set_estimated_bandwidth(in,
  1278. estimated_bw + group->reserved))
  1279. tb_tunnel_warn(tunnel,
  1280. "failed to update estimated bandwidth\n");
  1281. }
  1282. if (first_tunnel)
  1283. tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port,
  1284. first_tunnel->dst_port);
  1285. tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index);
  1286. }
  1287. static void tb_recalc_estimated_bandwidth(struct tb *tb)
  1288. {
  1289. struct tb_cm *tcm = tb_priv(tb);
  1290. int i;
  1291. tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n");
  1292. for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
  1293. struct tb_bandwidth_group *group = &tcm->groups[i];
  1294. if (!list_empty(&group->ports))
  1295. tb_recalc_estimated_bandwidth_for_group(group);
  1296. }
  1297. tb_dbg(tb, "bandwidth re-calculation done\n");
  1298. }
  1299. static bool __release_group_bandwidth(struct tb_bandwidth_group *group)
  1300. {
  1301. if (group->reserved) {
  1302. tb_dbg(group->tb, "group %d released total %d Mb/s\n", group->index,
  1303. group->reserved);
  1304. group->reserved = 0;
  1305. return true;
  1306. }
  1307. return false;
  1308. }
  1309. static void __configure_group_sym(struct tb_bandwidth_group *group)
  1310. {
  1311. struct tb_tunnel *tunnel;
  1312. struct tb_port *in;
  1313. if (list_empty(&group->ports))
  1314. return;
  1315. /*
  1316. * All the tunnels in the group go through the same USB4 links
  1317. * so we find the first one here and pass the IN and OUT
  1318. * adapters to tb_configure_sym() which now transitions the
  1319. * links back to symmetric if bandwidth requirement < asym_threshold.
  1320. *
  1321. * We do this here to avoid unnecessary transitions (for example
  1322. * if the graphics released bandwidth for other tunnel in the
  1323. * same group).
  1324. */
  1325. in = list_first_entry(&group->ports, struct tb_port, group_list);
  1326. tunnel = tb_find_tunnel(group->tb, TB_TUNNEL_DP, in, NULL);
  1327. if (tunnel)
  1328. tb_configure_sym(group->tb, in, tunnel->dst_port, true);
  1329. }
  1330. static void tb_bandwidth_group_release_work(struct work_struct *work)
  1331. {
  1332. struct tb_bandwidth_group *group =
  1333. container_of(work, typeof(*group), release_work.work);
  1334. struct tb *tb = group->tb;
  1335. mutex_lock(&tb->lock);
  1336. if (__release_group_bandwidth(group))
  1337. tb_recalc_estimated_bandwidth(tb);
  1338. __configure_group_sym(group);
  1339. mutex_unlock(&tb->lock);
  1340. }
  1341. static void tb_init_bandwidth_groups(struct tb_cm *tcm)
  1342. {
  1343. int i;
  1344. for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
  1345. struct tb_bandwidth_group *group = &tcm->groups[i];
  1346. group->tb = tcm_to_tb(tcm);
  1347. group->index = i + 1;
  1348. INIT_LIST_HEAD(&group->ports);
  1349. INIT_DELAYED_WORK(&group->release_work,
  1350. tb_bandwidth_group_release_work);
  1351. }
  1352. }
  1353. static void tb_bandwidth_group_attach_port(struct tb_bandwidth_group *group,
  1354. struct tb_port *in)
  1355. {
  1356. if (!group || WARN_ON(in->group))
  1357. return;
  1358. in->group = group;
  1359. list_add_tail(&in->group_list, &group->ports);
  1360. tb_port_dbg(in, "attached to bandwidth group %d\n", group->index);
  1361. }
  1362. static struct tb_bandwidth_group *tb_find_free_bandwidth_group(struct tb_cm *tcm)
  1363. {
  1364. int i;
  1365. for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
  1366. struct tb_bandwidth_group *group = &tcm->groups[i];
  1367. if (list_empty(&group->ports))
  1368. return group;
  1369. }
  1370. return NULL;
  1371. }
  1372. static struct tb_bandwidth_group *
  1373. tb_attach_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
  1374. struct tb_port *out)
  1375. {
  1376. struct tb_bandwidth_group *group;
  1377. struct tb_tunnel *tunnel;
  1378. /*
  1379. * Find all DP tunnels that go through all the same USB4 links
  1380. * as this one. Because we always setup tunnels the same way we
  1381. * can just check for the routers at both ends of the tunnels
  1382. * and if they are the same we have a match.
  1383. */
  1384. list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
  1385. if (!tb_tunnel_is_dp(tunnel))
  1386. continue;
  1387. if (tunnel->src_port->sw == in->sw &&
  1388. tunnel->dst_port->sw == out->sw) {
  1389. group = tunnel->src_port->group;
  1390. if (group) {
  1391. tb_bandwidth_group_attach_port(group, in);
  1392. return group;
  1393. }
  1394. }
  1395. }
  1396. /* Pick up next available group then */
  1397. group = tb_find_free_bandwidth_group(tcm);
  1398. if (group)
  1399. tb_bandwidth_group_attach_port(group, in);
  1400. else
  1401. tb_port_warn(in, "no available bandwidth groups\n");
  1402. return group;
  1403. }
  1404. static void tb_discover_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
  1405. struct tb_port *out)
  1406. {
  1407. if (usb4_dp_port_bandwidth_mode_enabled(in)) {
  1408. int index, i;
  1409. index = usb4_dp_port_group_id(in);
  1410. for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
  1411. if (tcm->groups[i].index == index) {
  1412. tb_bandwidth_group_attach_port(&tcm->groups[i], in);
  1413. return;
  1414. }
  1415. }
  1416. }
  1417. tb_attach_bandwidth_group(tcm, in, out);
  1418. }
  1419. static void tb_detach_bandwidth_group(struct tb_port *in)
  1420. {
  1421. struct tb_bandwidth_group *group = in->group;
  1422. if (group) {
  1423. in->group = NULL;
  1424. list_del_init(&in->group_list);
  1425. tb_port_dbg(in, "detached from bandwidth group %d\n", group->index);
  1426. /* No more tunnels so release the reserved bandwidth if any */
  1427. if (list_empty(&group->ports)) {
  1428. cancel_delayed_work(&group->release_work);
  1429. __release_group_bandwidth(group);
  1430. }
  1431. }
  1432. }
  1433. static void tb_discover_tunnels(struct tb *tb)
  1434. {
  1435. struct tb_cm *tcm = tb_priv(tb);
  1436. struct tb_tunnel *tunnel;
  1437. tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
  1438. list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
  1439. if (tb_tunnel_is_pci(tunnel)) {
  1440. struct tb_switch *parent = tunnel->dst_port->sw;
  1441. while (parent != tunnel->src_port->sw) {
  1442. parent->boot = true;
  1443. parent = tb_switch_parent(parent);
  1444. }
  1445. } else if (tb_tunnel_is_dp(tunnel)) {
  1446. struct tb_port *in = tunnel->src_port;
  1447. struct tb_port *out = tunnel->dst_port;
  1448. /* Keep the domain from powering down */
  1449. pm_runtime_get_sync(&in->sw->dev);
  1450. pm_runtime_get_sync(&out->sw->dev);
  1451. tb_discover_bandwidth_group(tcm, in, out);
  1452. }
  1453. }
  1454. }
  1455. static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
  1456. {
  1457. struct tb_port *src_port, *dst_port;
  1458. struct tb *tb;
  1459. if (!tunnel)
  1460. return;
  1461. tb_tunnel_deactivate(tunnel);
  1462. list_del(&tunnel->list);
  1463. tb = tunnel->tb;
  1464. src_port = tunnel->src_port;
  1465. dst_port = tunnel->dst_port;
  1466. switch (tunnel->type) {
  1467. case TB_TUNNEL_DP:
  1468. tb_detach_bandwidth_group(src_port);
  1469. /*
  1470. * In case of DP tunnel make sure the DP IN resource is
  1471. * deallocated properly.
  1472. */
  1473. tb_switch_dealloc_dp_resource(src_port->sw, src_port);
  1474. /*
  1475. * If bandwidth on a link is < asym_threshold
  1476. * transition the link to symmetric.
  1477. */
  1478. tb_configure_sym(tb, src_port, dst_port, true);
  1479. /* Now we can allow the domain to runtime suspend again */
  1480. pm_runtime_mark_last_busy(&dst_port->sw->dev);
  1481. pm_runtime_put_autosuspend(&dst_port->sw->dev);
  1482. pm_runtime_mark_last_busy(&src_port->sw->dev);
  1483. pm_runtime_put_autosuspend(&src_port->sw->dev);
  1484. fallthrough;
  1485. case TB_TUNNEL_USB3:
  1486. tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
  1487. break;
  1488. default:
  1489. /*
  1490. * PCIe and DMA tunnels do not consume guaranteed
  1491. * bandwidth.
  1492. */
  1493. break;
  1494. }
  1495. tb_tunnel_put(tunnel);
  1496. }
  1497. /*
  1498. * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
  1499. */
  1500. static void tb_free_invalid_tunnels(struct tb *tb)
  1501. {
  1502. struct tb_cm *tcm = tb_priv(tb);
  1503. struct tb_tunnel *tunnel;
  1504. struct tb_tunnel *n;
  1505. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
  1506. if (tb_tunnel_is_invalid(tunnel))
  1507. tb_deactivate_and_free_tunnel(tunnel);
  1508. }
  1509. }
  1510. /*
  1511. * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
  1512. */
  1513. static void tb_free_unplugged_children(struct tb_switch *sw)
  1514. {
  1515. struct tb_port *port;
  1516. tb_switch_for_each_port(sw, port) {
  1517. if (!tb_port_has_remote(port))
  1518. continue;
  1519. if (port->remote->sw->is_unplugged) {
  1520. tb_retimer_remove_all(port);
  1521. tb_remove_dp_resources(port->remote->sw);
  1522. tb_switch_unconfigure_link(port->remote->sw);
  1523. tb_switch_set_link_width(port->remote->sw,
  1524. TB_LINK_WIDTH_SINGLE);
  1525. tb_switch_remove(port->remote->sw);
  1526. port->remote = NULL;
  1527. if (port->dual_link_port)
  1528. port->dual_link_port->remote = NULL;
  1529. } else {
  1530. tb_free_unplugged_children(port->remote->sw);
  1531. }
  1532. }
  1533. }
  1534. static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
  1535. const struct tb_port *port)
  1536. {
  1537. struct tb_port *down = NULL;
  1538. /*
  1539. * To keep plugging devices consistently in the same PCIe
  1540. * hierarchy, do mapping here for switch downstream PCIe ports.
  1541. */
  1542. if (tb_switch_is_usb4(sw)) {
  1543. down = usb4_switch_map_pcie_down(sw, port);
  1544. } else if (!tb_route(sw)) {
  1545. int phy_port = tb_phy_port_from_link(port->port);
  1546. int index;
  1547. /*
  1548. * Hard-coded Thunderbolt port to PCIe down port mapping
  1549. * per controller.
  1550. */
  1551. if (tb_switch_is_cactus_ridge(sw) ||
  1552. tb_switch_is_alpine_ridge(sw))
  1553. index = !phy_port ? 6 : 7;
  1554. else if (tb_switch_is_falcon_ridge(sw))
  1555. index = !phy_port ? 6 : 8;
  1556. else if (tb_switch_is_titan_ridge(sw))
  1557. index = !phy_port ? 8 : 9;
  1558. else
  1559. goto out;
  1560. /* Validate the hard-coding */
  1561. if (WARN_ON(index > sw->config.max_port_number))
  1562. goto out;
  1563. down = &sw->ports[index];
  1564. }
  1565. if (down) {
  1566. if (WARN_ON(!tb_port_is_pcie_down(down)))
  1567. goto out;
  1568. if (tb_pci_port_is_enabled(down))
  1569. goto out;
  1570. return down;
  1571. }
  1572. out:
  1573. return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
  1574. }
  1575. static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
  1576. {
  1577. struct tb_port *host_port, *port;
  1578. struct tb_cm *tcm = tb_priv(tb);
  1579. host_port = tb_route(in->sw) ?
  1580. tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
  1581. list_for_each_entry(port, &tcm->dp_resources, list) {
  1582. if (!tb_port_is_dpout(port))
  1583. continue;
  1584. if (tb_port_is_enabled(port)) {
  1585. tb_port_dbg(port, "DP OUT in use\n");
  1586. continue;
  1587. }
  1588. /* Needs to be on different routers */
  1589. if (in->sw == port->sw) {
  1590. tb_port_dbg(port, "skipping DP OUT on same router\n");
  1591. continue;
  1592. }
  1593. tb_port_dbg(port, "DP OUT available\n");
  1594. /*
  1595. * Keep the DP tunnel under the topology starting from
  1596. * the same host router downstream port.
  1597. */
  1598. if (host_port && tb_route(port->sw)) {
  1599. struct tb_port *p;
  1600. p = tb_port_at(tb_route(port->sw), tb->root_switch);
  1601. if (p != host_port)
  1602. continue;
  1603. }
  1604. return port;
  1605. }
  1606. return NULL;
  1607. }
  1608. static void tb_dp_tunnel_active(struct tb_tunnel *tunnel, void *data)
  1609. {
  1610. struct tb_port *in = tunnel->src_port;
  1611. struct tb_port *out = tunnel->dst_port;
  1612. struct tb *tb = data;
  1613. mutex_lock(&tb->lock);
  1614. if (tb_tunnel_is_active(tunnel)) {
  1615. int consumed_up, consumed_down, ret;
  1616. tb_tunnel_dbg(tunnel, "DPRX capabilities read completed\n");
  1617. /* If fail reading tunnel's consumed bandwidth, tear it down */
  1618. ret = tb_tunnel_consumed_bandwidth(tunnel, &consumed_up,
  1619. &consumed_down);
  1620. if (ret) {
  1621. tb_tunnel_warn(tunnel,
  1622. "failed to read consumed bandwidth, tearing down\n");
  1623. tb_deactivate_and_free_tunnel(tunnel);
  1624. } else {
  1625. tb_reclaim_usb3_bandwidth(tb, in, out);
  1626. /*
  1627. * Transition the links to asymmetric if the
  1628. * consumption exceeds the threshold.
  1629. */
  1630. tb_configure_asym(tb, in, out, consumed_up,
  1631. consumed_down);
  1632. /*
  1633. * Update the domain with the new bandwidth
  1634. * estimation.
  1635. */
  1636. tb_recalc_estimated_bandwidth(tb);
  1637. /*
  1638. * In case DP tunnel exists, change host
  1639. * router's 1st children TMU mode to HiFi for
  1640. * CL0s to work.
  1641. */
  1642. tb_increase_tmu_accuracy(tunnel);
  1643. }
  1644. } else {
  1645. struct tb_port *in = tunnel->src_port;
  1646. /*
  1647. * This tunnel failed to establish. This means DPRX
  1648. * negotiation most likely did not complete which
  1649. * happens either because there is no graphics driver
  1650. * loaded or not all DP cables where connected to the
  1651. * discrete router.
  1652. *
  1653. * In both cases we remove the DP IN adapter from the
  1654. * available resources as it is not usable. This will
  1655. * also tear down the tunnel and try to re-use the
  1656. * released DP OUT.
  1657. *
  1658. * It will be added back only if there is hotplug for
  1659. * the DP IN again.
  1660. */
  1661. tb_tunnel_warn(tunnel, "not active, tearing down\n");
  1662. tb_dp_resource_unavailable(tb, in, "DPRX negotiation failed");
  1663. }
  1664. mutex_unlock(&tb->lock);
  1665. tb_domain_put(tb);
  1666. }
  1667. static void tb_tunnel_one_dp(struct tb *tb, struct tb_port *in,
  1668. struct tb_port *out)
  1669. {
  1670. int available_up, available_down, ret, link_nr;
  1671. struct tb_cm *tcm = tb_priv(tb);
  1672. struct tb_tunnel *tunnel;
  1673. /*
  1674. * This is only applicable to links that are not bonded (so
  1675. * when Thunderbolt 1 hardware is involved somewhere in the
  1676. * topology). For these try to share the DP bandwidth between
  1677. * the two lanes.
  1678. */
  1679. link_nr = 1;
  1680. list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
  1681. if (tb_tunnel_is_dp(tunnel)) {
  1682. link_nr = 0;
  1683. break;
  1684. }
  1685. }
  1686. /*
  1687. * DP stream needs the domain to be active so runtime resume
  1688. * both ends of the tunnel.
  1689. *
  1690. * This should bring the routers in the middle active as well
  1691. * and keeps the domain from runtime suspending while the DP
  1692. * tunnel is active.
  1693. */
  1694. pm_runtime_get_sync(&in->sw->dev);
  1695. pm_runtime_get_sync(&out->sw->dev);
  1696. if (tb_switch_alloc_dp_resource(in->sw, in)) {
  1697. tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
  1698. goto err_rpm_put;
  1699. }
  1700. if (!tb_attach_bandwidth_group(tcm, in, out))
  1701. goto err_dealloc_dp;
  1702. /* Make all unused USB3 bandwidth available for the new DP tunnel */
  1703. ret = tb_release_unused_usb3_bandwidth(tb, in, out);
  1704. if (ret) {
  1705. tb_warn(tb, "failed to release unused bandwidth\n");
  1706. goto err_detach_group;
  1707. }
  1708. ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down,
  1709. true);
  1710. if (ret) {
  1711. tb_tunnel_event(tb, TB_TUNNEL_NO_BANDWIDTH, TB_TUNNEL_DP, in, out);
  1712. goto err_reclaim_usb;
  1713. }
  1714. tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
  1715. available_up, available_down);
  1716. tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
  1717. available_down, tb_dp_tunnel_active,
  1718. tb_domain_get(tb));
  1719. if (!tunnel) {
  1720. tb_port_dbg(out, "could not allocate DP tunnel\n");
  1721. goto err_reclaim_usb;
  1722. }
  1723. list_add_tail(&tunnel->list, &tcm->tunnel_list);
  1724. ret = tb_tunnel_activate(tunnel);
  1725. if (ret && ret != -EINPROGRESS) {
  1726. tb_port_info(out, "DP tunnel activation failed, aborting\n");
  1727. list_del(&tunnel->list);
  1728. goto err_free;
  1729. }
  1730. return;
  1731. err_free:
  1732. tb_tunnel_put(tunnel);
  1733. err_reclaim_usb:
  1734. tb_reclaim_usb3_bandwidth(tb, in, out);
  1735. tb_domain_put(tb);
  1736. err_detach_group:
  1737. tb_detach_bandwidth_group(in);
  1738. err_dealloc_dp:
  1739. tb_switch_dealloc_dp_resource(in->sw, in);
  1740. err_rpm_put:
  1741. pm_runtime_mark_last_busy(&out->sw->dev);
  1742. pm_runtime_put_autosuspend(&out->sw->dev);
  1743. pm_runtime_mark_last_busy(&in->sw->dev);
  1744. pm_runtime_put_autosuspend(&in->sw->dev);
  1745. }
  1746. static void tb_tunnel_dp(struct tb *tb)
  1747. {
  1748. struct tb_cm *tcm = tb_priv(tb);
  1749. struct tb_port *port, *in, *out;
  1750. if (!tb_acpi_may_tunnel_dp()) {
  1751. tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
  1752. return;
  1753. }
  1754. /*
  1755. * Find pair of inactive DP IN and DP OUT adapters and then
  1756. * establish a DP tunnel between them.
  1757. */
  1758. tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
  1759. in = NULL;
  1760. out = NULL;
  1761. list_for_each_entry(port, &tcm->dp_resources, list) {
  1762. if (!tb_port_is_dpin(port))
  1763. continue;
  1764. if (tb_port_is_enabled(port)) {
  1765. tb_port_dbg(port, "DP IN in use\n");
  1766. continue;
  1767. }
  1768. in = port;
  1769. tb_port_dbg(in, "DP IN available\n");
  1770. out = tb_find_dp_out(tb, port);
  1771. if (out)
  1772. tb_tunnel_one_dp(tb, in, out);
  1773. else
  1774. tb_port_dbg(in, "no suitable DP OUT adapter available, not tunneling\n");
  1775. }
  1776. if (!in)
  1777. tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
  1778. }
  1779. static void tb_enter_redrive(struct tb_port *port)
  1780. {
  1781. struct tb_switch *sw = port->sw;
  1782. if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE))
  1783. return;
  1784. /*
  1785. * If we get hot-unplug for the DP IN port of the host router
  1786. * and the DP resource is not available anymore it means there
  1787. * is a monitor connected directly to the Type-C port and we are
  1788. * in "redrive" mode. For this to work we cannot enter RTD3 so
  1789. * we bump up the runtime PM reference count here.
  1790. */
  1791. if (!tb_port_is_dpin(port))
  1792. return;
  1793. if (tb_route(sw))
  1794. return;
  1795. if (!tb_switch_query_dp_resource(sw, port)) {
  1796. port->redrive = true;
  1797. pm_runtime_get(&sw->dev);
  1798. tb_port_dbg(port, "enter redrive mode, keeping powered\n");
  1799. }
  1800. }
  1801. static void tb_exit_redrive(struct tb_port *port)
  1802. {
  1803. struct tb_switch *sw = port->sw;
  1804. if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE))
  1805. return;
  1806. if (!tb_port_is_dpin(port))
  1807. return;
  1808. if (tb_route(sw))
  1809. return;
  1810. if (port->redrive && tb_switch_query_dp_resource(sw, port)) {
  1811. port->redrive = false;
  1812. pm_runtime_put(&sw->dev);
  1813. tb_port_dbg(port, "exit redrive mode\n");
  1814. }
  1815. }
  1816. static void tb_switch_enter_redrive(struct tb_switch *sw)
  1817. {
  1818. struct tb_port *port;
  1819. tb_switch_for_each_port(sw, port)
  1820. tb_enter_redrive(port);
  1821. }
  1822. /*
  1823. * Called during system and runtime suspend to forcefully exit redrive
  1824. * mode without querying whether the resource is available.
  1825. */
  1826. static void tb_switch_exit_redrive(struct tb_switch *sw)
  1827. {
  1828. struct tb_port *port;
  1829. if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE))
  1830. return;
  1831. tb_switch_for_each_port(sw, port) {
  1832. if (!tb_port_is_dpin(port))
  1833. continue;
  1834. if (port->redrive) {
  1835. port->redrive = false;
  1836. pm_runtime_put(&sw->dev);
  1837. tb_port_dbg(port, "exit redrive mode\n");
  1838. }
  1839. }
  1840. }
  1841. static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port,
  1842. const char *reason)
  1843. {
  1844. struct tb_port *in, *out;
  1845. struct tb_tunnel *tunnel;
  1846. if (tb_port_is_dpin(port)) {
  1847. tb_port_dbg(port, "DP IN resource unavailable: %s\n", reason);
  1848. in = port;
  1849. out = NULL;
  1850. } else {
  1851. tb_port_dbg(port, "DP OUT resource unavailable: %s\n", reason);
  1852. in = NULL;
  1853. out = port;
  1854. }
  1855. tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
  1856. if (tunnel)
  1857. tb_deactivate_and_free_tunnel(tunnel);
  1858. else
  1859. tb_enter_redrive(port);
  1860. list_del_init(&port->list);
  1861. /*
  1862. * See if there is another DP OUT port that can be used for
  1863. * to create another tunnel.
  1864. */
  1865. tb_recalc_estimated_bandwidth(tb);
  1866. tb_tunnel_dp(tb);
  1867. }
  1868. static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
  1869. {
  1870. struct tb_cm *tcm = tb_priv(tb);
  1871. struct tb_port *p;
  1872. if (tb_port_is_enabled(port))
  1873. return;
  1874. list_for_each_entry(p, &tcm->dp_resources, list) {
  1875. if (p == port)
  1876. return;
  1877. }
  1878. tb_port_dbg(port, "DP %s resource available after hotplug\n",
  1879. tb_port_is_dpin(port) ? "IN" : "OUT");
  1880. list_add_tail(&port->list, &tcm->dp_resources);
  1881. tb_exit_redrive(port);
  1882. /* Look for suitable DP IN <-> DP OUT pairs now */
  1883. tb_tunnel_dp(tb);
  1884. }
  1885. static void tb_disconnect_and_release_dp(struct tb *tb)
  1886. {
  1887. struct tb_cm *tcm = tb_priv(tb);
  1888. struct tb_tunnel *tunnel, *n;
  1889. /*
  1890. * Tear down all DP tunnels and release their resources. They
  1891. * will be re-established after resume based on plug events.
  1892. */
  1893. list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
  1894. if (tb_tunnel_is_dp(tunnel))
  1895. tb_deactivate_and_free_tunnel(tunnel);
  1896. }
  1897. while (!list_empty(&tcm->dp_resources)) {
  1898. struct tb_port *port;
  1899. port = list_first_entry(&tcm->dp_resources,
  1900. struct tb_port, list);
  1901. list_del_init(&port->list);
  1902. }
  1903. }
  1904. static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
  1905. {
  1906. struct tb_tunnel *tunnel;
  1907. struct tb_port *up;
  1908. up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
  1909. if (WARN_ON(!up))
  1910. return -ENODEV;
  1911. tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
  1912. if (WARN_ON(!tunnel))
  1913. return -ENODEV;
  1914. tb_switch_xhci_disconnect(sw);
  1915. tb_tunnel_deactivate(tunnel);
  1916. list_del(&tunnel->list);
  1917. tb_tunnel_put(tunnel);
  1918. return 0;
  1919. }
  1920. static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
  1921. {
  1922. struct tb_port *up, *down, *port;
  1923. struct tb_cm *tcm = tb_priv(tb);
  1924. struct tb_tunnel *tunnel;
  1925. up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
  1926. if (!up)
  1927. return 0;
  1928. /*
  1929. * Look up available down port. Since we are chaining it should
  1930. * be found right above this switch.
  1931. */
  1932. port = tb_switch_downstream_port(sw);
  1933. down = tb_find_pcie_down(tb_switch_parent(sw), port);
  1934. if (!down)
  1935. return 0;
  1936. tunnel = tb_tunnel_alloc_pci(tb, up, down);
  1937. if (!tunnel)
  1938. return -ENOMEM;
  1939. if (tb_tunnel_activate(tunnel)) {
  1940. tb_port_info(up,
  1941. "PCIe tunnel activation failed, aborting\n");
  1942. tb_tunnel_put(tunnel);
  1943. return -EIO;
  1944. }
  1945. /*
  1946. * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
  1947. * here.
  1948. */
  1949. if (tb_switch_pcie_l1_enable(sw))
  1950. tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
  1951. if (tb_switch_xhci_connect(sw))
  1952. tb_sw_warn(sw, "failed to connect xHCI\n");
  1953. list_add_tail(&tunnel->list, &tcm->tunnel_list);
  1954. return 0;
  1955. }
  1956. static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  1957. int transmit_path, int transmit_ring,
  1958. int receive_path, int receive_ring)
  1959. {
  1960. struct tb_cm *tcm = tb_priv(tb);
  1961. struct tb_port *nhi_port, *dst_port;
  1962. struct tb_tunnel *tunnel;
  1963. struct tb_switch *sw;
  1964. int ret;
  1965. sw = tb_to_switch(xd->dev.parent);
  1966. dst_port = tb_port_at(xd->route, sw);
  1967. nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
  1968. mutex_lock(&tb->lock);
  1969. /*
  1970. * When tunneling DMA paths the link should not enter CL states
  1971. * so disable them now.
  1972. */
  1973. tb_disable_clx(sw);
  1974. tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
  1975. transmit_ring, receive_path, receive_ring);
  1976. if (!tunnel) {
  1977. ret = -ENOMEM;
  1978. goto err_clx;
  1979. }
  1980. if (tb_tunnel_activate(tunnel)) {
  1981. tb_port_info(nhi_port,
  1982. "DMA tunnel activation failed, aborting\n");
  1983. ret = -EIO;
  1984. goto err_free;
  1985. }
  1986. list_add_tail(&tunnel->list, &tcm->tunnel_list);
  1987. mutex_unlock(&tb->lock);
  1988. return 0;
  1989. err_free:
  1990. tb_tunnel_put(tunnel);
  1991. err_clx:
  1992. tb_enable_clx(sw);
  1993. mutex_unlock(&tb->lock);
  1994. return ret;
  1995. }
  1996. static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  1997. int transmit_path, int transmit_ring,
  1998. int receive_path, int receive_ring)
  1999. {
  2000. struct tb_cm *tcm = tb_priv(tb);
  2001. struct tb_port *nhi_port, *dst_port;
  2002. struct tb_tunnel *tunnel, *n;
  2003. struct tb_switch *sw;
  2004. sw = tb_to_switch(xd->dev.parent);
  2005. dst_port = tb_port_at(xd->route, sw);
  2006. nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
  2007. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
  2008. if (!tb_tunnel_is_dma(tunnel))
  2009. continue;
  2010. if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
  2011. continue;
  2012. if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
  2013. receive_path, receive_ring))
  2014. tb_deactivate_and_free_tunnel(tunnel);
  2015. }
  2016. /*
  2017. * Try to re-enable CL states now, it is OK if this fails
  2018. * because we may still have another DMA tunnel active through
  2019. * the same host router USB4 downstream port.
  2020. */
  2021. tb_enable_clx(sw);
  2022. }
  2023. static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  2024. int transmit_path, int transmit_ring,
  2025. int receive_path, int receive_ring)
  2026. {
  2027. if (!xd->is_unplugged) {
  2028. mutex_lock(&tb->lock);
  2029. __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
  2030. transmit_ring, receive_path,
  2031. receive_ring);
  2032. mutex_unlock(&tb->lock);
  2033. }
  2034. return 0;
  2035. }
  2036. /* hotplug handling */
  2037. /*
  2038. * tb_handle_hotplug() - handle hotplug event
  2039. *
  2040. * Executes on tb->wq.
  2041. */
  2042. static void tb_handle_hotplug(struct work_struct *work)
  2043. {
  2044. struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work.work);
  2045. struct tb *tb = ev->tb;
  2046. struct tb_cm *tcm = tb_priv(tb);
  2047. struct tb_switch *sw;
  2048. struct tb_port *port;
  2049. /* Bring the domain back from sleep if it was suspended */
  2050. pm_runtime_get_sync(&tb->dev);
  2051. mutex_lock(&tb->lock);
  2052. if (!tcm->hotplug_active)
  2053. goto out; /* during init, suspend or shutdown */
  2054. sw = tb_switch_find_by_route(tb, ev->route);
  2055. if (!sw) {
  2056. tb_warn(tb,
  2057. "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
  2058. ev->route, ev->port, ev->unplug);
  2059. goto out;
  2060. }
  2061. if (ev->port > sw->config.max_port_number) {
  2062. tb_warn(tb,
  2063. "hotplug event from non existent port %llx:%x (unplug: %d)\n",
  2064. ev->route, ev->port, ev->unplug);
  2065. goto put_sw;
  2066. }
  2067. port = &sw->ports[ev->port];
  2068. if (tb_is_upstream_port(port)) {
  2069. tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
  2070. ev->route, ev->port, ev->unplug);
  2071. goto put_sw;
  2072. }
  2073. pm_runtime_get_sync(&sw->dev);
  2074. if (ev->unplug) {
  2075. tb_retimer_remove_all(port);
  2076. if (tb_port_has_remote(port)) {
  2077. tb_port_dbg(port, "switch unplugged\n");
  2078. tb_sw_set_unplugged(port->remote->sw);
  2079. tb_free_invalid_tunnels(tb);
  2080. tb_remove_dp_resources(port->remote->sw);
  2081. tb_switch_tmu_disable(port->remote->sw);
  2082. tb_switch_unconfigure_link(port->remote->sw);
  2083. tb_switch_set_link_width(port->remote->sw,
  2084. TB_LINK_WIDTH_SINGLE);
  2085. tb_switch_remove(port->remote->sw);
  2086. port->remote = NULL;
  2087. if (port->dual_link_port)
  2088. port->dual_link_port->remote = NULL;
  2089. /* Maybe we can create another DP tunnel */
  2090. tb_recalc_estimated_bandwidth(tb);
  2091. tb_tunnel_dp(tb);
  2092. } else if (port->xdomain) {
  2093. struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
  2094. tb_port_dbg(port, "xdomain unplugged\n");
  2095. /*
  2096. * Service drivers are unbound during
  2097. * tb_xdomain_remove() so setting XDomain as
  2098. * unplugged here prevents deadlock if they call
  2099. * tb_xdomain_disable_paths(). We will tear down
  2100. * all the tunnels below.
  2101. */
  2102. xd->is_unplugged = true;
  2103. tb_xdomain_remove(xd);
  2104. port->xdomain = NULL;
  2105. __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
  2106. tb_xdomain_put(xd);
  2107. tb_port_unconfigure_xdomain(port);
  2108. } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
  2109. tb_dp_resource_unavailable(tb, port, "adapter unplug");
  2110. } else if (!port->port) {
  2111. tb_sw_dbg(sw, "xHCI disconnect request\n");
  2112. tb_switch_xhci_disconnect(sw);
  2113. } else {
  2114. tb_port_dbg(port,
  2115. "got unplug event for disconnected port, ignoring\n");
  2116. }
  2117. } else if (port->remote) {
  2118. tb_port_dbg(port, "got plug event for connected port, ignoring\n");
  2119. } else if (!port->port && sw->authorized) {
  2120. tb_sw_dbg(sw, "xHCI connect request\n");
  2121. tb_switch_xhci_connect(sw);
  2122. } else {
  2123. if (tb_port_is_null(port)) {
  2124. tb_port_dbg(port, "hotplug: scanning\n");
  2125. tb_scan_port(port);
  2126. if (!port->remote)
  2127. tb_port_dbg(port, "hotplug: no switch found\n");
  2128. } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
  2129. tb_dp_resource_available(tb, port);
  2130. }
  2131. }
  2132. pm_runtime_mark_last_busy(&sw->dev);
  2133. pm_runtime_put_autosuspend(&sw->dev);
  2134. put_sw:
  2135. tb_switch_put(sw);
  2136. out:
  2137. mutex_unlock(&tb->lock);
  2138. pm_runtime_mark_last_busy(&tb->dev);
  2139. pm_runtime_put_autosuspend(&tb->dev);
  2140. kfree(ev);
  2141. }
  2142. static int tb_alloc_dp_bandwidth(struct tb_tunnel *tunnel, int *requested_up,
  2143. int *requested_down)
  2144. {
  2145. int allocated_up, allocated_down, available_up, available_down, ret;
  2146. int requested_up_corrected, requested_down_corrected, granularity;
  2147. int max_up, max_down, max_up_rounded, max_down_rounded;
  2148. struct tb_bandwidth_group *group;
  2149. struct tb *tb = tunnel->tb;
  2150. struct tb_port *in, *out;
  2151. bool downstream;
  2152. ret = tb_tunnel_allocated_bandwidth(tunnel, &allocated_up, &allocated_down);
  2153. if (ret)
  2154. return ret;
  2155. in = tunnel->src_port;
  2156. out = tunnel->dst_port;
  2157. tb_tunnel_dbg(tunnel, "bandwidth allocated currently %d/%d Mb/s\n",
  2158. allocated_up, allocated_down);
  2159. /*
  2160. * If we get rounded up request from graphics side, say HBR2 x 4
  2161. * that is 17500 instead of 17280 (this is because of the
  2162. * granularity), we allow it too. Here the graphics has already
  2163. * negotiated with the DPRX the maximum possible rates (which is
  2164. * 17280 in this case).
  2165. *
  2166. * Since the link cannot go higher than 17280 we use that in our
  2167. * calculations but the DP IN adapter Allocated BW write must be
  2168. * the same value (17500) otherwise the adapter will mark it as
  2169. * failed for graphics.
  2170. */
  2171. ret = tb_tunnel_maximum_bandwidth(tunnel, &max_up, &max_down);
  2172. if (ret)
  2173. goto fail;
  2174. ret = usb4_dp_port_granularity(in);
  2175. if (ret < 0)
  2176. goto fail;
  2177. granularity = ret;
  2178. max_up_rounded = roundup(max_up, granularity);
  2179. max_down_rounded = roundup(max_down, granularity);
  2180. /*
  2181. * This will "fix" the request down to the maximum supported
  2182. * rate * lanes if it is at the maximum rounded up level.
  2183. */
  2184. requested_up_corrected = *requested_up;
  2185. if (requested_up_corrected == max_up_rounded)
  2186. requested_up_corrected = max_up;
  2187. else if (requested_up_corrected < 0)
  2188. requested_up_corrected = 0;
  2189. requested_down_corrected = *requested_down;
  2190. if (requested_down_corrected == max_down_rounded)
  2191. requested_down_corrected = max_down;
  2192. else if (requested_down_corrected < 0)
  2193. requested_down_corrected = 0;
  2194. tb_tunnel_dbg(tunnel, "corrected bandwidth request %d/%d Mb/s\n",
  2195. requested_up_corrected, requested_down_corrected);
  2196. if ((*requested_up >= 0 && requested_up_corrected > max_up_rounded) ||
  2197. (*requested_down >= 0 && requested_down_corrected > max_down_rounded)) {
  2198. tb_tunnel_dbg(tunnel,
  2199. "bandwidth request too high (%d/%d Mb/s > %d/%d Mb/s)\n",
  2200. requested_up_corrected, requested_down_corrected,
  2201. max_up_rounded, max_down_rounded);
  2202. ret = -ENOBUFS;
  2203. goto fail;
  2204. }
  2205. downstream = tb_tunnel_direction_downstream(tunnel);
  2206. group = in->group;
  2207. if ((*requested_up >= 0 && requested_up_corrected <= allocated_up) ||
  2208. (*requested_down >= 0 && requested_down_corrected <= allocated_down)) {
  2209. if (tunnel->bw_mode) {
  2210. int reserved;
  2211. /*
  2212. * If requested bandwidth is less or equal than
  2213. * what is currently allocated to that tunnel we
  2214. * simply change the reservation of the tunnel
  2215. * and add the released bandwidth for the group
  2216. * for the next 10s. Then we release it for
  2217. * others to use.
  2218. */
  2219. if (downstream)
  2220. reserved = allocated_down - *requested_down;
  2221. else
  2222. reserved = allocated_up - *requested_up;
  2223. if (reserved > 0) {
  2224. group->reserved += reserved;
  2225. tb_dbg(tb, "group %d reserved %d total %d Mb/s\n",
  2226. group->index, reserved, group->reserved);
  2227. /*
  2228. * If it was not already pending,
  2229. * schedule release now. If it is then
  2230. * postpone it for the next 10s (unless
  2231. * it is already running in which case
  2232. * the 10s already expired and we should
  2233. * give the reserved back to others).
  2234. */
  2235. mod_delayed_work(system_percpu_wq, &group->release_work,
  2236. msecs_to_jiffies(TB_RELEASE_BW_TIMEOUT));
  2237. }
  2238. }
  2239. ret = tb_tunnel_alloc_bandwidth(tunnel, requested_up,
  2240. requested_down);
  2241. if (ret)
  2242. goto fail;
  2243. return 0;
  2244. }
  2245. /*
  2246. * More bandwidth is requested. Release all the potential
  2247. * bandwidth from USB3 first.
  2248. */
  2249. ret = tb_release_unused_usb3_bandwidth(tb, in, out);
  2250. if (ret)
  2251. goto fail;
  2252. /*
  2253. * Then go over all tunnels that cross the same USB4 ports (they
  2254. * are also in the same group but we use the same function here
  2255. * that we use with the normal bandwidth allocation).
  2256. */
  2257. ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down,
  2258. true);
  2259. if (ret)
  2260. goto reclaim;
  2261. tb_tunnel_dbg(tunnel, "bandwidth available for allocation %d/%d (+ %u reserved) Mb/s\n",
  2262. available_up, available_down, group->reserved);
  2263. if ((*requested_up >= 0 &&
  2264. available_up + group->reserved >= requested_up_corrected) ||
  2265. (*requested_down >= 0 &&
  2266. available_down + group->reserved >= requested_down_corrected)) {
  2267. int released = 0;
  2268. /*
  2269. * If bandwidth on a link is >= asym_threshold
  2270. * transition the link to asymmetric.
  2271. */
  2272. ret = tb_configure_asym(tb, in, out, *requested_up,
  2273. *requested_down);
  2274. if (ret) {
  2275. tb_configure_sym(tb, in, out, true);
  2276. goto fail;
  2277. }
  2278. ret = tb_tunnel_alloc_bandwidth(tunnel, requested_up,
  2279. requested_down);
  2280. if (ret) {
  2281. tb_tunnel_warn(tunnel, "failed to allocate bandwidth\n");
  2282. tb_configure_sym(tb, in, out, true);
  2283. }
  2284. if (downstream) {
  2285. if (*requested_down > available_down)
  2286. released = *requested_down - available_down;
  2287. } else {
  2288. if (*requested_up > available_up)
  2289. released = *requested_up - available_up;
  2290. }
  2291. if (released) {
  2292. group->reserved -= released;
  2293. tb_dbg(tb, "group %d released %d total %d Mb/s\n",
  2294. group->index, released, group->reserved);
  2295. }
  2296. } else {
  2297. ret = -ENOBUFS;
  2298. }
  2299. reclaim:
  2300. tb_reclaim_usb3_bandwidth(tb, in, out);
  2301. fail:
  2302. if (ret && ret != -ENODEV) {
  2303. /*
  2304. * Write back the same allocated (so no change), this
  2305. * makes the DPTX request fail on graphics side.
  2306. */
  2307. tb_tunnel_dbg(tunnel,
  2308. "failing the request by rewriting allocated %d/%d Mb/s\n",
  2309. allocated_up, allocated_down);
  2310. tb_tunnel_alloc_bandwidth(tunnel, &allocated_up, &allocated_down);
  2311. tb_tunnel_event(tb, TB_TUNNEL_NO_BANDWIDTH, TB_TUNNEL_DP, in, out);
  2312. }
  2313. return ret;
  2314. }
  2315. static void tb_handle_dp_bandwidth_request(struct work_struct *work)
  2316. {
  2317. struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work.work);
  2318. int requested_bw, requested_up, requested_down, ret;
  2319. struct tb_tunnel *tunnel;
  2320. struct tb *tb = ev->tb;
  2321. struct tb_cm *tcm = tb_priv(tb);
  2322. struct tb_switch *sw;
  2323. struct tb_port *in;
  2324. pm_runtime_get_sync(&tb->dev);
  2325. mutex_lock(&tb->lock);
  2326. if (!tcm->hotplug_active)
  2327. goto unlock;
  2328. sw = tb_switch_find_by_route(tb, ev->route);
  2329. if (!sw) {
  2330. tb_warn(tb, "bandwidth request from non-existent router %llx\n",
  2331. ev->route);
  2332. goto unlock;
  2333. }
  2334. in = &sw->ports[ev->port];
  2335. if (!tb_port_is_dpin(in)) {
  2336. tb_port_warn(in, "bandwidth request to non-DP IN adapter\n");
  2337. goto put_sw;
  2338. }
  2339. tb_port_dbg(in, "handling bandwidth allocation request, retry %d\n", ev->retry);
  2340. tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
  2341. if (!tunnel) {
  2342. tb_port_warn(in, "failed to find tunnel\n");
  2343. goto put_sw;
  2344. }
  2345. if (!usb4_dp_port_bandwidth_mode_enabled(in)) {
  2346. if (tunnel->bw_mode) {
  2347. /*
  2348. * Reset the tunnel back to use the legacy
  2349. * allocation.
  2350. */
  2351. tunnel->bw_mode = false;
  2352. tb_port_dbg(in, "DPTX disabled bandwidth allocation mode\n");
  2353. } else {
  2354. tb_port_warn(in, "bandwidth allocation mode not enabled\n");
  2355. }
  2356. goto put_sw;
  2357. }
  2358. ret = usb4_dp_port_requested_bandwidth(in);
  2359. if (ret < 0) {
  2360. if (ret == -ENODATA) {
  2361. /*
  2362. * There is no request active so this means the
  2363. * BW allocation mode was enabled from graphics
  2364. * side. At this point we know that the graphics
  2365. * driver has read the DPRX capabilities so we
  2366. * can offer better bandwidth estimation.
  2367. */
  2368. tb_port_dbg(in, "DPTX enabled bandwidth allocation mode, updating estimated bandwidth\n");
  2369. tb_recalc_estimated_bandwidth(tb);
  2370. } else {
  2371. tb_port_warn(in, "failed to read requested bandwidth\n");
  2372. }
  2373. goto put_sw;
  2374. }
  2375. requested_bw = ret;
  2376. tb_port_dbg(in, "requested bandwidth %d Mb/s\n", requested_bw);
  2377. if (tb_tunnel_direction_downstream(tunnel)) {
  2378. requested_up = -1;
  2379. requested_down = requested_bw;
  2380. } else {
  2381. requested_up = requested_bw;
  2382. requested_down = -1;
  2383. }
  2384. ret = tb_alloc_dp_bandwidth(tunnel, &requested_up, &requested_down);
  2385. if (ret) {
  2386. if (ret == -ENOBUFS) {
  2387. tb_tunnel_warn(tunnel,
  2388. "not enough bandwidth available\n");
  2389. } else if (ret == -ENOTCONN) {
  2390. tb_tunnel_dbg(tunnel, "not active yet\n");
  2391. /*
  2392. * We got bandwidth allocation request but the
  2393. * tunnel is not yet active. This means that
  2394. * tb_dp_tunnel_active() is not yet called for
  2395. * this tunnel. Allow it some time and retry
  2396. * this request a couple of times.
  2397. */
  2398. if (ev->retry < TB_BW_ALLOC_RETRIES) {
  2399. tb_tunnel_dbg(tunnel,
  2400. "retrying bandwidth allocation request\n");
  2401. tb_queue_dp_bandwidth_request(tb, ev->route,
  2402. ev->port,
  2403. ev->retry + 1,
  2404. msecs_to_jiffies(50));
  2405. } else {
  2406. tb_tunnel_dbg(tunnel,
  2407. "run out of retries, failing the request");
  2408. }
  2409. } else {
  2410. tb_tunnel_warn(tunnel,
  2411. "failed to change bandwidth allocation\n");
  2412. }
  2413. } else {
  2414. tb_tunnel_dbg(tunnel,
  2415. "bandwidth allocation changed to %d/%d Mb/s\n",
  2416. requested_up, requested_down);
  2417. /* Update other clients about the allocation change */
  2418. tb_recalc_estimated_bandwidth(tb);
  2419. }
  2420. put_sw:
  2421. tb_switch_put(sw);
  2422. unlock:
  2423. mutex_unlock(&tb->lock);
  2424. pm_runtime_mark_last_busy(&tb->dev);
  2425. pm_runtime_put_autosuspend(&tb->dev);
  2426. kfree(ev);
  2427. }
  2428. static void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port,
  2429. int retry, unsigned long delay)
  2430. {
  2431. struct tb_hotplug_event *ev;
  2432. ev = kmalloc_obj(*ev);
  2433. if (!ev)
  2434. return;
  2435. ev->tb = tb;
  2436. ev->route = route;
  2437. ev->port = port;
  2438. ev->retry = retry;
  2439. INIT_DELAYED_WORK(&ev->work, tb_handle_dp_bandwidth_request);
  2440. queue_delayed_work(tb->wq, &ev->work, delay);
  2441. }
  2442. static void tb_handle_notification(struct tb *tb, u64 route,
  2443. const struct cfg_error_pkg *error)
  2444. {
  2445. switch (error->error) {
  2446. case TB_CFG_ERROR_PCIE_WAKE:
  2447. case TB_CFG_ERROR_DP_CON_CHANGE:
  2448. case TB_CFG_ERROR_DPTX_DISCOVERY:
  2449. if (tb_cfg_ack_notification(tb->ctl, route, error))
  2450. tb_warn(tb, "could not ack notification on %llx\n",
  2451. route);
  2452. break;
  2453. case TB_CFG_ERROR_DP_BW:
  2454. if (tb_cfg_ack_notification(tb->ctl, route, error))
  2455. tb_warn(tb, "could not ack notification on %llx\n",
  2456. route);
  2457. tb_queue_dp_bandwidth_request(tb, route, error->port, 0, 0);
  2458. break;
  2459. default:
  2460. /* Ignore for now */
  2461. break;
  2462. }
  2463. }
  2464. /*
  2465. * tb_schedule_hotplug_handler() - callback function for the control channel
  2466. *
  2467. * Delegates to tb_handle_hotplug.
  2468. */
  2469. static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
  2470. const void *buf, size_t size)
  2471. {
  2472. const struct cfg_event_pkg *pkg = buf;
  2473. u64 route = tb_cfg_get_route(&pkg->header);
  2474. switch (type) {
  2475. case TB_CFG_PKG_ERROR:
  2476. tb_handle_notification(tb, route, (const struct cfg_error_pkg *)buf);
  2477. return;
  2478. case TB_CFG_PKG_EVENT:
  2479. break;
  2480. default:
  2481. tb_warn(tb, "unexpected event %#x, ignoring\n", type);
  2482. return;
  2483. }
  2484. if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
  2485. tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
  2486. pkg->port);
  2487. }
  2488. tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
  2489. }
  2490. static void tb_stop(struct tb *tb)
  2491. {
  2492. struct tb_cm *tcm = tb_priv(tb);
  2493. struct tb_tunnel *tunnel;
  2494. struct tb_tunnel *n;
  2495. cancel_delayed_work(&tcm->remove_work);
  2496. /* tunnels are only present after everything has been initialized */
  2497. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
  2498. /*
  2499. * DMA tunnels require the driver to be functional so we
  2500. * tear them down. Other protocol tunnels can be left
  2501. * intact.
  2502. */
  2503. if (tb_tunnel_is_dma(tunnel))
  2504. tb_tunnel_deactivate(tunnel);
  2505. tb_tunnel_put(tunnel);
  2506. }
  2507. tb_switch_remove(tb->root_switch);
  2508. tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
  2509. }
  2510. static void tb_deinit(struct tb *tb)
  2511. {
  2512. struct tb_cm *tcm = tb_priv(tb);
  2513. int i;
  2514. /* Cancel all the release bandwidth workers */
  2515. for (i = 0; i < ARRAY_SIZE(tcm->groups); i++)
  2516. cancel_delayed_work_sync(&tcm->groups[i].release_work);
  2517. }
  2518. static int tb_scan_finalize_switch(struct device *dev, void *data)
  2519. {
  2520. if (tb_is_switch(dev)) {
  2521. struct tb_switch *sw = tb_to_switch(dev);
  2522. /*
  2523. * If we found that the switch was already setup by the
  2524. * boot firmware, mark it as authorized now before we
  2525. * send uevent to userspace.
  2526. */
  2527. if (sw->boot)
  2528. sw->authorized = 1;
  2529. dev_set_uevent_suppress(dev, false);
  2530. kobject_uevent(&dev->kobj, KOBJ_ADD);
  2531. device_for_each_child(dev, NULL, tb_scan_finalize_switch);
  2532. }
  2533. return 0;
  2534. }
  2535. static int tb_start(struct tb *tb, bool reset)
  2536. {
  2537. struct tb_cm *tcm = tb_priv(tb);
  2538. bool discover = true;
  2539. int ret;
  2540. tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
  2541. if (IS_ERR(tb->root_switch))
  2542. return PTR_ERR(tb->root_switch);
  2543. /*
  2544. * ICM firmware upgrade needs running firmware and in native
  2545. * mode that is not available so disable firmware upgrade of the
  2546. * root switch.
  2547. *
  2548. * However, USB4 routers support NVM firmware upgrade if they
  2549. * implement the necessary router operations.
  2550. */
  2551. tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch);
  2552. /* All USB4 routers support runtime PM */
  2553. tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
  2554. ret = tb_switch_configure(tb->root_switch);
  2555. if (ret) {
  2556. tb_switch_put(tb->root_switch);
  2557. return ret;
  2558. }
  2559. /* Announce the switch to the world */
  2560. ret = tb_switch_add(tb->root_switch);
  2561. if (ret) {
  2562. tb_switch_put(tb->root_switch);
  2563. return ret;
  2564. }
  2565. /*
  2566. * To support highest CLx state, we set host router's TMU to
  2567. * Normal mode.
  2568. */
  2569. tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES);
  2570. /* Enable TMU if it is off */
  2571. tb_switch_tmu_enable(tb->root_switch);
  2572. /*
  2573. * Boot firmware might have created tunnels of its own. Since we
  2574. * cannot be sure they are usable for us, tear them down and
  2575. * reset the ports to handle it as new hotplug for USB4 v1
  2576. * routers (for USB4 v2 and beyond we already do host reset).
  2577. */
  2578. if (reset && tb_switch_is_usb4(tb->root_switch)) {
  2579. discover = false;
  2580. if (usb4_switch_version(tb->root_switch) == 1)
  2581. tb_switch_reset(tb->root_switch);
  2582. }
  2583. if (discover) {
  2584. /* Full scan to discover devices added before the driver was loaded. */
  2585. tb_scan_switch(tb->root_switch);
  2586. /* Find out tunnels created by the boot firmware */
  2587. tb_discover_tunnels(tb);
  2588. /* Add DP resources from the DP tunnels created by the boot firmware */
  2589. tb_discover_dp_resources(tb);
  2590. }
  2591. /*
  2592. * If the boot firmware did not create USB 3.x tunnels create them
  2593. * now for the whole topology.
  2594. */
  2595. tb_create_usb3_tunnels(tb->root_switch);
  2596. /* Add DP IN resources for the root switch */
  2597. tb_add_dp_resources(tb->root_switch);
  2598. tb_switch_enter_redrive(tb->root_switch);
  2599. /* Make the discovered switches available to the userspace */
  2600. device_for_each_child(&tb->root_switch->dev, NULL,
  2601. tb_scan_finalize_switch);
  2602. /* Allow tb_handle_hotplug to progress events */
  2603. tcm->hotplug_active = true;
  2604. return 0;
  2605. }
  2606. static int tb_suspend_noirq(struct tb *tb)
  2607. {
  2608. struct tb_cm *tcm = tb_priv(tb);
  2609. tb_dbg(tb, "suspending...\n");
  2610. tb_disconnect_and_release_dp(tb);
  2611. tb_switch_exit_redrive(tb->root_switch);
  2612. tb_switch_suspend(tb->root_switch, false);
  2613. tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
  2614. tb_dbg(tb, "suspend finished\n");
  2615. return 0;
  2616. }
  2617. static void tb_restore_children(struct tb_switch *sw)
  2618. {
  2619. struct tb_port *port;
  2620. /* No need to restore if the router is already unplugged */
  2621. if (sw->is_unplugged)
  2622. return;
  2623. if (tb_enable_clx(sw))
  2624. tb_sw_warn(sw, "failed to re-enable CL states\n");
  2625. if (tb_enable_tmu(sw))
  2626. tb_sw_warn(sw, "failed to restore TMU configuration\n");
  2627. tb_switch_configuration_valid(sw);
  2628. tb_switch_for_each_port(sw, port) {
  2629. if (!tb_port_has_remote(port) && !port->xdomain)
  2630. continue;
  2631. if (port->remote) {
  2632. tb_switch_set_link_width(port->remote->sw,
  2633. port->remote->sw->link_width);
  2634. tb_switch_configure_link(port->remote->sw);
  2635. tb_restore_children(port->remote->sw);
  2636. } else if (port->xdomain) {
  2637. tb_port_configure_xdomain(port, port->xdomain);
  2638. }
  2639. }
  2640. }
  2641. static int tb_resume_noirq(struct tb *tb)
  2642. {
  2643. struct tb_cm *tcm = tb_priv(tb);
  2644. struct tb_tunnel *tunnel, *n;
  2645. unsigned int usb3_delay = 0;
  2646. LIST_HEAD(tunnels);
  2647. tb_dbg(tb, "resuming...\n");
  2648. /*
  2649. * For non-USB4 hosts (Apple systems) remove any PCIe devices
  2650. * the firmware might have setup.
  2651. */
  2652. if (!tb_switch_is_usb4(tb->root_switch))
  2653. tb_switch_reset(tb->root_switch);
  2654. tb_switch_resume(tb->root_switch, false);
  2655. tb_free_invalid_tunnels(tb);
  2656. tb_free_unplugged_children(tb->root_switch);
  2657. tb_restore_children(tb->root_switch);
  2658. /*
  2659. * If we get here from suspend to disk the boot firmware or the
  2660. * restore kernel might have created tunnels of its own. Since
  2661. * we cannot be sure they are usable for us we find and tear
  2662. * them down.
  2663. */
  2664. tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
  2665. list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
  2666. if (tb_tunnel_is_usb3(tunnel))
  2667. usb3_delay = 500;
  2668. tb_tunnel_deactivate(tunnel);
  2669. tb_tunnel_put(tunnel);
  2670. }
  2671. /* Re-create our tunnels now */
  2672. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
  2673. /* USB3 requires delay before it can be re-activated */
  2674. if (tb_tunnel_is_usb3(tunnel)) {
  2675. msleep(usb3_delay);
  2676. /* Only need to do it once */
  2677. usb3_delay = 0;
  2678. }
  2679. tb_tunnel_activate(tunnel);
  2680. }
  2681. if (!list_empty(&tcm->tunnel_list)) {
  2682. /*
  2683. * the pcie links need some time to get going.
  2684. * 100ms works for me...
  2685. */
  2686. tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
  2687. msleep(100);
  2688. }
  2689. tb_switch_enter_redrive(tb->root_switch);
  2690. /* Allow tb_handle_hotplug to progress events */
  2691. tcm->hotplug_active = true;
  2692. tb_dbg(tb, "resume finished\n");
  2693. return 0;
  2694. }
  2695. static int tb_free_unplugged_xdomains(struct tb_switch *sw)
  2696. {
  2697. struct tb_port *port;
  2698. int ret = 0;
  2699. tb_switch_for_each_port(sw, port) {
  2700. if (tb_is_upstream_port(port))
  2701. continue;
  2702. if (port->xdomain && port->xdomain->is_unplugged) {
  2703. tb_retimer_remove_all(port);
  2704. tb_xdomain_remove(port->xdomain);
  2705. tb_port_unconfigure_xdomain(port);
  2706. port->xdomain = NULL;
  2707. ret++;
  2708. } else if (port->remote) {
  2709. ret += tb_free_unplugged_xdomains(port->remote->sw);
  2710. }
  2711. }
  2712. return ret;
  2713. }
  2714. static int tb_freeze_noirq(struct tb *tb)
  2715. {
  2716. struct tb_cm *tcm = tb_priv(tb);
  2717. tcm->hotplug_active = false;
  2718. return 0;
  2719. }
  2720. static int tb_thaw_noirq(struct tb *tb)
  2721. {
  2722. struct tb_cm *tcm = tb_priv(tb);
  2723. tcm->hotplug_active = true;
  2724. return 0;
  2725. }
  2726. static void tb_complete(struct tb *tb)
  2727. {
  2728. /*
  2729. * Release any unplugged XDomains and if there is a case where
  2730. * another domain is swapped in place of unplugged XDomain we
  2731. * need to run another rescan.
  2732. */
  2733. mutex_lock(&tb->lock);
  2734. if (tb_free_unplugged_xdomains(tb->root_switch))
  2735. tb_scan_switch(tb->root_switch);
  2736. mutex_unlock(&tb->lock);
  2737. }
  2738. static int tb_runtime_suspend(struct tb *tb)
  2739. {
  2740. struct tb_cm *tcm = tb_priv(tb);
  2741. mutex_lock(&tb->lock);
  2742. /*
  2743. * The below call only releases DP resources to allow exiting and
  2744. * re-entering redrive mode.
  2745. */
  2746. tb_disconnect_and_release_dp(tb);
  2747. tb_switch_exit_redrive(tb->root_switch);
  2748. tb_switch_suspend(tb->root_switch, true);
  2749. tcm->hotplug_active = false;
  2750. mutex_unlock(&tb->lock);
  2751. return 0;
  2752. }
  2753. static void tb_remove_work(struct work_struct *work)
  2754. {
  2755. struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
  2756. struct tb *tb = tcm_to_tb(tcm);
  2757. mutex_lock(&tb->lock);
  2758. if (tb->root_switch) {
  2759. tb_free_unplugged_children(tb->root_switch);
  2760. tb_free_unplugged_xdomains(tb->root_switch);
  2761. }
  2762. mutex_unlock(&tb->lock);
  2763. }
  2764. static int tb_runtime_resume(struct tb *tb)
  2765. {
  2766. struct tb_cm *tcm = tb_priv(tb);
  2767. struct tb_tunnel *tunnel, *n;
  2768. mutex_lock(&tb->lock);
  2769. tb_switch_resume(tb->root_switch, true);
  2770. tb_free_invalid_tunnels(tb);
  2771. tb_restore_children(tb->root_switch);
  2772. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
  2773. tb_tunnel_activate(tunnel);
  2774. tb_switch_enter_redrive(tb->root_switch);
  2775. tcm->hotplug_active = true;
  2776. mutex_unlock(&tb->lock);
  2777. /*
  2778. * Schedule cleanup of any unplugged devices. Run this in a
  2779. * separate thread to avoid possible deadlock if the device
  2780. * removal runtime resumes the unplugged device.
  2781. */
  2782. queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
  2783. return 0;
  2784. }
  2785. static const struct tb_cm_ops tb_cm_ops = {
  2786. .start = tb_start,
  2787. .stop = tb_stop,
  2788. .deinit = tb_deinit,
  2789. .suspend_noirq = tb_suspend_noirq,
  2790. .resume_noirq = tb_resume_noirq,
  2791. .freeze_noirq = tb_freeze_noirq,
  2792. .thaw_noirq = tb_thaw_noirq,
  2793. .complete = tb_complete,
  2794. .runtime_suspend = tb_runtime_suspend,
  2795. .runtime_resume = tb_runtime_resume,
  2796. .handle_event = tb_handle_event,
  2797. .disapprove_switch = tb_disconnect_pci,
  2798. .approve_switch = tb_tunnel_pci,
  2799. .approve_xdomain_paths = tb_approve_xdomain_paths,
  2800. .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
  2801. };
  2802. /*
  2803. * During suspend the Thunderbolt controller is reset and all PCIe
  2804. * tunnels are lost. The NHI driver will try to reestablish all tunnels
  2805. * during resume. This adds device links between the tunneled PCIe
  2806. * downstream ports and the NHI so that the device core will make sure
  2807. * NHI is resumed first before the rest.
  2808. */
  2809. static bool tb_apple_add_links(struct tb_nhi *nhi)
  2810. {
  2811. struct pci_dev *upstream, *pdev;
  2812. bool ret;
  2813. if (!x86_apple_machine)
  2814. return false;
  2815. switch (nhi->pdev->device) {
  2816. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  2817. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
  2818. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  2819. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  2820. break;
  2821. default:
  2822. return false;
  2823. }
  2824. upstream = pci_upstream_bridge(nhi->pdev);
  2825. while (upstream) {
  2826. if (!pci_is_pcie(upstream))
  2827. return false;
  2828. if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
  2829. break;
  2830. upstream = pci_upstream_bridge(upstream);
  2831. }
  2832. if (!upstream)
  2833. return false;
  2834. /*
  2835. * For each hotplug downstream port, create add device link
  2836. * back to NHI so that PCIe tunnels can be re-established after
  2837. * sleep.
  2838. */
  2839. ret = false;
  2840. for_each_pci_bridge(pdev, upstream->subordinate) {
  2841. const struct device_link *link;
  2842. if (!pci_is_pcie(pdev))
  2843. continue;
  2844. if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
  2845. !pdev->is_pciehp)
  2846. continue;
  2847. link = device_link_add(&pdev->dev, &nhi->pdev->dev,
  2848. DL_FLAG_AUTOREMOVE_SUPPLIER |
  2849. DL_FLAG_PM_RUNTIME);
  2850. if (link) {
  2851. dev_dbg(&nhi->pdev->dev, "created link from %s\n",
  2852. dev_name(&pdev->dev));
  2853. ret = true;
  2854. } else {
  2855. dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
  2856. dev_name(&pdev->dev));
  2857. }
  2858. }
  2859. return ret;
  2860. }
  2861. struct tb *tb_probe(struct tb_nhi *nhi)
  2862. {
  2863. struct tb_cm *tcm;
  2864. struct tb *tb;
  2865. tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
  2866. if (!tb)
  2867. return NULL;
  2868. if (tb_acpi_may_tunnel_pcie())
  2869. tb->security_level = TB_SECURITY_USER;
  2870. else
  2871. tb->security_level = TB_SECURITY_NOPCIE;
  2872. tb->cm_ops = &tb_cm_ops;
  2873. tcm = tb_priv(tb);
  2874. INIT_LIST_HEAD(&tcm->tunnel_list);
  2875. INIT_LIST_HEAD(&tcm->dp_resources);
  2876. INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
  2877. tb_init_bandwidth_groups(tcm);
  2878. tb_dbg(tb, "using software connection manager\n");
  2879. /*
  2880. * Device links are needed to make sure we establish tunnels
  2881. * before the PCIe/USB stack is resumed so complain here if we
  2882. * found them missing.
  2883. */
  2884. if (!tb_apple_add_links(nhi) && !tb_acpi_add_links(nhi))
  2885. tb_warn(tb, "device links to tunneled native ports are missing!\n");
  2886. return tb;
  2887. }