]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/x86/include/bus.h
MFC r319414:
[FreeBSD/stable/9.git] / sys / x86 / include / bus.h
1 /*-
2  * Copyright (c) KATO Takenori, 1999.
3  *
4  * All rights reserved.  Unpublished rights reserved under the copyright
5  * laws of Japan.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer as
13  *    the first lines of this file unmodified.
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. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33
34 /*      $NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $    */
35
36 /*-
37  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to The NetBSD Foundation
41  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
42  * NASA Ames Research Center.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63  * POSSIBILITY OF SUCH DAMAGE.
64  */
65
66 /*-
67  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
68  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
69  *
70  * Redistribution and use in source and binary forms, with or without
71  * modification, are permitted provided that the following conditions
72  * are met:
73  * 1. Redistributions of source code must retain the above copyright
74  *    notice, this list of conditions and the following disclaimer.
75  * 2. Redistributions in binary form must reproduce the above copyright
76  *    notice, this list of conditions and the following disclaimer in the
77  *    documentation and/or other materials provided with the distribution.
78  * 3. All advertising materials mentioning features or use of this software
79  *    must display the following acknowledgement:
80  *      This product includes software developed by Christopher G. Demetriou
81  *      for the NetBSD Project.
82  * 4. The name of the author may not be used to endorse or promote products
83  *    derived from this software without specific prior written permission
84  *
85  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
86  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
87  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
88  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
89  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
90  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
91  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
92  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
93  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
94  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95  */
96
97 #ifndef _X86_BUS_H_
98 #define _X86_BUS_H_
99
100 #include <machine/_bus.h>
101 #include <machine/cpufunc.h>
102
103 #ifndef __GNUCLIKE_ASM
104 # ifndef lint
105 #  error "no assembler code for your compiler"
106 # endif
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 #define BUS_SPACE_MAXSIZE       0xFFFFFFFF
118 #define BUS_SPACE_MAXADDR_24BIT 0xFFFFFF
119 #define BUS_SPACE_MAXADDR_32BIT 0xFFFFFFFF
120 #if defined(__amd64__) || defined(PAE)
121 #define BUS_SPACE_MAXADDR       0xFFFFFFFFFFFFFFFFULL
122 #else
123 #define BUS_SPACE_MAXADDR       0xFFFFFFFF
124 #endif
125
126 #define BUS_SPACE_UNRESTRICTED  (~0)
127
128 /*
129  * Map a region of device bus space into CPU virtual address space.
130  */
131
132 static __inline int bus_space_map(bus_space_tag_t t, bus_addr_t addr,
133                                   bus_size_t size, int flags,
134                                   bus_space_handle_t *bshp);
135
136 static __inline int
137 bus_space_map(bus_space_tag_t t __unused, bus_addr_t addr,
138               bus_size_t size __unused, int flags __unused,
139               bus_space_handle_t *bshp)
140 {
141
142         *bshp = addr;
143         return (0);
144 }
145
146 /*
147  * Unmap a region of device bus space.
148  */
149
150 static __inline void bus_space_unmap(bus_space_tag_t t, bus_space_handle_t bsh,
151                                      bus_size_t size);
152
153 static __inline void
154 bus_space_unmap(bus_space_tag_t t __unused, bus_space_handle_t bsh __unused,
155                 bus_size_t size __unused)
156 {
157 }
158
159 /*
160  * Get a new handle for a subregion of an already-mapped area of bus space.
161  */
162
163 static __inline int bus_space_subregion(bus_space_tag_t t,
164                                         bus_space_handle_t bsh,
165                                         bus_size_t offset, bus_size_t size,
166                                         bus_space_handle_t *nbshp);
167
168 static __inline int
169 bus_space_subregion(bus_space_tag_t t __unused, bus_space_handle_t bsh,
170                     bus_size_t offset, bus_size_t size __unused,
171                     bus_space_handle_t *nbshp)
172 {
173
174         *nbshp = bsh + offset;
175         return (0);
176 }
177
178 /*
179  * Allocate a region of memory that is accessible to devices in bus space.
180  */
181
182 int     bus_space_alloc(bus_space_tag_t t, bus_addr_t rstart,
183                         bus_addr_t rend, bus_size_t size, bus_size_t align,
184                         bus_size_t boundary, int flags, bus_addr_t *addrp,
185                         bus_space_handle_t *bshp);
186
187 /*
188  * Free a region of bus space accessible memory.
189  */
190
191 static __inline void bus_space_free(bus_space_tag_t t, bus_space_handle_t bsh,
192                                     bus_size_t size);
193
194 static __inline void
195 bus_space_free(bus_space_tag_t t __unused, bus_space_handle_t bsh __unused,
196                bus_size_t size __unused)
197 {
198 }
199
200
201 /*
202  * Read a 1, 2, 4, or 8 byte quantity from bus space
203  * described by tag/handle/offset.
204  */
205 static __inline u_int8_t bus_space_read_1(bus_space_tag_t tag,
206                                           bus_space_handle_t handle,
207                                           bus_size_t offset);
208
209 static __inline u_int16_t bus_space_read_2(bus_space_tag_t tag,
210                                            bus_space_handle_t handle,
211                                            bus_size_t offset);
212
213 static __inline u_int32_t bus_space_read_4(bus_space_tag_t tag,
214                                            bus_space_handle_t handle,
215                                            bus_size_t offset);
216
217 static __inline u_int8_t
218 bus_space_read_1(bus_space_tag_t tag, bus_space_handle_t handle,
219                  bus_size_t offset)
220 {
221
222         if (tag == X86_BUS_SPACE_IO)
223                 return (inb(handle + offset));
224         return (*(volatile u_int8_t *)(handle + offset));
225 }
226
227 static __inline u_int16_t
228 bus_space_read_2(bus_space_tag_t tag, bus_space_handle_t handle,
229                  bus_size_t offset)
230 {
231
232         if (tag == X86_BUS_SPACE_IO)
233                 return (inw(handle + offset));
234         return (*(volatile u_int16_t *)(handle + offset));
235 }
236
237 static __inline u_int32_t
238 bus_space_read_4(bus_space_tag_t tag, bus_space_handle_t handle,
239                  bus_size_t offset)
240 {
241
242         if (tag == X86_BUS_SPACE_IO)
243                 return (inl(handle + offset));
244         return (*(volatile u_int32_t *)(handle + offset));
245 }
246
247 #if 0   /* Cause a link error for bus_space_read_8 */
248 #define bus_space_read_8(t, h, o)       !!! bus_space_read_8 unimplemented !!!
249 #endif
250
251 /*
252  * Read `count' 1, 2, 4, or 8 byte quantities from bus space
253  * described by tag/handle/offset and copy into buffer provided.
254  */
255 static __inline void bus_space_read_multi_1(bus_space_tag_t tag,
256                                             bus_space_handle_t bsh,
257                                             bus_size_t offset, u_int8_t *addr,
258                                             size_t count);
259
260 static __inline void bus_space_read_multi_2(bus_space_tag_t tag,
261                                             bus_space_handle_t bsh,
262                                             bus_size_t offset, u_int16_t *addr,
263                                             size_t count);
264
265 static __inline void bus_space_read_multi_4(bus_space_tag_t tag,
266                                             bus_space_handle_t bsh,
267                                             bus_size_t offset, u_int32_t *addr,
268                                             size_t count);
269
270 static __inline void
271 bus_space_read_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
272                        bus_size_t offset, u_int8_t *addr, size_t count)
273 {
274
275         if (tag == X86_BUS_SPACE_IO)
276                 insb(bsh + offset, addr, count);
277         else {
278 #ifdef __GNUCLIKE_ASM
279                 __asm __volatile("                              \n\
280                         cld                                     \n\
281                 1:      movb (%2),%%al                          \n\
282                         stosb                                   \n\
283                         loop 1b"                                :
284                     "=D" (addr), "=c" (count)                   :
285                     "r" (bsh + offset), "0" (addr), "1" (count) :
286                     "%eax", "memory");
287 #endif
288         }
289 }
290
291 static __inline void
292 bus_space_read_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
293                        bus_size_t offset, u_int16_t *addr, size_t count)
294 {
295
296         if (tag == X86_BUS_SPACE_IO)
297                 insw(bsh + offset, addr, count);
298         else {
299 #ifdef __GNUCLIKE_ASM
300                 __asm __volatile("                              \n\
301                         cld                                     \n\
302                 1:      movw (%2),%%ax                          \n\
303                         stosw                                   \n\
304                         loop 1b"                                :
305                     "=D" (addr), "=c" (count)                   :
306                     "r" (bsh + offset), "0" (addr), "1" (count) :
307                     "%eax", "memory");
308 #endif
309         }
310 }
311
312 static __inline void
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)
315 {
316
317         if (tag == X86_BUS_SPACE_IO)
318                 insl(bsh + offset, addr, count);
319         else {
320 #ifdef __GNUCLIKE_ASM
321                 __asm __volatile("                              \n\
322                         cld                                     \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                         cld                                     \n\
368                 1:      inb %w2,%%al                            \n\
369                         stosb                                   \n\
370                         incl %2                                 \n\
371                         loop 1b"                                :
372                     "=D" (addr), "=c" (count), "=d" (_port_)    :
373                     "0" (addr), "1" (count), "2" (_port_)       :
374                     "%eax", "memory", "cc");
375 #endif
376         } else {
377                 bus_space_handle_t _port_ = bsh + offset;
378 #ifdef __GNUCLIKE_ASM
379                 __asm __volatile("                              \n\
380                         cld                                     \n\
381                         repne                                   \n\
382                         movsb"                                  :
383                     "=D" (addr), "=c" (count), "=S" (_port_)    :
384                     "0" (addr), "1" (count), "2" (_port_)       :
385                     "memory", "cc");
386 #endif
387         }
388 }
389
390 static __inline void
391 bus_space_read_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
392                         bus_size_t offset, u_int16_t *addr, size_t count)
393 {
394
395         if (tag == X86_BUS_SPACE_IO) {
396                 int _port_ = bsh + offset;
397 #ifdef __GNUCLIKE_ASM
398                 __asm __volatile("                              \n\
399                         cld                                     \n\
400                 1:      inw %w2,%%ax                            \n\
401                         stosw                                   \n\
402                         addl $2,%2                              \n\
403                         loop 1b"                                :
404                     "=D" (addr), "=c" (count), "=d" (_port_)    :
405                     "0" (addr), "1" (count), "2" (_port_)       :
406                     "%eax", "memory", "cc");
407 #endif
408         } else {
409                 bus_space_handle_t _port_ = bsh + offset;
410 #ifdef __GNUCLIKE_ASM
411                 __asm __volatile("                              \n\
412                         cld                                     \n\
413                         repne                                   \n\
414                         movsw"                                  :
415                     "=D" (addr), "=c" (count), "=S" (_port_)    :
416                     "0" (addr), "1" (count), "2" (_port_)       :
417                     "memory", "cc");
418 #endif
419         }
420 }
421
422 static __inline void
423 bus_space_read_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
424                         bus_size_t offset, u_int32_t *addr, size_t count)
425 {
426
427         if (tag == X86_BUS_SPACE_IO) {
428                 int _port_ = bsh + offset;
429 #ifdef __GNUCLIKE_ASM
430                 __asm __volatile("                              \n\
431                         cld                                     \n\
432                 1:      inl %w2,%%eax                           \n\
433                         stosl                                   \n\
434                         addl $4,%2                              \n\
435                         loop 1b"                                :
436                     "=D" (addr), "=c" (count), "=d" (_port_)    :
437                     "0" (addr), "1" (count), "2" (_port_)       :
438                     "%eax", "memory", "cc");
439 #endif
440         } else {
441                 bus_space_handle_t _port_ = bsh + offset;
442 #ifdef __GNUCLIKE_ASM
443                 __asm __volatile("                              \n\
444                         cld                                     \n\
445                         repne                                   \n\
446                         movsl"                                  :
447                     "=D" (addr), "=c" (count), "=S" (_port_)    :
448                     "0" (addr), "1" (count), "2" (_port_)       :
449                     "memory", "cc");
450 #endif
451         }
452 }
453
454 #if 0   /* Cause a link error for bus_space_read_region_8 */
455 #define bus_space_read_region_8 !!! bus_space_read_region_8 unimplemented !!!
456 #endif
457
458 /*
459  * Write the 1, 2, 4, or 8 byte value `value' to bus space
460  * described by tag/handle/offset.
461  */
462
463 static __inline void bus_space_write_1(bus_space_tag_t tag,
464                                        bus_space_handle_t bsh,
465                                        bus_size_t offset, u_int8_t value);
466
467 static __inline void bus_space_write_2(bus_space_tag_t tag,
468                                        bus_space_handle_t bsh,
469                                        bus_size_t offset, u_int16_t value);
470
471 static __inline void bus_space_write_4(bus_space_tag_t tag,
472                                        bus_space_handle_t bsh,
473                                        bus_size_t offset, u_int32_t value);
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 #if 0   /* Cause a link error for bus_space_write_8 */
509 #define bus_space_write_8       !!! bus_space_write_8 not implemented !!!
510 #endif
511
512 /*
513  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer
514  * provided to bus space described by tag/handle/offset.
515  */
516
517 static __inline void bus_space_write_multi_1(bus_space_tag_t tag,
518                                              bus_space_handle_t bsh,
519                                              bus_size_t offset,
520                                              const u_int8_t *addr,
521                                              size_t count);
522 static __inline void bus_space_write_multi_2(bus_space_tag_t tag,
523                                              bus_space_handle_t bsh,
524                                              bus_size_t offset,
525                                              const u_int16_t *addr,
526                                              size_t count);
527
528 static __inline void bus_space_write_multi_4(bus_space_tag_t tag,
529                                              bus_space_handle_t bsh,
530                                              bus_size_t offset,
531                                              const u_int32_t *addr,
532                                              size_t count);
533
534 static __inline void
535 bus_space_write_multi_1(bus_space_tag_t tag, bus_space_handle_t bsh,
536                         bus_size_t offset, const u_int8_t *addr, size_t count)
537 {
538
539         if (tag == X86_BUS_SPACE_IO)
540                 outsb(bsh + offset, addr, count);
541         else {
542 #ifdef __GNUCLIKE_ASM
543                 __asm __volatile("                              \n\
544                         cld                                     \n\
545                 1:      lodsb                                   \n\
546                         movb %%al,(%2)                          \n\
547                         loop 1b"                                :
548                     "=S" (addr), "=c" (count)                   :
549                     "r" (bsh + offset), "0" (addr), "1" (count) :
550                     "%eax", "memory", "cc");
551 #endif
552         }
553 }
554
555 static __inline void
556 bus_space_write_multi_2(bus_space_tag_t tag, bus_space_handle_t bsh,
557                         bus_size_t offset, const u_int16_t *addr, size_t count)
558 {
559
560         if (tag == X86_BUS_SPACE_IO)
561                 outsw(bsh + offset, addr, count);
562         else {
563 #ifdef __GNUCLIKE_ASM
564                 __asm __volatile("                              \n\
565                         cld                                     \n\
566                 1:      lodsw                                   \n\
567                         movw %%ax,(%2)                          \n\
568                         loop 1b"                                :
569                     "=S" (addr), "=c" (count)                   :
570                     "r" (bsh + offset), "0" (addr), "1" (count) :
571                     "%eax", "memory", "cc");
572 #endif
573         }
574 }
575
576 static __inline void
577 bus_space_write_multi_4(bus_space_tag_t tag, bus_space_handle_t bsh,
578                         bus_size_t offset, const u_int32_t *addr, size_t count)
579 {
580
581         if (tag == X86_BUS_SPACE_IO)
582                 outsl(bsh + offset, addr, count);
583         else {
584 #ifdef __GNUCLIKE_ASM
585                 __asm __volatile("                              \n\
586                         cld                                     \n\
587                 1:      lodsl                                   \n\
588                         movl %%eax,(%2)                         \n\
589                         loop 1b"                                :
590                     "=S" (addr), "=c" (count)                   :
591                     "r" (bsh + offset), "0" (addr), "1" (count) :
592                     "%eax", "memory", "cc");
593 #endif
594         }
595 }
596
597 #if 0   /* Cause a link error for bus_space_write_multi_8 */
598 #define bus_space_write_multi_8(t, h, o, a, c)                          \
599                         !!! bus_space_write_multi_8 unimplemented !!!
600 #endif
601
602 /*
603  * Write `count' 1, 2, 4, or 8 byte quantities from the buffer provided
604  * to bus space described by tag/handle starting at `offset'.
605  */
606
607 static __inline void bus_space_write_region_1(bus_space_tag_t tag,
608                                               bus_space_handle_t bsh,
609                                               bus_size_t offset,
610                                               const u_int8_t *addr,
611                                               size_t count);
612 static __inline void bus_space_write_region_2(bus_space_tag_t tag,
613                                               bus_space_handle_t bsh,
614                                               bus_size_t offset,
615                                               const u_int16_t *addr,
616                                               size_t count);
617 static __inline void bus_space_write_region_4(bus_space_tag_t tag,
618                                               bus_space_handle_t bsh,
619                                               bus_size_t offset,
620                                               const u_int32_t *addr,
621                                               size_t count);
622
623 static __inline void
624 bus_space_write_region_1(bus_space_tag_t tag, bus_space_handle_t bsh,
625                          bus_size_t offset, const u_int8_t *addr, size_t count)
626 {
627
628         if (tag == X86_BUS_SPACE_IO) {
629                 int _port_ = bsh + offset;
630 #ifdef __GNUCLIKE_ASM
631                 __asm __volatile("                              \n\
632                         cld                                     \n\
633                 1:      lodsb                                   \n\
634                         outb %%al,%w0                           \n\
635                         incl %0                                 \n\
636                         loop 1b"                                :
637                     "=d" (_port_), "=S" (addr), "=c" (count)    :
638                     "0" (_port_), "1" (addr), "2" (count)       :
639                     "%eax", "memory", "cc");
640 #endif
641         } else {
642                 bus_space_handle_t _port_ = bsh + offset;
643 #ifdef __GNUCLIKE_ASM
644                 __asm __volatile("                              \n\
645                         cld                                     \n\
646                         repne                                   \n\
647                         movsb"                                  :
648                     "=D" (_port_), "=S" (addr), "=c" (count)    :
649                     "0" (_port_), "1" (addr), "2" (count)       :
650                     "memory", "cc");
651 #endif
652         }
653 }
654
655 static __inline void
656 bus_space_write_region_2(bus_space_tag_t tag, bus_space_handle_t bsh,
657                          bus_size_t offset, const u_int16_t *addr, size_t count)
658 {
659
660         if (tag == X86_BUS_SPACE_IO) {
661                 int _port_ = bsh + offset;
662 #ifdef __GNUCLIKE_ASM
663                 __asm __volatile("                              \n\
664                         cld                                     \n\
665                 1:      lodsw                                   \n\
666                         outw %%ax,%w0                           \n\
667                         addl $2,%0                              \n\
668                         loop 1b"                                :
669                     "=d" (_port_), "=S" (addr), "=c" (count)    :
670                     "0" (_port_), "1" (addr), "2" (count)       :
671                     "%eax", "memory", "cc");
672 #endif
673         } else {
674                 bus_space_handle_t _port_ = bsh + offset;
675 #ifdef __GNUCLIKE_ASM
676                 __asm __volatile("                              \n\
677                         cld                                     \n\
678                         repne                                   \n\
679                         movsw"                                  :
680                     "=D" (_port_), "=S" (addr), "=c" (count)    :
681                     "0" (_port_), "1" (addr), "2" (count)       :
682                     "memory", "cc");
683 #endif
684         }
685 }
686
687 static __inline void
688 bus_space_write_region_4(bus_space_tag_t tag, bus_space_handle_t bsh,
689                          bus_size_t offset, const u_int32_t *addr, size_t count)
690 {
691
692         if (tag == X86_BUS_SPACE_IO) {
693                 int _port_ = bsh + offset;
694 #ifdef __GNUCLIKE_ASM
695                 __asm __volatile("                              \n\
696                         cld                                     \n\
697                 1:      lodsl                                   \n\
698                         outl %%eax,%w0                          \n\
699                         addl $4,%0                              \n\
700                         loop 1b"                                :
701                     "=d" (_port_), "=S" (addr), "=c" (count)    :
702                     "0" (_port_), "1" (addr), "2" (count)       :
703                     "%eax", "memory", "cc");
704 #endif
705         } else {
706                 bus_space_handle_t _port_ = bsh + offset;
707 #ifdef __GNUCLIKE_ASM
708                 __asm __volatile("                              \n\
709                         cld                                     \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_ */