Interrupts -2- (irq chip)

<kernel v5.4>

IRQ Chip

인터럽트 컨트롤러 드라이버를 위해 hw 제어를 담당하는 구현 부분을 가진다.

  • 여러 제조사의 인터럽트 컨트롤러들은 각 인터럽트 라인의 통제 방법이 각각 다르다.
  • 리눅스 IRQ 코어 레이어에서 irq chip은 다양한 인터럽트 컨트롤러의 라인별 제어 액션을 통일하여 처리하도록 구현되었다.
    • 인터럽트 라인마다  마스킹 또는 set/clear 동작을 수행하는 서비스를 제공한다.
    • irq_chip에 있는 여러 개의 후크 포인터에 연결된 콜백 함수를 통해 처리한다.
      • (*irq_mask)
      • (*irq_unmask)
      • (*irq_set_type)
    • 각 제조사의 인터럽트 컨트롤러 디바이스 드라이버에는 irq_chip의 후크에 연결될 처리 함수들을 제공해야 한다.
  • 인터럽트 라인의 마스킹 및 set/clear 처리 유형 등의 처리가 각각 다른 경우 irq_chip을 분리하여 구성할 수 있다.
  • 인터럽트 라인 개개별로 h/w 제어를 담당하는 irq_chip을 다음 함수를 사용하여 지정한다.
    • irq_set_chip()

 

다음 그림은 하나의 인터럽트 컨트롤러가 대응하는 모든 인터럽트들을 하나의 irq_chip에서 제어하는 모습을 보여준다.

 

다음 그림은 하나의 인터럽트 컨트롤러가 대응하는 인터럽트들 중 h/w 제어 기능이 서로 다른 부분(part)들을 나누어 각각의 irq_chip에서 제어하는 모습을 보여준다.

 

다음 그림은 한 개의 디바이스 드라이버는 2 개의 irq_chip 구현을 가지고 있고, 또 다른 한 개의 디바이스 드라이버는 1 개의 irq_chip 구현을 가지고 있는 모습을 자세히 보여준다.

 

hierarchy irq chip 구성

2 개 이상의 인터럽트 컨트롤러를 hierarchy하게 구성하여 사용할 수 있다.

 

다음 그림은 두 개 이상 hierarchy 구성된 인터럽트 컨트롤러들이 연결되어 처리되는 모습을 보여준다.

  • 자식 인터럽트 컨트롤러 A에서 수신한 인터럽트들은 cascade(chained)로 부모 인터럽트 컨트롤러 B에 전달된다.

 

GPIO cascade 인터럽트 연동

인터럽트 컨트롤러 아래에 gpio 등에서 인터럽트의 멀티플렉싱이 가능한 장치들이 추가로 cascade 연결된 경우 이들의 하위 인터럽트 라인을 식별하기 위해 cascade 연동을 한다. 다음 GPIO 글에서 관련 연동 방법을 소개하기로 한다.

 


Device Tree 기반의 인터럽트 컨트롤러 초기화

irqchip_init()

drivers/irqchip/irqchip.c

void __init irqchip_init(void)
{
        of_irq_init(__irqchip_of_table);
        acpi_probe_device_table(irqchip);
}

디바이스 트리 및 ACPI가 지정한 인터럽트 컨트롤러의 초기화 함수를 호출한다.

  • 코드 라인 3에서 __irqchip_of_table 섹션에 있는 of_device_id 구조체들을 검사하여 매치되는 인터럽트 컨트롤러 초기화 함수를 찾아 호출한다.
    • rpi2:
      • 2 개의 인터럽트 컨트롤러 드라이버 호출
        • drivers/irqchip/irq-bcm2836.c – bcm2836_arm_irqchip_l1_intc_of_init()
        • drivers/irqchip/irq-bcm2835.c – bcm2835_armctrl_of_init()
    • rock960
      • 1 개의 인터럽트 컨트롤러 드라이버 호출
        • drivers/irqchip/irq-gic-v3.c – gic_of_init()
  • 코드 라인 4에서 ACPI가 지정한 인터럽트 컨트롤러의 초기화 함수를 호출한다.

 

of_irq_init()

drivers/of/irq.c -1/2-

/**
 * of_irq_init - Scan and init matching interrupt controllers in DT
 * @matches: 0 terminated array of nodes to match and init function to call
 *
 * This function scans the device tree for matching interrupt controller nodes,
 * and calls their initialization functions in order with parents first.
 */
void __init of_irq_init(const struct of_device_id *matches)
{
        const struct of_device_id *match;
        struct device_node *np, *parent = NULL;
        struct of_intc_desc *desc, *temp_desc;
        struct list_head intc_desc_list, intc_parent_list;

        INIT_LIST_HEAD(&intc_desc_list);
        INIT_LIST_HEAD(&intc_parent_list);

        for_each_matching_node_and_match(np, matches, &match) {
                if (!of_property_read_bool(np, "interrupt-controller") ||
                                !of_device_is_available(np))
                        continue;

                if (WARN(!match->data, "of_irq_init: no init function for %s\n",
                         match->compatible))
                        continue;

                /*
                 * Here, we allocate and populate an of_intc_desc with the node
                 * pointer, interrupt-parent device_node etc.
                 */
                desc = kzalloc(sizeof(*desc), GFP_KERNEL);
                if (!desc) {
                        of_node_put(np);
                        goto err;
                }

                desc->irq_init_cb = match->data;
                desc->dev = of_node_get(np);
                desc->interrupt_parent = of_irq_find_parent(np);
                if (desc->interrupt_parent == np)
                        desc->interrupt_parent = NULL;
                list_add_tail(&desc->list, &intc_desc_list);
        }

Device Tree에 있는 인터럽트 컨트롤러와 매치되는 인터럽트 컨트롤러의 초기화 함수를 호출한다. 단 2개 이상의 인터럽트 컨트롤러를 사용하는 경우 상위 인터럽트 컨트롤러부터 초기화한다.

  • 코드 라인 8~9에서 임시로 사용되는 리스트 2개를 초기화한다.
    • intc_desc_list:
      • irqchip 테이블의 이름과 Device Tree의 인터럽트 컨트롤러 드라이버명(compatible)으로 매치되어 구성된 desc 구조체 리스트
        • ARM 임베디드 시스템은 보통 1 ~ 2개의 인터럽트 컨트롤러를 사용한다.
    • intc_parent_list:
      • 초기화 성공한 인터럽트 컨트롤러의 desc 구조체 리스트로 최상위 인터럽트 컨트롤러가 가장 앞으로 구성된다.
  • 코드 라인 11~14에서 DTB 트리의 모든 노드에서 irqchip 테이블의 이름 과 Device Tree의 인터럽트 컨트롤러 드라이버명(compatible)으로 일치한 노드들에서 “interrupt-controller” 속성을 못찾았거나 “status” 속성이 “ok”가 아닌 경우 skip 한다.
  • 코드 라인 24~28에서 of_intc_dest 구조체를 할당받아 온다.
  • 코드 라인 30~34에서 할당받은 구조체를 설정한다.
    • desc->dev에 인터럽트 컨트롤러 노드를 가리키게한다.
    • desc->interrupt_parent에 상위 인터럽트 컨트롤러 노드를 가리키게 한다. 만일 상위 인터럽트 컨트롤러가 자기 자신을 가리키면 null을 대입한다.
    • 참고: 상위 인터럽트 컨트롤러를 찾는 법
      • “interrupt-parent” 속성이 위치한 노드가 가리키는 phandle 값을 알아온 후 그 값으로 노드를 검색하여 알아온다.
  • 코드 라인 35에서 intc_desc_list 임시 리스트의 후미에 할당받은 구조체를 추가한다.

 

drivers/of/irq.c -2/2-

        /*
         * The root irq controller is the one without an interrupt-parent.
         * That one goes first, followed by the controllers that reference it,
         * followed by the ones that reference the 2nd level controllers, etc.
         */
        while (!list_empty(&intc_desc_list)) {
                /*
                 * Process all controllers with the current 'parent'.
                 * First pass will be looking for NULL as the parent.
                 * The assumption is that NULL parent means a root controller.
                 */
                list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
                        int ret;

                        if (desc->interrupt_parent != parent)
                                continue;

                        list_del(&desc->list);

                        of_node_set_flag(desc->dev, OF_POPULATED);

                        pr_debug("of_irq_init: init %pOF (%p), parent %p\n",
                                 desc->dev,
                                 desc->dev, desc->interrupt_parent);
                        ret = desc->irq_init_cb(desc->dev,
                                                desc->interrupt_parent);
                        if (ret) {
                                of_node_clear_flag(desc->dev, OF_POPULATED);
                                kfree(desc);
                                continue;
                        }

                        /*
                         * This one is now set up; add it to the parent list so
                         * its children can get processed in a subsequent pass.
                         */
                        list_add_tail(&desc->list, &intc_parent_list);
                }

                /* Get the next pending parent that might have children */
                desc = list_first_entry_or_null(&intc_parent_list,
                                                typeof(*desc), list);
                if (!desc) {
                        pr_err("of_irq_init: children remain, but no parents\n");
                        break;
                }
                list_del(&desc->list);
                parent = desc->dev;
                kfree(desc);
        }

        list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
                list_del(&desc->list);
                kfree(desc);
        }
