io.h 30 KB

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