dp_mst_sim_helper.c 26 KB

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