nv50_sor.c 12.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (C) 2008 Maarten Maathuis.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

27 28
#include <drm/drmP.h>
#include <drm/drm_crtc_helper.h>
29 30 31

#define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
#include "nouveau_reg.h"
32
#include "nouveau_drm.h"
33 34 35 36 37 38
#include "nouveau_dma.h"
#include "nouveau_encoder.h"
#include "nouveau_connector.h"
#include "nouveau_crtc.h"
#include "nv50_display.h"

39 40
#include <core/class.h>

41 42
#include <subdev/timer.h>

43
static void
44
nv50_sor_dp_train_set(struct drm_device *dev, struct dcb_output *dcb, u8 pattern)
45
{
46 47 48 49
	struct nv50_display *disp = nv50_display(dev);
	const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
	const u32 moff = (link << 2) | or;
	nv_call(disp->core, NV94_DISP_SOR_DP_TRAIN + moff, pattern);
50 51 52
}

static void
53
nv50_sor_dp_train_adj(struct drm_device *dev, struct dcb_output *dcb,
54 55
		      u8 lane, u8 swing, u8 preem)
{
56 57 58 59 60
	struct nv50_display *disp = nv50_display(dev);
	const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
	const u32 moff = (link << 2) | or;
	const u32 data = (swing << 8) | preem;
	nv_call(disp->core, NV94_DISP_SOR_DP_DRVCTL(lane) + moff, data);
61 62 63
}

static void
64
nv50_sor_dp_link_set(struct drm_device *dev, struct dcb_output *dcb, int crtc,
65 66
		     int link_nr, u32 link_bw, bool enhframe)
{
67 68 69 70
	struct nv50_display *disp = nv50_display(dev);
	const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
	const u32 moff = (crtc << 3) | (link << 2) | or;
	u32 data = ((link_bw / 27000) << 8) | link_nr;
71
	if (enhframe)
72 73
		data |= NV94_DISP_SOR_DP_LNKCTL_FRAME_ENH;
	nv_call(disp->core, NV94_DISP_SOR_DP_LNKCTL + moff, data);
74 75 76 77 78
}

static void
nv50_sor_dp_link_get(struct drm_device *dev, u32 or, u32 link, u32 *nr, u32 *bw)
{
79 80 81
	struct nouveau_device *device = nouveau_dev(dev);
	u32 dpctrl = nv_rd32(device, NV50_SOR_DP_CTRL(or, link)) & 0x000f0000;
	u32 clksor = nv_rd32(device, 0x614300 + (or * 0x800));
82 83 84 85 86 87 88 89 90 91 92 93 94
	if (clksor & 0x000c0000)
		*bw = 270000;
	else
		*bw = 162000;

	if      (dpctrl > 0x00030000) *nr = 4;
	else if (dpctrl > 0x00010000) *nr = 2;
	else			      *nr = 1;
}

