xref: /illumos-gate/usr/src/uts/i86pc/io/gfx_private/gfxp_vm.c (revision a07094369b21309434206d9b3601d162693466fc)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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/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