dp_hdcp2p2.c 24 KB

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