Merge branch 'topic/fix/hda' into for-linus
[sfrench/cifs-2.6.git] / arch / ia64 / xen / xencomm.c
1 /*
2  * Copyright (C) 2006 Hollis Blanchard <hollisb@us.ibm.com>, IBM Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  */
18
19 #include <linux/mm.h>
20
21 static unsigned long kernel_virtual_offset;
22 static int is_xencomm_initialized;
23
24 /* for xen early printk. It uses console io hypercall which uses xencomm.
25  * However early printk may use it before xencomm initialization.
26  */
27 int
28 xencomm_is_initialized(void)
29 {
30         return is_xencomm_initialized;
31 }
32
33 void
34 xencomm_initialize(void)
35 {
36         kernel_virtual_offset = KERNEL_START - ia64_tpa(KERNEL_START);
37         is_xencomm_initialized = 1;
38 }
39
40 /* Translate virtual address to physical address.  */
41 unsigned long
42 xencomm_vtop(unsigned long vaddr)
43 {
44         struct page *page;
45         struct vm_area_struct *vma;
46
47         if (vaddr == 0)
48                 return 0UL;
49
50         if (REGION_NUMBER(vaddr) == 5) {
51                 pgd_t *pgd;
52                 pud_t *pud;
53                 pmd_t *pmd;
54                 pte_t *ptep;
55
56                 /* On ia64, TASK_SIZE refers to current.  It is not initialized
57                    during boot.
58                    Furthermore the kernel is relocatable and __pa() doesn't
59                    work on  addresses.  */
60                 if (vaddr >= KERNEL_START
61                     && vaddr < (KERNEL_START + KERNEL_TR_PAGE_SIZE))
62                         return vaddr - kernel_virtual_offset;
63
64                 /* In kernel area -- virtually mapped.  */
65                 pgd = pgd_offset_k(vaddr);
66                 if (pgd_none(*pgd) || pgd_bad(*pgd))
67                         return ~0UL;
68
69                 pud = pud_offset(pgd, vaddr);
70                 if (pud_none(*pud) || pud_bad(*pud))
71                         return ~0UL;
72
73                 pmd = pmd_offset(pud, vaddr);
74                 if (pmd_none(*pmd) || pmd_bad(*pmd))
75                         return ~0UL;
76
77                 ptep = pte_offset_kernel(pmd, vaddr);
78                 if (!ptep)
79                         return ~0UL;
80
81                 return (pte_val(*ptep) & _PFN_MASK) | (vaddr & ~PAGE_MASK);
82         }
83
84         if (vaddr > TASK_SIZE) {
85                 /* percpu variables */
86                 if (REGION_NUMBER(vaddr) == 7 &&
87                     REGION_OFFSET(vaddr) >= (1ULL << IA64_MAX_PHYS_BITS))
88                         ia64_tpa(vaddr);
89
90                 /* kernel address */
91                 return __pa(vaddr);
92         }
93
94         /* XXX double-check (lack of) locking */
95         vma = find_extend_vma(current->mm, vaddr);
96         if (!vma)
97                 return ~0UL;
98
99         /* We assume the page is modified.  */
100         page = follow_page(vma, vaddr, FOLL_WRITE | FOLL_TOUCH);
101         if (!page)
102                 return ~0UL;
103
104         return (page_to_pfn(page) << PAGE_SHIFT) | (vaddr & ~PAGE_MASK);
105 }