tce_64.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /* flush a tce at 'tceaddr' to main memory */
  37. static inline void flush_tce(void* tceaddr)
  38. {
  39. /* a single tce can't cross a cache line */
  40. if (cpu_has_clflush)
  41. clflush(tceaddr);
  42. else
  43. wbinvd();
  44. }
  45. void tce_build(struct iommu_table *tbl, unsigned long index,
  46. unsigned int npages, unsigned long uaddr, int direction)
  47. {
  48. u64* tp;
  49. u64 t;
  50. u64 rpn;
  51. t = (1 << TCE_READ_SHIFT);
  52. if (direction != DMA_TO_DEVICE)
  53. t |= (1 << TCE_WRITE_SHIFT);
  54. tp = ((u64*)tbl->it_base) + index;
  55. while (npages--) {
  56. rpn = (virt_to_bus((void*)uaddr)) >> PAGE_SHIFT;
  57. t &= ~TCE_RPN_MASK;
  58. t |= (rpn << TCE_RPN_SHIFT);
  59. *tp = cpu_to_be64(t);
  60. flush_tce(tp);
  61. uaddr += PAGE_SIZE;
  62. tp++;
  63. }
  64. }
  65. void tce_free(struct iommu_table *tbl, long index, unsigned int npages)
  66. {
  67. u64* tp;
  68. tp = ((u64*)tbl->it_base) + index;
  69. while (npages--) {
  70. *tp = cpu_to_be64(0);
  71. flush_tce(tp);
  72. tp++;
  73. }
  74. }
  75. static inline unsigned int table_size_to_number_of_entries(unsigned char size)
  76. {
  77. /*
  78. * size is the order of the table, 0-7
  79. * smallest table is 8K entries, so shift result by 13 to
  80. * multiply by 8K
  81. */
  82. return (1 << size) << 13;
  83. }
  84. static int tce_table_setparms(struct pci_dev *dev, struct iommu_table *tbl)
  85. {
  86. unsigned int bitmapsz;
  87. unsigned long bmppages;
  88. int ret;
  89. tbl->it_busno = dev->bus->number;
  90. /* set the tce table size - measured in entries */
  91. tbl->it_size = table_size_to_number_of_entries(specified_table_size);
  92. /*
  93. * number of bytes needed for the bitmap size in number of
  94. * entries; we need one bit per entry
  95. */
  96. bitmapsz = tbl->it_size / BITS_PER_BYTE;
  97. bmppages = __get_free_pages(GFP_KERNEL, get_order(bitmapsz));
  98. if (!bmppages) {
  99. printk(KERN_ERR "Calgary: cannot allocate bitmap\n");
  100. ret = -ENOMEM;
  101. goto done;
  102. }
  103. tbl->it_map = (unsigned long*)bmppages;
  104. memset(tbl->it_map, 0, bitmapsz);
  105. tbl->it_hint = 0;
  106. spin_lock_init(&tbl->it_lock);
  107. return 0;
  108. done:
  109. return ret;
  110. }
  111. int __init build_tce_table(struct pci_dev *dev, void __iomem *bbar)
  112. {
  113. struct iommu_table *tbl;
  114. int ret;
  115. if (pci_iommu(dev->bus)) {
  116. printk(KERN_ERR "Calgary: dev %p has sysdata->iommu %p\n",
  117. dev, pci_iommu(dev->bus));
  118. BUG();
  119. }
  120. tbl = kzalloc(sizeof(struct iommu_table), GFP_KERNEL);
  121. if (!tbl) {
  122. printk(KERN_ERR "Calgary: error allocating iommu_table\n");
  123. ret = -ENOMEM;
  124. goto done;
  125. }
  126. ret = tce_table_setparms(dev, tbl);
  127. if (ret)
  128. goto free_tbl;
  129. tbl->bbar = bbar;
  130. set_pci_iommu(dev->bus, tbl);
  131. return 0;
  132. free_tbl:
  133. kfree(tbl);
  134. done:
  135. return ret;
  136. }
  137. void * __init alloc_tce_table(void)
  138. {
  139. unsigned int size;
  140. size = table_size_to_number_of_entries(specified_table_size);
  141. size *= TCE_ENTRY_SIZE;
  142. return __alloc_bootmem_low(size, size, 0);
  143. }
  144. void __init free_tce_table(void *tbl)
  145. {
  146. unsigned int size;
  147. if (!tbl)
  148. return;
  149. size = table_size_to_number_of_entries(specified_table_size);
  150. size *= TCE_ENTRY_SIZE;
  151. free_bootmem(__pa(tbl), size);
  152. }