1 /******************************************************************************
4 * Granting foreign access to our memory reservation.
6 * Copyright (c) 2005-2006, Christopher Clark
7 * Copyright (c) 2004-2005, K A Fraser
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
36 #include <linux/memblock.h>
37 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/vmalloc.h>
41 #include <linux/uaccess.h>
43 #include <linux/delay.h>
44 #include <linux/hardirq.h>
45 #include <linux/workqueue.h>
46 #include <linux/ratelimit.h>
47 #include <linux/moduleparam.h>
48 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
49 #include <linux/dma-mapping.h>
53 #include <xen/interface/xen.h>
55 #include <xen/grant_table.h>
56 #include <xen/interface/memory.h>
57 #include <xen/hvc-console.h>
58 #include <xen/swiotlb-xen.h>
59 #include <xen/balloon.h>
61 #include <asm/xen/cpuid.h>
63 #include <xen/mem-reservation.h>
64 #include <asm/xen/hypercall.h>
65 #include <asm/xen/interface.h>
67 #include <asm/sync_bitops.h>
69 #define GNTTAB_LIST_END 0xffffffff
71 static grant_ref_t **gnttab_list;
72 static unsigned int nr_grant_frames;
73 static int gnttab_free_count;
74 static grant_ref_t gnttab_free_head;
75 static DEFINE_SPINLOCK(gnttab_list_lock);
76 struct grant_frames xen_auto_xlat_grant_frames;
77 static unsigned int xen_gnttab_version;
78 module_param_named(version, xen_gnttab_version, uint, 0);
81 struct grant_entry_v1 *v1;
82 union grant_entry_v2 *v2;
86 /*This is a structure of function pointers for grant table*/
89 * Version of the grant interface.
93 * Grant refs per grant frame.
95 unsigned int grefs_per_grant_frame;
97 * Mapping a list of frames for storing grant entries. Frames parameter
98 * is used to store grant table address when grant table being setup,
99 * nr_gframes is the number of frames to map grant table. Returning
100 * GNTST_okay means success and negative value means failure.
102 int (*map_frames)(xen_pfn_t *frames, unsigned int nr_gframes);
104 * Release a list of frames which are mapped in map_frames for grant
107 void (*unmap_frames)(void);
109 * Introducing a valid entry into the grant table, granting the frame of
110 * this grant entry to domain for accessing. Ref
111 * parameter is reference of this introduced grant entry, domid is id of
112 * granted domain, frame is the page frame to be granted, and flags is
113 * status of the grant entry to be updated.
115 void (*update_entry)(grant_ref_t ref, domid_t domid,
116 unsigned long frame, unsigned flags);
118 * Stop granting a grant entry to domain for accessing. Ref parameter is
119 * reference of a grant entry whose grant access will be stopped.
120 * If the grant entry is currently mapped for reading or writing, just
121 * return failure(==0) directly and don't tear down the grant access.
122 * Otherwise, stop grant access for this entry and return success(==1).
124 int (*end_foreign_access_ref)(grant_ref_t ref);
126 * Read the frame number related to a given grant reference.
128 unsigned long (*read_frame)(grant_ref_t ref);
131 struct unmap_refs_callback_data {
132 struct completion completion;
136 static const struct gnttab_ops *gnttab_interface;
138 /* This reflects status of grant entries, so act as a global value. */
139 static grant_status_t *grstatus;
141 static struct gnttab_free_callback *gnttab_free_callback_list;
143 static int gnttab_expand(unsigned int req_entries);
145 #define RPP (PAGE_SIZE / sizeof(grant_ref_t))
146 #define SPP (PAGE_SIZE / sizeof(grant_status_t))
148 static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
150 return &gnttab_list[(entry) / RPP][(entry) % RPP];
152 /* This can be used as an l-value */
153 #define gnttab_entry(entry) (*__gnttab_entry(entry))
155 static int get_free_entries(unsigned count)
161 spin_lock_irqsave(&gnttab_list_lock, flags);
163 if ((gnttab_free_count < count) &&
164 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
165 spin_unlock_irqrestore(&gnttab_list_lock, flags);
169 ref = head = gnttab_free_head;
170 gnttab_free_count -= count;
172 head = gnttab_entry(head);
173 gnttab_free_head = gnttab_entry(head);
174 gnttab_entry(head) = GNTTAB_LIST_END;
176 spin_unlock_irqrestore(&gnttab_list_lock, flags);
181 static void do_free_callbacks(void)
183 struct gnttab_free_callback *callback, *next;
185 callback = gnttab_free_callback_list;
186 gnttab_free_callback_list = NULL;
188 while (callback != NULL) {
189 next = callback->next;
190 if (gnttab_free_count >= callback->count) {
191 callback->next = NULL;
192 callback->fn(callback->arg);
194 callback->next = gnttab_free_callback_list;
195 gnttab_free_callback_list = callback;
201 static inline void check_free_callbacks(void)
203 if (unlikely(gnttab_free_callback_list))
207 static void put_free_entry(grant_ref_t ref)
211 if (unlikely(ref < GNTTAB_NR_RESERVED_ENTRIES))
214 spin_lock_irqsave(&gnttab_list_lock, flags);
215 gnttab_entry(ref) = gnttab_free_head;
216 gnttab_free_head = ref;
218 check_free_callbacks();
219 spin_unlock_irqrestore(&gnttab_list_lock, flags);
223 * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2.
224 * Introducing a valid entry into the grant table:
225 * 1. Write ent->domid.
226 * 2. Write ent->frame: Frame to which access is permitted.
227 * 3. Write memory barrier (WMB).
228 * 4. Write ent->flags, inc. valid type.
230 static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
231 unsigned long frame, unsigned flags)
233 gnttab_shared.v1[ref].domid = domid;
234 gnttab_shared.v1[ref].frame = frame;
236 gnttab_shared.v1[ref].flags = flags;
239 static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid,
240 unsigned long frame, unsigned int flags)
242 gnttab_shared.v2[ref].hdr.domid = domid;
243 gnttab_shared.v2[ref].full_page.frame = frame;
244 wmb(); /* Hypervisor concurrent accesses. */
245 gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags;
249 * Public grant-issuing interface functions
251 void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
252 unsigned long frame, int readonly)
254 gnttab_interface->update_entry(ref, domid, frame,
255 GTF_permit_access | (readonly ? GTF_readonly : 0));
257 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
259 int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
264 ref = get_free_entries(1);
265 if (unlikely(ref < 0))
268 gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
272 EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
274 static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref)
279 pflags = &gnttab_shared.v1[ref].flags;
283 if (flags & (GTF_reading|GTF_writing))
285 } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
290 static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref)
292 gnttab_shared.v2[ref].hdr.flags = 0;
293 mb(); /* Concurrent access by hypervisor. */
294 if (grstatus[ref] & (GTF_reading|GTF_writing)) {
298 * The read of grstatus needs to have acquire semantics.
299 * On x86, reads already have that, and we just need to
300 * protect against compiler reorderings.
301 * On other architectures we may need a full barrier.
313 static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref)
315 return gnttab_interface->end_foreign_access_ref(ref);
318 int gnttab_end_foreign_access_ref(grant_ref_t ref)
320 if (_gnttab_end_foreign_access_ref(ref))
322 pr_warn("WARNING: g.e. %#x still in use!\n", ref);
325 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
327 static unsigned long gnttab_read_frame_v1(grant_ref_t ref)
329 return gnttab_shared.v1[ref].frame;
332 static unsigned long gnttab_read_frame_v2(grant_ref_t ref)
334 return gnttab_shared.v2[ref].full_page.frame;
337 struct deferred_entry {
338 struct list_head list;
343 static LIST_HEAD(deferred_list);
344 static void gnttab_handle_deferred(struct timer_list *);
345 static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred);
347 static void gnttab_handle_deferred(struct timer_list *unused)
349 unsigned int nr = 10;
350 struct deferred_entry *first = NULL;
353 spin_lock_irqsave(&gnttab_list_lock, flags);
355 struct deferred_entry *entry
356 = list_first_entry(&deferred_list,
357 struct deferred_entry, list);
361 list_del(&entry->list);
362 spin_unlock_irqrestore(&gnttab_list_lock, flags);
363 if (_gnttab_end_foreign_access_ref(entry->ref)) {
364 put_free_entry(entry->ref);
365 pr_debug("freeing g.e. %#x (pfn %#lx)\n",
366 entry->ref, page_to_pfn(entry->page));
367 put_page(entry->page);
371 if (!--entry->warn_delay)
372 pr_info("g.e. %#x still pending\n", entry->ref);
376 spin_lock_irqsave(&gnttab_list_lock, flags);
378 list_add_tail(&entry->list, &deferred_list);
379 else if (list_empty(&deferred_list))
382 if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) {
383 deferred_timer.expires = jiffies + HZ;
384 add_timer(&deferred_timer);
386 spin_unlock_irqrestore(&gnttab_list_lock, flags);
389 static void gnttab_add_deferred(grant_ref_t ref, struct page *page)
391 struct deferred_entry *entry;
392 gfp_t gfp = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
393 const char *what = KERN_WARNING "leaking";
395 entry = kmalloc(sizeof(*entry), gfp);
397 unsigned long gfn = gnttab_interface->read_frame(ref);
399 page = pfn_to_page(gfn_to_pfn(gfn));
408 entry->warn_delay = 60;
409 spin_lock_irqsave(&gnttab_list_lock, flags);
410 list_add_tail(&entry->list, &deferred_list);
411 if (!timer_pending(&deferred_timer)) {
412 deferred_timer.expires = jiffies + HZ;
413 add_timer(&deferred_timer);
415 spin_unlock_irqrestore(&gnttab_list_lock, flags);
416 what = KERN_DEBUG "deferring";
418 printk("%s g.e. %#x (pfn %#lx)\n",
419 what, ref, page ? page_to_pfn(page) : -1);
422 int gnttab_try_end_foreign_access(grant_ref_t ref)
424 int ret = _gnttab_end_foreign_access_ref(ref);
431 EXPORT_SYMBOL_GPL(gnttab_try_end_foreign_access);
433 void gnttab_end_foreign_access(grant_ref_t ref, struct page *page)
435 if (gnttab_try_end_foreign_access(ref)) {
439 gnttab_add_deferred(ref, page);
441 EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
443 void gnttab_free_grant_reference(grant_ref_t ref)
447 EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
449 void gnttab_free_grant_references(grant_ref_t head)
454 if (head == GNTTAB_LIST_END)
456 spin_lock_irqsave(&gnttab_list_lock, flags);
458 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
459 ref = gnttab_entry(ref);
462 gnttab_entry(ref) = gnttab_free_head;
463 gnttab_free_head = head;
464 gnttab_free_count += count;
465 check_free_callbacks();
466 spin_unlock_irqrestore(&gnttab_list_lock, flags);
468 EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
470 int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
472 int h = get_free_entries(count);
481 EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
483 int gnttab_empty_grant_references(const grant_ref_t *private_head)
485 return (*private_head == GNTTAB_LIST_END);
487 EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
489 int gnttab_claim_grant_reference(grant_ref_t *private_head)
491 grant_ref_t g = *private_head;
492 if (unlikely(g == GNTTAB_LIST_END))
494 *private_head = gnttab_entry(g);
497 EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
499 void gnttab_release_grant_reference(grant_ref_t *private_head,
502 gnttab_entry(release) = *private_head;
503 *private_head = release;
505 EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
507 void gnttab_request_free_callback(struct gnttab_free_callback *callback,
508 void (*fn)(void *), void *arg, u16 count)
511 struct gnttab_free_callback *cb;
513 spin_lock_irqsave(&gnttab_list_lock, flags);
515 /* Check if the callback is already on the list */
516 cb = gnttab_free_callback_list;
525 callback->count = count;
526 callback->next = gnttab_free_callback_list;
527 gnttab_free_callback_list = callback;
528 check_free_callbacks();
530 spin_unlock_irqrestore(&gnttab_list_lock, flags);
532 EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
534 void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
536 struct gnttab_free_callback **pcb;
539 spin_lock_irqsave(&gnttab_list_lock, flags);
540 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
541 if (*pcb == callback) {
542 *pcb = callback->next;
546 spin_unlock_irqrestore(&gnttab_list_lock, flags);
548 EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
550 static unsigned int gnttab_frames(unsigned int frames, unsigned int align)
552 return (frames * gnttab_interface->grefs_per_grant_frame + align - 1) /
556 static int grow_gnttab_list(unsigned int more_frames)
558 unsigned int new_nr_grant_frames, extra_entries, i;
559 unsigned int nr_glist_frames, new_nr_glist_frames;
560 unsigned int grefs_per_frame;
562 grefs_per_frame = gnttab_interface->grefs_per_grant_frame;
564 new_nr_grant_frames = nr_grant_frames + more_frames;
565 extra_entries = more_frames * grefs_per_frame;
567 nr_glist_frames = gnttab_frames(nr_grant_frames, RPP);
568 new_nr_glist_frames = gnttab_frames(new_nr_grant_frames, RPP);
569 for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
570 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
576 for (i = grefs_per_frame * nr_grant_frames;
577 i < grefs_per_frame * new_nr_grant_frames - 1; i++)
578 gnttab_entry(i) = i + 1;
580 gnttab_entry(i) = gnttab_free_head;
581 gnttab_free_head = grefs_per_frame * nr_grant_frames;
582 gnttab_free_count += extra_entries;
584 nr_grant_frames = new_nr_grant_frames;
586 check_free_callbacks();
591 while (i-- > nr_glist_frames)
592 free_page((unsigned long) gnttab_list[i]);
596 static unsigned int __max_nr_grant_frames(void)
598 struct gnttab_query_size query;
601 query.dom = DOMID_SELF;
603 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
604 if ((rc < 0) || (query.status != GNTST_okay))
605 return 4; /* Legacy max supported number of frames */
607 return query.max_nr_frames;
610 unsigned int gnttab_max_grant_frames(void)
612 unsigned int xen_max = __max_nr_grant_frames();
613 static unsigned int boot_max_nr_grant_frames;
615 /* First time, initialize it properly. */
616 if (!boot_max_nr_grant_frames)
617 boot_max_nr_grant_frames = __max_nr_grant_frames();
619 if (xen_max > boot_max_nr_grant_frames)
620 return boot_max_nr_grant_frames;
623 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
625 int gnttab_setup_auto_xlat_frames(phys_addr_t addr)
628 unsigned int max_nr_gframes = __max_nr_grant_frames();
632 if (xen_auto_xlat_grant_frames.count)
635 vaddr = memremap(addr, XEN_PAGE_SIZE * max_nr_gframes, MEMREMAP_WB);
637 pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n",
641 pfn = kcalloc(max_nr_gframes, sizeof(pfn[0]), GFP_KERNEL);
646 for (i = 0; i < max_nr_gframes; i++)
647 pfn[i] = XEN_PFN_DOWN(addr) + i;
649 xen_auto_xlat_grant_frames.vaddr = vaddr;
650 xen_auto_xlat_grant_frames.pfn = pfn;
651 xen_auto_xlat_grant_frames.count = max_nr_gframes;
655 EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames);
657 void gnttab_free_auto_xlat_frames(void)
659 if (!xen_auto_xlat_grant_frames.count)
661 kfree(xen_auto_xlat_grant_frames.pfn);
662 memunmap(xen_auto_xlat_grant_frames.vaddr);
664 xen_auto_xlat_grant_frames.pfn = NULL;
665 xen_auto_xlat_grant_frames.count = 0;
666 xen_auto_xlat_grant_frames.vaddr = NULL;
668 EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames);
670 int gnttab_pages_set_private(int nr_pages, struct page **pages)
674 for (i = 0; i < nr_pages; i++) {
675 #if BITS_PER_LONG < 64
676 struct xen_page_foreign *foreign;
678 foreign = kzalloc(sizeof(*foreign), GFP_KERNEL);
682 set_page_private(pages[i], (unsigned long)foreign);
684 SetPagePrivate(pages[i]);
689 EXPORT_SYMBOL_GPL(gnttab_pages_set_private);
692 * gnttab_alloc_pages - alloc pages suitable for grant mapping into
693 * @nr_pages: number of pages to alloc
694 * @pages: returns the pages
696 int gnttab_alloc_pages(int nr_pages, struct page **pages)
700 ret = xen_alloc_unpopulated_pages(nr_pages, pages);
704 ret = gnttab_pages_set_private(nr_pages, pages);
706 gnttab_free_pages(nr_pages, pages);
710 EXPORT_SYMBOL_GPL(gnttab_alloc_pages);
712 #ifdef CONFIG_XEN_UNPOPULATED_ALLOC
713 static inline void cache_init(struct gnttab_page_cache *cache)
718 static inline bool cache_empty(struct gnttab_page_cache *cache)
720 return !cache->pages;
723 static inline struct page *cache_deq(struct gnttab_page_cache *cache)
728 cache->pages = page->zone_device_data;
733 static inline void cache_enq(struct gnttab_page_cache *cache, struct page *page)
735 page->zone_device_data = cache->pages;
739 static inline void cache_init(struct gnttab_page_cache *cache)
741 INIT_LIST_HEAD(&cache->pages);
744 static inline bool cache_empty(struct gnttab_page_cache *cache)
746 return list_empty(&cache->pages);
749 static inline struct page *cache_deq(struct gnttab_page_cache *cache)
753 page = list_first_entry(&cache->pages, struct page, lru);
754 list_del(&page->lru);
759 static inline void cache_enq(struct gnttab_page_cache *cache, struct page *page)
761 list_add(&page->lru, &cache->pages);
765 void gnttab_page_cache_init(struct gnttab_page_cache *cache)
767 spin_lock_init(&cache->lock);
769 cache->num_pages = 0;
771 EXPORT_SYMBOL_GPL(gnttab_page_cache_init);
773 int gnttab_page_cache_get(struct gnttab_page_cache *cache, struct page **page)
777 spin_lock_irqsave(&cache->lock, flags);
779 if (cache_empty(cache)) {
780 spin_unlock_irqrestore(&cache->lock, flags);
781 return gnttab_alloc_pages(1, page);
784 page[0] = cache_deq(cache);
787 spin_unlock_irqrestore(&cache->lock, flags);
791 EXPORT_SYMBOL_GPL(gnttab_page_cache_get);
793 void gnttab_page_cache_put(struct gnttab_page_cache *cache, struct page **page,
799 spin_lock_irqsave(&cache->lock, flags);
801 for (i = 0; i < num; i++)
802 cache_enq(cache, page[i]);
803 cache->num_pages += num;
805 spin_unlock_irqrestore(&cache->lock, flags);
807 EXPORT_SYMBOL_GPL(gnttab_page_cache_put);
809 void gnttab_page_cache_shrink(struct gnttab_page_cache *cache, unsigned int num)
811 struct page *page[10];
815 spin_lock_irqsave(&cache->lock, flags);
817 while (cache->num_pages > num) {
818 page[i] = cache_deq(cache);
820 if (++i == ARRAY_SIZE(page)) {
821 spin_unlock_irqrestore(&cache->lock, flags);
822 gnttab_free_pages(i, page);
824 spin_lock_irqsave(&cache->lock, flags);
828 spin_unlock_irqrestore(&cache->lock, flags);
831 gnttab_free_pages(i, page);
833 EXPORT_SYMBOL_GPL(gnttab_page_cache_shrink);
835 void gnttab_pages_clear_private(int nr_pages, struct page **pages)
839 for (i = 0; i < nr_pages; i++) {
840 if (PagePrivate(pages[i])) {
841 #if BITS_PER_LONG < 64
842 kfree((void *)page_private(pages[i]));
844 ClearPagePrivate(pages[i]);
848 EXPORT_SYMBOL_GPL(gnttab_pages_clear_private);
851 * gnttab_free_pages - free pages allocated by gnttab_alloc_pages()
852 * @nr_pages; number of pages to free
855 void gnttab_free_pages(int nr_pages, struct page **pages)
857 gnttab_pages_clear_private(nr_pages, pages);
858 xen_free_unpopulated_pages(nr_pages, pages);
860 EXPORT_SYMBOL_GPL(gnttab_free_pages);
862 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
864 * gnttab_dma_alloc_pages - alloc DMAable pages suitable for grant mapping into
865 * @args: arguments to the function
867 int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_args *args)
869 unsigned long pfn, start_pfn;
873 size = args->nr_pages << PAGE_SHIFT;
875 args->vaddr = dma_alloc_coherent(args->dev, size,
877 GFP_KERNEL | __GFP_NOWARN);
879 args->vaddr = dma_alloc_wc(args->dev, size,
881 GFP_KERNEL | __GFP_NOWARN);
883 pr_debug("Failed to allocate DMA buffer of size %zu\n", size);
887 start_pfn = __phys_to_pfn(args->dev_bus_addr);
888 for (pfn = start_pfn, i = 0; pfn < start_pfn + args->nr_pages;
890 struct page *page = pfn_to_page(pfn);
892 args->pages[i] = page;
893 args->frames[i] = xen_page_to_gfn(page);
894 xenmem_reservation_scrub_page(page);
897 xenmem_reservation_va_mapping_reset(args->nr_pages, args->pages);
899 ret = xenmem_reservation_decrease(args->nr_pages, args->frames);
900 if (ret != args->nr_pages) {
901 pr_debug("Failed to decrease reservation for DMA buffer\n");
906 ret = gnttab_pages_set_private(args->nr_pages, args->pages);
913 gnttab_dma_free_pages(args);
916 EXPORT_SYMBOL_GPL(gnttab_dma_alloc_pages);
919 * gnttab_dma_free_pages - free DMAable pages
920 * @args: arguments to the function
922 int gnttab_dma_free_pages(struct gnttab_dma_alloc_args *args)
927 gnttab_pages_clear_private(args->nr_pages, args->pages);
929 for (i = 0; i < args->nr_pages; i++)
930 args->frames[i] = page_to_xen_pfn(args->pages[i]);
932 ret = xenmem_reservation_increase(args->nr_pages, args->frames);
933 if (ret != args->nr_pages) {
934 pr_debug("Failed to increase reservation for DMA buffer\n");
940 xenmem_reservation_va_mapping_update(args->nr_pages, args->pages,
943 size = args->nr_pages << PAGE_SHIFT;
945 dma_free_coherent(args->dev, size,
946 args->vaddr, args->dev_bus_addr);
948 dma_free_wc(args->dev, size,
949 args->vaddr, args->dev_bus_addr);
952 EXPORT_SYMBOL_GPL(gnttab_dma_free_pages);
955 /* Handling of paged out grant targets (GNTST_eagain) */
956 #define MAX_DELAY 256
958 gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
964 BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
965 if (*status == GNTST_eagain)
967 } while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
969 if (delay >= MAX_DELAY) {
970 pr_err("%s: %s eagain grant\n", func, current->comm);
971 *status = GNTST_bad_page;
975 void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count)
977 struct gnttab_map_grant_ref *op;
979 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count))
981 for (op = batch; op < batch + count; op++)
982 if (op->status == GNTST_eagain)
983 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op,
984 &op->status, __func__);
986 EXPORT_SYMBOL_GPL(gnttab_batch_map);
988 void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count)
990 struct gnttab_copy *op;
992 if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count))
994 for (op = batch; op < batch + count; op++)
995 if (op->status == GNTST_eagain)
996 gnttab_retry_eagain_gop(GNTTABOP_copy, op,
997 &op->status, __func__);
999 EXPORT_SYMBOL_GPL(gnttab_batch_copy);
1001 void gnttab_foreach_grant_in_range(struct page *page,
1002 unsigned int offset,
1007 unsigned int goffset;
1009 unsigned long xen_pfn;
1011 len = min_t(unsigned int, PAGE_SIZE - offset, len);
1012 goffset = xen_offset_in_page(offset);
1014 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(offset);
1017 glen = min_t(unsigned int, XEN_PAGE_SIZE - goffset, len);
1018 fn(pfn_to_gfn(xen_pfn), goffset, glen, data);
1025 EXPORT_SYMBOL_GPL(gnttab_foreach_grant_in_range);
1027 void gnttab_foreach_grant(struct page **pages,
1028 unsigned int nr_grefs,
1032 unsigned int goffset = 0;
1033 unsigned long xen_pfn = 0;
1036 for (i = 0; i < nr_grefs; i++) {
1037 if ((i % XEN_PFN_PER_PAGE) == 0) {
1038 xen_pfn = page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
1042 fn(pfn_to_gfn(xen_pfn), goffset, XEN_PAGE_SIZE, data);
1044 goffset += XEN_PAGE_SIZE;
1049 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
1050 struct gnttab_map_grant_ref *kmap_ops,
1051 struct page **pages, unsigned int count)
1055 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
1059 for (i = 0; i < count; i++) {
1060 switch (map_ops[i].status) {
1063 struct xen_page_foreign *foreign;
1065 SetPageForeign(pages[i]);
1066 foreign = xen_page_foreign(pages[i]);
1067 foreign->domid = map_ops[i].dom;
1068 foreign->gref = map_ops[i].ref;
1072 case GNTST_no_device_space:
1073 pr_warn_ratelimited("maptrack limit reached, can't map all guest pages\n");
1077 /* Retry eagain maps */
1078 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref,
1080 &map_ops[i].status, __func__);
1081 /* Test status in next loop iteration. */
1090 return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count);
1092 EXPORT_SYMBOL_GPL(gnttab_map_refs);
1094 int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
1095 struct gnttab_unmap_grant_ref *kunmap_ops,
1096 struct page **pages, unsigned int count)
1101 ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
1105 for (i = 0; i < count; i++)
1106 ClearPageForeign(pages[i]);
1108 return clear_foreign_p2m_mapping(unmap_ops, kunmap_ops, pages, count);
1110 EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
1112 #define GNTTAB_UNMAP_REFS_DELAY 5
1114 static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
1116 static void gnttab_unmap_work(struct work_struct *work)
1118 struct gntab_unmap_queue_data
1119 *unmap_data = container_of(work,
1120 struct gntab_unmap_queue_data,
1122 if (unmap_data->age != UINT_MAX)
1124 __gnttab_unmap_refs_async(unmap_data);
1127 static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
1132 for (pc = 0; pc < item->count; pc++) {
1133 if (page_count(item->pages[pc]) > 1) {
1134 unsigned long delay = GNTTAB_UNMAP_REFS_DELAY * (item->age + 1);
1135 schedule_delayed_work(&item->gnttab_work,
1136 msecs_to_jiffies(delay));
1141 ret = gnttab_unmap_refs(item->unmap_ops, item->kunmap_ops,
1142 item->pages, item->count);
1143 item->done(ret, item);
1146 void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
1148 INIT_DELAYED_WORK(&item->gnttab_work, gnttab_unmap_work);
1151 __gnttab_unmap_refs_async(item);
1153 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
1155 static void unmap_refs_callback(int result,
1156 struct gntab_unmap_queue_data *data)
1158 struct unmap_refs_callback_data *d = data->data;
1161 complete(&d->completion);
1164 int gnttab_unmap_refs_sync(struct gntab_unmap_queue_data *item)
1166 struct unmap_refs_callback_data data;
1168 init_completion(&data.completion);
1170 item->done = &unmap_refs_callback;
1171 gnttab_unmap_refs_async(item);
1172 wait_for_completion(&data.completion);
1176 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_sync);
1178 static unsigned int nr_status_frames(unsigned int nr_grant_frames)
1180 return gnttab_frames(nr_grant_frames, SPP);
1183 static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
1187 rc = arch_gnttab_map_shared(frames, nr_gframes,
1188 gnttab_max_grant_frames(),
1189 &gnttab_shared.addr);
1195 static void gnttab_unmap_frames_v1(void)
1197 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
1200 static int gnttab_map_frames_v2(xen_pfn_t *frames, unsigned int nr_gframes)
1203 unsigned int nr_sframes;
1204 struct gnttab_get_status_frames getframes;
1207 nr_sframes = nr_status_frames(nr_gframes);
1209 /* No need for kzalloc as it is initialized in following hypercall
1210 * GNTTABOP_get_status_frames.
1212 sframes = kmalloc_array(nr_sframes, sizeof(uint64_t), GFP_ATOMIC);
1216 getframes.dom = DOMID_SELF;
1217 getframes.nr_frames = nr_sframes;
1218 set_xen_guest_handle(getframes.frame_list, sframes);
1220 rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames,
1222 if (rc == -ENOSYS) {
1227 BUG_ON(rc || getframes.status);
1229 rc = arch_gnttab_map_status(sframes, nr_sframes,
1230 nr_status_frames(gnttab_max_grant_frames()),
1235 rc = arch_gnttab_map_shared(frames, nr_gframes,
1236 gnttab_max_grant_frames(),
1237 &gnttab_shared.addr);
1243 static void gnttab_unmap_frames_v2(void)
1245 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
1246 arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames));
1249 static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
1251 struct gnttab_setup_table setup;
1253 unsigned int nr_gframes = end_idx + 1;
1256 if (xen_feature(XENFEAT_auto_translated_physmap)) {
1257 struct xen_add_to_physmap xatp;
1258 unsigned int i = end_idx;
1260 BUG_ON(xen_auto_xlat_grant_frames.count < nr_gframes);
1262 * Loop backwards, so that the first hypercall has the largest
1263 * index, ensuring that the table will grow only once.
1266 xatp.domid = DOMID_SELF;
1268 xatp.space = XENMAPSPACE_grant_table;
1269 xatp.gpfn = xen_auto_xlat_grant_frames.pfn[i];
1270 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
1272 pr_warn("grant table add_to_physmap failed, err=%d\n",
1276 } while (i-- > start_idx);
1281 /* No need for kzalloc as it is initialized in following hypercall
1282 * GNTTABOP_setup_table.
1284 frames = kmalloc_array(nr_gframes, sizeof(unsigned long), GFP_ATOMIC);
1288 setup.dom = DOMID_SELF;
1289 setup.nr_frames = nr_gframes;
1290 set_xen_guest_handle(setup.frame_list, frames);
1292 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
1293 if (rc == -ENOSYS) {
1298 BUG_ON(rc || setup.status);
1300 rc = gnttab_interface->map_frames(frames, nr_gframes);
1307 static const struct gnttab_ops gnttab_v1_ops = {
1309 .grefs_per_grant_frame = XEN_PAGE_SIZE /
1310 sizeof(struct grant_entry_v1),
1311 .map_frames = gnttab_map_frames_v1,
1312 .unmap_frames = gnttab_unmap_frames_v1,
1313 .update_entry = gnttab_update_entry_v1,
1314 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
1315 .read_frame = gnttab_read_frame_v1,
1318 static const struct gnttab_ops gnttab_v2_ops = {
1320 .grefs_per_grant_frame = XEN_PAGE_SIZE /
1321 sizeof(union grant_entry_v2),
1322 .map_frames = gnttab_map_frames_v2,
1323 .unmap_frames = gnttab_unmap_frames_v2,
1324 .update_entry = gnttab_update_entry_v2,
1325 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2,
1326 .read_frame = gnttab_read_frame_v2,
1329 static bool gnttab_need_v2(void)
1332 uint32_t base, width;
1334 if (xen_pv_domain()) {
1335 base = xen_cpuid_base();
1336 if (cpuid_eax(base) < 5)
1337 return false; /* Information not available, use V1. */
1338 width = cpuid_ebx(base + 5) &
1339 XEN_CPUID_MACHINE_ADDRESS_WIDTH_MASK;
1340 return width > 32 + PAGE_SHIFT;
1343 return !!(max_possible_pfn >> 32);
1346 static void gnttab_request_version(void)
1349 struct gnttab_set_version gsv;
1351 if (gnttab_need_v2())
1356 /* Boot parameter overrides automatic selection. */
1357 if (xen_gnttab_version >= 1 && xen_gnttab_version <= 2)
1358 gsv.version = xen_gnttab_version;
1360 rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1);
1361 if (rc == 0 && gsv.version == 2)
1362 gnttab_interface = &gnttab_v2_ops;
1364 gnttab_interface = &gnttab_v1_ops;
1365 pr_info("Grant tables using version %d layout\n",
1366 gnttab_interface->version);
1369 static int gnttab_setup(void)
1371 unsigned int max_nr_gframes;
1373 max_nr_gframes = gnttab_max_grant_frames();
1374 if (max_nr_gframes < nr_grant_frames)
1377 if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) {
1378 gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr;
1379 if (gnttab_shared.addr == NULL) {
1380 pr_warn("gnttab share frames is not mapped!\n");
1384 return gnttab_map(0, nr_grant_frames - 1);
1387 int gnttab_resume(void)
1389 gnttab_request_version();
1390 return gnttab_setup();
1393 int gnttab_suspend(void)
1395 if (!xen_feature(XENFEAT_auto_translated_physmap))
1396 gnttab_interface->unmap_frames();
1400 static int gnttab_expand(unsigned int req_entries)
1403 unsigned int cur, extra;
1405 cur = nr_grant_frames;
1406 extra = ((req_entries + gnttab_interface->grefs_per_grant_frame - 1) /
1407 gnttab_interface->grefs_per_grant_frame);
1408 if (cur + extra > gnttab_max_grant_frames()) {
1409 pr_warn_ratelimited("xen/grant-table: max_grant_frames reached"
1410 " cur=%u extra=%u limit=%u"
1411 " gnttab_free_count=%u req_entries=%u\n",
1412 cur, extra, gnttab_max_grant_frames(),
1413 gnttab_free_count, req_entries);
1417 rc = gnttab_map(cur, cur + extra - 1);
1419 rc = grow_gnttab_list(extra);
1424 int gnttab_init(void)
1427 unsigned long max_nr_grant_frames;
1428 unsigned int max_nr_glist_frames, nr_glist_frames;
1429 unsigned int nr_init_grefs;
1432 gnttab_request_version();
1433 max_nr_grant_frames = gnttab_max_grant_frames();
1434 nr_grant_frames = 1;
1436 /* Determine the maximum number of frames required for the
1437 * grant reference free list on the current hypervisor.
1439 max_nr_glist_frames = (max_nr_grant_frames *
1440 gnttab_interface->grefs_per_grant_frame / RPP);
1442 gnttab_list = kmalloc_array(max_nr_glist_frames,
1443 sizeof(grant_ref_t *),
1445 if (gnttab_list == NULL)
1448 nr_glist_frames = gnttab_frames(nr_grant_frames, RPP);
1449 for (i = 0; i < nr_glist_frames; i++) {
1450 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
1451 if (gnttab_list[i] == NULL) {
1457 ret = arch_gnttab_init(max_nr_grant_frames,
1458 nr_status_frames(max_nr_grant_frames));
1462 if (gnttab_setup() < 0) {
1467 nr_init_grefs = nr_grant_frames *
1468 gnttab_interface->grefs_per_grant_frame;
1470 for (i = GNTTAB_NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
1471 gnttab_entry(i) = i + 1;
1473 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
1474 gnttab_free_count = nr_init_grefs - GNTTAB_NR_RESERVED_ENTRIES;
1475 gnttab_free_head = GNTTAB_NR_RESERVED_ENTRIES;
1477 printk("Grant table initialized\n");
1481 for (i--; i >= 0; i--)
1482 free_page((unsigned long)gnttab_list[i]);
1486 EXPORT_SYMBOL_GPL(gnttab_init);
1488 static int __gnttab_init(void)
1493 /* Delay grant-table initialization in the PV on HVM case */
1494 if (xen_hvm_domain() && !xen_pvh_domain())
1497 return gnttab_init();
1499 /* Starts after core_initcall so that xen_pvh_gnttab_setup can be called
1500 * beforehand to initialize xen_auto_xlat_grant_frames. */
1501 core_initcall_sync(__gnttab_init);