Subversion Repositories Kolibri OS

Rev

Rev 3192 | Rev 3480 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1123 serge 1
/*
2
 * Copyright (c) 2006-2008 Intel Corporation
3
 * Copyright (c) 2007 Dave Airlie 
4
 *
5
 * DRM core CRTC related functions
6
 *
7
 * Permission to use, copy, modify, distribute, and sell this software and its
8
 * documentation for any purpose is hereby granted without fee, provided that
9
 * the above copyright notice appear in all copies and that both that copyright
10
 * notice and this permission notice appear in supporting documentation, and
11
 * that the name of the copyright holders not be used in advertising or
12
 * publicity pertaining to distribution of the software without specific,
13
 * written prior permission.  The copyright holders make no representations
14
 * about the suitability of this software for any purpose.  It is provided "as
15
 * is" without express or implied warranty.
16
 *
17
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23
 * OF THIS SOFTWARE.
24
 *
25
 * Authors:
26
 *      Keith Packard
27
 *	Eric Anholt 
28
 *      Dave Airlie 
29
 *      Jesse Barnes 
30
 */
31
 
3031 serge 32
#include 
33
#include 
1123 serge 34
 
3031 serge 35
#include 
36
#include 
37
#include 
38
#include 
39
#include 
40
#include 
41
 
3192 Serge 42
/**
43
 * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
44
 * 						connector list
45
 * @dev: drm device to operate on
46
 *
47
 * Some userspace presumes that the first connected connector is the main
48
 * display, where it's supposed to display e.g. the login screen. For
49
 * laptops, this should be the main panel. Use this function to sort all
50
 * (eDP/LVDS) panels to the front of the connector list, instead of
51
 * painstakingly trying to initialize them in the right order.
52
 */
53
void drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
54
{
55
	struct drm_connector *connector, *tmp;
56
	struct list_head panel_list;
57
 
58
	INIT_LIST_HEAD(&panel_list);
59
 
60
	list_for_each_entry_safe(connector, tmp,
61
				 &dev->mode_config.connector_list, head) {
62
		if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
63
		    connector->connector_type == DRM_MODE_CONNECTOR_eDP)
64
			list_move_tail(&connector->head, &panel_list);
65
	}
66
 
67
	list_splice(&panel_list, &dev->mode_config.connector_list);
68
}
69
EXPORT_SYMBOL(drm_helper_move_panel_connectors_to_head);
70
 
1963 serge 71
static bool drm_kms_helper_poll = true;
72
 
1123 serge 73
static void drm_mode_validate_flag(struct drm_connector *connector,
74
				   int flags)
75
{
3031 serge 76
	struct drm_display_mode *mode;
1123 serge 77
 
78
	if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
79
		return;
80
 
3031 serge 81
	list_for_each_entry(mode, &connector->modes, head) {
1123 serge 82
		if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
83
				!(flags & DRM_MODE_FLAG_INTERLACE))
84
			mode->status = MODE_NO_INTERLACE;
85
		if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
86
				!(flags & DRM_MODE_FLAG_DBLSCAN))
87
			mode->status = MODE_NO_DBLESCAN;
88
	}
89
 
90
	return;
91
}
92
 
93
/**
1963 serge 94
 * drm_helper_probe_single_connector_modes - get complete set of display modes
3192 Serge 95
 * @connector: connector to probe
1123 serge 96
 * @maxX: max width for modes
97
 * @maxY: max height for modes
98
 *
99
 * LOCKING:
100
 * Caller must hold mode config lock.
101
 *
3192 Serge 102
 * Based on the helper callbacks implemented by @connector try to detect all
103
 * valid modes.  Modes will first be added to the connector's probed_modes list,
104
 * then culled (based on validity and the @maxX, @maxY parameters) and put into
105
 * the normal modes list.
1123 serge 106
 *
3192 Serge 107
 * Intended to be use as a generic implementation of the ->probe() @connector
108
 * callback for drivers that use the crtc helpers for output mode filtering and
109
 * detection.
1123 serge 110
 *
111
 * RETURNS:
112
 * Number of modes found on @connector.
113
 */
114
int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
115
					    uint32_t maxX, uint32_t maxY)
116
{
117
	struct drm_device *dev = connector->dev;
3031 serge 118
	struct drm_display_mode *mode;
1123 serge 119
	struct drm_connector_helper_funcs *connector_funcs =
120
		connector->helper_private;
121
	int count = 0;
122
	int mode_flags = 0;
123
 
1963 serge 124
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
125
			drm_get_connector_name(connector));
1123 serge 126
	/* set all modes to the unverified state */
3031 serge 127
	list_for_each_entry(mode, &connector->modes, head)
1123 serge 128
		mode->status = MODE_UNVERIFIED;
129
 
