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