dp_hdcp2p2.c 24 KB

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