1*9132e770Sskrll /* $NetBSD: bus_dmamem_common.c,v 1.5 2022/11/12 07:48:34 skrll Exp $ */
24b4465bdSchristos
34b4465bdSchristos /*-
44b4465bdSchristos * Copyright (c) 1997, 1998, 2009 The NetBSD Foundation, Inc.
54b4465bdSchristos * All rights reserved.
64b4465bdSchristos *
74b4465bdSchristos * This code is derived from software contributed to The NetBSD Foundation
84b4465bdSchristos * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
94b4465bdSchristos * NASA Ames Research Center.
104b4465bdSchristos *
114b4465bdSchristos * Redistribution and use in source and binary forms, with or without
124b4465bdSchristos * modification, are permitted provided that the following conditions
134b4465bdSchristos * are met:
144b4465bdSchristos * 1. Redistributions of source code must retain the above copyright
154b4465bdSchristos * notice, this list of conditions and the following disclaimer.
164b4465bdSchristos * 2. Redistributions in binary form must reproduce the above copyright
174b4465bdSchristos * notice, this list of conditions and the following disclaimer in the
184b4465bdSchristos * documentation and/or other materials provided with the distribution.
194b4465bdSchristos *
204b4465bdSchristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
214b4465bdSchristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
224b4465bdSchristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
234b4465bdSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
244b4465bdSchristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
254b4465bdSchristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
264b4465bdSchristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
274b4465bdSchristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
284b4465bdSchristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
294b4465bdSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
304b4465bdSchristos * POSSIBILITY OF SUCH DAMAGE.
314b4465bdSchristos */
324b4465bdSchristos
334b4465bdSchristos #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
344b4465bdSchristos
35*9132e770Sskrll __KERNEL_RCSID(0, "$NetBSD: bus_dmamem_common.c,v 1.5 2022/11/12 07:48:34 skrll Exp $");
364b4465bdSchristos
374b4465bdSchristos #include <sys/param.h>
384b4465bdSchristos #include <sys/systm.h>
394b4465bdSchristos #include <sys/proc.h>
404b4465bdSchristos #include <sys/bus.h>
414b4465bdSchristos
429fc45356Sriastradh #include <uvm/uvm_extern.h>
43add69999Sriastradh #include <uvm/uvm_page.h>
444b4465bdSchristos
45cf41d097Schristos #include <dev/bus_dma/bus_dmamem_common.h>
464b4465bdSchristos
474b4465bdSchristos /*
484b4465bdSchristos * _bus_dmamem_alloc_range_common --
494b4465bdSchristos * Allocate physical memory from the specified physical address range.
504b4465bdSchristos */
514b4465bdSchristos int
_bus_dmamem_alloc_range_common(bus_dma_tag_t t,bus_size_t size,bus_size_t alignment,bus_size_t boundary,bus_dma_segment_t * segs,int nsegs,int * rsegs,int flags,paddr_t low,paddr_t high)524b4465bdSchristos _bus_dmamem_alloc_range_common(bus_dma_tag_t t,
534b4465bdSchristos bus_size_t size,
544b4465bdSchristos bus_size_t alignment,
554b4465bdSchristos bus_size_t boundary,
564b4465bdSchristos bus_dma_segment_t *segs,
574b4465bdSchristos int nsegs,
584b4465bdSchristos int *rsegs,
594b4465bdSchristos int flags,
604b4465bdSchristos paddr_t low,
614b4465bdSchristos paddr_t high)
624b4465bdSchristos {
634b4465bdSchristos paddr_t curaddr, lastaddr;
644b4465bdSchristos struct vm_page *m;
654b4465bdSchristos struct pglist mlist;
664b4465bdSchristos int curseg, error;
674b4465bdSchristos
684b4465bdSchristos /* Always round the size. */
694b4465bdSchristos size = round_page(size);
704b4465bdSchristos
714b4465bdSchristos /* Allocate pages from the VM system. */
724b4465bdSchristos error = uvm_pglistalloc(size, low, high, alignment, boundary,
734b4465bdSchristos &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
744b4465bdSchristos if (__predict_false(error != 0))
754b4465bdSchristos return (error);
764b4465bdSchristos
774b4465bdSchristos /*
784b4465bdSchristos * Compute the location, size, and number of segments actually
794b4465bdSchristos * returned by the VM system.
804b4465bdSchristos */
814b4465bdSchristos m = TAILQ_FIRST(&mlist);
824b4465bdSchristos curseg = 0;
834b4465bdSchristos lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
844b4465bdSchristos segs[curseg].ds_len = PAGE_SIZE;
854b4465bdSchristos m = TAILQ_NEXT(m, pageq.queue);
864b4465bdSchristos
874b4465bdSchristos for (; m != NULL; m = TAILQ_NEXT(m, pageq.queue)) {
884b4465bdSchristos curaddr = VM_PAGE_TO_PHYS(m);
894b4465bdSchristos KASSERT(curaddr >= low);
904b4465bdSchristos KASSERT(curaddr < high);
914b4465bdSchristos if (curaddr == (lastaddr + PAGE_SIZE))
924b4465bdSchristos segs[curseg].ds_len += PAGE_SIZE;
934b4465bdSchristos else {
944b4465bdSchristos curseg++;
954b4465bdSchristos segs[curseg].ds_addr = curaddr;
964b4465bdSchristos segs[curseg].ds_len = PAGE_SIZE;
974b4465bdSchristos }
984b4465bdSchristos lastaddr = curaddr;
994b4465bdSchristos }
1004b4465bdSchristos
1014b4465bdSchristos *rsegs = curseg + 1;
1024b4465bdSchristos
1034b4465bdSchristos return (0);
1044b4465bdSchristos }
1054b4465bdSchristos
1064b4465bdSchristos /*
1074b4465bdSchristos * _bus_dmamem_free_common --
1084b4465bdSchristos * Free memory allocated with _bus_dmamem_alloc_range_common()
1094b4465bdSchristos * back to the VM system.
1104b4465bdSchristos */
1114b4465bdSchristos void
_bus_dmamem_free_common(bus_dma_tag_t t,bus_dma_segment_t * segs,int nsegs)1124b4465bdSchristos _bus_dmamem_free_common(bus_dma_tag_t t,
1134b4465bdSchristos bus_dma_segment_t *segs,
1144b4465bdSchristos int nsegs)
1154b4465bdSchristos {
1164b4465bdSchristos struct vm_page *m;
1174b4465bdSchristos bus_addr_t addr;
1184b4465bdSchristos struct pglist mlist;
1194b4465bdSchristos int curseg;
1204b4465bdSchristos
1214b4465bdSchristos TAILQ_INIT(&mlist);
1224b4465bdSchristos for (curseg = 0; curseg < nsegs; curseg++) {
1234b4465bdSchristos for (addr = segs[curseg].ds_addr;
1244b4465bdSchristos addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
1254b4465bdSchristos addr += PAGE_SIZE) {
1264b4465bdSchristos m = PHYS_TO_VM_PAGE(addr);
1274b4465bdSchristos TAILQ_INSERT_TAIL(&mlist, m, pageq.queue);
1284b4465bdSchristos }
1294b4465bdSchristos }
1304b4465bdSchristos
1314b4465bdSchristos uvm_pglistfree(&mlist);
1324b4465bdSchristos }
1334b4465bdSchristos
1344b4465bdSchristos /*
1354b4465bdSchristos * _bus_dmamem_map_common --
1364b4465bdSchristos * Map memory allocated with _bus_dmamem_alloc_range_common() into
1374b4465bdSchristos * the kernel virtual address space.
1384b4465bdSchristos */
1394b4465bdSchristos int
_bus_dmamem_map_common(bus_dma_tag_t t,bus_dma_segment_t * segs,int nsegs,size_t size,void ** kvap,int flags,int pmapflags)1404b4465bdSchristos _bus_dmamem_map_common(bus_dma_tag_t t,
1414b4465bdSchristos bus_dma_segment_t *segs,
1424b4465bdSchristos int nsegs,
1434b4465bdSchristos size_t size,
1444b4465bdSchristos void **kvap,
1454b4465bdSchristos int flags,
1464b4465bdSchristos int pmapflags)
1474b4465bdSchristos {
1484b4465bdSchristos vaddr_t va;
1494b4465bdSchristos bus_addr_t addr;
1504b4465bdSchristos int curseg;
1514b4465bdSchristos const uvm_flag_t kmflags =
1524b4465bdSchristos (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
1534b4465bdSchristos
1544b4465bdSchristos size = round_page(size);
1554b4465bdSchristos
1564b4465bdSchristos va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
1574b4465bdSchristos if (__predict_false(va == 0))
1584b4465bdSchristos return (ENOMEM);
1594b4465bdSchristos
1604b4465bdSchristos *kvap = (void *)va;
1614b4465bdSchristos
1624b4465bdSchristos for (curseg = 0; curseg < nsegs; curseg++) {
1634b4465bdSchristos for (addr = segs[curseg].ds_addr;
1644b4465bdSchristos addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
1654b4465bdSchristos addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
1664b4465bdSchristos KASSERT(size != 0);
1674b4465bdSchristos /* XXX pmap_kenter_pa()? */
1684b4465bdSchristos pmap_enter(pmap_kernel(), va, addr,
1694b4465bdSchristos VM_PROT_READ | VM_PROT_WRITE,
1704b4465bdSchristos pmapflags | PMAP_WIRED |
1714b4465bdSchristos VM_PROT_READ | VM_PROT_WRITE);
1724b4465bdSchristos }
1734b4465bdSchristos }
1744b4465bdSchristos pmap_update(pmap_kernel());
1754b4465bdSchristos
1764b4465bdSchristos return (0);
1774b4465bdSchristos }
1784b4465bdSchristos
1794b4465bdSchristos /*
1804b4465bdSchristos * _bus_dmamem_unmap_common --
1814b4465bdSchristos * Remove a mapping created with _bus_dmamem_map_common().
1824b4465bdSchristos */
1834b4465bdSchristos void
_bus_dmamem_unmap_common(bus_dma_tag_t t,void * kva,size_t size)1844b4465bdSchristos _bus_dmamem_unmap_common(bus_dma_tag_t t,
1854b4465bdSchristos void *kva,
1864b4465bdSchristos size_t size)
1874b4465bdSchristos {
1884b4465bdSchristos
1894b4465bdSchristos KASSERT(((vaddr_t)kva & PAGE_MASK) == 0);
1904b4465bdSchristos
1914b4465bdSchristos size = round_page(size);
1924b4465bdSchristos /* XXX pmap_kremove()? See above... */
1934b4465bdSchristos pmap_remove(pmap_kernel(), (vaddr_t)kva, (vaddr_t)kva + size);
1944b4465bdSchristos pmap_update(pmap_kernel());
1954b4465bdSchristos uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
1964b4465bdSchristos }
1974b4465bdSchristos
1984b4465bdSchristos /*
1994b4465bdSchristos * _bus_dmamem_mmap_common --
2004b4465bdSchristos * Mmap support for memory allocated with _bus_dmamem_alloc_range_common().
2014b4465bdSchristos */
2024b4465bdSchristos bus_addr_t
_bus_dmamem_mmap_common(bus_dma_tag_t t,bus_dma_segment_t * segs,int nsegs,off_t off,int prot,int flags)2034b4465bdSchristos _bus_dmamem_mmap_common(bus_dma_tag_t t,
2044b4465bdSchristos bus_dma_segment_t *segs,
2054b4465bdSchristos int nsegs,
2064b4465bdSchristos off_t off,
2074b4465bdSchristos int prot,
2084b4465bdSchristos int flags)
2094b4465bdSchristos {
2104b4465bdSchristos int i;
2114b4465bdSchristos
2124b4465bdSchristos for (i = 0; i < nsegs; i++) {
2134b4465bdSchristos KASSERT((off & PAGE_MASK) == 0);
2144b4465bdSchristos KASSERT((segs[i].ds_addr & PAGE_MASK) == 0);
2154b4465bdSchristos KASSERT((segs[i].ds_len & PAGE_MASK) == 0);
2164b4465bdSchristos if (off >= segs[i].ds_len) {
2174b4465bdSchristos off -= segs[i].ds_len;
2184b4465bdSchristos continue;
2194b4465bdSchristos }
2204b4465bdSchristos
2214b4465bdSchristos /* XXX BUS_DMA_COHERENT */
2224b4465bdSchristos
2234b4465bdSchristos return (segs[i].ds_addr + off);
2244b4465bdSchristos }
2254b4465bdSchristos
2264b4465bdSchristos /* Page not found. */
2274b4465bdSchristos return ((bus_addr_t)-1);
2284b4465bdSchristos }
229