1fc1821feSrugrat /*
2fc1821feSrugrat  * CDDL HEADER START
3fc1821feSrugrat  *
4fc1821feSrugrat  * The contents of this file are subject to the terms of the
5b9ae6f87Srugrat  * Common Development and Distribution License (the "License").
6b9ae6f87Srugrat  * You may not use this file except in compliance with the License.
7fc1821feSrugrat  *
8fc1821feSrugrat  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fc1821feSrugrat  * or http://www.opensolaris.org/os/licensing.
10fc1821feSrugrat  * See the License for the specific language governing permissions
11fc1821feSrugrat  * and limitations under the License.
12fc1821feSrugrat  *
13fc1821feSrugrat  * When distributing Covered Code, include this CDDL HEADER in each
14fc1821feSrugrat  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fc1821feSrugrat  * If applicable, add the following below this CDDL HEADER, with the
16fc1821feSrugrat  * fields enclosed by brackets "[]" replaced with your own identifying
17fc1821feSrugrat  * information: Portions Copyright [yyyy] [name of copyright owner]
18fc1821feSrugrat  *
19fc1821feSrugrat  * CDDL HEADER END
20fc1821feSrugrat  */
21fc1821feSrugrat /*
22b9ae6f87Srugrat  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fc1821feSrugrat  * Use is subject to license terms.
24fc1821feSrugrat  */
25fc1821feSrugrat 
26fc1821feSrugrat #include <sys/debug.h>
27fc1821feSrugrat #include <sys/types.h>
28fc1821feSrugrat #include <sys/param.h>
29fc1821feSrugrat #include <sys/time.h>
30fc1821feSrugrat #include <sys/buf.h>
31fc1821feSrugrat #include <sys/errno.h>
32fc1821feSrugrat #include <sys/systm.h>
33fc1821feSrugrat #include <sys/conf.h>
34fc1821feSrugrat #include <sys/signal.h>
35fc1821feSrugrat #include <vm/page.h>
36fc1821feSrugrat #include <vm/as.h>
37fc1821feSrugrat #include <vm/hat.h>
38fc1821feSrugrat #include <vm/seg.h>
39fc1821feSrugrat #include <vm/seg_dev.h>
40fc1821feSrugrat #include <vm/hat_i86.h>
41fc1821feSrugrat #include <sys/ddi.h>
42fc1821feSrugrat #include <sys/devops.h>
43fc1821feSrugrat #include <sys/sunddi.h>
44fc1821feSrugrat #include <sys/ddi_impldefs.h>
45fc1821feSrugrat #include <sys/fs/snode.h>
46fc1821feSrugrat #include <sys/pci.h>
47fc1821feSrugrat #include <sys/vmsystm.h>
48dc1b2691SGordon Ross #include <sys/gfx_private.h>
49fc1821feSrugrat 
50fc1821feSrugrat /*
51fc1821feSrugrat  * clone of ddi_segmap_setup(). Respects the requested cache
52fc1821feSrugrat  * attributes so hat_devload() gives user space WC and
53fc1821feSrugrat  * UC mappings for system memory.
54fc1821feSrugrat  */
55fc1821feSrugrat 
56fc1821feSrugrat /*ARGSUSED*/
57fc1821feSrugrat 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)58fc1821feSrugrat gfxp_ddi_segmap_setup(dev_t dev, off_t offset, struct as *as, caddr_t *addrp,
59fc1821feSrugrat     off_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cred,
60fc1821feSrugrat     ddi_device_acc_attr_t *accattrp, uint_t rnumber)
61fc1821feSrugrat {
62fc1821feSrugrat 	struct segdev_crargs dev_a;
63fc1821feSrugrat 	int (*mapfunc)(dev_t dev, off_t off, int prot);
64fc1821feSrugrat 	uint_t hat_attr;
65fc1821feSrugrat 	pfn_t pfn;
66fc1821feSrugrat 	int error, i;
67fc1821feSrugrat 
68fc1821feSrugrat 	if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
69fc1821feSrugrat 		return (ENODEV);
70fc1821feSrugrat 
71fc1821feSrugrat 	/*
72fc1821feSrugrat 	 * Character devices that support the d_mmap
73fc1821feSrugrat 	 * interface can only be mmap'ed shared.
74fc1821feSrugrat 	 */
75fc1821feSrugrat 	if ((flags & MAP_TYPE) != MAP_SHARED)
76fc1821feSrugrat 		return (EINVAL);
77fc1821feSrugrat 
78*584b574aSToomas Soome 	if (len == 0)
79*584b574aSToomas Soome 		return (EINVAL);
80*584b574aSToomas Soome 
81fc1821feSrugrat 	/*
82fc1821feSrugrat 	 * Check that this region is indeed mappable on this platform.
83fc1821feSrugrat 	 * Use the mapping function.
84fc1821feSrugrat 	 */
85fc1821feSrugrat 	if (ddi_device_mapping_check(dev, accattrp, rnumber, &hat_attr) == -1)
86fc1821feSrugrat 		return (ENXIO);
87fc1821feSrugrat 
88fc1821feSrugrat 	if (accattrp != NULL) {
89fc1821feSrugrat 		switch (accattrp->devacc_attr_dataorder) {
90fc1821feSrugrat 		case DDI_STRICTORDER_ACC:
91fc1821feSrugrat 			/* Want UC */
92fc1821feSrugrat 			hat_attr &= ~HAT_ORDER_MASK;
93fc1821feSrugrat 			hat_attr |= (HAT_STRICTORDER | HAT_PLAT_NOCACHE);
94fc1821feSrugrat 			break;
95fc1821feSrugrat 		case DDI_MERGING_OK_ACC:
96fc1821feSrugrat 			/* Want WC */
97fc1821feSrugrat 			hat_attr &= ~HAT_ORDER_MASK;
98fc1821feSrugrat 			hat_attr |= (HAT_MERGING_OK | HAT_PLAT_NOCACHE);
99fc1821feSrugrat 			break;
100fc1821feSrugrat 		}
101fc1821feSrugrat 	}
102fc1821feSrugrat 
103fc1821feSrugrat 	/*
104fc1821feSrugrat 	 * Check to ensure that the entire range is
105fc1821feSrugrat 	 * legal and we are not trying to map in
106fc1821feSrugrat 	 * more than the device will let us.
107fc1821feSrugrat 	 */
108*584b574aSToomas Soome 	/*
109*584b574aSToomas Soome 	 * Save the pfn at offset here. This pfn will be
110*584b574aSToomas Soome 	 * used later to get user address.
111*584b574aSToomas Soome 	 */
112*584b574aSToomas Soome 	pfn = (pfn_t)cdev_mmap(mapfunc, dev, offset, maxprot);
113*584b574aSToomas Soome 	if (pfn == PFN_INVALID)
114*584b574aSToomas Soome 		return (ENXIO);
115*584b574aSToomas Soome 	for (i = PAGESIZE; i < len; i += PAGESIZE) {
116*584b574aSToomas Soome 		if (cdev_mmap(mapfunc, dev, offset + i, maxprot) == PFN_INVALID)
117*584b574aSToomas Soome 			return (ENXIO);
118fc1821feSrugrat 	}
119fc1821feSrugrat 
120fc1821feSrugrat 	as_rangelock(as);
121fc1821feSrugrat 	if ((flags & MAP_FIXED) == 0) {
122fc1821feSrugrat 		/*
123fc1821feSrugrat 		 * Pick an address w/o worrying about
124fc1821feSrugrat 		 * any vac alignment constraints.
125fc1821feSrugrat 		 */
126fc1821feSrugrat 		map_addr(addrp, len, ptob(pfn), 0, flags);
127fc1821feSrugrat 		if (*addrp == NULL) {
128fc1821feSrugrat 			as_rangeunlock(as);
129fc1821feSrugrat 			return (ENOMEM);
130fc1821feSrugrat 		}
131fc1821feSrugrat 	} else {
132fc1821feSrugrat 		/*
133fc1821feSrugrat 		 * User-specified address; blow away any previous mappings.
134fc1821feSrugrat 		 */
135fc1821feSrugrat 		(void) as_unmap(as, *addrp, len);
136fc1821feSrugrat 	}
137fc1821feSrugrat 
138fc1821feSrugrat 	dev_a.mapfunc = mapfunc;
139fc1821feSrugrat 	dev_a.dev = dev;
140fc1821feSrugrat 	dev_a.offset = (offset_t)offset;
141fc1821feSrugrat 	dev_a.type = flags & MAP_TYPE;
142fc1821feSrugrat 	dev_a.prot = (uchar_t)prot;
143fc1821feSrugrat 	dev_a.maxprot = (uchar_t)maxprot;
144fc1821feSrugrat 	dev_a.hat_attr = hat_attr;
1452b74c1dcSrugrat #if DEBUG
1462b74c1dcSrugrat 	dev_a.hat_flags = 0;
1472b74c1dcSrugrat #else
148b9ae6f87Srugrat 	dev_a.hat_flags = HAT_LOAD_LOCK;
1492b74c1dcSrugrat #endif
150fc1821feSrugrat 	dev_a.devmap_data = NULL;
151fc1821feSrugrat 
152fc1821feSrugrat 	error = as_map(as, *addrp, len, segdev_create, &dev_a);
153fc1821feSrugrat 	as_rangeunlock(as);
154fc1821feSrugrat 
155fc1821feSrugrat 	return (error);
156fc1821feSrugrat }
157