Skip to contentSkip to conversation

drm/nouveau: better alignment of bo sizes and use roundup instead of ALIGN

PATCH In review Maarten Maathuis 1 participant 6 messages Dec 26, 2009 – Dec 27, 2009 4 messages not in the archive

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

PATCH 1/5 drm/nouveau: better alignment of bo sizes and use roundup instead of ALIGN

- Aligning to block size should ensure that the extra size is enough.
- Using roundup, because not all sizes are powers of two.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c    |    8 ++++++--
 drivers/gpu/drm/nouveau/nouveau_fbcon.c |    4 ++--
 2 files changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +6 -2
index 98c46bd..fd767e7 100644
@@ -73,6 +73,7 @@ nouveau_bo_fixup_align(struct drm_device *dev,
 		case 0x4800:
 		case 0x7a00:
 			if (dev_priv->chipset >= 0xA0) {
+				*size = roundup(*size, 28672);
 				/* This is based on high end cards with 448 bits
 				 * memory bus, could be different elsewhere.*/
 				*size += 6 * 28672;
@@ -80,9 +81,11 @@ nouveau_bo_fixup_align(struct drm_device *dev,
 				 * but we must also align to page size. */
 				*align = 2 * 8 * 28672;
 			} else if (dev_priv->chipset >= 0x90) {
+				*size = roundup(*size, 16384);
 				*size += 3 * 16384;
 				*align = 12 * 16834;
 			} else {
+				*size = roundup(*size, 8192);
 				*size += 3 * 8192;
 				/* 12 * 8192 is the actual alignment requirement
 				 * but we must also align to page size. */
@@ -110,10 +113,11 @@ nouveau_bo_fixup_align(struct drm_device *dev,
 		}
 	}
 
-	*size = ALIGN(*size, PAGE_SIZE);
+	/* ALIGN works only on powers of two. */
+	*size = roundup(*size, PAGE_SIZE);
 
 	if (dev_priv->card_type == NV_50) {
-		*size = ALIGN(*size, 65536);
+		*size = roundup(*size, 65536);
 		*align = max(65536, *align);
 	}
 }
drivers/gpu/drm/nouveau/nouveau_fbcon.c +2 -2
index 84af25c..44cbbee 100644
@@ -212,11 +212,11 @@ nouveau_fbcon_create(struct drm_device *dev, uint32_t fb_width,
 
 	mode_cmd.bpp = surface_bpp;
 	mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3);
-	mode_cmd.pitch = ALIGN(mode_cmd.pitch, 256);
+	mode_cmd.pitch = roundup(mode_cmd.pitch, 256);
 	mode_cmd.depth = surface_depth;
 
 	size = mode_cmd.pitch * mode_cmd.height;
-	size = ALIGN(size, PAGE_SIZE);
+	size = roundup(size, PAGE_SIZE);
 
 	ret = nouveau_gem_new(dev, dev_priv->channel, size, 0, TTM_PL_FLAG_VRAM,
 			      0, 0x0000, false, true, &nvbo);
Signature · 1 line
1.6.6.rc4

A message that is not in the archive (sent off-list or not yet received): 1261863788-8318-1-git-send-email-madman2003-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH 2/5 drm/nv50: improve handling of tiled buffers wrt to cpu/gpu access

- Always make sure they are in vram (this is the only place where we have the proper aperture).
- Hide the padding away from users, accessing this can cause problems for neighbour buffer objects.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c  |   20 ++++++++++++++++----
 drivers/gpu/drm/nouveau/nouveau_drv.h |    2 ++
 drivers/gpu/drm/nouveau/nouveau_gem.c |   15 ++++++++++++++-
 3 files changed, 32 insertions(+), 5 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +16 -4
index fd767e7..a052016 100644
@@ -130,7 +130,7 @@ nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
 {
 	struct drm_nouveau_private *dev_priv = dev->dev_private;
 	struct nouveau_bo *nvbo;
-	int ret = 0;
+	int ret = 0, original_size;
 
 	nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
 	if (!nvbo)
@@ -142,7 +142,17 @@ nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
 	nvbo->tile_mode = tile_mode;
 	nvbo->tile_flags = tile_flags;
 
+	/* NV50: align to page size, any extra size after that is padding. */
+	if (dev_priv->card_type == NV_50)
+		original_size = size = roundup(size, 65536);
+	else
+		original_size = size = roundup(size, PAGE_SIZE);
 	nouveau_bo_fixup_align(dev, tile_mode, tile_flags, &align, &size);
+	/* Touching this padding is dangerous as it will interfere with other
+	 * buffer objects.
+	 */
+	if (dev_priv->card_type == NV_50)
+		nvbo->padding = (size - original_size) >> PAGE_SHIFT;
 	align >>= PAGE_SHIFT;
 
 	nvbo->placement.fpfn = 0;
@@ -277,7 +287,8 @@ nouveau_bo_map(struct nouveau_bo *nvbo)
 	if (ret)
 		return ret;
 
-	ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
+	ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages - nvbo->padding,
+					&nvbo->kmap);
 	ttm_bo_unreserve(&nvbo->bo);
 	return ret;
 }
