Drm -- Memory Management

Introduction DRM 核心包括两个内存管理器,分别是 Translation Table Manager(TTM) 和 Graphics Execution Manager(GEM). TTM 是第一个被开发的 DRM 内存管理器。它提供了一个单一的用户空间 API 来满足所有硬件的需求,支持统一内存体系结构(Unified Memory Architecture,UMA)设备和具有专用视频 RAM (即大多数离散显卡)的设备。这导致了一大段复杂的代码,结果很难用于驱动程序开发。 由于 TTM 的复杂性,GEM 最初是由英特尔(Intel)赞助的一个项目。GEM 没有提供每个图形内存相关问题的解决方案,而是确定了驱动程序之间的公共代码,并创建了一个支持库来共享它。GEM 的初始化和执行要求比 TTM 简单,但没有视频 RAM 管理功能,因此仅限于 UMA 设备。 GEM Initialization 在 struct drm_driver driver feature 中设置 DRIVER_GEM bit. 中间层会自动调用drm_gem_init()来完成 GEM 的初始化. GEM Objects Creation GEM object 由结构体 struct drm_gem_object表示, 通过drm_gem_oject_init()初始化, 利用 shmem 来分配 anonymous pageable memory. 如果 hardware 需要 physical contiguous system memory(通常是嵌入式设备需求), 那么可以不需要使用 shmem, 而是通过drm_gem_private_object_init()初始化....

2024-12-10 · 3 min

Drm -- Panel and Bridge

Panel and Bridge 情况 1:设备树存在 panel 节点 imx6ull-dhcom-pdk2.dts 中的存在 panel 节点,对应 panel-simple.c: &lcdif { status = "okay"; port { display_out: endpoint { remote-endpoint = <&panel_in>; }; }; }; panel { compatible = "auo,g101evn010"; power-supply = <&ldo4_ext>; backlight = <&lcd_backlight>; port { panel_in: endpoint { remote-endpoint = <&display_out>; }; }; }; 这种情况比较简单,在底层 driver 中调用drm_of_find_panel_or_bridge找到设备树中的 panel 节点,和 panel driver 匹配,找到 panel driver 注册的 drm_panel 结构体。 再调用devm_drm_panel_bridge_add, 分配 panel_bridge, 注册一个固定的drm_bridge。 最后调用drm_bridge_attach, 调用到 bridge->funcs->attach, 即 panel_bridge_attach, 注册 connector,以及把 connector attach 到 encoder。...

2024-10-15 · 4 min

Debug DRM driver in QEMU with Buildroot support

Buildroot 编译 kernel 和 rootfs 我们想在 QEMU 中进行 debug,那么首先需要准备好 kernel image 和 rootfs。这边我们采用的是 buildroot 的方式来创建我们需要的 kernel 和 rootfs。 下载 buildroot 2024: wget https://buildroot.org/downloads/buildroot-2024.02.6.tar.gz 接着进入 buildroot 目录,执行 make qemu_x86_64_defconfig,使用 qemu 的配置: cd buildroot-2024.02.6/ make qemu_x86_64_defconfig 对该 config 文件作如下改动: # support custom linux source code BR2_PACKAGE_OVERRIDE_FILE="board/qemu/x86_64/custom_override.mk" BR2_LINUX_KERNEL_CUSTOM_VERSION=y BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="linux-6.10" BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/x86_64/custom_linux.config" # GDB support BR2_PACKAGE_HOST_GDB=y # cross gdb for host machine BR2_PACKAGE_GDB=y BR2_PACKAGE_GDB_SERVER=y BR2_TOOLCHAIN_BUILDROOT_CXX=y BR2_DEBUG_3=y BR2_ENABLE_DEBUG=y # enable debug symbol in packages BR2_OPTIMIZE_0=y BR2_PACKAGE_LIBDRM=y BR2_PACKAGE_DRMTEST=y # custom drm test package 改动目的主要是:...

