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