drm_dp_mst_topology.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  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 void 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. u8 number_sdp_streams,
  600. u8 *sdp_stream_sink)
  601. {
  602. struct drm_dp_sideband_msg_req_body req;
  603. memset(&req, 0, sizeof(req));
  604. req.req_type = DP_ALLOCATE_PAYLOAD;
  605. req.u.allocate_payload.port_number = port_num;
  606. req.u.allocate_payload.vcpi = vcpi;
  607. req.u.allocate_payload.pbn = pbn;
  608. req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
  609. memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
  610. number_sdp_streams);
  611. drm_dp_encode_sideband_req(&req, msg);
  612. msg->path_msg = true;
  613. return 0;
  614. }
  615. static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
  616. struct drm_dp_vcpi *vcpi)
  617. {
  618. int ret, vcpi_ret;
  619. mutex_lock(&mgr->payload_lock);
  620. ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
  621. if (ret > mgr->max_payloads) {
  622. ret = -EINVAL;
  623. DRM_DEBUG_KMS("out of payload ids %d\n", ret);
  624. goto out_unlock;
  625. }
  626. vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
  627. if (vcpi_ret > mgr->max_payloads) {
  628. ret = -EINVAL;
  629. DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
  630. goto out_unlock;
  631. }
  632. set_bit(ret, &mgr->payload_mask);
  633. set_bit(vcpi_ret, &mgr->vcpi_mask);
  634. vcpi->vcpi = vcpi_ret + 1;
  635. mgr->proposed_vcpis[ret - 1] = vcpi;
  636. out_unlock:
  637. mutex_unlock(&mgr->payload_lock);
  638. return ret;
  639. }
  640. static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
  641. int vcpi)
  642. {
  643. int i;
  644. if (vcpi == 0)
  645. return;
  646. mutex_lock(&mgr->payload_lock);
  647. DRM_DEBUG_KMS("putting payload %d\n", vcpi);
  648. clear_bit(vcpi - 1, &mgr->vcpi_mask);
  649. for (i = 0; i < mgr->max_payloads; i++) {
  650. if (mgr->proposed_vcpis[i])
  651. if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
  652. mgr->proposed_vcpis[i] = NULL;
  653. clear_bit(i + 1, &mgr->payload_mask);
  654. }
  655. }
  656. mutex_unlock(&mgr->payload_lock);
  657. }
  658. static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
  659. struct drm_dp_sideband_msg_tx *txmsg)
  660. {
  661. bool ret;
  662. /*
  663. * All updates to txmsg->state are protected by mgr->qlock, and the two
  664. * cases we check here are terminal states. For those the barriers
  665. * provided by the wake_up/wait_event pair are enough.
  666. */
  667. ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
  668. txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
  669. return ret;
  670. }
  671. static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
  672. struct drm_dp_sideband_msg_tx *txmsg)
  673. {
  674. struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
  675. int ret;
  676. ret = wait_event_timeout(mgr->tx_waitq,
  677. check_txmsg_state(mgr, txmsg),
  678. (4 * HZ));
  679. mutex_lock(&mstb->mgr->qlock);
  680. if (ret > 0) {
  681. if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
  682. ret = -EIO;
  683. goto out;
  684. }
  685. } else {
  686. DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
  687. /* dump some state */
  688. ret = -EIO;
  689. /* remove from q */
  690. if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
  691. txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
  692. list_del(&txmsg->next);
  693. }
  694. if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
  695. txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
  696. mstb->tx_slots[txmsg->seqno] = NULL;
  697. }
  698. }
  699. out:
  700. mutex_unlock(&mgr->qlock);
  701. return ret;
  702. }
  703. static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
  704. {
  705. struct drm_dp_mst_branch *mstb;
  706. mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
  707. if (!mstb)
  708. return NULL;
  709. mstb->lct = lct;
  710. if (lct > 1)
  711. memcpy(mstb->rad, rad, lct / 2);
  712. INIT_LIST_HEAD(&mstb->ports);
  713. kref_init(&mstb->kref);
  714. return mstb;
  715. }
  716. static void drm_dp_destroy_mst_branch_device(struct kref *kref)
  717. {
  718. struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
  719. struct drm_dp_mst_port *port, *tmp;
  720. bool wake_tx = false;
  721. /*
  722. * destroy all ports - don't need lock
  723. * as there are no more references to the mst branch
  724. * device at this point.
  725. */
  726. list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
  727. list_del(&port->next);
  728. drm_dp_put_port(port);
  729. }
  730. /* drop any tx slots msg */
  731. mutex_lock(&mstb->mgr->qlock);
  732. if (mstb->tx_slots[0]) {
  733. mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  734. mstb->tx_slots[0] = NULL;
  735. wake_tx = true;
  736. }
  737. if (mstb->tx_slots[1]) {
  738. mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  739. mstb->tx_slots[1] = NULL;
  740. wake_tx = true;
  741. }
  742. mutex_unlock(&mstb->mgr->qlock);
  743. if (wake_tx)
  744. wake_up(&mstb->mgr->tx_waitq);
  745. kfree(mstb);
  746. }
  747. static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
  748. {
  749. kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
  750. }
  751. static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
  752. {
  753. struct drm_dp_mst_branch *mstb;
  754. switch (old_pdt) {
  755. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  756. case DP_PEER_DEVICE_SST_SINK:
  757. /* remove i2c over sideband */
  758. drm_dp_mst_unregister_i2c_bus(&port->aux);
  759. break;
  760. case DP_PEER_DEVICE_MST_BRANCHING:
  761. mstb = port->mstb;
  762. port->mstb = NULL;
  763. drm_dp_put_mst_branch_device(mstb);
  764. break;
  765. }
  766. }
  767. static void drm_dp_destroy_port(struct kref *kref)
  768. {
  769. struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
  770. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  771. if (!port->input) {
  772. port->vcpi.num_slots = 0;
  773. kfree(port->cached_edid);
  774. /*
  775. * The only time we don't have a connector
  776. * on an output port is if the connector init
  777. * fails.
  778. */
  779. if (port->connector) {
  780. /* we can't destroy the connector here, as
  781. * we might be holding the mode_config.mutex
  782. * from an EDID retrieval */
  783. mutex_lock(&mgr->destroy_connector_lock);
  784. list_add(&port->next, &mgr->destroy_connector_list);
  785. mutex_unlock(&mgr->destroy_connector_lock);
  786. schedule_work(&mgr->destroy_connector_work);
  787. return;
  788. }
  789. /* no need to clean up vcpi
  790. * as if we have no connector we never setup a vcpi */
  791. drm_dp_port_teardown_pdt(port, port->pdt);
  792. }
  793. kfree(port);
  794. }
  795. static void drm_dp_put_port(struct drm_dp_mst_port *port)
  796. {
  797. kref_put(&port->kref, drm_dp_destroy_port);
  798. }
  799. 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)
  800. {
  801. struct drm_dp_mst_port *port;
  802. struct drm_dp_mst_branch *rmstb;
  803. if (to_find == mstb) {
  804. kref_get(&mstb->kref);
  805. return mstb;
  806. }
  807. list_for_each_entry(port, &mstb->ports, next) {
  808. if (port->mstb) {
  809. rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
  810. if (rmstb)
  811. return rmstb;
  812. }
  813. }
  814. return NULL;
  815. }
  816. 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)
  817. {
  818. struct drm_dp_mst_branch *rmstb = NULL;
  819. mutex_lock(&mgr->lock);
  820. if (mgr->mst_primary)
  821. rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
  822. mutex_unlock(&mgr->lock);
  823. return rmstb;
  824. }
  825. 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)
  826. {
  827. struct drm_dp_mst_port *port, *mport;
  828. list_for_each_entry(port, &mstb->ports, next) {
  829. if (port == to_find) {
  830. kref_get(&port->kref);
  831. return port;
  832. }
  833. if (port->mstb) {
  834. mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
  835. if (mport)
  836. return mport;
  837. }
  838. }
  839. return NULL;
  840. }
  841. 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)
  842. {
  843. struct drm_dp_mst_port *rport = NULL;
  844. mutex_lock(&mgr->lock);
  845. if (mgr->mst_primary)
  846. rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
  847. mutex_unlock(&mgr->lock);
  848. return rport;
  849. }
  850. static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
  851. {
  852. struct drm_dp_mst_port *port;
  853. list_for_each_entry(port, &mstb->ports, next) {
  854. if (port->port_num == port_num) {
  855. kref_get(&port->kref);
  856. return port;
  857. }
  858. }
  859. return NULL;
  860. }
  861. /*
  862. * calculate a new RAD for this MST branch device
  863. * if parent has an LCT of 2 then it has 1 nibble of RAD,
  864. * if parent has an LCT of 3 then it has 2 nibbles of RAD,
  865. */
  866. static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
  867. u8 *rad)
  868. {
  869. int lct = port->parent->lct;
  870. int shift = 4;
  871. int idx = lct / 2;
  872. if (lct > 1) {
  873. memcpy(rad, port->parent->rad, idx);
  874. shift = (lct % 2) ? 4 : 0;
  875. } else
  876. rad[0] = 0;
  877. rad[idx] |= port->port_num << shift;
  878. return lct + 1;
  879. }
  880. /*
  881. * return sends link address for new mstb
  882. */
  883. static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
  884. {
  885. int ret;
  886. u8 rad[6], lct;
  887. bool send_link = false;
  888. switch (port->pdt) {
  889. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  890. case DP_PEER_DEVICE_SST_SINK:
  891. /* add i2c over sideband */
  892. ret = drm_dp_mst_register_i2c_bus(&port->aux);
  893. break;
  894. case DP_PEER_DEVICE_MST_BRANCHING:
  895. lct = drm_dp_calculate_rad(port, rad);
  896. port->mstb = drm_dp_add_mst_branch_device(lct, rad);
  897. port->mstb->mgr = port->mgr;
  898. port->mstb->port_parent = port;
  899. send_link = true;
  900. break;
  901. }
  902. return send_link;
  903. }
  904. static void drm_dp_check_port_guid(struct drm_dp_mst_branch *mstb,
  905. struct drm_dp_mst_port *port)
  906. {
  907. int ret;
  908. if (port->dpcd_rev >= 0x12) {
  909. port->guid_valid = drm_dp_validate_guid(mstb->mgr, port->guid);
  910. if (!port->guid_valid) {
  911. ret = drm_dp_send_dpcd_write(mstb->mgr,
  912. port,
  913. DP_GUID,
  914. 16, port->guid);
  915. port->guid_valid = true;
  916. }
  917. }
  918. }
  919. static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
  920. int pnum,
  921. char *proppath,
  922. size_t proppath_size)
  923. {
  924. int i;
  925. char temp[8];
  926. snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
  927. for (i = 0; i < (mstb->lct - 1); i++) {
  928. int shift = (i % 2) ? 0 : 4;
  929. int port_num = mstb->rad[i / 2] >> shift;
  930. snprintf(temp, sizeof(temp), "-%d", port_num);
  931. strlcat(proppath, temp, proppath_size);
  932. }
  933. snprintf(temp, sizeof(temp), "-%d", pnum);
  934. strlcat(proppath, temp, proppath_size);
  935. }
  936. static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
  937. struct device *dev,
  938. struct drm_dp_link_addr_reply_port *port_msg)
  939. {
  940. struct drm_dp_mst_port *port;
  941. bool ret;
  942. bool created = false;
  943. int old_pdt = 0;
  944. int old_ddps = 0;
  945. port = drm_dp_get_port(mstb, port_msg->port_number);
  946. if (!port) {
  947. port = kzalloc(sizeof(*port), GFP_KERNEL);
  948. if (!port)
  949. return;
  950. kref_init(&port->kref);
  951. port->parent = mstb;
  952. port->port_num = port_msg->port_number;
  953. port->mgr = mstb->mgr;
  954. port->aux.name = "DPMST";
  955. port->aux.dev = dev;
  956. created = true;
  957. } else {
  958. old_pdt = port->pdt;
  959. old_ddps = port->ddps;
  960. }
  961. port->pdt = port_msg->peer_device_type;
  962. port->input = port_msg->input_port;
  963. port->mcs = port_msg->mcs;
  964. port->ddps = port_msg->ddps;
  965. port->ldps = port_msg->legacy_device_plug_status;
  966. port->dpcd_rev = port_msg->dpcd_revision;
  967. port->num_sdp_streams = port_msg->num_sdp_streams;
  968. port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
  969. memcpy(port->guid, port_msg->peer_guid, 16);
  970. /* manage mstb port lists with mgr lock - take a reference
  971. for this list */
  972. if (created) {
  973. mutex_lock(&mstb->mgr->lock);
  974. kref_get(&port->kref);
  975. list_add(&port->next, &mstb->ports);
  976. mutex_unlock(&mstb->mgr->lock);
  977. }
  978. if (old_ddps != port->ddps) {
  979. if (port->ddps) {
  980. drm_dp_check_port_guid(mstb, port);
  981. if (!port->input)
  982. drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
  983. } else {
  984. port->guid_valid = false;
  985. port->available_pbn = 0;
  986. }
  987. }
  988. if (old_pdt != port->pdt && !port->input) {
  989. drm_dp_port_teardown_pdt(port, old_pdt);
  990. ret = drm_dp_port_setup_pdt(port);
  991. if (ret == true)
  992. drm_dp_send_link_address(mstb->mgr, port->mstb);
  993. }
  994. if (created && !port->input) {
  995. char proppath[255];
  996. build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
  997. port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
  998. if (!port->connector) {
  999. /* remove it from the port list */
  1000. mutex_lock(&mstb->mgr->lock);
  1001. list_del(&port->next);
  1002. mutex_unlock(&mstb->mgr->lock);
  1003. /* drop port list reference */
  1004. drm_dp_put_port(port);
  1005. goto out;
  1006. }
  1007. if (port->port_num >= DP_MST_LOGICAL_PORT_0) {
  1008. port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
  1009. drm_mode_connector_set_tile_property(port->connector);
  1010. }
  1011. (*mstb->mgr->cbs->register_connector)(port->connector);
  1012. }
  1013. out:
  1014. /* put reference to this port */
  1015. drm_dp_put_port(port);
  1016. }
  1017. static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
  1018. struct drm_dp_connection_status_notify *conn_stat)
  1019. {
  1020. struct drm_dp_mst_port *port;
  1021. int old_pdt;
  1022. int old_ddps;
  1023. bool dowork = false;
  1024. port = drm_dp_get_port(mstb, conn_stat->port_number);
  1025. if (!port)
  1026. return;
  1027. old_ddps = port->ddps;
  1028. old_pdt = port->pdt;
  1029. port->pdt = conn_stat->peer_device_type;
  1030. port->mcs = conn_stat->message_capability_status;
  1031. port->ldps = conn_stat->legacy_device_plug_status;
  1032. port->ddps = conn_stat->displayport_device_plug_status;
  1033. if (old_ddps != port->ddps) {
  1034. if (port->ddps) {
  1035. drm_dp_check_port_guid(mstb, port);
  1036. dowork = true;
  1037. } else {
  1038. port->guid_valid = false;
  1039. port->available_pbn = 0;
  1040. }
  1041. }
  1042. if (old_pdt != port->pdt && !port->input) {
  1043. drm_dp_port_teardown_pdt(port, old_pdt);
  1044. if (drm_dp_port_setup_pdt(port))
  1045. dowork = true;
  1046. }
  1047. drm_dp_put_port(port);
  1048. if (dowork)
  1049. queue_work(system_long_wq, &mstb->mgr->work);
  1050. }
  1051. static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
  1052. u8 lct, u8 *rad)
  1053. {
  1054. struct drm_dp_mst_branch *mstb;
  1055. struct drm_dp_mst_port *port;
  1056. int i;
  1057. /* find the port by iterating down */
  1058. mutex_lock(&mgr->lock);
  1059. mstb = mgr->mst_primary;
  1060. for (i = 0; i < lct - 1; i++) {
  1061. int shift = (i % 2) ? 0 : 4;
  1062. int port_num = rad[i / 2] >> shift;
  1063. list_for_each_entry(port, &mstb->ports, next) {
  1064. if (port->port_num == port_num) {
  1065. mstb = port->mstb;
  1066. if (!mstb) {
  1067. DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
  1068. goto out;
  1069. }
  1070. break;
  1071. }
  1072. }
  1073. }
  1074. kref_get(&mstb->kref);
  1075. out:
  1076. mutex_unlock(&mgr->lock);
  1077. return mstb;
  1078. }
  1079. static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
  1080. struct drm_dp_mst_branch *mstb,
  1081. uint8_t *guid)
  1082. {
  1083. struct drm_dp_mst_branch *found_mstb;
  1084. struct drm_dp_mst_port *port;
  1085. list_for_each_entry(port, &mstb->ports, next) {
  1086. if (!port->mstb)
  1087. continue;
  1088. if (port->guid_valid && memcmp(port->guid, guid, 16) == 0)
  1089. return port->mstb;
  1090. found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
  1091. if (found_mstb)
  1092. return found_mstb;
  1093. }
  1094. return NULL;
  1095. }
  1096. static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
  1097. struct drm_dp_mst_topology_mgr *mgr,
  1098. uint8_t *guid)
  1099. {
  1100. struct drm_dp_mst_branch *mstb;
  1101. /* find the port by iterating down */
  1102. mutex_lock(&mgr->lock);
  1103. if (mgr->guid_valid && memcmp(mgr->guid, guid, 16) == 0)
  1104. mstb = mgr->mst_primary;
  1105. else
  1106. mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
  1107. if (mstb)
  1108. kref_get(&mstb->kref);
  1109. mutex_unlock(&mgr->lock);
  1110. return mstb;
  1111. }
  1112. static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  1113. struct drm_dp_mst_branch *mstb)
  1114. {
  1115. struct drm_dp_mst_port *port;
  1116. struct drm_dp_mst_branch *mstb_child;
  1117. if (!mstb->link_address_sent)
  1118. drm_dp_send_link_address(mgr, mstb);
  1119. list_for_each_entry(port, &mstb->ports, next) {
  1120. if (port->input)
  1121. continue;
  1122. if (!port->ddps)
  1123. continue;
  1124. if (!port->available_pbn)
  1125. drm_dp_send_enum_path_resources(mgr, mstb, port);
  1126. if (port->mstb) {
  1127. mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
  1128. if (mstb_child) {
  1129. drm_dp_check_and_send_link_address(mgr, mstb_child);
  1130. drm_dp_put_mst_branch_device(mstb_child);
  1131. }
  1132. }
  1133. }
  1134. }
  1135. static void drm_dp_mst_link_probe_work(struct work_struct *work)
  1136. {
  1137. struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
  1138. struct drm_dp_mst_branch *mstb;
  1139. mutex_lock(&mgr->lock);
  1140. mstb = mgr->mst_primary;
  1141. if (mstb) {
  1142. kref_get(&mstb->kref);
  1143. }
  1144. mutex_unlock(&mgr->lock);
  1145. if (mstb) {
  1146. drm_dp_check_and_send_link_address(mgr, mstb);
  1147. drm_dp_put_mst_branch_device(mstb);
  1148. }
  1149. }
  1150. static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
  1151. u8 *guid)
  1152. {
  1153. static u8 zero_guid[16];
  1154. if (!memcmp(guid, zero_guid, 16)) {
  1155. u64 salt = get_jiffies_64();
  1156. memcpy(&guid[0], &salt, sizeof(u64));
  1157. memcpy(&guid[8], &salt, sizeof(u64));
  1158. return false;
  1159. }
  1160. return true;
  1161. }
  1162. #if 0
  1163. static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
  1164. {
  1165. struct drm_dp_sideband_msg_req_body req;
  1166. req.req_type = DP_REMOTE_DPCD_READ;
  1167. req.u.dpcd_read.port_number = port_num;
  1168. req.u.dpcd_read.dpcd_address = offset;
  1169. req.u.dpcd_read.num_bytes = num_bytes;
  1170. drm_dp_encode_sideband_req(&req, msg);
  1171. return 0;
  1172. }
  1173. #endif
  1174. static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
  1175. bool up, u8 *msg, int len)
  1176. {
  1177. int ret;
  1178. int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
  1179. int tosend, total, offset;
  1180. int retries = 0;
  1181. retry:
  1182. total = len;
  1183. offset = 0;
  1184. do {
  1185. tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
  1186. ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
  1187. &msg[offset],
  1188. tosend);
  1189. if (ret != tosend) {
  1190. if (ret == -EIO && retries < 5) {
  1191. retries++;
  1192. goto retry;
  1193. }
  1194. DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
  1195. return -EIO;
  1196. }
  1197. offset += tosend;
  1198. total -= tosend;
  1199. } while (total > 0);
  1200. return 0;
  1201. }
  1202. static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
  1203. struct drm_dp_sideband_msg_tx *txmsg)
  1204. {
  1205. struct drm_dp_mst_branch *mstb = txmsg->dst;
  1206. u8 req_type;
  1207. /* both msg slots are full */
  1208. if (txmsg->seqno == -1) {
  1209. if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
  1210. DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
  1211. return -EAGAIN;
  1212. }
  1213. if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
  1214. txmsg->seqno = mstb->last_seqno;
  1215. mstb->last_seqno ^= 1;
  1216. } else if (mstb->tx_slots[0] == NULL)
  1217. txmsg->seqno = 0;
  1218. else
  1219. txmsg->seqno = 1;
  1220. mstb->tx_slots[txmsg->seqno] = txmsg;
  1221. }
  1222. req_type = txmsg->msg[0] & 0x7f;
  1223. if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
  1224. req_type == DP_RESOURCE_STATUS_NOTIFY)
  1225. hdr->broadcast = 1;
  1226. else
  1227. hdr->broadcast = 0;
  1228. hdr->path_msg = txmsg->path_msg;
  1229. hdr->lct = mstb->lct;
  1230. hdr->lcr = mstb->lct - 1;
  1231. if (mstb->lct > 1)
  1232. memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
  1233. hdr->seqno = txmsg->seqno;
  1234. return 0;
  1235. }
  1236. /*
  1237. * process a single block of the next message in the sideband queue
  1238. */
  1239. static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
  1240. struct drm_dp_sideband_msg_tx *txmsg,
  1241. bool up)
  1242. {
  1243. u8 chunk[48];
  1244. struct drm_dp_sideband_msg_hdr hdr;
  1245. int len, space, idx, tosend;
  1246. int ret;
  1247. memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
  1248. if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
  1249. txmsg->seqno = -1;
  1250. txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
  1251. }
  1252. /* make hdr from dst mst - for replies use seqno
  1253. otherwise assign one */
  1254. ret = set_hdr_from_dst_qlock(&hdr, txmsg);
  1255. if (ret < 0)
  1256. return ret;
  1257. /* amount left to send in this message */
  1258. len = txmsg->cur_len - txmsg->cur_offset;
  1259. /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
  1260. space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
  1261. tosend = min(len, space);
  1262. if (len == txmsg->cur_len)
  1263. hdr.somt = 1;
  1264. if (space >= len)
  1265. hdr.eomt = 1;
  1266. hdr.msg_len = tosend + 1;
  1267. drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
  1268. memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
  1269. /* add crc at end */
  1270. drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
  1271. idx += tosend + 1;
  1272. ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
  1273. if (ret) {
  1274. DRM_DEBUG_KMS("sideband msg failed to send\n");
  1275. return ret;
  1276. }
  1277. txmsg->cur_offset += tosend;
  1278. if (txmsg->cur_offset == txmsg->cur_len) {
  1279. txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
  1280. return 1;
  1281. }
  1282. return 0;
  1283. }
  1284. static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
  1285. {
  1286. struct drm_dp_sideband_msg_tx *txmsg;
  1287. int ret;
  1288. WARN_ON(!mutex_is_locked(&mgr->qlock));
  1289. /* construct a chunk from the first msg in the tx_msg queue */
  1290. if (list_empty(&mgr->tx_msg_downq)) {
  1291. mgr->tx_down_in_progress = false;
  1292. return;
  1293. }
  1294. mgr->tx_down_in_progress = true;
  1295. txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
  1296. ret = process_single_tx_qlock(mgr, txmsg, false);
  1297. if (ret == 1) {
  1298. /* txmsg is sent it should be in the slots now */
  1299. list_del(&txmsg->next);
  1300. } else if (ret) {
  1301. DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
  1302. list_del(&txmsg->next);
  1303. if (txmsg->seqno != -1)
  1304. txmsg->dst->tx_slots[txmsg->seqno] = NULL;
  1305. txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
  1306. wake_up(&mgr->tx_waitq);
  1307. }
  1308. if (list_empty(&mgr->tx_msg_downq)) {
  1309. mgr->tx_down_in_progress = false;
  1310. return;
  1311. }
  1312. }
  1313. /* called holding qlock */
  1314. static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
  1315. struct drm_dp_sideband_msg_tx *txmsg)
  1316. {
  1317. int ret;
  1318. /* construct a chunk from the first msg in the tx_msg queue */
  1319. ret = process_single_tx_qlock(mgr, txmsg, true);
  1320. if (ret != 1)
  1321. DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
  1322. txmsg->dst->tx_slots[txmsg->seqno] = NULL;
  1323. }
  1324. static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
  1325. struct drm_dp_sideband_msg_tx *txmsg)
  1326. {
  1327. mutex_lock(&mgr->qlock);
  1328. list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
  1329. if (!mgr->tx_down_in_progress)
  1330. process_single_down_tx_qlock(mgr);
  1331. mutex_unlock(&mgr->qlock);
  1332. }
  1333. static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
  1334. struct drm_dp_mst_branch *mstb)
  1335. {
  1336. int len;
  1337. struct drm_dp_sideband_msg_tx *txmsg;
  1338. int ret;
  1339. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1340. if (!txmsg)
  1341. return;
  1342. txmsg->dst = mstb;
  1343. len = build_link_address(txmsg);
  1344. mstb->link_address_sent = true;
  1345. drm_dp_queue_down_tx(mgr, txmsg);
  1346. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1347. if (ret > 0) {
  1348. int i;
  1349. if (txmsg->reply.reply_type == 1)
  1350. DRM_DEBUG_KMS("link address nak received\n");
  1351. else {
  1352. DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
  1353. for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
  1354. 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,
  1355. txmsg->reply.u.link_addr.ports[i].input_port,
  1356. txmsg->reply.u.link_addr.ports[i].peer_device_type,
  1357. txmsg->reply.u.link_addr.ports[i].port_number,
  1358. txmsg->reply.u.link_addr.ports[i].dpcd_revision,
  1359. txmsg->reply.u.link_addr.ports[i].mcs,
  1360. txmsg->reply.u.link_addr.ports[i].ddps,
  1361. txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
  1362. txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
  1363. txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
  1364. }
  1365. for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
  1366. drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
  1367. }
  1368. (*mgr->cbs->hotplug)(mgr);
  1369. }
  1370. } else {
  1371. mstb->link_address_sent = false;
  1372. DRM_DEBUG_KMS("link address failed %d\n", ret);
  1373. }
  1374. kfree(txmsg);
  1375. }
  1376. static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
  1377. struct drm_dp_mst_branch *mstb,
  1378. struct drm_dp_mst_port *port)
  1379. {
  1380. int len;
  1381. struct drm_dp_sideband_msg_tx *txmsg;
  1382. int ret;
  1383. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1384. if (!txmsg)
  1385. return -ENOMEM;
  1386. txmsg->dst = mstb;
  1387. len = build_enum_path_resources(txmsg, port->port_num);
  1388. drm_dp_queue_down_tx(mgr, txmsg);
  1389. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1390. if (ret > 0) {
  1391. if (txmsg->reply.reply_type == 1)
  1392. DRM_DEBUG_KMS("enum path resources nak received\n");
  1393. else {
  1394. if (port->port_num != txmsg->reply.u.path_resources.port_number)
  1395. DRM_ERROR("got incorrect port in response\n");
  1396. 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,
  1397. txmsg->reply.u.path_resources.avail_payload_bw_number);
  1398. port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
  1399. }
  1400. }
  1401. kfree(txmsg);
  1402. return 0;
  1403. }
  1404. static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
  1405. struct drm_dp_mst_port *port,
  1406. int id,
  1407. int pbn)
  1408. {
  1409. struct drm_dp_sideband_msg_tx *txmsg;
  1410. struct drm_dp_mst_branch *mstb;
  1411. int len, ret;
  1412. u8 sinks[DRM_DP_MAX_SDP_STREAMS];
  1413. int i;
  1414. mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
  1415. if (!mstb)
  1416. return -EINVAL;
  1417. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1418. if (!txmsg) {
  1419. ret = -ENOMEM;
  1420. goto fail_put;
  1421. }
  1422. for (i = 0; i < port->num_sdp_streams; i++)
  1423. sinks[i] = i;
  1424. txmsg->dst = mstb;
  1425. len = build_allocate_payload(txmsg, port->port_num,
  1426. id,
  1427. pbn, port->num_sdp_streams, sinks);
  1428. drm_dp_queue_down_tx(mgr, txmsg);
  1429. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1430. if (ret > 0) {
  1431. if (txmsg->reply.reply_type == 1) {
  1432. ret = -EINVAL;
  1433. } else
  1434. ret = 0;
  1435. }
  1436. kfree(txmsg);
  1437. fail_put:
  1438. drm_dp_put_mst_branch_device(mstb);
  1439. return ret;
  1440. }
  1441. static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
  1442. int id,
  1443. struct drm_dp_payload *payload)
  1444. {
  1445. int ret;
  1446. ret = drm_dp_dpcd_write_payload(mgr, id, payload);
  1447. if (ret < 0) {
  1448. payload->payload_state = 0;
  1449. return ret;
  1450. }
  1451. payload->payload_state = DP_PAYLOAD_LOCAL;
  1452. return 0;
  1453. }
  1454. static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
  1455. struct drm_dp_mst_port *port,
  1456. int id,
  1457. struct drm_dp_payload *payload)
  1458. {
  1459. int ret;
  1460. ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
  1461. if (ret < 0)
  1462. return ret;
  1463. payload->payload_state = DP_PAYLOAD_REMOTE;
  1464. return ret;
  1465. }
  1466. static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
  1467. struct drm_dp_mst_port *port,
  1468. int id,
  1469. struct drm_dp_payload *payload)
  1470. {
  1471. DRM_DEBUG_KMS("\n");
  1472. /* its okay for these to fail */
  1473. if (port) {
  1474. drm_dp_payload_send_msg(mgr, port, id, 0);
  1475. }
  1476. drm_dp_dpcd_write_payload(mgr, id, payload);
  1477. payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
  1478. return 0;
  1479. }
  1480. static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
  1481. int id,
  1482. struct drm_dp_payload *payload)
  1483. {
  1484. payload->payload_state = 0;
  1485. return 0;
  1486. }
  1487. /**
  1488. * drm_dp_update_payload_part1() - Execute payload update part 1
  1489. * @mgr: manager to use.
  1490. *
  1491. * This iterates over all proposed virtual channels, and tries to
  1492. * allocate space in the link for them. For 0->slots transitions,
  1493. * this step just writes the VCPI to the MST device. For slots->0
  1494. * transitions, this writes the updated VCPIs and removes the
  1495. * remote VC payloads.
  1496. *
  1497. * after calling this the driver should generate ACT and payload
  1498. * packets.
  1499. */
  1500. int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
  1501. {
  1502. int i, j;
  1503. int cur_slots = 1;
  1504. struct drm_dp_payload req_payload;
  1505. struct drm_dp_mst_port *port;
  1506. mutex_lock(&mgr->payload_lock);
  1507. for (i = 0; i < mgr->max_payloads; i++) {
  1508. /* solve the current payloads - compare to the hw ones
  1509. - update the hw view */
  1510. req_payload.start_slot = cur_slots;
  1511. if (mgr->proposed_vcpis[i]) {
  1512. port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
  1513. req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
  1514. req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
  1515. } else {
  1516. port = NULL;
  1517. req_payload.num_slots = 0;
  1518. }
  1519. if (mgr->payloads[i].start_slot != req_payload.start_slot) {
  1520. mgr->payloads[i].start_slot = req_payload.start_slot;
  1521. }
  1522. /* work out what is required to happen with this payload */
  1523. if (mgr->payloads[i].num_slots != req_payload.num_slots) {
  1524. /* need to push an update for this payload */
  1525. if (req_payload.num_slots) {
  1526. drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
  1527. mgr->payloads[i].num_slots = req_payload.num_slots;
  1528. mgr->payloads[i].vcpi = req_payload.vcpi;
  1529. } else if (mgr->payloads[i].num_slots) {
  1530. mgr->payloads[i].num_slots = 0;
  1531. drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]);
  1532. req_payload.payload_state = mgr->payloads[i].payload_state;
  1533. mgr->payloads[i].start_slot = 0;
  1534. }
  1535. mgr->payloads[i].payload_state = req_payload.payload_state;
  1536. }
  1537. cur_slots += req_payload.num_slots;
  1538. }
  1539. for (i = 0; i < mgr->max_payloads; i++) {
  1540. if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
  1541. DRM_DEBUG_KMS("removing payload %d\n", i);
  1542. for (j = i; j < mgr->max_payloads - 1; j++) {
  1543. memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
  1544. mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
  1545. if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
  1546. set_bit(j + 1, &mgr->payload_mask);
  1547. } else {
  1548. clear_bit(j + 1, &mgr->payload_mask);
  1549. }
  1550. }
  1551. memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
  1552. mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
  1553. clear_bit(mgr->max_payloads, &mgr->payload_mask);
  1554. }
  1555. }
  1556. mutex_unlock(&mgr->payload_lock);
  1557. return 0;
  1558. }
  1559. EXPORT_SYMBOL(drm_dp_update_payload_part1);
  1560. /**
  1561. * drm_dp_update_payload_part2() - Execute payload update part 2
  1562. * @mgr: manager to use.
  1563. *
  1564. * This iterates over all proposed virtual channels, and tries to
  1565. * allocate space in the link for them. For 0->slots transitions,
  1566. * this step writes the remote VC payload commands. For slots->0
  1567. * this just resets some internal state.
  1568. */
  1569. int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
  1570. {
  1571. struct drm_dp_mst_port *port;
  1572. int i;
  1573. int ret = 0;
  1574. mutex_lock(&mgr->payload_lock);
  1575. for (i = 0; i < mgr->max_payloads; i++) {
  1576. if (!mgr->proposed_vcpis[i])
  1577. continue;
  1578. port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
  1579. DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
  1580. if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
  1581. ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
  1582. } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
  1583. ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
  1584. }
  1585. if (ret) {
  1586. mutex_unlock(&mgr->payload_lock);
  1587. return ret;
  1588. }
  1589. }
  1590. mutex_unlock(&mgr->payload_lock);
  1591. return 0;
  1592. }
  1593. EXPORT_SYMBOL(drm_dp_update_payload_part2);
  1594. #if 0 /* unused as of yet */
  1595. static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
  1596. struct drm_dp_mst_port *port,
  1597. int offset, int size)
  1598. {
  1599. int len;
  1600. struct drm_dp_sideband_msg_tx *txmsg;
  1601. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1602. if (!txmsg)
  1603. return -ENOMEM;
  1604. len = build_dpcd_read(txmsg, port->port_num, 0, 8);
  1605. txmsg->dst = port->parent;
  1606. drm_dp_queue_down_tx(mgr, txmsg);
  1607. return 0;
  1608. }
  1609. #endif
  1610. static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
  1611. struct drm_dp_mst_port *port,
  1612. int offset, int size, u8 *bytes)
  1613. {
  1614. int len;
  1615. int ret;
  1616. struct drm_dp_sideband_msg_tx *txmsg;
  1617. struct drm_dp_mst_branch *mstb;
  1618. mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
  1619. if (!mstb)
  1620. return -EINVAL;
  1621. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1622. if (!txmsg) {
  1623. ret = -ENOMEM;
  1624. goto fail_put;
  1625. }
  1626. len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
  1627. txmsg->dst = mstb;
  1628. drm_dp_queue_down_tx(mgr, txmsg);
  1629. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  1630. if (ret > 0) {
  1631. if (txmsg->reply.reply_type == 1) {
  1632. ret = -EINVAL;
  1633. } else
  1634. ret = 0;
  1635. }
  1636. kfree(txmsg);
  1637. fail_put:
  1638. drm_dp_put_mst_branch_device(mstb);
  1639. return ret;
  1640. }
  1641. static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
  1642. {
  1643. struct drm_dp_sideband_msg_reply_body reply;
  1644. reply.reply_type = 0;
  1645. reply.req_type = req_type;
  1646. drm_dp_encode_sideband_reply(&reply, msg);
  1647. return 0;
  1648. }
  1649. static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
  1650. struct drm_dp_mst_branch *mstb,
  1651. int req_type, int seqno, bool broadcast)
  1652. {
  1653. struct drm_dp_sideband_msg_tx *txmsg;
  1654. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  1655. if (!txmsg)
  1656. return -ENOMEM;
  1657. txmsg->dst = mstb;
  1658. txmsg->seqno = seqno;
  1659. drm_dp_encode_up_ack_reply(txmsg, req_type);
  1660. mutex_lock(&mgr->qlock);
  1661. process_single_up_tx_qlock(mgr, txmsg);
  1662. mutex_unlock(&mgr->qlock);
  1663. kfree(txmsg);
  1664. return 0;
  1665. }
  1666. static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
  1667. int dp_link_count,
  1668. int *out)
  1669. {
  1670. switch (dp_link_bw) {
  1671. default:
  1672. DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
  1673. dp_link_bw, dp_link_count);
  1674. return false;
  1675. case DP_LINK_BW_1_62:
  1676. *out = 3 * dp_link_count;
  1677. break;
  1678. case DP_LINK_BW_2_7:
  1679. *out = 5 * dp_link_count;
  1680. break;
  1681. case DP_LINK_BW_5_4:
  1682. *out = 10 * dp_link_count;
  1683. break;
  1684. }
  1685. return true;
  1686. }
  1687. /**
  1688. * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
  1689. * @mgr: manager to set state for
  1690. * @mst_state: true to enable MST on this connector - false to disable.
  1691. *
  1692. * This is called by the driver when it detects an MST capable device plugged
  1693. * into a DP MST capable port, or when a DP MST capable device is unplugged.
  1694. */
  1695. int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
  1696. {
  1697. int ret = 0;
  1698. struct drm_dp_mst_branch *mstb = NULL;
  1699. mutex_lock(&mgr->lock);
  1700. if (mst_state == mgr->mst_state)
  1701. goto out_unlock;
  1702. mgr->mst_state = mst_state;
  1703. /* set the device into MST mode */
  1704. if (mst_state) {
  1705. WARN_ON(mgr->mst_primary);
  1706. /* get dpcd info */
  1707. ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
  1708. if (ret != DP_RECEIVER_CAP_SIZE) {
  1709. DRM_DEBUG_KMS("failed to read DPCD\n");
  1710. goto out_unlock;
  1711. }
  1712. if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
  1713. mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
  1714. &mgr->pbn_div)) {
  1715. ret = -EINVAL;
  1716. goto out_unlock;
  1717. }
  1718. mgr->total_pbn = 2560;
  1719. mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
  1720. mgr->avail_slots = mgr->total_slots;
  1721. /* add initial branch device at LCT 1 */
  1722. mstb = drm_dp_add_mst_branch_device(1, NULL);
  1723. if (mstb == NULL) {
  1724. ret = -ENOMEM;
  1725. goto out_unlock;
  1726. }
  1727. mstb->mgr = mgr;
  1728. /* give this the main reference */
  1729. mgr->mst_primary = mstb;
  1730. kref_get(&mgr->mst_primary->kref);
  1731. {
  1732. struct drm_dp_payload reset_pay;
  1733. reset_pay.start_slot = 0;
  1734. reset_pay.num_slots = 0x3f;
  1735. drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
  1736. }
  1737. ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  1738. DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
  1739. if (ret < 0) {
  1740. goto out_unlock;
  1741. }
  1742. /* sort out guid */
  1743. ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, mgr->guid, 16);
  1744. if (ret != 16) {
  1745. DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret);
  1746. goto out_unlock;
  1747. }
  1748. mgr->guid_valid = drm_dp_validate_guid(mgr, mgr->guid);
  1749. if (!mgr->guid_valid) {
  1750. ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, mgr->guid, 16);
  1751. mgr->guid_valid = true;
  1752. }
  1753. queue_work(system_long_wq, &mgr->work);
  1754. ret = 0;
  1755. } else {
  1756. /* disable MST on the device */
  1757. mstb = mgr->mst_primary;
  1758. mgr->mst_primary = NULL;
  1759. /* this can fail if the device is gone */
  1760. drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
  1761. ret = 0;
  1762. memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
  1763. mgr->payload_mask = 0;
  1764. set_bit(0, &mgr->payload_mask);
  1765. mgr->vcpi_mask = 0;
  1766. }
  1767. out_unlock:
  1768. mutex_unlock(&mgr->lock);
  1769. if (mstb)
  1770. drm_dp_put_mst_branch_device(mstb);
  1771. return ret;
  1772. }
  1773. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
  1774. /**
  1775. * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
  1776. * @mgr: manager to suspend
  1777. *
  1778. * This function tells the MST device that we can't handle UP messages
  1779. * anymore. This should stop it from sending any since we are suspended.
  1780. */
  1781. void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
  1782. {
  1783. mutex_lock(&mgr->lock);
  1784. drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  1785. DP_MST_EN | DP_UPSTREAM_IS_SRC);
  1786. mutex_unlock(&mgr->lock);
  1787. flush_work(&mgr->work);
  1788. flush_work(&mgr->destroy_connector_work);
  1789. }
  1790. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
  1791. /**
  1792. * drm_dp_mst_topology_mgr_resume() - resume the MST manager
  1793. * @mgr: manager to resume
  1794. *
  1795. * This will fetch DPCD and see if the device is still there,
  1796. * if it is, it will rewrite the MSTM control bits, and return.
  1797. *
  1798. * if the device fails this returns -1, and the driver should do
  1799. * a full MST reprobe, in case we were undocked.
  1800. */
  1801. int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
  1802. {
  1803. int ret = 0;
  1804. mutex_lock(&mgr->lock);
  1805. if (mgr->mst_primary) {
  1806. int sret;
  1807. sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
  1808. if (sret != DP_RECEIVER_CAP_SIZE) {
  1809. DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
  1810. ret = -1;
  1811. goto out_unlock;
  1812. }
  1813. ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
  1814. DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
  1815. if (ret < 0) {
  1816. DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
  1817. ret = -1;
  1818. goto out_unlock;
  1819. }
  1820. ret = 0;
  1821. } else
  1822. ret = -1;
  1823. out_unlock:
  1824. mutex_unlock(&mgr->lock);
  1825. return ret;
  1826. }
  1827. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
  1828. static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
  1829. {
  1830. int len;
  1831. u8 replyblock[32];
  1832. int replylen, origlen, curreply;
  1833. int ret;
  1834. struct drm_dp_sideband_msg_rx *msg;
  1835. int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
  1836. msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
  1837. len = min(mgr->max_dpcd_transaction_bytes, 16);
  1838. ret = drm_dp_dpcd_read(mgr->aux, basereg,
  1839. replyblock, len);
  1840. if (ret != len) {
  1841. DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
  1842. return;
  1843. }
  1844. ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
  1845. if (!ret) {
  1846. DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
  1847. return;
  1848. }
  1849. replylen = msg->curchunk_len + msg->curchunk_hdrlen;
  1850. origlen = replylen;
  1851. replylen -= len;
  1852. curreply = len;
  1853. while (replylen > 0) {
  1854. len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
  1855. ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
  1856. replyblock, len);
  1857. if (ret != len) {
  1858. DRM_DEBUG_KMS("failed to read a chunk\n");
  1859. }
  1860. ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
  1861. if (ret == false)
  1862. DRM_DEBUG_KMS("failed to build sideband msg\n");
  1863. curreply += len;
  1864. replylen -= len;
  1865. }
  1866. }
  1867. static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
  1868. {
  1869. int ret = 0;
  1870. drm_dp_get_one_sb_msg(mgr, false);
  1871. if (mgr->down_rep_recv.have_eomt) {
  1872. struct drm_dp_sideband_msg_tx *txmsg;
  1873. struct drm_dp_mst_branch *mstb;
  1874. int slot = -1;
  1875. mstb = drm_dp_get_mst_branch_device(mgr,
  1876. mgr->down_rep_recv.initial_hdr.lct,
  1877. mgr->down_rep_recv.initial_hdr.rad);
  1878. if (!mstb) {
  1879. DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
  1880. memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1881. return 0;
  1882. }
  1883. /* find the message */
  1884. slot = mgr->down_rep_recv.initial_hdr.seqno;
  1885. mutex_lock(&mgr->qlock);
  1886. txmsg = mstb->tx_slots[slot];
  1887. /* remove from slots */
  1888. mutex_unlock(&mgr->qlock);
  1889. if (!txmsg) {
  1890. DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
  1891. mstb,
  1892. mgr->down_rep_recv.initial_hdr.seqno,
  1893. mgr->down_rep_recv.initial_hdr.lct,
  1894. mgr->down_rep_recv.initial_hdr.rad[0],
  1895. mgr->down_rep_recv.msg[0]);
  1896. drm_dp_put_mst_branch_device(mstb);
  1897. memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1898. return 0;
  1899. }
  1900. drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
  1901. if (txmsg->reply.reply_type == 1) {
  1902. 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);
  1903. }
  1904. memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1905. drm_dp_put_mst_branch_device(mstb);
  1906. mutex_lock(&mgr->qlock);
  1907. txmsg->state = DRM_DP_SIDEBAND_TX_RX;
  1908. mstb->tx_slots[slot] = NULL;
  1909. mutex_unlock(&mgr->qlock);
  1910. wake_up(&mgr->tx_waitq);
  1911. }
  1912. return ret;
  1913. }
  1914. static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
  1915. {
  1916. int ret = 0;
  1917. drm_dp_get_one_sb_msg(mgr, true);
  1918. if (mgr->up_req_recv.have_eomt) {
  1919. struct drm_dp_sideband_msg_req_body msg;
  1920. struct drm_dp_mst_branch *mstb = NULL;
  1921. bool seqno;
  1922. if (!mgr->up_req_recv.initial_hdr.broadcast) {
  1923. mstb = drm_dp_get_mst_branch_device(mgr,
  1924. mgr->up_req_recv.initial_hdr.lct,
  1925. mgr->up_req_recv.initial_hdr.rad);
  1926. if (!mstb) {
  1927. DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
  1928. memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1929. return 0;
  1930. }
  1931. }
  1932. seqno = mgr->up_req_recv.initial_hdr.seqno;
  1933. drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
  1934. if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
  1935. drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
  1936. if (!mstb)
  1937. mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
  1938. if (!mstb) {
  1939. DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
  1940. memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1941. return 0;
  1942. }
  1943. drm_dp_update_port(mstb, &msg.u.conn_stat);
  1944. 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);
  1945. (*mgr->cbs->hotplug)(mgr);
  1946. } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
  1947. drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
  1948. if (!mstb)
  1949. mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
  1950. if (!mstb) {
  1951. DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
  1952. memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1953. return 0;
  1954. }
  1955. DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
  1956. }
  1957. drm_dp_put_mst_branch_device(mstb);
  1958. memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
  1959. }
  1960. return ret;
  1961. }
  1962. /**
  1963. * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
  1964. * @mgr: manager to notify irq for.
  1965. * @esi: 4 bytes from SINK_COUNT_ESI
  1966. * @handled: whether the hpd interrupt was consumed or not
  1967. *
  1968. * This should be called from the driver when it detects a short IRQ,
  1969. * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
  1970. * topology manager will process the sideband messages received as a result
  1971. * of this.
  1972. */
  1973. int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
  1974. {
  1975. int ret = 0;
  1976. int sc;
  1977. *handled = false;
  1978. sc = esi[0] & 0x3f;
  1979. if (sc != mgr->sink_count) {
  1980. mgr->sink_count = sc;
  1981. *handled = true;
  1982. }
  1983. if (esi[1] & DP_DOWN_REP_MSG_RDY) {
  1984. ret = drm_dp_mst_handle_down_rep(mgr);
  1985. *handled = true;
  1986. }
  1987. if (esi[1] & DP_UP_REQ_MSG_RDY) {
  1988. ret |= drm_dp_mst_handle_up_req(mgr);
  1989. *handled = true;
  1990. }
  1991. drm_dp_mst_kick_tx(mgr);
  1992. return ret;
  1993. }
  1994. EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
  1995. /**
  1996. * drm_dp_mst_detect_port() - get connection status for an MST port
  1997. * @mgr: manager for this port
  1998. * @port: unverified pointer to a port
  1999. *
  2000. * This returns the current connection state for a port. It validates the
  2001. * port pointer still exists so the caller doesn't require a reference
  2002. */
  2003. enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
  2004. struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  2005. {
  2006. enum drm_connector_status status = connector_status_disconnected;
  2007. /* we need to search for the port in the mgr in case its gone */
  2008. port = drm_dp_get_validated_port_ref(mgr, port);
  2009. if (!port)
  2010. return connector_status_disconnected;
  2011. if (!port->ddps)
  2012. goto out;
  2013. switch (port->pdt) {
  2014. case DP_PEER_DEVICE_NONE:
  2015. case DP_PEER_DEVICE_MST_BRANCHING:
  2016. break;
  2017. case DP_PEER_DEVICE_SST_SINK:
  2018. status = connector_status_connected;
  2019. /* for logical ports - cache the EDID */
  2020. if (port->port_num >= 8 && !port->cached_edid) {
  2021. port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
  2022. }
  2023. break;
  2024. case DP_PEER_DEVICE_DP_LEGACY_CONV:
  2025. if (port->ldps)
  2026. status = connector_status_connected;
  2027. break;
  2028. }
  2029. out:
  2030. drm_dp_put_port(port);
  2031. return status;
  2032. }
  2033. EXPORT_SYMBOL(drm_dp_mst_detect_port);
  2034. /**
  2035. * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
  2036. * @mgr: manager for this port
  2037. * @port: unverified pointer to a port.
  2038. *
  2039. * This returns whether the port supports audio or not.
  2040. */
  2041. bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
  2042. struct drm_dp_mst_port *port)
  2043. {
  2044. bool ret = false;
  2045. port = drm_dp_get_validated_port_ref(mgr, port);
  2046. if (!port)
  2047. return ret;
  2048. ret = port->has_audio;
  2049. drm_dp_put_port(port);
  2050. return ret;
  2051. }
  2052. EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
  2053. /**
  2054. * drm_dp_mst_get_edid() - get EDID for an MST port
  2055. * @connector: toplevel connector to get EDID for
  2056. * @mgr: manager for this port
  2057. * @port: unverified pointer to a port.
  2058. *
  2059. * This returns an EDID for the port connected to a connector,
  2060. * It validates the pointer still exists so the caller doesn't require a
  2061. * reference.
  2062. */
  2063. struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  2064. {
  2065. struct edid *edid = NULL;
  2066. /* we need to search for the port in the mgr in case its gone */
  2067. port = drm_dp_get_validated_port_ref(mgr, port);
  2068. if (!port)
  2069. return NULL;
  2070. if (port->cached_edid)
  2071. edid = drm_edid_duplicate(port->cached_edid);
  2072. else {
  2073. edid = drm_get_edid(connector, &port->aux.ddc);
  2074. drm_mode_connector_set_tile_property(connector);
  2075. }
  2076. port->has_audio = drm_detect_monitor_audio(edid);
  2077. drm_dp_put_port(port);
  2078. return edid;
  2079. }
  2080. EXPORT_SYMBOL(drm_dp_mst_get_edid);
  2081. /**
  2082. * drm_dp_find_vcpi_slots() - find slots for this PBN value
  2083. * @mgr: manager to use
  2084. * @pbn: payload bandwidth to convert into slots.
  2085. */
  2086. int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
  2087. int pbn)
  2088. {
  2089. int num_slots;
  2090. num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
  2091. if (num_slots > mgr->avail_slots)
  2092. return -ENOSPC;
  2093. return num_slots;
  2094. }
  2095. EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
  2096. static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
  2097. struct drm_dp_vcpi *vcpi, int pbn)
  2098. {
  2099. int num_slots;
  2100. int ret;
  2101. num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
  2102. if (num_slots > mgr->avail_slots)
  2103. return -ENOSPC;
  2104. vcpi->pbn = pbn;
  2105. vcpi->aligned_pbn = num_slots * mgr->pbn_div;
  2106. vcpi->num_slots = num_slots;
  2107. ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
  2108. if (ret < 0)
  2109. return ret;
  2110. return 0;
  2111. }
  2112. /**
  2113. * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
  2114. * @mgr: manager for this port
  2115. * @port: port to allocate a virtual channel for.
  2116. * @pbn: payload bandwidth number to request
  2117. * @slots: returned number of slots for this PBN.
  2118. */
  2119. bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
  2120. {
  2121. int ret;
  2122. port = drm_dp_get_validated_port_ref(mgr, port);
  2123. if (!port)
  2124. return false;
  2125. if (port->vcpi.vcpi > 0) {
  2126. DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
  2127. if (pbn == port->vcpi.pbn) {
  2128. *slots = port->vcpi.num_slots;
  2129. return true;
  2130. }
  2131. }
  2132. ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
  2133. if (ret) {
  2134. DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
  2135. goto out;
  2136. }
  2137. DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
  2138. *slots = port->vcpi.num_slots;
  2139. drm_dp_put_port(port);
  2140. return true;
  2141. out:
  2142. return false;
  2143. }
  2144. EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
  2145. int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  2146. {
  2147. int slots = 0;
  2148. port = drm_dp_get_validated_port_ref(mgr, port);
  2149. if (!port)
  2150. return slots;
  2151. slots = port->vcpi.num_slots;
  2152. drm_dp_put_port(port);
  2153. return slots;
  2154. }
  2155. EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
  2156. /**
  2157. * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
  2158. * @mgr: manager for this port
  2159. * @port: unverified pointer to a port.
  2160. *
  2161. * This just resets the number of slots for the ports VCPI for later programming.
  2162. */
  2163. void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  2164. {
  2165. port = drm_dp_get_validated_port_ref(mgr, port);
  2166. if (!port)
  2167. return;
  2168. port->vcpi.num_slots = 0;
  2169. drm_dp_put_port(port);
  2170. }
  2171. EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
  2172. /**
  2173. * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
  2174. * @mgr: manager for this port
  2175. * @port: unverified port to deallocate vcpi for
  2176. */
  2177. void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
  2178. {
  2179. port = drm_dp_get_validated_port_ref(mgr, port);
  2180. if (!port)
  2181. return;
  2182. drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
  2183. port->vcpi.num_slots = 0;
  2184. port->vcpi.pbn = 0;
  2185. port->vcpi.aligned_pbn = 0;
  2186. port->vcpi.vcpi = 0;
  2187. drm_dp_put_port(port);
  2188. }
  2189. EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
  2190. static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
  2191. int id, struct drm_dp_payload *payload)
  2192. {
  2193. u8 payload_alloc[3], status;
  2194. int ret;
  2195. int retries = 0;
  2196. drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
  2197. DP_PAYLOAD_TABLE_UPDATED);
  2198. payload_alloc[0] = id;
  2199. payload_alloc[1] = payload->start_slot;
  2200. payload_alloc[2] = payload->num_slots;
  2201. ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
  2202. if (ret != 3) {
  2203. DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
  2204. goto fail;
  2205. }
  2206. retry:
  2207. ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
  2208. if (ret < 0) {
  2209. DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
  2210. goto fail;
  2211. }
  2212. if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
  2213. retries++;
  2214. if (retries < 20) {
  2215. usleep_range(10000, 20000);
  2216. goto retry;
  2217. }
  2218. DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
  2219. ret = -EINVAL;
  2220. goto fail;
  2221. }
  2222. ret = 0;
  2223. fail:
  2224. return ret;
  2225. }
  2226. /**
  2227. * drm_dp_check_act_status() - Check ACT handled status.
  2228. * @mgr: manager to use
  2229. *
  2230. * Check the payload status bits in the DPCD for ACT handled completion.
  2231. */
  2232. int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
  2233. {
  2234. u8 status;
  2235. int ret;
  2236. int count = 0;
  2237. do {
  2238. ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
  2239. if (ret < 0) {
  2240. DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
  2241. goto fail;
  2242. }
  2243. if (status & DP_PAYLOAD_ACT_HANDLED)
  2244. break;
  2245. count++;
  2246. udelay(100);
  2247. } while (count < 30);
  2248. if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
  2249. DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
  2250. ret = -EINVAL;
  2251. goto fail;
  2252. }
  2253. return 0;
  2254. fail:
  2255. return ret;
  2256. }
  2257. EXPORT_SYMBOL(drm_dp_check_act_status);
  2258. /**
  2259. * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
  2260. * @clock: dot clock for the mode
  2261. * @bpp: bpp for the mode.
  2262. *
  2263. * This uses the formula in the spec to calculate the PBN value for a mode.
  2264. */
  2265. int drm_dp_calc_pbn_mode(int clock, int bpp)
  2266. {
  2267. fixed20_12 pix_bw;
  2268. fixed20_12 fbpp;
  2269. fixed20_12 result;
  2270. fixed20_12 margin, tmp;
  2271. u32 res;
  2272. pix_bw.full = dfixed_const(clock);
  2273. fbpp.full = dfixed_const(bpp);
  2274. tmp.full = dfixed_const(8);
  2275. fbpp.full = dfixed_div(fbpp, tmp);
  2276. result.full = dfixed_mul(pix_bw, fbpp);
  2277. margin.full = dfixed_const(54);
  2278. tmp.full = dfixed_const(64);
  2279. margin.full = dfixed_div(margin, tmp);
  2280. result.full = dfixed_div(result, margin);
  2281. margin.full = dfixed_const(1006);
  2282. tmp.full = dfixed_const(1000);
  2283. margin.full = dfixed_div(margin, tmp);
  2284. result.full = dfixed_mul(result, margin);
  2285. result.full = dfixed_div(result, tmp);
  2286. result.full = dfixed_ceil(result);
  2287. res = dfixed_trunc(result);
  2288. return res;
  2289. }
  2290. EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
  2291. static int test_calc_pbn_mode(void)
  2292. {
  2293. int ret;
  2294. ret = drm_dp_calc_pbn_mode(154000, 30);
  2295. if (ret != 689)
  2296. return -EINVAL;
  2297. ret = drm_dp_calc_pbn_mode(234000, 30);
  2298. if (ret != 1047)
  2299. return -EINVAL;
  2300. return 0;
  2301. }
  2302. /* we want to kick the TX after we've ack the up/down IRQs. */
  2303. static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
  2304. {
  2305. queue_work(system_long_wq, &mgr->tx_work);
  2306. }
  2307. static void drm_dp_mst_dump_mstb(struct seq_file *m,
  2308. struct drm_dp_mst_branch *mstb)
  2309. {
  2310. struct drm_dp_mst_port *port;
  2311. int tabs = mstb->lct;
  2312. char prefix[10];
  2313. int i;
  2314. for (i = 0; i < tabs; i++)
  2315. prefix[i] = '\t';
  2316. prefix[i] = '\0';
  2317. seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
  2318. list_for_each_entry(port, &mstb->ports, next) {
  2319. seq_printf(m, "%sport: %d: ddps: %d ldps: %d, sdp: %d/%d, %p, conn: %p\n", prefix, port->port_num, port->ddps, port->ldps, port->num_sdp_streams, port->num_sdp_stream_sinks, port, port->connector);
  2320. if (port->mstb)
  2321. drm_dp_mst_dump_mstb(m, port->mstb);
  2322. }
  2323. }
  2324. static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
  2325. char *buf)
  2326. {
  2327. int ret;
  2328. int i;
  2329. for (i = 0; i < 4; i++) {
  2330. ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
  2331. if (ret != 16)
  2332. break;
  2333. }
  2334. if (i == 4)
  2335. return true;
  2336. return false;
  2337. }
  2338. /**
  2339. * drm_dp_mst_dump_topology(): dump topology to seq file.
  2340. * @m: seq_file to dump output to
  2341. * @mgr: manager to dump current topology for.
  2342. *
  2343. * helper to dump MST topology to a seq file for debugfs.
  2344. */
  2345. void drm_dp_mst_dump_topology(struct seq_file *m,
  2346. struct drm_dp_mst_topology_mgr *mgr)
  2347. {
  2348. int i;
  2349. struct drm_dp_mst_port *port;
  2350. mutex_lock(&mgr->lock);
  2351. if (mgr->mst_primary)
  2352. drm_dp_mst_dump_mstb(m, mgr->mst_primary);
  2353. /* dump VCPIs */
  2354. mutex_unlock(&mgr->lock);
  2355. mutex_lock(&mgr->payload_lock);
  2356. seq_printf(m, "vcpi: %lx %lx\n", mgr->payload_mask, mgr->vcpi_mask);
  2357. for (i = 0; i < mgr->max_payloads; i++) {
  2358. if (mgr->proposed_vcpis[i]) {
  2359. port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
  2360. seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots);
  2361. } else
  2362. seq_printf(m, "vcpi %d:unsed\n", i);
  2363. }
  2364. for (i = 0; i < mgr->max_payloads; i++) {
  2365. seq_printf(m, "payload %d: %d, %d, %d\n",
  2366. i,
  2367. mgr->payloads[i].payload_state,
  2368. mgr->payloads[i].start_slot,
  2369. mgr->payloads[i].num_slots);
  2370. }
  2371. mutex_unlock(&mgr->payload_lock);
  2372. mutex_lock(&mgr->lock);
  2373. if (mgr->mst_primary) {
  2374. u8 buf[64];
  2375. bool bret;
  2376. int ret;
  2377. ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
  2378. seq_printf(m, "dpcd: ");
  2379. for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
  2380. seq_printf(m, "%02x ", buf[i]);
  2381. seq_printf(m, "\n");
  2382. ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
  2383. seq_printf(m, "faux/mst: ");
  2384. for (i = 0; i < 2; i++)
  2385. seq_printf(m, "%02x ", buf[i]);
  2386. seq_printf(m, "\n");
  2387. ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
  2388. seq_printf(m, "mst ctrl: ");
  2389. for (i = 0; i < 1; i++)
  2390. seq_printf(m, "%02x ", buf[i]);
  2391. seq_printf(m, "\n");
  2392. /* dump the standard OUI branch header */
  2393. ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
  2394. seq_printf(m, "branch oui: ");
  2395. for (i = 0; i < 0x3; i++)
  2396. seq_printf(m, "%02x", buf[i]);
  2397. seq_printf(m, " devid: ");
  2398. for (i = 0x3; i < 0x8; i++)
  2399. seq_printf(m, "%c", buf[i]);
  2400. seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
  2401. seq_printf(m, "\n");
  2402. bret = dump_dp_payload_table(mgr, buf);
  2403. if (bret == true) {
  2404. seq_printf(m, "payload table: ");
  2405. for (i = 0; i < 63; i++)
  2406. seq_printf(m, "%02x ", buf[i]);
  2407. seq_printf(m, "\n");
  2408. }
  2409. }
  2410. mutex_unlock(&mgr->lock);
  2411. }
  2412. EXPORT_SYMBOL(drm_dp_mst_dump_topology);
  2413. static void drm_dp_tx_work(struct work_struct *work)
  2414. {
  2415. struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
  2416. mutex_lock(&mgr->qlock);
  2417. if (mgr->tx_down_in_progress)
  2418. process_single_down_tx_qlock(mgr);
  2419. mutex_unlock(&mgr->qlock);
  2420. }
  2421. static void drm_dp_destroy_connector_work(struct work_struct *work)
  2422. {
  2423. struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
  2424. struct drm_dp_mst_port *port;
  2425. bool send_hotplug = false;
  2426. /*
  2427. * Not a regular list traverse as we have to drop the destroy
  2428. * connector lock before destroying the connector, to avoid AB->BA
  2429. * ordering between this lock and the config mutex.
  2430. */
  2431. for (;;) {
  2432. mutex_lock(&mgr->destroy_connector_lock);
  2433. port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
  2434. if (!port) {
  2435. mutex_unlock(&mgr->destroy_connector_lock);
  2436. break;
  2437. }
  2438. list_del(&port->next);
  2439. mutex_unlock(&mgr->destroy_connector_lock);
  2440. mgr->cbs->destroy_connector(mgr, port->connector);
  2441. drm_dp_port_teardown_pdt(port, port->pdt);
  2442. if (!port->input && port->vcpi.vcpi > 0)
  2443. drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
  2444. kfree(port);
  2445. send_hotplug = true;
  2446. }
  2447. if (send_hotplug)
  2448. (*mgr->cbs->hotplug)(mgr);
  2449. }
  2450. /**
  2451. * drm_dp_mst_topology_mgr_init - initialise a topology manager
  2452. * @mgr: manager struct to initialise
  2453. * @dev: device providing this structure - for i2c addition.
  2454. * @aux: DP helper aux channel to talk to this device
  2455. * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
  2456. * @max_payloads: maximum number of payloads this GPU can source
  2457. * @conn_base_id: the connector object ID the MST device is connected to.
  2458. *
  2459. * Return 0 for success, or negative error code on failure
  2460. */
  2461. int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
  2462. struct device *dev, struct drm_dp_aux *aux,
  2463. int max_dpcd_transaction_bytes,
  2464. int max_payloads, int conn_base_id)
  2465. {
  2466. mutex_init(&mgr->lock);
  2467. mutex_init(&mgr->qlock);
  2468. mutex_init(&mgr->payload_lock);
  2469. mutex_init(&mgr->destroy_connector_lock);
  2470. INIT_LIST_HEAD(&mgr->tx_msg_downq);
  2471. INIT_LIST_HEAD(&mgr->destroy_connector_list);
  2472. INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
  2473. INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
  2474. INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
  2475. init_waitqueue_head(&mgr->tx_waitq);
  2476. mgr->dev = dev;
  2477. mgr->aux = aux;
  2478. mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
  2479. mgr->max_payloads = max_payloads;
  2480. mgr->conn_base_id = conn_base_id;
  2481. mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
  2482. if (!mgr->payloads)
  2483. return -ENOMEM;
  2484. mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
  2485. if (!mgr->proposed_vcpis)
  2486. return -ENOMEM;
  2487. set_bit(0, &mgr->payload_mask);
  2488. test_calc_pbn_mode();
  2489. return 0;
  2490. }
  2491. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
  2492. /**
  2493. * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
  2494. * @mgr: manager to destroy
  2495. */
  2496. void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
  2497. {
  2498. flush_work(&mgr->work);
  2499. flush_work(&mgr->destroy_connector_work);
  2500. mutex_lock(&mgr->payload_lock);
  2501. kfree(mgr->payloads);
  2502. mgr->payloads = NULL;
  2503. kfree(mgr->proposed_vcpis);
  2504. mgr->proposed_vcpis = NULL;
  2505. mutex_unlock(&mgr->payload_lock);
  2506. mgr->dev = NULL;
  2507. mgr->aux = NULL;
  2508. }
  2509. EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
  2510. /* I2C device */
  2511. static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
  2512. int num)
  2513. {
  2514. struct drm_dp_aux *aux = adapter->algo_data;
  2515. struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
  2516. struct drm_dp_mst_branch *mstb;
  2517. struct drm_dp_mst_topology_mgr *mgr = port->mgr;
  2518. unsigned int i;
  2519. bool reading = false;
  2520. struct drm_dp_sideband_msg_req_body msg;
  2521. struct drm_dp_sideband_msg_tx *txmsg = NULL;
  2522. int ret;
  2523. mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
  2524. if (!mstb)
  2525. return -EREMOTEIO;
  2526. /* construct i2c msg */
  2527. /* see if last msg is a read */
  2528. if (msgs[num - 1].flags & I2C_M_RD)
  2529. reading = true;
  2530. if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
  2531. DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
  2532. ret = -EIO;
  2533. goto out;
  2534. }
  2535. memset(&msg, 0, sizeof(msg));
  2536. msg.req_type = DP_REMOTE_I2C_READ;
  2537. msg.u.i2c_read.num_transactions = num - 1;
  2538. msg.u.i2c_read.port_number = port->port_num;
  2539. for (i = 0; i < num - 1; i++) {
  2540. msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
  2541. msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
  2542. msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
  2543. }
  2544. msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
  2545. msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
  2546. txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
  2547. if (!txmsg) {
  2548. ret = -ENOMEM;
  2549. goto out;
  2550. }
  2551. txmsg->dst = mstb;
  2552. drm_dp_encode_sideband_req(&msg, txmsg);
  2553. drm_dp_queue_down_tx(mgr, txmsg);
  2554. ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
  2555. if (ret > 0) {
  2556. if (txmsg->reply.reply_type == 1) { /* got a NAK back */
  2557. ret = -EREMOTEIO;
  2558. goto out;
  2559. }
  2560. if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
  2561. ret = -EIO;
  2562. goto out;
  2563. }
  2564. memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
  2565. ret = num;
  2566. }
  2567. out:
  2568. kfree(txmsg);
  2569. drm_dp_put_mst_branch_device(mstb);
  2570. return ret;
  2571. }
  2572. static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
  2573. {
  2574. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
  2575. I2C_FUNC_SMBUS_READ_BLOCK_DATA |
  2576. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  2577. I2C_FUNC_10BIT_ADDR;
  2578. }
  2579. static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
  2580. .functionality = drm_dp_mst_i2c_functionality,
  2581. .master_xfer = drm_dp_mst_i2c_xfer,
  2582. };
  2583. /**
  2584. * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
  2585. * @aux: DisplayPort AUX channel
  2586. *
  2587. * Returns 0 on success or a negative error code on failure.
  2588. */
  2589. static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
  2590. {
  2591. aux->ddc.algo = &drm_dp_mst_i2c_algo;
  2592. aux->ddc.algo_data = aux;
  2593. aux->ddc.retries = 3;
  2594. aux->ddc.class = I2C_CLASS_DDC;
  2595. aux->ddc.owner = THIS_MODULE;
  2596. aux->ddc.dev.parent = aux->dev;
  2597. aux->ddc.dev.of_node = aux->dev->of_node;
  2598. strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
  2599. sizeof(aux->ddc.name));
  2600. return i2c_add_adapter(&aux->ddc);
  2601. }
  2602. /**
  2603. * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
  2604. * @aux: DisplayPort AUX channel
  2605. */
  2606. static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
  2607. {
  2608. i2c_del_adapter(&aux->ddc);
  2609. }