1.\"	$OpenBSD: bus_space.9,v 1.11 2004/04/16 21:23:08 jmc 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).
573The
574.Fn bus_space_vaddr
575method can be used to obtain the kernel virtual address of the mapped range.
576This is useful when software wants to do direct access to a memory
577device, e.g. a frame buffer.
578If this flag is specified and linear mapping is not possible, the
579.Fn bus_space_map
580call should fail.
581If this
582flag is not specified, the system may map the space in whatever way is
583most convenient.
584Use of this mapping method is not encouraged for normal device access;
585where linear access is not essential, use of the
586.Fn bus_space_read/write
587methods is strongly recommended.
588.El
589.Pp
590.\"Not all combinations of flags make sense or are supported with all
591.\"spaces.
592.\"For instance,
593.Dv BUS_SPACE_MAP_CACHEABLE
594may be meaningless when used on many systems' I/O port spaces.
595and on some systems
596.Dv BUS_SPACE_MAP_LINEAR
597without
598.Dv BUS_SPACE_MAP_PREFETCHABLE
599may never work.
600When the system hardware or firmware provides hints as to how spaces should be
601mapped (e.g. the PCI memory mapping registers' "prefetchable" bit), those
602hints should be followed for maximum compatibility.
603On some systems,
604requesting a mapping that cannot be satisfied (e.g. requesting a
605non-prefetchable mapping when the system can only provide a prefetchable one)
606will cause the request to fail.
607.Pp
608Some implementations may keep track of use of bus space for some or all
609bus spaces and refuse to allow duplicate allocations.
610This is encouraged for bus spaces which have no notion of slot-specific
611space addressing, such as ISA and VME, and for spaces which coexist with
612those spaces (e.g. EISA and PCI memory and I/O spaces co-existing with
613ISA memory and I/O spaces).
614.Pp
615Mapped regions may contain areas for which no there is no device on the
616bus.
617If space in those areas is accessed, the results are bus-dependent.
618.Pp
619.It Fn bus_space_unmap "space" "handle" "size"
620.Pp
621The
622.Fn bus_space_unmap
623function unmaps a region of bus space mapped with
624.Fn bus_space_map .
625When unmapping a region, the
626.Fa size
627specified should be the same as the size given to
628.Fn bus_space_map
629when mapping that region.
630.Pp
631After
632.Fn bus_space_unmap
633is called on a handle, that handle is no longer valid.
634If copies were made of the handle they are no longer valid, either.
635.Pp
636This function will never fail.
637If it would fail (e.g. because of an argument error), that indicates a
638software bug which should cause a panic.
639In that case,
640.Fn bus_space_unmap
641will never return.
642.Pp
643.It Fn bus_space_subregion "space" "handle" "offset" "size" "nhandlep"
644.Pp
645The
646.Fn bus_space_subregion
647function is a convenience function which makes a new handle to some
648subregion of an already-mapped region of bus space.
649The subregion described by the new handle starts at byte offset
650.Fa offset
651into the region described by
652.Fa handle ,
653with the size give by
654.Fa size ,
655and must be wholly contained within the original region.
656.Pp
657If successful,
658.Fn bus_space_subregion
659returns zero and fills in the bus space handle pointed to by
660.Fa nhandlep .
661If unsuccessful, it returns non-zero and leaves the bus space handle
662pointed to by
663.Fa nhandlep
664in an undefined state.
665In either case, the handle described by
666.Fa handle
667remains valid and is unmodified.
668.Pp
669When done with a handle created by
670.Fn bus_space_subregion ,
671the handle should be thrown away.
672Under no circumstances should
673.Fn bus_space_unmap
674be used on the handle.
675Doing so may confuse any resource management being done on the space,
676and will result in undefined behaviour.
677When
678.Fn bus_space_unmap
679or
680.Fn bus_space_free
681is called on a handle, all subregions of that handle become invalid.
682.Pp
683.It Fn bus_space_vaddr "tag" "handle"
684.Pp
685This method returns the kernel virtual address of a mapped bus space if and
686only if it was mapped with the
687.Dv BUS_SPACE_MAP_LINEAR
688flag.
689The range can be accessed by normal (volatile) pointer dereferences.
690If mapped with the
691.Dv BUS_SPACE_MAP_PREFETCHABLE
692flag, the
693.Fn bus_space_barrier
694method must be used to force a particular access order.
695.El
696.Sh ALLOCATING AND FREEING BUS SPACE
697Some devices require or allow bus space to be allocated by the operating
698system for device use.
699When the devices no longer need the space, the operating system should
700free it for use by other devices.
701The
702.Fn bus_space_alloc
703and
704.Fn bus_space_free
705functions provide these capabilities.
706.Pp
707.Bl -ohang -compact
708.It Xo
709.Fo bus_space_alloc
710.Fa "space" "reg_start" "reg_end" "size"
711.Fa "alignment" "boundary" "cacheable" "addrp" "handlep"
712.Fc
713.Xc
714.Pp
715The
716.Fn bus_space_alloc
717function allocates and maps a region of bus space with the size given by
718.Fa size ,
719corresponding to the given constraints.
720If successful, it returns zero, fills in the bus address pointed to by
721.Fa addrp
722with the bus space address of the allocated region, and fills in the bus
723space handle pointed to by
724.Fa handlep
725with the handle that can be used to access that region.
726If unsuccessful, it returns non-zero and leaves the bus address pointed
727to by
728.Fa addrp
729and the bus space handle pointed to by
730.Fa handlep
731in an undefined state.
732.Pp
733Constraints on the allocation are given by the
734.Fa reg_start ,
735.Fa reg_end ,
736.Fa alignment ,
737and
738.Fa boundary
739parameters.
740The allocated region will start at or after
741.Fa reg_start
742and end before or at
743.Fa reg_end .
744The
745.Fa alignment
746constraint must be a power of two, and the allocated region will start
747at an address that is an even multiple of that power of two.
748The
749.Fa boundary
750constraint, if non-zero, ensures that the region is allocated so that
751.Fa "first address in region"
752/
753.Fa boundary
754has the same value as
755.Fa "last address in region"
756/
757.Fa boundary .
758If the constraints cannot be met,
759.Fn bus_space_alloc
760will fail.
761It is an error to specify a set of constraints that can never be met
762.Po
763for example,
764.Fa size
765greater than
766.Fa boundary
767.Pc .
768.Pp
769The
770.Fa cacheable
771parameter is the same as the like-named parameter to
772.Fa bus_space_map ,
773the same flag values should be used, and they have the same meanings.
774.Pp
775Handles created by
776.Fn bus_space_alloc
777should only be freed with
778.Fn bus_space_free .
779Trying to use
780.Fn bus_space_unmap
781on them causes undefined behaviour.
782The
783.Fn bus_space_subregion
784function can be used on handles created by
785.Fn bus_space_alloc .
786.Pp
787.It Fn bus_space_free "space" "handle" "size"
788.Pp
789The
790.Fn bus_space_free
791function unmaps and frees a region of bus space mapped and allocated
792with
793.Fn bus_space_alloc .
794When unmapping a region, the
795.Fa size
796specified should be the same as the size given to
797.Fn bus_space_alloc
798when allocating the region.
799.Pp
800After
801.Fn bus_space_free
802is called on a handle, that handle is no longer valid.
803If copies were made of the handle, they are no longer valid, either.
804.Pp
805This function will never fail.
806If it would fail (e.g. because of an argument error), that indicates a
807software bug which should cause a panic.
808In that case,
809.Fn bus_space_free
810will never return.
811.El
812.Sh READING AND WRITING SINGLE DATA ITEMS
813The simplest way to access bus space is to read or write a single data
814item.
815The
816.Fn bus_space_read_N
817and
818.Fn bus_space_write_N
819families of functions provide the ability to read and write 1, 2, 4, and
8208 byte data items on busses which support those access sizes.
821.Pp
822.Bl -ohang -compact
823.It Fn bus_space_read_1 "space" "handle" "offset"
824.It Fn bus_space_read_2 "space" "handle" "offset"
825.It Fn bus_space_read_4 "space" "handle" "offset"
826.It Fn bus_space_read_8 "space" "handle" "offset"
827.Pp
828The
829.Fn bus_space_read_N
830family of functions reads a 1, 2, 4, or 8 byte data item from
831the offset specified by
832.Fa offset
833into the region specified by
834.Fa handle
835of the bus space specified by
836.Fa space .
837The location being read must lie within the bus space region specified
838by
839.Fa handle .
840.Pp
841For portability, the starting address of the region specified by
842.Fa handle
843plus the offset should be a multiple of the size of data item being
844read.
845On some systems, not obeying this requirement may cause incorrect data
846to be read, on others it may cause a system crash.
847.Pp
848Read operations done by the
849.Fn bus_space_read_N
850functions may be executed out of order with respect to other pending
851read and write operations unless order is enforced by use of the
852.Fn bus_space_barrier
853function.
854.Pp
855These functions will never fail.
856If they would fail (e.g. because of an argument error), that indicates a
857software bug which should cause a panic.
858In that case, they will never return.
859.Pp
860.It Fn bus_space_write_1 "space" "handle" "offset" "value"
861.It Fn bus_space_write_2 "space" "handle" "offset" "value"
862.It Fn bus_space_write_4 "space" "handle" "offset" "value"
863.It Fn bus_space_write_8 "space" "handle" "offset" "value"
864.Pp
865The
866.Fn bus_space_write_N
867family of functions writes a 1, 2, 4, or 8 byte data item to the offset
868specified by
869.Fa offset
870into the region specified by
871.Fa handle
872of the bus space specified by
873.Fa space .
874The location being written must lie within the bus space region
875specified by
876.Fa handle .
877.Pp
878For portability, the starting address of the region specified by
879.Fa handle
880plus the offset should be a multiple of the size of data item being
881written.
882On some systems, not obeying this requirement may cause incorrect data
883to be written, on others it may cause a system crash.
884.Pp
885Write operations done by the
886.Fn bus_space_write_N
887functions may be executed out of order with respect to other pending
888read and write operations unless order is enforced by use of the
889.Fn bus_space_barrier
890function.
891.Pp
892These functions will never fail.
893If they would fail (e.g. because of an argument error), that indicates a
894software bug which should cause a panic.
895In that case, they will never return.
896.El
897.Sh BARRIERS
898In order to allow high-performance buffering implementations to avoid
899bus activity on every operation, read and write ordering should be
900specified explicitly by drivers when necessary.
901The
902.Fn bus_space_barrier
903function provides that ability.
904.Pp
905.Bl -ohang -compact
906.It Fn bus_space_barrier "space" "handle" "offset" "length" "flags"
907.Pp
908The
909.Fn bus_space_barrier
910function enforces ordering of bus space read and write operations for
911the specified subregion (described by the
912.Fa offset
913and
914.Fa length
915parameters) of the region named by
916.Fa handle
917in the space named by
918.Fa space .
919.Pp
920The
921.Fa flags
922argument controls what types of operations are to be ordered.
923Supported flags are:
924.Bl -tag -width BUS_SPACE_BARRIER_WRITE -offset indent
925.It Dv BUS_SPACE_BARRIER_READ
926Synchronize read operations.
927.It Dv BUS_SPACE_BARRIER_WRITE
928Synchronize write operations.
929.El
930.Pp
931Those flags can be combined (or-ed together) to enforce ordering on both
932read and write operations.
933.Pp
934All of the specified type(s) of operation which are done to the region
935before the barrier operation are guaranteed to complete before any of
936the specified type(s) of operation done after the barrier.
937.Pp
938Example: Consider a hypothetical device with two single-byte ports, one
939write-only input port (at offset 0) and a read-only output port (at
940offset 1).
941Operation of the device is as follows: data bytes are written to the
942input port, and are placed by the device on a stack, the top of which is
943read by reading from the output port.
944The sequence to correctly write two data bytes to the device then read
945those two data bytes back would be:
946.Bd -literal
947/*
948 * t and h are the tag and handle for the mapped device's
949 * space.
950 */
951bus_space_write_1(t, h, 0, data0);
952bus_space_barrier(t, h, 0, 1, BUS_SPACE_BARRIER_WRITE);  /* 1 */
953bus_space_write_1(t, h, 0, data1);
954bus_space_barrier(t, h, 0, 2,
955    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);     /* 2 */
956ndata1 = bus_space_read_1(t, h, 1);
957bus_space_barrier(t, h, 1, 1, BUS_SPACE_BARRIER_READ);   /* 3 */
958ndata0 = bus_space_read_1(t, h, 1);
959/* data0 == ndata0, data1 == ndata1 */
960.Ed
961.Pp
962The first barrier makes sure that the first write finishes before the
963second write is issued, so that two writes to the input port are done in
964order and are not collapsed into a single write.
965This ensures that the data bytes are written to the device correctly and
966in order.
967.Pp
968The second barrier makes sure that the writes to the output port finish
969before any of the reads to the input port are issued, thereby making
970sure that all of the writes are finished before data is read.
971This ensures that the first byte read from the device really is the last
972one that was written.
973.Pp
974The third barrier makes sure that the first read finishes before the
975second read is issued, ensuring that data is read correctly and in order.
976.Pp
977The barriers in the example above are specified to cover the absolute
978minimum number of bus space locations.
979It is correct (and often easier) to make barrier operations cover the
980device's whole range of bus space, that is, to specify an offset of zero
981and the size of the whole region.
982.El
983.Sh REGION OPERATIONS
984Some devices use buffers which are mapped as regions in bus space.
985Often, drivers want to copy the contents of those buffers to or from
986memory, e.g. into mbufs which can be passed to higher levels of the
987system or from mbufs to be output to a network.
988In order to allow drivers to do this as efficiently as possible, the
989.Fn bus_space_read_region_N
990and
991.Fn bus_space_write_region_N
992families of functions are provided.
993.Pp
994Drivers occasionally need to copy one region of a bus space to another,
995or to set all locations in a region of bus space to contain a single
996value.
997The
998.Fn bus_space_copy_N
999family of functions and the
1000.Fn bus_space_set_region_N
1001family of functions allow drivers to perform these operations.
1002.Pp
1003.Bl -ohang -compact
1004.It Fn bus_space_read_region_1 "space" "handle" "offset" "datap" "count"
1005.It Fn bus_space_read_region_2 "space" "handle" "offset" "datap" "count"
1006.It Fn bus_space_read_region_4 "space" "handle" "offset" "datap" "count"
1007.It Fn bus_space_read_region_8 "space" "handle" "offset" "datap" "count"
1008.Pp
1009The
1010.Fn bus_space_read_region_N
1011family of functions reads
1012.Fa count
10131, 2, 4, or 8 byte data items from bus space starting at byte offset
1014.Fa offset
1015in the region specified by
1016.Fa handle
1017of the bus space specified by
1018.Fa space
1019and writes them into the array specified by
1020.Fa datap .
1021Each successive data item is read from an offset
10221, 2, 4, or 8 bytes after the previous data item (depending on which
1023function is used).
1024All locations being read must lie within the bus space region specified
1025by
1026.Fa handle .
1027.Pp
1028For portability, the starting address of the region specified by
1029.Fa handle
1030plus the offset should be a multiple of the size of data items being
1031read and the data array pointer should be properly aligned.
1032On some systems, not obeying these requirements may cause incorrect data
1033to be read, on others it may cause a system crash.
1034.Pp
1035Read operations done by the
1036.Fn bus_space_read_region_N
1037functions may be executed in any order.
1038They may also be executed out of order with respect to other pending
1039read and write operations unless order is enforced by use of the
1040.Fn bus_space_barrier
1041function.
1042There is no way to insert barriers between reads of individual bus space
1043locations executed by the
1044.Fn bus_space_read_region_N
1045functions.
1046.Pp
1047These functions will never fail.
1048If they would fail (e.g. because of an argument error), that indicates a
1049software bug which should cause a panic.
1050In that case, they will never return.
1051.Pp
1052.It Fn bus_space_write_region_1 "space" "handle" "offset" "datap" "count"
1053.It Fn bus_space_write_region_2 "space" "handle" "offset" "datap" "count"
1054.It Fn bus_space_write_region_4 "space" "handle" "offset" "datap" "count"
1055.It Fn bus_space_write_region_8 "space" "handle" "offset" "datap" "count"
1056.Pp
1057The
1058.Fn bus_space_write_region_N
1059family of functions reads
1060.Fa count
10611, 2, 4, or 8 byte data items from the array specified by
1062.Fa datap
1063and writes them to bus space starting at byte offset
1064.Fa offset
1065in the region specified by
1066.Fa handle
1067of the bus space specified by
1068.Fa space .
1069Each successive data item is written to an offset 1, 2, 4, or 8 bytes
1070after the previous data item (depending on which function is used).
1071All locations being written must lie within the bus space region
1072specified by
1073.Fa handle .
1074.Pp
1075For portability, the starting address of the region specified by
1076.Fa handle
1077plus the offset should be a multiple of the size of data items being
1078written and the data array pointer should be properly aligned.
1079On some systems, not obeying these requirements may cause incorrect data
1080to be written, on others it may cause a system crash.
1081.Pp
1082Write operations done by the
1083.Fn bus_space_write_region_N
1084functions may be executed in any order.
1085They may also be executed out of order with respect to other pending
1086read and write operations unless order is enforced by use of the
1087.Fn bus_space_barrier
1088function.
1089There is no way to insert barriers between writes of individual bus
1090space locations executed by the
1091.Fn bus_space_write_region_N
1092functions.
1093.Pp
1094These functions will never fail.
1095If they would fail (e.g. because of an argument error), that indicates a
1096software bug which should cause a panic.
1097In that case, they will never return.
1098.Pp
1099.It Fn bus_space_copy_1 "space" "srchandle" "srcoffset" "dsthandle" \
1100"dstoffset" "count"
1101.It Fn bus_space_copy_2 "space" "srchandle" "srcoffset" "dsthandle" \
1102"dstoffset" "count"
1103.It Fn bus_space_copy_4 "space" "srchandle" "srcoffset" "dsthandle" \
1104"dstoffset" "count"
1105.It Fn bus_space_copy_8 "space" "srchandle" "srcoffset" "dsthandle" \
1106"dstoffset" "count"
1107.Pp
1108The
1109.Fn bus_space_copy_N
1110family of functions copies
1111.Fa count
11121, 2, 4, or 8 byte data items in bus space from the area starting at
1113byte offset
1114.Fa srcoffset
1115in the region specified by
1116.Fa srchandle
1117of the bus space specified by
1118.Fa space
1119to the area starting at byte offset
1120.Fa dstoffset
1121in the region specified by
1122.Fa dsthandle
1123in the same bus space.
1124Each successive data item read or written has an offset 1, 2, 4, or 8
1125bytes after the previous data item (depending on which function is
1126used).
1127All locations being read and written must lie within the bus space
1128region specified by their respective handles.
1129.Pp
1130For portability, the starting addresses of the regions specified by
1131each handle plus its respective offset should be a multiple of the size
1132of data items being copied.
1133On some systems, not obeying this requirement may cause incorrect data
1134to be copied, on others it may cause a system crash.
1135.Pp
1136Read and write operations done by the
1137.Fn bus_space_copy_N
1138functions may be executed in any order.
1139They may also be executed out of order with respect to other pending
1140read and write operations unless order is enforced by use of the
1141.Fn bus_space_barrier function .
1142There is no way to insert barriers between reads or writes of individual
1143bus space locations executed by the
1144.Fn bus_space_copy_N
1145functions.
1146.Pp
1147Overlapping copies between different subregions of a single region of
1148bus space are handled correctly by the
1149.Fn bus_space_copy_N
1150functions.
1151.Pp
1152These functions will never fail.
1153If they would fail (e.g. because of an argument error), that indicates a
1154software bug which should cause a panic.
1155In that case, they will never return.
1156.Pp
1157.It Fn bus_space_set_region_1 "space" "handle" "offset" "value" "count"
1158.It Fn bus_space_set_region_2 "space" "handle" "offset" "value" "count"
1159.It Fn bus_space_set_region_4 "space" "handle" "offset" "value" "count"
1160.It Fn bus_space_set_region_8 "space" "handle" "offset" "value" "count"
1161.Pp
1162The
1163.Fn bus_space_set_region_N
1164family of functions writes the given
1165.Fa value
1166to
1167.Fa count
11681, 2, 4, or 8 byte data items in bus space starting at byte offset
1169.Fa offset
1170in the region specified by
1171.Fa handle
1172of the bus space specified by
1173.Fa space .
1174Each successive data item has an offset 1, 2, 4, or 8 bytes after the
1175previous data item (depending on which function is used).
1176All locations being written must lie within the bus space region
1177specified by
1178.Fa handle .
1179.Pp
1180For portability, the starting address of the region specified by
1181.Fa handle
1182plus the offset should be a multiple of the size of data items being
1183written.
1184On some systems, not obeying this requirement may cause incorrect data
1185to be written, on others it may cause a system crash.
1186.Pp
1187Write operations done by the
1188.Fn bus_space_set_region_N
1189functions may be executed in any order.
1190They may also be executed out of order with respect to other pending
1191read and write operations unless order is enforced by use of the
1192.Fn bus_space_barrier
1193function.
1194There is no way to insert barriers between writes of individual bus
1195space locations executed by the
1196.Fn bus_space_set_region_N
1197functions.
1198.Pp
1199These functions will never fail.
1200If they would fail (e.g. because of an argument error), that indicates a
1201software bug which should cause a panic.
1202In that case, they will never return.
1203.El
1204.Sh READING AND WRITING A SINGLE LOCATION MULTIPLE TIMES
1205Some devices implement single locations in bus space which are to be
1206read or written multiple times to communicate data, e.g. some ethernet
1207devices' packet buffer FIFOs.
1208In order to allow drivers to manipulate these types of devices as
1209efficiently as possible, the
1210.Fn bus_space_read_multi_N ,
1211.Fn bus_space_write_multi_N ,
1212and
1213.Fn bus_space_set_multi_N
1214families of functions are provided.
1215.Pp
1216.Bl -ohang -compact
1217.It Fn bus_space_read_multi_1 "space" "handle" "offset" "datap" "count"
1218.It Fn bus_space_read_multi_2 "space" "handle" "offset" "datap" "count"
1219.It Fn bus_space_read_multi_4 "space" "handle" "offset" "datap" "count"
1220.It Fn bus_space_read_multi_8 "space" "handle" "offset" "datap" "count"
1221.Pp
1222The
1223.Fn bus_space_read_multi_N
1224family of functions reads
1225.Fa count
12261, 2, 4, or 8 byte data items from bus space at byte offset
1227.Fa offset
1228in the region specified by
1229.Fa handle
1230of the bus space specified by
1231.Fa space
1232and writes them into the array specified by
1233.Fa datap .
1234Each successive data item is read from the same location in bus space.
1235The location being read must lie within the bus space region specified
1236by
1237.Fa handle .
1238.Pp
1239For portability, the starting address of the region specified by
1240.Fa handle
1241plus the offset should be a multiple of the size of data items being
1242read and the data array pointer should be properly aligned.
1243On some systems, not obeying these requirements may cause incorrect data
1244to be read, on others it may cause a system crash.
1245.Pp
1246Read operations done by the
1247.Fn bus_space_read_multi_N
1248functions may be executed out of order with respect to other pending
1249read and write operations unless order is enforced by use of the
1250.Fn bus_space_barrier
1251function.
1252Because the
1253.Fn bus_space_read_multi_N
1254functions read the same bus space location multiple times, they place an
1255implicit read barrier between each successive read of that bus space
1256location.
1257.Pp
1258These functions will never fail.
1259If they would fail (e.g. because of an argument error), that indicates a
1260software bug which should cause a panic.
1261In that case, they will never return.
1262.Pp
1263.It Fn bus_space_write_multi_1 "space" "handle" "offset" "datap" "count"
1264.It Fn bus_space_write_multi_2 "space" "handle" "offset" "datap" "count"
1265.It Fn bus_space_write_multi_4 "space" "handle" "offset" "datap" "count"
1266.It Fn bus_space_write_multi_8 "space" "handle" "offset" "datap" "count"
1267.Pp
1268The
1269.Fn bus_space_write_multi_N
1270family of functions reads
1271.Fa count
12721, 2, 4, or 8 byte data items from the array specified by
1273.Fa datap
1274and writes them into bus space at byte offset
1275.Fa offset
1276in the region specified by
1277.Fa handle
1278of the bus space specified by
1279.Fa space .
1280Each successive data item is written to the same location in bus space.
1281The location being written must lie within the bus space region
1282specified by
1283.Fa handle .
1284.Pp
1285For portability, the starting address of the region specified by
1286.Fa handle
1287plus the offset should be a multiple of the size of data items being
1288written and the data array pointer should be properly aligned.
1289On some systems, not obeying these requirements may cause incorrect data
1290to be written, on others it may cause a system crash.
1291.Pp
1292Write operations done by the
1293.Fn bus_space_write_multi_N
1294functions may be executed out of order with respect to other pending
1295read and write operations unless order is enforced by use of the
1296.Fn bus_space_barrier
1297function.
1298Because the
1299.Fn bus_space_write_multi_N
1300functions write the same bus space location multiple times, they place
1301an implicit write barrier between each successive write of that bus
1302space location.
1303.Pp
1304These functions will never fail.
1305If they would fail (e.g. because of an argument error), that indicates a
1306software bug which should cause a panic.
1307In that case, they will never return.
1308.Pp
1309.It Fn bus_space_set_multi_1 "space" "handle" "offset" "value" "count"
1310.It Fn bus_space_set_multi_2 "space" "handle" "offset" "value" "count"
1311.It Fn bus_space_set_multi_4 "space" "handle" "offset" "value" "count"
1312.It Fn bus_space_set_multi_8 "space" "handle" "offset" "value" "count"
1313.Pp
1314The
1315.Fn bus_space_set_multi_N
1316family of functions writes the 1, 2, 4, or 8 byte value
1317.Fa value
1318into bus space
1319.Fa count
1320times at byte offset
1321.Fa offset
1322in the region specified by
1323.Fa handle
1324of the bus space specified by
1325.Fa space .
1326The location being written must lie within the bus space region
1327specified by
1328.Fa handle .
1329.Pp
1330For portability, the address specified by
1331.Fa handle
1332plus the offset should be a multiple of the size of the data value being
1333written.
1334On some systems, not obeying these requirements may cause
1335incorrect data to be written, on others it may cause a system crash.
1336.Pp
1337Write operations done by the
1338.Fn bus_space_set_multi_N
1339functions may be executed out of order with respect to other pending
1340read and write operations unless order is enforced by use of the
1341.Fn bus_space_barrier
1342function.
1343Because the
1344.Fn bus_space_set_multi_N
1345functions write the same bus space location multiple times, they place
1346an implicit write barrier between each successive write of that bus
1347space location.
1348.Pp
1349These functions will never fail.
1350If they would fail (e.g. because of an argument error), that indicates a
1351software bug which should cause a panic.
1352In that case, they will never return.
1353.El
1354.Sh RAW FUNCTIONS
1355Most of the
1356.Nm
1357functions imply a host byte-order and a bus byte-order and take care of
1358any translation for the caller.
1359In some cases, however, hardware may map a FIFO or some other memory
1360region for which the caller may want to use multi-word, yet untranslated
1361access.
1362Access to these types of memory regions should be with the
1363.Fn bus_space_*_raw_*_N
1364functions.
1365.Pp
1366.Bl -ohang -compact
1367.\".It Fn bus_space_read_stream_1 "space" "handle" "offset"
1368.\".It Fn bus_space_read_stream_2 "space" "handle" "offset"
1369.\".It Fn bus_space_read_stream_4 "space" "handle" "offset"
1370.\".It Fn bus_space_read_stream_8 "space" "handle" "offset"
1371.\".It Fn bus_space_read_raw_multi_1 "space" "handle" "offset" "datap" "size"
1372.It Fn bus_space_read_raw_multi_2 "space" "handle" "offset" "datap" "size"
1373.It Fn bus_space_read_raw_multi_4 "space" "handle" "offset" "datap" "size"
1374.It Fn bus_space_read_raw_multi_8 "space" "handle" "offset" "datap" "size"
1375.\".It Fn bus_space_read_raw_region_1 "space" "handle" "offset" "datap" "size"
1376.It Fn bus_space_read_raw_region_2 "space" "handle" "offset" "datap" "size"
1377.It Fn bus_space_read_raw_region_4 "space" "handle" "offset" "datap" "size"
1378.It Fn bus_space_read_raw_region_8 "space" "handle" "offset" "datap" "size"
1379.\".It Fn bus_space_write_stream_1 "space" "handle" "offset" "value"
1380.\".It Fn bus_space_write_stream_2 "space" "handle" "offset" "value"
1381.\".It Fn bus_space_write_stream_4 "space" "handle" "offset" "value"
1382.\".It Fn bus_space_write_stream_8 "space" "handle" "offset" "value"
1383.\".It Fn bus_space_write_raw_multi_1 "space" "handle" "offset" "datap" "size"
1384.It Fn bus_space_write_raw_multi_2 "space" "handle" "offset" "datap" "size"
1385.It Fn bus_space_write_raw_multi_4 "space" "handle" "offset" "datap" "size"
1386.It Fn bus_space_write_raw_multi_8 "space" "handle" "offset" "datap" "size"
1387.\".It Fn bus_space_write_raw_region_1 "space" "handle" "offset" "datap" "size"
1388.It Fn bus_space_write_raw_region_2 "space" "handle" "offset" "datap" "size"
1389.It Fn bus_space_write_raw_region_4 "space" "handle" "offset" "datap" "size"
1390.It Fn bus_space_write_raw_region_8 "space" "handle" "offset" "datap" "size"
1391.El
1392.Pp
1393These functions, unlike their non-raw counterparts, all take a
1394u_int8_t pointer for the
1395.Fa datap
1396argument and a byte count for the
1397.Fa size
1398argument regardless of the access width being requested.
1399.Pp
1400.Fa datap
1401must reference a buffer that is correctly aligned for the
1402access width being requested or the results are undefined.
1403.Pp
1404.Fa size
1405must be a multiple of the access width or the results are undefined.
1406.Pp
1407In all other respects these functions are the same as their non-raw
1408counterparts.
1409Please consult the documentation for those functions for further
1410information.
1411.Sh EXPECTED CHANGES TO THE BUS_SPACE FUNCTIONS
1412The definition of the
1413.Nm
1414functions should not yet be considered finalized.
1415There are several changes and improvements which should be explored,
1416including:
1417.Bl -bullet
1418.It
1419Providing a mechanism by which incorrectly-written drivers will be
1420automatically given barriers and properly-written drivers won't be
1421forced to use more barriers than they need.
1422This should probably be done via a
1423.Li #define
1424in the incorrectly-written drivers.
1425Unfortunately, at this time, few drivers actually use barriers correctly
1426(or at all).
1427Because of that,
1428.Nm
1429implementations on architectures which do buffering must always do the
1430barriers inside the
1431.Nm
1432calls, to be safe.
1433That has a potentially significant performance impact.
1434.It
1435Exporting the
1436.Nm
1437functions to user-land so that applications (such as X servers) have
1438easier, more portable access to device space.
1439.It
1440Redefining bus space tags and handles so that machine-independent bus
1441interface drivers (for example PCI to VME bridges) could define and
1442implement bus spaces without requiring machine-dependent code.
1443If this is done, it should be done in such a way that machine-dependent
1444optimizations should remain possible.
1445.It
1446Converting bus spaces (such as PCI configuration space) which currently
1447use space-specific access methods to use the
1448.Nm
1449functions where that is appropriate.
1450.It
1451Redefining the way bus space is mapped and allocated, so that mapping
1452and allocation are done with bus specific functions which return bus
1453space tags.
1454This would allow further optimization than is currently possible, and
1455would also ease translation of the
1456.Nm
1457functions into user space (since mapping in user space would look like
1458it just used a different bus-specific mapping function).
1459.El
1460.Sh SEE ALSO
1461.Xr bus_dma 9
1462.\".Sh STANDARDS
1463.\".Pp
1464.\"The current version of the
1465.\".Nm
1466.\"interface specification differs slightly from the original
1467.\"specification that came into wide use.
1468.\"A few of the function names and arguments have changed
1469.\"for consistency and increased functionality.
1470.\"Drivers that were written to the
1471.\"old, deprecated specification can be compiled by defining the
1472.\".Dv __BUS_SPACE_COMPAT_OLDDEFS
1473.\"preprocessor symbol before including
1474.\".Pa Aq machine/bus.h .
1475.Sh HISTORY
1476The
1477.Nm
1478functions were introduced in a different form (memory and I/O spaces
1479were accessed via different sets of functions) in
1480.Nx 1.2 .
1481The functions were merged to work on generic
1482.Dq spaces
1483early in the
1484.Nx 1.3
1485development cycle, and many drivers were converted to use them.
1486This document was written later during the
1487.Nx 1.3
1488development cycle and the specification was updated to fix some
1489consistency problems and to add some missing functionality.
1490.Pp
1491The
1492.Ox
1493development team added the *_raw_* API, and discarded the *_stream_*
1494API.
1495.Sh AUTHORS
1496The
1497.Nm
1498interfaces were designed and implemented by the
1499.Nx
1500developer community.
1501Primary contributors and implementors were Chris Demetriou, Jason
1502Thorpe, and Charles Hannum, but the rest of the
1503.Nx
1504developers and the user community played a significant role in
1505development.
1506.Pp
1507Chris Demetriou wrote this manual page.
1508.Pp
1509Niklas Hallqvist did the *_raw_* API for
1510.Ox .
1511