Communicating with Hardware Ted Baker  Andy Wang COP 5641 / CIS 4930.

Slides:



Advertisements
Similar presentations
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Advertisements

Accessing I/O Devices Processor Memory BUS I/O Device 1 I/O Device 2.
INPUT-OUTPUT ORGANIZATION
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Input and Output CS 215 Lecture #20.
I/O Unit.
Avishai Wool lecture Introduction to Systems Programming Lecture 8 Input-Output.
Embedded Systems Programming Writing Device Drivers.
CHAP 8 Hardware Management ANT 鄭伊騏 (JOE)
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
04/16/2010CSCI 315 Operating Systems Design1 I/O Systems Notice: The slides for this lecture have been largely based on those accompanying an earlier edition.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
1 Input/Output Chapter 3 TOPICS Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks Reference: Operating Systems Design.
I/O Systems CSCI 444/544 Operating Systems Fall 2008.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Parallel Ports of PC Methods of interfacing Examples.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
INPUT-OUTPUT ORGANIZATION
I/O Systems ◦ Operating Systems ◦ CS550. Note:  Based on Operating Systems Concepts by Silberschatz, Galvin, and Gagne  Strongly recommended to read.
Input/Output. Input/Output Problems Wide variety of peripherals —Delivering different amounts of data —At different speeds —In different formats All slower.
1 Input/Output. 2 Principles of I/O Hardware Some typical device, network, and data base rates.
hardware and operating systems basics.
Chapter 10: Input / Output Devices Dr Mohamed Menacer Taibah University
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
Chapter 1 Computer System Overview Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
Dr. Rabie A. Ramadan Al-Azhar University Lecture 6
MICROPROCESSOR INPUT/OUTPUT
Hardware Definitions –Port: Point of connection –Bus: Interface Daisy Chain (A=>B=>…=>X) Shared Direct Device Access –Controller: Device Electronics –Registers:
Computer Architecture Lecture 8 by Engineer A. Lecturer Aymen Hasan AlAwady 30/12/2013 University of Kufa - Informatics Center for Research and Rehabilitation.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 8-2: I/O Management (Review) Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Interrupts and DMA CSCI The Role of the Operating System in Performing I/O Two main jobs of a computer are: –Processing –Performing I/O manage and.
I/O Systems I/O Hardware Application I/O Interface
1 Lecture 20: I/O n I/O hardware n I/O structure n communication with controllers n device interrupts n device drivers n streams.
1. Introduction 2. Methods for I/O Operations 3. Buses 4. Liquid Crystal Displays 5. Other Types of Displays 6. Graphics Adapters 7. Optical Discs 10/01/20151Input/Output.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Principles of I/0 hardware.
1 Comp 104: Operating Systems Concepts Devices. 2 Today Devices –Introduction –Handling I/O Device handling Buffering and caching.
1-1 Embedded Network Interface (ENI) API Concepts Shared RAM vs. FIFO modes ENI API’s.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 3: Operating-System Structures System Components Operating System Services.
Computers Operating System Essentials. Operating Systems PROGRAM HARDWARE OPERATING SYSTEM.
Chapter 13: I/O Systems. 13.2/34 Chapter 13: I/O Systems I/O Hardware Application I/O Interface Kernel I/O Subsystem Transforming I/O Requests to Hardware.
Accessing I/O Devices Processor Memory BUS I/O Device 1 I/O Device 2.
Programming the I/O Hardware Reference: –textbook: Tanenbaum ch.5.1 – s.htmlwww.cs.umb.edu/ulab/UsingCforHardwareReg.
Input-Output Organization
Embedded Network Interface (ENI). What is ENI? Embedded Network Interface Originally called DPO (Digital Product Option) card Printer without network.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 13: I/O Systems I/O Hardware Application I/O Interface Kernel I/O Subsystem.
Computer Hardware A computer is made of internal components Central Processor Unit Internal External and external components.
12/8/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
Chapter 13 – I/O Systems (Pgs ). Devices  Two conflicting properties A. Growing uniformity in interfaces (both h/w and s/w): e.g., USB, TWAIN.
1 Lecture 1: Computer System Structures We go over the aspects of computer architecture relevant to OS design  overview  input and output (I/O) organization.
Processor Memory Processor-memory bus I/O Device Bus Adapter I/O Device I/O Device Bus Adapter I/O Device I/O Device Expansion bus I/O Bus.
Device Driver Concepts Digital UNIX Internals II Device Driver Concepts Chapter 13.
Chapter 13: I/O Systems Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Jan 2, 2005 I/O through system calls Protection.
بسم الله الرحمن الرحيم MEMORY AND I/O.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 12: I/O Systems I/O hardwared Application I/O Interface Kernel I/O.
I/O: Input-Output By: Tommy Zeng. What is I/O? I/O – short for “Input – Output” How a computer interacts with its users Input – gets information from.
1 Device Controller I/O units typically consist of A mechanical component: the device itself An electronic component: the device controller or adapter.
ECE 456 Computer Architecture Lecture #9 – Input/Output Instructor: Dr. Honggang Wang Fall 2013.
Overview of microcomputer structure and operation
Input/Output (I/O) Important OS function – control I/O
Chapter 13: I/O Systems Modified by Dr. Neerja Mhaskar for CS 3SH3.
Linux Details: Device Drivers
1 Input-Output Organization Computer Organization Computer Architectures Lab Peripheral Devices Input-Output Interface Asynchronous Data Transfer Modes.
CS703 - Advanced Operating Systems
Communicating with Hardware
Interfacing Memory Interfacing.
Linux Details: Device Drivers
CHAP 8 Hardware Management
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Communicating with Hardware Ted Baker  Andy Wang COP 5641 / CIS 4930

