drm: Sync drm/drmP.h with Linux 4.7.10
[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  * $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 #include <vm/vm_kern.h>
37
38 static void drm_sg_cleanup(struct drm_sg_mem * entry)
39 {
40         if (entry == NULL)
41                 return;
42
43         if (entry->vaddr != 0)
44                 kmem_free(&kernel_map, entry->vaddr, IDX_TO_OFF(entry->pages));
45
46         kfree(entry->busaddr);
47         kfree(entry);
48 }
49
50 void drm_legacy_sg_cleanup(struct drm_device *dev)
51 {
52         if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
53             !drm_core_check_feature(dev, DRIVER_MODESET)) {
54                 drm_sg_cleanup(dev->sg);
55                 dev->sg = NULL;
56         }
57 }
58
59 int drm_legacy_sg_alloc(struct drm_device *dev, void *data,
60                         struct drm_file *file_priv)
61 {
62         struct drm_scatter_gather *request = data;
63         struct drm_sg_mem *entry;
64         vm_size_t size;
65         vm_pindex_t pindex;
66
67         if (dev->sg)
68                 return -EINVAL;
69
70         DRM_DEBUG("request size=%ld\n", request->size);
71
72         entry = kmalloc(sizeof(*entry), M_DRM, M_WAITOK | M_ZERO);
73
74         size = round_page(request->size);
75         entry->pages = OFF_TO_IDX(size);
76         entry->busaddr = kmalloc(entry->pages * sizeof(*entry->busaddr),
77             M_DRM, M_WAITOK | M_ZERO);
78
79         entry->vaddr = kmem_alloc_attr(&kernel_map, size,
80                                        VM_SUBSYS_DRM_SCAT, M_WAITOK | M_ZERO,
81                                        0, BUS_SPACE_MAXADDR_32BIT,
82                                        VM_MEMATTR_WRITE_COMBINING);
83         if (entry->vaddr == 0) {
84                 drm_sg_cleanup(entry);
85                 return (-ENOMEM);
86         }
87
88         for(pindex = 0; pindex < entry->pages; pindex++) {
89                 entry->busaddr[pindex] =
90                     vtophys(entry->vaddr + IDX_TO_OFF(pindex));
91         }
92
93         DRM_LOCK(dev);
94         if (dev->sg) {
95                 DRM_UNLOCK(dev);
96                 drm_sg_cleanup(entry);
97                 return (-EINVAL);
98         }
99         dev->sg = entry;
100         DRM_UNLOCK(dev);
101
102         request->handle = entry->vaddr;
103
104         DRM_DEBUG("allocated %ju pages @ 0x%08jx, contents=%08lx\n",
105             entry->pages, (uintmax_t)entry->vaddr,
106             *(unsigned long *)entry->vaddr);
107
108         return (0);
109 }
110
111 int drm_legacy_sg_free(struct drm_device *dev, void *data,
112                        struct drm_file *file_priv)
113 {
114         struct drm_scatter_gather *request = data;
115         struct drm_sg_mem *entry;
116
117         DRM_LOCK(dev);
118         entry = dev->sg;
119         dev->sg = NULL;
120         DRM_UNLOCK(dev);
121
122         if (!entry || entry->vaddr != request->handle)
123                 return (-EINVAL);
124
125         DRM_DEBUG("free 0x%jx\n", (uintmax_t)entry->vaddr);
126
127         drm_sg_cleanup(entry);
128
129         return (0);
130 }