1/*	$NetBSD: mga_ioc32.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $	*/
2
3/**
4 * \file mga_ioc32.c
5 *
6 * 32-bit ioctl compatibility routines for the MGA DRM.
7 *
8 * \author Dave Airlie <airlied@linux.ie> with code from patches by Egbert Eich
9 *
10 *
11 * Copyright (C) Paul Mackerras 2005
12 * Copyright (C) Egbert Eich 2003,2004
13 * Copyright (C) Dave Airlie 2005
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30 * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 * IN THE SOFTWARE.
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: mga_ioc32.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $");
38
39#include <linux/compat.h>
40
41#include "mga_drv.h"
42
43typedef struct drm32_mga_init {
44	int func;
45	u32 sarea_priv_offset;
46	int chipset;
47	int sgram;
48	unsigned int maccess;
49	unsigned int fb_cpp;
50	unsigned int front_offset, front_pitch;
51	unsigned int back_offset, back_pitch;
52	unsigned int depth_cpp;
53	unsigned int depth_offset, depth_pitch;
54	unsigned int texture_offset[MGA_NR_TEX_HEAPS];
55	unsigned int texture_size[MGA_NR_TEX_HEAPS];
56	u32 fb_offset;
57	u32 mmio_offset;
58	u32 status_offset;
59	u32 warp_offset;
60	u32 primary_offset;
61	u32 buffers_offset;
62} drm_mga_init32_t;
63
64static int compat_mga_init(struct file *file, unsigned int cmd,
65			   unsigned long arg)
66{
67	drm_mga_init32_t init32;
68	drm_mga_init_t init;
69
70	if (copy_from_user(&init32, (void __user *)arg, sizeof(init32)))
71		return -EFAULT;
72
73	init.func = init32.func;
74	init.sarea_priv_offset = init32.sarea_priv_offset;
75	memcpy(&init.chipset, &init32.chipset,
76		offsetof(drm_mga_init_t, fb_offset) -
77		offsetof(drm_mga_init_t, chipset));
78	init.fb_offset = init32.fb_offset;
79	init.mmio_offset = init32.mmio_offset;
80	init.status_offset = init32.status_offset;
81	init.warp_offset = init32.warp_offset;
82	init.primary_offset = init32.primary_offset;
83	init.buffers_offset = init32.buffers_offset;
84
85	return drm_ioctl_kernel(file, mga_dma_init, &init,
86				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
87}
88
89typedef struct drm_mga_getparam32 {
90	int param;
91	u32 value;
92} drm_mga_getparam32_t;
93
94static int compat_mga_getparam(struct file *file, unsigned int cmd,
95			       unsigned long arg)
96{
97	drm_mga_getparam32_t getparam32;
98	drm_mga_getparam_t getparam;
99
100	if (copy_from_user(&getparam32, (void __user *)arg, sizeof(getparam32)))
101		return -EFAULT;
102
103	getparam.param = getparam32.param;
104	getparam.value = compat_ptr(getparam32.value);
105	return drm_ioctl_kernel(file, mga_getparam, &getparam, DRM_AUTH);
106}
107
108typedef struct drm_mga_drm_bootstrap32 {
109	u32 texture_handle;
110	u32 texture_size;
111	u32 primary_size;
112	u32 secondary_bin_count;
113	u32 secondary_bin_size;
114	u32 agp_mode;
115	u8 agp_size;
116} drm_mga_dma_bootstrap32_t;
117
118static int compat_mga_dma_bootstrap(struct file *file, unsigned int cmd,
119				    unsigned long arg)
120{
121	drm_mga_dma_bootstrap32_t dma_bootstrap32;
122	drm_mga_dma_bootstrap_t dma_bootstrap;
123	int err;
124
125	if (copy_from_user(&dma_bootstrap32, (void __user *)arg,
126			   sizeof(dma_bootstrap32)))
127		return -EFAULT;
128
129	dma_bootstrap.texture_handle = dma_bootstrap32.texture_handle;
130	dma_bootstrap.texture_size = dma_bootstrap32.texture_size;
131	dma_bootstrap.primary_size = dma_bootstrap32.primary_size;
132	dma_bootstrap.secondary_bin_count = dma_bootstrap32.secondary_bin_count;
133	dma_bootstrap.secondary_bin_size = dma_bootstrap32.secondary_bin_size;
134	dma_bootstrap.agp_mode = dma_bootstrap32.agp_mode;
135	dma_bootstrap.agp_size = dma_bootstrap32.agp_size;
136
137	err = drm_ioctl_kernel(file, mga_dma_bootstrap, &dma_bootstrap,
138				DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY);
139	if (err)
140		return err;
141
142	dma_bootstrap32.texture_handle = dma_bootstrap.texture_handle;
143	dma_bootstrap32.texture_size = dma_bootstrap.texture_size;
144	dma_bootstrap32.primary_size = dma_bootstrap.primary_size;
145	dma_bootstrap32.secondary_bin_count = dma_bootstrap.secondary_bin_count;
146	dma_bootstrap32.secondary_bin_size = dma_bootstrap.secondary_bin_size;
147	dma_bootstrap32.agp_mode = dma_bootstrap.agp_mode;
148	dma_bootstrap32.agp_size = dma_bootstrap.agp_size;
149	if (copy_to_user((void __user *)arg, &dma_bootstrap32,
150			 sizeof(dma_bootstrap32)))
151		return -EFAULT;
152
153	return 0;
154}
155
156static struct {
157	drm_ioctl_compat_t *fn;
158	char *name;
159} mga_compat_ioctls[] = {
160#define DRM_IOCTL32_DEF(n, f)[DRM_##n] = {.fn = f, .name = #n}
161	DRM_IOCTL32_DEF(MGA_INIT, compat_mga_init),
162	DRM_IOCTL32_DEF(MGA_GETPARAM, compat_mga_getparam),
163	DRM_IOCTL32_DEF(MGA_DMA_BOOTSTRAP, compat_mga_dma_bootstrap),
164};
165
166/**
167 * Called whenever a 32-bit process running under a 64-bit kernel
168 * performs an ioctl on /dev/dri/card<n>.
169 *
170 * \param filp file pointer.
171 * \param cmd command.
172 * \param arg user argument.
173 * \return zero on success or negative number on failure.
174 */
175long mga_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
176{
177	unsigned int nr = DRM_IOCTL_NR(cmd);
178	struct drm_file *file_priv = filp->private_data;
179	drm_ioctl_compat_t *fn = NULL;
180	int ret;
181
182	if (nr < DRM_COMMAND_BASE)
183		return drm_compat_ioctl(filp, cmd, arg);
184
185	if (nr >= DRM_COMMAND_BASE + ARRAY_SIZE(mga_compat_ioctls))
186		return drm_ioctl(filp, cmd, arg);
187
188	fn = mga_compat_ioctls[nr - DRM_COMMAND_BASE].fn;
189	if (!fn)
190		return drm_ioctl(filp, cmd, arg);
191
192	DRM_DEBUG("pid=%d, dev=0x%lx, auth=%d, %s\n",
193		  task_pid_nr(current),
194		  (long)old_encode_dev(file_priv->minor->kdev->devt),
195		  file_priv->authenticated,
196		  mga_compat_ioctls[nr - DRM_COMMAND_BASE].name);
197	ret = (*fn) (filp, cmd, arg);
198	if (ret)
199		DRM_DEBUG("ret = %d\n", ret);
200	return ret;
201}
202