]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/drm_color_mgmt.c
drm/i915: make mappable struct resource centric
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / drm_color_mgmt.c
CommitLineData
f1e2f66c
DV
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
a6acccf8
DV
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.
ea0dd85a 39 * The data is interpreted as an array of &struct drm_color_lut elements.
a6acccf8
DV
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 *
717fd813
DV
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
2e38178e
DV
46 * driver boot-up state too. Drivers can access this blob through
47 * &drm_crtc_state.degamma_lut.
717fd813 48 *
a6acccf8
DV
49 * “DEGAMMA_LUT_SIZE”:
50 * Unsinged range property to give the size of the lookup table to be set
51 * on the DEGAMMA_LUT property (the size depends on the underlying
52 * hardware). If drivers support multiple LUT sizes then they should
53 * publish the largest size, and sub-sample smaller sized LUTs (e.g. for
54 * split-gamma modes) appropriately.
55 *
56 * “CTM”:
57 * Blob property to set the current transformation matrix (CTM) apply to
58 * pixel data after the lookup through the degamma LUT and before the
59 * lookup through the gamma LUT. The data is interpreted as a struct
60 * &drm_color_ctm.
61 *
717fd813
DV
62 * Setting this to NULL (blob property value set to 0) means a
63 * unit/pass-thru matrix should be used. This is generally the driver
2e38178e
DV
64 * boot-up state too. Drivers can access the blob for the color conversion
65 * matrix through &drm_crtc_state.ctm.
717fd813 66 *
a6acccf8
DV
67 * “GAMMA_LUT”:
68 * Blob property to set the gamma lookup table (LUT) mapping pixel data
69 * after the transformation matrix to data sent to the connector. The
ea0dd85a 70 * data is interpreted as an array of &struct drm_color_lut elements.
a6acccf8
DV
71 * Hardware might choose not to use the full precision of the LUT elements
72 * nor use all the elements of the LUT (for example the hardware might
73 * choose to interpolate between LUT[0] and LUT[4]).
74 *
717fd813
DV
75 * Setting this to NULL (blob property value set to 0) means a
76 * linear/pass-thru gamma table should be used. This is generally the
2e38178e
DV
77 * driver boot-up state too. Drivers can access this blob through
78 * &drm_crtc_state.gamma_lut.
717fd813 79 *
a6acccf8
DV
80 * “GAMMA_LUT_SIZE”:
81 * Unsigned range property to give the size of the lookup table to be set
82 * on the GAMMA_LUT property (the size depends on the underlying hardware).
83 * If drivers support multiple LUT sizes then they should publish the
84 * largest size, and sub-sample smaller sized LUTs (e.g. for split-gamma
85 * modes) appropriately.
86 *
87 * There is also support for a legacy gamma table, which is set up by calling
88 * drm_mode_crtc_set_gamma_size(). Drivers which support both should use
89 * drm_atomic_helper_legacy_gamma_set() to alias the legacy gamma ramp with the
90 * "GAMMA_LUT" property above.
91 */
f1e2f66c 92
8f2e045e
JN
93/**
94 * drm_color_lut_extract - clamp and round LUT entries
95 * @user_input: input value
96 * @bit_precision: number of bits the hw LUT supports
97 *
98 * Extract a degamma/gamma LUT value provided by user (in the form of
99 * &drm_color_lut entries) and round it to the precision supported by the
100 * hardware.
101 */
102uint32_t drm_color_lut_extract(uint32_t user_input, uint32_t bit_precision)
103{
104 uint32_t val = user_input;
105 uint32_t max = 0xffff >> (16 - bit_precision);
106
107 /* Round only if we're not using full precision. */
108 if (bit_precision < 16) {
109 val += 1UL << (16 - bit_precision - 1);
110 val >>= 16 - bit_precision;
111 }
112
113 return clamp_val(val, 0, max);
114}
115EXPORT_SYMBOL(drm_color_lut_extract);
116
f1e2f66c
DV
117/**
118 * drm_crtc_enable_color_mgmt - enable color management properties
119 * @crtc: DRM CRTC
120 * @degamma_lut_size: the size of the degamma lut (before CSC)
121 * @has_ctm: whether to attach ctm_property for CSC matrix
122 * @gamma_lut_size: the size of the gamma lut (after CSC)
123 *
124 * This function lets the driver enable the color correction
125 * properties on a CRTC. This includes 3 degamma, csc and gamma
126 * properties that userspace can set and 2 size properties to inform
127 * the userspace of the lut sizes. Each of the properties are
128 * optional. The gamma and degamma properties are only attached if
129 * their size is not 0 and ctm_property is only attached if has_ctm is
130 * true.
d7b66de5
DV
131 *
132 * Drivers should use drm_atomic_helper_legacy_gamma_set() to implement the
133 * legacy &drm_crtc_funcs.gamma_set callback.
f1e2f66c
DV
134 */
135void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
136 uint degamma_lut_size,
137 bool has_ctm,
138 uint gamma_lut_size)
139{
140 struct drm_device *dev = crtc->dev;
141 struct drm_mode_config *config = &dev->mode_config;
142
143 if (degamma_lut_size) {
144 drm_object_attach_property(&crtc->base,
145 config->degamma_lut_property, 0);
146 drm_object_attach_property(&crtc->base,
147 config->degamma_lut_size_property,
148 degamma_lut_size);
149 }
150
151 if (has_ctm)
152 drm_object_attach_property(&crtc->base,
153 config->ctm_property, 0);
154
155 if (gamma_lut_size) {
156 drm_object_attach_property(&crtc->base,
157 config->gamma_lut_property, 0);
158 drm_object_attach_property(&crtc->base,
159 config->gamma_lut_size_property,
160 gamma_lut_size);
161 }
162}
163EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
164
165/**
166 * drm_mode_crtc_set_gamma_size - set the gamma table size
167 * @crtc: CRTC to set the gamma table size for
168 * @gamma_size: size of the gamma table
169 *
170 * Drivers which support gamma tables should set this to the supported gamma
171 * table size when initializing the CRTC. Currently the drm core only supports a
172 * fixed gamma table size.
173 *
174 * Returns:
175 * Zero on success, negative errno on failure.
176 */
177int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
178 int gamma_size)
179{
180 uint16_t *r_base, *g_base, *b_base;
181 int i;
182
183 crtc->gamma_size = gamma_size;
184
185 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
186 GFP_KERNEL);
187 if (!crtc->gamma_store) {
188 crtc->gamma_size = 0;
189 return -ENOMEM;
190 }
191
192 r_base = crtc->gamma_store;
193 g_base = r_base + gamma_size;
194 b_base = g_base + gamma_size;
195 for (i = 0; i < gamma_size; i++) {
196 r_base[i] = i << 8;
197 g_base[i] = i << 8;
198 b_base[i] = i << 8;
199 }
200
201
202 return 0;
203}
204EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
205
206/**
207 * drm_mode_gamma_set_ioctl - set the gamma table
208 * @dev: DRM device
209 * @data: ioctl data
210 * @file_priv: DRM file info
211 *
212 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
213 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
214 *
215 * Called by the user via ioctl.
216 *
217 * Returns:
218 * Zero on success, negative errno on failure.
219 */
220int drm_mode_gamma_set_ioctl(struct drm_device *dev,
221 void *data, struct drm_file *file_priv)
222{
223 struct drm_mode_crtc_lut *crtc_lut = data;
224 struct drm_crtc *crtc;
225 void *r_base, *g_base, *b_base;
226 int size;
ca659e0e 227 struct drm_modeset_acquire_ctx ctx;
f1e2f66c
DV
228 int ret = 0;
229
230 if (!drm_core_check_feature(dev, DRIVER_MODESET))
231 return -EINVAL;
232
418da172 233 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
ca659e0e
DV
234 if (!crtc)
235 return -ENOENT;
f1e2f66c 236
ca659e0e
DV
237 if (crtc->funcs->gamma_set == NULL)
238 return -ENOSYS;
f1e2f66c
DV
239
240 /* memcpy into gamma store */
ca659e0e
DV
241 if (crtc_lut->gamma_size != crtc->gamma_size)
242 return -EINVAL;
243
244 drm_modeset_acquire_init(&ctx, 0);
ca659e0e
DV
245retry:
246 ret = drm_modeset_lock_all_ctx(dev, &ctx);
247 if (ret)
f1e2f66c 248 goto out;
f1e2f66c
DV
249
250 size = crtc_lut->gamma_size * (sizeof(uint16_t));
251 r_base = crtc->gamma_store;
252 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
253 ret = -EFAULT;
254 goto out;
255 }
256
257 g_base = r_base + size;
258 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
259 ret = -EFAULT;
260 goto out;
261 }
262
263 b_base = g_base + size;
264 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
265 ret = -EFAULT;
266 goto out;
267 }
268
6d124ff8
DV
269 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
270 crtc->gamma_size, &ctx);
f1e2f66c
DV
271
272out:
ca659e0e
DV
273 if (ret == -EDEADLK) {
274 drm_modeset_backoff(&ctx);
275 goto retry;
276 }
277 drm_modeset_drop_locks(&ctx);
278 drm_modeset_acquire_fini(&ctx);
279
f1e2f66c
DV
280 return ret;
281
282}
283
284/**
285 * drm_mode_gamma_get_ioctl - get the gamma table
286 * @dev: DRM device
287 * @data: ioctl data
288 * @file_priv: DRM file info
289 *
290 * Copy the current gamma table into the storage provided. This also provides
291 * the gamma table size the driver expects, which can be used to size the
292 * allocated storage.
293 *
294 * Called by the user via ioctl.
295 *
296 * Returns:
297 * Zero on success, negative errno on failure.
298 */
299int drm_mode_gamma_get_ioctl(struct drm_device *dev,
300 void *data, struct drm_file *file_priv)
301{
302 struct drm_mode_crtc_lut *crtc_lut = data;
303 struct drm_crtc *crtc;
304 void *r_base, *g_base, *b_base;
305 int size;
306 int ret = 0;
307
308 if (!drm_core_check_feature(dev, DRIVER_MODESET))
309 return -EINVAL;
310
418da172 311 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
04ee39ba
DV
312 if (!crtc)
313 return -ENOENT;
f1e2f66c
DV
314
315 /* memcpy into gamma store */
04ee39ba
DV
316 if (crtc_lut->gamma_size != crtc->gamma_size)
317 return -EINVAL;
f1e2f66c 318
04ee39ba 319 drm_modeset_lock(&crtc->mutex, NULL);
f1e2f66c
DV
320 size = crtc_lut->gamma_size * (sizeof(uint16_t));
321 r_base = crtc->gamma_store;
322 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
323 ret = -EFAULT;
324 goto out;
325 }
326
327 g_base = r_base + size;
328 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
329 ret = -EFAULT;
330 goto out;
331 }
332
333 b_base = g_base + size;
334 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
335 ret = -EFAULT;
336 goto out;
337 }
338out:
04ee39ba 339 drm_modeset_unlock(&crtc->mutex);
f1e2f66c
DV
340 return ret;
341}