Module Name:    src
Committed By:   rin
Date:           Fri Aug 10 04:44:16 UTC 2018

Modified Files:
        src/sys/arch/arm/broadcom: bcm283x_platform.c

Log Message:
* Convert bus address to physical address in xxx_bs_mmap(), as done in
  xxx_bs_map().

* Reuse bs_map and bs_mmap in arm_generic_bs_tag in order to
  - set pmap flags properly for aarch64
  - dedup codes

OK ryo


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/arm/broadcom/bcm283x_platform.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/arm/broadcom/bcm283x_platform.c
diff -u src/sys/arch/arm/broadcom/bcm283x_platform.c:1.11 src/sys/arch/arm/broadcom/bcm283x_platform.c:1.12
--- src/sys/arch/arm/broadcom/bcm283x_platform.c:1.11	Sun Aug  5 14:02:35 2018
+++ src/sys/arch/arm/broadcom/bcm283x_platform.c	Fri Aug 10 04:44:15 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: bcm283x_platform.c,v 1.11 2018/08/05 14:02:35 skrll Exp $	*/
+/*	$NetBSD: bcm283x_platform.c,v 1.12 2018/08/10 04:44:15 rin Exp $	*/
 
 /*-
  * Copyright (c) 2017 Jared D. McNeill <jmcne...@invisible.ca>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bcm283x_platform.c,v 1.11 2018/08/05 14:02:35 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bcm283x_platform.c,v 1.12 2018/08/10 04:44:15 rin Exp $");
 
 #include "opt_arm_debug.h"
 #include "opt_bcm283x.h"
@@ -134,125 +134,83 @@ struct bus_space bcm2835_a4x_bs_tag;
 struct bus_space bcm2836_bs_tag;
 struct bus_space bcm2836_a4x_bs_tag;
 
-int bcm283x_bs_map(void *, bus_addr_t, bus_size_t, int, bus_space_handle_t *);
-paddr_t bcm283x_bs_mmap(void *, bus_addr_t, off_t, int, int);
-paddr_t bcm283x_a4x_bs_mmap(void *, bus_addr_t, off_t, int, int);
+static paddr_t bcm2835_bus_to_phys(bus_addr_t);
+static paddr_t bcm2836_bus_to_phys(bus_addr_t);
 
-int
-bcm283x_bs_map(void *t, bus_addr_t ba, bus_size_t size, int flag,
-    bus_space_handle_t *bshp)
+static paddr_t
+bcm2835_bus_to_phys(bus_addr_t ba)
 {
-	u_long startpa, endpa, pa;
-	vaddr_t va;
-
-	/* Convert BA to PA */
-	pa = ba & ~BCM2835_BUSADDR_CACHE_MASK;
 
-	startpa = trunc_page(pa);
-	endpa = round_page(pa + size);
-
-	/* XXX use extent manager to check duplicate mapping */
-
-	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
-	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT | UVM_KMF_COLORMATCH);
-	if (!va)
-		return ENOMEM;
-
-	*bshp = (bus_space_handle_t)(va + (pa - startpa));
-
-	int pmapflags;
-	if (flag & BUS_SPACE_MAP_PREFETCHABLE)
-		pmapflags = PMAP_WRITE_COMBINE;
-	else if (flag & BUS_SPACE_MAP_CACHEABLE)
-		pmapflags = 0;
-	else
-		pmapflags = PMAP_NOCACHE;
-	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
-		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
-	}
-	pmap_update(pmap_kernel());
+	/* Attempt to find the PA device mapping */
+	if (ba >= BCM2835_PERIPHERALS_BASE_BUS &&
+	    ba < BCM2835_PERIPHERALS_BASE_BUS + BCM2835_PERIPHERALS_SIZE)
+		return BCM2835_PERIPHERALS_BUS_TO_PHYS(ba);
 
-	return 0;
+	return ba & ~BCM2835_BUSADDR_CACHE_MASK;
 }
 
-paddr_t
-bcm283x_bs_mmap(void *t, bus_addr_t bpa, off_t offset, int prot, int flags)
+static paddr_t
+bcm2836_bus_to_phys(bus_addr_t ba)
 {
-	/* Convert BA to PA */
-	const paddr_t pa = bpa & ~BCM2835_BUSADDR_CACHE_MASK;
-	paddr_t bus_flags = 0;
-
-	if (flags & BUS_SPACE_MAP_PREFETCHABLE)
-		bus_flags |= ARM_MMAP_WRITECOMBINE;
-
-	return arm_btop(pa + offset) | bus_flags;
-}
 
