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