blob: 964725e4f459b97bedec445fd0a14855bf938c27 [file] [log] [blame]
Arnd Bergmann3f7e2122009-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 Bergmann3f7e2122009-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 Bergmann3f7e2122009-05-13 22:56:35 +000025#define mmiowb() do {} while (0)
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040026#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +000027
Sinan Kaya64e2c6732018-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 Bergmann3f7e2122009-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 Bergmann3f7e2122009-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 Bergmann3f7e2122009-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 Bergmann3f7e2122009-05-13 22:56:35 +000085}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040086#endif
Arnd Bergmann3f7e2122009-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 Bergmann3f7e2122009-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 Bergmann3f7e2122009-05-13 22:56:35 +000093}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -040094#endif
Arnd Bergmann3f7e2122009-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 Bergmann3f7e2122009-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 Bergmann3f7e2122009-05-13 22:56:35 +0000101}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400102#endif
Arnd Bergmann3f7e2122009-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{
Sinan Kaya032d59e2018-04-05 09:09:10 -0400157 u8 val;
158
159 __io_br();
160 val = __raw_readb(addr);
161 __io_ar();
162 return val;
Thierry Reding9216efa2014-10-01 15:20:33 +0200163}
164#endif
165
166#ifndef readw
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100167#define readw readw
168static inline u16 readw(const volatile void __iomem *addr)
169{
Sinan Kaya032d59e2018-04-05 09:09:10 -0400170 u16 val;
171
172 __io_br();
173 val = __le16_to_cpu(__raw_readw(addr));
174 __io_ar();
175 return val;
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100176}
Thierry Reding9216efa2014-10-01 15:20:33 +0200177#endif
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100178
Thierry Reding9216efa2014-10-01 15:20:33 +0200179#ifndef readl
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100180#define readl readl
181static inline u32 readl(const volatile void __iomem *addr)
182{
Sinan Kaya032d59e2018-04-05 09:09:10 -0400183 u32 val;
184
185 __io_br();
186 val = __le32_to_cpu(__raw_readl(addr));
187 __io_ar();
188 return val;
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100189}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400190#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000191
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000192#ifdef CONFIG_64BIT
Thierry Reding9216efa2014-10-01 15:20:33 +0200193#ifndef readq
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100194#define readq readq
195static inline u64 readq(const volatile void __iomem *addr)
196{
Sinan Kaya032d59e2018-04-05 09:09:10 -0400197 u64 val;
198
199 __io_br();
200 val = __le64_to_cpu(__raw_readq(addr));
201 __io_ar();
202 return val;
Heiko Carstens7292e7e2013-01-07 14:17:23 +0100203}
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000204#endif
Jan Glaubercd248342012-11-29 12:50:30 +0100205#endif /* CONFIG_64BIT */
206
Thierry Reding9216efa2014-10-01 15:20:33 +0200207#ifndef writeb
208#define writeb writeb
209static inline void writeb(u8 value, volatile void __iomem *addr)
210{
Sinan Kaya755bd042018-04-05 09:09:11 -0400211 __io_bw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200212 __raw_writeb(value, addr);
Sinan Kaya755bd042018-04-05 09:09:11 -0400213 __io_aw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200214}
GuanXuetao7dc59bd2011-02-22 19:06:43 +0800215#endif
216
Thierry Reding9216efa2014-10-01 15:20:33 +0200217#ifndef writew
218#define writew writew
219static inline void writew(u16 value, volatile void __iomem *addr)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000220{
Sinan Kaya755bd042018-04-05 09:09:11 -0400221 __io_bw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200222 __raw_writew(cpu_to_le16(value), addr);
Sinan Kaya755bd042018-04-05 09:09:11 -0400223 __io_aw();
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000224}
Thierry Reding9216efa2014-10-01 15:20:33 +0200225#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000226
Thierry Reding9216efa2014-10-01 15:20:33 +0200227#ifndef writel
228#define writel writel
229static inline void writel(u32 value, volatile void __iomem *addr)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000230{
Sinan Kaya755bd042018-04-05 09:09:11 -0400231 __io_bw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200232 __raw_writel(__cpu_to_le32(value), addr);
Sinan Kaya755bd042018-04-05 09:09:11 -0400233 __io_aw();
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000234}
Thierry Reding9216efa2014-10-01 15:20:33 +0200235#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000236
Thierry Reding9216efa2014-10-01 15:20:33 +0200237#ifdef CONFIG_64BIT
238#ifndef writeq
239#define writeq writeq
240static inline void writeq(u64 value, volatile void __iomem *addr)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000241{
Sinan Kaya755bd042018-04-05 09:09:11 -0400242 __io_bw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200243 __raw_writeq(__cpu_to_le64(value), addr);
Sinan Kaya755bd042018-04-05 09:09:11 -0400244 __io_aw();
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000245}
Thierry Reding9216efa2014-10-01 15:20:33 +0200246#endif
247#endif /* CONFIG_64BIT */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000248
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200249/*
Arnd Bergmann1c8d2962014-11-11 19:55:45 +0100250 * {read,write}{b,w,l,q}_relaxed() are like the regular version, but
251 * are not guaranteed to provide ordering against spinlocks or memory
252 * accesses.
253 */
254#ifndef readb_relaxed
255#define readb_relaxed readb
256#endif
257
258#ifndef readw_relaxed
259#define readw_relaxed readw
260#endif
261
262#ifndef readl_relaxed
263#define readl_relaxed readl
264#endif
265
Robin Murphye5112672016-04-26 11:38:20 +0100266#if defined(readq) && !defined(readq_relaxed)
Will Deacon9439eb32013-09-03 10:44:00 +0100267#define readq_relaxed readq
268#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000269
Arnd Bergmann1c8d2962014-11-11 19:55:45 +0100270#ifndef writeb_relaxed
271#define writeb_relaxed writeb
272#endif
273
274#ifndef writew_relaxed
275#define writew_relaxed writew
276#endif
277
278#ifndef writel_relaxed
279#define writel_relaxed writel
280#endif
281
Robin Murphye5112672016-04-26 11:38:20 +0100282#if defined(writeq) && !defined(writeq_relaxed)
Arnd Bergmann1c8d2962014-11-11 19:55:45 +0100283#define writeq_relaxed writeq
284#endif
285
286/*
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200287 * {read,write}s{b,w,l,q}() repeatedly access the same memory address in
288 * native endianness in 8-, 16-, 32- or 64-bit chunks (@count times).
289 */
290#ifndef readsb
291#define readsb readsb
292static inline void readsb(const volatile void __iomem *addr, void *buffer,
293 unsigned int count)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000294{
295 if (count) {
296 u8 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200297
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000298 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200299 u8 x = __raw_readb(addr);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000300 *buf++ = x;
301 } while (--count);
302 }
303}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400304#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000305
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200306#ifndef readsw
307#define readsw readsw
308static inline void readsw(const volatile void __iomem *addr, void *buffer,
309 unsigned int count)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000310{
311 if (count) {
312 u16 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200313
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000314 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200315 u16 x = __raw_readw(addr);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000316 *buf++ = x;
317 } while (--count);
318 }
319}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400320#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000321
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200322#ifndef readsl
323#define readsl readsl
324static inline void readsl(const volatile void __iomem *addr, void *buffer,
325 unsigned int count)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000326{
327 if (count) {
328 u32 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200329
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000330 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200331 u32 x = __raw_readl(addr);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000332 *buf++ = x;
333 } while (--count);
334 }
335}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400336#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000337
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200338#ifdef CONFIG_64BIT
339#ifndef readsq
340#define readsq readsq
341static inline void readsq(const volatile void __iomem *addr, void *buffer,
342 unsigned int count)
343{
344 if (count) {
345 u64 *buf = buffer;
346
347 do {
348 u64 x = __raw_readq(addr);
349 *buf++ = x;
350 } while (--count);
351 }
352}
353#endif
354#endif /* CONFIG_64BIT */
355
356#ifndef writesb
357#define writesb writesb
358static inline void writesb(volatile void __iomem *addr, const void *buffer,
359 unsigned int count)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000360{
361 if (count) {
362 const u8 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200363
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000364 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200365 __raw_writeb(*buf++, addr);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000366 } while (--count);
367 }
368}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400369#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000370
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200371#ifndef writesw
372#define writesw writesw
373static inline void writesw(volatile void __iomem *addr, const void *buffer,
374 unsigned int count)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000375{
376 if (count) {
377 const u16 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200378
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000379 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200380 __raw_writew(*buf++, addr);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000381 } while (--count);
382 }
383}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400384#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000385
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200386#ifndef writesl
387#define writesl writesl
388static inline void writesl(volatile void __iomem *addr, const void *buffer,
389 unsigned int count)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000390{
391 if (count) {
392 const u32 *buf = buffer;
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200393
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000394 do {
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200395 __raw_writel(*buf++, addr);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000396 } while (--count);
397 }
398}
Mike Frysinger35dbc0e2010-10-18 03:09:39 -0400399#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000400
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200401#ifdef CONFIG_64BIT
402#ifndef writesq
403#define writesq writesq
404static inline void writesq(volatile void __iomem *addr, const void *buffer,
405 unsigned int count)
406{
407 if (count) {
408 const u64 *buf = buffer;
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000409
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200410 do {
411 __raw_writeq(*buf++, addr);
412 } while (--count);
413 }
414}
415#endif
416#endif /* CONFIG_64BIT */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000417
Thierry Reding9216efa2014-10-01 15:20:33 +0200418#ifndef PCI_IOBASE
419#define PCI_IOBASE ((void __iomem *)0)
420#endif
421
GuanXuetao7dc59bd2011-02-22 19:06:43 +0800422#ifndef IO_SPACE_LIMIT
423#define IO_SPACE_LIMIT 0xffff
424#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000425
Thierry Reding9216efa2014-10-01 15:20:33 +0200426/*
427 * {in,out}{b,w,l}() access little endian I/O. {in,out}{b,w,l}_p() can be
428 * implemented on hardware that needs an additional delay for I/O accesses to
429 * take effect.
430 */
431
432#ifndef inb
433#define inb inb
434static inline u8 inb(unsigned long addr)
435{
436 return readb(PCI_IOBASE + addr);
437}
438#endif
439
440#ifndef inw
441#define inw inw
442static inline u16 inw(unsigned long addr)
443{
444 return readw(PCI_IOBASE + addr);
445}
446#endif
447
448#ifndef inl
449#define inl inl
450static inline u32 inl(unsigned long addr)
451{
452 return readl(PCI_IOBASE + addr);
453}
454#endif
455
456#ifndef outb
457#define outb outb
458static inline void outb(u8 value, unsigned long addr)
459{
460 writeb(value, PCI_IOBASE + addr);
461}
462#endif
463
464#ifndef outw
465#define outw outw
466static inline void outw(u16 value, unsigned long addr)
467{
468 writew(value, PCI_IOBASE + addr);
469}
470#endif
471
472#ifndef outl
473#define outl outl
474static inline void outl(u32 value, unsigned long addr)
475{
476 writel(value, PCI_IOBASE + addr);
477}
478#endif
479
480#ifndef inb_p
481#define inb_p inb_p
482static inline u8 inb_p(unsigned long addr)
483{
484 return inb(addr);
485}
486#endif
487
488#ifndef inw_p
489#define inw_p inw_p
490static inline u16 inw_p(unsigned long addr)
491{
492 return inw(addr);
493}
494#endif
495
496#ifndef inl_p
497#define inl_p inl_p
498static inline u32 inl_p(unsigned long addr)
499{
500 return inl(addr);
501}
502#endif
503
504#ifndef outb_p
505#define outb_p outb_p
506static inline void outb_p(u8 value, unsigned long addr)
507{
508 outb(value, addr);
509}
510#endif
511
512#ifndef outw_p
513#define outw_p outw_p
514static inline void outw_p(u16 value, unsigned long addr)
515{
516 outw(value, addr);
517}
518#endif
519
520#ifndef outl_p
521#define outl_p outl_p
522static inline void outl_p(u32 value, unsigned long addr)
523{
524 outl(value, addr);
525}
526#endif
527
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200528/*
529 * {in,out}s{b,w,l}{,_p}() are variants of the above that repeatedly access a
530 * single I/O port multiple times.
531 */
532
533#ifndef insb
534#define insb insb
535static inline void insb(unsigned long addr, void *buffer, unsigned int count)
536{
537 readsb(PCI_IOBASE + addr, buffer, count);
538}
539#endif
540
541#ifndef insw
542#define insw insw
543static inline void insw(unsigned long addr, void *buffer, unsigned int count)
544{
545 readsw(PCI_IOBASE + addr, buffer, count);
546}
547#endif
548
549#ifndef insl
550#define insl insl
551static inline void insl(unsigned long addr, void *buffer, unsigned int count)
552{
553 readsl(PCI_IOBASE + addr, buffer, count);
554}
555#endif
556
557#ifndef outsb
558#define outsb outsb
559static inline void outsb(unsigned long addr, const void *buffer,
560 unsigned int count)
561{
562 writesb(PCI_IOBASE + addr, buffer, count);
563}
564#endif
565
566#ifndef outsw
567#define outsw outsw
568static inline void outsw(unsigned long addr, const void *buffer,
569 unsigned int count)
570{
571 writesw(PCI_IOBASE + addr, buffer, count);
572}
573#endif
574
575#ifndef outsl
576#define outsl outsl
577static inline void outsl(unsigned long addr, const void *buffer,
578 unsigned int count)
579{
580 writesl(PCI_IOBASE + addr, buffer, count);
581}
582#endif
583
584#ifndef insb_p
585#define insb_p insb_p
586static inline void insb_p(unsigned long addr, void *buffer, unsigned int count)
587{
588 insb(addr, buffer, count);
589}
590#endif
591
592#ifndef insw_p
593#define insw_p insw_p
594static inline void insw_p(unsigned long addr, void *buffer, unsigned int count)
595{
596 insw(addr, buffer, count);
597}
598#endif
599
600#ifndef insl_p
601#define insl_p insl_p
602static inline void insl_p(unsigned long addr, void *buffer, unsigned int count)
603{
604 insl(addr, buffer, count);
605}
606#endif
607
608#ifndef outsb_p
609#define outsb_p outsb_p
610static inline void outsb_p(unsigned long addr, const void *buffer,
611 unsigned int count)
612{
613 outsb(addr, buffer, count);
614}
615#endif
616
617#ifndef outsw_p
618#define outsw_p outsw_p
619static inline void outsw_p(unsigned long addr, const void *buffer,
620 unsigned int count)
621{
622 outsw(addr, buffer, count);
623}
624#endif
625
626#ifndef outsl_p
627#define outsl_p outsl_p
628static inline void outsl_p(unsigned long addr, const void *buffer,
629 unsigned int count)
630{
631 outsl(addr, buffer, count);
632}
633#endif
634
Thierry Reding9216efa2014-10-01 15:20:33 +0200635#ifndef CONFIG_GENERIC_IOMAP
636#ifndef ioread8
637#define ioread8 ioread8
638static inline u8 ioread8(const volatile void __iomem *addr)
639{
640 return readb(addr);
641}
642#endif
643
644#ifndef ioread16
645#define ioread16 ioread16
646static inline u16 ioread16(const volatile void __iomem *addr)
647{
648 return readw(addr);
649}
650#endif
651
652#ifndef ioread32
653#define ioread32 ioread32
654static inline u32 ioread32(const volatile void __iomem *addr)
655{
656 return readl(addr);
657}
658#endif
659
Horia Geantă9e44fb12016-05-19 18:10:56 +0300660#ifdef CONFIG_64BIT
661#ifndef ioread64
662#define ioread64 ioread64
663static inline u64 ioread64(const volatile void __iomem *addr)
664{
665 return readq(addr);
666}
667#endif
668#endif /* CONFIG_64BIT */
669
Thierry Reding9216efa2014-10-01 15:20:33 +0200670#ifndef iowrite8
671#define iowrite8 iowrite8
672static inline void iowrite8(u8 value, volatile void __iomem *addr)
673{
674 writeb(value, addr);
675}
676#endif
677
678#ifndef iowrite16
679#define iowrite16 iowrite16
680static inline void iowrite16(u16 value, volatile void __iomem *addr)
681{
682 writew(value, addr);
683}
684#endif
685
686#ifndef iowrite32
687#define iowrite32 iowrite32
688static inline void iowrite32(u32 value, volatile void __iomem *addr)
689{
690 writel(value, addr);
691}
692#endif
693
Horia Geantă9e44fb12016-05-19 18:10:56 +0300694#ifdef CONFIG_64BIT
695#ifndef iowrite64
696#define iowrite64 iowrite64
697static inline void iowrite64(u64 value, volatile void __iomem *addr)
698{
699 writeq(value, addr);
700}
701#endif
702#endif /* CONFIG_64BIT */
703
Thierry Reding9216efa2014-10-01 15:20:33 +0200704#ifndef ioread16be
705#define ioread16be ioread16be
706static inline u16 ioread16be(const volatile void __iomem *addr)
707{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300708 return swab16(readw(addr));
Thierry Reding9216efa2014-10-01 15:20:33 +0200709}
710#endif
711
712#ifndef ioread32be
713#define ioread32be ioread32be
714static inline u32 ioread32be(const volatile void __iomem *addr)
715{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300716 return swab32(readl(addr));
Thierry Reding9216efa2014-10-01 15:20:33 +0200717}
718#endif
719
Horia Geantă9e44fb12016-05-19 18:10:56 +0300720#ifdef CONFIG_64BIT
721#ifndef ioread64be
722#define ioread64be ioread64be
723static inline u64 ioread64be(const volatile void __iomem *addr)
724{
725 return swab64(readq(addr));
726}
727#endif
728#endif /* CONFIG_64BIT */
729
Thierry Reding9216efa2014-10-01 15:20:33 +0200730#ifndef iowrite16be
731#define iowrite16be iowrite16be
732static inline void iowrite16be(u16 value, void volatile __iomem *addr)
733{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300734 writew(swab16(value), addr);
Thierry Reding9216efa2014-10-01 15:20:33 +0200735}
736#endif
737
738#ifndef iowrite32be
739#define iowrite32be iowrite32be
740static inline void iowrite32be(u32 value, volatile void __iomem *addr)
741{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300742 writel(swab32(value), addr);
Thierry Reding9216efa2014-10-01 15:20:33 +0200743}
744#endif
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200745
Horia Geantă9e44fb12016-05-19 18:10:56 +0300746#ifdef CONFIG_64BIT
747#ifndef iowrite64be
748#define iowrite64be iowrite64be
749static inline void iowrite64be(u64 value, volatile void __iomem *addr)
750{
751 writeq(swab64(value), addr);
752}
753#endif
754#endif /* CONFIG_64BIT */
755
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200756#ifndef ioread8_rep
757#define ioread8_rep ioread8_rep
758static inline void ioread8_rep(const volatile void __iomem *addr, void *buffer,
759 unsigned int count)
760{
761 readsb(addr, buffer, count);
762}
763#endif
764
765#ifndef ioread16_rep
766#define ioread16_rep ioread16_rep
767static inline void ioread16_rep(const volatile void __iomem *addr,
768 void *buffer, unsigned int count)
769{
770 readsw(addr, buffer, count);
771}
772#endif
773
774#ifndef ioread32_rep
775#define ioread32_rep ioread32_rep
776static inline void ioread32_rep(const volatile void __iomem *addr,
777 void *buffer, unsigned int count)
778{
779 readsl(addr, buffer, count);
780}
781#endif
782
Horia Geantă9e44fb12016-05-19 18:10:56 +0300783#ifdef CONFIG_64BIT
784#ifndef ioread64_rep
785#define ioread64_rep ioread64_rep
786static inline void ioread64_rep(const volatile void __iomem *addr,
787 void *buffer, unsigned int count)
788{
789 readsq(addr, buffer, count);
790}
791#endif
792#endif /* CONFIG_64BIT */
793
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200794#ifndef iowrite8_rep
795#define iowrite8_rep iowrite8_rep
796static inline void iowrite8_rep(volatile void __iomem *addr,
797 const void *buffer,
798 unsigned int count)
799{
800 writesb(addr, buffer, count);
801}
802#endif
803
804#ifndef iowrite16_rep
805#define iowrite16_rep iowrite16_rep
806static inline void iowrite16_rep(volatile void __iomem *addr,
807 const void *buffer,
808 unsigned int count)
809{
810 writesw(addr, buffer, count);
811}
812#endif
813
814#ifndef iowrite32_rep
815#define iowrite32_rep iowrite32_rep
816static inline void iowrite32_rep(volatile void __iomem *addr,
817 const void *buffer,
818 unsigned int count)
819{
820 writesl(addr, buffer, count);
821}
822#endif
Horia Geantă9e44fb12016-05-19 18:10:56 +0300823
824#ifdef CONFIG_64BIT
825#ifndef iowrite64_rep
826#define iowrite64_rep iowrite64_rep
827static inline void iowrite64_rep(volatile void __iomem *addr,
828 const void *buffer,
829 unsigned int count)
830{
831 writesq(addr, buffer, count);
832}
833#endif
834#endif /* CONFIG_64BIT */
Thierry Reding9216efa2014-10-01 15:20:33 +0200835#endif /* CONFIG_GENERIC_IOMAP */
836
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000837#ifdef __KERNEL__
838
839#include <linux/vmalloc.h>
Thierry Reding9216efa2014-10-01 15:20:33 +0200840#define __io_virt(x) ((void __force *)(x))
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000841
842#ifndef CONFIG_GENERIC_IOMAP
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000843struct pci_dev;
Jan Glaubercd248342012-11-29 12:50:30 +0100844extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);
845
846#ifndef pci_iounmap
Thierry Reding9216efa2014-10-01 15:20:33 +0200847#define pci_iounmap pci_iounmap
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000848static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
849{
850}
Jan Glaubercd248342012-11-29 12:50:30 +0100851#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000852#endif /* CONFIG_GENERIC_IOMAP */
853
854/*
855 * Change virtual addresses to physical addresses and vv.
856 * These are pretty trivial
857 */
Jan Glaubercd248342012-11-29 12:50:30 +0100858#ifndef virt_to_phys
Thierry Reding9216efa2014-10-01 15:20:33 +0200859#define virt_to_phys virt_to_phys
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000860static inline unsigned long virt_to_phys(volatile void *address)
861{
862 return __pa((unsigned long)address);
863}
Thierry Reding9216efa2014-10-01 15:20:33 +0200864#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000865
Thierry Reding9216efa2014-10-01 15:20:33 +0200866#ifndef phys_to_virt
867#define phys_to_virt phys_to_virt
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000868static inline void *phys_to_virt(unsigned long address)
869{
870 return __va(address);
871}
Jan Glaubercd248342012-11-29 12:50:30 +0100872#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000873
Luis R. Rodriguez8c7ea502015-07-09 17:28:16 -0700874/**
875 * DOC: ioremap() and ioremap_*() variants
876 *
877 * If you have an IOMMU your architecture is expected to have both ioremap()
878 * and iounmap() implemented otherwise the asm-generic helpers will provide a
879 * direct mapping.
880 *
881 * There are ioremap_*() call variants, if you have no IOMMU we naturally will
882 * default to direct mapping for all of them, you can override these defaults.
883 * If you have an IOMMU you are highly encouraged to provide your own
884 * ioremap variant implementation as there currently is no safe architecture
885 * agnostic default. To avoid possible improper behaviour default asm-generic
886 * ioremap_*() variants all return NULL when an IOMMU is available. If you've
887 * defined your own ioremap_*() variant you must then declare your own
888 * ioremap_*() variant as defined to itself to avoid the default NULL return.
889 */
890
891#ifdef CONFIG_MMU
892
893#ifndef ioremap_uc
894#define ioremap_uc ioremap_uc
895static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
896{
897 return NULL;
898}
899#endif
900
901#else /* !CONFIG_MMU */
902
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000903/*
904 * Change "struct page" to physical address.
Jonas Bonnf1ecc692011-07-02 17:17:35 +0200905 *
906 * This implementation is for the no-MMU case only... if you have an MMU
907 * you'll need to provide your own definitions.
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000908 */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000909
Thierry Reding9216efa2014-10-01 15:20:33 +0200910#ifndef ioremap
911#define ioremap ioremap
912static inline void __iomem *ioremap(phys_addr_t offset, size_t size)
913{
914 return (void __iomem *)(unsigned long)offset;
915}
916#endif
917
918#ifndef __ioremap
919#define __ioremap __ioremap
920static inline void __iomem *__ioremap(phys_addr_t offset, size_t size,
921 unsigned long flags)
922{
923 return ioremap(offset, size);
924}
925#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000926
927#ifndef ioremap_nocache
Thierry Reding9216efa2014-10-01 15:20:33 +0200928#define ioremap_nocache ioremap_nocache
929static inline void __iomem *ioremap_nocache(phys_addr_t offset, size_t size)
930{
931 return ioremap(offset, size);
932}
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000933#endif
934
Luis R. Rodrigueze4b6be332015-05-11 10:15:53 +0200935#ifndef ioremap_uc
936#define ioremap_uc ioremap_uc
937static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
938{
939 return ioremap_nocache(offset, size);
940}
941#endif
942
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000943#ifndef ioremap_wc
Thierry Reding9216efa2014-10-01 15:20:33 +0200944#define ioremap_wc ioremap_wc
945static inline void __iomem *ioremap_wc(phys_addr_t offset, size_t size)
946{
947 return ioremap_nocache(offset, size);
948}
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000949#endif
950
Toshi Kanid8382702015-06-04 18:55:15 +0200951#ifndef ioremap_wt
952#define ioremap_wt ioremap_wt
953static inline void __iomem *ioremap_wt(phys_addr_t offset, size_t size)
954{
955 return ioremap_nocache(offset, size);
956}
957#endif
958
Thierry Reding9216efa2014-10-01 15:20:33 +0200959#ifndef iounmap
960#define iounmap iounmap
Toshi Kanid8382702015-06-04 18:55:15 +0200961
Mark Saltere66d3c42011-10-04 09:25:56 -0400962static inline void iounmap(void __iomem *addr)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000963{
964}
Thierry Reding9216efa2014-10-01 15:20:33 +0200965#endif
Jonas Bonnf1ecc692011-07-02 17:17:35 +0200966#endif /* CONFIG_MMU */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000967
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700968#ifdef CONFIG_HAS_IOPORT_MAP
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000969#ifndef CONFIG_GENERIC_IOMAP
Thierry Reding9216efa2014-10-01 15:20:33 +0200970#ifndef ioport_map
971#define ioport_map ioport_map
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000972static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
973{
Liviu Dudau112eeaa2014-09-29 15:29:20 +0100974 return PCI_IOBASE + (port & IO_SPACE_LIMIT);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000975}
Thierry Reding9216efa2014-10-01 15:20:33 +0200976#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000977
Thierry Reding9216efa2014-10-01 15:20:33 +0200978#ifndef ioport_unmap
979#define ioport_unmap ioport_unmap
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000980static inline void ioport_unmap(void __iomem *p)
981{
982}
Thierry Reding9216efa2014-10-01 15:20:33 +0200983#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000984#else /* CONFIG_GENERIC_IOMAP */
985extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
986extern void ioport_unmap(void __iomem *p);
987#endif /* CONFIG_GENERIC_IOMAP */
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700988#endif /* CONFIG_HAS_IOPORT_MAP */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000989
Andy Shevchenkoeabc2a72017-06-30 20:09:33 +0300990/*
991 * Convert a virtual cached pointer to an uncached pointer
992 */
Michael Holzheu576ebd72013-05-21 16:08:22 +0200993#ifndef xlate_dev_kmem_ptr
Thierry Reding9216efa2014-10-01 15:20:33 +0200994#define xlate_dev_kmem_ptr xlate_dev_kmem_ptr
995static inline void *xlate_dev_kmem_ptr(void *addr)
996{
997 return addr;
998}
Michael Holzheu576ebd72013-05-21 16:08:22 +0200999#endif
Thierry Reding9216efa2014-10-01 15:20:33 +02001000
Michael Holzheu576ebd72013-05-21 16:08:22 +02001001#ifndef xlate_dev_mem_ptr
Thierry Reding9216efa2014-10-01 15:20:33 +02001002#define xlate_dev_mem_ptr xlate_dev_mem_ptr
1003static inline void *xlate_dev_mem_ptr(phys_addr_t addr)
1004{
1005 return __va(addr);
1006}
1007#endif
1008
1009#ifndef unxlate_dev_mem_ptr
1010#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
1011static inline void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
1012{
1013}
Michael Holzheu576ebd72013-05-21 16:08:22 +02001014#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001015
James Hoganc93d0312012-11-23 16:13:05 +00001016#ifdef CONFIG_VIRT_TO_BUS
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001017#ifndef virt_to_bus
Thierry Reding9216efa2014-10-01 15:20:33 +02001018static inline unsigned long virt_to_bus(void *address)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001019{
Thierry Reding9216efa2014-10-01 15:20:33 +02001020 return (unsigned long)address;
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001021}
1022
1023static inline void *bus_to_virt(unsigned long address)
1024{
Thierry Reding9216efa2014-10-01 15:20:33 +02001025 return (void *)address;
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001026}
1027#endif
James Hoganc93d0312012-11-23 16:13:05 +00001028#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001029
Jan Glaubercd248342012-11-29 12:50:30 +01001030#ifndef memset_io
Thierry Reding9216efa2014-10-01 15:20:33 +02001031#define memset_io memset_io
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001032/**
1033 * memset_io Set a range of I/O memory to a constant value
1034 * @addr: The beginning of the I/O-memory range to set
1035 * @val: The value to set the memory to
1036 * @count: The number of bytes to set
1037 *
1038 * Set a range of I/O memory to a given value.
1039 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001040static inline void memset_io(volatile void __iomem *addr, int value,
1041 size_t size)
1042{
1043 memset(__io_virt(addr), value, size);
1044}
Jan Glaubercd248342012-11-29 12:50:30 +01001045#endif
1046
1047#ifndef memcpy_fromio
Thierry Reding9216efa2014-10-01 15:20:33 +02001048#define memcpy_fromio memcpy_fromio
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001049/**
1050 * memcpy_fromio Copy a block of data from I/O memory
1051 * @dst: The (RAM) destination for the copy
1052 * @src: The (I/O memory) source for the data
1053 * @count: The number of bytes to copy
1054 *
1055 * Copy a block of data from I/O memory.
1056 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001057static inline void memcpy_fromio(void *buffer,
1058 const volatile void __iomem *addr,
1059 size_t size)
1060{
1061 memcpy(buffer, __io_virt(addr), size);
1062}
Jan Glaubercd248342012-11-29 12:50:30 +01001063#endif
Thierry Reding9216efa2014-10-01 15:20:33 +02001064
Jan Glaubercd248342012-11-29 12:50:30 +01001065#ifndef memcpy_toio
Thierry Reding9216efa2014-10-01 15:20:33 +02001066#define memcpy_toio memcpy_toio
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001067/**
1068 * memcpy_toio Copy a block of data into I/O memory
1069 * @dst: The (I/O memory) destination for the copy
1070 * @src: The (RAM) source for the data
1071 * @count: The number of bytes to copy
1072 *
1073 * Copy a block of data to I/O memory.
1074 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001075static inline void memcpy_toio(volatile void __iomem *addr, const void *buffer,
1076 size_t size)
1077{
1078 memcpy(__io_virt(addr), buffer, size);
1079}
Jan Glaubercd248342012-11-29 12:50:30 +01001080#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001081
1082#endif /* __KERNEL__ */
1083
1084#endif /* __ASM_GENERIC_IO_H */