xref: /dragonfly/sys/dev/drm/drm_color_mgmt.c (revision 2b57e6df)
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <drm/drmP.h>
24 #include <drm/drm_crtc.h>
25 #include <drm/drm_color_mgmt.h>
26 
27 #include "drm_crtc_internal.h"
28 
29 /**
30  * DOC: overview
31  *
32  * Color management or color space adjustments is supported through a set of 5
33  * properties on the &drm_crtc object. They are set up by calling
34  * drm_crtc_enable_color_mgmt().
35  *
36  * "DEGAMMA_LUT”:
37  *	Blob property to set the degamma lookup table (LUT) mapping pixel data
38  *	from the framebuffer before it is given to the transformation matrix.
39  *	The data is interpreted as an array of &struct drm_color_lut elements.
40  *	Hardware might choose not to use the full precision of the LUT elements
41  *	nor use all the elements of the LUT (for example the hardware might
42  *	choose to interpolate between LUT[0] and LUT[4]).
43  *
44  *	Setting this to NULL (blob property value set to 0) means a
45  *	linear/pass-thru gamma table should be used. This is generally the
46  *	driver boot-up state too.
47  *
48  * “DEGAMMA_LUT_SIZE”:
49  *	Unsinged range property to give the size of the lookup table to be set
50  *	on the DEGAMMA_LUT property (the size depends on the underlying
51  *	hardware). If drivers support multiple LUT sizes then they should
52  *	publish the largest size, and sub-sample smaller sized LUTs (e.g. for
53  *	split-gamma modes) appropriately.
54  *
55  * “CTM”:
56  *	Blob property to set the current transformation matrix (CTM) apply to
57  *	pixel data after the lookup through the degamma LUT and before the
58  *	lookup through the gamma LUT. The data is interpreted as a struct
59  *	&drm_color_ctm.
60  *
61  *	Setting this to NULL (blob property value set to 0) means a
62  *	unit/pass-thru matrix should be used. This is generally the driver
63  *	boot-up state too.
64  *
65  * “GAMMA_LUT”:
66  *	Blob property to set the gamma lookup table (LUT) mapping pixel data
67  *	after the transformation matrix to data sent to the connector. The
68  *	data is interpreted as an array of &struct drm_color_lut elements.
69  *	Hardware might choose not to use the full precision of the LUT elements
70  *	nor use all the elements of the LUT (for example the hardware might
71  *	choose to interpolate between LUT[0] and LUT[4]).
72  *
73  *	Setting this to NULL (blob property value set to 0) means a
74  *	linear/pass-thru gamma table should be used. This is generally the
75  *	driver boot-up state too.
76  *
77  * “GAMMA_LUT_SIZE”:
78  *	Unsigned range property to give the size of the lookup table to be set
79  *	on the GAMMA_LUT property (the size depends on the underlying hardware).
80  *	If drivers support multiple LUT sizes then they should publish the
81  *	largest size, and sub-sample smaller sized LUTs (e.g. for split-gamma
82  *	modes) appropriately.
83  *
84  * There is also support for a legacy gamma table, which is set up by calling
85  * drm_mode_crtc_set_gamma_size(). Drivers which support both should use
86  * drm_atomic_helper_legacy_gamma_set() to alias the legacy gamma ramp with the
87  * "GAMMA_LUT" property above.
88  */
89 
90 /**
91  * drm_crtc_enable_color_mgmt - enable color management properties
92  * @crtc: DRM CRTC
93  * @degamma_lut_size: the size of the degamma lut (before CSC)
94  * @has_ctm: whether to attach ctm_property for CSC matrix
95  * @gamma_lut_size: the size of the gamma lut (after CSC)
96  *
97  * This function lets the driver enable the color correction
98  * properties on a CRTC. This includes 3 degamma, csc and gamma
99  * properties that userspace can set and 2 size properties to inform
100  * the userspace of the lut sizes. Each of the properties are
101  * optional. The gamma and degamma properties are only attached if
102  * their size is not 0 and ctm_property is only attached if has_ctm is
103  * true.
104  */
105 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
106 				uint degamma_lut_size,
107 				bool has_ctm,
108 				uint gamma_lut_size)
109 {
110 	struct drm_device *dev = crtc->dev;
111 	struct drm_mode_config *config = &dev->mode_config;
112 
113 	if (degamma_lut_size) {
114 		drm_object_attach_property(&crtc->base,
115 					   config->degamma_lut_property, 0);
116 		drm_object_attach_property(&crtc->base,
117 					   config->degamma_lut_size_property,
118 					   degamma_lut_size);
119 	}
120 
121 	if (has_ctm)
122 		drm_object_attach_property(&crtc->base,
123 					   config->ctm_property, 0);
124 
125 	if (gamma_lut_size) {
126 		drm_object_attach_property(&crtc->base,
127 					   config->gamma_lut_property, 0);
128 		drm_object_attach_property(&crtc->base,
129 					   config->gamma_lut_size_property,
130 					   gamma_lut_size);
131 	}
132 }
133 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
134 
135 /**
136  * drm_mode_crtc_set_gamma_size - set the gamma table size
137  * @crtc: CRTC to set the gamma table size for
138  * @gamma_size: size of the gamma table
139  *
140  * Drivers which support gamma tables should set this to the supported gamma
141  * table size when initializing the CRTC. Currently the drm core only supports a
142  * fixed gamma table size.
143  *
144  * Returns:
145  * Zero on success, negative errno on failure.
146  */
147 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
148 				 int gamma_size)
149 {
150 	uint16_t *r_base, *g_base, *b_base;
151 	int i;
152 
153 	crtc->gamma_size = gamma_size;
154 
155 	crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
156 				    GFP_KERNEL);
157 	if (!crtc->gamma_store) {
158 		crtc->gamma_size = 0;
159 		return -ENOMEM;
160 	}
161 
162 	r_base = crtc->gamma_store;
163 	g_base = r_base + gamma_size;
164 	b_base = g_base + gamma_size;
165 	for (i = 0; i < gamma_size; i++) {
166 		r_base[i] = i << 8;
167 		g_base[i] = i << 8;
168 		b_base[i] = i << 8;
169 	}
170 
171 
172 	return 0;
173 }
174 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
175 
176 /**
177  * drm_mode_gamma_set_ioctl - set the gamma table
178  * @dev: DRM device
179  * @data: ioctl data
180  * @file_priv: DRM file info
181  *
182  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
183  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
184  *
185  * Called by the user via ioctl.
186  *
187  * Returns:
188  * Zero on success, negative errno on failure.
189  */
190 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
191 			     void *data, struct drm_file *file_priv)
192 {
193 	struct drm_mode_crtc_lut *crtc_lut = data;
194 	struct drm_crtc *crtc;
195 	void *r_base, *g_base, *b_base;
196 	int size;
197 	struct drm_modeset_acquire_ctx ctx;
198 	int ret = 0;
199 
200 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
201 		return -EINVAL;
202 
203 	crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
204 	if (!crtc)
205 		return -ENOENT;
206 
207 	if (crtc->funcs->gamma_set == NULL)
208 		return -ENOSYS;
209 
210 	/* memcpy into gamma store */
211 	if (crtc_lut->gamma_size != crtc->gamma_size)
212 		return -EINVAL;
213 
214 	drm_modeset_acquire_init(&ctx, 0);
215 retry:
216 	ret = drm_modeset_lock_all_ctx(dev, &ctx);
217 	if (ret)
218 		goto out;
219 
220 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
221 	r_base = crtc->gamma_store;
222 	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
223 		ret = -EFAULT;
224 		goto out;
225 	}
226 
227 	g_base = r_base + size;
228 	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
229 		ret = -EFAULT;
230 		goto out;
231 	}
232 
233 	b_base = g_base + size;
234 	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
235 		ret = -EFAULT;
236 		goto out;
237 	}
238 
239 	ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
240 				     crtc->gamma_size, &ctx);
241 
242 out:
243 	if (ret == -EDEADLK) {
244 		drm_modeset_backoff(&ctx);
245 		goto retry;
246 	}
247 	drm_modeset_drop_locks(&ctx);
248 	drm_modeset_acquire_fini(&ctx);
249 
250 	return ret;
251 
252 }
253 
254 /**
255  * drm_mode_gamma_get_ioctl - get the gamma table
256  * @dev: DRM device
257  * @data: ioctl data
258  * @file_priv: DRM file info
259  *
260  * Copy the current gamma table into the storage provided. This also provides
261  * the gamma table size the driver expects, which can be used to size the
262  * allocated storage.
263  *
264  * Called by the user via ioctl.
265  *
266  * Returns:
267  * Zero on success, negative errno on failure.
268  */
269 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
270 			     void *data, struct drm_file *file_priv)
271 {
272 	struct drm_mode_crtc_lut *crtc_lut = data;
273 	struct drm_crtc *crtc;
274 	void *r_base, *g_base, *b_base;
275 	int size;
276 	int ret = 0;
277 
278 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
279 		return -EINVAL;
280 
281 	crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
282 	if (!crtc)
283 		return -ENOENT;
284 
285 	/* memcpy into gamma store */
286 	if (crtc_lut->gamma_size != crtc->gamma_size)
287 		return -EINVAL;
288 
289 	drm_modeset_lock(&crtc->mutex, NULL);
290 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
291 	r_base = crtc->gamma_store;
292 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
293 		ret = -EFAULT;
294 		goto out;
295 	}
296 
297 	g_base = r_base + size;
298 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
299 		ret = -EFAULT;
300 		goto out;
301 	}
302 
303 	b_base = g_base + size;
304 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
305 		ret = -EFAULT;
306 		goto out;
307 	}
308 out:
309 	drm_modeset_unlock(&crtc->mutex);
310 	return ret;
311 }
312