1
\ *****************************************************************************
2
\ * Copyright (c) 2004, 2008 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
10
\ * IBM Corporation - initial implementation
11
\ ****************************************************************************/
13
\ this is a C-to-Forth translation from the translate
14
\ address code in the client
15
\ with extensions to handle different sizes of #size-cells
17
\ this tries to figure out if it is a PCI device what kind of
18
\ translation is wanted
19
\ if prop_type is 0, "reg" property is used, otherwise "assigned-addresses"
20
: pci-address-type ( node address prop_type -- type )
21
-rot 2 pick ( prop_type node address prop_type )
23
swap s" reg" rot get-property ( prop_type address data dlen false )
25
swap s" assigned-addresses" rot get-property ( prop_type address data dlen false )
27
IF 2drop -1 EXIT THEN 4 / 5 /
28
\ advance (phys-addr(3) size(2)) steps
30
\ BARs and Expansion ROM must be in assigned-addresses...
31
\ so if prop_type is 0 ("reg") and a config space offset is set
32
\ we skip this entry...
33
dup l@ FF AND 0<> ( prop_type address data cfgspace_offset? )
34
3 pick 0= ( prop_type address data cfgspace_offset? reg_prop? )
36
2dup 8 + ( prop_type address data address data' )
37
2dup l@ 2 pick 8 + l@ + <= -rot l@ >= and IF
38
l@ 03000000 and 18 rshift nip
39
\ no 64bit translations supported pretend it is 32bit
46
\ advance in 4 byte steps and (phys-addr(3) size(2)) steps
52
: (range-read-cells) ( range-addr #cells -- range-value )
53
\ if number of cells != 1; do 64bit read; else a 32bit read
57
\ this functions tries to find a mapping for the given address
58
\ it assumes that if we have #address-cells == 3 that we are trying
59
\ to do a PCI translation
61
\ nac - #address-cells
63
\ pnac - parent #address-cells
65
: (map-one-range) ( type range pnac nsc nac address -- address true | address false )
66
\ only check for the type if nac == 3 (PCI)
67
over 3 = 5 pick l@ 3000000 and 18 rshift 7 pick <> and IF
68
>r 2drop 3drop r> false EXIT
71
4 pick 4 pick 3 pick + 4 * +
75
( type range pnac nsc nac address range nsc )
77
( type range pnac nsc nac address size )
84
( type range pnac nsc nac address size range nac )
87
( type range pnac nsc nac address size child-mapping )
88
dup >r dup 3 pick > >r + over <= r> or IF
89
\ address is not inside the mapping range
90
>r 2drop 3drop r> r> drop false EXIT
93
( type range pnac nsc nac address offset )
94
\ add the offset on the parent mapping
100
( type range pnac nsc nac address offset parent-mapping-address )
103
\ read parent mapping
105
( type range pnac nsc nac address offset parent-mapping )
106
+ >r 3drop 3drop r> true
109
\ this word translates the given address starting from the node specified
110
\ in node; the word will return to the node it was started from
111
: translate-address ( node address -- address )
112
\ check for address type in "assigned-addresses"
113
2dup 1 pci-address-type ( node address type )
115
\ not found in "assigned-addresses", check in "reg"
116
drop 2dup 0 pci-address-type ( node address type )
119
\ check if it is the root node
120
dup parent 0= IF 2drop EXIT THEN
121
( address type parent )
122
s" #address-cells" 2 pick get-property 2drop l@ >r \ nac
123
s" #size-cells" 2 pick get-property 2drop l@ >r \ nsc
124
s" #address-cells" 2 pick parent get-property 2drop l@ >r \ pnac
125
-rot ( node address type )
126
s" ranges" 4 pick get-property IF
128
ABORT" no ranges property; not translatable"
131
( node address type ranges pnac nsc nac length )
132
4 / >r 3dup + + >r 5 roll r> r> swap / 0 ?DO
133
( node type ranges pnac nsc nac address )
134
6dup (map-one-range) IF
140
( node type pnac nsc nac address ranges )
141
4 pick 4 pick 4 pick + + 4 * + 4 -roll
143
>r 2drop 2drop r> ( node type address )
144
swap rot parent ( address type node )
149
\ this words translates the given address starting from the current node
150
: translate-my-address ( address -- address' )
151
get-node swap translate-address