tce_64.c 4.1 KB

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