From 610526f0a56a23db9a0cb31df216a4e41c9ab820 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 3 Jul 2016 11:52:54 +0200
Subject: [PATCH 51/55] nv: Adjust for recent linux spinlock changes.

---
 sys/dev/drm/include/linux/spinlock.h           |  8 +++-----
 .../engine/graph/nouveau_engine_graph_nv04.c   |  2 +-
 .../engine/graph/nouveau_engine_graph_nv10.c   |  2 +-
 .../engine/graph/nouveau_engine_graph_nv50.c   |  2 +-
 sys/dev/drm/nouveau/core/include/core/engine.h |  2 +-
 sys/dev/drm/nouveau/core/include/core/event.h  |  4 ++--
 sys/dev/drm/nouveau/core/include/engine/fifo.h |  2 +-
 .../core/subdev/bar/nouveau_subdev_bar_nv50.c  |  2 +-
 .../core/subdev/bar/nouveau_subdev_bar_nvc0.c  |  2 +-
 .../instmem/nouveau_subdev_instmem_nv50.c      |  2 +-
 .../subdev/therm/nouveau_subdev_therm_fantog.c |  2 +-
 sys/dev/drm/nouveau/core/subdev/therm/priv.h   |  6 +++---
 sys/dev/drm/nouveau/core/subdev/timer/nv04.h   |  2 +-
 sys/dev/drm/nouveau/nouveau_bios.h             |  2 +-
 sys/dev/drm/nouveau/nouveau_bo.c               |  8 ++++----
 sys/dev/drm/nouveau/nouveau_drm.h              |  2 +-
 sys/dev/drm/nouveau/nouveau_fence.c            | 18 +++++++++---------
 sys/dev/drm/nouveau/nouveau_fence.h            |  2 +-
 sys/dev/drm/nouveau/nouveau_nv17_fence.c       |  4 ++--
 sys/dev/drm/nouveau/nv10_fence.h               |  2 +-
 20 files changed, 37 insertions(+), 39 deletions(-)

diff --git a/sys/dev/drm/include/linux/spinlock.h b/sys/dev/drm/include/linux/spinlock.h
index 9c4768cecb..b37bd8422e 100644
--- a/sys/dev/drm/include/linux/spinlock.h
+++ b/sys/dev/drm/include/linux/spinlock.h
@@ -34,12 +34,10 @@
 #include <linux/rwlock.h>
 #include <asm/barrier.h>
 
-#define spin_lock_init(x)	spin_init(x, "DRM spinlock")
-#define spin_lock_irqsave(x, f)	{ (void)f; spin_lock(x); }
-#define spin_unlock_irqrestore(x, f) { (void)f; spin_unlock(x); }
-#define spin_is_locked(x)	spin_held(x)
+#define spin_lock_init(lock)	lockinit(lock, "DRM spinlock", 0, LK_CANRECURSE)
+#define spin_is_locked(lock)	(lockstatus(lock, curthread) == LK_EXCLUSIVE)
 
-#define assert_spin_locked(x)	KKASSERT(lockcountnb(x))
+#define assert_spin_locked(lock) KKASSERT(spin_is_locked(lock))
 
 /*
  * The spin_lock_irq() family of functions stop hardware interrupts
diff --git a/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv04.c b/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv04.c
index cb5e7c3f5e..fb6dbc8047 100644
--- a/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv04.c
+++ b/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv04.c
@@ -354,7 +354,7 @@ nv04_graph_ctx_regs[] = {
 struct nv04_graph_priv {
 	struct nouveau_graph base;
 	struct nv04_graph_chan *chan[16];
-	struct spinlock lock;
+	struct lock lock;
 };
 
 struct nv04_graph_chan {
diff --git a/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv10.c b/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv10.c
index 730d3088ff..6bee2a7f61 100644
--- a/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv10.c
+++ b/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv10.c
@@ -392,7 +392,7 @@ static int nv17_graph_ctx_regs[] = {
 struct nv10_graph_priv {
 	struct nouveau_graph base;
 	struct nv10_graph_chan *chan[32];
-	struct spinlock lock;
+	struct lock lock;
 };
 
 struct nv10_graph_chan {
diff --git a/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv50.c b/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv50.c
index e976b6d90b..a815a925f6 100644
--- a/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv50.c
+++ b/sys/dev/drm/nouveau/core/engine/graph/nouveau_engine_graph_nv50.c
@@ -39,7 +39,7 @@
 
 struct nv50_graph_priv {
 	struct nouveau_graph base;
-	struct spinlock lock;
+	struct lock lock;
 	u32 size;
 };
 
diff --git a/sys/dev/drm/nouveau/core/include/core/engine.h b/sys/dev/drm/nouveau/core/include/core/engine.h
index b1d7dd3292..d74f71b51e 100644
--- a/sys/dev/drm/nouveau/core/include/core/engine.h
+++ b/sys/dev/drm/nouveau/core/include/core/engine.h
@@ -13,7 +13,7 @@ struct nouveau_engine {
 	struct nouveau_oclass *sclass;
 
 	struct list_head contexts;
-	struct spinlock lock;
+	struct lock lock;
 
 	void (*tile_prog)(struct nouveau_engine *, int region);
 	int  (*tlb_flush)(struct nouveau_engine *);
diff --git a/sys/dev/drm/nouveau/core/include/core/event.h b/sys/dev/drm/nouveau/core/include/core/event.h
index aa5802a3f9..454e18f333 100644
--- a/sys/dev/drm/nouveau/core/include/core/event.h
+++ b/sys/dev/drm/nouveau/core/include/core/event.h
@@ -16,8 +16,8 @@ struct nvkm_event {
 	int types_nr;
 	int index_nr;
 
-	struct spinlock refs_lock;
-	struct spinlock list_lock;
+	struct lock refs_lock;
+	struct lock list_lock;
 	struct list_head list;
 	int *refs;
 };
diff --git a/sys/dev/drm/nouveau/core/include/engine/fifo.h b/sys/dev/drm/nouveau/core/include/engine/fifo.h
index 7cbbaf2e69..3b8acb8c40 100644
--- a/sys/dev/drm/nouveau/core/include/engine/fifo.h
+++ b/sys/dev/drm/nouveau/core/include/engine/fifo.h
@@ -73,7 +73,7 @@ struct nouveau_fifo {
 	struct nvkm_event uevent; /* async user trigger */
 
 	struct nouveau_object **channel;
