Commit | Line | Data |
---|---|---|
6ee73861 BS |
1 | /* |
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 | ||
ebb945a9 BS |
27 | #include <subdev/fb.h> |
28 | #include <subdev/vm.h> | |
29 | #include <subdev/instmem.h> | |
6ee73861 | 30 | |
ebb945a9 BS |
31 | #include "nouveau_drm.h" |
32 | #include "nouveau_ttm.h" | |
33 | #include "nouveau_gem.h" | |
6ee73861 | 34 | |
bc9e7b9a BS |
35 | static int |
36 | nouveau_vram_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) | |
37 | { | |
897a6e27 MS |
38 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
39 | struct nouveau_fb *pfb = nouveau_fb(drm->device); | |
40 | man->priv = pfb; | |
bc9e7b9a BS |
41 | return 0; |
42 | } | |
43 | ||
44 | static int | |
45 | nouveau_vram_manager_fini(struct ttm_mem_type_manager *man) | |
46 | { | |
897a6e27 | 47 | man->priv = NULL; |
bc9e7b9a BS |
48 | return 0; |
49 | } | |
50 | ||
51 | static inline void | |
52 | nouveau_mem_node_cleanup(struct nouveau_mem *node) | |
53 | { | |
54 | if (node->vma[0].node) { | |
55 | nouveau_vm_unmap(&node->vma[0]); | |
56 | nouveau_vm_put(&node->vma[0]); | |
57 | } | |
58 | ||
59 | if (node->vma[1].node) { | |
60 | nouveau_vm_unmap(&node->vma[1]); | |
61 | nouveau_vm_put(&node->vma[1]); | |
62 | } | |
63 | } | |
64 | ||
65 | static void | |
66 | nouveau_vram_manager_del(struct ttm_mem_type_manager *man, | |
67 | struct ttm_mem_reg *mem) | |
68 | { | |
ebb945a9 BS |
69 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
70 | struct nouveau_fb *pfb = nouveau_fb(drm->device); | |
bc9e7b9a | 71 | nouveau_mem_node_cleanup(mem->mm_node); |
dceef5d8 | 72 | pfb->ram->put(pfb, (struct nouveau_mem **)&mem->mm_node); |
bc9e7b9a BS |
73 | } |
74 | ||
75 | static int | |
76 | nouveau_vram_manager_new(struct ttm_mem_type_manager *man, | |
77 | struct ttm_buffer_object *bo, | |
78 | struct ttm_placement *placement, | |
e3f20279 | 79 | uint32_t flags, |
bc9e7b9a BS |
80 | struct ttm_mem_reg *mem) |
81 | { | |
ebb945a9 BS |
82 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
83 | struct nouveau_fb *pfb = nouveau_fb(drm->device); | |
bc9e7b9a BS |
84 | struct nouveau_bo *nvbo = nouveau_bo(bo); |
85 | struct nouveau_mem *node; | |
86 | u32 size_nc = 0; | |
87 | int ret; | |
88 | ||
89 | if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) | |
90 | size_nc = 1 << nvbo->page_shift; | |
91 | ||
dceef5d8 | 92 | ret = pfb->ram->get(pfb, mem->num_pages << PAGE_SHIFT, |
ebb945a9 BS |
93 | mem->page_alignment << PAGE_SHIFT, size_nc, |
94 | (nvbo->tile_flags >> 8) & 0x3ff, &node); | |
bc9e7b9a BS |
95 | if (ret) { |
96 | mem->mm_node = NULL; | |
97 | return (ret == -ENOSPC) ? 0 : ret; | |
98 | } | |
99 | ||
100 | node->page_shift = nvbo->page_shift; | |
101 | ||
102 | mem->mm_node = node; | |
103 | mem->start = node->offset >> PAGE_SHIFT; | |
104 | return 0; | |
105 | } | |
106 | ||
5b8a43ae | 107 | static void |
bc9e7b9a BS |
108 | nouveau_vram_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
109 | { | |
897a6e27 MS |
110 | struct nouveau_fb *pfb = man->priv; |
111 | struct nouveau_mm *mm = &pfb->vram; | |
bc9e7b9a BS |
112 | struct nouveau_mm_node *r; |
113 | u32 total = 0, free = 0; | |
114 | ||
51a506c0 | 115 | mutex_lock(&nv_subdev(pfb)->mutex); |
bc9e7b9a BS |
116 | list_for_each_entry(r, &mm->nodes, nl_entry) { |
117 | printk(KERN_DEBUG "%s %d: 0x%010llx 0x%010llx\n", | |
118 | prefix, r->type, ((u64)r->offset << 12), | |
119 | (((u64)r->offset + r->length) << 12)); | |
120 | ||
121 | total += r->length; | |
122 | if (!r->type) | |
123 | free += r->length; | |
124 | } | |
51a506c0 | 125 | mutex_unlock(&nv_subdev(pfb)->mutex); |
bc9e7b9a BS |
126 | |
127 | printk(KERN_DEBUG "%s total: 0x%010llx free: 0x%010llx\n", | |
128 | prefix, (u64)total << 12, (u64)free << 12); | |
129 | printk(KERN_DEBUG "%s block: 0x%08x\n", | |
130 | prefix, mm->block_size << 12); | |
131 | } | |
132 | ||
133 | const struct ttm_mem_type_manager_func nouveau_vram_manager = { | |
134 | nouveau_vram_manager_init, | |
135 | nouveau_vram_manager_fini, | |
136 | nouveau_vram_manager_new, | |
137 | nouveau_vram_manager_del, | |
138 | nouveau_vram_manager_debug | |
139 | }; | |
140 | ||
141 | static int | |
142 | nouveau_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) | |
143 | { | |
144 | return 0; | |
145 | } | |
146 | ||
147 | static int | |
148 | nouveau_gart_manager_fini(struct ttm_mem_type_manager *man) | |
149 | { | |
150 | return 0; | |
151 | } | |
152 | ||
153 | static void | |
154 | nouveau_gart_manager_del(struct ttm_mem_type_manager *man, | |
155 | struct ttm_mem_reg *mem) | |
156 | { | |
157 | nouveau_mem_node_cleanup(mem->mm_node); | |
158 | kfree(mem->mm_node); | |
159 | mem->mm_node = NULL; | |
160 | } | |
161 | ||
162 | static int | |
163 | nouveau_gart_manager_new(struct ttm_mem_type_manager *man, | |
164 | struct ttm_buffer_object *bo, | |
165 | struct ttm_placement *placement, | |
e3f20279 | 166 | uint32_t flags, |
bc9e7b9a BS |
167 | struct ttm_mem_reg *mem) |
168 | { | |
de7b7d59 BS |
169 | struct nouveau_drm *drm = nouveau_bdev(bo->bdev); |
170 | struct nouveau_bo *nvbo = nouveau_bo(bo); | |
bc9e7b9a BS |
171 | struct nouveau_mem *node; |
172 | ||
bc9e7b9a BS |
173 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
174 | if (!node) | |
175 | return -ENOMEM; | |
2e2cfbe6 | 176 | |
bc9e7b9a BS |
177 | node->page_shift = 12; |
178 | ||
de7b7d59 BS |
179 | switch (nv_device(drm->device)->card_type) { |
180 | case NV_50: | |
181 | if (nv_device(drm->device)->chipset != 0x50) | |
182 | node->memtype = (nvbo->tile_flags & 0x7f00) >> 8; | |
183 | break; | |
184 | case NV_C0: | |
de7b7d59 BS |
185 | case NV_E0: |
186 | node->memtype = (nvbo->tile_flags & 0xff00) >> 8; | |
187 | break; | |
188 | default: | |
189 | break; | |
190 | } | |
191 | ||
bc9e7b9a BS |
192 | mem->mm_node = node; |
193 | mem->start = 0; | |
194 | return 0; | |
195 | } | |
196 | ||
5b8a43ae | 197 | static void |
bc9e7b9a BS |
198 | nouveau_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
199 | { | |
200 | } | |
201 | ||
202 | const struct ttm_mem_type_manager_func nouveau_gart_manager = { | |
203 | nouveau_gart_manager_init, | |
204 | nouveau_gart_manager_fini, | |
205 | nouveau_gart_manager_new, | |
206 | nouveau_gart_manager_del, | |
207 | nouveau_gart_manager_debug | |
208 | }; | |
209 | ||
ebb945a9 | 210 | #include <core/subdev/vm/nv04.h> |
bc9e7b9a BS |
211 | static int |
212 | nv04_gart_manager_init(struct ttm_mem_type_manager *man, unsigned long psize) | |
213 | { | |
ebb945a9 BS |
214 | struct nouveau_drm *drm = nouveau_bdev(man->bdev); |
215 | struct nouveau_vmmgr *vmm = nouveau_vmmgr(drm->device); | |
216 | struct nv04_vmmgr_priv *priv = (void *)vmm; | |
217 | struct nouveau_vm *vm = NULL; | |
218 | nouveau_vm_ref(priv->vm, &vm, NULL); | |
219 | man->priv = vm; | |
220 | return 0; | |
bc9e7b9a BS |
221 | } |
222 | ||
223 | static int | |
224 | nv04_gart_manager_fini(struct ttm_mem_type_manager *man) | |
225 | { | |
226 | struct nouveau_vm *vm = man->priv; | |
227 | nouveau_vm_ref(NULL, &vm, NULL); | |
228 | man->priv = NULL; | |
229 | return 0; | |
230 | } | |
231 | ||
232 | static void | |
233 | nv04_gart_manager_del(struct ttm_mem_type_manager *man, struct ttm_mem_reg *mem) | |
234 | { | |
235 | struct nouveau_mem *node = mem->mm_node; | |
236 | if (node->vma[0].node) | |
237 | nouveau_vm_put(&node->vma[0]); | |
238 | kfree(mem->mm_node); | |
239 | mem->mm_node = NULL; | |
240 | } | |
241 | ||
242 | static int | |
243 | nv04_gart_manager_new(struct ttm_mem_type_manager *man, | |
244 | struct ttm_buffer_object *bo, | |
245 | struct ttm_placement *placement, | |
e3f20279 | 246 | uint32_t flags, |
bc9e7b9a BS |
247 | struct ttm_mem_reg *mem) |
248 | { | |
249 | struct nouveau_mem *node; | |
250 | int ret; | |
251 | ||
252 | node = kzalloc(sizeof(*node), GFP_KERNEL); | |
253 | if (!node) | |
254 | return -ENOMEM; | |
255 | ||
256 | node->page_shift = 12; | |
257 | ||
258 | ret = nouveau_vm_get(man->priv, mem->num_pages << 12, node->page_shift, | |
259 | NV_MEM_ACCESS_RW, &node->vma[0]); | |
260 | if (ret) { | |
261 | kfree(node); | |
262 | return ret; | |
263 | } | |
264 | ||
265 | mem->mm_node = node; | |
266 | mem->start = node->vma[0].offset >> PAGE_SHIFT; | |
267 | return 0; | |
268 | } | |
269 | ||
5b8a43ae | 270 | static void |
bc9e7b9a BS |
271 | nv04_gart_manager_debug(struct ttm_mem_type_manager *man, const char *prefix) |
272 | { | |
273 | } | |
274 | ||
275 | const struct ttm_mem_type_manager_func nv04_gart_manager = { | |
276 | nv04_gart_manager_init, | |
277 | nv04_gart_manager_fini, | |
278 | nv04_gart_manager_new, | |
279 | nv04_gart_manager_del, | |
280 | nv04_gart_manager_debug | |
281 | }; | |
282 | ||
6ee73861 BS |
283 | int |
284 | nouveau_ttm_mmap(struct file *filp, struct vm_area_struct *vma) | |
285 | { | |
286 | struct drm_file *file_priv = filp->private_data; | |
77145f1c | 287 | struct nouveau_drm *drm = nouveau_drm(file_priv->minor->dev); |
6ee73861 BS |
288 | |
289 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) | |
290 | return drm_mmap(filp, vma); | |
291 | ||
ebb945a9 | 292 | return ttm_bo_mmap(filp, vma, &drm->ttm.bdev); |
6ee73861 BS |
293 | } |
294 | ||
295 | static int | |
ba4420c2 | 296 | nouveau_ttm_mem_global_init(struct drm_global_reference *ref) |
6ee73861 BS |
297 | { |
298 | return ttm_mem_global_init(ref->object); | |
299 | } | |
300 | ||
301 | static void | |
ba4420c2 | 302 | nouveau_ttm_mem_global_release(struct drm_global_reference *ref) |
6ee73861 BS |
303 | { |
304 | ttm_mem_global_release(ref->object); | |
305 | } | |
306 | ||
307 | int | |
ebb945a9 | 308 | nouveau_ttm_global_init(struct nouveau_drm *drm) |
6ee73861 | 309 | { |
ba4420c2 | 310 | struct drm_global_reference *global_ref; |
6ee73861 BS |
311 | int ret; |
312 | ||
ebb945a9 | 313 | global_ref = &drm->ttm.mem_global_ref; |
ba4420c2 | 314 | global_ref->global_type = DRM_GLOBAL_TTM_MEM; |
6ee73861 BS |
315 | global_ref->size = sizeof(struct ttm_mem_global); |
316 | global_ref->init = &nouveau_ttm_mem_global_init; | |
317 | global_ref->release = &nouveau_ttm_mem_global_release; | |
318 | ||
ba4420c2 | 319 | ret = drm_global_item_ref(global_ref); |
6ee73861 BS |
320 | if (unlikely(ret != 0)) { |
321 | DRM_ERROR("Failed setting up TTM memory accounting\n"); | |
ebb945a9 | 322 | drm->ttm.mem_global_ref.release = NULL; |
6ee73861 BS |
323 | return ret; |
324 | } | |
325 | ||
ebb945a9 BS |
326 | drm->ttm.bo_global_ref.mem_glob = global_ref->object; |
327 | global_ref = &drm->ttm.bo_global_ref.ref; | |
ba4420c2 | 328 | global_ref->global_type = DRM_GLOBAL_TTM_BO; |
6ee73861 BS |
329 | global_ref->size = sizeof(struct ttm_bo_global); |
330 | global_ref->init = &ttm_bo_global_init; | |
331 | global_ref->release = &ttm_bo_global_release; | |
332 | ||
ba4420c2 | 333 | ret = drm_global_item_ref(global_ref); |
6ee73861 BS |
334 | if (unlikely(ret != 0)) { |
335 | DRM_ERROR("Failed setting up TTM BO subsystem\n"); | |
ebb945a9 BS |
336 | drm_global_item_unref(&drm->ttm.mem_global_ref); |
337 | drm->ttm.mem_global_ref.release = NULL; | |
6ee73861 BS |
338 | return ret; |
339 | } | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
344 | void | |
ebb945a9 | 345 | nouveau_ttm_global_release(struct nouveau_drm *drm) |
6ee73861 | 346 | { |
ebb945a9 | 347 | if (drm->ttm.mem_global_ref.release == NULL) |
6ee73861 BS |
348 | return; |
349 | ||
ebb945a9 BS |
350 | drm_global_item_unref(&drm->ttm.bo_global_ref.ref); |
351 | drm_global_item_unref(&drm->ttm.mem_global_ref); | |
352 | drm->ttm.mem_global_ref.release = NULL; | |
353 | } | |
354 | ||
355 | int | |
356 | nouveau_ttm_init(struct nouveau_drm *drm) | |
357 | { | |
358 | struct drm_device *dev = drm->dev; | |
420b9469 | 359 | struct nouveau_device *device = nv_device(drm->device); |
ebb945a9 BS |
360 | u32 bits; |
361 | int ret; | |
362 | ||
dc73b45a | 363 | bits = nouveau_vmmgr(drm->device)->dma_bits; |
420b9469 AC |
364 | if (nv_device_is_pci(device)) { |
365 | if (drm->agp.stat == ENABLED || | |
366 | !pci_dma_supported(dev->pdev, DMA_BIT_MASK(bits))) | |
367 | bits = 32; | |
368 | ||
369 | ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(bits)); | |
370 | if (ret) | |
371 | return ret; | |
372 | ||
373 | ret = pci_set_consistent_dma_mask(dev->pdev, | |
374 | DMA_BIT_MASK(bits)); | |
375 | if (ret) | |
376 | pci_set_consistent_dma_mask(dev->pdev, | |
377 | DMA_BIT_MASK(32)); | |
378 | } | |
ebb945a9 BS |
379 | |
380 | ret = nouveau_ttm_global_init(drm); | |
381 | if (ret) | |
382 | return ret; | |
383 | ||
384 | ret = ttm_bo_device_init(&drm->ttm.bdev, | |
385 | drm->ttm.bo_global_ref.ref.object, | |
44d847b7 DH |
386 | &nouveau_bo_driver, |
387 | dev->anon_inode->i_mapping, | |
388 | DRM_FILE_PAGE_OFFSET, | |
ebb945a9 BS |
389 | bits <= 32 ? true : false); |
390 | if (ret) { | |
391 | NV_ERROR(drm, "error initialising bo driver, %d\n", ret); | |
392 | return ret; | |
393 | } | |
394 | ||
395 | /* VRAM init */ | |
dceef5d8 | 396 | drm->gem.vram_available = nouveau_fb(drm->device)->ram->size; |
ebb945a9 BS |
397 | drm->gem.vram_available -= nouveau_instmem(drm->device)->reserved; |
398 | ||
399 | ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_VRAM, | |
400 | drm->gem.vram_available >> PAGE_SHIFT); | |
401 | if (ret) { | |
402 | NV_ERROR(drm, "VRAM mm init failed, %d\n", ret); | |
403 | return ret; | |
404 | } | |
405 | ||
420b9469 AC |
406 | drm->ttm.mtrr = arch_phys_wc_add(nv_device_resource_start(device, 1), |
407 | nv_device_resource_len(device, 1)); | |
ebb945a9 BS |
408 | |
409 | /* GART init */ | |
410 | if (drm->agp.stat != ENABLED) { | |
411 | drm->gem.gart_available = nouveau_vmmgr(drm->device)->limit; | |
ebb945a9 BS |
412 | } else { |
413 | drm->gem.gart_available = drm->agp.size; | |
414 | } | |
415 | ||
416 | ret = ttm_bo_init_mm(&drm->ttm.bdev, TTM_PL_TT, | |
417 | drm->gem.gart_available >> PAGE_SHIFT); | |
418 | if (ret) { | |
419 | NV_ERROR(drm, "GART mm init failed, %d\n", ret); | |
420 | return ret; | |
421 | } | |
422 | ||
423 | NV_INFO(drm, "VRAM: %d MiB\n", (u32)(drm->gem.vram_available >> 20)); | |
424 | NV_INFO(drm, "GART: %d MiB\n", (u32)(drm->gem.gart_available >> 20)); | |
425 | return 0; | |
426 | } | |
427 | ||
428 | void | |
429 | nouveau_ttm_fini(struct nouveau_drm *drm) | |
430 | { | |
431 | mutex_lock(&drm->dev->struct_mutex); | |
432 | ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_VRAM); | |
433 | ttm_bo_clean_mm(&drm->ttm.bdev, TTM_PL_TT); | |
434 | mutex_unlock(&drm->dev->struct_mutex); | |
435 | ||
436 | ttm_bo_device_release(&drm->ttm.bdev); | |
437 | ||
438 | nouveau_ttm_global_release(drm); | |
439 | ||
247d36d7 AL |
440 | arch_phys_wc_del(drm->ttm.mtrr); |
441 | drm->ttm.mtrr = 0; | |
6ee73861 | 442 | } |