#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_iommu.c,v 1.1 2021/09/04 12:34:39 jmcneill Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <sys/queue.h>
#include <libfdt.h>
#include <dev/fdt/fdtvar.h>
struct fdtbus_iommu {
device_t iommu_dev;
int iommu_phandle;
const struct fdtbus_iommu_func *iommu_funcs;
u_int iommu_cells;
LIST_ENTRY(fdtbus_iommu) iommu_next;
};
static LIST_HEAD(, fdtbus_iommu) fdtbus_iommus =
LIST_HEAD_INITIALIZER(fdtbus_iommus);
static struct fdtbus_iommu *
fdtbus_get_iommu(int phandle)
{
struct fdtbus_iommu *iommu;
LIST_FOREACH(iommu, &fdtbus_iommus, iommu_next) {
if (iommu->iommu_phandle == phandle) {
return iommu;
}
}
return NULL;
}
int
fdtbus_register_iommu(device_t dev, int phandle,
const struct fdtbus_iommu_func *funcs)
{
struct fdtbus_iommu *iommu;
u_int cells;
if (funcs == NULL || funcs->map == NULL) {
return EINVAL;
}
if (of_getprop_uint32(phandle, "#iommu-cells", &cells) != 0) {
return ENXIO;
}
if (fdtbus_get_iommu(phandle) != NULL) {
return EEXIST;
}
iommu = kmem_alloc(sizeof(*iommu), KM_SLEEP);
iommu->iommu_dev = dev;
iommu->iommu_phandle = phandle;
iommu->iommu_funcs = funcs;
iommu->iommu_cells = cells;
LIST_INSERT_HEAD(&fdtbus_iommus, iommu, iommu_next);
return 0;
}
bus_dma_tag_t
fdtbus_iommu_map(int phandle, u_int index, bus_dma_tag_t dmat)
{
struct fdtbus_iommu *iommu;
const u_int *p;
u_int n, cells;
int len, resid;
p = fdtbus_get_prop(phandle, "iommus", &len);
if (p == NULL) {
return dmat;
}
for (n = 0, resid = len; resid > 0; n++) {
const int iommu_phandle =
fdtbus_get_phandle_from_native(be32toh(p[0]));
if (of_getprop_uint32(iommu_phandle, "#iommu-cells", &cells)) {
break;
}
if (n == index) {
iommu = fdtbus_get_iommu(iommu_phandle);
if (iommu == NULL) {
break;
}
return iommu->iommu_funcs->map(iommu->iommu_dev,
cells > 0 ? &p[1] : NULL, dmat);
}
resid -= (cells + 1) * 4;
p += cells + 1;
}
return dmat;
}
bus_dma_tag_t
fdtbus_iommu_map_pci(int phandle, uint32_t rid, bus_dma_tag_t dmat)
{
struct fdtbus_iommu *iommu;
uint32_t map_mask;
const u_int *p;
u_int cells;
int len;
len = 0;
p = fdtbus_get_prop(phandle, "iommu-map", &len);
KASSERT(p != NULL || len == 0);
if (of_getprop_uint32(phandle, "iommu-map-mask", &map_mask) == 0) {
rid &= map_mask;
}
while (len >= 4) {
const u_int rid_base = be32toh(*p++);
const int iommu_phandle =
fdtbus_get_phandle_from_native(be32toh(*p++));
const u_int iommu_base = be32toh(*p++);
const u_int length = be32toh(*p++);
len -= 4;
if (iommu_phandle <= 0) {
continue;
}
if (of_getprop_uint32(iommu_phandle, "#iommu-cells", &cells)) {
continue;
}
if (cells != 1) {
continue;
}
iommu = fdtbus_get_iommu(iommu_phandle);
if (iommu == NULL) {
continue;
}
if (rid >= rid_base && rid < rid_base + length) {
const uint32_t sid = rid - rid_base + iommu_base;
return iommu->iommu_funcs->map(iommu->iommu_dev,
&sid, dmat);
}
}
return dmat;
}