Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: iommu.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | id: @(#)iommu.fth 1.3 07/05/06 | |
43 | purpose: | |
44 | copyright: Copyright 2007 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Use is subject to license terms. | |
46 | ||
47 | headerless | |
48 | ||
49 | my-space h# fff.ffff and constant devhandle | |
50 | 0 constant tsbnum | |
51 | ||
52 | 1 constant pci-map-attr-read | |
53 | 2 constant pci-map-attr-write | |
54 | pci-map-attr-read pci-map-attr-write or | |
55 | constant io-attributes | |
56 | ||
57 | h# b0 constant iommu-map-fun | |
58 | h# b1 constant iommu-demap-fun | |
59 | h# b2 constant iommu-getmap-fun | |
60 | h# b3 constant iommu-getbypass-fun | |
61 | h# b4 constant config-get-fun | |
62 | h# b5 constant config-put-fun | |
63 | h# b8 constant dma-sync-fun | |
64 | ||
65 | h# 80 constant fast-trap# | |
66 | ||
67 | 1 constant pci-map-attr-read | |
68 | 2 constant pci-map-attr-write | |
69 | pci-map-attr-read pci-map-attr-write or constant pci-map-attr | |
70 | ||
71 | 1 constant pci-sync-device | |
72 | 2 constant pci-sync-cpu | |
73 | pci-sync-device pci-sync-cpu or constant pci-sync | |
74 | ||
75 | variable dma-list dma-list off | |
76 | virtual-dma-addr virtual-dma-size dma-list free-memrange | |
77 | ||
78 | 1 constant ENOCUP | |
79 | 2 constant ENORADDR | |
80 | 3 constant EOINTR | |
81 | 4 constant EBADPGSZ | |
82 | 5 constant EBADTSB | |
83 | 6 constant EINVAL | |
84 | 7 constant EBADTRAP | |
85 | 8 constant EBADALIGN | |
86 | 9 constant EWOULDBLOCK | |
87 | d# 10 constant ENOACCESS | |
88 | d# 11 constant EIO | |
89 | d# 12 constant ECPUERROR | |
90 | d# 13 constant ENOTSUPPORTED | |
91 | d# 14 constant ENOMAP | |
92 | d# 15 constant ETOOMANY | |
93 | ||
94 | : fast-trap ( ??? #in #out fun# -- ??? ) | |
95 | dup >r fast-trap# htrap ( ??? status )( R: fun# ) | |
96 | r> swap ( ??? fun# status ) | |
97 | ?dup if ( ??? fun# status| ) | |
98 | cmn-error[ | |
99 | case | |
100 | ENOCUP of " ENOCUP" endof | |
101 | ENORADDR of " Invalid real address" endof | |
102 | EOINTR of " EOINTR" endof | |
103 | EBADPGSZ of " EBADPGSZ" endof | |
104 | EBADTSB of " EBADTSB" endof | |
105 | EINVAL of " Invalid hypervisor argument(s)" endof | |
106 | EBADTRAP of " EBADTRAP" endof | |
107 | EBADALIGN of " Improperly aligned address" endof | |
108 | EWOULDBLOCK of " EWOULDBLOCK" endof | |
109 | ENOACCESS of " Access to real address offset not permitted" endof | |
110 | EIO of " EIO" endof | |
111 | ECPUERROR of " ECPUERROR" endof | |
112 | ENOTSUPPORTED of " Hypervisor function not supported" endof | |
113 | ENOMAP of " ENOMAP" endof | |
114 | ETOOMANY of " ETOOMANY" endof | |
115 | " Invalid Hypervisor error code" rot | |
116 | endcase | |
117 | rot " %s. function: %x" ]cmn-end | |
118 | else ( ??? fun# ) | |
119 | drop ( ??? ) | |
120 | then ( ??? ) | |
121 | ; | |
122 | ||
123 | headers | |
124 | ||
125 | : pci-config-get ( pci-device pci-config-offset size -- data error-flag ) | |
126 | >r >r >r devhandle r> r> r> ( devhandle pci-device pci-config-offset size ) | |
127 | 4 3 config-get-fun fast-trap ( data error-flag ) | |
128 | ; | |
129 | ||
130 | : pci-config-put ( pci-device pci-config-offset size data -- error-flag ) | |
131 | >r >r >r >r devhandle r> r> r> r> ( devhandle pci-device pci-config-offset size data ) | |
132 | 5 2 config-put-fun fast-trap ( error-flag ) | |
133 | ; | |
134 | ||
135 | : pci-iommu-getmap ( tsbindex -- raddr io-attributes ) | |
136 | devhandle swap 2 3 iommu-getmap-fun fast-trap | |
137 | ; | |
138 | ||
139 | : (pci-iommu-map) ( tsbid #ttes io-page-list-p -- #ttes-actual ) | |
140 | >r >r >r devhandle r> r> pci-map-attr r> ( dev tsbid #ttes io-attributes io-page-list-p ) | |
141 | 5 2 iommu-map-fun fast-trap ( #ttes-actual ) | |
142 | ; | |
143 | ||
144 | : pci-iommu-map ( tsbindex #ttes io-page-list-p -- ) | |
145 | begin ( tsbindex #ttes io-page-list-p ) | |
146 | over >r 3dup (pci-iommu-map) r> ( tsbindex #ttes io-page-list-p #ttes-actual #ttes ) | |
147 | over <> ( tsbindex #ttes io-page-list-p #ttes-actual more? ) | |
148 | while ( tsbindex #ttes io-page-list-p #ttes-actual ) | |
149 | tuck /x * + >r ( tsbindex #ttes #ttes-actual )( R: io-page-list-p' ) | |
150 | tuck - >r ( tsbindex #ttes-actual )( R: io-page-list-p' #ttes' ) | |
151 | + r> r> ( tsbindex' #ttes' io-page-list-p' )( R: ) | |
152 | repeat ( tsbindex' #ttes' io-page-list-p' ) | |
153 | 2drop 2drop ( ) | |
154 | ; | |
155 | ||
156 | : (pci-iommu-demap) ( tsbindex #ttes -- #ttes-actual ) | |
157 | >r >r devhandle r> r> ( dev tsbindex #ttes ) | |
158 | 3 2 iommu-demap-fun fast-trap ( #ttes-actual ) | |
159 | ; | |
160 | ||
161 | : pci-iommu-demap ( tsbindex #ttes -- ) | |
162 | begin ( tsbindex #ttes ) | |
163 | dup ( tsbindex #ttes #ttes ) | |
164 | while | |
165 | 2dup (pci-iommu-demap) ( tsbindex #ttes #ttes-actual ) | |
166 | tuck - -rot + swap ( tsbindex' #ttes' ) | |
167 | repeat ( tsbindex' #ttes' ) | |
168 | 2drop ( ) | |
169 | ; | |
170 | ||
171 | : (pci-dma-sync) ( raddr size io-sync-direction -- #synced ) | |
172 | >r devhandle -rot r> ( dev raddr size io-sync-direction ) | |
173 | 4 2 dma-sync-fun fast-trap ( #synced ) | |
174 | ; | |
175 | ||
176 | : pci-dma-sync ( raddr size -- ) | |
177 | 2dup >r >r ( raddr size ) ( R: raddr size ) | |
178 | begin ( raddr size ) | |
179 | dup ( raddr size size ) | |
180 | while | |
181 | 2dup pci-sync-cpu (pci-dma-sync) ( raddr size #synced ) | |
182 | tuck - -rot + swap ( raddr' size' ) | |
183 | repeat ( raddr' size' ) | |
184 | 2drop ( ) | |
185 | ||
186 | r> r> ( raddr size ) | |
187 | begin ( raddr size ) | |
188 | dup ( raddr size size ) | |
189 | while ( raddr size ) | |
190 | 2dup pci-sync-device (pci-dma-sync) ( raddr size #synced ) | |
191 | tuck - -rot + swap ( raddr' size' ) | |
192 | repeat ( raddr' size' ) | |
193 | 2drop ( ) | |
194 | ; | |
195 | ||
196 | : setup-io-page-list ( raddr io-page-list #ttes -- ) | |
197 | 0 ?do ( raddr io-page-list ) | |
198 | 2dup x! /x + swap ( io-page-list' raddr ) | |
199 | mmu-pagesize + swap ( raddr' io-page-list' ) | |
200 | loop ( raddr' io-page-list' ) | |
201 | 2drop ( ) | |
202 | ; | |
203 | ||
204 | external | |
205 | ||
206 | : dma-alloc ( size -- vaddr ) mmu-pagesize swap 0 claim ; | |
207 | : dma-free ( vaddr size -- ) swap release ; | |
208 | ||
209 | : dma-sync ( virt-addr dev-addr size -- ) | |
210 | rot >physical drop swap ( dev-addr raddr size) | |
211 | pci-dma-sync ( dev-addr ) | |
212 | drop | |
213 | ; | |
214 | ||
215 | : dma-map-in ( virt size cache? -- dma-virt ) | |
216 | drop mmu-pagesize round-up ( virt size' ) | |
217 | mmu-pagesize over dma-list ( virt size align size list ) | |
218 | allocate-memrange throw dup >r ( virt size dma-virt )( R: dma-virt ) | |
219 | virtual-dma-base dup and32 - ( virt size offset ) | |
220 | mmu-pagesize / -rot ( tsbindex virt size ) | |
221 | mmu-pagesize / tuck tuck /x * ( tsbindex #ttes #ttes virt /io-page-list ) | |
222 | >r r@ /x swap 0 claim ( tsbindex #ttes #ttes virt io-page-list )( R: dma-virt /io-page-list ) | |
223 | dup >r swap ( tsbindex #ttes #ttes io-page-list virt )( R: dma-virt /io-page-list io-page-list ) | |
224 | >physical drop ( tsbindex #ttes #ttes io-page-list raddr ) | |
225 | swap rot setup-io-page-list ( tsbindex #ttes ) | |
226 | r@ >physical drop ( tsbindex #ttes io-page-list-p )( R: dma-virt /io-page-list io-page-list ) | |
227 | pci-iommu-map r> r> ( io-page-list /io-page-list )( R: dma-virt ) | |
228 | swap release r> ( dma-virt )( R: ) | |
229 | ; | |
230 | ||
231 | : dma-map-out ( virt dma-virt size -- ) | |
232 | mmu-pagesize round-up ( virt devadr size' ) | |
233 | 2dup dma-list free-memrange ( virt devadr size' ) | |
234 | mmu-pagesize / swap ( virt #ttes devadr ) | |
235 | virtual-dma-base dup and32 - ( virt #ttes offset ) | |
236 | mmu-pagesize / swap ( virt tsbindex #ttes ) | |
237 | pci-iommu-demap drop ( ) | |
238 | ; | |
239 | ||
240 | headers | |
241 | ||
242 | : dma-mapped? ( dma-virt -- raddr ) | |
243 | virtual-dma-base dup and32 - ( offset ) | |
244 | mmu-pagesize / ( tsbindex ) | |
245 | pci-iommu-getmap ( raddr attributes ) | |
246 | ." attributes: 0x" .h cr ( raddr ) | |
247 | ." raddr: 0x" .h ( ) | |
248 | ; | |
249 | ||
250 | headerless |