sde_hdcp_1x.c 39 KB

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