|
|
1.1 root 1: #! /bin/sh
2:
1.1.1.2 ! root 3: # $Id: bus-device-auto.sh,v 1.2 2006/09/30 12:43:35 fredette Exp $
1.1 root 4:
5: # generic/bus-device-auto.sh - automatically generates C code for
6: # generic bus device support:
7:
8: #
9: # Copyright (c) 2004 Matt Fredette
10: # All rights reserved.
11: #
12: # Redistribution and use in source and binary forms, with or without
13: # modification, are permitted provided that the following conditions
14: # are met:
15: # 1. Redistributions of source code must retain the above copyright
16: # notice, this list of conditions and the following disclaimer.
17: # 2. Redistributions in binary form must reproduce the above copyright
18: # notice, this list of conditions and the following disclaimer in the
19: # documentation and/or other materials provided with the distribution.
20: # 3. All advertising materials mentioning features or use of this software
21: # must display the following acknowledgement:
22: # This product includes software developed by Matt Fredette.
23: # 4. The name of the author may not be used to endorse or promote products
24: # derived from this software without specific prior written permission.
25: #
26: # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27: # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28: # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29: # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
30: # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31: # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32: # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34: # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35: # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: # POSSIBILITY OF SUCH DAMAGE.
37: #
38:
39: header=false
40:
41: for option
42: do
43: case $option in
44: --header) header=true ;;
45: esac
46: done
47:
48: PROG=`basename $0`
49: cat <<EOF
50: /* automatically generated by $PROG, do not edit! */
1.1.1.2 ! root 51: _TME_RCSID("\$Id: bus-device-auto.sh,v 1.2 2006/09/30 12:43:35 fredette Exp $");
1.1 root 52: EOF
53:
54: if $header; then :; else
55: cat <<EOF
56:
57: /* this indexes an initiator bus router array for a device with a port size
58: of 8 * (2 ^ siz_lg2) bits: */
59: #define TME_BUS_ROUTER_INIT_INDEX(siz_lg2, cycle_size, address) \\
60: ((( \\
61: /* by the maximum cycle size: */ \\
62: ((cycle_size) - 1) \\
63: \\
64: /* by the address alignment: */ \\
65: << siz_lg2) \\
66: + ((address) & ((1 << (siz_lg2)) - 1))) \\
67: \\
68: /* factor in the size of the generic bus router array: */ \\
69: * TME_BUS_ROUTER_SIZE(siz_lg2))
70:
71: /* this gives the number of entries that must be in a generic bus
72: router array for a device with a bus size of 8 * (2 ^ siz_lg2)
73: bits: */
74: #define TME_BUS_ROUTER_INIT_SIZE(siz_lg2) \\
75: TME_BUS_ROUTER_INIT_INDEX(siz_lg2, (1 << (siz_lg2)) + 1, 0)
76:
77: EOF
78: fi
79:
80: # permute over initiator bus port width:
81: #
82: i_width=8
83: while test ${i_width} != 32; do
84: i_width=`expr ${i_width} \* 2`
85:
86: # permute over initiator endianness:
87: #
88: for endian in b l; do
89: if test ${endian} = b; then endian_what=big; else endian_what=little; fi
90:
91: # start the array:
92: #
93: echo ""
94: echo "/* the ${i_width}-bit ${endian_what}-endian bus master bus router: */"
95: what="const tme_bus_lane_t tme_bus_device_router_${i_width}e${endian}"
96: if $header; then
97: echo "extern ${what}[];"
98: continue
99: fi
100: echo "${what}[TME_BUS_ROUTER_INIT_SIZE(TME_BUS${i_width}_LOG2)] = {"
101:
102: # permute over initiator maximum cycle size:
103: #
104: i_size=0
105: while test `expr ${i_size} \< ${i_width}` = 1; do
106: i_size=`expr ${i_size} + 8`
107:
108: # permute over initiator address offset:
109: #
110: i_offset=0
111: while test `expr ${i_offset} \< ${i_width}` = 1; do
112:
113: # calculate the initiator least and greatest lanes:
114: #
115: placeholder=false
116: if test ${endian} = b; then
117: i_lane_greatest=`expr -8 + ${i_width} - ${i_offset}`
118: i_lane_least=`expr 8 + ${i_lane_greatest} - ${i_size}`
119: if test `expr ${i_lane_least} \< 0` = 1; then
120: placeholder=true
121: fi
122: else
123: i_lane_least=$i_offset
124: i_lane_greatest=`expr -8 + ${i_offset} + ${i_size}`
125: if test `expr ${i_lane_greatest} \>= ${i_width}` = 1; then
126: placeholder=true
127: fi
128: fi
129:
130: # permute over responder bus port width:
131: #
132: r_width=4
133: while test `expr ${r_width} \< ${i_width}` = 1; do
134: r_width=`expr ${r_width} \* 2`
135:
136: # permute over responder bus port least lane:
137: #
138: r_lane_least=0
139: while test `expr ${r_lane_least} \< ${i_width}` = 1; do
140: r_lane_greatest=`expr -8 + ${r_lane_least} + ${r_width}`
141:
142: # emit the initiator information:
143: #
144: echo ""
145: echo " /* initiator maximum cycle size: ${i_size} bits"
146: echo " initiator address offset: ${i_offset} bits"
147: if $placeholder; then
148: echo " (a ${i_width}-bit initiator cannot request ${i_size} bits at an ${i_offset}-bit offset - this is an array placeholder)"
149: fi
150:
151: # emit the responder information:
152: #
153: echo " responder bus port size: ${r_width} bits"
154: echo -n " responder port least lane: D"`expr ${r_lane_least} + 7`"-D${r_lane_least}"
155:
156: # if the responder bus port greatest lane is
157: # greater than the initiator bus port width,
158: # part of the responder's port is outside of
159: # the initiator's port:
160: #
161: if test `expr ${r_lane_greatest} \>= ${i_width}` = 1; then
162: echo ""
163: echo -n " (responder port not correctly positioned for this initiator)"
164: fi
165: echo ": */"
166:
167: # permute over the lanes:
168: #
169: lane=0
170: if test ${endian} = b; then
171: route=`expr ${i_size} / 8`
172: route_increment=-1
173: else
174: route=-1
175: route_increment=1
176: fi
177: while test `expr ${lane} \< ${i_width}` = 1; do
178: echo -n " /* D"`expr ${lane} + 7`"-D${lane} */ "
179:
180: # see if this lane is on in the responder:
181: #
182: if test `expr ${lane} \>= ${r_lane_least}` = 1 \
183: && test `expr ${lane} \<= ${r_lane_greatest}` = 1; then
184: r_lane_on=true
185: else
186: r_lane_on=false
187: fi
188:
189: # see if this lane is on in the initiator:
190: #
191: if test `expr ${lane} \>= ${i_lane_least}` = 1 \
192: && test `expr ${lane} \<= ${i_lane_greatest}` = 1; then
193: i_lane_on=true
194: route=`expr ${route} + ${route_increment}`
195: else
196: i_lane_on=false
197: fi
198:
199: # if this is a placeholder entry:
200: #
201: if $placeholder; then
202: echo -n "TME_BUS_LANE_ABORT"
203:
204: # otherwise, this is a real entry:
205: #
206: else
207: if $i_lane_on; then
208: echo -n "TME_BUS_LANE_ROUTE(${route})"
209: if $r_lane_on; then :; else
210: echo -n " | TME_BUS_LANE_WARN"
211: fi
212: else
213: echo -n "TME_BUS_LANE_UNDEF"
214: fi
215: fi
216:
217: echo ","
218: lane=`expr ${lane} + 8`
219: done
220:
221: r_lane_least=`expr ${r_lane_least} + 8`
222: done
223: done
224:
225: i_offset=`expr ${i_offset} + 8`
226: done
227: done
228:
229: # finish the array:
230: #
231: echo "};"
232: done
233:
234: # permute over read/write:
235: #
236: for name in read write; do
237: capname=`echo $name | tr a-z A-Z`
238: if test $name = read; then
239: naming="reading"
240: from="from"
241: constbuffer=""
242: else
243: naming="writing"
244: from="to"
245: constbuffer="const "
246: fi
247:
248: echo ""
249: echo "/* the ${i_width}-bit bus master DMA ${name} function: */"
250: if $header; then
251: echo "int tme_bus_device_dma_${name}_${i_width} _TME_P((struct tme_bus_device *,"
252: echo " tme_bus_addr_t,"
253: echo " tme_bus_addr_t,"
254: echo " ${constbuffer}tme_uint8_t *,"
255: echo " unsigned int));"
256: continue
257: fi
258: echo "int"
259: echo "tme_bus_device_dma_${name}_${i_width}(struct tme_bus_device *bus_device,"
260: echo " tme_bus_addr_t address_init,"
261: echo " tme_bus_addr_t size,"
262: echo " ${constbuffer}tme_uint8_t *buffer,"
263: echo " unsigned int locks)"
264: echo "{"
265: echo " struct tme_bus_tlb *tlb, tlb_local;"
266: echo " struct tme_bus_connection *conn_bus;"
267: echo " tme_bus_addr_t count_minus_one, count;"
268: echo " struct tme_bus_cycle cycle;"
269: echo " tme_bus_addr_t address_resp;"
270: echo " int shift;"
271: echo " int err;"
272: echo ""
273: echo " /* assume no error: */"
274: echo " err = TME_OK;"
275: echo ""
276: echo " /* loop while we have more bytes to ${name}: */"
277: echo " for (; err == TME_OK && size > 0; ) {"
278: echo ""
279: echo " /* hash this address into a TLB entry: */"
280: echo " tlb = (*bus_device->tme_bus_device_tlb_hash)"
281: echo " (bus_device,"
282: echo " address_init,"
283: echo " TME_BUS_CYCLE_${capname});"
284: echo ""
1.1.1.2 ! root 285: echo " /* busy this TLB entry: */"
! 286: echo " tme_bus_tlb_busy(tlb);"
! 287: echo ""
! 288: echo " /* if this TLB entry is invalid, doesn't cover this address, or if it doesn't"
1.1 root 289: echo " allow ${naming}, reload it: */"
1.1.1.2 ! root 290: echo " if (tme_bus_tlb_is_invalid(tlb)"
! 291: echo " || address_init < tlb->tme_bus_tlb_addr_first"
! 292: echo " || address_init > tlb->tme_bus_tlb_addr_last"
1.1 root 293: echo " || (tlb->tme_bus_tlb_emulator_off_${name} == TME_EMULATOR_OFF_UNDEF"
294: echo " && !(tlb->tme_bus_tlb_cycles_ok & TME_BUS_CYCLE_${capname}))) {"
295: echo ""
1.1.1.2 ! root 296: echo " /* unbusy this TLB entry for filling: */"
! 297: echo " tme_bus_tlb_unbusy_fill(tlb);"
! 298: echo ""
1.1 root 299: echo " /* reserve this TLB entry: */"
1.1.1.2 ! root 300: echo " tlb_local.tme_bus_tlb_global = tlb;"
1.1 root 301: echo " tlb = &tlb_local;"
302: echo ""
303: echo " /* get our bus connection: */"
1.1.1.2 ! root 304: echo " conn_bus = tme_memory_atomic_pointer_read(struct tme_bus_connection *,"
! 305: echo " bus_device->tme_bus_device_connection,"
! 306: echo " &bus_device->tme_bus_device_connection_rwlock);"
1.1 root 307: echo ""
308: echo " /* unlock the device: */"
309: echo " (*bus_device->tme_bus_device_unlock)(bus_device, locks);"
310: echo ""
311: echo " /* reload the TLB entry: */"
312: echo " err = (*conn_bus->tme_bus_tlb_fill)"
313: echo " (conn_bus,"
314: echo " tlb,"
315: echo " address_init,"
316: echo " TME_BUS_CYCLE_${capname});"
317: echo ""
318: echo " /* lock the device: */"
319: echo " (*bus_device->tme_bus_device_lock)(bus_device, locks);"
320: echo ""
321: echo " /* return if we couldn't fill the TLB entry: */"
322: echo " if (err != TME_OK) {"
323: echo " return (err);"
324: echo " }"
325: echo ""
326: echo " /* store the TLB entry: */"
327: echo " tme_bus_tlb_back(tlb);"
1.1.1.2 ! root 328: echo ""
! 329: echo " /* loop to check the newly filled TLB entry: */"
! 330: echo " continue;"
1.1 root 331: echo " }"
332: echo ""
333: echo " /* if this TLB entry allows fast ${naming}: */"
334: echo " if (tlb->tme_bus_tlb_emulator_off_${name} != TME_EMULATOR_OFF_UNDEF) {"
335: echo ""
336: echo " /* see how many bytes we can fast ${name} ${from} this TLB entry,"
337: echo " starting at this address: */"
1.1.1.2 ! root 338: echo " count_minus_one = (tlb->tme_bus_tlb_addr_last - address_init);"
1.1 root 339: echo ""
340: echo " /* ${name} that many bytes or size bytes, whichever is smaller: */"
341: echo " count_minus_one = TME_MIN(count_minus_one,"
342: echo " (size - 1));"
343: echo " count = count_minus_one + 1;"
344: echo " assert (count != 0);"
345: echo ""
1.1.1.2 ! root 346: echo " /* do the bus ${name}: */"
! 347: echo " tme_memory_bus_${name}_buffer((tlb->tme_bus_tlb_emulator_off_${name} + address_init), buffer, count, tlb->tme_bus_tlb_rwlock, sizeof(tme_uint8_t), sizeof(tme_uint${i_width}_t));"
! 348: echo ""
! 349: echo " /* unbusy this TLB entry: */"
! 350: echo " tme_bus_tlb_unbusy(tlb);"
1.1 root 351: echo " }"
352: echo ""
353: echo " /* otherwise, we have to do a slow ${name}: */"
354: echo " else {"
355: echo ""
356: echo " /* get the size of this bus cycle: */"
357: echo " count = (1 << TME_BUS${i_width}_LOG2);"
358: echo " count -= (address_init & (count - 1));"
359: echo " count = TME_MIN(count, size);"
360: echo ""
361: echo " /* fill the cycle structure: */"
362: echo " cycle.tme_bus_cycle_type = TME_BUS_CYCLE_${capname};"
363: echo " cycle.tme_bus_cycle_size = count;"
364: echo " cycle.tme_bus_cycle_buffer = (tme_uint8_t *) buffer; /* XXX this breaks const */"
365: echo " cycle.tme_bus_cycle_buffer_increment = 1;"
366: echo " cycle.tme_bus_cycle_lane_routing"
367: echo " = (bus_device->tme_bus_device_router"
368: echo " + TME_BUS_ROUTER_INIT_INDEX(TME_BUS${i_width}_LOG2, count, address_init));"
369: echo ""
370: echo " /* XXX this should come from a socket configuration: */"
371: echo " cycle.tme_bus_cycle_port = TME_BUS_CYCLE_PORT(0, TME_BUS${i_width}_LOG2);"
372: echo ""
373: echo " /* form the physical address for the bus cycle handler: */"
374: echo " address_resp = tlb->tme_bus_tlb_addr_offset + address_init;"
375: echo " shift = tlb->tme_bus_tlb_addr_shift;"
376: echo " if (shift < 0) {"
377: echo " address_resp <<= (0 - shift);"
378: echo " }"
379: echo " else if (shift > 0) {"
380: echo " address_resp >>= shift;"
381: echo " }"
382: echo " cycle.tme_bus_cycle_address = address_resp;"
383: echo ""
1.1.1.2 ! root 384: echo " /* unbusy this TLB entry: */"
! 385: echo " tme_bus_tlb_unbusy(tlb);"
! 386: echo ""
1.1 root 387: echo " /* unlock the device: */"
388: echo " (*bus_device->tme_bus_device_unlock)(bus_device, locks);"
389: echo ""
390: echo " /* run the bus cycle: */"
391: echo " err = (*tlb->tme_bus_tlb_cycle)"
392: echo " (tlb->tme_bus_tlb_cycle_private, &cycle);"
393: echo ""
1.1.1.2 ! root 394: echo " /* if the TLB entry was invalidated before the ${name}: */"
! 395: echo " if (err == EBADF"
! 396: echo " && tme_bus_tlb_is_invalid(tlb)) {"
! 397: echo " count = 0;"
1.1 root 398: echo " }"
399: echo ""
400: echo " /* otherwise, any other error might be a bus error: */"
401: echo " else if (err != TME_OK) {"
402: echo " err = tme_bus_tlb_fault(tlb, &cycle, err);"
403: echo " assert (err != TME_OK);"
404: echo " }"
405: echo ""
406: echo " /* lock the device: */"
407: echo " (*bus_device->tme_bus_device_lock)(bus_device, locks);"
408: echo " }"
409: echo ""
410: echo " /* update the address, buffer, and size and continue: */"
411: echo " address_init += count;"
412: echo " buffer += count;"
413: echo " size -= count;"
414: echo " }"
415: echo ""
416: echo " return (err);"
417: echo "}"
418: done
419:
420: done
421:
422: # done:
423: #
424: exit 0
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.