1 /**************************************************************************
2 * Copyright (c) 2008, Intel Corporation.
3 * All Rights Reserved.
4 * Copyright (c) 2008, Tungsten Graphics Inc. Cedar Park, TX., USA.
5 * All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 **************************************************************************/
21
22
23 #include <drm/drmP.h>
24 #include "psb_drv.h"
25 #include "psb_ttm_userobj_api.h"
26 #include <linux/io.h>
27
28
29 static struct vm_operations_struct psb_ttm_vm_ops;
30
31 /**
32 * NOTE: driver_private of drm_file is now a struct psb_file_data struct
33 * pPriv in struct psb_file_data contains the original psb_fpriv;
34 */
psb_open(struct inode * inode,struct file * filp)35 int psb_open(struct inode *inode, struct file *filp)
36 {
37 struct drm_file *file_priv;
38 struct drm_psb_private *dev_priv;
39 struct psb_fpriv *psb_fp;
40 struct psb_file_data *pvr_file_priv;
41 int ret;
42
43 DRM_DEBUG("\n");
44
45 ret = drm_open(inode, filp);
46 if (unlikely(ret))
47 return ret;
48
49 psb_fp = kzalloc(sizeof(*psb_fp), GFP_KERNEL);
50
51 if (unlikely(psb_fp == NULL))
52 goto out_err0;
53
54 file_priv = (struct drm_file *) filp->private_data;
55 dev_priv = psb_priv(file_priv->minor->dev);
56
57 DRM_DEBUG("is_master %d\n", file_priv->is_master ? 1 : 0);
58
59 psb_fp->tfile = ttm_object_file_init(dev_priv->tdev,
60 PSB_FILE_OBJECT_HASH_ORDER);
61 if (unlikely(psb_fp->tfile == NULL))
62 goto out_err1;
63
64 pvr_file_priv = (struct psb_file_data *)file_priv->driver_priv;
65 if (!pvr_file_priv) {
66 DRM_ERROR("drm file private is NULL\n");
67 goto out_err1;
68 }
69
70 pvr_file_priv->priv = psb_fp;
71 if (unlikely(dev_priv->bdev.dev_mapping == NULL))
72 dev_priv->bdev.dev_mapping = dev_priv->dev->dev_mapping;
73
74 return 0;
75
76 out_err1:
77 kfree(psb_fp);
78 out_err0:
79 (void) drm_release(inode, filp);
80 return ret;
81 }
82
psb_release(struct inode * inode,struct file * filp)83 int psb_release(struct inode *inode, struct file *filp)
84 {
85 struct drm_file *file_priv;
86 struct psb_fpriv *psb_fp;
87 struct drm_psb_private *dev_priv;
88 int ret;
89 file_priv = (struct drm_file *) filp->private_data;
90 psb_fp = psb_fpriv(file_priv);
91 dev_priv = psb_priv(file_priv->minor->dev);
92
93 ttm_object_file_release(&psb_fp->tfile);
94 kfree(psb_fp);
95
96 ret = drm_release(inode, filp);
97
98 return ret;
99 }
100
psb_fence_signaled_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)101 int psb_fence_signaled_ioctl(struct drm_device *dev, void *data,
102 struct drm_file *file_priv)
103 {
104
105 return ttm_fence_signaled_ioctl(psb_fpriv(file_priv)->tfile, data);
106 }
107
psb_fence_finish_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)108 int psb_fence_finish_ioctl(struct drm_device *dev, void *data,
109 struct drm_file *file_priv)
110 {
111 return ttm_fence_finish_ioctl(psb_fpriv(file_priv)->tfile, data);
112 }
113
psb_fence_unref_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)114 int psb_fence_unref_ioctl(struct drm_device *dev, void *data,
115 struct drm_file *file_priv)
116 {
117 return ttm_fence_unref_ioctl(psb_fpriv(file_priv)->tfile, data);
118 }
119
psb_pl_waitidle_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)120 int psb_pl_waitidle_ioctl(struct drm_device *dev, void *data,
121 struct drm_file *file_priv)
122 {
123 return ttm_pl_waitidle_ioctl(psb_fpriv(file_priv)->tfile, data);
124 }
125
psb_pl_setstatus_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)126 int psb_pl_setstatus_ioctl(struct drm_device *dev, void *data,
127 struct drm_file *file_priv)
128 {
129 return ttm_pl_setstatus_ioctl(psb_fpriv(file_priv)->tfile,
130 &psb_priv(dev)->ttm_lock, data);
131
132 }
133
psb_pl_synccpu_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)134 int psb_pl_synccpu_ioctl(struct drm_device *dev, void *data,
135 struct drm_file *file_priv)
136 {
137 return ttm_pl_synccpu_ioctl(psb_fpriv(file_priv)->tfile, data);
138 }
139
psb_pl_unref_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)140 int psb_pl_unref_ioctl(struct drm_device *dev, void *data,
141 struct drm_file *file_priv)
142 {
143 return ttm_pl_unref_ioctl(psb_fpriv(file_priv)->tfile, data);
144
145 }
146
psb_pl_reference_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)147 int psb_pl_reference_ioctl(struct drm_device *dev, void *data,
148 struct drm_file *file_priv)
149 {
150 return ttm_pl_reference_ioctl(psb_fpriv(file_priv)->tfile, data);
151
152 }
153
psb_pl_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)154 int psb_pl_create_ioctl(struct drm_device *dev, void *data,
155 struct drm_file *file_priv)
156 {
157 struct drm_psb_private *dev_priv = psb_priv(dev);
158
159 return ttm_pl_create_ioctl(psb_fpriv(file_priv)->tfile,
160 &dev_priv->bdev, &dev_priv->ttm_lock, data);
161
162 }
163
psb_pl_ub_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)164 int psb_pl_ub_create_ioctl(struct drm_device *dev, void *data,
165 struct drm_file *file_priv)
166 {
167 struct drm_psb_private *dev_priv = psb_priv(dev);
168
169 return ttm_pl_ub_create_ioctl(psb_fpriv(file_priv)->tfile,
170 &dev_priv->bdev, &dev_priv->ttm_lock, data);
171
172 }
173 /**
174 * psb_ttm_fault - Wrapper around the ttm fault method.
175 *
176 * @vma: The struct vm_area_struct as in the vm fault() method.
177 * @vmf: The struct vm_fault as in the vm fault() method.
178 *
179 * Since ttm_fault() will reserve buffers while faulting,
180 * we need to take the ttm read lock around it, as this driver
181 * relies on the ttm_lock in write mode to exclude all threads from
182 * reserving and thus validating buffers in aperture- and memory shortage
183 * situations.
184 */
185
psb_ttm_fault(struct vm_area_struct * vma,struct vm_fault * vmf)186 static int psb_ttm_fault(struct vm_area_struct *vma,
187 struct vm_fault *vmf)
188 {
189 struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
190 vma->vm_private_data;
191 struct drm_psb_private *dev_priv =
192 container_of(bo->bdev, struct drm_psb_private, bdev);
193 int ret;
194
195 ret = ttm_read_lock(&dev_priv->ttm_lock, true);
196 if (unlikely(ret != 0))
197 return VM_FAULT_NOPAGE;
198
199 ret = dev_priv->ttm_vm_ops->fault(vma, vmf);
200
201 ttm_read_unlock(&dev_priv->ttm_lock);
202 return ret;
203 }
204
205 /**
206 * if vm_pgoff < DRM_PSB_FILE_PAGE_OFFSET call directly to
207 * PVRMMap
208 */
psb_mmap(struct file * filp,struct vm_area_struct * vma)209 int psb_mmap(struct file *filp, struct vm_area_struct *vma)
210 {
211 struct drm_file *file_priv;
212 struct drm_psb_private *dev_priv;
213 int ret;
214
215 if (vma->vm_pgoff < DRM_PSB_FILE_PAGE_OFFSET ||
216 vma->vm_pgoff > 2 * DRM_PSB_FILE_PAGE_OFFSET)
217 #if 0 /* FIXMEAC */
218 return PVRMMap(filp, vma);
219 #else
220 return -EINVAL;
221 #endif
222
223 file_priv = (struct drm_file *) filp->private_data;
224 dev_priv = psb_priv(file_priv->minor->dev);
225
226 ret = ttm_bo_mmap(filp, vma, &dev_priv->bdev);
227 if (unlikely(ret != 0))
228 return ret;
229
230 if (unlikely(dev_priv->ttm_vm_ops == NULL)) {
231 dev_priv->ttm_vm_ops = (struct vm_operations_struct *)
232 vma->vm_ops;
233 psb_ttm_vm_ops = *vma->vm_ops;
234 psb_ttm_vm_ops.fault = &psb_ttm_fault;
235 }
236
237 vma->vm_ops = &psb_ttm_vm_ops;
238
239 return 0;
240 }
241 /*
242 ssize_t psb_ttm_write(struct file *filp, const char __user *buf,
243 size_t count, loff_t *f_pos)
244 {
245 struct drm_file *file_priv = (struct drm_file *)filp->private_data;
246 struct drm_psb_private *dev_priv = psb_priv(file_priv->minor->dev);
247
248 return ttm_bo_io(&dev_priv->bdev, filp, buf, NULL, count, f_pos, 1);
249 }
250
251 ssize_t psb_ttm_read(struct file *filp, char __user *buf,
252 size_t count, loff_t *f_pos)
253 {
254 struct drm_file *file_priv = (struct drm_file *)filp->private_data;
255 struct drm_psb_private *dev_priv = psb_priv(file_priv->minor->dev);
256
257 return ttm_bo_io(&dev_priv->bdev, filp, NULL, buf, count, f_pos, 1);
258 }
259 */
psb_verify_access(struct ttm_buffer_object * bo,struct file * filp)260 int psb_verify_access(struct ttm_buffer_object *bo,
261 struct file *filp)
262 {
263 struct drm_file *file_priv = (struct drm_file *)filp->private_data;
264
265 if (capable(CAP_SYS_ADMIN))
266 return 0;
267
268 if (unlikely(!file_priv->authenticated))
269 return -EPERM;
270
271 return ttm_pl_verify_access(bo, psb_fpriv(file_priv)->tfile);
272 }
273
psb_ttm_mem_global_init(struct drm_global_reference * ref)274 static int psb_ttm_mem_global_init(struct drm_global_reference *ref)
275 {
276 return ttm_mem_global_init(ref->object);
277 }
278
psb_ttm_mem_global_release(struct drm_global_reference * ref)279 static void psb_ttm_mem_global_release(struct drm_global_reference *ref)
280 {
281 ttm_mem_global_release(ref->object);
282 }
283
psb_ttm_global_init(struct drm_psb_private * dev_priv)284 int psb_ttm_global_init(struct drm_psb_private *dev_priv)
285 {
286 struct drm_global_reference *global_ref;
287 int ret;
288
289 global_ref = &dev_priv->mem_global_ref;
290 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
291 global_ref->size = sizeof(struct ttm_mem_global);
292 global_ref->init = &psb_ttm_mem_global_init;
293 global_ref->release = &psb_ttm_mem_global_release;
294
295 ret = drm_global_item_ref(global_ref);
296 if (unlikely(ret != 0)) {
297 DRM_ERROR("Failed referencing a global TTM memory object.\n");
298 return ret;
299 }
300
301 dev_priv->bo_global_ref.mem_glob = dev_priv->mem_global_ref.object;
302 global_ref = &dev_priv->bo_global_ref.ref;
303 global_ref->global_type = DRM_GLOBAL_TTM_BO;
304 global_ref->size = sizeof(struct ttm_bo_global);
305 global_ref->init = &ttm_bo_global_init;
306 global_ref->release = &ttm_bo_global_release;
307 ret = drm_global_item_ref(global_ref);
308 if (ret != 0) {
309 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
310 drm_global_item_unref(global_ref);
311 return ret;
312 }
313 return 0;
314 }
315
psb_ttm_global_release(struct drm_psb_private * dev_priv)316 void psb_ttm_global_release(struct drm_psb_private *dev_priv)
317 {
318 drm_global_item_unref(&dev_priv->mem_global_ref);
319 }
320
psb_getpageaddrs_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)321 int psb_getpageaddrs_ioctl(struct drm_device *dev, void *data,
322 struct drm_file *file_priv)
323 {
324 struct drm_psb_getpageaddrs_arg *arg = data;
325 struct ttm_buffer_object *bo;
326 struct ttm_tt *ttm;
327 struct page **tt_pages;
328 unsigned long i, num_pages;
329 unsigned long *p = arg->page_addrs;
330 int ret = 0;
331
332 bo = ttm_buffer_object_lookup(psb_fpriv(file_priv)->tfile,
333 arg->handle);
334 if (unlikely(bo == NULL)) {
335 printk(KERN_ERR
336 "Could not find buffer object for getpageaddrs.\n");
337 return -EINVAL;
338 }
339
340 arg->gtt_offset = bo->offset;
341 ttm = bo->ttm;
342 num_pages = ttm->num_pages;
343 tt_pages = ttm->pages;
344
345 for (i = 0; i < num_pages; i++)
346 p[i] = (unsigned long)page_to_phys(tt_pages[i]);
347
348 return ret;
349 }
350