jr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright 2008-2014 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Based on CAAM driver in drivers/crypto/caam in Linux
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include "fsl_sec.h"
  11. #include "jr.h"
  12. #include "jobdesc.h"
  13. #include "desc_constr.h"
  14. #ifdef CONFIG_FSL_CORENET
  15. #include <asm/fsl_pamu.h>
  16. #endif
  17. #define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1))
  18. #define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size))
  19. struct jobring jr;
  20. static inline void start_jr0(void)
  21. {
  22. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  23. u32 ctpr_ms = sec_in32(&sec->ctpr_ms);
  24. u32 scfgr = sec_in32(&sec->scfgr);
  25. if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) {
  26. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  27. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1
  28. */
  29. if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) ||
  30. (!(ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) &&
  31. (scfgr & SEC_SCFGR_VIRT_EN)))
  32. sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
  33. } else {
  34. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  35. if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR)
  36. sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
  37. }
  38. }
  39. static inline void jr_reset_liodn(void)
  40. {
  41. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  42. sec_out32(&sec->jrliodnr[0].ls, 0);
  43. }
  44. static inline void jr_disable_irq(void)
  45. {
  46. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  47. uint32_t jrcfg = sec_in32(&regs->jrcfg1);
  48. jrcfg = jrcfg | JR_INTMASK;
  49. sec_out32(&regs->jrcfg1, jrcfg);
  50. }
  51. static void jr_initregs(void)
  52. {
  53. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  54. phys_addr_t ip_base = virt_to_phys((void *)jr.input_ring);
  55. phys_addr_t op_base = virt_to_phys((void *)jr.output_ring);
  56. #ifdef CONFIG_PHYS_64BIT
  57. sec_out32(&regs->irba_h, ip_base >> 32);
  58. #else
  59. sec_out32(&regs->irba_h, 0x0);
  60. #endif
  61. sec_out32(&regs->irba_l, (uint32_t)ip_base);
  62. #ifdef CONFIG_PHYS_64BIT
  63. sec_out32(&regs->orba_h, op_base >> 32);
  64. #else
  65. sec_out32(&regs->orba_h, 0x0);
  66. #endif
  67. sec_out32(&regs->orba_l, (uint32_t)op_base);
  68. sec_out32(&regs->ors, JR_SIZE);
  69. sec_out32(&regs->irs, JR_SIZE);
  70. if (!jr.irq)
  71. jr_disable_irq();
  72. }
  73. static int jr_init(void)
  74. {
  75. memset(&jr, 0, sizeof(struct jobring));
  76. jr.jq_id = DEFAULT_JR_ID;
  77. jr.irq = DEFAULT_IRQ;
  78. #ifdef CONFIG_FSL_CORENET
  79. jr.liodn = DEFAULT_JR_LIODN;
  80. #endif
  81. jr.size = JR_SIZE;
  82. jr.input_ring = (dma_addr_t *)memalign(ARCH_DMA_MINALIGN,
  83. JR_SIZE * sizeof(dma_addr_t));
  84. if (!jr.input_ring)
  85. return -1;
  86. jr.op_size = roundup(JR_SIZE * sizeof(struct op_ring),
  87. ARCH_DMA_MINALIGN);
  88. jr.output_ring =
  89. (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr.op_size);
  90. if (!jr.output_ring)
  91. return -1;
  92. memset(jr.input_ring, 0, JR_SIZE * sizeof(dma_addr_t));
  93. memset(jr.output_ring, 0, jr.op_size);
  94. start_jr0();
  95. jr_initregs();
  96. return 0;
  97. }
  98. static int jr_sw_cleanup(void)
  99. {
  100. jr.head = 0;
  101. jr.tail = 0;
  102. jr.read_idx = 0;
  103. jr.write_idx = 0;
  104. memset(jr.info, 0, sizeof(jr.info));
  105. memset(jr.input_ring, 0, jr.size * sizeof(dma_addr_t));
  106. memset(jr.output_ring, 0, jr.size * sizeof(struct op_ring));
  107. return 0;
  108. }
  109. static int jr_hw_reset(void)
  110. {
  111. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  112. uint32_t timeout = 100000;
  113. uint32_t jrint, jrcr;
  114. sec_out32(&regs->jrcr, JRCR_RESET);
  115. do {
  116. jrint = sec_in32(&regs->jrint);
  117. } while (((jrint & JRINT_ERR_HALT_MASK) ==
  118. JRINT_ERR_HALT_INPROGRESS) && --timeout);
  119. jrint = sec_in32(&regs->jrint);
  120. if (((jrint & JRINT_ERR_HALT_MASK) !=
  121. JRINT_ERR_HALT_INPROGRESS) && timeout == 0)
  122. return -1;
  123. timeout = 100000;
  124. sec_out32(&regs->jrcr, JRCR_RESET);
  125. do {
  126. jrcr = sec_in32(&regs->jrcr);
  127. } while ((jrcr & JRCR_RESET) && --timeout);
  128. if (timeout == 0)
  129. return -1;
  130. return 0;
  131. }
  132. /* -1 --- error, can't enqueue -- no space available */
  133. static int jr_enqueue(uint32_t *desc_addr,
  134. void (*callback)(uint32_t status, void *arg),
  135. void *arg)
  136. {
  137. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  138. int head = jr.head;
  139. uint32_t desc_word;
  140. int length = desc_len(desc_addr);
  141. int i;
  142. #ifdef CONFIG_PHYS_64BIT
  143. uint32_t *addr_hi, *addr_lo;
  144. #endif
  145. /* The descriptor must be submitted to SEC block as per endianness
  146. * of the SEC Block.
  147. * So, if the endianness of Core and SEC block is different, each word
  148. * of the descriptor will be byte-swapped.
  149. */
  150. for (i = 0; i < length; i++) {
  151. desc_word = desc_addr[i];
  152. sec_out32((uint32_t *)&desc_addr[i], desc_word);
  153. }
  154. phys_addr_t desc_phys_addr = virt_to_phys(desc_addr);
  155. if (sec_in32(&regs->irsa) == 0 ||
  156. CIRC_SPACE(jr.head, jr.tail, jr.size) <= 0)
  157. return -1;
  158. jr.info[head].desc_phys_addr = desc_phys_addr;
  159. jr.info[head].callback = (void *)callback;
  160. jr.info[head].arg = arg;
  161. jr.info[head].op_done = 0;
  162. unsigned long start = (unsigned long)&jr.info[head] &
  163. ~(ARCH_DMA_MINALIGN - 1);
  164. unsigned long end = ALIGN((unsigned long)&jr.info[head] +
  165. sizeof(struct jr_info), ARCH_DMA_MINALIGN);
  166. flush_dcache_range(start, end);
  167. #ifdef CONFIG_PHYS_64BIT
  168. /* Write the 64 bit Descriptor address on Input Ring.
  169. * The 32 bit hign and low part of the address will
  170. * depend on endianness of SEC block.
  171. */
  172. #ifdef CONFIG_SYS_FSL_SEC_LE
  173. addr_lo = (uint32_t *)(&jr.input_ring[head]);
  174. addr_hi = (uint32_t *)(&jr.input_ring[head]) + 1;
  175. #elif defined(CONFIG_SYS_FSL_SEC_BE)
  176. addr_hi = (uint32_t *)(&jr.input_ring[head]);
  177. addr_lo = (uint32_t *)(&jr.input_ring[head]) + 1;
  178. #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
  179. sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32));
  180. sec_out32(addr_lo, (uint32_t)(desc_phys_addr));
  181. #else
  182. /* Write the 32 bit Descriptor address on Input Ring. */
  183. sec_out32(&jr.input_ring[head], desc_phys_addr);
  184. #endif /* ifdef CONFIG_PHYS_64BIT */
  185. start = (unsigned long)&jr.input_ring[head] & ~(ARCH_DMA_MINALIGN - 1);
  186. end = ALIGN((unsigned long)&jr.input_ring[head] +
  187. sizeof(dma_addr_t), ARCH_DMA_MINALIGN);
  188. flush_dcache_range(start, end);
  189. jr.head = (head + 1) & (jr.size - 1);
  190. /* Invalidate output ring */
  191. start = (unsigned long)jr.output_ring &
  192. ~(ARCH_DMA_MINALIGN - 1);
  193. end = ALIGN((unsigned long)jr.output_ring + jr.op_size,
  194. ARCH_DMA_MINALIGN);
  195. invalidate_dcache_range(start, end);
  196. sec_out32(&regs->irja, 1);
  197. return 0;
  198. }
  199. static int jr_dequeue(void)
  200. {
  201. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  202. int head = jr.head;
  203. int tail = jr.tail;
  204. int idx, i, found;
  205. void (*callback)(uint32_t status, void *arg);
  206. void *arg = NULL;
  207. #ifdef CONFIG_PHYS_64BIT
  208. uint32_t *addr_hi, *addr_lo;
  209. #else
  210. uint32_t *addr;
  211. #endif
  212. while (sec_in32(&regs->orsf) && CIRC_CNT(jr.head, jr.tail, jr.size)) {
  213. found = 0;
  214. phys_addr_t op_desc;
  215. #ifdef CONFIG_PHYS_64BIT
  216. /* Read the 64 bit Descriptor address from Output Ring.
  217. * The 32 bit hign and low part of the address will
  218. * depend on endianness of SEC block.
  219. */
  220. #ifdef CONFIG_SYS_FSL_SEC_LE
  221. addr_lo = (uint32_t *)(&jr.output_ring[jr.tail].desc);
  222. addr_hi = (uint32_t *)(&jr.output_ring[jr.tail].desc) + 1;
  223. #elif defined(CONFIG_SYS_FSL_SEC_BE)
  224. addr_hi = (uint32_t *)(&jr.output_ring[jr.tail].desc);
  225. addr_lo = (uint32_t *)(&jr.output_ring[jr.tail].desc) + 1;
  226. #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
  227. op_desc = ((u64)sec_in32(addr_hi) << 32) |
  228. ((u64)sec_in32(addr_lo));
  229. #else
  230. /* Read the 32 bit Descriptor address from Output Ring. */
  231. addr = (uint32_t *)&jr.output_ring[jr.tail].desc;
  232. op_desc = sec_in32(addr);
  233. #endif /* ifdef CONFIG_PHYS_64BIT */
  234. uint32_t status = sec_in32(&jr.output_ring[jr.tail].status);
  235. for (i = 0; CIRC_CNT(head, tail + i, jr.size) >= 1; i++) {
  236. idx = (tail + i) & (jr.size - 1);
  237. if (op_desc == jr.info[idx].desc_phys_addr) {
  238. found = 1;
  239. break;
  240. }
  241. }
  242. /* Error condition if match not found */
  243. if (!found)
  244. return -1;
  245. jr.info[idx].op_done = 1;
  246. callback = (void *)jr.info[idx].callback;
  247. arg = jr.info[idx].arg;
  248. /* When the job on tail idx gets done, increment
  249. * tail till the point where job completed out of oredr has
  250. * been taken into account
  251. */
  252. if (idx == tail)
  253. do {
  254. tail = (tail + 1) & (jr.size - 1);
  255. } while (jr.info[tail].op_done);
  256. jr.tail = tail;
  257. jr.read_idx = (jr.read_idx + 1) & (jr.size - 1);
  258. sec_out32(&regs->orjr, 1);
  259. jr.info[idx].op_done = 0;
  260. callback(status, arg);
  261. }
  262. return 0;
  263. }
  264. static void desc_done(uint32_t status, void *arg)
  265. {
  266. struct result *x = arg;
  267. x->status = status;
  268. caam_jr_strstatus(status);
  269. x->done = 1;
  270. }
  271. int run_descriptor_jr(uint32_t *desc)
  272. {
  273. unsigned long long timeval = get_ticks();
  274. unsigned long long timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
  275. struct result op;
  276. int ret = 0;
  277. memset(&op, 0, sizeof(op));
  278. ret = jr_enqueue(desc, desc_done, &op);
  279. if (ret) {
  280. debug("Error in SEC enq\n");
  281. ret = JQ_ENQ_ERR;
  282. goto out;
  283. }
  284. timeval = get_ticks();
  285. timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
  286. while (op.done != 1) {
  287. ret = jr_dequeue();
  288. if (ret) {
  289. debug("Error in SEC deq\n");
  290. ret = JQ_DEQ_ERR;
  291. goto out;
  292. }
  293. if ((get_ticks() - timeval) > timeout) {
  294. debug("SEC Dequeue timed out\n");
  295. ret = JQ_DEQ_TO_ERR;
  296. goto out;
  297. }
  298. }
  299. if (op.status) {
  300. debug("Error %x\n", op.status);
  301. ret = op.status;
  302. }
  303. out:
  304. return ret;
  305. }
  306. int jr_reset(void)
  307. {
  308. if (jr_hw_reset() < 0)
  309. return -1;
  310. /* Clean up the jobring structure maintained by software */
  311. jr_sw_cleanup();
  312. return 0;
  313. }
  314. int sec_reset(void)
  315. {
  316. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  317. uint32_t mcfgr = sec_in32(&sec->mcfgr);
  318. uint32_t timeout = 100000;
  319. mcfgr |= MCFGR_SWRST;
  320. sec_out32(&sec->mcfgr, mcfgr);
  321. mcfgr |= MCFGR_DMA_RST;
  322. sec_out32(&sec->mcfgr, mcfgr);
  323. do {
  324. mcfgr = sec_in32(&sec->mcfgr);
  325. } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout);
  326. if (timeout == 0)
  327. return -1;
  328. timeout = 100000;
  329. do {
  330. mcfgr = sec_in32(&sec->mcfgr);
  331. } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout);
  332. if (timeout == 0)
  333. return -1;
  334. return 0;
  335. }
  336. static int instantiate_rng(void)
  337. {
  338. struct result op;
  339. u32 *desc;
  340. u32 rdsta_val;
  341. int ret = 0;
  342. ccsr_sec_t __iomem *sec =
  343. (ccsr_sec_t __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
  344. struct rng4tst __iomem *rng =
  345. (struct rng4tst __iomem *)&sec->rng;
  346. memset(&op, 0, sizeof(struct result));
  347. desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6);
  348. if (!desc) {
  349. printf("cannot allocate RNG init descriptor memory\n");
  350. return -1;
  351. }
  352. inline_cnstr_jobdesc_rng_instantiation(desc);
  353. int size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN);
  354. flush_dcache_range((unsigned long)desc,
  355. (unsigned long)desc + size);
  356. ret = run_descriptor_jr(desc);
  357. if (ret)
  358. printf("RNG: Instantiation failed with error %x\n", ret);
  359. rdsta_val = sec_in32(&rng->rdsta);
  360. if (op.status || !(rdsta_val & RNG_STATE0_HANDLE_INSTANTIATED))
  361. return -1;
  362. return ret;
  363. }
  364. static u8 get_rng_vid(void)
  365. {
  366. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  367. u32 cha_vid = sec_in32(&sec->chavid_ls);
  368. return (cha_vid & SEC_CHAVID_RNG_LS_MASK) >> SEC_CHAVID_LS_RNG_SHIFT;
  369. }
  370. /*
  371. * By default, the TRNG runs for 200 clocks per sample;
  372. * 1200 clocks per sample generates better entropy.
  373. */
  374. static void kick_trng(int ent_delay)
  375. {
  376. ccsr_sec_t __iomem *sec =
  377. (ccsr_sec_t __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
  378. struct rng4tst __iomem *rng =
  379. (struct rng4tst __iomem *)&sec->rng;
  380. u32 val;
  381. /* put RNG4 into program mode */
  382. sec_setbits32(&rng->rtmctl, RTMCTL_PRGM);
  383. /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the
  384. * length (in system clocks) of each Entropy sample taken
  385. * */
  386. val = sec_in32(&rng->rtsdctl);
  387. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  388. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  389. sec_out32(&rng->rtsdctl, val);
  390. /* min. freq. count, equal to 1/4 of the entropy sample length */
  391. sec_out32(&rng->rtfreqmin, ent_delay >> 2);
  392. /* disable maximum frequency count */
  393. sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE);
  394. /*
  395. * select raw sampling in both entropy shifter
  396. * and statistical checker
  397. */
  398. sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC);
  399. /* put RNG4 into run mode */
  400. sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM);
  401. }
  402. static int rng_init(void)
  403. {
  404. int ret, ent_delay = RTSDCTL_ENT_DLY_MIN;
  405. ccsr_sec_t __iomem *sec =
  406. (ccsr_sec_t __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
  407. struct rng4tst __iomem *rng =
  408. (struct rng4tst __iomem *)&sec->rng;
  409. u32 rdsta = sec_in32(&rng->rdsta);
  410. /* Check if RNG state 0 handler is already instantiated */
  411. if (rdsta & RNG_STATE0_HANDLE_INSTANTIATED)
  412. return 0;
  413. do {
  414. /*
  415. * If either of the SH's were instantiated by somebody else
  416. * then it is assumed that the entropy
  417. * parameters are properly set and thus the function
  418. * setting these (kick_trng(...)) is skipped.
  419. * Also, if a handle was instantiated, do not change
  420. * the TRNG parameters.
  421. */
  422. kick_trng(ent_delay);
  423. ent_delay += 400;
  424. /*
  425. * if instantiate_rng(...) fails, the loop will rerun
  426. * and the kick_trng(...) function will modfiy the
  427. * upper and lower limits of the entropy sampling
  428. * interval, leading to a sucessful initialization of
  429. * the RNG.
  430. */
  431. ret = instantiate_rng();
  432. } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  433. if (ret) {
  434. printf("RNG: Failed to instantiate RNG\n");
  435. return ret;
  436. }
  437. /* Enable RDB bit so that RNG works faster */
  438. sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE);
  439. return ret;
  440. }
  441. int sec_init(void)
  442. {
  443. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  444. uint32_t mcr = sec_in32(&sec->mcfgr);
  445. int ret = 0;
  446. #ifdef CONFIG_FSL_CORENET
  447. uint32_t liodnr;
  448. uint32_t liodn_ns;
  449. uint32_t liodn_s;
  450. #endif
  451. mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT);
  452. #ifdef CONFIG_PHYS_64BIT
  453. mcr |= (1 << MCFGR_PS_SHIFT);
  454. #endif
  455. sec_out32(&sec->mcfgr, mcr);
  456. #ifdef CONFIG_FSL_CORENET
  457. liodnr = sec_in32(&sec->jrliodnr[0].ls);
  458. liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT;
  459. liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT;
  460. #endif
  461. ret = jr_init();
  462. if (ret < 0) {
  463. printf("SEC initialization failed\n");
  464. return -1;
  465. }
  466. #ifdef CONFIG_FSL_CORENET
  467. ret = sec_config_pamu_table(liodn_ns, liodn_s);
  468. if (ret < 0)
  469. return -1;
  470. pamu_enable();
  471. #endif
  472. if (get_rng_vid() >= 4) {
  473. if (rng_init() < 0) {
  474. printf("RNG instantiation failed\n");
  475. return -1;
  476. }
  477. printf("SEC: RNG instantiated\n");
  478. }
  479. return ret;
  480. }