xref: /titanic_41/usr/src/uts/sun4/io/px/px_mmu.c (revision f2b7ce3eb6db75966c27b08fa312c3158f8dfabf)
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 /*
30  * PX mmu initialization and configuration
31  */
32 #include <sys/types.h>
33 #include <sys/kmem.h>
34 #include <sys/async.h>
35 #include <sys/sysmacros.h>
36 #include <sys/sunddi.h>
37 #include <sys/ddi_impldefs.h>
38 #include <sys/vmem.h>
39 #include <sys/machsystm.h>	/* lddphys() */
40 #include <sys/iommutsb.h>
41 #include "px_obj.h"
42 
43 int
44 px_mmu_attach(px_t *px_p)
45 {
46 	dev_info_t		*dip = px_p->px_dip;
47 	px_mmu_t			*mmu_p;
48 	uint32_t		base_pg_index, i = 0;
49 	char			map_name[32];
50 	px_dvma_range_prop_t	*dvma_prop;
51 	int			dvma_prop_len;
52 	uint32_t		cache_size, tsb_entries;
53 
54 	/*
55 	 * Allocate mmu state structure and link it to the
56 	 * px state structure.
57 	 */
58 	mmu_p = kmem_zalloc(sizeof (px_mmu_t), KM_SLEEP);
59 	if (mmu_p == NULL)
60 		return (DDI_FAILURE);
61 
62 	px_p->px_mmu_p = mmu_p;
63 	mmu_p->mmu_px_p = px_p;
64 	mmu_p->mmu_inst = ddi_get_instance(dip);
65 
66 	/*
67 	 * Check for "virtual-dma" property that specifies
68 	 * the DVMA range.
69 	 */
70 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
71 	    "virtual-dma", (caddr_t)&dvma_prop, &dvma_prop_len) !=
72 	    DDI_PROP_SUCCESS) {
73 
74 		DBG(DBG_ATTACH, dip, "Getting virtual-dma failed\n");
75 
76 		kmem_free(mmu_p, sizeof (px_mmu_t));
77 		px_p->px_mmu_p = NULL;
78 
79 		return (DDI_FAILURE);
80 	}
81 
82 	mmu_p->mmu_dvma_base = dvma_prop->dvma_base;
83 	mmu_p->mmu_dvma_end = dvma_prop->dvma_base +
84 	    (dvma_prop->dvma_len - 1);
85 	tsb_entries = dvma_prop->dvma_len >> 13;
86 
87 	kmem_free(dvma_prop, dvma_prop_len);
88 
89 	/*
90 	 * Setup base and bounds for DVMA and bypass mappings.
91 	 */
92 	mmu_p->mmu_dvma_cache_locks =
93 		kmem_zalloc(px_dvma_page_cache_entries, KM_SLEEP);
94 
95 	mmu_p->dvma_base_pg = MMU_BTOP(mmu_p->mmu_dvma_base);
96 	mmu_p->mmu_dvma_reserve = tsb_entries >> 1;
97 	mmu_p->dvma_end_pg = MMU_BTOP(mmu_p->mmu_dvma_end);
98 
99 	/*
100 	 * Create a virtual memory map for dvma address space.
101 	 * Reserve 'size' bytes of low dvma space for fast track cache.
102 	 */
103 	(void) snprintf(map_name, sizeof (map_name), "%s%d_dvma",
104 	    ddi_driver_name(dip), ddi_get_instance(dip));
105 
106 	cache_size = MMU_PTOB(px_dvma_page_cache_entries *
107 		px_dvma_page_cache_clustsz);
108 	mmu_p->mmu_dvma_fast_end = mmu_p->mmu_dvma_base +
109 		cache_size - 1;
110 
111 	mmu_p->mmu_dvma_map = vmem_create(map_name,
112 	    (void *)(mmu_p->mmu_dvma_fast_end + 1),
113 	    (tsb_entries - cache_size) << MMU_PAGE_SHIFT, MMU_PAGE_SIZE,
114 	    NULL, NULL, NULL, MMU_PAGE_SIZE, VM_SLEEP);
115 
116 	mutex_init(&mmu_p->dvma_debug_lock, NULL, MUTEX_DRIVER, NULL);
117 
118 	base_pg_index = MMU_BTOP(mmu_p->mmu_dvma_end) - tsb_entries + 1;
119 
120 	for (i = 0; i < tsb_entries; i++) {
121 		r_addr_t ra = 0;
122 		io_attributes_t attr;
123 		caddr_t va;
124 
125 		if (px_lib_iommu_getmap(px_p->px_dip, PCI_TSBID(0, i),
126 		    &attr, &ra) == DDI_SUCCESS) {
127 			va = (caddr_t)(MMU_PTOB(base_pg_index + i));
128 			(void) vmem_xalloc(mmu_p->mmu_dvma_map, MMU_PAGE_SIZE,
129 			    MMU_PAGE_SIZE, 0, 0, va, va + MMU_PAGE_SIZE,
130 			    VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
131 		}
132 	}
133 
134 	return (DDI_SUCCESS);
135 }
136 
137 void
138 px_mmu_detach(px_t *px_p)
139 {
140 	px_mmu_t *mmu_p = px_p->px_mmu_p;
141 
142 	/*
143 	 * Free the dvma resource map.
144 	 */
145 	vmem_destroy(mmu_p->mmu_dvma_map);
146 
147 	kmem_free(mmu_p->mmu_dvma_cache_locks,
148 	    px_dvma_page_cache_entries);
149 
150 	if (DVMA_DBG_ON(mmu_p))
151 		px_dvma_debug_fini(mmu_p);
152 
153 	mutex_destroy(&mmu_p->dvma_debug_lock);
154 
155 	/*
156 	 * Free the mmu state structure.
157 	 */
158 	kmem_free(mmu_p, sizeof (px_mmu_t));
159 	px_p->px_mmu_p = NULL;
160 }
161 
162 px_mmu_map_pages(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_dvma_addr_t dvma_pg,
163     size_t npages, size_t pfn_index)
164 {
165 	dev_info_t	*dip = mmu_p->mmu_px_p->px_dip;
166 	px_dvma_addr_t	pg_index = MMU_PAGE_INDEX(mmu_p, dvma_pg);
167 	io_attributes_t	attr = PX_GET_MP_TTE(mp->dmai_tte);
168 	int		ret;
169 
170 	ASSERT(npages <= mp->dmai_ndvmapages);
171 	DBG(DBG_MAP_WIN, mmu_p->mmu_px_p->px_dip,
172 		"px_mmu_map_pages:%x+%x=%x npages=0x%x pfn_index=0x%x\n",
173 		(uint_t)mmu_p->dvma_base_pg, (uint_t)pg_index, dvma_pg,
174 		(uint_t)npages, (uint_t)pfn_index);
175 
176 	if ((ret = px_lib_iommu_map(dip, PCI_TSBID(0, pg_index), npages,
177 	    attr, (void *)mp, pfn_index, MMU_MAP_MP)) != DDI_SUCCESS) {
178 		DBG(DBG_MAP_WIN, mmu_p->mmu_px_p->px_dip,
179 		    "px_mmu_map_pages: px_iommu_map failed, ret %x\n", ret);
180 
181 		return (ret);
182 	}
183 
184 	if (DVMA_DBG_ON(mmu_p))
185 		px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
186 		    mp->dmai_size, mp);
187 
188 	return (ret);
189 }
190 
191 void
192 px_mmu_unmap_pages(px_mmu_t *mmu_p, px_dvma_addr_t dvma_pg, uint_t npages)
193 {
194 	px_dvma_addr_t	pg_index = MMU_PAGE_INDEX(mmu_p, dvma_pg);
195 
196 	DBG(DBG_UNMAP_WIN, mmu_p->mmu_px_p->px_dip,
197 		"px_mmu_unmap_pages:%x+%x=%x npages=0x%x\n",
198 		(uint_t)mmu_p->dvma_base_pg, (uint_t)pg_index, dvma_pg,
199 		(uint_t)npages);
200 
201 	(void) px_lib_iommu_demap(mmu_p->mmu_px_p->px_dip,
202 	    PCI_TSBID(0, pg_index), npages);
203 }
204 
205 /*
206  * px_mmu_map_window - map a dvma window into the mmu
207  * used by: px_dma_win(), px_dma_ctlops() - DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
208  * return value: none
209  */
210 /*ARGSUSED*/
211 int
212 px_mmu_map_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_window_t win_no)
213 {
214 	uint32_t obj_pg0_off = mp->dmai_roffset;
215 	uint32_t win_pg0_off = win_no ? 0 : obj_pg0_off;
216 	size_t win_size = mp->dmai_winsize;
217 	size_t pfn_index = win_size * win_no;			/* temp value */
218 	size_t obj_off = win_no ? pfn_index - obj_pg0_off : 0;	/* xferred sz */
219 	px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping);
220 	size_t res_size = mp->dmai_object.dmao_size - obj_off + win_pg0_off;
221 	int ret = DDI_SUCCESS;
222 
223 	ASSERT(!(win_size & MMU_PAGE_OFFSET));
224 	if (win_no >= mp->dmai_nwin)
225 		return (ret);
226 	if (res_size < win_size)		/* last window */
227 		win_size = res_size;		/* mp->dmai_winsize unchanged */
228 
229 	mp->dmai_mapping = MMU_PTOB(dvma_pg) | win_pg0_off;
230 	mp->dmai_size = win_size - win_pg0_off;	/* cur win xferrable size */
231 	mp->dmai_offset = obj_off;		/* win offset into object */
232 	pfn_index = MMU_BTOP(pfn_index);	/* index into pfnlist */
233 	ret = px_mmu_map_pages(mmu_p, mp, dvma_pg, MMU_BTOPR(win_size),
234 	    pfn_index);
235 
236 	return (ret);
237 }
238 
239 /*
240  * px_mmu_unmap_window
241  * This routine is called to break down the mmu mappings to a dvma window.
242  * Non partial mappings are viewed as single window mapping.
243  * used by: px_dma_unbindhdl(), px_dma_window(),
244  *	and px_dma_ctlops() - DDI_DMA_FREE, DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
245  * return value: none
246  */
247 /*ARGSUSED*/
248 void
249 px_mmu_unmap_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
250 {
251 	px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping);
252 	uint_t npages = MMU_BTOP(mp->dmai_winsize);
253 
254 	px_mmu_unmap_pages(mmu_p, dvma_pg, npages);
255 
256 	if (DVMA_DBG_ON(mmu_p))
257 		px_dvma_free_debug(mmu_p, (char *)mp->dmai_mapping,
258 		    mp->dmai_size, mp);
259 }
260 
261 
262 #if 0
263 /*
264  * The following table is for reference only. It denotes the
265  * the TSB table size measured in number of 8 byte entries.
266  * It is represented by bits 3:0 in the MMU TSB CTRL REG.
267  */
268 static int px_mmu_tsb_sizes[] = {
269 	0x0,		/* 1K */
270 	0x1,		/* 2K */
271 	0x2,		/* 4K */
272 	0x3,		/* 8K */
273 	0x4,		/* 16K */
274 	0x5,		/* 32K */
275 	0x6,		/* 64K */
276 	0x7,		/* 128K */
277 	0x8		/* 256K */
278 };
279 #endif
280 
281 static char *px_mmu_errsts[] = {
282 	"Protection Error", "Invalid Error", "Timeout", "ECC Error(UE)"
283 };
284 
285 /*ARGSUSED*/
286 static int
287 px_log_mmu_err(px_t *px_p)
288 {
289 	/*
290 	 * Place holder, the correct eror bits need tobe logged.
291 	 */
292 	return (0);
293 }
294