11.2Sskrll/*	$NetBSD: obio_space.c,v 1.2 2023/04/21 14:58:34 skrll Exp $	*/
21.1Smacallan
31.1Smacallan/*
41.1Smacallan * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
51.1Smacallan * All rights reserved.
61.1Smacallan *
71.1Smacallan * Written by Jason R. Thorpe for Wasabi Systems, Inc.
81.1Smacallan *
91.1Smacallan * Redistribution and use in source and binary forms, with or without
101.1Smacallan * modification, are permitted provided that the following conditions
111.1Smacallan * are met:
121.1Smacallan * 1. Redistributions of source code must retain the above copyright
131.1Smacallan *    notice, this list of conditions and the following disclaimer.
141.1Smacallan * 2. Redistributions in binary form must reproduce the above copyright
151.1Smacallan *    notice, this list of conditions and the following disclaimer in the
161.1Smacallan *    documentation and/or other materials provided with the distribution.
171.1Smacallan * 3. All advertising materials mentioning features or use of this software
181.1Smacallan *    must display the following acknowledgement:
191.1Smacallan *	This product includes software developed for the NetBSD Project by
201.1Smacallan *	Wasabi Systems, Inc.
211.1Smacallan * 4. The name of Wasabi Systems, Inc. may not be used to endorse
221.1Smacallan *    or promote products derived from this software without specific prior
231.1Smacallan *    written permission.
241.1Smacallan *
251.1Smacallan * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
261.1Smacallan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.1Smacallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.1Smacallan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
291.1Smacallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.1Smacallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.1Smacallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.1Smacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.1Smacallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.1Smacallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.1Smacallan * POSSIBILITY OF SUCH DAMAGE.
361.1Smacallan */
371.1Smacallan
381.1Smacallan/*
391.1Smacallan * bus_space functions for Tungsten on-board devices
401.1Smacallan */
411.1Smacallan
421.1Smacallan#include <sys/cdefs.h>
431.2Sskrll__KERNEL_RCSID(0, "$NetBSD: obio_space.c,v 1.2 2023/04/21 14:58:34 skrll Exp $");
441.1Smacallan
451.1Smacallan#include <sys/param.h>
461.1Smacallan#include <sys/systm.h>
471.1Smacallan
481.1Smacallan#include <uvm/uvm_extern.h>
491.1Smacallan
501.1Smacallan#include <sys/bus.h>
511.1Smacallan
521.1Smacallan/* Prototypes for all the bus_space structure functions */
531.1Smacallanbs_protos(obio);
541.1Smacallanbs_protos(generic);
551.1Smacallanbs_protos(generic_armv4);
561.1Smacallanbs_protos(bs_notimpl);
571.1Smacallan
581.1Smacallan/*
591.1Smacallan * The obio bus space tag.  This is constant for all instances, so
601.1Smacallan * we never have to explicitly "create" it.
611.1Smacallan */
621.1Smacallanstruct bus_space obio_bs_tag = {
631.1Smacallan	/* cookie */
641.1Smacallan	.bs_cookie = (void *) 0,
651.1Smacallan
661.1Smacallan	/* mapping/unmapping */
671.1Smacallan	.bs_map = obio_bs_map,
681.1Smacallan	.bs_unmap = obio_bs_unmap,
691.1Smacallan	.bs_subregion = obio_bs_subregion,
701.1Smacallan
711.1Smacallan	/* allocation/deallocation */
721.1Smacallan	.bs_alloc = obio_bs_alloc,
731.1Smacallan	.bs_free = obio_bs_free,
741.1Smacallan
751.1Smacallan	/* get kernel virtual address */
761.1Smacallan	.bs_vaddr = obio_bs_vaddr,
771.1Smacallan
781.1Smacallan	/* mmap */
791.1Smacallan	.bs_mmap = bs_notimpl_bs_mmap,
801.1Smacallan
811.1Smacallan	/* barrier */
821.1Smacallan	.bs_barrier = obio_bs_barrier,
831.1Smacallan
841.1Smacallan	/* read (single) */
851.1Smacallan	.bs_r_1 = generic_bs_r_1,
861.1Smacallan	.bs_r_2 = generic_armv4_bs_r_2,
871.1Smacallan	.bs_r_4 = generic_bs_r_4,
881.1Smacallan	.bs_r_8 = bs_notimpl_bs_r_8,
891.1Smacallan
901.1Smacallan	/* read multiple */
911.1Smacallan	.bs_rm_1 = generic_bs_rm_1,
921.1Smacallan	.bs_rm_2 = bs_notimpl_bs_rm_2,
931.1Smacallan	.bs_rm_4 = bs_notimpl_bs_rm_4,
941.1Smacallan	.bs_rm_8 = bs_notimpl_bs_rm_8,
951.1Smacallan
961.1Smacallan	/* read region */
971.1Smacallan	.bs_rr_1 = generic_bs_rr_1,
981.1Smacallan	.bs_rr_2 = bs_notimpl_bs_rr_2,
991.1Smacallan	.bs_rr_4 = bs_notimpl_bs_rr_4,
1001.1Smacallan	.bs_rr_8 = bs_notimpl_bs_rr_8,
1011.1Smacallan
1021.1Smacallan	/* write (single) */
1031.1Smacallan	.bs_w_1 = generic_bs_w_1,
1041.1Smacallan	.bs_w_2 = generic_armv4_bs_w_2,
1051.1Smacallan	.bs_w_4 = generic_bs_w_4,
1061.1Smacallan	.bs_w_8 = bs_notimpl_bs_w_8,
1071.1Smacallan
1081.1Smacallan	/* write multiple */
1091.1Smacallan	.bs_wm_1 = generic_bs_wm_1,
1101.1Smacallan	.bs_wm_2 = bs_notimpl_bs_wm_2,
1111.1Smacallan	.bs_wm_4 = bs_notimpl_bs_wm_4,
1121.1Smacallan	.bs_wm_8 = bs_notimpl_bs_wm_8,
1131.1Smacallan
1141.1Smacallan	/* write region */
1151.1Smacallan	.bs_wr_1 = bs_notimpl_bs_wr_1,
1161.1Smacallan	.bs_wr_2 = bs_notimpl_bs_wr_2,
1171.1Smacallan	.bs_wr_4 = bs_notimpl_bs_wr_4,
1181.1Smacallan	.bs_wr_8 = bs_notimpl_bs_wr_8,
1191.1Smacallan
1201.1Smacallan	/* set multiple */
1211.1Smacallan	.bs_sm_1 = bs_notimpl_bs_sm_1,
1221.1Smacallan	.bs_sm_2 = bs_notimpl_bs_sm_2,
1231.1Smacallan	.bs_sm_4 = bs_notimpl_bs_sm_4,
1241.1Smacallan	.bs_sm_8 = bs_notimpl_bs_sm_8,
1251.1Smacallan
1261.1Smacallan	/* set region */
1271.1Smacallan	.bs_sr_1 = bs_notimpl_bs_sr_1,
1281.1Smacallan	.bs_sr_2 = bs_notimpl_bs_sr_2,
1291.1Smacallan	.bs_sr_4 = bs_notimpl_bs_sr_4,
1301.1Smacallan	.bs_sr_8 = bs_notimpl_bs_sr_8,
1311.1Smacallan
1321.1Smacallan	/* copy */
1331.1Smacallan	.bs_c_1 = bs_notimpl_bs_c_1,
1341.1Smacallan	.bs_c_2 = bs_notimpl_bs_c_2,
1351.1Smacallan	.bs_c_4 = bs_notimpl_bs_c_4,
1361.1Smacallan	.bs_c_8 = bs_notimpl_bs_c_8,
1371.1Smacallan};
1381.1Smacallan
1391.1Smacallanint
1401.1Smacallanobio_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
1411.1Smacallan    bus_space_handle_t *bshp)
1421.1Smacallan{
1431.1Smacallan	const struct pmap_devmap *pd;
1441.1Smacallan	paddr_t startpa, endpa, pa, offset;
1451.1Smacallan	vaddr_t va;
1461.1Smacallan
1471.1Smacallan	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
1481.1Smacallan		/* Device was statically mapped. */
1491.1Smacallan		*bshp = pd->pd_va + (bpa - pd->pd_pa);
1501.1Smacallan		return (0);
1511.1Smacallan	}
1521.1Smacallan
1531.1Smacallan	endpa = round_page(bpa + size);
1541.1Smacallan	offset = bpa & PAGE_MASK;
1551.1Smacallan	startpa = trunc_page(bpa);
1561.2Sskrll
1571.1Smacallan	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
1581.1Smacallan	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
1591.1Smacallan	if (va == 0)
1601.1Smacallan		return (ENOMEM);
1611.1Smacallan
1621.1Smacallan	*bshp = va + offset;
1631.1Smacallan
1641.1Smacallan	const int pmapflags =
1651.1Smacallan	    (flag & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
1661.1Smacallan		? 0
1671.1Smacallan		: PMAP_NOCACHE;
1681.1Smacallan
1691.1Smacallan	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
1701.1Smacallan		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
1711.1Smacallan	}
1721.1Smacallan	pmap_update(pmap_kernel());
1731.1Smacallan
1741.1Smacallan	return (0);
1751.1Smacallan}
1761.1Smacallan
1771.1Smacallanint
1781.1Smacallanobio_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend, bus_size_t size,
1791.1Smacallan    bus_size_t alignment, bus_size_t boundary, int flags, bus_addr_t *bpap,
1801.1Smacallan    bus_space_handle_t *bshp)
1811.1Smacallan{
1821.1Smacallan
1831.1Smacallan	panic("obio_bs_alloc(): not implemented");
1841.1Smacallan}
1851.1Smacallan
1861.1Smacallan
1871.1Smacallanvoid
1881.1Smacallanobio_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
1891.1Smacallan{
1901.1Smacallan	vaddr_t va, endva;
1911.1Smacallan
1921.1Smacallan	if (pmap_devmap_find_va(bsh, size) != NULL) {
1931.1Smacallan		/* Device was statically mapped; nothing to do. */
1941.1Smacallan		return;
1951.1Smacallan	}
1961.1Smacallan
1971.1Smacallan	endva = round_page(bsh + size);
1981.1Smacallan	va = trunc_page(bsh);
1991.1Smacallan
2001.1Smacallan	pmap_kremove(va, endva - va);
2011.1Smacallan	uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
2021.1Smacallan}
2031.1Smacallan
2041.2Sskrllvoid
2051.1Smacallanobio_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
2061.1Smacallan{
2071.1Smacallan
2081.1Smacallan	panic("obio_bs_free(): not implemented");
2091.1Smacallan}
2101.1Smacallan
2111.1Smacallanint
2121.1Smacallanobio_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
2131.1Smacallan    bus_size_t size, bus_space_handle_t *nbshp)
2141.1Smacallan{
2151.1Smacallan
2161.1Smacallan	*nbshp = bsh + offset;
2171.1Smacallan	return (0);
2181.1Smacallan}
2191.1Smacallan
2201.1Smacallanvoid *
2211.1Smacallanobio_bs_vaddr(void *t, bus_space_handle_t bsh)
2221.1Smacallan{
2231.1Smacallan
2241.1Smacallan	return ((void *)bsh);
2251.1Smacallan}
2261.1Smacallan
2271.1Smacallanvoid
2281.1Smacallanobio_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
2291.1Smacallan    bus_size_t len, int flags)
2301.1Smacallan{
2311.1Smacallan
2321.1Smacallan	/* Nothing to do. */
2331.1Smacallan}
234