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