sgmap_common.c revision 1.25 1 1.25 dyoung /* $NetBSD: sgmap_common.c,v 1.25 2011/07/01 19:22:35 dyoung 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.25 dyoung __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.25 2011/07/01 19:22:35 dyoung 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.2 thorpej * Create the extent map used to manage the virtual address
107 1.2 thorpej * space.
108 1.2 thorpej */
109 1.21 drochner sgmap->aps_ex = extent_create(name, sgvabase, sgvasize - 1,
110 1.8 thorpej M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
111 1.10 thorpej if (sgmap->aps_ex == NULL) {
112 1.10 thorpej printf("unable to create extent map for sgmap `%s'\n",
113 1.10 thorpej name);
114 1.10 thorpej goto die;
115 1.10 thorpej }
116 1.6 thorpej
117 1.6 thorpej /*
118 1.6 thorpej * Allocate a spill page if that hasn't already been done.
119 1.6 thorpej */
120 1.6 thorpej if (alpha_sgmap_prefetch_spill_page_va == 0) {
121 1.20 thorpej if (bus_dmamem_alloc(t, PAGE_SIZE, 0, 0, &seg, 1, &rseg,
122 1.10 thorpej BUS_DMA_NOWAIT)) {
123 1.10 thorpej printf("unable to allocate spill page for sgmap `%s'\n",
124 1.10 thorpej name);
125 1.10 thorpej goto die;
126 1.10 thorpej }
127 1.6 thorpej alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr;
128 1.6 thorpej alpha_sgmap_prefetch_spill_page_va =
129 1.6 thorpej ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa);
130 1.23 christos memset((void *)alpha_sgmap_prefetch_spill_page_va, 0,
131 1.20 thorpej PAGE_SIZE);
132 1.6 thorpej }
133 1.10 thorpej
134 1.10 thorpej return;
135 1.10 thorpej die:
136 1.10 thorpej panic("alpha_sgmap_init");
137 1.2 thorpej }
138 1.2 thorpej
139 1.2 thorpej int
140 1.16 thorpej alpha_sgmap_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
141 1.16 thorpej bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
142 1.14 thorpej {
143 1.14 thorpej bus_dmamap_t map;
144 1.14 thorpej int error;
145 1.14 thorpej
146 1.14 thorpej error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
147 1.15 thorpej boundary, flags, &map);
148 1.14 thorpej if (error)
149 1.14 thorpej return (error);
150 1.14 thorpej
151 1.18 thorpej /* XXX BUS_DMA_ALLOCNOW */
152 1.15 thorpej
153 1.15 thorpej if (error == 0)
154 1.15 thorpej *dmamp = map;
155 1.15 thorpej else
156 1.15 thorpej alpha_sgmap_dmamap_destroy(t, map);
157 1.14 thorpej
158 1.14 thorpej return (error);
159 1.14 thorpej }
160 1.14 thorpej
161 1.14 thorpej void
162 1.16 thorpej alpha_sgmap_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
163 1.14 thorpej {
164 1.14 thorpej
165 1.18 thorpej KASSERT(map->dm_mapsize == 0);
166 1.14 thorpej
167 1.14 thorpej _bus_dmamap_destroy(t, map);
168 1.2 thorpej }
169