AM263Px MCU+ SDK  11.00.00
sipc_notify_mailbox.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef SIPC_NOTIFY_MAILBOX_H_
34 #define SIPC_NOTIFY_MAILBOX_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 /* this file has define's and inline function's to program the HW mailbox registers and SW queue structure */
45 #define MAILBOX_MAX_SW_QUEUE_STRUCT_SIZE (sizeof(SIPC_SwQueue))
46 
47 /* The HW mailbox only allows to trigger a interrupt on another core,
48  * the SIPC Notify needs ability to pass x byte message along with a interrupt
49  *
50  * Basically this mimics the functionality of HW mailbox with HW FIFO in AM243x SOC
51  *
52  * This needs to be in sync with the addresses being set for SW queue memory in soc/{soc}/sipc_notify_cfg.c
53  *
54  * The new queue has two more parameters i.e EleSize = Size of 1 queue element in words
55  * Qlength = total length of this Queue */
56 
62 typedef struct SIPC_SwQueue_
63 {
64  uint32_t rdIdx;
65  uint32_t wrIdx;
66  uint16_t EleSize ;
67  uint16_t Qlength ;
68  uint8_t *Qfifo;
69 } SIPC_SwQueue;
70 
71 #if defined(__aarch64__) || defined(__arm__)
72 static inline void asm_dsb_memory(void)
73 {
74  __asm__ __volatile__( "dsb sy" "\n\t": : : "memory");
75 }
76 
77 static inline void asm_isb_memory(void)
78 {
79  __asm__ __volatile__( "isb" "\n\t": : : "memory");
80 }
81 #endif
82 
83 /* Read from SW fifo within a mailbox */
84 static inline int32_t SIPC_mailboxRead(SIPC_SwQueue *swQ, uint8_t *Buff)
85 {
86  int32_t status = SystemP_FAILURE;
87 
88  volatile uint32_t rdIdx = swQ->rdIdx;
89  volatile uint32_t wrIdx = swQ->wrIdx;
90 
91  if((rdIdx < swQ->Qlength) && (wrIdx < swQ->Qlength))
92  {
93  /* If this condition meets then it means there is something in the fifo*/
94  if( rdIdx != wrIdx)
95  {
96  /* Copy EleSize bytes from Queue memory to the buffer */
97  memcpy(Buff, SOC_phyToVirt((uint64_t)(swQ->Qfifo + (swQ->EleSize*rdIdx))),swQ->EleSize);
98 
99  rdIdx = (rdIdx+1)%swQ->Qlength;
100 
101  swQ->rdIdx = rdIdx;
102 
103  rdIdx = swQ->rdIdx; /* read back to ensure the update has reached the memory */
104 
105  #if defined(__aarch64__) || defined(__arm__)
106  asm_dsb_memory();
107  asm_isb_memory();
108  #endif
109 
110  status = SystemP_SUCCESS;
111  }
112  }
113 
114  return status;
115 }
116 
117 /* Write to SW fifo and trigger HW interrupt using HW mailbox */
118 static inline int32_t SIPC_mailboxWrite(uint32_t mailboxBaseAddr, uint32_t wrIntrBitPos, SIPC_SwQueue *swQ, uint8_t *Buff)
119 {
120  int32_t status = SystemP_FAILURE;
121 
122  volatile uint32_t rdIdx = swQ->rdIdx;
123  volatile uint32_t wrIdx = swQ->wrIdx;
124 
125  if((rdIdx < swQ->Qlength) && (wrIdx < swQ->Qlength))
126  {
127  if( ( (wrIdx+1)%swQ->Qlength ) != rdIdx )
128  {
129  volatile uint32_t *addr = (uint32_t *)mailboxBaseAddr;
130 
131  /* There is some space in the FIFO */
132 
133  memcpy(SOC_phyToVirt((uint64_t)(swQ->Qfifo + (swQ->EleSize*wrIdx))),Buff,swQ->EleSize);
134 
135  wrIdx = (wrIdx+1)%swQ->Qlength;
136 
137  swQ->wrIdx = wrIdx;
138 
139  wrIdx = swQ->wrIdx; /* read back to ensure the update has reached the memory */
140 
141  #if defined(__aarch64__) || defined(__arm__)
142  asm_dsb_memory();
143  asm_isb_memory();
144  #endif
145 
146  /* Trigger interrupt to other core */
147  *addr = (1U << (wrIntrBitPos));
148 
149  status = SystemP_SUCCESS;
150  }
151  }
152  return status;
153 }
154 
155 static inline void SIPC_mailboxClearAllInt(uint32_t mailboxBaseAddr)
156 {
157  volatile uint32_t *addr = (uint32_t *)mailboxBaseAddr;
158  *addr = 0x1111111;
159 }
160 
161 static inline uint32_t SIPC_mailboxGetPendingIntr(uint32_t mailboxBaseAddr)
162 {
163  volatile uint32_t *addr = (uint32_t *)mailboxBaseAddr;
164 
165  return *addr;
166 }
167 
168 static inline void SIPC_mailboxClearPendingIntr(uint32_t mailboxBaseAddr, uint32_t pendingIntr)
169 {
170  volatile uint32_t *addr = ( uint32_t *)mailboxBaseAddr;
171  *addr = pendingIntr;
172 }
173 
174 static inline uint32_t SIPC_mailboxIsPendingIntr(uint32_t pendingIntr, uint32_t coreId)
175 {
176  extern uint32_t gSIPCCoreIntrBitPos[];
177 
178  uint32_t isPending = 0;
179  isPending = pendingIntr & (1 << gSIPCCoreIntrBitPos[coreId]);
180  return isPending;
181 }
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif /*SIPC_NOTIFY_MAILBOX_H_*/
188 
SIPC_mailboxIsPendingIntr
static uint32_t SIPC_mailboxIsPendingIntr(uint32_t pendingIntr, uint32_t coreId)
Definition: sipc_notify_mailbox.h:174
SIPC_SwQueue::wrIdx
uint32_t wrIdx
Definition: sipc_notify_mailbox.h:65
SIPC_SwQueue::rdIdx
uint32_t rdIdx
Definition: sipc_notify_mailbox.h:64
SIPC_mailboxWrite
static int32_t SIPC_mailboxWrite(uint32_t mailboxBaseAddr, uint32_t wrIntrBitPos, SIPC_SwQueue *swQ, uint8_t *Buff)
Definition: sipc_notify_mailbox.h:118
SIPC_SwQueue::Qfifo
uint8_t * Qfifo
Definition: sipc_notify_mailbox.h:68
SIPC_mailboxGetPendingIntr
static uint32_t SIPC_mailboxGetPendingIntr(uint32_t mailboxBaseAddr)
Definition: sipc_notify_mailbox.h:161
SOC_phyToVirt
void * SOC_phyToVirt(uint64_t phyAddr)
Physical to Virtual (CPU) address translation function.
SIPC_SwQueue::Qlength
uint16_t Qlength
Definition: sipc_notify_mailbox.h:67
SIPC_SwQueue
SIPC swQ structure which holds the data pointer to a fifo Queue in HSM MBOX memory.
Definition: sipc_notify_mailbox.h:63
SystemP_SUCCESS
#define SystemP_SUCCESS
Return status when the API execution was successful.
Definition: SystemP.h:56
SystemP_FAILURE
#define SystemP_FAILURE
Return status when the API execution was not successful due to a failure.
Definition: SystemP.h:61
SIPC_SwQueue::EleSize
uint16_t EleSize
Definition: sipc_notify_mailbox.h:66
SIPC_mailboxClearPendingIntr
static void SIPC_mailboxClearPendingIntr(uint32_t mailboxBaseAddr, uint32_t pendingIntr)
Definition: sipc_notify_mailbox.h:168
SIPC_mailboxClearAllInt
static void SIPC_mailboxClearAllInt(uint32_t mailboxBaseAddr)
Definition: sipc_notify_mailbox.h:155
SIPC_mailboxRead
static int32_t SIPC_mailboxRead(SIPC_SwQueue *swQ, uint8_t *Buff)
Definition: sipc_notify_mailbox.h:84