2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
30 #include "radeon_drm.h"
33 int radeon_gem_object_init(struct drm_gem_object
*obj
)
40 void radeon_gem_object_free(struct drm_gem_object
*gobj
)
42 struct radeon_bo
*robj
= gem_to_radeon_bo(gobj
);
45 if (robj
->gem_base
.import_attach
)
46 drm_prime_gem_destroy(&robj
->gem_base
, robj
->tbo
.sg
);
47 radeon_bo_unref(&robj
);
51 int radeon_gem_object_create(struct radeon_device
*rdev
, int size
,
52 int alignment
, int initial_domain
,
53 bool discardable
, bool kernel
,
54 struct drm_gem_object
**obj
)
56 struct radeon_bo
*robj
;
60 /* At least align on page size */
61 if (alignment
< PAGE_SIZE
) {
62 alignment
= PAGE_SIZE
;
64 r
= radeon_bo_create(rdev
, size
, alignment
, kernel
, initial_domain
, NULL
, &robj
);
66 if (r
!= -ERESTARTSYS
)
67 DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n",
68 size
, initial_domain
, alignment
, r
);
71 *obj
= &robj
->gem_base
;
73 mutex_lock(&rdev
->gem
.mutex
);
74 list_add_tail(&robj
->list
, &rdev
->gem
.objects
);
75 mutex_unlock(&rdev
->gem
.mutex
);
80 int radeon_gem_set_domain(struct drm_gem_object
*gobj
,
81 uint32_t rdomain
, uint32_t wdomain
)
83 struct radeon_bo
*robj
;
87 /* FIXME: reeimplement */
88 robj
= gem_to_radeon_bo(gobj
);
89 /* work out where to validate the buffer to */
96 printk(KERN_WARNING
"Set domain without domain !\n");
99 if (domain
== RADEON_GEM_DOMAIN_CPU
) {
100 /* Asking for cpu access wait for object idle */
101 r
= radeon_bo_wait(robj
, NULL
, false);
103 printk(KERN_ERR
"Failed to wait for object !\n");
110 int radeon_gem_init(struct radeon_device
*rdev
)
112 INIT_LIST_HEAD(&rdev
->gem
.objects
);
116 void radeon_gem_fini(struct radeon_device
*rdev
)
118 radeon_bo_force_delete(rdev
);
122 * Call from drm_gem_handle_create which appear in both new and open ioctl
125 int radeon_gem_object_open(struct drm_gem_object
*obj
, struct drm_file
*file_priv
)
130 void radeon_gem_object_close(struct drm_gem_object
*obj
,
131 struct drm_file
*file_priv
)
133 struct radeon_bo
*rbo
= gem_to_radeon_bo(obj
);
134 struct radeon_device
*rdev
= rbo
->rdev
;
135 struct radeon_fpriv
*fpriv
= file_priv
->driver_priv
;
136 struct radeon_vm
*vm
= &fpriv
->vm
;
138 if (rdev
->family
< CHIP_CAYMAN
) {
142 if (radeon_bo_reserve(rbo
, false)) {
143 dev_err(rdev
->dev
, "leaking bo va because we fail to reserve bo\n");
146 radeon_vm_bo_rmv(rdev
, vm
, rbo
);
147 radeon_bo_unreserve(rbo
);
150 static int radeon_gem_handle_lockup(struct radeon_device
*rdev
, int r
)
153 r
= radeon_gpu_reset(rdev
);
163 int radeon_gem_info_ioctl(struct drm_device
*dev
, void *data
,
164 struct drm_file
*filp
)
166 struct radeon_device
*rdev
= dev
->dev_private
;
167 struct drm_radeon_gem_info
*args
= data
;
168 struct ttm_mem_type_manager
*man
;
171 man
= &rdev
->mman
.bdev
.man
[TTM_PL_VRAM
];
173 args
->vram_size
= rdev
->mc
.real_vram_size
;
174 args
->vram_visible
= (u64
)man
->size
<< PAGE_SHIFT
;
175 if (rdev
->stollen_vga_memory
)
176 args
->vram_visible
-= radeon_bo_size(rdev
->stollen_vga_memory
);
177 args
->vram_visible
-= radeon_fbdev_total_size(rdev
);
178 args
->gart_size
= rdev
->mc
.gtt_size
- 4096 - RADEON_IB_POOL_SIZE
*64*1024;
179 for(i
= 0; i
< RADEON_NUM_RINGS
; ++i
)
180 args
->gart_size
-= rdev
->ring
[i
].ring_size
;
184 int radeon_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
185 struct drm_file
*filp
)
187 /* TODO: implement */
188 DRM_ERROR("unimplemented %s\n", __func__
);
192 int radeon_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
193 struct drm_file
*filp
)
195 /* TODO: implement */
196 DRM_ERROR("unimplemented %s\n", __func__
);
200 int radeon_gem_create_ioctl(struct drm_device
*dev
, void *data
,
201 struct drm_file
*filp
)
203 struct radeon_device
*rdev
= dev
->dev_private
;
204 struct drm_radeon_gem_create
*args
= data
;
205 struct drm_gem_object
*gobj
;
209 down_read(&rdev
->exclusive_lock
);
210 /* create a gem object to contain this object in */
211 args
->size
= roundup(args
->size
, PAGE_SIZE
);
212 r
= radeon_gem_object_create(rdev
, args
->size
, args
->alignment
,
213 args
->initial_domain
, false,
216 up_read(&rdev
->exclusive_lock
);
217 r
= radeon_gem_handle_lockup(rdev
, r
);
220 r
= drm_gem_handle_create(filp
, gobj
, &handle
);
221 /* drop reference from allocate - handle holds it now */
222 drm_gem_object_unreference_unlocked(gobj
);
224 up_read(&rdev
->exclusive_lock
);
225 r
= radeon_gem_handle_lockup(rdev
, r
);
228 args
->handle
= handle
;
229 up_read(&rdev
->exclusive_lock
);
233 int radeon_gem_set_domain_ioctl(struct drm_device
*dev
, void *data
,
234 struct drm_file
*filp
)
236 /* transition the BO to a domain -
237 * just validate the BO into a certain domain */
238 struct radeon_device
*rdev
= dev
->dev_private
;
239 struct drm_radeon_gem_set_domain
*args
= data
;
240 struct drm_gem_object
*gobj
;
241 struct radeon_bo
*robj
;
244 /* for now if someone requests domain CPU -
245 * just make sure the buffer is finished with */
246 down_read(&rdev
->exclusive_lock
);
248 /* just do a BO wait for now */
249 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
251 up_read(&rdev
->exclusive_lock
);
254 robj
= gem_to_radeon_bo(gobj
);
256 r
= radeon_gem_set_domain(gobj
, args
->read_domains
, args
->write_domain
);
258 drm_gem_object_unreference_unlocked(gobj
);
259 up_read(&rdev
->exclusive_lock
);
260 r
= radeon_gem_handle_lockup(robj
->rdev
, r
);
264 int radeon_mode_dumb_mmap(struct drm_file
*filp
,
265 struct drm_device
*dev
,
266 uint32_t handle
, uint64_t *offset_p
)
268 struct drm_gem_object
*gobj
;
269 struct radeon_bo
*robj
;
271 gobj
= drm_gem_object_lookup(dev
, filp
, handle
);
275 robj
= gem_to_radeon_bo(gobj
);
276 *offset_p
= radeon_bo_mmap_offset(robj
);
277 drm_gem_object_unreference_unlocked(gobj
);
281 int radeon_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
282 struct drm_file
*filp
)
284 struct drm_radeon_gem_mmap
*args
= data
;
286 return radeon_mode_dumb_mmap(filp
, dev
, args
->handle
, &args
->addr_ptr
);
289 int radeon_gem_busy_ioctl(struct drm_device
*dev
, void *data
,
290 struct drm_file
*filp
)
292 struct radeon_device
*rdev
= dev
->dev_private
;
293 struct drm_radeon_gem_busy
*args
= data
;
294 struct drm_gem_object
*gobj
;
295 struct radeon_bo
*robj
;
297 uint32_t cur_placement
= 0;
299 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
303 robj
= gem_to_radeon_bo(gobj
);
304 r
= radeon_bo_wait(robj
, &cur_placement
, true);
305 switch (cur_placement
) {
307 args
->domain
= RADEON_GEM_DOMAIN_VRAM
;
310 args
->domain
= RADEON_GEM_DOMAIN_GTT
;
313 args
->domain
= RADEON_GEM_DOMAIN_CPU
;
317 drm_gem_object_unreference_unlocked(gobj
);
318 r
= radeon_gem_handle_lockup(rdev
, r
);
322 int radeon_gem_wait_idle_ioctl(struct drm_device
*dev
, void *data
,
323 struct drm_file
*filp
)
325 struct radeon_device
*rdev
= dev
->dev_private
;
326 struct drm_radeon_gem_wait_idle
*args
= data
;
327 struct drm_gem_object
*gobj
;
328 struct radeon_bo
*robj
;
331 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
335 robj
= gem_to_radeon_bo(gobj
);
336 r
= radeon_bo_wait(robj
, NULL
, false);
337 /* callback hw specific functions if any */
338 if (rdev
->asic
->ioctl_wait_idle
)
339 robj
->rdev
->asic
->ioctl_wait_idle(rdev
, robj
);
340 drm_gem_object_unreference_unlocked(gobj
);
341 r
= radeon_gem_handle_lockup(rdev
, r
);
345 int radeon_gem_set_tiling_ioctl(struct drm_device
*dev
, void *data
,
346 struct drm_file
*filp
)
348 struct drm_radeon_gem_set_tiling
*args
= data
;
349 struct drm_gem_object
*gobj
;
350 struct radeon_bo
*robj
;
353 DRM_DEBUG("%d \n", args
->handle
);
354 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
357 robj
= gem_to_radeon_bo(gobj
);
358 r
= radeon_bo_set_tiling_flags(robj
, args
->tiling_flags
, args
->pitch
);
359 drm_gem_object_unreference_unlocked(gobj
);
363 int radeon_gem_get_tiling_ioctl(struct drm_device
*dev
, void *data
,
364 struct drm_file
*filp
)
366 struct drm_radeon_gem_get_tiling
*args
= data
;
367 struct drm_gem_object
*gobj
;
368 struct radeon_bo
*rbo
;
372 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
375 rbo
= gem_to_radeon_bo(gobj
);
376 r
= radeon_bo_reserve(rbo
, false);
377 if (unlikely(r
!= 0))
379 radeon_bo_get_tiling_flags(rbo
, &args
->tiling_flags
, &args
->pitch
);
380 radeon_bo_unreserve(rbo
);
382 drm_gem_object_unreference_unlocked(gobj
);
386 int radeon_gem_va_ioctl(struct drm_device
*dev
, void *data
,
387 struct drm_file
*filp
)
389 struct drm_radeon_gem_va
*args
= data
;
390 struct drm_gem_object
*gobj
;
391 struct radeon_device
*rdev
= dev
->dev_private
;
392 struct radeon_fpriv
*fpriv
= filp
->driver_priv
;
393 struct radeon_bo
*rbo
;
394 struct radeon_bo_va
*bo_va
;
398 if (!rdev
->vm_manager
.enabled
) {
399 args
->operation
= RADEON_VA_RESULT_ERROR
;
404 * We don't support vm_id yet, to be sure we don't have have broken
405 * userspace, reject anyone trying to use non 0 value thus moving
406 * forward we can use those fields without breaking existant userspace
409 args
->operation
= RADEON_VA_RESULT_ERROR
;
413 if (args
->offset
< RADEON_VA_RESERVED_SIZE
) {
414 dev_err(&dev
->pdev
->dev
,
415 "offset 0x%lX is in reserved area 0x%X\n",
416 (unsigned long)args
->offset
,
417 RADEON_VA_RESERVED_SIZE
);
418 args
->operation
= RADEON_VA_RESULT_ERROR
;
422 /* don't remove, we need to enforce userspace to set the snooped flag
423 * otherwise we will endup with broken userspace and we won't be able
424 * to enable this feature without adding new interface
426 invalid_flags
= RADEON_VM_PAGE_VALID
| RADEON_VM_PAGE_SYSTEM
;
427 if ((args
->flags
& invalid_flags
)) {
428 dev_err(&dev
->pdev
->dev
, "invalid flags 0x%08X vs 0x%08X\n",
429 args
->flags
, invalid_flags
);
430 args
->operation
= RADEON_VA_RESULT_ERROR
;
433 if (!(args
->flags
& RADEON_VM_PAGE_SNOOPED
)) {
434 dev_err(&dev
->pdev
->dev
, "only supported snooped mapping for now\n");
435 args
->operation
= RADEON_VA_RESULT_ERROR
;
439 switch (args
->operation
) {
441 case RADEON_VA_UNMAP
:
444 dev_err(&dev
->pdev
->dev
, "unsupported operation %d\n",
446 args
->operation
= RADEON_VA_RESULT_ERROR
;
450 gobj
= drm_gem_object_lookup(dev
, filp
, args
->handle
);
452 args
->operation
= RADEON_VA_RESULT_ERROR
;
455 rbo
= gem_to_radeon_bo(gobj
);
456 r
= radeon_bo_reserve(rbo
, false);
458 args
->operation
= RADEON_VA_RESULT_ERROR
;
459 drm_gem_object_unreference_unlocked(gobj
);
462 switch (args
->operation
) {
464 bo_va
= radeon_bo_va(rbo
, &fpriv
->vm
);
466 args
->operation
= RADEON_VA_RESULT_VA_EXIST
;
467 args
->offset
= bo_va
->soffset
;
470 r
= radeon_vm_bo_add(rdev
, &fpriv
->vm
, rbo
,
471 args
->offset
, args
->flags
);
473 case RADEON_VA_UNMAP
:
474 r
= radeon_vm_bo_rmv(rdev
, &fpriv
->vm
, rbo
);
479 args
->operation
= RADEON_VA_RESULT_OK
;
481 args
->operation
= RADEON_VA_RESULT_ERROR
;
484 radeon_bo_unreserve(rbo
);
485 drm_gem_object_unreference_unlocked(gobj
);
489 int radeon_mode_dumb_create(struct drm_file
*file_priv
,
490 struct drm_device
*dev
,
491 struct drm_mode_create_dumb
*args
)
493 struct radeon_device
*rdev
= dev
->dev_private
;
494 struct drm_gem_object
*gobj
;
498 args
->pitch
= radeon_align_pitch(rdev
, args
->width
, args
->bpp
, 0) * ((args
->bpp
+ 1) / 8);
499 args
->size
= args
->pitch
* args
->height
;
500 args
->size
= ALIGN(args
->size
, PAGE_SIZE
);
502 r
= radeon_gem_object_create(rdev
, args
->size
, 0,
503 RADEON_GEM_DOMAIN_VRAM
,
504 false, ttm_bo_type_device
,
509 r
= drm_gem_handle_create(file_priv
, gobj
, &handle
);
510 /* drop reference from allocate - handle holds it now */
511 drm_gem_object_unreference_unlocked(gobj
);
515 args
->handle
= handle
;
519 int radeon_mode_dumb_destroy(struct drm_file
*file_priv
,
520 struct drm_device
*dev
,
523 return drm_gem_handle_delete(file_priv
, handle
);