> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs-staging.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 构建并安装 RT 内核

Qualcomm<sup>®</sup> Linux RT 内核使用与标准内核相同的 Yocto 工具链构建。
`meta-qcom` 层提供了一个专用的 Bitbake 配方，可自动获取 LTS RT 内核源码、
应用 `PREEMPT_RT` 补丁集并启用所需的 Kconfig 选项。

## 选择正确的 RT 配方

根据目标分支的不同，有两个 RT 配方可用：

| **配方**                      |   **内核轨道**  | **适用场景**               |
| :-------------------------- | :---------: | :--------------------- |
| `linux-qcom-rt_6.18.bb`     |  LTS 6.18.x | 为 QLI 发布版本构建（稳定、可用于生产） |
| `linux-qcom-next-rt_git.bb` | `qcom-next` | 基于最新开发树构建              |

两个配方都位于 `meta-qcom/recipes-kernel/linux/`。本页所有示例均使用
LTS 配方。

## 构建 RT 内核镜像

### 1. 同步工作区

在构建之前，克隆某个发布标签处的 `meta-qcom` 并同步所有 Qualcomm Linux meta 层。
有关工作区设置说明，请参阅
[使用 Yocto 构建内核](./build-kernel-yocto)。

### 2. 打开 RT 内核的 kas shell

```bash theme={null}
kas shell meta-qcom/ci/qcs6490-rb3gen2-core-kit.yml:meta-qcom/ci/linux-qcom-rt-6.18.yml:meta-qcom/ci/qcom-distro-kvm.yml
```

请将 `qcs6490-rb3gen2-core-kit.yml` 替换为您目标开发板对应的 kas machine
文件。

### 3. 构建镜像

```bash theme={null}
bitbake qcom-multimedia-image
```

构建输出位于：

```text theme={null}
build/tmp/deploy/images/<Machine>/<Image>-*.rootfs.qcomflash/
```

### 4. 烧写到设备

```bash theme={null}
cd build/tmp/deploy/images/<Machine>/<Image>-*.rootfs.qcomflash/
fastboot flash efi efi.bin
fastboot flash dtb_a dtb.bin
fastboot reboot
```

## 自定义 RT 内核

### 添加补丁

1. 将补丁文件放入 `recipes-kernel/linux/linux-qcom-6.18/`：

   ```text theme={null}
   meta-qcom/recipes-kernel/linux/linux-qcom-6.18/your_patch.patch
   ```

2. 将其追加到 `linux-qcom-rt_6.18.bb` 中的 `SRC_URI`：

   ```bitbake theme={null}
   SRC_URI += " \
       file://your_patch.patch \
   "
   ```

### 添加 Kconfig 配置片段

1. 创建一个包含所需 `CONFIG_*` 符号的 `.cfg` 文件：

   ```text theme={null}
   meta-qcom/recipes-kernel/linux/linux-qcom-6.18/configs/qcom_rt_custom.cfg
   ```

   示例内容：

   ```text theme={null}
   CONFIG_CPU_ISOLATION=y
   CONFIG_HZ_1000=y
   # CONFIG_TRANSPARENT_HUGEPAGE is not set
   ```

2. 将其追加到 `linux-qcom-rt_6.18.bb` 中的 `SRC_URI`：

   ```bitbake theme={null}
   SRC_URI += " \
       file://configs/qcom_rt_custom.cfg \
   "
   ```

### 修改内核命令行

在 `meta-qcom/ci/base.yml` 中使用 `KERNEL_CMDLINE_EXTRA` 变量添加或
覆盖命令行参数：

```yaml theme={null}
KERNEL_CMDLINE_EXTRA:append: " isolcpus=7 nohz_full=7 rcu_nocbs=7"
```

对于平台特定的命令行调优变量，请在
`meta-qcom/conf/machine/<machine-name>.conf` 中设置：

```text theme={null}
QCOM_RT_CPU        = "7"
QCOM_IRQAFF        = "0-6"
QCOM_RCU_NOCBS     = "7"
QCOM_RCU_EXPEDITED = "1"
QCOM_CPUIDLE_OFF   = "1"
```

这些变量在平台内核命令行上配置 RT 核心隔离、IRQ 亲和性以及 RCU
回调排除。

## 验证 RT 内核已安装

烧写并重启后，确认 RT 内核已激活：

```bash theme={null}
uname -r
# Expected: a version string with the -rt suffix, e.g. 6.18.0-rt1+

zcat /proc/config.gz | grep CONFIG_PREEMPT_RT
# Expected: CONFIG_PREEMPT_RT=y
```

有关安装后的延迟验证，请参阅
[RT 验证和已知限制](./rt-validation-and-known-limitations)。
