Subversion Repositories Kolibri OS

Rev

Rev 1123 | Rev 1126 | 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
 
32
#include "drmP.h"
33
#include "drm_crtc.h"
34
#include "drm_crtc_helper.h"
35
 
36
/*
37
 * Detailed mode info for 800x600@60Hz
38
 */
39
static struct drm_display_mode std_modes[] = {
40
	{ DRM_MODE("800x600", DRM_MODE_TYPE_DEFAULT, 40000, 800, 840,
41
		   968, 1056, 0, 600, 601, 605, 628, 0,
42
		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
43
};
44
 
45
static void drm_mode_validate_flag(struct drm_connector *connector,
46
				   int flags)
47
{
48
	struct drm_display_mode *mode, *t;
49
 
50
	if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
51
		return;
52
 
53
	list_for_each_entry_safe(mode, t, &connector->modes, head) {
54
		if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
55
				!(flags & DRM_MODE_FLAG_INTERLACE))
56
			mode->status = MODE_NO_INTERLACE;
57
		if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
58
				!(flags & DRM_MODE_FLAG_DBLSCAN))
59
			mode->status = MODE_NO_DBLESCAN;
60
	}
61
 
62
	return;
63
}
64
 
65
/**
66
 * drm_helper_probe_connector_modes - get complete set of display modes
67
 * @dev: DRM device
68
 * @maxX: max width for modes
69
 * @maxY: max height for modes
70
 *
71
 * LOCKING:
72
 * Caller must hold mode config lock.
73
 *
74
 * Based on @dev's mode_config layout, scan all the connectors and try to detect
75
 * modes on them.  Modes will first be added to the connector's probed_modes
76
 * list, then culled (based on validity and the @maxX, @maxY parameters) and
77
 * put into the normal modes list.
78
 *
79
 * Intended to be used either at bootup time or when major configuration
80
 * changes have occurred.
81
 *
82
 * FIXME: take into account monitor limits
83
 *
84
 * RETURNS:
85
 * Number of modes found on @connector.
86
 */
87
int drm_helper_probe_single_connector_modes(struct drm_connector *connector,
88
					    uint32_t maxX, uint32_t maxY)
89
{
90
	struct drm_device *dev = connector->dev;
91
	struct drm_display_mode *mode, *t;
92
	struct drm_connector_helper_funcs *connector_funcs =
93
		connector->helper_private;
94
	int count = 0;
95
	int mode_flags = 0;
96
 
97
	DRM_DEBUG("%s\n", drm_get_connector_name(connector));
98
	/* set all modes to the unverified state */
99
	list_for_each_entry_safe(mode, t, &connector->modes, head)
100
		mode->status = MODE_UNVERIFIED;
101
 
102
	connector->status = connector->funcs->detect(connector);
103
 
104
	if (connector->status == connector_status_disconnected) {
105
		DRM_DEBUG("%s is disconnected\n",
106
			  drm_get_connector_name(connector));
107
		/* TODO set EDID to NULL */
108
		return 0;
109
	}
110
 
111
	count = (*connector_funcs->get_modes)(connector);
112
	if (!count)
113
		return 0;
114
 
115
	drm_mode_connector_list_update(connector);
116
 
117
	if (maxX && maxY)
118
		drm_mode_validate_size(dev, &connector->modes, maxX,
119
				       maxY, 0);
120
 
121
	if (connector->interlace_allowed)
122
		mode_flags |= DRM_MODE_FLAG_INTERLACE;
123
	if (connector->doublescan_allowed)
124
		mode_flags |= DRM_MODE_FLAG_DBLSCAN;
125
	drm_mode_validate_flag(connector, mode_flags);
126
 
127
	list_for_each_entry_safe(mode, t, &connector->modes, head) {
128
		if (mode->status == MODE_OK)
129
			mode->status = connector_funcs->mode_valid(connector,
130
								   mode);
131
	}
132
 
133
 
134
	drm_mode_prune_invalid(dev, &connector->modes, true);
135
 
136
	if (list_empty(&connector->modes))
137
		return 0;
138
 
139
	drm_mode_sort(&connector->modes);
140
 
141
	DRM_DEBUG("Probed modes for %s\n", drm_get_connector_name(connector));
142
	list_for_each_entry_safe(mode, t, &connector->modes, head) {
143
		mode->vrefresh = drm_mode_vrefresh(mode);
144
 
145
		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
146
		drm_mode_debug_printmodeline(mode);
147
	}
148
 
149
	return count;
150
}
151
EXPORT_SYMBOL(drm_helper_probe_single_connector_modes);
152
 
