feat(cellular, power): enable SDM660 IPA v2.6L 4G LTE, power-key status screen, and DVFS standby
Offline project checks / check (push) Canceled after 0s

This commit is contained in:
shenlei
2026-11-18 01:39:26 +09:00
parent 5a50a8264c
commit 8d12d341bf
43 changed files with 6850 additions and 229 deletions
+82
View File
@@ -0,0 +1,82 @@
# Command-engine initialization comparison
Investigation checkpoint: 2026-11-13. The last live command reached BAM EOT
but did not change the DMA target. No command-engine or Internet success is
established. The additional global-register observations below are prepared
in source; they have not yet been compiled or sampled on the phone.
The reference is Xiaomi `platina-o-oss`, commit
`0bb2780bccb9c7e05f0edf5db8860e779179b5f9`. Paths below are relative to that
tree. Register offsets are relative to the corrected IPA or BAM aperture,
not the original wrapper address.
| Item | Pinned Android behavior | Current experiment / evidence |
|---|---|---|
| IPA reset | `ipa_v2/ipa_utils.c:ipa_init_hw()` asserts then deasserts IPA soft reset at `0x3c` | No global reset; reset ownership remains unresolved |
| IPA enable / BCR | Configuration `0x11`, then v2.6L BCR `0x001fff7f` | Already tried; BCR reads back `0x001fef7f`, SRAM writeback still fails |
| BAM reset | `sps/bam.c:bam_init()` resets and enables BAM | No global reset; AP pipe checks do not establish Q6 pipe idleness |
| BAM local clock gating | IPA sets `SPS_BAM_NO_LOCAL_CLK_GATING`; SPS clears control bits 18:17 | Previous control `0x20002` has field value 1; source now records it explicitly |
| BAM configuration | SPS writes `0xfffff7ff` unless the separate no-external-pipe-reset option is used | No write by the diagnostic; new read at BAM `0x7c` is pending |
| BAM summing threshold | IPA explicitly supplies `IPA_SUMMING_THRESHOLD = 0x10` | New read at BAM `0x8` is pending; do not confuse this with the per-pipe event threshold or SPS's fallback of 4 |
| Initial core vote | `ipa_v2/ipa.c` starts at turbo, 200 MHz | Existing diagnostic requests 75 MHz after preparing the clock; new JSON records the provider's cached request |
| SMMU clock / bus votes | Vendor DT names `AGGR2_NOC_SMMU_CLK` and supplies two bus paths | Need to compare matching mainline SMMU/interconnect ownership before adding duplicate votes |
| Command EP configuration | `ipa_setup_apps_pipes()` requests DMA mode but sets `skip_ep_cfg=true` | EP control/mode both read 0; requested mode alone does not justify a register write |
| Addressing | Vendor supports both bypass and translated SPS allocations | Both 32-bit and below-1-GiB translated tests failed; preserve the strict DMA domain |
The constants for the older IPA-specific BAM configuration path are not the
v2.6L initialization value. In `ipa_setup_bam_cfg()`, the write is conditional
on hardware older than v2.5, even though the switch also selects a constant
for v2.6L. Preserve that condition when reading the code.
## Next observation
Build `platina_ipa_v2_cmd_test.ko` with the same prepared source, configuration,
Clang and symbol table used for the existing diagnostic. A previous `.ko`
does not contain the new fields. Record the new source and binary hashes
before copying or loading it. Normal modpost and module checks are required.
Use the established opt-in boot procedure to keep MSS offline from startup;
do not stop an already running modem. Verify the corrected ANOC2 context IRQs,
strict DMA domain, battery guard and USB recovery access first. In that test
boot, load only the inspection mode:
```sh
sudo insmod ./platina_ipa_v2_cmd_test.ko inspect=1
sudo cat /sys/module/platina_ipa_v2_cmd_test/parameters/snapshot
sudo rmmod platina_ipa_v2_cmd_test
```
Retain the complete kernel log as well as JSON. New fields are
`bam_config_sampled`, `bam_control`, `bam_cfg_bits`, `bam_desc_threshold` and
`core_cached_hz`. Numbers in JSON are decimal. An early probe rejection can
leave fields zero; only `bam_config_sampled=true` establishes that all three
BAM global reads finished. The cached clock value is not a measured hardware
frequency. Inspection success still has `command_completed=false` and
`datapath_ready=false`.
The new source adds reads only to the BAM global configuration sequence.
It does not change global reset, clock rate, BAM configuration, thresholds,
command bytes or completion rules. The existing command test's per-pipe
writes remain available only with `run=1`.
Compare the inherited state before selecting a single additional experiment.
Do not write the whole vendor initialization sequence merely because one
read differs. Q6/WLAN register reads remain excluded, and an unchanged DMA
target after a submitted command still requires retaining resources until
reboot. Restore the checked original DSP/RMTFS service files and normal boot
after the inspection session.
## Supplementary upstream review
The September 2021 RFC pages for BAM transport and v2 commands are authored
by Sireesh Kodali (the command patch also credits Vladimir Lypak). They are
useful cross-checks, not a qualified driver for this board:
- `https://lists.openwall.net/linux-kernel/2021/09/20/40`
- `https://lkml.iu.edu/2109.2/04407.html`
Their transport path uses a dedicated immediate-command flag and places the
opcode in the descriptor length. Do not copy the RFC command layout without
checking the vendor's v2 bitfields: the pinned shared-memory command puts
direction at word 2 bit 16, followed by `skip_pipeline_clear` at bit 17.
The current encoder and golden-byte test retain that vendor format.
+4
View File
@@ -0,0 +1,4 @@
# Opt-in diagnostic; deliberately absent from the runtime overlay.
obj-m += platina_ipa_v2_probe.o
obj-m += platina_ipa_v2_dma_test.o
obj-m += platina_ipa_v2_cmd_test.o
+164
View File
@@ -0,0 +1,164 @@
# IPA v2.6L Android bring-up
This directory is an opt-in kernel investigation, not an Internet driver.
It is not installed by the runtime overlay and has no automatic service.
As of 2026-11-13, strict DMA mapping and the corrected DT context IRQs have
been verified. A BAM descriptor reached EOT, but the SRAM read did not update
its DMA target. Cellular Internet and command-engine execution remain
unverified. See `docs/CELLULAR.md` in the repository for measured results.
The reference is `MiCode/Xiaomi_Kernel_OpenSource`, branch `platina-o-oss`,
commit `0bb2780bccb9c7e05f0edf5db8860e779179b5f9`:
| Source | What it establishes |
|---|---|
| `arch/arm/boot/dts/qcom/sdm660.dtsi` | Raw IPA/BAM resources, clocks and AP SMMU stream ID |
| `drivers/platform/msm/ipa/ipa_v2/ipa_reg.h` | Register offset `0x40000` within the wrapper |
| `drivers/platform/msm/ipa/ipa_v2/ipa_utils.c` | v2.6L endpoints, register setup and memory partition selection |
| `drivers/platform/msm/ipa/ipa_v2/ipa.c` | BAM registration, command pipe and table initialization order |
| `drivers/platform/msm/ipa/ipa_v2/ipa_ram_mmap.h` | v2.6L SRAM layout; it differs from the experimental GSI data |
| `drivers/platform/msm/ipa/ipa_v2/ipa_dp.c` | Immediate-command descriptor length contains the opcode |
| `include/linux/msm-sps.h` | IPA immediate flag `0x0100`; ordinary BAM command flag `0x0800` |
| `drivers/platform/msm/sps/bam.c` | NDP 4K BAM register layout and pipe ownership |
| `drivers/platform/msm/ipa/ipa_v2/rmnet_ipa.c` | Q6 SRAM initialization precedes IPA QMI service startup |
| `drivers/platform/msm/ipa/ipa_v2/ipa_qmi_service.c` | QMI initialization advertises real table offsets and WAN RX endpoint |
## Read-only diagnostic
Build with a fully prepared **matching** kernel source/configuration and symbol
table, using the same toolchain as that kernel:
```sh
make -C /path/to/kernel ARCH=arm64 LLVM=1 \
M=/path/to/mi8-platina-linux/project/cellular/ipa-v2 \
CONFIG_DEBUG_INFO_BTF_MODULES= modules
```
The probe does not need split BTF debug information. Omitting it avoids
referencing the rebuilt kernel's BTF type IDs when inspecting the currently
running kernel. This does not disable CFI or relax module loading checks.
`platina_ipa_v2_probe.ko` accepts only the platina machine and the existing
SDM660 IPA platform device. It refuses a bound driver and reserves both MMIO
ranges while sampling. It prepares the core clock before requesting the
original driver's 75 MHz SVS vote, then restores the old cached request while
the clock is still prepared and releases it. This order matters: the matching
`clk-smd-rpm.c` ignores rate changes while unprepared. `core_cached_hz` is the
provider's cached request, not a measured hardware frequency.
It reads version/configuration, SRAM bounds, BAM execution-environment
ownership and the four AP pipe states. It does not reset IPA/BAM, write IPA/BAM
registers, allocate DMA descriptors, acknowledge interrupts or send QMI.
There is no device alias: module loading must be explicit. With the reviewed
matching module placed on the device, one snapshot is:
```sh
sudo insmod ./platina_ipa_v2_probe.ko
sudo cat /sys/module/platina_ipa_v2_probe/parameters/snapshot
sudo rmmod platina_ipa_v2_probe
```
The JSON always says `datapath_ready: false`. Readable registers, enabled
pipes, or an IPA QMI reply do not establish public packet delivery. The IPA
register named `ENABLED_PIPES` contains the available pipe count on v2.6L;
`ipa_num_pipes` is not an enabled-pipe bitmap. Each BAM pipe's control bit 1
indicates whether that pipe is enabled.
## DMA mapping and command diagnostics
The same Makefile builds `platina_ipa_v2_dma_test.ko` and
`platina_ipa_v2_cmd_test.ko`. Neither has a device alias or automatic loader.
`scripts/phone-cellular-bam-overlay.py` generates a temporary, separately
named device for the mapping test; it does not replace the production node.
The test checks each allocation against actual CPU backing pages. It does
not submit DMA or prove that hardware uses those translations.
The command module binds only the corrected IPA platform device. It requires
the strict DMA domain and context fault handlers, a private IOMMU group,
MSS offline with no startup references, and initially disabled IPA plus idle
AP/USB pipes. It serializes against MSS startup during the experiment. Do
not stop a running modem to prepare this test: that earlier approach was
followed by an unexplained reboot. The opt-in DSP/RMTFS boot configuration
holds MSS offline only with the exact token `platina.ipa_bringup=1`; ordinary
boots retain their original modem startup behavior. Live service changes
must be restored from checked backups after testing.
`inspect=1` performs only the idle-register checks, without enabling IPA,
accessing SRAM or submitting descriptors. `run=1` instead submits one 16-byte
SRAM read on command pipe 3. Exactly one mode must be selected. The optional
`dma_bits=30` diagnostic bounds IOVA allocation below 1 GiB; the default is
32, and both retain strict IOMMU translation. The low-address experiment
did not fix missing writeback and establishes no new hardware address limit.
The hardware EE0 attribute bitmap includes Q6 DECOMP pipe 11, but reading its
control register while MSS was offline was immediately followed by reset.
The diagnostic therefore reads only the vendor AP/USB endpoints 0–5. It
does not claim to inspect all global pipe state or permit a global reset.
It also uses the actual vendor BCR constant, enables IPA before SRAM access,
and reads SRAM with explicit 32-bit transactions.
Success requires `result=0`, `bam_eot_seen=true`, `command_completed=true`,
`sram_bytes_match=true` and `resources_pinned=false`. Module insertion or
EOT alone is not success. The latest real result is `result=-5`, EOT seen,
target unchanged, command incomplete and resources pinned. A failed memory
comparison waits an additional second after EOT, then retains the driver,
module, clock and mappings until reboot. Do not unload it or start MSS in
that state. After a normal reboot the test resources are gone and the
original boot image and service configuration should be verified.
The C command encoder has a native golden-byte and bounds test, including
opcode 19 in the descriptor's size field and `IMME|EOT = 0x4100`. The tests
exercise the actual shared encoder, but cannot qualify hardware execution.
The next source revision adds read-only BAM global configuration/threshold
and cached core-clock observations. It is not yet compiled or live-tested;
the earlier `.ko` files do not contain these fields. The pinned Android
comparison and inspection procedure are in [INITIALIZATION.md](INITIALIZATION.md).
## Dependencies for the real port
1. Correct register aperture, clock/interconnect votes, AP DMA addressing and
SMMU ownership. The historical `ipa-shared` region is actually BAM.
The running snapshot already shows BAM enabled with EE ownership assigned.
An active modem must be coordinated before any global IPA/BAM reset; do
not reuse a DMA controller driver's unconditional global-reset probe.
2. A BAM command producer at endpoint 3, with **IMME**, not `DMA_PREP_CMD`.
Keep the DMA-mapped payload size separate from the descriptor's opcode.
Reclaim mappings only after hardware completion or a confirmed DMA stop.
3. v2.6L SRAM canaries, header/route/filter tables and modem memory, initialized
through that functioning command pipe. Do not copy the v3.1 memory map.
4. AP RX descriptors, refill/completion and endpoint configuration, including
WAN RX endpoint 5, before starting data traffic. The vendor advertises the
reserved WAN endpoint in `INIT_MODEM_DRIVER`; QMI service startup precedes
the network interface's later data-pipe setup.
5. The modem/uC handshake, RMNET/MAP mux configuration, packet delivery and
modem restart cleanup. Only then qualify WDS calls and public DNS/TCP.
The original firmware assertion identifies an uninitialized immediate-command
client in modem firmware. The missing AP initialization is a supported working
hypothesis, not proof that one QMI request will restore PPP downlink.
## Verified v2.6L memory partition
Evaluating the pinned vendor `ipa_ram_mmap.h` gives these logical SRAM offsets.
The phone reports 8192 bytes and zero restricted bytes. The vendor adds the
restricted-byte count when forming table addresses for immediate commands and
QMI; these are not system DMA addresses.
| Region | Offset | Size |
|---|---:|---:|
| uC information | `0x0080` | `0x0200` |
| IPv4 filter header | `0x0288` | `0x0058` |
| IPv6 filter header | `0x02e8` | `0x0058` |
| IPv4 route header | `0x0348` | `0x003c` |
| IPv6 route header | `0x0388` | `0x003c` |
| Modem header | `0x03c8` | `0x0140` |
| Modem compression/decompression | `0x0510` | `0x0200` |
| Modem memory | `0x0714` | `0x18e8` |
| End, including final canary | `0x2000` | — |
The gaps contain the vendor's canaries. Do not fill an apparent gap or replace
these offsets with the scaffold's v3.1 memory partitions. AP route indices are
7 through 14; modem indices are 0 through 6. The zero-sized AP SRAM header and
filter regions do not remove their required DMA-backed tables or initialization.
+121
View File
@@ -0,0 +1,121 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef PLATINA_IPA_V2_CMD_FORMAT_H
#define PLATINA_IPA_V2_CMD_FORMAT_H
/* Wire layout from pinned platina-o-oss ipa_hw_defs.h and msm-sps.h. */
#ifdef __KERNEL__
#include <linux/byteorder/generic.h>
#include <linux/errno.h>
#include <linux/types.h>
#else
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef uint16_t __le16;
typedef uint32_t __le32;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define cpu_to_le16(x) ((u16)(x))
#define cpu_to_le32(x) ((u32)(x))
#else
#define cpu_to_le16(x) __builtin_bswap16((u16)(x))
#define cpu_to_le32(x) __builtin_bswap32((u32)(x))
#endif
#endif
#define IPA_V2_REGISTER_WRITE 12
#define IPA_V2_DMA_SHARED_MEM 19
#define IPA_V2_BAM_IMME 0x0100
#define IPA_V2_BAM_EOT 0x4000
struct ipa_v2_bam_desc {
__le32 address;
__le16 size;
__le16 flags;
};
struct ipa_v2_shared_mem_cmd {
__le32 size;
__le32 system_address;
__le32 local_address_direction;
__le32 reserved;
};
struct ipa_v2_reg_write_cmd {
__le32 flags_offset;
__le32 value;
__le32 value_mask;
};
static inline bool ipa_v2_dma_range_valid(u64 address, u32 length)
{
return length && address <= 0xffffffffULL &&
length - 1 <= 0xffffffffULL - address;
}
static inline int ipa_v2_shared_mem_encode(struct ipa_v2_shared_mem_cmd *cmd,
u64 address, u32 length, u32 local,
u32 sram_size, u32 restricted,
bool read_from_ipa, bool skip_clear)
{
u32 flags;
if (!ipa_v2_dma_range_valid(address, length) || length > 0xffff ||
local > 0xffff || local < restricted || local > sram_size ||
length > sram_size - local)
return -ERANGE;
cmd->size = cpu_to_le32(length << 16);
cmd->system_address = cpu_to_le32(address);
flags = local | (read_from_ipa ? (1U << 16) : 0U) |
(skip_clear ? (1U << 17) : 0U);
cmd->local_address_direction = cpu_to_le32(flags);
cmd->reserved = 0;
return 0;
}
static inline int ipa_v2_shared_read_encode(struct ipa_v2_shared_mem_cmd *cmd,
u64 address, u32 length, u32 local,
u32 sram_size, u32 restricted)
{
return ipa_v2_shared_mem_encode(cmd, address, length, local, sram_size,
restricted, true, false);
}
static inline int ipa_v2_reg_write_encode(struct ipa_v2_reg_write_cmd *cmd,
u32 offset, u32 value,
u32 value_mask, bool skip_clear)
{
if (offset > 0xffff || (offset & 0x3))
return -ERANGE;
cmd->flags_offset = cpu_to_le32((offset << 16) |
(skip_clear ? (1U << 15) : 0U));
cmd->value = cpu_to_le32(value);
cmd->value_mask = cpu_to_le32(value_mask);
return 0;
}
static inline int ipa_v2_imm_cmd_desc(struct ipa_v2_bam_desc *desc,
u64 payload_address, u32 payload_length,
u16 opcode)
{
if (!ipa_v2_dma_range_valid(payload_address, payload_length))
return -ERANGE;
desc->address = cpu_to_le32(payload_address);
/* The size field is the opcode, never the payload allocation length. */
desc->size = cpu_to_le16(opcode);
desc->flags = cpu_to_le16(IPA_V2_BAM_IMME | IPA_V2_BAM_EOT);
return 0;
}
static inline int ipa_v2_shared_read_desc(struct ipa_v2_bam_desc *desc,
u64 payload_address, u32 payload_length)
{
if (payload_length != sizeof(struct ipa_v2_shared_mem_cmd))
return -ERANGE;
return ipa_v2_imm_cmd_desc(desc, payload_address, payload_length,
IPA_V2_DMA_SHARED_MEM);
}
#endif
+48
View File
@@ -0,0 +1,48 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef PLATINA_IPA_V2_DMA_H
#define PLATINA_IPA_V2_DMA_H
#include <linux/dma-mapping.h>
#include <linux/iommu.h>
#include <linux/scatterlist.h>
/* Validate every page boundary against the DMA allocator's actual CPU pages. */
static inline int ipa_v2_verify_mapping(struct device *dev,
struct iommu_domain *domain,
void *cpu, dma_addr_t dma)
{
struct sg_table table;
struct scatterlist *sg;
size_t offset = 0;
unsigned int i;
int ret;
if (dma > U32_MAX || PAGE_SIZE - 1 > U32_MAX - dma)
return -ERANGE;
ret = dma_get_sgtable(dev, &table, cpu, dma, PAGE_SIZE);
if (ret)
return ret;
for_each_sg(table.sgl, sg, table.orig_nents, i) {
size_t j;
for (j = 0; j < sg->length; j += PAGE_SIZE) {
size_t len = min_t(size_t, PAGE_SIZE, sg->length - j);
phys_addr_t physical = sg_phys(sg) + j;
if (len > PAGE_SIZE - offset ||
iommu_iova_to_phys(domain, dma + offset) != physical ||
iommu_iova_to_phys(domain, dma + offset + len - 1) !=
physical + len - 1) {
ret = -EFAULT;
goto out;
}
offset += len;
}
}
ret = offset == PAGE_SIZE ? 0 : -EFAULT;
out:
sg_free_table(&table);
return ret;
}
#endif
@@ -0,0 +1,804 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Opt-in single-command BAM qualification, not a packet transport.
* Requires the corrected IPA DT and ANOC2 context fault IRQ polarity.
*
* The test enables an otherwise inactive IPA, submits one SRAM-to-system
* read on AP command pipe 3 and compares bytes with the CPU SRAM aperture.
* It never resets the global IPA/BAM, writes SRAM or starts modem/QMI data.
* Uncertain DMA completion pins the driver, mappings and clock until reboot.
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device/class.h>
#include <linux/init.h>
#include <linux/interconnect.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/remoteproc.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include "ipa_v2_cmd_format.h"
#include "ipa_v2_dma.h"
#define IPA_HW_VERSION 0x30
#define IPA_CFG 0x38
#define IPA_SW_RESET 0x3c
#define IPA_MEM_SIZE 0x50
#define IPA_BCR 0x5b0
#define IPA_BCR_V2_6L 0x001fff7f
#define IPA_DEBUG_CNT_CTRL_0 0x640
#define IPA_IRQ_STTS_EE0 0x1008
#define IPA_REG_TEST_MASK 0x1ff1f171U
#define IPA_REG_TEST_VAL 0x15a15150U
#define IPA_SRAM 0x5000
#define IPA_EP_CTRL(n) (0x70 + (n) * 4)
#define IPA_EP_MODE(n) (0x2c0 + (n) * 4)
#define IPA_SVS_RATE 75000000
#define IPA_TURBO_RATE 200000000
#define BAM_CTRL 0x0
#define BAM_LOCAL_CLK_GATING 0x60000
#define BAM_DESC_THRESHOLD 0x8
#define BAM_SUMMING_THRESHOLD 0x10
#define BAM_CFG_BITS 0x7c
#define BAM_CFG_VENDOR 0xfffff7ffU
#define BAM_IRQ_STATUS 0x14
#define BAM_REVISION 0x1000
#define BAM_NUM_PIPES 0x1008
#define BAM_EE0_IRQ_MASK 0x3004
#define BAM_EE0_PIPES 0x300c
#define PIPE_CTRL(n) (0x13000 + (n) * 0x1000)
#define PIPE_RESET(n) (0x13004 + (n) * 0x1000)
#define PIPE_IRQ_STATUS(n) (0x13010 + (n) * 0x1000)
#define PIPE_IRQ_CLEAR(n) (0x13014 + (n) * 0x1000)
#define PIPE_IRQ_ENABLE(n) (0x13018 + (n) * 0x1000)
#define PIPE_SW_OFFSET(n) (0x13800 + (n) * 0x1000)
#define PIPE_EVENT(n) (0x13818 + (n) * 0x1000)
#define PIPE_DESC_ADDRESS(n) (0x1381c + (n) * 0x1000)
#define PIPE_FIFO_SIZE(n) (0x13820 + (n) * 0x1000)
#define PIPE_THRESHOLD(n) (0x13828 + (n) * 0x1000)
#define CMD_PIPE 3
/* Vendor endpoint map: USB 0/1, AP LAN/command/WAN 2..5. */
#define AP_PIPE_MASK GENMASK(5, 0)
#define PIPE_ENABLE BIT(1)
#define PIPE_SYSTEM BIT(5)
#define PIPE_EOT BIT(5)
#define PIPE_ERRORS (BIT(4) | BIT(6) | BIT(7))
#define BAM_ERRORS (BIT(1) | BIT(2))
#define READ_BYTES 16
#define BUFFER_COUNT 3
struct command_test {
struct device *dev;
struct clk *clock;
struct icc_path *icc_mem, *icc_cfg;
struct platform_device *modem_device;
struct rproc *modem;
void __iomem *ipa, *bam;
void *cpu[BUFFER_COUNT];
dma_addr_t dma[BUFFER_COUNT];
u64 old_dma_mask, old_coherent_mask;
unsigned long old_rate;
u32 old_cfg, old_bcr, old_dbg_cnt0;
u32 bam_control, bam_cfg_bits, bam_desc_threshold;
unsigned long core_cached_hz;
u32 read_offset, pipe_status, bam_status, reg_after;
u32 irq_before, irq_after;
u32 local_address;
unsigned int fault_handlers;
bool bam_eot_seen, bytes_match, pinned, modem_locked, dma_masks_saved;
bool bam_config_sampled, bam_cfg_saved, icc_voted;
};
static bool run, inspect;
static bool icc_vote = true;
static bool bam_cfg;
static bool sw_reset;
static bool skip_clear;
static bool turbo_clk;
static unsigned int mode_op;
static unsigned int dma_bits = 32;
static unsigned int cfg_val = 0x11;
static int local_off = 0x0714;
module_param(dma_bits, uint, 0400);
MODULE_PARM_DESC(dma_bits, "Diagnostic DMA aperture: 30 or 32 bits, always translated");
module_param(cfg_val, uint, 0400);
MODULE_PARM_DESC(cfg_val, "IPA_COMP_CFG value when enabled (default 0x11, or 0x1d)");
module_param(local_off, int, 0400);
MODULE_PARM_DESC(local_off, "Explicit SRAM local offset (default 0x0714 modem_ofst, below 0x1ffc canary)");
module_param(mode_op, uint, 0400);
MODULE_PARM_DESC(mode_op, "0: SRAM->DDR read, 1: IPA_REGISTER_WRITE, 2: DDR->SRAM write");
module_param(icc_vote, bool, 0400);
MODULE_PARM_DESC(icc_vote, "Vote on IPA memory and config interconnects if present in DT");
module_param(bam_cfg, bool, 0400);
MODULE_PARM_DESC(bam_cfg, "Program vendor BAM global threshold, cfg bits and clock gating");
module_param(sw_reset, bool, 0400);
MODULE_PARM_DESC(sw_reset, "Pulse IPA_COMP_SW_RESET before IPA enable while MSS is offline");
module_param(skip_clear, bool, 0400);
MODULE_PARM_DESC(skip_clear, "Set skip_pipeline_clear in immediate command");
module_param(turbo_clk, bool, 0400);
MODULE_PARM_DESC(turbo_clk, "Vote 200 MHz turbo IPA clock instead of 75 MHz SVS");
module_param(run, bool, 0400);
MODULE_PARM_DESC(run, "Explicitly authorize the single BAM immediate command test");
module_param(inspect, bool, 0400);
MODULE_PARM_DESC(inspect, "Read idle registers only; never enable IPA or submit DMA");
static char snapshot[1024];
static bool probe_seen, resources_pinned, registered;
static int probe_result = -ENODEV;
static int snapshot_get(char *buffer, const struct kernel_param *kp)
{
return sysfs_emit(buffer, "%s\n", snapshot);
}
static const struct kernel_param_ops snapshot_ops = {
.get = snapshot_get,
};
module_param_cb(snapshot, &snapshot_ops, NULL, 0400);
MODULE_PARM_DESC(snapshot, "Explicit result; loading alone does not prove success");
static void record_result(struct command_test *test, int result)
{
scnprintf(snapshot, sizeof(snapshot),
"{\"datapath_ready\":false,\"result\":%d,"
"\"mode_op\":%u,\"icc_voted\":%s,"
"\"bam_cfg_applied\":%s,\"sw_reset_applied\":%s,\"skip_clear\":%s,"
"\"bam_eot_seen\":%s,\"command_completed\":%s,\"sram_bytes_match\":%s,"
"\"resources_pinned\":%s,\"pipe\":%u,\"read_bytes\":%u,"
"\"inspected_pipe_mask\":%u,\"inspection_only\":%s,"
"\"dma_bits\":%u,"
"\"bam_config_sampled\":%s,\"bam_control\":%u,"
"\"bam_cfg_bits\":%u,\"bam_desc_threshold\":%u,"
"\"core_cached_hz\":%lu,\"reg_after\":%u,"
"\"cfg_val\":%u,\"irq_before\":%u,\"irq_after\":%u,"
"\"local_address\":%u,\"read_offset\":%u,"
"\"pipe_status\":%u,\"bam_status\":%u,"
"\"fault_handlers_present\":%u}",
result, mode_op, test->icc_voted ? "true" : "false",
(!inspect && bam_cfg) ? "true" : "false",
(!inspect && sw_reset) ? "true" : "false",
skip_clear ? "true" : "false",
test->bam_eot_seen ? "true" : "false",
test->bam_eot_seen && test->bytes_match ? "true" : "false",
test->bytes_match ? "true" : "false",
test->pinned ? "true" : "false", CMD_PIPE, READ_BYTES,
(unsigned int)AP_PIPE_MASK, inspect ? "true" : "false",
dma_bits,
test->bam_config_sampled ? "true" : "false", test->bam_control,
test->bam_cfg_bits, test->bam_desc_threshold,
test->core_cached_hz, test->reg_after,
cfg_val, test->irq_before, test->irq_after,
test->local_address, test->read_offset, test->pipe_status,
test->bam_status, test->fault_handlers);
dev_info(test->dev, "command test: %s\n", snapshot);
}
static int verify_fault_irqs(struct of_phandle_args *iommu, unsigned int *active)
{
struct device_node *smmu = iommu->np;
struct of_phandle_args irq;
u32 globals;
int count, i, number, ret;
if (of_property_read_u32(smmu, "#global-interrupts", &globals))
return -EINVAL;
count = of_irq_count(smmu);
if (count <= globals || count > 66)
return -EINVAL;
for (i = globals; i < count; i++) {
ret = of_irq_parse_one(smmu, i, &irq);
if (ret)
return ret;
ret = irq.args_count == 3 && irq.args[0] == 0 &&
irq.args[2] == IRQ_TYPE_LEVEL_HIGH ? 0 : -EINVAL;
of_node_put(irq.np);
if (ret)
return ret;
number = of_irq_get(smmu, i);
if (number <= 0)
return number ?: -ENXIO;
if (irq_has_action(number))
(*active)++;
}
return *active ? 0 : -ENXIO;
}
static int count_device(struct device *dev, void *data)
{
(*(unsigned int *)data)++;
return 0;
}
static int find_modem_child(struct device *child, void *data)
{
struct rproc **modem = data;
if (!child->class || strcmp(child->class->name, "remoteproc") ||
!child->type || strcmp(child->type->name, "remoteproc"))
return 0;
/* Parent driver is locked; retain this public remoteproc class device. */
get_device(child);
*modem = container_of(child, struct rproc, dev);
return 1;
}
static int lock_offline_modem(struct command_test *test)
{
struct device_node *node;
struct device *dev;
int ret;
node = of_find_node_by_path("/soc@0/remoteproc@4080000");
if (!node)
return -ENODEV;
test->modem_device = of_find_device_by_node(node);
of_node_put(node);
if (!test->modem_device)
return -ENODEV;
dev = &test->modem_device->dev;
device_lock(dev);
if (!dev->driver || strcmp(dev->driver->name, "qcom-q6v5-mss"))
return -ENODEV;
/* The MSS driver's private data is qproc, not struct rproc. */
ret = device_for_each_child(dev, &test->modem, find_modem_child);
if (ret != 1)
return ret ?: -ENODEV;
mutex_lock(&test->modem->lock);
test->modem_locked = true;
if (test->modem->state != RPROC_OFFLINE || atomic_read(&test->modem->power))
return -EBUSY;
return 0;
}
static void put_modem(struct command_test *test)
{
if (test->modem_locked)
mutex_unlock(&test->modem->lock);
test->modem_locked = false;
if (test->modem)
put_device(&test->modem->dev);
if (test->modem_device) {
device_unlock(&test->modem_device->dev);
put_device(&test->modem_device->dev);
}
test->modem = NULL;
test->modem_device = NULL;
}
static u32 traced_read(struct command_test *test, void __iomem *base,
u32 offset, const char *name)
{
u32 value;
/* Preserve the attempted address in ramoops before a possible bus fault. */
dev_info(test->dev, "reading %s at offset %#x\n", name, offset);
value = readl(base + offset);
dev_info(test->dev, "%s=%#x\n", name, value);
return value;
}
static bool other_ap_pipe_active(struct command_test *test)
{
unsigned int i;
/*
* PIPE_ATTR_EE is not an authorization to read every advertised pipe.
* Reading Q6 DECOMP pipe 11 while MSS is offline resets this phone.
* Inspect only vendor AP/USB endpoints, qualified by the read-only test.
* MSS stays locked offline, IPA must start disabled, and no global BAM
* reset is permitted. Q6/WLAN pipe registers are never accessed here.
*/
for (i = 0; i <= 5; i++) {
if (i == CMD_PIPE)
continue;
if (traced_read(test, test->bam, PIPE_CTRL(i), "AP/USB pipe control") &
PIPE_ENABLE)
return true;
}
return false;
}
/* Only called after a completed command, or before its event was posted. */
static int stop_completed_test(struct command_test *test)
{
void __iomem *bam = test->bam;
if (other_ap_pipe_active(test))
return -EBUSY;
/* Stop the attached engine before disabling/resetting its BAM pipe. */
writel(test->old_cfg, test->ipa + IPA_CFG);
if (readl(test->ipa + IPA_CFG) != test->old_cfg)
return -EIO;
writel(0, bam + PIPE_CTRL(CMD_PIPE));
if (readl(bam + PIPE_CTRL(CMD_PIPE)) & PIPE_ENABLE)
return -EIO;
writel(0, bam + PIPE_IRQ_ENABLE(CMD_PIPE));
writel(readl(bam + PIPE_IRQ_STATUS(CMD_PIPE)),
bam + PIPE_IRQ_CLEAR(CMD_PIPE));
writel(1, bam + PIPE_RESET(CMD_PIPE));
wmb(); /* Assert the pipe reset before deasserting it. */
writel(0, bam + PIPE_RESET(CMD_PIPE));
if (readl(bam + PIPE_CTRL(CMD_PIPE)) ||
readl(bam + PIPE_SW_OFFSET(CMD_PIPE)) ||
(readl(bam + PIPE_EVENT(CMD_PIPE)) & 0xffff))
return -EIO;
writel(0, bam + PIPE_DESC_ADDRESS(CMD_PIPE));
writel(0, bam + PIPE_FIFO_SIZE(CMD_PIPE));
writel(0, bam + PIPE_THRESHOLD(CMD_PIPE));
if (test->bam_cfg_saved) {
writel(test->bam_desc_threshold, bam + BAM_DESC_THRESHOLD);
writel(test->bam_cfg_bits, bam + BAM_CFG_BITS);
writel(test->bam_control, bam + BAM_CTRL);
}
writel(test->old_bcr, test->ipa + IPA_BCR);
return readl(test->ipa + IPA_BCR) == test->old_bcr ? 0 : -EIO;
}
static int run_command(struct command_test *test)
{
void __iomem *bam = test->bam;
struct ipa_v2_shared_mem_cmd *command = test->cpu[1];
struct ipa_v2_reg_write_cmd *reg_cmd = test->cpu[1];
struct ipa_v2_bam_desc *descriptor = test->cpu[0];
__le32 before[READ_BYTES / sizeof(u32)], after[READ_BYTES / sizeof(u32)];
__le32 expected[READ_BYTES / sizeof(u32)];
unsigned long timeout;
u32 memory, ownership;
unsigned int i;
int ret, stop_ret;
test->old_cfg = readl(test->ipa + IPA_CFG);
test->old_bcr = readl(test->ipa + IPA_BCR);
test->bam_control = traced_read(test, bam, BAM_CTRL, "BAM control");
test->bam_cfg_bits = traced_read(test, bam, BAM_CFG_BITS, "BAM config bits");
test->bam_desc_threshold = traced_read(test, bam, BAM_DESC_THRESHOLD,
"BAM descriptor summing threshold");
test->bam_config_sampled = true;
ownership = readl(bam + BAM_EE0_PIPES);
memory = readl(test->ipa + IPA_MEM_SIZE);
dev_info(test->dev, "idle hardware: cfg=%#x BCR=%#x memory=%#x BAM=%#x ownership=%#x\n",
test->old_cfg, test->old_bcr, memory, test->bam_control, ownership);
dev_info(test->dev, "inherited BAM config=%#x threshold=%#x core_cached_hz=%lu icc=%u\n",
test->bam_cfg_bits, test->bam_desc_threshold,
test->core_cached_hz, test->icc_voted);
dev_info(test->dev, "checking identity and AP/USB pipe controls\n");
if (readl(test->ipa + IPA_HW_VERSION) != 0x20060001 ||
test->old_cfg != 0x10 || !(test->bam_control & BIT(1)) ||
(readl(bam + BAM_REVISION) & 0xff) != 0x27 ||
(readl(bam + BAM_NUM_PIPES) & 0xc0ff) != 20 ||
!(ownership & BIT(31)) ||
(ownership & AP_PIPE_MASK) != AP_PIPE_MASK ||
memory != 8192 || other_ap_pipe_active(test))
return -EOPNOTSUPP;
dev_info(test->dev, "checking command pipe registers\n");
/* Demand a pristine, disabled pipe; never take over an existing queue. */
if (traced_read(test, bam, PIPE_CTRL(CMD_PIPE), "command control") ||
traced_read(test, bam, PIPE_IRQ_ENABLE(CMD_PIPE), "command IRQ enable") ||
traced_read(test, bam, PIPE_IRQ_STATUS(CMD_PIPE), "command IRQ status") ||
traced_read(test, bam, PIPE_SW_OFFSET(CMD_PIPE), "command SW offset") ||
traced_read(test, bam, PIPE_DESC_ADDRESS(CMD_PIPE), "command FIFO address") ||
traced_read(test, bam, PIPE_FIFO_SIZE(CMD_PIPE), "command FIFO size") ||
traced_read(test, bam, PIPE_THRESHOLD(CMD_PIPE), "command threshold") ||
(traced_read(test, bam, PIPE_EVENT(CMD_PIPE), "command event") & 0xffff) ||
(traced_read(test, bam, BAM_EE0_IRQ_MASK, "EE0 IRQ mask") & BIT(CMD_PIPE)) ||
(traced_read(test, bam, BAM_IRQ_STATUS, "BAM IRQ status") & BAM_ERRORS))
return -EBUSY;
if (inspect) {
traced_read(test, test->ipa, IPA_EP_CTRL(CMD_PIPE), "IPA command EP control");
traced_read(test, test->ipa, IPA_EP_MODE(CMD_PIPE), "IPA command EP mode");
dev_info(test->dev, "idle register inspection completed; no DMA submitted\n");
return 0;
}
test->local_address = local_off >= 0 ? (u32)local_off :
(memory & 0xffff) - READ_BYTES;
if (test->local_address > (memory & 0xffff) - READ_BYTES ||
(test->local_address & 0x3))
return -ERANGE;
dev_info(test->dev, "enabling IPA (mode_op=%u cfg_val=%#x local=%#x sw_reset=%u bam_cfg=%u skip_clear=%u)\n",
mode_op, cfg_val, test->local_address, sw_reset, bam_cfg, skip_clear);
if (sw_reset) {
writel(1, test->ipa + IPA_SW_RESET);
wmb();
writel(0, test->ipa + IPA_SW_RESET);
wmb();
}
writel(cfg_val, test->ipa + IPA_CFG);
writel(IPA_BCR_V2_6L, test->ipa + IPA_BCR);
traced_read(test, test->ipa, IPA_CFG, "enabled IPA config");
traced_read(test, test->ipa, IPA_BCR, "enabled IPA BCR");
traced_read(test, test->ipa, IPA_EP_CTRL(CMD_PIPE), "IPA command EP control");
traced_read(test, test->ipa, IPA_EP_MODE(CMD_PIPE), "IPA command EP mode");
test->irq_before = traced_read(test, test->ipa, IPA_IRQ_STTS_EE0, "IPA IRQ_STTS_EE0 before");
if (bam_cfg) {
writel(test->bam_control & ~BAM_LOCAL_CLK_GATING, bam + BAM_CTRL);
writel(BAM_SUMMING_THRESHOLD, bam + BAM_DESC_THRESHOLD);
writel(BAM_CFG_VENDOR, bam + BAM_CFG_BITS);
test->bam_cfg_saved = true;
traced_read(test, bam, BAM_CTRL, "updated BAM control");
traced_read(test, bam, BAM_DESC_THRESHOLD, "updated BAM threshold");
traced_read(test, bam, BAM_CFG_BITS, "updated BAM cfg bits");
}
/* Vendor SRAM access follows IPA enable and uses 32-bit transactions. */
for (i = 0; i < ARRAY_SIZE(before); i++)
before[i] = cpu_to_le32(readl(test->ipa + IPA_SRAM +
test->local_address + i * sizeof(u32)));
for (i = 0; i < sizeof(before); i++) {
((u8 *)expected)[i] = ~((u8 *)before)[i];
((u8 *)test->cpu[2])[i] = ~((u8 *)before)[i];
}
if (mode_op == 1) {
test->old_dbg_cnt0 = readl(test->ipa + IPA_DEBUG_CNT_CTRL_0);
writel(0, test->ipa + IPA_DEBUG_CNT_CTRL_0);
ret = ipa_v2_reg_write_encode(reg_cmd, IPA_DEBUG_CNT_CTRL_0,
IPA_REG_TEST_VAL,
IPA_REG_TEST_MASK, skip_clear);
if (ret)
return ret;
ret = ipa_v2_imm_cmd_desc(descriptor, test->dma[1],
sizeof(*reg_cmd), IPA_V2_REGISTER_WRITE);
if (ret)
return ret;
} else {
ret = ipa_v2_shared_mem_encode(command, test->dma[2], READ_BYTES,
test->local_address, memory & 0xffff,
memory >> 16, mode_op == 0,
skip_clear);
if (ret)
return ret;
ret = ipa_v2_imm_cmd_desc(descriptor, test->dma[1],
sizeof(*command), IPA_V2_DMA_SHARED_MEM);
if (ret)
return ret;
}
writel(1, bam + PIPE_RESET(CMD_PIPE));
wmb(); /* Assert the pipe reset before programming its FIFO. */
writel(0, bam + PIPE_RESET(CMD_PIPE));
writel(test->dma[0], bam + PIPE_DESC_ADDRESS(CMD_PIPE));
writel(PAGE_SIZE, bam + PIPE_FIFO_SIZE(CMD_PIPE));
writel(16, bam + PIPE_THRESHOLD(CMD_PIPE));
/* Poll status; the EE interrupt mask for pipe 3 stays clear throughout. */
writel(PIPE_EOT | PIPE_ERRORS, bam + PIPE_IRQ_ENABLE(CMD_PIPE));
dma_wmb();
writel(PIPE_SYSTEM | PIPE_ENABLE, bam + PIPE_CTRL(CMD_PIPE));
writel(sizeof(*descriptor), bam + PIPE_EVENT(CMD_PIPE));
dev_info(test->dev, "BAM pipe 3 descriptor posted\n");
timeout = jiffies + msecs_to_jiffies(1000);
ret = -ETIMEDOUT;
do {
test->read_offset = readl(bam + PIPE_SW_OFFSET(CMD_PIPE)) & 0xffff;
test->pipe_status = readl(bam + PIPE_IRQ_STATUS(CMD_PIPE));
test->bam_status = readl(bam + BAM_IRQ_STATUS);
if ((test->pipe_status & PIPE_ERRORS) ||
(test->bam_status & BAM_ERRORS)) {
ret = -EIO;
break;
}
if (test->read_offset == sizeof(*descriptor) &&
(test->pipe_status & PIPE_EOT)) {
test->bam_eot_seen = true;
ret = 0;
break;
}
usleep_range(100, 200);
} while (time_before(jiffies, timeout));
if (!test->bam_eot_seen) {
test->pinned = true;
return ret;
}
/* EOT alone only qualifies BAM consumption, not the IPA operation. */
timeout = jiffies + msecs_to_jiffies(1000);
do {
dma_rmb();
test->pipe_status = readl(bam + PIPE_IRQ_STATUS(CMD_PIPE));
test->bam_status = readl(bam + BAM_IRQ_STATUS);
if ((test->pipe_status & PIPE_ERRORS) || (test->bam_status & BAM_ERRORS))
break;
if (mode_op == 1) {
test->reg_after = readl(test->ipa + IPA_DEBUG_CNT_CTRL_0);
if (test->reg_after == IPA_REG_TEST_VAL)
break;
} else if (mode_op == 2) {
for (i = 0; i < ARRAY_SIZE(after); i++)
after[i] = cpu_to_le32(readl(test->ipa + IPA_SRAM +
test->local_address + i * sizeof(u32)));
if (!memcmp(after, expected, sizeof(expected)))
break;
} else {
if (!memcmp(test->cpu[2], before, sizeof(before)))
break;
}
usleep_range(100, 200);
} while (time_before(jiffies, timeout));
for (i = 0; i < ARRAY_SIZE(after); i++)
after[i] = cpu_to_le32(readl(test->ipa + IPA_SRAM +
test->local_address + i * sizeof(u32)));
test->irq_after = traced_read(test, test->ipa, IPA_IRQ_STTS_EE0, "IPA IRQ_STTS_EE0 after");
if (mode_op == 1) {
test->reg_after = readl(test->ipa + IPA_DEBUG_CNT_CTRL_0);
writel(test->old_dbg_cnt0, test->ipa + IPA_DEBUG_CNT_CTRL_0);
dev_info(test->dev, "IPA_REGISTER_WRITE reg_after=%#x (expected %#x)\n",
test->reg_after, IPA_REG_TEST_VAL);
test->bytes_match = (test->reg_after == IPA_REG_TEST_VAL) &&
!(test->pipe_status & PIPE_ERRORS) &&
!(test->bam_status & BAM_ERRORS);
} else if (mode_op == 2) {
/* Restore original SRAM contents via 32-bit MMIO writes. */
for (i = 0; i < ARRAY_SIZE(before); i++)
writel(le32_to_cpu(before[i]), test->ipa + IPA_SRAM +
test->local_address + i * sizeof(u32));
dev_info(test->dev, "SRAM write before=%*phN after=%*phN expected=%*phN\n",
READ_BYTES, before, READ_BYTES, after, READ_BYTES, expected);
test->bytes_match = !memcmp(after, expected, sizeof(expected)) &&
!(test->pipe_status & PIPE_ERRORS) &&
!(test->bam_status & BAM_ERRORS);
} else {
dev_info(test->dev, "SRAM before=%*phN after=%*phN DMA target=%*phN\n",
READ_BYTES, before, READ_BYTES, after, READ_BYTES, test->cpu[2]);
test->bytes_match = !memcmp(before, after, sizeof(before)) &&
!memcmp(test->cpu[2], after, sizeof(after)) &&
!(test->pipe_status & PIPE_ERRORS) &&
!(test->bam_status & BAM_ERRORS);
}
dev_info(test->dev, "BAM completed; comparison=%u\n", test->bytes_match);
if (!test->bytes_match) {
if (mode_op == 1) {
/* REGISTER_WRITE has no QSB DDR target/source; safe to stop. */
stop_ret = stop_completed_test(test);
if (stop_ret)
test->pinned = true;
return -EIO;
}
/* IPA may still hold a QSB DMA address after BAM descriptor EOT. */
test->pinned = true;
return -EIO;
}
ret = 0;
stop_ret = stop_completed_test(test);
if (stop_ret) {
test->pinned = true;
return stop_ret;
}
return ret;
}
static int command_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct of_phandle_args args;
struct device_node *smmu;
struct iommu_domain *domain;
struct iommu_group *group;
struct command_test *test = NULL;
struct resource *res;
unsigned int count = 0;
int ret, i, restore_ret;
probe_seen = true;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res || res->start != 0x147c0000 || resource_size(res) != 0xeffc) {
ret = -EINVAL;
dev_err(dev, "corrected IPA resource and IOMMU DT required\n");
goto out_result;
}
res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
if (!res || res->start != 0x14784000 || resource_size(res) != 0x26934) {
ret = -EINVAL;
goto out_result;
}
test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
if (!test) {
ret = -ENOMEM;
goto out_result;
}
test->dev = dev;
ret = of_parse_phandle_with_args(dev->of_node, "iommus", "#iommu-cells",
0, &args);
if (ret)
goto out_result;
smmu = of_find_node_by_path("/soc@0/iommu@16c0000");
ret = smmu && args.np == smmu && args.args_count == 1 &&
args.args[0] == 0x19c0 ? 0 : -EINVAL;
of_node_put(smmu);
if (!ret)
ret = verify_fault_irqs(&args, &test->fault_handlers);
of_node_put(args.np);
if (ret) {
dev_err(dev, "valid ANOC2 context fault IRQs required: %d\n", ret);
goto out_result;
}
domain = iommu_get_domain_for_dev(dev);
if (!domain || domain->type != IOMMU_DOMAIN_DMA) {
ret = -EOPNOTSUPP;
goto out_result;
}
group = iommu_group_get(dev);
if (!group) {
ret = -ENODEV;
goto out_result;
}
ret = iommu_group_for_each_dev(group, &count, count_device);
iommu_group_put(group);
if (ret || count != 1) {
ret = ret ?: -EBUSY;
goto out_result;
}
test->old_dma_mask = dma_get_mask(dev);
test->old_coherent_mask = dev->coherent_dma_mask;
test->dma_masks_saved = true;
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_bits));
if (ret)
goto out_result;
ret = lock_offline_modem(test);
if (ret) {
dev_err(dev, "stopped modem required for command qualification: %d\n", ret);
goto out_result;
}
test->ipa = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(test->ipa)) {
ret = PTR_ERR(test->ipa);
goto out_result;
}
test->bam = devm_platform_ioremap_resource(pdev, 1);
if (IS_ERR(test->bam)) {
ret = PTR_ERR(test->bam);
goto out_result;
}
test->clock = devm_clk_get(dev, "core");
if (IS_ERR(test->clock)) {
ret = PTR_ERR(test->clock);
goto out_result;
}
if (icc_vote) {
test->icc_mem = of_icc_get(dev, "memory");
if (IS_ERR(test->icc_mem)) {
ret = PTR_ERR(test->icc_mem);
test->icc_mem = NULL;
goto out_result;
}
test->icc_cfg = of_icc_get(dev, "config");
if (IS_ERR(test->icc_cfg)) {
ret = PTR_ERR(test->icc_cfg);
test->icc_cfg = NULL;
goto out_icc;
}
if (test->icc_mem) {
ret = icc_set_bw(test->icc_mem, 80000, 640000);
if (ret)
goto out_icc;
}
if (test->icc_cfg) {
ret = icc_set_bw(test->icc_cfg, 80000, 80000);
if (ret)
goto out_icc;
}
test->icc_voted = test->icc_mem || test->icc_cfg;
}
test->old_rate = clk_get_rate(test->clock);
ret = clk_prepare_enable(test->clock);
if (ret)
goto out_icc;
ret = clk_set_rate(test->clock, turbo_clk ? IPA_TURBO_RATE : IPA_SVS_RATE);
test->core_cached_hz = clk_get_rate(test->clock);
if (ret || test->core_cached_hz != (turbo_clk ? IPA_TURBO_RATE : IPA_SVS_RATE)) {
ret = ret ?: -EIO;
goto out_clock;
}
for (i = 0; i < BUFFER_COUNT; i++) {
test->cpu[i] = dma_alloc_coherent(dev, PAGE_SIZE, &test->dma[i], GFP_KERNEL);
if (!test->cpu[i]) {
ret = -ENOMEM;
goto out_free;
}
ret = ipa_v2_verify_mapping(dev, domain, test->cpu[i], test->dma[i]);
if (ret)
goto out_free;
dev_info(dev, "verified buffer %d IOVA=%pad\n", i, &test->dma[i]);
}
ret = run_command(test);
if (test->pinned) {
resources_pinned = true;
probe_result = ret;
__module_get(THIS_MODULE);
record_result(test, ret);
dev_err(dev, "uncertain DMA stop; resources retained until reboot\n");
put_modem(test);
return 0;
}
out_free:
for (i = BUFFER_COUNT - 1; i >= 0; i--)
if (test->cpu[i])
dma_free_coherent(dev, PAGE_SIZE, test->cpu[i], test->dma[i]);
out_clock:
restore_ret = clk_set_rate(test->clock, test->old_rate);
if (restore_ret)
ret = restore_ret;
clk_disable_unprepare(test->clock);
out_icc:
if (test->icc_cfg) {
icc_set_bw(test->icc_cfg, 0, 0);
icc_put(test->icc_cfg);
test->icc_cfg = NULL;
}
if (test->icc_mem) {
icc_set_bw(test->icc_mem, 0, 0);
icc_put(test->icc_mem);
test->icc_mem = NULL;
}
out_result:
if (test) {
if (test->dma_masks_saved) {
restore_ret = dma_set_mask(dev, test->old_dma_mask);
if (restore_ret)
ret = restore_ret;
restore_ret = dma_set_coherent_mask(dev, test->old_coherent_mask);
if (restore_ret)
ret = restore_ret;
}
record_result(test, ret);
put_modem(test);
}
probe_result = ret;
if (ret)
dev_err(dev, "command test failed: %d\n", ret);
return ret;
}
static const struct of_device_id command_match[] = {
{ .compatible = "qcom,sdm660-ipa" },
{ }
};
static struct platform_driver command_driver = {
.probe = command_probe,
.driver = {
.name = "platina-ipa-v2-cmd-test",
.of_match_table = command_match,
.probe_type = PROBE_FORCE_SYNCHRONOUS,
.suppress_bind_attrs = true,
},
};
static int __init command_init(void)
{
int ret;
if (run == inspect || mode_op > 2 || (dma_bits != 30 && dma_bits != 32) ||
!(cfg_val & 0x1) || (cfg_val & ~0x1fU) ||
PAGE_SIZE != 4096 || !of_machine_is_compatible("xiaomi,platina"))
return -ENODEV;
ret = platform_driver_register(&command_driver);
if (ret)
return ret;
registered = true;
if (resources_pinned)
return 0;
platform_driver_unregister(&command_driver);
registered = false;
return probe_seen ? probe_result : -ENODEV;
}
static void __exit command_exit(void)
{
if (registered)
platform_driver_unregister(&command_driver);
}
module_init(command_init);
module_exit(command_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Platina IPA v2.6L opt-in BAM immediate SRAM read test");
@@ -0,0 +1,269 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Temporary AP DMA mapping qualification for the IPA v2.6L BAM port.
*
* The old platina DT omits the AP IOMMU stream. A generated overlay adds
* one temporary test device using the vendor AP stream (ANOC2, 0x19c0).
* The platform/IOMMU core attaches its default DMA domain. We do not
* manufacture physical DMA addresses or attach an identity domain.
*
* No IPA/BAM MMIO, clock change, IRQ, DMA submission, reset, QMI or netdev.
* All allocations and the overlay are removed before successful init.
*/
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/firmware.h>
#include <linux/init.h>
#include <linux/iommu.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/scatterlist.h>
#include <linux/sysfs.h>
#include "ipa_v2_dma.h"
#define AP_STREAM_ID 0x19c0
#define ANOC2_PATH "/soc@0/iommu@16c0000"
#define TEST_PATH "/soc@0/platina-ipa-dma-test@147c0000"
#define TEST_COMPAT "xiaomi,platina-ipa-v2-dma-test"
#define TEST_BUFFERS 3
static char *overlay_firmware = "platina/ipa-v2-dma-test.dtbo";
module_param(overlay_firmware, charp, 0400);
MODULE_PARM_DESC(overlay_firmware, "Overlay generated from this boot's live DT");
static char snapshot[512];
static bool probe_seen;
static int probe_result = -ENODEV;
static int overlay_id;
static int snapshot_get(char *buffer, const struct kernel_param *kp)
{
return sysfs_emit(buffer, "%s\n", snapshot);
}
static const struct kernel_param_ops snapshot_ops = {
.get = snapshot_get,
};
module_param_cb(snapshot, &snapshot_ops, NULL, 0400);
MODULE_PARM_DESC(snapshot, "Mapping test result; no hardware DMA was submitted");
static int count_device(struct device *dev, void *data)
{
unsigned int *count = data;
(*count)++;
return 0;
}
static bool node_at_path(struct device_node *node, const char *path)
{
struct device_node *expected = of_find_node_by_path(path);
bool match = expected && node == expected;
of_node_put(expected);
return match;
}
/* Reject another declaration of the AP stream before adding a test device. */
static int ap_stream_unused(void)
{
struct of_phandle_args args;
struct device_node *node;
int i, ret;
for_each_node_with_property(node, "iommus") {
for (i = 0; ; i++) {
bool conflict;
ret = of_parse_phandle_with_args(node, "iommus",
"#iommu-cells", i, &args);
if (ret == -ENOENT)
break;
if (ret) {
of_node_put(node);
return ret;
}
conflict = node_at_path(args.np, ANOC2_PATH) &&
args.args_count && args.args[0] == AP_STREAM_ID;
of_node_put(args.np);
if (conflict) {
pr_err("%s: AP stream already declared by %pOF\n",
KBUILD_MODNAME, node);
of_node_put(node);
return -EBUSY;
}
}
}
return 0;
}
static int dma_test_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct of_phandle_args args;
struct iommu_domain *domain;
struct iommu_group *group;
struct resource *res;
void *cpu[TEST_BUFFERS] = { };
dma_addr_t dma[TEST_BUFFERS] = { };
unsigned int count = 0;
int ret, i, group_id;
probe_seen = true;
ret = -EINVAL;
if (!node_at_path(dev->of_node, TEST_PATH))
goto out_result;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res || res->start != 0x147c0000 || resource_size(res) != 0x1000)
goto out_result;
res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
if (!res || res->start != 0x14784000 || resource_size(res) != 0x26934)
goto out_result;
ret = of_parse_phandle_with_args(dev->of_node, "iommus", "#iommu-cells",
0, &args);
if (ret)
goto out_result;
ret = args.args_count == 1 && args.args[0] == AP_STREAM_ID &&
node_at_path(args.np, ANOC2_PATH) ? 0 : -EINVAL;
of_node_put(args.np);
if (ret)
goto out_result;
domain = iommu_get_domain_for_dev(dev);
if (!domain || domain->type != IOMMU_DOMAIN_DMA) {
ret = -EOPNOTSUPP;
dev_err(dev, "strict translated DMA domain required\n");
goto out_result;
}
group = iommu_group_get(dev);
if (!group) {
ret = -ENODEV;
goto out_result;
}
ret = iommu_group_for_each_dev(group, &count, count_device);
group_id = iommu_group_id(group);
iommu_group_put(group);
if (ret || count != 1) {
ret = ret ?: -EBUSY;
goto out_result;
}
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
if (ret)
goto out_result;
for (i = 0; i < TEST_BUFFERS; i++) {
cpu[i] = dma_alloc_coherent(dev, PAGE_SIZE, &dma[i], GFP_KERNEL);
if (!cpu[i]) {
ret = -ENOMEM;
goto out_free;
}
ret = ipa_v2_verify_mapping(dev, domain, cpu[i], dma[i]);
if (ret)
goto out_free;
}
scnprintf(snapshot, sizeof(snapshot),
"{\"datapath_ready\":false,\"mapping_verified\":true,"
"\"hardware_dma_verified\":false,\"stream_id\":%u,"
"\"iommu_group\":%d,\"domain_type\":%u,\"dma_bits\":32,"
"\"page_size\":%lu,\"buffers\":%u,"
"\"iovas\":[%llu,%llu,%llu]}",
AP_STREAM_ID, group_id, domain->type, PAGE_SIZE, TEST_BUFFERS,
(unsigned long long)dma[0], (unsigned long long)dma[1],
(unsigned long long)dma[2]);
dev_info(dev, "mapping verified: %s\n", snapshot);
out_free:
for (i = TEST_BUFFERS - 1; i >= 0; i--)
if (cpu[i])
dma_free_coherent(dev, PAGE_SIZE, cpu[i], dma[i]);
out_result:
probe_result = ret;
if (ret)
dev_err(dev, "DMA mapping test failed: %d\n", ret);
return ret;
}
static const struct of_device_id dma_test_match[] = {
{ .compatible = TEST_COMPAT },
{ }
};
/* No MODULE_DEVICE_TABLE: explicit loading only, never an automatic service. */
static struct platform_driver dma_test_driver = {
.probe = dma_test_probe,
.driver = {
.name = "platina-ipa-v2-dma-test",
.of_match_table = dma_test_match,
.probe_type = PROBE_FORCE_SYNCHRONOUS,
.suppress_bind_attrs = true,
},
};
static int __init dma_test_init(void)
{
const struct firmware *firmware;
struct platform_device *original;
struct device_node *node;
int ret, remove_ret;
if (!of_machine_is_compatible("xiaomi,platina"))
return -ENODEV;
node = of_find_compatible_node(NULL, NULL, "qcom,sdm660-ipa");
if (!node)
return -ENODEV;
original = of_find_device_by_node(node);
of_node_put(node);
if (!original)
return -ENODEV;
device_lock(&original->dev);
if (original->dev.driver || iommu_get_domain_for_dev(&original->dev)) {
ret = -EBUSY;
goto out_device;
}
ret = ap_stream_unused();
if (ret)
goto out_device;
ret = request_firmware_direct(&firmware, overlay_firmware, &original->dev);
if (ret)
goto out_device;
if (!firmware->size || firmware->size > 4096) {
ret = -EINVAL;
goto out_firmware;
}
ret = platform_driver_register(&dma_test_driver);
if (ret)
goto out_firmware;
ret = of_overlay_fdt_apply(firmware->data, firmware->size, &overlay_id, NULL);
if (!ret)
ret = probe_seen ? probe_result : -ENODEV;
platform_driver_unregister(&dma_test_driver);
if (overlay_id > 0) {
remove_ret = of_overlay_remove(&overlay_id);
if (remove_ret) {
pr_err("%s: overlay cleanup failed: %d\n",
KBUILD_MODNAME, remove_ret);
ret = remove_ret;
}
}
if (!ret)
pr_info("%s: buffers freed, test device and overlay removed\n",
KBUILD_MODNAME);
out_firmware:
release_firmware(firmware);
out_device:
device_unlock(&original->dev);
put_device(&original->dev);
return ret;
}
static void __exit dma_test_exit(void)
{
}
module_init(dma_test_init);
module_exit(dma_test_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Platina IPA v2.6L temporary strict IOMMU mapping test");
@@ -0,0 +1,214 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Read-only register snapshot for the platina IPA v2.6L port.
*
* Register locations: Xiaomi platina-o-oss commit
* 0bb2780bccb9c7e05f0edf5db8860e779179b5f9, ipa_v2/ipa_reg.h and
* drivers/platform/msm/sps/bam.c (SPS_BAM_NDP_4K).
*
* No device alias, driver binding, reset, DMA, IRQ acknowledgement, or QMI
* messages. Clock references and MMIO reservations exist only during init.
*/
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/sysfs.h>
#define IPA_WRAPPER_BASE 0x14780000
#define IPA_REG_OFFSET 0x40000
#define IPA_REG_BASE (IPA_WRAPPER_BASE + IPA_REG_OFFSET)
#define IPA_REG_SIZE 0x1000
#define IPA_BAM_BASE 0x14784000
#define IPA_BAM_SIZE 0x26934
/* Original driver's SVS vote; RPM does not expose a measured frequency. */
#define IPA_SVS_RATE 75000000
#define IPA_COMP_HW_VERSION 0x30
#define IPA_VERSION 0x34
#define IPA_COMP_CFG 0x38
#define IPA_SHARED_MEM_SIZE 0x50
#define IPA_ENABLED_PIPES 0x5dc
#define BAM_CTRL 0x0
#define BAM_DESC_THRESHOLD 0x8
#define BAM_IRQ_STATUS 0x14
#define BAM_CNFG_BITS 0x7c
#define BAM_REVISION 0x1000
#define BAM_NUM_PIPES 0x1008
#define BAM_AHB_ERR_CTRL 0x1024
#define BAM_AHB_ERR_ADDR 0x1028
#define BAM_PIPE_ATTR_EE0 0x300c
#define BAM_PIPE_CTRL(n) (0x13000 + 0x1000 * (n))
#define BAM_PIPE_SW_OFSTS(n) (0x13800 + 0x1000 * (n))
static char snapshot[1024];
static int snapshot_get(char *buffer, const struct kernel_param *kp)
{
return sysfs_emit(buffer, "%s\n", snapshot);
}
static const struct kernel_param_ops snapshot_ops = {
.get = snapshot_get,
};
module_param_cb(snapshot, &snapshot_ops, NULL, 0400);
MODULE_PARM_DESC(snapshot, "Read-only IPA/BAM snapshot; does not prove datapath readiness");
static int __init platina_ipa_v2_probe_init(void)
{
struct platform_device *pdev;
struct device_node *node;
struct resource *resource;
struct clk *clock;
void __iomem *ipa, *bam;
u32 version, hardware, memory, bam_revision, bam_info, num_pipes;
unsigned long clock_rate, old_rate;
int ret, restore_ret;
if (!of_machine_is_compatible("xiaomi,platina"))
return -ENODEV;
node = of_find_compatible_node(NULL, NULL, "qcom,sdm660-ipa");
if (!node)
return -ENODEV;
pdev = of_find_device_by_node(node);
of_node_put(node);
if (!pdev)
return -EPROBE_DEFER;
/* Serialize against driver binding for the entire snapshot. */
device_lock(&pdev->dev);
if (pdev->dev.driver) {
ret = -EBUSY;
goto out_device;
}
resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!resource || (resource->start != IPA_WRAPPER_BASE &&
resource->start != IPA_REG_BASE)) {
ret = -EINVAL;
goto out_device;
}
/* The old DT maps the wrapper, not the actual IPA register aperture. */
if (!request_mem_region(IPA_REG_BASE, IPA_REG_SIZE, KBUILD_MODNAME)) {
ret = -EBUSY;
goto out_device;
}
if (!request_mem_region(IPA_BAM_BASE, IPA_BAM_SIZE, KBUILD_MODNAME)) {
ret = -EBUSY;
goto out_ipa_region;
}
clock = clk_get(&pdev->dev, "core");
if (IS_ERR(clock)) {
ret = PTR_ERR(clock);
goto out_bam_region;
}
old_rate = clk_get_rate(clock);
/*
* Follow the vendor prepare-before-set order. This RPM provider ignores
* set_rate while unprepared, so an earlier successful call is no vote.
*/
ret = clk_prepare_enable(clock);
if (ret)
goto out_clock;
ret = clk_set_rate(clock, IPA_SVS_RATE);
if (ret)
goto out_restore_rate;
clock_rate = clk_get_rate(clock);
if (clock_rate != IPA_SVS_RATE) {
ret = -ENODEV;
goto out_restore_rate;
}
ipa = ioremap(IPA_REG_BASE, IPA_REG_SIZE);
if (!ipa) {
ret = -ENOMEM;
goto out_restore_rate;
}
bam = ioremap(IPA_BAM_BASE, IPA_BAM_SIZE);
if (!bam) {
ret = -ENOMEM;
goto out_unmap_ipa;
}
version = readl(ipa + IPA_VERSION);
hardware = readl(ipa + IPA_COMP_HW_VERSION);
memory = readl(ipa + IPA_SHARED_MEM_SIZE);
bam_revision = readl(bam + BAM_REVISION);
bam_info = readl(bam + BAM_NUM_PIPES);
num_pipes = bam_info & 0xff;
if (!version || version == U32_MAX || !bam_revision ||
bam_revision == U32_MAX || num_pipes < 10 || num_pipes > 20) {
dev_err(&pdev->dev,
"invalid snapshot: version=%#x hardware=%#x BAM=%#x pipes=%u\n",
version, hardware, bam_revision, num_pipes);
ret = -ENODEV;
goto out_unmap_bam;
}
scnprintf(snapshot, sizeof(snapshot),
"{\"datapath_ready\":false,\"core_vote_hz\":%u,"
"\"core_cached_hz\":%lu,"
"\"ipa_version\":%u,\"ipa_hw_version\":%u,\"ipa_cfg\":%u,"
"\"sram_size\":%u,\"sram_restricted\":%u,\"ipa_num_pipes\":%u,"
"\"bam_revision\":%u,\"bam_pipes\":%u,\"bam_ctrl\":%u,"
"\"bam_cfg_bits\":%u,\"bam_desc_threshold\":%u,"
"\"bam_ahb_err_ctrl\":%u,\"bam_ahb_err_addr\":%u,"
"\"bam_num_pipes_raw\":%u,"
"\"bam_irq_status\":%u,\"bam_ee0_pipes\":%u,"
"\"ap_lan_rx\":{\"pipe\":2,\"ctrl\":%u,\"offsets\":%u},"
"\"ap_command_tx\":{\"pipe\":3,\"ctrl\":%u,\"offsets\":%u},"
"\"ap_tx\":{\"pipe\":4,\"ctrl\":%u,\"offsets\":%u},"
"\"ap_wan_rx\":{\"pipe\":5,\"ctrl\":%u,\"offsets\":%u}}",
IPA_SVS_RATE, clock_rate, version, hardware, readl(ipa + IPA_COMP_CFG),
memory & 0xffff, memory >> 16, readl(ipa + IPA_ENABLED_PIPES),
bam_revision, num_pipes, readl(bam + BAM_CTRL),
readl(bam + BAM_CNFG_BITS), readl(bam + BAM_DESC_THRESHOLD),
readl(bam + BAM_AHB_ERR_CTRL), readl(bam + BAM_AHB_ERR_ADDR),
bam_info,
readl(bam + BAM_IRQ_STATUS), readl(bam + BAM_PIPE_ATTR_EE0),
readl(bam + BAM_PIPE_CTRL(2)), readl(bam + BAM_PIPE_SW_OFSTS(2)),
readl(bam + BAM_PIPE_CTRL(3)), readl(bam + BAM_PIPE_SW_OFSTS(3)),
readl(bam + BAM_PIPE_CTRL(4)), readl(bam + BAM_PIPE_SW_OFSTS(4)),
readl(bam + BAM_PIPE_CTRL(5)), readl(bam + BAM_PIPE_SW_OFSTS(5)));
dev_info(&pdev->dev, "read-only snapshot: %s\n", snapshot);
ret = 0;
out_unmap_bam:
iounmap(bam);
out_unmap_ipa:
iounmap(ipa);
out_restore_rate:
/* Restore while prepared: the RPM provider ignores an unprepared set. */
restore_ret = clk_set_rate(clock, old_rate);
if (restore_ret) {
dev_err(&pdev->dev, "could not restore IPA clock rate: %d\n",
restore_ret);
if (!ret)
ret = restore_ret;
}
clk_disable_unprepare(clock);
out_clock:
clk_put(clock);
out_bam_region:
release_mem_region(IPA_BAM_BASE, IPA_BAM_SIZE);
out_ipa_region:
release_mem_region(IPA_REG_BASE, IPA_REG_SIZE);
out_device:
device_unlock(&pdev->dev);
put_device(&pdev->dev);
return ret;
}
static void __exit platina_ipa_v2_probe_exit(void)
{
}
module_init(platina_ipa_v2_probe_init);
module_exit(platina_ipa_v2_probe_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Platina IPA v2.6L read-only bring-up diagnostic");
+5
View File
@@ -0,0 +1,5 @@
# Append only for opt-in IPA hardware qualification, alongside phone-dsp.openrc.
# Without -s, rmtfs serves files without starting/stopping the modem rproc.
case " $(cat /proc/cmdline 2>/dev/null) " in
*" platina.ipa_bringup=1 "*) command_args="-P -r -v" ;;
esac
+2 -2
View File
@@ -16,8 +16,8 @@
"candidate": {
"ports": "ports/candidate",
"kernel": "7.2.12",
"kernel_package": "7.2.12-r22",
"device_package": "5-r31",
"kernel_package": "7.2.12-r25",
"device_package": "5-r32",
"firmware_package": "2-r5"
}
},