err:
        list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
                list_del(&desc->list);
                of_node_put(desc->dev);
                kfree(desc);
        }
}
  • 코드 라인 6에서 intc_desc_list 리스트의 모든 엔트리가 없어질 때까지 루프를 돈다.
  • 코드 라인 12에서 intc_desc_list 리스트의 desc 엔트리에 대해 루프를 돈다.
  • 코드 라인 15~16에서 남은 인터럽트 컨트롤러 중 최상위가 아닌 경우 skip 한다.
  • 코드 라인 18~20에서 initc_desc_list에서 하나를 제거하고, OF_POPULATED 플래그를 설정한다.
  • 코드 라인 22~24에서 매치되어 사용할 인터럽트 컨트롤러 드라이버 이름과 주소 그리고 부모 인터럽트 컨트롤러 주소 정보를 출력한다.
    • “of_irq_init: init <driver_name> @ <addr>, parent <addr>”
  • 코드 라인 25~31에서 매치된 of_device_id 구조체의 data에서 인터럽트 컨트롤러 드라이버 시작 함수를 호출한다.
  • 코드 라인 37에서 초기화 성공한 인터럽트 컨트롤러 디스크립터 구조체 정보를 intc_parent_list 라는 이름의 임시 리스트에 추가한다.
    • 부모 없는 child 인터럽트 컨트롤러가 있는지 오류 체크를 위함
  • 코드 라인 41~46에서 초기화 성공한 인터럽트 컨트롤러가 없으면 “of_irq_init: children remain, but no parents” 메시지를 출력하고 처리를 중단한다.
    • A: 조부모 IC <- B: 부모 IC, C: 아들 IC 구성 시
      • A-> B -> C 순서대로 초기화되어야 한다.
  • 코드 라인 47~49에서 임시 변수 parent에 처리한 인터럽트 컨트롤러 노드를 가리키게 하고 intc_desc_list 라는 이름의 임시 리스트에서 제거한다.
  • 코드 라인 52~61에서 임시로 사용했던 intc_parent_list 및 intc_desc_list에 등록된 desc 구조체를 모두 할당 해제한다.

 

다음 그림은 2개의 인터럽트 컨트롤러에 대해 상위 인터럽트 컨트롤러의 초기화 함수부터 호출되는 것을 보여준다.

 

irqchip 테이블

__irqchip_of_table

  • .init.data 섹션에서 .dtb.init.rodata 바로 뒤에 위치한다.
  • 엔트리들은 of_device_id 구조체로 구성된다.
  • __irqchip_of_table = .; *(__irqchip_of_table) *(__irqchip_of_table_end)
    • arch/arm64/kernel/vmlinux.lds

 

드라이버 내에서 irqchip 초기화 선언

case 1) rock960용 gic-v3 드라이버

gic-v3 드라이버는 다음 기본 드라이버 이외에 시스템 구성에 따라 gic-v3-its, gic-v3-mbi 등 다양한 드라이버가 추가 호출될 수 있다.

drivers/irqchip/irq-gic-v3.c

IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
  • __irqchip_of_table 섹션 위치에 of_device_id 구조체가 다음과 같이 정의 된다.
    • .compatible = “arm,gic-v3”
    • .data = gic_of_init()

 

case 2) rpi2용 2 개의 인터럽트 컨트롤러 드라이버

rpi2용 인터럽트 컨트롤러 드라이버는 두 개의 드라이버를 사용한다.

drivers/irqchip/irq-bcm2835.c

IRQCHIP_DECLARE(bcm2835_armctrl_ic, "brcm,bcm2835-armctrl-ic", armctrl_of_init);

 

drivers/irqchip/irq-bcm2836.c

IRQCHIP_DECLARE(bcm2836_arm_irqchip_l1_intc, "brcm,bcm2836-l1-intc",
                bcm2836_arm_irqchip_l1_intc_of_init);

 

IRQCHIP_DECLARE()

drivers/irqchip/irqchip.h

/*
 * This macro must be used by the different irqchip drivers to declare
 * the association between their DT compatible string and their
 * initialization function.
 *      
 * @name: name that must be unique accross all IRQCHIP_DECLARE of the
 * same file.
 * @compstr: compatible string of the irqchip driver
 * @fn: initialization function
 */
#define IRQCHIP_DECLARE(name, compat, fn) OF_DECLARE_2(irqchip, name, compat, fn)

irq_chip 디바이스 드라이버를 구성할 때 사용하는 선언문으로 여기서 만들어진 of_device_id 구조체는 __irqchip_of_table 섹션에 등록된다.

 

include/linux/of.h

#define OF_DECLARE_2(table, name, compat, fn) \
                _OF_DECLARE(table, name, compat, fn, of_init_fn_2)

 

include/linux/of.h

#define _OF_DECLARE(table, name, compat, fn, fn_type)                   \
        static const struct of_device_id __of_table_##name              \
                __used __section(__##table##_of_table)                  \
                 = { .compatible = compat,                              \
                     .data = (fn == (fn_type)NULL) ? fn : fn  }

 


Chip 관련 설정

하나의 irq 디스크립터에 담당 Chip 지정

irq_set_chip()

kernel/irq/chip.c

/**
 *      irq_set_chip - set the irq chip for an irq
 *      @irq:   irq number
 *      @chip:  pointer to irq chip description structure
 */
int irq_set_chip(unsigned int irq, struct irq_chip *chip)
{
        unsigned long flags;
        struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);

        if (!desc)
                return -EINVAL;

        if (!chip)
                chip = &no_irq_chip;

        desc->irq_data.chip = chip;
        irq_put_desc_unlock(desc, flags);
        /*
         * For !CONFIG_SPARSE_IRQ make the irq show up in
         * allocated_irqs.
         */
        irq_mark_irq(irq);
        return 0;
}
EXPORT_SYMBOL(irq_set_chip);

irq 디스크립터에 해당 irq를 마스킹 또는 set/clear 동작을 시킬 수 있는 irq chip 정보를 설정한다.

  • 코드 라인 9~10에서 만일 인터럽트 컨트롤러 chip을 사용하지 않은 경우  no_irq_chip(name=”none”) 구조체 정보를 대신 대입한다.
  • 코드 라인 12에서 irq 디스크립터의 chip_data에 chip 정보를 기록한다.
  • 코드 라인 18에서 Sparse IRQ를 사용하지 않는 경우allocated_irqs 비트맵에 요청 irq에 해당하는 비트를 mark하여 irq가 사용됨을 표시한다.

 

irq_mark_irq()

kernel/irq/irqdesc.c

void irq_mark_irq(unsigned int irq)
{       
        mutex_lock(&sparse_irq_lock);
        bitmap_set(allocated_irqs, irq, 1);
        mutex_unlock(&sparse_irq_lock);
}

Sparse IRQ를 사용하지 않는 경우 전역 allocated_irqs 비트맵을 사용하여 irq 번호에 해당하는 비트를 설정하여 irq가 사용됨을 표시한다.

  • ARM64 시스템은 디폴트로 CONFIG_SPARSE_IRQ를 사용하므로 위의 비트맵 관리를 하지 않는다.

 

 


Generic Core 인터럽트 핸들러

irq 디스크립터 기반으로 핸들러를 지정하고 인터럽트 발생 시 이들이 호출될 수 있는 generic 인터럽트 핸들링을 할 수 있는 API들이 준비되어 있다.

 

다음 4단계의 인터럽트 핸들러 처리 루틴을 알아본다.

  • CPU Exception(or 인터럽트) 벡터
    • 인터럽트 발생 시 cpu에 처음 진입부
  • 인터럽트 Chip 핸들러
    • 인터럽트 발생 시 담당 인터럽트 컨트롤러가 최초 처리할 핸들러
  • Flow Handler
    • 각 인터럽트 번호마다 인터럽트 flow 유형별 처리를 지정하는데, 보통 커널에 마련된 generic default flow 핸들러를 사용하거, custom 핸들러를 등록하여 사용한다.
  • 최종 디바이스의 인터럽트 핸들러
    • 최종 사용자 디바이스 드라이버에서 해당 인터럽트를 처리하기 위한 핸들러

 

다음 그림은 단계별로 나뉜 인터럽트 핸들러가 처리되는 과정을 보여준다.

 

다음 그림은 GIC v3 인터럽트 컨트롤러를 사용하여 단계별로 인터럽트 핸들러가 처리되는 과정을 보여준다.

 

다음 그림은 RPI2용 두 개의 인터럽트 컨트롤러가 cascade 연결 구성하여 단계별로 인터럽트 핸들러가 처리되는 과정을 보여준다.

 

다음 그림은 GIC v3 인터럽트 컨트롤러에서 ppi 중 특정 인터럽트가 irq 파티션으로 cascade 연결 구성하여 단계별로 인터럽트 핸들러가 처리되는 과정을 보여준다.

 

irq별 flow 핸들러 설정

irq_set_handler()

include/linux/irq.h

static inline void
irq_set_handler(unsigned int irq, irq_flow_handler_t handle)
{
        __irq_set_handler(irq, handle, 0, NULL);
}

요청한 @irq에 대해 사용될 인터럽트 핸들러를 설정한다.

 

chained irq flow 핸들러 설정

irq_set_chained_handler()

include/linux/irq.h

/*
 * Set a highlevel chained flow handler for a given IRQ.
 * (a chained handler is automatically enabled and set to
 *  IRQ_NOREQUEST, IRQ_NOPROBE, and IRQ_NOTHREAD) 
 */
static inline void
irq_set_chained_handler(unsigned int irq, irq_flow_handler_t handle)
{
        __irq_set_handler(irq, handle, 1, NULL);
}

요청한 @irq에 대해 하위 cascade 연결된 인터럽트 컨트롤러에게 인터럽트를 전달할 목적으로 사용되는 chained 핸들러를 설정한다.

  • rpi2: 로컬 인터럽트 컨트롤러가 부모이고 hwirq=8에 chained 핸들러를 설정한다.
    • chained handler: bcm2836_chained_handle_irq()

 

__irq_set_handler()

kernel/irq/chip.c

void
__irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
                  const char *name)
{
        unsigned long flags;
        struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);

        if (!desc)
                return;

        __irq_do_set_handler(desc, handle, is_chained, name);
        irq_put_desc_busunlock(desc, flags);
}
EXPORT_SYMBOL_GPL(__irq_set_handler);

@irq에 대해 irq 디스크립터에 대한 lock을 획득한채로 irq flow 핸들러를 지정한다.

  • 코드 라인 6~9에서 irq 디스크립터에 대한 lock을 획득한다.
    • 세 번째 인자에 대해 0을 지정하여 칩에 대한 락은 획득하지 않는다.
  • 코드 라인 11에서 irq 핸들러를 설정하기 위해 호출한다.
  • 코드 라인 12에서 irq 디스크립터에 대한 lock을 해제한다.

 

