dp_hdcp2p2.c 22 KB

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