1221 serge 130
	if (connector->force) {
131
		if (connector->force == DRM_FORCE_ON)
132
			connector->status = connector_status_connected;
133
		else
134
			connector->status = connector_status_disconnected;
135
		if (connector->funcs->force)
136
			connector->funcs->force(connector);
1963 serge 137
	} else {
3243 Serge 138
//        dbgprintf("call detect funcs %p ", connector->funcs);
139
//        dbgprintf("detect %p\n", connector->funcs->detect);
1963 serge 140
		connector->status = connector->funcs->detect(connector, true);
3243 Serge 141
//        dbgprintf("status %x\n", connector->status);
1963 serge 142
	}
1123 serge 143
 
144
	if (connector->status == connector_status_disconnected) {
1963 serge 145
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
146
			connector->base.id, drm_get_connector_name(connector));
147
		drm_mode_connector_update_edid_property(connector, NULL);
1179 serge 148
		goto prune;
1123 serge 149
	}
150
 
3031 serge 151
#ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
152
	count = drm_load_edid_firmware(connector);
153
	if (count == 0)
154
#endif
1123 serge 155
	count = (*connector_funcs->get_modes)(connector);
3031 serge 156
 
1963 serge 157
	if (count == 0 && connector->status == connector_status_connected)
1321 serge 158
		count = drm_add_modes_noedid(connector, 1024, 768);
1963 serge 159
	if (count == 0)
160
		goto prune;
1123 serge 161
 
162
	drm_mode_connector_list_update(connector);
163
 
164
	if (maxX && maxY)
165
		drm_mode_validate_size(dev, &connector->modes, maxX,
166
				       maxY, 0);
167
 
168
	if (connector->interlace_allowed)
169
		mode_flags |= DRM_MODE_FLAG_INTERLACE;
170
	if (connector->doublescan_allowed)
171
		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
172
	drm_mode_validate_flag(connector, mode_flags);
173
 
3031 serge 174
	list_for_each_entry(mode, &connector->modes, head) {
1123 serge 175
		if (mode->status == MODE_OK)
176
			mode->status = connector_funcs->mode_valid(connector,
177
								   mode);
178
	}
179
 
1179 serge 180
prune:
1123 serge 181
	drm_mode_prune_invalid(dev, &connector->modes, true);
182
 
183
	if (list_empty(&connector->modes))
184
		return 0;
185
 
186
	drm_mode_sort(&connector->modes);
187
 
1963 serge 188
	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
1179 serge 189
				drm_get_connector_name(connector));
3031 serge 190
	list_for_each_entry(mode, &connector->modes, head) {
1123 serge 191
		mode->vrefresh = drm_mode_vrefresh(mode);
192
 
193
		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
194
		drm_mode_debug_printmodeline(mode);
195
	}
196
 
197
	return count;
198
}
199
EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
200
 
201
/**
202
 * drm_helper_encoder_in_use - check if a given encoder is in use
203
 * @encoder: encoder to check
204
 *
205
 * LOCKING:
206
 * Caller must hold mode config lock.
207
 *
208
 * Walk @encoders's DRM device's mode_config and see if it's in use.
209
 *
210
 * RETURNS:
211
 * True if @encoder is part of the mode_config, false otherwise.
212
 */
213
bool drm_helper_encoder_in_use(struct drm_encoder *encoder)
214
{
215
	struct drm_connector *connector;
216
	struct drm_device *dev = encoder->dev;
217
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
218
		if (connector->encoder == encoder)
219
			return true;
220
	return false;
221
}
222
EXPORT_SYMBOL(drm_helper_encoder_in_use);
223
 
224
/**
225
 * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
226
 * @crtc: CRTC to check
227
 *
228
 * LOCKING:
229
 * Caller must hold mode config lock.
230
 *
231
 * Walk @crtc's DRM device's mode_config and see if it's in use.
232
 *
233
 * RETURNS:
234
 * True if @crtc is part of the mode_config, false otherwise.
235
 */
236
bool drm_helper_crtc_in_use(struct drm_crtc *crtc)
237
{
238
	struct drm_encoder *encoder;
239
	struct drm_device *dev = crtc->dev;
240
	/* FIXME: Locking around list access? */
241
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
242
		if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
243
			return true;
244
	return false;
245
}
246
EXPORT_SYMBOL(drm_helper_crtc_in_use);
247
 
1963 serge 248
static void
249
drm_encoder_disable(struct drm_encoder *encoder)
250
{
251
	struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
252
 
253
	if (encoder_funcs->disable)
254
		(*encoder_funcs->disable)(encoder);
255
	else
256
		(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
257
}
258
 
1123 serge 259
/**
1404 serge 260
 * drm_helper_disable_unused_functions - disable unused objects
1123 serge 261
 * @dev: DRM device
262
 *
263
 * LOCKING:
264
 * Caller must hold mode config lock.
265
 *
266
 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
267
 * by calling its dpms function, which should power it off.
268
 */
269
void drm_helper_disable_unused_functions(struct drm_device *dev)
270
{
271
	struct drm_encoder *encoder;
1179 serge 272
	struct drm_connector *connector;
1123 serge 273
	struct drm_crtc *crtc;
274
 
1179 serge 275
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
276
		if (!connector->encoder)
277
			continue;
278
		if (connector->status == connector_status_disconnected)
279
			connector->encoder = NULL;
280
	}
281
 
1123 serge 282
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1179 serge 283
		if (!drm_helper_encoder_in_use(encoder)) {
1963 serge 284
			drm_encoder_disable(encoder);
1179 serge 285
			/* disconnector encoder from any connector */
286
			encoder->crtc = NULL;
287
		}
1123 serge 288
	}
