blob: 0f2020010aab08670da4b854d072feffad30023d [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 Skeggsa01ca782015-08-20 14:54:15 +1000157 ret = nvif_object_init(&dmac->base.user, 0xf0000000, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000158 &(struct nv_dma_v0) {
159 .target = NV_DMA_V0_TARGET_VRAM,
160 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000161 .start = syncbuf + 0x0000,
162 .limit = syncbuf + 0x0fff,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000163 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000164 &dmac->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000165 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000166 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000167
Ben Skeggsa01ca782015-08-20 14:54:15 +1000168 ret = nvif_object_init(&dmac->base.user, 0xf0000001, NV_DMA_IN_MEMORY,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000169 &(struct nv_dma_v0) {
170 .target = NV_DMA_V0_TARGET_VRAM,
171 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000172 .start = 0,
Ben Skeggsf392ec42014-08-10 04:10:28 +1000173 .limit = device->info.ram_user - 1,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000174 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000175 &dmac->vram);
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000176 if (ret)
Ben Skeggs47057302012-11-16 13:58:48 +1000177 return ret;
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000178
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000179 return ret;
180}
181
Ben Skeggs410f3ec2014-08-10 04:10:25 +1000182/******************************************************************************
Ben Skeggsbdb8c212011-11-12 01:30:24 +1000183 * EVO channel helpers
184 *****************************************************************************/
Ben Skeggs15907002018-05-08 20:39:47 +1000185u32 *
186evo_wait(struct nv50_dmac *evoc, int nr)
Ben Skeggs51beb422011-07-05 10:33:08 +1000187{
Ben Skeggse225f442012-11-21 14:40:21 +1000188 struct nv50_dmac *dmac = evoc;
Ben Skeggsa01ca782015-08-20 14:54:15 +1000189 struct nvif_device *device = dmac->base.device;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000190 u32 put = nvif_rd32(&dmac->base.user, 0x0000) / 4;
Ben Skeggs51beb422011-07-05 10:33:08 +1000191
Daniel Vetter59ad1462012-12-02 14:49:44 +0100192 mutex_lock(&dmac->lock);
Ben Skeggsde8268c2012-11-16 10:24:31 +1000193 if (put + nr >= (PAGE_SIZE / 4) - 8) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000194 dmac->ptr[put] = 0x20000000;
Ben Skeggs51beb422011-07-05 10:33:08 +1000195
Ben Skeggs0ad72862014-08-10 04:10:22 +1000196 nvif_wr32(&dmac->base.user, 0x0000, 0x00000000);
Ben Skeggs54442042015-08-20 14:54:11 +1000197 if (nvif_msec(device, 2000,
198 if (!nvif_rd32(&dmac->base.user, 0x0004))
199 break;
200 ) < 0) {
Daniel Vetter59ad1462012-12-02 14:49:44 +0100201 mutex_unlock(&dmac->lock);
Joe Perches8dfe1622017-02-28 04:55:54 -0800202 pr_err("nouveau: evo channel stalled\n");
Ben Skeggs51beb422011-07-05 10:33:08 +1000203 return NULL;
204 }
205
206 put = 0;
207 }
208
Ben Skeggsb5a794b2012-10-16 14:18:32 +1000209 return dmac->ptr + put;
Ben Skeggs51beb422011-07-05 10:33:08 +1000210}
211
Ben Skeggs15907002018-05-08 20:39:47 +1000212void
213evo_kick(u32 *push, struct nv50_dmac *evoc)
Ben Skeggs51beb422011-07-05 10:33:08 +1000214{
Ben Skeggse225f442012-11-21 14:40:21 +1000215 struct nv50_dmac *dmac = evoc;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000216 nvif_wr32(&dmac->base.user, 0x0000, (push - dmac->ptr) << 2);
Daniel Vetter59ad1462012-12-02 14:49:44 +0100217 mutex_unlock(&dmac->lock);
Ben Skeggs51beb422011-07-05 10:33:08 +1000218}
219
Ben Skeggs438d99e2011-07-05 16:48:06 +1000220/******************************************************************************
Ben Skeggsd92c8ad2016-11-04 17:20:36 +1000221 * Output path helpers
Ben Skeggsa91d3222014-12-22 16:30:13 +1000222 *****************************************************************************/
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000223static void
224nv50_outp_release(struct nouveau_encoder *nv_encoder)
225{
226 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
227 struct {
228 struct nv50_disp_mthd_v1 base;
229 } args = {
230 .base.version = 1,
231 .base.method = NV50_DISP_MTHD_V1_RELEASE,
232 .base.hasht = nv_encoder->dcb->hasht,
233 .base.hashm = nv_encoder->dcb->hashm,
234 };
235
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000236 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000237 nv_encoder->or = -1;
238 nv_encoder->link = 0;
239}
240
241static int
242nv50_outp_acquire(struct nouveau_encoder *nv_encoder)
243{
244 struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
245 struct nv50_disp *disp = nv50_disp(drm->dev);
246 struct {
247 struct nv50_disp_mthd_v1 base;
248 struct nv50_disp_acquire_v0 info;
249 } args = {
250 .base.version = 1,
251 .base.method = NV50_DISP_MTHD_V1_ACQUIRE,
252 .base.hasht = nv_encoder->dcb->hasht,
253 .base.hashm = nv_encoder->dcb->hashm,
254 };
255 int ret;
256
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000257 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000258 if (ret) {
259 NV_ERROR(drm, "error acquiring output path: %d\n", ret);
260 return ret;
261 }
262
263 nv_encoder->or = args.info.or;
264 nv_encoder->link = args.info.link;
265 return 0;
266}
267
Ben Skeggsd92c8ad2016-11-04 17:20:36 +1000268static int
269nv50_outp_atomic_check_view(struct drm_encoder *encoder,
270 struct drm_crtc_state *crtc_state,
271 struct drm_connector_state *conn_state,
272 struct drm_display_mode *native_mode)
273{
274 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
275 struct drm_display_mode *mode = &crtc_state->mode;
276 struct drm_connector *connector = conn_state->connector;
277 struct nouveau_conn_atom *asyc = nouveau_conn_atom(conn_state);
278 struct nouveau_drm *drm = nouveau_drm(encoder->dev);
279
280 NV_ATOMIC(drm, "%s atomic_check\n", encoder->name);
281 asyc->scaler.full = false;
282 if (!native_mode)
283 return 0;
284
285 if (asyc->scaler.mode == DRM_MODE_SCALE_NONE) {
286 switch (connector->connector_type) {
287 case DRM_MODE_CONNECTOR_LVDS:
288 case DRM_MODE_CONNECTOR_eDP:
289 /* Force use of scaler for non-EDID modes. */
290 if (adjusted_mode->type & DRM_MODE_TYPE_DRIVER)
291 break;
292 mode = native_mode;
293 asyc->scaler.full = true;
294 break;
295 default:
296 break;
297 }
298 } else {
299 mode = native_mode;
300 }
301
302 if (!drm_mode_equal(adjusted_mode, mode)) {
303 drm_mode_copy(adjusted_mode, mode);
304 crtc_state->mode_changed = true;
305 }
306
307 return 0;
308}
309
Ben Skeggs839ca902016-11-04 17:20:36 +1000310static int
311nv50_outp_atomic_check(struct drm_encoder *encoder,
312 struct drm_crtc_state *crtc_state,
313 struct drm_connector_state *conn_state)
Ben Skeggsa91d3222014-12-22 16:30:13 +1000314{
Ben Skeggs839ca902016-11-04 17:20:36 +1000315 struct nouveau_connector *nv_connector =
316 nouveau_connector(conn_state->connector);
317 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
318 nv_connector->native_mode);
Ben Skeggsa91d3222014-12-22 16:30:13 +1000319}
320
321/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +1000322 * DAC
323 *****************************************************************************/
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000324static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000325nv50_dac_disable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000326{
Ben Skeggsf20c6652016-11-04 17:20:36 +1000327 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggs0a368772018-05-08 20:39:47 +1000328 struct nv50_core *core = nv50_disp(encoder->dev)->core;
329 if (nv_encoder->crtc)
330 core->func->dac->ctrl(core, nv_encoder->or, 0x00000000, NULL);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000331 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000332 nv50_outp_release(nv_encoder);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000333}
334
335static void
Ben Skeggs839ca902016-11-04 17:20:36 +1000336nv50_dac_enable(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000337{
338 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
339 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000340 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
Ben Skeggs0a368772018-05-08 20:39:47 +1000341 struct nv50_core *core = nv50_disp(encoder->dev)->core;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000342
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000343 nv50_outp_acquire(nv_encoder);
344
Ben Skeggs0a368772018-05-08 20:39:47 +1000345 core->func->dac->ctrl(core, nv_encoder->or, 1 << nv_crtc->index, asyh);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000346 asyh->or.depth = 0;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000347
348 nv_encoder->crtc = encoder->crtc;
349}
350
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000351static enum drm_connector_status
Ben Skeggse225f442012-11-21 14:40:21 +1000352nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000353{
Ben Skeggsc4abd312014-08-10 04:10:26 +1000354 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +1000355 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsc4abd312014-08-10 04:10:26 +1000356 struct {
357 struct nv50_disp_mthd_v1 base;
358 struct nv50_disp_dac_load_v0 load;
359 } args = {
360 .base.version = 1,
361 .base.method = NV50_DISP_MTHD_V1_DAC_LOAD,
362 .base.hasht = nv_encoder->dcb->hasht,
363 .base.hashm = nv_encoder->dcb->hashm,
364 };
365 int ret;
Ben Skeggsb6819932011-07-08 11:14:50 +1000366
Ben Skeggsc4abd312014-08-10 04:10:26 +1000367 args.load.data = nouveau_drm(encoder->dev)->vbios.dactestval;
368 if (args.load.data == 0)
369 args.load.data = 340;
370
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000371 ret = nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggsc4abd312014-08-10 04:10:26 +1000372 if (ret || !args.load.load)
Ben Skeggs35b21d32012-11-08 12:08:55 +1000373 return connector_status_disconnected;
Ben Skeggsb6819932011-07-08 11:14:50 +1000374
Ben Skeggs35b21d32012-11-08 12:08:55 +1000375 return connector_status_connected;
Ben Skeggsb6d8e7e2011-07-07 09:51:29 +1000376}
377
Ben Skeggsf20c6652016-11-04 17:20:36 +1000378static const struct drm_encoder_helper_funcs
379nv50_dac_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +1000380 .atomic_check = nv50_outp_atomic_check,
381 .enable = nv50_dac_enable,
382 .disable = nv50_dac_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +1000383 .detect = nv50_dac_detect
384};
385
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000386static void
Ben Skeggse225f442012-11-21 14:40:21 +1000387nv50_dac_destroy(struct drm_encoder *encoder)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000388{
389 drm_encoder_cleanup(encoder);
390 kfree(encoder);
391}
392
Ben Skeggsf20c6652016-11-04 17:20:36 +1000393static const struct drm_encoder_funcs
394nv50_dac_func = {
Ben Skeggse225f442012-11-21 14:40:21 +1000395 .destroy = nv50_dac_destroy,
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000396};
397
398static int
Ben Skeggse225f442012-11-21 14:40:21 +1000399nv50_dac_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000400{
Ben Skeggs5ed50202013-02-11 20:15:03 +1000401 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000402 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +1000403 struct nvkm_i2c_bus *bus;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000404 struct nouveau_encoder *nv_encoder;
405 struct drm_encoder *encoder;
Ben Skeggs5ed50202013-02-11 20:15:03 +1000406 int type = DRM_MODE_ENCODER_DAC;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000407
408 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
409 if (!nv_encoder)
410 return -ENOMEM;
411 nv_encoder->dcb = dcbe;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +1000412
413 bus = nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
414 if (bus)
415 nv_encoder->i2c = &bus->i2c;
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000416
417 encoder = to_drm_encoder(nv_encoder);
418 encoder->possible_crtcs = dcbe->heads;
419 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +1000420 drm_encoder_init(connector->dev, encoder, &nv50_dac_func, type,
421 "dac-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000422 drm_encoder_helper_add(encoder, &nv50_dac_help);
Ben Skeggs8eaa9662011-07-06 15:25:47 +1000423
424 drm_mode_connector_attach_encoder(connector, encoder);
425 return 0;
426}
Ben Skeggs26f6d882011-07-04 16:25:18 +1000427
428/******************************************************************************
Ben Skeggs78951d22011-11-11 18:13:13 +1000429 * Audio
430 *****************************************************************************/
431static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000432nv50_audio_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
433{
434 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
435 struct nv50_disp *disp = nv50_disp(encoder->dev);
436 struct {
437 struct nv50_disp_mthd_v1 base;
438 struct nv50_disp_sor_hda_eld_v0 eld;
439 } args = {
440 .base.version = 1,
441 .base.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
442 .base.hasht = nv_encoder->dcb->hasht,
443 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
444 (0x0100 << nv_crtc->index),
445 };
446
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000447 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggsf20c6652016-11-04 17:20:36 +1000448}
449
450static void
451nv50_audio_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +1000452{
453 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggscc2a9072014-09-15 21:29:05 +1000454 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs78951d22011-11-11 18:13:13 +1000455 struct nouveau_connector *nv_connector;
Ben Skeggse225f442012-11-21 14:40:21 +1000456 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggsd889c522014-09-15 21:11:51 +1000457 struct __packed {
458 struct {
459 struct nv50_disp_mthd_v1 mthd;
460 struct nv50_disp_sor_hda_eld_v0 eld;
461 } base;
Ben Skeggs120b0c32014-08-10 04:10:26 +1000462 u8 data[sizeof(nv_connector->base.eld)];
463 } args = {
Ben Skeggsd889c522014-09-15 21:11:51 +1000464 .base.mthd.version = 1,
465 .base.mthd.method = NV50_DISP_MTHD_V1_SOR_HDA_ELD,
466 .base.mthd.hasht = nv_encoder->dcb->hasht,
Ben Skeggscc2a9072014-09-15 21:29:05 +1000467 .base.mthd.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
468 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +1000469 };
Ben Skeggs78951d22011-11-11 18:13:13 +1000470
471 nv_connector = nouveau_encoder_connector_get(nv_encoder);
472 if (!drm_detect_monitor_audio(nv_connector->edid))
473 return;
474
Ben Skeggs120b0c32014-08-10 04:10:26 +1000475 memcpy(args.data, nv_connector->base.eld, sizeof(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +1000476
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000477 nvif_mthd(&disp->disp->object, 0, &args,
Jani Nikula938fd8a2014-10-28 16:20:48 +0200478 sizeof(args.base) + drm_eld_size(args.data));
Ben Skeggs78951d22011-11-11 18:13:13 +1000479}
480
Ben Skeggsf20c6652016-11-04 17:20:36 +1000481/******************************************************************************
482 * HDMI
483 *****************************************************************************/
Ben Skeggs78951d22011-11-11 18:13:13 +1000484static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000485nv50_hdmi_disable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc)
Ben Skeggs78951d22011-11-11 18:13:13 +1000486{
487 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse225f442012-11-21 14:40:21 +1000488 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs120b0c32014-08-10 04:10:26 +1000489 struct {
490 struct nv50_disp_mthd_v1 base;
Ben Skeggsf20c6652016-11-04 17:20:36 +1000491 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Ben Skeggs120b0c32014-08-10 04:10:26 +1000492 } args = {
493 .base.version = 1,
Ben Skeggsf20c6652016-11-04 17:20:36 +1000494 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
495 .base.hasht = nv_encoder->dcb->hasht,
496 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
497 (0x0100 << nv_crtc->index),
Ben Skeggs120b0c32014-08-10 04:10:26 +1000498 };
Ben Skeggs78951d22011-11-11 18:13:13 +1000499
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000500 nvif_mthd(&disp->disp->object, 0, &args, sizeof(args));
Ben Skeggs78951d22011-11-11 18:13:13 +1000501}
502
Ben Skeggs78951d22011-11-11 18:13:13 +1000503static void
Ben Skeggsf20c6652016-11-04 17:20:36 +1000504nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
Ben Skeggs78951d22011-11-11 18:13:13 +1000505{
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000506 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
507 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggse225f442012-11-21 14:40:21 +1000508 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggse00f2232014-08-10 04:10:26 +1000509 struct {
510 struct nv50_disp_mthd_v1 base;
511 struct nv50_disp_sor_hdmi_pwr_v0 pwr;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400512 u8 infoframes[2 * 17]; /* two frames, up to 17 bytes each */
Ben Skeggse00f2232014-08-10 04:10:26 +1000513 } args = {
514 .base.version = 1,
515 .base.method = NV50_DISP_MTHD_V1_SOR_HDMI_PWR,
516 .base.hasht = nv_encoder->dcb->hasht,
517 .base.hashm = (0xf0ff & nv_encoder->dcb->hashm) |
518 (0x0100 << nv_crtc->index),
519 .pwr.state = 1,
520 .pwr.rekey = 56, /* binary driver, and tegra, constant */
521 };
522 struct nouveau_connector *nv_connector;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000523 u32 max_ac_packet;
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400524 union hdmi_infoframe avi_frame;
525 union hdmi_infoframe vendor_frame;
526 int ret;
527 int size;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000528
529 nv_connector = nouveau_encoder_connector_get(nv_encoder);
530 if (!drm_detect_hdmi_monitor(nv_connector->edid))
531 return;
532
Shashank Sharma0c1f5282017-07-13 21:03:07 +0530533 ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame.avi, mode,
534 false);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400535 if (!ret) {
536 /* We have an AVI InfoFrame, populate it to the display */
537 args.pwr.avi_infoframe_length
538 = hdmi_infoframe_pack(&avi_frame, args.infoframes, 17);
539 }
540
Ville Syrjäläf1781e92017-11-13 19:04:19 +0200541 ret = drm_hdmi_vendor_infoframe_from_display_mode(&vendor_frame.vendor.hdmi,
542 &nv_connector->base, mode);
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400543 if (!ret) {
544 /* We have a Vendor InfoFrame, populate it to the display */
545 args.pwr.vendor_infoframe_length
546 = hdmi_infoframe_pack(&vendor_frame,
547 args.infoframes
548 + args.pwr.avi_infoframe_length,
549 17);
550 }
551
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000552 max_ac_packet = mode->htotal - mode->hdisplay;
Ben Skeggse00f2232014-08-10 04:10:26 +1000553 max_ac_packet -= args.pwr.rekey;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000554 max_ac_packet -= 18; /* constant from tegra */
Ben Skeggse00f2232014-08-10 04:10:26 +1000555 args.pwr.max_ac_packet = max_ac_packet / 32;
Ben Skeggs64d9cc02011-11-11 19:51:20 +1000556
Alastair Bridgewater34fd3e52017-04-11 13:11:18 -0400557 size = sizeof(args.base)
558 + sizeof(args.pwr)
559 + args.pwr.avi_infoframe_length
560 + args.pwr.vendor_infoframe_length;
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000561 nvif_mthd(&disp->disp->object, 0, &args, size);
Ben Skeggsf20c6652016-11-04 17:20:36 +1000562 nv50_audio_enable(encoder, mode);
Ben Skeggs78951d22011-11-11 18:13:13 +1000563}
564
565/******************************************************************************
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000566 * MST
567 *****************************************************************************/
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000568#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
569#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
570#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
571
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000572struct nv50_mstm {
573 struct nouveau_encoder *outp;
574
575 struct drm_dp_mst_topology_mgr mgr;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000576 struct nv50_msto *msto[4];
577
578 bool modified;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000579 bool disabled;
580 int links;
Ben Skeggs52aa30f2016-11-04 17:20:36 +1000581};
582
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000583struct nv50_mstc {
584 struct nv50_mstm *mstm;
585 struct drm_dp_mst_port *port;
586 struct drm_connector connector;
587
588 struct drm_display_mode *native;
589 struct edid *edid;
590
591 int pbn;
592};
593
594struct nv50_msto {
595 struct drm_encoder encoder;
596
597 struct nv50_head *head;
598 struct nv50_mstc *mstc;
599 bool disabled;
600};
601
602static struct drm_dp_payload *
603nv50_msto_payload(struct nv50_msto *msto)
604{
605 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
606 struct nv50_mstc *mstc = msto->mstc;
607 struct nv50_mstm *mstm = mstc->mstm;
608 int vcpi = mstc->port->vcpi.vcpi, i;
609
610 NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
611 for (i = 0; i < mstm->mgr.max_payloads; i++) {
612 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
613 NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
614 mstm->outp->base.base.name, i, payload->vcpi,
615 payload->start_slot, payload->num_slots);
616 }
617
618 for (i = 0; i < mstm->mgr.max_payloads; i++) {
619 struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
620 if (payload->vcpi == vcpi)
621 return payload;
622 }
623
624 return NULL;
625}
626
627static void
628nv50_msto_cleanup(struct nv50_msto *msto)
629{
630 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
631 struct nv50_mstc *mstc = msto->mstc;
632 struct nv50_mstm *mstm = mstc->mstm;
633
634 NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
635 if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
636 drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
637 if (msto->disabled) {
638 msto->mstc = NULL;
639 msto->head = NULL;
640 msto->disabled = false;
641 }
642}
643
644static void
645nv50_msto_prepare(struct nv50_msto *msto)
646{
647 struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
648 struct nv50_mstc *mstc = msto->mstc;
649 struct nv50_mstm *mstm = mstc->mstm;
650 struct {
651 struct nv50_disp_mthd_v1 base;
652 struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
653 } args = {
654 .base.version = 1,
655 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
656 .base.hasht = mstm->outp->dcb->hasht,
657 .base.hashm = (0xf0ff & mstm->outp->dcb->hashm) |
658 (0x0100 << msto->head->base.index),
659 };
660
661 NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
662 if (mstc->port && mstc->port->vcpi.vcpi > 0) {
663 struct drm_dp_payload *payload = nv50_msto_payload(msto);
664 if (payload) {
665 args.vcpi.start_slot = payload->start_slot;
666 args.vcpi.num_slots = payload->num_slots;
667 args.vcpi.pbn = mstc->port->vcpi.pbn;
668 args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
669 }
670 }
671
672 NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
673 msto->encoder.name, msto->head->base.base.name,
674 args.vcpi.start_slot, args.vcpi.num_slots,
675 args.vcpi.pbn, args.vcpi.aligned_pbn);
Ben Skeggs0d4a2c52018-05-08 20:39:47 +1000676 nvif_mthd(&drm->display->disp.object, 0, &args, sizeof(args));
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000677}
678
679static int
680nv50_msto_atomic_check(struct drm_encoder *encoder,
681 struct drm_crtc_state *crtc_state,
682 struct drm_connector_state *conn_state)
683{
684 struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
685 struct nv50_mstm *mstm = mstc->mstm;
686 int bpp = conn_state->connector->display_info.bpc * 3;
687 int slots;
688
689 mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
690
691 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
692 if (slots < 0)
693 return slots;
694
695 return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
696 mstc->native);
697}
698
699static void
700nv50_msto_enable(struct drm_encoder *encoder)
701{
702 struct nv50_head *head = nv50_head(encoder->crtc);
703 struct nv50_msto *msto = nv50_msto(encoder);
704 struct nv50_mstc *mstc = NULL;
705 struct nv50_mstm *mstm = NULL;
706 struct drm_connector *connector;
Gustavo Padovan875dd622017-05-11 16:10:46 -0300707 struct drm_connector_list_iter conn_iter;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000708 u8 proto, depth;
709 int slots;
710 bool r;
711
Gustavo Padovan875dd622017-05-11 16:10:46 -0300712 drm_connector_list_iter_begin(encoder->dev, &conn_iter);
713 drm_for_each_connector_iter(connector, &conn_iter) {
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000714 if (connector->state->best_encoder == &msto->encoder) {
715 mstc = nv50_mstc(connector);
716 mstm = mstc->mstm;
717 break;
718 }
719 }
Gustavo Padovan875dd622017-05-11 16:10:46 -0300720 drm_connector_list_iter_end(&conn_iter);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000721
722 if (WARN_ON(!mstc))
723 return;
724
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -0700725 slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
726 r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, slots);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000727 WARN_ON(!r);
728
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000729 if (!mstm->links++)
730 nv50_outp_acquire(mstm->outp);
731
732 if (mstm->outp->link & 1)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000733 proto = 0x8;
734 else
735 proto = 0x9;
736
737 switch (mstc->connector.display_info.bpc) {
738 case 6: depth = 0x2; break;
739 case 8: depth = 0x5; break;
740 case 10:
741 default: depth = 0x6; break;
742 }
743
744 mstm->outp->update(mstm->outp, head->base.index,
Ben Skeggs2ca7fb52018-05-08 20:39:47 +1000745 nv50_head_atom(head->base.base.state), proto, depth);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000746
747 msto->head = head;
748 msto->mstc = mstc;
749 mstm->modified = true;
750}
751
752static void
753nv50_msto_disable(struct drm_encoder *encoder)
754{
755 struct nv50_msto *msto = nv50_msto(encoder);
756 struct nv50_mstc *mstc = msto->mstc;
757 struct nv50_mstm *mstm = mstc->mstm;
758
759 if (mstc->port)
760 drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
761
762 mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
763 mstm->modified = true;
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000764 if (!--mstm->links)
765 mstm->disabled = true;
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000766 msto->disabled = true;
767}
768
769static const struct drm_encoder_helper_funcs
770nv50_msto_help = {
771 .disable = nv50_msto_disable,
772 .enable = nv50_msto_enable,
773 .atomic_check = nv50_msto_atomic_check,
774};
775
776static void
777nv50_msto_destroy(struct drm_encoder *encoder)
778{
779 struct nv50_msto *msto = nv50_msto(encoder);
780 drm_encoder_cleanup(&msto->encoder);
781 kfree(msto);
782}
783
784static const struct drm_encoder_funcs
785nv50_msto = {
786 .destroy = nv50_msto_destroy,
787};
788
789static int
790nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
791 struct nv50_msto **pmsto)
792{
793 struct nv50_msto *msto;
794 int ret;
795
796 if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
797 return -ENOMEM;
798
799 ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
800 DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
801 if (ret) {
802 kfree(*pmsto);
803 *pmsto = NULL;
804 return ret;
805 }
806
807 drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
808 msto->encoder.possible_crtcs = heads;
809 return 0;
810}
811
812static struct drm_encoder *
813nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
814 struct drm_connector_state *connector_state)
815{
816 struct nv50_head *head = nv50_head(connector_state->crtc);
817 struct nv50_mstc *mstc = nv50_mstc(connector);
818 if (mstc->port) {
819 struct nv50_mstm *mstm = mstc->mstm;
820 return &mstm->msto[head->base.index]->encoder;
821 }
822 return NULL;
823}
824
825static struct drm_encoder *
826nv50_mstc_best_encoder(struct drm_connector *connector)
827{
828 struct nv50_mstc *mstc = nv50_mstc(connector);
829 if (mstc->port) {
830 struct nv50_mstm *mstm = mstc->mstm;
831 return &mstm->msto[0]->encoder;
832 }
833 return NULL;
834}
835
836static enum drm_mode_status
837nv50_mstc_mode_valid(struct drm_connector *connector,
838 struct drm_display_mode *mode)
839{
840 return MODE_OK;
841}
842
843static int
844nv50_mstc_get_modes(struct drm_connector *connector)
845{
846 struct nv50_mstc *mstc = nv50_mstc(connector);
847 int ret = 0;
848
849 mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
850 drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
Jani Nikulad471ed02017-11-01 16:21:02 +0200851 if (mstc->edid)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000852 ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000853
854 if (!mstc->connector.display_info.bpc)
855 mstc->connector.display_info.bpc = 8;
856
857 if (mstc->native)
858 drm_mode_destroy(mstc->connector.dev, mstc->native);
859 mstc->native = nouveau_conn_native_mode(&mstc->connector);
860 return ret;
861}
862
863static const struct drm_connector_helper_funcs
864nv50_mstc_help = {
865 .get_modes = nv50_mstc_get_modes,
866 .mode_valid = nv50_mstc_mode_valid,
867 .best_encoder = nv50_mstc_best_encoder,
868 .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
869};
870
871static enum drm_connector_status
872nv50_mstc_detect(struct drm_connector *connector, bool force)
873{
874 struct nv50_mstc *mstc = nv50_mstc(connector);
875 if (!mstc->port)
876 return connector_status_disconnected;
877 return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
878}
879
880static void
881nv50_mstc_destroy(struct drm_connector *connector)
882{
883 struct nv50_mstc *mstc = nv50_mstc(connector);
884 drm_connector_cleanup(&mstc->connector);
885 kfree(mstc);
886}
887
888static const struct drm_connector_funcs
889nv50_mstc = {
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000890 .reset = nouveau_conn_reset,
891 .detect = nv50_mstc_detect,
892 .fill_modes = drm_helper_probe_single_connector_modes,
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000893 .destroy = nv50_mstc_destroy,
894 .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
895 .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
896 .atomic_set_property = nouveau_conn_atomic_set_property,
897 .atomic_get_property = nouveau_conn_atomic_get_property,
898};
899
900static int
901nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
902 const char *path, struct nv50_mstc **pmstc)
903{
904 struct drm_device *dev = mstm->outp->base.base.dev;
905 struct nv50_mstc *mstc;
906 int ret, i;
907
908 if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
909 return -ENOMEM;
910 mstc->mstm = mstm;
911 mstc->port = port;
912
913 ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
914 DRM_MODE_CONNECTOR_DisplayPort);
915 if (ret) {
916 kfree(*pmstc);
917 *pmstc = NULL;
918 return ret;
919 }
920
921 drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
922
923 mstc->connector.funcs->reset(&mstc->connector);
924 nouveau_conn_attach_properties(&mstc->connector);
925
Colin Ian King27a451e2017-08-17 23:03:23 +0100926 for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto[i]; i++)
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000927 drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
928
929 drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
930 drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
931 drm_mode_connector_set_path_property(&mstc->connector, path);
932 return 0;
933}
934
935static void
936nv50_mstm_cleanup(struct nv50_mstm *mstm)
937{
938 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
939 struct drm_encoder *encoder;
940 int ret;
941
942 NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
943 ret = drm_dp_check_act_status(&mstm->mgr);
944
945 ret = drm_dp_update_payload_part2(&mstm->mgr);
946
947 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
948 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
949 struct nv50_msto *msto = nv50_msto(encoder);
950 struct nv50_mstc *mstc = msto->mstc;
951 if (mstc && mstc->mstm == mstm)
952 nv50_msto_cleanup(msto);
953 }
954 }
955
956 mstm->modified = false;
957}
958
959static void
960nv50_mstm_prepare(struct nv50_mstm *mstm)
961{
962 struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
963 struct drm_encoder *encoder;
964 int ret;
965
966 NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
967 ret = drm_dp_update_payload_part1(&mstm->mgr);
968
969 drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
970 if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
971 struct nv50_msto *msto = nv50_msto(encoder);
972 struct nv50_mstc *mstc = msto->mstc;
973 if (mstc && mstc->mstm == mstm)
974 nv50_msto_prepare(msto);
975 }
976 }
Ben Skeggs6c22ea32017-05-19 23:59:35 +1000977
978 if (mstm->disabled) {
979 if (!mstm->links)
980 nv50_outp_release(mstm->outp);
981 mstm->disabled = false;
982 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +1000983}
984
985static void
986nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
987{
988 struct nv50_mstm *mstm = nv50_mstm(mgr);
989 drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
990}
991
992static void
993nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
994 struct drm_connector *connector)
995{
996 struct nouveau_drm *drm = nouveau_drm(connector->dev);
997 struct nv50_mstc *mstc = nv50_mstc(connector);
998
999 drm_connector_unregister(&mstc->connector);
1000
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001001 drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
Lyude Paul352672d2018-05-02 19:38:48 -04001002
1003 drm_modeset_lock(&drm->dev->mode_config.connection_mutex, NULL);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001004 mstc->port = NULL;
Lyude Paul352672d2018-05-02 19:38:48 -04001005 drm_modeset_unlock(&drm->dev->mode_config.connection_mutex);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001006
1007 drm_connector_unreference(&mstc->connector);
1008}
1009
1010static void
1011nv50_mstm_register_connector(struct drm_connector *connector)
1012{
1013 struct nouveau_drm *drm = nouveau_drm(connector->dev);
1014
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001015 drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001016
1017 drm_connector_register(connector);
1018}
1019
1020static struct drm_connector *
1021nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
1022 struct drm_dp_mst_port *port, const char *path)
1023{
1024 struct nv50_mstm *mstm = nv50_mstm(mgr);
1025 struct nv50_mstc *mstc;
1026 int ret;
1027
1028 ret = nv50_mstc_new(mstm, port, path, &mstc);
1029 if (ret) {
1030 if (mstc)
1031 mstc->connector.funcs->destroy(&mstc->connector);
1032 return NULL;
1033 }
1034
1035 return &mstc->connector;
1036}
1037
1038static const struct drm_dp_mst_topology_cbs
1039nv50_mstm = {
1040 .add_connector = nv50_mstm_add_connector,
1041 .register_connector = nv50_mstm_register_connector,
1042 .destroy_connector = nv50_mstm_destroy_connector,
1043 .hotplug = nv50_mstm_hotplug,
1044};
1045
1046void
1047nv50_mstm_service(struct nv50_mstm *mstm)
1048{
Ben Skeggs227f66d2017-10-03 16:24:28 +10001049 struct drm_dp_aux *aux = mstm ? mstm->mgr.aux : NULL;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001050 bool handled = true;
1051 int ret;
1052 u8 esi[8] = {};
1053
Ben Skeggs227f66d2017-10-03 16:24:28 +10001054 if (!aux)
1055 return;
1056
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001057 while (handled) {
1058 ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
1059 if (ret != 8) {
1060 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1061 return;
1062 }
1063
1064 drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
1065 if (!handled)
1066 break;
1067
1068 drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
1069 }
1070}
1071
1072void
1073nv50_mstm_remove(struct nv50_mstm *mstm)
1074{
1075 if (mstm)
1076 drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
1077}
1078
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001079static int
1080nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
1081{
1082 struct nouveau_encoder *outp = mstm->outp;
1083 struct {
1084 struct nv50_disp_mthd_v1 base;
1085 struct nv50_disp_sor_dp_mst_link_v0 mst;
1086 } args = {
1087 .base.version = 1,
1088 .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_LINK,
1089 .base.hasht = outp->dcb->hasht,
1090 .base.hashm = outp->dcb->hashm,
1091 .mst.state = state,
1092 };
1093 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001094 struct nvif_object *disp = &drm->display->disp.object;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001095 int ret;
1096
1097 if (dpcd >= 0x12) {
1098 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CTRL, &dpcd);
1099 if (ret < 0)
1100 return ret;
1101
1102 dpcd &= ~DP_MST_EN;
1103 if (state)
1104 dpcd |= DP_MST_EN;
1105
1106 ret = drm_dp_dpcd_writeb(mstm->mgr.aux, DP_MSTM_CTRL, dpcd);
1107 if (ret < 0)
1108 return ret;
1109 }
1110
1111 return nvif_mthd(disp, 0, &args, sizeof(args));
1112}
1113
1114int
1115nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
1116{
1117 int ret, state = 0;
1118
1119 if (!mstm)
1120 return 0;
1121
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10001122 if (dpcd[0] >= 0x12) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001123 ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
1124 if (ret < 0)
1125 return ret;
1126
Ben Skeggs3ca03ca2016-11-07 14:51:53 +10001127 if (!(dpcd[1] & DP_MST_CAP))
1128 dpcd[0] = 0x11;
1129 else
1130 state = allow;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001131 }
1132
1133 ret = nv50_mstm_enable(mstm, dpcd[0], state);
1134 if (ret)
1135 return ret;
1136
1137 ret = drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, state);
1138 if (ret)
1139 return nv50_mstm_enable(mstm, dpcd[0], 0);
1140
1141 return mstm->mgr.mst_state;
1142}
1143
1144static void
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001145nv50_mstm_fini(struct nv50_mstm *mstm)
1146{
1147 if (mstm && mstm->mgr.mst_state)
1148 drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
1149}
1150
1151static void
1152nv50_mstm_init(struct nv50_mstm *mstm)
1153{
1154 if (mstm && mstm->mgr.mst_state)
1155 drm_dp_mst_topology_mgr_resume(&mstm->mgr);
1156}
1157
1158static void
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001159nv50_mstm_del(struct nv50_mstm **pmstm)
1160{
1161 struct nv50_mstm *mstm = *pmstm;
1162 if (mstm) {
1163 kfree(*pmstm);
1164 *pmstm = NULL;
1165 }
1166}
1167
1168static int
1169nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
1170 int conn_base_id, struct nv50_mstm **pmstm)
1171{
1172 const int max_payloads = hweight8(outp->dcb->heads);
1173 struct drm_device *dev = outp->base.base.dev;
1174 struct nv50_mstm *mstm;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001175 int ret, i;
1176 u8 dpcd;
1177
1178 /* This is a workaround for some monitors not functioning
1179 * correctly in MST mode on initial module load. I think
1180 * some bad interaction with the VBIOS may be responsible.
1181 *
1182 * A good ol' off and on again seems to work here ;)
1183 */
1184 ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
1185 if (ret >= 0 && dpcd >= 0x12)
1186 drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001187
1188 if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
1189 return -ENOMEM;
1190 mstm->outp = outp;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001191 mstm->mgr.cbs = &nv50_mstm;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001192
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001193 ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev, aux, aux_max,
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001194 max_payloads, conn_base_id);
1195 if (ret)
1196 return ret;
1197
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001198 for (i = 0; i < max_payloads; i++) {
1199 ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
1200 i, &mstm->msto[i]);
1201 if (ret)
1202 return ret;
1203 }
1204
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001205 return 0;
1206}
1207
1208/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10001209 * SOR
1210 *****************************************************************************/
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001211static void
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001212nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001213 struct nv50_head_atom *asyh, u8 proto, u8 depth)
Ben Skeggse84a35a2014-06-05 10:59:55 +10001214{
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10001215 struct nv50_disp *disp = nv50_disp(nv_encoder->base.base.dev);
Ben Skeggs0a368772018-05-08 20:39:47 +10001216 struct nv50_core *core = disp->core;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001217
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001218 if (!asyh) {
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001219 nv_encoder->ctrl &= ~BIT(head);
1220 if (!(nv_encoder->ctrl & 0x0000000f))
1221 nv_encoder->ctrl = 0;
1222 } else {
1223 nv_encoder->ctrl |= proto << 8;
1224 nv_encoder->ctrl |= BIT(head);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001225 asyh->or.depth = depth;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001226 }
1227
Ben Skeggs0a368772018-05-08 20:39:47 +10001228 core->func->sor->ctrl(core, nv_encoder->or, nv_encoder->ctrl, asyh);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001229}
1230
1231static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001232nv50_sor_disable(struct drm_encoder *encoder)
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001233{
1234 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001235 struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001236
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001237 nv_encoder->crtc = NULL;
Ben Skeggse84a35a2014-06-05 10:59:55 +10001238
1239 if (nv_crtc) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001240 struct nvkm_i2c_aux *aux = nv_encoder->aux;
1241 u8 pwr;
1242
1243 if (aux) {
1244 int ret = nvkm_rdaux(aux, DP_SET_POWER, &pwr, 1);
1245 if (ret == 0) {
1246 pwr &= ~DP_SET_POWER_MASK;
1247 pwr |= DP_SET_POWER_D3;
1248 nvkm_wraux(aux, DP_SET_POWER, &pwr, 1);
1249 }
1250 }
1251
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001252 nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001253 nv50_audio_disable(encoder, nv_crtc);
1254 nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001255 nv50_outp_release(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001256 }
Ben Skeggs4cbb0f82012-03-12 15:23:44 +10001257}
1258
1259static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001260nv50_sor_enable(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001261{
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001262 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1263 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001264 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
1265 struct drm_display_mode *mode = &asyh->state.adjusted_mode;
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001266 struct {
1267 struct nv50_disp_mthd_v1 base;
1268 struct nv50_disp_sor_lvds_script_v0 lvds;
1269 } lvds = {
1270 .base.version = 1,
1271 .base.method = NV50_DISP_MTHD_V1_SOR_LVDS_SCRIPT,
1272 .base.hasht = nv_encoder->dcb->hasht,
1273 .base.hashm = nv_encoder->dcb->hashm,
1274 };
Ben Skeggse225f442012-11-21 14:40:21 +10001275 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs78951d22011-11-11 18:13:13 +10001276 struct drm_device *dev = encoder->dev;
Ben Skeggs77145f12012-07-31 16:16:21 +10001277 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001278 struct nouveau_connector *nv_connector;
Ben Skeggs77145f12012-07-31 16:16:21 +10001279 struct nvbios *bios = &drm->vbios;
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001280 u8 proto = 0xf;
1281 u8 depth = 0x0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001282
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001283 nv_connector = nouveau_encoder_connector_get(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001284 nv_encoder->crtc = encoder->crtc;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001285 nv50_outp_acquire(nv_encoder);
Ben Skeggse84a35a2014-06-05 10:59:55 +10001286
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001287 switch (nv_encoder->dcb->type) {
Ben Skeggscb75d972012-07-11 10:44:20 +10001288 case DCB_OUTPUT_TMDS:
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001289 if (nv_encoder->link & 1) {
Hauke Mehrtens16ef53a92015-11-03 21:00:10 -05001290 proto = 0x1;
1291 /* Only enable dual-link if:
1292 * - Need to (i.e. rate > 165MHz)
1293 * - DCB says we can
1294 * - Not an HDMI monitor, since there's no dual-link
1295 * on HDMI.
1296 */
1297 if (mode->clock >= 165000 &&
1298 nv_encoder->dcb->duallink_possible &&
1299 !drm_detect_hdmi_monitor(nv_connector->edid))
1300 proto |= 0x4;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001301 } else {
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001302 proto = 0x2;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001303 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001304
Ben Skeggsf20c6652016-11-04 17:20:36 +10001305 nv50_hdmi_enable(&nv_encoder->base.base, mode);
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001306 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001307 case DCB_OUTPUT_LVDS:
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001308 proto = 0x0;
1309
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001310 if (bios->fp_no_ddc) {
1311 if (bios->fp.dual_link)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001312 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001313 if (bios->fp.if_is_24bit)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001314 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001315 } else {
Ben Skeggsbefb51e2011-11-18 10:23:59 +10001316 if (nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001317 if (((u8 *)nv_connector->edid)[121] == 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001318 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001319 } else
1320 if (mode->clock >= bios->fp.duallink_transition_clk) {
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001321 lvds.lvds.script |= 0x0100;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001322 }
1323
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001324 if (lvds.lvds.script & 0x0100) {
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001325 if (bios->fp.strapless_is_24bit & 2)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001326 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001327 } else {
1328 if (bios->fp.strapless_is_24bit & 1)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001329 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001330 }
1331
1332 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggsa3761fa2014-08-10 04:10:27 +10001333 lvds.lvds.script |= 0x0200;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001334 }
Ben Skeggs4a230fa2012-11-09 11:25:37 +10001335
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001336 nvif_mthd(&disp->disp->object, 0, &lvds, sizeof(lvds));
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001337 break;
Ben Skeggscb75d972012-07-11 10:44:20 +10001338 case DCB_OUTPUT_DP:
Ben Skeggsf20c6652016-11-04 17:20:36 +10001339 if (nv_connector->base.display_info.bpc == 6)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001340 depth = 0x2;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001341 else
1342 if (nv_connector->base.display_info.bpc == 8)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001343 depth = 0x5;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001344 else
Ben Skeggsbf2c8862012-11-21 14:49:54 +10001345 depth = 0x6;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001346
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001347 if (nv_encoder->link & 1)
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001348 proto = 0x8;
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001349 else
Ben Skeggs419e8dc2012-11-16 11:40:34 +10001350 proto = 0x9;
Ben Skeggsf20c6652016-11-04 17:20:36 +10001351
1352 nv50_audio_enable(encoder, mode);
Ben Skeggs6e83fda2012-03-11 01:28:48 +10001353 break;
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001354 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10001355 BUG();
Ben Skeggs3b6d83d12011-07-08 12:52:14 +10001356 break;
1357 }
Ben Skeggsff8ff502011-07-08 11:53:37 +10001358
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001359 nv_encoder->update(nv_encoder, nv_crtc->index, asyh, proto, depth);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001360}
1361
Ben Skeggsf20c6652016-11-04 17:20:36 +10001362static const struct drm_encoder_helper_funcs
1363nv50_sor_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001364 .atomic_check = nv50_outp_atomic_check,
1365 .enable = nv50_sor_enable,
1366 .disable = nv50_sor_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10001367};
1368
Ben Skeggs83fc0832011-07-05 13:08:40 +10001369static void
Ben Skeggse225f442012-11-21 14:40:21 +10001370nv50_sor_destroy(struct drm_encoder *encoder)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001371{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001372 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1373 nv50_mstm_del(&nv_encoder->dp.mstm);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001374 drm_encoder_cleanup(encoder);
1375 kfree(encoder);
1376}
1377
Ben Skeggsf20c6652016-11-04 17:20:36 +10001378static const struct drm_encoder_funcs
1379nv50_sor_func = {
Ben Skeggse225f442012-11-21 14:40:21 +10001380 .destroy = nv50_sor_destroy,
Ben Skeggs83fc0832011-07-05 13:08:40 +10001381};
1382
1383static int
Ben Skeggse225f442012-11-21 14:40:21 +10001384nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
Ben Skeggs83fc0832011-07-05 13:08:40 +10001385{
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001386 struct nouveau_connector *nv_connector = nouveau_connector(connector);
Ben Skeggs5ed50202013-02-11 20:15:03 +10001387 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs34508f92018-05-08 20:39:47 +10001388 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001389 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs83fc0832011-07-05 13:08:40 +10001390 struct nouveau_encoder *nv_encoder;
1391 struct drm_encoder *encoder;
Ben Skeggs34508f92018-05-08 20:39:47 +10001392 u8 ver, hdr, cnt, len;
1393 u32 data;
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001394 int type, ret;
Ben Skeggs5ed50202013-02-11 20:15:03 +10001395
1396 switch (dcbe->type) {
1397 case DCB_OUTPUT_LVDS: type = DRM_MODE_ENCODER_LVDS; break;
1398 case DCB_OUTPUT_TMDS:
1399 case DCB_OUTPUT_DP:
1400 default:
1401 type = DRM_MODE_ENCODER_TMDS;
1402 break;
1403 }
Ben Skeggs83fc0832011-07-05 13:08:40 +10001404
1405 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1406 if (!nv_encoder)
1407 return -ENOMEM;
1408 nv_encoder->dcb = dcbe;
Ben Skeggsd665c7e2016-11-04 17:20:36 +10001409 nv_encoder->update = nv50_sor_update;
Ben Skeggs83fc0832011-07-05 13:08:40 +10001410
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001411 encoder = to_drm_encoder(nv_encoder);
1412 encoder->possible_crtcs = dcbe->heads;
1413 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10001414 drm_encoder_init(connector->dev, encoder, &nv50_sor_func, type,
1415 "sor-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001416 drm_encoder_helper_add(encoder, &nv50_sor_help);
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001417
1418 drm_mode_connector_attach_encoder(connector, encoder);
1419
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001420 if (dcbe->type == DCB_OUTPUT_DP) {
Ben Skeggs13a86512017-07-19 16:49:59 +10001421 struct nv50_disp *disp = nv50_disp(encoder->dev);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001422 struct nvkm_i2c_aux *aux =
1423 nvkm_i2c_aux_find(i2c, dcbe->i2c_index);
1424 if (aux) {
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10001425 if (disp->disp->object.oclass < GF110_DISP) {
Ben Skeggs13a86512017-07-19 16:49:59 +10001426 /* HW has no support for address-only
1427 * transactions, so we're required to
1428 * use custom I2C-over-AUX code.
1429 */
1430 nv_encoder->i2c = &aux->i2c;
1431 } else {
1432 nv_encoder->i2c = &nv_connector->aux.ddc;
1433 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001434 nv_encoder->aux = aux;
1435 }
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001436
Ben Skeggs34508f92018-05-08 20:39:47 +10001437 if ((data = nvbios_dp_table(bios, &ver, &hdr, &cnt, &len)) &&
1438 ver >= 0x40 && (nvbios_rd08(bios, data + 0x08) & 0x04)) {
Ben Skeggs52aa30f2016-11-04 17:20:36 +10001439 ret = nv50_mstm_new(nv_encoder, &nv_connector->aux, 16,
1440 nv_connector->base.base.id,
1441 &nv_encoder->dp.mstm);
1442 if (ret)
1443 return ret;
1444 }
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001445 } else {
1446 struct nvkm_i2c_bus *bus =
1447 nvkm_i2c_bus_find(i2c, dcbe->i2c_index);
1448 if (bus)
1449 nv_encoder->i2c = &bus->i2c;
1450 }
1451
Ben Skeggs83fc0832011-07-05 13:08:40 +10001452 return 0;
1453}
Ben Skeggs26f6d882011-07-04 16:25:18 +10001454
1455/******************************************************************************
Ben Skeggseb6313a2013-02-11 09:52:58 +10001456 * PIOR
1457 *****************************************************************************/
Ben Skeggs839ca902016-11-04 17:20:36 +10001458static int
1459nv50_pior_atomic_check(struct drm_encoder *encoder,
1460 struct drm_crtc_state *crtc_state,
1461 struct drm_connector_state *conn_state)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001462{
Ben Skeggs839ca902016-11-04 17:20:36 +10001463 int ret = nv50_outp_atomic_check(encoder, crtc_state, conn_state);
1464 if (ret)
1465 return ret;
1466 crtc_state->adjusted_mode.clock *= 2;
1467 return 0;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001468}
1469
1470static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001471nv50_pior_disable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001472{
Ben Skeggsf20c6652016-11-04 17:20:36 +10001473 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
Ben Skeggs0a368772018-05-08 20:39:47 +10001474 struct nv50_core *core = nv50_disp(encoder->dev)->core;
1475 if (nv_encoder->crtc)
1476 core->func->pior->ctrl(core, nv_encoder->or, 0x00000000, NULL);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001477 nv_encoder->crtc = NULL;
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001478 nv50_outp_release(nv_encoder);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001479}
1480
1481static void
Ben Skeggs839ca902016-11-04 17:20:36 +10001482nv50_pior_enable(struct drm_encoder *encoder)
Ben Skeggseb6313a2013-02-11 09:52:58 +10001483{
Ben Skeggseb6313a2013-02-11 09:52:58 +10001484 struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
1485 struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
1486 struct nouveau_connector *nv_connector;
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001487 struct nv50_head_atom *asyh = nv50_head_atom(nv_crtc->base.state);
Ben Skeggs0a368772018-05-08 20:39:47 +10001488 struct nv50_core *core = nv50_disp(encoder->dev)->core;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001489 u8 owner = 1 << nv_crtc->index;
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001490 u8 proto;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001491
Ben Skeggs6c22ea32017-05-19 23:59:35 +10001492 nv50_outp_acquire(nv_encoder);
1493
Ben Skeggseb6313a2013-02-11 09:52:58 +10001494 nv_connector = nouveau_encoder_connector_get(nv_encoder);
1495 switch (nv_connector->base.display_info.bpc) {
Ben Skeggs2ca7fb52018-05-08 20:39:47 +10001496 case 10: asyh->or.depth = 0x6; break;
1497 case 8: asyh->or.depth = 0x5; break;
1498 case 6: asyh->or.depth = 0x2; break;
1499 default: asyh->or.depth = 0x0; break;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001500 }
1501
1502 switch (nv_encoder->dcb->type) {
1503 case DCB_OUTPUT_TMDS:
1504 case DCB_OUTPUT_DP:
1505 proto = 0x0;
1506 break;
1507 default:
Ben Skeggsaf7db032016-03-03 12:56:33 +10001508 BUG();
Ben Skeggseb6313a2013-02-11 09:52:58 +10001509 break;
1510 }
1511
Ben Skeggs0a368772018-05-08 20:39:47 +10001512 core->func->pior->ctrl(core, nv_encoder->or, (proto << 8) | owner, asyh);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001513 nv_encoder->crtc = encoder->crtc;
1514}
1515
Ben Skeggsf20c6652016-11-04 17:20:36 +10001516static const struct drm_encoder_helper_funcs
1517nv50_pior_help = {
Ben Skeggs839ca902016-11-04 17:20:36 +10001518 .atomic_check = nv50_pior_atomic_check,
1519 .enable = nv50_pior_enable,
1520 .disable = nv50_pior_disable,
Ben Skeggsf20c6652016-11-04 17:20:36 +10001521};
Ben Skeggseb6313a2013-02-11 09:52:58 +10001522
1523static void
1524nv50_pior_destroy(struct drm_encoder *encoder)
1525{
1526 drm_encoder_cleanup(encoder);
1527 kfree(encoder);
1528}
1529
Ben Skeggsf20c6652016-11-04 17:20:36 +10001530static const struct drm_encoder_funcs
1531nv50_pior_func = {
Ben Skeggseb6313a2013-02-11 09:52:58 +10001532 .destroy = nv50_pior_destroy,
1533};
1534
1535static int
1536nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
1537{
1538 struct nouveau_drm *drm = nouveau_drm(connector->dev);
Ben Skeggs1167c6b2016-05-18 13:57:42 +10001539 struct nvkm_i2c *i2c = nvxx_i2c(&drm->client.device);
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001540 struct nvkm_i2c_bus *bus = NULL;
1541 struct nvkm_i2c_aux *aux = NULL;
1542 struct i2c_adapter *ddc;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001543 struct nouveau_encoder *nv_encoder;
1544 struct drm_encoder *encoder;
1545 int type;
1546
1547 switch (dcbe->type) {
1548 case DCB_OUTPUT_TMDS:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001549 bus = nvkm_i2c_bus_find(i2c, NVKM_I2C_BUS_EXT(dcbe->extdev));
1550 ddc = bus ? &bus->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001551 type = DRM_MODE_ENCODER_TMDS;
1552 break;
1553 case DCB_OUTPUT_DP:
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001554 aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbe->extdev));
Ben Skeggs62b290f2018-05-08 20:39:47 +10001555 ddc = aux ? &aux->i2c : NULL;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001556 type = DRM_MODE_ENCODER_TMDS;
1557 break;
1558 default:
1559 return -ENODEV;
1560 }
1561
1562 nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
1563 if (!nv_encoder)
1564 return -ENOMEM;
1565 nv_encoder->dcb = dcbe;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001566 nv_encoder->i2c = ddc;
Ben Skeggs2aa5eac2015-08-20 14:54:15 +10001567 nv_encoder->aux = aux;
Ben Skeggseb6313a2013-02-11 09:52:58 +10001568
1569 encoder = to_drm_encoder(nv_encoder);
1570 encoder->possible_crtcs = dcbe->heads;
1571 encoder->possible_clones = 0;
Ben Skeggs5a223da2016-11-04 17:20:36 +10001572 drm_encoder_init(connector->dev, encoder, &nv50_pior_func, type,
1573 "pior-%04x-%04x", dcbe->hasht, dcbe->hashm);
Ben Skeggsf20c6652016-11-04 17:20:36 +10001574 drm_encoder_helper_add(encoder, &nv50_pior_help);
Ben Skeggseb6313a2013-02-11 09:52:58 +10001575
1576 drm_mode_connector_attach_encoder(connector, encoder);
1577 return 0;
1578}
1579
1580/******************************************************************************
Ben Skeggs839ca902016-11-04 17:20:36 +10001581 * Atomic
1582 *****************************************************************************/
1583
1584static void
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001585nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 *interlock)
Ben Skeggs839ca902016-11-04 17:20:36 +10001586{
1587 struct nv50_disp *disp = nv50_disp(drm->dev);
Ben Skeggs09e1b782018-05-08 20:39:47 +10001588 struct nv50_core *core = disp->core;
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001589 struct nv50_mstm *mstm;
1590 struct drm_encoder *encoder;
Ben Skeggs839ca902016-11-04 17:20:36 +10001591
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001592 NV_ATOMIC(drm, "commit core %08x\n", interlock[NV50_DISP_INTERLOCK_BASE]);
Ben Skeggs839ca902016-11-04 17:20:36 +10001593
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001594 drm_for_each_encoder(encoder, drm->dev) {
1595 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1596 mstm = nouveau_encoder(encoder)->dp.mstm;
1597 if (mstm && mstm->modified)
1598 nv50_mstm_prepare(mstm);
1599 }
1600 }
1601
Ben Skeggs09e1b782018-05-08 20:39:47 +10001602 core->func->ntfy_init(disp->sync, NV50_DISP_CORE_NTFY);
1603 core->func->update(core, interlock, true);
1604 if (core->func->ntfy_wait_done(disp->sync, NV50_DISP_CORE_NTFY,
1605 disp->core->chan.base.device))
1606 NV_ERROR(drm, "core notifier timeout\n");
Ben Skeggsf479c0b2016-11-04 17:20:36 +10001607
1608 drm_for_each_encoder(encoder, drm->dev) {
1609 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
1610 mstm = nouveau_encoder(encoder)->dp.mstm;
1611 if (mstm && mstm->modified)
1612 nv50_mstm_cleanup(mstm);
1613 }
1614 }
Ben Skeggs839ca902016-11-04 17:20:36 +10001615}
1616
1617static void
1618nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
1619{
1620 struct drm_device *dev = state->dev;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001621 struct drm_crtc_state *new_crtc_state, *old_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001622 struct drm_crtc *crtc;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001623 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001624 struct drm_plane *plane;
1625 struct nouveau_drm *drm = nouveau_drm(dev);
1626 struct nv50_disp *disp = nv50_disp(dev);
1627 struct nv50_atom *atom = nv50_atom(state);
1628 struct nv50_outp_atom *outp, *outt;
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001629 u32 interlock[NV50_DISP_INTERLOCK__SIZE] = {};
Ben Skeggs839ca902016-11-04 17:20:36 +10001630 int i;
1631
1632 NV_ATOMIC(drm, "commit %d %d\n", atom->lock_core, atom->flush_disable);
1633 drm_atomic_helper_wait_for_fences(dev, state, false);
1634 drm_atomic_helper_wait_for_dependencies(state);
1635 drm_atomic_helper_update_legacy_modeset_state(dev, state);
1636
1637 if (atom->lock_core)
1638 mutex_lock(&disp->mutex);
1639
1640 /* Disable head(s). */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001641 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001642 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001643 struct nv50_head *head = nv50_head(crtc);
1644
1645 NV_ATOMIC(drm, "%s: clr %04x (set %04x)\n", crtc->name,
1646 asyh->clr.mask, asyh->set.mask);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001647 if (old_crtc_state->active && !new_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001648 drm_crtc_vblank_off(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001649
1650 if (asyh->clr.mask) {
1651 nv50_head_flush_clr(head, asyh, atom->flush_disable);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001652 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001653 }
1654 }
1655
1656 /* Disable plane(s). */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001657 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1658 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001659 struct nv50_wndw *wndw = nv50_wndw(plane);
1660
1661 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", plane->name,
1662 asyw->clr.mask, asyw->set.mask);
1663 if (!asyw->clr.mask)
1664 continue;
1665
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001666 nv50_wndw_flush_clr(wndw, interlock, atom->flush_disable, asyw);
Ben Skeggs839ca902016-11-04 17:20:36 +10001667 }
1668
1669 /* Disable output path(s). */
1670 list_for_each_entry(outp, &atom->outp, head) {
1671 const struct drm_encoder_helper_funcs *help;
1672 struct drm_encoder *encoder;
1673
1674 encoder = outp->encoder;
1675 help = encoder->helper_private;
1676
1677 NV_ATOMIC(drm, "%s: clr %02x (set %02x)\n", encoder->name,
1678 outp->clr.mask, outp->set.mask);
1679
1680 if (outp->clr.mask) {
1681 help->disable(encoder);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001682 interlock[NV50_DISP_INTERLOCK_CORE] |= 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001683 if (outp->flush_disable) {
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001684 nv50_disp_atomic_commit_core(drm, interlock);
1685 memset(interlock, 0x00, sizeof(interlock));
Ben Skeggs839ca902016-11-04 17:20:36 +10001686 }
1687 }
1688 }
1689
1690 /* Flush disable. */
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001691 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001692 if (atom->flush_disable) {
Ben Skeggs04fc14b2018-05-08 20:39:47 +10001693 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1694 struct nv50_wndw *wndw = nv50_wndw(plane);
1695 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1696 if (wndw->func->update)
1697 wndw->func->update(wndw, interlock);
1698 }
1699 }
1700
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001701 nv50_disp_atomic_commit_core(drm, interlock);
1702 memset(interlock, 0x00, sizeof(interlock));
Ben Skeggs839ca902016-11-04 17:20:36 +10001703 }
1704 }
1705
1706 /* Update output path(s). */
1707 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
1708 const struct drm_encoder_helper_funcs *help;
1709 struct drm_encoder *encoder;
1710
1711 encoder = outp->encoder;
1712 help = encoder->helper_private;
1713
1714 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", encoder->name,
1715 outp->set.mask, outp->clr.mask);
1716
1717 if (outp->set.mask) {
1718 help->enable(encoder);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001719 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001720 }
1721
1722 list_del(&outp->head);
1723 kfree(outp);
1724 }
1725
1726 /* Update head(s). */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001727 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001728 struct nv50_head_atom *asyh = nv50_head_atom(new_crtc_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001729 struct nv50_head *head = nv50_head(crtc);
1730
1731 NV_ATOMIC(drm, "%s: set %04x (clr %04x)\n", crtc->name,
1732 asyh->set.mask, asyh->clr.mask);
1733
1734 if (asyh->set.mask) {
1735 nv50_head_flush_set(head, asyh);
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001736 interlock[NV50_DISP_INTERLOCK_CORE] = 1;
Ben Skeggs839ca902016-11-04 17:20:36 +10001737 }
Ben Skeggs839ca902016-11-04 17:20:36 +10001738
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001739 if (new_crtc_state->active) {
1740 if (!old_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001741 drm_crtc_vblank_on(crtc);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001742 if (new_crtc_state->event)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001743 drm_crtc_vblank_get(crtc);
1744 }
Ben Skeggs2b507892017-01-24 09:32:26 +10001745 }
1746
Ben Skeggs839ca902016-11-04 17:20:36 +10001747 /* Update plane(s). */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001748 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1749 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001750 struct nv50_wndw *wndw = nv50_wndw(plane);
1751
1752 NV_ATOMIC(drm, "%s: set %02x (clr %02x)\n", plane->name,
1753 asyw->set.mask, asyw->clr.mask);
1754 if ( !asyw->set.mask &&
1755 (!asyw->clr.mask || atom->flush_disable))
1756 continue;
1757
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001758 nv50_wndw_flush_set(wndw, interlock, asyw);
Ben Skeggs839ca902016-11-04 17:20:36 +10001759 }
1760
1761 /* Flush update. */
Ben Skeggs04fc14b2018-05-08 20:39:47 +10001762 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1763 struct nv50_wndw *wndw = nv50_wndw(plane);
1764 if (interlock[wndw->interlock.type] & wndw->interlock.data) {
1765 if (wndw->func->update)
1766 wndw->func->update(wndw, interlock);
1767 }
1768 }
1769
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001770 if (interlock[NV50_DISP_INTERLOCK_CORE]) {
1771 if (interlock[NV50_DISP_INTERLOCK_BASE] ||
1772 !atom->state.legacy_cursor_update)
1773 nv50_disp_atomic_commit_core(drm, interlock);
Ben Skeggs09e1b782018-05-08 20:39:47 +10001774 else
Ben Skeggs53e0a3e2018-05-08 20:39:47 +10001775 disp->core->func->update(disp->core, interlock, false);
Ben Skeggs839ca902016-11-04 17:20:36 +10001776 }
1777
1778 if (atom->lock_core)
1779 mutex_unlock(&disp->mutex);
1780
1781 /* Wait for HW to signal completion. */
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001782 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1783 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001784 struct nv50_wndw *wndw = nv50_wndw(plane);
1785 int ret = nv50_wndw_wait_armed(wndw, asyw);
1786 if (ret)
1787 NV_ERROR(drm, "%s: timeout\n", plane->name);
1788 }
1789
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001790 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
1791 if (new_crtc_state->event) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001792 unsigned long flags;
Mario Kleinerbd9f6602016-11-23 07:58:54 +01001793 /* Get correct count/ts if racing with vblank irq */
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001794 if (new_crtc_state->active)
Dave Airlie0c697fa2017-08-15 16:16:58 +10001795 drm_crtc_accurate_vblank_count(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001796 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001797 drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
Ben Skeggs839ca902016-11-04 17:20:36 +10001798 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001799
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001800 new_crtc_state->event = NULL;
Maarten Lankhorstefa47932017-08-15 10:52:50 +02001801 if (new_crtc_state->active)
Ben Skeggs4a5431a2017-07-24 11:01:52 +10001802 drm_crtc_vblank_put(crtc);
Ben Skeggs839ca902016-11-04 17:20:36 +10001803 }
1804 }
1805
1806 drm_atomic_helper_commit_hw_done(state);
1807 drm_atomic_helper_cleanup_planes(dev, state);
1808 drm_atomic_helper_commit_cleanup_done(state);
1809 drm_atomic_state_put(state);
1810}
1811
1812static void
1813nv50_disp_atomic_commit_work(struct work_struct *work)
1814{
1815 struct drm_atomic_state *state =
1816 container_of(work, typeof(*state), commit_work);
1817 nv50_disp_atomic_commit_tail(state);
1818}
1819
1820static int
1821nv50_disp_atomic_commit(struct drm_device *dev,
1822 struct drm_atomic_state *state, bool nonblock)
1823{
1824 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsd324c5b2017-11-01 09:12:25 +10001825 struct drm_plane_state *new_plane_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001826 struct drm_plane *plane;
1827 struct drm_crtc *crtc;
1828 bool active = false;
1829 int ret, i;
1830
1831 ret = pm_runtime_get_sync(dev->dev);
1832 if (ret < 0 && ret != -EACCES)
1833 return ret;
1834
1835 ret = drm_atomic_helper_setup_commit(state, nonblock);
1836 if (ret)
1837 goto done;
1838
1839 INIT_WORK(&state->commit_work, nv50_disp_atomic_commit_work);
1840
1841 ret = drm_atomic_helper_prepare_planes(dev, state);
1842 if (ret)
1843 goto done;
1844
1845 if (!nonblock) {
1846 ret = drm_atomic_helper_wait_for_fences(dev, state, true);
1847 if (ret)
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001848 goto err_cleanup;
Ben Skeggs839ca902016-11-04 17:20:36 +10001849 }
1850
Maarten Lankhorst85726362017-07-11 16:33:05 +02001851 ret = drm_atomic_helper_swap_state(state, true);
1852 if (ret)
1853 goto err_cleanup;
1854
Ben Skeggsd324c5b2017-11-01 09:12:25 +10001855 for_each_new_plane_in_state(state, plane, new_plane_state, i) {
1856 struct nv50_wndw_atom *asyw = nv50_wndw_atom(new_plane_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001857 struct nv50_wndw *wndw = nv50_wndw(plane);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001858
Ben Skeggsccd27db2018-05-08 20:39:47 +10001859 if (asyw->set.image)
1860 nv50_wndw_ntfy_enable(wndw, asyw);
Ben Skeggs839ca902016-11-04 17:20:36 +10001861 }
1862
Ben Skeggs839ca902016-11-04 17:20:36 +10001863 drm_atomic_state_get(state);
1864
1865 if (nonblock)
1866 queue_work(system_unbound_wq, &state->commit_work);
1867 else
1868 nv50_disp_atomic_commit_tail(state);
1869
1870 drm_for_each_crtc(crtc, dev) {
1871 if (crtc->state->enable) {
1872 if (!drm->have_disp_power_ref) {
1873 drm->have_disp_power_ref = true;
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001874 return 0;
Ben Skeggs839ca902016-11-04 17:20:36 +10001875 }
1876 active = true;
1877 break;
1878 }
1879 }
1880
1881 if (!active && drm->have_disp_power_ref) {
1882 pm_runtime_put_autosuspend(dev->dev);
1883 drm->have_disp_power_ref = false;
1884 }
1885
Maarten Lankhorst813a7e12017-07-11 16:33:03 +02001886err_cleanup:
1887 if (ret)
1888 drm_atomic_helper_cleanup_planes(dev, state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001889done:
1890 pm_runtime_put_autosuspend(dev->dev);
1891 return ret;
1892}
1893
1894static struct nv50_outp_atom *
1895nv50_disp_outp_atomic_add(struct nv50_atom *atom, struct drm_encoder *encoder)
1896{
1897 struct nv50_outp_atom *outp;
1898
1899 list_for_each_entry(outp, &atom->outp, head) {
1900 if (outp->encoder == encoder)
1901 return outp;
1902 }
1903
1904 outp = kzalloc(sizeof(*outp), GFP_KERNEL);
1905 if (!outp)
1906 return ERR_PTR(-ENOMEM);
1907
1908 list_add(&outp->head, &atom->outp);
1909 outp->encoder = encoder;
1910 return outp;
1911}
1912
1913static int
1914nv50_disp_outp_atomic_check_clr(struct nv50_atom *atom,
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001915 struct drm_connector_state *old_connector_state)
Ben Skeggs839ca902016-11-04 17:20:36 +10001916{
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001917 struct drm_encoder *encoder = old_connector_state->best_encoder;
1918 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001919 struct drm_crtc *crtc;
1920 struct nv50_outp_atom *outp;
1921
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001922 if (!(crtc = old_connector_state->crtc))
Ben Skeggs839ca902016-11-04 17:20:36 +10001923 return 0;
1924
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001925 old_crtc_state = drm_atomic_get_old_crtc_state(&atom->state, crtc);
1926 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1927 if (old_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001928 outp = nv50_disp_outp_atomic_add(atom, encoder);
1929 if (IS_ERR(outp))
1930 return PTR_ERR(outp);
1931
1932 if (outp->encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
1933 outp->flush_disable = true;
1934 atom->flush_disable = true;
1935 }
1936 outp->clr.ctrl = true;
1937 atom->lock_core = true;
1938 }
1939
1940 return 0;
1941}
1942
1943static int
1944nv50_disp_outp_atomic_check_set(struct nv50_atom *atom,
1945 struct drm_connector_state *connector_state)
1946{
1947 struct drm_encoder *encoder = connector_state->best_encoder;
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001948 struct drm_crtc_state *new_crtc_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001949 struct drm_crtc *crtc;
1950 struct nv50_outp_atom *outp;
1951
1952 if (!(crtc = connector_state->crtc))
1953 return 0;
1954
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001955 new_crtc_state = drm_atomic_get_new_crtc_state(&atom->state, crtc);
1956 if (new_crtc_state->active && drm_atomic_crtc_needs_modeset(new_crtc_state)) {
Ben Skeggs839ca902016-11-04 17:20:36 +10001957 outp = nv50_disp_outp_atomic_add(atom, encoder);
1958 if (IS_ERR(outp))
1959 return PTR_ERR(outp);
1960
1961 outp->set.ctrl = true;
1962 atom->lock_core = true;
1963 }
1964
1965 return 0;
1966}
1967
1968static int
1969nv50_disp_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
1970{
1971 struct nv50_atom *atom = nv50_atom(state);
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001972 struct drm_connector_state *old_connector_state, *new_connector_state;
Ben Skeggs839ca902016-11-04 17:20:36 +10001973 struct drm_connector *connector;
1974 int ret, i;
1975
1976 ret = drm_atomic_helper_check(dev, state);
1977 if (ret)
1978 return ret;
1979
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001980 for_each_oldnew_connector_in_state(state, connector, old_connector_state, new_connector_state, i) {
1981 ret = nv50_disp_outp_atomic_check_clr(atom, old_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001982 if (ret)
1983 return ret;
1984
Maarten Lankhorst3c847d62017-07-19 16:39:19 +02001985 ret = nv50_disp_outp_atomic_check_set(atom, new_connector_state);
Ben Skeggs839ca902016-11-04 17:20:36 +10001986 if (ret)
1987 return ret;
1988 }
1989
1990 return 0;
1991}
1992
1993static void
1994nv50_disp_atomic_state_clear(struct drm_atomic_state *state)
1995{
1996 struct nv50_atom *atom = nv50_atom(state);
1997 struct nv50_outp_atom *outp, *outt;
1998
1999 list_for_each_entry_safe(outp, outt, &atom->outp, head) {
2000 list_del(&outp->head);
2001 kfree(outp);
2002 }
2003
2004 drm_atomic_state_default_clear(state);
2005}
2006
2007static void
2008nv50_disp_atomic_state_free(struct drm_atomic_state *state)
2009{
2010 struct nv50_atom *atom = nv50_atom(state);
2011 drm_atomic_state_default_release(&atom->state);
2012 kfree(atom);
2013}
2014
2015static struct drm_atomic_state *
2016nv50_disp_atomic_state_alloc(struct drm_device *dev)
2017{
2018 struct nv50_atom *atom;
2019 if (!(atom = kzalloc(sizeof(*atom), GFP_KERNEL)) ||
2020 drm_atomic_state_init(dev, &atom->state) < 0) {
2021 kfree(atom);
2022 return NULL;
2023 }
2024 INIT_LIST_HEAD(&atom->outp);
2025 return &atom->state;
2026}
2027
2028static const struct drm_mode_config_funcs
2029nv50_disp_func = {
2030 .fb_create = nouveau_user_framebuffer_create,
Noralf Trønnesd0f54f52017-12-05 19:25:00 +01002031 .output_poll_changed = drm_fb_helper_output_poll_changed,
Ben Skeggs839ca902016-11-04 17:20:36 +10002032 .atomic_check = nv50_disp_atomic_check,
2033 .atomic_commit = nv50_disp_atomic_commit,
2034 .atomic_state_alloc = nv50_disp_atomic_state_alloc,
2035 .atomic_state_clear = nv50_disp_atomic_state_clear,
2036 .atomic_state_free = nv50_disp_atomic_state_free,
2037};
2038
2039/******************************************************************************
Ben Skeggs26f6d882011-07-04 16:25:18 +10002040 * Init
2041 *****************************************************************************/
Ben Skeggsab0af552014-08-10 04:10:19 +10002042
Ben Skeggs2a44e492011-11-09 11:36:33 +10002043void
Ben Skeggse225f442012-11-21 14:40:21 +10002044nv50_display_fini(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002045{
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002046 struct nouveau_encoder *nv_encoder;
2047 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002048 struct drm_plane *plane;
2049
2050 drm_for_each_plane(plane, dev) {
2051 struct nv50_wndw *wndw = nv50_wndw(plane);
2052 if (plane->funcs != &nv50_wndw)
2053 continue;
2054 nv50_wndw_fini(wndw);
2055 }
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002056
2057 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2058 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
2059 nv_encoder = nouveau_encoder(encoder);
2060 nv50_mstm_fini(nv_encoder->dp.mstm);
2061 }
2062 }
Ben Skeggs26f6d882011-07-04 16:25:18 +10002063}
2064
2065int
Ben Skeggse225f442012-11-21 14:40:21 +10002066nv50_display_init(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002067{
Ben Skeggs09e1b782018-05-08 20:39:47 +10002068 struct nv50_core *core = nv50_disp(dev)->core;
Ben Skeggs354d3502016-11-04 17:20:36 +10002069 struct drm_encoder *encoder;
Ben Skeggs973f10c2016-11-04 17:20:36 +10002070 struct drm_plane *plane;
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002071
Ben Skeggs09e1b782018-05-08 20:39:47 +10002072 core->func->init(core);
Ben Skeggs973f10c2016-11-04 17:20:36 +10002073
Ben Skeggs354d3502016-11-04 17:20:36 +10002074 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2075 if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
Ben Skeggs9c5753b2017-05-19 23:59:35 +10002076 struct nouveau_encoder *nv_encoder =
2077 nouveau_encoder(encoder);
Ben Skeggsf479c0b2016-11-04 17:20:36 +10002078 nv50_mstm_init(nv_encoder->dp.mstm);
Ben Skeggs354d3502016-11-04 17:20:36 +10002079 }
2080 }
2081
Ben Skeggs973f10c2016-11-04 17:20:36 +10002082 drm_for_each_plane(plane, dev) {
2083 struct nv50_wndw *wndw = nv50_wndw(plane);
2084 if (plane->funcs != &nv50_wndw)
2085 continue;
2086 nv50_wndw_init(wndw);
2087 }
2088
Ben Skeggs9f9bdaa2013-03-02 13:21:31 +10002089 return 0;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002090}
2091
2092void
Ben Skeggse225f442012-11-21 14:40:21 +10002093nv50_display_destroy(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002094{
Ben Skeggse225f442012-11-21 14:40:21 +10002095 struct nv50_disp *disp = nv50_disp(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002096
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10002097 nv50_core_del(&disp->core);
Ben Skeggsbdb8c212011-11-12 01:30:24 +10002098
Ben Skeggs816af2f2011-11-16 15:48:48 +10002099 nouveau_bo_unmap(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002100 if (disp->sync)
2101 nouveau_bo_unpin(disp->sync);
Ben Skeggs816af2f2011-11-16 15:48:48 +10002102 nouveau_bo_ref(NULL, &disp->sync);
Ben Skeggs51beb422011-07-05 10:33:08 +10002103
Ben Skeggs77145f12012-07-31 16:16:21 +10002104 nouveau_display(dev)->priv = NULL;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002105 kfree(disp);
2106}
2107
Ben Skeggs839ca902016-11-04 17:20:36 +10002108MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
2109static int nouveau_atomic = 0;
2110module_param_named(atomic, nouveau_atomic, int, 0400);
2111
Ben Skeggs26f6d882011-07-04 16:25:18 +10002112int
Ben Skeggse225f442012-11-21 14:40:21 +10002113nv50_display_create(struct drm_device *dev)
Ben Skeggs26f6d882011-07-04 16:25:18 +10002114{
Ben Skeggs1167c6b2016-05-18 13:57:42 +10002115 struct nvif_device *device = &nouveau_drm(dev)->client.device;
Ben Skeggs77145f12012-07-31 16:16:21 +10002116 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10002117 struct dcb_table *dcb = &drm->vbios.dcb;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002118 struct drm_connector *connector, *tmp;
Ben Skeggse225f442012-11-21 14:40:21 +10002119 struct nv50_disp *disp;
Ben Skeggscb75d972012-07-11 10:44:20 +10002120 struct dcb_output *dcbe;
Ben Skeggs7c5f6a82012-03-04 16:25:59 +10002121 int crtcs, ret, i;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002122
2123 disp = kzalloc(sizeof(*disp), GFP_KERNEL);
2124 if (!disp)
2125 return -ENOMEM;
Ben Skeggs77145f12012-07-31 16:16:21 +10002126
Ben Skeggs839ca902016-11-04 17:20:36 +10002127 mutex_init(&disp->mutex);
2128
Ben Skeggs77145f12012-07-31 16:16:21 +10002129 nouveau_display(dev)->priv = disp;
Ben Skeggse225f442012-11-21 14:40:21 +10002130 nouveau_display(dev)->dtor = nv50_display_destroy;
2131 nouveau_display(dev)->init = nv50_display_init;
2132 nouveau_display(dev)->fini = nv50_display_fini;
Ben Skeggs0ad72862014-08-10 04:10:22 +10002133 disp->disp = &nouveau_display(dev)->disp;
Ben Skeggs839ca902016-11-04 17:20:36 +10002134 dev->mode_config.funcs = &nv50_disp_func;
Ilia Mirkinc20bb152018-02-03 14:11:23 -05002135 dev->driver->driver_features |= DRIVER_PREFER_XBGR_30BPP;
Ben Skeggs839ca902016-11-04 17:20:36 +10002136 if (nouveau_atomic)
2137 dev->driver->driver_features |= DRIVER_ATOMIC;
Ben Skeggs26f6d882011-07-04 16:25:18 +10002138
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002139 /* small shared memory area we use for notifiers and semaphores */
Ben Skeggsbab7cc12016-05-24 17:26:48 +10002140 ret = nouveau_bo_new(&drm->client, 4096, 0x1000, TTM_PL_FLAG_VRAM,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +01002141 0, 0x0000, NULL, NULL, &disp->sync);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002142 if (!ret) {
Ben Skeggs547ad072014-11-10 12:35:06 +10002143 ret = nouveau_bo_pin(disp->sync, TTM_PL_FLAG_VRAM, true);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002144 if (!ret) {
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002145 ret = nouveau_bo_map(disp->sync);
Marcin Slusarz04c8c212012-11-25 23:04:23 +01002146 if (ret)
2147 nouveau_bo_unpin(disp->sync);
2148 }
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002149 if (ret)
2150 nouveau_bo_ref(NULL, &disp->sync);
2151 }
2152
2153 if (ret)
2154 goto out;
2155
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002156 /* allocate master evo channel */
Ben Skeggs9ca6f1e2018-05-08 20:39:47 +10002157 ret = nv50_core_new(drm, &disp->core);
Ben Skeggsb5a794b2012-10-16 14:18:32 +10002158 if (ret)
2159 goto out;
2160
Ben Skeggs438d99e2011-07-05 16:48:06 +10002161 /* create crtc objects to represent the hw heads */
Ben Skeggs0d4a2c52018-05-08 20:39:47 +10002162 if (disp->disp->object.oclass >= GF110_DISP)
Ilia Mirkineba5e562017-07-03 13:06:26 -04002163 crtcs = nvif_rd32(&device->object, 0x612004) & 0xf;
Ben Skeggs63718a02012-11-16 11:44:14 +10002164 else
Ilia Mirkineba5e562017-07-03 13:06:26 -04002165 crtcs = 0x3;
Ben Skeggs63718a02012-11-16 11:44:14 +10002166
Ilia Mirkineba5e562017-07-03 13:06:26 -04002167 for (i = 0; i < fls(crtcs); i++) {
2168 if (!(crtcs & (1 << i)))
2169 continue;
Ben Skeggs9bfdee92016-11-04 17:20:36 +10002170 ret = nv50_head_create(dev, i);
Ben Skeggs438d99e2011-07-05 16:48:06 +10002171 if (ret)
2172 goto out;
2173 }
2174
Ben Skeggs83fc0832011-07-05 13:08:40 +10002175 /* create encoder/connector objects based on VBIOS DCB table */
2176 for (i = 0, dcbe = &dcb->entry[0]; i < dcb->entries; i++, dcbe++) {
2177 connector = nouveau_connector_create(dev, dcbe->connector);
2178 if (IS_ERR(connector))
2179 continue;
2180
Ben Skeggseb6313a2013-02-11 09:52:58 +10002181 if (dcbe->location == DCB_LOC_ON_CHIP) {
2182 switch (dcbe->type) {
2183 case DCB_OUTPUT_TMDS:
2184 case DCB_OUTPUT_LVDS:
2185 case DCB_OUTPUT_DP:
2186 ret = nv50_sor_create(connector, dcbe);
2187 break;
2188 case DCB_OUTPUT_ANALOG:
2189 ret = nv50_dac_create(connector, dcbe);
2190 break;
2191 default:
2192 ret = -ENODEV;
2193 break;
2194 }
2195 } else {
2196 ret = nv50_pior_create(connector, dcbe);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002197 }
2198
Ben Skeggseb6313a2013-02-11 09:52:58 +10002199 if (ret) {
2200 NV_WARN(drm, "failed to create encoder %d/%d/%d: %d\n",
2201 dcbe->location, dcbe->type,
2202 ffs(dcbe->or) - 1, ret);
Ben Skeggs94f54f52013-03-05 22:26:06 +10002203 ret = 0;
Ben Skeggs83fc0832011-07-05 13:08:40 +10002204 }
2205 }
2206
2207 /* cull any connectors we created that don't have an encoder */
2208 list_for_each_entry_safe(connector, tmp, &dev->mode_config.connector_list, head) {
2209 if (connector->encoder_ids[0])
2210 continue;
2211
Ben Skeggs77145f12012-07-31 16:16:21 +10002212 NV_WARN(drm, "%s has no encoders, removing\n",
Jani Nikula8c6c3612014-06-03 14:56:18 +03002213 connector->name);
Ben Skeggs83fc0832011-07-05 13:08:40 +10002214 connector->funcs->destroy(connector);
2215 }
2216
Ben Skeggs26f6d882011-07-04 16:25:18 +10002217out:
2218 if (ret)
Ben Skeggse225f442012-11-21 14:40:21 +10002219 nv50_display_destroy(dev);
Ben Skeggs26f6d882011-07-04 16:25:18 +10002220 return ret;
2221}