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