Skip to contentSkip to conversation

Introduce nouveau_bo_wait for waiting on a BO with a GPU channel (v2)

PATCH New Luca Barbieri 1 participant 3 messages Feb 9, 2010 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 Introduce nouveau_bo_wait for waiting on a BO with a GPU channel (v2)

Changes in v2:
- Addressed review comments

nouveau_bo_wait will make the GPU channel wait for fence if possible, otherwise falling back to waiting with the CPU using ttm_bo_wait.

The nouveau_fence_sync function currently returns -ENOSYS, and is the focus of the next patch.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c    |   68 ++++++++++++++++++++++++++++++-
 drivers/gpu/drm/nouveau/nouveau_drv.h   |    2 +
 drivers/gpu/drm/nouveau/nouveau_fence.c |    6 +++
 drivers/gpu/drm/nouveau/nouveau_gem.c   |   20 +++++----
 4 files changed, 86 insertions(+), 10 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +67 -1
index 028719f..2da6acf 100644
@@ -35,6 +35,70 @@
 
 #include <linux/log2.h>
 
+int
+nouveau_bo_wait(struct ttm_buffer_object *bo, struct nouveau_channel *chan)
+{
+	int ret = 0;
+
+	if (likely(!bo->sync_obj))
+		return 0;
+
+	spin_lock(&bo->lock);
+	if (chan) {
+		struct nouveau_fence *new_fence;
+		struct nouveau_channel *waited_chan;
+
+		do {
+			struct nouveau_fence *prev_fence;
+			prev_fence = bo->sync_obj;
+
+			waited_chan = nouveau_fence_channel(prev_fence);
+			if (likely(!waited_chan || waited_chan == chan))
+				break;
+
+			nouveau_fence_ref(prev_fence);
+
+			ret = ttm_bo_wait(bo, false, false, true);
+			if (!ret)
+				goto unref_break;
+
+			if (unlikely(prev_fence != bo->sync_obj))
+				goto unref_continue;
+
+			spin_unlock(&bo->lock);
+			new_fence = nouveau_fence_sync(prev_fence, chan);
+			spin_lock(&bo->lock);
+
+			if (likely(!IS_ERR(new_fence))) {
+				if (likely(bo->sync_obj)) {
+					if (unlikely(bo->sync_obj != prev_fence)) {
+						nouveau_fence_unref((void **)&new_fence);
+						continue;
+					}
+					nouveau_fence_unref((void **)&bo->sync_obj);
+				}
+				bo->sync_obj = new_fence;
+				ret = 0;
+unref_break:
+				nouveau_fence_unref((void **)&prev_fence);
+				break;
+			}
+
+			if (unlikely(prev_fence != bo->sync_obj)) {
+unref_continue:
+				nouveau_fence_unref((void **)&prev_fence);
+				continue;
+			}
+
+			nouveau_fence_unref((void **)&prev_fence);
+			ret = ttm_bo_wait(bo, false, false, false);
+		} while (0);
+	} else
+		ret = ttm_bo_wait(bo, false, false, false);
+	spin_unlock(&bo->lock);
+	return ret;
+}
+
 static void
 nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
 {
@@ -469,8 +533,10 @@ nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
 
 	ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL,
 					evict, no_wait, new_mem);
+
+	/* TODO: this should be redundant, since we do the check in validate */
 	if (nvbo->channel && nvbo->channel != chan)
-		ret = nouveau_fence_wait(fence, NULL, false, false);
+		ret = nouveau_bo_wait(&nvbo->bo, nvbo->channel);
 	nouveau_fence_unref((void *)&fence);
 	return ret;
 }
drivers/gpu/drm/nouveau/nouveau_drv.h +2 -0
index 64987a9..bb9024c 100644
@@ -1111,6 +1111,7 @@ extern int nv04_crtc_create(struct drm_device *, int index);
 
 /* nouveau_bo.c */
 extern struct ttm_bo_driver nouveau_bo_driver;