void
nv50_sor_dp_calc_tu(struct drm_device *dev, int or, int link, u32 clk, u32 bpp)
{
95 96
	struct nouveau_device *device = nouveau_dev(dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	const u32 symbol = 100000;
	int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
	int TU, VTUi, VTUf, VTUa;
	u64 link_data_rate, link_ratio, unk;
	u32 best_diff = 64 * symbol;
	u32 link_nr, link_bw, r;

	/* calculate packed data rate for each lane */
	nv50_sor_dp_link_get(dev, or, link, &link_nr, &link_bw);
	link_data_rate = (clk * bpp / 8) / link_nr;

	/* calculate ratio of packed data rate to link symbol rate */
	link_ratio = link_data_rate * symbol;
	r = do_div(link_ratio, link_bw);

	for (TU = 64; TU >= 32; TU--) {
		/* calculate average number of valid symbols in each TU */
		u32 tu_valid = link_ratio * TU;
		u32 calc, diff;

		/* find a hw representation for the fraction.. */
		VTUi = tu_valid / symbol;
		calc = VTUi * symbol;
		diff = tu_valid - calc;
		if (diff) {
			if (diff >= (symbol / 2)) {
				VTUf = symbol / (symbol - diff);
				if (symbol - (VTUf * diff))
					VTUf++;

				if (VTUf <= 15) {
					VTUa  = 1;
					calc += symbol - (symbol / VTUf);
				} else {
					VTUa  = 0;
					VTUf  = 1;
					calc += symbol;
				}
			} else {
				VTUa  = 0;
				VTUf  = min((int)(symbol / diff), 15);
				calc += symbol / VTUf;
			}

			diff = calc - tu_valid;
		} else {
			/* no remainder, but the hw doesn't like the fractional
			 * part to be zero.  decrement the integer part and
			 * have the fraction add a whole symbol back
			 */
			VTUa = 0;
			VTUf = 1;
			VTUi--;
		}

		if (diff < best_diff) {
			best_diff = diff;
			bestTU = TU;
			bestVTUa = VTUa;
			bestVTUf = VTUf;
			bestVTUi = VTUi;
			if (diff == 0)
				break;
		}
	}

	if (!bestTU) {
164
		NV_ERROR(drm, "DP: unable to find suitable config\n");
165 166 167 168 169 170 171 172 173 174
		return;
	}

	/* XXX close to vbios numbers, but not right */
	unk  = (symbol - link_ratio) * bestTU;
	unk *= link_ratio;
	r = do_div(unk, symbol);
	r = do_div(unk, symbol);
	unk += 6;

175 176
	nv_mask(device, NV50_SOR_DP_CTRL(or, link), 0x000001fc, bestTU << 2);
	nv_mask(device, NV50_SOR_DP_SCFG(or, link), 0x010f7f3f, bestVTUa << 24 |
177 178 179 180
							     bestVTUf << 16 |
							     bestVTUi << 8 |
							     unk);
}
181
static void
182
nv50_sor_disconnect(struct drm_encoder *encoder)
183
{
184
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
185
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
186
	struct drm_device *dev = encoder->dev;
187
	struct nouveau_channel *evo = nv50_display(dev)->master;
188 189
	int ret;

190 191
	if (!nv_encoder->crtc)
		return;
192
	nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
193

194
	NV_DEBUG(drm, "Disconnecting SOR %d\n", nv_encoder->or);
195

196
	ret = RING_SPACE(evo, 4);
197
	if (ret) {
198
		NV_ERROR(drm, "no space while disconnecting SOR\n");
199 200
		return;
	}
201
	BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
202
	OUT_RING  (evo, 0);
203
	BEGIN_NV04(evo, 0, NV50_EVO_UPDATE, 1);
204
	OUT_RING  (evo, 0);
205

206 207
	nouveau_hdmi_mode_set(encoder, NULL);

208 209
	nv_encoder->crtc = NULL;
	nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
210 211 212 213 214
}

static void
nv50_sor_dpms(struct drm_encoder *encoder, int mode)
{
215
	struct nv50_display *priv = nv50_display(encoder->dev);
216
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
217 218
	struct drm_device *dev = encoder->dev;
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
219
	struct drm_encoder *enc;
220 221
	int or = nv_encoder->or;

222
	NV_DEBUG(drm, "or %d type %d mode %d\n", or, nv_encoder->dcb->type, mode);
223

224 225 226 227 228
	nv_encoder->last_dpms = mode;
	list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
		struct nouveau_encoder *nvenc = nouveau_encoder(enc);

		if (nvenc == nv_encoder ||
229 230 231
		    (nvenc->dcb->type != DCB_OUTPUT_TMDS &&
		     nvenc->dcb->type != DCB_OUTPUT_LVDS &&
		     nvenc->dcb->type != DCB_OUTPUT_DP) ||
232 233 234 235 236 237 238
		    nvenc->dcb->or != nv_encoder->dcb->or)
			continue;

		if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
			return;
	}

239
	nv_call(priv->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
240

241
	if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
242 243 244 245 246
		struct dp_train_func func = {
			.link_set = nv50_sor_dp_link_set,
			.train_set = nv50_sor_dp_train_set,
			.train_adj = nv50_sor_dp_train_adj
		};
247

248
		nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, &func);
249
	}
250 251 252 253 254
}

static void
nv50_sor_save(struct drm_encoder *encoder)
{
255 256
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
	NV_ERROR(drm, "!!\n");
257 258 259 260 261
}

static void
nv50_sor_restore(struct drm_encoder *encoder)
{
262 263
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
	NV_ERROR(drm, "!!\n");
264 265 266
}

static bool
267 268
nv50_sor_mode_fixup(struct drm_encoder *encoder,
		    const struct drm_display_mode *mode,
269 270
		    struct drm_display_mode *adjusted_mode)
{
271
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
272 273 274
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
	struct nouveau_connector *connector;

275
	NV_DEBUG(drm, "or %d\n", nv_encoder->or);
276 277 278

	connector = nouveau_encoder_connector_get(nv_encoder);
	if (!connector) {
279
		NV_ERROR(drm, "Encoder has no connector\n");
280 281 282 283
		return false;
	}

	if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
V
Ville Syrjälä 已提交
284 285
	     connector->native_mode)
		drm_mode_copy(adjusted_mode, connector->native_mode);
286 287 288 289 290 291 292

	return true;
}

