p2m.c 4.9 KB

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