1494N/A/*
1494N/A * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
1494N/A */
1494N/A
1494N/A/*
1494N/A * Copyright (c) 2012 Intel Corporation. All rights reserved.
1494N/A */
1494N/A
1494N/A/**
1494N/A * \file drm_scatter.c
1494N/A * IOCTLs to manage scatter/gather memory
1494N/A *
1494N/A * \author Gareth Hughes <gareth@valinux.com>
1494N/A */
1494N/A
1494N/A/*
1494N/A * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com
1494N/A *
1494N/A * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1494N/A * All Rights Reserved.
1494N/A *
1494N/A * Permission is hereby granted, free of charge, to any person obtaining a
1494N/A * copy of this software and associated documentation files (the "Software"),
1494N/A * to deal in the Software without restriction, including without limitation
1494N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1494N/A * and/or sell copies of the Software, and to permit persons to whom the
1494N/A * Software is furnished to do so, subject to the following conditions:
1494N/A *
1494N/A * The above copyright notice and this permission notice (including the next
1494N/A * paragraph) shall be included in all copies or substantial portions of the
1494N/A * Software.
1494N/A *
1494N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1494N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1494N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1494N/A * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1494N/A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1494N/A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1494N/A * DEALINGS IN THE SOFTWARE.
1494N/A */
1494N/A
1494N/A#include "drmP.h"
1494N/A#include "drm_io32.h"
1494N/A
1494N/A#define DEBUG_SCATTER 0
1494N/A
1494N/Avoid drm_sg_cleanup(struct drm_sg_mem *entry)
1494N/A{
1494N/A int pages = entry->pages;
1494N/A
1494N/A if (entry->busaddr) {
1494N/A kmem_free(entry->busaddr, sizeof (*entry->busaddr) * pages);
1494N/A entry->busaddr = NULL;
1494N/A }
1494N/A
1494N/A ASSERT(entry->umem_cookie == NULL);
1494N/A
1494N/A if (entry->dmah_sg) {
1494N/A drm_pci_free(entry->dmah_sg);
1494N/A entry->dmah_sg = NULL;
1494N/A }
1494N/A
1494N/A if (entry->dmah_gart) {
1494N/A drm_pci_free(entry->dmah_gart);
1494N/A entry->dmah_gart = NULL;
1494N/A }
1494N/A
1494N/A kfree(entry, sizeof (struct drm_sg_mem));
1494N/A}
1494N/A
1494N/A#ifdef _LP64
1494N/A#define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
1494N/A#else
1494N/A#define ScatterHandle(x) (unsigned int)(x)
1494N/A#endif
1494N/A
1494N/Aint drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request)
1494N/A{
1494N/A struct drm_sg_mem *entry;
1494N/A unsigned long pages;
1494N/A drm_dma_handle_t *dmah;
1494N/A
1494N/A DRM_DEBUG("\n");
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_SG))
1494N/A return -EINVAL;
1494N/A
1494N/A if (dev->sg)
1494N/A return -EINVAL;
1494N/A
1494N/A entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1494N/A if (!entry)
1494N/A return -ENOMEM;
1494N/A
1494N/A (void) memset(entry, 0, sizeof(*entry));
1494N/A pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
1494N/A DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
1494N/A
1494N/A entry->pages = (int)pages;
1494N/A dmah = drm_pci_alloc(dev, ptob(pages), 4096, 0xfffffffful, pages);
1494N/A if (dmah == NULL)
1494N/A goto err_exit;
1494N/A entry->busaddr = (void *)kmem_zalloc(sizeof (*entry->busaddr) *
1494N/A pages, KM_SLEEP);
1494N/A
1494N/A entry->handle = ScatterHandle((unsigned long)dmah->vaddr);
1494N/A entry->virtual = (void *)dmah->vaddr;
1494N/A request->handle = entry->handle;
1494N/A entry->dmah_sg = dmah;
1494N/A
1494N/A dev->sg = entry;
1494N/A
1494N/A return 0;
1494N/A
1494N/Aerr_exit:
1494N/A drm_sg_cleanup(entry);
1494N/A return -ENOMEM;
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_sg_alloc_ioctl(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_scatter_gather *request = data;
1494N/A
1494N/A return drm_sg_alloc(dev, request);
1494N/A
1494N/A}
1494N/A
1494N/A/* LINTED */
1494N/Aint drm_sg_free(DRM_IOCTL_ARGS)
1494N/A{
1494N/A struct drm_scatter_gather *request = data;
1494N/A struct drm_sg_mem *entry;
1494N/A
1494N/A if (!drm_core_check_feature(dev, DRIVER_SG))
1494N/A return -EINVAL;
1494N/A
1494N/A entry = dev->sg;
1494N/A dev->sg = NULL;
1494N/A
1494N/A if (!entry || entry->handle != request->handle)
1494N/A return -EINVAL;
1494N/A
1494N/A DRM_DEBUG("virtual = %p\n", entry->virtual);
1494N/A
1494N/A drm_sg_cleanup(entry);
1494N/A
1494N/A return 0;
1494N/A}