153
int drm_helper_probe_connector_modes(struct drm_device *dev, uint32_t maxX,
154
				      uint32_t maxY)
155
{
156
	struct drm_connector *connector;
157
	int count = 0;
158
 
159
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
160
		count += drm_helper_probe_single_connector_modes(connector,
161
								 maxX, maxY);
162
	}
163
 
164
	return count;
165
}
166
EXPORT_SYMBOL(drm_helper_probe_connector_modes);
167
 
168
static void drm_helper_add_std_modes(struct drm_device *dev,
169
				     struct drm_connector *connector)
170
{
171
	struct drm_display_mode *mode, *t;
172
	int i;
173
 
174
	for (i = 0; i < ARRAY_SIZE(std_modes); i++) {
175
		struct drm_display_mode *stdmode;
176
 
177
		/*
178
		 * When no valid EDID modes are available we end up
179
		 * here and bailed in the past, now we add some standard
180
		 * modes and move on.
181
		 */
182
		stdmode = drm_mode_duplicate(dev, &std_modes[i]);
183
		drm_mode_probed_add(connector, stdmode);
184
		drm_mode_list_concat(&connector->probed_modes,
185
				     &connector->modes);
186
 
187
		DRM_DEBUG("Adding mode %s to %s\n", stdmode->name,
188
			  drm_get_connector_name(connector));
189
	}
190
	drm_mode_sort(&connector->modes);
191
 
192
	DRM_DEBUG("Added std modes on %s\n", drm_get_connector_name(connector));
193
	list_for_each_entry_safe(mode, t, &connector->modes, head) {
194
		mode->vrefresh = drm_mode_vrefresh(mode);
195
 
196
		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
197
		drm_mode_debug_printmodeline(mode);
198
	}
199
}
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
 
248
/**
249
 * drm_disable_unused_functions - disable unused objects
250
 * @dev: DRM device
251
 *
252
 * LOCKING:
253
 * Caller must hold mode config lock.
254
 *
255
 * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
256
 * by calling its dpms function, which should power it off.
257
 */
258
void drm_helper_disable_unused_functions(struct drm_device *dev)
259
{
260
	struct drm_encoder *encoder;
261
	struct drm_encoder_helper_funcs *encoder_funcs;
262
	struct drm_crtc *crtc;
263
 
264
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
265
		encoder_funcs = encoder->helper_private;
266
		if (!drm_helper_encoder_in_use(encoder))
267
			(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
268
	}
269
 
270
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
271
		struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
272
		crtc->enabled = drm_helper_crtc_in_use(crtc);
273
		if (!crtc->enabled) {
274
			crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
275
			crtc->fb = NULL;
276
		}
277
	}
278
}
279
EXPORT_SYMBOL(drm_helper_disable_unused_functions);
280
 
281
static struct drm_display_mode *drm_has_preferred_mode(struct drm_connector *connector, int width, int height)
282
{
283
	struct drm_display_mode *mode;
284
 
285
	list_for_each_entry(mode, &connector->modes, head) {
286
		if (drm_mode_width(mode) > width ||
287
		    drm_mode_height(mode) > height)
288
			continue;
289
		if (mode->type & DRM_MODE_TYPE_PREFERRED)
290
			return mode;
291
	}
292
	return NULL;
293
}
294
 
295
static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
296
{
297
	bool enable;
298
 
299
	if (strict) {
300
		enable = connector->status == connector_status_connected;
301
	} else {
302
		enable = connector->status != connector_status_disconnected;
303
	}
304
	return enable;
305
}
306
 
307
static void drm_enable_connectors(struct drm_device *dev, bool *enabled)
308
{
309
	bool any_enabled = false;
310
	struct drm_connector *connector;
311
	int i = 0;
312
 
313
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
314
		enabled[i] = drm_connector_enabled(connector, true);
315
		DRM_DEBUG("connector %d enabled? %s\n", connector->base.id,
316
			  enabled[i] ? "yes" : "no");
317
		any_enabled |= enabled[i];
318
		i++;
319
	}
320
 
321
	if (any_enabled)
322
		return;
323
 
324
	i = 0;
