platform_driver Probe, Resources, and IRQs
Enter platform_driver.probe() to translate inclusive Device Tree register ranges and GIC interrupt specifiers into checked Linux 6.12 capabilities, preserve negative deferral codes, and design hardware quiescence for a void remove() callback.
What you'll learn
- Propagate an interrupt dependency error before requesting the IRQ.
- Compute the size of an inclusive resource range.
- Plan quiescence before managed resources disappear.
Capability Acquisition and Why remove Returns void in Linux 6.12
Once platform_match() (m01-l01) pairs a struct platform_device with your struct platform_driver, really_probe() calls your .probe(pdev) function. Why does platform_driver.probe return int while platform_driver.remove returns void in Linux 6.12? The asymmetry reflects a physical reality of hardware systems! During probe(), a driver acquires capabilities before exposing any user interface: allocating per-device state, reserving and mapping MMIO, resolving clocks/resets, and translating interrupts. Any missing resource or unready supplier (-EPROBE_DEFER) can legitimately abort probe() with a negative errno while leaving zero published side effects. Once bound, however, driver unbind or device removal is an unconditional obligation, not a negotiation: returning an error from legacy int remove() never kept detached hardware alive—it only leaked half-dismantled kernel objects, which is why mainline converted platform_driver.remove to void!
Inside probe(), first-month kernel bugs cluster around confusing four completely distinct number spaces and three incompatible C error conventions, all mapped in Figure 1.2a:
- Inclusive Physical Resource Bounds (
struct resource): WhenDevice Treespecifiesreg = <0x1000 0x1000>;, Linux stores.start = 0x1000and inclusive.end = 0x1fff(usingresource_size_t, never raw pointer casts). Subtractingend - startyields4095—silently losing1byte!resource_size(res)computesend - start + 1 = 4096bytes (4 KiB). - Error-Pointer
MMIOMapping (devm_platform_ioremap_resource): Looks upIORESOURCE_MEMindex0, claims exclusive/proc/iomemownership (devm_request_mem_region), and maps uncachedMT_DEVICEkernel virtual memory (devm_ioremap). Crucially, on failure it returnsERR_PTR(-errno)(0xFFFF_F000..0xFFFF_FFFFonARMv7-A), NOTNULL! Testingif (!d->base)treats an error pointer as valid and crashes on the firstreadl()—always checkIS_ERR(d->base)and returnPTR_ERR(d->base). - Signed Linux Virtual IRQ (
platform_get_irq): Walks theDevice Treeinterrupt-parent(GICor GPIOirq_domain) to translate interrupt index0into a positive Linuxvirq(e.g.,41)—or returns a negativeerrnosuch as-EPROBE_DEFER(-517). Castingirqtounsigned intbeforerequest_irq()turns-517into4294966779U, replacing clean with a permanent-EINVALfailure!
Figure 1.2a. Translating firmware descriptions into checked kernel capabilities — inclusive resource math, IS_ERR() MMIO checks, and signed IRQ propagation.
mental model: Acquiring capabilities in probe() is like signing out an engineering test bay: the bay's building coordinate (struct resource physical address) is not the electronic keycard (void __iomem *), and when the lease ends (void remove), you must power down active machinery (DMA, timers, IRQs) before facilities reclaims the keycard and furniture (devres release).
Where it stops working: Unlike a physical room lease, kernel helpers encode failures across three incompatible C conventions (NULL, ERR_PTR, and negative int), and touching mapped MMIO while a peripheral clock is gated triggers an immediate synchronous external abort on ARMv7-A.
definition (platform_driver.probe(), devm_platform_ioremap_resource() & platform_get_irq()): platform_driver.probe(pdev) is the transactional int-returning binding callback that acquires per-device memory, MMIO, clocks, and interrupts before exposing interfaces, paired with void (*remove)(pdev) for mandatory teardown. devm_platform_ioremap_resource(pdev, index) combines IORESOURCE_MEM lookup, exclusive region reservation, and managed ioremap(), encoding failures as ERR_PTR(-errno). platform_get_irq(pdev, index) translates firmware interrupt index index through its irq_domain into a positive Linux IRQ number or a negative errno (< 0). Neither managed MMIO nor devm_kzalloc() stops DMA, disables peripheral interrupts, or flushes workers automatically on unbind.
Step through the four-stage capability acquisition and teardown contract below:
- Step 1. Step 1: Allocate zeroed per-instance driver state via devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL).
- Step 2. Step 2: Reserve and map inclusive [start..end] MMIO via devm_platform_ioremap_resource(); check IS_ERR().
- Step 3. Step 3: Translate interrupt index 0 via platform_get_irq(); return irq immediately if irq < 0 (preserving -517).
- Step 4. Step 4: Publish interfaces last in probe(), and quiesce DMA/IRQs/workers inside void remove() before runs.
Figure 1.2b. Check NULL on devm_kzalloc, IS_ERR() on devm_platform_ioremap_resource, and irq < 0 on platform_get_irq before publishing interfaces or requesting an IRQ.
Hardware Clock Prerequisites and Quiescing Before Managed Unmap
Inspect Figure 1.2b carefully: why does demo_probe() below deliberately stop before calling readl(d->base)? Because devm_platform_ioremap_resource() only builds ARMv7-A MMU page-table entries (MT_DEVICE)—it does not enable the peripheral's CCM clock gate or deassert hardware reset! On our Cortex-A7 SoC, reading an unclocked IP block stalls the AXI/IPG bridge and raises an Asynchronous/Synchronous External Abort (Unhandled fault: external abort on non-linefetch). Always enable clocks, regulators, and pm_runtime before touching registers, and never call devm_request_mem_region() a second time on a resource already claimed by devm_platform_ioremap_resource() (it will fail with -EBUSY!).
Figure 1.2b. probe() commits interfaces last so errors leave zero partial state; void remove() must quiesce active hardware and callbacks before devres unmaps memory.
Look equally closely at the right half of Figure 1.2b. An empty demo_remove() is valid only because demo_probe() started zero hardware DMA engines, interrupts, timers, or user nodes! The moment a real driver starts DMA or schedules a workqueue, remove() must stop user entry points, halt DMA, mask peripheral interrupts, and synchronize in-flight workers before returning—because the instant remove() returns, devres_release_all() unmaps d->base and frees *d!
devm_platform_ioremap_resource(9) GL-E05 Quick Reference devm_platform_ioremap_resource(9) NAME devm_platform_ioremap_resource, platform_get_irq - platform capability helpers SIGNATURES (Linux 6.12) int (*probe)(struct platform_device *pdev); void (*remove)(struct platform_device *pdev); void __iomem *devm_platform_ioremap_resource(struct platform_device *pdev, unsigned int index); int platform_get_irq(struct platform_device *pdev, unsigned int num); ERROR CONVENTIONS devm_kzalloc -> NULL on failure (-ENOMEM) devm_platform_ioremap_resource -> IS_ERR(p) / PTR_ERR(p) (never NULL!) platform_get_irq -> irq < 0 negative errno (preserve -EPROBE_DEFER) Kernel Systems Academy Lesson 1.2 devm_platform_ioremap_resource(9) GL-E05 quick reference · full manual page: man 9 devm_platform_ioremap_resource | Kernel Helper / Callback | Return Convention | Exact Check Idiom | Bug If Checked Wrongly |
|---|---|---|---|
devm_kzalloc(dev, sz, GFP_KERNEL) | void * or NULL | if (!d) return -ENOMEM; | NULL pointer dereference in probe() |
resource_size(res) ([start..end]) | resource_size_t bytes | end - start + 1 (0x1000..0x1fff -> 4096) | Raw end - start loses 1 byte (4095) |
devm_platform_ioremap_resource(p, 0) | void __iomem * or ERR_PTR | if (IS_ERR(b)) return PTR_ERR(b); | if (!b) misses 0xFFFF_FFEA -> Kernel Oops |
platform_get_irq(p, 0) | Positive virq or -errno | if (irq < 0) return irq; | (unsigned)irq turns -517 into 4294966779U |
void (*remove)(pdev) | void (cannot veto!) | Stop DMA/IRQs/work before return | In-flight ISR/DMA faults after devres unmaps |
Transactional Acquisition in Kernel C and ISO C11 Verification
Compare target_excerpt.c (cross-compiled against a Linux 6.12 ARCH=arm tree with MODULE_DEVICE_TABLE(of, demo_ids) for automatic udev/kmod modalias loading) with our host verifier verify.c. In verify.c, acquire(start, end, irq, out) models transactional capability validation: it rejects NULL output, reversed bounds (end < start), 64-bit wraparound (end - start == UINT64_MAX, since [0 .. UINT64_MAX] contains 2^64 bytes which cannot fit in uint64_t!), non-positive IRQ 0 (-EINVAL = -22), and negative IRQ errors (-517 internal deferral code propagated verbatim)—committing *out = next only after every precondition passes so failed calls leave m ({4096, 41}) completely untouched!
learner@ksa:~/gl-e05/m01-l02$ bat -H 12 -H 13 -H 14 -H 15 -H 16 -H 17 -H 21 target_excerpt.c File: target_excerpt.c #include <linux/module.h> #include <linux/platform_device.h> #include <linux/io.h> #include <linux/of.h> #include <linux/slab.h> struct demo { void __iomem *base; int irq; }; static int demo_probe(struct platform_device *pdev) { struct demo *d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL); if (!d) return -ENOMEM; d->base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(d->base)) return PTR_ERR(d->base); d->irq = platform_get_irq(pdev, 0); if (d->irq < 0) return d->irq; platform_set_drvdata(pdev, d); return 0; /* No hardware or user interface has been enabled here. */ } static void demo_remove(struct platform_device *pdev) { } static const struct of_device_id demo_ids[] = { { .compatible = "ksa,resource-demo" }, { } }; MODULE_DEVICE_TABLE(of, demo_ids); static struct platform_driver demo_driver = { .probe = demo_probe, .remove = demo_remove, .driver = { .name = "resource-demo", .of_match_table = demo_ids }, }; module_platform_driver(demo_driver); MODULE_LICENSE("GPL");- devm_platform_ioremap_resource returns ERR_PTR(-errno) on missing or conflicting resources -- decode with IS_ERR/PTR_ERR.
- Keep d->irq signed (int) and propagate negative errors (including -EPROBE_DEFER) before any request_irq() call.
- Linux 6.12 platform_driver.remove returns void; real drivers must quiesce active DMA, IRQs, and workers here before devres runs.
learner@ksa:~/gl-e05/m01-l02$ bat -H 6 -H 7 -H 8 -H 9 -H 19 -H 20 verify.c File: verify.c #include <assert.h> #include <stdint.h> #include <stdio.h> struct mapped { uint64_t size; int irq; }; static int acquire(uint64_t start, uint64_t end, int irq, struct mapped *out) { if (!out || end < start || end - start == UINT64_MAX) return -22; if (irq <= 0) return irq < 0 ? irq : -22; struct mapped next = { end - start + UINT64_C(1), irq }; *out = next; return 0; } int main(void) { struct mapped m = { 7, 9 }; assert(acquire(0x1000, 0x1fff, 41, &m) == 0); assert(m.size == 4096 && m.irq == 41); assert(acquire(2, 1, 41, &m) == -22); assert(acquire(0, 99, -517, &m) == -517); assert(m.size == 4096 && m.irq == 41); assert(acquire(0, UINT64_MAX, 1, &m) == -22); assert(acquire(0, 99, 0, &m) == -22); puts("resource_bytes=4096 irq=41 defer=preserved partial_state=unchanged"); return 0; }- Reject reversed intervals (end < start) and [0..UINT64_MAX] +1 overflow before computing end - start + 1.
- Propagate negative IRQ errnos (such as -517) untouched while rejecting invalid Linux IRQ 0 with -22 (-EINVAL).
- Verify transactional non-mutation: after failed -22 and -517 calls, m still holds size=4096 and irq=41.
Running the Resource & IRQ Boundary Verifier
Compile and run verify.c below. Confirm 0x1000..0x1fff evaluates to 4096 bytes, -517 propagates unchanged, [0, UINT64_MAX] is rejected without wrapping end - start + 1 to 0, and m retains {4096, 41} across every rejected call. Keep host-versus-target evidence crisp: this native C11 run verifies interval arithmetic and error propagation, whereas target sign-off inspects /proc/iomem, /sys/kernel/debug/irq/irqs/41, and actual GIC delivery on ARMv7-A.
learner@ksa:~/gl-e05/m01-l02gcc -std=c11 -Wall -Wextra -Werror verify.c -o verifylearner@ksa:~/gl-e05/m01-l02./verifyresource_bytes=4096 irq=41 defer=preserved partial_state=unchangedlearner@ksa:~/gl-e05/m01-l02echo $?0learner@ksa:~/gl-e05/m01-l02learner@ksa:~/gl-e05/m01-l02strace -e trace=openat,read,write,close ./verifyopenat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3close(3) = 0openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\243\2\0\0\0\0\0"..., 832) = 832close(3) = 0write(1, "resource_bytes=4096 irq=41 defer"..., 67) = 67+++ exited with 0 +++Why Managed Memory Never Substitutes for Hardware Quiescence
Notice the two deeper architectural lessons demonstrated by demo_probe() and acquire(). First, commit interfaces last: if you register a /dev node or net_device before platform_get_irq() succeeds, user space can open the half-initialized device on another core microseconds before probe() aborts! Second, managed release (devm_*) automates resource deallocation, not activity quiescence. When an operator unbinds a device (echo 209c000.ctrl > unbind), demo_remove() runs before devres_release_all(). Any ongoing DMA transfer, peripheral interrupt enable bit, or scheduled worker must be stopped inside remove() (or via ordered devres actions, covered next) before the MMIO mapping and struct demo disappear.
Common Production Pitfalls
warning: Symptom: dmesg logs genirq: Flags mismatch or request_irq fails with -EINVAL on a huge number (4294966779), permanently aborting a driver that merely needed to wait for its interrupt parent. Root Cause: platform_get_irq() returned -EPROBE_DEFER (-517) into an unsigned int irq variable (or was cast to unsigned int without checking irq < 0). Fix: Store int irq = platform_get_irq(pdev, 0); in a signed int and return irq immediately whenever irq < 0.
warning: Symptom: devm_platform_ioremap_resource(pdev, 0) succeeds, yet the very first readl(d->base) in probe() crashes the Cortex-A7 with Unhandled fault: external abort on non-linefetch. Root Cause: Virtual MT_DEVICE page-table entries exist, but the peripheral's CCM clock gate is still off or its reset line is still asserted, stalling the AXI/IPG bus transaction. Fix: Acquire and clk_prepare_enable() required clocks and deassert reset lines before performing any readl()/writel() access.
Practice & Retrieval
Guided Practice: Auditing Inclusive Resource Math & Error Conventions
A Device Tree node declares reg = <0x021f8000 0x4000>;. (1) Write the exact .start and inclusive .end stored in struct resource (0x021f8000 and 0x021fbfff) and verify end - start + 1 == 16384 (16 KiB). (2) Spot the two bugs in u32 irq = platform_get_irq(pdev, 0); void __iomem *b = devm_platform_ioremap_resource(pdev, 0); if (!b) return -ENOMEM; (irq must be signed int checked for < 0, and b must be checked with IS_ERR(b) returning PTR_ERR(b)!).
Independent Practice: Quiescence-Before-Unmap State Machine
Create a scratch program quiesce_audit.c modeling struct hw_dev { bool dma_active, worker_running, mmio_mapped; }; with unbind_safe() and unbind_buggy() paths. Prove with assert() that unmapping mmio_mapped while dma_active || worker_running violates the teardown invariant, whereas stopping DMA and synchronizing the worker inside void remove() before devres unmaps mmio_mapped passes cleanly under gcc -std=c11 -Wall -Wextra -Werror.
Key takeaways
- In
Linux 6.12,platform_driver.probereturnsint(enabling transactional error/deferral rollback), whereasplatform_driver.removereturnsvoid(unbind cannot be vetoed). struct resourcestores inclusive[start..end]bounds (resource_size = end - start + 1);devm_platform_ioremap_resource()reserves and mapsMMIOand encodes errors viaIS_ERR()/PTR_ERR(), neverNULL.platform_get_irq()translates an interrupt index into a positive LinuxIRQor negativeerrno(< 0, including-EPROBE_DEFER = -517); always checkirq < 0before requesting the interrupt.- Managed
MMIOand memory neither enable gated hardware clocks nor halt activeDMA/workers on unbind. Later: Lessonm01-l03examines the internaldevresLIFO stack,devm_add_action_or_reset(), and cooperative-EPROBE_DEFERretries. - Stop device activity and synchronize callbacks before memory release.
- The managed records release C, then B, then A.