xref: /illumos-gate/usr/src/uts/i86pc/io/gfx_private/gfxp_vm.c (revision d6bb6a8465e557cb946ef49d56ed3202f6218652)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/debug.h>
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/buf.h>
33 #include <sys/errno.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/signal.h>
37 #include <sys/file.h>
38 #include <sys/uio.h>
39 #include <sys/ioctl.h>
40 #include <sys/map.h>
41 #include <sys/proc.h>
42 #include <sys/user.h>
43 #include <sys/mman.h>
44 #include <sys/cred.h>
45 #include <sys/open.h>
46 #include <sys/stat.h>
47 #include <sys/utsname.h>
48 #include <sys/kmem.h>
49 #include <sys/cmn_err.h>
50 #include <sys/vnode.h>
51 #include <vm/page.h>
52 #include <vm/as.h>
53 #include <vm/hat.h>
54 #include <vm/seg.h>
55 #include <vm/seg_kmem.h>
56 #include <vm/hat_i86.h>
57 #include <sys/vmsystm.h>
58 #include <sys/ddi.h>
59 #include <sys/devops.h>
60 #include <sys/sunddi.h>
61 #include <sys/ddi_impldefs.h>
62 #include <sys/fs/snode.h>
63 #include <sys/pci.h>
64 #include <sys/modctl.h>
65 #include <sys/uio.h>
66 #include <sys/visual_io.h>
67 #include <sys/fbio.h>
68 #include <sys/ddidmareq.h>
69 #include <sys/tnf_probe.h>
70 #include <sys/kstat.h>
71 #include <sys/callb.h>
72 #include <sys/promif.h>
73 #include <sys/atomic.h>
74 #include "gfx_private.h"
75 
76 /*
77  * Create a kva mapping for a pa (start..start+size) with
78  * the specified cache attributes (mode).
79  */
80 gfxp_kva_t
81 gfxp_map_kernel_space(uint64_t start, size_t size, uint32_t mode)
82 {
83 	uint_t pgoffset;
84 	uint64_t base;
85 	pgcnt_t npages;
86 	caddr_t cvaddr;
87 	int hat_flags;
88 	uint_t hat_attr;
89 
90 	if (size == 0)
91 		return (0);
92 
93 	if (mode == GFXP_MEMORY_CACHED)
94 		hat_attr = HAT_STORECACHING_OK;
95 	else if (mode == GFXP_MEMORY_WRITECOMBINED)
96 		hat_attr = HAT_MERGING_OK | HAT_PLAT_NOCACHE;
97 	else	/* GFXP_MEMORY_UNCACHED */
98 		hat_attr = HAT_STRICTORDER | HAT_PLAT_NOCACHE;
99 	hat_flags = HAT_LOAD_LOCK;
100 	pgoffset = start & PAGEOFFSET;
101 	base = start - pgoffset;
102 	npages = btopr(size + pgoffset);
103 	cvaddr = vmem_alloc(heap_arena, ptob(npages), VM_NOSLEEP);
104 	if (cvaddr == NULL)
105 		return (NULL);
106 	hat_devload(kas.a_hat, cvaddr, ptob(npages), btop(base),
107 			PROT_READ|PROT_WRITE|hat_attr, hat_flags);
108 	return (cvaddr + pgoffset);
109 }
110 
111 /*
112  * Destroy the mapping created by gfxp_map_kernel_space().
113  * Physical memory is not reclaimed.
114  */
115 void
116 gfxp_unmap_kernel_space(gfxp_kva_t address, size_t size)
117 {
118 	uint_t pgoffset;
119 	caddr_t base;
120 	pgcnt_t npages;
121 
122 	if (size == 0 || address == NULL)
123 		return;
124 
125 	pgoffset = (uintptr_t)address & PAGEOFFSET;
126 	base = (caddr_t)address - pgoffset;
127 	npages = btopr(size + pgoffset);
128 	hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
129 	vmem_free(heap_arena, base, ptob(npages));
130 }
131 
132 /*
133  * For a VA return the pfn
134  */
135 int
136 gfxp_va2pa(struct as *as, caddr_t addr, uint64_t *pa)
137 {
138 	*pa = (uint64_t)(hat_getpfnum(as->a_hat, addr) << PAGESHIFT);
139 	return (0);
140 }
141 
142 /*
143  * The KVA returned from ddi_dma_mem_alloc() always has WB/cached PTEs.
144  * This causes severe coherency problems when the pages are exported to
145  * user space with uncached/UC/WC PTEs.  Fix the cache attributes for
146  * each page, until ddi_dma_mem_alloc() returns KVAs with the correct
147  * cache attributes.
148  */
149 void
150 gfxp_fix_mem_cache_attrs(caddr_t kva_start, size_t length, int cache_attr)
151 {
152 	struct hat *hat = kas.a_hat;
153 	uint_t hat_attr;
154 	uint_t hat_flags;
155 	pfn_t pfnum;
156 	caddr_t kva;
157 	caddr_t kva_max;
158 
159 	if (hat_getattr(hat, kva_start, &hat_attr) == -1)
160 		return;
161 
162 	if (cache_attr == GFXP_MEMORY_UNCACHED) {
163 		hat_attr &= ~HAT_ORDER_MASK;
164 		hat_attr |= HAT_STRICTORDER | HAT_PLAT_NOCACHE;
165 	} else if (cache_attr == GFXP_MEMORY_WRITECOMBINED) {
166 		hat_attr &= ~HAT_ORDER_MASK;
167 		hat_attr |= HAT_MERGING_OK | HAT_PLAT_NOCACHE;
168 	} else
169 		return;
170 
171 	hat_attr |= HAT_NOSYNC;
172 
173 	hat_flags = HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST;
174 
175 	kva = (caddr_t)((uintptr_t)kva_start & (uintptr_t)PAGEMASK);
176 	kva_max = (caddr_t)((uintptr_t)(kva_start + length + PAGEOFFSET) &
177 		(uintptr_t)PAGEMASK);
178 
179 	for (; kva < kva_max; kva += PAGESIZE) {
180 		pfnum = hat_getpfnum(hat, kva);
181 		hat_unload(hat, kva, PAGESIZE, HAT_UNLOAD_UNLOCK);
182 		hat_devload(hat, kva, PAGESIZE, pfnum, hat_attr, hat_flags);
183 	}
184 }
185 
186 int
187 gfxp_ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
188     ddi_device_acc_attr_t  *accattrp, uint_t flags, int (*waitfp) (caddr_t),
189     caddr_t arg, caddr_t *kaddrp, size_t *real_length,
190     ddi_acc_handle_t *handlep)
191 {
192 	int cache_attr;
193 
194 	if (ddi_dma_mem_alloc(handle, length, accattrp, flags, waitfp, arg,
195 	    kaddrp, real_length, handlep) == DDI_FAILURE) {
196 		return (DDI_FAILURE);
197 	}
198 
199 	if (accattrp == NULL)
200 		return (DDI_SUCCESS);
201 
202 	if (accattrp->devacc_attr_dataorder == DDI_STRICTORDER_ACC)
203 		cache_attr = GFXP_MEMORY_UNCACHED;
204 	else if (accattrp->devacc_attr_dataorder == DDI_MERGING_OK_ACC)
205 		cache_attr = GFXP_MEMORY_WRITECOMBINED;
206 	else
207 		return (DDI_SUCCESS);
208 
209 	gfxp_fix_mem_cache_attrs(*kaddrp, *real_length, cache_attr);
210 
211 	return (DDI_SUCCESS);
212 }
213 
214 int
215 gfxp_mlock_user_memory(caddr_t address, size_t length)
216 {
217 	struct as *as = ttoproc(curthread)->p_as;
218 	int error = 0;
219 
220 	if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
221 		return (set_errno(EINVAL));
222 
223 	if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
224 	    RANGE_OKAY)
225 		return (set_errno(ENOMEM));
226 
227 	error = as_ctl(as, address, length, MC_LOCK, 0, 0, NULL, 0);
228 	if (error)
229 		(void) set_errno(error);
230 
231 	return (error);
232 }
233 
234 int
235 gfxp_munlock_user_memory(caddr_t address, size_t length)
236 {
237 	struct as *as = ttoproc(curthread)->p_as;
238 	int error = 0;
239 
240 	if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
241 		return (set_errno(EINVAL));
242 
243 	if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
244 	    RANGE_OKAY)
245 		return (set_errno(ENOMEM));
246 
247 	error = as_ctl(as, address, length, MC_UNLOCK, 0, 0, NULL, 0);
248 	if (error)
249 		(void) set_errno(error);
250 
251 	return (error);
252 }
253