2024-09-26 · 2 min

Drm -- Encoder

struct drm_encoder_funcs { void (*reset)(struct drm_encoder *encoder); void (*destroy)(struct drm_encoder *encoder); int (*late_register)(struct drm_encoder *encoder); void (*early_unregister)(struct drm_encoder *encoder); void (*debugfs_init)(struct drm_encoder *encoder, struct dentry *root); }; reset: reset encoder destroy: drm_encoder_cleanup late_register: 和 crtc 相关接口类似 early_unregister: 同上 debugfs_init: 注册 debugfs_init struct drm_encoder { struct drm_device *dev; struct list_head head; struct drm_mode_object base; char *name; int encoder_type; /// DRM_MODE_ENCODER_<foo> unsigned index; uint32_t possible_crtcs; uint32_t possible_clones; struct drm_crtc *crtc; struct list_head bridge_chain; const struct drm_encoder_funcs *funcs; const struct drm_encoder_helper_funcs *helper_private; struct dentry *debugfs_entry; }; struct drm_encoder_helper_funcs { void (*dpms)(struct drm_encoder *encoder, int mode); enum drm_mode_status (*mode_valid)(struct drm_encoder *crtc, const struct drm_display_mode *mode); bool (*mode_fixup)(struct drm_encoder *encoder, const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode); void (*prepare)(struct drm_encoder *encoder); void (*commit)(struct drm_encoder *encoder); void (*mode_set)(struct drm_encoder *encoder, struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode); void (*atomic_mode_set)(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state, struct drm_connector_state *conn_state); enum drm_connector_status (*detect)(struct drm_encoder *encoder, struct drm_connector *connector); void (*atomic_disable)(struct drm_encoder *encoder, struct drm_atomic_state *state); void (*atomic_enable)(struct drm_encoder *encoder, struct drm_atomic_state *state); void (*disable)(struct drm_encoder *encoder); void (*enable)(struct drm_encoder *encoder); int (*atomic_check)(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state, struct drm_connector_state *conn_state); }; dpms: optional, legacy support....

2024-09-18 · 1 min

Drm -- Connector

Data Structure struct drm_connector { struct drm_device *dev; struct device *kdev; struct device_attribute *attr; struct fwnode_handle *fwnode; struct list_head head; struct list_head global_connector_list_entry; struct drm_mode_object base; char *name; struct mutex mutex; unsigned index; int connector_type; /// DRM_MODE_CONNECTOR_<foo> int connector_type_id; /// index into connector type enum bool interlace_allowed; bool doublescan_allowed; bool stereo_allowed; bool ycbcr_420_allowed; enum drm_connector_registration_state registration_state; struct list_head modes; enum drm_connector_status status; struct list_head probed_modes; struct drm_display_info display_info; const struct drm_connector_funcs *funcs; struct drm_property_blob *edid_blob_ptr; struct drm_object_properties properties; struct drm_property *scaling_mode_property; struct drm_property *vrr_capable_property; struct drm_property *colorspace_property; struct drm_property_blob *path_blob_ptr; struct drm_property *max_bpc_property; struct drm_privacy_screen *privacy_screen; struct notifier_block privacy_screen_notifier; struct drm_property *privacy_screen_sw_state_property; struct drm_property *privacy_screen_hw_state_property; uint8_t polled; int dpms; const struct drm_connector_helper_funcs *helper_private; struct drm_cmdline_mode cmdline_mode; enum drm_connector_force force; const struct drm_edid *edid_override; struct mutex edid_override_mutex; u64 epoch_counter; u32 possible_encoders; struct drm_encoder *encoder; #define MAX_ELD_BYTES 128 uint8_t eld[MAX_ELD_BYTES]; bool latency_present[2]; int video_latency[2]; int audio_latency[2]; struct i2c_adapter *ddc; int null_edid_counter; unsigned bad_edid_counter; bool edid_corrupt; u8 real_edid_checksum; struct dentry *debugfs_entry; struct drm_connector_state *state; struct drm_property_blob *tile_blob_ptr; bool has_tile; struct drm_tile_group *tile_group; bool tile_is_single_monitor; uint8_t num_h_tile, num_v_tile; uint8_t tile_h_loc, tile_v_loc; uint16_t tile_h_size, tile_v_size; struct llist_node free_node; struct hdr_sink_metadata hdr_sink_metadata; }; polled: 有三个宏,DRM_CONNECTOR_POLL_HPD: connector 能主动 detect 到 hotplug 并发出 hotplug event。 DRM_CONNECTOR_POLL_CONNECT:需要 polling 是否发生了 connect。DRM_CONNECTOR_POLL_DISCONNECT:需要 polling 是否发生了 disconnect。如果 polled 设置为 0,表示不支持检测 connection status 变化。...

2024-09-18 · 3 min

Drm -- CRTC

数据结构 drm_crtc struct drm_crtc { struct drm_device *dev; struct device_node *port; struct list_head head; char *name; struct drm_modeset_lock mutex; struct drm_mode_object base; struct drm_plane *primary; // for legacy IOCTL struct drm_plane *cursor; // for legacy IOCTL unsigned index; int cursor_x; // legacy int cursor_y; // legacy bool enabled; // legacy struct drm_display_mode mode; // legacy struct drm_display_mode hwmode; // legacy int x; // legacy int y; // legacy const struct drm_crtc_funcs *funcs; uint32_t gamma_size; // legacy uint16_t *gamma_store; // legacy const struct drm_crtc_helper_funcs *helper_private; struct drm_object_properties properties; struct drm_property *scaling_filter_property; struct drm_crtc_state *state; struct list_head commit_list; spinlock_t commit_lock; struct dentry *debugfs_entry; struct drm_crtc_crc crc; unsigned int fence_context; spinlock_t fence_lock; unsigned long fence_seqno; char timeline_name[32]; struct drm_self_refresh_data *self_refresh_data; }; drm_crtc_state struct drm_crtc_state { struct drm_crtc *crtc; bool enable; bool active; bool planes_changed : 1; bool mode_changed : 1; bool active_changed : 1; bool connectors_changed : 1; bool zpos_changed : 1; bool color_mgmt_changed : 1; bool no_vblank : 1; u32 plane_mask; u32 connector_mask; u32 encoder_mask; struct drm_display_mode adjusted_mode; struct drm_display_mode mode; struct drm_property_blob *mode_blob; struct drm_property_blob *degamma_lut; struct drm_property_blob *ctm; struct drm_property_blob *gamma_lut; u32 target_vblank; bool async_flip; bool vrr_enabled; bool self_refresh_active; enum drm_scaling_filter scaling_filter; struct drm_pending_vblank_event *event; struct drm_crtc_commit *commit; struct drm_atomic_state *state; }; enable: gate all other state。控制 crtc 是否需要 enable,控制 resource assignment...

2024-09-04 · 5 min

DRM -- Object and Property

Property CRTCs, planes, connectors 都有各自的 properties(字符串到值的映射)。Userspace 通过设置这些 properties,即可完成对显示参数的设置。 目前只有 CRTCs, planes, connectors 三者有 properties,因此 Userspace 只能对该三者的 properties 进行设置。 DRM 中定义了一系列 standard properties,这些 properties 在每个平台上都会创建,比如 connector 的 standard properties 会通过 drm_connector_create_standard_properties() 在 connector init 过程中自动创建,其他还有 specific 的 properties 需要底层 driver 调用特定的函数来创建,比如 drm_mode_create_dvi_i_properties() 可以创建 select subconnector property。 standard property 保存在 drm_device->mode_config 中,specific property 需要调用各自的创建函数来创建,保存在 drm_crtc/connector/plane 中。 struct drm_property { struct list_head head; // property链表 struct drm_mode_object base; uint32_t flags; char name[DRM_PROP_NAME_LEN]; // property名称 uint32_t num_values; uint64_t *values; struct drm_device *dev; struct list_head enum_list; }; flags: property flags,需要是以下选项之一:...

2024-09-02 · 3 min

Drm -- KMS Core

数据结构 drm_mode_config_funcs mode setting functions, 该结构体需要在底层 driver 中初始化。 struct drm_mode_config_funcs { struct drm_framebuffer *(*fb_create)(struct drm_device *dev, struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd); const struct drm_format_info *(*get_format_info)(const struct drm_mode_fb_cmd2 *mode_cmd); void (*output_poll_changed)(struct drm_device *dev); enum drm_mode_status (*mode_valid)(struct drm_device *dev, const struct drm_display_mode *mode); int (*atomic_check)(struct drm_device *dev, struct drm_atomic_state *state); int (*atomic_commit)(struct drm_device *dev,struct drm_atomic_state *state, bool nonblock); struct drm_atomic_state *(*atomic_state_alloc)(struct drm_device *dev); void (*atomic_state_clear)(struct drm_atomic_state *state); void (*atomic_state_free)(struct drm_atomic_state *state); }; fb_create: 创建 framebuffer 的回调函数,在 userspace 调用 drmModeAddFB2()之后会调用到。....

2024-08-28 · 3 min

DRM -- VBlank

Overview 一篇关于垂直同步 V-Sync 解释的文章:https://daily.elepover.com/2021/03/27/vsync/index.html 当 GPU 渲染的速度 > 显示器刷新的速度时,GPU 在显示器还来不及完成渲染完一帧时,就切换了 framebuffer,会导致出现撕裂现象。 帧率大于显示器刷新率时,启用垂直同步。 帧率小于显示器刷新率时,禁用垂直同步。 为了支持 vblank,底层 drvier 需要调用 drm_vblank_init()初始化,另外需要实现 drm_crtc_funcs.enable_vblank 和 drm_crtc_funcs.disable_vblank 两个回调函数,并且在 vblank 中断中调用 drm_crtc_handle_vblank()。 数据结构 struct drm_vblank_crtc { struct drm_device *dev; wait_queue_head_t queue; struct timer_list disable_timer; seqlock_t seqlock; atomic64_t count; ktime_t time; atomic_t refcount; u32 last; u32 max_vblank_count; unsigned int inmodeset; unsigned int pipe; int framedur_ns; int linedur_ns; struct drm_display_mode hwmode; bool enabled; struct kthread_worker *worker; struct list_head pending_work; wait_queue_head_t work_wait_queue; }; pipe: 表示第几个 crtc,在 drm_vblank_init 中初始化。...

2024-08-28 · 1 min

DRM -- Plane

数据结构 struct drm_plane{ } struct drm_plane_state { struct drm_plane *plane; // backpointer指向plane struct drm_crtc *crtc; // 通过drm_atomic_set_crtc_for_plane绑定的crtc struct drm_framebuffer *fb; // 通过drm_atomic_set_fb_for_plane绑定的fb struct dma_fence *fence; int32_t crtc_x; int32_t crtc_y; uint32_t crtc_w, crtc_h; uint32_t src_x; uint32_t src_y; uint32_t src_h, src_w; int32_t hotspot_x, hotspot_y; u16 alpha; uint16_t pixel_blend_mode; unsigned int rotation; unsigned int zpos; unsigned int normalized_zpos; enum drm_color_encoding color_encoding; enum drm_color_range color_range; struct drm_property_blob *fb_damage_clips; bool ignore_damage_clips; struct drm_rect src, dst; bool visible; enum drm_scaling_filter scaling_filter; struct drm_crtc_commit *commit; struct drm_atomic_state *state; bool color_mgmt_changed : 1; }; crtc_x/y/w/h: 设置该 plane 可见区域的起始位置和大小。...

2024-08-28 · 3 min