__irq_do_set_handler()

kernel/irq/chip.c

static void
__irq_do_set_handler(struct irq_desc *desc, irq_flow_handler_t handle,
                     int is_chained, const char *name)
{
        if (!handle) {
                handle = handle_bad_irq;
        } else {
                struct irq_data *irq_data = &desc->irq_data;
#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
                /*
                 * With hierarchical domains we might run into a
                 * situation where the outermost chip is not yet set
                 * up, but the inner chips are there.  Instead of
                 * bailing we install the handler, but obviously we
                 * cannot enable/startup the interrupt at this point.
                 */
                while (irq_data) {
                        if (irq_data->chip != &no_irq_chip)
                                break;
                        /*
                         * Bail out if the outer chip is not set up
                         * and the interrupt supposed to be started
                         * right away.
                         */
                        if (WARN_ON(is_chained))
                                return;
                        /* Try the parent */
                        irq_data = irq_data->parent_data;
                }
#endif
                if (WARN_ON(!irq_data || irq_data->chip == &no_irq_chip))
                        return;
        }

        /* Uninstall? */
        if (handle == handle_bad_irq) {
                if (desc->irq_data.chip != &no_irq_chip)
                        mask_ack_irq(desc);
                irq_state_set_disabled(desc);
                if (is_chained)
                        desc->action = NULL;
                desc->depth = 1;
        }
        desc->handle_irq = handle;
        desc->name = name;

        if (handle != handle_bad_irq && is_chained) {
                unsigned int type = irqd_get_trigger_type(&desc->irq_data);

                /*
                 * We're about to start this interrupt immediately,
                 * hence the need to set the trigger configuration.
                 * But the .set_type callback may have overridden the
                 * flow handler, ignoring that we're dealing with a
                 * chained interrupt. Reset it immediately because we
                 * do know better.
                 */
                if (type != IRQ_TYPE_NONE) {
                        __irq_set_trigger(desc, type);
                        desc->handle_irq = handle;
                }

                irq_settings_set_noprobe(desc);
                irq_settings_set_norequest(desc);
                irq_settings_set_nothread(desc);
                desc->action = &chained_action;
                irq_activate_and_startup(desc, IRQ_RESEND);
        }
}

요청한 irq 디스크립터에 인터럽트 flow 핸들러와 이름을 대입하고 activate 한다.

  • 코드 라인 5~6에서 인자 @handle이 지정되지 않은 경우 인터럽트 핸들러로 handle_bad_irq() 함수를 대신 사용한다.
  • 코드 라인 7~33에서 커널이 irq 도메인 hierarchy를 지원하는 경우 핸들러가 설정되지 않은 최상위 irq 디스크립터를 찾는다.
    • 2 개 이상의 인터럽트 컨트롤러 칩을 거치는 경우 irq_data가 parent irq_data에 연결되어 구성된다.
  • 코드 라인 36~43에서 지정된 핸들러 함수가 없는 경우 irq 상태를 disable로 변경하고 depth=1로 설정한다. 기존에 chip 정보가 지정된 경우 irq를 mask 상태로 바꾼다.
  • 코드 라인 44~45에서 핸들러 함수와 이름을 대입한다.
  • 코드 라인 47~68에서 핸들러 함수가 지정되었고 chain 걸린 경우 irq 상태를 noprobe, norequest, nothread로 바꾸고 irq를 enable하고 activate 한다.
    • irq_desc->irq_data->state_use_accessors에 _IRQ_NOPROBE, _IRQ_NOREQUEST, _IRQ_NOTHREAD 플래그를 추가한다.

 

chip 및 IRQ 핸들러 설정

irq_set_chip_and_handler()

include/linux/irq.h

static inline void irq_set_chip_and_handler(unsigned int irq, struct irq_chip *chip,
                                            irq_flow_handler_t handle)
{
        irq_set_chip_and_handler_name(irq, chip, handle, NULL);
}

irq 디스크립터에 chip, flow handler를 대입한다. 단 이름(name)에는 null을 대입한다.

 

irq_set_chip_and_handler_name()

kernel/irq/chip.c

void
irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
                              irq_flow_handler_t handle, const char *name)
{
        irq_set_chip(irq, chip);
        __irq_set_handler(irq, handle, 0, name);
}
EXPORT_SYMBOL_GPL(irq_set_chip_and_handler_name);

irq 디스크립터에 chip 정보를 대입한다. 그리고 flow 핸들러와 이름을 대입하다.

 

per-cpu 지원  IRQ 설정

irq_set_percpu_devid()

kernel/irq/irqdesc.c

int irq_set_percpu_devid(unsigned int irq)
{
        return irq_set_percpu_devid_partition(irq, NULL);
}

@irq 번호에 대해 per-cpu 인터럽트로 설정한다.

  • GIC 컨트롤러등에서 SGI/PPI 인터럽트들은 같은 번호로 대응하는 모든 cpu에 대해 처리할 수 있다.
  • NULL을 입력하는 경우 모든 cpu들을 대상으로 지정한다.

 

irq_set_percpu_devid_partition()

kernel/irq/irqdesc.c

int irq_set_percpu_devid_partition(unsigned int irq,
                                   const struct cpumask *affinity)
{
        struct irq_desc *desc = irq_to_desc(irq);

        if (!desc)
                return -EINVAL;

        if (desc->percpu_enabled)
                return -EINVAL;

        desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);

        if (!desc->percpu_enabled)
                return -ENOMEM;

        if (affinity)
                desc->percpu_affinity = affinity;
        else
                desc->percpu_affinity = cpu_possible_mask;

        irq_set_percpu_devid_flags(irq);
        return 0;
}

요청 irq를 per-cpu(코어별로 라우팅되는) 처리 용도로 준비한다. (@affinity가 null인 경우 모든 possible cpu에 대해 지정한다)

  • 코드 라인 4~7에서 요청 irq번호에 맞는 irq 디스크립터가 없는 경우 -EINVAL 에러를 반환한다.
  • 코드 라인 9~10에서 이미 per-cpu로 설정된 경우 -EINVAL 에러를 반환한다.
  • 코드 라인 12~15에서 cpu 비트맵을 할당하여 percpu_enabled에 대입한다. 할당이 실패한 경우 -ENOMEM 에러를 반환한다.
  • 코드 라인 17~20에서 @affinity 설정이 있으면 해당 affinity cpu들에 대해 지정하고, null인 경우 모든 possible cpu를 대상으로 지정한다.
  • 코드 라인 22에서 irqd에 noautoen, per_cpu, nothread, noprobe, per_cpu_devid 플래그를 설정한다.
  • 코드 라인 23에서 정상 값 0을 반환한다.

참고: genirq: Add support for per-cpu dev_id interrupts 

 

irq_set_percpu_devid_flags()

include/linux/irq.h

static inline void irq_set_percpu_devid_flags(unsigned int irq)
{
        irq_set_status_flags(irq,
                             IRQ_NOAUTOEN | IRQ_PER_CPU | IRQ_NOTHREAD |
                             IRQ_NOPROBE | IRQ_PER_CPU_DEVID);
}

irq 상태로 noautoen, per_cpu, nothread, noprobe, per_cpu_devid 플래그를 설정한다.

 


generic 인터럽트 flow 핸들러 수행

generic_handle_irq()

kernel/irq/irqdesc.c

/**
 * generic_handle_irq - Invoke the handler for a particular irq
 * @irq:        The irq number to handle
 *
 */
int generic_handle_irq(unsigned int irq)
{
        struct irq_desc *desc = irq_to_desc(irq);

        if (!desc)
                return -EINVAL;
        generic_handle_irq_desc(irq, desc);
        return 0;
}
EXPORT_SYMBOL_GPL(generic_handle_irq);

요청한 @irq 번호에 해당하는 generic 핸들러 함수를 호출한다.

 

generic_handle_irq_desc()

include/linux/irqdesc.h

/*
 * Architectures call this to let the generic IRQ layer
 * handle an interrupt.
 */
static inline void generic_handle_irq_desc(unsigned int irq, struct irq_desc *desc)
{
        desc->handle_irq(irq, desc);
}

요청한 @irq 디스크립터에 등록한 generic 핸들러 함수를 호출한다.

 

다양한 generic 인터럽트 flow 핸들러들

generic 인터럽트 핸들러가 각각의 형태별로 미리 준비되어 있다. 이들을 간단히 알아보자.

  • handle_percpu_irq()
    • per-cpu용 인터럽트 핸들러
    • ack -> handle_irq_event_percpu() 호출 -> eoi
  • handle_level_irq()
    • 레벨 타입 트리거 설정된 경우 사용할 인터럽트 핸들러
    • mask_ack -> handle_irq_event() 호출 -> unmask(oneshot 스레드 완료시)
  • handle_simple_irq()
    • 별도의 ack, mask, unmask, eoi 등의 HW 처리가 전혀 필요 없는 핸들러이다.
    • handle_irq_event() 호출
  • handle_fasteoi_irq()
    • transparent 컨트롤러들을 위한 핸들러
    • mask(oneshot 일때) -> preflow 핸들러 호출 -> handle_irq_event() 호출 -> eoiunmask(oneshot 스레드 완료시)
  • handle_edge_irq()
    • 엣지 타입 트리거 설정된 경우 사용할 인터럽트 핸들러
    • handle_level_irq()와 다른 점은 mask/unmask 처리를 하지 않는다.
    • ack -> handle_irq_event() 호출
  • handle_edge_eoi_irq()
    • 엣지 eoi 타입 트리거 설정된 경우 사용할 인터럽트 핸들러
    • powerpc 아키텍처에서만 사용하는 특수한 형태이다.
    • handle_irq_event() 호출 -> eoi
  •  handle_nested_irq()
    •  irq thread로부터의 nested irq 핸들러
    • action->(*thread_fn) 호출
  • handle_percpu_devid_irq()
    • per-cpu용 인터럽트 핸들러
    • ack -> action->(*handler) 호출 -> eoi
  • handle_fasteoi_nmi()
    • Pesudo-NMI로 사용되는 인터럽트 핸들러
    • -> action->(*handler) 호출 -> eoi
  • handle_percpu_devid_fasteoi_nmi()
    • Pesudo-NMI로 사용되는 per-cpu 타입 인터럽트 핸들러
    • -> action->(*handler) 호출 -> eoi

 

