xref: /netbsd-src/share/man/man9/bus_space.9 (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1.\" $NetBSD: bus_space.9,v 1.20 2001/09/04 02:10:52 thorpej Exp $
2.\"
3.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Christopher G. Demetriou.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgment:
19.\" 	This product includes software developed by the NetBSD
20.\" 	Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd August 13, 1997
38.Dt BUS_SPACE 9
39.Os
40.Sh NAME
41.Nm bus_space ,
42.Nm bus_space_barrier ,
43.Nm bus_space_copy_region_1 ,
44.Nm bus_space_copy_region_2 ,
45.Nm bus_space_copy_region_4 ,
46.Nm bus_space_copy_region_8 ,
47.Nm bus_space_free ,
48.Nm bus_space_map ,
49.Nm bus_space_peek_1 ,
50.Nm bus_space_peek_2 ,
51.Nm bus_space_peek_4 ,
52.Nm bus_space_peek_8 ,
53.Nm bus_space_poke_1 ,
54.Nm bus_space_poke_2 ,
55.Nm bus_space_poke_4 ,
56.Nm bus_space_poke_8 ,
57.Nm bus_space_read_1 ,
58.Nm bus_space_read_2 ,
59.Nm bus_space_read_4 ,
60.Nm bus_space_read_8 ,
61.Nm bus_space_read_multi_1 ,
62.Nm bus_space_read_multi_2 ,
63.Nm bus_space_read_multi_4 ,
64.Nm bus_space_read_multi_8 ,
65.Nm bus_space_read_multi_stream_1 ,
66.Nm bus_space_read_multi_stream_2 ,
67.Nm bus_space_read_multi_stream_4 ,
68.Nm bus_space_read_multi_stream_8 ,
69.Nm bus_space_read_region_1 ,
70.Nm bus_space_read_region_2 ,
71.Nm bus_space_read_region_4 ,
72.Nm bus_space_read_region_8 ,
73.Nm bus_space_read_region_stream_1 ,
74.Nm bus_space_read_region_stream_2 ,
75.Nm bus_space_read_region_stream_4 ,
76.Nm bus_space_read_region_stream_8 ,
77.Nm bus_space_read_stream_1 ,
78.Nm bus_space_read_stream_2 ,
79.Nm bus_space_read_stream_4 ,
80.Nm bus_space_read_stream_8 ,
81.Nm bus_space_set_region_1 ,
82.Nm bus_space_set_region_2 ,
83.Nm bus_space_set_region_4 ,
84.Nm bus_space_set_region_8 ,
85.Nm bus_space_subregion ,
86.Nm bus_space_unmap ,
87.Nm bus_space_vaddr ,
88.Nm bus_space_mmap ,
89.Nm bus_space_write_1 ,
90.Nm bus_space_write_2 ,
91.Nm bus_space_write_4 ,
92.Nm bus_space_write_8 ,
93.Nm bus_space_write_multi_1 ,
94.Nm bus_space_write_multi_2 ,
95.Nm bus_space_write_multi_4 ,
96.Nm bus_space_write_multi_8 ,
97.Nm bus_space_write_multi_stream_1 ,
98.Nm bus_space_write_multi_stream_2 ,
99.Nm bus_space_write_multi_stream_4 ,
100.Nm bus_space_write_multi_stream_8 ,
101.Nm bus_space_write_region_1 ,
102.Nm bus_space_write_region_2 ,
103.Nm bus_space_write_region_4 ,
104.Nm bus_space_write_region_8
105.Nm bus_space_write_region_stream_1 ,
106.Nm bus_space_write_region_stream_2 ,
107.Nm bus_space_write_region_stream_4 ,
108.Nm bus_space_write_region_stream_8 ,
109.Nm bus_space_write_stream_1 ,
110.Nm bus_space_write_stream_2 ,
111.Nm bus_space_write_stream_4 ,
112.Nm bus_space_write_stream_8 ,
113.Nd bus space manipulation functions
114.Sh SYNOPSIS
115.Fd #include <machine/bus.h>
116.Ft int
117.Fn bus_space_map "bus_space_tag_t space" "bus_addr_t address" \
118"bus_size_t size" "int flags" "bus_space_handle_t *handlep"
119.Ft void
120.Fn bus_space_unmap "bus_space_tag_t space" "bus_space_handle_t handle" \
121"bus_size_t size"
122.Ft int
123.Fn bus_space_subregion "bus_space_tag_t space" "bus_space_handle_t handle" \
124"bus_size_t offset" "bus_size_t size" "bus_space_handle_t *nhandlep"
125.Ft int
126.Fo bus_space_alloc
127.Fa "bus_space_tag_t space" "bus_addr_t reg_start" "bus_addr_t reg_end"
128.Fa "bus_size_t size" "bus_size_t alignment" "bus_size_t boundary"
129.Fa "int flags" "bus_addr_t *addrp" "bus_space_handle_t *handlep"
130.Fc
131.Ft void
132.Fn bus_space_free "bus_space_tag_t space" "bus_space_handle_t handle" \
133"bus_size_t size"
134.Ft void *
135.Fn bus_space_vaddr "bus_space_tag_t space" "bus_space_handle_t handle"
136.Ft paddr_t
137.Fn bus_space_mmap "bus_space_tag_t space" "bus_addr_t addr" "off_t off" \
138"int prot" "int flags"
139.Ft int
140.Fn bus_space_peek_1 "bus_space_tag_t space" "bus_space_handle_t handle" \
141"bus_size_t offset" "u_int8_t *datap"
142.Ft int
143.Fn bus_space_peek_2 "bus_space_tag_t space" "bus_space_handle_t handle" \
144"bus_size_t offset" "u_int16_t *datap"
145.Ft int
146.Fn bus_space_peek_4 "bus_space_tag_t space" "bus_space_handle_t handle" \
147"bus_size_t offset" "u_int32_t *datap"
148.Ft int
149.Fn bus_space_peek_8 "bus_space_tag_t space" "bus_space_handle_t handle" \
150"bus_size_t offset" "u_int64_t *datap"
151.Ft int
152.Fn bus_space_poke_1 "bus_space_tag_t space" "bus_space_handle_t handle" \
153"bus_size_t offset" "u_int8_t data"
154.Ft int
155.Fn bus_space_poke_2 "bus_space_tag_t space" "bus_space_handle_t handle" \
156"bus_size_t offset" "u_int16_t data"
157.Ft int
158.Fn bus_space_poke_4 "bus_space_tag_t space" "bus_space_handle_t handle" \
159"bus_size_t offset" "u_int32_t data"
160.Ft int
161.Fn bus_space_poke_8 "bus_space_tag_t space" "bus_space_handle_t handle" \
162"bus_size_t offset" "u_int64_t data"
163.Ft u_int8_t
164.Fn bus_space_read_1 "bus_space_tag_t space" "bus_space_handle_t handle" \
165"bus_size_t offset"
166.Ft u_int16_t
167.Fn bus_space_read_2 "bus_space_tag_t space" "bus_space_handle_t handle" \
168"bus_size_t offset"
169.Ft u_int32_t
170.Fn bus_space_read_4 "bus_space_tag_t space" "bus_space_handle_t handle" \
171"bus_size_t offset"
172.Ft u_int64_t
173.Fn bus_space_read_8 "bus_space_tag_t space" "bus_space_handle_t handle" \
174"bus_size_t offset"
175.Ft void
176.Fn bus_space_write_1 "bus_space_tag_t space" "bus_space_handle_t handle" \
177"bus_size_t offset" "u_int8_t value"
178.Ft void
179.Fn bus_space_write_2 "bus_space_tag_t space" "bus_space_handle_t handle" \
180"bus_size_t offset" "u_int16_t value"
181.Ft void
182.Fn bus_space_write_4 "bus_space_tag_t space" "bus_space_handle_t handle" \
183"bus_size_t offset" "u_int32_t value"
184.Ft void
185.Fn bus_space_write_8 "bus_space_tag_t space" "bus_space_handle_t handle" \
186"bus_size_t offset" "u_int64_t value"
187.Ft void
188.Fn bus_space_barrier "bus_space_tag_t space" "bus_space_handle_t handle" \
189"bus_size_t offset" "bus_size_t length" "int flags"
190.Ft void
191.Fn bus_space_read_region_1 "bus_space_tag_t space" \
192"bus_space_handle_t handle" "bus_size_t offset" "u_int8_t *datap" \
193"bus_size_t count"
194.Ft void
195.Fn bus_space_read_region_2 "bus_space_tag_t space" \
196"bus_space_handle_t handle" "bus_size_t offset" "u_int16_t *datap" \
197"bus_size_t count"
198.Ft void
199.Fn bus_space_read_region_4 "bus_space_tag_t space" \
200"bus_space_handle_t handle" "bus_size_t offset" "u_int32_t *datap" \
201"bus_size_t count"
202.Ft void
203.Fn bus_space_read_region_8 "bus_space_tag_t space" \
204"bus_space_handle_t handle" "bus_size_t offset" "u_int64_t *datap" \
205"bus_size_t count"
206.Ft void
207.Fn bus_space_read_region_stream_1 "bus_space_tag_t space" \
208"bus_space_handle_t handle" "bus_size_t offset" "u_int8_t *datap" \
209"bus_size_t count"
210.Ft void
211.Fn bus_space_read_region_stream_2 "bus_space_tag_t space" \
212"bus_space_handle_t handle" "bus_size_t offset" "u_int16_t *datap" \
213"bus_size_t count"
214.Ft void
215.Fn bus_space_read_region_stream_4 "bus_space_tag_t space" \
216"bus_space_handle_t handle" "bus_size_t offset" "u_int32_t *datap" \
217"bus_size_t count"
218.Ft void
219.Fn bus_space_read_region_stream_8 "bus_space_tag_t space" \
220"bus_space_handle_t handle" "bus_size_t offset" "u_int64_t *datap" \
221"bus_size_t count"
222.Ft void
223.Fn bus_space_write_region_1 "bus_space_tag_t space" \
224"bus_space_handle_t handle" "bus_size_t offset" "const u_int8_t *datap" \
225"bus_size_t count"
226.Ft void
227.Fn bus_space_write_region_2 "bus_space_tag_t space" \
228"bus_space_handle_t handle" "bus_size_t offset" "const u_int16_t *datap" \
229"bus_size_t count"
230.Ft void
231.Fn bus_space_write_region_4 "bus_space_tag_t space" \
232"bus_space_handle_t handle" "bus_size_t offset" "const u_int32_t *datap" \
233"bus_size_t count"
234.Ft void
235.Fn bus_space_write_region_8 "bus_space_tag_t space" \
236"bus_space_handle_t handle" "bus_size_t offset" "const u_int64_t *datap" \
237"bus_size_t count"
238.Ft void
239.Fn bus_space_write_region_stream_1 "bus_space_tag_t space" \
240"bus_space_handle_t handle" "bus_size_t offset" "const u_int8_t *datap" \
241"bus_size_t count"
242.Ft void
243.Fn bus_space_write_region_stream_2 "bus_space_tag_t space" \
244"bus_space_handle_t handle" "bus_size_t offset" "const u_int16_t *datap" \
245"bus_size_t count"
246.Ft void
247.Fn bus_space_write_region_stream_4 "bus_space_tag_t space" \
248"bus_space_handle_t handle" "bus_size_t offset" "const u_int32_t *datap" \
249"bus_size_t count"
250.Ft void
251.Fn bus_space_write_region_stream_8 "bus_space_tag_t space" \
252"bus_space_handle_t handle" "bus_size_t offset" "const u_int64_t *datap" \
253"bus_size_t count"
254.Ft void
255.Fn bus_space_copy_region_1 "bus_space_tag_t space" \
256"bus_space_handle_t srchandle" "bus_size_t srcoffset" \
257"bus_space_handle_t dsthandle" "bus_size_t dstoffset" "bus_size_t count"
258.Ft void
259.Fn bus_space_copy_region_2 "bus_space_tag_t space" \
260"bus_space_handle_t srchandle" "bus_size_t srcoffset" \
261"bus_space_handle_t dsthandle" "bus_size_t dstoffset" "bus_size_t count"
262.Ft void
263.Fn bus_space_copy_region_4 "bus_space_tag_t space" \
264"bus_space_handle_t srchandle" "bus_size_t srcoffset" \
265"bus_space_handle_t dsthandle" "bus_size_t dstoffset" "bus_size_t count"
266.Ft void
267.Fn bus_space_copy_region_8 "bus_space_tag_t space" \
268"bus_space_handle_t srchandle" "bus_size_t srcoffset" \
269"bus_space_handle_t dsthandle" "bus_size_t dstoffset" "bus_size_t count"
270.Ft void
271.Fn bus_space_set_region_1 "bus_space_tag_t space" \
272"bus_space_handle_t handle" "bus_size_t offset" "u_int8_t value" \
273"bus_size_t count"
274.Ft void
275.Fn bus_space_set_region_2 "bus_space_tag_t space" \
276"bus_space_handle_t handle" "bus_size_t offset" "u_int16_t value" \
277"bus_size_t count"
278.Ft void
279.Fn bus_space_set_region_4 "bus_space_tag_t space" \
280"bus_space_handle_t handle" "bus_size_t offset" "u_int32_t value" \
281"bus_size_t count"
282.Ft void
283.Fn bus_space_set_region_8 "bus_space_tag_t space" \
284"bus_space_handle_t handle" "bus_size_t offset" "u_int64_t value" \
285"bus_size_t count"
286.Ft void
287.Fn bus_space_read_multi_1 "bus_space_tag_t space" \
288"bus_space_handle_t handle" "bus_size_t offset" "u_int8_t *datap" \
289"bus_size_t count"
290.Ft void
291.Fn bus_space_read_multi_2 "bus_space_tag_t space" \
292"bus_space_handle_t handle" "bus_size_t offset" "u_int16_t *datap" \
293"bus_size_t count"
294.Ft void
295.Fn bus_space_read_multi_4 "bus_space_tag_t space" \
296"bus_space_handle_t handle" "bus_size_t offset" "u_int32_t *datap" \
297"bus_size_t count"
298.Ft void
299.Fn bus_space_read_multi_8 "bus_space_tag_t space" \
300"bus_space_handle_t handle" "bus_size_t offset" "u_int64_t *datap" \
301"bus_size_t count"
302.Ft void
303.Fn bus_space_read_multi_stream_1 "bus_space_tag_t space" \
304"bus_space_handle_t handle" "bus_size_t offset" "u_int8_t *datap" \
305"bus_size_t count"
306.Ft void
307.Fn bus_space_read_multi_stream_2 "bus_space_tag_t space" \
308"bus_space_handle_t handle" "bus_size_t offset" "u_int16_t *datap" \
309"bus_size_t count"
310.Ft void
311.Fn bus_space_read_multi_stream_4 "bus_space_tag_t space" \
312"bus_space_handle_t handle" "bus_size_t offset" "u_int32_t *datap" \
313"bus_size_t count"
314.Ft void
315.Fn bus_space_read_multi_stream_8 "bus_space_tag_t space" \
316"bus_space_handle_t handle" "bus_size_t offset" "u_int64_t *datap" \
317"bus_size_t count"
318.Ft void
319.Fn bus_space_write_multi_1 "bus_space_tag_t space" \
320"bus_space_handle_t handle" "bus_size_t offset" "const u_int8_t *datap" \
321"bus_size_t count"
322.Ft void
323.Fn bus_space_write_multi_2 "bus_space_tag_t space" \
324"bus_space_handle_t handle" "bus_size_t offset" "const u_int16_t *datap" \
325"bus_size_t count"
326.Ft void
327.Fn bus_space_write_multi_4 "bus_space_tag_t space" \
328"bus_space_handle_t handle" "bus_size_t offset" "const u_int32_t *datap" \
329"bus_size_t count"
330.Ft void
331.Fn bus_space_write_multi_8 "bus_space_tag_t space" \
332"bus_space_handle_t handle" "bus_size_t offset" "const u_int64_t *datap" \
333"bus_size_t count"
334.Ft void
335.Fn bus_space_write_multi_stream_1 "bus_space_tag_t space" \
336"bus_space_handle_t handle" "bus_size_t offset" "const u_int8_t *datap" \
337"bus_size_t count"
338.Ft void
339.Fn bus_space_write_multi_stream_2 "bus_space_tag_t space" \
340"bus_space_handle_t handle" "bus_size_t offset" "const u_int16_t *datap" \
341"bus_size_t count"
342.Ft void
343.Fn bus_space_write_multi_stream_4 "bus_space_tag_t space" \
344"bus_space_handle_t handle" "bus_size_t offset" "const u_int32_t *datap" \
345"bus_size_t count"
346.Ft void
347.Fn bus_space_write_multi_stream_8 "bus_space_tag_t space" \
348"bus_space_handle_t handle" "bus_size_t offset" "const u_int64_t *datap" \
349"bus_size_t count"
350.Sh DESCRIPTION
351.Pp
352The
353.Nm
354functions exist to allow device drivers
355machine-independent access to bus memory and register areas.  All of the
356functions and types described in this document can be used by including
357the
358.Pa Aq machine/bus.h
359header file.
360.Pp
361Many common devices are used on multiple architectures, but are accessed
362differently on each because of architectural constraints.
363For instance, a device which is mapped in one system's I/O space may be
364mapped in memory space on a second system.  On a third system, architectural
365limitations might change the way registers need to be accessed (e.g.
366creating a non-linear register space).
367In some cases, a single
368driver may need to access the same type of device in multiple ways in a
369single system or architecture.  The goal of the
370.Nm
371functions is to allow a single driver source file to manipulate a set
372of devices on different system architectures, and to allow a single driver
373object file to manipulate a set of devices on multiple bus types on a
374single architecture.
375.Pp
376Not all busses have to implement all functions described in this
377document, though that is encouraged if the operations are logically
378supported by the bus.  Unimplemented functions should cause
379compile-time errors if possible.
380.Pp
381All of the interface definitions described in this document are shown as
382function prototypes and discussed as if they were required to be
383functions.  Implementations are encouraged to implement prototyped
384(type-checked) versions of these interfaces, but may implement them as
385macros if appropriate.  Machine-dependent types, variables, and functions
386should be marked clearly in
387.Pa Aq machine/bus.h
388to avoid confusion with the
389machine-independent types and functions, and, if possible, should be
390given names which make the machine-dependence clear.
391.Sh CONCEPTS AND GUIDELINES
392.Pp
393Bus spaces are described by bus space tags, which can be created only by
394machine-dependent code.  A given machine may have several different types
395of bus space (e.g. memory space and I/O space), and thus may provide
396multiple different bus space tags.
397Individual busses or devices on a machine may use more than one bus space
398tag.  For instance, ISA devices are
399given an ISA memory space tag and an ISA I/O space tag.  Architectures
400may have several different tags which represent the same type of
401space, for instance because of multiple different host bus interface
402chipsets.
403.Pp
404A range in bus space is described by a bus address and a bus size.  The
405bus address describes the start of the range in bus space.  The bus
406size describes the size of the range in bytes.  Busses which are not byte
407addressable may require use of bus space ranges with appropriately
408aligned addresses and properly rounded sizes.
409.Pp
410Access to regions of bus space is facilitated by use of bus space handles,
411which are usually created by mapping a specific range of a bus space.
412Handles may also be created by allocating
413and mapping a range of bus space, the actual location of which is picked
414by the implementation within bounds specified by the caller of the
415allocation function.
416.Pp
417All of the bus space access functions require one bus space tag
418argument, at least one handle argument, and at least one offset argument
419(a bus size).
420The bus space tag specifies the space, each handle specifies a region in
421the space, and each offset specifies the offset into the region of the
422actual location(s) to be accessed.  Offsets are given in bytes, though busses
423may impose alignment constraints.  The offset used to access data
424relative to a given handle must be such that all of the data being
425accessed is in the mapped region that the handle describes.  Trying to
426access data outside that region is an error.
427.Pp
428Because some architectures' memory systems use buffering to improve
429memory and device access performance, there is a mechanism which can be
430used to create
431.Dq barriers
432in the bus space read and write stream.  There
433are three types of barriers: read, write, and read/write.  All reads
434started to the region before a read barrier must complete before any reads
435after the read barrier are started.  (The analogous requirement is true for
436write barriers.)  Read/write barriers force all reads and writes started
437before the barrier to complete before any reads or writes after the
438barrier are started.  Correctly-written drivers will include all
439appropriate barriers, and assume only the read/write ordering imposed by
440the barrier operations.
441.Pp
442People trying to write portable drivers with the
443.Nm
444functions should
445try to make minimal assumptions about what the system allows.  In particular,
446they should expect that the system requires bus space addresses being
447accessed to be naturally aligned (i.e. base address of handle added to
448offset is a multiple of the access size), and that the system does
449alignment checking on pointers (i.e. pointer to objects being read and
450written must point to properly-aligned data).
451.Pp
452The descriptions of the
453.Nm
454functions given below all assume that
455they are called with proper arguments.  If called with invalid arguments
456or arguments that are out of range (e.g. trying to access data outside of
457the region mapped when a given handle was created), undefined behaviour
458results.  In that case, they may cause the
459system to halt, either intentionally (via panic) or unintentionally (by
460causing a fatal trap of by some other means) or may cause improper
461operation which is not immediately fatal.  Functions which return
462void or which return data read from bus space (i.e., functions which
463don't obviously return an error code) do not fail.  They could only fail
464if given invalid arguments, and in that case their behaviour is undefined.
465Functions which take a count of bytes have undefined results if the specified
466.Fa count
467is zero.
468.Pp
469.Sh TYPES
470Several types are defined in
471.Pa Aq machine/bus.h
472to facilitate use of the
473.Nm
474functions by drivers.
475.Pp
476.Bl -ohang -compact
477.It Fa bus_addr_t
478.Pp
479The
480.Fa bus_addr_t
481type is used to describe bus addresses.  It must be an
482unsigned integral type
483capable of holding the largest bus address usable by the architecture.  This
484type is primarily used when mapping and unmapping bus space.
485.Pp
486.It Fa bus_size_t
487.Pp
488The
489.Fa bus_size_t
490type is used to describe sizes of ranges in bus space.  It must be an
491unsigned integral type capable of holding the size of the largest bus
492address range usable on the architecture.  This type is used by virtually all
493of the
494.Nm
495functions, describing sizes when mapping regions and
496offsets into regions when performing space access operations.
497.Pp
498.It Fa bus_space_tag_t
499.Pp
500The
501.Fa bus_space_tag_t
502type is used to describe a particular bus space on a machine.  Its
503contents are machine-dependent and should be considered opaque by
504machine-independent code.  This type is used by all
505.Nm
506functions to name the space on which they're operating.
507.Pp
508.It Fa bus_space_handle_t
509.Pp
510The
511.Fa bus_space_handle_t
512type is used to describe a mapping of a range of bus space.  Its
513contents are machine-dependent and should be considered opaque by
514machine-independent code.  This type is used when performing bus space
515access operations.
516.El
517.Pp
518.Sh MAPPING AND UNMAPPING BUS SPACE
519.Pp
520Bus space must be mapped before it can be used, and should be
521unmapped when it is no longer needed.  The
522.Fn bus_space_map
523and
524.Fn bus_space_unmap
525functions provide these capabilities.
526.Pp
527Some drivers need to be able to pass a subregion of already-mapped bus
528space to another driver or module within a driver.  The
529.Fn bus_space_subregion
530function allows such subregions to be created.
531.Pp
532.Bl -ohang -compact
533.It Fn bus_space_map "space" "address" "size" "flags" "handlep"
534.Pp
535The
536.Fn bus_space_map
537function maps the region of bus space named by the
538.Fa space ,
539.Fa address ,
540and
541.Fa size
542arguments.  If successful, it returns zero
543and fills in the bus space handle pointed to by
544.Fa handlep
545with the handle
546that can be used to access the mapped region.  If unsuccessful,
547it will return non-zero and leave the bus space handle pointed
548to by
549.Fa handlep
550in an undefined state.
551.Pp
552The
553.Fa flags
554argument controls how the space is to be mapped.  Supported flags include:
555.Bl -tag -width BUS_SPACE_MAP_CACHEABLE -offset indent
556.It Dv BUS_SPACE_MAP_CACHEABLE
557Try to map the space so that accesses can be cached
558by the system cache. If this flag is not specified, the
559implementation should map the space so that it will not be cached.
560This mapping method will only be useful in very rare occasions.
561.Pp
562This flag must have a value of 1 on all implementations for backward
563compatibility.
564.It Dv BUS_SPACE_MAP_PREFETCHABLE
565Try to map the space so that accesses can be prefetched by the system,
566and writes can be buffered.
567This means, accesses should be side effect free (idempotent). The
568.Fn bus_space_barrier
569methods will flush the write buffer or force actual read accesses.
570If this flag is not specified, the
571implementation should map the space so that it will not be prefetched
572or delayed.
573.It Dv BUS_SPACE_MAP_LINEAR
574Try to map the space so that its contents can be accessed linearly via
575normal memory access methods (e.g. pointer dereferencing and structure
576accesses). The
577.Fn bus_space_vaddr
578method can be used to obtain the kernel virtual address of the mapped range.
579This is useful when software wants to do direct access to a memory
580device, e.g. a frame buffer.  If this flag is specified and linear
581mapping is not possible, the
582.Fn bus_space_map
583call should fail.  If this
584flag is not specified, the system may map the space in whatever way is
585most convenient.
586Use of this mapping method is not encouraged for normal device access;
587where linear access is not essential, use of the
588.Fn bus_space_read/write
589methods is strongly recommended.
590.El
591.Pp
592Not all combinations of flags make sense or are supported with all
593spaces.  For instance,
594.Dv BUS_SPACE_MAP_CACHEABLE
595may be meaningless when
596used on many systems' I/O port spaces, and on some systems
597.Dv BUS_SPACE_MAP_LINEAR
598without
599.Dv BUS_SPACE_MAP_PREFETCHABLE
600may never work.
601When the system hardware or firmware provides hints as to how spaces should be
602mapped (e.g. the PCI memory mapping registers' "prefetchable" bit), those
603hints should be followed for maximum compatibility.  On some systems,
604requesting a mapping that cannot be satisfied (e.g. requesting a
605non-prefetchable mapping when the system can only provide a prefetchable one)
606will cause the request to fail.
607.Pp
608Some implementations may keep track of use of bus space for some or all
609bus spaces and refuse to allow duplicate allocations.  This is encouraged
610for bus spaces which have no notion of slot-specific space addressing,
611such as ISA and VME, and for spaces which coexist with those spaces
612(e.g. EISA and PCI memory and I/O spaces co-existing with ISA memory and
613I/O spaces).
614.Pp
615Mapped regions may contain areas for which no there is no device on the
616bus.  If space in those areas is accessed, the results are
617bus-dependent.
618.Pp
619.It Fn bus_space_unmap "space" "handle" "size"
620.Pp
621The
622.Fn bus_space_unmap
623function unmaps a region of bus space mapped with
624.Fn bus_space_map .
625When unmapping a region, the
626.Fa size
627specified should be
628the same as the size given to
629.Fn bus_space_map
630when mapping that region.
631.Pp
632After
633.Fn bus_space_unmap
634is called on a handle, that handle is no longer
635valid.  (If copies were made of the handle they are no longer valid,
636either.)
637.Pp
638This function will never fail.  If it would fail (e.g. because of an
639argument error), that indicates a software bug which should cause a
640panic.  In that case,
641.Fn bus_space_unmap
642will never return.
643.Pp
644.It Fn bus_space_subregion "space" "handle" "offset" "size" "nhandlep"
645.Pp
646The
647.Fn bus_space_subregion
648function is a convenience function which makes a
649new handle to some subregion of an already-mapped region of bus space.
650The subregion described by the new handle starts at byte offset
651.Fa offset
652into the region described by
653.Fa handle ,
654with the size given by
655.Fa size ,
656and must be wholly contained within the original region.
657.Pp
658If successful,
659.Fn bus_space_subregion
660returns zero and fills in the bus
661space handle pointed to by
662.Fa nhandlep .
663If unsuccessful, it returns non-zero and leaves the bus space handle
664pointed to by
665.Fa nhandlep
666in an
667undefined state.  In either case, the handle described by
668.Fa handle
669remains valid and is unmodified.
670.Pp
671When done with a handle created by
672.Fn bus_space_subregion ,
673the handle should
674be thrown away.  Under no circumstances should
675.Fn bus_space_unmap
676be used on the handle.  Doing so may confuse any resource management
677being done on the space, and will result in undefined behaviour.  When
678.Fn bus_space_unmap
679or
680.Fn bus_space_free
681is called on a handle, all subregions of that handle become invalid.
682.Pp
683.It Fn bus_space_vaddr "tag" "handle"
684.Pp
685This method returns the kernel virtual address of a mapped bus space if and
686only if it was mapped with the
687.Dv BUS_SPACE_MAP_LINEAR
688flag. The range can be accessed by normal (volatile) pointer dereferences.
689If mapped with the
690.Dv BUS_SPACE_MAP_PREFETCHABLE
691flag, the
692.Fn bus_space_barrier
693method must be used to force a particular access order.
694.Pp
695.It Fn bus_space_mmap "tag" "addr" "off" "prot" "flags"
696.Pp
697This method is used to provide support for memory mapping bus space
698into user applications.  If an address space is addressable via
699volatile pointer dereferences,
700.Fn bus_space_mmap
701will return the physical address (possibly encoded as a machine-dependent
702cookie) of the bus space indicated by
703.Fa addr
704and
705.Fa off .
706.Fa addr
707is the base address of the device or device region, and
708.Fa off
709is the offset into that region that is being requested.
710If the request is made with
711.Dv BUS_SPACE_MAP_LINEAR
712as a flag, then a linear region must be returned to the caller.
713If the region cannot be mapped (either the address does not exist,
714or the constraints can not be met),
715.Fn bus_space_mmap
716returns
717.Dv -1
718to indicate failure.
719.Pp
720Note that it is not necessary that the region being requested by a
721.Fn bus_space_mmap
722call be mapped into a
723.Fa bus_space_handle_t .
724.Pp
725.Fn bus_space_mmap
726is called once per
727.Dv PAGE_SIZE
728page in the range.  The
729.Fa prot
730argument indicates the memory protection requested by the user application
731for the range.
732.El
733.Pp
734.Sh ALLOCATING AND FREEING BUS SPACE
735.Pp
736Some devices require or allow bus space to be allocated by the operating
737system for device use.  When the devices no longer need the space, the
738operating system should free it for use by other devices.  The
739.Fn bus_space_alloc
740and
741.Fn bus_space_free
742functions provide these capabilities.
743.Pp
744.Bl -ohang -compact
745.It Xo
746.Fo bus_space_alloc
747.Fa "space" "reg_start" "reg_end" "size"
748.Fa "alignment" "boundary" "flags" "addrp" "handlep"
749.Fc
750.Xc
751.Pp
752The
753.Fn bus_space_alloc
754function allocates and maps a region of bus space with the size given by
755.Fa size ,
756corresponding to the given constraints.  If successful, it returns
757zero, fills in the bus address pointed to by
758.Fa addrp
759with the bus space address of the allocated region, and fills in
760the bus space handle pointed to by
761.Fa handlep
762with the handle that can be used to access that region.
763If unsuccessful, it returns non-zero and leaves the bus address pointed to by
764.Fa addrp
765and the bus space handle pointed to by
766.Fa handlep
767in an undefined state.
768.Pp
769Constraints on the allocation are given by the
770.Fa reg_start ,
771.Fa reg_end ,
772.Fa alignment ,
773and
774.Fa boundary
775parameters.  The allocated region will start at or after
776.Fa reg_start
777and end before or at
778.Fa reg_end .
779The
780.Fa alignment
781constraint must be a power of two, and the allocated region will start at
782an address that is an even multiple of that power of two.  The
783.Fa boundary
784constraint, if non-zero, ensures that the region is allocated so that
785.Fa "first address in region"
786/
787.Fa boundary
788has the same value as
789.Fa "last address in region"
790/
791.Fa boundary .
792If the constraints cannot be met,
793.Fn bus_space_alloc
794will fail.  It is an error to specify a set of
795constraints that can never be met
796.Po
797for example,
798.Fa size
799greater than
800.Fa boundary
801.Pc .
802.Pp
803The
804.Fa flags
805parameter is the same as the like-named parameter to
806.Fa bus_space_map ,
807the same flag values should be used, and they have the
808same meanings.
809.Pp
810Handles created by
811.Fn bus_space_alloc
812should only be freed with
813.Fn bus_space_free .
814Trying to use
815.Fn bus_space_unmap
816on them causes undefined behaviour.  The
817.Fn bus_space_subregion
818function can be used on
819handles created by
820.Fn bus_space_alloc .
821.Pp
822.It Fn bus_space_free "space" "handle" "size"
823.Pp
824The
825.Fn bus_space_free
826function unmaps and frees a region of bus space mapped
827and allocated with
828.Fn bus_space_alloc .
829When unmapping a region, the
830.Fa size
831specified should be the same as the size given to
832.Fn bus_space_alloc
833when allocating the region.
834.Pp
835After
836.Fn bus_space_free
837is called on a handle, that handle is no longer valid.  (If copies were
838made of the handle, they are no longer valid, either.)
839.Pp
840This function will never fail.  If it would fail (e.g. because of an
841argument error), that indicates a software bug which should cause a
842panic.  In that case,
843.Fn bus_space_free
844will never return.
845.El
846.Pp
847.Sh READING AND WRITING SINGLE DATA ITEMS
848.Pp
849The simplest way to access bus space is to read or write a single data
850item.  The
851.Fn bus_space_read_N
852and
853.Fn bus_space_write_N
854families of functions provide
855the ability to read and write 1, 2, 4, and 8 byte data items on busses
856which support those access sizes.
857.Pp
858.Bl -ohang -compact
859.It Fn bus_space_read_1 "space" "handle" "offset"
860.It Fn bus_space_read_2 "space" "handle" "offset"
861.It Fn bus_space_read_4 "space" "handle" "offset"
862.It Fn bus_space_read_8 "space" "handle" "offset"
863.Pp
864The
865.Fn bus_space_read_N
866family of functions reads a 1, 2, 4, or 8 byte data item from
867the offset specified by
868.Fa offset
869into the region specified by
870.Fa handle
871of the bus space specified by
872.Fa space .
873The location being read must lie within the bus space region specified by
874.Fa handle .
875.Pp
876For portability, the starting address of the region specified by
877.Fa handle
878plus the offset should be a multiple of the size of data item being read.
879On some systems, not obeying this requirement may cause incorrect data to
880be read, on others it may cause a system crash.
881.Pp
882Read operations done by the
883.Fn bus_space_read_N
884functions may be executed out
885of order with respect to other pending read and write operations unless
886order is enforced by use of the
887.Fn bus_space_barrier
888function.
889.Pp
890These functions will never fail.  If they would fail (e.g. because of an
891argument error), that indicates a software bug which should cause a
892panic.  In that case, they will never return.
893.Pp
894.It Fn bus_space_write_1 "space" "handle" "offset" "value"
895.It Fn bus_space_write_2 "space" "handle" "offset" "value"
896.It Fn bus_space_write_4 "space" "handle" "offset" "value"
897.It Fn bus_space_write_8 "space" "handle" "offset" "value"
898.Pp
899The
900.Fn bus_space_write_N
901family of functions writes a 1, 2, 4, or 8 byte data item to the offset
902specified by
903.Fa offset
904into the region specified by
905.Fa handle
906of the bus space specified by
907.Fa space .
908The location being written must lie within
909the bus space region specified by
910.Fa handle .
911.Pp
912For portability, the starting address of the region specified by
913.Fa handle
914plus the offset should be a multiple of the size of data item being
915written.  On some systems, not obeying this requirement may cause
916incorrect data to be written, on others it may cause a system crash.
917.Pp
918Write operations done by the
919.Fn bus_space_write_N
920functions may be executed
921out of order with respect to other pending read and write operations
922unless order is enforced by use of the
923.Fn bus_space_barrier
924function.
925.Pp
926These functions will never fail.  If they would fail (e.g. because of an
927argument error), that indicates a software bug which should cause a
928panic.  In that case, they will never return.
929.El
930.Pp
931
932
933.Sh PROBING BUS SPACE FOR HARDWARE WHICH MAY NOT RESPOND
934.Pp
935One problem with the
936.Fn bus_space_read_N
937and
938.Fn bus_space_write_N
939family of functions is that they provide no protection against
940exceptions which can occur when no physical hardware or
941device responds to the read or write cycles. In such a situation,
942the system typically would panic due to a kernel-mode bus error. The
943.Fn bus_space_peek_N
944and
945.Fn bus_space_poke_N
946family of functions provide a mechanism to handle these exceptions
947gracefully without the risk of crashing the system.
948.Pp
949As with
950.Fn bus_space_read_N
951and
952.Fn bus_space_write_N ,
953the peek and poke functions provide the ability to read and
954write 1, 2, 4, and 8 byte data items on busses which support those
955access sizes. All of the constraints specified in the descriptions of
956the
957.Fn bus_space_read_N
958and
959.Fn bus_space_write_N
960functions also apply to
961.Fn bus_space_peek_N
962and
963.Fn bus_space_poke_N .
964.Pp
965In addition, explicit calls to the
966.Fn bus_space_barrier
967function are not required as the implementation will ensure all
968pending operations complete before the peek or poke operation starts.
969The implementation will also ensure that the peek or poke operations
970complete before returning.
971.Pp
972The return value indicates the outcome of the peek or poke operation.
973A return value of zero implies that a hardware device is
974responding to the operation at the specified offset in the bus space.
975A non-zero return value indicates that the kernel intercepted a
976hardware exception (e.g. bus error) when the peek or poke operation
977was attempted.
978Note that some busses are incapable of generating exceptions when
979non-existent hardware is accessed. In such cases, these functions
980will always return zero and the value of the data read by
981.Fn bus_space_peek_N
982will be unspecified.
983.Pp
984Finally, it should be noted that at this time the
985.Fn bus_space_peek_N
986and
987.Fn bus_space_poke_N
988functions are not re-entrant and should not, therefore, be used
989from within an interrupt service routine.
990This constraint may be removed at some point in the future.
991.Pp
992.Bl -ohang -compact
993.It Fn bus_space_peek_1 "space" "handle" "offset" "datap"
994.It Fn bus_space_peek_2 "space" "handle" "offset" "datap"
995.It Fn bus_space_peek_4 "space" "handle" "offset" "datap"
996.It Fn bus_space_peek_8 "space" "handle" "offset" "datap"
997.Pp
998The
999.Fn bus_space_peek_N
1000family of functions cautiously read a 1, 2, 4, or 8 byte data item from
1001the offset specified by
1002.Fa offset
1003in the region specified by
1004.Fa handle
1005of the bus space specified by
1006.Fa space .
1007The data item read is stored in the location pointed to by
1008.Fa datap .
1009It is permissible for
1010.Fa datap
1011to be NULL, in which case the data item will be discarded after being read.
1012.Pp
1013.It Fn bus_space_poke_1 "space" "handle" "offset" "value"
1014.It Fn bus_space_poke_2 "space" "handle" "offset" "value"
1015.It Fn bus_space_poke_4 "space" "handle" "offset" "value"
1016.It Fn bus_space_poke_8 "space" "handle" "offset" "value"
1017.Pp
1018The
1019.Fn bus_space_poke_N
1020family of functions cautiously write a 1, 2, 4, or 8 byte data item
1021specified by
1022.Fa value
1023to the offset specified by
1024.Fa offset
1025in the region specified by
1026.Fa handle
1027of the bus space specified by
1028.Fa space .
1029.El
1030.Pp
1031.Sh BARRIERS
1032.Pp
1033In order to allow high-performance buffering implementations to avoid bus
1034activity on every operation, read and write ordering should be specified
1035explicitly by drivers when necessary.  The
1036.Fn bus_space_barrier
1037function provides that ability.
1038.Pp
1039.Bl -ohang -compact
1040.It Fn bus_space_barrier "space" "handle" "offset" "length" "flags"
1041.Pp
1042The
1043.Fn bus_space_barrier
1044function enforces ordering of bus space read and write operations
1045for the specified subregion (described by the
1046.Fa offset
1047and
1048.Fa length
1049parameters) of the region named by
1050.Fa handle
1051in the space named by
1052.Fa space .
1053.Pp
1054The
1055.Fa flags
1056argument controls what types of operations are to be ordered.
1057Supported flags are:
1058.Bl -tag -width BUS_SPACE_BARRIER_WRITE -offset indent
1059.It Dv BUS_SPACE_BARRIER_READ
1060Synchronize read operations.
1061.It Dv BUS_SPACE_BARRIER_WRITE
1062Synchronize write operations.
1063.El
1064.Pp
1065Those flags can be combined (or-ed together) to enforce ordering on both
1066read and write operations.
1067.Pp
1068All of the specified type(s) of operation which are done to the region
1069before the barrier operation are guaranteed to complete before any of the
1070specified type(s) of operation done after the barrier.
1071.Pp
1072Example: Consider a hypothetical device with two single-byte ports, one
1073write-only input port (at offset 0) and a read-only output port (at
1074offset 1).  Operation of the device is as follows: data bytes are written
1075to the input port, and are placed by the device on a stack, the top of
1076which is read by reading from the output port.  The sequence to correctly
1077write two data bytes to the device then read those two data bytes back
1078would be:
1079.Pp
1080.Bd -literal
1081/*
1082 * t and h are the tag and handle for the mapped device's
1083 * space.
1084 */
1085bus_space_write_1(t, h, 0, data0);
1086bus_space_barrier(t, h, 0, 1, BUS_SPACE_BARRIER_WRITE);  /* 1 */
1087bus_space_write_1(t, h, 0, data1);
1088bus_space_barrier(t, h, 0, 2,
1089    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);     /* 2 */
1090ndata1 = bus_space_read_1(t, h, 1);
1091bus_space_barrier(t, h, 1, 1, BUS_SPACE_BARRIER_READ);   /* 3 */
1092ndata0 = bus_space_read_1(t, h, 1);
1093/* data0 == ndata0, data1 == ndata1 */
1094.Ed
1095.Pp
1096The first barrier makes sure that the first write finishes before the
1097second write is issued, so that two writes to the input port are done
1098in order and are not collapsed into a single write.  This ensures that
1099the data bytes are written to the device correctly and in order.
1100.Pp
1101The second barrier makes sure that the writes to the output port finish
1102before any of the reads to the input port are issued, thereby making sure
1103that all of the writes are finished before data is read.  This ensures
1104that the first byte read from the device really is the last one that was
1105written.
1106.Pp
1107The third barrier makes sure that the first read finishes before the
1108second read is issued, ensuring that data is read correctly and in order.
1109.Pp
1110The barriers in the example above are specified to cover the absolute
1111minimum number of bus space locations.  It is correct (and often
1112easier) to make barrier operations cover the device's whole range of bus
1113space, that is, to specify an offset of zero and the size of the
1114whole region.
1115.El
1116.Pp
1117.Sh REGION OPERATIONS
1118.Pp
1119Some devices use buffers which are mapped as regions in bus space.
1120Often, drivers want to copy the contents of those buffers to or from
1121memory, e.g. into mbufs which can be passed to higher levels of the
1122system or from mbufs to be output to a network.  In order to allow
1123drivers to do this as efficiently as possible, the
1124.Fn bus_space_read_region_N
1125and
1126.Fn bus_space_write_region_N
1127families of functions are provided.
1128.Pp
1129Drivers occasionally need to copy one region of a bus space to another,
1130or to set all locations in a region of bus space to contain a single
1131value.  The
1132.Fn bus_space_copy_region_N
1133family of functions and the
1134.Fn bus_space_set_region_N
1135family of functions allow drivers to perform these operations.
1136.Pp
1137.Bl -ohang -compact
1138.It Fn bus_space_read_region_1 "space" "handle" "offset" "datap" "count"
1139.It Fn bus_space_read_region_2 "space" "handle" "offset" "datap" "count"
1140.It Fn bus_space_read_region_4 "space" "handle" "offset" "datap" "count"
1141.It Fn bus_space_read_region_8 "space" "handle" "offset" "datap" "count"
1142.Pp
1143The
1144.Fn bus_space_read_region_N
1145family of functions reads
1146.Fa count
11471, 2, 4, or 8 byte data items from bus space
1148starting at byte offset
1149.Fa offset
1150in the region specified by
1151.Fa handle
1152of the bus space specified by
1153.Fa space
1154and writes them into the array specified by
1155.Fa datap .
1156Each successive data item is read from an offset
11571, 2, 4, or 8 bytes after the previous data item (depending on which
1158function is used).  All locations being read must lie within the bus
1159space region specified by
1160.Fa handle .
1161.Pp
1162For portability, the starting address of the region specified by
1163.Fa handle
1164plus the offset should be a multiple of the size of data items being
1165read and the data array pointer should be properly aligned.  On some
1166systems, not obeying these requirements may cause incorrect data to be
1167read, on others it may cause a system crash.
1168.Pp
1169Read operations done by the
1170.Fn bus_space_read_region_N
1171functions may be executed in any order.  They may also be executed out
1172of order with respect to other pending read and write operations unless
1173order is enforced by use of the
1174.Fn bus_space_barrier
1175function.  There is no way to insert barriers between reads of
1176individual bus space locations executed by the
1177.Fn bus_space_read_region_N
1178functions.
1179.Pp
1180These functions will never fail.  If they would fail (e.g. because of an
1181argument error), that indicates a software bug which should cause a
1182panic.  In that case, they will never return.
1183.Pp
1184.It Fn bus_space_write_region_1 "space" "handle" "offset" "datap" "count"
1185.It Fn bus_space_write_region_2 "space" "handle" "offset" "datap" "count"
1186.It Fn bus_space_write_region_4 "space" "handle" "offset" "datap" "count"
1187.It Fn bus_space_write_region_8 "space" "handle" "offset" "datap" "count"
1188.Pp
1189The
1190.Fn bus_space_write_region_N
1191family of functions reads
1192.Fa count
11931, 2, 4, or 8 byte data items from the array
1194specified by
1195.Fa datap
1196and writes them to bus space starting at byte offset
1197.Fa offset
1198in the region specified by
1199.Fa handle
1200of the bus space specified
1201by
1202.Fa space .
1203Each successive data item is written to an offset 1, 2, 4,
1204or 8 bytes after the previous data item (depending on which function is
1205used).  All locations being written must lie within the bus space region
1206specified by
1207.Fa handle .
1208.Pp
1209For portability, the starting address of the region specified by
1210.Fa handle
1211plus the offset should be a multiple of the size of data items being
1212written and the data array pointer should be properly aligned.  On some
1213systems, not obeying these requirements may cause incorrect data to be
1214written, on others it may cause a system crash.
1215.Pp
1216Write operations done by the
1217.Fn bus_space_write_region_N
1218functions may be
1219executed in any order.  They may also be executed out of order with
1220respect to other pending read and write operations unless order is
1221enforced by use of the
1222.Fn bus_space_barrier
1223function.  There is no way to insert barriers between writes of
1224individual bus space locations executed by the
1225.Fn bus_space_write_region_N
1226functions.
1227.Pp
1228These functions will never fail.  If they would fail (e.g. because of an
1229argument error), that indicates a software bug which should cause a
1230panic.  In that case, they will never return.
1231.Pp
1232.It Fn bus_space_copy_region_1 "space" "srchandle" "srcoffset" "dsthandle" \
1233"dstoffset" "count"
1234.It Fn bus_space_copy_region_2 "space" "srchandle" "srcoffset" "dsthandle" \
1235"dstoffset" "count"
1236.It Fn bus_space_copy_region_4 "space" "srchandle" "srcoffset" "dsthandle" \
1237"dstoffset" "count"
1238.It Fn bus_space_copy_region_8 "space" "srchandle" "srcoffset" "dsthandle" \
1239"dstoffset" "count"
1240.Pp
1241The
1242.Fn bus_space_copy_region_N
1243family of functions copies
1244.Fa count
12451, 2, 4, or 8 byte data items in bus space
1246from the area starting at byte offset
1247.Fa srcoffset
1248in the region specified by
1249.Fa srchandle
1250of the bus space specified by
1251.Fa space
1252to the area starting at byte offset
1253.Fa dstoffset
1254in the region specified by
1255.Fa dsthandle
1256in the same bus space.  Each successive data item read or written has
1257an offset 1, 2, 4, or 8 bytes after the previous data item (depending
1258on which function is used).  All locations being read and written must
1259lie within the bus space region specified by their respective handles.
1260.Pp
1261For portability, the starting addresses of the regions specified by
1262each handle plus its respective offset should be a multiple of the size
1263of data items being copied.  On some systems, not obeying this
1264requirement may cause incorrect data to be copied, on others it may cause
1265a system crash.
1266.Pp
1267Read and write operations done by the
1268.Fn bus_space_copy_region_N
1269functions may be executed in any order.  They may also be executed out
1270of order with respect to other pending read and write operations unless
1271order is enforced by use of the
1272.Fn bus_space_barrier function .
1273There is no way to insert barriers between reads or writes of
1274individual bus space locations executed by the
1275.Fn bus_space_copy_region_N
1276functions.
1277.Pp
1278Overlapping copies between different subregions of a single region
1279of bus space are handled correctly by the
1280.Fn bus_space_copy_region_N
1281functions.
1282.Pp
1283These functions will never fail.  If they would fail (e.g. because of an
1284argument error), that indicates a software bug which should cause a
1285panic.  In that case, they will never return.
1286.Pp
1287.It Fn bus_space_set_region_1 "space" "handle" "offset" "value" "count"
1288.It Fn bus_space_set_region_2 "space" "handle" "offset" "value" "count"
1289.It Fn bus_space_set_region_4 "space" "handle" "offset" "value" "count"
1290.It Fn bus_space_set_region_8 "space" "handle" "offset" "value" "count"
1291.Pp
1292The
1293.Fn bus_space_set_region_N
1294family of functions writes the given
1295.Fa value
1296to
1297.Fa count
12981, 2, 4, or 8 byte
1299data items in bus space starting at byte offset
1300.Fa offset
1301in the region specified by
1302.Fa handle
1303of the bus space specified by
1304.Fa space .
1305Each successive data item has an offset 1, 2, 4, or 8 bytes after the
1306previous data item (depending on which function is used).  All
1307locations being written must lie within the bus space region specified
1308by
1309.Fa handle .
1310.Pp
1311For portability, the starting address of the region specified by
1312.Fa handle
1313plus the offset should be a multiple of the size of data items being
1314written.  On some systems, not obeying this requirement may cause
1315incorrect data to be written, on others it may cause a system crash.
1316.Pp
1317Write operations done by the
1318.Fn bus_space_set_region_N
1319functions may be
1320executed in any order.  They may also be executed out of order with
1321respect to other pending read and write operations unless order is
1322enforced by use of the
1323.Fn bus_space_barrier
1324function.  There is no way to insert barriers between writes of
1325individual bus space locations executed by the
1326.Fn bus_space_set_region_N
1327functions.
1328.Pp
1329These functions will never fail.  If they would fail (e.g. because of an
1330argument error), that indicates a software bug which should cause a
1331panic.  In that case, they will never return.
1332.El
1333.Pp
1334.Sh READING AND WRITING A SINGLE LOCATION MULTIPLE TIMES
1335.Pp
1336Some devices implement single locations in bus space which are to be read
1337or written multiple times to communicate data, e.g. some ethernet
1338devices' packet buffer FIFOs.  In order to allow drivers to manipulate
1339these types of devices as efficiently as possible, the
1340.Fn bus_space_read_multi_N
1341and
1342.Fn bus_space_write_multi_N
1343families of functions are provided.
1344.Pp
1345.Bl -ohang -compact
1346.It Fn bus_space_read_multi_1 "space" "handle" "offset" "datap" "count"
1347.It Fn bus_space_read_multi_2 "space" "handle" "offset" "datap" "count"
1348.It Fn bus_space_read_multi_4 "space" "handle" "offset" "datap" "count"
1349.It Fn bus_space_read_multi_8 "space" "handle" "offset" "datap" "count"
1350.Pp
1351The
1352.Fn bus_space_read_multi_N
1353family of functions reads
1354.Fa count
13551, 2, 4, or 8 byte data items from bus space
1356at byte offset
1357.Fa offset
1358in the region specified by
1359.Fa handle
1360of the bus space specified by
1361.Fa space
1362and writes them into the array specified by
1363.Fa datap .
1364Each successive data item is read from the same location in bus
1365space.  The location being read must lie within the bus space region
1366specified by
1367.Fa handle .
1368.Pp
1369For portability, the starting address of the region specified by
1370.Fa handle
1371plus the offset should be a multiple of the size of data items being
1372read and the data array pointer should be properly aligned.  On some
1373systems, not obeying these requirements may cause incorrect data to be
1374read, on others it may cause a system crash.
1375.Pp
1376Read operations done by the
1377.Fn bus_space_read_multi_N
1378functions may be
1379executed out of order with respect to other pending read and write
1380operations unless order is enforced by use of the
1381.Fn bus_space_barrier
1382function.  Because the
1383.Fn bus_space_read_multi_N
1384functions read the same bus space location multiple times, they
1385place an implicit read barrier between each successive read of that bus
1386space location.
1387.Pp
1388These functions will never fail.  If they would fail (e.g. because of an
1389argument error), that indicates a software bug which should cause a
1390panic.  In that case, they will never return.
1391.Pp
1392.It Fn bus_space_write_multi_1 "space" "handle" "offset" "datap" "count"
1393.It Fn bus_space_write_multi_2 "space" "handle" "offset" "datap" "count"
1394.It Fn bus_space_write_multi_4 "space" "handle" "offset" "datap" "count"
1395.It Fn bus_space_write_multi_8 "space" "handle" "offset" "datap" "count"
1396.Pp
1397The
1398.Fn bus_space_write_multi_N
1399family of functions reads
1400.Fa count
14011, 2, 4, or 8 byte data items from the array
1402specified by
1403.Fa datap
1404and writes them into bus space at byte offset
1405.Fa offset
1406in the region specified by
1407.Fa handle
1408of the bus space specified by
1409.Fa space .
1410Each successive data item is written to the same location in
1411bus space.  The location being written must lie within the bus space
1412region specified by
1413.Fa handle .
1414.Pp
1415For portability, the starting address of the region specified by
1416.Fa handle
1417plus the offset should be a multiple of the size of data items being
1418written and the data array pointer should be properly aligned.  On some
1419systems, not obeying these requirements may cause incorrect data to be
1420written, on others it may cause a system crash.
1421.Pp
1422Write operations done by the
1423.Fn bus_space_write_multi_N
1424functions may be executed out of order with respect to other pending
1425read and write operations unless order is enforced by use of the
1426.Fn bus_space_barrier
1427function.  Because the
1428.Fn bus_space_write_multi_N
1429functions write the same bus space location multiple times, they
1430place an implicit write barrier between each successive write of that
1431bus space location.
1432.Pp
1433These functions will never fail.  If they would fail (e.g. because of an
1434argument error), that indicates a software bug which should cause a
1435panic.  In that case, they will never return.
1436.El
1437.Pp
1438.Sh STREAM FUNCTIONS
1439.Pp
1440Most of the
1441.Nm
1442functions imply a host byte-order and a bus byte-order and take care of
1443any translation for the caller.  In some cases, however, hardware may
1444map a FIFO or some other memory region for which the caller may want to
1445use multi-word, yet untranslated access.  Access to these types of memory
1446regions should be with the
1447.Fn bus_space_*_stream_N
1448functions.
1449.Pp
1450.Bl -ohang -compact
1451.It Fn bus_space_read_stream_1 "space" "handle" "offset"
1452.It Fn bus_space_read_stream_2 "space" "handle" "offset"
1453.It Fn bus_space_read_stream_4 "space" "handle" "offset"
1454.It Fn bus_space_read_stream_8 "space" "handle" "offset"
1455.It Fn bus_space_read_multi_stream_1 "space" "handle" "offset" "datap" "count"
1456.It Fn bus_space_read_multi_stream_2 "space" "handle" "offset" "datap" "count"
1457.It Fn bus_space_read_multi_stream_4 "space" "handle" "offset" "datap" "count"
1458.It Fn bus_space_read_multi_stream_8 "space" "handle" "offset" "datap" "count"
1459.It Fn bus_space_read_region_stream_1 "space" "handle" "offset" "datap" "count"
1460.It Fn bus_space_read_region_stream_2 "space" "handle" "offset" "datap" "count"
1461.It Fn bus_space_read_region_stream_4 "space" "handle" "offset" "datap" "count"
1462.It Fn bus_space_read_region_stream_8 "space" "handle" "offset" "datap" "count"
1463.It Fn bus_space_write_stream_1 "space" "handle" "offset" "value"
1464.It Fn bus_space_write_stream_2 "space" "handle" "offset" "value"
1465.It Fn bus_space_write_stream_4 "space" "handle" "offset" "value"
1466.It Fn bus_space_write_stream_8 "space" "handle" "offset" "value"
1467.It Fn bus_space_write_multi_stream_1 "space" "handle" "offset" "datap" "count"
1468.It Fn bus_space_write_multi_stream_2 "space" "handle" "offset" "datap" "count"
1469.It Fn bus_space_write_multi_stream_4 "space" "handle" "offset" "datap" "count"
1470.It Fn bus_space_write_multi_stream_8 "space" "handle" "offset" "datap" "count"
1471.It Fn bus_space_write_region_stream_1 "space" "handle" "offset" "datap" "count"
1472.It Fn bus_space_write_region_stream_2 "space" "handle" "offset" "datap" "count"
1473.It Fn bus_space_write_region_stream_4 "space" "handle" "offset" "datap" "count"
1474.It Fn bus_space_write_region_stream_8 "space" "handle" "offset" "datap" "count"
1475.El
1476.Pp
1477These functions are defined just as their non-stream counterparts,
1478except that they provide no byte-order translation.
1479.Pp
1480.Sh EXPECTED CHANGES TO THE BUS_SPACE FUNCTIONS
1481.Pp
1482The definition of the
1483.Nm
1484functions should not yet be considered finalized.  There are several
1485changes and improvements which should be explored, including:
1486.Pp
1487.Bl -bullet
1488.It
1489Providing a mechanism by which incorrectly-written drivers will be
1490automatically given barriers and properly-written drivers won't be forced
1491to use more barriers than they need.  This should probably be done via a
1492.Li #define
1493in the incorrectly-written drivers.
1494Unfortunately, at this time, few drivers actually use barriers correctly
1495(or at all).  Because of that,
1496.Nm
1497implementations on architectures which do buffering must always
1498do the barriers inside the
1499.Nm
1500calls, to be safe.  That has a potentially significant
1501performance impact.
1502.It
1503Exporting the
1504.Nm
1505functions to user-land so that applications
1506(such as X servers) have easier, more portable access to device space.
1507.It
1508Redefining bus space tags and handles so that machine-independent bus
1509interface drivers (for example PCI to VME bridges) could define and
1510implement bus spaces without requiring machine-dependent code.  If this
1511is done, it should be done in such a way that machine-dependent
1512optimizations should remain possible.
1513.It
1514Converting bus spaces (such as PCI configuration space) which currently
1515use space-specific access methods to use the
1516.Nm
1517functions where that is appropriate.
1518.It
1519Redefining the way bus space is mapped and allocated, so that mapping
1520and allocation are done with bus specific functions which return bus
1521space tags.  This would allow further optimization than is currently
1522possible, and would also ease translation of the
1523.Nm
1524functions
1525into user space (since mapping in user space would look like it just used
1526a different bus-specific mapping function).
1527.El
1528.Pp
1529.Sh COMPATIBILITY
1530.Pp
1531The current version of the
1532.Nm
1533interface specification differs slightly from the original
1534specification that came into wide use.
1535A few of the function names and arguments have changed
1536for consistency and increased functionality.
1537Drivers that were written to the
1538old, deprecated specification can be compiled by defining the
1539.Dv __BUS_SPACE_COMPAT_OLDDEFS
1540preprocessor symbol before including
1541.Pa Aq machine/bus.h .
1542.Pp
1543.Sh HISTORY
1544.Pp
1545The
1546.Nm
1547functions were introduced in a different form (memory and I/O spaces
1548were accessed via different sets of functions) in
1549.Nx 1.2 .
1550The functions were merged to work on generic
1551.Dq spaces
1552early in the
1553.Nx 1.3
1554development cycle, and many drivers were converted to use them.
1555This document was written later during the
1556.Nx 1.3
1557development cycle and the specification was updated to fix some
1558consistency problems and to add some missing functionality.
1559.Pp
1560.Sh AUTHORS
1561.Pp
1562The
1563.Nm
1564interfaces were designed and implemented by the
1565.Nx
1566developer
1567community.  Primary contributors and implementors were Chris Demetriou,
1568Jason Thorpe, and Charles Hannum, but the rest of the
1569.Nx
1570developers and the user community played a significant role in development.
1571.Pp
1572Chris Demetriou wrote this manual page.
1573.Pp
1574.Sh SEE ALSO
1575.Xr bus_dma 9
1576