Skip to contentSkip to conversation

Clean up write-combining MTRR addition

PATCH Superseded Andy Lutomirski 5 participants 16 messages May 3, 2013 – May 7, 2013

Subsystems (11)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Clean up write-combining MTRR addition

A fair number of drivers (mostly graphics) add write-combining MTRRs. Most ignore errors and most add the MTRR even on PAT systems which don't need to use MTRRs.

This series adds new functions mtrr_{add,del}_wc_if_needed and drm_mtrr_{add,del}_wc that report errors and do nothing if PAT is enabled.

I've only tested the radeon driver, since I don't have test hardware easily available for the other drivers.

Benefits include:
- Simpler code
- No more complaints about MTRR conflict warnings on PAT systems
- Eventual unexporting of the MTRR API?

This series eliminates about half of the mtrr_add calls in drivers/.

The series is also at:
https://git.kernel.org/cgit/linux/kernel/git/luto/linux.git/log/?h=mtrr_cleanup

Andy Lutomirski (7):
x86: Add mtrr_{add,del}_wc_if_needed
drm (ast,cirrus,mgag200,nouveau,savage,vmwgfx): Rework

    drm_mtrr_{add,del}

drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs
drm: Use drm_mtrr_add_wc for the AGP aperture
i915: Use drm_mtrr_{add,del}_wc
radeon: Switch to drm_mtrr_add_wc and add a missing drm_mtrr_del_wc
uvesafb: Clean up MTRR code

 Documentation/fb/uvesafb.txt           | 16 +++-----
 arch/x86/include/asm/mtrr.h            | 21 ++++++++++
 arch/x86/kernel/cpu/mtrr/main.c        | 53 +++++++++++++++++++++++++
 drivers/gpu/drm/ast/ast_ttm.c          | 14 ++-----
 drivers/gpu/drm/cirrus/cirrus_ttm.c    | 15 ++------
 drivers/gpu/drm/drm_bufs.c             | 11 ++----
 drivers/gpu/drm/drm_pci.c              |  8 ++--
 drivers/gpu/drm/drm_stub.c             | 10 +----
 drivers/gpu/drm/drm_vm.c               | 13 ++++---
 drivers/gpu/drm/i915/i915_dma.c        | 43 +++------------------
 drivers/gpu/drm/mgag200/mgag200_ttm.c  | 14 ++-----
 drivers/gpu/drm/nouveau/nouveau_ttm.c  | 13 ++-----
 drivers/gpu/drm/radeon/radeon_object.c |  5 ++-
 drivers/gpu/drm/savage/savage_bci.c    | 42 ++++++++------------
 drivers/gpu/drm/savage/savage_drv.h    |  5 +--
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.c    | 10 ++---
 drivers/video/uvesafb.c                | 70 +++++++++-------------------------
 include/drm/drmP.h                     | 23 +++++------
 include/video/uvesafb.h                |  1 +
 19 files changed, 169 insertions(+), 218 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/7 x86: Add mtrr_{add,del}_wc_if_needed

These MTRR helpers add a WC MTRR if PAT is disabled. Modern drivers should be using ioremap_wc, etc. to get WC memory; the MTRR is just a fallback if the system doesn't have PAT. So, rather than allocating an unnecessary MTRR even on PAT systems and having error handling spread out and handled differently by different drivers, consolidate it all and don't waste the MTRR on PAT-supporting systems. (Follow-up changes will update drivers.)

This should be a simple change, a bit of a clean-up, and it will flush out any drivers that actually depend on the MTRR for write combining.

---
 arch/x86/include/asm/mtrr.h     | 21 ++++++++++++++++
 arch/x86/kernel/cpu/mtrr/main.c | 53 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
arch/x86/include/asm/mtrr.h +21 -0
index e235582..cc96c72 100644
@@ -45,6 +45,18 @@ extern void mtrr_aps_init(void);
 extern void mtrr_bp_restore(void);
 extern int mtrr_trim_uncached_memory(unsigned long end_pfn);
 extern int amd_special_default_mtrr(void);
+
+/*
+ * These are for drivers (mostly video) that want to add WC MTRRs as a
+ * fallback if PAT is unavailable.  There is no need to check for errors.
+ *
+ * The handles used by the _if_needed functions are *not* MTRR indices.
+ * mtrr_del_wc_if_needed(0) and mtrr_del_wc_if_needed(-1) are
+ * guaranteed to have no effect.
+ */
+extern int __must_check mtrr_add_wc_if_needed(unsigned long base,
+					      unsigned long size);
+extern void mtrr_del_wc_if_needed(int handle);
 #  else
 static inline u8 mtrr_type_lookup(u64 addr, u64 end)
 {
@@ -86,6 +98,15 @@ static inline void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi)
 #define set_mtrr_aps_delayed_init() do {} while (0)
 #define mtrr_aps_init() do {} while (0)
 #define mtrr_bp_restore() do {} while (0)
+
+static inline int __must_check mtrr_add_wc_if_needed(unsigned long base,
+						     unsigned long size)
+{
+	return -ENODEV;
+}
+static inline void mtrr_del_wc_if_needed(int handle)
+{
+}
 #  endif
 
 #ifdef CONFIG_COMPAT
