Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 24 Aug 2019 00:36:00 +0000 (UTC)
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r351449 - in stable: 11/sys/amd64/amd64 11/sys/i386/i386 12/sys/amd64/amd64 12/sys/i386/i386
Message-ID:  <201908240036.x7O0a0Tn099182@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jhb
Date: Sat Aug 24 00:35:59 2019
New Revision: 351449
URL: https://svnweb.freebsd.org/changeset/base/351449

Log:
  MFC 350551:
  Don't reset memory attributes when mapping physical addresses for ACPI.
  
  Previously, AcpiOsMemory was using pmap_mapbios which would always map
  the requested address Write-Back (WB).  For several AMD Ryzen laptops,
  the BIOS uses AcpiOsMemory to directly access the PCI MCFG region in
  order to access PCI config registers.  This has the side effect of
  remapping the MCFG region in the direct map as WB instead of UC
  hanging the laptops during boot.
  
  On the one laptop I examined in detail, the _PIC global method used to
  switch from 8259A PICs to I/O APICs uses a pair of PCI config space
  registers at offset 0x84 in the device at 0:0:0 to as a pair of
  address/data registers to access an indirect register in the chipset
  and clear a single bit to switch modes.
  
  To fix, alter the semantics of pmap_mapbios() such that it does not
  modify the attributes of any existing mappings and instead uses the
  existing attributes.  If a new mapping is created, this new mapping
  uses WB (the default memory attribute).
  
  Special thanks to the gentleman whose name I don't have who brought
  two affected laptops to the hacker lounge at BSDCan.  Direct access to
  the affected systems permitted finding the root cause within an hour
  or so.
  
  PR:		231760, 236899

Modified:
  stable/11/sys/amd64/amd64/pmap.c
  stable/11/sys/i386/i386/pmap.c
Directory Properties:
  stable/11/   (props changed)

Changes in other areas also in this revision:
Modified:
  stable/12/sys/amd64/amd64/pmap.c
  stable/12/sys/i386/i386/pmap.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/11/sys/amd64/amd64/pmap.c
==============================================================================
--- stable/11/sys/amd64/amd64/pmap.c	Sat Aug 24 00:01:19 2019	(r351448)
+++ stable/11/sys/amd64/amd64/pmap.c	Sat Aug 24 00:35:59 2019	(r351449)
@@ -615,6 +615,13 @@ static caddr_t crashdumpmap;
 #define	PMAP_ENTER_NORECLAIM	0x1000000	/* Don't reclaim PV entries. */
 #define	PMAP_ENTER_NOREPLACE	0x2000000	/* Don't replace mappings. */
 
+/*
+ * Internal flags for pmap_mapdev_internal() and
+ * pmap_change_attr_locked().
+ */
+#define	MAPDEV_FLUSHCACHE	0x0000001	/* Flush cache after mapping. */
+#define	MAPDEV_SETATTR		0x0000002	/* Modify existing attrs. */
+
 static void	free_pv_chunk(struct pv_chunk *pc);
 static void	free_pv_entry(pmap_t pmap, pv_entry_t pv);
 static pv_entry_t get_pv_entry(pmap_t pmap, struct rwlock **lockp);
@@ -635,7 +642,7 @@ static pv_entry_t pmap_pvh_remove(struct md_page *pvh,
 		    vm_offset_t va);
 
 static int pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode,
-    bool noflush);
+    int flags);
 static boolean_t pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
 static boolean_t pmap_demote_pde_locked(pmap_t pmap, pd_entry_t *pde,
     vm_offset_t va, struct rwlock **lockp);