325
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
326
		enabled[i] = drm_connector_enabled(connector, false);
327
		i++;
328
	}
329
}
330
 
331
static bool drm_target_preferred(struct drm_device *dev,
332
				 struct drm_display_mode **modes,
333
				 bool *enabled, int width, int height)
334
{
335
	struct drm_connector *connector;
336
	int i = 0;
337
 
338
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
339
 
340
		if (enabled[i] == false) {
341
			i++;
342
			continue;
343
		}
344
 
345
		DRM_DEBUG("looking for preferred mode on connector %d\n",
346
			  connector->base.id);
347
 
348
		modes[i] = drm_has_preferred_mode(connector, width, height);
349
		/* No preferred modes, pick one off the list */
350
		if (!modes[i] && !list_empty(&connector->modes)) {
351
			list_for_each_entry(modes[i], &connector->modes, head)
352
				break;
353
		}
354
		DRM_DEBUG("found mode %s\n", modes[i] ? modes[i]->name :
355
			  "none");
356
		i++;
357
	}
358
	return true;
359
}
360
 
361
static int drm_pick_crtcs(struct drm_device *dev,
362
			  struct drm_crtc **best_crtcs,
363
			  struct drm_display_mode **modes,
364
			  int n, int width, int height)
365
{
366
	int c, o;
367
	struct drm_connector *connector;
368
	struct drm_connector_helper_funcs *connector_funcs;
369
	struct drm_encoder *encoder;
370
	struct drm_crtc *best_crtc;
371
	int my_score, best_score, score;
372
	struct drm_crtc **crtcs, *crtc;
373
 
374
	if (n == dev->mode_config.num_connector)
375
		return 0;
376
	c = 0;
377
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
378
		if (c == n)
379
			break;
380
		c++;
381
	}
382
 
383
	best_crtcs[n] = NULL;
384
	best_crtc = NULL;
385
	best_score = drm_pick_crtcs(dev, best_crtcs, modes, n+1, width, height);
386
	if (modes[n] == NULL)
387
		return best_score;
388
 
389
	crtcs = kmalloc(dev->mode_config.num_connector *
390
			sizeof(struct drm_crtc *), GFP_KERNEL);
391
	if (!crtcs)
392
		return best_score;
393
 
394
	my_score = 1;
395
	if (connector->status == connector_status_connected)
396
		my_score++;
397
	if (drm_has_preferred_mode(connector, width, height))
398
		my_score++;
399
 
400
	connector_funcs = connector->helper_private;
401
	encoder = connector_funcs->best_encoder(connector);
402
	if (!encoder)
403
		goto out;
404
 
405
	connector->encoder = encoder;
406
 
407
	/* select a crtc for this connector and then attempt to configure
408
	   remaining connectors */
409
	c = 0;
410
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
411
 
412
		if ((connector->encoder->possible_crtcs & (1 << c)) == 0) {
413
			c++;
414
			continue;
415
		}
416
 
417
		for (o = 0; o < n; o++)
418
			if (best_crtcs[o] == crtc)
419
				break;
420
 
421
		if (o < n) {
422
			/* ignore cloning for now */
423
			c++;
424
			continue;
425
		}
426
 
427
		crtcs[n] = crtc;
428
		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_crtc *));
429
		score = my_score + drm_pick_crtcs(dev, crtcs, modes, n + 1,
430
						  width, height);
431
		if (score > best_score) {
432
			best_crtc = crtc;
433
			best_score = score;
434
			memcpy(best_crtcs, crtcs,
435
			       dev->mode_config.num_connector *
436
			       sizeof(struct drm_crtc *));
437
		}
438
		c++;
439
	}
440
out:
441
	kfree(crtcs);
442
	return best_score;
443
}
444
 
445
static void drm_setup_crtcs(struct drm_device *dev)
446
{
447
	struct drm_crtc **crtcs;
448
	struct drm_display_mode **modes;
449
	struct drm_encoder *encoder;
450
	struct drm_connector *connector;
451
	bool *enabled;
452
	int width, height;
453
	int i, ret;
454
 
455
	DRM_DEBUG("\n");
456
 
457
	width = dev->mode_config.max_width;
458
	height = dev->mode_config.max_height;
459
 
460
	/* clean out all the encoder/crtc combos */
461
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
462
		encoder->crtc = NULL;
463
	}
464
 
