]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - drivers/gpu/drm/nouveau/nv50_display.c
drm: Nuke fb->pixel_format
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / nouveau / nv50_display.c
index 8d48ee47514939365df2100259ab797dbe6e6017..cb85cb72dc1c30cb5114a3f31ecaf3b508e5aa22 100644 (file)
@@ -418,12 +418,12 @@ nv50_dmac_ctxdma_del(struct nv50_dmac_ctxdma *ctxdma)
 }
 
 static struct nv50_dmac_ctxdma *
-nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, u32 handle,
-                    struct nouveau_framebuffer *fb)
+nv50_dmac_ctxdma_new(struct nv50_dmac *dmac, struct nouveau_framebuffer *fb)
 {
        struct nouveau_drm *drm = nouveau_drm(fb->base.dev);
        struct nv50_dmac_ctxdma *ctxdma;
-       const u8  kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
+       const u8    kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
+       const u32 handle = 0xfb000000 | kind;
        struct {
                struct nv_dma_v0 base;
                union {
@@ -574,7 +574,7 @@ nv50_core_create(struct nvif_device *device, struct nvif_object *disp,
                .pushbuf = 0xb0007d00,
        };
        static const s32 oclass[] = {
-               GP104_DISP_CORE_CHANNEL_DMA,
+               GP102_DISP_CORE_CHANNEL_DMA,
                GP100_DISP_CORE_CHANNEL_DMA,
                GM200_DISP_CORE_CHANNEL_DMA,
                GM107_DISP_CORE_CHANNEL_DMA,
@@ -658,11 +658,8 @@ nv50_ovly_create(struct nvif_device *device, struct nvif_object *disp,
 
 struct nv50_head {
        struct nouveau_crtc base;
-       struct nouveau_bo *image;
        struct nv50_ovly ovly;
        struct nv50_oimm oimm;
-
-       struct nv50_base *_base;
 };
 
 #define nv50_head(c) ((struct nv50_head *)nouveau_crtc(c))
@@ -740,40 +737,6 @@ evo_kick(u32 *push, void *evoc)
        *((p)++) = _d;                                                         \
 } while(0)
 
-static bool
-evo_sync_wait(void *data)
-{
-       if (nouveau_bo_rd32(data, EVO_MAST_NTFY) != 0x00000000)
-               return true;
-       usleep_range(1, 2);
-       return false;
-}
-
-static int
-evo_sync(struct drm_device *dev)
-{
-       struct nvif_device *device = &nouveau_drm(dev)->device;
-       struct nv50_disp *disp = nv50_disp(dev);
-       struct nv50_mast *mast = nv50_mast(dev);
-       u32 *push = evo_wait(mast, 8);
-       if (push) {
-               nouveau_bo_wr32(disp->sync, EVO_MAST_NTFY, 0x00000000);
-               evo_mthd(push, 0x0084, 1);
-               evo_data(push, 0x80000000 | EVO_MAST_NTFY);
-               evo_mthd(push, 0x0080, 2);
-               evo_data(push, 0x00000000);
-               evo_data(push, 0x00000000);
-               evo_kick(push, mast);
-               if (nvif_msec(device, 2000,
-                       if (evo_sync_wait(disp->sync))
-                               break;
-               ) >= 0)
-                       return 0;
-       }
-
-       return -EBUSY;
-}
-
 /******************************************************************************
  * Plane
  *****************************************************************************/
@@ -789,8 +752,6 @@ struct nv50_wndw {
        u16 ntfy;
        u16 sema;
        u32 data;
-
-       struct nv50_wndw_atom asy;
 };
 
 struct nv50_wndw_func {
@@ -990,21 +951,17 @@ nv50_wndw_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
        struct nv50_wndw_atom *asyw = nv50_wndw_atom(state);
        struct nv50_head_atom *asyh;
        struct nv50_dmac_ctxdma *ctxdma;
-       u32 name;
-       u8 kind;
        int ret;
 
        NV_ATOMIC(drm, "%s prepare: %p\n", plane->name, state->fb);
        if (!asyw->state.fb)
                return 0;
-       kind = (fb->nvbo->tile_flags & 0x0000ff00) >> 8;
-       name = 0xfb000000 | kind;
 
        ret = nouveau_bo_pin(fb->nvbo, TTM_PL_FLAG_VRAM, true);
        if (ret)
                return ret;
 
-       ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, name, fb);
+       ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, fb);
        if (IS_ERR(ctxdma)) {
                nouveau_bo_unpin(fb->nvbo);
                return PTR_ERR(ctxdma);
@@ -1196,7 +1153,7 @@ nv50_curs_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
        if (asyw->state.fb->width != asyw->state.fb->height)
                return -EINVAL;
 
-       switch (asyw->state.fb->pixel_format) {
+       switch (asyw->state.fb->format->format) {
        case DRM_FORMAT_ARGB8888: asyh->curs.format = 1; break;
        default:
                WARN_ON(1);
@@ -1461,12 +1418,10 @@ static int
 nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
                  struct nv50_head_atom *asyh)
 {
-       const u32 format = asyw->state.fb->pixel_format;
-       const struct drm_format_info *info;
+       const struct drm_framebuffer *fb = asyw->state.fb;
        int ret;
 
-       info = drm_format_info(format);
-       if (!info || !info->depth)
+       if (!fb->format->depth)
                return -EINVAL;
 
        ret = drm_plane_helper_check_state(&asyw->state, &asyw->clip,
@@ -1476,14 +1431,14 @@ nv50_base_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
        if (ret)
                return ret;
 
-       asyh->base.depth = info->depth;
-       asyh->base.cpp = info->cpp[0];
+       asyh->base.depth = fb->format->depth;
+       asyh->base.cpp = fb->format->cpp[0];
        asyh->base.x = asyw->state.src.x1 >> 16;
        asyh->base.y = asyw->state.src.y1 >> 16;
        asyh->base.w = asyw->state.fb->width;
        asyh->base.h = asyw->state.fb->height;
 
-       switch (format) {
+       switch (fb->format->format) {
        case DRM_FORMAT_C8         : asyw->image.format = 0x1e; break;
        case DRM_FORMAT_RGB565     : asyw->image.format = 0xe8; break;
        case DRM_FORMAT_XRGB1555   :
@@ -1581,151 +1536,6 @@ nv50_base_new(struct nouveau_drm *drm, struct nv50_head *head,
                                &base->wndw.notify);
 }
 
-/******************************************************************************
- * Page flipping channel
- *****************************************************************************/
-struct nouveau_bo *
-nv50_display_crtc_sema(struct drm_device *dev, int crtc)
-{
-       return nv50_disp(dev)->sync;
-}
-
-struct nv50_display_flip {
-       struct nv50_disp *disp;
-       struct nv50_base *base;
-};
-
-static bool
-nv50_display_flip_wait(void *data)
-{
-       struct nv50_display_flip *flip = data;
-       if (nouveau_bo_rd32(flip->disp->sync, flip->base->wndw.sema / 4) ==
-                                             flip->base->wndw.data)
-               return true;
-       usleep_range(1, 2);
-       return false;
-}
-
-void
-nv50_display_flip_stop(struct drm_crtc *crtc)
-{
-       struct nvif_device *device = &nouveau_drm(crtc->dev)->device;
-       struct nv50_base *base = nv50_head(crtc)->_base;
-       struct nv50_wndw *wndw = &base->wndw;
-       struct nv50_wndw_atom *asyw = &wndw->asy;
-       struct nv50_display_flip flip = {
-               .disp = nv50_disp(crtc->dev),
-               .base = base,
-       };
-
-       asyw->state.crtc = NULL;
-       asyw->state.fb = NULL;
-       nv50_wndw_atomic_check(&wndw->plane, &asyw->state);
-       nv50_wndw_flush_clr(wndw, 0, true, asyw);
-
-       nvif_msec(device, 2000,
-               if (nv50_display_flip_wait(&flip))
-                       break;
-       );
-}
-
-int
-nv50_display_flip_next(struct drm_crtc *crtc, struct drm_framebuffer *fb,
-                      struct nouveau_channel *chan, u32 swap_interval)
-{
-       struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
-       struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
-       struct nv50_head *head = nv50_head(crtc);
-       struct nv50_base *base = nv50_head(crtc)->_base;
-       struct nv50_wndw *wndw = &base->wndw;
-       struct nv50_wndw_atom *asyw = &wndw->asy;
-       int ret;
-
-       if (crtc->primary->fb->width != fb->width ||
-           crtc->primary->fb->height != fb->height)
-               return -EINVAL;
-
-       if (chan == NULL)
-               evo_sync(crtc->dev);
-
-       if (chan && chan->user.oclass < G82_CHANNEL_GPFIFO) {
-               ret = RING_SPACE(chan, 8);
-               if (ret)
-                       return ret;
-
-               BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
-               OUT_RING  (chan, NvEvoSema0 + nv_crtc->index);
-               OUT_RING  (chan, base->wndw.sema ^ 0x10);
-               BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
-               OUT_RING  (chan, base->wndw.data + 1);
-               BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_OFFSET, 2);
-               OUT_RING  (chan, base->wndw.sema);
-               OUT_RING  (chan, base->wndw.data);
-       } else
-       if (chan && chan->user.oclass < FERMI_CHANNEL_GPFIFO) {
-               u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + base->wndw.sema;
-               ret = RING_SPACE(chan, 12);
-               if (ret)
-                       return ret;
-
-               BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
-               OUT_RING  (chan, chan->vram.handle);
-               BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
-               OUT_RING  (chan, upper_32_bits(addr ^ 0x10));
-               OUT_RING  (chan, lower_32_bits(addr ^ 0x10));
-               OUT_RING  (chan, base->wndw.data + 1);
-               OUT_RING  (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG);
-               BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
-               OUT_RING  (chan, upper_32_bits(addr));
-               OUT_RING  (chan, lower_32_bits(addr));
-               OUT_RING  (chan, base->wndw.data);
-               OUT_RING  (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL);
-       } else
-       if (chan) {
-               u64 addr = nv84_fence_crtc(chan, nv_crtc->index) + base->wndw.sema;
-               ret = RING_SPACE(chan, 10);
-               if (ret)
-                       return ret;
-
-               BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
-               OUT_RING  (chan, upper_32_bits(addr ^ 0x10));
-               OUT_RING  (chan, lower_32_bits(addr ^ 0x10));
-               OUT_RING  (chan, base->wndw.data + 1);
-               OUT_RING  (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_WRITE_LONG |
-                                NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
-               BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
-               OUT_RING  (chan, upper_32_bits(addr));
-               OUT_RING  (chan, lower_32_bits(addr));
-               OUT_RING  (chan, base->wndw.data);
-               OUT_RING  (chan, NV84_SUBCHAN_SEMAPHORE_TRIGGER_ACQUIRE_EQUAL |
-                                NVC0_SUBCHAN_SEMAPHORE_TRIGGER_YIELD);
-       }
-
-       if (chan) {
-               base->wndw.sema ^= 0x10;
-               base->wndw.data++;
-               FIRE_RING (chan);
-       }
-
-       /* queue the flip */
-       asyw->state.crtc = &head->base.base;
-       asyw->state.fb = fb;
-       asyw->interval = swap_interval;
-       asyw->image.handle = nv_fb->r_handle;
-       asyw->image.offset = nv_fb->nvbo->bo.offset;
-       asyw->sema.handle = base->chan.base.sync.handle;
-       asyw->sema.offset = base->wndw.sema;
-       asyw->sema.acquire = base->wndw.data++;
-       asyw->sema.release = base->wndw.data;
-       nv50_wndw_atomic_check(&wndw->plane, &asyw->state);
-       asyw->set.sema = true;
-       nv50_wndw_flush_set(wndw, 0, asyw);
-       nv50_wndw_wait_armed(wndw, asyw);
-
-       nouveau_bo_ref(nv_fb->nvbo, &head->image);
-       return 0;
-}
-
 /******************************************************************************
  * Head
  *****************************************************************************/
@@ -1914,6 +1724,11 @@ nv50_head_core_set(struct nv50_head *head, struct nv50_head_atom *asyh)
                        evo_data(push, asyh->core.handle);
                        evo_mthd(push, 0x08c0 + head->base.index * 0x400, 1);
                        evo_data(push, (asyh->core.y << 16) | asyh->core.x);
+                       /* EVO will complain with INVALID_STATE if we have an
+                        * active cursor and (re)specify HeadSetContextDmaIso
+                        * without also updating HeadSetOffsetCursor.
+                        */
+                       asyh->set.curs = asyh->curs.visible;
                } else
                if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
                        evo_mthd(push, 0x0860 + head->base.index * 0x400, 1);
@@ -2363,21 +2178,8 @@ nv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state)
        return 0;
 }
 
-/******************************************************************************
- * CRTC
- *****************************************************************************/
-
-static int
-nv50_crtc_mode_set_base_atomic(struct drm_crtc *crtc,
-                              struct drm_framebuffer *fb, int x, int y,
-                              enum mode_set_atomic state)
-{
-       WARN_ON(1);
-       return 0;
-}
-
 static void
-nv50_crtc_lut_load(struct drm_crtc *crtc)
+nv50_head_lut_load(struct drm_crtc *crtc)
 {
        struct nv50_disp *disp = nv50_disp(crtc->dev);
        struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
@@ -2402,45 +2204,18 @@ nv50_crtc_lut_load(struct drm_crtc *crtc)
 }
 
 static int
-nv50_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
-                   uint32_t size)
+nv50_head_mode_set_base_atomic(struct drm_crtc *crtc,
+                              struct drm_framebuffer *fb, int x, int y,
+                              enum mode_set_atomic state)
 {
-       struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
-       u32 i;
-
-       for (i = 0; i < size; i++) {
-               nv_crtc->lut.r[i] = r[i];
-               nv_crtc->lut.g[i] = g[i];
-               nv_crtc->lut.b[i] = b[i];
-       }
-
-       nv50_crtc_lut_load(crtc);
-
+       WARN_ON(1);
        return 0;
 }
 
-static void
-nv50_crtc_destroy(struct drm_crtc *crtc)
-{
-       struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
-       struct nv50_disp *disp = nv50_disp(crtc->dev);
-       struct nv50_head *head = nv50_head(crtc);
-
-       nv50_dmac_destroy(&head->ovly.base, disp->disp);
-       nv50_pioc_destroy(&head->oimm.base);
-
-       nouveau_bo_unmap(nv_crtc->lut.nvbo);
-       if (nv_crtc->lut.nvbo)
-               nouveau_bo_unpin(nv_crtc->lut.nvbo);
-       nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
-
-       drm_crtc_cleanup(crtc);
-       kfree(crtc);
-}
-
-static const struct drm_crtc_helper_funcs nv50_crtc_hfunc = {
-       .mode_set_base_atomic = nv50_crtc_mode_set_base_atomic,
-       .load_lut = nv50_crtc_lut_load,
+static const struct drm_crtc_helper_funcs
+nv50_head_help = {
+       .mode_set_base_atomic = nv50_head_mode_set_base_atomic,
+       .load_lut = nv50_head_lut_load,
        .atomic_check = nv50_head_atomic_check,
 };
 
@@ -2515,6 +2290,23 @@ backoff:
        goto retry;
 }
 
+static int
+nv50_head_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
+                   uint32_t size)
+{
+       struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
+       u32 i;
+
+       for (i = 0; i < size; i++) {
+               nv_crtc->lut.r[i] = r[i];
+               nv_crtc->lut.g[i] = g[i];
+               nv_crtc->lut.b[i] = b[i];
+       }
+
+       nv50_head_lut_load(crtc);
+       return 0;
+}
+
 static void
 nv50_head_atomic_destroy_state(struct drm_crtc *crtc,
                               struct drm_crtc_state *state)
@@ -2567,10 +2359,30 @@ nv50_head_reset(struct drm_crtc *crtc)
        __drm_atomic_helper_crtc_reset(crtc, &asyh->state);
 }
 
-static const struct drm_crtc_funcs nv50_crtc_func = {
+static void
+nv50_head_destroy(struct drm_crtc *crtc)
+{
+       struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
+       struct nv50_disp *disp = nv50_disp(crtc->dev);
+       struct nv50_head *head = nv50_head(crtc);
+
+       nv50_dmac_destroy(&head->ovly.base, disp->disp);
+       nv50_pioc_destroy(&head->oimm.base);
+
+       nouveau_bo_unmap(nv_crtc->lut.nvbo);
+       if (nv_crtc->lut.nvbo)
+               nouveau_bo_unpin(nv_crtc->lut.nvbo);
+       nouveau_bo_ref(NULL, &nv_crtc->lut.nvbo);
+
+       drm_crtc_cleanup(crtc);
+       kfree(crtc);
+}
+
+static const struct drm_crtc_funcs
+nv50_head_func = {
        .reset = nv50_head_reset,
-       .gamma_set = nv50_crtc_gamma_set,
-       .destroy = nv50_crtc_destroy,
+       .gamma_set = nv50_head_gamma_set,
+       .destroy = nv50_head_destroy,
        .set_config = drm_atomic_helper_set_config,
        .page_flip = nv50_head_page_flip,
        .set_property = drm_atomic_helper_crtc_set_property,
@@ -2579,7 +2391,7 @@ static const struct drm_crtc_funcs nv50_crtc_func = {
 };
 
 static int
-nv50_crtc_create(struct drm_device *dev, int index)
+nv50_head_create(struct drm_device *dev, int index)
 {
        struct nouveau_drm *drm = nouveau_drm(dev);
        struct nvif_device *device = &drm->device;
@@ -2610,12 +2422,10 @@ nv50_crtc_create(struct drm_device *dev, int index)
        }
 
        crtc = &head->base.base;
-       head->_base = base;
-
        drm_crtc_init_with_planes(dev, crtc, &base->wndw.plane,
-                                 &curs->wndw.plane, &nv50_crtc_func,
+                                 &curs->wndw.plane, &nv50_head_func,
                                  "head-%d", head->base.index);
-       drm_crtc_helper_add(crtc, &nv50_crtc_hfunc);
+       drm_crtc_helper_add(crtc, &nv50_head_help);
        drm_mode_crtc_set_gamma_size(crtc, 256);
 
        ret = nouveau_bo_new(dev, 8192, 0x100, TTM_PL_FLAG_VRAM,
@@ -2646,7 +2456,7 @@ nv50_crtc_create(struct drm_device *dev, int index)
 
 out:
        if (ret)
-               nv50_crtc_destroy(crtc);
+               nv50_head_destroy(crtc);
        return ret;
 }
 
@@ -2997,12 +2807,502 @@ nv50_hdmi_enable(struct drm_encoder *encoder, struct drm_display_mode *mode)
 /******************************************************************************
  * MST
  *****************************************************************************/
+#define nv50_mstm(p) container_of((p), struct nv50_mstm, mgr)
+#define nv50_mstc(p) container_of((p), struct nv50_mstc, connector)
+#define nv50_msto(p) container_of((p), struct nv50_msto, encoder)
+
 struct nv50_mstm {
        struct nouveau_encoder *outp;
 
        struct drm_dp_mst_topology_mgr mgr;
+       struct nv50_msto *msto[4];
+
+       bool modified;
+};
+
+struct nv50_mstc {
+       struct nv50_mstm *mstm;
+       struct drm_dp_mst_port *port;
+       struct drm_connector connector;
+
+       struct drm_display_mode *native;
+       struct edid *edid;
+
+       int pbn;
+};
+
+struct nv50_msto {
+       struct drm_encoder encoder;
+
+       struct nv50_head *head;
+       struct nv50_mstc *mstc;
+       bool disabled;
+};
+
+static struct drm_dp_payload *
+nv50_msto_payload(struct nv50_msto *msto)
+{
+       struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
+       struct nv50_mstc *mstc = msto->mstc;
+       struct nv50_mstm *mstm = mstc->mstm;
+       int vcpi = mstc->port->vcpi.vcpi, i;
+
+       NV_ATOMIC(drm, "%s: vcpi %d\n", msto->encoder.name, vcpi);
+       for (i = 0; i < mstm->mgr.max_payloads; i++) {
+               struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
+               NV_ATOMIC(drm, "%s: %d: vcpi %d start 0x%02x slots 0x%02x\n",
+                         mstm->outp->base.base.name, i, payload->vcpi,
+                         payload->start_slot, payload->num_slots);
+       }
+
+       for (i = 0; i < mstm->mgr.max_payloads; i++) {
+               struct drm_dp_payload *payload = &mstm->mgr.payloads[i];
+               if (payload->vcpi == vcpi)
+                       return payload;
+       }
+
+       return NULL;
+}
+
+static void
+nv50_msto_cleanup(struct nv50_msto *msto)
+{
+       struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
+       struct nv50_mstc *mstc = msto->mstc;
+       struct nv50_mstm *mstm = mstc->mstm;
+
+       NV_ATOMIC(drm, "%s: msto cleanup\n", msto->encoder.name);
+       if (mstc->port && mstc->port->vcpi.vcpi > 0 && !nv50_msto_payload(msto))
+               drm_dp_mst_deallocate_vcpi(&mstm->mgr, mstc->port);
+       if (msto->disabled) {
+               msto->mstc = NULL;
+               msto->head = NULL;
+               msto->disabled = false;
+       }
+}
+
+static void
+nv50_msto_prepare(struct nv50_msto *msto)
+{
+       struct nouveau_drm *drm = nouveau_drm(msto->encoder.dev);
+       struct nv50_mstc *mstc = msto->mstc;
+       struct nv50_mstm *mstm = mstc->mstm;
+       struct {
+               struct nv50_disp_mthd_v1 base;
+               struct nv50_disp_sor_dp_mst_vcpi_v0 vcpi;
+       } args = {
+               .base.version = 1,
+               .base.method = NV50_DISP_MTHD_V1_SOR_DP_MST_VCPI,
+               .base.hasht  = mstm->outp->dcb->hasht,
+               .base.hashm  = (0xf0ff & mstm->outp->dcb->hashm) |
+                              (0x0100 << msto->head->base.index),
+       };
+
+       NV_ATOMIC(drm, "%s: msto prepare\n", msto->encoder.name);
+       if (mstc->port && mstc->port->vcpi.vcpi > 0) {
+               struct drm_dp_payload *payload = nv50_msto_payload(msto);
+               if (payload) {
+                       args.vcpi.start_slot = payload->start_slot;
+                       args.vcpi.num_slots = payload->num_slots;
+                       args.vcpi.pbn = mstc->port->vcpi.pbn;
+                       args.vcpi.aligned_pbn = mstc->port->vcpi.aligned_pbn;
+               }
+       }
+
+       NV_ATOMIC(drm, "%s: %s: %02x %02x %04x %04x\n",
+                 msto->encoder.name, msto->head->base.base.name,
+                 args.vcpi.start_slot, args.vcpi.num_slots,
+                 args.vcpi.pbn, args.vcpi.aligned_pbn);
+       nvif_mthd(&drm->display->disp, 0, &args, sizeof(args));
+}
+
+static int
+nv50_msto_atomic_check(struct drm_encoder *encoder,
+                      struct drm_crtc_state *crtc_state,
+                      struct drm_connector_state *conn_state)
+{
+       struct nv50_mstc *mstc = nv50_mstc(conn_state->connector);
+       struct nv50_mstm *mstm = mstc->mstm;
+       int bpp = conn_state->connector->display_info.bpc * 3;
+       int slots;
+
+       mstc->pbn = drm_dp_calc_pbn_mode(crtc_state->adjusted_mode.clock, bpp);
+
+       slots = drm_dp_find_vcpi_slots(&mstm->mgr, mstc->pbn);
+       if (slots < 0)
+               return slots;
+
+       return nv50_outp_atomic_check_view(encoder, crtc_state, conn_state,
+                                          mstc->native);
+}
+
+static void
+nv50_msto_enable(struct drm_encoder *encoder)
+{
+       struct nv50_head *head = nv50_head(encoder->crtc);
+       struct nv50_msto *msto = nv50_msto(encoder);
+       struct nv50_mstc *mstc = NULL;
+       struct nv50_mstm *mstm = NULL;
+       struct drm_connector *connector;
+       u8 proto, depth;
+       int slots;
+       bool r;
+
+       drm_for_each_connector(connector, encoder->dev) {
+               if (connector->state->best_encoder == &msto->encoder) {
+                       mstc = nv50_mstc(connector);
+                       mstm = mstc->mstm;
+                       break;
+               }
+       }
+
+       if (WARN_ON(!mstc))
+               return;
+
+       r = drm_dp_mst_allocate_vcpi(&mstm->mgr, mstc->port, mstc->pbn, &slots);
+       WARN_ON(!r);
+
+       if (mstm->outp->dcb->sorconf.link & 1)
+               proto = 0x8;
+       else
+               proto = 0x9;
+
+       switch (mstc->connector.display_info.bpc) {
+       case  6: depth = 0x2; break;
+       case  8: depth = 0x5; break;
+       case 10:
+       default: depth = 0x6; break;
+       }
+
+       mstm->outp->update(mstm->outp, head->base.index,
+                          &head->base.base.state->adjusted_mode, proto, depth);
+
+       msto->head = head;
+       msto->mstc = mstc;
+       mstm->modified = true;
+}
+
+static void
+nv50_msto_disable(struct drm_encoder *encoder)
+{
+       struct nv50_msto *msto = nv50_msto(encoder);
+       struct nv50_mstc *mstc = msto->mstc;
+       struct nv50_mstm *mstm = mstc->mstm;
+
+       if (mstc->port)
+               drm_dp_mst_reset_vcpi_slots(&mstm->mgr, mstc->port);
+
+       mstm->outp->update(mstm->outp, msto->head->base.index, NULL, 0, 0);
+       mstm->modified = true;
+       msto->disabled = true;
+}
+
+static const struct drm_encoder_helper_funcs
+nv50_msto_help = {
+       .disable = nv50_msto_disable,
+       .enable = nv50_msto_enable,
+       .atomic_check = nv50_msto_atomic_check,
+};
+
+static void
+nv50_msto_destroy(struct drm_encoder *encoder)
+{
+       struct nv50_msto *msto = nv50_msto(encoder);
+       drm_encoder_cleanup(&msto->encoder);
+       kfree(msto);
+}
+
+static const struct drm_encoder_funcs
+nv50_msto = {
+       .destroy = nv50_msto_destroy,
+};
+
+static int
+nv50_msto_new(struct drm_device *dev, u32 heads, const char *name, int id,
+             struct nv50_msto **pmsto)
+{
+       struct nv50_msto *msto;
+       int ret;
+
+       if (!(msto = *pmsto = kzalloc(sizeof(*msto), GFP_KERNEL)))
+               return -ENOMEM;
+
+       ret = drm_encoder_init(dev, &msto->encoder, &nv50_msto,
+                              DRM_MODE_ENCODER_DPMST, "%s-mst-%d", name, id);
+       if (ret) {
+               kfree(*pmsto);
+               *pmsto = NULL;
+               return ret;
+       }
+
+       drm_encoder_helper_add(&msto->encoder, &nv50_msto_help);
+       msto->encoder.possible_crtcs = heads;
+       return 0;
+}
+
+static struct drm_encoder *
+nv50_mstc_atomic_best_encoder(struct drm_connector *connector,
+                             struct drm_connector_state *connector_state)
+{
+       struct nv50_head *head = nv50_head(connector_state->crtc);
+       struct nv50_mstc *mstc = nv50_mstc(connector);
+       if (mstc->port) {
+               struct nv50_mstm *mstm = mstc->mstm;
+               return &mstm->msto[head->base.index]->encoder;
+       }
+       return NULL;
+}
+
+static struct drm_encoder *
+nv50_mstc_best_encoder(struct drm_connector *connector)
+{
+       struct nv50_mstc *mstc = nv50_mstc(connector);
+       if (mstc->port) {
+               struct nv50_mstm *mstm = mstc->mstm;
+               return &mstm->msto[0]->encoder;
+       }
+       return NULL;
+}
+
+static enum drm_mode_status
+nv50_mstc_mode_valid(struct drm_connector *connector,
+                    struct drm_display_mode *mode)
+{
+       return MODE_OK;
+}
+
+static int
+nv50_mstc_get_modes(struct drm_connector *connector)
+{
+       struct nv50_mstc *mstc = nv50_mstc(connector);
+       int ret = 0;
+
+       mstc->edid = drm_dp_mst_get_edid(&mstc->connector, mstc->port->mgr, mstc->port);
+       drm_mode_connector_update_edid_property(&mstc->connector, mstc->edid);
+       if (mstc->edid) {
+               ret = drm_add_edid_modes(&mstc->connector, mstc->edid);
+               drm_edid_to_eld(&mstc->connector, mstc->edid);
+       }
+
+       if (!mstc->connector.display_info.bpc)
+               mstc->connector.display_info.bpc = 8;
+
+       if (mstc->native)
+               drm_mode_destroy(mstc->connector.dev, mstc->native);
+       mstc->native = nouveau_conn_native_mode(&mstc->connector);
+       return ret;
+}
+
+static const struct drm_connector_helper_funcs
+nv50_mstc_help = {
+       .get_modes = nv50_mstc_get_modes,
+       .mode_valid = nv50_mstc_mode_valid,
+       .best_encoder = nv50_mstc_best_encoder,
+       .atomic_best_encoder = nv50_mstc_atomic_best_encoder,
+};
+
+static enum drm_connector_status
+nv50_mstc_detect(struct drm_connector *connector, bool force)
+{
+       struct nv50_mstc *mstc = nv50_mstc(connector);
+       if (!mstc->port)
+               return connector_status_disconnected;
+       return drm_dp_mst_detect_port(connector, mstc->port->mgr, mstc->port);
+}
+
+static void
+nv50_mstc_destroy(struct drm_connector *connector)
+{
+       struct nv50_mstc *mstc = nv50_mstc(connector);
+       drm_connector_cleanup(&mstc->connector);
+       kfree(mstc);
+}
+
+static const struct drm_connector_funcs
+nv50_mstc = {
+       .dpms = drm_atomic_helper_connector_dpms,
+       .reset = nouveau_conn_reset,
+       .detect = nv50_mstc_detect,
+       .fill_modes = drm_helper_probe_single_connector_modes,
+       .set_property = drm_atomic_helper_connector_set_property,
+       .destroy = nv50_mstc_destroy,
+       .atomic_duplicate_state = nouveau_conn_atomic_duplicate_state,
+       .atomic_destroy_state = nouveau_conn_atomic_destroy_state,
+       .atomic_set_property = nouveau_conn_atomic_set_property,
+       .atomic_get_property = nouveau_conn_atomic_get_property,
+};
+
+static int
+nv50_mstc_new(struct nv50_mstm *mstm, struct drm_dp_mst_port *port,
+             const char *path, struct nv50_mstc **pmstc)
+{
+       struct drm_device *dev = mstm->outp->base.base.dev;
+       struct nv50_mstc *mstc;
+       int ret, i;
+
+       if (!(mstc = *pmstc = kzalloc(sizeof(*mstc), GFP_KERNEL)))
+               return -ENOMEM;
+       mstc->mstm = mstm;
+       mstc->port = port;
+
+       ret = drm_connector_init(dev, &mstc->connector, &nv50_mstc,
+                                DRM_MODE_CONNECTOR_DisplayPort);
+       if (ret) {
+               kfree(*pmstc);
+               *pmstc = NULL;
+               return ret;
+       }
+
+       drm_connector_helper_add(&mstc->connector, &nv50_mstc_help);
+
+       mstc->connector.funcs->reset(&mstc->connector);
+       nouveau_conn_attach_properties(&mstc->connector);
+
+       for (i = 0; i < ARRAY_SIZE(mstm->msto) && mstm->msto; i++)
+               drm_mode_connector_attach_encoder(&mstc->connector, &mstm->msto[i]->encoder);
+
+       drm_object_attach_property(&mstc->connector.base, dev->mode_config.path_property, 0);
+       drm_object_attach_property(&mstc->connector.base, dev->mode_config.tile_property, 0);
+       drm_mode_connector_set_path_property(&mstc->connector, path);
+       return 0;
+}
+
+static void
+nv50_mstm_cleanup(struct nv50_mstm *mstm)
+{
+       struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
+       struct drm_encoder *encoder;
+       int ret;
+
+       NV_ATOMIC(drm, "%s: mstm cleanup\n", mstm->outp->base.base.name);
+       ret = drm_dp_check_act_status(&mstm->mgr);
+
+       ret = drm_dp_update_payload_part2(&mstm->mgr);
+
+       drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
+               if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
+                       struct nv50_msto *msto = nv50_msto(encoder);
+                       struct nv50_mstc *mstc = msto->mstc;
+                       if (mstc && mstc->mstm == mstm)
+                               nv50_msto_cleanup(msto);
+               }
+       }
+
+       mstm->modified = false;
+}
+
+static void
+nv50_mstm_prepare(struct nv50_mstm *mstm)
+{
+       struct nouveau_drm *drm = nouveau_drm(mstm->outp->base.base.dev);
+       struct drm_encoder *encoder;
+       int ret;
+
+       NV_ATOMIC(drm, "%s: mstm prepare\n", mstm->outp->base.base.name);
+       ret = drm_dp_update_payload_part1(&mstm->mgr);
+
+       drm_for_each_encoder(encoder, mstm->outp->base.base.dev) {
+               if (encoder->encoder_type == DRM_MODE_ENCODER_DPMST) {
+                       struct nv50_msto *msto = nv50_msto(encoder);
+                       struct nv50_mstc *mstc = msto->mstc;
+                       if (mstc && mstc->mstm == mstm)
+                               nv50_msto_prepare(msto);
+               }
+       }
+}
+
+static void
+nv50_mstm_hotplug(struct drm_dp_mst_topology_mgr *mgr)
+{
+       struct nv50_mstm *mstm = nv50_mstm(mgr);
+       drm_kms_helper_hotplug_event(mstm->outp->base.base.dev);
+}
+
+static void
+nv50_mstm_destroy_connector(struct drm_dp_mst_topology_mgr *mgr,
+                           struct drm_connector *connector)
+{
+       struct nouveau_drm *drm = nouveau_drm(connector->dev);
+       struct nv50_mstc *mstc = nv50_mstc(connector);
+
+       drm_connector_unregister(&mstc->connector);
+
+       drm_modeset_lock_all(drm->dev);
+       drm_fb_helper_remove_one_connector(&drm->fbcon->helper, &mstc->connector);
+       mstc->port = NULL;
+       drm_modeset_unlock_all(drm->dev);
+
+       drm_connector_unreference(&mstc->connector);
+}
+
+static void
+nv50_mstm_register_connector(struct drm_connector *connector)
+{
+       struct nouveau_drm *drm = nouveau_drm(connector->dev);
+
+       drm_modeset_lock_all(drm->dev);
+       drm_fb_helper_add_one_connector(&drm->fbcon->helper, connector);
+       drm_modeset_unlock_all(drm->dev);
+
+       drm_connector_register(connector);
+}
+
+static struct drm_connector *
+nv50_mstm_add_connector(struct drm_dp_mst_topology_mgr *mgr,
+                       struct drm_dp_mst_port *port, const char *path)
+{
+       struct nv50_mstm *mstm = nv50_mstm(mgr);
+       struct nv50_mstc *mstc;
+       int ret;
+
+       ret = nv50_mstc_new(mstm, port, path, &mstc);
+       if (ret) {
+               if (mstc)
+                       mstc->connector.funcs->destroy(&mstc->connector);
+               return NULL;
+       }
+
+       return &mstc->connector;
+}
+
+static const struct drm_dp_mst_topology_cbs
+nv50_mstm = {
+       .add_connector = nv50_mstm_add_connector,
+       .register_connector = nv50_mstm_register_connector,
+       .destroy_connector = nv50_mstm_destroy_connector,
+       .hotplug = nv50_mstm_hotplug,
 };
 
+void
+nv50_mstm_service(struct nv50_mstm *mstm)
+{
+       struct drm_dp_aux *aux = mstm->mgr.aux;
+       bool handled = true;
+       int ret;
+       u8 esi[8] = {};
+
+       while (handled) {
+               ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT_ESI, esi, 8);
+               if (ret != 8) {
+                       drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
+                       return;
+               }
+
+               drm_dp_mst_hpd_irq(&mstm->mgr, esi, &handled);
+               if (!handled)
+                       break;
+
+               drm_dp_dpcd_write(aux, DP_SINK_COUNT_ESI + 1, &esi[1], 3);
+       }
+}
+
+void
+nv50_mstm_remove(struct nv50_mstm *mstm)
+{
+       if (mstm)
+               drm_dp_mst_topology_mgr_set_mst(&mstm->mgr, false);
+}
+
 static int
 nv50_mstm_enable(struct nv50_mstm *mstm, u8 dpcd, int state)
 {
@@ -3046,12 +3346,15 @@ nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
        if (!mstm)
                return 0;
 
-       if (dpcd[0] >= 0x12 && allow) {
+       if (dpcd[0] >= 0x12) {
                ret = drm_dp_dpcd_readb(mstm->mgr.aux, DP_MSTM_CAP, &dpcd[1]);
                if (ret < 0)
                        return ret;
 
-               state = dpcd[1] & DP_MST_CAP;
+               if (!(dpcd[1] & DP_MST_CAP))
+                       dpcd[0] = 0x11;
+               else
+                       state = allow;
        }
 
        ret = nv50_mstm_enable(mstm, dpcd[0], state);
@@ -3065,6 +3368,20 @@ nv50_mstm_detect(struct nv50_mstm *mstm, u8 dpcd[8], int allow)
        return mstm->mgr.mst_state;
 }
 
+static void
+nv50_mstm_fini(struct nv50_mstm *mstm)
+{
+       if (mstm && mstm->mgr.mst_state)
+               drm_dp_mst_topology_mgr_suspend(&mstm->mgr);
+}
+
+static void
+nv50_mstm_init(struct nv50_mstm *mstm)
+{
+       if (mstm && mstm->mgr.mst_state)
+               drm_dp_mst_topology_mgr_resume(&mstm->mgr);
+}
+
 static void
 nv50_mstm_del(struct nv50_mstm **pmstm)
 {
@@ -3082,17 +3399,36 @@ nv50_mstm_new(struct nouveau_encoder *outp, struct drm_dp_aux *aux, int aux_max,
        const int max_payloads = hweight8(outp->dcb->heads);
        struct drm_device *dev = outp->base.base.dev;
        struct nv50_mstm *mstm;
-       int ret;
+       int ret, i;
+       u8 dpcd;
+
+       /* This is a workaround for some monitors not functioning
+        * correctly in MST mode on initial module load.  I think
+        * some bad interaction with the VBIOS may be responsible.
+        *
+        * A good ol' off and on again seems to work here ;)
+        */
+       ret = drm_dp_dpcd_readb(aux, DP_DPCD_REV, &dpcd);
+       if (ret >= 0 && dpcd >= 0x12)
+               drm_dp_dpcd_writeb(aux, DP_MSTM_CTRL, 0);
 
        if (!(mstm = *pmstm = kzalloc(sizeof(*mstm), GFP_KERNEL)))
                return -ENOMEM;
        mstm->outp = outp;
+       mstm->mgr.cbs = &nv50_mstm;
 
        ret = drm_dp_mst_topology_mgr_init(&mstm->mgr, dev->dev, aux, aux_max,
                                           max_payloads, conn_base_id);
        if (ret)
                return ret;
 
+       for (i = 0; i < max_payloads; i++) {
+               ret = nv50_msto_new(dev, outp->dcb->heads, outp->base.base.name,
+                                   i, &mstm->msto[i]);
+               if (ret)
+                       return ret;
+       }
+
        return 0;
 }
 
@@ -3119,19 +3455,50 @@ nv50_sor_dpms(struct drm_encoder *encoder, int mode)
 }
 
 static void
-nv50_sor_ctrl(struct nouveau_encoder *nv_encoder, u32 mask, u32 data)
+nv50_sor_update(struct nouveau_encoder *nv_encoder, u8 head,
+               struct drm_display_mode *mode, u8 proto, u8 depth)
 {
-       struct nv50_mast *mast = nv50_mast(nv_encoder->base.base.dev);
-       u32 temp = (nv_encoder->ctrl & ~mask) | (data & mask), *push;
-       if (temp != nv_encoder->ctrl && (push = evo_wait(mast, 2))) {
-               if (nv50_vers(mast) < GF110_DISP_CORE_CHANNEL_DMA) {
+       struct nv50_dmac *core = &nv50_mast(nv_encoder->base.base.dev)->base;
+       u32 *push;
+
+       if (!mode) {
+               nv_encoder->ctrl &= ~BIT(head);
+               if (!(nv_encoder->ctrl & 0x0000000f))
+                       nv_encoder->ctrl = 0;
+       } else {
+               nv_encoder->ctrl |= proto << 8;
+               nv_encoder->ctrl |= BIT(head);
+       }
+
+       if ((push = evo_wait(core, 6))) {
+               if (core->base.user.oclass < GF110_DISP_CORE_CHANNEL_DMA) {
+                       if (mode) {
+                               if (mode->flags & DRM_MODE_FLAG_NHSYNC)
+                                       nv_encoder->ctrl |= 0x00001000;
+                               if (mode->flags & DRM_MODE_FLAG_NVSYNC)
+                                       nv_encoder->ctrl |= 0x00002000;
+                               nv_encoder->ctrl |= depth << 16;
+                       }
                        evo_mthd(push, 0x0600 + (nv_encoder->or * 0x40), 1);
-                       evo_data(push, (nv_encoder->ctrl = temp));
                } else {
+                       if (mode) {
+                               u32 magic = 0x31ec6000 | (head << 25);
+                               u32 syncs = 0x00000001;
+                               if (mode->flags & DRM_MODE_FLAG_NHSYNC)
+                                       syncs |= 0x00000008;
+                               if (mode->flags & DRM_MODE_FLAG_NVSYNC)
+                                       syncs |= 0x00000010;
+                               if (mode->flags & DRM_MODE_FLAG_INTERLACE)
+                                       magic |= 0x00000001;
+
+                               evo_mthd(push, 0x0404 + (head * 0x300), 2);
+                               evo_data(push, syncs | (depth << 6));
+                               evo_data(push, magic);
+                       }
                        evo_mthd(push, 0x0200 + (nv_encoder->or * 0x20), 1);
-                       evo_data(push, (nv_encoder->ctrl = temp));
                }
-               evo_kick(push, mast);
+               evo_data(push, nv_encoder->ctrl);
+               evo_kick(push, core);
        }
 }
 
@@ -3156,7 +3523,7 @@ nv50_sor_disable(struct drm_encoder *encoder)
                        }
                }
 
-               nv50_sor_ctrl(nv_encoder, 1 << nv_crtc->index, 0);
+               nv_encoder->update(nv_encoder, nv_crtc->index, NULL, 0, 0);
                nv50_audio_disable(encoder, nv_crtc);
                nv50_hdmi_disable(&nv_encoder->base.base, nv_crtc);
        }
@@ -3178,13 +3545,10 @@ nv50_sor_enable(struct drm_encoder *encoder)
                .base.hashm   = nv_encoder->dcb->hashm,
        };
        struct nv50_disp *disp = nv50_disp(encoder->dev);
