eeh_cache.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * PCI address cache; allows the lookup of PCI devices based on I/O address
  3. *
  4. * Copyright IBM Corporation 2004
  5. * Copyright Linas Vepstas <linas@austin.ibm.com> 2004
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/list.h>
  22. #include <linux/pci.h>
  23. #include <linux/rbtree.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/atomic.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/ppc-pci.h>
  29. /**
  30. * The pci address cache subsystem. This subsystem places
  31. * PCI device address resources into a red-black tree, sorted
  32. * according to the address range, so that given only an i/o
  33. * address, the corresponding PCI device can be **quickly**
  34. * found. It is safe to perform an address lookup in an interrupt
  35. * context; this ability is an important feature.
  36. *
  37. * Currently, the only customer of this code is the EEH subsystem;
  38. * thus, this code has been somewhat tailored to suit EEH better.
  39. * In particular, the cache does *not* hold the addresses of devices
  40. * for which EEH is not enabled.
  41. *
  42. * (Implementation Note: The RB tree seems to be better/faster
  43. * than any hash algo I could think of for this problem, even
  44. * with the penalty of slow pointer chases for d-cache misses).
  45. */
  46. struct pci_io_addr_range {
  47. struct rb_node rb_node;
  48. unsigned long addr_lo;
  49. unsigned long addr_hi;
  50. struct pci_dev *pcidev;
  51. unsigned int flags;
  52. };
  53. static struct pci_io_addr_cache {
  54. struct rb_root rb_root;
  55. spinlock_t piar_lock;
  56. } pci_io_addr_cache_root;
  57. static inline struct pci_dev *__pci_addr_cache_get_device(unsigned long addr)
  58. {
  59. struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
  60. while (n) {
  61. struct pci_io_addr_range *piar;
  62. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  63. if (addr < piar->addr_lo) {
  64. n = n->rb_left;
  65. } else {
  66. if (addr > piar->addr_hi) {
  67. n = n->rb_right;
  68. } else {
  69. pci_dev_get(piar->pcidev);
  70. return piar->pcidev;
  71. }
  72. }
  73. }
  74. return NULL;
  75. }
  76. /**
  77. * pci_addr_cache_get_device - Get device, given only address
  78. * @addr: mmio (PIO) phys address or i/o port number
  79. *
  80. * Given an mmio phys address, or a port number, find a pci device
  81. * that implements this address. Be sure to pci_dev_put the device
  82. * when finished. I/O port numbers are assumed to be offset
  83. * from zero (that is, they do *not* have pci_io_addr added in).
  84. * It is safe to call this function within an interrupt.
  85. */
  86. struct pci_dev *pci_addr_cache_get_device(unsigned long addr)
  87. {
  88. struct pci_dev *dev;
  89. unsigned long flags;
  90. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  91. dev = __pci_addr_cache_get_device(addr);
  92. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  93. return dev;
  94. }
  95. #ifdef DEBUG
  96. /*
  97. * Handy-dandy debug print routine, does nothing more
  98. * than print out the contents of our addr cache.
  99. */
  100. static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
  101. {
  102. struct rb_node *n;
  103. int cnt = 0;
  104. n = rb_first(&cache->rb_root);
  105. while (n) {
  106. struct pci_io_addr_range *piar;
  107. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  108. printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
  109. (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
  110. piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
  111. cnt++;
  112. n = rb_next(n);
  113. }
  114. }
  115. #endif
  116. /* Insert address range into the rb tree. */
  117. static struct pci_io_addr_range *
  118. pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
  119. unsigned long ahi, unsigned int flags)
  120. {
  121. struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
  122. struct rb_node *parent = NULL;
  123. struct pci_io_addr_range *piar;
  124. /* Walk tree, find a place to insert into tree */
  125. while (*p) {
  126. parent = *p;
  127. piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
  128. if (ahi < piar->addr_lo) {
  129. p = &parent->rb_left;
  130. } else if (alo > piar->addr_hi) {
  131. p = &parent->rb_right;
  132. } else {
  133. if (dev != piar->pcidev ||
  134. alo != piar->addr_lo || ahi != piar->addr_hi) {
  135. printk(KERN_WARNING "PIAR: overlapping address range\n");
  136. }
  137. return piar;
  138. }
  139. }
  140. piar = kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
  141. if (!piar)
  142. return NULL;
  143. pci_dev_get(dev);
  144. piar->addr_lo = alo;
  145. piar->addr_hi = ahi;
  146. piar->pcidev = dev;
  147. piar->flags = flags;
  148. #ifdef DEBUG
  149. printk(KERN_DEBUG "PIAR: insert range=[%lx:%lx] dev=%s\n",
  150. alo, ahi, pci_name(dev));
  151. #endif
  152. rb_link_node(&piar->rb_node, parent, p);
  153. rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
  154. return piar;
  155. }
  156. static void __pci_addr_cache_insert_device(struct pci_dev *dev)
  157. {
  158. struct device_node *dn;
  159. struct eeh_dev *edev;
  160. int i;
  161. dn = pci_device_to_OF_node(dev);
  162. if (!dn) {
  163. printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
  164. return;
  165. }
  166. edev = of_node_to_eeh_dev(dn);
  167. if (!edev) {
  168. pr_warning("PCI: no EEH dev found for dn=%s\n",
  169. dn->full_name);
  170. return;
  171. }
  172. /* Skip any devices for which EEH is not enabled. */
  173. if (!(edev->mode & EEH_MODE_SUPPORTED) ||
  174. edev->mode & EEH_MODE_NOCHECK) {
  175. #ifdef DEBUG
  176. pr_info("PCI: skip building address cache for=%s - %s\n",
  177. pci_name(dev), dn->full_name);
  178. #endif
  179. return;
  180. }
  181. /* Walk resources on this device, poke them into the tree */
  182. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  183. unsigned long start = pci_resource_start(dev,i);
  184. unsigned long end = pci_resource_end(dev,i);
  185. unsigned int flags = pci_resource_flags(dev,i);
  186. /* We are interested only bus addresses, not dma or other stuff */
  187. if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  188. continue;
  189. if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
  190. continue;
  191. pci_addr_cache_insert(dev, start, end, flags);
  192. }
  193. }
  194. /**
  195. * pci_addr_cache_insert_device - Add a device to the address cache
  196. * @dev: PCI device whose I/O addresses we are interested in.
  197. *
  198. * In order to support the fast lookup of devices based on addresses,
  199. * we maintain a cache of devices that can be quickly searched.
  200. * This routine adds a device to that cache.
  201. */
  202. void pci_addr_cache_insert_device(struct pci_dev *dev)
  203. {
  204. unsigned long flags;
  205. /* Ignore PCI bridges */
  206. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE)
  207. return;
  208. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  209. __pci_addr_cache_insert_device(dev);
  210. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  211. }
  212. static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
  213. {
  214. struct rb_node *n;
  215. restart:
  216. n = rb_first(&pci_io_addr_cache_root.rb_root);
  217. while (n) {
  218. struct pci_io_addr_range *piar;
  219. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  220. if (piar->pcidev == dev) {
  221. rb_erase(n, &pci_io_addr_cache_root.rb_root);
  222. pci_dev_put(piar->pcidev);
  223. kfree(piar);
  224. goto restart;
  225. }
  226. n = rb_next(n);
  227. }
  228. }
  229. /**
  230. * pci_addr_cache_remove_device - remove pci device from addr cache
  231. * @dev: device to remove
  232. *
  233. * Remove a device from the addr-cache tree.
  234. * This is potentially expensive, since it will walk
  235. * the tree multiple times (once per resource).
  236. * But so what; device removal doesn't need to be that fast.
  237. */
  238. void pci_addr_cache_remove_device(struct pci_dev *dev)
  239. {
  240. unsigned long flags;
  241. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  242. __pci_addr_cache_remove_device(dev);
  243. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  244. }
  245. /**
  246. * pci_addr_cache_build - Build a cache of I/O addresses
  247. *
  248. * Build a cache of pci i/o addresses. This cache will be used to
  249. * find the pci device that corresponds to a given address.
  250. * This routine scans all pci busses to build the cache.
  251. * Must be run late in boot process, after the pci controllers
  252. * have been scanned for devices (after all device resources are known).
  253. */
  254. void __init pci_addr_cache_build(void)
  255. {
  256. struct device_node *dn;
  257. struct eeh_dev *edev;
  258. struct pci_dev *dev = NULL;
  259. spin_lock_init(&pci_io_addr_cache_root.piar_lock);
  260. for_each_pci_dev(dev) {
  261. pci_addr_cache_insert_device(dev);
  262. dn = pci_device_to_OF_node(dev);
  263. if (!dn)
  264. continue;
  265. edev = of_node_to_eeh_dev(dn);
  266. if (!edev)
  267. continue;
  268. pci_dev_get(dev); /* matching put is in eeh_remove_device() */
  269. dev->dev.archdata.edev = edev;
  270. edev->pdev = dev;
  271. eeh_sysfs_add_device(dev);
  272. }
  273. #ifdef DEBUG
  274. /* Verify tree built up above, echo back the list of addrs. */
  275. pci_addr_cache_print(&pci_io_addr_cache_root);
  276. #endif
  277. }