xref: /illumos-gate/usr/src/uts/sun4/io/efcode/fc_ddi.c (revision 4f364e7c95ee7fd9d5bbeddc1940e92405bb0e72)
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 /*
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 #include <sys/types.h>
30 #include <sys/kmem.h>
31 #include <sys/systm.h>
32 #include <sys/ddi.h>
33 #include <sys/sunddi.h>
34 #include <sys/sunndi.h>
35 #include <sys/ddidmareq.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/ndi_impldefs.h>
38 #include <sys/fcode.h>
39 
40 /*
41  * We want to call the attachment point's dma ctl op, not his parent's
42  * dma ctl op, so we have to code this ourselves.
43  */
44 
45 int
46 fc_ddi_dma_alloc_handle(dev_info_t *dip, ddi_dma_attr_t *attr,
47     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
48 {
49 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
50 	    int (*)(caddr_t), caddr_t, ddi_dma_handle_t *);
51 
52 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_allochdl;
53 	return ((*funcp)(dip, dip, attr, waitfp, arg, handlep));
54 }
55 
56 int
57 fc_ddi_dma_buf_bind_handle(ddi_dma_handle_t handle, struct buf *bp,
58     uint_t flags, int (*waitfp)(caddr_t), caddr_t arg,
59     ddi_dma_cookie_t *cookiep, uint_t *ccountp)
60 {
61 	struct ddi_dma_req dmareq;
62 	ddi_dma_impl_t *hp;
63 	dev_info_t *dip;
64 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
65 	    struct ddi_dma_req *, ddi_dma_cookie_t *, uint_t *);
66 
67 	hp = (ddi_dma_impl_t *)handle;
68 	dip = hp->dmai_rdip;
69 
70 	dmareq.dmar_flags = flags;
71 	dmareq.dmar_fp = waitfp;
72 	dmareq.dmar_arg = arg;
73 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
74 
75 	if ((bp->b_flags & (B_PAGEIO|B_REMAPPED)) == B_PAGEIO) {
76 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
77 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
78 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
79 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
80 	} else {
81 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
82 		if ((bp->b_flags & (B_SHADOW|B_REMAPPED)) == B_SHADOW) {
83 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
84 							bp->b_shadow;
85 			dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
86 		} else {
87 			dmareq.dmar_object.dmao_type =
88 				(bp->b_flags & (B_PHYS | B_REMAPPED))?
89 				DMA_OTYP_BUFVADDR : DMA_OTYP_VADDR;
90 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
91 		}
92 
93 		/*
94 		 * If the buffer has no proc pointer, or the proc
95 		 * struct has the kernel address space, or the buffer has
96 		 * been marked B_REMAPPED (meaning that it is now
97 		 * mapped into the kernel's address space), then
98 		 * the address space is kas (kernel address space).
99 		 */
100 		if (bp->b_proc == NULL || bp->b_proc->p_as == &kas ||
101 		    (bp->b_flags & B_REMAPPED) != 0) {
102 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
103 		} else {
104 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
105 			    bp->b_proc->p_as;
106 		}
107 	}
108 
109 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_bindhdl;
110 	return ((*funcp)(dip, dip, handle, &dmareq, cookiep, ccountp));
111 }
112 
113 int
114 fc_ddi_dma_unbind_handle(ddi_dma_handle_t handle)
115 {
116 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
117 	ddi_dma_impl_t *hp;
118 	dev_info_t *dip;
119 
120 	hp = (ddi_dma_impl_t *)handle;
121 	dip = hp->dmai_rdip;
122 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
123 	return ((*funcp)(dip, dip, handle));
124 }
125 
126 void
127 fc_ddi_dma_free_handle(ddi_dma_handle_t *handlep)
128 {
129 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
130 	ddi_dma_impl_t *hp;
131 	dev_info_t *dip;
132 
133 	hp = (ddi_dma_impl_t *)*handlep;
134 	dip = hp->dmai_rdip;
135 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_freehdl;
136 	(void) (*funcp)(dip, dip, *handlep);
137 }
138 
139 int
140 fc_ddi_dma_sync(ddi_dma_handle_t h, off_t o, size_t l, uint_t whom)
141 {
142 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
143 	dev_info_t *dip;
144 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t, off_t,
145 		size_t, uint_t);
146 
147 	/*
148 	 * the DMA nexus driver will set DMP_NOSYNC if the
149 	 * platform does not require any sync operation. For
150 	 * example if the memory is uncached or consistent
151 	 * and without any I/O write buffers involved.
152 	 */
153 	if ((hp->dmai_rflags & DMP_NOSYNC) == DMP_NOSYNC)
154 		return (DDI_SUCCESS);
155 
156 	dip = hp->dmai_rdip;
157 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
158 	return ((*funcp)(dip, dip, h, o, l, whom));
159 }
160 
161 /*
162  * Create untyped properties, just like 1275 properties.
163  * XXX: Assumes property encoding is the natural byte order.
164  */
165 int
166 fc_ndi_prop_update(dev_t match_dev, dev_info_t *dip,
167     char *name, uchar_t *data, uint_t nelements)
168 {
169 	return (ddi_prop_update_common(match_dev, dip,
170 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY,
171 	    name, data, nelements, ddi_prop_fm_encode_bytes));
172 }
173