]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/gpu/drm/drm_color_mgmt.c
b81dcb1d4cb3b7227c71ba77a98eb2af4ddb2a08
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / drm_color_mgmt.c
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_color_lut_extract - clamp and round LUT entries
92 * @user_input: input value
93 * @bit_precision: number of bits the hw LUT supports
94 *
95 * Extract a degamma/gamma LUT value provided by user (in the form of
96 * &drm_color_lut entries) and round it to the precision supported by the
97 * hardware.
98 */
99 uint32_t drm_color_lut_extract(uint32_t user_input, uint32_t bit_precision)
100 {
101 uint32_t val = user_input;
102 uint32_t max = 0xffff >> (16 - bit_precision);
103
104 /* Round only if we're not using full precision. */
105 if (bit_precision < 16) {
106 val += 1UL << (16 - bit_precision - 1);
107 val >>= 16 - bit_precision;
108 }
109
110 return clamp_val(val, 0, max);
111 }
112 EXPORT_SYMBOL(drm_color_lut_extract);
113
114 /**
115 * drm_crtc_enable_color_mgmt - enable color management properties
116 * @crtc: DRM CRTC
117 * @degamma_lut_size: the size of the degamma lut (before CSC)
118 * @has_ctm: whether to attach ctm_property for CSC matrix
119 * @gamma_lut_size: the size of the gamma lut (after CSC)
120 *
121 * This function lets the driver enable the color correction
122 * properties on a CRTC. This includes 3 degamma, csc and gamma
123 * properties that userspace can set and 2 size properties to inform
124 * the userspace of the lut sizes. Each of the properties are
125 * optional. The gamma and degamma properties are only attached if
126 * their size is not 0 and ctm_property is only attached if has_ctm is
127 * true.
128 */
129 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
130 uint degamma_lut_size,
131 bool has_ctm,
132 uint gamma_lut_size)
133 {
134 struct drm_device *dev = crtc->dev;
135 struct drm_mode_config *config = &dev->mode_config;
136
137 if (degamma_lut_size) {
138 drm_object_attach_property(&crtc->base,
139 config->degamma_lut_property, 0);
140 drm_object_attach_property(&crtc->base,
141 config->degamma_lut_size_property,
142 degamma_lut_size);
143 }
144
145 if (has_ctm)
146 drm_object_attach_property(&crtc->base,
147 config->ctm_property, 0);
148
149 if (gamma_lut_size) {
150 drm_object_attach_property(&crtc->base,
151 config->gamma_lut_property, 0);
152 drm_object_attach_property(&crtc->base,
153 config->gamma_lut_size_property,
154 gamma_lut_size);
155 }
156 }
157 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
158
159 /**
160 * drm_mode_crtc_set_gamma_size - set the gamma table size
161 * @crtc: CRTC to set the gamma table size for
162 * @gamma_size: size of the gamma table
163 *
164 * Drivers which support gamma tables should set this to the supported gamma
165 * table size when initializing the CRTC. Currently the drm core only supports a
166 * fixed gamma table size.
167 *
168 * Returns:
169 * Zero on success, negative errno on failure.
170 */
171 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
172 int gamma_size)
173 {
174 uint16_t *r_base, *g_base, *b_base;
175 int i;
176
177 crtc->gamma_size = gamma_size;
178
179 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
180 GFP_KERNEL);
181 if (!crtc->gamma_store) {
182 crtc->gamma_size = 0;
183 return -ENOMEM;
184 }
185
186 r_base = crtc->gamma_store;
187 g_base = r_base + gamma_size;
188 b_base = g_base + gamma_size;
189 for (i = 0; i < gamma_size; i++) {
190 r_base[i] = i << 8;
191 g_base[i] = i << 8;
192 b_base[i] = i << 8;
193 }
194
195
196 return 0;
197 }
198 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
199
200 /**
201 * drm_mode_gamma_set_ioctl - set the gamma table
202 * @dev: DRM device
203 * @data: ioctl data
204 * @file_priv: DRM file info
205 *
206 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
207 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
208 *
209 * Called by the user via ioctl.
210 *
211 * Returns:
212 * Zero on success, negative errno on failure.
213 */
214 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
215 void *data, struct drm_file *file_priv)
216 {
217 struct drm_mode_crtc_lut *crtc_lut = data;
218 struct drm_crtc *crtc;
219 void *r_base, *g_base, *b_base;
220 int size;
221 struct drm_modeset_acquire_ctx ctx;
222 int ret = 0;
223
224 if (!drm_core_check_feature(dev, DRIVER_MODESET))
225 return -EINVAL;
226
227 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
228 if (!crtc)
229 return -ENOENT;
230
231 if (crtc->funcs->gamma_set == NULL)
232 return -ENOSYS;
233
234 /* memcpy into gamma store */
235 if (crtc_lut->gamma_size != crtc->gamma_size)
236 return -EINVAL;
237
238 drm_modeset_acquire_init(&ctx, 0);
239 dev->mode_config.acquire_ctx = &ctx;
240 retry:
241 ret = drm_modeset_lock_all_ctx(dev, &ctx);
242 if (ret)
243 goto out;
244
245 size = crtc_lut->gamma_size * (sizeof(uint16_t));
246 r_base = crtc->gamma_store;
247 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
248 ret = -EFAULT;
249 goto out;
250 }
251
252 g_base = r_base + size;
253 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
254 ret = -EFAULT;
255 goto out;
256 }
257
258 b_base = g_base + size;
259 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
260 ret = -EFAULT;
261 goto out;
262 }
263
264 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
265 crtc->gamma_size, &ctx);
266
267 out:
268 if (ret == -EDEADLK) {
269 drm_modeset_backoff(&ctx);
270 goto retry;
271 }
272 drm_modeset_drop_locks(&ctx);
273 drm_modeset_acquire_fini(&ctx);
274
275 return ret;
276
277 }
278
279 /**
280 * drm_mode_gamma_get_ioctl - get the gamma table
281 * @dev: DRM device
282 * @data: ioctl data
283 * @file_priv: DRM file info
284 *
285 * Copy the current gamma table into the storage provided. This also provides
286 * the gamma table size the driver expects, which can be used to size the
287 * allocated storage.
288 *
289 * Called by the user via ioctl.
290 *
291 * Returns:
292 * Zero on success, negative errno on failure.
293 */
294 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
295 void *data, struct drm_file *file_priv)
296 {
297 struct drm_mode_crtc_lut *crtc_lut = data;
298 struct drm_crtc *crtc;
299 void *r_base, *g_base, *b_base;
300 int size;
301 int ret = 0;
302
303 if (!drm_core_check_feature(dev, DRIVER_MODESET))
304 return -EINVAL;
305
306 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
307 if (!crtc)
308 return -ENOENT;
309
310 /* memcpy into gamma store */
311 if (crtc_lut->gamma_size != crtc->gamma_size)
312 return -EINVAL;
313
314 drm_modeset_lock(&crtc->mutex, NULL);
315 size = crtc_lut->gamma_size * (sizeof(uint16_t));
316 r_base = crtc->gamma_store;
317 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
318 ret = -EFAULT;
319 goto out;
320 }
321
322 g_base = r_base + size;
323 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
324 ret = -EFAULT;
325 goto out;
326 }
327
328 b_base = g_base + size;
329 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
330 ret = -EFAULT;
331 goto out;
332 }
333 out:
334 drm_modeset_unlock(&crtc->mutex);
335 return ret;
336 }