1

I am trying to use the ARM watchdog thorough kernel space. I have a watchdog driver which is statically built and deployed in the kernel. The memory remapped by the driver is seen in /proc/iomem.

cat /proc/iomem | grep wdt
ff567000-ff567018 : /wdt@ff567000

The driver has remapped the address starting from 0xff567000 to a virtual address in the kernel.
Now I wrote a module to ioremap the same address and write to it.

static int __init wdt_init(void)
{
    int ret;

    wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
    if (!wdev)
        return -ENOMEM;
    mutex_init(&wdev->lock);

    printk(KERN_INFO "before mapping %p.\n", wdev->base);

    wdev->base = ioremap (0xff567000, 0x18);
    if (!wdev->base) 
    {
        ret = -ENOMEM;
        goto fail;
    }
    printk(KERN_INFO "wdt base address successfully mapped to %p.\n", wdev->base);
    wdt_start(wdev);
    wdt_get_timeout (wdev);

    wdt_set_timeout (wdev, 30);
    wdt_get_timeout (wdev);

    while (1)
    {
        wdt_ping(wdev);
    }

    return 0;

fail:
    printk(KERN_INFO "failed to map wdt base address\n");

    return ret;
}

The output seen after inserting the module is:

root@bdk:/opt# insmod test_ioremap.ko 
[ 1770.862628] before mapping   (null).
[ 1770.867477] VXR10 wdt base address successfully mapped to f0988000.

Further I am able to read and write to the watchdog registers successfully.

Please tell me, Will this mapping have any effect on the normal working of the driver ?

Thanks for all your help in advance.

nishad kamdar
  • 67
  • 2
  • 11
  • Simply making another mapping on device "memory" should cause no issues on ARM. But multiple drivers accessing/manipulating the same device memory will probably cause device malfunction and/or unpredictable behavior. Well-behaved drivers (e.g. the original watchdog driver) call **request_mem_region()** before **ioremap()**, which would completely avoid this issue that you raise. BTW your code does fully comply with kernel coding style. – sawdust Mar 02 '18 at 07:29
  • Ok thanks for the input, the driver in question uses devm_ioremap_resource(), and platform_get_resource() before that. Is that safe enough ? – nishad kamdar Mar 03 '18 at 07:31
  • No, because you misunderstand what I wrote. Your module is ill-behaved by defying driver conventions. It's only *"safe"* when all drivers (and code that access HW) adhere to the conventions. – sawdust Mar 03 '18 at 19:54
  • I am a bit confused. My module is not a driver. I was talking about the driver which originally maps the memory region uses the above functions for mapping. – nishad kamdar Mar 04 '18 at 09:26
  • I found a comment here, [link](https://lists.kernelnewbies.org/pipermail/kernelnewbies/2011-May/001959.html), which says **if u call ioremap() multiple times, u will just remove the previous mapping setup.** This is something that concerned me. – nishad kamdar Mar 04 '18 at 09:27
  • 1
    *"My module is not a driver"* -- Your *"module"* accesses device registers/memory just like a kernel driver. Just because you don't want to call it a driver does not make it just a *"module"*. Your link is interesting (but not arch specific), and I could be wrong. But [Russell King](https://lkml.org/lkml/2015/7/7/235), the ARM Linux guru mentions *"On ARM, we (probably) have a lot of cases where ioremap() is used multiple times for the same physical address space."* Regardless, the preferred practice is only one mapping. So your *"module"* is ill-behaved by defying driver conventions. – sawdust Mar 04 '18 at 22:53
  • ok, thanks i get your point. – nishad kamdar Mar 05 '18 at 04:38
  • @sawdust, you missed `request_muxed_region()` API (IIRC the name of the call). – 0andriy Mar 22 '18 at 12:18
  • @sawdust, sorry for the delay, but can you please post your first comment as an answer. It seems to be that it is the best explanation to my query. And I did not see any malfunction on ARM due to multiple mapping. – nishad kamdar Jul 30 '19 at 04:43

1 Answers1

1

Simply making another mapping on device "memory" should cause no issues on ARM. But multiple drivers accessing/manipulating the same device memory will probably cause device malfunction and/or unpredictable behavior. Well-behaved drivers (e.g. the original watchdog driver) call request_mem_region() before ioremap(), which would completely avoid this issue that you raise.

Russell King, the ARM Linux guru mentions "On ARM, we (probably) have a lot of cases where ioremap() is used multiple times for the same physical address space." Regardless, the preferred practice is only one mapping.

nishad kamdar
  • 67
  • 2
  • 11