-	struct spinlock lock;
+	struct lock lock;
 	u16 min;
 	u16 max;
 
diff --git a/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nv50.c b/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nv50.c
index e5e00da93e..441cdf5d91 100644
--- a/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nv50.c
+++ b/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nv50.c
@@ -32,7 +32,7 @@
 
 struct nv50_bar_priv {
 	struct nouveau_bar base;
-	struct spinlock lock;
+	struct lock lock;
 	struct nouveau_gpuobj *mem;
 	struct nouveau_gpuobj *pad;
 	struct nouveau_gpuobj *pgd;
diff --git a/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nvc0.c b/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nvc0.c
index c324e5cc8d..cd26139bb6 100644
--- a/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nvc0.c
+++ b/sys/dev/drm/nouveau/core/subdev/bar/nouveau_subdev_bar_nvc0.c
@@ -38,7 +38,7 @@ struct nvc0_bar_priv_vm {
 
 struct nvc0_bar_priv {
 	struct nouveau_bar base;
-	struct spinlock lock;
+	struct lock lock;
 	struct nvc0_bar_priv_vm bar[2];
 };
 
diff --git a/sys/dev/drm/nouveau/core/subdev/instmem/nouveau_subdev_instmem_nv50.c b/sys/dev/drm/nouveau/core/subdev/instmem/nouveau_subdev_instmem_nv50.c
index 1821d54040..2f6ff258ae 100644
--- a/sys/dev/drm/nouveau/core/subdev/instmem/nouveau_subdev_instmem_nv50.c
+++ b/sys/dev/drm/nouveau/core/subdev/instmem/nouveau_subdev_instmem_nv50.c
@@ -29,7 +29,7 @@
 
 struct nv50_instmem_priv {
 	struct nouveau_instmem base;
-	struct spinlock lock;
+	struct lock lock;
 	u64 addr;
 };
 
diff --git a/sys/dev/drm/nouveau/core/subdev/therm/nouveau_subdev_therm_fantog.c b/sys/dev/drm/nouveau/core/subdev/therm/nouveau_subdev_therm_fantog.c
index 32c6c53151..debbe4935b 100644
--- a/sys/dev/drm/nouveau/core/subdev/therm/nouveau_subdev_therm_fantog.c
+++ b/sys/dev/drm/nouveau/core/subdev/therm/nouveau_subdev_therm_fantog.c
@@ -33,7 +33,7 @@
 struct nouveau_fantog_priv {
 	struct nouveau_fan base;
 	struct nouveau_alarm alarm;
-	struct spinlock lock;
+	struct lock lock;
 	u32 period_us;
 	u32 percent;
 	struct dcb_gpio_func func;
diff --git a/sys/dev/drm/nouveau/core/subdev/therm/priv.h b/sys/dev/drm/nouveau/core/subdev/therm/priv.h
index ceffd7fc26..23d2d5afe6 100644
--- a/sys/dev/drm/nouveau/core/subdev/therm/priv.h
+++ b/sys/dev/drm/nouveau/core/subdev/therm/priv.h
@@ -41,7 +41,7 @@ struct nouveau_fan {
 	struct nvbios_perf_fan perf;
 
 	struct nouveau_alarm alarm;
-	struct spinlock lock;
+	struct lock lock;
 	int percent;
 
 	int (*get)(struct nouveau_therm *therm);
@@ -73,7 +73,7 @@ struct nouveau_therm_priv {
 
 	/* automatic thermal management */
 	struct nouveau_alarm alarm;
-	struct spinlock lock;
+	struct lock lock;
 	struct nouveau_therm_trip_point *last_trip;
 	int mode;
 	int cstate;
@@ -87,7 +87,7 @@ struct nouveau_therm_priv {
 
 	/* alarms priv */
 	struct {
-		struct spinlock alarm_program_lock;
+		struct lock alarm_program_lock;
 		struct nouveau_alarm therm_poll_alarm;
 		enum nouveau_therm_thrs_state alarm_state[NOUVEAU_THERM_THRS_NR];
 		void (*program_alarms)(struct nouveau_therm *);
diff --git a/sys/dev/drm/nouveau/core/subdev/timer/nv04.h b/sys/dev/drm/nouveau/core/subdev/timer/nv04.h
index 03a7c3962e..5159132d00 100644
--- a/sys/dev/drm/nouveau/core/subdev/timer/nv04.h
+++ b/sys/dev/drm/nouveau/core/subdev/timer/nv04.h
@@ -14,7 +14,7 @@
 struct nv04_timer_priv {
 	struct nouveau_timer base;
 	struct list_head alarms;
-	struct spinlock lock;
+	struct lock lock;
 	u64 suspend_time;
 };
 
diff --git a/sys/dev/drm/nouveau/nouveau_bios.h b/sys/dev/drm/nouveau/nouveau_bios.h
index d982e38fe3..f9492cb598 100644
--- a/sys/dev/drm/nouveau/nouveau_bios.h
+++ b/sys/dev/drm/nouveau/nouveau_bios.h
@@ -92,7 +92,7 @@ struct nvbios {
 	uint8_t digital_min_front_porch;
 	bool fp_no_ddc;
 
-	struct spinlock lock;
+	struct lock lock;
 
 	bool execute;
 
diff --git a/sys/dev/drm/nouveau/nouveau_bo.c b/sys/dev/drm/nouveau/nouveau_bo.c
index 9ad18a1518..9bc8bc3734 100644
--- a/sys/dev/drm/nouveau/nouveau_bo.c
+++ b/sys/dev/drm/nouveau/nouveau_bo.c
@@ -75,7 +75,7 @@ nv10_bo_get_tile_region(struct drm_device *dev, int i)
 	struct nouveau_drm *drm = nouveau_drm(dev);
 	struct nouveau_drm_tile *tile = &drm->tile.reg[i];
 
-	spin_lock(&drm->tile.lock);
+	lockmgr(&drm->tile.lock, LK_EXCLUSIVE);
 
 	if (!tile->used &&
 	    (!tile->fence || nouveau_fence_done(tile->fence)))
@@ -83,7 +83,7 @@ nv10_bo_get_tile_region(struct drm_device *dev, int i)
 	else
 		tile = NULL;
 
-	spin_unlock(&drm->tile.lock);
+	lockmgr(&drm->tile.lock, LK_RELEASE);
 	return tile;
 }
 
@@ -94,10 +94,10 @@ nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
 	struct nouveau_drm *drm = nouveau_drm(dev);
 
 	if (tile) {
-		spin_lock(&drm->tile.lock);
+		lockmgr(&drm->tile.lock, LK_EXCLUSIVE);
 		tile->fence = nouveau_fence_ref(fence);
 		tile->used = false;
-		spin_unlock(&drm->tile.lock);
+		lockmgr(&drm->tile.lock, LK_RELEASE);
 	}
 }
 
diff --git a/sys/dev/drm/nouveau/nouveau_drm.h b/sys/dev/drm/nouveau/nouveau_drm.h
index 1c7cc37d37..653479d03b 100644
--- a/sys/dev/drm/nouveau/nouveau_drm.h
+++ b/sys/dev/drm/nouveau/nouveau_drm.h
@@ -152,7 +152,7 @@ struct nouveau_drm {
 	/* nv10-nv40 tiling regions */
 	struct {
 		struct nouveau_drm_tile reg[15];
-		struct spinlock lock;
+		struct lock lock;
 	} tile;
 
 	/* modesetting */
diff --git a/sys/dev/drm/nouveau/nouveau_fence.c b/sys/dev/drm/nouveau/nouveau_fence.c
index 472bf9441f..fe12df950d 100644
--- a/sys/dev/drm/nouveau/nouveau_fence.c
+++ b/sys/dev/drm/nouveau/nouveau_fence.c
@@ -58,11 +58,11 @@ void
 nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
 {
 	struct nouveau_fence *fence, *fnext;
-	spin_lock(&fctx->lock);
+	lockmgr(&fctx->lock, LK_EXCLUSIVE);
 	list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
 		nouveau_fence_signal(fence);
 	}
-	spin_unlock(&fctx->lock);
+	lockmgr(&fctx->lock, LK_RELEASE);
 }
 
 void
@@ -102,9 +102,9 @@ nouveau_fence_work(struct nouveau_fence *fence,
 		return;
 	}
 
-	spin_lock(&fctx->lock);
+	lockmgr(&fctx->lock, LK_EXCLUSIVE);
 	if (!fence->channel) {
-		spin_unlock(&fctx->lock);
+		lockmgr(&fctx->lock, LK_RELEASE);
 		kfree(work);
 		func(data);
 		return;
@@ -114,7 +114,7 @@ nouveau_fence_work(struct nouveau_fence *fence,
 	work->func = func;
 	work->data = data;
 	list_add(&work->head, &fence->work);
-	spin_unlock(&fctx->lock);
+	lockmgr(&fctx->lock, LK_RELEASE);
 }
 
 static void
@@ -123,7 +123,7 @@ nouveau_fence_update(struct nouveau_channel *chan)
 	struct nouveau_fence_chan *fctx = chan->fence;
 	struct nouveau_fence *fence, *fnext;
 
-	spin_lock(&fctx->lock);
+	lockmgr(&fctx->lock, LK_EXCLUSIVE);
 	list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
 		if (fctx->read(chan) < fence->sequence)
 			break;
@@ -131,7 +131,7 @@ nouveau_fence_update(struct nouveau_channel *chan)
 		nouveau_fence_signal(fence);
 		nouveau_fence_unref(&fence);
 	}
-	spin_unlock(&fctx->lock);
+	lockmgr(&fctx->lock, LK_RELEASE);
 }
 
 int
@@ -147,9 +147,9 @@ nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
 	ret = fctx->emit(fence);
 	if (!ret) {
 		kref_get(&fence->kref);
-		spin_lock(&fctx->lock);
+		lockmgr(&fctx->lock, LK_EXCLUSIVE);
 		list_add_tail(&fence->head, &fctx->pending);
-		spin_unlock(&fctx->lock);
+		lockmgr(&fctx->lock, LK_RELEASE);
 	}
 
 	return ret;
diff --git a/sys/dev/drm/nouveau/nouveau_fence.h b/sys/dev/drm/nouveau/nouveau_fence.h
index 12675188cc..4a55a5f3c3 100644
--- a/sys/dev/drm/nouveau/nouveau_fence.h
+++ b/sys/dev/drm/nouveau/nouveau_fence.h
@@ -38,7 +38,7 @@ struct nouveau_fence_chan {
 	int  (*emit32)(struct nouveau_channel *, u64, u32);
 	int  (*sync32)(struct nouveau_channel *, u64, u32);
 
-	struct spinlock lock;
+	struct lock lock;
 	u32 sequence;
 };
 
diff --git a/sys/dev/drm/nouveau/nouveau_nv17_fence.c b/sys/dev/drm/nouveau/nouveau_nv17_fence.c
index ca907479f9..d7beb4b47c 100644
--- a/sys/dev/drm/nouveau/nouveau_nv17_fence.c
+++ b/sys/dev/drm/nouveau/nouveau_nv17_fence.c
@@ -42,10 +42,10 @@ nv17_fence_sync(struct nouveau_fence *fence,
 	if (!mutex_trylock(&cli->mutex))
 		return -EBUSY;
 
-	spin_lock(&priv->lock);
+	lockmgr(&priv->lock, LK_EXCLUSIVE);
 	value = priv->sequence;
 	priv->sequence += 2;
-	spin_unlock(&priv->lock);
+	lockmgr(&priv->lock, LK_RELEASE);
 
 	ret = RING_SPACE(prev, 5);
 	if (!ret) {
diff --git a/sys/dev/drm/nouveau/nv10_fence.h b/sys/dev/drm/nouveau/nv10_fence.h
index 453ff86bb5..ade4641014 100644
--- a/sys/dev/drm/nouveau/nv10_fence.h
+++ b/sys/dev/drm/nouveau/nv10_fence.h
@@ -13,7 +13,7 @@ struct nv10_fence_chan {
 struct nv10_fence_priv {
 	struct nouveau_fence_priv base;
 	struct nouveau_bo *bo;
-	struct spinlock lock;
+	struct lock lock;
 	u32 sequence;
 };
 
-- 
2.23.0