@@ -661,8 +672,9 @@ nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
 	if (dev_priv->card_type == NV_50) {
 		ret = nv50_mem_vm_bind_linear(dev,
 					      offset + dev_priv->vm_vram_base,
-					      new_mem->size, nvbo->tile_flags,
-					      offset);
+					      new_mem->size -
+					      (nvbo->padding << PAGE_SHIFT),
+					      nvbo->tile_flags, offset);
 		if (ret)
 			return ret;
 
drivers/gpu/drm/nouveau/nouveau_drv.h +2 -0
index 7da88a9..ed9bb0d 100644
@@ -91,6 +91,8 @@ struct nouveau_bo {
 
 	uint32_t tile_mode;
 	uint32_t tile_flags;
+	/* tile_flags dependent padding (in pages). */
+	int padding;
 	struct nouveau_tile_reg *tile;
 
 	struct drm_gem_object *gem;
drivers/gpu/drm/nouveau/nouveau_gem.c +14 -1
index 18fd8ac..427b43c 100644
@@ -95,7 +95,7 @@ nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep)
 	else
 		rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
 
-	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
+	rep->size = (nvbo->bo.mem.num_pages - nvbo->padding) << PAGE_SHIFT;
 	rep->offset = nvbo->bo.offset;
 	rep->map_handle = nvbo->mappable ? nvbo->bo.addr_space_offset : 0;
 	rep->tile_mode = nvbo->tile_mode;
@@ -908,6 +908,19 @@ nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
 		return ret;
 	nvbo = nouveau_gem_object(gem);
 
