在 Linux (ARM) 下,可以在 ISR 中使用 I/O 内存访问吗?

Could I/O memory access be used inside ISR under Linux (ARM)?

我正在 Linux 下创建与 FPGA 通信的驱动程序。 FPGA通过GPMC接口连接。当我从驱动程序上下文中测试 read/write - 一切正常。但问题是我需要在中断时读取一些地址。所以我创建了中断处理程序,将其注册并将 iomemory 读取放入其中(readw 函数)。但是当中断被触发时 - 只读取零。我从上到下测试了驱动程序的每个部分,看起来问题出在 ISR 中的内存访问中。当我用常量值替换 io 访问时 - 它成功传递给用户级应用程序。

ARM 版本:armv7a (Cortex ARM-A8 (DM3730))

编译器:CodeSourcery 2014.05

这是来自驱动程序的一些代码,表示已执行的操作:

// Request physical memory region for FPGA address IO
void* uni_PhysMem_request(const unsigned long addr, const unsigned long size) {
    // Handle to be returned
    void* handle = NULL;
    // Check if memory region successfully requested (mapped to module)
    if (!request_mem_region(addr, size, moduleName)) {
        printk(KERN_ERR "\t\t\t\t%s() failed to request_mem_region(0x%p, %lu)\n", __func__, (void*)addr, size);
    }
    // Remap physical memory
    if (!(handle = ioremap(addr, size))) {
        printk(KERN_ERR "\t\t\t\t%s() failed to ioremap(0x%p, %lu)\n", __func__, (void*)addr, size);
    }
    // Return virtual address;
    return handle;
}

// ...

// ISR
static irqreturn_t uni_IRQ_handler(int irq, void *dev_id) {
    size_t readed = 0;
    if (irq == irqNumber) {
        printk(KERN_DEBUG "\t\t\t\tIRQ handling...\n");
        printk(KERN_DEBUG "\t\t\t\tGPIO %d pin is %s\n", irqGPIOPin, ((gpio_get_value(irqGPIOPin) == 0) ? "LOW" : "HIGH"));
        // gUniAddr is a struct which holds GPMC remapped virtual address (from uni_PhysMem_request), offset and read size
        if ((readed = uni_ReadBuffer_IRQ(gUniAddr.gpmc.addr, gUniAddr.gpmc.offset, gUniAddr.size)) < 0) {
            printk(KERN_ERR "\t\t\t\tunable to read data\n");
        }
        else {
            printk(KERN_INFO "\t\t\t\tdata readed success (%zu bytes)\n", readed);
        }
    }
    return IRQ_HANDLED;
}

// ...

// Read buffer by IRQ
ssize_t uni_ReadBuffer_IRQ(void* physAddr, unsigned long physOffset, size_t buffSize) {
    size_t size = 0;
    size_t i;
    for (i = 0; i < buffSize; i += 2) {
        size += uni_RB_write(readw(physAddr + physOffset)); // Here readed value sent to ring buffer. When "readw" replaced with any constant - everything OK
    }
    return size;
}

看起来问题出在代码优化上。我更改了 uni_RB_write 函数以传递物理地址和数据大小,现在也通过 ioread16_rep 函数读取。所以现在一切正常。