289
 
290
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
291
		struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
292
		crtc->enabled = drm_helper_crtc_in_use(crtc);
293
		if (!crtc->enabled) {
1963 serge 294
			if (crtc_funcs->disable)
295
				(*crtc_funcs->disable)(crtc);
296
			else
297
				(*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
1123 serge 298
			crtc->fb = NULL;
299
		}
300
	}
301
}
302
EXPORT_SYMBOL(drm_helper_disable_unused_functions);
303
 
304
/**
305
 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
306
 * @encoder: encoder to test
307
 * @crtc: crtc to test
308
 *
309
 * Return false if @encoder can't be driven by @crtc, true otherwise.
310
 */
311
static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
312
				struct drm_crtc *crtc)
313
{
314
	struct drm_device *dev;
315
	struct drm_crtc *tmp;
316
	int crtc_mask = 1;
317
 
1963 serge 318
	WARN(!crtc, "checking null crtc?\n");
1123 serge 319
 
320
	dev = crtc->dev;
321
 
322
	list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
323
		if (tmp == crtc)
324
			break;
325
		crtc_mask <<= 1;
326
	}
327
 
328
	if (encoder->possible_crtcs & crtc_mask)
329
		return true;
330
	return false;
331
}
332
 
333
/*
334
 * Check the CRTC we're going to map each output to vs. its current
335
 * CRTC.  If they don't match, we have to disable the output and the CRTC
336
 * since the driver will have to re-route things.
337
 */
338
static void
339
drm_crtc_prepare_encoders(struct drm_device *dev)
340
{
341
	struct drm_encoder_helper_funcs *encoder_funcs;
342
	struct drm_encoder *encoder;
343
 
344
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
345
		encoder_funcs = encoder->helper_private;
346
		/* Disable unused encoders */
347
		if (encoder->crtc == NULL)
1963 serge 348
			drm_encoder_disable(encoder);
1123 serge 349
		/* Disable encoders whose CRTC is about to change */
350
		if (encoder_funcs->get_crtc &&
351
		    encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
1963 serge 352
			drm_encoder_disable(encoder);
1123 serge 353
	}
354
}
355
 
356
/**
3192 Serge 357
 * drm_crtc_helper_set_mode - internal helper to set a mode
1123 serge 358
 * @crtc: CRTC to program
359
 * @mode: mode to use
3192 Serge 360
 * @x: horizontal offset into the surface
361
 * @y: vertical offset into the surface
362
 * @old_fb: old framebuffer, for cleanup
1123 serge 363
 *
364
 * LOCKING:
365
 * Caller must hold mode config lock.
366
 *
367
 * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
3192 Serge 368
 * to fixup or reject the mode prior to trying to set it. This is an internal
369
 * helper that drivers could e.g. use to update properties that require the
370
 * entire output pipe to be disabled and re-enabled in a new configuration. For
371
 * example for changing whether audio is enabled on a hdmi link or for changing
372
 * panel fitter or dither attributes. It is also called by the
373
 * drm_crtc_helper_set_config() helper function to drive the mode setting
374
 * sequence.
1123 serge 375
 *
376
 * RETURNS:
377
 * True if the mode was set successfully, or false otherwise.
378
 */
379
bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
380
			      struct drm_display_mode *mode,
381
			      int x, int y,
382
			      struct drm_framebuffer *old_fb)
383
{
384
	struct drm_device *dev = crtc->dev;
1963 serge 385
	struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
1123 serge 386
	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
387
	struct drm_encoder_helper_funcs *encoder_funcs;
388
	int saved_x, saved_y;
389
	struct drm_encoder *encoder;
390
	bool ret = true;
391
 
392
	crtc->enabled = drm_helper_crtc_in_use(crtc);
393
	if (!crtc->enabled)
394
		return true;
395
 
1963 serge 396
	adjusted_mode = drm_mode_duplicate(dev, mode);
3031 serge 397
	if (!adjusted_mode)
398
		return false;
1963 serge 399
 
400
	saved_hwmode = crtc->hwmode;
1123 serge 401
	saved_mode = crtc->mode;
402
	saved_x = crtc->x;
403
	saved_y = crtc->y;
404
 
405
	/* Update crtc values up front so the driver can rely on them for mode
406
	 * setting.
407
	 */
408
	crtc->mode = *mode;
409
	crtc->x = x;
410
	crtc->y = y;
411
 
412
	/* Pass our mode to the connectors and the CRTC to give them a chance to
413
	 * adjust it according to limitations or connector properties, and also
414
	 * a chance to reject the mode entirely.
415
	 */
416
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
417
 
418
		if (encoder->crtc != crtc)
419
			continue;
420
		encoder_funcs = encoder->helper_private;
421
		if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
422
						      adjusted_mode))) {
3031 serge 423
			DRM_DEBUG_KMS("Encoder fixup failed\n");
1123 serge 424
			goto done;
425
		}
