sgmap_common.c revision 1.27 1 1.27 thorpej /* $NetBSD: sgmap_common.c,v 1.27 2020/06/17 04:12:39 thorpej Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.18 thorpej * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.2 thorpej * NASA Ames Research Center.
10 1.2 thorpej *
11 1.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.2 thorpej * modification, are permitted provided that the following conditions
13 1.2 thorpej * are met:
14 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.2 thorpej *
20 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.2 thorpej */
32 1.2 thorpej
33 1.2 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 1.2 thorpej
35 1.27 thorpej __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.27 2020/06/17 04:12:39 thorpej Exp $");
36 1.2 thorpej
37 1.2 thorpej #include <sys/param.h>
38 1.2 thorpej #include <sys/systm.h>
39 1.2 thorpej #include <sys/kernel.h>
40 1.2 thorpej #include <sys/malloc.h>
41 1.2 thorpej #include <sys/proc.h>
42 1.2 thorpej
43 1.13 mrg #include <uvm/uvm_extern.h>
44 1.2 thorpej
45 1.14 thorpej #define _ALPHA_BUS_DMA_PRIVATE
46 1.25 dyoung #include <sys/bus.h>
47 1.2 thorpej
48 1.2 thorpej #include <alpha/common/sgmapvar.h>
49 1.2 thorpej
50 1.6 thorpej /*
51 1.6 thorpej * Some systems will prefetch the next page during a memory -> device DMA.
52 1.6 thorpej * This can cause machine checks if there is not a spill page after the
53 1.6 thorpej * last page of the DMA (thus avoiding hitting an invalid SGMAP PTE).
54 1.6 thorpej */
55 1.11 thorpej vaddr_t alpha_sgmap_prefetch_spill_page_va;
56 1.6 thorpej bus_addr_t alpha_sgmap_prefetch_spill_page_pa;
57 1.6 thorpej
58 1.2 thorpej void
59 1.16 thorpej alpha_sgmap_init(bus_dma_tag_t t, struct alpha_sgmap *sgmap, const char *name,
60 1.16 thorpej bus_addr_t wbase, bus_addr_t sgvabase, bus_size_t sgvasize, size_t ptesize,
61 1.16 thorpej void *ptva, bus_size_t minptalign)
62 1.2 thorpej {
63 1.2 thorpej bus_dma_segment_t seg;
64 1.2 thorpej size_t ptsize;
65 1.2 thorpej int rseg;
66 1.2 thorpej
67 1.10 thorpej if (sgvasize & PGOFSET) {
68 1.10 thorpej printf("size botch for sgmap `%s'\n", name);
69 1.10 thorpej goto die;
70 1.10 thorpej }
71 1.2 thorpej
72 1.2 thorpej sgmap->aps_wbase = wbase;
73 1.2 thorpej sgmap->aps_sgvabase = sgvabase;
74 1.2 thorpej sgmap->aps_sgvasize = sgvasize;
75 1.2 thorpej
76 1.2 thorpej if (ptva != NULL) {
77 1.2 thorpej /*
78 1.2 thorpej * We already have a page table; this may be a system
79 1.2 thorpej * where the page table resides in bridge-resident SRAM.
80 1.2 thorpej */
81 1.2 thorpej sgmap->aps_pt = ptva;
82 1.2 thorpej sgmap->aps_ptpa = 0;
83 1.2 thorpej } else {
84 1.2 thorpej /*
85 1.4 thorpej * Compute the page table size and allocate it. At minimum,
86 1.4 thorpej * this must be aligned to the page table size. However,
87 1.4 thorpej * some platforms have more strict alignment reqirements.
88 1.2 thorpej */
89 1.20 thorpej ptsize = (sgvasize / PAGE_SIZE) * ptesize;
90 1.4 thorpej if (minptalign != 0) {
91 1.4 thorpej if (minptalign < ptsize)
92 1.4 thorpej minptalign = ptsize;
93 1.4 thorpej } else
94 1.4 thorpej minptalign = ptsize;
95 1.4 thorpej if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
96 1.10 thorpej BUS_DMA_NOWAIT)) {
97 1.19 provos panic("unable to allocate page table for sgmap `%s'",
98 1.10 thorpej name);
99 1.10 thorpej goto die;
100 1.10 thorpej }
101 1.2 thorpej sgmap->aps_ptpa = seg.ds_addr;
102 1.23 christos sgmap->aps_pt = (void *)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa);
103 1.2 thorpej }
104 1.2 thorpej
105 1.2 thorpej /*
106 1.27 thorpej * Create the arena used to manage the virtual address
107 1.2 thorpej * space.
108 1.27 thorpej *
109 1.27 thorpej * XXX Consider using a quantum cache up to MAXPHYS+PAGE_SIZE
110 1.27 thorpej * XXX (extra page to handle the spill page). For now, we don't,
111 1.27 thorpej * XXX because we are using constrained allocations everywhere.
112 1.2 thorpej */
113 1.27 thorpej sgmap->aps_arena = vmem_create(name, sgvabase, sgvasize,
114 1.27 thorpej PAGE_SIZE, /* quantum */
115 1.27 thorpej NULL, /* importfn */
116 1.27 thorpej NULL, /* releasefn */
117 1.27 thorpej NULL, /* source */
118 1.27 thorpej 0, /* qcache_max */
119 1.27 thorpej VM_SLEEP,
120 1.27 thorpej IPL_VM);
121 1.27 thorpej KASSERT(sgmap->aps_arena != NULL);
122 1.6 thorpej
123 1.6 thorpej /*
124 1.6 thorpej * Allocate a spill page if that hasn't already been done.
125 1.6 thorpej */
126 1.6 thorpej if (alpha_sgmap_prefetch_spill_page_va == 0) {
127 1.20 thorpej if (bus_dmamem_alloc(t, PAGE_SIZE, 0, 0, &seg, 1, &rseg,
128 1.10 thorpej BUS_DMA_NOWAIT)) {
129 1.10 thorpej printf("unable to allocate spill page for sgmap `%s'\n",
130 1.10 thorpej name);
131 1.10 thorpej goto die;
132 1.10 thorpej }
133 1.6 thorpej alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr;
134 1.6 thorpej alpha_sgmap_prefetch_spill_page_va =
135 1.6 thorpej ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa);
136 1.23 christos memset((void *)alpha_sgmap_prefetch_spill_page_va, 0,
137 1.20 thorpej PAGE_SIZE);
138 1.6 thorpej }
139 1.10 thorpej
140 1.10 thorpej return;
141 1.10 thorpej die:
142 1.10 thorpej panic("alpha_sgmap_init");
143 1.2 thorpej }
144 1.2 thorpej
145 1.2 thorpej int
146 1.16 thorpej alpha_sgmap_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
147 1.16 thorpej bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
148 1.14 thorpej {
149 1.14 thorpej bus_dmamap_t map;
150 1.14 thorpej int error;
151 1.14 thorpej
152 1.14 thorpej error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
153 1.15 thorpej boundary, flags, &map);
154 1.14 thorpej if (error)
155 1.14 thorpej return (error);
156 1.14 thorpej
157 1.18 thorpej /* XXX BUS_DMA_ALLOCNOW */
158 1.15 thorpej
159 1.15 thorpej if (error == 0)
160 1.15 thorpej *dmamp = map;
161 1.15 thorpej else
162 1.15 thorpej alpha_sgmap_dmamap_destroy(t, map);
163 1.14 thorpej
164 1.14 thorpej return (error);
165 1.14 thorpej }
166 1.14 thorpej
167 1.14 thorpej void
168 1.16 thorpej alpha_sgmap_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
169 1.14 thorpej {
170 1.14 thorpej
171 1.18 thorpej KASSERT(map->dm_mapsize == 0);
172 1.14 thorpej
173 1.14 thorpej _bus_dmamap_destroy(t, map);
174 1.2 thorpej }
175