1/******************************************************************************
2 * grant_table.c
3 * x86 specific part
4 *
5 * Granting foreign access to our memory reservation.
6 *
7 * Copyright (c) 2005-2006, Christopher Clark
8 * Copyright (c) 2004-2005, K A Fraser
9 * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
10 *                    VA Linux Systems Japan. Split out x86 specific part.
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#include <linux/sched.h>
38#include <linux/mm.h>
39#include <linux/slab.h>
40#include <linux/vmalloc.h>
41
42#include <xen/interface/xen.h>
43#include <xen/page.h>
44#include <xen/grant_table.h>
45#include <xen/xen.h>
46
47#include <asm/pgtable.h>
48
49static struct gnttab_vm_area {
50	struct vm_struct *area;
51	pte_t **ptes;
52} gnttab_shared_vm_area;
53
54int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes,
55			   unsigned long max_nr_gframes,
56			   void **__shared)
57{
58	void *shared = *__shared;
59	unsigned long addr;
60	unsigned long i;
61
62	if (shared == NULL)
63		*__shared = shared = gnttab_shared_vm_area.area->addr;
64
65	addr = (unsigned long)shared;
66
67	for (i = 0; i < nr_gframes; i++) {
68		set_pte_at(&init_mm, addr, gnttab_shared_vm_area.ptes[i],
69			   mfn_pte(frames[i], PAGE_KERNEL));
70		addr += PAGE_SIZE;
71	}
72
73	return 0;
74}
75
76void arch_gnttab_unmap(void *shared, unsigned long nr_gframes)
77{
78	unsigned long addr;
79	unsigned long i;
80
81	addr = (unsigned long)shared;
82
83	for (i = 0; i < nr_gframes; i++) {
84		set_pte_at(&init_mm, addr, gnttab_shared_vm_area.ptes[i],
85			   __pte(0));
86		addr += PAGE_SIZE;
87	}
88}
89
90static int arch_gnttab_valloc(struct gnttab_vm_area *area, unsigned nr_frames)
91{
92	area->ptes = kmalloc(sizeof(pte_t *) * nr_frames, GFP_KERNEL);
93	if (area->ptes == NULL)
94		return -ENOMEM;
95
96	area->area = alloc_vm_area(PAGE_SIZE * nr_frames, area->ptes);
97	if (area->area == NULL) {
98		kfree(area->ptes);
99		return -ENOMEM;
100	}
101
102	return 0;
103}
104
105int arch_gnttab_init(unsigned long nr_shared)
106{
107	if (!xen_pv_domain())
108		return 0;
109
110	return arch_gnttab_valloc(&gnttab_shared_vm_area, nr_shared);
111}
112
113#ifdef CONFIG_XEN_PVH
114#include <xen/balloon.h>
115#include <xen/events.h>
116#include <linux/slab.h>
117static int __init xlated_setup_gnttab_pages(void)
118{
119	struct page **pages;
120	xen_pfn_t *pfns;
121	void *vaddr;
122	int rc;
123	unsigned int i;
124	unsigned long nr_grant_frames = gnttab_max_grant_frames();
125
126	BUG_ON(nr_grant_frames == 0);
127	pages = kcalloc(nr_grant_frames, sizeof(pages[0]), GFP_KERNEL);
128	if (!pages)
129		return -ENOMEM;
130
131	pfns = kcalloc(nr_grant_frames, sizeof(pfns[0]), GFP_KERNEL);
132	if (!pfns) {
133		kfree(pages);
134		return -ENOMEM;
135	}
136	rc = alloc_xenballooned_pages(nr_grant_frames, pages);
137	if (rc) {
138		pr_warn("%s Couldn't balloon alloc %ld pfns rc:%d\n", __func__,
139			nr_grant_frames, rc);
140		kfree(pages);
141		kfree(pfns);
142		return rc;
143	}
144	for (i = 0; i < nr_grant_frames; i++)
145		pfns[i] = page_to_pfn(pages[i]);
146
147	vaddr = vmap(pages, nr_grant_frames, 0, PAGE_KERNEL);
148	if (!vaddr) {
149		pr_warn("%s Couldn't map %ld pfns rc:%d\n", __func__,
150			nr_grant_frames, rc);
151		free_xenballooned_pages(nr_grant_frames, pages);
152		kfree(pages);
153		kfree(pfns);
154		return -ENOMEM;
155	}
156	kfree(pages);
157
158	xen_auto_xlat_grant_frames.pfn = pfns;
159	xen_auto_xlat_grant_frames.count = nr_grant_frames;
160	xen_auto_xlat_grant_frames.vaddr = vaddr;
161
162	return 0;
163}
164
165static int __init xen_pvh_gnttab_setup(void)
166{
167	if (!xen_pvh_domain())
168		return -ENODEV;
169
170	return xlated_setup_gnttab_pages();
171}
172/* Call it _before_ __gnttab_init as we need to initialize the
173 * xen_auto_xlat_grant_frames first. */
174core_initcall(xen_pvh_gnttab_setup);
175#endif
176