Isaku Yamahata | 7ad8631 | 2008-10-17 11:17:55 +0900 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * arch/ia64/xen/grant-table.c |
| 3 | * |
| 4 | * Copyright (c) 2006 Isaku Yamahata <yamahata at valinux co jp> |
| 5 | * VA Linux Systems Japan K.K. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/vmalloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Isaku Yamahata | 7ad8631 | 2008-10-17 11:17:55 +0900 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | |
| 28 | #include <xen/interface/xen.h> |
| 29 | #include <xen/interface/memory.h> |
| 30 | #include <xen/grant_table.h> |
| 31 | |
| 32 | #include <asm/xen/hypervisor.h> |
| 33 | |
| 34 | struct vm_struct *xen_alloc_vm_area(unsigned long size) |
| 35 | { |
| 36 | int order; |
| 37 | unsigned long virt; |
| 38 | unsigned long nr_pages; |
| 39 | struct vm_struct *area; |
| 40 | |
| 41 | order = get_order(size); |
| 42 | virt = __get_free_pages(GFP_KERNEL, order); |
| 43 | if (virt == 0) |
| 44 | goto err0; |
| 45 | nr_pages = 1 << order; |
| 46 | scrub_pages(virt, nr_pages); |
| 47 | |
| 48 | area = kmalloc(sizeof(*area), GFP_KERNEL); |
| 49 | if (area == NULL) |
| 50 | goto err1; |
| 51 | |
| 52 | area->flags = VM_IOREMAP; |
| 53 | area->addr = (void *)virt; |
| 54 | area->size = size; |
| 55 | area->pages = NULL; |
| 56 | area->nr_pages = nr_pages; |
| 57 | area->phys_addr = 0; /* xenbus_map_ring_valloc uses this field! */ |
| 58 | |
| 59 | return area; |
| 60 | |
| 61 | err1: |
| 62 | free_pages(virt, order); |
| 63 | err0: |
| 64 | return NULL; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(xen_alloc_vm_area); |
| 67 | |
| 68 | void xen_free_vm_area(struct vm_struct *area) |
| 69 | { |
| 70 | unsigned int order = get_order(area->size); |
| 71 | unsigned long i; |
| 72 | unsigned long phys_addr = __pa(area->addr); |
| 73 | |
| 74 | /* This area is used for foreign page mappping. |
| 75 | * So underlying machine page may not be assigned. */ |
| 76 | for (i = 0; i < (1 << order); i++) { |
| 77 | unsigned long ret; |
| 78 | unsigned long gpfn = (phys_addr >> PAGE_SHIFT) + i; |
| 79 | struct xen_memory_reservation reservation = { |
| 80 | .nr_extents = 1, |
| 81 | .address_bits = 0, |
| 82 | .extent_order = 0, |
| 83 | .domid = DOMID_SELF |
| 84 | }; |
| 85 | set_xen_guest_handle(reservation.extent_start, &gpfn); |
| 86 | ret = HYPERVISOR_memory_op(XENMEM_populate_physmap, |
| 87 | &reservation); |
| 88 | BUG_ON(ret != 1); |
| 89 | } |
| 90 | free_pages((unsigned long)area->addr, order); |
| 91 | kfree(area); |
| 92 | } |
| 93 | EXPORT_SYMBOL_GPL(xen_free_vm_area); |
| 94 | |
| 95 | |
| 96 | /**************************************************************************** |
| 97 | * grant table hack |
| 98 | * cmd: GNTTABOP_xxx |
| 99 | */ |
| 100 | |
| 101 | int arch_gnttab_map_shared(unsigned long *frames, unsigned long nr_gframes, |
| 102 | unsigned long max_nr_gframes, |
| 103 | struct grant_entry **__shared) |
| 104 | { |
| 105 | *__shared = __va(frames[0] << PAGE_SHIFT); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | void arch_gnttab_unmap_shared(struct grant_entry *shared, |
| 110 | unsigned long nr_gframes) |
| 111 | { |
| 112 | /* nothing */ |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | gnttab_map_grant_ref_pre(struct gnttab_map_grant_ref *uop) |
| 117 | { |
| 118 | uint32_t flags; |
| 119 | |
| 120 | flags = uop->flags; |
| 121 | |
| 122 | if (flags & GNTMAP_host_map) { |
| 123 | if (flags & GNTMAP_application_map) { |
| 124 | printk(KERN_DEBUG |
| 125 | "GNTMAP_application_map is not supported yet: " |
| 126 | "flags 0x%x\n", flags); |
| 127 | BUG(); |
| 128 | } |
| 129 | if (flags & GNTMAP_contains_pte) { |
| 130 | printk(KERN_DEBUG |
| 131 | "GNTMAP_contains_pte is not supported yet: " |
| 132 | "flags 0x%x\n", flags); |
| 133 | BUG(); |
| 134 | } |
| 135 | } else if (flags & GNTMAP_device_map) { |
| 136 | printk("GNTMAP_device_map is not supported yet 0x%x\n", flags); |
| 137 | BUG(); /* not yet. actually this flag is not used. */ |
| 138 | } else { |
| 139 | BUG(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | int |
| 144 | HYPERVISOR_grant_table_op(unsigned int cmd, void *uop, unsigned int count) |
| 145 | { |
| 146 | if (cmd == GNTTABOP_map_grant_ref) { |
| 147 | unsigned int i; |
| 148 | for (i = 0; i < count; i++) { |
| 149 | gnttab_map_grant_ref_pre( |
| 150 | (struct gnttab_map_grant_ref *)uop + i); |
| 151 | } |
| 152 | } |
| 153 | return xencomm_hypercall_grant_table_op(cmd, uop, count); |
| 154 | } |
| 155 | |
| 156 | EXPORT_SYMBOL(HYPERVISOR_grant_table_op); |