Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / include / xen / gnttab.h
1 /******************************************************************************
2  * gnttab.h
3  * 
4  * Two sets of functionality:
5  * 1. Granting foreign access to our memory reservation.
6  * 2. Accessing others' memory reservations via grant references.
7  * (i.e., mechanisms for both sender and recipient of grant references)
8  * 
9  * Copyright (c) 2004-2005, K A Fraser
10  * Copyright (c) 2005, Christopher Clark
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License version 2
14  * as published by the Free Software Foundation; or, when distributed
15  * separately from the Linux kernel or incorporated into other
16  * software packages, subject to the following license:
17  * 
18  * Permission is hereby granted, free of charge, to any person obtaining a copy
19  * of this source file (the "Software"), to deal in the Software without
20  * restriction, including without limitation the rights to use, copy, modify,
21  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22  * and to permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  * 
25  * The above copyright notice and this permission notice shall be included in
26  * all copies or substantial portions of the Software.
27  * 
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34  * IN THE SOFTWARE.
35  */
36
37 #ifndef __ASM_GNTTAB_H__
38 #define __ASM_GNTTAB_H__
39
40 #include <asm/hypervisor.h>
41 #include <asm/maddr.h> /* maddr_t */
42 #include <linux/mm.h>
43 #include <linux/delay.h>
44 #include <xen/interface/grant_table.h>
45 #include <xen/features.h>
46
47 #define GRANT_INVALID_REF       0
48
49 struct gnttab_free_callback {
50         struct gnttab_free_callback *next;
51         void (*fn)(void *);
52         void *arg;
53         u16 count;
54         u8 queued;
55 };
56
57 int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
58                                 int flags);
59
60 /*
61  * End access through the given grant reference, iff the grant entry is no
62  * longer in use.  Return 1 if the grant entry was freed, 0 if it is still in
63  * use.
64  */
65 int gnttab_end_foreign_access_ref(grant_ref_t ref);
66
67 /*
68  * Eventually end access through the given grant reference, and once that
69  * access has been ended, free the given page too.  Access will be ended
70  * immediately iff the grant entry is not in use, otherwise it will happen
71  * some time later.  page may be 0, in which case no freeing will occur.
72  */
73 void gnttab_end_foreign_access(grant_ref_t ref, unsigned long page);
74
75 int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn);
76
77 unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref);
78 unsigned long gnttab_end_foreign_transfer(grant_ref_t ref);
79
80 int gnttab_query_foreign_access(grant_ref_t ref);
81
82 /*
83  * operations on reserved batches of grant references
84  */
85 int gnttab_alloc_grant_references(u16 count, grant_ref_t *pprivate_head);
86
87 void gnttab_free_grant_reference(grant_ref_t ref);
88
89 void gnttab_free_grant_references(grant_ref_t head);
90
91 int gnttab_empty_grant_references(const grant_ref_t *pprivate_head);
92
93 int gnttab_claim_grant_reference(grant_ref_t *pprivate_head);
94
95 void gnttab_release_grant_reference(grant_ref_t *private_head,
96                                     grant_ref_t release);
97
98 void gnttab_request_free_callback(struct gnttab_free_callback *callback,
99                                   void (*fn)(void *), void *arg, u16 count);
100 void gnttab_cancel_free_callback(struct gnttab_free_callback *callback);
101
102 void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
103                                      unsigned long frame, int flags);
104
105 void gnttab_grant_foreign_transfer_ref(grant_ref_t, domid_t domid,
106                                        unsigned long pfn);
107
108 int gnttab_copy_grant_page(grant_ref_t ref, struct page **pagep);
109 #if defined(CONFIG_XEN_BACKEND) || defined(CONFIG_XEN_BACKEND_MODULE)
110 void __gnttab_dma_map_page(struct page *page);
111 #else
112 #define __gnttab_dma_map_page __gnttab_dma_unmap_page
113 #endif
114 static inline void __gnttab_dma_unmap_page(struct page *page)
115 {
116 }
117
118 void gnttab_reset_grant_page(struct page *page);
119
120 #ifndef CONFIG_XEN
121 int gnttab_resume(void);
122 #endif
123
124 void *arch_gnttab_alloc_shared(unsigned long *frames);
125
126 static inline void
127 gnttab_set_map_op(struct gnttab_map_grant_ref *map, maddr_t addr,
128                   uint32_t flags, grant_ref_t ref, domid_t domid)
129 {
130         if (flags & GNTMAP_contains_pte)
131                 map->host_addr = addr;
132         else if (xen_feature(XENFEAT_auto_translated_physmap))
133                 map->host_addr = __pa(addr);
134         else
135                 map->host_addr = addr;
136
137         map->flags = flags;
138         map->ref = ref;
139         map->dom = domid;
140 }
141
142 static inline void
143 gnttab_set_unmap_op(struct gnttab_unmap_grant_ref *unmap, maddr_t addr,
144                     uint32_t flags, grant_handle_t handle)
145 {
146         if (flags & GNTMAP_contains_pte)
147                 unmap->host_addr = addr;
148         else if (xen_feature(XENFEAT_auto_translated_physmap))
149                 unmap->host_addr = __pa(addr);
150         else
151                 unmap->host_addr = addr;
152
153         unmap->handle = handle;
154         unmap->dev_bus_addr = 0;
155 }
156
157 static inline void
158 gnttab_set_replace_op(struct gnttab_unmap_and_replace *unmap, maddr_t addr,
159                       maddr_t new_addr, grant_handle_t handle)
160 {
161         if (xen_feature(XENFEAT_auto_translated_physmap)) {
162                 unmap->host_addr = __pa(addr);
163                 unmap->new_addr = __pa(new_addr);
164         } else {
165                 unmap->host_addr = addr;
166                 unmap->new_addr = new_addr;
167         }
168
169         unmap->handle = handle;
170 }
171
172 #define gnttab_check_GNTST_eagain_while(__HCop, __HCarg_p)                      \
173 {                                                                               \
174         u8 __hc_delay = 1;                                                      \
175         int __ret;                                                              \
176         while (unlikely((__HCarg_p)->status == GNTST_eagain && __hc_delay)) {   \
177                 msleep(__hc_delay++);                                           \
178                 __ret = HYPERVISOR_grant_table_op(__HCop, (__HCarg_p), 1);      \
179                 BUG_ON(__ret);                                                  \
180         }                                                                       \
181         if (__hc_delay == 0) {                                                  \
182                 pr_err("%s: %s gnt busy\n", __func__, current->comm);           \
183                 (__HCarg_p)->status = GNTST_bad_page;                           \
184         }                                                                       \
185         if ((__HCarg_p)->status != GNTST_okay)                                  \
186                 pr_err("%s: %s gnt status %x\n",                                \
187                         __func__, current->comm, (__HCarg_p)->status);          \
188 }
189
190 #define gnttab_check_GNTST_eagain_do_while(__HCop, __HCarg_p)                   \
191 {                                                                               \
192         u8 __hc_delay = 1;                                                      \
193         int __ret;                                                              \
194         do {                                                                    \
195                 __ret = HYPERVISOR_grant_table_op(__HCop, (__HCarg_p), 1);      \
196                 BUG_ON(__ret);                                                  \
197                 if ((__HCarg_p)->status == GNTST_eagain)                        \
198                         msleep(__hc_delay++);                                   \
199         } while ((__HCarg_p)->status == GNTST_eagain && __hc_delay);            \
200         if (__hc_delay == 0) {                                                  \
201                 pr_err("%s: %s gnt busy\n", __func__, current->comm);           \
202                 (__HCarg_p)->status = GNTST_bad_page;                           \
203         }                                                                       \
204         if ((__HCarg_p)->status != GNTST_okay)                                  \
205                 pr_err("%s: %s gnt status %x\n",                                \
206                         __func__, current->comm, (__HCarg_p)->status);          \
207 }
208
209 #endif /* __ASM_GNTTAB_H__ */