1 1.9 riastrad /* $NetBSD: linux_writecomb.c,v 1.9 2021/12/19 10:47:13 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.1 riastrad #include <sys/cdefs.h> 33 1.9 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_writecomb.c,v 1.9 2021/12/19 10:47:13 riastradh Exp $"); 34 1.1 riastrad 35 1.4 jmcneill #if defined(__i386__) || defined(__x86_64__) 36 1.4 jmcneill #define HAS_MTRR 1 37 1.4 jmcneill #endif 38 1.4 jmcneill 39 1.4 jmcneill #if defined(_KERNEL_OPT) && defined(HAS_MTRR) 40 1.1 riastrad #include "opt_mtrr.h" 41 1.1 riastrad #endif 42 1.1 riastrad 43 1.1 riastrad #include <sys/kmem.h> 44 1.1 riastrad #include <sys/mutex.h> 45 1.1 riastrad 46 1.4 jmcneill #if defined(MTRR) 47 1.1 riastrad #include <machine/mtrr.h> 48 1.4 jmcneill #endif 49 1.1 riastrad 50 1.1 riastrad #include <linux/idr.h> 51 1.1 riastrad #include <linux/io.h> 52 1.1 riastrad 53 1.1 riastrad static struct { 54 1.1 riastrad kmutex_t lock; 55 1.1 riastrad struct idr idr; 56 1.1 riastrad } linux_writecomb __cacheline_aligned; 57 1.1 riastrad 58 1.1 riastrad int 59 1.1 riastrad linux_writecomb_init(void) 60 1.1 riastrad { 61 1.1 riastrad 62 1.3 mrg mutex_init(&linux_writecomb.lock, MUTEX_DEFAULT, IPL_VM); 63 1.1 riastrad idr_init(&linux_writecomb.idr); 64 1.1 riastrad 65 1.1 riastrad return 0; 66 1.1 riastrad } 67 1.1 riastrad 68 1.1 riastrad void 69 1.1 riastrad linux_writecomb_fini(void) 70 1.1 riastrad { 71 1.1 riastrad 72 1.1 riastrad KASSERT(idr_is_empty(&linux_writecomb.idr)); 73 1.1 riastrad idr_destroy(&linux_writecomb.idr); 74 1.1 riastrad mutex_destroy(&linux_writecomb.lock); 75 1.1 riastrad } 76 1.1 riastrad 77 1.1 riastrad int 78 1.1 riastrad arch_phys_wc_add(unsigned long base, unsigned long size) 79 1.1 riastrad { 80 1.4 jmcneill #if defined(MTRR) 81 1.1 riastrad struct mtrr *mtrr; 82 1.1 riastrad int n = 1; 83 1.1 riastrad int id; 84 1.1 riastrad int ret; 85 1.1 riastrad 86 1.1 riastrad mtrr = kmem_alloc(sizeof(*mtrr), KM_SLEEP); 87 1.1 riastrad mtrr->base = base; 88 1.1 riastrad mtrr->len = size; 89 1.1 riastrad mtrr->type = MTRR_TYPE_WC; 90 1.1 riastrad mtrr->flags = MTRR_VALID; 91 1.1 riastrad 92 1.1 riastrad /* XXX errno NetBSD->Linux */ 93 1.1 riastrad ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL); 94 1.1 riastrad if (ret) { 95 1.1 riastrad KASSERT(n == 0); 96 1.1 riastrad goto fail0; 97 1.1 riastrad } 98 1.1 riastrad KASSERT(n == 1); 99 1.1 riastrad 100 1.1 riastrad idr_preload(GFP_KERNEL); 101 1.1 riastrad mutex_spin_enter(&linux_writecomb.lock); 102 1.1 riastrad id = idr_alloc(&linux_writecomb.idr, mtrr, 0, 0, GFP_NOWAIT); 103 1.1 riastrad mutex_spin_exit(&linux_writecomb.lock); 104 1.1 riastrad idr_preload_end(); 105 1.1 riastrad if (id < 0) 106 1.1 riastrad goto fail1; 107 1.1 riastrad 108 1.1 riastrad return id; 109 1.1 riastrad 110 1.1 riastrad fail1: KASSERT(id < 0); 111 1.1 riastrad mtrr->type = 0; 112 1.1 riastrad mtrr->flags = 0; 113 1.1 riastrad /* XXX errno NetBSD->Linux */ 114 1.1 riastrad ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL); 115 1.8 riastrad KASSERTMSG(ret == 0, "mtrr_set failed to delete: %d", -ret); 116 1.8 riastrad KASSERTMSG(n == 1, "mtrr_set returned wrong number: %d", n); 117 1.1 riastrad ret = id; 118 1.1 riastrad fail0: KASSERT(ret < 0); 119 1.1 riastrad kmem_free(mtrr, sizeof(*mtrr)); 120 1.1 riastrad return ret; 121 1.4 jmcneill #else 122 1.4 jmcneill return -1; 123 1.4 jmcneill #endif 124 1.1 riastrad } 125 1.1 riastrad 126 1.1 riastrad void 127 1.1 riastrad arch_phys_wc_del(int id) 128 1.1 riastrad { 129 1.4 jmcneill #if defined(MTRR) 130 1.1 riastrad struct mtrr *mtrr; 131 1.6 riastrad int n = 1; 132 1.1 riastrad int ret __diagused; 133 1.1 riastrad 134 1.1 riastrad KASSERT(0 <= id); 135 1.1 riastrad 136 1.1 riastrad mutex_spin_enter(&linux_writecomb.lock); 137 1.1 riastrad mtrr = idr_find(&linux_writecomb.idr, id); 138 1.1 riastrad idr_remove(&linux_writecomb.idr, id); 139 1.5 riastrad mutex_spin_exit(&linux_writecomb.lock); 140 1.1 riastrad 141 1.1 riastrad if (mtrr != NULL) { 142 1.1 riastrad mtrr->type = 0; 143 1.1 riastrad mtrr->flags = 0; 144 1.1 riastrad /* XXX errno NetBSD->Linux */ 145 1.1 riastrad ret = -mtrr_set(mtrr, &n, NULL, MTRR_GETSET_KERNEL); 146 1.8 riastrad KASSERTMSG(ret == 0, "mtrr_set failed to delete: %d", -ret); 147 1.8 riastrad KASSERTMSG(n == 1, "mtrr_set returned wrong number: %d", n); 148 1.1 riastrad kmem_free(mtrr, sizeof(*mtrr)); 149 1.1 riastrad } 150 1.4 jmcneill #endif 151 1.1 riastrad } 152 1.1 riastrad 153 1.1 riastrad int 154 1.7 riastrad arch_phys_wc_index(int handle) 155 1.1 riastrad { 156 1.1 riastrad 157 1.1 riastrad /* XXX Actually implement this...requires changes to our MTRR API. */ 158 1.1 riastrad return handle; 159 1.1 riastrad } 160 1.9 riastrad 161 1.9 riastrad /* XXX Reserve PAT space on x86. */ 162 1.9 riastrad 163 1.9 riastrad int 164 1.9 riastrad arch_io_reserve_memtype_wc(resource_size_t start, resource_size_t size) 165 1.9 riastrad { 166 1.9 riastrad return 0; 167 1.9 riastrad } 168 1.9 riastrad 169 1.9 riastrad void 170 1.9 riastrad arch_io_free_memtype_wc(resource_size_t start, resource_size_t size) 171 1.9 riastrad { 172 1.9 riastrad } 173