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