Mon Dec 2 08:33:52 2019 UTC ()
Use LFENCE/SFENCE/MFENCE in x86 bus_space_barrier.

These are needed for BUS_SPACE_MAP_PREFETCHABLE mappings.  On x86,
these are WC-type memory regions, which means -- unlike normal
WB-type memory regions -- loads can be reordered with loads,
requiring LFENCE, and stores can be reordered with stores, requiring
SFENCE.

Reference: AMD64 Architecture Programmer's Manual, Volume 2: System
Programming, Sec. 7.4.1 `Memory Barrier Interaction with Memory
Types', Table 7-3 `Memory Access Ordering Rules'.


(riastradh)
diff -r1.41 -r1.42 src/sys/arch/x86/x86/bus_space.c

cvs diff -r1.41 -r1.42 src/sys/arch/x86/x86/bus_space.c (expand / switch to unified diff)

--- src/sys/arch/x86/x86/bus_space.c 2019/02/11 14:59:33 1.41
+++ src/sys/arch/x86/x86/bus_space.c 2019/12/02 08:33:52 1.42
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: bus_space.c,v 1.41 2019/02/11 14:59:33 cherry Exp $ */ 1/* $NetBSD: bus_space.c,v 1.42 2019/12/02 08:33:52 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc. 4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center. 9 * Simulation Facility, NASA Ames Research Center.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.41 2019/02/11 14:59:33 cherry Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.42 2019/12/02 08:33:52 riastradh Exp $");
35 35
36#include <sys/param.h> 36#include <sys/param.h>
37#include <sys/systm.h> 37#include <sys/systm.h>
38#include <sys/malloc.h> 38#include <sys/malloc.h>
39#include <sys/extent.h> 39#include <sys/extent.h>
40#include <sys/kmem.h> 40#include <sys/kmem.h>
41 41
42#include <uvm/uvm_extern.h> 42#include <uvm/uvm_extern.h>
43 43
44#include <dev/isa/isareg.h> 44#include <dev/isa/isareg.h>
45 45
46#include <sys/bus.h> 46#include <sys/bus.h>
47#include <machine/pio.h> 47#include <machine/pio.h>
@@ -868,27 +868,59 @@ bus_space_copy_region_4(bus_space_tag_t  @@ -868,27 +868,59 @@ bus_space_copy_region_4(bus_space_tag_t
868 for (addr1 += 4 * (c - 1), addr2 += 4 * (c - 1); 868 for (addr1 += 4 * (c - 1), addr2 += 4 * (c - 1);
869 c != 0; c--, addr1 -= 4, addr2 -= 4) 869 c != 0; c--, addr1 -= 4, addr2 -= 4)
870 *(volatile uint32_t *)(addr2) = 870 *(volatile uint32_t *)(addr2) =
871 *(volatile uint32_t *)(addr1); 871 *(volatile uint32_t *)(addr1);
872 } 872 }
873 } 873 }
874} 874}
875 875
876void 876void
877bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t bsh, 877bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t bsh,
878 bus_size_t offset, bus_size_t len, int flags) 878 bus_size_t offset, bus_size_t len, int flags)
879{ 879{
880 880
881 /* Function call is enough to prevent reordering of loads. */ 881 /*
 882 * For default mappings, which are mapped with UC-type memory
 883 * regions, all loads and stores are issued in program order.
 884 *
 885 * For BUS_SPACE_MAP_PREFETCHABLE mappings, which are mapped
 886 * with WC-type memory regions, loads and stores may be issued
 887 * out of order, potentially requiring any of the three x86
 888 * fences -- LFENCE, SFENCE, MFENCE.
 889 *
 890 * For BUS_SPACE_MAP_CACHEABLE mappings, which are mapped with
 891 * WB-type memory regions (like normal memory), store/load may
 892 * be reordered to load/store, potentially requiring MFENCE.
 893 *
 894 * We can't easily tell here how the region was mapped (without
 895 * consulting the page tables), so just issue the fence
 896 * unconditionally. Chances are either it's necessary or the
 897 * cost is small in comparison to device register I/O.
 898 */
 899 switch (flags) {
 900 case 0:
 901 break;
 902 case BUS_SPACE_BARRIER_READ:
 903 x86_lfence();
 904 break;
 905 case BUS_SPACE_BARRIER_WRITE:
 906 x86_sfence();
 907 break;
 908 case BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE:
 909 x86_mfence();
 910 break;
 911 default:
 912 panic("unknown bus space barrier: 0x%x", (unsigned)flags);
 913 }
882} 914}
883 915
884void * 916void *
885bus_space_vaddr(bus_space_tag_t tag, bus_space_handle_t bsh) 917bus_space_vaddr(bus_space_tag_t tag, bus_space_handle_t bsh)
886{ 918{
887 919
888 return x86_bus_space_is_mem(tag) ? (void *)bsh : NULL; 920 return x86_bus_space_is_mem(tag) ? (void *)bsh : NULL;
889} 921}
890 922
891static const void * 923static const void *
892bit_to_function_pointer(const struct bus_space_overrides *ov, uint64_t bit) 924bit_to_function_pointer(const struct bus_space_overrides *ov, uint64_t bit)
893{ 925{
894 switch (bit) { 926 switch (bit) {