1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
36 * @addtogroup scugic_v3_8
39 * This file contains low-level driver functions that can be used to access the
40 * device. The user should refer to the hardware device specification for more
41 * details of the device operation.
42 * These routines are used when the user does not want to create an instance of
43 * XScuGic structure but still wants to use the ScuGic device. Hence the
44 * routines provided here take device id or scugic base address as arguments.
45 * Separate static versions of DistInit and CPUInit are provided to implement
46 * the low level driver routines.
49 * MODIFICATION HISTORY:
51 * Ver Who Date Changes
52 * ----- ---- -------- -------------------------------------------------------
53 * 1.01a sdm 07/18/11 First release
54 * 1.03a srt 02/27/13 Moved Offset calculation macros from *_hw.c (CR
56 * Added support to direct interrupts to the appropriate CPU.
57 * Earlier interrupts were directed to CPU1 (hard coded). Now
58 * depending upon the CPU selected by the user (xparameters.h),
59 * interrupts will be directed to the relevant CPU.
60 * This fixes CR 699688.
61 * 1.04a hk 05/04/13 Fix for CR#705621. Moved functions
62 * XScuGic_SetPriTrigTypeByDistAddr and
63 * XScuGic_GetPriTrigTypeByDistAddr here from xscugic.c
64 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
65 * 3.6 kvn 02/17/17 Add support for changing GIC CPU master at run time.
66 * kvn 02/28/17 Make the CpuId as static variable and Added new
67 * XScugiC_GetCpuId to access CpuId.
68 * 3.9 mus 02/21/18 Added new API's XScuGic_InterruptUnmapFromCpuByDistAddr
69 * and XScuGic_UnmapAllInterruptsFromCpuByDistAddr, These
70 * API's can be used by applications to unmap specific/all
71 * interrupts from target CPU. It fixes CR#992490.
75 ******************************************************************************/
78 /***************************** Include Files *********************************/
80 #include "xil_types.h"
81 #include "xil_assert.h"
83 #include "xparameters.h"
85 /************************** Constant Definitions *****************************/
87 /**************************** Type Definitions *******************************/
89 /***************** Macros (Inline Functions) Definitions *********************/
91 /************************** Function Prototypes ******************************/
93 static void DistInit(XScuGic_Config *Config, u32 CpuID);
94 static void CPUInit(XScuGic_Config *Config);
95 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress);
97 /************************** Variable Definitions *****************************/
99 extern XScuGic_Config XScuGic_ConfigTable[XPAR_XSCUGIC_NUM_INSTANCES];
102 /*****************************************************************************/
105 * DistInit initializes the distributor of the GIC. The
106 * initialization entails:
108 * - Write the trigger mode, priority and target CPU
109 * - All interrupt sources are disabled
110 * - Enable the distributor
112 * @param InstancePtr is a pointer to the XScuGic instance.
113 * @param CpuID is the Cpu ID to be initialized.
119 ******************************************************************************/
120 static void DistInit(XScuGic_Config *Config, u32 CpuID)
123 u32 LocalCpuID = CpuID;
126 #warning "Building GIC for AMP"
129 * The distrubutor should not be initialized by FreeRTOS in the case of
130 * AMP -- it is assumed that Linux is the master of this device in that
136 XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET, 0U);
139 * Set the security domains in the int_security registers for non-secure
140 * interrupts. All are secure, so leave at the default. Set to 1 for
141 * non-secure interrupts.
146 * For the Shared Peripheral Interrupts INT_ID[MAX..32], set:
150 * 1. The trigger mode in the int_config register
151 * Only write to the SPI interrupts, so start at 32
153 for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+16U) {
155 * Each INT_ID uses two bits, or 16 INT_ID per register
156 * Set them all to be level sensitive, active HIGH.
158 XScuGic_WriteReg(Config->DistBaseAddress,
159 XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id), 0U);
163 #define DEFAULT_PRIORITY 0xa0a0a0a0U
164 for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
166 * 2. The priority using int the priority_level register
167 * The priority_level and spi_target registers use one byte per
169 * Write a default value that can be changed elsewhere.
171 XScuGic_WriteReg(Config->DistBaseAddress,
172 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
176 for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U) {
178 * 3. The CPU interface in the spi_target register
179 * Only write to the SPI interrupts, so start at 32
181 LocalCpuID |= LocalCpuID << 8U;
182 LocalCpuID |= LocalCpuID << 16U;
184 XScuGic_WriteReg(Config->DistBaseAddress,
185 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), LocalCpuID);
188 for (Int_Id = 0U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+32U) {
190 * 4. Enable the SPI using the enable_set register. Leave all disabled
193 XScuGic_WriteReg(Config->DistBaseAddress,
194 XSCUGIC_EN_DIS_OFFSET_CALC(XSCUGIC_DISABLE_OFFSET,
200 XScuGic_WriteReg(Config->DistBaseAddress, XSCUGIC_DIST_EN_OFFSET,
201 XSCUGIC_EN_INT_MASK);
205 /*****************************************************************************/
208 * CPUInit initializes the CPU Interface of the GIC. The initialization entails:
210 * - Set the priority of the CPU.
211 * - Enable the CPU interface
213 * @param ConfigPtr is a pointer to a config table for the particular
214 * device this driver is associated with.
220 ******************************************************************************/
221 static void CPUInit(XScuGic_Config *Config)
224 * Program the priority mask of the CPU using the Priority mask
227 XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CPU_PRIOR_OFFSET,
231 * If the CPU operates in both security domains, set parameters in the
232 * control_s register.
233 * 1. Set FIQen=1 to use FIQ for secure interrupts,
234 * 2. Program the AckCtl bit
235 * 3. Program the SBPR bit to select the binary pointer behavior
236 * 4. Set EnableS = 1 to enable secure interrupts
237 * 5. Set EnbleNS = 1 to enable non secure interrupts
241 * If the CPU operates only in the secure domain, setup the
242 * control_s register.
244 * 2. Set EnableS=1, to enable the CPU interface to signal secure .
245 * interrupts Only enable the IRQ output unless secure interrupts
248 XScuGic_WriteReg(Config->CpuBaseAddress, XSCUGIC_CONTROL_OFFSET, 0x07U);
252 /*****************************************************************************/
255 * CfgInitialize a specific interrupt controller instance/driver. The
256 * initialization entails:
258 * - Initialize fields of the XScuGic structure
259 * - Initial vector table with stub function calls
260 * - All interrupt sources are disabled
262 * @param InstancePtr is a pointer to the XScuGic instance to be worked on.
263 * @param ConfigPtr is a pointer to a config table for the particular device
264 * this driver is associated with.
265 * @param EffectiveAddr is the device base address in the virtual memory address
266 * space. The caller is responsible for keeping the address mapping
267 * from EffectiveAddr to the device physical base address unchanged
268 * once this function is invoked. Unexpected errors may occur if the
269 * address mapping changes after this function is called. If address
270 * translation is not used, use Config->BaseAddress for this parameters,
271 * passing the physical address instead.
275 * - XST_SUCCESS if initialization was successful
281 ******************************************************************************/
282 s32 XScuGic_DeviceInitialize(u32 DeviceId)
284 XScuGic_Config *Config;
285 u32 Cpu_Id = XScuGic_GetCpuID() + (u32)1;
287 Config = &XScuGic_ConfigTable[(u32 )DeviceId];
289 DistInit(Config, Cpu_Id);
296 /*****************************************************************************/
298 * This function is the primary interrupt handler for the driver. It must be
299 * connected to the interrupt source such that it is called when an interrupt of
300 * the interrupt controller is active. It will resolve which interrupts are
301 * active and enabled and call the appropriate interrupt handler. It uses
302 * the Interrupt Type information to determine when to acknowledge the
303 * interrupt.Highest priority interrupts are serviced first.
305 * This function assumes that an interrupt vector table has been previously
306 * initialized. It does not verify that entries in the table are valid before
307 * calling an interrupt handler.
309 * @param DeviceId is the unique identifier for the ScuGic device.
315 ******************************************************************************/
316 void XScuGic_DeviceInterruptHandler(void *DeviceId)
321 XScuGic_VectorTableEntry *TablePtr;
322 XScuGic_Config *CfgPtr;
324 CfgPtr = &XScuGic_ConfigTable[(INTPTR )DeviceId];
327 * Read the int_ack register to identify the highest priority
328 * interrupt ID and make sure it is valid. Reading Int_Ack will
329 * clear the interrupt in the GIC.
331 IntIDFull = XScuGic_ReadReg(CfgPtr->CpuBaseAddress, XSCUGIC_INT_ACK_OFFSET);
332 InterruptID = IntIDFull & XSCUGIC_ACK_INTID_MASK;
333 if(XSCUGIC_MAX_NUM_INTR_INPUTS < InterruptID){
338 * If the interrupt is shared, do some locking here if there are
339 * multiple processors.
342 * If pre-eption is required:
343 * Re-enable pre-emption by setting the CPSR I bit for non-secure ,
344 * interrupts or the F bit for secure interrupts
348 * If we need to change security domains, issue a SMC instruction here.
352 * Execute the ISR. Jump into the Interrupt service routine based on
353 * the IRQSource. A software trigger is cleared by the ACK.
355 TablePtr = &(CfgPtr->HandlerTable[InterruptID]);
356 if(TablePtr != NULL) {
357 TablePtr->Handler(TablePtr->CallBackRef);
362 * Write to the EOI register, we are all done here.
363 * Let this function return, the boot code will restore the stack.
365 XScuGic_WriteReg(CfgPtr->CpuBaseAddress, XSCUGIC_EOI_OFFSET, IntIDFull);
368 * Return from the interrupt. Change security domains could happen
373 /*****************************************************************************/
376 * Register a handler function for a specific interrupt ID. The vector table
377 * of the interrupt controller is updated, overwriting any previous handler.
378 * The handler function will be called when an interrupt occurs for the given
381 * @param BaseAddress is the CPU Interface Register base address of the
382 * interrupt controller whose vector table will be modified.
383 * @param InterruptId is the interrupt ID to be associated with the input
385 * @param Handler is the function pointer that will be added to
386 * the vector table for the given interrupt ID.
387 * @param CallBackRef is the argument that will be passed to the new
388 * handler function when it is called. This is user-specific.
394 * Note that this function has no effect if the input base address is invalid.
396 ******************************************************************************/
397 void XScuGic_RegisterHandler(u32 BaseAddress, s32 InterruptID,
398 Xil_InterruptHandler IntrHandler, void *CallBackRef)
400 XScuGic_Config *CfgPtr;
401 CfgPtr = LookupConfigByBaseAddress(BaseAddress);
404 if( IntrHandler != NULL) {
405 CfgPtr->HandlerTable[InterruptID].Handler = IntrHandler;
407 if( CallBackRef != NULL) {
408 CfgPtr->HandlerTable[InterruptID].CallBackRef = CallBackRef;
413 /*****************************************************************************/
416 * Looks up the device configuration based on the CPU interface base address of
417 * the device. A table contains the configuration info for each device in the
420 * @param CpuBaseAddress is the CPU Interface Register base address.
422 * @return A pointer to the configuration structure for the specified
423 * device, or NULL if the device was not found.
427 ******************************************************************************/
428 static XScuGic_Config *LookupConfigByBaseAddress(u32 CpuBaseAddress)
430 XScuGic_Config *CfgPtr = NULL;
433 for (Index = 0U; Index < XPAR_SCUGIC_NUM_INSTANCES; Index++) {
434 if (XScuGic_ConfigTable[Index].CpuBaseAddress ==
436 CfgPtr = &XScuGic_ConfigTable[Index];
441 return (XScuGic_Config *)CfgPtr;
444 /****************************************************************************/
446 * Sets the interrupt priority and trigger type for the specificd IRQ source.
448 * @param BaseAddr is the device base address
449 * @param Int_Id is the IRQ source number to modify
450 * @param Priority is the new priority for the IRQ source. 0 is highest
451 * priority, 0xF8 (248) is lowest. There are 32 priority levels
452 * supported with a step of 8. Hence the supported priorities are
453 * 0, 8, 16, 32, 40 ..., 248.
454 * @param Trigger is the new trigger type for the IRQ source.
455 * Each bit pair describes the configuration for an INT_ID.
456 * SFI Read Only b10 always
457 * PPI Read Only depending on how the PPIs are configured.
458 * b01 Active HIGH level sensitive
459 * b11 Rising edge sensitive
460 * SPI LSB is read only.
461 * b01 Active HIGH level sensitive
462 * b11 Rising edge sensitive/
466 * @note This API has the similar functionality of XScuGic_SetPriority
467 * TriggerType() and should be used when there is no InstancePtr.
469 *****************************************************************************/
470 void XScuGic_SetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
471 u8 Priority, u8 Trigger)
474 u8 LocalPriority = Priority;
476 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
477 Xil_AssertVoid(Trigger <= XSCUGIC_INT_CFG_MASK);
478 Xil_AssertVoid(LocalPriority <= XSCUGIC_MAX_INTR_PRIO_VAL);
481 * Determine the register to write to using the Int_Id.
483 RegValue = XScuGic_ReadReg(DistBaseAddress,
484 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
487 * The priority bits are Bits 7 to 3 in GIC Priority Register. This
488 * means the number of priority levels supported are 32 and they are
489 * in steps of 8. The priorities can be 0, 8, 16, 32, 48, ... etc.
490 * The lower order 3 bits are masked before putting it in the register.
492 LocalPriority = LocalPriority & XSCUGIC_INTR_PRIO_MASK;
494 * Shift and Mask the correct bits for the priority and trigger in the
497 RegValue &= ~(XSCUGIC_PRIORITY_MASK << ((Int_Id%4U)*8U));
498 RegValue |= (u32)LocalPriority << ((Int_Id%4U)*8U);
501 * Write the value back to the register.
503 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id),
506 * Determine the register to write to using the Int_Id.
508 RegValue = XScuGic_ReadReg(DistBaseAddress,
509 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
512 * Shift and Mask the correct bits for the priority and trigger in the
515 RegValue &= ~(XSCUGIC_INT_CFG_MASK << ((Int_Id%16U)*2U));
516 RegValue |= (u32)Trigger << ((Int_Id%16U)*2U);
519 * Write the value back to the register.
521 XScuGic_WriteReg(DistBaseAddress, XSCUGIC_INT_CFG_OFFSET_CALC(Int_Id),
525 /****************************************************************************/
527 * Gets the interrupt priority and trigger type for the specificd IRQ source.
529 * @param BaseAddr is the device base address
530 * @param Int_Id is the IRQ source number to modify
531 * @param Priority is a pointer to the value of the priority of the IRQ
532 * source. This is a return value.
533 * @param Trigger is pointer to the value of the trigger of the IRQ
534 * source. This is a return value.
538 * @note This API has the similar functionality of XScuGic_GetPriority
539 * TriggerType() and should be used when there is no InstancePtr.
541 *****************************************************************************/
542 void XScuGic_GetPriTrigTypeByDistAddr(u32 DistBaseAddress, u32 Int_Id,
543 u8 *Priority, u8 *Trigger)
547 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
548 Xil_AssertVoid(Priority != NULL);
549 Xil_AssertVoid(Trigger != NULL);
552 * Determine the register to read to using the Int_Id.
554 RegValue = XScuGic_ReadReg(DistBaseAddress,
555 XSCUGIC_PRIORITY_OFFSET_CALC(Int_Id));
558 * Shift and Mask the correct bits for the priority and trigger in the
561 RegValue = RegValue >> ((Int_Id%4U)*8U);
562 *Priority = (u8)(RegValue & XSCUGIC_PRIORITY_MASK);
565 * Determine the register to read to using the Int_Id.
567 RegValue = XScuGic_ReadReg(DistBaseAddress,
568 XSCUGIC_INT_CFG_OFFSET_CALC (Int_Id));
571 * Shift and Mask the correct bits for the priority and trigger in the
574 RegValue = RegValue >> ((Int_Id%16U)*2U);
576 *Trigger = (u8)(RegValue & XSCUGIC_INT_CFG_MASK);
579 /****************************************************************************/
581 * Unmaps specific SPI interrupt from the target CPU
583 * @param DistBaseAddress is the device base address
584 * @param Cpu_Id is a CPU number from which the interrupt has to be
586 * @param Int_Id is the IRQ source number to modify
592 *****************************************************************************/
593 void XScuGic_InterruptUnmapFromCpuByDistAddr(u32 DistBaseAddress,
594 u8 Cpu_Id, u32 Int_Id)
599 Xil_AssertVoid(Int_Id < XSCUGIC_MAX_NUM_INTR_INPUTS);
601 RegValue = XScuGic_ReadReg(DistBaseAddress,
602 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id));
605 * Identify bit position corresponding to Int_Id and Cpu_Id,
606 * in interrupt target register and clear it
608 BitPos = ((Int_Id % 4U) * 8U) + Cpu_Id;
609 RegValue &= (~ ( 1U << BitPos ));
610 XScuGic_WriteReg(DistBaseAddress,
611 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), RegValue);
614 /****************************************************************************/
616 * Unmaps all SPI interrupts from the target CPU
618 * @param DistBaseAddress is the device base address
619 * @param Cpu_Id is a CPU number from which the interrupts has to be
626 *****************************************************************************/
627 void XScuGic_UnmapAllInterruptsFromCpuByDistAddr(u32 DistBaseAddress,
632 u32 LocalCpuID = (1U << Cpu_Id);
634 LocalCpuID |= LocalCpuID << 8U;
635 LocalCpuID |= LocalCpuID << 16U;
637 for (Int_Id = 32U; Int_Id<XSCUGIC_MAX_NUM_INTR_INPUTS;Int_Id=Int_Id+4U)
640 Target_Cpu = XScuGic_ReadReg(DistBaseAddress,
641 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id));
642 /* Remove LocalCpuID from interrupt target register */
643 Target_Cpu &= (~LocalCpuID);
644 XScuGic_WriteReg(DistBaseAddress,
645 XSCUGIC_SPI_TARGET_OFFSET_CALC(Int_Id), Target_Cpu);