xref: /illumos-gate/usr/src/uts/i86pc/io/gfx_private/gfxp_segmap.c (revision 2b74c1dcee1ce5160892682ccfed76c50004acaa)
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 <vm/page.h>
38 #include <vm/as.h>
39 #include <vm/hat.h>
40 #include <vm/seg.h>
41 #include <vm/seg_dev.h>
42 #include <vm/hat_i86.h>
43 #include <sys/ddi.h>
44 #include <sys/devops.h>
45 #include <sys/sunddi.h>
46 #include <sys/ddi_impldefs.h>
47 #include <sys/fs/snode.h>
48 #include <sys/pci.h>
49 #include <sys/vmsystm.h>
50 #include "gfx_private.h"
51 
52 /*
53  * clone of ddi_segmap_setup(). Respects the requested cache
54  * attributes so hat_devload() gives user space WC and
55  * UC mappings for system memory.
56  */
57 
58 /*ARGSUSED*/
59 int
60 gfxp_ddi_segmap_setup(dev_t dev, off_t offset, struct as *as, caddr_t *addrp,
61     off_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cred,
62     ddi_device_acc_attr_t *accattrp, uint_t rnumber)
63 {
64 	struct segdev_crargs dev_a;
65 	int (*mapfunc)(dev_t dev, off_t off, int prot);
66 	uint_t hat_attr;
67 	pfn_t pfn;
68 	int error, i;
69 
70 	if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
71 		return (ENODEV);
72 
73 	/*
74 	 * Character devices that support the d_mmap
75 	 * interface can only be mmap'ed shared.
76 	 */
77 	if ((flags & MAP_TYPE) != MAP_SHARED)
78 		return (EINVAL);
79 
80 	/*
81 	 * Check that this region is indeed mappable on this platform.
82 	 * Use the mapping function.
83 	 */
84 	if (ddi_device_mapping_check(dev, accattrp, rnumber, &hat_attr) == -1)
85 		return (ENXIO);
86 
87 	if (accattrp != NULL) {
88 		switch (accattrp->devacc_attr_dataorder) {
89 		case DDI_STRICTORDER_ACC:
90 			/* Want UC */
91 			hat_attr &= ~HAT_ORDER_MASK;
92 			hat_attr |= (HAT_STRICTORDER | HAT_PLAT_NOCACHE);
93 			break;
94 		case DDI_MERGING_OK_ACC:
95 			/* Want WC */
96 			hat_attr &= ~HAT_ORDER_MASK;
97 			hat_attr |= (HAT_MERGING_OK | HAT_PLAT_NOCACHE);
98 			break;
99 		}
100 	}
101 
102 	/*
103 	 * Check to ensure that the entire range is
104 	 * legal and we are not trying to map in
105 	 * more than the device will let us.
106 	 */
107 	for (i = 0; i < len; i += PAGESIZE) {
108 		if (i == 0) {
109 			/*
110 			 * Save the pfn at offset here. This pfn will be
111 			 * used later to get user address.
112 			 */
113 			if ((pfn = (pfn_t)cdev_mmap(mapfunc, dev, offset,
114 					maxprot)) == PFN_INVALID)
115 				return (ENXIO);
116 		} else {
117 			if (cdev_mmap(mapfunc, dev, offset + i, maxprot) ==
118 				PFN_INVALID)
119 				return (ENXIO);
120 		}
121 	}
122 
123 	as_rangelock(as);
124 	if ((flags & MAP_FIXED) == 0) {
125 		/*
126 		 * Pick an address w/o worrying about
127 		 * any vac alignment constraints.
128 		 */
129 		map_addr(addrp, len, ptob(pfn), 0, flags);
130 		if (*addrp == NULL) {
131 			as_rangeunlock(as);
132 			return (ENOMEM);
133 		}
134 	} else {
135 		/*
136 		 * User-specified address; blow away any previous mappings.
137 		 */
138 		(void) as_unmap(as, *addrp, len);
139 	}
140 
141 	dev_a.mapfunc = mapfunc;
142 	dev_a.dev = dev;
143 	dev_a.offset = (offset_t)offset;
144 	dev_a.type = flags & MAP_TYPE;
145 	dev_a.prot = (uchar_t)prot;
146 	dev_a.maxprot = (uchar_t)maxprot;
147 	dev_a.hat_attr = hat_attr;
148 #if DEBUG
149 	dev_a.hat_flags = 0;
150 #else
151 	dev_a.hat_flags = HAT_LOAD_LOCK;
152 #endif
153 	dev_a.devmap_data = NULL;
154 
155 	error = as_map(as, *addrp, len, segdev_create, &dev_a);
156 	as_rangeunlock(as);
157 
158 	return (error);
159 }
160