2 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-NetBSDE
4 * Copyright (c) KATO Takenori, 1999.
6 * All rights reserved. Unpublished rights reserved under the copyright
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer as
15 * the first lines of this file unmodified.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 /* $NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $ */
39 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
40 * All rights reserved.
42 * This code is derived from software contributed to The NetBSD Foundation
43 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
44 * NASA Ames Research Center.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
55 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
69 * Copyright (c) 1996 Charles M. Hannum. All rights reserved.
70 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. All advertising materials mentioning features or use of this software
81 * must display the following acknowledgement:
82 * This product includes software developed by Christopher G. Demetriou
83 * for the NetBSD Project.
84 * 4. The name of the author may not be used to endorse or promote products
85 * derived from this software without specific prior written permission
87 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
88 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
90 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
91 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
92 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
93 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
94 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
95 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
96 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99 #ifndef _MACHINE_BUS_H_
100 #define _MACHINE_BUS_H_
102 #include <machine/_bus.h>
103 #include <machine/cpufunc.h>
104 #include <machine/bus_dma.h>
107 * Values for the x86 bus space tag, not to be used directly by MI code.
109 #define X86_BUS_SPACE_IO 0 /* space is i/o space */
110 #define X86_BUS_SPACE_MEM 1 /* space is mem space */
112 #define BUS_SPACE_MAXSIZE_24BIT 0xFFFFFF
113 #define BUS_SPACE_MAXSIZE_32BIT 0xFFFFFFFF
114 #if defined(__amd64__)
115 #define BUS_SPACE_MAXSIZE 0xFFFFFFFFFFFFFFFFULL
117 #define BUS_SPACE_MAXSIZE 0xFFFFFFFF
119 #define BUS_SPACE_MAXADDR_24BIT 0xFFFFFF
120 #define BUS_SPACE_MAXADDR_32BIT 0xFFFFFFFF
121 #if defined(__amd64__) || defined(PAE)
122 #define BUS_SPACE_MAXADDR_48BIT 0xFFFFFFFFFFFFULL
123 #define BUS_SPACE_MAXADDR 0xFFFFFFFFFFFFFFFFULL
125 #define BUS_SPACE_MAXADDR 0xFFFFFFFF
128 #define BUS_SPACE_INVALID_DATA (~0)
129 #define BUS_SPACE_UNRESTRICTED (~0)
131 #define BUS_SPACE_BARRIER_READ 0x01 /* force read barrier */
132 #define BUS_SPACE_BARRIER_WRITE 0x02 /* force write barrier */
134 #if defined(SAN_NEEDS_INTERCEPTORS) && !defined(SAN_RUNTIME)
135 #include <sys/bus_san.h>
139 * Map a region of device bus space into CPU virtual address space.
142 int bus_space_map(bus_space_tag_t tag, bus_addr_t addr, bus_size_t size,
143 int flags, bus_space_handle_t *bshp);
146 * Unmap a region of device bus space.
149 void bus_space_unmap(bus_space_tag_t tag, bus_space_handle_t bsh,
153 * Get a new handle for a subregion of an already-mapped area of bus space.
156 static __inline int bus_space_subregion(bus_space_tag_t t,
157 bus_space_handle_t bsh,
158 bus_size_t offset, bus_size_t size,
159 bus_space_handle_t *nbshp);
162 bus_space_subregion(bus_space_tag_t t __unused, bus_space_handle_t bsh,
163 bus_size_t offset, bus_size_t size __unused,
164 bus_space_handle_t *nbshp)
167 *nbshp = bsh + offset;
172 * Allocate a region of memory that is accessible to devices in bus space.
175 int bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart,
176 bus_addr_t rend, bus_size_t size, bus_size_t align,
177 bus_size_t boundary, int flags, bus_addr_t *addrp,
178 bus_space_handle_t *bshp);
181 * Free a region of bus space accessible memory.
184 static __inline void bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh,
188 bus_space_free(bus_space_tag_t t __unused, bus_space_handle_t bsh __unused,
189 bus_size_t size __unused)
194 * Read a 1, 2, 4, or 8 byte quantity from bus space
195 * described by tag/handle/offset.
197 static __inline u_int8_t bus_space_read_1(bus_space_tag_t tag,
198 bus_space_handle_t handle,
201 static __inline u_int16_t bus_space_read_2(bus_space_tag_t tag,
202 bus_space_handle_t handle,
205 static __inline u_int32_t bus_space_read_4(bus_space_tag_t tag,
206 bus_space_handle_t handle,
210 static __inline uint64_t bus_space_read_8(bus_space_tag_t tag,
211 bus_space_handle_t handle,
215 static __inline u_int8_t
216 bus_space_read_1(bus_space_tag_t tag, bus_space_handle_t handle,
220 if (tag == X86_BUS_SPACE_IO)
221 return (inb(handle + offset));
222 return (*(volatile u_int8_t *)(handle + offset));
225 static __inline u_int16_t
226 bus_space_read_2(bus_space_tag_t tag, bus_space_handle_t handle,
230 if (tag == X86_BUS_SPACE_IO)
231 return (inw(handle + offset));
232 return (*(volatile u_int16_t *)(handle + offset));
235 static __inline u_int32_t
236 bus_space_read_4(bus_space_tag_t tag, bus_space_handle_t handle,
240 if (tag == X86_BUS_SPACE_IO)
241 return (inl(handle + offset));
242 return (*(volatile u_int32_t *)(handle + offset));
246 static __inline uint64_t
247 bus_space_read_8(bus_space_tag_t tag, bus_space_handle_t handle,
251 if (tag == X86_BUS_SPACE_IO) /* No 8 byte IO space access on x86 */
252 return (BUS_SPACE_INVALID_DATA);
253 return (*(volatile uint64_t *)(handle + offset));
258 * Read `count' 1, 2, 4, or 8 byte quantities from bus space
259 * described by tag/handle/offset and copy into buffer provided.
261 static __inline void bus_space_read_multi_1(bus_space_tag_t tag,
262 bus_space_handle_t bsh,
263 bus_size_t offset, u_int8_t *addr,
266 static __inline void bus_space_read_multi_2(bus_space_tag_t tag,
267 bus_space_handle_t bsh,
268 bus_size_t offset, u_int16_t *addr,
271 static __inline void bus_space_read_multi_4(bus_space_tag_t tag,
272 bus_space_handle_t bsh,
273 bus_size_t offset, u_int32_t *addr,
277 bus_space_read_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
278 bus_size_t offset, u_int8_t *addr, size_t count)
281 if (tag == X86_BUS_SPACE_IO)
282 insb(bsh + offset, addr, count);
284 __asm __volatile(" \n\
285 1: movb (%2),%%al \n\
288 "=D" (addr), "=c" (count) :
289 "r" (bsh + offset), "0" (addr), "1" (count) :
295 bus_space_read_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
296 bus_size_t offset, u_int16_t *addr, size_t count)
299 if (tag == X86_BUS_SPACE_IO)
300 insw(bsh + offset, addr, count);
302 __asm __volatile(" \n\
303 1: movw (%2),%%ax \n\
306 "=D" (addr), "=c" (count) :
307 "r" (bsh + offset), "0" (addr), "1" (count) :
313 bus_space_read_multi_4(bus_space_tag_t tag, bus_space_handle_t bsh,
314 bus_size_t offset, u_int32_t *addr, size_t count)
317 if (tag == X86_BUS_SPACE_IO)
318 insl(bsh + offset, addr, count);
320 __asm __volatile(" \n\
321 1: movl (%2),%%eax \n\
324 "=D" (addr), "=c" (count) :
325 "r" (bsh + offset), "0" (addr), "1" (count) :
330 #if 0 /* Cause a link error for bus_space_read_multi_8 */
331 #define bus_space_read_multi_8 !!! bus_space_read_multi_8 unimplemented !!!
335 * Read `count' 1, 2, 4, or 8 byte quantities from bus space
336 * described by tag/handle and starting at `offset' and copy into
339 static __inline void bus_space_read_region_1(bus_space_tag_t tag,
340 bus_space_handle_t bsh,
341 bus_size_t offset, u_int8_t *addr,
344 static __inline void bus_space_read_region_2(bus_space_tag_t tag,
345 bus_space_handle_t bsh,
346 bus_size_t offset, u_int16_t *addr,
349 static __inline void bus_space_read_region_4(bus_space_tag_t tag,
350 bus_space_handle_t bsh,
351 bus_size_t offset, u_int32_t *addr,
355 bus_space_read_region_1(bus_space_tag_t tag, bus_space_handle_t bsh,
356 bus_size_t offset, u_int8_t *addr, size_t count)
359 if (tag == X86_BUS_SPACE_IO) {
360 int _port_ = bsh + offset;
361 __asm __volatile(" \n\
366 "=D" (addr), "=c" (count), "=d" (_port_) :
367 "0" (addr), "1" (count), "2" (_port_) :
368 "%eax", "memory", "cc");
370 bus_space_handle_t _port_ = bsh + offset;
371 __asm __volatile(" \n\
374 "=D" (addr), "=c" (count), "=S" (_port_) :
375 "0" (addr), "1" (count), "2" (_port_) :
381 bus_space_read_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
382 bus_size_t offset, u_int16_t *addr, size_t count)
385 if (tag == X86_BUS_SPACE_IO) {
386 int _port_ = bsh + offset;
387 __asm __volatile(" \n\
392 "=D" (addr), "=c" (count), "=d" (_port_) :
393 "0" (addr), "1" (count), "2" (_port_) :
394 "%eax", "memory", "cc");
396 bus_space_handle_t _port_ = bsh + offset;
397 __asm __volatile(" \n\
400 "=D" (addr), "=c" (count), "=S" (_port_) :
401 "0" (addr), "1" (count), "2" (_port_) :
407 bus_space_read_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
408 bus_size_t offset, u_int32_t *addr, size_t count)
411 if (tag == X86_BUS_SPACE_IO) {
412 int _port_ = bsh + offset;
413 __asm __volatile(" \n\
418 "=D" (addr), "=c" (count), "=d" (_port_) :
419 "0" (addr), "1" (count), "2" (_port_) :
420 "%eax", "memory", "cc");
422 bus_space_handle_t _port_ = bsh + offset;
423 __asm __volatile(" \n\
426 "=D" (addr), "=c" (count), "=S" (_port_) :
427 "0" (addr), "1" (count), "2" (_port_) :
432 #if 0 /* Cause a link error for bus_space_read_region_8 */
433 #define bus_space_read_region_8 !!! bus_space_read_region_8 unimplemented !!!
437 * Write the 1, 2, 4, or 8 byte value `value' to bus space
438 * described by tag/handle/offset.
441 static __inline void bus_space_write_1(bus_space_tag_t tag,
442 bus_space_handle_t bsh,
443 bus_size_t offset, u_int8_t value);
445 static __inline void bus_space_write_2(bus_space_tag_t tag,
446 bus_space_handle_t bsh,
447 bus_size_t offset, u_int16_t value);
449 static __inline void bus_space_write_4(bus_space_tag_t tag,
450 bus_space_handle_t bsh,
451 bus_size_t offset, u_int32_t value);
454 static __inline void bus_space_write_8(bus_space_tag_t tag,
455 bus_space_handle_t bsh,
456 bus_size_t offset, uint64_t value);
460 bus_space_write_1(bus_space_tag_t tag, bus_space_handle_t bsh,
461 bus_size_t offset, u_int8_t value)
464 if (tag == X86_BUS_SPACE_IO)
465 outb(bsh + offset, value);
467 *(volatile u_int8_t *)(bsh + offset) = value;
471 bus_space_write_2(bus_space_tag_t tag, bus_space_handle_t bsh,
472 bus_size_t offset, u_int16_t value)
475 if (tag == X86_BUS_SPACE_IO)
476 outw(bsh + offset, value);
478 *(volatile u_int16_t *)(bsh + offset) = value;
482 bus_space_write_4(bus_space_tag_t tag, bus_space_handle_t bsh,
483 bus_size_t offset, u_int32_t value)
486 if (tag == X86_BUS_SPACE_IO)
487 outl(bsh + offset, value);
489 *(volatile u_int32_t *)(bsh + offset) = value;
494 bus_space_write_8(bus_space_tag_t tag, bus_space_handle_t bsh,
495 bus_size_t offset, uint64_t value)
498 if (tag == X86_BUS_SPACE_IO) /* No 8 byte IO space access on x86 */
501 *(volatile uint64_t *)(bsh + offset) = value;
506 * Write `count' 1, 2, 4, or 8 byte quantities from the buffer
507 * provided to bus space described by tag/handle/offset.
510 static __inline void bus_space_write_multi_1(bus_space_tag_t tag,
511 bus_space_handle_t bsh,
513 const u_int8_t *addr,
515 static __inline void bus_space_write_multi_2(bus_space_tag_t tag,
516 bus_space_handle_t bsh,
518 const u_int16_t *addr,
521 static __inline void bus_space_write_multi_4(bus_space_tag_t tag,
522 bus_space_handle_t bsh,
524 const u_int32_t *addr,
528 bus_space_write_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
529 bus_size_t offset, const u_int8_t *addr, size_t count)
532 if (tag == X86_BUS_SPACE_IO)
533 outsb(bsh + offset, addr, count);
535 __asm __volatile(" \n\
539 "=S" (addr), "=c" (count) :
540 "r" (bsh + offset), "0" (addr), "1" (count) :
541 "%eax", "memory", "cc");
546 bus_space_write_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
547 bus_size_t offset, const u_int16_t *addr, size_t count)
550 if (tag == X86_BUS_SPACE_IO)
551 outsw(bsh + offset, addr, count);
553 __asm __volatile(" \n\
557 "=S" (addr), "=c" (count) :
558 "r" (bsh + offset), "0" (addr), "1" (count) :
559 "%eax", "memory", "cc");
564 bus_space_write_multi_4(bus_space_tag_t tag, bus_space_handle_t bsh,
565 bus_size_t offset, const u_int32_t *addr, size_t count)
568 if (tag == X86_BUS_SPACE_IO)
569 outsl(bsh + offset, addr, count);
571 __asm __volatile(" \n\
575 "=S" (addr), "=c" (count) :
576 "r" (bsh + offset), "0" (addr), "1" (count) :
577 "%eax", "memory", "cc");
581 #if 0 /* Cause a link error for bus_space_write_multi_8 */
582 #define bus_space_write_multi_8(t, h, o, a, c) \
583 !!! bus_space_write_multi_8 unimplemented !!!
587 * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided
588 * to bus space described by tag/handle starting at `offset'.
591 static __inline void bus_space_write_region_1(bus_space_tag_t tag,
592 bus_space_handle_t bsh,
594 const u_int8_t *addr,
596 static __inline void bus_space_write_region_2(bus_space_tag_t tag,
597 bus_space_handle_t bsh,
599 const u_int16_t *addr,
601 static __inline void bus_space_write_region_4(bus_space_tag_t tag,
602 bus_space_handle_t bsh,
604 const u_int32_t *addr,
608 bus_space_write_region_1(bus_space_tag_t tag, bus_space_handle_t bsh,
609 bus_size_t offset, const u_int8_t *addr, size_t count)
612 if (tag == X86_BUS_SPACE_IO) {
613 int _port_ = bsh + offset;
614 __asm __volatile(" \n\
619 "=d" (_port_), "=S" (addr), "=c" (count) :
620 "0" (_port_), "1" (addr), "2" (count) :
621 "%eax", "memory", "cc");
623 bus_space_handle_t _port_ = bsh + offset;
624 __asm __volatile(" \n\
627 "=D" (_port_), "=S" (addr), "=c" (count) :
628 "0" (_port_), "1" (addr), "2" (count) :
634 bus_space_write_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
635 bus_size_t offset, const u_int16_t *addr, size_t count)
638 if (tag == X86_BUS_SPACE_IO) {
639 int _port_ = bsh + offset;
640 __asm __volatile(" \n\
645 "=d" (_port_), "=S" (addr), "=c" (count) :
646 "0" (_port_), "1" (addr), "2" (count) :
647 "%eax", "memory", "cc");
649 bus_space_handle_t _port_ = bsh + offset;
650 __asm __volatile(" \n\
653 "=D" (_port_), "=S" (addr), "=c" (count) :
654 "0" (_port_), "1" (addr), "2" (count) :
660 bus_space_write_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
661 bus_size_t offset, const u_int32_t *addr, size_t count)
664 if (tag == X86_BUS_SPACE_IO) {
665 int _port_ = bsh + offset;
666 __asm __volatile(" \n\
671 "=d" (_port_), "=S" (addr), "=c" (count) :
672 "0" (_port_), "1" (addr), "2" (count) :
673 "%eax", "memory", "cc");
675 bus_space_handle_t _port_ = bsh + offset;
676 __asm __volatile(" \n\
679 "=D" (_port_), "=S" (addr), "=c" (count) :
680 "0" (_port_), "1" (addr), "2" (count) :
685 #if 0 /* Cause a link error for bus_space_write_region_8 */
686 #define bus_space_write_region_8 \
687 !!! bus_space_write_region_8 unimplemented !!!
691 * Write the 1, 2, 4, or 8 byte value `val' to bus space described
692 * by tag/handle/offset `count' times.
695 static __inline void bus_space_set_multi_1(bus_space_tag_t tag,
696 bus_space_handle_t bsh,
698 u_int8_t value, size_t count);
699 static __inline void bus_space_set_multi_2(bus_space_tag_t tag,
700 bus_space_handle_t bsh,
702 u_int16_t value, size_t count);
703 static __inline void bus_space_set_multi_4(bus_space_tag_t tag,
704 bus_space_handle_t bsh,
706 u_int32_t value, size_t count);
709 bus_space_set_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
710 bus_size_t offset, u_int8_t value, size_t count)
712 bus_space_handle_t addr = bsh + offset;
714 if (tag == X86_BUS_SPACE_IO)
719 *(volatile u_int8_t *)(addr) = value;
723 bus_space_set_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
724 bus_size_t offset, u_int16_t value, size_t count)
726 bus_space_handle_t addr = bsh + offset;
728 if (tag == X86_BUS_SPACE_IO)
733 *(volatile u_int16_t *)(addr) = value;
737 bus_space_set_multi_4(bus_space_tag_t tag, bus_space_handle_t bsh,
738 bus_size_t offset, u_int32_t value, size_t count)
740 bus_space_handle_t addr = bsh + offset;
742 if (tag == X86_BUS_SPACE_IO)
747 *(volatile u_int32_t *)(addr) = value;
750 #if 0 /* Cause a link error for bus_space_set_multi_8 */
751 #define bus_space_set_multi_8 !!! bus_space_set_multi_8 unimplemented !!!
755 * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described
756 * by tag/handle starting at `offset'.
759 static __inline void bus_space_set_region_1(bus_space_tag_t tag,
760 bus_space_handle_t bsh,
761 bus_size_t offset, u_int8_t value,
763 static __inline void bus_space_set_region_2(bus_space_tag_t tag,
764 bus_space_handle_t bsh,
765 bus_size_t offset, u_int16_t value,
767 static __inline void bus_space_set_region_4(bus_space_tag_t tag,
768 bus_space_handle_t bsh,
769 bus_size_t offset, u_int32_t value,
773 bus_space_set_region_1(bus_space_tag_t tag, bus_space_handle_t bsh,
774 bus_size_t offset, u_int8_t value, size_t count)
776 bus_space_handle_t addr = bsh + offset;
778 if (tag == X86_BUS_SPACE_IO)
779 for (; count != 0; count--, addr++)
782 for (; count != 0; count--, addr++)
783 *(volatile u_int8_t *)(addr) = value;
787 bus_space_set_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
788 bus_size_t offset, u_int16_t value, size_t count)
790 bus_space_handle_t addr = bsh + offset;
792 if (tag == X86_BUS_SPACE_IO)
793 for (; count != 0; count--, addr += 2)
796 for (; count != 0; count--, addr += 2)
797 *(volatile u_int16_t *)(addr) = value;
801 bus_space_set_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
802 bus_size_t offset, u_int32_t value, size_t count)
804 bus_space_handle_t addr = bsh + offset;
806 if (tag == X86_BUS_SPACE_IO)
807 for (; count != 0; count--, addr += 4)
810 for (; count != 0; count--, addr += 4)
811 *(volatile u_int32_t *)(addr) = value;
814 #if 0 /* Cause a link error for bus_space_set_region_8 */
815 #define bus_space_set_region_8 !!! bus_space_set_region_8 unimplemented !!!
819 * Copy `count' 1, 2, 4, or 8 byte values from bus space starting
820 * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2.
823 static __inline void bus_space_copy_region_1(bus_space_tag_t tag,
824 bus_space_handle_t bsh1,
826 bus_space_handle_t bsh2,
827 bus_size_t off2, size_t count);
829 static __inline void bus_space_copy_region_2(bus_space_tag_t tag,
830 bus_space_handle_t bsh1,
832 bus_space_handle_t bsh2,
833 bus_size_t off2, size_t count);
835 static __inline void bus_space_copy_region_4(bus_space_tag_t tag,
836 bus_space_handle_t bsh1,
838 bus_space_handle_t bsh2,
839 bus_size_t off2, size_t count);
842 bus_space_copy_region_1(bus_space_tag_t tag, bus_space_handle_t bsh1,
843 bus_size_t off1, bus_space_handle_t bsh2,
844 bus_size_t off2, size_t count)
846 bus_space_handle_t addr1 = bsh1 + off1;
847 bus_space_handle_t addr2 = bsh2 + off2;
849 if (tag == X86_BUS_SPACE_IO) {
850 if (addr1 >= addr2) {
851 /* src after dest: copy forward */
852 for (; count != 0; count--, addr1++, addr2++)
853 outb(addr2, inb(addr1));
855 /* dest after src: copy backwards */
856 for (addr1 += (count - 1), addr2 += (count - 1);
857 count != 0; count--, addr1--, addr2--)
858 outb(addr2, inb(addr1));
861 if (addr1 >= addr2) {
862 /* src after dest: copy forward */
863 for (; count != 0; count--, addr1++, addr2++)
864 *(volatile u_int8_t *)(addr2) =
865 *(volatile u_int8_t *)(addr1);
867 /* dest after src: copy backwards */
868 for (addr1 += (count - 1), addr2 += (count - 1);
869 count != 0; count--, addr1--, addr2--)
870 *(volatile u_int8_t *)(addr2) =
871 *(volatile u_int8_t *)(addr1);
877 bus_space_copy_region_2(bus_space_tag_t tag, bus_space_handle_t bsh1,
878 bus_size_t off1, bus_space_handle_t bsh2,
879 bus_size_t off2, size_t count)
881 bus_space_handle_t addr1 = bsh1 + off1;
882 bus_space_handle_t addr2 = bsh2 + off2;
884 if (tag == X86_BUS_SPACE_IO) {
885 if (addr1 >= addr2) {
886 /* src after dest: copy forward */
887 for (; count != 0; count--, addr1 += 2, addr2 += 2)
888 outw(addr2, inw(addr1));
890 /* dest after src: copy backwards */
891 for (addr1 += 2 * (count - 1), addr2 += 2 * (count - 1);
892 count != 0; count--, addr1 -= 2, addr2 -= 2)
893 outw(addr2, inw(addr1));
896 if (addr1 >= addr2) {
897 /* src after dest: copy forward */
898 for (; count != 0; count--, addr1 += 2, addr2 += 2)
899 *(volatile u_int16_t *)(addr2) =
900 *(volatile u_int16_t *)(addr1);
902 /* dest after src: copy backwards */
903 for (addr1 += 2 * (count - 1), addr2 += 2 * (count - 1);
904 count != 0; count--, addr1 -= 2, addr2 -= 2)
905 *(volatile u_int16_t *)(addr2) =
906 *(volatile u_int16_t *)(addr1);
912 bus_space_copy_region_4(bus_space_tag_t tag, bus_space_handle_t bsh1,
913 bus_size_t off1, bus_space_handle_t bsh2,
914 bus_size_t off2, size_t count)
916 bus_space_handle_t addr1 = bsh1 + off1;
917 bus_space_handle_t addr2 = bsh2 + off2;
919 if (tag == X86_BUS_SPACE_IO) {
920 if (addr1 >= addr2) {
921 /* src after dest: copy forward */
922 for (; count != 0; count--, addr1 += 4, addr2 += 4)
923 outl(addr2, inl(addr1));
925 /* dest after src: copy backwards */
926 for (addr1 += 4 * (count - 1), addr2 += 4 * (count - 1);
927 count != 0; count--, addr1 -= 4, addr2 -= 4)
928 outl(addr2, inl(addr1));
931 if (addr1 >= addr2) {
932 /* src after dest: copy forward */
933 for (; count != 0; count--, addr1 += 4, addr2 += 4)
934 *(volatile u_int32_t *)(addr2) =
935 *(volatile u_int32_t *)(addr1);
937 /* dest after src: copy backwards */
938 for (addr1 += 4 * (count - 1), addr2 += 4 * (count - 1);
939 count != 0; count--, addr1 -= 4, addr2 -= 4)
940 *(volatile u_int32_t *)(addr2) =
941 *(volatile u_int32_t *)(addr1);
946 #if 0 /* Cause a link error for bus_space_copy_8 */
947 #define bus_space_copy_region_8 !!! bus_space_copy_region_8 unimplemented !!!
951 * Bus read/write barrier methods.
953 * void bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t bsh,
954 * bus_size_t offset, bus_size_t len, int flags);
957 * Note that BUS_SPACE_BARRIER_WRITE doesn't do anything other than
958 * prevent reordering by the compiler; all Intel x86 processors currently
959 * retire operations outside the CPU in program order.
962 bus_space_barrier(bus_space_tag_t tag __unused, bus_space_handle_t bsh __unused,
963 bus_size_t offset __unused, bus_size_t len __unused, int flags)
965 if (flags & BUS_SPACE_BARRIER_READ)
967 __asm __volatile("lock; addl $0,0(%%rsp)" : : : "memory");
969 __asm __volatile("lock; addl $0,0(%%esp)" : : : "memory");
975 #ifdef BUS_SPACE_NO_LEGACY
978 #define inb(a) compiler_error
979 #define inw(a) compiler_error
980 #define inl(a) compiler_error
981 #define outb(a, b) compiler_error
982 #define outw(a, b) compiler_error
983 #define outl(a, b) compiler_error
987 * Stream accesses are the same as normal accesses on x86; there are no
988 * supported bus systems with an endianess different from the host one.
990 #define bus_space_read_stream_1(t, h, o) bus_space_read_1((t), (h), (o))
991 #define bus_space_read_stream_2(t, h, o) bus_space_read_2((t), (h), (o))
992 #define bus_space_read_stream_4(t, h, o) bus_space_read_4((t), (h), (o))
994 #define bus_space_read_multi_stream_1(t, h, o, a, c) \
995 bus_space_read_multi_1((t), (h), (o), (a), (c))
996 #define bus_space_read_multi_stream_2(t, h, o, a, c) \
997 bus_space_read_multi_2((t), (h), (o), (a), (c))
998 #define bus_space_read_multi_stream_4(t, h, o, a, c) \
999 bus_space_read_multi_4((t), (h), (o), (a), (c))
1001 #define bus_space_write_stream_1(t, h, o, v) \
1002 bus_space_write_1((t), (h), (o), (v))
1003 #define bus_space_write_stream_2(t, h, o, v) \
1004 bus_space_write_2((t), (h), (o), (v))
1005 #define bus_space_write_stream_4(t, h, o, v) \
1006 bus_space_write_4((t), (h), (o), (v))
1008 #define bus_space_write_multi_stream_1(t, h, o, a, c) \
1009 bus_space_write_multi_1((t), (h), (o), (a), (c))
1010 #define bus_space_write_multi_stream_2(t, h, o, a, c) \
1011 bus_space_write_multi_2((t), (h), (o), (a), (c))
1012 #define bus_space_write_multi_stream_4(t, h, o, a, c) \
1013 bus_space_write_multi_4((t), (h), (o), (a), (c))
1015 #define bus_space_set_multi_stream_1(t, h, o, v, c) \
1016 bus_space_set_multi_1((t), (h), (o), (v), (c))
1017 #define bus_space_set_multi_stream_2(t, h, o, v, c) \
1018 bus_space_set_multi_2((t), (h), (o), (v), (c))
1019 #define bus_space_set_multi_stream_4(t, h, o, v, c) \
1020 bus_space_set_multi_4((t), (h), (o), (v), (c))
1022 #define bus_space_read_region_stream_1(t, h, o, a, c) \
1023 bus_space_read_region_1((t), (h), (o), (a), (c))
1024 #define bus_space_read_region_stream_2(t, h, o, a, c) \
1025 bus_space_read_region_2((t), (h), (o), (a), (c))
1026 #define bus_space_read_region_stream_4(t, h, o, a, c) \
1027 bus_space_read_region_4((t), (h), (o), (a), (c))
1029 #define bus_space_write_region_stream_1(t, h, o, a, c) \
1030 bus_space_write_region_1((t), (h), (o), (a), (c))
1031 #define bus_space_write_region_stream_2(t, h, o, a, c) \
1032 bus_space_write_region_2((t), (h), (o), (a), (c))
1033 #define bus_space_write_region_stream_4(t, h, o, a, c) \
1034 bus_space_write_region_4((t), (h), (o), (a), (c))
1036 #define bus_space_set_region_stream_1(t, h, o, v, c) \
1037 bus_space_set_region_1((t), (h), (o), (v), (c))
1038 #define bus_space_set_region_stream_2(t, h, o, v, c) \
1039 bus_space_set_region_2((t), (h), (o), (v), (c))
1040 #define bus_space_set_region_stream_4(t, h, o, v, c) \
1041 bus_space_set_region_4((t), (h), (o), (v), (c))
1043 #define bus_space_copy_region_stream_1(t, h1, o1, h2, o2, c) \
1044 bus_space_copy_region_1((t), (h1), (o1), (h2), (o2), (c))
1045 #define bus_space_copy_region_stream_2(t, h1, o1, h2, o2, c) \
1046 bus_space_copy_region_2((t), (h1), (o1), (h2), (o2), (c))
1047 #define bus_space_copy_region_stream_4(t, h1, o1, h2, o2, c) \
1048 bus_space_copy_region_4((t), (h1), (o1), (h2), (o2), (c))
1050 #define BUS_PEEK_FUNC(width, type) \
1052 bus_space_peek_##width(bus_space_tag_t tag, \
1053 bus_space_handle_t hnd, bus_size_t offset, type *value) \
1056 tmp = bus_space_read_##width(tag, hnd, offset); \
1057 *value = (type)tmp; \
1060 BUS_PEEK_FUNC(1, uint8_t)
1061 BUS_PEEK_FUNC(2, uint16_t)
1062 BUS_PEEK_FUNC(4, uint32_t)
1064 BUS_PEEK_FUNC(8, uint64_t)
1067 #define BUS_POKE_FUNC(width, type) \
1069 bus_space_poke_##width(bus_space_tag_t tag, \
1070 bus_space_handle_t hnd, bus_size_t offset, type value) \
1072 bus_space_write_##width(tag, hnd, offset, value); \
1075 BUS_POKE_FUNC(1, uint8_t)
1076 BUS_POKE_FUNC(2, uint16_t)
1077 BUS_POKE_FUNC(4, uint32_t)
1079 BUS_POKE_FUNC(8, uint64_t)
1082 #endif /* !SAN_NEEDS_INTERCEPTORS && SAN_RUNTIME */
1084 #endif /* !_MACHINE_BUS_H_ */