drm/nouveau: Implement the pageflip ioctl.
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nv50_graph.c
index cbf5ae2f67d4a110de1bb9d9c129224fbcdd6c3c..6d81f4dab37d45fa76aa5975c02260d460b05d8b 100644 (file)
 #include "nouveau_drv.h"
 #include "nouveau_ramht.h"
 #include "nouveau_grctx.h"
+#include "nouveau_dma.h"
+#include "nv50_evo.h"
+
+static int nv50_graph_register(struct drm_device *);
 
 static void
 nv50_graph_init_reset(struct drm_device *dev)
@@ -145,12 +149,15 @@ nv50_graph_init(struct drm_device *dev)
        nv50_graph_init_reset(dev);
        nv50_graph_init_regs__nv(dev);
        nv50_graph_init_regs(dev);
-       nv50_graph_init_intr(dev);
 
        ret = nv50_graph_init_ctxctl(dev);
        if (ret)
                return ret;
 
+       ret = nv50_graph_register(dev);
+       if (ret)
+               return ret;
+       nv50_graph_init_intr(dev);
        return 0;
 }
 
@@ -190,7 +197,7 @@ nv50_graph_channel(struct drm_device *dev)
        inst = (inst & NV50_PGRAPH_CTXCTL_CUR_INSTANCE) << 12;
 
        for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
-               struct nouveau_channel *chan = dev_priv->fifos[i];
+               struct nouveau_channel *chan = dev_priv->channels.ptr[i];
 
                if (chan && chan->ramin && chan->ramin->vinst == inst)
                        return chan;
@@ -211,7 +218,7 @@ nv50_graph_create_context(struct nouveau_channel *chan)
 
        NV_DEBUG(dev, "ch%d\n", chan->id);
 
-       ret = nouveau_gpuobj_new(dev, chan, pgraph->grctx_size, 0x1000,
+       ret = nouveau_gpuobj_new(dev, chan, pgraph->grctx_size, 0,
                                 NVOBJ_FLAG_ZERO_ALLOC |
                                 NVOBJ_FLAG_ZERO_FREE, &chan->ramin_grctx);
        if (ret)
@@ -242,17 +249,28 @@ nv50_graph_destroy_context(struct nouveau_channel *chan)
 {
        struct drm_device *dev = chan->dev;
        struct drm_nouveau_private *dev_priv = dev->dev_private;
+       struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
        int i, hdr = (dev_priv->chipset == 0x50) ? 0x200 : 0x20;
+       unsigned long flags;
 
        NV_DEBUG(dev, "ch%d\n", chan->id);
 
        if (!chan->ramin)
                return;
 
+       spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
+       pgraph->fifo_access(dev, false);
+
+       if (pgraph->channel(dev) == chan)
+               pgraph->unload_context(dev);
+
        for (i = hdr; i < hdr + 24; i += 4)
                nv_wo32(chan->ramin, i, 0);
        dev_priv->engine.instmem.flush(dev);
 
+       pgraph->fifo_access(dev, true);
+       spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
+
        nouveau_gpuobj_ref(NULL, &chan->ramin_grctx);
 }
 
@@ -322,8 +340,8 @@ nv50_graph_context_switch(struct drm_device *dev)
 }
 
 static int
-nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan, int grclass,
-                          int mthd, uint32_t data)
+nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan,
+                          u32 class, u32 mthd, u32 data)
 {
        struct nouveau_gpuobj *gpuobj;
 
@@ -340,8 +358,8 @@ nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan, int grclass,
 }
 
 static int
-nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan, int grclass,
-                             int mthd, uint32_t data)
+nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan,
+                             u32 class, u32 mthd, u32 data)
 {
        if (nouveau_notifier_offset(chan->nvsw.vblsem, &data))
                return -ERANGE;
@@ -351,16 +369,16 @@ nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan, int grclass,
 }
 
 static int
-nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan, int grclass,
-                                  int mthd, uint32_t data)
+nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan,
+                                  u32 class, u32 mthd, u32 data)
 {
        chan->nvsw.vblsem_rval = data;
        return 0;
 }
 
 static int
-nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan, int grclass,
-                              int mthd, uint32_t data)
+nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan,
+                              u32 class, u32 mthd, u32 data)
 {
        struct drm_device *dev = chan->dev;
        struct drm_nouveau_private *dev_priv = dev->dev_private;
@@ -368,37 +386,121 @@ nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan, int grclass,
        if (!chan->nvsw.vblsem || chan->nvsw.vblsem_offset == ~0 || data > 1)
                return -EINVAL;
 
-       if (!(nv_rd32(dev, NV50_PDISPLAY_INTR_EN) &
-                     NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data))) {
-               nv_wr32(dev, NV50_PDISPLAY_INTR_1,
-                       NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(data));
-               nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
-                       NV50_PDISPLAY_INTR_EN) |
-                       NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data));
+       drm_vblank_get(dev, data);
+       list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
+       return 0;
+}
+
+static int
+nv50_graph_nvsw_mthd_page_flip(struct nouveau_channel *chan,
+                              u32 class, u32 mthd, u32 data)
+{
+       struct nouveau_page_flip_state s;
+
+       if (!nouveau_finish_page_flip(chan, &s)) {
+               /* XXX - Do something here */
        }
 
-       list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
        return 0;
 }
 
