dp_hdcp2p2.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2020, 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. if (!data) {
  146. DP_ERR("invalid input\n");
  147. return -EINVAL;
  148. }
  149. ctrl = data->context;
  150. if (!ctrl) {
  151. DP_ERR("invalid ctrl\n");
  152. return -EINVAL;
  153. }
  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. cdata.context = ctrl->lib_ctx;
  282. cdata.min_enc_level = min_enc_level;
  283. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  284. }
  285. static int dp_hdcp2p2_aux_read_message(struct dp_hdcp2p2_ctrl *ctrl)
  286. {
  287. int rc = 0, max_size = 16, read_size = 0, bytes_read = 0;
  288. int size = ctrl->request.length, offset = ctrl->msg_part->offset;
  289. u8 *buf = ctrl->request.data;
  290. s64 diff_ms;
  291. ktime_t start_read, finish_read;
  292. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE ||
  293. atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTH_FAIL) {
  294. DP_ERR("invalid hdcp state\n");
  295. rc = -EINVAL;
  296. goto exit;
  297. }
  298. if (!buf) {
  299. DP_ERR("invalid request buffer\n");
  300. rc = -EINVAL;
  301. goto exit;
  302. }
  303. DP_DEBUG("offset(0x%x), size(%d)\n", offset, size);
  304. start_read = ktime_get();
  305. do {
  306. read_size = min(size, max_size);
  307. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  308. offset, buf, read_size);
  309. if (bytes_read != read_size) {
  310. DP_ERR("fail: offset(0x%x), size(0x%x), rc(0x%x)\n",
  311. offset, read_size, bytes_read);
  312. rc = -EINVAL;
  313. break;
  314. }
  315. buf += read_size;
  316. offset += read_size;
  317. size -= read_size;
  318. } while (size > 0);
  319. finish_read = ktime_get();
  320. diff_ms = ktime_ms_delta(finish_read, start_read);
  321. if (ctrl->transaction_timeout && diff_ms > ctrl->transaction_timeout) {
  322. DP_ERR("HDCP read timeout exceeded (%dms > %dms)\n", diff_ms,
  323. ctrl->transaction_timeout);
  324. rc = -ETIMEDOUT;
  325. }
  326. exit:
  327. return rc;
  328. }
  329. static int dp_hdcp2p2_aux_write_message(struct dp_hdcp2p2_ctrl *ctrl,
  330. u8 *buf, int size, uint offset, uint timeout)
  331. {
  332. int const max_size = 16;
  333. int rc = 0, write_size = 0, bytes_written = 0;
  334. DP_DEBUG("offset(0x%x), size(%d)\n", offset, size);
  335. do {
  336. write_size = min(size, max_size);
  337. bytes_written = drm_dp_dpcd_write(ctrl->init_data.drm_aux,
  338. offset, buf, write_size);
  339. if (bytes_written != write_size) {
  340. DP_ERR("fail: offset(0x%x), size(0x%x), rc(0x%x)\n",
  341. offset, write_size, bytes_written);
  342. rc = -EINVAL;
  343. break;
  344. }
  345. buf += write_size;
  346. offset += write_size;
  347. size -= write_size;
  348. } while (size > 0);
  349. return rc;
  350. }
  351. static bool dp_hdcp2p2_feature_supported(void *input)
  352. {
  353. int rc;
  354. struct dp_hdcp2p2_ctrl *ctrl = input;
  355. struct sde_hdcp_2x_ops *lib = NULL;
  356. bool supported = false;
  357. rc = dp_hdcp2p2_valid_handle(ctrl);
  358. if (rc)
  359. return supported;
  360. lib = ctrl->lib;
  361. if (lib->feature_supported)
  362. supported = lib->feature_supported(
  363. ctrl->lib_ctx);
  364. return supported;
  365. }
  366. static void dp_hdcp2p2_force_encryption(void *data, bool enable)
  367. {
  368. int rc;
  369. struct dp_hdcp2p2_ctrl *ctrl = data;
  370. struct sde_hdcp_2x_ops *lib = NULL;
  371. rc = dp_hdcp2p2_valid_handle(ctrl);
  372. if (rc)
  373. return;
  374. lib = ctrl->lib;
  375. if (lib->force_encryption)
  376. lib->force_encryption(ctrl->lib_ctx, enable);
  377. }
  378. static void dp_hdcp2p2_send_msg(struct dp_hdcp2p2_ctrl *ctrl)
  379. {
  380. int rc = 0;
  381. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_INVALID};
  382. if (!ctrl) {
  383. DP_ERR("invalid input\n");
  384. rc = -EINVAL;
  385. goto exit;
  386. }
  387. cdata.context = ctrl->lib_ctx;
  388. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  389. DP_ERR("hdcp is off\n");
  390. goto exit;
  391. }
  392. mutex_lock(&ctrl->msg_lock);
  393. rc = dp_hdcp2p2_aux_write_message(ctrl, ctrl->response.data,
  394. ctrl->response.length, ctrl->msg_part->offset,
  395. ctrl->transaction_delay);
  396. if (rc) {
  397. DP_ERR("Error sending msg to sink %d\n", rc);
  398. mutex_unlock(&ctrl->msg_lock);
  399. goto exit;
  400. }
  401. cdata.cmd = HDCP_2X_CMD_MSG_SEND_SUCCESS;
  402. cdata.timeout = ctrl->transaction_delay;
  403. mutex_unlock(&ctrl->msg_lock);
  404. exit:
  405. if (rc == -ETIMEDOUT)
  406. cdata.cmd = HDCP_2X_CMD_MSG_SEND_TIMEOUT;
  407. else if (rc)
  408. cdata.cmd = HDCP_2X_CMD_MSG_SEND_FAILED;
  409. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  410. }
  411. static int dp_hdcp2p2_get_msg_from_sink(struct dp_hdcp2p2_ctrl *ctrl)
  412. {
  413. int rc = 0;
  414. struct sde_hdcp_2x_wakeup_data cdata = { HDCP_2X_CMD_INVALID };
  415. cdata.context = ctrl->lib_ctx;
  416. rc = dp_hdcp2p2_aux_read_message(ctrl);
  417. if (rc) {
  418. DP_ERR("error reading message %d\n", rc);
  419. goto exit;
  420. }
  421. cdata.total_message_length = ctrl->total_message_length;
  422. cdata.timeout = ctrl->transaction_delay;
  423. exit:
  424. if (rc == -ETIMEDOUT)
  425. cdata.cmd = HDCP_2X_CMD_MSG_RECV_TIMEOUT;
  426. else if (rc)
  427. cdata.cmd = HDCP_2X_CMD_MSG_RECV_FAILED;
  428. else
  429. cdata.cmd = HDCP_2X_CMD_MSG_RECV_SUCCESS;
  430. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  431. return rc;
  432. }
  433. static void dp_hdcp2p2_recv_msg(struct dp_hdcp2p2_ctrl *ctrl)
  434. {
  435. struct sde_hdcp_2x_wakeup_data cdata = { HDCP_2X_CMD_INVALID };
  436. cdata.context = ctrl->lib_ctx;
  437. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  438. DP_ERR("hdcp is off\n");
  439. return;
  440. }
  441. if (ctrl->transaction_delay)
  442. msleep(ctrl->transaction_delay);
  443. dp_hdcp2p2_get_msg_from_sink(ctrl);
  444. }
  445. static void dp_hdcp2p2_link_check(struct dp_hdcp2p2_ctrl *ctrl)
  446. {
  447. int rc = 0, retries = 10;
  448. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_INVALID};
  449. if (!ctrl) {
  450. DP_ERR("invalid input\n");
  451. return;
  452. }
  453. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTH_FAIL ||
  454. atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  455. DP_ERR("invalid hdcp state\n");
  456. return;
  457. }
  458. cdata.context = ctrl->lib_ctx;
  459. if (ctrl->sink_rx_status & ctrl->abort_mask) {
  460. if (ctrl->sink_rx_status & BIT(3))
  461. DP_ERR("reauth_req set by sink\n");
  462. if (ctrl->sink_rx_status & BIT(4))
  463. DP_ERR("link failure reported by sink\n");
  464. ctrl->sink_rx_status = 0;
  465. ctrl->rx_status = 0;
  466. rc = -ENOLINK;
  467. cdata.cmd = HDCP_2X_CMD_LINK_FAILED;
  468. atomic_set(&ctrl->auth_state, HDCP_STATE_AUTH_FAIL);
  469. goto exit;
  470. }
  471. /* wait for polling to start till spec allowed timeout */
  472. while (!ctrl->polling && retries--)
  473. msleep(20);
  474. /* check if sink has made a message available */
  475. if (ctrl->polling && (ctrl->sink_rx_status & ctrl->rx_status)) {
  476. ctrl->sink_rx_status = 0;
  477. ctrl->rx_status = 0;
  478. dp_hdcp2p2_get_msg_from_sink(ctrl);
  479. ctrl->polling = false;
  480. }
  481. exit:
  482. if (rc)
  483. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  484. }
  485. static void dp_hdcp2p2_start_auth(struct dp_hdcp2p2_ctrl *ctrl)
  486. {
  487. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_START_AUTH};
  488. cdata.context = ctrl->lib_ctx;
  489. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTHENTICATING)
  490. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  491. }
  492. static int dp_hdcp2p2_read_rx_status(struct dp_hdcp2p2_ctrl *ctrl,
  493. u8 *rx_status)
  494. {
  495. u32 const cp_irq_dpcd_offset = 0x201;
  496. u32 const rxstatus_dpcd_offset = 0x69493;
  497. ssize_t const bytes_to_read = 1;
  498. ssize_t bytes_read = 0;
  499. u8 buf = 0;
  500. int rc = 0;
  501. bool cp_irq = false;
  502. *rx_status = 0;
  503. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  504. cp_irq_dpcd_offset, &buf, bytes_to_read);
  505. if (bytes_read != bytes_to_read) {
  506. DP_ERR("cp irq read failed\n");
  507. rc = bytes_read;
  508. goto error;
  509. }
  510. cp_irq = buf & BIT(2);
  511. DP_DEBUG("cp_irq=0x%x\n", cp_irq);
  512. buf = 0;
  513. if (cp_irq) {
  514. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  515. rxstatus_dpcd_offset, &buf, bytes_to_read);
  516. if (bytes_read != bytes_to_read) {
  517. DP_ERR("rxstatus read failed\n");
  518. rc = bytes_read;
  519. goto error;
  520. }
  521. *rx_status = buf;
  522. DP_DEBUG("rx_status=0x%x\n", *rx_status);
  523. }
  524. error:
  525. return rc;
  526. }
  527. static int dp_hdcp2p2_cp_irq(void *input)
  528. {
  529. int rc;
  530. struct dp_hdcp2p2_ctrl *ctrl = input;
  531. rc = dp_hdcp2p2_valid_handle(ctrl);
  532. if (rc)
  533. return rc;
  534. if (atomic_read(&ctrl->auth_state) == HDCP_STATE_AUTH_FAIL ||
  535. atomic_read(&ctrl->auth_state) == HDCP_STATE_INACTIVE) {
  536. DP_ERR("invalid hdcp state\n");
  537. return -EINVAL;
  538. }
  539. ctrl->sink_rx_status = 0;
  540. rc = dp_hdcp2p2_read_rx_status(ctrl, &ctrl->sink_rx_status);
  541. if (rc) {
  542. DP_ERR("failed to read rx status\n");
  543. return rc;
  544. }
  545. DP_DEBUG("sink_rx_status=0x%x\n", ctrl->sink_rx_status);
  546. if (!ctrl->sink_rx_status) {
  547. DP_DEBUG("not a hdcp 2.2 irq\n");
  548. return -EINVAL;
  549. }
  550. kfifo_put(&ctrl->cmd_q, HDCP_TRANSPORT_CMD_LINK_CHECK);
  551. wake_up(&ctrl->wait_q);
  552. return 0;
  553. }
  554. static int dp_hdcp2p2_isr(void *input)
  555. {
  556. struct dp_hdcp2p2_ctrl *ctrl = (struct dp_hdcp2p2_ctrl *)input;
  557. int rc = 0;
  558. struct dss_io_data *io;
  559. struct dp_hdcp2p2_interrupts *intr;
  560. u32 hdcp_int_val = 0;
  561. if (!ctrl || !ctrl->init_data.dp_ahb) {
  562. DP_ERR("invalid input\n");
  563. rc = -EINVAL;
  564. goto end;
  565. }
  566. io = ctrl->init_data.dp_ahb;
  567. intr = ctrl->intr;
  568. while (intr && intr->reg) {
  569. struct dp_hdcp2p2_int_set *int_set = intr->int_set;
  570. hdcp_int_val = dp_read(io->base + intr->reg);
  571. while (int_set && int_set->interrupt) {
  572. if (hdcp_int_val & (int_set->interrupt >> 2)) {
  573. DP_DEBUG("%s\n", int_set->name);
  574. if (int_set->func)
  575. int_set->func(ctrl);
  576. dp_write(io->base + intr->reg, hdcp_int_val |
  577. (int_set->interrupt >> 1));
  578. }
  579. int_set++;
  580. }
  581. intr++;
  582. }
  583. end:
  584. return rc;
  585. }
  586. static bool dp_hdcp2p2_supported(void *input)
  587. {
  588. struct dp_hdcp2p2_ctrl *ctrl = input;
  589. u32 const rxcaps_dpcd_offset = 0x6921d;
  590. ssize_t bytes_read = 0;
  591. u8 buf[DP_HDCP_RXCAPS_LENGTH];
  592. DP_DEBUG("Checking sink capability\n");
  593. bytes_read = drm_dp_dpcd_read(ctrl->init_data.drm_aux,
  594. rxcaps_dpcd_offset, &buf, DP_HDCP_RXCAPS_LENGTH);
  595. if (bytes_read != DP_HDCP_RXCAPS_LENGTH) {
  596. DP_ERR("RxCaps read failed\n");
  597. goto error;
  598. }
  599. DP_DEBUG("HDCP_CAPABLE=%lu\n", (buf[2] & BIT(1)) >> 1);
  600. DP_DEBUG("VERSION=%d\n", buf[0]);
  601. if ((buf[2] & BIT(1)) && (buf[0] == 0x2))
  602. return true;
  603. error:
  604. return false;
  605. }
  606. static int dp_hdcp2p2_change_streams(struct dp_hdcp2p2_ctrl *ctrl,
  607. struct sde_hdcp_2x_wakeup_data *cdata)
  608. {
  609. if (!ctrl || cdata->num_streams == 0 || !cdata->streams) {
  610. DP_ERR("invalid input\n");
  611. return -EINVAL;
  612. }
  613. if (!ctrl->lib_ctx) {
  614. DP_ERR("HDCP library needs to be acquired\n");
  615. return -EINVAL;
  616. }
  617. if (!ctrl->lib) {
  618. DP_ERR("invalid lib ops data\n");
  619. return -EINVAL;
  620. }
  621. cdata->context = ctrl->lib_ctx;
  622. return ctrl->lib->wakeup(cdata);
  623. }
  624. static int dp_hdcp2p2_register_streams(void *input, u8 num_streams,
  625. struct stream_info *streams)
  626. {
  627. struct dp_hdcp2p2_ctrl *ctrl = input;
  628. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_OPEN_STREAMS};
  629. cdata.streams = streams;
  630. cdata.num_streams = num_streams;
  631. return dp_hdcp2p2_change_streams(ctrl, &cdata);
  632. }
  633. static int dp_hdcp2p2_deregister_streams(void *input, u8 num_streams,
  634. struct stream_info *streams)
  635. {
  636. struct dp_hdcp2p2_ctrl *ctrl = input;
  637. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_CLOSE_STREAMS};
  638. cdata.streams = streams;
  639. cdata.num_streams = num_streams;
  640. return dp_hdcp2p2_change_streams(ctrl, &cdata);
  641. }
  642. void sde_dp_hdcp2p2_deinit(void *input)
  643. {
  644. struct dp_hdcp2p2_ctrl *ctrl = (struct dp_hdcp2p2_ctrl *)input;
  645. struct sde_hdcp_2x_wakeup_data cdata = {HDCP_2X_CMD_INVALID};
  646. if (!ctrl) {
  647. DP_ERR("invalid input\n");
  648. return;
  649. }
  650. if (atomic_read(&ctrl->auth_state) != HDCP_STATE_AUTH_FAIL) {
  651. cdata.cmd = HDCP_2X_CMD_STOP;
  652. cdata.context = ctrl->lib_ctx;
  653. dp_hdcp2p2_wakeup_lib(ctrl, &cdata);
  654. }
  655. sde_hdcp_2x_deregister(ctrl->lib_ctx);
  656. kthread_stop(ctrl->thread);
  657. mutex_destroy(&ctrl->mutex);
  658. mutex_destroy(&ctrl->msg_lock);
  659. kfree(ctrl);
  660. }
  661. static int dp_hdcp2p2_main(void *data)
  662. {
  663. struct dp_hdcp2p2_ctrl *ctrl = data;
  664. enum hdcp_transport_wakeup_cmd cmd;
  665. while (1) {
  666. wait_event(ctrl->wait_q,
  667. !kfifo_is_empty(&ctrl->cmd_q) ||
  668. kthread_should_stop() ||
  669. kthread_should_park());
  670. if (kthread_should_stop())
  671. break;
  672. if (kfifo_is_empty(&ctrl->cmd_q) && kthread_should_park()) {
  673. kthread_parkme();
  674. continue;
  675. }
  676. if (!kfifo_get(&ctrl->cmd_q, &cmd))
  677. continue;
  678. switch (cmd) {
  679. case HDCP_TRANSPORT_CMD_SEND_MESSAGE:
  680. dp_hdcp2p2_send_msg(ctrl);
  681. break;
  682. case HDCP_TRANSPORT_CMD_RECV_MESSAGE:
  683. if (ctrl->rx_status)
  684. ctrl->polling = true;
  685. else
  686. dp_hdcp2p2_recv_msg(ctrl);
  687. break;
  688. case HDCP_TRANSPORT_CMD_STATUS_SUCCESS:
  689. dp_hdcp2p2_send_auth_status(ctrl);
  690. break;
  691. case HDCP_TRANSPORT_CMD_STATUS_FAILED:
  692. dp_hdcp2p2_set_interrupts(ctrl, false);
  693. dp_hdcp2p2_send_auth_status(ctrl);
  694. break;
  695. case HDCP_TRANSPORT_CMD_LINK_POLL:
  696. ctrl->polling = true;
  697. break;
  698. case HDCP_TRANSPORT_CMD_LINK_CHECK:
  699. dp_hdcp2p2_link_check(ctrl);
  700. break;
  701. case HDCP_TRANSPORT_CMD_AUTHENTICATE:
  702. dp_hdcp2p2_start_auth(ctrl);
  703. break;
  704. default:
  705. break;
  706. }
  707. }
  708. return 0;
  709. }
  710. static void dp_hdcp2p2_abort(void *input, bool abort)
  711. {
  712. struct dp_hdcp2p2_ctrl *ctrl = input;
  713. atomic_set(&ctrl->abort, abort);
  714. }
  715. void *sde_dp_hdcp2p2_init(struct sde_hdcp_init_data *init_data)
  716. {
  717. int rc;
  718. struct dp_hdcp2p2_ctrl *ctrl;
  719. static struct sde_hdcp_ops ops = {
  720. .isr = dp_hdcp2p2_isr,
  721. .reauthenticate = dp_hdcp2p2_reauthenticate,
  722. .authenticate = dp_hdcp2p2_authenticate,
  723. .feature_supported = dp_hdcp2p2_feature_supported,
  724. .force_encryption = dp_hdcp2p2_force_encryption,
  725. .sink_support = dp_hdcp2p2_supported,
  726. .set_mode = dp_hdcp2p2_register,
  727. .on = dp_hdcp2p2_on,
  728. .off = dp_hdcp2p2_off,
  729. .abort = dp_hdcp2p2_abort,
  730. .cp_irq = dp_hdcp2p2_cp_irq,
  731. .register_streams = dp_hdcp2p2_register_streams,
  732. .deregister_streams = dp_hdcp2p2_deregister_streams,
  733. };
  734. static struct hdcp_transport_ops client_ops = {
  735. .wakeup = dp_hdcp2p2_wakeup,
  736. };
  737. static struct dp_hdcp2p2_int_set int_set1[] = {
  738. {BIT(17), "authentication successful", NULL},
  739. {BIT(20), "authentication failed", NULL},
  740. {BIT(24), "encryption enabled", NULL},
  741. {BIT(27), "encryption disabled", NULL},
  742. {0},
  743. };
  744. static struct dp_hdcp2p2_int_set int_set2[] = {
  745. {BIT(2), "key fifo underflow", NULL},
  746. {0},
  747. };
  748. static struct dp_hdcp2p2_interrupts intr[] = {
  749. {DP_INTR_STATUS2, int_set1},
  750. {DP_INTR_STATUS3, int_set2},
  751. {0}
  752. };
  753. static struct sde_hdcp_2x_ops hdcp2x_ops;
  754. struct sde_hdcp_2x_register_data register_data = {0};
  755. if (!init_data || !init_data->cb_data ||
  756. !init_data->notify_status || !init_data->drm_aux) {
  757. DP_ERR("invalid input\n");
  758. return ERR_PTR(-EINVAL);
  759. }
  760. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  761. if (!ctrl)
  762. return ERR_PTR(-ENOMEM);
  763. ctrl->init_data = *init_data;
  764. ctrl->lib = &hdcp2x_ops;
  765. ctrl->response.data = NULL;
  766. ctrl->request.data = NULL;
  767. ctrl->sink_status = SINK_DISCONNECTED;
  768. ctrl->intr = intr;
  769. INIT_KFIFO(ctrl->cmd_q);
  770. init_waitqueue_head(&ctrl->wait_q);
  771. atomic_set(&ctrl->auth_state, HDCP_STATE_INACTIVE);
  772. ctrl->ops = &ops;
  773. mutex_init(&ctrl->mutex);
  774. mutex_init(&ctrl->msg_lock);
  775. register_data.hdcp_data = &ctrl->lib_ctx;
  776. register_data.client_ops = &client_ops;
  777. register_data.ops = &hdcp2x_ops;
  778. register_data.client_data = ctrl;
  779. rc = sde_hdcp_2x_register(&register_data);
  780. if (rc) {
  781. DP_ERR("Unable to register with HDCP 2.2 library\n");
  782. goto error;
  783. }
  784. if (IS_ENABLED(CONFIG_HDCP_QSEECOM))
  785. msm_hdcp_register_cb(init_data->msm_hdcp_dev, ctrl,
  786. dp_hdcp2p2_min_level_change);
  787. ctrl->thread = kthread_run(dp_hdcp2p2_main, ctrl, "dp_hdcp2p2");
  788. if (IS_ERR(ctrl->thread)) {
  789. DP_ERR("unable to start DP hdcp2p2 thread\n");
  790. rc = PTR_ERR(ctrl->thread);
  791. ctrl->thread = NULL;
  792. goto error;
  793. }
  794. return ctrl;
  795. error:
  796. kfree(ctrl);
  797. return ERR_PTR(rc);
  798. }
  799. struct sde_hdcp_ops *sde_dp_hdcp2p2_get(void *input)
  800. {
  801. return ((struct dp_hdcp2p2_ctrl *)input)->ops;
  802. }