xref: /dragonfly/sys/dev/drm/drm_scatter.c (revision c69bf40f)
1 /*-
2  * Copyright (c) 2009 Robert C. Noland III <rnoland@FreeBSD.org>
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * $FreeBSD: src/sys/dev/drm2/drm_scatter.c,v 1.1 2012/05/22 11:07:44 kib Exp $
25  */
26 
27 /** @file drm_scatter.c
28  * Allocation of memory for scatter-gather mappings by the graphics chip.
29  * The memory allocated here is then made into an aperture in the card
30  * by mapping the pages into the GART.
31  */
32 
33 #include <drm/drmP.h>
34 #include "drm_legacy.h"
35 
36 static void drm_sg_cleanup(struct drm_sg_mem * entry)
37 {
38 	if (entry == NULL)
39 		return;
40 
41 	if (entry->vaddr != 0)
42 		kmem_free(&kernel_map, entry->vaddr, IDX_TO_OFF(entry->pages));
43 
44 	kfree(entry->busaddr);
45 	kfree(entry);
46 }
47 
48 void drm_legacy_sg_cleanup(struct drm_device *dev)
49 {
50 	if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
51 	    !drm_core_check_feature(dev, DRIVER_MODESET)) {
52 		drm_sg_cleanup(dev->sg);
53 		dev->sg = NULL;
54 	}
55 }
56 
57 int drm_legacy_sg_alloc(struct drm_device *dev, void *data,
58 			struct drm_file *file_priv)
59 {
60 	struct drm_scatter_gather *request = data;
61 	struct drm_sg_mem *entry;
62 	vm_size_t size;
63 	vm_pindex_t pindex;
64 
65 	if (dev->sg)
66 		return -EINVAL;
67 
68 	DRM_DEBUG("request size=%ld\n", request->size);
69 
70 	entry = kmalloc(sizeof(*entry), M_DRM, M_WAITOK | M_ZERO);
71 
72 	size = round_page(request->size);
73 	entry->pages = OFF_TO_IDX(size);
74 	entry->busaddr = kmalloc(entry->pages * sizeof(*entry->busaddr),
75 	    M_DRM, M_WAITOK | M_ZERO);
76 
77 	entry->vaddr = kmem_alloc_attr(&kernel_map, size,
78 				       VM_SUBSYS_DRM_SCAT, M_WAITOK | M_ZERO,
79 				       0, BUS_SPACE_MAXADDR_32BIT,
80 				       VM_MEMATTR_WRITE_COMBINING);
81 	if (entry->vaddr == 0) {
82 		drm_sg_cleanup(entry);
83 		return (-ENOMEM);
84 	}
85 
86 	for(pindex = 0; pindex < entry->pages; pindex++) {
87 		entry->busaddr[pindex] =
88 		    vtophys(entry->vaddr + IDX_TO_OFF(pindex));
89 	}
90 
91 	DRM_LOCK(dev);
92 	if (dev->sg) {
93 		DRM_UNLOCK(dev);
94 		drm_sg_cleanup(entry);
95 		return (-EINVAL);
96 	}
97 	dev->sg = entry;
98 	DRM_UNLOCK(dev);
99 
100 	request->handle = entry->vaddr;
101 
102 	DRM_DEBUG("allocated %ju pages @ 0x%08jx, contents=%08lx\n",
103 	    entry->pages, (uintmax_t)entry->vaddr,
104 	    *(unsigned long *)entry->vaddr);
105 
106 	return (0);
107 }
108 
109 int drm_legacy_sg_free(struct drm_device *dev, void *data,
110 		       struct drm_file *file_priv)
111 {
112 	struct drm_scatter_gather *request = data;
113 	struct drm_sg_mem *entry;
114 
115 	DRM_LOCK(dev);
116 	entry = dev->sg;
117 	dev->sg = NULL;
118 	DRM_UNLOCK(dev);
119 
120 	if (!entry || entry->vaddr != request->handle)
121 		return (-EINVAL);
122 
123 	DRM_DEBUG("free 0x%jx\n", (uintmax_t)entry->vaddr);
124 
125 	drm_sg_cleanup(entry);
126 
127 	return (0);
128 }
129