io.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Generic I/O port emulation.
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #ifndef __ASM_GENERIC_IO_H
  8. #define __ASM_GENERIC_IO_H
  9. #include <asm/page.h> /* I/O is all done through memory accesses */
  10. #include <linux/string.h> /* for memset() and memcpy() */
  11. #include <linux/sizes.h>
  12. #include <linux/types.h>
  13. #include <linux/instruction_pointer.h>
  14. #ifdef CONFIG_GENERIC_IOMAP
  15. #include <asm-generic/iomap.h>
  16. #endif
  17. #include <asm/mmiowb.h>
  18. #include <asm-generic/pci_iomap.h>
  19. #ifndef __io_br
  20. #define __io_br() barrier()
  21. #endif
  22. /* prevent prefetching of coherent DMA data ahead of a dma-complete */
  23. #ifndef __io_ar
  24. #ifdef rmb
  25. #define __io_ar(v) rmb()
  26. #else
  27. #define __io_ar(v) barrier()
  28. #endif
  29. #endif
  30. /* flush writes to coherent DMA data before possibly triggering a DMA read */
  31. #ifndef __io_bw
  32. #ifdef wmb
  33. #define __io_bw() wmb()
  34. #else
  35. #define __io_bw() barrier()
  36. #endif
  37. #endif
  38. /* serialize device access against a spin_unlock, usually handled there. */
  39. #ifndef __io_aw
  40. #define __io_aw() mmiowb_set_pending()
  41. #endif
  42. #ifndef __io_pbw
  43. #define __io_pbw() __io_bw()
  44. #endif
  45. #ifndef __io_paw
  46. #define __io_paw() __io_aw()
  47. #endif
  48. #ifndef __io_pbr
  49. #define __io_pbr() __io_br()
  50. #endif
  51. #ifndef __io_par
  52. #define __io_par(v) __io_ar(v)
  53. #endif
  54. /*
  55. * "__DISABLE_TRACE_MMIO__" flag can be used to disable MMIO tracing for
  56. * specific kernel drivers in case of excessive/unwanted logging.
  57. *
  58. * Usage: Add a #define flag at the beginning of the driver file.
  59. * Ex: #define __DISABLE_TRACE_MMIO__
  60. * #include <...>
  61. * ...
  62. */
  63. #if IS_ENABLED(CONFIG_TRACE_MMIO_ACCESS) && !(defined(__DISABLE_TRACE_MMIO__))
  64. #include <linux/tracepoint-defs.h>
  65. DECLARE_TRACEPOINT(rwmmio_write);
  66. DECLARE_TRACEPOINT(rwmmio_post_write);
  67. DECLARE_TRACEPOINT(rwmmio_read);
  68. DECLARE_TRACEPOINT(rwmmio_post_read);
  69. void log_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
  70. unsigned long caller_addr, unsigned long caller_addr0);
  71. void log_post_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
  72. unsigned long caller_addr, unsigned long caller_addr0);
  73. void log_read_mmio(u8 width, const volatile void __iomem *addr,
  74. unsigned long caller_addr, unsigned long caller_addr0);
  75. void log_post_read_mmio(u64 val, u8 width, const volatile void __iomem *addr,
  76. unsigned long caller_addr, unsigned long caller_addr0);
  77. #else
  78. static inline void log_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
  79. unsigned long caller_addr, unsigned long caller_addr0) {}
  80. static inline void log_post_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
  81. unsigned long caller_addr, unsigned long caller_addr0) {}
  82. static inline void log_read_mmio(u8 width, const volatile void __iomem *addr,
  83. unsigned long caller_addr, unsigned long caller_addr0) {}
  84. static inline void log_post_read_mmio(u64 val, u8 width, const volatile void __iomem *addr,
  85. unsigned long caller_addr, unsigned long caller_addr0) {}
  86. #endif /* CONFIG_TRACE_MMIO_ACCESS */
  87. /*
  88. * __raw_{read,write}{b,w,l,q}() access memory in native endianness.
  89. *
  90. * On some architectures memory mapped IO needs to be accessed differently.
  91. * On the simple architectures, we just read/write the memory location
  92. * directly.
  93. */
  94. #ifndef __raw_readb
  95. #define __raw_readb __raw_readb
  96. static inline u8 __raw_readb(const volatile void __iomem *addr)
  97. {
  98. return *(const volatile u8 __force *)addr;
  99. }
  100. #endif
  101. #ifndef __raw_readw
  102. #define __raw_readw __raw_readw
  103. static inline u16 __raw_readw(const volatile void __iomem *addr)
  104. {
  105. return *(const volatile u16 __force *)addr;
  106. }
  107. #endif
  108. #ifndef __raw_readl
  109. #define __raw_readl __raw_readl
  110. static inline u32 __raw_readl(const volatile void __iomem *addr)
  111. {
  112. return *(const volatile u32 __force *)addr;
  113. }
  114. #endif
  115. #ifdef CONFIG_64BIT
  116. #ifndef __raw_readq
  117. #define __raw_readq __raw_readq
  118. static inline u64 __raw_readq(const volatile void __iomem *addr)
  119. {
  120. return *(const volatile u64 __force *)addr;
  121. }
  122. #endif
  123. #endif /* CONFIG_64BIT */
  124. #ifndef __raw_writeb
  125. #define __raw_writeb __raw_writeb
  126. static inline void __raw_writeb(u8 value, volatile void __iomem *addr)
  127. {
  128. *(volatile u8 __force *)addr = value;
  129. }
  130. #endif
  131. #ifndef __raw_writew
  132. #define __raw_writew __raw_writew
  133. static inline void __raw_writew(u16 value, volatile void __iomem *addr)
  134. {
  135. *(volatile u16 __force *)addr = value;
  136. }
  137. #endif
  138. #ifndef __raw_writel
  139. #define __raw_writel __raw_writel
  140. static inline void __raw_writel(u32 value, volatile void __iomem *addr)
  141. {
  142. *(volatile u32 __force *)addr = value;
  143. }
  144. #endif
  145. #ifdef CONFIG_64BIT
  146. #ifndef __raw_writeq
  147. #define __raw_writeq __raw_writeq
  148. static inline void __raw_writeq(u64 value, volatile void __iomem *addr)
  149. {
  150. *(volatile u64 __force *)addr = value;
  151. }
  152. #endif
  153. #endif /* CONFIG_64BIT */
  154. /*
  155. * {read,write}{b,w,l,q}() access little endian memory and return result in
  156. * native endianness.
  157. */
  158. #ifndef readb
  159. #define readb readb
  160. static inline u8 readb(const volatile void __iomem *addr)
  161. {
  162. u8 val;
  163. log_read_mmio(8, addr, _THIS_IP_, _RET_IP_);
  164. __io_br();
  165. val = __raw_readb(addr);
  166. __io_ar(val);
  167. log_post_read_mmio(val, 8, addr, _THIS_IP_, _RET_IP_);
  168. return val;
  169. }
  170. #endif
  171. #ifndef readw
  172. #define readw readw
  173. static inline u16 readw(const volatile void __iomem *addr)
  174. {
  175. u16 val;
  176. log_read_mmio(16, addr, _THIS_IP_, _RET_IP_);
  177. __io_br();
  178. val = __le16_to_cpu((__le16 __force)__raw_readw(addr));
  179. __io_ar(val);
  180. log_post_read_mmio(val, 16, addr, _THIS_IP_, _RET_IP_);
  181. return val;
  182. }
  183. #endif
  184. #ifndef readl
  185. #define readl readl
  186. static inline u32 readl(const volatile void __iomem *addr)
  187. {
  188. u32 val;
  189. log_read_mmio(32, addr, _THIS_IP_, _RET_IP_);
  190. __io_br();
  191. val = __le32_to_cpu((__le32 __force)__raw_readl(addr));
  192. __io_ar(val);
  193. log_post_read_mmio(val, 32, addr, _THIS_IP_, _RET_IP_);
  194. return val;
  195. }
  196. #endif
  197. #ifdef CONFIG_64BIT
  198. #ifndef readq
  199. #define readq readq
  200. static inline u64 readq(const volatile void __iomem *addr)
  201. {
  202. u64 val;
  203. log_read_mmio(64, addr, _THIS_IP_, _RET_IP_);
  204. __io_br();
  205. val = __le64_to_cpu((__le64 __force)__raw_readq(addr));
  206. __io_ar(val);
  207. log_post_read_mmio(val, 64, addr, _THIS_IP_, _RET_IP_);
  208. return val;
  209. }
  210. #endif
  211. #endif /* CONFIG_64BIT */
  212. #ifndef writeb
  213. #define writeb writeb
  214. static inline void writeb(u8 value, volatile void __iomem *addr)
  215. {
  216. log_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  217. __io_bw();
  218. __raw_writeb(value, addr);
  219. __io_aw();
  220. log_post_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  221. }
  222. #endif
  223. #ifndef writew
  224. #define writew writew
  225. static inline void writew(u16 value, volatile void __iomem *addr)
  226. {
  227. log_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  228. __io_bw();
  229. __raw_writew((u16 __force)cpu_to_le16(value), addr);
  230. __io_aw();
  231. log_post_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  232. }
  233. #endif
  234. #ifndef writel
  235. #define writel writel
  236. static inline void writel(u32 value, volatile void __iomem *addr)
  237. {
  238. log_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  239. __io_bw();
  240. __raw_writel((u32 __force)__cpu_to_le32(value), addr);
  241. __io_aw();
  242. log_post_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  243. }
  244. #endif
  245. #ifdef CONFIG_64BIT
  246. #ifndef writeq
  247. #define writeq writeq
  248. static inline void writeq(u64 value, volatile void __iomem *addr)
  249. {
  250. log_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  251. __io_bw();
  252. __raw_writeq((u64 __force)__cpu_to_le64(value), addr);
  253. __io_aw();
  254. log_post_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  255. }
  256. #endif
  257. #endif /* CONFIG_64BIT */
  258. /*
  259. * {read,write}{b,w,l,q}_relaxed() are like the regular version, but
  260. * are not guaranteed to provide ordering against spinlocks or memory
  261. * accesses.
  262. */
  263. #ifndef readb_relaxed
  264. #define readb_relaxed readb_relaxed
  265. static inline u8 readb_relaxed(const volatile void __iomem *addr)
  266. {
  267. u8 val;
  268. log_read_mmio(8, addr, _THIS_IP_, _RET_IP_);
  269. val = __raw_readb(addr);
  270. log_post_read_mmio(val, 8, addr, _THIS_IP_, _RET_IP_);
  271. return val;
  272. }
  273. #endif
  274. #ifndef readw_relaxed
  275. #define readw_relaxed readw_relaxed
  276. static inline u16 readw_relaxed(const volatile void __iomem *addr)
  277. {
  278. u16 val;
  279. log_read_mmio(16, addr, _THIS_IP_, _RET_IP_);
  280. val = __le16_to_cpu((__le16 __force)__raw_readw(addr));
  281. log_post_read_mmio(val, 16, addr, _THIS_IP_, _RET_IP_);
  282. return val;
  283. }
  284. #endif
  285. #ifndef readl_relaxed
  286. #define readl_relaxed readl_relaxed
  287. static inline u32 readl_relaxed(const volatile void __iomem *addr)
  288. {
  289. u32 val;
  290. log_read_mmio(32, addr, _THIS_IP_, _RET_IP_);
  291. val = __le32_to_cpu((__le32 __force)__raw_readl(addr));
  292. log_post_read_mmio(val, 32, addr, _THIS_IP_, _RET_IP_);
  293. return val;
  294. }
  295. #endif
  296. #if defined(readq) && !defined(readq_relaxed)
  297. #define readq_relaxed readq_relaxed
  298. static inline u64 readq_relaxed(const volatile void __iomem *addr)
  299. {
  300. u64 val;
  301. log_read_mmio(64, addr, _THIS_IP_, _RET_IP_);
  302. val = __le64_to_cpu((__le64 __force)__raw_readq(addr));
  303. log_post_read_mmio(val, 64, addr, _THIS_IP_, _RET_IP_);
  304. return val;
  305. }
  306. #endif
  307. #ifndef writeb_relaxed
  308. #define writeb_relaxed writeb_relaxed
  309. static inline void writeb_relaxed(u8 value, volatile void __iomem *addr)
  310. {
  311. log_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  312. __raw_writeb(value, addr);
  313. log_post_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  314. }
  315. #endif
  316. #ifndef writew_relaxed
  317. #define writew_relaxed writew_relaxed
  318. static inline void writew_relaxed(u16 value, volatile void __iomem *addr)
  319. {
  320. log_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  321. __raw_writew((u16 __force)cpu_to_le16(value), addr);
  322. log_post_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  323. }
  324. #endif
  325. #ifndef writel_relaxed
  326. #define writel_relaxed writel_relaxed
  327. static inline void writel_relaxed(u32 value, volatile void __iomem *addr)
  328. {
  329. log_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  330. __raw_writel((u32 __force)__cpu_to_le32(value), addr);
  331. log_post_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  332. }
  333. #endif
  334. #if defined(writeq) && !defined(writeq_relaxed)
  335. #define writeq_relaxed writeq_relaxed
  336. static inline void writeq_relaxed(u64 value, volatile void __iomem *addr)
  337. {
  338. log_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  339. __raw_writeq((u64 __force)__cpu_to_le64(value), addr);
  340. log_post_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  341. }
  342. #endif
  343. /*
  344. * {read,write}s{b,w,l,q}() repeatedly access the same memory address in
  345. * native endianness in 8-, 16-, 32- or 64-bit chunks (@count times).
  346. */
  347. #ifndef readsb
  348. #define readsb readsb
  349. static inline void readsb(const volatile void __iomem *addr, void *buffer,
  350. unsigned int count)
  351. {
  352. if (count) {
  353. u8 *buf = buffer;
  354. do {
  355. u8 x = __raw_readb(addr);
  356. *buf++ = x;
  357. } while (--count);
  358. }
  359. }
  360. #endif
  361. #ifndef readsw
  362. #define readsw readsw
  363. static inline void readsw(const volatile void __iomem *addr, void *buffer,
  364. unsigned int count)
  365. {
  366. if (count) {
  367. u16 *buf = buffer;
  368. do {
  369. u16 x = __raw_readw(addr);
  370. *buf++ = x;
  371. } while (--count);
  372. }
  373. }
  374. #endif
  375. #ifndef readsl
  376. #define readsl readsl
  377. static inline void readsl(const volatile void __iomem *addr, void *buffer,
  378. unsigned int count)
  379. {
  380. if (count) {
  381. u32 *buf = buffer;
  382. do {
  383. u32 x = __raw_readl(addr);
  384. *buf++ = x;
  385. } while (--count);
  386. }
  387. }
  388. #endif
  389. #ifdef CONFIG_64BIT
  390. #ifndef readsq
  391. #define readsq readsq
  392. static inline void readsq(const volatile void __iomem *addr, void *buffer,
  393. unsigned int count)
  394. {
  395. if (count) {
  396. u64 *buf = buffer;
  397. do {
  398. u64 x = __raw_readq(addr);
  399. *buf++ = x;
  400. } while (--count);
  401. }
  402. }
  403. #endif
  404. #endif /* CONFIG_64BIT */
  405. #ifndef writesb
  406. #define writesb writesb
  407. static inline void writesb(volatile void __iomem *addr, const void *buffer,
  408. unsigned int count)
  409. {
  410. if (count) {
  411. const u8 *buf = buffer;
  412. do {
  413. __raw_writeb(*buf++, addr);
  414. } while (--count);
  415. }
  416. }
  417. #endif
  418. #ifndef writesw
  419. #define writesw writesw
  420. static inline void writesw(volatile void __iomem *addr, const void *buffer,
  421. unsigned int count)
  422. {
  423. if (count) {
  424. const u16 *buf = buffer;
  425. do {
  426. __raw_writew(*buf++, addr);
  427. } while (--count);
  428. }
  429. }
  430. #endif
  431. #ifndef writesl
  432. #define writesl writesl
  433. static inline void writesl(volatile void __iomem *addr, const void *buffer,
  434. unsigned int count)
  435. {
  436. if (count) {
  437. const u32 *buf = buffer;
  438. do {
  439. __raw_writel(*buf++, addr);
  440. } while (--count);
  441. }
  442. }
  443. #endif
  444. #ifdef CONFIG_64BIT
  445. #ifndef writesq
  446. #define writesq writesq
  447. static inline void writesq(volatile void __iomem *addr, const void *buffer,
  448. unsigned int count)
  449. {
  450. if (count) {
  451. const u64 *buf = buffer;
  452. do {
  453. __raw_writeq(*buf++, addr);
  454. } while (--count);
  455. }
  456. }
  457. #endif
  458. #endif /* CONFIG_64BIT */
  459. #ifndef PCI_IOBASE
  460. #define PCI_IOBASE ((void __iomem *)0)
  461. #endif
  462. #ifndef IO_SPACE_LIMIT
  463. #define IO_SPACE_LIMIT 0xffff
  464. #endif
  465. /*
  466. * {in,out}{b,w,l}() access little endian I/O. {in,out}{b,w,l}_p() can be
  467. * implemented on hardware that needs an additional delay for I/O accesses to
  468. * take effect.
  469. */
  470. #if !defined(inb) && !defined(_inb)
  471. #define _inb _inb
  472. static inline u8 _inb(unsigned long addr)
  473. {
  474. u8 val;
  475. __io_pbr();
  476. val = __raw_readb(PCI_IOBASE + addr);
  477. __io_par(val);
  478. return val;
  479. }
  480. #endif
  481. #if !defined(inw) && !defined(_inw)
  482. #define _inw _inw
  483. static inline u16 _inw(unsigned long addr)
  484. {
  485. u16 val;
  486. __io_pbr();
  487. val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
  488. __io_par(val);
  489. return val;
  490. }
  491. #endif
  492. #if !defined(inl) && !defined(_inl)
  493. #define _inl _inl
  494. static inline u32 _inl(unsigned long addr)
  495. {
  496. u32 val;
  497. __io_pbr();
  498. val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
  499. __io_par(val);
  500. return val;
  501. }
  502. #endif
  503. #if !defined(outb) && !defined(_outb)
  504. #define _outb _outb
  505. static inline void _outb(u8 value, unsigned long addr)
  506. {
  507. __io_pbw();
  508. __raw_writeb(value, PCI_IOBASE + addr);
  509. __io_paw();
  510. }
  511. #endif
  512. #if !defined(outw) && !defined(_outw)
  513. #define _outw _outw
  514. static inline void _outw(u16 value, unsigned long addr)
  515. {
  516. __io_pbw();
  517. __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
  518. __io_paw();
  519. }
  520. #endif
  521. #if !defined(outl) && !defined(_outl)
  522. #define _outl _outl
  523. static inline void _outl(u32 value, unsigned long addr)
  524. {
  525. __io_pbw();
  526. __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
  527. __io_paw();
  528. }
  529. #endif
  530. #include <linux/logic_pio.h>
  531. #ifndef inb
  532. #define inb _inb
  533. #endif
  534. #ifndef inw
  535. #define inw _inw
  536. #endif
  537. #ifndef inl
  538. #define inl _inl
  539. #endif
  540. #ifndef outb
  541. #define outb _outb
  542. #endif
  543. #ifndef outw
  544. #define outw _outw
  545. #endif
  546. #ifndef outl
  547. #define outl _outl
  548. #endif
  549. #ifndef inb_p
  550. #define inb_p inb_p
  551. static inline u8 inb_p(unsigned long addr)
  552. {
  553. return inb(addr);
  554. }
  555. #endif
  556. #ifndef inw_p
  557. #define inw_p inw_p
  558. static inline u16 inw_p(unsigned long addr)
  559. {
  560. return inw(addr);
  561. }
  562. #endif
  563. #ifndef inl_p
  564. #define inl_p inl_p
  565. static inline u32 inl_p(unsigned long addr)
  566. {
  567. return inl(addr);
  568. }
  569. #endif
  570. #ifndef outb_p
  571. #define outb_p outb_p
  572. static inline void outb_p(u8 value, unsigned long addr)
  573. {
  574. outb(value, addr);
  575. }
  576. #endif
  577. #ifndef outw_p
  578. #define outw_p outw_p
  579. static inline void outw_p(u16 value, unsigned long addr)
  580. {
  581. outw(value, addr);
  582. }
  583. #endif
  584. #ifndef outl_p
  585. #define outl_p outl_p
  586. static inline void outl_p(u32 value, unsigned long addr)
  587. {
  588. outl(value, addr);
  589. }
  590. #endif
  591. /*
  592. * {in,out}s{b,w,l}{,_p}() are variants of the above that repeatedly access a
  593. * single I/O port multiple times.
  594. */
  595. #ifndef insb
  596. #define insb insb
  597. static inline void insb(unsigned long addr, void *buffer, unsigned int count)
  598. {
  599. readsb(PCI_IOBASE + addr, buffer, count);
  600. }
  601. #endif
  602. #ifndef insw
  603. #define insw insw
  604. static inline void insw(unsigned long addr, void *buffer, unsigned int count)
  605. {
  606. readsw(PCI_IOBASE + addr, buffer, count);
  607. }
  608. #endif
  609. #ifndef insl
  610. #define insl insl
  611. static inline void insl(unsigned long addr, void *buffer, unsigned int count)
  612. {
  613. readsl(PCI_IOBASE + addr, buffer, count);
  614. }
  615. #endif
  616. #ifndef outsb
  617. #define outsb outsb
  618. static inline void outsb(unsigned long addr, const void *buffer,
  619. unsigned int count)
  620. {
  621. writesb(PCI_IOBASE + addr, buffer, count);
  622. }
  623. #endif
  624. #ifndef outsw
  625. #define outsw outsw
  626. static inline void outsw(unsigned long addr, const void *buffer,
  627. unsigned int count)
  628. {
  629. writesw(PCI_IOBASE + addr, buffer, count);
  630. }
  631. #endif
  632. #ifndef outsl
  633. #define outsl outsl
  634. static inline void outsl(unsigned long addr, const void *buffer,
  635. unsigned int count)
  636. {
  637. writesl(PCI_IOBASE + addr, buffer, count);
  638. }
  639. #endif
  640. #ifndef insb_p
  641. #define insb_p insb_p
  642. static inline void insb_p(unsigned long addr, void *buffer, unsigned int count)
  643. {
  644. insb(addr, buffer, count);
  645. }
  646. #endif
  647. #ifndef insw_p
  648. #define insw_p insw_p
  649. static inline void insw_p(unsigned long addr, void *buffer, unsigned int count)
  650. {
  651. insw(addr, buffer, count);
  652. }
  653. #endif
  654. #ifndef insl_p
  655. #define insl_p insl_p
  656. static inline void insl_p(unsigned long addr, void *buffer, unsigned int count)
  657. {
  658. insl(addr, buffer, count);
  659. }
  660. #endif
  661. #ifndef outsb_p
  662. #define outsb_p outsb_p
  663. static inline void outsb_p(unsigned long addr, const void *buffer,
  664. unsigned int count)
  665. {
  666. outsb(addr, buffer, count);
  667. }
  668. #endif
  669. #ifndef outsw_p
  670. #define outsw_p outsw_p
  671. static inline void outsw_p(unsigned long addr, const void *buffer,
  672. unsigned int count)
  673. {
  674. outsw(addr, buffer, count);
  675. }
  676. #endif
  677. #ifndef outsl_p
  678. #define outsl_p outsl_p
  679. static inline void outsl_p(unsigned long addr, const void *buffer,
  680. unsigned int count)
  681. {
  682. outsl(addr, buffer, count);
  683. }
  684. #endif
  685. #ifndef CONFIG_GENERIC_IOMAP
  686. #ifndef ioread8
  687. #define ioread8 ioread8
  688. static inline u8 ioread8(const volatile void __iomem *addr)
  689. {
  690. return readb(addr);
  691. }
  692. #endif
  693. #ifndef ioread16
  694. #define ioread16 ioread16
  695. static inline u16 ioread16(const volatile void __iomem *addr)
  696. {
  697. return readw(addr);
  698. }
  699. #endif
  700. #ifndef ioread32
  701. #define ioread32 ioread32
  702. static inline u32 ioread32(const volatile void __iomem *addr)
  703. {
  704. return readl(addr);
  705. }
  706. #endif
  707. #ifdef CONFIG_64BIT
  708. #ifndef ioread64
  709. #define ioread64 ioread64
  710. static inline u64 ioread64(const volatile void __iomem *addr)
  711. {
  712. return readq(addr);
  713. }
  714. #endif
  715. #endif /* CONFIG_64BIT */
  716. #ifndef iowrite8
  717. #define iowrite8 iowrite8
  718. static inline void iowrite8(u8 value, volatile void __iomem *addr)
  719. {
  720. writeb(value, addr);
  721. }
  722. #endif
  723. #ifndef iowrite16
  724. #define iowrite16 iowrite16
  725. static inline void iowrite16(u16 value, volatile void __iomem *addr)
  726. {
  727. writew(value, addr);
  728. }
  729. #endif
  730. #ifndef iowrite32
  731. #define iowrite32 iowrite32
  732. static inline void iowrite32(u32 value, volatile void __iomem *addr)
  733. {
  734. writel(value, addr);
  735. }
  736. #endif
  737. #ifdef CONFIG_64BIT
  738. #ifndef iowrite64
  739. #define iowrite64 iowrite64
  740. static inline void iowrite64(u64 value, volatile void __iomem *addr)
  741. {
  742. writeq(value, addr);
  743. }
  744. #endif
  745. #endif /* CONFIG_64BIT */
  746. #ifndef ioread16be
  747. #define ioread16be ioread16be
  748. static inline u16 ioread16be(const volatile void __iomem *addr)
  749. {
  750. return swab16(readw(addr));
  751. }
  752. #endif
  753. #ifndef ioread32be
  754. #define ioread32be ioread32be
  755. static inline u32 ioread32be(const volatile void __iomem *addr)
  756. {
  757. return swab32(readl(addr));
  758. }
  759. #endif
  760. #ifdef CONFIG_64BIT
  761. #ifndef ioread64be
  762. #define ioread64be ioread64be
  763. static inline u64 ioread64be(const volatile void __iomem *addr)
  764. {
  765. return swab64(readq(addr));
  766. }
  767. #endif
  768. #endif /* CONFIG_64BIT */
  769. #ifndef iowrite16be
  770. #define iowrite16be iowrite16be
  771. static inline void iowrite16be(u16 value, void volatile __iomem *addr)
  772. {
  773. writew(swab16(value), addr);
  774. }
  775. #endif
  776. #ifndef iowrite32be
  777. #define iowrite32be iowrite32be
  778. static inline void iowrite32be(u32 value, volatile void __iomem *addr)
  779. {
  780. writel(swab32(value), addr);
  781. }
  782. #endif
  783. #ifdef CONFIG_64BIT
  784. #ifndef iowrite64be
  785. #define iowrite64be iowrite64be
  786. static inline void iowrite64be(u64 value, volatile void __iomem *addr)
  787. {
  788. writeq(swab64(value), addr);
  789. }
  790. #endif
  791. #endif /* CONFIG_64BIT */
  792. #ifndef ioread8_rep
  793. #define ioread8_rep ioread8_rep
  794. static inline void ioread8_rep(const volatile void __iomem *addr, void *buffer,
  795. unsigned int count)
  796. {
  797. readsb(addr, buffer, count);
  798. }
  799. #endif
  800. #ifndef ioread16_rep
  801. #define ioread16_rep ioread16_rep
  802. static inline void ioread16_rep(const volatile void __iomem *addr,
  803. void *buffer, unsigned int count)
  804. {
  805. readsw(addr, buffer, count);
  806. }
  807. #endif
  808. #ifndef ioread32_rep
  809. #define ioread32_rep ioread32_rep
  810. static inline void ioread32_rep(const volatile void __iomem *addr,
  811. void *buffer, unsigned int count)
  812. {
  813. readsl(addr, buffer, count);
  814. }
  815. #endif
  816. #ifdef CONFIG_64BIT
  817. #ifndef ioread64_rep
  818. #define ioread64_rep ioread64_rep
  819. static inline void ioread64_rep(const volatile void __iomem *addr,
  820. void *buffer, unsigned int count)
  821. {
  822. readsq(addr, buffer, count);
  823. }
  824. #endif
  825. #endif /* CONFIG_64BIT */
  826. #ifndef iowrite8_rep
  827. #define iowrite8_rep iowrite8_rep
  828. static inline void iowrite8_rep(volatile void __iomem *addr,
  829. const void *buffer,
  830. unsigned int count)
  831. {
  832. writesb(addr, buffer, count);
  833. }
  834. #endif
  835. #ifndef iowrite16_rep
  836. #define iowrite16_rep iowrite16_rep
  837. static inline void iowrite16_rep(volatile void __iomem *addr,
  838. const void *buffer,
  839. unsigned int count)
  840. {
  841. writesw(addr, buffer, count);
  842. }
  843. #endif
  844. #ifndef iowrite32_rep
  845. #define iowrite32_rep iowrite32_rep
  846. static inline void iowrite32_rep(volatile void __iomem *addr,
  847. const void *buffer,
  848. unsigned int count)
  849. {
  850. writesl(addr, buffer, count);
  851. }
  852. #endif
  853. #ifdef CONFIG_64BIT
  854. #ifndef iowrite64_rep
  855. #define iowrite64_rep iowrite64_rep
  856. static inline void iowrite64_rep(volatile void __iomem *addr,
  857. const void *buffer,
  858. unsigned int count)
  859. {
  860. writesq(addr, buffer, count);
  861. }
  862. #endif
  863. #endif /* CONFIG_64BIT */
  864. #endif /* CONFIG_GENERIC_IOMAP */
  865. #ifdef __KERNEL__
  866. #define __io_virt(x) ((void __force *)(x))
  867. /*
  868. * Change virtual addresses to physical addresses and vv.
  869. * These are pretty trivial
  870. */
  871. #ifndef virt_to_phys
  872. #define virt_to_phys virt_to_phys
  873. static inline unsigned long virt_to_phys(volatile void *address)
  874. {
  875. return __pa((unsigned long)address);
  876. }
  877. #endif
  878. #ifndef phys_to_virt
  879. #define phys_to_virt phys_to_virt
  880. static inline void *phys_to_virt(unsigned long address)
  881. {
  882. return __va(address);
  883. }
  884. #endif
  885. /**
  886. * DOC: ioremap() and ioremap_*() variants
  887. *
  888. * Architectures with an MMU are expected to provide ioremap() and iounmap()
  889. * themselves or rely on GENERIC_IOREMAP. For NOMMU architectures we provide
  890. * a default nop-op implementation that expect that the physical address used
  891. * for MMIO are already marked as uncached, and can be used as kernel virtual
  892. * addresses.
  893. *
  894. * ioremap_wc() and ioremap_wt() can provide more relaxed caching attributes
  895. * for specific drivers if the architecture choses to implement them. If they
  896. * are not implemented we fall back to plain ioremap. Conversely, ioremap_np()
  897. * can provide stricter non-posted write semantics if the architecture
  898. * implements them.
  899. */
  900. #ifndef CONFIG_MMU
  901. #ifndef ioremap
  902. #define ioremap ioremap
  903. static inline void __iomem *ioremap(phys_addr_t offset, size_t size)
  904. {
  905. return (void __iomem *)(unsigned long)offset;
  906. }
  907. #endif
  908. #ifndef iounmap
  909. #define iounmap iounmap
  910. static inline void iounmap(volatile void __iomem *addr)
  911. {
  912. }
  913. #endif
  914. #elif defined(CONFIG_GENERIC_IOREMAP)
  915. #include <linux/pgtable.h>
  916. void __iomem *generic_ioremap_prot(phys_addr_t phys_addr, size_t size,
  917. pgprot_t prot);
  918. void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
  919. unsigned long prot);
  920. void iounmap(volatile void __iomem *addr);
  921. void generic_iounmap(volatile void __iomem *addr);
  922. #ifndef ioremap
  923. #define ioremap ioremap
  924. static inline void __iomem *ioremap(phys_addr_t addr, size_t size)
  925. {
  926. /* _PAGE_IOREMAP needs to be supplied by the architecture */
  927. return ioremap_prot(addr, size, _PAGE_IOREMAP);
  928. }
  929. #endif
  930. #endif /* !CONFIG_MMU || CONFIG_GENERIC_IOREMAP */
  931. #ifndef ioremap_wc
  932. #define ioremap_wc ioremap
  933. #endif
  934. #ifndef ioremap_wt
  935. #define ioremap_wt ioremap
  936. #endif
  937. /*
  938. * ioremap_uc is special in that we do require an explicit architecture
  939. * implementation. In general you do not want to use this function in a
  940. * driver and use plain ioremap, which is uncached by default. Similarly
  941. * architectures should not implement it unless they have a very good
  942. * reason.
  943. */
  944. #ifndef ioremap_uc
  945. #define ioremap_uc ioremap_uc
  946. static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
  947. {
  948. return NULL;
  949. }
  950. #endif
  951. /*
  952. * ioremap_np needs an explicit architecture implementation, as it
  953. * requests stronger semantics than regular ioremap(). Portable drivers
  954. * should instead use one of the higher-level abstractions, like
  955. * devm_ioremap_resource(), to choose the correct variant for any given
  956. * device and bus. Portable drivers with a good reason to want non-posted
  957. * write semantics should always provide an ioremap() fallback in case
  958. * ioremap_np() is not available.
  959. */
  960. #ifndef ioremap_np
  961. #define ioremap_np ioremap_np
  962. static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size)
  963. {
  964. return NULL;
  965. }
  966. #endif
  967. #ifdef CONFIG_HAS_IOPORT_MAP
  968. #ifndef CONFIG_GENERIC_IOMAP
  969. #ifndef ioport_map
  970. #define ioport_map ioport_map
  971. static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
  972. {
  973. port &= IO_SPACE_LIMIT;
  974. return (port > MMIO_UPPER_LIMIT) ? NULL : PCI_IOBASE + port;
  975. }
  976. #define ARCH_HAS_GENERIC_IOPORT_MAP
  977. #endif
  978. #ifndef ioport_unmap
  979. #define ioport_unmap ioport_unmap
  980. static inline void ioport_unmap(void __iomem *p)
  981. {
  982. }
  983. #endif
  984. #else /* CONFIG_GENERIC_IOMAP */
  985. extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
  986. extern void ioport_unmap(void __iomem *p);
  987. #endif /* CONFIG_GENERIC_IOMAP */
  988. #endif /* CONFIG_HAS_IOPORT_MAP */
  989. #ifndef CONFIG_GENERIC_IOMAP
  990. #ifndef pci_iounmap
  991. #define ARCH_WANTS_GENERIC_PCI_IOUNMAP
  992. #endif
  993. #endif
  994. #ifndef xlate_dev_mem_ptr
  995. #define xlate_dev_mem_ptr xlate_dev_mem_ptr
  996. static inline void *xlate_dev_mem_ptr(phys_addr_t addr)
  997. {
  998. return __va(addr);
  999. }
  1000. #endif
  1001. #ifndef unxlate_dev_mem_ptr
  1002. #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
  1003. static inline void unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
  1004. {
  1005. }
  1006. #endif
  1007. #ifndef memset_io
  1008. #define memset_io memset_io
  1009. /**
  1010. * memset_io Set a range of I/O memory to a constant value
  1011. * @addr: The beginning of the I/O-memory range to set
  1012. * @val: The value to set the memory to
  1013. * @count: The number of bytes to set
  1014. *
  1015. * Set a range of I/O memory to a given value.
  1016. */
  1017. static inline void memset_io(volatile void __iomem *addr, int value,
  1018. size_t size)
  1019. {
  1020. memset(__io_virt(addr), value, size);
  1021. }
  1022. #endif
  1023. #ifndef memcpy_fromio
  1024. #define memcpy_fromio memcpy_fromio
  1025. /**
  1026. * memcpy_fromio Copy a block of data from I/O memory
  1027. * @dst: The (RAM) destination for the copy
  1028. * @src: The (I/O memory) source for the data
  1029. * @count: The number of bytes to copy
  1030. *
  1031. * Copy a block of data from I/O memory.
  1032. */
  1033. static inline void memcpy_fromio(void *buffer,
  1034. const volatile void __iomem *addr,
  1035. size_t size)
  1036. {
  1037. memcpy(buffer, __io_virt(addr), size);
  1038. }
  1039. #endif
  1040. #ifndef memcpy_toio
  1041. #define memcpy_toio memcpy_toio
  1042. /**
  1043. * memcpy_toio Copy a block of data into I/O memory
  1044. * @dst: The (I/O memory) destination for the copy
  1045. * @src: The (RAM) source for the data
  1046. * @count: The number of bytes to copy
  1047. *
  1048. * Copy a block of data to I/O memory.
  1049. */
  1050. static inline void memcpy_toio(volatile void __iomem *addr, const void *buffer,
  1051. size_t size)
  1052. {
  1053. memcpy(__io_virt(addr), buffer, size);
  1054. }
  1055. #endif
  1056. extern int devmem_is_allowed(unsigned long pfn);
  1057. #endif /* __KERNEL__ */
  1058. #endif /* __ASM_GENERIC_IO_H */