Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / arch / alpha / kernel / io.c
blob838586abb1e02f8fbe5d8415d8f3730fdec5ad68
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Alpha IO and memory functions.
4 */
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/string.h>
9 #include <linux/module.h>
10 #include <asm/io.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. */
16 unsigned int
17 ioread8(const void __iomem *addr)
19 unsigned int ret;
20 mb();
21 ret = IO_CONCAT(__IO_PREFIX,ioread8)(addr);
22 mb();
23 return ret;
26 unsigned int ioread16(const void __iomem *addr)
28 unsigned int ret;
29 mb();
30 ret = IO_CONCAT(__IO_PREFIX,ioread16)(addr);
31 mb();
32 return ret;
35 unsigned int ioread32(const void __iomem *addr)
37 unsigned int ret;
38 mb();
39 ret = IO_CONCAT(__IO_PREFIX,ioread32)(addr);
40 mb();
41 return ret;
44 void iowrite8(u8 b, void __iomem *addr)
46 mb();
47 IO_CONCAT(__IO_PREFIX,iowrite8)(b, addr);
50 void iowrite16(u16 b, void __iomem *addr)
52 mb();
53 IO_CONCAT(__IO_PREFIX,iowrite16)(b, addr);
56 void iowrite32(u32 b, void __iomem *addr)
58 mb();
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));
99 EXPORT_SYMBOL(inb);
100 EXPORT_SYMBOL(inw);
101 EXPORT_SYMBOL(inl);
102 EXPORT_SYMBOL(outb);
103 EXPORT_SYMBOL(outw);
104 EXPORT_SYMBOL(outl);
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)
157 u8 ret;
158 mb();
159 ret = __raw_readb(addr);
160 mb();
161 return ret;
164 u16 readw(const volatile void __iomem *addr)
166 u16 ret;
167 mb();
168 ret = __raw_readw(addr);
169 mb();
170 return ret;
173 u32 readl(const volatile void __iomem *addr)
175 u32 ret;
176 mb();
177 ret = __raw_readl(addr);
178 mb();
179 return ret;
182 u64 readq(const volatile void __iomem *addr)
184 u64 ret;
185 mb();
186 ret = __raw_readq(addr);
187 mb();
188 return ret;
191 void writeb(u8 b, volatile void __iomem *addr)
193 mb();
194 __raw_writeb(b, addr);
197 void writew(u16 b, volatile void __iomem *addr)
199 mb();
200 __raw_writew(b, addr);
203 void writel(u32 b, volatile void __iomem *addr)
205 mb();
206 __raw_writel(b, addr);
209 void writeq(u64 b, volatile void __iomem *addr)
211 mb();
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)
230 mb();
231 return __raw_readb(addr);
234 u16 readw_relaxed(const volatile void __iomem *addr)
236 mb();
237 return __raw_readw(addr);
240 u32 readl_relaxed(const volatile void __iomem *addr)
242 mb();
243 return __raw_readl(addr);
246 u64 readq_relaxed(const volatile void __iomem *addr)
248 mb();
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) {
263 if (!count)
264 return;
265 count--;
266 *(unsigned char *)dst = ioread8(port);
267 dst += 1;
270 while (count >= 4) {
271 unsigned int w;
272 count -= 4;
273 w = ioread8(port);
274 w |= ioread8(port) << 8;
275 w |= ioread8(port) << 16;
276 w |= ioread8(port) << 24;
277 *(unsigned int *)dst = w;
278 dst += 4;
281 while (count) {
282 --count;
283 *(unsigned char *)dst = ioread8(port);
284 dst += 1;
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);
294 EXPORT_SYMBOL(insb);
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)) {
306 if (!count)
307 return;
308 BUG_ON((unsigned long)dst & 0x1);
309 count--;
310 *(unsigned short *)dst = ioread16(port);
311 dst += 2;
314 while (count >= 2) {
315 unsigned int w;
316 count -= 2;
317 w = ioread16(port);
318 w |= ioread16(port) << 16;
319 *(unsigned int *)dst = w;
320 dst += 4;
323 if (count) {
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);
334 EXPORT_SYMBOL(insw);
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)) {
346 while (count--) {
347 struct S { int x __attribute__((packed)); };
348 ((struct S *)dst)->x = ioread32(port);
349 dst += 4;
351 } else {
352 /* Buffer 32-bit aligned. */
353 while (count--) {
354 *(unsigned int *)dst = ioread32(port);
355 dst += 4;
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);
366 EXPORT_SYMBOL(insl);
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;
378 while (count--)
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)) {
400 if (!count)
401 return;
402 BUG_ON((unsigned long)src & 0x1);
403 iowrite16(*(unsigned short *)src, port);
404 src += 2;
405 --count;
408 while (count >= 2) {
409 unsigned int w;
410 count -= 2;
411 w = *(unsigned int *)src;
412 src += 4;
413 iowrite16(w >> 0, port);
414 iowrite16(w >> 16, port);
417 if (count) {
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)) {
440 while (count--) {
441 struct S { int x __attribute__((packed)); };
442 iowrite32(((struct S *)src)->x, port);
443 src += 4;
445 } else {
446 /* Buffer 32-bit aligned. */
447 while (count--) {
448 iowrite32(*(unsigned int *)src, port);
449 src += 4;
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
470 a byte at a time. */
472 if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
473 count -= 8;
474 do {
475 *(u64 *)to = __raw_readq(from);
476 count -= 8;
477 to += 8;
478 from += 8;
479 } while (count >= 0);
480 count += 8;
483 if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
484 count -= 4;
485 do {
486 *(u32 *)to = __raw_readl(from);
487 count -= 4;
488 to += 4;
489 from += 4;
490 } while (count >= 0);
491 count += 4;
494 if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
495 count -= 2;
496 do {
497 *(u16 *)to = __raw_readw(from);
498 count -= 2;
499 to += 2;
500 from += 2;
501 } while (count >= 0);
502 count += 2;
505 while (count > 0) {
506 *(u8 *) to = __raw_readb(from);
507 count--;
508 to++;
509 from++;
511 mb();
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
524 a byte at a time. */
525 /* FIXME -- align FROM. */
527 if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
528 count -= 8;
529 do {
530 __raw_writeq(*(const u64 *)from, to);
531 count -= 8;
532 to += 8;
533 from += 8;
534 } while (count >= 0);
535 count += 8;
538 if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
539 count -= 4;
540 do {
541 __raw_writel(*(const u32 *)from, to);
542 count -= 4;
543 to += 4;
544 from += 4;
545 } while (count >= 0);
546 count += 4;
549 if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
550 count -= 2;
551 do {
552 __raw_writew(*(const u16 *)from, to);
553 count -= 2;
554 to += 2;
555 from += 2;
556 } while (count >= 0);
557 count += 2;
560 while (count > 0) {
561 __raw_writeb(*(const u8 *) from, to);
562 count--;
563 to++;
564 from++;
566 mb();
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)) {
579 __raw_writeb(c, to);
580 to++;
581 count--;
584 /* Handle any initial odd halfword */
585 if (count >= 2 && ((u64)to & 2)) {
586 __raw_writew(c, to);
587 to += 2;
588 count -= 2;
591 /* Handle any initial odd word */
592 if (count >= 4 && ((u64)to & 4)) {
593 __raw_writel(c, to);
594 to += 4;
595 count -= 4;
598 /* Handle all full-sized quadwords: we're aligned
599 (or have a small count) */
600 count -= 8;
601 if (count >= 0) {
602 do {
603 __raw_writeq(c, to);
604 to += 8;
605 count -= 8;
606 } while (count >= 0);
608 count += 8;
610 /* The tail is word-aligned if we still have count >= 4 */
611 if (count >= 4) {
612 __raw_writel(c, to);
613 to += 4;
614 count -= 4;
617 /* The tail is half-word aligned if we have count >= 2 */
618 if (count >= 2) {
619 __raw_writew(c, to);
620 to += 2;
621 count -= 2;
624 /* And finally, one last byte.. */
625 if (count) {
626 __raw_writeb(c, to);
628 mb();
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. */
636 void
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);
644 if (s_isio) {
645 if (d_isio) {
646 /* FIXME: Should handle unaligned ops and
647 operation widening. */
649 count /= 2;
650 while (count--) {
651 u16 tmp = __raw_readw(ios++);
652 __raw_writew(tmp, iod++);
655 else
656 memcpy_fromio(d, ios, count);
657 } else {
658 if (d_isio)
659 memcpy_toio(iod, s, count);
660 else
661 memcpy(d, 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);