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