io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Alpha IO and memory functions.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/string.h>
  8. #include <linux/module.h>
  9. #include <asm/io.h>
  10. /* Out-of-line versions of the i/o routines that redirect into the
  11. platform-specific version. Note that "platform-specific" may mean
  12. "generic", which bumps through the machine vector. */
  13. unsigned int
  14. ioread8(void __iomem *addr)
  15. {
  16. unsigned int ret;
  17. mb();
  18. ret = IO_CONCAT(__IO_PREFIX,ioread8)(addr);
  19. mb();
  20. return ret;
  21. }
  22. unsigned int ioread16(void __iomem *addr)
  23. {
  24. unsigned int ret;
  25. mb();
  26. ret = IO_CONCAT(__IO_PREFIX,ioread16)(addr);
  27. mb();
  28. return ret;
  29. }
  30. unsigned int ioread32(void __iomem *addr)
  31. {
  32. unsigned int ret;
  33. mb();
  34. ret = IO_CONCAT(__IO_PREFIX,ioread32)(addr);
  35. mb();
  36. return ret;
  37. }
  38. void iowrite8(u8 b, void __iomem *addr)
  39. {
  40. mb();
  41. IO_CONCAT(__IO_PREFIX,iowrite8)(b, addr);
  42. }
  43. void iowrite16(u16 b, void __iomem *addr)
  44. {
  45. mb();
  46. IO_CONCAT(__IO_PREFIX,iowrite16)(b, addr);
  47. }
  48. void iowrite32(u32 b, void __iomem *addr)
  49. {
  50. mb();
  51. IO_CONCAT(__IO_PREFIX,iowrite32)(b, addr);
  52. }
  53. EXPORT_SYMBOL(ioread8);
  54. EXPORT_SYMBOL(ioread16);
  55. EXPORT_SYMBOL(ioread32);
  56. EXPORT_SYMBOL(iowrite8);
  57. EXPORT_SYMBOL(iowrite16);
  58. EXPORT_SYMBOL(iowrite32);
  59. u8 inb(unsigned long port)
  60. {
  61. return ioread8(ioport_map(port, 1));
  62. }
  63. u16 inw(unsigned long port)
  64. {
  65. return ioread16(ioport_map(port, 2));
  66. }
  67. u32 inl(unsigned long port)
  68. {
  69. return ioread32(ioport_map(port, 4));
  70. }
  71. void outb(u8 b, unsigned long port)
  72. {
  73. iowrite8(b, ioport_map(port, 1));
  74. }
  75. void outw(u16 b, unsigned long port)
  76. {
  77. iowrite16(b, ioport_map(port, 2));
  78. }
  79. void outl(u32 b, unsigned long port)
  80. {
  81. iowrite32(b, ioport_map(port, 4));
  82. }
  83. EXPORT_SYMBOL(inb);
  84. EXPORT_SYMBOL(inw);
  85. EXPORT_SYMBOL(inl);
  86. EXPORT_SYMBOL(outb);
  87. EXPORT_SYMBOL(outw);
  88. EXPORT_SYMBOL(outl);
  89. u8 __raw_readb(const volatile void __iomem *addr)
  90. {
  91. return IO_CONCAT(__IO_PREFIX,readb)(addr);
  92. }
  93. u16 __raw_readw(const volatile void __iomem *addr)
  94. {
  95. return IO_CONCAT(__IO_PREFIX,readw)(addr);
  96. }
  97. u32 __raw_readl(const volatile void __iomem *addr)
  98. {
  99. return IO_CONCAT(__IO_PREFIX,readl)(addr);
  100. }
  101. u64 __raw_readq(const volatile void __iomem *addr)
  102. {
  103. return IO_CONCAT(__IO_PREFIX,readq)(addr);
  104. }
  105. void __raw_writeb(u8 b, volatile void __iomem *addr)
  106. {
  107. IO_CONCAT(__IO_PREFIX,writeb)(b, addr);
  108. }
  109. void __raw_writew(u16 b, volatile void __iomem *addr)
  110. {
  111. IO_CONCAT(__IO_PREFIX,writew)(b, addr);
  112. }
  113. void __raw_writel(u32 b, volatile void __iomem *addr)
  114. {
  115. IO_CONCAT(__IO_PREFIX,writel)(b, addr);
  116. }
  117. void __raw_writeq(u64 b, volatile void __iomem *addr)
  118. {
  119. IO_CONCAT(__IO_PREFIX,writeq)(b, addr);
  120. }
  121. EXPORT_SYMBOL(__raw_readb);
  122. EXPORT_SYMBOL(__raw_readw);
  123. EXPORT_SYMBOL(__raw_readl);
  124. EXPORT_SYMBOL(__raw_readq);
  125. EXPORT_SYMBOL(__raw_writeb);
  126. EXPORT_SYMBOL(__raw_writew);
  127. EXPORT_SYMBOL(__raw_writel);
  128. EXPORT_SYMBOL(__raw_writeq);
  129. u8 readb(const volatile void __iomem *addr)
  130. {
  131. u8 ret;
  132. mb();
  133. ret = __raw_readb(addr);
  134. mb();
  135. return ret;
  136. }
  137. u16 readw(const volatile void __iomem *addr)
  138. {
  139. u16 ret;
  140. mb();
  141. ret = __raw_readw(addr);
  142. mb();
  143. return ret;
  144. }
  145. u32 readl(const volatile void __iomem *addr)
  146. {
  147. u32 ret;
  148. mb();
  149. ret = __raw_readl(addr);
  150. mb();
  151. return ret;
  152. }
  153. u64 readq(const volatile void __iomem *addr)
  154. {
  155. u64 ret;
  156. mb();
  157. ret = __raw_readq(addr);
  158. mb();
  159. return ret;
  160. }
  161. void writeb(u8 b, volatile void __iomem *addr)
  162. {
  163. mb();
  164. __raw_writeb(b, addr);
  165. }
  166. void writew(u16 b, volatile void __iomem *addr)
  167. {
  168. mb();
  169. __raw_writew(b, addr);
  170. }
  171. void writel(u32 b, volatile void __iomem *addr)
  172. {
  173. mb();
  174. __raw_writel(b, addr);
  175. }
  176. void writeq(u64 b, volatile void __iomem *addr)
  177. {
  178. mb();
  179. __raw_writeq(b, addr);
  180. }
  181. EXPORT_SYMBOL(readb);
  182. EXPORT_SYMBOL(readw);
  183. EXPORT_SYMBOL(readl);
  184. EXPORT_SYMBOL(readq);
  185. EXPORT_SYMBOL(writeb);
  186. EXPORT_SYMBOL(writew);
  187. EXPORT_SYMBOL(writel);
  188. EXPORT_SYMBOL(writeq);
  189. /*
  190. * The _relaxed functions must be ordered w.r.t. each other, but they don't
  191. * have to be ordered w.r.t. other memory accesses.
  192. */
  193. u8 readb_relaxed(const volatile void __iomem *addr)
  194. {
  195. mb();
  196. return __raw_readb(addr);
  197. }
  198. u16 readw_relaxed(const volatile void __iomem *addr)
  199. {
  200. mb();
  201. return __raw_readw(addr);
  202. }
  203. u32 readl_relaxed(const volatile void __iomem *addr)
  204. {
  205. mb();
  206. return __raw_readl(addr);
  207. }
  208. u64 readq_relaxed(const volatile void __iomem *addr)
  209. {
  210. mb();
  211. return __raw_readq(addr);
  212. }
  213. EXPORT_SYMBOL(readb_relaxed);
  214. EXPORT_SYMBOL(readw_relaxed);
  215. EXPORT_SYMBOL(readl_relaxed);
  216. EXPORT_SYMBOL(readq_relaxed);
  217. /*
  218. * Read COUNT 8-bit bytes from port PORT into memory starting at SRC.
  219. */
  220. void ioread8_rep(void __iomem *port, void *dst, unsigned long count)
  221. {
  222. while ((unsigned long)dst & 0x3) {
  223. if (!count)
  224. return;
  225. count--;
  226. *(unsigned char *)dst = ioread8(port);
  227. dst += 1;
  228. }
  229. while (count >= 4) {
  230. unsigned int w;
  231. count -= 4;
  232. w = ioread8(port);
  233. w |= ioread8(port) << 8;
  234. w |= ioread8(port) << 16;
  235. w |= ioread8(port) << 24;
  236. *(unsigned int *)dst = w;
  237. dst += 4;
  238. }
  239. while (count) {
  240. --count;
  241. *(unsigned char *)dst = ioread8(port);
  242. dst += 1;
  243. }
  244. }
  245. void insb(unsigned long port, void *dst, unsigned long count)
  246. {
  247. ioread8_rep(ioport_map(port, 1), dst, count);
  248. }
  249. EXPORT_SYMBOL(ioread8_rep);
  250. EXPORT_SYMBOL(insb);
  251. /*
  252. * Read COUNT 16-bit words from port PORT into memory starting at
  253. * SRC. SRC must be at least short aligned. This is used by the
  254. * IDE driver to read disk sectors. Performance is important, but
  255. * the interfaces seems to be slow: just using the inlined version
  256. * of the inw() breaks things.
  257. */
  258. void ioread16_rep(void __iomem *port, void *dst, unsigned long count)
  259. {
  260. if (unlikely((unsigned long)dst & 0x3)) {
  261. if (!count)
  262. return;
  263. BUG_ON((unsigned long)dst & 0x1);
  264. count--;
  265. *(unsigned short *)dst = ioread16(port);
  266. dst += 2;
  267. }
  268. while (count >= 2) {
  269. unsigned int w;
  270. count -= 2;
  271. w = ioread16(port);
  272. w |= ioread16(port) << 16;
  273. *(unsigned int *)dst = w;
  274. dst += 4;
  275. }
  276. if (count) {
  277. *(unsigned short*)dst = ioread16(port);
  278. }
  279. }
  280. void insw(unsigned long port, void *dst, unsigned long count)
  281. {
  282. ioread16_rep(ioport_map(port, 2), dst, count);
  283. }
  284. EXPORT_SYMBOL(ioread16_rep);
  285. EXPORT_SYMBOL(insw);
  286. /*
  287. * Read COUNT 32-bit words from port PORT into memory starting at
  288. * SRC. Now works with any alignment in SRC. Performance is important,
  289. * but the interfaces seems to be slow: just using the inlined version
  290. * of the inl() breaks things.
  291. */
  292. void ioread32_rep(void __iomem *port, void *dst, unsigned long count)
  293. {
  294. if (unlikely((unsigned long)dst & 0x3)) {
  295. while (count--) {
  296. struct S { int x __attribute__((packed)); };
  297. ((struct S *)dst)->x = ioread32(port);
  298. dst += 4;
  299. }
  300. } else {
  301. /* Buffer 32-bit aligned. */
  302. while (count--) {
  303. *(unsigned int *)dst = ioread32(port);
  304. dst += 4;
  305. }
  306. }
  307. }
  308. void insl(unsigned long port, void *dst, unsigned long count)
  309. {
  310. ioread32_rep(ioport_map(port, 4), dst, count);
  311. }
  312. EXPORT_SYMBOL(ioread32_rep);
  313. EXPORT_SYMBOL(insl);
  314. /*
  315. * Like insb but in the opposite direction.
  316. * Don't worry as much about doing aligned memory transfers:
  317. * doing byte reads the "slow" way isn't nearly as slow as
  318. * doing byte writes the slow way (no r-m-w cycle).
  319. */
  320. void iowrite8_rep(void __iomem *port, const void *xsrc, unsigned long count)
  321. {
  322. const unsigned char *src = xsrc;
  323. while (count--)
  324. iowrite8(*src++, port);
  325. }
  326. void outsb(unsigned long port, const void *src, unsigned long count)
  327. {
  328. iowrite8_rep(ioport_map(port, 1), src, count);
  329. }
  330. EXPORT_SYMBOL(iowrite8_rep);
  331. EXPORT_SYMBOL(outsb);
  332. /*
  333. * Like insw but in the opposite direction. This is used by the IDE
  334. * driver to write disk sectors. Performance is important, but the
  335. * interfaces seems to be slow: just using the inlined version of the
  336. * outw() breaks things.
  337. */
  338. void iowrite16_rep(void __iomem *port, const void *src, unsigned long count)
  339. {
  340. if (unlikely((unsigned long)src & 0x3)) {
  341. if (!count)
  342. return;
  343. BUG_ON((unsigned long)src & 0x1);
  344. iowrite16(*(unsigned short *)src, port);
  345. src += 2;
  346. --count;
  347. }
  348. while (count >= 2) {
  349. unsigned int w;
  350. count -= 2;
  351. w = *(unsigned int *)src;
  352. src += 4;
  353. iowrite16(w >> 0, port);
  354. iowrite16(w >> 16, port);
  355. }
  356. if (count) {
  357. iowrite16(*(unsigned short *)src, port);
  358. }
  359. }
  360. void outsw(unsigned long port, const void *src, unsigned long count)
  361. {
  362. iowrite16_rep(ioport_map(port, 2), src, count);
  363. }
  364. EXPORT_SYMBOL(iowrite16_rep);
  365. EXPORT_SYMBOL(outsw);
  366. /*
  367. * Like insl but in the opposite direction. This is used by the IDE
  368. * driver to write disk sectors. Works with any alignment in SRC.
  369. * Performance is important, but the interfaces seems to be slow:
  370. * just using the inlined version of the outl() breaks things.
  371. */
  372. void iowrite32_rep(void __iomem *port, const void *src, unsigned long count)
  373. {
  374. if (unlikely((unsigned long)src & 0x3)) {
  375. while (count--) {
  376. struct S { int x __attribute__((packed)); };
  377. iowrite32(((struct S *)src)->x, port);
  378. src += 4;
  379. }
  380. } else {
  381. /* Buffer 32-bit aligned. */
  382. while (count--) {
  383. iowrite32(*(unsigned int *)src, port);
  384. src += 4;
  385. }
  386. }
  387. }
  388. void outsl(unsigned long port, const void *src, unsigned long count)
  389. {
  390. iowrite32_rep(ioport_map(port, 4), src, count);
  391. }
  392. EXPORT_SYMBOL(iowrite32_rep);
  393. EXPORT_SYMBOL(outsl);
  394. /*
  395. * Copy data from IO memory space to "real" memory space.
  396. * This needs to be optimized.
  397. */
  398. void memcpy_fromio(void *to, const volatile void __iomem *from, long count)
  399. {
  400. /* Optimize co-aligned transfers. Everything else gets handled
  401. a byte at a time. */
  402. if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
  403. count -= 8;
  404. do {
  405. *(u64 *)to = __raw_readq(from);
  406. count -= 8;
  407. to += 8;
  408. from += 8;
  409. } while (count >= 0);
  410. count += 8;
  411. }
  412. if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
  413. count -= 4;
  414. do {
  415. *(u32 *)to = __raw_readl(from);
  416. count -= 4;
  417. to += 4;
  418. from += 4;
  419. } while (count >= 0);
  420. count += 4;
  421. }
  422. if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
  423. count -= 2;
  424. do {
  425. *(u16 *)to = __raw_readw(from);
  426. count -= 2;
  427. to += 2;
  428. from += 2;
  429. } while (count >= 0);
  430. count += 2;
  431. }
  432. while (count > 0) {
  433. *(u8 *) to = __raw_readb(from);
  434. count--;
  435. to++;
  436. from++;
  437. }
  438. mb();
  439. }
  440. EXPORT_SYMBOL(memcpy_fromio);
  441. /*
  442. * Copy data from "real" memory space to IO memory space.
  443. * This needs to be optimized.
  444. */
  445. void memcpy_toio(volatile void __iomem *to, const void *from, long count)
  446. {
  447. /* Optimize co-aligned transfers. Everything else gets handled
  448. a byte at a time. */
  449. /* FIXME -- align FROM. */
  450. if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
  451. count -= 8;
  452. do {
  453. __raw_writeq(*(const u64 *)from, to);
  454. count -= 8;
  455. to += 8;
  456. from += 8;
  457. } while (count >= 0);
  458. count += 8;
  459. }
  460. if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
  461. count -= 4;
  462. do {
  463. __raw_writel(*(const u32 *)from, to);
  464. count -= 4;
  465. to += 4;
  466. from += 4;
  467. } while (count >= 0);
  468. count += 4;
  469. }
  470. if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
  471. count -= 2;
  472. do {
  473. __raw_writew(*(const u16 *)from, to);
  474. count -= 2;
  475. to += 2;
  476. from += 2;
  477. } while (count >= 0);
  478. count += 2;
  479. }
  480. while (count > 0) {
  481. __raw_writeb(*(const u8 *) from, to);
  482. count--;
  483. to++;
  484. from++;
  485. }
  486. mb();
  487. }
  488. EXPORT_SYMBOL(memcpy_toio);
  489. /*
  490. * "memset" on IO memory space.
  491. */
  492. void _memset_c_io(volatile void __iomem *to, unsigned long c, long count)
  493. {
  494. /* Handle any initial odd byte */
  495. if (count > 0 && ((u64)to & 1)) {
  496. __raw_writeb(c, to);
  497. to++;
  498. count--;
  499. }
  500. /* Handle any initial odd halfword */
  501. if (count >= 2 && ((u64)to & 2)) {
  502. __raw_writew(c, to);
  503. to += 2;
  504. count -= 2;
  505. }
  506. /* Handle any initial odd word */
  507. if (count >= 4 && ((u64)to & 4)) {
  508. __raw_writel(c, to);
  509. to += 4;
  510. count -= 4;
  511. }
  512. /* Handle all full-sized quadwords: we're aligned
  513. (or have a small count) */
  514. count -= 8;
  515. if (count >= 0) {
  516. do {
  517. __raw_writeq(c, to);
  518. to += 8;
  519. count -= 8;
  520. } while (count >= 0);
  521. }
  522. count += 8;
  523. /* The tail is word-aligned if we still have count >= 4 */
  524. if (count >= 4) {
  525. __raw_writel(c, to);
  526. to += 4;
  527. count -= 4;
  528. }
  529. /* The tail is half-word aligned if we have count >= 2 */
  530. if (count >= 2) {
  531. __raw_writew(c, to);
  532. to += 2;
  533. count -= 2;
  534. }
  535. /* And finally, one last byte.. */
  536. if (count) {
  537. __raw_writeb(c, to);
  538. }
  539. mb();
  540. }
  541. EXPORT_SYMBOL(_memset_c_io);
  542. /* A version of memcpy used by the vga console routines to move data around
  543. arbitrarily between screen and main memory. */
  544. void
  545. scr_memcpyw(u16 *d, const u16 *s, unsigned int count)
  546. {
  547. const u16 __iomem *ios = (const u16 __iomem *) s;
  548. u16 __iomem *iod = (u16 __iomem *) d;
  549. int s_isio = __is_ioaddr(s);
  550. int d_isio = __is_ioaddr(d);
  551. if (s_isio) {
  552. if (d_isio) {
  553. /* FIXME: Should handle unaligned ops and
  554. operation widening. */
  555. count /= 2;
  556. while (count--) {
  557. u16 tmp = __raw_readw(ios++);
  558. __raw_writew(tmp, iod++);
  559. }
  560. }
  561. else
  562. memcpy_fromio(d, ios, count);
  563. } else {
  564. if (d_isio)
  565. memcpy_toio(iod, s, count);
  566. else
  567. memcpy(d, s, count);
  568. }
  569. }
  570. EXPORT_SYMBOL(scr_memcpyw);
  571. void __iomem *ioport_map(unsigned long port, unsigned int size)
  572. {
  573. return IO_CONCAT(__IO_PREFIX,ioportmap) (port);
  574. }
  575. void ioport_unmap(void __iomem *addr)
  576. {
  577. }
  578. EXPORT_SYMBOL(ioport_map);
  579. EXPORT_SYMBOL(ioport_unmap);