arch/x86/kernel/cpu/mtrr/main.c +53 -0

65 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 2/7 drm (ast,cirrus,mgag200,nouveau,savage,vmwgfx): Rework drm_mtrr_{add,del}

In reply to Andy Lutomirski

This replaces drm_mtrr_{add,del} with drm_mtrr_{add,del}_wc. The interface is simplified (because the base and size parameters to drm_mtrr_del never did anything) and it uses mtrr_{add,del}_wc_if_needed to avoid allocating MTRRs on systems that don't need them.

---
 drivers/gpu/drm/ast/ast_ttm.c         | 14 ++++--------
 drivers/gpu/drm/cirrus/cirrus_ttm.c   | 15 ++++---------
 drivers/gpu/drm/mgag200/mgag200_ttm.c | 14 ++++--------
 drivers/gpu/drm/nouveau/nouveau_ttm.c | 13 ++++-------
 drivers/gpu/drm/savage/savage_bci.c   | 42 +++++++++++++----------------------
 drivers/gpu/drm/savage/savage_drv.h   |  5 +----
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.c   | 10 ++++-----
 include/drm/drmP.h                    | 23 ++++++++-----------
 8 files changed, 45 insertions(+), 91 deletions(-)
drivers/gpu/drm/ast/ast_ttm.c +4 -10
index 3602731..5286c96 100644
@@ -271,26 +271,20 @@ int ast_mm_init(struct ast_private *ast)
 		return ret;
 	}
 
-	ast->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
-				    pci_resource_len(dev->pdev, 0),
-				    DRM_MTRR_WC);
+	ast->fb_mtrr = drm_mtrr_add_wc(pci_resource_start(dev->pdev, 0),
+				       pci_resource_len(dev->pdev, 0));
 
 	return 0;
 }
 
 void ast_mm_fini(struct ast_private *ast)
 {
-	struct drm_device *dev = ast->dev;
 	ttm_bo_device_release(&ast->ttm.bdev);
 
 	ast_ttm_global_release(ast);
 
-	if (ast->fb_mtrr >= 0) {
-		drm_mtrr_del(ast->fb_mtrr,
-			     pci_resource_start(dev->pdev, 0),
-			     pci_resource_len(dev->pdev, 0), DRM_MTRR_WC);
-		ast->fb_mtrr = -1;
-	}
+	drm_mtrr_del_wc(ast->fb_mtrr);
+	ast->fb_mtrr = -1;
 }
 
 void ast_ttm_placement(struct ast_bo *bo, int domain)
drivers/gpu/drm/cirrus/cirrus_ttm.c +4 -11
index 1413a26..95e87ee 100644
@@ -271,9 +271,8 @@ int cirrus_mm_init(struct cirrus_device *cirrus)
 		return ret;
 	}
 
-	cirrus->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
-				    pci_resource_len(dev->pdev, 0),
-				    DRM_MTRR_WC);
+	cirrus->fb_mtrr = drm_mtrr_add_wc(pci_resource_start(dev->pdev, 0),
+					  pci_resource_len(dev->pdev, 0));
 
 	cirrus->mm_inited = true;
 	return 0;
@@ -281,8 +280,6 @@ int cirrus_mm_init(struct cirrus_device *cirrus)
 
 void cirrus_mm_fini(struct cirrus_device *cirrus)
 {
-	struct drm_device *dev = cirrus->dev;
-
 	if (!cirrus->mm_inited)
 		return;
 
@@ -290,12 +287,8 @@ void cirrus_mm_fini(struct cirrus_device *cirrus)
 
 	cirrus_ttm_global_release(cirrus);
 
-	if (cirrus->fb_mtrr >= 0) {
-		drm_mtrr_del(cirrus->fb_mtrr,
-			     pci_resource_start(dev->pdev, 0),
-			     pci_resource_len(dev->pdev, 0), DRM_MTRR_WC);
-		cirrus->fb_mtrr = -1;
-	}
+	drm_mtrr_del_wc(cirrus->fb_mtrr);
+	cirrus->fb_mtrr = -1;
 }
 
 void cirrus_ttm_placement(struct cirrus_bo *bo, int domain)
drivers/gpu/drm/mgag200/mgag200_ttm.c +4 -10
index 8fc9d92..62245b4 100644
@@ -270,26 +270,20 @@ int mgag200_mm_init(struct mga_device *mdev)
 		return ret;
 	}
 
-	mdev->fb_mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 0),
-				    pci_resource_len(dev->pdev, 0),
-				    DRM_MTRR_WC);
+	mdev->fb_mtrr = drm_mtrr_add_wc(pci_resource_start(dev->pdev, 0),
+					pci_resource_len(dev->pdev, 0));
 
 	return 0;
 }
 
 void mgag200_mm_fini(struct mga_device *mdev)
 {
-	struct drm_device *dev = mdev->dev;
 	ttm_bo_device_release(&mdev->ttm.bdev);
 
 	mgag200_ttm_global_release(mdev);
 
-	if (mdev->fb_mtrr >= 0) {
-		drm_mtrr_del(mdev->fb_mtrr,
-			     pci_resource_start(dev->pdev, 0),
-			     pci_resource_len(dev->pdev, 0), DRM_MTRR_WC);
-		mdev->fb_mtrr = -1;
-	}
+	drm_mtrr_del_wc(mdev->fb_mtrr);
+	mdev->fb_mtrr = -1;
 }
 
 void mgag200_ttm_placement(struct mgag200_bo *bo, int domain)
