blob: fbea69d6f90902d93f910a4d93e50ba1ed9e3c61 [file] [log] [blame]
Qian Cai45b2fda2019-07-15 09:42:53 -04001/*
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_memory.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Memory management wrappers for DRM
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
Dave Airlieb5e89ed2005-09-25 14:28:13 +10009/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040036#include <linux/export.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020037#include <linux/highmem.h>
38#include <linux/pci.h>
39#include <linux/vmalloc.h>
Michael D Labriola913b2cb2019-02-19 18:08:12 -050040#include <xen/xen.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020041
42#include <drm/drm_agpsupport.h>
Ville Syrjälä2c055932019-07-10 15:51:41 +030043#include <drm/drm_cache.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020044#include <drm/drm_device.h>
45
David Herrmanncc5ea592014-08-29 12:12:32 +020046#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Daniel Vettera7fb8a22015-09-09 16:45:52 +020048#if IS_ENABLED(CONFIG_AGP)
David Herrmannd6db6562014-08-29 12:12:35 +020049
50#ifdef HAVE_PAGE_AGP
51# include <asm/agp.h>
52#else
53# ifdef __powerpc__
Benjamin Herrenschmidt37035e72016-06-27 19:00:23 +100054# define PAGE_AGP pgprot_noncached_wc(PAGE_KERNEL)
David Herrmannd6db6562014-08-29 12:12:35 +020055# else
56# define PAGE_AGP PAGE_KERNEL
57# endif
58#endif
59
Adrian Bunk031de962006-04-10 23:18:27 -070060static void *agp_remap(unsigned long offset, unsigned long size,
Mamta Shukla7bd01a02018-10-16 02:56:44 +053061 struct drm_device *dev)
Dave Airlie31f64bd12006-04-07 16:55:43 +100062{
Dave Airlie07613ba2009-06-12 14:11:41 +100063 unsigned long i, num_pages =
Dave Airlie31f64bd12006-04-07 16:55:43 +100064 PAGE_ALIGN(size) / PAGE_SIZE;
65 struct drm_agp_mem *agpmem;
66 struct page **page_map;
Dave Airlie07613ba2009-06-12 14:11:41 +100067 struct page **phys_page_map;
Dave Airlie31f64bd12006-04-07 16:55:43 +100068 void *addr;
69
70 size = PAGE_ALIGN(size);
71
72#ifdef __alpha__
73 offset -= dev->hose->mem_space->start;
74#endif
75
Dave Airliebd1b3312007-05-26 05:01:51 +100076 list_for_each_entry(agpmem, &dev->agp->memory, head)
Dave Airlie31f64bd12006-04-07 16:55:43 +100077 if (agpmem->bound <= offset
78 && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
79 (offset + size))
80 break;
Dan Carpenter404b0172010-04-27 14:11:05 -070081 if (&agpmem->head == &dev->agp->memory)
Dave Airlie31f64bd12006-04-07 16:55:43 +100082 return NULL;
83
84 /*
85 * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
86 * the CPU do not get remapped by the GART. We fix this by using the kernel's
87 * page-table instead (that's probably faster anyhow...).
88 */
89 /* note: use vmalloc() because num_pages could be large... */
Kees Cook42bc47b2018-06-12 14:27:11 -070090 page_map = vmalloc(array_size(num_pages, sizeof(struct page *)));
Dave Airlie31f64bd12006-04-07 16:55:43 +100091 if (!page_map)
92 return NULL;
93
Dave Airlie07613ba2009-06-12 14:11:41 +100094 phys_page_map = (agpmem->memory->pages + (offset - agpmem->bound) / PAGE_SIZE);
Dave Airlie31f64bd12006-04-07 16:55:43 +100095 for (i = 0; i < num_pages; ++i)
Dave Airlie07613ba2009-06-12 14:11:41 +100096 page_map[i] = phys_page_map[i];
Dave Airlie31f64bd12006-04-07 16:55:43 +100097 addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
98 vfree(page_map);
99
100 return addr;
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/** Wrapper around agp_free_memory() */
Mamta Shukla7bd01a02018-10-16 02:56:44 +0530104void drm_free_agp(struct agp_memory *handle, int pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Daniel Vetter89c37262010-08-23 22:53:36 +0200106 agp_free_memory(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/** Wrapper around agp_bind_memory() */
Mamta Shukla7bd01a02018-10-16 02:56:44 +0530110int drm_bind_agp(struct agp_memory *handle, unsigned int start)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Daniel Vetter89c37262010-08-23 22:53:36 +0200112 return agp_bind_memory(handle, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/** Wrapper around agp_unbind_memory() */
Mamta Shukla7bd01a02018-10-16 02:56:44 +0530116int drm_unbind_agp(struct agp_memory *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Daniel Vetter89c37262010-08-23 22:53:36 +0200118 return agp_unbind_memory(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Adrian Bunk031de962006-04-10 23:18:27 -0700120
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200121#else /* CONFIG_AGP */
Adrian Bunk031de962006-04-10 23:18:27 -0700122static inline void *agp_remap(unsigned long offset, unsigned long size,
Mamta Shukla7bd01a02018-10-16 02:56:44 +0530123 struct drm_device *dev)
Adrian Bunk031de962006-04-10 23:18:27 -0700124{
125 return NULL;
126}
127
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200128#endif /* CONFIG_AGP */
Dave Airlie31f64bd12006-04-07 16:55:43 +1000129
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200130void drm_legacy_ioremap(struct drm_local_map *map, struct drm_device *dev)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100131{
Daniel Vetterd9906752013-12-11 11:34:35 +0100132 if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100133 map->handle = agp_remap(map->offset, map->size, dev);
134 else
135 map->handle = ioremap(map->offset, map->size);
136}
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200137EXPORT_SYMBOL(drm_legacy_ioremap);
Christoph Hellwig004a7722007-01-08 21:56:59 +1100138
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200139void drm_legacy_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
Dave Airlie242e3df2008-07-15 15:48:05 +1000140{
Daniel Vetterd9906752013-12-11 11:34:35 +0100141 if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
Dave Airlie9b8d5a12009-02-07 11:15:41 +1000142 map->handle = agp_remap(map->offset, map->size, dev);
143 else
144 map->handle = ioremap_wc(map->offset, map->size);
Dave Airlie242e3df2008-07-15 15:48:05 +1000145}
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200146EXPORT_SYMBOL(drm_legacy_ioremap_wc);
Dave Airlie9b8d5a12009-02-07 11:15:41 +1000147
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200148void drm_legacy_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100149{
150 if (!map->handle || !map->size)
151 return;
152
Daniel Vetterd9906752013-12-11 11:34:35 +0100153 if (dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100154 vunmap(map->handle);
155 else
156 iounmap(map->handle);
157}
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200158EXPORT_SYMBOL(drm_legacy_ioremapfree);
Chunming Zhou826263632018-02-09 10:44:08 +0800159
Michael D Labriola913b2cb2019-02-19 18:08:12 -0500160bool drm_need_swiotlb(int dma_bits)
Chunming Zhou826263632018-02-09 10:44:08 +0800161{
162 struct resource *tmp;
Arnd Bergmann58871702018-02-22 12:47:51 +0100163 resource_size_t max_iomem = 0;
Chunming Zhou826263632018-02-09 10:44:08 +0800164
Michael D Labriola913b2cb2019-02-19 18:08:12 -0500165 /*
166 * Xen paravirtual hosts require swiotlb regardless of requested dma
167 * transfer size.
168 *
169 * NOTE: Really, what it requires is use of the dma_alloc_coherent
170 * allocator used in ttm_dma_populate() instead of
171 * ttm_populate_and_map_pages(), which bounce buffers so much in
172 * Xen it leads to swiotlb buffer exhaustion.
173 */
174 if (xen_pv_domain())
175 return true;
176
Christian König64e1f832019-03-13 10:11:19 +0100177 /*
178 * Enforce dma_alloc_coherent when memory encryption is active as well
179 * for the same reasons as for Xen paravirtual hosts.
180 */
181 if (mem_encrypt_active())
182 return true;
183
Chunming Zhou826263632018-02-09 10:44:08 +0800184 for (tmp = iomem_resource.child; tmp; tmp = tmp->sibling) {
185 max_iomem = max(max_iomem, tmp->end);
186 }
187
Michael D Labriola913b2cb2019-02-19 18:08:12 -0500188 return max_iomem > ((u64)1 << dma_bits);
Chunming Zhou826263632018-02-09 10:44:08 +0800189}
Michael D Labriola913b2cb2019-02-19 18:08:12 -0500190EXPORT_SYMBOL(drm_need_swiotlb);