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
gfxp_ddi_segmap_setup(dev_t dev,off_t offset,struct as * as,caddr_t * addrp,off_t len,uint_t prot,uint_t maxprot,uint_t flags,cred_t * cred,ddi_device_acc_attr_t * accattrp,uint_t rnumber)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 if (len == 0)
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 /*
109 * Save the pfn at offset here. This pfn will be
110 * used later to get user address.
111 */
112 pfn = (pfn_t)cdev_mmap(mapfunc, dev, offset, maxprot);
113 if (pfn == PFN_INVALID)
114 return (ENXIO);
115 for (i = PAGESIZE; i < len; i += PAGESIZE) {
116 if (cdev_mmap(mapfunc, dev, offset + i, maxprot) == PFN_INVALID)
117 return (ENXIO);
118 }
119
120 as_rangelock(as);
121 if ((flags & MAP_FIXED) == 0) {
122 /*
123 * Pick an address w/o worrying about
124 * any vac alignment constraints.
125 */
126 map_addr(addrp, len, ptob(pfn), 0, flags);
127 if (*addrp == NULL) {
128 as_rangeunlock(as);
129 return (ENOMEM);
130 }
131 } else {
132 /*
133 * User-specified address; blow away any previous mappings.
134 */
135 (void) as_unmap(as, *addrp, len);
136 }
137
138 dev_a.mapfunc = mapfunc;
139 dev_a.dev = dev;
140 dev_a.offset = (offset_t)offset;
141 dev_a.type = flags & MAP_TYPE;
142 dev_a.prot = (uchar_t)prot;
143 dev_a.maxprot = (uchar_t)maxprot;
144 dev_a.hat_attr = hat_attr;
145 #if DEBUG
146 dev_a.hat_flags = 0;
147 #else
148 dev_a.hat_flags = HAT_LOAD_LOCK;
149 #endif
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