-       struct nv50_mast *mast = nv50_mast(encoder->dev);
        struct drm_device *dev = encoder->dev;
        struct nouveau_drm *drm = nouveau_drm(dev);
        struct nouveau_connector *nv_connector;
        struct nvbios *bios = &drm->vbios;
-       u32 mask, ctrl;
-       u8 owner = 1 << nv_crtc->index;
        u8 proto = 0xf;
        u8 depth = 0x0;
 
@@ -3263,38 +3627,7 @@ nv50_sor_enable(struct drm_encoder *encoder)
                break;
        }
 
-       if (nv50_vers(mast) >= GF110_DISP) {
-               u32 *push = evo_wait(mast, 3);
-               if (push) {
-                       u32 magic = 0x31ec6000 | (nv_crtc->index << 25);
-                       u32 syncs = 0x00000001;
-
-                       if (mode->flags & DRM_MODE_FLAG_NHSYNC)
-                               syncs |= 0x00000008;
-                       if (mode->flags & DRM_MODE_FLAG_NVSYNC)
-                               syncs |= 0x00000010;
-
-                       if (mode->flags & DRM_MODE_FLAG_INTERLACE)
-                               magic |= 0x00000001;
-
-                       evo_mthd(push, 0x0404 + (nv_crtc->index * 0x300), 2);
-                       evo_data(push, syncs | (depth << 6));
-                       evo_data(push, magic);
-                       evo_kick(push, mast);
-               }
-
-               ctrl = proto << 8;
-               mask = 0x00000f00;
-       } else {
-               ctrl = (depth << 16) | (proto << 8);
-               if (mode->flags & DRM_MODE_FLAG_NHSYNC)
-                       ctrl |= 0x00001000;
-               if (mode->flags & DRM_MODE_FLAG_NVSYNC)
-                       ctrl |= 0x00002000;
-               mask = 0x000f3f00;
-       }
-
-       nv50_sor_ctrl(nv_encoder, mask | owner, ctrl | owner);
+       nv_encoder->update(nv_encoder, nv_crtc->index, mode, proto, depth);
 }
 
 static const struct drm_encoder_helper_funcs
