io.h 23 KB

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