xref: /freebsd/sys/dev/drm2/drm_memory.c (revision 477129542c073f9c1e46c12e27da454b82ac8ff3)
1 /*-
2  *Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * Copyright (c) 2011 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Konstantin Belousov
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the next
18  * paragraph) shall be included in all copies or substantial portions of the
19  * Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  *
29  * Authors:
30  *    Rickard E. (Rik) Faith <faith@valinux.com>
31  *    Gareth Hughes <gareth@valinux.com>
32  *
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 /** @file drm_memory.c
39  * Wrappers for kernel memory allocation routines, and MTRR management support.
40  *
41  * This file previously implemented a memory consumption tracking system using
42  * the "area" argument for various different types of allocations, but that
43  * has been stripped out for now.
44  */
45 
46 #include <dev/drm2/drmP.h>
47 
48 MALLOC_DEFINE(DRM_MEM_DMA, "drm_dma", "DRM DMA Data Structures");
49 MALLOC_DEFINE(DRM_MEM_SAREA, "drm_sarea", "DRM SAREA Data Structures");
50 MALLOC_DEFINE(DRM_MEM_DRIVER, "drm_driver", "DRM DRIVER Data Structures");
51 MALLOC_DEFINE(DRM_MEM_MAGIC, "drm_magic", "DRM MAGIC Data Structures");
52 MALLOC_DEFINE(DRM_MEM_IOCTLS, "drm_ioctls", "DRM IOCTL Data Structures");
53 MALLOC_DEFINE(DRM_MEM_MAPS, "drm_maps", "DRM MAP Data Structures");
54 MALLOC_DEFINE(DRM_MEM_BUFS, "drm_bufs", "DRM BUFFER Data Structures");
55 MALLOC_DEFINE(DRM_MEM_SEGS, "drm_segs", "DRM SEGMENTS Data Structures");
56 MALLOC_DEFINE(DRM_MEM_PAGES, "drm_pages", "DRM PAGES Data Structures");
57 MALLOC_DEFINE(DRM_MEM_FILES, "drm_files", "DRM FILE Data Structures");
58 MALLOC_DEFINE(DRM_MEM_QUEUES, "drm_queues", "DRM QUEUE Data Structures");
59 MALLOC_DEFINE(DRM_MEM_CMDS, "drm_cmds", "DRM COMMAND Data Structures");
60 MALLOC_DEFINE(DRM_MEM_MAPPINGS, "drm_mapping", "DRM MAPPING Data Structures");
61 MALLOC_DEFINE(DRM_MEM_BUFLISTS, "drm_buflists", "DRM BUFLISTS Data Structures");
62 MALLOC_DEFINE(DRM_MEM_AGPLISTS, "drm_agplists", "DRM AGPLISTS Data Structures");
63 MALLOC_DEFINE(DRM_MEM_CTXBITMAP, "drm_ctxbitmap",
64     "DRM CTXBITMAP Data Structures");
65 MALLOC_DEFINE(DRM_MEM_SGLISTS, "drm_sglists", "DRM SGLISTS Data Structures");
66 MALLOC_DEFINE(DRM_MEM_DRAWABLE, "drm_drawable", "DRM DRAWABLE Data Structures");
67 MALLOC_DEFINE(DRM_MEM_MM, "drm_sman", "DRM MEMORY MANAGER Data Structures");
68 MALLOC_DEFINE(DRM_MEM_HASHTAB, "drm_hashtab", "DRM HASHTABLE Data Structures");
69 MALLOC_DEFINE(DRM_MEM_KMS, "drm_kms", "DRM KMS Data Structures");
70 
71 void drm_mem_init(void)
72 {
73 }
74 
75 void drm_mem_uninit(void)
76 {
77 }
78 
79 void *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map)
80 {
81 	return pmap_mapdev_attr(map->offset, map->size, VM_MEMATTR_WRITE_COMBINING);
82 }
83 
84 void *drm_ioremap(struct drm_device *dev, drm_local_map_t *map)
85 {
86 	return pmap_mapdev(map->offset, map->size);
87 }
88 
89 void drm_ioremapfree(drm_local_map_t *map)
90 {
91 	pmap_unmapdev((vm_offset_t) map->virtual, map->size);
92 }
93 
94 int
95 drm_mtrr_add(unsigned long offset, size_t size, int flags)
96 {
97 	int act;
98 	struct mem_range_desc mrdesc;
99 
100 	mrdesc.mr_base = offset;
101 	mrdesc.mr_len = size;
102 	mrdesc.mr_flags = flags;
103 	act = MEMRANGE_SET_UPDATE;
104 	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
105 	return mem_range_attr_set(&mrdesc, &act);
106 }
107 
108 int
109 drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags)
110 {
111 	int act;
112 	struct mem_range_desc mrdesc;
113 
114 	mrdesc.mr_base = offset;
115 	mrdesc.mr_len = size;
116 	mrdesc.mr_flags = flags;
117 	act = MEMRANGE_SET_REMOVE;
118 	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
119 	return mem_range_attr_set(&mrdesc, &act);
120 }
121 
122 void
123 drm_clflush_pages(vm_page_t *pages, unsigned long num_pages)
124 {
125 
126 	pmap_invalidate_cache_pages(pages, num_pages);
127 }
128 
129 void
130 drm_clflush_virt_range(char *addr, unsigned long length)
131 {
132 
133 	pmap_invalidate_cache_range((vm_offset_t)addr,
134 	    (vm_offset_t)addr + length, TRUE);
135 }
136