@@ -3343,6 +3676,7 @@ nv50_sor_create(struct drm_connector *connector, struct dcb_output *dcbe)
                return -ENOMEM;
        nv_encoder->dcb = dcbe;
        nv_encoder->or = ffs(dcbe->or) - 1;
+       nv_encoder->update = nv50_sor_update;
 
        encoder = to_drm_encoder(nv_encoder);
        encoder->possible_crtcs = dcbe->heads;
@@ -3550,67 +3884,6 @@ nv50_pior_create(struct drm_connector *connector, struct dcb_output *dcbe)
        return 0;
 }
 
-/******************************************************************************
- * Framebuffer
- *****************************************************************************/
-
-static void
-nv50_fb_dtor(struct drm_framebuffer *fb)
-{
-}
-
-static int
-nv50_fb_ctor(struct drm_framebuffer *fb)
-{
-       struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
-       struct nouveau_drm *drm = nouveau_drm(fb->dev);
-       struct nouveau_bo *nvbo = nv_fb->nvbo;
-       struct nv50_disp *disp = nv50_disp(fb->dev);
-       u8 kind = nouveau_bo_tile_layout(nvbo) >> 8;
-       u8 tile = nvbo->tile_mode;
-       struct drm_crtc *crtc;
-
-       if (drm->device.info.chipset >= 0xc0)
-               tile >>= 4; /* yep.. */
-
-       switch (fb->depth) {
-       case  8: nv_fb->r_format = 0x1e00; break;
-       case 15: nv_fb->r_format = 0xe900; break;
-       case 16: nv_fb->r_format = 0xe800; break;
-       case 24:
-       case 32: nv_fb->r_format = 0xcf00; break;
-       case 30: nv_fb->r_format = 0xd100; break;
-       default:
-                NV_ERROR(drm, "unknown depth %d\n", fb->depth);
-                return -EINVAL;
-       }
-
-       if (disp->disp->oclass < G82_DISP) {
-               nv_fb->r_pitch   = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
-                                           (fb->pitches[0] | 0x00100000);
-               nv_fb->r_format |= kind << 16;
-       } else
-       if (disp->disp->oclass < GF110_DISP) {
-               nv_fb->r_pitch  = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
-                                          (fb->pitches[0] | 0x00100000);
-       } else {
-               nv_fb->r_pitch  = kind ? (((fb->pitches[0] / 4) << 4) | tile) :
-                                          (fb->pitches[0] | 0x01000000);
-       }
-       nv_fb->r_handle = 0xffff0000 | kind;
-
-       list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
-               struct nv50_wndw *wndw = nv50_wndw(crtc->primary);
-               struct nv50_dmac_ctxdma *ctxdma;
-
-               ctxdma = nv50_dmac_ctxdma_new(wndw->dmac, nv_fb->r_handle, nv_fb);
-               if (IS_ERR(ctxdma))
-                       return PTR_ERR(ctxdma);
-       }
-
-       return 0;
-}
-
 /******************************************************************************
  * Atomic
  *****************************************************************************/
