blob: 18355a53763f6811d4e9ca960816bd4183dfc99c [file] [log] [blame]
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -07001/******************************************************************************
2 * grant_table.c
3 *
4 * Granting foreign access to our memory reservation.
5 *
6 * Copyright (c) 2005-2006, Christopher Clark
7 * Copyright (c) 2004-2005, K A Fraser
8 *
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:
14 *
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:
21 *
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
24 *
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
31 * IN THE SOFTWARE.
32 */
33
34#include <linux/module.h>
35#include <linux/sched.h>
36#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -070038#include <linux/vmalloc.h>
39#include <linux/uaccess.h>
Stefano Stabellini183d03c2010-05-17 17:08:21 +010040#include <linux/io.h>
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -070041
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070042#include <xen/xen.h>
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -070043#include <xen/interface/xen.h>
44#include <xen/page.h>
45#include <xen/grant_table.h>
Stefano Stabellini183d03c2010-05-17 17:08:21 +010046#include <xen/interface/memory.h>
Jeremy Fitzhardingeecbf29c2008-12-16 12:37:07 -080047#include <asm/xen/hypercall.h>
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -070048
49#include <asm/pgtable.h>
50#include <asm/sync_bitops.h>
51
52
53/* External tools reserve first few grant table entries. */
54#define NR_RESERVED_ENTRIES 8
55#define GNTTAB_LIST_END 0xffffffff
Annie Li0f9f5a92011-11-22 09:58:06 +080056#define GREFS_PER_GRANT_FRAME (PAGE_SIZE / sizeof(struct grant_entry_v1))
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -070057
58static grant_ref_t **gnttab_list;
59static unsigned int nr_grant_frames;
60static unsigned int boot_max_nr_grant_frames;
61static int gnttab_free_count;
62static grant_ref_t gnttab_free_head;
63static DEFINE_SPINLOCK(gnttab_list_lock);
Stefano Stabellini183d03c2010-05-17 17:08:21 +010064unsigned long xen_hvm_resume_frames;
65EXPORT_SYMBOL_GPL(xen_hvm_resume_frames);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -070066
Annie Li0f9f5a92011-11-22 09:58:06 +080067static union {
68 struct grant_entry_v1 *v1;
69 void *addr;
70} gnttab_shared;
71
72/*This is a structure of function pointers for grant table*/
73struct gnttab_ops {
74 /*
75 * Mapping a list of frames for storing grant entries. First input
76 * parameter is used to storing grant table address when grant table
77 * being setup, second parameter is the number of frames to map grant
78 * table. Returning GNTST_okay means success and negative value means
79 * failure.
80 */
81 int (*map_frames)(unsigned long *, unsigned int);
82 /*
83 * Release a list of frames which are mapped in map_frames for grant
84 * entry status.
85 */
86 void (*unmap_frames)(void);
87 /*
88 * Introducing a valid entry into the grant table, granting the frame
89 * of this grant entry to domain for accessing, or transfering, or
90 * transitively accessing. First input parameter is reference of this
91 * introduced grant entry, second one is domid of granted domain, third
92 * one is the frame to be granted, and the last one is status of the
93 * grant entry to be updated.
94 */
95 void (*update_entry)(grant_ref_t, domid_t, unsigned long, unsigned);
96 /*
97 * Stop granting a grant entry to domain for accessing. First input
98 * parameter is reference of a grant entry whose grant access will be
99 * stopped, second one is not in use now. If the grant entry is
100 * currently mapped for reading or writing, just return failure(==0)
101 * directly and don't tear down the grant access. Otherwise, stop grant
102 * access for this entry and return success(==1).
103 */
104 int (*end_foreign_access_ref)(grant_ref_t, int);
105 /*
106 * Stop granting a grant entry to domain for transfer. If tranfer has
107 * not started, just reclaim the grant entry and return failure(==0).
108 * Otherwise, wait for the transfer to complete and then return the
109 * frame.
110 */
111 unsigned long (*end_foreign_transfer_ref)(grant_ref_t);
112 /*
113 * Query the status of a grant entry. Input parameter is reference of
114 * queried grant entry, return value is the status of queried entry.
115 * Detailed status(writing/reading) can be gotten from the return value
116 * by bit operations.
117 */
118 int (*query_foreign_access)(grant_ref_t);
119};
120
121static struct gnttab_ops *gnttab_interface;
122
123static int grant_table_version;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700124
125static struct gnttab_free_callback *gnttab_free_callback_list;
126
127static int gnttab_expand(unsigned int req_entries);
128
129#define RPP (PAGE_SIZE / sizeof(grant_ref_t))
130
131static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
132{
133 return &gnttab_list[(entry) / RPP][(entry) % RPP];
134}
135/* This can be used as an l-value */
136#define gnttab_entry(entry) (*__gnttab_entry(entry))
137
138static int get_free_entries(unsigned count)
139{
140 unsigned long flags;
Konrad Rzeszutek Wilk272800d2011-07-22 14:00:06 -0400141 int ref, rc = 0;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700142 grant_ref_t head;
143
144 spin_lock_irqsave(&gnttab_list_lock, flags);
145
146 if ((gnttab_free_count < count) &&
147 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
148 spin_unlock_irqrestore(&gnttab_list_lock, flags);
149 return rc;
150 }
151
152 ref = head = gnttab_free_head;
153 gnttab_free_count -= count;
154 while (count-- > 1)
155 head = gnttab_entry(head);
156 gnttab_free_head = gnttab_entry(head);
157 gnttab_entry(head) = GNTTAB_LIST_END;
158
159 spin_unlock_irqrestore(&gnttab_list_lock, flags);
160
161 return ref;
162}
163
164static void do_free_callbacks(void)
165{
166 struct gnttab_free_callback *callback, *next;
167
168 callback = gnttab_free_callback_list;
169 gnttab_free_callback_list = NULL;
170
171 while (callback != NULL) {
172 next = callback->next;
173 if (gnttab_free_count >= callback->count) {
174 callback->next = NULL;
175 callback->fn(callback->arg);
176 } else {
177 callback->next = gnttab_free_callback_list;
178 gnttab_free_callback_list = callback;
179 }
180 callback = next;
181 }
182}
183
184static inline void check_free_callbacks(void)
185{
186 if (unlikely(gnttab_free_callback_list))
187 do_free_callbacks();
188}
189
190static void put_free_entry(grant_ref_t ref)
191{
192 unsigned long flags;
193 spin_lock_irqsave(&gnttab_list_lock, flags);
194 gnttab_entry(ref) = gnttab_free_head;
195 gnttab_free_head = ref;
196 gnttab_free_count++;
197 check_free_callbacks();
198 spin_unlock_irqrestore(&gnttab_list_lock, flags);
199}
200
Annie Li0f9f5a92011-11-22 09:58:06 +0800201/*
202 * Introducing a valid entry into the grant table:
203 * 1. Write ent->domid.
204 * 2. Write ent->frame:
205 * GTF_permit_access: Frame to which access is permitted.
206 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
207 * frame, or zero if none.
208 * 3. Write memory barrier (WMB).
209 * 4. Write ent->flags, inc. valid type.
210 */
211static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
212 unsigned long frame, unsigned flags)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700213{
Annie Li0f9f5a92011-11-22 09:58:06 +0800214 gnttab_shared.v1[ref].domid = domid;
215 gnttab_shared.v1[ref].frame = frame;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700216 wmb();
Annie Li0f9f5a92011-11-22 09:58:06 +0800217 gnttab_shared.v1[ref].flags = flags;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700218}
219
220/*
221 * Public grant-issuing interface functions
222 */
223void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
224 unsigned long frame, int readonly)
225{
Annie Li0f9f5a92011-11-22 09:58:06 +0800226 gnttab_interface->update_entry(ref, domid, frame,
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700227 GTF_permit_access | (readonly ? GTF_readonly : 0));
228}
229EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
230
231int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
232 int readonly)
233{
234 int ref;
235
236 ref = get_free_entries(1);
237 if (unlikely(ref < 0))
238 return -ENOSPC;
239
240 gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
241
242 return ref;
243}
244EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
245
Annie Li0f9f5a92011-11-22 09:58:06 +0800246static int gnttab_query_foreign_access_v1(grant_ref_t ref)
247{
248 return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
249}
250
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700251int gnttab_query_foreign_access(grant_ref_t ref)
252{
Annie Li0f9f5a92011-11-22 09:58:06 +0800253 return gnttab_interface->query_foreign_access(ref);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700254}
255EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
256
Annie Li0f9f5a92011-11-22 09:58:06 +0800257static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700258{
259 u16 flags, nflags;
260
Annie Li0f9f5a92011-11-22 09:58:06 +0800261 nflags = gnttab_shared.v1[ref].flags;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700262 do {
263 flags = nflags;
264 if (flags & (GTF_reading|GTF_writing)) {
265 printk(KERN_ALERT "WARNING: g.e. still in use!\n");
266 return 0;
267 }
Annie Li0f9f5a92011-11-22 09:58:06 +0800268 } while ((nflags = sync_cmpxchg(&gnttab_shared.v1[ref].flags, flags, 0)) != flags);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700269
270 return 1;
271}
Annie Li0f9f5a92011-11-22 09:58:06 +0800272
273int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
274{
275 return gnttab_interface->end_foreign_access_ref(ref, readonly);
276}
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700277EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
278
279void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
280 unsigned long page)
281{
282 if (gnttab_end_foreign_access_ref(ref, readonly)) {
283 put_free_entry(ref);
284 if (page != 0)
285 free_page(page);
286 } else {
287 /* XXX This needs to be fixed so that the ref and page are
288 placed on a list to be freed up later. */
289 printk(KERN_WARNING
290 "WARNING: leaking g.e. and page still in use!\n");
291 }
292}
293EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
294
295int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
296{
297 int ref;
298
299 ref = get_free_entries(1);
300 if (unlikely(ref < 0))
301 return -ENOSPC;
302 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
303
304 return ref;
305}
306EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
307
308void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
309 unsigned long pfn)
310{
Annie Li0f9f5a92011-11-22 09:58:06 +0800311 gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700312}
313EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
314
Annie Li0f9f5a92011-11-22 09:58:06 +0800315static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700316{
317 unsigned long frame;
318 u16 flags;
319
320 /*
321 * If a transfer is not even yet started, try to reclaim the grant
322 * reference and return failure (== 0).
323 */
Annie Li0f9f5a92011-11-22 09:58:06 +0800324 while (!((flags = gnttab_shared.v1[ref].flags) & GTF_transfer_committed)) {
325 if (sync_cmpxchg(&gnttab_shared.v1[ref].flags, flags, 0) == flags)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700326 return 0;
327 cpu_relax();
328 }
329
330 /* If a transfer is in progress then wait until it is completed. */
331 while (!(flags & GTF_transfer_completed)) {
Annie Li0f9f5a92011-11-22 09:58:06 +0800332 flags = gnttab_shared.v1[ref].flags;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700333 cpu_relax();
334 }
335
336 rmb(); /* Read the frame number /after/ reading completion status. */
Annie Li0f9f5a92011-11-22 09:58:06 +0800337 frame = gnttab_shared.v1[ref].frame;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700338 BUG_ON(frame == 0);
339
340 return frame;
341}
Annie Li0f9f5a92011-11-22 09:58:06 +0800342
343unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
344{
345 return gnttab_interface->end_foreign_transfer_ref(ref);
346}
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700347EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
348
349unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
350{
351 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
352 put_free_entry(ref);
353 return frame;
354}
355EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
356
357void gnttab_free_grant_reference(grant_ref_t ref)
358{
359 put_free_entry(ref);
360}
361EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
362
363void gnttab_free_grant_references(grant_ref_t head)
364{
365 grant_ref_t ref;
366 unsigned long flags;
367 int count = 1;
368 if (head == GNTTAB_LIST_END)
369 return;
370 spin_lock_irqsave(&gnttab_list_lock, flags);
371 ref = head;
372 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
373 ref = gnttab_entry(ref);
374 count++;
375 }
376 gnttab_entry(ref) = gnttab_free_head;
377 gnttab_free_head = head;
378 gnttab_free_count += count;
379 check_free_callbacks();
380 spin_unlock_irqrestore(&gnttab_list_lock, flags);
381}
382EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
383
384int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
385{
386 int h = get_free_entries(count);
387
388 if (h < 0)
389 return -ENOSPC;
390
391 *head = h;
392
393 return 0;
394}
395EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
396
397int gnttab_empty_grant_references(const grant_ref_t *private_head)
398{
399 return (*private_head == GNTTAB_LIST_END);
400}
401EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
402
403int gnttab_claim_grant_reference(grant_ref_t *private_head)
404{
405 grant_ref_t g = *private_head;
406 if (unlikely(g == GNTTAB_LIST_END))
407 return -ENOSPC;
408 *private_head = gnttab_entry(g);
409 return g;
410}
411EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
412
413void gnttab_release_grant_reference(grant_ref_t *private_head,
414 grant_ref_t release)
415{
416 gnttab_entry(release) = *private_head;
417 *private_head = release;
418}
419EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
420
421void gnttab_request_free_callback(struct gnttab_free_callback *callback,
422 void (*fn)(void *), void *arg, u16 count)
423{
424 unsigned long flags;
425 spin_lock_irqsave(&gnttab_list_lock, flags);
426 if (callback->next)
427 goto out;
428 callback->fn = fn;
429 callback->arg = arg;
430 callback->count = count;
431 callback->next = gnttab_free_callback_list;
432 gnttab_free_callback_list = callback;
433 check_free_callbacks();
434out:
435 spin_unlock_irqrestore(&gnttab_list_lock, flags);
436}
437EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
438
439void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
440{
441 struct gnttab_free_callback **pcb;
442 unsigned long flags;
443
444 spin_lock_irqsave(&gnttab_list_lock, flags);
445 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
446 if (*pcb == callback) {
447 *pcb = callback->next;
448 break;
449 }
450 }
451 spin_unlock_irqrestore(&gnttab_list_lock, flags);
452}
453EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
454
455static int grow_gnttab_list(unsigned int more_frames)
456{
457 unsigned int new_nr_grant_frames, extra_entries, i;
Michael Abd-El-Malekbbc60c12008-04-04 02:33:48 -0700458 unsigned int nr_glist_frames, new_nr_glist_frames;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700459
460 new_nr_grant_frames = nr_grant_frames + more_frames;
461 extra_entries = more_frames * GREFS_PER_GRANT_FRAME;
462
Michael Abd-El-Malekbbc60c12008-04-04 02:33:48 -0700463 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
464 new_nr_glist_frames =
465 (new_nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
466 for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700467 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
468 if (!gnttab_list[i])
469 goto grow_nomem;
470 }
471
472
473 for (i = GREFS_PER_GRANT_FRAME * nr_grant_frames;
474 i < GREFS_PER_GRANT_FRAME * new_nr_grant_frames - 1; i++)
475 gnttab_entry(i) = i + 1;
476
477 gnttab_entry(i) = gnttab_free_head;
478 gnttab_free_head = GREFS_PER_GRANT_FRAME * nr_grant_frames;
479 gnttab_free_count += extra_entries;
480
481 nr_grant_frames = new_nr_grant_frames;
482
483 check_free_callbacks();
484
485 return 0;
486
487grow_nomem:
Michael Abd-El-Malekbbc60c12008-04-04 02:33:48 -0700488 for ( ; i >= nr_glist_frames; i--)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700489 free_page((unsigned long) gnttab_list[i]);
490 return -ENOMEM;
491}
492
493static unsigned int __max_nr_grant_frames(void)
494{
495 struct gnttab_query_size query;
496 int rc;
497
498 query.dom = DOMID_SELF;
499
500 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
501 if ((rc < 0) || (query.status != GNTST_okay))
502 return 4; /* Legacy max supported number of frames */
503
504 return query.max_nr_frames;
505}
506
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100507unsigned int gnttab_max_grant_frames(void)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700508{
509 unsigned int xen_max = __max_nr_grant_frames();
510
511 if (xen_max > boot_max_nr_grant_frames)
512 return boot_max_nr_grant_frames;
513 return xen_max;
514}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100515EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700516
Stefano Stabellini289b7772010-12-10 14:54:44 +0000517int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100518 struct gnttab_map_grant_ref *kmap_ops,
519 struct page **pages, unsigned int count)
Stefano Stabellini289b7772010-12-10 14:54:44 +0000520{
521 int i, ret;
522 pte_t *pte;
523 unsigned long mfn;
524
525 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
Jeremy Fitzhardinge87f1d402010-12-13 14:42:30 +0000526 if (ret)
527 return ret;
Stefano Stabellini289b7772010-12-10 14:54:44 +0000528
Daniel De Graafaab8f112011-02-03 12:19:02 -0500529 if (xen_feature(XENFEAT_auto_translated_physmap))
530 return ret;
531
Stefano Stabellini289b7772010-12-10 14:54:44 +0000532 for (i = 0; i < count; i++) {
Ian Campbelldc4972a2011-03-04 17:38:21 +0000533 /* Do not add to override if the map failed. */
534 if (map_ops[i].status)
535 continue;
536
Konrad Rzeszutek Wilkcf8d9162011-02-28 17:58:48 -0500537 if (map_ops[i].flags & GNTMAP_contains_pte) {
538 pte = (pte_t *) (mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) +
Stefano Stabellini289b7772010-12-10 14:54:44 +0000539 (map_ops[i].host_addr & ~PAGE_MASK));
Konrad Rzeszutek Wilkcf8d9162011-02-28 17:58:48 -0500540 mfn = pte_mfn(*pte);
541 } else {
542 /* If you really wanted to do this:
543 * mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
544 *
545 * The reason we do not implement it is b/c on the
546 * unmap path (gnttab_unmap_refs) we have no means of
547 * checking whether the page is !GNTMAP_contains_pte.
548 *
549 * That is without some extra data-structure to carry
550 * the struct page, bool clear_pte, and list_head next
551 * tuples and deal with allocation/delallocation, etc.
552 *
553 * The users of this API set the GNTMAP_contains_pte
554 * flag so lets just return not supported until it
555 * becomes neccessary to implement.
556 */
557 return -EOPNOTSUPP;
558 }
Stefano Stabellini0930bba2011-09-29 11:57:56 +0100559 ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]);
Jeremy Fitzhardinge87f1d402010-12-13 14:42:30 +0000560 if (ret)
561 return ret;
Stefano Stabellini289b7772010-12-10 14:54:44 +0000562 }
563
564 return ret;
565}
566EXPORT_SYMBOL_GPL(gnttab_map_refs);
567
568int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
569 struct page **pages, unsigned int count)
570{
571 int i, ret;
572
573 ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
Jeremy Fitzhardinge87f1d402010-12-13 14:42:30 +0000574 if (ret)
575 return ret;
576
Daniel De Graafaab8f112011-02-03 12:19:02 -0500577 if (xen_feature(XENFEAT_auto_translated_physmap))
578 return ret;
579
Jeremy Fitzhardinge87f1d402010-12-13 14:42:30 +0000580 for (i = 0; i < count; i++) {
Konrad Rzeszutek Wilkcf8d9162011-02-28 17:58:48 -0500581 ret = m2p_remove_override(pages[i], true /* clear the PTE */);
Jeremy Fitzhardinge87f1d402010-12-13 14:42:30 +0000582 if (ret)
583 return ret;
584 }
Stefano Stabellini289b7772010-12-10 14:54:44 +0000585
586 return ret;
587}
588EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
589
Annie Li0f9f5a92011-11-22 09:58:06 +0800590static int gnttab_map_frames_v1(unsigned long *frames, unsigned int nr_gframes)
591{
592 int rc;
593
594 rc = arch_gnttab_map_shared(frames, nr_gframes,
595 gnttab_max_grant_frames(),
596 &gnttab_shared.addr);
597 BUG_ON(rc);
598
599 return 0;
600}
601
602static void gnttab_unmap_frames_v1(void)
603{
604 arch_gnttab_unmap_shared(gnttab_shared.addr, nr_grant_frames);
605}
606
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700607static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
608{
609 struct gnttab_setup_table setup;
610 unsigned long *frames;
611 unsigned int nr_gframes = end_idx + 1;
612 int rc;
613
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100614 if (xen_hvm_domain()) {
615 struct xen_add_to_physmap xatp;
616 unsigned int i = end_idx;
617 rc = 0;
618 /*
619 * Loop backwards, so that the first hypercall has the largest
620 * index, ensuring that the table will grow only once.
621 */
622 do {
623 xatp.domid = DOMID_SELF;
624 xatp.idx = i;
625 xatp.space = XENMAPSPACE_grant_table;
626 xatp.gpfn = (xen_hvm_resume_frames >> PAGE_SHIFT) + i;
627 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
628 if (rc != 0) {
629 printk(KERN_WARNING
630 "grant table add_to_physmap failed, err=%d\n", rc);
631 break;
632 }
633 } while (i-- > start_idx);
634
635 return rc;
636 }
637
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700638 frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
639 if (!frames)
640 return -ENOMEM;
641
642 setup.dom = DOMID_SELF;
643 setup.nr_frames = nr_gframes;
Isaku Yamahata87e27cf2008-04-02 10:53:52 -0700644 set_xen_guest_handle(setup.frame_list, frames);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700645
646 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
647 if (rc == -ENOSYS) {
648 kfree(frames);
649 return -ENOSYS;
650 }
651
652 BUG_ON(rc || setup.status);
653
Annie Li0f9f5a92011-11-22 09:58:06 +0800654 rc = gnttab_interface->map_frames(frames, nr_gframes);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700655
656 kfree(frames);
657
Annie Li0f9f5a92011-11-22 09:58:06 +0800658 return rc;
659}
660
661static struct gnttab_ops gnttab_v1_ops = {
662 .map_frames = gnttab_map_frames_v1,
663 .unmap_frames = gnttab_unmap_frames_v1,
664 .update_entry = gnttab_update_entry_v1,
665 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
666 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
667 .query_foreign_access = gnttab_query_foreign_access_v1,
668};
669
670static void gnttab_request_version(void)
671{
672 grant_table_version = 1;
673 gnttab_interface = &gnttab_v1_ops;
674 printk(KERN_INFO "Grant tables using version %d layout.\n",
675 grant_table_version);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700676}
677
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100678int gnttab_resume(void)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700679{
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100680 unsigned int max_nr_gframes;
681
Annie Li0f9f5a92011-11-22 09:58:06 +0800682 gnttab_request_version();
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100683 max_nr_gframes = gnttab_max_grant_frames();
684 if (max_nr_gframes < nr_grant_frames)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700685 return -ENOSYS;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100686
687 if (xen_pv_domain())
688 return gnttab_map(0, nr_grant_frames - 1);
689
Annie Li0f9f5a92011-11-22 09:58:06 +0800690 if (gnttab_shared.addr == NULL) {
691 gnttab_shared.addr = ioremap(xen_hvm_resume_frames,
692 PAGE_SIZE * max_nr_gframes);
693 if (gnttab_shared.addr == NULL) {
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100694 printk(KERN_WARNING
695 "Failed to ioremap gnttab share frames!");
696 return -ENOMEM;
697 }
698 }
699
700 gnttab_map(0, nr_grant_frames - 1);
701
702 return 0;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700703}
704
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100705int gnttab_suspend(void)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700706{
Annie Li0f9f5a92011-11-22 09:58:06 +0800707 gnttab_interface->unmap_frames();
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700708 return 0;
709}
710
711static int gnttab_expand(unsigned int req_entries)
712{
713 int rc;
714 unsigned int cur, extra;
715
716 cur = nr_grant_frames;
717 extra = ((req_entries + (GREFS_PER_GRANT_FRAME-1)) /
718 GREFS_PER_GRANT_FRAME);
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100719 if (cur + extra > gnttab_max_grant_frames())
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700720 return -ENOSPC;
721
722 rc = gnttab_map(cur, cur + extra - 1);
723 if (rc == 0)
724 rc = grow_gnttab_list(extra);
725
726 return rc;
727}
728
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100729int gnttab_init(void)
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700730{
731 int i;
Michael Abd-El-Malekbbc60c12008-04-04 02:33:48 -0700732 unsigned int max_nr_glist_frames, nr_glist_frames;
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700733 unsigned int nr_init_grefs;
734
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700735 nr_grant_frames = 1;
736 boot_max_nr_grant_frames = __max_nr_grant_frames();
737
738 /* Determine the maximum number of frames required for the
739 * grant reference free list on the current hypervisor.
740 */
741 max_nr_glist_frames = (boot_max_nr_grant_frames *
Michael Abd-El-Malekbbc60c12008-04-04 02:33:48 -0700742 GREFS_PER_GRANT_FRAME / RPP);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700743
744 gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
745 GFP_KERNEL);
746 if (gnttab_list == NULL)
747 return -ENOMEM;
748
Michael Abd-El-Malekbbc60c12008-04-04 02:33:48 -0700749 nr_glist_frames = (nr_grant_frames * GREFS_PER_GRANT_FRAME + RPP - 1) / RPP;
750 for (i = 0; i < nr_glist_frames; i++) {
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700751 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
752 if (gnttab_list[i] == NULL)
753 goto ini_nomem;
754 }
755
756 if (gnttab_resume() < 0)
757 return -ENODEV;
758
759 nr_init_grefs = nr_grant_frames * GREFS_PER_GRANT_FRAME;
760
761 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
762 gnttab_entry(i) = i + 1;
763
764 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
765 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
766 gnttab_free_head = NR_RESERVED_ENTRIES;
767
768 printk("Grant table initialized\n");
769 return 0;
770
771 ini_nomem:
772 for (i--; i >= 0; i--)
773 free_page((unsigned long)gnttab_list[i]);
774 kfree(gnttab_list);
775 return -ENOMEM;
776}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100777EXPORT_SYMBOL_GPL(gnttab_init);
Jeremy Fitzhardingead9a8612007-07-17 18:37:06 -0700778
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100779static int __devinit __gnttab_init(void)
780{
781 /* Delay grant-table initialization in the PV on HVM case */
782 if (xen_hvm_domain())
783 return 0;
784
785 if (!xen_pv_domain())
786 return -ENODEV;
787
788 return gnttab_init();
789}
790
791core_initcall(__gnttab_init);