drivers/gpu/drm/nouveau/nouveau_ttm.c +4 -9
index 9be9cb5..1506d78 100644
@@ -377,9 +377,8 @@ nouveau_ttm_init(struct nouveau_drm *drm)
 		return ret;
 	}
 
-	drm->ttm.mtrr = drm_mtrr_add(pci_resource_start(dev->pdev, 1),
-				     pci_resource_len(dev->pdev, 1),
-				     DRM_MTRR_WC);
+	drm->ttm.mtrr = drm_mtrr_add_wc(pci_resource_start(dev->pdev, 1),
+					pci_resource_len(dev->pdev, 1));
 
 	/* GART init */
 	if (drm->agp.stat != ENABLED) {
@@ -414,10 +413,6 @@ nouveau_ttm_fini(struct nouveau_drm *drm)
 
 	nouveau_ttm_global_release(drm);
 
-	if (drm->ttm.mtrr >= 0) {
-		drm_mtrr_del(drm->ttm.mtrr,
-			     pci_resource_start(drm->dev->pdev, 1),
-			     pci_resource_len(drm->dev->pdev, 1), DRM_MTRR_WC);
-		drm->ttm.mtrr = -1;
-	}
+	drm_mtrr_del_wc(drm->ttm.mtrr);
+	drm->ttm.mtrr = -1;
 }
drivers/gpu/drm/savage/savage_bci.c +15 -27

66 lines not on this page · raw message

drivers/gpu/drm/savage/savage_drv.h +1 -4
index df2aac6..c05082a 100644
@@ -160,10 +160,7 @@ typedef struct drm_savage_private {
 	drm_local_map_t *cmd_dma;
 	drm_local_map_t fake_dma;
 
-	struct {
-		int handle;
-		unsigned long base, size;
-	} mtrr[3];
+	int mtrr_handles[3];
 
 	/* BCI and status-related stuff */
 	volatile uint32_t *status_ptr, *bci_ptr;
drivers/gpu/drm/vmwgfx/vmwgfx_drv.c +4 -6
index 07dfd82..c104ae4 100644
@@ -565,8 +565,8 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
 		dev_priv->has_gmr = false;
 	}
 
-	dev_priv->mmio_mtrr = drm_mtrr_add(dev_priv->mmio_start,
-					   dev_priv->mmio_size, DRM_MTRR_WC);
+	dev_priv->mmio_mtrr = drm_mtrr_add_wc(dev_priv->mmio_start,
+					      dev_priv->mmio_size);
 
 	dev_priv->mmio_virt = ioremap_wc(dev_priv->mmio_start,
 					 dev_priv->mmio_size);
@@ -664,8 +664,7 @@ out_no_device:
 out_err4:
 	iounmap(dev_priv->mmio_virt);
 out_err3:
-	drm_mtrr_del(dev_priv->mmio_mtrr, dev_priv->mmio_start,
-		     dev_priv->mmio_size, DRM_MTRR_WC);
+	drm_mtrr_del_wc(dev_priv->mmio_mtrr);
 	if (dev_priv->has_gmr)
 		(void) ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_GMR);
 	(void)ttm_bo_clean_mm(&dev_priv->bdev, TTM_PL_VRAM);
@@ -709,8 +708,7 @@ static int vmw_driver_unload(struct drm_device *dev)
 
 	ttm_object_device_release(&dev_priv->tdev);
 	iounmap(dev_priv->mmio_virt);
-	drm_mtrr_del(dev_priv->mmio_mtrr, dev_priv->mmio_start,
-		     dev_priv->mmio_size, DRM_MTRR_WC);
+	drm_mtrr_del_wc(dev_priv->mmio_mtrr);
 	if (dev_priv->has_gmr)
 		(void)ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_GMR);
 	(void)ttm_bo_clean_mm(&dev_priv->bdev, TTM_PL_VRAM);
include/drm/drmP.h +9 -14
index 2d94d74..2a3e1fd 100644
@@ -1250,18 +1250,15 @@ static inline int drm_core_has_MTRR(struct drm_device *dev)
 	return drm_core_check_feature(dev, DRIVER_USE_MTRR);
 }
 
-#define DRM_MTRR_WC		MTRR_TYPE_WRCOMB
-
-static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
-			       unsigned int flags)
+static inline int __must_check drm_mtrr_add_wc(unsigned long offset,
+					       unsigned long size)
 {
-	return mtrr_add(offset, size, flags, 1);
+	return mtrr_add_wc_if_needed(offset, size);
 }
 
-static inline int drm_mtrr_del(int handle, unsigned long offset,
-			       unsigned long size, unsigned int flags)
+static inline void drm_mtrr_del_wc(int handle)
 {
-	return mtrr_del(handle, offset, size);
+	mtrr_del_wc_if_needed(handle);
 }
 
 #else