426
	}
427
 
428
	if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
3031 serge 429
		DRM_DEBUG_KMS("CRTC fixup failed\n");
1123 serge 430
		goto done;
431
	}
1963 serge 432
	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1123 serge 433
 
434
	/* Prepare the encoders and CRTCs before setting the mode. */
435
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
436
 
437
		if (encoder->crtc != crtc)
438
			continue;
439
		encoder_funcs = encoder->helper_private;
440
		/* Disable the encoders as the first thing we do. */
441
		encoder_funcs->prepare(encoder);
442
	}
443
 
444
	drm_crtc_prepare_encoders(dev);
445
 
446
	crtc_funcs->prepare(crtc);
447
 
448
	/* Set up the DPLL and any encoders state that needs to adjust or depend
449
	 * on the DPLL.
450
	 */
451
	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
452
	if (!ret)
453
	    goto done;
454
 
455
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
456
 
457
		if (encoder->crtc != crtc)
458
			continue;
459
 
1963 serge 460
		DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
461
			encoder->base.id, drm_get_encoder_name(encoder),
462
			mode->base.id, mode->name);
1123 serge 463
		encoder_funcs = encoder->helper_private;
464
		encoder_funcs->mode_set(encoder, mode, adjusted_mode);
465
	}
466
 
467
	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
468
	crtc_funcs->commit(crtc);
469
 
470
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
471
 
472
		if (encoder->crtc != crtc)
473
			continue;
474
 
475
		encoder_funcs = encoder->helper_private;
476
		encoder_funcs->commit(encoder);
477
 
478
	}
479
 
1963 serge 480
	/* Store real post-adjustment hardware mode. */
481
	crtc->hwmode = *adjusted_mode;
482
 
483
	/* Calculate and store various constants which
484
	 * are later needed by vblank and swap-completion
485
	 * timestamping. They are derived from true hwmode.
486
	 */
487
	drm_calc_timestamping_constants(crtc);
488
 
1123 serge 489
	/* FIXME: add subpixel order */
490
done:
1963 serge 491
	drm_mode_destroy(dev, adjusted_mode);
1123 serge 492
	if (!ret) {
1963 serge 493
		crtc->hwmode = saved_hwmode;
1123 serge 494
		crtc->mode = saved_mode;
495
		crtc->x = saved_x;
496
		crtc->y = saved_y;
497
	}
1179 serge 498
 
1123 serge 499
	return ret;
500
}
501
EXPORT_SYMBOL(drm_crtc_helper_set_mode);
502
 
503
 
3031 serge 504
static int
505
drm_crtc_helper_disable(struct drm_crtc *crtc)
506
{
507
	struct drm_device *dev = crtc->dev;
508
	struct drm_connector *connector;
509
	struct drm_encoder *encoder;
510
 
511
	/* Decouple all encoders and their attached connectors from this crtc */
512
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
513
		if (encoder->crtc != crtc)
514
			continue;
515
 
516
		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
517
			if (connector->encoder != encoder)
518
				continue;
519
 
520
			connector->encoder = NULL;
521
		}
522
	}
523
 
524
	drm_helper_disable_unused_functions(dev);
525
	return 0;
526
}
527
 
1123 serge 528
/**
529
 * drm_crtc_helper_set_config - set a new config from userspace
3192 Serge 530
 * @set: mode set configuration
1123 serge 531
 *
532
 * LOCKING:
533
 * Caller must hold mode config lock.
534
 *
3192 Serge 535
 * Setup a new configuration, provided by the upper layers (either an ioctl call
536
 * from userspace or internally e.g. from the fbdev suppport code) in @set, and
537
 * enable it. This is the main helper functions for drivers that implement
538
 * kernel mode setting with the crtc helper functions and the assorted
539
 * ->prepare(), ->modeset() and ->commit() helper callbacks.
1123 serge 540
 *
541
 * RETURNS:
3192 Serge 542
 * Returns 0 on success, -ERRNO on failure.
1123 serge 543
 */
