tce.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Derived from arch/powerpc/platforms/pseries/iommu.c
  3. *
  4. * Copyright (C) IBM Corporation, 2006
  5. *
  6. * Author: Jon Mason <jdmason@us.ibm.com>
  7. * Author: Muli Ben-Yehuda <muli@il.ibm.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/config.h>
  24. #include <linux/types.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/string.h>
  29. #include <linux/pci.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/bootmem.h>
  32. #include <asm/tce.h>
  33. #include <asm/calgary.h>
  34. #include <asm/proto.h>
  35. /* flush a tce at 'tceaddr' to main memory */
  36. static inline void flush_tce(void* tceaddr)
  37. {
  38. /* a single tce can't cross a cache line */
  39. if (cpu_has_clflush)
  40. asm volatile("clflush (%0)" :: "r" (tceaddr));
  41. else
  42. asm volatile("wbinvd":::"memory");
  43. }
  44. void tce_build(struct iommu_table *tbl, unsigned long index,
  45. unsigned int npages, unsigned long uaddr, int direction)
  46. {
  47. u64* tp;
  48. u64 t;
  49. u64 rpn;
  50. t = (1 << TCE_READ_SHIFT);
  51. if (direction != DMA_TO_DEVICE)
  52. t |= (1 << TCE_WRITE_SHIFT);
  53. tp = ((u64*)tbl->it_base) + index;
  54. while (npages--) {
  55. rpn = (virt_to_bus((void*)uaddr)) >> PAGE_SHIFT;
  56. t &= ~TCE_RPN_MASK;
  57. t |= (rpn << TCE_RPN_SHIFT);
  58. *tp = cpu_to_be64(t);
  59. flush_tce(tp);
  60. uaddr += PAGE_SIZE;
  61. tp++;
  62. }
  63. }
  64. void tce_free(struct iommu_table *tbl, long index, unsigned int npages)
  65. {
  66. u64* tp;
  67. tp = ((u64*)tbl->it_base) + index;
  68. while (npages--) {
  69. *tp = cpu_to_be64(0);
  70. flush_tce(tp);
  71. tp++;
  72. }
  73. }
  74. static inline unsigned int table_size_to_number_of_entries(unsigned char size)
  75. {
  76. /*
  77. * size is the order of the table, 0-7
  78. * smallest table is 8K entries, so shift result by 13 to
  79. * multiply by 8K
  80. */
  81. return (1 << size) << 13;
  82. }
  83. static int tce_table_setparms(struct pci_dev *dev, struct iommu_table *tbl)
  84. {
  85. unsigned int bitmapsz;
  86. unsigned long bmppages;
  87. int ret;
  88. tbl->it_busno = dev->bus->number;
  89. /* set the tce table size - measured in entries */
  90. tbl->it_size = table_size_to_number_of_entries(specified_table_size);
  91. tbl->it_base = (unsigned long)tce_table_kva[dev->bus->number];
  92. if (!tbl->it_base) {
  93. printk(KERN_ERR "Calgary: iommu_table_setparms: "
  94. "no table allocated?!\n");
  95. ret = -ENOMEM;
  96. goto done;
  97. }
  98. /*
  99. * number of bytes needed for the bitmap size in number of
  100. * entries; we need one bit per entry
  101. */
  102. bitmapsz = tbl->it_size / BITS_PER_BYTE;
  103. bmppages = __get_free_pages(GFP_KERNEL, get_order(bitmapsz));
  104. if (!bmppages) {
  105. printk(KERN_ERR "Calgary: cannot allocate bitmap\n");
  106. ret = -ENOMEM;
  107. goto done;
  108. }
  109. tbl->it_map = (unsigned long*)bmppages;
  110. memset(tbl->it_map, 0, bitmapsz);
  111. tbl->it_hint = 0;
  112. spin_lock_init(&tbl->it_lock);
  113. return 0;
  114. done:
  115. return ret;
  116. }
  117. int build_tce_table(struct pci_dev *dev, void __iomem *bbar)
  118. {
  119. struct iommu_table *tbl;
  120. int ret;
  121. if (dev->sysdata) {
  122. printk(KERN_ERR "Calgary: dev %p has sysdata %p\n",
  123. dev, dev->sysdata);
  124. BUG();
  125. }
  126. tbl = kzalloc(sizeof(struct iommu_table), GFP_KERNEL);
  127. if (!tbl) {
  128. printk(KERN_ERR "Calgary: error allocating iommu_table\n");
  129. ret = -ENOMEM;
  130. goto done;
  131. }
  132. ret = tce_table_setparms(dev, tbl);
  133. if (ret)
  134. goto free_tbl;
  135. tce_free(tbl, 0, tbl->it_size);
  136. tbl->bbar = bbar;
  137. /*
  138. * NUMA is already using the bus's sysdata pointer, so we use
  139. * the bus's pci_dev's sysdata instead.
  140. */
  141. dev->sysdata = tbl;
  142. return 0;
  143. free_tbl:
  144. kfree(tbl);
  145. done:
  146. return ret;
  147. }
  148. void* alloc_tce_table(void)
  149. {
  150. unsigned int size;
  151. size = table_size_to_number_of_entries(specified_table_size);
  152. size *= TCE_ENTRY_SIZE;
  153. return __alloc_bootmem_low(size, size, 0);
  154. }
  155. void free_tce_table(void *tbl)
  156. {
  157. unsigned int size;
  158. if (!tbl)
  159. return;
  160. size = table_size_to_number_of_entries(specified_table_size);
  161. size *= TCE_ENTRY_SIZE;
  162. free_bootmem(__pa(tbl), size);
  163. }