1 /*
2 * Copyright 2017-2021 NXP
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #ifndef _SEC_HW_SPECIFIC_H_
9 #define _SEC_HW_SPECIFIC_H_
10
11 #include "caam.h"
12 #include "sec_jr_driver.h"
13
14 /* DEFINES AND MACROS */
15
16 /* Used to retry resetting a job ring in SEC hardware. */
17 #define SEC_TIMEOUT 100000
18
19 /*
20 * Offset to the registers of a job ring.
21 *Is different for each job ring.
22 */
23 #define CHAN_BASE(jr) ((phys_addr_t)(jr)->register_base_addr)
24
25 #define unlikely(x) __builtin_expect(!!(x), 0)
26
27 #define SEC_JOB_RING_IS_FULL(pi, ci, ring_max_size, ring_threshold) \
28 ((((pi) + 1 + ((ring_max_size) - (ring_threshold))) & \
29 (ring_max_size - 1)) == ((ci)))
30
31 #define SEC_CIRCULAR_COUNTER(x, max) (((x) + 1) & (max - 1))
32
33 /* Struct representing various job ring registers */
34 struct jobring_regs {
35 #ifdef NXP_SEC_BE
36 unsigned int irba_h;
37 unsigned int irba_l;
38 #else
39 unsigned int irba_l;
40 unsigned int irba_h;
41 #endif
42 unsigned int rsvd1;
43 unsigned int irs;
44 unsigned int rsvd2;
45 unsigned int irsa;
46 unsigned int rsvd3;
47 unsigned int irja;
48 #ifdef NXP_SEC_BE
49 unsigned int orba_h;
50 unsigned int orba_l;
51 #else
52 unsigned int orba_l;
53 unsigned int orba_h;
54 #endif
55 unsigned int rsvd4;
56 unsigned int ors;
57 unsigned int rsvd5;
58 unsigned int orjr;
59 unsigned int rsvd6;
60 unsigned int orsf;
61 unsigned int rsvd7;
62 unsigned int jrsta;
63 unsigned int rsvd8;
64 unsigned int jrint;
65 unsigned int jrcfg0;
66 unsigned int jrcfg1;
67 unsigned int rsvd9;
68 unsigned int irri;
69 unsigned int rsvd10;
70 unsigned int orwi;
71 unsigned int rsvd11;
72 unsigned int jrcr;
73 };
74
75 /* Offsets representing common SEC Registers */
76 #define SEC_REG_MCFGR_OFFSET 0x0004
77 #define SEC_REG_SCFGR_OFFSET 0x000C
78 #define SEC_REG_JR0ICIDR_MS_OFFSET 0x0010
79 #define SEC_REG_JR0ICIDR_LS_OFFSET 0x0014
80 #define SEC_REG_JR1ICIDR_MS_OFFSET 0x0018
81 #define SEC_REG_JR1ICIDR_LS_OFFSET 0x001C
82 #define SEC_REG_JR2ICIDR_MS_OFFSET 0x0020
83 #define SEC_REG_JR2ICIDR_LS_OFFSET 0x0024
84 #define SEC_REG_JR3ICIDR_MS_OFFSET 0x0028
85 #define SEC_REG_JR3ICIDR_LS_OFFSET 0x002C
86 #define SEC_REG_JRSTARTR_OFFSET 0x005C
87 #define SEC_REG_CTPR_MS_OFFSET 0x0FA8
88
89 /* Offsets representing various RNG registers */
90 #define RNG_REG_RTMCTL_OFFSET 0x0600
91 #define RNG_REG_RTSDCTL_OFFSET 0x0610
92 #define RNG_REG_RTFRQMIN_OFFSET 0x0618
93 #define RNG_REG_RTFRQMAX_OFFSET 0x061C
94 #define RNG_REG_RDSTA_OFFSET 0x06C0
95 #define ALG_AAI_SH_SHIFT 4
96
97 /* SEC Registers Bitmasks */
98 #define MCFGR_PS_SHIFT 16
99 #define MCFGR_AWCACHE_SHIFT 8
100 #define MCFGR_AWCACHE_MASK (0xF << MCFGR_AWCACHE_SHIFT)
101 #define MCFGR_ARCACHE_SHIFT 12
102 #define MCFGR_ARCACHE_MASK (0xF << MCFGR_ARCACHE_SHIFT)
103
104 #define SCFGR_RNGSH0 0x00000200
105 #define SCFGR_VIRT_EN 0x00008000
106
107 #define JRICID_MS_LICID 0x80000000
108 #define JRICID_MS_LAMTD 0x00020000
109 #define JRICID_MS_AMTDT 0x00010000
110 #define JRICID_MS_TZ 0x00008000
111 #define JRICID_LS_SDID_MASK 0x00000FFF
112 #define JRICID_LS_NSEQID_MASK 0x0FFF0000
113 #define JRICID_LS_NSEQID_SHIFT 16
114 #define JRICID_LS_SEQID_MASK 0x00000FFF
115
116 #define JRSTARTR_STARTJR0 0x00000001
117 #define JRSTARTR_STARTJR1 0x00000002
118 #define JRSTARTR_STARTJR2 0x00000004
119 #define JRSTARTR_STARTJR3 0x00000008
120
121 #define CTPR_VIRT_EN_POR 0x00000002
122 #define CTPR_VIRT_EN_INC 0x00000001
123
124 /* RNG RDSTA bitmask */
125 #define RNG_STATE0_HANDLE_INSTANTIATED 0x00000001
126 #define RTMCTL_PRGM 0x00010000 /* 1 -> program mode, 0 -> run mode */
127 /* use von Neumann data in both entropy shifter and statistical checker */
128 #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC 0
129 /* use raw data in both entropy shifter and statistical checker */
130 #define RTMCTL_SAMP_MODE_RAW_ES_SC 1
131 /* use von Neumann data in entropy shifter, raw data in statistical checker */
132 #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_RAW_SC 2
133 /* invalid combination */
134 #define RTMCTL_SAMP_MODE_INVALID 3
135 #define RTSDCTL_ENT_DLY_MIN 3200
136 #define RTSDCTL_ENT_DLY_MAX 12800
137 #define RTSDCTL_ENT_DLY_SHIFT 16
138 #define RTSDCTL_ENT_DLY_MASK (U(0xffff) << RTSDCTL_ENT_DLY_SHIFT)
139 #define RTFRQMAX_DISABLE (1 << 20)
140
141 /* Constants for error handling on job ring */
142 #define JR_REG_JRINT_ERR_TYPE_SHIFT 8
143 #define JR_REG_JRINT_ERR_ORWI_SHIFT 16
144 #define JR_REG_JRINIT_JRE_SHIFT 1
145
146 #define JRINT_JRE (1 << JR_REG_JRINIT_JRE_SHIFT)
147 #define JRINT_ERR_WRITE_STATUS (1 << JR_REG_JRINT_ERR_TYPE_SHIFT)
148 #define JRINT_ERR_BAD_INPUT_BASE (3 << JR_REG_JRINT_ERR_TYPE_SHIFT)
149 #define JRINT_ERR_BAD_OUTPUT_BASE (4 << JR_REG_JRINT_ERR_TYPE_SHIFT)
150 #define JRINT_ERR_WRITE_2_IRBA (5 << JR_REG_JRINT_ERR_TYPE_SHIFT)
151 #define JRINT_ERR_WRITE_2_ORBA (6 << JR_REG_JRINT_ERR_TYPE_SHIFT)
152 #define JRINT_ERR_RES_B4_HALT (7 << JR_REG_JRINT_ERR_TYPE_SHIFT)
153 #define JRINT_ERR_REM_TOO_MANY (8 << JR_REG_JRINT_ERR_TYPE_SHIFT)
154 #define JRINT_ERR_ADD_TOO_MANY (9 << JR_REG_JRINT_ERR_TYPE_SHIFT)
155 #define JRINT_ERR_HALT_MASK 0x0C
156 #define JRINT_ERR_HALT_INPROGRESS 0x04
157 #define JRINT_ERR_HALT_COMPLETE 0x08
158
159 #define JR_REG_JRCR_VAL_RESET 0x00000001
160
161 #define JR_REG_JRCFG_LO_ICTT_SHIFT 0x10
162 #define JR_REG_JRCFG_LO_ICDCT_SHIFT 0x08
163 #define JR_REG_JRCFG_LO_ICEN_EN 0x02
164 #define JR_REG_JRCFG_LO_IMSK_EN 0x01
165
166 /* Constants for Descriptor Processing errors */
167 #define SEC_HW_ERR_SSRC_NO_SRC 0x00
168 #define SEC_HW_ERR_SSRC_CCB_ERR 0x02
169 #define SEC_HW_ERR_SSRC_JMP_HALT_U 0x03
170 #define SEC_HW_ERR_SSRC_DECO 0x04
171 #define SEC_HW_ERR_SSRC_JR 0x06
172 #define SEC_HW_ERR_SSRC_JMP_HALT_COND 0x07
173
174 #define SEC_HW_ERR_DECO_HFN_THRESHOLD 0xF1
175 #define SEC_HW_ERR_CCB_ICV_CHECK_FAIL 0x0A
176
177 /* Macros for extracting error codes for the job ring */
178
179 #define JR_REG_JRINT_ERR_TYPE_EXTRACT(value) \
180 ((value) & 0x00000F00)
181
182 #define JR_REG_JRINT_ERR_ORWI_EXTRACT(value) \
183 (((value) & 0x3FFF0000) >> \
184 JR_REG_JRINT_ERR_ORWI_SHIFT)
185
186 #define JR_REG_JRINT_JRE_EXTRACT(value) \
187 ((value) & JRINT_JRE)
188
189 /* Macros for manipulating JR registers */
190 typedef struct {
191 #ifdef NXP_SEC_BE
192 uint32_t high;
193 uint32_t low;
194 #else
195 uint32_t low;
196 uint32_t high;
197 #endif
198 } ptr_addr_t;
199
200 #if defined(CONFIG_PHYS_64BIT)
201 #define sec_read_addr(a) sec_in64((a))
202 #define sec_write_addr(a, v) sec_out64((a), (v))
203 #else
204 #define sec_read_addr(a) sec_in32((a))
205 #define sec_write_addr(a, v) sec_out32((a), (v))
206 #endif
207
208 #define JR_REG(name, jr) (CHAN_BASE(jr) + JR_REG_##name##_OFFSET)
209 #define JR_REG_LO(name, jr) (CHAN_BASE(jr) + JR_REG_##name##_OFFSET_LO)
210
211 #define GET_JR_REG(name, jr) (sec_in32(JR_REG(name, (jr))))
212 #define GET_JR_REG_LO(name, jr) (sec_in32(JR_REG_LO(name, (jr))))
213
214 #define SET_JR_REG(name, jr, val) \
215 (sec_out32(JR_REG(name, (jr)), (val)))
216
217 #define SET_JR_REG_LO(name, jr, val) \
218 (sec_out32(JR_REG_LO(name, (jr)), (val)))
219
220 /* STRUCTURES AND OTHER TYPEDEFS */
221 /* Lists the possible states for a job ring. */
222 typedef enum sec_job_ring_state_e {
223 SEC_JOB_RING_STATE_STARTED, /* Job ring is initialized */
224 SEC_JOB_RING_STATE_RESET, /* Job ring reset is in progres */
225 } sec_job_ring_state_t;
226
227 struct sec_job_ring_t {
228 /*
229 * Consumer index for job ring (jobs array).
230 * @note: cidx and pidx are accessed from
231 * different threads.
232 * Place the cidx and pidx inside the structure
233 * so that they lay on different cachelines, to
234 * avoid false sharing between threads when the
235 * threads run on different cores!
236 */
237 uint32_t cidx;
238
239 /* Producer index for job ring (jobs array) */
240 uint32_t pidx;
241
242 /* Ring of input descriptors. Size of array is power of 2 to allow
243 * fast update of producer/consumer indexes with bitwise operations.
244 */
245 phys_addr_t *input_ring;
246
247 /* Ring of output descriptors. */
248 struct sec_outring_entry *output_ring;
249
250 /* The file descriptor used for polling for interrupts notifications */
251 uint32_t irq_fd;
252
253 /* Model used by SEC Driver to receive notifications from SEC.
254 * Can be either of the three:
255 * #SEC_NOTIFICATION_TYPE_IRQ or
256 * #SEC_NOTIFICATION_TYPE_POLL
257 */
258 uint32_t jr_mode;
259 /* Base address for SEC's register memory for this job ring. */
260 void *register_base_addr;
261 /* notifies if coelescing is enabled for the job ring */
262 uint8_t coalescing_en;
263 /* The state of this job ring */
264 sec_job_ring_state_t jr_state;
265 };
266
267 /* Forward structure declaration */
268 typedef struct sec_job_ring_t sec_job_ring_t;
269
270 struct sec_outring_entry {
271 phys_addr_t desc; /* Pointer to completed descriptor */
272 uint32_t status; /* Status for completed descriptor */
273 } __packed;
274
275 /* Lists the states possible for the SEC user space driver. */
276 typedef enum sec_driver_state_e {
277 SEC_DRIVER_STATE_IDLE, /*< Driver not initialized */
278 SEC_DRIVER_STATE_STARTED, /*< Driver initialized and */
279 SEC_DRIVER_STATE_RELEASE, /*< Driver release is in progress */
280 } sec_driver_state_t;
281
282 /* Union describing the possible error codes that */
283 /* can be set in the descriptor status word */
284
285 union hw_error_code {
286 uint32_t error;
287 union {
288 struct {
289 uint32_t ssrc:4;
290 uint32_t ssed_val:28;
291 } __packed value;
292 struct {
293 uint32_t ssrc:4;
294 uint32_t res:28;
295 } __packed no_status_src;
296 struct {
297 uint32_t ssrc:4;
298 uint32_t jmp:1;
299 uint32_t res:11;
300 uint32_t desc_idx:8;
301 uint32_t cha_id:4;
302 uint32_t err_id:4;
303 } __packed ccb_status_src;
304 struct {
305 uint32_t ssrc:4;
306 uint32_t jmp:1;
307 uint32_t res:11;
308 uint32_t desc_idx:8;
309 uint32_t offset:8;
310 } __packed jmp_halt_user_src;
311 struct {
312 uint32_t ssrc:4;
313 uint32_t jmp:1;
314 uint32_t res:11;
315 uint32_t desc_idx:8;
316 uint32_t desc_err:8;
317 } __packed deco_src;
318 struct {
319 uint32_t ssrc:4;
320 uint32_t res:17;
321 uint32_t naddr:3;
322 uint32_t desc_err:8;
323 } __packed jr_src;
324 struct {
325 uint32_t ssrc:4;
326 uint32_t jmp:1;
327 uint32_t res:11;
328 uint32_t desc_idx:8;
329 uint32_t cond:8;
330 } __packed jmp_halt_cond_src;
331 } __packed error_desc;
332 } __packed;
333
334 /* FUNCTION PROTOTYPES */
335
336 /*
337 * @brief Initialize a job ring/channel in SEC device.
338 * Write configuration register/s to properly initialize a job ring.
339 *
340 * @param [in] job_ring The job ring
341 *
342 * @retval 0 for success
343 * @retval other for error
344 */
345 int hw_reset_job_ring(sec_job_ring_t *job_ring);
346
347 /*
348 * @brief Reset a job ring/channel in SEC device.
349 * Write configuration register/s to reset a job ring.
350 *
351 * @param [in] job_ring The job ring
352 *
353 * @retval 0 for success
354 * @retval -1 in case job ring reset failed
355 */
356 int hw_shutdown_job_ring(sec_job_ring_t *job_ring);
357
358 /*
359 * @brief Handle a job ring/channel error in SEC device.
360 * Identify the error type and clear error bits if required.
361 *
362 * @param [in] job_ring The job ring
363 * @param [in] sec_error_code error code as first read from SEC engine
364 */
365
366 void hw_handle_job_ring_error(sec_job_ring_t *job_ring,
367 uint32_t sec_error_code);
368 /*
369 * @brief Handle a job ring error in the device.
370 * Identify the error type and printout a explanatory
371 * messages.
372 *
373 * @param [in] job_ring The job ring
374 *
375 */
376
377 int hw_job_ring_error(sec_job_ring_t *job_ring);
378
379 /* @brief Set interrupt coalescing parameters on the Job Ring.
380 * @param [in] job_ring The job ring
381 * @param [in] irq_coalesing_timer
382 * Interrupt coalescing timer threshold.
383 * This value determines the maximum
384 * amount of time after processing a descriptor
385 * before raising an interrupt.
386 * @param [in] irq_coalescing_count
387 * Interrupt coalescing count threshold.
388 * This value determines how many descriptors
389 * are completed before raising an interrupt.
390 */
391
392 int hw_job_ring_set_coalescing_param(sec_job_ring_t *job_ring,
393 uint16_t irq_coalescing_timer,
394 uint8_t irq_coalescing_count);
395
396 /* @brief Enable interrupt coalescing on a job ring
397 * @param [in] job_ring The job ring
398 */
399
400 int hw_job_ring_enable_coalescing(sec_job_ring_t *job_ring);
401
402 /*
403 * @brief Disable interrupt coalescing on a job ring
404 * @param [in] job_ring The job ring
405 */
406
407 int hw_job_ring_disable_coalescing(sec_job_ring_t *job_ring);
408
409 /*
410 * @brief Poll the HW for already processed jobs in the JR
411 * and notify the available jobs to UA.
412 *
413 * @param [in] job_ring The job ring to poll.
414 * @param [in] limit The maximum number of jobs to notify.
415 * If set to negative value, all available
416 * jobs are notified.
417 *
418 * @retval >=0 for No of jobs notified to UA.
419 * @retval -1 for error
420 */
421
422 int hw_poll_job_ring(struct sec_job_ring_t *job_ring, int32_t limit);
423
424 /* @brief Poll the HW for already processed jobs in the JR
425 * and silently discard the available jobs or notify them to UA
426 * with indicated error code.
427
428 * @param [in,out] job_ring The job ring to poll.
429 * @param [in] do_notify Can be #TRUE or #FALSE.
430 * Indicates if descriptors to be discarded
431 * or notified to UA with given error_code.
432 * @param [in] error_code The detailed SEC error code.
433 * @param [out] notified_descs Number of notified descriptors.
434 * Can be NULL if do_notify is #FALSE
435 */
436 void hw_flush_job_ring(struct sec_job_ring_t *job_ring,
437 uint32_t do_notify,
438 uint32_t error_code, uint32_t *notified_descs);
439
440 /*
441 * @brief Flush job rings of any processed descs.
442 * The processed descs are silently dropped,
443 * WITHOUT being notified to UA.
444 */
445 void flush_job_rings(void);
446
447 /*
448 * @brief Handle desc that generated error in SEC engine.
449 * Identify the exact type of error and handle the error.
450 * Depending on the error type, the job ring could be reset.
451 * All descs that are submitted for processing on this job ring
452 * are notified to User Application with error status and detailed error code.
453
454 * @param [in] job_ring Job ring
455 * @param [in] sec_error_code Error code read from job ring's Channel
456 * Status Register
457 * @param [out] notified_descs Number of notified descs. Can be NULL if
458 * do_notify is #FALSE
459 * @param [out] do_driver_shutdown If set to #TRUE, then UA is returned code
460 * #SEC_PROCESSING_ERROR
461 * which is indication that UA must call
462 * sec_release() after this.
463 */
464 void sec_handle_desc_error(struct sec_job_ring_t *job_ring,
465 uint32_t sec_error_code,
466 uint32_t *notified_descs,
467 uint32_t *do_driver_shutdown);
468
469 /*
470 * @brief Release the software and hardware resources tied to a job ring.
471 * @param [in] job_ring The job ring
472 * @retval 0 for success
473 * @retval -1 for error
474 */
475 int shutdown_job_ring(struct sec_job_ring_t *job_ring);
476
477 /*
478 * @brief Enable irqs on associated job ring.
479 * @param [in] job_ring The job ring
480 * @retval 0 for success
481 * @retval -1 for error
482 */
483 int jr_enable_irqs(struct sec_job_ring_t *job_ring);
484
485 /*
486 * @brief Disable irqs on associated job ring.
487 * @param [in] job_ring The job ring
488 * @retval 0 for success
489 * @retval -1 for error
490 */
491 int jr_disable_irqs(struct sec_job_ring_t *job_ring);
492
493 /*
494 * IRJA - Input Ring Jobs Added Register shows
495 * how many new jobs were added to the Input Ring.
496 */
hw_enqueue_desc_on_job_ring(struct jobring_regs * regs,int num)497 static inline void hw_enqueue_desc_on_job_ring(struct jobring_regs *regs,
498 int num)
499 {
500 sec_out32(®s->irja, num);
501 }
502
503 #endif /* _SEC_HW_SPECIFIC_H_ */
504