svn commit: r284144 - head/sys/dev/proto
Marcel Moolenaar
marcel at FreeBSD.org
Mon Jun 8 03:00:38 UTC 2015
Author: marcel
Date: Mon Jun 8 03:00:36 2015
New Revision: 284144
URL: https://svnweb.freebsd.org/changeset/base/284144
Log:
Add DMA memory allocation and freeing.
Slightly rework the tag handling.
Modified:
head/sys/dev/proto/proto_busdma.c
head/sys/dev/proto/proto_busdma.h
head/sys/dev/proto/proto_core.c
head/sys/dev/proto/proto_dev.h
Modified: head/sys/dev/proto/proto_busdma.c
==============================================================================
--- head/sys/dev/proto/proto_busdma.c Mon Jun 8 02:35:43 2015 (r284143)
+++ head/sys/dev/proto/proto_busdma.c Mon Jun 8 03:00:36 2015 (r284144)
@@ -39,6 +39,8 @@ __FBSDID("$FreeBSD$");
#include <sys/queue.h>
#include <sys/rman.h>
#include <sys/sbuf.h>
+#include <vm/vm.h>
+#include <vm/pmap.h>
#include <dev/proto/proto.h>
#include <dev/proto/proto_dev.h>
@@ -47,33 +49,72 @@ __FBSDID("$FreeBSD$");
MALLOC_DEFINE(M_PROTO_BUSDMA, "proto_busdma", "DMA management data");
static int
-proto_busdma_tag_create(struct proto_ioc_busdma *ioc,
- struct proto_tag **tag_io, bus_dma_tag_t *busdma_tag_io)
+proto_busdma_tag_create(struct proto_busdma *busdma, struct proto_tag *parent,
+ struct proto_ioc_busdma *ioc)
{
struct proto_tag *tag;
- int error;
- error = bus_dma_tag_create(*busdma_tag_io, ioc->u.tag.align,
- ioc->u.tag.bndry, ioc->u.tag.maxaddr, BUS_SPACE_MAXADDR,
- NULL, NULL, ioc->u.tag.maxsz, ioc->u.tag.nsegs,
- ioc->u.tag.maxsegsz, ioc->u.tag.flags, NULL, NULL,
- busdma_tag_io);
- if (error)
- return (error);
+ /*
+ * If nsegs is 1, ignore maxsegsz. What this means is that if we have
+ * just 1 segment, then maxsz should be equal to maxsegsz. To keep it
+ * simple for us, limit maxsegsz to maxsz in any case.
+ */
+ if (ioc->u.tag.maxsegsz > ioc->u.tag.maxsz || ioc->u.tag.nsegs == 1)
+ ioc->u.tag.maxsegsz = ioc->u.tag.maxsz;
+
+ /* A bndry of 0 really means ~0, or no boundary. */
+ if (ioc->u.tag.bndry == 0)
+ ioc->u.tag.bndry = ~0U;
tag = malloc(sizeof(*tag), M_PROTO_BUSDMA, M_WAITOK | M_ZERO);
- tag->parent = *tag_io;
- tag->busdma_tag = *busdma_tag_io;
- *tag_io = tag;
+ if (parent != NULL) {
+ tag->parent = parent;
+ LIST_INSERT_HEAD(&parent->children, tag, peers);
+ tag->align = MAX(ioc->u.tag.align, parent->align);
+ tag->bndry = MIN(ioc->u.tag.bndry, parent->bndry);
+ tag->maxaddr = MIN(ioc->u.tag.maxaddr, parent->maxaddr);
+ tag->maxsz = MIN(ioc->u.tag.maxsz, parent->maxsz);
+ tag->maxsegsz = MIN(ioc->u.tag.maxsegsz, parent->maxsegsz);
+ tag->nsegs = MIN(ioc->u.tag.nsegs, parent->nsegs);
+ tag->datarate = MIN(ioc->u.tag.datarate, parent->datarate);
+ /* Write constraints back */
+ ioc->u.tag.align = tag->align;
+ ioc->u.tag.bndry = tag->bndry;
+ ioc->u.tag.maxaddr = tag->maxaddr;
+ ioc->u.tag.maxsz = tag->maxsz;
+ ioc->u.tag.maxsegsz = tag->maxsegsz;
+ ioc->u.tag.nsegs = tag->nsegs;
+ ioc->u.tag.datarate = tag->datarate;
+ } else {
+ tag->align = ioc->u.tag.align;
+ tag->bndry = ioc->u.tag.bndry;
+ tag->maxaddr = ioc->u.tag.maxaddr;
+ tag->maxsz = ioc->u.tag.maxsz;
+ tag->maxsegsz = ioc->u.tag.maxsegsz;
+ tag->nsegs = ioc->u.tag.nsegs;
+ tag->datarate = ioc->u.tag.datarate;
+ }
+ LIST_INSERT_HEAD(&busdma->tags, tag, tags);
+ ioc->result = (uintptr_t)(void *)tag;
return (0);
}
-static void
-proto_busdma_tag_destroy(struct proto_tag *tag)
+static int
+proto_busdma_tag_destroy(struct proto_busdma *busdma, struct proto_tag *tag)
{
- bus_dma_tag_destroy(tag->busdma_tag);
+ if (!LIST_EMPTY(&tag->mds))
+ return (EBUSY);
+ if (!LIST_EMPTY(&tag->children))
+ return (EBUSY);
+
+ if (tag->parent != NULL) {
+ LIST_REMOVE(tag, peers);
+ tag->parent = NULL;
+ }
+ LIST_REMOVE(tag, tags);
free(tag, M_PROTO_BUSDMA);
+ return (0);
}
static struct proto_tag *
@@ -81,11 +122,66 @@ proto_busdma_tag_lookup(struct proto_bus
{
struct proto_tag *tag;
- LIST_FOREACH(tag, &busdma->tags, link) {
+ LIST_FOREACH(tag, &busdma->tags, tags) {
if ((void *)tag == (void *)key)
return (tag);
- }
- return (NULL);
+ }
+ return (NULL);
+}
+
+static int
+proto_busdma_mem_alloc(struct proto_busdma *busdma, struct proto_tag *tag,
+ struct proto_ioc_busdma *ioc)
+{
+ struct proto_md *md;
+ int error;
+
+ md = malloc(sizeof(*md), M_PROTO_BUSDMA, M_WAITOK | M_ZERO);
+ md->tag = tag;
+
+ error = bus_dma_tag_create(busdma->bd_roottag, tag->align, tag->bndry,
+ tag->maxaddr, BUS_SPACE_MAXADDR, NULL, NULL, tag->maxsz,
+ tag->nsegs, tag->maxsegsz, 0, NULL, NULL, &md->bd_tag);
+ if (error) {
+ free(md, M_PROTO_BUSDMA);
+ return (error);
+ }
+ error = bus_dmamem_alloc(md->bd_tag, &md->kva, 0, &md->bd_map);
+ if (error) {
+ bus_dma_tag_destroy(md->bd_tag);
+ free(md, M_PROTO_BUSDMA);
+ return (error);
+ }
+ LIST_INSERT_HEAD(&tag->mds, md, peers);
+ LIST_INSERT_HEAD(&busdma->mds, md, mds);
+ ioc->u.mem.nsegs = 1;
+ ioc->u.mem.physaddr = pmap_kextract((uintptr_t)(md->kva));
+ ioc->result = (uintptr_t)(void *)md;
+ return (0);
+}
+
+static int
+proto_busdma_mem_free(struct proto_busdma *busdma, struct proto_md *md)
+{
+
+ LIST_REMOVE(md, mds);
+ LIST_REMOVE(md, peers);
+ bus_dmamem_free(md->bd_tag, md->kva, md->bd_map);
+ bus_dma_tag_destroy(md->bd_tag);
+ free(md, M_PROTO_BUSDMA);
+ return (0);
+}
+
+static struct proto_md *
+proto_busdma_md_lookup(struct proto_busdma *busdma, u_long key)
+{
+ struct proto_md *md;
+
+ LIST_FOREACH(md, &busdma->mds, mds) {
+ if ((void *)md == (void *)key)
+ return (md);
+ }
+ return (NULL);
}
struct proto_busdma *
@@ -109,12 +205,13 @@ proto_busdma_detach(struct proto_softc *
int
proto_busdma_cleanup(struct proto_softc *sc, struct proto_busdma *busdma)
{
+ struct proto_md *md, *md1;
struct proto_tag *tag, *tag1;
- LIST_FOREACH_SAFE(tag, &busdma->tags, link, tag1) {
- LIST_REMOVE(tag, link);
- proto_busdma_tag_destroy(tag);
- }
+ LIST_FOREACH_SAFE(md, &busdma->mds, mds, md1)
+ proto_busdma_mem_free(busdma, md);
+ LIST_FOREACH_SAFE(tag, &busdma->tags, tags, tag1)
+ proto_busdma_tag_destroy(busdma, tag);
return (0);
}
@@ -123,19 +220,14 @@ proto_busdma_ioctl(struct proto_softc *s
struct proto_ioc_busdma *ioc)
{
struct proto_tag *tag;
- bus_dma_tag_t busdma_tag;
+ struct proto_md *md;
int error;
error = 0;
switch (ioc->request) {
case PROTO_IOC_BUSDMA_TAG_CREATE:
- busdma_tag = bus_get_dma_tag(sc->sc_dev);
- tag = NULL;
- error = proto_busdma_tag_create(ioc, &tag, &busdma_tag);
- if (error)
- break;
- LIST_INSERT_HEAD(&busdma->tags, tag, link);
- ioc->key = (uintptr_t)(void *)tag;
+ busdma->bd_roottag = bus_get_dma_tag(sc->sc_dev);
+ error = proto_busdma_tag_create(busdma, NULL, ioc);
break;
case PROTO_IOC_BUSDMA_TAG_DERIVE:
tag = proto_busdma_tag_lookup(busdma, ioc->key);
@@ -143,12 +235,7 @@ proto_busdma_ioctl(struct proto_softc *s
error = EINVAL;
break;
}
- busdma_tag = tag->busdma_tag;
- error = proto_busdma_tag_create(ioc, &tag, &busdma_tag);
- if (error)
- break;
- LIST_INSERT_HEAD(&busdma->tags, tag, link);
- ioc->key = (uintptr_t)(void *)tag;
+ error = proto_busdma_tag_create(busdma, tag, ioc);
break;
case PROTO_IOC_BUSDMA_TAG_DESTROY:
tag = proto_busdma_tag_lookup(busdma, ioc->key);
@@ -156,8 +243,23 @@ proto_busdma_ioctl(struct proto_softc *s
error = EINVAL;
break;
}
- LIST_REMOVE(tag, link);
- proto_busdma_tag_destroy(tag);
+ error = proto_busdma_tag_destroy(busdma, tag);
+ break;
+ case PROTO_IOC_BUSDMA_MEM_ALLOC:
+ tag = proto_busdma_tag_lookup(busdma, ioc->u.mem.tag);
+ if (tag == NULL) {
+ error = EINVAL;
+ break;
+ }
+ error = proto_busdma_mem_alloc(busdma, tag, ioc);
+ break;
+ case PROTO_IOC_BUSDMA_MEM_FREE:
+ md = proto_busdma_md_lookup(busdma, ioc->key);
+ if (md == NULL) {
+ error = EINVAL;
+ break;
+ }
+ error = proto_busdma_mem_free(busdma, md);
break;
default:
error = EINVAL;
Modified: head/sys/dev/proto/proto_busdma.h
==============================================================================
--- head/sys/dev/proto/proto_busdma.h Mon Jun 8 02:35:43 2015 (r284143)
+++ head/sys/dev/proto/proto_busdma.h Mon Jun 8 03:00:36 2015 (r284144)
@@ -29,14 +29,36 @@
#ifndef _DEV_PROTO_BUSDMA_H_
#define _DEV_PROTO_BUSDMA_H_
+struct proto_md;
+
struct proto_tag {
- LIST_ENTRY(proto_tag) link;
+ LIST_ENTRY(proto_tag) tags;
struct proto_tag *parent;
- bus_dma_tag_t busdma_tag;
+ LIST_ENTRY(proto_tag) peers;
+ LIST_HEAD(,proto_tag) children;
+ LIST_HEAD(,proto_md) mds;
+ bus_addr_t align;
+ bus_addr_t bndry;
+ bus_addr_t maxaddr;
+ bus_size_t maxsz;
+ bus_size_t maxsegsz;
+ u_int nsegs;
+ u_int datarate;
+};
+
+struct proto_md {
+ LIST_ENTRY(proto_md) mds;
+ LIST_ENTRY(proto_md) peers;
+ struct proto_tag *tag;
+ void *kva;
+ bus_dma_tag_t bd_tag;
+ bus_dmamap_t bd_map;
};
struct proto_busdma {
LIST_HEAD(,proto_tag) tags;
+ LIST_HEAD(,proto_md) mds;
+ bus_dma_tag_t bd_roottag;
};
struct proto_busdma *proto_busdma_attach(struct proto_softc *);
Modified: head/sys/dev/proto/proto_core.c
==============================================================================
--- head/sys/dev/proto/proto_core.c Mon Jun 8 02:35:43 2015 (r284143)
+++ head/sys/dev/proto/proto_core.c Mon Jun 8 03:00:36 2015 (r284144)
@@ -370,6 +370,10 @@ proto_ioctl(struct cdev *cdev, u_long cm
error = 0;
switch (cmd) {
case PROTO_IOC_REGION:
+ if (r->r_type == PROTO_RES_BUSDMA) {
+ error = EINVAL;
+ break;
+ }
region = (struct proto_ioc_region *)data;
region->size = r->r_size;
if (r->r_type == PROTO_RES_PCICFG)
@@ -378,6 +382,10 @@ proto_ioctl(struct cdev *cdev, u_long cm
region->address = rman_get_start(r->r_d.res);
break;
case PROTO_IOC_BUSDMA:
+ if (r->r_type != PROTO_RES_BUSDMA) {
+ error = EINVAL;
+ break;
+ }
busdma = (struct proto_ioc_busdma *)data;
error = proto_busdma_ioctl(sc, r->r_d.busdma, busdma);
break;
Modified: head/sys/dev/proto/proto_dev.h
==============================================================================
--- head/sys/dev/proto/proto_dev.h Mon Jun 8 02:35:43 2015 (r284143)
+++ head/sys/dev/proto/proto_dev.h Mon Jun 8 03:00:36 2015 (r284144)
@@ -45,6 +45,8 @@ struct proto_ioc_busdma {
#define PROTO_IOC_BUSDMA_TAG_CREATE 1
#define PROTO_IOC_BUSDMA_TAG_DERIVE 2
#define PROTO_IOC_BUSDMA_TAG_DESTROY 3
+#define PROTO_IOC_BUSDMA_MEM_ALLOC 10
+#define PROTO_IOC_BUSDMA_MEM_FREE 11
unsigned long key;
union {
struct {
@@ -57,6 +59,12 @@ struct proto_ioc_busdma {
unsigned int datarate;
unsigned int flags;
} tag;
+ struct {
+ unsigned long tag;
+ unsigned int flags;
+ unsigned int nsegs;
+ unsigned long physaddr;
+ } mem;
} u;
unsigned long result;
};
More information about the svn-src-all
mailing list