Topics Port-mapped vs. memory-mapped I/O Suppressing erroneous optimizations on I/O operations I/O macros/operations The parallel port The short example module

I/O Ports and I/O Memory Every peripheral device is controlled by writing and reading its registers Either in the memory address space (memory-mapped I/O) Can access devices like memory Or the I/O address space (port-mapped I/O) Need to use special instructions

I/O Ports and I/O Memory Linux provides virtual I/O ports At the hardware level Accessed at consecutive addresses Assert commands to the address bus and control bus Read from or write to the data bus

I/O Registers and Conventional Memory Need to watch out for CPU and compiler optimizations I/O operations have side effects When accessing registers No caching Automatically handled by Linux initialization code No read and write reordering Need to insert memory barrier calls

I/O Registers and Conventional Memory To prevent compiler optimizations across the barrier, call #include void barrier(void); Invalidate values in registers Forces refetches as needed Suppresses instruction reordering Hardware is free to do its own reordering

I/O Registers and Conventional Memory Other barrier calls #include /* all reads are completed before this barrier */ void rmb(void); /* blocks reordering of reads (across the barrier) that depend on data from other reads */ void read_barrier_depends(void); /* all writes are completed before this barrier */ void wmb(void); /* all reads & writes are completed before this barrier */ void mb(void);

I/O Registers and Conventional Memory A typical usage iowrite32(io_destination_address, dev->registers.addr); iowrite32(io_size, dev->registers.size); iowrite32(DEV_READ, dev->registers.operation); wmb(); iowrite32(DEV_GO, dev->registers.control); Different barrier calls for SMP void smp_rmb(void); void smp_read_barrier_depends(void); void smp_wmb(void); void smp_mb(void);

I/O Registers and Conventional Memory Most synchronization primitives can function as memory barriers spinlock, atomic_t

Using I/O Ports Allow drivers communicate with devices To allocate, call #include struct resource *request_region(unsigned long first, unsigned long n, const char *name); Allocate n ports with first name is the name of the device Returns non-NULL on success

Using I/O Ports See /proc/ioports to see the current allocation f : dma : pic : timer : timer f : keyboard : rtc f : dma page reg 00a0-00a1 : pic2 00c0-00df : dma2 00f0-00ff : fpu : ide1

Using I/O Ports If your allocation fails Try other ports Remove the device module using those ports To free I/O ports, call void release_region(unsigned long start, unsigned long n);

Manipulating I/O Ports Main interactions: reads and writes Needs to differentiate 8-bit, 16-bit, 32- bit ports #include /* 8-bit functions */ unsigned inb(unsigned port); void outb(unsigned char byte, unsigned port); /* 16-bit functions */ unsigned inw(unsigned port); void outw(unsigned short word, unsigned port);

Manipulating I/O Ports /* 32-bit functions */ unsigned inl(unsigned port); void outl(unsigned longword, unsigned port);

I/O Port Access from User Space Via /dev/port #include Same inb/outb, inw/outw, inl/outl calls Must compile with –O option Must use ioperm and iopl calls to get permission to operate on ports Must run as root

I/O Port Access from User Space See misc-progs/inp.c and misc- progs/outp.c Need to create symlinks to the binary ln –s inp inb ln –s inp inw ln –s inp inl ln –s outp outb ln –s outp outw ln –s outp outl

I/O Port Access from User Space Specify the port number to read and write To read 1 byte from port 0x40 > inb : d4 To write 1 byte “ 0xa5” to port 0x40 > outb 40 1 a5 Don’t try this at home /dev/port is a security hole

String Operations String instructions can transfer a sequence of bytes, words, or longs Available on some processors The port and the host system might have different byte ordering rules

String Operations Prototypes void insb(unsigned port, void *addr, unsigned long count); void outsb(unsigned port, void *addr, unsigned long count); void insw(unsigned port, void *addr, unsigned long count); void outsw(unsigned port, void *addr, unsigned long count); void insl(unsigned port, void *addr, unsigned long count); void outsl(unsigned port, void *addr, unsigned long count);