@@ -1269,16 +1266,14 @@ static inline int drm_mtrr_del(int handle, unsigned long offset,
 
 #define DRM_MTRR_WC		0
 
-static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
-			       unsigned int flags)
+static inline int __must_check drm_mtrr_add_wc(unsigned long offset,
+					       unsigned long size)
 {
-	return 0;
+	return -1;
 }
 
-static inline int drm_mtrr_del(int handle, unsigned long offset,
-			       unsigned long size, unsigned int flags)
+static inline void drm_mtrr_del_wc(int handle)
 {
-	return 0;
 }
 #endif
 
Signature · 1 line
1.8.1.4
#3 lore raw

Re: [PATCH 2/7] drm (ast, cirrus, mgag200, nouveau, savage, vmwgfx): Rework drm_mtrr_{add, del}

Quoted 8 lines · Andy Lutomirski

This replaces drm_mtrr_{add,del} with drm_mtrr_{add,del}_wc. The interface is simplified (because the base and size parameters to drm_mtrr_del never did anything) and it uses mtrr_{add,del}_wc_if_needed to avoid allocating MTRRs on systems that don't need them.

---

[snip]

Quoted 37 lines
include/drm/drmP.h +7 -10
index 2d94d74..2a3e1fd 100644
@@ -1250,18 +1250,15 @@ static inline int drm_core_has_MTRR(struct drm_device *dev)
 	return drm_core_check_feature(dev, DRIVER_USE_MTRR);
 }
 
-#define DRM_MTRR_WC		MTRR_TYPE_WRCOMB
-
-static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
-			       unsigned int flags)
+static inline int __must_check drm_mtrr_add_wc(unsigned long offset,
+					       unsigned long size)
 {
-	return mtrr_add(offset, size, flags, 1);
+	return mtrr_add_wc_if_needed(offset, size);
 }
 
-static inline int drm_mtrr_del(int handle, unsigned long offset,
-			       unsigned long size, unsigned int flags)
+static inline void drm_mtrr_del_wc(int handle)
 {
-	return mtrr_del(handle, offset, size);
+	mtrr_del_wc_if_needed(handle);
 }
 
 #else
@@ -1269,16 +1266,14 @@ static inline int drm_mtrr_del(int handle, unsigned long offset,
 
 #define DRM_MTRR_WC		0
 
-static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
-			       unsigned int flags)
+static inline int __must_check drm_mtrr_add_wc(unsigned long offset,
+					       unsigned long size)
 {

include/drm/drmP.h static inline int drm_mtrr_del(int handle, unsigned long offset, · line 1277

Quoted 10 lines
include/drm/drmP.h +2 -4
-	return 0;
+	return -1;
 }
 
-static inline int drm_mtrr_del(int handle, unsigned long offset,
-			       unsigned long size, unsigned int flags)
+static inline void drm_mtrr_del_wc(int handle)
 {
-	return 0;
 }

Tbh I'm not a big fan of the drm_ indirection. Historically that was useful as an OS abstraction layer so that the same drivers could be used unchanged on Linux and the *BSD. But those days are long gone and drm drivers are now proper Linux drivers, and generally OS HALs seem to be frowned upon.

Is there another reason than just being consistent with the historic stuff here? If we need dummy functions for !CONFIG_MTRR I think those should simply be in the core.

And if the inconsistency bugs you I'd volunteer myself to ditch the old drm_ mtrr helpers.
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#4 lore raw

Re: [PATCH 2/7] drm (ast, cirrus, mgag200, nouveau, savage, vmwgfx): Rework drm_mtrr_{add, del}

Quoted 17 lines · Daniel Vetter
Quoted 8 lines · Andy Lutomirski

This replaces drm_mtrr_{add,del} with drm_mtrr_{add,del}_wc. The interface is simplified (because the base and size parameters to drm_mtrr_del never did anything) and it uses mtrr_{add,del}_wc_if_needed to avoid allocating MTRRs on systems that don't need them.

---

Tbh I'm not a big fan of the drm_ indirection. Historically that was useful as an OS abstraction layer so that the same drivers could be used unchanged on Linux and the *BSD. But those days are long gone and drm drivers are now proper Linux drivers, and generally OS HALs seem to be frowned upon.

Quoted 3 lines

Is there another reason than just being consistent with the historic stuff here? If we need dummy functions for !CONFIG_MTRR I think those should simply be in the core.

I admit to a bit of cargo-culting. There was a layer of indirection, so I kept it. I'll just call mtrr_add/del_wc_if_needed directly in v2 (I added those functions in patch 1 of this series).

I'd assume you're okay with skipping mtrr addition if PAT is available since i915 already does it :) (Although the current logic is buggy -- cpu_has_pat is the wrong flag to check -- I think pat_enabled is better.)

Note to self: I should remove #include <asm/pat.h> in i915_dma.c in v2.

--Andy

#5 lore raw

PATCH 3/7 drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs

In reply to Andy Lutomirski

---

This needs careful review. I don't really know what this code does, nor do I have the hardware. (I don't understand AGP and the associated caching implications.)

 drivers/gpu/drm/drm_bufs.c | 11 ++++-------
 drivers/gpu/drm/drm_vm.c   | 13 +++++++------
 2 files changed, 11 insertions(+), 13 deletions(-)
