]> begriffs open source - freertos/blob - portable/GCC/MicroBlazeV9/port.c
Add SMP in the License Header (#402)
[freertos] / portable / GCC / MicroBlazeV9 / port.c
1 /*
2  * FreeRTOS SMP Kernel V202110.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*-----------------------------------------------------------
29  * Implementation of functions defined in portable.h for the MicroBlaze port.
30  *----------------------------------------------------------*/
31
32
33 /* Scheduler includes. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36
37 /* Standard includes. */
38 #include <string.h>
39
40 /* Hardware includes. */
41 #include <xintc_i.h>
42 #include <xil_exception.h>
43 #include <microblaze_exceptions_g.h>
44
45 /* Tasks are started with a critical section nesting of 0 - however, prior to
46 the scheduler being commenced interrupts should not be enabled, so the critical
47 nesting variable is initialised to a non-zero value. */
48 #define portINITIAL_NESTING_VALUE       ( 0xff )
49
50 /* The bit within the MSR register that enabled/disables interrupts and
51 exceptions respectively. */
52 #define portMSR_IE                                      ( 0x02U )
53 #define portMSR_EE                                      ( 0x100U )
54
55 /* If the floating point unit is included in the MicroBlaze build, then the
56 FSR register is saved as part of the task context.  portINITIAL_FSR is the value
57 given to the FSR register when the initial context is set up for a task being
58 created. */
59 #define portINITIAL_FSR                         ( 0U )
60
61 /*-----------------------------------------------------------*/
62
63 /*
64  * Initialise the interrupt controller instance.
65  */
66 static int32_t prvInitialiseInterruptController( void );
67
68 /* Ensure the interrupt controller instance variable is initialised before it is
69  * used, and that the initialisation only happens once.
70  */
71 static int32_t prvEnsureInterruptControllerIsInitialised( void );
72
73 /*-----------------------------------------------------------*/
74
75 /* Counts the nesting depth of calls to portENTER_CRITICAL().  Each task
76 maintains its own count, so this variable is saved as part of the task
77 context. */
78 volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
79
80 /* This port uses a separate stack for interrupts.  This prevents the stack of
81 every task needing to be large enough to hold an entire interrupt stack on top
82 of the task stack. */
83 uint32_t *pulISRStack;
84
85 /* If an interrupt requests a context switch, then ulTaskSwitchRequested will
86 get set to 1.  ulTaskSwitchRequested is inspected just before the main interrupt
87 handler exits.  If, at that time, ulTaskSwitchRequested is set to 1, the kernel
88 will call vTaskSwitchContext() to ensure the task that runs immediately after
89 the interrupt exists is the highest priority task that is able to run.  This is
90 an unusual mechanism, but is used for this port because a single interrupt can
91 cause the servicing of multiple peripherals - and it is inefficient to call
92 vTaskSwitchContext() multiple times as each peripheral is serviced. */
93 volatile uint32_t ulTaskSwitchRequested = 0UL;
94
95 /* The instance of the interrupt controller used by this port.  This is required
96 by the Xilinx library API functions. */
97 static XIntc xInterruptControllerInstance;
98
99 /*-----------------------------------------------------------*/
100
101 /*
102  * Initialise the stack of a task to look exactly as if a call to
103  * portSAVE_CONTEXT had been made.
104  *
105  * See the portable.h header file.
106  */
107 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
108 {
109 extern void *_SDA2_BASE_, *_SDA_BASE_;
110 const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;
111 const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;
112 extern void _start1( void );
113
114         /* Place a few bytes of known values on the bottom of the stack.
115         This is essential for the Microblaze port and these lines must
116         not be omitted. */
117         *pxTopOfStack = ( StackType_t ) 0x00000000;
118         pxTopOfStack--;
119         *pxTopOfStack = ( StackType_t ) 0x00000000;
120         pxTopOfStack--;
121         *pxTopOfStack = ( StackType_t ) 0x00000000;
122         pxTopOfStack--;
123
124         #if( XPAR_MICROBLAZE_USE_FPU != 0 )
125                 /* The FSR value placed in the initial task context is just 0. */
126                 *pxTopOfStack = portINITIAL_FSR;
127                 pxTopOfStack--;
128         #endif
129
130         /* The MSR value placed in the initial task context should have interrupts
131         disabled.  Each task will enable interrupts automatically when it enters
132         the running state for the first time. */
133         *pxTopOfStack = mfmsr() & ~portMSR_IE;
134
135         #if( MICROBLAZE_EXCEPTIONS_ENABLED == 1 )
136         {
137                 /* Ensure exceptions are enabled for the task. */
138                 *pxTopOfStack |= portMSR_EE;
139         }
140         #endif
141
142         pxTopOfStack--;
143
144         /* First stack an initial value for the critical section nesting.  This
145         is initialised to zero. */
146         *pxTopOfStack = ( StackType_t ) 0x00;
147
148         /* R0 is always zero. */
149         /* R1 is the SP. */
150
151         /* Place an initial value for all the general purpose registers. */
152         pxTopOfStack--;
153         *pxTopOfStack = ( StackType_t ) ulR2;   /* R2 - read only small data area. */
154         pxTopOfStack--;
155         *pxTopOfStack = ( StackType_t ) 0x03;   /* R3 - return values and temporaries. */
156         pxTopOfStack--;
157         *pxTopOfStack = ( StackType_t ) 0x04;   /* R4 - return values and temporaries. */
158         pxTopOfStack--;
159         *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */
160
161         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
162                 pxTopOfStack--;
163                 *pxTopOfStack = ( StackType_t ) 0x06;   /* R6 - other parameters and temporaries. */
164                 pxTopOfStack--;
165                 *pxTopOfStack = ( StackType_t ) 0x07;   /* R7 - other parameters and temporaries. */
166                 pxTopOfStack--;
167                 *pxTopOfStack = ( StackType_t ) NULL;   /* R8 - other parameters and temporaries. */
168                 pxTopOfStack--;
169                 *pxTopOfStack = ( StackType_t ) 0x09;   /* R9 - other parameters and temporaries. */
170                 pxTopOfStack--;
171                 *pxTopOfStack = ( StackType_t ) 0x0a;   /* R10 - other parameters and temporaries. */
172                 pxTopOfStack--;
173                 *pxTopOfStack = ( StackType_t ) 0x0b;   /* R11 - temporaries. */
174                 pxTopOfStack--;
175                 *pxTopOfStack = ( StackType_t ) 0x0c;   /* R12 - temporaries. */
176                 pxTopOfStack--;
177         #else
178                 pxTopOfStack-= 8;
179         #endif
180
181         *pxTopOfStack = ( StackType_t ) ulR13;  /* R13 - read/write small data area. */
182         pxTopOfStack--;
183         *pxTopOfStack = ( StackType_t ) pxCode; /* R14 - return address for interrupt. */
184         pxTopOfStack--;
185         *pxTopOfStack = ( StackType_t ) _start1;        /* R15 - return address for subroutine. */
186
187         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
188                 pxTopOfStack--;
189                 *pxTopOfStack = ( StackType_t ) 0x10;   /* R16 - return address for trap (debugger). */
190                 pxTopOfStack--;
191                 *pxTopOfStack = ( StackType_t ) 0x11;   /* R17 - return address for exceptions, if configured. */
192                 pxTopOfStack--;
193                 *pxTopOfStack = ( StackType_t ) 0x12;   /* R18 - reserved for assembler and compiler temporaries. */
194                 pxTopOfStack--;
195         #else
196                 pxTopOfStack -= 4;
197         #endif
198
199         *pxTopOfStack = ( StackType_t ) 0x00;   /* R19 - must be saved across function calls. Callee-save.  Seems to be interpreted as the frame pointer. */
200
201         #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
202                 pxTopOfStack--;
203                 *pxTopOfStack = ( StackType_t ) 0x14;   /* R20 - reserved for storing a pointer to the Global Offset Table (GOT) in Position Independent Code (PIC). Non-volatile in non-PIC code. Must be saved across function calls. Callee-save.  Not used by FreeRTOS. */
204                 pxTopOfStack--;
205                 *pxTopOfStack = ( StackType_t ) 0x15;   /* R21 - must be saved across function calls. Callee-save. */
206                 pxTopOfStack--;
207                 *pxTopOfStack = ( StackType_t ) 0x16;   /* R22 - must be saved across function calls. Callee-save. */
208                 pxTopOfStack--;
209                 *pxTopOfStack = ( StackType_t ) 0x17;   /* R23 - must be saved across function calls. Callee-save. */
210                 pxTopOfStack--;
211                 *pxTopOfStack = ( StackType_t ) 0x18;   /* R24 - must be saved across function calls. Callee-save. */
212                 pxTopOfStack--;
213                 *pxTopOfStack = ( StackType_t ) 0x19;   /* R25 - must be saved across function calls. Callee-save. */
214                 pxTopOfStack--;
215                 *pxTopOfStack = ( StackType_t ) 0x1a;   /* R26 - must be saved across function calls. Callee-save. */
216                 pxTopOfStack--;
217                 *pxTopOfStack = ( StackType_t ) 0x1b;   /* R27 - must be saved across function calls. Callee-save. */
218                 pxTopOfStack--;
219                 *pxTopOfStack = ( StackType_t ) 0x1c;   /* R28 - must be saved across function calls. Callee-save. */
220                 pxTopOfStack--;
221                 *pxTopOfStack = ( StackType_t ) 0x1d;   /* R29 - must be saved across function calls. Callee-save. */
222                 pxTopOfStack--;
223                 *pxTopOfStack = ( StackType_t ) 0x1e;   /* R30 - must be saved across function calls. Callee-save. */
224                 pxTopOfStack--;
225                 *pxTopOfStack = ( StackType_t ) 0x1f;   /* R31 - must be saved across function calls. Callee-save. */
226                 pxTopOfStack--;
227         #else
228                 pxTopOfStack -= 13;
229         #endif
230
231         /* Return a pointer to the top of the stack that has been generated so this
232         can     be stored in the task control block for the task. */
233         return pxTopOfStack;
234 }
235 /*-----------------------------------------------------------*/
236
237 BaseType_t xPortStartScheduler( void )
238 {
239 extern void ( vPortStartFirstTask )( void );
240 extern uint32_t _stack[];
241
242         /* Setup the hardware to generate the tick.  Interrupts are disabled when
243         this function is called.
244
245         This port uses an application defined callback function to install the tick
246         interrupt handler because the kernel will run on lots of different
247         MicroBlaze and FPGA configurations - not all of which will have the same
248         timer peripherals defined or available.  An example definition of
249         vApplicationSetupTimerInterrupt() is provided in the official demo
250         application that accompanies this port. */
251         vApplicationSetupTimerInterrupt();
252
253         /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
254         pulISRStack = ( uint32_t * ) _stack;
255
256         /* Ensure there is enough space for the functions called from the interrupt
257         service routines to write back into the stack frame of the caller. */
258         pulISRStack -= 2;
259
260         /* Restore the context of the first task that is going to run.  From here
261         on, the created tasks will be executing. */
262         vPortStartFirstTask();
263
264         /* Should not get here as the tasks are now running! */
265         return pdFALSE;
266 }
267 /*-----------------------------------------------------------*/
268
269 void vPortEndScheduler( void )
270 {
271         /* Not implemented in ports where there is nothing to return to.
272         Artificially force an assert. */
273         configASSERT( uxCriticalNesting == 1000UL );
274 }
275 /*-----------------------------------------------------------*/
276
277 /*
278  * Manual context switch called by portYIELD or taskYIELD.
279  */
280 void vPortYield( void )
281 {
282 extern void VPortYieldASM( void );
283
284         /* Perform the context switch in a critical section to assure it is
285         not interrupted by the tick ISR.  It is not a problem to do this as
286         each task maintains its own interrupt status. */
287         portENTER_CRITICAL();
288         {
289                 /* Jump directly to the yield function to ensure there is no
290                 compiler generated prologue code. */
291                 asm volatile (  "bralid r14, VPortYieldASM              \n\t" \
292                                                 "or r0, r0, r0                                  \n\t" );
293         }
294         portEXIT_CRITICAL();
295 }
296 /*-----------------------------------------------------------*/
297
298 void vPortEnableInterrupt( uint8_t ucInterruptID )
299 {
300 int32_t lReturn;
301
302         /* An API function is provided to enable an interrupt in the interrupt
303         controller because the interrupt controller instance variable is private
304         to this file. */
305         lReturn = prvEnsureInterruptControllerIsInitialised();
306         if( lReturn == pdPASS )
307         {
308                 /* Critical section protects read/modify/writer operation inside
309                 XIntc_Enable(). */
310                 portENTER_CRITICAL();
311                 {
312                         XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
313                 }
314                 portEXIT_CRITICAL();
315         }
316
317         configASSERT( lReturn );
318 }
319 /*-----------------------------------------------------------*/
320
321 void vPortDisableInterrupt( uint8_t ucInterruptID )
322 {
323 int32_t lReturn;
324
325         /* An API function is provided to disable an interrupt in the interrupt
326         controller because the interrupt controller instance variable is private
327         to this file. */
328         lReturn = prvEnsureInterruptControllerIsInitialised();
329
330         if( lReturn == pdPASS )
331         {
332                 XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
333         }
334
335         configASSERT( lReturn );
336 }
337 /*-----------------------------------------------------------*/
338
339 BaseType_t xPortInstallInterruptHandler( uint8_t ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
340 {
341 int32_t lReturn;
342
343         /* An API function is provided to install an interrupt handler because the
344         interrupt controller instance variable is private to this file. */
345
346         lReturn = prvEnsureInterruptControllerIsInitialised();
347
348         if( lReturn == pdPASS )
349         {
350                 lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
351         }
352
353         if( lReturn == XST_SUCCESS )
354         {
355                 lReturn = pdPASS;
356         }
357
358         configASSERT( lReturn == pdPASS );
359
360         return lReturn;
361 }
362 /*-----------------------------------------------------------*/
363
364 static int32_t prvEnsureInterruptControllerIsInitialised( void )
365 {
366 static int32_t lInterruptControllerInitialised = pdFALSE;
367 int32_t lReturn;
368
369         /* Ensure the interrupt controller instance variable is initialised before
370         it is used, and that the initialisation only happens once. */
371         if( lInterruptControllerInitialised != pdTRUE )
372         {
373                 lReturn = prvInitialiseInterruptController();
374
375                 if( lReturn == pdPASS )
376                 {
377                         lInterruptControllerInitialised = pdTRUE;
378                 }
379         }
380         else
381         {
382                 lReturn = pdPASS;
383         }
384
385         return lReturn;
386 }
387 /*-----------------------------------------------------------*/
388
389 /*
390  * Handler for the timer interrupt.  This is the handler that the application
391  * defined callback function vApplicationSetupTimerInterrupt() should install.
392  */
393 void vPortTickISR( void *pvUnused )
394 {
395 extern void vApplicationClearTimerInterrupt( void );
396
397         /* Ensure the unused parameter does not generate a compiler warning. */
398         ( void ) pvUnused;
399
400         /* This port uses an application defined callback function to clear the tick
401         interrupt because the kernel will run on lots of different MicroBlaze and
402         FPGA configurations - not all of which will have the same timer peripherals
403         defined or available.  An example definition of
404         vApplicationClearTimerInterrupt() is provided in the official demo
405         application that accompanies this port. */
406         vApplicationClearTimerInterrupt();
407
408         /* Increment the RTOS tick - this might cause a task to unblock. */
409         if( xTaskIncrementTick() != pdFALSE )
410         {
411                 /* Force vTaskSwitchContext() to be called as the interrupt exits. */
412                 ulTaskSwitchRequested = 1;
413         }
414 }
415 /*-----------------------------------------------------------*/
416
417 static int32_t prvInitialiseInterruptController( void )
418 {
419 int32_t lStatus;
420
421         lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
422
423         if( lStatus == XST_SUCCESS )
424         {
425                 /* Initialise the exception table. */
426                 Xil_ExceptionInit();
427
428             /* Service all pending interrupts each time the handler is entered. */
429             XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
430
431             /* Install exception handlers if the MicroBlaze is configured to handle
432             exceptions, and the application defined constant
433             configINSTALL_EXCEPTION_HANDLERS is set to 1. */
434                 #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
435             {
436                 vPortExceptionsInstallHandlers();
437             }
438                 #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
439
440                 /* Start the interrupt controller.  Interrupts are enabled when the
441                 scheduler starts. */
442                 lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
443
444                 if( lStatus == XST_SUCCESS )
445                 {
446                         lStatus = pdPASS;
447                 }
448                 else
449                 {
450                         lStatus = pdFAIL;
451                 }
452         }
453
454         configASSERT( lStatus == pdPASS );
455
456         return lStatus;
457 }
458 /*-----------------------------------------------------------*/
459
460