Path: blob/main/sys/powerpc/pseries/plpar_iommu.c
106507 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2013, Nathan Whitehorn <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice unmodified, this list of conditions, and the following11* disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/bus.h>30#include <sys/kernel.h>31#include <sys/libkern.h>32#include <sys/module.h>33#include <sys/vmem.h>3435#include <dev/ofw/ofw_bus.h>36#include <dev/ofw/ofw_bus_subr.h>37#include <dev/ofw/openfirm.h>3839#include <machine/bus.h>4041#include <powerpc/pseries/phyp-hvcall.h>42#include <powerpc/pseries/plpar_iommu.h>4344MALLOC_DEFINE(M_PHYPIOMMU, "iommu", "IOMMU data for PAPR LPARs");4546struct papr_iommu_map {47uint32_t iobn;48vmem_t *vmem;49struct papr_iommu_map *next;50};5152static SLIST_HEAD(iommu_maps, iommu_map) iommu_map_head =53SLIST_HEAD_INITIALIZER(iommu_map_head);54static int papr_supports_stuff_tce = -1;5556struct iommu_map {57uint32_t iobn;58vmem_t *vmem;5960SLIST_ENTRY(iommu_map) entries;61};6263struct dma_window {64struct iommu_map *map;65bus_addr_t start;66bus_addr_t end;67};6869int70phyp_iommu_set_dma_tag(device_t bus, device_t dev, bus_dma_tag_t tag)71{72device_t p;73phandle_t node;74cell_t dma_acells, dma_scells, dmawindow[6];75struct iommu_map *i;76int cell;7778for (p = dev; device_get_parent(p) != NULL; p = device_get_parent(p)) {79if (ofw_bus_has_prop(p, "ibm,my-dma-window"))80break;81if (ofw_bus_has_prop(p, "ibm,dma-window"))82break;83}8485if (p == NULL)86return (ENXIO);8788node = ofw_bus_get_node(p);89if (OF_getencprop(node, "ibm,#dma-size-cells", &dma_scells,90sizeof(cell_t)) <= 0)91OF_searchencprop(node, "#size-cells", &dma_scells,92sizeof(cell_t));93if (OF_getencprop(node, "ibm,#dma-address-cells", &dma_acells,94sizeof(cell_t)) <= 0)95OF_searchencprop(node, "#address-cells", &dma_acells,96sizeof(cell_t));9798if (ofw_bus_has_prop(p, "ibm,my-dma-window"))99OF_getencprop(node, "ibm,my-dma-window", dmawindow,100sizeof(cell_t)*(dma_scells + dma_acells + 1));101else102OF_getencprop(node, "ibm,dma-window", dmawindow,103sizeof(cell_t)*(dma_scells + dma_acells + 1));104105struct dma_window *window = malloc(sizeof(struct dma_window),106M_PHYPIOMMU, M_WAITOK);107window->start = 0;108for (cell = 1; cell < 1 + dma_acells; cell++) {109window->start <<= 32;110window->start |= dmawindow[cell];111}112window->end = 0;113for (; cell < 1 + dma_acells + dma_scells; cell++) {114window->end <<= 32;115window->end |= dmawindow[cell];116}117window->end += window->start;118119if (bootverbose)120device_printf(dev, "Mapping IOMMU domain %#x\n", dmawindow[0]);121window->map = NULL;122SLIST_FOREACH(i, &iommu_map_head, entries) {123if (i->iobn == dmawindow[0]) {124window->map = i;125break;126}127}128129if (window->map == NULL) {130window->map = malloc(sizeof(struct iommu_map), M_PHYPIOMMU,131M_WAITOK);132window->map->iobn = dmawindow[0];133/*134* Allocate IOMMU range beginning at PAGE_SIZE. Some drivers135* (em(4), for example) do not like getting mappings at 0.136*/137window->map->vmem = vmem_create("IOMMU mappings", PAGE_SIZE,138trunc_page(VMEM_ADDR_MAX) - PAGE_SIZE, PAGE_SIZE, 0,139M_BESTFIT | M_NOWAIT);140SLIST_INSERT_HEAD(&iommu_map_head, window->map, entries);141}142143/*144* Check experimentally whether we can use H_STUFF_TCE. It is required145* by the spec but some firmware (e.g. QEMU) does not actually support146* it147*/148if (papr_supports_stuff_tce == -1)149papr_supports_stuff_tce = !(phyp_hcall(H_STUFF_TCE,150window->map->iobn, 0, 0, 0) == H_FUNCTION);151152bus_dma_tag_set_iommu(tag, bus, window);153154return (0);155}156157int158phyp_iommu_map(device_t dev, bus_dma_segment_t *segs, int *nsegs,159bus_addr_t min, bus_addr_t max, bus_size_t alignment, bus_addr_t boundary,160void *cookie)161{162struct dma_window *window = cookie;163bus_addr_t minaddr, maxaddr;164bus_addr_t alloced;165bus_size_t allocsize;166int error, i, j;167uint64_t tce;168minaddr = window->start;169maxaddr = window->end;170171/* XXX: handle exclusion range in a more useful way */172if (min < maxaddr)173maxaddr = min;174175/* XXX: consolidate segs? */176for (i = 0; i < *nsegs; i++) {177allocsize = round_page(segs[i].ds_len +178(segs[i].ds_addr & PAGE_MASK));179error = vmem_xalloc(window->map->vmem, allocsize,180(alignment < PAGE_SIZE) ? PAGE_SIZE : alignment, 0,181boundary, minaddr, maxaddr, M_BESTFIT | M_NOWAIT, &alloced);182if (error != 0) {183panic("VMEM failure: %d\n", error);184return (error);185}186KASSERT(alloced % PAGE_SIZE == 0, ("Alloc not page aligned"));187KASSERT((alloced + (segs[i].ds_addr & PAGE_MASK)) %188alignment == 0,189("Allocated segment does not match alignment constraint"));190191tce = trunc_page(segs[i].ds_addr);192tce |= 0x3; /* read/write */193for (j = 0; j < allocsize; j += PAGE_SIZE) {194error = phyp_hcall(H_PUT_TCE, window->map->iobn,195alloced + j, tce + j);196if (error < 0) {197panic("IOMMU mapping error: %d\n", error);198return (ENOMEM);199}200}201202segs[i].ds_addr = alloced + (segs[i].ds_addr & PAGE_MASK);203KASSERT(segs[i].ds_addr > 0, ("Address needs to be positive"));204KASSERT(segs[i].ds_addr + segs[i].ds_len < maxaddr,205("Address not in range"));206if (error < 0) {207panic("IOMMU mapping error: %d\n", error);208return (ENOMEM);209}210}211212return (0);213}214215int216phyp_iommu_unmap(device_t dev, bus_dma_segment_t *segs, int nsegs, void *cookie)217{218struct dma_window *window = cookie;219bus_addr_t pageround;220bus_size_t roundedsize;221int i;222bus_addr_t j;223224for (i = 0; i < nsegs; i++) {225pageround = trunc_page(segs[i].ds_addr);226roundedsize = round_page(segs[i].ds_len +227(segs[i].ds_addr & PAGE_MASK));228229if (papr_supports_stuff_tce) {230phyp_hcall(H_STUFF_TCE, window->map->iobn, pageround, 0,231roundedsize/PAGE_SIZE);232} else {233for (j = 0; j < roundedsize; j += PAGE_SIZE)234phyp_hcall(H_PUT_TCE, window->map->iobn,235pageround + j, 0);236}237238vmem_xfree(window->map->vmem, pageround, roundedsize);239}240241return (0);242}243244245