Message ID | 1447754231-7772-6-git-send-email-shannon.zhao@linaro.org (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On 17/11/15 09:57, shannon.zhao@linaro.org wrote: > From: Shannon Zhao <shannon.zhao@linaro.org> > > Add a bus_notifier for platform bus device in order to map the device > mmio regions when DOM0 booting with ACPI. [...] > --- /dev/null > +++ b/drivers/xen/platform.c > @@ -0,0 +1,104 @@ > +/* > + * Copyright (c) 2015, Linaro Limited. > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms and conditions of the GNU General Public License, > + * version 2, as published by the Free Software Foundation. > + * > + * This program is distributed in the hope it will be useful, but WITHOUT > + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or > + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for > + * more details. > + * > + * You should have received a copy of the GNU General Public License along with > + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple > + * Place - Suite 330, Boston, MA 02111-1307 USA. "You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>." > + * > + * Author: Shannon Zhao <shannon.zhao@linaro.org> Omit this since it just becomes stale and git will know the correct authorship. > +static int xen_platform_notifier(struct notifier_block *nb, > + unsigned long action, void *data) > +{ > + struct platform_device *pdev = to_platform_device(data); > + int r = 0; > + > + if (!acpi_disabled && (action == BUS_NOTIFY_ADD_DEVICE)) > + r = xen_map_platform_device_mmio(pdev); Why even register the notifier if acpi_disabled? > + > + if (r) > + { > + dev_err(&pdev->dev, "Failed to add_to_physmap device (%s) mmio!\n", > + pdev->name); > + return NOTIFY_OK; Return NOTIFY_BAD here? The device will be unusable. > + } > + > + return NOTIFY_DONE; > +} > + > +static struct notifier_block platform_device_nb = { > + .notifier_call = xen_platform_notifier, > +}; > + > +static int __init register_xen_platform_notifier(void) > +{ > + if (!xen_initial_domain()) > + return 0; > + > + return bus_register_notifier(&platform_bus_type, &platform_device_nb); Why only platform busses? What about PCI devices? Where will they get added to dom0's physmap? David
On 2015/11/17 22:38, Konrad Rzeszutek Wilk wrote: > On Tue, Nov 17, 2015 at 05:57:03PM +0800, shannon.zhao@linaro.org wrote: >> From: Shannon Zhao <shannon.zhao@linaro.org> >> >> Add a bus_notifier for platform bus device in order to map the device >> mmio regions when DOM0 booting with ACPI. >> >> Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org> >> --- >> drivers/xen/Makefile | 1 + >> drivers/xen/platform.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ >> 2 files changed, 105 insertions(+) >> create mode 100644 drivers/xen/platform.c >> >> diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile >> index e293bc5..2f867e7 100644 >> --- a/drivers/xen/Makefile >> +++ b/drivers/xen/Makefile >> @@ -11,6 +11,7 @@ CFLAGS_features.o := $(nostackp) >> >> CFLAGS_efi.o += -fshort-wchar >> >> +dom0-$(CONFIG_ARM64) += platform.o >> dom0-$(CONFIG_PCI) += pci.o >> dom0-$(CONFIG_USB_SUPPORT) += dbgp.o >> dom0-$(CONFIG_XEN_ACPI) += acpi.o $(xen-pad-y) >> diff --git a/drivers/xen/platform.c b/drivers/xen/platform.c >> new file mode 100644 >> index 0000000..0f95e7f >> --- /dev/null >> +++ b/drivers/xen/platform.c >> @@ -0,0 +1,104 @@ >> +/* >> + * Copyright (c) 2015, Linaro Limited. >> + * >> + * This program is free software; you can redistribute it and/or modify it >> + * under the terms and conditions of the GNU General Public License, >> + * version 2, as published by the Free Software Foundation. >> + * >> + * This program is distributed in the hope it will be useful, but WITHOUT >> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or >> + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for >> + * more details. >> + * >> + * You should have received a copy of the GNU General Public License along with >> + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple >> + * Place - Suite 330, Boston, MA 02111-1307 USA. >> + * >> + * Author: Shannon Zhao <shannon.zhao@linaro.org> >> + */ >> + >> +#include <linux/platform_device.h> >> +#include <linux/acpi.h> >> +#include <xen/xen.h> >> +#include <xen/interface/memory.h> >> +#include <asm/xen/hypervisor.h> >> +#include <asm/xen/hypercall.h> >> + >> +static int xen_map_platform_device_mmio(struct platform_device *pdev) >> +{ >> + int i, rc = 0; > > unsigned int i; >> + >> + for (i = 0; i < pdev->num_resources; i++) >> + { >> + struct resource *r = &pdev->resource[i]; >> + >> + if (resource_type(r) == IORESOURCE_MEM) > > Make this: > > if (resource_type(r) != IORESOURCE_MEM) > continue; > >> + { > > And then you don't have to move all of this code to the right. > >> + int j; > unsigned int >> + int nr = DIV_ROUND_UP(resource_size(r), PAGE_SIZE); > > unsigned int >> + xen_pfn_t *gpfns = kmalloc(sizeof(xen_pfn_t) * nr, >> + GFP_KERNEL); >> + xen_ulong_t *idxs = kmalloc(sizeof(xen_ulong_t) * nr, >> + GFP_KERNEL); >> + int *errs = kmalloc(sizeof(int) * nr, GFP_KERNEL); > > You can use kzalloc. >> + struct xen_add_to_physmap_range xatp; > > This declaration of variables and then setting does not look like that > pretty to my mind. (And it is a headache to debug if for example 'nr' ends up > being zero for example). Or worst -1U since you had it as 'int'. > Yes, a little. Maybe I can add a check for nr. Would that be fine? Thanks,
On 2015/11/18 0:32, David Vrabel wrote: > On 17/11/15 09:57, shannon.zhao@linaro.org wrote: >> From: Shannon Zhao <shannon.zhao@linaro.org> >> >> Add a bus_notifier for platform bus device in order to map the device >> mmio regions when DOM0 booting with ACPI. > [...] >> --- /dev/null >> +++ b/drivers/xen/platform.c >> @@ -0,0 +1,104 @@ >> +/* >> + * Copyright (c) 2015, Linaro Limited. >> + * >> + * This program is free software; you can redistribute it and/or modify it >> + * under the terms and conditions of the GNU General Public License, >> + * version 2, as published by the Free Software Foundation. >> + * >> + * This program is distributed in the hope it will be useful, but WITHOUT >> + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or >> + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for >> + * more details. >> + * >> + * You should have received a copy of the GNU General Public License along with >> + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple >> + * Place - Suite 330, Boston, MA 02111-1307 USA. > > "You should have received a copy of the GNU General Public License > along with this program. If not, see <http://www.gnu.org/licenses/>." > >> + * >> + * Author: Shannon Zhao <shannon.zhao@linaro.org> > > Omit this since it just becomes stale and git will know the correct > authorship. > >> +static int xen_platform_notifier(struct notifier_block *nb, >> + unsigned long action, void *data) >> +{ >> + struct platform_device *pdev = to_platform_device(data); >> + int r = 0; >> + >> + if (!acpi_disabled && (action == BUS_NOTIFY_ADD_DEVICE)) >> + r = xen_map_platform_device_mmio(pdev); > > Why even register the notifier if acpi_disabled? > >> + >> + if (r) >> + { >> + dev_err(&pdev->dev, "Failed to add_to_physmap device (%s) mmio!\n", >> + pdev->name); >> + return NOTIFY_OK; > > Return NOTIFY_BAD here? The device will be unusable. > >> + } >> + >> + return NOTIFY_DONE; >> +} >> + >> +static struct notifier_block platform_device_nb = { >> + .notifier_call = xen_platform_notifier, >> +}; >> + >> +static int __init register_xen_platform_notifier(void) >> +{ >> + if (!xen_initial_domain()) >> + return 0; >> + >> + return bus_register_notifier(&platform_bus_type, &platform_device_nb); > > Why only platform busses? What about PCI devices? Where will they get > added to dom0's physmap? > I've said in the design doc[1] that "And for PCI bus devices, Linux could reuse the existing PCI bus_notifier like X86." Since currently Xen on ARM doesn't support PCI (IIRC) and if it adds the support in the future, we could make the codes in drivers/xen/pci.c common to ARM and x86 at that moment. Thanks, [1] http://lists.xen.org/archives/html/xen-devel/2015-11/msg00488.html
On Tue, 17 Nov 2015, shannon.zhao@linaro.org wrote: > From: Shannon Zhao <shannon.zhao@linaro.org> > > Add a bus_notifier for platform bus device in order to map the device > mmio regions when DOM0 booting with ACPI. > > Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org> > --- > drivers/xen/Makefile | 1 + > drivers/xen/platform.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 105 insertions(+) > create mode 100644 drivers/xen/platform.c > > diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile > index e293bc5..2f867e7 100644 > --- a/drivers/xen/Makefile > +++ b/drivers/xen/Makefile > @@ -11,6 +11,7 @@ CFLAGS_features.o := $(nostackp) > > CFLAGS_efi.o += -fshort-wchar > > +dom0-$(CONFIG_ARM64) += platform.o > dom0-$(CONFIG_PCI) += pci.o > dom0-$(CONFIG_USB_SUPPORT) += dbgp.o > dom0-$(CONFIG_XEN_ACPI) += acpi.o $(xen-pad-y) > diff --git a/drivers/xen/platform.c b/drivers/xen/platform.c > new file mode 100644 > index 0000000..0f95e7f > --- /dev/null > +++ b/drivers/xen/platform.c > @@ -0,0 +1,104 @@ > +/* > + * Copyright (c) 2015, Linaro Limited. > + * > + * This program is free software; you can redistribute it and/or modify it > + * under the terms and conditions of the GNU General Public License, > + * version 2, as published by the Free Software Foundation. > + * > + * This program is distributed in the hope it will be useful, but WITHOUT > + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or > + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for > + * more details. > + * > + * You should have received a copy of the GNU General Public License along with > + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple > + * Place - Suite 330, Boston, MA 02111-1307 USA. > + * > + * Author: Shannon Zhao <shannon.zhao@linaro.org> > + */ > + > +#include <linux/platform_device.h> > +#include <linux/acpi.h> > +#include <xen/xen.h> > +#include <xen/interface/memory.h> > +#include <asm/xen/hypervisor.h> > +#include <asm/xen/hypercall.h> > + > +static int xen_map_platform_device_mmio(struct platform_device *pdev) > +{ > + int i, rc = 0; > + > + for (i = 0; i < pdev->num_resources; i++) > + { > + struct resource *r = &pdev->resource[i]; > + > + if (resource_type(r) == IORESOURCE_MEM) > + { > + int j; > + int nr = DIV_ROUND_UP(resource_size(r), PAGE_SIZE); > + xen_pfn_t *gpfns = kmalloc(sizeof(xen_pfn_t) * nr, > + GFP_KERNEL); > + xen_ulong_t *idxs = kmalloc(sizeof(xen_ulong_t) * nr, > + GFP_KERNEL); > + int *errs = kmalloc(sizeof(int) * nr, GFP_KERNEL); please check for kmalloc returning NULL and return -ENOMEM > + struct xen_add_to_physmap_range xatp; > + > + for (j = 0; j < nr; j++) { > + gpfns[j] = (r->start >> PAGE_SHIFT) + j; > + idxs[j] = (r->start >> PAGE_SHIFT) + j; XEN_PFN_DOWN > + errs[j] = 0; > + } > + > + xatp.domid = DOMID_SELF; > + xatp.size = nr; > + xatp.space = XENMAPSPACE_dev_mmio; > + > + set_xen_guest_handle(xatp.gpfns, gpfns); > + set_xen_guest_handle(xatp.idxs, idxs); > + set_xen_guest_handle(xatp.errs, errs); > + > + rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, > + &xatp); > + kfree(gpfns); > + kfree(idxs); > + kfree(errs); > + if (rc != 0) > + return rc; > + } > + } > + > + return rc; > +} > + > +static int xen_platform_notifier(struct notifier_block *nb, > + unsigned long action, void *data) > +{ > + struct platform_device *pdev = to_platform_device(data); > + int r = 0; > + > + if (!acpi_disabled && (action == BUS_NOTIFY_ADD_DEVICE)) > + r = xen_map_platform_device_mmio(pdev); > + > + if (r) > + { > + dev_err(&pdev->dev, "Failed to add_to_physmap device (%s) mmio!\n", > + pdev->name); > + return NOTIFY_OK; > + } > + > + return NOTIFY_DONE; > +} > + > +static struct notifier_block platform_device_nb = { > + .notifier_call = xen_platform_notifier, > +}; > + > +static int __init register_xen_platform_notifier(void) > +{ > + if (!xen_initial_domain()) > + return 0; > + > + return bus_register_notifier(&platform_bus_type, &platform_device_nb); > +} > + > +arch_initcall(register_xen_platform_notifier); > -- > 2.1.0 >
diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile index e293bc5..2f867e7 100644 --- a/drivers/xen/Makefile +++ b/drivers/xen/Makefile @@ -11,6 +11,7 @@ CFLAGS_features.o := $(nostackp) CFLAGS_efi.o += -fshort-wchar +dom0-$(CONFIG_ARM64) += platform.o dom0-$(CONFIG_PCI) += pci.o dom0-$(CONFIG_USB_SUPPORT) += dbgp.o dom0-$(CONFIG_XEN_ACPI) += acpi.o $(xen-pad-y) diff --git a/drivers/xen/platform.c b/drivers/xen/platform.c new file mode 100644 index 0000000..0f95e7f --- /dev/null +++ b/drivers/xen/platform.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2015, Linaro Limited. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307 USA. + * + * Author: Shannon Zhao <shannon.zhao@linaro.org> + */ + +#include <linux/platform_device.h> +#include <linux/acpi.h> +#include <xen/xen.h> +#include <xen/interface/memory.h> +#include <asm/xen/hypervisor.h> +#include <asm/xen/hypercall.h> + +static int xen_map_platform_device_mmio(struct platform_device *pdev) +{ + int i, rc = 0; + + for (i = 0; i < pdev->num_resources; i++) + { + struct resource *r = &pdev->resource[i]; + + if (resource_type(r) == IORESOURCE_MEM) + { + int j; + int nr = DIV_ROUND_UP(resource_size(r), PAGE_SIZE); + xen_pfn_t *gpfns = kmalloc(sizeof(xen_pfn_t) * nr, + GFP_KERNEL); + xen_ulong_t *idxs = kmalloc(sizeof(xen_ulong_t) * nr, + GFP_KERNEL); + int *errs = kmalloc(sizeof(int) * nr, GFP_KERNEL); + struct xen_add_to_physmap_range xatp; + + for (j = 0; j < nr; j++) { + gpfns[j] = (r->start >> PAGE_SHIFT) + j; + idxs[j] = (r->start >> PAGE_SHIFT) + j; + errs[j] = 0; + } + + xatp.domid = DOMID_SELF; + xatp.size = nr; + xatp.space = XENMAPSPACE_dev_mmio; + + set_xen_guest_handle(xatp.gpfns, gpfns); + set_xen_guest_handle(xatp.idxs, idxs); + set_xen_guest_handle(xatp.errs, errs); + + rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, + &xatp); + kfree(gpfns); + kfree(idxs); + kfree(errs); + if (rc != 0) + return rc; + } + } + + return rc; +} + +static int xen_platform_notifier(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct platform_device *pdev = to_platform_device(data); + int r = 0; + + if (!acpi_disabled && (action == BUS_NOTIFY_ADD_DEVICE)) + r = xen_map_platform_device_mmio(pdev); + + if (r) + { + dev_err(&pdev->dev, "Failed to add_to_physmap device (%s) mmio!\n", + pdev->name); + return NOTIFY_OK; + } + + return NOTIFY_DONE; +} + +static struct notifier_block platform_device_nb = { + .notifier_call = xen_platform_notifier, +}; + +static int __init register_xen_platform_notifier(void) +{ + if (!xen_initial_domain()) + return 0; + + return bus_register_notifier(&platform_bus_type, &platform_device_nb); +} + +arch_initcall(register_xen_platform_notifier);