sde_hdcp_1x.c 37 KB

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