-static struct nouveau_pgraph_object_method nv50_graph_nvsw_methods[] = {
-       { 0x018c, nv50_graph_nvsw_dma_vblsem },
-       { 0x0400, nv50_graph_nvsw_vblsem_offset },
-       { 0x0404, nv50_graph_nvsw_vblsem_release_val },
-       { 0x0408, nv50_graph_nvsw_vblsem_release },
-       {}
-};
-
-struct nouveau_pgraph_object_class nv50_graph_grclass[] = {
-       { 0x506e, true, nv50_graph_nvsw_methods }, /* nvsw */
-       { 0x0030, false, NULL }, /* null */
-       { 0x5039, false, NULL }, /* m2mf */
-       { 0x502d, false, NULL }, /* 2d */
-       { 0x50c0, false, NULL }, /* compute */
-       { 0x85c0, false, NULL }, /* compute (nva3, nva5, nva8) */
-       { 0x5097, false, NULL }, /* tesla (nv50) */
-       { 0x8297, false, NULL }, /* tesla (nv8x/nv9x) */
-       { 0x8397, false, NULL }, /* tesla (nva0, nvaa, nvac) */
-       { 0x8597, false, NULL }, /* tesla (nva3, nva5, nva8) */
-       {}
-};
+static int
+nv50_graph_register(struct drm_device *dev)
+{
+       struct drm_nouveau_private *dev_priv = dev->dev_private;
+
+       if (dev_priv->engine.graph.registered)
+               return 0;
+
+       NVOBJ_CLASS(dev, 0x506e, SW); /* nvsw */
+       NVOBJ_MTHD (dev, 0x506e, 0x018c, nv50_graph_nvsw_dma_vblsem);
+       NVOBJ_MTHD (dev, 0x506e, 0x0400, nv50_graph_nvsw_vblsem_offset);
+       NVOBJ_MTHD (dev, 0x506e, 0x0404, nv50_graph_nvsw_vblsem_release_val);
+       NVOBJ_MTHD (dev, 0x506e, 0x0408, nv50_graph_nvsw_vblsem_release);
+       NVOBJ_MTHD (dev, 0x506e, 0x0500, nv50_graph_nvsw_mthd_page_flip);
+
+       NVOBJ_CLASS(dev, 0x0030, GR); /* null */
+       NVOBJ_CLASS(dev, 0x5039, GR); /* m2mf */
+       NVOBJ_CLASS(dev, 0x502d, GR); /* 2d */
+       NVOBJ_CLASS(dev, 0x50c0, GR); /* compute */
+       NVOBJ_CLASS(dev, 0x85c0, GR); /* compute (nva3, nva5, nva8) */
+
+       /* tesla */
+       if (dev_priv->chipset == 0x50)
+               NVOBJ_CLASS(dev, 0x5097, GR); /* tesla (nv50) */
+       else
+       if (dev_priv->chipset < 0xa0)
+               NVOBJ_CLASS(dev, 0x8297, GR); /* tesla (nv8x/nv9x) */
+       else {
+               switch (dev_priv->chipset) {
+               case 0xa0:
+               case 0xaa:
+               case 0xac:
+                       NVOBJ_CLASS(dev, 0x8397, GR);
+                       break;
+               case 0xa3:
+               case 0xa5:
+               case 0xa8:
+                       NVOBJ_CLASS(dev, 0x8597, GR);
+                       break;
+               case 0xaf:
+                       NVOBJ_CLASS(dev, 0x8697, GR);
+                       break;
+               }
+       }
+
+       dev_priv->engine.graph.registered = true;
+       return 0;
+}
+
+void
+nv50_graph_tlb_flush(struct drm_device *dev)
+{
+       nv50_vm_flush(dev, 0);
+}
+
+void
+nv86_graph_tlb_flush(struct drm_device *dev)
+{
+       struct drm_nouveau_private *dev_priv = dev->dev_private;
+       struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
+       bool idle, timeout = false;
+       unsigned long flags;
+       u64 start;
+       u32 tmp;
+
+       spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
+       nv_mask(dev, 0x400500, 0x00000001, 0x00000000);
+
+       start = ptimer->read(dev);
+       do {
+               idle = true;
+
+               for (tmp = nv_rd32(dev, 0x400380); tmp && idle; tmp >>= 3) {
+                       if ((tmp & 7) == 1)
+                               idle = false;
+               }
+
+               for (tmp = nv_rd32(dev, 0x400384); tmp && idle; tmp >>= 3) {
+                       if ((tmp & 7) == 1)
+                               idle = false;
+               }
+
+               for (tmp = nv_rd32(dev, 0x400388); tmp && idle; tmp >>= 3) {
+                       if ((tmp & 7) == 1)
+                               idle = false;
+               }
+       } while (!idle && !(timeout = ptimer->read(dev) - start > 2000000000));
+
+       if (timeout) {
+               NV_ERROR(dev, "PGRAPH TLB flush idle timeout fail: "
+                             "0x%08x 0x%08x 0x%08x 0x%08x\n",
+                        nv_rd32(dev, 0x400700), nv_rd32(dev, 0x400380),
+                        nv_rd32(dev, 0x400384), nv_rd32(dev, 0x400388));
+       }
+
+       nv50_vm_flush(dev, 0);
+
+       nv_mask(dev, 0x400500, 0x00000001, 0x00000001);
+       spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
+}