drivers/gpu/drm/drm_bufs.c +4 -7
index 0128147..0ae9cbb 100644
@@ -210,8 +210,8 @@ static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
 		if (drm_core_has_MTRR(dev)) {
 			if (map->type == _DRM_FRAME_BUFFER ||
 			    (map->flags & _DRM_WRITE_COMBINING)) {
-				map->mtrr = mtrr_add(map->offset, map->size,
-						     MTRR_TYPE_WRCOMB, 1);
+				map->mtrr =
+					drm_mtrr_add_wc(map->offset, map->size);
 			}
 		}
 		if (map->type == _DRM_REGISTERS) {
@@ -451,11 +451,8 @@ int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
 		iounmap(map->handle);
 		/* FALLTHROUGH */
 	case _DRM_FRAME_BUFFER:
-		if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
-			int retcode;
-			retcode = mtrr_del(map->mtrr, map->offset, map->size);
-			DRM_DEBUG("mtrr_del=%d\n", retcode);
-		}
+		if (drm_core_has_MTRR(dev))
+			drm_mtrr_del_wc(map->mtrr);
 		break;
 	case _DRM_SHM:
 		vfree(map->handle);
drivers/gpu/drm/drm_vm.c +7 -6
index db7bd29..b255fd7 100644
@@ -43,15 +43,16 @@
 static void drm_vm_open(struct vm_area_struct *vma);
 static void drm_vm_close(struct vm_area_struct *vma);
 
-static pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
+static pgprot_t drm_io_prot(struct drm_local_map *map,
+			    struct vm_area_struct *vma)
 {
 	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
 
 #if defined(__i386__) || defined(__x86_64__)
-	if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
-		pgprot_val(tmp) |= _PAGE_PCD;
-		pgprot_val(tmp) &= ~_PAGE_PWT;
-	}
+	if (map->flags & _DRM_WRITE_COMBINING)
+		tmp = pgprot_writecombine(tmp);
+	else if (map->type != _DRM_AGP)
+		tmp = pgprot_noncached(tmp);
 #elif defined(__powerpc__)
 	pgprot_val(tmp) |= _PAGE_NO_CACHE;
 	if (map_type == _DRM_REGISTERS)
@@ -617,7 +618,7 @@ int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
 	case _DRM_REGISTERS:
 		offset = drm_core_get_reg_ofs(dev);
 		vma->vm_flags |= VM_IO;	/* not in core dump */
