]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/include/bus.h
MFV r354582: file 5.37.
[FreeBSD/FreeBSD.git] / sys / x86 / include / bus.h
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-NetBSDE
3  *
4  * Copyright (c) KATO Takenori, 1999.
5  *
6  * All rights reserved.  Unpublished rights reserved under the copyright
7  * laws of Japan.
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  * 
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.
21  * 
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.
32  *
33  * $FreeBSD$
34  */
35
36 /*      $NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $    */
37
38 /*-
39  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
40  * All rights reserved.
41  *
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.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
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.
54  *
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.
66  */
67
68 /*-
69  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
70  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions
74  * are met:
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
86  *
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.
97  */
98
99 #ifndef _X86_BUS_H_
100 #define _X86_BUS_H_
101
102 #include <machine/_bus.h>
103 #include <machine/cpufunc.h>
104
105 #ifndef __GNUCLIKE_ASM
106 #error "no assembler code for your compiler"
107 #endif
108
109 /*
110  * Values for the x86 bus space tag, not to be used directly by MI code.
111  */
112 #define X86_BUS_SPACE_IO        0       /* space is i/o space */
113 #define X86_BUS_SPACE_MEM       1       /* space is mem space */
114
115 #define BUS_SPACE_MAXSIZE_24BIT 0xFFFFFF
116 #define BUS_SPACE_MAXSIZE_32BIT 0xFFFFFFFF
117 #if defined(__amd64__)
118 #define BUS_SPACE_MAXSIZE       0xFFFFFFFFFFFFFFFFULL
119 #else
120 #define BUS_SPACE_MAXSIZE       0xFFFFFFFF
121 #endif
122 #define BUS_SPACE_MAXADDR_24BIT 0xFFFFFF
123 #define BUS_SPACE_MAXADDR_32BIT 0xFFFFFFFF
124 #if defined(__amd64__) || defined(PAE)
125 #define BUS_SPACE_MAXADDR_48BIT 0xFFFFFFFFFFFFULL
126 #define BUS_SPACE_MAXADDR       0xFFFFFFFFFFFFFFFFULL
127 #else
128 #define BUS_SPACE_MAXADDR       0xFFFFFFFF
129 #endif
130
131 #define BUS_SPACE_INVALID_DATA  (~0)
132 #define BUS_SPACE_UNRESTRICTED  (~0)
133
134 /*
135  * Map a region of device bus space into CPU virtual address space.
136  */
137
138 int bus_space_map(bus_space_tag_t tag, bus_addr_t addr, bus_size_t size,
139     int flags, bus_space_handle_t *bshp);
140
141 /*
142  * Unmap a region of device bus space.
143  */
144
145 void bus_space_unmap(bus_space_tag_t tag, bus_space_handle_t bsh,
146     bus_size_t size);
147
148 /*
149  * Get a new handle for a subregion of an already-mapped area of bus space.
150  */
151
152 static __inline int bus_space_subregion(bus_space_tag_t t,
153                                         bus_space_handle_t bsh,
154                                         bus_size_t offset, bus_size_t size,
155                                         bus_space_handle_t *nbshp);
156
157 static __inline int
158 bus_space_subregion(bus_space_tag_t t __unused, bus_space_handle_t bsh,
159                     bus_size_t offset, bus_size_t size __unused,
160                     bus_space_handle_t *nbshp)
161 {
162
163         *nbshp = bsh + offset;
164         return (0);
165 }
166
167 /*
168  * Allocate a region of memory that is accessible to devices in bus space.
169  */
170
171 int     bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart,
172                         bus_addr_t rend, bus_size_t size, bus_size_t align,
173                         bus_size_t boundary, int flags, bus_addr_t *addrp,
174                         bus_space_handle_t *bshp);
175
176 /*
177  * Free a region of bus space accessible memory.
178  */
179
180 static __inline void bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh,
181                                     bus_size_t size);
182
183 static __inline void
184 bus_space_free(bus_space_tag_t t __unused, bus_space_handle_t bsh __unused,
185                bus_size_t size __unused)
186 {
187 }
188
189
190 /*
191  * Read a 1, 2, 4, or 8 byte quantity from bus space
192  * described by tag/handle/offset.
193  */
194 static __inline u_int8_t bus_space_read_1(bus_space_tag_t tag,
195                                           bus_space_handle_t handle,
196                                           bus_size_t offset);
197
198 static __inline u_int16_t bus_space_read_2(bus_space_tag_t tag,
199                                            bus_space_handle_t handle,
200                                            bus_size_t offset);
201
202 static __inline u_int32_t bus_space_read_4(bus_space_tag_t tag,
203                                            bus_space_handle_t handle,
204                                            bus_size_t offset);
205
206 #ifdef __amd64__
207 static __inline uint64_t bus_space_read_8(bus_space_tag_t tag,
208                                           bus_space_handle_t handle,
209                                           bus_size_t offset);
210 #endif
211
212 static __inline u_int8_t
213 bus_space_read_1(bus_space_tag_t tag, bus_space_handle_t handle,
214                  bus_size_t offset)
215 {
216
217         if (tag == X86_BUS_SPACE_IO)
218                 return (inb(handle + offset));
219         return (*(volatile u_int8_t *)(handle + offset));
220 }
221
222 static __inline u_int16_t
223 bus_space_read_2(bus_space_tag_t tag, bus_space_handle_t handle,
224                  bus_size_t offset)
225 {
226
227         if (tag == X86_BUS_SPACE_IO)
228                 return (inw(handle + offset));
229         return (*(volatile u_int16_t *)(handle + offset));
230 }
231
232 static __inline u_int32_t
233 bus_space_read_4(bus_space_tag_t tag, bus_space_handle_t handle,
234                  bus_size_t offset)
235 {
236
237         if (tag == X86_BUS_SPACE_IO)
238                 return (inl(handle + offset));
239         return (*(volatile u_int32_t *)(handle + offset));
240 }
241
242 #ifdef __amd64__
243 static __inline uint64_t
244 bus_space_read_8(bus_space_tag_t tag, bus_space_handle_t handle,
245                  bus_size_t offset)
246 {
247
248         if (tag == X86_BUS_SPACE_IO) /* No 8 byte IO space access on x86 */
249                 return (BUS_SPACE_INVALID_DATA);
250         return (*(volatile uint64_t *)(handle + offset));
251 }
252 #endif
253
254 /*
255  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
256  * described by tag/handle/offset and copy into buffer provided.
257  */
258 static __inline void bus_space_read_multi_1(bus_space_tag_t tag,
259                                             bus_space_handle_t bsh,
260                                             bus_size_t offset, u_int8_t *addr,
261                                             size_t count);
262
263 static __inline void bus_space_read_multi_2(bus_space_tag_t tag,
264                                             bus_space_handle_t bsh,
265                                             bus_size_t offset, u_int16_t *addr,
266                                             size_t count);
267
268 static __inline void bus_space_read_multi_4(bus_space_tag_t tag,
269                                             bus_space_handle_t bsh,
270                                             bus_size_t offset, u_int32_t *addr,
271                                             size_t count);
272
273 static __inline void
274 bus_space_read_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
275                        bus_size_t offset, u_int8_t *addr, size_t count)
276 {
277
278         if (tag == X86_BUS_SPACE_IO)
279                 insb(bsh + offset, addr, count);
280         else {
281 #ifdef __GNUCLIKE_ASM
282                 __asm __volatile("                              \n\
283                 1:      movb (%2),%%al                          \n\
284                         stosb                                   \n\
285                         loop 1b"                                :
286                     "=D" (addr), "=c" (count)                   :
287                     "r" (bsh + offset), "0" (addr), "1" (count) :
288                     "%eax", "memory");
289 #endif
290         }
291 }
292
293 static __inline void
294 bus_space_read_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
295                        bus_size_t offset, u_int16_t *addr, size_t count)
296 {
297
298         if (tag == X86_BUS_SPACE_IO)
299                 insw(bsh + offset, addr, count);
300         else {
301 #ifdef __GNUCLIKE_ASM
302                 __asm __volatile("                              \n\
303                 1:      movw (%2),%%ax                          \n\
304                         stosw                                   \n\
305                         loop 1b"                                :
306                     "=D" (addr), "=c" (count)                   :
307                     "r" (bsh + offset), "0" (addr), "1" (count) :
308                     "%eax", "memory");
309 #endif
310         }
311 }
312
313 static __inline void
314 bus_space_read_multi_4(bus_space_tag_t tag, bus_space_handle_t bsh,
315                        bus_size_t offset, u_int32_t *addr, size_t count)
316 {
317
318         if (tag == X86_BUS_SPACE_IO)
319                 insl(bsh + offset, addr, count);
320         else {
321 #ifdef __GNUCLIKE_ASM
322                 __asm __volatile("                              \n\
323                 1:      movl (%2),%%eax                         \n\
324                         stosl                                   \n\
325                         loop 1b"                                :
326                     "=D" (addr), "=c" (count)                   :
327                     "r" (bsh + offset), "0" (addr), "1" (count) :
328                     "%eax", "memory");
329 #endif
330         }
331 }
332
333 #if 0   /* Cause a link error for bus_space_read_multi_8 */
334 #define bus_space_read_multi_8  !!! bus_space_read_multi_8 unimplemented !!!
335 #endif
336
337 /*
338  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
339  * described by tag/handle and starting at `offset' and copy into
340  * buffer provided.
341  */
342 static __inline void bus_space_read_region_1(bus_space_tag_t tag,
343                                              bus_space_handle_t bsh,
344                                              bus_size_t offset, u_int8_t *addr,
345                                              size_t count);
346
347 static __inline void bus_space_read_region_2(bus_space_tag_t tag,
348                                              bus_space_handle_t bsh,
349                                              bus_size_t offset, u_int16_t *addr,
350                                              size_t count);
351
352 static __inline void bus_space_read_region_4(bus_space_tag_t tag,
353                                              bus_space_handle_t bsh,
354                                              bus_size_t offset, u_int32_t *addr,
355                                              size_t count);
356
357
358 static __inline void
359 bus_space_read_region_1(bus_space_tag_t tag, bus_space_handle_t bsh,
360                         bus_size_t offset, u_int8_t *addr, size_t count)
361 {
362
363         if (tag == X86_BUS_SPACE_IO) {
364                 int _port_ = bsh + offset;
365 #ifdef __GNUCLIKE_ASM
366                 __asm __volatile("                              \n\
367                 1:      inb %w2,%%al                            \n\
368                         stosb                                   \n\
369                         incl %2                                 \n\
370                         loop 1b"                                :
371                     "=D" (addr), "=c" (count), "=d" (_port_)    :
372                     "0" (addr), "1" (count), "2" (_port_)       :
373                     "%eax", "memory", "cc");
374 #endif
375         } else {
376                 bus_space_handle_t _port_ = bsh + offset;
377 #ifdef __GNUCLIKE_ASM
378                 __asm __volatile("                              \n\
379                         repne                                   \n\
380                         movsb"                                  :
381                     "=D" (addr), "=c" (count), "=S" (_port_)    :
382                     "0" (addr), "1" (count), "2" (_port_)       :
383                     "memory", "cc");
384 #endif
385         }
386 }
387
388 static __inline void
389 bus_space_read_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
390                         bus_size_t offset, u_int16_t *addr, size_t count)
391 {
392
393         if (tag == X86_BUS_SPACE_IO) {
394                 int _port_ = bsh + offset;
395 #ifdef __GNUCLIKE_ASM
396                 __asm __volatile("                              \n\
397                 1:      inw %w2,%%ax                            \n\
398                         stosw                                   \n\
399                         addl $2,%2                              \n\
400                         loop 1b"                                :
401                     "=D" (addr), "=c" (count), "=d" (_port_)    :
402                     "0" (addr), "1" (count), "2" (_port_)       :
403                     "%eax", "memory", "cc");
404 #endif
405         } else {
406                 bus_space_handle_t _port_ = bsh + offset;
407 #ifdef __GNUCLIKE_ASM
408                 __asm __volatile("                              \n\
409                         repne                                   \n\
410                         movsw"                                  :
411                     "=D" (addr), "=c" (count), "=S" (_port_)    :
412                     "0" (addr), "1" (count), "2" (_port_)       :
413                     "memory", "cc");
414 #endif
415         }
416 }
417
418 static __inline void
419 bus_space_read_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
420                         bus_size_t offset, u_int32_t *addr, size_t count)
421 {
422
423         if (tag == X86_BUS_SPACE_IO) {
424                 int _port_ = bsh + offset;
425 #ifdef __GNUCLIKE_ASM
426                 __asm __volatile("                              \n\
427                 1:      inl %w2,%%eax                           \n\
428                         stosl                                   \n\
429                         addl $4,%2                              \n\
430                         loop 1b"                                :
431                     "=D" (addr), "=c" (count), "=d" (_port_)    :
432                     "0" (addr), "1" (count), "2" (_port_)       :
433                     "%eax", "memory", "cc");
434 #endif
435         } else {
436                 bus_space_handle_t _port_ = bsh + offset;
437 #ifdef __GNUCLIKE_ASM
438                 __asm __volatile("                              \n\
439                         repne                                   \n\
440                         movsl"                                  :
441                     "=D" (addr), "=c" (count), "=S" (_port_)    :
442                     "0" (addr), "1" (count), "2" (_port_)       :
443                     "memory", "cc");
444 #endif
445         }
446 }
447
448 #if 0   /* Cause a link error for bus_space_read_region_8 */
449 #define bus_space_read_region_8 !!! bus_space_read_region_8 unimplemented !!!
450 #endif
451
452 /*
453  * Write the 1, 2, 4, or 8 byte value `value' to bus space
454  * described by tag/handle/offset.
455  */
456
457 static __inline void bus_space_write_1(bus_space_tag_t tag,
458                                        bus_space_handle_t bsh,
459                                        bus_size_t offset, u_int8_t value);
460
461 static __inline void bus_space_write_2(bus_space_tag_t tag,
462                                        bus_space_handle_t bsh,
463                                        bus_size_t offset, u_int16_t value);
464
465 static __inline void bus_space_write_4(bus_space_tag_t tag,
466                                        bus_space_handle_t bsh,
467                                        bus_size_t offset, u_int32_t value);
468
469 #ifdef __amd64__
470 static __inline void bus_space_write_8(bus_space_tag_t tag,
471                                        bus_space_handle_t bsh,
472                                        bus_size_t offset, uint64_t value);
473 #endif
474
475 static __inline void
476 bus_space_write_1(bus_space_tag_t tag, bus_space_handle_t bsh,
477                        bus_size_t offset, u_int8_t value)
478 {
479
480         if (tag == X86_BUS_SPACE_IO)
481                 outb(bsh + offset, value);
482         else
483                 *(volatile u_int8_t *)(bsh + offset) = value;
484 }
485
486 static __inline void
487 bus_space_write_2(bus_space_tag_t tag, bus_space_handle_t bsh,
488                        bus_size_t offset, u_int16_t value)
489 {
490
491         if (tag == X86_BUS_SPACE_IO)
492                 outw(bsh + offset, value);
493         else
494                 *(volatile u_int16_t *)(bsh + offset) = value;
495 }
496
497 static __inline void
498 bus_space_write_4(bus_space_tag_t tag, bus_space_handle_t bsh,
499                        bus_size_t offset, u_int32_t value)
500 {
501
502         if (tag == X86_BUS_SPACE_IO)
503                 outl(bsh + offset, value);
504         else
505                 *(volatile u_int32_t *)(bsh + offset) = value;
506 }
507
508 #ifdef __amd64__
509 static __inline void
510 bus_space_write_8(bus_space_tag_t tag, bus_space_handle_t bsh,
511                   bus_size_t offset, uint64_t value)
512 {
513
514         if (tag == X86_BUS_SPACE_IO) /* No 8 byte IO space access on x86 */
515                 return;
516         else
517                 *(volatile uint64_t *)(bsh + offset) = value;
518 }
519 #endif
520
521 /*
522  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer
523  * provided to bus space described by tag/handle/offset.
524  */
525
526 static __inline void bus_space_write_multi_1(bus_space_tag_t tag,
527                                              bus_space_handle_t bsh,
528                                              bus_size_t offset,
529                                              const u_int8_t *addr,
530                                              size_t count);
531 static __inline void bus_space_write_multi_2(bus_space_tag_t tag,
532                                              bus_space_handle_t bsh,
533                                              bus_size_t offset,
534                                              const u_int16_t *addr,
535                                              size_t count);
536
537 static __inline void bus_space_write_multi_4(bus_space_tag_t tag,
538                                              bus_space_handle_t bsh,
539                                              bus_size_t offset,
540                                              const u_int32_t *addr,
541                                              size_t count);
542
543 static __inline void
544 bus_space_write_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
545                         bus_size_t offset, const u_int8_t *addr, size_t count)
546 {
547
548         if (tag == X86_BUS_SPACE_IO)
549                 outsb(bsh + offset, addr, count);
550         else {
551 #ifdef __GNUCLIKE_ASM
552                 __asm __volatile("                              \n\
553                 1:      lodsb                                   \n\
554                         movb %%al,(%2)                          \n\
555                         loop 1b"                                :
556                     "=S" (addr), "=c" (count)                   :
557                     "r" (bsh + offset), "0" (addr), "1" (count) :
558                     "%eax", "memory", "cc");
559 #endif
560         }
561 }
562
563 static __inline void
564 bus_space_write_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
565                         bus_size_t offset, const u_int16_t *addr, size_t count)
566 {
567
568         if (tag == X86_BUS_SPACE_IO)
569                 outsw(bsh + offset, addr, count);
570         else {
571 #ifdef __GNUCLIKE_ASM
572                 __asm __volatile("                              \n\
573                 1:      lodsw                                   \n\
574                         movw %%ax,(%2)                          \n\
575                         loop 1b"                                :
576                     "=S" (addr), "=c" (count)                   :
577                     "r" (bsh + offset), "0" (addr), "1" (count) :
578                     "%eax", "memory", "cc");
579 #endif
580         }
581 }
582
583 static __inline void
584 bus_space_write_multi_4(bus_space_tag_t tag, bus_space_handle_t bsh,
585                         bus_size_t offset, const u_int32_t *addr, size_t count)
586 {
587
588         if (tag == X86_BUS_SPACE_IO)
589                 outsl(bsh + offset, addr, count);
590         else {
591 #ifdef __GNUCLIKE_ASM
592                 __asm __volatile("                              \n\
593                 1:      lodsl                                   \n\
594                         movl %%eax,(%2)                         \n\
595                         loop 1b"                                :
596                     "=S" (addr), "=c" (count)                   :
597                     "r" (bsh + offset), "0" (addr), "1" (count) :
598                     "%eax", "memory", "cc");
599 #endif
600         }
601 }
602
603 #if 0   /* Cause a link error for bus_space_write_multi_8 */
604 #define bus_space_write_multi_8(t, h, o, a, c)                          \
605                         !!! bus_space_write_multi_8 unimplemented !!!
606 #endif
607
608 /*
609  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided
610  * to bus space described by tag/handle starting at `offset'.
611  */
612
613 static __inline void bus_space_write_region_1(bus_space_tag_t tag,
614                                               bus_space_handle_t bsh,
615                                               bus_size_t offset,
616                                               const u_int8_t *addr,
617                                               size_t count);
618 static __inline void bus_space_write_region_2(bus_space_tag_t tag,
619                                               bus_space_handle_t bsh,
620                                               bus_size_t offset,
621                                               const u_int16_t *addr,
622                                               size_t count);
623 static __inline void bus_space_write_region_4(bus_space_tag_t tag,
624                                               bus_space_handle_t bsh,
625                                               bus_size_t offset,
626                                               const u_int32_t *addr,
627                                               size_t count);
628
629 static __inline void
630 bus_space_write_region_1(bus_space_tag_t tag, bus_space_handle_t bsh,
631                          bus_size_t offset, const u_int8_t *addr, size_t count)
632 {
633
634         if (tag == X86_BUS_SPACE_IO) {
635                 int _port_ = bsh + offset;
636 #ifdef __GNUCLIKE_ASM
637                 __asm __volatile("                              \n\
638                 1:      lodsb                                   \n\
639                         outb %%al,%w0                           \n\
640                         incl %0                                 \n\
641                         loop 1b"                                :
642                     "=d" (_port_), "=S" (addr), "=c" (count)    :
643                     "0" (_port_), "1" (addr), "2" (count)       :
644                     "%eax", "memory", "cc");
645 #endif
646         } else {
647                 bus_space_handle_t _port_ = bsh + offset;
648 #ifdef __GNUCLIKE_ASM
649                 __asm __volatile("                              \n\
650                         repne                                   \n\
651                         movsb"                                  :
652                     "=D" (_port_), "=S" (addr), "=c" (count)    :
653                     "0" (_port_), "1" (addr), "2" (count)       :
654                     "memory", "cc");
655 #endif
656         }
657 }
658
659 static __inline void
660 bus_space_write_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
661                          bus_size_t offset, const u_int16_t *addr, size_t count)
662 {
663
664         if (tag == X86_BUS_SPACE_IO) {
665                 int _port_ = bsh + offset;
666 #ifdef __GNUCLIKE_ASM
667                 __asm __volatile("                              \n\
668                 1:      lodsw                                   \n\
669                         outw %%ax,%w0                           \n\
670                         addl $2,%0                              \n\
671                         loop 1b"                                :
672                     "=d" (_port_), "=S" (addr), "=c" (count)    :
673                     "0" (_port_), "1" (addr), "2" (count)       :
674                     "%eax", "memory", "cc");
675 #endif
676         } else {
677                 bus_space_handle_t _port_ = bsh + offset;
678 #ifdef __GNUCLIKE_ASM
679                 __asm __volatile("                              \n\
680                         repne                                   \n\
681                         movsw"                                  :
682                     "=D" (_port_), "=S" (addr), "=c" (count)    :
683                     "0" (_port_), "1" (addr), "2" (count)       :
684                     "memory", "cc");
685 #endif
686         }
687 }
688
689 static __inline void
690 bus_space_write_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
691                          bus_size_t offset, const u_int32_t *addr, size_t count)
692 {
693
694         if (tag == X86_BUS_SPACE_IO) {
695                 int _port_ = bsh + offset;
696 #ifdef __GNUCLIKE_ASM
697                 __asm __volatile("                              \n\
698                 1:      lodsl                                   \n\
699                         outl %%eax,%w0                          \n\
700                         addl $4,%0                              \n\
701                         loop 1b"                                :
702                     "=d" (_port_), "=S" (addr), "=c" (count)    :
703                     "0" (_port_), "1" (addr), "2" (count)       :
704                     "%eax", "memory", "cc");
705 #endif
706         } else {
707                 bus_space_handle_t _port_ = bsh + offset;
708 #ifdef __GNUCLIKE_ASM
709                 __asm __volatile("                              \n\
710                         repne                                   \n\
711                         movsl"                                  :
712                     "=D" (_port_), "=S" (addr), "=c" (count)    :
713                     "0" (_port_), "1" (addr), "2" (count)       :
714                     "memory", "cc");
715 #endif
716         }
717 }
718
719 #if 0   /* Cause a link error for bus_space_write_region_8 */
720 #define bus_space_write_region_8                                        \
721                         !!! bus_space_write_region_8 unimplemented !!!
722 #endif
723
724 /*
725  * Write the 1, 2, 4, or 8 byte value `val' to bus space described
726  * by tag/handle/offset `count' times.
727  */
728
729 static __inline void bus_space_set_multi_1(bus_space_tag_t tag,
730                                            bus_space_handle_t bsh,
731                                            bus_size_t offset,
732                                            u_int8_t value, size_t count);
733 static __inline void bus_space_set_multi_2(bus_space_tag_t tag,
734                                            bus_space_handle_t bsh,
735                                            bus_size_t offset,
736                                            u_int16_t value, size_t count);
737 static __inline void bus_space_set_multi_4(bus_space_tag_t tag,
738                                            bus_space_handle_t bsh,
739                                            bus_size_t offset,
740                                            u_int32_t value, size_t count);
741
742 static __inline void
743 bus_space_set_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
744                       bus_size_t offset, u_int8_t value, size_t count)
745 {
746         bus_space_handle_t addr = bsh + offset;
747
748         if (tag == X86_BUS_SPACE_IO)
749                 while (count--)
750                         outb(addr, value);
751         else
752                 while (count--)
753                         *(volatile u_int8_t *)(addr) = value;
754 }
755
756 static __inline void
757 bus_space_set_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
758                      bus_size_t offset, u_int16_t value, size_t count)
759 {
760         bus_space_handle_t addr = bsh + offset;
761
762         if (tag == X86_BUS_SPACE_IO)
763                 while (count--)
764                         outw(addr, value);
765         else
766                 while (count--)
767                         *(volatile u_int16_t *)(addr) = value;
768 }
769
770 static __inline void
771 bus_space_set_multi_4(bus_space_tag_t tag, bus_space_handle_t bsh,
772                       bus_size_t offset, u_int32_t value, size_t count)
773 {
774         bus_space_handle_t addr = bsh + offset;
775
776         if (tag == X86_BUS_SPACE_IO)
777                 while (count--)
778                         outl(addr, value);
779         else
780                 while (count--)
781                         *(volatile u_int32_t *)(addr) = value;
782 }
783
784 #if 0   /* Cause a link error for bus_space_set_multi_8 */
785 #define bus_space_set_multi_8 !!! bus_space_set_multi_8 unimplemented !!!
786 #endif
787
788 /*
789  * Write `count' 1, 2, 4, or 8 byte value `val' to bus space described
790  * by tag/handle starting at `offset'.
791  */
792
793 static __inline void bus_space_set_region_1(bus_space_tag_t tag,
794                                             bus_space_handle_t bsh,
795                                             bus_size_t offset, u_int8_t value,
796                                             size_t count);
797 static __inline void bus_space_set_region_2(bus_space_tag_t tag,
798                                             bus_space_handle_t bsh,
799                                             bus_size_t offset, u_int16_t value,
800                                             size_t count);
801 static __inline void bus_space_set_region_4(bus_space_tag_t tag,
802                                             bus_space_handle_t bsh,
803                                             bus_size_t offset, u_int32_t value,
804                                             size_t count);
805
806 static __inline void
807 bus_space_set_region_1(bus_space_tag_t tag, bus_space_handle_t bsh,
808                        bus_size_t offset, u_int8_t value, size_t count)
809 {
810         bus_space_handle_t addr = bsh + offset;
811
812         if (tag == X86_BUS_SPACE_IO)
813                 for (; count != 0; count--, addr++)
814                         outb(addr, value);
815         else
816                 for (; count != 0; count--, addr++)
817                         *(volatile u_int8_t *)(addr) = value;
818 }
819
820 static __inline void
821 bus_space_set_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
822                        bus_size_t offset, u_int16_t value, size_t count)
823 {
824         bus_space_handle_t addr = bsh + offset;
825
826         if (tag == X86_BUS_SPACE_IO)
827                 for (; count != 0; count--, addr += 2)
828                         outw(addr, value);
829         else
830                 for (; count != 0; count--, addr += 2)
831                         *(volatile u_int16_t *)(addr) = value;
832 }
833
834 static __inline void
835 bus_space_set_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
836                        bus_size_t offset, u_int32_t value, size_t count)
837 {
838         bus_space_handle_t addr = bsh + offset;
839
840         if (tag == X86_BUS_SPACE_IO)
841                 for (; count != 0; count--, addr += 4)
842                         outl(addr, value);
843         else
844                 for (; count != 0; count--, addr += 4)
845                         *(volatile u_int32_t *)(addr) = value;
846 }
847
848 #if 0   /* Cause a link error for bus_space_set_region_8 */
849 #define bus_space_set_region_8  !!! bus_space_set_region_8 unimplemented !!!
850 #endif
851
852 /*
853  * Copy `count' 1, 2, 4, or 8 byte values from bus space starting
854  * at tag/bsh1/off1 to bus space starting at tag/bsh2/off2.
855  */
856
857 static __inline void bus_space_copy_region_1(bus_space_tag_t tag,
858                                              bus_space_handle_t bsh1,
859                                              bus_size_t off1,
860                                              bus_space_handle_t bsh2,
861                                              bus_size_t off2, size_t count);
862
863 static __inline void bus_space_copy_region_2(bus_space_tag_t tag,
864                                              bus_space_handle_t bsh1,
865                                              bus_size_t off1,
866                                              bus_space_handle_t bsh2,
867                                              bus_size_t off2, size_t count);
868
869 static __inline void bus_space_copy_region_4(bus_space_tag_t tag,
870                                              bus_space_handle_t bsh1,
871                                              bus_size_t off1,
872                                              bus_space_handle_t bsh2,
873                                              bus_size_t off2, size_t count);
874
875 static __inline void
876 bus_space_copy_region_1(bus_space_tag_t tag, bus_space_handle_t bsh1,
877                         bus_size_t off1, bus_space_handle_t bsh2,
878                         bus_size_t off2, size_t count)
879 {
880         bus_space_handle_t addr1 = bsh1 + off1;
881         bus_space_handle_t addr2 = bsh2 + off2;
882
883         if (tag == X86_BUS_SPACE_IO) {
884                 if (addr1 >= addr2) {
885                         /* src after dest: copy forward */
886                         for (; count != 0; count--, addr1++, addr2++)
887                                 outb(addr2, inb(addr1));
888                 } else {
889                         /* dest after src: copy backwards */
890                         for (addr1 += (count - 1), addr2 += (count - 1);
891                             count != 0; count--, addr1--, addr2--)
892                                 outb(addr2, inb(addr1));
893                 }
894         } else {
895                 if (addr1 >= addr2) {
896                         /* src after dest: copy forward */
897                         for (; count != 0; count--, addr1++, addr2++)
898                                 *(volatile u_int8_t *)(addr2) =
899                                     *(volatile u_int8_t *)(addr1);
900                 } else {
901                         /* dest after src: copy backwards */
902                         for (addr1 += (count - 1), addr2 += (count - 1);
903                             count != 0; count--, addr1--, addr2--)
904                                 *(volatile u_int8_t *)(addr2) =
905                                     *(volatile u_int8_t *)(addr1);
906                 }
907         }
908 }
909
910 static __inline void
911 bus_space_copy_region_2(bus_space_tag_t tag, bus_space_handle_t bsh1,
912                         bus_size_t off1, bus_space_handle_t bsh2,
913                         bus_size_t off2, size_t count)
914 {
915         bus_space_handle_t addr1 = bsh1 + off1;
916         bus_space_handle_t addr2 = bsh2 + off2;
917
918         if (tag == X86_BUS_SPACE_IO) {
919                 if (addr1 >= addr2) {
920                         /* src after dest: copy forward */
921                         for (; count != 0; count--, addr1 += 2, addr2 += 2)
922                                 outw(addr2, inw(addr1));
923                 } else {
924                         /* dest after src: copy backwards */
925                         for (addr1 += 2 * (count - 1), addr2 += 2 * (count - 1);
926                             count != 0; count--, addr1 -= 2, addr2 -= 2)
927                                 outw(addr2, inw(addr1));
928                 }
929         } else {
930                 if (addr1 >= addr2) {
931                         /* src after dest: copy forward */
932                         for (; count != 0; count--, addr1 += 2, addr2 += 2)
933                                 *(volatile u_int16_t *)(addr2) =
934                                     *(volatile u_int16_t *)(addr1);
935                 } else {
936                         /* dest after src: copy backwards */
937                         for (addr1 += 2 * (count - 1), addr2 += 2 * (count - 1);
938                             count != 0; count--, addr1 -= 2, addr2 -= 2)
939                                 *(volatile u_int16_t *)(addr2) =
940                                     *(volatile u_int16_t *)(addr1);
941                 }
942         }
943 }
944
945 static __inline void
946 bus_space_copy_region_4(bus_space_tag_t tag, bus_space_handle_t bsh1,
947                         bus_size_t off1, bus_space_handle_t bsh2,
948                         bus_size_t off2, size_t count)
949 {
950         bus_space_handle_t addr1 = bsh1 + off1;
951         bus_space_handle_t addr2 = bsh2 + off2;
952
953         if (tag == X86_BUS_SPACE_IO) {
954                 if (addr1 >= addr2) {
955                         /* src after dest: copy forward */
956                         for (; count != 0; count--, addr1 += 4, addr2 += 4)
957                                 outl(addr2, inl(addr1));
958                 } else {
959                         /* dest after src: copy backwards */
960                         for (addr1 += 4 * (count - 1), addr2 += 4 * (count - 1);
961                             count != 0; count--, addr1 -= 4, addr2 -= 4)
962                                 outl(addr2, inl(addr1));
963                 }
964         } else {
965                 if (addr1 >= addr2) {
966                         /* src after dest: copy forward */
967                         for (; count != 0; count--, addr1 += 4, addr2 += 4)
968                                 *(volatile u_int32_t *)(addr2) =
969                                     *(volatile u_int32_t *)(addr1);
970                 } else {
971                         /* dest after src: copy backwards */
972                         for (addr1 += 4 * (count - 1), addr2 += 4 * (count - 1);
973                             count != 0; count--, addr1 -= 4, addr2 -= 4)
974                                 *(volatile u_int32_t *)(addr2) =
975                                     *(volatile u_int32_t *)(addr1);
976                 }
977         }
978 }
979
980 #if 0   /* Cause a link error for bus_space_copy_8 */
981 #define bus_space_copy_region_8 !!! bus_space_copy_region_8 unimplemented !!!
982 #endif
983
984 /*
985  * Bus read/write barrier methods.
986  *
987  *      void bus_space_barrier(bus_space_tag_t tag, bus_space_handle_t bsh,
988  *                             bus_size_t offset, bus_size_t len, int flags);
989  *
990  *
991  * Note that BUS_SPACE_BARRIER_WRITE doesn't do anything other than
992  * prevent reordering by the compiler; all Intel x86 processors currently
993  * retire operations outside the CPU in program order.
994  */
995 #define BUS_SPACE_BARRIER_READ  0x01            /* force read barrier */
996 #define BUS_SPACE_BARRIER_WRITE 0x02            /* force write barrier */
997
998 static __inline void
999 bus_space_barrier(bus_space_tag_t tag __unused, bus_space_handle_t bsh __unused,
1000                   bus_size_t offset __unused, bus_size_t len __unused, int flags)
1001 {
1002 #ifdef __GNUCLIKE_ASM
1003         if (flags & BUS_SPACE_BARRIER_READ)
1004 #ifdef __amd64__
1005                 __asm __volatile("lock; addl $0,0(%%rsp)" : : : "memory");
1006 #else
1007                 __asm __volatile("lock; addl $0,0(%%esp)" : : : "memory");
1008 #endif
1009         else
1010                 __compiler_membar();
1011 #endif
1012 }
1013
1014 #ifdef BUS_SPACE_NO_LEGACY
1015 #undef inb
1016 #undef outb
1017 #define inb(a) compiler_error
1018 #define inw(a) compiler_error
1019 #define inl(a) compiler_error
1020 #define outb(a, b) compiler_error
1021 #define outw(a, b) compiler_error
1022 #define outl(a, b) compiler_error
1023 #endif
1024
1025 #include <machine/bus_dma.h>
1026
1027 /*
1028  * Stream accesses are the same as normal accesses on x86; there are no
1029  * supported bus systems with an endianess different from the host one.
1030  */
1031 #define bus_space_read_stream_1(t, h, o)        bus_space_read_1((t), (h), (o))
1032 #define bus_space_read_stream_2(t, h, o)        bus_space_read_2((t), (h), (o))
1033 #define bus_space_read_stream_4(t, h, o)        bus_space_read_4((t), (h), (o))
1034
1035 #define bus_space_read_multi_stream_1(t, h, o, a, c) \
1036         bus_space_read_multi_1((t), (h), (o), (a), (c))
1037 #define bus_space_read_multi_stream_2(t, h, o, a, c) \
1038         bus_space_read_multi_2((t), (h), (o), (a), (c))
1039 #define bus_space_read_multi_stream_4(t, h, o, a, c) \
1040         bus_space_read_multi_4((t), (h), (o), (a), (c))
1041
1042 #define bus_space_write_stream_1(t, h, o, v) \
1043         bus_space_write_1((t), (h), (o), (v))
1044 #define bus_space_write_stream_2(t, h, o, v) \
1045         bus_space_write_2((t), (h), (o), (v))
1046 #define bus_space_write_stream_4(t, h, o, v) \
1047         bus_space_write_4((t), (h), (o), (v))
1048
1049 #define bus_space_write_multi_stream_1(t, h, o, a, c) \
1050         bus_space_write_multi_1((t), (h), (o), (a), (c))
1051 #define bus_space_write_multi_stream_2(t, h, o, a, c) \
1052         bus_space_write_multi_2((t), (h), (o), (a), (c))
1053 #define bus_space_write_multi_stream_4(t, h, o, a, c) \
1054         bus_space_write_multi_4((t), (h), (o), (a), (c))
1055
1056 #define bus_space_set_multi_stream_1(t, h, o, v, c) \
1057         bus_space_set_multi_1((t), (h), (o), (v), (c))
1058 #define bus_space_set_multi_stream_2(t, h, o, v, c) \
1059         bus_space_set_multi_2((t), (h), (o), (v), (c))
1060 #define bus_space_set_multi_stream_4(t, h, o, v, c) \
1061         bus_space_set_multi_4((t), (h), (o), (v), (c))
1062
1063 #define bus_space_read_region_stream_1(t, h, o, a, c) \
1064         bus_space_read_region_1((t), (h), (o), (a), (c))
1065 #define bus_space_read_region_stream_2(t, h, o, a, c) \
1066         bus_space_read_region_2((t), (h), (o), (a), (c))
1067 #define bus_space_read_region_stream_4(t, h, o, a, c) \
1068         bus_space_read_region_4((t), (h), (o), (a), (c))
1069
1070 #define bus_space_write_region_stream_1(t, h, o, a, c) \
1071         bus_space_write_region_1((t), (h), (o), (a), (c))
1072 #define bus_space_write_region_stream_2(t, h, o, a, c) \
1073         bus_space_write_region_2((t), (h), (o), (a), (c))
1074 #define bus_space_write_region_stream_4(t, h, o, a, c) \
1075         bus_space_write_region_4((t), (h), (o), (a), (c))
1076
1077 #define bus_space_set_region_stream_1(t, h, o, v, c) \
1078         bus_space_set_region_1((t), (h), (o), (v), (c))
1079 #define bus_space_set_region_stream_2(t, h, o, v, c) \
1080         bus_space_set_region_2((t), (h), (o), (v), (c))
1081 #define bus_space_set_region_stream_4(t, h, o, v, c) \
1082         bus_space_set_region_4((t), (h), (o), (v), (c))
1083
1084 #define bus_space_copy_region_stream_1(t, h1, o1, h2, o2, c) \
1085         bus_space_copy_region_1((t), (h1), (o1), (h2), (o2), (c))
1086 #define bus_space_copy_region_stream_2(t, h1, o1, h2, o2, c) \
1087         bus_space_copy_region_2((t), (h1), (o1), (h2), (o2), (c))
1088 #define bus_space_copy_region_stream_4(t, h1, o1, h2, o2, c) \
1089         bus_space_copy_region_4((t), (h1), (o1), (h2), (o2), (c))
1090
1091 #endif /* _X86_BUS_H_ */