sde_hdcp_1x.c 39 KB

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