-		vma->vm_page_prot = drm_io_prot(map->type, vma);
+		vma->vm_page_prot = drm_io_prot(map, vma);
 		if (io_remap_pfn_range(vma, vma->vm_start,
 				       (map->offset + offset) >> PAGE_SHIFT,
 				       vma->vm_end - vma->vm_start,
Signature · 1 line
1.8.1.4
#6 lore raw

Re: [PATCH 3/7] drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs

Quoted 6 lines · Andy Lutomirski

---

This needs careful review. I don't really know what this code does, nor do I have the hardware. (I don't understand AGP and the associated caching implications.)

This patch is wrong (I didn't update the matching mtrr_del), and I'm reworking this whole series. But I may need some help on this one:
why is the mtrr handle of a map (whatever a map is) exported to userspace via the ADD_MAP and GET_MAP ioctls? What (if anything) is userspace supposed to do with it? Do I need to return a valid MTRR register number? Is there any userspace code at all that sets _DRM_WRITE_COMBINING in DRM_IOCTL_ADD_MAP with appropriate alignment and needs the MTRR, for which the drm driver doesn't already add the MTRR?

--Andy

#7 lore raw

Re: [PATCH 3/7] drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs

Quoted 18 lines · Andy Lutomirski
Quoted 6 lines · Andy Lutomirski

---

This needs careful review. I don't really know what this code does, nor do I have the hardware. (I don't understand AGP and the associated caching implications.)

This patch is wrong (I didn't update the matching mtrr_del), and I'm reworking this whole series. But I may need some help on this one:
why is the mtrr handle of a map (whatever a map is) exported to userspace via the ADD_MAP and GET_MAP ioctls? What (if anything) is userspace supposed to do with it? Do I need to return a valid MTRR register number? Is there any userspace code at all that sets _DRM_WRITE_COMBINING in DRM_IOCTL_ADD_MAP with appropriate alignment and needs the MTRR, for which the drm driver doesn't already add the MTRR?

Quoted 3 lines

--Andy

From memory, even on pat system we need mtrr for VRAM is PCI BAR. We

cover it with a write combine MTRR. The whole ioctl is use by some ddx or maybe even directly the XServer to do this mtrr mess in userspace.

Sorry for the bad news, but that's what i remember on that front thought i would need to read all the code again.

Cheers,
Jerome

#8 lore raw

Re: [PATCH 3/7] drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs

Quoted 19 lines · Jerome Glisse
Quoted 18 lines · Andy Lutomirski
Quoted 6 lines · Andy Lutomirski

---

This needs careful review. I don't really know what this code does, nor do I have the hardware. (I don't understand AGP and the associated caching implications.)

This patch is wrong (I didn't update the matching mtrr_del), and I'm reworking this whole series. But I may need some help on this one:
why is the mtrr handle of a map (whatever a map is) exported to userspace via the ADD_MAP and GET_MAP ioctls? What (if anything) is userspace supposed to do with it? Do I need to return a valid MTRR register number? Is there any userspace code at all that sets _DRM_WRITE_COMBINING in DRM_IOCTL_ADD_MAP with appropriate alignment and needs the MTRR, for which the drm driver doesn't already add the MTRR?

Quoted 5 lines
Quoted 1 line

--Andy

From memory, even on pat system we need mtrr for VRAM is PCI BAR. We cover it with a write combine MTRR. The whole ioctl is use by some ddx or maybe even directly the XServer to do this mtrr mess in userspace.

Egads! So we have a _DRM_WRITE_COMBINING flag, which will continue to work fine, but almost nothing uses it.

I'm amazed this stuff works in the current code, though. Apparently the memory type (WC or UC) of a drm mapping is determined by the mtrr the driver set, but if one part of the BAR is textures or the framebuffer and another part is an outgoing command ring, won't one of them end up with the wrong memory type?

I'd really hate to have to track fake mtrrs in the drm core to emulate real mtrrs.

Quoted 3 lines

Sorry for the bad news, but that's what i remember on that front thought i would need to read all the code again.

On the bright side, in the entire 2005 monolithic xorg tarball, the only thing that looks at the mtrr exported to userspace appears to be dritests.

--Andy

maintainer
#9 lore raw

Re: [PATCH 3/7] drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs

Quoted 5 lines
Quoted 4 lines

From memory, even on pat system we need mtrr for VRAM is PCI BAR. We cover it with a write combine MTRR. The whole ioctl is use by some ddx or maybe even directly the XServer to do this mtrr mess in userspace.

Quoted 8 lines

Egads! So we have a _DRM_WRITE_COMBINING flag, which will continue to work fine, but almost nothing uses it.

I'm amazed this stuff works in the current code, though. Apparently the memory type (WC or UC) of a drm mapping is determined by the mtrr the driver set, but if one part of the BAR is textures or the framebuffer and another part is an outgoing command ring, won't one of them end up with the wrong memory type?

Nobody sane puts the command ring in VRAM.

Dave.

#10 lore raw

Re: [PATCH 3/7] drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs

In reply to Andy Lutomirski
Quoted 26 lines · Andy Lutomirski
Quoted 19 lines · Jerome Glisse
Quoted 18 lines · Andy Lutomirski
Quoted 6 lines · Andy Lutomirski

---

This needs careful review. I don't really know what this code does, nor do I have the hardware. (I don't understand AGP and the associated caching implications.)

This patch is wrong (I didn't update the matching mtrr_del), and I'm reworking this whole series. But I may need some help on this one:
why is the mtrr handle of a map (whatever a map is) exported to userspace via the ADD_MAP and GET_MAP ioctls? What (if anything) is userspace supposed to do with it? Do I need to return a valid MTRR register number? Is there any userspace code at all that sets _DRM_WRITE_COMBINING in DRM_IOCTL_ADD_MAP with appropriate alignment and needs the MTRR, for which the drm driver doesn't already add the MTRR?

Quoted 5 lines
Quoted 1 line

--Andy

From memory, even on pat system we need mtrr for VRAM is PCI BAR. We cover it with a write combine MTRR. The whole ioctl is use by some ddx or maybe even directly the XServer to do this mtrr mess in userspace.

Quoted 8 lines

Egads! So we have a _DRM_WRITE_COMBINING flag, which will continue to work fine, but almost nothing uses it.

I'm amazed this stuff works in the current code, though. Apparently the memory type (WC or UC) of a drm mapping is determined by the mtrr the driver set, but if one part of the BAR is textures or the framebuffer and another part is an outgoing command ring, won't one of them end up with the wrong memory type?

A lot of old chips used to put the registers and framebuffer in the same BAR. IIRC, the xserver and later libpciaccess had workarounds to deal with this.

Alex

#11 lore raw

Re: [PATCH 3/7] drm: Update drm_addmap and drm_mmap to use PAT WC instead of MTRRs

Quoted 36 lines · Alex Deucher
Quoted 26 lines · Andy Lutomirski
Quoted 19 lines · Jerome Glisse
Quoted 18 lines · Andy Lutomirski
Quoted 6 lines · Andy Lutomirski

---

This needs careful review. I don't really know what this code does, nor do I have the hardware. (I don't understand AGP and the associated caching implications.)

This patch is wrong (I didn't update the matching mtrr_del), and I'm reworking this whole series. But I may need some help on this one:
why is the mtrr handle of a map (whatever a map is) exported to userspace via the ADD_MAP and GET_MAP ioctls? What (if anything) is userspace supposed to do with it? Do I need to return a valid MTRR register number? Is there any userspace code at all that sets _DRM_WRITE_COMBINING in DRM_IOCTL_ADD_MAP with appropriate alignment and needs the MTRR, for which the drm driver doesn't already add the MTRR?

Quoted 5 lines
Quoted 1 line

--Andy

From memory, even on pat system we need mtrr for VRAM is PCI BAR. We cover it with a write combine MTRR. The whole ioctl is use by some ddx or maybe even directly the XServer to do this mtrr mess in userspace.

Quoted 8 lines

Egads! So we have a _DRM_WRITE_COMBINING flag, which will continue to work fine, but almost nothing uses it.

I'm amazed this stuff works in the current code, though. Apparently the memory type (WC or UC) of a drm mapping is determined by the mtrr the driver set, but if one part of the BAR is textures or the framebuffer and another part is an outgoing command ring, won't one of them end up with the wrong memory type?

Quoted 3 lines

A lot of old chips used to put the registers and framebuffer in the same BAR. IIRC, the xserver and later libpciaccess had workarounds to deal with this.

I think I read the code wrong (so my patch is garbage). Maybe there's actually no problem -- if DRM_AGP and DRM_FRAME_BUFFER are always WC, DRM_REGISTERS is only WC if explicitly requested, and DRM_SHM is always WB, so everything should be covered.

Anything using libpciaccess ought to be unaffected by my changes -- I don't want to change /proc/mtrr or the sysfs stuff. The only possible issue is if there's a memtype conflict, but that's nothing new.

--Andy

#12 lore raw

PATCH 4/7 drm: Use drm_mtrr_add_wc for the AGP aperture

In reply to Andy Lutomirski
---
 drivers/gpu/drm/drm_pci.c  |  8 ++++----
 drivers/gpu/drm/drm_stub.c | 10 ++--------
 2 files changed, 6 insertions(+), 12 deletions(-)
drivers/gpu/drm/drm_pci.c +4 -4
index bd719e9..3628683 100644
@@ -278,10 +278,10 @@ int drm_pci_agp_init(struct drm_device *dev)
 		}
 		if (drm_core_has_MTRR(dev)) {
 			if (dev->agp)
-				dev->agp->agp_mtrr =
-					mtrr_add(dev->agp->agp_info.aper_base,
-						 dev->agp->agp_info.aper_size *
-						 1024 * 1024, MTRR_TYPE_WRCOMB, 1);
+				dev->agp->agp_mtrr = drm_mtrr_add_wc(
+					dev->agp->agp_info.aper_base,
+					dev->agp->agp_info.aper_size *
+					1024 * 1024);
 		}
 	}
 	return 0;
