blob: c2b1578ed552911baa801144c874546923806539 [file] [log] [blame]
Ben Skeggs56d237d2014-05-19 14:54:33 +10001/*
Ben Skeggs26f6d882011-07-04 16:25:18 +10002 * Copyright 2011 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
Ben Skeggs15907002018-05-08 20:39:47 +100024#include "disp.h"
25#include "atom.h"
26#include "core.h"
27#include "head.h"
28#include "wndw.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100029
Ben Skeggs51beb422011-07-05 10:33:08 +100030#include <linux/dma-mapping.h>
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -040031#include <linux/hdmi.h>
Ben Skeggs83fc0832011-07-05 13:08:40 +100032
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drmP.h>
Ben Skeggs973f10c2016-11-04 17:20:36 +100034#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drm_crtc_helper.h>
Ben Skeggs48743222014-05-31 01:48:06 +100036#include <drm/drm_dp_helper.h>
Daniel Vetterb516a9e2015-12-04 09:45:43 +010037#include <drm/drm_fb_helper.h>
Ben Skeggsad633612016-11-04 17:20:36 +100038#include <drm/drm_plane_helper.h>
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -040039#include <drm/drm_edid.h>
Ben Skeggs26f6d882011-07-04 16:25:18 +100040
Ben Skeggsfdb751e2014-08-10 04:10:23 +100041#include <nvif/class.h>
Ben Skeggs845f2722015-11-08 12:16:40 +100042#include <nvif/cl0002.h>
Ben Skeggs7568b102015-11-08 10:44:19 +100043#include <nvif/cl5070.h>
Ben Skeggs7568b102015-11-08 10:44:19 +100044#include <nvif/cl507d.h>
Ben Skeggs973f10c2016-11-04 17:20:36 +100045#include <nvif/event.h>
Ben Skeggsfdb751e2014-08-10 04:10:23 +100046
Ben Skeggs4dc28132016-05-20 09:22:55 +100047#include "nouveau_drv.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100048#include "nouveau_dma.h"
49#include "nouveau_gem.h"
Ben Skeggs26f6d882011-07-04 16:25:18 +100050#include "nouveau_connector.h"
51#include "nouveau_encoder.h"
Ben Skeggsf589be82012-07-22 11:55:54 +100052#include "nouveau_fence.h"
Ben Skeggs839ca902016-11-04 17:20:36 +100053#include "nouveau_fbcon.h"
Ben Skeggs816af2f2011-11-16 15:48:48 +100054
Ben Skeggsb5a794b2012-10-16 14:18:32 +100055/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +100056 * Atomic state
57 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +100058
59struct nv50_outp_atom {
60 struct list_head head;
61
62 struct drm_encoder *encoder;
63 bool flush_disable;
64
65 union {
66 struct {
67 bool ctrl:1;
68 };
69 u8 mask;
70 } clr;
71
72 union {
73 struct {
74 bool ctrl:1;
75 };
76 u8 mask;
77 } set;
78};
79
Ben Skeggs3dbd0362016-11-04 17:20:36 +100080/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +100081 * EVO channel
82 *****************************************************************************/
83
Ben Skeggsb5a794b2012-10-16 14:18:32 +100084static int
Ben Skeggsa01ca782015-08-20 14:54:15 +100085nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +100086 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +100087 struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100088{
Ben Skeggs41a63402015-08-20 14:54:16 +100089 struct nvif_sclass *sclass;
90 int ret, i, n;
Ben Skeggs6af52892014-11-03 15:01:33 +100091
Ben Skeggsa01ca782015-08-20 14:54:15 +100092 chan->device = device;
93
Ben Skeggs41a63402015-08-20 14:54:16 +100094 ret = n = nvif_object_sclass_get(disp, &sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +100095 if (ret < 0)
96 return ret;
97
Ben Skeggs410f3ec2014-08-10 04:10:25 +100098 while (oclass[0]) {
Ben Skeggs41a63402015-08-20 14:54:16 +100099 for (i = 0; i < n; i++) {
100 if (sclass[i].oclass == oclass[0]) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000101 ret = nvif_object_init(disp, 0, oclass[0],
Ben Skeggsa01ca782015-08-20 14:54:15 +1000102 data, size, &chan->user);
Ben Skeggs6af52892014-11-03 15:01:33 +1000103 if (ret == 0)
Ben Skeggs01326052017-11-01 03:56:19 +1000104 nvif_object_map(&chan->user, NULL, 0);
Ben Skeggs41a63402015-08-20 14:54:16 +1000105 nvif_object_sclass_put(&sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000106 return ret;
107 }
Ben Skeggsb76f1522014-08-10 04:10:28 +1000108 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000109 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000110 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000111
Ben Skeggs41a63402015-08-20 14:54:16 +1000112 nvif_object_sclass_put(&sclass);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000113 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000114}
115
116static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000117nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000118{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000119 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000120}
121
122/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000123 * DMA EVO channel
124 *****************************************************************************/
125
Ben Skeggs15907002018-05-08 20:39:47 +1000126void
Ben Skeggsf5650472018-05-08 20:39:46 +1000127nv50_dmac_destroy(struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000128{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000129 nvif_object_fini(&dmac->vram);
130 nvif_object_fini(&dmac->sync);
131
132 nv50_chan_destroy(&dmac->base);
133
Ben Skeggsf5650472018-05-08 20:39:46 +1000134 nvif_mem_fini(&dmac->push);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000135}
136
Ben Skeggs15907002018-05-08 20:39:47 +1000137int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000138nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000139 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000140 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000141{
Ben Skeggsf5650472018-05-08 20:39:46 +1000142 struct nouveau_cli *cli = (void *)device->object.client;
Ben Skeggs648d4df2014-08-10 04:10:27 +1000143 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000144 int ret;
145
Daniel Vetter59ad1462012-12-02 14:49:44 +0100146 mutex_init(&dmac->lock);
147
Ben Skeggsf5650472018-05-08 20:39:46 +1000148 ret = nvif_mem_init_map(&cli->mmu, NVIF_MEM_COHERENT, 0x1000,
149 &dmac->push);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000150 if (ret)
151 return ret;
152
Ben Skeggsf5650472018-05-08 20:39:46 +1000153 dmac->ptr = dmac->push.object.map.ptr;
154
155 args->pushbuf = nvif_handle(&dmac->push.object);
Ben Skeggsbf81df92015-08-20 14:54:16 +1000156
Ben Skeggsa01ca782015-08-20 14:54:15 +1000157 ret = nv50_chan_create(device, disp, oclass, head, data, size,
158 &dmac->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000159 if (ret)
160 return ret;
161
Ben Skeggsa01ca782015-08-20 14:54:15 +1000162 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000163 &(struct nv_dma_v0) {
164 .target = NV_DMA_V0_TARGET_VRAM,
165 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000166 .start = syncbuf + 0x0000,
167 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000168 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000169 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000170 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000171 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000172
Ben Skeggsa01ca782015-08-20 14:54:15 +1000173 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000174 &(struct nv_dma_v0) {
175 .target = NV_DMA_V0_TARGET_VRAM,
176 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000177 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000178 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000179 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000180 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000181 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000182 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000183
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000184 return ret;
185}
186
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000187/******************************************************************************
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000188 * EVO channel helpers
189 *****************************************************************************/
Ben Skeggs15907002018-05-08 20:39:47 +1000190u32 *
191evo_wait(struct nv50_dmac *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000192{
Ben Skeggse225f442012-11-21 14:40:21 +1000193 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000194 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000195 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000196
Daniel Vetter59ad1462012-12-02 14:49:44 +0100197 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000198 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000199 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000200
Ben Skeggs0ad72862014-08-10 04:10:22 +1000201 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000202 if (nvif_msec(device, 2000,
203 if (!nvif_rd32(&dmac->base.user, 0x0004))
204 break;
205 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100206 mutex_unlock(&dmac->lock);
Joe Perches8dfe1622017-02-28 04:55:54 -0800207 pr_err("nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000208 return NULL;
209 }
210
211 put = 0;
212 }
213
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000214 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000215}
216
Ben Skeggs15907002018-05-08 20:39:47 +1000217void
218evo_kick(u32 *push, struct nv50_dmac *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000219{
Ben Skeggse225f442012-11-21 14:40:21 +1000220 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000221 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100222 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000223}
224
Ben Skeggs438d99e2011-07-05 16:48:06 +1000225/******************************************************************************
Ben Skeggsd92c8ad2016-11-04 17:20:36 +1000226 * Output path helpers
Ben Skeggsa91d3222014-12-22 16:30:13 +1000227 *****************************************************************************/
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000228static void
229nv50_outp_release(struct nouveau_encoder *nv_encoder)
230{
231 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
232 struct {
233 struct nv50_disp_mthd_v1 base;
234 } args = {
235 .base.version = 1,
236 .base.method = NV50_DISP_MTHD_V1_RELEASE,
237 .base.hasht = nv_encoder->dcb->hasht,
238 .base.hashm = nv_encoder->dcb->hashm,
239 };
240
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000241 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000242 nv_encoder->or = -1;
243 nv_encoder->link = 0;
244}
245
246static int
247nv50_outp_acquire(struct nouveau_encoder *nv_encoder)
248{
249 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
250 struct nv50_disp *disp = nv50_disp(drm->dev);
251 struct {
252 struct nv50_disp_mthd_v1 base;
253 struct nv50_disp_acquire_v0 info;
254 } args = {
255 .base.version = 1,
256 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
257 .base.hasht = nv_encoder->dcb->hasht,
258 .base.hashm = nv_encoder->dcb->hashm,
259 };
260 int ret;
261
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000262 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000263 if (ret) {
264 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
265 return ret;
266 }
267
268 nv_encoder->or = args.info.or;
269 nv_encoder->link = args.info.link;
270 return 0;
271}
272
Ben Skeggsd92c8ad2016-11-04 17:20:36 +1000273static int
274nv50_outp_atomic_check_view(struct drm_encoder *encoder,
275 struct drm_crtc_state *crtc_state,
276 struct drm_connector_state *conn_state,
277 struct drm_display_mode *native_mode)
278{
279 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
280 struct drm_display_mode *mode = &crtc_state->mode;
281 struct drm_connector *connector = conn_state->connector;
282 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
283 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
284
285 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
286 asyc->scaler.full = false;
287 if (!native_mode)
288 return 0;
289
290 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
291 switch (connector->connector_type) {
292 case DRM_MODE_CONNECTOR_LVDS:
293 case DRM_MODE_CONNECTOR_eDP:
294 /* Force use of scaler for non-EDID modes. */
295 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
296 break;
297 mode = native_mode;
298 asyc->scaler.full = true;
299 break;
300 default:
301 break;
302 }
303 } else {
304 mode = native_mode;
305 }
306
307 if (!drm_mode_equal(adjusted_mode, mode)) {
308 drm_mode_copy(adjusted_mode, mode);
309 crtc_state->mode_changed = true;
310 }
311
312 return 0;
313}
314
Ben Skeggs839ca902016-11-04 17:20:36 +1000315static int
316nv50_outp_atomic_check(struct drm_encoder *encoder,
317 struct drm_crtc_state *crtc_state,
318 struct drm_connector_state *conn_state)
Ben Skeggsa91d3222014-12-22 16:30:13 +1000319{
Ben Skeggs839ca902016-11-04 17:20:36 +1000320 struct nouveau_connector *nv_connector =
321 nouveau_connector(conn_state->connector);
322 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
323 nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +1000324}
325
326/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000327 * DAC
328 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000329static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000330nv50_dac_disable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000331{
Ben Skeggsf20c6652016-11-04 17:20:36 +1000332 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggs0a368772018-05-08 20:39:47 +1000333 struct nv50_core *core = nv50_disp(encoder->dev)->core;
334 if (nv_encoder->crtc)
335 core->func->dac->ctrl(core, nv_encoder->or, 0x00000000, NULL);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000336 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000337 nv50_outp_release(nv_encoder);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000338}
339
340static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000341nv50_dac_enable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000342{
343 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
344 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000345 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
Ben Skeggs0a368772018-05-08 20:39:47 +1000346 struct nv50_core *core = nv50_disp(encoder->dev)->core;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000347
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000348 nv50_outp_acquire(nv_encoder);
349
Ben Skeggs0a368772018-05-08 20:39:47 +1000350 core->func->dac->ctrl(core, nv_encoder->or, 1 << nv_crtc->index, asyh);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000351 asyh->or.depth = 0;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000352
353 nv_encoder->crtc = encoder->crtc;
354}
355
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000356static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +1000357nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000358{
Ben Skeggsc4abd312014-08-10 04:10:26 +1000359 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +1000360 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +1000361 struct {
362 struct nv50_disp_mthd_v1 base;
363 struct nv50_disp_dac_load_v0 load;
364 } args = {
365 .base.version = 1,
366 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
367 .base.hasht = nv_encoder->dcb->hasht,
368 .base.hashm = nv_encoder->dcb->hashm,
369 };
370 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +1000371
Ben Skeggsc4abd312014-08-10 04:10:26 +1000372 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
373 if (args.load.data == 0)
374 args.load.data = 340;
375
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000376 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggsc4abd312014-08-10 04:10:26 +1000377 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +1000378 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +1000379
Ben Skeggs35b21d32012-11-08 12:08:55 +1000380 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000381}
382
Ben Skeggsf20c6652016-11-04 17:20:36 +1000383static const struct drm_encoder_helper_funcs
384nv50_dac_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +1000385 .atomic_check = nv50_outp_atomic_check,
386 .enable = nv50_dac_enable,
387 .disable = nv50_dac_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +1000388 .detect = nv50_dac_detect
389};
390
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000391static void
Ben Skeggse225f442012-11-21 14:40:21 +1000392nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000393{
394 drm_encoder_cleanup(encoder);
395 kfree(encoder);
396}
397
Ben Skeggsf20c6652016-11-04 17:20:36 +1000398static const struct drm_encoder_funcs
399nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +1000400 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000401};
402
403static int
Ben Skeggse225f442012-11-21 14:40:21 +1000404nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000405{
Ben Skeggs5ed50202013-02-11 20:15:03 +1000406 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000407 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +1000408 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000409 struct nouveau_encoder *nv_encoder;
410 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +1000411 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000412
413 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
414 if (!nv_encoder)
415 return -ENOMEM;
416 nv_encoder->dcb = dcbe;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +1000417
418 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
419 if (bus)
420 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000421
422 encoder = to_drm_encoder(nv_encoder);
423 encoder->possible_crtcs = dcbe->heads;
424 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +1000425 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
426 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000427 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000428
429 drm_mode_connector_attach_encoder(connector, encoder);
430 return 0;
431}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000432
433/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +1000434 * Audio
435 *****************************************************************************/
436static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000437nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
438{
439 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
440 struct nv50_disp *disp = nv50_disp(encoder->dev);
441 struct {
442 struct nv50_disp_mthd_v1 base;
443 struct nv50_disp_sor_hda_eld_v0 eld;
444 } args = {
445 .base.version = 1,
446 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
447 .base.hasht = nv_encoder->dcb->hasht,
448 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
449 (0x0100 << nv_crtc->index),
450 };
451
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000452 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggsf20c6652016-11-04 17:20:36 +1000453}
454
455static void
456nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +1000457{
458 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +1000459 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +1000460 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +1000461 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +1000462 struct __packed {
463 struct {
464 struct nv50_disp_mthd_v1 mthd;
465 struct nv50_disp_sor_hda_eld_v0 eld;
466 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +1000467 u8 data[sizeof(nv_connector->base.eld)];
468 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +1000469 .base.mthd.version = 1,
470 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
471 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +1000472 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
473 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +1000474 };
Ben Skeggs78951d22011-11-11 18:13:13 +1000475
476 nv_connector = nouveau_encoder_connector_get(nv_encoder);
477 if (!drm_detect_monitor_audio(nv_connector->edid))
478 return;
479
Ben Skeggs120b0c32014-08-10 04:10:26 +1000480 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +1000481
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000482 nvif_mthd(&disp->disp->object, 0, &args,
Jani Nikula938fd8a2014-10-28 16:20:48 +0200483 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +1000484}
485
Ben Skeggsf20c6652016-11-04 17:20:36 +1000486/******************************************************************************
487 * HDMI
488 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +1000489static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000490nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +1000491{
492 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +1000493 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +1000494 struct {
495 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +1000496 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +1000497 } args = {
498 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +1000499 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
500 .base.hasht = nv_encoder->dcb->hasht,
501 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
502 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +1000503 };
Ben Skeggs78951d22011-11-11 18:13:13 +1000504
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000505 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +1000506}
507
Ben Skeggs78951d22011-11-11 18:13:13 +1000508static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000509nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +1000510{
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000511 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
512 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000513 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +1000514 struct {
515 struct nv50_disp_mthd_v1 base;
516 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400517 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
Ben Skeggse00f2232014-08-10 04:10:26 +1000518 } args = {
519 .base.version = 1,
520 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
521 .base.hasht = nv_encoder->dcb->hasht,
522 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
523 (0x0100 << nv_crtc->index),
524 .pwr.state = 1,
525 .pwr.rekey = 56, /* binary driver, and tegra, constant */
526 };
527 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000528 u32 max_ac_packet;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400529 union hdmi_infoframe avi_frame;
530 union hdmi_infoframe vendor_frame;
531 int ret;
532 int size;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000533
534 nv_connector = nouveau_encoder_connector_get(nv_encoder);
535 if (!drm_detect_hdmi_monitor(nv_connector->edid))
536 return;
537
Shashank Sharma0c1f5282017-07-13 21:03:07 +0530538 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi, mode,
539 false);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400540 if (!ret) {
541 /* We have an AVI InfoFrame, populate it to the display */
542 args.pwr.avi_infoframe_length
543 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
544 }
545
Ville Syrjäläf1781e92017-11-13 19:04:19 +0200546 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
547 &nv_connector->base, mode);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400548 if (!ret) {
549 /* We have a Vendor InfoFrame, populate it to the display */
550 args.pwr.vendor_infoframe_length
551 = hdmi_infoframe_pack(&vendor_frame,
552 args.infoframes
553 + args.pwr.avi_infoframe_length,
554 17);
555 }
556
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000557 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +1000558 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000559 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +1000560 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000561
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400562 size = sizeof(args.base)
563 + sizeof(args.pwr)
564 + args.pwr.avi_infoframe_length
565 + args.pwr.vendor_infoframe_length;
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000566 nvif_mthd(&disp->disp->object, 0, &args, size);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000567 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +1000568}
569
570/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000571 * MST
572 *****************************************************************************/
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000573#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
574#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
575#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
576
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000577struct nv50_mstm {
578 struct nouveau_encoder *outp;
579
580 struct drm_dp_mst_topology_mgr mgr;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000581 struct nv50_msto *msto[4];
582
583 bool modified;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000584 bool disabled;
585 int links;
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000586};
587
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000588struct nv50_mstc {
589 struct nv50_mstm *mstm;
590 struct drm_dp_mst_port *port;
591 struct drm_connector connector;
592
593 struct drm_display_mode *native;
594 struct edid *edid;
595
596 int pbn;
597};
598
599struct nv50_msto {
600 struct drm_encoder encoder;
601
602 struct nv50_head *head;
603 struct nv50_mstc *mstc;
604 bool disabled;
605};
606
607static struct drm_dp_payload *
608nv50_msto_payload(struct nv50_msto *msto)
609{
610 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
611 struct nv50_mstc *mstc = msto->mstc;
612 struct nv50_mstm *mstm = mstc->mstm;
613 int vcpi = mstc->port->vcpi.vcpi, i;
614
615 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
616 for (i = 0; i < mstm->mgr.max_payloads; i++) {
617 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
618 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
619 mstm->outp->base.base.name, i, payload->vcpi,
620 payload->start_slot, payload->num_slots);
621 }
622
623 for (i = 0; i < mstm->mgr.max_payloads; i++) {
624 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
625 if (payload->vcpi == vcpi)
626 return payload;
627 }
628
629 return NULL;
630}
631
632static void
633nv50_msto_cleanup(struct nv50_msto *msto)
634{
635 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
636 struct nv50_mstc *mstc = msto->mstc;
637 struct nv50_mstm *mstm = mstc->mstm;
638
639 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
640 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
641 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
642 if (msto->disabled) {
643 msto->mstc = NULL;
644 msto->head = NULL;
645 msto->disabled = false;
646 }
647}
648
649static void
650nv50_msto_prepare(struct nv50_msto *msto)
651{
652 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
653 struct nv50_mstc *mstc = msto->mstc;
654 struct nv50_mstm *mstm = mstc->mstm;
655 struct {
656 struct nv50_disp_mthd_v1 base;
657 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
658 } args = {
659 .base.version = 1,
660 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
661 .base.hasht = mstm->outp->dcb->hasht,
662 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
663 (0x0100 << msto->head->base.index),
664 };
665
666 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
667 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
668 struct drm_dp_payload *payload = nv50_msto_payload(msto);
669 if (payload) {
670 args.vcpi.start_slot = payload->start_slot;
671 args.vcpi.num_slots = payload->num_slots;
672 args.vcpi.pbn = mstc->port->vcpi.pbn;
673 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
674 }
675 }
676
677 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
678 msto->encoder.name, msto->head->base.base.name,
679 args.vcpi.start_slot, args.vcpi.num_slots,
680 args.vcpi.pbn, args.vcpi.aligned_pbn);
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000681 nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000682}
683
684static int
685nv50_msto_atomic_check(struct drm_encoder *encoder,
686 struct drm_crtc_state *crtc_state,
687 struct drm_connector_state *conn_state)
688{
689 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
690 struct nv50_mstm *mstm = mstc->mstm;
691 int bpp = conn_state->connector->display_info.bpc * 3;
692 int slots;
693
694 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
695
696 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
697 if (slots < 0)
698 return slots;
699
700 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
701 mstc->native);
702}
703
704static void
705nv50_msto_enable(struct drm_encoder *encoder)
706{
707 struct nv50_head *head = nv50_head(encoder->crtc);
708 struct nv50_msto *msto = nv50_msto(encoder);
709 struct nv50_mstc *mstc = NULL;
710 struct nv50_mstm *mstm = NULL;
711 struct drm_connector *connector;
Gustavo Padovan875dd622017-05-11 16:10:46 -0300712 struct drm_connector_list_iter conn_iter;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000713 u8 proto, depth;
714 int slots;
715 bool r;
716
Gustavo Padovan875dd622017-05-11 16:10:46 -0300717 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
718 drm_for_each_connector_iter(connector, &conn_iter) {
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000719 if (connector->state->best_encoder == &msto->encoder) {
720 mstc = nv50_mstc(connector);
721 mstm = mstc->mstm;
722 break;
723 }
724 }
Gustavo Padovan875dd622017-05-11 16:10:46 -0300725 drm_connector_list_iter_end(&conn_iter);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000726
727 if (WARN_ON(!mstc))
728 return;
729
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -0700730 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
731 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000732 WARN_ON(!r);
733
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000734 if (!mstm->links++)
735 nv50_outp_acquire(mstm->outp);
736
737 if (mstm->outp->link & 1)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000738 proto = 0x8;
739 else
740 proto = 0x9;
741
742 switch (mstc->connector.display_info.bpc) {
743 case 6: depth = 0x2; break;
744 case 8: depth = 0x5; break;
745 case 10:
746 default: depth = 0x6; break;
747 }
748
749 mstm->outp->update(mstm->outp, head->base.index,
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000750 nv50_head_atom(head->base.base.state), proto, depth);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000751
752 msto->head = head;
753 msto->mstc = mstc;
754 mstm->modified = true;
755}
756
757static void
758nv50_msto_disable(struct drm_encoder *encoder)
759{
760 struct nv50_msto *msto = nv50_msto(encoder);
761 struct nv50_mstc *mstc = msto->mstc;
762 struct nv50_mstm *mstm = mstc->mstm;
763
764 if (mstc->port)
765 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
766
767 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
768 mstm->modified = true;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000769 if (!--mstm->links)
770 mstm->disabled = true;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000771 msto->disabled = true;
772}
773
774static const struct drm_encoder_helper_funcs
775nv50_msto_help = {
776 .disable = nv50_msto_disable,
777 .enable = nv50_msto_enable,
778 .atomic_check = nv50_msto_atomic_check,
779};
780
781static void
782nv50_msto_destroy(struct drm_encoder *encoder)
783{
784 struct nv50_msto *msto = nv50_msto(encoder);
785 drm_encoder_cleanup(&msto->encoder);
786 kfree(msto);
787}
788
789static const struct drm_encoder_funcs
790nv50_msto = {
791 .destroy = nv50_msto_destroy,
792};
793
794static int
795nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
796 struct nv50_msto **pmsto)
797{
798 struct nv50_msto *msto;
799 int ret;
800
801 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
802 return -ENOMEM;
803
804 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
805 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
806 if (ret) {
807 kfree(*pmsto);
808 *pmsto = NULL;
809 return ret;
810 }
811
812 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
813 msto->encoder.possible_crtcs = heads;
814 return 0;
815}
816
817static struct drm_encoder *
818nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
819 struct drm_connector_state *connector_state)
820{
821 struct nv50_head *head = nv50_head(connector_state->crtc);
822 struct nv50_mstc *mstc = nv50_mstc(connector);
823 if (mstc->port) {
824 struct nv50_mstm *mstm = mstc->mstm;
825 return &mstm->msto[head->base.index]->encoder;
826 }
827 return NULL;
828}
829
830static struct drm_encoder *
831nv50_mstc_best_encoder(struct drm_connector *connector)
832{
833 struct nv50_mstc *mstc = nv50_mstc(connector);
834 if (mstc->port) {
835 struct nv50_mstm *mstm = mstc->mstm;
836 return &mstm->msto[0]->encoder;
837 }
838 return NULL;
839}
840
841static enum drm_mode_status
842nv50_mstc_mode_valid(struct drm_connector *connector,
843 struct drm_display_mode *mode)
844{
845 return MODE_OK;
846}
847
848static int
849nv50_mstc_get_modes(struct drm_connector *connector)
850{
851 struct nv50_mstc *mstc = nv50_mstc(connector);
852 int ret = 0;
853
854 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
855 drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
Jani Nikulad471ed02017-11-01 16:21:02 +0200856 if (mstc->edid)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000857 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000858
859 if (!mstc->connector.display_info.bpc)
860 mstc->connector.display_info.bpc = 8;
861
862 if (mstc->native)
863 drm_mode_destroy(mstc->connector.dev, mstc->native);
864 mstc->native = nouveau_conn_native_mode(&mstc->connector);
865 return ret;
866}
867
868static const struct drm_connector_helper_funcs
869nv50_mstc_help = {
870 .get_modes = nv50_mstc_get_modes,
871 .mode_valid = nv50_mstc_mode_valid,
872 .best_encoder = nv50_mstc_best_encoder,
873 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
874};
875
876static enum drm_connector_status
877nv50_mstc_detect(struct drm_connector *connector, bool force)
878{
879 struct nv50_mstc *mstc = nv50_mstc(connector);
880 if (!mstc->port)
881 return connector_status_disconnected;
882 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
883}
884
885static void
886nv50_mstc_destroy(struct drm_connector *connector)
887{
888 struct nv50_mstc *mstc = nv50_mstc(connector);
889 drm_connector_cleanup(&mstc->connector);
890 kfree(mstc);
891}
892
893static const struct drm_connector_funcs
894nv50_mstc = {
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000895 .reset = nouveau_conn_reset,
896 .detect = nv50_mstc_detect,
897 .fill_modes = drm_helper_probe_single_connector_modes,
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000898 .destroy = nv50_mstc_destroy,
899 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
900 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
901 .atomic_set_property = nouveau_conn_atomic_set_property,
902 .atomic_get_property = nouveau_conn_atomic_get_property,
903};
904
905static int
906nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
907 const char *path, struct nv50_mstc **pmstc)
908{
909 struct drm_device *dev = mstm->outp->base.base.dev;
910 struct nv50_mstc *mstc;
911 int ret, i;
912
913 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
914 return -ENOMEM;
915 mstc->mstm = mstm;
916 mstc->port = port;
917
918 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
919 DRM_MODE_CONNECTOR_DisplayPort);
920 if (ret) {
921 kfree(*pmstc);
922 *pmstc = NULL;
923 return ret;
924 }
925
926 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
927
928 mstc->connector.funcs->reset(&mstc->connector);
929 nouveau_conn_attach_properties(&mstc->connector);
930
Colin Ian King27a451e2017-08-17 23:03:23 +0100931 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto[i]; i++)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000932 drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
933
934 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
935 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
936 drm_mode_connector_set_path_property(&mstc->connector, path);
937 return 0;
938}
939
940static void
941nv50_mstm_cleanup(struct nv50_mstm *mstm)
942{
943 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
944 struct drm_encoder *encoder;
945 int ret;
946
947 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
948 ret = drm_dp_check_act_status(&mstm->mgr);
949
950 ret = drm_dp_update_payload_part2(&mstm->mgr);
951
952 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
953 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
954 struct nv50_msto *msto = nv50_msto(encoder);
955 struct nv50_mstc *mstc = msto->mstc;
956 if (mstc && mstc->mstm == mstm)
957 nv50_msto_cleanup(msto);
958 }
959 }
960
961 mstm->modified = false;
962}
963
964static void
965nv50_mstm_prepare(struct nv50_mstm *mstm)
966{
967 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
968 struct drm_encoder *encoder;
969 int ret;
970
971 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
972 ret = drm_dp_update_payload_part1(&mstm->mgr);
973
974 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
975 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
976 struct nv50_msto *msto = nv50_msto(encoder);
977 struct nv50_mstc *mstc = msto->mstc;
978 if (mstc && mstc->mstm == mstm)
979 nv50_msto_prepare(msto);
980 }
981 }
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000982
983 if (mstm->disabled) {
984 if (!mstm->links)
985 nv50_outp_release(mstm->outp);
986 mstm->disabled = false;
987 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000988}
989
990static void
991nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
992{
993 struct nv50_mstm *mstm = nv50_mstm(mgr);
994 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
995}
996
997static void
998nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
999 struct drm_connector *connector)
1000{
1001 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1002 struct nv50_mstc *mstc = nv50_mstc(connector);
1003
1004 drm_connector_unregister(&mstc->connector);
1005
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001006 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
Lyude Paul352672d2018-05-02 19:38:48 -04001007
1008 drm_modeset_lock(&drm->dev->mode_config.connection_mutex, NULL);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001009 mstc->port = NULL;
Lyude Paul352672d2018-05-02 19:38:48 -04001010 drm_modeset_unlock(&drm->dev->mode_config.connection_mutex);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001011
1012 drm_connector_unreference(&mstc->connector);
1013}
1014
1015static void
1016nv50_mstm_register_connector(struct drm_connector *connector)
1017{
1018 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1019
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001020 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001021
1022 drm_connector_register(connector);
1023}
1024
1025static struct drm_connector *
1026nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1027 struct drm_dp_mst_port *port, const char *path)
1028{
1029 struct nv50_mstm *mstm = nv50_mstm(mgr);
1030 struct nv50_mstc *mstc;
1031 int ret;
1032
1033 ret = nv50_mstc_new(mstm, port, path, &mstc);
1034 if (ret) {
1035 if (mstc)
1036 mstc->connector.funcs->destroy(&mstc->connector);
1037 return NULL;
1038 }
1039
1040 return &mstc->connector;
1041}
1042
1043static const struct drm_dp_mst_topology_cbs
1044nv50_mstm = {
1045 .add_connector = nv50_mstm_add_connector,
1046 .register_connector = nv50_mstm_register_connector,
1047 .destroy_connector = nv50_mstm_destroy_connector,
1048 .hotplug = nv50_mstm_hotplug,
1049};
1050
1051void
1052nv50_mstm_service(struct nv50_mstm *mstm)
1053{
Ben Skeggs227f66d2017-10-03 16:24:28 +10001054 struct drm_dp_aux *aux = mstm ? mstm->mgr.aux : NULL;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001055 bool handled = true;
1056 int ret;
1057 u8 esi[8] = {};
1058
Ben Skeggs227f66d2017-10-03 16:24:28 +10001059 if (!aux)
1060 return;
1061
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001062 while (handled) {
1063 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1064 if (ret != 8) {
1065 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1066 return;
1067 }
1068
1069 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1070 if (!handled)
1071 break;
1072
1073 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
1074 }
1075}
1076
1077void
1078nv50_mstm_remove(struct nv50_mstm *mstm)
1079{
1080 if (mstm)
1081 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1082}
1083
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001084static int
1085nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
1086{
1087 struct nouveau_encoder *outp = mstm->outp;
1088 struct {
1089 struct nv50_disp_mthd_v1 base;
1090 struct nv50_disp_sor_dp_mst_link_v0 mst;
1091 } args = {
1092 .base.version = 1,
1093 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
1094 .base.hasht = outp->dcb->hasht,
1095 .base.hashm = outp->dcb->hashm,
1096 .mst.state = state,
1097 };
1098 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001099 struct nvif_object *disp = &drm->display->disp.object;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001100 int ret;
1101
1102 if (dpcd >= 0x12) {
1103 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
1104 if (ret < 0)
1105 return ret;
1106
1107 dpcd &= ~DP_MST_EN;
1108 if (state)
1109 dpcd |= DP_MST_EN;
1110
1111 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
1112 if (ret < 0)
1113 return ret;
1114 }
1115
1116 return nvif_mthd(disp, 0, &args, sizeof(args));
1117}
1118
1119int
1120nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
1121{
1122 int ret, state = 0;
1123
1124 if (!mstm)
1125 return 0;
1126
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10001127 if (dpcd[0] >= 0x12) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001128 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
1129 if (ret < 0)
1130 return ret;
1131
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10001132 if (!(dpcd[1] & DP_MST_CAP))
1133 dpcd[0] = 0x11;
1134 else
1135 state = allow;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001136 }
1137
1138 ret = nv50_mstm_enable(mstm, dpcd[0], state);
1139 if (ret)
1140 return ret;
1141
1142 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
1143 if (ret)
1144 return nv50_mstm_enable(mstm, dpcd[0], 0);
1145
1146 return mstm->mgr.mst_state;
1147}
1148
1149static void
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001150nv50_mstm_fini(struct nv50_mstm *mstm)
1151{
1152 if (mstm && mstm->mgr.mst_state)
1153 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1154}
1155
1156static void
1157nv50_mstm_init(struct nv50_mstm *mstm)
1158{
1159 if (mstm && mstm->mgr.mst_state)
1160 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
1161}
1162
1163static void
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001164nv50_mstm_del(struct nv50_mstm **pmstm)
1165{
1166 struct nv50_mstm *mstm = *pmstm;
1167 if (mstm) {
1168 kfree(*pmstm);
1169 *pmstm = NULL;
1170 }
1171}
1172
1173static int
1174nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1175 int conn_base_id, struct nv50_mstm **pmstm)
1176{
1177 const int max_payloads = hweight8(outp->dcb->heads);
1178 struct drm_device *dev = outp->base.base.dev;
1179 struct nv50_mstm *mstm;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001180 int ret, i;
1181 u8 dpcd;
1182
1183 /* This is a workaround for some monitors not functioning
1184 * correctly in MST mode on initial module load. I think
1185 * some bad interaction with the VBIOS may be responsible.
1186 *
1187 * A good ol' off and on again seems to work here ;)
1188 */
1189 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
1190 if (ret >= 0 && dpcd >= 0x12)
1191 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001192
1193 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1194 return -ENOMEM;
1195 mstm->outp = outp;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001196 mstm->mgr.cbs = &nv50_mstm;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001197
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001198 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001199 max_payloads, conn_base_id);
1200 if (ret)
1201 return ret;
1202
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001203 for (i = 0; i < max_payloads; i++) {
1204 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
1205 i, &mstm->msto[i]);
1206 if (ret)
1207 return ret;
1208 }
1209
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001210 return 0;
1211}
1212
1213/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001214 * SOR
1215 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001216static void
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001217nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001218 struct nv50_head_atom *asyh, u8 proto, u8 depth)
Ben Skeggse84a35a2014-06-05 10:59:55 +10001219{
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10001220 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
Ben Skeggs0a368772018-05-08 20:39:47 +10001221 struct nv50_core *core = disp->core;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001222
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001223 if (!asyh) {
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001224 nv_encoder->ctrl &= ~BIT(head);
1225 if (!(nv_encoder->ctrl & 0x0000000f))
1226 nv_encoder->ctrl = 0;
1227 } else {
1228 nv_encoder->ctrl |= proto << 8;
1229 nv_encoder->ctrl |= BIT(head);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001230 asyh->or.depth = depth;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001231 }
1232
Ben Skeggs0a368772018-05-08 20:39:47 +10001233 core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001234}
1235
1236static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001237nv50_sor_disable(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001238{
1239 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001240 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001241
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001242 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001243
1244 if (nv_crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001245 struct nvkm_i2c_aux *aux = nv_encoder->aux;
1246 u8 pwr;
1247
1248 if (aux) {
1249 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
1250 if (ret == 0) {
1251 pwr &= ~DP_SET_POWER_MASK;
1252 pwr |= DP_SET_POWER_D3;
1253 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
1254 }
1255 }
1256
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001257 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001258 nv50_audio_disable(encoder, nv_crtc);
1259 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001260 nv50_outp_release(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001261 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001262}
1263
1264static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001265nv50_sor_enable(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001266{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001267 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1268 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001269 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1270 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001271 struct {
1272 struct nv50_disp_mthd_v1 base;
1273 struct nv50_disp_sor_lvds_script_v0 lvds;
1274 } lvds = {
1275 .base.version = 1,
1276 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1277 .base.hasht = nv_encoder->dcb->hasht,
1278 .base.hashm = nv_encoder->dcb->hashm,
1279 };
Ben Skeggse225f442012-11-21 14:40:21 +10001280 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001281 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001282 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001283 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001284 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001285 u8 proto = 0xf;
1286 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001287
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001288 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001289 nv_encoder->crtc = encoder->crtc;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001290 nv50_outp_acquire(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001291
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001292 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001293 case DCB_OUTPUT_TMDS:
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001294 if (nv_encoder->link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05001295 proto = 0x1;
1296 /* Only enable dual-link if:
1297 * - Need to (i.e. rate > 165MHz)
1298 * - DCB says we can
1299 * - Not an HDMI monitor, since there's no dual-link
1300 * on HDMI.
1301 */
1302 if (mode->clock >= 165000 &&
1303 nv_encoder->dcb->duallink_possible &&
1304 !drm_detect_hdmi_monitor(nv_connector->edid))
1305 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001306 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001307 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001308 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001309
Ben Skeggsf20c6652016-11-04 17:20:36 +10001310 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001311 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001312 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001313 proto = 0x0;
1314
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001315 if (bios->fp_no_ddc) {
1316 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001317 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001318 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001319 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001320 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001321 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001322 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001323 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001324 } else
1325 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001326 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001327 }
1328
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001329 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001330 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001331 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001332 } else {
1333 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001334 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001335 }
1336
1337 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001338 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001339 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001340
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001341 nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001342 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001343 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10001344 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001345 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001346 else
1347 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001348 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001349 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001350 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001351
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001352 if (nv_encoder->link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001353 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001354 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001355 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001356
1357 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001358 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001359 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10001360 BUG();
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001361 break;
1362 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001363
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001364 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001365}
1366
Ben Skeggsf20c6652016-11-04 17:20:36 +10001367static const struct drm_encoder_helper_funcs
1368nv50_sor_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001369 .atomic_check = nv50_outp_atomic_check,
1370 .enable = nv50_sor_enable,
1371 .disable = nv50_sor_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10001372};
1373
Ben Skeggs83fc0832011-07-05 13:08:40 +10001374static void
Ben Skeggse225f442012-11-21 14:40:21 +10001375nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001376{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001377 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1378 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001379 drm_encoder_cleanup(encoder);
1380 kfree(encoder);
1381}
1382
Ben Skeggsf20c6652016-11-04 17:20:36 +10001383static const struct drm_encoder_funcs
1384nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10001385 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001386};
1387
1388static int
Ben Skeggse225f442012-11-21 14:40:21 +10001389nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001390{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001391 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10001392 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001393 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001394 struct nouveau_encoder *nv_encoder;
1395 struct drm_encoder *encoder;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001396 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001397
1398 switch (dcbe->type) {
1399 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1400 case DCB_OUTPUT_TMDS:
1401 case DCB_OUTPUT_DP:
1402 default:
1403 type = DRM_MODE_ENCODER_TMDS;
1404 break;
1405 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001406
1407 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1408 if (!nv_encoder)
1409 return -ENOMEM;
1410 nv_encoder->dcb = dcbe;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001411 nv_encoder->update = nv50_sor_update;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001412
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001413 encoder = to_drm_encoder(nv_encoder);
1414 encoder->possible_crtcs = dcbe->heads;
1415 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10001416 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1417 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001418 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001419
1420 drm_mode_connector_attach_encoder(connector, encoder);
1421
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001422 if (dcbe->type == DCB_OUTPUT_DP) {
Ben Skeggs13a86512017-07-19 16:49:59 +10001423 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001424 struct nvkm_i2c_aux *aux =
1425 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1426 if (aux) {
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001427 if (disp->disp->object.oclass < GF110_DISP) {
Ben Skeggs13a86512017-07-19 16:49:59 +10001428 /* HW has no support for address-only
1429 * transactions, so we're required to
1430 * use custom I2C-over-AUX code.
1431 */
1432 nv_encoder->i2c = &aux->i2c;
1433 } else {
1434 nv_encoder->i2c = &nv_connector->aux.ddc;
1435 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001436 nv_encoder->aux = aux;
1437 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001438
1439 /*TODO: Use DP Info Table to check for support. */
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001440 if (disp->disp->object.oclass >= GF110_DISP) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001441 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
1442 nv_connector->base.base.id,
1443 &nv_encoder->dp.mstm);
1444 if (ret)
1445 return ret;
1446 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001447 } else {
1448 struct nvkm_i2c_bus *bus =
1449 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1450 if (bus)
1451 nv_encoder->i2c = &bus->i2c;
1452 }
1453
Ben Skeggs83fc0832011-07-05 13:08:40 +10001454 return 0;
1455}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001456
1457/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10001458 * PIOR
1459 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +10001460static int
1461nv50_pior_atomic_check(struct drm_encoder *encoder,
1462 struct drm_crtc_state *crtc_state,
1463 struct drm_connector_state *conn_state)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001464{
Ben Skeggs839ca902016-11-04 17:20:36 +10001465 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1466 if (ret)
1467 return ret;
1468 crtc_state->adjusted_mode.clock *= 2;
1469 return 0;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001470}
1471
1472static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001473nv50_pior_disable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001474{
Ben Skeggsf20c6652016-11-04 17:20:36 +10001475 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggs0a368772018-05-08 20:39:47 +10001476 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1477 if (nv_encoder->crtc)
1478 core->func->pior->ctrl(core, nv_encoder->or, 0x00000000, NULL);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001479 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001480 nv50_outp_release(nv_encoder);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001481}
1482
1483static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001484nv50_pior_enable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001485{
Ben Skeggseb6313a2013-02-11 09:52:58 +10001486 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1487 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1488 struct nouveau_connector *nv_connector;
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001489 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
Ben Skeggs0a368772018-05-08 20:39:47 +10001490 struct nv50_core *core = nv50_disp(encoder->dev)->core;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001491 u8 owner = 1 << nv_crtc->index;
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001492 u8 proto;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001493
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001494 nv50_outp_acquire(nv_encoder);
1495
Ben Skeggseb6313a2013-02-11 09:52:58 +10001496 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1497 switch (nv_connector->base.display_info.bpc) {
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001498 case 10: asyh->or.depth = 0x6; break;
1499 case 8: asyh->or.depth = 0x5; break;
1500 case 6: asyh->or.depth = 0x2; break;
1501 default: asyh->or.depth = 0x0; break;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001502 }
1503
1504 switch (nv_encoder->dcb->type) {
1505 case DCB_OUTPUT_TMDS:
1506 case DCB_OUTPUT_DP:
1507 proto = 0x0;
1508 break;
1509 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10001510 BUG();
Ben Skeggseb6313a2013-02-11 09:52:58 +10001511 break;
1512 }
1513
Ben Skeggs0a368772018-05-08 20:39:47 +10001514 core->func->pior->ctrl(core, nv_encoder->or, (proto << 8) | owner, asyh);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001515 nv_encoder->crtc = encoder->crtc;
1516}
1517
Ben Skeggsf20c6652016-11-04 17:20:36 +10001518static const struct drm_encoder_helper_funcs
1519nv50_pior_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001520 .atomic_check = nv50_pior_atomic_check,
1521 .enable = nv50_pior_enable,
1522 .disable = nv50_pior_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10001523};
Ben Skeggseb6313a2013-02-11 09:52:58 +10001524
1525static void
1526nv50_pior_destroy(struct drm_encoder *encoder)
1527{
1528 drm_encoder_cleanup(encoder);
1529 kfree(encoder);
1530}
1531
Ben Skeggsf20c6652016-11-04 17:20:36 +10001532static const struct drm_encoder_funcs
1533nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10001534 .destroy = nv50_pior_destroy,
1535};
1536
1537static int
1538nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1539{
1540 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001541 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001542 struct nvkm_i2c_bus *bus = NULL;
1543 struct nvkm_i2c_aux *aux = NULL;
1544 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001545 struct nouveau_encoder *nv_encoder;
1546 struct drm_encoder *encoder;
1547 int type;
1548
1549 switch (dcbe->type) {
1550 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001551 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1552 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001553 type = DRM_MODE_ENCODER_TMDS;
1554 break;
1555 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001556 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
Ben Skeggs62b290f2018-05-08 20:39:47 +10001557 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001558 type = DRM_MODE_ENCODER_TMDS;
1559 break;
1560 default:
1561 return -ENODEV;
1562 }
1563
1564 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1565 if (!nv_encoder)
1566 return -ENOMEM;
1567 nv_encoder->dcb = dcbe;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001568 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001569 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001570
1571 encoder = to_drm_encoder(nv_encoder);
1572 encoder->possible_crtcs = dcbe->heads;
1573 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10001574 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1575 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001576 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001577
1578 drm_mode_connector_attach_encoder(connector, encoder);
1579 return 0;
1580}
1581
1582/******************************************************************************
Ben Skeggs839ca902016-11-04 17:20:36 +10001583 * Atomic
1584 *****************************************************************************/
1585
1586static void
1587nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
1588{
1589 struct nv50_disp *disp = nv50_disp(drm->dev);
Ben Skeggs09e1b782018-05-08 20:39:47 +10001590 struct nv50_core *core = disp->core;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001591 struct nv50_mstm *mstm;
1592 struct drm_encoder *encoder;
Ben Skeggs839ca902016-11-04 17:20:36 +10001593
1594 NV_ATOMIC(drm, "commit core %08x\n", interlock);
1595
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001596 drm_for_each_encoder(encoder, drm->dev) {
1597 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1598 mstm = nouveau_encoder(encoder)->dp.mstm;
1599 if (mstm && mstm->modified)
1600 nv50_mstm_prepare(mstm);
1601 }
1602 }
1603
Ben Skeggs09e1b782018-05-08 20:39:47 +10001604 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1605 core->func->update(core, interlock, true);
1606 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1607 disp->core->chan.base.device))
1608 NV_ERROR(drm, "core notifier timeout\n");
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001609
1610 drm_for_each_encoder(encoder, drm->dev) {
1611 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1612 mstm = nouveau_encoder(encoder)->dp.mstm;
1613 if (mstm && mstm->modified)
1614 nv50_mstm_cleanup(mstm);
1615 }
1616 }
Ben Skeggs839ca902016-11-04 17:20:36 +10001617}
1618
1619static void
1620nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1621{
1622 struct drm_device *dev = state->dev;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001623 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001624 struct drm_crtc *crtc;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001625 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001626 struct drm_plane *plane;
1627 struct nouveau_drm *drm = nouveau_drm(dev);
1628 struct nv50_disp *disp = nv50_disp(dev);
1629 struct nv50_atom *atom = nv50_atom(state);
1630 struct nv50_outp_atom *outp, *outt;
1631 u32 interlock_core = 0;
1632 u32 interlock_chan = 0;
1633 int i;
1634
1635 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
1636 drm_atomic_helper_wait_for_fences(dev, state, false);
1637 drm_atomic_helper_wait_for_dependencies(state);
1638 drm_atomic_helper_update_legacy_modeset_state(dev, state);
1639
1640 if (atom->lock_core)
1641 mutex_lock(&disp->mutex);
1642
1643 /* Disable head(s). */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001644 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001645 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001646 struct nv50_head *head = nv50_head(crtc);
1647
1648 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
1649 asyh->clr.mask, asyh->set.mask);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001650 if (old_crtc_state->active && !new_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001651 drm_crtc_vblank_off(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001652
1653 if (asyh->clr.mask) {
1654 nv50_head_flush_clr(head, asyh, atom->flush_disable);
1655 interlock_core |= 1;
1656 }
1657 }
1658
1659 /* Disable plane(s). */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001660 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1661 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001662 struct nv50_wndw *wndw = nv50_wndw(plane);
1663
1664 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
1665 asyw->clr.mask, asyw->set.mask);
1666 if (!asyw->clr.mask)
1667 continue;
1668
1669 interlock_chan |= nv50_wndw_flush_clr(wndw, interlock_core,
1670 atom->flush_disable,
1671 asyw);
1672 }
1673
1674 /* Disable output path(s). */
1675 list_for_each_entry(outp, &atom->outp, head) {
1676 const struct drm_encoder_helper_funcs *help;
1677 struct drm_encoder *encoder;
1678
1679 encoder = outp->encoder;
1680 help = encoder->helper_private;
1681
1682 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
1683 outp->clr.mask, outp->set.mask);
1684
1685 if (outp->clr.mask) {
1686 help->disable(encoder);
1687 interlock_core |= 1;
1688 if (outp->flush_disable) {
1689 nv50_disp_atomic_commit_core(drm, interlock_chan);
1690 interlock_core = 0;
1691 interlock_chan = 0;
1692 }
1693 }
1694 }
1695
1696 /* Flush disable. */
1697 if (interlock_core) {
1698 if (atom->flush_disable) {
1699 nv50_disp_atomic_commit_core(drm, interlock_chan);
1700 interlock_core = 0;
1701 interlock_chan = 0;
1702 }
1703 }
1704
1705 /* Update output path(s). */
1706 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
1707 const struct drm_encoder_helper_funcs *help;
1708 struct drm_encoder *encoder;
1709
1710 encoder = outp->encoder;
1711 help = encoder->helper_private;
1712
1713 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
1714 outp->set.mask, outp->clr.mask);
1715
1716 if (outp->set.mask) {
1717 help->enable(encoder);
1718 interlock_core = 1;
1719 }
1720
1721 list_del(&outp->head);
1722 kfree(outp);
1723 }
1724
1725 /* Update head(s). */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001726 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001727 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001728 struct nv50_head *head = nv50_head(crtc);
1729
1730 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
1731 asyh->set.mask, asyh->clr.mask);
1732
1733 if (asyh->set.mask) {
1734 nv50_head_flush_set(head, asyh);
1735 interlock_core = 1;
1736 }
Ben Skeggs839ca902016-11-04 17:20:36 +10001737
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001738 if (new_crtc_state->active) {
1739 if (!old_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001740 drm_crtc_vblank_on(crtc);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001741 if (new_crtc_state->event)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001742 drm_crtc_vblank_get(crtc);
1743 }
Ben Skeggs2b507892017-01-24 09:32:26 +10001744 }
1745
Ben Skeggs839ca902016-11-04 17:20:36 +10001746 /* Update plane(s). */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001747 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1748 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001749 struct nv50_wndw *wndw = nv50_wndw(plane);
1750
1751 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
1752 asyw->set.mask, asyw->clr.mask);
1753 if ( !asyw->set.mask &&
1754 (!asyw->clr.mask || atom->flush_disable))
1755 continue;
1756
1757 interlock_chan |= nv50_wndw_flush_set(wndw, interlock_core, asyw);
1758 }
1759
1760 /* Flush update. */
1761 if (interlock_core) {
Ben Skeggs09e1b782018-05-08 20:39:47 +10001762 if (interlock_chan || !atom->state.legacy_cursor_update)
Ben Skeggs839ca902016-11-04 17:20:36 +10001763 nv50_disp_atomic_commit_core(drm, interlock_chan);
Ben Skeggs09e1b782018-05-08 20:39:47 +10001764 else
1765 disp->core->func->update(disp->core, 0, false);
Ben Skeggs839ca902016-11-04 17:20:36 +10001766 }
1767
1768 if (atom->lock_core)
1769 mutex_unlock(&disp->mutex);
1770
1771 /* Wait for HW to signal completion. */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001772 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1773 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001774 struct nv50_wndw *wndw = nv50_wndw(plane);
1775 int ret = nv50_wndw_wait_armed(wndw, asyw);
1776 if (ret)
1777 NV_ERROR(drm, "%s: timeout\n", plane->name);
1778 }
1779
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001780 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1781 if (new_crtc_state->event) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001782 unsigned long flags;
Mario Kleinerbd9f6602016-11-23 07:58:54 +01001783 /* Get correct count/ts if racing with vblank irq */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001784 if (new_crtc_state->active)
Dave Airlie0c697fa2017-08-15 16:16:58 +10001785 drm_crtc_accurate_vblank_count(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001786 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001787 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Ben Skeggs839ca902016-11-04 17:20:36 +10001788 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001789
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001790 new_crtc_state->event = NULL;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001791 if (new_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001792 drm_crtc_vblank_put(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001793 }
1794 }
1795
1796 drm_atomic_helper_commit_hw_done(state);
1797 drm_atomic_helper_cleanup_planes(dev, state);
1798 drm_atomic_helper_commit_cleanup_done(state);
1799 drm_atomic_state_put(state);
1800}
1801
1802static void
1803nv50_disp_atomic_commit_work(struct work_struct *work)
1804{
1805 struct drm_atomic_state *state =
1806 container_of(work, typeof(*state), commit_work);
1807 nv50_disp_atomic_commit_tail(state);
1808}
1809
1810static int
1811nv50_disp_atomic_commit(struct drm_device *dev,
1812 struct drm_atomic_state *state, bool nonblock)
1813{
1814 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsd324c5b2017-11-01 09:12:25 +10001815 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001816 struct drm_plane *plane;
1817 struct drm_crtc *crtc;
1818 bool active = false;
1819 int ret, i;
1820
1821 ret = pm_runtime_get_sync(dev->dev);
1822 if (ret < 0 && ret != -EACCES)
1823 return ret;
1824
1825 ret = drm_atomic_helper_setup_commit(state, nonblock);
1826 if (ret)
1827 goto done;
1828
1829 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
1830
1831 ret = drm_atomic_helper_prepare_planes(dev, state);
1832 if (ret)
1833 goto done;
1834
1835 if (!nonblock) {
1836 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1837 if (ret)
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001838 goto err_cleanup;
Ben Skeggs839ca902016-11-04 17:20:36 +10001839 }
1840
Maarten Lankhorst85726362017-07-11 16:33:05 +02001841 ret = drm_atomic_helper_swap_state(state, true);
1842 if (ret)
1843 goto err_cleanup;
1844
Ben Skeggsd324c5b2017-11-01 09:12:25 +10001845 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1846 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001847 struct nv50_wndw *wndw = nv50_wndw(plane);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001848
Ben Skeggsccd27db2018-05-08 20:39:47 +10001849 if (asyw->set.image)
1850 nv50_wndw_ntfy_enable(wndw, asyw);
Ben Skeggs839ca902016-11-04 17:20:36 +10001851 }
1852
Ben Skeggs839ca902016-11-04 17:20:36 +10001853 drm_atomic_state_get(state);
1854
1855 if (nonblock)
1856 queue_work(system_unbound_wq, &state->commit_work);
1857 else
1858 nv50_disp_atomic_commit_tail(state);
1859
1860 drm_for_each_crtc(crtc, dev) {
1861 if (crtc->state->enable) {
1862 if (!drm->have_disp_power_ref) {
1863 drm->have_disp_power_ref = true;
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001864 return 0;
Ben Skeggs839ca902016-11-04 17:20:36 +10001865 }
1866 active = true;
1867 break;
1868 }
1869 }
1870
1871 if (!active && drm->have_disp_power_ref) {
1872 pm_runtime_put_autosuspend(dev->dev);
1873 drm->have_disp_power_ref = false;
1874 }
1875
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001876err_cleanup:
1877 if (ret)
1878 drm_atomic_helper_cleanup_planes(dev, state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001879done:
1880 pm_runtime_put_autosuspend(dev->dev);
1881 return ret;
1882}
1883
1884static struct nv50_outp_atom *
1885nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
1886{
1887 struct nv50_outp_atom *outp;
1888
1889 list_for_each_entry(outp, &atom->outp, head) {
1890 if (outp->encoder == encoder)
1891 return outp;
1892 }
1893
1894 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
1895 if (!outp)
1896 return ERR_PTR(-ENOMEM);
1897
1898 list_add(&outp->head, &atom->outp);
1899 outp->encoder = encoder;
1900 return outp;
1901}
1902
1903static int
1904nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001905 struct drm_connector_state *old_connector_state)
Ben Skeggs839ca902016-11-04 17:20:36 +10001906{
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001907 struct drm_encoder *encoder = old_connector_state->best_encoder;
1908 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001909 struct drm_crtc *crtc;
1910 struct nv50_outp_atom *outp;
1911
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001912 if (!(crtc = old_connector_state->crtc))
Ben Skeggs839ca902016-11-04 17:20:36 +10001913 return 0;
1914
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001915 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
1916 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1917 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001918 outp = nv50_disp_outp_atomic_add(atom, encoder);
1919 if (IS_ERR(outp))
1920 return PTR_ERR(outp);
1921
1922 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1923 outp->flush_disable = true;
1924 atom->flush_disable = true;
1925 }
1926 outp->clr.ctrl = true;
1927 atom->lock_core = true;
1928 }
1929
1930 return 0;
1931}
1932
1933static int
1934nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
1935 struct drm_connector_state *connector_state)
1936{
1937 struct drm_encoder *encoder = connector_state->best_encoder;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001938 struct drm_crtc_state *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001939 struct drm_crtc *crtc;
1940 struct nv50_outp_atom *outp;
1941
1942 if (!(crtc = connector_state->crtc))
1943 return 0;
1944
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001945 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1946 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001947 outp = nv50_disp_outp_atomic_add(atom, encoder);
1948 if (IS_ERR(outp))
1949 return PTR_ERR(outp);
1950
1951 outp->set.ctrl = true;
1952 atom->lock_core = true;
1953 }
1954
1955 return 0;
1956}
1957
1958static int
1959nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
1960{
1961 struct nv50_atom *atom = nv50_atom(state);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001962 struct drm_connector_state *old_connector_state, *new_connector_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001963 struct drm_connector *connector;
1964 int ret, i;
1965
1966 ret = drm_atomic_helper_check(dev, state);
1967 if (ret)
1968 return ret;
1969
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001970 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
1971 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001972 if (ret)
1973 return ret;
1974
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001975 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001976 if (ret)
1977 return ret;
1978 }
1979
1980 return 0;
1981}
1982
1983static void
1984nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
1985{
1986 struct nv50_atom *atom = nv50_atom(state);
1987 struct nv50_outp_atom *outp, *outt;
1988
1989 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
1990 list_del(&outp->head);
1991 kfree(outp);
1992 }
1993
1994 drm_atomic_state_default_clear(state);
1995}
1996
1997static void
1998nv50_disp_atomic_state_free(struct drm_atomic_state *state)
1999{
2000 struct nv50_atom *atom = nv50_atom(state);
2001 drm_atomic_state_default_release(&atom->state);
2002 kfree(atom);
2003}
2004
2005static struct drm_atomic_state *
2006nv50_disp_atomic_state_alloc(struct drm_device *dev)
2007{
2008 struct nv50_atom *atom;
2009 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2010 drm_atomic_state_init(dev, &atom->state) < 0) {
2011 kfree(atom);
2012 return NULL;
2013 }
2014 INIT_LIST_HEAD(&atom->outp);
2015 return &atom->state;
2016}
2017
2018static const struct drm_mode_config_funcs
2019nv50_disp_func = {
2020 .fb_create = nouveau_user_framebuffer_create,
Noralf Trønnesd0f54f52017-12-05 19:25:00 +01002021 .output_poll_changed = drm_fb_helper_output_poll_changed,
Ben Skeggs839ca902016-11-04 17:20:36 +10002022 .atomic_check = nv50_disp_atomic_check,
2023 .atomic_commit = nv50_disp_atomic_commit,
2024 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2025 .atomic_state_clear = nv50_disp_atomic_state_clear,
2026 .atomic_state_free = nv50_disp_atomic_state_free,
2027};
2028
2029/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002030 * Init
2031 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002032
Ben Skeggs2a44e492011-11-09 11:36:33 +10002033void
Ben Skeggse225f442012-11-21 14:40:21 +10002034nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002035{
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002036 struct nouveau_encoder *nv_encoder;
2037 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002038 struct drm_plane *plane;
2039
2040 drm_for_each_plane(plane, dev) {
2041 struct nv50_wndw *wndw = nv50_wndw(plane);
2042 if (plane->funcs != &nv50_wndw)
2043 continue;
2044 nv50_wndw_fini(wndw);
2045 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002046
2047 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2048 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2049 nv_encoder = nouveau_encoder(encoder);
2050 nv50_mstm_fini(nv_encoder->dp.mstm);
2051 }
2052 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002053}
2054
2055int
Ben Skeggse225f442012-11-21 14:40:21 +10002056nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002057{
Ben Skeggs09e1b782018-05-08 20:39:47 +10002058 struct nv50_core *core = nv50_disp(dev)->core;
Ben Skeggs354d3502016-11-04 17:20:36 +10002059 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002060 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002061
Ben Skeggs09e1b782018-05-08 20:39:47 +10002062 core->func->init(core);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002063
Ben Skeggs354d3502016-11-04 17:20:36 +10002064 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2065 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
Ben Skeggs9c5753b2017-05-19 23:59:35 +10002066 struct nouveau_encoder *nv_encoder =
2067 nouveau_encoder(encoder);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002068 nv50_mstm_init(nv_encoder->dp.mstm);
Ben Skeggs354d3502016-11-04 17:20:36 +10002069 }
2070 }
2071
Ben Skeggs973f10c2016-11-04 17:20:36 +10002072 drm_for_each_plane(plane, dev) {
2073 struct nv50_wndw *wndw = nv50_wndw(plane);
2074 if (plane->funcs != &nv50_wndw)
2075 continue;
2076 nv50_wndw_init(wndw);
2077 }
2078
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002079 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002080}
2081
2082void
Ben Skeggse225f442012-11-21 14:40:21 +10002083nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002084{
Ben Skeggse225f442012-11-21 14:40:21 +10002085 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002086
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10002087 nv50_core_del(&disp->core);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002088
Ben Skeggs816af2f2011-11-16 15:48:48 +10002089 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002090 if (disp->sync)
2091 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002092 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002093
Ben Skeggs77145f12012-07-31 16:16:21 +10002094 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002095 kfree(disp);
2096}
2097
Ben Skeggs839ca902016-11-04 17:20:36 +10002098MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
2099static int nouveau_atomic = 0;
2100module_param_named(atomic, nouveau_atomic, int, 0400);
2101
Ben Skeggs26f6d882011-07-04 16:25:18 +10002102int
Ben Skeggse225f442012-11-21 14:40:21 +10002103nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002104{
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002105 struct nvif_device *device = &nouveau_drm(dev)->client.device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002106 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002107 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002108 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002109 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002110 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002111 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002112
2113 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2114 if (!disp)
2115 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10002116
Ben Skeggs839ca902016-11-04 17:20:36 +10002117 mutex_init(&disp->mutex);
2118
Ben Skeggs77145f12012-07-31 16:16:21 +10002119 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002120 nouveau_display(dev)->dtor = nv50_display_destroy;
2121 nouveau_display(dev)->init = nv50_display_init;
2122 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002123 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs839ca902016-11-04 17:20:36 +10002124 dev->mode_config.funcs = &nv50_disp_func;
Ilia Mirkinc20bb152018-02-03 14:11:23 -05002125 dev->driver->driver_features |= DRIVER_PREFER_XBGR_30BPP;
Ben Skeggs839ca902016-11-04 17:20:36 +10002126 if (nouveau_atomic)
2127 dev->driver->driver_features |= DRIVER_ATOMIC;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002128
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002129 /* small shared memory area we use for notifiers and semaphores */
Ben Skeggsbab7cc12016-05-24 17:26:48 +10002130 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002131 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002132 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002133 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002134 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002135 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002136 if (ret)
2137 nouveau_bo_unpin(disp->sync);
2138 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002139 if (ret)
2140 nouveau_bo_ref(NULL, &disp->sync);
2141 }
2142
2143 if (ret)
2144 goto out;
2145
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002146 /* allocate master evo channel */
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10002147 ret = nv50_core_new(drm, &disp->core);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002148 if (ret)
2149 goto out;
2150
Ben Skeggs438d99e2011-07-05 16:48:06 +10002151 /* create crtc objects to represent the hw heads */
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10002152 if (disp->disp->object.oclass >= GF110_DISP)
Ilia Mirkineba5e562017-07-03 13:06:26 -04002153 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
Ben Skeggs63718a02012-11-16 11:44:14 +10002154 else
Ilia Mirkineba5e562017-07-03 13:06:26 -04002155 crtcs = 0x3;
Ben Skeggs63718a02012-11-16 11:44:14 +10002156
Ilia Mirkineba5e562017-07-03 13:06:26 -04002157 for (i = 0; i < fls(crtcs); i++) {
2158 if (!(crtcs & (1 << i)))
2159 continue;
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002160 ret = nv50_head_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002161 if (ret)
2162 goto out;
2163 }
2164
Ben Skeggs83fc0832011-07-05 13:08:40 +10002165 /* create encoder/connector objects based on VBIOS DCB table */
2166 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2167 connector = nouveau_connector_create(dev, dcbe->connector);
2168 if (IS_ERR(connector))
2169 continue;
2170
Ben Skeggseb6313a2013-02-11 09:52:58 +10002171 if (dcbe->location == DCB_LOC_ON_CHIP) {
2172 switch (dcbe->type) {
2173 case DCB_OUTPUT_TMDS:
2174 case DCB_OUTPUT_LVDS:
2175 case DCB_OUTPUT_DP:
2176 ret = nv50_sor_create(connector, dcbe);
2177 break;
2178 case DCB_OUTPUT_ANALOG:
2179 ret = nv50_dac_create(connector, dcbe);
2180 break;
2181 default:
2182 ret = -ENODEV;
2183 break;
2184 }
2185 } else {
2186 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002187 }
2188
Ben Skeggseb6313a2013-02-11 09:52:58 +10002189 if (ret) {
2190 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2191 dcbe->location, dcbe->type,
2192 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002193 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002194 }
2195 }
2196
2197 /* cull any connectors we created that don't have an encoder */
2198 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2199 if (connector->encoder_ids[0])
2200 continue;
2201
Ben Skeggs77145f12012-07-31 16:16:21 +10002202 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002203 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002204 connector->funcs->destroy(connector);
2205 }
2206
Ben Skeggs26f6d882011-07-04 16:25:18 +10002207out:
2208 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002209 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002210 return ret;
2211}