@@ -6836,7 +6843,7 @@ pmap_pde_attr(pd_entry_t *pde, int cache_bits, int mas
  * NOT real memory.
  */
 static void *
-pmap_mapdev_internal(vm_paddr_t pa, vm_size_t size, int mode, bool noflush)
+pmap_mapdev_internal(vm_paddr_t pa, vm_size_t size, int mode, int flags)
 {
 	struct pmap_preinit_mapping *ppim;
 	vm_offset_t va, offset;
@@ -6870,7 +6877,8 @@ pmap_mapdev_internal(vm_paddr_t pa, vm_size_t size, in
 		for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
 			ppim = pmap_preinit_mapping + i;
 			if (ppim->pa == pa && ppim->sz == size &&
-			    ppim->mode == mode)
+			    (ppim->mode == mode ||
+			    (flags & MAPDEV_SETATTR) == 0))
 				return ((void *)(ppim->va + offset));
 		}
 		/*
@@ -6879,9 +6887,12 @@ pmap_mapdev_internal(vm_paddr_t pa, vm_size_t size, in
 		 */
 		if (pa < dmaplimit && pa + size <= dmaplimit) {
 			va = PHYS_TO_DMAP(pa);
-			PMAP_LOCK(kernel_pmap);
-			i = pmap_change_attr_locked(va, size, mode, noflush);
-			PMAP_UNLOCK(kernel_pmap);
+			if ((flags & MAPDEV_SETATTR) != 0) {
+				PMAP_LOCK(kernel_pmap);
+				i = pmap_change_attr_locked(va, size, mode, flags);
+				PMAP_UNLOCK(kernel_pmap);
+			} else
+				i = 0;
 			if (!i)
 				return ((void *)(va + offset));
 		}
@@ -6892,7 +6903,7 @@ pmap_mapdev_internal(vm_paddr_t pa, vm_size_t size, in
 	for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
 		pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
 	pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
-	if (!noflush)
+	if ((flags & MAPDEV_FLUSHCACHE) != 0)
 		pmap_invalidate_cache_range(va, va + tmpsize, FALSE);
 	return ((void *)(va + offset));
 }
@@ -6901,28 +6912,31 @@ void *
 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
 {
 
-	return (pmap_mapdev_internal(pa, size, mode, false));
+	return (pmap_mapdev_internal(pa, size, mode, MAPDEV_FLUSHCACHE |
+	    MAPDEV_SETATTR));
 }
 
 void *
 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
 {
 
-	return (pmap_mapdev_internal(pa, size, PAT_UNCACHEABLE, false));
+	return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
 }
 
 void *
 pmap_mapdev_pciecfg(vm_paddr_t pa, vm_size_t size)
 {
 
-	return (pmap_mapdev_internal(pa, size, PAT_UNCACHEABLE, true));
+	return (pmap_mapdev_internal(pa, size, PAT_UNCACHEABLE,
+	    MAPDEV_SETATTR));
 }
 
 void *
 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
 {
 
-	return (pmap_mapdev_internal(pa, size, PAT_WRITE_BACK, false));
+	return (pmap_mapdev_internal(pa, size, PAT_WRITE_BACK,
+	    MAPDEV_FLUSHCACHE));
 }
 
 void
@@ -7061,13 +7075,13 @@ pmap_change_attr(vm_offset_t va, vm_size_t size, int m
 	int error;
 
 	PMAP_LOCK(kernel_pmap);
-	error = pmap_change_attr_locked(va, size, mode, false);
+	error = pmap_change_attr_locked(va, size, mode, MAPDEV_FLUSHCACHE);
 	PMAP_UNLOCK(kernel_pmap);
 	return (error);
 }
 
 static int
-pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode, bool noflush)
+pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode, int flags)
 {
 	vm_offset_t base, offset, tmpva;
 	vm_paddr_t pa_start, pa_end, pa_end1;
@@ -7184,7 +7198,7 @@ pmap_change_attr_locked(vm_offset_t va, vm_size_t size
 					/* Run ended, update direct map. */
 					error = pmap_change_attr_locked(
 					    PHYS_TO_DMAP(pa_start),
-					    pa_end - pa_start, mode, noflush);
+					    pa_end - pa_start, mode, flags);
 					if (error != 0)
 						break;
 					/* Start physical address run. */
@@ -7214,7 +7228,7 @@ pmap_change_attr_locked(vm_offset_t va, vm_size_t size
 					/* Run ended, update direct map. */
 					error = pmap_change_attr_locked(
 					    PHYS_TO_DMAP(pa_start),
-					    pa_end - pa_start, mode, noflush);
+					    pa_end - pa_start, mode, flags);
 					if (error != 0)
 						break;
 					/* Start physical address run. */
@@ -7242,7 +7256,7 @@ pmap_change_attr_locked(vm_offset_t va, vm_size_t size
 					/* Run ended, update direct map. */
 					error = pmap_change_attr_locked(
 					    PHYS_TO_DMAP(pa_start),
-					    pa_end - pa_start, mode, noflush);
+					    pa_end - pa_start, mode, flags);
 					if (error != 0)
 						break;
 					/* Start physical address run. */
@@ -7257,7 +7271,7 @@ pmap_change_attr_locked(vm_offset_t va, vm_size_t size
 		pa_end1 = MIN(pa_end, dmaplimit);
 		if (pa_start != pa_end1)
 			error = pmap_change_attr_locked(PHYS_TO_DMAP(pa_start),
-			    pa_end1 - pa_start, mode, noflush);
+			    pa_end1 - pa_start, mode, flags);
 	}
 
 	/*
@@ -7266,7 +7280,7 @@ pmap_change_attr_locked(vm_offset_t va, vm_size_t size
 	 */
 	if (changed) {
 		pmap_invalidate_range(kernel_pmap, base, tmpva);
-		if (!noflush)
+		if ((flags & MAPDEV_FLUSHCACHE) != 0)
 			pmap_invalidate_cache_range(base, tmpva, FALSE);
 	}
 	return (error);

