sec_drv.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Hisilicon SEC units found on Hip06 Hip07
  4. *
  5. * Copyright (c) 2016-2017 Hisilicon Limited.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/atomic.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-direction.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/io.h>
  14. #include <linux/iommu.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqreturn.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include "sec_drv.h"
  24. #define SEC_QUEUE_AR_FROCE_ALLOC 0
  25. #define SEC_QUEUE_AR_FROCE_NOALLOC 1
  26. #define SEC_QUEUE_AR_FROCE_DIS 2
  27. #define SEC_QUEUE_AW_FROCE_ALLOC 0
  28. #define SEC_QUEUE_AW_FROCE_NOALLOC 1
  29. #define SEC_QUEUE_AW_FROCE_DIS 2
  30. /* SEC_ALGSUB registers */
  31. #define SEC_ALGSUB_CLK_EN_REG 0x03b8
  32. #define SEC_ALGSUB_CLK_DIS_REG 0x03bc
  33. #define SEC_ALGSUB_CLK_ST_REG 0x535c
  34. #define SEC_ALGSUB_RST_REQ_REG 0x0aa8
  35. #define SEC_ALGSUB_RST_DREQ_REG 0x0aac
  36. #define SEC_ALGSUB_RST_ST_REG 0x5a54
  37. #define SEC_ALGSUB_RST_ST_IS_RST BIT(0)
  38. #define SEC_ALGSUB_BUILD_RST_REQ_REG 0x0ab8
  39. #define SEC_ALGSUB_BUILD_RST_DREQ_REG 0x0abc
  40. #define SEC_ALGSUB_BUILD_RST_ST_REG 0x5a5c
  41. #define SEC_ALGSUB_BUILD_RST_ST_IS_RST BIT(0)
  42. #define SEC_SAA_BASE 0x00001000UL
  43. /* SEC_SAA registers */
  44. #define SEC_SAA_CTRL_REG(x) ((x) * SEC_SAA_ADDR_SIZE)
  45. #define SEC_SAA_CTRL_GET_QM_EN BIT(0)
  46. #define SEC_ST_INTMSK1_REG 0x0200
  47. #define SEC_ST_RINT1_REG 0x0400
  48. #define SEC_ST_INTSTS1_REG 0x0600
  49. #define SEC_BD_MNG_STAT_REG 0x0800
  50. #define SEC_PARSING_STAT_REG 0x0804
  51. #define SEC_LOAD_TIME_OUT_CNT_REG 0x0808
  52. #define SEC_CORE_WORK_TIME_OUT_CNT_REG 0x080c
  53. #define SEC_BACK_TIME_OUT_CNT_REG 0x0810
  54. #define SEC_BD1_PARSING_RD_TIME_OUT_CNT_REG 0x0814
  55. #define SEC_BD1_PARSING_WR_TIME_OUT_CNT_REG 0x0818
  56. #define SEC_BD2_PARSING_RD_TIME_OUT_CNT_REG 0x081c
  57. #define SEC_BD2_PARSING_WR_TIME_OUT_CNT_REG 0x0820
  58. #define SEC_SAA_ACC_REG 0x083c
  59. #define SEC_BD_NUM_CNT_IN_SEC_REG 0x0858
  60. #define SEC_LOAD_WORK_TIME_CNT_REG 0x0860
  61. #define SEC_CORE_WORK_WORK_TIME_CNT_REG 0x0864
  62. #define SEC_BACK_WORK_TIME_CNT_REG 0x0868
  63. #define SEC_SAA_IDLE_TIME_CNT_REG 0x086c
  64. #define SEC_SAA_CLK_CNT_REG 0x0870
  65. /* SEC_COMMON registers */
  66. #define SEC_CLK_EN_REG 0x0000
  67. #define SEC_CTRL_REG 0x0004
  68. #define SEC_COMMON_CNT_CLR_CE_REG 0x0008
  69. #define SEC_COMMON_CNT_CLR_CE_CLEAR BIT(0)
  70. #define SEC_COMMON_CNT_CLR_CE_SNAP_EN BIT(1)
  71. #define SEC_SECURE_CTRL_REG 0x000c
  72. #define SEC_AXI_CACHE_CFG_REG 0x0010
  73. #define SEC_AXI_QOS_CFG_REG 0x0014
  74. #define SEC_IPV4_MASK_TABLE_REG 0x0020
  75. #define SEC_IPV6_MASK_TABLE_X_REG(x) (0x0024 + (x) * 4)
  76. #define SEC_FSM_MAX_CNT_REG 0x0064
  77. #define SEC_CTRL2_REG 0x0068
  78. #define SEC_CTRL2_DATA_AXI_RD_OTSD_CFG_M GENMASK(3, 0)
  79. #define SEC_CTRL2_DATA_AXI_RD_OTSD_CFG_S 0
  80. #define SEC_CTRL2_DATA_AXI_WR_OTSD_CFG_M GENMASK(6, 4)
  81. #define SEC_CTRL2_DATA_AXI_WR_OTSD_CFG_S 4
  82. #define SEC_CTRL2_CLK_GATE_EN BIT(7)
  83. #define SEC_CTRL2_ENDIAN_BD BIT(8)
  84. #define SEC_CTRL2_ENDIAN_BD_TYPE BIT(9)
  85. #define SEC_CNT_PRECISION_CFG_REG 0x006c
  86. #define SEC_DEBUG_BD_CFG_REG 0x0070
  87. #define SEC_DEBUG_BD_CFG_WB_NORMAL BIT(0)
  88. #define SEC_DEBUG_BD_CFG_WB_EN BIT(1)
  89. #define SEC_Q_SIGHT_SEL 0x0074
  90. #define SEC_Q_SIGHT_HIS_CLR 0x0078
  91. #define SEC_Q_VMID_CFG_REG(q) (0x0100 + (q) * 4)
  92. #define SEC_Q_WEIGHT_CFG_REG(q) (0x200 + (q) * 4)
  93. #define SEC_STAT_CLR_REG 0x0a00
  94. #define SEC_SAA_IDLE_CNT_CLR_REG 0x0a04
  95. #define SEC_QM_CPL_Q_IDBUF_DFX_CFG_REG 0x0b00
  96. #define SEC_QM_CPL_Q_IDBUF_DFX_RESULT_REG 0x0b04
  97. #define SEC_QM_BD_DFX_CFG_REG 0x0b08
  98. #define SEC_QM_BD_DFX_RESULT_REG 0x0b0c
  99. #define SEC_QM_BDID_DFX_RESULT_REG 0x0b10
  100. #define SEC_QM_BD_DFIFO_STATUS_REG 0x0b14
  101. #define SEC_QM_BD_DFX_CFG2_REG 0x0b1c
  102. #define SEC_QM_BD_DFX_RESULT2_REG 0x0b20
  103. #define SEC_QM_BD_IDFIFO_STATUS_REG 0x0b18
  104. #define SEC_QM_BD_DFIFO_STATUS2_REG 0x0b28
  105. #define SEC_QM_BD_IDFIFO_STATUS2_REG 0x0b2c
  106. #define SEC_HASH_IPV4_MASK 0xfff00000
  107. #define SEC_MAX_SAA_NUM 0xa
  108. #define SEC_SAA_ADDR_SIZE 0x1000
  109. #define SEC_Q_INIT_REG 0x0
  110. #define SEC_Q_INIT_WO_STAT_CLEAR 0x2
  111. #define SEC_Q_INIT_AND_STAT_CLEAR 0x3
  112. #define SEC_Q_CFG_REG 0x8
  113. #define SEC_Q_CFG_REORDER BIT(0)
  114. #define SEC_Q_PROC_NUM_CFG_REG 0x10
  115. #define SEC_QUEUE_ENB_REG 0x18
  116. #define SEC_Q_DEPTH_CFG_REG 0x50
  117. #define SEC_Q_DEPTH_CFG_DEPTH_M GENMASK(11, 0)
  118. #define SEC_Q_DEPTH_CFG_DEPTH_S 0
  119. #define SEC_Q_BASE_HADDR_REG 0x54
  120. #define SEC_Q_BASE_LADDR_REG 0x58
  121. #define SEC_Q_WR_PTR_REG 0x5c
  122. #define SEC_Q_OUTORDER_BASE_HADDR_REG 0x60
  123. #define SEC_Q_OUTORDER_BASE_LADDR_REG 0x64
  124. #define SEC_Q_OUTORDER_RD_PTR_REG 0x68
  125. #define SEC_Q_OT_TH_REG 0x6c
  126. #define SEC_Q_ARUSER_CFG_REG 0x70
  127. #define SEC_Q_ARUSER_CFG_FA BIT(0)
  128. #define SEC_Q_ARUSER_CFG_FNA BIT(1)
  129. #define SEC_Q_ARUSER_CFG_RINVLD BIT(2)
  130. #define SEC_Q_ARUSER_CFG_PKG BIT(3)
  131. #define SEC_Q_AWUSER_CFG_REG 0x74
  132. #define SEC_Q_AWUSER_CFG_FA BIT(0)
  133. #define SEC_Q_AWUSER_CFG_FNA BIT(1)
  134. #define SEC_Q_AWUSER_CFG_PKG BIT(2)
  135. #define SEC_Q_ERR_BASE_HADDR_REG 0x7c
  136. #define SEC_Q_ERR_BASE_LADDR_REG 0x80
  137. #define SEC_Q_CFG_VF_NUM_REG 0x84
  138. #define SEC_Q_SOFT_PROC_PTR_REG 0x88
  139. #define SEC_Q_FAIL_INT_MSK_REG 0x300
  140. #define SEC_Q_FLOW_INT_MKS_REG 0x304
  141. #define SEC_Q_FAIL_RINT_REG 0x400
  142. #define SEC_Q_FLOW_RINT_REG 0x404
  143. #define SEC_Q_FAIL_INT_STATUS_REG 0x500
  144. #define SEC_Q_FLOW_INT_STATUS_REG 0x504
  145. #define SEC_Q_STATUS_REG 0x600
  146. #define SEC_Q_RD_PTR_REG 0x604
  147. #define SEC_Q_PRO_PTR_REG 0x608
  148. #define SEC_Q_OUTORDER_WR_PTR_REG 0x60c
  149. #define SEC_Q_OT_CNT_STATUS_REG 0x610
  150. #define SEC_Q_INORDER_BD_NUM_ST_REG 0x650
  151. #define SEC_Q_INORDER_GET_FLAG_ST_REG 0x654
  152. #define SEC_Q_INORDER_ADD_FLAG_ST_REG 0x658
  153. #define SEC_Q_INORDER_TASK_INT_NUM_LEFT_ST_REG 0x65c
  154. #define SEC_Q_RD_DONE_PTR_REG 0x660
  155. #define SEC_Q_CPL_Q_BD_NUM_ST_REG 0x700
  156. #define SEC_Q_CPL_Q_PTR_ST_REG 0x704
  157. #define SEC_Q_CPL_Q_H_ADDR_ST_REG 0x708
  158. #define SEC_Q_CPL_Q_L_ADDR_ST_REG 0x70c
  159. #define SEC_Q_CPL_TASK_INT_NUM_LEFT_ST_REG 0x710
  160. #define SEC_Q_WRR_ID_CHECK_REG 0x714
  161. #define SEC_Q_CPLQ_FULL_CHECK_REG 0x718
  162. #define SEC_Q_SUCCESS_BD_CNT_REG 0x800
  163. #define SEC_Q_FAIL_BD_CNT_REG 0x804
  164. #define SEC_Q_GET_BD_CNT_REG 0x808
  165. #define SEC_Q_IVLD_CNT_REG 0x80c
  166. #define SEC_Q_BD_PROC_GET_CNT_REG 0x810
  167. #define SEC_Q_BD_PROC_DONE_CNT_REG 0x814
  168. #define SEC_Q_LAT_CLR_REG 0x850
  169. #define SEC_Q_PKT_LAT_MAX_REG 0x854
  170. #define SEC_Q_PKT_LAT_AVG_REG 0x858
  171. #define SEC_Q_PKT_LAT_MIN_REG 0x85c
  172. #define SEC_Q_ID_CLR_CFG_REG 0x900
  173. #define SEC_Q_1ST_BD_ERR_ID_REG 0x904
  174. #define SEC_Q_1ST_AUTH_FAIL_ID_REG 0x908
  175. #define SEC_Q_1ST_RD_ERR_ID_REG 0x90c
  176. #define SEC_Q_1ST_ECC2_ERR_ID_REG 0x910
  177. #define SEC_Q_1ST_IVLD_ID_REG 0x914
  178. #define SEC_Q_1ST_BD_WR_ERR_ID_REG 0x918
  179. #define SEC_Q_1ST_ERR_BD_WR_ERR_ID_REG 0x91c
  180. #define SEC_Q_1ST_BD_MAC_WR_ERR_ID_REG 0x920
  181. struct sec_debug_bd_info {
  182. #define SEC_DEBUG_BD_INFO_SOFT_ERR_CHECK_M GENMASK(22, 0)
  183. u32 soft_err_check;
  184. #define SEC_DEBUG_BD_INFO_HARD_ERR_CHECK_M GENMASK(9, 0)
  185. u32 hard_err_check;
  186. u32 icv_mac1st_word;
  187. #define SEC_DEBUG_BD_INFO_GET_ID_M GENMASK(19, 0)
  188. u32 sec_get_id;
  189. /* W4---W15 */
  190. u32 reserv_left[12];
  191. };
  192. struct sec_out_bd_info {
  193. #define SEC_OUT_BD_INFO_Q_ID_M GENMASK(11, 0)
  194. #define SEC_OUT_BD_INFO_ECC_2BIT_ERR BIT(14)
  195. u16 data;
  196. };
  197. #define SEC_MAX_DEVICES 8
  198. static struct sec_dev_info *sec_devices[SEC_MAX_DEVICES];
  199. static DEFINE_MUTEX(sec_id_lock);
  200. static int sec_queue_map_io(struct sec_queue *queue)
  201. {
  202. struct device *dev = queue->dev_info->dev;
  203. struct resource *res;
  204. res = platform_get_resource(to_platform_device(dev),
  205. IORESOURCE_MEM,
  206. 2 + queue->queue_id);
  207. if (!res) {
  208. dev_err(dev, "Failed to get queue %d memory resource\n",
  209. queue->queue_id);
  210. return -ENOMEM;
  211. }
  212. queue->regs = ioremap(res->start, resource_size(res));
  213. if (!queue->regs)
  214. return -ENOMEM;
  215. return 0;
  216. }
  217. static void sec_queue_unmap_io(struct sec_queue *queue)
  218. {
  219. iounmap(queue->regs);
  220. }
  221. static int sec_queue_ar_pkgattr(struct sec_queue *queue, u32 ar_pkg)
  222. {
  223. void __iomem *addr = queue->regs + SEC_Q_ARUSER_CFG_REG;
  224. u32 regval;
  225. regval = readl_relaxed(addr);
  226. if (ar_pkg)
  227. regval |= SEC_Q_ARUSER_CFG_PKG;
  228. else
  229. regval &= ~SEC_Q_ARUSER_CFG_PKG;
  230. writel_relaxed(regval, addr);
  231. return 0;
  232. }
  233. static int sec_queue_aw_pkgattr(struct sec_queue *queue, u32 aw_pkg)
  234. {
  235. void __iomem *addr = queue->regs + SEC_Q_AWUSER_CFG_REG;
  236. u32 regval;
  237. regval = readl_relaxed(addr);
  238. regval |= SEC_Q_AWUSER_CFG_PKG;
  239. writel_relaxed(regval, addr);
  240. return 0;
  241. }
  242. static int sec_clk_en(struct sec_dev_info *info)
  243. {
  244. void __iomem *base = info->regs[SEC_COMMON];
  245. u32 i = 0;
  246. writel_relaxed(0x7, base + SEC_ALGSUB_CLK_EN_REG);
  247. do {
  248. usleep_range(1000, 10000);
  249. if ((readl_relaxed(base + SEC_ALGSUB_CLK_ST_REG) & 0x7) == 0x7)
  250. return 0;
  251. i++;
  252. } while (i < 10);
  253. dev_err(info->dev, "sec clock enable fail!\n");
  254. return -EIO;
  255. }
  256. static int sec_clk_dis(struct sec_dev_info *info)
  257. {
  258. void __iomem *base = info->regs[SEC_COMMON];
  259. u32 i = 0;
  260. writel_relaxed(0x7, base + SEC_ALGSUB_CLK_DIS_REG);
  261. do {
  262. usleep_range(1000, 10000);
  263. if ((readl_relaxed(base + SEC_ALGSUB_CLK_ST_REG) & 0x7) == 0)
  264. return 0;
  265. i++;
  266. } while (i < 10);
  267. dev_err(info->dev, "sec clock disable fail!\n");
  268. return -EIO;
  269. }
  270. static int sec_reset_whole_module(struct sec_dev_info *info)
  271. {
  272. void __iomem *base = info->regs[SEC_COMMON];
  273. bool is_reset, b_is_reset;
  274. u32 i = 0;
  275. writel_relaxed(1, base + SEC_ALGSUB_RST_REQ_REG);
  276. writel_relaxed(1, base + SEC_ALGSUB_BUILD_RST_REQ_REG);
  277. while (1) {
  278. usleep_range(1000, 10000);
  279. is_reset = readl_relaxed(base + SEC_ALGSUB_RST_ST_REG) &
  280. SEC_ALGSUB_RST_ST_IS_RST;
  281. b_is_reset = readl_relaxed(base + SEC_ALGSUB_BUILD_RST_ST_REG) &
  282. SEC_ALGSUB_BUILD_RST_ST_IS_RST;
  283. if (is_reset && b_is_reset)
  284. break;
  285. i++;
  286. if (i > 10) {
  287. dev_err(info->dev, "Reset req failed\n");
  288. return -EIO;
  289. }
  290. }
  291. i = 0;
  292. writel_relaxed(1, base + SEC_ALGSUB_RST_DREQ_REG);
  293. writel_relaxed(1, base + SEC_ALGSUB_BUILD_RST_DREQ_REG);
  294. while (1) {
  295. usleep_range(1000, 10000);
  296. is_reset = readl_relaxed(base + SEC_ALGSUB_RST_ST_REG) &
  297. SEC_ALGSUB_RST_ST_IS_RST;
  298. b_is_reset = readl_relaxed(base + SEC_ALGSUB_BUILD_RST_ST_REG) &
  299. SEC_ALGSUB_BUILD_RST_ST_IS_RST;
  300. if (!is_reset && !b_is_reset)
  301. break;
  302. i++;
  303. if (i > 10) {
  304. dev_err(info->dev, "Reset dreq failed\n");
  305. return -EIO;
  306. }
  307. }
  308. return 0;
  309. }
  310. static void sec_bd_endian_little(struct sec_dev_info *info)
  311. {
  312. void __iomem *addr = info->regs[SEC_SAA] + SEC_CTRL2_REG;
  313. u32 regval;
  314. regval = readl_relaxed(addr);
  315. regval &= ~(SEC_CTRL2_ENDIAN_BD | SEC_CTRL2_ENDIAN_BD_TYPE);
  316. writel_relaxed(regval, addr);
  317. }
  318. /*
  319. * sec_cache_config - configure optimum cache placement
  320. */
  321. static void sec_cache_config(struct sec_dev_info *info)
  322. {
  323. struct iommu_domain *domain;
  324. void __iomem *addr = info->regs[SEC_SAA] + SEC_CTRL_REG;
  325. domain = iommu_get_domain_for_dev(info->dev);
  326. /* Check that translation is occurring */
  327. if (domain && (domain->type & __IOMMU_DOMAIN_PAGING))
  328. writel_relaxed(0x44cf9e, addr);
  329. else
  330. writel_relaxed(0x4cfd9, addr);
  331. }
  332. static void sec_data_axiwr_otsd_cfg(struct sec_dev_info *info, u32 cfg)
  333. {
  334. void __iomem *addr = info->regs[SEC_SAA] + SEC_CTRL2_REG;
  335. u32 regval;
  336. regval = readl_relaxed(addr);
  337. regval &= ~SEC_CTRL2_DATA_AXI_WR_OTSD_CFG_M;
  338. regval |= (cfg << SEC_CTRL2_DATA_AXI_WR_OTSD_CFG_S) &
  339. SEC_CTRL2_DATA_AXI_WR_OTSD_CFG_M;
  340. writel_relaxed(regval, addr);
  341. }
  342. static void sec_data_axird_otsd_cfg(struct sec_dev_info *info, u32 cfg)
  343. {
  344. void __iomem *addr = info->regs[SEC_SAA] + SEC_CTRL2_REG;
  345. u32 regval;
  346. regval = readl_relaxed(addr);
  347. regval &= ~SEC_CTRL2_DATA_AXI_RD_OTSD_CFG_M;
  348. regval |= (cfg << SEC_CTRL2_DATA_AXI_RD_OTSD_CFG_S) &
  349. SEC_CTRL2_DATA_AXI_RD_OTSD_CFG_M;
  350. writel_relaxed(regval, addr);
  351. }
  352. static void sec_clk_gate_en(struct sec_dev_info *info, bool clkgate)
  353. {
  354. void __iomem *addr = info->regs[SEC_SAA] + SEC_CTRL2_REG;
  355. u32 regval;
  356. regval = readl_relaxed(addr);
  357. if (clkgate)
  358. regval |= SEC_CTRL2_CLK_GATE_EN;
  359. else
  360. regval &= ~SEC_CTRL2_CLK_GATE_EN;
  361. writel_relaxed(regval, addr);
  362. }
  363. static void sec_comm_cnt_cfg(struct sec_dev_info *info, bool clr_ce)
  364. {
  365. void __iomem *addr = info->regs[SEC_SAA] + SEC_COMMON_CNT_CLR_CE_REG;
  366. u32 regval;
  367. regval = readl_relaxed(addr);
  368. if (clr_ce)
  369. regval |= SEC_COMMON_CNT_CLR_CE_CLEAR;
  370. else
  371. regval &= ~SEC_COMMON_CNT_CLR_CE_CLEAR;
  372. writel_relaxed(regval, addr);
  373. }
  374. static void sec_commsnap_en(struct sec_dev_info *info, bool snap_en)
  375. {
  376. void __iomem *addr = info->regs[SEC_SAA] + SEC_COMMON_CNT_CLR_CE_REG;
  377. u32 regval;
  378. regval = readl_relaxed(addr);
  379. if (snap_en)
  380. regval |= SEC_COMMON_CNT_CLR_CE_SNAP_EN;
  381. else
  382. regval &= ~SEC_COMMON_CNT_CLR_CE_SNAP_EN;
  383. writel_relaxed(regval, addr);
  384. }
  385. static void sec_ipv6_hashmask(struct sec_dev_info *info, u32 hash_mask[])
  386. {
  387. void __iomem *base = info->regs[SEC_SAA];
  388. int i;
  389. for (i = 0; i < 10; i++)
  390. writel_relaxed(hash_mask[0],
  391. base + SEC_IPV6_MASK_TABLE_X_REG(i));
  392. }
  393. static int sec_ipv4_hashmask(struct sec_dev_info *info, u32 hash_mask)
  394. {
  395. if (hash_mask & SEC_HASH_IPV4_MASK) {
  396. dev_err(info->dev, "Sec Ipv4 Hash Mask Input Error!\n ");
  397. return -EINVAL;
  398. }
  399. writel_relaxed(hash_mask,
  400. info->regs[SEC_SAA] + SEC_IPV4_MASK_TABLE_REG);
  401. return 0;
  402. }
  403. static void sec_set_dbg_bd_cfg(struct sec_dev_info *info, u32 cfg)
  404. {
  405. void __iomem *addr = info->regs[SEC_SAA] + SEC_DEBUG_BD_CFG_REG;
  406. u32 regval;
  407. regval = readl_relaxed(addr);
  408. /* Always disable write back of normal bd */
  409. regval &= ~SEC_DEBUG_BD_CFG_WB_NORMAL;
  410. if (cfg)
  411. regval &= ~SEC_DEBUG_BD_CFG_WB_EN;
  412. else
  413. regval |= SEC_DEBUG_BD_CFG_WB_EN;
  414. writel_relaxed(regval, addr);
  415. }
  416. static void sec_saa_getqm_en(struct sec_dev_info *info, u32 saa_indx, u32 en)
  417. {
  418. void __iomem *addr = info->regs[SEC_SAA] + SEC_SAA_BASE +
  419. SEC_SAA_CTRL_REG(saa_indx);
  420. u32 regval;
  421. regval = readl_relaxed(addr);
  422. if (en)
  423. regval |= SEC_SAA_CTRL_GET_QM_EN;
  424. else
  425. regval &= ~SEC_SAA_CTRL_GET_QM_EN;
  426. writel_relaxed(regval, addr);
  427. }
  428. static void sec_saa_int_mask(struct sec_dev_info *info, u32 saa_indx,
  429. u32 saa_int_mask)
  430. {
  431. writel_relaxed(saa_int_mask,
  432. info->regs[SEC_SAA] + SEC_SAA_BASE + SEC_ST_INTMSK1_REG +
  433. saa_indx * SEC_SAA_ADDR_SIZE);
  434. }
  435. static void sec_streamid(struct sec_dev_info *info, int i)
  436. {
  437. #define SEC_SID 0x600
  438. #define SEC_VMID 0
  439. writel_relaxed((SEC_VMID | ((SEC_SID & 0xffff) << 8)),
  440. info->regs[SEC_SAA] + SEC_Q_VMID_CFG_REG(i));
  441. }
  442. static void sec_queue_ar_alloc(struct sec_queue *queue, u32 alloc)
  443. {
  444. void __iomem *addr = queue->regs + SEC_Q_ARUSER_CFG_REG;
  445. u32 regval;
  446. regval = readl_relaxed(addr);
  447. if (alloc == SEC_QUEUE_AR_FROCE_ALLOC) {
  448. regval |= SEC_Q_ARUSER_CFG_FA;
  449. regval &= ~SEC_Q_ARUSER_CFG_FNA;
  450. } else {
  451. regval &= ~SEC_Q_ARUSER_CFG_FA;
  452. regval |= SEC_Q_ARUSER_CFG_FNA;
  453. }
  454. writel_relaxed(regval, addr);
  455. }
  456. static void sec_queue_aw_alloc(struct sec_queue *queue, u32 alloc)
  457. {
  458. void __iomem *addr = queue->regs + SEC_Q_AWUSER_CFG_REG;
  459. u32 regval;
  460. regval = readl_relaxed(addr);
  461. if (alloc == SEC_QUEUE_AW_FROCE_ALLOC) {
  462. regval |= SEC_Q_AWUSER_CFG_FA;
  463. regval &= ~SEC_Q_AWUSER_CFG_FNA;
  464. } else {
  465. regval &= ~SEC_Q_AWUSER_CFG_FA;
  466. regval |= SEC_Q_AWUSER_CFG_FNA;
  467. }
  468. writel_relaxed(regval, addr);
  469. }
  470. static void sec_queue_reorder(struct sec_queue *queue, bool reorder)
  471. {
  472. void __iomem *base = queue->regs;
  473. u32 regval;
  474. regval = readl_relaxed(base + SEC_Q_CFG_REG);
  475. if (reorder)
  476. regval |= SEC_Q_CFG_REORDER;
  477. else
  478. regval &= ~SEC_Q_CFG_REORDER;
  479. writel_relaxed(regval, base + SEC_Q_CFG_REG);
  480. }
  481. static void sec_queue_depth(struct sec_queue *queue, u32 depth)
  482. {
  483. void __iomem *addr = queue->regs + SEC_Q_DEPTH_CFG_REG;
  484. u32 regval;
  485. regval = readl_relaxed(addr);
  486. regval &= ~SEC_Q_DEPTH_CFG_DEPTH_M;
  487. regval |= (depth << SEC_Q_DEPTH_CFG_DEPTH_S) & SEC_Q_DEPTH_CFG_DEPTH_M;
  488. writel_relaxed(regval, addr);
  489. }
  490. static void sec_queue_cmdbase_addr(struct sec_queue *queue, u64 addr)
  491. {
  492. writel_relaxed(upper_32_bits(addr), queue->regs + SEC_Q_BASE_HADDR_REG);
  493. writel_relaxed(lower_32_bits(addr), queue->regs + SEC_Q_BASE_LADDR_REG);
  494. }
  495. static void sec_queue_outorder_addr(struct sec_queue *queue, u64 addr)
  496. {
  497. writel_relaxed(upper_32_bits(addr),
  498. queue->regs + SEC_Q_OUTORDER_BASE_HADDR_REG);
  499. writel_relaxed(lower_32_bits(addr),
  500. queue->regs + SEC_Q_OUTORDER_BASE_LADDR_REG);
  501. }
  502. static void sec_queue_errbase_addr(struct sec_queue *queue, u64 addr)
  503. {
  504. writel_relaxed(upper_32_bits(addr),
  505. queue->regs + SEC_Q_ERR_BASE_HADDR_REG);
  506. writel_relaxed(lower_32_bits(addr),
  507. queue->regs + SEC_Q_ERR_BASE_LADDR_REG);
  508. }
  509. static void sec_queue_irq_disable(struct sec_queue *queue)
  510. {
  511. writel_relaxed((u32)~0, queue->regs + SEC_Q_FLOW_INT_MKS_REG);
  512. }
  513. static void sec_queue_irq_enable(struct sec_queue *queue)
  514. {
  515. writel_relaxed(0, queue->regs + SEC_Q_FLOW_INT_MKS_REG);
  516. }
  517. static void sec_queue_abn_irq_disable(struct sec_queue *queue)
  518. {
  519. writel_relaxed((u32)~0, queue->regs + SEC_Q_FAIL_INT_MSK_REG);
  520. }
  521. static void sec_queue_stop(struct sec_queue *queue)
  522. {
  523. disable_irq(queue->task_irq);
  524. sec_queue_irq_disable(queue);
  525. writel_relaxed(0x0, queue->regs + SEC_QUEUE_ENB_REG);
  526. }
  527. static void sec_queue_start(struct sec_queue *queue)
  528. {
  529. sec_queue_irq_enable(queue);
  530. enable_irq(queue->task_irq);
  531. queue->expected = 0;
  532. writel_relaxed(SEC_Q_INIT_AND_STAT_CLEAR, queue->regs + SEC_Q_INIT_REG);
  533. writel_relaxed(0x1, queue->regs + SEC_QUEUE_ENB_REG);
  534. }
  535. static struct sec_queue *sec_alloc_queue(struct sec_dev_info *info)
  536. {
  537. int i;
  538. mutex_lock(&info->dev_lock);
  539. /* Get the first idle queue in SEC device */
  540. for (i = 0; i < SEC_Q_NUM; i++)
  541. if (!info->queues[i].in_use) {
  542. info->queues[i].in_use = true;
  543. info->queues_in_use++;
  544. mutex_unlock(&info->dev_lock);
  545. return &info->queues[i];
  546. }
  547. mutex_unlock(&info->dev_lock);
  548. return ERR_PTR(-ENODEV);
  549. }
  550. static int sec_queue_free(struct sec_queue *queue)
  551. {
  552. struct sec_dev_info *info = queue->dev_info;
  553. if (queue->queue_id >= SEC_Q_NUM) {
  554. dev_err(info->dev, "No queue %d\n", queue->queue_id);
  555. return -ENODEV;
  556. }
  557. if (!queue->in_use) {
  558. dev_err(info->dev, "Queue %d is idle\n", queue->queue_id);
  559. return -ENODEV;
  560. }
  561. mutex_lock(&info->dev_lock);
  562. queue->in_use = false;
  563. info->queues_in_use--;
  564. mutex_unlock(&info->dev_lock);
  565. return 0;
  566. }
  567. static irqreturn_t sec_isr_handle_th(int irq, void *q)
  568. {
  569. sec_queue_irq_disable(q);
  570. return IRQ_WAKE_THREAD;
  571. }
  572. static irqreturn_t sec_isr_handle(int irq, void *q)
  573. {
  574. struct sec_queue *queue = q;
  575. struct sec_queue_ring_cmd *msg_ring = &queue->ring_cmd;
  576. struct sec_queue_ring_cq *cq_ring = &queue->ring_cq;
  577. struct sec_out_bd_info *outorder_msg;
  578. struct sec_bd_info *msg;
  579. u32 ooo_read, ooo_write;
  580. void __iomem *base = queue->regs;
  581. int q_id;
  582. ooo_read = readl(base + SEC_Q_OUTORDER_RD_PTR_REG);
  583. ooo_write = readl(base + SEC_Q_OUTORDER_WR_PTR_REG);
  584. outorder_msg = cq_ring->vaddr + ooo_read;
  585. q_id = outorder_msg->data & SEC_OUT_BD_INFO_Q_ID_M;
  586. msg = msg_ring->vaddr + q_id;
  587. while ((ooo_write != ooo_read) && msg->w0 & SEC_BD_W0_DONE) {
  588. /*
  589. * Must be before callback otherwise blocks adding other chained
  590. * elements
  591. */
  592. set_bit(q_id, queue->unprocessed);
  593. if (q_id == queue->expected)
  594. while (test_bit(queue->expected, queue->unprocessed)) {
  595. clear_bit(queue->expected, queue->unprocessed);
  596. msg = msg_ring->vaddr + queue->expected;
  597. msg->w0 &= ~SEC_BD_W0_DONE;
  598. msg_ring->callback(msg,
  599. queue->shadow[queue->expected]);
  600. queue->shadow[queue->expected] = NULL;
  601. queue->expected = (queue->expected + 1) %
  602. SEC_QUEUE_LEN;
  603. atomic_dec(&msg_ring->used);
  604. }
  605. ooo_read = (ooo_read + 1) % SEC_QUEUE_LEN;
  606. writel(ooo_read, base + SEC_Q_OUTORDER_RD_PTR_REG);
  607. ooo_write = readl(base + SEC_Q_OUTORDER_WR_PTR_REG);
  608. outorder_msg = cq_ring->vaddr + ooo_read;
  609. q_id = outorder_msg->data & SEC_OUT_BD_INFO_Q_ID_M;
  610. msg = msg_ring->vaddr + q_id;
  611. }
  612. sec_queue_irq_enable(queue);
  613. return IRQ_HANDLED;
  614. }
  615. static int sec_queue_irq_init(struct sec_queue *queue)
  616. {
  617. struct sec_dev_info *info = queue->dev_info;
  618. int irq = queue->task_irq;
  619. int ret;
  620. ret = request_threaded_irq(irq, sec_isr_handle_th, sec_isr_handle,
  621. IRQF_TRIGGER_RISING, queue->name, queue);
  622. if (ret) {
  623. dev_err(info->dev, "request irq(%d) failed %d\n", irq, ret);
  624. return ret;
  625. }
  626. disable_irq(irq);
  627. return 0;
  628. }
  629. static int sec_queue_irq_uninit(struct sec_queue *queue)
  630. {
  631. free_irq(queue->task_irq, queue);
  632. return 0;
  633. }
  634. static struct sec_dev_info *sec_device_get(void)
  635. {
  636. struct sec_dev_info *sec_dev = NULL;
  637. struct sec_dev_info *this_sec_dev;
  638. int least_busy_n = SEC_Q_NUM + 1;
  639. int i;
  640. /* Find which one is least busy and use that first */
  641. for (i = 0; i < SEC_MAX_DEVICES; i++) {
  642. this_sec_dev = sec_devices[i];
  643. if (this_sec_dev &&
  644. this_sec_dev->queues_in_use < least_busy_n) {
  645. least_busy_n = this_sec_dev->queues_in_use;
  646. sec_dev = this_sec_dev;
  647. }
  648. }
  649. return sec_dev;
  650. }
  651. static struct sec_queue *sec_queue_alloc_start(struct sec_dev_info *info)
  652. {
  653. struct sec_queue *queue;
  654. queue = sec_alloc_queue(info);
  655. if (IS_ERR(queue)) {
  656. dev_err(info->dev, "alloc sec queue failed! %ld\n",
  657. PTR_ERR(queue));
  658. return queue;
  659. }
  660. sec_queue_start(queue);
  661. return queue;
  662. }
  663. /**
  664. * sec_queue_alloc_start_safe - get a hw queue from appropriate instance
  665. *
  666. * This function does extremely simplistic load balancing. It does not take into
  667. * account NUMA locality of the accelerator, or which cpu has requested the
  668. * queue. Future work may focus on optimizing this in order to improve full
  669. * machine throughput.
  670. */
  671. struct sec_queue *sec_queue_alloc_start_safe(void)
  672. {
  673. struct sec_dev_info *info;
  674. struct sec_queue *queue = ERR_PTR(-ENODEV);
  675. mutex_lock(&sec_id_lock);
  676. info = sec_device_get();
  677. if (!info)
  678. goto unlock;
  679. queue = sec_queue_alloc_start(info);
  680. unlock:
  681. mutex_unlock(&sec_id_lock);
  682. return queue;
  683. }
  684. /**
  685. * sec_queue_stop_release() - free up a hw queue for reuse
  686. * @queue: The queue we are done with.
  687. *
  688. * This will stop the current queue, terminanting any transactions
  689. * that are inflight an return it to the pool of available hw queuess
  690. */
  691. int sec_queue_stop_release(struct sec_queue *queue)
  692. {
  693. struct device *dev = queue->dev_info->dev;
  694. int ret;
  695. sec_queue_stop(queue);
  696. ret = sec_queue_free(queue);
  697. if (ret)
  698. dev_err(dev, "Releasing queue failed %d\n", ret);
  699. return ret;
  700. }
  701. /**
  702. * sec_queue_empty() - Is this hardware queue currently empty.
  703. *
  704. * We need to know if we have an empty queue for some of the chaining modes
  705. * as if it is not empty we may need to hold the message in a software queue
  706. * until the hw queue is drained.
  707. */
  708. bool sec_queue_empty(struct sec_queue *queue)
  709. {
  710. struct sec_queue_ring_cmd *msg_ring = &queue->ring_cmd;
  711. return !atomic_read(&msg_ring->used);
  712. }
  713. /**
  714. * sec_queue_send() - queue up a single operation in the hw queue
  715. * @queue: The queue in which to put the message
  716. * @msg: The message
  717. * @ctx: Context to be put in the shadow array and passed back to cb on result.
  718. *
  719. * This function will return -EAGAIN if the queue is currently full.
  720. */
  721. int sec_queue_send(struct sec_queue *queue, struct sec_bd_info *msg, void *ctx)
  722. {
  723. struct sec_queue_ring_cmd *msg_ring = &queue->ring_cmd;
  724. void __iomem *base = queue->regs;
  725. u32 write, read;
  726. mutex_lock(&msg_ring->lock);
  727. read = readl(base + SEC_Q_RD_PTR_REG);
  728. write = readl(base + SEC_Q_WR_PTR_REG);
  729. if (write == read && atomic_read(&msg_ring->used) == SEC_QUEUE_LEN) {
  730. mutex_unlock(&msg_ring->lock);
  731. return -EAGAIN;
  732. }
  733. memcpy(msg_ring->vaddr + write, msg, sizeof(*msg));
  734. queue->shadow[write] = ctx;
  735. write = (write + 1) % SEC_QUEUE_LEN;
  736. /* Ensure content updated before queue advance */
  737. wmb();
  738. writel(write, base + SEC_Q_WR_PTR_REG);
  739. atomic_inc(&msg_ring->used);
  740. mutex_unlock(&msg_ring->lock);
  741. return 0;
  742. }
  743. bool sec_queue_can_enqueue(struct sec_queue *queue, int num)
  744. {
  745. struct sec_queue_ring_cmd *msg_ring = &queue->ring_cmd;
  746. return SEC_QUEUE_LEN - atomic_read(&msg_ring->used) >= num;
  747. }
  748. static void sec_queue_hw_init(struct sec_queue *queue)
  749. {
  750. sec_queue_ar_alloc(queue, SEC_QUEUE_AR_FROCE_NOALLOC);
  751. sec_queue_aw_alloc(queue, SEC_QUEUE_AR_FROCE_NOALLOC);
  752. sec_queue_ar_pkgattr(queue, 1);
  753. sec_queue_aw_pkgattr(queue, 1);
  754. /* Enable out of order queue */
  755. sec_queue_reorder(queue, true);
  756. /* Interrupt after a single complete element */
  757. writel_relaxed(1, queue->regs + SEC_Q_PROC_NUM_CFG_REG);
  758. sec_queue_depth(queue, SEC_QUEUE_LEN - 1);
  759. sec_queue_cmdbase_addr(queue, queue->ring_cmd.paddr);
  760. sec_queue_outorder_addr(queue, queue->ring_cq.paddr);
  761. sec_queue_errbase_addr(queue, queue->ring_db.paddr);
  762. writel_relaxed(0x100, queue->regs + SEC_Q_OT_TH_REG);
  763. sec_queue_abn_irq_disable(queue);
  764. sec_queue_irq_disable(queue);
  765. writel_relaxed(SEC_Q_INIT_AND_STAT_CLEAR, queue->regs + SEC_Q_INIT_REG);
  766. }
  767. static int sec_hw_init(struct sec_dev_info *info)
  768. {
  769. struct iommu_domain *domain;
  770. u32 sec_ipv4_mask = 0;
  771. u32 sec_ipv6_mask[10] = {};
  772. u32 i, ret;
  773. domain = iommu_get_domain_for_dev(info->dev);
  774. /*
  775. * Enable all available processing unit clocks.
  776. * Only the first cluster is usable with translations.
  777. */
  778. if (domain && (domain->type & __IOMMU_DOMAIN_PAGING))
  779. info->num_saas = 5;
  780. else
  781. info->num_saas = 10;
  782. writel_relaxed(GENMASK(info->num_saas - 1, 0),
  783. info->regs[SEC_SAA] + SEC_CLK_EN_REG);
  784. /* 32 bit little endian */
  785. sec_bd_endian_little(info);
  786. sec_cache_config(info);
  787. /* Data axi port write and read outstanding config as per datasheet */
  788. sec_data_axiwr_otsd_cfg(info, 0x7);
  789. sec_data_axird_otsd_cfg(info, 0x7);
  790. /* Enable clock gating */
  791. sec_clk_gate_en(info, true);
  792. /* Set CNT_CYC register not read clear */
  793. sec_comm_cnt_cfg(info, false);
  794. /* Enable CNT_CYC */
  795. sec_commsnap_en(info, false);
  796. writel_relaxed((u32)~0, info->regs[SEC_SAA] + SEC_FSM_MAX_CNT_REG);
  797. ret = sec_ipv4_hashmask(info, sec_ipv4_mask);
  798. if (ret) {
  799. dev_err(info->dev, "Failed to set ipv4 hashmask %d\n", ret);
  800. return -EIO;
  801. }
  802. sec_ipv6_hashmask(info, sec_ipv6_mask);
  803. /* do not use debug bd */
  804. sec_set_dbg_bd_cfg(info, 0);
  805. if (domain && (domain->type & __IOMMU_DOMAIN_PAGING)) {
  806. for (i = 0; i < SEC_Q_NUM; i++) {
  807. sec_streamid(info, i);
  808. /* Same QoS for all queues */
  809. writel_relaxed(0x3f,
  810. info->regs[SEC_SAA] +
  811. SEC_Q_WEIGHT_CFG_REG(i));
  812. }
  813. }
  814. for (i = 0; i < info->num_saas; i++) {
  815. sec_saa_getqm_en(info, i, 1);
  816. sec_saa_int_mask(info, i, 0);
  817. }
  818. return 0;
  819. }
  820. static void sec_hw_exit(struct sec_dev_info *info)
  821. {
  822. int i;
  823. for (i = 0; i < SEC_MAX_SAA_NUM; i++) {
  824. sec_saa_int_mask(info, i, (u32)~0);
  825. sec_saa_getqm_en(info, i, 0);
  826. }
  827. }
  828. static void sec_queue_base_init(struct sec_dev_info *info,
  829. struct sec_queue *queue, int queue_id)
  830. {
  831. queue->dev_info = info;
  832. queue->queue_id = queue_id;
  833. snprintf(queue->name, sizeof(queue->name),
  834. "%s_%d", dev_name(info->dev), queue->queue_id);
  835. }
  836. static int sec_map_io(struct sec_dev_info *info, struct platform_device *pdev)
  837. {
  838. struct resource *res;
  839. int i;
  840. for (i = 0; i < SEC_NUM_ADDR_REGIONS; i++) {
  841. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  842. if (!res) {
  843. dev_err(info->dev, "Memory resource %d not found\n", i);
  844. return -EINVAL;
  845. }
  846. info->regs[i] = devm_ioremap(info->dev, res->start,
  847. resource_size(res));
  848. if (!info->regs[i]) {
  849. dev_err(info->dev,
  850. "Memory resource %d could not be remapped\n",
  851. i);
  852. return -EINVAL;
  853. }
  854. }
  855. return 0;
  856. }
  857. static int sec_base_init(struct sec_dev_info *info,
  858. struct platform_device *pdev)
  859. {
  860. int ret;
  861. ret = sec_map_io(info, pdev);
  862. if (ret)
  863. return ret;
  864. ret = sec_clk_en(info);
  865. if (ret)
  866. return ret;
  867. ret = sec_reset_whole_module(info);
  868. if (ret)
  869. goto sec_clk_disable;
  870. ret = sec_hw_init(info);
  871. if (ret)
  872. goto sec_clk_disable;
  873. return 0;
  874. sec_clk_disable:
  875. sec_clk_dis(info);
  876. return ret;
  877. }
  878. static void sec_base_exit(struct sec_dev_info *info)
  879. {
  880. sec_hw_exit(info);
  881. sec_clk_dis(info);
  882. }
  883. #define SEC_Q_CMD_SIZE \
  884. round_up(SEC_QUEUE_LEN * sizeof(struct sec_bd_info), PAGE_SIZE)
  885. #define SEC_Q_CQ_SIZE \
  886. round_up(SEC_QUEUE_LEN * sizeof(struct sec_out_bd_info), PAGE_SIZE)
  887. #define SEC_Q_DB_SIZE \
  888. round_up(SEC_QUEUE_LEN * sizeof(struct sec_debug_bd_info), PAGE_SIZE)
  889. static int sec_queue_res_cfg(struct sec_queue *queue)
  890. {
  891. struct device *dev = queue->dev_info->dev;
  892. struct sec_queue_ring_cmd *ring_cmd = &queue->ring_cmd;
  893. struct sec_queue_ring_cq *ring_cq = &queue->ring_cq;
  894. struct sec_queue_ring_db *ring_db = &queue->ring_db;
  895. int ret;
  896. ring_cmd->vaddr = dma_zalloc_coherent(dev, SEC_Q_CMD_SIZE,
  897. &ring_cmd->paddr,
  898. GFP_KERNEL);
  899. if (!ring_cmd->vaddr)
  900. return -ENOMEM;
  901. atomic_set(&ring_cmd->used, 0);
  902. mutex_init(&ring_cmd->lock);
  903. ring_cmd->callback = sec_alg_callback;
  904. ring_cq->vaddr = dma_zalloc_coherent(dev, SEC_Q_CQ_SIZE,
  905. &ring_cq->paddr,
  906. GFP_KERNEL);
  907. if (!ring_cq->vaddr) {
  908. ret = -ENOMEM;
  909. goto err_free_ring_cmd;
  910. }
  911. ring_db->vaddr = dma_zalloc_coherent(dev, SEC_Q_DB_SIZE,
  912. &ring_db->paddr,
  913. GFP_KERNEL);
  914. if (!ring_db->vaddr) {
  915. ret = -ENOMEM;
  916. goto err_free_ring_cq;
  917. }
  918. queue->task_irq = platform_get_irq(to_platform_device(dev),
  919. queue->queue_id * 2 + 1);
  920. if (queue->task_irq <= 0) {
  921. ret = -EINVAL;
  922. goto err_free_ring_db;
  923. }
  924. return 0;
  925. err_free_ring_db:
  926. dma_free_coherent(dev, SEC_Q_DB_SIZE, queue->ring_db.vaddr,
  927. queue->ring_db.paddr);
  928. err_free_ring_cq:
  929. dma_free_coherent(dev, SEC_Q_CQ_SIZE, queue->ring_cq.vaddr,
  930. queue->ring_cq.paddr);
  931. err_free_ring_cmd:
  932. dma_free_coherent(dev, SEC_Q_CMD_SIZE, queue->ring_cmd.vaddr,
  933. queue->ring_cmd.paddr);
  934. return ret;
  935. }
  936. static void sec_queue_free_ring_pages(struct sec_queue *queue)
  937. {
  938. struct device *dev = queue->dev_info->dev;
  939. dma_free_coherent(dev, SEC_Q_DB_SIZE, queue->ring_db.vaddr,
  940. queue->ring_db.paddr);
  941. dma_free_coherent(dev, SEC_Q_CQ_SIZE, queue->ring_cq.vaddr,
  942. queue->ring_cq.paddr);
  943. dma_free_coherent(dev, SEC_Q_CMD_SIZE, queue->ring_cmd.vaddr,
  944. queue->ring_cmd.paddr);
  945. }
  946. static int sec_queue_config(struct sec_dev_info *info, struct sec_queue *queue,
  947. int queue_id)
  948. {
  949. int ret;
  950. sec_queue_base_init(info, queue, queue_id);
  951. ret = sec_queue_res_cfg(queue);
  952. if (ret)
  953. return ret;
  954. ret = sec_queue_map_io(queue);
  955. if (ret) {
  956. dev_err(info->dev, "Queue map failed %d\n", ret);
  957. sec_queue_free_ring_pages(queue);
  958. return ret;
  959. }
  960. sec_queue_hw_init(queue);
  961. return 0;
  962. }
  963. static void sec_queue_unconfig(struct sec_dev_info *info,
  964. struct sec_queue *queue)
  965. {
  966. sec_queue_unmap_io(queue);
  967. sec_queue_free_ring_pages(queue);
  968. }
  969. static int sec_id_alloc(struct sec_dev_info *info)
  970. {
  971. int ret = 0;
  972. int i;
  973. mutex_lock(&sec_id_lock);
  974. for (i = 0; i < SEC_MAX_DEVICES; i++)
  975. if (!sec_devices[i])
  976. break;
  977. if (i == SEC_MAX_DEVICES) {
  978. ret = -ENOMEM;
  979. goto unlock;
  980. }
  981. info->sec_id = i;
  982. sec_devices[info->sec_id] = info;
  983. unlock:
  984. mutex_unlock(&sec_id_lock);
  985. return ret;
  986. }
  987. static void sec_id_free(struct sec_dev_info *info)
  988. {
  989. mutex_lock(&sec_id_lock);
  990. sec_devices[info->sec_id] = NULL;
  991. mutex_unlock(&sec_id_lock);
  992. }
  993. static int sec_probe(struct platform_device *pdev)
  994. {
  995. struct sec_dev_info *info;
  996. struct device *dev = &pdev->dev;
  997. int i, j;
  998. int ret;
  999. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  1000. if (ret) {
  1001. dev_err(dev, "Failed to set 64 bit dma mask %d", ret);
  1002. return -ENODEV;
  1003. }
  1004. info = devm_kzalloc(dev, (sizeof(*info)), GFP_KERNEL);
  1005. if (!info)
  1006. return -ENOMEM;
  1007. info->dev = dev;
  1008. mutex_init(&info->dev_lock);
  1009. info->hw_sgl_pool = dmam_pool_create("sgl", dev,
  1010. sizeof(struct sec_hw_sgl), 64, 0);
  1011. if (!info->hw_sgl_pool) {
  1012. dev_err(dev, "Failed to create sec sgl dma pool\n");
  1013. return -ENOMEM;
  1014. }
  1015. ret = sec_base_init(info, pdev);
  1016. if (ret) {
  1017. dev_err(dev, "Base initialization fail! %d\n", ret);
  1018. return ret;
  1019. }
  1020. for (i = 0; i < SEC_Q_NUM; i++) {
  1021. ret = sec_queue_config(info, &info->queues[i], i);
  1022. if (ret)
  1023. goto queues_unconfig;
  1024. ret = sec_queue_irq_init(&info->queues[i]);
  1025. if (ret) {
  1026. sec_queue_unconfig(info, &info->queues[i]);
  1027. goto queues_unconfig;
  1028. }
  1029. }
  1030. ret = sec_algs_register();
  1031. if (ret) {
  1032. dev_err(dev, "Failed to register algorithms with crypto %d\n",
  1033. ret);
  1034. goto queues_unconfig;
  1035. }
  1036. platform_set_drvdata(pdev, info);
  1037. ret = sec_id_alloc(info);
  1038. if (ret)
  1039. goto algs_unregister;
  1040. return 0;
  1041. algs_unregister:
  1042. sec_algs_unregister();
  1043. queues_unconfig:
  1044. for (j = i - 1; j >= 0; j--) {
  1045. sec_queue_irq_uninit(&info->queues[j]);
  1046. sec_queue_unconfig(info, &info->queues[j]);
  1047. }
  1048. sec_base_exit(info);
  1049. return ret;
  1050. }
  1051. static int sec_remove(struct platform_device *pdev)
  1052. {
  1053. struct sec_dev_info *info = platform_get_drvdata(pdev);
  1054. int i;
  1055. /* Unexpose as soon as possible, reuse during remove is fine */
  1056. sec_id_free(info);
  1057. sec_algs_unregister();
  1058. for (i = 0; i < SEC_Q_NUM; i++) {
  1059. sec_queue_irq_uninit(&info->queues[i]);
  1060. sec_queue_unconfig(info, &info->queues[i]);
  1061. }
  1062. sec_base_exit(info);
  1063. return 0;
  1064. }
  1065. static const __maybe_unused struct of_device_id sec_match[] = {
  1066. { .compatible = "hisilicon,hip06-sec" },
  1067. { .compatible = "hisilicon,hip07-sec" },
  1068. {}
  1069. };
  1070. MODULE_DEVICE_TABLE(of, sec_match);
  1071. static const __maybe_unused struct acpi_device_id sec_acpi_match[] = {
  1072. { "HISI02C1", 0 },
  1073. { }
  1074. };
  1075. MODULE_DEVICE_TABLE(acpi, sec_acpi_match);
  1076. static struct platform_driver sec_driver = {
  1077. .probe = sec_probe,
  1078. .remove = sec_remove,
  1079. .driver = {
  1080. .name = "hisi_sec_platform_driver",
  1081. .of_match_table = sec_match,
  1082. .acpi_match_table = ACPI_PTR(sec_acpi_match),
  1083. },
  1084. };
  1085. module_platform_driver(sec_driver);
  1086. MODULE_LICENSE("GPL");
  1087. MODULE_DESCRIPTION("Hisilicon Security Accelerators");
  1088. MODULE_AUTHOR("Zaibo Xu <xuzaibo@huawei.com");
  1089. MODULE_AUTHOR("Jonathan Cameron <jonathan.cameron@huawei.com>");