I need to write a linux kernel driver for a PCIe device with SRAM.
For the first attempt, I've written a driver to access SRAM from PCIe with a character device.
Everything works as expected, but there is one problem. SRAM is slow 1MB takes about 2 secs to read/write, this is a hardware limitation. The CPU is 100% busy while reading/writing. Witch is a problem. I don't need speed, reading/writing can be slow, but why it takes so much CPU?
The buffer is initialized with pci_iomap
:
g_mmio_buffer[0] = pci_iomap(pdev, SRAM_BAR_H, g_mmio_length);
read/write functions looks like this:
static ssize_t dev_read(struct file *fp, char *buf, size_t len, loff_t *off) { unsigned long rval; size_t copied; rval = copy_to_user(buf, g_mmio_buffer[SRAM_BAR] + *off, len); if (rval < 0) return -EFAULT; copied = len - rval; *off += copied; return copied;}static ssize_t dev_write(struct file *fp, const char *buf, size_t len, loff_t *off) { unsigned long rval; size_t copied; rval = copy_from_user(g_mmio_buffer[SRAM_BAR] + *off, buf, len); if (rval < 0) return -EFAULT; copied = len - rval; *off += copied; return copied;}
The question is what can I do with high CPU usage?
Should I rewrite the driver to use a block device instead of a character?
Allow CPU to work on another process while reading/saving data?