465
	crtcs = kcalloc(dev->mode_config.num_connector,
466
			sizeof(struct drm_crtc *), GFP_KERNEL);
467
	modes = kcalloc(dev->mode_config.num_connector,
468
			sizeof(struct drm_display_mode *), GFP_KERNEL);
469
	enabled = kcalloc(dev->mode_config.num_connector,
470
			  sizeof(bool), GFP_KERNEL);
471
 
472
	drm_enable_connectors(dev, enabled);
473
 
474
	ret = drm_target_preferred(dev, modes, enabled, width, height);
475
	if (!ret)
476
		DRM_ERROR("Unable to find initial modes\n");
477
 
478
	DRM_DEBUG("picking CRTCs for %dx%d config\n", width, height);
479
 
480
	drm_pick_crtcs(dev, crtcs, modes, 0, width, height);
481
 
1125 serge 482
    dbgprintf("done\n");
483
 
1123 serge 484
	i = 0;
485
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
486
		struct drm_display_mode *mode = modes[i];
487
		struct drm_crtc *crtc = crtcs[i];
488
 
489
		if (connector->encoder == NULL) {
490
			i++;
491
			continue;
492
		}
493
 
494
		if (mode && crtc) {
495
			DRM_DEBUG("desired mode %s set on crtc %d\n",
496
				  mode->name, crtc->base.id);
497
			crtc->desired_mode = mode;
498
			connector->encoder->crtc = crtc;
499
		} else
500
			connector->encoder->crtc = NULL;
501
		i++;
502
	}
503
 
504
	kfree(crtcs);
505
	kfree(modes);
506
	kfree(enabled);
1125 serge 507
 
508
    LEAVE();
1123 serge 509
}
510
 
511
/**
512
 * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
513
 * @encoder: encoder to test
514
 * @crtc: crtc to test
515
 *
516
 * Return false if @encoder can't be driven by @crtc, true otherwise.
517
 */
518
static bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
519
				struct drm_crtc *crtc)
520
{
521
	struct drm_device *dev;
522
	struct drm_crtc *tmp;
523
	int crtc_mask = 1;
524
 
1125 serge 525
//   WARN(!crtc, "checking null crtc?");
1123 serge 526
 
527
	dev = crtc->dev;
528
 
529
	list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
530
		if (tmp == crtc)
531
			break;
532
		crtc_mask <<= 1;
533
	}
534
 
535
	if (encoder->possible_crtcs & crtc_mask)
536
		return true;
537
	return false;
538
}
539
 
540
/*
541
 * Check the CRTC we're going to map each output to vs. its current
542
 * CRTC.  If they don't match, we have to disable the output and the CRTC
543
 * since the driver will have to re-route things.
544
 */
545
static void
546
drm_crtc_prepare_encoders(struct drm_device *dev)
547
{
548
	struct drm_encoder_helper_funcs *encoder_funcs;
549
	struct drm_encoder *encoder;
550
 
551
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
552
		encoder_funcs = encoder->helper_private;
553
		/* Disable unused encoders */
554
		if (encoder->crtc == NULL)
555
			(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
556
		/* Disable encoders whose CRTC is about to change */
557
		if (encoder_funcs->get_crtc &&
558
		    encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
559
			(*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
560
	}
561
}
562
 
563
/**
564
 * drm_crtc_set_mode - set a mode
565
 * @crtc: CRTC to program
566
 * @mode: mode to use
567
 * @x: width of mode
568
 * @y: height of mode
569
 *
570
 * LOCKING:
571
 * Caller must hold mode config lock.
572
 *
573
 * Try to set @mode on @crtc.  Give @crtc and its associated connectors a chance
574
 * to fixup or reject the mode prior to trying to set it.
575
 *
576
 * RETURNS:
577
 * True if the mode was set successfully, or false otherwise.
578
 */
579
bool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
580
			      struct drm_display_mode *mode,
581
			      int x, int y,
582
			      struct drm_framebuffer *old_fb)
583
{
584
	struct drm_device *dev = crtc->dev;
585
	struct drm_display_mode *adjusted_mode, saved_mode;
586
	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
587
	struct drm_encoder_helper_funcs *encoder_funcs;
588
	int saved_x, saved_y;
589
	struct drm_encoder *encoder;
590
	bool ret = true;
591
 
592
	adjusted_mode = drm_mode_duplicate(dev, mode);
593
 
594
	crtc->enabled = drm_helper_crtc_in_use(crtc);
595
 
596
	if (!crtc->enabled)
597
		return true;
598
 
599
	saved_mode = crtc->mode;
600
	saved_x = crtc->x;
601
	saved_y = crtc->y;
602
 
603
	/* Update crtc values up front so the driver can rely on them for mode
604
	 * setting.
605
	 */
606
	crtc->mode = *mode;
607
	crtc->x = x;
608
	crtc->y = y;
609
 
610
	/* Pass our mode to the connectors and the CRTC to give them a chance to
611
	 * adjust it according to limitations or connector properties, and also
612
	 * a chance to reject the mode entirely.
613
	 */
614
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
615
 
616
		if (encoder->crtc != crtc)
617
			continue;
618
		encoder_funcs = encoder->helper_private;
619
		if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
620
						      adjusted_mode))) {
621
			goto done;
622
		}
