drm_dp_mst_topology.c 83 KB

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