1 /* $NetBSD: bus_mem.c,v 1.14 2017/05/22 16:39:40 ragge Exp $ */
2 /*
3 * Copyright (c) 1998 Matt Thomas
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Ludd by Bertram Barth.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: bus_mem.c,v 1.14 2017/05/22 16:39:40 ragge Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/cpu.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/kernel.h>
40
41 #include <uvm/uvm_extern.h>
42
43
44 static int
vax_mem_bus_space_map(void * t,bus_addr_t pa,bus_size_t size,int cacheable,bus_space_handle_t * bshp,int f2)45 vax_mem_bus_space_map(
46 void *t,
47 bus_addr_t pa,
48 bus_size_t size,
49 int cacheable,
50 bus_space_handle_t *bshp,
51 int f2)
52 {
53 vaddr_t va;
54
55 size += (pa & VAX_PGOFSET); /* have to include the byte offset */
56 va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
57 if (va == 0)
58 return (ENOMEM);
59
60 *bshp = (bus_space_handle_t)(va + (pa & VAX_PGOFSET));
61
62 ioaccess(va, pa, (size + VAX_NBPG - 1) >> VAX_PGSHIFT);
63
64 return 0;
65 }
66
67 static int
vax_mem_bus_space_subregion(void * t,bus_space_handle_t h,bus_size_t o,bus_size_t s,bus_space_handle_t * hp)68 vax_mem_bus_space_subregion(
69 void *t,
70 bus_space_handle_t h,
71 bus_size_t o,
72 bus_size_t s,
73 bus_space_handle_t *hp)
74 {
75 *hp = h + o;
76 return (0);
77 }
78
79 static void
vax_mem_bus_space_unmap(void * t,bus_space_handle_t h,bus_size_t size,int f)80 vax_mem_bus_space_unmap(
81 void *t,
82 bus_space_handle_t h,
83 bus_size_t size,
84 int f)
85 {
86 u_long va = trunc_page(h);
87 u_long endva = round_page(h + size);
88
89 /*
90 * Free the kernel virtual mapping.
91 */
92 iounaccess(va, size >> VAX_PGSHIFT);
93 uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
94 }
95
96 static int
vax_mem_bus_space_alloc(void * t,bus_addr_t rs,bus_addr_t re,bus_size_t s,bus_size_t a,bus_size_t b,int f,bus_addr_t * ap,bus_space_handle_t * hp)97 vax_mem_bus_space_alloc(
98 void *t,
99 bus_addr_t rs,
100 bus_addr_t re,
101 bus_size_t s,
102 bus_size_t a,
103 bus_size_t b,
104 int f,
105 bus_addr_t *ap,
106 bus_space_handle_t *hp)
107 {
108 panic("vax_mem_bus_alloc not implemented");
109 }
110
111 static void
vax_mem_bus_space_free(void * t,bus_space_handle_t h,bus_size_t s)112 vax_mem_bus_space_free(
113 void *t,
114 bus_space_handle_t h,
115 bus_size_t s)
116 {
117 panic("vax_mem_bus_free not implemented");
118 }
119
120 static paddr_t
vax_mem_bus_space_mmap(void * v,bus_addr_t addr,off_t off,int prot,int flags)121 vax_mem_bus_space_mmap(void *v, bus_addr_t addr, off_t off, int prot, int flags)
122 {
123 bus_addr_t rv;
124
125 rv = addr + off;
126 return btop(rv);
127 }
128
129 struct vax_bus_space vax_mem_bus_space = {
130 NULL,
131 vax_mem_bus_space_map,
132 vax_mem_bus_space_unmap,
133 vax_mem_bus_space_subregion,
134 vax_mem_bus_space_alloc,
135 vax_mem_bus_space_free,
136 vax_mem_bus_space_mmap,
137 };
138