Modified: stable/11/sys/i386/i386/pmap.c
==============================================================================
--- stable/11/sys/i386/i386/pmap.c	Sat Aug 24 00:01:19 2019	(r351448)
+++ stable/11/sys/i386/i386/pmap.c	Sat Aug 24 00:35:59 2019	(r351449)
@@ -286,6 +286,11 @@ static struct mtx PMAP2mutex;
 
 int pti;
 
+/*
+ * Internal flags for pmap_mapdev_internal().
+ */
+#define	MAPDEV_SETATTR		0x0000001	/* Modify existing attrs. */
+
 static void	free_pv_chunk(struct pv_chunk *pc);
 static void	free_pv_entry(pmap_t pmap, pv_entry_t pv);
 static pv_entry_t get_pv_entry(pmap_t pmap, boolean_t try);
@@ -5225,11 +5230,12 @@ pmap_pde_attr(pd_entry_t *pde, int cache_bits)
  * routine is intended to be used for mapping device memory,
  * NOT real memory.
  */
-void *
-pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
+static void *
+pmap_mapdev_internal(vm_paddr_t pa, vm_size_t size, int mode, int flags)
 {
 	struct pmap_preinit_mapping *ppim;
 	vm_offset_t va, offset;
+	vm_page_t m;
 	vm_size_t tmpsize;
 	int i;
 
@@ -5237,9 +5243,11 @@ pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mo
 	size = round_page(offset + size);
 	pa = pa & PG_FRAME;
 
-	if (pa < KERNLOAD && pa + size <= KERNLOAD)
+	if (pa < KERNLOAD && pa + size <= KERNLOAD) {
 		va = KERNBASE + pa;
-	else if (!pmap_initialized) {
+		if ((flags & MAPDEV_SETATTR) == 0)
+			return ((void *)(va + offset));
+	} else if (!pmap_initialized) {
 		va = 0;
 		for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
 			ppim = pmap_preinit_mapping + i;
@@ -5262,21 +5270,38 @@ pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mo
 		for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
 			ppim = pmap_preinit_mapping + i;
 			if (ppim->pa == pa && ppim->sz == size &&
-			    ppim->mode == mode)
+			    (ppim->mode == mode ||
+			    (flags & MAPDEV_SETATTR) == 0))
 				return ((void *)(ppim->va + offset));
 		}
 		va = kva_alloc(size);
 		if (va == 0)
 			panic("%s: Couldn't allocate KVA", __func__);
 	}
-	for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
+	for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE) {
+		if ((flags & MAPDEV_SETATTR) == 0 && pmap_initialized) {
+			m = PHYS_TO_VM_PAGE(pa);
+			if (m != NULL && VM_PAGE_TO_PHYS(m) == pa) {
+				pmap_kenter_attr(va + tmpsize, pa + tmpsize,
+				    m->md.pat_mode);
+				continue;
+			}
+		}
 		pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
+	}
 	pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
 	pmap_invalidate_cache_range(va, va + size, FALSE);
 	return ((void *)(va + offset));
 }
 
 void *
+pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
+{
+
+	return (pmap_mapdev_internal(pa, size, mode, MAPDEV_SETATTR));
+}
+
+void *
 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
 {
 
@@ -5287,7 +5312,7 @@ void *
 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
 {
 
-	return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
+	return (pmap_mapdev_internal(pa, size, PAT_WRITE_BACK, 0));
 }
 
 void



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201908240036.x7O0a0Tn099182>