2 * Copyright 2010 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "nouveau_drv.h"
27 #include "nouveau_mm.h"
28 #include "nouveau_vm.h"
31 nouveau_vm_map_at(struct nouveau_vma
*vma
, u64 delta
, struct nouveau_mem
*node
)
33 struct nouveau_vm
*vm
= vma
->vm
;
34 struct nouveau_mm_node
*r
;
35 int big
= vma
->node
->type
!= vm
->spg_shift
;
36 u32 offset
= vma
->node
->offset
+ (delta
>> 12);
37 u32 bits
= vma
->node
->type
- 12;
38 u32 pde
= (offset
>> vm
->pgt_bits
) - vm
->fpde
;
39 u32 pte
= (offset
& ((1 << vm
->pgt_bits
) - 1)) >> bits
;
40 u32 max
= 1 << (vm
->pgt_bits
- bits
);
44 list_for_each_entry(r
, &node
->regions
, rl_entry
) {
45 u64 phys
= (u64
)r
->offset
<< 12;
46 u32 num
= r
->length
>> bits
;
49 struct nouveau_gpuobj
*pgt
= vm
->pgt
[pde
].obj
[big
];
52 if (unlikely(end
>= max
))
56 vm
->map(vma
, pgt
, node
, pte
, len
, phys
, delta
);
60 if (unlikely(end
>= max
)) {
61 phys
+= len
<< (bits
+ 12);
66 delta
+= (u64
)len
<< vma
->node
->type
;
74 nouveau_vm_map(struct nouveau_vma
*vma
, struct nouveau_mem
*node
)
76 nouveau_vm_map_at(vma
, 0, node
);
80 nouveau_vm_map_sg_table(struct nouveau_vma
*vma
, u64 delta
, u64 length
,
81 struct nouveau_mem
*mem
)
83 struct nouveau_vm
*vm
= vma
->vm
;
84 int big
= vma
->node
->type
!= vm
->spg_shift
;
85 u32 offset
= vma
->node
->offset
+ (delta
>> 12);
86 u32 bits
= vma
->node
->type
- 12;
87 u32 num
= length
>> vma
->node
->type
;
88 u32 pde
= (offset
>> vm
->pgt_bits
) - vm
->fpde
;
89 u32 pte
= (offset
& ((1 << vm
->pgt_bits
) - 1)) >> bits
;
90 u32 max
= 1 << (vm
->pgt_bits
- bits
);
94 struct scatterlist
*sg
;
96 for_each_sg(mem
->sg
->sgl
, sg
, mem
->sg
->nents
, i
) {
97 struct nouveau_gpuobj
*pgt
= vm
->pgt
[pde
].obj
[big
];
98 sglen
= sg_dma_len(sg
) >> PAGE_SHIFT
;
101 if (unlikely(end
>= max
))
105 for (m
= 0; m
< len
; m
++) {
106 dma_addr_t addr
= sg_dma_address(sg
) + (m
<< PAGE_SHIFT
);
108 vm
->map_sg(vma
, pgt
, mem
, pte
, 1, &addr
);
115 if (unlikely(end
>= max
)) {
120 for (; m
< sglen
; m
++) {
121 dma_addr_t addr
= sg_dma_address(sg
) + (m
<< PAGE_SHIFT
);
123 vm
->map_sg(vma
, pgt
, mem
, pte
, 1, &addr
);
137 nouveau_vm_map_sg(struct nouveau_vma
*vma
, u64 delta
, u64 length
,
138 struct nouveau_mem
*mem
)
140 struct nouveau_vm
*vm
= vma
->vm
;
141 dma_addr_t
*list
= mem
->pages
;
142 int big
= vma
->node
->type
!= vm
->spg_shift
;
143 u32 offset
= vma
->node
->offset
+ (delta
>> 12);
144 u32 bits
= vma
->node
->type
- 12;
145 u32 num
= length
>> vma
->node
->type
;
146 u32 pde
= (offset
>> vm
->pgt_bits
) - vm
->fpde
;
147 u32 pte
= (offset
& ((1 << vm
->pgt_bits
) - 1)) >> bits
;
148 u32 max
= 1 << (vm
->pgt_bits
- bits
);
152 struct nouveau_gpuobj
*pgt
= vm
->pgt
[pde
].obj
[big
];
155 if (unlikely(end
>= max
))
159 vm
->map_sg(vma
, pgt
, mem
, pte
, len
, list
);
164 if (unlikely(end
>= max
)) {
174 nouveau_vm_unmap_at(struct nouveau_vma
*vma
, u64 delta
, u64 length
)
176 struct nouveau_vm
*vm
= vma
->vm
;
177 int big
= vma
->node
->type
!= vm
->spg_shift
;
178 u32 offset
= vma
->node
->offset
+ (delta
>> 12);
179 u32 bits
= vma
->node
->type
- 12;
180 u32 num
= length
>> vma
->node
->type
;
181 u32 pde
= (offset
>> vm
->pgt_bits
) - vm
->fpde
;
182 u32 pte
= (offset
& ((1 << vm
->pgt_bits
) - 1)) >> bits
;
183 u32 max
= 1 << (vm
->pgt_bits
- bits
);
187 struct nouveau_gpuobj
*pgt
= vm
->pgt
[pde
].obj
[big
];
190 if (unlikely(end
>= max
))
194 vm
->unmap(pgt
, pte
, len
);
198 if (unlikely(end
>= max
)) {
208 nouveau_vm_unmap(struct nouveau_vma
*vma
)
210 nouveau_vm_unmap_at(vma
, 0, (u64
)vma
->node
->length
<< 12);
214 nouveau_vm_unmap_pgt(struct nouveau_vm
*vm
, int big
, u32 fpde
, u32 lpde
)
216 struct nouveau_vm_pgd
*vpgd
;
217 struct nouveau_vm_pgt
*vpgt
;
218 struct nouveau_gpuobj
*pgt
;
221 for (pde
= fpde
; pde
<= lpde
; pde
++) {
222 vpgt
= &vm
->pgt
[pde
- vm
->fpde
];
223 if (--vpgt
->refcount
[big
])
226 pgt
= vpgt
->obj
[big
];
227 vpgt
->obj
[big
] = NULL
;
229 list_for_each_entry(vpgd
, &vm
->pgd_list
, head
) {
230 vm
->map_pgt(vpgd
->obj
, pde
, vpgt
->obj
);
233 mutex_unlock(&vm
->mm
.mutex
);
234 nouveau_gpuobj_ref(NULL
, &pgt
);
235 mutex_lock(&vm
->mm
.mutex
);
240 nouveau_vm_map_pgt(struct nouveau_vm
*vm
, u32 pde
, u32 type
)
242 struct nouveau_vm_pgt
*vpgt
= &vm
->pgt
[pde
- vm
->fpde
];
243 struct nouveau_vm_pgd
*vpgd
;
244 struct nouveau_gpuobj
*pgt
;
245 int big
= (type
!= vm
->spg_shift
);
249 pgt_size
= (1 << (vm
->pgt_bits
+ 12)) >> type
;
252 mutex_unlock(&vm
->mm
.mutex
);
253 ret
= nouveau_gpuobj_new(vm
->dev
, NULL
, pgt_size
, 0x1000,
254 NVOBJ_FLAG_ZERO_ALLOC
, &pgt
);
255 mutex_lock(&vm
->mm
.mutex
);
259 /* someone beat us to filling the PDE while we didn't have the lock */
260 if (unlikely(vpgt
->refcount
[big
]++)) {
261 mutex_unlock(&vm
->mm
.mutex
);
262 nouveau_gpuobj_ref(NULL
, &pgt
);
263 mutex_lock(&vm
->mm
.mutex
);
267 vpgt
->obj
[big
] = pgt
;
268 list_for_each_entry(vpgd
, &vm
->pgd_list
, head
) {
269 vm
->map_pgt(vpgd
->obj
, pde
, vpgt
->obj
);
276 nouveau_vm_get(struct nouveau_vm
*vm
, u64 size
, u32 page_shift
,
277 u32 access
, struct nouveau_vma
*vma
)
279 u32 align
= (1 << page_shift
) >> 12;
280 u32 msize
= size
>> 12;
284 mutex_lock(&vm
->mm
.mutex
);
285 ret
= nouveau_mm_get(&vm
->mm
, page_shift
, msize
, 0, align
, &vma
->node
);
286 if (unlikely(ret
!= 0)) {
287 mutex_unlock(&vm
->mm
.mutex
);
291 fpde
= (vma
->node
->offset
>> vm
->pgt_bits
);
292 lpde
= (vma
->node
->offset
+ vma
->node
->length
- 1) >> vm
->pgt_bits
;
293 for (pde
= fpde
; pde
<= lpde
; pde
++) {
294 struct nouveau_vm_pgt
*vpgt
= &vm
->pgt
[pde
- vm
->fpde
];
295 int big
= (vma
->node
->type
!= vm
->spg_shift
);
297 if (likely(vpgt
->refcount
[big
])) {
298 vpgt
->refcount
[big
]++;
302 ret
= nouveau_vm_map_pgt(vm
, pde
, vma
->node
->type
);
305 nouveau_vm_unmap_pgt(vm
, big
, fpde
, pde
- 1);
306 nouveau_mm_put(&vm
->mm
, vma
->node
);
307 mutex_unlock(&vm
->mm
.mutex
);
312 mutex_unlock(&vm
->mm
.mutex
);
315 vma
->offset
= (u64
)vma
->node
->offset
<< 12;
316 vma
->access
= access
;
321 nouveau_vm_put(struct nouveau_vma
*vma
)
323 struct nouveau_vm
*vm
= vma
->vm
;
326 if (unlikely(vma
->node
== NULL
))
328 fpde
= (vma
->node
->offset
>> vm
->pgt_bits
);
329 lpde
= (vma
->node
->offset
+ vma
->node
->length
- 1) >> vm
->pgt_bits
;
331 mutex_lock(&vm
->mm
.mutex
);
332 nouveau_vm_unmap_pgt(vm
, vma
->node
->type
!= vm
->spg_shift
, fpde
, lpde
);
333 nouveau_mm_put(&vm
->mm
, vma
->node
);
335 mutex_unlock(&vm
->mm
.mutex
);
339 nouveau_vm_new(struct drm_device
*dev
, u64 offset
, u64 length
, u64 mm_offset
,
340 struct nouveau_vm
**pvm
)
342 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
343 struct nouveau_vm
*vm
;
344 u64 mm_length
= (offset
+ length
) - mm_offset
;
348 vm
= kzalloc(sizeof(*vm
), GFP_KERNEL
);
352 if (dev_priv
->card_type
== NV_50
) {
353 vm
->map_pgt
= nv50_vm_map_pgt
;
354 vm
->map
= nv50_vm_map
;
355 vm
->map_sg
= nv50_vm_map_sg
;
356 vm
->unmap
= nv50_vm_unmap
;
357 vm
->flush
= nv50_vm_flush
;
362 block
= (1 << pgt_bits
);
367 if (dev_priv
->card_type
>= NV_C0
) {
368 vm
->map_pgt
= nvc0_vm_map_pgt
;
369 vm
->map
= nvc0_vm_map
;
370 vm
->map_sg
= nvc0_vm_map_sg
;
371 vm
->unmap
= nvc0_vm_unmap
;
372 vm
->flush
= nvc0_vm_flush
;
382 vm
->fpde
= offset
>> pgt_bits
;
383 vm
->lpde
= (offset
+ length
- 1) >> pgt_bits
;
384 vm
->pgt
= kcalloc(vm
->lpde
- vm
->fpde
+ 1, sizeof(*vm
->pgt
), GFP_KERNEL
);
390 INIT_LIST_HEAD(&vm
->pgd_list
);
393 vm
->pgt_bits
= pgt_bits
- 12;
395 ret
= nouveau_mm_init(&vm
->mm
, mm_offset
>> 12, mm_length
>> 12,
407 nouveau_vm_link(struct nouveau_vm
*vm
, struct nouveau_gpuobj
*pgd
)
409 struct nouveau_vm_pgd
*vpgd
;
415 vpgd
= kzalloc(sizeof(*vpgd
), GFP_KERNEL
);
419 nouveau_gpuobj_ref(pgd
, &vpgd
->obj
);
421 mutex_lock(&vm
->mm
.mutex
);
422 for (i
= vm
->fpde
; i
<= vm
->lpde
; i
++)
423 vm
->map_pgt(pgd
, i
, vm
->pgt
[i
- vm
->fpde
].obj
);
424 list_add(&vpgd
->head
, &vm
->pgd_list
);
425 mutex_unlock(&vm
->mm
.mutex
);
430 nouveau_vm_unlink(struct nouveau_vm
*vm
, struct nouveau_gpuobj
*mpgd
)
432 struct nouveau_vm_pgd
*vpgd
, *tmp
;
433 struct nouveau_gpuobj
*pgd
= NULL
;
438 mutex_lock(&vm
->mm
.mutex
);
439 list_for_each_entry_safe(vpgd
, tmp
, &vm
->pgd_list
, head
) {
440 if (vpgd
->obj
== mpgd
) {
442 list_del(&vpgd
->head
);
447 mutex_unlock(&vm
->mm
.mutex
);
449 nouveau_gpuobj_ref(NULL
, &pgd
);
453 nouveau_vm_del(struct nouveau_vm
*vm
)
455 struct nouveau_vm_pgd
*vpgd
, *tmp
;
457 list_for_each_entry_safe(vpgd
, tmp
, &vm
->pgd_list
, head
) {
458 nouveau_vm_unlink(vm
, vpgd
->obj
);
461 nouveau_mm_fini(&vm
->mm
);
467 nouveau_vm_ref(struct nouveau_vm
*ref
, struct nouveau_vm
**ptr
,
468 struct nouveau_gpuobj
*pgd
)
470 struct nouveau_vm
*vm
;
475 ret
= nouveau_vm_link(vm
, pgd
);
486 nouveau_vm_unlink(vm
, pgd
);
488 if (--vm
->refcount
== 0)