blob: 578b6883dd6ab08833de09dfba862395dfd9063e [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{
Sinan Kaya87fe2d52018-04-05 09:09:13 -0400436 u8 val;
437
438 __io_pbr();
439 val = __raw_readb(PCI_IOBASE + addr);
440 __io_par();
441 return val;
Thierry Reding9216efa2014-10-01 15:20:33 +0200442}
443#endif
444
445#ifndef inw
446#define inw inw
447static inline u16 inw(unsigned long addr)
448{
Sinan Kaya87fe2d52018-04-05 09:09:13 -0400449 u16 val;
450
451 __io_pbr();
452 val = __le16_to_cpu(__raw_readw(PCI_IOBASE + addr));
453 __io_par();
454 return val;
Thierry Reding9216efa2014-10-01 15:20:33 +0200455}
456#endif
457
458#ifndef inl
459#define inl inl
460static inline u32 inl(unsigned long addr)
461{
Sinan Kaya87fe2d52018-04-05 09:09:13 -0400462 u32 val;
463
464 __io_pbr();
465 val = __le32_to_cpu(__raw_readl(PCI_IOBASE + addr));
466 __io_par();
467 return val;
Thierry Reding9216efa2014-10-01 15:20:33 +0200468}
469#endif
470
471#ifndef outb
472#define outb outb
473static inline void outb(u8 value, unsigned long addr)
474{
Sinan Kayaa7851aa2018-04-05 09:09:12 -0400475 __io_pbw();
476 __raw_writeb(value, PCI_IOBASE + addr);
477 __io_paw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200478}
479#endif
480
481#ifndef outw
482#define outw outw
483static inline void outw(u16 value, unsigned long addr)
484{
Sinan Kayaa7851aa2018-04-05 09:09:12 -0400485 __io_pbw();
486 __raw_writew(cpu_to_le16(value), PCI_IOBASE + addr);
487 __io_paw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200488}
489#endif
490
491#ifndef outl
492#define outl outl
493static inline void outl(u32 value, unsigned long addr)
494{
Sinan Kayaa7851aa2018-04-05 09:09:12 -0400495 __io_pbw();
496 __raw_writel(cpu_to_le32(value), PCI_IOBASE + addr);
497 __io_paw();
Thierry Reding9216efa2014-10-01 15:20:33 +0200498}
499#endif
500
501#ifndef inb_p
502#define inb_p inb_p
503static inline u8 inb_p(unsigned long addr)
504{
505 return inb(addr);
506}
507#endif
508
509#ifndef inw_p
510#define inw_p inw_p
511static inline u16 inw_p(unsigned long addr)
512{
513 return inw(addr);
514}
515#endif
516
517#ifndef inl_p
518#define inl_p inl_p
519static inline u32 inl_p(unsigned long addr)
520{
521 return inl(addr);
522}
523#endif
524
525#ifndef outb_p
526#define outb_p outb_p
527static inline void outb_p(u8 value, unsigned long addr)
528{
529 outb(value, addr);
530}
531#endif
532
533#ifndef outw_p
534#define outw_p outw_p
535static inline void outw_p(u16 value, unsigned long addr)
536{
537 outw(value, addr);
538}
539#endif
540
541#ifndef outl_p
542#define outl_p outl_p
543static inline void outl_p(u32 value, unsigned long addr)
544{
545 outl(value, addr);
546}
547#endif
548
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200549/*
550 * {in,out}s{b,w,l}{,_p}() are variants of the above that repeatedly access a
551 * single I/O port multiple times.
552 */
553
554#ifndef insb
555#define insb insb
556static inline void insb(unsigned long addr, void *buffer, unsigned int count)
557{
558 readsb(PCI_IOBASE + addr, buffer, count);
559}
560#endif
561
562#ifndef insw
563#define insw insw
564static inline void insw(unsigned long addr, void *buffer, unsigned int count)
565{
566 readsw(PCI_IOBASE + addr, buffer, count);
567}
568#endif
569
570#ifndef insl
571#define insl insl
572static inline void insl(unsigned long addr, void *buffer, unsigned int count)
573{
574 readsl(PCI_IOBASE + addr, buffer, count);
575}
576#endif
577
578#ifndef outsb
579#define outsb outsb
580static inline void outsb(unsigned long addr, const void *buffer,
581 unsigned int count)
582{
583 writesb(PCI_IOBASE + addr, buffer, count);
584}
585#endif
586
587#ifndef outsw
588#define outsw outsw
589static inline void outsw(unsigned long addr, const void *buffer,
590 unsigned int count)
591{
592 writesw(PCI_IOBASE + addr, buffer, count);
593}
594#endif
595
596#ifndef outsl
597#define outsl outsl
598static inline void outsl(unsigned long addr, const void *buffer,
599 unsigned int count)
600{
601 writesl(PCI_IOBASE + addr, buffer, count);
602}
603#endif
604
605#ifndef insb_p
606#define insb_p insb_p
607static inline void insb_p(unsigned long addr, void *buffer, unsigned int count)
608{
609 insb(addr, buffer, count);
610}
611#endif
612
613#ifndef insw_p
614#define insw_p insw_p
615static inline void insw_p(unsigned long addr, void *buffer, unsigned int count)
616{
617 insw(addr, buffer, count);
618}
619#endif
620
621#ifndef insl_p
622#define insl_p insl_p
623static inline void insl_p(unsigned long addr, void *buffer, unsigned int count)
624{
625 insl(addr, buffer, count);
626}
627#endif
628
629#ifndef outsb_p
630#define outsb_p outsb_p
631static inline void outsb_p(unsigned long addr, const void *buffer,
632 unsigned int count)
633{
634 outsb(addr, buffer, count);
635}
636#endif
637
638#ifndef outsw_p
639#define outsw_p outsw_p
640static inline void outsw_p(unsigned long addr, const void *buffer,
641 unsigned int count)
642{
643 outsw(addr, buffer, count);
644}
645#endif
646
647#ifndef outsl_p
648#define outsl_p outsl_p
649static inline void outsl_p(unsigned long addr, const void *buffer,
650 unsigned int count)
651{
652 outsl(addr, buffer, count);
653}
654#endif
655
Thierry Reding9216efa2014-10-01 15:20:33 +0200656#ifndef CONFIG_GENERIC_IOMAP
657#ifndef ioread8
658#define ioread8 ioread8
659static inline u8 ioread8(const volatile void __iomem *addr)
660{
661 return readb(addr);
662}
663#endif
664
665#ifndef ioread16
666#define ioread16 ioread16
667static inline u16 ioread16(const volatile void __iomem *addr)
668{
669 return readw(addr);
670}
671#endif
672
673#ifndef ioread32
674#define ioread32 ioread32
675static inline u32 ioread32(const volatile void __iomem *addr)
676{
677 return readl(addr);
678}
679#endif
680
Horia Geantă9e44fb12016-05-19 18:10:56 +0300681#ifdef CONFIG_64BIT
682#ifndef ioread64
683#define ioread64 ioread64
684static inline u64 ioread64(const volatile void __iomem *addr)
685{
686 return readq(addr);
687}
688#endif
689#endif /* CONFIG_64BIT */
690
Thierry Reding9216efa2014-10-01 15:20:33 +0200691#ifndef iowrite8
692#define iowrite8 iowrite8
693static inline void iowrite8(u8 value, volatile void __iomem *addr)
694{
695 writeb(value, addr);
696}
697#endif
698
699#ifndef iowrite16
700#define iowrite16 iowrite16
701static inline void iowrite16(u16 value, volatile void __iomem *addr)
702{
703 writew(value, addr);
704}
705#endif
706
707#ifndef iowrite32
708#define iowrite32 iowrite32
709static inline void iowrite32(u32 value, volatile void __iomem *addr)
710{
711 writel(value, addr);
712}
713#endif
714
Horia Geantă9e44fb12016-05-19 18:10:56 +0300715#ifdef CONFIG_64BIT
716#ifndef iowrite64
717#define iowrite64 iowrite64
718static inline void iowrite64(u64 value, volatile void __iomem *addr)
719{
720 writeq(value, addr);
721}
722#endif
723#endif /* CONFIG_64BIT */
724
Thierry Reding9216efa2014-10-01 15:20:33 +0200725#ifndef ioread16be
726#define ioread16be ioread16be
727static inline u16 ioread16be(const volatile void __iomem *addr)
728{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300729 return swab16(readw(addr));
Thierry Reding9216efa2014-10-01 15:20:33 +0200730}
731#endif
732
733#ifndef ioread32be
734#define ioread32be ioread32be
735static inline u32 ioread32be(const volatile void __iomem *addr)
736{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300737 return swab32(readl(addr));
Thierry Reding9216efa2014-10-01 15:20:33 +0200738}
739#endif
740
Horia Geantă9e44fb12016-05-19 18:10:56 +0300741#ifdef CONFIG_64BIT
742#ifndef ioread64be
743#define ioread64be ioread64be
744static inline u64 ioread64be(const volatile void __iomem *addr)
745{
746 return swab64(readq(addr));
747}
748#endif
749#endif /* CONFIG_64BIT */
750
Thierry Reding9216efa2014-10-01 15:20:33 +0200751#ifndef iowrite16be
752#define iowrite16be iowrite16be
753static inline void iowrite16be(u16 value, void volatile __iomem *addr)
754{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300755 writew(swab16(value), addr);
Thierry Reding9216efa2014-10-01 15:20:33 +0200756}
757#endif
758
759#ifndef iowrite32be
760#define iowrite32be iowrite32be
761static inline void iowrite32be(u32 value, volatile void __iomem *addr)
762{
Horia Geantă7a1aedb2016-05-19 18:10:43 +0300763 writel(swab32(value), addr);
Thierry Reding9216efa2014-10-01 15:20:33 +0200764}
765#endif
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200766
Horia Geantă9e44fb12016-05-19 18:10:56 +0300767#ifdef CONFIG_64BIT
768#ifndef iowrite64be
769#define iowrite64be iowrite64be
770static inline void iowrite64be(u64 value, volatile void __iomem *addr)
771{
772 writeq(swab64(value), addr);
773}
774#endif
775#endif /* CONFIG_64BIT */
776
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200777#ifndef ioread8_rep
778#define ioread8_rep ioread8_rep
779static inline void ioread8_rep(const volatile void __iomem *addr, void *buffer,
780 unsigned int count)
781{
782 readsb(addr, buffer, count);
783}
784#endif
785
786#ifndef ioread16_rep
787#define ioread16_rep ioread16_rep
788static inline void ioread16_rep(const volatile void __iomem *addr,
789 void *buffer, unsigned int count)
790{
791 readsw(addr, buffer, count);
792}
793#endif
794
795#ifndef ioread32_rep
796#define ioread32_rep ioread32_rep
797static inline void ioread32_rep(const volatile void __iomem *addr,
798 void *buffer, unsigned int count)
799{
800 readsl(addr, buffer, count);
801}
802#endif
803
Horia Geantă9e44fb12016-05-19 18:10:56 +0300804#ifdef CONFIG_64BIT
805#ifndef ioread64_rep
806#define ioread64_rep ioread64_rep
807static inline void ioread64_rep(const volatile void __iomem *addr,
808 void *buffer, unsigned int count)
809{
810 readsq(addr, buffer, count);
811}
812#endif
813#endif /* CONFIG_64BIT */
814
Thierry Reding9ab3a7a2014-07-04 13:07:57 +0200815#ifndef iowrite8_rep
816#define iowrite8_rep iowrite8_rep
817static inline void iowrite8_rep(volatile void __iomem *addr,
818 const void *buffer,
819 unsigned int count)
820{
821 writesb(addr, buffer, count);
822}
823#endif
824
825#ifndef iowrite16_rep
826#define iowrite16_rep iowrite16_rep
827static inline void iowrite16_rep(volatile void __iomem *addr,
828 const void *buffer,
829 unsigned int count)
830{
831 writesw(addr, buffer, count);
832}
833#endif
834
835#ifndef iowrite32_rep
836#define iowrite32_rep iowrite32_rep
837static inline void iowrite32_rep(volatile void __iomem *addr,
838 const void *buffer,
839 unsigned int count)
840{
841 writesl(addr, buffer, count);
842}
843#endif
Horia Geantă9e44fb12016-05-19 18:10:56 +0300844
845#ifdef CONFIG_64BIT
846#ifndef iowrite64_rep
847#define iowrite64_rep iowrite64_rep
848static inline void iowrite64_rep(volatile void __iomem *addr,
849 const void *buffer,
850 unsigned int count)
851{
852 writesq(addr, buffer, count);
853}
854#endif
855#endif /* CONFIG_64BIT */
Thierry Reding9216efa2014-10-01 15:20:33 +0200856#endif /* CONFIG_GENERIC_IOMAP */
857
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000858#ifdef __KERNEL__
859
860#include <linux/vmalloc.h>
Thierry Reding9216efa2014-10-01 15:20:33 +0200861#define __io_virt(x) ((void __force *)(x))
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000862
863#ifndef CONFIG_GENERIC_IOMAP
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000864struct pci_dev;
Jan Glaubercd248342012-11-29 12:50:30 +0100865extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);
866
867#ifndef pci_iounmap
Thierry Reding9216efa2014-10-01 15:20:33 +0200868#define pci_iounmap pci_iounmap
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000869static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
870{
871}
Jan Glaubercd248342012-11-29 12:50:30 +0100872#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000873#endif /* CONFIG_GENERIC_IOMAP */
874
875/*
876 * Change virtual addresses to physical addresses and vv.
877 * These are pretty trivial
878 */
Jan Glaubercd248342012-11-29 12:50:30 +0100879#ifndef virt_to_phys
Thierry Reding9216efa2014-10-01 15:20:33 +0200880#define virt_to_phys virt_to_phys
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000881static inline unsigned long virt_to_phys(volatile void *address)
882{
883 return __pa((unsigned long)address);
884}
Thierry Reding9216efa2014-10-01 15:20:33 +0200885#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000886
Thierry Reding9216efa2014-10-01 15:20:33 +0200887#ifndef phys_to_virt
888#define phys_to_virt phys_to_virt
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000889static inline void *phys_to_virt(unsigned long address)
890{
891 return __va(address);
892}
Jan Glaubercd248342012-11-29 12:50:30 +0100893#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000894
Luis R. Rodriguez8c7ea502015-07-09 17:28:16 -0700895/**
896 * DOC: ioremap() and ioremap_*() variants
897 *
898 * If you have an IOMMU your architecture is expected to have both ioremap()
899 * and iounmap() implemented otherwise the asm-generic helpers will provide a
900 * direct mapping.
901 *
902 * There are ioremap_*() call variants, if you have no IOMMU we naturally will
903 * default to direct mapping for all of them, you can override these defaults.
904 * If you have an IOMMU you are highly encouraged to provide your own
905 * ioremap variant implementation as there currently is no safe architecture
906 * agnostic default. To avoid possible improper behaviour default asm-generic
907 * ioremap_*() variants all return NULL when an IOMMU is available. If you've
908 * defined your own ioremap_*() variant you must then declare your own
909 * ioremap_*() variant as defined to itself to avoid the default NULL return.
910 */
911
912#ifdef CONFIG_MMU
913
914#ifndef ioremap_uc
915#define ioremap_uc ioremap_uc
916static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
917{
918 return NULL;
919}
920#endif
921
922#else /* !CONFIG_MMU */
923
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000924/*
925 * Change "struct page" to physical address.
Jonas Bonnf1ecc692011-07-02 17:17:35 +0200926 *
927 * This implementation is for the no-MMU case only... if you have an MMU
928 * you'll need to provide your own definitions.
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000929 */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000930
Thierry Reding9216efa2014-10-01 15:20:33 +0200931#ifndef ioremap
932#define ioremap ioremap
933static inline void __iomem *ioremap(phys_addr_t offset, size_t size)
934{
935 return (void __iomem *)(unsigned long)offset;
936}
937#endif
938
939#ifndef __ioremap
940#define __ioremap __ioremap
941static inline void __iomem *__ioremap(phys_addr_t offset, size_t size,
942 unsigned long flags)
943{
944 return ioremap(offset, size);
945}
946#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000947
948#ifndef ioremap_nocache
Thierry Reding9216efa2014-10-01 15:20:33 +0200949#define ioremap_nocache ioremap_nocache
950static inline void __iomem *ioremap_nocache(phys_addr_t offset, size_t size)
951{
952 return ioremap(offset, size);
953}
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000954#endif
955
Luis R. Rodrigueze4b6be332015-05-11 10:15:53 +0200956#ifndef ioremap_uc
957#define ioremap_uc ioremap_uc
958static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
959{
960 return ioremap_nocache(offset, size);
961}
962#endif
963
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000964#ifndef ioremap_wc
Thierry Reding9216efa2014-10-01 15:20:33 +0200965#define ioremap_wc ioremap_wc
966static inline void __iomem *ioremap_wc(phys_addr_t offset, size_t size)
967{
968 return ioremap_nocache(offset, size);
969}
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000970#endif
971
Toshi Kanid8382702015-06-04 18:55:15 +0200972#ifndef ioremap_wt
973#define ioremap_wt ioremap_wt
974static inline void __iomem *ioremap_wt(phys_addr_t offset, size_t size)
975{
976 return ioremap_nocache(offset, size);
977}
978#endif
979
Thierry Reding9216efa2014-10-01 15:20:33 +0200980#ifndef iounmap
981#define iounmap iounmap
Toshi Kanid8382702015-06-04 18:55:15 +0200982
Mark Saltere66d3c42011-10-04 09:25:56 -0400983static inline void iounmap(void __iomem *addr)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000984{
985}
Thierry Reding9216efa2014-10-01 15:20:33 +0200986#endif
Jonas Bonnf1ecc692011-07-02 17:17:35 +0200987#endif /* CONFIG_MMU */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000988
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700989#ifdef CONFIG_HAS_IOPORT_MAP
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000990#ifndef CONFIG_GENERIC_IOMAP
Thierry Reding9216efa2014-10-01 15:20:33 +0200991#ifndef ioport_map
992#define ioport_map ioport_map
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000993static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
994{
Liviu Dudau112eeaa2014-09-29 15:29:20 +0100995 return PCI_IOBASE + (port & IO_SPACE_LIMIT);
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000996}
Thierry Reding9216efa2014-10-01 15:20:33 +0200997#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +0000998
Thierry Reding9216efa2014-10-01 15:20:33 +0200999#ifndef ioport_unmap
1000#define ioport_unmap ioport_unmap
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001001static inline void ioport_unmap(void __iomem *p)
1002{
1003}
Thierry Reding9216efa2014-10-01 15:20:33 +02001004#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001005#else /* CONFIG_GENERIC_IOMAP */
1006extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
1007extern void ioport_unmap(void __iomem *p);
1008#endif /* CONFIG_GENERIC_IOMAP */
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -07001009#endif /* CONFIG_HAS_IOPORT_MAP */
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001010
Andy Shevchenkoeabc2a72017-06-30 20:09:33 +03001011/*
1012 * Convert a virtual cached pointer to an uncached pointer
1013 */
Michael Holzheu576ebd72013-05-21 16:08:22 +02001014#ifndef xlate_dev_kmem_ptr
Thierry Reding9216efa2014-10-01 15:20:33 +02001015#define xlate_dev_kmem_ptr xlate_dev_kmem_ptr
1016static inline void *xlate_dev_kmem_ptr(void *addr)
1017{
1018 return addr;
1019}
Michael Holzheu576ebd72013-05-21 16:08:22 +02001020#endif
Thierry Reding9216efa2014-10-01 15:20:33 +02001021
Michael Holzheu576ebd72013-05-21 16:08:22 +02001022#ifndef xlate_dev_mem_ptr
Thierry Reding9216efa2014-10-01 15:20:33 +02001023#define xlate_dev_mem_ptr xlate_dev_mem_ptr
1024static inline void *xlate_dev_mem_ptr(phys_addr_t addr)
1025{
1026 return __va(addr);
1027}
1028#endif
1029
1030#ifndef unxlate_dev_mem_ptr
1031#define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
1032static inline void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
1033{
1034}
Michael Holzheu576ebd72013-05-21 16:08:22 +02001035#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001036
James Hoganc93d0312012-11-23 16:13:05 +00001037#ifdef CONFIG_VIRT_TO_BUS
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001038#ifndef virt_to_bus
Thierry Reding9216efa2014-10-01 15:20:33 +02001039static inline unsigned long virt_to_bus(void *address)
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001040{
Thierry Reding9216efa2014-10-01 15:20:33 +02001041 return (unsigned long)address;
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001042}
1043
1044static inline void *bus_to_virt(unsigned long address)
1045{
Thierry Reding9216efa2014-10-01 15:20:33 +02001046 return (void *)address;
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001047}
1048#endif
James Hoganc93d0312012-11-23 16:13:05 +00001049#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001050
Jan Glaubercd248342012-11-29 12:50:30 +01001051#ifndef memset_io
Thierry Reding9216efa2014-10-01 15:20:33 +02001052#define memset_io memset_io
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001053/**
1054 * memset_io Set a range of I/O memory to a constant value
1055 * @addr: The beginning of the I/O-memory range to set
1056 * @val: The value to set the memory to
1057 * @count: The number of bytes to set
1058 *
1059 * Set a range of I/O memory to a given value.
1060 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001061static inline void memset_io(volatile void __iomem *addr, int value,
1062 size_t size)
1063{
1064 memset(__io_virt(addr), value, size);
1065}
Jan Glaubercd248342012-11-29 12:50:30 +01001066#endif
1067
1068#ifndef memcpy_fromio
Thierry Reding9216efa2014-10-01 15:20:33 +02001069#define memcpy_fromio memcpy_fromio
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001070/**
1071 * memcpy_fromio Copy a block of data from I/O memory
1072 * @dst: The (RAM) destination for the copy
1073 * @src: The (I/O memory) source for the data
1074 * @count: The number of bytes to copy
1075 *
1076 * Copy a block of data from I/O memory.
1077 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001078static inline void memcpy_fromio(void *buffer,
1079 const volatile void __iomem *addr,
1080 size_t size)
1081{
1082 memcpy(buffer, __io_virt(addr), size);
1083}
Jan Glaubercd248342012-11-29 12:50:30 +01001084#endif
Thierry Reding9216efa2014-10-01 15:20:33 +02001085
Jan Glaubercd248342012-11-29 12:50:30 +01001086#ifndef memcpy_toio
Thierry Reding9216efa2014-10-01 15:20:33 +02001087#define memcpy_toio memcpy_toio
Andy Shevchenkoc2327da2017-06-30 20:09:32 +03001088/**
1089 * memcpy_toio Copy a block of data into I/O memory
1090 * @dst: The (I/O memory) destination for the copy
1091 * @src: The (RAM) source for the data
1092 * @count: The number of bytes to copy
1093 *
1094 * Copy a block of data to I/O memory.
1095 */
Thierry Reding9216efa2014-10-01 15:20:33 +02001096static inline void memcpy_toio(volatile void __iomem *addr, const void *buffer,
1097 size_t size)
1098{
1099 memcpy(__io_virt(addr), buffer, size);
1100}
Jan Glaubercd248342012-11-29 12:50:30 +01001101#endif
Arnd Bergmann3f7e2122009-05-13 22:56:35 +00001102
1103#endif /* __KERNEL__ */
1104
1105#endif /* __ASM_GENERIC_IO_H */