다음 그림은 커널에 준비된 generic 인터럽트 핸들러들의 기능을 보여준다.

 

다음 그림은 타이머 인터럽트 핸들러가 호출되는 과정을 보여준다.

  • 1단계: exception vector
  • 2단계: controller 핸들러
  • 3단계: irq 라인(디스크립터)에 설정된 generic core 인터럽트 핸들러 (hierarchy 가능)
  • 4단계: 최종 consumer 디바이스에 위치한 인터럽트 핸들러(action에 등록된)
  • 5단계(option): consumer 디바이스는 별도의 dispatch 이벤트 핸들러를 사용하기도 한다.

 

핸들러 함수 -1- (level)

handle_level_irq()

kernel/irq/chip.c

/**
 *      handle_level_irq - Level type irq handler
 *      @irq:   the interrupt number
 *      @desc:  the interrupt description structure for this irq
 *
 *      Level type interrupts are active as long as the hardware line has
 *      the active level. This may require to mask the interrupt and unmask
 *      it after the associated handler has acknowledged the device, so the
 *      interrupt line is back to inactive.
 */
void    
handle_level_irq(unsigned int irq, struct irq_desc *desc)
{
        raw_spin_lock(&desc->lock);
        mask_ack_irq(desc);

        if (!irq_may_run(desc))
                goto out_unlock;

        desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
        kstat_incr_irqs_this_cpu(irq, desc);

        /*
         * If its disabled or no action available
         * keep it masked and get out of here
         */ 
        if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
                desc->istate |= IRQS_PENDING;
                goto out_unlock;
        }                               
                                        
        handle_irq_event(desc);
                        
        cond_unmask_irq(desc);

out_unlock:                
        raw_spin_unlock(&desc->lock);
}               
EXPORT_SYMBOL_GPL(handle_level_irq);

레벨 타입의 트리거를 가진 인터럽트 핸들러로 인터럽트 처리 전에 mask와 ack 처리를 수행하고 인터럽트 처리 후에 조건부로 unmask를 수행한다.

  • 코드 라인 5에서 인터럽트 컨트롤러를 통해 mask와 ack 처리를 한다.
  • 코드 라인 7~8에서 irq의 실행이 곧 준비되지 않을 것 같으면 인터럽트 처리를 포기하고 빠져나간다.
  • 코드 라인 10에서 istate에서 replay 및 waiting 플래그를 제거한다.
  • 코드 라인 11에서 해당 인터럽트 카운터를 증가시킨다.
  • 코드 라인 17~20에서 irq 디스크립터에 action 핸들러가 없거나 disable 설정된 경우 istate에 pending 플래그를 추가하고 함수를 빠져나간다.
  • 코드 라인 22에서 irq 디스크립터의 istate에서 pending 플래그를 제거하고, 모든 action에 등록한 인터럽트 핸들러들을 호출하여 수행한다. 수행 중인 동안에는 irq 진행중 상태가 유지된다.
  • 코드 라인 24에서 조건에 따라 unmask 처리를 수행한다.

 

핸들러 함수 -2- (percpu, devid)

handle_percpu_devid_irq()

kernel/irq/chip.c

/**
 * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
 * @desc:       the interrupt description structure for this irq
 *
 * Per CPU interrupts on SMP machines without locking requirements. Same as
 * handle_percpu_irq() above but with the following extras:
 *
 * action->percpu_dev_id is a pointer to percpu variables which
 * contain the real device id for the cpu on which this handler is
 * called
 */
void handle_percpu_devid_irq(struct irq_desc *desc)
{
        struct irq_chip *chip = irq_desc_get_chip(desc);
        struct irqaction *action = desc->action;
        unsigned int irq = irq_desc_get_irq(desc);
        irqreturn_t res;

        /*
         * PER CPU interrupts are not serialized. Do not touch
         * desc->tot_count.
         */
        __kstat_incr_irqs_this_cpu(desc);

        if (chip->irq_ack)
                chip->irq_ack(&desc->irq_data);

        if (likely(action)) {
                trace_irq_handler_entry(irq, action);
                res = action->handler(irq, raw_cpu_ptr(action->percpu_dev_id));
                trace_irq_handler_exit(irq, action, res);
        } else {
                unsigned int cpu = smp_processor_id();
                bool enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);

                if (enabled)
                        irq_percpu_disable(desc, cpu);

                pr_err_once("Spurious%s percpu IRQ%u on CPU%u\n",
                            enabled ? " and unmasked" : "", irq, cpu);
        }

        if (chip->irq_eoi)
                chip->irq_eoi(&desc->irq_data);
}

요청 irq의 generic per-cpu 핸들러로, action에 등록한 핸들러 함수를 호출 시 irq의 현재 cpu에 대한 dev_id를 인수로 전달한다. 핸들러 함수 호출 전후로 ack 및 eoi 처리를 한다.

  • 코드 라인 17에서 요청한 irq 디스크립터의 action에 등록한 per-cpu dev_id 값을 알아온다.
  • 코드 라인 12에서 현재 cpu에 대한 kstat 카운터를 증가시킨다.
  • 코드 라인 14~15에서 핸들러 함수 호출 전에 irq_chip의 irq_ack 콜백이 등록된 경우 호출한다.
  • 코드 라인 17~30에서 등록된 action 핸들러 함수를 호출한다. 호출 시 irq 번호와 dev_id를 인수로 넘겨준다. 현재 cpu에 해당하는 action 핸들러 함수가 없는 경우 disable 한다.
  • 코드 라인 32~33에서 핸들러 함수 호출 후에 irq_chip의 irq_eoi 콜백이 등록된 경우 호출한다.

 

핸들러 함수 -3- (fasteoi)

handle_fasteoi_irq()

kernel/irq/chip.c

/**
 *      handle_fasteoi_irq - irq handler for transparent controllers
 *      @desc:  the interrupt description structure for this irq
 *
 *      Only a single callback will be issued to the chip: an ->eoi()
 *      call when the interrupt has been serviced. This enables support
 *      for modern forms of interrupt handlers, which handle the flow
 *      details in hardware, transparently.
 */
void handle_fasteoi_irq(struct irq_desc *desc)
{
        struct irq_chip *chip = desc->irq_data.chip;

        raw_spin_lock(&desc->lock);

        if (!irq_may_run(desc))
                goto out;

        desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);

        /*
         * If its disabled or no action available
         * then mask it and get out of here:
         */
        if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
                desc->istate |= IRQS_PENDING;
                mask_irq(desc);
                goto out;
        }

        kstat_incr_irqs_this_cpu(desc);
        if (desc->istate & IRQS_ONESHOT)
                mask_irq(desc);

        preflow_handler(desc);
        handle_irq_event(desc);

        cond_unmask_eoi_irq(desc, chip);

        raw_spin_unlock(&desc->lock);
        return;
out:
        if (!(chip->flags & IRQCHIP_EOI_IF_HANDLED))
                chip->irq_eoi(&desc->irq_data);
        raw_spin_unlock(&desc->lock);
}
EXPORT_SYMBOL_GPL(handle_fasteoi_irq);

해당 인터럽트에 등록된 action 핸들러를 수행하고 eoi 처리를 위해 irq_chip 드라이버의 (*irq_eoi) 후크 함수를 호출한다.

  • 코드 라인 7~8에서 인터럽트를 처리할 수 있는 상황이 아닌 경우 out: 레이블로 이동하여 eoi 처리만 수행하고 함수를 빠져나간다.
  • 코드 라인 10에서 인터럽트 state에서 IRQS_REPLAY와 IRQS_WAITING을 제거한다.
  • 코드 라인 16~20에서 action 핸들러가 지정되지 않았거나 인터럽트 설정이 disable된 경우 인터럽트 state에 IRQS_PENDING을 추가하고 컨트롤러에 해당 인터럽트를 mask 한 후 out: 레이블로 이동하여 eoi 처리만 수행하고 함수를 빠져나간다.
  • 코드 라인 22에서 현재 cpu에 대한 kstat 카운터를 증가시킨다.
  • 코드 라인 23~24에서 oneshot 인터럽트인 경우 해당 인터럽트를 mask 한다.
  • 코드 라인 26에서 preflow 핸들러가 설정된 경우 이를 호출한다.
  • 코드 라인 27에서 해당 인터럽트에 등록된 action 핸들러를 호출한다.
  • 코드 라인 29~32에서 몇 가지 조건에 따라 eoi 처리를 위해 irq_chip 드라이버의 (*irq_eoi) 후크 함수를 호출한 후 함수를 정상적으로 완료한다.
  • 코드 라인 33~35에서 out: 레이블이다. IRQCHIP_EOI_IF_HANDLED 플래그 설정이 없는 경우 eoi 처리를 위해 irq_chip 드라이버의 (*irq_eoi) 후크 함수를 호출한다.

 

핸들러 함수 -4- (fasteoi, NMI)

handle_fasteoi_nmi()

kernel/irq/chip.c

/**
 *      handle_fasteoi_nmi - irq handler for NMI interrupt lines
 *      @desc:  the interrupt description structure for this irq
 *
 *      A simple NMI-safe handler, considering the restrictions
 *      from request_nmi.
 *
 *      Only a single callback will be issued to the chip: an ->eoi()
 *      call when the interrupt has been serviced. This enables support
 *      for modern forms of interrupt handlers, which handle the flow
 *      details in hardware, transparently.
 */
