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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/debug.h>
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/buf.h>
32 #include <sys/errno.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/signal.h>
36 #include <vm/page.h>
37 #include <vm/as.h>
38 #include <vm/hat.h>
39 #include <vm/seg.h>
40 #include <vm/seg_dev.h>
41 #include <vm/hat_i86.h>
42 #include <sys/ddi.h>
43 #include <sys/devops.h>
44 #include <sys/sunddi.h>
45 #include <sys/ddi_impldefs.h>
46 #include <sys/fs/snode.h>
47 #include <sys/pci.h>
48 #include <sys/vmsystm.h>
49 #include <sys/int_fmtio.h>
50 #include <sys/gfx_private.h>
51 
52 #ifdef __xpv
53 #include <sys/hypervisor.h>
54 #endif
55 
56 /*
57  * Create a dummy ddi_umem_cookie given to gfxp_devmap_umem_setup().
58  */
59 ddi_umem_cookie_t
gfxp_umem_cookie_init(caddr_t kva,size_t size)60 gfxp_umem_cookie_init(caddr_t kva, size_t size)
61 {
62 	struct ddi_umem_cookie *umem_cookie;
63 
64 	umem_cookie = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
65 
66 	if (umem_cookie == NULL)
67 		return (NULL);
68 
69 	umem_cookie->cvaddr = kva;
70 	umem_cookie->type = KMEM_NON_PAGEABLE;
71 	umem_cookie->size = size;
72 
73 	return ((ddi_umem_cookie_t)umem_cookie);
74 }
75 
76 void
gfxp_umem_cookie_destroy(ddi_umem_cookie_t cookie)77 gfxp_umem_cookie_destroy(ddi_umem_cookie_t cookie)
78 {
79 	kmem_free(cookie, sizeof (struct ddi_umem_cookie));
80 }
81 
82 /*
83  * called by driver devmap routine to pass kernel virtual address mapping
84  * info to the framework.
85  */
86 /*ARGSUSED*/
87 int
gfxp_devmap_umem_setup(devmap_cookie_t dhc,dev_info_t * dip,struct devmap_callback_ctl * callbackops,ddi_umem_cookie_t cookie,offset_t off,size_t len,uint_t maxprot,uint_t flags,ddi_device_acc_attr_t * accattrp)88 gfxp_devmap_umem_setup(devmap_cookie_t dhc, dev_info_t *dip,
89     struct devmap_callback_ctl *callbackops, ddi_umem_cookie_t cookie,
90     offset_t off, size_t len, uint_t maxprot, uint_t flags,
91     ddi_device_acc_attr_t *accattrp)
92 {
93 	uint_t l_flags = flags & ~IOMEM_DATA_MASK; /* clear cache attrs */
94 	int e;
95 
96 	/*
97 	 * Set an appropriate attribute from devacc_attr_dataorder
98 	 * to keep compatibility. The cache attributes are igonred
99 	 * if specified.
100 	 */
101 	if (accattrp != NULL) {
102 		if (accattrp->devacc_attr_dataorder == DDI_STRICTORDER_ACC) {
103 			l_flags |= IOMEM_DATA_UNCACHED;
104 		} else if (accattrp->devacc_attr_dataorder ==
105 		    DDI_MERGING_OK_ACC) {
106 			l_flags |= IOMEM_DATA_UC_WR_COMBINE;
107 		} else {
108 			l_flags |= IOMEM_DATA_CACHED;
109 		}
110 	}
111 
112 	e = devmap_umem_setup(dhc, dip, callbackops, cookie, off, len, maxprot,
113 	    l_flags, accattrp);
114 	return (e);
115 }
116 
117 /*
118  * Replacement for devmap_devmem_setup() which will map a machine address
119  * instead of a register set/offset.
120  */
121 void
gfxp_map_devmem(devmap_cookie_t dhc,gfx_maddr_t maddr,size_t length,ddi_device_acc_attr_t * attrp)122 gfxp_map_devmem(devmap_cookie_t dhc, gfx_maddr_t maddr, size_t length,
123     ddi_device_acc_attr_t *attrp)
124 {
125 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
126 	pfn_t pfn;
127 
128 
129 #ifdef __xpv
130 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
131 	pfn = xen_assign_pfn(mmu_btop(maddr));
132 #else
133 	pfn = mmu_btop(maddr);
134 #endif
135 
136 	dhp->dh_pfn = pfn;
137 	dhp->dh_len = mmu_ptob(mmu_btopr(length));
138 	dhp->dh_roff = 0;
139 
140 #ifndef DEVMAP_DEVMEM_COOKIE
141 #define	DEVMAP_DEVMEM_COOKIE	((ddi_umem_cookie_t)0x1) /* XXPV */
142 #endif /* DEVMAP_DEVMEM_COOKIE */
143 	dhp->dh_cookie = DEVMAP_DEVMEM_COOKIE;
144 	/*LINTED: E_EXPR_NULL_EFFECT*/
145 	dhp->dh_flags |= DEVMAP_DEFAULTS;
146 	dhp->dh_maxprot = PROT_ALL & dhp->dh_orig_maxprot;
147 
148 	/* no callbacks needed */
149 	bzero(&dhp->dh_callbackops, sizeof (struct devmap_callback_ctl));
150 
151 	switch (attrp->devacc_attr_dataorder) {
152 	case DDI_UNORDERED_OK_ACC:
153 		dhp->dh_hat_attr = HAT_UNORDERED_OK;
154 		break;
155 	case DDI_MERGING_OK_ACC:
156 		dhp->dh_hat_attr = HAT_MERGING_OK;
157 		break;
158 	case DDI_LOADCACHING_OK_ACC:
159 		dhp->dh_hat_attr = HAT_LOADCACHING_OK;
160 		break;
161 	case DDI_STORECACHING_OK_ACC:
162 		dhp->dh_hat_attr = HAT_STORECACHING_OK;
163 		break;
164 	case DDI_STRICTORDER_ACC:
165 	default:
166 		dhp->dh_hat_attr = HAT_STRICTORDER;
167 	}
168 
169 	/* don't use large pages */
170 	dhp->dh_mmulevel = 0;
171 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
172 
173 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
174 }
175