1 /* $NetBSD: mbmem.c,v 1.22 2021/08/07 16:19:06 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass, Gordon W. Ross, and Matthew Fredette. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: mbmem.c,v 1.22 2021/08/07 16:19:06 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/device.h> 38 39 #include <uvm/uvm_extern.h> 40 41 #define _SUN68K_BUS_DMA_PRIVATE 42 #include <machine/autoconf.h> 43 #include <machine/pmap.h> 44 #include <machine/dvma.h> 45 46 #include <sun2/sun2/control.h> 47 #include <sun2/sun2/machdep.h> 48 #include <sun2/sun2/mbmem.h> 49 50 /* Does this machine have a Multibus? */ 51 extern int cpu_has_multibus; 52 53 static int mbmem_match(device_t, cfdata_t, void *); 54 static void mbmem_attach(device_t, device_t, void *); 55 56 struct mbmem_softc { 57 device_t sc_dev; /* base device */ 58 bus_space_tag_t sc_bustag; /* parent bus tag */ 59 bus_dma_tag_t sc_dmatag; /* parent bus dma tag */ 60 }; 61 62 CFATTACH_DECL_NEW(mbmem, sizeof(struct mbmem_softc), 63 mbmem_match, mbmem_attach, NULL, NULL); 64 65 static int mbmem_attached; 66 67 static paddr_t mbmem_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t, off_t, 68 int, int); 69 static int _mbmem_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t, 70 int, vaddr_t, bus_space_handle_t *); 71 static int mbmem_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t, 72 struct proc *, int); 73 static int mbmem_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t, 74 bus_dma_segment_t *, int, bus_size_t, int); 75 76 static struct sun68k_bus_space_tag mbmem_space_tag = { 77 NULL, /* cookie */ 78 NULL, /* parent bus tag */ 79 _mbmem_bus_map, /* bus_space_map */ 80 NULL, /* bus_space_unmap */ 81 NULL, /* bus_space_subregion */ 82 NULL, /* bus_space_barrier */ 83 mbmem_bus_mmap, /* bus_space_mmap */ 84 NULL, /* bus_intr_establish */ 85 NULL, /* bus_space_peek_N */ 86 NULL /* bus_space_poke_N */ 87 }; 88 89 static struct sun68k_bus_dma_tag mbmem_dma_tag; 90 91 static int 92 mbmem_match(device_t parent, cfdata_t cf, void *aux) 93 { 94 struct mainbus_attach_args *ma = aux; 95 96 if (mbmem_attached) 97 return 0; 98 99 return cpu_has_multibus && 100 (ma->ma_name == NULL || strcmp(cf->cf_name, ma->ma_name) == 0); 101 } 102 103 static void 104 mbmem_attach(device_t parent, device_t self, void *aux) 105 { 106 struct mainbus_attach_args *ma = aux; 107 struct mbmem_softc *sc = device_private(self); 108 struct mbmem_attach_args mbma; 109 const char *const *cpp; 110 static const char *const special[] = { 111 /* find these first */ 112 NULL 113 }; 114 115 mbmem_attached = 1; 116 117 sc->sc_dev = self; 118 aprint_normal("\n"); 119 120 sc->sc_bustag = ma->ma_bustag; 121 sc->sc_dmatag = ma->ma_dmatag; 122 123 mbmem_space_tag.cookie = sc; 124 mbmem_space_tag.parent = sc->sc_bustag; 125 126 mbmem_dma_tag = *sc->sc_dmatag; 127 mbmem_dma_tag._cookie = sc; 128 mbmem_dma_tag._dmamap_load = mbmem_dmamap_load; 129 mbmem_dma_tag._dmamap_load_raw = mbmem_dmamap_load_raw; 130 131 /* 132 * Prepare the skeleton attach arguments for our devices. 133 * The values we give in the locators are indications to 134 * sun68k_bus_search about which locators must and must not 135 * be defined. 136 */ 137 mbma = *ma; 138 mbma.mbma_bustag = &mbmem_space_tag; 139 mbma.mbma_dmatag = &mbmem_dma_tag; 140 mbma.mbma_paddr = LOCATOR_REQUIRED; 141 mbma.mbma_pri = LOCATOR_OPTIONAL; 142 143 /* Find all `early' mbmem devices */ 144 for (cpp = special; *cpp != NULL; cpp++) { 145 mbma.mbma_name = *cpp; 146 config_search(self, &mbma, 147 CFARGS(.search = sun68k_bus_search)); 148 } 149 150 /* Find all other mbmem devices */ 151 mbma.mbma_name = NULL; 152 config_search(self, &mbma, 153 CFARGS(.search = sun68k_bus_search)); 154 } 155 156 int 157 _mbmem_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, 158 bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp) 159 { 160 struct mbmem_softc *sc = t->cookie; 161 162 if ((paddr + size) > MBMEM_SIZE) 163 panic("%s: out of range", __func__); 164 165 return bus_space_map2(sc->sc_bustag, PMAP_MBMEM, paddr, 166 size, flags, vaddr, hp); 167 } 168 169 paddr_t 170 mbmem_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off, 171 int prot, int flags) 172 { 173 struct mbmem_softc *sc = t->cookie; 174 175 return bus_space_mmap2(sc->sc_bustag, PMAP_MBMEM, paddr, off, 176 prot, flags); 177 } 178 179 static int 180 mbmem_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 181 bus_size_t buflen, struct proc *p, int flags) 182 { 183 int error; 184 185 error = _bus_dmamap_load(t, map, buf, buflen, p, flags); 186 if (error == 0) 187 map->dm_segs[0].ds_addr &= DVMA_MBMEM_SLAVE_MASK; 188 return error; 189 } 190 191 static int 192 mbmem_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, 193 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags) 194 { 195 int error; 196 197 error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags); 198 if (error == 0) 199 map->dm_segs[0].ds_addr &= DVMA_MBMEM_SLAVE_MASK; 200 return error; 201 } 202