Home | History | Annotate | Line # | Download | only in x86
xen_bus_dma.c revision 1.9.42.3
      1 /*	$NetBSD: xen_bus_dma.c,v 1.9.42.3 2009/01/17 13:28:39 mjf Exp $	*/
      2 /*	NetBSD bus_dma.c,v 1.21 2005/04/16 07:53:35 yamt Exp */
      3 
      4 /*-
      5  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
     10  * Simulation Facility, NASA Ames Research Center.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: xen_bus_dma.c,v 1.9.42.3 2009/01/17 13:28:39 mjf Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/mbuf.h>
     41 #include <sys/proc.h>
     42 
     43 #include <machine/bus.h>
     44 #include <machine/bus_private.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 
     48 extern paddr_t avail_end;
     49 
     50 /* Pure 2^n version of get_order */
     51 static inline int get_order(unsigned long size)
     52 {
     53 	int order = -1;
     54 	size = (size - 1) >> (PAGE_SHIFT - 1);
     55 	do {
     56 		size >>= 1;
     57 		order++;
     58 	} while (size);
     59 	return order;
     60 }
     61 
     62 static int
     63 _xen_alloc_contig(bus_size_t size, bus_size_t alignment, bus_size_t boundary,
     64     struct pglist *mlistp, int flags, bus_addr_t low, bus_addr_t high)
     65 {
     66 	int order, i;
     67 	unsigned long npagesreq, npages, mfn;
     68 	bus_addr_t pa;
     69 	struct vm_page *pg, *pgnext;
     70 	int s, error;
     71 #ifdef XEN3
     72 	struct xen_memory_reservation res;
     73 #endif
     74 
     75 	/*
     76 	 * When requesting a contigous memory region, the hypervisor will
     77 	 * return a memory range aligned on size. This will automagically
     78 	 * handle "boundary", but the only way to enforce alignment
     79 	 * is to request a memory region of size max(alignment, size).
     80 	 */
     81 	order = max(get_order(size), get_order(alignment));
     82 	npages = (1 << order);
     83 	npagesreq = (size >> PAGE_SHIFT);
     84 	KASSERT(npages >= npagesreq);
     85 
     86 	/* get npages from UWM, and give them back to the hypervisor */
     87 	error = uvm_pglistalloc(npages << PAGE_SHIFT, 0, avail_end, 0, 0,
     88 	    mlistp, npages, (flags & BUS_DMA_NOWAIT) == 0);
     89 	if (error)
     90 		return (error);
     91 
     92 	for (pg = mlistp->tqh_first; pg != NULL; pg = pg->pageq.queue.tqe_next) {
     93 		pa = VM_PAGE_TO_PHYS(pg);
     94 		mfn = xpmap_ptom(pa) >> PAGE_SHIFT;
     95 		xpmap_phys_to_machine_mapping[
     96 		    (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = INVALID_P2M_ENTRY;
     97 #ifdef XEN3
     98 		xenguest_handle(res.extent_start) = &mfn;
     99 		res.nr_extents = 1;
    100 		res.extent_order = 0;
    101 		res.domid = DOMID_SELF;
    102 		if (HYPERVISOR_memory_op(XENMEM_decrease_reservation, &res)
    103 		    < 0) {
    104 #ifdef DEBUG
    105 			printf("xen_alloc_contig: XENMEM_decrease_reservation "
    106 			    "failed!\n");
    107 #endif
    108 			xpmap_phys_to_machine_mapping[
    109 			    (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn;
    110 
    111 			error = ENOMEM;
    112 			goto failed;
    113 		}
    114 #else
    115 		if (HYPERVISOR_dom_mem_op(MEMOP_decrease_reservation,
    116 		    &mfn, 1, 0) != 1) {
    117 #ifdef DEBUG
    118 			printf("xen_alloc_contig: MEMOP_decrease_reservation "
    119 			    "failed!\n");
    120 #endif
    121 			xpmap_phys_to_machine_mapping[
    122 			    (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn;
    123 			error = ENOMEM;
    124 			goto failed;
    125 		}
    126 #endif
    127 	}
    128 	/* Get the new contiguous memory extent */
    129 #ifdef XEN3
    130 	xenguest_handle(res.extent_start) = &mfn;
    131 	res.nr_extents = 1;
    132 	res.extent_order = order;
    133 	res.address_bits = get_order(high) + PAGE_SHIFT;
    134 	res.domid = DOMID_SELF;
    135 	if (HYPERVISOR_memory_op(XENMEM_increase_reservation, &res) < 0) {
    136 #ifdef DEBUG
    137 		printf("xen_alloc_contig: XENMEM_increase_reservation "
    138 		    "failed!\n");
    139 #endif
    140 		error = ENOMEM;
    141 		pg = NULL;
    142 		goto failed;
    143 	}
    144 #else
    145 	if (HYPERVISOR_dom_mem_op(MEMOP_increase_reservation,
    146 	    &mfn, 1, order) != 1) {
    147 #ifdef DEBUG
    148 		printf("xen_alloc_contig: MEMOP_increase_reservation "
    149 		    "failed!\n");
    150 #endif
    151 		error = ENOMEM;
    152 		pg = NULL;
    153 		goto failed;
    154 	}
    155 #endif
    156 	s = splvm();
    157 	/* Map the new extent in place of the old pages */
    158 	for (pg = mlistp->tqh_first, i = 0; pg != NULL; pg = pgnext, i++) {
    159 		pgnext = pg->pageq.queue.tqe_next;
    160 		pa = VM_PAGE_TO_PHYS(pg);
    161 		xpmap_phys_to_machine_mapping[
    162 		    (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn+i;
    163 		xpq_queue_machphys_update((mfn+i) << PAGE_SHIFT, pa);
    164 		/* while here, give extra pages back to UVM */
    165 		if (i >= npagesreq) {
    166 			TAILQ_REMOVE(mlistp, pg, pageq.queue);
    167 			uvm_pagefree(pg);
    168 		}
    169 
    170 	}
    171 	/* Flush updates through and flush the TLB */
    172 	xpq_queue_tlb_flush();
    173 	xpq_flush_queue();
    174 	splx(s);
    175 	return 0;
    176 
    177 failed:
    178 	/*
    179 	 * Attempt to recover from a failed decrease or increase reservation:
    180 	 * if decrease_reservation failed, we don't have given all pages
    181 	 * back to Xen; give them back to UVM, and get the missing pages
    182 	 * from Xen.
    183 	 * if increase_reservation failed, we expect pg to be NULL and we just
    184 	 * get back the missing pages from Xen one by one.
    185 	 */
    186 	/* give back remaining pages to UVM */
    187 	for (; pg != NULL; pg = pgnext) {
    188 		pgnext = pg->pageq.queue.tqe_next;
    189 		TAILQ_REMOVE(mlistp, pg, pageq.queue);
    190 		uvm_pagefree(pg);
    191 	}
    192 	/* remplace the pages that we already gave to Xen */
    193 	s = splvm();
    194 	for (pg = mlistp->tqh_first; pg != NULL; pg = pgnext) {
    195 		pgnext = pg->pageq.queue.tqe_next;
    196 #ifdef XEN3
    197 		xenguest_handle(res.extent_start) = &mfn;
    198 		res.nr_extents = 1;
    199 		res.extent_order = 0;
    200 		res.address_bits = 32;
    201 		res.domid = DOMID_SELF;
    202 		if (HYPERVISOR_memory_op(XENMEM_increase_reservation, &res)
    203 		    < 0) {
    204 			printf("xen_alloc_contig: recovery "
    205 			    "XENMEM_increase_reservation failed!\n");
    206 			break;
    207 		}
    208 #else
    209 		if (HYPERVISOR_dom_mem_op(MEMOP_increase_reservation,
    210 		    &mfn, 1, 0) != 1) {
    211 			printf("xen_alloc_contig: recovery "
    212 			    "MEMOP_increase_reservation failed!\n");
    213 			break;
    214 		}
    215 #endif
    216 		pa = VM_PAGE_TO_PHYS(pg);
    217 		xpmap_phys_to_machine_mapping[
    218 		    (pa - XPMAP_OFFSET) >> PAGE_SHIFT] = mfn;
    219 		xpq_queue_machphys_update((mfn) << PAGE_SHIFT, pa);
    220 		TAILQ_REMOVE(mlistp, pg, pageq.queue);
    221 		uvm_pagefree(pg);
    222 	}
    223 	/* Flush updates through and flush the TLB */
    224 	xpq_queue_tlb_flush();
    225 	xpq_flush_queue();
    226 	splx(s);
    227 	return error;
    228 }
    229 
    230 
    231 /*
    232  * Allocate physical memory from the given physical address range.
    233  * Called by DMA-safe memory allocation methods.
    234  * We need our own version to deal with physical vs machine addresses.
    235  */
    236 int
    237 _xen_bus_dmamem_alloc_range(bus_dma_tag_t t, bus_size_t size,
    238     bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
    239     int nsegs, int *rsegs, int flags, bus_addr_t low, bus_addr_t high)
    240 {
    241 	bus_addr_t curaddr, lastaddr;
    242 	struct vm_page *m;
    243 	struct pglist mlist;
    244 	int curseg, error;
    245 	int doingrealloc = 0;
    246 
    247 	/* Always round the size. */
    248 	size = round_page(size);
    249 
    250 	KASSERT((alignment & (alignment - 1)) == 0);
    251 	KASSERT((boundary & (boundary - 1)) == 0);
    252 	if (alignment < PAGE_SIZE)
    253 		alignment = PAGE_SIZE;
    254 	if (boundary != 0 && boundary < size)
    255 		return (EINVAL);
    256 
    257 	/*
    258 	 * Allocate pages from the VM system.
    259 	 */
    260 	error = uvm_pglistalloc(size, 0, avail_end, alignment, boundary,
    261 	    &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    262 	if (error)
    263 		return (error);
    264 again:
    265 
    266 	/*
    267 	 * Compute the location, size, and number of segments actually
    268 	 * returned by the VM code.
    269 	 */
    270 	m = mlist.tqh_first;
    271 	curseg = 0;
    272 	curaddr = lastaddr = segs[curseg].ds_addr = _BUS_VM_PAGE_TO_BUS(m);
    273 	if (curaddr < low || curaddr >= high)
    274 		goto badaddr;
    275 	segs[curseg].ds_len = PAGE_SIZE;
    276 	m = m->pageq.queue.tqe_next;
    277 	if ((segs[curseg].ds_addr & (alignment - 1)) != 0)
    278 		goto dorealloc;
    279 
    280 	for (; m != NULL; m = m->pageq.queue.tqe_next) {
    281 		curaddr = _BUS_VM_PAGE_TO_BUS(m);
    282 		if (curaddr < low || curaddr >= high)
    283 			goto badaddr;
    284 		if (curaddr == (lastaddr + PAGE_SIZE)) {
    285 			segs[curseg].ds_len += PAGE_SIZE;
    286 			if ((lastaddr & boundary) != (curaddr & boundary))
    287 				goto dorealloc;
    288 		} else {
    289 			curseg++;
    290 			if (curseg >= nsegs || (curaddr & (alignment - 1)) != 0)
    291 				goto dorealloc;
    292 			segs[curseg].ds_addr = curaddr;
    293 			segs[curseg].ds_len = PAGE_SIZE;
    294 		}
    295 		lastaddr = curaddr;
    296 	}
    297 
    298 	*rsegs = curseg + 1;
    299 	return (0);
    300 
    301 badaddr:
    302 #ifdef XEN3
    303 	if (doingrealloc == 0)
    304 		goto dorealloc;
    305 	if (curaddr < low) {
    306 		/* no way to enforce this */
    307 		printf("_xen_bus_dmamem_alloc_range: no way to "
    308 		    "enforce address range\n");
    309 		uvm_pglistfree(&mlist);
    310 		return EINVAL;
    311 	}
    312 	printf("xen_bus_dmamem_alloc_range: "
    313 	    "curraddr=0x%lx > high=0x%lx\n",
    314 	    (u_long)curaddr, (u_long)high);
    315 	panic("xen_bus_dmamem_alloc_range 1");
    316 #else /* !XEN3 */
    317 	/*
    318 	 * If machine addresses are outside the allowed
    319 	 * range we have to bail. Xen2 doesn't offer an
    320 	 * interface to get memory in a specific address
    321 	 * range.
    322 	 */
    323 	printf("_xen_bus_dmamem_alloc_range: no way to "
    324 	    "enforce address range\n");
    325 	uvm_pglistfree(&mlist);
    326 	return EINVAL;
    327 #endif /* XEN3 */
    328 dorealloc:
    329 	if (doingrealloc == 1)
    330 		panic("_xen_bus_dmamem_alloc_range: "
    331 		   "xen_alloc_contig returned "
    332 		   "too much segments");
    333 	doingrealloc = 1;
    334 	/*
    335 	 * Too much segments, or memory doesn't fit
    336 	 * constraints. Free this memory and
    337 	 * get a contigous segment from the hypervisor.
    338 	 */
    339 	uvm_pglistfree(&mlist);
    340 	for (curseg = 0; curseg < nsegs; curseg++) {
    341 		segs[curseg].ds_addr = 0;
    342 		segs[curseg].ds_len = 0;
    343 	}
    344 	error = _xen_alloc_contig(size, alignment,
    345 	    boundary, &mlist, flags, low, high);
    346 	if (error)
    347 		return error;
    348 	goto again;
    349 }
    350