《Linux DRM Developer's Guide》学习笔记--DRM internals
The DRM layer provides several services to graphics drivers, many of them driven by the application interfaces it provides through libdrm, the library that wraps most of the DRM ioctls. These include vblank event handling, memory management, output management, framebuffer management, command submission & fencing, suspend/resume support, and DMA services.
Driver Initialization
At the core of every DRM driver is a struct drm_driver structure. Drivers typically statically initialize a drm_driver structure, and then pass it to drm_dev_alloc() to allocate a device instance. After the device instance is fully initialized it can be registered (which makes it accessible from userspace) using drm_dev_register().
A device instance for a drm driver is represented by struct drm_device. This is allocated with drm_dev_alloc(), usually from bus-specific ->:c:func:probe() callbacks implemented by the driver. The driver then needs to initialize all the various subsystems for the drm device like memory management, vblank handling, modesetting support and intial output configuration plus obviously initialize all the corresponding hardware bits. An important part of this is also calling drm_dev_set_unique() to set the userspace-visible unique name of this device instance. Finally when everything is up and running and ready for userspace the device instance can be published using drm_dev_register().
When cleaning up a device instance everything needs to be done in reverse: First unpublish the device instance with drm_dev_unregister(). Then clean up any other resources allocated at device initialization and drop the driver’s reference to drm_device using drm_dev_put().
int drm_dev_is_unplugged(struct drm_device * dev)
This function can be called to check whether a hotpluggable is unplugged. Unplugging itself is singalled through drm_dev_unplug(). If a device is unplugged, these two functions guarantee that any store before calling drm_dev_unplug() is visible to callers of this function after it completes
int drm_dev_init(struct drm_device * dev, struct drm_driver * driver, struct device * parent)
Initialize a new DRM device. No device registration is done. Call drm_dev_register() to advertice the device to user space and register it with other core subsystems. This should be done last in the device initialization sequence to make sure userspace can’t access an inconsistent state.
struct drm_device * drm_dev_alloc(struct drm_driver * driver, struct device * parent
Allocate and initialize a new DRM device. No device registration is done. Call drm_dev_register() to advertice the device to user space and register it with other core subsystems. This should be done last in the device initialization sequence to make sure userspace can’t access an inconsistent state
int drm_dev_register(struct drm_device * dev, unsigned long flags)
Register the DRM device dev with the system, advertise device to user-space and start normal device operation. dev must be allocated via drm_dev_alloc() previously.
Never call this twice on any device!
int drm_dev_set_unique(struct drm_device * dev, const char * name)
Sets the unique name of a DRM device using the specified string. Drivers can use this at driver probe time if the unique name of the devices they drive is static.
int drm_irq_install(struct drm_device * dev, int irq)
Initializes the IRQ related data. Installs the handler, calling the driver drm_driver.irq_preinstall and drm_driver.irq_postinstall functions before and after the installation
struct drm_file
DRM file private data
int drm_open(struct inode * inode, struct file * filp)
This function must be used by drivers as their file_operations.open method. It looks up the correct DRM device and instantiates all the per-file resources for it. It also calls the drm_driver.open driver callback.
ssize_t drm_read(struct file * filp, char __user * buffer, size_t count, loff_t * offset)
read method for DRM file
__poll_t drm_poll(struct file * filp, struct poll_table_struct * wait)
This function must be used by drivers as their file_operations.read method iff they use DRM events for asynchronous signalling to userspace. Since events are used by the KMS API for vblank and page flip completion this means all modern display drivers must use it.