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 union {
191 uint64_t m_whole;
192 struct {
193 #ifdef NXP_SEC_BE
194 uint32_t high;
195 uint32_t low;
196 #else
197 uint32_t low;
198 uint32_t high;
199 #endif
200 } m_halves;
201 } ptr_addr_t;
202
203 #if defined(CONFIG_PHYS_64BIT)
204 #define sec_read_addr(a) sec_in64((a))
205 #define sec_write_addr(a, v) sec_out64((a), (v))
206 #else
207 #define sec_read_addr(a) sec_in32((a))
208 #define sec_write_addr(a, v) sec_out32((a), (v))
209 #endif
210
211 #define JR_REG(name, jr) (CHAN_BASE(jr) + JR_REG_##name##_OFFSET)
212 #define JR_REG_LO(name, jr) (CHAN_BASE(jr) + JR_REG_##name##_OFFSET_LO)
213
214 #define GET_JR_REG(name, jr) (sec_in32(JR_REG(name, (jr))))
215 #define GET_JR_REG_LO(name, jr) (sec_in32(JR_REG_LO(name, (jr))))
216
217 #define SET_JR_REG(name, jr, val) \
218 (sec_out32(JR_REG(name, (jr)), (val)))
219
220 #define SET_JR_REG_LO(name, jr, val) \
221 (sec_out32(JR_REG_LO(name, (jr)), (val)))
222
223 /* STRUCTURES AND OTHER TYPEDEFS */
224 /* Lists the possible states for a job ring. */
225 typedef enum sec_job_ring_state_e {
226 SEC_JOB_RING_STATE_STARTED, /* Job ring is initialized */
227 SEC_JOB_RING_STATE_RESET, /* Job ring reset is in progres */
228 } sec_job_ring_state_t;
229
230 struct sec_job_ring_t {
231 /*
232 * Consumer index for job ring (jobs array).
233 * @note: cidx and pidx are accessed from
234 * different threads.
235 * Place the cidx and pidx inside the structure
236 * so that they lay on different cachelines, to
237 * avoid false sharing between threads when the
238 * threads run on different cores!
239 */
240 uint32_t cidx;
241
242 /* Producer index for job ring (jobs array) */
243 uint32_t pidx;
244
245 /* Ring of input descriptors. Size of array is power of 2 to allow
246 * fast update of producer/consumer indexes with bitwise operations.
247 */
248 phys_addr_t *input_ring;
249
250 /* Ring of output descriptors. */
251 struct sec_outring_entry *output_ring;
252
253 /* The file descriptor used for polling for interrupts notifications */
254 uint32_t irq_fd;
255
256 /* Model used by SEC Driver to receive notifications from SEC.
257 * Can be either of the three:
258 * #SEC_NOTIFICATION_TYPE_IRQ or
259 * #SEC_NOTIFICATION_TYPE_POLL
260 */
261 uint32_t jr_mode;
262 /* Base address for SEC's register memory for this job ring. */
263 void *register_base_addr;
264 /* notifies if coelescing is enabled for the job ring */
265 uint8_t coalescing_en;
266 /* The state of this job ring */
267 sec_job_ring_state_t jr_state;
268 };
269
270 /* Forward structure declaration */
271 typedef struct sec_job_ring_t sec_job_ring_t;
272
273 struct sec_outring_entry {
274 phys_addr_t desc; /* Pointer to completed descriptor */
275 uint32_t status; /* Status for completed descriptor */
276 } __packed;
277
278 /* Lists the states possible for the SEC user space driver. */
279 typedef enum sec_driver_state_e {
280 SEC_DRIVER_STATE_IDLE, /*< Driver not initialized */
281 SEC_DRIVER_STATE_STARTED, /*< Driver initialized and */
282 SEC_DRIVER_STATE_RELEASE, /*< Driver release is in progress */
283 } sec_driver_state_t;
284
285 /* Union describing the possible error codes that */
286 /* can be set in the descriptor status word */
287
288 union hw_error_code {
289 uint32_t error;
290 union {
291 struct {
292 uint32_t ssrc:4;
293 uint32_t ssed_val:28;
294 } __packed value;
295 struct {
296 uint32_t ssrc:4;
297 uint32_t res:28;
298 } __packed no_status_src;
299 struct {
300 uint32_t ssrc:4;
301 uint32_t jmp:1;
302 uint32_t res:11;
303 uint32_t desc_idx:8;
304 uint32_t cha_id:4;
305 uint32_t err_id:4;
306 } __packed ccb_status_src;
307 struct {
308 uint32_t ssrc:4;
309 uint32_t jmp:1;
310 uint32_t res:11;
311 uint32_t desc_idx:8;
312 uint32_t offset:8;
313 } __packed jmp_halt_user_src;
314 struct {
315 uint32_t ssrc:4;
316 uint32_t jmp:1;
317 uint32_t res:11;
318 uint32_t desc_idx:8;
319 uint32_t desc_err:8;
320 } __packed deco_src;
321 struct {
322 uint32_t ssrc:4;
323 uint32_t res:17;
324 uint32_t naddr:3;
325 uint32_t desc_err:8;
326 } __packed jr_src;
327 struct {
328 uint32_t ssrc:4;
329 uint32_t jmp:1;
330 uint32_t res:11;
331 uint32_t desc_idx:8;
332 uint32_t cond:8;
333 } __packed jmp_halt_cond_src;
334 } __packed error_desc;
335 } __packed;
336
337 /* FUNCTION PROTOTYPES */
338
339 /*
340 * @brief Initialize a job ring/channel in SEC device.
341 * Write configuration register/s to properly initialize a job ring.
342 *
343 * @param [in] job_ring The job ring
344 *
345 * @retval 0 for success
346 * @retval other for error
347 */
348 int hw_reset_job_ring(sec_job_ring_t *job_ring);
349
350 /*
351 * @brief Reset a job ring/channel in SEC device.
352 * Write configuration register/s to reset a job ring.
353 *
354 * @param [in] job_ring The job ring
355 *
356 * @retval 0 for success
357 * @retval -1 in case job ring reset failed
358 */
359 int hw_shutdown_job_ring(sec_job_ring_t *job_ring);
360
361 /*
362 * @brief Handle a job ring/channel error in SEC device.
363 * Identify the error type and clear error bits if required.
364 *
365 * @param [in] job_ring The job ring
366 * @param [in] sec_error_code error code as first read from SEC engine
367 */
368
369 void hw_handle_job_ring_error(sec_job_ring_t *job_ring,
370 uint32_t sec_error_code);
371 /*
372 * @brief Handle a job ring error in the device.
373 * Identify the error type and printout a explanatory
374 * messages.
375 *
376 * @param [in] job_ring The job ring
377 *
378 */
379
380 int hw_job_ring_error(sec_job_ring_t *job_ring);
381
382 /* @brief Set interrupt coalescing parameters on the Job Ring.
383 * @param [in] job_ring The job ring
384 * @param [in] irq_coalesing_timer
385 * Interrupt coalescing timer threshold.
386 * This value determines the maximum
387 * amount of time after processing a descriptor
388 * before raising an interrupt.
389 * @param [in] irq_coalescing_count
390 * Interrupt coalescing count threshold.
391 * This value determines how many descriptors
392 * are completed before raising an interrupt.
393 */
394
395 int hw_job_ring_set_coalescing_param(sec_job_ring_t *job_ring,
396 uint16_t irq_coalescing_timer,
397 uint8_t irq_coalescing_count);
398
399 /* @brief Enable interrupt coalescing on a job ring
400 * @param [in] job_ring The job ring
401 */
402
403 int hw_job_ring_enable_coalescing(sec_job_ring_t *job_ring);
404
405 /*
406 * @brief Disable interrupt coalescing on a job ring
407 * @param [in] job_ring The job ring
408 */
409
410 int hw_job_ring_disable_coalescing(sec_job_ring_t *job_ring);
411
412 /*
413 * @brief Poll the HW for already processed jobs in the JR
414 * and notify the available jobs to UA.
415 *
416 * @param [in] job_ring The job ring to poll.
417 * @param [in] limit The maximum number of jobs to notify.
418 * If set to negative value, all available
419 * jobs are notified.
420 *
421 * @retval >=0 for No of jobs notified to UA.
422 * @retval -1 for error
423 */
424
425 int hw_poll_job_ring(struct sec_job_ring_t *job_ring, int32_t limit);
426
427 /* @brief Poll the HW for already processed jobs in the JR
428 * and silently discard the available jobs or notify them to UA
429 * with indicated error code.
430
431 * @param [in,out] job_ring The job ring to poll.
432 * @param [in] do_notify Can be #TRUE or #FALSE.
433 * Indicates if descriptors to be discarded
434 * or notified to UA with given error_code.
435 * @param [in] error_code The detailed SEC error code.
436 * @param [out] notified_descs Number of notified descriptors.
437 * Can be NULL if do_notify is #FALSE
438 */
439 void hw_flush_job_ring(struct sec_job_ring_t *job_ring,
440 uint32_t do_notify,
441 uint32_t error_code, uint32_t *notified_descs);
442
443 /*
444 * @brief Flush job rings of any processed descs.
445 * The processed descs are silently dropped,
446 * WITHOUT being notified to UA.
447 */
448 void flush_job_rings(void);
449
450 /*
451 * @brief Handle desc that generated error in SEC engine.
452 * Identify the exact type of error and handle the error.
453 * Depending on the error type, the job ring could be reset.
454 * All descs that are submitted for processing on this job ring
455 * are notified to User Application with error status and detailed error code.
456
457 * @param [in] job_ring Job ring
458 * @param [in] sec_error_code Error code read from job ring's Channel
459 * Status Register
460 * @param [out] notified_descs Number of notified descs. Can be NULL if
461 * do_notify is #FALSE
462 * @param [out] do_driver_shutdown If set to #TRUE, then UA is returned code
463 * #SEC_PROCESSING_ERROR
464 * which is indication that UA must call
465 * sec_release() after this.
466 */
467 void sec_handle_desc_error(struct sec_job_ring_t *job_ring,
468 uint32_t sec_error_code,
469 uint32_t *notified_descs,
470 uint32_t *do_driver_shutdown);
471
472 /*
473 * @brief Release the software and hardware resources tied to a job ring.
474 * @param [in] job_ring The job ring
475 * @retval 0 for success
476 * @retval -1 for error
477 */
478 int shutdown_job_ring(struct sec_job_ring_t *job_ring);
479
480 /*
481 * @brief Enable irqs on associated job ring.
482 * @param [in] job_ring The job ring
483 * @retval 0 for success
484 * @retval -1 for error
485 */
486 int jr_enable_irqs(struct sec_job_ring_t *job_ring);
487
488 /*
489 * @brief Disable irqs on associated job ring.
490 * @param [in] job_ring The job ring
491 * @retval 0 for success
492 * @retval -1 for error
493 */
494 int jr_disable_irqs(struct sec_job_ring_t *job_ring);
495
496 /*
497 * IRJA - Input Ring Jobs Added Register shows
498 * how many new jobs were added to the Input Ring.
499 */
hw_enqueue_desc_on_job_ring(struct jobring_regs * regs,int num)500 static inline void hw_enqueue_desc_on_job_ring(struct jobring_regs *regs,
501 int num)
502 {
503 sec_out32(®s->irja, num);
504 }
505
506 #endif /* _SEC_HW_SPECIFIC_H_ */
507