Home | History | Annotate | Line # | Download | only in drm
drm_scatter.c revision 1.2
      1  1.2  riastrad /*	$NetBSD: drm_scatter.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /**
      4  1.1  riastrad  * \file drm_scatter.c
      5  1.1  riastrad  * IOCTLs to manage scatter/gather memory
      6  1.1  riastrad  *
      7  1.1  riastrad  * \author Gareth Hughes <gareth (at) valinux.com>
      8  1.1  riastrad  */
      9  1.1  riastrad 
     10  1.1  riastrad /*
     11  1.1  riastrad  * Created: Mon Dec 18 23:20:54 2000 by gareth (at) valinux.com
     12  1.1  riastrad  *
     13  1.1  riastrad  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
     14  1.1  riastrad  * All Rights Reserved.
     15  1.1  riastrad  *
     16  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     17  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     18  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     19  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     20  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     21  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     22  1.1  riastrad  *
     23  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     24  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     25  1.1  riastrad  * Software.
     26  1.1  riastrad  *
     27  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     28  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     29  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     30  1.1  riastrad  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     31  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     32  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     33  1.1  riastrad  * DEALINGS IN THE SOFTWARE.
     34  1.1  riastrad  */
     35  1.1  riastrad 
     36  1.2  riastrad #include <sys/cdefs.h>
     37  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_scatter.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
     38  1.2  riastrad 
     39  1.1  riastrad #include <linux/vmalloc.h>
     40  1.1  riastrad #include <linux/slab.h>
     41  1.1  riastrad #include <drm/drmP.h>
     42  1.2  riastrad #include "drm_legacy.h"
     43  1.1  riastrad 
     44  1.1  riastrad #define DEBUG_SCATTER 0
     45  1.1  riastrad 
     46  1.1  riastrad static inline void *drm_vmalloc_dma(unsigned long size)
     47  1.1  riastrad {
     48  1.1  riastrad #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
     49  1.1  riastrad 	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL | _PAGE_NO_CACHE);
     50  1.1  riastrad #else
     51  1.1  riastrad 	return vmalloc_32(size);
     52  1.1  riastrad #endif
     53  1.1  riastrad }
     54  1.1  riastrad 
     55  1.2  riastrad static void drm_sg_cleanup(struct drm_sg_mem * entry)
     56  1.1  riastrad {
     57  1.1  riastrad 	struct page *page;
     58  1.1  riastrad 	int i;
     59  1.1  riastrad 
     60  1.1  riastrad 	for (i = 0; i < entry->pages; i++) {
     61  1.1  riastrad 		page = entry->pagelist[i];
     62  1.1  riastrad 		if (page)
     63  1.1  riastrad 			ClearPageReserved(page);
     64  1.1  riastrad 	}
     65  1.1  riastrad 
     66  1.1  riastrad 	vfree(entry->virtual);
     67  1.1  riastrad 
     68  1.1  riastrad 	kfree(entry->busaddr);
     69  1.1  riastrad 	kfree(entry->pagelist);
     70  1.1  riastrad 	kfree(entry);
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.2  riastrad void drm_legacy_sg_cleanup(struct drm_device *dev)
     74  1.2  riastrad {
     75  1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
     76  1.2  riastrad 	    !drm_core_check_feature(dev, DRIVER_MODESET)) {
     77  1.2  riastrad 		drm_sg_cleanup(dev->sg);
     78  1.2  riastrad 		dev->sg = NULL;
     79  1.2  riastrad 	}
     80  1.2  riastrad }
     81  1.1  riastrad #ifdef _LP64
     82  1.1  riastrad # define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
     83  1.1  riastrad #else
     84  1.1  riastrad # define ScatterHandle(x) (unsigned int)(x)
     85  1.1  riastrad #endif
     86  1.1  riastrad 
     87  1.2  riastrad int drm_legacy_sg_alloc(struct drm_device *dev, void *data,
     88  1.2  riastrad 			struct drm_file *file_priv)
     89  1.1  riastrad {
     90  1.2  riastrad 	struct drm_scatter_gather *request = data;
     91  1.1  riastrad 	struct drm_sg_mem *entry;
     92  1.1  riastrad 	unsigned long pages, i, j;
     93  1.1  riastrad 
     94  1.1  riastrad 	DRM_DEBUG("\n");
     95  1.1  riastrad 
     96  1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET))
     97  1.2  riastrad 		return -EINVAL;
     98  1.2  riastrad 
     99  1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_SG))
    100  1.1  riastrad 		return -EINVAL;
    101  1.1  riastrad 
    102  1.1  riastrad 	if (dev->sg)
    103  1.1  riastrad 		return -EINVAL;
    104  1.1  riastrad 
    105  1.1  riastrad 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
    106  1.1  riastrad 	if (!entry)
    107  1.1  riastrad 		return -ENOMEM;
    108  1.1  riastrad 
    109  1.1  riastrad 	pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
    110  1.1  riastrad 	DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
    111  1.1  riastrad 
    112  1.1  riastrad 	entry->pages = pages;
    113  1.1  riastrad 	entry->pagelist = kcalloc(pages, sizeof(*entry->pagelist), GFP_KERNEL);
    114  1.1  riastrad 	if (!entry->pagelist) {
    115  1.1  riastrad 		kfree(entry);
    116  1.1  riastrad 		return -ENOMEM;
    117  1.1  riastrad 	}
    118  1.1  riastrad 
    119  1.1  riastrad 	entry->busaddr = kcalloc(pages, sizeof(*entry->busaddr), GFP_KERNEL);
    120  1.1  riastrad 	if (!entry->busaddr) {
    121  1.1  riastrad 		kfree(entry->pagelist);
    122  1.1  riastrad 		kfree(entry);
    123  1.1  riastrad 		return -ENOMEM;
    124  1.1  riastrad 	}
    125  1.1  riastrad 
    126  1.1  riastrad 	entry->virtual = drm_vmalloc_dma(pages << PAGE_SHIFT);
    127  1.1  riastrad 	if (!entry->virtual) {
    128  1.1  riastrad 		kfree(entry->busaddr);
    129  1.1  riastrad 		kfree(entry->pagelist);
    130  1.1  riastrad 		kfree(entry);
    131  1.1  riastrad 		return -ENOMEM;
    132  1.1  riastrad 	}
    133  1.1  riastrad 
    134  1.1  riastrad 	/* This also forces the mapping of COW pages, so our page list
    135  1.1  riastrad 	 * will be valid.  Please don't remove it...
    136  1.1  riastrad 	 */
    137  1.1  riastrad 	memset(entry->virtual, 0, pages << PAGE_SHIFT);
    138  1.1  riastrad 
    139  1.1  riastrad 	entry->handle = ScatterHandle((unsigned long)entry->virtual);
    140  1.1  riastrad 
    141  1.1  riastrad 	DRM_DEBUG("handle  = %08lx\n", entry->handle);
    142  1.1  riastrad 	DRM_DEBUG("virtual = %p\n", entry->virtual);
    143  1.1  riastrad 
    144  1.1  riastrad 	for (i = (unsigned long)entry->virtual, j = 0; j < pages;
    145  1.1  riastrad 	     i += PAGE_SIZE, j++) {
    146  1.1  riastrad 		entry->pagelist[j] = vmalloc_to_page((void *)i);
    147  1.1  riastrad 		if (!entry->pagelist[j])
    148  1.1  riastrad 			goto failed;
    149  1.1  riastrad 		SetPageReserved(entry->pagelist[j]);
    150  1.1  riastrad 	}
    151  1.1  riastrad 
    152  1.1  riastrad 	request->handle = entry->handle;
    153  1.1  riastrad 
    154  1.1  riastrad 	dev->sg = entry;
    155  1.1  riastrad 
    156  1.1  riastrad #if DEBUG_SCATTER
    157  1.1  riastrad 	/* Verify that each page points to its virtual address, and vice
    158  1.1  riastrad 	 * versa.
    159  1.1  riastrad 	 */
    160  1.1  riastrad 	{
    161  1.1  riastrad 		int error = 0;
    162  1.1  riastrad 
    163  1.1  riastrad 		for (i = 0; i < pages; i++) {
    164  1.1  riastrad 			unsigned long *tmp;
    165  1.1  riastrad 
    166  1.1  riastrad 			tmp = page_address(entry->pagelist[i]);
    167  1.1  riastrad 			for (j = 0;
    168  1.1  riastrad 			     j < PAGE_SIZE / sizeof(unsigned long);
    169  1.1  riastrad 			     j++, tmp++) {
    170  1.1  riastrad 				*tmp = 0xcafebabe;
    171  1.1  riastrad 			}
    172  1.1  riastrad 			tmp = (unsigned long *)((u8 *) entry->virtual +
    173  1.1  riastrad 						(PAGE_SIZE * i));
    174  1.1  riastrad 			for (j = 0;
    175  1.1  riastrad 			     j < PAGE_SIZE / sizeof(unsigned long);
    176  1.1  riastrad 			     j++, tmp++) {
    177  1.1  riastrad 				if (*tmp != 0xcafebabe && error == 0) {
    178  1.1  riastrad 					error = 1;
    179  1.1  riastrad 					DRM_ERROR("Scatter allocation error, "
    180  1.1  riastrad 						  "pagelist does not match "
    181  1.1  riastrad 						  "virtual mapping\n");
    182  1.1  riastrad 				}
    183  1.1  riastrad 			}
    184  1.1  riastrad 			tmp = page_address(entry->pagelist[i]);
    185  1.1  riastrad 			for (j = 0;
    186  1.1  riastrad 			     j < PAGE_SIZE / sizeof(unsigned long);
    187  1.1  riastrad 			     j++, tmp++) {
    188  1.1  riastrad 				*tmp = 0;
    189  1.1  riastrad 			}
    190  1.1  riastrad 		}
    191  1.1  riastrad 		if (error == 0)
    192  1.1  riastrad 			DRM_ERROR("Scatter allocation matches pagelist\n");
    193  1.1  riastrad 	}
    194  1.1  riastrad #endif
    195  1.1  riastrad 
    196  1.1  riastrad 	return 0;
    197  1.1  riastrad 
    198  1.1  riastrad       failed:
    199  1.1  riastrad 	drm_sg_cleanup(entry);
    200  1.1  riastrad 	return -ENOMEM;
    201  1.1  riastrad }
    202  1.1  riastrad 
    203  1.2  riastrad int drm_legacy_sg_free(struct drm_device *dev, void *data,
    204  1.1  riastrad 		       struct drm_file *file_priv)
    205  1.1  riastrad {
    206  1.1  riastrad 	struct drm_scatter_gather *request = data;
    207  1.2  riastrad 	struct drm_sg_mem *entry;
    208  1.1  riastrad 
    209  1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET))
    210  1.2  riastrad 		return -EINVAL;
    211  1.1  riastrad 
    212  1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_SG))
    213  1.1  riastrad 		return -EINVAL;
    214  1.1  riastrad 
    215  1.1  riastrad 	entry = dev->sg;
    216  1.1  riastrad 	dev->sg = NULL;
    217  1.1  riastrad 
    218  1.1  riastrad 	if (!entry || entry->handle != request->handle)
    219  1.1  riastrad 		return -EINVAL;
    220  1.1  riastrad 
    221  1.1  riastrad 	DRM_DEBUG("virtual  = %p\n", entry->virtual);
    222  1.1  riastrad 
    223  1.1  riastrad 	drm_sg_cleanup(entry);
    224  1.1  riastrad 
    225  1.1  riastrad 	return 0;
    226  1.1  riastrad }
    227