drm/ttm: nouveau: memory coherency fixes for ARM
Subsystems (5)
cover drm/ttm: nouveau: memory coherency fixes for ARM
This small series introduces TTM helper functions as well as Nouveau hooks that are needed to ensure buffer coherency on ARM. Most of this series is a forward-port of some patches Lucas Stach sent last year and that are also needed for Nouveau GK20A support:
http://lists.freedesktop.org/archives/nouveau/2013-August/014026.html
Another patch takes care of flushing the CPU write-buffer when writing BOs through a non-BAR path.
Alexandre Courbot (1):
drm/nouveau: introduce CPU cache flushing macro
Lucas Stach (3):
drm/ttm: recognize ARM arch in ioprot handler
drm/ttm: introduce dma cache sync helpers
drm/nouveau: hook up cache sync functions
drivers/gpu/drm/nouveau/core/os.h | 17 +++++++++++++++ drivers/gpu/drm/nouveau/nouveau_bo.c | 40 +++++++++++++++++++++++++++++++++-- drivers/gpu/drm/nouveau/nouveau_bo.h | 20 ++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_gem.c | 8 ++++++- drivers/gpu/drm/ttm/ttm_bo_util.c | 2 +- drivers/gpu/drm/ttm/ttm_tt.c | 25 ++++++++++++++++++++++ include/drm/ttm/ttm_bo_driver.h | 28 ++++++++++++++++++++++++ 7 files changed, 136 insertions(+), 4 deletions(-)
Signature · 1 line
1.9.2
PATCH 1/4 drm/ttm: recognize ARM arch in ioprot handler
From: Lucas Stach <[email protected]>
--- drivers/gpu/drm/ttm/ttm_bo_util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_util.c +1 -1
@@ -500,7 +500,7 @@ pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp) pgprot_val(tmp) |= _PAGE_GUARDED; }#endif -#if defined(__ia64__) +#if defined(__ia64__) || defined(__arm__)if (caching_flags &TTM_PL_FLAG_WC ) tmp =pgprot_writecombine (tmp);else
Signature · 1 line
1.9.2
PATCH 2/4 drm/ttm: introduce dma cache sync helpers
In reply to Alexandre CourbotFrom: Lucas Stach <[email protected]>
On arches with non-coherent PCI, we need to flush caches ourselfes at the appropriate places. Introduce two small helpers to make things easy for TTM based drivers.
--- drivers/gpu/drm/ttm/ttm_tt.c | 25 +++++++++++++++++++++++++ include/drm/ttm/ttm_bo_driver.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)
drivers/gpu/drm/ttm/ttm_tt.c +25 -0
@@ -38,6 +38,7 @@#include <linux/swap.h> #include <linux/slab.h> #include <linux/export.h> +#include <linux/dma-mapping.h> #include <drm/drm_cache.h> #include <drm/drm_mem_util.h> #include <drm/ttm/ttm_module.h>
@@ -248,6 +249,30 @@ void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma) }EXPORT_SYMBOL (ttm_dma_tt_fini); +void ttm_dma_tt_cache_sync_for_device (struct ttm_dma_tt *ttm_dma, +struct device *dev) +{ +int i; + +for (i =0 ; i < ttm_dma->ttm.num_pages; i++) { +dma_sync_single_for_device (dev, ttm_dma->dma_address[i], +PAGE_SIZE ,DMA_TO_DEVICE ); + } +} +EXPORT_SYMBOL (ttm_dma_tt_cache_sync_for_device); + +void ttm_dma_tt_cache_sync_for_cpu (struct ttm_dma_tt *ttm_dma, +struct device *dev) +{ +int i; + +for (i =0 ; i < ttm_dma->ttm.num_pages; i++) { +dma_sync_single_for_cpu (dev, ttm_dma->dma_address[i], +PAGE_SIZE ,DMA_FROM_DEVICE ); + } +} +EXPORT_SYMBOL (ttm_dma_tt_cache_sync_for_cpu); +void ttm_tt_unbind (struct ttm_tt *ttm) {int ret;
include/drm/ttm/ttm_bo_driver.h +28 -0
@@ -41,6 +41,7 @@#include <linux/fs.h> #include <linux/spinlock.h> #include <linux/reservation.h> +#include <linux/device.h> struct ttm_backend_func {/**
@@ -690,6 +691,33 @@ extern int ttm_tt_swapout(struct ttm_tt *ttm,*/ extern void ttm_tt_unpopulate (struct ttm_tt *ttm); +/** +* ttm_dma_tt_cache_sync_for_device: +* +* @ttm A struct ttm_tt of the type returned by ttm_dma_tt_init. +* @dev A struct device representing the device to which to sync. +* +* This function will flush the CPU caches on arches where snooping in the +* TT is not available. On fully coherent arches this will turn into an (almost) +* noop. This makes sure that data written by the CPU is visible to the device. +*/ +extern void ttm_dma_tt_cache_sync_for_device (struct ttm_dma_tt *ttm_dma, +struct device *dev); + +/** +* ttm_dma_tt_cache_sync_for_cpu: +* +* @ttm A struct ttm_tt of the type returned by ttm_dma_tt_init. +* @dev A struct device representing the device from which to sync. +* +* This function will invalidate the CPU caches on arches where snooping in the +* TT is not available. On fully coherent arches this will turn into an (almost) +* noop. This makes sure that the CPU does not read any stale cached or +* prefetched data. +*/ +extern void ttm_dma_tt_cache_sync_for_cpu (struct ttm_dma_tt *ttm_dma, +struct device *dev); +/* * ttm_bo.c */
Signature · 1 line
1.9.2
PATCH 3/4 drm/nouveau: hook up cache sync functions
In reply to Alexandre CourbotFrom: Lucas Stach <[email protected]>
--- drivers/gpu/drm/nouveau/nouveau_bo.c | 32 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_bo.h | 20 ++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_gem.c | 8 +++++++- 3 files changed, 59 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +32 -0
@@ -407,6 +407,8 @@ nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible, {int ret; +nouveau_bo_sync_for_device (nvbo); + ret =ttm_bo_validate (&nvbo->bo, &nvbo->placement, interruptible, no_wait_gpu);if (ret)
@@ -487,6 +489,36 @@ nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)return 0 ; } +#ifdef NOUVEAU_NEED_CACHE_SYNC +void +nouveau_bo_sync_for_cpu (struct nouveau_bo *nvbo) +{ +struct nouveau_device *device; +struct ttm_tt *ttm = nvbo->bo.ttm; + + device =nouveau_dev (nouveau_bdev (ttm->bdev)->dev); + +if (nvbo->bo.ttm && nvbo->bo.ttm->caching_state == tt_cached) +ttm_dma_tt_cache_sync_for_cpu ((struct ttm_dma_tt *)nvbo->bo.ttm, +nv_device_base (device)); +} + +void +nouveau_bo_sync_for_device (struct nouveau_bo *nvbo) +{ +struct ttm_tt *ttm = nvbo->bo.ttm; + +if (ttm && ttm->caching_state == tt_cached) { +struct nouveau_device *device; + + device =nouveau_dev (nouveau_bdev (ttm->bdev)->dev); + +ttm_dma_tt_cache_sync_for_device ((struct ttm_dma_tt *)ttm, +nv_device_base (device)); + } +} +#endif +static int nouveau_bo_init_mem_type (struct ttm_bo_device *bdev,uint32_t type,struct ttm_mem_type_manager *man)
drivers/gpu/drm/nouveau/nouveau_bo.h +20 -0
@@ -89,6 +89,26 @@ int nouveau_bo_vma_add(struct nouveau_bo *, struct nouveau_vm *, struct nouveau_vma *);void nouveau_bo_vma_del (struct nouveau_bo *,struct nouveau_vma *); +#if IS_ENABLED (CONFIG_ARCH_TEGRA ) +#define NOUVEAU_NEED_CACHE_SYNC +#endif + +#ifdef NOUVEAU_NEED_CACHE_SYNC +void nouveau_bo_sync_for_cpu (struct nouveau_bo *); +void nouveau_bo_sync_for_device (struct nouveau_bo *); +#else +static inline void +nouveau_bo_sync_for_cpu (struct nouveau_bo *) +{ +} + +static inline void +nouveau_bo_sync_for_device (struct nouveau_bo *) +{ +} +#endif + +/* TODO: submit equivalent to TTM generic API upstream? */ static inline void __iomem *nvbo_kmap_obj_iovirtual (struct nouveau_bo *nvbo)
drivers/gpu/drm/nouveau/nouveau_gem.c +7 -1
@@ -897,7 +897,13 @@ nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, ret =ttm_bo_wait (&nvbo->bo, true, true, no_wait);spin_unlock (&nvbo->bo.bdev->fence_lock);drm_gem_object_unreference_unlocked (gem); -return ret; + +if (ret) +return ret; + +nouveau_bo_sync_for_cpu (nvbo); + +return 0 ; }int
Signature · 1 line
1.9.2
PATCH 4/4 drm/nouveau: introduce CPU cache flushing macro
In reply to Alexandre CourbotSome architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
This patch introduces a macro that flushes the caches on ARM and translates to a no-op on other architectures, and uses it when writing to in-memory BOs. It will also be useful for implementations of instmem that access shared memory directly instead of going through PRAMIN.
--- drivers/gpu/drm/nouveau/core/os.h | 17 +++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_bo.c | 8 ++++++-- 2 files changed, 23 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/core/os.h +17 -0
@@ -38,4 +38,21 @@#endif /* def __BIG_ENDIAN else */ #endif /* !ioread32_native */ +#if defined(__arm__) + +#define nv_cpu_cache_flush_area (va, size) \ +do { \ + phys_addr_t pa = virt_to_phys(va); \ + __cpuc_flush_dcache_area(va, size); \ + outer_flush_range(pa, pa + size); \ +} while (0) + +#else + +#define nv_cpu_cache_flush_area (va, size) \ +do { \ +} while (0) + +#endif /* defined(__arm__) */ +#endif
drivers/gpu/drm/nouveau/nouveau_bo.c +6 -2
@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } } u32
@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + } } static struct ttm_tt *
Signature · 1 line
1.9.2
Re: [PATCH 2/4] drm/ttm: introduce dma cache sync helpers
In reply to Alexandre CourbotQuoted 3 lines · Alexandre Courbot
From: Lucas Stach <[email protected]>
On arches with non-coherent PCI,
I guess since this applies to gk20a
Quoted 1 line
we need to flush caches ourselfes at
"ourselves". Or perhaps even reword to something like: "..., caches need to be flushed and invalidated explicitly", since dma_sync_for_cpu() does invalidate rather than flush.
Quoted 11 lines
the appropriate places. Introduce two small helpers to make things easy for TTM based drivers.
Signed-off-by Lucas Stach <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- drivers/gpu/drm/ttm/ttm_tt.c | 25 +++++++++++++++++++++++++ include/drm/ttm/ttm_bo_driver.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)drivers/gpu/drm/ttm/ttm_tt.c +0
-0
[...]
drivers/gpu/drm/ttm/ttm_tt.c void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma) · line 255
Quoted 4 lines
drivers/gpu/drm/ttm/ttm_tt.c +4
-0+void ttm_dma_tt_cache_sync_for_device (struct ttm_dma_tt *ttm_dma, +struct device *dev) +{ +int i;
This should probably be unsigned long to match the type of ttm_dma->ttm.num_pages.
Thierry
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 3/4] drm/nouveau: hook up cache sync functions
In reply to Alexandre CourbotQuoted 5 lines · Alexandre Courbot
From: Lucas Stach <[email protected]>
Signed-off-by Lucas Stach <[email protected]>[[email protected]: make conditional and platform-friendly]Signed-off-by Alexandre Courbot <[email protected]>
Perhaps having a propery commit message here would be good.
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0
[...]
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) · line 503
Quoted 12 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +12
-0+#ifdef NOUVEAU_NEED_CACHE_SYNC +void +nouveau_bo_sync_for_cpu (struct nouveau_bo *nvbo) +{ +struct nouveau_device *device; +struct ttm_tt *ttm = nvbo->bo.ttm; + + device =nouveau_dev (nouveau_bdev (ttm->bdev)->dev); + +if (nvbo->bo.ttm && nvbo->bo.ttm->caching_state == tt_cached) +ttm_dma_tt_cache_sync_for_cpu ((struct ttm_dma_tt *)nvbo->bo.ttm, +nv_device_base (device));
Can we be certain at this point that the struct ttm_tt is in fact a struct ttm_dma_tt?
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.h +0
-0
[...]
drivers/gpu/drm/nouveau/nouveau_bo.h int nouveau_bo_vma_add(struct nouveau_bo *, struct nouveau_vm *, · line 94
Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_bo.h +3
-0+#if IS_ENABLED (CONFIG_ARCH_TEGRA ) +#define NOUVEAU_NEED_CACHE_SYNC +#endif
I know I gave this as an example myself when we discussed this offline, but I'm now thinking that this might actually be better off in Kconfig.
Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_bo.h +6
-0+#ifdef NOUVEAU_NEED_CACHE_SYNC +void nouveau_bo_sync_for_cpu (struct nouveau_bo *); +void nouveau_bo_sync_for_device (struct nouveau_bo *); +#else +static inline void +nouveau_bo_sync_for_cpu (struct nouveau_bo *)
drivers/gpu/drm/nouveau/nouveau_bo.h int nouveau_bo_vma_add(struct nouveau_bo *, struct nouveau_vm *, · line 111
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.h +10
-0+{ +} + +static inline void +nouveau_bo_sync_for_device (struct nouveau_bo *) +{ +} +#endif + +
There's a gratuituous blank line here.
Quoted 7 lines
drivers/gpu/drm/nouveau/nouveau_gem.c +0
-0@@ -897,7 +897,13 @@ nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, ret =ttm_bo_wait (&nvbo->bo, true, true, no_wait);spin_unlock (&nvbo->bo.bdev->fence_lock);
drivers/gpu/drm/nouveau/nouveau_gem.c nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, · line 907
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_gem.c +7
-1drm_gem_object_unreference_unlocked (gem); -return ret; + +if (ret) +return ret; + +nouveau_bo_sync_for_cpu (nvbo); + +return 0 ; }
This could be rewritten as:
if (!ret) nouveau_bo_sync_for_cpu(nvbo); return ret;
Which would be slightly shorter.
On second thought, perhaps part of nouveau_gem_ioctl_cpu_prep() could be refactored into a separate function to make this more symmetric. If we put that in nouveau_bo.c and name it nouveau_bo_wait() for example, the dummies can go away and both nouveau_bo_sync_for_{cpu,device}() can be made static. I also think that's cleaner because it has both variants of the nouveau_bo_sync_for_*() calls in the same file.
Thierry
_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau
Re: [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
In reply to Alexandre CourbotQuoted 3 lines · Alexandre Courbot
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
It seems to me that the above is generally true for all architectures, not just ARM.
Also: s/explicitely/explicitly/
Quoted 5 lines
This patch introduces a macro that flushes the caches on ARM and translates to a no-op on other architectures, and uses it when writing to in-memory BOs. It will also be useful for implementations of instmem that access shared memory directly instead of going through PRAMIN.
Presumably instmem can access shared memory on all architectures, so this doesn't seem like a property of the architecture but rather of the memory pool backing the instmem.
In that case I wonder if this shouldn't be moved into an operation that is implemented by the backing memory pool and be a noop where the cache doesn't need explicit flushing.
Quoted 6 lines
drivers/gpu/drm/nouveau/core/os.h +0
-0@@ -38,4 +38,21 @@#endif /* def __BIG_ENDIAN else */
drivers/gpu/drm/nouveau/core/os.h line 48
Quoted 10 lines
drivers/gpu/drm/nouveau/core/os.h +8
-0#endif /* !ioread32_native */ +#if defined(__arm__) + +#define nv_cpu_cache_flush_area (va, size) \ +do { \ + phys_addr_t pa = virt_to_phys(va); \ + __cpuc_flush_dcache_area(va, size); \ + outer_flush_range(pa, pa + size); \ +} while (0)
Couldn't this be a static inline function?
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0
[...]
Quoted 16 lines
index
0886f47e5244..b9c9729c5733100644drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } } u32
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) · line 469
Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + }
This looks rather like a sledgehammer to me. Effectively this turns nvbo into an uncached buffer. With additional overhead of constantly flushing caches. Wouldn't it make more sense to locate the places where these are called and flush the cache after all the writes have completed?
Thierry
_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau
Re: [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
Am Montag, den 19.05.2014, 11:02 +0200 schrieb Thierry Reding:
Quoted 8 lines · Alexandre Courbot
Quoted 3 lines
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
It seems to me that the above is generally true for all architectures, not just ARM.
No, on PCI coherent arches, like x86 and some PowerPCs, the GPU will snoop the CPU caches and therefore an explicit cache flush is not required.
Quoted 51 lines
Also: s/explicitely/explicitly/
Quoted 5 lines
This patch introduces a macro that flushes the caches on ARM and translates to a no-op on other architectures, and uses it when writing to in-memory BOs. It will also be useful for implementations of instmem that access shared memory directly instead of going through PRAMIN.
Presumably instmem can access shared memory on all architectures, so this doesn't seem like a property of the architecture but rather of the memory pool backing the instmem.
In that case I wonder if this shouldn't be moved into an operation that is implemented by the backing memory pool and be a noop where the cache doesn't need explicit flushing.
Quoted 6 lines
drivers/gpu/drm/nouveau/core/os.h +0
-0@@ -38,4 +38,21 @@#endif /* def __BIG_ENDIAN else */ drivers/gpu/drm/nouveau/core/os.h line 48
Quoted 10 lines
drivers/gpu/drm/nouveau/core/os.h +8
-0#endif /* !ioread32_native */ +#if defined(__arm__) + +#define nv_cpu_cache_flush_area (va, size) \ +do { \ + phys_addr_t pa = virt_to_phys(va); \ + __cpuc_flush_dcache_area(va, size); \ + outer_flush_range(pa, pa + size); \ +} while (0)Couldn't this be a static inline function?
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
Quoted 14 lines
index
0886f47e5244..b9c9729c5733100644drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } }
Quoted 16 lines
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) · line 469
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1u32@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + }This looks rather like a sledgehammer to me. Effectively this turns nvbo into an uncached buffer. With additional overhead of constantly flushing caches. Wouldn't it make more sense to locate the places where these are called and flush the cache after all the writes have completed?
I don't think the explicit flushing for those things makes sense. I think it is a lot more effective to just map the BOs write-combined on PCI non-coherent arches. This way any writes will be buffered. Reads will be slow, but I don't think nouveau is reading back a lot from those buffers.
Using the write-combining buffer doesn't need any additional synchronization as it will get flushed on pushbuf kickoff anyways.
Regards,
Lucas
Signature · 2 lines
Pengutronix e.K. | Lucas Stach | Industrial Linux Solutions | http://www.pengutronix.de/ |
Re: [PATCH 3/4] drm/nouveau: hook up cache sync functions
In reply to Alexandre CourbotAm Montag, den 19.05.2014, 16:10 +0900 schrieb Alexandre Courbot:
Quoted 25 lines
From: Lucas Stach <[email protected]>
Signed-off-by Lucas Stach <[email protected]>[[email protected]: make conditional and platform-friendly]Signed-off-by Alexandre Courbot <[email protected]>--- drivers/gpu/drm/nouveau/nouveau_bo.c | 32 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_bo.h | 20 ++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_gem.c | 8 +++++++- 3 files changed, 59 insertions(+), 1 deletion(-)drivers/gpu/drm/nouveau/nouveau_bo.c +2
-0@@ -407,6 +407,8 @@ nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible, {int ret; +nouveau_bo_sync_for_device (nvbo); + ret =ttm_bo_validate (&nvbo->bo, &nvbo->placement, interruptible, no_wait_gpu);if (ret)
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) · line 492
Quoted 4 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +1
-0@@ -487,6 +489,36 @@ nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)return 0 ; } +#ifdef NOUVEAU_NEED_CACHE_SYNC
I don't like this ifdef at all. I know calling this functions will add a little overhead to x86 where it isn't strictly required, but I think it's negligible.
When I looked at them the dma_sync_single_for_[device|cpu] functions which are called here map out to just a drain of the PCI store buffer on x86, which should be fast enough to be done unconditionally. They won't so any time-consuming cache synchronization on PCI coherent arches.
Regards,
Lucas
Signature · 2 lines
Pengutronix e.K. | Lucas Stach | Industrial Linux Solutions | http://www.pengutronix.de/ |
Re: [PATCH 3/4] drm/nouveau: hook up cache sync functions
In reply to Thierry RedingAm Montag, den 19.05.2014, 10:46 +0200 schrieb Thierry Reding:
Quoted 11 lines · Alexandre Courbot
Quoted 5 lines
From: Lucas Stach <[email protected]>
Signed-off-by Lucas Stach <[email protected]>[[email protected]: make conditional and platform-friendly]Signed-off-by Alexandre Courbot <[email protected]>Perhaps having a propery commit message here would be good.
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
Quoted 15 lines
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) · line 503
Quoted 12 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +12
-0+#ifdef NOUVEAU_NEED_CACHE_SYNC +void +nouveau_bo_sync_for_cpu (struct nouveau_bo *nvbo) +{ +struct nouveau_device *device; +struct ttm_tt *ttm = nvbo->bo.ttm; + + device =nouveau_dev (nouveau_bdev (ttm->bdev)->dev); + +if (nvbo->bo.ttm && nvbo->bo.ttm->caching_state == tt_cached) +ttm_dma_tt_cache_sync_for_cpu ((struct ttm_dma_tt *)nvbo->bo.ttm, +nv_device_base (device));Can we be certain at this point that the struct
ttm_ttis in fact a structttm_dma_tt?
Yes, for all cases except AGP, where things are mapped WC anyways (so caching_state is always != cached) nouveau_bo_driver uses nouveau_ttm_tt_create() as its ttm_tt_create callback. This in turn calls nouveau_sgdma_create_ttm() which uses ttm_dma_tt_init() unconditionally.
Regards,
Lucas
Signature · 2 lines
Pengutronix e.K. | Lucas Stach | Industrial Linux Solutions | http://www.pengutronix.de/ |
Re: [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
In reply to Lucas StachQuoted 6 lines · Lucas Stach
Am Montag, den 19.05.2014, 11:02 +0200 schrieb Thierry Reding:
Quoted 5 lines · Alexandre Courbot
Quoted 3 lines
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
Quoted 6 lines
Quoted 3 lines
It seems to me that the above is generally true for all architectures, not just ARM.
No, on PCI coherent arches, like x86 and some PowerPCs, the GPU will snoop the CPU caches and therefore an explicit cache flush is not required.
I was criticizing the wording in the commit message. Perhaps it could be enhanced with what you just said.
Quoted 32 lines
Quoted 16 lines
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
Quoted 14 lines
index
0886f47e5244..b9c9729c5733100644drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } }Quoted 16 lines
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) · line 469
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1u32@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + }This looks rather like a sledgehammer to me. Effectively this turns nvbo into an uncached buffer. With additional overhead of constantly flushing caches. Wouldn't it make more sense to locate the places where these are called and flush the cache after all the writes have completed?
Quoted 7 lines
I don't think the explicit flushing for those things makes sense. I think it is a lot more effective to just map the BOs write-combined on PCI non-coherent arches. This way any writes will be buffered. Reads will be slow, but I don't think nouveau is reading back a lot from those buffers.
Using the write-combining buffer doesn't need any additional synchronization as it will get flushed on pushbuf kickoff anyways.
Sounds good to me.
Thierry
Re: [Nouveau] [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
Quoted 10 lines · Thierry Reding
Quoted 9 lines · Lucas Stach
Am Montag, den 19.05.2014, 11:02 +0200 schrieb Thierry Reding:
Quoted 8 lines · Alexandre Courbot
Quoted 3 lines
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
It seems to me that the above is generally true for all architectures, not just ARM.
Quoted 6 lines
Quoted 3 lines
No, on PCI coherent arches, like x86 and some PowerPCs, the GPU will snoop the CPU caches and therefore an explicit cache flush is not required.
I was criticizing the wording in the commit message. Perhaps it could be enhanced with what you just said.
Shouldn't this be done in the dma mapping layer? I know that i915 does all the cpu cache flushing itself, but that's because the x86 dma layer refuses to believe that there are non-coherent platforms on x86. But on arm it can cope.
This is somewhat important for dma-buf buffer sharing since if the cpu cache control is done in drivers you must do double-flushing on shared buffers. Atm you have to do that anyway, but at least this would make it easier. The other problem is that ttm reinvents half of the dma mapping functions.
Just my 2 cents.
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Re: [PATCH 2/4] drm/ttm: introduce dma cache sync helpers
In reply to Thierry RedingQuoted 5 lines · Thierry Reding
Quoted 3 lines · Alexandre Courbot
From: Lucas Stach <[email protected]>
On arches with non-coherent PCI,
Quoted 7 lines
I guess since this applies to gk20a
Quoted 1 line
we need to flush caches ourselfes at
"ourselves". Or perhaps even reword to something like: "..., caches need to be flushed and invalidated explicitly", since
dma_sync_for_cpu()does invalidate rather than flush.
Rephrased as "On arches for which access to GPU memory is non-coherent, caches need to be flushed and invalidated explicitly at the appropriate places."
Quoted 13 lines
Quoted 12 lines
the appropriate places. Introduce two small helpers to make things easy for TTM based drivers.
Signed-off-by Lucas Stach <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- drivers/gpu/drm/ttm/ttm_tt.c | 25 +++++++++++++++++++++++++ include/drm/ttm/ttm_bo_driver.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)drivers/gpu/drm/ttm/ttm_tt.c +0
-0[...]
Quoted 7 lines
drivers/gpu/drm/ttm/ttm_tt.c void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma) · line 255
Quoted 4 lines
drivers/gpu/drm/ttm/ttm_tt.c +4
-0+void ttm_dma_tt_cache_sync_for_device (struct ttm_dma_tt *ttm_dma, +struct device *dev) +{ +int i;This should probably be unsigned long to match the type of
ttm_dma->ttm.num_pages.
Fixed.
Thanks,
Alex.
Re: [PATCH 3/4] drm/nouveau: hook up cache sync functions
In reply to Thierry RedingQuoted 46 lines · Thierry Reding
Quoted 5 lines · Alexandre Courbot
From: Lucas Stach <[email protected]>
Signed-off-by Lucas Stach <[email protected]>[[email protected]: make conditional and platform-friendly]Signed-off-by Alexandre Courbot <[email protected]>Perhaps having a propery commit message here would be good.
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) · line 503
Quoted 12 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +12
-0+#ifdef NOUVEAU_NEED_CACHE_SYNC +void +nouveau_bo_sync_for_cpu (struct nouveau_bo *nvbo) +{ +struct nouveau_device *device; +struct ttm_tt *ttm = nvbo->bo.ttm; + + device =nouveau_dev (nouveau_bdev (ttm->bdev)->dev); + +if (nvbo->bo.ttm && nvbo->bo.ttm->caching_state == tt_cached) +ttm_dma_tt_cache_sync_for_cpu ((struct ttm_dma_tt *)nvbo->bo.ttm, +nv_device_base (device));Can we be certain at this point that the struct
ttm_ttis in fact a structttm_dma_tt?Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.h +0
-0[...]
drivers/gpu/drm/nouveau/nouveau_bo.h int nouveau_bo_vma_add(struct nouveau_bo *, struct nouveau_vm *, · line 94
Quoted 3 lines
drivers/gpu/drm/nouveau/nouveau_bo.h +3
-0+#if IS_ENABLED (CONFIG_ARCH_TEGRA ) +#define NOUVEAU_NEED_CACHE_SYNC +#endif I know I gave this as an example myself when we discussed this offline, but I'm now thinking that this might actually be better off in
Kconfig.Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.h +10
-0+#ifdef NOUVEAU_NEED_CACHE_SYNC +void nouveau_bo_sync_for_cpu (struct nouveau_bo *); +void nouveau_bo_sync_for_device (struct nouveau_bo *); +#else +static inline void +nouveau_bo_sync_for_cpu (struct nouveau_bo *) +{ +} + +static inline void
Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_bo.h int nouveau_bo_vma_add(struct nouveau_bo *, struct nouveau_vm *, · line 111
Quoted 6 lines
drivers/gpu/drm/nouveau/nouveau_bo.h +6
-0+nouveau_bo_sync_for_device(struct nouveau_bo *) +{ +} +#endif + +There's a gratuituous blank line here.
Fixed.
Quoted 21 lines
Quoted 8 lines
drivers/gpu/drm/nouveau/nouveau_gem.c +0
-0@@ -897,7 +897,13 @@ nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, ret =ttm_bo_wait (&nvbo->bo, true, true, no_wait);spin_unlock (&nvbo->bo.bdev->fence_lock);drivers/gpu/drm/nouveau/nouveau_gem.c nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, · line 907
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_gem.c +7
-1drm_gem_object_unreference_unlocked (gem); -return ret; + +if (ret) +return ret; + +nouveau_bo_sync_for_cpu (nvbo); + +return 0 ; }This could be rewritten as:
Quoted 6 lines
if (!ret) nouveau_bo_sync_for_cpu(nvbo); return ret;Which would be slightly shorter.
I prefer to have a clear, easy to read code flow here by keeping error-handling within conditions (and not the other way round). This kind of optimization is very well done by the compiler.
Quoted 7 lines
On second thought, perhaps part of
nouveau_gem_ioctl_cpu_prep()could be refactored into a separate function to make this more symmetric. If we put that innouveau_bo.cand name itnouveau_bo_wait()for example, the dummies can go away and both nouveau_bo_sync_for_{cpu,device}() can be made static. I also think that's cleaner because it has both variants of the nouveau_bo_sync_for_*() calls in the same file.
Yep, agreed. I will give it a try in the next version of the series.
Thanks,
Alex.
Re: [PATCH 3/4] drm/nouveau: hook up cache sync functions
In reply to Lucas StachQuoted 35 lines · Lucas Stach
Am Montag, den 19.05.2014, 16:10 +0900 schrieb Alexandre Courbot:
Quoted 25 lines
From: Lucas Stach <[email protected]>
Signed-off-by Lucas Stach <[email protected]>[[email protected]: make conditional and platform-friendly]Signed-off-by Alexandre Courbot <[email protected]>--- drivers/gpu/drm/nouveau/nouveau_bo.c | 32 ++++++++++++++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_bo.h | 20 ++++++++++++++++++++ drivers/gpu/drm/nouveau/nouveau_gem.c | 8 +++++++- 3 files changed, 59 insertions(+), 1 deletion(-)drivers/gpu/drm/nouveau/nouveau_bo.c +2
-0@@ -407,6 +407,8 @@ nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible, {int ret; +nouveau_bo_sync_for_device (nvbo); + ret =ttm_bo_validate (&nvbo->bo, &nvbo->placement, interruptible, no_wait_gpu);if (ret)drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) · line 492
Quoted 4 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +1
-0@@ -487,6 +489,36 @@ nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)return 0 ; } +#ifdef NOUVEAU_NEED_CACHE_SYNC I don't like this ifdef at all. I know calling this functions will add a little overhead to x86 where it isn't strictly required, but I think it's negligible.
Quoted 4 lines
When I looked at them the dma_sync_single_for_[device|cpu] functions which are called here map out to just a drain of the PCI store buffer on x86, which should be fast enough to be done unconditionally. They won't so any time-consuming cache synchronization on PCI coherent arches.
If Ben agrees with it I am also perfectly fine with getting rid of this #ifdef.
Re: [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
In reply to Thierry RedingQuoted 57 lines · Thierry Reding
Quoted 6 lines · Lucas Stach
Am Montag, den 19.05.2014, 11:02 +0200 schrieb Thierry Reding:
Quoted 5 lines · Alexandre Courbot
Quoted 3 lines
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
Quoted 6 lines
Quoted 3 lines
It seems to me that the above is generally true for all architectures, not just ARM.
No, on PCI coherent arches, like x86 and some PowerPCs, the GPU will snoop the CPU caches and therefore an explicit cache flush is not required.
I was criticizing the wording in the commit message. Perhaps it could be enhanced with what you just said.
Quoted 32 lines
Quoted 16 lines
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
Quoted 14 lines
index
0886f47e5244..b9c9729c5733100644drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } }Quoted 16 lines
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) · line 469
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1u32@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + }This looks rather like a sledgehammer to me. Effectively this turns nvbo into an uncached buffer. With additional overhead of constantly flushing caches. Wouldn't it make more sense to locate the places where these are called and flush the cache after all the writes have completed?
Quoted 7 lines
I don't think the explicit flushing for those things makes sense. I think it is a lot more effective to just map the BOs write-combined on PCI non-coherent arches. This way any writes will be buffered. Reads will be slow, but I don't think nouveau is reading back a lot from those buffers.
Using the write-combining buffer doesn't need any additional synchronization as it will get flushed on pushbuf kickoff anyways.
Quoted 1 line
Sounds good to me.
I will need to wrap my head around TTM some more to understand how to do this the right way, but it is true that brute-forcing in-memory BO mappings to be WC make the addition of nv_cpu_cache_flush_area() unneeded. Is that the direction we want to take with this?
Re: [PATCH 2/4] drm/ttm: introduce dma cache sync helpers
In reply to Alexandre CourbotQuoted 9 lines · Alexandre Courbot
Quoted 7 lines · Thierry Reding
Quoted 3 lines · Alexandre Courbot
From: Lucas Stach <[email protected]>
On arches with non-coherent PCI,
I guess since this applies to gk20a
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
we need to flush caches ourselfes at
"ourselves". Or perhaps even reword to something like: "..., caches need to be flushed and invalidated explicitly", since
dma_sync_for_cpu()does invalidate rather than flush.Rephrased as "On arches for which access to GPU memory is non-coherent, caches need to be flushed and invalidated explicitly at the appropriate places."
Nit: s/arches/architectures/
Thierry
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
In reply to Lucas StachQuoted 80 lines · Lucas Stach
Am Montag, den 19.05.2014, 11:02 +0200 schrieb Thierry Reding:
Quoted 8 lines · Alexandre Courbot
Quoted 3 lines
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
It seems to me that the above is generally true for all architectures, not just ARM.
No, on PCI coherent arches, like x86 and some PowerPCs, the GPU will snoop the CPU caches and therefore an explicit cache flush is not required.
Quoted 51 lines
Also: s/explicitely/explicitly/
Quoted 5 lines
This patch introduces a macro that flushes the caches on ARM and translates to a no-op on other architectures, and uses it when writing to in-memory BOs. It will also be useful for implementations of instmem that access shared memory directly instead of going through PRAMIN.
Presumably instmem can access shared memory on all architectures, so this doesn't seem like a property of the architecture but rather of the memory pool backing the instmem.
In that case I wonder if this shouldn't be moved into an operation that is implemented by the backing memory pool and be a noop where the cache doesn't need explicit flushing.
Quoted 6 lines
drivers/gpu/drm/nouveau/core/os.h +0
-0@@ -38,4 +38,21 @@#endif /* def __BIG_ENDIAN else */ drivers/gpu/drm/nouveau/core/os.h line 48
Quoted 10 lines
drivers/gpu/drm/nouveau/core/os.h +8
-0#endif /* !ioread32_native */ +#if defined(__arm__) + +#define nv_cpu_cache_flush_area (va, size) \ +do { \ + phys_addr_t pa = virt_to_phys(va); \ + __cpuc_flush_dcache_area(va, size); \ + outer_flush_range(pa, pa + size); \ +} while (0)Couldn't this be a static inline function?
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
Quoted 14 lines
index
0886f47e5244..b9c9729c5733100644drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } }Quoted 16 lines
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) · line 469
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1u32@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + }This looks rather like a sledgehammer to me. Effectively this turns nvbo into an uncached buffer. With additional overhead of constantly flushing caches. Wouldn't it make more sense to locate the places where these are called and flush the cache after all the writes have completed?
Quoted 7 lines
I don't think the explicit flushing for those things makes sense. I think it is a lot more effective to just map the BOs write-combined on PCI non-coherent arches. This way any writes will be buffered. Reads will be slow, but I don't think nouveau is reading back a lot from those buffers.
Using the write-combining buffer doesn't need any additional synchronization as it will get flushed on pushbuf kickoff anyways.
I tried to go that way, and something interesting happened.
What I did: remove this patch and instead set the following caching parameters for the TTM_PL_TT case in nouveau_bo_init_mem_type():
man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
man->default_caching = TTM_PL_FLAG_WC;What happened: no runtime errors as what happened when caching is enabled. However, many of the vertex and texture buffers seem to be partially corrupted. In glmark2 the 3d models had many vertices (but not all) at the wrong position. Note that not all the scenes ended up being corrupted - in particular, when two consecutive scenes used the same model, the second instance would be uncorrupted.
Forcing the caching to TTM_PL_FLAG_UNCACHED led to the same result. What is interesting is that while data like vertices and textures got corrupted, pushbuffers and shader programs seem to be just fine, as I could not see any runtime error.
I don't really understand what kind of caching behavior could lead to that. If anyone has any idea, I'd love to hear.
Thanks,
Alex.
Re: [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
Quoted 104 lines · Alexandre Courbot
Quoted 80 lines · Lucas Stach
Am Montag, den 19.05.2014, 11:02 +0200 schrieb Thierry Reding:
Quoted 8 lines · Alexandre Courbot
Quoted 3 lines
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
It seems to me that the above is generally true for all architectures, not just ARM.
No, on PCI coherent arches, like x86 and some PowerPCs, the GPU will snoop the CPU caches and therefore an explicit cache flush is not required.
Quoted 51 lines
Also: s/explicitely/explicitly/
Quoted 5 lines
This patch introduces a macro that flushes the caches on ARM and translates to a no-op on other architectures, and uses it when writing to in-memory BOs. It will also be useful for implementations of instmem that access shared memory directly instead of going through PRAMIN.
Presumably instmem can access shared memory on all architectures, so this doesn't seem like a property of the architecture but rather of the memory pool backing the instmem.
In that case I wonder if this shouldn't be moved into an operation that is implemented by the backing memory pool and be a noop where the cache doesn't need explicit flushing.
Quoted 6 lines
drivers/gpu/drm/nouveau/core/os.h +0
-0@@ -38,4 +38,21 @@#endif /* def __BIG_ENDIAN else */ drivers/gpu/drm/nouveau/core/os.h line 48
Quoted 10 lines
drivers/gpu/drm/nouveau/core/os.h +8
-0#endif /* !ioread32_native */ +#if defined(__arm__) + +#define nv_cpu_cache_flush_area (va, size) \ +do { \ + phys_addr_t pa = virt_to_phys(va); \ + __cpuc_flush_dcache_area(va, size); \ + outer_flush_range(pa, pa + size); \ +} while (0)Couldn't this be a static inline function?
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
Quoted 14 lines
index
0886f47e5244..b9c9729c5733100644drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } }Quoted 16 lines
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) · line 469
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1u32@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + }This looks rather like a sledgehammer to me. Effectively this turns nvbo into an uncached buffer. With additional overhead of constantly flushing caches. Wouldn't it make more sense to locate the places where these are called and flush the cache after all the writes have completed?
Quoted 7 lines
I don't think the explicit flushing for those things makes sense. I think it is a lot more effective to just map the BOs write-combined on PCI non-coherent arches. This way any writes will be buffered. Reads will be slow, but I don't think nouveau is reading back a lot from those buffers.
Using the write-combining buffer doesn't need any additional synchronization as it will get flushed on pushbuf kickoff anyways.I tried to go that way, and something interesting happened.
What I did: remove this patch and instead set the following caching parameters for the
TTM_PL_TTcase innouveau_bo_init_mem_type():man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC; man->default_caching = TTM_PL_FLAG_WC;What happened: no runtime errors as what happened when caching is enabled. However, many of the vertex and texture buffers seem to be partially corrupted. In glmark2 the 3d models had many vertices (but not all) at the wrong position. Note that not all the scenes ended up being corrupted - in particular, when two consecutive scenes used the same model, the second instance would be uncorrupted.
Quoted 4 lines
Forcing the caching to
TTM_PL_FLAG_UNCACHEDled to the same result. What is interesting is that while data like vertices and textures got corrupted, pushbuffers and shader programs seem to be just fine, as I could not see any runtime error.
An interesting fact: if I change ttm_bo_kmap_ttm() such as kernel mappings of BOs are always performed write-combined, and leave the TTM_PL_TT default caching to TTM_PL_FLAG_CACHED so user-space mappings remain cached, the corruptions just vanish. It seems to be the fact of setting user-space mappings to anything non-cached that leads to this puzzling behavior. Certainly some subtlety of ARM mappings are getting over my head here.
If we need to implement different policies for kernel and user-space mappings, this might complicate things a bit, especially since support needs to be in TTM and not only Nouveau. I will submit a RFC tomorrow if I don't hear better ideas by then.
Alex.
Re: [PATCH 4/4] drm/nouveau: introduce CPU cache flushing macro
In reply to a message not in the archivePerhaps a bit late, but here's some random pointers I recall from my previous experience: on ARM, caching policy of different mappings of the same physical memory object must always correspond perfectly. If the userspace mapping is uncached, so should the kernel mapping be. If the memory is allocated using get_(zeroed_)page(), by default the caching policy for the pointer returned is WRITE_ALLOC on SMP systems. One useful tool for debugging cache problems is the pagewalk api's (mm/pagewalk.c), which will help you find the raw page table entry (and thus it's caching properties) for a virtual memory address.
Roy
op 12-06-14 15:50, Alexandre Courbot schreef:
Quoted 123 lines · Alexandre Courbot
Quoted 99 lines · Lucas Stach
Quoted 76 lines
Am Montag, den 19.05.2014, 11:02 +0200 schrieb Thierry Reding:
Quoted 7 lines · Alexandre Courbot
Quoted 3 lines
Some architectures (e.g. ARM) need the CPU buffers to be explicitely flushed for a memory write to take effect. Not doing so results in synchronization issues, especially after writing to BOs.
It seems to me that the above is generally true for all architectures, not just ARM.
No, on PCI coherent arches, like x86 and some PowerPCs, the GPU will snoop the CPU caches and therefore an explicit cache flush is not required.
Quoted 49 lines
Also: s/explicitely/explicitly/
Quoted 5 lines
This patch introduces a macro that flushes the caches on ARM and translates to a no-op on other architectures, and uses it when writing to in-memory BOs. It will also be useful for implementations of instmem that access shared memory directly instead of going through PRAMIN.
Presumably instmem can access shared memory on all architectures, so this doesn't seem like a property of the architecture but rather of the memory pool backing the instmem.
In that case I wonder if this shouldn't be moved into an operation that is implemented by the backing memory pool and be a noop where the cache doesn't need explicit flushing.
Quoted 6 lines
drivers/gpu/drm/nouveau/core/os.h +0
-0@@ -38,4 +38,21 @@#endif /* def __BIG_ENDIAN else */ drivers/gpu/drm/nouveau/core/os.h line 48
Quoted 10 lines
drivers/gpu/drm/nouveau/core/os.h +8
-0#endif /* !ioread32_native */ +#if defined(__arm__) + +#define nv_cpu_cache_flush_area (va, size) \ +do { \ + phys_addr_t pa = virt_to_phys(va); \ + __cpuc_flush_dcache_area(va, size); \ + outer_flush_range(pa, pa + size); \ +} while (0)Couldn't this be a static inline function?
Quoted 1 line
drivers/gpu/drm/nouveau/nouveau_bo.c +0
-0[...]
Quoted 14 lines
index
0886f47e5244..b9c9729c5733100644drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1@@ -437,8 +437,10 @@ nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) mem = &mem[index];if (is_iomem)iowrite16_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,2 ); + } }Quoted 15 lines
drivers/gpu/drm/nouveau/nouveau_bo.c nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) · line 469
Quoted 10 lines
drivers/gpu/drm/nouveau/nouveau_bo.c +3
-1u32@@ -461,8 +463,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) mem = &mem[index];if (is_iomem)iowrite32_native (val, (void __force __iomem *)mem); -else +else { *mem = val; +nv_cpu_cache_flush_area (mem,4 ); + }This looks rather like a sledgehammer to me. Effectively this turns nvbo into an uncached buffer. With additional overhead of constantly flushing caches. Wouldn't it make more sense to locate the places where these are called and flush the cache after all the writes have completed?
Quoted 7 lines
I don't think the explicit flushing for those things makes sense. I think it is a lot more effective to just map the BOs write-combined on PCI non-coherent arches. This way any writes will be buffered. Reads will be slow, but I don't think nouveau is reading back a lot from those buffers.
Using the write-combining buffer doesn't need any additional synchronization as it will get flushed on pushbuf kickoff anyways.I tried to go that way, and something interesting happened.
What I did: remove this patch and instead set the following caching parameters for the
TTM_PL_TTcase innouveau_bo_init_mem_type():man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC; man->default_caching = TTM_PL_FLAG_WC;What happened: no runtime errors as what happened when caching is enabled. However, many of the vertex and texture buffers seem to be partially corrupted. In glmark2 the 3d models had many vertices (but not all) at the wrong position. Note that not all the scenes ended up being corrupted - in particular, when two consecutive scenes used the same model, the second instance would be uncorrupted.
Quoted 4 lines
Forcing the caching to
TTM_PL_FLAG_UNCACHEDled to the same result. What is interesting is that while data like vertices and textures got corrupted, pushbuffers and shader programs seem to be just fine, as I could not see any runtime error.An interesting fact: if I change
ttm_bo_kmap_ttm()such as kernel mappings of BOs are always performed write-combined, and leave theTTM_PL_TTdefault caching toTTM_PL_FLAG_CACHEDso user-space mappings remain cached, the corruptions just vanish. It seems to be the fact of setting user-space mappings to anything non-cached that leads to this puzzling behavior. Certainly some subtlety of ARM mappings are getting over my head here.If we need to implement different policies for kernel and user-space mappings, this might complicate things a bit, especially since support needs to be in TTM and not only Nouveau. I will submit a RFC tomorrow if I don't hear better ideas by then.
Alex.
_______________________________________________
Nouveau mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/nouveau