中断翻译服务(ITS):ARM GIC 中的 LPI 路由与消息信号中断管理
文章目录
中断翻译服务 是 ARM 通用中断控制器ARM 通用中断控制器 的一个组件(在 GICv3 中可选,在 GICv4 中必需),用于路由特定局部性外设中断(Locality-specific Peripheral Interrupts),例如来自 PCI Express 设备的消息信号中断。ITS 管理一系列用于设备、事件、中断集合及其他实体的映射表,以便重新分发消息信号中断,并有助于隔离来自不同虚拟机的中断。
如果你的 ARM GIC 带有 ITS,则必须使用它来管理和发送 LPI——再分发器(Redistributor)中的 LPI 寄存器接口将不会实现。
检测 ITS
Devicetree
当从 Devicetree 启动系统时,ITS 会作为描述 GIC 的节点的子节点列出:
gic: interrupt-controller@0e001000 {
compatible = "arm,gic-700", "arm,gic-v3";
#address-cells = <2>;
#interrupt-cells = <3>;
#size-cells = <2>;
single-redist;
ranges;
interrupt-controller;
# ...
its_pcie: its@0e050000 {
compatible = "arm,gic-v3-its";
msi-controller;
reg = <0x0 0x0e050000 0x0 0x30000>;
};
};
ITS 节点的 regreg 参数会告诉你其控制寄存器的起始位置。
请注意,单个系统可能有多个 ITS 设备,每个设备服务于不同来源的中断。
ACPI
如果你从 ACPI 引导,ITS 将作为 MADT 中的条目之一列出,具体为条目类型 0xF。其布局如下:
struct its_entry { uint8_t type; uint8_t length; uint16_t reserved_0; uint32_t gic_its_id; // if there are multiple ITSes, all of these values must be unique struct its_control *its_control; // 64-bit address uint32_t reserved_1; }
再次注意,可能存在多个 ITS 单元。
启动 ITS
在设置并启用 GIC 本体,并使用适当的 LPI 表初始化 GICR_PROPBASER 和 GICR_PENDBASER 寄存器之后,就可以初始化 ITS。ITS 暴露一系列控制寄存器,起始于 Devicetree 或 MADT 中给出的基地址:
| 名称 | 偏移 | 宽度 | 类型 | 描述 |
|---|---|---|---|---|
| GITS_CTLR | 0x0000 | 4 | RW | 控制寄存器(激活并读取 ITS 状态) |
| GITS_IIDR | 0x0004 | 4 | RO | 标识寄存器 |
| GITS_TYPER | 0x0008 | 8 | RO | 类型寄存器(读取 ITS 特性) |
| GITS_MPAMIDR | 0x0010 | 4 | RO | 支持的 MPAM 大小 |
| GITS_PARTIDR | 0x0014 | 4 | RW | PARTID 寄存器 |
| GITS_MPIDR | 0x0018 | 4 | RO | ITS 亲和性(它可以向其发送事件的 PE 集合) |
| GITS_STATUSR | 0x0040 | 4 | RO | (可选)错误报告状态寄存器 |
| GITS_UMSIR | 0x0048 | 8 | RO | (可选)未映射 MSI 寄存器 |
| GITS_CBASER | 0x0080 | 8 | RW | 命令队列的基地址 |
| GITS_CWRITER | 0x0088 | 8 | RW | 命令队列的写入位置 |
| GITS_CREADR | 0x0090 | 8 | RO | 命令队列的读取位置 |
| GITS_BASER<n> | 0x0100 + 0x0008 * n | 8 | RW | 翻译表描述符 |
先禁用 ITS
首先应确保 ITS 已禁用且未运行任何命令, 方法是先清除 GITS_CTLR 的使能位(位 0),然后等待静默位 (位 31)被置位:
its_control->ctlr = its_control->ctlr & 0xfffffffe; while (!(its_control->ctlr & 0x80000000)) ;
分配 ITS 表
然后,为 GITS_BASER 寄存器分配表。每个 GITS_BASER 寄存器描述一个 ITS 用于内部记账的表——在执行期间你不会访问这些表,但你需要 为其保留内存并将它们清零。GITS_BASER 寄存器的布局如下:
| 名称 | 位偏移 | 类型 | 描述 |
|---|---|---|---|
| Valid | 63 | RW | 初始为清零状态,在为表分配内存时置位。 |
| Indirect | 62 | RW | 如果清零,描述一个平坦表。如果置位,描述一个两级表。 |
| InnerCache | 59-61 | RW | 设置对表的内存访问的内部可缓存性(例如 0b001 表示 Normal Inner Non-cacheable) |
| Type | 56-58 | RO | 描述该表所涉及的实体类型(1 表示设备,2 表示 vPE,4 表示中断集合)。特别是,如果该字段为 0,则该寄存器不描述翻译表,可以跳过。 |
| OuterCache | 53-55 | RW | 设置对表的内存访问的外部可缓存性。0 表示应使用 InnerCache 中的值。 |
| EntrySize | 48-52 | RO | 每个表项的字节数减一(因此值 0xf 表示 0x10 字节)。 |
| PhysicalAddress | 12-47 | RW | 指定表的页对齐物理地址。 |
| Shareability | 10-11 | RW | 指定表的共享属性(Non-shareable、Inner Shareable、Outer Shareable) |
| PageSize | 8-9 | RO | 一个表示 Size 中描述的页面大小的代码(0 表示 4KiB,1 表示 16KiB,2 表示 64KiB) |
| Size | 0-7 | RW | 为该表分配的页面数减一。Page_Size 指定每个页面的大小。 |
for (int n = 0; n < 8; n++) { uint64_t baser_value = its_control->baser[n]; int table_type = (baser_value & 0x0700000000000000) >> 0x38 if (table_type != 0) { size_t entry_size = ((baser_value & 0x1f000000000000) >> 0x30) + 1; size_t entry_count = get_entry_count (table_type); size_t needed_size = entry_size * entry_count; int page_size_code = baser_value & 0x300; size_t page_size = 1000 << (page_size_code >> 7); size_t allocation_size = (needed_size + page_size - 1) & ~(page_size - 1) size_t size_in_pages = allocation_size / page_size; void *table = alloc_aligned_to_0x10000_uncacheable(allocation_size); // whatever your kernel allocation function is its_control->baser[n] = baser_value & 0xffff000000000000) | 0x8800000000000000 | (uint64_t) table | (size_in_pages - 1); } }
每个表所需的条目数取决于它存储的条目类型:
- Device 表存储由请求设备的 DeviceID 索引的条目。对于 PCIe,这是设备的 requester ID(总线、设备和功能号的打包元组),这意味着如果此 ITS 的用途是服务 PCIe 总线,你可能需要为该表实现 0x10000 个条目。
- vPE 表存储虚拟中断的处理器标识(仅限 GICv4)。如果你的操作系统不使用这些,请分配 1。
- Collection 表存储中断集合的映射。集合是一种虚拟映射,主要用于将事件链接到特定的 Redistributor。如果你在中断路由方面没有做任何特别复杂的事情,该表应为你希望服务硬件中断的每个处理器设置一个条目。
分配命令队列
你最后要分配的内存是命令队列,你将用它向 ITS 发送命令。 此代码分配一个页面大小的队列:
// set both indices to zero its_control->cwriter = 0; its_control->creadr = 0; // allocate a 1-page buffer and set the base register to that its_control->cbaser = ((uint64_t) alloc_aligned_to_0x10000_uncacheable(0x1000)) | 0x8800000000000000;
启用 ITS
分配完所有必需的内存后,启用 ITS 只需设置 Enable 位即可:
its_control->ctlr = its_control->ctlr | 1;
ITS 命令
启用 ITS 后,与它的交互主要由发送命令组成。 命令长度为 32 字节,其内容主要排列为四个四字:
struct its_command = { uint64_t qword0; uint64_t qword1; uint64_t qword2; uint64_t qword3; };
要发送命令,请将其写入 cwriter & 0xfffe0 + cbaser & 0xfffffff000 指向的位置,
然后将 cwriter 增加 32,必要时回绕到 0。ITS 将立即开始处理
命令,并相应更新 creadr 的值。如果命令失败
导致队列停滞,creadr 的低位将被置位。
ITS 提供了相当多的命令可供使用,这些命令列在 GICv3/4 规范中。 以下是简化中断设置所需的命令列表。
MAPC
MAPC 创建一个中断集合并将其映射到 Redistributor。它也可用于 移除先前创建的映射。
GITS_TYPER 的第 0x10 位(PTA)将说明应如何指定 Redistributor:
- 如果该位被置位,则它应为 Redistributor 的物理地址。
- 否则,它应为 Redistributor 的处理器编号。
int valid; // whether you are creating or removing a mapping int icid; // whatever collection ID you want to assign int pta = its_control->typer & 0x80000; uint64_t rdbase_field = (pta ? (uint64_t) gicr : (gicr->typer & 0xffff00) << 8) its_command->qword0 = 9; its_command->qword1 = 0; its_command->qword2 = (valid ? 0x80000000000000000 : 0) | rdbase_field | icid; its_command->qword3 = 0;
MAPD
MAPD 为给定的 DeviceID 创建一个 Device 条目,并将其映射到你为其创建的中断转换表。 它也可用于移除现有的映射。
中断转换表是另一个内部记账表,为每个你将从中服务中断的设备分配。 该表中条目的大小由 GITS_TYPER 中的 ITT_Entry_Size 字段给出。
int valid; // whether you are creating or removing a mapping int device_id; // the device ID you are mapping int size_log_2; // the log base 2 of the max number of the event ID you want to support size_t itt_entry_size = (its_control->typer & 0xf0) >> 4 + 1; void *itt = alloc_aligned_to_0x1000_uncacheable(itt_entry_size << size_log_2); its_command->qword0 = 9 | device_id << 0x20; its_command->qword1 = size_log_2 - 1; its_command->qword2 = (valid ? 0x80000000000000000 : 0) | (uint64_t) itt; its_command->qword3 = 0;
MAPTI
MAPTI 将给定的 DeviceID/EventID 元组映射到给定 ICID 上的给定 LPI。
int device_id; // the device ID you are mapping int event_id; // the event ID you are mapping int lpi; // the LPI ID you want to map to int icid; // the ICID you want to service this interrupt its_command->qword0 = 0xa | device_id << 0x20; its_command->qword1 = event_id | lpi << 0x20; its_command->qword2 = icid; its_command->qword3 = 0;
DISCARD
DISCARD 取消映射现有的 DeviceID/EventID 元组。
int device_id; // the device ID you are mapping int event_id; // the event ID you are mapping its_command->qword0 = 0xf | device_id << 0x20; its_command->qword1 = event_id; its_command->qword2 = 0; its_command->qword3 = 0;
INV
INV 确保 Redistributor 中针对给定 DeviceID/EventID 元组的任何缓存 与内存中保存的 LPI 配置表匹配(这些配置表可由其他命令更改)。
int device_id; // the device ID you are mapping int event_id; // the event ID you are mapping uint64_t rdbase_field = same_way_you_calculated_rdbase_field_earlier(); its_command->qword0 = 0xc | device_id << 0x20; its_command->qword1 = event_id; its_command->qword2 = rdbase_field; its_command->qword3 = 0;
请注意,某些命令会自动执行此失效操作,你无需运行此 命令。特别是,DISCARD 会执行此失效操作,但 MAP 命令不会。
SYNC
确保与给定 Redistributor 关联的所有未完成的 ITS 操作在执行任何进一步的 ITS 命令之前被全局 观察到。
uint64_t rdbase_field = same_way_you_calculated_rdbase_field_earlier(); its_command->qword0 = 0x5; its_command->qword1 = 0; its_command->qword2 = rdbase_field; its_command->qword3 = 0;
接收消息信号中断
使用命令队列在 ITS 上设置好中断映射后,你现在已准备好从连接的设备发送 消息信号中断。
消息信号中断的目标地址是 GITS_TRANSLATER 寄存器, 这是一个位于控制寄存器基址偏移 0x10040 处的 4 字节只写寄存器 (严格来说,位于所谓的“ITS 转换寄存器映射”中的偏移 0x40 处)。 你将需要指示你的 MSI 设备将你设置的 EventID 写入此地址:
struct its_translation *its_translation = (struct its_translation *)((uint64_t)its_control + 0x10000); struct msix_table_entry *entry; // an MSI-X table entry in the device's PCIe config space msix_table_entry->data = event_id; msix_table_entry->address = &(its_translation->translater); msix_table_entry->vector_control = 0;
当设备写入此寄存器时,它还会通过辅助通道传递其 DeviceID,该通道无法被中断发送者伪造,但可被 ITS 读取。此 DeviceID 根据 ITS 连接的设备类型而异,但对于 PCI Express,它是设备的 requester ID(bus << 8 | device << 3 | function)。
写入此寄存器的任何尚未映射的 DeviceID/EventID 组合将被丢弃,或在 GITS_UMSIR 寄存器上报告。
参考
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Interrupt Translation Service;离线修订号:30730。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论