xref: /illumos-gate/usr/src/uts/i86pc/io/gfx_private/gfxp_vm.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/debug.h>
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/buf.h>
34 #include <sys/errno.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/signal.h>
38 #include <sys/file.h>
39 #include <sys/uio.h>
40 #include <sys/ioctl.h>
41 #include <sys/map.h>
42 #include <sys/proc.h>
43 #include <sys/user.h>
44 #include <sys/mman.h>
45 #include <sys/cred.h>
46 #include <sys/open.h>
47 #include <sys/stat.h>
48 #include <sys/utsname.h>
49 #include <sys/kmem.h>
50 #include <sys/cmn_err.h>
51 #include <sys/vnode.h>
52 #include <vm/page.h>
53 #include <vm/as.h>
54 #include <vm/hat.h>
55 #include <vm/seg.h>
56 #include <vm/seg_kmem.h>
57 #include <vm/hat_i86.h>
58 #include <sys/vmsystm.h>
59 #include <sys/ddi.h>
60 #include <sys/devops.h>
61 #include <sys/sunddi.h>
62 #include <sys/ddi_impldefs.h>
63 #include <sys/fs/snode.h>
64 #include <sys/pci.h>
65 #include <sys/modctl.h>
66 #include <sys/uio.h>
67 #include <sys/visual_io.h>
68 #include <sys/fbio.h>
69 #include <sys/ddidmareq.h>
70 #include <sys/tnf_probe.h>
71 #include <sys/kstat.h>
72 #include <sys/callb.h>
73 #include <sys/promif.h>
74 #include <sys/atomic.h>
75 #include "gfx_private.h"
76 
77 #ifdef __xpv
78 #include <sys/hypervisor.h>
79 #endif
80 
81 /*
82  * Create a kva mapping for a pa (start..start+size) with
83  * the specified cache attributes (mode).
84  */
85 gfxp_kva_t
86 gfxp_map_kernel_space(uint64_t start, size_t size, uint32_t mode)
87 {
88 	uint_t pgoffset;
89 	uint64_t base;
90 	pgcnt_t npages;
91 	caddr_t cvaddr;
92 	int hat_flags;
93 	uint_t hat_attr;
94 	pfn_t pfn;
95 
96 	if (size == 0)
97 		return (0);
98 
99 #ifdef __xpv
100 	/*
101 	 * The hypervisor doesn't allow r/w mappings to some pages, such as
102 	 * page tables, gdt, etc. Detect %cr3 to notify users of this interface.
103 	 */
104 	if (start == mmu_ptob(mmu_btop(getcr3())))
105 		return (0);
106 #endif
107 
108 	if (mode == GFXP_MEMORY_CACHED)
109 		hat_attr = HAT_STORECACHING_OK;
110 	else if (mode == GFXP_MEMORY_WRITECOMBINED)
111 		hat_attr = HAT_MERGING_OK | HAT_PLAT_NOCACHE;
112 	else	/* GFXP_MEMORY_UNCACHED */
113 		hat_attr = HAT_STRICTORDER | HAT_PLAT_NOCACHE;
114 	hat_flags = HAT_LOAD_LOCK;
115 	pgoffset = start & PAGEOFFSET;
116 	base = start - pgoffset;
117 	npages = btopr(size + pgoffset);
118 	cvaddr = vmem_alloc(heap_arena, ptob(npages), VM_NOSLEEP);
119 	if (cvaddr == NULL)
120 		return (NULL);
121 
122 #ifdef __xpv
123 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
124 	pfn = xen_assign_pfn(mmu_btop(base));
125 #else
126 	pfn = btop(base);
127 #endif
128 
129 	hat_devload(kas.a_hat, cvaddr, ptob(npages), pfn,
130 	    PROT_READ|PROT_WRITE|hat_attr, hat_flags);
131 	return (cvaddr + pgoffset);
132 }
133 
134 /*
135  * Destroy the mapping created by gfxp_map_kernel_space().
136  * Physical memory is not reclaimed.
137  */
138 void
139 gfxp_unmap_kernel_space(gfxp_kva_t address, size_t size)
140 {
141 	uint_t pgoffset;
142 	caddr_t base;
143 	pgcnt_t npages;
144 
145 	if (size == 0 || address == NULL)
146 		return;
147 
148 	pgoffset = (uintptr_t)address & PAGEOFFSET;
149 	base = (caddr_t)address - pgoffset;
150 	npages = btopr(size + pgoffset);
151 	hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
152 	vmem_free(heap_arena, base, ptob(npages));
153 }
154 
155 /*
156  * For a VA return the pfn
157  */
158 int
159 gfxp_va2pa(struct as *as, caddr_t addr, uint64_t *pa)
160 {
161 #ifdef __xpv
162 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
163 	*pa = pa_to_ma(pfn_to_pa(hat_getpfnum(as->a_hat, addr)));
164 #else
165 	*pa = pfn_to_pa(hat_getpfnum(as->a_hat, addr));
166 #endif
167 	return (0);
168 }
169 
170 /*
171  * NOP now
172  */
173 /* ARGSUSED */
174 void
175 gfxp_fix_mem_cache_attrs(caddr_t kva_start, size_t length, int cache_attr)
176 {
177 }
178 
179 int
180 gfxp_ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
181     ddi_device_acc_attr_t  *accattrp, uint_t flags, int (*waitfp) (caddr_t),
182     caddr_t arg, caddr_t *kaddrp, size_t *real_length,
183     ddi_acc_handle_t *handlep)
184 {
185 	uint_t l_flags = flags & ~IOMEM_DATA_MASK; /* clear cache attrs */
186 	int e;
187 
188 	/*
189 	 * Set an appropriate attribute from devacc_attr_dataorder
190 	 * to keep compatibility. The cache attributes are igonred
191 	 * if specified.
192 	 */
193 	if (accattrp != NULL) {
194 		if (accattrp->devacc_attr_dataorder == DDI_STRICTORDER_ACC) {
195 			l_flags |= IOMEM_DATA_UNCACHED;
196 		} else if (accattrp->devacc_attr_dataorder ==
197 		    DDI_MERGING_OK_ACC) {
198 			l_flags |= IOMEM_DATA_UC_WR_COMBINE;
199 		} else {
200 			l_flags |= IOMEM_DATA_CACHED;
201 		}
202 	}
203 
204 	e = ddi_dma_mem_alloc(handle, length, accattrp, l_flags, waitfp,
205 	    arg, kaddrp, real_length, handlep);
206 	return (e);
207 }
208 
209 int
210 gfxp_mlock_user_memory(caddr_t address, size_t length)
211 {
212 	struct as *as = ttoproc(curthread)->p_as;
213 	int error = 0;
214 
215 	if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
216 		return (set_errno(EINVAL));
217 
218 	if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
219 	    RANGE_OKAY)
220 		return (set_errno(ENOMEM));
221 
222 	error = as_ctl(as, address, length, MC_LOCK, 0, 0, NULL, 0);
223 	if (error)
224 		(void) set_errno(error);
225 
226 	return (error);
227 }
228 
229 int
230 gfxp_munlock_user_memory(caddr_t address, size_t length)
231 {
232 	struct as *as = ttoproc(curthread)->p_as;
233 	int error = 0;
234 
235 	if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
236 		return (set_errno(EINVAL));
237 
238 	if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
239 	    RANGE_OKAY)
240 		return (set_errno(ENOMEM));
241 
242 	error = as_ctl(as, address, length, MC_UNLOCK, 0, 0, NULL, 0);
243 	if (error)
244 		(void) set_errno(error);
245 
246 	return (error);
247 }
248 
249 gfx_maddr_t
250 gfxp_convert_addr(paddr_t paddr)
251 {
252 #ifdef __xpv
253 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
254 	return (pfn_to_pa(xen_assign_pfn(btop(paddr))));
255 #else
256 	return ((gfx_maddr_t)paddr);
257 #endif
258 }
259