xref: /netbsd/sys/arch/arc/arc/bus_space_sparse.c (revision 0872f585)
1 /*	$NetBSD: bus_space_sparse.c,v 1.20 2016/07/12 23:53:18 matt Exp $	*/
2 /*	NetBSD: bus_machdep.c,v 1.1 2000/01/26 18:48:00 drochner Exp 	*/
3 
4 /*-
5  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
10  * Simulation Facility, NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * For sparse bus space
36  *
37  * This bus_space uses KSEG2 mapping, if the physical address is not
38  * accessible via KSEG0/KSEG1.
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: bus_space_sparse.c,v 1.20 2016/07/12 23:53:18 matt Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/bus.h>
46 #include <sys/extent.h>
47 #include <sys/malloc.h>
48 #include <sys/systm.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <mips/locore.h>
53 #include <mips/pte.h>
54 
55 extern paddr_t kvtophys(vaddr_t);	/* XXX */
56 
57 void
arc_sparse_bus_space_init(bus_space_tag_t bst,const char * name,paddr_t paddr,bus_addr_t start,bus_size_t size)58 arc_sparse_bus_space_init(bus_space_tag_t bst, const char *name, paddr_t paddr,
59     bus_addr_t start, bus_size_t size)
60 {
61 
62 	arc_bus_space_init(bst, name, paddr, ARC_BUS_SPACE_UNMAPPED,
63 	    start, size);
64 	bst->bs_compose_handle = arc_sparse_bus_space_compose_handle;
65 	bst->bs_dispose_handle = arc_sparse_bus_space_dispose_handle;
66 	bst->bs_paddr = arc_sparse_bus_space_paddr;
67 }
68 
69 int
arc_sparse_bus_space_compose_handle(bus_space_tag_t bst,bus_addr_t addr,bus_size_t size,int flags,bus_space_handle_t * bshp)70 arc_sparse_bus_space_compose_handle(bus_space_tag_t bst, bus_addr_t addr,
71     bus_size_t size, int flags, bus_space_handle_t *bshp)
72 {
73 	bus_size_t offset = addr - bst->bs_start;
74 	/*
75 	 * Since all buses can be linearly mappable, we don't have to check
76 	 * BUS_SPACE_MAP_LINEAR and BUS_SPACE_MAP_PREFETCHABLE.
77 	 */
78 	const bool cacheable = (flags & BUS_SPACE_MAP_CACHEABLE) != 0;
79 	const u_int pmap_flags = cacheable ? PMAP_WRITE_BACK : 0;
80 
81 	/*
82 	 * XXX - `bst->bs_pbase' must be page aligned,
83 	 * mips_trunc/round_page() cannot treat paddr_t due to overflow.
84 	 */
85 	paddr_t start = bst->bs_pbase + mips_trunc_page(offset);
86 	paddr_t end = bst->bs_pbase + mips_round_page(offset + size);
87 
88 	if (end <= MIPS_KSEG1_START - MIPS_KSEG0_START) {
89 		/* mappable on KSEG0 or KSEG1 */
90 		*bshp = (cacheable ?
91 		    MIPS_PHYS_TO_KSEG0(start) :
92 		    MIPS_PHYS_TO_KSEG1(start));
93 	} else {
94 		vaddr_t vaddr = uvm_km_alloc(kernel_map, (vsize_t)(end - start),
95 		    0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
96 
97 		if (vaddr == 0)
98 			panic("arc_sparse_bus_space_compose_handle: "
99 			      "cannot allocate KVA 0x%llx..0x%llx",
100 			      start, end);
101 		for (vaddr_t va = vaddr; start < end;
102 		     start += PAGE_SIZE, va += PAGE_SIZE) {
103 			pmap_kenter_pa(va, start, VM_PROT_READ|VM_PROT_WRITE,
104 			    pmap_flags);
105 		}
106 		pmap_update(pmap_kernel());
107 		vaddr += (offset & PGOFSET);
108 		*bshp = vaddr;
109 	}
110 	return 0;
111 }
112 
113 int
arc_sparse_bus_space_dispose_handle(bus_space_tag_t bst,bus_space_handle_t bsh,bus_size_t size)114 arc_sparse_bus_space_dispose_handle(bus_space_tag_t bst, bus_space_handle_t bsh,
115     bus_size_t size)
116 {
117 	vaddr_t start = mips_trunc_page(bsh);
118 	vaddr_t end = mips_round_page(bsh + size);
119 
120 	if (start < MIPS_KSEG2_START) /* KSEG0/KSEG1 */
121 		return 0;
122 
123 	pmap_kremove(start, end - start);
124 	pmap_update(pmap_kernel());
125 	uvm_km_free(kernel_map, start, end - start, UVM_KMF_VAONLY);
126 	return 0;
127 }
128 
129 int
arc_sparse_bus_space_paddr(bus_space_tag_t bst,bus_space_handle_t bsh,paddr_t * pap)130 arc_sparse_bus_space_paddr(bus_space_tag_t bst, bus_space_handle_t bsh,
131     paddr_t *pap)
132 {
133 
134 	*pap = kvtophys(bsh);
135 	return 0;
136 }
137