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