xref: /illumos-gate/usr/src/uts/i86pc/io/gfx_private/gfxp_vm.c (revision fbd1c0dae6f4a2ccc2ce0527c7f19d3dd5ea90b8)
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 /*
78  * Create a kva mapping for a pa (start..start+size) with
79  * the specified cache attributes (mode).
80  */
81 gfxp_kva_t
82 gfxp_map_kernel_space(uint64_t start, size_t size, uint32_t mode)
83 {
84 	uint_t pgoffset;
85 	uint64_t base;
86 	pgcnt_t npages;
87 	caddr_t cvaddr;
88 	int hat_flags;
89 	uint_t hat_attr;
90 
91 	if (size == 0)
92 		return (0);
93 
94 	if (mode == GFXP_MEMORY_CACHED)
95 		hat_attr = HAT_STORECACHING_OK;
96 	else if (mode == GFXP_MEMORY_WRITECOMBINED)
97 		hat_attr = HAT_MERGING_OK | HAT_PLAT_NOCACHE;
98 	else	/* GFXP_MEMORY_UNCACHED */
99 		hat_attr = HAT_STRICTORDER | HAT_PLAT_NOCACHE;
100 	hat_flags = HAT_LOAD_LOCK;
101 	pgoffset = start & PAGEOFFSET;
102 	base = start - pgoffset;
103 	npages = btopr(size + pgoffset);
104 	cvaddr = vmem_alloc(heap_arena, ptob(npages), VM_NOSLEEP);
105 	if (cvaddr == NULL)
106 		return (NULL);
107 	hat_devload(kas.a_hat, cvaddr, ptob(npages), base >> PAGESHIFT,
108 			PROT_READ|PROT_WRITE|hat_attr, hat_flags);
109 	return (cvaddr + pgoffset);
110 }
111 
112 /*
113  * Destroy the mapping created by gfxp_map_kernel_space().
114  * Physical memory is not reclaimed.
115  */
116 void
117 gfxp_unmap_kernel_space(gfxp_kva_t address, size_t size)
118 {
119 	uint_t pgoffset;
120 	caddr_t base;
121 	pgcnt_t npages;
122 
123 	if (size == 0 || address == NULL)
124 		return;
125 
126 	pgoffset = (uintptr_t)address & PAGEOFFSET;
127 	base = (caddr_t)address - pgoffset;
128 	npages = btopr(size + pgoffset);
129 	hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
130 	vmem_free(heap_arena, base, ptob(npages));
131 }
132 
133 /*
134  * For a VA return the pfn
135  */
136 int
137 gfxp_va2pa(struct as *as, caddr_t addr, uint64_t *pa)
138 {
139 	*pa = (uint64_t)(hat_getpfnum(as->a_hat, addr) << PAGESHIFT);
140 	return (0);
141 }
142 
143 /*
144  * NOP now
145  */
146 /* ARGSUSED */
147 void
148 gfxp_fix_mem_cache_attrs(caddr_t kva_start, size_t length, int cache_attr)
149 {
150 }
151 
152 int
153 gfxp_ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
154     ddi_device_acc_attr_t  *accattrp, uint_t flags, int (*waitfp) (caddr_t),
155     caddr_t arg, caddr_t *kaddrp, size_t *real_length,
156     ddi_acc_handle_t *handlep)
157 {
158 	uint_t l_flags = flags & ~IOMEM_DATA_MASK; /* clear cache attrs */
159 	int e;
160 
161 	/*
162 	 * Set an appropriate attribute from devacc_attr_dataorder
163 	 * to keep compatibility. The cache attributes are igonred
164 	 * if specified.
165 	 */
166 	if (accattrp != NULL) {
167 		if (accattrp->devacc_attr_dataorder == DDI_STRICTORDER_ACC) {
168 			l_flags |= IOMEM_DATA_UNCACHED;
169 		} else if (accattrp->devacc_attr_dataorder ==
170 		    DDI_MERGING_OK_ACC) {
171 			l_flags |= IOMEM_DATA_UC_WR_COMBINE;
172 		} else {
173 			l_flags |= IOMEM_DATA_CACHED;
174 		}
175 	}
176 
177 	e = ddi_dma_mem_alloc(handle, length, accattrp, l_flags, waitfp,
178 	    arg, kaddrp, real_length, handlep);
179 	return (e);
180 }
181 
182 int
183 gfxp_mlock_user_memory(caddr_t address, size_t length)
184 {
185 	struct as *as = ttoproc(curthread)->p_as;
186 	int error = 0;
187 
188 	if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
189 		return (set_errno(EINVAL));
190 
191 	if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
192 	    RANGE_OKAY)
193 		return (set_errno(ENOMEM));
194 
195 	error = as_ctl(as, address, length, MC_LOCK, 0, 0, NULL, 0);
196 	if (error)
197 		(void) set_errno(error);
198 
199 	return (error);
200 }
201 
202 int
203 gfxp_munlock_user_memory(caddr_t address, size_t length)
204 {
205 	struct as *as = ttoproc(curthread)->p_as;
206 	int error = 0;
207 
208 	if (((uintptr_t)address & PAGEOFFSET) != 0 || length == 0)
209 		return (set_errno(EINVAL));
210 
211 	if (valid_usr_range(address, length, 0, as, as->a_userlimit) !=
212 	    RANGE_OKAY)
213 		return (set_errno(ENOMEM));
214 
215 	error = as_ctl(as, address, length, MC_UNLOCK, 0, 0, NULL, 0);
216 	if (error)
217 		(void) set_errno(error);
218 
219 	return (error);
220 }
221 
222 gfx_maddr_t
223 gfxp_convert_addr(paddr_t paddr)
224 {
225 	return ((gfx_maddr_t)paddr);
226 }
227