eeh_cache.c 8.6 KB

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