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