623
	}
624
 
625
	if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
626
		goto done;
627
	}
628
 
629
	/* Prepare the encoders and CRTCs before setting the mode. */
630
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
631
 
632
		if (encoder->crtc != crtc)
633
			continue;
634
		encoder_funcs = encoder->helper_private;
635
		/* Disable the encoders as the first thing we do. */
636
		encoder_funcs->prepare(encoder);
637
	}
638
 
639
	drm_crtc_prepare_encoders(dev);
640
 
641
	crtc_funcs->prepare(crtc);
642
 
643
	/* Set up the DPLL and any encoders state that needs to adjust or depend
644
	 * on the DPLL.
645
	 */
646
	ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
647
	if (!ret)
648
	    goto done;
649
 
650
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
651
 
652
		if (encoder->crtc != crtc)
653
			continue;
654
 
655
		DRM_INFO("%s: set mode %s %x\n", drm_get_encoder_name(encoder),
656
			 mode->name, mode->base.id);
657
		encoder_funcs = encoder->helper_private;
658
		encoder_funcs->mode_set(encoder, mode, adjusted_mode);
659
	}
660
 
661
	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
662
	crtc_funcs->commit(crtc);
663
 
664
	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
665
 
666
		if (encoder->crtc != crtc)
667
			continue;
668
 
669
		encoder_funcs = encoder->helper_private;
670
		encoder_funcs->commit(encoder);
671
 
672
	}
673
 
674
	/* XXX free adjustedmode */
675
	drm_mode_destroy(dev, adjusted_mode);
676
	/* FIXME: add subpixel order */
677
done:
678
	if (!ret) {
679
		crtc->mode = saved_mode;
680
		crtc->x = saved_x;
681
		crtc->y = saved_y;
682
	}
683
 
684
	return ret;
685
}
686
EXPORT_SYMBOL(drm_crtc_helper_set_mode);
687
 
688
 
689
/**
690
 * drm_crtc_helper_set_config - set a new config from userspace
691
 * @crtc: CRTC to setup
692
 * @crtc_info: user provided configuration
693
 * @new_mode: new mode to set
694
 * @connector_set: set of connectors for the new config
695
 * @fb: new framebuffer
696
 *
697
 * LOCKING:
698
 * Caller must hold mode config lock.
699
 *
700
 * Setup a new configuration, provided by the user in @crtc_info, and enable
701
 * it.
702
 *
703
 * RETURNS:
704
 * Zero. (FIXME)
705
 */
