Home | History | Annotate | Line # | Download | only in drm
drm_memory.c revision 1.2
      1 /*	$NetBSD: drm_memory.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $	*/
      2 
      3 /**
      4  * \file drm_memory.c
      5  * Memory management wrappers for DRM
      6  *
      7  * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
      8  * \author Gareth Hughes <gareth (at) valinux.com>
      9  */
     10 
     11 /*
     12  * Created: Thu Feb  4 14:00:34 1999 by faith (at) valinux.com
     13  *
     14  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
     15  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
     16  * All Rights Reserved.
     17  *
     18  * Permission is hereby granted, free of charge, to any person obtaining a
     19  * copy of this software and associated documentation files (the "Software"),
     20  * to deal in the Software without restriction, including without limitation
     21  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     22  * and/or sell copies of the Software, and to permit persons to whom the
     23  * Software is furnished to do so, subject to the following conditions:
     24  *
     25  * The above copyright notice and this permission notice (including the next
     26  * paragraph) shall be included in all copies or substantial portions of the
     27  * Software.
     28  *
     29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     30  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     31  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     32  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     33  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     34  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     35  * OTHER DEALINGS IN THE SOFTWARE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: drm_memory.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
     40 
     41 #include <linux/highmem.h>
     42 #include <linux/export.h>
     43 #include <drm/drmP.h>
     44 #include "drm_legacy.h"
     45 
     46 #if IS_ENABLED(CONFIG_AGP)
     47 
     48 #ifdef HAVE_PAGE_AGP
     49 # include <asm/agp.h>
     50 #else
     51 # ifdef __powerpc__
     52 #  define PAGE_AGP	__pgprot(_PAGE_KERNEL | _PAGE_NO_CACHE)
     53 # else
     54 #  define PAGE_AGP	PAGE_KERNEL
     55 # endif
     56 #endif
     57 
     58 static void *agp_remap(unsigned long offset, unsigned long size,
     59 		       struct drm_device * dev)
     60 {
     61 	unsigned long i, num_pages =
     62 	    PAGE_ALIGN(size) / PAGE_SIZE;
     63 	struct drm_agp_mem *agpmem;
     64 	struct page **page_map;
     65 	struct page **phys_page_map;
     66 	void *addr;
     67 
     68 	size = PAGE_ALIGN(size);
     69 
     70 #ifdef __alpha__
     71 	offset -= dev->hose->mem_space->start;
     72 #endif
     73 
     74 	list_for_each_entry(agpmem, &dev->agp->memory, head)
     75 		if (agpmem->bound <= offset
     76 		    && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
     77 		    (offset + size))
     78 			break;
     79 	if (&agpmem->head == &dev->agp->memory)
     80 		return NULL;
     81 
     82 	/*
     83 	 * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
     84 	 * the CPU do not get remapped by the GART.  We fix this by using the kernel's
     85 	 * page-table instead (that's probably faster anyhow...).
     86 	 */
     87 	/* note: use vmalloc() because num_pages could be large... */
     88 	page_map = vmalloc(num_pages * sizeof(struct page *));
     89 	if (!page_map)
     90 		return NULL;
     91 
     92 	phys_page_map = (agpmem->memory->pages + (offset - agpmem->bound) / PAGE_SIZE);
     93 	for (i = 0; i < num_pages; ++i)
     94 		page_map[i] = phys_page_map[i];
     95 	addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
     96 	vfree(page_map);
     97 
     98 	return addr;
     99 }
    100 
    101 /** Wrapper around agp_free_memory() */
    102 void drm_free_agp(struct agp_memory * handle, int pages)
    103 {
    104 	agp_free_memory(handle);
    105 }
    106 
    107 /** Wrapper around agp_bind_memory() */
    108 int drm_bind_agp(struct agp_memory * handle, unsigned int start)
    109 {
    110 	return agp_bind_memory(handle, start);
    111 }
    112 
    113 /** Wrapper around agp_unbind_memory() */
    114 int drm_unbind_agp(struct agp_memory * handle)
    115 {
    116 	return agp_unbind_memory(handle);
    117 }
    118 
    119 #else /*  CONFIG_AGP  */
    120 static inline void *agp_remap(unsigned long offset, unsigned long size,
    121 			      struct drm_device * dev)
    122 {
    123 	return NULL;
    124 }
    125 
    126 #endif /* CONFIG_AGP */
    127 
    128 void drm_legacy_ioremap(struct drm_local_map *map, struct drm_device *dev)
    129 {
    130 	if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
    131 		map->handle = agp_remap(map->offset, map->size, dev);
    132 	else
    133 		map->handle = ioremap(map->offset, map->size);
    134 }
    135 EXPORT_SYMBOL(drm_legacy_ioremap);
    136 
    137 void drm_legacy_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
    138 {
    139 	if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
    140 		map->handle = agp_remap(map->offset, map->size, dev);
    141 	else
    142 		map->handle = ioremap_wc(map->offset, map->size);
    143 }
    144 EXPORT_SYMBOL(drm_legacy_ioremap_wc);
    145 
    146 void drm_legacy_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
    147 {
    148 	if (!map->handle || !map->size)
    149 		return;
    150 
    151 	if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
    152 		vunmap(map->handle);
    153 	else
    154 		iounmap(map->handle);
    155 }
    156 EXPORT_SYMBOL(drm_legacy_ioremapfree);
    157