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