dp_hdcp2p2.c 23 KB

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