dp_hdcp2p2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/io.h>
  7. #include <linux/slab.h>
  8. #include <linux/stat.h>
  9. #include <linux/types.h>
  10. #include <linux/kthread.h>
  11. #include <linux/msm_hdcp.h>
  12. #include <linux/kfifo.h>
  13. #include <drm/drm_dp_helper.h>
  14. #include "sde_hdcp_2x.h"
  15. #include "dp_debug.h"
  16. #define DP_INTR_STATUS2 (0x00000024)
  17. #define DP_INTR_STATUS3 (0x00000028)
  18. #define dp_read(offset) readl_relaxed((offset))
  19. #define dp_write(offset, data) writel_relaxed((data), (offset))
  20. #define DP_HDCP_RXCAPS_LENGTH 3
  21. enum dp_hdcp2p2_sink_status {
  22. SINK_DISCONNECTED,
  23. SINK_CONNECTED
  24. };
  25. struct dp_hdcp2p2_ctrl {
  26. DECLARE_KFIFO(cmd_q, enum hdcp_transport_wakeup_cmd, 8);
  27. wait_queue_head_t wait_q;
  28. atomic_t auth_state;
  29. atomic_t abort;
  30. enum dp_hdcp2p2_sink_status sink_status; /* Is sink connected */
  31. struct dp_hdcp2p2_interrupts *intr;
  32. struct sde_hdcp_init_data init_data;
  33. struct mutex mutex; /* mutex to protect access to ctrl */
  34. struct mutex msg_lock; /* mutex to protect access to msg buffer */
  35. struct sde_hdcp_ops *ops;
  36. void *lib_ctx; /* Handle to HDCP 2.2 Trustzone library */
  37. struct sde_hdcp_2x_ops *lib; /* Ops for driver to call into TZ */
  38. struct task_struct *thread;
  39. struct hdcp2_buffer response;
  40. struct hdcp2_buffer request;
  41. uint32_t total_message_length;
  42. uint32_t transaction_delay;
  43. uint32_t transaction_timeout;
  44. struct sde_hdcp_2x_msg_part msg_part[HDCP_MAX_MESSAGE_PARTS];
  45. u8 sink_rx_status;
  46. u8 rx_status;
  47. char abort_mask;
  48. bool polling;
  49. };
  50. struct dp_hdcp2p2_int_set {
  51. u32 interrupt;
  52. char *name;
  53. void (*func)(struct dp_hdcp2p2_ctrl *ctrl);
  54. };
  55. struct dp_hdcp2p2_interrupts {
  56. u32 reg;
  57. struct dp_hdcp2p2_int_set *int_set;
  58. };
  59. static inline int dp_hdcp2p2_valid_handle(struct dp_hdcp2p2_ctrl *ctrl)
  60. {
  61. if (!ctrl) {
  62. DP_ERR("invalid input\n");
  63. return -EINVAL;
  64. }
  65. if (!ctrl->lib_ctx) {
  66. DP_ERR("HDCP library needs to be acquired\n");
  67. return -EINVAL;
  68. }
  69. if (!ctrl->lib) {
  70. DP_ERR("invalid lib ops data\n");
  71. return -EINVAL;
  72. }
  73. return 0;
  74. }
  75. static inline bool dp_hdcp2p2_is_valid_state(struct dp_hdcp2p2_ctrl *ctrl)
  76. {
  77. enum hdcp_transport_wakeup_cmd cmd;
  78. if (kfifo_peek(&ctrl->cmd_q, &cmd) &&
  79. cmd == HDCP_TRANSPORT_CMD_AUTHENTICATE)
  80. return true;
  81. if (atomic_read(&ctrl->auth_state) != HDCP_STATE_INACTIVE)
  82. return true;
  83. return false;
  84. }
  85. static int dp_hdcp2p2_copy_buf(struct dp_hdcp2p2_ctrl *ctrl,
  86. struct hdcp_transport_wakeup_data *data)
  87. {
  88. int i = 0;
  89. uint32_t num_messages = 0;
  90. if (!data || !data->message_data)
  91. return 0;
  92. mutex_lock(&ctrl->msg_lock);
  93. num_messages = data->message_data->num_messages;
  94. ctrl->total_message_length = 0; /* Total length of all messages */
  95. for (i = 0; i < num_messages; i++)
  96. ctrl->total_message_length +=
  97. data->message_data->messages[i].length;
  98. memcpy(ctrl->msg_part, data->message_data->messages,
  99. sizeof(data->message_data->messages));
  100. ctrl->rx_status = data->message_data->rx_status;
  101. ctrl->abort_mask = data->abort_mask;
  102. if (!ctrl->total_message_length) {
  103. mutex_unlock(&ctrl->msg_lock);
  104. return 0;
  105. }
  106. ctrl->response.data = data->buf;
  107. ctrl->response.length = ctrl->total_message_length;
  108. ctrl->request.data = data->buf;
  109. ctrl->request.length = ctrl->total_message_length;
  110. ctrl->transaction_delay = data->transaction_delay;
  111. ctrl->transaction_timeout = data->transaction_timeout;
  112. mutex_unlock(&ctrl->msg_lock);
  113. return 0;
  114. }
  115. static void dp_hdcp2p2_send_auth_status(struct dp_hdcp2p2_ctrl *ctrl)
  116. {
  117. ctrl->init_data.notify_status(ctrl->init_data.cb_data,
  118. atomic_read(&ctrl->auth_state));
  119. }
  120. static void dp_hdcp2p2_set_interrupts(struct dp_hdcp2p2_ctrl *ctrl, bool enable)
  121. {
  122. void __iomem *base = ctrl->init_data.dp_ahb->base;
  123. struct dp_hdcp2p2_interrupts *intr = ctrl->intr;
  124. if (atomic_read(&ctrl->abort))
  125. return;
  126. while (intr && intr->reg) {
  127. struct dp_hdcp2p2_int_set *int_set = intr->int_set;
  128. u32 interrupts = 0;
  129. while (int_set && int_set->interrupt) {
  130. interrupts |= int_set->interrupt;
  131. int_set++;
  132. }
  133. if (enable)
  134. dp_write(base + intr->reg,
  135. dp_read(base + intr->reg) | interrupts);
  136. else
  137. dp_write(base + intr->reg,
  138. dp_read(base + intr->reg) & ~interrupts);
  139. intr++;
  140. }
  141. }
  142. static int dp_hdcp2p2_wakeup(struct hdcp_transport_wakeup_data *data)
  143. {
  144. struct dp_hdcp2p2_ctrl *ctrl;
  145. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY);
  146. if (!data) {
  147. DP_ERR("invalid input\n");
  148. return -EINVAL;
  149. }
  150. ctrl = data->context;
  151. if (!ctrl) {
  152. DP_ERR("invalid ctrl\n");
  153. return -EINVAL;
  154. }
  155. if (dp_hdcp2p2_copy_buf(ctrl, data))
  156. goto exit;
  157. ctrl->polling = false;
  158. switch (data->cmd) {
  159. case HDCP_TRANSPORT_CMD_STATUS_SUCCESS:
  160. atomic_set(&ctrl->auth_state, HDCP_STATE_AUTHENTICATED);
  161. kfifo_put(&ctrl->cmd_q, data->cmd);
  162. wake_up(&ctrl->wait_q);
  163. break;
  164. case HDCP_TRANSPORT_CMD_STATUS_FAILED:
  165. atomic_set(&ctrl->auth_state, HDCP_STATE_AUTH_FAIL);
  166. kfifo_put(&ctrl->cmd_q, data->cmd);
  167. kthread_park(ctrl->thread);
  168. break;
  169. default:
  170. kfifo_put(&ctrl->cmd_q, data->cmd);
  171. wake_up(&ctrl->wait_q);
  172. break;
  173. }
  174. exit:
  175. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, data->cmd);
  176. return 0;
  177. }
  178. static inline void dp_hdcp2p2_wakeup_lib(struct dp_hdcp2p2_ctrl *ctrl,
  179. struct sde_hdcp_2x_wakeup_data *data)
  180. {
  181. int rc = 0;
  182. if (ctrl && ctrl->lib && ctrl->lib->wakeup &&
  183. data && (data->cmd != HDCP_2X_CMD_INVALID)) {
  184. rc = ctrl->lib->wakeup(data);
  185. if (rc)
  186. DP_ERR("error sending %s to lib\n",
  187. sde_hdcp_2x_cmd_to_str(data->cmd));
  188. }
  189. }
  190. static void dp_hdcp2p2_reset(struct dp_hdcp2p2_ctrl *ctrl)
  191. {
  192. if (!ctrl) {
  193. DP_ERR("invalid input\n");
  194. return;
  195. }
  196. ctrl->sink_status = SINK_DISCONNECTED;
  197. atomic_set(&ctrl->auth_state, HDCP_STATE_INACTIVE);
  198. }
  199. static int dp_hdcp2p2_register(void *input, bool mst_enabled)
  200. {
  201. int rc;
  202. struct dp_hdcp2p2_ctrl *ctrl = input;
  203. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_ENABLE};
  204. rc = dp_hdcp2p2_valid_handle(ctrl);
  205. if (rc)
  206. return rc;
  207. if (mst_enabled)
  208. cdata.device_type = HDCP_TXMTR_DP_MST;
  209. else
  210. cdata.device_type = HDCP_TXMTR_DP;
  211. cdata.context = ctrl->lib_ctx;
  212. rc = ctrl->lib->wakeup(&cdata);
  213. return rc;
  214. }
  215. static int dp_hdcp2p2_on(void *input)
  216. {
  217. int rc = 0;
  218. struct dp_hdcp2p2_ctrl *ctrl = input;
  219. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_INVALID};
  220. rc = dp_hdcp2p2_valid_handle(ctrl);
  221. if (rc)
  222. return rc;
  223. cdata.cmd = HDCP_2X_CMD_START;
  224. cdata.context = ctrl->lib_ctx;
  225. rc = ctrl->lib->wakeup(&cdata);
  226. if (rc)
  227. DP_ERR("Unable to start the HDCP 2.2 library (%d)\n", rc);
  228. return rc;
  229. }
  230. static void dp_hdcp2p2_off(void *input)
  231. {
  232. int rc;
  233. struct dp_hdcp2p2_ctrl *ctrl = (struct dp_hdcp2p2_ctrl *)input;
  234. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_DISABLE};
  235. rc = dp_hdcp2p2_valid_handle(ctrl);
  236. if (rc)
  237. return;
  238. dp_hdcp2p2_set_interrupts(ctrl, false);
  239. dp_hdcp2p2_reset(ctrl);
  240. kthread_park(ctrl->thread);
  241. cdata.context = ctrl->lib_ctx;
  242. ctrl->lib->wakeup(&cdata);
  243. }
  244. static int dp_hdcp2p2_authenticate(void *input)
  245. {
  246. int rc;
  247. struct dp_hdcp2p2_ctrl *ctrl = input;
  248. struct hdcp_transport_wakeup_data cdata = {
  249. HDCP_TRANSPORT_CMD_AUTHENTICATE};
  250. rc = dp_hdcp2p2_valid_handle(ctrl);
  251. if (rc)
  252. return rc;
  253. dp_hdcp2p2_set_interrupts(ctrl, true);
  254. ctrl->sink_status = SINK_CONNECTED;
  255. atomic_set(&ctrl->auth_state, HDCP_STATE_AUTHENTICATING);
  256. kthread_park(ctrl->thread);
  257. kfifo_reset(&ctrl->cmd_q);
  258. kthread_unpark(ctrl->thread);
  259. cdata.context = input;
  260. dp_hdcp2p2_wakeup(&cdata);
  261. return rc;
  262. }
  263. static int dp_hdcp2p2_reauthenticate(void *input)
  264. {
  265. struct dp_hdcp2p2_ctrl *ctrl = (struct dp_hdcp2p2_ctrl *)input;
  266. if (!ctrl) {
  267. DP_ERR("invalid input\n");
  268. return -EINVAL;
  269. }
  270. dp_hdcp2p2_reset((struct dp_hdcp2p2_ctrl *)input);
  271. return dp_hdcp2p2_authenticate(input);
  272. }
  273. static void dp_hdcp2p2_min_level_change(void *client_ctx,
  274. u8 min_enc_level)
  275. {
  276. struct dp_hdcp2p2_ctrl *ctrl = (struct dp_hdcp2p2_ctrl *)client_ctx;
  277. struct sde_hdcp_2x_wakeup_data cdata = {
  278. HDCP_2X_CMD_MIN_ENC_LEVEL};
  279. if (!ctrl) {
  280. DP_ERR("invalid input\n");
  281. return;
  282. }
  283. cdata.context = ctrl->lib_ctx;
  284. cdata.min_enc_level = min_enc_level;
  285. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  286. }
  287. static int dp_hdcp2p2_aux_read_message(struct dp_hdcp2p2_ctrl *ctrl)
  288. {
  289. int rc = 0, max_size = 16, read_size = 0, bytes_read = 0;
  290. int size = ctrl->request.length, offset = ctrl->msg_part->offset;
  291. u8 *buf = ctrl->request.data;
  292. s64 diff_ms;
  293. ktime_t start_read, finish_read;
  294. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE ||
  295. atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTH_FAIL) {
  296. DP_ERR("invalid hdcp state\n");
  297. rc = -EINVAL;
  298. goto exit;
  299. }
  300. if (!buf) {
  301. DP_ERR("invalid request buffer\n");
  302. rc = -EINVAL;
  303. goto exit;
  304. }
  305. DP_DEBUG("offset(0x%x), size(%d)\n", offset, size);
  306. start_read = ktime_get();
  307. do {
  308. read_size = min(size, max_size);
  309. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  310. offset, buf, read_size);
  311. if (bytes_read != read_size) {
  312. DP_ERR("fail: offset(0x%x), size(0x%x), rc(0x%x)\n",
  313. offset, read_size, bytes_read);
  314. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY,
  315. offset,
  316. read_size,
  317. bytes_read);
  318. rc = -EINVAL;
  319. break;
  320. }
  321. buf += read_size;
  322. offset += read_size;
  323. size -= read_size;
  324. } while (size > 0);
  325. finish_read = ktime_get();
  326. diff_ms = ktime_ms_delta(finish_read, start_read);
  327. if (ctrl->transaction_timeout && diff_ms > ctrl->transaction_timeout) {
  328. DP_ERR("HDCP read timeout exceeded (%lldms > %ums)\n", diff_ms,
  329. ctrl->transaction_timeout);
  330. rc = -ETIMEDOUT;
  331. }
  332. exit:
  333. return rc;
  334. }
  335. static int dp_hdcp2p2_aux_write_message(struct dp_hdcp2p2_ctrl *ctrl,
  336. u8 *buf, int size, uint offset, uint timeout)
  337. {
  338. int const max_size = 16;
  339. int rc = 0, write_size = 0, bytes_written = 0;
  340. DP_DEBUG("offset(0x%x), size(%d)\n", offset, size);
  341. do {
  342. write_size = min(size, max_size);
  343. bytes_written = drm_dp_dpcd_write(ctrl->init_data.drm_aux,
  344. offset, buf, write_size);
  345. if (bytes_written != write_size) {
  346. DP_ERR("fail: offset(0x%x), size(0x%x), rc(0x%x)\n",
  347. offset, write_size, bytes_written);
  348. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY,
  349. offset,
  350. write_size,
  351. bytes_written);
  352. rc = -EINVAL;
  353. break;
  354. }
  355. buf += write_size;
  356. offset += write_size;
  357. size -= write_size;
  358. } while (size > 0);
  359. return rc;
  360. }
  361. static bool dp_hdcp2p2_feature_supported(void *input)
  362. {
  363. int rc;
  364. struct dp_hdcp2p2_ctrl *ctrl = input;
  365. struct sde_hdcp_2x_ops *lib = NULL;
  366. bool supported = false;
  367. rc = dp_hdcp2p2_valid_handle(ctrl);
  368. if (rc)
  369. return supported;
  370. lib = ctrl->lib;
  371. if (lib->feature_supported)
  372. supported = lib->feature_supported(
  373. ctrl->lib_ctx);
  374. return supported;
  375. }
  376. static void dp_hdcp2p2_force_encryption(void *data, bool enable)
  377. {
  378. int rc;
  379. struct dp_hdcp2p2_ctrl *ctrl = data;
  380. struct sde_hdcp_2x_ops *lib = NULL;
  381. rc = dp_hdcp2p2_valid_handle(ctrl);
  382. if (rc)
  383. return;
  384. lib = ctrl->lib;
  385. if (lib->force_encryption)
  386. lib->force_encryption(ctrl->lib_ctx, enable);
  387. }
  388. static void dp_hdcp2p2_send_msg(struct dp_hdcp2p2_ctrl *ctrl)
  389. {
  390. int rc = 0;
  391. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_INVALID};
  392. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY);
  393. if (!ctrl) {
  394. DP_ERR("invalid input\n");
  395. rc = -EINVAL;
  396. goto exit;
  397. }
  398. cdata.context = ctrl->lib_ctx;
  399. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  400. DP_ERR("hdcp is off\n");
  401. goto exit;
  402. }
  403. mutex_lock(&ctrl->msg_lock);
  404. rc = dp_hdcp2p2_aux_write_message(ctrl, ctrl->response.data,
  405. ctrl->response.length, ctrl->msg_part->offset,
  406. ctrl->transaction_delay);
  407. if (rc) {
  408. DP_ERR("Error sending msg to sink %d\n", rc);
  409. mutex_unlock(&ctrl->msg_lock);
  410. goto exit;
  411. }
  412. cdata.cmd = HDCP_2X_CMD_MSG_SEND_SUCCESS;
  413. cdata.timeout = ctrl->transaction_delay;
  414. mutex_unlock(&ctrl->msg_lock);
  415. exit:
  416. if (rc == -ETIMEDOUT)
  417. cdata.cmd = HDCP_2X_CMD_MSG_SEND_TIMEOUT;
  418. else if (rc)
  419. cdata.cmd = HDCP_2X_CMD_MSG_SEND_FAILED;
  420. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  421. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT, cdata.cmd);
  422. }
  423. static int dp_hdcp2p2_get_msg_from_sink(struct dp_hdcp2p2_ctrl *ctrl)
  424. {
  425. int rc = 0;
  426. struct sde_hdcp_2x_wakeup_data cdata = { HDCP_2X_CMD_INVALID };
  427. cdata.context = ctrl->lib_ctx;
  428. rc = dp_hdcp2p2_aux_read_message(ctrl);
  429. if (rc) {
  430. DP_ERR("error reading message %d\n", rc);
  431. goto exit;
  432. }
  433. cdata.total_message_length = ctrl->total_message_length;
  434. cdata.timeout = ctrl->transaction_delay;
  435. exit:
  436. if (rc == -ETIMEDOUT)
  437. cdata.cmd = HDCP_2X_CMD_MSG_RECV_TIMEOUT;
  438. else if (rc)
  439. cdata.cmd = HDCP_2X_CMD_MSG_RECV_FAILED;
  440. else
  441. cdata.cmd = HDCP_2X_CMD_MSG_RECV_SUCCESS;
  442. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  443. return rc;
  444. }
  445. static void dp_hdcp2p2_recv_msg(struct dp_hdcp2p2_ctrl *ctrl)
  446. {
  447. struct sde_hdcp_2x_wakeup_data cdata = { HDCP_2X_CMD_INVALID };
  448. cdata.context = ctrl->lib_ctx;
  449. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  450. DP_ERR("hdcp is off\n");
  451. return;
  452. }
  453. if (ctrl->transaction_delay)
  454. msleep(ctrl->transaction_delay);
  455. dp_hdcp2p2_get_msg_from_sink(ctrl);
  456. }
  457. static void dp_hdcp2p2_link_check(struct dp_hdcp2p2_ctrl *ctrl)
  458. {
  459. int rc = 0;
  460. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_INVALID};
  461. if (!ctrl) {
  462. DP_ERR("invalid input\n");
  463. return;
  464. }
  465. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTH_FAIL ||
  466. atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  467. DP_ERR("invalid hdcp state\n");
  468. return;
  469. }
  470. cdata.context = ctrl->lib_ctx;
  471. if (ctrl->sink_rx_status & ctrl->abort_mask) {
  472. if (ctrl->sink_rx_status & BIT(3))
  473. DP_ERR("reauth_req set by sink\n");
  474. if (ctrl->sink_rx_status & BIT(4))
  475. DP_ERR("link failure reported by sink\n");
  476. ctrl->sink_rx_status = 0;
  477. ctrl->rx_status = 0;
  478. rc = -ENOLINK;
  479. cdata.cmd = HDCP_2X_CMD_LINK_FAILED;
  480. atomic_set(&ctrl->auth_state, HDCP_STATE_AUTH_FAIL);
  481. goto exit;
  482. }
  483. /* check if sink has made a message available */
  484. if (ctrl->polling && (ctrl->sink_rx_status & ctrl->rx_status)) {
  485. ctrl->sink_rx_status = 0;
  486. ctrl->rx_status = 0;
  487. dp_hdcp2p2_get_msg_from_sink(ctrl);
  488. ctrl->polling = false;
  489. }
  490. exit:
  491. if (rc)
  492. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  493. }
  494. static void dp_hdcp2p2_start_auth(struct dp_hdcp2p2_ctrl *ctrl)
  495. {
  496. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_START_AUTH};
  497. cdata.context = ctrl->lib_ctx;
  498. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTHENTICATING)
  499. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  500. }
  501. static int dp_hdcp2p2_read_rx_status(struct dp_hdcp2p2_ctrl *ctrl,
  502. u8 *rx_status)
  503. {
  504. u32 const cp_irq_dpcd_offset = 0x201;
  505. u32 const rxstatus_dpcd_offset = 0x69493;
  506. ssize_t const bytes_to_read = 1;
  507. ssize_t bytes_read = 0;
  508. u8 buf = 0;
  509. int rc = 0;
  510. bool cp_irq = false;
  511. *rx_status = 0;
  512. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  513. cp_irq_dpcd_offset, &buf, bytes_to_read);
  514. if (bytes_read != bytes_to_read) {
  515. DP_ERR("cp irq read failed\n");
  516. rc = bytes_read;
  517. goto error;
  518. }
  519. cp_irq = buf & BIT(2);
  520. DP_DEBUG("cp_irq=0x%x\n", cp_irq);
  521. buf = 0;
  522. if (cp_irq) {
  523. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  524. rxstatus_dpcd_offset, &buf, bytes_to_read);
  525. if (bytes_read != bytes_to_read) {
  526. DP_ERR("rxstatus read failed\n");
  527. rc = bytes_read;
  528. goto error;
  529. }
  530. *rx_status = buf;
  531. DP_DEBUG("rx_status=0x%x\n", *rx_status);
  532. }
  533. error:
  534. return rc;
  535. }
  536. static int dp_hdcp2p2_cp_irq(void *input)
  537. {
  538. int rc, retries = 15;
  539. struct dp_hdcp2p2_ctrl *ctrl = input;
  540. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_ENTRY);
  541. rc = dp_hdcp2p2_valid_handle(ctrl);
  542. if (rc)
  543. return rc;
  544. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTH_FAIL ||
  545. atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  546. DP_ERR("invalid hdcp state\n");
  547. return -EINVAL;
  548. }
  549. ctrl->sink_rx_status = 0;
  550. rc = dp_hdcp2p2_read_rx_status(ctrl, &ctrl->sink_rx_status);
  551. if (rc) {
  552. DP_ERR("failed to read rx status\n");
  553. return rc;
  554. }
  555. DP_DEBUG("sink_rx_status=0x%x\n", ctrl->sink_rx_status);
  556. if (!ctrl->sink_rx_status) {
  557. DP_DEBUG("not a hdcp 2.2 irq\n");
  558. return -EINVAL;
  559. }
  560. /*
  561. * Wait for link to be transitioned to polling mode. This wait
  562. * should be done in this CP_IRQ handler and NOT in the event thread
  563. * as the transition to link polling happens in the event thread
  564. * as part of the wake up from the HDCP engine.
  565. *
  566. * One specific case where this sequence of event commonly happens
  567. * is when executing HDCP 2.3 CTS test 1B-09 with Unigraf UCD-400
  568. * test equipment (TE). As part of this test, the TE issues a CP-IRQ
  569. * right after the successful completion of the HDCP authentication
  570. * part 2. This CP-IRQ handler gets invoked even before the HDCP
  571. * state engine gets transitioned to the polling mode, which can
  572. * cause the test to fail as we would not read the
  573. * RepeaterAuth_Send_ReceiverID_List from the TE in response to the
  574. * CP_IRQ.
  575. *
  576. * Skip this wait when any of the fields in the abort mask is set.
  577. */
  578. if (ctrl->sink_rx_status & ctrl->abort_mask)
  579. goto exit;
  580. while (!ctrl->polling && retries--)
  581. msleep(20);
  582. exit:
  583. kfifo_put(&ctrl->cmd_q, HDCP_TRANSPORT_CMD_LINK_CHECK);
  584. wake_up(&ctrl->wait_q);
  585. SDE_EVT32_EXTERNAL(SDE_EVTLOG_FUNC_EXIT);
  586. return 0;
  587. }
  588. static int dp_hdcp2p2_isr(void *input)
  589. {
  590. struct dp_hdcp2p2_ctrl *ctrl = (struct dp_hdcp2p2_ctrl *)input;
  591. int rc = 0;
  592. struct dss_io_data *io;
  593. struct dp_hdcp2p2_interrupts *intr;
  594. u32 hdcp_int_val = 0;
  595. if (!ctrl || !ctrl->init_data.dp_ahb) {
  596. DP_ERR("invalid input\n");
  597. rc = -EINVAL;
  598. goto end;
  599. }
  600. io = ctrl->init_data.dp_ahb;
  601. intr = ctrl->intr;
  602. while (intr && intr->reg) {
  603. struct dp_hdcp2p2_int_set *int_set = intr->int_set;
  604. hdcp_int_val = dp_read(io->base + intr->reg);
  605. while (int_set && int_set->interrupt) {
  606. if (hdcp_int_val & (int_set->interrupt >> 2)) {
  607. DP_DEBUG("%s\n", int_set->name);
  608. if (int_set->func)
  609. int_set->func(ctrl);
  610. dp_write(io->base + intr->reg, hdcp_int_val |
  611. (int_set->interrupt >> 1));
  612. }
  613. int_set++;
  614. }
  615. intr++;
  616. }
  617. end:
  618. return rc;
  619. }
  620. static bool dp_hdcp2p2_supported(void *input)
  621. {
  622. struct dp_hdcp2p2_ctrl *ctrl = input;
  623. u32 const rxcaps_dpcd_offset = 0x6921d;
  624. ssize_t bytes_read = 0;
  625. u8 buf[DP_HDCP_RXCAPS_LENGTH];
  626. DP_DEBUG("Checking sink capability\n");
  627. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  628. rxcaps_dpcd_offset, &buf, DP_HDCP_RXCAPS_LENGTH);
  629. if (bytes_read != DP_HDCP_RXCAPS_LENGTH) {
  630. DP_ERR("RxCaps read failed\n");
  631. goto error;
  632. }
  633. DP_DEBUG("HDCP_CAPABLE=%lu\n", (buf[2] & BIT(1)) >> 1);
  634. DP_DEBUG("VERSION=%d\n", buf[0]);
  635. if ((buf[2] & BIT(1)) && (buf[0] == 0x2))
  636. return true;
  637. error:
  638. return false;
  639. }
  640. static int dp_hdcp2p2_change_streams(struct dp_hdcp2p2_ctrl *ctrl,
  641. struct sde_hdcp_2x_wakeup_data *cdata)
  642. {
  643. if (!ctrl || cdata->num_streams == 0 || !cdata->streams) {
  644. DP_ERR("invalid input\n");
  645. return -EINVAL;
  646. }
  647. if (!ctrl->lib_ctx) {
  648. DP_ERR("HDCP library needs to be acquired\n");
  649. return -EINVAL;
  650. }
  651. if (!ctrl->lib) {
  652. DP_ERR("invalid lib ops data\n");
  653. return -EINVAL;
  654. }
  655. cdata->context = ctrl->lib_ctx;
  656. return ctrl->lib->wakeup(cdata);
  657. }
  658. static int dp_hdcp2p2_register_streams(void *input, u8 num_streams,
  659. struct stream_info *streams)
  660. {
  661. struct dp_hdcp2p2_ctrl *ctrl = input;
  662. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_OPEN_STREAMS};
  663. cdata.streams = streams;
  664. cdata.num_streams = num_streams;
  665. return dp_hdcp2p2_change_streams(ctrl, &cdata);
  666. }
  667. static int dp_hdcp2p2_deregister_streams(void *input, u8 num_streams,
  668. struct stream_info *streams)
  669. {
  670. struct dp_hdcp2p2_ctrl *ctrl = input;
  671. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_CLOSE_STREAMS};
  672. cdata.streams = streams;
  673. cdata.num_streams = num_streams;
  674. return dp_hdcp2p2_change_streams(ctrl, &cdata);
  675. }
  676. void sde_dp_hdcp2p2_deinit(void *input)
  677. {
  678. struct dp_hdcp2p2_ctrl *ctrl = (struct dp_hdcp2p2_ctrl *)input;
  679. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_INVALID};
  680. if (!ctrl) {
  681. DP_ERR("invalid input\n");
  682. return;
  683. }
  684. if (atomic_read(&ctrl->auth_state) != HDCP_STATE_AUTH_FAIL) {
  685. cdata.cmd = HDCP_2X_CMD_STOP;
  686. cdata.context = ctrl->lib_ctx;
  687. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  688. }
  689. sde_hdcp_2x_deregister(ctrl->lib_ctx);
  690. kthread_stop(ctrl->thread);
  691. mutex_destroy(&ctrl->mutex);
  692. mutex_destroy(&ctrl->msg_lock);
  693. kfree(ctrl);
  694. }
  695. static int dp_hdcp2p2_main(void *data)
  696. {
  697. struct dp_hdcp2p2_ctrl *ctrl = data;
  698. enum hdcp_transport_wakeup_cmd cmd;
  699. while (1) {
  700. wait_event(ctrl->wait_q,
  701. !kfifo_is_empty(&ctrl->cmd_q) ||
  702. kthread_should_stop() ||
  703. kthread_should_park());
  704. if (kthread_should_stop())
  705. break;
  706. if (kfifo_is_empty(&ctrl->cmd_q) && kthread_should_park()) {
  707. kthread_parkme();
  708. continue;
  709. }
  710. if (!kfifo_get(&ctrl->cmd_q, &cmd))
  711. continue;
  712. switch (cmd) {
  713. case HDCP_TRANSPORT_CMD_SEND_MESSAGE:
  714. dp_hdcp2p2_send_msg(ctrl);
  715. break;
  716. case HDCP_TRANSPORT_CMD_RECV_MESSAGE:
  717. if (ctrl->rx_status)
  718. ctrl->polling = true;
  719. else
  720. dp_hdcp2p2_recv_msg(ctrl);
  721. break;
  722. case HDCP_TRANSPORT_CMD_STATUS_SUCCESS:
  723. dp_hdcp2p2_send_auth_status(ctrl);
  724. break;
  725. case HDCP_TRANSPORT_CMD_STATUS_FAILED:
  726. dp_hdcp2p2_set_interrupts(ctrl, false);
  727. dp_hdcp2p2_send_auth_status(ctrl);
  728. break;
  729. case HDCP_TRANSPORT_CMD_LINK_POLL:
  730. ctrl->polling = true;
  731. break;
  732. case HDCP_TRANSPORT_CMD_LINK_CHECK:
  733. dp_hdcp2p2_link_check(ctrl);
  734. break;
  735. case HDCP_TRANSPORT_CMD_AUTHENTICATE:
  736. dp_hdcp2p2_start_auth(ctrl);
  737. break;
  738. default:
  739. break;
  740. }
  741. }
  742. return 0;
  743. }
  744. static void dp_hdcp2p2_abort(void *input, bool abort)
  745. {
  746. struct dp_hdcp2p2_ctrl *ctrl = input;
  747. atomic_set(&ctrl->abort, abort);
  748. }
  749. void *sde_dp_hdcp2p2_init(struct sde_hdcp_init_data *init_data)
  750. {
  751. int rc;
  752. struct dp_hdcp2p2_ctrl *ctrl;
  753. static struct sde_hdcp_ops ops = {
  754. .isr = dp_hdcp2p2_isr,
  755. .reauthenticate = dp_hdcp2p2_reauthenticate,
  756. .authenticate = dp_hdcp2p2_authenticate,
  757. .feature_supported = dp_hdcp2p2_feature_supported,
  758. .force_encryption = dp_hdcp2p2_force_encryption,
  759. .sink_support = dp_hdcp2p2_supported,
  760. .set_mode = dp_hdcp2p2_register,
  761. .on = dp_hdcp2p2_on,
  762. .off = dp_hdcp2p2_off,
  763. .abort = dp_hdcp2p2_abort,
  764. .cp_irq = dp_hdcp2p2_cp_irq,
  765. .register_streams = dp_hdcp2p2_register_streams,
  766. .deregister_streams = dp_hdcp2p2_deregister_streams,
  767. };
  768. static struct hdcp_transport_ops client_ops = {
  769. .wakeup = dp_hdcp2p2_wakeup,
  770. };
  771. static struct dp_hdcp2p2_int_set int_set1[] = {
  772. {BIT(17), "authentication successful", NULL},
  773. {BIT(20), "authentication failed", NULL},
  774. {BIT(24), "encryption enabled", NULL},
  775. {BIT(27), "encryption disabled", NULL},
  776. {0},
  777. };
  778. static struct dp_hdcp2p2_int_set int_set2[] = {
  779. {BIT(2), "key fifo underflow", NULL},
  780. {0},
  781. };
  782. static struct dp_hdcp2p2_interrupts intr[] = {
  783. {DP_INTR_STATUS2, int_set1},
  784. {DP_INTR_STATUS3, int_set2},
  785. {0}
  786. };
  787. static struct sde_hdcp_2x_ops hdcp2x_ops;
  788. struct sde_hdcp_2x_register_data register_data = {0};
  789. if (!init_data || !init_data->cb_data ||
  790. !init_data->notify_status || !init_data->drm_aux) {
  791. DP_ERR("invalid input\n");
  792. return ERR_PTR(-EINVAL);
  793. }
  794. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  795. if (!ctrl)
  796. return ERR_PTR(-ENOMEM);
  797. ctrl->init_data = *init_data;
  798. ctrl->lib = &hdcp2x_ops;
  799. ctrl->response.data = NULL;
  800. ctrl->request.data = NULL;
  801. ctrl->sink_status = SINK_DISCONNECTED;
  802. ctrl->intr = intr;
  803. INIT_KFIFO(ctrl->cmd_q);
  804. init_waitqueue_head(&ctrl->wait_q);
  805. atomic_set(&ctrl->auth_state, HDCP_STATE_INACTIVE);
  806. ctrl->ops = &ops;
  807. mutex_init(&ctrl->mutex);
  808. mutex_init(&ctrl->msg_lock);
  809. register_data.hdcp_data = &ctrl->lib_ctx;
  810. register_data.client_ops = &client_ops;
  811. register_data.ops = &hdcp2x_ops;
  812. register_data.client_data = ctrl;
  813. rc = sde_hdcp_2x_register(&register_data);
  814. if (rc) {
  815. DP_ERR("Unable to register with HDCP 2.2 library\n");
  816. goto error;
  817. }
  818. if (IS_ENABLED(CONFIG_HDCP_QSEECOM))
  819. msm_hdcp_register_cb(init_data->msm_hdcp_dev, ctrl,
  820. dp_hdcp2p2_min_level_change);
  821. ctrl->thread = kthread_run(dp_hdcp2p2_main, ctrl, "dp_hdcp2p2");
  822. if (IS_ERR(ctrl->thread)) {
  823. DP_ERR("unable to start DP hdcp2p2 thread\n");
  824. rc = PTR_ERR(ctrl->thread);
  825. ctrl->thread = NULL;
  826. goto error;
  827. }
  828. return ctrl;
  829. error:
  830. kfree(ctrl);
  831. return ERR_PTR(rc);
  832. }
  833. struct sde_hdcp_ops *sde_dp_hdcp2p2_get(void *input)
  834. {
  835. return ((struct dp_hdcp2p2_ctrl *)input)->ops;
  836. }