1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  */
22 
23 #include "igt_amd.h"
24 #include "igt.h"
25 #include <amdgpu_drm.h>
26 
27 uint32_t igt_amd_create_bo(int fd, uint64_t size)
28 {
29 	union drm_amdgpu_gem_create create;
30 
31 	memset(&create, 0, sizeof(create));
32 	create.in.bo_size = size;
33 	create.in.alignment = 256;
34 	create.in.domains = AMDGPU_GEM_DOMAIN_VRAM;
35 	create.in.domain_flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
36 				 | AMDGPU_GEM_CREATE_VRAM_CLEARED;
37 
38 	do_ioctl(fd, DRM_IOCTL_AMDGPU_GEM_CREATE, &create);
39 	igt_assert(create.out.handle);
40 
41 	return create.out.handle;
42 }
43 
44 void *igt_amd_mmap_bo(int fd, uint32_t handle, uint64_t size, int prot)
45 {
46 	union drm_amdgpu_gem_mmap map;
47 	void *ptr;
48 
49 	memset(&map, 0, sizeof(map));
50 	map.in.handle = handle;
51 
52 	do_ioctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &map);
53 
54 	ptr = mmap(0, size, prot, MAP_SHARED, fd, map.out.addr_ptr);
55 	return ptr == MAP_FAILED ? NULL : ptr;
56 }
57