@@ -3620,10 +3893,20 @@ nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
 {
        struct nv50_disp *disp = nv50_disp(drm->dev);
        struct nv50_dmac *core = &disp->mast.base;
+       struct nv50_mstm *mstm;
+       struct drm_encoder *encoder;
        u32 *push;
 
        NV_ATOMIC(drm, "commit core %08x\n", interlock);
 
+       drm_for_each_encoder(encoder, drm->dev) {
+               if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
+                       mstm = nouveau_encoder(encoder)->dp.mstm;
+                       if (mstm && mstm->modified)
+                               nv50_mstm_prepare(mstm);
+               }
+       }
+
        if ((push = evo_wait(core, 5))) {
                evo_mthd(push, 0x0084, 1);
                evo_data(push, 0x80000000);
@@ -3639,6 +3922,14 @@ nv50_disp_atomic_commit_core(struct nouveau_drm *drm, u32 interlock)
                ) < 0)
                        NV_ERROR(drm, "EVO timeout\n");
        }
+
+       drm_for_each_encoder(encoder, drm->dev) {
+               if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
+                       mstm = nouveau_encoder(encoder)->dp.mstm;
+                       if (mstm && mstm->modified)
+                               nv50_mstm_cleanup(mstm);
+               }
+       }
 }
 
 static void
