drm_dma.c revision d0538f66491267879b7418b21ad78e3dcc2dcc83
9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * drm_dma.c -- DMA IOCTL and function support -*- linux-c -*-
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * All Rights Reserved.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Permission is hereby granted, free of charge, to any person obtaining a
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * copy of this software and associated documentation files (the "Software"),
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * to deal in the Software without restriction, including without limitation
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * and/or sell copies of the Software, and to permit persons to whom the
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Software is furnished to do so, subject to the following conditions:
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * The above copyright notice and this permission notice (including the next
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * paragraph) shall be included in all copies or substantial portions of the
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Software.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * OTHER DEALINGS IN THE SOFTWARE.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Authors:
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Rickard E. (Rik) Faith <faith@valinux.com>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Gareth Hughes <gareth@valinux.com>
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Use is subject to license terms.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#pragma ident "%Z%%M% %I% %E% SMI"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl#include "drmP.h"
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlint
9512fe850e98fdd448c638ca63fdd92a8a510255ahldrm_dma_setup(drm_device_t *dev)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl{
9512fe850e98fdd448c638ca63fdd92a8a510255ahl int i;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl drm_buf_entry_t *pbuf;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl dev->dma = drm_calloc(1, sizeof (*dev->dma), DRM_MEM_DMA);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (dev->dma == NULL)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl return (ENOMEM);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl mutex_init(&dev->dma_lock, NULL, MUTEX_DRIVER, NULL);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl pbuf = &(dev->dma->bufs[0]);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl for (i = 0; i <= DRM_MAX_ORDER; i++, pbuf++)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl bzero(pbuf, sizeof (drm_buf_entry_t));
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl return (0);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl}
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlvoid
9512fe850e98fdd448c638ca63fdd92a8a510255ahldrm_dma_takedown(drm_device_t *dev)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl{
9512fe850e98fdd448c638ca63fdd92a8a510255ahl drm_device_dma_t *dma = dev->dma;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl int i, j;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (dma == NULL)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl return;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl /* Clear dma buffers */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl for (i = 0; i <= DRM_MAX_ORDER; i++) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (dma->bufs[i].seg_count) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl drm_free(dma->bufs[i].seglist,
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee dma->bufs[i].seg_count *
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee sizeof (*dma->bufs[0].seglist), DRM_MEM_SEGS);
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee }
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee for (j = 0; j < dma->bufs[i].buf_count; j++) {
23b5c241225a8ade2b6b9f06ebb891ee459e3b02tomee if (dma->bufs[i].buflist[j].dev_private) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl drm_free(dma->bufs[i].buflist[j].dev_private,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl dma->bufs[i].buflist[j].dev_priv_size,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl DRM_MEM_BUFS);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl if (dma->bufs[i].buf_count)
9512fe850e98fdd448c638ca63fdd92a8a510255ahl drm_free(dma->bufs[i].buflist,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl dma->bufs[i].buf_count *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl sizeof (*dma->bufs[0].buflist), DRM_MEM_BUFS);
}
if (dma->buflist) {
drm_free(dma->buflist,
dma->buf_count *sizeof (*dma->buflist),
DRM_MEM_BUFS);
}
if (dma->pagelist) {
drm_free(dma->pagelist,
dma->page_count *sizeof (*dma->pagelist),
DRM_MEM_PAGES);
}
drm_free(dev->dma, sizeof (*dev->dma), DRM_MEM_DRIVER);
dev->dma = NULL;
mutex_destroy(&dev->dma_lock);
}
/*ARGSUSED*/
void
drm_free_buffer(drm_device_t *dev, drm_buf_t *buf)
{
if (!buf)
return;
buf->pending = 0;
buf->filp = NULL;
buf->used = 0;
}
void
drm_reclaim_buffers(drm_device_t *dev, drm_file_t *fpriv)
{
drm_device_dma_t *dma = dev->dma;
int i;
if (!dma)
return;
for (i = 0; i < dma->buf_count; i++) {
if (dma->buflist[i]->filp == fpriv) {
switch (dma->buflist[i]->list) {
case DRM_LIST_NONE:
drm_free_buffer(dev, dma->buflist[i]);
break;
case DRM_LIST_WAIT:
dma->buflist[i]->list = DRM_LIST_RECLAIM;
break;
default:
/* Buffer already on hardware. */
break;
}
}
}
}
/* Call into the driver-specific DMA handler */
int
drm_dma(DRM_IOCTL_ARGS)
{
DRM_DEVICE;
if (dev->driver->dma_ioctl) {
return (dev->driver->dma_ioctl(dev, data, fpriv, mode));
} else {
return (EINVAL);
}
}