sde_hdcp_1x.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[sde-hdcp1x] %s: " fmt, __func__
  6. #include <linux/io.h>
  7. #include <linux/types.h>
  8. #include <linux/delay.h>
  9. #include <linux/slab.h>
  10. #include <linux/stat.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/msm_hdcp.h>
  13. #include <drm/drm_dp_helper.h>
  14. #include "sde_hdcp.h"
  15. #include "hdcp/msm_hdmi_hdcp_mgr.h"
  16. #include "dp/dp_reg.h"
  17. #define SDE_HDCP_STATE_NAME (sde_hdcp_state_name(hdcp->hdcp_state))
  18. /* QFPROM Registers for HDMI/HDCP */
  19. #define QFPROM_RAW_FEAT_CONFIG_ROW0_LSB (0x000000F8)
  20. #define QFPROM_RAW_FEAT_CONFIG_ROW0_MSB (0x000000FC)
  21. #define QFPROM_RAW_VERSION_4 (0x000000A8)
  22. #define SEC_CTRL_HW_VERSION (0x00006000)
  23. #define HDCP_KSV_LSB (0x000060D8)
  24. #define HDCP_KSV_MSB (0x000060DC)
  25. #define HDCP_KSV_VERSION_4_OFFSET (0x00000014)
  26. /* SEC_CTRL version that supports HDCP SEL */
  27. #define HDCP_SEL_MIN_SEC_VERSION (0x50010000)
  28. /* HDCP Keys state based on HDMI_HDCP_LINK0_STATUS:KEYS_STATE */
  29. #define HDCP_KEYS_STATE_NO_KEYS 0
  30. #define HDCP_KEYS_STATE_NOT_CHECKED 1
  31. #define HDCP_KEYS_STATE_CHECKING 2
  32. #define HDCP_KEYS_STATE_VALID 3
  33. #define HDCP_KEYS_STATE_AKSV_NOT_VALID 4
  34. #define HDCP_KEYS_STATE_CHKSUM_MISMATCH 5
  35. #define HDCP_KEYS_STATE_PROD_AKSV 6
  36. #define HDCP_KEYS_STATE_RESERVED 7
  37. #define TZ_HDCP_CMD_ID 0x00004401
  38. #define HDCP_INT_CLR (isr->auth_success_ack | isr->auth_fail_ack | \
  39. isr->auth_fail_info_ack | isr->tx_req_ack | \
  40. isr->encryption_ready_ack | \
  41. isr->encryption_not_ready_ack | isr->tx_req_done_ack)
  42. #define HDCP_INT_EN (isr->auth_success_mask | isr->auth_fail_mask | \
  43. isr->encryption_ready_mask | \
  44. isr->encryption_not_ready_mask)
  45. #define HDCP_POLL_SLEEP_US (20 * 1000)
  46. #define HDCP_POLL_TIMEOUT_US (HDCP_POLL_SLEEP_US * 100)
  47. #define sde_hdcp_1x_state(x) (hdcp->hdcp_state == x)
  48. struct sde_hdcp_sink_addr {
  49. char *name;
  50. u32 addr;
  51. u32 len;
  52. };
  53. struct sde_hdcp_1x_reg_data {
  54. u32 reg_id;
  55. struct sde_hdcp_sink_addr *sink;
  56. };
  57. struct sde_hdcp_sink_addr_map {
  58. /* addresses to read from sink */
  59. struct sde_hdcp_sink_addr bcaps;
  60. struct sde_hdcp_sink_addr bksv;
  61. struct sde_hdcp_sink_addr r0;
  62. struct sde_hdcp_sink_addr bstatus;
  63. struct sde_hdcp_sink_addr cp_irq_status;
  64. struct sde_hdcp_sink_addr ksv_fifo;
  65. struct sde_hdcp_sink_addr v_h0;
  66. struct sde_hdcp_sink_addr v_h1;
  67. struct sde_hdcp_sink_addr v_h2;
  68. struct sde_hdcp_sink_addr v_h3;
  69. struct sde_hdcp_sink_addr v_h4;
  70. /* addresses to write to sink */
  71. struct sde_hdcp_sink_addr an;
  72. struct sde_hdcp_sink_addr aksv;
  73. struct sde_hdcp_sink_addr ainfo;
  74. };
  75. struct sde_hdcp_int_set {
  76. /* interrupt register */
  77. u32 int_reg;
  78. /* interrupt enable/disable masks */
  79. u32 auth_success_mask;
  80. u32 auth_fail_mask;
  81. u32 encryption_ready_mask;
  82. u32 encryption_not_ready_mask;
  83. u32 tx_req_mask;
  84. u32 tx_req_done_mask;
  85. /* interrupt acknowledgment */
  86. u32 auth_success_ack;
  87. u32 auth_fail_ack;
  88. u32 auth_fail_info_ack;
  89. u32 encryption_ready_ack;
  90. u32 encryption_not_ready_ack;
  91. u32 tx_req_ack;
  92. u32 tx_req_done_ack;
  93. /* interrupt status */
  94. u32 auth_success_int;
  95. u32 auth_fail_int;
  96. u32 encryption_ready;
  97. u32 encryption_not_ready;
  98. u32 tx_req_int;
  99. u32 tx_req_done_int;
  100. };
  101. struct sde_hdcp_reg_set {
  102. u32 status;
  103. u32 keys_offset;
  104. u32 r0_offset;
  105. u32 v_offset;
  106. u32 ctrl;
  107. u32 aksv_lsb;
  108. u32 aksv_msb;
  109. u32 entropy_ctrl0;
  110. u32 entropy_ctrl1;
  111. u32 sec_sha_ctrl;
  112. u32 sec_sha_data;
  113. u32 sha_status;
  114. u32 data2_0;
  115. u32 data3;
  116. u32 data4;
  117. u32 data5;
  118. u32 data6;
  119. u32 sec_data0;
  120. u32 sec_data1;
  121. u32 sec_data7;
  122. u32 sec_data8;
  123. u32 sec_data9;
  124. u32 sec_data10;
  125. u32 sec_data11;
  126. u32 sec_data12;
  127. u32 reset;
  128. u32 reset_bit;
  129. u32 repeater;
  130. };
  131. #define HDCP_REG_SET_CLIENT_HDMI \
  132. {0}
  133. #define HDCP_REG_SET_CLIENT_DP \
  134. {DP_HDCP_STATUS, 16, 14, 13, DP_HDCP_CTRL, \
  135. DP_HDCP_SW_LOWER_AKSV, DP_HDCP_SW_UPPER_AKSV, \
  136. DP_HDCP_ENTROPY_CTRL0, DP_HDCP_ENTROPY_CTRL1, \
  137. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_SHA_CTRL, \
  138. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_SHA_DATA, \
  139. DP_HDCP_SHA_STATUS, DP_HDCP_RCVPORT_DATA2_0, \
  140. DP_HDCP_RCVPORT_DATA3, DP_HDCP_RCVPORT_DATA4, \
  141. DP_HDCP_RCVPORT_DATA5, DP_HDCP_RCVPORT_DATA6, \
  142. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA0, \
  143. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA1, \
  144. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA7, \
  145. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA8, \
  146. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA9, \
  147. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA10, \
  148. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA11, \
  149. HDCP_SEC_DP_TZ_HV_HLOS_HDCP_RCVPORT_DATA12, \
  150. DP_SW_RESET, BIT(1), BIT(1)}
  151. #define HDCP_HDMI_SINK_ADDR_MAP \
  152. {{"bcaps", 0x40, 1}, {"bksv", 0x00, 5}, {"r0'", 0x08, 2}, \
  153. {"bstatus", 0x41, 2}, {"??", 0x0, 0}, {"ksv-fifo", 0x43, 0}, \
  154. {"v_h0", 0x20, 4}, {"v_h1", 0x24, 4}, {"v_h2", 0x28, 4}, \
  155. {"v_h3", 0x2c, 4}, {"v_h4", 0x30, 4}, {"an", 0x18, 8}, \
  156. {"aksv", 0x10, 5}, {"ainfo", 0x00, 0},}
  157. #define HDCP_DP_SINK_ADDR_MAP \
  158. {{"bcaps", 0x68028, 1}, {"bksv", 0x68000, 5}, {"r0'", 0x68005, 2}, \
  159. {"binfo", 0x6802A, 2}, {"cp_irq_status", 0x68029, 1}, \
  160. {"ksv-fifo", 0x6802C, 0}, {"v_h0", 0x68014, 4}, {"v_h1", 0x68018, 4}, \
  161. {"v_h2", 0x6801C, 4}, {"v_h3", 0x68020, 4}, {"v_h4", 0x68024, 4}, \
  162. {"an", 0x6800C, 8}, {"aksv", 0x68007, 5}, {"ainfo", 0x6803B, 1} }
  163. #define HDCP_HDMI_INT_SET \
  164. {0}
  165. #define HDCP_DP_INT_SET \
  166. {DP_INTR_STATUS2, \
  167. BIT(17), BIT(20), BIT(24), BIT(27), 0, 0, \
  168. BIT(16), BIT(19), BIT(21), BIT(23), BIT(26), 0, 0, \
  169. BIT(15), BIT(18), BIT(22), BIT(25), 0, 0}
  170. struct sde_hdcp_1x {
  171. u8 bcaps;
  172. u32 tp_msgid;
  173. u32 an_0, an_1, aksv_0, aksv_1;
  174. u32 aksv_msb, aksv_lsb;
  175. bool sink_r0_ready;
  176. bool reauth;
  177. bool ksv_ready;
  178. bool force_encryption;
  179. atomic_t abort;
  180. enum sde_hdcp_state hdcp_state;
  181. struct HDCP_V2V1_MSG_TOPOLOGY current_tp;
  182. struct delayed_work hdcp_auth_work;
  183. struct completion r0_checked;
  184. struct completion sink_r0_available;
  185. struct sde_hdcp_init_data init_data;
  186. struct sde_hdcp_ops *ops;
  187. struct sde_hdcp_reg_set reg_set;
  188. struct sde_hdcp_int_set int_set;
  189. struct sde_hdcp_sink_addr_map sink_addr;
  190. struct workqueue_struct *workq;
  191. void *hdcp1_handle;
  192. };
  193. static int sde_hdcp_1x_count_one(u8 *array, u8 len)
  194. {
  195. int i, j, count = 0;
  196. for (i = 0; i < len; i++)
  197. for (j = 0; j < 8; j++)
  198. count += (((array[i] >> j) & 0x1) ? 1 : 0);
  199. return count;
  200. }
  201. static int sde_hdcp_1x_enable_hdcp_engine(void *input)
  202. {
  203. int rc = 0;
  204. struct dss_io_data *dp_ahb;
  205. struct dss_io_data *dp_aux;
  206. struct dss_io_data *dp_link;
  207. struct sde_hdcp_1x *hdcp = input;
  208. struct sde_hdcp_reg_set *reg_set;
  209. if (!hdcp || !hdcp->init_data.dp_ahb ||
  210. !hdcp->init_data.dp_aux ||
  211. !hdcp->init_data.dp_link) {
  212. pr_err("invalid input\n");
  213. rc = -EINVAL;
  214. goto end;
  215. }
  216. if (!sde_hdcp_1x_state(HDCP_STATE_INACTIVE) &&
  217. !sde_hdcp_1x_state(HDCP_STATE_AUTH_FAIL)) {
  218. pr_err("%s: invalid state. returning\n",
  219. SDE_HDCP_STATE_NAME);
  220. rc = -EINVAL;
  221. goto end;
  222. }
  223. dp_ahb = hdcp->init_data.dp_ahb;
  224. dp_aux = hdcp->init_data.dp_aux;
  225. dp_link = hdcp->init_data.dp_link;
  226. reg_set = &hdcp->reg_set;
  227. DSS_REG_W(dp_aux, reg_set->aksv_lsb, hdcp->aksv_lsb);
  228. DSS_REG_W(dp_aux, reg_set->aksv_msb, hdcp->aksv_msb);
  229. /* Setup seed values for random number An */
  230. DSS_REG_W(dp_link, reg_set->entropy_ctrl0, 0xB1FFB0FF);
  231. DSS_REG_W(dp_link, reg_set->entropy_ctrl1, 0xF00DFACE);
  232. /* make sure hw is programmed */
  233. wmb();
  234. /* enable hdcp engine */
  235. DSS_REG_W(dp_ahb, reg_set->ctrl, 0x1);
  236. hdcp->hdcp_state = HDCP_STATE_AUTHENTICATING;
  237. end:
  238. return rc;
  239. }
  240. static int sde_hdcp_1x_read(struct sde_hdcp_1x *hdcp,
  241. struct sde_hdcp_sink_addr *sink,
  242. u8 *buf, bool realign)
  243. {
  244. int const max_size = 15;
  245. int rc = 0, read_size = 0, bytes_read = 0;
  246. if (hdcp->init_data.client_id == HDCP_CLIENT_DP) {
  247. int size = sink->len, offset = sink->addr;
  248. do {
  249. read_size = min(size, max_size);
  250. bytes_read = drm_dp_dpcd_read(hdcp->init_data.drm_aux,
  251. offset, buf, read_size);
  252. if (bytes_read != read_size) {
  253. pr_err("fail: offset(0x%x), size(0x%x), rc(0x%x)\n",
  254. offset, read_size, bytes_read);
  255. rc = -EIO;
  256. break;
  257. }
  258. buf += read_size;
  259. size -= read_size;
  260. if (!realign)
  261. offset += read_size;
  262. } while (size > 0);
  263. }
  264. return rc;
  265. }
  266. static int sde_hdcp_1x_write(struct sde_hdcp_1x *hdcp,
  267. struct sde_hdcp_sink_addr *sink, u8 *buf)
  268. {
  269. int const max_size = 16;
  270. int rc = 0, write_size = 0, bytes_written = 0;
  271. if (hdcp->init_data.client_id == HDCP_CLIENT_DP) {
  272. int size = sink->len, offset = sink->addr;
  273. do {
  274. write_size = min(size, max_size);
  275. bytes_written =
  276. drm_dp_dpcd_write(hdcp->init_data.drm_aux,
  277. offset, buf, write_size);
  278. if (bytes_written != write_size) {
  279. pr_err("fail: offset(0x%x), size(0x%x), rc(0x%x)\n",
  280. offset, write_size, bytes_written);
  281. rc = -EIO;
  282. break;
  283. }
  284. buf += write_size;
  285. offset += write_size;
  286. size -= write_size;
  287. } while (size > 0);
  288. }
  289. return rc;
  290. }
  291. static void sde_hdcp_1x_enable_interrupts(struct sde_hdcp_1x *hdcp)
  292. {
  293. u32 intr_reg;
  294. struct dss_io_data *io;
  295. struct sde_hdcp_int_set *isr;
  296. io = hdcp->init_data.dp_ahb;
  297. isr = &hdcp->int_set;
  298. intr_reg = DSS_REG_R(io, isr->int_reg);
  299. intr_reg |= HDCP_INT_CLR | HDCP_INT_EN;
  300. DSS_REG_W(io, isr->int_reg, intr_reg);
  301. }
  302. static int sde_hdcp_1x_read_bcaps(struct sde_hdcp_1x *hdcp)
  303. {
  304. int rc;
  305. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  306. struct dss_io_data *hdcp_io = hdcp->init_data.hdcp_io;
  307. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  308. pr_err("invalid state\n");
  309. return -EINVAL;
  310. }
  311. rc = sde_hdcp_1x_read(hdcp, &hdcp->sink_addr.bcaps,
  312. &hdcp->bcaps, false);
  313. if (rc) {
  314. pr_err("error reading bcaps\n");
  315. goto error;
  316. }
  317. pr_debug("bcaps read: 0x%x\n", hdcp->bcaps);
  318. hdcp->current_tp.ds_type = hdcp->bcaps & reg_set->repeater ?
  319. DS_REPEATER : DS_RECEIVER;
  320. pr_debug("ds: %s\n", hdcp->current_tp.ds_type == DS_REPEATER ?
  321. "repeater" : "receiver");
  322. /* Write BCAPS to the hardware */
  323. DSS_REG_W(hdcp_io, reg_set->sec_data12, hdcp->bcaps);
  324. error:
  325. return rc;
  326. }
  327. static int sde_hdcp_1x_wait_for_hw_ready(struct sde_hdcp_1x *hdcp)
  328. {
  329. int rc;
  330. u32 link0_status;
  331. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  332. struct dss_io_data *dp_ahb = hdcp->init_data.dp_ahb;
  333. struct dss_io_data *dp_aux = hdcp->init_data.dp_aux;
  334. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  335. pr_err("invalid state\n");
  336. return -EINVAL;
  337. }
  338. /* Wait for HDCP keys to be checked and validated */
  339. rc = readl_poll_timeout(dp_ahb->base + reg_set->status, link0_status,
  340. ((link0_status >> reg_set->keys_offset) & 0x7)
  341. == HDCP_KEYS_STATE_VALID ||
  342. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING),
  343. HDCP_POLL_SLEEP_US, HDCP_POLL_TIMEOUT_US);
  344. if (rc) {
  345. pr_err("key not ready\n");
  346. goto error;
  347. }
  348. /*
  349. * 1.1_Features turned off by default.
  350. * No need to write AInfo since 1.1_Features is disabled.
  351. */
  352. DSS_REG_W(dp_aux, reg_set->data4, 0);
  353. /* Wait for An0 and An1 bit to be ready */
  354. rc = readl_poll_timeout(dp_ahb->base + reg_set->status, link0_status,
  355. (link0_status & (BIT(8) | BIT(9))) ||
  356. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING),
  357. HDCP_POLL_SLEEP_US, HDCP_POLL_TIMEOUT_US);
  358. if (rc) {
  359. pr_err("An not ready\n");
  360. goto error;
  361. }
  362. /* As per hardware recommendations, wait before reading An */
  363. msleep(20);
  364. error:
  365. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING))
  366. rc = -EINVAL;
  367. return rc;
  368. }
  369. static int sde_hdcp_1x_send_an_aksv_to_sink(struct sde_hdcp_1x *hdcp)
  370. {
  371. int rc;
  372. u8 an[8], aksv[5];
  373. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  374. pr_err("invalid state\n");
  375. return -EINVAL;
  376. }
  377. an[0] = hdcp->an_0 & 0xFF;
  378. an[1] = (hdcp->an_0 >> 8) & 0xFF;
  379. an[2] = (hdcp->an_0 >> 16) & 0xFF;
  380. an[3] = (hdcp->an_0 >> 24) & 0xFF;
  381. an[4] = hdcp->an_1 & 0xFF;
  382. an[5] = (hdcp->an_1 >> 8) & 0xFF;
  383. an[6] = (hdcp->an_1 >> 16) & 0xFF;
  384. an[7] = (hdcp->an_1 >> 24) & 0xFF;
  385. pr_debug("an read: 0x%2x%2x%2x%2x%2x%2x%2x%2x\n",
  386. an[7], an[6], an[5], an[4], an[3], an[2], an[1], an[0]);
  387. rc = sde_hdcp_1x_write(hdcp, &hdcp->sink_addr.an, an);
  388. if (rc) {
  389. pr_err("error writing an to sink\n");
  390. goto error;
  391. }
  392. /* Copy An and AKSV to byte arrays for transmission */
  393. aksv[0] = hdcp->aksv_0 & 0xFF;
  394. aksv[1] = (hdcp->aksv_0 >> 8) & 0xFF;
  395. aksv[2] = (hdcp->aksv_0 >> 16) & 0xFF;
  396. aksv[3] = (hdcp->aksv_0 >> 24) & 0xFF;
  397. aksv[4] = hdcp->aksv_1 & 0xFF;
  398. pr_debug("aksv read: 0x%2x%2x%2x%2x%2x\n",
  399. aksv[4], aksv[3], aksv[2], aksv[1], aksv[0]);
  400. rc = sde_hdcp_1x_write(hdcp, &hdcp->sink_addr.aksv, aksv);
  401. if (rc) {
  402. pr_err("error writing aksv to sink\n");
  403. goto error;
  404. }
  405. error:
  406. return rc;
  407. }
  408. static int sde_hdcp_1x_read_an_aksv_from_hw(struct sde_hdcp_1x *hdcp)
  409. {
  410. struct dss_io_data *dp_ahb = hdcp->init_data.dp_ahb;
  411. struct dss_io_data *dp_aux = hdcp->init_data.dp_aux;
  412. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  413. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  414. pr_err("invalid state\n");
  415. return -EINVAL;
  416. }
  417. hdcp->an_0 = DSS_REG_R(dp_ahb, reg_set->data5);
  418. if (hdcp->init_data.client_id == HDCP_CLIENT_DP) {
  419. udelay(1);
  420. hdcp->an_0 = DSS_REG_R(dp_ahb, reg_set->data5);
  421. }
  422. hdcp->an_1 = DSS_REG_R(dp_ahb, reg_set->data6);
  423. if (hdcp->init_data.client_id == HDCP_CLIENT_DP) {
  424. udelay(1);
  425. hdcp->an_1 = DSS_REG_R(dp_ahb, reg_set->data6);
  426. }
  427. /* Read AKSV */
  428. hdcp->aksv_0 = DSS_REG_R(dp_aux, reg_set->data3);
  429. hdcp->aksv_1 = DSS_REG_R(dp_aux, reg_set->data4);
  430. return 0;
  431. }
  432. static int sde_hdcp_1x_get_bksv_from_sink(struct sde_hdcp_1x *hdcp)
  433. {
  434. int rc;
  435. u8 *bksv = hdcp->current_tp.bksv;
  436. u32 link0_bksv_0, link0_bksv_1;
  437. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  438. struct dss_io_data *hdcp_io = hdcp->init_data.hdcp_io;
  439. rc = sde_hdcp_1x_read(hdcp, &hdcp->sink_addr.bksv, bksv, false);
  440. if (rc) {
  441. pr_err("error reading bksv from sink\n");
  442. goto error;
  443. }
  444. pr_debug("bksv read: 0x%2x%2x%2x%2x%2x\n",
  445. bksv[4], bksv[3], bksv[2], bksv[1], bksv[0]);
  446. /* check there are 20 ones in BKSV */
  447. if (sde_hdcp_1x_count_one(bksv, 5) != 20) {
  448. pr_err("%s: BKSV doesn't have 20 1's and 20 0's\n",
  449. SDE_HDCP_STATE_NAME);
  450. rc = -EINVAL;
  451. goto error;
  452. }
  453. link0_bksv_0 = bksv[3];
  454. link0_bksv_0 = (link0_bksv_0 << 8) | bksv[2];
  455. link0_bksv_0 = (link0_bksv_0 << 8) | bksv[1];
  456. link0_bksv_0 = (link0_bksv_0 << 8) | bksv[0];
  457. link0_bksv_1 = bksv[4];
  458. DSS_REG_W(hdcp_io, reg_set->sec_data0, link0_bksv_0);
  459. DSS_REG_W(hdcp_io, reg_set->sec_data1, link0_bksv_1);
  460. error:
  461. return rc;
  462. }
  463. static void sde_hdcp_1x_enable_sink_irq_hpd(struct sde_hdcp_1x *hdcp)
  464. {
  465. u8 const required_major = 1, required_minor = 2;
  466. u8 sink_major = 0, sink_minor = 0;
  467. u8 enable_hpd_irq = 0x1;
  468. int rc;
  469. unsigned char revision = *hdcp->init_data.revision;
  470. sink_major = (revision >> 4) & 0x0f;
  471. sink_minor = revision & 0x0f;
  472. pr_debug("revision: %d.%d\n", sink_major, sink_minor);
  473. if ((sink_minor < required_minor) || (sink_major < required_major) ||
  474. (hdcp->current_tp.ds_type != DS_REPEATER)) {
  475. pr_debug("sink irq hpd not enabled\n");
  476. return;
  477. }
  478. rc = sde_hdcp_1x_write(hdcp, &hdcp->sink_addr.ainfo, &enable_hpd_irq);
  479. if (rc)
  480. pr_debug("error writing ainfo to sink\n");
  481. }
  482. static int sde_hdcp_1x_verify_r0(struct sde_hdcp_1x *hdcp)
  483. {
  484. int rc, r0_retry = 3;
  485. u8 buf[2];
  486. u32 link0_status, timeout_count;
  487. u32 const r0_read_delay_us = 1;
  488. u32 const r0_read_timeout_us = r0_read_delay_us * 10;
  489. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  490. struct dss_io_data *io = hdcp->init_data.dp_ahb;
  491. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  492. pr_err("invalid state\n");
  493. return -EINVAL;
  494. }
  495. /* Wait for HDCP R0 computation to be completed */
  496. rc = readl_poll_timeout(io->base + reg_set->status, link0_status,
  497. (link0_status & BIT(reg_set->r0_offset)) ||
  498. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING),
  499. HDCP_POLL_SLEEP_US, HDCP_POLL_TIMEOUT_US);
  500. if (rc) {
  501. pr_err("R0 not ready\n");
  502. goto error;
  503. }
  504. /*
  505. * HDCP Compliace Test case 1A-01:
  506. * Wait here at least 100ms before reading R0'
  507. */
  508. if (hdcp->init_data.client_id == HDCP_CLIENT_HDMI) {
  509. msleep(100);
  510. } else {
  511. if (!hdcp->sink_r0_ready) {
  512. reinit_completion(&hdcp->sink_r0_available);
  513. timeout_count = wait_for_completion_timeout(
  514. &hdcp->sink_r0_available, HZ / 2);
  515. if (hdcp->reauth) {
  516. pr_err("sink R0 not ready\n");
  517. rc = -EINVAL;
  518. goto error;
  519. }
  520. }
  521. }
  522. do {
  523. memset(buf, 0, sizeof(buf));
  524. rc = sde_hdcp_1x_read(hdcp, &hdcp->sink_addr.r0,
  525. buf, false);
  526. if (rc) {
  527. pr_err("error reading R0' from sink\n");
  528. goto error;
  529. }
  530. pr_debug("sink R0'read: %2x%2x\n", buf[1], buf[0]);
  531. DSS_REG_W(io, reg_set->data2_0, (((u32)buf[1]) << 8) | buf[0]);
  532. rc = readl_poll_timeout(io->base + reg_set->status,
  533. link0_status, (link0_status & BIT(12)) ||
  534. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING),
  535. r0_read_delay_us, r0_read_timeout_us);
  536. } while (rc && --r0_retry);
  537. error:
  538. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING))
  539. rc = -EINVAL;
  540. return rc;
  541. }
  542. static int sde_hdcp_1x_authentication_part1(struct sde_hdcp_1x *hdcp)
  543. {
  544. int rc;
  545. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  546. pr_err("invalid state\n");
  547. return -EINVAL;
  548. }
  549. sde_hdcp_1x_enable_interrupts(hdcp);
  550. rc = sde_hdcp_1x_read_bcaps(hdcp);
  551. if (rc)
  552. goto error;
  553. rc = sde_hdcp_1x_wait_for_hw_ready(hdcp);
  554. if (rc)
  555. goto error;
  556. rc = sde_hdcp_1x_read_an_aksv_from_hw(hdcp);
  557. if (rc)
  558. goto error;
  559. rc = sde_hdcp_1x_get_bksv_from_sink(hdcp);
  560. if (rc)
  561. goto error;
  562. rc = sde_hdcp_1x_send_an_aksv_to_sink(hdcp);
  563. if (rc)
  564. goto error;
  565. sde_hdcp_1x_enable_sink_irq_hpd(hdcp);
  566. rc = sde_hdcp_1x_verify_r0(hdcp);
  567. if (rc)
  568. goto error;
  569. pr_info("SUCCESSFUL\n");
  570. return 0;
  571. error:
  572. pr_err("%s: FAILED\n", SDE_HDCP_STATE_NAME);
  573. return rc;
  574. }
  575. static int sde_hdcp_1x_transfer_v_h(struct sde_hdcp_1x *hdcp)
  576. {
  577. int rc = 0;
  578. struct dss_io_data *io = hdcp->init_data.hdcp_io;
  579. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  580. struct sde_hdcp_1x_reg_data reg_data[] = {
  581. {reg_set->sec_data7, &hdcp->sink_addr.v_h0},
  582. {reg_set->sec_data8, &hdcp->sink_addr.v_h1},
  583. {reg_set->sec_data9, &hdcp->sink_addr.v_h2},
  584. {reg_set->sec_data10, &hdcp->sink_addr.v_h3},
  585. {reg_set->sec_data11, &hdcp->sink_addr.v_h4},
  586. };
  587. struct sde_hdcp_sink_addr sink = {"V", reg_data->sink->addr};
  588. u32 size = ARRAY_SIZE(reg_data);
  589. u8 buf[0xFF] = {0};
  590. u32 i = 0, len = 0;
  591. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  592. pr_err("invalid state\n");
  593. return -EINVAL;
  594. }
  595. for (i = 0; i < size; i++) {
  596. struct sde_hdcp_1x_reg_data *rd = reg_data + i;
  597. len += rd->sink->len;
  598. }
  599. sink.len = len;
  600. rc = sde_hdcp_1x_read(hdcp, &sink, buf, false);
  601. if (rc) {
  602. pr_err("error reading %s\n", sink.name);
  603. goto end;
  604. }
  605. for (i = 0; i < size; i++) {
  606. struct sde_hdcp_1x_reg_data *rd = reg_data + i;
  607. u32 reg_data;
  608. memcpy(&reg_data, buf + (sizeof(u32) * i), sizeof(u32));
  609. DSS_REG_W(io, rd->reg_id, reg_data);
  610. }
  611. end:
  612. return rc;
  613. }
  614. static int sde_hdcp_1x_validate_downstream(struct sde_hdcp_1x *hdcp)
  615. {
  616. int rc;
  617. u8 buf[2] = {0, 0};
  618. u8 device_count, depth;
  619. u8 max_cascade_exceeded, max_devs_exceeded;
  620. u16 bstatus;
  621. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  622. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  623. pr_err("invalid state\n");
  624. return -EINVAL;
  625. }
  626. rc = sde_hdcp_1x_read(hdcp, &hdcp->sink_addr.bstatus,
  627. buf, false);
  628. if (rc) {
  629. pr_err("error reading bstatus\n");
  630. goto end;
  631. }
  632. bstatus = buf[1];
  633. bstatus = (bstatus << 8) | buf[0];
  634. device_count = bstatus & 0x7F;
  635. pr_debug("device count %d\n", device_count);
  636. /* Cascaded repeater depth */
  637. depth = (bstatus >> 8) & 0x7;
  638. pr_debug("depth %d\n", depth);
  639. /*
  640. * HDCP Compliance 1B-05:
  641. * Check if no. of devices connected to repeater
  642. * exceed max_devices_connected from bit 7 of Bstatus.
  643. */
  644. max_devs_exceeded = (bstatus & BIT(7)) >> 7;
  645. if (max_devs_exceeded == 0x01) {
  646. pr_err("no. of devs connected exceed max allowed\n");
  647. rc = -EINVAL;
  648. goto end;
  649. }
  650. /*
  651. * HDCP Compliance 1B-06:
  652. * Check if no. of cascade connected to repeater
  653. * exceed max_cascade_connected from bit 11 of Bstatus.
  654. */
  655. max_cascade_exceeded = (bstatus & BIT(11)) >> 11;
  656. if (max_cascade_exceeded == 0x01) {
  657. pr_err("no. of cascade connections exceed max allowed\n");
  658. rc = -EINVAL;
  659. goto end;
  660. }
  661. /* Update topology information */
  662. hdcp->current_tp.dev_count = device_count;
  663. hdcp->current_tp.max_cascade_exceeded = max_cascade_exceeded;
  664. hdcp->current_tp.max_dev_exceeded = max_devs_exceeded;
  665. hdcp->current_tp.depth = depth;
  666. DSS_REG_W(hdcp->init_data.hdcp_io,
  667. reg_set->sec_data12, hdcp->bcaps | (bstatus << 8));
  668. end:
  669. return rc;
  670. }
  671. static int sde_hdcp_1x_read_ksv_fifo(struct sde_hdcp_1x *hdcp)
  672. {
  673. u32 ksv_read_retry = 20, ksv_bytes, rc = 0;
  674. u8 *ksv_fifo = hdcp->current_tp.ksv_list;
  675. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  676. pr_err("invalid state\n");
  677. return -EINVAL;
  678. }
  679. memset(ksv_fifo, 0, sizeof(hdcp->current_tp.ksv_list));
  680. /* each KSV is 5 bytes long */
  681. ksv_bytes = 5 * hdcp->current_tp.dev_count;
  682. hdcp->sink_addr.ksv_fifo.len = ksv_bytes;
  683. while (ksv_bytes && --ksv_read_retry) {
  684. rc = sde_hdcp_1x_read(hdcp, &hdcp->sink_addr.ksv_fifo,
  685. ksv_fifo, true);
  686. if (rc)
  687. pr_err("could not read ksv fifo (%d)\n",
  688. ksv_read_retry);
  689. else
  690. break;
  691. }
  692. if (rc)
  693. pr_err("error reading ksv_fifo\n");
  694. return rc;
  695. }
  696. static int sde_hdcp_1x_write_ksv_fifo(struct sde_hdcp_1x *hdcp)
  697. {
  698. int i, rc = 0;
  699. u8 *ksv_fifo = hdcp->current_tp.ksv_list;
  700. u32 ksv_bytes = hdcp->sink_addr.ksv_fifo.len;
  701. struct dss_io_data *io = hdcp->init_data.dp_ahb;
  702. struct dss_io_data *sec_io = hdcp->init_data.hdcp_io;
  703. struct sde_hdcp_reg_set *reg_set = &hdcp->reg_set;
  704. u32 sha_status = 0, status;
  705. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  706. pr_err("invalid state\n");
  707. return -EINVAL;
  708. }
  709. /* reset SHA Controller */
  710. DSS_REG_W(sec_io, reg_set->sec_sha_ctrl, 0x1);
  711. DSS_REG_W(sec_io, reg_set->sec_sha_ctrl, 0x0);
  712. for (i = 0; i < ksv_bytes - 1; i++) {
  713. /* Write KSV byte and do not set DONE bit[0] */
  714. DSS_REG_W_ND(sec_io, reg_set->sec_sha_data, ksv_fifo[i] << 16);
  715. /*
  716. * Once 64 bytes have been written, we need to poll for
  717. * HDCP_SHA_BLOCK_DONE before writing any further
  718. */
  719. if (i && !((i + 1) % 64)) {
  720. rc = readl_poll_timeout(io->base + reg_set->sha_status,
  721. sha_status, (sha_status & BIT(0)) ||
  722. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING),
  723. HDCP_POLL_SLEEP_US, HDCP_POLL_TIMEOUT_US);
  724. if (rc) {
  725. pr_err("block not done\n");
  726. goto error;
  727. }
  728. }
  729. }
  730. /* Write l to DONE bit[0] */
  731. DSS_REG_W_ND(sec_io, reg_set->sec_sha_data,
  732. (ksv_fifo[ksv_bytes - 1] << 16) | 0x1);
  733. /* Now wait for HDCP_SHA_COMP_DONE */
  734. rc = readl_poll_timeout(io->base + reg_set->sha_status, sha_status,
  735. (sha_status & BIT(4)) ||
  736. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING),
  737. HDCP_POLL_SLEEP_US, HDCP_POLL_TIMEOUT_US);
  738. if (rc) {
  739. pr_err("V computation not done\n");
  740. goto error;
  741. }
  742. /* Wait for V_MATCHES */
  743. rc = readl_poll_timeout(io->base + reg_set->status, status,
  744. (status & BIT(reg_set->v_offset)) ||
  745. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING),
  746. HDCP_POLL_SLEEP_US, HDCP_POLL_TIMEOUT_US);
  747. if (rc) {
  748. pr_err("V mismatch\n");
  749. rc = -EINVAL;
  750. }
  751. error:
  752. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING))
  753. rc = -EINVAL;
  754. return rc;
  755. }
  756. static int sde_hdcp_1x_wait_for_ksv_ready(struct sde_hdcp_1x *hdcp)
  757. {
  758. int rc, timeout;
  759. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  760. pr_err("invalid state\n");
  761. return -EINVAL;
  762. }
  763. /*
  764. * Wait until READY bit is set in BCAPS, as per HDCP specifications
  765. * maximum permitted time to check for READY bit is five seconds.
  766. */
  767. rc = sde_hdcp_1x_read(hdcp, &hdcp->sink_addr.bcaps,
  768. &hdcp->bcaps, false);
  769. if (rc) {
  770. pr_err("error reading bcaps\n");
  771. goto error;
  772. }
  773. if (hdcp->init_data.client_id == HDCP_CLIENT_HDMI) {
  774. timeout = 50;
  775. while (!(hdcp->bcaps & BIT(5)) && --timeout) {
  776. rc = sde_hdcp_1x_read(hdcp,
  777. &hdcp->sink_addr.bcaps,
  778. &hdcp->bcaps, false);
  779. if (rc ||
  780. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  781. pr_err("error reading bcaps\n");
  782. goto error;
  783. }
  784. msleep(100);
  785. }
  786. } else {
  787. u8 cp_buf = 0;
  788. struct sde_hdcp_sink_addr *sink =
  789. &hdcp->sink_addr.cp_irq_status;
  790. timeout = jiffies_to_msecs(jiffies);
  791. while (1) {
  792. rc = sde_hdcp_1x_read(hdcp, sink, &cp_buf, false);
  793. if (rc)
  794. goto error;
  795. if (cp_buf & BIT(0))
  796. break;
  797. /* max timeout of 5 sec as per hdcp 1.x spec */
  798. if (abs(timeout - jiffies_to_msecs(jiffies)) > 5000) {
  799. timeout = 0;
  800. break;
  801. }
  802. if (hdcp->ksv_ready || hdcp->reauth ||
  803. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING))
  804. break;
  805. /* re-read after a minimum delay */
  806. msleep(20);
  807. }
  808. }
  809. if (!timeout || hdcp->reauth ||
  810. !sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  811. pr_err("DS KSV not ready\n");
  812. rc = -EINVAL;
  813. } else {
  814. hdcp->ksv_ready = true;
  815. }
  816. error:
  817. return rc;
  818. }
  819. static int sde_hdcp_1x_authentication_part2(struct sde_hdcp_1x *hdcp)
  820. {
  821. int rc;
  822. int v_retry = 3;
  823. rc = sde_hdcp_1x_validate_downstream(hdcp);
  824. if (rc)
  825. goto error;
  826. rc = sde_hdcp_1x_read_ksv_fifo(hdcp);
  827. if (rc)
  828. goto error;
  829. do {
  830. rc = sde_hdcp_1x_transfer_v_h(hdcp);
  831. if (rc)
  832. goto error;
  833. /* do not proceed further if no device connected */
  834. if (!hdcp->current_tp.dev_count)
  835. goto error;
  836. rc = sde_hdcp_1x_write_ksv_fifo(hdcp);
  837. } while (--v_retry && rc);
  838. error:
  839. if (rc) {
  840. pr_err("%s: FAILED\n", SDE_HDCP_STATE_NAME);
  841. } else {
  842. hdcp->hdcp_state = HDCP_STATE_AUTHENTICATED;
  843. pr_info("SUCCESSFUL\n");
  844. }
  845. return rc;
  846. }
  847. static void sde_hdcp_1x_update_auth_status(struct sde_hdcp_1x *hdcp)
  848. {
  849. if (IS_ENABLED(CONFIG_HDCP_QSEECOM) &&
  850. sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATED)) {
  851. msm_hdcp_cache_repeater_topology(hdcp->init_data.msm_hdcp_dev,
  852. &hdcp->current_tp);
  853. msm_hdcp_notify_topology(hdcp->init_data.msm_hdcp_dev);
  854. }
  855. if (hdcp->init_data.notify_status &&
  856. !sde_hdcp_1x_state(HDCP_STATE_INACTIVE)) {
  857. hdcp->init_data.notify_status(
  858. hdcp->init_data.cb_data,
  859. hdcp->hdcp_state);
  860. }
  861. }
  862. static void sde_hdcp_1x_auth_work(struct work_struct *work)
  863. {
  864. int rc = 0;
  865. struct delayed_work *dw = to_delayed_work(work);
  866. struct sde_hdcp_1x *hdcp = container_of(dw,
  867. struct sde_hdcp_1x, hdcp_auth_work);
  868. struct dss_io_data *io;
  869. if (!hdcp) {
  870. pr_err("invalid input\n");
  871. return;
  872. }
  873. if (!sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  874. pr_err("invalid state\n");
  875. return;
  876. }
  877. if (atomic_read(&hdcp->abort))
  878. goto end;
  879. hdcp->sink_r0_ready = false;
  880. hdcp->reauth = false;
  881. hdcp->ksv_ready = false;
  882. io = hdcp->init_data.core_io;
  883. /* Enabling Software DDC for HDMI and REF timer for DP */
  884. if (hdcp->init_data.client_id == HDCP_CLIENT_DP) {
  885. io = hdcp->init_data.dp_aux;
  886. DSS_REG_W(io, DP_DP_HPD_REFTIMER, 0x10013);
  887. }
  888. /*
  889. * Program h/w to enable encryption as soon as authentication is
  890. * successful. This is applicable for HDMI sinks and HDCP 1.x compliance
  891. * test cases.
  892. */
  893. if (hdcp->init_data.client_id == HDCP_CLIENT_HDMI ||
  894. hdcp->force_encryption)
  895. hdcp1_set_enc(hdcp->hdcp1_handle, true);
  896. rc = sde_hdcp_1x_authentication_part1(hdcp);
  897. if (rc)
  898. goto end;
  899. if (hdcp->current_tp.ds_type == DS_REPEATER) {
  900. rc = sde_hdcp_1x_wait_for_ksv_ready(hdcp);
  901. if (rc)
  902. goto end;
  903. } else {
  904. hdcp->hdcp_state = HDCP_STATE_AUTHENTICATED;
  905. goto end;
  906. }
  907. hdcp->ksv_ready = false;
  908. rc = sde_hdcp_1x_authentication_part2(hdcp);
  909. if (rc)
  910. goto end;
  911. /*
  912. * Disabling software DDC before going into part3 to make sure
  913. * there is no Arbitration between software and hardware for DDC
  914. */
  915. end:
  916. if (rc && !sde_hdcp_1x_state(HDCP_STATE_INACTIVE))
  917. hdcp->hdcp_state = HDCP_STATE_AUTH_FAIL;
  918. sde_hdcp_1x_update_auth_status(hdcp);
  919. }
  920. static int sde_hdcp_1x_authenticate(void *input)
  921. {
  922. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  923. int rc = 0;
  924. if (!hdcp) {
  925. pr_err("invalid input\n");
  926. rc = -EINVAL;
  927. goto error;
  928. }
  929. flush_delayed_work(&hdcp->hdcp_auth_work);
  930. if (!sde_hdcp_1x_state(HDCP_STATE_INACTIVE)) {
  931. pr_err("invalid state\n");
  932. rc = -EINVAL;
  933. goto error;
  934. }
  935. rc = hdcp1_start(hdcp->hdcp1_handle, &hdcp->aksv_msb, &hdcp->aksv_lsb);
  936. if (rc) {
  937. pr_err("hdcp1_start failed (%d)\n", rc);
  938. goto error;
  939. }
  940. if (!sde_hdcp_1x_enable_hdcp_engine(input)) {
  941. queue_delayed_work(hdcp->workq,
  942. &hdcp->hdcp_auth_work, HZ/2);
  943. } else {
  944. hdcp->hdcp_state = HDCP_STATE_AUTH_FAIL;
  945. sde_hdcp_1x_update_auth_status(hdcp);
  946. }
  947. error:
  948. return rc;
  949. } /* hdcp_1x_authenticate */
  950. static int sde_hdcp_1x_reauthenticate(void *input)
  951. {
  952. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  953. struct dss_io_data *io;
  954. struct sde_hdcp_reg_set *reg_set;
  955. struct sde_hdcp_int_set *isr;
  956. u32 reg;
  957. if (!hdcp || !hdcp->init_data.dp_ahb) {
  958. pr_err("invalid input\n");
  959. return -EINVAL;
  960. }
  961. io = hdcp->init_data.dp_ahb;
  962. reg_set = &hdcp->reg_set;
  963. isr = &hdcp->int_set;
  964. if (!sde_hdcp_1x_state(HDCP_STATE_AUTH_FAIL)) {
  965. pr_err("invalid state\n");
  966. return -EINVAL;
  967. }
  968. /* Disable HDCP interrupts */
  969. DSS_REG_W(io, isr->int_reg, DSS_REG_R(io, isr->int_reg) & ~HDCP_INT_EN);
  970. reg = DSS_REG_R(io, reg_set->reset);
  971. DSS_REG_W(io, reg_set->reset, reg | reg_set->reset_bit);
  972. /* Disable encryption and disable the HDCP block */
  973. DSS_REG_W(io, reg_set->ctrl, 0);
  974. DSS_REG_W(io, reg_set->reset, reg & ~reg_set->reset_bit);
  975. hdcp->hdcp_state = HDCP_STATE_INACTIVE;
  976. return sde_hdcp_1x_authenticate(hdcp);
  977. } /* hdcp_1x_reauthenticate */
  978. static void sde_hdcp_1x_off(void *input)
  979. {
  980. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  981. struct dss_io_data *io;
  982. struct sde_hdcp_reg_set *reg_set;
  983. struct sde_hdcp_int_set *isr;
  984. int rc = 0;
  985. u32 reg;
  986. if (!hdcp || !hdcp->init_data.dp_ahb) {
  987. pr_err("invalid input\n");
  988. return;
  989. }
  990. io = hdcp->init_data.dp_ahb;
  991. reg_set = &hdcp->reg_set;
  992. isr = &hdcp->int_set;
  993. if (sde_hdcp_1x_state(HDCP_STATE_INACTIVE)) {
  994. pr_err("invalid state\n");
  995. return;
  996. }
  997. /*
  998. * Disable HDCP interrupts.
  999. * Also, need to set the state to inactive here so that any ongoing
  1000. * reauth works will know that the HDCP session has been turned off.
  1001. */
  1002. DSS_REG_W(io, isr->int_reg,
  1003. DSS_REG_R(io, isr->int_reg) & ~HDCP_INT_EN);
  1004. hdcp->hdcp_state = HDCP_STATE_INACTIVE;
  1005. /* complete any wait pending */
  1006. complete_all(&hdcp->sink_r0_available);
  1007. complete_all(&hdcp->r0_checked);
  1008. /*
  1009. * Cancel any pending auth/reauth attempts.
  1010. * If one is ongoing, this will wait for it to finish.
  1011. * No more reauthentiaction attempts will be scheduled since we
  1012. * set the currect state to inactive.
  1013. */
  1014. rc = cancel_delayed_work_sync(&hdcp->hdcp_auth_work);
  1015. if (rc)
  1016. pr_debug("%s: Deleted hdcp auth work\n",
  1017. SDE_HDCP_STATE_NAME);
  1018. if (hdcp->init_data.client_id == HDCP_CLIENT_HDMI ||
  1019. hdcp->force_encryption)
  1020. hdcp1_set_enc(hdcp->hdcp1_handle, false);
  1021. reg = DSS_REG_R(io, reg_set->reset);
  1022. DSS_REG_W(io, reg_set->reset, reg | reg_set->reset_bit);
  1023. /* Disable encryption and disable the HDCP block */
  1024. DSS_REG_W(io, reg_set->ctrl, 0);
  1025. DSS_REG_W(io, reg_set->reset, reg & ~reg_set->reset_bit);
  1026. hdcp->sink_r0_ready = false;
  1027. hdcp1_stop(hdcp->hdcp1_handle);
  1028. pr_debug("%s: HDCP: Off\n", SDE_HDCP_STATE_NAME);
  1029. } /* hdcp_1x_off */
  1030. static int sde_hdcp_1x_isr(void *input)
  1031. {
  1032. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  1033. int rc = 0;
  1034. struct dss_io_data *io;
  1035. u32 hdcp_int_val;
  1036. struct sde_hdcp_reg_set *reg_set;
  1037. struct sde_hdcp_int_set *isr;
  1038. if (!hdcp || !hdcp->init_data.dp_ahb) {
  1039. pr_err("invalid input\n");
  1040. rc = -EINVAL;
  1041. goto error;
  1042. }
  1043. io = hdcp->init_data.dp_ahb;
  1044. reg_set = &hdcp->reg_set;
  1045. isr = &hdcp->int_set;
  1046. hdcp_int_val = DSS_REG_R(io, isr->int_reg);
  1047. /* Ignore HDCP interrupts if HDCP is disabled */
  1048. if (sde_hdcp_1x_state(HDCP_STATE_INACTIVE)) {
  1049. DSS_REG_W(io, isr->int_reg, hdcp_int_val | HDCP_INT_CLR);
  1050. return 0;
  1051. }
  1052. if (hdcp_int_val & isr->auth_success_int) {
  1053. /* AUTH_SUCCESS_INT */
  1054. DSS_REG_W(io, isr->int_reg,
  1055. (hdcp_int_val | isr->auth_success_ack));
  1056. pr_debug("%s: AUTH SUCCESS\n", SDE_HDCP_STATE_NAME);
  1057. if (sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING))
  1058. complete_all(&hdcp->r0_checked);
  1059. }
  1060. if (hdcp_int_val & isr->auth_fail_int) {
  1061. /* AUTH_FAIL_INT */
  1062. u32 link_status = DSS_REG_R(io, reg_set->status);
  1063. DSS_REG_W(io, isr->int_reg,
  1064. (hdcp_int_val | isr->auth_fail_ack));
  1065. pr_debug("%s: AUTH FAIL, LINK0_STATUS=0x%08x\n",
  1066. SDE_HDCP_STATE_NAME, link_status);
  1067. if (sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATED)) {
  1068. hdcp->hdcp_state = HDCP_STATE_AUTH_FAIL;
  1069. sde_hdcp_1x_update_auth_status(hdcp);
  1070. } else if (sde_hdcp_1x_state(HDCP_STATE_AUTHENTICATING)) {
  1071. complete_all(&hdcp->r0_checked);
  1072. }
  1073. /* Clear AUTH_FAIL_INFO as well */
  1074. DSS_REG_W(io, isr->int_reg,
  1075. (hdcp_int_val | isr->auth_fail_info_ack));
  1076. }
  1077. if (hdcp_int_val & isr->tx_req_int) {
  1078. /* DDC_XFER_REQ_INT */
  1079. DSS_REG_W(io, isr->int_reg,
  1080. (hdcp_int_val | isr->tx_req_ack));
  1081. pr_debug("%s: DDC_XFER_REQ_INT received\n",
  1082. SDE_HDCP_STATE_NAME);
  1083. }
  1084. if (hdcp_int_val & isr->tx_req_done_int) {
  1085. /* DDC_XFER_DONE_INT */
  1086. DSS_REG_W(io, isr->int_reg,
  1087. (hdcp_int_val | isr->tx_req_done_ack));
  1088. pr_debug("%s: DDC_XFER_DONE received\n",
  1089. SDE_HDCP_STATE_NAME);
  1090. }
  1091. if (hdcp_int_val & isr->encryption_ready) {
  1092. /* Encryption enabled */
  1093. DSS_REG_W(io, isr->int_reg,
  1094. (hdcp_int_val | isr->encryption_ready_ack));
  1095. pr_debug("%s: encryption ready received\n",
  1096. SDE_HDCP_STATE_NAME);
  1097. }
  1098. if (hdcp_int_val & isr->encryption_not_ready) {
  1099. /* Encryption enabled */
  1100. DSS_REG_W(io, isr->int_reg,
  1101. (hdcp_int_val | isr->encryption_not_ready_ack));
  1102. pr_debug("%s: encryption not ready received\n",
  1103. SDE_HDCP_STATE_NAME);
  1104. }
  1105. error:
  1106. return rc;
  1107. }
  1108. static bool sde_hdcp_1x_feature_supported(void *input)
  1109. {
  1110. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  1111. bool feature_supported = false;
  1112. if (!hdcp) {
  1113. pr_err("invalid input\n");
  1114. return -EINVAL;
  1115. }
  1116. feature_supported = hdcp1_feature_supported(hdcp->hdcp1_handle);
  1117. pr_debug("feature_supported = %d\n", feature_supported);
  1118. return feature_supported;
  1119. }
  1120. static void sde_hdcp_1x_force_encryption(void *input, bool enable)
  1121. {
  1122. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  1123. if (!hdcp) {
  1124. pr_err("invalid input\n");
  1125. return;
  1126. }
  1127. hdcp->force_encryption = enable;
  1128. pr_info("force_encryption=%d\n", hdcp->force_encryption);
  1129. }
  1130. static bool sde_hdcp_1x_sink_support(void *input)
  1131. {
  1132. return true;
  1133. }
  1134. void sde_hdcp_1x_deinit(void *input)
  1135. {
  1136. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  1137. if (!hdcp) {
  1138. pr_err("invalid input\n");
  1139. return;
  1140. }
  1141. if (hdcp->workq)
  1142. destroy_workqueue(hdcp->workq);
  1143. hdcp1_deinit(hdcp->hdcp1_handle);
  1144. kfree(hdcp);
  1145. } /* hdcp_1x_deinit */
  1146. static void sde_hdcp_1x_update_client_reg_set(struct sde_hdcp_1x *hdcp)
  1147. {
  1148. if (hdcp->init_data.client_id == HDCP_CLIENT_DP) {
  1149. struct sde_hdcp_reg_set reg_set = HDCP_REG_SET_CLIENT_DP;
  1150. struct sde_hdcp_sink_addr_map sink_addr = HDCP_DP_SINK_ADDR_MAP;
  1151. struct sde_hdcp_int_set isr = HDCP_DP_INT_SET;
  1152. hdcp->reg_set = reg_set;
  1153. hdcp->sink_addr = sink_addr;
  1154. hdcp->int_set = isr;
  1155. }
  1156. }
  1157. static bool sde_hdcp_1x_is_cp_irq_raised(struct sde_hdcp_1x *hdcp)
  1158. {
  1159. int ret;
  1160. u8 buf = 0;
  1161. struct sde_hdcp_sink_addr sink = {"irq", 0x201, 1};
  1162. ret = sde_hdcp_1x_read(hdcp, &sink, &buf, false);
  1163. if (ret)
  1164. pr_err("error reading irq_vector\n");
  1165. return buf & BIT(2) ? true : false;
  1166. }
  1167. static void sde_hdcp_1x_clear_cp_irq(struct sde_hdcp_1x *hdcp)
  1168. {
  1169. int ret;
  1170. u8 buf = BIT(2);
  1171. struct sde_hdcp_sink_addr sink = {"irq", 0x201, 1};
  1172. ret = sde_hdcp_1x_write(hdcp, &sink, &buf);
  1173. if (ret)
  1174. pr_err("error clearing irq_vector\n");
  1175. }
  1176. static int sde_hdcp_1x_cp_irq(void *input)
  1177. {
  1178. struct sde_hdcp_1x *hdcp = (struct sde_hdcp_1x *)input;
  1179. u8 buf = 0;
  1180. int ret;
  1181. if (!hdcp) {
  1182. pr_err("invalid input\n");
  1183. goto irq_not_handled;
  1184. }
  1185. if (!sde_hdcp_1x_is_cp_irq_raised(hdcp)) {
  1186. pr_debug("cp_irq not raised\n");
  1187. goto irq_not_handled;
  1188. }
  1189. ret = sde_hdcp_1x_read(hdcp, &hdcp->sink_addr.cp_irq_status,
  1190. &buf, false);
  1191. if (ret) {
  1192. pr_err("error reading cp_irq_status\n");
  1193. goto irq_not_handled;
  1194. }
  1195. if ((buf & BIT(2)) || (buf & BIT(3))) {
  1196. pr_err("%s\n",
  1197. buf & BIT(2) ? "LINK_INTEGRITY_FAILURE" :
  1198. "REAUTHENTICATION_REQUEST");
  1199. hdcp->reauth = true;
  1200. if (!sde_hdcp_1x_state(HDCP_STATE_INACTIVE))
  1201. hdcp->hdcp_state = HDCP_STATE_AUTH_FAIL;
  1202. complete_all(&hdcp->sink_r0_available);
  1203. sde_hdcp_1x_update_auth_status(hdcp);
  1204. } else if (buf & BIT(1)) {
  1205. pr_debug("R0' AVAILABLE\n");
  1206. hdcp->sink_r0_ready = true;
  1207. complete_all(&hdcp->sink_r0_available);
  1208. } else if ((buf & BIT(0))) {
  1209. pr_debug("KSVs READY\n");
  1210. hdcp->ksv_ready = true;
  1211. } else {
  1212. pr_debug("spurious interrupt\n");
  1213. }
  1214. sde_hdcp_1x_clear_cp_irq(hdcp);
  1215. return 0;
  1216. irq_not_handled:
  1217. return -EINVAL;
  1218. }
  1219. static void sde_hdcp_1x_abort(void *data, bool abort)
  1220. {
  1221. struct sde_hdcp_1x *hdcp = data;
  1222. atomic_set(&hdcp->abort, abort);
  1223. cancel_delayed_work_sync(&hdcp->hdcp_auth_work);
  1224. flush_workqueue(hdcp->workq);
  1225. }
  1226. void *sde_hdcp_1x_init(struct sde_hdcp_init_data *init_data)
  1227. {
  1228. struct sde_hdcp_1x *hdcp = NULL;
  1229. char name[20];
  1230. static struct sde_hdcp_ops ops = {
  1231. .isr = sde_hdcp_1x_isr,
  1232. .cp_irq = sde_hdcp_1x_cp_irq,
  1233. .reauthenticate = sde_hdcp_1x_reauthenticate,
  1234. .authenticate = sde_hdcp_1x_authenticate,
  1235. .feature_supported = sde_hdcp_1x_feature_supported,
  1236. .force_encryption = sde_hdcp_1x_force_encryption,
  1237. .sink_support = sde_hdcp_1x_sink_support,
  1238. .abort = sde_hdcp_1x_abort,
  1239. .off = sde_hdcp_1x_off
  1240. };
  1241. if (!init_data || !init_data->notify_status ||
  1242. !init_data->workq || !init_data->cb_data) {
  1243. pr_err("invalid input\n");
  1244. goto error;
  1245. }
  1246. if (init_data->sec_access && !init_data->hdcp_io) {
  1247. pr_err("hdcp_io required\n");
  1248. goto error;
  1249. }
  1250. hdcp = kzalloc(sizeof(*hdcp), GFP_KERNEL);
  1251. if (!hdcp)
  1252. goto error;
  1253. hdcp->init_data = *init_data;
  1254. hdcp->ops = &ops;
  1255. snprintf(name, sizeof(name), "hdcp_1x_%d",
  1256. hdcp->init_data.client_id);
  1257. hdcp->workq = create_workqueue(name);
  1258. if (!hdcp->workq) {
  1259. pr_err("Error creating workqueue\n");
  1260. goto workqueue_error;
  1261. }
  1262. hdcp->hdcp1_handle = hdcp1_init();
  1263. if (!hdcp->hdcp1_handle) {
  1264. pr_err("Error creating HDCP 1.x handle\n");
  1265. goto hdcp1_handle_error;
  1266. }
  1267. sde_hdcp_1x_update_client_reg_set(hdcp);
  1268. INIT_DELAYED_WORK(&hdcp->hdcp_auth_work, sde_hdcp_1x_auth_work);
  1269. hdcp->hdcp_state = HDCP_STATE_INACTIVE;
  1270. init_completion(&hdcp->r0_checked);
  1271. init_completion(&hdcp->sink_r0_available);
  1272. hdcp->force_encryption = false;
  1273. pr_debug("HDCP module initialized. HDCP_STATE=%s\n",
  1274. SDE_HDCP_STATE_NAME);
  1275. return (void *)hdcp;
  1276. hdcp1_handle_error:
  1277. destroy_workqueue(hdcp->workq);
  1278. workqueue_error:
  1279. kfree(hdcp);
  1280. error:
  1281. return NULL;
  1282. } /* hdcp_1x_init */
  1283. struct sde_hdcp_ops *sde_hdcp_1x_get(void *input)
  1284. {
  1285. return ((struct sde_hdcp_1x *)input)->ops;
  1286. }