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