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