Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / slof / fs / pci-helper.fs
1 \ *****************************************************************************
2 \ * Copyright (c) 2004, 2011 IBM Corporation
3 \ * All rights reserved.
4 \ * This program and the accompanying materials
5 \ * are made available under the terms of the BSD License
6 \ * which accompanies this distribution, and is available at
7 \ * http://www.opensource.org/licenses/bsd-license.php
8 \ *
9 \ * Contributors:
10 \ *     IBM Corporation - initial implementation
11 \ ****************************************************************************/
12
13 \ ----------------------------------------------------------
14 \ **************** PCI Helper functions  *******************
15 \ ----------------------------------------------------------
16
17 \ convert an integer to string of len digits
18 : int2str ( int len -- str len ) swap s>d rot <# 0 ?DO # LOOP #> ;
19
20 \ convert addr to busnr
21 : pci-addr2bus ( addr -- busnr ) 10 rshift FF and ;
22
23 \ convert addr to devnr
24 : pci-addr2dev ( addr -- dev ) B rshift 1F and ;
25
26 \ convert addr to functionnumber
27 : pci-addr2fn ( addr -- dev ) 8 rshift 7 and ;
28
29 \ convert busnr devnr to addr
30 : pci-bus2addr ( busnr devnr -- addr ) B lshift swap 10 lshift + ;
31
32 \ print out a pci config addr
33 : pci-addr-out ( addr -- ) dup pci-addr2bus 2 0.r space FFFF and 4 0.r ;
34
35 \ Dump out the whole configspace
36 : pci-dump ( addr -- )
37         10 0 DO
38                 dup
39                 cr i 4 * +
40                 dup pci-addr-out space
41                 rtas-config-l@ 8 0.r
42         LOOP
43         drop cr
44 ;
45
46
47 \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 \ the following functions use l@ to fetch the data,
49 \ that's because the some pcie cores have probs with w@
50 \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
52 \ read Vendor ID
53 : pci-vendor@ ( addr -- id )                 rtas-config-l@ FFFF and ;
54
55 \ read Device ID
56 : pci-device@ ( addr -- id )                 rtas-config-l@ 10 rshift ;
57
58 \ read Status
59 : pci-status@ ( addr -- status )         4 + rtas-config-l@ 10 rshift ;
60
61 \ read Revision ID
62 : pci-revision@ ( addr -- id )           8 + rtas-config-b@ ;
63
64 \ read Class Code
65 : pci-class@  ( addr -- class )          8 + rtas-config-l@ 8 rshift ;
66
67 \ read Cache Line Size
68 : pci-cache@  ( addr -- size )           C + rtas-config-b@ ;
69
70 \ read Header Type
71 : pci-htype@  ( addr -- type )           E + rtas-config-b@  ;
72
73 \ read Sub Vendor ID
74 : pci-sub-vendor@ ( addr -- sub-id )    2C + rtas-config-l@ FFFF and ;
75
76 \ read Sub Device ID
77 : pci-sub-device@ ( addr -- sub-id )    2C + rtas-config-l@ 10 rshift FFFF and ;
78
79 \ read Interrupt Pin
80 : pci-interrupt@  ( addr -- interrupt ) 3D + rtas-config-b@ ;
81
82 \ read Minimum Grant
83 : pci-min-grant@  ( addr -- min-gnt )   3E + rtas-config-b@ ;
84
85 \ read Maximum Latency
86 : pci-max-lat@  ( addr -- max-lat )     3F + rtas-config-b@ ;
87
88 \ Check if Capabilities are valid
89 : pci-capabilities?  ( addr -- 0|1 ) pci-status@ 4 rshift 1 and ;
90
91 \ fetch the offset of the next capability
92 : pci-cap-next  ( cap-addr -- next-cap-off ) rtas-config-b@ FC and ;
93
94 \ calc the address of the next capability
95 : pci-cap-next-addr  ( cap-addr -- next-cap-addr ) 1+ dup pci-cap-next dup IF swap -100 and + ELSE nip THEN ;
96
97
98 \ Dump out all capabilities
99 : pci-cap-dump ( addr -- )
100         cr
101         dup pci-capabilities? IF
102                 33 + BEGIN
103                         pci-cap-next-addr dup 0<>
104                 WHILE
105                         dup pci-addr-out s"  : " type
106                         dup rtas-config-b@ 2 0.r cr
107                 REPEAT
108                 s" end found "
109         ELSE
110                 s" capabilities not enabled!"
111         THEN
112         type cr drop
113 ;
114
115 \ search the capability-list for this id
116 : pci-cap-find ( addr id -- capp-addr|0 )
117         swap dup pci-capabilities? IF
118                 33 + BEGIN
119                         pci-cap-next-addr dup 0<> IF
120                                 dup rtas-config-b@ 2 pick =
121                         ELSE
122                                 true
123                         THEN
124                 UNTIL
125                 nip
126         ELSE
127                 2drop 0
128         THEN
129 ;
130
131 \ check wether this device is a pci-express device
132 : pci-express? ( addr -- 0|1 ) 10 pci-cap-find 0<> ;
133
134 \ check wether this device is a pci-express device
135 : pci-x? ( addr -- 0|1 ) 07 pci-cap-find 0<> ;
136
137 \ check wether this device has extended config space
138 : pci-config-ext? ( addr -- 0|1 ) pci-express? ;
139
140
141 \ Disable Bus Master, Memory Space and I/O Space for this device
142 : pci-device-disable ( -- ) my-space 4 + dup rtas-config-l@ 7 invert and swap rtas-config-l! ;
143
144 \ Enable Bus Master
145 : pci-master-enable ( -- ) my-space 4 + dup rtas-config-l@ 4 or swap rtas-config-l! ;
146
147 \ Disable Bus Master
148 : pci-master-disable ( -- ) my-space 4 + dup rtas-config-l@ 4 invert and swap rtas-config-l! ;
149
150 \ Enable response to mem accesses of pci device
151 : pci-mem-enable ( -- ) my-space 4 + dup rtas-config-w@ 2 or swap rtas-config-w! ;
152
153 \ Enable response to I/O accesses of pci-device
154 : pci-io-enable ( -- ) my-space 4 + dup rtas-config-w@ 1 or swap rtas-config-w! ;
155
156 \ Enable Bus Master, I/O and mem access
157 : pci-enable ( -- ) my-space 4 + dup rtas-config-w@ 7 or swap rtas-config-w! ;
158
159 \ Enable #PERR and #SERR errors of pci-device
160 : pci-error-enable ( -- ) my-space 4 + dup rtas-config-w@ 140 or swap rtas-config-w! ;
161
162 \ prints out the ScanInformation about a device
163 \ char is a sign for device type e.g. D - device ; B - bridge
164 : pci-out ( addr char -- )
165         15 spaces
166         over pci-addr-out
167         s"  (" type emit s" ) : " type
168         dup pci-vendor@ 4 0.r space
169         pci-device@ 4 0.r
170         4 spaces
171 ;
172
173
174 \ set and fetch the interrupt Pin
175 : pci-irq-line@  ( addr -- irq-pin ) 3C + rtas-config-b@ ;
176 : pci-irq-line!  ( pin addr -- ) 3C + rtas-config-b! ;
177
178 \ set and fetch primary bus number
179 : pci-bus-prim! ( nr addr -- ) 18 + dup rtas-config-l@ FFFFFF00 and rot + swap rtas-config-l! ;
180 : pci-bus-prim@ ( addr -- nr ) 18 + rtas-config-l@ FF and ;
181
182 \ set and fetch secondary bus number
183 : pci-bus-scnd! ( nr addr -- ) 18 + dup rtas-config-l@ FFFF00FF and rot 8 lshift + swap rtas-config-l! ;
184 : pci-bus-scnd@ ( addr -- nr ) 18 + rtas-config-l@ 8 rshift FF and ;
185
186 \ set and fetch subordinate bus number
187 : pci-bus-subo! ( nr addr -- ) 18 + dup rtas-config-l@ FF00FFFF and rot 10 lshift + swap rtas-config-l! ;
188 : pci-bus-subo@ ( addr -- nr ) 18 + rtas-config-l@ 10 rshift FF and ;
189
190 \ set and fetch primary, secondary and subordinate bus number
191 : pci-bus! ( subo scnd prim addr -- ) swap rot 8 lshift + rot 10 lshift + swap 18 + dup rtas-config-l@ FF000000 and rot + swap rtas-config-l! ;
192 : pci-bus@ ( addr -- subo scnd prim ) 18 + rtas-config-l@ dup 10 rshift FF and swap dup 8 rshift FF and swap FF and ;
193
194 \ Reset secondary Status
195 : pci-reset-2nd ( addr -- ) 1C + dup rtas-config-l@ FFFF0000 or swap rtas-config-l! ;