1*c5a673c1Sskrll /* $NetBSD: obio_space.c,v 1.2 2023/04/21 14:58:34 skrll Exp $ */
26fe6c35aSmacallan
36fe6c35aSmacallan /*
46fe6c35aSmacallan * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
56fe6c35aSmacallan * All rights reserved.
66fe6c35aSmacallan *
76fe6c35aSmacallan * Written by Jason R. Thorpe for Wasabi Systems, Inc.
86fe6c35aSmacallan *
96fe6c35aSmacallan * Redistribution and use in source and binary forms, with or without
106fe6c35aSmacallan * modification, are permitted provided that the following conditions
116fe6c35aSmacallan * are met:
126fe6c35aSmacallan * 1. Redistributions of source code must retain the above copyright
136fe6c35aSmacallan * notice, this list of conditions and the following disclaimer.
146fe6c35aSmacallan * 2. Redistributions in binary form must reproduce the above copyright
156fe6c35aSmacallan * notice, this list of conditions and the following disclaimer in the
166fe6c35aSmacallan * documentation and/or other materials provided with the distribution.
176fe6c35aSmacallan * 3. All advertising materials mentioning features or use of this software
186fe6c35aSmacallan * must display the following acknowledgement:
196fe6c35aSmacallan * This product includes software developed for the NetBSD Project by
206fe6c35aSmacallan * Wasabi Systems, Inc.
216fe6c35aSmacallan * 4. The name of Wasabi Systems, Inc. may not be used to endorse
226fe6c35aSmacallan * or promote products derived from this software without specific prior
236fe6c35aSmacallan * written permission.
246fe6c35aSmacallan *
256fe6c35aSmacallan * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
266fe6c35aSmacallan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
276fe6c35aSmacallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
286fe6c35aSmacallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
296fe6c35aSmacallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
306fe6c35aSmacallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
316fe6c35aSmacallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
326fe6c35aSmacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
336fe6c35aSmacallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
346fe6c35aSmacallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
356fe6c35aSmacallan * POSSIBILITY OF SUCH DAMAGE.
366fe6c35aSmacallan */
376fe6c35aSmacallan
386fe6c35aSmacallan /*
396fe6c35aSmacallan * bus_space functions for Tungsten on-board devices
406fe6c35aSmacallan */
416fe6c35aSmacallan
426fe6c35aSmacallan #include <sys/cdefs.h>
43*c5a673c1Sskrll __KERNEL_RCSID(0, "$NetBSD: obio_space.c,v 1.2 2023/04/21 14:58:34 skrll Exp $");
446fe6c35aSmacallan
456fe6c35aSmacallan #include <sys/param.h>
466fe6c35aSmacallan #include <sys/systm.h>
476fe6c35aSmacallan
486fe6c35aSmacallan #include <uvm/uvm_extern.h>
496fe6c35aSmacallan
506fe6c35aSmacallan #include <sys/bus.h>
516fe6c35aSmacallan
526fe6c35aSmacallan /* Prototypes for all the bus_space structure functions */
536fe6c35aSmacallan bs_protos(obio);
546fe6c35aSmacallan bs_protos(generic);
556fe6c35aSmacallan bs_protos(generic_armv4);
566fe6c35aSmacallan bs_protos(bs_notimpl);
576fe6c35aSmacallan
586fe6c35aSmacallan /*
596fe6c35aSmacallan * The obio bus space tag. This is constant for all instances, so
606fe6c35aSmacallan * we never have to explicitly "create" it.
616fe6c35aSmacallan */
626fe6c35aSmacallan struct bus_space obio_bs_tag = {
636fe6c35aSmacallan /* cookie */
646fe6c35aSmacallan .bs_cookie = (void *) 0,
656fe6c35aSmacallan
666fe6c35aSmacallan /* mapping/unmapping */
676fe6c35aSmacallan .bs_map = obio_bs_map,
686fe6c35aSmacallan .bs_unmap = obio_bs_unmap,
696fe6c35aSmacallan .bs_subregion = obio_bs_subregion,
706fe6c35aSmacallan
716fe6c35aSmacallan /* allocation/deallocation */
726fe6c35aSmacallan .bs_alloc = obio_bs_alloc,
736fe6c35aSmacallan .bs_free = obio_bs_free,
746fe6c35aSmacallan
756fe6c35aSmacallan /* get kernel virtual address */
766fe6c35aSmacallan .bs_vaddr = obio_bs_vaddr,
776fe6c35aSmacallan
786fe6c35aSmacallan /* mmap */
796fe6c35aSmacallan .bs_mmap = bs_notimpl_bs_mmap,
806fe6c35aSmacallan
816fe6c35aSmacallan /* barrier */
826fe6c35aSmacallan .bs_barrier = obio_bs_barrier,
836fe6c35aSmacallan
846fe6c35aSmacallan /* read (single) */
856fe6c35aSmacallan .bs_r_1 = generic_bs_r_1,
866fe6c35aSmacallan .bs_r_2 = generic_armv4_bs_r_2,
876fe6c35aSmacallan .bs_r_4 = generic_bs_r_4,
886fe6c35aSmacallan .bs_r_8 = bs_notimpl_bs_r_8,
896fe6c35aSmacallan
906fe6c35aSmacallan /* read multiple */
916fe6c35aSmacallan .bs_rm_1 = generic_bs_rm_1,
926fe6c35aSmacallan .bs_rm_2 = bs_notimpl_bs_rm_2,
936fe6c35aSmacallan .bs_rm_4 = bs_notimpl_bs_rm_4,
946fe6c35aSmacallan .bs_rm_8 = bs_notimpl_bs_rm_8,
956fe6c35aSmacallan
966fe6c35aSmacallan /* read region */
976fe6c35aSmacallan .bs_rr_1 = generic_bs_rr_1,
986fe6c35aSmacallan .bs_rr_2 = bs_notimpl_bs_rr_2,
996fe6c35aSmacallan .bs_rr_4 = bs_notimpl_bs_rr_4,
1006fe6c35aSmacallan .bs_rr_8 = bs_notimpl_bs_rr_8,
1016fe6c35aSmacallan
1026fe6c35aSmacallan /* write (single) */
1036fe6c35aSmacallan .bs_w_1 = generic_bs_w_1,
1046fe6c35aSmacallan .bs_w_2 = generic_armv4_bs_w_2,
1056fe6c35aSmacallan .bs_w_4 = generic_bs_w_4,
1066fe6c35aSmacallan .bs_w_8 = bs_notimpl_bs_w_8,
1076fe6c35aSmacallan
1086fe6c35aSmacallan /* write multiple */
1096fe6c35aSmacallan .bs_wm_1 = generic_bs_wm_1,
1106fe6c35aSmacallan .bs_wm_2 = bs_notimpl_bs_wm_2,
1116fe6c35aSmacallan .bs_wm_4 = bs_notimpl_bs_wm_4,
1126fe6c35aSmacallan .bs_wm_8 = bs_notimpl_bs_wm_8,
1136fe6c35aSmacallan
1146fe6c35aSmacallan /* write region */
1156fe6c35aSmacallan .bs_wr_1 = bs_notimpl_bs_wr_1,
1166fe6c35aSmacallan .bs_wr_2 = bs_notimpl_bs_wr_2,
1176fe6c35aSmacallan .bs_wr_4 = bs_notimpl_bs_wr_4,
1186fe6c35aSmacallan .bs_wr_8 = bs_notimpl_bs_wr_8,
1196fe6c35aSmacallan
1206fe6c35aSmacallan /* set multiple */
1216fe6c35aSmacallan .bs_sm_1 = bs_notimpl_bs_sm_1,
1226fe6c35aSmacallan .bs_sm_2 = bs_notimpl_bs_sm_2,
1236fe6c35aSmacallan .bs_sm_4 = bs_notimpl_bs_sm_4,
1246fe6c35aSmacallan .bs_sm_8 = bs_notimpl_bs_sm_8,
1256fe6c35aSmacallan
1266fe6c35aSmacallan /* set region */
1276fe6c35aSmacallan .bs_sr_1 = bs_notimpl_bs_sr_1,
1286fe6c35aSmacallan .bs_sr_2 = bs_notimpl_bs_sr_2,
1296fe6c35aSmacallan .bs_sr_4 = bs_notimpl_bs_sr_4,
1306fe6c35aSmacallan .bs_sr_8 = bs_notimpl_bs_sr_8,
1316fe6c35aSmacallan
1326fe6c35aSmacallan /* copy */
1336fe6c35aSmacallan .bs_c_1 = bs_notimpl_bs_c_1,
1346fe6c35aSmacallan .bs_c_2 = bs_notimpl_bs_c_2,
1356fe6c35aSmacallan .bs_c_4 = bs_notimpl_bs_c_4,
1366fe6c35aSmacallan .bs_c_8 = bs_notimpl_bs_c_8,
1376fe6c35aSmacallan };
1386fe6c35aSmacallan
1396fe6c35aSmacallan int
obio_bs_map(void * t,bus_addr_t bpa,bus_size_t size,int flag,bus_space_handle_t * bshp)1406fe6c35aSmacallan obio_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
1416fe6c35aSmacallan bus_space_handle_t *bshp)
1426fe6c35aSmacallan {
1436fe6c35aSmacallan const struct pmap_devmap *pd;
1446fe6c35aSmacallan paddr_t startpa, endpa, pa, offset;
1456fe6c35aSmacallan vaddr_t va;
1466fe6c35aSmacallan
1476fe6c35aSmacallan if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
1486fe6c35aSmacallan /* Device was statically mapped. */
1496fe6c35aSmacallan *bshp = pd->pd_va + (bpa - pd->pd_pa);
1506fe6c35aSmacallan return (0);
1516fe6c35aSmacallan }
1526fe6c35aSmacallan
1536fe6c35aSmacallan endpa = round_page(bpa + size);
1546fe6c35aSmacallan offset = bpa & PAGE_MASK;
1556fe6c35aSmacallan startpa = trunc_page(bpa);
1566fe6c35aSmacallan
1576fe6c35aSmacallan va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
1586fe6c35aSmacallan UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
1596fe6c35aSmacallan if (va == 0)
1606fe6c35aSmacallan return (ENOMEM);
1616fe6c35aSmacallan
1626fe6c35aSmacallan *bshp = va + offset;
1636fe6c35aSmacallan
1646fe6c35aSmacallan const int pmapflags =
1656fe6c35aSmacallan (flag & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
1666fe6c35aSmacallan ? 0
1676fe6c35aSmacallan : PMAP_NOCACHE;
1686fe6c35aSmacallan
1696fe6c35aSmacallan for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
1706fe6c35aSmacallan pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
1716fe6c35aSmacallan }
1726fe6c35aSmacallan pmap_update(pmap_kernel());
1736fe6c35aSmacallan
1746fe6c35aSmacallan return (0);
1756fe6c35aSmacallan }
1766fe6c35aSmacallan
1776fe6c35aSmacallan int
obio_bs_alloc(void * t,bus_addr_t rstart,bus_addr_t rend,bus_size_t size,bus_size_t alignment,bus_size_t boundary,int flags,bus_addr_t * bpap,bus_space_handle_t * bshp)1786fe6c35aSmacallan obio_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend, bus_size_t size,
1796fe6c35aSmacallan bus_size_t alignment, bus_size_t boundary, int flags, bus_addr_t *bpap,
1806fe6c35aSmacallan bus_space_handle_t *bshp)
1816fe6c35aSmacallan {
1826fe6c35aSmacallan
1836fe6c35aSmacallan panic("obio_bs_alloc(): not implemented");
1846fe6c35aSmacallan }
1856fe6c35aSmacallan
1866fe6c35aSmacallan
1876fe6c35aSmacallan void
obio_bs_unmap(void * t,bus_space_handle_t bsh,bus_size_t size)1886fe6c35aSmacallan obio_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
1896fe6c35aSmacallan {
1906fe6c35aSmacallan vaddr_t va, endva;
1916fe6c35aSmacallan
1926fe6c35aSmacallan if (pmap_devmap_find_va(bsh, size) != NULL) {
1936fe6c35aSmacallan /* Device was statically mapped; nothing to do. */
1946fe6c35aSmacallan return;
1956fe6c35aSmacallan }
1966fe6c35aSmacallan
1976fe6c35aSmacallan endva = round_page(bsh + size);
1986fe6c35aSmacallan va = trunc_page(bsh);
1996fe6c35aSmacallan
2006fe6c35aSmacallan pmap_kremove(va, endva - va);
2016fe6c35aSmacallan uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
2026fe6c35aSmacallan }
2036fe6c35aSmacallan
2046fe6c35aSmacallan void
obio_bs_free(void * t,bus_space_handle_t bsh,bus_size_t size)2056fe6c35aSmacallan obio_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
2066fe6c35aSmacallan {
2076fe6c35aSmacallan
2086fe6c35aSmacallan panic("obio_bs_free(): not implemented");
2096fe6c35aSmacallan }
2106fe6c35aSmacallan
2116fe6c35aSmacallan int
obio_bs_subregion(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)2126fe6c35aSmacallan obio_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
2136fe6c35aSmacallan bus_size_t size, bus_space_handle_t *nbshp)
2146fe6c35aSmacallan {
2156fe6c35aSmacallan
2166fe6c35aSmacallan *nbshp = bsh + offset;
2176fe6c35aSmacallan return (0);
2186fe6c35aSmacallan }
2196fe6c35aSmacallan
2206fe6c35aSmacallan void *
obio_bs_vaddr(void * t,bus_space_handle_t bsh)2216fe6c35aSmacallan obio_bs_vaddr(void *t, bus_space_handle_t bsh)
2226fe6c35aSmacallan {
2236fe6c35aSmacallan
2246fe6c35aSmacallan return ((void *)bsh);
2256fe6c35aSmacallan }
2266fe6c35aSmacallan
2276fe6c35aSmacallan void
obio_bs_barrier(void * t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t len,int flags)2286fe6c35aSmacallan obio_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
2296fe6c35aSmacallan bus_size_t len, int flags)
2306fe6c35aSmacallan {
2316fe6c35aSmacallan
2326fe6c35aSmacallan /* Nothing to do. */
2336fe6c35aSmacallan }
234