xen: re-introduce support for grant v2 interface
[linux-2.6-block.git] / drivers / xen / grant-table.c
CommitLineData
ad9a8612
JF
1/******************************************************************************
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
283c0972
JP
34#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
35
ad9a8612
JF
36#include <linux/sched.h>
37#include <linux/mm.h>
5a0e3ad6 38#include <linux/slab.h>
ad9a8612
JF
39#include <linux/vmalloc.h>
40#include <linux/uaccess.h>
183d03cc 41#include <linux/io.h>
c571898f 42#include <linux/delay.h>
f62805f1 43#include <linux/hardirq.h>
3f9f1c67 44#include <linux/workqueue.h>
29d11cfd 45#include <linux/ratelimit.h>
ad9a8612 46
1ccbf534 47#include <xen/xen.h>
ad9a8612
JF
48#include <xen/interface/xen.h>
49#include <xen/page.h>
50#include <xen/grant_table.h>
183d03cc 51#include <xen/interface/memory.h>
85ff6acb 52#include <xen/hvc-console.h>
3d24bbd7 53#include <xen/swiotlb-xen.h>
ff4b156f 54#include <xen/balloon.h>
ecbf29cd 55#include <asm/xen/hypercall.h>
4d9310e3 56#include <asm/xen/interface.h>
ad9a8612
JF
57
58#include <asm/pgtable.h>
59#include <asm/sync_bitops.h>
60
ad9a8612
JF
61/* External tools reserve first few grant table entries. */
62#define NR_RESERVED_ENTRIES 8
63#define GNTTAB_LIST_END 0xffffffff
ad9a8612
JF
64
65static grant_ref_t **gnttab_list;
66static unsigned int nr_grant_frames;
ad9a8612
JF
67static int gnttab_free_count;
68static grant_ref_t gnttab_free_head;
69static DEFINE_SPINLOCK(gnttab_list_lock);
efaf30a3 70struct grant_frames xen_auto_xlat_grant_frames;
ad9a8612 71
0f9f5a95
AL
72static union {
73 struct grant_entry_v1 *v1;
b988b8ff 74 union grant_entry_v2 *v2;
0f9f5a95
AL
75 void *addr;
76} gnttab_shared;
77
78/*This is a structure of function pointers for grant table*/
79struct gnttab_ops {
80 /*
9dbc71d5
AL
81 * Mapping a list of frames for storing grant entries. Frames parameter
82 * is used to store grant table address when grant table being setup,
83 * nr_gframes is the number of frames to map grant table. Returning
84 * GNTST_okay means success and negative value means failure.
0f9f5a95 85 */
ef32f892 86 int (*map_frames)(xen_pfn_t *frames, unsigned int nr_gframes);
0f9f5a95
AL
87 /*
88 * Release a list of frames which are mapped in map_frames for grant
89 * entry status.
90 */
91 void (*unmap_frames)(void);
92 /*
9dbc71d5
AL
93 * Introducing a valid entry into the grant table, granting the frame of
94 * this grant entry to domain for accessing or transfering. Ref
95 * parameter is reference of this introduced grant entry, domid is id of
96 * granted domain, frame is the page frame to be granted, and flags is
97 * status of the grant entry to be updated.
0f9f5a95 98 */
9dbc71d5
AL
99 void (*update_entry)(grant_ref_t ref, domid_t domid,
100 unsigned long frame, unsigned flags);
0f9f5a95 101 /*
9dbc71d5
AL
102 * Stop granting a grant entry to domain for accessing. Ref parameter is
103 * reference of a grant entry whose grant access will be stopped,
104 * readonly is not in use in this function. If the grant entry is
0f9f5a95
AL
105 * currently mapped for reading or writing, just return failure(==0)
106 * directly and don't tear down the grant access. Otherwise, stop grant
107 * access for this entry and return success(==1).
108 */
9dbc71d5 109 int (*end_foreign_access_ref)(grant_ref_t ref, int readonly);
0f9f5a95 110 /*
9dbc71d5
AL
111 * Stop granting a grant entry to domain for transfer. Ref parameter is
112 * reference of a grant entry whose grant transfer will be stopped. If
113 * tranfer has not started, just reclaim the grant entry and return
114 * failure(==0). Otherwise, wait for the transfer to complete and then
115 * return the frame.
0f9f5a95 116 */
9dbc71d5 117 unsigned long (*end_foreign_transfer_ref)(grant_ref_t ref);
0f9f5a95 118 /*
9dbc71d5 119 * Query the status of a grant entry. Ref parameter is reference of
0f9f5a95
AL
120 * queried grant entry, return value is the status of queried entry.
121 * Detailed status(writing/reading) can be gotten from the return value
122 * by bit operations.
123 */
9dbc71d5 124 int (*query_foreign_access)(grant_ref_t ref);
b988b8ff
JG
125 /*
126 * Grant a domain to access a range of bytes within the page referred by
127 * an available grant entry. Ref parameter is reference of a grant entry
128 * which will be sub-page accessed, domid is id of grantee domain, frame
129 * is frame address of subpage grant, flags is grant type and flag
130 * information, page_off is offset of the range of bytes, and length is
131 * length of bytes to be accessed.
132 */
133 void (*update_subpage_entry)(grant_ref_t ref, domid_t domid,
134 unsigned long frame, int flags,
135 unsigned page_off, unsigned length);
136 /*
137 * Redirect an available grant entry on domain A to another grant
138 * reference of domain B, then allow domain C to use grant reference
139 * of domain B transitively. Ref parameter is an available grant entry
140 * reference on domain A, domid is id of domain C which accesses grant
141 * entry transitively, flags is grant type and flag information,
142 * trans_domid is id of domain B whose grant entry is finally accessed
143 * transitively, trans_gref is grant entry transitive reference of
144 * domain B.
145 */
146 void (*update_trans_entry)(grant_ref_t ref, domid_t domid, int flags,
147 domid_t trans_domid, grant_ref_t trans_gref);
0f9f5a95
AL
148};
149
b44166cd
BL
150struct unmap_refs_callback_data {
151 struct completion completion;
152 int result;
153};
154
86fc2136 155static const struct gnttab_ops *gnttab_interface;
0f9f5a95 156
b988b8ff
JG
157/* This reflects status of grant entries, so act as a global value. */
158static grant_status_t *grstatus;
159
0f9f5a95 160static int grant_table_version;
d0b4d64a 161static int grefs_per_grant_frame;
ad9a8612
JF
162
163static struct gnttab_free_callback *gnttab_free_callback_list;
164
165static int gnttab_expand(unsigned int req_entries);
166
167#define RPP (PAGE_SIZE / sizeof(grant_ref_t))
b988b8ff 168#define SPP (PAGE_SIZE / sizeof(grant_status_t))
ad9a8612
JF
169
170static inline grant_ref_t *__gnttab_entry(grant_ref_t entry)
171{
172 return &gnttab_list[(entry) / RPP][(entry) % RPP];
173}
174/* This can be used as an l-value */
175#define gnttab_entry(entry) (*__gnttab_entry(entry))
176
177static int get_free_entries(unsigned count)
178{
179 unsigned long flags;
272800dc 180 int ref, rc = 0;
ad9a8612
JF
181 grant_ref_t head;
182
183 spin_lock_irqsave(&gnttab_list_lock, flags);
184
185 if ((gnttab_free_count < count) &&
186 ((rc = gnttab_expand(count - gnttab_free_count)) < 0)) {
187 spin_unlock_irqrestore(&gnttab_list_lock, flags);
188 return rc;
189 }
190
191 ref = head = gnttab_free_head;
192 gnttab_free_count -= count;
193 while (count-- > 1)
194 head = gnttab_entry(head);
195 gnttab_free_head = gnttab_entry(head);
196 gnttab_entry(head) = GNTTAB_LIST_END;
197
198 spin_unlock_irqrestore(&gnttab_list_lock, flags);
199
200 return ref;
201}
202
203static void do_free_callbacks(void)
204{
205 struct gnttab_free_callback *callback, *next;
206
207 callback = gnttab_free_callback_list;
208 gnttab_free_callback_list = NULL;
209
210 while (callback != NULL) {
211 next = callback->next;
212 if (gnttab_free_count >= callback->count) {
213 callback->next = NULL;
214 callback->fn(callback->arg);
215 } else {
216 callback->next = gnttab_free_callback_list;
217 gnttab_free_callback_list = callback;
218 }
219 callback = next;
220 }
221}
222
223static inline void check_free_callbacks(void)
224{
225 if (unlikely(gnttab_free_callback_list))
226 do_free_callbacks();
227}
228
229static void put_free_entry(grant_ref_t ref)
230{
231 unsigned long flags;
232 spin_lock_irqsave(&gnttab_list_lock, flags);
233 gnttab_entry(ref) = gnttab_free_head;
234 gnttab_free_head = ref;
235 gnttab_free_count++;
236 check_free_callbacks();
237 spin_unlock_irqrestore(&gnttab_list_lock, flags);
238}
239
0f9f5a95 240/*
b988b8ff 241 * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2.
0f9f5a95
AL
242 * Introducing a valid entry into the grant table:
243 * 1. Write ent->domid.
244 * 2. Write ent->frame:
245 * GTF_permit_access: Frame to which access is permitted.
246 * GTF_accept_transfer: Pseudo-phys frame slot being filled by new
247 * frame, or zero if none.
248 * 3. Write memory barrier (WMB).
249 * 4. Write ent->flags, inc. valid type.
250 */
251static void gnttab_update_entry_v1(grant_ref_t ref, domid_t domid,
252 unsigned long frame, unsigned flags)
ad9a8612 253{
0f9f5a95
AL
254 gnttab_shared.v1[ref].domid = domid;
255 gnttab_shared.v1[ref].frame = frame;
ad9a8612 256 wmb();
0f9f5a95 257 gnttab_shared.v1[ref].flags = flags;
ad9a8612
JF
258}
259
b988b8ff
JG
260static void gnttab_update_entry_v2(grant_ref_t ref, domid_t domid,
261 unsigned long frame, unsigned int flags)
262{
263 gnttab_shared.v2[ref].hdr.domid = domid;
264 gnttab_shared.v2[ref].full_page.frame = frame;
265 wmb(); /* Hypervisor concurrent accesses. */
266 gnttab_shared.v2[ref].hdr.flags = GTF_permit_access | flags;
267}
268
ad9a8612
JF
269/*
270 * Public grant-issuing interface functions
271 */
272void gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
273 unsigned long frame, int readonly)
274{
0f9f5a95 275 gnttab_interface->update_entry(ref, domid, frame,
ad9a8612
JF
276 GTF_permit_access | (readonly ? GTF_readonly : 0));
277}
278EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_ref);
279
280int gnttab_grant_foreign_access(domid_t domid, unsigned long frame,
281 int readonly)
282{
283 int ref;
284
285 ref = get_free_entries(1);
286 if (unlikely(ref < 0))
287 return -ENOSPC;
288
289 gnttab_grant_foreign_access_ref(ref, domid, frame, readonly);
290
291 return ref;
292}
293EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access);
294
b988b8ff
JG
295static void gnttab_update_subpage_entry_v2(grant_ref_t ref, domid_t domid,
296 unsigned long frame, int flags,
297 unsigned page_off, unsigned length)
298{
299 gnttab_shared.v2[ref].sub_page.frame = frame;
300 gnttab_shared.v2[ref].sub_page.page_off = page_off;
301 gnttab_shared.v2[ref].sub_page.length = length;
302 gnttab_shared.v2[ref].hdr.domid = domid;
303 wmb();
304 gnttab_shared.v2[ref].hdr.flags =
305 GTF_permit_access | GTF_sub_page | flags;
306}
307
308int gnttab_grant_foreign_access_subpage_ref(grant_ref_t ref, domid_t domid,
309 unsigned long frame, int flags,
310 unsigned page_off,
311 unsigned length)
312{
313 if (flags & (GTF_accept_transfer | GTF_reading |
314 GTF_writing | GTF_transitive))
315 return -EPERM;
316
317 if (gnttab_interface->update_subpage_entry == NULL)
318 return -ENOSYS;
319
320 gnttab_interface->update_subpage_entry(ref, domid, frame, flags,
321 page_off, length);
322
323 return 0;
324}
325EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_subpage_ref);
326
327int gnttab_grant_foreign_access_subpage(domid_t domid, unsigned long frame,
328 int flags, unsigned page_off,
329 unsigned length)
330{
331 int ref, rc;
332
333 ref = get_free_entries(1);
334 if (unlikely(ref < 0))
335 return -ENOSPC;
336
337 rc = gnttab_grant_foreign_access_subpage_ref(ref, domid, frame, flags,
338 page_off, length);
339 if (rc < 0) {
340 put_free_entry(ref);
341 return rc;
342 }
343
344 return ref;
345}
346EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_subpage);
347
348bool gnttab_subpage_grants_available(void)
349{
350 return gnttab_interface->update_subpage_entry != NULL;
351}
352EXPORT_SYMBOL_GPL(gnttab_subpage_grants_available);
353
354static void gnttab_update_trans_entry_v2(grant_ref_t ref, domid_t domid,
355 int flags, domid_t trans_domid,
356 grant_ref_t trans_gref)
357{
358 gnttab_shared.v2[ref].transitive.trans_domid = trans_domid;
359 gnttab_shared.v2[ref].transitive.gref = trans_gref;
360 gnttab_shared.v2[ref].hdr.domid = domid;
361 wmb();
362 gnttab_shared.v2[ref].hdr.flags =
363 GTF_permit_access | GTF_transitive | flags;
364}
365
366int gnttab_grant_foreign_access_trans_ref(grant_ref_t ref, domid_t domid,
367 int flags, domid_t trans_domid,
368 grant_ref_t trans_gref)
369{
370 if (flags & (GTF_accept_transfer | GTF_reading |
371 GTF_writing | GTF_sub_page))
372 return -EPERM;
373
374 if (gnttab_interface->update_trans_entry == NULL)
375 return -ENOSYS;
376
377 gnttab_interface->update_trans_entry(ref, domid, flags, trans_domid,
378 trans_gref);
379
380 return 0;
381}
382EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_trans_ref);
383
384int gnttab_grant_foreign_access_trans(domid_t domid, int flags,
385 domid_t trans_domid,
386 grant_ref_t trans_gref)
387{
388 int ref, rc;
389
390 ref = get_free_entries(1);
391 if (unlikely(ref < 0))
392 return -ENOSPC;
393
394 rc = gnttab_grant_foreign_access_trans_ref(ref, domid, flags,
395 trans_domid, trans_gref);
396 if (rc < 0) {
397 put_free_entry(ref);
398 return rc;
399 }
400
401 return ref;
402}
403EXPORT_SYMBOL_GPL(gnttab_grant_foreign_access_trans);
404
405bool gnttab_trans_grants_available(void)
406{
407 return gnttab_interface->update_trans_entry != NULL;
408}
409EXPORT_SYMBOL_GPL(gnttab_trans_grants_available);
410
0f9f5a95 411static int gnttab_query_foreign_access_v1(grant_ref_t ref)
ad9a8612 412{
0f9f5a95
AL
413 return gnttab_shared.v1[ref].flags & (GTF_reading|GTF_writing);
414}
ad9a8612 415
b988b8ff
JG
416static int gnttab_query_foreign_access_v2(grant_ref_t ref)
417{
418 return grstatus[ref] & (GTF_reading|GTF_writing);
419}
420
0f9f5a95
AL
421int gnttab_query_foreign_access(grant_ref_t ref)
422{
423 return gnttab_interface->query_foreign_access(ref);
ad9a8612
JF
424}
425EXPORT_SYMBOL_GPL(gnttab_query_foreign_access);
426
0f9f5a95 427static int gnttab_end_foreign_access_ref_v1(grant_ref_t ref, int readonly)
ad9a8612
JF
428{
429 u16 flags, nflags;
b1e495b2 430 u16 *pflags;
ad9a8612 431
b1e495b2
AL
432 pflags = &gnttab_shared.v1[ref].flags;
433 nflags = *pflags;
ad9a8612
JF
434 do {
435 flags = nflags;
569ca5b3 436 if (flags & (GTF_reading|GTF_writing))
ad9a8612 437 return 0;
b1e495b2 438 } while ((nflags = sync_cmpxchg(pflags, flags, 0)) != flags);
ad9a8612
JF
439
440 return 1;
441}
0f9f5a95 442
b988b8ff
JG
443static int gnttab_end_foreign_access_ref_v2(grant_ref_t ref, int readonly)
444{
445 gnttab_shared.v2[ref].hdr.flags = 0;
446 mb(); /* Concurrent access by hypervisor. */
447 if (grstatus[ref] & (GTF_reading|GTF_writing)) {
448 return 0;
449 } else {
450 /*
451 * The read of grstatus needs to have acquire semantics.
452 * On x86, reads already have that, and we just need to
453 * protect against compiler reorderings.
454 * On other architectures we may need a full barrier.
455 */
456#ifdef CONFIG_X86
457 barrier();
458#else
459 mb();
460#endif
461 }
462
463 return 1;
464}
465
569ca5b3 466static inline int _gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
0f9f5a95
AL
467{
468 return gnttab_interface->end_foreign_access_ref(ref, readonly);
469}
569ca5b3
JB
470
471int gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
472{
473 if (_gnttab_end_foreign_access_ref(ref, readonly))
474 return 1;
475 pr_warn("WARNING: g.e. %#x still in use!\n", ref);
476 return 0;
477}
ad9a8612
JF
478EXPORT_SYMBOL_GPL(gnttab_end_foreign_access_ref);
479
569ca5b3
JB
480struct deferred_entry {
481 struct list_head list;
482 grant_ref_t ref;
483 bool ro;
484 uint16_t warn_delay;
485 struct page *page;
486};
487static LIST_HEAD(deferred_list);
488static void gnttab_handle_deferred(unsigned long);
489static DEFINE_TIMER(deferred_timer, gnttab_handle_deferred, 0, 0);
490
491static void gnttab_handle_deferred(unsigned long unused)
492{
493 unsigned int nr = 10;
494 struct deferred_entry *first = NULL;
495 unsigned long flags;
496
497 spin_lock_irqsave(&gnttab_list_lock, flags);
498 while (nr--) {
499 struct deferred_entry *entry
500 = list_first_entry(&deferred_list,
501 struct deferred_entry, list);
502
503 if (entry == first)
504 break;
505 list_del(&entry->list);
506 spin_unlock_irqrestore(&gnttab_list_lock, flags);
507 if (_gnttab_end_foreign_access_ref(entry->ref, entry->ro)) {
508 put_free_entry(entry->ref);
509 if (entry->page) {
510 pr_debug("freeing g.e. %#x (pfn %#lx)\n",
511 entry->ref, page_to_pfn(entry->page));
512 __free_page(entry->page);
513 } else
514 pr_info("freeing g.e. %#x\n", entry->ref);
515 kfree(entry);
516 entry = NULL;
517 } else {
518 if (!--entry->warn_delay)
283c0972 519 pr_info("g.e. %#x still pending\n", entry->ref);
569ca5b3
JB
520 if (!first)
521 first = entry;
522 }
523 spin_lock_irqsave(&gnttab_list_lock, flags);
524 if (entry)
525 list_add_tail(&entry->list, &deferred_list);
526 else if (list_empty(&deferred_list))
527 break;
528 }
529 if (!list_empty(&deferred_list) && !timer_pending(&deferred_timer)) {
530 deferred_timer.expires = jiffies + HZ;
531 add_timer(&deferred_timer);
532 }
533 spin_unlock_irqrestore(&gnttab_list_lock, flags);
534}
535
536static void gnttab_add_deferred(grant_ref_t ref, bool readonly,
537 struct page *page)
538{
539 struct deferred_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
540 const char *what = KERN_WARNING "leaking";
541
542 if (entry) {
543 unsigned long flags;
544
545 entry->ref = ref;
546 entry->ro = readonly;
547 entry->page = page;
548 entry->warn_delay = 60;
549 spin_lock_irqsave(&gnttab_list_lock, flags);
550 list_add_tail(&entry->list, &deferred_list);
551 if (!timer_pending(&deferred_timer)) {
552 deferred_timer.expires = jiffies + HZ;
553 add_timer(&deferred_timer);
554 }
555 spin_unlock_irqrestore(&gnttab_list_lock, flags);
556 what = KERN_DEBUG "deferring";
557 }
558 printk("%s g.e. %#x (pfn %#lx)\n",
559 what, ref, page ? page_to_pfn(page) : -1);
560}
561
ad9a8612
JF
562void gnttab_end_foreign_access(grant_ref_t ref, int readonly,
563 unsigned long page)
564{
565 if (gnttab_end_foreign_access_ref(ref, readonly)) {
566 put_free_entry(ref);
567 if (page != 0)
568 free_page(page);
569ca5b3
JB
569 } else
570 gnttab_add_deferred(ref, readonly,
571 page ? virt_to_page(page) : NULL);
ad9a8612
JF
572}
573EXPORT_SYMBOL_GPL(gnttab_end_foreign_access);
574
575int gnttab_grant_foreign_transfer(domid_t domid, unsigned long pfn)
576{
577 int ref;
578
579 ref = get_free_entries(1);
580 if (unlikely(ref < 0))
581 return -ENOSPC;
582 gnttab_grant_foreign_transfer_ref(ref, domid, pfn);
583
584 return ref;
585}
586EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer);
587
588void gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid,
589 unsigned long pfn)
590{
0f9f5a95 591 gnttab_interface->update_entry(ref, domid, pfn, GTF_accept_transfer);
ad9a8612
JF
592}
593EXPORT_SYMBOL_GPL(gnttab_grant_foreign_transfer_ref);
594
0f9f5a95 595static unsigned long gnttab_end_foreign_transfer_ref_v1(grant_ref_t ref)
ad9a8612
JF
596{
597 unsigned long frame;
598 u16 flags;
b1e495b2
AL
599 u16 *pflags;
600
601 pflags = &gnttab_shared.v1[ref].flags;
ad9a8612
JF
602
603 /*
604 * If a transfer is not even yet started, try to reclaim the grant
605 * reference and return failure (== 0).
606 */
b1e495b2
AL
607 while (!((flags = *pflags) & GTF_transfer_committed)) {
608 if (sync_cmpxchg(pflags, flags, 0) == flags)
ad9a8612
JF
609 return 0;
610 cpu_relax();
611 }
612
613 /* If a transfer is in progress then wait until it is completed. */
614 while (!(flags & GTF_transfer_completed)) {
b1e495b2 615 flags = *pflags;
ad9a8612
JF
616 cpu_relax();
617 }
618
619 rmb(); /* Read the frame number /after/ reading completion status. */
0f9f5a95 620 frame = gnttab_shared.v1[ref].frame;
ad9a8612
JF
621 BUG_ON(frame == 0);
622
623 return frame;
624}
0f9f5a95 625
b988b8ff
JG
626static unsigned long gnttab_end_foreign_transfer_ref_v2(grant_ref_t ref)
627{
628 unsigned long frame;
629 u16 flags;
630 u16 *pflags;
631
632 pflags = &gnttab_shared.v2[ref].hdr.flags;
633
634 /*
635 * If a transfer is not even yet started, try to reclaim the grant
636 * reference and return failure (== 0).
637 */
638 while (!((flags = *pflags) & GTF_transfer_committed)) {
639 if (sync_cmpxchg(pflags, flags, 0) == flags)
640 return 0;
641 cpu_relax();
642 }
643
644 /* If a transfer is in progress then wait until it is completed. */
645 while (!(flags & GTF_transfer_completed)) {
646 flags = *pflags;
647 cpu_relax();
648 }
649
650 rmb(); /* Read the frame number /after/ reading completion status. */
651 frame = gnttab_shared.v2[ref].full_page.frame;
652 BUG_ON(frame == 0);
653
654 return frame;
655}
656
0f9f5a95
AL
657unsigned long gnttab_end_foreign_transfer_ref(grant_ref_t ref)
658{
659 return gnttab_interface->end_foreign_transfer_ref(ref);
660}
ad9a8612
JF
661EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer_ref);
662
663unsigned long gnttab_end_foreign_transfer(grant_ref_t ref)
664{
665 unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
666 put_free_entry(ref);
667 return frame;
668}
669EXPORT_SYMBOL_GPL(gnttab_end_foreign_transfer);
670
671void gnttab_free_grant_reference(grant_ref_t ref)
672{
673 put_free_entry(ref);
674}
675EXPORT_SYMBOL_GPL(gnttab_free_grant_reference);
676
677void gnttab_free_grant_references(grant_ref_t head)
678{
679 grant_ref_t ref;
680 unsigned long flags;
681 int count = 1;
682 if (head == GNTTAB_LIST_END)
683 return;
684 spin_lock_irqsave(&gnttab_list_lock, flags);
685 ref = head;
686 while (gnttab_entry(ref) != GNTTAB_LIST_END) {
687 ref = gnttab_entry(ref);
688 count++;
689 }
690 gnttab_entry(ref) = gnttab_free_head;
691 gnttab_free_head = head;
692 gnttab_free_count += count;
693 check_free_callbacks();
694 spin_unlock_irqrestore(&gnttab_list_lock, flags);
695}
696EXPORT_SYMBOL_GPL(gnttab_free_grant_references);
697
698int gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
699{
700 int h = get_free_entries(count);
701
702 if (h < 0)
703 return -ENOSPC;
704
705 *head = h;
706
707 return 0;
708}
709EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references);
710
711int gnttab_empty_grant_references(const grant_ref_t *private_head)
712{
713 return (*private_head == GNTTAB_LIST_END);
714}
715EXPORT_SYMBOL_GPL(gnttab_empty_grant_references);
716
717int gnttab_claim_grant_reference(grant_ref_t *private_head)
718{
719 grant_ref_t g = *private_head;
720 if (unlikely(g == GNTTAB_LIST_END))
721 return -ENOSPC;
722 *private_head = gnttab_entry(g);
723 return g;
724}
725EXPORT_SYMBOL_GPL(gnttab_claim_grant_reference);
726
727void gnttab_release_grant_reference(grant_ref_t *private_head,
728 grant_ref_t release)
729{
730 gnttab_entry(release) = *private_head;
731 *private_head = release;
732}
733EXPORT_SYMBOL_GPL(gnttab_release_grant_reference);
734
735void gnttab_request_free_callback(struct gnttab_free_callback *callback,
736 void (*fn)(void *), void *arg, u16 count)
737{
738 unsigned long flags;
5f338d90
RPM
739 struct gnttab_free_callback *cb;
740
ad9a8612 741 spin_lock_irqsave(&gnttab_list_lock, flags);
5f338d90
RPM
742
743 /* Check if the callback is already on the list */
744 cb = gnttab_free_callback_list;
745 while (cb) {
746 if (cb == callback)
747 goto out;
748 cb = cb->next;
749 }
750
ad9a8612
JF
751 callback->fn = fn;
752 callback->arg = arg;
753 callback->count = count;
754 callback->next = gnttab_free_callback_list;
755 gnttab_free_callback_list = callback;
756 check_free_callbacks();
757out:
758 spin_unlock_irqrestore(&gnttab_list_lock, flags);
759}
760EXPORT_SYMBOL_GPL(gnttab_request_free_callback);
761
762void gnttab_cancel_free_callback(struct gnttab_free_callback *callback)
763{
764 struct gnttab_free_callback **pcb;
765 unsigned long flags;
766
767 spin_lock_irqsave(&gnttab_list_lock, flags);
768 for (pcb = &gnttab_free_callback_list; *pcb; pcb = &(*pcb)->next) {
769 if (*pcb == callback) {
770 *pcb = callback->next;
771 break;
772 }
773 }
774 spin_unlock_irqrestore(&gnttab_list_lock, flags);
775}
776EXPORT_SYMBOL_GPL(gnttab_cancel_free_callback);
777
778static int grow_gnttab_list(unsigned int more_frames)
779{
780 unsigned int new_nr_grant_frames, extra_entries, i;
bbc60c18 781 unsigned int nr_glist_frames, new_nr_glist_frames;
ad9a8612 782
d0b4d64a
MW
783 BUG_ON(grefs_per_grant_frame == 0);
784
ad9a8612 785 new_nr_grant_frames = nr_grant_frames + more_frames;
d0b4d64a 786 extra_entries = more_frames * grefs_per_grant_frame;
ad9a8612 787
d0b4d64a 788 nr_glist_frames = (nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP;
bbc60c18 789 new_nr_glist_frames =
d0b4d64a 790 (new_nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP;
bbc60c18 791 for (i = nr_glist_frames; i < new_nr_glist_frames; i++) {
ad9a8612
JF
792 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_ATOMIC);
793 if (!gnttab_list[i])
794 goto grow_nomem;
795 }
796
797
d0b4d64a
MW
798 for (i = grefs_per_grant_frame * nr_grant_frames;
799 i < grefs_per_grant_frame * new_nr_grant_frames - 1; i++)
ad9a8612
JF
800 gnttab_entry(i) = i + 1;
801
802 gnttab_entry(i) = gnttab_free_head;
d0b4d64a 803 gnttab_free_head = grefs_per_grant_frame * nr_grant_frames;
ad9a8612
JF
804 gnttab_free_count += extra_entries;
805
806 nr_grant_frames = new_nr_grant_frames;
807
808 check_free_callbacks();
809
810 return 0;
811
812grow_nomem:
46e3626a 813 while (i-- > nr_glist_frames)
ad9a8612
JF
814 free_page((unsigned long) gnttab_list[i]);
815 return -ENOMEM;
816}
817
818static unsigned int __max_nr_grant_frames(void)
819{
820 struct gnttab_query_size query;
821 int rc;
822
823 query.dom = DOMID_SELF;
824
825 rc = HYPERVISOR_grant_table_op(GNTTABOP_query_size, &query, 1);
826 if ((rc < 0) || (query.status != GNTST_okay))
827 return 4; /* Legacy max supported number of frames */
828
829 return query.max_nr_frames;
830}
831
183d03cc 832unsigned int gnttab_max_grant_frames(void)
ad9a8612
JF
833{
834 unsigned int xen_max = __max_nr_grant_frames();
7f256020
KRW
835 static unsigned int boot_max_nr_grant_frames;
836
837 /* First time, initialize it properly. */
838 if (!boot_max_nr_grant_frames)
839 boot_max_nr_grant_frames = __max_nr_grant_frames();
ad9a8612
JF
840
841 if (xen_max > boot_max_nr_grant_frames)
842 return boot_max_nr_grant_frames;
843 return xen_max;
844}
183d03cc 845EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
ad9a8612 846
47c54205 847int gnttab_setup_auto_xlat_frames(phys_addr_t addr)
efaf30a3
KRW
848{
849 xen_pfn_t *pfn;
850 unsigned int max_nr_gframes = __max_nr_grant_frames();
851 unsigned int i;
852 void *vaddr;
853
854 if (xen_auto_xlat_grant_frames.count)
855 return -EINVAL;
856
5ed5451d 857 vaddr = xen_remap(addr, XEN_PAGE_SIZE * max_nr_gframes);
efaf30a3 858 if (vaddr == NULL) {
47c54205
JG
859 pr_warn("Failed to ioremap gnttab share frames (addr=%pa)!\n",
860 &addr);
efaf30a3
KRW
861 return -ENOMEM;
862 }
863 pfn = kcalloc(max_nr_gframes, sizeof(pfn[0]), GFP_KERNEL);
864 if (!pfn) {
865 xen_unmap(vaddr);
866 return -ENOMEM;
867 }
868 for (i = 0; i < max_nr_gframes; i++)
5ed5451d 869 pfn[i] = XEN_PFN_DOWN(addr) + i;
efaf30a3
KRW
870
871 xen_auto_xlat_grant_frames.vaddr = vaddr;
872 xen_auto_xlat_grant_frames.pfn = pfn;
873 xen_auto_xlat_grant_frames.count = max_nr_gframes;
874
875 return 0;
876}
877EXPORT_SYMBOL_GPL(gnttab_setup_auto_xlat_frames);
878
879void gnttab_free_auto_xlat_frames(void)
880{
881 if (!xen_auto_xlat_grant_frames.count)
882 return;
883 kfree(xen_auto_xlat_grant_frames.pfn);
884 xen_unmap(xen_auto_xlat_grant_frames.vaddr);
885
886 xen_auto_xlat_grant_frames.pfn = NULL;
887 xen_auto_xlat_grant_frames.count = 0;
888 xen_auto_xlat_grant_frames.vaddr = NULL;
889}
890EXPORT_SYMBOL_GPL(gnttab_free_auto_xlat_frames);
891
ff4b156f
DV
892/**
893 * gnttab_alloc_pages - alloc pages suitable for grant mapping into
894 * @nr_pages: number of pages to alloc
895 * @pages: returns the pages
896 */
897int gnttab_alloc_pages(int nr_pages, struct page **pages)
898{
8da7633f 899 int i;
ff4b156f
DV
900 int ret;
901
81b286e0 902 ret = alloc_xenballooned_pages(nr_pages, pages);
ff4b156f
DV
903 if (ret < 0)
904 return ret;
905
8da7633f
JH
906 for (i = 0; i < nr_pages; i++) {
907#if BITS_PER_LONG < 64
908 struct xen_page_foreign *foreign;
909
910 foreign = kzalloc(sizeof(*foreign), GFP_KERNEL);
911 if (!foreign) {
912 gnttab_free_pages(nr_pages, pages);
913 return -ENOMEM;
914 }
915 set_page_private(pages[i], (unsigned long)foreign);
916#endif
917 SetPagePrivate(pages[i]);
918 }
919
ff4b156f
DV
920 return 0;
921}
922EXPORT_SYMBOL(gnttab_alloc_pages);
923
924/**
925 * gnttab_free_pages - free pages allocated by gnttab_alloc_pages()
926 * @nr_pages; number of pages to free
927 * @pages: the pages
928 */
929void gnttab_free_pages(int nr_pages, struct page **pages)
930{
8da7633f
JH
931 int i;
932
933 for (i = 0; i < nr_pages; i++) {
934 if (PagePrivate(pages[i])) {
935#if BITS_PER_LONG < 64
936 kfree((void *)page_private(pages[i]));
937#endif
938 ClearPagePrivate(pages[i]);
939 }
940 }
ff4b156f
DV
941 free_xenballooned_pages(nr_pages, pages);
942}
943EXPORT_SYMBOL(gnttab_free_pages);
944
c571898f
ALC
945/* Handling of paged out grant targets (GNTST_eagain) */
946#define MAX_DELAY 256
947static inline void
948gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
949 const char *func)
950{
951 unsigned delay = 1;
952
953 do {
954 BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
955 if (*status == GNTST_eagain)
956 msleep(delay++);
957 } while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
958
959 if (delay >= MAX_DELAY) {
283c0972 960 pr_err("%s: %s eagain grant\n", func, current->comm);
c571898f
ALC
961 *status = GNTST_bad_page;
962 }
963}
964
965void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count)
966{
967 struct gnttab_map_grant_ref *op;
968
969 if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count))
970 BUG();
971 for (op = batch; op < batch + count; op++)
972 if (op->status == GNTST_eagain)
973 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op,
974 &op->status, __func__);
975}
976EXPORT_SYMBOL_GPL(gnttab_batch_map);
977
978void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count)
979{
980 struct gnttab_copy *op;
981
982 if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count))
983 BUG();
984 for (op = batch; op < batch + count; op++)
985 if (op->status == GNTST_eagain)
986 gnttab_retry_eagain_gop(GNTTABOP_copy, op,
987 &op->status, __func__);
988}
989EXPORT_SYMBOL_GPL(gnttab_batch_copy);
990
008c320a
JG
991void gnttab_foreach_grant_in_range(struct page *page,
992 unsigned int offset,
993 unsigned int len,
994 xen_grant_fn_t fn,
995 void *data)
996{
997 unsigned int goffset;
998 unsigned int glen;
999 unsigned long xen_pfn;
1000
1001 len = min_t(unsigned int, PAGE_SIZE - offset, len);
1002 goffset = xen_offset_in_page(offset);
1003
1004 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(offset);
1005
1006 while (len) {
1007 glen = min_t(unsigned int, XEN_PAGE_SIZE - goffset, len);
1008 fn(pfn_to_gfn(xen_pfn), goffset, glen, data);
1009
1010 goffset = 0;
1011 xen_pfn++;
1012 len -= glen;
1013 }
1014}
1015EXPORT_SYMBOL_GPL(gnttab_foreach_grant_in_range);
1016
f73314b2
JG
1017void gnttab_foreach_grant(struct page **pages,
1018 unsigned int nr_grefs,
1019 xen_grant_fn_t fn,
1020 void *data)
1021{
1022 unsigned int goffset = 0;
1023 unsigned long xen_pfn = 0;
1024 unsigned int i;
1025
1026 for (i = 0; i < nr_grefs; i++) {
1027 if ((i % XEN_PFN_PER_PAGE) == 0) {
1028 xen_pfn = page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
1029 goffset = 0;
1030 }
1031
1032 fn(pfn_to_gfn(xen_pfn), goffset, XEN_PAGE_SIZE, data);
1033
1034 goffset += XEN_PAGE_SIZE;
1035 xen_pfn++;
1036 }
1037}
1038
e85fc980 1039int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
c123799a 1040 struct gnttab_map_grant_ref *kmap_ops,
e85fc980 1041 struct page **pages, unsigned int count)
289b777e
SS
1042{
1043 int i, ret;
289b777e
SS
1044
1045 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
87f1d40a
JF
1046 if (ret)
1047 return ret;
289b777e 1048
8da7633f
JH
1049 for (i = 0; i < count; i++) {
1050 /* Retry eagain maps */
c571898f
ALC
1051 if (map_ops[i].status == GNTST_eagain)
1052 gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, map_ops + i,
1053 &map_ops[i].status, __func__);
1054
8da7633f
JH
1055 if (map_ops[i].status == GNTST_okay) {
1056 struct xen_page_foreign *foreign;
1057
1058 SetPageForeign(pages[i]);
1059 foreign = xen_page_foreign(pages[i]);
1060 foreign->domid = map_ops[i].dom;
1061 foreign->gref = map_ops[i].ref;
1062 }
1063 }
1064
1429d46d 1065 return set_foreign_p2m_mapping(map_ops, kmap_ops, pages, count);
289b777e
SS
1066}
1067EXPORT_SYMBOL_GPL(gnttab_map_refs);
1068
e85fc980 1069int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
853d0289 1070 struct gnttab_unmap_grant_ref *kunmap_ops,
e85fc980 1071 struct page **pages, unsigned int count)
289b777e 1072{
8da7633f 1073 unsigned int i;
1429d46d 1074 int ret;
289b777e
SS
1075
1076 ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
87f1d40a
JF
1077 if (ret)
1078 return ret;
1079
8da7633f
JH
1080 for (i = 0; i < count; i++)
1081 ClearPageForeign(pages[i]);
1082
853d0289 1083 return clear_foreign_p2m_mapping(unmap_ops, kunmap_ops, pages, count);
289b777e
SS
1084}
1085EXPORT_SYMBOL_GPL(gnttab_unmap_refs);
1086
3f9f1c67
JH
1087#define GNTTAB_UNMAP_REFS_DELAY 5
1088
1089static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
1090
1091static void gnttab_unmap_work(struct work_struct *work)
1092{
1093 struct gntab_unmap_queue_data
1094 *unmap_data = container_of(work,
1095 struct gntab_unmap_queue_data,
1096 gnttab_work.work);
1097 if (unmap_data->age != UINT_MAX)
1098 unmap_data->age++;
1099 __gnttab_unmap_refs_async(unmap_data);
1100}
1101
1102static void __gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
1103{
1104 int ret;
1105 int pc;
1106
1107 for (pc = 0; pc < item->count; pc++) {
1108 if (page_count(item->pages[pc]) > 1) {
1109 unsigned long delay = GNTTAB_UNMAP_REFS_DELAY * (item->age + 1);
1110 schedule_delayed_work(&item->gnttab_work,
1111 msecs_to_jiffies(delay));
1112 return;
1113 }
1114 }
1115
1116 ret = gnttab_unmap_refs(item->unmap_ops, item->kunmap_ops,
1117 item->pages, item->count);
1118 item->done(ret, item);
1119}
1120
1121void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
1122{
1123 INIT_DELAYED_WORK(&item->gnttab_work, gnttab_unmap_work);
1124 item->age = 0;
1125
1126 __gnttab_unmap_refs_async(item);
1127}
1128EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
1129
b44166cd
BL
1130static void unmap_refs_callback(int result,
1131 struct gntab_unmap_queue_data *data)
1132{
1133 struct unmap_refs_callback_data *d = data->data;
1134
1135 d->result = result;
1136 complete(&d->completion);
1137}
1138
1139int gnttab_unmap_refs_sync(struct gntab_unmap_queue_data *item)
1140{
1141 struct unmap_refs_callback_data data;
1142
1143 init_completion(&data.completion);
1144 item->data = &data;
1145 item->done = &unmap_refs_callback;
1146 gnttab_unmap_refs_async(item);
1147 wait_for_completion(&data.completion);
1148
1149 return data.result;
1150}
1151EXPORT_SYMBOL_GPL(gnttab_unmap_refs_sync);
1152
b988b8ff
JG
1153static unsigned int nr_status_frames(unsigned int nr_grant_frames)
1154{
1155 BUG_ON(grefs_per_grant_frame == 0);
1156 return (nr_grant_frames * grefs_per_grant_frame + SPP - 1) / SPP;
1157}
1158
ef32f892 1159static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
0f9f5a95
AL
1160{
1161 int rc;
1162
1163 rc = arch_gnttab_map_shared(frames, nr_gframes,
1164 gnttab_max_grant_frames(),
1165 &gnttab_shared.addr);
1166 BUG_ON(rc);
1167
1168 return 0;
1169}
1170
1171static void gnttab_unmap_frames_v1(void)
1172{
85ff6acb
AL
1173 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
1174}
1175
b988b8ff
JG
1176static int gnttab_map_frames_v2(xen_pfn_t *frames, unsigned int nr_gframes)
1177{
1178 uint64_t *sframes;
1179 unsigned int nr_sframes;
1180 struct gnttab_get_status_frames getframes;
1181 int rc;
1182
1183 nr_sframes = nr_status_frames(nr_gframes);
1184
1185 /* No need for kzalloc as it is initialized in following hypercall
1186 * GNTTABOP_get_status_frames.
1187 */
1188 sframes = kmalloc_array(nr_sframes, sizeof(uint64_t), GFP_ATOMIC);
1189 if (!sframes)
1190 return -ENOMEM;
1191
1192 getframes.dom = DOMID_SELF;
1193 getframes.nr_frames = nr_sframes;
1194 set_xen_guest_handle(getframes.frame_list, sframes);
1195
1196 rc = HYPERVISOR_grant_table_op(GNTTABOP_get_status_frames,
1197 &getframes, 1);
1198 if (rc == -ENOSYS) {
1199 kfree(sframes);
1200 return -ENOSYS;
1201 }
1202
1203 BUG_ON(rc || getframes.status);
1204
1205 rc = arch_gnttab_map_status(sframes, nr_sframes,
1206 nr_status_frames(gnttab_max_grant_frames()),
1207 &grstatus);
1208 BUG_ON(rc);
1209 kfree(sframes);
1210
1211 rc = arch_gnttab_map_shared(frames, nr_gframes,
1212 gnttab_max_grant_frames(),
1213 &gnttab_shared.addr);
1214 BUG_ON(rc);
1215
1216 return 0;
1217}
1218
1219static void gnttab_unmap_frames_v2(void)
1220{
1221 arch_gnttab_unmap(gnttab_shared.addr, nr_grant_frames);
1222 arch_gnttab_unmap(grstatus, nr_status_frames(nr_grant_frames));
1223}
1224
ad9a8612
JF
1225static int gnttab_map(unsigned int start_idx, unsigned int end_idx)
1226{
1227 struct gnttab_setup_table setup;
ef32f892 1228 xen_pfn_t *frames;
ad9a8612
JF
1229 unsigned int nr_gframes = end_idx + 1;
1230 int rc;
1231
6926f6d6 1232 if (xen_feature(XENFEAT_auto_translated_physmap)) {
183d03cc
SS
1233 struct xen_add_to_physmap xatp;
1234 unsigned int i = end_idx;
1235 rc = 0;
efaf30a3 1236 BUG_ON(xen_auto_xlat_grant_frames.count < nr_gframes);
183d03cc
SS
1237 /*
1238 * Loop backwards, so that the first hypercall has the largest
1239 * index, ensuring that the table will grow only once.
1240 */
1241 do {
1242 xatp.domid = DOMID_SELF;
1243 xatp.idx = i;
1244 xatp.space = XENMAPSPACE_grant_table;
efaf30a3 1245 xatp.gpfn = xen_auto_xlat_grant_frames.pfn[i];
183d03cc
SS
1246 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
1247 if (rc != 0) {
283c0972
JP
1248 pr_warn("grant table add_to_physmap failed, err=%d\n",
1249 rc);
183d03cc
SS
1250 break;
1251 }
1252 } while (i-- > start_idx);
1253
1254 return rc;
1255 }
1256
85ff6acb
AL
1257 /* No need for kzalloc as it is initialized in following hypercall
1258 * GNTTABOP_setup_table.
1259 */
ad9a8612
JF
1260 frames = kmalloc(nr_gframes * sizeof(unsigned long), GFP_ATOMIC);
1261 if (!frames)
1262 return -ENOMEM;
1263
1264 setup.dom = DOMID_SELF;
1265 setup.nr_frames = nr_gframes;
87e27cf6 1266 set_xen_guest_handle(setup.frame_list, frames);
ad9a8612
JF
1267
1268 rc = HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1);
1269 if (rc == -ENOSYS) {
1270 kfree(frames);
1271 return -ENOSYS;
1272 }
1273
1274 BUG_ON(rc || setup.status);
1275
0f9f5a95 1276 rc = gnttab_interface->map_frames(frames, nr_gframes);
ad9a8612
JF
1277
1278 kfree(frames);
1279
0f9f5a95
AL
1280 return rc;
1281}
1282
86fc2136 1283static const struct gnttab_ops gnttab_v1_ops = {
0f9f5a95
AL
1284 .map_frames = gnttab_map_frames_v1,
1285 .unmap_frames = gnttab_unmap_frames_v1,
1286 .update_entry = gnttab_update_entry_v1,
1287 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v1,
1288 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v1,
1289 .query_foreign_access = gnttab_query_foreign_access_v1,
1290};
1291
b988b8ff
JG
1292static const struct gnttab_ops gnttab_v2_ops = {
1293 .map_frames = gnttab_map_frames_v2,
1294 .unmap_frames = gnttab_unmap_frames_v2,
1295 .update_entry = gnttab_update_entry_v2,
1296 .end_foreign_access_ref = gnttab_end_foreign_access_ref_v2,
1297 .end_foreign_transfer_ref = gnttab_end_foreign_transfer_ref_v2,
1298 .query_foreign_access = gnttab_query_foreign_access_v2,
1299 .update_subpage_entry = gnttab_update_subpage_entry_v2,
1300 .update_trans_entry = gnttab_update_trans_entry_v2,
1301};
1302
0f9f5a95
AL
1303static void gnttab_request_version(void)
1304{
b988b8ff
JG
1305 int rc;
1306 struct gnttab_set_version gsv;
85ff6acb 1307
b988b8ff
JG
1308 gsv.version = 1;
1309
1310 rc = HYPERVISOR_grant_table_op(GNTTABOP_set_version, &gsv, 1);
1311 if (rc == 0 && gsv.version == 2) {
1312 grant_table_version = 2;
1313 grefs_per_grant_frame = XEN_PAGE_SIZE /
1314 sizeof(union grant_entry_v2);
1315 gnttab_interface = &gnttab_v2_ops;
1316 } else if (grant_table_version == 2) {
1317 /*
1318 * If we've already used version 2 features,
1319 * but then suddenly discover that they're not
1320 * available (e.g. migrating to an older
1321 * version of Xen), almost unbounded badness
1322 * can happen.
1323 */
1324 panic("we need grant tables version 2, but only version 1 is available");
1325 } else {
1326 grant_table_version = 1;
1327 grefs_per_grant_frame = XEN_PAGE_SIZE /
1328 sizeof(struct grant_entry_v1);
1329 gnttab_interface = &gnttab_v1_ops;
1330 }
283c0972 1331 pr_info("Grant tables using version %d layout\n", grant_table_version);
ad9a8612
JF
1332}
1333
d0b4d64a 1334static int gnttab_setup(void)
ad9a8612 1335{
183d03cc
SS
1336 unsigned int max_nr_gframes;
1337
1338 max_nr_gframes = gnttab_max_grant_frames();
1339 if (max_nr_gframes < nr_grant_frames)
ad9a8612 1340 return -ENOSYS;
183d03cc 1341
45684753 1342 if (xen_feature(XENFEAT_auto_translated_physmap) && gnttab_shared.addr == NULL) {
efaf30a3 1343 gnttab_shared.addr = xen_auto_xlat_grant_frames.vaddr;
0f9f5a95 1344 if (gnttab_shared.addr == NULL) {
efaf30a3
KRW
1345 pr_warn("gnttab share frames (addr=0x%08lx) is not mapped!\n",
1346 (unsigned long)xen_auto_xlat_grant_frames.vaddr);
183d03cc
SS
1347 return -ENOMEM;
1348 }
1349 }
45684753 1350 return gnttab_map(0, nr_grant_frames - 1);
ad9a8612
JF
1351}
1352
d0b4d64a
MW
1353int gnttab_resume(void)
1354{
1355 gnttab_request_version();
1356 return gnttab_setup();
1357}
1358
0e91398f 1359int gnttab_suspend(void)
ad9a8612 1360{
13cd36a3
DV
1361 if (!xen_feature(XENFEAT_auto_translated_physmap))
1362 gnttab_interface->unmap_frames();
ad9a8612
JF
1363 return 0;
1364}
1365
1366static int gnttab_expand(unsigned int req_entries)
1367{
1368 int rc;
1369 unsigned int cur, extra;
1370
d0b4d64a 1371 BUG_ON(grefs_per_grant_frame == 0);
ad9a8612 1372 cur = nr_grant_frames;
d0b4d64a
MW
1373 extra = ((req_entries + (grefs_per_grant_frame-1)) /
1374 grefs_per_grant_frame);
29d11cfd
WW
1375 if (cur + extra > gnttab_max_grant_frames()) {
1376 pr_warn_ratelimited("xen/grant-table: max_grant_frames reached"
1377 " cur=%u extra=%u limit=%u"
1378 " gnttab_free_count=%u req_entries=%u\n",
1379 cur, extra, gnttab_max_grant_frames(),
1380 gnttab_free_count, req_entries);
ad9a8612 1381 return -ENOSPC;
29d11cfd 1382 }
ad9a8612
JF
1383
1384 rc = gnttab_map(cur, cur + extra - 1);
1385 if (rc == 0)
1386 rc = grow_gnttab_list(extra);
1387
1388 return rc;
1389}
1390
183d03cc 1391int gnttab_init(void)
ad9a8612
JF
1392{
1393 int i;
162e3717 1394 unsigned long max_nr_grant_frames;
bbc60c18 1395 unsigned int max_nr_glist_frames, nr_glist_frames;
ad9a8612 1396 unsigned int nr_init_grefs;
6b5e7d9e 1397 int ret;
ad9a8612 1398
d0b4d64a 1399 gnttab_request_version();
162e3717 1400 max_nr_grant_frames = gnttab_max_grant_frames();
ad9a8612 1401 nr_grant_frames = 1;
ad9a8612
JF
1402
1403 /* Determine the maximum number of frames required for the
1404 * grant reference free list on the current hypervisor.
1405 */
d0b4d64a 1406 BUG_ON(grefs_per_grant_frame == 0);
162e3717 1407 max_nr_glist_frames = (max_nr_grant_frames *
d0b4d64a 1408 grefs_per_grant_frame / RPP);
ad9a8612
JF
1409
1410 gnttab_list = kmalloc(max_nr_glist_frames * sizeof(grant_ref_t *),
1411 GFP_KERNEL);
1412 if (gnttab_list == NULL)
1413 return -ENOMEM;
1414
d0b4d64a 1415 nr_glist_frames = (nr_grant_frames * grefs_per_grant_frame + RPP - 1) / RPP;
bbc60c18 1416 for (i = 0; i < nr_glist_frames; i++) {
ad9a8612 1417 gnttab_list[i] = (grant_ref_t *)__get_free_page(GFP_KERNEL);
6b5e7d9e
JL
1418 if (gnttab_list[i] == NULL) {
1419 ret = -ENOMEM;
ad9a8612 1420 goto ini_nomem;
6b5e7d9e 1421 }
ad9a8612
JF
1422 }
1423
b988b8ff
JG
1424 ret = arch_gnttab_init(max_nr_grant_frames,
1425 nr_status_frames(max_nr_grant_frames));
162e3717
DV
1426 if (ret < 0)
1427 goto ini_nomem;
1428
d0b4d64a 1429 if (gnttab_setup() < 0) {
6b5e7d9e
JL
1430 ret = -ENODEV;
1431 goto ini_nomem;
1432 }
ad9a8612 1433
d0b4d64a 1434 nr_init_grefs = nr_grant_frames * grefs_per_grant_frame;
ad9a8612
JF
1435
1436 for (i = NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++)
1437 gnttab_entry(i) = i + 1;
1438
1439 gnttab_entry(nr_init_grefs - 1) = GNTTAB_LIST_END;
1440 gnttab_free_count = nr_init_grefs - NR_RESERVED_ENTRIES;
1441 gnttab_free_head = NR_RESERVED_ENTRIES;
1442
1443 printk("Grant table initialized\n");
1444 return 0;
1445
1446 ini_nomem:
1447 for (i--; i >= 0; i--)
1448 free_page((unsigned long)gnttab_list[i]);
1449 kfree(gnttab_list);
6b5e7d9e 1450 return ret;
ad9a8612 1451}
183d03cc
SS
1452EXPORT_SYMBOL_GPL(gnttab_init);
1453
345a5255 1454static int __gnttab_init(void)
183d03cc 1455{
8613d78a
BO
1456 if (!xen_domain())
1457 return -ENODEV;
1458
183d03cc 1459 /* Delay grant-table initialization in the PV on HVM case */
8613d78a 1460 if (xen_hvm_domain() && !xen_pvh_domain())
183d03cc
SS
1461 return 0;
1462
183d03cc
SS
1463 return gnttab_init();
1464}
6926f6d6
KRW
1465/* Starts after core_initcall so that xen_pvh_gnttab_setup can be called
1466 * beforehand to initialize xen_auto_xlat_grant_frames. */
1467core_initcall_sync(__gnttab_init);