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