+	/* Buffer objects with tile flags need to be in vram, so move them. */
+	if (nvbo->tile_flags) {
+		ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
+		if (ret)
+			return ret;
+
+		ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, false, false);
+		if (ret)
+			return ret;
+
+		ttm_bo_unreserve(&nvbo->bo);
+	}
+
 	if (nvbo->cpu_filp) {
 		if (nvbo->cpu_filp == file_priv)
 			goto out;
Signature · 1 line
1.6.6.rc4

A message that is not in the archive (sent off-list or not yet received): 1261863788-8318-2-git-send-email-madman2003-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#4 lore raw

PATCH 3/5 drm/nv50: fix align typo for g9x

- The resulting misallignment was causing stuff to be overwritten everywhere, resulting in card hangs eventually.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +1 -1
index a052016..0a08ece 100644
@@ -83,7 +83,7 @@ nouveau_bo_fixup_align(struct drm_device *dev,
 			} else if (dev_priv->chipset >= 0x90) {
 				*size = roundup(*size, 16384);
 				*size += 3 * 16384;
-				*align = 12 * 16834;
+				*align = 12 * 16384;
 			} else {
 				*size = roundup(*size, 8192);
 				*size += 3 * 8192;
Signature · 1 line
1.6.6.rc4

A message that is not in the archive (sent off-list or not yet received): 1261863788-8318-3-git-send-email-madman2003-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#6 lore raw

PATCH 4/5 drm/nv50: make the blocksize depend on vram size

- This should be better than what we have now.
- I'm less sure about the non power of two path.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c |   36 +++++++++++++++++----------------
 1 files changed, 19 insertions(+), 17 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +19 -17
index 0a08ece..8143612 100644
@@ -33,6 +33,8 @@
 #include "nouveau_drv.h"
 #include "nouveau_dma.h"
 
+#include <linux/log2.h>
+
 static void
 nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
 {
@@ -67,29 +69,29 @@ nouveau_bo_fixup_align(struct drm_device *dev,
 	 * avoid corruption of other buffer objects.
 	 */
 	if (dev_priv->card_type == NV_50) {
+		uint32_t block_size = nouveau_mem_fb_amount(dev) >> 15;
+		int i = 1;
+
 		switch (tile_flags) {
 		case 0x1800:
 		case 0x2800:
 		case 0x4800:
 		case 0x7a00:
-			if (dev_priv->chipset >= 0xA0) {
-				*size = roundup(*size, 28672);
-				/* This is based on high end cards with 448 bits
-				 * memory bus, could be different elsewhere.*/
-				*size += 6 * 28672;
-				/* 8 * 28672 is the actual alignment requirement
-				 * but we must also align to page size. */
-				*align = 2 * 8 * 28672;
-			} else if (dev_priv->chipset >= 0x90) {
-				*size = roundup(*size, 16384);
-				*size += 3 * 16384;
-				*align = 12 * 16384;
+			*size = roundup(*size, block_size);
+			if (is_power_of_2(block_size)) {
+				*size += 3 * block_size;
+				for (i = 1; i < 10; i++) {
+					*align = 12 * i * block_size;
+					if (!(*align % 65536))
+						break;
+				}
 			} else {
-				*size = roundup(*size, 8192);
-				*size += 3 * 8192;
-				/* 12 * 8192 is the actual alignment requirement
-				 * but we must also align to page size. */
-				*align = 2 * 12 * 8192;
+				*size += 6 * block_size;
+				for (i = 1; i < 10; i++) {
+					*align = 8 * i * block_size;
+					if (!(*align % 65536))
+						break;
+				}
 			}
 			break;
 		default:
Signature · 1 line
1.6.6.rc4

A message that is not in the archive (sent off-list or not yet received): 1261863788-8318-4-git-send-email-madman2003-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#8 lore raw

PATCH 5/5 drm/nv50: synchronize user channel after buffer object move on kernel channel

- This is not yet a generic implementation that will work everywhere, but it's a start.
- This will fix the corruption surrounding pixmap/texture bo moves on nv50.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c      |    7 ++
 drivers/gpu/drm/nouveau/nouveau_channel.c |    9 ++-
 drivers/gpu/drm/nouveau/nouveau_dma.c     |   26 +++++++
 drivers/gpu/drm/nouveau/nouveau_dma.h     |   11 ++-
 drivers/gpu/drm/nouveau/nouveau_drv.h     |   11 +++
 drivers/gpu/drm/nouveau/nouveau_fence.c   |  103 +++++++++++++++++++++++++++++
 drivers/gpu/drm/nouveau/nouveau_object.c  |    2 +-
 drivers/gpu/drm/nouveau/nv50_graph.c      |   16 +++++
 8 files changed, 179 insertions(+), 6 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +7 -0
index 8143612..cd4986a 100644
@@ -474,6 +474,13 @@ nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
 	if (ret)
 		return ret;
 
+	/* Make the user channel wait for the kernel channel to be done. */
+	if (nvbo->channel && chan != nvbo->channel) {
+		ret = nouveau_fence_sync(nvbo->channel, fence);
+		if (ret)
+			return ret;
+	}
+
 	ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL,
 					evict, no_wait, new_mem);
 	nouveau_fence_unref((void *)&fence);
drivers/gpu/drm/nouveau/nouveau_channel.c +8 -1
index 9aaa972..c1ac34b 100644
@@ -414,7 +414,14 @@ nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
 		init->subchan[0].grclass = 0x0039;
 	else
 		init->subchan[0].grclass = 0x5039;
-	init->nr_subchan = 1;
+	if (dev_priv->card_type >= NV_50) {
+		init->subchan[1].handle = NvSw;
+		init->subchan[1].grclass = NV50_NVSW;
+	}
+	if (dev_priv->card_type < NV_50)
+		init->nr_subchan = 1;
+	else
+		init->nr_subchan = 2;
 
 	/* Named memory object area */
 	ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
drivers/gpu/drm/nouveau/nouveau_dma.c +26 -0
index 7035536..7e66cc8 100644
@@ -52,6 +52,23 @@ nouveau_dma_init(struct nouveau_channel *chan)
 	if (ret)
 		return ret;
 
+	/* Allocate what we need for (simple) cross channel synchronisation. */
+	if (dev_priv->card_type >= NV_50) {
+		struct nouveau_gpuobj *nvsw = NULL;
+
+		ret = nouveau_gpuobj_sw_new(chan, NV50_NVSW, &nvsw);
+		if (ret)
+			return ret;
+
+		ret = nouveau_gpuobj_ref_add(dev, chan, NvSw, nvsw, NULL);
+		if (ret)
+			return ret;
+
+		ret = nouveau_notifier_alloc(chan, NvNotify1, 32, &chan->sync_ntfy);
+		if (ret)
+			return ret;
+	}
+
 	/* Map push buffer */
 	ret = nouveau_bo_map(chan->pushbuf_bo);
 	if (ret)
@@ -87,6 +104,15 @@ nouveau_dma_init(struct nouveau_channel *chan)
 	BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_NOTIFY, 1);
 	OUT_RING(chan, NvNotify0);
 
+	/* Bind NvSw to channel. */
+	if (dev_priv->card_type >= NV_50) {
+		ret = RING_SPACE(chan, 2);
+		if (ret)
+			return ret;
+		BEGIN_RING(chan, NvSubSw, 0, 1);
+		OUT_RING(chan, NvSw);
+	}
+
 	/* Sit back and pray the channel works.. */
 	FIRE_RING(chan);
 
drivers/gpu/drm/nouveau/nouveau_dma.h +7 -4
index 04e85d8..3c74902 100644
@@ -46,10 +46,11 @@
 /* Hardcoded object assignments to subchannels (subchannel id). */
 enum {
 	NvSubM2MF	= 0,
-	NvSub2D		= 1,
-	NvSubCtxSurf2D  = 1,
-	NvSubGdiRect    = 2,
-	NvSubImageBlit  = 3
+	NvSubSw            = 1,
+	NvSub2D		= 2,
+	NvSubCtxSurf2D  = 2,
+	NvSubGdiRect    = 3,
+	NvSubImageBlit  = 4
 };
 
 /* Object handles. */
@@ -67,6 +68,8 @@ enum {
 	NvClipRect	= 0x8000000b,
 	NvGdiRect	= 0x8000000c,
 	NvImageBlit	= 0x8000000d,
+	NvSw		= 0x8000000e,
+	NvNotify1		= 0x8000000f,
 
 	/* G80+ display objects */
 	NvEvoVRAM	= 0x01000000,
drivers/gpu/drm/nouveau/nouveau_drv.h +11 -0
index ed9bb0d..1857b79 100644
@@ -230,6 +230,7 @@ struct nouveau_channel {
 
 	/* GPU object info for stuff used in-kernel (mm_enabled) */
 	uint32_t m2mf_ntfy;
+	uint32_t sync_ntfy;
 	uint32_t vram_handle;
 	uint32_t gart_handle;
 	bool accel_done;
@@ -790,6 +791,8 @@ extern int nouveau_gpuobj_gart_dma_new(struct nouveau_channel *,
 				       uint32_t *o_ret);
 extern int nouveau_gpuobj_gr_new(struct nouveau_channel *, int class,
 				 struct nouveau_gpuobj **);
+extern int nouveau_gpuobj_sw_new(struct nouveau_channel *, int class,
+				 struct nouveau_gpuobj **);
 extern int nouveau_ioctl_grobj_alloc(struct drm_device *, void *data,
 				     struct drm_file *);
 extern int nouveau_ioctl_gpuobj_free(struct drm_device *, void *data,
@@ -1134,6 +1137,12 @@ 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 int nouveau_fence_sync(struct nouveau_channel *chan,
+				struct nouveau_fence *fence);
+extern int nouveau_fence_semaphore_pre_acquire(struct nouveau_channel *chan,
+				int sequence);
+extern int nouveau_fence_semaphore_flush(struct nouveau_channel *chan,
+				int channel);
 
 /* nouveau_gem.c */
 extern int nouveau_gem_new(struct drm_device *, struct nouveau_channel *,
@@ -1341,5 +1350,7 @@ nv_two_reg_pll(struct drm_device *dev)
 #define NV50_NVSW_VBLSEM_OFFSET                                      0x00000400
 #define NV50_NVSW_VBLSEM_RELEASE_VALUE                               0x00000404
 #define NV50_NVSW_VBLSEM_RELEASE                                     0x00000408
+#define NV50_NVSW_SEMAPHORE_PRE_ACQUIRE                       0x00000500
+#define NV50_NVSW_SEMAPHORE_FLUSH                                  0x00000504
 
 #endif /* __NOUVEAU_DRV_H__ */
drivers/gpu/drm/nouveau/nouveau_fence.c +103 -0

106 lines not on this page · raw message

drivers/gpu/drm/nouveau/nouveau_object.c +1 -1
index 93379bb..6c2cf81 100644
@@ -881,7 +881,7 @@ nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
 	return 0;
 }
 
-static int
+int
 nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
 		      struct nouveau_gpuobj **gpuobj_ret)
 {
drivers/gpu/drm/nouveau/nv50_graph.c +16 -0
index ca79f32..9585537 100644
@@ -367,11 +367,27 @@ nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan, int grclass,
 	return 0;
 }
 
+static int
+nv50_graph_semaphore_pre_acquire(struct nouveau_channel *chan, int grclass,
+			       int mthd, uint32_t data)
+{
+	return nouveau_fence_semaphore_pre_acquire(chan, data);
+}
+
+static int
+nv50_graph_semaphore_flush(struct nouveau_channel *chan, int grclass,
+			       int mthd, uint32_t data)
+{
+	return nouveau_fence_semaphore_flush(chan, data);
+}
+
 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 },
+	{ 0x0500, nv50_graph_semaphore_pre_acquire },
+	{ 0x0504, nv50_graph_semaphore_flush },
 	{}
 };
 
Signature · 1 line
1.6.6.rc4
#9 lore raw
In reply to a message not in the archive

pushed 1 to 4

Quoted 69 lines · Maarten Maathuis

- Aligning to block size should ensure that the extra size is enough.
- Using roundup, because not all sizes are powers of two.

---
 drivers/gpu/drm/nouveau/nouveau_bo.c    |    8 ++++++-- drivers/gpu/drm/nouveau/nouveau_fbcon.c |    4 ++--

 2 files changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +0 -0
index 98c46bd..fd767e7 100644
@@ -73,6 +73,7 @@ nouveau_bo_fixup_align(struct drm_device *dev,

               case 0x4800:
               case 0x7a00:
                       if (dev_priv->chipset >= 0xA0) {
+                               *size = roundup(*size, 28672);
                               /* This is based on high end cards with 448 bits
                                * memory bus, could be different elsewhere.*/ *size += 6 * 28672;

drivers/gpu/drm/nouveau/nouveau_bo.c +5 -2
@@ -80,9 +81,11 @@ nouveau_bo_fixup_align(struct drm_device *dev,
                                 * but we must also align to page size. */
                                *align = 2 * 8 * 28672;
                        } else if (dev_priv->chipset >= 0x90) {
+                               *size = roundup(*size, 16384);
                                *size += 3 * 16384;
                                *align = 12 * 16834;
                        } else {
+                               *size = roundup(*size, 8192);
                                *size += 3 * 8192;
                                /* 12 * 8192 is the actual alignment requirement
                                 * but we must also align to page size. */
@@ -110,10 +113,11 @@ nouveau_bo_fixup_align(struct drm_device *dev,
                }
        }
 
-       *size = ALIGN(*size, PAGE_SIZE);
+       /* ALIGN works only on powers of two. */
+       *size = roundup(*size, PAGE_SIZE);
 
        if (dev_priv->card_type == NV_50) {
-               *size = ALIGN(*size, 65536);
+               *size = roundup(*size, 65536);
                *align = max(65536, *align);
        }
  }
drivers/gpu/drm/nouveau/nouveau_fbcon.c +2 -3
index 84af25c..44cbbee 100644
@@ -212,11 +212,11 @@ nouveau_fbcon_create(struct drm_device *dev, uint32_t fb_width,
 
        mode_cmd.bpp = surface_bpp;
        mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3);
-       mode_cmd.pitch = ALIGN(mode_cmd.pitch, 256);
+       mode_cmd.pitch = roundup(mode_cmd.pitch, 256);
        mode_cmd.depth = surface_depth;
 
        size = mode_cmd.pitch * mode_cmd.height;
-       size = ALIGN(size, PAGE_SIZE);
+       size = roundup(size, PAGE_SIZE);
 
        ret = nouveau_gem_new(dev, dev_priv->channel, size, 0, TTM_PL_FLAG_VRAM,
                              0, 0x0000, false, true, &nvbo);
--

1.6.6.rc4