void handle_fasteoi_nmi(struct irq_desc *desc)
{
        struct irq_chip *chip = irq_desc_get_chip(desc);
        struct irqaction *action = desc->action;
        unsigned int irq = irq_desc_get_irq(desc);
        irqreturn_t res;

        __kstat_incr_irqs_this_cpu(desc);

        trace_irq_handler_entry(irq, action);
        /*
         * NMIs cannot be shared, there is only one action.
         */
        res = action->handler(irq, action->dev_id);
        trace_irq_handler_exit(irq, action, res);

        if (chip->irq_eoi)
                chip->irq_eoi(&desc->irq_data);
}
EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);

해당 인터럽트에 등록된 action 핸들러를 수행하고 eoi 처리를 위해 irq_chip 드라이버의 (*irq_eoi) 후크 함수를 호출한다.

  • 코드 라인 8에서 현재 cpu에 대한 kstat 카운터를 증가시킨다.
  • 코드 라인 14에서 해당 인터럽트에 등록된 action 핸들러를 호출한다.
  • 코드 라인 17~18에서 eoi 처리를 위해 irq_chip 드라이버의 (*irq_eoi) 후크 함수를 호출한다.

 

핸들러 함수 -5- (percpu, devid, fasteoi, NMI)

handle_percpu_devid_fasteoi_nmi()

kernel/irq/chip.c

/**
 * handle_percpu_devid_fasteoi_nmi - Per CPU local NMI handler with per cpu
 *                                   dev ids
 * @desc:       the interrupt description structure for this irq
 *
 * Similar to handle_fasteoi_nmi, but handling the dev_id cookie
 * as a percpu pointer.
 */
void handle_percpu_devid_fasteoi_nmi(struct irq_desc *desc)
{
        struct irq_chip *chip = irq_desc_get_chip(desc);
        struct irqaction *action = desc->action;
        unsigned int irq = irq_desc_get_irq(desc);
        irqreturn_t res;

        __kstat_incr_irqs_this_cpu(desc);

        trace_irq_handler_entry(irq, action);
        res = action->handler(irq, raw_cpu_ptr(action->percpu_dev_id));
        trace_irq_handler_exit(irq, action, res);

        if (chip->irq_eoi)
                chip->irq_eoi(&desc->irq_data);
}

해당 per-cpu 인터럽트에 등록된 action 핸들러를 수행하고 eoi 처리를 위해 irq_chip 드라이버의 (*irq_eoi) 후크 함수를 호출한다.

  • 코드 라인 8에서 현재 cpu에 대한 kstat 카운터를 증가시킨다.
  • 코드 라인 11에서 해당 인터럽트에 등록된 action 핸들러를 호출한다.
  • 코드 라인 14~15에서 eoi 처리를 위해 irq_chip 드라이버의 (*irq_eoi) 후크 함수를 호출한다.

 

핸들러 함수 -6- (simple)

handle_simple_irq()

kernel/irq/chip.c

/**
 *      handle_simple_irq - Simple and software-decoded IRQs.
 *      @desc:  the interrupt description structure for this irq
 *
 *      Simple interrupts are either sent from a demultiplexing interrupt
 *      handler or come from hardware, where no interrupt hardware control
 *      is necessary.
 *
 *      Note: The caller is expected to handle the ack, clear, mask and
 *      unmask issues if necessary.
 */
void handle_simple_irq(struct irq_desc *desc)
{
        raw_spin_lock(&desc->lock);

        if (!irq_may_run(desc))
                goto out_unlock;

        desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);

        if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
                desc->istate |= IRQS_PENDING;
                goto out_unlock;
        }

        kstat_incr_irqs_this_cpu(desc);
        handle_irq_event(desc);

out_unlock:
        raw_spin_unlock(&desc->lock);
}
EXPORT_SYMBOL_GPL(handle_simple_irq);

해당 인터럽트에 등록된 action 핸들러를 수행한다. 단 ack 및 eoi 등의 hw 제어는 하나도 처리하지 않는다.

  • 코드 라인 5~6에서 인터럽트를 처리할 수 있는 상황이 아닌 경우 out_unlock: 레이블로 이동하여 함수를 빠져나간다.
  • 코드 라인 8에서 인터럽트 state에서 IRQS_REPLAY와 IRQS_WAITING을 제거한다.
  • 코드 라인 10~13에서 action 핸들러가 지정되지 않았거나 인터럽트 설정이 disable된 경우 인터럽트 state에 IRQS_PENDING을 추가하고 out_unlock 레이블로 이동하여 함수를 빠져나간다.
  • 코드 라인 15에서 현재 cpu에 대한 kstat 카운터를 증가시킨다.
  • 코드 라인 16에서 해당 인터럽트에 등록된 action 핸들러를 호출한다.

 

핸들러 함수 -7- (edge)

handle_edge_irq()

kernel/irq/chip.c

/**
 *      handle_edge_irq - edge type IRQ handler
 *      @desc:  the interrupt description structure for this irq
 *
 *      Interrupt occures on the falling and/or rising edge of a hardware
 *      signal. The occurrence is latched into the irq controller hardware
 *      and must be acked in order to be reenabled. After the ack another
 *      interrupt can happen on the same source even before the first one
 *      is handled by the associated event handler. If this happens it
 *      might be necessary to disable (mask) the interrupt depending on the
 *      controller hardware. This requires to reenable the interrupt inside
 *      of the loop which handles the interrupts which have arrived while
 *      the handler was running. If all pending interrupts are handled, the
 *      loop is left.
 */
void handle_edge_irq(struct irq_desc *desc)
{
        raw_spin_lock(&desc->lock);

        desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);

        if (!irq_may_run(desc)) {
                desc->istate |= IRQS_PENDING;
                mask_ack_irq(desc);
                goto out_unlock;
        }

        /*
         * If its disabled or no action available then mask it and get
         * out of here.
         */
        if (irqd_irq_disabled(&desc->irq_data) || !desc->action) {
                desc->istate |= IRQS_PENDING;
                mask_ack_irq(desc);
                goto out_unlock;
        }

        kstat_incr_irqs_this_cpu(desc);

        /* Start handling the irq */
        desc->irq_data.chip->irq_ack(&desc->irq_data);

        do {
                if (unlikely(!desc->action)) {
                        mask_irq(desc);
                        goto out_unlock;
                }

                /*
                 * When another irq arrived while we were handling
                 * one, we could have masked the irq.
                 * Renable it, if it was not disabled in meantime.
                 */
                if (unlikely(desc->istate & IRQS_PENDING)) {
                        if (!irqd_irq_disabled(&desc->irq_data) &&
                            irqd_irq_masked(&desc->irq_data))
                                unmask_irq(desc);
                }

                handle_irq_event(desc);

        } while ((desc->istate & IRQS_PENDING) &&
                 !irqd_irq_disabled(&desc->irq_data));

out_unlock:
        raw_spin_unlock(&desc->lock);
}
EXPORT_SYMBOL(handle_edge_irq);

해당 edge 트리거 타입 인터럽트에 등록된 action 핸들러를 수행한다. action 핸들러 수행 전에 ack를 처리한다.

  • 코드 라인 5에서 인터럽트 state에서 IRQS_REPLAY와 IRQS_WAITING을 제거한다.
  • 코드 라인 7~11에서 인터럽트를 처리할 수 있는 상황이 아닌 경우 인터럽트 state에 IRQS_PENDING을 추가하고, 인터럽트를 mask 처리한 후 out_unlock: 레이블로 이동하여 함수를 빠져나간다.
  • 코드 라인 17~21에서 action 핸들러가 지정되지 않았거나 인터럽트 설정이 disable된 경우 인터럽트 state에 IRQS_PENDING을 추가하고, 인터럽트를 mask 처리한 후 out_unlock: 레이블로 이동하여 함수를 빠져나간다.
  • 코드 라인 23에서 현재 cpu에 대한 kstat 카운터를 증가시킨다.
  • 코드 라인 26에서 ack 처리를 위해 irq_chip 드라이버의 (*irq_ack) 후크 함수를 호출한다.
  • 코드 라인 28~48에서 펜딩된 인터럽트가 있는 경우 반복하며 해당 인터럽트에 등록된 action 핸들러를 호출한다.
    • action 핸들러가 지정되지 않은 경우 인터럽트를 mask 처리한 후 out_unlock: 레이블로 이동하여 함수를 빠져나간다.
    • 펜딩된 irq가 있는데 irq가 mask된 상태인 경우 인터럽트를 unmask 처리한다.

 

핸들러 함수 -8- (nested)

handle_nested_irq()

kernel/irq/chip.c

/*
 *      handle_nested_irq - Handle a nested irq from a irq thread
 *      @irq:   the interrupt number
 *
 *      Handle interrupts which are nested into a threaded interrupt
 *      handler. The handler function is called inside the calling
 *      threads context.
 */
void handle_nested_irq(unsigned int irq)
{
        struct irq_desc *desc = irq_to_desc(irq);
        struct irqaction *action;
        irqreturn_t action_ret;

        might_sleep();

        raw_spin_lock_irq(&desc->lock);

        desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);

        action = desc->action;
        if (unlikely(!action || irqd_irq_disabled(&desc->irq_data))) {
                desc->istate |= IRQS_PENDING;
                goto out_unlock;
        }

        kstat_incr_irqs_this_cpu(desc);
        irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
        raw_spin_unlock_irq(&desc->lock);

        action_ret = IRQ_NONE;
        for_each_action_of_desc(desc, action)
                action_ret |= action->thread_fn(action->irq, action->dev_id);

        if (!noirqdebug)
                note_interrupt(desc, action_ret);

        raw_spin_lock_irq(&desc->lock);
        irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);

out_unlock:
        raw_spin_unlock_irq(&desc->lock);
}
EXPORT_SYMBOL_GPL(handle_nested_irq);

