blob: 187eb84e4da5269527911556dd0a48335d2b9eae [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA,
3 * All Rights Reserved.
4 * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA,
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sub license,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27#include "drmP.h"
28
29#include "nouveau_drv.h"
30
31static struct vm_operations_struct nouveau_ttm_vm_ops;
32static const struct vm_operations_struct *ttm_vm_ops;
33
34static int
35nouveau_ttm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
36{
37 struct ttm_buffer_object *bo = vma->vm_private_data;
38 int ret;
39
40 if (unlikely(bo == NULL))
41 return VM_FAULT_NOPAGE;
42
43 ret = ttm_vm_ops->fault(vma, vmf);
44 return ret;
45}
46
47int
48nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
49{
50 struct drm_file *file_priv = filp->private_data;
51 struct drm_nouveau_private *dev_priv =
52 file_priv->minor->dev->dev_private;
53 int ret;
54
55 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
56 return drm_mmap(filp, vma);
57
58 ret = ttm_bo_mmap(filp, vma, &dev_priv->ttm.bdev);
59 if (unlikely(ret != 0))
60 return ret;
61
62 if (unlikely(ttm_vm_ops == NULL)) {
63 ttm_vm_ops = vma->vm_ops;
64 nouveau_ttm_vm_ops = *ttm_vm_ops;
65 nouveau_ttm_vm_ops.fault = &nouveau_ttm_fault;
66 }
67
68 vma->vm_ops = &nouveau_ttm_vm_ops;
69 return 0;
70}
71
72static int
73nouveau_ttm_mem_global_init(struct ttm_global_reference *ref)
74{
75 return ttm_mem_global_init(ref->object);
76}
77
78static void
79nouveau_ttm_mem_global_release(struct ttm_global_reference *ref)
80{
81 ttm_mem_global_release(ref->object);
82}
83
84int
85nouveau_ttm_global_init(struct drm_nouveau_private *dev_priv)
86{
87 struct ttm_global_reference *global_ref;
88 int ret;
89
90 global_ref = &dev_priv->ttm.mem_global_ref;
91 global_ref->global_type = TTM_GLOBAL_TTM_MEM;
92 global_ref->size = sizeof(struct ttm_mem_global);
93 global_ref->init = &nouveau_ttm_mem_global_init;
94 global_ref->release = &nouveau_ttm_mem_global_release;
95
96 ret = ttm_global_item_ref(global_ref);
97 if (unlikely(ret != 0)) {
98 DRM_ERROR("Failed setting up TTM memory accounting\n");
99 dev_priv->ttm.mem_global_ref.release = NULL;
100 return ret;
101 }
102
103 dev_priv->ttm.bo_global_ref.mem_glob = global_ref->object;
104 global_ref = &dev_priv->ttm.bo_global_ref.ref;
105 global_ref->global_type = TTM_GLOBAL_TTM_BO;
106 global_ref->size = sizeof(struct ttm_bo_global);
107 global_ref->init = &ttm_bo_global_init;
108 global_ref->release = &ttm_bo_global_release;
109
110 ret = ttm_global_item_ref(global_ref);
111 if (unlikely(ret != 0)) {
112 DRM_ERROR("Failed setting up TTM BO subsystem\n");
113 ttm_global_item_unref(&dev_priv->ttm.mem_global_ref);
114 dev_priv->ttm.mem_global_ref.release = NULL;
115 return ret;
116 }
117
118 return 0;
119}
120
121void
122nouveau_ttm_global_release(struct drm_nouveau_private *dev_priv)
123{
124 if (dev_priv->ttm.mem_global_ref.release == NULL)
125 return;
126
127 ttm_global_item_unref(&dev_priv->ttm.bo_global_ref.ref);
128 ttm_global_item_unref(&dev_priv->ttm.mem_global_ref);
129 dev_priv->ttm.mem_global_ref.release = NULL;
130}
131