drm_dp_tunnel.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2023 Intel Corporation
  4. */
  5. #include <linux/ref_tracker.h>
  6. #include <linux/types.h>
  7. #include <drm/drm_atomic_state_helper.h>
  8. #include <drm/drm_atomic.h>
  9. #include <drm/drm_print.h>
  10. #include <drm/display/drm_dp.h>
  11. #include <drm/display/drm_dp_helper.h>
  12. #include <drm/display/drm_dp_tunnel.h>
  13. #define to_group(__private_obj) \
  14. container_of(__private_obj, struct drm_dp_tunnel_group, base)
  15. #define to_group_state(__private_state) \
  16. container_of(__private_state, struct drm_dp_tunnel_group_state, base)
  17. #define is_dp_tunnel_private_obj(__obj) \
  18. ((__obj)->funcs == &tunnel_group_funcs)
  19. #define for_each_new_group_in_state(__state, __new_group_state, __i) \
  20. for ((__i) = 0; \
  21. (__i) < (__state)->num_private_objs; \
  22. (__i)++) \
  23. for_each_if ((__state)->private_objs[__i].ptr && \
  24. is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
  25. ((__new_group_state) = \
  26. to_group_state((__state)->private_objs[__i].new_state), 1))
  27. #define for_each_old_group_in_state(__state, __old_group_state, __i) \
  28. for ((__i) = 0; \
  29. (__i) < (__state)->num_private_objs; \
  30. (__i)++) \
  31. for_each_if ((__state)->private_objs[__i].ptr && \
  32. is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
  33. ((__old_group_state) = \
  34. to_group_state((__state)->private_objs[__i].old_state), 1))
  35. #define for_each_tunnel_in_group(__group, __tunnel) \
  36. list_for_each_entry(__tunnel, &(__group)->tunnels, node)
  37. #define for_each_tunnel_state(__group_state, __tunnel_state) \
  38. list_for_each_entry(__tunnel_state, &(__group_state)->tunnel_states, node)
  39. #define for_each_tunnel_state_safe(__group_state, __tunnel_state, __tunnel_state_tmp) \
  40. list_for_each_entry_safe(__tunnel_state, __tunnel_state_tmp, \
  41. &(__group_state)->tunnel_states, node)
  42. #define kbytes_to_mbits(__kbytes) \
  43. DIV_ROUND_UP((__kbytes) * 8, 1000)
  44. #define DPTUN_BW_ARG(__bw) ((__bw) < 0 ? (__bw) : kbytes_to_mbits(__bw))
  45. #define __tun_prn(__tunnel, __level, __type, __fmt, ...) \
  46. drm_##__level##__type((__tunnel)->group->mgr->dev, \
  47. "[DPTUN %s][%s] " __fmt, \
  48. drm_dp_tunnel_name(__tunnel), \
  49. (__tunnel)->aux->name, ## \
  50. __VA_ARGS__)
  51. #define tun_dbg(__tunnel, __fmt, ...) \
  52. __tun_prn(__tunnel, dbg, _kms, __fmt, ## __VA_ARGS__)
  53. #define tun_dbg_stat(__tunnel, __err, __fmt, ...) do { \
  54. if (__err) \
  55. __tun_prn(__tunnel, dbg, _kms, __fmt " (Failed, err: %pe)\n", \
  56. ## __VA_ARGS__, ERR_PTR(__err)); \
  57. else \
  58. __tun_prn(__tunnel, dbg, _kms, __fmt " (Ok)\n", \
  59. ## __VA_ARGS__); \
  60. } while (0)
  61. #define tun_dbg_atomic(__tunnel, __fmt, ...) \
  62. __tun_prn(__tunnel, dbg, _atomic, __fmt, ## __VA_ARGS__)
  63. #define tun_grp_dbg(__group, __fmt, ...) \
  64. drm_dbg_kms((__group)->mgr->dev, \
  65. "[DPTUN %s] " __fmt, \
  66. drm_dp_tunnel_group_name(__group), ## \
  67. __VA_ARGS__)
  68. #define DP_TUNNELING_BASE DP_TUNNELING_OUI
  69. #define __DPTUN_REG_RANGE(__start, __size) \
  70. GENMASK_ULL((__start) + (__size) - 1, (__start))
  71. #define DPTUN_REG_RANGE(__addr, __size) \
  72. __DPTUN_REG_RANGE((__addr) - DP_TUNNELING_BASE, (__size))
  73. #define DPTUN_REG(__addr) DPTUN_REG_RANGE(__addr, 1)
  74. #define DPTUN_INFO_REG_MASK ( \
  75. DPTUN_REG_RANGE(DP_TUNNELING_OUI, DP_TUNNELING_OUI_BYTES) | \
  76. DPTUN_REG_RANGE(DP_TUNNELING_DEV_ID, DP_TUNNELING_DEV_ID_BYTES) | \
  77. DPTUN_REG(DP_TUNNELING_HW_REV) | \
  78. DPTUN_REG(DP_TUNNELING_SW_REV_MAJOR) | \
  79. DPTUN_REG(DP_TUNNELING_SW_REV_MINOR) | \
  80. DPTUN_REG(DP_TUNNELING_CAPABILITIES) | \
  81. DPTUN_REG(DP_IN_ADAPTER_INFO) | \
  82. DPTUN_REG(DP_USB4_DRIVER_ID) | \
  83. DPTUN_REG(DP_USB4_DRIVER_BW_CAPABILITY) | \
  84. DPTUN_REG(DP_IN_ADAPTER_TUNNEL_INFORMATION) | \
  85. DPTUN_REG(DP_BW_GRANULARITY) | \
  86. DPTUN_REG(DP_ESTIMATED_BW) | \
  87. DPTUN_REG(DP_ALLOCATED_BW) | \
  88. DPTUN_REG(DP_TUNNELING_MAX_LINK_RATE) | \
  89. DPTUN_REG(DP_TUNNELING_MAX_LANE_COUNT) | \
  90. DPTUN_REG(DP_DPTX_BW_ALLOCATION_MODE_CONTROL))
  91. static const DECLARE_BITMAP(dptun_info_regs, 64) = {
  92. DPTUN_INFO_REG_MASK & -1UL,
  93. #if BITS_PER_LONG == 32
  94. DPTUN_INFO_REG_MASK >> 32,
  95. #endif
  96. };
  97. struct drm_dp_tunnel_regs {
  98. u8 buf[HWEIGHT64(DPTUN_INFO_REG_MASK)];
  99. };
  100. struct drm_dp_tunnel_group;
  101. struct drm_dp_tunnel {
  102. struct drm_dp_tunnel_group *group;
  103. struct list_head node;
  104. struct kref kref;
  105. struct ref_tracker *tracker;
  106. struct drm_dp_aux *aux;
  107. char name[8];
  108. int bw_granularity;
  109. int estimated_bw;
  110. int allocated_bw;
  111. int max_dprx_rate;
  112. u8 max_dprx_lane_count;
  113. u8 adapter_id;
  114. bool bw_alloc_supported:1;
  115. bool bw_alloc_enabled:1;
  116. bool has_io_error:1;
  117. bool destroyed:1;
  118. };
  119. struct drm_dp_tunnel_group_state;
  120. struct drm_dp_tunnel_state {
  121. struct drm_dp_tunnel_group_state *group_state;
  122. struct drm_dp_tunnel_ref tunnel_ref;
  123. struct list_head node;
  124. u32 stream_mask;
  125. int *stream_bw;
  126. };
  127. struct drm_dp_tunnel_group_state {
  128. struct drm_private_state base;
  129. struct list_head tunnel_states;
  130. };
  131. struct drm_dp_tunnel_group {
  132. struct drm_private_obj base;
  133. struct drm_dp_tunnel_mgr *mgr;
  134. struct list_head tunnels;
  135. /* available BW including the allocated_bw of all tunnels in the group */
  136. int available_bw;
  137. u8 drv_group_id;
  138. char name[8];
  139. bool active:1;
  140. };
  141. struct drm_dp_tunnel_mgr {
  142. struct drm_device *dev;
  143. int group_count;
  144. struct drm_dp_tunnel_group *groups;
  145. wait_queue_head_t bw_req_queue;
  146. #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
  147. struct ref_tracker_dir ref_tracker;
  148. #endif
  149. };
  150. /*
  151. * The following helpers provide a way to read out the tunneling DPCD
  152. * registers with a minimal amount of AUX transfers (1 transfer per contiguous
  153. * range, as permitted by the 16 byte per transfer AUX limit), not accessing
  154. * other registers to avoid any read side-effects.
  155. */
  156. static int next_reg_area(int *offset)
  157. {
  158. *offset = find_next_bit(dptun_info_regs, 64, *offset);
  159. return find_next_zero_bit(dptun_info_regs, 64, *offset + 1) - *offset;
  160. }
  161. #define tunnel_reg_ptr(__regs, __address) ({ \
  162. WARN_ON(!test_bit((__address) - DP_TUNNELING_BASE, dptun_info_regs)); \
  163. &(__regs)->buf[bitmap_weight(dptun_info_regs, (__address) - DP_TUNNELING_BASE)]; \
  164. })
  165. static int read_tunnel_regs(struct drm_dp_aux *aux, struct drm_dp_tunnel_regs *regs)
  166. {
  167. int offset = 0;
  168. int len;
  169. while ((len = next_reg_area(&offset))) {
  170. int address = DP_TUNNELING_BASE + offset;
  171. if (drm_dp_dpcd_read(aux, address, tunnel_reg_ptr(regs, address), len) < 0)
  172. return -EIO;
  173. offset += len;
  174. }
  175. return 0;
  176. }
  177. static u8 tunnel_reg(const struct drm_dp_tunnel_regs *regs, int address)
  178. {
  179. return *tunnel_reg_ptr(regs, address);
  180. }
  181. static u8 tunnel_reg_drv_group_id(const struct drm_dp_tunnel_regs *regs)
  182. {
  183. u8 drv_id = tunnel_reg(regs, DP_USB4_DRIVER_ID) & DP_USB4_DRIVER_ID_MASK;
  184. u8 group_id = tunnel_reg(regs, DP_IN_ADAPTER_TUNNEL_INFORMATION) & DP_GROUP_ID_MASK;
  185. if (!group_id)
  186. return 0;
  187. return (drv_id << DP_GROUP_ID_BITS) | group_id;
  188. }
  189. /* Return granularity in kB/s units */
  190. static int tunnel_reg_bw_granularity(const struct drm_dp_tunnel_regs *regs)
  191. {
  192. int gr = tunnel_reg(regs, DP_BW_GRANULARITY) & DP_BW_GRANULARITY_MASK;
  193. if (gr > 2)
  194. return -1;
  195. return (250000 << gr) / 8;
  196. }
  197. static int tunnel_reg_max_dprx_rate(const struct drm_dp_tunnel_regs *regs)
  198. {
  199. u8 bw_code = tunnel_reg(regs, DP_TUNNELING_MAX_LINK_RATE);
  200. return drm_dp_bw_code_to_link_rate(bw_code);
  201. }
  202. static int tunnel_reg_max_dprx_lane_count(const struct drm_dp_tunnel_regs *regs)
  203. {
  204. return tunnel_reg(regs, DP_TUNNELING_MAX_LANE_COUNT) &
  205. DP_TUNNELING_MAX_LANE_COUNT_MASK;
  206. }
  207. static bool tunnel_reg_bw_alloc_supported(const struct drm_dp_tunnel_regs *regs)
  208. {
  209. u8 cap_mask = DP_TUNNELING_SUPPORT | DP_IN_BW_ALLOCATION_MODE_SUPPORT;
  210. if ((tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & cap_mask) != cap_mask)
  211. return false;
  212. return tunnel_reg(regs, DP_USB4_DRIVER_BW_CAPABILITY) &
  213. DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT;
  214. }
  215. static bool tunnel_reg_bw_alloc_enabled(const struct drm_dp_tunnel_regs *regs)
  216. {
  217. return tunnel_reg(regs, DP_DPTX_BW_ALLOCATION_MODE_CONTROL) &
  218. DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE;
  219. }
  220. static u8 tunnel_group_drv_id(u8 drv_group_id)
  221. {
  222. return drv_group_id >> DP_GROUP_ID_BITS;
  223. }
  224. static u8 tunnel_group_id(u8 drv_group_id)
  225. {
  226. return drv_group_id & DP_GROUP_ID_MASK;
  227. }
  228. const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
  229. {
  230. return tunnel->name;
  231. }
  232. EXPORT_SYMBOL(drm_dp_tunnel_name);
  233. static const char *drm_dp_tunnel_group_name(const struct drm_dp_tunnel_group *group)
  234. {
  235. return group->name;
  236. }
  237. static struct drm_dp_tunnel_group *
  238. lookup_or_alloc_group(struct drm_dp_tunnel_mgr *mgr, u8 drv_group_id)
  239. {
  240. struct drm_dp_tunnel_group *group = NULL;
  241. int i;
  242. for (i = 0; i < mgr->group_count; i++) {
  243. /*
  244. * A tunnel group with 0 group ID shouldn't have more than one
  245. * tunnels.
  246. */
  247. if (tunnel_group_id(drv_group_id) &&
  248. mgr->groups[i].drv_group_id == drv_group_id)
  249. return &mgr->groups[i];
  250. if (!group && !mgr->groups[i].active)
  251. group = &mgr->groups[i];
  252. }
  253. if (!group) {
  254. drm_dbg_kms(mgr->dev,
  255. "DPTUN: Can't allocate more tunnel groups\n");
  256. return NULL;
  257. }
  258. group->drv_group_id = drv_group_id;
  259. group->active = true;
  260. /*
  261. * The group name format here and elsewhere: Driver-ID:Group-ID:*
  262. * (* standing for all DP-Adapters/tunnels in the group).
  263. */
  264. snprintf(group->name, sizeof(group->name), "%d:%d:*",
  265. tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
  266. tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1));
  267. return group;
  268. }
  269. static void free_group(struct drm_dp_tunnel_group *group)
  270. {
  271. struct drm_dp_tunnel_mgr *mgr = group->mgr;
  272. if (drm_WARN_ON(mgr->dev, !list_empty(&group->tunnels)))
  273. return;
  274. group->drv_group_id = 0;
  275. group->available_bw = -1;
  276. group->active = false;
  277. }
  278. static struct drm_dp_tunnel *
  279. tunnel_get(struct drm_dp_tunnel *tunnel)
  280. {
  281. kref_get(&tunnel->kref);
  282. return tunnel;
  283. }
  284. static void free_tunnel(struct kref *kref)
  285. {
  286. struct drm_dp_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref);
  287. struct drm_dp_tunnel_group *group = tunnel->group;
  288. list_del(&tunnel->node);
  289. if (list_empty(&group->tunnels))
  290. free_group(group);
  291. kfree(tunnel);
  292. }
  293. static void tunnel_put(struct drm_dp_tunnel *tunnel)
  294. {
  295. kref_put(&tunnel->kref, free_tunnel);
  296. }
  297. #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
  298. static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
  299. struct ref_tracker **tracker)
  300. {
  301. ref_tracker_alloc(&tunnel->group->mgr->ref_tracker,
  302. tracker, GFP_KERNEL);
  303. }
  304. static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
  305. struct ref_tracker **tracker)
  306. {
  307. ref_tracker_free(&tunnel->group->mgr->ref_tracker,
  308. tracker);
  309. }
  310. #else
  311. static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
  312. struct ref_tracker **tracker)
  313. {
  314. }
  315. static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
  316. struct ref_tracker **tracker)
  317. {
  318. }
  319. #endif
  320. /**
  321. * drm_dp_tunnel_get - Get a reference for a DP tunnel
  322. * @tunnel: Tunnel object
  323. * @tracker: Debug tracker for the reference
  324. *
  325. * Get a reference for @tunnel, along with a debug tracker to help locating
  326. * the source of a reference leak/double reference put etc. issue.
  327. *
  328. * The reference must be dropped after use calling drm_dp_tunnel_put()
  329. * passing @tunnel and *@tracker returned from here.
  330. *
  331. * Returns @tunnel - as a convenience - along with *@tracker.
  332. */
  333. struct drm_dp_tunnel *
  334. drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel,
  335. struct ref_tracker **tracker)
  336. {
  337. track_tunnel_ref(tunnel, tracker);
  338. return tunnel_get(tunnel);
  339. }
  340. EXPORT_SYMBOL(drm_dp_tunnel_get);
  341. /**
  342. * drm_dp_tunnel_put - Put a reference for a DP tunnel
  343. * @tunnel: Tunnel object
  344. * @tracker: Debug tracker for the reference
  345. *
  346. * Put a reference for @tunnel along with its debug *@tracker, which
  347. * was obtained with drm_dp_tunnel_get().
  348. */
  349. void drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel,
  350. struct ref_tracker **tracker)
  351. {
  352. untrack_tunnel_ref(tunnel, tracker);
  353. tunnel_put(tunnel);
  354. }
  355. EXPORT_SYMBOL(drm_dp_tunnel_put);
  356. static bool add_tunnel_to_group(struct drm_dp_tunnel_mgr *mgr,
  357. u8 drv_group_id,
  358. struct drm_dp_tunnel *tunnel)
  359. {
  360. struct drm_dp_tunnel_group *group;
  361. group = lookup_or_alloc_group(mgr, drv_group_id);
  362. if (!group)
  363. return false;
  364. tunnel->group = group;
  365. list_add(&tunnel->node, &group->tunnels);
  366. return true;
  367. }
  368. static struct drm_dp_tunnel *
  369. create_tunnel(struct drm_dp_tunnel_mgr *mgr,
  370. struct drm_dp_aux *aux,
  371. const struct drm_dp_tunnel_regs *regs)
  372. {
  373. u8 drv_group_id = tunnel_reg_drv_group_id(regs);
  374. struct drm_dp_tunnel *tunnel;
  375. tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
  376. if (!tunnel)
  377. return NULL;
  378. INIT_LIST_HEAD(&tunnel->node);
  379. kref_init(&tunnel->kref);
  380. tunnel->aux = aux;
  381. tunnel->adapter_id = tunnel_reg(regs, DP_IN_ADAPTER_INFO) & DP_IN_ADAPTER_NUMBER_MASK;
  382. snprintf(tunnel->name, sizeof(tunnel->name), "%d:%d:%d",
  383. tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
  384. tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1),
  385. tunnel->adapter_id & ((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1));
  386. tunnel->bw_granularity = tunnel_reg_bw_granularity(regs);
  387. tunnel->allocated_bw = tunnel_reg(regs, DP_ALLOCATED_BW) *
  388. tunnel->bw_granularity;
  389. /*
  390. * An initial allocated BW of 0 indicates an undefined state: the
  391. * actual allocation is determined by the TBT CM, usually following a
  392. * legacy allocation policy (based on the max DPRX caps). From the
  393. * driver's POV the state becomes defined only after the first
  394. * allocation request.
  395. */
  396. if (!tunnel->allocated_bw)
  397. tunnel->allocated_bw = -1;
  398. tunnel->bw_alloc_supported = tunnel_reg_bw_alloc_supported(regs);
  399. tunnel->bw_alloc_enabled = tunnel_reg_bw_alloc_enabled(regs);
  400. if (!add_tunnel_to_group(mgr, drv_group_id, tunnel)) {
  401. kfree(tunnel);
  402. return NULL;
  403. }
  404. track_tunnel_ref(tunnel, &tunnel->tracker);
  405. return tunnel;
  406. }
  407. static void destroy_tunnel(struct drm_dp_tunnel *tunnel)
  408. {
  409. untrack_tunnel_ref(tunnel, &tunnel->tracker);
  410. tunnel_put(tunnel);
  411. }
  412. /**
  413. * drm_dp_tunnel_set_io_error - Set the IO error flag for a DP tunnel
  414. * @tunnel: Tunnel object
  415. *
  416. * Set the IO error flag for @tunnel. Drivers can call this function upon
  417. * detecting a failure that affects the tunnel functionality, for instance
  418. * after a DP AUX transfer failure on the port @tunnel is connected to.
  419. *
  420. * This disables further management of @tunnel, including any related
  421. * AUX accesses for tunneling DPCD registers, returning error to the
  422. * initiators of these. The driver is supposed to drop this tunnel and -
  423. * optionally - recreate it.
  424. */
  425. void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel)
  426. {
  427. tunnel->has_io_error = true;
  428. }
  429. EXPORT_SYMBOL(drm_dp_tunnel_set_io_error);
  430. #define SKIP_DPRX_CAPS_CHECK BIT(0)
  431. #define ALLOW_ALLOCATED_BW_CHANGE BIT(1)
  432. static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
  433. const struct drm_dp_tunnel_regs *regs,
  434. unsigned int flags)
  435. {
  436. u8 drv_group_id = tunnel_reg_drv_group_id(regs);
  437. bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
  438. bool ret = true;
  439. if (!tunnel_reg_bw_alloc_supported(regs)) {
  440. if (tunnel_group_id(drv_group_id)) {
  441. drm_dbg_kms(mgr->dev,
  442. "DPTUN: A non-zero group ID is only allowed with BWA support\n");
  443. ret = false;
  444. }
  445. if (tunnel_reg(regs, DP_ALLOCATED_BW)) {
  446. drm_dbg_kms(mgr->dev,
  447. "DPTUN: BW is allocated without BWA support\n");
  448. ret = false;
  449. }
  450. return ret;
  451. }
  452. if (!tunnel_group_id(drv_group_id)) {
  453. drm_dbg_kms(mgr->dev,
  454. "DPTUN: BWA support requires a non-zero group ID\n");
  455. ret = false;
  456. }
  457. if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {
  458. drm_dbg_kms(mgr->dev,
  459. "DPTUN: Invalid DPRX lane count: %d\n",
  460. tunnel_reg_max_dprx_lane_count(regs));
  461. ret = false;
  462. }
  463. if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
  464. drm_dbg_kms(mgr->dev,
  465. "DPTUN: DPRX rate is 0\n");
  466. ret = false;
  467. }
  468. if (tunnel_reg_bw_granularity(regs) < 0) {
  469. drm_dbg_kms(mgr->dev,
  470. "DPTUN: Invalid BW granularity\n");
  471. ret = false;
  472. }
  473. if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs, DP_ESTIMATED_BW)) {
  474. drm_dbg_kms(mgr->dev,
  475. "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n",
  476. DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) *
  477. tunnel_reg_bw_granularity(regs)),
  478. DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) *
  479. tunnel_reg_bw_granularity(regs)));
  480. ret = false;
  481. }
  482. return ret;
  483. }
  484. static int tunnel_allocated_bw(const struct drm_dp_tunnel *tunnel)
  485. {
  486. return max(tunnel->allocated_bw, 0);
  487. }
  488. static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel *tunnel,
  489. const struct drm_dp_tunnel_regs *regs,
  490. unsigned int flags)
  491. {
  492. u8 new_drv_group_id = tunnel_reg_drv_group_id(regs);
  493. bool ret = true;
  494. if (tunnel->bw_alloc_supported != tunnel_reg_bw_alloc_supported(regs)) {
  495. tun_dbg(tunnel,
  496. "BW alloc support has changed %s -> %s\n",
  497. str_yes_no(tunnel->bw_alloc_supported),
  498. str_yes_no(tunnel_reg_bw_alloc_supported(regs)));
  499. ret = false;
  500. }
  501. if (tunnel->group->drv_group_id != new_drv_group_id) {
  502. tun_dbg(tunnel,
  503. "Driver/group ID has changed %d:%d:* -> %d:%d:*\n",
  504. tunnel_group_drv_id(tunnel->group->drv_group_id),
  505. tunnel_group_id(tunnel->group->drv_group_id),
  506. tunnel_group_drv_id(new_drv_group_id),
  507. tunnel_group_id(new_drv_group_id));
  508. ret = false;
  509. }
  510. if (!tunnel->bw_alloc_supported)
  511. return ret;
  512. if (tunnel->bw_granularity != tunnel_reg_bw_granularity(regs)) {
  513. tun_dbg(tunnel,
  514. "BW granularity has changed: %d -> %d Mb/s\n",
  515. DPTUN_BW_ARG(tunnel->bw_granularity),
  516. DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs)));
  517. ret = false;
  518. }
  519. /*
  520. * On some devices at least the BW alloc mode enabled status is always
  521. * reported as 0, so skip checking that here.
  522. */
  523. if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) &&
  524. tunnel_allocated_bw(tunnel) !=
  525. tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity) {
  526. tun_dbg(tunnel,
  527. "Allocated BW has changed: %d -> %d Mb/s\n",
  528. DPTUN_BW_ARG(tunnel->allocated_bw),
  529. DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity));
  530. ret = false;
  531. }
  532. return ret;
  533. }
  534. static int
  535. read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
  536. struct drm_dp_tunnel_regs *regs,
  537. unsigned int flags)
  538. {
  539. int err;
  540. err = read_tunnel_regs(tunnel->aux, regs);
  541. if (err < 0) {
  542. drm_dp_tunnel_set_io_error(tunnel);
  543. return err;
  544. }
  545. if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
  546. return -EINVAL;
  547. if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
  548. return -EINVAL;
  549. return 0;
  550. }
  551. static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const struct drm_dp_tunnel_regs *regs)
  552. {
  553. bool changed = false;
  554. if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate) {
  555. tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs);
  556. changed = true;
  557. }
  558. if (tunnel_reg_max_dprx_lane_count(regs) != tunnel->max_dprx_lane_count) {
  559. tunnel->max_dprx_lane_count = tunnel_reg_max_dprx_lane_count(regs);
  560. changed = true;
  561. }
  562. return changed;
  563. }
  564. static int dev_id_len(const u8 *dev_id, int max_len)
  565. {
  566. while (max_len && dev_id[max_len - 1] == '\0')
  567. max_len--;
  568. return max_len;
  569. }
  570. static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
  571. {
  572. int max_dprx_bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
  573. tunnel->max_dprx_lane_count);
  574. /*
  575. * A BW request of roundup(max_dprx_bw, tunnel->bw_granularity) results in
  576. * an allocation of max_dprx_bw. A BW request above this rounded-up
  577. * value will fail.
  578. */
  579. return min(roundup(max_dprx_bw, tunnel->bw_granularity),
  580. MAX_DP_REQUEST_BW * tunnel->bw_granularity);
  581. }
  582. static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
  583. {
  584. return min(get_max_dprx_bw(tunnel), tunnel->group->available_bw);
  585. }
  586. /**
  587. * drm_dp_tunnel_detect - Detect DP tunnel on the link
  588. * @mgr: Tunnel manager
  589. * @aux: DP AUX on which the tunnel will be detected
  590. *
  591. * Detect if there is any DP tunnel on the link and add it to the tunnel
  592. * group's tunnel list.
  593. *
  594. * Returns a pointer to a tunnel on success, or an ERR_PTR() error on
  595. * failure.
  596. */
  597. struct drm_dp_tunnel *
  598. drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
  599. struct drm_dp_aux *aux)
  600. {
  601. struct drm_dp_tunnel_regs regs;
  602. struct drm_dp_tunnel *tunnel;
  603. int err;
  604. err = read_tunnel_regs(aux, &regs);
  605. if (err)
  606. return ERR_PTR(err);
  607. if (!(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
  608. DP_TUNNELING_SUPPORT))
  609. return ERR_PTR(-ENODEV);
  610. /* The DPRX caps are valid only after enabling BW alloc mode. */
  611. if (!tunnel_regs_are_valid(mgr, &regs, SKIP_DPRX_CAPS_CHECK))
  612. return ERR_PTR(-EINVAL);
  613. tunnel = create_tunnel(mgr, aux, &regs);
  614. if (!tunnel)
  615. return ERR_PTR(-ENOMEM);
  616. tun_dbg(tunnel,
  617. "OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%s BWA-Sup:%s BWA-En:%s\n",
  618. DP_TUNNELING_OUI_BYTES,
  619. tunnel_reg_ptr(&regs, DP_TUNNELING_OUI),
  620. dev_id_len(tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),
  621. tunnel_reg_ptr(&regs, DP_TUNNELING_DEV_ID),
  622. (tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >>
  623. DP_TUNNELING_HW_REV_MAJOR_SHIFT,
  624. (tunnel_reg(&regs, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >>
  625. DP_TUNNELING_HW_REV_MINOR_SHIFT,
  626. tunnel_reg(&regs, DP_TUNNELING_SW_REV_MAJOR),
  627. tunnel_reg(&regs, DP_TUNNELING_SW_REV_MINOR),
  628. str_yes_no(tunnel_reg(&regs, DP_TUNNELING_CAPABILITIES) &
  629. DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT),
  630. str_yes_no(tunnel->bw_alloc_supported),
  631. str_yes_no(tunnel->bw_alloc_enabled));
  632. return tunnel;
  633. }
  634. EXPORT_SYMBOL(drm_dp_tunnel_detect);
  635. /**
  636. * drm_dp_tunnel_destroy - Destroy tunnel object
  637. * @tunnel: Tunnel object
  638. *
  639. * Remove the tunnel from the tunnel topology and destroy it.
  640. *
  641. * Returns 0 on success, -ENODEV if the tunnel has been destroyed already.
  642. */
  643. int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
  644. {
  645. if (!tunnel)
  646. return 0;
  647. if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
  648. return -ENODEV;
  649. tun_dbg(tunnel, "destroying\n");
  650. tunnel->destroyed = true;
  651. destroy_tunnel(tunnel);
  652. return 0;
  653. }
  654. EXPORT_SYMBOL(drm_dp_tunnel_destroy);
  655. static int check_tunnel(const struct drm_dp_tunnel *tunnel)
  656. {
  657. if (tunnel->destroyed)
  658. return -ENODEV;
  659. if (tunnel->has_io_error)
  660. return -EIO;
  661. return 0;
  662. }
  663. static int group_allocated_bw(struct drm_dp_tunnel_group *group)
  664. {
  665. struct drm_dp_tunnel *tunnel;
  666. int group_allocated_bw = 0;
  667. for_each_tunnel_in_group(group, tunnel) {
  668. if (check_tunnel(tunnel) == 0 &&
  669. tunnel->bw_alloc_enabled)
  670. group_allocated_bw += tunnel_allocated_bw(tunnel);
  671. }
  672. return group_allocated_bw;
  673. }
  674. /*
  675. * The estimated BW reported by the TBT Connection Manager for each tunnel in
  676. * a group includes the BW already allocated for the given tunnel and the
  677. * unallocated BW which is free to be used by any tunnel in the group.
  678. */
  679. static int group_free_bw(const struct drm_dp_tunnel *tunnel)
  680. {
  681. return tunnel->estimated_bw - tunnel_allocated_bw(tunnel);
  682. }
  683. static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel)
  684. {
  685. return group_allocated_bw(tunnel->group) +
  686. group_free_bw(tunnel);
  687. }
  688. static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
  689. const struct drm_dp_tunnel_regs *regs)
  690. {
  691. struct drm_dp_tunnel *tunnel_iter;
  692. int group_available_bw;
  693. bool changed;
  694. tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity;
  695. if (calc_group_available_bw(tunnel) == tunnel->group->available_bw)
  696. return 0;
  697. for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
  698. int err;
  699. if (tunnel_iter == tunnel)
  700. continue;
  701. if (check_tunnel(tunnel_iter) != 0 ||
  702. !tunnel_iter->bw_alloc_enabled)
  703. continue;
  704. err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV);
  705. if (err) {
  706. tun_dbg(tunnel_iter,
  707. "Probe failed, assume disconnected (err %pe)\n",
  708. ERR_PTR(err));
  709. drm_dp_tunnel_set_io_error(tunnel_iter);
  710. }
  711. }
  712. group_available_bw = calc_group_available_bw(tunnel);
  713. tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
  714. DPTUN_BW_ARG(tunnel->group->available_bw),
  715. DPTUN_BW_ARG(group_available_bw));
  716. changed = tunnel->group->available_bw != group_available_bw;
  717. tunnel->group->available_bw = group_available_bw;
  718. return changed ? 1 : 0;
  719. }
  720. static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable)
  721. {
  722. u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
  723. u8 val;
  724. if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
  725. goto out_err;
  726. if (enable)
  727. val |= mask;
  728. else
  729. val &= ~mask;
  730. if (drm_dp_dpcd_writeb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
  731. goto out_err;
  732. tunnel->bw_alloc_enabled = enable;
  733. return 0;
  734. out_err:
  735. drm_dp_tunnel_set_io_error(tunnel);
  736. return -EIO;
  737. }
  738. /**
  739. * drm_dp_tunnel_enable_bw_alloc - Enable DP tunnel BW allocation mode
  740. * @tunnel: Tunnel object
  741. *
  742. * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
  743. *
  744. * Returns 0 in case of success, negative error code otherwise.
  745. */
  746. int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
  747. {
  748. struct drm_dp_tunnel_regs regs;
  749. int err;
  750. err = check_tunnel(tunnel);
  751. if (err)
  752. return err;
  753. if (!tunnel->bw_alloc_supported)
  754. return -EOPNOTSUPP;
  755. if (!tunnel_group_id(tunnel->group->drv_group_id))
  756. return -EINVAL;
  757. err = set_bw_alloc_mode(tunnel, true);
  758. if (err)
  759. goto out;
  760. /*
  761. * After a BWA disable/re-enable sequence the allocated BW can either
  762. * stay at its last requested value or, for instance after system
  763. * suspend/resume, TBT CM can reset back the allocation to the amount
  764. * allocated in the legacy/non-BWA mode. Accordingly allow for the
  765. * allocation to change wrt. the last SW state.
  766. */
  767. err = read_and_verify_tunnel_regs(tunnel, &regs,
  768. ALLOW_ALLOCATED_BW_CHANGE);
  769. if (err) {
  770. set_bw_alloc_mode(tunnel, false);
  771. goto out;
  772. }
  773. if (!tunnel->max_dprx_rate)
  774. update_dprx_caps(tunnel, &regs);
  775. if (tunnel->group->available_bw == -1) {
  776. err = update_group_available_bw(tunnel, &regs);
  777. if (err > 0)
  778. err = 0;
  779. }
  780. out:
  781. tun_dbg_stat(tunnel, err,
  782. "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s",
  783. tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
  784. DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
  785. DPTUN_BW_ARG(tunnel->group->available_bw));
  786. return err;
  787. }
  788. EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
  789. /**
  790. * drm_dp_tunnel_disable_bw_alloc - Disable DP tunnel BW allocation mode
  791. * @tunnel: Tunnel object
  792. *
  793. * Disable the DP tunnel BW allocation mode on @tunnel.
  794. *
  795. * Returns 0 in case of success, negative error code otherwise.
  796. */
  797. int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
  798. {
  799. int err;
  800. err = check_tunnel(tunnel);
  801. if (err)
  802. return err;
  803. tunnel->allocated_bw = -1;
  804. err = set_bw_alloc_mode(tunnel, false);
  805. tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
  806. return err;
  807. }
  808. EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
  809. /**
  810. * drm_dp_tunnel_bw_alloc_is_enabled - Query the BW allocation mode enabled state
  811. * @tunnel: Tunnel object
  812. *
  813. * Query if the BW allocation mode is enabled for @tunnel.
  814. *
  815. * Returns %true if the BW allocation mode is enabled for @tunnel.
  816. */
  817. bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
  818. {
  819. return tunnel && tunnel->bw_alloc_enabled;
  820. }
  821. EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
  822. static int clear_bw_req_state(struct drm_dp_aux *aux)
  823. {
  824. u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
  825. if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, bw_req_mask) < 0)
  826. return -EIO;
  827. return 0;
  828. }
  829. static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed)
  830. {
  831. u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
  832. u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
  833. u8 val;
  834. int err;
  835. if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
  836. return -EIO;
  837. *status_changed = val & status_change_mask;
  838. val &= bw_req_mask;
  839. if (!val)
  840. return -EAGAIN;
  841. err = clear_bw_req_state(aux);
  842. if (err < 0)
  843. return err;
  844. return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
  845. }
  846. static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
  847. {
  848. struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
  849. int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
  850. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  851. long timeout;
  852. int err;
  853. if (bw < 0) {
  854. err = -EINVAL;
  855. goto out;
  856. }
  857. if (request_bw * tunnel->bw_granularity == tunnel->allocated_bw)
  858. return 0;
  859. /* Atomic check should prevent the following. */
  860. if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
  861. err = -EINVAL;
  862. goto out;
  863. }
  864. err = clear_bw_req_state(tunnel->aux);
  865. if (err)
  866. goto out;
  867. if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) {
  868. err = -EIO;
  869. goto out;
  870. }
  871. timeout = msecs_to_jiffies(3000);
  872. add_wait_queue(&mgr->bw_req_queue, &wait);
  873. for (;;) {
  874. bool status_changed;
  875. err = bw_req_complete(tunnel->aux, &status_changed);
  876. if (err != -EAGAIN)
  877. break;
  878. if (status_changed) {
  879. struct drm_dp_tunnel_regs regs;
  880. err = read_and_verify_tunnel_regs(tunnel, &regs,
  881. ALLOW_ALLOCATED_BW_CHANGE);
  882. if (err)
  883. break;
  884. }
  885. if (!timeout) {
  886. err = -ETIMEDOUT;
  887. break;
  888. }
  889. timeout = wait_woken(&wait, TASK_UNINTERRUPTIBLE, timeout);
  890. };
  891. remove_wait_queue(&mgr->bw_req_queue, &wait);
  892. if (err)
  893. goto out;
  894. tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
  895. out:
  896. tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s",
  897. DPTUN_BW_ARG(request_bw * tunnel->bw_granularity),
  898. DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
  899. DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
  900. DPTUN_BW_ARG(tunnel->group->available_bw));
  901. if (err == -EIO)
  902. drm_dp_tunnel_set_io_error(tunnel);
  903. return err;
  904. }
  905. /**
  906. * drm_dp_tunnel_alloc_bw - Allocate BW for a DP tunnel
  907. * @tunnel: Tunnel object
  908. * @bw: BW in kB/s units
  909. *
  910. * Allocate @bw kB/s for @tunnel. The allocated BW must be freed after use by
  911. * calling this function for the same tunnel setting @bw to 0.
  912. *
  913. * Returns 0 in case of success, a negative error code otherwise.
  914. */
  915. int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
  916. {
  917. int err;
  918. err = check_tunnel(tunnel);
  919. if (err)
  920. return err;
  921. return allocate_tunnel_bw(tunnel, bw);
  922. }
  923. EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
  924. /**
  925. * drm_dp_tunnel_get_allocated_bw - Get the BW allocated for a DP tunnel
  926. * @tunnel: Tunnel object
  927. *
  928. * Get the current BW allocated for @tunnel. After the tunnel is created /
  929. * resumed and the BW allocation mode is enabled for it, the allocation
  930. * becomes determined only after the first allocation request by the driver
  931. * calling drm_dp_tunnel_alloc_bw().
  932. *
  933. * Return the BW allocated for the tunnel, or -1 if the allocation is
  934. * undetermined.
  935. */
  936. int drm_dp_tunnel_get_allocated_bw(struct drm_dp_tunnel *tunnel)
  937. {
  938. return tunnel->allocated_bw;
  939. }
  940. EXPORT_SYMBOL(drm_dp_tunnel_get_allocated_bw);
  941. /*
  942. * Return 0 if the status hasn't changed, 1 if the status has changed, a
  943. * negative error code in case of an I/O failure.
  944. */
  945. static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel)
  946. {
  947. u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
  948. u8 val;
  949. if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
  950. goto out_err;
  951. val &= mask;
  952. if (val) {
  953. if (drm_dp_dpcd_writeb(tunnel->aux, DP_TUNNELING_STATUS, val) < 0)
  954. goto out_err;
  955. return 1;
  956. }
  957. if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
  958. return 0;
  959. /*
  960. * Check for estimated BW changes explicitly to account for lost
  961. * BW change notifications.
  962. */
  963. if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) < 0)
  964. goto out_err;
  965. if (val * tunnel->bw_granularity != tunnel->estimated_bw)
  966. return 1;
  967. return 0;
  968. out_err:
  969. drm_dp_tunnel_set_io_error(tunnel);
  970. return -EIO;
  971. }
  972. /**
  973. * drm_dp_tunnel_update_state - Update DP tunnel SW state with the HW state
  974. * @tunnel: Tunnel object
  975. *
  976. * Update the SW state of @tunnel with the HW state.
  977. *
  978. * Returns 0 if the state has not changed, 1 if it has changed and got updated
  979. * successfully and a negative error code otherwise.
  980. */
  981. int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
  982. {
  983. struct drm_dp_tunnel_regs regs;
  984. bool changed = false;
  985. int ret;
  986. ret = check_tunnel(tunnel);
  987. if (ret < 0)
  988. return ret;
  989. ret = check_and_clear_status_change(tunnel);
  990. if (ret < 0)
  991. goto out;
  992. if (!ret)
  993. return 0;
  994. ret = read_and_verify_tunnel_regs(tunnel, &regs, 0);
  995. if (ret)
  996. goto out;
  997. if (update_dprx_caps(tunnel, &regs))
  998. changed = true;
  999. ret = update_group_available_bw(tunnel, &regs);
  1000. if (ret == 1)
  1001. changed = true;
  1002. out:
  1003. tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
  1004. "State update: Changed:%s DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s",
  1005. str_yes_no(changed),
  1006. tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
  1007. DPTUN_BW_ARG(tunnel->allocated_bw),
  1008. DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
  1009. DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
  1010. DPTUN_BW_ARG(tunnel->group->available_bw));
  1011. if (ret < 0)
  1012. return ret;
  1013. if (changed)
  1014. return 1;
  1015. return 0;
  1016. }
  1017. EXPORT_SYMBOL(drm_dp_tunnel_update_state);
  1018. /*
  1019. * drm_dp_tunnel_handle_irq - Handle DP tunnel IRQs
  1020. *
  1021. * Handle any pending DP tunnel IRQs, waking up waiters for a completion
  1022. * event.
  1023. *
  1024. * Returns 1 if the state of the tunnel has changed which requires calling
  1025. * drm_dp_tunnel_update_state(), a negative error code in case of a failure,
  1026. * 0 otherwise.
  1027. */
  1028. int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux)
  1029. {
  1030. u8 val;
  1031. if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
  1032. return -EIO;
  1033. if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
  1034. wake_up_all(&mgr->bw_req_queue);
  1035. if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED))
  1036. return 1;
  1037. return 0;
  1038. }
  1039. EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
  1040. /**
  1041. * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX
  1042. * @tunnel: Tunnel object
  1043. *
  1044. * The function is used to query the maximum link rate of the DPRX connected
  1045. * to @tunnel. Note that this rate will not be limited by the BW limit of the
  1046. * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
  1047. * registers.
  1048. *
  1049. * Returns the maximum link rate in 10 kbit/s units.
  1050. */
  1051. int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
  1052. {
  1053. return tunnel->max_dprx_rate;
  1054. }
  1055. EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
  1056. /**
  1057. * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX
  1058. * @tunnel: Tunnel object
  1059. *
  1060. * The function is used to query the maximum lane count of the DPRX connected
  1061. * to @tunnel. Note that this lane count will not be limited by the BW limit of
  1062. * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
  1063. * registers.
  1064. *
  1065. * Returns the maximum lane count.
  1066. */
  1067. int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
  1068. {
  1069. return tunnel->max_dprx_lane_count;
  1070. }
  1071. EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
  1072. /**
  1073. * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel
  1074. * @tunnel: Tunnel object
  1075. *
  1076. * This function is used to query the estimated total available BW of the
  1077. * tunnel. This includes the currently allocated and free BW for all the
  1078. * tunnels in @tunnel's group. The available BW is valid only after the BW
  1079. * allocation mode has been enabled for the tunnel and its state got updated
  1080. * calling drm_dp_tunnel_update_state().
  1081. *
  1082. * Returns the @tunnel group's estimated total available bandwidth in kB/s
  1083. * units, or -1 if the available BW isn't valid (the BW allocation mode is
  1084. * not enabled or the tunnel's state hasn't been updated).
  1085. */
  1086. int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
  1087. {
  1088. return tunnel->group->available_bw;
  1089. }
  1090. EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
  1091. static struct drm_dp_tunnel_group_state *
  1092. drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
  1093. const struct drm_dp_tunnel *tunnel)
  1094. {
  1095. return (struct drm_dp_tunnel_group_state *)
  1096. drm_atomic_get_private_obj_state(state,
  1097. &tunnel->group->base);
  1098. }
  1099. static struct drm_dp_tunnel_state *
  1100. add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
  1101. struct drm_dp_tunnel *tunnel)
  1102. {
  1103. struct drm_dp_tunnel_state *tunnel_state;
  1104. tun_dbg_atomic(tunnel,
  1105. "Adding state for tunnel %p to group state %p\n",
  1106. tunnel, group_state);
  1107. tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
  1108. if (!tunnel_state)
  1109. return NULL;
  1110. tunnel_state->group_state = group_state;
  1111. drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
  1112. INIT_LIST_HEAD(&tunnel_state->node);
  1113. list_add(&tunnel_state->node, &group_state->tunnel_states);
  1114. return tunnel_state;
  1115. }
  1116. static void free_tunnel_state(struct drm_dp_tunnel_state *tunnel_state)
  1117. {
  1118. tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
  1119. "Freeing state for tunnel %p\n",
  1120. tunnel_state->tunnel_ref.tunnel);
  1121. list_del(&tunnel_state->node);
  1122. kfree(tunnel_state->stream_bw);
  1123. drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
  1124. kfree(tunnel_state);
  1125. }
  1126. static void free_group_state(struct drm_dp_tunnel_group_state *group_state)
  1127. {
  1128. struct drm_dp_tunnel_state *tunnel_state;
  1129. struct drm_dp_tunnel_state *tunnel_state_tmp;
  1130. for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp)
  1131. free_tunnel_state(tunnel_state);
  1132. kfree(group_state);
  1133. }
  1134. static struct drm_dp_tunnel_state *
  1135. get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
  1136. const struct drm_dp_tunnel *tunnel)
  1137. {
  1138. struct drm_dp_tunnel_state *tunnel_state;
  1139. for_each_tunnel_state(group_state, tunnel_state)
  1140. if (tunnel_state->tunnel_ref.tunnel == tunnel)
  1141. return tunnel_state;
  1142. return NULL;
  1143. }
  1144. static struct drm_dp_tunnel_state *
  1145. get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
  1146. struct drm_dp_tunnel *tunnel)
  1147. {
  1148. struct drm_dp_tunnel_state *tunnel_state;
  1149. tunnel_state = get_tunnel_state(group_state, tunnel);
  1150. if (tunnel_state)
  1151. return tunnel_state;
  1152. return add_tunnel_state(group_state, tunnel);
  1153. }
  1154. static struct drm_private_state *
  1155. tunnel_group_duplicate_state(struct drm_private_obj *obj)
  1156. {
  1157. struct drm_dp_tunnel_group_state *group_state;
  1158. struct drm_dp_tunnel_state *tunnel_state;
  1159. group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
  1160. if (!group_state)
  1161. return NULL;
  1162. INIT_LIST_HEAD(&group_state->tunnel_states);
  1163. __drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base);
  1164. for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
  1165. struct drm_dp_tunnel_state *new_tunnel_state;
  1166. new_tunnel_state = get_or_add_tunnel_state(group_state,
  1167. tunnel_state->tunnel_ref.tunnel);
  1168. if (!new_tunnel_state)
  1169. goto out_free_state;
  1170. new_tunnel_state->stream_mask = tunnel_state->stream_mask;
  1171. new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw,
  1172. sizeof(*tunnel_state->stream_bw) *
  1173. hweight32(tunnel_state->stream_mask),
  1174. GFP_KERNEL);
  1175. if (!new_tunnel_state->stream_bw)
  1176. goto out_free_state;
  1177. }
  1178. return &group_state->base;
  1179. out_free_state:
  1180. free_group_state(group_state);
  1181. return NULL;
  1182. }
  1183. static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state)
  1184. {
  1185. free_group_state(to_group_state(state));
  1186. }
  1187. static const struct drm_private_state_funcs tunnel_group_funcs = {
  1188. .atomic_duplicate_state = tunnel_group_duplicate_state,
  1189. .atomic_destroy_state = tunnel_group_destroy_state,
  1190. };
  1191. /**
  1192. * drm_dp_tunnel_atomic_get_state - get/allocate the new atomic state for a tunnel
  1193. * @state: Atomic state
  1194. * @tunnel: Tunnel to get the state for
  1195. *
  1196. * Get the new atomic state for @tunnel, duplicating it from the old tunnel
  1197. * state if not yet allocated.
  1198. *
  1199. * Return the state or an ERR_PTR() error on failure.
  1200. */
  1201. struct drm_dp_tunnel_state *
  1202. drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
  1203. struct drm_dp_tunnel *tunnel)
  1204. {
  1205. struct drm_dp_tunnel_group_state *group_state;
  1206. struct drm_dp_tunnel_state *tunnel_state;
  1207. group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel);
  1208. if (IS_ERR(group_state))
  1209. return ERR_CAST(group_state);
  1210. tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
  1211. if (!tunnel_state)
  1212. return ERR_PTR(-ENOMEM);
  1213. return tunnel_state;
  1214. }
  1215. EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
  1216. /**
  1217. * drm_dp_tunnel_atomic_get_old_state - get the old atomic state for a tunnel
  1218. * @state: Atomic state
  1219. * @tunnel: Tunnel to get the state for
  1220. *
  1221. * Get the old atomic state for @tunnel.
  1222. *
  1223. * Return the old state or NULL if the tunnel's atomic state is not in @state.
  1224. */
  1225. struct drm_dp_tunnel_state *
  1226. drm_dp_tunnel_atomic_get_old_state(struct drm_atomic_state *state,
  1227. const struct drm_dp_tunnel *tunnel)
  1228. {
  1229. struct drm_dp_tunnel_group_state *old_group_state;
  1230. int i;
  1231. for_each_old_group_in_state(state, old_group_state, i)
  1232. if (to_group(old_group_state->base.obj) == tunnel->group)
  1233. return get_tunnel_state(old_group_state, tunnel);
  1234. return NULL;
  1235. }
  1236. EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_old_state);
  1237. /**
  1238. * drm_dp_tunnel_atomic_get_new_state - get the new atomic state for a tunnel
  1239. * @state: Atomic state
  1240. * @tunnel: Tunnel to get the state for
  1241. *
  1242. * Get the new atomic state for @tunnel.
  1243. *
  1244. * Return the new state or NULL if the tunnel's atomic state is not in @state.
  1245. */
  1246. struct drm_dp_tunnel_state *
  1247. drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
  1248. const struct drm_dp_tunnel *tunnel)
  1249. {
  1250. struct drm_dp_tunnel_group_state *new_group_state;
  1251. int i;
  1252. for_each_new_group_in_state(state, new_group_state, i)
  1253. if (to_group(new_group_state->base.obj) == tunnel->group)
  1254. return get_tunnel_state(new_group_state, tunnel);
  1255. return NULL;
  1256. }
  1257. EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
  1258. static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
  1259. {
  1260. struct drm_dp_tunnel_group_state *group_state;
  1261. group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
  1262. if (!group_state)
  1263. return false;
  1264. INIT_LIST_HEAD(&group_state->tunnel_states);
  1265. group->mgr = mgr;
  1266. group->available_bw = -1;
  1267. INIT_LIST_HEAD(&group->tunnels);
  1268. drm_atomic_private_obj_init(mgr->dev, &group->base, &group_state->base,
  1269. &tunnel_group_funcs);
  1270. return true;
  1271. }
  1272. static void cleanup_group(struct drm_dp_tunnel_group *group)
  1273. {
  1274. drm_atomic_private_obj_fini(&group->base);
  1275. }
  1276. #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
  1277. static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
  1278. {
  1279. const struct drm_dp_tunnel_state *tunnel_state;
  1280. u32 stream_mask = 0;
  1281. for_each_tunnel_state(group_state, tunnel_state) {
  1282. drm_WARN(to_group(group_state->base.obj)->mgr->dev,
  1283. tunnel_state->stream_mask & stream_mask,
  1284. "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n",
  1285. tunnel_state->tunnel_ref.tunnel->name,
  1286. tunnel_state->stream_mask,
  1287. stream_mask);
  1288. stream_mask |= tunnel_state->stream_mask;
  1289. }
  1290. }
  1291. #else
  1292. static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
  1293. {
  1294. }
  1295. #endif
  1296. static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
  1297. {
  1298. return hweight32(stream_mask & (BIT(stream_id) - 1));
  1299. }
  1300. static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
  1301. unsigned long old_mask, unsigned long new_mask)
  1302. {
  1303. unsigned long move_mask = old_mask & new_mask;
  1304. int *new_bws = NULL;
  1305. int id;
  1306. WARN_ON(!new_mask);
  1307. if (old_mask == new_mask)
  1308. return 0;
  1309. new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws), GFP_KERNEL);
  1310. if (!new_bws)
  1311. return -ENOMEM;
  1312. for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
  1313. new_bws[stream_id_to_idx(new_mask, id)] =
  1314. tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)];
  1315. kfree(tunnel_state->stream_bw);
  1316. tunnel_state->stream_bw = new_bws;
  1317. tunnel_state->stream_mask = new_mask;
  1318. return 0;
  1319. }
  1320. static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
  1321. u8 stream_id, int bw)
  1322. {
  1323. int err;
  1324. err = resize_bw_array(tunnel_state,
  1325. tunnel_state->stream_mask,
  1326. tunnel_state->stream_mask | BIT(stream_id));
  1327. if (err)
  1328. return err;
  1329. tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw;
  1330. return 0;
  1331. }
  1332. static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
  1333. u8 stream_id)
  1334. {
  1335. if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
  1336. free_tunnel_state(tunnel_state);
  1337. return 0;
  1338. }
  1339. return resize_bw_array(tunnel_state,
  1340. tunnel_state->stream_mask,
  1341. tunnel_state->stream_mask & ~BIT(stream_id));
  1342. }
  1343. /**
  1344. * drm_dp_tunnel_atomic_set_stream_bw - Set the BW for a DP tunnel stream
  1345. * @state: Atomic state
  1346. * @tunnel: DP tunnel containing the stream
  1347. * @stream_id: Stream ID
  1348. * @bw: BW of the stream
  1349. *
  1350. * Set a DP tunnel stream's required BW in the atomic state.
  1351. *
  1352. * Returns 0 in case of success, a negative error code otherwise.
  1353. */
  1354. int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
  1355. struct drm_dp_tunnel *tunnel,
  1356. u8 stream_id, int bw)
  1357. {
  1358. struct drm_dp_tunnel_group_state *new_group_state;
  1359. struct drm_dp_tunnel_state *tunnel_state;
  1360. int err;
  1361. if (drm_WARN_ON(tunnel->group->mgr->dev,
  1362. stream_id > BITS_PER_TYPE(tunnel_state->stream_mask)))
  1363. return -EINVAL;
  1364. tun_dbg(tunnel,
  1365. "Setting %d Mb/s for stream %d\n",
  1366. DPTUN_BW_ARG(bw), stream_id);
  1367. new_group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel);
  1368. if (IS_ERR(new_group_state))
  1369. return PTR_ERR(new_group_state);
  1370. if (bw == 0) {
  1371. tunnel_state = get_tunnel_state(new_group_state, tunnel);
  1372. if (!tunnel_state)
  1373. return 0;
  1374. return clear_stream_bw(tunnel_state, stream_id);
  1375. }
  1376. tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel);
  1377. if (drm_WARN_ON(state->dev, !tunnel_state))
  1378. return -EINVAL;
  1379. err = set_stream_bw(tunnel_state, stream_id, bw);
  1380. if (err)
  1381. return err;
  1382. check_unique_stream_ids(new_group_state);
  1383. return 0;
  1384. }
  1385. EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
  1386. /**
  1387. * drm_dp_tunnel_atomic_get_required_bw - Get the BW required by a DP tunnel
  1388. * @tunnel_state: Atomic state of the queried tunnel
  1389. *
  1390. * Calculate the BW required by a tunnel adding up the required BW of all
  1391. * the streams in the tunnel.
  1392. *
  1393. * Return the total BW required by the tunnel.
  1394. */
  1395. int drm_dp_tunnel_atomic_get_required_bw(const struct drm_dp_tunnel_state *tunnel_state)
  1396. {
  1397. int tunnel_bw = 0;
  1398. int i;
  1399. if (!tunnel_state || !tunnel_state->stream_mask)
  1400. return 0;
  1401. for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
  1402. tunnel_bw += tunnel_state->stream_bw[i];
  1403. return tunnel_bw;
  1404. }
  1405. EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_required_bw);
  1406. /**
  1407. * drm_dp_tunnel_atomic_get_group_streams_in_state - Get mask of stream IDs in a group
  1408. * @state: Atomic state
  1409. * @tunnel: Tunnel object
  1410. * @stream_mask: Mask of streams in @tunnel's group
  1411. *
  1412. * Get the mask of all the stream IDs in the tunnel group of @tunnel.
  1413. *
  1414. * Return 0 in case of success - with the stream IDs in @stream_mask - or a
  1415. * negative error code in case of failure.
  1416. */
  1417. int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
  1418. const struct drm_dp_tunnel *tunnel,
  1419. u32 *stream_mask)
  1420. {
  1421. struct drm_dp_tunnel_group_state *group_state;
  1422. struct drm_dp_tunnel_state *tunnel_state;
  1423. group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel);
  1424. if (IS_ERR(group_state))
  1425. return PTR_ERR(group_state);
  1426. *stream_mask = 0;
  1427. for_each_tunnel_state(group_state, tunnel_state)
  1428. *stream_mask |= tunnel_state->stream_mask;
  1429. return 0;
  1430. }
  1431. EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
  1432. static int
  1433. drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
  1434. u32 *failed_stream_mask)
  1435. {
  1436. struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
  1437. struct drm_dp_tunnel_state *new_tunnel_state;
  1438. u32 group_stream_mask = 0;
  1439. int group_bw = 0;
  1440. for_each_tunnel_state(new_group_state, new_tunnel_state) {
  1441. struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;
  1442. int max_dprx_bw = get_max_dprx_bw(tunnel);
  1443. int tunnel_bw = drm_dp_tunnel_atomic_get_required_bw(new_tunnel_state);
  1444. tun_dbg(tunnel,
  1445. "%sRequired %d/%d Mb/s total for tunnel.\n",
  1446. tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
  1447. DPTUN_BW_ARG(tunnel_bw),
  1448. DPTUN_BW_ARG(max_dprx_bw));
  1449. if (tunnel_bw > max_dprx_bw) {
  1450. *failed_stream_mask = new_tunnel_state->stream_mask;
  1451. return -ENOSPC;
  1452. }
  1453. group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
  1454. max_dprx_bw);
  1455. group_stream_mask |= new_tunnel_state->stream_mask;
  1456. }
  1457. tun_grp_dbg(group,
  1458. "%sRequired %d/%d Mb/s total for tunnel group.\n",
  1459. group_bw > group->available_bw ? "Not enough BW: " : "",
  1460. DPTUN_BW_ARG(group_bw),
  1461. DPTUN_BW_ARG(group->available_bw));
  1462. if (group_bw > group->available_bw) {
  1463. *failed_stream_mask = group_stream_mask;
  1464. return -ENOSPC;
  1465. }
  1466. return 0;
  1467. }
  1468. /**
  1469. * drm_dp_tunnel_atomic_check_stream_bws - Check BW limit for all streams in state
  1470. * @state: Atomic state
  1471. * @failed_stream_mask: Mask of stream IDs with a BW limit failure
  1472. *
  1473. * Check the required BW of each DP tunnel in @state against both the DPRX BW
  1474. * limit of the tunnel and the BW limit of the tunnel group. Return a mask of
  1475. * stream IDs in @failed_stream_mask once a check fails. The mask will contain
  1476. * either all the streams in a tunnel (in case a DPRX BW limit check failed) or
  1477. * all the streams in a tunnel group (in case a group BW limit check failed).
  1478. *
  1479. * Return 0 if all the BW limit checks passed, -ENOSPC in case a BW limit
  1480. * check failed - with @failed_stream_mask containing the streams failing the
  1481. * check - or a negative error code otherwise.
  1482. */
  1483. int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
  1484. u32 *failed_stream_mask)
  1485. {
  1486. struct drm_dp_tunnel_group_state *new_group_state;
  1487. int i;
  1488. for_each_new_group_in_state(state, new_group_state, i) {
  1489. int ret;
  1490. ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state,
  1491. failed_stream_mask);
  1492. if (ret)
  1493. return ret;
  1494. }
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
  1498. static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
  1499. {
  1500. int i;
  1501. for (i = 0; i < mgr->group_count; i++) {
  1502. cleanup_group(&mgr->groups[i]);
  1503. drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels));
  1504. }
  1505. #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
  1506. ref_tracker_dir_exit(&mgr->ref_tracker);
  1507. #endif
  1508. kfree(mgr->groups);
  1509. kfree(mgr);
  1510. }
  1511. /**
  1512. * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
  1513. * @dev: DRM device object
  1514. * @max_group_count: Maximum number of tunnel groups
  1515. *
  1516. * Creates a DP tunnel manager for @dev.
  1517. *
  1518. * Returns a pointer to the tunnel manager if created successfully or error
  1519. * pointer in case of failure.
  1520. */
  1521. struct drm_dp_tunnel_mgr *
  1522. drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
  1523. {
  1524. struct drm_dp_tunnel_mgr *mgr;
  1525. int i;
  1526. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  1527. if (!mgr)
  1528. return ERR_PTR(-ENOMEM);
  1529. mgr->dev = dev;
  1530. init_waitqueue_head(&mgr->bw_req_queue);
  1531. mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups), GFP_KERNEL);
  1532. if (!mgr->groups) {
  1533. kfree(mgr);
  1534. return ERR_PTR(-ENOMEM);
  1535. }
  1536. #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
  1537. ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
  1538. #endif
  1539. for (i = 0; i < max_group_count; i++) {
  1540. if (!init_group(mgr, &mgr->groups[i])) {
  1541. destroy_mgr(mgr);
  1542. return ERR_PTR(-ENOMEM);
  1543. }
  1544. mgr->group_count++;
  1545. }
  1546. return mgr;
  1547. }
  1548. EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
  1549. /**
  1550. * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
  1551. * @mgr: Tunnel manager object
  1552. *
  1553. * Destroy the tunnel manager.
  1554. */
  1555. void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
  1556. {
  1557. destroy_mgr(mgr);
  1558. }
  1559. EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);