Linux Drive design-interrupts and clocks

Source: Internet
Author: User

Interrupt and clock technology can improve driver efficiency

Interrupt

Implementation of interrupts in Linux

Typically, a driver only needs to request an interrupt and add an interrupt handler, and the interrupt arrival and interrupt function calls are done by the kernel implementation framework. So the programmer just has to make sure to apply the correct interrupt number and write the correct interrupt handler function.

The macro classification of interrupts

1. Hard Interrupt

Interrupts generated by the system hardware. System hardware usually causes external events. External event events are random and abrupt, so hardware interrupts are also random and bursty.

2. Soft Interrupt

  Soft interrupts are generated when an interrupt instruction is executed. The soft interrupt does not use the peripheral to apply the interrupt request signal, so the soft interrupt occurrence is not random is the program arranges well. The soft interrupt instruction int N,n in the assembler must be an interrupt vector.

The processor receives a soft interrupt with two sources: (1) The processor executes to the wrong instruction code, and (2) the software generates an interrupt, such as a process dispatch, which is the use of a soft interrupt.

Where interrupts are generated classification

1. External interrupts

An external interrupt is typically an interrupt request made by a computer peripheral, such as a keyboard interrupt, a timer interrupt, and so on. External interrupts can be masked programmatically.

2. Internal interruption

Internal finger due to hardware error (such as sudden power outage, parity error, etc.) or operation error (divisor is 0, operation overflow, single-step interrupt, etc.) caused by the interruption. An internal interrupt is a non-blocking interrupt. Typically, most internal outages are handled by the Linux kernel, so driver programmers do not need to be concerned about these issues.

Synchronous and asynchronous interrupts

1. Synchronous interrupts

Synchronous interrupts are controlled by the CPU during instruction execution, and the CPU interrupts after executing an instruction. That is, in the process of instruction execution, there is an immediate interruption, as long as the instruction is not finished, the CPU will not go to perform the interrupt. synchronization interrupts are generally caused by a program error , such as a Fault in memory management, error 0, etc. When the CPU decides to handle a synchronous interrupt, the exception handler is called, which restores the system from the wrong state. When an error is not recoverable, a blue screen is lost.

2. Asynchronous interrupts

Asynchronous interrupts are generated randomly by hardware devices, which do not consider synchronization with the processor clock when interrupts occur, which can be generated at any time. For example, in a NIC driver, when a network card receives a packet, an asynchronous interrupt event is sent to the CPU, indicating that the data arrives and the CPU does not know when the event will be received. The interrupt handler for an asynchronous interrupt is executed asynchronously with the kernel's execution order, and the two do not have a necessary connection or affect each other.

The implementation process of interrupts

The implementation of interrupts is more complicated, which involves the concepts of interrupt signal line and interrupt controller.

1. Interrupt signal Line (IRQ)

 The interrupt signal line is the general designation of the interrupt input line and the interrupt output line. The interrupt input line is the pin that receives the interrupt information. The interrupt output line refers to the pin that sends the interrupt information. Each interrupt-capable peripheral has one or more interrupt output lines (IRQ) to notify the processor of an interrupt.

2. Interrupt Controller

 The interrupt controller is located between the ARM processor core and the interrupt source. An external interrupt source sends an interrupt to the interrupt controller. The interrupt controller is judged by priority and then sends the interrupt request to the ARM processor core via the PIN.

 

When an external interrupt produces an interrupt at the same time, the interrupt priority generation logic determines which interrupt will be executed. Interrupt screen Register: When the shield bit is 0, the corresponding interrupt can be performed normally, otherwise the corresponding interrupt is forbidden.

Interrupt handling Process

  

Installation and release of interrupts

The interrupt should be installed when the device needs to interrupt functionality. If the driver programmer does not tell the Linux kernel to use interrupts by installing interrupts, the kernel simply answers and ignores the interrupt.

1. Disconnection in Application

The disconnection in the application allows the kernel to instruct the peripheral which interrupt number should be used and which interrupt handler function.

Function implementation: KERNEL/IRQ/MANAGE.C

REQUEST_IRQ (unsigned int IRQ, irq_handler_t handler, unsigned long flags,    const char *name, void *dev) {return request_ THREADED_IRQ (IRQ, Handler, NULL, flags, name, dev);}

2. Release the interrupt line kernel/irq/manage.c

void Free_irq (unsigned int irq, void *dev_id) {kfree (__FREE_IRQ (IRQ, dev_id));}

Key Interrupt Instance

Clock mechanism

Some clock mechanisms are often used in Linux drivers, mainly to delay time. During this time, the hardware equipment can complete the corresponding work.

Time Measurement

A global variable Hz associated with the clock interrupt. Clock interrupts are generated by the system's timing hardware at periodic intervals, and this periodic value is expressed by Hz.

Hz is typically defined as 1000, indicating that a second clock interrupt occurs 1000 times. Each time a clock interrupt occurs, the value of the inner counter of the kernel is added to 1. The internal counter is represented by the jiffies variable, which is set to 0 when the system is initialized.

Time delay

In C, the sleep () function is often used to delay a program for a period of time, and this function can achieve millisecond delay. In the device driver, a lot of operation on the device also need to delay a period of time, so that the device to complete some specific tasks.

In the Linux kernel, two kinds of common delay technologies

1. Short time delay

When a device driver waits for hardware processing to complete, it actively delays for a period of time. This time is typically dozens of milliseconds, or even less. For example, when a driver writes data to a register on a device, it requires the driver to wait for a certain amount of time because the register is slow to write, and then continues to do the following work.

static inline void Ndelay (unsigned long x), static inline void Udelay (unsigned long usecs), static inline void Msleep (unsign Ed int msecs);

2. Long time delay

Long-time delay implementations are generally compared to the values of the current jiffies and target jiffies. Long delays can be achieved by using busy waits.

Example of a delay of 3 seconds:

unsigned long timeout = jiffies + 3*hz;while (time_before (jiffies, timeout));

TIME_BEFORE macro Simple comparison two time worth the size. Returns true if the parameter 1< parameter 2.

To be continue ...

Linux Drive design-interrupts and clocks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.