544
int drm_crtc_helper_set_config(struct drm_mode_set *set)
545
{
546
	struct drm_device *dev;
1179 serge 547
	struct drm_crtc *save_crtcs, *new_crtc, *crtc;
548
	struct drm_encoder *save_encoders, *new_encoder, *encoder;
1123 serge 549
	struct drm_framebuffer *old_fb = NULL;
1179 serge 550
	bool mode_changed = false; /* if true do a full mode set */
551
	bool fb_changed = false; /* if true and !mode_changed just do a flip */
552
	struct drm_connector *save_connectors, *connector;
1123 serge 553
	int count = 0, ro, fail = 0;
554
	struct drm_crtc_helper_funcs *crtc_funcs;
3031 serge 555
	struct drm_mode_set save_set;
556
	int ret;
1963 serge 557
	int i;
1123 serge 558
 
1179 serge 559
	DRM_DEBUG_KMS("\n");
1123 serge 560
 
561
	if (!set)
562
		return -EINVAL;
563
 
564
	if (!set->crtc)
565
		return -EINVAL;
566
 
567
	if (!set->crtc->helper_private)
568
		return -EINVAL;
569
 
570
	crtc_funcs = set->crtc->helper_private;
571
 
1963 serge 572
	if (!set->mode)
573
		set->fb = NULL;
574
 
575
	if (set->fb) {
576
		DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
577
				set->crtc->base.id, set->fb->base.id,
1123 serge 578
		  (int)set->num_connectors, set->x, set->y);
1963 serge 579
	} else {
580
		DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
3031 serge 581
		return drm_crtc_helper_disable(set->crtc);
1963 serge 582
	}
1123 serge 583
 
584
	dev = set->crtc->dev;
585
 
1179 serge 586
	/* Allocate space for the backup of all (non-pointer) crtc, encoder and
587
	 * connector data. */
588
	save_crtcs = kzalloc(dev->mode_config.num_crtc *
589
			     sizeof(struct drm_crtc), GFP_KERNEL);
1123 serge 590
	if (!save_crtcs)
591
		return -ENOMEM;
592
 
1179 serge 593
	save_encoders = kzalloc(dev->mode_config.num_encoder *
594
				sizeof(struct drm_encoder), GFP_KERNEL);
1123 serge 595
	if (!save_encoders) {
596
		kfree(save_crtcs);
597
		return -ENOMEM;
598
	}
599
 
1179 serge 600
	save_connectors = kzalloc(dev->mode_config.num_connector *
601
				sizeof(struct drm_connector), GFP_KERNEL);
602
	if (!save_connectors) {
603
		kfree(save_crtcs);
604
		kfree(save_encoders);
605
		return -ENOMEM;
606
	}
607
 
608
	/* Copy data. Note that driver private data is not affected.
609
	 * Should anything bad happen only the expected state is
610
	 * restored, not the drivers personal bookkeeping.
611
	 */
612
	count = 0;
613
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
614
		save_crtcs[count++] = *crtc;
615
	}
616
 
617
	count = 0;
618
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
619
		save_encoders[count++] = *encoder;
620
	}
621
 
622
	count = 0;
623
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
624
		save_connectors[count++] = *connector;
625
	}
626
 
3031 serge 627
	save_set.crtc = set->crtc;
628
	save_set.mode = &set->crtc->mode;
629
	save_set.x = set->crtc->x;
630
	save_set.y = set->crtc->y;
631
	save_set.fb = set->crtc->fb;
632
 
1123 serge 633
	/* We should be able to check here if the fb has the same properties
634
	 * and then just flip_or_move it */
635
	if (set->crtc->fb != set->fb) {
636
		/* If we have no fb then treat it as a full mode set */
637
		if (set->crtc->fb == NULL) {
1179 serge 638
			DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
1123 serge 639
			mode_changed = true;
1179 serge 640
		} else if (set->fb == NULL) {
641
			mode_changed = true;
2160 serge 642
		} else if (set->fb->depth != set->crtc->fb->depth) {
643
			mode_changed = true;
644
		} else if (set->fb->bits_per_pixel !=
645
			   set->crtc->fb->bits_per_pixel) {
646
			mode_changed = true;
1430 serge 647
		} else
1123 serge 648
			fb_changed = true;
649
	}
650
 
651
	if (set->x != set->crtc->x || set->y != set->crtc->y)
652
		fb_changed = true;
653
 
654
	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
1179 serge 655
		DRM_DEBUG_KMS("modes are different, full mode set\n");
1123 serge 656
		drm_mode_debug_printmodeline(&set->crtc->mode);
657
		drm_mode_debug_printmodeline(set->mode);
658
		mode_changed = true;
659
	}
660
 
661
	/* a) traverse passed in connector list and get encoders for them */
662
	count = 0;
663
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
664
		struct drm_connector_helper_funcs *connector_funcs =
665
			connector->helper_private;
666
		new_encoder = connector->encoder;
667
		for (ro = 0; ro < set->num_connectors; ro++) {
668
			if (set->connectors[ro] == connector) {
669
				new_encoder = connector_funcs->best_encoder(connector);
670
				/* if we can't get an encoder for a connector
671
				   we are setting now - then fail */
672
				if (new_encoder == NULL)
673
					/* don't break so fail path works correct */
674
					fail = 1;
675
				break;
676
			}
677
		}
678
 