@@ -3802,6 +4093,8 @@ nv50_disp_atomic_commit_tail(struct drm_atomic_state *state)
        for_each_crtc_in_state(state, crtc, crtc_state, i) {
                if (crtc->state->event) {
                        unsigned long flags;
+                       /* Get correct count/ts if racing with vblank irq */
+                       drm_accurate_vblank_count(crtc);
                        spin_lock_irqsave(&crtc->dev->event_lock, flags);
                        drm_crtc_send_vblank_event(crtc, crtc->state->event);
                        spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
@@ -4048,6 +4341,8 @@ nv50_disp_func = {
 void
 nv50_display_fini(struct drm_device *dev)
 {
+       struct nouveau_encoder *nv_encoder;
+       struct drm_encoder *encoder;
        struct drm_plane *plane;
 
        drm_for_each_plane(plane, dev) {
@@ -4056,12 +4351,18 @@ nv50_display_fini(struct drm_device *dev)
                        continue;
                nv50_wndw_fini(wndw);
        }
+
+       list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
+               if (encoder->encoder_type != DRM_MODE_ENCODER_DPMST) {
+                       nv_encoder = nouveau_encoder(encoder);
+                       nv50_mstm_fini(nv_encoder->dp.mstm);
+               }
+       }
 }
 
 int
 nv50_display_init(struct drm_device *dev)
 {
-       struct nv50_disp *disp = nv50_disp(dev);
        struct drm_encoder *encoder;
        struct drm_plane *plane;
        struct drm_crtc *crtc;
@@ -4071,13 +4372,6 @@ nv50_display_init(struct drm_device *dev)
        if (!push)
                return -EBUSY;
 
-       list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
-               struct nv50_wndw *wndw = &nv50_head(crtc)->_base->wndw;
-
-               nv50_crtc_lut_load(crtc);
-               nouveau_bo_wr32(disp->sync, wndw->sema / 4, wndw->data);
-       }
-
        evo_mthd(push, 0x0088, 1);
        evo_data(push, nv50_mast(dev)->base.sync.handle);
        evo_kick(push, nv50_mast(dev));
@@ -4091,9 +4385,15 @@ nv50_display_init(struct drm_device *dev)
                        help = encoder->helper_private;
                        if (help && help->dpms)
                                help->dpms(encoder, DRM_MODE_DPMS_ON);
+
+                       nv50_mstm_init(nv_encoder->dp.mstm);
                }
        }
 
+       drm_for_each_crtc(crtc, dev) {
+               nv50_head_lut_load(crtc);
+       }
+
        drm_for_each_plane(plane, dev) {
                struct nv50_wndw *wndw = nv50_wndw(plane);
                if (plane->funcs != &nv50_wndw)
@@ -4145,8 +4445,6 @@ nv50_display_create(struct drm_device *dev)
        nouveau_display(dev)->dtor = nv50_display_destroy;
        nouveau_display(dev)->init = nv50_display_init;
        nouveau_display(dev)->fini = nv50_display_fini;
-       nouveau_display(dev)->fb_ctor = nv50_fb_ctor;
-       nouveau_display(dev)->fb_dtor = nv50_fb_dtor;
        disp->disp = &nouveau_display(dev)->disp;
        dev->mode_config.funcs = &nv50_disp_func;
        if (nouveau_atomic)
@@ -4182,7 +4480,7 @@ nv50_display_create(struct drm_device *dev)
                crtcs = 2;
 
        for (i = 0; i < crtcs; i++) {
-               ret = nv50_crtc_create(dev, i);
+               ret = nv50_head_create(dev, i);
                if (ret)
                        goto out;
        }