The PCIe host support for the Renesas RZ/G3E, merged in Linux 7.1, extends the driver of the RZ/G3S. That driver programs its controller with the bitfield macros of the kernel, GENMASK() to define register fields and FIELD_PREP() and FIELD_GET() to write and read them. The RZ/G3E needed registers whose mask is known only at run time, read from a table of SoC data, and FIELD_PREP() rejects such masks at build time.
The series was based on the field_prep() helper that Geert Uytterhoeven was adding for this case at the time, merged in Linux 6.19, instead of a helper of its own. Backporting the series to the CIP 6.1 and 6.12 kernels, which lack that helper, then needed a local one-line version.
The article on __ffs, __fls and alignment covers the bit-scan helpers and the DMA window arithmetic of the same series.
Contents
Register Fields with BIT and GENMASK
A 32-bit register of a controller usually packs several fields, each a group of bits with its own meaning. The driver describes each register by its offset and each field by a mask, a value with ones on the bits of the field. Configuration accesses read and write the configuration space of a PCIe function, 4 KiB of standard and device-specific registers, which the PCI core uses to identify and set up each device. The controller performs them on behalf of the CPU, and the request address register tells it which bus, device, function and register to reach:
#define RZG3S_PCI_REQADR1 0x90 #define RZG3S_PCI_REQADR1_BUS GENMASK(31, 24) #define RZG3S_PCI_REQADR1_DEV GENMASK(23, 19) #define RZG3S_PCI_REQADR1_FUNC GENMASK(18, 16) #define RZG3S_PCI_REQADR1_REG GENMASK(11, 0) #define RZG3S_PCI_PCSTAT1 0x408 #define RZG3S_PCI_PCSTAT1_LTSSM_STATE GENMASK(14, 10) #define RZG3S_PCI_PCSTAT1_DL_DOWN_STS BIT(0)
BIT(n) is 1UL << n, a single-bit field or flag. The UL makes the shift work on an unsigned long, so BIT(31) is well defined where 1 << 31 shifts into the sign bit of an int, as the bit operations article shows. GENMASK(h, l) sets bits h down to l, both included, so GENMASK(23, 19) sets bits 23, 22, 21, 20 and 19, which is 0x00f80000, five bits for the 32 possible device numbers.
The register field is 12 bits wide because the configuration space of a function is 4 KiB. Bits 15:12 belong to no field. Written this way, the definitions copy the bit ranges of the manual, and nobody has to convert 0x00f80000 back to bits to check them.
Both macros have the type unsigned long, 64 bits on arm64. BIT_ULL() and GENMASK_ULL() give 64-bit values on 32-bit CPUs too, and since Linux 6.16 GENMASK_U8() to GENMASK_U128() give a fixed type. The bounds need not be literals. To check which link speeds the other end supports, the driver builds a mask of the speed bits up to 8.0 GT/s with GENMASK(PCI_EXP_LNKSTA_CLS_8_0GB - 1, 0), which is 0x7, bits 2:0, one bit per speed.
Writing a Field with FIELD_PREP
FIELD_PREP(mask, val) moves a value into its field. It shifts the value left by the position of the lowest set bit of the mask, then ANDs it with the mask. The configuration access code builds the request address from four fields and ORs them together:
/* Set the destination */ writel_relaxed(FIELD_PREP(RZG3S_PCI_REQADR1_BUS, bus->number) | FIELD_PREP(RZG3S_PCI_REQADR1_DEV, dev) | FIELD_PREP(RZG3S_PCI_REQADR1_FUNC, func) | FIELD_PREP(RZG3S_PCI_REQADR1_REG, reg), host->axi + RZG3S_PCI_REQADR1);
For register 0x10 of device 0, function 0 on bus 1, each call gives one part of the register value:
FIELD_PREP(RZG3S_PCI_REQADR1_BUS, 1) = 0x01000000 1 << 24 FIELD_PREP(RZG3S_PCI_REQADR1_DEV, 0) = 0x00000000 0 << 19 FIELD_PREP(RZG3S_PCI_REQADR1_FUNC, 0) = 0x00000000 0 << 16 FIELD_PREP(RZG3S_PCI_REQADR1_REG, 0x10) = 0x00000010 0x10 << 0 OR of the four = 0x01000010
The shift comes from the mask, computed by the compiler with __builtin_ffsll() minus one, so no shift constant appears in the driver and a field moved in a new SoC revision only needs its GENMASK() changed. These values, and the others in this article, were checked with BUILD_BUG_ON() in a module built against the kernel headers.
Build-Time Checks
FIELD_PREP() and its companions check their arguments when the driver is compiled, and each check has its own message. Building small test modules that break one rule each gives these errors from GCC:
FIELD_PREP(RZG3S_PCI_REQADR1_FUNC, 8) error: ... declared with attribute error: FIELD_PREP: value too large for the field FIELD_PREP(f->mask, v) /* mask read through a pointer */ error: ... declared with attribute error: FIELD_PREP: mask is not constant FIELD_GET(GENMASK_ULL(39, 32), reg) /* reg is a u32 */ error: ... declared with attribute error: FIELD_GET: type of reg too small for mask GENMASK(3, 8) error: static assertion failed: "const_true((8) > (3)) is true"
The function field has three bits, so 8 does not fit, and the mistake stops the build before the driver ever runs. A mask must be known at compile time, non-zero and made of contiguous bits. A mask wider than the variable it is read from is refused, and so are the bounds of GENMASK() given in the wrong order.
FIELD_PREP() is a statement expression, a GCC extension written ({ ... }) whose value is that of its last statement, so that it can declare variables and run its checks. Statement expressions are only allowed inside functions, so not in a static initializer. FIELD_PREP_CONST() is the variant for initializers, with fewer checks.
Reading a Field with FIELD_GET
FIELD_GET(mask, reg) goes the other way. It ANDs the register value with the mask, which clears every other field, then shifts the result right by the position of the lowest bit of the mask. Before changing the link speed, the driver waits for the LTSSM to reach the L0 state. The LTSSM, the link training and status state machine, brings the link up after reset and through speed changes, and L0 is the state where the link is up and carries traffic; the controller numbers it 0xc:
ret = readl_poll_timeout(host->axi + RZG3S_PCI_PCSTAT1, tmp, FIELD_GET(RZG3S_PCI_PCSTAT1_LTSSM_STATE, tmp) == ltssm_state_l0, PCIE_LINK_WAIT_SLEEP_MS * MILLI, PCIE_LINK_WAIT_SLEEP_MS * MILLI * PCIE_LINK_WAIT_MAX_RETRIES);
readl_poll_timeout() reads PCSTAT1 into tmp until the condition is true or the timeout expires. With L0 in the LTSSM field and all other bits clear, the register reads 0x3000:
Comparing tmp & RZG3S_PCI_PCSTAT1_LTSSM_STATE directly with 0xc would never match, because the field still sits at bit 10. The shift is what makes the value comparable with the state number of the manual.
Read-Modify-Write
Writing one field of a register that holds others requires reading it first, otherwise the write clears the other fields. The driver has a helper for its memory-mapped registers:
static void rzg3s_pcie_update_bits(void __iomem *base, u32 offset, u32 mask,
u32 val)
{
u32 tmp;
tmp = readl_relaxed(base + offset);
tmp &= ~mask;
tmp |= val & mask;
writel_relaxed(tmp, base + offset);
}
~mask has ones everywhere except on the field, so the AND clears the field and keeps the rest, and the OR then puts the new value, already shifted by FIELD_PREP(), in its place. The link speed code uses it to set the target speed in bits 3:0 of the Link Control 2 register of the root port, 8.0 GT/s on the RZ/G3E since the Gen3 patch of the series:
rzg3s_pcie_update_bits(host->pcie, pcie_cap + PCI_EXP_LNKCTL2, PCI_EXP_LNKCTL2_TLS, FIELD_PREP(PCI_EXP_LNKCTL2_TLS, link_speed));
With a target of 5.0 GT/s before the call, the field holds 2. The AND clears bits 3:0, the OR writes 3, the code of 8.0 GT/s, and the other bits of the register keep their value.
Since Linux 6.16, FIELD_MODIFY(mask, ®, val) does the clearing and the ORing on a variable in memory. It takes a pointer and updates the variable in place, so the read and the write of the register stay in the caller. The driver does not use it; the same update written with it would read:
u16 lnkctl2 = readw_relaxed(host->pcie + pcie_cap + PCI_EXP_LNKCTL2); FIELD_MODIFY(PCI_EXP_LNKCTL2_TLS, &lnkctl2, PCI_EXP_LNKCTL2_TLS_8_0GT); writew_relaxed(lnkctl2, host->pcie + pcie_cap + PCI_EXP_LNKCTL2);
Passing the variable itself, as in reg = FIELD_MODIFY(mask, reg, val), fails to build with “invalid type argument of unary *”. A regmap is the kernel API that gives drivers the same register access whether the registers sit in memory or behind I2C or SPI, with an optional cache and a lock. For registers behind a regmap, regmap_update_bits(map, reg, mask, val) reads, modifies and writes under that lock. Neither rzg3s_pcie_update_bits() nor FIELD_MODIFY() takes a lock.
Masks Known at Run Time with field_prep()
The PCIe controller also depends on bits of the system controller, the SYSC, a block of miscellaneous SoC settings that the driver reaches through a syscon regmap. The two SoCs use different SYSC bits. The RZ/G3S releases a reset through bit 0 at offset 0xd74.
The RZ/G3E has no such reset, but needs a bit that puts the controller in root complex mode, since the same controller can also act as an endpoint, and a bit that allows ASPM L1, a low-power link state that the link enters when idle, in two other registers. The series moved these bits into the SoC data:
static const struct rzg3s_pcie_soc_data rzg3e_soc_data = {
[...]
.sysc_info = {
.functions = {
[RZG3S_SYSC_FUNC_ID_L1_ALLOW] = {
.offset = 0x1020,
.mask = BIT(0),
},
[RZG3S_SYSC_FUNC_ID_MODE] = {
.offset = 0x1024,
.mask = BIT(0),
},
},
},
};
One function writes any of these bits through the SYSC regmap:
static int rzg3s_sysc_config_func(struct rzg3s_sysc *sysc,
enum rzg3s_sysc_func_id fid, u32 val)
{
const struct rzg3s_sysc_info *info = sysc->info;
const struct rzg3s_sysc_function *functions = info->functions;
if (fid >= RZG3S_SYSC_FUNC_ID_MAX)
return -EINVAL;
if (!functions[fid].mask)
return 0;
return regmap_update_bits(sysc->regmap, functions[fid].offset,
functions[fid].mask,
field_prep(functions[fid].mask, val));
}
The mask is read through sysc->info, so it is not a compile-time constant and FIELD_PREP() stops the build with “mask is not constant”, as above. Constness is decided by the compiler after optimization. In a test with a static table of a single entry, GCC folded the mask and FIELD_PREP() built, which the pointer to SoC data of the driver prevents. field_prep() and field_get() in <linux/bitfield.h> accept it.
When the mask is constant, field_prep() expands to the checked version of FIELD_PREP(). Otherwise it computes the shift at run time with __ffs(), or __ffs64() for a 64-bit mask. A zero mask has no shift, since __ffs(0) is undefined, which is why rzg3s_sysc_config_func() returns before calling field_prep() for a SYSC function the SoC does not have, such as the reset on the RZ/G3E.
The class code of a PCI function tells the PCI core what kind of device it is, and 0x0604 is a PCI-to-PCI bridge, which a root port must report. The RZ/G3E needs its class code written explicitly, where the RZ/G3S has the right value after reset. The class code is bits 31:8 of the register at offset 0x08 of the configuration header, the revision ID bits 7:0. The driver keeps the mask in a variable and writes the PCI-to-PCI bridge class, 0x060400, without touching the revision:
u32 mask = GENMASK(31, 8); [...] rzg3s_pcie_update_bits(host->pcie, PCI_CLASS_REVISION, mask, field_prep(mask, PCI_CLASS_BRIDGE_PCI_NORMAL));
GCC sees through the variable, so __builtin_constant_p(mask) is true and field_prep() expands to the checked FIELD_PREP(), which gives 0x06040000. FIELD_PREP(mask, ...) builds here as well.
Backporting to Kernels Without field_prep()
The series was also backported to the CIP 6.1 and 6.12 kernels, the long-term kernels of the Civil Infrastructure Platform, which predate field_prep(). The first backport took the two bitfield commits of Linux 6.19 along with it. Upstream, that series also converted the drivers that had their own field_prep() or field_get().
The CIP kernels still carry those local definitions, in the at91 clock driver for both, and in 6.12 also in the mlx90614 temperature sensor, the ma35 pin controller and the USB audio quirks. With the generic helpers next to them, the build warned, so the backport reverts the two commits and adds a local macro to the PCIe driver, the same line those drivers use:
#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
ffs() counts bits from 1, hence the - 1. The macro has none of the checks of the generic helper, handles 32-bit masks only, since ffs() takes an int, and gives an undefined shift for a zero mask, which the early return of rzg3s_sysc_config_func() avoids. For a backport, it keeps the driver code identical to mainline without touching other drivers.
Summing Up
In this driver, each field is a GENMASK() next to its register offset, and FIELD_PREP() and FIELD_GET() reject at build time the values and masks that do not fit, as the errors above show. The SoC data added for the RZ/G3E turned some masks into variables, which field_prep() handles as long as the mask is never zero. Before taking such a driver to an older kernel, check whether field_prep() and FIELD_MODIFY() exist there, since they appeared in 6.19 and 6.16. For the CIP kernels, a one-line local macro in the driver was simpler than backporting the generic helpers.
How do you handle register layouts that differ between SoCs of the same family? Tell us in the comments.


