dp_mst_sim_helper.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*
  2. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. /*
  16. * Copyright © 2014 Red Hat
  17. *
  18. * Permission to use, copy, modify, distribute, and sell this software and its
  19. * documentation for any purpose is hereby granted without fee, provided that
  20. * the above copyright notice appear in all copies and that both that copyright
  21. * notice and this permission notice appear in supporting documentation, and
  22. * that the name of the copyright holders not be used in advertising or
  23. * publicity pertaining to distribution of the software without specific,
  24. * written prior permission. The copyright holders make no representations
  25. * about the suitability of this software for any purpose. It is provided "as
  26. * is" without express or implied warranty.
  27. *
  28. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  29. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  30. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  31. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  32. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  33. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  34. * OF THIS SOFTWARE.
  35. */
  36. #include <linux/types.h>
  37. #include <linux/completion.h>
  38. #include <linux/delay.h>
  39. #include <drm/drm_fixed.h>
  40. #include <drm/drm_edid.h>
  41. #include <drm/drm_dp_mst_helper.h>
  42. #include "dp_mst_sim_helper.h"
  43. #include "dp_debug.h"
  44. #define DP_MST_DEBUG(fmt, ...) DP_DEBUG(fmt, ##__VA_ARGS__)
  45. #define DP_MST_INFO(fmt, ...) DP_INFO(fmt, ##__VA_ARGS__)
  46. #define DP_MST_DEBUG_V(fmt, ...) DP_DEBUG_V(fmt, ##__VA_ARGS__)
  47. #define DP_MST_INFO_V(fmt, ...) DP_INFO_V(fmt, ##__VA_ARGS__)
  48. #define DDC_SEGMENT_ADDR 0x30
  49. struct dp_mst_sim_context {
  50. void *host_dev;
  51. void (*host_hpd_irq)(void *host_dev);
  52. void (*host_req)(void *host_dev, const u8 *in, int in_size,
  53. u8 *out, int *out_size);
  54. struct dp_mst_sim_port *ports;
  55. u32 port_num;
  56. struct drm_dp_sideband_msg_rx down_req;
  57. struct drm_dp_sideband_msg_rx down_rep;
  58. struct mutex session_lock;
  59. struct completion session_comp;
  60. struct workqueue_struct *wq;
  61. int reset_cnt;
  62. u8 esi[16];
  63. u8 guid[16];
  64. u8 dpcd[1024];
  65. };
  66. struct dp_mst_sim_work {
  67. struct work_struct base;
  68. struct dp_mst_sim_context *ctx;
  69. unsigned int address;
  70. u8 buffer[256];
  71. size_t size;
  72. };
  73. struct dp_mst_notify_work {
  74. struct work_struct base;
  75. struct dp_mst_sim_context *ctx;
  76. u32 port_mask;
  77. };
  78. #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG)
  79. static void dp_sideband_hex_dump(const char *name,
  80. u32 address, u8 *buffer, size_t size)
  81. {
  82. char prefix[64];
  83. int i, linelen, remaining = size;
  84. const int rowsize = 16;
  85. u8 linebuf[64];
  86. snprintf(prefix, sizeof(prefix), "%s(%d) %4xh(%2zu): ",
  87. name, current->pid, address, size);
  88. for (i = 0; i < size; i += rowsize) {
  89. linelen = min(remaining, rowsize);
  90. remaining -= rowsize;
  91. hex_dump_to_buffer(buffer + i, linelen, rowsize, 1,
  92. linebuf, sizeof(linebuf), false);
  93. DP_MST_DEBUG_V("%s%s\n", prefix, linebuf);
  94. }
  95. }
  96. #else
  97. static void dp_sideband_hex_dump(const char *name,
  98. u32 address, u8 *buffer, size_t size)
  99. {
  100. }
  101. #endif /* CONFIG_DYNAMIC_DEBUG */
  102. static u8 dp_mst_sim_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
  103. {
  104. u8 bitmask = 0x80;
  105. u8 bitshift = 7;
  106. u8 array_index = 0;
  107. int number_of_bits = num_nibbles * 4;
  108. u8 remainder = 0;
  109. while (number_of_bits != 0) {
  110. number_of_bits--;
  111. remainder <<= 1;
  112. remainder |= (data[array_index] & bitmask) >> bitshift;
  113. bitmask >>= 1;
  114. bitshift--;
  115. if (bitmask == 0) {
  116. bitmask = 0x80;
  117. bitshift = 7;
  118. array_index++;
  119. }
  120. if ((remainder & 0x10) == 0x10)
  121. remainder ^= 0x13;
  122. }
  123. number_of_bits = 4;
  124. while (number_of_bits != 0) {
  125. number_of_bits--;
  126. remainder <<= 1;
  127. if ((remainder & 0x10) != 0)
  128. remainder ^= 0x13;
  129. }
  130. return remainder;
  131. }
  132. static u8 dp_mst_sim_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
  133. {
  134. u8 bitmask = 0x80;
  135. u8 bitshift = 7;
  136. u8 array_index = 0;
  137. int number_of_bits = number_of_bytes * 8;
  138. u16 remainder = 0;
  139. while (number_of_bits != 0) {
  140. number_of_bits--;
  141. remainder <<= 1;
  142. remainder |= (data[array_index] & bitmask) >> bitshift;
  143. bitmask >>= 1;
  144. bitshift--;
  145. if (bitmask == 0) {
  146. bitmask = 0x80;
  147. bitshift = 7;
  148. array_index++;
  149. }
  150. if ((remainder & 0x100) == 0x100)
  151. remainder ^= 0xd5;
  152. }
  153. number_of_bits = 8;
  154. while (number_of_bits != 0) {
  155. number_of_bits--;
  156. remainder <<= 1;
  157. if ((remainder & 0x100) != 0)
  158. remainder ^= 0xd5;
  159. }
  160. return remainder & 0xff;
  161. }
  162. static bool dp_mst_sim_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
  163. u8 *buf, int buflen, u8 *hdrlen)
  164. {
  165. u8 crc4;
  166. u8 len;
  167. int i;
  168. u8 idx;
  169. if (buf[0] == 0)
  170. return false;
  171. len = 3;
  172. len += ((buf[0] & 0xf0) >> 4) / 2;
  173. if (len > buflen)
  174. return false;
  175. crc4 = dp_mst_sim_msg_header_crc4(buf, (len * 2) - 1);
  176. if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
  177. DP_MST_DEBUG("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
  178. return false;
  179. }
  180. hdr->lct = (buf[0] & 0xf0) >> 4;
  181. hdr->lcr = (buf[0] & 0xf);
  182. idx = 1;
  183. for (i = 0; i < (hdr->lct / 2); i++)
  184. hdr->rad[i] = buf[idx++];
  185. hdr->broadcast = (buf[idx] >> 7) & 0x1;
  186. hdr->path_msg = (buf[idx] >> 6) & 0x1;
  187. hdr->msg_len = buf[idx] & 0x3f;
  188. idx++;
  189. hdr->somt = (buf[idx] >> 7) & 0x1;
  190. hdr->eomt = (buf[idx] >> 6) & 0x1;
  191. hdr->seqno = (buf[idx] >> 4) & 0x1;
  192. idx++;
  193. *hdrlen = idx;
  194. return true;
  195. }
  196. static bool dp_mst_sim_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
  197. u8 *replybuf, u8 replybuflen, bool hdr)
  198. {
  199. int ret;
  200. u8 crc4;
  201. if (hdr) {
  202. u8 hdrlen;
  203. struct drm_dp_sideband_msg_hdr recv_hdr;
  204. ret = dp_mst_sim_decode_sideband_msg_hdr(&recv_hdr,
  205. replybuf, replybuflen, &hdrlen);
  206. if (ret == false)
  207. return false;
  208. /*
  209. * ignore out-of-order messages or messages that are part of a
  210. * failed transaction
  211. */
  212. if (!recv_hdr.somt && !msg->have_somt)
  213. return false;
  214. /* get length contained in this portion */
  215. msg->curchunk_len = recv_hdr.msg_len;
  216. msg->curchunk_hdrlen = hdrlen;
  217. /* we have already gotten an somt - don't bother parsing */
  218. if (recv_hdr.somt && msg->have_somt)
  219. return false;
  220. if (recv_hdr.somt) {
  221. memcpy(&msg->initial_hdr, &recv_hdr,
  222. sizeof(struct drm_dp_sideband_msg_hdr));
  223. msg->have_somt = true;
  224. }
  225. if (recv_hdr.eomt)
  226. msg->have_eomt = true;
  227. /* copy the bytes for the remainder of this header chunk */
  228. msg->curchunk_idx = min(msg->curchunk_len,
  229. (u8)(replybuflen - hdrlen));
  230. memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
  231. } else {
  232. memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
  233. msg->curchunk_idx += replybuflen;
  234. }
  235. if (msg->curchunk_idx >= msg->curchunk_len) {
  236. /* do CRC */
  237. crc4 = dp_mst_sim_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
  238. /* copy chunk into bigger msg */
  239. memcpy(&msg->msg[msg->curlen], msg->chunk,
  240. msg->curchunk_len - 1);
  241. msg->curlen += msg->curchunk_len - 1;
  242. }
  243. return true;
  244. }
  245. static void dp_mst_sim_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
  246. u8 *buf, int *len)
  247. {
  248. int idx = 0;
  249. int i;
  250. u8 crc4;
  251. buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
  252. for (i = 0; i < (hdr->lct / 2); i++)
  253. buf[idx++] = hdr->rad[i];
  254. buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
  255. (hdr->msg_len & 0x3f);
  256. buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
  257. crc4 = dp_mst_sim_msg_header_crc4(buf, (idx * 2) - 1);
  258. buf[idx - 1] |= (crc4 & 0xf);
  259. *len = idx;
  260. }
  261. static bool dp_get_one_sb_msg(struct drm_dp_sideband_msg_rx *msg,
  262. struct drm_dp_aux_msg *aux_msg)
  263. {
  264. int ret;
  265. if (!msg->have_somt) {
  266. ret = dp_mst_sim_sideband_msg_build(msg,
  267. aux_msg->buffer, aux_msg->size, true);
  268. if (!ret) {
  269. DP_ERR("sideband hdr build failed\n");
  270. return false;
  271. }
  272. } else {
  273. ret = dp_mst_sim_sideband_msg_build(msg,
  274. aux_msg->buffer, aux_msg->size, false);
  275. if (!ret) {
  276. DP_ERR("sideband msg build failed\n");
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. static int dp_sideband_build_nak_rep(
  283. struct dp_mst_sim_context *ctx)
  284. {
  285. struct drm_dp_sideband_msg_rx *msg = &ctx->down_req;
  286. u8 *buf = ctx->down_rep.msg;
  287. int idx = 0;
  288. buf[idx] = msg->msg[0] | 0x80;
  289. idx++;
  290. memcpy(&buf[idx], ctx->guid, 16);
  291. idx += 16;
  292. buf[idx] = 0x4;
  293. idx++;
  294. buf[idx] = 0;
  295. idx++;
  296. return idx;
  297. }
  298. static int dp_sideband_build_link_address_rep(
  299. struct dp_mst_sim_context *ctx)
  300. {
  301. struct dp_mst_sim_port *port;
  302. u8 *buf = ctx->down_rep.msg;
  303. int idx = 0;
  304. u32 i, tmp;
  305. buf[idx] = DP_LINK_ADDRESS;
  306. idx++;
  307. memcpy(&buf[idx], ctx->guid, 16);
  308. idx += 16;
  309. buf[idx] = ctx->port_num;
  310. idx++;
  311. for (i = 0; i < ctx->port_num; i++) {
  312. port = &ctx->ports[i];
  313. tmp = 0;
  314. if (port->input)
  315. tmp |= 0x80;
  316. tmp |= port->pdt << 4;
  317. tmp |= i & 0xF;
  318. buf[idx] = tmp;
  319. idx++;
  320. tmp = 0;
  321. if (port->mcs)
  322. tmp |= 0x80;
  323. if (port->ddps)
  324. tmp |= 0x40;
  325. if (port->input) {
  326. buf[idx] = tmp;
  327. idx++;
  328. continue;
  329. }
  330. if (port->ldps)
  331. tmp |= 0x20;
  332. buf[idx] = tmp;
  333. idx++;
  334. buf[idx] = port->dpcd_rev;
  335. idx++;
  336. memcpy(&buf[idx], port->peer_guid, 16);
  337. idx += 16;
  338. buf[idx] = (port->num_sdp_streams << 4) |
  339. (port->num_sdp_stream_sinks);
  340. idx++;
  341. }
  342. return idx;
  343. }
  344. static int dp_sideband_build_remote_i2c_read_rep(
  345. struct dp_mst_sim_context *ctx)
  346. {
  347. struct dp_mst_sim_port *port;
  348. struct drm_dp_remote_i2c_read i2c_read;
  349. u8 *buf;
  350. int idx;
  351. u32 i, start, len;
  352. buf = ctx->down_req.msg;
  353. idx = 1;
  354. i2c_read.num_transactions = buf[idx] & 0x3;
  355. i2c_read.port_number = buf[idx] >> 4;
  356. idx++;
  357. if (i2c_read.port_number >= ctx->port_num)
  358. goto err;
  359. for (i = 0; i < i2c_read.num_transactions; i++) {
  360. i2c_read.transactions[i].i2c_dev_id = buf[idx] & 0x7f;
  361. idx++;
  362. i2c_read.transactions[i].num_bytes = buf[idx];
  363. idx++;
  364. i2c_read.transactions[i].bytes = &buf[idx];
  365. idx += i2c_read.transactions[i].num_bytes;
  366. i2c_read.transactions[i].no_stop_bit = (buf[idx] >> 4) & 0x1;
  367. i2c_read.transactions[i].i2c_transaction_delay = buf[idx] & 0xf;
  368. idx++;
  369. }
  370. i2c_read.read_i2c_device_id = buf[idx];
  371. idx++;
  372. i2c_read.num_bytes_read = buf[idx];
  373. idx++;
  374. port = &ctx->ports[i2c_read.port_number];
  375. if (i2c_read.num_transactions == 1) {
  376. if (i2c_read.transactions[0].i2c_dev_id != DDC_ADDR ||
  377. i2c_read.transactions[0].num_bytes != 1) {
  378. DP_ERR("unsupported i2c address\n");
  379. goto err;
  380. }
  381. start = i2c_read.transactions[0].bytes[0];
  382. } else if (i2c_read.num_transactions == 2) {
  383. if (i2c_read.transactions[0].i2c_dev_id != DDC_SEGMENT_ADDR ||
  384. i2c_read.transactions[0].num_bytes != 1 ||
  385. i2c_read.transactions[1].i2c_dev_id != DDC_ADDR ||
  386. i2c_read.transactions[1].num_bytes != 1) {
  387. DP_ERR("unsupported i2c address\n");
  388. goto err;
  389. }
  390. start = i2c_read.transactions[0].bytes[0] * EDID_LENGTH * 2 +
  391. i2c_read.transactions[1].bytes[0];
  392. } else {
  393. DP_ERR("unsupported i2c transaction\n");
  394. goto err;
  395. }
  396. len = i2c_read.num_bytes_read;
  397. if (start + len > port->edid_size) {
  398. DP_ERR("edid data exceeds maximum\n");
  399. goto err;
  400. }
  401. buf = ctx->down_rep.msg;
  402. idx = 0;
  403. buf[idx] = DP_REMOTE_I2C_READ;
  404. idx++;
  405. buf[idx] = i2c_read.port_number;
  406. idx++;
  407. buf[idx] = len;
  408. idx++;
  409. memcpy(&buf[idx], &port->edid[start], len);
  410. idx += len;
  411. return idx;
  412. err:
  413. return dp_sideband_build_nak_rep(ctx);
  414. }
  415. static int dp_sideband_build_enum_path_resources_rep(
  416. struct dp_mst_sim_context *ctx)
  417. {
  418. struct dp_mst_sim_port *port;
  419. u8 port_num;
  420. u8 *buf;
  421. int idx;
  422. buf = ctx->down_req.msg;
  423. port_num = buf[1] >> 4;
  424. if (port_num >= ctx->port_num) {
  425. DP_ERR("invalid port num\n");
  426. goto err;
  427. }
  428. port = &ctx->ports[port_num];
  429. buf = ctx->down_rep.msg;
  430. idx = 0;
  431. buf[idx] = DP_ENUM_PATH_RESOURCES;
  432. idx++;
  433. buf[idx] = port_num << 4;
  434. idx++;
  435. buf[idx] = port->full_pbn >> 8;
  436. idx++;
  437. buf[idx] = port->full_pbn & 0xFF;
  438. idx++;
  439. buf[idx] = port->avail_pbn >> 8;
  440. idx++;
  441. buf[idx] = port->avail_pbn & 0xFF;
  442. idx++;
  443. return idx;
  444. err:
  445. return dp_sideband_build_nak_rep(ctx);
  446. }
  447. static int dp_sideband_build_allocate_payload_rep(
  448. struct dp_mst_sim_context *ctx)
  449. {
  450. struct drm_dp_allocate_payload allocate_payload;
  451. u8 *buf;
  452. int idx;
  453. u32 i;
  454. buf = ctx->down_req.msg;
  455. idx = 1;
  456. allocate_payload.port_number = buf[idx] >> 4;
  457. allocate_payload.number_sdp_streams = buf[idx] & 0xF;
  458. idx++;
  459. allocate_payload.vcpi = buf[idx];
  460. idx++;
  461. allocate_payload.pbn = (buf[idx] << 8) | buf[idx+1];
  462. idx += 2;
  463. for (i = 0; i < allocate_payload.number_sdp_streams / 2; i++) {
  464. allocate_payload.sdp_stream_sink[i * 2] = buf[idx] >> 4;
  465. allocate_payload.sdp_stream_sink[i * 2 + 1] = buf[idx] & 0xf;
  466. idx++;
  467. }
  468. if (allocate_payload.number_sdp_streams & 1) {
  469. i = allocate_payload.number_sdp_streams - 1;
  470. allocate_payload.sdp_stream_sink[i] = buf[idx] >> 4;
  471. idx++;
  472. }
  473. if (allocate_payload.port_number >= ctx->port_num) {
  474. DP_ERR("invalid port num\n");
  475. goto err;
  476. }
  477. buf = ctx->down_rep.msg;
  478. idx = 0;
  479. buf[idx] = DP_ALLOCATE_PAYLOAD;
  480. idx++;
  481. buf[idx] = allocate_payload.port_number;
  482. idx++;
  483. buf[idx] = allocate_payload.vcpi;
  484. idx++;
  485. buf[idx] = allocate_payload.pbn >> 8;
  486. idx++;
  487. buf[idx] = allocate_payload.pbn & 0xFF;
  488. idx++;
  489. return idx;
  490. err:
  491. return dp_sideband_build_nak_rep(ctx);
  492. }
  493. static int dp_sideband_build_power_updown_phy_rep(
  494. struct dp_mst_sim_context *ctx)
  495. {
  496. u8 port_num;
  497. u8 *buf;
  498. int idx;
  499. buf = ctx->down_req.msg;
  500. port_num = buf[1] >> 4;
  501. if (port_num >= ctx->port_num) {
  502. DP_ERR("invalid port num\n");
  503. goto err;
  504. }
  505. buf = ctx->down_rep.msg;
  506. idx = 0;
  507. buf[idx] = ctx->down_req.msg[0];
  508. idx++;
  509. buf[idx] = port_num;
  510. idx++;
  511. return idx;
  512. err:
  513. return dp_sideband_build_nak_rep(ctx);
  514. }
  515. static int dp_sideband_build_clear_payload_id_table_rep(
  516. struct dp_mst_sim_context *ctx)
  517. {
  518. u8 *buf = ctx->down_rep.msg;
  519. int idx = 0;
  520. buf[idx] = DP_CLEAR_PAYLOAD_ID_TABLE;
  521. idx++;
  522. return idx;
  523. }
  524. static int dp_sideband_build_connection_notify_req(
  525. struct dp_mst_sim_context *ctx, int port_idx)
  526. {
  527. struct dp_mst_sim_port *port = &ctx->ports[port_idx];
  528. u8 *buf = ctx->down_rep.msg;
  529. int idx = 0;
  530. buf[idx] = DP_CONNECTION_STATUS_NOTIFY;
  531. idx++;
  532. buf[idx] = port_idx << 4;
  533. idx++;
  534. memcpy(&buf[idx], &port->peer_guid, 16);
  535. idx += 16;
  536. buf[idx] = (port->ldps << 6) |
  537. (port->ddps << 5) |
  538. (port->mcs << 4) |
  539. (port->input << 3) |
  540. (port->pdt & 0x7);
  541. idx++;
  542. return idx;
  543. }
  544. static inline int dp_sideband_update_esi(
  545. struct dp_mst_sim_context *ctx, u8 val)
  546. {
  547. ctx->esi[0] = ctx->port_num;
  548. ctx->esi[1] = val;
  549. ctx->esi[2] = 0;
  550. return 0;
  551. }
  552. static inline bool dp_sideband_pending_esi(
  553. struct dp_mst_sim_context *ctx, u8 val)
  554. {
  555. return !!(ctx->esi[1] & val);
  556. }
  557. static int dp_mst_sim_clear_esi(struct dp_mst_sim_context *ctx,
  558. struct drm_dp_aux_msg *msg)
  559. {
  560. size_t i;
  561. u8 old_esi = ctx->esi[1];
  562. u32 addr = msg->address - DP_SINK_COUNT_ESI;
  563. if (msg->size - addr >= 16) {
  564. msg->reply = DP_AUX_NATIVE_REPLY_NACK;
  565. return 0;
  566. }
  567. mutex_lock(&ctx->session_lock);
  568. for (i = 0; i < msg->size; i++)
  569. ctx->esi[addr + i] &= ~((u8 *)msg->buffer)[i];
  570. if (old_esi != ctx->esi[1])
  571. complete(&ctx->session_comp);
  572. mutex_unlock(&ctx->session_lock);
  573. msg->reply = DP_AUX_NATIVE_REPLY_ACK;
  574. return 0;
  575. }
  576. static int dp_mst_sim_read_esi(struct dp_mst_sim_context *ctx,
  577. struct drm_dp_aux_msg *msg)
  578. {
  579. u32 addr = msg->address - DP_SINK_COUNT_ESI;
  580. if (msg->size - addr >= 16) {
  581. msg->reply = DP_AUX_NATIVE_REPLY_NACK;
  582. return 0;
  583. }
  584. memcpy(msg->buffer, &ctx->esi[addr], msg->size);
  585. msg->reply = DP_AUX_NATIVE_REPLY_ACK;
  586. return 0;
  587. }
  588. static int dp_mst_sim_down_req_internal(struct dp_mst_sim_context *ctx,
  589. struct drm_dp_aux_msg *aux_msg)
  590. {
  591. struct drm_dp_sideband_msg_rx *msg = &ctx->down_req;
  592. struct drm_dp_sideband_msg_hdr hdr;
  593. bool seqno;
  594. int ret, size, len, hdr_len;
  595. ret = dp_get_one_sb_msg(msg, aux_msg);
  596. if (!ret)
  597. return -EINVAL;
  598. if (!msg->have_eomt)
  599. return 0;
  600. seqno = msg->initial_hdr.seqno;
  601. switch (msg->msg[0]) {
  602. case DP_LINK_ADDRESS:
  603. size = dp_sideband_build_link_address_rep(ctx);
  604. break;
  605. case DP_REMOTE_I2C_READ:
  606. size = dp_sideband_build_remote_i2c_read_rep(ctx);
  607. break;
  608. case DP_ENUM_PATH_RESOURCES:
  609. size = dp_sideband_build_enum_path_resources_rep(ctx);
  610. break;
  611. case DP_ALLOCATE_PAYLOAD:
  612. size = dp_sideband_build_allocate_payload_rep(ctx);
  613. break;
  614. case DP_POWER_DOWN_PHY:
  615. case DP_POWER_UP_PHY:
  616. size = dp_sideband_build_power_updown_phy_rep(ctx);
  617. break;
  618. case DP_CLEAR_PAYLOAD_ID_TABLE:
  619. size = dp_sideband_build_clear_payload_id_table_rep(ctx);
  620. break;
  621. default:
  622. size = dp_sideband_build_nak_rep(ctx);
  623. break;
  624. }
  625. if (ctx->host_req)
  626. ctx->host_req(ctx->host_dev,
  627. ctx->down_req.msg, ctx->down_req.curlen,
  628. ctx->down_rep.msg, &size);
  629. memset(msg, 0, sizeof(*msg));
  630. msg = &ctx->down_rep;
  631. msg->curlen = 0;
  632. mutex_lock(&ctx->session_lock);
  633. while (msg->curlen < size) {
  634. if (ctx->reset_cnt)
  635. break;
  636. /* copy data */
  637. len = min(size - msg->curlen, 44);
  638. memcpy(&ctx->dpcd[3], &msg->msg[msg->curlen], len);
  639. msg->curlen += len;
  640. /* build header */
  641. memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
  642. hdr.broadcast = 0;
  643. hdr.path_msg = 0;
  644. hdr.lct = 1;
  645. hdr.lcr = 0;
  646. hdr.seqno = seqno;
  647. hdr.msg_len = len + 1;
  648. hdr.eomt = (msg->curlen == size);
  649. hdr.somt = (msg->curlen == len);
  650. dp_mst_sim_encode_sideband_msg_hdr(&hdr, ctx->dpcd, &hdr_len);
  651. /* build crc */
  652. ctx->dpcd[len + 3] = dp_mst_sim_msg_data_crc4(&ctx->dpcd[3], len);
  653. /* update esi */
  654. dp_sideband_update_esi(ctx, DP_DOWN_REP_MSG_RDY);
  655. /* notify host */
  656. mutex_unlock(&ctx->session_lock);
  657. ctx->host_hpd_irq(ctx->host_dev);
  658. mutex_lock(&ctx->session_lock);
  659. /* wait until esi is cleared */
  660. while (dp_sideband_pending_esi(ctx, DP_DOWN_REP_MSG_RDY)) {
  661. if (ctx->reset_cnt)
  662. break;
  663. mutex_unlock(&ctx->session_lock);
  664. wait_for_completion(&ctx->session_comp);
  665. mutex_lock(&ctx->session_lock);
  666. }
  667. }
  668. mutex_unlock(&ctx->session_lock);
  669. return 0;
  670. }
  671. static void dp_mst_sim_down_req_work(struct work_struct *work)
  672. {
  673. struct dp_mst_sim_work *sim_work =
  674. container_of(work, struct dp_mst_sim_work, base);
  675. struct drm_dp_aux_msg msg;
  676. msg.address = sim_work->address;
  677. msg.buffer = sim_work->buffer;
  678. msg.size = sim_work->size;
  679. dp_mst_sim_down_req_internal(sim_work->ctx, &msg);
  680. kfree(sim_work);
  681. }
  682. static int dp_mst_sim_down_req(struct dp_mst_sim_context *ctx,
  683. struct drm_dp_aux_msg *aux_msg)
  684. {
  685. struct dp_mst_sim_work *work;
  686. if (aux_msg->size >= 256) {
  687. aux_msg->reply = DP_AUX_NATIVE_REPLY_NACK;
  688. return 0;
  689. }
  690. dp_sideband_hex_dump("request",
  691. aux_msg->address, aux_msg->buffer, aux_msg->size);
  692. work = kzalloc(sizeof(*work), GFP_KERNEL);
  693. if (!work) {
  694. aux_msg->reply = DP_AUX_NATIVE_REPLY_NACK;
  695. return 0;
  696. }
  697. work->ctx = ctx;
  698. work->address = aux_msg->address;
  699. work->size = aux_msg->size;
  700. memcpy(work->buffer, aux_msg->buffer, aux_msg->size);
  701. INIT_WORK(&work->base, dp_mst_sim_down_req_work);
  702. queue_work(ctx->wq, &work->base);
  703. aux_msg->reply = DP_AUX_NATIVE_REPLY_ACK;
  704. return 0;
  705. }
  706. static int dp_mst_sim_down_rep(struct dp_mst_sim_context *ctx,
  707. struct drm_dp_aux_msg *msg)
  708. {
  709. u32 addr = msg->address - DP_SIDEBAND_MSG_DOWN_REP_BASE;
  710. memcpy(msg->buffer, &ctx->dpcd[addr], msg->size);
  711. msg->reply = DP_AUX_NATIVE_REPLY_ACK;
  712. dp_sideband_hex_dump("reply",
  713. addr, msg->buffer, msg->size);
  714. return 0;
  715. }
  716. static int dp_mst_sim_up_req(struct dp_mst_sim_context *ctx,
  717. struct drm_dp_aux_msg *msg)
  718. {
  719. u32 addr = msg->address - DP_SIDEBAND_MSG_UP_REQ_BASE;
  720. memcpy(msg->buffer, &ctx->dpcd[addr], msg->size);
  721. msg->reply = DP_AUX_NATIVE_REPLY_ACK;
  722. dp_sideband_hex_dump("up_req",
  723. addr, msg->buffer, msg->size);
  724. return 0;
  725. }
  726. static void dp_mst_sim_reset_work(struct work_struct *work)
  727. {
  728. struct dp_mst_notify_work *notify_work =
  729. container_of(work, struct dp_mst_notify_work, base);
  730. struct dp_mst_sim_context *ctx = notify_work->ctx;
  731. mutex_lock(&ctx->session_lock);
  732. --ctx->reset_cnt;
  733. reinit_completion(&ctx->session_comp);
  734. mutex_unlock(&ctx->session_lock);
  735. }
  736. static int dp_mst_sim_reset(struct dp_mst_sim_context *ctx,
  737. struct drm_dp_aux_msg *msg)
  738. {
  739. struct dp_mst_notify_work *work;
  740. if (!msg->size || ((u8 *)msg->buffer)[0])
  741. return msg->size;
  742. mutex_lock(&ctx->session_lock);
  743. ++ctx->reset_cnt;
  744. complete(&ctx->session_comp);
  745. mutex_unlock(&ctx->session_lock);
  746. work = kzalloc(sizeof(*work), GFP_KERNEL);
  747. if (!work)
  748. return msg->size;
  749. work->ctx = ctx;
  750. INIT_WORK(&work->base, dp_mst_sim_reset_work);
  751. queue_work(ctx->wq, &work->base);
  752. return msg->size;
  753. }
  754. int dp_mst_sim_transfer(void *mst_sim_context, struct drm_dp_aux_msg *msg)
  755. {
  756. struct dp_mst_sim_context *ctx = mst_sim_context;
  757. if (!ctx || !ctx->port_num || !msg)
  758. return -ENOENT;
  759. if (msg->request == DP_AUX_NATIVE_WRITE) {
  760. if (msg->address >= DP_SIDEBAND_MSG_DOWN_REQ_BASE &&
  761. msg->address < DP_SIDEBAND_MSG_DOWN_REQ_BASE + 256)
  762. return dp_mst_sim_down_req(mst_sim_context, msg);
  763. if (msg->address >= DP_SIDEBAND_MSG_UP_REP_BASE &&
  764. msg->address < DP_SIDEBAND_MSG_UP_REP_BASE + 256)
  765. return 0;
  766. if (msg->address >= DP_SINK_COUNT_ESI &&
  767. msg->address < DP_SINK_COUNT_ESI + 14)
  768. return dp_mst_sim_clear_esi(mst_sim_context, msg);
  769. if (msg->address == DP_MSTM_CTRL)
  770. return dp_mst_sim_reset(mst_sim_context, msg);
  771. } else if (msg->request == DP_AUX_NATIVE_READ) {
  772. if (msg->address >= DP_SIDEBAND_MSG_DOWN_REP_BASE &&
  773. msg->address < DP_SIDEBAND_MSG_DOWN_REP_BASE + 256)
  774. return dp_mst_sim_down_rep(mst_sim_context, msg);
  775. if (msg->address >= DP_SIDEBAND_MSG_UP_REQ_BASE &&
  776. msg->address < DP_SIDEBAND_MSG_UP_REQ_BASE + 256)
  777. return dp_mst_sim_up_req(mst_sim_context, msg);
  778. if (msg->address >= DP_SINK_COUNT_ESI &&
  779. msg->address < DP_SINK_COUNT_ESI + 14)
  780. return dp_mst_sim_read_esi(mst_sim_context, msg);
  781. }
  782. return -EINVAL;
  783. }
  784. static void dp_mst_sim_up_req_work(struct work_struct *work)
  785. {
  786. struct dp_mst_notify_work *notify_work =
  787. container_of(work, struct dp_mst_notify_work, base);
  788. struct dp_mst_sim_context *ctx = notify_work->ctx;
  789. struct drm_dp_sideband_msg_rx *msg = &ctx->down_rep;
  790. struct drm_dp_sideband_msg_hdr hdr;
  791. int len, hdr_len, i;
  792. mutex_lock(&ctx->session_lock);
  793. for (i = 0; i < ctx->port_num; i++) {
  794. if (ctx->reset_cnt)
  795. break;
  796. if (!(notify_work->port_mask & (1 << i)))
  797. continue;
  798. len = dp_sideband_build_connection_notify_req(ctx, i);
  799. /* copy data */
  800. memcpy(&ctx->dpcd[3], msg->msg, len);
  801. /* build header */
  802. memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
  803. hdr.broadcast = 0;
  804. hdr.path_msg = 0;
  805. hdr.lct = 1;
  806. hdr.lcr = 0;
  807. hdr.seqno = 0;
  808. hdr.msg_len = len + 1;
  809. hdr.eomt = 1;
  810. hdr.somt = 1;
  811. dp_mst_sim_encode_sideband_msg_hdr(&hdr, ctx->dpcd, &hdr_len);
  812. /* build crc */
  813. ctx->dpcd[len + 3] = dp_mst_sim_msg_data_crc4(&ctx->dpcd[3], len);
  814. /* update esi */
  815. dp_sideband_update_esi(ctx, DP_UP_REQ_MSG_RDY);
  816. /* notify host */
  817. mutex_unlock(&ctx->session_lock);
  818. ctx->host_hpd_irq(ctx->host_dev);
  819. mutex_lock(&ctx->session_lock);
  820. /* wait until esi is cleared */
  821. while (dp_sideband_pending_esi(ctx, DP_UP_REQ_MSG_RDY)) {
  822. if (ctx->reset_cnt)
  823. break;
  824. mutex_unlock(&ctx->session_lock);
  825. wait_for_completion(&ctx->session_comp);
  826. mutex_lock(&ctx->session_lock);
  827. }
  828. }
  829. mutex_unlock(&ctx->session_lock);
  830. kfree(notify_work);
  831. }
  832. static void dp_mst_sim_notify(struct dp_mst_sim_context *ctx,
  833. u32 port_mask)
  834. {
  835. struct dp_mst_notify_work *work;
  836. work = kzalloc(sizeof(*work), GFP_KERNEL);
  837. if (!work)
  838. return;
  839. work->ctx = ctx;
  840. work->port_mask = port_mask;
  841. INIT_WORK(&work->base, dp_mst_sim_up_req_work);
  842. queue_work(ctx->wq, &work->base);
  843. }
  844. int dp_mst_sim_update(void *mst_sim_context, u32 port_num,
  845. struct dp_mst_sim_port *ports)
  846. {
  847. struct dp_mst_sim_context *ctx = mst_sim_context;
  848. u8 *edid;
  849. int rc = 0;
  850. u32 update_mask = 0;
  851. u32 i;
  852. if (!ctx || port_num >= 15)
  853. return -EINVAL;
  854. mutex_lock(&ctx->session_lock);
  855. /* get update mask */
  856. if (port_num && ctx->port_num == port_num) {
  857. for (i = 0; i < port_num; i++) {
  858. if (ports[i].pdt != ctx->ports[i].pdt ||
  859. ports[i].input != ctx->ports[i].input ||
  860. ports[i].ldps != ctx->ports[i].ldps ||
  861. ports[i].ddps != ctx->ports[i].ddps ||
  862. ports[i].mcs != ctx->ports[i].mcs)
  863. update_mask |= (1 << i);
  864. }
  865. }
  866. for (i = 0; i < ctx->port_num; i++)
  867. kfree(ctx->ports[i].edid);
  868. kfree(ctx->ports);
  869. ctx->port_num = 0;
  870. ctx->ports = kcalloc(port_num, sizeof(*ports), GFP_KERNEL);
  871. if (!ctx->ports) {
  872. rc = -ENOMEM;
  873. goto fail;
  874. }
  875. ctx->port_num = port_num;
  876. for (i = 0; i < port_num; i++) {
  877. ctx->ports[i] = ports[i];
  878. if (ports[i].edid_size) {
  879. if (!ports[i].edid) {
  880. rc = -EINVAL;
  881. goto fail;
  882. }
  883. edid = kzalloc(ports[i].edid_size,
  884. GFP_KERNEL);
  885. if (!edid) {
  886. rc = -ENOMEM;
  887. goto fail;
  888. }
  889. memcpy(edid, ports[i].edid, ports[i].edid_size);
  890. ctx->ports[i].edid = edid;
  891. }
  892. }
  893. fail:
  894. if (rc) {
  895. for (i = 0; i < ctx->port_num; i++)
  896. kfree(ctx->ports[i].edid);
  897. kfree(ctx->ports);
  898. }
  899. mutex_unlock(&ctx->session_lock);
  900. if (update_mask)
  901. dp_mst_sim_notify(ctx, update_mask);
  902. return rc;
  903. }
  904. int dp_mst_sim_create(const struct dp_mst_sim_cfg *cfg,
  905. void **mst_sim_context)
  906. {
  907. struct dp_mst_sim_context *ctx;
  908. if (!cfg || !mst_sim_context)
  909. return -EINVAL;
  910. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  911. if (!ctx)
  912. return -ENOMEM;
  913. ctx->host_dev = cfg->host_dev;
  914. ctx->host_hpd_irq = cfg->host_hpd_irq;
  915. ctx->host_req = cfg->host_req;
  916. memcpy(ctx->guid, cfg->guid, 16);
  917. mutex_init(&ctx->session_lock);
  918. init_completion(&ctx->session_comp);
  919. ctx->wq = create_singlethread_workqueue("dp_mst_sim");
  920. if (IS_ERR_OR_NULL(ctx->wq)) {
  921. DP_ERR("Error creating wq\n");
  922. kfree(ctx);
  923. return -EPERM;
  924. }
  925. *mst_sim_context = ctx;
  926. return 0;
  927. }
  928. int dp_mst_sim_destroy(void *mst_sim_context)
  929. {
  930. struct dp_mst_sim_context *ctx = mst_sim_context;
  931. u32 i;
  932. if (!ctx)
  933. return -EINVAL;
  934. for (i = 0; i < ctx->port_num; i++)
  935. kfree(ctx->ports[i].edid);
  936. kfree(ctx->ports);
  937. destroy_workqueue(ctx->wq);
  938. return 0;
  939. }