1145132Sanholt/*-
2207067Srnoland * Copyright (c) 2009 Robert C. Noland III <rnoland@FreeBSD.org>
3145132Sanholt * All Rights Reserved.
4145132Sanholt *
5145132Sanholt * Permission is hereby granted, free of charge, to any person obtaining a
6145132Sanholt * copy of this software and associated documentation files (the "Software"),
7145132Sanholt * to deal in the Software without restriction, including without limitation
8145132Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9145132Sanholt * and/or sell copies of the Software, and to permit persons to whom the
10145132Sanholt * Software is furnished to do so, subject to the following conditions:
11145132Sanholt *
12145132Sanholt * The above copyright notice and this permission notice (including the next
13145132Sanholt * paragraph) shall be included in all copies or substantial portions of the
14145132Sanholt * Software.
15145132Sanholt *
16145132Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17145132Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18145132Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19145132Sanholt * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20145132Sanholt * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21145132Sanholt * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22145132Sanholt * DEALINGS IN THE SOFTWARE.
23145132Sanholt */
24145132Sanholt
25152909Sanholt#include <sys/cdefs.h>
26152909Sanholt__FBSDID("$FreeBSD: stable/11/sys/dev/drm/drm_scatter.c 316073 2017-03-28 06:07:59Z kib $");
27152909Sanholt
28182080Srnoland/** @file drm_scatter.c
29182080Srnoland * Allocation of memory for scatter-gather mappings by the graphics chip.
30182080Srnoland * The memory allocated here is then made into an aperture in the card
31207067Srnoland * by mapping the pages into the GART.
32182080Srnoland */
33182080Srnoland
34145132Sanholt#include "dev/drm/drmP.h"
35145132Sanholt
36186295Srnolandint
37186295Srnolanddrm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather *request)
38145132Sanholt{
39186295Srnoland	struct drm_sg_mem *entry;
40207067Srnoland	vm_size_t size;
41207067Srnoland	vm_pindex_t pindex;
42145132Sanholt
43183573Srnoland	if (dev->sg)
44145132Sanholt		return EINVAL;
45145132Sanholt
46207067Srnoland	DRM_DEBUG("request size=%ld\n", request->size);
47145132Sanholt
48207067Srnoland	entry = malloc(sizeof(*entry), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
49207067Srnoland
50207067Srnoland	size = round_page(request->size);
51316073Skib	entry->pages = atop(size);
52198694Srnoland	entry->busaddr = malloc(entry->pages * sizeof(*entry->busaddr),
53207067Srnoland	    DRM_MEM_SGLISTS, M_WAITOK | M_ZERO);
54145132Sanholt
55254025Sjeff	entry->vaddr = kmem_alloc_attr(kernel_arena, size, M_WAITOK | M_ZERO,
56207067Srnoland	    0, BUS_SPACE_MAXADDR_32BIT, VM_MEMATTR_WRITE_COMBINING);
57207067Srnoland	if (entry->vaddr == 0) {
58198694Srnoland		drm_sg_cleanup(entry);
59207067Srnoland		return (ENOMEM);
60152909Sanholt	}
61145132Sanholt
62207067Srnoland	for(pindex = 0; pindex < entry->pages; pindex++) {
63207067Srnoland		entry->busaddr[pindex] =
64207067Srnoland		    vtophys(entry->vaddr + IDX_TO_OFF(pindex));
65186295Srnoland	}
66186295Srnoland
67145132Sanholt	DRM_LOCK();
68145132Sanholt	if (dev->sg) {
69145132Sanholt		DRM_UNLOCK();
70145132Sanholt		drm_sg_cleanup(entry);
71207067Srnoland		return (EINVAL);
72145132Sanholt	}
73145132Sanholt	dev->sg = entry;
74145132Sanholt	DRM_UNLOCK();
75145132Sanholt
76207067Srnoland	request->handle = entry->vaddr;
77207066Srnoland
78207067Srnoland	DRM_DEBUG("allocated %ju pages @ 0x%08zx, contents=%08lx\n",
79207067Srnoland	    entry->pages, entry->vaddr, *(unsigned long *)entry->vaddr);
80207066Srnoland
81207067Srnoland	return (0);
82145132Sanholt}
83145132Sanholt
84186295Srnolandint
85186295Srnolanddrm_sg_alloc_ioctl(struct drm_device *dev, void *data,
86186295Srnoland		   struct drm_file *file_priv)
87186295Srnoland{
88183573Srnoland	struct drm_scatter_gather *request = data;
89182080Srnoland
90186295Srnoland	DRM_DEBUG("\n");
91182080Srnoland
92207067Srnoland	return (drm_sg_alloc(dev, request));
93182080Srnoland}
94182080Srnoland
95186295Srnolandvoid
96186295Srnolanddrm_sg_cleanup(struct drm_sg_mem *entry)
97182080Srnoland{
98207067Srnoland	if (entry == NULL)
99207067Srnoland		return;
100186295Srnoland
101207067Srnoland	if (entry->vaddr != 0)
102254025Sjeff		kmem_free(kernel_arena, entry->vaddr, IDX_TO_OFF(entry->pages));
103207067Srnoland
104207067Srnoland	free(entry->busaddr, DRM_MEM_SGLISTS);
105207067Srnoland	free(entry, DRM_MEM_DRIVER);
106207067Srnoland
107207067Srnoland	return;
108186295Srnoland}
109186295Srnoland
110186295Srnolandint
111186295Srnolanddrm_sg_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
112186295Srnoland{
113183573Srnoland	struct drm_scatter_gather *request = data;
114186295Srnoland	struct drm_sg_mem *entry;
115145132Sanholt
116145132Sanholt	DRM_LOCK();
117145132Sanholt	entry = dev->sg;
118145132Sanholt	dev->sg = NULL;
119145132Sanholt	DRM_UNLOCK();
120145132Sanholt
121207067Srnoland	if (!entry || entry->vaddr != request->handle)
122207067Srnoland		return (EINVAL);
123145132Sanholt
124207067Srnoland	DRM_DEBUG("free 0x%zx\n", entry->vaddr);
125145132Sanholt
126145132Sanholt	drm_sg_cleanup(entry);
127145132Sanholt
128207067Srnoland	return (0);
129145132Sanholt}
130