Chapter 4 Input Output System
Chapter 4 Input Output System
Estructura de Computadores
Dept. de Arquitectura de Computadores
Cache
Graphics Network
Disk Disk
Control bus
Address bus
Data bus
CPU
Data
Wrt
Rd
Address port
Register
I/O Device
Address bus
Data bus
CPU
Comand
Status.reg
Read reg
Write reg
I/O Device
Main
Memory 000FFFFFh
Main
00000000h Memory
Address bus
Data bus 00000000h
CPU
Address bus
Data bus
CPU
System without I/O device FFFFFFFFh
I/O
Device
00100000h
00000000h
Address bus
Data bus 00000000h
CPU
Address bus
Data bus
CPU
System without I/O device FFFFh
I/O
Device
0000h
3F20 0008 GPFSEL2 X FSEL9 FSEL8 FSEL7 FSEL6 FSEL5 FSEL4 FSEL3 FSEL2 FSEL1 FSEL0
3F20 0034 GPLEV0 X FSEL9 FSEL8 FSEL7 FSEL6 FSEL5 FSEL4 FSEL3 FSEL2 FSEL1 FSEL0
---- ----
M3
M2
M1
M0
3F20 3000 CS
M3
M2
M1
M0
Comparators
64 bit counter
CO
C3
C0 and C2 are
used by the GPU
Timer read
Routine implementation:
• We can use registers r0 and r1 as input parameters
• r0 contains the timer port address
• r1 contains the waiting time
• We must preserve registers modified inside our routine
• r4 contains the ending time
• r5 loads current timer value
espera: push {r4, r5} @ Save r4 and r5 in the stack
ldr r4, [r0, #STCLO] @ Load CLO timer
add r4, r1 @ Add waiting time -> this is our ending time
ret1: ldr r5, [r0, #STCLO] @ Enter waiting loop: load current CLO timer
cmp r5, r4 @ Compare current time with ending time
blo ret1 @ If lower, go back to read timer again
pop {r4, r5} @ Restore r4 and r5
bx lr @ Return from routine
Dept. Arquitectura de Computadores 38
Example 3: red LED blinking
.set GPBASE, 0x3F200000
.set GPFSEL0, 0x00
.set GPSET0, 0x1c
.set GPCLR0, 0x28
.set STBASE, 0x3F003000
.set STCLO, 0x04
Waiting time
Convention:
◦ Exception: any event (other than branches and
jumps) that changes the normal flow of
instructions
If it is an external event the exception is called
Interrupt
CPU
I/O
device
Dept. Arquitectura de Computadores 52
Interrupt Driven I/O
An I/O interrupt is asynchronous wrt instruction execution
◦ Is not associated with any instruction so doesn’t prevent any instruction from completing
You can pick your own convenient point to handle the interrupt
Control unit needs only check for a pending I/O interrupt at the time it starts a new instruction
With I/O interrupts
◦ Need a way to identify the device generating the interrupt
Vectored interrupts: the device can send a vector (id.) to the processor, which uses it to address the table of
the interrupt vectors, from where it gets the address of the handle.
Non vectored interrupts: the device places a status field in the Cause register, jumps to a handler at a fixed
direction.
Auto-vectored interrupts: each exception has vector associated to it.
When the handle gets control, it knows the identity of the device and
can immediately start the I/O operation
◦ Can have different urgencies (so need a way to prioritize them)
I/O interrupts have lower priority than internal exceptions
UNIX OS uses four to six levels
Interrupt priority levels (IPLs) assigned by the OS to each process can be
raised and lowered via changes to the Status’s Interrupt mask field
• Lowest ILP: all interrupts are permitted
• Highest ILP: all interrupts are blocked
cpsr
spsr spsr spsr spsr spsr
Current mode Banked out registers
Dept. Arquitectura de Computadores 59
Handling exceptions
When an exception occurs, the core:
1. Copies CPSR into SPSR_<mode>
2. Sets appropriate CPSR bits
0x1C FIQ
◦ Change to ARM state
◦ Change to exception mode 0x18 IRQb irq_handler
◦ Disable interrupts (if appropriate) 0x14 (Reserved)
3. Stores the return address in 0x10 Data Abort
LR_<mode> 0x0C Prefetch Abort
4. Sets PC to vector address 0x08 Supervisor Call
0x04 Undefined Instruction
To return, exception handler needs to: 0x00 Reset
1. Restore CPSR from SPSR_<mode>
2. Restore PC from LR_<mode> Vector Table
Vector table can also be at
This can only be done in ARM state. 0xFFFF0000 on most cores
256MB
128MB
16KB
16KB
8
4
0x1C FIQ
!"#$% &' ()) %&*+ 0x18 IRQ (b disp)
0xEA000000 + =
0x14 (Reserved)
!"#$% &'()) %& 0x10 Data Abort
0xE9FFFFFE = 0x0C Prefetch Abort
8 digits! 0x08 Supervisor Call
0x3A7FFFFF8* !"#$% &'()) %&
Undefined Instruction
0x04
0x00 Reset
We can divide by 4 by rotating right
the numerator (after adding 3 to Vector Table
save the two most significant bits)
Dept. Arquitectura de Computadores 70
macro ADDEXC offset, dirDest
ASM b disp
…
Binary 11101010
dirDest irq_handler
Hex E A
8
.macro 4 ADDEXC offset, dirDest
0x1C FIQ
ldr r1, =(\dirDest-\offset+0xA7FFFFFB)
ror!"#$%r1, #2%&*+
&' ()) 0x18 IRQ (b disp)
0xEA000000 + =
str r1, [r0, #\offset] 0x14 (Reserved)
.endm 0x10 Data Abort
!"#$% &'()) %&
0xE9FFFFFE = 0x0C Prefetch Abort
0x08 Supervisor Call
0x3A7FFFFF8* !"#$% &'()) %&
Undefined Instruction
0x04
0x00 Reset
We can divide by 4 by rotating right
the numerator (after adding 3 to Vector Table
save the two most significant bits)
Dept. Arquitectura de Computadores 71
2- Disable Interruptions and
initialize the stack
Each mode has its stack pointer (sp)
◦ Change the mode (via cpsr_c)
◦ Instructions msr (sr <- reg) y mrs (reg <-sr).
◦ Initialize the corresponding sp register
Initial state in BareMetal is SVC
◦ sp_fiq=0x4000, sp_irq=0x8000, sp_svc=0x08000000:
Bits in PR1 8 8 8 8
GPU IRQ 7 10 10 10 10
I2c_spi_s
GPU IRQ 9 11 11 11
lv_int
GPU IRQ 10 12 12 12 12
GPU IRQ 53 15 15 15 15
20 20 Gpio_int3 20 20
21 21 I2c_int 21 21
22 22 Spi_int 22 22
23 23 Pcm_int 23 23
24 24 24 24 24
25 25 Uart_int 25 25
26 26 26 26 26
27 27 27 27 27
28 28 28 28 28
29 Aux_int 29 29 29
30 30 Sd_host 30 30
31 31 31 31 31
Disable Basic IRQs
IRQ basic pending
Disable IRQs 1
Disable IRQs 2
IRQ pending 1
IRQ pending 2
Enable IRQs 1
Enable IRQs 2
FIQ control
3F00 B204
3F00 B200
3F00 B208
3F00 B20C
3F00 B210
3F00 B214
3F00 B218
3F00 B21C
3F00 B220
3F00 B224
4- Enable sources of interruption
Three groups: pending, enable and disable
In each group:
3F00 B200 IRQ basic pending
• IRQ basic: summary
3F00 B204 IRQ pending 1 Pending • IRQs 1 and 2: in detail
3F00 B208 IRQ pending 2
3F00 B214 Enable IRQs 2 Enable There is also one port for
3F00 B218 Enable Basic IRQs FIQ control
3F00 B21C Disable IRQs 1
77
triggered by
triggered by
4- Enable sources of interruption
any pin of
Interrupt
Interrupt
A. Doorbell 0 Systim_c2 2
Select
A. Doorbell 1 Systim_c3 3 FIQ
Source
GPU 0 halted 4 4
GPU 1 halted 5 5
Illegal type 1 6 6
Bits in PR1 8 8 8
GPU IRQ 7 10 10 10
I2c_spi_s
GPU IRQ 9 11 11
lv_int
GPU IRQ 10 12 12 12
GPU IRQ 53 15 15 15
20 20 Gpio_int3 1 20
21 21 I2c_int 21
22 22 Spi_int 22
23 23 Pcm_int 23
24 24 24 24
25 25 Uart_int 25
26 26 26 26
27 27 27 27
28 28 28 28
29 Aux_int 29 29
30 30 Sd_host 30
31 31 31 31
Basic Control
IRQ 1 IRQ 2 FIQ
IRQ
4- Enable sources of interruption
Set the corresponding bit in the appropriate
Enable IRQs port
◦ GPIO interruption enable: use GPRENn, GPFENn,
GPHENn, GPLENn, GPARENn y GPAFENn
◦ System Timer: each counter (C0-C3) can be
enabled/disabled in Enable IRQ1 / Disable IRQ1
ports.
In case of interruption, the handler must identify
the source reading the IRQ pending ports
◦ GPIO interruption detection: use GPEDSn.
◦ System Timer interrupt detection: STCS notifies
interruption due to C0 : C3 counters
3F20 003C --
Rising edge ENable Enable interrupt request with a
3F20 0040 GPEDS0 rising edge (sync., avoid glitch)
3F20 0044 GPEDS1
3F20 0048 --
3F20 004C GPREN0 Falling edge Enable Enable interrupt request with a
falling edge (sync., avoid glitch)
3F20 0050 GPREN1
3F20 0054 --
3F20 005C GPFEN1 The suppression of glitches is done by sampling the pin using
---- ---- the system clock and then looking for a “011” (rising) or
“011” (falling edge) pattern on the sampled signal.
irq_handler:
push {r0, r1}
ldr r0, =GPBASE
mov r1, #0b00000000000000000000001000000000 Turn on RED led (GPIO9)
str r1, [r0, #GPSET0]
pop {r0, r1}
subs pc, lr, #4 PC LR - 4
irq_handler:
push {r0, r1, r2}
ldr r0, =GPBASE
ldr r2, [r0, #GPEDS0] Check GPIO2
ands r2, #0b00000000000000000000000000000100 was pressed
movne r1, #0b00000000000000000000001000000000 Turn on
strne r1, [r0, #GPSET0] GPIO9 red led
movne r1, #0b00000000000000000000000000000100 Clear GPIO2
strne r1, [r0, #GPEDS0] event
fin: pop {r0, r1, r2}
subs pc, lr, #4
FIQ Enable
control
Source
Select
FIQ
FIQ
31
30
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
29
Systim_c3
Systim_c2
Systim_c1
Systim_c0
Usb_con
IRQ 1
Aux_int
31
30
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
4
3 1
Gpio_int3
Gpio_int2
Gpio_int1
Gpio_int0
I2c_spi_s
Uart_int
Sd_host
Pcm_int
Spi_int
I2c_int
IRQ 2
lv_int
pwa1
pwa0
smi
31
28
27
26
24
15
12
10
29
0
20
Reg. Reg.
File File
CPU CPU
I/O I/O
device device
With DMA
Without DMA
Memory Bus
MIPS Bus
Controller
Inst
Cache Data
Cache
DMA
Main controller Hard disk
Memory
Memory Bus
Control bus
MIPS Bus
Controller
Inst
Cache Data
Cache
Cache
Graphics Network
Disk Disk
◦ Serial ATA cables (red) are much thinner than parallel ATA cables
(green)
I/O device signals a request by raising ReadReq and putting the addr on
the data lines
1. Memory sees ReadReq, reads addr from data lines, and raises Ack
2. I/O device sees Ack and releases the ReadReq and data lines
3. Memory sees ReadReq go low and drops Ack
4. When memory has data ready, it places it on data lines and raises DataRdy
5. I/O device sees DataRdy, reads the data from data lines, and raises Ack
6. Memory sees Ack, releases the data lines, and drops DataRdy
7. I/O device sees DataRdy go low and drops Ack
Dept. Arquitectura de Computadores 113
Key Characteristics of I/O
Standards
Firewire USB 2.0 PCIe Serial SA SCSI
ATA
Use External External Internal Internal External
Devices 63 127 1 1 4
per channel
Max length 4.5 meters 5 meters 0.5 meters 1 meter 8 meters
Data Width 4 2 2 per lane 4 4
Peak 50MB/sec 0.2MB/sec 250MB/sec 300MB/sec 300MB/sec
Bandwidth (400) (low) per lane
100MB/sec 1.5MB/sec (1x)
(800) (full) Come as
60MB/sec 1x, 2x, 4x,
(high) 8x, 16x,
32x
Hot Yes Yes Depends Yes Yes
pluggable?
Dept. Arquitectura de Computadores 114
A Typical I/O System
Intel Xeon 5300 Intel Xeon 5300
processor processor