注:文章都是通过阅读各位前辈总结的资料 Android 11.0 && Linux(Kernel 4.19)Rockchip平台源码、加上自己的思考分析总结出来的,其中难免有理解不对的地方,欢迎大家批评指正。文章为个人学习、研究、欣赏之用,图文内容整理自互联网,如有侵权,请联系删除(◕‿◕),转载请注明出处(©Rockchip ©Android @Linux 版权所有),谢谢。

(==文章基于 Kernel-4.19==)&&(==文章基于 Android 11.0==)

【zhoujinjian.com博客原图链接】

【开发板 RockPi4bPlusV1.6】

【开发板 RockPi4bPlusV1.6 Android 11.0 && Linux(Kernel 4.19)源码链接】:(repo init -u https://github.com/radxa/manifests.git -b Android11_Radxa_rk11.1 -m rockchip-r-release.xml)

【开发板 RockPi4bPlusV1.6 Android 11.0 && Linux(Kernel 4.19)编译指南】

正是由于前人(各位大神)的分析和总结,帮助我节约了大量的时间和精力,特别感谢,由于不喜欢图片水印,去除了水印,敬请谅解!!!

本文转自Rockchip RK3399 - DRM crtc基础知识,如有侵权,请联系删除。


开发板 :ROCK Pi 4B+开发板
eMMC32GB
LPDDR44GB
显示屏 :7英寸HDMI接口显示屏
u-boot2017.09
linux4.19


一、LCD硬件原理

1.1 CRT介绍

CRT是阴极射线管(Cathode Ray Tube)的缩写,它是一种使用电子束在荧光屏上创建图像的显示设备。CRT显示器在过去很长一段时间内是主流的显示技术,现已被液晶显示屏(LCDLiquid Crystal Display)或其他新兴技术所替代。

CRT显示器中,扫描电子束从左到右、从上到下移动,照亮屏幕上的荧光点,从而创建图像。电子束每秒多次扫描整个屏幕,产生闪烁效果,需要与正在显示的内容同步。

随着LCDLEDOLED等新型显示技术的出现,底层原理已经发生了变化;

  • LCDLCD的构造是在两片平行的玻璃当中放置液态的晶体,两片玻璃中间有许多垂直和水平的细小电线,透过通电与否来控制杆状水晶分子改变方向,将光线折射出来产生画面; LCD通常需要背光源,例如冷阴极荧光灯(CCFL)或LED背光,以照亮液晶面板;这些背光源位于液晶面板的后面,因此LCD显示屏通常较厚;
  • LEDLED是一种通过控制半导体发光二极管的显示方式,其大概的样子就是由很多个通常是红色的发光二极管组成,靠灯的亮灭来显示字符,用来显示文字、图形、图像、动画、行情、视频、录像信号等各种信息的显示屏幕;LED显示屏的LED作为自身的背光源,这使得LED显示屏可以更薄,并且能效更高;
  • OLEDOLED显示屏使用有机发光二极管(OLED),每个像素都能发光,因此无需背光源;每个像素可以独立控制,实现高对比度和色彩鲜艳;

这些显示器不再使用扫描电子束,而是使用可以单独控制发光或阻挡光的像素矩阵。这使得图像更加清晰,刷新率更快,能源效率也得到了提高;

然而,刷新显示的基本概念并没有发生很大变化。显示内容仍然通过向每个像素发送信号来控制其亮度和颜色来进行更新。这些信号由显示控制器根据输入视频信号生成,并且它们确定屏幕上显示的内容。

1.2 LCD示意图

下图是LCD显示器示意图,里面的每个点就是一个像素点。它里面有一个电子枪,一边移动,一边发出各种颜色的光。用动态图表示如下:

img

电子枪是如何移动的?

  • 有一条Pixel Clock时钟线与LCD相连,每发出一次Pixel Clock,电子枪就移动一个像素。

颜色如何确定?

  • 由连接LCD的三组线RGB三原色混合而成:R(Red)、G(Green)、B(Blue)确定。

电子枪如何得知应跳到下一行?

  • 有一条HSYNC信号线与LCD相连,每发出一次脉冲(高低电平),电子枪就跳到下一行,该信号叫做行同步信号。

电子枪如何得知应跳到原点?

  • 有一条VSYNC信号线与LCD相连,每发出一次脉冲(高低电平),电子枪就跳到原点,该信号叫做帧同步信号。

RGB线上的数据从何而来?

  • 内存里面划分一块显存(frameBuffer),里面存放了要显示的数据,LCD控制器从里面将数据读出来,通过显示接口(比如mipilvdshdmiedp、``dp`)传给电子枪,电子枪再依次打到显示屏上。

img

因此要想在显示设备上显示图像,至少需要3个信号:

  • Pixel Clock:时钟信号,在每个时钟周期更新屏幕上一个像素;
  • HSYNC:水平同步信号,引脚每发出一个脉冲,表示一行的数据开始发送;
  • VSYNC:垂直同步信号,引脚每发出一个脉冲,表示一帧的数据开始发送。

1.3 时序参数

在上图中我们发现除了Pixel ClockHSYNCVSYNC信号外,还包含了大量的时序参数,这里我们一一介绍:

  • HBPDhback porch):行同步信号的后肩,单位为1个Pixel Clock时钟周期;
  • HFPDhfront porch):行同步信号的前肩,单位为1个Pixel Clock时钟周期;
  • HSPWhsync pulse):行同步信号的脉宽,单位为1个Pixel Clock时钟周期;
  • HOZVALhdisplay):LCD的水平宽度;
  • VBPDvback porch):帧同步信号的后肩,单位为1个HSYNC时钟周期;
  • VFPDvfront porch):帧同步信号的前肩,单位为1个HSYNC时钟周期;
  • VSPWvsync pulse):帧同步信号的脉宽,单位为1个HSYNC时钟周期;
  • LINEVALvdisplay):LCD的垂直宽度;

如果这部分内容看不到,可以参考我之前写的这篇文章:Mini2440裸机开发之LCD基础

二、crtc数据结构

DRM框架中,crtcframebuffer中读取待显示的图像,并按照响应的格式输出给encoder,其主要承担的作用为:

  • 配置适合显示的显示模式、分辨率、刷新率等参数,并输出相应的时序;
  • 扫描framebuffer发送到一个或多个显示器;
  • 更新framebuffer

概括下就是,对显示器进行扫描,产生时序信号的模块、负责帧切换、电源控制、色彩调整等等。

2.1 struct drm_crtc

linux内核使用struct drm_crtc来表示一个crtc控制器,包括显示模式、分辨率、刷新率等参数。定义在include/drm/drm_crtc.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* struct drm_crtc - central CRTC control structure
*
* Each CRTC may have one or more connectors associated with it. This structure
* allows the CRTC to be controlled.
*/
struct drm_crtc {
/** @dev: parent DRM device */
struct drm_device *dev;
/** @port: OF node used by drm_of_find_possible_crtcs(). */
struct device_node *port;
/**
* @head:
*
* List of all CRTCs on @dev, linked from &drm_mode_config.crtc_list.
* Invariant over the lifetime of @dev and therefore does not need
* locking.
*/
struct list_head head;

/** @name: human readable name, can be overwritten by the driver */
char *name;

/**
* @mutex:
*
* This provides a read lock for the overall CRTC state (mode, dpms
* state, ...) and a write lock for everything which can be update
* without a full modeset (fb, cursor data, CRTC properties ...). A full
* modeset also need to grab &drm_mode_config.connection_mutex.
*
* For atomic drivers specifically this protects @state.
*/
struct drm_modeset_lock mutex;

/** @base: base KMS object for ID tracking etc. */
struct drm_mode_object base;

/**
* @primary:
* Primary plane for this CRTC. Note that this is only
* relevant for legacy IOCTL, it specifies the plane implicitly used by
* the SETCRTC and PAGE_FLIP IOCTLs. It does not have any significance
* beyond that.
*/
struct drm_plane *primary;

/**
* @cursor:
* Cursor plane for this CRTC. Note that this is only relevant for
* legacy IOCTL, it specifies the plane implicitly used by the SETCURSOR
* and SETCURSOR2 IOCTLs. It does not have any significance
* beyond that.
*/
struct drm_plane *cursor;
/**
* @index: Position inside the mode_config.list, can be used as an array
* index. It is invariant over the lifetime of the CRTC.
*/
unsigned index;

/**
* @cursor_x: Current x position of the cursor, used for universal
* cursor planes because the SETCURSOR IOCTL only can update the
* framebuffer without supplying the coordinates. Drivers should not use
* this directly, atomic drivers should look at &drm_plane_state.crtc_x
* of the cursor plane instead.
*/
int cursor_x;
/**
* @cursor_y: Current y position of the cursor, used for universal
* cursor planes because the SETCURSOR IOCTL only can update the
* framebuffer without supplying the coordinates. Drivers should not use
* this directly, atomic drivers should look at &drm_plane_state.crtc_y
* of the cursor plane instead.
*/
int cursor_y;

/**
* @enabled:
*
* Is this CRTC enabled? Should only be used by legacy drivers, atomic
* drivers should instead consult &drm_crtc_state.enable and
* &drm_crtc_state.active. Atomic drivers can update this by calling
* drm_atomic_helper_update_legacy_modeset_state().
*/
bool enabled;

/**
* @mode:
*
* Current mode timings. Should only be used by legacy drivers, atomic
* drivers should instead consult &drm_crtc_state.mode. Atomic drivers
* can update this by calling
* drm_atomic_helper_update_legacy_modeset_state().
*/
struct drm_display_mode mode;
/**
* @hwmode:
*
* Programmed mode in hw, after adjustments for encoders, crtc, panel
* scaling etc. Should only be used by legacy drivers, for high
* precision vblank timestamps in
* drm_crtc_vblank_helper_get_vblank_timestamp().
*
* Note that atomic drivers should not use this, but instead use
* &drm_crtc_state.adjusted_mode. And for high-precision timestamps
* drm_crtc_vblank_helper_get_vblank_timestamp() used
* &drm_vblank_crtc.hwmode,
* which is filled out by calling drm_calc_timestamping_constants().
*/
struct drm_display_mode hwmode;

/**
* @x:
* x position on screen. Should only be used by legacy drivers, atomic
* drivers should look at &drm_plane_state.crtc_x of the primary plane
* instead. Updated by calling
* drm_atomic_helper_update_legacy_modeset_state().
*/
int x;
/**
* @y:
* y position on screen. Should only be used by legacy drivers, atomic
* drivers should look at &drm_plane_state.crtc_y of the primary plane
* instead. Updated by calling
* drm_atomic_helper_update_legacy_modeset_state().
*/
int y;

/** @funcs: CRTC control functions */
const struct drm_crtc_funcs *funcs;

/**
* @gamma_size: Size of legacy gamma ramp reported to userspace. Set up
* by calling drm_mode_crtc_set_gamma_size().
*
* Note that atomic drivers need to instead use
* &drm_crtc_state.gamma_lut. See drm_crtc_enable_color_mgmt().
*/
uint32_t gamma_size;

/**
* @gamma_store: Gamma ramp values used by the legacy SETGAMMA and
* GETGAMMA IOCTls. Set up by calling drm_mode_crtc_set_gamma_size().
*
* Note that atomic drivers need to instead use
* &drm_crtc_state.gamma_lut. See drm_crtc_enable_color_mgmt().
*/
uint16_t *gamma_store;
/** @helper_private: mid-layer private data */
const struct drm_crtc_helper_funcs *helper_private;

/** @properties: property tracking for this CRTC */
struct drm_object_properties properties;

/**
* @scaling_filter_property: property to apply a particular filter while
* scaling.
*/
struct drm_property *scaling_filter_property;

/**
* @state:
*
* Current atomic state for this CRTC.
*
* This is protected by @mutex. Note that nonblocking atomic commits
* access the current CRTC state without taking locks. Either by going
* through the &struct drm_atomic_state pointers, see
* for_each_oldnew_crtc_in_state(), for_each_old_crtc_in_state() and
* for_each_new_crtc_in_state(). Or through careful ordering of atomic
* commit operations as implemented in the atomic helpers, see
* &struct drm_crtc_commit.
*/
struct drm_crtc_state *state;

/**
* @commit_list:
*
* List of &drm_crtc_commit structures tracking pending commits.
* Protected by @commit_lock. This list holds its own full reference,
* as does the ongoing commit.
*
* "Note that the commit for a state change is also tracked in
* &drm_crtc_state.commit. For accessing the immediately preceding
* commit in an atomic update it is recommended to just use that
* pointer in the old CRTC state, since accessing that doesn't need
* any locking or list-walking. @commit_list should only be used to
* stall for framebuffer cleanup that's signalled through
* &drm_crtc_commit.cleanup_done."
*/
struct list_head commit_list;

/**
* @commit_lock:
*
* Spinlock to protect @commit_list.
*/
spinlock_t commit_lock;
/**
* @debugfs_entry:
*
* Debugfs directory for this CRTC.
*/
struct dentry *debugfs_entry;

/**
* @crc:
*
* Configuration settings of CRC capture.
*/
struct drm_crtc_crc crc;

/**
* @fence_context:
*
* timeline context used for fence operations.
*/
unsigned int fence_context;

/**
* @fence_lock:
*
* spinlock to protect the fences in the fence_context.
*/
spinlock_t fence_lock;
/**
* @fence_seqno:
*
* Seqno variable used as monotonic counter for the fences
* created on the CRTC's timeline.
*/
unsigned long fence_seqno;

/**
* @timeline_name:
*
* The name of the CRTC's fence timeline.
*/
char timeline_name[32];

/**
* @self_refresh_data: Holds the state for the self refresh helpers
*
* Initialized via drm_self_refresh_helper_init().
*/
struct drm_self_refresh_data *self_refresh_data;
};

该结构体包含以下成员变量:

  • dev:该crtc所属的DRM设备;
  • port:设备节点,被drm_of_find_possible_crtcs使用;
  • head:链表节点,用于将当前节点添加到&drm_mode_config.crtc_list链表;
  • name:名称,可以被驱动程序覆盖;
  • mutex:用于保护crtc状态的互斥锁;
  • basestruct drm_mode_object
  • primary:与该crtc相关的primary plane
  • cursor:与该crtc相关的cursor plane
  • index:在mode_config.list中的位置,可以用作数组索引;
  • cursor_x:光标的x坐标;
  • cursor_y:光标的y坐标;
  • enabled:指示该crtc是否已启用;
  • mode:当前模式时序信息;
  • hwmode:硬件中编程的模式;
  • x:在屏幕上的x坐标;
  • y:在屏幕上的y坐标;
  • funcscrtc控制函数;
  • gamma_size:报告给用户空间的legacy gamma ramp大小;
  • gamma_storelegacy gamma values数据存储区域;
  • helper_private:中间层私有数据;
  • properties:跟踪该crtc的属性;
  • scaling_filter_property:应用于缩放时的特定滤镜的属性;
  • state:当前crtc的原子状态;
  • commit_list:跟踪挂起提交的列表;
  • commit_lock:保护commit_list的自旋锁;
  • debugfs_entry:用于调试的目录条目;
  • crccrc捕获的配置设置;
  • fence_context:用于围栏操作的时间线上下文;
  • fence_lock:保护时间线上的围栏的自旋锁;
  • fence_seqno:用作crtc时间线上创建的围栏的单调计数器;
  • timeline_namecrtc时间线的名称;
  • self_refresh_data:保存自刷新辅助程序状态的数据;
2.1.1 struct drm_display_mode

struct drm_display_mode用于表示显示模式,包含了显示的各种时序参数配置(与显示器息息相关)。定义在include/drm/drm_modes.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* struct drm_display_mode - DRM kernel-internal display mode structure
* @hdisplay: horizontal display size
* @hsync_start: horizontal sync start
* @hsync_end: horizontal sync end
* @htotal: horizontal total size
* @hskew: horizontal skew?!
* @vdisplay: vertical display size
* @vsync_start: vertical sync start
* @vsync_end: vertical sync end
* @vtotal: vertical total size
* @vscan: vertical scan?!
* @crtc_hdisplay: hardware mode horizontal display size
* @crtc_hblank_start: hardware mode horizontal blank start
* @crtc_hblank_end: hardware mode horizontal blank end
* @crtc_hsync_start: hardware mode horizontal sync start
* @crtc_hsync_end: hardware mode horizontal sync end
* @crtc_htotal: hardware mode horizontal total size
* @crtc_hskew: hardware mode horizontal skew?!
* @crtc_vdisplay: hardware mode vertical display size
* @crtc_vblank_start: hardware mode vertical blank start
* @crtc_vblank_end: hardware mode vertical blank end
* @crtc_vsync_start: hardware mode vertical sync start
* @crtc_vsync_end: hardware mode vertical sync end
* @crtc_vtotal: hardware mode vertical total size
*
* This is the kernel API display mode information structure. For the
* user-space version see struct drm_mode_modeinfo.
*
* The horizontal and vertical timings are defined per the following diagram.
*
* ::
*
*
* Active Front Sync Back
* Region Porch Porch
* <-----------------------><----------------><-------------><-------------->
* //////////////////////|
* ////////////////////// |
* ////////////////////// |.................. ................
* _______________
* <----- [hv]display ----->
* <------------- [hv]sync_start ------------>
* <--------------------- [hv]sync_end --------------------->
* <-------------------------------- [hv]total ----------------------------->*
*
* This structure contains two copies of timings. First are the plain timings,
* which specify the logical mode, as it would be for a progressive 1:1 scanout
* at the refresh rate userspace can observe through vblank timestamps. Then
* there's the hardware timings, which are corrected for interlacing,
* double-clocking and similar things. They are provided as a convenience, and
* can be appropriately computed using drm_mode_set_crtcinfo().
*
* For printing you can use %DRM_MODE_FMT and DRM_MODE_ARG().
*/
struct drm_display_mode {
/**
* @clock:
*
* Pixel clock in kHz.
*/
int clock; /* in kHz */
u16 hdisplay;
u16 hsync_start;
u16 hsync_end;
u16 htotal;
u16 hskew;
u16 vdisplay;
u16 vsync_start;
u16 vsync_end;
u16 vtotal;
u16 vscan;
/**
* @flags:
*
* Sync and timing flags:
*
* - DRM_MODE_FLAG_PHSYNC: horizontal sync is active high.
* - DRM_MODE_FLAG_NHSYNC: horizontal sync is active low.
* - DRM_MODE_FLAG_PVSYNC: vertical sync is active high.
* - DRM_MODE_FLAG_NVSYNC: vertical sync is active low.
* - DRM_MODE_FLAG_INTERLACE: mode is interlaced.
* - DRM_MODE_FLAG_DBLSCAN: mode uses doublescan.
* - DRM_MODE_FLAG_CSYNC: mode uses composite sync.
* - DRM_MODE_FLAG_PCSYNC: composite sync is active high.
* - DRM_MODE_FLAG_NCSYNC: composite sync is active low.
* - DRM_MODE_FLAG_HSKEW: hskew provided (not used?).
* - DRM_MODE_FLAG_BCAST: <deprecated>
* - DRM_MODE_FLAG_PIXMUX: <deprecated>
* - DRM_MODE_FLAG_DBLCLK: double-clocked mode.
* - DRM_MODE_FLAG_CLKDIV2: half-clocked mode.
*
* Additionally there's flags to specify how 3D modes are packed:
*
* - DRM_MODE_FLAG_3D_NONE: normal, non-3D mode.
* - DRM_MODE_FLAG_3D_FRAME_PACKING: 2 full frames for left and right.
* - DRM_MODE_FLAG_3D_FIELD_ALTERNATIVE: interleaved like fields.
* - DRM_MODE_FLAG_3D_LINE_ALTERNATIVE: interleaved lines.
* - DRM_MODE_FLAG_3D_SIDE_BY_SIDE_FULL: side-by-side full frames.
* - DRM_MODE_FLAG_3D_L_DEPTH: ?
* - DRM_MODE_FLAG_3D_L_DEPTH_GFX_GFX_DEPTH: ?
* - DRM_MODE_FLAG_3D_TOP_AND_BOTTOM: frame split into top and bottom
* parts.
* - DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF: frame split into left and
* right parts.
*/
u32 flags;
u32 flags;

/**
* @crtc_clock:
*
* Actual pixel or dot clock in the hardware. This differs from the
* logical @clock when e.g. using interlacing, double-clocking, stereo
* modes or other fancy stuff that changes the timings and signals
* actually sent over the wire.
*
* This is again in kHz.
*
* Note that with digital outputs like HDMI or DP there's usually a
* massive confusion between the dot clock and the signal clock at the
* bit encoding level. Especially when a 8b/10b encoding is used and the
* difference is exactly a factor of 10.
*/
int crtc_clock;
u16 crtc_hdisplay;
u16 crtc_hblank_start;
u16 crtc_hblank_end;
u16 crtc_hsync_start;
u16 crtc_hsync_end;
u16 crtc_htotal;
u16 crtc_hskew;
u16 crtc_vdisplay;
u16 crtc_vblank_start;
u16 crtc_vblank_end;
u16 crtc_vsync_start;
u16 crtc_vsync_end;
u16 crtc_vtotal;

/**
* @width_mm:
*
* Addressable size of the output in mm, projectors should set this to
* 0.
*/
u16 width_mm;

/**
* @height_mm:
*
* Addressable size of the output in mm, projectors should set this to
* 0.
*/
u16 height_mm;
/**
* @type:
*
* A bitmask of flags, mostly about the source of a mode. Possible flags
* are:
*
* - DRM_MODE_TYPE_PREFERRED: Preferred mode, usually the native
* resolution of an LCD panel. There should only be one preferred
* mode per connector at any given time.
* - DRM_MODE_TYPE_DRIVER: Mode created by the driver, which is all of
* them really. Drivers must set this bit for all modes they create
* and expose to userspace.
* - DRM_MODE_TYPE_USERDEF: Mode defined or selected via the kernel
* command line.
*
* Plus a big list of flags which shouldn't be used at all, but are
* still around since these flags are also used in the userspace ABI.
* We no longer accept modes with these types though:
*
* - DRM_MODE_TYPE_BUILTIN: Meant for hard-coded modes, unused.
* Use DRM_MODE_TYPE_DRIVER instead.
* - DRM_MODE_TYPE_DEFAULT: Again a leftover, use
* DRM_MODE_TYPE_PREFERRED instead.
* - DRM_MODE_TYPE_CLOCK_C and DRM_MODE_TYPE_CRTC_C: Define leftovers
* which are stuck around for hysterical raisins only. No one has an
* idea what they were meant for. Don't use.
*/
u8 type;

/**
* @expose_to_userspace:
*
* Indicates whether the mode is to be exposed to the userspace.
* This is to maintain a set of exposed modes while preparing
* user-mode's list in drm_mode_getconnector ioctl. The purpose of
* this only lies in the ioctl function, and is not to be used
* outside the function.
*/
bool expose_to_userspace;

/**
* @head:
*
* struct list_head for mode lists.
*/
struct list_head head;

/**
* @name:
*
* Human-readable name of the mode, filled out with drm_mode_set_name().
*/
char name[DRM_DISPLAY_MODE_LEN];

/**
* @status:
*
* Status of the mode, used to filter out modes not supported by the
* hardware. See enum &drm_mode_status.
*/
enum drm_mode_status status;
/**
* @picture_aspect_ratio:
*
* Field for setting the HDMI picture aspect ratio of a mode.
*/
enum hdmi_picture_aspect picture_aspect_ratio;

};

结构体包含以下成员变量:

  • hdisplay:行有效像素;
  • hsync_start:行同步起始像素;
  • hsync_end:行同步结束像素;
  • htotal:行总大小;
  • hskew:行偏差,通常为 0(暂不清楚具体含义);
  • vdisplay:帧有效行;
  • vsync_start:帧同步起始行;
  • vsync_end:帧同步结束行;
  • vtotal:一帧总行数;
  • vscan: 帧扫描信号,通常为 0(暂不清楚具体含义);
  • crtc_hdisplay:硬件模式的行有效像素;;
  • crtc_hblank_start:硬件模式的行空白起始像素;
  • crtc_hblank_end:硬件模式的行空白结束像素;
  • crtc_hsync_start:硬件模式的行同步起始像素;
  • crtc_hsync_end:硬件模式的行同步结束像素;
  • crtc_htotal:硬件模式的行总大小;
  • crtc_hskew:硬件模式的行偏差,通常为 0(暂不清楚具体含义);
  • crtc_vdisplay:硬件模式的帧有效行;
  • crtc_vblank_start:硬件模式的帧空白起始行;
  • crtc_vblank_end:硬件模式的帧空白结束行;
  • crtc_vsync_start:硬件模式的帧同步起始行;
  • crtc_vsync_end:硬件模式的帧同步结束行;
  • crtc_vtotal:硬件模式的一帧总行数;

关于hdisplayhsync_starthsync_endhtotal对应着我们第一节中介绍的水平时序参数;

同理vdisplayvsync_startvsync_endvtotal对应着垂直时序参数;

2.1.2 struct drm_crtc_state

struct drm_crtc_state用于表示crtc的状态,定义在include/drm/drm_crtc.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* struct drm_crtc_state - mutable CRTC state
*
* Note that the distinction between @enable and @active is rather subtle:
* Flipping @active while @enable is set without changing anything else may
* never return in a failure from the &drm_mode_config_funcs.atomic_check
* callback. Userspace assumes that a DPMS On will always succeed. In other
* words: @enable controls resource assignment, @active controls the actual
* hardware state.
*
* The three booleans active_changed, connectors_changed and mode_changed are
* intended to indicate whether a full modeset is needed, rather than strictly
* describing what has changed in a commit. See also:
* drm_atomic_crtc_needs_modeset()
*
* WARNING: Transitional helpers (like drm_helper_crtc_mode_set() or
* drm_helper_crtc_mode_set_base()) do not maintain many of the derived control
* state like @plane_mask so drivers not converted over to atomic helpers should
* not rely on these being accurate!
*/
struct drm_crtc_state {
/** @crtc: backpointer to the CRTC */
struct drm_crtc *crtc;

/**
* @enable: Whether the CRTC should be enabled, gates all other state.
* This controls reservations of shared resources. Actual hardware state
* is controlled by @active.
*/
bool enable;

/**
* @active: Whether the CRTC is actively displaying (used for DPMS).
* Implies that @enable is set. The driver must not release any shared
* resources if @active is set to false but @enable still true, because
* userspace expects that a DPMS ON always succeeds.
*
* Hence drivers must not consult @active in their various
* &drm_mode_config_funcs.atomic_check callback to reject an atomic
* commit. They can consult it to aid in the computation of derived
* hardware state, since even in the DPMS OFF state the display hardware
* should be as much powered down as when the CRTC is completely
* disabled through setting @enable to false.
*/
bool active;

/**
* @planes_changed: Planes on this crtc are updated. Used by the atomic
* helpers and drivers to steer the atomic commit control flow.
*/
bool planes_changed : 1;

/**
* @mode_changed: @mode or @enable has been changed. Used by the atomic
* helpers and drivers to steer the atomic commit control flow. See also
* drm_atomic_crtc_needs_modeset().
*
* Drivers are supposed to set this for any CRTC state changes that
* require a full modeset. They can also reset it to false if e.g. a
* @mode change can be done without a full modeset by only changing
* scaler settings.
*/
bool mode_changed : 1;
/**
* @active_changed: @active has been toggled. Used by the atomic
* helpers and drivers to steer the atomic commit control flow. See also
* drm_atomic_crtc_needs_modeset().
*/
bool active_changed : 1;

/**
* @connectors_changed: Connectors to this crtc have been updated,
* either in their state or routing. Used by the atomic
* helpers and drivers to steer the atomic commit control flow. See also
* drm_atomic_crtc_needs_modeset().
*
* Drivers are supposed to set this as-needed from their own atomic
* check code, e.g. from &drm_encoder_helper_funcs.atomic_check
*/
bool connectors_changed : 1;

/**
* @zpos_changed: zpos values of planes on this crtc have been updated.
* Used by the atomic helpers and drivers to steer the atomic commit
* control flow.
*/
bool zpos_changed : 1;
/**
* @color_mgmt_changed: Color management properties have changed
* (@gamma_lut, @degamma_lut or @ctm). Used by the atomic helpers and
* drivers to steer the atomic commit control flow.
*/
bool color_mgmt_changed : 1;

/**
* @no_vblank:
*
* Reflects the ability of a CRTC to send VBLANK events. This state
* usually depends on the pipeline configuration. If set to true, DRM
* atomic helpers will send out a fake VBLANK event during display
* updates after all hardware changes have been committed. This is
* implemented in drm_atomic_helper_fake_vblank().
*
* One usage is for drivers and/or hardware without support for VBLANK
* interrupts. Such drivers typically do not initialize vblanking
* (i.e., call drm_vblank_init() with the number of CRTCs). For CRTCs
* without initialized vblanking, this field is set to true in
* drm_atomic_helper_check_modeset(), and a fake VBLANK event will be
* send out on each update of the display pipeline by
* drm_atomic_helper_fake_vblank().
*
* Another usage is CRTCs feeding a writeback connector operating in
* oneshot mode. In this case the fake VBLANK event is only generated
* when a job is queued to the writeback connector, and we want the
* core to fake VBLANK events when this part of the pipeline hasn't
* changed but others had or when the CRTC and connectors are being
* disabled.
*
* __drm_atomic_helper_crtc_duplicate_state() will not reset the value
* from the current state, the CRTC driver is then responsible for
* updating this field when needed.
*
* Note that the combination of &drm_crtc_state.event == NULL and
* &drm_crtc_state.no_blank == true is valid and usually used when the
* writeback connector attached to the CRTC has a new job queued. In
* this case the driver will send the VBLANK event on its own when the
* writeback job is complete.
bool no_vblank : 1;

/**
* @plane_mask: Bitmask of drm_plane_mask(plane) of planes attached to
* this CRTC.
*/
u32 plane_mask;

/**
* @connector_mask: Bitmask of drm_connector_mask(connector) of
* connectors attached to this CRTC.
*/
u32 connector_mask;

/**
* @encoder_mask: Bitmask of drm_encoder_mask(encoder) of encoders
* attached to this CRTC.
*/
u32 encoder_mask;

/**
* @adjusted_mode:
*
* Internal display timings which can be used by the driver to handle
* differences between the mode requested by userspace in @mode and what
* is actually programmed into the hardware.
*
* For drivers using &drm_bridge, this stores hardware display timings
* used between the CRTC and the first bridge. For other drivers, the
* meaning of the adjusted_mode field is purely driver implementation
* defined information, and will usually be used to store the hardware
* display timings used between the CRTC and encoder blocks.
*/
struct drm_display_mode adjusted_mode;

/**
* @mode:
*
* Display timings requested by userspace. The driver should try to
* match the refresh rate as close as possible (but note that it's
* undefined what exactly is close enough, e.g. some of the HDMI modes
* only differ in less than 1% of the refresh rate). The active width
* and height as observed by userspace for positioning planes must match
* exactly.
*
* For external connectors where the sink isn't fixed (like with a
* built-in panel), this mode here should match the physical mode on the
* wire to the last details (i.e. including sync polarities and
* everything).
*/
struct drm_display_mode mode;

/**
* @mode_blob: &drm_property_blob for @mode, for exposing the mode to
* atomic userspace.
*/
struct drm_property_blob *mode_blob;

/**
* @degamma_lut:
*
* Lookup table for converting framebuffer pixel data before apply the
* color conversion matrix @ctm. See drm_crtc_enable_color_mgmt(). The
* blob (if not NULL) is an array of &struct drm_color_lut.
*/
struct drm_property_blob *degamma_lut;

/**
* @ctm:
*
* Color transformation matrix. See drm_crtc_enable_color_mgmt(). The
* blob (if not NULL) is a &struct drm_color_ctm.
*/
struct drm_property_blob *ctm;

/**
* @gamma_lut:
*
* Lookup table for converting pixel data after the color conversion
* matrix @ctm. See drm_crtc_enable_color_mgmt(). The blob (if not
* NULL) is an array of &struct drm_color_lut.
*
* Note that for mostly historical reasons stemming from Xorg heritage,
* this is also used to store the color map (also sometimes color lut,
* CLUT or color palette) for indexed formats like DRM_FORMAT_C8.
*/
struct drm_property_blob *gamma_lut;

/**
* @target_vblank:
*
* Target vertical blank period when a page flip
* should take effect.
*/
u32 target_vblank;

/**
* @async_flip:
*
* This is set when DRM_MODE_PAGE_FLIP_ASYNC is set in the legacy
* PAGE_FLIP IOCTL. It's not wired up for the atomic IOCTL itself yet.
*/
bool async_flip;

/**
* @vrr_enabled:
*
* Indicates if variable refresh rate should be enabled for the CRTC.
* Support for the requested vrr state will depend on driver and
* hardware capabiltiy - lacking support is not treated as failure.
*/
bool vrr_enabled;

/**
* @self_refresh_active:
*
* Used by the self refresh helpers to denote when a self refresh
* transition is occurring. This will be set on enable/disable callbacks
* when self refresh is being enabled or disabled. In some cases, it may
* not be desirable to fully shut off the crtc during self refresh.
* CRTC's can inspect this flag and determine the best course of action.
*/
bool self_refresh_active;

/**
* @scaling_filter:
*
* Scaling filter to be applied
*/
enum drm_scaling_filter scaling_filter;

/**
* @event:
*
* Optional pointer to a DRM event to signal upon completion of the
* state update. The driver must send out the event when the atomic
* commit operation completes. There are two cases:
*
* - The event is for a CRTC which is being disabled through this
* atomic commit. In that case the event can be send out any time
* after the hardware has stopped scanning out the current
* framebuffers. It should contain the timestamp and counter for the
* last vblank before the display pipeline was shut off. The simplest
* way to achieve that is calling drm_crtc_send_vblank_event()
* somewhen after drm_crtc_vblank_off() has been called.
*
* - For a CRTC which is enabled at the end of the commit (even when it
* undergoes an full modeset) the vblank timestamp and counter must
* be for the vblank right before the first frame that scans out the
* new set of buffers. Again the event can only be sent out after the
* hardware has stopped scanning out the old buffers.
*
* - Events for disabled CRTCs are not allowed, and drivers can ignore
* that case.
*
* For very simple hardware without VBLANK interrupt, enabling
* &struct drm_crtc_state.no_vblank makes DRM's atomic commit helpers
* send a fake VBLANK event at the end of the display update after all
* hardware changes have been applied. See
* drm_atomic_helper_fake_vblank().
*
* For more complex hardware this
* can be handled by the drm_crtc_send_vblank_event() function,
* which the driver should call on the provided event upon completion of
* the atomic commit. Note that if the driver supports vblank signalling
* and timestamping the vblank counters and timestamps must agree with
* the ones returned from page flip events. With the current vblank
* helper infrastructure this can be achieved by holding a vblank
* reference while the page flip is pending, acquired through
* drm_crtc_vblank_get() and released with drm_crtc_vblank_put().
* Drivers are free to implement their own vblank counter and timestamp
* tracking though, e.g. if they have accurate timestamp registers in
* hardware.
*
* For hardware which supports some means to synchronize vblank

* interrupt delivery with committing display state there's also
* drm_crtc_arm_vblank_event(). See the documentation of that function
* for a detailed discussion of the constraints it needs to be used
* safely.
*
* If the device can't notify of flip completion in a race-free way
* at all, then the event should be armed just after the page flip is
* committed. In the worst case the driver will send the event to
* userspace one frame too late. This doesn't allow for a real atomic
* update, but it should avoid tearing.
*/
struct drm_pending_vblank_event *event;

/**
* @commit:
*
* This tracks how the commit for this update proceeds through the
* various phases. This is never cleared, except when we destroy the
* state, so that subsequent commits can synchronize with previous ones.
*/
struct drm_crtc_commit *commit;

/** @state: backpointer to global drm_atomic_state */
struct drm_atomic_state *state;
};

2.2 操作函数

2.2.1 struct drm_crtc_funcs

struct drm_crtc_funcs用于定义与crtc相关的函数和操作,该结构体包含了一系列函数指针,用于实现包括模式设置、帧缓冲区映射、显示控制等功能。定义在include/drm/drm_crtc.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
* struct drm_crtc_funcs - control CRTCs for a given device
*
* The drm_crtc_funcs structure is the central CRTC management structure
* in the DRM. Each CRTC controls one or more connectors (note that the name
* CRTC is simply historical, a CRTC may control LVDS, VGA, DVI, TV out, etc.
* connectors, not just CRTs).
*
* Each driver is responsible for filling out this structure at startup time,
* in addition to providing other modesetting features, like i2c and DDC
* bus accessors.
*/
struct drm_crtc_funcs {
/**
* @reset:
*
* Reset CRTC hardware and software state to off. This function isn't
* called by the core directly, only through drm_mode_config_reset().
* It's not a helper hook only for historical reasons.
*
* Atomic drivers can use drm_atomic_helper_crtc_reset() to reset
* atomic state using this hook.
*/
void (*reset)(struct drm_crtc *crtc);

/**
* @cursor_set:
*
* Update the cursor image. The cursor position is relative to the CRTC
* and can be partially or fully outside of the visible area.
*
* Note that contrary to all other KMS functions the legacy cursor entry
* points don't take a framebuffer object, but instead take directly a
* raw buffer object id from the driver's buffer manager (which is
* either GEM or TTM for current drivers).
*
* This entry point is deprecated, drivers should instead implement
* universal plane support and register a proper cursor plane using
* drm_crtc_init_with_planes().
*
* This callback is optional
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*cursor_set)(struct drm_crtc *crtc, struct drm_file *file_priv,
uint32_t handle, uint32_t width, uint32_t height);

/**
* @cursor_set2:
*
* Update the cursor image, including hotspot information. The hotspot
* must not affect the cursor position in CRTC coordinates, but is only
* meant as a hint for virtualized display hardware to coordinate the
* guests and hosts cursor position. The cursor hotspot is relative to
* the cursor image. Otherwise this works exactly like @cursor_set.
*
* This entry point is deprecated, drivers should instead implement
* universal plane support and register a proper cursor plane using
* drm_crtc_init_with_planes().
*
* This callback is optional.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*cursor_set2)(struct drm_crtc *crtc, struct drm_file *file_priv,
uint32_t handle, uint32_t width, uint32_t height,
int32_t hot_x, int32_t hot_y);

/**
* @cursor_move:
*
* Update the cursor position. The cursor does not need to be visible
* when this hook is called.
*
* This entry point is deprecated, drivers should instead implement
* universal plane support and register a proper cursor plane using
* drm_crtc_init_with_planes().
*
* This callback is optional.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*cursor_move)(struct drm_crtc *crtc, int x, int y);

/**
* @gamma_set:
*
* Set gamma on the CRTC.
*
* This callback is optional.
*
* Atomic drivers who want to support gamma tables should implement the
* atomic color management support, enabled by calling
* drm_crtc_enable_color_mgmt(), which then supports the legacy gamma
* interface through the drm_atomic_helper_legacy_gamma_set()
* compatibility implementation.
*/
int (*gamma_set)(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
uint32_t size,
struct drm_modeset_acquire_ctx *ctx);

/**
* @destroy:
*
* Clean up CRTC resources. This is only called at driver unload time
* through drm_mode_config_cleanup() since a CRTC cannot be hotplugged
* in DRM.
*/
void (*destroy)(struct drm_crtc *crtc);

/**
* @set_config:
*
* This is the main legacy entry point to change the modeset state on a
* CRTC. All the details of the desired configuration are passed in a
* &struct drm_mode_set - see there for details.
*
* Drivers implementing atomic modeset should use
* drm_atomic_helper_set_config() to implement this hook.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*set_config)(struct drm_mode_set *set,
struct drm_modeset_acquire_ctx *ctx);
/**
* @page_flip:
*
* Legacy entry point to schedule a flip to the given framebuffer.
*
* Page flipping is a synchronization mechanism that replaces the frame
* buffer being scanned out by the CRTC with a new frame buffer during
* vertical blanking, avoiding tearing (except when requested otherwise
* through the DRM_MODE_PAGE_FLIP_ASYNC flag). When an application
* requests a page flip the DRM core verifies that the new frame buffer
* is large enough to be scanned out by the CRTC in the currently
* configured mode and then calls this hook with a pointer to the new
* frame buffer.
*
* The driver must wait for any pending rendering to the new framebuffer
* to complete before executing the flip. It should also wait for any
* pending rendering from other drivers if the underlying buffer is a
* shared dma-buf.
*
* An application can request to be notified when the page flip has
* completed. The drm core will supply a &struct drm_event in the event
* parameter in this case. This can be handled by the
* drm_crtc_send_vblank_event() function, which the driver should call on
* the provided event upon completion of the flip. Note that if
* the driver supports vblank signalling and timestamping the vblank
* counters and timestamps must agree with the ones returned from page
* flip events. With the current vblank helper infrastructure this can
* be achieved by holding a vblank reference while the page flip is
* pending, acquired through drm_crtc_vblank_get() and released with
* drm_crtc_vblank_put(). Drivers are free to implement their own vblank
* counter and timestamp tracking though, e.g. if they have accurate
* timestamp registers in hardware.
*
* This callback is optional.
*
* NOTE:
*
* Very early versions of the KMS ABI mandated that the driver must
* block (but not reject) any rendering to the old framebuffer until the
* flip operation has completed and the old framebuffer is no longer
* visible. This requirement has been lifted, and userspace is instead
* expected to request delivery of an event and wait with recycling old
* buffers until such has been received.
*
* RETURNS:
*
* 0 on success or a negative error code on failure. Note that if a
* page flip operation is already pending the callback should return
* -EBUSY. Pageflips on a disabled CRTC (either by setting a NULL mode
* or just runtime disabled through DPMS respectively the new atomic
* "ACTIVE" state) should result in an -EINVAL error code. Note that
* drm_atomic_helper_page_flip() checks this already for atomic drivers.
*/
int (*page_flip)(struct drm_crtc *crtc,
struct drm_framebuffer *fb,
struct drm_pending_vblank_event *event,
uint32_t flags,
struct drm_modeset_acquire_ctx *ctx);

/**
* @page_flip_target:
*
* Same as @page_flip but with an additional parameter specifying the
* absolute target vertical blank period (as reported by
* drm_crtc_vblank_count()) when the flip should take effect.
*
* Note that the core code calls drm_crtc_vblank_get before this entry
* point, and will call drm_crtc_vblank_put if this entry point returns
* any non-0 error code. It's the driver's responsibility to call
* drm_crtc_vblank_put after this entry point returns 0, typically when
* the flip completes.
*/
int (*page_flip_target)(struct drm_crtc *crtc,
struct drm_framebuffer *fb,
struct drm_pending_vblank_event *event,
uint32_t flags, uint32_t target,
struct drm_modeset_acquire_ctx *ctx);

/**
* @set_property:
*
* This is the legacy entry point to update a property attached to the
* CRTC.
*
* This callback is optional if the driver does not support any legacy
* driver-private properties. For atomic drivers it is not used because
* property handling is done entirely in the DRM core.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*set_property)(struct drm_crtc *crtc,
struct drm_property *property, uint64_t val);

/**
* @atomic_duplicate_state:
*
* Duplicate the current atomic state for this CRTC and return it.
* The core and helpers guarantee that any atomic state duplicated with
* this hook and still owned by the caller (i.e. not transferred to the
* driver by calling &drm_mode_config_funcs.atomic_commit) will be
* cleaned up by calling the @atomic_destroy_state hook in this
* structure.
*
* This callback is mandatory for atomic drivers.
*
* Atomic drivers which don't subclass &struct drm_crtc_state should use
* drm_atomic_helper_crtc_duplicate_state(). Drivers that subclass the
* state structure to extend it with driver-private state should use
* __drm_atomic_helper_crtc_duplicate_state() to make sure shared state is
* duplicated in a consistent fashion across drivers.
*
* It is an error to call this hook before &drm_crtc.state has been
* initialized correctly.
*
* NOTE:
*
* If the duplicate state references refcounted resources this hook must
* acquire a reference for each of them. The driver must release these
* references again in @atomic_destroy_state.
*
* RETURNS:
*
* Duplicated atomic state or NULL when the allocation failed.
*/
struct drm_crtc_state *(*atomic_duplicate_state)(struct drm_crtc *crtc);

/**
* @atomic_destroy_state:
*
* Destroy a state duplicated with @atomic_duplicate_state and release
* or unreference all resources it references
*
* This callback is mandatory for atomic drivers.
*/
void (*atomic_destroy_state)(struct drm_crtc *crtc,
struct drm_crtc_state *state);

/**
* @atomic_set_property:
*
* Decode a driver-private property value and store the decoded value
* into the passed-in state structure. Since the atomic core decodes all
* standardized properties (even for extensions beyond the core set of
* properties which might not be implemented by all drivers) this
* requires drivers to subclass the state structure.
*
* Such driver-private properties should really only be implemented for
* truly hardware/vendor specific state. Instead it is preferred to
* standardize atomic extension and decode the properties used to expose
* such an extension in the core.
*
* Do not call this function directly, use
* drm_atomic_crtc_set_property() instead.
*
* This callback is optional if the driver does not support any
* driver-private atomic properties.
*
* NOTE:
*
* This function is called in the state assembly phase of atomic
* modesets, which can be aborted for any reason (including on
* userspace's request to just check whether a configuration would be
* possible). Drivers MUST NOT touch any persistent state (hardware or
* software) or data structures except the passed in @state parameter.
*
* Also since userspace controls in which order properties are set this
* function must not do any input validation (since the state update is
* incomplete and hence likely inconsistent). Instead any such input
* validation must be done in the various atomic_check callbacks.
*
* RETURNS:
*
* 0 if the property has been found, -EINVAL if the property isn't
* implemented by the driver (which should never happen, the core only
* asks for properties attached to this CRTC). No other validation is
* allowed by the driver. The core already checks that the property
* value is within the range (integer, valid enum value, ...) the driver
* set when registering the property.
*/
int (*atomic_set_property)(struct drm_crtc *crtc,
struct drm_crtc_state *state,
struct drm_property *property,
uint64_t val);
/**
* @atomic_get_property:
*
* Reads out the decoded driver-private property. This is used to
* implement the GETCRTC IOCTL.
*
* Do not call this function directly, use
* drm_atomic_crtc_get_property() instead.
*
* This callback is optional if the driver does not support any
* driver-private atomic properties.
*
* RETURNS:
*
* 0 on success, -EINVAL if the property isn't implemented by the
* driver (which should never happen, the core only asks for
* properties attached to this CRTC).
*/
int (*atomic_get_property)(struct drm_crtc *crtc,
const struct drm_crtc_state *state,
struct drm_property *property,
uint64_t *val);

/**
* @late_register:
*
* This optional hook can be used to register additional userspace
* interfaces attached to the crtc like debugfs interfaces.
* It is called late in the driver load sequence from drm_dev_register().
* Everything added from this callback should be unregistered in
* the early_unregister callback.
*
* Returns:
*
* 0 on success, or a negative error code on failure.
*/
int (*late_register)(struct drm_crtc *crtc);

/**
* @early_unregister:
*
* This optional hook should be used to unregister the additional
* userspace interfaces attached to the crtc from
* @late_register. It is called from drm_dev_unregister(),
* early in the driver unload sequence to disable userspace access
* before data structures are torndown.
*/
void (*early_unregister)(struct drm_crtc *crtc);

/**
* @set_crc_source:
*
* Changes the source of CRC checksums of frames at the request of
* userspace, typically for testing purposes. The sources available are
* specific of each driver and a %NULL value indicates that CRC
* generation is to be switched off.
*
* When CRC generation is enabled, the driver should call
* drm_crtc_add_crc_entry() at each frame, providing any information
* that characterizes the frame contents in the crcN arguments, as
* provided from the configured source. Drivers must accept an "auto"
* source name that will select a default source for this CRTC.
*
* This may trigger an atomic modeset commit if necessary, to enable CRC
* generation.
*
* Note that "auto" can depend upon the current modeset configuration,
* e.g. it could pick an encoder or output specific CRC sampling point.
*
* This callback is optional if the driver does not support any CRC
* generation functionality.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*set_crc_source)(struct drm_crtc *crtc, const char *source);

/**
* @verify_crc_source:
*
* verifies the source of CRC checksums of frames before setting the
* source for CRC and during crc open. Source parameter can be NULL
* while disabling crc source.
*
* This callback is optional if the driver does not support any CRC
* generation functionality.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*verify_crc_source)(struct drm_crtc *crtc, const char *source,
size_t *values_cnt);
/**
* @get_crc_sources:
*
* Driver callback for getting a list of all the available sources for
* CRC generation. This callback depends upon verify_crc_source, So
* verify_crc_source callback should be implemented before implementing
* this. Driver can pass full list of available crc sources, this
* callback does the verification on each crc-source before passing it
* to userspace.
*
* This callback is optional if the driver does not support exporting of
* possible CRC sources list.
*
* RETURNS:
*
* a constant character pointer to the list of all the available CRC
* sources. On failure driver should return NULL. count should be
* updated with number of sources in list. if zero we don't process any
* source from the list.
*/
const char *const *(*get_crc_sources)(struct drm_crtc *crtc,
size_t *count);

/**
* @atomic_print_state:
*
* If driver subclasses &struct drm_crtc_state, it should implement
* this optional hook for printing additional driver specific state.
*
* Do not call this directly, use drm_atomic_crtc_print_state()
* instead.
*/
void (*atomic_print_state)(struct drm_printer *p,
const struct drm_crtc_state *state);

/**
* @get_vblank_counter:
*
* Driver callback for fetching a raw hardware vblank counter for the
* CRTC. It's meant to be used by new drivers as the replacement of
* &drm_driver.get_vblank_counter hook.
*
* This callback is optional. If a device doesn't have a hardware
* counter, the driver can simply leave the hook as NULL. The DRM core
* will account for missed vblank events while interrupts where disabled
* based on system timestamps.
*
* Wraparound handling and loss of events due to modesetting is dealt
* with in the DRM core code, as long as drivers call
* drm_crtc_vblank_off() and drm_crtc_vblank_on() when disabling or
* enabling a CRTC.
*
* See also &drm_device.vblank_disable_immediate and
* &drm_device.max_vblank_count.
*
* Returns:
*
* Raw vblank counter value.
*/
u32 (*get_vblank_counter)(struct drm_crtc *crtc);

/**
* @enable_vblank:
*
* Enable vblank interrupts for the CRTC. It's meant to be used by
* new drivers as the replacement of &drm_driver.enable_vblank hook.
*
* Returns:
*
* Zero on success, appropriate errno if the vblank interrupt cannot
* be enabled.
*/
int (*enable_vblank)(struct drm_crtc *crtc);

/**
* @disable_vblank:
*
* Disable vblank interrupts for the CRTC. It's meant to be used by
* new drivers as the replacement of &drm_driver.disable_vblank hook.
*/
void (*disable_vblank)(struct drm_crtc *crtc);

/**
* @get_vblank_timestamp:
*
* Called by drm_get_last_vbltimestamp(). Should return a precise
* timestamp when the most recent vblank interval ended or will end.
*
* Specifically, the timestamp in @vblank_time should correspond as
* closely as possible to the time when the first video scanline of
* the video frame after the end of vblank will start scanning out,
* the time immediately after end of the vblank interval. If the
* @crtc is currently inside vblank, this will be a time in the future.
* If the @crtc is currently scanning out a frame, this will be the
* past start time of the current scanout. This is meant to adhere
* to the OpenML OML_sync_control extension specification.
*
* Parameters:
*
* crtc:
* CRTC for which timestamp should be returned.
* max_error:
* Maximum allowable timestamp error in nanoseconds.
* Implementation should strive to provide timestamp
* with an error of at most max_error nanoseconds.
* Returns true upper bound on error for timestamp.
* vblank_time:
* Target location for returned vblank timestamp.
* in_vblank_irq:
* True when called from drm_crtc_handle_vblank(). Some drivers
* need to apply some workarounds for gpu-specific vblank irq quirks
* if flag is set.
*
* Returns:
*
* True on success, false on failure, which means the core should
* fallback to a simple timestamp taken in drm_crtc_handle_vblank().
*/
bool (*get_vblank_timestamp)(struct drm_crtc *crtc,
int *max_error,
ktime_t *vblank_time,
bool in_vblank_irq);
};

这里定义了一大堆回调函数:

  • reset:用于将crtc硬件和软件重置为关闭状态,这个函数不会直接调用,只会通过drm_mode_config_reset调用;

  • cursor_set:更新光标图像,光标位置是相对于crtc,并且可以部分或完全位于可见区域之外;

  • cursor_set2:更新光标图像,包括热点信息;

  • cursor_move:更新光标位置。在调用此钩子时,光标不需要可见;

  • gamma_set:在crtc上设置gamma

  • destroy:清理crtc资源;

  • set_config:改变modeset state, 负责配置几个内容:

    • 更新正在扫描的framebuffer
    • 配置显示模式:时序、分辨率等;
    • 将连接器/编码器附加到crtc
  • page_flip:用于反转给定的framebuffer

2.2.2 struct drm_crtc_helper_funcs

struct drm_crtc_helper_funcs用于定义与crtc助手函数相关的操作。这些助手函数提供了一些辅助性的操作,如时序生成、同步信号发送、格式转换等,以便更好地协助实现扫描输出帧缓冲区内容的功能。定义在include/drm/drm_modeset_helper_vtables.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* struct drm_crtc_helper_funcs - helper operations for CRTCs
*
* These hooks are used by the legacy CRTC helpers, the transitional plane
* helpers and the new atomic modesetting helpers.
*/
struct drm_crtc_helper_funcs {
/**
* @dpms:
*
* Callback to control power levels on the CRTC. If the mode passed in
* is unsupported, the provider must use the next lowest power level.
* This is used by the legacy CRTC helpers to implement DPMS
* functionality in drm_helper_connector_dpms().
*
* This callback is also used to disable a CRTC by calling it with
* DRM_MODE_DPMS_OFF if the @disable hook isn't used.
*
* This callback is used by the legacy CRTC helpers. Atomic helpers
* also support using this hook for enabling and disabling a CRTC to
* facilitate transitions to atomic, but it is deprecated. Instead
* @atomic_enable and @atomic_disable should be used.
*/
void (*dpms)(struct drm_crtc *crtc, int mode);

/**
* @prepare:
*
* This callback should prepare the CRTC for a subsequent modeset, which
* in practice means the driver should disable the CRTC if it is
* running. Most drivers ended up implementing this by calling their
* @dpms hook with DRM_MODE_DPMS_OFF.
*
* This callback is used by the legacy CRTC helpers. Atomic helpers
* also support using this hook for disabling a CRTC to facilitate
* transitions to atomic, but it is deprecated. Instead @atomic_disable
* should be used.
*/
void (*prepare)(struct drm_crtc *crtc);

/**
* @commit:
*
* This callback should commit the new mode on the CRTC after a modeset,
* which in practice means the driver should enable the CRTC. Most
* drivers ended up implementing this by calling their @dpms hook with
* DRM_MODE_DPMS_ON.
*
* This callback is used by the legacy CRTC helpers. Atomic helpers
* also support using this hook for enabling a CRTC to facilitate
* transitions to atomic, but it is deprecated. Instead @atomic_enable
* should be used.
*/
void (*commit)(struct drm_crtc *crtc);
/**
* @mode_valid:
*
* This callback is used to check if a specific mode is valid in this
* crtc. This should be implemented if the crtc has some sort of
* restriction in the modes it can display. For example, a given crtc
* may be responsible to set a clock value. If the clock can not
* produce all the values for the available modes then this callback
* can be used to restrict the number of modes to only the ones that
* can be displayed.
*
* This hook is used by the probe helpers to filter the mode list in
* drm_helper_probe_single_connector_modes(), and it is used by the
* atomic helpers to validate modes supplied by userspace in
* drm_atomic_helper_check_modeset().
*
* This function is optional.
*
* NOTE:
*
* Since this function is both called from the check phase of an atomic
* commit, and the mode validation in the probe paths it is not allowed
* to look at anything else but the passed-in mode, and validate it
* against configuration-invariant hardward constraints. Any further
* limits which depend upon the configuration can only be checked in
* @mode_fixup or @atomic_check.
*
* RETURNS:
*
* drm_mode_status Enum
*/
enum drm_mode_status (*mode_valid)(struct drm_crtc *crtc,
const struct drm_display_mode *mode);

/**
* @mode_fixup:
*
* This callback is used to validate a mode. The parameter mode is the
* display mode that userspace requested, adjusted_mode is the mode the
* encoders need to be fed with. Note that this is the inverse semantics
* of the meaning for the &drm_encoder and &drm_bridge_funcs.mode_fixup
* vfunc. If the CRTC cannot support the requested conversion from mode
* to adjusted_mode it should reject the modeset. See also
* &drm_crtc_state.adjusted_mode for more details.
*
* This function is used by both legacy CRTC helpers and atomic helpers.
* With atomic helpers it is optional.
*
* NOTE:
*
* This function is called in the check phase of atomic modesets, which
* can be aborted for any reason (including on userspace's request to
* just check whether a configuration would be possible). Atomic drivers
* MUST NOT touch any persistent state (hardware or software) or data
* structures except the passed in adjusted_mode parameter.
*
* This is in contrast to the legacy CRTC helpers where this was
* allowed.
*
* Atomic drivers which need to inspect and adjust more state should
* instead use the @atomic_check callback, but note that they're not
* perfectly equivalent: @mode_valid is called from
* drm_atomic_helper_check_modeset(), but @atomic_check is called from
* drm_atomic_helper_check_planes(), because originally it was meant for
* plane update checks only.
*
* Also beware that userspace can request its own custom modes, neither
* core nor helpers filter modes to the list of probe modes reported by
* the GETCONNECTOR IOCTL and stored in &drm_connector.modes. To ensure
* that modes are filtered consistently put any CRTC constraints and
* limits checks into @mode_valid.
*
* RETURNS:
*
* True if an acceptable configuration is possible, false if the modeset
* operation should be rejected.
*/
bool (*mode_fixup)(struct drm_crtc *crtc,
const struct drm_display_mode *mode,
struct drm_display_mode *adjusted_mode);

/**
* @mode_set:
*
* This callback is used by the legacy CRTC helpers to set a new mode,
* position and framebuffer. Since it ties the primary plane to every
* mode change it is incompatible with universal plane support. And
* since it can't update other planes it's incompatible with atomic
* modeset support.
*
* This callback is only used by CRTC helpers and deprecated.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*mode_set)(struct drm_crtc *crtc, struct drm_display_mode *mode,
struct drm_display_mode *adjusted_mode, int x, int y,
struct drm_framebuffer *old_fb);
/**
* @mode_set_nofb:
*
* This callback is used to update the display mode of a CRTC without
* changing anything of the primary plane configuration. This fits the
* requirement of atomic and hence is used by the atomic helpers. It is
* also used by the transitional plane helpers to implement a
* @mode_set hook in drm_helper_crtc_mode_set().
*
* Note that the display pipe is completely off when this function is
* called. Atomic drivers which need hardware to be running before they
* program the new display mode (e.g. because they implement runtime PM)
* should not use this hook. This is because the helper library calls
* this hook only once per mode change and not every time the display
* pipeline is suspended using either DPMS or the new "ACTIVE" property.
* Which means register values set in this callback might get reset when
* the CRTC is suspended, but not restored. Such drivers should instead
* move all their CRTC setup into the @atomic_enable callback.
*
* This callback is optional.
*/
void (*mode_set_nofb)(struct drm_crtc *crtc);

/**
* @mode_set_base:
*
* This callback is used by the legacy CRTC helpers to set a new
* framebuffer and scanout position. It is optional and used as an
* optimized fast-path instead of a full mode set operation with all the
* resulting flickering. If it is not present
* drm_crtc_helper_set_config() will fall back to a full modeset, using
* the @mode_set callback. Since it can't update other planes it's
* incompatible with atomic modeset support.
*
* This callback is only used by the CRTC helpers and deprecated.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*mode_set_base)(struct drm_crtc *crtc, int x, int y,
struct drm_framebuffer *old_fb);

/**
* @mode_set_base_atomic:
*
* This callback is used by the fbdev helpers to set a new framebuffer
* and scanout without sleeping, i.e. from an atomic calling context. It
* is only used to implement kgdb support.
*
* This callback is optional and only needed for kgdb support in the fbdev
* helpers.
*
* RETURNS:
*
* 0 on success or a negative error code on failure.
*/
int (*mode_set_base_atomic)(struct drm_crtc *crtc,
struct drm_framebuffer *fb, int x, int y,
enum mode_set_atomic);

/**
* @disable:
*
* This callback should be used to disable the CRTC. With the atomic
* drivers it is called after all encoders connected to this CRTC have
* been shut off already using their own
* &drm_encoder_helper_funcs.disable hook. If that sequence is too
* simple drivers can just add their own hooks and call it from this
* CRTC callback here by looping over all encoders connected to it using
* for_each_encoder_on_crtc().
*
* This hook is used both by legacy CRTC helpers and atomic helpers.
* Atomic drivers don't need to implement it if there's no need to
* disable anything at the CRTC level. To ensure that runtime PM
* handling (using either DPMS or the new "ACTIVE" property) works
* @disable must be the inverse of @atomic_enable for atomic drivers.
* Atomic drivers should consider to use @atomic_disable instead of
* this one.
*
* NOTE:
*
* With legacy CRTC helpers there's a big semantic difference between
* @disable and other hooks (like @prepare or @dpms) used to shut down a
* CRTC: @disable is only called when also logically disabling the
* display pipeline and needs to release any resources acquired in
* @mode_set (like shared PLLs, or again release pinned framebuffers).
*
* Therefore @disable must be the inverse of @mode_set plus @commit for
* drivers still using legacy CRTC helpers, which is different from the
* rules under atomic.
*/
void (*disable)(struct drm_crtc *crtc);

/**
* @atomic_check:
*
* Drivers should check plane-update related CRTC constraints in this
* hook. They can also check mode related limitations but need to be
* aware of the calling order, since this hook is used by
* drm_atomic_helper_check_planes() whereas the preparations needed to
* check output routing and the display mode is done in
* drm_atomic_helper_check_modeset(). Therefore drivers that want to
* check output routing and display mode constraints in this callback
* must ensure that drm_atomic_helper_check_modeset() has been called
* beforehand. This is calling order used by the default helper
* implementation in drm_atomic_helper_check().
*
* When using drm_atomic_helper_check_planes() this hook is called
* after the &drm_plane_helper_funcs.atomic_check hook for planes, which
* allows drivers to assign shared resources requested by planes in this
* callback here. For more complicated dependencies the driver can call
* the provided check helpers multiple times until the computed state
* has a final configuration and everything has been checked.
*
* This function is also allowed to inspect any other object's state and
* can add more state objects to the atomic commit if needed. Care must
* be taken though to ensure that state check and compute functions for
* these added states are all called, and derived state in other objects
* all updated. Again the recommendation is to just call check helpers
* until a maximal configuration is reached.
*
* This callback is used by the atomic modeset helpers and by the
* transitional plane helpers, but it is optional.
*
* NOTE:
*
* This function is called in the check phase of an atomic update. The
* driver is not allowed to change anything outside of the free-standing
* state object passed-in.
*
* Also beware that userspace can request its own custom modes, neither
* core nor helpers filter modes to the list of probe modes reported by
* the GETCONNECTOR IOCTL and stored in &drm_connector.modes. To ensure
* that modes are filtered consistently put any CRTC constraints and
* limits checks into @mode_valid.
*
* RETURNS:
*
* 0 on success, -EINVAL if the state or the transition can't be
* supported, -ENOMEM on memory allocation failure and -EDEADLK if an
* attempt to obtain another state object ran into a &drm_modeset_lock
* deadlock.
*/
int (*atomic_check)(struct drm_crtc *crtc,
struct drm_atomic_state *state);
/**
* @atomic_begin:
*
* Drivers should prepare for an atomic update of multiple planes on
* a CRTC in this hook. Depending upon hardware this might be vblank
* evasion, blocking updates by setting bits or doing preparatory work
* for e.g. manual update display.
*
* This hook is called before any plane commit functions are called.
*
* Note that the power state of the display pipe when this function is
* called depends upon the exact helpers and calling sequence the driver
* has picked. See drm_atomic_helper_commit_planes() for a discussion of
* the tradeoffs and variants of plane commit helpers.
*
* This callback is used by the atomic modeset helpers and by the
* transitional plane helpers, but it is optional.
*/
void (*atomic_begin)(struct drm_crtc *crtc,
struct drm_atomic_state *state);
/**
* @atomic_flush:
*
* Drivers should finalize an atomic update of multiple planes on
* a CRTC in this hook. Depending upon hardware this might include
* checking that vblank evasion was successful, unblocking updates by
* setting bits or setting the GO bit to flush out all updates.
*
* Simple hardware or hardware with special requirements can commit and
* flush out all updates for all planes from this hook and forgo all the
* other commit hooks for plane updates.
*
* This hook is called after any plane commit functions are called.
*
* Note that the power state of the display pipe when this function is
* called depends upon the exact helpers and calling sequence the driver
* has picked. See drm_atomic_helper_commit_planes() for a discussion of
* the tradeoffs and variants of plane commit helpers.
*
* This callback is used by the atomic modeset helpers and by the
* transitional plane helpers, but it is optional.
*/
void (*atomic_flush)(struct drm_crtc *crtc,
struct drm_atomic_state *state);

/**
* @atomic_enable:
*
* This callback should be used to enable the CRTC. With the atomic
* drivers it is called before all encoders connected to this CRTC are
* enabled through the encoder's own &drm_encoder_helper_funcs.enable
* hook. If that sequence is too simple drivers can just add their own
* hooks and call it from this CRTC callback here by looping over all
* encoders connected to it using for_each_encoder_on_crtc().
*
* This hook is used only by atomic helpers, for symmetry with
* @atomic_disable. Atomic drivers don't need to implement it if there's
* no need to enable anything at the CRTC level. To ensure that runtime
* PM handling (using either DPMS or the new "ACTIVE" property) works
* @atomic_enable must be the inverse of @atomic_disable for atomic
* drivers.
*
* This function is optional.
*/
void (*atomic_enable)(struct drm_crtc *crtc,
struct drm_atomic_state *state);

/**
* @atomic_disable:
*
* This callback should be used to disable the CRTC. With the atomic
* drivers it is called after all encoders connected to this CRTC have
* been shut off already using their own
* &drm_encoder_helper_funcs.disable hook. If that sequence is too
* simple drivers can just add their own hooks and call it from this
* CRTC callback here by looping over all encoders connected to it using
* for_each_encoder_on_crtc().
*
* This hook is used only by atomic helpers. Atomic drivers don't
* need to implement it if there's no need to disable anything at the
* CRTC level.
*
* This function is optional.
*/
void (*atomic_disable)(struct drm_crtc *crtc,
struct drm_atomic_state *state);

/**
* @get_scanout_position:
*
* Called by vblank timestamping code.
*
* Returns the current display scanout position from a CRTC and an
* optional accurate ktime_get() timestamp of when the position was
* measured. Note that this is a helper callback which is only used
* if a driver uses drm_crtc_vblank_helper_get_vblank_timestamp()
* for the @drm_crtc_funcs.get_vblank_timestamp callback.
*
* Parameters:
*
* crtc:
* The CRTC.
* in_vblank_irq:
* True when called from drm_crtc_handle_vblank(). Some drivers
* need to apply some workarounds for gpu-specific vblank irq
* quirks if the flag is set.
* vpos:
* Target location for current vertical scanout position.
* hpos:
* Target location for current horizontal scanout position.
* stime:
* Target location for timestamp taken immediately before
* scanout position query. Can be NULL to skip timestamp.
* etime:
* Target location for timestamp taken immediately after
* scanout position query. Can be NULL to skip timestamp.
* mode:
* Current display timings.
*
* Returns vpos as a positive number while in active scanout area.
* Returns vpos as a negative number inside vblank, counting the number
* of scanlines to go until end of vblank, e.g., -1 means "one scanline
* until start of active scanout / end of vblank."
*
* Returns:
*
* True on success, false if a reliable scanout position counter could
* not be read out.
*/
bool (*get_scanout_position)(struct drm_crtc *crtc,
bool in_vblank_irq, int *vpos, int *hpos,
ktime_t *stime, ktime_t *etime,
const struct drm_display_mode *mode);
};

其中:

  • dpms:电源管理接口,一般由drm_helper_connector_dpms调用;

  • prepare:为modeset做准备,一般就是调用dpms接口关闭crtc

  • commit:和prepare接口对应,是在modeset完成后,调用该接口来enable crtc;

  • drm_mode_status :检查display mode的有效性, drm_helper_probe_single_connector_modesdrm_atomic_helper_check_modeset会调用到;

  • disable:关闭crtc

  • atomic_check:检查待更新的drm_crtc_state

  • mode_set:这个回调函数被legacy CRTC helpers用来设置新的模式、位置和framebuffer

三、核心API

3.1 drm_crtc_init_with_planes

drm_crtc_init_with_planes函数使用指定的primary and cursor planes初始化的crtc对象,定义在drivers/gpu/drm/drm_crtc.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* drm_crtc_init_with_planes - Initialise a new CRTC object with
* specified primary and cursor planes.
* @dev: DRM device
* @crtc: CRTC object to init
* @primary: Primary plane for CRTC
* @cursor: Cursor plane for CRTC
* @funcs: callbacks for the new CRTC
* @name: printf style format string for the CRTC name, or NULL for default name
*
* Inits a new object created as base part of a driver crtc object. Drivers
* should use this function instead of drm_crtc_init(), which is only provided
* for backwards compatibility with drivers which do not yet support universal
* planes). For really simple hardware which has only 1 plane look at
* drm_simple_display_pipe_init() instead.
* The &drm_crtc_funcs.destroy hook should call drm_crtc_cleanup() and kfree()
* the crtc structure. The crtc structure should not be allocated with
* devm_kzalloc().
*
* The @primary and @cursor planes are only relevant for legacy uAPI, see
* &drm_crtc.primary and &drm_crtc.cursor.
*
* Note: consider using drmm_crtc_alloc_with_planes() or
* drmm_crtc_init_with_planes() instead of drm_crtc_init_with_planes()
* to let the DRM managed resource infrastructure take care of cleanup
* and deallocation.
*
* Returns:
* Zero on success, error code on failure.
*/
int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
struct drm_plane *primary, // primary plane
struct drm_plane *cursor, // cursor plane
const struct drm_crtc_funcs *funcs, // crtc回调函数,重点关注
const char *name, ...)
{
va_list ap;
int ret;

WARN_ON(!funcs->destroy);

va_start(ap, name);
ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
name, ap);
va_end(ap);

return ret;
}

drm_crtc_init_with_planes功能是由__drm_crtc_init_with_planes实现的;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* DOC: standard CRTC properties
*
* DRM CRTCs have a few standardized properties:
*
* ACTIVE:
* Atomic property for setting the power state of the CRTC. When set to 1
* the CRTC will actively display content. When set to 0 the CRTC will be
* powered off. There is no expectation that user-space will reset CRTC
* resources like the mode and planes when setting ACTIVE to 0.
*
* User-space can rely on an ACTIVE change to 1 to never fail an atomic
* test as long as no other property has changed. If a change to ACTIVE
* fails an atomic test, this is a driver bug. For this reason setting
* ACTIVE to 0 must not release internal resources (like reserved memory
* bandwidth or clock generators).
*
* Note that the legacy DPMS property on connectors is internally routed
* to control this property for atomic drivers.
* MODE_ID:
* Atomic property for setting the CRTC display timings. The value is the
* ID of a blob containing the DRM mode info. To disable the CRTC,
* user-space must set this property to 0.
*
* Setting MODE_ID to 0 will release reserved resources for the CRTC.
* SCALING_FILTER:
* Atomic property for setting the scaling filter for CRTC scaler
*
* The value of this property can be one of the following:
*
* Default:
* Driver's default scaling filter
* Nearest Neighbor:
* Nearest Neighbor scaling filter
*/

__printf(6, 0)
static int __drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
struct drm_plane *primary,
struct drm_plane *cursor,
const struct drm_crtc_funcs *funcs, // 重点关注
const char *name, va_list ap)
{
// 获取drm模式配置
struct drm_mode_config *config = &dev->mode_config;
int ret;

// plane类型校验
WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);

/* crtc index is used with 32bit bitmasks */
if (WARN_ON(config->num_crtc >= 32))
return -EINVAL;

WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
(!funcs->atomic_destroy_state ||
!funcs->atomic_duplicate_state));

// 保存drm设备
crtc->dev = dev;
// 设置crtc操作函数
crtc->funcs = funcs;

INIT_LIST_HEAD(&crtc->commit_list);
spin_lock_init(&crtc->commit_lock);

drm_modeset_lock_init(&crtc->mutex);

// 基于基数树为crtc分配一个唯一id,根绝该id可以定位到该crtc
ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
if (ret)
return ret;
// 设置名称
if (name) {
crtc->name = kvasprintf(GFP_KERNEL, name, ap);
} else {
crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
drm_num_crtcs(dev));
}
if (!crtc->name) {
drm_mode_object_unregister(dev, &crtc->base);
return -ENOMEM;
}

crtc->fence_context = dma_fence_context_alloc(1);
spin_lock_init(&crtc->fence_lock);
snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
"CRTC:%d-%s", crtc->base.id, crtc->name);

// 初始化properties
crtc->base.properties = &crtc->properties;

// 将当前crtc节点添加到链表config->crtc_list中
list_add_tail(&crtc->head, &config->crtc_list);
// 初始化crtc索引
crtc->index = config->num_crtc++;

crtc->primary = primary;
crtc->cursor = cursor;
if (primary && !primary->possible_crtcs)
primary->possible_crtcs = drm_crtc_mask(crtc);
if (cursor && !cursor->possible_crtcs)
cursor->possible_crtcs = drm_crtc_mask(crtc);

// 调试相关的
ret = drm_crtc_crc_init(crtc);
if (ret) {
drm_mode_object_unregister(dev, &crtc->base);
return ret;
}
// 附加一些属性
if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
drm_object_attach_property(&crtc->base, config->prop_active, 0);
drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
drm_object_attach_property(&crtc->base,
config->prop_out_fence_ptr, 0);
drm_object_attach_property(&crtc->base,
config->prop_vrr_enabled, 0);
}

return 0;
}

3.2 drm_crtc_crc_init

drm_crtc_crc_init函数用于初始化的crtc对象,定义在drivers/gpu/drm/drm_crtc.c

1
2
3
4
5
6
7
8
9
10
11
static int drm_crtc_crc_init(struct drm_crtc *crtc)
{
#ifdef CONFIG_DEBUG_FS
spin_lock_init(&crtc->crc.lock);
init_waitqueue_head(&crtc->crc.wq);
crtc->crc.source = kstrdup("auto", GFP_KERNEL);
if (!crtc->crc.source)
return -ENOMEM;
#endif
return 0;
}

3.3 drm_crtc_helper_add

drm_crtc_helper_add函数用于设置crtc的辅助函数helper_private,定义在include/drm/drm_modeset_helper_vtables.h

1
2
3
4
5
6
7
8
9
10
/**
* drm_crtc_helper_add - sets the helper vtable for a crtc
* @crtc: DRM CRTC
* @funcs: helper vtable to set for @crtc
*/
static inline void drm_crtc_helper_add(struct drm_crtc *crtc,
const struct drm_crtc_helper_funcs *funcs)
{
crtc->helper_private = funcs;
}

四、Rockchip vop驱动

这里我们介绍一下Rochchip DRM驱动中与vop相关的实现,具体实现文件:

  • drivers/gpu/drm/rockchip/rockchip_drm_vop.c
  • drivers/gpu/drm/rockchip/rockchip_vop_reg.c
  • drivers/gpu/drm/rockchip/rockchip_drm_vop.h;
  • drivers/gpu/drm/rockchip/rockchip_vop_reg.h

4.1 DRM驱动入口函数回顾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static int __init rockchip_drm_init(void)
{
int ret;


if (drm_firmware_drivers_only())
return -ENODEV;

// 1. 根据配置来决定是否添加xxx_xxx_driver到数组rockchip_sub_drivers
num_rockchip_sub_drivers = 0;
ADD_ROCKCHIP_SUB_DRIVER(vop_platform_driver, CONFIG_ROCKCHIP_VOP);
......

// 2. 注册多个platform driver
ret = platform_register_drivers(rockchip_sub_drivers,
num_rockchip_sub_drivers);
if (ret)
return ret;

// 3. 注册rockchip_drm_platform_driver
ret = platform_driver_register(&rockchip_drm_platform_driver);
if (ret)
goto err_unreg_drivers;

return 0;
......
}

其中:

1
ADD_ROCKCHIP_SUB_DRIVER(vop_platform_driver, CONFIG_ROCKCHIP_VOP);

会将vop_platform_driver保存到rockchip_sub_drivers数组中。

并调用platform_register_drivers遍历rockchip_sub_drivers数组,多次调用platform_driver_register注册platform driver

4.2 vop_platform_driver

vop_platform_driver定义在drivers/gpu/drm/rockchip/rockchip_vop_reg.c

1
2
3
4
5
6
7
8
struct platform_driver vop_platform_driver = {
.probe = vop_probe,
.remove = vop_remove,
.driver = {
.name = "rockchip-vop",
.of_match_table = vop_driver_dt_match,
},
};
4.2.1 of_match_table

其中of_match_table用于设备树匹配,匹配设备树中compatible = "rockchip,rk3399-vop-big"或者compatible = "rockchip,rk3399-vop-lit"的设备节点;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
static const struct of_device_id vop_driver_dt_match[] = {
{ .compatible = "rockchip,rk3036-vop",
.data = &rk3036_vop },
{ .compatible = "rockchip,rk3126-vop",
.data = &rk3126_vop },
{ .compatible = "rockchip,px30-vop-big",
.data = &px30_vop_big },
{ .compatible = "rockchip,px30-vop-lit",
.data = &px30_vop_lit },
{ .compatible = "rockchip,rk3066-vop",
.data = &rk3066_vop },
{ .compatible = "rockchip,rk3188-vop",
.data = &rk3188_vop },
{ .compatible = "rockchip,rk3288-vop",
.data = &rk3288_vop },
{ .compatible = "rockchip,rk3368-vop",
.data = &rk3368_vop },
{ .compatible = "rockchip,rk3366-vop",
.data = &rk3366_vop },
{ .compatible = "rockchip,rk3399-vop-big",
.data = &rk3399_vop_big },
{ .compatible = "rockchip,rk3399-vop-lit",
.data = &rk3399_vop_lit },
{ .compatible = "rockchip,rk3228-vop",
.data = &rk3228_vop },
{ .compatible = "rockchip,rk3328-vop",
.data = &rk3328_vop },
{},
};
4.2.2 vop_probe

plaftrom总线设备驱动模型中,我们知道当内核中有platform设备和platform驱动匹配,会调用到platform_driver里的成员.probe,在这里就是vop_probe函数;

1
2
3
4
5
6
7
8
9
10
11
12
static int vop_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;

// 未使用设备树 退出
if (!dev->of_node) {
DRM_DEV_ERROR(dev, "can't find vop devices\n");
return -ENODEV;
}

return component_add(dev, &vop_component_ops);
}

这里代码很简单,就是为设备pdev->dev向系统注册一个component,其中组件可执行的初始化操作被设置为了vop_component_ops,其定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c

1
2
3
4
const struct component_ops vop_component_ops = {
.bind = vop_bind,
.unbind = vop_unbind,
};

我们需要重点关注bind函数的实现,这个函数内容较多我们单独小节介绍。

4.3 vopb设备节点

vopb设备节点为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
vopb: vop@ff900000 {
compatible = "rockchip,rk3399-vop-big";
reg = <0x0 0xff900000 0x0 0x2000>, <0x0 0xff902000 0x0 0x1000>;
interrupts = <GIC_SPI 118 IRQ_TYPE_LEVEL_HIGH 0>;
assigned-clocks = <&cru ACLK_VOP0>, <&cru HCLK_VOP0>;
assigned-clock-rates = <400000000>, <100000000>;
clocks = <&cru ACLK_VOP0>, <&cru DCLK_VOP0>, <&cru HCLK_VOP0>;
clock-names = "aclk_vop", "dclk_vop", "hclk_vop";
iommus = <&vopb_mmu>;
power-domains = <&power RK3399_PD_VOPB>;
resets = <&cru SRST_A_VOP0>, <&cru SRST_H_VOP0>, <&cru SRST_D_VOP0>;
reset-names = "axi", "ahb", "dclk";
status = "disabled";

vopb_out: port {
#address-cells = <1>;
#size-cells = <0>;

vopb_out_edp: endpoint@0 {
reg = <0>;
remote-endpoint = <&edp_in_vopb>;
};

vopb_out_mipi: endpoint@1 {
reg = <1>;
remote-endpoint = <&mipi_in_vopb>;
};

vopb_out_hdmi: endpoint@2 {
reg = <2>;
remote-endpoint = <&hdmi_in_vopb>;
};

vopb_out_mipi1: endpoint@3 {
reg = <3>;
remote-endpoint = <&mipi1_in_vopb>;
};

vopb_out_dp: endpoint@4 {
reg = <4>;
remote-endpoint = <&dp_in_vopb>;
};
};
};

vop_bind

vop_bind函数定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c,涉及到对vop设备节点的解析,咱们以vopb设备节点为例进行分析;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
static int vop_bind(struct device *dev, struct device *master, void *data)
{
// 1. 获取platform_device
struct platform_device *pdev = to_platform_device(dev);
const struct vop_data *vop_data;
struct drm_device *drm_dev = data;
struct vop *vop;
struct resource *res;
int ret, irq;

// 2. 得到rk3399_vop_big
vop_data = of_device_get_match_data(dev);
if (!vop_data)
return -ENODEV;

/* Allocate vop struct and its vop_win array,初始化vop win */
vop = devm_kzalloc(dev, struct_size(vop, win, vop_data->win_size),
GFP_KERNEL);
if (!vop)
return -ENOMEM;

vop->dev = dev;
vop->data = vop_data;
vop->drm_dev = drm_dev;

// 设置驱动数据为vop
dev_set_drvdata(dev, vop);

// 3. 初始化vop win
vop_win_init(vop);

// 4. 获取第一个内存资源,即<0x0 0xff900000 0x0 0x2000>; VOP_BIG相关寄存器基地址
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

// 将VOP_BIG相关寄存器起始物理地址映射到虚拟地址,并返回虚拟地址
vop->regs = devm_ioremap_resource(dev, res);
if (IS_ERR(vop->regs))
return PTR_ERR(vop->regs);
vop->len = resource_size(res);

// 5. 获取第二个内存资源,即<0x0 0xff902000 0x0 0x1000>; LUT相关寄存器基地址
res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
if (res) {
if (vop_data->lut_size != 1024 && vop_data->lut_size != 256) {
DRM_DEV_ERROR(dev, "unsupported gamma LUT size %d\n", vop_data->lut_size);
return -EINVAL;
}
// LUT相关寄存器起始物理地址映射到虚拟地址,并返回虚拟地址
vop->lut_regs = devm_ioremap_resource(dev, res);
if (IS_ERR(vop->lut_regs))
return PTR_ERR(vop->lut_regs);
}

vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
if (!vop->regsbak)
return -ENOMEM;


// 6. 获取第1个IRQ编号 interrupts = <GIC_SPI 118 IRQ_TYPE_LEVEL_HIGH 0>;
irq = platform_get_irq(pdev, 0);
if (irq < 0) {
DRM_DEV_ERROR(dev, "cannot find irq for vop\n");
return irq;
}
vop->irq = (unsigned int)irq;

// 初始化自旋锁和互斥锁
spin_lock_init(&vop->reg_lock);
spin_lock_init(&vop->irq_lock);
mutex_init(&vop->vop_lock);

// 7. 创建crtc对象,并和plane关联在一起
ret = vop_create_crtc(vop);
if (ret)
return ret;

// 电源管理相关
pm_runtime_enable(&pdev->dev);

// 8. vop初始化
ret = vop_initial(vop);
if (ret < 0) {
DRM_DEV_ERROR(&pdev->dev,
"cannot initial vop dev - err %d\n", ret);
goto err_disable_pm_runtime;
}

// 9. 申请中断,中断处理函数设置为vop_isr
ret = devm_request_irq(dev, vop->irq, vop_isr,
IRQF_SHARED, dev_name(dev), vop);
if (ret)
goto err_disable_pm_runtime;

// 未设置
if (vop->data->feature & VOP_FEATURE_INTERNAL_RGB) {
vop->rgb = rockchip_rgb_init(dev, &vop->crtc, vop->drm_dev);
if (IS_ERR(vop->rgb)) {
ret = PTR_ERR(vop->rgb);
goto err_disable_pm_runtime;
}
}

// 10. dma初始化
rockchip_drm_dma_init_device(drm_dev, dev);

return 0;

err_disable_pm_runtime:
pm_runtime_disable(&pdev->dev);
vop_destroy_crtc(vop);
return ret;
}

这里我们以设备节点vopb为例,对这段代码进行分析,主要流程如下:

(1) 调用to_platform_device获取platform device,设备节点为vopb

(2) 调用of_device_get_match_data获取与特定设备匹配的数据,这里获取到的数据为rk3399_vop_big;

(3) 调用vop_win_init初始化vop win

(4) 首先调用platform_get_resource(pdev, IORESOURCE_MEM, 0)获取第一个内存资源,即:

1
<0x0 0xff900000 0x0 0x2000>

0xff900000VOP_BIG相关寄存器基地址;

接着调用devm_ioremap_resource(dev, res)VOP_BIG相关寄存器起始物理地址映射到虚拟地址,并返回虚拟地址;

(5) 调用platform_get_resource(pdev, IORESOURCE_MEM, 1)获取第二个内存资源,即:

1
<0x0 0xff902000 0x0 0x1000>

0xff902000LUT相关寄存器基地址;

接着调用devm_ioremap_resource(dev, res)LUT相关寄存器起始物理地址映射到虚拟地址,并返回虚拟地址;

(6) 调用platform_get_irq(pdev, 0) 获取第1个IRQ编号:

1
interrupts = <GIC_SPI 118 IRQ_TYPE_LEVEL_HIGH 0>

(7) 调用vop_create_crtc(vop)初始化crtc对象,并和plane关联在一起;

(8) 调用vop_initial(vop)进行vop初始化;

(9) 调用devm_request_irq(dev, vop->irq, vop_isr, IRQF_SHARED, dev_name(dev), vop)申请中断,中断处理函数设置为vop_isr

(10) 调用rockchip_drm_dma_init_device(drm_dev, dev)进行dma相关的初始化工作;

5.1 struct vop

struct vop定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c,用于表示显示处理器;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 描述vop win
struct vop_win {
struct drm_plane base; // 对应的struct drm_plane数据结构
const struct vop_win_data *data; // vop win data
const struct vop_win_yuv2yuv_data *yuv2yuv_data;
struct vop *vop; // 指向vop
};


// 描述显示处理器vop
struct vop {
struct drm_crtc crtc;
struct device *dev;
struct drm_device *drm_dev;
bool is_enabled;

struct completion dsp_hold_completion;
unsigned int win_enabled;

/* protected by dev->event_lock */
struct drm_pending_vblank_event *event;

struct drm_flip_work fb_unref_work;
unsigned long pending;

struct completion line_flag_completion;

const struct vop_data *data;

uint32_t *regsbak;
void __iomem *regs;
void __iomem *lut_regs;

/* physical map length of vop register */
uint32_t len;

/* one time only one process allowed to config the register */
spinlock_t reg_lock;
/* lock vop irq reg */
spinlock_t irq_lock;
/* protects crtc enable/disable */
struct mutex vop_lock;

unsigned int irq;

/* vop AHP clk */
struct clk *hclk;
/* vop dclk */
struct clk *dclk;
/* vop share memory frequency */
struct clk *aclk;

/* vop dclk reset */
struct reset_control *dclk_rst;

/* optional internal rgb encoder */
struct rockchip_rgb *rgb;

struct vop_win win[];
};

其中:

  • crtc:指向一个struct drm_crtc;
  • dev:这是一个指向设备结构体的指针,通常用于表示与vop关联的设备;
  • data :存储与vop相关的数据;
  • drm_dev :指向一个struct drm_device,表示当前drm设备;
  • regsvop相关寄存器起始虚拟地址;
  • lenvop相关寄存器所占内存长度,单位字节;
  • regsbak :指向一块内存,用于备份vop相关寄存器;
  • irq :存储vop中断IRQ编号;
  • hclkdclkaclkvop相关的各种时钟;
  • rgb:这是一个用于管理RGB编码器的结构体指针;
  • win:这是一个表示vop win的结构体数组,比如RK399 vop 支持4个图层,因此该该数组有4个元素;

5.2 struct vop_data

struct vop_data定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.h,用于描述与vop相关的数据,主要是一些寄存器信息;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct vop_data {
uint32_t version;
const struct vop_intr *intr;
const struct vop_common *common;
const struct vop_misc *misc;
const struct vop_modeset *modeset; // 模式配置相关寄存器信息
const struct vop_output *output; // 输出配置相关寄存器信息
const struct vop_afbc *afbc;
const struct vop_win_yuv2yuv_data *win_yuv2yuv;
const struct vop_win_data *win; // vop win配置相关寄存器信息
unsigned int win_size; // vop win个数
unsigned int lut_size;

#define VOP_FEATURE_OUTPUT_RGB10 BIT(0)
#define VOP_FEATURE_INTERNAL_RGB BIT(1)
u64 feature;
};

rk3399_vop_big为例,定义在drivers/gpu/drm/rockchip/rockchip_vop_reg.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static const struct vop_data rk3399_vop_big = {
.version = VOP_VERSION(3, 5),
.feature = VOP_FEATURE_OUTPUT_RGB10,
.intr = &rk3366_vop_intr,
.common = &rk3399_common,
.modeset = &rk3288_modeset, // 模式配置相关寄存器信息
.output = &rk3399_output, // 输出配置相关寄存器信息
.afbc = &rk3399_vop_afbc,
.misc = &rk3368_misc,
.win = rk3399_vop_win_data, // vop win配置相关寄存器信息
.win_size = ARRAY_SIZE(rk3399_vop_win_data), // vop win个数
.win_yuv2yuv = rk3399_vop_big_win_yuv2yuv_data,
.lut_size = 1024,
};

其中rk3366_vop_intrrk3288_modesetrk3399_outputrk3399_common等配置了大量的寄存器信息。

5.2.1 rk3288_modeset

rk3288_modeset定义如下:

1
2
3
4
5
6
7
8
static const struct vop_modeset rk3288_modeset = {
.htotal_pw = VOP_REG(RK3288_DSP_HTOTAL_HS_END, 0x1fff1fff, 0), // horizontal total size配置
.hact_st_end = VOP_REG(RK3288_DSP_HACT_ST_END, 0x1fff1fff, 0),
.vtotal_pw = VOP_REG(RK3288_DSP_VTOTAL_VS_END, 0x1fff1fff, 0), // vertical total size配置
.vact_st_end = VOP_REG(RK3288_DSP_VACT_ST_END, 0x1fff1fff, 0),
.hpost_st_end = VOP_REG(RK3288_POST_DSP_HACT_INFO, 0x1fff1fff, 0),
.vpost_st_end = VOP_REG(RK3288_POST_DSP_VACT_INFO, 0x1fff1fff, 0),
};

VOP_REG用于定义寄存器信息:

1
2
3
4
5
6
7
8
9
10
11
#define _VOP_REG(off, _mask, _shift, _write_mask, _relaxed) \
{ \
.offset = off, \
.mask = _mask, \
.shift = _shift, \
.write_mask = _write_mask, \
.relaxed = _relaxed, \
}

#define VOP_REG(off, _mask, _shift) \
_VOP_REG(off, _mask, _shift, false, true)
5.2.2 rk3399_common

rk3399_common定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static const struct vop_common rk3399_common = {
.standby = VOP_REG_SYNC(RK3399_SYS_CTRL, 0x1, 22),
.gate_en = VOP_REG(RK3399_SYS_CTRL, 0x1, 23),
.mmu_en = VOP_REG(RK3399_SYS_CTRL, 0x1, 20),
.dither_down_sel = VOP_REG(RK3399_DSP_CTRL1, 0x1, 4),
.dither_down_mode = VOP_REG(RK3399_DSP_CTRL1, 0x1, 3),
.dither_down_en = VOP_REG(RK3399_DSP_CTRL1, 0x1, 2),
.pre_dither_down = VOP_REG(RK3399_DSP_CTRL1, 0x1, 1),
.dither_up = VOP_REG(RK3399_DSP_CTRL1, 0x1, 6),
.dsp_lut_en = VOP_REG(RK3399_DSP_CTRL1, 0x1, 0),
.update_gamma_lut = VOP_REG(RK3399_DSP_CTRL1, 0x1, 7),
.lut_buffer_index = VOP_REG(RK3399_DBG_POST_REG1, 0x1, 1),
.data_blank = VOP_REG(RK3399_DSP_CTRL0, 0x1, 19),
.dsp_blank = VOP_REG(RK3399_DSP_CTRL0, 0x3, 18),
.out_mode = VOP_REG(RK3399_DSP_CTRL0, 0xf, 0),
.cfg_done = VOP_REG_SYNC(RK3399_REG_CFG_DONE, 0x1, 0),
};
5.2.3 rk3399_vop_wn_data

rk3399_vop_wn_data定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
static const struct vop_win_phy rk3399_win01_data = {
.scl = &rk3288_win_full_scl,
.data_formats = formats_win_full,
.nformats = ARRAY_SIZE(formats_win_full),
.format_modifiers = format_modifiers_win_full_afbc,
.enable = VOP_REG(RK3288_WIN0_CTRL0, 0x1, 0),
.format = VOP_REG(RK3288_WIN0_CTRL0, 0x7, 1),
.rb_swap = VOP_REG(RK3288_WIN0_CTRL0, 0x1, 12),
.uv_swap = VOP_REG(RK3288_WIN0_CTRL0, 0x1, 15),
.x_mir_en = VOP_REG(RK3288_WIN0_CTRL0, 0x1, 21), // 水平方向上反转(翻转)使能
.y_mir_en = VOP_REG(RK3288_WIN0_CTRL0, 0x1, 22), // 垂直方向上反转(翻转)使能
.act_info = VOP_REG(RK3288_WIN0_ACT_INFO, 0x1fff1fff, 0),
.dsp_info = VOP_REG(RK3288_WIN0_DSP_INFO, 0x0fff0fff, 0),
.dsp_st = VOP_REG(RK3288_WIN0_DSP_ST, 0x1fff1fff, 0),
.yrgb_mst = VOP_REG(RK3288_WIN0_YRGB_MST, 0xffffffff, 0),
.uv_mst = VOP_REG(RK3288_WIN0_CBR_MST, 0xffffffff, 0),
.yrgb_vir = VOP_REG(RK3288_WIN0_VIR, 0x3fff, 0),
.uv_vir = VOP_REG(RK3288_WIN0_VIR, 0x3fff, 16),
.src_alpha_ctl = VOP_REG(RK3288_WIN0_SRC_ALPHA_CTRL, 0xff, 0),
.dst_alpha_ctl = VOP_REG(RK3288_WIN0_DST_ALPHA_CTRL, 0xff, 0),
.channel = VOP_REG(RK3288_WIN0_CTRL2, 0xff, 0),
};

......

/*
* rk3399 vop big windows register layout is same as rk3288, but we
* have a separate rk3399 win data array here so that we can advertise
* AFBC on the primary plane.
*/
static const struct vop_win_data rk3399_vop_win_data[] = {
{ .base = 0x00, .phy = &rk3399_win01_data,
.type = DRM_PLANE_TYPE_PRIMARY }, // primary plane
{ .base = 0x40, .phy = &rk3368_win01_data,
.type = DRM_PLANE_TYPE_OVERLAY }, // overlay plane
{ .base = 0x00, .phy = &rk3368_win23_data,
.type = DRM_PLANE_TYPE_OVERLAY }, // overlay plane
{ .base = 0x50, .phy = &rk3368_win23_data,
.type = DRM_PLANE_TYPE_CURSOR }, // cursor plane
};

RK3399vop包含4个图层,因此这里存放就是RK3399的4个图层配置相关寄存器的信息。

5.2.4 rk3399_vop_big_win_yuv2yuv_data

rk3399_vop_big_win_yuv2yuv_data定义如下:

1
2
3
4
5
6
7
8
9
static const struct vop_win_yuv2yuv_data rk3399_vop_big_win_yuv2yuv_data[] = {
{ .base = 0x00, .phy = &rk3399_yuv2yuv_win01_data,
.y2r_en = VOP_REG(RK3399_YUV2YUV_WIN, 0x1, 1) },
{ .base = 0x60, .phy = &rk3399_yuv2yuv_win01_data,
.y2r_en = VOP_REG(RK3399_YUV2YUV_WIN, 0x1, 9) },
{ .base = 0xC0, .phy = &rk3399_yuv2yuv_win23_data },
{ .base = 0x120, .phy = &rk3399_yuv2yuv_win23_data },

};

5.3 rk3399_output

rk3399_output定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static const struct vop_output rk3399_output = {
.dp_dclk_pol = VOP_REG(RK3399_DSP_CTRL1, 0x1, 19),
.rgb_dclk_pol = VOP_REG(RK3368_DSP_CTRL1, 0x1, 19),
.hdmi_dclk_pol = VOP_REG(RK3368_DSP_CTRL1, 0x1, 23),
.edp_dclk_pol = VOP_REG(RK3368_DSP_CTRL1, 0x1, 27),
.mipi_dclk_pol = VOP_REG(RK3368_DSP_CTRL1, 0x1, 31),
// 接口引脚极性相关寄存器位
.dp_pin_pol = VOP_REG(RK3399_DSP_CTRL1, 0x7, 16),
.rgb_pin_pol = VOP_REG(RK3368_DSP_CTRL1, 0x7, 16),
.hdmi_pin_pol = VOP_REG(RK3368_DSP_CTRL1, 0x7, 20),
.edp_pin_pol = VOP_REG(RK3368_DSP_CTRL1, 0x7, 24),
.mipi_pin_pol = VOP_REG(RK3368_DSP_CTRL1, 0x7, 28),
// 接口使能相关寄存器位
.dp_en = VOP_REG(RK3399_SYS_CTRL, 0x1, 11), // dp接口输出使能
.rgb_en = VOP_REG(RK3288_SYS_CTRL, 0x1, 12), // rgb接口输出使能
.hdmi_en = VOP_REG(RK3288_SYS_CTRL, 0x1, 13), // hdmi接口输出使能
.edp_en = VOP_REG(RK3288_SYS_CTRL, 0x1, 14), // edp接口输出使能
.mipi_en = VOP_REG(RK3288_SYS_CTRL, 0x1, 15), // mipi接口输出使能
.mipi_dual_channel_en = VOP_REG(RK3288_SYS_CTRL, 0x1, 3), // mipi1接口输出使能
};

这里以如下代码为例:

1
2
3
#define RK3288_SYS_CTRL                         0x0008

.hdmi_en = VOP_REG(RK3288_SYS_CTRL, 0x1, 13),

其中:

  • RK3288_SYS_CTRLoffset,表示寄存器偏移,相对vop基址,
  • 0x1mask:表示掩码;
  • 13shift,表示偏移位;

因此,总结下来就是地址为0xff900000+0x0008=0xff900008的寄存器的位13用于hdmi接口的使能。

不幸的是在RK3399 datasheet没有找到与vop相关的任何信息,我猜测Rockchip应该是vop这部分并不对用户开放。

不过我们可以通过drivers/gpu/drm/rockchip/rockchip_vop_reg.h中的宏定义猜测出vop包含了哪些寄存器;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* rk3399 register definition */
#define RK3399_REG_CFG_DONE 0x0000
#define RK3399_VERSION_INFO 0x0004
#define RK3399_SYS_CTRL 0x0008
#define RK3399_SYS_CTRL1 0x000c
#define RK3399_DSP_CTRL0 0x0010
#define RK3399_DSP_CTRL1 0x0014
#define RK3399_DSP_BG 0x0018
#define RK3399_MCU_CTRL 0x001c
#define RK3399_WB_CTRL0 0x0020
#define RK3399_WB_CTRL1 0x0024
#define RK3399_WB_YRGB_MST 0x0028
#define RK3399_WB_CBR_MST 0x002c
#define RK3399_WIN0_CTRL0 0x0030
#define RK3399_WIN0_CTRL1 0x0034
#define RK3399_WIN0_COLOR_KEY 0x0038
#define RK3399_WIN0_VIR 0x003c
#define RK3399_WIN0_YRGB_MST 0x0040
#define RK3399_WIN0_CBR_MST 0x0044
#define RK3399_WIN0_ACT_INFO 0x0048
#define RK3399_WIN0_DSP_INFO 0x004c
#define RK3399_WIN0_DSP_ST 0x0050
#define RK3399_WIN0_SCL_FACTOR_YRGB 0x0054
#define RK3399_WIN0_SCL_FACTOR_CBR 0x0058
#define RK3399_WIN0_SCL_OFFSET 0x005c
#define RK3399_WIN0_SRC_ALPHA_CTRL 0x0060
#define RK3399_WIN0_DST_ALPHA_CTRL 0x0064
#define RK3399_WIN0_FADING_CTRL 0x0068
#define RK3399_WIN0_CTRL2 0x006c
#define RK3399_WIN1_CTRL0 0x0070
#define RK3399_WIN1_CTRL1 0x0074
#define RK3399_WIN1_COLOR_KEY 0x0078
#define RK3399_WIN1_VIR 0x007c
#define RK3399_WIN1_YRGB_MST 0x0080
#define RK3399_WIN1_CBR_MST 0x0084
#define RK3399_WIN1_ACT_INFO 0x0088
#define RK3399_WIN1_DSP_INFO 0x008c
#define RK3399_WIN1_DSP_ST 0x0090
#define RK3399_WIN1_SCL_FACTOR_YRGB 0x0094
#define RK3399_WIN1_SCL_FACTOR_CBR 0x0098
#define RK3399_WIN1_SCL_OFFSET 0x009c
#define RK3399_WIN1_SRC_ALPHA_CTRL 0x00a0
#define RK3399_WIN1_DST_ALPHA_CTRL 0x00a4
#define RK3399_WIN1_FADING_CTRL 0x00a8
#define RK3399_WIN1_CTRL2 0x00ac
#define RK3399_WIN2_CTRL0 0x00b0
#define RK3399_WIN2_CTRL1 0x00b4
#define RK3399_WIN2_VIR0_1 0x00b8
#define RK3399_WIN2_VIR2_3 0x00bc
#define RK3399_WIN2_MST0 0x00c0
#define RK3399_WIN2_DSP_INFO0 0x00c4
#define RK3399_WIN2_DSP_ST0 0x00c8
#define RK3399_WIN2_COLOR_KEY 0x00cc
#define RK3399_WIN2_MST1 0x00d0
#define RK3399_WIN2_DSP_INFO1 0x00d4
#define RK3399_WIN2_DSP_ST1 0x00d8
#define RK3399_WIN2_SRC_ALPHA_CTRL 0x00dc
#define RK3399_WIN2_MST2 0x00e0
#define RK3399_WIN2_DSP_INFO2 0x00e4
#define RK3399_WIN2_DSP_ST2 0x00e8
#define RK3399_WIN2_DST_ALPHA_CTRL 0x00ec
#define RK3399_WIN2_MST3 0x00f0
#define RK3399_WIN2_DSP_INFO3 0x00f4
#define RK3399_WIN2_DSP_ST3 0x00f8
#define RK3399_WIN2_FADING_CTRL 0x00fc
#define RK3399_WIN3_CTRL0 0x0100
#define RK3399_WIN3_CTRL1 0x0104
#define RK3399_WIN3_VIR0_1 0x0108
#define RK3399_WIN3_VIR2_3 0x010c
#define RK3399_WIN3_MST0 0x0110
#define RK3399_WIN3_DSP_INFO0 0x0114
#define RK3399_WIN3_DSP_ST0 0x0118
#define RK3399_WIN3_COLOR_KEY 0x011c
#define RK3399_WIN3_MST1 0x0120
#define RK3399_WIN3_DSP_INFO1 0x0124
#define RK3399_WIN3_DSP_ST1 0x0128
#define RK3399_WIN3_SRC_ALPHA_CTRL 0x012c
#define RK3399_WIN3_MST2 0x0130
#define RK3399_WIN3_DSP_INFO2 0x0134
#define RK3399_WIN3_DSP_ST2 0x0138
#define RK3399_WIN3_DST_ALPHA_CTRL 0x013c
#define RK3399_WIN3_MST3 0x0140
#define RK3399_WIN3_DSP_INFO3 0x0144
#define RK3399_WIN3_DSP_ST3 0x0148
#define RK3399_WIN3_FADING_CTRL 0x014c
#define RK3399_HWC_CTRL0 0x0150
#define RK3399_HWC_CTRL1 0x0154
#define RK3399_HWC_MST 0x0158
#define RK3399_HWC_DSP_ST 0x015c
#define RK3399_HWC_SRC_ALPHA_CTRL 0x0160
#define RK3399_HWC_DST_ALPHA_CTRL 0x0164
#define RK3399_HWC_FADING_CTRL 0x0168
#define RK3399_HWC_RESERVED1 0x016c
#define RK3399_POST_DSP_HACT_INFO 0x0170
#define RK3399_POST_DSP_VACT_INFO 0x0174
#define RK3399_POST_SCL_FACTOR_YRGB 0x0178
#define RK3399_POST_RESERVED 0x017c
#define RK3399_POST_SCL_CTRL 0x0180
#define RK3399_POST_DSP_VACT_INFO_F1 0x0184
#define RK3399_DSP_HTOTAL_HS_END 0x0188
#define RK3399_DSP_HACT_ST_END 0x018c
#define RK3399_DSP_VTOTAL_VS_END 0x0190
#define RK3399_DSP_VACT_ST_END 0x0194
#define RK3399_DSP_VS_ST_END_F1 0x0198
#define RK3399_DSP_VACT_ST_END_F1 0x019c
#define RK3399_PWM_CTRL 0x01a0
#define RK3399_PWM_PERIOD_HPR 0x01a4
#define RK3399_PWM_DUTY_LPR 0x01a8
#define RK3399_PWM_CNT 0x01ac
#define RK3399_BCSH_COLOR_BAR 0x01b0
#define RK3399_BCSH_BCS 0x01b4
#define RK3399_BCSH_H 0x01b8
#define RK3399_BCSH_CTRL 0x01bc
#define RK3399_CABC_CTRL0 0x01c0
#define RK3399_CABC_CTRL1 0x01c4
#define RK3399_CABC_CTRL2 0x01c8
#define RK3399_CABC_CTRL3 0x01cc
#define RK3399_CABC_GAUSS_LINE0_0 0x01d0
#define RK3399_CABC_GAUSS_LINE0_1 0x01d4
#define RK3399_CABC_GAUSS_LINE1_0 0x01d8
#define RK3399_CABC_GAUSS_LINE1_1 0x01dc
#define RK3399_CABC_GAUSS_LINE2_0 0x01e0
#define RK3399_CABC_GAUSS_LINE2_1 0x01e4
#define RK3399_FRC_LOWER01_0 0x01e8
#define RK3399_FRC_LOWER01_1 0x01ec
#define RK3399_FRC_LOWER10_0 0x01f0
#define RK3399_FRC_LOWER10_1 0x01f4
#define RK3399_FRC_LOWER11_0 0x01f8
#define RK3399_FRC_LOWER11_1 0x01fc
#define RK3399_AFBCD0_CTRL 0x0200
#define RK3399_AFBCD0_HDR_PTR 0x0204
#define RK3399_AFBCD0_PIC_SIZE 0x0208
#define RK3399_AFBCD0_STATUS 0x020c
#define RK3399_AFBCD1_CTRL 0x0220
#define RK3399_AFBCD1_HDR_PTR 0x0224
#define RK3399_AFBCD1_PIC_SIZE 0x0228
#define RK3399_AFBCD1_STATUS 0x022c
#define RK3399_AFBCD2_CTRL 0x0240
#define RK3399_AFBCD2_HDR_PTR 0x0244
#define RK3399_AFBCD2_PIC_SIZE 0x0248
#define RK3399_AFBCD2_STATUS 0x024c
#define RK3399_AFBCD3_CTRL 0x0260
#define RK3399_AFBCD3_HDR_PTR 0x0264
#define RK3399_AFBCD3_PIC_SIZE 0x0268
#define RK3399_AFBCD3_STATUS 0x026c
#define RK3399_INTR_EN0 0x0280
#define RK3399_INTR_CLEAR0 0x0284
#define RK3399_INTR_STATUS0 0x0288
#define RK3399_INTR_RAW_STATUS0 0x028c
#define RK3399_INTR_EN1 0x0290
#define RK3399_INTR_CLEAR1 0x0294
#define RK3399_INTR_STATUS1 0x0298
#define RK3399_INTR_RAW_STATUS1 0x029c
#define RK3399_LINE_FLAG 0x02a0
#define RK3399_VOP_STATUS 0x02a4
#define RK3399_BLANKING_VALUE 0x02a8
#define RK3399_MCU_BYPASS_PORT 0x02ac
#define RK3399_WIN0_DSP_BG 0x02b0
#define RK3399_WIN1_DSP_BG 0x02b4
#define RK3399_WIN2_DSP_BG 0x02b8
#define RK3399_WIN3_DSP_BG 0x02bc
#define RK3399_YUV2YUV_WIN 0x02c0
#define RK3399_YUV2YUV_POST 0x02c4
#define RK3399_AUTO_GATING_EN 0x02cc
#define RK3399_DBG_POST_REG1 0x036c
#define RK3399_WIN0_CSC_COE 0x03a0
#define RK3399_WIN1_CSC_COE 0x03c0
#define RK3399_WIN2_CSC_COE 0x03e0
#define RK3399_WIN3_CSC_COE 0x0400
#define RK3399_HWC_CSC_COE 0x0420
#define RK3399_BCSH_R2Y_CSC_COE 0x0440
#define RK3399_BCSH_Y2R_CSC_COE 0x0460
#define RK3399_POST_YUV2YUV_Y2R_COE 0x0480
#define RK3399_POST_YUV2YUV_3X3_COE 0x04a0
#define RK3399_POST_YUV2YUV_R2Y_COE 0x04c0
#define RK3399_WIN0_YUV2YUV_Y2R 0x04e0
#define RK3399_WIN0_YUV2YUV_3X3 0x0500
#define RK3399_WIN0_YUV2YUV_R2Y 0x0520
#define RK3399_WIN1_YUV2YUV_Y2R 0x0540
#define RK3399_WIN1_YUV2YUV_3X3 0x0560
#define RK3399_WIN1_YUV2YUV_R2Y 0x0580
#define RK3399_WIN2_YUV2YUV_Y2R 0x05a0
#define RK3399_WIN2_YUV2YUV_3X3 0x05c0
#define RK3399_WIN2_YUV2YUV_R2Y 0x05e0
#define RK3399_WIN3_YUV2YUV_Y2R 0x0600
#define RK3399_WIN3_YUV2YUV_3X3 0x0620
#define RK3399_WIN3_YUV2YUV_R2Y 0x0640
#define RK3399_WIN2_LUT_ADDR 0x1000
#define RK3399_WIN3_LUT_ADDR 0x1400
#define RK3399_HWC_LUT_ADDR 0x1800
#define RK3399_CABC_GAMMA_LUT_ADDR 0x1c00
#define RK3399_GAMMA_LUT_ADDR 0x2000

5.4 vo_win_init

vo_win_init用于初始化vop win,那么什么是vop win,指定就是vop图层,以RK3399vop包含4个图层;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* Initialize the vop->win array elements.
*/
static void vop_win_init(struct vop *vop)
{
const struct vop_data *vop_data = vop->data;
unsigned int i;

// 遍历每一个图层
for (i = 0; i < vop_data->win_size; i++) {
// 获取第i个vop win
struct vop_win *vop_win = &vop->win[i];
// 获取第i个vop win配置相关寄存器信息
const struct vop_win_data *win_data = &vop_data->win[i];

// 初始化成员
vop_win->data = win_data;
vop_win->vop = vop;

// rk3399定义了win_yuv2yuv,因此初始化yuv2yuv_data
if (vop_data->win_yuv2yuv)
vop_win->yuv2yuv_data = &vop_data->win_yuv2yuv[i];
}
}

5.5 vop_create_crtc

vop_create_crtc用于初始化crtc对象,并和plane关联在一起,这里我们仍然以设备节点vopb为例进行分析;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
static int vop_create_crtc(struct vop *vop)
{
// 获取vop data
const struct vop_data *vop_data = vop->data;
// 获取device,对应的设备节点为vopb
struct device *dev = vop->dev;
// 获取drm device
struct drm_device *drm_dev = vop->drm_dev;
struct drm_plane *primary = NULL, *cursor = NULL, *plane, *tmp;
// 获取drm crtc
struct drm_crtc *crtc = &vop->crtc;
struct device_node *port;
int ret;
int i;

/*
* Create drm_plane for primary and cursor planes first, since we need
* to pass them to drm_crtc_init_with_planes, which sets the
* "possible_crtcs" to the newly initialized crtc.
* 1. 遍历每一个vop win,每个vop win内部包含一个drm_plane,对类型为primary和cursor plane
* 进行初始化
*/
for (i = 0; i < vop_data->win_size; i++) {
// 获取第i个vop win
struct vop_win *vop_win = &vop->win[i];
// 获取第i个vop win data
const struct vop_win_data *win_data = vop_win->data;

// 只处理primary和cursor plane
if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
win_data->type != DRM_PLANE_TYPE_CURSOR)
continue;

// 进行plane的初始化,其中funcs被设置为vop_plane_funcs
ret = drm_universal_plane_init(vop->drm_dev, // drm设备
&vop_win->base, // 要初始化的plane对象
0, // 可能的CRTCs的位掩码
&vop_plane_funcs, // plane的控制函数集合
win_data->phy->data_formats, // 支持的格式数组(DRM_FORMAT_*)
win_data->phy->nformats, // formats数组的长度
win_data->phy->format_modifiers,
win_data->type, // plane的类型
NULL);
if (ret) {
DRM_DEV_ERROR(vop->dev, "failed to init plane %d\n",
ret);
goto err_cleanup_planes;
}

// 获取当前plane
plane = &vop_win->base;
// 设置plane的辅助函数helper_private为plane_helper_funcs
drm_plane_helper_add(plane, &plane_helper_funcs);
// 如果vop win data配置了x_mir_en/y_mir_en,则调用drm_plane_create_rotation_property为plane附加rotation property
vop_plane_add_properties(plane, win_data);
// 保存primary plane
if (plane->type == DRM_PLANE_TYPE_PRIMARY)
primary = plane;
// 保存cursor plane
else if (plane->type == DRM_PLANE_TYPE_CURSOR)
cursor = plane;
}

// 2. 使用指定的primary and cursor planes初始化的crtc对象,其中crtc回调函数funcs设置为vop_crtc_funcs
ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
&vop_crtc_funcs, NULL);
if (ret)
goto err_cleanup_planes;

// 3. 设置crtc的辅助函数helper_private为vop_crtc_helper_funcs
drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
// 进入
if (vop->lut_regs) {
drm_mode_crtc_set_gamma_size(crtc, vop_data->lut_size);
drm_crtc_enable_color_mgmt(crtc, 0, false, vop_data->lut_size);
}
/*
* Create drm_planes for overlay windows with possible_crtcs restricted
* to the newly created crtc.
* 4. 遍历每一个vop win,每个vop win内部包含一个drm_plane,对类型为overlay plane
* 进行初始化
*/
for (i = 0; i < vop_data->win_size; i++) {
// 获取第i个vop win
struct vop_win *vop_win = &vop->win[i];
// 获取第i个vop win data
const struct vop_win_data *win_data = vop_win->data;
unsigned long possible_crtcs = drm_crtc_mask(crtc);

// 只处理overlay plane
if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
continue;

// 进行plane的初始化,其中funcs被设置为vop_plane_funcs
ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
possible_crtcs,
&vop_plane_funcs,
win_data->phy->data_formats,
win_data->phy->nformats,
win_data->phy->format_modifiers,
win_data->type, NULL);
if (ret) {
DRM_DEV_ERROR(vop->dev, "failed to init overlay %d\n",
ret);
goto err_cleanup_crtc;
}
// 设置plane的辅助函数helper_private为plane_helper_funcs
drm_plane_helper_add(&vop_win->base, &plane_helper_funcs);
// 如果vop win data配置了x_mir_en/y_mir_en,则调用drm_plane_create_rotation_property为plane附加rotation property
vop_plane_add_properties(&vop_win->base, win_data);
}


// 5. 从vopb节点的子节点列表中查找名为port的子节点,也就是vopb_out节点
port = of_get_child_by_name(dev->of_node, "port");
if (!port) {
DRM_DEV_ERROR(vop->dev, "no port node found in %pOF\n",
dev->of_node);
ret = -ENOENT;
goto err_cleanup_crtc;
}

// 6. 初始化工作队列
drm_flip_work_init(&vop->fb_unref_work, "fb_unref",
vop_fb_unref_worker);

// 初始化完成量,
init_completion(&vop->dsp_hold_completion);
init_completion(&vop->line_flag_completion);
// 设置port节点
crtc->port = port;

// 7. 对crtc进行自刷新相关的辅助函数初始化
ret = drm_self_refresh_helper_init(crtc);
if (ret)
DRM_DEV_DEBUG_KMS(vop->dev,
"Failed to init %s with SR helpers %d, ignoring\n",
crtc->name, ret);

return 0;

err_cleanup_crtc:
drm_crtc_cleanup(crtc);
err_cleanup_planes:
list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
head)
drm_plane_cleanup(plane);
return ret;

err_cleanup_crtc:
drm_crtc_cleanup(crtc);
err_cleanup_planes:
list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
head)
drm_plane_cleanup(plane);
return ret;
}

以下是该函数的主要步骤:

(1) 遍历每一个vop win,每个vop win内部包含一个drm_plane,对类型为primarycursor plane进行初始化;

具体是调用drm_universal_plane_init来初始化drm_plane,并且添加plane辅助函数、设置属性等;

其中funcs被设置为vop_plane_funcs,定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c

1
2
3
4
5
6
7
8
9
static const struct drm_plane_funcs vop_plane_funcs = {
.update_plane = drm_atomic_helper_update_plane,
.disable_plane = drm_atomic_helper_disable_plane,
.destroy = vop_plane_destroy,
.reset = drm_atomic_helper_plane_reset,
.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
.format_mod_supported = rockchip_mod_supported,
};

helper_private被设为plane_helper_funcs,定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c

1
2
3
4
5
6
7
static const struct drm_plane_helper_funcs plane_helper_funcs = {
.atomic_check = vop_plane_atomic_check,
.atomic_update = vop_plane_atomic_update,
.atomic_disable = vop_plane_atomic_disable,
.atomic_async_check = vop_plane_atomic_async_check,
.atomic_async_update = vop_plane_atomic_async_update,
};

(2) 调用drm_crtc_init_with_planes使用指定的primary and cursor planes初始化的crtc对象,其中crtc回调函数funcs设置为vop_crtc_funcs,定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c

1
2
3
4
5
6
7
8
9
10
11
12
static const struct drm_crtc_funcs vop_crtc_funcs = {
.set_config = drm_atomic_helper_set_config,
.page_flip = drm_atomic_helper_page_flip,
.destroy = vop_crtc_destroy,
.reset = vop_crtc_reset,
.atomic_duplicate_state = vop_crtc_duplicate_state,
.atomic_destroy_state = vop_crtc_destroy_state,
.enable_vblank = vop_crtc_enable_vblank,
.disable_vblank = vop_crtc_disable_vblank,
.set_crc_source = vop_crtc_set_crc_source,
.verify_crc_source = vop_crtc_verify_crc_source,
};

(3) 调用drm_crtc_helper_add设置crtc的辅助函数helper_privatevop_crtc_helper_funcs,定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c

1
2
3
4
5
6
7
8
static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
.mode_fixup = vop_crtc_mode_fixup,
.atomic_check = vop_crtc_atomic_check,
.atomic_begin = vop_crtc_atomic_begin,
.atomic_flush = vop_crtc_atomic_flush,
.atomic_enable = vop_crtc_atomic_enable,
.atomic_disable = vop_crtc_atomic_disable,
};

(4) 遍历每一个vop win,每个vop win内部包含一个drm_plane,对类型为overlay plane进行初始化;

(5) 调用of_get_child_by_namevopb节点的子节点列表中查找名为port的子节点,也就是vopb_out节点;

(6) 调用drm_flip_work_init(&vop->fb_unref_work, "fb_unref",vop_fb_unref_worker)初始化工作队列;

函数drm_flip_work_init定义在drivers/gpu/drm/drm_flip_work.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
static void flip_worker(struct work_struct *w)
{
struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker);
struct list_head tasks;
unsigned long flags;

while (1) {
struct drm_flip_task *task, *tmp;

INIT_LIST_HEAD(&tasks);
spin_lock_irqsave(&work->lock, flags);
list_splice_tail(&work->commited, &tasks);
INIT_LIST_HEAD(&work->commited);
spin_unlock_irqrestore(&work->lock, flags);

if (list_empty(&tasks))
break;

list_for_each_entry_safe(task, tmp, &tasks, node) {
work->func(work, task->data);
kfree(task);
}
}
}


/**
* drm_flip_work_init - initialize flip-work
* @work: the flip-work to initialize
* @name: debug name
* @func: the callback work function
*
* Initializes/allocates resources for the flip-work
*/
void drm_flip_work_init(struct drm_flip_work *work,
const char *name, drm_flip_func_t func)
{
work->name = name;
INIT_LIST_HEAD(&work->queued);
INIT_LIST_HEAD(&work->commited);
spin_lock_init(&work->lock);
work->func = func;

// 设置work->worker工作函数为flip_worker
INIT_WORK(&work->worker, flip_worker);
}

(7) 调用drm_self_refresh_helper_initcrtc进行自刷新相关的辅助函数初始化;

drm_self_refresh_helper_init定义在drivers/gpu/drm/drm_self_refresh_helper.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
* @crtc: the crtc which supports self refresh supported displays
*
* Returns zero if successful or -errno on failure
*/
int drm_self_refresh_helper_init(struct drm_crtc *crtc)
{
struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;

/* Helper is already initialized */
if (WARN_ON(sr_data))
return -EINVAL;

sr_data = kzalloc(sizeof(*sr_data), GFP_KERNEL);
if (!sr_data)
return -ENOMEM;

INIT_DELAYED_WORK(&sr_data->entry_work,
drm_self_refresh_helper_entry_work);
sr_data->crtc = crtc;
mutex_init(&sr_data->avg_mutex);
ewma_psr_time_init(&sr_data->entry_avg_ms);
ewma_psr_time_init(&sr_data->exit_avg_ms);

/*
* Seed the averages so they're non-zero (and sufficiently large
* for even poorly performing panels). As time goes on, this will be
* averaged out and the values will trend to their true value.
*/
ewma_psr_time_add(&sr_data->entry_avg_ms, SELF_REFRESH_AVG_SEED_MS);
ewma_psr_time_add(&sr_data->exit_avg_ms, SELF_REFRESH_AVG_SEED_MS);

crtc->self_refresh_data = sr_data;
return 0;
}

5.6 vop_initial

vop_initial用户vop初始化,定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
static int vop_initial(struct vop *vop)
{
struct reset_control *ahb_rst;
int i, ret;

// 根据时钟名称hclk_vop获取时钟,设备节点属性clock-names、clocks,指定了名字为hclk_vop对应的时钟为<&cru HCLK_VOP0>
vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
if (IS_ERR(vop->hclk)) {
DRM_DEV_ERROR(vop->dev, "failed to get hclk source\n");
return PTR_ERR(vop->hclk);
}

// 根据时钟名称aclk_vop获取时钟,设备节点属性clock-names、clocks,指定了名字为aclk_vop对应的时钟为<&cru ACLK_VOP0>
vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
if (IS_ERR(vop->aclk)) {
DRM_DEV_ERROR(vop->dev, "failed to get aclk source\n");
return PTR_ERR(vop->aclk);
}

// 根据时钟名称dclk_vop获取时钟,设备节点属性clock-names、clocks,指定了名字为dclk_vop对应的时钟为<&cru DCLK_VOP0>
vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
if (IS_ERR(vop->dclk)) {
DRM_DEV_ERROR(vop->dev, "failed to get dclk source\n");
return PTR_ERR(vop->dclk);
}

// 电源相关,,使能设备的runtime pm功能 暂且忽略
ret = pm_runtime_resume_and_get(vop->dev);
if (ret < 0) {
DRM_DEV_ERROR(vop->dev, "failed to get pm runtime: %d\n", ret);
return ret;
}

// dclk时钟准备,使其处于可用状态,但不启用它
ret = clk_prepare(vop->dclk);
if (ret < 0) {
DRM_DEV_ERROR(vop->dev, "failed to prepare dclk\n");
goto err_put_pm_runtime;
}

/* Enable both the hclk and aclk to setup the vop,hclk时钟准备和使能 */
ret = clk_prepare_enable(vop->hclk);
if (ret < 0) {
DRM_DEV_ERROR(vop->dev, "failed to prepare/enable hclk\n");
goto err_unprepare_dclk;
}

// aclk时钟准备和使能
ret = clk_prepare_enable(vop->aclk);
if (ret < 0) {
DRM_DEV_ERROR(vop->dev, "failed to prepare/enable aclk\n");
goto err_disable_hclk;
}

/*
* do hclk_reset, reset all vop registers. 获取ahb相应的reset句柄
*/
ahb_rst = devm_reset_control_get(vop->dev, "ahb");
if (IS_ERR(ahb_rst)) {
DRM_DEV_ERROR(vop->dev, "failed to get ahb reset\n");
ret = PTR_ERR(ahb_rst);
goto err_disable_aclk;
}
// 对传入的reset资源进行复位操作
reset_control_assert(ahb_rst);
// 睡眠,单位为微妙
usleep_range(10, 20);
// 对传入的reset资源进行解复位操作
reset_control_deassert(ahb_rst);

// 设置rk3399_vop_big.intr.clear所描述寄存器相应位的值
VOP_INTR_SET_TYPE(vop, clear, INTR_MASK, 1);
// 设置rk3399_vop_big.intr.enable所描述寄存器相应位的值
VOP_INTR_SET_TYPE(vop, enable, INTR_MASK, 0);

// 备份vop相关寄存器的值
for (i = 0; i < vop->len; i += sizeof(u32))
vop->regsbak[i / 4] = readl_relaxed(vop->regs + i);

// 设置rk3399_vop_big.misc.global_regdone_en所描述寄存器相应位的值
VOP_REG_SET(vop, misc, global_regdone_en, 1);
// 设置rk3399_vop_big.common.dsp_blank所描述寄存器相应位的值
VOP_REG_SET(vop, common, dsp_blank, 0);

// 遍历每一个vop win
for (i = 0; i < vop->data->win_size; i++) {
// 获取第i个vop win
struct vop_win *vop_win = &vop->win[i];
// 获取第i个vop win data
const struct vop_win_data *win = vop_win->data;
int channel = i * 2 + 1;
// 设置rk3399_vop_big.win[i].phy.channel所描述寄存器相应位的值
VOP_WIN_SET(vop, win, channel, (channel + 1) << 4 | channel);
// 禁止当前vop win
vop_win_disable(vop, vop_win);
// 设置rk3399_vop_big.win[i].phy.gate所描述寄存器相应位的值
VOP_WIN_SET(vop, win, gate, 1);
}

// 设置rk3399_vop_big.common.cfg_done所描述寄存器相应位的值,enable reg config
vop_cfg_done(vop);

/*
* do dclk_reset, let all config take affect. 获取dclk相应的reset句柄
*/
vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
if (IS_ERR(vop->dclk_rst)) {
DRM_DEV_ERROR(vop->dev, "failed to get dclk reset\n");
ret = PTR_ERR(vop->dclk_rst);
goto err_disable_aclk;
}

// 对传入的reset资源进行复位操作
reset_control_assert(vop->dclk_rst);
// 睡眠,单位为微妙
usleep_range(10, 20);
// 对传入的reset资源进行解复位操作
reset_control_deassert(vop->dclk_rst);

// 禁止时钟hclk
clk_disable(vop->hclk);
// 禁止时钟aclk
clk_disable(vop->aclk);

// 使能标志位
vop->is_enabled = false;

pm_runtime_put_sync(vop->dev);

return 0;

err_disable_aclk:
clk_disable_unprepare(vop->aclk);
err_disable_hclk:
clk_disable_unprepare(vop->hclk);
err_unprepare_dclk:
clk_unprepare(vop->dclk);
err_put_pm_runtime:
pm_runtime_put_sync(vop->dev);
return ret;
}

该函数主要做了两件事件:

  • vop相关时钟初始化:aclk_vopdclk_vophclk_vop
  • vop相关寄存器配置,底层通过vop_reg_set设置配置寄存器值;
5.6.1 VOP_REG_SET

VOP_REG_SET定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c,目的就是设置指定寄存器指定位的值;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
static void vop_reg_set(struct vop *vop, 
const struct vop_reg *reg, // 寄存器信息
uint32_t _offset, // 寄存器偏移 传入0
uint32_t _mask, // 掩码 传入0xffffffff
uint32_t v, // 值
const char *reg_name)
{
int offset, mask, shift;

// 参数校验
if (!reg || !reg->mask) {
DRM_DEV_DEBUG(vop->dev, "Warning: not support %s\n", reg_name);
return;
}

// 偏移位
offset = reg->offset + _offset;

// 掩码
mask = reg->mask & _mask;
shift = reg->shift;

if (reg->write_mask) { // false
v = ((v << shift) & 0xffff) | (mask << (shift + 16));
} else {
// 获取寄存器的值
uint32_t cached_val = vop->regsbak[offset >> 2];
// 计算新的值
v = (cached_val & ~(mask << shift)) | ((v & mask) << shift);
// 保存新值
vop->regsbak[offset >> 2] = v;
}

if (reg->relaxed) // true
// 写寄存器
writel_relaxed(v, vop->regs + offset);
else
writel(v, vop->regs + offset);
}


#define VOP_REG_SET(vop, group, name, v) \
vop_reg_set(vop, &vop->data->group->name, 0, ~0, v, #name)

比如VOP_REG_SET(vop, common, dsp_blank, 0);实际上就是配置RK3399_DSP_CTRL0寄存器位[19:18]为0;

1
rk3399_vop_big.common.dsp_blankk = VOP_REG(RK3399_DSP_CTRL0, 0x3, 18)
5.6.2 VOP_INTR_SET_TYPE

VOP_INTR_SET_TYPE定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c,目的就是设置指定寄存器指定位的值;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define VOP_INTR_SET_MASK(vop, name, mask, v) \
vop_reg_set(vop, &vop->data->intr->name, 0, mask, v, #name)

#define VOP_INTR_SET_TYPE(vop, name, type, v) \
do { \
int i, reg = 0, mask = 0; \
for (i = 0; i < vop->data->intr->nintrs; i++) { \
if (vop->data->intr->intrs[i] & type) { \
reg |= (v) << i; \
mask |= 1 << i; \
} \
} \
VOP_INTR_SET_MASK(vop, name, mask, reg); \
} while (0)
5.6.3 VOP_WIN_SET

VOP_WIN_SET定义在drivers/gpu/drm/rockchip/rockchip_drm_vop.c,目的就是设置指定寄存器指定位的值;

1
2
#define VOP_WIN_SET(vop, win, name, v) \
vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)

5.7 vop_isr

vop_isrGIC_SPI中断处理函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static irqreturn_t vop_isr(int irq, void *data)
{
struct vop *vop = data;
struct drm_crtc *crtc = &vop->crtc;
uint32_t active_irqs;
int ret = IRQ_NONE;

/*
* The irq is shared with the iommu. If the runtime-pm state of the
* vop-device is disabled the irq has to be targeted at the iommu.
*/
if (!pm_runtime_get_if_in_use(vop->dev))
return IRQ_NONE;

if (vop_core_clks_enable(vop)) {
DRM_DEV_ERROR_RATELIMITED(vop->dev, "couldn't enable clocks\n");
goto out;
}

/*
* interrupt register has interrupt status, enable and clear bits, we
* must hold irq_lock to avoid a race with enable/disable_vblank().
*/
spin_lock(&vop->irq_lock);

active_irqs = VOP_INTR_GET_TYPE(vop, status, INTR_MASK);
/* Clear all active interrupt sources */
if (active_irqs)
VOP_INTR_SET_TYPE(vop, clear, active_irqs, 1);

spin_unlock(&vop->irq_lock);

/* This is expected for vop iommu irqs, since the irq is shared */
if (!active_irqs)
goto out_disable;

if (active_irqs & DSP_HOLD_VALID_INTR) {
// 唤醒等待此特定complete事件的单个线程
complete(&vop->dsp_hold_completion);
active_irqs &= ~DSP_HOLD_VALID_INTR;
ret = IRQ_HANDLED;
}

if (active_irqs & LINE_FLAG_INTR) {
// 唤醒等待此特定complete事件的单个线程
complete(&vop->line_flag_completion);
active_irqs &= ~LINE_FLAG_INTR;
ret = IRQ_HANDLED;
}

if (active_irqs & FS_INTR) {
drm_crtc_handle_vblank(crtc);
vop_handle_vblank(vop);
active_irqs &= ~FS_INTR;
ret = IRQ_HANDLED;
}

/* Unhandled irqs are spurious. */
if (active_irqs)
DRM_DEV_ERROR(vop->dev, "Unknown VOP IRQs: %#02x\n",
active_irqs);

out_disable:
vop_core_clks_disable(vop);
out:
pm_runtime_put(vop->dev);
return ret;
}

5.8 rockchip_drm_dma_init_device

rockchip_drm_dma_init_device定义在drivers/gpu/drm/rockchip/rockchip_drm_drv.c

1
2
3
4
5
6
7
8
9
10
void rockchip_drm_dma_init_device(struct drm_device *drm_dev,
struct device *dev)
{
struct rockchip_drm_private *private = drm_dev->dev_private;

if (!device_iommu_mapped(dev))
private->iommu_dev = ERR_PTR(-ENODEV);
else if (!private->iommu_dev)
private->iommu_dev = dev;
}

5.9 总结

经过上述的分析,我们大致了解到vop_bind的主要功能’;

  • 解析vopb设备节点;

    • 获取aclk_vop, dclk_vop, hclk_vop时钟,并进行时钟准备和使能功能;
    • 获取vop中断,并申请vop中断;
    • 获取vopb相关寄存器虚拟地址;
    • 获取ahbdclk 复位句柄,并通过复位句柄进行设备的复位操作;
  • 初始化drm_crtc,其中:

    • 回调函数funcs设置为vop_crtc_funcs
    • 辅助函数helper_private设置为vop_crtc_helper_funcs
  • 由于RK3399有4个vop win,每个vop win内部包含一个drm_plane,对drm_plane进行初始化,其中:

    • 回调函数funcs设置为vop_plane_funcs
    • 辅助函数helper_private设置为plane_helper_funcs
  • 配置vop相关的寄存器,比如rk3399_vop_big.intr.clearrk3399_vop_big.intr.enable等;

需要注意的是:vop_bind中没有进行显示模式的配置,即显示的各种时序参数配置(与显示器息息相关)。

参考文章

[1] DRM (Direct Rendering Manager)

[2] Wiki: Direct Rendering Manager

[3] The DRM/KMS subsystem from a newbie’s point of view

[4] DRM驱动概念、组成、框架、源码分析

[5] linux驱动系列学习之DRM(十)

[6] DRM驱动程序开发(VKMS

[7] MIPI自学笔记

[8] Linux reset子系统和驱动实例