hw interrupt context가 아닌 irq 스레드 context에서 동작하며, 해당 인터럽트에 등록된 모든 action 스레드 핸들러를 수행한다.

  • 코드 라인 7에서 Preemption point를 위해 필요 시 슬립한다.
  • 코드 라인 11에서 인터럽트 state에서 IRQS_REPLAY와 IRQS_WAITING을 제거한다.
  • 코드 라인 13~17에서 action 핸들러가 지정되지 않았거나 인터럽트 설정이 disable된 경우 인터럽트 state에 IRQS_PENDING을 추가하고out_unlock: 레이블로 이동하여 함수를 빠져나간다.
  • 코드 라인 19에서 현재 cpu에 대한 kstat 카운터를 증가시킨다.
  • 코드 라인 20에서 핸들러를 호출하는 동안 IRQD_IRQ_INPROGRESS 플래그를 설정한다.
  • 코드 라인 23~25에서 인터럽트에 등록된 모든 action 스레드 핸들러를 호출한다.
  • 코드 라인31에서 핸들러를 호출하는 동안 사용한 IRQD_IRQ_INPROGRESS 플래그를 클리어한다.

 

핸들러 공용 함수들

irq_may_run()

kernel/irq/chip.c

static bool irq_may_run(struct irq_desc *desc)
{
        unsigned int mask = IRQD_IRQ_INPROGRESS | IRQD_WAKEUP_ARMED;

        /*
         * If the interrupt is not in progress and is not an armed
         * wakeup interrupt, proceed.
         */
        if (!irqd_has_set(&desc->irq_data, mask))
                return true;

        /*
         * If the interrupt is an armed wakeup source, mark it pending
         * and suspended, disable it and notify the pm core about the
         * event.
         */
        if (irq_pm_check_wakeup(desc))
                return false;

        /*
         * Handle a potential concurrent poll on a different core.
         */
        return irq_check_poll(desc);
}

irq의 실행이 곧 준비될 것 같으면 기다린다. 만일 처리가 불가능하면 false를 반환하고, 인터럽트 처리가 가능한 경우 true를 반환한다.

  • 코드 라인 9~10에서 irq 처리가 진행중이거나 armed wakeup 인터럽트가 아닌 경우 true를 반환하여 인터럽트가 계속 진행되게 한다.
  • 코드 라인 17~18에서 CONFIG_PM_SLEEP 커널 옵션을 사용하는 경우 Power Management 기능을 사용할 수 있는데 suspend 하지 않은 경우 true를 반환한다.
    • armed wakeup 인터럽트인 경우 인터럽트 디스크립터의 istate 상태에 suspended 및 pending을 추가하고 깨울 때까지 대기한다.
  • 코드 라인 23에서 irq polling을 중단 설정하지 않은 경우 다른 cpu에서 irq polling을 수행하는 중이면 완료될 때까지 기다린다.

 

핸들러 최종 호출 단계들

handle_irq_event()

kernel/irq/handle.c

irqreturn_t handle_irq_event(struct irq_desc *desc)
{
        irqreturn_t ret;

        desc->istate &= ~IRQS_PENDING;
        irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
        raw_spin_unlock(&desc->lock);

        ret = handle_irq_event_percpu(desc, action);

        raw_spin_lock(&desc->lock);
        irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
        return ret;
}

percpu 타입이 아닌 일반 irq 디스크립터에 등록된 모든 action 핸들러들을 호출하여 수행한다.

  • irq 디스크립터의 istate에서 pending 플래그를 제거하고 모든 action에 등록한 인터럽트 핸들러들을 호출하여 수행한다.
  • 수행 중인 동안에는 irq 진행중 상태가 유지된다.

 

handle_irq_event_percpu()

kernel/irq/handle.c

irqreturn_t handle_irq_event_percpu(struct irq_desc *desc)
{
        irqreturn_t retval;
        unsigned int flags = 0;

        retval = __handle_irq_event_percpu(desc, &flags);

        add_interrupt_randomness(desc->irq_data.irq, flags);

        if (!noirqdebug)
                note_interrupt(desc, retval);
        return retval;
}

percpu 타입 irq 디스크립터에 등록된 모든 action 핸들러들을 호출하여 수행한다.

 

__handle_irq_event_percpu()

kernel/irq/handle.c

irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc, unsigned int *flags)
{
        irqreturn_t retval = IRQ_NONE;
        unsigned int irq = desc->irq_data.irq;
        struct irqaction *action;

        record_irq_time(desc);

        for_each_action_of_desc(desc, action) {
                irqreturn_t res;

                trace_irq_handler_entry(irq, action);
                res = action->handler(irq, action->dev_id);
                trace_irq_handler_exit(irq, action, res);

                if (WARN_ONCE(!irqs_disabled(),"irq %u handler %pS enabled interrupts\n",
                              irq, action->handler))
                        local_irq_disable();

                switch (res) {
                case IRQ_WAKE_THREAD:
                        /*
                         * Catch drivers which return WAKE_THREAD but
                         * did not set up a thread function
                         */
                        if (unlikely(!action->thread_fn)) {
                                warn_no_thread(irq, action);
                                break;
                        }

                        __irq_wake_thread(desc, action);

                        /* Fall through - to add to randomness */
                case IRQ_HANDLED:
                        *flags |= action->flags;
                        break;

                default:
                        break;
                }

                retval |= res;
        }

        return retval;
}

percpu 타입 irq 디스크립터에 등록된 모든 action 핸들러들을 호출하여 수행한다.

  • 코드 라인 9~14에서 irq 디스크립터에 등록된 모든 action 핸들러를 순회하며 호출한다.
  • 코드 라인 16~18에서 핸들러 수행 후 local irq가 disable 상태를 유지하지 못한 경우 경고 메시지를 출력하고 local irq를 disable 한다.
    • “irq N handler <function> enabled interrupts” 경고 메시지
  • 코드 라인 20~31에서 처리 결과가 IRQ_WAKE_THREAD인 경우 thread를 깨우고 계속 다음 행을 수행한다.
  • 코드 라인 34~40에서 처리 결과가 IRQ_HANDLED인 경우 flags에 action->flags를 추가하고, 그 외의 처리 결과는 skip 한다.
  • 코드 라인 42에서 retval에 처리 결과를 누적시키고 더 처리할 action이 있는 경우 루프를 돈다.

 


IRQ chip Operations

IRQ startup & shutdown

irq_startup()

kernel/irq/chip.c

int irq_startup(struct irq_desc *desc, bool resend, bool force)
{
        struct irq_data *d = irq_desc_get_irq_data(desc);
        struct cpumask *aff = irq_data_get_affinity_mask(d);
        int ret = 0;

        desc->depth = 0;

        if (irqd_is_started(d)) {
                irq_enable(desc);
        } else {
                switch (__irq_startup_managed(desc, aff, force)) {
                case IRQ_STARTUP_NORMAL:
                        ret = __irq_startup(desc);
                        irq_setup_affinity(desc);
                        break;
                case IRQ_STARTUP_MANAGED:
                        irq_do_set_affinity(d, aff, false);
                        ret = __irq_startup(desc);
                        break;
                case IRQ_STARTUP_ABORT:
                        irqd_set_managed_shutdown(d);
                        return 0;
                }
        }
        if (resend)
                check_irq_resend(desc);

        return ret;
}

요청 irq에 대해 enable 시키고 activate 처리를 수행하며 관련 콜백 함수를 호출한다.

  • 코드 라인 9~10에서 이미 startup된 경우 irq만 enable 한다.
  • 코드 라인 11~25에서 managed 상태에 따라 다음과 같이 처리한다.
    • normal 상태로 시작하는 경우 요청 irq의 startup을 수행 후 affinity를 선택 및 처리한다.
    • managed 상태로 시작하는 경우 요청 irq의 affnity 처리 후 startup을 처리한다.
    • abort 인 경우 irqd에 IRQD_MANAGED_SHUTDOWN 플래그를 설정한다.
  • 코드 라인 26~27에서 인자 @resend가 설정된 경우 pending된 irq를 다시 호출하게 tasklet을 리스케쥴하다.

 

__irq_startup()

kernel/irq/chip.c

static int __irq_startup(struct irq_desc *desc)
{
        struct irq_data *d = irq_desc_get_irq_data(desc);
        int ret = 0;

        /* Warn if this interrupt is not activated but try nevertheless */
        WARN_ON_ONCE(!irqd_is_activated(d));

        if (d->chip->irq_startup) {
                ret = d->chip->irq_startup(d);
                irq_state_clr_disabled(desc);
                irq_state_clr_masked(desc);
        } else {
                irq_enable(desc);
        }
        irq_state_set_started(desc);
        return ret;
}

요청 irq의 startup을 처리한다.

  • 코드 라인 9~12에서 irq_startup 후크에 등록된 함수가 있는 경우 이를 호출하고 disabled와 masked 상태를 클리어한다.
  • 코드 라인 13~15에서 irq_startup 후크에 비어 있는 경우 enable 처리한다.
  • 코드 라인 16에서 irqd에 IRQD_IRQ_STARTED 플래그를 설정한다.

 

irq_shutdown()

kernel/irq/chip.c

void irq_shutdown(struct irq_desc *desc)
{
        if (irqd_is_started(&desc->irq_data)) {
                desc->depth = 1;
                if (desc->irq_data.chip->irq_shutdown) {
                        desc->irq_data.chip->irq_shutdown(&desc->irq_data);
                        irq_state_set_disabled(desc);
                        irq_state_set_masked(desc);
                } else {
                        __irq_disable(desc, true);
                }
                irq_state_clr_started(desc);
        }
}

요청 irq에 대해 shutdown 또는 disable 처리한다.

  • 코드 라인 5~8에서 (*irq_shutdonw) 후크가 있으면 shutdown 처리를 수행한 후, irqd에 IRQD_IRQ_DISABLED및 IRQD_IRQ_MASKED플래그를 설정한다.
  • 코드 라인 9~11에서 (*irq_shutdonw) 후크가 있으면 disable 처리를 수행한다.
  • 코드 라인 12에서 irqd에 IRQD_IRQ_STARTED 플래그를 클리어한다.

 

