source: rtems/c/src/lib/libbsp/powerpc/motorola_powerpc/irq/irq_init.c @ ba46ffa6
Last change on this file since ba46ffa6 was ba46ffa6, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 14, 1999 at 4:51:13 PM

This is a large patch from Eric Valette <valette@…> that was
described in the message following this paragraph. This patch also includes
a mcp750 BSP.

From valette@… Mon Jun 14 10:03:08 1999
Date: Tue, 18 May 1999 01:30:14 +0200 (CEST)
From: VALETTE Eric <valette@…>
To: joel@…
Cc: raguet@…, rtems-snapshots@…, valette@…
Subject: Questions/Suggestion? regarding RTEMS PowerPC code (long)

Dear knowledgeable RTEMS powerpc users,

As some of you may know, I'm currently finalizing a port
of RTEMS on a MCP750 Motorola board. I have done most
of it but have some questions to ask before submitting
the port.

In order to understand some of the changes I have made
or would like to make, maybe it is worth describing the
MCP750 Motorola board.

the MCP750 is a COMPACT PCI powerpc board with :

1) a MPC750 233 MHz processor,
2) a raven bus bridge/PCI controller that
implement an OPENPIC compliant interrupt controller,
3) a VIA 82C586 PCI/ISA bridge that offers a PC
compliant IO for keyboard, serial line, IDE, and
the well known PC 8259 cascaded PIC interrupt
architecture model,
4) a DEC 21140 Ethernet controller,
5) the PPCBUG Motorola firmware in flash,
6) A DEC PCI bridge,

This architecture is common to most Motorola 60x/7xx
board except that :

1) on VME board, the DEC PCI bridge is replaced by
a VME chipset,
2) the VIA 82C586 PCI/ISA bridge is replaced by
another bridge that is almost fully compatible
with the via bridge...

So the port should be a rather close basis for many
60x/7xx motorola board...

On this board, I already have ported Linux 2.2.3 and
use it both as a development and target board.

Now the questions/suggestions I have :


As far as I know exceptions on PPC are handled like
interrupts. I dislike this very much as :

a) Except for the decrementer exception (and
maybe some other on mpc8xx), exceptions are
not recoverable and the handler just need to print
the full context and go to the firmware or debugger...
b) The interrupt switch is only necessary for the
decrementer and external interrupt (at least on
c) The full context for exception is never saved and
thus cannot be used by debugger... I do understand
the most important for interrupts low level code
is to save the minimal context enabling to call C
code for performance reasons. On non recoverable
exception on the other hand, the most important is
to save the maximum information concerning proc status
in order to analyze the reason of the fault. At
least we will need this in order to implement the
port of RGDB on PPC

==> I wrote an API for connecting raw exceptions (and thus
raw interrupts) for mpc750. It should be valid for most
powerpc processors... I hope to find a way to make this coexist
with actual code layout. The code is actually located
in lib/libcpu/powerpc/mpc750 and is thus optional
(provided I write my own version of exec/score/cpu/powerpc/cpu.c ...)

See remark about files/directory layout organization in 4)

2) Current Implementation of ISR low level code

I do not understand why the MSR EE flags is cleared
again in exec/score/cpu/powerpc/irq_stubs.S


mfmsr r5
mfspr r6, sprg2


lwz r6,msr_initial(r11)
and r6,r6,r5
mfmsr r5


Reading the doc, when a decrementer interrupt or an
external interrupt is active, the MSR EE flag is already
cleared. BTW if exception/interrupt could occur, it would
trash SRR0 and SRR1. In fact the code may be useful to set
MSR[RI] that re-enables exception processing. BTW I will need
to set other value in MSR to handle interrupts :

a) I want the MSR[IR] and MSR[DR] to be set for
performance reasons and also because I need DBAT
support to have access to PCI memory space as the
interrupt controller is in the PCI space.

Reading the code, I see others have the same kind of request :

