xref: /titanic_51/usr/src/uts/sun4/io/px/px_mmu.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 	px_err_add_fh(&px_p->px_fault, PX_ERR_MMU,
135 	    (caddr_t)px_p->px_address[PX_REG_CSR]);
136 
137 	return (DDI_SUCCESS);
138 }
139 
140 void
141 px_mmu_detach(px_t *px_p)
142 {
143 	px_mmu_t *mmu_p = px_p->px_mmu_p;
144 
145 	/*
146 	 * Free the dvma resource map.
147 	 */
148 	vmem_destroy(mmu_p->mmu_dvma_map);
149 
150 	kmem_free(mmu_p->mmu_dvma_cache_locks,
151 	    px_dvma_page_cache_entries);
152 
153 	if (DVMA_DBG_ON(mmu_p))
154 		px_dvma_debug_fini(mmu_p);
155 
156 	mutex_destroy(&mmu_p->dvma_debug_lock);
157 
158 	/*
159 	 * Free the mmu state structure.
160 	 */
161 	kmem_free(mmu_p, sizeof (px_mmu_t));
162 	px_p->px_mmu_p = NULL;
163 }
164 
165 int
166 px_mmu_intr(dev_info_t *dip, px_fh_t *fh_p)
167 {
168 	uint32_t offset = px_fhd_tbl[fh_p->fh_err_id].fhd_st;
169 	uint64_t stat = fh_p->fh_stat;
170 
171 	if (stat)
172 		LOG(DBG_ERR_INTR, dip, "[%x]=%16llx mmu stat\n", offset, stat);
173 	return (stat ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED);
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 	int		ret;
184 
185 	ASSERT(npages <= mp->dmai_ndvmapages);
186 	DBG(DBG_MAP_WIN, mmu_p->mmu_px_p->px_dip,
187 		"px_mmu_map_pages:%x+%x=%x npages=0x%x pfn_index=0x%x\n",
188 		(uint_t)mmu_p->dvma_base_pg, (uint_t)pg_index, dvma_pg,
189 		(uint_t)npages, (uint_t)pfn_index);
190 
191 	if ((ret = px_lib_iommu_map(dip, PCI_TSBID(0, pg_index), npages,
192 	    attr, (void *)mp, pfn_index, MMU_MAP_MP)) != DDI_SUCCESS) {
193 		DBG(DBG_MAP_WIN, mmu_p->mmu_px_p->px_dip,
194 		    "px_mmu_map_pages: px_iommu_map failed, ret %x\n", ret);
195 
196 		return (ret);
197 	}
198 
199 	if (DVMA_DBG_ON(mmu_p))
200 		px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
201 		    mp->dmai_size, mp);
202 
203 	return (ret);
204 }
205 
206 void
207 px_mmu_unmap_pages(px_mmu_t *mmu_p, px_dvma_addr_t dvma_pg, uint_t npages)
208 {
209 	px_dvma_addr_t	pg_index = MMU_PAGE_INDEX(mmu_p, dvma_pg);
210 
211 	DBG(DBG_UNMAP_WIN, mmu_p->mmu_px_p->px_dip,
212 		"px_mmu_unmap_pages:%x+%x=%x npages=0x%x\n",
213 		(uint_t)mmu_p->dvma_base_pg, (uint_t)pg_index, dvma_pg,
214 		(uint_t)npages);
215 
216 	(void) px_lib_iommu_demap(mmu_p->mmu_px_p->px_dip,
217 	    PCI_TSBID(0, pg_index), npages);
218 }
219 
220 /*
221  * px_mmu_map_window - map a dvma window into the mmu
222  * used by: px_dma_win(), px_dma_ctlops() - DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
223  * return value: none
224  */
225 /*ARGSUSED*/
226 int
227 px_mmu_map_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_window_t win_no)
228 {
229 	uint32_t obj_pg0_off = mp->dmai_roffset;
230 	uint32_t win_pg0_off = win_no ? 0 : obj_pg0_off;
231 	size_t win_size = mp->dmai_winsize;
232 	size_t pfn_index = win_size * win_no;			/* temp value */
233 	size_t obj_off = win_no ? pfn_index - obj_pg0_off : 0;	/* xferred sz */
234 	px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping);
235 	size_t res_size = mp->dmai_object.dmao_size - obj_off + win_pg0_off;
236 	int ret = DDI_SUCCESS;
237 
238 	ASSERT(!(win_size & MMU_PAGE_OFFSET));
239 	if (win_no >= mp->dmai_nwin)
240 		return (ret);
241 	if (res_size < win_size)		/* last window */
242 		win_size = res_size;		/* mp->dmai_winsize unchanged */
243 
244 	mp->dmai_mapping = MMU_PTOB(dvma_pg) | win_pg0_off;
245 	mp->dmai_size = win_size - win_pg0_off;	/* cur win xferrable size */
246 	mp->dmai_offset = obj_off;		/* win offset into object */
247 	pfn_index = MMU_BTOP(pfn_index);	/* index into pfnlist */
248 	ret = px_mmu_map_pages(mmu_p, mp, dvma_pg, MMU_BTOPR(win_size),
249 	    pfn_index);
250 
251 	return (ret);
252 }
253 
254 /*
255  * px_mmu_unmap_window
256  * This routine is called to break down the mmu mappings to a dvma window.
257  * Non partial mappings are viewed as single window mapping.
258  * used by: px_dma_unbindhdl(), px_dma_window(),
259  *	and px_dma_ctlops() - DDI_DMA_FREE, DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
260  * return value: none
261  */
262 /*ARGSUSED*/
263 void
264 px_mmu_unmap_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
265 {
266 	px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping);
267 	uint_t npages = MMU_BTOP(mp->dmai_winsize);
268 
269 	px_mmu_unmap_pages(mmu_p, dvma_pg, npages);
270 
271 	if (DVMA_DBG_ON(mmu_p))
272 		px_dvma_free_debug(mmu_p, (char *)mp->dmai_mapping,
273 		    mp->dmai_size, mp);
274 }
275 
276 
277 #if 0
278 /*
279  * The following table is for reference only. It denotes the
280  * the TSB table size measured in number of 8 byte entries.
281  * It is represented by bits 3:0 in the MMU TSB CTRL REG.
282  */
283 static int px_mmu_tsb_sizes[] = {
284 	0x0,		/* 1K */
285 	0x1,		/* 2K */
286 	0x2,		/* 4K */
287 	0x3,		/* 8K */
288 	0x4,		/* 16K */
289 	0x5,		/* 32K */
290 	0x6,		/* 64K */
291 	0x7,		/* 128K */
292 	0x8		/* 256K */
293 };
294 #endif
295 
296 static char *px_mmu_errsts[] = {
297 	"Protection Error", "Invalid Error", "Timeout", "ECC Error(UE)"
298 };
299 
300 /*ARGSUSED*/
301 static int
302 px_log_mmu_err(px_t *px_p)
303 {
304 	/*
305 	 * Place holder, the correct eror bits need tobe logged.
306 	 */
307 	return (0);
308 }
309