IRQ Enable & Disable

irq_enable()

kernel/irq/chip.c

void irq_enable(struct irq_desc *desc)
{
        if (!irqd_irq_disabled(&desc->irq_data)) {
                unmask_irq(desc);
        } else {
                irq_state_clr_disabled(desc);
                if (desc->irq_data.chip->irq_enable) {
                        desc->irq_data.chip->irq_enable(&desc->irq_data);
                        irq_state_clr_masked(desc);
                } else {
                        unmask_irq(desc);
                }
        }
}

요청 irq를 enable 한다.

  • 코드 라인 3~4에서 irqd가 disable 상태가 아니면 unmask 처리한다.
  • 코드 라인 5~13에서 IRQD_IRQ_DISABLED 플래그를 제거하고, (*irq_enable) 후크를 호출한 후 IRQD_IRQ_MASKED 플래그를 제거한다. 만일 (*irq_enable) 후크가 없는 경우 unmask 처리한다.

 

irq_disable()

kernel/irq/chip.c

/**
 * irq_disable - Mark interrupt disabled
 * @desc:       irq descriptor which should be disabled
 *
 * If the chip does not implement the irq_disable callback, we
 * use a lazy disable approach. That means we mark the interrupt
 * disabled, but leave the hardware unmasked. That's an
 * optimization because we avoid the hardware access for the
 * common case where no interrupt happens after we marked it
 * disabled. If an interrupt happens, then the interrupt flow
 * handler masks the line at the hardware level and marks it
 * pending.
 *
 * If the interrupt chip does not implement the irq_disable callback,
 * a driver can disable the lazy approach for a particular irq line by
 * calling 'irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY)'. This can
 * be used for devices which cannot disable the interrupt at the
 * device level under certain circumstances and have to use
 * disable_irq[_nosync] instead.
 */
void irq_disable(struct irq_desc *desc)
{
        __irq_disable(desc, irq_settings_disable_unlazy(desc));
}

요청 irq를 disable 한다.

 

__irq_disable()

kernel/irq/chip.c

static void __irq_disable(struct irq_desc *desc, bool mask)
{
        if (irqd_irq_disabled(&desc->irq_data)) {
                if (mask)
                        mask_irq(desc);
        } else {
                irq_state_set_disabled(desc);
                if (desc->irq_data.chip->irq_disable) {
                        desc->irq_data.chip->irq_disable(&desc->irq_data);
                        irq_state_set_masked(desc);
                } else if (mask) {
                        mask_irq(desc);
                }
        }
}

요청 irq를 disable 한다.

  • 코드 라인 3~5에서 이미 disable 상태인 경우 @mask 설정이 있으면 mask 처리를 수행한다.
  • 코드 라인 6~14에서 IRQD_IRQ_DISABLED 플래그를 설정하고, (*irq_disable) 후크를 호출한 후 IRQD_IRQ_MASKED 플래그를 설정한다. 만일 (*irq_disable) 후크가 없는 경우 mask 처리한다.

 

IRQ mask & unmask

mask_irq()

kernel/irq/chip.c

void mask_irq(struct irq_desc *desc)
{
        if (irqd_irq_masked(&desc->irq_data))
                return;

        if (desc->irq_data.chip->irq_mask) {
                desc->irq_data.chip->irq_mask(&desc->irq_data);
                irq_state_set_masked(desc);
        }
}

요청 irq를 마스크한다.

  • 코드 라인 3~4에서 이미 인터럽트가 mask되어 있는 경우 함수를 빠져나간다.
  • 코드 라인 6~9에서 chip->irq_mask 후크에 등록된 콜백 함수를 호출하고 masked 플래그를 설정한다.

 

unmask_irq()

kernel/irq/chip.c

void unmask_irq(struct irq_desc *desc)
{
        if (!irqd_irq_masked(&desc->irq_data)) 
                return;

        if (desc->irq_data.chip->irq_unmask) {
                desc->irq_data.chip->irq_unmask(&desc->irq_data);
                irq_state_clr_masked(desc);
        }
}

요청 irq를 언마스크한다.

  • 코드 라인 3~4에서 이미 인터럽트가 unmask되어 있는 경우 함수를 빠져나간다.
  • 코드 라인 6~9에서 chip->irq_unmask 후크에 등록된 콜백 함수를 호출하고 masked 플래그를 클리어한다.

 

cond_unmask_irq()

kernel/irq/chip.c

/*
 * Called unconditionally from handle_level_irq() and only for oneshot
 * interrupts from handle_fasteoi_irq()
 */
