dp_hdcp2p2.c 24 KB

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