1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Linaro Limited, Shannon Zhao
6 #include <linux/platform_device.h>
7 #include <linux/acpi.h>
10 #include <xen/interface/memory.h>
11 #include <asm/xen/hypervisor.h>
12 #include <asm/xen/hypercall.h>
14 static int xen_unmap_device_mmio(const struct resource *resources,
17 unsigned int i, j, nr;
19 const struct resource *r;
20 struct xen_remove_from_physmap xrp;
22 for (i = 0; i < count; i++) {
24 nr = DIV_ROUND_UP(resource_size(r), XEN_PAGE_SIZE);
25 if ((resource_type(r) != IORESOURCE_MEM) || (nr == 0))
28 for (j = 0; j < nr; j++) {
29 xrp.domid = DOMID_SELF;
30 xrp.gpfn = XEN_PFN_DOWN(r->start) + j;
31 rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap,
41 static int xen_map_device_mmio(const struct resource *resources,
44 unsigned int i, j, nr;
46 const struct resource *r;
51 for (i = 0; i < count; i++) {
52 struct xen_add_to_physmap_range xatp = {
54 .space = XENMAPSPACE_dev_mmio
58 nr = DIV_ROUND_UP(resource_size(r), XEN_PAGE_SIZE);
59 if ((resource_type(r) != IORESOURCE_MEM) || (nr == 0))
62 gpfns = kcalloc(nr, sizeof(xen_pfn_t), GFP_KERNEL);
63 idxs = kcalloc(nr, sizeof(xen_ulong_t), GFP_KERNEL);
64 errs = kcalloc(nr, sizeof(int), GFP_KERNEL);
65 if (!gpfns || !idxs || !errs) {
73 for (j = 0; j < nr; j++) {
75 * The regions are always mapped 1:1 to DOM0 and this is
76 * fine because the memory map for DOM0 is the same as
77 * the host (except for the RAM).
79 gpfns[j] = XEN_PFN_DOWN(r->start) + j;
80 idxs[j] = XEN_PFN_DOWN(r->start) + j;
85 set_xen_guest_handle(xatp.gpfns, gpfns);
86 set_xen_guest_handle(xatp.idxs, idxs);
87 set_xen_guest_handle(xatp.errs, errs);
89 rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
100 xen_unmap_device_mmio(resources, i);
104 static int xen_platform_notifier(struct notifier_block *nb,
105 unsigned long action, void *data)
107 struct platform_device *pdev = to_platform_device(data);
110 if (pdev->num_resources == 0 || pdev->resource == NULL)
114 case BUS_NOTIFY_ADD_DEVICE:
115 r = xen_map_device_mmio(pdev->resource, pdev->num_resources);
117 case BUS_NOTIFY_DEL_DEVICE:
118 r = xen_unmap_device_mmio(pdev->resource, pdev->num_resources);
124 dev_err(&pdev->dev, "Platform: Failed to %s device %s MMIO!\n",
125 action == BUS_NOTIFY_ADD_DEVICE ? "map" :
126 (action == BUS_NOTIFY_DEL_DEVICE ? "unmap" : "?"),
132 static struct notifier_block platform_device_nb = {
133 .notifier_call = xen_platform_notifier,
136 static int __init register_xen_platform_notifier(void)
138 if (!xen_initial_domain() || acpi_disabled)
141 return bus_register_notifier(&platform_bus_type, &platform_device_nb);
144 arch_initcall(register_xen_platform_notifier);
146 #ifdef CONFIG_ARM_AMBA
147 #include <linux/amba/bus.h>
149 static int xen_amba_notifier(struct notifier_block *nb,
150 unsigned long action, void *data)
152 struct amba_device *adev = to_amba_device(data);
156 case BUS_NOTIFY_ADD_DEVICE:
157 r = xen_map_device_mmio(&adev->res, 1);
159 case BUS_NOTIFY_DEL_DEVICE:
160 r = xen_unmap_device_mmio(&adev->res, 1);
166 dev_err(&adev->dev, "AMBA: Failed to %s device %s MMIO!\n",
167 action == BUS_NOTIFY_ADD_DEVICE ? "map" :
168 (action == BUS_NOTIFY_DEL_DEVICE ? "unmap" : "?"),
169 adev->dev.init_name);
174 static struct notifier_block amba_device_nb = {
175 .notifier_call = xen_amba_notifier,
178 static int __init register_xen_amba_notifier(void)
180 if (!xen_initial_domain() || acpi_disabled)
183 return bus_register_notifier(&amba_bustype, &amba_device_nb);
186 arch_initcall(register_xen_amba_notifier);