2 * Copyright (c) 2015, Linaro Limited, Shannon Zhao
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/platform_device.h>
18 #include <linux/acpi.h>
21 #include <xen/interface/memory.h>
22 #include <asm/xen/hypervisor.h>
23 #include <asm/xen/hypercall.h>
25 static int xen_unmap_device_mmio(const struct resource
*resources
,
28 unsigned int i
, j
, nr
;
30 const struct resource
*r
;
31 struct xen_remove_from_physmap xrp
;
33 for (i
= 0; i
< count
; i
++) {
35 nr
= DIV_ROUND_UP(resource_size(r
), XEN_PAGE_SIZE
);
36 if ((resource_type(r
) != IORESOURCE_MEM
) || (nr
== 0))
39 for (j
= 0; j
< nr
; j
++) {
40 xrp
.domid
= DOMID_SELF
;
41 xrp
.gpfn
= XEN_PFN_DOWN(r
->start
) + j
;
42 rc
= HYPERVISOR_memory_op(XENMEM_remove_from_physmap
,
52 static int xen_map_device_mmio(const struct resource
*resources
,
55 unsigned int i
, j
, nr
;
57 const struct resource
*r
;
62 for (i
= 0; i
< count
; i
++) {
63 struct xen_add_to_physmap_range xatp
= {
65 .space
= XENMAPSPACE_dev_mmio
69 nr
= DIV_ROUND_UP(resource_size(r
), XEN_PAGE_SIZE
);
70 if ((resource_type(r
) != IORESOURCE_MEM
) || (nr
== 0))
73 gpfns
= kcalloc(nr
, sizeof(xen_pfn_t
), GFP_KERNEL
);
74 idxs
= kcalloc(nr
, sizeof(xen_ulong_t
), GFP_KERNEL
);
75 errs
= kcalloc(nr
, sizeof(int), GFP_KERNEL
);
76 if (!gpfns
|| !idxs
|| !errs
) {
84 for (j
= 0; j
< nr
; j
++) {
86 * The regions are always mapped 1:1 to DOM0 and this is
87 * fine because the memory map for DOM0 is the same as
88 * the host (except for the RAM).
90 gpfns
[j
] = XEN_PFN_DOWN(r
->start
) + j
;
91 idxs
[j
] = XEN_PFN_DOWN(r
->start
) + j
;
96 set_xen_guest_handle(xatp
.gpfns
, gpfns
);
97 set_xen_guest_handle(xatp
.idxs
, idxs
);
98 set_xen_guest_handle(xatp
.errs
, errs
);
100 rc
= HYPERVISOR_memory_op(XENMEM_add_to_physmap_range
, &xatp
);
111 xen_unmap_device_mmio(resources
, i
);
115 static int xen_platform_notifier(struct notifier_block
*nb
,
116 unsigned long action
, void *data
)
118 struct platform_device
*pdev
= to_platform_device(data
);
121 if (pdev
->num_resources
== 0 || pdev
->resource
== NULL
)
125 case BUS_NOTIFY_ADD_DEVICE
:
126 r
= xen_map_device_mmio(pdev
->resource
, pdev
->num_resources
);
128 case BUS_NOTIFY_DEL_DEVICE
:
129 r
= xen_unmap_device_mmio(pdev
->resource
, pdev
->num_resources
);
135 dev_err(&pdev
->dev
, "Platform: Failed to %s device %s MMIO!\n",
136 action
== BUS_NOTIFY_ADD_DEVICE
? "map" :
137 (action
== BUS_NOTIFY_DEL_DEVICE
? "unmap" : "?"),
143 static struct notifier_block platform_device_nb
= {
144 .notifier_call
= xen_platform_notifier
,
147 static int __init
register_xen_platform_notifier(void)
149 if (!xen_initial_domain() || acpi_disabled
)
152 return bus_register_notifier(&platform_bus_type
, &platform_device_nb
);
155 arch_initcall(register_xen_platform_notifier
);
157 #ifdef CONFIG_ARM_AMBA
158 #include <linux/amba/bus.h>
160 static int xen_amba_notifier(struct notifier_block
*nb
,
161 unsigned long action
, void *data
)
163 struct amba_device
*adev
= to_amba_device(data
);
167 case BUS_NOTIFY_ADD_DEVICE
:
168 r
= xen_map_device_mmio(&adev
->res
, 1);
170 case BUS_NOTIFY_DEL_DEVICE
:
171 r
= xen_unmap_device_mmio(&adev
->res
, 1);
177 dev_err(&adev
->dev
, "AMBA: Failed to %s device %s MMIO!\n",
178 action
== BUS_NOTIFY_ADD_DEVICE
? "map" :
179 (action
== BUS_NOTIFY_DEL_DEVICE
? "unmap" : "?"),
180 adev
->dev
.init_name
);
185 static struct notifier_block amba_device_nb
= {
186 .notifier_call
= xen_amba_notifier
,
189 static int __init
register_xen_amba_notifier(void)
191 if (!xen_initial_domain() || acpi_disabled
)
194 return bus_register_notifier(&amba_bustype
, &amba_device_nb
);
197 arch_initcall(register_xen_amba_notifier
);