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