Linux kernel --hwirq 、virq之间的映射关系

待上传

// SPDX-License-Identifier: GPL-2.0+ /* * APM X-Gene MSI Driver * * Copyright (c) 2014, Applied Micro Circuits Corporation * Author: Tanmay Inamdar <tinamdar@apm.com> * Duc Dang <dhdang@apm.com> */ #include <linux/cpu.h> #include <linux/interrupt.h> #include <linux/module.h> #include <linux/msi.h> #include <linux/of_irq.h> #include <linux/irqchip/chained_irq.h> #include <linux/pci.h> #include <linux/platform_device.h> #include <linux/of_pci.h> #define MSI_IR0 0x000000 #define MSI_INT0 0x800000 #define IDX_PER_GROUP 8 #define IRQS_PER_IDX 16 #define NR_HW_IRQS 16 #define NR_MSI_VEC (IDX_PER_GROUP * IRQS_PER_IDX * NR_HW_IRQS) struct xgene_msi_group { struct xgene_msi *msi; int gic_irq; u32 msi_grp; }; struct xgene_msi { struct device_node *node; struct irq_domain *inner_domain; struct irq_domain *msi_domain; u64 msi_addr; void __iomem *msi_regs; unsigned long *bitmap; struct mutex bitmap_lock; struct xgene_msi_group *msi_groups; int num_cpus; }; /* Global data */ static struct xgene_msi xgene_msi_ctrl; static struct irq_chip xgene_msi_top_irq_chip = { .name = "X-Gene1 MSI", .irq_enable = pci_msi_unmask_irq, .irq_disable = pci_msi_mask_irq, .irq_mask = pci_msi_mask_irq, .irq_unmask = pci_msi_unmask_irq, }; static struct msi_domain_info xgene_msi_domain_info = { .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_PCI_MSIX), .chip = &xgene_msi_top_irq_chip, }; /* * X-Gene v1 has 16 groups of MSI termination registers MSInIRx, where * n is group number (0..F), x is index of registers in each group (0..7) * The register layout is as follows: * MSI0IR0 base_addr * MSI0IR1 base_addr + 0x10000 * ... ... * MSI0IR6 base_addr + 0x60000 * MSI0IR7 base_addr + 0x70000 * MSI1IR0 base_addr + 0x80000 * MSI1IR1 base_addr + 0x90000 * ... ... * MSI1IR7 base_addr + 0xF0000 * MSI2IR0 base_addr + 0x100000 * ... ... * MSIFIR0 base_addr + 0x780000 * MSIFIR1 base_addr + 0x790000 * ... ... * MSIFIR7 base_addr + 0x7F0000 * MSIINT0 base_addr + 0x800000 * MSIINT1 base_addr + 0x810000 * ... ... * MSIINTF base_addr + 0x8F0000 * * Each index register supports 16 MSI vectors (0..15) to generate interrupt. * There are total 16 GIC IRQs assigned for these 16 groups of MSI termination * registers. * * Each MSI termination group has 1 MSIINTn register (n is 0..15) to indicate * the MSI pending status caused by 1 of its 8 index registers. */ /* MSInIRx read helper */ static u32 xgene_msi_ir_read(struct xgene_msi *msi, u32 msi_grp, u32 msir_idx) { return readl_relaxed(msi->msi_regs + MSI_IR0 + (msi_grp << 19) + (msir_idx << 16)); } /* MSIINTn read helper */ static u32 xgene_msi_int_read(struct xgene_msi *msi, u32 msi_grp) { return readl_relaxed(msi->msi_regs + MSI_INT0 + (msi_grp << 16)); } /* * With 2048 MSI vectors supported, the MSI message can be constructed using * following scheme: * - Divide into 8 256-vector groups * Group 0: 0-255 * Group 1: 256-511 * Group 2: 512-767 * ... * Group 7: 1792-2047 * - Each 256-vector group is divided into 16 16-vector groups * As an example: 16 16-vector groups for 256-vector group 0-255 is * Group 0: 0-15 * Group 1: 16-32 * ... * Group 15: 240-255 * - The termination address of MSI vector in 256-vector group n and 16-vector * group x is the address of MSIxIRn * - The data for MSI vector in 16-vector group x is x */ static u32 hwirq_to_reg_set(unsigned long hwirq) { return (hwirq / (NR_HW_IRQS * IRQS_PER_IDX)); } static u32 hwirq_to_group(unsigned long hwirq) { return (hwirq % NR_HW_IRQS); } static u32 hwirq_to_msi_data(unsigned long hwirq) { return ((hwirq / NR_HW_IRQS) % IRQS_PER_IDX); } static void xgene_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) { struct xgene_msi *msi = irq_data_get_irq_chip_data(data); u32 reg_set = hwirq_to_reg_set(data->hwirq); u32 group = hwirq_to_group(data->hwirq); u64 target_addr = msi->msi_addr + (((8 * group) + reg_set) << 16); msg->address_hi = upper_32_bits(target_addr); msg->address_lo = lower_32_bits(target_addr); msg->data = hwirq_to_msi_data(data->hwirq); } /* * X-Gene v1 only has 16 MSI GIC IRQs for 2048 MSI vectors. To maintain * the expected behaviour of .set_affinity for each MSI interrupt, the 16 * MSI GIC IRQs are statically allocated to 8 X-Gene v1 cores (2 GIC IRQs * for each core). The MSI vector is moved fom 1 MSI GIC IRQ to another * MSI GIC IRQ to steer its MSI interrupt to correct X-Gene v1 core. As a * consequence, the total MSI vectors that X-Gene v1 supports will be * reduced to 256 (2048/8) vectors. */ static int hwirq_to_cpu(unsigned long hwirq) { return (hwirq % xgene_msi_ctrl.num_cpus); } static unsigned long hwirq_to_canonical_hwirq(unsigned long hwirq) { return (hwirq - hwirq_to_cpu(hwirq)); } static int xgene_msi_set_affinity(struct irq_data *irqdata, const struct cpumask *mask, bool force) { int target_cpu = cpumask_first(mask); int curr_cpu; curr_cpu = hwirq_to_cpu(irqdata->hwirq); if (curr_cpu == target_cpu) return IRQ_SET_MASK_OK_DONE; /* Update MSI number to target the new CPU */ irqdata->hwirq = hwirq_to_canonical_hwirq(irqdata->hwirq) + target_cpu; return IRQ_SET_MASK_OK; } static struct irq_chip xgene_msi_bottom_irq_chip = { .name = "MSI", .irq_set_affinity = xgene_msi_set_affinity, .irq_compose_msi_msg = xgene_compose_msi_msg, }; static int xgene_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, unsigned int nr_irqs, void *args) { struct xgene_msi *msi = domain->host_data; int msi_irq; mutex_lock(&msi->bitmap_lock); msi_irq = bitmap_find_next_zero_area(msi->bitmap, NR_MSI_VEC, 0, msi->num_cpus, 0); if (msi_irq < NR_MSI_VEC) bitmap_set(msi->bitmap, msi_irq, msi->num_cpus); else msi_irq = -ENOSPC; mutex_unlock(&msi->bitmap_lock); if (msi_irq < 0) return msi_irq; irq_domain_set_info(domain, virq, msi_irq, &xgene_msi_bottom_irq_chip, domain->host_data, handle_simple_irq, NULL, NULL); return 0; } static void xgene_irq_domain_free(struct irq_domain *domain, unsigned int virq, unsigned int nr_irqs) { struct irq_data *d = irq_domain_get_irq_data(domain, virq); struct xgene_msi *msi = irq_data_get_irq_chip_data(d); u32 hwirq; mutex_lock(&msi->bitmap_lock); hwirq = hwirq_to_canonical_hwirq(d->hwirq); bitmap_clear(msi->bitmap, hwirq, msi->num_cpus); mutex_unlock(&msi->bitmap_lock); irq_domain_free_irqs_parent(domain, virq, nr_irqs); } static const struct irq_domain_ops msi_domain_ops = { .alloc = xgene_irq_domain_alloc, .free = xgene_irq_domain_free, }; static int xgene_allocate_domains(struct xgene_msi *msi) { msi->inner_domain = irq_domain_add_linear(NULL, NR_MSI_VEC, &msi_domain_ops, msi); if (!msi->inner_domain) return -ENOMEM; msi->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(msi->node), &xgene_msi_domain_info, msi->inner_domain); if (!msi->msi_domain) { irq_domain_remove(msi->inner_domain); return -ENOMEM; } return 0; } static void xgene_free_domains(struct xgene_msi *msi) { if (msi->msi_domain) irq_domain_remove(msi->msi_domain); if (msi->inner_domain) irq_domain_remove(msi->inner_domain); } static int xgene_msi_init_allocator(struct xgene_msi *xgene_msi) { int size = BITS_TO_LONGS(NR_MSI_VEC) * sizeof(long); xgene_msi->bitmap = kzalloc(size, GFP_KERNEL); if (!xgene_msi->bitmap) return -ENOMEM; mutex_init(&xgene_msi->bitmap_lock); xgene_msi->msi_groups = kcalloc(NR_HW_IRQS, sizeof(struct xgene_msi_group), GFP_KERNEL); if (!xgene_msi->msi_groups) return -ENOMEM; return 0; } static void xgene_msi_isr(struct irq_desc *desc) { struct irq_chip *chip = irq_desc_get_chip(desc); struct xgene_msi_group *msi_groups; struct xgene_msi *xgene_msi; unsigned int virq; int msir_index, msir_val, hw_irq; u32 intr_index, grp_select, msi_grp; chained_irq_enter(chip, desc); msi_groups = irq_desc_get_handler_data(desc); xgene_msi = msi_groups->msi; msi_grp = msi_groups->msi_grp; /* * MSIINTn (n is 0..F) indicates if there is a pending MSI interrupt * If bit x of this register is set (x is 0..7), one or more interupts * corresponding to MSInIRx is set. */ grp_select = xgene_msi_int_read(xgene_msi, msi_grp); while (grp_select) { msir_index = ffs(grp_select) - 1; /* * Calculate MSInIRx address to read to check for interrupts * (refer to termination address and data assignment * described in xgene_compose_msi_msg() ) */ msir_val = xgene_msi_ir_read(xgene_msi, msi_grp, msir_index); while (msir_val) { intr_index = ffs(msir_val) - 1; /* * Calculate MSI vector number (refer to the termination * address and data assignment described in * xgene_compose_msi_msg function) */ hw_irq = (((msir_index * IRQS_PER_IDX) + intr_index) * NR_HW_IRQS) + msi_grp; /* * As we have multiple hw_irq that maps to single MSI, * always look up the virq using the hw_irq as seen from * CPU0 */ hw_irq = hwirq_to_canonical_hwirq(hw_irq); virq = irq_find_mapping(xgene_msi->inner_domain, hw_irq); WARN_ON(!virq); if (virq != 0) generic_handle_irq(virq); msir_val &= ~(1 << intr_index); } grp_select &= ~(1 << msir_index); if (!grp_select) { /* * We handled all interrupts happened in this group, * resample this group MSI_INTx register in case * something else has been made pending in the meantime */ grp_select = xgene_msi_int_read(xgene_msi, msi_grp); } } chained_irq_exit(chip, desc); } static enum cpuhp_state pci_xgene_online; static int xgene_msi_remove(struct platform_device *pdev) { struct xgene_msi *msi = platform_get_drvdata(pdev); if (pci_xgene_online) cpuhp_remove_state(pci_xgene_online); cpuhp_remove_state(CPUHP_PCI_XGENE_DEAD); kfree(msi->msi_groups); kfree(msi->bitmap); msi->bitmap = NULL; xgene_free_domains(msi); return 0; } static int xgene_msi_hwirq_alloc(unsigned int cpu) { struct xgene_msi *msi = &xgene_msi_ctrl; struct xgene_msi_group *msi_group; cpumask_var_t mask; int i; int err; for (i = cpu; i < NR_HW_IRQS; i += msi->num_cpus) { msi_group = &msi->msi_groups[i]; if (!msi_group->gic_irq) continue; irq_set_chained_handler_and_data(msi_group->gic_irq, xgene_msi_isr, msi_group); /* * Statically allocate MSI GIC IRQs to each CPU core. * With 8-core X-Gene v1, 2 MSI GIC IRQs are allocated * to each core. */ if (alloc_cpumask_var(&mask, GFP_KERNEL)) { cpumask_clear(mask); cpumask_set_cpu(cpu, mask); err = irq_set_affinity(msi_group->gic_irq, mask); if (err) pr_err("failed to set affinity for GIC IRQ"); free_cpumask_var(mask); } else { pr_err("failed to alloc CPU mask for affinity\n"); err = -EINVAL; } if (err) { irq_set_chained_handler_and_data(msi_group->gic_irq, NULL, NULL); return err; } } return 0; } static int xgene_msi_hwirq_free(unsigned int cpu) { struct xgene_msi *msi = &xgene_msi_ctrl; struct xgene_msi_group *msi_group; int i; for (i = cpu; i < NR_HW_IRQS; i += msi->num_cpus) { msi_group = &msi->msi_groups[i]; if (!msi_group->gic_irq) continue; irq_set_chained_handler_and_data(msi_group->gic_irq, NULL, NULL); } return 0; } static const struct of_device_id xgene_msi_match_table[] = { {.compatible = "apm,xgene1-msi"}, {}, }; static int xgene_msi_probe(struct platform_device *pdev) { struct resource *res; int rc, irq_index; struct xgene_msi *xgene_msi; int virt_msir; u32 msi_val, msi_idx; xgene_msi = &xgene_msi_ctrl; platform_set_drvdata(pdev, xgene_msi); res = platform_get_resource(pdev, IORESOURCE_MEM, 0); xgene_msi->msi_regs = devm_ioremap_resource(&pdev->dev, res); if (IS_ERR(xgene_msi->msi_regs)) { dev_err(&pdev->dev, "no reg space\n"); rc = PTR_ERR(xgene_msi->msi_regs); goto error; } xgene_msi->msi_addr = res->start; xgene_msi->node = pdev->dev.of_node; xgene_msi->num_cpus = num_possible_cpus(); rc = xgene_msi_init_allocator(xgene_msi); if (rc) { dev_err(&pdev->dev, "Error allocating MSI bitmap\n"); goto error; } rc = xgene_allocate_domains(xgene_msi); if (rc) { dev_err(&pdev->dev, "Failed to allocate MSI domain\n"); goto error; } for (irq_index = 0; irq_index < NR_HW_IRQS; irq_index++) { virt_msir = platform_get_irq(pdev, irq_index); if (virt_msir < 0) { rc = virt_msir; goto error; } xgene_msi->msi_groups[irq_index].gic_irq = virt_msir; xgene_msi->msi_groups[irq_index].msi_grp = irq_index; xgene_msi->msi_groups[irq_index].msi = xgene_msi; } /* * MSInIRx registers are read-to-clear; before registering * interrupt handlers, read all of them to clear spurious * interrupts that may occur before the driver is probed. */ for (irq_index = 0; irq_index < NR_HW_IRQS; irq_index++) { for (msi_idx = 0; msi_idx < IDX_PER_GROUP; msi_idx++) xgene_msi_ir_read(xgene_msi, irq_index, msi_idx); /* Read MSIINTn to confirm */ msi_val = xgene_msi_int_read(xgene_msi, irq_index); if (msi_val) { dev_err(&pdev->dev, "Failed to clear spurious IRQ\n"); rc = -EINVAL; goto error; } } rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "pci/xgene:online", xgene_msi_hwirq_alloc, NULL); if (rc < 0) goto err_cpuhp; pci_xgene_online = rc; rc = cpuhp_setup_state(CPUHP_PCI_XGENE_DEAD, "pci/xgene:dead", NULL, xgene_msi_hwirq_free); if (rc) goto err_cpuhp; dev_info(&pdev->dev, "APM X-Gene PCIe MSI driver loaded\n"); return 0; err_cpuhp: dev_err(&pdev->dev, "failed to add CPU MSI notifier\n"); error: xgene_msi_remove(pdev); return rc; } static struct platform_driver xgene_msi_driver = { .driver = { .name = "xgene-msi", .of_match_table = xgene_msi_match_table, }, .probe = xgene_msi_probe, .remove = xgene_msi_remove, }; static int __init xgene_pcie_msi_init(void) { return platform_driver_register(&xgene_msi_driver); } subsys_initcall(xgene_pcie_msi_init); 请分析代码,看看是如何配置中断亲和性的
最新发布
09-25
<think>好的,我现在需要帮助用户解决如何在安卓或Linux系统中查看hwirq中断向量表的问题。首先,我要理清用户的需求。用户之前询问了中断向量表的查看方法,现在进一步想了解hwirq相关的具体方法和工具。根据提供的引用内容,用户可能已经接触到一些内核机制,比如irq_domain、tasklet、工作队列,以及init进程的启动阶段。需要结合这些信息来回答。 首先,用户提到的hwirq(Hardware Interrupt Number)是硬件中断号,与Linux内核中的中断处理机制相关。在Linux中,中断处理涉及到irq_domain结构,用于映射硬件中断号到虚拟中断号(virq)。引用[1]提到irq_domain_set_info设置irq_desc的handle_irq指针,这说明用户可能对中断处理流程有一定了解,需要更深入的工具或方法来查看hwirq对应的向量表。 接下来,我需要回忆Linux内核中关于中断处理的相关知识。hwirqvirq的映射通常由irq_domain管理,每个硬件设备的中断控制器可能有自己的irq_domain。用户需要查看这些信息,可能需要通过内核提供的调试接口或工具。 根据之前的回答,用户已经知道如何查看/proc/interrupts和dmesg日志,但hwirq的具体信息可能需要更底层的工具。例如,sysfs中的irq目录可能包含相关信息。此外,可能需要使用调试工具如crash或直接分析内核符号。 另外,引用[2]提到了内核编译的过程,可能用户需要重新编译内核或模块来启用某些调试选项,比如CONFIG_GENERIC_IRQ_DEBUGFS,以便访问更详细的中断信息。同时,引用[3]中的tasklet和工作队列可能与中断下半部处理有关,但用户的问题更关注hwirq的映射,这部分属于上半部。 用户的需求可能涉及以下步骤:1. 确认系统是否支持相关调试功能;2. 使用sysfs或debugfs查看irq_domain的信息;3. 分析内核符号或使用调试工具;4. 可能需要root权限或重新编译内核。 需要注意权限问题,很多操作需要root权限,用户可能需要在已root的设备或使用工程机进行操作。此外,不同内核版本或安卓版本可能存在差异,需要指出可能的变数。 最后,确保回答结构清晰,分步骤说明方法,并推荐相关工具和命令,同时提醒注意事项。需要引用之前的引用内容,如引用[1]中的irq_domain_set_info,来说明hwirqvirq映射关系。</think>### 安卓/Linux系统中查看hwirq中断向量表的方法与工具 在Linux内核中,硬件中断号(hwirq)与虚拟中断号(virq)通过`irq_domain`机制进行映射,该机制由中断控制器(如GIC)驱动实现[^1]。以下是查看hwirq中断向量表的具体方法和工具: --- #### 1. **通过sysfs查看中断映射关系** - **路径**:`/sys/kernel/irq/<virq>/` 每个虚拟中断号(virq)目录下包含硬件中断信息: ```bash adb shell su -c "ls /sys/kernel/irq/*/hwirq" ``` 可直接查看`hwirq`字段,例如: ``` /sys/kernel/irq/33/hwirq: 123 /sys/kernel/irq/48/hwirq: 44 ``` 表示virq 33对应hwirq 123,virq 48对应hwirq 44[^1]。 --- #### 2. **使用debugfs获取详细中断信息** - **启用debugfs**:需内核编译时开启`CONFIG_GENERIC_IRQ_DEBUGFS`选项[^2]。 - **查看irq_domain映射**: ```bash adb shell su -c "cat /sys/kernel/debug/irq/domains" ``` 输出示例: ``` Domain: gic-400 Mapped: 256 HWIRQ 0-255 --> IRQ 16-271 ``` 显示中断控制器(如GIC)的hwirqvirq的映射范围。 --- #### 3. **分析内核符号与源码** - **查看irq_domain结构**: 通过内核符号表(`System.map`)或`objdump`工具查找`irq_domain`相关函数: ```bash arm-linux-gnueabi-objdump -D vmlinux | grep "irq_domain_add" ``` 可定位`irq_domain`的初始化代码,观察hwirq映射逻辑。 - **源码分析**: 在Linux内核源码中,关键函数如`irq_create_mapping()`负责建立hwirqvirq映射关系: ```c // kernel/irq/irqdomain.c unsigned int irq_create_mapping(struct irq_domain *domain, irq_hw_number_t hwirq) { // ... } ``` 结合具体中断控制器驱动(如`drivers/irqchip/irq-gic.c`)分析实现细节[^1]。 --- #### 4. **动态调试工具** - **crash工具**: 在内核崩溃转储文件中,通过`irq -d`命令查看所有irq_desc信息: ``` crash> irq -d IRQ DESC CHIP NAME HWIRQ 33 ffffff801a23 GIC-400 timer 123 48 ffffff801b45 GIC-400 uart 44 ``` 直接显示hwirqvirq的对应关系。 - **ftrace跟踪中断处理**: 启用ftrace记录中断事件: ```bash adb shell su -c "echo 1 > /sys/kernel/debug/tracing/events/irq/enable" adb shell su -c "cat /sys/kernel/debug/tracing/trace_pipe" ``` 可捕获hwirq触发的中断处理流程。 --- #### 5. **专用硬件工具** - **JTAG调试器**: 直接读取中断控制器的寄存器值,例如ARM GIC的`GICD_IROUTER`寄存器存储hwirq的CPU亲和性。 - **ChipScope/SignalTap**: 通过FPGA逻辑分析仪捕获硬件中断信号与hwirq的实时行为。 --- #### 注意事项 1. **权限要求**:需root权限或内核调试符号支持。 2. **内核版本差异**:不同内核版本中`irq_domain`接口可能变化(如Linux 4.19后引入分层irqdomain)。 3. **安全性**:直接操作中断映射可能导致系统不稳定。 --- ### 相关问题 1. 如何通过`irq_domain`机制实现多级中断控制器的级联管理? 2. 在ARM GICv3架构中,hwirq的分配策略有何变化? 3. 为什么现代Linux内核不再允许用户态直接访问hwirq映射表[^2]?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值