drm_dp_mst_topology.c 74 KB

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