55 */
66
77#include <assert.h>
8+ #include <asu_doorbell.h>
9+ #include <drivers/amd/asu_client.h>
810#include <initcall.h>
911#include <io.h>
1012#include <kernel/delay.h>
2022#include <trace.h>
2123#include <util.h>
2224
23- #include <asu_client.h>
24- #include <asu_doorbell.h>
25-
2625#define ASU_QUEUE_BUFFER_FULL 0xFFU
2726#define ASU_CLIENT_READY 0xFFFFFFFFU
2827#define ASU_TARGET_IPI_INT_MASK 1U
2928
30- #define ASU_GLOBAL_BASEADDR 0xEBF80000U
31- #define ASU_GLOBAL_GLOBAL_CNTRL (ASU_GLOBAL_BASEADDR + 0x00000000U)
29+ #define ASU_BASEADDR 0xEBF80000U
30+ #define ASU_GLOBAL_CNTRL (ASU_BASEADDR + 0x00000000U)
3231
33- #define ASU_GLOBAL_BASEADDR_SIZE 0x10000U
32+ #define ASU_BASEADDR_SIZE 0x10000U
3433#define ASU_GLOBAL_ADDR_LIMIT 0x1000U
3534
36- #define ASU_GLOBAL_GLOBAL_CNTRL_FW_IS_PRESENT_MASK 0x10U
37- #define ASU_ASUFW_BIT_CHECK_TIMEOUT_VALUE 0xFFFFFU
35+ #define ASU_GLOBAL_CNTRL_FW_IS_PRESENT_MASK 0x10U
36+ #define ASU_ASUFW_BIT_CHECK_TIMEOUT_VALUE 0xFFFFFU
3837
3938#define ASU_CHNL_IPI_BITMASK GENMASK_32(31, 16)
4039
@@ -53,14 +52,14 @@ struct asu_ref_to_callback {
5352};
5453
5554struct asu_ids {
56- uint8_t id_array [ASU_UNIQUE_ID_MAX ];
55+ uint8_t ids [ASU_UNIQUE_ID_MAX ];
5756 unsigned int slock ; /* id array spin lock */
5857};
5958
60- static struct asu_ids ids ;
59+ static struct asu_ids asuid ;
6160static struct asu_client * asu ;
6261
63- /**
62+ /*
6463 * asu_fwcheck() - Check if ASU firmware is present and ready
6564 * Polls the ASU global control register to verify if the HSM firmware
6665 * is present and ready for interaction. Uses a timeout of approximately
@@ -71,37 +70,31 @@ static struct asu_client *asu;
7170static TEE_Result asu_fwcheck (void )
7271{
7372 uint64_t timeout = 0 ;
74- bool fw_state = false;
7573
76- /* Timeout is set to ~1sec.
74+ /*
75+ * Timeout is set to ~1sec.
7776 * This is the worst case time within which ASUFW ready for interaction
7877 * with components requests.
7978 */
8079 timeout = timeout_init_us (ASU_ASUFW_BIT_CHECK_TIMEOUT_VALUE );
8180
8281 do {
8382 if (io_read32 ((vaddr_t )asu -> global_ctrl ) &
84- ASU_GLOBAL_GLOBAL_CNTRL_FW_IS_PRESENT_MASK ) {
85- fw_state = true;
86- DMSG ("Got the HSM FW" );
87- break ;
83+ ASU_GLOBAL_CNTRL_FW_IS_PRESENT_MASK ) {
84+ DMSG ("ASU FW is ready!" );
85+ return TEE_SUCCESS ;
8886 }
8987 } while (!timeout_elapsed (timeout ));
9088
91- if (fw_state ) {
92- DMSG ("ASU FW is ready!" );
93- return TEE_SUCCESS ;
94- }
95-
9689 EMSG ("ASU FW is NOT Present!" );
9790
9891 return TEE_ERROR_BAD_STATE ;
9992}
10093
101- /**
94+ /*
10295 * asu_get_channelID() - Determine the ASU channel ID for APU communication
10396 *
104- * Maps the Runtime Configuration Area (RTCA) to identify which ASU channel
97+ * Maps the Runtime Configuration Area (RTCA) to find which ASU channel
10598 * should be used by the APU for communication. Searches through available
10699 * channels to find one matching the APU's local IPI ID.
107100 *
@@ -113,9 +106,11 @@ static uint32_t asu_get_channelID(void)
113106 TEE_Result ret = TEE_ERROR_GENERIC ;
114107 void * comm_chnl_info = NULL ;
115108 uint32_t channel_id = ASU_MAX_IPI_CHANNELS ;
116- uintptr_t membase = 0 ;
109+ vaddr_t membase = 0 ;
110+ uint32_t id = 0 ;
117111
118- /* RTCA is mapped only to identify the ASU Channel ID
112+ /*
113+ * RTCA is mapped only to find the ASU Channel ID
119114 * to be used by APU.
120115 * After reading the information RTCA region is unmapped.
121116 */
@@ -127,10 +122,10 @@ static uint32_t asu_get_channelID(void)
127122 return channel_id ;
128123 }
129124
130- for (uint32_t id = 0 ; id < ASU_MAX_IPI_CHANNELS ; id ++ ) {
131- membase = (uintptr_t )comm_chnl_info +
132- ASU_RTCA_CHANNEL_BASE_OFFSET +
133- ASU_RTCA_CHANNEL_INFO_LEN * id ;
125+ for (id = 0 ; id < ASU_MAX_IPI_CHANNELS ; id ++ ) {
126+ membase = (vaddr_t )comm_chnl_info +
127+ ASU_RTCA_CHANNEL_BASE_OFFSET +
128+ ASU_RTCA_CHANNEL_INFO_LEN * id ;
134129 if ((io_read32 (membase ) & ASU_CHNL_IPI_BITMASK ) ==
135130 (CFG_AMD_APU_LCL_IPI_ID << 16 )) {
136131 channel_id = id ;
@@ -144,14 +139,14 @@ static uint32_t asu_get_channelID(void)
144139
145140 ret = core_mmu_remove_mapping (MEM_AREA_IO_SEC ,
146141 comm_chnl_info , ASU_GLOBAL_ADDR_LIMIT );
147- if (ret != TEE_SUCCESS )
142+ if (ret )
148143 EMSG ("Failed to unmap RTCA" );
149144
150145 return channel_id ;
151146}
152147
153- /**
154- * alloc_unique_id () - Generate a unique identifier for ASU operations
148+ /*
149+ * asu_alloc_unique_id () - Generate a unique identifier for ASU operations
155150 *
156151 * Creates a unique ID by cycling through available IDs in the callback
157152 * reference array. Ensures no ID collision by checking if the slot is
@@ -160,35 +155,44 @@ static uint32_t asu_get_channelID(void)
160155 * Return: Unique ID (1 to ASU_UNIQUE_ID_MAX-1) or ASU_UNIQUE_ID_MAX if none
161156 * available
162157 */
163- uint8_t alloc_unique_id (void )
158+ uint8_t asu_alloc_unique_id (void )
164159{
165160 uint8_t unqid = 0 ;
166161 uint32_t state = 0 ;
167162
168- state = cpu_spin_lock_xsave (& ids .slock );
163+ state = cpu_spin_lock_xsave (& asuid .slock );
169164 while (unqid < ASU_UNIQUE_ID_MAX ) {
170- if (ids . id_array [unqid ] == ASU_UNIQUE_ID_MAX ) {
171- ids . id_array [unqid ] = unqid ;
165+ if (asuid . ids [unqid ] == ASU_UNIQUE_ID_MAX ) {
166+ asuid . ids [unqid ] = unqid ;
172167 DMSG ("Got unique ID %" PRIu8 , unqid );
173168 break ;
174169 }
175170 unqid ++ ;
176171 };
177- cpu_spin_unlock_xrestore (& ids .slock , state );
172+ cpu_spin_unlock_xrestore (& asuid .slock , state );
178173
179174 return unqid ;
180175}
181176
182- void free_unique_id (uint8_t uniqueid )
177+ /**
178+ * asu_free_unique_id() - Release a previously allocated unique ID
179+ * @uniqueid: The unique ID to be freed
180+ *
181+ * Marks the specified unique ID as available for reuse.
182+ * The released ID is set to ASU_UNIQUE_ID_MAX
183+ * to indicate its availability. Intended to be used in environments where
184+ * concurrent access to the unique ID pool occurs.
185+ */
186+ void asu_free_unique_id (uint8_t uniqueid )
183187{
184188 uint32_t state = 0 ;
185189
186- state = cpu_spin_lock_xsave (& ids .slock );
187- ids . id_array [uniqueid ] = ASU_UNIQUE_ID_MAX ;
188- cpu_spin_unlock_xrestore (& ids .slock , state );
190+ state = cpu_spin_lock_xsave (& asuid .slock );
191+ asuid . ids [uniqueid ] = ASU_UNIQUE_ID_MAX ;
192+ cpu_spin_unlock_xrestore (& asuid .slock , state );
189193}
190194
191- /**
195+ /*
192196 * get_free_index() - Find a free buffer index in the specified priority queue
193197 * @priority: Priority level (ASU_PRIORITY_HIGH or ASU_PRIORITY_LOW)
194198 *
@@ -237,7 +241,7 @@ static void put_free_index(struct asu_channel_queue_buf *bufptr)
237241 cpu_spin_unlock_xrestore (& asu -> slock , state );
238242}
239243
240- /**
244+ /*
241245 * asu_validate_client_parameters() - Validate client parameter structure
242246 * @param_ptr: Pointer to client parameters to validate
243247 *
@@ -253,7 +257,7 @@ TEE_Result asu_validate_client_parameters(struct asu_client_params *param_ptr)
253257 return TEE_ERROR_BAD_PARAMETERS ;
254258 }
255259
256- if (!param_ptr -> cbrefptr ) {
260+ if (!param_ptr -> cbptr ) {
257261 EMSG ("Callback function not available" );
258262 return TEE_ERROR_ITEM_NOT_FOUND ;
259263 }
@@ -267,7 +271,7 @@ TEE_Result asu_validate_client_parameters(struct asu_client_params *param_ptr)
267271 return TEE_SUCCESS ;
268272}
269273
270- /**
274+ /*
271275 * send_doorbell() - Send IPI doorbell interrupt to ASU
272276 *
273277 * Triggers an Inter-Processor Interrupt (IPI) to notify the ASU
@@ -283,7 +287,7 @@ static TEE_Result send_doorbell(void)
283287 return TEE_SUCCESS ;
284288}
285289
286- /**
290+ /*
287291 * asu_update_queue_buffer_n_send_ipi() - Queue command and send IPI
288292 * @param: Client parameters including priority
289293 * @req_buffer: Request buffer containing command data
@@ -331,13 +335,13 @@ TEE_Result asu_update_queue_buffer_n_send_ipi(struct asu_client_params *param,
331335 qptr = & asu -> chnl_memptr -> p1_chnl_q ;
332336 }
333337
334- bufptr -> req_buf .header = header ;
338+ bufptr -> req .header = header ;
335339 if (req_buffer && size != 0U )
336- memcpy (bufptr -> req_buf .arg , req_buffer , size );
340+ memcpy (bufptr -> req .arg , req_buffer , size );
337341
338342 bufptr -> respbufstatus = 0U ;
339343
340- qptr -> is_cmd_present = ASU_TRUE ;
344+ qptr -> cmd_is_present = true ;
341345 qptr -> req_sent ++ ;
342346 ret = send_doorbell ();
343347 if (ret != TEE_SUCCESS ) {
@@ -353,9 +357,9 @@ TEE_Result asu_update_queue_buffer_n_send_ipi(struct asu_client_params *param,
353357 wfe ();
354358 }
355359
356- * status = bufptr -> resp_buf .arg [ASU_RESPONSE_STATUS_INDEX ];
360+ * status = bufptr -> resp .arg [ASU_RESPONSE_STATUS_INDEX ];
357361 if (param -> cbhandler && !(* status ))
358- ret = param -> cbhandler (param -> cbrefptr , & bufptr -> resp_buf );
362+ ret = param -> cbhandler (param -> cbptr , & bufptr -> resp );
359363 put_free_index (bufptr );
360364
361365 return ret ;
@@ -370,7 +374,7 @@ static void asu_clear_intr(void)
370374 status & IPIPSU_ALL_MASK );
371375}
372376
373- /**
377+ /*
374378 * asu_resp_handler() - Interrupt handler for ASU responses
375379 * @handler: Interrupt handler structure (unused)
376380 *
@@ -393,7 +397,7 @@ static struct itr_handler doorbell_handler = {
393397 .handler = asu_resp_handler ,
394398};
395399
396- /**
400+ /*
397401 * setup_doorbell() - Initialize doorbell interrupt handling
398402 *
399403 * Maps the doorbell register region, configures interrupt settings,
@@ -409,7 +413,7 @@ static void *setup_doorbell(void)
409413
410414 dbell = core_mmu_add_mapping (MEM_AREA_IO_SEC ,
411415 configtable .baseaddr ,
412- ASU_GLOBAL_BASEADDR_SIZE );
416+ ASU_BASEADDR_SIZE );
413417 if (!dbell ) {
414418 EMSG ("Failed to Map Door Bell register" );
415419 return dbell ;
@@ -424,7 +428,7 @@ static void *setup_doorbell(void)
424428 IRQ_TYPE_LEVEL_HIGH , 7 );
425429 if (res ) {
426430 core_mmu_remove_mapping (MEM_AREA_IO_SEC , dbell ,
427- ASU_GLOBAL_BASEADDR_SIZE );
431+ ASU_BASEADDR_SIZE );
428432 panic ();
429433 }
430434
@@ -437,12 +441,12 @@ static void asu_init_unique_id(void)
437441{
438442 uint32_t idx = 0 ;
439443
440- ids .slock = SPINLOCK_UNLOCK ;
441- for (idx = 0 ; idx < ASU_UNIQUE_ID_MAX ; idx ++ )
442- ids . id_array [idx ] = ASU_UNIQUE_ID_MAX ;
444+ asuid .slock = SPINLOCK_UNLOCK ;
445+ for (idx = 0 ; idx < ARRAY_SIZE ( asuid . ids ) ; idx ++ )
446+ asuid . ids [idx ] = ASU_UNIQUE_ID_MAX ;
443447}
444448
445- /**
449+ /*
446450 * asu_init() - Initialize the ASU driver and communication channel
447451 *
448452 * Performs complete ASU driver initialization including memory allocation,
@@ -466,8 +470,8 @@ static TEE_Result asu_init(void)
466470 }
467471
468472 asu -> global_ctrl = core_mmu_add_mapping (MEM_AREA_IO_SEC ,
469- ASU_GLOBAL_BASEADDR ,
470- ASU_GLOBAL_BASEADDR_SIZE );
473+ ASU_BASEADDR ,
474+ ASU_BASEADDR_SIZE );
471475 if (!asu -> global_ctrl ) {
472476 EMSG ("Failed to initialized ASU" );
473477 goto free ;
@@ -514,7 +518,7 @@ static TEE_Result asu_init(void)
514518 ASU_GLOBAL_ADDR_LIMIT );
515519global_unmap :
516520 core_mmu_remove_mapping (MEM_AREA_IO_SEC , asu -> global_ctrl ,
517- ASU_GLOBAL_BASEADDR_SIZE );
521+ ASU_BASEADDR_SIZE );
518522free :
519523 free (asu );
520524
0 commit comments