Home | History | Annotate | Line # | Download | only in xen
      1 /* $NetBSD: xenmem.c,v 1.5 2023/12/20 15:34:46 thorpej Exp $ */
      2 /*
      3  * Copyright (c) 2022 Manuel Bouyer.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  *
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: xenmem.c,v 1.5 2023/12/20 15:34:46 thorpej Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/extent.h>
     33 #include <sys/kmem.h>
     34 #include <uvm/uvm_physseg.h>
     35 
     36 #include <xen/xenmem.h>
     37 
     38 /*
     39  * Xen physical space management
     40  * The xenmem_ex extent manage the VM's pseudo-physical memory space.
     41  * This contains the mmory allocated to the VM, and is also used to allocate
     42  * extra physical space used for XENMEM_add_to_physmap (in come cases)
     43  * In the !XENPV case, the physical space is managed by bus_space, so
     44  * we reuse the iomem_ex
     45  */
     46 
     47 extern struct  extent *iomem_ex;
     48 #define XENMEM_EX iomem_ex
     49 
     50 paddr_t
     51 xenmem_alloc_pa(u_long size, u_long align, bool waitok)
     52 {
     53 	u_long result;
     54 	int error;
     55 
     56 #ifdef _LP64
     57 	/* allocate above the 4Gb range to not collide wit devices */
     58 	error = extent_alloc_subregion(XENMEM_EX, 0x100000000UL, MAXIOMEM,
     59 	    size, align, 0,
     60 	    (waitok ? (EX_WAITSPACE | EX_WAITOK) : EX_NOWAIT) | EX_MALLOCOK,
     61 	    &result);
     62 #else
     63 	error = extent_alloc(XENMEM_EX, size, align, 0,
     64 	    (waitok ? (EX_WAITSPACE | EX_WAITOK) : EX_NOWAIT) | EX_MALLOCOK,
     65 	    &result);
     66 #endif
     67 	if (error) {
     68 		printf("xenmem_alloc_pa: failed %d\n", error);
     69 		return 0;
     70 	}
     71 	return result;
     72 }
     73 
     74 void
     75 xenmem_free_pa(paddr_t start, u_long size)
     76 {
     77 	int error;
     78 	error = extent_free(XENMEM_EX, start, size, EX_NOWAIT);
     79 	if (error) {
     80 		printf("WARNING: xenmem_alloc_pa failed: %d\n", error);
     81 	}
     82 }
     83