xref: /titanic_44/usr/src/uts/sun4/io/px/px_dma.h (revision f6db9f272f0061301cfaa1c0001b7d636eae31f4)
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 #ifndef	_SYS_PX_DMA_H
28 #define	_SYS_PX_DMA_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 typedef	pfn_t px_iopfn_t;
37 
38 #define	MAKE_DMA_COOKIE(cp, address, size)	\
39 	{					\
40 		(cp)->dmac_notused = 0;		\
41 		(cp)->dmac_type = 0;		\
42 		(cp)->dmac_laddress = (address);	\
43 		(cp)->dmac_size = (size);	\
44 	}
45 
46 #define	HAS_REDZONE(mp)	(((mp)->dmai_rflags & DDI_DMA_REDZONE) ? 1 : 0)
47 
48 typedef struct px_dma_hdl {
49 	ddi_dma_impl_t	pdh_ddi_hdl;
50 	ddi_dma_attr_t	pdh_attr_dev;
51 } px_dma_hdl_t;
52 
53 struct px_dma_impl { /* forthdebug only, keep in sync with ddi_dma_impl_t */
54 	ulong_t		dmai_mapping;
55 	uint_t		dmai_size;
56 	off_t		dmai_offset;
57 	uint_t		dmai_minxfer;
58 	uint_t		dmai_burstsizes;
59 	uint_t		dmai_ndvmapages;
60 	uint_t		dmai_roffset;
61 	uint_t		dmai_rflags;
62 	uint_t		dmai_flags;
63 	uint_t		dmai_nwin;
64 	uint_t		dmai_winsize;
65 	caddr_t		dmai_tte;
66 	void		*dmai_pfnlst;
67 	uint_t		*dmai_pfn0;
68 	void		*dmai_winlst;
69 	dev_info_t	*dmai_rdip;
70 	ddi_dma_obj_t	dmai_object;
71 	ddi_dma_attr_t	dmai_attr_aug;
72 	ddi_dma_cookie_t *dmai_cookie;
73 
74 	int		(*dmai_fault_check)(struct ddi_dma_impl *handle);
75 	void		(*dmai_fault_notify)(struct ddi_dma_impl *handle);
76 	int		dmai_fault;
77 
78 	ddi_dma_attr_t	dmai_attr_dev;
79 };
80 
81 /* Included in case other px-specific flags are added later. */
82 #define	PX_DMA_SYNC_DDI_FLAGS	((1 << 16) - 1)	/* Look for only DDI flags */
83 
84 /*
85  * flags for overloading dmai_inuse field of the dma request
86  * structure:
87  */
88 #define	dmai_flags		dmai_inuse
89 #define	dmai_tte		dmai_nexus_private
90 #define	dmai_fdvma		dmai_nexus_private
91 #define	dmai_pfnlst		dmai_iopte
92 #define	dmai_winlst		dmai_minfo
93 #define	dmai_pfn0		dmai_sbi
94 #define	dmai_roffset		dmai_pool
95 #define	MP_PFN0(mp)		((px_iopfn_t)(mp)->dmai_pfn0)
96 #define	WINLST(mp)		((px_dma_win_t *)(mp)->dmai_winlst)
97 #define	DEV_ATTR(mp)		((ddi_dma_attr_t *)(mp + 1))
98 #define	SET_DMAATTR(p, lo, hi, nocross, cntmax)	\
99 	(p)->dma_attr_addr_lo	= (lo); \
100 	(p)->dma_attr_addr_hi	= (hi); \
101 	(p)->dma_attr_seg	= (nocross); \
102 	(p)->dma_attr_count_max	= (cntmax);
103 
104 #define	SET_DMAALIGN(p, align)	\
105 	(p)->dma_attr_align = (align);
106 
107 #define	DMAI_FLAGS_INUSE	0x1
108 #define	DMAI_FLAGS_BYPASSREQ	0x2
109 #define	DMAI_FLAGS_PEER_ONLY	0x4
110 #define	DMAI_FLAGS_NOCTX	0x8
111 #define	DMAI_FLAGS_DVMA		0x10
112 #define	DMAI_FLAGS_BYPASS	0x20
113 #define	DMAI_FLAGS_PTP		0x40
114 #define	DMAI_FLAGS_DMA		(DMAI_FLAGS_BYPASS | DMAI_FLAGS_PTP)
115 #define	DMAI_FLAGS_DMA_TYPE	(DMAI_FLAGS_DMA | DMAI_FLAGS_DVMA)
116 #define	DMAI_FLAGS_CONTEXT	0x100
117 #define	DMAI_FLAGS_FASTTRACK	0x200
118 #define	DMAI_FLAGS_VMEMCACHE	0x400
119 #define	DMAI_FLAGS_PGPFN	0x800
120 #define	DMAI_FLAGS_NOSYSLIMIT	0x1000
121 #define	DMAI_FLAGS_NOFASTLIMIT	0x2000
122 #define	DMAI_FLAGS_NOSYNC	0x4000
123 #define	DMAI_FLAGS_PTP32	0x10000
124 #define	DMAI_FLAGS_PTP64	0x20000
125 #define	DMAI_FLAGS_PRESERVE	(DMAI_FLAGS_PEER_ONLY | DMAI_FLAGS_BYPASSREQ | \
126 	DMAI_FLAGS_NOSYSLIMIT | DMAI_FLAGS_NOFASTLIMIT | DMAI_FLAGS_NOCTX)
127 
128 #define	HAS_NOFASTLIMIT(mp)	((mp)->dmai_flags & DMAI_FLAGS_NOFASTLIMIT)
129 #define	HAS_NOSYSLIMIT(mp)	((mp)->dmai_flags & DMAI_FLAGS_NOSYSLIMIT)
130 #define	PX_DMA_ISPEERONLY(mp)	((mp)->dmai_flags & DMAI_FLAGS_PEER_ONLY)
131 #define	PX_DMA_ISPGPFN(mp)	((mp)->dmai_flags & DMAI_FLAGS_PGPFN)
132 #define	PX_DMA_TYPE(mp)	((mp)->dmai_flags & DMAI_FLAGS_DMA_TYPE)
133 #define	PX_DMA_ISDVMA(mp)	(PX_DMA_TYPE(mp) == DMAI_FLAGS_DVMA)
134 #define	PX_DMA_ISBYPASS(mp)	(PX_DMA_TYPE(mp) == DMAI_FLAGS_BYPASS)
135 #define	PX_DMA_ISPTP(mp)	(PX_DMA_TYPE(mp) == DMAI_FLAGS_PTP)
136 #define	PX_DMA_ISPTP32(mp)	((mp)->dmai_flags & DMAI_FLAGS_PTP32)
137 #define	PX_DMA_ISPTP64(mp)	((mp)->dmai_flags & DMAI_FLAGS_PTP64)
138 #define	PX_DMA_CANFAST(mp)	(((mp)->dmai_ndvmapages + HAS_REDZONE(mp) \
139 		<= px_dvma_page_cache_clustsz) && HAS_NOFASTLIMIT(mp))
140 #define	PX_DMA_WINNPGS(mp)	MMU_BTOP((mp)->dmai_winsize)
141 #define	PX_DMA_CANCACHE(mp)	(!HAS_REDZONE(mp) && \
142 		(PX_DMA_WINNPGS(mp) == 1) && HAS_NOSYSLIMIT(mp))
143 
144 #define	DEV_NOFASTLIMIT(lo, hi, fastlo, fasthi, align_pg) \
145 	(((lo) <= (fastlo)) && ((hi) >= (fasthi)) && \
146 	((align_pg) <= px_dvma_page_cache_clustsz))
147 
148 #define	DEV_NOSYSLIMIT(lo, hi, syslo, syshi, align_pg) \
149 	(((lo) <= (syslo)) && ((hi) >= (syshi)) && (align_pg == 1))
150 
151 #define	PX_DMA_NOCTX(rdip) (!px_use_contexts || (px_ctx_no_active_flush && \
152 	ddi_prop_exists(DDI_DEV_T_ANY, rdip, \
153 		DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "active-dma-flush")))
154 #define	PX_DMA_USECTX(mp)	(!(mp->dmai_flags & DMAI_FLAGS_NOCTX))
155 
156 #define	PX_DMA_BADPTP(pfn, attrp) \
157 	((IOMMU_PTOB(pfn) < attrp->dma_attr_addr_lo) || \
158 	(IOMMU_PTOB(pfn) > attrp->dma_attr_addr_hi))
159 #define	PX_DMA_CURWIN(mp) \
160 	(((mp)->dmai_offset + (mp)->dmai_roffset) / (mp)->dmai_winsize)
161 
162 #ifdef PX_DMA_PROF
163 
164 /* collect fast track failure statistics */
165 #define	PX_DVMA_FASTTRAK_PROF(mp) { \
166 if ((mp->dmai_ndvmapages + HAS_REDZONE(mp)) > px_dvma_page_cache_clustsz) \
167 	px_dvmaft_npages++; \
168 else if (!HAS_NOFASTLIMIT(mp)) \
169 	px_dvmaft_limit++; \
170 }
171 
172 #else /* !PX_DMA_PROF */
173 
174 #define	PX_DVMA_FASTTRAK_PROF(mp)
175 
176 #endif	/* PX_DMA_PROF */
177 
178 typedef struct px_dma_win {
179 	struct px_dma_win *win_next;
180 	uint32_t win_ncookies;
181 	uint32_t win_curseg;
182 	uint64_t win_size;
183 	uint64_t win_offset;
184 	/* cookie table: sizeof (ddi_dma_cookie_t) * win_ncookies */
185 } px_dma_win_t;
186 
187 /* dvma debug records */
188 struct px_dvma_rec {
189 	char *dvma_addr;
190 	uint_t len;
191 	ddi_dma_impl_t *mp;
192 	struct px_dvma_rec *next;
193 };
194 
195 extern int px_dma_attach(px_t *px_p);
196 extern int px_dma_win(dev_info_t *dip, dev_info_t *rdip,
197 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
198 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp);
199 
200 extern ddi_dma_impl_t *px_dma_allocmp(dev_info_t *dip, dev_info_t *rdip,
201 	int (*waitfp)(caddr_t), caddr_t arg);
202 extern void px_dma_freemp(ddi_dma_impl_t *mp);
203 extern void px_dma_freepfn(ddi_dma_impl_t *mp);
204 extern ddi_dma_impl_t *px_dma_lmts2hdl(dev_info_t *dip, dev_info_t *rdip,
205 	px_mmu_t *mmu_p, ddi_dma_req_t *dmareq);
206 extern int px_dma_attr2hdl(px_t *px_p, ddi_dma_impl_t *mp);
207 extern int px_dma_type(px_t *px_p, ddi_dma_req_t *req, ddi_dma_impl_t *mp);
208 extern int px_dma_pfn(px_t *px_p, ddi_dma_req_t *req, ddi_dma_impl_t *mp);
209 extern int px_dvma_win(px_t *px_p, ddi_dma_req_t *r, ddi_dma_impl_t *mp);
210 extern void px_dma_freewin(ddi_dma_impl_t *mp);
211 extern int px_dvma_map_fast(px_mmu_t *mmu_p, ddi_dma_impl_t *mp);
212 extern int px_dvma_map(ddi_dma_impl_t *mp, ddi_dma_req_t *dmareq,
213 	px_mmu_t *mmu_p);
214 extern void px_dvma_unmap(px_mmu_t *mmu_p, ddi_dma_impl_t *mp);
215 extern int px_dma_physwin(px_t *px_p, ddi_dma_req_t *dmareq,
216 	ddi_dma_impl_t *mp);
217 extern int px_dvma_ctl(dev_info_t *dip, dev_info_t *rdip,
218 	ddi_dma_impl_t *mp, enum ddi_dma_ctlops cmd, off_t *offp,
219 	size_t *lenp, caddr_t *objp, uint_t cache_flags);
220 extern int px_dma_ctl(dev_info_t *dip, dev_info_t *rdip,
221 	ddi_dma_impl_t *mp, enum ddi_dma_ctlops cmd, off_t *offp,
222 	size_t *lenp, caddr_t *objp, uint_t cache_flags);
223 
224 #define	PX_GET_MP_NCOOKIES(mp)		((mp)->dmai_ncookies)
225 #define	PX_SET_MP_NCOOKIES(mp, nc)	((mp)->dmai_ncookies = (nc))
226 #define	PX_GET_MP_PFN1_ADDR(mp)	(((px_iopfn_t *)(mp)->dmai_pfnlst) + 1)
227 
228 #define	PX_GET_MP_TTE(tte) \
229 	(((uint64_t)(uintptr_t)(tte) >> 5) << (32 + 5) | \
230 			((uint32_t)(uintptr_t)(tte)) & 0x12)
231 #define	PX_SAVE_MP_TTE(mp, tte)	\
232 	(mp)->dmai_tte = (caddr_t)((uintptr_t)HI32(tte) | ((tte) & 0x12))
233 
234 #define	PX_GET_MP_PFN1(mp, page_no) \
235 	(((px_iopfn_t *)(mp)->dmai_pfnlst)[page_no])
236 #define	PX_GET_MP_PFN(mp, page_no)	((mp)->dmai_ndvmapages == 1 ? \
237 	(px_iopfn_t)(mp)->dmai_pfnlst : PX_GET_MP_PFN1(mp, page_no))
238 
239 #define	PX_SET_MP_PFN(mp, page_no, pfn) { \
240 	if ((mp)->dmai_ndvmapages == 1) { \
241 		ASSERT(!((page_no) || (mp)->dmai_pfnlst)); \
242 		(mp)->dmai_pfnlst = (void *)(pfn); \
243 	} else \
244 		((px_iopfn_t *)(mp)->dmai_pfnlst)[page_no] = \
245 		    (px_iopfn_t)(pfn);			     \
246 }
247 #define	PX_SET_MP_PFN1(mp, page_no, pfn) { \
248 	((px_iopfn_t *)(mp)->dmai_pfnlst)[page_no] = (pfn); \
249 }
250 
251 extern int px_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
252 	ddi_dma_handle_t handle);
253 
254 #if defined(DEBUG)
255 extern void px_dump_dma_handle(uint64_t flag, dev_info_t *dip,
256 	ddi_dma_impl_t *hp);
257 #else
258 #define	px_dump_dma_handle(flag, dip, hp)
259 #endif
260 
261 #ifdef	__cplusplus
262 }
263 #endif
264 
265 #endif	/* _SYS_PX_DMA_H */
266