1/**
2 * \file drm_scatter.c
3 * IOCTLs to manage scatter/gather memory
4 *
5 * \author Gareth Hughes <gareth@valinux.com>
6 */
7
8/*
9 * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com
10 *
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
32 */
33
34#include <linux/vmalloc.h>
35#include <linux/slab.h>
36#include <drm/drmP.h>
37#include "drm_legacy.h"
38
39#define DEBUG_SCATTER 0
40
41static inline void *drm_vmalloc_dma(unsigned long size)
42{
43#if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
44	return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL | _PAGE_NO_CACHE);
45#else
46	return vmalloc_32(size);
47#endif
48}
49
50static void drm_sg_cleanup(struct drm_sg_mem * entry)
51{
52	struct page *page;
53	int i;
54
55	for (i = 0; i < entry->pages; i++) {
56		page = entry->pagelist[i];
57		if (page)
58			ClearPageReserved(page);
59	}
60
61	vfree(entry->virtual);
62
63	kfree(entry->busaddr);
64	kfree(entry->pagelist);
65	kfree(entry);
66}
67
68void drm_legacy_sg_cleanup(struct drm_device *dev)
69{
70	if (drm_core_check_feature(dev, DRIVER_SG) && dev->sg &&
71	    !drm_core_check_feature(dev, DRIVER_MODESET)) {
72		drm_sg_cleanup(dev->sg);
73		dev->sg = NULL;
74	}
75}
76#ifdef _LP64
77# define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
78#else
79# define ScatterHandle(x) (unsigned int)(x)
80#endif
81
82int drm_legacy_sg_alloc(struct drm_device *dev, void *data,
83			struct drm_file *file_priv)
84{
85	struct drm_scatter_gather *request = data;
86	struct drm_sg_mem *entry;
87	unsigned long pages, i, j;
88
89	DRM_DEBUG("\n");
90
91	if (drm_core_check_feature(dev, DRIVER_MODESET))
92		return -EINVAL;
93
94	if (!drm_core_check_feature(dev, DRIVER_SG))
95		return -EINVAL;
96
97	if (dev->sg)
98		return -EINVAL;
99
100	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
101	if (!entry)
102		return -ENOMEM;
103
104	pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
105	DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
106
107	entry->pages = pages;
108	entry->pagelist = kcalloc(pages, sizeof(*entry->pagelist), GFP_KERNEL);
109	if (!entry->pagelist) {
110		kfree(entry);
111		return -ENOMEM;
112	}
113
114	entry->busaddr = kcalloc(pages, sizeof(*entry->busaddr), GFP_KERNEL);
115	if (!entry->busaddr) {
116		kfree(entry->pagelist);
117		kfree(entry);
118		return -ENOMEM;
119	}
120
121	entry->virtual = drm_vmalloc_dma(pages << PAGE_SHIFT);
122	if (!entry->virtual) {
123		kfree(entry->busaddr);
124		kfree(entry->pagelist);
125		kfree(entry);
126		return -ENOMEM;
127	}
128
129	/* This also forces the mapping of COW pages, so our page list
130	 * will be valid.  Please don't remove it...
131	 */
132	memset(entry->virtual, 0, pages << PAGE_SHIFT);
133
134	entry->handle = ScatterHandle((unsigned long)entry->virtual);
135
136	DRM_DEBUG("handle  = %08lx\n", entry->handle);
137	DRM_DEBUG("virtual = %p\n", entry->virtual);
138
139	for (i = (unsigned long)entry->virtual, j = 0; j < pages;
140	     i += PAGE_SIZE, j++) {
141		entry->pagelist[j] = vmalloc_to_page((void *)i);
142		if (!entry->pagelist[j])
143			goto failed;
144		SetPageReserved(entry->pagelist[j]);
145	}
146
147	request->handle = entry->handle;
148
149	dev->sg = entry;
150
151#if DEBUG_SCATTER
152	/* Verify that each page points to its virtual address, and vice
153	 * versa.
154	 */
155	{
156		int error = 0;
157
158		for (i = 0; i < pages; i++) {
159			unsigned long *tmp;
160
161			tmp = page_address(entry->pagelist[i]);
162			for (j = 0;
163			     j < PAGE_SIZE / sizeof(unsigned long);
164			     j++, tmp++) {
165				*tmp = 0xcafebabe;
166			}
167			tmp = (unsigned long *)((u8 *) entry->virtual +
168						(PAGE_SIZE * i));
169			for (j = 0;
170			     j < PAGE_SIZE / sizeof(unsigned long);
171			     j++, tmp++) {
172				if (*tmp != 0xcafebabe && error == 0) {
173					error = 1;
174					DRM_ERROR("Scatter allocation error, "
175						  "pagelist does not match "
176						  "virtual mapping\n");
177				}
178			}
179			tmp = page_address(entry->pagelist[i]);
180			for (j = 0;
181			     j < PAGE_SIZE / sizeof(unsigned long);
182			     j++, tmp++) {
183				*tmp = 0;
184			}
185		}
186		if (error == 0)
187			DRM_ERROR("Scatter allocation matches pagelist\n");
188	}
189#endif
190
191	return 0;
192
193      failed:
194	drm_sg_cleanup(entry);
195	return -ENOMEM;
196}
197
198int drm_legacy_sg_free(struct drm_device *dev, void *data,
199		       struct drm_file *file_priv)
200{
201	struct drm_scatter_gather *request = data;
202	struct drm_sg_mem *entry;
203
204	if (drm_core_check_feature(dev, DRIVER_MODESET))
205		return -EINVAL;
206
207	if (!drm_core_check_feature(dev, DRIVER_SG))
208		return -EINVAL;
209
210	entry = dev->sg;
211	dev->sg = NULL;
212
213	if (!entry || entry->handle != request->handle)
214		return -EINVAL;
215
216	DRM_DEBUG("virtual  = %p\n", entry->virtual);
217
218	drm_sg_cleanup(entry);
219
220	return 0;
221}
222