679
		if (new_encoder != connector->encoder) {
1179 serge 680
			DRM_DEBUG_KMS("encoder changed, full mode switch\n");
1123 serge 681
			mode_changed = true;
1179 serge 682
			/* If the encoder is reused for another connector, then
683
			 * the appropriate crtc will be set later.
684
			 */
685
			if (connector->encoder)
686
				connector->encoder->crtc = NULL;
1123 serge 687
			connector->encoder = new_encoder;
688
		}
689
	}
690
 
691
	if (fail) {
692
		ret = -EINVAL;
1179 serge 693
		goto fail;
1123 serge 694
	}
695
 
696
	count = 0;
697
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
698
		if (!connector->encoder)
699
			continue;
700
 
701
		if (connector->encoder->crtc == set->crtc)
702
			new_crtc = NULL;
703
		else
704
			new_crtc = connector->encoder->crtc;
705
 
706
		for (ro = 0; ro < set->num_connectors; ro++) {
707
			if (set->connectors[ro] == connector)
708
				new_crtc = set->crtc;
709
		}
710
 
711
		/* Make sure the new CRTC will work with the encoder */
712
		if (new_crtc &&
713
		    !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
714
			ret = -EINVAL;
1179 serge 715
			goto fail;
1123 serge 716
		}
717
		if (new_crtc != connector->encoder->crtc) {
1179 serge 718
			DRM_DEBUG_KMS("crtc changed, full mode switch\n");
1123 serge 719
			mode_changed = true;
720
			connector->encoder->crtc = new_crtc;
721
		}
1963 serge 722
		if (new_crtc) {
723
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
724
				connector->base.id, drm_get_connector_name(connector),
725
				new_crtc->base.id);
726
		} else {
727
			DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
728
				connector->base.id, drm_get_connector_name(connector));
729
		}
1123 serge 730
	}
731
 
732
	/* mode_set_base is not a required function */
733
	if (fb_changed && !crtc_funcs->mode_set_base)
734
		mode_changed = true;
735
 
736
	if (mode_changed) {
1963 serge 737
		set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
738
		if (set->crtc->enabled) {
1179 serge 739
			DRM_DEBUG_KMS("attempting to set mode from"
740
					" userspace\n");
1123 serge 741
			drm_mode_debug_printmodeline(set->mode);
1963 serge 742
			old_fb = set->crtc->fb;
743
			set->crtc->fb = set->fb;
1123 serge 744
			if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
745
						      set->x, set->y,
746
						      old_fb)) {
1963 serge 747
				DRM_ERROR("failed to set mode on [CRTC:%d]\n",
748
					  set->crtc->base.id);
749
				set->crtc->fb = old_fb;
1123 serge 750
				ret = -EINVAL;
1179 serge 751
				goto fail;
1123 serge 752
			}
1963 serge 753
			DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
754
			for (i = 0; i < set->num_connectors; i++) {
755
				DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
756
					      drm_get_connector_name(set->connectors[i]));
3031 serge 757
				set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
1963 serge 758
			}
1123 serge 759
		}
760
		drm_helper_disable_unused_functions(dev);
761
	} else if (fb_changed) {
1179 serge 762
		set->crtc->x = set->x;
763
		set->crtc->y = set->y;
764
 
1123 serge 765
		old_fb = set->crtc->fb;
766
		if (set->crtc->fb != set->fb)
767
			set->crtc->fb = set->fb;
768
		ret = crtc_funcs->mode_set_base(set->crtc,
769
						set->x, set->y, old_fb);
1963 serge 770
		if (ret != 0) {
771
			set->crtc->fb = old_fb;
1179 serge 772
			goto fail;
1123 serge 773
	}
1963 serge 774
	}
1123 serge 775
 
1179 serge 776
	kfree(save_connectors);
1123 serge 777
	kfree(save_encoders);
778
	kfree(save_crtcs);
779
	return 0;
780
 
1179 serge 781
fail:
782
	/* Restore all previous data. */
1123 serge 783
	count = 0;
1179 serge 784
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
785
		*crtc = save_crtcs[count++];
786
	}
1123 serge 787
 
1179 serge 788
	count = 0;
789
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
790
		*encoder = save_encoders[count++];
1123 serge 791
	}
1179 serge 792
 
1123 serge 793
	count = 0;
794
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1179 serge 795
		*connector = save_connectors[count++];
1123 serge 796
	}
1179 serge 797
 
3031 serge 798
	/* Try to restore the config */
799
	if (mode_changed &&
800
	    !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
801
				      save_set.y, save_set.fb))
802
		DRM_ERROR("failed to restore config after modeset failure\n");
803
 
1179 serge 804
	kfree(save_connectors);
1123 serge 805
	kfree(save_encoders);
1179 serge 806
	kfree(save_crtcs);
1123 serge 807
	return ret;
808
}
809
EXPORT_SYMBOL(drm_crtc_helper_set_config);
810
 
