tce.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Derived from arch/powerpc/platforms/pseries/iommu.c
  3. *
  4. * Copyright (C) 2006 Jon Mason <jdmason@us.ibm.com>, IBM Corporation
  5. * Copyright (C) 2006 Muli Ben-Yehuda <muli@il.ibm.com>, IBM Corporation
  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/config.h>
  22. #include <linux/types.h>
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/string.h>
  27. #include <linux/pci.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/bootmem.h>
  30. #include <asm/tce.h>
  31. #include <asm/calgary.h>
  32. #include <asm/proto.h>
  33. /* flush a tce at 'tceaddr' to main memory */
  34. static inline void flush_tce(void* tceaddr)
  35. {
  36. /* a single tce can't cross a cache line */
  37. if (cpu_has_clflush)
  38. asm volatile("clflush (%0)" :: "r" (tceaddr));
  39. else
  40. asm volatile("wbinvd":::"memory");
  41. }
  42. void tce_build(struct iommu_table *tbl, unsigned long index,
  43. unsigned int npages, unsigned long uaddr, int direction)
  44. {
  45. u64* tp;
  46. u64 t;
  47. u64 rpn;
  48. t = (1 << TCE_READ_SHIFT);
  49. if (direction != DMA_TO_DEVICE)
  50. t |= (1 << TCE_WRITE_SHIFT);
  51. tp = ((u64*)tbl->it_base) + index;
  52. while (npages--) {
  53. rpn = (virt_to_bus((void*)uaddr)) >> PAGE_SHIFT;
  54. t &= ~TCE_RPN_MASK;
  55. t |= (rpn << TCE_RPN_SHIFT);
  56. *tp = cpu_to_be64(t);
  57. flush_tce(tp);
  58. uaddr += PAGE_SIZE;
  59. tp++;
  60. }
  61. }
  62. void tce_free(struct iommu_table *tbl, long index, unsigned int npages)
  63. {
  64. u64* tp;
  65. tp = ((u64*)tbl->it_base) + index;
  66. while (npages--) {
  67. *tp = cpu_to_be64(0);
  68. flush_tce(tp);
  69. tp++;
  70. }
  71. }
  72. static inline unsigned int table_size_to_number_of_entries(unsigned char size)
  73. {
  74. /*
  75. * size is the order of the table, 0-7
  76. * smallest table is 8K entries, so shift result by 13 to
  77. * multiply by 8K
  78. */
  79. return (1 << size) << 13;
  80. }
  81. static int tce_table_setparms(struct pci_dev *dev, struct iommu_table *tbl)
  82. {
  83. unsigned int bitmapsz;
  84. unsigned int tce_table_index;
  85. unsigned long bmppages;
  86. int ret;
  87. tbl->it_busno = dev->bus->number;
  88. /* set the tce table size - measured in entries */
  89. tbl->it_size = table_size_to_number_of_entries(specified_table_size);
  90. tce_table_index = bus_to_phb(tbl->it_busno);
  91. tbl->it_base = (unsigned long)tce_table_kva[tce_table_index];
  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. }