1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. 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, sub license,
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
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, 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 #include <linux/config.h>
26 #include "savage.h"
27 #include "drmP.h"
28 #include "savage_drv.h"
29
30 #define DRIVER_AUTHOR "John Zhao, S3 Graphics Inc."
31
32 #define DRIVER_NAME "savage"
33 #define DRIVER_DESC "Savage4 Family"
34 #define DRIVER_DATE "20011023"
35
36 #define DRIVER_MAJOR 1
37 #define DRIVER_MINOR 0
38 #define DRIVER_PATCHLEVEL 0
39
40 /* Currently Savage4 not implement DMA */
41 /* mark off by Jiayo Hsu, Oct. 23, 2001*/
42
43
44 #define DRIVER_IOCTLS \
45 [DRM_IOCTL_NR(DRM_IOCTL_SAVAGE_ALLOC_CONTINUOUS_MEM)] \
46 = {savage_alloc_continuous_mem,1,0},\
47 [DRM_IOCTL_NR( DRM_IOCTL_SAVAGE_GET_PHYSICS_ADDRESS)] \
48 = {savage_get_physics_address,1,0},\
49 [DRM_IOCTL_NR(DRM_IOCTL_SAVAGE_FREE_CONTINUOUS_MEM)] \
50 = {savage_free_cont_mem,1,0}
51
savage_alloc_continuous_mem(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)52 int savage_alloc_continuous_mem(struct inode *inode, struct file *filp,
53 unsigned int cmd, unsigned long arg)
54 {
55 drm_savage_alloc_cont_mem_t cont_mem;
56 unsigned long size, addr;
57 void *ret;
58 int i;
59 mem_map_t *p;
60 pgprot_t flags;
61
62 /* add to list */
63 drm_file_t *priv = filp->private_data;
64 drm_device_t *dev = priv->dev;
65 drm_map_t *map;
66 drm_map_list_t *list;
67
68 dma_addr_t pa;
69
70 if (copy_from_user(&cont_mem, (drm_savage_alloc_cont_mem_t *) arg, sizeof(cont_mem)))
71 return -EFAULT;
72
73 /*check the parameters */
74 if (cont_mem.size <= 0)
75 return -EINVAL;
76 if( 0xFFFFFFFFUL / cont_mem.size < cont_mem.type )
77 return -EINVAL;
78
79 map = DRM(alloc) (sizeof(*map), DRM_MEM_MAPS);
80 if (!map)
81 return -ENOMEM;
82
83 size = cont_mem.type * cont_mem.size;
84
85 ret = pci_alloc_consistent(/*FIXME*/NULL, size, &pa);
86 if (ret == NULL)
87 return -ENOMEM;
88
89 /* Set the reserverd flag so that the remap_page_range can map these page */
90 for (i = 0, p = virt_to_page(ret); i < size / PAGE_SIZE; i++, p++)
91 SetPageReserved(p);
92
93 cont_mem.phyaddress = pa;
94 cont_mem.location = DRM_SAVAGE_MEM_LOCATION_PCI; /* pci only at present */
95
96 /*Map the memory to user space */
97 down_write(¤t->mm->mmap_sem);
98 addr = do_mmap(NULL, 0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, cont_mem.phyaddress);
99 if ((unsigned long)addr > -1024UL)
100 {
101 up_write(¤t->mm->mmap_sem);
102 return -EINVAL;
103 }
104 pgprot_val(flags) = _PAGE_PRESENT | _PAGE_RW | _PAGE_USER;
105 if (remap_page_range(addr, cont_mem.phyaddress, size, flags))
106 {
107 up_write(¤t->mm->mmap_sem);
108 return -EINVAL;
109 }
110 up_write(¤t->mm->mmap_sem);
111
112 for (i = 0, p = virt_to_page(ret); i < size / PAGE_SIZE; i++, p++)
113 ClearPageReserved(p);
114
115 cont_mem.linear = addr;
116
117 /*map list */
118 map->handle = ret; /* to distinguish with other */
119 map->offset = cont_mem.phyaddress;
120 map->size = size;
121 map->mtrr = -1;
122 /*map-flags,type?? */
123
124 list = DRM(alloc) (sizeof(*list), DRM_MEM_MAPS);
125 if (!list) {
126 DRM(free) (map, sizeof(*map), DRM_MEM_MAPS);
127 return -EINVAL;
128 }
129 memset(list, 0, sizeof(*list));
130 list->map = map;
131
132 down(&dev->struct_sem);
133 list_add(&list->head, &dev->maplist->head);
134 up(&dev->struct_sem);
135
136 if (copy_to_user((drm_savage_alloc_cont_mem_t *) arg, &cont_mem, sizeof(cont_mem)))
137 return -EFAULT;
138
139 #warning "Race at the very least"
140 for (i = 0, p = virt_to_page(ret); i < size / PAGE_SIZE; i++, p++)
141 atomic_set(&p->count, 1);
142
143 return 1; /*success */
144 }
145
savage_get_physics_address(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)146 int savage_get_physics_address(struct inode *inode, struct file *filp,
147 unsigned int cmd, unsigned long arg)
148 {
149
150 drm_savage_get_physcis_address_t req;
151 unsigned long buf;
152 pgd_t *pgd;
153 pmd_t *pmd;
154 pte_t *pte;
155 struct mm_struct *mm;
156
157 if (copy_from_user(&req, (drm_savage_get_physcis_address_t *) arg, sizeof(req)))
158 return -EFAULT;
159 buf = req.v_address;
160
161 #warning "FIXME: need to redo logic for this"
162 /*What kind of virtual address ? */
163 if (buf >= (unsigned long) high_memory)
164 mm = &init_mm;
165 else
166 mm = current->mm;
167
168 spin_lock(&mm->page_table_lock);
169
170 pgd = pgd_offset(mm, buf);
171 pmd = pmd_offset(pgd, buf);
172 pte = pte_offset(pmd, buf);
173
174 if (!pte_present(*pte))
175 {
176 spin_unlock(&mm->page_table_lock);
177 return -EINVAL;
178 }
179 req.p_address = ((pte_val(*pte) & PAGE_MASK) | (buf & (PAGE_SIZE - 1)));
180 spin_unlock(&mm->page_table_lock);
181
182 if (copy_to_user((drm_savage_get_physcis_address_t *) arg, &req, sizeof(req)))
183 return -EFAULT;
184 return 1;
185 }
186
187 /*free the continuous memory*/
savage_free_cont_mem(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)188 int savage_free_cont_mem(struct inode *inode, struct file *filp,
189 unsigned int cmd, unsigned long arg)
190 {
191 drm_savage_alloc_cont_mem_t cont_mem;
192 unsigned long size;
193
194 /*map list */
195 drm_file_t *priv = filp->private_data;
196 drm_device_t *dev = priv->dev;
197 drm_map_t *map;
198 struct list_head *list;
199 drm_map_list_t *r_list = NULL;
200
201 if (copy_from_user(&cont_mem, (drm_savage_alloc_cont_mem_t *) arg, sizeof(cont_mem)))
202 return -EFAULT;
203 #warning "fix size overflow check"
204 size = cont_mem.type * cont_mem.size;
205 if (size <= 0)
206 return -EINVAL;
207
208 /* find the map in the list */
209 list_for_each(list, &dev->maplist->head) {
210 r_list = (drm_map_list_t *) list;
211
212 if (r_list->map && r_list->map->offset == cont_mem.phyaddress)
213 break;
214 }
215 /*find none */
216 if (list == (&dev->maplist->head)) {
217 up(&dev->struct_sem);
218 return -EINVAL;
219 }
220 map = r_list->map;
221 list_del(list);
222 DRM(free) (list, sizeof(*list), DRM_MEM_MAPS);
223
224 /*unmap the user space */
225 if (do_munmap(current->mm, cont_mem.linear, size) != 0)
226 return -EFAULT;
227 /*free the page */
228 pci_free_consistent(NULL, size, map->handle, cont_mem.phyaddress);
229
230 return 1;
231 }
232
233
234 #include "drm_agpsupport.h"
235 #include "drm_auth.h"
236 #include "drm_bufs.h"
237 #include "drm_context.h"
238 #include "drm_dma.h"
239 #include "drm_drawable.h"
240 #include "drm_drv.h"
241 #include "drm_fops.h"
242 #include "drm_init.h"
243 #include "drm_ioctl.h"
244 #include "drm_lock.h"
245 #include "drm_memory.h"
246 #include "drm_proc.h"
247 #include "drm_vm.h"
248 #include "drm_stub.h"
249