blob: 475456c19b765d52b75e20ed1e51f2f6a0f20fa3 [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 Skeggs34508f92018-05-08 20:39:47 +100055#include <subdev/bios/dp.h>
56
Ben Skeggsb5a794b2012-10-16 14:18:32 +100057/******************************************************************************
Ben Skeggs3dbd0362016-11-04 17:20:36 +100058 * Atomic state
59 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +100060
61struct nv50_outp_atom {
62 struct list_head head;
63
64 struct drm_encoder *encoder;
65 bool flush_disable;
66
Ben Skeggsf88bc9d32018-05-08 20:39:47 +100067 union nv50_outp_atom_mask {
Ben Skeggs839ca902016-11-04 17:20:36 +100068 struct {
69 bool ctrl:1;
70 };
71 u8 mask;
Ben Skeggsf88bc9d32018-05-08 20:39:47 +100072 } set, clr;
Ben Skeggs839ca902016-11-04 17:20:36 +100073};
74
Ben Skeggs3dbd0362016-11-04 17:20:36 +100075/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +100076 * EVO channel
77 *****************************************************************************/
78
Ben Skeggsb5a794b2012-10-16 14:18:32 +100079static int
Ben Skeggsa01ca782015-08-20 14:54:15 +100080nv50_chan_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +100081 const s32 *oclass, u8 head, void *data, u32 size,
Ben Skeggsa01ca782015-08-20 14:54:15 +100082 struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +100083{
Ben Skeggs41a63402015-08-20 14:54:16 +100084 struct nvif_sclass *sclass;
85 int ret, i, n;
Ben Skeggs6af52892014-11-03 15:01:33 +100086
Ben Skeggsa01ca782015-08-20 14:54:15 +100087 chan->device = device;
88
Ben Skeggs41a63402015-08-20 14:54:16 +100089 ret = n = nvif_object_sclass_get(disp, &sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +100090 if (ret < 0)
91 return ret;
92
Ben Skeggs410f3ec2014-08-10 04:10:25 +100093 while (oclass[0]) {
Ben Skeggs41a63402015-08-20 14:54:16 +100094 for (i = 0; i < n; i++) {
95 if (sclass[i].oclass == oclass[0]) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +100096 ret = nvif_object_init(disp, 0, oclass[0],
Ben Skeggsa01ca782015-08-20 14:54:15 +100097 data, size, &chan->user);
Ben Skeggs6af52892014-11-03 15:01:33 +100098 if (ret == 0)
Ben Skeggs01326052017-11-01 03:56:19 +100099 nvif_object_map(&chan->user, NULL, 0);
Ben Skeggs41a63402015-08-20 14:54:16 +1000100 nvif_object_sclass_put(&sclass);
Ben Skeggs6af52892014-11-03 15:01:33 +1000101 return ret;
102 }
Ben Skeggsb76f1522014-08-10 04:10:28 +1000103 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000104 oclass++;
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000105 }
Ben Skeggs6af52892014-11-03 15:01:33 +1000106
Ben Skeggs41a63402015-08-20 14:54:16 +1000107 nvif_object_sclass_put(&sclass);
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000108 return -ENOSYS;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000109}
110
111static void
Ben Skeggs0ad72862014-08-10 04:10:22 +1000112nv50_chan_destroy(struct nv50_chan *chan)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000113{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000114 nvif_object_fini(&chan->user);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000115}
116
117/******************************************************************************
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000118 * DMA EVO channel
119 *****************************************************************************/
120
Ben Skeggs15907002018-05-08 20:39:47 +1000121void
Ben Skeggsf5650472018-05-08 20:39:46 +1000122nv50_dmac_destroy(struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000123{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000124 nvif_object_fini(&dmac->vram);
125 nvif_object_fini(&dmac->sync);
126
127 nv50_chan_destroy(&dmac->base);
128
Ben Skeggsf5650472018-05-08 20:39:46 +1000129 nvif_mem_fini(&dmac->push);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000130}
131
Ben Skeggs15907002018-05-08 20:39:47 +1000132int
Ben Skeggsa01ca782015-08-20 14:54:15 +1000133nv50_dmac_create(struct nvif_device *device, struct nvif_object *disp,
Ben Skeggs315a8b22015-08-20 14:54:16 +1000134 const s32 *oclass, u8 head, void *data, u32 size, u64 syncbuf,
Ben Skeggse225f442012-11-21 14:40:21 +1000135 struct nv50_dmac *dmac)
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000136{
Ben Skeggsf5650472018-05-08 20:39:46 +1000137 struct nouveau_cli *cli = (void *)device->object.client;
Ben Skeggs648d4df2014-08-10 04:10:27 +1000138 struct nv50_disp_core_channel_dma_v0 *args = data;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000139 int ret;
140
Daniel Vetter59ad1462012-12-02 14:49:44 +0100141 mutex_init(&dmac->lock);
142
Ben Skeggsf5650472018-05-08 20:39:46 +1000143 ret = nvif_mem_init_map(&cli->mmu, NVIF_MEM_COHERENT, 0x1000,
144 &dmac->push);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000145 if (ret)
146 return ret;
147
Ben Skeggsf5650472018-05-08 20:39:46 +1000148 dmac->ptr = dmac->push.object.map.ptr;
149
150 args->pushbuf = nvif_handle(&dmac->push.object);
Ben Skeggsbf81df92015-08-20 14:54:16 +1000151
Ben Skeggsa01ca782015-08-20 14:54:15 +1000152 ret = nv50_chan_create(device, disp, oclass, head, data, size,
153 &dmac->base);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000154 if (ret)
155 return ret;
156
Ben Skeggsfacaed62018-05-08 20:39:48 +1000157 if (!syncbuf)
158 return 0;
159
Ben Skeggsa01ca782015-08-20 14:54:15 +1000160 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000161 &(struct nv_dma_v0) {
162 .target = NV_DMA_V0_TARGET_VRAM,
163 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000164 .start = syncbuf + 0x0000,
165 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000166 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000167 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000168 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000169 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000170
Ben Skeggsa01ca782015-08-20 14:54:15 +1000171 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000172 &(struct nv_dma_v0) {
173 .target = NV_DMA_V0_TARGET_VRAM,
174 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000175 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000176 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000177 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000178 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000179 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000180 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000181
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000182 return ret;
183}
184
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000185/******************************************************************************
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000186 * EVO channel helpers
187 *****************************************************************************/
Ben Skeggs15907002018-05-08 20:39:47 +1000188u32 *
189evo_wait(struct nv50_dmac *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000190{
Ben Skeggse225f442012-11-21 14:40:21 +1000191 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000192 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000193 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000194
Daniel Vetter59ad1462012-12-02 14:49:44 +0100195 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000196 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000197 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000198
Ben Skeggs0ad72862014-08-10 04:10:22 +1000199 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000200 if (nvif_msec(device, 2000,
201 if (!nvif_rd32(&dmac->base.user, 0x0004))
202 break;
203 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100204 mutex_unlock(&dmac->lock);
Joe Perches8dfe1622017-02-28 04:55:54 -0800205 pr_err("nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000206 return NULL;
207 }
208
209 put = 0;
210 }
211
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000212 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000213}
214
Ben Skeggs15907002018-05-08 20:39:47 +1000215void
216evo_kick(u32 *push, struct nv50_dmac *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000217{
Ben Skeggse225f442012-11-21 14:40:21 +1000218 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000219 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100220 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000221}
222
Ben Skeggs438d99e2011-07-05 16:48:06 +1000223/******************************************************************************
Ben Skeggsd92c8ad2016-11-04 17:20:36 +1000224 * Output path helpers
Ben Skeggsa91d3222014-12-22 16:30:13 +1000225 *****************************************************************************/
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000226static void
227nv50_outp_release(struct nouveau_encoder *nv_encoder)
228{
229 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
230 struct {
231 struct nv50_disp_mthd_v1 base;
232 } args = {
233 .base.version = 1,
234 .base.method = NV50_DISP_MTHD_V1_RELEASE,
235 .base.hasht = nv_encoder->dcb->hasht,
236 .base.hashm = nv_encoder->dcb->hashm,
237 };
238
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000239 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000240 nv_encoder->or = -1;
241 nv_encoder->link = 0;
242}
243
244static int
245nv50_outp_acquire(struct nouveau_encoder *nv_encoder)
246{
247 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
248 struct nv50_disp *disp = nv50_disp(drm->dev);
249 struct {
250 struct nv50_disp_mthd_v1 base;
251 struct nv50_disp_acquire_v0 info;
252 } args = {
253 .base.version = 1,
254 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
255 .base.hasht = nv_encoder->dcb->hasht,
256 .base.hashm = nv_encoder->dcb->hashm,
257 };
258 int ret;
259
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000260 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000261 if (ret) {
262 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
263 return ret;
264 }
265
266 nv_encoder->or = args.info.or;
267 nv_encoder->link = args.info.link;
268 return 0;
269}
270
Ben Skeggsd92c8ad2016-11-04 17:20:36 +1000271static int
272nv50_outp_atomic_check_view(struct drm_encoder *encoder,
273 struct drm_crtc_state *crtc_state,
274 struct drm_connector_state *conn_state,
275 struct drm_display_mode *native_mode)
276{
277 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
278 struct drm_display_mode *mode = &crtc_state->mode;
279 struct drm_connector *connector = conn_state->connector;
280 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
281 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
282
283 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
284 asyc->scaler.full = false;
285 if (!native_mode)
286 return 0;
287
288 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
289 switch (connector->connector_type) {
290 case DRM_MODE_CONNECTOR_LVDS:
291 case DRM_MODE_CONNECTOR_eDP:
292 /* Force use of scaler for non-EDID modes. */
293 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
294 break;
295 mode = native_mode;
296 asyc->scaler.full = true;
297 break;
298 default:
299 break;
300 }
301 } else {
302 mode = native_mode;
303 }
304
305 if (!drm_mode_equal(adjusted_mode, mode)) {
306 drm_mode_copy(adjusted_mode, mode);
307 crtc_state->mode_changed = true;
308 }
309
310 return 0;
311}
312
Ben Skeggs839ca902016-11-04 17:20:36 +1000313static int
314nv50_outp_atomic_check(struct drm_encoder *encoder,
315 struct drm_crtc_state *crtc_state,
316 struct drm_connector_state *conn_state)
Ben Skeggsa91d3222014-12-22 16:30:13 +1000317{
Ben Skeggs839ca902016-11-04 17:20:36 +1000318 struct nouveau_connector *nv_connector =
319 nouveau_connector(conn_state->connector);
320 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
321 nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +1000322}
323
324/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000325 * DAC
326 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000327static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000328nv50_dac_disable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000329{
Ben Skeggsf20c6652016-11-04 17:20:36 +1000330 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggs0a368772018-05-08 20:39:47 +1000331 struct nv50_core *core = nv50_disp(encoder->dev)->core;
332 if (nv_encoder->crtc)
333 core->func->dac->ctrl(core, nv_encoder->or, 0x00000000, NULL);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000334 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000335 nv50_outp_release(nv_encoder);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000336}
337
338static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000339nv50_dac_enable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000340{
341 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
342 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000343 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
Ben Skeggs0a368772018-05-08 20:39:47 +1000344 struct nv50_core *core = nv50_disp(encoder->dev)->core;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000345
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000346 nv50_outp_acquire(nv_encoder);
347
Ben Skeggs0a368772018-05-08 20:39:47 +1000348 core->func->dac->ctrl(core, nv_encoder->or, 1 << nv_crtc->index, asyh);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000349 asyh->or.depth = 0;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000350
351 nv_encoder->crtc = encoder->crtc;
352}
353
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000354static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +1000355nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000356{
Ben Skeggsc4abd312014-08-10 04:10:26 +1000357 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +1000358 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +1000359 struct {
360 struct nv50_disp_mthd_v1 base;
361 struct nv50_disp_dac_load_v0 load;
362 } args = {
363 .base.version = 1,
364 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
365 .base.hasht = nv_encoder->dcb->hasht,
366 .base.hashm = nv_encoder->dcb->hashm,
367 };
368 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +1000369
Ben Skeggsc4abd312014-08-10 04:10:26 +1000370 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
371 if (args.load.data == 0)
372 args.load.data = 340;
373
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000374 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggsc4abd312014-08-10 04:10:26 +1000375 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +1000376 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +1000377
Ben Skeggs35b21d32012-11-08 12:08:55 +1000378 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000379}
380
Ben Skeggsf20c6652016-11-04 17:20:36 +1000381static const struct drm_encoder_helper_funcs
382nv50_dac_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +1000383 .atomic_check = nv50_outp_atomic_check,
384 .enable = nv50_dac_enable,
385 .disable = nv50_dac_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +1000386 .detect = nv50_dac_detect
387};
388
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000389static void
Ben Skeggse225f442012-11-21 14:40:21 +1000390nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000391{
392 drm_encoder_cleanup(encoder);
393 kfree(encoder);
394}
395
Ben Skeggsf20c6652016-11-04 17:20:36 +1000396static const struct drm_encoder_funcs
397nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +1000398 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000399};
400
401static int
Ben Skeggse225f442012-11-21 14:40:21 +1000402nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000403{
Ben Skeggs5ed50202013-02-11 20:15:03 +1000404 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000405 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +1000406 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000407 struct nouveau_encoder *nv_encoder;
408 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +1000409 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000410
411 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
412 if (!nv_encoder)
413 return -ENOMEM;
414 nv_encoder->dcb = dcbe;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +1000415
416 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
417 if (bus)
418 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000419
420 encoder = to_drm_encoder(nv_encoder);
421 encoder->possible_crtcs = dcbe->heads;
422 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +1000423 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
424 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000425 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000426
Daniel Vettercde4c442018-07-09 10:40:07 +0200427 drm_connector_attach_encoder(connector, encoder);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000428 return 0;
429}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000430
431/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +1000432 * Audio
433 *****************************************************************************/
434static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000435nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
436{
437 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
438 struct nv50_disp *disp = nv50_disp(encoder->dev);
439 struct {
440 struct nv50_disp_mthd_v1 base;
441 struct nv50_disp_sor_hda_eld_v0 eld;
442 } args = {
443 .base.version = 1,
444 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
445 .base.hasht = nv_encoder->dcb->hasht,
446 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
447 (0x0100 << nv_crtc->index),
448 };
449
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000450 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggsf20c6652016-11-04 17:20:36 +1000451}
452
453static void
454nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +1000455{
456 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +1000457 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +1000458 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +1000459 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +1000460 struct __packed {
461 struct {
462 struct nv50_disp_mthd_v1 mthd;
463 struct nv50_disp_sor_hda_eld_v0 eld;
464 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +1000465 u8 data[sizeof(nv_connector->base.eld)];
466 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +1000467 .base.mthd.version = 1,
468 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
469 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +1000470 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
471 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +1000472 };
Ben Skeggs78951d22011-11-11 18:13:13 +1000473
474 nv_connector = nouveau_encoder_connector_get(nv_encoder);
475 if (!drm_detect_monitor_audio(nv_connector->edid))
476 return;
477
Ben Skeggs120b0c32014-08-10 04:10:26 +1000478 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +1000479
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000480 nvif_mthd(&disp->disp->object, 0, &args,
Jani Nikula938fd8a2014-10-28 16:20:48 +0200481 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +1000482}
483
Ben Skeggsf20c6652016-11-04 17:20:36 +1000484/******************************************************************************
485 * HDMI
486 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +1000487static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000488nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +1000489{
490 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +1000491 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +1000492 struct {
493 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +1000494 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +1000495 } args = {
496 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +1000497 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
498 .base.hasht = nv_encoder->dcb->hasht,
499 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
500 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +1000501 };
Ben Skeggs78951d22011-11-11 18:13:13 +1000502
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000503 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +1000504}
505
Ben Skeggs78951d22011-11-11 18:13:13 +1000506static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000507nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +1000508{
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000509 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
510 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000511 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +1000512 struct {
513 struct nv50_disp_mthd_v1 base;
514 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400515 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
Ben Skeggse00f2232014-08-10 04:10:26 +1000516 } args = {
517 .base.version = 1,
518 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
519 .base.hasht = nv_encoder->dcb->hasht,
520 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
521 (0x0100 << nv_crtc->index),
522 .pwr.state = 1,
523 .pwr.rekey = 56, /* binary driver, and tegra, constant */
524 };
525 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000526 u32 max_ac_packet;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400527 union hdmi_infoframe avi_frame;
528 union hdmi_infoframe vendor_frame;
529 int ret;
530 int size;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000531
532 nv_connector = nouveau_encoder_connector_get(nv_encoder);
533 if (!drm_detect_hdmi_monitor(nv_connector->edid))
534 return;
535
Shashank Sharma0c1f5282017-07-13 21:03:07 +0530536 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi, mode,
537 false);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400538 if (!ret) {
539 /* We have an AVI InfoFrame, populate it to the display */
540 args.pwr.avi_infoframe_length
541 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
542 }
543
Ville Syrjäläf1781e92017-11-13 19:04:19 +0200544 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
545 &nv_connector->base, mode);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400546 if (!ret) {
547 /* We have a Vendor InfoFrame, populate it to the display */
548 args.pwr.vendor_infoframe_length
549 = hdmi_infoframe_pack(&vendor_frame,
550 args.infoframes
551 + args.pwr.avi_infoframe_length,
552 17);
553 }
554
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000555 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +1000556 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000557 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +1000558 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000559
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400560 size = sizeof(args.base)
561 + sizeof(args.pwr)
562 + args.pwr.avi_infoframe_length
563 + args.pwr.vendor_infoframe_length;
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000564 nvif_mthd(&disp->disp->object, 0, &args, size);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000565 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +1000566}
567
568/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000569 * MST
570 *****************************************************************************/
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000571#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
572#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
573#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
574
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000575struct nv50_mstm {
576 struct nouveau_encoder *outp;
577
578 struct drm_dp_mst_topology_mgr mgr;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000579 struct nv50_msto *msto[4];
580
581 bool modified;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000582 bool disabled;
583 int links;
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000584};
585
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000586struct nv50_mstc {
587 struct nv50_mstm *mstm;
588 struct drm_dp_mst_port *port;
589 struct drm_connector connector;
590
591 struct drm_display_mode *native;
592 struct edid *edid;
593
594 int pbn;
595};
596
597struct nv50_msto {
598 struct drm_encoder encoder;
599
600 struct nv50_head *head;
601 struct nv50_mstc *mstc;
602 bool disabled;
603};
604
605static struct drm_dp_payload *
606nv50_msto_payload(struct nv50_msto *msto)
607{
608 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
609 struct nv50_mstc *mstc = msto->mstc;
610 struct nv50_mstm *mstm = mstc->mstm;
611 int vcpi = mstc->port->vcpi.vcpi, i;
612
613 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
614 for (i = 0; i < mstm->mgr.max_payloads; i++) {
615 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
616 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
617 mstm->outp->base.base.name, i, payload->vcpi,
618 payload->start_slot, payload->num_slots);
619 }
620
621 for (i = 0; i < mstm->mgr.max_payloads; i++) {
622 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
623 if (payload->vcpi == vcpi)
624 return payload;
625 }
626
627 return NULL;
628}
629
630static void
631nv50_msto_cleanup(struct nv50_msto *msto)
632{
633 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
634 struct nv50_mstc *mstc = msto->mstc;
635 struct nv50_mstm *mstm = mstc->mstm;
636
637 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
638 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
639 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
640 if (msto->disabled) {
641 msto->mstc = NULL;
642 msto->head = NULL;
643 msto->disabled = false;
644 }
645}
646
647static void
648nv50_msto_prepare(struct nv50_msto *msto)
649{
650 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
651 struct nv50_mstc *mstc = msto->mstc;
652 struct nv50_mstm *mstm = mstc->mstm;
653 struct {
654 struct nv50_disp_mthd_v1 base;
655 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
656 } args = {
657 .base.version = 1,
658 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
659 .base.hasht = mstm->outp->dcb->hasht,
660 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
661 (0x0100 << msto->head->base.index),
662 };
663
664 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
665 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
666 struct drm_dp_payload *payload = nv50_msto_payload(msto);
667 if (payload) {
668 args.vcpi.start_slot = payload->start_slot;
669 args.vcpi.num_slots = payload->num_slots;
670 args.vcpi.pbn = mstc->port->vcpi.pbn;
671 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
672 }
673 }
674
675 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
676 msto->encoder.name, msto->head->base.base.name,
677 args.vcpi.start_slot, args.vcpi.num_slots,
678 args.vcpi.pbn, args.vcpi.aligned_pbn);
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000679 nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000680}
681
682static int
683nv50_msto_atomic_check(struct drm_encoder *encoder,
684 struct drm_crtc_state *crtc_state,
685 struct drm_connector_state *conn_state)
686{
687 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
688 struct nv50_mstm *mstm = mstc->mstm;
689 int bpp = conn_state->connector->display_info.bpc * 3;
690 int slots;
691
692 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
693
694 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
695 if (slots < 0)
696 return slots;
697
698 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
699 mstc->native);
700}
701
702static void
703nv50_msto_enable(struct drm_encoder *encoder)
704{
705 struct nv50_head *head = nv50_head(encoder->crtc);
706 struct nv50_msto *msto = nv50_msto(encoder);
707 struct nv50_mstc *mstc = NULL;
708 struct nv50_mstm *mstm = NULL;
709 struct drm_connector *connector;
Gustavo Padovan875dd622017-05-11 16:10:46 -0300710 struct drm_connector_list_iter conn_iter;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000711 u8 proto, depth;
712 int slots;
713 bool r;
714
Gustavo Padovan875dd622017-05-11 16:10:46 -0300715 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
716 drm_for_each_connector_iter(connector, &conn_iter) {
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000717 if (connector->state->best_encoder == &msto->encoder) {
718 mstc = nv50_mstc(connector);
719 mstm = mstc->mstm;
720 break;
721 }
722 }
Gustavo Padovan875dd622017-05-11 16:10:46 -0300723 drm_connector_list_iter_end(&conn_iter);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000724
725 if (WARN_ON(!mstc))
726 return;
727
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -0700728 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
729 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000730 WARN_ON(!r);
731
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000732 if (!mstm->links++)
733 nv50_outp_acquire(mstm->outp);
734
735 if (mstm->outp->link & 1)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000736 proto = 0x8;
737 else
738 proto = 0x9;
739
740 switch (mstc->connector.display_info.bpc) {
741 case 6: depth = 0x2; break;
742 case 8: depth = 0x5; break;
743 case 10:
744 default: depth = 0x6; break;
745 }
746
747 mstm->outp->update(mstm->outp, head->base.index,
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000748 nv50_head_atom(head->base.base.state), proto, depth);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000749
750 msto->head = head;
751 msto->mstc = mstc;
752 mstm->modified = true;
753}
754
755static void
756nv50_msto_disable(struct drm_encoder *encoder)
757{
758 struct nv50_msto *msto = nv50_msto(encoder);
759 struct nv50_mstc *mstc = msto->mstc;
760 struct nv50_mstm *mstm = mstc->mstm;
761
762 if (mstc->port)
763 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
764
765 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
766 mstm->modified = true;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000767 if (!--mstm->links)
768 mstm->disabled = true;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000769 msto->disabled = true;
770}
771
772static const struct drm_encoder_helper_funcs
773nv50_msto_help = {
774 .disable = nv50_msto_disable,
775 .enable = nv50_msto_enable,
776 .atomic_check = nv50_msto_atomic_check,
777};
778
779static void
780nv50_msto_destroy(struct drm_encoder *encoder)
781{
782 struct nv50_msto *msto = nv50_msto(encoder);
783 drm_encoder_cleanup(&msto->encoder);
784 kfree(msto);
785}
786
787static const struct drm_encoder_funcs
788nv50_msto = {
789 .destroy = nv50_msto_destroy,
790};
791
792static int
793nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
794 struct nv50_msto **pmsto)
795{
796 struct nv50_msto *msto;
797 int ret;
798
799 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
800 return -ENOMEM;
801
802 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
803 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
804 if (ret) {
805 kfree(*pmsto);
806 *pmsto = NULL;
807 return ret;
808 }
809
810 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
811 msto->encoder.possible_crtcs = heads;
812 return 0;
813}
814
815static struct drm_encoder *
816nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
817 struct drm_connector_state *connector_state)
818{
819 struct nv50_head *head = nv50_head(connector_state->crtc);
820 struct nv50_mstc *mstc = nv50_mstc(connector);
821 if (mstc->port) {
822 struct nv50_mstm *mstm = mstc->mstm;
823 return &mstm->msto[head->base.index]->encoder;
824 }
825 return NULL;
826}
827
828static struct drm_encoder *
829nv50_mstc_best_encoder(struct drm_connector *connector)
830{
831 struct nv50_mstc *mstc = nv50_mstc(connector);
832 if (mstc->port) {
833 struct nv50_mstm *mstm = mstc->mstm;
834 return &mstm->msto[0]->encoder;
835 }
836 return NULL;
837}
838
839static enum drm_mode_status
840nv50_mstc_mode_valid(struct drm_connector *connector,
841 struct drm_display_mode *mode)
842{
843 return MODE_OK;
844}
845
846static int
847nv50_mstc_get_modes(struct drm_connector *connector)
848{
849 struct nv50_mstc *mstc = nv50_mstc(connector);
850 int ret = 0;
851
852 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
Daniel Vetterc555f022018-07-09 10:40:06 +0200853 drm_connector_update_edid_property(&mstc->connector, mstc->edid);
Jani Nikulad471ed02017-11-01 16:21:02 +0200854 if (mstc->edid)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000855 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000856
857 if (!mstc->connector.display_info.bpc)
858 mstc->connector.display_info.bpc = 8;
859
860 if (mstc->native)
861 drm_mode_destroy(mstc->connector.dev, mstc->native);
862 mstc->native = nouveau_conn_native_mode(&mstc->connector);
863 return ret;
864}
865
866static const struct drm_connector_helper_funcs
867nv50_mstc_help = {
868 .get_modes = nv50_mstc_get_modes,
869 .mode_valid = nv50_mstc_mode_valid,
870 .best_encoder = nv50_mstc_best_encoder,
871 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
872};
873
874static enum drm_connector_status
875nv50_mstc_detect(struct drm_connector *connector, bool force)
876{
877 struct nv50_mstc *mstc = nv50_mstc(connector);
878 if (!mstc->port)
879 return connector_status_disconnected;
880 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
881}
882
883static void
884nv50_mstc_destroy(struct drm_connector *connector)
885{
886 struct nv50_mstc *mstc = nv50_mstc(connector);
887 drm_connector_cleanup(&mstc->connector);
888 kfree(mstc);
889}
890
891static const struct drm_connector_funcs
892nv50_mstc = {
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000893 .reset = nouveau_conn_reset,
894 .detect = nv50_mstc_detect,
895 .fill_modes = drm_helper_probe_single_connector_modes,
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000896 .destroy = nv50_mstc_destroy,
897 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
898 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
899 .atomic_set_property = nouveau_conn_atomic_set_property,
900 .atomic_get_property = nouveau_conn_atomic_get_property,
901};
902
903static int
904nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
905 const char *path, struct nv50_mstc **pmstc)
906{
907 struct drm_device *dev = mstm->outp->base.base.dev;
908 struct nv50_mstc *mstc;
909 int ret, i;
910
911 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
912 return -ENOMEM;
913 mstc->mstm = mstm;
914 mstc->port = port;
915
916 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
917 DRM_MODE_CONNECTOR_DisplayPort);
918 if (ret) {
919 kfree(*pmstc);
920 *pmstc = NULL;
921 return ret;
922 }
923
924 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
925
926 mstc->connector.funcs->reset(&mstc->connector);
927 nouveau_conn_attach_properties(&mstc->connector);
928
Colin Ian King27a451e2017-08-17 23:03:23 +0100929 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto[i]; i++)
Daniel Vettercde4c442018-07-09 10:40:07 +0200930 drm_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000931
932 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
933 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
934 drm_mode_connector_set_path_property(&mstc->connector, path);
935 return 0;
936}
937
938static void
939nv50_mstm_cleanup(struct nv50_mstm *mstm)
940{
941 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
942 struct drm_encoder *encoder;
943 int ret;
944
945 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
946 ret = drm_dp_check_act_status(&mstm->mgr);
947
948 ret = drm_dp_update_payload_part2(&mstm->mgr);
949
950 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
951 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
952 struct nv50_msto *msto = nv50_msto(encoder);
953 struct nv50_mstc *mstc = msto->mstc;
954 if (mstc && mstc->mstm == mstm)
955 nv50_msto_cleanup(msto);
956 }
957 }
958
959 mstm->modified = false;
960}
961
962static void
963nv50_mstm_prepare(struct nv50_mstm *mstm)
964{
965 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
966 struct drm_encoder *encoder;
967 int ret;
968
969 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
970 ret = drm_dp_update_payload_part1(&mstm->mgr);
971
972 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
973 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
974 struct nv50_msto *msto = nv50_msto(encoder);
975 struct nv50_mstc *mstc = msto->mstc;
976 if (mstc && mstc->mstm == mstm)
977 nv50_msto_prepare(msto);
978 }
979 }
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000980
981 if (mstm->disabled) {
982 if (!mstm->links)
983 nv50_outp_release(mstm->outp);
984 mstm->disabled = false;
985 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000986}
987
988static void
989nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
990{
991 struct nv50_mstm *mstm = nv50_mstm(mgr);
992 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
993}
994
995static void
996nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
997 struct drm_connector *connector)
998{
999 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1000 struct nv50_mstc *mstc = nv50_mstc(connector);
1001
1002 drm_connector_unregister(&mstc->connector);
1003
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001004 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
Lyude Paul352672d2018-05-02 19:38:48 -04001005
1006 drm_modeset_lock(&drm->dev->mode_config.connection_mutex, NULL);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001007 mstc->port = NULL;
Lyude Paul352672d2018-05-02 19:38:48 -04001008 drm_modeset_unlock(&drm->dev->mode_config.connection_mutex);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001009
1010 drm_connector_unreference(&mstc->connector);
1011}
1012
1013static void
1014nv50_mstm_register_connector(struct drm_connector *connector)
1015{
1016 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1017
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001018 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001019
1020 drm_connector_register(connector);
1021}
1022
1023static struct drm_connector *
1024nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1025 struct drm_dp_mst_port *port, const char *path)
1026{
1027 struct nv50_mstm *mstm = nv50_mstm(mgr);
1028 struct nv50_mstc *mstc;
1029 int ret;
1030
1031 ret = nv50_mstc_new(mstm, port, path, &mstc);
1032 if (ret) {
1033 if (mstc)
1034 mstc->connector.funcs->destroy(&mstc->connector);
1035 return NULL;
1036 }
1037
1038 return &mstc->connector;
1039}
1040
1041static const struct drm_dp_mst_topology_cbs
1042nv50_mstm = {
1043 .add_connector = nv50_mstm_add_connector,
1044 .register_connector = nv50_mstm_register_connector,
1045 .destroy_connector = nv50_mstm_destroy_connector,
1046 .hotplug = nv50_mstm_hotplug,
1047};
1048
1049void
1050nv50_mstm_service(struct nv50_mstm *mstm)
1051{
Ben Skeggs227f66d2017-10-03 16:24:28 +10001052 struct drm_dp_aux *aux = mstm ? mstm->mgr.aux : NULL;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001053 bool handled = true;
1054 int ret;
1055 u8 esi[8] = {};
1056
Ben Skeggs227f66d2017-10-03 16:24:28 +10001057 if (!aux)
1058 return;
1059
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001060 while (handled) {
1061 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1062 if (ret != 8) {
1063 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1064 return;
1065 }
1066
1067 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1068 if (!handled)
1069 break;
1070
1071 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
1072 }
1073}
1074
1075void
1076nv50_mstm_remove(struct nv50_mstm *mstm)
1077{
1078 if (mstm)
1079 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1080}
1081
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001082static int
1083nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
1084{
1085 struct nouveau_encoder *outp = mstm->outp;
1086 struct {
1087 struct nv50_disp_mthd_v1 base;
1088 struct nv50_disp_sor_dp_mst_link_v0 mst;
1089 } args = {
1090 .base.version = 1,
1091 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
1092 .base.hasht = outp->dcb->hasht,
1093 .base.hashm = outp->dcb->hashm,
1094 .mst.state = state,
1095 };
1096 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001097 struct nvif_object *disp = &drm->display->disp.object;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001098 int ret;
1099
1100 if (dpcd >= 0x12) {
1101 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
1102 if (ret < 0)
1103 return ret;
1104
1105 dpcd &= ~DP_MST_EN;
1106 if (state)
1107 dpcd |= DP_MST_EN;
1108
1109 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
1110 if (ret < 0)
1111 return ret;
1112 }
1113
1114 return nvif_mthd(disp, 0, &args, sizeof(args));
1115}
1116
1117int
1118nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
1119{
1120 int ret, state = 0;
1121
1122 if (!mstm)
1123 return 0;
1124
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10001125 if (dpcd[0] >= 0x12) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001126 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
1127 if (ret < 0)
1128 return ret;
1129
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10001130 if (!(dpcd[1] & DP_MST_CAP))
1131 dpcd[0] = 0x11;
1132 else
1133 state = allow;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001134 }
1135
1136 ret = nv50_mstm_enable(mstm, dpcd[0], state);
1137 if (ret)
1138 return ret;
1139
1140 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
1141 if (ret)
1142 return nv50_mstm_enable(mstm, dpcd[0], 0);
1143
1144 return mstm->mgr.mst_state;
1145}
1146
1147static void
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001148nv50_mstm_fini(struct nv50_mstm *mstm)
1149{
1150 if (mstm && mstm->mgr.mst_state)
1151 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1152}
1153
1154static void
1155nv50_mstm_init(struct nv50_mstm *mstm)
1156{
1157 if (mstm && mstm->mgr.mst_state)
1158 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
1159}
1160
1161static void
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001162nv50_mstm_del(struct nv50_mstm **pmstm)
1163{
1164 struct nv50_mstm *mstm = *pmstm;
1165 if (mstm) {
1166 kfree(*pmstm);
1167 *pmstm = NULL;
1168 }
1169}
1170
1171static int
1172nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1173 int conn_base_id, struct nv50_mstm **pmstm)
1174{
1175 const int max_payloads = hweight8(outp->dcb->heads);
1176 struct drm_device *dev = outp->base.base.dev;
1177 struct nv50_mstm *mstm;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001178 int ret, i;
1179 u8 dpcd;
1180
1181 /* This is a workaround for some monitors not functioning
1182 * correctly in MST mode on initial module load. I think
1183 * some bad interaction with the VBIOS may be responsible.
1184 *
1185 * A good ol' off and on again seems to work here ;)
1186 */
1187 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
1188 if (ret >= 0 && dpcd >= 0x12)
1189 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001190
1191 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1192 return -ENOMEM;
1193 mstm->outp = outp;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001194 mstm->mgr.cbs = &nv50_mstm;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001195
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001196 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001197 max_payloads, conn_base_id);
1198 if (ret)
1199 return ret;
1200
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001201 for (i = 0; i < max_payloads; i++) {
1202 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
1203 i, &mstm->msto[i]);
1204 if (ret)
1205 return ret;
1206 }
1207
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001208 return 0;
1209}
1210
1211/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001212 * SOR
1213 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001214static void
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001215nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001216 struct nv50_head_atom *asyh, u8 proto, u8 depth)
Ben Skeggse84a35a2014-06-05 10:59:55 +10001217{
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10001218 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
Ben Skeggs0a368772018-05-08 20:39:47 +10001219 struct nv50_core *core = disp->core;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001220
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001221 if (!asyh) {
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001222 nv_encoder->ctrl &= ~BIT(head);
1223 if (!(nv_encoder->ctrl & 0x0000000f))
1224 nv_encoder->ctrl = 0;
1225 } else {
1226 nv_encoder->ctrl |= proto << 8;
1227 nv_encoder->ctrl |= BIT(head);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001228 asyh->or.depth = depth;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001229 }
1230
Ben Skeggs0a368772018-05-08 20:39:47 +10001231 core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001232}
1233
1234static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001235nv50_sor_disable(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001236{
1237 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001238 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001239
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001240 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001241
1242 if (nv_crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001243 struct nvkm_i2c_aux *aux = nv_encoder->aux;
1244 u8 pwr;
1245
1246 if (aux) {
1247 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
1248 if (ret == 0) {
1249 pwr &= ~DP_SET_POWER_MASK;
1250 pwr |= DP_SET_POWER_D3;
1251 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
1252 }
1253 }
1254
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001255 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001256 nv50_audio_disable(encoder, nv_crtc);
1257 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001258 nv50_outp_release(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001259 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001260}
1261
1262static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001263nv50_sor_enable(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001264{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001265 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1266 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001267 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1268 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001269 struct {
1270 struct nv50_disp_mthd_v1 base;
1271 struct nv50_disp_sor_lvds_script_v0 lvds;
1272 } lvds = {
1273 .base.version = 1,
1274 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1275 .base.hasht = nv_encoder->dcb->hasht,
1276 .base.hashm = nv_encoder->dcb->hashm,
1277 };
Ben Skeggse225f442012-11-21 14:40:21 +10001278 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001279 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001280 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001281 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001282 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001283 u8 proto = 0xf;
1284 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001285
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001286 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001287 nv_encoder->crtc = encoder->crtc;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001288 nv50_outp_acquire(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001289
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001290 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001291 case DCB_OUTPUT_TMDS:
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001292 if (nv_encoder->link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05001293 proto = 0x1;
1294 /* Only enable dual-link if:
1295 * - Need to (i.e. rate > 165MHz)
1296 * - DCB says we can
1297 * - Not an HDMI monitor, since there's no dual-link
1298 * on HDMI.
1299 */
1300 if (mode->clock >= 165000 &&
1301 nv_encoder->dcb->duallink_possible &&
1302 !drm_detect_hdmi_monitor(nv_connector->edid))
1303 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001304 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001305 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001306 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001307
Ben Skeggsf20c6652016-11-04 17:20:36 +10001308 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001309 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001310 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001311 proto = 0x0;
1312
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001313 if (bios->fp_no_ddc) {
1314 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001315 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001316 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001317 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001318 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001319 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001320 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001321 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001322 } else
1323 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001324 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001325 }
1326
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001327 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001328 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001329 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001330 } else {
1331 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001332 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001333 }
1334
1335 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001336 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001337 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001338
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001339 nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001340 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001341 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10001342 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001343 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001344 else
1345 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001346 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001347 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001348 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001349
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001350 if (nv_encoder->link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001351 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001352 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001353 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001354
1355 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001356 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001357 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10001358 BUG();
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001359 break;
1360 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001361
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001362 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001363}
1364
Ben Skeggsf20c6652016-11-04 17:20:36 +10001365static const struct drm_encoder_helper_funcs
1366nv50_sor_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001367 .atomic_check = nv50_outp_atomic_check,
1368 .enable = nv50_sor_enable,
1369 .disable = nv50_sor_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10001370};
1371
Ben Skeggs83fc0832011-07-05 13:08:40 +10001372static void
Ben Skeggse225f442012-11-21 14:40:21 +10001373nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001374{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001375 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1376 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001377 drm_encoder_cleanup(encoder);
1378 kfree(encoder);
1379}
1380
Ben Skeggsf20c6652016-11-04 17:20:36 +10001381static const struct drm_encoder_funcs
1382nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10001383 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001384};
1385
1386static int
Ben Skeggse225f442012-11-21 14:40:21 +10001387nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001388{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001389 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10001390 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs34508f92018-05-08 20:39:47 +10001391 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001392 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001393 struct nouveau_encoder *nv_encoder;
1394 struct drm_encoder *encoder;
Ben Skeggs34508f92018-05-08 20:39:47 +10001395 u8 ver, hdr, cnt, len;
1396 u32 data;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001397 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001398
1399 switch (dcbe->type) {
1400 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1401 case DCB_OUTPUT_TMDS:
1402 case DCB_OUTPUT_DP:
1403 default:
1404 type = DRM_MODE_ENCODER_TMDS;
1405 break;
1406 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001407
1408 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1409 if (!nv_encoder)
1410 return -ENOMEM;
1411 nv_encoder->dcb = dcbe;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001412 nv_encoder->update = nv50_sor_update;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001413
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001414 encoder = to_drm_encoder(nv_encoder);
1415 encoder->possible_crtcs = dcbe->heads;
1416 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10001417 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1418 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001419 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001420
Daniel Vettercde4c442018-07-09 10:40:07 +02001421 drm_connector_attach_encoder(connector, encoder);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001422
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001423 if (dcbe->type == DCB_OUTPUT_DP) {
Ben Skeggs13a86512017-07-19 16:49:59 +10001424 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001425 struct nvkm_i2c_aux *aux =
1426 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1427 if (aux) {
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001428 if (disp->disp->object.oclass < GF110_DISP) {
Ben Skeggs13a86512017-07-19 16:49:59 +10001429 /* HW has no support for address-only
1430 * transactions, so we're required to
1431 * use custom I2C-over-AUX code.
1432 */
1433 nv_encoder->i2c = &aux->i2c;
1434 } else {
1435 nv_encoder->i2c = &nv_connector->aux.ddc;
1436 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001437 nv_encoder->aux = aux;
1438 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001439
Ben Skeggs34508f92018-05-08 20:39:47 +10001440 if ((data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len)) &&
1441 ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04)) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001442 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
1443 nv_connector->base.base.id,
1444 &nv_encoder->dp.mstm);
1445 if (ret)
1446 return ret;
1447 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001448 } else {
1449 struct nvkm_i2c_bus *bus =
1450 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1451 if (bus)
1452 nv_encoder->i2c = &bus->i2c;
1453 }
1454
Ben Skeggs83fc0832011-07-05 13:08:40 +10001455 return 0;
1456}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001457
1458/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10001459 * PIOR
1460 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +10001461static int
1462nv50_pior_atomic_check(struct drm_encoder *encoder,
1463 struct drm_crtc_state *crtc_state,
1464 struct drm_connector_state *conn_state)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001465{
Ben Skeggs839ca902016-11-04 17:20:36 +10001466 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1467 if (ret)
1468 return ret;
1469 crtc_state->adjusted_mode.clock *= 2;
1470 return 0;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001471}
1472
1473static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001474nv50_pior_disable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001475{
Ben Skeggsf20c6652016-11-04 17:20:36 +10001476 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggs0a368772018-05-08 20:39:47 +10001477 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1478 if (nv_encoder->crtc)
1479 core->func->pior->ctrl(core, nv_encoder->or, 0x00000000, NULL);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001480 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001481 nv50_outp_release(nv_encoder);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001482}
1483
1484static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001485nv50_pior_enable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001486{
Ben Skeggseb6313a2013-02-11 09:52:58 +10001487 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1488 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1489 struct nouveau_connector *nv_connector;
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001490 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
Ben Skeggs0a368772018-05-08 20:39:47 +10001491 struct nv50_core *core = nv50_disp(encoder->dev)->core;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001492 u8 owner = 1 << nv_crtc->index;
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001493 u8 proto;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001494
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001495 nv50_outp_acquire(nv_encoder);
1496
Ben Skeggseb6313a2013-02-11 09:52:58 +10001497 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1498 switch (nv_connector->base.display_info.bpc) {
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001499 case 10: asyh->or.depth = 0x6; break;
1500 case 8: asyh->or.depth = 0x5; break;
1501 case 6: asyh->or.depth = 0x2; break;
1502 default: asyh->or.depth = 0x0; break;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001503 }
1504
1505 switch (nv_encoder->dcb->type) {
1506 case DCB_OUTPUT_TMDS:
1507 case DCB_OUTPUT_DP:
1508 proto = 0x0;
1509 break;
1510 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10001511 BUG();
Ben Skeggseb6313a2013-02-11 09:52:58 +10001512 break;
1513 }
1514
Ben Skeggs0a368772018-05-08 20:39:47 +10001515 core->func->pior->ctrl(core, nv_encoder->or, (proto << 8) | owner, asyh);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001516 nv_encoder->crtc = encoder->crtc;
1517}
1518
Ben Skeggsf20c6652016-11-04 17:20:36 +10001519static const struct drm_encoder_helper_funcs
1520nv50_pior_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001521 .atomic_check = nv50_pior_atomic_check,
1522 .enable = nv50_pior_enable,
1523 .disable = nv50_pior_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10001524};
Ben Skeggseb6313a2013-02-11 09:52:58 +10001525
1526static void
1527nv50_pior_destroy(struct drm_encoder *encoder)
1528{
1529 drm_encoder_cleanup(encoder);
1530 kfree(encoder);
1531}
1532
Ben Skeggsf20c6652016-11-04 17:20:36 +10001533static const struct drm_encoder_funcs
1534nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10001535 .destroy = nv50_pior_destroy,
1536};
1537
1538static int
1539nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1540{
1541 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001542 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001543 struct nvkm_i2c_bus *bus = NULL;
1544 struct nvkm_i2c_aux *aux = NULL;
1545 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001546 struct nouveau_encoder *nv_encoder;
1547 struct drm_encoder *encoder;
1548 int type;
1549
1550 switch (dcbe->type) {
1551 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001552 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1553 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001554 type = DRM_MODE_ENCODER_TMDS;
1555 break;
1556 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001557 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
Ben Skeggs62b290f2018-05-08 20:39:47 +10001558 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001559 type = DRM_MODE_ENCODER_TMDS;
1560 break;
1561 default:
1562 return -ENODEV;
1563 }
1564
1565 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1566 if (!nv_encoder)
1567 return -ENOMEM;
1568 nv_encoder->dcb = dcbe;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001569 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001570 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001571
1572 encoder = to_drm_encoder(nv_encoder);
1573 encoder->possible_crtcs = dcbe->heads;
1574 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10001575 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1576 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001577 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001578
Daniel Vettercde4c442018-07-09 10:40:07 +02001579 drm_connector_attach_encoder(connector, encoder);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001580 return 0;
1581}
1582
1583/******************************************************************************
Ben Skeggs839ca902016-11-04 17:20:36 +10001584 * Atomic
1585 *****************************************************************************/
1586
1587static void
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001588nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 *interlock)
Ben Skeggs839ca902016-11-04 17:20:36 +10001589{
1590 struct nv50_disp *disp = nv50_disp(drm->dev);
Ben Skeggs09e1b782018-05-08 20:39:47 +10001591 struct nv50_core *core = disp->core;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001592 struct nv50_mstm *mstm;
1593 struct drm_encoder *encoder;
Ben Skeggs839ca902016-11-04 17:20:36 +10001594
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001595 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
Ben Skeggs839ca902016-11-04 17:20:36 +10001596
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001597 drm_for_each_encoder(encoder, drm->dev) {
1598 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1599 mstm = nouveau_encoder(encoder)->dp.mstm;
1600 if (mstm && mstm->modified)
1601 nv50_mstm_prepare(mstm);
1602 }
1603 }
1604
Ben Skeggs09e1b782018-05-08 20:39:47 +10001605 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1606 core->func->update(core, interlock, true);
1607 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1608 disp->core->chan.base.device))
1609 NV_ERROR(drm, "core notifier timeout\n");
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001610
1611 drm_for_each_encoder(encoder, drm->dev) {
1612 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1613 mstm = nouveau_encoder(encoder)->dp.mstm;
1614 if (mstm && mstm->modified)
1615 nv50_mstm_cleanup(mstm);
1616 }
1617 }
Ben Skeggs839ca902016-11-04 17:20:36 +10001618}
1619
1620static void
1621nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1622{
1623 struct drm_device *dev = state->dev;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001624 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001625 struct drm_crtc *crtc;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001626 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001627 struct drm_plane *plane;
1628 struct nouveau_drm *drm = nouveau_drm(dev);
1629 struct nv50_disp *disp = nv50_disp(dev);
1630 struct nv50_atom *atom = nv50_atom(state);
1631 struct nv50_outp_atom *outp, *outt;
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001632 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
Ben Skeggs839ca902016-11-04 17:20:36 +10001633 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);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001655 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001656 }
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
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001669 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
Ben Skeggs839ca902016-11-04 17:20:36 +10001670 }
1671
1672 /* Disable output path(s). */
1673 list_for_each_entry(outp, &atom->outp, head) {
1674 const struct drm_encoder_helper_funcs *help;
1675 struct drm_encoder *encoder;
1676
1677 encoder = outp->encoder;
1678 help = encoder->helper_private;
1679
1680 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
1681 outp->clr.mask, outp->set.mask);
1682
1683 if (outp->clr.mask) {
1684 help->disable(encoder);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001685 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001686 if (outp->flush_disable) {
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001687 nv50_disp_atomic_commit_core(drm, interlock);
1688 memset(interlock, 0x00, sizeof(interlock));
Ben Skeggs839ca902016-11-04 17:20:36 +10001689 }
1690 }
1691 }
1692
1693 /* Flush disable. */
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001694 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001695 if (atom->flush_disable) {
Ben Skeggs04fc14b2018-05-08 20:39:47 +10001696 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1697 struct nv50_wndw *wndw = nv50_wndw(plane);
1698 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1699 if (wndw->func->update)
1700 wndw->func->update(wndw, interlock);
1701 }
1702 }
1703
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001704 nv50_disp_atomic_commit_core(drm, interlock);
1705 memset(interlock, 0x00, sizeof(interlock));
Ben Skeggs839ca902016-11-04 17:20:36 +10001706 }
1707 }
1708
1709 /* Update output path(s). */
1710 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
1711 const struct drm_encoder_helper_funcs *help;
1712 struct drm_encoder *encoder;
1713
1714 encoder = outp->encoder;
1715 help = encoder->helper_private;
1716
1717 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
1718 outp->set.mask, outp->clr.mask);
1719
1720 if (outp->set.mask) {
1721 help->enable(encoder);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001722 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001723 }
1724
1725 list_del(&outp->head);
1726 kfree(outp);
1727 }
1728
1729 /* Update head(s). */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001730 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001731 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001732 struct nv50_head *head = nv50_head(crtc);
1733
1734 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
1735 asyh->set.mask, asyh->clr.mask);
1736
1737 if (asyh->set.mask) {
1738 nv50_head_flush_set(head, asyh);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001739 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001740 }
Ben Skeggs839ca902016-11-04 17:20:36 +10001741
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001742 if (new_crtc_state->active) {
1743 if (!old_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001744 drm_crtc_vblank_on(crtc);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001745 if (new_crtc_state->event)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001746 drm_crtc_vblank_get(crtc);
1747 }
Ben Skeggs2b507892017-01-24 09:32:26 +10001748 }
1749
Ben Skeggs839ca902016-11-04 17:20:36 +10001750 /* Update plane(s). */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001751 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1752 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001753 struct nv50_wndw *wndw = nv50_wndw(plane);
1754
1755 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
1756 asyw->set.mask, asyw->clr.mask);
1757 if ( !asyw->set.mask &&
1758 (!asyw->clr.mask || atom->flush_disable))
1759 continue;
1760
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001761 nv50_wndw_flush_set(wndw, interlock, asyw);
Ben Skeggs839ca902016-11-04 17:20:36 +10001762 }
1763
1764 /* Flush update. */
Ben Skeggs04fc14b2018-05-08 20:39:47 +10001765 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1766 struct nv50_wndw *wndw = nv50_wndw(plane);
1767 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1768 if (wndw->func->update)
1769 wndw->func->update(wndw, interlock);
1770 }
1771 }
1772
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001773 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
1774 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
1775 !atom->state.legacy_cursor_update)
1776 nv50_disp_atomic_commit_core(drm, interlock);
Ben Skeggs09e1b782018-05-08 20:39:47 +10001777 else
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001778 disp->core->func->update(disp->core, interlock, false);
Ben Skeggs839ca902016-11-04 17:20:36 +10001779 }
1780
1781 if (atom->lock_core)
1782 mutex_unlock(&disp->mutex);
1783
1784 /* Wait for HW to signal completion. */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001785 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1786 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001787 struct nv50_wndw *wndw = nv50_wndw(plane);
1788 int ret = nv50_wndw_wait_armed(wndw, asyw);
1789 if (ret)
1790 NV_ERROR(drm, "%s: timeout\n", plane->name);
1791 }
1792
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001793 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1794 if (new_crtc_state->event) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001795 unsigned long flags;
Mario Kleinerbd9f6602016-11-23 07:58:54 +01001796 /* Get correct count/ts if racing with vblank irq */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001797 if (new_crtc_state->active)
Dave Airlie0c697fa2017-08-15 16:16:58 +10001798 drm_crtc_accurate_vblank_count(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001799 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001800 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Ben Skeggs839ca902016-11-04 17:20:36 +10001801 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001802
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001803 new_crtc_state->event = NULL;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001804 if (new_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001805 drm_crtc_vblank_put(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001806 }
1807 }
1808
1809 drm_atomic_helper_commit_hw_done(state);
1810 drm_atomic_helper_cleanup_planes(dev, state);
1811 drm_atomic_helper_commit_cleanup_done(state);
1812 drm_atomic_state_put(state);
1813}
1814
1815static void
1816nv50_disp_atomic_commit_work(struct work_struct *work)
1817{
1818 struct drm_atomic_state *state =
1819 container_of(work, typeof(*state), commit_work);
1820 nv50_disp_atomic_commit_tail(state);
1821}
1822
1823static int
1824nv50_disp_atomic_commit(struct drm_device *dev,
1825 struct drm_atomic_state *state, bool nonblock)
1826{
1827 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsd324c5b2017-11-01 09:12:25 +10001828 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001829 struct drm_plane *plane;
1830 struct drm_crtc *crtc;
1831 bool active = false;
1832 int ret, i;
1833
1834 ret = pm_runtime_get_sync(dev->dev);
1835 if (ret < 0 && ret != -EACCES)
1836 return ret;
1837
1838 ret = drm_atomic_helper_setup_commit(state, nonblock);
1839 if (ret)
1840 goto done;
1841
1842 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
1843
1844 ret = drm_atomic_helper_prepare_planes(dev, state);
1845 if (ret)
1846 goto done;
1847
1848 if (!nonblock) {
1849 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1850 if (ret)
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001851 goto err_cleanup;
Ben Skeggs839ca902016-11-04 17:20:36 +10001852 }
1853
Maarten Lankhorst85726362017-07-11 16:33:05 +02001854 ret = drm_atomic_helper_swap_state(state, true);
1855 if (ret)
1856 goto err_cleanup;
1857
Ben Skeggsd324c5b2017-11-01 09:12:25 +10001858 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1859 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001860 struct nv50_wndw *wndw = nv50_wndw(plane);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001861
Ben Skeggsccd27db2018-05-08 20:39:47 +10001862 if (asyw->set.image)
1863 nv50_wndw_ntfy_enable(wndw, asyw);
Ben Skeggs839ca902016-11-04 17:20:36 +10001864 }
1865
Ben Skeggs839ca902016-11-04 17:20:36 +10001866 drm_atomic_state_get(state);
1867
1868 if (nonblock)
1869 queue_work(system_unbound_wq, &state->commit_work);
1870 else
1871 nv50_disp_atomic_commit_tail(state);
1872
1873 drm_for_each_crtc(crtc, dev) {
1874 if (crtc->state->enable) {
1875 if (!drm->have_disp_power_ref) {
1876 drm->have_disp_power_ref = true;
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001877 return 0;
Ben Skeggs839ca902016-11-04 17:20:36 +10001878 }
1879 active = true;
1880 break;
1881 }
1882 }
1883
1884 if (!active && drm->have_disp_power_ref) {
1885 pm_runtime_put_autosuspend(dev->dev);
1886 drm->have_disp_power_ref = false;
1887 }
1888
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001889err_cleanup:
1890 if (ret)
1891 drm_atomic_helper_cleanup_planes(dev, state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001892done:
1893 pm_runtime_put_autosuspend(dev->dev);
1894 return ret;
1895}
1896
1897static struct nv50_outp_atom *
1898nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
1899{
1900 struct nv50_outp_atom *outp;
1901
1902 list_for_each_entry(outp, &atom->outp, head) {
1903 if (outp->encoder == encoder)
1904 return outp;
1905 }
1906
1907 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
1908 if (!outp)
1909 return ERR_PTR(-ENOMEM);
1910
1911 list_add(&outp->head, &atom->outp);
1912 outp->encoder = encoder;
1913 return outp;
1914}
1915
1916static int
1917nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001918 struct drm_connector_state *old_connector_state)
Ben Skeggs839ca902016-11-04 17:20:36 +10001919{
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001920 struct drm_encoder *encoder = old_connector_state->best_encoder;
1921 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001922 struct drm_crtc *crtc;
1923 struct nv50_outp_atom *outp;
1924
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001925 if (!(crtc = old_connector_state->crtc))
Ben Skeggs839ca902016-11-04 17:20:36 +10001926 return 0;
1927
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001928 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
1929 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1930 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001931 outp = nv50_disp_outp_atomic_add(atom, encoder);
1932 if (IS_ERR(outp))
1933 return PTR_ERR(outp);
1934
1935 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1936 outp->flush_disable = true;
1937 atom->flush_disable = true;
1938 }
1939 outp->clr.ctrl = true;
1940 atom->lock_core = true;
1941 }
1942
1943 return 0;
1944}
1945
1946static int
1947nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
1948 struct drm_connector_state *connector_state)
1949{
1950 struct drm_encoder *encoder = connector_state->best_encoder;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001951 struct drm_crtc_state *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001952 struct drm_crtc *crtc;
1953 struct nv50_outp_atom *outp;
1954
1955 if (!(crtc = connector_state->crtc))
1956 return 0;
1957
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001958 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1959 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001960 outp = nv50_disp_outp_atomic_add(atom, encoder);
1961 if (IS_ERR(outp))
1962 return PTR_ERR(outp);
1963
1964 outp->set.ctrl = true;
1965 atom->lock_core = true;
1966 }
1967
1968 return 0;
1969}
1970
1971static int
1972nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
1973{
1974 struct nv50_atom *atom = nv50_atom(state);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001975 struct drm_connector_state *old_connector_state, *new_connector_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001976 struct drm_connector *connector;
Ben Skeggs119608a2018-05-08 20:39:47 +10001977 struct drm_crtc_state *new_crtc_state;
1978 struct drm_crtc *crtc;
Ben Skeggs839ca902016-11-04 17:20:36 +10001979 int ret, i;
1980
Ben Skeggs119608a2018-05-08 20:39:47 +10001981 /* We need to handle colour management on a per-plane basis. */
1982 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1983 if (new_crtc_state->color_mgmt_changed) {
1984 ret = drm_atomic_add_affected_planes(state, crtc);
1985 if (ret)
1986 return ret;
1987 }
1988 }
1989
Ben Skeggs839ca902016-11-04 17:20:36 +10001990 ret = drm_atomic_helper_check(dev, state);
1991 if (ret)
1992 return ret;
1993
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001994 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
1995 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001996 if (ret)
1997 return ret;
1998
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001999 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10002000 if (ret)
2001 return ret;
2002 }
2003
2004 return 0;
2005}
2006
2007static void
2008nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
2009{
2010 struct nv50_atom *atom = nv50_atom(state);
2011 struct nv50_outp_atom *outp, *outt;
2012
2013 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2014 list_del(&outp->head);
2015 kfree(outp);
2016 }
2017
2018 drm_atomic_state_default_clear(state);
2019}
2020
2021static void
2022nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2023{
2024 struct nv50_atom *atom = nv50_atom(state);
2025 drm_atomic_state_default_release(&atom->state);
2026 kfree(atom);
2027}
2028
2029static struct drm_atomic_state *
2030nv50_disp_atomic_state_alloc(struct drm_device *dev)
2031{
2032 struct nv50_atom *atom;
2033 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2034 drm_atomic_state_init(dev, &atom->state) < 0) {
2035 kfree(atom);
2036 return NULL;
2037 }
2038 INIT_LIST_HEAD(&atom->outp);
2039 return &atom->state;
2040}
2041
2042static const struct drm_mode_config_funcs
2043nv50_disp_func = {
2044 .fb_create = nouveau_user_framebuffer_create,
Noralf Trønnesd0f54f52017-12-05 19:25:00 +01002045 .output_poll_changed = drm_fb_helper_output_poll_changed,
Ben Skeggs839ca902016-11-04 17:20:36 +10002046 .atomic_check = nv50_disp_atomic_check,
2047 .atomic_commit = nv50_disp_atomic_commit,
2048 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2049 .atomic_state_clear = nv50_disp_atomic_state_clear,
2050 .atomic_state_free = nv50_disp_atomic_state_free,
2051};
2052
2053/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002054 * Init
2055 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002056
Ben Skeggs2a44e492011-11-09 11:36:33 +10002057void
Ben Skeggse225f442012-11-21 14:40:21 +10002058nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002059{
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002060 struct nouveau_encoder *nv_encoder;
2061 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002062 struct drm_plane *plane;
2063
2064 drm_for_each_plane(plane, dev) {
2065 struct nv50_wndw *wndw = nv50_wndw(plane);
2066 if (plane->funcs != &nv50_wndw)
2067 continue;
2068 nv50_wndw_fini(wndw);
2069 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002070
2071 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2072 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2073 nv_encoder = nouveau_encoder(encoder);
2074 nv50_mstm_fini(nv_encoder->dp.mstm);
2075 }
2076 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002077}
2078
2079int
Ben Skeggse225f442012-11-21 14:40:21 +10002080nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002081{
Ben Skeggs09e1b782018-05-08 20:39:47 +10002082 struct nv50_core *core = nv50_disp(dev)->core;
Ben Skeggs354d3502016-11-04 17:20:36 +10002083 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002084 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002085
Ben Skeggs09e1b782018-05-08 20:39:47 +10002086 core->func->init(core);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002087
Ben Skeggs354d3502016-11-04 17:20:36 +10002088 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2089 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
Ben Skeggs9c5753b2017-05-19 23:59:35 +10002090 struct nouveau_encoder *nv_encoder =
2091 nouveau_encoder(encoder);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002092 nv50_mstm_init(nv_encoder->dp.mstm);
Ben Skeggs354d3502016-11-04 17:20:36 +10002093 }
2094 }
2095
Ben Skeggs973f10c2016-11-04 17:20:36 +10002096 drm_for_each_plane(plane, dev) {
2097 struct nv50_wndw *wndw = nv50_wndw(plane);
2098 if (plane->funcs != &nv50_wndw)
2099 continue;
2100 nv50_wndw_init(wndw);
2101 }
2102
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002103 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002104}
2105
2106void
Ben Skeggse225f442012-11-21 14:40:21 +10002107nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002108{
Ben Skeggse225f442012-11-21 14:40:21 +10002109 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002110
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10002111 nv50_core_del(&disp->core);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002112
Ben Skeggs816af2f2011-11-16 15:48:48 +10002113 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002114 if (disp->sync)
2115 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002116 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002117
Ben Skeggs77145f12012-07-31 16:16:21 +10002118 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002119 kfree(disp);
2120}
2121
Ben Skeggs839ca902016-11-04 17:20:36 +10002122MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
2123static int nouveau_atomic = 0;
2124module_param_named(atomic, nouveau_atomic, int, 0400);
2125
Ben Skeggs26f6d882011-07-04 16:25:18 +10002126int
Ben Skeggse225f442012-11-21 14:40:21 +10002127nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002128{
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002129 struct nvif_device *device = &nouveau_drm(dev)->client.device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002130 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002131 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002132 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002133 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002134 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002135 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002136
2137 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2138 if (!disp)
2139 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10002140
Ben Skeggs839ca902016-11-04 17:20:36 +10002141 mutex_init(&disp->mutex);
2142
Ben Skeggs77145f12012-07-31 16:16:21 +10002143 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002144 nouveau_display(dev)->dtor = nv50_display_destroy;
2145 nouveau_display(dev)->init = nv50_display_init;
2146 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002147 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs839ca902016-11-04 17:20:36 +10002148 dev->mode_config.funcs = &nv50_disp_func;
Ilia Mirkinc20bb152018-02-03 14:11:23 -05002149 dev->driver->driver_features |= DRIVER_PREFER_XBGR_30BPP;
Ben Skeggs839ca902016-11-04 17:20:36 +10002150 if (nouveau_atomic)
2151 dev->driver->driver_features |= DRIVER_ATOMIC;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002152
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002153 /* small shared memory area we use for notifiers and semaphores */
Ben Skeggsbab7cc12016-05-24 17:26:48 +10002154 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002155 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002156 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002157 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002158 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002159 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002160 if (ret)
2161 nouveau_bo_unpin(disp->sync);
2162 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002163 if (ret)
2164 nouveau_bo_ref(NULL, &disp->sync);
2165 }
2166
2167 if (ret)
2168 goto out;
2169
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002170 /* allocate master evo channel */
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10002171 ret = nv50_core_new(drm, &disp->core);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002172 if (ret)
2173 goto out;
2174
Ben Skeggs438d99e2011-07-05 16:48:06 +10002175 /* create crtc objects to represent the hw heads */
Ben Skeggsfacaed62018-05-08 20:39:48 +10002176 if (disp->disp->object.oclass >= GV100_DISP)
2177 crtcs = nvif_rd32(&device->object, 0x610060) & 0xff;
2178 else
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10002179 if (disp->disp->object.oclass >= GF110_DISP)
Ilia Mirkineba5e562017-07-03 13:06:26 -04002180 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
Ben Skeggs63718a02012-11-16 11:44:14 +10002181 else
Ilia Mirkineba5e562017-07-03 13:06:26 -04002182 crtcs = 0x3;
Ben Skeggs63718a02012-11-16 11:44:14 +10002183
Ilia Mirkineba5e562017-07-03 13:06:26 -04002184 for (i = 0; i < fls(crtcs); i++) {
2185 if (!(crtcs & (1 << i)))
2186 continue;
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002187 ret = nv50_head_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002188 if (ret)
2189 goto out;
2190 }
2191
Ben Skeggs83fc0832011-07-05 13:08:40 +10002192 /* create encoder/connector objects based on VBIOS DCB table */
2193 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2194 connector = nouveau_connector_create(dev, dcbe->connector);
2195 if (IS_ERR(connector))
2196 continue;
2197
Ben Skeggseb6313a2013-02-11 09:52:58 +10002198 if (dcbe->location == DCB_LOC_ON_CHIP) {
2199 switch (dcbe->type) {
2200 case DCB_OUTPUT_TMDS:
2201 case DCB_OUTPUT_LVDS:
2202 case DCB_OUTPUT_DP:
2203 ret = nv50_sor_create(connector, dcbe);
2204 break;
2205 case DCB_OUTPUT_ANALOG:
2206 ret = nv50_dac_create(connector, dcbe);
2207 break;
2208 default:
2209 ret = -ENODEV;
2210 break;
2211 }
2212 } else {
2213 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002214 }
2215
Ben Skeggseb6313a2013-02-11 09:52:58 +10002216 if (ret) {
2217 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2218 dcbe->location, dcbe->type,
2219 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002220 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002221 }
2222 }
2223
2224 /* cull any connectors we created that don't have an encoder */
2225 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2226 if (connector->encoder_ids[0])
2227 continue;
2228
Ben Skeggs77145f12012-07-31 16:16:21 +10002229 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002230 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002231 connector->funcs->destroy(connector);
2232 }
2233
Ben Skeggs26f6d882011-07-04 16:25:18 +10002234out:
2235 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002236 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002237 return ret;
2238}