p2m.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <linux/bootmem.h>
  2. #include <linux/gfp.h>
  3. #include <linux/export.h>
  4. #include <linux/spinlock.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/page.h>
  13. #include <xen/swiotlb-xen.h>
  14. #include <asm/cacheflush.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_phys;
  22. };
  23. static rwlock_t p2m_lock;
  24. struct rb_root phys_to_mach = RB_ROOT;
  25. EXPORT_SYMBOL_GPL(phys_to_mach);
  26. static int xen_add_phys_to_mach_entry(struct xen_p2m_entry *new)
  27. {
  28. struct rb_node **link = &phys_to_mach.rb_node;
  29. struct rb_node *parent = NULL;
  30. struct xen_p2m_entry *entry;
  31. int rc = 0;
  32. while (*link) {
  33. parent = *link;
  34. entry = rb_entry(parent, struct xen_p2m_entry, rbnode_phys);
  35. if (new->pfn == entry->pfn)
  36. goto err_out;
  37. if (new->pfn < entry->pfn)
  38. link = &(*link)->rb_left;
  39. else
  40. link = &(*link)->rb_right;
  41. }
  42. rb_link_node(&new->rbnode_phys, parent, link);
  43. rb_insert_color(&new->rbnode_phys, &phys_to_mach);
  44. goto out;
  45. err_out:
  46. rc = -EINVAL;
  47. pr_warn("%s: cannot add pfn=%pa -> mfn=%pa: pfn=%pa -> mfn=%pa already exists\n",
  48. __func__, &new->pfn, &new->mfn, &entry->pfn, &entry->mfn);
  49. out:
  50. return rc;
  51. }
  52. unsigned long __pfn_to_mfn(unsigned long pfn)
  53. {
  54. struct rb_node *n = phys_to_mach.rb_node;
  55. struct xen_p2m_entry *entry;
  56. unsigned long irqflags;
  57. read_lock_irqsave(&p2m_lock, irqflags);
  58. while (n) {
  59. entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
  60. if (entry->pfn <= pfn &&
  61. entry->pfn + entry->nr_pages > pfn) {
  62. read_unlock_irqrestore(&p2m_lock, irqflags);
  63. return entry->mfn + (pfn - entry->pfn);
  64. }
  65. if (pfn < entry->pfn)
  66. n = n->rb_left;
  67. else
  68. n = n->rb_right;
  69. }
  70. read_unlock_irqrestore(&p2m_lock, irqflags);
  71. return INVALID_P2M_ENTRY;
  72. }
  73. EXPORT_SYMBOL_GPL(__pfn_to_mfn);
  74. int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
  75. struct gnttab_map_grant_ref *kmap_ops,
  76. struct page **pages, unsigned int count)
  77. {
  78. int i;
  79. for (i = 0; i < count; i++) {
  80. struct gnttab_unmap_grant_ref unmap;
  81. int rc;
  82. if (map_ops[i].status)
  83. continue;
  84. if (likely(set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
  85. map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT)))
  86. continue;
  87. /*
  88. * Signal an error for this slot. This in turn requires
  89. * immediate unmapping.
  90. */
  91. map_ops[i].status = GNTST_general_error;
  92. unmap.host_addr = map_ops[i].host_addr,
  93. unmap.handle = map_ops[i].handle;
  94. map_ops[i].handle = ~0;
  95. if (map_ops[i].flags & GNTMAP_device_map)
  96. unmap.dev_bus_addr = map_ops[i].dev_bus_addr;
  97. else
  98. unmap.dev_bus_addr = 0;
  99. /*
  100. * Pre-populate the status field, to be recognizable in
  101. * the log message below.
  102. */
  103. unmap.status = 1;
  104. rc = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
  105. &unmap, 1);
  106. if (rc || unmap.status != GNTST_okay)
  107. pr_err_once("gnttab unmap failed: rc=%d st=%d\n",
  108. rc, unmap.status);
  109. }
  110. return 0;
  111. }
  112. EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
  113. int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
  114. struct gnttab_unmap_grant_ref *kunmap_ops,
  115. struct page **pages, unsigned int count)
  116. {
  117. int i;
  118. for (i = 0; i < count; i++) {
  119. set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
  120. INVALID_P2M_ENTRY);
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
  125. bool __set_phys_to_machine_multi(unsigned long pfn,
  126. unsigned long mfn, unsigned long nr_pages)
  127. {
  128. int rc;
  129. unsigned long irqflags;
  130. struct xen_p2m_entry *p2m_entry;
  131. struct rb_node *n = phys_to_mach.rb_node;
  132. if (mfn == INVALID_P2M_ENTRY) {
  133. write_lock_irqsave(&p2m_lock, irqflags);
  134. while (n) {
  135. p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
  136. if (p2m_entry->pfn <= pfn &&
  137. p2m_entry->pfn + p2m_entry->nr_pages > pfn) {
  138. rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
  139. write_unlock_irqrestore(&p2m_lock, irqflags);
  140. kfree(p2m_entry);
  141. return true;
  142. }
  143. if (pfn < p2m_entry->pfn)
  144. n = n->rb_left;
  145. else
  146. n = n->rb_right;
  147. }
  148. write_unlock_irqrestore(&p2m_lock, irqflags);
  149. return true;
  150. }
  151. p2m_entry = kzalloc(sizeof(*p2m_entry), GFP_NOWAIT);
  152. if (!p2m_entry)
  153. return false;
  154. p2m_entry->pfn = pfn;
  155. p2m_entry->nr_pages = nr_pages;
  156. p2m_entry->mfn = mfn;
  157. write_lock_irqsave(&p2m_lock, irqflags);
  158. rc = xen_add_phys_to_mach_entry(p2m_entry);
  159. if (rc < 0) {
  160. write_unlock_irqrestore(&p2m_lock, irqflags);
  161. return false;
  162. }
  163. write_unlock_irqrestore(&p2m_lock, irqflags);
  164. return true;
  165. }
  166. EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
  167. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  168. {
  169. return __set_phys_to_machine_multi(pfn, mfn, 1);
  170. }
  171. EXPORT_SYMBOL_GPL(__set_phys_to_machine);
  172. static int p2m_init(void)
  173. {
  174. rwlock_init(&p2m_lock);
  175. return 0;
  176. }
  177. arch_initcall(p2m_init);