+extern int nouveau_bo_wait(struct ttm_buffer_object *bo, struct nouveau_channel *chan);
 extern int nouveau_bo_new(struct drm_device *, struct nouveau_channel *,
 			  int size, int align, uint32_t flags,
 			  uint32_t tile_mode, uint32_t tile_flags,
@@ -1136,6 +1137,7 @@ extern int nouveau_fence_emit(struct nouveau_fence *);
 struct nouveau_channel *nouveau_fence_channel(struct nouveau_fence *);
 extern bool nouveau_fence_signalled(void *obj, void *arg);
 extern int nouveau_fence_wait(void *obj, void *arg, bool lazy, bool intr);
+extern struct nouveau_fence *nouveau_fence_sync(struct nouveau_fence *, struct nouveau_channel *);
 extern int nouveau_fence_flush(void *obj, void *arg);
 extern void nouveau_fence_unref(void **obj);
 extern void *nouveau_fence_ref(void *obj);
drivers/gpu/drm/nouveau/nouveau_fence.c +6 -0
index faddf53..9b1c2c3 100644
@@ -56,6 +56,12 @@ nouveau_fence_del(struct kref *ref)
 	kfree(fence);
 }
 
+struct nouveau_fence*
+nouveau_fence_sync(struct nouveau_fence *waited_fence, struct nouveau_channel *chan)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
 void
 nouveau_fence_update(struct nouveau_channel *chan)
 {
drivers/gpu/drm/nouveau/nouveau_gem.c +11 -9
index 34063c5..f73ac83 100644
@@ -354,15 +354,6 @@ validate_list(struct nouveau_channel *chan, struct list_head *list,
 
 	list_for_each_entry(nvbo, list, entry) {
 		struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
-		struct nouveau_fence *prev_fence = nvbo->bo.sync_obj;
-
-		if (prev_fence && nouveau_fence_channel(prev_fence) != chan) {
-			spin_lock(&nvbo->bo.lock);
-			ret = ttm_bo_wait(&nvbo->bo, false, false, false);
-			spin_unlock(&nvbo->bo.lock);
-			if (unlikely(ret))
-				return ret;
-		}
 
 		ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
 					     b->write_domains,
@@ -377,6 +368,17 @@ validate_list(struct nouveau_channel *chan, struct list_head *list,
 		if (unlikely(ret))
 			return ret;
 
+		/* we must wait *after* validation, since we do the move
+		   with the kernel channel.
+
+		    Note that this may spin/sleep on a fence
+		    TODO: is this a good idea, or should we bail and retry?
+		  */
+		ret = nouveau_bo_wait(&nvbo->bo, chan);
+		if (unlikely(ret))
+			return ret;
+
+
 		if (nvbo->bo.offset == b->presumed_offset &&
 		    ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
 		      b->presumed_domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
Signature · 1 line
1.6.6.1.476.g01ddb

A message that is not in the archive (sent off-list or not yet received): 1265706036-7452-1-git-send-email-luca-Ukmtq+NC3rhBHFWNQifrYwC/[email protected]

#2 lore raw

PATCH 2/3 drm/nouveau: add lockless dynamic semaphore allocator (v2)

Changes in v2:
- Addressed review comments
- Fixed lockless algorithm (must not dec if negative in addition to if 0)
- Made spinlock irqsave (fences are completed in IRQs)

This patch adds code to allocate semaphores in a dynamic way using a lockless algorithm.

1. Semaphore BOs

Semaphore BOs are BOs containing semaphores. Each is 4KB large and contains 1024 4-byte semaphores. They are pinned and mapped.

Semaphore BOs are allocated on-demand and freed at device takedown. Those that are not fully allocated are kept on a free list.

Each is assigned an handle. DMA objects and references are created on demand for each channel that needs to use a semaphore BO. Those objects and references are automatically destroyed at channel destruction time.

Typically only a single semaphore BO will be used.

2. Semaphore allocation

Each semaphore BO contains a bitmask of free semaphores within the BO. Allocation is done in a lockless fashion using a count of free semaphores and the bitmask.

Semaphores are released once the fence on the waiting side passed. This is done by adding fields to nouveau_fence.

Semaphore values are zeroed when the semaphore BO is allocated, and are afterwards only modified by the GPU.

This is performed by storing a bitmask that allows to alternate between using the values 0 and 1 for a given semaphore.

---
 drivers/gpu/drm/nouveau/nouveau_drv.h   |    9 +
 drivers/gpu/drm/nouveau/nouveau_fence.c |  265 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/nouveau/nouveau_state.c |    4 +
 3 files changed, 278 insertions(+), 0 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drv.h +9 -0
index bb9024c..93e5427 100644
@@ -621,6 +621,13 @@ struct drm_nouveau_private {
 	struct {
 		struct dentry *channel_root;
 	} debugfs;
+
+	struct {
+		spinlock_t free_list_lock;
+		struct nouveau_sem_bo *free_list;
+		uint32_t handles;
+		uint32_t max_handles;
+	} sem;
 };
 
 static inline struct drm_nouveau_private *
@@ -1142,6 +1149,8 @@ extern int nouveau_fence_flush(void *obj, void *arg);
 extern void nouveau_fence_unref(void **obj);
 extern void *nouveau_fence_ref(void *obj);
 extern void nouveau_fence_handler(struct drm_device *dev, int channel);
+extern void nouveau_fence_device_init(struct drm_device *dev);
+extern void nouveau_fence_device_takedown(struct drm_device *dev);
 
 /* nouveau_gem.c */
 extern int nouveau_gem_new(struct drm_device *, struct nouveau_channel *,
drivers/gpu/drm/nouveau/nouveau_fence.c +265 -0

280 lines not on this page · raw message

drivers/gpu/drm/nouveau/nouveau_state.c +4 -0
index 3586667..bd3c43a 100644
@@ -413,6 +413,8 @@ nouveau_card_init(struct drm_device *dev)
 	if (ret)
 		goto out_mem;
 
+	nouveau_fence_device_init(dev);
+
 	/* PMC */
 	ret = engine->mc.init(dev);
 	if (ret)
@@ -533,6 +535,8 @@ static void nouveau_card_takedown(struct drm_device *dev)
 		engine->timer.takedown(dev);
 		engine->mc.takedown(dev);
 
+		nouveau_fence_device_takedown(dev);
+
 		mutex_lock(&dev->struct_mutex);
 		ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
 		ttm_bo_clean_mm(&dev_priv->ttm.bdev, TTM_PL_TT);
Signature · 1 line
1.6.6.1.476.g01ddb
#3 lore raw

PATCH 3/3 Use semaphores for fully on-GPU interchannel synchronization (v2)

In reply to a message not in the archive

Changes in v2:
- Addressed review comments

This patch implements the nouveau_fence_sync interface introduced in the previous patch using dynamically allocated semaphores, introduced in the other previous patch.

This is tested on NV40, but should work on NV17-NV50 (previous cards will just fallback to CPU waiting).

Unlike a previously posted patch, this patch does not make any use of software methods and is designed to do all work on the GPU, and be as fast as possible.

To perform inter-channel synchronization, commands are emitted on both channels involved.

First, a semaphore is allocated, and a valid handle for it is inserted in the channel if necessary.

DMA_SEMAPHORE is set only if different from the last used one. This is usually not the case, and thus SEMAPHORE interrupts only happen once per channel usually.

After that, SEMAPHORE_OFFSET is set if changed and then either ACQUIRE or RELEASE is used.

On the waiting channel, a fence is also emitted. Once that fence expires, the semaphore is released and can be reused for any purpose.

This results in synchronization taking place fully on the GPU, with no CPU waiting necessary.

---
 drivers/gpu/drm/nouveau/nouveau_drv.h   |    7 ++
 drivers/gpu/drm/nouveau/nouveau_fence.c |  136 +++++++++++++++++++++++++++++--
 2 files changed, 136 insertions(+), 7 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drv.h +7 -0
index 93e5427..d3aa20e 100644
@@ -195,6 +195,8 @@ struct nouveau_channel {
 		uint32_t sequence;
 		uint32_t sequence_ack;
 		uint32_t last_sequence_irq;
+		atomic_t sem_count;
+		unsigned sem_threshold;
 	} fence;
 
 	/* DMA push buffer */
@@ -255,6 +257,11 @@ struct nouveau_channel {
 		char name[32];
 		struct drm_info_list info;
 	} debugfs;
+
+	struct {
+		unsigned handle;
+		unsigned num;
+	} sem;
 };
 
 struct nouveau_instmem_engine {
drivers/gpu/drm/nouveau/nouveau_fence.c +129 -7

179 lines not on this page · raw message

Signature · 1 line
1.6.6.1.476.g01ddb