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 4 + ( prop_type address data address data' )
37 2dup @ 2 pick 8 + @ + <= -rot @ >= and IF
38 l@ 03000000 and 18 rshift nip
44 \ advance in 4 byte steps and (phys-addr(3) size(2)) steps
50 : (range-read-cells) ( range-addr #cells -- range-value )
51 \ if number of cells != 1; do 64bit read; else a 32bit read
55 \ this functions tries to find a mapping for the given address
56 \ it assumes that if we have #address-cells == 3 that we are trying
57 \ to do a PCI translation
59 \ nac - #address-cells
61 \ pnac - parent #address-cells
63 : (map-one-range) ( type range pnac nsc nac address -- address true | address false )
64 \ only check for the type if nac == 3 (PCI)
65 over 3 = 5 pick l@ 3000000 and 18 rshift 7 pick <> and IF
66 >r 2drop 3drop r> false EXIT
69 4 pick 4 pick 3 pick + 4 * +
73 ( type range pnac nsc nac address range nsc )
75 ( type range pnac nsc nac address size )
82 ( type range pnac nsc nac address size range nac )
85 ( type range pnac nsc nac address size child-mapping )
86 dup >r dup 3 pick > >r + over <= r> or IF
87 \ address is not inside the mapping range
88 >r 2drop 3drop r> r> drop false EXIT
91 ( type range pnac nsc nac address offset )
92 \ add the offset on the parent mapping
98 ( type range pnac nsc nac address offset parent-mapping-address )
101 \ read parent mapping
103 ( type range pnac nsc nac address offset parent-mapping )
104 + >r 3drop 3drop r> true
107 \ this word translates the given address starting from the node specified
108 \ in node; the word will return to the node it was started from
109 : translate-address ( node address -- address )
110 \ check for address type in "assigned-addresses"
111 2dup 1 pci-address-type ( node address type )
113 \ not found in "assigned-addresses", check in "reg"
114 drop 2dup 0 pci-address-type ( node address type )
117 \ check if it is the root node
118 dup parent 0= IF 2drop EXIT THEN
119 ( address type parent )
120 s" #address-cells" 2 pick get-property 2drop l@ >r \ nac
121 s" #size-cells" 2 pick get-property 2drop l@ >r \ nsc
122 s" #address-cells" 2 pick parent get-property 2drop l@ >r \ pnac
123 -rot ( node address type )
124 s" ranges" 4 pick get-property IF
126 ABORT" no ranges property; not translatable"
129 ( node address type ranges pnac nsc nac length )
130 4 / >r 3dup + + >r 5 roll r> r> swap / 0 ?DO
131 ( node type ranges pnac nsc nac address )
132 6dup (map-one-range) IF
138 ( node type pnac nsc nac address ranges )
139 4 pick 4 pick 4 pick + + 4 * + 4 -roll
141 >r 2drop 2drop r> ( node type address )
142 swap rot parent ( address type node )
147 \ this words translates the given address starting from the current node
148 : translate-my-address ( address -- address' )
149 get-node swap translate-address