/* SCE 980217


  • We need address translation ON when we call our ISR routine

mtmsr r5


This is just another prof that even the lowest level
IRQ code is fundamentally board dependent and
not simply processor dependent especially when
the processor use external interrupt controller
because it has a single interrupt request line...

Note that if you look at the PPC code high level interrupt
handling code, as the "set_vector" routine that really connects
the interrupt is in the BSP/startup/genpvec.c,
the fact that IRQ handling is BSP specific is DE-FACTO

I know I have already expressed this and understand that this
would require some heavy change in the code but believe
me you will reach a point where you will not be able
to find a compatible while optimum implementation for low level
interrupt handling code...) In my case this is already true...

So please consider removing low level IRQ handling from
exec/score/cpu/* and only let there exception handling code...
Exceptions are usually only processor dependent and do
not depend on external hardware mechanism to be masked or
acknowledged or re-enabled (there are probably exception but ...)

I have already done this for pc386 bsp but need to make it again.
This time I will even propose an API.

3) R2/R13 manipulation for EABI implementation

I do not understand the handling of r2 and r13 in the
EABI case. The specification for r2 says pointer to sdata2,
sbss2 section => constant. However I do not see -ffixed-r2
passed to any compilation system in make/custom/*
(for info linux does this on PPC).

So either this is a default compiler option when choosing
powerpc-rtems and thus we do not need to do anything with
this register as all the code is compiled with this compiler
and linked together OR this register may be used by rtems code
and then we do not need any special initialization or

The specification for r13 says pointer to the small data
area. r13 argumentation is the same except that as far
as I know the usage of the small data area requires
specific compiler support so that access to variables is
compiled via loading the LSB in a register and then
using r13 to get full address... It is like a small
memory model and it was present in IBM C compilers.

=> I propose to suppress any specific code for r2 and
r13 in the EABI case.

4) Code layout organization (yes again :-))

I think there are a number of design flaws in the way
the code is for ppc organized and I will try to point them out.
I have been beaten by this again on this new port, and
was beaten last year while modifying code for pc386.

a) exec/score/cpu/* vs lib/libcpu/cpu/*.

I think that too many things are put in exec/score/cpu that
have nothing to do with RTEMS internals but are rather
related to CPU feature.

This include at least :

a) registers access routine (e.g GET_MSR_Value),
b) interrupt masking/unmasking routines,
c) cache_mngt_routine,
d) mmu_mngt_routine,
e) Routines to connect the raw_exception, raw_interrupt

b) lib/libcpu/cpu/powerpc/*

With a processor family as exuberant as the powerpc family,
and their well known subtle differences (604 vs 750) or
unfortunately majors (8xx vs 60x) the directory structure
is fine (except maybe the names that are not homogeneous)


ppc421 mpc821 ...

I only needed to add mpc750. But the fact that libcpu.a was not
produced was a pain and the fact that this organization may
duplicates code is also problematic.

So, except if the support of automake provides a better solution
I would like to propose something like this :


mpc421 mpc821 ... mpc750 shared wrapup

with the following rules :

a) "shared" would act as a source container for sources that may
be shared among processors. Needed files would be compiled inside
the processor specific directory using the vpath Makefile
mechanism. "shared" may also contain compilation code
for routine that are really shared and not worth to inline...
(did not found many things so far as registers access routine
ARE WORTH INLINING)... In the case something is compiled there,
it should create libcpushared.a

b) layout under processor specific directory is free provided

1)the result of the compilation process exports :

libcpu/powerpc/"PROC"/*.h in $(PROJECT_INCLUDE)/libcpu

2) each processor specific directory creates
a library called libcpuspecific.a

Note that this organization enables to have a file that
is nearly the same than in shared but that must differ
because of processor differences...

c) "wrapup" should create libcpu.a using libcpushared.a
libcpuspecific.a and export it $(PROJECT_INCLUDE)/libcpu

The only thing I have no ideal solution is the way to put shared
definitions in "shared" and only processor specific definition
in "proc". To give a concrete example, most MSR bit definition
are shared among PPC processors and only some differs. if we create
a single msr.h in shared it will have ifdef. If in msr.h we
include libcpu/msr_c.h we will need to have it in each prowerpc
specific directory (even empty). Opinions are welcomed ...

Note that a similar mechanism exist in libbsp/i386 that also
contains a shared directory that is used by several bsp
like pc386 and i386ex and a similar wrapup mechanism...

NB: I have done this for mpc750 and other processors could just use
similar Makefiles...

c) The exec/score/cpu/powerpc directory layout.

I think the directory layout should be the same than the
libcpu/powerpc. As it is not, there are a lot of ifdefs
inside the code... And of course low level interrupt handling
code should be removed...

Besides that I do not understand why

1) things are compiled in the wrap directory,
2) some includes are moved to rtems/score,

I think the "preinstall" mechanism enables to put
everything in the current directory (or better in a per processor

5) Interrupt handling API

Again :-). But I think that using all the features the PIC
offers is a MUST for RT system. I already explained in the
prologue of this (long and probably boring) mail that the MCP750
boards offers an OPENPIC compliant architecture and that
the VIA 82586 PCI/ISA bridge offers a PC compatible IO and
PIC mapping. Here is a logical view of the RAVEN/VIA 82586
interrupt mapping :

| OPEN | <-----|8259|
| PIC | | | 2 ------
|(RAVEN)| | | <-----|8259|
| | | | | | 11
| | | | | | <----
| | | | | |
| | | | | |

| VIA PCI/ISA bridge
| x
-------- PCI interrupts

OPENPIC offers interrupt priorities among PCI interrupts
and interrupt selective masking. The 8259 offers the same kind
of feature. With actual powerpc interrupt code :

1) there is no way to specify priorities among
interrupts handler. This is REALLY a bad thing.
For me it is as importnat as having priorities
for threads...
2) for my implementation, each ISR should
contain the code that acknowledge the RAVEN
and 8259 cascade, modify interrupt mask on both
chips, and reenable interrupt at processor level,
..., restore then on interrupt return,.... This code
is actually similar to code located in some
genpvec.c powerpc files,
3) I must update _ISR_Nesting_level because
irq.inl use it...
4) the libchip code connects the ISR via set_vector
but the libchip handler code does not contain any code to
manipulate external interrupt controller hardware
in order to acknoledge the interrupt or re-enable
them (except for the target hardware of course)
So this code is broken unless set_vector adds an
additionnal prologue/epilogue before calling/returning
from in order to acknoledge/mask the raven and the
8259 PICS... => Anyway already EACH BSP MUST REWRITE

I would rather offer an API similar to the one provided
in libbsp/i386/shared/irq/irq.h so that :

1) Once the driver supplied methods is called the
only things the ISR has to do is to worry about the
external hardware that triggered the interrupt.
Everything on openpic/VIA/processor would have been
done by the low levels (same things as set-vector)
2) The caller will need to supply the on/off/isOn
routine that are fundamental to correctly implements
debuggers/performance monitoring is a portable way
3) A globally configurable interrupt priorities

I have nothing against providing a compatible
set_vector just to make libchip happy but
as I have already explained in other
mails (months ago), I really think that the ISR
connection should be handled by the BSP and that no
code containing irq connection should exist the
rtems generic layers... Thus I really dislike
libchip on this aspect because in a long term
it will force to adopt the less reach API
for interrupt handling that exists (set_vector).

Additional note : I think the _ISR_Is_in_progress()
inline routine should be :

1) Put in a processor specific section,
2) Should not rely on a global variable,

As :

a) on symmetric MP, there is one interrupt level
per CPU,
b) On processor that have an ISP (e,g 68040),
this variable is useless (MSR bit testing could
be used)
c) On PPC, instead of using the address of the
variable via CPU_IRQ_info.Nest_level a dedicated
SPR could be used.

NOTE: most of this is also true for _Thread_Dispatch_disable_level


Please do not take what I said in the mail as a criticism for
anyone who submitted ppc code. Any code present helped me
a lot understanding PPC behavior. I just wanted by this
mail to :

1) try to better understand the actual code,
2) propose concrete ways of enhancing current code
by providing an alternative implementation for MCP750. I
will make my best effort to try to brake nothing but this
is actually hard due to the file layout organisation.
3) make understandable some changes I will probably make
if joel let me do them :-)

Any comments/objections are welcomed as usual.


/ ` Eric Valette

/-- o _. Canon CRF

(_, / (_(_( Rue de la touche lambert

35517 Cesson-Sevigne Cedex

Tel: +33 (0)2 99 87 68 91 Fax: +33 (0)2 99 84 11 30
E-mail: valette@…

  • Property mode set to 100644
File size: 9.4 KB
1/* irq_init.c
2 *
3 *  This file contains the implementation of rtems initialization
4 *  related to interrupt handling.
5 *
6 *  CopyRight (C) 1999
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *
11 *
12 *  $Id$
13 */
14#include <bsp/consoleIo.h>
15#include <libcpu/io.h>
16#include <libcpu/spr.h>
17#include <bsp/pci.h>
18#include <bsp/residual.h>
19#include <bsp/openpic.h>
20#include <bsp/irq.h>
21#include <bsp.h>
22#include <libcpu/raw_exception.h>
24typedef struct {
25  unsigned char bus;    /* few chance the PCI/ISA bridge is not on first bus but ... */
26  unsigned char device;
27  unsigned char function;
28} pci_isa_bridge_device;
30pci_isa_bridge_device* via_82c586 = 0;
31static pci_isa_bridge_device bridge;
33extern unsigned int external_exception_vector_prolog_code_size;
34extern void external_exception_vector_prolog_code();
35extern unsigned int decrementer_exception_vector_prolog_code_size;
36extern void decrementer_exception_vector_prolog_code();
39 * default on/off function
40 */
41static void nop_func(){}
43 * default isOn function
44 */
45static int not_connected() {return 0;}
47 * default possible isOn function
48 */
49static int connected() {return 1;}
51static rtems_irq_connect_data           rtemsIrq[BSP_IRQ_NUMBER];
52static rtems_irq_global_settings        initial_config;
53static rtems_irq_connect_data           defaultIrq = {
54  /* vectorIdex,         hdl            , on            , off           , isOn */
55  0,                     nop_func       , nop_func      , nop_func      , not_connected
57static rtems_irq_prio irqPrioTable[BSP_IRQ_NUMBER]={
58  /*
59   * actual rpiorities for interrupt :
60   *    0   means that only current interrupt is masked
61   *    255 means all other interrupts are masked
62   */
63  /*
64   * ISA interrupts.
65   * The second entry has a priority of 255 because
66   * it is the slave pic entry and is should always remain
67   * unmasked.
68   */
69  0,0,
70  255,
71  0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
72  /*
73   * PCI Interrupts
74   */
75  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* for raven prio 0 means unactive... */
76  /*
77   * Processor exceptions handled as interrupts
78   */
79  0
82static unsigned char mcp750_openpic_initsenses[] = {
83    1,  /* MCP750_INT_PCB(8259) */
84    0,  /* MCP750_INT_FALCON_ECC_ERR */
85    1,  /* MCP750_INT_PCI_ETHERNET */
86    1,  /* MCP750_INT_PCI_PMC */
87    1,  /* MCP750_INT_PCI_WATCHDOG_TIMER1 */
88    1,  /* MCP750_INT_PCI_PRST_SIGNAL */
89    1,  /* MCP750_INT_PCI_FALL_SIGNAL */
90    1,  /* MCP750_INT_PCI_DEG_SIGNAL */
91    1,  /* MCP750_INT_PCI_BUS1_INTA */
92    1,  /* MCP750_INT_PCI_BUS1_INTB */
93    1,  /* MCP750_INT_PCI_BUS1_INTC */
94    1,  /* MCP750_INT_PCI_BUS1_INTD */
95    1,  /* MCP750_INT_PCI_BUS2_INTA */
96    1,  /* MCP750_INT_PCI_BUS2_INTB */
97    1,  /* MCP750_INT_PCI_BUS2_INTC */
98    1,  /* MCP750_INT_PCI_BUS2_INTD */
101void isa_bridge_interrupts_setup(void)
103  pci_isa_bridge_device pci_dev;
104  unsigned long temp;
105  unsigned char tmp;
106  unsigned char maxBus;
107  unsigned found = 0;
109  maxBus = BusCountPCI();
110  pci_dev.function      = 0; /* Assumes the bidge is the first function */
112  for (pci_dev.bus = 0; pci_dev.bus < maxBus; pci_dev.bus++) {
113#ifdef SCAN_PCI_PRINT       
114    printk("isa_bridge_interrupts_setup: Scanning bus %d\n", pci_dev.bus);
116    for (pci_dev.device = 0; pci_dev.device < PCI_MAX_DEVICES; pci_dev.device++) {
117#ifdef SCAN_PCI_PRINT       
118      printk("isa_bridge_interrupts_setup: Scanning device %d\n", pci_dev.device);
120      pci_read_config_dword(pci_dev.bus, pci_dev.device,  pci_dev.function,
121                               PCI_VENDOR_ID, &temp);
122#ifdef SCAN_PCI_PRINT       
123      printk("Vendor/device = %x\n", temp);
125      if ( (temp == (((unsigned short) PCI_VENDOR_ID_VIA) | (PCI_DEVICE_ID_VIA_82C586_1 << 16)))
126           ||
127           (temp == (((unsigned short) PCI_VENDOR_ID_VIA) | (PCI_DEVICE_ID_VIA_82C586_0 << 16)))
128         ) {
129        bridge = pci_dev;
130        via_82c586 = &bridge;
131        /*
132         * Should print : bus = 0, device = 11, function = 0 on a MCP750.
133         */
134        printk("Via PCI/ISA bridge found at bus = %d, device = %d, function = %d\n",
135               via_82c586->bus,
136               via_82c586->device,
137               via_82c586->function);
138        found = 1;
139        goto loop_exit;
141      }
142    }
143  }
145  if (!found) BSP_panic("VIA_82C586 PCI/ISA bridge not found!n");
147  tmp = inb(0x810);
148  if  ( !(tmp & 0x2)) {
149    printk("This is a second generation MCP750 board\n");
150    printk("We must reprogram the PCI/ISA bridge...\n");
151    pci_read_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
152                         0x47,  &tmp);
153    printk(" PCI ISA bridge control2 = %x\n", (unsigned) tmp);
154    /*
155     * Enable 4D0/4D1 ISA interrupt level/edge config registers
156     */
157    tmp |= 0x20;
158    pci_write_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
159                          0x47, tmp);
160    /*
161     * Now program the ISA interrupt edge/level
162     */
164    outb(tmp, ISA8259_S_ELCR);
165    tmp = ELCRM_INT5_LVL;
166    outb(tmp, ISA8259_M_ELCR);;
167    /*
168     * Set the Interrupt inputs to non-inverting level interrupt
169     */
170    pci_read_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
171                            0x54, &tmp);
172    printk(" PCI ISA bridge PCI/IRQ Edge/Level Select = %x\n", (unsigned) tmp);
173    tmp = 0;
174    pci_write_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
175                          0x54, tmp);
176  }
177  else {
178    printk("This is a first generation MCP750 board\n");
179    printk("We just show the actual value used by PCI/ISA bridge\n");
180    pci_read_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
181                         0x47,  &tmp);
182    printk(" PCI ISA bridge control2 = %x\n", (unsigned) tmp);
183    /*
184     * Enable 4D0/4D1 ISA interrupt level/edge config registers
185     */
186    tmp |= 0x20;
187    pci_write_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
188                          0x47, tmp);
189    tmp = inb(ISA8259_S_ELCR);
190    printk(" PCI ISA bridge slave edge/level control bit = %x\n", (unsigned) tmp);
191    tmp = inb(ISA8259_M_ELCR);;
192    printk(" PCI ISA bridge master edge/level control bit = %x\n", (unsigned) tmp);
193    /*
194     * Must disable the 4D0/4D1 ISA interrupt level/edge config registers
195     * or the card will die a soon as we we will enable external interrupts
196     */
197    pci_read_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
198                         0x47,  &tmp);
199    tmp &= ~(0x20);
200    pci_write_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
201                          0x47, tmp);
202    /*
203     * Show the Interrupt inputs inverting/non-inverting level status
204     */
205    pci_read_config_byte(via_82c586->bus, via_82c586->device, via_82c586->function,
206                         0x54, &tmp);
207    printk(" PCI ISA bridge PCI/IRQ Edge/Level Select = %x\n", (unsigned) tmp);
208  }
211  /*
212   * This code assumes the exceptions management setup has already
213   * been done. We just need to replace the exceptions that will
214   * be handled like interrupt. On mcp750/mpc750 this means the
215   * decrementer exception and the external exception.
216   */
217void BSP_rtems_irq_mng_init(unsigned cpuId)
219  rtems_raw_except_connect_data vectorDesc;
220  int i;
221  register unsigned int msr;
223  /*
224   * First initialize the Interrupt management hardware
225   */
226  OpenPIC_InitSenses = mcp750_openpic_initsenses;
227  OpenPIC_NumInitSenses = sizeof(mcp750_openpic_initsenses) / sizeof(char);
228  printk("Going to initialize raven interrupt controller (openpic compliant)\n");
229  openpic_init(1);
230  printk("Going to initialize the PCI/ISA bridge IRQ related setting (VIA 82C586)\n");
231  isa_bridge_interrupts_setup();
232  printk("Going to initialize the ISA PC legacy IRQ management hardware\n");
233  BSP_i8259s_init();
234  /*
235   * Initialize Rtems management interrupt table
236   */
237    /*
238     * re-init the rtemsIrq table
239     */
240    for (i = 0; i < BSP_IRQ_NUMBER; i++) {
241      rtemsIrq[i]      = defaultIrq;
242      rtemsIrq[i].name = i;
243    }
244    /*
245     * Init initial Interrupt management config
246     */
247    initial_config.irqNb        = BSP_IRQ_NUMBER;
248    initial_config.defaultEntry = defaultIrq;
249    initial_config.irqHdlTbl    = rtemsIrq;
250    initial_config.irqBase      = BSP_ASM_IRQ_VECTOR_BASE;
251    initial_config.irqPrioTbl   = irqPrioTable;
253    if (!BSP_rtems_irq_mngt_set(&initial_config)) {
254      /*
255       * put something here that will show the failure...
256       */
257      BSP_panic("Unable to initialize RTEMS interrupt Management!!! System locked\n");
258    }
260  /*
261   * We must connect the raw irq handler for the two
262   * expected interrupt sources : decrementer and external interrupts.
263   */
264    vectorDesc.exceptIndex      =       ASM_DEC_VECTOR;
265    vectorDesc.hdl.vector       =       ASM_DEC_VECTOR;
266    vectorDesc.hdl.raw_hdl      =       decrementer_exception_vector_prolog_code;
267    vectorDesc.hdl.raw_hdl_size =       &decrementer_exception_vector_prolog_code_size;
268    vectorDesc.on               =       nop_func;
269              =       nop_func;
270    vectorDesc.isOn             =       connected;
271    if (!mpc60x_set_exception (&vectorDesc)) {
272      BSP_panic("Unable to initialize RTEMS decrementer raw exception\n");
273    }
274    vectorDesc.exceptIndex      =       ASM_EXT_VECTOR;
275    vectorDesc.hdl.vector       =       ASM_EXT_VECTOR;
276    vectorDesc.hdl.raw_hdl      =       external_exception_vector_prolog_code;
277    vectorDesc.hdl.raw_hdl_size =       &external_exception_vector_prolog_code_size;
278    if (!mpc60x_set_exception (&vectorDesc)) {
279      BSP_panic("Unable to initialize RTEMS external raw exception\n");
280    }
281    printk("RTEMS IRQ management is now operationnal\n");
282    printk("Going to enable interrupts at processor level\n");
283    _CPU_MSR_GET(msr);
284    msr = msr |= MSR_EE;
285    _CPU_MSR_SET(msr);
286    printk("Interrupts enabled\n");
Note: See TracBrowser for help on using the repository browser.