706
int drm_crtc_helper_set_config(struct drm_mode_set *set)
707
{
708
	struct drm_device *dev;
709
	struct drm_crtc **save_crtcs, *new_crtc;
710
	struct drm_encoder **save_encoders, *new_encoder;
711
	struct drm_framebuffer *old_fb = NULL;
712
	bool save_enabled;
713
	bool mode_changed = false;
714
	bool fb_changed = false;
715
	struct drm_connector *connector;
716
	int count = 0, ro, fail = 0;
717
	struct drm_crtc_helper_funcs *crtc_funcs;
718
	int ret = 0;
719
 
720
	DRM_DEBUG("\n");
721
 
722
	if (!set)
723
		return -EINVAL;
724
 
725
	if (!set->crtc)
726
		return -EINVAL;
727
 
728
	if (!set->crtc->helper_private)
729
		return -EINVAL;
730
 
731
	crtc_funcs = set->crtc->helper_private;
732
 
733
	DRM_DEBUG("crtc: %p %d fb: %p connectors: %p num_connectors: %d (x, y) (%i, %i)\n",
734
		  set->crtc, set->crtc->base.id, set->fb, set->connectors,
735
		  (int)set->num_connectors, set->x, set->y);
736
 
737
	dev = set->crtc->dev;
738
 
739
	/* save previous config */
740
	save_enabled = set->crtc->enabled;
741
 
742
	/*
743
	 * We do mode_config.num_connectors here since we'll look at the
744
	 * CRTC and encoder associated with each connector later.
745
	 */
746
	save_crtcs = kzalloc(dev->mode_config.num_connector *
747
			     sizeof(struct drm_crtc *), GFP_KERNEL);
748
	if (!save_crtcs)
749
		return -ENOMEM;
750
 
751
	save_encoders = kzalloc(dev->mode_config.num_connector *
752
				sizeof(struct drm_encoders *), GFP_KERNEL);
753
	if (!save_encoders) {
754
		kfree(save_crtcs);
755
		return -ENOMEM;
756
	}
757
 
758
	/* We should be able to check here if the fb has the same properties
759
	 * and then just flip_or_move it */
760
	if (set->crtc->fb != set->fb) {
761
		/* If we have no fb then treat it as a full mode set */
762
		if (set->crtc->fb == NULL) {
763
			DRM_DEBUG("crtc has no fb, full mode set\n");
764
			mode_changed = true;
765
		} else if ((set->fb->bits_per_pixel !=
766
			 set->crtc->fb->bits_per_pixel) ||
767
			 set->fb->depth != set->crtc->fb->depth)
768
			fb_changed = true;
769
		else
770
			fb_changed = true;
771
	}
772
 
773
	if (set->x != set->crtc->x || set->y != set->crtc->y)
774
		fb_changed = true;
775
 
776
	if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
777
		DRM_DEBUG("modes are different, full mode set\n");
778
		drm_mode_debug_printmodeline(&set->crtc->mode);
779
		drm_mode_debug_printmodeline(set->mode);
780
		mode_changed = true;
781
	}
782
 
783
	/* a) traverse passed in connector list and get encoders for them */
784
	count = 0;
785
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
786
		struct drm_connector_helper_funcs *connector_funcs =
787
			connector->helper_private;
788
		save_encoders[count++] = connector->encoder;
789
		new_encoder = connector->encoder;
790
		for (ro = 0; ro < set->num_connectors; ro++) {
791
			if (set->connectors[ro] == connector) {
792
				new_encoder = connector_funcs->best_encoder(connector);
793
				/* if we can't get an encoder for a connector
794
				   we are setting now - then fail */
795
				if (new_encoder == NULL)
796
					/* don't break so fail path works correct */
797
					fail = 1;
798
				break;
799
			}
800
		}
801
 
802
		if (new_encoder != connector->encoder) {
803
			DRM_DEBUG("encoder changed, full mode switch\n");
804
			mode_changed = true;
805
			connector->encoder = new_encoder;
806
		}
807
	}
808
 
809
	if (fail) {
810
		ret = -EINVAL;
811
		goto fail_no_encoder;
812
	}
813
 
814
	count = 0;
815
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
816
		if (!connector->encoder)
817
			continue;
818
 
819
		save_crtcs[count++] = connector->encoder->crtc;
820
 
821
		if (connector->encoder->crtc == set->crtc)
822
			new_crtc = NULL;
823
		else
824
			new_crtc = connector->encoder->crtc;
825
 
826
		for (ro = 0; ro < set->num_connectors; ro++) {
827
			if (set->connectors[ro] == connector)
828
				new_crtc = set->crtc;
829
		}
830
 
831
		/* Make sure the new CRTC will work with the encoder */
832
		if (new_crtc &&
833
		    !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
834
			ret = -EINVAL;
835
			goto fail_set_mode;
836
		}
837
		if (new_crtc != connector->encoder->crtc) {
838
			DRM_DEBUG("crtc changed, full mode switch\n");
839
			mode_changed = true;
840
			connector->encoder->crtc = new_crtc;
841
		}
842
		DRM_DEBUG("setting connector %d crtc to %p\n",
843
			  connector->base.id, new_crtc);
844
	}
845
 
846
	/* mode_set_base is not a required function */
847
	if (fb_changed && !crtc_funcs->mode_set_base)
