linux devicedriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · eeprom device 4....
TRANSCRIPT
![Page 1: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/1.jpg)
Linux Device Drivers
Jie LiaoApril 7, 2016
1
![Page 2: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/2.jpg)
Adevicedriverisapieceofsoftwarethatallowstheoperatingsystemtocommunicatewiththedevices
2
![Page 3: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/3.jpg)
Roadmap
• Fundamentals• Basic device types• Bus controller/device drivers• Linux graphic stack• Universal drivers
3
![Page 4: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/4.jpg)
Physicaldevicetopology
CPU
PCI bus
USB bus
I2C bus
ALSAdevice
GPUdevice
USB Netdevice
EEPROMdevice
4
![Page 5: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/5.jpg)
Linux device model
• Unified model to represent devices and describedevice topology•Minimize code duplication• Devicedriversseparatedfromcontrollerdrivers• Hardwaredescriptionseparatedfromdriversthemselves• Device and driver binding
5
![Page 6: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/6.jpg)
struct device{
struct device*parent;struct device_private *p;
struct kobject kobj;
struct bus_type*bus;/*typeofbusdeviceison*/struct device_driver *driver;/*whichdriverhasallocatedthis device */
void*driver_data;/*Driverdata,setandgetwith dev_set/get_drvdata */
u64 *dma_mask;u64 coherent_dma_mask;
void(*release)(struct device*dev);/*some fields are evicted */…
}6
![Page 7: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/7.jpg)
struct device_driver{
const char*name;struct bus_type *bus;struct module *owner;const char*mod_name; /*used forbuilt-inmodules */bool suppress_bind_attrs; /*disables bind/unbind viasysfs */
const struct of_device_id *of_match_table;const struct acpi_device_id *acpi_match_table;
int (*probe) (struct device *dev);int (*remove) (struct device *dev);void (*shutdown) (struct device *dev);int (*suspend) (struct device *dev,pm_message_t state);int (*resume) (struct device *dev);const struct attribute_group **groups;
const struct dev_pm_ops *pm;struct driver_private *p;
}7
![Page 8: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/8.jpg)
struct bus_type{
const char*name;const char*dev_name;struct device *dev_root;
int (*match)(struct device *dev,struct device_driver *drv);int (*uevent)(struct device *dev,struct kobj_uevent_env *env);int (*probe)(struct device *dev);int (*remove)(struct device *dev);void (*shutdown)(struct device *dev);
int (*suspend)(struct device *dev,pm_message_t state);int (*resume)(struct device *dev);
const struct dev_pm_ops *pm;/*some fields are evicted */…
}8
![Page 9: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/9.jpg)
struct class{
const char*name;
int (*dev_uevent)(struct device*dev,struct kobj_uevent_env *env);char*(*devnode)(struct device*dev,umode_t *mode);
void(*class_release)(struct class*class);void(*dev_release)(struct device*dev);
int (*suspend)(struct device*dev,pm_message_t state);int (*resume)(struct device*dev);
const struct dev_pm_ops *pm;
struct subsys_private *p;…/*some fields are evicted */…
}
9
![Page 10: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/10.jpg)
PCI bus
USB busALSAdevice
GPUdevice
USB Netdevice
GPUdeviceinstance
USB Netdeviceinstance
usb_bus_type
ALSAdeviceinstance
PCI Adapter driver
USB Core
PCI devicedriverUSB adapter
driver
USB devicedriver
Network driver
PCI devicedriverALSA driver
PCI devicedriverGraphic driver
Soundclass
Graphicclass
Netclass
pci_bus_type
Genericbus driver
PCI Core
hardware
Kernelmodel
10
![Page 11: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/11.jpg)
Kobject & sysfs{
const char*name;
struct list_head entry;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
struct kernfs_node *sd;
struct kref kref;
/*some fields are evicted */
…
}
• Reference counting
• Sysfs representation
• Data structure glue
• Hotplug event handling
11
![Page 12: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/12.jpg)
Subsystem
• A top-level view to the system’s structure as a whole
• A view to the device model data structure ratherthan physical connection
12
![Page 13: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/13.jpg)
PCI busUSB busALSA
deviceGPUdevice
USB Netdevice
GPUdeviceinstance
USB Netdeviceinstance
usb_bus_type
ALSAdeviceinstance
PCI Adapter driver
USB Core
PCI devicedriverUSB adapter
driver
USB devicedriver
Network driver
PCI devicedriverALSA driver
PCI devicedriverGraphic driver
Soundclass
Graphicclass
Netclass
pci_bus_type
Genericbus driver
PCI Core
/sys/class/ /sys/devices/ /sys/bus/
kernel
hardware
user space sysfs
13
![Page 14: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/14.jpg)
PCI busUSB busALSA
deviceGPUdevice
USB Netdevice
usb_bus_type
PCI Adapter driver
USB Core
PCI devicedriverUSB adapter
driver
USB devicedriver
Network driver
PCI devicedriverALSA driver
PCI devicedriverGraphic driver
Soundclass
Graphicclass
Netclass
pci_bus_type
Genericbus driver
PCI Core
/sys/class/ /sys/devices/ /sys/bus/
hotplug
kernel
hardware
user space sysfs
uevent
udev/dev/
Graphicstack
Networkstack
System call interface
Soundstack
14
![Page 15: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/15.jpg)
PCI bus
PCI controller driver
netclass
PCI Core
/sys/class/ /sys/devices/ /sys/bus/
kernel
hardware
userspace
sysfs
Graphicstack
systemcall interface
FS/Blocklayer
TCP/IPstack
SCSI Core
GPUdeviceSCSI bus
SCSIdisk
USB bus
Networkdevice
SCSIcontrollerdriver USBcontrollerdriver
pci_bus_type
usb_bus_type
scsi_bus_type
USB Core
GPUdriver
Diskdriver
Networkdriver
graphicclass
blockclass
![Page 16: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/16.jpg)
Not a complete model
• Devices are wrapped into subsystem’s data structures• One device/driver could be registered to multiple subsystems• Multiple layers of frameworks between the driver and user space• Common frameworks extract common driver functionalities• etc.
15
![Page 17: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/17.jpg)
Memory-mapped I/O and port I/O
MMIO Registers
RAM
Physical address space, accessed usingnormal load/store instructions
PIO Registers
Separate address space, accessedusingspecific instructions
low
high
16
![Page 18: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/18.jpg)
Memory-mapped I/O and port I/O
struct resource *request_mem_region(start, len, name)
struct resource *request_region (start,len, name)
void __iomem *ioremap(phy_addr, size)
read[bwl]/write[bwl] in[bwl]/out[bwl]
MMIO PIO
17
![Page 19: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/19.jpg)
DMA
18http://free-electrons.com/doc/training/linux-kernel/linux-kernel-slides.pdf
![Page 20: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/20.jpg)
DMA memory mappingCoherent DMA mapping Streaming DMA mapping
• Exists for thewhole module life time• Kernel allocates the buffer• CPU can access the memory while
the mapping is valid• Set up could be expensive
• Usually used for oneDMA transfer• Buffer already allocated by the driver• CPU cannot access the memory while
the mapping is valid• Recommended solution
void *dma_alloc_coherent (dev, size, handle, gfp)
void *dma_map_single (dev, handle, size, dir)
DMA requires physical memory to be contiguous !
19
![Page 21: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/21.jpg)
Initiate a DMA transfer
Allocate a DMA channel
Set slave and controller config
Get a DMA descriptor
Submit the transaction
Issue pendingDMA request
struct dma_chan *dma_request_chan(dev,name)
int dmaengine_slave_config(chan, config)
struct dma_async_tx_descriptor*dmaengine_prep_slave_sg(
chan,sgl, sg_len,direction, flags);
dma_cookie_t dmaengine_submit(desc)
voiddma_async_issue_pending(chan);
20
![Page 22: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/22.jpg)
Interruptint devm_request_irq(
struct device*dev,unsignedint irq,irq_handler_t handler,unsignedlongirq_flags,const char*devname,void*dev_id);
voiddevm_free_irq(struct device*dev,unsignedint irq,void*dev_id);
21
![Page 23: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/23.jpg)
Interrupt Handling
• Top half• Should complete as soon as possible• Interrupt disabled• Take data out and invoke the bottomhalf
• Bottom half• Does the rest of the interrupt handling at a postponed time• Interrupt enabled• Implemented as softirqs, tasklets or workqueues
22
![Page 24: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/24.jpg)
Hotplug with udev
Plug/removedevice
Driver corenotified byinterrupt
Allocate resourcesMatch driver andprobe if possible Kobject
created
Udevdlistening
inotifySet rules in
/etc/udev/rules.d/
Rules in /lib/udev/rules.d/can override
Create a file in /devLoad modules when
necessary
uevent
23
![Page 25: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/25.jpg)
Basic device types
Type Char devices Block devices Network Devices
Transfer format Transfer data in characters Transfer data in blocks Transfer data in packets
User space control Standard system call via/dev/
Standard system call via/dev/
Socket API and ifconfig
Kernel space represent cdev/ char_device_struct1
gendisk net_device
Operation connection struct file_operations block_device_operations net_device_ops
Registration API cdev_init()/register_chrdev() register_blkdev() register_netdev()
24
![Page 26: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/26.jpg)
Block devices
25http://free-electrons.com/doc/block_drivers.pdf
![Page 27: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/27.jpg)
Network devices
Network stack layerstruct sk_buff
NIC driver
Bus infrastructure (PCI, USB…)
struct net_device
Bus driver instantiation
26
![Page 28: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/28.jpg)
USB
DeviceConfig
interface
interface
endpoint
endpoint
endpoint
endpoint
endpoint
endpoint
USBdevicedriver
USBdevicedriver
Upper layer software
USB device driver
USB core
USB host controller driver
struct usb_device ->struct device dev
struct usb_driver ->struct usbdrv_wrap ->
struct device_driver driver
27
![Page 29: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/29.jpg)
USB communication
USB host controller driver does theURB transfer and notifies USB core
USB device driver creates URB andpopulates the fields
USB device driver submitsthe URB to USB core
USB core submits the URB to theUSB host controller driver
USB core notifies USBdevice driver of the transfer
status
Submitted URBcould be canceled
28
![Page 30: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/30.jpg)
I2C
Processor
I2C controller(master)
I2C device(slave)
I2C core
Bus infrastructure (PCI, platform)
I2C device driver
Upper layer software
I2C controller driver
struct i2c_client ->struct device
struct i2c_driver ->struct device_driver driver
29
![Page 31: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/31.jpg)
I2C data transfer
• Only master can initiate transaction, slaves onlyrespond to the master
• i2c_master_send/recv(i2c_client, buf, count)
• I2c_transfer
30
![Page 32: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/32.jpg)
Platform bus
• Peripheral devices are usually connected to CPU on SoCs
• Legacy peripherals assume to be attached to a bus
• Pseudo bus to glue attached peripherals
31
![Page 33: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/33.jpg)
PCI
• Most common bus in PCs• Support various devices including graphics card,sound card, network card and bus controllers• Automatic device configuration through BIOS-typeboot firmware or kernel configuration
struct pci_dev -> struct device devStruct pci_driver -> struct device_driver driver
32
![Page 34: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/34.jpg)
PCI device has 256-byte configuration registermemory region
00:de10801207001000a10000031000800010:000000f20c0000e8000000000c0000f020:0000000001e0000000000000de10321030:000000f360000000000000000a010000
pci_[read |write]_config_[byte | word | dword]
33https://en.wikipedia.org/wiki/PCI_configuration_space
![Page 35: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/35.jpg)
PCI device has up to 6 MMIO or PIO regions
MMIO mapping or PIO mapping
unsignedlongpci_resource_[start|len|end|flags](struct pci_dev *pdev,int bar)
For generic steps to initialize PCI devices, refer to: http://free-electrons.com/kerneldoc/latest/PCI/pci.txt34
![Page 36: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/36.jpg)
Linux graphic stack
X11 application OpenGL application Framebuffer application
2D driver 3D driver
libdrm
DRM/KMS FBDEV
Graphic card driver
Bus infrastructrue
user space
kernel space
35
![Page 37: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/37.jpg)
Linux Framebuffer
http://free-electrons.com/pub/conferences/2014/elce/brezillon-drm-kms/brezillon-drm-kms.pdf 36
![Page 38: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/38.jpg)
DRM/KMS
http://free-electrons.com/pub/conferences/2014/elce/brezillon-drm-kms/brezillon-drm-kms.pdf 37
![Page 39: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/39.jpg)
Universal drivers
• Linux kernel has already extracted common code into frameworks foreach class and bus• PCI core, USB core, I2C core, serial core etc.• RPM framework, pctl framework, clock framework, mfd framework
• We need a data structure to describe the flow (bring up data-drienagain)
• Driver diversity and complexity are the challenges
38
![Page 40: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/40.jpg)
device users
device drivers abstraction
deviceknowledge
computationinternalstates
driver frameworks
devices
universal driver
device users
device drivers
devices
abstraction
management
computationinternalstates
driver frameworks
![Page 41: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/41.jpg)
UniversalDriver
universal driverregistration
universal deviceregistration
universaldrv_data list
universaldevice list
match
devicea
devicec
devicebdriverB
driverC
driverA
Bus cores
v universal probev universal reg read/writev universal suspend/resume
![Page 42: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/42.jpg)
Backup slides start from this slide…
39
![Page 43: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/43.jpg)
Frameworks work with Linux device model
40http://free-electrons.com/doc/kernel-architecture.pdf
![Page 44: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/44.jpg)
Physical / virtual memory mapping
Kernel
Process n
0xFFFFFFFF
0xC0000000
0x00000000
RAM
ZONE_HIGH
ZONE_NORMAL
ZONE_DMA
I/O Memory
41
![Page 45: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/45.jpg)
64-bit x86 machine memory mapping
• Virtualmemorymapwith4levelpagetables:
• 0000000000000000 - 00007fffffffffff(=47bits)userspace,differentpermm
• holecausedby[48:63]signextension
• ffff800000000000- ffff87ffffffffff(=43bits)guardhole,reservedforhypervisor
• ffff880000000000- ffffc7ffffffffff(=64TB)directmappingofallphys.memory
• ffffc80000000000- ffffc8ffffffffff(=40bits)hole
• ffffc90000000000- ffffe8ffffffffff(=45bits)vmalloc/ioremapspace
• ffffe90000000000- ffffe9ffffffffff(=40bits)hole
• ffffea0000000000- ffffeaffffffffff (=40bits)virtualmemorymap(1TB)
• ...unusedhole...
• ffffec0000000000- fffffc0000000000(=44bits)kasan shadowmemory(16TB)
• ...unusedhole...
• ffffff0000000000- ffffff7fffffffff(=39bits)%esp fixup stacks
• ...unusedhole...
• ffffffff80000000- ffffffffa0000000(=512MB)kerneltextmapping,fromphys 0
• ffffffffa0000000- ffffffffff5fffff(=1525MB)modulemappingspace
• ffffffffff600000- ffffffffffdfffff (=8MB)vsyscalls
• ffffffffffe00000- ffffffffffffffff (=2MB)unusedhole
42
![Page 46: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/46.jpg)
mmap
• Map (device) file content to part of the virtualaddress space of the process• Avoid expensive read/write system calls to accessI/O memory or ports• Supported by the MMU hardware (device drivermust havemmap operation defined)
43
![Page 47: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/47.jpg)
struct dma_slave_config {enum dma_transfer_directiondirection;dma_addr_t src_addr;dma_addr_t dst_addr;enum dma_slave_buswidth src_addr_width;enum dma_slave_buswidth dst_addr_width;u32src_maxburst;u32dst_maxburst;bool device_fc;unsignedint slave_id;
}
struct dma_async_tx_descriptor {dma_cookie_t cookie;enum dma_ctrl_flags flags;/*nota'long'topackwith
cookie*/dma_addr_t phys;struct dma_chan *chan;dma_cookie_t (*tx_submit)(struct
dma_async_tx_descriptor *tx);dma_async_tx_callback callback;void*callback_param;
#ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCHstruct dma_async_tx_descriptor *next;struct dma_async_tx_descriptor *parent;spinlock_t lock;
#endif}
44
![Page 48: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/48.jpg)
Windows device driver architecture
• PlugandPlay(PnP)Device Tree• Deviceobjectanddriverpairscomposedevicestack• PnPmanagernotifyadeviceordriverattachment
45
![Page 49: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/49.jpg)
Windows device driver architecture
46https://msdn.microsoft.com/en-us/library/windows/hardware/ff554721(v=vs.85).aspx
![Page 50: Linux DeviceDriversjl98.web.rice.edu/docs/resources/linux-device-driver.pdf · EEPROM device 4. Linux devicemodel •Unifiedmodeltorepresentdevicesand describe ... user space sysfs](https://reader030.vdocument.in/reader030/viewer/2022021502/5aecb41a7f8b9a66258eea3b/html5/thumbnails/50.jpg)
ApplicationsaccessdevicesinMacOSX
• I/OKitfamilyAPIs• POSIXAPIs,which provides access tostorage,networkandserial• I/ORegistry,which is a databasefordeviceproperties
47https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/KernelProgramming/Architecture/Architecture.html