1251607Sdim/*- 2251607Sdim * Copyright (c) 2013, Nathan Whitehorn <nwhitehorn@FreeBSD.org> 3251607Sdim * All rights reserved. 4251607Sdim * 5251607Sdim * Redistribution and use in source and binary forms, with or without 6251607Sdim * modification, are permitted provided that the following conditions 7251607Sdim * are met: 8251607Sdim * 1. Redistributions of source code must retain the above copyright 9251607Sdim * notice unmodified, this list of conditions, and the following 10251607Sdim * disclaimer. 11251607Sdim * 2. Redistributions in binary form must reproduce the above copyright 12251607Sdim * notice, this list of conditions and the following disclaimer in the 13251607Sdim * documentation and/or other materials provided with the distribution. 14251607Sdim * 15251607Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16251607Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17251607Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18251607Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19251607Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20251607Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21251607Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22251607Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23251607Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24251607Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25251607Sdim */ 26251607Sdim 27251607Sdim#include <sys/cdefs.h> 28251607Sdim__FBSDID("$FreeBSD$"); 29251607Sdim 30251607Sdim#include <sys/param.h> 31251607Sdim#include <sys/bus.h> 32251607Sdim#include <sys/kernel.h> 33251607Sdim#include <sys/libkern.h> 34251607Sdim#include <sys/module.h> 35251607Sdim#include <sys/vmem.h> 36251607Sdim 37251607Sdim#include <dev/ofw/ofw_bus.h> 38251607Sdim#include <dev/ofw/ofw_bus_subr.h> 39251607Sdim#include <dev/ofw/openfirm.h> 40251607Sdim 41251607Sdim#include <machine/bus.h> 42251607Sdim 43251607Sdim#include <powerpc/pseries/phyp-hvcall.h> 44251607Sdim#include <powerpc/pseries/plpar_iommu.h> 45251607Sdim 46251607SdimMALLOC_DEFINE(M_PHYPIOMMU, "iommu", "IOMMU data for PAPR LPARs"); 47251607Sdim 48251607Sdimstruct papr_iommu_map { 49251607Sdim uint32_t iobn; 50251607Sdim vmem_t *vmem; 51251607Sdim struct papr_iommu_map *next; 52251607Sdim}; 53263509Sdim 54263509Sdimstatic SLIST_HEAD(iommu_maps, iommu_map) iommu_map_head = 55251607Sdim SLIST_HEAD_INITIALIZER(iommu_map_head); 56251607Sdimstatic int papr_supports_stuff_tce = -1; 57251607Sdim 58251607Sdimstruct iommu_map { 59251607Sdim uint32_t iobn; 60251607Sdim vmem_t *vmem; 61 62 SLIST_ENTRY(iommu_map) entries; 63}; 64 65struct dma_window { 66 struct iommu_map *map; 67 bus_addr_t start; 68 bus_addr_t end; 69}; 70 71int 72phyp_iommu_set_dma_tag(device_t dev, device_t child, bus_dma_tag_t tag) 73{ 74 device_t p; 75 phandle_t node; 76 cell_t dma_acells, dma_scells, dmawindow[5]; 77 struct iommu_map *i; 78 79 for (p = child; p != NULL; p = device_get_parent(p)) { 80 if (ofw_bus_has_prop(p, "ibm,my-dma-window")) 81 break; 82 if (ofw_bus_has_prop(p, "ibm,dma-window")) 83 break; 84 } 85 86 if (p == NULL) 87 return (ENXIO); 88 89 node = ofw_bus_get_node(p); 90 if (OF_getprop(node, "ibm,#dma-size-cells", &dma_scells, 91 sizeof(cell_t)) <= 0) 92 OF_searchprop(node, "#size-cells", &dma_scells, sizeof(cell_t)); 93 if (OF_getprop(node, "ibm,#dma-address-cells", &dma_acells, 94 sizeof(cell_t)) <= 0) 95 OF_searchprop(node, "#address-cells", &dma_acells, 96 sizeof(cell_t)); 97 98 if (ofw_bus_has_prop(p, "ibm,my-dma-window")) 99 OF_getprop(node, "ibm,my-dma-window", dmawindow, 100 sizeof(cell_t)*(dma_scells + dma_acells + 1)); 101 else 102 OF_getprop(node, "ibm,dma-window", dmawindow, 103 sizeof(cell_t)*(dma_scells + dma_acells + 1)); 104 105 struct dma_window *window = malloc(sizeof(struct dma_window), 106 M_PHYPIOMMU, M_WAITOK); 107 if (dma_acells == 1) 108 window->start = dmawindow[1]; 109 else 110 window->start = ((uint64_t)(dmawindow[1]) << 32) | dmawindow[2]; 111 if (dma_scells == 1) 112 window->end = window->start + dmawindow[dma_acells + 1]; 113 else 114 window->end = window->start + 115 (((uint64_t)(dmawindow[dma_acells + 1]) << 32) | 116 dmawindow[dma_acells + 2]); 117 118 window->map = NULL; 119 SLIST_FOREACH(i, &iommu_map_head, entries) { 120 if (i->iobn == dmawindow[0]) { 121 window->map = i; 122 break; 123 } 124 } 125 126 if (window->map == NULL) { 127 window->map = malloc(sizeof(struct iommu_map), M_PHYPIOMMU, 128 M_WAITOK); 129 window->map->iobn = dmawindow[0]; 130 /* 131 * Allocate IOMMU range beginning at PAGE_SIZE. Some drivers 132 * (em(4), for example) do not like getting mappings at 0. 133 */ 134 window->map->vmem = vmem_create("IOMMU mappings", PAGE_SIZE, 135 trunc_page(VMEM_ADDR_MAX) - PAGE_SIZE, PAGE_SIZE, 0, 136 M_BESTFIT | M_NOWAIT); 137 } 138 139 /* 140 * Check experimentally whether we can use H_STUFF_TCE. It is required 141 * by the spec but some firmware (e.g. QEMU) does not actually support 142 * it 143 */ 144 if (papr_supports_stuff_tce == -1) 145 papr_supports_stuff_tce = !(phyp_hcall(H_STUFF_TCE, 146 window->map->iobn, 0, 0, 0) == H_FUNCTION); 147 148 bus_dma_tag_set_iommu(tag, dev, window); 149 150 return (0); 151} 152 153int 154phyp_iommu_map(device_t dev, bus_dma_segment_t *segs, int *nsegs, 155 bus_addr_t min, bus_addr_t max, bus_size_t alignment, bus_addr_t boundary, 156 void *cookie) 157{ 158 struct dma_window *window = cookie; 159 bus_addr_t minaddr, maxaddr; 160 bus_addr_t alloced; 161 bus_size_t allocsize; 162 int error, i, j; 163 uint64_t tce; 164 minaddr = window->start; 165 maxaddr = window->end; 166 167 /* XXX: handle exclusion range in a more useful way */ 168 if (min < maxaddr) 169 maxaddr = min; 170 171 /* XXX: consolidate segs? */ 172 for (i = 0; i < *nsegs; i++) { 173 allocsize = round_page(segs[i].ds_len + 174 (segs[i].ds_addr & PAGE_MASK)); 175 error = vmem_xalloc(window->map->vmem, allocsize, 176 (alignment < PAGE_SIZE) ? PAGE_SIZE : alignment, 0, 177 boundary, minaddr, maxaddr, M_BESTFIT | M_NOWAIT, &alloced); 178 if (error != 0) { 179 panic("VMEM failure: %d\n", error); 180 return (error); 181 } 182 KASSERT(alloced % PAGE_SIZE == 0, ("Alloc not page aligned")); 183 KASSERT((alloced + (segs[i].ds_addr & PAGE_MASK)) % 184 alignment == 0, 185 ("Allocated segment does not match alignment constraint")); 186 187 tce = trunc_page(segs[i].ds_addr); 188 tce |= 0x3; /* read/write */ 189 if (papr_supports_stuff_tce) { 190 error = phyp_hcall(H_STUFF_TCE, window->map->iobn, 191 alloced, tce, allocsize/PAGE_SIZE); 192 } else { 193 for (j = 0; j < allocsize; j += PAGE_SIZE) 194 error = phyp_hcall(H_PUT_TCE, window->map->iobn, 195 alloced + j, tce + j); 196 } 197 198 segs[i].ds_addr = alloced + (segs[i].ds_addr & PAGE_MASK); 199 KASSERT(segs[i].ds_addr > 0, ("Address needs to be positive")); 200 KASSERT(segs[i].ds_addr + segs[i].ds_len < maxaddr, 201 ("Address not in range")); 202 if (error < 0) { 203 panic("IOMMU mapping error: %d\n", error); 204 return (ENOMEM); 205 } 206 } 207 208 return (0); 209} 210 211int 212phyp_iommu_unmap(device_t dev, bus_dma_segment_t *segs, int nsegs, void *cookie) 213{ 214 struct dma_window *window = cookie; 215 bus_addr_t pageround; 216 bus_size_t roundedsize; 217 int i; 218 bus_addr_t j; 219 220 for (i = 0; i < nsegs; i++) { 221 pageround = trunc_page(segs[i].ds_addr); 222 roundedsize = round_page(segs[i].ds_len + 223 (segs[i].ds_addr & PAGE_MASK)); 224 225 if (papr_supports_stuff_tce) { 226 phyp_hcall(H_STUFF_TCE, window->map->iobn, pageround, 0, 227 roundedsize/PAGE_SIZE); 228 } else { 229 for (j = 0; j < roundedsize; j += PAGE_SIZE) 230 phyp_hcall(H_PUT_TCE, window->map->iobn, 231 pageround + j, 0); 232 } 233 234 vmem_xfree(window->map->vmem, pageround, roundedsize); 235 } 236 237 return (0); 238} 239 240