drivers/gpu/drm/drm_stub.c +2 -8
index 7d30802..96e21bd 100644
@@ -451,14 +451,8 @@ void drm_put_dev(struct drm_device *dev)
 
 	drm_lastclose(dev);
 
-	if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) &&
-	    dev->agp && dev->agp->agp_mtrr >= 0) {
-		int retval;
-		retval = mtrr_del(dev->agp->agp_mtrr,
-				  dev->agp->agp_info.aper_base,
-				  dev->agp->agp_info.aper_size * 1024 * 1024);
-		DRM_DEBUG("mtrr_del=%d\n", retval);
-	}
+	if (drm_core_has_MTRR(dev) && drm_core_has_AGP(dev) && dev->agp)
+		drm_mtrr_del_wc(dev->agp->agp_mtrr);
 
 	if (dev->driver->unload)
 		dev->driver->unload(dev);
Signature · 1 line
1.8.1.4
#13 lore raw

PATCH 5/7 i915: Use drm_mtrr_{add,del}_wc

In reply to Andy Lutomirski

i915 open-coded logic that was essentially equivalent to the new drm_mtrr_{add,del}_wc.

---
 drivers/gpu/drm/i915/i915_dma.c | 43 ++++++-----------------------------------
 1 file changed, 6 insertions(+), 37 deletions(-)
drivers/gpu/drm/i915/i915_dma.c +6 -37
index 4fa6beb..0fc95ba 100644
@@ -1393,29 +1393,6 @@ void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
 	master->driver_priv = NULL;
 }
 
-static void
-i915_mtrr_setup(struct drm_i915_private *dev_priv, unsigned long base,
-		unsigned long size)
-{
-	dev_priv->mm.gtt_mtrr = -1;
-
-#if defined(CONFIG_X86_PAT)
-	if (cpu_has_pat)
-		return;
-#endif
-
-	/* Set up a WC MTRR for non-PAT systems.  This is more common than
-	 * one would think, because the kernel disables PAT on first
-	 * generation Core chips because WC PAT gets overridden by a UC
-	 * MTRR if present.  Even if a UC MTRR isn't present.
-	 */
-	dev_priv->mm.gtt_mtrr = mtrr_add(base, size, MTRR_TYPE_WRCOMB, 1);
-	if (dev_priv->mm.gtt_mtrr < 0) {
-		DRM_INFO("MTRR allocation failed.  Graphics "
-			 "performance may suffer.\n");
-	}
-}
-
 static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
 {
 	struct apertures_struct *ap;
@@ -1552,8 +1529,8 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags)
 		goto out_rmmap;
 	}
 
