^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2015, Linaro Limited, Shannon Zhao
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <xen/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <xen/interface/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/xen/hypervisor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static int xen_unmap_device_mmio(const struct resource *resources,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) unsigned int i, j, nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const struct resource *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct xen_remove_from_physmap xrp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) r = &resources[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) nr = DIV_ROUND_UP(resource_size(r), XEN_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if ((resource_type(r) != IORESOURCE_MEM) || (nr == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) for (j = 0; j < nr; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) xrp.domid = DOMID_SELF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) xrp.gpfn = XEN_PFN_DOWN(r->start) + j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) rc = HYPERVISOR_memory_op(XENMEM_remove_from_physmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) &xrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int xen_map_device_mmio(const struct resource *resources,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int i, j, nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) const struct resource *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) xen_pfn_t *gpfns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) xen_ulong_t *idxs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int *errs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct xen_add_to_physmap_range xatp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .domid = DOMID_SELF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .space = XENMAPSPACE_dev_mmio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) r = &resources[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) nr = DIV_ROUND_UP(resource_size(r), XEN_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if ((resource_type(r) != IORESOURCE_MEM) || (nr == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) gpfns = kcalloc(nr, sizeof(xen_pfn_t), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) idxs = kcalloc(nr, sizeof(xen_ulong_t), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) errs = kcalloc(nr, sizeof(int), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!gpfns || !idxs || !errs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) kfree(gpfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) kfree(idxs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) kfree(errs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) for (j = 0; j < nr; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * The regions are always mapped 1:1 to DOM0 and this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * fine because the memory map for DOM0 is the same as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * the host (except for the RAM).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) gpfns[j] = XEN_PFN_DOWN(r->start) + j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) idxs[j] = XEN_PFN_DOWN(r->start) + j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) xatp.size = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) set_xen_guest_handle(xatp.gpfns, gpfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) set_xen_guest_handle(xatp.idxs, idxs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) set_xen_guest_handle(xatp.errs, errs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) kfree(gpfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) kfree(idxs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) kfree(errs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) xen_unmap_device_mmio(resources, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int xen_platform_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct platform_device *pdev = to_platform_device(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (pdev->num_resources == 0 || pdev->resource == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) case BUS_NOTIFY_ADD_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) r = xen_map_device_mmio(pdev->resource, pdev->num_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) case BUS_NOTIFY_DEL_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) r = xen_unmap_device_mmio(pdev->resource, pdev->num_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dev_err(&pdev->dev, "Platform: Failed to %s device %s MMIO!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) action == BUS_NOTIFY_ADD_DEVICE ? "map" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) (action == BUS_NOTIFY_DEL_DEVICE ? "unmap" : "?"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static struct notifier_block platform_device_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .notifier_call = xen_platform_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int __init register_xen_platform_notifier(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (!xen_initial_domain() || acpi_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return bus_register_notifier(&platform_bus_type, &platform_device_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) arch_initcall(register_xen_platform_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #ifdef CONFIG_ARM_AMBA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #include <linux/amba/bus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int xen_amba_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct amba_device *adev = to_amba_device(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) case BUS_NOTIFY_ADD_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) r = xen_map_device_mmio(&adev->res, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) case BUS_NOTIFY_DEL_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) r = xen_unmap_device_mmio(&adev->res, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) dev_err(&adev->dev, "AMBA: Failed to %s device %s MMIO!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) action == BUS_NOTIFY_ADD_DEVICE ? "map" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) (action == BUS_NOTIFY_DEL_DEVICE ? "unmap" : "?"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) adev->dev.init_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static struct notifier_block amba_device_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .notifier_call = xen_amba_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int __init register_xen_amba_notifier(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!xen_initial_domain() || acpi_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return bus_register_notifier(&amba_bustype, &amba_device_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) arch_initcall(register_xen_amba_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #endif