p2m.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <linux/bootmem.h>
  2. #include <linux/gfp.h>
  3. #include <linux/export.h>
  4. #include <linux/rwlock.h>
  5. #include <linux/slab.h>
  6. #include <linux/types.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/swiotlb.h>
  10. #include <xen/xen.h>
  11. #include <xen/interface/memory.h>
  12. #include <xen/swiotlb-xen.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/xen/page.h>
  15. #include <asm/xen/hypercall.h>
  16. #include <asm/xen/interface.h>
  17. struct xen_p2m_entry {
  18. unsigned long pfn;
  19. unsigned long mfn;
  20. unsigned long nr_pages;
  21. struct rb_node rbnode_mach;
  22. struct rb_node rbnode_phys;
  23. };
  24. rwlock_t p2m_lock;
  25. struct rb_root phys_to_mach = RB_ROOT;
  26. static struct rb_root mach_to_phys = RB_ROOT;
  27. static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new)
  28. {
  29. struct rb_node **link = &phys_to_mach.rb_node;
  30. struct rb_node *parent = NULL;
  31. struct xen_p2m_entry *entry;
  32. int rc = 0;
  33. while (*link) {
  34. parent = *link;
  35. entry = rb_entry(parent, struct xen_p2m_entry, rbnode_phys);
  36. if (new->mfn == entry->mfn)
  37. goto err_out;
  38. if (new->pfn == entry->pfn)
  39. goto err_out;
  40. if (new->pfn < entry->pfn)
  41. link = &(*link)->rb_left;
  42. else
  43. link = &(*link)->rb_right;
  44. }
  45. rb_link_node(&new->rbnode_phys, parent, link);
  46. rb_insert_color(&new->rbnode_phys, &phys_to_mach);
  47. goto out;
  48. err_out:
  49. rc = -EINVAL;
  50. pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
  51. __func__, &new->pfn, &new->mfn, &entry->pfn, &entry->mfn);
  52. out:
  53. return rc;
  54. }
  55. unsigned long __pfn_to_mfn(unsigned long pfn)
  56. {
  57. struct rb_node *n = phys_to_mach.rb_node;
  58. struct xen_p2m_entry *entry;
  59. unsigned long irqflags;
  60. read_lock_irqsave(&p2m_lock, irqflags);
  61. while (n) {
  62. entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
  63. if (entry->pfn <= pfn &&
  64. entry->pfn + entry->nr_pages > pfn) {
  65. read_unlock_irqrestore(&p2m_lock, irqflags);
  66. return entry->mfn + (pfn - entry->pfn);
  67. }
  68. if (pfn < entry->pfn)
  69. n = n->rb_left;
  70. else
  71. n = n->rb_right;
  72. }
  73. read_unlock_irqrestore(&p2m_lock, irqflags);
  74. return INVALID_P2M_ENTRY;
  75. }
  76. EXPORT_SYMBOL_GPL(__pfn_to_mfn);
  77. static int xen_add_mach_to_phys_entry(struct xen_p2m_entry *new)
  78. {
  79. struct rb_node **link = &mach_to_phys.rb_node;
  80. struct rb_node *parent = NULL;
  81. struct xen_p2m_entry *entry;
  82. int rc = 0;
  83. while (*link) {
  84. parent = *link;
  85. entry = rb_entry(parent, struct xen_p2m_entry, rbnode_mach);
  86. if (new->mfn == entry->mfn)
  87. goto err_out;
  88. if (new->pfn == entry->pfn)
  89. goto err_out;
  90. if (new->mfn < entry->mfn)
  91. link = &(*link)->rb_left;
  92. else
  93. link = &(*link)->rb_right;
  94. }
  95. rb_link_node(&new->rbnode_mach, parent, link);
  96. rb_insert_color(&new->rbnode_mach, &mach_to_phys);
  97. goto out;
  98. err_out:
  99. rc = -EINVAL;
  100. pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
  101. __func__, &new->pfn, &new->mfn, &entry->pfn, &entry->mfn);
  102. out:
  103. return rc;
  104. }
  105. unsigned long __mfn_to_pfn(unsigned long mfn)
  106. {
  107. struct rb_node *n = mach_to_phys.rb_node;
  108. struct xen_p2m_entry *entry;
  109. unsigned long irqflags;
  110. read_lock_irqsave(&p2m_lock, irqflags);
  111. while (n) {
  112. entry = rb_entry(n, struct xen_p2m_entry, rbnode_mach);
  113. if (entry->mfn <= mfn &&
  114. entry->mfn + entry->nr_pages > mfn) {
  115. read_unlock_irqrestore(&p2m_lock, irqflags);
  116. return entry->pfn + (mfn - entry->mfn);
  117. }
  118. if (mfn < entry->mfn)
  119. n = n->rb_left;
  120. else
  121. n = n->rb_right;
  122. }
  123. read_unlock_irqrestore(&p2m_lock, irqflags);
  124. return INVALID_P2M_ENTRY;
  125. }
  126. EXPORT_SYMBOL_GPL(__mfn_to_pfn);
  127. bool __set_phys_to_machine_multi(unsigned long pfn,
  128. unsigned long mfn, unsigned long nr_pages)
  129. {
  130. int rc;
  131. unsigned long irqflags;
  132. struct xen_p2m_entry *p2m_entry;
  133. struct rb_node *n = phys_to_mach.rb_node;
  134. if (mfn == INVALID_P2M_ENTRY) {
  135. write_lock_irqsave(&p2m_lock, irqflags);
  136. while (n) {
  137. p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
  138. if (p2m_entry->pfn <= pfn &&
  139. p2m_entry->pfn + p2m_entry->nr_pages > pfn) {
  140. rb_erase(&p2m_entry->rbnode_mach, &mach_to_phys);
  141. rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
  142. write_unlock_irqrestore(&p2m_lock, irqflags);
  143. kfree(p2m_entry);
  144. return true;
  145. }
  146. if (pfn < p2m_entry->pfn)
  147. n = n->rb_left;
  148. else
  149. n = n->rb_right;
  150. }
  151. write_unlock_irqrestore(&p2m_lock, irqflags);
  152. return true;
  153. }
  154. p2m_entry = kzalloc(sizeof(struct xen_p2m_entry), GFP_NOWAIT);
  155. if (!p2m_entry) {
  156. pr_warn("cannot allocate xen_p2m_entry\n");
  157. return false;
  158. }
  159. p2m_entry->pfn = pfn;
  160. p2m_entry->nr_pages = nr_pages;
  161. p2m_entry->mfn = mfn;
  162. write_lock_irqsave(&p2m_lock, irqflags);
  163. if ((rc = xen_add_phys_to_mach_entry(p2m_entry) < 0) ||
  164. (rc = xen_add_mach_to_phys_entry(p2m_entry) < 0)) {
  165. write_unlock_irqrestore(&p2m_lock, irqflags);
  166. return false;
  167. }
  168. write_unlock_irqrestore(&p2m_lock, irqflags);
  169. return true;
  170. }
  171. EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
  172. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  173. {
  174. return __set_phys_to_machine_multi(pfn, mfn, 1);
  175. }
  176. EXPORT_SYMBOL_GPL(__set_phys_to_machine);
  177. int p2m_init(void)
  178. {
  179. rwlock_init(&p2m_lock);
  180. return 0;
  181. }
  182. arch_initcall(p2m_init);