-	i915_mtrr_setup(dev_priv, dev_priv->gtt.mappable_base,
-			aperture_size);
+	dev_priv->mm.gtt_mtrr = drm_mtrr_add_wc(dev_priv->gtt.mappable_base,
+						aperture_size);
 
 	/* The i915 workqueue is primarily used for batched retirement of
 	 * requests (and thus managing bo) once the task has been completed
@@ -1656,12 +1633,8 @@ out_gem_unload:
 	intel_teardown_mchbar(dev);
 	destroy_workqueue(dev_priv->wq);
 out_mtrrfree:
-	if (dev_priv->mm.gtt_mtrr >= 0) {
-		mtrr_del(dev_priv->mm.gtt_mtrr,
-			 dev_priv->gtt.mappable_base,
-			 aperture_size);
-		dev_priv->mm.gtt_mtrr = -1;
-	}
+	drm_mtrr_del_wc(dev_priv->mm.gtt_mtrr);
+	dev_priv->mm.gtt_mtrr = -1;
 	io_mapping_free(dev_priv->gtt.mappable);
 out_rmmap:
 	pci_iounmap(dev->pdev, dev_priv->regs);
@@ -1697,12 +1670,8 @@ int i915_driver_unload(struct drm_device *dev)
 	cancel_delayed_work_sync(&dev_priv->mm.retire_work);
 
 	io_mapping_free(dev_priv->gtt.mappable);
-	if (dev_priv->mm.gtt_mtrr >= 0) {
-		mtrr_del(dev_priv->mm.gtt_mtrr,
-			 dev_priv->gtt.mappable_base,
-			 dev_priv->gtt.mappable_end);
-		dev_priv->mm.gtt_mtrr = -1;
-	}
+	drm_mtrr_del_wc(dev_priv->mm.gtt_mtrr);
+	dev_priv->mm.gtt_mtrr = -1;
 
 	acpi_video_unregister();
 
Signature · 1 line
1.8.1.4
#14 lore raw

PATCH 6/7 radeon: Switch to drm_mtrr_add_wc and add a missing drm_mtrr_del_wc

In reply to Andy Lutomirski
---
 drivers/gpu/drm/radeon/radeon_object.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/radeon/radeon_object.c +3 -2
index d3aface..01d4906 100644
@@ -321,8 +321,8 @@ void radeon_bo_force_delete(struct radeon_device *rdev)
 int radeon_bo_init(struct radeon_device *rdev)
 {
 	/* Add an MTRR for the VRAM */
-	rdev->mc.vram_mtrr = mtrr_add(rdev->mc.aper_base, rdev->mc.aper_size,
-			MTRR_TYPE_WRCOMB, 1);
+	rdev->mc.vram_mtrr = drm_mtrr_add_wc(rdev->mc.aper_base,
+					     rdev->mc.aper_size);
 	DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
 		rdev->mc.mc_vram_size >> 20,
 		(unsigned long long)rdev->mc.aper_size >> 20);
@@ -334,6 +334,7 @@ int radeon_bo_init(struct radeon_device *rdev)
 void radeon_bo_fini(struct radeon_device *rdev)
 {
 	radeon_ttm_fini(rdev);
+	drm_mtrr_del_wc(rdev->mc.vram_mtrr);
 }
 
 void radeon_bo_list_add_object(struct radeon_bo_list *lobj,
Signature · 1 line
1.8.1.4
#15 lore raw

PATCH 7/7 uvesafb: Clean up MTRR code

In reply to Andy Lutomirski

The old code allowed very strange memory types. Now it works like all the other video drivers: ioremap_wc is used unconditionally, and MTRRs are set if PAT is unavailable (unless MTRR is disabled by a module parameter).

UC, WB, and WT support is gone. If there are MTRR conflicts that prevent addition of a WC MTRR, adding a non-conflicting MTRR is pointless; it's better to just turn off MTRR support entirely.

As an added bonus, any MTRR added is freed on unload.

---

I can't test this -- the link to v86d in Documentation/fb/uvesafb.txt seems to be broken, and Fedora doesn't seem to package it.

 Documentation/fb/uvesafb.txt | 16 ++++------
 drivers/video/uvesafb.c      | 70 ++++++++++++--------------------------------
 include/video/uvesafb.h      |  1 +
 3 files changed, 24 insertions(+), 63 deletions(-)
Documentation/fb/uvesafb.txt +5 -11
index eefdd91..f6362d8 100644
@@ -81,17 +81,11 @@ pmipal  Use the protected mode interface for palette changes.
 
 mtrr:n  Setup memory type range registers for the framebuffer
         where n:
-              0 - disabled (equivalent to nomtrr) (default)
-              1 - uncachable
-              2 - write-back
-              3 - write-combining
-              4 - write-through
-
-        If you see the following in dmesg, choose the type that matches
-        the old one.  In this example, use "mtrr:2".
-...
-mtrr: type mismatch for e0000000,8000000 old: write-back new: write-combining
-...
+              0 - disabled (equivalent to nomtrr)
+              3 - write-combining (default)
+
+	Values other than 0 and 3 will result in a warning and will be
+	treated just like 3.
 
 nomtrr  Do not use memory type range registers.
 
drivers/video/uvesafb.c +18 -52

106 lines not on this page · raw message

include/video/uvesafb.h +1 -0
index 1a91850..30f5362 100644
@@ -134,6 +134,7 @@ struct uvesafb_par {
 
 	int mode_idx;
 	struct vbe_crtc_ib crtc;
+	int mtrr_handle;
 };
 
 #endif /* _UVESAFB_H */
Signature · 1 line
1.8.1.4