From 9fa7653fa0b01f4c4e263d1a32e139e32a071023 Mon Sep 17 00:00:00 2001
From: zrj <rimvydas.jasinskas@gmail.com>
Date: Tue, 1 Dec 2015 10:00:37 +0200
Subject: [PATCH 19/55] nv: compat dmah for dummy pages

adapted from radeon_gart.c, assumes drm_device is *not* used in drm_pci_*()
---
 .../core/subdev/vm/nouveau_subdev_vm_nv04.c    |  6 ++++--
 .../core/subdev/vm/nouveau_subdev_vm_nv44.c    |  9 +++++++--
 sys/dev/drm/nouveau/core/subdev/vm/nv04.h      |  1 +
 sys/dev/drm/nouveau/nouveau_nv50_display.c     | 18 +++++++++++++-----
 4 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv04.c b/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv04.c
index ed45437167..a91f79cf8f 100644
--- a/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv04.c
+++ b/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv04.c
@@ -133,8 +133,10 @@ nv04_vmmgr_dtor(struct nouveau_object *object)
 		nouveau_vm_ref(NULL, &priv->vm, NULL);
 	}
 	if (priv->nullp) {
-		pci_free_consistent(nv_device(priv)->pdev, 16 * 1024,
-				    priv->nullp, priv->null);
+		drm_pci_free(NULL, priv->dmah); /* 16 * 1024 */
+		priv->dmah = NULL;
+		priv->nullp =NULL;
+		priv->null = 0;
 	}
 	nouveau_vmmgr_destroy(&priv->base);
 }
diff --git a/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv44.c b/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv44.c
index 1238d3cc6c..84c5643446 100644
--- a/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv44.c
+++ b/sys/dev/drm/nouveau/core/subdev/vm/nouveau_subdev_vm_nv44.c
@@ -161,6 +161,7 @@ nv44_vmmgr_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
 {
 	struct nouveau_device *device = nv_device(parent);
 	struct nv04_vmmgr_priv *priv;
+	drm_dma_handle_t *dmah;
 	int ret;
 
 	if (pci_find_extcap(device->pdev->dev, PCIY_AGP, NULL) == 0 ||
@@ -185,12 +186,16 @@ nv44_vmmgr_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
 	priv->base.unmap = nv44_vm_unmap;
 	priv->base.flush = nv44_vm_flush;
 
-	priv->nullp = pci_alloc_consistent(device->pdev, 16 * 1024, &priv->null);
-	if (!priv->nullp) {
+	dmah = drm_pci_alloc(NULL, 16 * 1024, PAGE_SIZE);
+	if (dmah == NULL) {
 		nv_error(priv, "unable to allocate dummy pages\n");
 		return -ENOMEM;
 	}
 
+	priv->dmah = dmah;
+	priv->nullp = dmah->vaddr;
+	priv->null = dmah->busaddr;
+
 	ret = nouveau_vm_create(&priv->base, 0, NV44_GART_SIZE, 0, 4096,
 				&priv->vm);
 	if (ret)
diff --git a/sys/dev/drm/nouveau/core/subdev/vm/nv04.h b/sys/dev/drm/nouveau/core/subdev/vm/nv04.h
index ec42d4bc86..f49992350c 100644
--- a/sys/dev/drm/nouveau/core/subdev/vm/nv04.h
+++ b/sys/dev/drm/nouveau/core/subdev/vm/nv04.h
@@ -8,6 +8,7 @@ struct nv04_vmmgr_priv {
 	struct nouveau_vm *vm;
 	dma_addr_t null;
 	void *nullp;
+	drm_dma_handle_t *dmah;
 };
 
 static inline struct nv04_vmmgr_priv *
diff --git a/sys/dev/drm/nouveau/nouveau_nv50_display.c b/sys/dev/drm/nouveau/nouveau_nv50_display.c
index 6ca6ac0248..76e4e01cac 100644
--- a/sys/dev/drm/nouveau/nouveau_nv50_display.c
+++ b/sys/dev/drm/nouveau/nouveau_nv50_display.c
@@ -166,6 +166,7 @@ struct nv50_dmac {
 	struct nv50_chan base;
 	dma_addr_t handle;
 	u32 *ptr;
+	drm_dma_handle_t *dmah;
 
 	struct nvif_object sync;
 	struct nvif_object vram;
@@ -185,8 +186,10 @@ nv50_dmac_destroy(struct nv50_dmac *dmac, struct nvif_object *disp)
 	nv50_chan_destroy(&dmac->base);
 
 	if (dmac->ptr) {
-		struct pci_dev *pdev = nvkm_device(nvif_device(disp))->pdev;
-		pci_free_consistent(pdev, PAGE_SIZE, dmac->ptr, dmac->handle);
+		drm_pci_free(NULL, dmac->dmah);
+		dmac->dmah = NULL;
+		dmac->ptr = NULL;
+		dmac->handle = 0;
 	}
 }
 
@@ -200,13 +203,18 @@ nv50_dmac_create(struct nvif_object *disp, const u32 *oclass, u8 head,
 	struct nvif_object pushbuf;
 	int ret;
 
+	drm_dma_handle_t *dmah;
+
 	mutex_init(&dmac->lock);
 
-	dmac->ptr = pci_alloc_consistent(nvkm_device(device)->pdev,
-					 PAGE_SIZE, &dmac->handle);
-	if (!dmac->ptr)
+	dmah = drm_pci_alloc(NULL, PAGE_SIZE, PAGE_SIZE);
+	if (dmah == NULL)
 		return -ENOMEM;
 
+	dmac->dmah = dmah;
+	dmac->ptr = dmah->vaddr;
+	dmac->handle = dmah->busaddr;
+
 	ret = nvif_object_init(nvif_object(device), NULL,
 			       args->pushbuf, NV_DMA_FROM_MEMORY,
 			       &(struct nv_dma_v0) {
-- 
2.23.0