848
		mode_changed = true;
849
 
850
	if (mode_changed) {
851
		old_fb = set->crtc->fb;
852
		set->crtc->fb = set->fb;
853
		set->crtc->enabled = (set->mode != NULL);
854
		if (set->mode != NULL) {
855
			DRM_DEBUG("attempting to set mode from userspace\n");
856
			drm_mode_debug_printmodeline(set->mode);
857
			if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
858
						      set->x, set->y,
859
						      old_fb)) {
860
				DRM_ERROR("failed to set mode on crtc %p\n",
861
					  set->crtc);
862
				ret = -EINVAL;
863
				goto fail_set_mode;
864
			}
865
			/* TODO are these needed? */
866
			set->crtc->desired_x = set->x;
867
			set->crtc->desired_y = set->y;
868
			set->crtc->desired_mode = set->mode;
869
		}
870
		drm_helper_disable_unused_functions(dev);
871
	} else if (fb_changed) {
872
		old_fb = set->crtc->fb;
873
		if (set->crtc->fb != set->fb)
874
			set->crtc->fb = set->fb;
875
		ret = crtc_funcs->mode_set_base(set->crtc,
876
						set->x, set->y, old_fb);
877
		if (ret != 0)
878
		    goto fail_set_mode;
879
	}
880
 
881
	kfree(save_encoders);
882
	kfree(save_crtcs);
883
	return 0;
884
 
885
fail_set_mode:
886
	set->crtc->enabled = save_enabled;
887
	set->crtc->fb = old_fb;
888
	count = 0;
889
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
890
		if (!connector->encoder)
891
			continue;
892
 
893
		connector->encoder->crtc = save_crtcs[count++];
894
	}
895
fail_no_encoder:
896
	kfree(save_crtcs);
897
	count = 0;
898
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
899
		connector->encoder = save_encoders[count++];
900
	}
901
	kfree(save_encoders);
902
	return ret;
903
}
904
EXPORT_SYMBOL(drm_crtc_helper_set_config);
905
 
906
bool drm_helper_plugged_event(struct drm_device *dev)
907
{
908
	DRM_DEBUG("\n");
909
 
910
	drm_helper_probe_connector_modes(dev, dev->mode_config.max_width,
911
					 dev->mode_config.max_height);
912
 
913
	drm_setup_crtcs(dev);
914
 
915
	/* alert the driver fb layer */
1125 serge 916
 //  dev->mode_config.funcs->fb_changed(dev);
1123 serge 917
 
918
	/* FIXME: send hotplug event */
919
	return true;
920
}
921
/**
922
 * drm_initial_config - setup a sane initial connector configuration
923
 * @dev: DRM device
924
 *
925
 * LOCKING:
926
 * Called at init time, must take mode config lock.
927
 *
928
 * Scan the CRTCs and connectors and try to put together an initial setup.
929
 * At the moment, this is a cloned configuration across all heads with
930
 * a new framebuffer object as the backing store.
931
 *
932
 * RETURNS:
933
 * Zero if everything went ok, nonzero otherwise.
934
 */
935
bool drm_helper_initial_config(struct drm_device *dev)
936
{
937
	struct drm_connector *connector;
938
	int count = 0;
939
 
1125 serge 940
    ENTRY();
941
 
1123 serge 942
	count = drm_helper_probe_connector_modes(dev,
943
						 dev->mode_config.max_width,
944
						 dev->mode_config.max_height);
945
 
946
	/*
947
	 * None of the available connectors had any modes, so add some
948
	 * and try to light them up anyway
949
	 */
950
	if (!count) {
951
		DRM_ERROR("connectors have no modes, using standard modes\n");
952
		list_for_each_entry(connector,
953
				    &dev->mode_config.connector_list,
954
				    head)
955
			drm_helper_add_std_modes(dev, connector);
956
	}
957
 
958
	drm_setup_crtcs(dev);
959
 
960
	/* alert the driver fb layer */
1125 serge 961
 //  dev->mode_config.funcs->fb_changed(dev);
1123 serge 962
 
1125 serge 963
    LEAVE();
964
 
1123 serge 965
	return 0;
966
}
967
EXPORT_SYMBOL(drm_helper_initial_config);
968
 
