1945Srugrat /*
2945Srugrat * CDDL HEADER START
3945Srugrat *
4945Srugrat * The contents of this file are subject to the terms of the
51595Srugrat * Common Development and Distribution License (the "License").
61595Srugrat * You may not use this file except in compliance with the License.
7945Srugrat *
8945Srugrat * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9945Srugrat * or http://www.opensolaris.org/os/licensing.
10945Srugrat * See the License for the specific language governing permissions
11945Srugrat * and limitations under the License.
12945Srugrat *
13945Srugrat * When distributing Covered Code, include this CDDL HEADER in each
14945Srugrat * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15945Srugrat * If applicable, add the following below this CDDL HEADER, with the
16945Srugrat * fields enclosed by brackets "[]" replaced with your own identifying
17945Srugrat * information: Portions Copyright [yyyy] [name of copyright owner]
18945Srugrat *
19945Srugrat * CDDL HEADER END
20945Srugrat */
21945Srugrat /*
221595Srugrat * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23945Srugrat * Use is subject to license terms.
24945Srugrat */
25945Srugrat
26945Srugrat #pragma ident "%Z%%M% %I% %E% SMI"
27945Srugrat
28945Srugrat #include <sys/debug.h>
29945Srugrat #include <sys/types.h>
30945Srugrat #include <sys/param.h>
31945Srugrat #include <sys/time.h>
32945Srugrat #include <sys/buf.h>
33945Srugrat #include <sys/errno.h>
34945Srugrat #include <sys/systm.h>
35945Srugrat #include <sys/conf.h>
36945Srugrat #include <sys/signal.h>
37945Srugrat #include <vm/page.h>
38945Srugrat #include <vm/as.h>
39945Srugrat #include <vm/hat.h>
40945Srugrat #include <vm/seg.h>
41945Srugrat #include <vm/seg_dev.h>
42945Srugrat #include <vm/hat_i86.h>
43945Srugrat #include <sys/ddi.h>
44945Srugrat #include <sys/devops.h>
45945Srugrat #include <sys/sunddi.h>
46945Srugrat #include <sys/ddi_impldefs.h>
47945Srugrat #include <sys/fs/snode.h>
48945Srugrat #include <sys/pci.h>
49945Srugrat #include <sys/vmsystm.h>
50945Srugrat #include "gfx_private.h"
51945Srugrat
52945Srugrat /*
53945Srugrat * clone of ddi_segmap_setup(). Respects the requested cache
54945Srugrat * attributes so hat_devload() gives user space WC and
55945Srugrat * UC mappings for system memory.
56945Srugrat */
57945Srugrat
58945Srugrat /*ARGSUSED*/
59945Srugrat 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)60945Srugrat gfxp_ddi_segmap_setup(dev_t dev, off_t offset, struct as *as, caddr_t *addrp,
61945Srugrat off_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cred,
62945Srugrat ddi_device_acc_attr_t *accattrp, uint_t rnumber)
63945Srugrat {
64945Srugrat struct segdev_crargs dev_a;
65945Srugrat int (*mapfunc)(dev_t dev, off_t off, int prot);
66945Srugrat uint_t hat_attr;
67945Srugrat pfn_t pfn;
68945Srugrat int error, i;
69945Srugrat
70945Srugrat if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
71945Srugrat return (ENODEV);
72945Srugrat
73945Srugrat /*
74945Srugrat * Character devices that support the d_mmap
75945Srugrat * interface can only be mmap'ed shared.
76945Srugrat */
77945Srugrat if ((flags & MAP_TYPE) != MAP_SHARED)
78945Srugrat return (EINVAL);
79945Srugrat
80945Srugrat /*
81945Srugrat * Check that this region is indeed mappable on this platform.
82945Srugrat * Use the mapping function.
83945Srugrat */
84945Srugrat if (ddi_device_mapping_check(dev, accattrp, rnumber, &hat_attr) == -1)
85945Srugrat return (ENXIO);
86945Srugrat
87945Srugrat if (accattrp != NULL) {
88945Srugrat switch (accattrp->devacc_attr_dataorder) {
89945Srugrat case DDI_STRICTORDER_ACC:
90945Srugrat /* Want UC */
91945Srugrat hat_attr &= ~HAT_ORDER_MASK;
92945Srugrat hat_attr |= (HAT_STRICTORDER | HAT_PLAT_NOCACHE);
93945Srugrat break;
94945Srugrat case DDI_MERGING_OK_ACC:
95945Srugrat /* Want WC */
96945Srugrat hat_attr &= ~HAT_ORDER_MASK;
97945Srugrat hat_attr |= (HAT_MERGING_OK | HAT_PLAT_NOCACHE);
98945Srugrat break;
99945Srugrat }
100945Srugrat }
101945Srugrat
102945Srugrat /*
103945Srugrat * Check to ensure that the entire range is
104945Srugrat * legal and we are not trying to map in
105945Srugrat * more than the device will let us.
106945Srugrat */
107945Srugrat for (i = 0; i < len; i += PAGESIZE) {
108945Srugrat if (i == 0) {
109945Srugrat /*
110945Srugrat * Save the pfn at offset here. This pfn will be
111945Srugrat * used later to get user address.
112945Srugrat */
113945Srugrat if ((pfn = (pfn_t)cdev_mmap(mapfunc, dev, offset,
114945Srugrat maxprot)) == PFN_INVALID)
115945Srugrat return (ENXIO);
116945Srugrat } else {
117945Srugrat if (cdev_mmap(mapfunc, dev, offset + i, maxprot) ==
118945Srugrat PFN_INVALID)
119945Srugrat return (ENXIO);
120945Srugrat }
121945Srugrat }
122945Srugrat
123945Srugrat as_rangelock(as);
124945Srugrat if ((flags & MAP_FIXED) == 0) {
125945Srugrat /*
126945Srugrat * Pick an address w/o worrying about
127945Srugrat * any vac alignment constraints.
128945Srugrat */
129945Srugrat map_addr(addrp, len, ptob(pfn), 0, flags);
130945Srugrat if (*addrp == NULL) {
131945Srugrat as_rangeunlock(as);
132945Srugrat return (ENOMEM);
133945Srugrat }
134945Srugrat } else {
135945Srugrat /*
136945Srugrat * User-specified address; blow away any previous mappings.
137945Srugrat */
138945Srugrat (void) as_unmap(as, *addrp, len);
139945Srugrat }
140945Srugrat
141945Srugrat dev_a.mapfunc = mapfunc;
142945Srugrat dev_a.dev = dev;
143945Srugrat dev_a.offset = (offset_t)offset;
144945Srugrat dev_a.type = flags & MAP_TYPE;
145945Srugrat dev_a.prot = (uchar_t)prot;
146945Srugrat dev_a.maxprot = (uchar_t)maxprot;
147945Srugrat dev_a.hat_attr = hat_attr;
148*1679Srugrat #if DEBUG
149*1679Srugrat dev_a.hat_flags = 0;
150*1679Srugrat #else
1511595Srugrat dev_a.hat_flags = HAT_LOAD_LOCK;
152*1679Srugrat #endif
153945Srugrat dev_a.devmap_data = NULL;
154945Srugrat
155945Srugrat error = as_map(as, *addrp, len, segdev_create, &dev_a);
156945Srugrat as_rangeunlock(as);
157945Srugrat
158945Srugrat return (error);
159945Srugrat }
160