GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / staging / ccree / ssi_fips.c
1 /*
2  * Copyright (C) 2012-2017 ARM Limited or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/fips.h>
19
20 #include "ssi_config.h"
21 #include "ssi_driver.h"
22 #include "cc_hal.h"
23 #include "ssi_fips.h"
24
25 static void fips_dsr(unsigned long devarg);
26
27 struct ssi_fips_handle {
28         struct tasklet_struct tasklet;
29 };
30
31 /* The function called once at driver entry point to check
32  * whether TEE FIPS error occurred.
33  */
34 static bool cc_get_tee_fips_status(struct ssi_drvdata *drvdata)
35 {
36         u32 reg;
37         void __iomem *cc_base = drvdata->cc_base;
38
39         reg = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, GPR_HOST));
40         return (reg == (CC_FIPS_SYNC_TEE_STATUS | CC_FIPS_SYNC_MODULE_OK));
41 }
42
43 /*
44  * This function should push the FIPS REE library status towards the TEE library
45  * by writing the error state to HOST_GPR0 register.
46  */
47 void cc_set_ree_fips_status(struct ssi_drvdata *drvdata, bool status)
48 {
49         void __iomem *cc_base = drvdata->cc_base;
50         int val = CC_FIPS_SYNC_REE_STATUS;
51
52         val |= (status ? CC_FIPS_SYNC_MODULE_OK : CC_FIPS_SYNC_MODULE_ERROR);
53
54         CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_GPR0), val);
55 }
56
57 void ssi_fips_fini(struct ssi_drvdata *drvdata)
58 {
59         struct ssi_fips_handle *fips_h = drvdata->fips_handle;
60
61         if (!fips_h)
62                 return; /* Not allocated */
63
64         /* Kill tasklet */
65         tasklet_kill(&fips_h->tasklet);
66
67         kfree(fips_h);
68         drvdata->fips_handle = NULL;
69 }
70
71 void fips_handler(struct ssi_drvdata *drvdata)
72 {
73         struct ssi_fips_handle *fips_handle_ptr =
74                 drvdata->fips_handle;
75
76         tasklet_schedule(&fips_handle_ptr->tasklet);
77 }
78
79 static inline void tee_fips_error(void)
80 {
81         if (fips_enabled)
82                 panic("ccree: TEE reported cryptographic error in fips mode!\n");
83         else
84                 SSI_LOG_ERR("TEE reported error!\n");
85 }
86
87 /* Deferred service handler, run as interrupt-fired tasklet */
88 static void fips_dsr(unsigned long devarg)
89 {
90         struct ssi_drvdata *drvdata = (struct ssi_drvdata *)devarg;
91         void __iomem *cc_base = drvdata->cc_base;
92         u32 irq, state, val;
93
94         irq = (drvdata->irq & (SSI_GPR0_IRQ_MASK));
95
96         if (irq) {
97                 state = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, GPR_HOST));
98
99                 if (state != (CC_FIPS_SYNC_TEE_STATUS | CC_FIPS_SYNC_MODULE_OK))
100                         tee_fips_error();
101         }
102
103         /* after verifing that there is nothing to do,
104          * unmask AXI completion interrupt.
105          */
106         val = (CC_REG_OFFSET(HOST_RGF, HOST_IMR) & ~irq);
107         CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_IMR), val);
108 }
109
110 /* The function called once at driver entry point .*/
111 int ssi_fips_init(struct ssi_drvdata *p_drvdata)
112 {
113         struct ssi_fips_handle *fips_h;
114
115         fips_h = kzalloc(sizeof(*fips_h), GFP_KERNEL);
116         if (!fips_h)
117                 return -ENOMEM;
118
119         p_drvdata->fips_handle = fips_h;
120
121         SSI_LOG_DEBUG("Initializing fips tasklet\n");
122         tasklet_init(&fips_h->tasklet, fips_dsr, (unsigned long)p_drvdata);
123
124         if (!cc_get_tee_fips_status(p_drvdata))
125                 tee_fips_error();
126
127         return 0;
128 }