969
static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
970
{
971
	int dpms = DRM_MODE_DPMS_OFF;
972
	struct drm_connector *connector;
973
	struct drm_device *dev = encoder->dev;
974
 
975
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
976
		if (connector->encoder == encoder)
977
			if (connector->dpms < dpms)
978
				dpms = connector->dpms;
979
	return dpms;
980
}
981
 
982
static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
983
{
984
	int dpms = DRM_MODE_DPMS_OFF;
985
	struct drm_connector *connector;
986
	struct drm_device *dev = crtc->dev;
987
 
988
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
989
		if (connector->encoder && connector->encoder->crtc == crtc)
990
			if (connector->dpms < dpms)
991
				dpms = connector->dpms;
992
	return dpms;
993
}
994
 
995
/**
996
 * drm_helper_connector_dpms
997
 * @connector affected connector
998
 * @mode DPMS mode
999
 *
1000
 * Calls the low-level connector DPMS function, then
1001
 * calls appropriate encoder and crtc DPMS functions as well
1002
 */
1003
void drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1004
{
1005
	struct drm_encoder *encoder = connector->encoder;
1006
	struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1007
	int old_dpms;
1008
 
1009
	if (mode == connector->dpms)
1010
		return;
1011
 
1012
	old_dpms = connector->dpms;
1013
	connector->dpms = mode;
1014
 
1015
	/* from off to on, do crtc then encoder */
1016
	if (mode < old_dpms) {
1017
		if (crtc) {
1018
			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1019
			if (crtc_funcs->dpms)
1020
				(*crtc_funcs->dpms) (crtc,
1021
						     drm_helper_choose_crtc_dpms(crtc));
1022
		}
1023
		if (encoder) {
1024
			struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1025
			if (encoder_funcs->dpms)
1026
				(*encoder_funcs->dpms) (encoder,
1027
							drm_helper_choose_encoder_dpms(encoder));
1028
		}
1029
	}
1030
 
1031
	/* from on to off, do encoder then crtc */
1032
	if (mode > old_dpms) {
1033
		if (encoder) {
1034
			struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1035
			if (encoder_funcs->dpms)
1036
				(*encoder_funcs->dpms) (encoder,
1037
							drm_helper_choose_encoder_dpms(encoder));
1038
		}
1039
		if (crtc) {
1040
			struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1041
			if (crtc_funcs->dpms)
1042
				(*crtc_funcs->dpms) (crtc,
1043
						     drm_helper_choose_crtc_dpms(crtc));
1044
		}
1045
	}
1046
 
1047
	return;
1048
}
1049
EXPORT_SYMBOL(drm_helper_connector_dpms);
1050
 
1051
/**
1052
 * drm_hotplug_stage_two
1053
 * @dev DRM device
1054
 * @connector hotpluged connector
1055
 *
1056
 * LOCKING.
1057
 * Caller must hold mode config lock, function might grab struct lock.
1058
 *
1059
 * Stage two of a hotplug.
1060
 *
1061
 * RETURNS:
1062
 * Zero on success, errno on failure.
1063
 */
1064
int drm_helper_hotplug_stage_two(struct drm_device *dev)
1065
{
1066
	drm_helper_plugged_event(dev);
1067
 
1068
	return 0;
1069
}
1070
EXPORT_SYMBOL(drm_helper_hotplug_stage_two);
1071
 
1072
int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1073
				   struct drm_mode_fb_cmd *mode_cmd)
1074
{
1075
	fb->width = mode_cmd->width;
1076
	fb->height = mode_cmd->height;
1077
	fb->pitch = mode_cmd->pitch;
1078
	fb->bits_per_pixel = mode_cmd->bpp;
1079
	fb->depth = mode_cmd->depth;
1080
 
1081
	return 0;
1082
}
1083
EXPORT_SYMBOL(drm_helper_mode_fill_fb_struct);
1084
 
1085
int drm_helper_resume_force_mode(struct drm_device *dev)
1086
{
1087
	struct drm_crtc *crtc;
1088
	int ret;
1089
 
1090
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1091
 
1092
		if (!crtc->enabled)
1093
			continue;
1094
 
1095
		ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1096
					       crtc->x, crtc->y, crtc->fb);
1097
 
1098
		if (ret == false)
1099
			DRM_ERROR("failed to set mode on crtc %p\n", crtc);
1100
	}
1101
	return 0;
1102
}
1103
EXPORT_SYMBOL(drm_helper_resume_force_mode);