drm_dp_mst_topology.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  1. /*
  2. * Copyright © 2014 Red Hat
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/sched.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/i2c.h>
  29. #include <drm/drm_dp_mst_helper.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm_fixed.h>
  32. /**
  33. * DOC: dp mst helper
  34. *
  35. * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
  36. * protocol. The helpers contain a topology manager and bandwidth manager.
  37. * The helpers encapsulate the sending and received of sideband msgs.
  38. */
  39. static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
  40. char *buf);
  41. static int test_calc_pbn_mode(void);
  42. static void drm_dp_put_port(struct drm_dp_mst_port *port);
  43. static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
  44. int id,
  45. struct drm_dp_payload *payload);
  46. static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
  47. struct drm_dp_mst_port *port,
  48. int offset, int size, u8 *bytes);
  49. static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  50. struct drm_dp_mst_branch *mstb);
  51. static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
  52. struct drm_dp_mst_branch *mstb,
  53. struct drm_dp_mst_port *port);
  54. static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
  55. u8 *guid);
  56. static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
  57. static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
  58. static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
  59. /* sideband msg handling */
  60. static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
  61. {
  62. u8 bitmask = 0x80;
  63. u8 bitshift = 7;
  64. u8 array_index = 0;
  65. int number_of_bits = num_nibbles * 4;
  66. u8 remainder = 0;
  67. while (number_of_bits != 0) {
  68. number_of_bits--;
  69. remainder <<= 1;
  70. remainder |= (data[array_index] & bitmask) >> bitshift;
  71. bitmask >>= 1;
  72. bitshift--;
  73. if (bitmask == 0) {
  74. bitmask = 0x80;
  75. bitshift = 7;
  76. array_index++;
  77. }
  78. if ((remainder & 0x10) == 0x10)
  79. remainder ^= 0x13;
  80. }
  81. number_of_bits = 4;
  82. while (number_of_bits != 0) {
  83. number_of_bits--;
  84. remainder <<= 1;
  85. if ((remainder & 0x10) != 0)
  86. remainder ^= 0x13;
  87. }
  88. return remainder;
  89. }
  90. static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
  91. {
  92. u8 bitmask = 0x80;
  93. u8 bitshift = 7;
  94. u8 array_index = 0;
  95. int number_of_bits = number_of_bytes * 8;
  96. u16 remainder = 0;
  97. while (number_of_bits != 0) {
  98. number_of_bits--;
  99. remainder <<= 1;
  100. remainder |= (data[array_index] & bitmask) >> bitshift;
  101. bitmask >>= 1;
  102. bitshift--;
  103. if (bitmask == 0) {
  104. bitmask = 0x80;
  105. bitshift = 7;
  106. array_index++;
  107. }
  108. if ((remainder & 0x100) == 0x100)
  109. remainder ^= 0xd5;
  110. }
  111. number_of_bits = 8;
  112. while (number_of_bits != 0) {
  113. number_of_bits--;
  114. remainder <<= 1;
  115. if ((remainder & 0x100) != 0)
  116. remainder ^= 0xd5;
  117. }
  118. return remainder & 0xff;
  119. }
  120. static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
  121. {
  122. u8 size = 3;
  123. size += (hdr->lct / 2);
  124. return size;
  125. }
  126. static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
  127. u8 *buf, int *len)
  128. {
  129. int idx = 0;
  130. int i;
  131. u8 crc4;
  132. buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
  133. for (i = 0; i < (hdr->lct / 2); i++)
  134. buf[idx++] = hdr->rad[i];
  135. buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
  136. (hdr->msg_len & 0x3f);
  137. buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
  138. crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
  139. buf[idx - 1] |= (crc4 & 0xf);
  140. *len = idx;
  141. }
  142. static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
  143. u8 *buf, int buflen, u8 *hdrlen)
  144. {
  145. u8 crc4;
  146. u8 len;
  147. int i;
  148. u8 idx;
  149. if (buf[0] == 0)
  150. return false;
  151. len = 3;
  152. len += ((buf[0] & 0xf0) >> 4) / 2;
  153. if (len > buflen)
  154. return false;
  155. crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
  156. if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
  157. DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
  158. return false;
  159. }
  160. hdr->lct = (buf[0] & 0xf0) >> 4;
  161. hdr->lcr = (buf[0] & 0xf);
  162. idx = 1;
  163. for (i = 0; i < (hdr->lct / 2); i++)
  164. hdr->rad[i] = buf[idx++];
  165. hdr->broadcast = (buf[idx] >> 7) & 0x1;
  166. hdr->path_msg = (buf[idx] >> 6) & 0x1;
  167. hdr->msg_len = buf[idx] & 0x3f;
  168. idx++;
  169. hdr->somt = (buf[idx] >> 7) & 0x1;
  170. hdr->eomt = (buf[idx] >> 6) & 0x1;
  171. hdr->seqno = (buf[idx] >> 4) & 0x1;
  172. idx++;
  173. *hdrlen = idx;
  174. return true;
  175. }
  176. static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
  177. struct drm_dp_sideband_msg_tx *raw)
  178. {
  179. int idx = 0;
  180. int i;
  181. u8 *buf = raw->msg;
  182. buf[idx++] = req->req_type & 0x7f;
  183. switch (req->req_type) {
  184. case DP_ENUM_PATH_RESOURCES:
  185. buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
  186. idx++;
  187. break;
  188. case DP_ALLOCATE_PAYLOAD:
  189. buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
  190. (req->u.allocate_payload.number_sdp_streams & 0xf);
  191. idx++;
  192. buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
  193. idx++;
  194. buf[idx] = (req->u.allocate_payload.pbn >> 8);
  195. idx++;
  196. buf[idx] = (req->u.allocate_payload.pbn & 0xff);
  197. idx++;
  198. for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
  199. buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
  200. (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
  201. idx++;
  202. }
  203. if (req->u.allocate_payload.number_sdp_streams & 1) {
  204. i = req->u.allocate_payload.number_sdp_streams - 1;
  205. buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
  206. idx++;
  207. }
  208. break;
  209. case DP_QUERY_PAYLOAD:
  210. buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
  211. idx++;
  212. buf[idx] = (req->u.query_payload.vcpi & 0x7f);
  213. idx++;
  214. break;
  215. case DP_REMOTE_DPCD_READ:
  216. buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
  217. buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
  218. idx++;
  219. buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
  220. idx++;
  221. buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
  222. idx++;
  223. buf[idx] = (req->u.dpcd_read.num_bytes);
  224. idx++;
  225. break;
  226. case DP_REMOTE_DPCD_WRITE:
  227. buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
  228. buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
  229. idx++;
  230. buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
  231. idx++;
  232. buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
  233. idx++;
  234. buf[idx] = (req->u.dpcd_write.num_bytes);
  235. idx++;
  236. memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
  237. idx += req->u.dpcd_write.num_bytes;
  238. break;
  239. case DP_REMOTE_I2C_READ:
  240. buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
  241. buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
  242. idx++;
  243. for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
  244. buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
  245. idx++;
  246. buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
  247. idx++;
  248. memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
  249. idx += req->u.i2c_read.transactions[i].num_bytes;
  250. buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
  251. buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
  252. idx++;
  253. }
  254. buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
  255. idx++;
  256. buf[idx] = (req->u.i2c_read.num_bytes_read);
  257. idx++;
  258. break;
  259. case DP_REMOTE_I2C_WRITE:
  260. buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
  261. idx++;
  262. buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
  263. idx++;
  264. buf[idx] = (req->u.i2c_write.num_bytes);
  265. idx++;
  266. memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
  267. idx += req->u.i2c_write.num_bytes;
  268. break;
  269. }
  270. raw->cur_len = idx;
  271. }
  272. static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
  273. {
  274. u8 crc4;
  275. crc4 = drm_dp_msg_data_crc4(msg, len);
  276. msg[len] = crc4;
  277. }
  278. static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
  279. struct drm_dp_sideband_msg_tx *raw)
  280. {
  281. int idx = 0;
  282. u8 *buf = raw->msg;
  283. buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
  284. raw->cur_len = idx;
  285. }
  286. /* this adds a chunk of msg to the builder to get the final msg */
  287. static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
  288. u8 *replybuf, u8 replybuflen, bool hdr)
  289. {
  290. int ret;
  291. u8 crc4;
  292. if (hdr) {
  293. u8 hdrlen;
  294. struct drm_dp_sideband_msg_hdr recv_hdr;
  295. ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
  296. if (ret == false) {
  297. print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
  298. return false;
  299. }
  300. /* get length contained in this portion */
  301. msg->curchunk_len = recv_hdr.msg_len;
  302. msg->curchunk_hdrlen = hdrlen;
  303. /* we have already gotten an somt - don't bother parsing */
  304. if (recv_hdr.somt && msg->have_somt)
  305. return false;
  306. if (recv_hdr.somt) {
  307. memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
  308. msg->have_somt = true;
  309. }
  310. if (recv_hdr.eomt)
  311. msg->have_eomt = true;
  312. /* copy the bytes for the remainder of this header chunk */
  313. msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
  314. memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
  315. } else {
  316. memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
  317. msg->curchunk_idx += replybuflen;
  318. }
  319. if (msg->curchunk_idx >= msg->curchunk_len) {
  320. /* do CRC */
  321. crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
  322. /* copy chunk into bigger msg */
  323. memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
  324. msg->curlen += msg->curchunk_len - 1;
  325. }
  326. return true;
  327. }
  328. static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
  329. struct drm_dp_sideband_msg_reply_body *repmsg)
  330. {
  331. int idx = 1;
  332. int i;
  333. memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
  334. idx += 16;
  335. repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
  336. idx++;
  337. if (idx > raw->curlen)
  338. goto fail_len;
  339. for (i = 0; i < repmsg->u.link_addr.nports; i++) {
  340. if (raw->msg[idx] & 0x80)
  341. repmsg->u.link_addr.ports[i].input_port = 1;
  342. repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
  343. repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
  344. idx++;
  345. if (idx > raw->curlen)
  346. goto fail_len;
  347. repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
  348. repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
  349. if (repmsg->u.link_addr.ports[i].input_port == 0)
  350. repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
  351. idx++;
  352. if (idx > raw->curlen)
  353. goto fail_len;
  354. if (repmsg->u.link_addr.ports[i].input_port == 0) {
  355. repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
  356. idx++;
  357. if (idx > raw->curlen)
  358. goto fail_len;
  359. memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
  360. idx += 16;
  361. if (idx > raw->curlen)
  362. goto fail_len;
  363. repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
  364. repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
  365. idx++;
  366. }
  367. if (idx > raw->curlen)
  368. goto fail_len;
  369. }
  370. return true;
  371. fail_len:
  372. DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
  373. return false;
  374. }
  375. static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
  376. struct drm_dp_sideband_msg_reply_body *repmsg)
  377. {
  378. int idx = 1;
  379. repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
  380. idx++;
  381. if (idx > raw->curlen)
  382. goto fail_len;
  383. repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
  384. if (idx > raw->curlen)
  385. goto fail_len;
  386. memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
  387. return true;
  388. fail_len:
  389. DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
  390. return false;
  391. }
  392. static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
  393. struct drm_dp_sideband_msg_reply_body *repmsg)
  394. {
  395. int idx = 1;
  396. repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
  397. idx++;
  398. if (idx > raw->curlen)
  399. goto fail_len;
  400. return true;
  401. fail_len:
  402. DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
  403. return false;
  404. }
  405. static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
  406. struct drm_dp_sideband_msg_reply_body *repmsg)
  407. {
  408. int idx = 1;
  409. repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
  410. idx++;
  411. if (idx > raw->curlen)
  412. goto fail_len;
  413. repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
  414. idx++;
  415. /* TODO check */
  416. memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
  417. return true;
  418. fail_len:
  419. DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
  420. return false;
  421. }
  422. static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
  423. struct drm_dp_sideband_msg_reply_body *repmsg)
  424. {
  425. int idx = 1;
  426. repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
  427. idx++;
  428. if (idx > raw->curlen)
  429. goto fail_len;
  430. repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
  431. idx += 2;
  432. if (idx > raw->curlen)
  433. goto fail_len;
  434. repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
  435. idx += 2;
  436. if (idx > raw->curlen)
  437. goto fail_len;
  438. return true;
  439. fail_len:
  440. DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
  441. return false;
  442. }
  443. static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
  444. struct drm_dp_sideband_msg_reply_body *repmsg)
  445. {
  446. int idx = 1;
  447. repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
  448. idx++;
  449. if (idx > raw->curlen)
  450. goto fail_len;
  451. repmsg->u.allocate_payload.vcpi = raw->msg[idx];
  452. idx++;
  453. if (idx > raw->curlen)
  454. goto fail_len;
  455. repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
  456. idx += 2;
  457. if (idx > raw->curlen)
  458. goto fail_len;
  459. return true;
  460. fail_len:
  461. DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
  462. return false;
  463. }
  464. static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
  465. struct drm_dp_sideband_msg_reply_body *repmsg)
  466. {
  467. int idx = 1;
  468. repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
  469. idx++;
  470. if (idx > raw->curlen)
  471. goto fail_len;
  472. repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
  473. idx += 2;
  474. if (idx > raw->curlen)
  475. goto fail_len;
  476. return true;
  477. fail_len:
  478. DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
  479. return false;
  480. }
  481. static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
  482. struct drm_dp_sideband_msg_reply_body *msg)
  483. {
  484. memset(msg, 0, sizeof(*msg));
  485. msg->reply_type = (raw->msg[0] & 0x80) >> 7;
  486. msg->req_type = (raw->msg[0] & 0x7f);
  487. if (msg->reply_type) {
  488. memcpy(msg->u.nak.guid, &raw->msg[1], 16);
  489. msg->u.nak.reason = raw->msg[17];
  490. msg->u.nak.nak_data = raw->msg[18];
  491. return false;
  492. }
  493. switch (msg->req_type) {
  494. case DP_LINK_ADDRESS:
  495. return drm_dp_sideband_parse_link_address(raw, msg);
  496. case DP_QUERY_PAYLOAD:
  497. return drm_dp_sideband_parse_query_payload_ack(raw, msg);
  498. case DP_REMOTE_DPCD_READ:
  499. return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
  500. case DP_REMOTE_DPCD_WRITE:
  501. return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
  502. case DP_REMOTE_I2C_READ:
  503. return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
  504. case DP_ENUM_PATH_RESOURCES:
  505. return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
  506. case DP_ALLOCATE_PAYLOAD:
  507. return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
  508. default:
  509. DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
  510. return false;
  511. }
  512. }
  513. static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
  514. struct drm_dp_sideband_msg_req_body *msg)
  515. {
  516. int idx = 1;
  517. msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
  518. idx++;
  519. if (idx > raw->curlen)
  520. goto fail_len;
  521. memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
  522. idx += 16;
  523. if (idx > raw->curlen)
  524. goto fail_len;
  525. msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
  526. msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
  527. msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
  528. msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
  529. msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
  530. idx++;
  531. return true;
  532. fail_len:
  533. DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
  534. return false;
  535. }
  536. static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
  537. struct drm_dp_sideband_msg_req_body *msg)
  538. {
  539. int idx = 1;
  540. msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
  541. idx++;
  542. if (idx > raw->curlen)
  543. goto fail_len;
  544. memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
  545. idx += 16;
  546. if (idx > raw->curlen)
  547. goto fail_len;
  548. msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
  549. idx++;
  550. return true;
  551. fail_len:
  552. DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
  553. return false;
  554. }
  555. static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
  556. struct drm_dp_sideband_msg_req_body *msg)
  557. {
  558. memset(msg, 0, sizeof(*msg));
  559. msg->req_type = (raw->msg[0] & 0x7f);
  560. switch (msg->req_type) {
  561. case DP_CONNECTION_STATUS_NOTIFY:
  562. return drm_dp_sideband_parse_connection_status_notify(raw, msg);
  563. case DP_RESOURCE_STATUS_NOTIFY:
  564. return drm_dp_sideband_parse_resource_status_notify(raw, msg);
  565. default:
  566. DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
  567. return false;
  568. }
  569. }
  570. static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
  571. {
  572. struct drm_dp_sideband_msg_req_body req;
  573. req.req_type = DP_REMOTE_DPCD_WRITE;
  574. req.u.dpcd_write.port_number = port_num;
  575. req.u.dpcd_write.dpcd_address = offset;
  576. req.u.dpcd_write.num_bytes = num_bytes;
  577. req.u.dpcd_write.bytes = bytes;
  578. drm_dp_encode_sideband_req(&req, msg);
  579. return 0;
  580. }
  581. static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
  582. {
  583. struct drm_dp_sideband_msg_req_body req;
  584. req.req_type = DP_LINK_ADDRESS;
  585. drm_dp_encode_sideband_req(&req, msg);
  586. return 0;
  587. }
  588. static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
  589. {
  590. struct drm_dp_sideband_msg_req_body req;
  591. req.req_type = DP_ENUM_PATH_RESOURCES;
  592. req.u.port_num.port_number = port_num;
  593. drm_dp_encode_sideband_req(&req, msg);
  594. msg->path_msg = true;
  595. return 0;
  596. }
  597. static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
  598. u8 vcpi, uint16_t pbn)
  599. {
  600. struct drm_dp_sideband_msg_req_body req;
  601. memset(&req, 0, sizeof(req));
  602. req.req_type = DP_ALLOCATE_PAYLOAD;
  603. req.u.allocate_payload.port_number = port_num;
  604. req.u.allocate_payload.vcpi = vcpi;
  605. req.u.allocate_payload.pbn = pbn;
  606. drm_dp_encode_sideband_req(&req, msg);
  607. msg->path_msg = true;
  608. return 0;
  609. }
  610. static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
  611. struct drm_dp_vcpi *vcpi)
  612. {
  613. int ret, vcpi_ret;
  614. mutex_lock(&mgr->payload_lock);
  615. ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
  616. if (ret > mgr->max_payloads) {
  617. ret = -EINVAL;
  618. DRM_DEBUG_KMS("out of payload ids %d\n", ret);
  619. goto out_unlock;
  620. }
  621. vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
  622. if (vcpi_ret > mgr->max_payloads) {
  623. ret = -EINVAL;
  624. DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
  625. goto out_unlock;
  626. }
  627. set_bit(ret, &mgr->payload_mask);
  628. set_bit(vcpi_ret, &mgr->vcpi_mask);
  629. vcpi->vcpi = vcpi_ret + 1;
  630. mgr->proposed_vcpis[ret - 1] = vcpi;
  631. out_unlock:
  632. mutex_unlock(&mgr->payload_lock);
  633. return ret;
  634. }
  635. static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
  636. int vcpi)
  637. {
  638. int i;
  639. if (vcpi == 0)
  640. return;
  641. mutex_lock(&mgr->payload_lock);
  642. DRM_DEBUG_KMS("putting payload %d\n", vcpi);
  643. clear_bit(vcpi - 1, &mgr->vcpi_mask);
  644. for (i = 0; i < mgr->max_payloads; i++) {
  645. if (mgr->proposed_vcpis[i])
  646. if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
  647. mgr->proposed_vcpis[i] = NULL;
  648. clear_bit(i + 1, &mgr->payload_mask);
  649. }
  650. }
  651. mutex_unlock(&mgr->payload_lock);
  652. }
  653. static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
  654. struct drm_dp_sideband_msg_tx *txmsg)
  655. {
  656. bool ret;
  657. mutex_lock(&mgr->qlock);
  658. ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
  659. txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
  660. mutex_unlock(&mgr->qlock);
  661. return ret;
  662. }
  663. static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
  664. struct drm_dp_sideband_msg_tx *txmsg)
  665. {
  666. struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
  667. int ret;
  668. ret = wait_event_timeout(mgr->tx_waitq,
  669. check_txmsg_state(mgr, txmsg),
  670. (4 * HZ));
  671. mutex_lock(&mstb->mgr->qlock);
  672. if (ret > 0) {
  673. if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
  674. ret = -EIO;
  675. goto out;
  676. }
  677. } else {
  678. DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
  679. /* dump some state */
  680. ret = -EIO;
  681. /* remove from q */
  682. if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
  683. txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
  684. list_del(&txmsg->next);
  685. }
  686. if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
  687. txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
  688. mstb->tx_slots[txmsg->seqno] = NULL;
  689. }
  690. }
  691. out:
  692. mutex_unlock(&mgr->qlock);
  693. return ret;
  694. }
  695. static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
  696. {
  697. struct drm_dp_mst_branch *mstb;
  698. mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
  699. if (!mstb)
  700. return NULL;
  701. mstb->lct = lct;
  702. if (lct > 1)
  703. memcpy(mstb->rad, rad, lct / 2);
  704. INIT_LIST_HEAD(&mstb->ports);
  705. kref_init(&mstb->kref);
  706. return mstb;
  707. }
  708. static void drm_dp_destroy_mst_branch_device(struct kref *kref)
  709. {
  710. struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
  711. struct drm_dp_mst_port *port, *tmp;
  712. bool wake_tx = false;
  713. cancel_work_sync(&mstb->mgr->work);
  714. /*
  715. * destroy all ports - don't need lock
  716. * as there are no more references to the mst branch
  717. * device at this point.
  718. */
  719. list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
  720. list_del(&port->next);
  721. drm_dp_put_port(port);
  722. }
  723. /* drop any tx slots msg */
  724. mutex_lock(&mstb->mgr->qlock);
  725. if (mstb->tx_slots[0]) {
  726. mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  727. mstb->tx_slots[0] = NULL;
  728. wake_tx = true;
  729. }
  730. if (mstb->tx_slots[1]) {
  731. mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  732. mstb->tx_slots[1] = NULL;
  733. wake_tx = true;
  734. }
  735. mutex_unlock(&mstb->mgr->qlock);
  736. if (wake_tx)
  737. wake_up(&mstb->mgr->tx_waitq);
  738. kfree(mstb);
  739. }
  740. static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
  741. {
  742. kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
  743. }
  744. static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
  745. {
  746. struct drm_dp_mst_branch *mstb;
  747. switch (old_pdt) {
  748. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  749. case DP_PEER_DEVICE_SST_SINK:
  750. /* remove i2c over sideband */
  751. drm_dp_mst_unregister_i2c_bus(&port->aux);
  752. break;
  753. case DP_PEER_DEVICE_MST_BRANCHING:
  754. mstb = port->mstb;
  755. port->mstb = NULL;
  756. drm_dp_put_mst_branch_device(mstb);
  757. break;
  758. }
  759. }
  760. static void drm_dp_destroy_port(struct kref *kref)
  761. {
  762. struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
  763. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  764. if (!port->input) {
  765. port->vcpi.num_slots = 0;
  766. if (port->connector)
  767. (*port->mgr->cbs->destroy_connector)(mgr, port->connector);
  768. drm_dp_port_teardown_pdt(port, port->pdt);
  769. if (!port->input && port->vcpi.vcpi > 0)
  770. drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
  771. }
  772. kfree(port);
  773. (*mgr->cbs->hotplug)(mgr);
  774. }
  775. static void drm_dp_put_port(struct drm_dp_mst_port *port)
  776. {
  777. kref_put(&port->kref, drm_dp_destroy_port);
  778. }
  779. static struct drm_dp_mst_branch *drm_dp_mst_get_validated_mstb_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_branch *to_find)
  780. {
  781. struct drm_dp_mst_port *port;
  782. struct drm_dp_mst_branch *rmstb;
  783. if (to_find == mstb) {
  784. kref_get(&mstb->kref);
  785. return mstb;
  786. }
  787. list_for_each_entry(port, &mstb->ports, next) {
  788. if (port->mstb) {
  789. rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
  790. if (rmstb)
  791. return rmstb;
  792. }
  793. }
  794. return NULL;
  795. }
  796. static struct drm_dp_mst_branch *drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb)
  797. {
  798. struct drm_dp_mst_branch *rmstb = NULL;
  799. mutex_lock(&mgr->lock);
  800. if (mgr->mst_primary)
  801. rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
  802. mutex_unlock(&mgr->lock);
  803. return rmstb;
  804. }
  805. static struct drm_dp_mst_port *drm_dp_mst_get_port_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_port *to_find)
  806. {
  807. struct drm_dp_mst_port *port, *mport;
  808. list_for_each_entry(port, &mstb->ports, next) {
  809. if (port == to_find) {
  810. kref_get(&port->kref);
  811. return port;
  812. }
  813. if (port->mstb) {
  814. mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
  815. if (mport)
  816. return mport;
  817. }
  818. }
  819. return NULL;
  820. }
  821. static struct drm_dp_mst_port *drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  822. {
  823. struct drm_dp_mst_port *rport = NULL;
  824. mutex_lock(&mgr->lock);
  825. if (mgr->mst_primary)
  826. rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
  827. mutex_unlock(&mgr->lock);
  828. return rport;
  829. }
  830. static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
  831. {
  832. struct drm_dp_mst_port *port;
  833. list_for_each_entry(port, &mstb->ports, next) {
  834. if (port->port_num == port_num) {
  835. kref_get(&port->kref);
  836. return port;
  837. }
  838. }
  839. return NULL;
  840. }
  841. /*
  842. * calculate a new RAD for this MST branch device
  843. * if parent has an LCT of 2 then it has 1 nibble of RAD,
  844. * if parent has an LCT of 3 then it has 2 nibbles of RAD,
  845. */
  846. static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
  847. u8 *rad)
  848. {
  849. int lct = port->parent->lct;
  850. int shift = 4;
  851. int idx = lct / 2;
  852. if (lct > 1) {
  853. memcpy(rad, port->parent->rad, idx);
  854. shift = (lct % 2) ? 4 : 0;
  855. } else
  856. rad[0] = 0;
  857. rad[idx] |= port->port_num << shift;
  858. return lct + 1;
  859. }
  860. /*
  861. * return sends link address for new mstb
  862. */
  863. static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
  864. {
  865. int ret;
  866. u8 rad[6], lct;
  867. bool send_link = false;
  868. switch (port->pdt) {
  869. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  870. case DP_PEER_DEVICE_SST_SINK:
  871. /* add i2c over sideband */
  872. ret = drm_dp_mst_register_i2c_bus(&port->aux);
  873. break;
  874. case DP_PEER_DEVICE_MST_BRANCHING:
  875. lct = drm_dp_calculate_rad(port, rad);
  876. port->mstb = drm_dp_add_mst_branch_device(lct, rad);
  877. port->mstb->mgr = port->mgr;
  878. port->mstb->port_parent = port;
  879. send_link = true;
  880. break;
  881. }
  882. return send_link;
  883. }
  884. static void drm_dp_check_port_guid(struct drm_dp_mst_branch *mstb,
  885. struct drm_dp_mst_port *port)
  886. {
  887. int ret;
  888. if (port->dpcd_rev >= 0x12) {
  889. port->guid_valid = drm_dp_validate_guid(mstb->mgr, port->guid);
  890. if (!port->guid_valid) {
  891. ret = drm_dp_send_dpcd_write(mstb->mgr,
  892. port,
  893. DP_GUID,
  894. 16, port->guid);
  895. port->guid_valid = true;
  896. }
  897. }
  898. }
  899. static void build_mst_prop_path(struct drm_dp_mst_port *port,
  900. struct drm_dp_mst_branch *mstb,
  901. char *proppath,
  902. size_t proppath_size)
  903. {
  904. int i;
  905. char temp[8];
  906. snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
  907. for (i = 0; i < (mstb->lct - 1); i++) {
  908. int shift = (i % 2) ? 0 : 4;
  909. int port_num = mstb->rad[i / 2] >> shift;
  910. snprintf(temp, sizeof(temp), "-%d", port_num);
  911. strlcat(proppath, temp, proppath_size);
  912. }
  913. snprintf(temp, sizeof(temp), "-%d", port->port_num);
  914. strlcat(proppath, temp, proppath_size);
  915. }
  916. static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
  917. struct device *dev,
  918. struct drm_dp_link_addr_reply_port *port_msg)
  919. {
  920. struct drm_dp_mst_port *port;
  921. bool ret;
  922. bool created = false;
  923. int old_pdt = 0;
  924. int old_ddps = 0;
  925. port = drm_dp_get_port(mstb, port_msg->port_number);
  926. if (!port) {
  927. port = kzalloc(sizeof(*port), GFP_KERNEL);
  928. if (!port)
  929. return;
  930. kref_init(&port->kref);
  931. port->parent = mstb;
  932. port->port_num = port_msg->port_number;
  933. port->mgr = mstb->mgr;
  934. port->aux.name = "DPMST";
  935. port->aux.dev = dev;
  936. created = true;
  937. } else {
  938. old_pdt = port->pdt;
  939. old_ddps = port->ddps;
  940. }
  941. port->pdt = port_msg->peer_device_type;
  942. port->input = port_msg->input_port;
  943. port->mcs = port_msg->mcs;
  944. port->ddps = port_msg->ddps;
  945. port->ldps = port_msg->legacy_device_plug_status;
  946. port->dpcd_rev = port_msg->dpcd_revision;
  947. port->num_sdp_streams = port_msg->num_sdp_streams;
  948. port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
  949. memcpy(port->guid, port_msg->peer_guid, 16);
  950. /* manage mstb port lists with mgr lock - take a reference
  951. for this list */
  952. if (created) {
  953. mutex_lock(&mstb->mgr->lock);
  954. kref_get(&port->kref);
  955. list_add(&port->next, &mstb->ports);
  956. mutex_unlock(&mstb->mgr->lock);
  957. }
  958. if (old_ddps != port->ddps) {
  959. if (port->ddps) {
  960. drm_dp_check_port_guid(mstb, port);
  961. if (!port->input)
  962. drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
  963. } else {
  964. port->guid_valid = false;
  965. port->available_pbn = 0;
  966. }
  967. }
  968. if (old_pdt != port->pdt && !port->input) {
  969. drm_dp_port_teardown_pdt(port, old_pdt);
  970. ret = drm_dp_port_setup_pdt(port);
  971. if (ret == true) {
  972. drm_dp_send_link_address(mstb->mgr, port->mstb);
  973. port->mstb->link_address_sent = true;
  974. }
  975. }
  976. if (created && !port->input) {
  977. char proppath[255];
  978. build_mst_prop_path(port, mstb, proppath, sizeof(proppath));
  979. port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
  980. }
  981. /* put reference to this port */
  982. drm_dp_put_port(port);
  983. }
  984. static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
  985. struct drm_dp_connection_status_notify *conn_stat)
  986. {
  987. struct drm_dp_mst_port *port;
  988. int old_pdt;
  989. int old_ddps;
  990. bool dowork = false;
  991. port = drm_dp_get_port(mstb, conn_stat->port_number);
  992. if (!port)
  993. return;
  994. old_ddps = port->ddps;
  995. old_pdt = port->pdt;
  996. port->pdt = conn_stat->peer_device_type;
  997. port->mcs = conn_stat->message_capability_status;
  998. port->ldps = conn_stat->legacy_device_plug_status;
  999. port->ddps = conn_stat->displayport_device_plug_status;
  1000. if (old_ddps != port->ddps) {
  1001. if (port->ddps) {
  1002. drm_dp_check_port_guid(mstb, port);
  1003. dowork = true;
  1004. } else {
  1005. port->guid_valid = false;
  1006. port->available_pbn = 0;
  1007. }
  1008. }
  1009. if (old_pdt != port->pdt && !port->input) {
  1010. drm_dp_port_teardown_pdt(port, old_pdt);
  1011. if (drm_dp_port_setup_pdt(port))
  1012. dowork = true;
  1013. }
  1014. drm_dp_put_port(port);
  1015. if (dowork)
  1016. queue_work(system_long_wq, &mstb->mgr->work);
  1017. }
  1018. static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
  1019. u8 lct, u8 *rad)
  1020. {
  1021. struct drm_dp_mst_branch *mstb;
  1022. struct drm_dp_mst_port *port;
  1023. int i;
  1024. /* find the port by iterating down */
  1025. mstb = mgr->mst_primary;
  1026. for (i = 0; i < lct - 1; i++) {
  1027. int shift = (i % 2) ? 0 : 4;
  1028. int port_num = rad[i / 2] >> shift;
  1029. list_for_each_entry(port, &mstb->ports, next) {
  1030. if (port->port_num == port_num) {
  1031. if (!port->mstb) {
  1032. DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
  1033. return NULL;
  1034. }
  1035. mstb = port->mstb;
  1036. break;
  1037. }
  1038. }
  1039. }
  1040. kref_get(&mstb->kref);
  1041. return mstb;
  1042. }
  1043. static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  1044. struct drm_dp_mst_branch *mstb)
  1045. {
  1046. struct drm_dp_mst_port *port;
  1047. if (!mstb->link_address_sent) {
  1048. drm_dp_send_link_address(mgr, mstb);
  1049. mstb->link_address_sent = true;
  1050. }
  1051. list_for_each_entry(port, &mstb->ports, next) {
  1052. if (port->input)
  1053. continue;
  1054. if (!port->ddps)
  1055. continue;
  1056. if (!port->available_pbn)
  1057. drm_dp_send_enum_path_resources(mgr, mstb, port);
  1058. if (port->mstb)
  1059. drm_dp_check_and_send_link_address(mgr, port->mstb);
  1060. }
  1061. }
  1062. static void drm_dp_mst_link_probe_work(struct work_struct *work)
  1063. {
  1064. struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
  1065. drm_dp_check_and_send_link_address(mgr, mgr->mst_primary);
  1066. }
  1067. static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
  1068. u8 *guid)
  1069. {
  1070. static u8 zero_guid[16];
  1071. if (!memcmp(guid, zero_guid, 16)) {
  1072. u64 salt = get_jiffies_64();
  1073. memcpy(&guid[0], &salt, sizeof(u64));
  1074. memcpy(&guid[8], &salt, sizeof(u64));
  1075. return false;
  1076. }
  1077. return true;
  1078. }
  1079. #if 0
  1080. static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
  1081. {
  1082. struct drm_dp_sideband_msg_req_body req;
  1083. req.req_type = DP_REMOTE_DPCD_READ;
  1084. req.u.dpcd_read.port_number = port_num;
  1085. req.u.dpcd_read.dpcd_address = offset;
  1086. req.u.dpcd_read.num_bytes = num_bytes;
  1087. drm_dp_encode_sideband_req(&req, msg);
  1088. return 0;
  1089. }
  1090. #endif
  1091. static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
  1092. bool up, u8 *msg, int len)
  1093. {
  1094. int ret;
  1095. int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
  1096. int tosend, total, offset;
  1097. int retries = 0;
  1098. retry:
  1099. total = len;
  1100. offset = 0;
  1101. do {
  1102. tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
  1103. ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
  1104. &msg[offset],
  1105. tosend);
  1106. if (ret != tosend) {
  1107. if (ret == -EIO && retries < 5) {
  1108. retries++;
  1109. goto retry;
  1110. }
  1111. DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
  1112. WARN(1, "fail\n");
  1113. return -EIO;
  1114. }
  1115. offset += tosend;
  1116. total -= tosend;
  1117. } while (total > 0);
  1118. return 0;
  1119. }
  1120. static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
  1121. struct drm_dp_sideband_msg_tx *txmsg)
  1122. {
  1123. struct drm_dp_mst_branch *mstb = txmsg->dst;
  1124. /* both msg slots are full */
  1125. if (txmsg->seqno == -1) {
  1126. if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
  1127. DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
  1128. return -EAGAIN;
  1129. }
  1130. if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
  1131. txmsg->seqno = mstb->last_seqno;
  1132. mstb->last_seqno ^= 1;
  1133. } else if (mstb->tx_slots[0] == NULL)
  1134. txmsg->seqno = 0;
  1135. else
  1136. txmsg->seqno = 1;
  1137. mstb->tx_slots[txmsg->seqno] = txmsg;
  1138. }
  1139. hdr->broadcast = 0;
  1140. hdr->path_msg = txmsg->path_msg;
  1141. hdr->lct = mstb->lct;
  1142. hdr->lcr = mstb->lct - 1;
  1143. if (mstb->lct > 1)
  1144. memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
  1145. hdr->seqno = txmsg->seqno;
  1146. return 0;
  1147. }
  1148. /*
  1149. * process a single block of the next message in the sideband queue
  1150. */
  1151. static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
  1152. struct drm_dp_sideband_msg_tx *txmsg,
  1153. bool up)
  1154. {
  1155. u8 chunk[48];
  1156. struct drm_dp_sideband_msg_hdr hdr;
  1157. int len, space, idx, tosend;
  1158. int ret;
  1159. memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
  1160. if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
  1161. txmsg->seqno = -1;
  1162. txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
  1163. }
  1164. /* make hdr from dst mst - for replies use seqno
  1165. otherwise assign one */
  1166. ret = set_hdr_from_dst_qlock(&hdr, txmsg);
  1167. if (ret < 0)
  1168. return ret;
  1169. /* amount left to send in this message */
  1170. len = txmsg->cur_len - txmsg->cur_offset;
  1171. /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
  1172. space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
  1173. tosend = min(len, space);
  1174. if (len == txmsg->cur_len)
  1175. hdr.somt = 1;
  1176. if (space >= len)
  1177. hdr.eomt = 1;
  1178. hdr.msg_len = tosend + 1;
  1179. drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
  1180. memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
  1181. /* add crc at end */
  1182. drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
  1183. idx += tosend + 1;
  1184. ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
  1185. if (ret) {
  1186. DRM_DEBUG_KMS("sideband msg failed to send\n");
  1187. return ret;
  1188. }
  1189. txmsg->cur_offset += tosend;
  1190. if (txmsg->cur_offset == txmsg->cur_len) {
  1191. txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
  1192. return 1;
  1193. }
  1194. return 0;
  1195. }
  1196. /* must be called holding qlock */
  1197. static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
  1198. {
  1199. struct drm_dp_sideband_msg_tx *txmsg;
  1200. int ret;
  1201. /* construct a chunk from the first msg in the tx_msg queue */
  1202. if (list_empty(&mgr->tx_msg_downq)) {
  1203. mgr->tx_down_in_progress = false;
  1204. return;
  1205. }
  1206. mgr->tx_down_in_progress = true;
  1207. txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
  1208. ret = process_single_tx_qlock(mgr, txmsg, false);
  1209. if (ret == 1) {
  1210. /* txmsg is sent it should be in the slots now */
  1211. list_del(&txmsg->next);
  1212. } else if (ret) {
  1213. DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
  1214. list_del(&txmsg->next);
  1215. if (txmsg->seqno != -1)
  1216. txmsg->dst->tx_slots[txmsg->seqno] = NULL;
  1217. txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  1218. wake_up(&mgr->tx_waitq);
  1219. }
  1220. if (list_empty(&mgr->tx_msg_downq)) {
  1221. mgr->tx_down_in_progress = false;
  1222. return;
  1223. }
  1224. }
  1225. /* called holding qlock */
  1226. static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
  1227. {
  1228. struct drm_dp_sideband_msg_tx *txmsg;
  1229. int ret;
  1230. /* construct a chunk from the first msg in the tx_msg queue */
  1231. if (list_empty(&mgr->tx_msg_upq)) {
  1232. mgr->tx_up_in_progress = false;
  1233. return;
  1234. }
  1235. txmsg = list_first_entry(&mgr->tx_msg_upq, struct drm_dp_sideband_msg_tx, next);
  1236. ret = process_single_tx_qlock(mgr, txmsg, true);
  1237. if (ret == 1) {
  1238. /* up txmsgs aren't put in slots - so free after we send it */
  1239. list_del(&txmsg->next);
  1240. kfree(txmsg);
  1241. } else if (ret)
  1242. DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
  1243. mgr->tx_up_in_progress = true;
  1244. }
  1245. static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
  1246. struct drm_dp_sideband_msg_tx *txmsg)
  1247. {
  1248. mutex_lock(&mgr->qlock);
  1249. list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
  1250. if (!mgr->tx_down_in_progress)
  1251. process_single_down_tx_qlock(mgr);
  1252. mutex_unlock(&mgr->qlock);
  1253. }
  1254. static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  1255. struct drm_dp_mst_branch *mstb)
  1256. {
  1257. int len;
  1258. struct drm_dp_sideband_msg_tx *txmsg;
  1259. int ret;
  1260. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1261. if (!txmsg)
  1262. return -ENOMEM;
  1263. txmsg->dst = mstb;
  1264. len = build_link_address(txmsg);
  1265. drm_dp_queue_down_tx(mgr, txmsg);
  1266. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1267. if (ret > 0) {
  1268. int i;
  1269. if (txmsg->reply.reply_type == 1)
  1270. DRM_DEBUG_KMS("link address nak received\n");
  1271. else {
  1272. DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
  1273. for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
  1274. DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n", i,
  1275. txmsg->reply.u.link_addr.ports[i].input_port,
  1276. txmsg->reply.u.link_addr.ports[i].peer_device_type,
  1277. txmsg->reply.u.link_addr.ports[i].port_number,
  1278. txmsg->reply.u.link_addr.ports[i].dpcd_revision,
  1279. txmsg->reply.u.link_addr.ports[i].mcs,
  1280. txmsg->reply.u.link_addr.ports[i].ddps,
  1281. txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
  1282. txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
  1283. txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
  1284. }
  1285. for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
  1286. drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
  1287. }
  1288. (*mgr->cbs->hotplug)(mgr);
  1289. }
  1290. } else
  1291. DRM_DEBUG_KMS("link address failed %d\n", ret);
  1292. kfree(txmsg);
  1293. return 0;
  1294. }
  1295. static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
  1296. struct drm_dp_mst_branch *mstb,
  1297. struct drm_dp_mst_port *port)
  1298. {
  1299. int len;
  1300. struct drm_dp_sideband_msg_tx *txmsg;
  1301. int ret;
  1302. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1303. if (!txmsg)
  1304. return -ENOMEM;
  1305. txmsg->dst = mstb;
  1306. len = build_enum_path_resources(txmsg, port->port_num);
  1307. drm_dp_queue_down_tx(mgr, txmsg);
  1308. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1309. if (ret > 0) {
  1310. if (txmsg->reply.reply_type == 1)
  1311. DRM_DEBUG_KMS("enum path resources nak received\n");
  1312. else {
  1313. if (port->port_num != txmsg->reply.u.path_resources.port_number)
  1314. DRM_ERROR("got incorrect port in response\n");
  1315. DRM_DEBUG_KMS("enum path resources %d: %d %d\n", txmsg->reply.u.path_resources.port_number, txmsg->reply.u.path_resources.full_payload_bw_number,
  1316. txmsg->reply.u.path_resources.avail_payload_bw_number);
  1317. port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
  1318. }
  1319. }
  1320. kfree(txmsg);
  1321. return 0;
  1322. }
  1323. static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
  1324. struct drm_dp_mst_port *port,
  1325. int id,
  1326. int pbn)
  1327. {
  1328. struct drm_dp_sideband_msg_tx *txmsg;
  1329. struct drm_dp_mst_branch *mstb;
  1330. int len, ret;
  1331. mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
  1332. if (!mstb)
  1333. return -EINVAL;
  1334. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1335. if (!txmsg) {
  1336. ret = -ENOMEM;
  1337. goto fail_put;
  1338. }
  1339. txmsg->dst = mstb;
  1340. len = build_allocate_payload(txmsg, port->port_num,
  1341. id,
  1342. pbn);
  1343. drm_dp_queue_down_tx(mgr, txmsg);
  1344. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1345. if (ret > 0) {
  1346. if (txmsg->reply.reply_type == 1) {
  1347. ret = -EINVAL;
  1348. } else
  1349. ret = 0;
  1350. }
  1351. kfree(txmsg);
  1352. fail_put:
  1353. drm_dp_put_mst_branch_device(mstb);
  1354. return ret;
  1355. }
  1356. static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
  1357. int id,
  1358. struct drm_dp_payload *payload)
  1359. {
  1360. int ret;
  1361. ret = drm_dp_dpcd_write_payload(mgr, id, payload);
  1362. if (ret < 0) {
  1363. payload->payload_state = 0;
  1364. return ret;
  1365. }
  1366. payload->payload_state = DP_PAYLOAD_LOCAL;
  1367. return 0;
  1368. }
  1369. static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
  1370. struct drm_dp_mst_port *port,
  1371. int id,
  1372. struct drm_dp_payload *payload)
  1373. {
  1374. int ret;
  1375. ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
  1376. if (ret < 0)
  1377. return ret;
  1378. payload->payload_state = DP_PAYLOAD_REMOTE;
  1379. return ret;
  1380. }
  1381. static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
  1382. struct drm_dp_mst_port *port,
  1383. int id,
  1384. struct drm_dp_payload *payload)
  1385. {
  1386. DRM_DEBUG_KMS("\n");
  1387. /* its okay for these to fail */
  1388. if (port) {
  1389. drm_dp_payload_send_msg(mgr, port, id, 0);
  1390. }
  1391. drm_dp_dpcd_write_payload(mgr, id, payload);
  1392. payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
  1393. return 0;
  1394. }
  1395. static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
  1396. int id,
  1397. struct drm_dp_payload *payload)
  1398. {
  1399. payload->payload_state = 0;
  1400. return 0;
  1401. }
  1402. /**
  1403. * drm_dp_update_payload_part1() - Execute payload update part 1
  1404. * @mgr: manager to use.
  1405. *
  1406. * This iterates over all proposed virtual channels, and tries to
  1407. * allocate space in the link for them. For 0->slots transitions,
  1408. * this step just writes the VCPI to the MST device. For slots->0
  1409. * transitions, this writes the updated VCPIs and removes the
  1410. * remote VC payloads.
  1411. *
  1412. * after calling this the driver should generate ACT and payload
  1413. * packets.
  1414. */
  1415. int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
  1416. {
  1417. int i, j;
  1418. int cur_slots = 1;
  1419. struct drm_dp_payload req_payload;
  1420. struct drm_dp_mst_port *port;
  1421. mutex_lock(&mgr->payload_lock);
  1422. for (i = 0; i < mgr->max_payloads; i++) {
  1423. /* solve the current payloads - compare to the hw ones
  1424. - update the hw view */
  1425. req_payload.start_slot = cur_slots;
  1426. if (mgr->proposed_vcpis[i]) {
  1427. port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
  1428. req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
  1429. } else {
  1430. port = NULL;
  1431. req_payload.num_slots = 0;
  1432. }
  1433. if (mgr->payloads[i].start_slot != req_payload.start_slot) {
  1434. mgr->payloads[i].start_slot = req_payload.start_slot;
  1435. }
  1436. /* work out what is required to happen with this payload */
  1437. if (mgr->payloads[i].num_slots != req_payload.num_slots) {
  1438. /* need to push an update for this payload */
  1439. if (req_payload.num_slots) {
  1440. drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
  1441. mgr->payloads[i].num_slots = req_payload.num_slots;
  1442. } else if (mgr->payloads[i].num_slots) {
  1443. mgr->payloads[i].num_slots = 0;
  1444. drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]);
  1445. req_payload.payload_state = mgr->payloads[i].payload_state;
  1446. mgr->payloads[i].start_slot = 0;
  1447. }
  1448. mgr->payloads[i].payload_state = req_payload.payload_state;
  1449. }
  1450. cur_slots += req_payload.num_slots;
  1451. }
  1452. for (i = 0; i < mgr->max_payloads; i++) {
  1453. if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
  1454. DRM_DEBUG_KMS("removing payload %d\n", i);
  1455. for (j = i; j < mgr->max_payloads - 1; j++) {
  1456. memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
  1457. mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
  1458. if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
  1459. set_bit(j + 1, &mgr->payload_mask);
  1460. } else {
  1461. clear_bit(j + 1, &mgr->payload_mask);
  1462. }
  1463. }
  1464. memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
  1465. mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
  1466. clear_bit(mgr->max_payloads, &mgr->payload_mask);
  1467. }
  1468. }
  1469. mutex_unlock(&mgr->payload_lock);
  1470. return 0;
  1471. }
  1472. EXPORT_SYMBOL(drm_dp_update_payload_part1);
  1473. /**
  1474. * drm_dp_update_payload_part2() - Execute payload update part 2
  1475. * @mgr: manager to use.
  1476. *
  1477. * This iterates over all proposed virtual channels, and tries to
  1478. * allocate space in the link for them. For 0->slots transitions,
  1479. * this step writes the remote VC payload commands. For slots->0
  1480. * this just resets some internal state.
  1481. */
  1482. int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
  1483. {
  1484. struct drm_dp_mst_port *port;
  1485. int i;
  1486. int ret = 0;
  1487. mutex_lock(&mgr->payload_lock);
  1488. for (i = 0; i < mgr->max_payloads; i++) {
  1489. if (!mgr->proposed_vcpis[i])
  1490. continue;
  1491. port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
  1492. DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
  1493. if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
  1494. ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
  1495. } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
  1496. ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
  1497. }
  1498. if (ret) {
  1499. mutex_unlock(&mgr->payload_lock);
  1500. return ret;
  1501. }
  1502. }
  1503. mutex_unlock(&mgr->payload_lock);
  1504. return 0;
  1505. }
  1506. EXPORT_SYMBOL(drm_dp_update_payload_part2);
  1507. #if 0 /* unused as of yet */
  1508. static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
  1509. struct drm_dp_mst_port *port,
  1510. int offset, int size)
  1511. {
  1512. int len;
  1513. struct drm_dp_sideband_msg_tx *txmsg;
  1514. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1515. if (!txmsg)
  1516. return -ENOMEM;
  1517. len = build_dpcd_read(txmsg, port->port_num, 0, 8);
  1518. txmsg->dst = port->parent;
  1519. drm_dp_queue_down_tx(mgr, txmsg);
  1520. return 0;
  1521. }
  1522. #endif
  1523. static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
  1524. struct drm_dp_mst_port *port,
  1525. int offset, int size, u8 *bytes)
  1526. {
  1527. int len;
  1528. int ret;
  1529. struct drm_dp_sideband_msg_tx *txmsg;
  1530. struct drm_dp_mst_branch *mstb;
  1531. mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
  1532. if (!mstb)
  1533. return -EINVAL;
  1534. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1535. if (!txmsg) {
  1536. ret = -ENOMEM;
  1537. goto fail_put;
  1538. }
  1539. len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
  1540. txmsg->dst = mstb;
  1541. drm_dp_queue_down_tx(mgr, txmsg);
  1542. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1543. if (ret > 0) {
  1544. if (txmsg->reply.reply_type == 1) {
  1545. ret = -EINVAL;
  1546. } else
  1547. ret = 0;
  1548. }
  1549. kfree(txmsg);
  1550. fail_put:
  1551. drm_dp_put_mst_branch_device(mstb);
  1552. return ret;
  1553. }
  1554. static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
  1555. {
  1556. struct drm_dp_sideband_msg_reply_body reply;
  1557. reply.reply_type = 1;
  1558. reply.req_type = req_type;
  1559. drm_dp_encode_sideband_reply(&reply, msg);
  1560. return 0;
  1561. }
  1562. static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
  1563. struct drm_dp_mst_branch *mstb,
  1564. int req_type, int seqno, bool broadcast)
  1565. {
  1566. struct drm_dp_sideband_msg_tx *txmsg;
  1567. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1568. if (!txmsg)
  1569. return -ENOMEM;
  1570. txmsg->dst = mstb;
  1571. txmsg->seqno = seqno;
  1572. drm_dp_encode_up_ack_reply(txmsg, req_type);
  1573. mutex_lock(&mgr->qlock);
  1574. list_add_tail(&txmsg->next, &mgr->tx_msg_upq);
  1575. if (!mgr->tx_up_in_progress) {
  1576. process_single_up_tx_qlock(mgr);
  1577. }
  1578. mutex_unlock(&mgr->qlock);
  1579. return 0;
  1580. }
  1581. static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
  1582. int dp_link_count,
  1583. int *out)
  1584. {
  1585. switch (dp_link_bw) {
  1586. default:
  1587. DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
  1588. dp_link_bw, dp_link_count);
  1589. return false;
  1590. case DP_LINK_BW_1_62:
  1591. *out = 3 * dp_link_count;
  1592. break;
  1593. case DP_LINK_BW_2_7:
  1594. *out = 5 * dp_link_count;
  1595. break;
  1596. case DP_LINK_BW_5_4:
  1597. *out = 10 * dp_link_count;
  1598. break;
  1599. }
  1600. return true;
  1601. }
  1602. /**
  1603. * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
  1604. * @mgr: manager to set state for
  1605. * @mst_state: true to enable MST on this connector - false to disable.
  1606. *
  1607. * This is called by the driver when it detects an MST capable device plugged
  1608. * into a DP MST capable port, or when a DP MST capable device is unplugged.
  1609. */
  1610. int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
  1611. {
  1612. int ret = 0;
  1613. struct drm_dp_mst_branch *mstb = NULL;
  1614. mutex_lock(&mgr->lock);
  1615. if (mst_state == mgr->mst_state)
  1616. goto out_unlock;
  1617. mgr->mst_state = mst_state;
  1618. /* set the device into MST mode */
  1619. if (mst_state) {
  1620. WARN_ON(mgr->mst_primary);
  1621. /* get dpcd info */
  1622. ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
  1623. if (ret != DP_RECEIVER_CAP_SIZE) {
  1624. DRM_DEBUG_KMS("failed to read DPCD\n");
  1625. goto out_unlock;
  1626. }
  1627. if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
  1628. mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
  1629. &mgr->pbn_div)) {
  1630. ret = -EINVAL;
  1631. goto out_unlock;
  1632. }
  1633. mgr->total_pbn = 2560;
  1634. mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
  1635. mgr->avail_slots = mgr->total_slots;
  1636. /* add initial branch device at LCT 1 */
  1637. mstb = drm_dp_add_mst_branch_device(1, NULL);
  1638. if (mstb == NULL) {
  1639. ret = -ENOMEM;
  1640. goto out_unlock;
  1641. }
  1642. mstb->mgr = mgr;
  1643. /* give this the main reference */
  1644. mgr->mst_primary = mstb;
  1645. kref_get(&mgr->mst_primary->kref);
  1646. {
  1647. struct drm_dp_payload reset_pay;
  1648. reset_pay.start_slot = 0;
  1649. reset_pay.num_slots = 0x3f;
  1650. drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
  1651. }
  1652. ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  1653. DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
  1654. if (ret < 0) {
  1655. goto out_unlock;
  1656. }
  1657. /* sort out guid */
  1658. ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, mgr->guid, 16);
  1659. if (ret != 16) {
  1660. DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret);
  1661. goto out_unlock;
  1662. }
  1663. mgr->guid_valid = drm_dp_validate_guid(mgr, mgr->guid);
  1664. if (!mgr->guid_valid) {
  1665. ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, mgr->guid, 16);
  1666. mgr->guid_valid = true;
  1667. }
  1668. queue_work(system_long_wq, &mgr->work);
  1669. ret = 0;
  1670. } else {
  1671. /* disable MST on the device */
  1672. mstb = mgr->mst_primary;
  1673. mgr->mst_primary = NULL;
  1674. /* this can fail if the device is gone */
  1675. drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
  1676. ret = 0;
  1677. memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
  1678. mgr->payload_mask = 0;
  1679. set_bit(0, &mgr->payload_mask);
  1680. mgr->vcpi_mask = 0;
  1681. }
  1682. out_unlock:
  1683. mutex_unlock(&mgr->lock);
  1684. if (mstb)
  1685. drm_dp_put_mst_branch_device(mstb);
  1686. return ret;
  1687. }
  1688. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
  1689. /**
  1690. * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
  1691. * @mgr: manager to suspend
  1692. *
  1693. * This function tells the MST device that we can't handle UP messages
  1694. * anymore. This should stop it from sending any since we are suspended.
  1695. */
  1696. void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
  1697. {
  1698. mutex_lock(&mgr->lock);
  1699. drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  1700. DP_MST_EN | DP_UPSTREAM_IS_SRC);
  1701. mutex_unlock(&mgr->lock);
  1702. }
  1703. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
  1704. /**
  1705. * drm_dp_mst_topology_mgr_resume() - resume the MST manager
  1706. * @mgr: manager to resume
  1707. *
  1708. * This will fetch DPCD and see if the device is still there,
  1709. * if it is, it will rewrite the MSTM control bits, and return.
  1710. *
  1711. * if the device fails this returns -1, and the driver should do
  1712. * a full MST reprobe, in case we were undocked.
  1713. */
  1714. int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
  1715. {
  1716. int ret = 0;
  1717. mutex_lock(&mgr->lock);
  1718. if (mgr->mst_primary) {
  1719. int sret;
  1720. sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
  1721. if (sret != DP_RECEIVER_CAP_SIZE) {
  1722. DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
  1723. ret = -1;
  1724. goto out_unlock;
  1725. }
  1726. ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  1727. DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
  1728. if (ret < 0) {
  1729. DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
  1730. ret = -1;
  1731. goto out_unlock;
  1732. }
  1733. ret = 0;
  1734. } else
  1735. ret = -1;
  1736. out_unlock:
  1737. mutex_unlock(&mgr->lock);
  1738. return ret;
  1739. }
  1740. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
  1741. static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
  1742. {
  1743. int len;
  1744. u8 replyblock[32];
  1745. int replylen, origlen, curreply;
  1746. int ret;
  1747. struct drm_dp_sideband_msg_rx *msg;
  1748. int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
  1749. msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
  1750. len = min(mgr->max_dpcd_transaction_bytes, 16);
  1751. ret = drm_dp_dpcd_read(mgr->aux, basereg,
  1752. replyblock, len);
  1753. if (ret != len) {
  1754. DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
  1755. return;
  1756. }
  1757. ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
  1758. if (!ret) {
  1759. DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
  1760. return;
  1761. }
  1762. replylen = msg->curchunk_len + msg->curchunk_hdrlen;
  1763. origlen = replylen;
  1764. replylen -= len;
  1765. curreply = len;
  1766. while (replylen > 0) {
  1767. len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
  1768. ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
  1769. replyblock, len);
  1770. if (ret != len) {
  1771. DRM_DEBUG_KMS("failed to read a chunk\n");
  1772. }
  1773. ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
  1774. if (ret == false)
  1775. DRM_DEBUG_KMS("failed to build sideband msg\n");
  1776. curreply += len;
  1777. replylen -= len;
  1778. }
  1779. }
  1780. static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
  1781. {
  1782. int ret = 0;
  1783. drm_dp_get_one_sb_msg(mgr, false);
  1784. if (mgr->down_rep_recv.have_eomt) {
  1785. struct drm_dp_sideband_msg_tx *txmsg;
  1786. struct drm_dp_mst_branch *mstb;
  1787. int slot = -1;
  1788. mstb = drm_dp_get_mst_branch_device(mgr,
  1789. mgr->down_rep_recv.initial_hdr.lct,
  1790. mgr->down_rep_recv.initial_hdr.rad);
  1791. if (!mstb) {
  1792. DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
  1793. memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1794. return 0;
  1795. }
  1796. /* find the message */
  1797. slot = mgr->down_rep_recv.initial_hdr.seqno;
  1798. mutex_lock(&mgr->qlock);
  1799. txmsg = mstb->tx_slots[slot];
  1800. /* remove from slots */
  1801. mutex_unlock(&mgr->qlock);
  1802. if (!txmsg) {
  1803. DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
  1804. mstb,
  1805. mgr->down_rep_recv.initial_hdr.seqno,
  1806. mgr->down_rep_recv.initial_hdr.lct,
  1807. mgr->down_rep_recv.initial_hdr.rad[0],
  1808. mgr->down_rep_recv.msg[0]);
  1809. drm_dp_put_mst_branch_device(mstb);
  1810. memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1811. return 0;
  1812. }
  1813. drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
  1814. if (txmsg->reply.reply_type == 1) {
  1815. DRM_DEBUG_KMS("Got NAK reply: req 0x%02x, reason 0x%02x, nak data 0x%02x\n", txmsg->reply.req_type, txmsg->reply.u.nak.reason, txmsg->reply.u.nak.nak_data);
  1816. }
  1817. memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1818. drm_dp_put_mst_branch_device(mstb);
  1819. mutex_lock(&mgr->qlock);
  1820. txmsg->state = DRM_DP_SIDEBAND_TX_RX;
  1821. mstb->tx_slots[slot] = NULL;
  1822. mutex_unlock(&mgr->qlock);
  1823. wake_up(&mgr->tx_waitq);
  1824. }
  1825. return ret;
  1826. }
  1827. static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
  1828. {
  1829. int ret = 0;
  1830. drm_dp_get_one_sb_msg(mgr, true);
  1831. if (mgr->up_req_recv.have_eomt) {
  1832. struct drm_dp_sideband_msg_req_body msg;
  1833. struct drm_dp_mst_branch *mstb;
  1834. bool seqno;
  1835. mstb = drm_dp_get_mst_branch_device(mgr,
  1836. mgr->up_req_recv.initial_hdr.lct,
  1837. mgr->up_req_recv.initial_hdr.rad);
  1838. if (!mstb) {
  1839. DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
  1840. memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1841. return 0;
  1842. }
  1843. seqno = mgr->up_req_recv.initial_hdr.seqno;
  1844. drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
  1845. if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
  1846. drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
  1847. drm_dp_update_port(mstb, &msg.u.conn_stat);
  1848. DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n", msg.u.conn_stat.port_number, msg.u.conn_stat.legacy_device_plug_status, msg.u.conn_stat.displayport_device_plug_status, msg.u.conn_stat.message_capability_status, msg.u.conn_stat.input_port, msg.u.conn_stat.peer_device_type);
  1849. (*mgr->cbs->hotplug)(mgr);
  1850. } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
  1851. drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
  1852. DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
  1853. }
  1854. drm_dp_put_mst_branch_device(mstb);
  1855. memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1856. }
  1857. return ret;
  1858. }
  1859. /**
  1860. * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
  1861. * @mgr: manager to notify irq for.
  1862. * @esi: 4 bytes from SINK_COUNT_ESI
  1863. * @handled: whether the hpd interrupt was consumed or not
  1864. *
  1865. * This should be called from the driver when it detects a short IRQ,
  1866. * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
  1867. * topology manager will process the sideband messages received as a result
  1868. * of this.
  1869. */
  1870. int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
  1871. {
  1872. int ret = 0;
  1873. int sc;
  1874. *handled = false;
  1875. sc = esi[0] & 0x3f;
  1876. if (sc != mgr->sink_count) {
  1877. mgr->sink_count = sc;
  1878. *handled = true;
  1879. }
  1880. if (esi[1] & DP_DOWN_REP_MSG_RDY) {
  1881. ret = drm_dp_mst_handle_down_rep(mgr);
  1882. *handled = true;
  1883. }
  1884. if (esi[1] & DP_UP_REQ_MSG_RDY) {
  1885. ret |= drm_dp_mst_handle_up_req(mgr);
  1886. *handled = true;
  1887. }
  1888. drm_dp_mst_kick_tx(mgr);
  1889. return ret;
  1890. }
  1891. EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
  1892. /**
  1893. * drm_dp_mst_detect_port() - get connection status for an MST port
  1894. * @mgr: manager for this port
  1895. * @port: unverified pointer to a port
  1896. *
  1897. * This returns the current connection state for a port. It validates the
  1898. * port pointer still exists so the caller doesn't require a reference
  1899. */
  1900. enum drm_connector_status drm_dp_mst_detect_port(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  1901. {
  1902. enum drm_connector_status status = connector_status_disconnected;
  1903. /* we need to search for the port in the mgr in case its gone */
  1904. port = drm_dp_get_validated_port_ref(mgr, port);
  1905. if (!port)
  1906. return connector_status_disconnected;
  1907. if (!port->ddps)
  1908. goto out;
  1909. switch (port->pdt) {
  1910. case DP_PEER_DEVICE_NONE:
  1911. case DP_PEER_DEVICE_MST_BRANCHING:
  1912. break;
  1913. case DP_PEER_DEVICE_SST_SINK:
  1914. status = connector_status_connected;
  1915. break;
  1916. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  1917. if (port->ldps)
  1918. status = connector_status_connected;
  1919. break;
  1920. }
  1921. out:
  1922. drm_dp_put_port(port);
  1923. return status;
  1924. }
  1925. EXPORT_SYMBOL(drm_dp_mst_detect_port);
  1926. /**
  1927. * drm_dp_mst_get_edid() - get EDID for an MST port
  1928. * @connector: toplevel connector to get EDID for
  1929. * @mgr: manager for this port
  1930. * @port: unverified pointer to a port.
  1931. *
  1932. * This returns an EDID for the port connected to a connector,
  1933. * It validates the pointer still exists so the caller doesn't require a
  1934. * reference.
  1935. */
  1936. struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  1937. {
  1938. struct edid *edid = NULL;
  1939. /* we need to search for the port in the mgr in case its gone */
  1940. port = drm_dp_get_validated_port_ref(mgr, port);
  1941. if (!port)
  1942. return NULL;
  1943. edid = drm_get_edid(connector, &port->aux.ddc);
  1944. drm_dp_put_port(port);
  1945. return edid;
  1946. }
  1947. EXPORT_SYMBOL(drm_dp_mst_get_edid);
  1948. /**
  1949. * drm_dp_find_vcpi_slots() - find slots for this PBN value
  1950. * @mgr: manager to use
  1951. * @pbn: payload bandwidth to convert into slots.
  1952. */
  1953. int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
  1954. int pbn)
  1955. {
  1956. int num_slots;
  1957. num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
  1958. if (num_slots > mgr->avail_slots)
  1959. return -ENOSPC;
  1960. return num_slots;
  1961. }
  1962. EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
  1963. static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
  1964. struct drm_dp_vcpi *vcpi, int pbn)
  1965. {
  1966. int num_slots;
  1967. int ret;
  1968. num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
  1969. if (num_slots > mgr->avail_slots)
  1970. return -ENOSPC;
  1971. vcpi->pbn = pbn;
  1972. vcpi->aligned_pbn = num_slots * mgr->pbn_div;
  1973. vcpi->num_slots = num_slots;
  1974. ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
  1975. if (ret < 0)
  1976. return ret;
  1977. return 0;
  1978. }
  1979. /**
  1980. * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
  1981. * @mgr: manager for this port
  1982. * @port: port to allocate a virtual channel for.
  1983. * @pbn: payload bandwidth number to request
  1984. * @slots: returned number of slots for this PBN.
  1985. */
  1986. bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
  1987. {
  1988. int ret;
  1989. port = drm_dp_get_validated_port_ref(mgr, port);
  1990. if (!port)
  1991. return false;
  1992. if (port->vcpi.vcpi > 0) {
  1993. DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
  1994. if (pbn == port->vcpi.pbn) {
  1995. *slots = port->vcpi.num_slots;
  1996. return true;
  1997. }
  1998. }
  1999. ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
  2000. if (ret) {
  2001. DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
  2002. goto out;
  2003. }
  2004. DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
  2005. *slots = port->vcpi.num_slots;
  2006. drm_dp_put_port(port);
  2007. return true;
  2008. out:
  2009. return false;
  2010. }
  2011. EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
  2012. /**
  2013. * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
  2014. * @mgr: manager for this port
  2015. * @port: unverified pointer to a port.
  2016. *
  2017. * This just resets the number of slots for the ports VCPI for later programming.
  2018. */
  2019. void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  2020. {
  2021. port = drm_dp_get_validated_port_ref(mgr, port);
  2022. if (!port)
  2023. return;
  2024. port->vcpi.num_slots = 0;
  2025. drm_dp_put_port(port);
  2026. }
  2027. EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
  2028. /**
  2029. * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
  2030. * @mgr: manager for this port
  2031. * @port: unverified port to deallocate vcpi for
  2032. */
  2033. void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  2034. {
  2035. port = drm_dp_get_validated_port_ref(mgr, port);
  2036. if (!port)
  2037. return;
  2038. drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
  2039. port->vcpi.num_slots = 0;
  2040. port->vcpi.pbn = 0;
  2041. port->vcpi.aligned_pbn = 0;
  2042. port->vcpi.vcpi = 0;
  2043. drm_dp_put_port(port);
  2044. }
  2045. EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
  2046. static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
  2047. int id, struct drm_dp_payload *payload)
  2048. {
  2049. u8 payload_alloc[3], status;
  2050. int ret;
  2051. int retries = 0;
  2052. drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
  2053. DP_PAYLOAD_TABLE_UPDATED);
  2054. payload_alloc[0] = id;
  2055. payload_alloc[1] = payload->start_slot;
  2056. payload_alloc[2] = payload->num_slots;
  2057. ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
  2058. if (ret != 3) {
  2059. DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
  2060. goto fail;
  2061. }
  2062. retry:
  2063. ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
  2064. if (ret < 0) {
  2065. DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
  2066. goto fail;
  2067. }
  2068. if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
  2069. retries++;
  2070. if (retries < 20) {
  2071. usleep_range(10000, 20000);
  2072. goto retry;
  2073. }
  2074. DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
  2075. ret = -EINVAL;
  2076. goto fail;
  2077. }
  2078. ret = 0;
  2079. fail:
  2080. return ret;
  2081. }
  2082. /**
  2083. * drm_dp_check_act_status() - Check ACT handled status.
  2084. * @mgr: manager to use
  2085. *
  2086. * Check the payload status bits in the DPCD for ACT handled completion.
  2087. */
  2088. int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
  2089. {
  2090. u8 status;
  2091. int ret;
  2092. int count = 0;
  2093. do {
  2094. ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
  2095. if (ret < 0) {
  2096. DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
  2097. goto fail;
  2098. }
  2099. if (status & DP_PAYLOAD_ACT_HANDLED)
  2100. break;
  2101. count++;
  2102. udelay(100);
  2103. } while (count < 30);
  2104. if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
  2105. DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
  2106. ret = -EINVAL;
  2107. goto fail;
  2108. }
  2109. return 0;
  2110. fail:
  2111. return ret;
  2112. }
  2113. EXPORT_SYMBOL(drm_dp_check_act_status);
  2114. /**
  2115. * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
  2116. * @clock: dot clock for the mode
  2117. * @bpp: bpp for the mode.
  2118. *
  2119. * This uses the formula in the spec to calculate the PBN value for a mode.
  2120. */
  2121. int drm_dp_calc_pbn_mode(int clock, int bpp)
  2122. {
  2123. fixed20_12 pix_bw;
  2124. fixed20_12 fbpp;
  2125. fixed20_12 result;
  2126. fixed20_12 margin, tmp;
  2127. u32 res;
  2128. pix_bw.full = dfixed_const(clock);
  2129. fbpp.full = dfixed_const(bpp);
  2130. tmp.full = dfixed_const(8);
  2131. fbpp.full = dfixed_div(fbpp, tmp);
  2132. result.full = dfixed_mul(pix_bw, fbpp);
  2133. margin.full = dfixed_const(54);
  2134. tmp.full = dfixed_const(64);
  2135. margin.full = dfixed_div(margin, tmp);
  2136. result.full = dfixed_div(result, margin);
  2137. margin.full = dfixed_const(1006);
  2138. tmp.full = dfixed_const(1000);
  2139. margin.full = dfixed_div(margin, tmp);
  2140. result.full = dfixed_mul(result, margin);
  2141. result.full = dfixed_div(result, tmp);
  2142. result.full = dfixed_ceil(result);
  2143. res = dfixed_trunc(result);
  2144. return res;
  2145. }
  2146. EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
  2147. static int test_calc_pbn_mode(void)
  2148. {
  2149. int ret;
  2150. ret = drm_dp_calc_pbn_mode(154000, 30);
  2151. if (ret != 689)
  2152. return -EINVAL;
  2153. ret = drm_dp_calc_pbn_mode(234000, 30);
  2154. if (ret != 1047)
  2155. return -EINVAL;
  2156. return 0;
  2157. }
  2158. /* we want to kick the TX after we've ack the up/down IRQs. */
  2159. static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
  2160. {
  2161. queue_work(system_long_wq, &mgr->tx_work);
  2162. }
  2163. static void drm_dp_mst_dump_mstb(struct seq_file *m,
  2164. struct drm_dp_mst_branch *mstb)
  2165. {
  2166. struct drm_dp_mst_port *port;
  2167. int tabs = mstb->lct;
  2168. char prefix[10];
  2169. int i;
  2170. for (i = 0; i < tabs; i++)
  2171. prefix[i] = '\t';
  2172. prefix[i] = '\0';
  2173. seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
  2174. list_for_each_entry(port, &mstb->ports, next) {
  2175. seq_printf(m, "%sport: %d: ddps: %d ldps: %d, %p, conn: %p\n", prefix, port->port_num, port->ddps, port->ldps, port, port->connector);
  2176. if (port->mstb)
  2177. drm_dp_mst_dump_mstb(m, port->mstb);
  2178. }
  2179. }
  2180. static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
  2181. char *buf)
  2182. {
  2183. int ret;
  2184. int i;
  2185. for (i = 0; i < 4; i++) {
  2186. ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
  2187. if (ret != 16)
  2188. break;
  2189. }
  2190. if (i == 4)
  2191. return true;
  2192. return false;
  2193. }
  2194. /**
  2195. * drm_dp_mst_dump_topology(): dump topology to seq file.
  2196. * @m: seq_file to dump output to
  2197. * @mgr: manager to dump current topology for.
  2198. *
  2199. * helper to dump MST topology to a seq file for debugfs.
  2200. */
  2201. void drm_dp_mst_dump_topology(struct seq_file *m,
  2202. struct drm_dp_mst_topology_mgr *mgr)
  2203. {
  2204. int i;
  2205. struct drm_dp_mst_port *port;
  2206. mutex_lock(&mgr->lock);
  2207. if (mgr->mst_primary)
  2208. drm_dp_mst_dump_mstb(m, mgr->mst_primary);
  2209. /* dump VCPIs */
  2210. mutex_unlock(&mgr->lock);
  2211. mutex_lock(&mgr->payload_lock);
  2212. seq_printf(m, "vcpi: %lx %lx\n", mgr->payload_mask, mgr->vcpi_mask);
  2213. for (i = 0; i < mgr->max_payloads; i++) {
  2214. if (mgr->proposed_vcpis[i]) {
  2215. port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
  2216. seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots);
  2217. } else
  2218. seq_printf(m, "vcpi %d:unsed\n", i);
  2219. }
  2220. for (i = 0; i < mgr->max_payloads; i++) {
  2221. seq_printf(m, "payload %d: %d, %d, %d\n",
  2222. i,
  2223. mgr->payloads[i].payload_state,
  2224. mgr->payloads[i].start_slot,
  2225. mgr->payloads[i].num_slots);
  2226. }
  2227. mutex_unlock(&mgr->payload_lock);
  2228. mutex_lock(&mgr->lock);
  2229. if (mgr->mst_primary) {
  2230. u8 buf[64];
  2231. bool bret;
  2232. int ret;
  2233. ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
  2234. seq_printf(m, "dpcd: ");
  2235. for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
  2236. seq_printf(m, "%02x ", buf[i]);
  2237. seq_printf(m, "\n");
  2238. ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
  2239. seq_printf(m, "faux/mst: ");
  2240. for (i = 0; i < 2; i++)
  2241. seq_printf(m, "%02x ", buf[i]);
  2242. seq_printf(m, "\n");
  2243. ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
  2244. seq_printf(m, "mst ctrl: ");
  2245. for (i = 0; i < 1; i++)
  2246. seq_printf(m, "%02x ", buf[i]);
  2247. seq_printf(m, "\n");
  2248. bret = dump_dp_payload_table(mgr, buf);
  2249. if (bret == true) {
  2250. seq_printf(m, "payload table: ");
  2251. for (i = 0; i < 63; i++)
  2252. seq_printf(m, "%02x ", buf[i]);
  2253. seq_printf(m, "\n");
  2254. }
  2255. }
  2256. mutex_unlock(&mgr->lock);
  2257. }
  2258. EXPORT_SYMBOL(drm_dp_mst_dump_topology);
  2259. static void drm_dp_tx_work(struct work_struct *work)
  2260. {
  2261. struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
  2262. mutex_lock(&mgr->qlock);
  2263. if (mgr->tx_down_in_progress)
  2264. process_single_down_tx_qlock(mgr);
  2265. mutex_unlock(&mgr->qlock);
  2266. }
  2267. /**
  2268. * drm_dp_mst_topology_mgr_init - initialise a topology manager
  2269. * @mgr: manager struct to initialise
  2270. * @dev: device providing this structure - for i2c addition.
  2271. * @aux: DP helper aux channel to talk to this device
  2272. * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
  2273. * @max_payloads: maximum number of payloads this GPU can source
  2274. * @conn_base_id: the connector object ID the MST device is connected to.
  2275. *
  2276. * Return 0 for success, or negative error code on failure
  2277. */
  2278. int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
  2279. struct device *dev, struct drm_dp_aux *aux,
  2280. int max_dpcd_transaction_bytes,
  2281. int max_payloads, int conn_base_id)
  2282. {
  2283. mutex_init(&mgr->lock);
  2284. mutex_init(&mgr->qlock);
  2285. mutex_init(&mgr->payload_lock);
  2286. INIT_LIST_HEAD(&mgr->tx_msg_upq);
  2287. INIT_LIST_HEAD(&mgr->tx_msg_downq);
  2288. INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
  2289. INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
  2290. init_waitqueue_head(&mgr->tx_waitq);
  2291. mgr->dev = dev;
  2292. mgr->aux = aux;
  2293. mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
  2294. mgr->max_payloads = max_payloads;
  2295. mgr->conn_base_id = conn_base_id;
  2296. mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
  2297. if (!mgr->payloads)
  2298. return -ENOMEM;
  2299. mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
  2300. if (!mgr->proposed_vcpis)
  2301. return -ENOMEM;
  2302. set_bit(0, &mgr->payload_mask);
  2303. test_calc_pbn_mode();
  2304. return 0;
  2305. }
  2306. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
  2307. /**
  2308. * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
  2309. * @mgr: manager to destroy
  2310. */
  2311. void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
  2312. {
  2313. mutex_lock(&mgr->payload_lock);
  2314. kfree(mgr->payloads);
  2315. mgr->payloads = NULL;
  2316. kfree(mgr->proposed_vcpis);
  2317. mgr->proposed_vcpis = NULL;
  2318. mutex_unlock(&mgr->payload_lock);
  2319. mgr->dev = NULL;
  2320. mgr->aux = NULL;
  2321. }
  2322. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
  2323. /* I2C device */
  2324. static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  2325. int num)
  2326. {
  2327. struct drm_dp_aux *aux = adapter->algo_data;
  2328. struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
  2329. struct drm_dp_mst_branch *mstb;
  2330. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  2331. unsigned int i;
  2332. bool reading = false;
  2333. struct drm_dp_sideband_msg_req_body msg;
  2334. struct drm_dp_sideband_msg_tx *txmsg = NULL;
  2335. int ret;
  2336. mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
  2337. if (!mstb)
  2338. return -EREMOTEIO;
  2339. /* construct i2c msg */
  2340. /* see if last msg is a read */
  2341. if (msgs[num - 1].flags & I2C_M_RD)
  2342. reading = true;
  2343. if (!reading) {
  2344. DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
  2345. ret = -EIO;
  2346. goto out;
  2347. }
  2348. msg.req_type = DP_REMOTE_I2C_READ;
  2349. msg.u.i2c_read.num_transactions = num - 1;
  2350. msg.u.i2c_read.port_number = port->port_num;
  2351. for (i = 0; i < num - 1; i++) {
  2352. msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
  2353. msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
  2354. msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
  2355. }
  2356. msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
  2357. msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
  2358. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2359. if (!txmsg) {
  2360. ret = -ENOMEM;
  2361. goto out;
  2362. }
  2363. txmsg->dst = mstb;
  2364. drm_dp_encode_sideband_req(&msg, txmsg);
  2365. drm_dp_queue_down_tx(mgr, txmsg);
  2366. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  2367. if (ret > 0) {
  2368. if (txmsg->reply.reply_type == 1) { /* got a NAK back */
  2369. ret = -EREMOTEIO;
  2370. goto out;
  2371. }
  2372. if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
  2373. ret = -EIO;
  2374. goto out;
  2375. }
  2376. memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
  2377. ret = num;
  2378. }
  2379. out:
  2380. kfree(txmsg);
  2381. drm_dp_put_mst_branch_device(mstb);
  2382. return ret;
  2383. }
  2384. static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
  2385. {
  2386. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  2387. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  2388. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  2389. I2C_FUNC_10BIT_ADDR;
  2390. }
  2391. static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
  2392. .functionality = drm_dp_mst_i2c_functionality,
  2393. .master_xfer = drm_dp_mst_i2c_xfer,
  2394. };
  2395. /**
  2396. * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
  2397. * @aux: DisplayPort AUX channel
  2398. *
  2399. * Returns 0 on success or a negative error code on failure.
  2400. */
  2401. static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
  2402. {
  2403. aux->ddc.algo = &drm_dp_mst_i2c_algo;
  2404. aux->ddc.algo_data = aux;
  2405. aux->ddc.retries = 3;
  2406. aux->ddc.class = I2C_CLASS_DDC;
  2407. aux->ddc.owner = THIS_MODULE;
  2408. aux->ddc.dev.parent = aux->dev;
  2409. aux->ddc.dev.of_node = aux->dev->of_node;
  2410. strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  2411. sizeof(aux->ddc.name));
  2412. return i2c_add_adapter(&aux->ddc);
  2413. }
  2414. /**
  2415. * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
  2416. * @aux: DisplayPort AUX channel
  2417. */
  2418. static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
  2419. {
  2420. i2c_del_adapter(&aux->ddc);
  2421. }