2 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, TX., USA,
4 * Copyright (c) 2009 VMware, Inc., Palo Alto, CA., USA,
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 #include "nouveau_drv.h"
27 #include "nouveau_gem.h"
28 #include "nouveau_mem.h"
29 #include "nouveau_ttm.h"
31 #include <drm/drm_legacy.h>
33 #include <core/tegra.h>
36 nouveau_manager_init(struct ttm_mem_type_manager
*man
, unsigned long psize
)
42 nouveau_manager_fini(struct ttm_mem_type_manager
*man
)
48 nouveau_manager_del(struct ttm_mem_type_manager
*man
, struct ttm_mem_reg
*reg
)
54 nouveau_manager_debug(struct ttm_mem_type_manager
*man
,
55 struct drm_printer
*printer
)
60 nouveau_vram_manager_del(struct ttm_mem_type_manager
*man
,
61 struct ttm_mem_reg
*reg
)
63 struct nvkm_memory
*memory
= nouveau_mem(reg
)->_mem
->memory
;
65 nvkm_memory_unref(&memory
);
69 nouveau_vram_manager_new(struct ttm_mem_type_manager
*man
,
70 struct ttm_buffer_object
*bo
,
71 const struct ttm_place
*place
,
72 struct ttm_mem_reg
*reg
)
74 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
75 struct nouveau_drm
*drm
= nvbo
->cli
->drm
;
76 struct nouveau_mem
*mem
;
79 if (drm
->client
.device
.info
.ram_size
== 0)
82 ret
= nouveau_mem_new(&drm
->master
, nvbo
->kind
, nvbo
->comp
, reg
);
83 mem
= nouveau_mem(reg
);
87 ret
= nouveau_mem_vram(reg
, nvbo
->contig
, nvbo
->page
);
100 const struct ttm_mem_type_manager_func nouveau_vram_manager
= {
101 .init
= nouveau_manager_init
,
102 .takedown
= nouveau_manager_fini
,
103 .get_node
= nouveau_vram_manager_new
,
104 .put_node
= nouveau_vram_manager_del
,
105 .debug
= nouveau_manager_debug
,
109 nouveau_gart_manager_new(struct ttm_mem_type_manager
*man
,
110 struct ttm_buffer_object
*bo
,
111 const struct ttm_place
*place
,
112 struct ttm_mem_reg
*reg
)
114 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
115 struct nouveau_drm
*drm
= nvbo
->cli
->drm
;
116 struct nouveau_mem
*mem
;
119 ret
= nouveau_mem_new(&drm
->master
, nvbo
->kind
, nvbo
->comp
, reg
);
120 mem
= nouveau_mem(reg
);
124 mem
->_mem
= &mem
->__mem
;
129 const struct ttm_mem_type_manager_func nouveau_gart_manager
= {
130 .init
= nouveau_manager_init
,
131 .takedown
= nouveau_manager_fini
,
132 .get_node
= nouveau_gart_manager_new
,
133 .put_node
= nouveau_manager_del
,
134 .debug
= nouveau_manager_debug
138 nv04_gart_manager_new(struct ttm_mem_type_manager
*man
,
139 struct ttm_buffer_object
*bo
,
140 const struct ttm_place
*place
,
141 struct ttm_mem_reg
*reg
)
143 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
144 struct nouveau_drm
*drm
= nvbo
->cli
->drm
;
145 struct nouveau_mem
*mem
;
146 struct nvkm_mmu
*mmu
= nvxx_mmu(&drm
->client
.device
);
149 ret
= nouveau_mem_new(&drm
->master
, nvbo
->kind
, nvbo
->comp
, reg
);
150 mem
= nouveau_mem(reg
);
154 ret
= nvkm_vm_get(mmu
->vmm
, reg
->num_pages
<< 12, 12,
155 NV_MEM_ACCESS_RW
, &mem
->vma
[0]);
157 nouveau_mem_del(reg
);
158 if (ret
== -ENOSPC
) {
165 mem
->_mem
= &mem
->__mem
;
166 reg
->start
= mem
->vma
[0].addr
>> PAGE_SHIFT
;
170 const struct ttm_mem_type_manager_func nv04_gart_manager
= {
171 .init
= nouveau_manager_init
,
172 .takedown
= nouveau_manager_fini
,
173 .get_node
= nv04_gart_manager_new
,
174 .put_node
= nouveau_manager_del
,
175 .debug
= nouveau_manager_debug
179 nouveau_ttm_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
181 struct drm_file
*file_priv
= filp
->private_data
;
182 struct nouveau_drm
*drm
= nouveau_drm(file_priv
->minor
->dev
);
184 if (unlikely(vma
->vm_pgoff
< DRM_FILE_PAGE_OFFSET
))
185 return drm_legacy_mmap(filp
, vma
);
187 return ttm_bo_mmap(filp
, vma
, &drm
->ttm
.bdev
);
191 nouveau_ttm_mem_global_init(struct drm_global_reference
*ref
)
193 return ttm_mem_global_init(ref
->object
);
197 nouveau_ttm_mem_global_release(struct drm_global_reference
*ref
)
199 ttm_mem_global_release(ref
->object
);
203 nouveau_ttm_global_init(struct nouveau_drm
*drm
)
205 struct drm_global_reference
*global_ref
;
208 global_ref
= &drm
->ttm
.mem_global_ref
;
209 global_ref
->global_type
= DRM_GLOBAL_TTM_MEM
;
210 global_ref
->size
= sizeof(struct ttm_mem_global
);
211 global_ref
->init
= &nouveau_ttm_mem_global_init
;
212 global_ref
->release
= &nouveau_ttm_mem_global_release
;
214 ret
= drm_global_item_ref(global_ref
);
215 if (unlikely(ret
!= 0)) {
216 DRM_ERROR("Failed setting up TTM memory accounting\n");
217 drm
->ttm
.mem_global_ref
.release
= NULL
;
221 drm
->ttm
.bo_global_ref
.mem_glob
= global_ref
->object
;
222 global_ref
= &drm
->ttm
.bo_global_ref
.ref
;
223 global_ref
->global_type
= DRM_GLOBAL_TTM_BO
;
224 global_ref
->size
= sizeof(struct ttm_bo_global
);
225 global_ref
->init
= &ttm_bo_global_init
;
226 global_ref
->release
= &ttm_bo_global_release
;
228 ret
= drm_global_item_ref(global_ref
);
229 if (unlikely(ret
!= 0)) {
230 DRM_ERROR("Failed setting up TTM BO subsystem\n");
231 drm_global_item_unref(&drm
->ttm
.mem_global_ref
);
232 drm
->ttm
.mem_global_ref
.release
= NULL
;
240 nouveau_ttm_global_release(struct nouveau_drm
*drm
)
242 if (drm
->ttm
.mem_global_ref
.release
== NULL
)
245 drm_global_item_unref(&drm
->ttm
.bo_global_ref
.ref
);
246 drm_global_item_unref(&drm
->ttm
.mem_global_ref
);
247 drm
->ttm
.mem_global_ref
.release
= NULL
;
251 nouveau_ttm_init(struct nouveau_drm
*drm
)
253 struct nvkm_device
*device
= nvxx_device(&drm
->client
.device
);
254 struct nvkm_pci
*pci
= device
->pci
;
255 struct drm_device
*dev
= drm
->dev
;
259 if (pci
&& pci
->agp
.bridge
) {
260 drm
->agp
.bridge
= pci
->agp
.bridge
;
261 drm
->agp
.base
= pci
->agp
.base
;
262 drm
->agp
.size
= pci
->agp
.size
;
263 drm
->agp
.cma
= pci
->agp
.cma
;
266 bits
= nvxx_mmu(&drm
->client
.device
)->dma_bits
;
267 if (nvxx_device(&drm
->client
.device
)->func
->pci
) {
270 } else if (device
->func
->tegra
) {
271 struct nvkm_device_tegra
*tegra
= device
->func
->tegra(device
);
274 * If the platform can use a IOMMU, then the addressable DMA
275 * space is constrained by the IOMMU bit
277 if (tegra
->func
->iommu_bit
)
278 bits
= min(bits
, tegra
->func
->iommu_bit
);
282 ret
= dma_set_mask(dev
->dev
, DMA_BIT_MASK(bits
));
283 if (ret
&& bits
!= 32) {
285 ret
= dma_set_mask(dev
->dev
, DMA_BIT_MASK(bits
));
290 ret
= dma_set_coherent_mask(dev
->dev
, DMA_BIT_MASK(bits
));
292 dma_set_coherent_mask(dev
->dev
, DMA_BIT_MASK(32));
294 ret
= nouveau_ttm_global_init(drm
);
298 ret
= ttm_bo_device_init(&drm
->ttm
.bdev
,
299 drm
->ttm
.bo_global_ref
.ref
.object
,
301 dev
->anon_inode
->i_mapping
,
302 DRM_FILE_PAGE_OFFSET
,
303 bits
<= 32 ? true : false);
305 NV_ERROR(drm
, "error initialising bo driver, %d\n", ret
);
310 drm
->gem
.vram_available
= drm
->client
.device
.info
.ram_user
;
312 arch_io_reserve_memtype_wc(device
->func
->resource_addr(device
, 1),
313 device
->func
->resource_size(device
, 1));
315 ret
= ttm_bo_init_mm(&drm
->ttm
.bdev
, TTM_PL_VRAM
,
316 drm
->gem
.vram_available
>> PAGE_SHIFT
);
318 NV_ERROR(drm
, "VRAM mm init failed, %d\n", ret
);
322 drm
->ttm
.mtrr
= arch_phys_wc_add(device
->func
->resource_addr(device
, 1),
323 device
->func
->resource_size(device
, 1));
326 if (!drm
->agp
.bridge
) {
327 drm
->gem
.gart_available
= nvxx_mmu(&drm
->client
.device
)->limit
;
329 drm
->gem
.gart_available
= drm
->agp
.size
;
332 ret
= ttm_bo_init_mm(&drm
->ttm
.bdev
, TTM_PL_TT
,
333 drm
->gem
.gart_available
>> PAGE_SHIFT
);
335 NV_ERROR(drm
, "GART mm init failed, %d\n", ret
);
339 NV_INFO(drm
, "VRAM: %d MiB\n", (u32
)(drm
->gem
.vram_available
>> 20));
340 NV_INFO(drm
, "GART: %d MiB\n", (u32
)(drm
->gem
.gart_available
>> 20));
345 nouveau_ttm_fini(struct nouveau_drm
*drm
)
347 struct nvkm_device
*device
= nvxx_device(&drm
->client
.device
);
349 ttm_bo_clean_mm(&drm
->ttm
.bdev
, TTM_PL_VRAM
);
350 ttm_bo_clean_mm(&drm
->ttm
.bdev
, TTM_PL_TT
);
352 ttm_bo_device_release(&drm
->ttm
.bdev
);
354 nouveau_ttm_global_release(drm
);
356 arch_phys_wc_del(drm
->ttm
.mtrr
);
358 arch_io_free_memtype_wc(device
->func
->resource_addr(device
, 1),
359 device
->func
->resource_size(device
, 1));