eeh_cache.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. resource_size_t addr_lo;
  49. resource_size_t addr_hi;
  50. struct eeh_dev *edev;
  51. struct pci_dev *pcidev;
  52. unsigned long flags;
  53. };
  54. static struct pci_io_addr_cache {
  55. struct rb_root rb_root;
  56. spinlock_t piar_lock;
  57. } pci_io_addr_cache_root;
  58. static inline struct eeh_dev *__eeh_addr_cache_get_device(unsigned long addr)
  59. {
  60. struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
  61. while (n) {
  62. struct pci_io_addr_range *piar;
  63. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  64. if (addr < piar->addr_lo)
  65. n = n->rb_left;
  66. else if (addr > piar->addr_hi)
  67. n = n->rb_right;
  68. else
  69. return piar->edev;
  70. }
  71. return NULL;
  72. }
  73. /**
  74. * eeh_addr_cache_get_dev - Get device, given only address
  75. * @addr: mmio (PIO) phys address or i/o port number
  76. *
  77. * Given an mmio phys address, or a port number, find a pci device
  78. * that implements this address. I/O port numbers are assumed to be offset
  79. * from zero (that is, they do *not* have pci_io_addr added in).
  80. * It is safe to call this function within an interrupt.
  81. */
  82. struct eeh_dev *eeh_addr_cache_get_dev(unsigned long addr)
  83. {
  84. struct eeh_dev *edev;
  85. unsigned long flags;
  86. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  87. edev = __eeh_addr_cache_get_device(addr);
  88. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  89. return edev;
  90. }
  91. #ifdef DEBUG
  92. /*
  93. * Handy-dandy debug print routine, does nothing more
  94. * than print out the contents of our addr cache.
  95. */
  96. static void eeh_addr_cache_print(struct pci_io_addr_cache *cache)
  97. {
  98. struct rb_node *n;
  99. int cnt = 0;
  100. n = rb_first(&cache->rb_root);
  101. while (n) {
  102. struct pci_io_addr_range *piar;
  103. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  104. pr_debug("PCI: %s addr range %d [%pap-%pap]: %s\n",
  105. (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
  106. &piar->addr_lo, &piar->addr_hi, pci_name(piar->pcidev));
  107. cnt++;
  108. n = rb_next(n);
  109. }
  110. }
  111. #endif
  112. /* Insert address range into the rb tree. */
  113. static struct pci_io_addr_range *
  114. eeh_addr_cache_insert(struct pci_dev *dev, resource_size_t alo,
  115. resource_size_t ahi, unsigned long flags)
  116. {
  117. struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
  118. struct rb_node *parent = NULL;
  119. struct pci_io_addr_range *piar;
  120. /* Walk tree, find a place to insert into tree */
  121. while (*p) {
  122. parent = *p;
  123. piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
  124. if (ahi < piar->addr_lo) {
  125. p = &parent->rb_left;
  126. } else if (alo > piar->addr_hi) {
  127. p = &parent->rb_right;
  128. } else {
  129. if (dev != piar->pcidev ||
  130. alo != piar->addr_lo || ahi != piar->addr_hi) {
  131. pr_warn("PIAR: overlapping address range\n");
  132. }
  133. return piar;
  134. }
  135. }
  136. piar = kzalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
  137. if (!piar)
  138. return NULL;
  139. piar->addr_lo = alo;
  140. piar->addr_hi = ahi;
  141. piar->edev = pci_dev_to_eeh_dev(dev);
  142. piar->pcidev = dev;
  143. piar->flags = flags;
  144. #ifdef DEBUG
  145. pr_debug("PIAR: insert range=[%pap:%pap] dev=%s\n",
  146. &alo, &ahi, pci_name(dev));
  147. #endif
  148. rb_link_node(&piar->rb_node, parent, p);
  149. rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
  150. return piar;
  151. }
  152. static void __eeh_addr_cache_insert_dev(struct pci_dev *dev)
  153. {
  154. struct pci_dn *pdn;
  155. struct eeh_dev *edev;
  156. int i;
  157. pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
  158. if (!pdn) {
  159. pr_warn("PCI: no pci dn found for dev=%s\n",
  160. pci_name(dev));
  161. return;
  162. }
  163. edev = pdn_to_eeh_dev(pdn);
  164. if (!edev) {
  165. pr_warn("PCI: no EEH dev found for %s\n",
  166. pci_name(dev));
  167. return;
  168. }
  169. /* Skip any devices for which EEH is not enabled. */
  170. if (!edev->pe) {
  171. dev_dbg(&dev->dev, "EEH: Skip building address cache\n");
  172. return;
  173. }
  174. /*
  175. * Walk resources on this device, poke the first 7 (6 normal BAR and 1
  176. * ROM BAR) into the tree.
  177. */
  178. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  179. resource_size_t start = pci_resource_start(dev,i);
  180. resource_size_t end = pci_resource_end(dev,i);
  181. unsigned long flags = pci_resource_flags(dev,i);
  182. /* We are interested only bus addresses, not dma or other stuff */
  183. if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  184. continue;
  185. if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
  186. continue;
  187. eeh_addr_cache_insert(dev, start, end, flags);
  188. }
  189. }
  190. /**
  191. * eeh_addr_cache_insert_dev - Add a device to the address cache
  192. * @dev: PCI device whose I/O addresses we are interested in.
  193. *
  194. * In order to support the fast lookup of devices based on addresses,
  195. * we maintain a cache of devices that can be quickly searched.
  196. * This routine adds a device to that cache.
  197. */
  198. void eeh_addr_cache_insert_dev(struct pci_dev *dev)
  199. {
  200. unsigned long flags;
  201. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  202. __eeh_addr_cache_insert_dev(dev);
  203. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  204. }
  205. static inline void __eeh_addr_cache_rmv_dev(struct pci_dev *dev)
  206. {
  207. struct rb_node *n;
  208. restart:
  209. n = rb_first(&pci_io_addr_cache_root.rb_root);
  210. while (n) {
  211. struct pci_io_addr_range *piar;
  212. piar = rb_entry(n, struct pci_io_addr_range, rb_node);
  213. if (piar->pcidev == dev) {
  214. rb_erase(n, &pci_io_addr_cache_root.rb_root);
  215. kfree(piar);
  216. goto restart;
  217. }
  218. n = rb_next(n);
  219. }
  220. }
  221. /**
  222. * eeh_addr_cache_rmv_dev - remove pci device from addr cache
  223. * @dev: device to remove
  224. *
  225. * Remove a device from the addr-cache tree.
  226. * This is potentially expensive, since it will walk
  227. * the tree multiple times (once per resource).
  228. * But so what; device removal doesn't need to be that fast.
  229. */
  230. void eeh_addr_cache_rmv_dev(struct pci_dev *dev)
  231. {
  232. unsigned long flags;
  233. spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
  234. __eeh_addr_cache_rmv_dev(dev);
  235. spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
  236. }
  237. /**
  238. * eeh_addr_cache_build - Build a cache of I/O addresses
  239. *
  240. * Build a cache of pci i/o addresses. This cache will be used to
  241. * find the pci device that corresponds to a given address.
  242. * This routine scans all pci busses to build the cache.
  243. * Must be run late in boot process, after the pci controllers
  244. * have been scanned for devices (after all device resources are known).
  245. */
  246. void eeh_addr_cache_build(void)
  247. {
  248. struct pci_dn *pdn;
  249. struct eeh_dev *edev;
  250. struct pci_dev *dev = NULL;
  251. spin_lock_init(&pci_io_addr_cache_root.piar_lock);
  252. for_each_pci_dev(dev) {
  253. pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
  254. if (!pdn)
  255. continue;
  256. edev = pdn_to_eeh_dev(pdn);
  257. if (!edev)
  258. continue;
  259. dev->dev.archdata.edev = edev;
  260. edev->pdev = dev;
  261. eeh_addr_cache_insert_dev(dev);
  262. eeh_sysfs_add_device(dev);
  263. }
  264. #ifdef DEBUG
  265. /* Verify tree built up above, echo back the list of addrs. */
  266. eeh_addr_cache_print(&pci_io_addr_cache_root);
  267. #endif
  268. }