811
static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
812
{
813
	int dpms = DRM_MODE_DPMS_OFF;
814
	struct drm_connector *connector;
815
	struct drm_device *dev = encoder->dev;
816
 
817
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
818
		if (connector->encoder == encoder)
819
			if (connector->dpms < dpms)
820
				dpms = connector->dpms;
821
	return dpms;
822
}
823
 
824
static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
825
{
826
	int dpms = DRM_MODE_DPMS_OFF;
827
	struct drm_connector *connector;
828
	struct drm_device *dev = crtc->dev;
829
 
830
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
831
		if (connector->encoder && connector->encoder->crtc == crtc)
832
			if (connector->dpms < dpms)
833
				dpms = connector->dpms;
834
	return dpms;
835
}
836
 
837
/**
3192 Serge 838
 * drm_helper_connector_dpms() - connector dpms helper implementation
839
 * @connector: affected connector
840
 * @mode: DPMS mode
1123 serge 841
 *
3192 Serge 842
 * This is the main helper function provided by the crtc helper framework for
843
 * implementing the DPMS connector attribute. It computes the new desired DPMS
844
 * state for all encoders and crtcs in the output mesh and calls the ->dpms()
845
 * callback provided by the driver appropriately.
1123 serge 846
 */
847
void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
848
{
849
	struct drm_encoder *encoder = connector->encoder;
850
	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
851
	int old_dpms;
852
 
853
	if (mode == connector->dpms)
854
		return;
855
 
856
	old_dpms = connector->dpms;
857
	connector->dpms = mode;
858
 
859
	/* from off to on, do crtc then encoder */
860
	if (mode < old_dpms) {
861
		if (crtc) {
862
			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
863
			if (crtc_funcs->dpms)
864
				(*crtc_funcs->dpms) (crtc,
865
						     drm_helper_choose_crtc_dpms(crtc));
866
		}
867
		if (encoder) {
868
			struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
869
			if (encoder_funcs->dpms)
870
				(*encoder_funcs->dpms) (encoder,
871
							drm_helper_choose_encoder_dpms(encoder));
872
		}
873
	}
874
 
875
	/* from on to off, do encoder then crtc */
876
	if (mode > old_dpms) {
877
		if (encoder) {
878
			struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
879
			if (encoder_funcs->dpms)
880
				(*encoder_funcs->dpms) (encoder,
881
							drm_helper_choose_encoder_dpms(encoder));
882
		}
883
		if (crtc) {
884
			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
885
			if (crtc_funcs->dpms)
886
				(*crtc_funcs->dpms) (crtc,
887
						     drm_helper_choose_crtc_dpms(crtc));
888
		}
889
	}
890
 
891
	return;
892
}
893
EXPORT_SYMBOL(drm_helper_connector_dpms);
894
 
895
int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
3031 serge 896
				   struct drm_mode_fb_cmd2 *mode_cmd)
1123 serge 897
{
3031 serge 898
	int i;
899
 
1123 serge 900
	fb->width = mode_cmd->width;
901
	fb->height = mode_cmd->height;
3031 serge 902
	for (i = 0; i < 4; i++) {
903
		fb->pitches[i] = mode_cmd->pitches[i];
904
		fb->offsets[i] = mode_cmd->offsets[i];
905
	}
906
	drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
907
				    &fb->bits_per_pixel);
908
	fb->pixel_format = mode_cmd->pixel_format;
1123 serge 909
 
910
	return 0;
911
}
912
EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
913
 
914
int drm_helper_resume_force_mode(struct drm_device *dev)
915
{
916
	struct drm_crtc *crtc;
1404 serge 917
	struct drm_encoder *encoder;
918
	struct drm_encoder_helper_funcs *encoder_funcs;
919
	struct drm_crtc_helper_funcs *crtc_funcs;
1123 serge 920
	int ret;
921
 
922
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
923
 
1179 serge 924
       if (!crtc->enabled)
925
           continue;
1123 serge 926
 
1179 serge 927
		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1123 serge 928
					       crtc->x, crtc->y, crtc->fb);
929
 
930
		if (ret == false)
931
			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1404 serge 932
 
933
		/* Turn off outputs that were already powered off */
934
		if (drm_helper_choose_crtc_dpms(crtc)) {
935
			list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
936
 
937
				if(encoder->crtc != crtc)
938
					continue;
939
 
940
				encoder_funcs = encoder->helper_private;
941
				if (encoder_funcs->dpms)
942
					(*encoder_funcs->dpms) (encoder,
943
								drm_helper_choose_encoder_dpms(encoder));
1963 serge 944
			}
1404 serge 945
 
946
				crtc_funcs = crtc->helper_private;
947
				if (crtc_funcs->dpms)
948
					(*crtc_funcs->dpms) (crtc,
949
							     drm_helper_choose_crtc_dpms(crtc));
950
			}
951
		}
1179 serge 952
	/* disable the unused connectors while restoring the modesetting */
953
	drm_helper_disable_unused_functions(dev);
1123 serge 954
	return 0;
