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