static void cond_unmask_irq(struct irq_desc *desc)
{
        /*
         * We need to unmask in the following cases:
         * - Standard level irq (IRQF_ONESHOT is not set)
         * - Oneshot irq which did not wake the thread (caused by a
         *   spurious interrupt or a primary handler handling it
         *   completely).
         */
        if (!irqd_irq_disabled(&desc->irq_data) &&
            irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
                unmask_irq(desc);
}

조건에 따라 요청 irq를 언마스크한다.

  • 다음의 조건에서 unmask 처리를 수행하지 않는다.
    • 이미 인터럽트가 disable된 상태
    • 이미 인터럽트가 mask된 상태
    • oneshot 인터럽트의 경우
      • mask 상태를 유지하기 위해 unmask를 수행하지 않는다.

 

IRQ mask_ack & unmask_eoi

mask_ack_irq()

kernel/irq/chip.c

static inline void mask_ack_irq(struct irq_desc *desc)
{
        if (desc->irq_data.chip->irq_mask_ack) {
                desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
                irq_state_set_masked(desc);
        } else {
                mask_irq(desc);
                if (desc->irq_data.chip->irq_ack)
                        desc->irq_data.chip->irq_ack(&desc->irq_data);
        }
}

요청 irq에 대해 mask와 ack 처리를 하고 masked 플래그를 설정한다. 보통 같은 인터럽트에 대해 계속 진입되는 것을 방지가힉 위해 인터럽트 핸들러 초입에서 호출되며  마스크 처리와 응답 처리를 같이 수행하게 한다.

  • 코드 라인 3~5에서 인터럽트 컨트롤러의 irq_mask_ack 후크에 등록한 콜백 함수를 호출하고, irqd를 masked 상태로 설정한다.
  • 코드 라인 6~10에서 등록되지 않은 경우 mask 처리를 수행하고, irq_ack 후크에 등록된 콜백 함수를 연속해서 호출한다.

 

cond_unmask_eoi_irq()

kernel/irq/chip.c

static void cond_unmask_eoi_irq(struct irq_desc *desc, struct irq_chip *chip)
{
        if (!(desc->istate & IRQS_ONESHOT)) {
                chip->irq_eoi(&desc->irq_data);
                return;
        }
        /*
         * We need to unmask in the following cases:
         * - Oneshot irq which did not wake the thread (caused by a
         *   spurious interrupt or a primary handler handling it
         *   completely).
         */
        if (!irqd_irq_disabled(&desc->irq_data) &&
            irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot) {
                chip->irq_eoi(&desc->irq_data);
                unmask_irq(desc);
        } else if (!(chip->flags & IRQCHIP_EOI_THREADED)) {
                chip->irq_eoi(&desc->irq_data);
        }
}

요청 irq의 eoi 처리를 하고 조건에 따라 unmask 처리한다.

  • 코드 라인 3~6에서 인터럽트가 oneshot 설정되지 않은 경우 eoi 처리 후 함수를 빠져나간다.
  • 코드 라인 13~16에서 irq가 enable된 상태에서 mask되었고 oneshot 스레드 처리중이 아닌 경우 eoi 처리 및 unmask 처리를 한다.
    • 스레드가 완료되지 않고 동작중인 경우 unmask 처리하지 않는다.
  • 코드 라인 17~19에서 eoi 스레드 플래그가 설정되지 않은 경우 eoi 처리만 한다.

 

기타

irq_domain_activate_irq()

kernel/irq/irqdomain.c

/**
 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
 *                           interrupt
 * @irq_data:   Outermost irq_data associated with interrupt
 * @reserve:    If set only reserve an interrupt vector instead of assigning one
 *
 * This is the second step to call domain_ops->activate to program interrupt
 * controllers, so the interrupt could actually get delivered.
 */
int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
{
        int ret = 0;

        if (!irqd_is_activated(irq_data))
                ret = __irq_domain_activate_irq(irq_data, reserve);
        if (!ret)
                irqd_set_activated(irq_data);
        return ret;
}

irq domain에 구성된 최상위까지 activate 후크에 등록한 콜백 함수를 호출한다.

  • parent_data가 상위 irq_data를 가리키는 경우 재귀 호출된다.

 

irq_domain_deactivate_irq()

kernel/irq/irqdomain.c

/**
 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
 *                             deactivate interrupt
 * @irq_data: outermost irq_data associated with interrupt
 *
 * It calls domain_ops->deactivate to program interrupt controllers to disable
 * interrupt delivery.
 */
void irq_domain_deactivate_irq(struct irq_data *irq_data)
{
        if (irqd_is_activated(irq_data)) {
                __irq_domain_deactivate_irq(irq_data);
                irqd_clr_activated(irq_data);
        }
}

irq domain에 구성된 최상위까지 deactivate 후크에 등록한 콜백 함수를 호출한다.

  • parent_data가 상위 irq_data를 가리키는 경우 재귀 호출된다.

 

IRQ 설정

각 디바이스 드라이버에서 irq를 설정할 때 사용하는 함수는 다음 그림과 같다.

 

구조체

irq_chip 구조체

include/linux/irq.h

/**
 * struct irq_chip - hardware interrupt chip descriptor
 *
 * @parent_device:      pointer to parent device for irqchip
 * @name:               name for /proc/interrupts
 * @irq_startup:        start up the interrupt (defaults to ->enable if NULL)
 * @irq_shutdown:       shut down the interrupt (defaults to ->disable if NULL)
 * @irq_enable:         enable the interrupt (defaults to chip->unmask if NULL)
 * @irq_disable:        disable the interrupt
 * @irq_ack:            start of a new interrupt
 * @irq_mask:           mask an interrupt source
 * @irq_mask_ack:       ack and mask an interrupt source
 * @irq_unmask:         unmask an interrupt source
 * @irq_eoi:            end of interrupt
 * @irq_set_affinity:   Set the CPU affinity on SMP machines. If the force
 *                      argument is true, it tells the driver to
 *                      unconditionally apply the affinity setting. Sanity
 *                      checks against the supplied affinity mask are not
 *                      required. This is used for CPU hotplug where the
 *                      target CPU is not yet set in the cpu_online_mask.
 * @irq_retrigger:      resend an IRQ to the CPU
 * @irq_set_type:       set the flow type (IRQ_TYPE_LEVEL/etc.) of an IRQ
 * @irq_set_wake:       enable/disable power-management wake-on of an IRQ
 * @irq_bus_lock:       function to lock access to slow bus (i2c) chips
 * @irq_bus_sync_unlock:function to sync and unlock slow bus (i2c) chips
 * @irq_cpu_online:     configure an interrupt source for a secondary CPU
 * @irq_cpu_offline:    un-configure an interrupt source for a secondary CPU
 * @irq_suspend:        function called from core code on suspend once per
 *                      chip, when one or more interrupts are installed
 * @irq_resume:         function called from core code on resume once per chip,
 *                      when one ore more interrupts are installed
 * @irq_pm_shutdown:    function called from core code on shutdown once per chip
 * @irq_calc_mask:      Optional function to set irq_data.mask for special cases
 * @irq_print_chip:     optional to print special chip info in show_interrupts
 * @irq_request_resources:      optional to request resources before calling
 *                              any other callback related to this irq
 * @irq_release_resources:      optional to release resources acquired with
 *                              irq_request_resources
 * @irq_compose_msi_msg:        optional to compose message content for MSI
 * @irq_write_msi_msg:  optional to write message content for MSI
 * @irq_get_irqchip_state:      return the internal state of an interrupt
 * @irq_set_irqchip_state:      set the internal state of a interrupt
 * @irq_set_vcpu_affinity:      optional to target a vCPU in a virtual machine
 * @ipi_send_single:    send a single IPI to destination cpus
 * @ipi_send_mask:      send an IPI to destination cpus in cpumask
 * @irq_nmi_setup:      function called from core code before enabling an NMI
 * @irq_nmi_teardown:   function called from core code after disabling an NMI
 * @flags:              chip specific flags
 */
struct irq_chip {
        struct device   *parent_device;
        const char      *name;
        unsigned int    (*irq_startup)(struct irq_data *data);
        void            (*irq_shutdown)(struct irq_data *data);
        void            (*irq_enable)(struct irq_data *data);
        void            (*irq_disable)(struct irq_data *data);

        void            (*irq_ack)(struct irq_data *data);
        void            (*irq_mask)(struct irq_data *data);
        void            (*irq_mask_ack)(struct irq_data *data);
        void            (*irq_unmask)(struct irq_data *data);
        void            (*irq_eoi)(struct irq_data *data);

        int             (*irq_set_affinity)(struct irq_data *data, const struct cpumask *dest, bool
force);
        int             (*irq_retrigger)(struct irq_data *data);
        int             (*irq_set_type)(struct irq_data *data, unsigned int flow_type);
        int             (*irq_set_wake)(struct irq_data *data, unsigned int on);

        void            (*irq_bus_lock)(struct irq_data *data);
        void            (*irq_bus_sync_unlock)(struct irq_data *data);

        void            (*irq_cpu_online)(struct irq_data *data);
        void            (*irq_cpu_offline)(struct irq_data *data);

        void            (*irq_suspend)(struct irq_data *data);
        void            (*irq_resume)(struct irq_data *data);
        void            (*irq_pm_shutdown)(struct irq_data *data);

        void            (*irq_calc_mask)(struct irq_data *data);

        void            (*irq_print_chip)(struct irq_data *data, struct seq_file *p);
        int             (*irq_request_resources)(struct irq_data *data);
        void            (*irq_release_resources)(struct irq_data *data);

        void            (*irq_compose_msi_msg)(struct irq_data *data, struct msi_msg *msg);
        void            (*irq_write_msi_msg)(struct irq_data *data, struct msi_msg *msg);

        int             (*irq_get_irqchip_state)(struct irq_data *data, enum irqchip_irq_state whichh
, bool *state);
        int             (*irq_set_irqchip_state)(struct irq_data *data, enum irqchip_irq_state whichh
, bool state);

        int             (*irq_set_vcpu_affinity)(struct irq_data *data, void *vcpu_info);

        void            (*ipi_send_single)(struct irq_data *data, unsigned int cpu);
        void            (*ipi_send_mask)(struct irq_data *data, const struct cpumask *dest);

        int             (*irq_nmi_setup)(struct irq_data *data);
        void            (*irq_nmi_teardown)(struct irq_data *data);

        unsigned long   flags;
};

인터럽트 HW 칩에 대한 디스크립터로 대부분의 멤버들은 operation 후크들로 이루어졌다.

  • * name
    • /proc/interrupts에 출력할 이름
  • (*irq_startup)
    • 인터럽트 startup
    • core – irq_startup() 함수에서 호출하는데 이 후크가 지정되지 않으면 core – irq_enable() 함수를 호출한다.
  • (*irq_shutdown)
    • 인터럽트 shutdown
    • core – irq_shutdown() 함수에서 호출하는데 이 후크가 지정되지 않으면 core – irq_disable() 함수를 호출한다.
  • (*irq_enable)
    • 인터럽트 enable
    • core – irq_enable() 함수에서 호출하는데 이 후크가 지정되지 않으면 chip->irq_unmask() 후크를 호출한다.
  • (*irq_disable)
    • 인터럽트 disable
    • core – irq_disable() 함수에서 호출한다.
  • (*irq_ack)
    • 인터럽트 핸들러에서 새 인터럽트가 들어온 경우 호출한다. (새 인터럽트의 시작)
    • ack가 호출되면 eoi가 처리되기 전에 cpu에 새 인터럽트가 진입하지 못한다.
  • (*irq_mask)
    • 인터럽트 소스 마스크
    • c0re – mask_irq() 함수에서 호출한다.
  • (*irq_mask_ack)
    • 인터럽트 핸들러에서 새 인터럽트가 들어온 경우 호출하며 irq 마스크도 같이 수행한다.
    • core – mask_ack_irq() 함수에서 호출하는데 이 후크가 지정되지 않으면 chip->irq_mask() 후크와 chip->irq_ack() 후크를 따로 하나씩 호출한다.
  • (*irq_unmask)
    • 인터럽트 소스 언마스크
    • c0re – unmask_irq() 함수에서 호출한다.
  • (*irq_eoi)
    • 인터럽트 핸들러 처리의 종료 시 호출한다.
  • (*irq_set_affinity)
    • SMP 머신에서 irq 소스를 허용할 cpu affinity를 설정한다.
    • core – irq_do_set_affinity() 함수에서 호출한다.
  • (*irq_retrigger)
    • cpu로 irq를 재 전송
  • (*irq_set_type)
    • irq 라인 트리거 타입을 설정한다.
  • (*irq_set_wake)
    • irq의 pm wake-on의 enable/disable 여부 설정
    • core – irq_set_irq_wake() -> set_irq_kake_real() 함수에서 호출한다.
  • (*irq_bus_lock)
    • slow bus chip에 대한 lock으로 인터럽트를 구성하고 해제할 때 lock을 사용 한다.
      • 사용 함수
        • setup_irq()
        • free_irq()
        • request_threaded_irq()
        • setup_percpu_irq()
        • free_percpu_irq()
        • request_percpu_irq()
        • irq_finalize_oneshot()
  • (*irq_bus_sync_unlock)
    • slow bus chip에 대한 sync와 unlock
  • (*irq_cpu_online)
    • cpu가 online될 때 core – irq_cpu_online() 함수에서 호출한다.
  • (*irq_cpu_offline)
    • cpu가 offline될 때 core – irq_cpu_offline() 함수에서 호출한다.
  • (*irq_suspend)
    • PM(Power Management) 기능을 사용하는 경우 core가 suspend 되는 경우 호출되어 irq chip도같이 suspend 동작을 알려 동작을 멈추게 한다.
  • (*irq_resume)
    • PM(Power Management) 기능을 사용하는 경우 suspend된 irq chip이 깨어나 다시 동작하게 한다.
  • (*irq_pm_shutdown)
    • irq에 대한 전원 소비를 없애기 위해 shutdown 한다.
  • (*irq_calc_mask)
    • irq_data.mask의 설정을 사용한 특별한 옵션 기능
  • (*irq_print_chip)
    • show interruptes 시 추가 옵션 칩 정보를 출력
  • (*irq_request_resources)
    • irq와 관련된 모든 콜백 함수를 호출하기 전에 리소스를 요청하기 위한 옵션
  • (*irq_release_resources)
    • irq_request_resources에서 얻은 리소스를 해제
  • (*irq_compose_msi_msg)
    • MSI를 위한 메시지 구성
  • (*irq_write_msi_msg)
    • MSI를 위한 메시지 기록 옵션
  • (*irq_get_irqchip_state)
    • irqchip 내부 상태를 가져온다.
  • (*irq_set_irqchip_state)
    • irqchip 내부 상태를 저장한다.
  • (*irq_set_vcpu_affinity)
    • 가상 cpu에 대한 affinity를 지정한다.
  • (*ipi_send_single)
    • 하나의 cpu에 IPI를 전달한다.
  • (*ipi_send_mask)
    • mask 지정된 cpu에 IPI를 전달한다.
  • (*irq_nmi_setup)
    • Pesudo-NMI를 설정한다.
  • (*irq_nmi_teardown)
    • Pesudo-NMI를 해제한다.
  • flags
    • chip 관련 플래그들

 

참고

 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다