xref: /netbsd-src/sys/arch/arc/jazz/jazzdmatlb.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: jazzdmatlb.c,v 1.17 2020/06/17 06:20:05 thorpej Exp $	*/
2 /*	$OpenBSD: dma.c,v 1.5 1998/03/01 16:49:57 niklas Exp $	*/
3 
4 /*-
5  * Copyright (C) 2000 Shuichiro URATA.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * Jazz derived system dma driver. Handles resource allocation and
32  * logical (virtual) address remaping.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: jazzdmatlb.c,v 1.17 2020/06/17 06:20:05 thorpej Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/device.h>
42 #include <sys/proc.h>
43 #include <sys/vmem.h>
44 
45 #include <uvm/uvm_extern.h>
46 
47 #include <machine/autoconf.h>
48 #include <sys/bus.h>
49 
50 #include <arc/jazz/jazzdmatlbreg.h>
51 #include <arc/jazz/jazzdmatlbvar.h>
52 
53 #include <mips/cache.h>
54 
55 extern paddr_t	kvtophys(vaddr_t);	/* XXX */
56 
57 #define NDMATLB		(JAZZ_DMATLB_SIZE / sizeof(jazz_dma_pte_t))
58 
59 static bus_space_tag_t dmatlb_iot;
60 static bus_space_handle_t dmatlb_ioh;
61 
62 static vmem_t *dmatlb_arena;
63 static jazz_dma_pte_t *dma_tlb;
64 
65 /*
66  *  Initialize the dma mapping register area and pool.
67  */
68 void
69 jazz_dmatlb_init(bus_space_tag_t iot, bus_addr_t ioaddr)
70 {
71 	int err;
72 
73 	dmatlb_iot = iot;
74 	err = bus_space_map(iot, ioaddr, JAZZ_DMATLB_REGSIZE, 0, &dmatlb_ioh);
75 	if (err != 0)
76 		panic("jazz_dmatlb_init: cannot map 0x%lx", ioaddr);
77 
78 	dma_tlb = (jazz_dma_pte_t *)PICA_TL_BASE;
79 
80 	mips_dcache_wbinv_all();/* Make sure no map entries are cached */
81 	memset((char *)dma_tlb, 0, JAZZ_DMATLB_SIZE);
82 
83 	dmatlb_arena = vmem_create("dmatlb", 0, NDMATLB,
84 				   1,		/* quantum */
85 				   NULL,	/* importfn */
86 				   NULL,	/* releasefn */
87 				   NULL,	/* source */
88 				   0,		/* qcache_max */
89 				   VM_SLEEP,
90 				   IPL_VM);
91 	KASSERT(dmatlb_arena != NULL);
92 
93 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_MAP,
94 	    MIPS_KSEG1_TO_PHYS(dma_tlb));
95 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_LIMIT,
96 	    JAZZ_DMATLB_SIZE);
97 	jazz_dmatlb_flush();
98 }
99 
100 /*
101  *  Allocate an array of 'size' DMA PTEs.
102  *  Return address to first pte.
103  */
104 jazz_dma_pte_t *
105 jazz_dmatlb_alloc(int npte, bus_size_t boundary, int flags, bus_addr_t *addr)
106 {
107 	vmem_addr_t start;
108 	int err;
109 
110 	const vm_flag_t vmflags = VM_INSTANTFIT |
111 	    ((flags & BUS_DMA_WAITOK) ? VM_SLEEP : VM_NOSLEEP);
112 
113 	err = vmem_xalloc(dmatlb_arena, npte,
114 			  1,			/* align */
115 			  0,			/* phase */
116 			  boundary / JAZZ_DMA_PAGE_SIZE,
117 			  VMEM_ADDR_MIN,
118 			  VMEM_ADDR_MAX,
119 			  vmflags,
120 			  &start);
121 	if (err)
122 		return NULL;
123 
124 	*addr = start * JAZZ_DMA_PAGE_SIZE;
125 
126 	return dma_tlb + start;
127 }
128 
129 /*
130  *  Free an array of DMA PTEs.
131  */
132 void
133 jazz_dmatlb_free(bus_addr_t addr, int npte)
134 {
135 	vmem_addr_t start;
136 
137 	start = addr / JAZZ_DMA_PAGE_SIZE;
138 	vmem_xfree(dmatlb_arena, start, npte);
139 }
140 
141 /*
142  *  Map up a virtual address space in dma space given by
143  *  the dma control structure.
144  */
145 void
146 jazz_dmatlb_map_va(struct vmspace *vm, vaddr_t va, vsize_t size,
147     jazz_dma_pte_t *dma_pte)
148 {
149 	paddr_t pa;
150 
151 	size = jazz_dma_page_round(size + jazz_dma_page_offs(va));
152 	va &= JAZZ_DMA_PAGE_NUM;
153 	while (size > 0) {
154 		if (!VMSPACE_IS_KERNEL_P(vm))
155 			(void)pmap_extract(vm_map_pmap(&vm->vm_map), va, &pa);
156 		else
157 			pa = kvtophys(va);
158 
159 		pa &= JAZZ_DMA_PAGE_NUM;
160 		dma_pte->lo_addr = pa;
161 		dma_pte->hi_addr = 0;
162 		dma_pte++;
163 		va += JAZZ_DMA_PAGE_SIZE;
164 		size -= JAZZ_DMA_PAGE_SIZE;
165 	}
166 }
167 
168 /*
169  *  Map up a physical address space in dma space given by
170  *  the dma control structure.
171  */
172 void
173 jazz_dmatlb_map_pa(paddr_t pa, psize_t size, jazz_dma_pte_t *dma_pte)
174 {
175 
176 	size = jazz_dma_page_round(size + jazz_dma_page_offs(pa));
177 	pa &= JAZZ_DMA_PAGE_NUM;
178 	while (size > 0) {
179 		dma_pte->lo_addr = pa;
180 		dma_pte->hi_addr = 0;
181 		dma_pte++;
182 		pa += JAZZ_DMA_PAGE_SIZE;
183 		size -= JAZZ_DMA_PAGE_SIZE;
184 	}
185 }
186 
187 /*
188  *  Prepare for new dma by flushing
189  */
190 void
191 jazz_dmatlb_flush(void)
192 {
193 
194 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_IVALID, 0);
195 }
196