Pausing I/O Sometimes the CPU transfers data too quickly to or from the bus Need to insert a small delay after each I/O instruction Send outb to port 0x80 (on the x86) Busy wait See for details Use pausing functions (e.g., inb_p, outb_p )

Platform Dependencies I/O instructions are highly CPU dependent by their nature x86 and X86_64 unsigned short port numbers ARM Ports are memory-mapped unsigned int port numbers

Platform Dependencies MIPS and MIPS64 unsigned long port numbers PowerPC unsigned char * ports on 32-bit systems unsigned long on 64-bit systems SPARC Memory-mapped I/O unsigned long ports

An I/O Port Example A digital I/O port Byte-wide I/O location Either memory-mapped or port-mapped Separate input pins and output pins (most of the time) E.g., parallel port

An Overview of the Parallel Port 5V (TTL) logic levels Made up of three 8-bit ports 12 output bits and 5 input bits First parallel interface consists of port 0x378-0x37a, second at 0x278-0x27a First port (0x378/0x278) is a bidirectional data register Pins 2-9

An Overview of the Parallel Port Second port is a status register Online, out of paper, busy Third port is an output-only control register Controls whether interrupts are enabled

An Overview of the Parallel Port

A Sample Driver short (Simple Hardware Operations and Raw Tests) Uses ports 0x378-0x37f /dev/short0 reads and writes the 8-bit port 0x378 /dev/short1 reads and writes port 0x379 … Not sophisticated enough to handle printers

A Sample Driver /dev/short0 is based on a tight loop while (count--) { outb(*(ptr++), port); wmb(); /* write memory barrier */ } To test, try % echo –n “any string” > /dev/short0 The last character stays on the output pins -n removes automatic insertion of “ \n ”

A Sample Driver To read, try % dd if=/dev/short0 bs=1 count=1 | od –t x1 1+0 records in 1+0 records out 1 byte (1 B) copied, 4.4e-5 seconds, 22.7 kB/s dd converts and copies a file bs = transfer granularity in bytes count = number of transfers od performs an octal dump -t x1 prints 1 byte in hex “g” in hex

A Sample Driver Variants of short /dev/short0p and the others use outb_p and inb_p pause functions /dev/short0s and the others use the string instructions

Using I/O Memory Outside of the x86 world, the main mechanism used to communicate with devices is through memory-mapped I/Os

Using I/O Memory Should not use pointers directly Use wrappers to improve portability Depending on the platform I/O memory may or may not be accessed through page tables With the use of page tables, you need to call ioremap before doing any I/O Without using the page tables, just use wrapper functions

I/O Memory Allocation and Mapping To allocate I/O memory, call #include struct resource *request_mem_region(unsigned long start, unsigned long len, char *name); start : starting memory location len : bytes name : displayed in /proc/iomem

I/O Memory Allocation and Mapping more /proc/iomem b7ff : System RAM 0009b ffff : reserved 000a bffff : Video RAM area 000c c7fff : Video ROM 000c c8fff : Adapter ROM 000f fffff : System ROM ff6ffff : System RAM c7f2f : Kernel code 002c7f ff : Kernel data 7ff ff77fff : ACPI Tables 7ff ff7ffff : ACPI Non-volatile Storage...

I/O Memory Allocation and Mapping To free memory regions, call void release_mem_region(unsigned long start, unsigned long len); To make memory accessible, call #include void *ioremap(unsigned long phys_addr, unsigned long size); void iounmap(void *addr);

Accessing I/O Memory Should use predefined macros to perform memory-mapped I/Os unsigned int ioread8(void *addr); unsigned int ioread16(void *addr); unsigned int ioread32(void *addr); void iowrite8(u8 value, void *addr); void iowrite16(u16 value, void *addr); void iowrite32(u32 value, void *addr);

Accessing I/O Memory To perform repeated I/Os, use void ioread8_rep(void *addr, void *buf, unsigned long count); void ioread16_rep(void *addr, void *buf, unsigned long count); void ioread32_rep(void *addr, void *buf, unsigned long count); void iowrite8_rep(void *addr, const void *buf, unsigned long count); void iowrite16_rep(void *addr, const void *buf, unsigned long count); void iowrite32_rep(void *addr, const void *buf, unsigned long count); count : number of repetitions

Accessing I/O Memory Other operations void memset_io(void *addr, u8 value, unsigned int count); void memcpy_fromio(void *dest, void *source, unsigned int count); void memcpy_toio(void *dest, void *source, unsigned int count); count: in bytes

Ports as I/O Memory Linux 2.6 introduces ioport_map Remaps I/O ports and makes them appear to be I/O memory void *ioport_map(unsigned long port, unsigned int count); void ioport_unmap(void *addr); port = first port number count = number of I/O ports

Reusing short for I/O Memory To try the memory-mapped I/O, type %./short_load use_mem=1 base=0xb7ffffc0 % echo –n 7 > /dev/short0 The internal loop uses iowrite8 while (count--) { iowrite8(*ptr++, address); wmb( ); }