/**1* \file drm_dma.c2* DMA IOCTL and function support3*4* \author Rickard E. (Rik) Faith <[email protected]>5* \author Gareth Hughes <[email protected]>6*/78/*9* Created: Fri Mar 19 14:30:16 1999 by [email protected]10*11* Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.12* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.13* All Rights Reserved.14*15* Permission is hereby granted, free of charge, to any person obtaining a16* copy of this software and associated documentation files (the "Software"),17* to deal in the Software without restriction, including without limitation18* the rights to use, copy, modify, merge, publish, distribute, sublicense,19* and/or sell copies of the Software, and to permit persons to whom the20* Software is furnished to do so, subject to the following conditions:21*22* The above copyright notice and this permission notice (including the next23* paragraph) shall be included in all copies or substantial portions of the24* Software.25*26* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR27* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,28* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL29* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR30* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,31* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR32* OTHER DEALINGS IN THE SOFTWARE.33*/3435#include "drmP.h"3637/**38* Initialize the DMA data.39*40* \param dev DRM device.41* \return zero on success or a negative value on failure.42*43* Allocate and initialize a drm_device_dma structure.44*/45int drm_dma_setup(struct drm_device *dev)46{47int i;4849dev->dma = kzalloc(sizeof(*dev->dma), GFP_KERNEL);50if (!dev->dma)51return -ENOMEM;5253for (i = 0; i <= DRM_MAX_ORDER; i++)54memset(&dev->dma->bufs[i], 0, sizeof(dev->dma->bufs[0]));5556return 0;57}5859/**60* Cleanup the DMA resources.61*62* \param dev DRM device.63*64* Free all pages associated with DMA buffers, the buffers and pages lists, and65* finally the drm_device::dma structure itself.66*/67void drm_dma_takedown(struct drm_device *dev)68{69struct drm_device_dma *dma = dev->dma;70int i, j;7172if (!dma)73return;7475/* Clear dma buffers */76for (i = 0; i <= DRM_MAX_ORDER; i++) {77if (dma->bufs[i].seg_count) {78DRM_DEBUG("order %d: buf_count = %d,"79" seg_count = %d\n",80i,81dma->bufs[i].buf_count,82dma->bufs[i].seg_count);83for (j = 0; j < dma->bufs[i].seg_count; j++) {84if (dma->bufs[i].seglist[j]) {85drm_pci_free(dev, dma->bufs[i].seglist[j]);86}87}88kfree(dma->bufs[i].seglist);89}90if (dma->bufs[i].buf_count) {91for (j = 0; j < dma->bufs[i].buf_count; j++) {92kfree(dma->bufs[i].buflist[j].dev_private);93}94kfree(dma->bufs[i].buflist);95}96}9798kfree(dma->buflist);99kfree(dma->pagelist);100kfree(dev->dma);101dev->dma = NULL;102}103104/**105* Free a buffer.106*107* \param dev DRM device.108* \param buf buffer to free.109*110* Resets the fields of \p buf.111*/112void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf)113{114if (!buf)115return;116117buf->waiting = 0;118buf->pending = 0;119buf->file_priv = NULL;120buf->used = 0;121122if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE)123&& waitqueue_active(&buf->dma_wait)) {124wake_up_interruptible(&buf->dma_wait);125}126}127128/**129* Reclaim the buffers.130*131* \param file_priv DRM file private.132*133* Frees each buffer associated with \p file_priv not already on the hardware.134*/135void drm_core_reclaim_buffers(struct drm_device *dev,136struct drm_file *file_priv)137{138struct drm_device_dma *dma = dev->dma;139int i;140141if (!dma)142return;143for (i = 0; i < dma->buf_count; i++) {144if (dma->buflist[i]->file_priv == file_priv) {145switch (dma->buflist[i]->list) {146case DRM_LIST_NONE:147drm_free_buffer(dev, dma->buflist[i]);148break;149case DRM_LIST_WAIT:150dma->buflist[i]->list = DRM_LIST_RECLAIM;151break;152default:153/* Buffer already on hardware. */154break;155}156}157}158}159160EXPORT_SYMBOL(drm_core_reclaim_buffers);161162163