using uio in an embedded platform - elinux.org
TRANSCRIPT
![Page 1: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/1.jpg)
Using UIO in an embedded Using UIO in an embedded platformplatform
Katsuya MATSUBARA Igel Co., Ltd
Hisao MUNAKATA Renesas Solution Corp.
2008.4.17
![Page 2: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/2.jpg)
2008/4/17 2CELF ELC 2008
BackgroundBackground
Have you thought to control a device from user-space?
In fact, there are user level device drivers now.– Multimedia devices (graphic accelerator, etc.)– USB: libusb
![Page 3: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/3.jpg)
2008/4/17 3CELF ELC 2008
UIO (Userspace I/O)UIO (Userspace I/O)
By Han. J. Koch– This interface allows the ability to write the majority of a
driver in userspace with only very shell of a driver in the kernel itself. It uses a char device and sysfs to interact with a userspace process to process interrupts and control memory accesses. (Quoted from Greg Kroah-Hartman's log)
Merged into 2.6.23
![Page 4: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/4.jpg)
2008/4/17 4CELF ELC 2008
ContentsContents
What UIO provides How to write a UIO driver How overhead of UIO
![Page 5: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/5.jpg)
2008/4/17 5CELF ELC 2008
What functions UIO providesWhat functions UIO provides
Interrupt handling I/O memory access Continuous memory allocation
![Page 6: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/6.jpg)
2008/4/17 6CELF ELC 2008
Concern about UIO usageConcern about UIO usage
device dependency in application in-kernel resource unavailable Harder to share a device Inconstant latency Preemptive
![Page 7: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/7.jpg)
2008/4/17 7CELF ELC 2008
Benefit for embeddedBenefit for embedded
Application tight-coupled to device behavior.– Copy-less I/O– Interrupt event relay to user-space
Minor or special device use Exclusive use (no need to share) Make kernel stable and safe. Easier to develop, use rich user libraries.
![Page 8: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/8.jpg)
2008/4/17 8CELF ELC 2008
How to write a UIO driverHow to write a UIO driver
Example target device: SuperH on-chip timer unit (TMU)
TMU has the following features:– Count down periodic counter– 5 channels (SH-3, SH-4)– Selectable base frequency– Interrupt when underflow– The kernel uses 1 or 2 channels for tick and high
resolution timer.
![Page 9: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/9.jpg)
2008/4/17 9CELF ELC 2008
Use case of TMU UIO driverUse case of TMU UIO driver
As a raw time counter for measurement– cf) pentium counter (rdtsc)
As a private periodic timer without dependence on tick precision
![Page 10: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/10.jpg)
2008/4/17 10CELF ELC 2008
Write a UIO kernel driver for Write a UIO kernel driver for SH TMUSH TMU
1. Setup and register an uio_info.2. Catch interrupts and do time-critical process in a
kernel interrupt handler.
![Page 11: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/11.jpg)
2008/4/17 11CELF ELC 2008
API of UIO (in kernel)API of UIO (in kernel)
struct uio_info– name: device name
– version: device driver version
– irq: interrupt number or UIO_IRQ_CUSTOM
– irq_flags: flags for request_irq()
– handler: device's irq handler (optional)• e.g. Make sure that the interrupt has been occurred by the
device.• e.g. Stop the interrupt
– mem[]: memory regions that can be mapped to user-space
![Page 12: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/12.jpg)
2008/4/17 12CELF ELC 2008
API of UIO (in kernel) (contd.)API of UIO (in kernel) (contd.)
struct uio_mem– addr: memory address
– size: size of memory
– memtype: type of memory region• UIO_MEM_PHYS
– I/O and physical memory
• UIO_MEM_LOGICAL– Logical memory (e.g. allocated by kmalloc())
• UIO_MEM_VIRTUAL– Virtual memory (e.g. allocated by vmalloc())
– internal_addr: another address for kernel driver internal use
• e.g. ioremap()-ed address
![Page 13: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/13.jpg)
2008/4/17 13CELF ELC 2008
Code of SH TMU UIO kernel Code of SH TMU UIO kernel driverdriver
info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
info>mem[0].size = TMU_012_SIZE;
info>mem[0].memtype = UIO_MEM_PHYS;
info>mem[0].addr = TMU_012_BASE; /* address of TMU registers */
info>name = "SH TMU2";
info>version = "0.01";
info>irq = TMU2_IRQ;
info>irq_flags = IRQF_DISABLED;
info>handler = sh_tmu_interrupt_handler;
uio_register_device(dev, info);
![Page 14: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/14.jpg)
2008/4/17 14CELF ELC 2008
Code of SH TMU UIO kernel Code of SH TMU UIO kernel driver (contd.)driver (contd.)
static irqreturn_t
sh_tmu_interrupt_handler(int irq, struct uio_info *dev_info)
{unsigned long timer_status;
timer_status = ctrl_inw(TMU2_TCR);timer_status &= ~0x100;ctrl_outw(timer_status, TMU2_TCR);
return IRQ_HANDLED;
}
![Page 15: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/15.jpg)
2008/4/17 15CELF ELC 2008
Write a UIO user driver for SH Write a UIO user driver for SH TMUTMU
1. Look for an appropriate UIO device2. Open the UIO device3. Mmap memory regions through the UIO device4. Initialize the device through the mmapped
memory regions.5. (Wait for interrupts by reading the UIO device.)6. (Handle the interrupts.)7. Input/output data through the mmapped memory
regions.
![Page 16: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/16.jpg)
2008/4/17 16CELF ELC 2008
API of UIO (in user-space)API of UIO (in user-space)
/sys/class/uio?/: information about device and UIO– name: UIO name
– version: UIO version
– maps/map?/: memory regions• addr: address of memory region
• size: region size
/dev/uio?: device access– read(): wait for interrupts
– mmap(): map device memory regions to user space• offset = region number * PAGESIZE
![Page 17: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/17.jpg)
2008/4/17 17CELF ELC 2008
Code of SH TMU UIO user Code of SH TMU UIO user driverdriver
fd = open(“/dev/uio0”, O_RDWR|O_SYNC);
/* Map device's registers into user memory */
/* fitting the memory area on pages */
offset = addr & ~PAGE_MASK;
addr = 0 /* region 0 */ * PAGE_SIZE;
size = (size + PAGE_SIZE 1) / PAGE_SIZE * PAGE_SIZE;
iomem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr);
iomem += offset;
![Page 18: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/18.jpg)
2008/4/17 18CELF ELC 2008
Code of SH TMU UIO user Code of SH TMU UIO user driver (contd.)driver (contd.)
/* Stop the counting */
*(u_char *)SH_TMU_TSTR(iomem) |= ~(TSTR_TSTR2);
...
/* Wait for an interrupt */;
read(fd, &n_pending, sizeof(u_long));
val = *(u_int *)SH_TMU2_TCNT(iomem);
...
/* Stop the TMU */
*(u_char *)SH_TMU_TSTR(iomem) &= ~(TSTR_TSTR2);
munmap(iomem, size);
close(fd);
![Page 19: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/19.jpg)
2008/4/17 19CELF ELC 2008
MeasurementMeasurement
Renesas RTS7751R2D board– SH7751R(SH-4 architecture) 240MHz– 256MB RAM– 5channels of TMU– NFS rootfs
Base software– Linux-2.6.25-rc9– UIO driver for SH TMU2
![Page 20: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/20.jpg)
2008/4/17 20CELF ELC 2008
UIO OverheadUIO Overhead
Latency in UIO interrupt handling (preliminary results)
– The latency depends on scheduler.• SCHED_FIFO, SCHED_RR priority• Realtime preemption patch (CONFIG_PREEMPT_RT)
Read the counter value with less overhead.
load MIN(us) MAX(us) AVERAGE(us)none 60 89 64ping f 60 199 147
104 203 126make vmlinux
![Page 21: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/21.jpg)
2008/4/17 21CELF ELC 2008
FYI: Backport for older FYI: Backport for older kernelskernels
Not so hard. small code (820 lines in uio.c and uio_driver.h) use a few legacy framework (interrupt handling,
device file, and sysfs) e.g. backport into 2.6.16
– Change arguments of interrupt handler– Replace device_create() and device_destroy()
invocations with appropriate code.
![Page 22: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/22.jpg)
2008/4/17 22CELF ELC 2008
FYI: Recent changesFYI: Recent changes
Some fixes (in 2.6.25)– Cache off in physical memory mapping
Another UIO driver (in the GregKH tree)– SMX Cryptengine
![Page 23: Using UIO in an embedded platform - eLinux.org](https://reader031.vdocuments.net/reader031/viewer/2022012422/6176796ef6ea0f0699755a60/html5/thumbnails/23.jpg)
2008/4/17 23CELF ELC 2008
ConclusionConclusion
UIO provides for user-space– Low overhead device access – Continuous physical memory allocation– Interrupt handling
UIO is useful for embedded systems– Minor or special device– Exclusive use– Make applications closer to device
I hope that after my presentation, you will want to try using UIO.