lc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt link controller support
  4. *
  5. * Copyright (C) 2019, Intel Corporation
  6. * Author: Mika Westerberg <[email protected]>
  7. */
  8. #include "tb.h"
  9. /**
  10. * tb_lc_read_uuid() - Read switch UUID from link controller common register
  11. * @sw: Switch whose UUID is read
  12. * @uuid: UUID is placed here
  13. */
  14. int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid)
  15. {
  16. if (!sw->cap_lc)
  17. return -EINVAL;
  18. return tb_sw_read(sw, uuid, TB_CFG_SWITCH, sw->cap_lc + TB_LC_FUSE, 4);
  19. }
  20. static int read_lc_desc(struct tb_switch *sw, u32 *desc)
  21. {
  22. if (!sw->cap_lc)
  23. return -EINVAL;
  24. return tb_sw_read(sw, desc, TB_CFG_SWITCH, sw->cap_lc + TB_LC_DESC, 1);
  25. }
  26. static int find_port_lc_cap(struct tb_port *port)
  27. {
  28. struct tb_switch *sw = port->sw;
  29. int start, phys, ret, size;
  30. u32 desc;
  31. ret = read_lc_desc(sw, &desc);
  32. if (ret)
  33. return ret;
  34. /* Start of port LC registers */
  35. start = (desc & TB_LC_DESC_SIZE_MASK) >> TB_LC_DESC_SIZE_SHIFT;
  36. size = (desc & TB_LC_DESC_PORT_SIZE_MASK) >> TB_LC_DESC_PORT_SIZE_SHIFT;
  37. phys = tb_phy_port_from_link(port->port);
  38. return sw->cap_lc + start + phys * size;
  39. }
  40. static int tb_lc_set_port_configured(struct tb_port *port, bool configured)
  41. {
  42. bool upstream = tb_is_upstream_port(port);
  43. struct tb_switch *sw = port->sw;
  44. u32 ctrl, lane;
  45. int cap, ret;
  46. if (sw->generation < 2)
  47. return 0;
  48. cap = find_port_lc_cap(port);
  49. if (cap < 0)
  50. return cap;
  51. ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
  52. if (ret)
  53. return ret;
  54. /* Resolve correct lane */
  55. if (port->port % 2)
  56. lane = TB_LC_SX_CTRL_L1C;
  57. else
  58. lane = TB_LC_SX_CTRL_L2C;
  59. if (configured) {
  60. ctrl |= lane;
  61. if (upstream)
  62. ctrl |= TB_LC_SX_CTRL_UPSTREAM;
  63. } else {
  64. ctrl &= ~lane;
  65. if (upstream)
  66. ctrl &= ~TB_LC_SX_CTRL_UPSTREAM;
  67. }
  68. return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
  69. }
  70. /**
  71. * tb_lc_configure_port() - Let LC know about configured port
  72. * @port: Port that is set as configured
  73. *
  74. * Sets the port configured for power management purposes.
  75. */
  76. int tb_lc_configure_port(struct tb_port *port)
  77. {
  78. return tb_lc_set_port_configured(port, true);
  79. }
  80. /**
  81. * tb_lc_unconfigure_port() - Let LC know about unconfigured port
  82. * @port: Port that is set as configured
  83. *
  84. * Sets the port unconfigured for power management purposes.
  85. */
  86. void tb_lc_unconfigure_port(struct tb_port *port)
  87. {
  88. tb_lc_set_port_configured(port, false);
  89. }
  90. static int tb_lc_set_xdomain_configured(struct tb_port *port, bool configure)
  91. {
  92. struct tb_switch *sw = port->sw;
  93. u32 ctrl, lane;
  94. int cap, ret;
  95. if (sw->generation < 2)
  96. return 0;
  97. cap = find_port_lc_cap(port);
  98. if (cap < 0)
  99. return cap;
  100. ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
  101. if (ret)
  102. return ret;
  103. /* Resolve correct lane */
  104. if (port->port % 2)
  105. lane = TB_LC_SX_CTRL_L1D;
  106. else
  107. lane = TB_LC_SX_CTRL_L2D;
  108. if (configure)
  109. ctrl |= lane;
  110. else
  111. ctrl &= ~lane;
  112. return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
  113. }
  114. /**
  115. * tb_lc_configure_xdomain() - Inform LC that the link is XDomain
  116. * @port: Switch downstream port connected to another host
  117. *
  118. * Sets the lane configured for XDomain accordingly so that the LC knows
  119. * about this. Returns %0 in success and negative errno in failure.
  120. */
  121. int tb_lc_configure_xdomain(struct tb_port *port)
  122. {
  123. return tb_lc_set_xdomain_configured(port, true);
  124. }
  125. /**
  126. * tb_lc_unconfigure_xdomain() - Unconfigure XDomain from port
  127. * @port: Switch downstream port that was connected to another host
  128. *
  129. * Unsets the lane XDomain configuration.
  130. */
  131. void tb_lc_unconfigure_xdomain(struct tb_port *port)
  132. {
  133. tb_lc_set_xdomain_configured(port, false);
  134. }
  135. /**
  136. * tb_lc_start_lane_initialization() - Start lane initialization
  137. * @port: Device router lane 0 adapter
  138. *
  139. * Starts lane initialization for @port after the router resumed from
  140. * sleep. Should be called for those downstream lane adapters that were
  141. * not connected (tb_lc_configure_port() was not called) before sleep.
  142. *
  143. * Returns %0 in success and negative errno in case of failure.
  144. */
  145. int tb_lc_start_lane_initialization(struct tb_port *port)
  146. {
  147. struct tb_switch *sw = port->sw;
  148. int ret, cap;
  149. u32 ctrl;
  150. if (!tb_route(sw))
  151. return 0;
  152. if (sw->generation < 2)
  153. return 0;
  154. cap = find_port_lc_cap(port);
  155. if (cap < 0)
  156. return cap;
  157. ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
  158. if (ret)
  159. return ret;
  160. ctrl |= TB_LC_SX_CTRL_SLI;
  161. return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, cap + TB_LC_SX_CTRL, 1);
  162. }
  163. /**
  164. * tb_lc_is_clx_supported() - Check whether CLx is supported by the lane adapter
  165. * @port: Lane adapter
  166. *
  167. * TB_LC_LINK_ATTR_CPS bit reflects if the link supports CLx including
  168. * active cables (if connected on the link).
  169. */
  170. bool tb_lc_is_clx_supported(struct tb_port *port)
  171. {
  172. struct tb_switch *sw = port->sw;
  173. int cap, ret;
  174. u32 val;
  175. cap = find_port_lc_cap(port);
  176. if (cap < 0)
  177. return false;
  178. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_ATTR, 1);
  179. if (ret)
  180. return false;
  181. return !!(val & TB_LC_LINK_ATTR_CPS);
  182. }
  183. /**
  184. * tb_lc_is_usb_plugged() - Is there USB device connected to port
  185. * @port: Device router lane 0 adapter
  186. *
  187. * Returns true if the @port has USB type-C device connected.
  188. */
  189. bool tb_lc_is_usb_plugged(struct tb_port *port)
  190. {
  191. struct tb_switch *sw = port->sw;
  192. int cap, ret;
  193. u32 val;
  194. if (sw->generation != 3)
  195. return false;
  196. cap = find_port_lc_cap(port);
  197. if (cap < 0)
  198. return false;
  199. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_CS_42, 1);
  200. if (ret)
  201. return false;
  202. return !!(val & TB_LC_CS_42_USB_PLUGGED);
  203. }
  204. /**
  205. * tb_lc_is_xhci_connected() - Is the internal xHCI connected
  206. * @port: Device router lane 0 adapter
  207. *
  208. * Returns true if the internal xHCI has been connected to @port.
  209. */
  210. bool tb_lc_is_xhci_connected(struct tb_port *port)
  211. {
  212. struct tb_switch *sw = port->sw;
  213. int cap, ret;
  214. u32 val;
  215. if (sw->generation != 3)
  216. return false;
  217. cap = find_port_lc_cap(port);
  218. if (cap < 0)
  219. return false;
  220. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_REQ, 1);
  221. if (ret)
  222. return false;
  223. return !!(val & TB_LC_LINK_REQ_XHCI_CONNECT);
  224. }
  225. static int __tb_lc_xhci_connect(struct tb_port *port, bool connect)
  226. {
  227. struct tb_switch *sw = port->sw;
  228. int cap, ret;
  229. u32 val;
  230. if (sw->generation != 3)
  231. return -EINVAL;
  232. cap = find_port_lc_cap(port);
  233. if (cap < 0)
  234. return cap;
  235. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_REQ, 1);
  236. if (ret)
  237. return ret;
  238. if (connect)
  239. val |= TB_LC_LINK_REQ_XHCI_CONNECT;
  240. else
  241. val &= ~TB_LC_LINK_REQ_XHCI_CONNECT;
  242. return tb_sw_write(sw, &val, TB_CFG_SWITCH, cap + TB_LC_LINK_REQ, 1);
  243. }
  244. /**
  245. * tb_lc_xhci_connect() - Connect internal xHCI
  246. * @port: Device router lane 0 adapter
  247. *
  248. * Tells LC to connect the internal xHCI to @port. Returns %0 on success
  249. * and negative errno in case of failure. Can be called for Thunderbolt 3
  250. * routers only.
  251. */
  252. int tb_lc_xhci_connect(struct tb_port *port)
  253. {
  254. int ret;
  255. ret = __tb_lc_xhci_connect(port, true);
  256. if (ret)
  257. return ret;
  258. tb_port_dbg(port, "xHCI connected\n");
  259. return 0;
  260. }
  261. /**
  262. * tb_lc_xhci_disconnect() - Disconnect internal xHCI
  263. * @port: Device router lane 0 adapter
  264. *
  265. * Tells LC to disconnect the internal xHCI from @port. Can be called
  266. * for Thunderbolt 3 routers only.
  267. */
  268. void tb_lc_xhci_disconnect(struct tb_port *port)
  269. {
  270. __tb_lc_xhci_connect(port, false);
  271. tb_port_dbg(port, "xHCI disconnected\n");
  272. }
  273. static int tb_lc_set_wake_one(struct tb_switch *sw, unsigned int offset,
  274. unsigned int flags)
  275. {
  276. u32 ctrl;
  277. int ret;
  278. /*
  279. * Enable wake on PCIe and USB4 (wake coming from another
  280. * router).
  281. */
  282. ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH,
  283. offset + TB_LC_SX_CTRL, 1);
  284. if (ret)
  285. return ret;
  286. ctrl &= ~(TB_LC_SX_CTRL_WOC | TB_LC_SX_CTRL_WOD | TB_LC_SX_CTRL_WODPC |
  287. TB_LC_SX_CTRL_WODPD | TB_LC_SX_CTRL_WOP | TB_LC_SX_CTRL_WOU4);
  288. if (flags & TB_WAKE_ON_CONNECT)
  289. ctrl |= TB_LC_SX_CTRL_WOC | TB_LC_SX_CTRL_WOD;
  290. if (flags & TB_WAKE_ON_USB4)
  291. ctrl |= TB_LC_SX_CTRL_WOU4;
  292. if (flags & TB_WAKE_ON_PCIE)
  293. ctrl |= TB_LC_SX_CTRL_WOP;
  294. if (flags & TB_WAKE_ON_DP)
  295. ctrl |= TB_LC_SX_CTRL_WODPC | TB_LC_SX_CTRL_WODPD;
  296. return tb_sw_write(sw, &ctrl, TB_CFG_SWITCH, offset + TB_LC_SX_CTRL, 1);
  297. }
  298. /**
  299. * tb_lc_set_wake() - Enable/disable wake
  300. * @sw: Switch whose wakes to configure
  301. * @flags: Wakeup flags (%0 to disable)
  302. *
  303. * For each LC sets wake bits accordingly.
  304. */
  305. int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags)
  306. {
  307. int start, size, nlc, ret, i;
  308. u32 desc;
  309. if (sw->generation < 2)
  310. return 0;
  311. if (!tb_route(sw))
  312. return 0;
  313. ret = read_lc_desc(sw, &desc);
  314. if (ret)
  315. return ret;
  316. /* Figure out number of link controllers */
  317. nlc = desc & TB_LC_DESC_NLC_MASK;
  318. start = (desc & TB_LC_DESC_SIZE_MASK) >> TB_LC_DESC_SIZE_SHIFT;
  319. size = (desc & TB_LC_DESC_PORT_SIZE_MASK) >> TB_LC_DESC_PORT_SIZE_SHIFT;
  320. /* For each link controller set sleep bit */
  321. for (i = 0; i < nlc; i++) {
  322. unsigned int offset = sw->cap_lc + start + i * size;
  323. ret = tb_lc_set_wake_one(sw, offset, flags);
  324. if (ret)
  325. return ret;
  326. }
  327. return 0;
  328. }
  329. /**
  330. * tb_lc_set_sleep() - Inform LC that the switch is going to sleep
  331. * @sw: Switch to set sleep
  332. *
  333. * Let the switch link controllers know that the switch is going to
  334. * sleep.
  335. */
  336. int tb_lc_set_sleep(struct tb_switch *sw)
  337. {
  338. int start, size, nlc, ret, i;
  339. u32 desc;
  340. if (sw->generation < 2)
  341. return 0;
  342. ret = read_lc_desc(sw, &desc);
  343. if (ret)
  344. return ret;
  345. /* Figure out number of link controllers */
  346. nlc = desc & TB_LC_DESC_NLC_MASK;
  347. start = (desc & TB_LC_DESC_SIZE_MASK) >> TB_LC_DESC_SIZE_SHIFT;
  348. size = (desc & TB_LC_DESC_PORT_SIZE_MASK) >> TB_LC_DESC_PORT_SIZE_SHIFT;
  349. /* For each link controller set sleep bit */
  350. for (i = 0; i < nlc; i++) {
  351. unsigned int offset = sw->cap_lc + start + i * size;
  352. u32 ctrl;
  353. ret = tb_sw_read(sw, &ctrl, TB_CFG_SWITCH,
  354. offset + TB_LC_SX_CTRL, 1);
  355. if (ret)
  356. return ret;
  357. ctrl |= TB_LC_SX_CTRL_SLP;
  358. ret = tb_sw_write(sw, &ctrl, TB_CFG_SWITCH,
  359. offset + TB_LC_SX_CTRL, 1);
  360. if (ret)
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * tb_lc_lane_bonding_possible() - Is lane bonding possible towards switch
  367. * @sw: Switch to check
  368. *
  369. * Checks whether conditions for lane bonding from parent to @sw are
  370. * possible.
  371. */
  372. bool tb_lc_lane_bonding_possible(struct tb_switch *sw)
  373. {
  374. struct tb_port *up;
  375. int cap, ret;
  376. u32 val;
  377. if (sw->generation < 2)
  378. return false;
  379. up = tb_upstream_port(sw);
  380. cap = find_port_lc_cap(up);
  381. if (cap < 0)
  382. return false;
  383. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, cap + TB_LC_PORT_ATTR, 1);
  384. if (ret)
  385. return false;
  386. return !!(val & TB_LC_PORT_ATTR_BE);
  387. }
  388. static int tb_lc_dp_sink_from_port(const struct tb_switch *sw,
  389. struct tb_port *in)
  390. {
  391. struct tb_port *port;
  392. /* The first DP IN port is sink 0 and second is sink 1 */
  393. tb_switch_for_each_port(sw, port) {
  394. if (tb_port_is_dpin(port))
  395. return in != port;
  396. }
  397. return -EINVAL;
  398. }
  399. static int tb_lc_dp_sink_available(struct tb_switch *sw, int sink)
  400. {
  401. u32 val, alloc;
  402. int ret;
  403. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
  404. sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
  405. if (ret)
  406. return ret;
  407. /*
  408. * Sink is available for CM/SW to use if the allocation valie is
  409. * either 0 or 1.
  410. */
  411. if (!sink) {
  412. alloc = val & TB_LC_SNK_ALLOCATION_SNK0_MASK;
  413. if (!alloc || alloc == TB_LC_SNK_ALLOCATION_SNK0_CM)
  414. return 0;
  415. } else {
  416. alloc = (val & TB_LC_SNK_ALLOCATION_SNK1_MASK) >>
  417. TB_LC_SNK_ALLOCATION_SNK1_SHIFT;
  418. if (!alloc || alloc == TB_LC_SNK_ALLOCATION_SNK1_CM)
  419. return 0;
  420. }
  421. return -EBUSY;
  422. }
  423. /**
  424. * tb_lc_dp_sink_query() - Is DP sink available for DP IN port
  425. * @sw: Switch whose DP sink is queried
  426. * @in: DP IN port to check
  427. *
  428. * Queries through LC SNK_ALLOCATION registers whether DP sink is available
  429. * for the given DP IN port or not.
  430. */
  431. bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in)
  432. {
  433. int sink;
  434. /*
  435. * For older generations sink is always available as there is no
  436. * allocation mechanism.
  437. */
  438. if (sw->generation < 3)
  439. return true;
  440. sink = tb_lc_dp_sink_from_port(sw, in);
  441. if (sink < 0)
  442. return false;
  443. return !tb_lc_dp_sink_available(sw, sink);
  444. }
  445. /**
  446. * tb_lc_dp_sink_alloc() - Allocate DP sink
  447. * @sw: Switch whose DP sink is allocated
  448. * @in: DP IN port the DP sink is allocated for
  449. *
  450. * Allocate DP sink for @in via LC SNK_ALLOCATION registers. If the
  451. * resource is available and allocation is successful returns %0. In all
  452. * other cases returs negative errno. In particular %-EBUSY is returned if
  453. * the resource was not available.
  454. */
  455. int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in)
  456. {
  457. int ret, sink;
  458. u32 val;
  459. if (sw->generation < 3)
  460. return 0;
  461. sink = tb_lc_dp_sink_from_port(sw, in);
  462. if (sink < 0)
  463. return sink;
  464. ret = tb_lc_dp_sink_available(sw, sink);
  465. if (ret)
  466. return ret;
  467. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
  468. sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
  469. if (ret)
  470. return ret;
  471. if (!sink) {
  472. val &= ~TB_LC_SNK_ALLOCATION_SNK0_MASK;
  473. val |= TB_LC_SNK_ALLOCATION_SNK0_CM;
  474. } else {
  475. val &= ~TB_LC_SNK_ALLOCATION_SNK1_MASK;
  476. val |= TB_LC_SNK_ALLOCATION_SNK1_CM <<
  477. TB_LC_SNK_ALLOCATION_SNK1_SHIFT;
  478. }
  479. ret = tb_sw_write(sw, &val, TB_CFG_SWITCH,
  480. sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
  481. if (ret)
  482. return ret;
  483. tb_port_dbg(in, "sink %d allocated\n", sink);
  484. return 0;
  485. }
  486. /**
  487. * tb_lc_dp_sink_dealloc() - De-allocate DP sink
  488. * @sw: Switch whose DP sink is de-allocated
  489. * @in: DP IN port whose DP sink is de-allocated
  490. *
  491. * De-allocate DP sink from @in using LC SNK_ALLOCATION registers.
  492. */
  493. int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in)
  494. {
  495. int ret, sink;
  496. u32 val;
  497. if (sw->generation < 3)
  498. return 0;
  499. sink = tb_lc_dp_sink_from_port(sw, in);
  500. if (sink < 0)
  501. return sink;
  502. /* Needs to be owned by CM/SW */
  503. ret = tb_lc_dp_sink_available(sw, sink);
  504. if (ret)
  505. return ret;
  506. ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
  507. sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
  508. if (ret)
  509. return ret;
  510. if (!sink)
  511. val &= ~TB_LC_SNK_ALLOCATION_SNK0_MASK;
  512. else
  513. val &= ~TB_LC_SNK_ALLOCATION_SNK1_MASK;
  514. ret = tb_sw_write(sw, &val, TB_CFG_SWITCH,
  515. sw->cap_lc + TB_LC_SNK_ALLOCATION, 1);
  516. if (ret)
  517. return ret;
  518. tb_port_dbg(in, "sink %d de-allocated\n", sink);
  519. return 0;
  520. }
  521. /**
  522. * tb_lc_force_power() - Forces LC to be powered on
  523. * @sw: Thunderbolt switch
  524. *
  525. * This is useful to let authentication cycle pass even without
  526. * a Thunderbolt link present.
  527. */
  528. int tb_lc_force_power(struct tb_switch *sw)
  529. {
  530. u32 in = 0xffff;
  531. return tb_sw_write(sw, &in, TB_CFG_SWITCH, TB_LC_POWER, 1);
  532. }