iomap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Implement the default iomap interfaces
  3. *
  4. * (C) Copyright 2004 Linus Torvalds
  5. */
  6. #include <linux/pci.h>
  7. #include <linux/io.h>
  8. #ifdef CONFIG_GENERIC_IOMAP
  9. #include <linux/module.h>
  10. /*
  11. * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
  12. * access or a MMIO access, these functions don't care. The info is
  13. * encoded in the hardware mapping set up by the mapping functions
  14. * (or the cookie itself, depending on implementation and hw).
  15. *
  16. * The generic routines don't assume any hardware mappings, and just
  17. * encode the PIO/MMIO as part of the cookie. They coldly assume that
  18. * the MMIO IO mappings are not in the low address range.
  19. *
  20. * Architectures for which this is not true can't use this generic
  21. * implementation and should do their own copy.
  22. */
  23. #ifndef HAVE_ARCH_PIO_SIZE
  24. /*
  25. * We encode the physical PIO addresses (0-0xffff) into the
  26. * pointer by offsetting them with a constant (0x10000) and
  27. * assuming that all the low addresses are always PIO. That means
  28. * we can do some sanity checks on the low bits, and don't
  29. * need to just take things for granted.
  30. */
  31. #define PIO_OFFSET 0x10000UL
  32. #define PIO_MASK 0x0ffffUL
  33. #define PIO_RESERVED 0x40000UL
  34. #endif
  35. /*
  36. * Ugly macros are a way of life.
  37. */
  38. #define VERIFY_PIO(port) BUG_ON((port & ~PIO_MASK) != PIO_OFFSET)
  39. #define IO_COND(addr, is_pio, is_mmio) do { \
  40. unsigned long port = (unsigned long __force)addr; \
  41. if (port < PIO_RESERVED) { \
  42. VERIFY_PIO(port); \
  43. port &= PIO_MASK; \
  44. is_pio; \
  45. } else { \
  46. is_mmio; \
  47. } \
  48. } while (0)
  49. #ifndef pio_read16be
  50. #define pio_read16be(port) swab16(inw(port))
  51. #define pio_read32be(port) swab32(inl(port))
  52. #endif
  53. #ifndef mmio_read16be
  54. #define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr))
  55. #define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr))
  56. #endif
  57. unsigned int fastcall ioread8(void __iomem *addr)
  58. {
  59. IO_COND(addr, return inb(port), return readb(addr));
  60. }
  61. unsigned int fastcall ioread16(void __iomem *addr)
  62. {
  63. IO_COND(addr, return inw(port), return readw(addr));
  64. }
  65. unsigned int fastcall ioread16be(void __iomem *addr)
  66. {
  67. IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
  68. }
  69. unsigned int fastcall ioread32(void __iomem *addr)
  70. {
  71. IO_COND(addr, return inl(port), return readl(addr));
  72. }
  73. unsigned int fastcall ioread32be(void __iomem *addr)
  74. {
  75. IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
  76. }
  77. EXPORT_SYMBOL(ioread8);
  78. EXPORT_SYMBOL(ioread16);
  79. EXPORT_SYMBOL(ioread16be);
  80. EXPORT_SYMBOL(ioread32);
  81. EXPORT_SYMBOL(ioread32be);
  82. #ifndef pio_write16be
  83. #define pio_write16be(val,port) outw(swab16(val),port)
  84. #define pio_write32be(val,port) outl(swab32(val),port)
  85. #endif
  86. #ifndef mmio_write16be
  87. #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
  88. #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
  89. #endif
  90. void fastcall iowrite8(u8 val, void __iomem *addr)
  91. {
  92. IO_COND(addr, outb(val,port), writeb(val, addr));
  93. }
  94. void fastcall iowrite16(u16 val, void __iomem *addr)
  95. {
  96. IO_COND(addr, outw(val,port), writew(val, addr));
  97. }
  98. void fastcall iowrite16be(u16 val, void __iomem *addr)
  99. {
  100. IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
  101. }
  102. void fastcall iowrite32(u32 val, void __iomem *addr)
  103. {
  104. IO_COND(addr, outl(val,port), writel(val, addr));
  105. }
  106. void fastcall iowrite32be(u32 val, void __iomem *addr)
  107. {
  108. IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
  109. }
  110. EXPORT_SYMBOL(iowrite8);
  111. EXPORT_SYMBOL(iowrite16);
  112. EXPORT_SYMBOL(iowrite16be);
  113. EXPORT_SYMBOL(iowrite32);
  114. EXPORT_SYMBOL(iowrite32be);
  115. /*
  116. * These are the "repeat MMIO read/write" functions.
  117. * Note the "__raw" accesses, since we don't want to
  118. * convert to CPU byte order. We write in "IO byte
  119. * order" (we also don't have IO barriers).
  120. */
  121. #ifndef mmio_insb
  122. static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
  123. {
  124. while (--count >= 0) {
  125. u8 data = __raw_readb(addr);
  126. *dst = data;
  127. dst++;
  128. }
  129. }
  130. static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
  131. {
  132. while (--count >= 0) {
  133. u16 data = __raw_readw(addr);
  134. *dst = data;
  135. dst++;
  136. }
  137. }
  138. static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
  139. {
  140. while (--count >= 0) {
  141. u32 data = __raw_readl(addr);
  142. *dst = data;
  143. dst++;
  144. }
  145. }
  146. #endif
  147. #ifndef mmio_outsb
  148. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  149. {
  150. while (--count >= 0) {
  151. __raw_writeb(*src, addr);
  152. src++;
  153. }
  154. }
  155. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  156. {
  157. while (--count >= 0) {
  158. __raw_writew(*src, addr);
  159. src++;
  160. }
  161. }
  162. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  163. {
  164. while (--count >= 0) {
  165. __raw_writel(*src, addr);
  166. src++;
  167. }
  168. }
  169. #endif
  170. void fastcall ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  171. {
  172. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  173. }
  174. void fastcall ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  175. {
  176. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  177. }
  178. void fastcall ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  179. {
  180. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  181. }
  182. EXPORT_SYMBOL(ioread8_rep);
  183. EXPORT_SYMBOL(ioread16_rep);
  184. EXPORT_SYMBOL(ioread32_rep);
  185. void fastcall iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  186. {
  187. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  188. }
  189. void fastcall iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  190. {
  191. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  192. }
  193. void fastcall iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  194. {
  195. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  196. }
  197. EXPORT_SYMBOL(iowrite8_rep);
  198. EXPORT_SYMBOL(iowrite16_rep);
  199. EXPORT_SYMBOL(iowrite32_rep);
  200. /* Create a virtual mapping cookie for an IO port range */
  201. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  202. {
  203. if (port > PIO_MASK)
  204. return NULL;
  205. return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  206. }
  207. void ioport_unmap(void __iomem *addr)
  208. {
  209. /* Nothing to do */
  210. }
  211. EXPORT_SYMBOL(ioport_map);
  212. EXPORT_SYMBOL(ioport_unmap);
  213. /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
  214. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  215. {
  216. unsigned long start = pci_resource_start(dev, bar);
  217. unsigned long len = pci_resource_len(dev, bar);
  218. unsigned long flags = pci_resource_flags(dev, bar);
  219. if (!len || !start)
  220. return NULL;
  221. if (maxlen && len > maxlen)
  222. len = maxlen;
  223. if (flags & IORESOURCE_IO)
  224. return ioport_map(start, len);
  225. if (flags & IORESOURCE_MEM) {
  226. if (flags & IORESOURCE_CACHEABLE)
  227. return ioremap(start, len);
  228. return ioremap_nocache(start, len);
  229. }
  230. /* What? */
  231. return NULL;
  232. }
  233. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  234. {
  235. IO_COND(addr, /* nothing */, iounmap(addr));
  236. }
  237. EXPORT_SYMBOL(pci_iomap);
  238. EXPORT_SYMBOL(pci_iounmap);
  239. #endif /* CONFIG_GENERIC_IOMAP */
  240. /*
  241. * Generic iomap devres
  242. */
  243. static void devm_ioport_map_release(struct device *dev, void *res)
  244. {
  245. ioport_unmap(*(void __iomem **)res);
  246. }
  247. static int devm_ioport_map_match(struct device *dev, void *res,
  248. void *match_data)
  249. {
  250. return *(void **)res == match_data;
  251. }
  252. /**
  253. * devm_ioport_map - Managed ioport_map()
  254. * @dev: Generic device to map ioport for
  255. * @port: Port to map
  256. * @nr: Number of ports to map
  257. *
  258. * Managed ioport_map(). Map is automatically unmapped on driver
  259. * detach.
  260. */
  261. void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
  262. unsigned int nr)
  263. {
  264. void __iomem **ptr, *addr;
  265. ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
  266. if (!ptr)
  267. return NULL;
  268. addr = ioport_map(port, nr);
  269. if (addr) {
  270. *ptr = addr;
  271. devres_add(dev, ptr);
  272. } else
  273. devres_free(ptr);
  274. return addr;
  275. }
  276. EXPORT_SYMBOL(devm_ioport_map);
  277. /**
  278. * devm_ioport_unmap - Managed ioport_unmap()
  279. * @dev: Generic device to unmap for
  280. * @addr: Address to unmap
  281. *
  282. * Managed ioport_unmap(). @addr must have been mapped using
  283. * devm_ioport_map().
  284. */
  285. void devm_ioport_unmap(struct device *dev, void __iomem *addr)
  286. {
  287. ioport_unmap(addr);
  288. WARN_ON(devres_destroy(dev, devm_ioport_map_release,
  289. devm_ioport_map_match, (void *)addr));
  290. }
  291. EXPORT_SYMBOL(devm_ioport_unmap);
  292. static void devm_ioremap_release(struct device *dev, void *res)
  293. {
  294. iounmap(*(void __iomem **)res);
  295. }
  296. static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
  297. {
  298. return *(void **)res == match_data;
  299. }
  300. /**
  301. * devm_ioremap - Managed ioremap()
  302. * @dev: Generic device to remap IO address for
  303. * @offset: BUS offset to map
  304. * @size: Size of map
  305. *
  306. * Managed ioremap(). Map is automatically unmapped on driver detach.
  307. */
  308. void __iomem *devm_ioremap(struct device *dev, unsigned long offset,
  309. unsigned long size)
  310. {
  311. void __iomem **ptr, *addr;
  312. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  313. if (!ptr)
  314. return NULL;
  315. addr = ioremap(offset, size);
  316. if (addr) {
  317. *ptr = addr;
  318. devres_add(dev, ptr);
  319. } else
  320. devres_free(ptr);
  321. return addr;
  322. }
  323. EXPORT_SYMBOL(devm_ioremap);
  324. /**
  325. * devm_ioremap_nocache - Managed ioremap_nocache()
  326. * @dev: Generic device to remap IO address for
  327. * @offset: BUS offset to map
  328. * @size: Size of map
  329. *
  330. * Managed ioremap_nocache(). Map is automatically unmapped on driver
  331. * detach.
  332. */
  333. void __iomem *devm_ioremap_nocache(struct device *dev, unsigned long offset,
  334. unsigned long size)
  335. {
  336. void __iomem **ptr, *addr;
  337. ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
  338. if (!ptr)
  339. return NULL;
  340. addr = ioremap_nocache(offset, size);
  341. if (addr) {
  342. *ptr = addr;
  343. devres_add(dev, ptr);
  344. } else
  345. devres_free(ptr);
  346. return addr;
  347. }
  348. EXPORT_SYMBOL(devm_ioremap_nocache);
  349. /**
  350. * devm_iounmap - Managed iounmap()
  351. * @dev: Generic device to unmap for
  352. * @addr: Address to unmap
  353. *
  354. * Managed iounmap(). @addr must have been mapped using devm_ioremap*().
  355. */
  356. void devm_iounmap(struct device *dev, void __iomem *addr)
  357. {
  358. iounmap(addr);
  359. WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
  360. (void *)addr));
  361. }
  362. EXPORT_SYMBOL(devm_iounmap);
  363. /*
  364. * PCI iomap devres
  365. */
  366. #define PCIM_IOMAP_MAX PCI_ROM_RESOURCE
  367. struct pcim_iomap_devres {
  368. void __iomem *table[PCIM_IOMAP_MAX];
  369. };
  370. static void pcim_iomap_release(struct device *gendev, void *res)
  371. {
  372. struct pci_dev *dev = container_of(gendev, struct pci_dev, dev);
  373. struct pcim_iomap_devres *this = res;
  374. int i;
  375. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  376. if (this->table[i])
  377. pci_iounmap(dev, this->table[i]);
  378. }
  379. /**
  380. * pcim_iomap_table - access iomap allocation table
  381. * @pdev: PCI device to access iomap table for
  382. *
  383. * Access iomap allocation table for @dev. If iomap table doesn't
  384. * exist and @pdev is managed, it will be allocated. All iomaps
  385. * recorded in the iomap table are automatically unmapped on driver
  386. * detach.
  387. *
  388. * This function might sleep when the table is first allocated but can
  389. * be safely called without context and guaranteed to succed once
  390. * allocated.
  391. */
  392. void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
  393. {
  394. struct pcim_iomap_devres *dr, *new_dr;
  395. dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
  396. if (dr)
  397. return dr->table;
  398. new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
  399. if (!new_dr)
  400. return NULL;
  401. dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
  402. return dr->table;
  403. }
  404. EXPORT_SYMBOL(pcim_iomap_table);
  405. /**
  406. * pcim_iomap - Managed pcim_iomap()
  407. * @pdev: PCI device to iomap for
  408. * @bar: BAR to iomap
  409. * @maxlen: Maximum length of iomap
  410. *
  411. * Managed pci_iomap(). Map is automatically unmapped on driver
  412. * detach.
  413. */
  414. void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
  415. {
  416. void __iomem **tbl;
  417. BUG_ON(bar >= PCIM_IOMAP_MAX);
  418. tbl = (void __iomem **)pcim_iomap_table(pdev);
  419. if (!tbl || tbl[bar]) /* duplicate mappings not allowed */
  420. return NULL;
  421. tbl[bar] = pci_iomap(pdev, bar, maxlen);
  422. return tbl[bar];
  423. }
  424. EXPORT_SYMBOL(pcim_iomap);
  425. /**
  426. * pcim_iounmap - Managed pci_iounmap()
  427. * @pdev: PCI device to iounmap for
  428. * @addr: Address to unmap
  429. *
  430. * Managed pci_iounmap(). @addr must have been mapped using pcim_iomap().
  431. */
  432. void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
  433. {
  434. void __iomem **tbl;
  435. int i;
  436. pci_iounmap(pdev, addr);
  437. tbl = (void __iomem **)pcim_iomap_table(pdev);
  438. BUG_ON(!tbl);
  439. for (i = 0; i < PCIM_IOMAP_MAX; i++)
  440. if (tbl[i] == addr) {
  441. tbl[i] = NULL;
  442. return;
  443. }
  444. WARN_ON(1);
  445. }
  446. EXPORT_SYMBOL(pcim_iounmap);
  447. /**
  448. * pcim_iomap_regions - Request and iomap PCI BARs
  449. * @pdev: PCI device to map IO resources for
  450. * @mask: Mask of BARs to request and iomap
  451. * @name: Name used when requesting regions
  452. *
  453. * Request and iomap regions specified by @mask.
  454. */
  455. int pcim_iomap_regions(struct pci_dev *pdev, u16 mask, const char *name)
  456. {
  457. void __iomem * const *iomap;
  458. int i, rc;
  459. iomap = pcim_iomap_table(pdev);
  460. if (!iomap)
  461. return -ENOMEM;
  462. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  463. unsigned long len;
  464. if (!(mask & (1 << i)))
  465. continue;
  466. rc = -EINVAL;
  467. len = pci_resource_len(pdev, i);
  468. if (!len)
  469. goto err_inval;
  470. rc = pci_request_region(pdev, i, name);
  471. if (rc)
  472. goto err_region;
  473. rc = -ENOMEM;
  474. if (!pcim_iomap(pdev, i, 0))
  475. goto err_iomap;
  476. }
  477. return 0;
  478. err_iomap:
  479. pcim_iounmap(pdev, iomap[i]);
  480. err_region:
  481. pci_release_region(pdev, i);
  482. err_inval:
  483. while (--i >= 0) {
  484. pcim_iounmap(pdev, iomap[i]);
  485. pci_release_region(pdev, i);
  486. }
  487. return rc;
  488. }
  489. EXPORT_SYMBOL(pcim_iomap_regions);