1 // SPDX-License-Identifier: GPL-2.0
3 * Alpha IO and memory functions.
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/string.h>
9 #include <linux/module.h>
12 /* Out-of-line versions of the i/o routines that redirect into the
13 platform-specific version. Note that "platform-specific" may mean
14 "generic", which bumps through the machine vector. */
17 ioread8(const void __iomem
*addr
)
21 ret
= IO_CONCAT(__IO_PREFIX
,ioread8
)(addr
);
26 unsigned int ioread16(const void __iomem
*addr
)
30 ret
= IO_CONCAT(__IO_PREFIX
,ioread16
)(addr
);
35 unsigned int ioread32(const void __iomem
*addr
)
39 ret
= IO_CONCAT(__IO_PREFIX
,ioread32
)(addr
);
44 void iowrite8(u8 b
, void __iomem
*addr
)
47 IO_CONCAT(__IO_PREFIX
,iowrite8
)(b
, addr
);
50 void iowrite16(u16 b
, void __iomem
*addr
)
53 IO_CONCAT(__IO_PREFIX
,iowrite16
)(b
, addr
);
56 void iowrite32(u32 b
, void __iomem
*addr
)
59 IO_CONCAT(__IO_PREFIX
,iowrite32
)(b
, addr
);
62 EXPORT_SYMBOL(ioread8
);
63 EXPORT_SYMBOL(ioread16
);
64 EXPORT_SYMBOL(ioread32
);
65 EXPORT_SYMBOL(iowrite8
);
66 EXPORT_SYMBOL(iowrite16
);
67 EXPORT_SYMBOL(iowrite32
);
69 u8
inb(unsigned long port
)
71 return ioread8(ioport_map(port
, 1));
74 u16
inw(unsigned long port
)
76 return ioread16(ioport_map(port
, 2));
79 u32
inl(unsigned long port
)
81 return ioread32(ioport_map(port
, 4));
84 void outb(u8 b
, unsigned long port
)
86 iowrite8(b
, ioport_map(port
, 1));
89 void outw(u16 b
, unsigned long port
)
91 iowrite16(b
, ioport_map(port
, 2));
94 void outl(u32 b
, unsigned long port
)
96 iowrite32(b
, ioport_map(port
, 4));
106 u8
__raw_readb(const volatile void __iomem
*addr
)
108 return IO_CONCAT(__IO_PREFIX
,readb
)(addr
);
111 u16
__raw_readw(const volatile void __iomem
*addr
)
113 return IO_CONCAT(__IO_PREFIX
,readw
)(addr
);
116 u32
__raw_readl(const volatile void __iomem
*addr
)
118 return IO_CONCAT(__IO_PREFIX
,readl
)(addr
);
121 u64
__raw_readq(const volatile void __iomem
*addr
)
123 return IO_CONCAT(__IO_PREFIX
,readq
)(addr
);
126 void __raw_writeb(u8 b
, volatile void __iomem
*addr
)
128 IO_CONCAT(__IO_PREFIX
,writeb
)(b
, addr
);
131 void __raw_writew(u16 b
, volatile void __iomem
*addr
)
133 IO_CONCAT(__IO_PREFIX
,writew
)(b
, addr
);
136 void __raw_writel(u32 b
, volatile void __iomem
*addr
)
138 IO_CONCAT(__IO_PREFIX
,writel
)(b
, addr
);
141 void __raw_writeq(u64 b
, volatile void __iomem
*addr
)
143 IO_CONCAT(__IO_PREFIX
,writeq
)(b
, addr
);
146 EXPORT_SYMBOL(__raw_readb
);
147 EXPORT_SYMBOL(__raw_readw
);
148 EXPORT_SYMBOL(__raw_readl
);
149 EXPORT_SYMBOL(__raw_readq
);
150 EXPORT_SYMBOL(__raw_writeb
);
151 EXPORT_SYMBOL(__raw_writew
);
152 EXPORT_SYMBOL(__raw_writel
);
153 EXPORT_SYMBOL(__raw_writeq
);
155 u8
readb(const volatile void __iomem
*addr
)
159 ret
= __raw_readb(addr
);
164 u16
readw(const volatile void __iomem
*addr
)
168 ret
= __raw_readw(addr
);
173 u32
readl(const volatile void __iomem
*addr
)
177 ret
= __raw_readl(addr
);
182 u64
readq(const volatile void __iomem
*addr
)
186 ret
= __raw_readq(addr
);
191 void writeb(u8 b
, volatile void __iomem
*addr
)
194 __raw_writeb(b
, addr
);
197 void writew(u16 b
, volatile void __iomem
*addr
)
200 __raw_writew(b
, addr
);
203 void writel(u32 b
, volatile void __iomem
*addr
)
206 __raw_writel(b
, addr
);
209 void writeq(u64 b
, volatile void __iomem
*addr
)
212 __raw_writeq(b
, addr
);
215 EXPORT_SYMBOL(readb
);
216 EXPORT_SYMBOL(readw
);
217 EXPORT_SYMBOL(readl
);
218 EXPORT_SYMBOL(readq
);
219 EXPORT_SYMBOL(writeb
);
220 EXPORT_SYMBOL(writew
);
221 EXPORT_SYMBOL(writel
);
222 EXPORT_SYMBOL(writeq
);
225 * The _relaxed functions must be ordered w.r.t. each other, but they don't
226 * have to be ordered w.r.t. other memory accesses.
228 u8
readb_relaxed(const volatile void __iomem
*addr
)
231 return __raw_readb(addr
);
234 u16
readw_relaxed(const volatile void __iomem
*addr
)
237 return __raw_readw(addr
);
240 u32
readl_relaxed(const volatile void __iomem
*addr
)
243 return __raw_readl(addr
);
246 u64
readq_relaxed(const volatile void __iomem
*addr
)
249 return __raw_readq(addr
);
252 EXPORT_SYMBOL(readb_relaxed
);
253 EXPORT_SYMBOL(readw_relaxed
);
254 EXPORT_SYMBOL(readl_relaxed
);
255 EXPORT_SYMBOL(readq_relaxed
);
258 * Read COUNT 8-bit bytes from port PORT into memory starting at SRC.
260 void ioread8_rep(const void __iomem
*port
, void *dst
, unsigned long count
)
262 while ((unsigned long)dst
& 0x3) {
266 *(unsigned char *)dst
= ioread8(port
);
274 w
|= ioread8(port
) << 8;
275 w
|= ioread8(port
) << 16;
276 w
|= ioread8(port
) << 24;
277 *(unsigned int *)dst
= w
;
283 *(unsigned char *)dst
= ioread8(port
);
288 void insb(unsigned long port
, void *dst
, unsigned long count
)
290 ioread8_rep(ioport_map(port
, 1), dst
, count
);
293 EXPORT_SYMBOL(ioread8_rep
);
297 * Read COUNT 16-bit words from port PORT into memory starting at
298 * SRC. SRC must be at least short aligned. This is used by the
299 * IDE driver to read disk sectors. Performance is important, but
300 * the interfaces seems to be slow: just using the inlined version
301 * of the inw() breaks things.
303 void ioread16_rep(const void __iomem
*port
, void *dst
, unsigned long count
)
305 if (unlikely((unsigned long)dst
& 0x3)) {
308 BUG_ON((unsigned long)dst
& 0x1);
310 *(unsigned short *)dst
= ioread16(port
);
318 w
|= ioread16(port
) << 16;
319 *(unsigned int *)dst
= w
;
324 *(unsigned short*)dst
= ioread16(port
);
328 void insw(unsigned long port
, void *dst
, unsigned long count
)
330 ioread16_rep(ioport_map(port
, 2), dst
, count
);
333 EXPORT_SYMBOL(ioread16_rep
);
338 * Read COUNT 32-bit words from port PORT into memory starting at
339 * SRC. Now works with any alignment in SRC. Performance is important,
340 * but the interfaces seems to be slow: just using the inlined version
341 * of the inl() breaks things.
343 void ioread32_rep(const void __iomem
*port
, void *dst
, unsigned long count
)
345 if (unlikely((unsigned long)dst
& 0x3)) {
347 struct S
{ int x
__attribute__((packed
)); };
348 ((struct S
*)dst
)->x
= ioread32(port
);
352 /* Buffer 32-bit aligned. */
354 *(unsigned int *)dst
= ioread32(port
);
360 void insl(unsigned long port
, void *dst
, unsigned long count
)
362 ioread32_rep(ioport_map(port
, 4), dst
, count
);
365 EXPORT_SYMBOL(ioread32_rep
);
370 * Like insb but in the opposite direction.
371 * Don't worry as much about doing aligned memory transfers:
372 * doing byte reads the "slow" way isn't nearly as slow as
373 * doing byte writes the slow way (no r-m-w cycle).
375 void iowrite8_rep(void __iomem
*port
, const void *xsrc
, unsigned long count
)
377 const unsigned char *src
= xsrc
;
379 iowrite8(*src
++, port
);
382 void outsb(unsigned long port
, const void *src
, unsigned long count
)
384 iowrite8_rep(ioport_map(port
, 1), src
, count
);
387 EXPORT_SYMBOL(iowrite8_rep
);
388 EXPORT_SYMBOL(outsb
);
392 * Like insw but in the opposite direction. This is used by the IDE
393 * driver to write disk sectors. Performance is important, but the
394 * interfaces seems to be slow: just using the inlined version of the
395 * outw() breaks things.
397 void iowrite16_rep(void __iomem
*port
, const void *src
, unsigned long count
)
399 if (unlikely((unsigned long)src
& 0x3)) {
402 BUG_ON((unsigned long)src
& 0x1);
403 iowrite16(*(unsigned short *)src
, port
);
411 w
= *(unsigned int *)src
;
413 iowrite16(w
>> 0, port
);
414 iowrite16(w
>> 16, port
);
418 iowrite16(*(unsigned short *)src
, port
);
422 void outsw(unsigned long port
, const void *src
, unsigned long count
)
424 iowrite16_rep(ioport_map(port
, 2), src
, count
);
427 EXPORT_SYMBOL(iowrite16_rep
);
428 EXPORT_SYMBOL(outsw
);
432 * Like insl but in the opposite direction. This is used by the IDE
433 * driver to write disk sectors. Works with any alignment in SRC.
434 * Performance is important, but the interfaces seems to be slow:
435 * just using the inlined version of the outl() breaks things.
437 void iowrite32_rep(void __iomem
*port
, const void *src
, unsigned long count
)
439 if (unlikely((unsigned long)src
& 0x3)) {
441 struct S
{ int x
__attribute__((packed
)); };
442 iowrite32(((struct S
*)src
)->x
, port
);
446 /* Buffer 32-bit aligned. */
448 iowrite32(*(unsigned int *)src
, port
);
454 void outsl(unsigned long port
, const void *src
, unsigned long count
)
456 iowrite32_rep(ioport_map(port
, 4), src
, count
);
459 EXPORT_SYMBOL(iowrite32_rep
);
460 EXPORT_SYMBOL(outsl
);
464 * Copy data from IO memory space to "real" memory space.
465 * This needs to be optimized.
467 void memcpy_fromio(void *to
, const volatile void __iomem
*from
, long count
)
469 /* Optimize co-aligned transfers. Everything else gets handled
472 if (count
>= 8 && ((u64
)to
& 7) == ((u64
)from
& 7)) {
475 *(u64
*)to
= __raw_readq(from
);
479 } while (count
>= 0);
483 if (count
>= 4 && ((u64
)to
& 3) == ((u64
)from
& 3)) {
486 *(u32
*)to
= __raw_readl(from
);
490 } while (count
>= 0);
494 if (count
>= 2 && ((u64
)to
& 1) == ((u64
)from
& 1)) {
497 *(u16
*)to
= __raw_readw(from
);
501 } while (count
>= 0);
506 *(u8
*) to
= __raw_readb(from
);
514 EXPORT_SYMBOL(memcpy_fromio
);
518 * Copy data from "real" memory space to IO memory space.
519 * This needs to be optimized.
521 void memcpy_toio(volatile void __iomem
*to
, const void *from
, long count
)
523 /* Optimize co-aligned transfers. Everything else gets handled
525 /* FIXME -- align FROM. */
527 if (count
>= 8 && ((u64
)to
& 7) == ((u64
)from
& 7)) {
530 __raw_writeq(*(const u64
*)from
, to
);
534 } while (count
>= 0);
538 if (count
>= 4 && ((u64
)to
& 3) == ((u64
)from
& 3)) {
541 __raw_writel(*(const u32
*)from
, to
);
545 } while (count
>= 0);
549 if (count
>= 2 && ((u64
)to
& 1) == ((u64
)from
& 1)) {
552 __raw_writew(*(const u16
*)from
, to
);
556 } while (count
>= 0);
561 __raw_writeb(*(const u8
*) from
, to
);
569 EXPORT_SYMBOL(memcpy_toio
);
573 * "memset" on IO memory space.
575 void _memset_c_io(volatile void __iomem
*to
, unsigned long c
, long count
)
577 /* Handle any initial odd byte */
578 if (count
> 0 && ((u64
)to
& 1)) {
584 /* Handle any initial odd halfword */
585 if (count
>= 2 && ((u64
)to
& 2)) {
591 /* Handle any initial odd word */
592 if (count
>= 4 && ((u64
)to
& 4)) {
598 /* Handle all full-sized quadwords: we're aligned
599 (or have a small count) */
606 } while (count
>= 0);
610 /* The tail is word-aligned if we still have count >= 4 */
617 /* The tail is half-word aligned if we have count >= 2 */
624 /* And finally, one last byte.. */
631 EXPORT_SYMBOL(_memset_c_io
);
633 /* A version of memcpy used by the vga console routines to move data around
634 arbitrarily between screen and main memory. */
637 scr_memcpyw(u16
*d
, const u16
*s
, unsigned int count
)
639 const u16 __iomem
*ios
= (const u16 __iomem
*) s
;
640 u16 __iomem
*iod
= (u16 __iomem
*) d
;
641 int s_isio
= __is_ioaddr(s
);
642 int d_isio
= __is_ioaddr(d
);
646 /* FIXME: Should handle unaligned ops and
647 operation widening. */
651 u16 tmp
= __raw_readw(ios
++);
652 __raw_writew(tmp
, iod
++);
656 memcpy_fromio(d
, ios
, count
);
659 memcpy_toio(iod
, s
, count
);
665 EXPORT_SYMBOL(scr_memcpyw
);
667 void __iomem
*ioport_map(unsigned long port
, unsigned int size
)
669 return IO_CONCAT(__IO_PREFIX
,ioportmap
) (port
);
672 void ioport_unmap(void __iomem
*addr
)
676 EXPORT_SYMBOL(ioport_map
);
677 EXPORT_SYMBOL(ioport_unmap
);