2 * Copyright (c) 2013, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/libkern.h>
34 #include <sys/module.h>
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/ofw_bus_subr.h>
39 #include <dev/ofw/openfirm.h>
41 #include <machine/bus.h>
43 #include <powerpc/pseries/phyp-hvcall.h>
44 #include <powerpc/pseries/plpar_iommu.h>
46 MALLOC_DEFINE(M_PHYPIOMMU, "iommu", "IOMMU data for PAPR LPARs");
48 struct papr_iommu_map {
51 struct papr_iommu_map *next;
54 static SLIST_HEAD(iommu_maps, iommu_map) iommu_map_head =
55 SLIST_HEAD_INITIALIZER(iommu_map_head);
56 static int papr_supports_stuff_tce = -1;
62 SLIST_ENTRY(iommu_map) entries;
66 struct iommu_map *map;
72 phyp_iommu_set_dma_tag(device_t bus, device_t dev, bus_dma_tag_t tag)
76 cell_t dma_acells, dma_scells, dmawindow[6];
80 for (p = dev; device_get_parent(p) != NULL; p = device_get_parent(p)) {
81 if (ofw_bus_has_prop(p, "ibm,my-dma-window"))
83 if (ofw_bus_has_prop(p, "ibm,dma-window"))
90 node = ofw_bus_get_node(p);
91 if (OF_getencprop(node, "ibm,#dma-size-cells", &dma_scells,
93 OF_searchencprop(node, "#size-cells", &dma_scells,
95 if (OF_getencprop(node, "ibm,#dma-address-cells", &dma_acells,
97 OF_searchencprop(node, "#address-cells", &dma_acells,
100 if (ofw_bus_has_prop(p, "ibm,my-dma-window"))
101 OF_getencprop(node, "ibm,my-dma-window", dmawindow,
102 sizeof(cell_t)*(dma_scells + dma_acells + 1));
104 OF_getencprop(node, "ibm,dma-window", dmawindow,
105 sizeof(cell_t)*(dma_scells + dma_acells + 1));
107 struct dma_window *window = malloc(sizeof(struct dma_window),
108 M_PHYPIOMMU, M_WAITOK);
110 for (cell = 1; cell < 1 + dma_acells; cell++) {
111 window->start <<= 32;
112 window->start |= dmawindow[cell];
115 for (; cell < 1 + dma_acells + dma_scells; cell++) {
117 window->end |= dmawindow[cell];
119 window->end += window->start;
122 device_printf(dev, "Mapping IOMMU domain %#x\n", dmawindow[0]);
124 SLIST_FOREACH(i, &iommu_map_head, entries) {
125 if (i->iobn == dmawindow[0]) {
131 if (window->map == NULL) {
132 window->map = malloc(sizeof(struct iommu_map), M_PHYPIOMMU,
134 window->map->iobn = dmawindow[0];
136 * Allocate IOMMU range beginning at PAGE_SIZE. Some drivers
137 * (em(4), for example) do not like getting mappings at 0.
139 window->map->vmem = vmem_create("IOMMU mappings", PAGE_SIZE,
140 trunc_page(VMEM_ADDR_MAX) - PAGE_SIZE, PAGE_SIZE, 0,
141 M_BESTFIT | M_NOWAIT);
142 SLIST_INSERT_HEAD(&iommu_map_head, window->map, entries);
146 * Check experimentally whether we can use H_STUFF_TCE. It is required
147 * by the spec but some firmware (e.g. QEMU) does not actually support
150 if (papr_supports_stuff_tce == -1)
151 papr_supports_stuff_tce = !(phyp_hcall(H_STUFF_TCE,
152 window->map->iobn, 0, 0, 0) == H_FUNCTION);
154 bus_dma_tag_set_iommu(tag, bus, window);
160 phyp_iommu_map(device_t dev, bus_dma_segment_t *segs, int *nsegs,
161 bus_addr_t min, bus_addr_t max, bus_size_t alignment, bus_addr_t boundary,
164 struct dma_window *window = cookie;
165 bus_addr_t minaddr, maxaddr;
167 bus_size_t allocsize;
170 minaddr = window->start;
171 maxaddr = window->end;
173 /* XXX: handle exclusion range in a more useful way */
177 /* XXX: consolidate segs? */
178 for (i = 0; i < *nsegs; i++) {
179 allocsize = round_page(segs[i].ds_len +
180 (segs[i].ds_addr & PAGE_MASK));
181 error = vmem_xalloc(window->map->vmem, allocsize,
182 (alignment < PAGE_SIZE) ? PAGE_SIZE : alignment, 0,
183 boundary, minaddr, maxaddr, M_BESTFIT | M_NOWAIT, &alloced);
185 panic("VMEM failure: %d\n", error);
188 KASSERT(alloced % PAGE_SIZE == 0, ("Alloc not page aligned"));
189 KASSERT((alloced + (segs[i].ds_addr & PAGE_MASK)) %
191 ("Allocated segment does not match alignment constraint"));
193 tce = trunc_page(segs[i].ds_addr);
194 tce |= 0x3; /* read/write */
195 for (j = 0; j < allocsize; j += PAGE_SIZE) {
196 error = phyp_hcall(H_PUT_TCE, window->map->iobn,
197 alloced + j, tce + j);
199 panic("IOMMU mapping error: %d\n", error);
204 segs[i].ds_addr = alloced + (segs[i].ds_addr & PAGE_MASK);
205 KASSERT(segs[i].ds_addr > 0, ("Address needs to be positive"));
206 KASSERT(segs[i].ds_addr + segs[i].ds_len < maxaddr,
207 ("Address not in range"));
209 panic("IOMMU mapping error: %d\n", error);
218 phyp_iommu_unmap(device_t dev, bus_dma_segment_t *segs, int nsegs, void *cookie)
220 struct dma_window *window = cookie;
221 bus_addr_t pageround;
222 bus_size_t roundedsize;
226 for (i = 0; i < nsegs; i++) {
227 pageround = trunc_page(segs[i].ds_addr);
228 roundedsize = round_page(segs[i].ds_len +
229 (segs[i].ds_addr & PAGE_MASK));
231 if (papr_supports_stuff_tce) {
232 phyp_hcall(H_STUFF_TCE, window->map->iobn, pageround, 0,
233 roundedsize/PAGE_SIZE);
235 for (j = 0; j < roundedsize; j += PAGE_SIZE)
236 phyp_hcall(H_PUT_TCE, window->map->iobn,
240 vmem_xfree(window->map->vmem, pageround, roundedsize);