blob: 570433b3418092133571eeb568ab5a10d8e69fcf [file] [log] [blame]
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +00001/* Generic I/O port emulation, based on MN10300 code
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11#ifndef __ASM_GENERIC_IO_H
12#define __ASM_GENERIC_IO_H
13
14#include <asm/page.h> /* I/O is all done through memory accesses */
Thierry Reding9216efa2014-10-01 15:20:33 +020015#include <linux/string.h> /* for memset() and memcpy() */
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000016#include <linux/types.h>
17
18#ifdef CONFIG_GENERIC_IOMAP
19#include <asm-generic/iomap.h>
20#endif
21
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +020022#include <asm-generic/pci_iomap.h>
23
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040024#ifndef mmiowb
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000025#define mmiowb() do {} while (0)
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040026#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000027
Sinan Kaya64e2c672018-04-05 09:09:09 -040028#ifndef __io_br
29#define __io_br() barrier()
30#endif
31
32/* prevent prefetching of coherent DMA data ahead of a dma-complete */
33#ifndef __io_ar
34#ifdef rmb
35#define __io_ar() rmb()
36#else
37#define __io_ar() barrier()
38#endif
39#endif
40
41/* flush writes to coherent DMA data before possibly triggering a DMA read */
42#ifndef __io_bw
43#ifdef wmb
44#define __io_bw() wmb()
45#else
46#define __io_bw() barrier()
47#endif
48#endif
49
50/* serialize device access against a spin_unlock, usually handled there. */
51#ifndef __io_aw
52#define __io_aw() barrier()
53#endif
54
55#ifndef __io_pbw
56#define __io_pbw() __io_bw()
57#endif
58
59#ifndef __io_paw
60#define __io_paw() __io_aw()
61#endif
62
63#ifndef __io_pbr
64#define __io_pbr() __io_br()
65#endif
66
67#ifndef __io_par
68#define __io_par() __io_ar()
69#endif
70
71
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000072/*
Thierry Reding9216efa2014-10-01 15:20:33 +020073 * __raw_{read,write}{b,w,l,q}() access memory in native endianness.
74 *
75 * On some architectures memory mapped IO needs to be accessed differently.
76 * On the simple architectures, we just read/write the memory location
77 * directly.
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000078 */
Thierry Reding9216efa2014-10-01 15:20:33 +020079
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040080#ifndef __raw_readb
Thierry Reding9216efa2014-10-01 15:20:33 +020081#define __raw_readb __raw_readb
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000082static inline u8 __raw_readb(const volatile void __iomem *addr)
83{
Thierry Reding9216efa2014-10-01 15:20:33 +020084 return *(const volatile u8 __force *)addr;
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000085}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040086#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000087
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040088#ifndef __raw_readw
Thierry Reding9216efa2014-10-01 15:20:33 +020089#define __raw_readw __raw_readw
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000090static inline u16 __raw_readw(const volatile void __iomem *addr)
91{
Thierry Reding9216efa2014-10-01 15:20:33 +020092 return *(const volatile u16 __force *)addr;
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000093}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040094#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000095
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040096#ifndef __raw_readl
Thierry Reding9216efa2014-10-01 15:20:33 +020097#define __raw_readl __raw_readl
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +000098static inline u32 __raw_readl(const volatile void __iomem *addr)
99{
Thierry Reding9216efa2014-10-01 15:20:33 +0200100 return *(const volatile u32 __force *)addr;
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000101}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400102#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000103
Thierry Reding9216efa2014-10-01 15:20:33 +0200104#ifdef CONFIG_64BIT
105#ifndef __raw_readq
106#define __raw_readq __raw_readq
107static inline u64 __raw_readq(const volatile void __iomem *addr)
108{
109 return *(const volatile u64 __force *)addr;
110}
111#endif
112#endif /* CONFIG_64BIT */
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100113
Thierry Reding9216efa2014-10-01 15:20:33 +0200114#ifndef __raw_writeb
115#define __raw_writeb __raw_writeb
116static inline void __raw_writeb(u8 value, volatile void __iomem *addr)
117{
118 *(volatile u8 __force *)addr = value;
119}
120#endif
121
122#ifndef __raw_writew
123#define __raw_writew __raw_writew
124static inline void __raw_writew(u16 value, volatile void __iomem *addr)
125{
126 *(volatile u16 __force *)addr = value;
127}
128#endif
129
130#ifndef __raw_writel
131#define __raw_writel __raw_writel
132static inline void __raw_writel(u32 value, volatile void __iomem *addr)
133{
134 *(volatile u32 __force *)addr = value;
135}
136#endif
137
138#ifdef CONFIG_64BIT
139#ifndef __raw_writeq
140#define __raw_writeq __raw_writeq
141static inline void __raw_writeq(u64 value, volatile void __iomem *addr)
142{
143 *(volatile u64 __force *)addr = value;
144}
145#endif
146#endif /* CONFIG_64BIT */
147
148/*
149 * {read,write}{b,w,l,q}() access little endian memory and return result in
150 * native endianness.
151 */
152
153#ifndef readb
154#define readb readb
155static inline u8 readb(const volatile void __iomem *addr)
156{
157 return __raw_readb(addr);
158}
159#endif
160
161#ifndef readw
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100162#define readw readw
163static inline u16 readw(const volatile void __iomem *addr)
164{
165 return __le16_to_cpu(__raw_readw(addr));
166}
Thierry Reding9216efa2014-10-01 15:20:33 +0200167#endif
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100168
Thierry Reding9216efa2014-10-01 15:20:33 +0200169#ifndef readl
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100170#define readl readl
171static inline u32 readl(const volatile void __iomem *addr)
172{
173 return __le32_to_cpu(__raw_readl(addr));
174}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400175#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000176
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000177#ifdef CONFIG_64BIT
Thierry Reding9216efa2014-10-01 15:20:33 +0200178#ifndef readq
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100179#define readq readq
180static inline u64 readq(const volatile void __iomem *addr)
181{
182 return __le64_to_cpu(__raw_readq(addr));
183}
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000184#endif
Jan Glaubercd248342012-11-29 12:50:30 +0100185#endif /* CONFIG_64BIT */
186
Thierry Reding9216efa2014-10-01 15:20:33 +0200187#ifndef writeb
188#define writeb writeb
189static inline void writeb(u8 value, volatile void __iomem *addr)
190{
191 __raw_writeb(value, addr);
192}
GuanXuetao7dc59bd2011-02-22 19:06:43 +0800193#endif
194
Thierry Reding9216efa2014-10-01 15:20:33 +0200195#ifndef writew
196#define writew writew
197static inline void writew(u16 value, volatile void __iomem *addr)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000198{
Thierry Reding9216efa2014-10-01 15:20:33 +0200199 __raw_writew(cpu_to_le16(value), addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000200}
Thierry Reding9216efa2014-10-01 15:20:33 +0200201#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000202
Thierry Reding9216efa2014-10-01 15:20:33 +0200203#ifndef writel
204#define writel writel
205static inline void writel(u32 value, volatile void __iomem *addr)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000206{
Thierry Reding9216efa2014-10-01 15:20:33 +0200207 __raw_writel(__cpu_to_le32(value), addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000208}
Thierry Reding9216efa2014-10-01 15:20:33 +0200209#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000210
Thierry Reding9216efa2014-10-01 15:20:33 +0200211#ifdef CONFIG_64BIT
212#ifndef writeq
213#define writeq writeq
214static inline void writeq(u64 value, volatile void __iomem *addr)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000215{
Thierry Reding9216efa2014-10-01 15:20:33 +0200216 __raw_writeq(__cpu_to_le64(value), addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000217}
Thierry Reding9216efa2014-10-01 15:20:33 +0200218#endif
219#endif /* CONFIG_64BIT */
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000220
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200221/*
Arnd Bergmann1c8d2962014-11-11 19:55:45 +0100222 * {read,write}{b,w,l,q}_relaxed() are like the regular version, but
223 * are not guaranteed to provide ordering against spinlocks or memory
224 * accesses.
225 */
226#ifndef readb_relaxed
227#define readb_relaxed readb
228#endif
229
230#ifndef readw_relaxed
231#define readw_relaxed readw
232#endif
233
234#ifndef readl_relaxed
235#define readl_relaxed readl
236#endif
237
Robin Murphye5112672016-04-26 11:38:20 +0100238#if defined(readq) && !defined(readq_relaxed)
Will Deacon9439eb32013-09-03 10:44:00 +0100239#define readq_relaxed readq
240#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000241
Arnd Bergmann1c8d2962014-11-11 19:55:45 +0100242#ifndef writeb_relaxed
243#define writeb_relaxed writeb
244#endif
245
246#ifndef writew_relaxed
247#define writew_relaxed writew
248#endif
249
250#ifndef writel_relaxed
251#define writel_relaxed writel
252#endif
253
Robin Murphye5112672016-04-26 11:38:20 +0100254#if defined(writeq) && !defined(writeq_relaxed)
Arnd Bergmann1c8d2962014-11-11 19:55:45 +0100255#define writeq_relaxed writeq
256#endif
257
258/*
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200259 * {read,write}s{b,w,l,q}() repeatedly access the same memory address in
260 * native endianness in 8-, 16-, 32- or 64-bit chunks (@count times).
261 */
262#ifndef readsb
263#define readsb readsb
264static inline void readsb(const volatile void __iomem *addr, void *buffer,
265 unsigned int count)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000266{
267 if (count) {
268 u8 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200269
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000270 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200271 u8 x = __raw_readb(addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000272 *buf++ = x;
273 } while (--count);
274 }
275}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400276#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000277
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200278#ifndef readsw
279#define readsw readsw
280static inline void readsw(const volatile void __iomem *addr, void *buffer,
281 unsigned int count)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000282{
283 if (count) {
284 u16 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200285
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000286 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200287 u16 x = __raw_readw(addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000288 *buf++ = x;
289 } while (--count);
290 }
291}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400292#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000293
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200294#ifndef readsl
295#define readsl readsl
296static inline void readsl(const volatile void __iomem *addr, void *buffer,
297 unsigned int count)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000298{
299 if (count) {
300 u32 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200301
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000302 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200303 u32 x = __raw_readl(addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000304 *buf++ = x;
305 } while (--count);
306 }
307}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400308#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000309
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200310#ifdef CONFIG_64BIT
311#ifndef readsq
312#define readsq readsq
313static inline void readsq(const volatile void __iomem *addr, void *buffer,
314 unsigned int count)
315{
316 if (count) {
317 u64 *buf = buffer;
318
319 do {
320 u64 x = __raw_readq(addr);
321 *buf++ = x;
322 } while (--count);
323 }
324}
325#endif
326#endif /* CONFIG_64BIT */
327
328#ifndef writesb
329#define writesb writesb
330static inline void writesb(volatile void __iomem *addr, const void *buffer,
331 unsigned int count)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000332{
333 if (count) {
334 const u8 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200335
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000336 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200337 __raw_writeb(*buf++, addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000338 } while (--count);
339 }
340}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400341#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000342
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200343#ifndef writesw
344#define writesw writesw
345static inline void writesw(volatile void __iomem *addr, const void *buffer,
346 unsigned int count)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000347{
348 if (count) {
349 const u16 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200350
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000351 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200352 __raw_writew(*buf++, addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000353 } while (--count);
354 }
355}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400356#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000357
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200358#ifndef writesl
359#define writesl writesl
360static inline void writesl(volatile void __iomem *addr, const void *buffer,
361 unsigned int count)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000362{
363 if (count) {
364 const u32 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200365
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000366 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200367 __raw_writel(*buf++, addr);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000368 } while (--count);
369 }
370}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400371#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000372
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200373#ifdef CONFIG_64BIT
374#ifndef writesq
375#define writesq writesq
376static inline void writesq(volatile void __iomem *addr, const void *buffer,
377 unsigned int count)
378{
379 if (count) {
380 const u64 *buf = buffer;
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000381
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200382 do {
383 __raw_writeq(*buf++, addr);
384 } while (--count);
385 }
386}
387#endif
388#endif /* CONFIG_64BIT */
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000389
Thierry Reding9216efa2014-10-01 15:20:33 +0200390#ifndef PCI_IOBASE
391#define PCI_IOBASE ((void __iomem *)0)
392#endif
393
GuanXuetao7dc59bd2011-02-22 19:06:43 +0800394#ifndef IO_SPACE_LIMIT
395#define IO_SPACE_LIMIT 0xffff
396#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000397
Thierry Reding9216efa2014-10-01 15:20:33 +0200398/*
399 * {in,out}{b,w,l}() access little endian I/O. {in,out}{b,w,l}_p() can be
400 * implemented on hardware that needs an additional delay for I/O accesses to
401 * take effect.
402 */
403
404#ifndef inb
405#define inb inb
406static inline u8 inb(unsigned long addr)
407{
408 return readb(PCI_IOBASE + addr);
409}
410#endif
411
412#ifndef inw
413#define inw inw
414static inline u16 inw(unsigned long addr)
415{
416 return readw(PCI_IOBASE + addr);
417}
418#endif
419
420#ifndef inl
421#define inl inl
422static inline u32 inl(unsigned long addr)
423{
424 return readl(PCI_IOBASE + addr);
425}
426#endif
427
428#ifndef outb
429#define outb outb
430static inline void outb(u8 value, unsigned long addr)
431{
432 writeb(value, PCI_IOBASE + addr);
433}
434#endif
435
436#ifndef outw
437#define outw outw
438static inline void outw(u16 value, unsigned long addr)
439{
440 writew(value, PCI_IOBASE + addr);
441}
442#endif
443
444#ifndef outl
445#define outl outl
446static inline void outl(u32 value, unsigned long addr)
447{
448 writel(value, PCI_IOBASE + addr);
449}
450#endif
451
452#ifndef inb_p
453#define inb_p inb_p
454static inline u8 inb_p(unsigned long addr)
455{
456 return inb(addr);
457}
458#endif
459
460#ifndef inw_p
461#define inw_p inw_p
462static inline u16 inw_p(unsigned long addr)
463{
464 return inw(addr);
465}
466#endif
467
468#ifndef inl_p
469#define inl_p inl_p
470static inline u32 inl_p(unsigned long addr)
471{
472 return inl(addr);
473}
474#endif
475
476#ifndef outb_p
477#define outb_p outb_p
478static inline void outb_p(u8 value, unsigned long addr)
479{
480 outb(value, addr);
481}
482#endif
483
484#ifndef outw_p
485#define outw_p outw_p
486static inline void outw_p(u16 value, unsigned long addr)
487{
488 outw(value, addr);
489}
490#endif
491
492#ifndef outl_p
493#define outl_p outl_p
494static inline void outl_p(u32 value, unsigned long addr)
495{
496 outl(value, addr);
497}
498#endif
499
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200500/*
501 * {in,out}s{b,w,l}{,_p}() are variants of the above that repeatedly access a
502 * single I/O port multiple times.
503 */
504
505#ifndef insb
506#define insb insb
507static inline void insb(unsigned long addr, void *buffer, unsigned int count)
508{
509 readsb(PCI_IOBASE + addr, buffer, count);
510}
511#endif
512
513#ifndef insw
514#define insw insw
515static inline void insw(unsigned long addr, void *buffer, unsigned int count)
516{
517 readsw(PCI_IOBASE + addr, buffer, count);
518}
519#endif
520
521#ifndef insl
522#define insl insl
523static inline void insl(unsigned long addr, void *buffer, unsigned int count)
524{
525 readsl(PCI_IOBASE + addr, buffer, count);
526}
527#endif
528
529#ifndef outsb
530#define outsb outsb
531static inline void outsb(unsigned long addr, const void *buffer,
532 unsigned int count)
533{
534 writesb(PCI_IOBASE + addr, buffer, count);
535}
536#endif
537
538#ifndef outsw
539#define outsw outsw
540static inline void outsw(unsigned long addr, const void *buffer,
541 unsigned int count)
542{
543 writesw(PCI_IOBASE + addr, buffer, count);
544}
545#endif
546
547#ifndef outsl
548#define outsl outsl
549static inline void outsl(unsigned long addr, const void *buffer,
550 unsigned int count)
551{
552 writesl(PCI_IOBASE + addr, buffer, count);
553}
554#endif
555
556#ifndef insb_p
557#define insb_p insb_p
558static inline void insb_p(unsigned long addr, void *buffer, unsigned int count)
559{
560 insb(addr, buffer, count);
561}
562#endif
563
564#ifndef insw_p
565#define insw_p insw_p
566static inline void insw_p(unsigned long addr, void *buffer, unsigned int count)
567{
568 insw(addr, buffer, count);
569}
570#endif
571
572#ifndef insl_p
573#define insl_p insl_p
574static inline void insl_p(unsigned long addr, void *buffer, unsigned int count)
575{
576 insl(addr, buffer, count);
577}
578#endif
579
580#ifndef outsb_p
581#define outsb_p outsb_p
582static inline void outsb_p(unsigned long addr, const void *buffer,
583 unsigned int count)
584{
585 outsb(addr, buffer, count);
586}
587#endif
588
589#ifndef outsw_p
590#define outsw_p outsw_p
591static inline void outsw_p(unsigned long addr, const void *buffer,
592 unsigned int count)
593{
594 outsw(addr, buffer, count);
595}
596#endif
597
598#ifndef outsl_p
599#define outsl_p outsl_p
600static inline void outsl_p(unsigned long addr, const void *buffer,
601 unsigned int count)
602{
603 outsl(addr, buffer, count);
604}
605#endif
606
Thierry Reding9216efa2014-10-01 15:20:33 +0200607#ifndef CONFIG_GENERIC_IOMAP
608#ifndef ioread8
609#define ioread8 ioread8
610static inline u8 ioread8(const volatile void __iomem *addr)
611{
612 return readb(addr);
613}
614#endif
615
616#ifndef ioread16
617#define ioread16 ioread16
618static inline u16 ioread16(const volatile void __iomem *addr)
619{
620 return readw(addr);
621}
622#endif
623
624#ifndef ioread32
625#define ioread32 ioread32
626static inline u32 ioread32(const volatile void __iomem *addr)
627{
628 return readl(addr);
629}
630#endif
631
Horia Geantă9e44fb12016-05-19 18:10:56 +0300632#ifdef CONFIG_64BIT
633#ifndef ioread64
634#define ioread64 ioread64
635static inline u64 ioread64(const volatile void __iomem *addr)
636{
637 return readq(addr);
638}
639#endif
640#endif /* CONFIG_64BIT */
641
Thierry Reding9216efa2014-10-01 15:20:33 +0200642#ifndef iowrite8
643#define iowrite8 iowrite8
644static inline void iowrite8(u8 value, volatile void __iomem *addr)
645{
646 writeb(value, addr);
647}
648#endif
649
650#ifndef iowrite16
651#define iowrite16 iowrite16
652static inline void iowrite16(u16 value, volatile void __iomem *addr)
653{
654 writew(value, addr);
655}
656#endif
657
658#ifndef iowrite32
659#define iowrite32 iowrite32
660static inline void iowrite32(u32 value, volatile void __iomem *addr)
661{
662 writel(value, addr);
663}
664#endif
665
Horia Geantă9e44fb12016-05-19 18:10:56 +0300666#ifdef CONFIG_64BIT
667#ifndef iowrite64
668#define iowrite64 iowrite64
669static inline void iowrite64(u64 value, volatile void __iomem *addr)
670{
671 writeq(value, addr);
672}
673#endif
674#endif /* CONFIG_64BIT */
675
Thierry Reding9216efa2014-10-01 15:20:33 +0200676#ifndef ioread16be
677#define ioread16be ioread16be
678static inline u16 ioread16be(const volatile void __iomem *addr)
679{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300680 return swab16(readw(addr));
Thierry Reding9216efa2014-10-01 15:20:33 +0200681}
682#endif
683
684#ifndef ioread32be
685#define ioread32be ioread32be
686static inline u32 ioread32be(const volatile void __iomem *addr)
687{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300688 return swab32(readl(addr));
Thierry Reding9216efa2014-10-01 15:20:33 +0200689}
690#endif
691
Horia Geantă9e44fb12016-05-19 18:10:56 +0300692#ifdef CONFIG_64BIT
693#ifndef ioread64be
694#define ioread64be ioread64be
695static inline u64 ioread64be(const volatile void __iomem *addr)
696{
697 return swab64(readq(addr));
698}
699#endif
700#endif /* CONFIG_64BIT */
701
Thierry Reding9216efa2014-10-01 15:20:33 +0200702#ifndef iowrite16be
703#define iowrite16be iowrite16be
704static inline void iowrite16be(u16 value, void volatile __iomem *addr)
705{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300706 writew(swab16(value), addr);
Thierry Reding9216efa2014-10-01 15:20:33 +0200707}
708#endif
709
710#ifndef iowrite32be
711#define iowrite32be iowrite32be
712static inline void iowrite32be(u32 value, volatile void __iomem *addr)
713{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300714 writel(swab32(value), addr);
Thierry Reding9216efa2014-10-01 15:20:33 +0200715}
716#endif
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200717
Horia Geantă9e44fb12016-05-19 18:10:56 +0300718#ifdef CONFIG_64BIT
719#ifndef iowrite64be
720#define iowrite64be iowrite64be
721static inline void iowrite64be(u64 value, volatile void __iomem *addr)
722{
723 writeq(swab64(value), addr);
724}
725#endif
726#endif /* CONFIG_64BIT */
727
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200728#ifndef ioread8_rep
729#define ioread8_rep ioread8_rep
730static inline void ioread8_rep(const volatile void __iomem *addr, void *buffer,
731 unsigned int count)
732{
733 readsb(addr, buffer, count);
734}
735#endif
736
737#ifndef ioread16_rep
738#define ioread16_rep ioread16_rep
739static inline void ioread16_rep(const volatile void __iomem *addr,
740 void *buffer, unsigned int count)
741{
742 readsw(addr, buffer, count);
743}
744#endif
745
746#ifndef ioread32_rep
747#define ioread32_rep ioread32_rep
748static inline void ioread32_rep(const volatile void __iomem *addr,
749 void *buffer, unsigned int count)
750{
751 readsl(addr, buffer, count);
752}
753#endif
754
Horia Geantă9e44fb12016-05-19 18:10:56 +0300755#ifdef CONFIG_64BIT
756#ifndef ioread64_rep
757#define ioread64_rep ioread64_rep
758static inline void ioread64_rep(const volatile void __iomem *addr,
759 void *buffer, unsigned int count)
760{
761 readsq(addr, buffer, count);
762}
763#endif
764#endif /* CONFIG_64BIT */
765
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200766#ifndef iowrite8_rep
767#define iowrite8_rep iowrite8_rep
768static inline void iowrite8_rep(volatile void __iomem *addr,
769 const void *buffer,
770 unsigned int count)
771{
772 writesb(addr, buffer, count);
773}
774#endif
775
776#ifndef iowrite16_rep
777#define iowrite16_rep iowrite16_rep
778static inline void iowrite16_rep(volatile void __iomem *addr,
779 const void *buffer,
780 unsigned int count)
781{
782 writesw(addr, buffer, count);
783}
784#endif
785
786#ifndef iowrite32_rep
787#define iowrite32_rep iowrite32_rep
788static inline void iowrite32_rep(volatile void __iomem *addr,
789 const void *buffer,
790 unsigned int count)
791{
792 writesl(addr, buffer, count);
793}
794#endif
Horia Geantă9e44fb12016-05-19 18:10:56 +0300795
796#ifdef CONFIG_64BIT
797#ifndef iowrite64_rep
798#define iowrite64_rep iowrite64_rep
799static inline void iowrite64_rep(volatile void __iomem *addr,
800 const void *buffer,
801 unsigned int count)
802{
803 writesq(addr, buffer, count);
804}
805#endif
806#endif /* CONFIG_64BIT */
Thierry Reding9216efa2014-10-01 15:20:33 +0200807#endif /* CONFIG_GENERIC_IOMAP */
808
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000809#ifdef __KERNEL__
810
811#include <linux/vmalloc.h>
Thierry Reding9216efa2014-10-01 15:20:33 +0200812#define __io_virt(x) ((void __force *)(x))
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000813
814#ifndef CONFIG_GENERIC_IOMAP
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000815struct pci_dev;
Jan Glaubercd248342012-11-29 12:50:30 +0100816extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);
817
818#ifndef pci_iounmap
Thierry Reding9216efa2014-10-01 15:20:33 +0200819#define pci_iounmap pci_iounmap
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000820static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
821{
822}
Jan Glaubercd248342012-11-29 12:50:30 +0100823#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000824#endif /* CONFIG_GENERIC_IOMAP */
825
826/*
827 * Change virtual addresses to physical addresses and vv.
828 * These are pretty trivial
829 */
Jan Glaubercd248342012-11-29 12:50:30 +0100830#ifndef virt_to_phys
Thierry Reding9216efa2014-10-01 15:20:33 +0200831#define virt_to_phys virt_to_phys
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000832static inline unsigned long virt_to_phys(volatile void *address)
833{
834 return __pa((unsigned long)address);
835}
Thierry Reding9216efa2014-10-01 15:20:33 +0200836#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000837
Thierry Reding9216efa2014-10-01 15:20:33 +0200838#ifndef phys_to_virt
839#define phys_to_virt phys_to_virt
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000840static inline void *phys_to_virt(unsigned long address)
841{
842 return __va(address);
843}
Jan Glaubercd248342012-11-29 12:50:30 +0100844#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000845
Luis R. Rodriguez8c7ea502015-07-09 17:28:16 -0700846/**
847 * DOC: ioremap() and ioremap_*() variants
848 *
849 * If you have an IOMMU your architecture is expected to have both ioremap()
850 * and iounmap() implemented otherwise the asm-generic helpers will provide a
851 * direct mapping.
852 *
853 * There are ioremap_*() call variants, if you have no IOMMU we naturally will
854 * default to direct mapping for all of them, you can override these defaults.
855 * If you have an IOMMU you are highly encouraged to provide your own
856 * ioremap variant implementation as there currently is no safe architecture
857 * agnostic default. To avoid possible improper behaviour default asm-generic
858 * ioremap_*() variants all return NULL when an IOMMU is available. If you've
859 * defined your own ioremap_*() variant you must then declare your own
860 * ioremap_*() variant as defined to itself to avoid the default NULL return.
861 */
862
863#ifdef CONFIG_MMU
864
865#ifndef ioremap_uc
866#define ioremap_uc ioremap_uc
867static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
868{
869 return NULL;
870}
871#endif
872
873#else /* !CONFIG_MMU */
874
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000875/*
876 * Change "struct page" to physical address.
Jonas Bonnf1ecc692011-07-02 17:17:35 +0200877 *
878 * This implementation is for the no-MMU case only... if you have an MMU
879 * you'll need to provide your own definitions.
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000880 */
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000881
Thierry Reding9216efa2014-10-01 15:20:33 +0200882#ifndef ioremap
883#define ioremap ioremap
884static inline void __iomem *ioremap(phys_addr_t offset, size_t size)
885{
886 return (void __iomem *)(unsigned long)offset;
887}
888#endif
889
890#ifndef __ioremap
891#define __ioremap __ioremap
892static inline void __iomem *__ioremap(phys_addr_t offset, size_t size,
893 unsigned long flags)
894{
895 return ioremap(offset, size);
896}
897#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000898
899#ifndef ioremap_nocache
Thierry Reding9216efa2014-10-01 15:20:33 +0200900#define ioremap_nocache ioremap_nocache
901static inline void __iomem *ioremap_nocache(phys_addr_t offset, size_t size)
902{
903 return ioremap(offset, size);
904}
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000905#endif
906
Luis R. Rodrigueze4b6be332015-05-11 10:15:53 +0200907#ifndef ioremap_uc
908#define ioremap_uc ioremap_uc
909static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
910{
911 return ioremap_nocache(offset, size);
912}
913#endif
914
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000915#ifndef ioremap_wc
Thierry Reding9216efa2014-10-01 15:20:33 +0200916#define ioremap_wc ioremap_wc
917static inline void __iomem *ioremap_wc(phys_addr_t offset, size_t size)
918{
919 return ioremap_nocache(offset, size);
920}
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000921#endif
922
Toshi Kanid8382702015-06-04 18:55:15 +0200923#ifndef ioremap_wt
924#define ioremap_wt ioremap_wt
925static inline void __iomem *ioremap_wt(phys_addr_t offset, size_t size)
926{
927 return ioremap_nocache(offset, size);
928}
929#endif
930
Thierry Reding9216efa2014-10-01 15:20:33 +0200931#ifndef iounmap
932#define iounmap iounmap
Toshi Kanid8382702015-06-04 18:55:15 +0200933
Mark Saltere66d3c42011-10-04 09:25:56 -0400934static inline void iounmap(void __iomem *addr)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000935{
936}
Thierry Reding9216efa2014-10-01 15:20:33 +0200937#endif
Jonas Bonnf1ecc692011-07-02 17:17:35 +0200938#endif /* CONFIG_MMU */
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000939
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700940#ifdef CONFIG_HAS_IOPORT_MAP
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000941#ifndef CONFIG_GENERIC_IOMAP
Thierry Reding9216efa2014-10-01 15:20:33 +0200942#ifndef ioport_map
943#define ioport_map ioport_map
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000944static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
945{
Liviu Dudau112eeaa2014-09-29 15:29:20 +0100946 return PCI_IOBASE + (port & IO_SPACE_LIMIT);
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000947}
Thierry Reding9216efa2014-10-01 15:20:33 +0200948#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000949
Thierry Reding9216efa2014-10-01 15:20:33 +0200950#ifndef ioport_unmap
951#define ioport_unmap ioport_unmap
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000952static inline void ioport_unmap(void __iomem *p)
953{
954}
Thierry Reding9216efa2014-10-01 15:20:33 +0200955#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000956#else /* CONFIG_GENERIC_IOMAP */
957extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
958extern void ioport_unmap(void __iomem *p);
959#endif /* CONFIG_GENERIC_IOMAP */
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700960#endif /* CONFIG_HAS_IOPORT_MAP */
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000961
Andy Shevchenkoeabc2a72017-06-30 20:09:33 +0300962/*
963 * Convert a virtual cached pointer to an uncached pointer
964 */
Michael Holzheu576ebd72013-05-21 16:08:22 +0200965#ifndef xlate_dev_kmem_ptr
Thierry Reding9216efa2014-10-01 15:20:33 +0200966#define xlate_dev_kmem_ptr xlate_dev_kmem_ptr
967static inline void *xlate_dev_kmem_ptr(void *addr)
968{
969 return addr;
970}
Michael Holzheu576ebd72013-05-21 16:08:22 +0200971#endif
Thierry Reding9216efa2014-10-01 15:20:33 +0200972
Michael Holzheu576ebd72013-05-21 16:08:22 +0200973#ifndef xlate_dev_mem_ptr
Thierry Reding9216efa2014-10-01 15:20:33 +0200974#define xlate_dev_mem_ptr xlate_dev_mem_ptr
975static inline void *xlate_dev_mem_ptr(phys_addr_t addr)
976{
977 return __va(addr);
978}
979#endif
980
981#ifndef unxlate_dev_mem_ptr
982#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
983static inline void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
984{
985}
Michael Holzheu576ebd72013-05-21 16:08:22 +0200986#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000987
James Hoganc93d0312012-11-23 16:13:05 +0000988#ifdef CONFIG_VIRT_TO_BUS
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000989#ifndef virt_to_bus
Thierry Reding9216efa2014-10-01 15:20:33 +0200990static inline unsigned long virt_to_bus(void *address)
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000991{
Thierry Reding9216efa2014-10-01 15:20:33 +0200992 return (unsigned long)address;
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000993}
994
995static inline void *bus_to_virt(unsigned long address)
996{
Thierry Reding9216efa2014-10-01 15:20:33 +0200997 return (void *)address;
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +0000998}
999#endif
James Hoganc93d0312012-11-23 16:13:05 +00001000#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +00001001
Jan Glaubercd248342012-11-29 12:50:30 +01001002#ifndef memset_io
Thierry Reding9216efa2014-10-01 15:20:33 +02001003#define memset_io memset_io
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001004/**
1005 * memset_io Set a range of I/O memory to a constant value
1006 * @addr: The beginning of the I/O-memory range to set
1007 * @val: The value to set the memory to
1008 * @count: The number of bytes to set
1009 *
1010 * Set a range of I/O memory to a given value.
1011 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001012static inline void memset_io(volatile void __iomem *addr, int value,
1013 size_t size)
1014{
1015 memset(__io_virt(addr), value, size);
1016}
Jan Glaubercd248342012-11-29 12:50:30 +01001017#endif
1018
1019#ifndef memcpy_fromio
Thierry Reding9216efa2014-10-01 15:20:33 +02001020#define memcpy_fromio memcpy_fromio
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001021/**
1022 * memcpy_fromio Copy a block of data from I/O memory
1023 * @dst: The (RAM) destination for the copy
1024 * @src: The (I/O memory) source for the data
1025 * @count: The number of bytes to copy
1026 *
1027 * Copy a block of data from I/O memory.
1028 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001029static inline void memcpy_fromio(void *buffer,
1030 const volatile void __iomem *addr,
1031 size_t size)
1032{
1033 memcpy(buffer, __io_virt(addr), size);
1034}
Jan Glaubercd248342012-11-29 12:50:30 +01001035#endif
Thierry Reding9216efa2014-10-01 15:20:33 +02001036
Jan Glaubercd248342012-11-29 12:50:30 +01001037#ifndef memcpy_toio
Thierry Reding9216efa2014-10-01 15:20:33 +02001038#define memcpy_toio memcpy_toio
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001039/**
1040 * memcpy_toio Copy a block of data into I/O memory
1041 * @dst: The (I/O memory) destination for the copy
1042 * @src: The (RAM) source for the data
1043 * @count: The number of bytes to copy
1044 *
1045 * Copy a block of data to I/O memory.
1046 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001047static inline void memcpy_toio(volatile void __iomem *addr, const void *buffer,
1048 size_t size)
1049{
1050 memcpy(__io_virt(addr), buffer, size);
1051}
Jan Glaubercd248342012-11-29 12:50:30 +01001052#endif
Arnd Bergmann3f7e212d2009-05-13 22:56:35 +00001053
1054#endif /* __KERNEL__ */
1055
1056#endif /* __ASM_GENERIC_IO_H */