Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * \file drm_scatter.c
0003  * IOCTLs to manage scatter/gather memory
0004  *
0005  * \author Gareth Hughes <gareth@valinux.com>
0006  */
0007 
0008 /*
0009  * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com
0010  *
0011  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
0012  * All Rights Reserved.
0013  *
0014  * Permission is hereby granted, free of charge, to any person obtaining a
0015  * copy of this software and associated documentation files (the "Software"),
0016  * to deal in the Software without restriction, including without limitation
0017  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0018  * and/or sell copies of the Software, and to permit persons to whom the
0019  * Software is furnished to do so, subject to the following conditions:
0020  *
0021  * The above copyright notice and this permission notice (including the next
0022  * paragraph) shall be included in all copies or substantial portions of the
0023  * Software.
0024  *
0025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0026  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0027  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0028  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
0029  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0030  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0031  * DEALINGS IN THE SOFTWARE.
0032  */
0033 
0034 #include <linux/mm.h>
0035 #include <linux/slab.h>
0036 #include <linux/vmalloc.h>
0037 
0038 #include <drm/drm.h>
0039 #include <drm/drm_drv.h>
0040 #include <drm/drm_print.h>
0041 
0042 #include "drm_legacy.h"
0043 
0044 #define DEBUG_SCATTER 0
0045 
0046 static void drm_sg_cleanup(struct drm_sg_mem * entry)
0047 {
0048     struct page *page;
0049     int i;
0050 
0051     for (i = 0; i < entry->pages; i++) {
0052         page = entry->pagelist[i];
0053         if (page)
0054             ClearPageReserved(page);
0055     }
0056 
0057     vfree(entry->virtual);
0058 
0059     kfree(entry->busaddr);
0060     kfree(entry->pagelist);
0061     kfree(entry);
0062 }
0063 
0064 void drm_legacy_sg_cleanup(struct drm_device *dev)
0065 {
0066     if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
0067         drm_core_check_feature(dev, DRIVER_LEGACY)) {
0068         drm_sg_cleanup(dev->sg);
0069         dev->sg = NULL;
0070     }
0071 }
0072 #ifdef _LP64
0073 # define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
0074 #else
0075 # define ScatterHandle(x) (unsigned int)(x)
0076 #endif
0077 
0078 int drm_legacy_sg_alloc(struct drm_device *dev, void *data,
0079             struct drm_file *file_priv)
0080 {
0081     struct drm_scatter_gather *request = data;
0082     struct drm_sg_mem *entry;
0083     unsigned long pages, i, j;
0084 
0085     DRM_DEBUG("\n");
0086 
0087     if (!drm_core_check_feature(dev, DRIVER_LEGACY))
0088         return -EOPNOTSUPP;
0089 
0090     if (!drm_core_check_feature(dev, DRIVER_SG))
0091         return -EOPNOTSUPP;
0092 
0093     if (request->size > SIZE_MAX - PAGE_SIZE)
0094         return -EINVAL;
0095 
0096     if (dev->sg)
0097         return -EINVAL;
0098 
0099     entry = kzalloc(sizeof(*entry), GFP_KERNEL);
0100     if (!entry)
0101         return -ENOMEM;
0102 
0103     pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
0104     DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
0105 
0106     entry->pages = pages;
0107     entry->pagelist = kcalloc(pages, sizeof(*entry->pagelist), GFP_KERNEL);
0108     if (!entry->pagelist) {
0109         kfree(entry);
0110         return -ENOMEM;
0111     }
0112 
0113     entry->busaddr = kcalloc(pages, sizeof(*entry->busaddr), GFP_KERNEL);
0114     if (!entry->busaddr) {
0115         kfree(entry->pagelist);
0116         kfree(entry);
0117         return -ENOMEM;
0118     }
0119 
0120     entry->virtual = vmalloc_32(pages << PAGE_SHIFT);
0121     if (!entry->virtual) {
0122         kfree(entry->busaddr);
0123         kfree(entry->pagelist);
0124         kfree(entry);
0125         return -ENOMEM;
0126     }
0127 
0128     /* This also forces the mapping of COW pages, so our page list
0129      * will be valid.  Please don't remove it...
0130      */
0131     memset(entry->virtual, 0, pages << PAGE_SHIFT);
0132 
0133     entry->handle = ScatterHandle((unsigned long)entry->virtual);
0134 
0135     DRM_DEBUG("handle  = %08lx\n", entry->handle);
0136     DRM_DEBUG("virtual = %p\n", entry->virtual);
0137 
0138     for (i = (unsigned long)entry->virtual, j = 0; j < pages;
0139          i += PAGE_SIZE, j++) {
0140         entry->pagelist[j] = vmalloc_to_page((void *)i);
0141         if (!entry->pagelist[j])
0142             goto failed;
0143         SetPageReserved(entry->pagelist[j]);
0144     }
0145 
0146     request->handle = entry->handle;
0147 
0148     dev->sg = entry;
0149 
0150 #if DEBUG_SCATTER
0151     /* Verify that each page points to its virtual address, and vice
0152      * versa.
0153      */
0154     {
0155         int error = 0;
0156 
0157         for (i = 0; i < pages; i++) {
0158             unsigned long *tmp;
0159 
0160             tmp = page_address(entry->pagelist[i]);
0161             for (j = 0;
0162                  j < PAGE_SIZE / sizeof(unsigned long);
0163                  j++, tmp++) {
0164                 *tmp = 0xcafebabe;
0165             }
0166             tmp = (unsigned long *)((u8 *) entry->virtual +
0167                         (PAGE_SIZE * i));
0168             for (j = 0;
0169                  j < PAGE_SIZE / sizeof(unsigned long);
0170                  j++, tmp++) {
0171                 if (*tmp != 0xcafebabe && error == 0) {
0172                     error = 1;
0173                     DRM_ERROR("Scatter allocation error, "
0174                           "pagelist does not match "
0175                           "virtual mapping\n");
0176                 }
0177             }
0178             tmp = page_address(entry->pagelist[i]);
0179             for (j = 0;
0180                  j < PAGE_SIZE / sizeof(unsigned long);
0181                  j++, tmp++) {
0182                 *tmp = 0;
0183             }
0184         }
0185         if (error == 0)
0186             DRM_ERROR("Scatter allocation matches pagelist\n");
0187     }
0188 #endif
0189 
0190     return 0;
0191 
0192       failed:
0193     drm_sg_cleanup(entry);
0194     return -ENOMEM;
0195 }
0196 
0197 int drm_legacy_sg_free(struct drm_device *dev, void *data,
0198                struct drm_file *file_priv)
0199 {
0200     struct drm_scatter_gather *request = data;
0201     struct drm_sg_mem *entry;
0202 
0203     if (!drm_core_check_feature(dev, DRIVER_LEGACY))
0204         return -EOPNOTSUPP;
0205 
0206     if (!drm_core_check_feature(dev, DRIVER_SG))
0207         return -EOPNOTSUPP;
0208 
0209     entry = dev->sg;
0210     dev->sg = NULL;
0211 
0212     if (!entry || entry->handle != request->handle)
0213         return -EINVAL;
0214 
0215     DRM_DEBUG("virtual  = %p\n", entry->virtual);
0216 
0217     drm_sg_cleanup(entry);
0218 
0219     return 0;
0220 }