Rev 4392 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4280 | Serge | 1 | /* |
2 | * Copyright © 2008-2012 Intel Corporation |
||
3 | * |
||
4 | * Permission is hereby granted, free of charge, to any person obtaining a |
||
5 | * copy of this software and associated documentation files (the "Software"), |
||
6 | * to deal in the Software without restriction, including without limitation |
||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||
8 | * and/or sell copies of the Software, and to permit persons to whom the |
||
9 | * Software is furnished to do so, subject to the following conditions: |
||
10 | * |
||
11 | * The above copyright notice and this permission notice (including the next |
||
12 | * paragraph) shall be included in all copies or substantial portions of the |
||
13 | * Software. |
||
14 | * |
||
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
||
21 | * IN THE SOFTWARE. |
||
22 | * |
||
23 | * Authors: |
||
24 | * Eric Anholt |
||
25 | * Chris Wilson |
||
26 | * |
||
27 | */ |
||
28 | |||
29 | #include |
||
30 | #include |
||
31 | #include "i915_drv.h" |
||
32 | |||
33 | static struct sg_table * |
||
34 | i915_pages_create_for_fb(struct drm_device *dev, |
||
35 | u32 offset, u32 size) |
||
36 | { |
||
37 | struct drm_i915_private *dev_priv = dev->dev_private; |
||
38 | struct sg_table *st; |
||
39 | struct scatterlist *sg; |
||
40 | addr_t fb_pages; |
||
41 | |||
42 | DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size); |
||
43 | // BUG_ON(offset > dev_priv->gtt.stolen_size - size); |
||
44 | |||
45 | /* We hide that we have no struct page backing our stolen object |
||
46 | * by wrapping the contiguous physical allocation with a fake |
||
47 | * dma mapping in a single scatterlist. |
||
48 | */ |
||
49 | |||
50 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
||
51 | if (st == NULL) |
||
52 | return NULL; |
||
53 | |||
54 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
||
55 | kfree(st); |
||
56 | return NULL; |
||
57 | } |
||
58 | |||
59 | fb_pages = AllocPages(size/PAGE_SIZE); |
||
60 | if(fb_pages == 0) |
||
61 | { |
||
62 | kfree(st); |
||
63 | return NULL; |
||
64 | }; |
||
65 | |||
66 | sg = st->sgl; |
||
67 | sg->offset = offset; |
||
68 | sg->length = size; |
||
69 | |||
70 | sg_dma_address(sg) = (dma_addr_t)fb_pages; |
||
71 | sg_dma_len(sg) = size; |
||
72 | |||
73 | return st; |
||
74 | } |
||
75 | |||
76 | |||
77 | static int kos_fb_object_get_pages(struct drm_i915_gem_object *obj) |
||
78 | { |
||
79 | BUG(); |
||
80 | return -EINVAL; |
||
81 | } |
||
82 | |||
83 | static void kos_fb_object_put_pages(struct drm_i915_gem_object *obj) |
||
84 | { |
||
85 | /* Should only be called during free */ |
||
86 | sg_free_table(obj->pages); |
||
87 | kfree(obj->pages); |
||
88 | } |
||
89 | |||
90 | static const struct drm_i915_gem_object_ops kos_fb_object_ops = { |
||
91 | .get_pages = kos_fb_object_get_pages, |
||
92 | .put_pages = kos_fb_object_put_pages, |
||
93 | }; |
||
94 | |||
95 | static struct drm_i915_gem_object * |
||
96 | _kos_fb_object_create(struct drm_device *dev, |
||
97 | struct drm_mm_node *fb_node) |
||
98 | { |
||
99 | struct drm_i915_gem_object *obj; |
||
100 | |||
101 | obj = i915_gem_object_alloc(dev); |
||
102 | if (obj == NULL) |
||
103 | return NULL; |
||
104 | |||
105 | drm_gem_private_object_init(dev, &obj->base, fb_node->size); |
||
106 | i915_gem_object_init(obj, &kos_fb_object_ops); |
||
107 | |||
108 | obj->pages = i915_pages_create_for_fb(dev, |
||
109 | fb_node->start, fb_node->size); |
||
110 | if (obj->pages == NULL) |
||
111 | goto cleanup; |
||
112 | |||
113 | obj->has_dma_mapping = true; |
||
114 | i915_gem_object_pin_pages(obj); |
||
115 | obj->stolen = fb_node; |
||
116 | |||
117 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
||
118 | obj->cache_level = I915_CACHE_NONE; |
||
119 | obj->tiling_mode = I915_TILING_X; |
||
120 | |||
121 | return obj; |
||
122 | |||
123 | cleanup: |
||
124 | i915_gem_object_free(obj); |
||
125 | return NULL; |
||
126 | } |
||
127 | |||
128 | |||
129 | struct drm_i915_gem_object * |
||
130 | kos_gem_fb_object_create(struct drm_device *dev, |
||
131 | u32 gtt_offset, |
||
132 | u32 size) |
||
133 | { |
||
134 | struct drm_i915_private *dev_priv = dev->dev_private; |
||
135 | struct i915_address_space *ggtt = &dev_priv->gtt.base; |
||
136 | struct drm_i915_gem_object *obj; |
||
137 | struct drm_mm_node *fb_node; |
||
138 | struct i915_vma *vma; |
||
139 | int ret; |
||
140 | |||
141 | DRM_DEBUG_KMS("creating preallocated framebuffer object: gtt_offset=%x, size=%x\n", |
||
142 | gtt_offset, size); |
||
143 | |||
144 | /* KISS and expect everything to be page-aligned */ |
||
145 | BUG_ON(size & 4095); |
||
146 | |||
147 | if (WARN_ON(size == 0)) |
||
148 | return NULL; |
||
149 | |||
150 | fb_node = kzalloc(sizeof(*fb_node), GFP_KERNEL); |
||
151 | if (!fb_node) |
||
152 | return NULL; |
||
153 | |||
154 | fb_node->start = gtt_offset; |
||
155 | fb_node->size = size; |
||
156 | |||
157 | obj = _kos_fb_object_create(dev, fb_node); |
||
158 | if (obj == NULL) { |
||
159 | DRM_DEBUG_KMS("failed to preallocate framebuffer object\n"); |
||
160 | kfree(fb_node); |
||
161 | return NULL; |
||
162 | } |
||
163 | |||
4560 | Serge | 164 | vma = i915_gem_obj_lookup_or_create_vma(obj, ggtt); |
4280 | Serge | 165 | if (IS_ERR(vma)) { |
166 | ret = PTR_ERR(vma); |
||
167 | goto err_out; |
||
168 | } |
||
169 | |||
170 | /* To simplify the initialisation sequence between KMS and GTT, |
||
171 | * we allow construction of the stolen object prior to |
||
172 | * setting up the GTT space. The actual reservation will occur |
||
173 | * later. |
||
174 | */ |
||
175 | vma->node.start = gtt_offset; |
||
176 | vma->node.size = size; |
||
177 | if (drm_mm_initialized(&ggtt->mm)) { |
||
178 | ret = drm_mm_reserve_node(&ggtt->mm, &vma->node); |
||
179 | if (ret) { |
||
180 | DRM_DEBUG_KMS("failed to allocate framebuffer GTT space\n"); |
||
181 | goto err_vma; |
||
182 | } |
||
183 | } |
||
184 | |||
4392 | Serge | 185 | // obj->has_global_gtt_mapping = 1; |
4280 | Serge | 186 | |
187 | list_add_tail(&obj->global_list, &dev_priv->mm.bound_list); |
||
188 | list_add_tail(&vma->mm_list, &ggtt->inactive_list); |
||
189 | |||
4371 | Serge | 190 | mutex_lock(&dev->object_name_lock); |
191 | idr_preload(GFP_KERNEL); |
||
192 | |||
193 | if (!obj->base.name) { |
||
194 | ret = idr_alloc(&dev->object_name_idr, &obj->base, 1, 0, GFP_NOWAIT); |
||
195 | if (ret < 0) |
||
196 | goto err_gem; |
||
197 | |||
198 | obj->base.name = ret; |
||
199 | |||
200 | /* Allocate a reference for the name table. */ |
||
201 | drm_gem_object_reference(&obj->base); |
||
202 | |||
4389 | Serge | 203 | DRM_DEBUG_KMS("%s allocate fb name %d\n", __FUNCTION__, obj->base.name ); |
4371 | Serge | 204 | } |
205 | |||
206 | idr_preload_end(); |
||
207 | mutex_unlock(&dev->object_name_lock); |
||
208 | drm_gem_object_unreference(&obj->base); |
||
4280 | Serge | 209 | return obj; |
210 | |||
4371 | Serge | 211 | err_gem: |
212 | idr_preload_end(); |
||
213 | mutex_unlock(&dev->object_name_lock); |
||
4280 | Serge | 214 | err_vma: |
215 | i915_gem_vma_destroy(vma); |
||
216 | err_out: |
||
217 | kfree(fb_node); |
||
218 | drm_gem_object_unreference(&obj->base); |
||
219 | return NULL; |
||
220 | }> |
||
221 |