xref: /aosp_15_r20/external/coreboot/src/vendorcode/cavium/include/bdk/libbdk-os/bdk-thread.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 #ifndef __CB_BDK_THREAD_H__
2 #define __CB_BDK_THREAD_H__
3 /***********************license start***********************************
4 * Copyright (c) 2003-2017  Cavium Inc. ([email protected]). All rights
5 * reserved.
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 *   * Redistributions of source code must retain the above copyright
13 *     notice, this list of conditions and the following disclaimer.
14 *
15 *   * Redistributions in binary form must reproduce the above
16 *     copyright notice, this list of conditions and the following
17 *     disclaimer in the documentation and/or other materials provided
18 *     with the distribution.
19 *
20 *   * Neither the name of Cavium Inc. nor the names of
21 *     its contributors may be used to endorse or promote products
22 *     derived from this software without specific prior written
23 *     permission.
24 *
25 * This Software, including technical data, may be subject to U.S. export
26 * control laws, including the U.S. Export Administration Act and its
27 * associated regulations, and may be subject to export or import
28 * regulations in other countries.
29 *
30 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
31 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
32 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
33 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
34 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
35 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
36 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
37 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
38 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK
39 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
40 ***********************license end**************************************/
41 
42 /**
43  * @file
44  *
45  * Functions for controlling threads.
46  *
47  * <hr>$Revision: 49448 $<hr>
48  *
49  * @defgroup thread Threading library
50  * @{
51  */
52 
53 /* Use a larger stack size for main() as it tends to do lots of
54     extra stuff. For example, DDR init requires a bigger stack */
55 #define BDK_THREAD_MAIN_STACK_SIZE 16384
56 #define BDK_THREAD_DEFAULT_STACK_SIZE 8192
57 
58 typedef void (*bdk_thread_func_t)(int arg, void *arg1);
59 
60 extern int bdk_thread_initialize(void);
61 extern void bdk_thread_yield(void);
62 extern int bdk_thread_create(bdk_node_t node, uint64_t coremask, bdk_thread_func_t func, int arg0, void *arg1, int stack_size);
63 extern void bdk_thread_destroy(void) __attribute__ ((noreturn));
64 extern void bdk_thread_first(bdk_thread_func_t func, int arg0, void *arg1, int stack_size) __attribute__ ((noreturn));
65 
66 /**
67  * Number of the Core on which the program is currently running.
68  *
69  * @return Number of cores
70  */
71 static inline int bdk_get_core_num(void) __attribute__ ((always_inline));
bdk_get_core_num(void)72 static inline int bdk_get_core_num(void)
73 {
74     if (CAVIUM_IS_MODEL(CAVIUM_CN8XXX))
75     {
76         int mpidr_el1;
77         BDK_MRS(MPIDR_EL1, mpidr_el1);
78         /* Core is 4 bits from AFF0 and rest from AFF1 */
79         int core_num;
80         core_num = mpidr_el1 & 0xf;
81         core_num |= (mpidr_el1 & 0xff00) >> 4;
82         return core_num;
83     }
84     else
85     {
86         uint64_t cvm_pn_el1;
87         BDK_MRS(s3_0_c11_c4_2, cvm_pn_el1);
88         return cvm_pn_el1 & 0xffff;
89     }
90 }
91 
92 /**
93  * Return a mask representing this core in a 64bit bitmask
94  *
95  * @return
96  */
97 static inline uint64_t bdk_core_to_mask(void) __attribute__ ((always_inline));
bdk_core_to_mask(void)98 static inline uint64_t bdk_core_to_mask(void)
99 {
100     return 1ull << bdk_get_core_num();
101 }
102 
bdk_is_boot_core(void)103 static inline int bdk_is_boot_core(void)
104 {
105     const int master = 0x80000000 | (bdk_numa_master() << 16);
106     int mpidr_el1;
107     BDK_MRS_NV(MPIDR_EL1, mpidr_el1);
108     return mpidr_el1 == master;
109 }
110 
111 
112 static inline void *bdk_thread_get_id(void) __attribute__ ((always_inline));
bdk_thread_get_id(void)113 static inline void *bdk_thread_get_id(void)
114 {
115     uint64_t current;
116     BDK_MRS_NV(TPIDR_EL3, current);
117     /* If we haven't started threading yet use the core number. Add one
118         so the thread id is never zero */
119     if (!current)
120         current = bdk_get_core_num() + 1;
121     return (void*)current;
122 }
123 
124 /** @} */
125 #endif	/* !__CB_BDK_THREAD_H__ */
126