9c2f84969f0115be10757b4bf34c630f6fb31306
[dragonfly.git] / sys / dev / drm / drm_scatter.c
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
25 /** @file drm_scatter.c
26  * Allocation of memory for scatter-gather mappings by the graphics chip.
27  *
28  * The memory allocated here is then made into an aperture in the card
29  * by mapping the pages into the GART.
30  */
31
32 #include "dev/drm/drmP.h"
33
34 int
35 drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather *request)
36 {
37         struct drm_sg_mem *entry;
38         vm_size_t size;
39         vm_pindex_t pindex;
40
41         if (dev->sg)
42                 return EINVAL;
43
44         DRM_DEBUG("request size=%ld\n", request->size);
45
46         entry = malloc(sizeof(*entry), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
47
48         size = round_page(request->size);
49         entry->pages = OFF_TO_IDX(size);
50         entry->busaddr = malloc(entry->pages * sizeof(*entry->busaddr),
51             DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
52
53         entry->vaddr = kmem_alloc_attr(&kernel_map, size, M_WAITOK | M_ZERO,
54             0, BUS_SPACE_MAXADDR_32BIT, VM_MEMATTR_WRITE_COMBINING);
55         if (entry->vaddr == 0) {
56                 drm_sg_cleanup(entry);
57                 return (ENOMEM);
58         }
59
60         for(pindex = 0; pindex < entry->pages; pindex++) {
61                 entry->busaddr[pindex] =
62                     vtophys(entry->vaddr + IDX_TO_OFF(pindex));
63         }
64
65         DRM_LOCK();
66         if (dev->sg) {
67                 DRM_UNLOCK();
68                 drm_sg_cleanup(entry);
69                 return (EINVAL);
70         }
71         dev->sg = entry;
72         DRM_UNLOCK();
73
74         request->handle = entry->vaddr;
75
76         DRM_DEBUG("allocated %ju pages @ 0x%08zx, contents=%08lx\n",
77             entry->pages, entry->vaddr, *(unsigned long *)entry->vaddr);
78
79         return (0);
80 }
81
82 int
83 drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
84                    struct drm_file *file_priv)
85 {
86         struct drm_scatter_gather *request = data;
87
88         DRM_DEBUG("\n");
89
90         return (drm_sg_alloc(dev, request));
91 }
92
93 void
94 drm_sg_cleanup(struct drm_sg_mem *entry)
95 {
96         if (entry == NULL)
97                 return;
98
99         if (entry->vaddr != 0)
100                 kmem_free(&kernel_map, entry->vaddr, IDX_TO_OFF(entry->pages));
101
102         free(entry->busaddr, DRM_MEM_SGLISTS);
103         free(entry, DRM_MEM_DRIVER);
104
105         return;
106 }
107
108 int
109 drm_sg_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
110 {
111         struct drm_scatter_gather *request = data;
112         struct drm_sg_mem *entry;
113
114         DRM_LOCK();
115         entry = dev->sg;
116         dev->sg = NULL;
117         DRM_UNLOCK();
118
119         if (!entry || entry->vaddr != request->handle)
120                 return (EINVAL);
121
122         DRM_DEBUG("free 0x%zx\n", entry->vaddr);
123
124         drm_sg_cleanup(entry);
125
126         return (0);
127 }