static void
nv50_sor_prepare(struct drm_encoder *encoder)
{
293
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
294
	nv50_sor_disconnect(encoder);
295
	if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
296 297 298
		/* avoid race between link training and supervisor intr */
		nv50_display_sync(encoder->dev);
	}
299 300 301 302 303 304 305 306
}

static void
nv50_sor_commit(struct drm_encoder *encoder)
{
}

static void
307 308
nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
		  struct drm_display_mode *mode)
309
{
310
	struct nouveau_channel *evo = nv50_display(encoder->dev)->master;
311
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
312
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
313
	struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
314
	struct nouveau_connector *nv_connector;
315 316 317
	uint32_t mode_ctl = 0;
	int ret;

318
	NV_DEBUG(drm, "or %d type %d -> crtc %d\n",
319
		     nv_encoder->or, nv_encoder->dcb->type, crtc->index);
320
	nv_encoder->crtc = encoder->crtc;
321 322

	switch (nv_encoder->dcb->type) {
323
	case DCB_OUTPUT_TMDS:
324
		if (nv_encoder->dcb->sorconf.link & 1) {
325
			if (mode->clock < 165000)
326 327 328 329 330
				mode_ctl = 0x0100;
			else
				mode_ctl = 0x0500;
		} else
			mode_ctl = 0x0200;
331

332
		nouveau_hdmi_mode_set(encoder, mode);
333
		break;
334
	case DCB_OUTPUT_DP:
335
		nv_connector = nouveau_encoder_connector_get(nv_encoder);
336
		if (nv_connector && nv_connector->base.display_info.bpc == 6) {
337
			nv_encoder->dp.datarate = mode->clock * 18 / 8;
338
			mode_ctl |= 0x00020000;
339
		} else {
340
			nv_encoder->dp.datarate = mode->clock * 24 / 8;
341
			mode_ctl |= 0x00050000;
342
		}
343

344 345 346 347 348 349 350 351 352 353 354 355 356 357
		if (nv_encoder->dcb->sorconf.link & 1)
			mode_ctl |= 0x00000800;
		else
			mode_ctl |= 0x00000900;
		break;
	default:
		break;
	}

	if (crtc->index == 1)
		mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
	else
		mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;

358
	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
359 360
		mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;

361
	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
362 363
		mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;

364 365
	nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);

366 367
	ret = RING_SPACE(evo, 2);
	if (ret) {
368
		NV_ERROR(drm, "no space while connecting SOR\n");
369
		nv_encoder->crtc = NULL;
370 371
		return;
	}
372
	BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
373
	OUT_RING(evo, mode_ctl);
374 375 376 377 378 379
}

static struct drm_crtc *
nv50_sor_crtc_get(struct drm_encoder *encoder)
{
	return nouveau_encoder(encoder)->crtc;
380 381 382 383 384 385 386 387 388 389
}

static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
	.dpms = nv50_sor_dpms,
	.save = nv50_sor_save,
	.restore = nv50_sor_restore,
	.mode_fixup = nv50_sor_mode_fixup,
	.prepare = nv50_sor_prepare,
	.commit = nv50_sor_commit,
	.mode_set = nv50_sor_mode_set,
390 391 392
	.get_crtc = nv50_sor_crtc_get,
	.detect = NULL,
	.disable = nv50_sor_disconnect
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
};

static void
nv50_sor_destroy(struct drm_encoder *encoder)
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);

	drm_encoder_cleanup(encoder);
	kfree(nv_encoder);
}

static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
	.destroy = nv50_sor_destroy,
};

int
409
nv50_sor_create(struct drm_connector *connector, struct dcb_output *entry)
410 411
{
	struct nouveau_encoder *nv_encoder = NULL;
412
	struct drm_device *dev = connector->dev;
413 414 415 416
	struct drm_encoder *encoder;
	int type;

	switch (entry->type) {
417 418
	case DCB_OUTPUT_TMDS:
	case DCB_OUTPUT_DP:
419 420
		type = DRM_MODE_ENCODER_TMDS;
		break;
421
	case DCB_OUTPUT_LVDS:
422 423 424 425 426 427 428 429 430 431 432 433 434
		type = DRM_MODE_ENCODER_LVDS;
		break;
	default:
		return -EINVAL;
	}

	nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
	if (!nv_encoder)
		return -ENOMEM;
	encoder = to_drm_encoder(nv_encoder);

	nv_encoder->dcb = entry;
	nv_encoder->or = ffs(entry->or) - 1;
435
	nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
436 437 438 439 440 441 442

	drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
	drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);

	encoder->possible_crtcs = entry->heads;
	encoder->possible_clones = 0;

443
	drm_mode_connector_attach_encoder(connector, encoder);
444 445
	return 0;
}