p2m.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/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. if (map_ops[i].status)
  81. continue;
  82. set_phys_to_machine(map_ops[i].host_addr >> XEN_PAGE_SHIFT,
  83. map_ops[i].dev_bus_addr >> XEN_PAGE_SHIFT);
  84. }
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
  88. int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
  89. struct gnttab_unmap_grant_ref *kunmap_ops,
  90. struct page **pages, unsigned int count)
  91. {
  92. int i;
  93. for (i = 0; i < count; i++) {
  94. set_phys_to_machine(unmap_ops[i].host_addr >> XEN_PAGE_SHIFT,
  95. INVALID_P2M_ENTRY);
  96. }
  97. return 0;
  98. }
  99. EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
  100. bool __set_phys_to_machine_multi(unsigned long pfn,
  101. unsigned long mfn, unsigned long nr_pages)
  102. {
  103. int rc;
  104. unsigned long irqflags;
  105. struct xen_p2m_entry *p2m_entry;
  106. struct rb_node *n = phys_to_mach.rb_node;
  107. if (mfn == INVALID_P2M_ENTRY) {
  108. write_lock_irqsave(&p2m_lock, irqflags);
  109. while (n) {
  110. p2m_entry = rb_entry(n, struct xen_p2m_entry, rbnode_phys);
  111. if (p2m_entry->pfn <= pfn &&
  112. p2m_entry->pfn + p2m_entry->nr_pages > pfn) {
  113. rb_erase(&p2m_entry->rbnode_phys, &phys_to_mach);
  114. write_unlock_irqrestore(&p2m_lock, irqflags);
  115. kfree(p2m_entry);
  116. return true;
  117. }
  118. if (pfn < p2m_entry->pfn)
  119. n = n->rb_left;
  120. else
  121. n = n->rb_right;
  122. }
  123. write_unlock_irqrestore(&p2m_lock, irqflags);
  124. return true;
  125. }
  126. p2m_entry = kzalloc(sizeof(struct xen_p2m_entry), GFP_NOWAIT);
  127. if (!p2m_entry) {
  128. pr_warn("cannot allocate xen_p2m_entry\n");
  129. return false;
  130. }
  131. p2m_entry->pfn = pfn;
  132. p2m_entry->nr_pages = nr_pages;
  133. p2m_entry->mfn = mfn;
  134. write_lock_irqsave(&p2m_lock, irqflags);
  135. if ((rc = xen_add_phys_to_mach_entry(p2m_entry)) < 0) {
  136. write_unlock_irqrestore(&p2m_lock, irqflags);
  137. return false;
  138. }
  139. write_unlock_irqrestore(&p2m_lock, irqflags);
  140. return true;
  141. }
  142. EXPORT_SYMBOL_GPL(__set_phys_to_machine_multi);
  143. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  144. {
  145. return __set_phys_to_machine_multi(pfn, mfn, 1);
  146. }
  147. EXPORT_SYMBOL_GPL(__set_phys_to_machine);
  148. static int p2m_init(void)
  149. {
  150. rwlock_init(&p2m_lock);
  151. return 0;
  152. }
  153. arch_initcall(p2m_init);