drm/i915: Simplify i915_gtt_color_adjust()
[linux-block.git] / drivers / gpu / drm / drm_mode_config.c
CommitLineData
28575f16
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/drm_mode_config.h>
24#include <drm/drmP.h>
25
26#include "drm_crtc_internal.h"
27#include "drm_internal.h"
28
29int drm_modeset_register_all(struct drm_device *dev)
30{
31 int ret;
32
33 ret = drm_plane_register_all(dev);
34 if (ret)
35 goto err_plane;
36
37 ret = drm_crtc_register_all(dev);
38 if (ret)
39 goto err_crtc;
40
41 ret = drm_encoder_register_all(dev);
42 if (ret)
43 goto err_encoder;
44
45 ret = drm_connector_register_all(dev);
46 if (ret)
47 goto err_connector;
48
49 return 0;
50
51err_connector:
52 drm_encoder_unregister_all(dev);
53err_encoder:
54 drm_crtc_unregister_all(dev);
55err_crtc:
56 drm_plane_unregister_all(dev);
57err_plane:
58 return ret;
59}
60
61void drm_modeset_unregister_all(struct drm_device *dev)
62{
63 drm_connector_unregister_all(dev);
64 drm_encoder_unregister_all(dev);
65 drm_crtc_unregister_all(dev);
66 drm_plane_unregister_all(dev);
67}
68
69/**
70 * drm_mode_getresources - get graphics configuration
71 * @dev: drm device for the ioctl
72 * @data: data pointer for the ioctl
73 * @file_priv: drm file for the ioctl call
74 *
75 * Construct a set of configuration description structures and return
76 * them to the user, including CRTC, connector and framebuffer configuration.
77 *
78 * Called by the user via ioctl.
79 *
80 * Returns:
81 * Zero on success, negative errno on failure.
82 */
83int drm_mode_getresources(struct drm_device *dev, void *data,
84 struct drm_file *file_priv)
85{
86 struct drm_mode_card_res *card_res = data;
28575f16
DV
87 struct drm_framebuffer *fb;
88 struct drm_connector *connector;
89 struct drm_crtc *crtc;
90 struct drm_encoder *encoder;
697cc9c8 91 int count, ret = 0;
28575f16
DV
92 uint32_t __user *fb_id;
93 uint32_t __user *crtc_id;
94 uint32_t __user *connector_id;
95 uint32_t __user *encoder_id;
96
97 if (!drm_core_check_feature(dev, DRIVER_MODESET))
98 return -EINVAL;
99
100
101 mutex_lock(&file_priv->fbs_lock);
697cc9c8
DV
102 count = 0;
103 fb_id = u64_to_user_ptr(card_res->fb_id_ptr);
104 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
105 if (count < card_res->count_fbs &&
106 put_user(fb->base.id, fb_id + count)) {
107 mutex_unlock(&file_priv->fbs_lock);
108 return -EFAULT;
28575f16 109 }
697cc9c8 110 count++;
28575f16 111 }
697cc9c8 112 card_res->count_fbs = count;
28575f16
DV
113 mutex_unlock(&file_priv->fbs_lock);
114
115 /* mode_config.mutex protects the connector list against e.g. DP MST
116 * connector hot-adding. CRTC/Plane lists are invariant. */
117 mutex_lock(&dev->mode_config.mutex);
28575f16
DV
118 card_res->max_height = dev->mode_config.max_height;
119 card_res->min_height = dev->mode_config.min_height;
120 card_res->max_width = dev->mode_config.max_width;
121 card_res->min_width = dev->mode_config.min_width;
122
697cc9c8
DV
123 count = 0;
124 crtc_id = u64_to_user_ptr(card_res->crtc_id_ptr);
125 drm_for_each_crtc(crtc, dev) {
126 if (count < card_res->count_crtcs &&
127 put_user(crtc->base.id, crtc_id + count)) {
128 ret = -EFAULT;
129 goto out;
28575f16 130 }
697cc9c8 131 count++;
28575f16 132 }
697cc9c8
DV
133 card_res->count_crtcs = count;
134
135 count = 0;
136 encoder_id = u64_to_user_ptr(card_res->encoder_id_ptr);
137 drm_for_each_encoder(encoder, dev) {
138 if (count < card_res->count_encoders &&
139 put_user(encoder->base.id, encoder_id + count)) {
140 ret = -EFAULT;
141 goto out;
28575f16 142 }
697cc9c8 143 count++;
28575f16 144 }
697cc9c8
DV
145 card_res->count_encoders = count;
146
147 count = 0;
148 connector_id = u64_to_user_ptr(card_res->connector_id_ptr);
149 drm_for_each_connector(connector, dev) {
150 if (count < card_res->count_connectors &&
151 put_user(connector->base.id, connector_id + count)) {
152 ret = -EFAULT;
153 goto out;
28575f16 154 }
697cc9c8 155 count++;
28575f16 156 }
697cc9c8 157 card_res->count_connectors = count;
28575f16
DV
158
159out:
160 mutex_unlock(&dev->mode_config.mutex);
161 return ret;
162}
163
164/**
165 * drm_mode_config_reset - call ->reset callbacks
166 * @dev: drm device
167 *
168 * This functions calls all the crtc's, encoder's and connector's ->reset
169 * callback. Drivers can use this in e.g. their driver load or resume code to
170 * reset hardware and software state.
171 */
172void drm_mode_config_reset(struct drm_device *dev)
173{
174 struct drm_crtc *crtc;
175 struct drm_plane *plane;
176 struct drm_encoder *encoder;
177 struct drm_connector *connector;
178
179 drm_for_each_plane(plane, dev)
180 if (plane->funcs->reset)
181 plane->funcs->reset(plane);
182
183 drm_for_each_crtc(crtc, dev)
184 if (crtc->funcs->reset)
185 crtc->funcs->reset(crtc);
186
187 drm_for_each_encoder(encoder, dev)
188 if (encoder->funcs->reset)
189 encoder->funcs->reset(encoder);
190
191 mutex_lock(&dev->mode_config.mutex);
192 drm_for_each_connector(connector, dev)
193 if (connector->funcs->reset)
194 connector->funcs->reset(connector);
195 mutex_unlock(&dev->mode_config.mutex);
196}
197EXPORT_SYMBOL(drm_mode_config_reset);
198
199/*
200 * Global properties
201 */
202static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
203 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
204 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
205 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
206};
207
208static int drm_mode_create_standard_properties(struct drm_device *dev)
209{
210 struct drm_property *prop;
211 int ret;
212
213 ret = drm_connector_create_standard_properties(dev);
214 if (ret)
215 return ret;
216
217 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
218 "type", drm_plane_type_enum_list,
219 ARRAY_SIZE(drm_plane_type_enum_list));
220 if (!prop)
221 return -ENOMEM;
222 dev->mode_config.plane_type_property = prop;
223
224 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
225 "SRC_X", 0, UINT_MAX);
226 if (!prop)
227 return -ENOMEM;
228 dev->mode_config.prop_src_x = prop;
229
230 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
231 "SRC_Y", 0, UINT_MAX);
232 if (!prop)
233 return -ENOMEM;
234 dev->mode_config.prop_src_y = prop;
235
236 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
237 "SRC_W", 0, UINT_MAX);
238 if (!prop)
239 return -ENOMEM;
240 dev->mode_config.prop_src_w = prop;
241
242 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
243 "SRC_H", 0, UINT_MAX);
244 if (!prop)
245 return -ENOMEM;
246 dev->mode_config.prop_src_h = prop;
247
248 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
249 "CRTC_X", INT_MIN, INT_MAX);
250 if (!prop)
251 return -ENOMEM;
252 dev->mode_config.prop_crtc_x = prop;
253
254 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
255 "CRTC_Y", INT_MIN, INT_MAX);
256 if (!prop)
257 return -ENOMEM;
258 dev->mode_config.prop_crtc_y = prop;
259
260 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
261 "CRTC_W", 0, INT_MAX);
262 if (!prop)
263 return -ENOMEM;
264 dev->mode_config.prop_crtc_w = prop;
265
266 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
267 "CRTC_H", 0, INT_MAX);
268 if (!prop)
269 return -ENOMEM;
270 dev->mode_config.prop_crtc_h = prop;
271
272 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
273 "FB_ID", DRM_MODE_OBJECT_FB);
274 if (!prop)
275 return -ENOMEM;
276 dev->mode_config.prop_fb_id = prop;
277
96260142
GP
278 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
279 "IN_FENCE_FD", -1, INT_MAX);
280 if (!prop)
281 return -ENOMEM;
282 dev->mode_config.prop_in_fence_fd = prop;
283
beaf5af4
GP
284 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
285 "OUT_FENCE_PTR", 0, U64_MAX);
286 if (!prop)
287 return -ENOMEM;
288 dev->mode_config.prop_out_fence_ptr = prop;
289
28575f16
DV
290 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
291 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
292 if (!prop)
293 return -ENOMEM;
294 dev->mode_config.prop_crtc_id = prop;
295
296 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
297 "ACTIVE");
298 if (!prop)
299 return -ENOMEM;
300 dev->mode_config.prop_active = prop;
301
302 prop = drm_property_create(dev,
303 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
304 "MODE_ID", 0);
305 if (!prop)
306 return -ENOMEM;
307 dev->mode_config.prop_mode_id = prop;
308
309 prop = drm_property_create(dev,
310 DRM_MODE_PROP_BLOB,
311 "DEGAMMA_LUT", 0);
312 if (!prop)
313 return -ENOMEM;
314 dev->mode_config.degamma_lut_property = prop;
315
316 prop = drm_property_create_range(dev,
317 DRM_MODE_PROP_IMMUTABLE,
318 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
319 if (!prop)
320 return -ENOMEM;
321 dev->mode_config.degamma_lut_size_property = prop;
322
323 prop = drm_property_create(dev,
324 DRM_MODE_PROP_BLOB,
325 "CTM", 0);
326 if (!prop)
327 return -ENOMEM;
328 dev->mode_config.ctm_property = prop;
329
330 prop = drm_property_create(dev,
331 DRM_MODE_PROP_BLOB,
332 "GAMMA_LUT", 0);
333 if (!prop)
334 return -ENOMEM;
335 dev->mode_config.gamma_lut_property = prop;
336
337 prop = drm_property_create_range(dev,
338 DRM_MODE_PROP_IMMUTABLE,
339 "GAMMA_LUT_SIZE", 0, UINT_MAX);
340 if (!prop)
341 return -ENOMEM;
342 dev->mode_config.gamma_lut_size_property = prop;
343
344 return 0;
345}
346
347/**
348 * drm_mode_config_init - initialize DRM mode_configuration structure
349 * @dev: DRM device
350 *
351 * Initialize @dev's mode_config structure, used for tracking the graphics
352 * configuration of @dev.
353 *
354 * Since this initializes the modeset locks, no locking is possible. Which is no
355 * problem, since this should happen single threaded at init time. It is the
356 * driver's problem to ensure this guarantee.
357 *
358 */
359void drm_mode_config_init(struct drm_device *dev)
360{
361 mutex_init(&dev->mode_config.mutex);
362 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
363 mutex_init(&dev->mode_config.idr_mutex);
364 mutex_init(&dev->mode_config.fb_lock);
365 mutex_init(&dev->mode_config.blob_lock);
366 INIT_LIST_HEAD(&dev->mode_config.fb_list);
367 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
368 INIT_LIST_HEAD(&dev->mode_config.connector_list);
369 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
370 INIT_LIST_HEAD(&dev->mode_config.property_list);
371 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
372 INIT_LIST_HEAD(&dev->mode_config.plane_list);
373 idr_init(&dev->mode_config.crtc_idr);
374 idr_init(&dev->mode_config.tile_idr);
375 ida_init(&dev->mode_config.connector_ida);
376
377 drm_modeset_lock_all(dev);
378 drm_mode_create_standard_properties(dev);
379 drm_modeset_unlock_all(dev);
380
381 /* Just to be sure */
382 dev->mode_config.num_fb = 0;
383 dev->mode_config.num_connector = 0;
384 dev->mode_config.num_crtc = 0;
385 dev->mode_config.num_encoder = 0;
386 dev->mode_config.num_overlay_plane = 0;
387 dev->mode_config.num_total_plane = 0;
388}
389EXPORT_SYMBOL(drm_mode_config_init);
390
391/**
392 * drm_mode_config_cleanup - free up DRM mode_config info
393 * @dev: DRM device
394 *
395 * Free up all the connectors and CRTCs associated with this DRM device, then
396 * free up the framebuffers and associated buffer objects.
397 *
398 * Note that since this /should/ happen single-threaded at driver/device
399 * teardown time, no locking is required. It's the driver's job to ensure that
400 * this guarantee actually holds true.
401 *
402 * FIXME: cleanup any dangling user buffer objects too
403 */
404void drm_mode_config_cleanup(struct drm_device *dev)
405{
406 struct drm_connector *connector, *ot;
407 struct drm_crtc *crtc, *ct;
408 struct drm_encoder *encoder, *enct;
409 struct drm_framebuffer *fb, *fbt;
410 struct drm_property *property, *pt;
411 struct drm_property_blob *blob, *bt;
412 struct drm_plane *plane, *plt;
413
414 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
415 head) {
416 encoder->funcs->destroy(encoder);
417 }
418
419 list_for_each_entry_safe(connector, ot,
420 &dev->mode_config.connector_list, head) {
421 connector->funcs->destroy(connector);
422 }
423
424 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
425 head) {
426 drm_property_destroy(dev, property);
427 }
428
429 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
430 head) {
431 plane->funcs->destroy(plane);
432 }
433
434 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
435 crtc->funcs->destroy(crtc);
436 }
437
438 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
439 head_global) {
440 drm_property_unreference_blob(blob);
441 }
442
443 /*
444 * Single-threaded teardown context, so it's not required to grab the
445 * fb_lock to protect against concurrent fb_list access. Contrary, it
446 * would actually deadlock with the drm_framebuffer_cleanup function.
447 *
448 * Also, if there are any framebuffers left, that's a driver leak now,
449 * so politely WARN about this.
450 */
451 WARN_ON(!list_empty(&dev->mode_config.fb_list));
452 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
453 drm_framebuffer_free(&fb->base.refcount);
454 }
455
456 ida_destroy(&dev->mode_config.connector_ida);
457 idr_destroy(&dev->mode_config.tile_idr);
458 idr_destroy(&dev->mode_config.crtc_idr);
459 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
460}
461EXPORT_SYMBOL(drm_mode_config_cleanup);