955
}
956
EXPORT_SYMBOL(drm_helper_resume_force_mode);
1963 serge 957
 
958
#if 0
959
 
960
#define DRM_OUTPUT_POLL_PERIOD (10*HZ)
961
static void output_poll_execute(struct work_struct *work)
962
{
963
	struct delayed_work *delayed_work = to_delayed_work(work);
964
	struct drm_device *dev = container_of(delayed_work, struct drm_device, mode_config.output_poll_work);
965
	struct drm_connector *connector;
966
	enum drm_connector_status old_status;
967
	bool repoll = false, changed = false;
968
 
969
	if (!drm_kms_helper_poll)
970
		return;
971
 
972
	mutex_lock(&dev->mode_config.mutex);
973
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
974
 
3192 Serge 975
		/* Ignore forced connectors. */
976
		if (connector->force)
1963 serge 977
			continue;
978
 
3192 Serge 979
		/* Ignore HPD capable connectors and connectors where we don't
980
		 * want any hotplug detection at all for polling. */
981
		if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
982
			continue;
983
 
1963 serge 984
			repoll = true;
985
 
986
		old_status = connector->status;
987
		/* if we are connected and don't want to poll for disconnect
988
		   skip it */
989
		if (old_status == connector_status_connected &&
3192 Serge 990
		    !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
1963 serge 991
			continue;
992
 
993
		connector->status = connector->funcs->detect(connector, false);
994
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
995
			      connector->base.id,
996
			      drm_get_connector_name(connector),
997
			      old_status, connector->status);
998
		if (old_status != connector->status)
999
			changed = true;
1000
	}
1001
 
1002
	mutex_unlock(&dev->mode_config.mutex);
1003
 
3192 Serge 1004
	if (changed)
1005
		drm_kms_helper_hotplug_event(dev);
1963 serge 1006
 
1007
	if (repoll)
3031 serge 1008
		schedule_delayed_work(delayed_work, DRM_OUTPUT_POLL_PERIOD);
1963 serge 1009
}
1010
 
1011
void drm_kms_helper_poll_disable(struct drm_device *dev)
1012
{
1013
	if (!dev->mode_config.poll_enabled)
1014
		return;
1015
	cancel_delayed_work_sync(&dev->mode_config.output_poll_work);
1016
}
1017
EXPORT_SYMBOL(drm_kms_helper_poll_disable);
1018
 
1019
void drm_kms_helper_poll_enable(struct drm_device *dev)
1020
{
1021
	bool poll = false;
1022
	struct drm_connector *connector;
1023
 
1024
	if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1025
		return;
1026
 
1027
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3192 Serge 1028
		if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT |
1029
					 DRM_CONNECTOR_POLL_DISCONNECT))
1963 serge 1030
			poll = true;
1031
	}
1032
 
1033
	if (poll)
3031 serge 1034
		schedule_delayed_work(&dev->mode_config.output_poll_work, DRM_OUTPUT_POLL_PERIOD);
1963 serge 1035
}
1036
EXPORT_SYMBOL(drm_kms_helper_poll_enable);
1037
 
1038
void drm_kms_helper_poll_init(struct drm_device *dev)
1039
{
1040
	INIT_DELAYED_WORK(&dev->mode_config.output_poll_work, output_poll_execute);
1041
	dev->mode_config.poll_enabled = true;
1042
 
1043
	drm_kms_helper_poll_enable(dev);
1044
}
1045
EXPORT_SYMBOL(drm_kms_helper_poll_init);
1046
 
1047
void drm_kms_helper_poll_fini(struct drm_device *dev)
1048
{
1049
	drm_kms_helper_poll_disable(dev);
1050
}
1051
EXPORT_SYMBOL(drm_kms_helper_poll_fini);
1052
 
1053
void drm_helper_hpd_irq_event(struct drm_device *dev)
1054
{
3192 Serge 1055
	struct drm_connector *connector;
1056
	enum drm_connector_status old_status;
1057
	bool changed = false;
1058
 
1963 serge 1059
	if (!dev->mode_config.poll_enabled)
1060
		return;
1061
 
3192 Serge 1062
	mutex_lock(&dev->mode_config.mutex);
1063
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1064
 
1065
		/* Only handle HPD capable connectors. */
1066
		if (!(connector->polled & DRM_CONNECTOR_POLL_HPD))
1067
			continue;
1068
 
1069
		old_status = connector->status;
1070
 
1071
		connector->status = connector->funcs->detect(connector, false);
1072
		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n",
1073
			      connector->base.id,
1074
			      drm_get_connector_name(connector),
1075
			      old_status, connector->status);
1076
		if (old_status != connector->status)
1077
			changed = true;
1078
	}
1079
 
1080
	mutex_unlock(&dev->mode_config.mutex);
1081
 
1082
	if (changed)
1083
		drm_kms_helper_hotplug_event(dev);
1963 serge 1084
}
1085
EXPORT_SYMBOL(drm_helper_hpd_irq_event);
1086
 
1087
#endif