-paddr_t
-bcm283x_a4x_bs_mmap(void *t, bus_addr_t bpa, off_t offset, int prot, int flags)
-{
-	/* Convert BA to PA */
-	const paddr_t pa = bpa & ~BCM2835_BUSADDR_CACHE_MASK;
-	paddr_t bus_flags = 0;
+	/* Attempt to find the PA device mapping */
+	if (ba >= BCM2835_PERIPHERALS_BASE_BUS &&
+	    ba < BCM2835_PERIPHERALS_BASE_BUS + BCM2835_PERIPHERALS_SIZE)
+		return BCM2836_PERIPHERALS_BUS_TO_PHYS(ba);
 
-	if (flags & BUS_SPACE_MAP_PREFETCHABLE)
-		bus_flags |= ARM_MMAP_WRITECOMBINE;
+	if (ba >= BCM2836_ARM_LOCAL_BASE &&
+	    ba < BCM2836_ARM_LOCAL_BASE + BCM2836_ARM_LOCAL_SIZE)
+		return ba;
 
-	return arm_btop(pa + 4 * offset) | bus_flags;
+	return ba & ~BCM2835_BUSADDR_CACHE_MASK;
 }
 
 int
 bcm2835_bs_map(void *t, bus_addr_t ba, bus_size_t size, int flag,
     bus_space_handle_t *bshp)
 {
-	const struct pmap_devmap *pd;
-	bool match = false;
-	u_long pa;
+	const paddr_t pa = bcm2835_bus_to_phys(ba);
 
-	/* Attempt to find the PA device mapping */
-	if (ba >= BCM2835_PERIPHERALS_BASE_BUS &&
-	    ba < BCM2835_PERIPHERALS_BASE_BUS + BCM2835_PERIPHERALS_SIZE) {
-		match = true;
-		pa = BCM2835_PERIPHERALS_BUS_TO_PHYS(ba);
-	}
+	return bus_space_map(&arm_generic_bs_tag, pa, size, flag, bshp);
+}
 
-	if (match && (pd = pmap_devmap_find_pa(pa, size)) != NULL) {
-		/* Device was statically mapped. */
-		*bshp = pd->pd_va + (pa - pd->pd_pa);
-		return 0;
-	}
+paddr_t
+bcm2835_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
+{
+	const paddr_t pa = bcm2835_bus_to_phys(ba);
 
-	return bcm283x_bs_map(t, ba, size, flag, bshp);
+	return bus_space_mmap(&arm_generic_bs_tag, pa, offset, prot, flags);
+}
+
+paddr_t
+bcm2835_a4x_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
+{
+
+	return bcm2835_bs_mmap(t, ba, 4 * offset, prot, flags);
 }
 
 int
 bcm2836_bs_map(void *t, bus_addr_t ba, bus_size_t size, int flag,
     bus_space_handle_t *bshp)
 {
-	const struct pmap_devmap *pd;
-	bool match = false;
-	u_long pa;
+	const paddr_t pa = bcm2836_bus_to_phys(ba);
 
-	/* Attempt to find the PA device mapping */
-	if (ba >= BCM2835_PERIPHERALS_BASE_BUS &&
-	    ba < BCM2835_PERIPHERALS_BASE_BUS + BCM2835_PERIPHERALS_SIZE) {
-		match = true;
-		pa = BCM2836_PERIPHERALS_BUS_TO_PHYS(ba);
-	}
+	return bus_space_map(&arm_generic_bs_tag, pa, size, flag, bshp);
+}
 
-	if (ba >= BCM2836_ARM_LOCAL_BASE &&
-	    ba < BCM2836_ARM_LOCAL_BASE + BCM2836_ARM_LOCAL_SIZE) {
-		match = true;
-		pa = ba;
-	}
+paddr_t
+bcm2836_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
+{
+	const paddr_t pa = bcm2836_bus_to_phys(ba);
 
-	if (match && (pd = pmap_devmap_find_pa(pa, size)) != NULL) {
-		/* Device was statically mapped. */
-		*bshp = pd->pd_va + (pa - pd->pd_pa);
-		return 0;
-	}
+	return bus_space_mmap(&arm_generic_bs_tag, pa, offset, prot, flags);
+}
+
+paddr_t
+bcm2836_a4x_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
+{
 
-	return bcm283x_bs_map(t, ba, size, flag, bshp);
+	return bcm2836_bs_mmap(t, ba, 4 * offset, prot, flags);
 }
 
 struct arm32_dma_range bcm2835_dma_ranges[] = {
@@ -1223,9 +1181,9 @@ bcm2835_platform_bootstrap(void)
 	bcm2835_a4x_bs_tag = arm_generic_a4x_bs_tag;
 
 	bcm2835_bs_tag.bs_map = bcm2835_bs_map;
-	bcm2835_bs_tag.bs_mmap = bcm283x_bs_mmap;
+	bcm2835_bs_tag.bs_mmap = bcm2835_bs_mmap;
 	bcm2835_a4x_bs_tag.bs_map = bcm2835_bs_map;
-	bcm2835_a4x_bs_tag.bs_mmap = bcm283x_a4x_bs_mmap;
+	bcm2835_a4x_bs_tag.bs_mmap = bcm2835_a4x_bs_mmap;
 
 	fdtbus_set_decoderegprop(false);
 
@@ -1244,9 +1202,9 @@ bcm2836_platform_bootstrap(void)
 	bcm2836_a4x_bs_tag = arm_generic_a4x_bs_tag;
 
 	bcm2836_bs_tag.bs_map = bcm2836_bs_map;
-	bcm2836_bs_tag.bs_mmap = bcm283x_bs_mmap;
+	bcm2836_bs_tag.bs_mmap = bcm2836_bs_mmap;
 	bcm2836_a4x_bs_tag.bs_map = bcm2836_bs_map;
-	bcm2836_a4x_bs_tag.bs_mmap = bcm283x_a4x_bs_mmap;
+	bcm2836_a4x_bs_tag.bs_mmap = bcm2836_a4x_bs_mmap;
 
 	fdtbus_set_decoderegprop(false);
 

Reply via email to