Debian Codegraph @

src/imagination/vulkan/pvr_free_list.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "pvr_free_list.h"
8
9 #include "vk_log.h"
10
11 #include "hwdef/pvr_hw_utils.h"
12
13 #include "pvr_device.h"
14 #include "pvr_physical_device.h"
15
16 #define ROGUE_BIF_PM_FREELIST_BASE_ADDR_ALIGNSIZE 16U
17
18 /* FIXME: Is there a hardware define we can use instead? */
19 /* 1 DWord per PM physical page stored in the free list */
20 #define ROGUE_FREE_LIST_ENTRY_SIZE ((uint32_t)sizeof(uint32_t))
21
22 VkResult pvr_free_list_create(struct pvr_device *device,
23 uint32_t initial_size,
24 uint32_t max_size,
25 uint32_t grow_size,
26 uint32_t grow_threshold,
27 struct pvr_free_list *parent_free_list,
28 struct pvr_free_list **const free_list_out)
29 {
30 const struct pvr_device_runtime_info *runtime_info =
31 &device->pdevice->dev_runtime_info;
32 struct pvr_winsys_free_list *parent_ws_free_list =
33 parent_free_list ? parent_free_list->ws_free_list : NULL;
34 const uint64_t bo_flags = PVR_BO_ALLOC_FLAG_GPU_UNCACHED |
35 PVR_BO_ALLOC_FLAG_PM_FW_PROTECT;
36 struct pvr_free_list *free_list;
37 uint32_t cache_line_size;
38 uint32_t initial_num_pages;
39 uint32_t grow_num_pages;
40 uint32_t max_num_pages;
41 uint64_t addr_alignment;
42 uint64_t size_alignment;
43 uint64_t size;
44 VkResult result;
45
46 assert((initial_size + grow_size) <= max_size);
47 assert(max_size != 0);
48 assert(grow_threshold <= 100);
49
50 /* Make sure the free list is created with at least a single page. */
51 if (initial_size == 0)
52 initial_size = ROGUE_BIF_PM_PHYSICAL_PAGE_SIZE;
53
54 /* The freelists sizes must respect the PM freelist base address alignment
55 * requirement. As the freelist entries are cached by the SLC, it's also
56 * necessary to ensure the sizes respect the SLC cache line size to avoid
57 * invalid entries appearing in the cache, which would be problematic after
58 * a grow operation, as the SLC entries aren't invalidated. We do this by
59 * making sure the freelist values are appropriately aligned.
60 *
61 * To calculate the alignment, we first take the largest of the freelist
62 * base address alignment and the SLC cache line size. We then divide this
63 * by the freelist entry size to determine the number of freelist entries
64 * required by the PM. Finally, as each entry holds a single PM physical
65 * page, we multiple the number of entries by the page size.
66 *
67 * As an example, if the base address alignment is 16 bytes, the SLC cache
68 * line size is 64 bytes and the freelist entry size is 4 bytes then 16
69 * entries are required, as we take the SLC cacheline size (being the larger
70 * of the two values) and divide this by 4. If the PM page size is 4096
71 * bytes then we end up with an alignment of 65536 bytes.
72 */
73 cache_line_size = pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
74
75 addr_alignment =
76 MAX2(ROGUE_BIF_PM_FREELIST_BASE_ADDR_ALIGNSIZE, cache_line_size);
77 size_alignment = (addr_alignment / ROGUE_FREE_LIST_ENTRY_SIZE) *
78 ROGUE_BIF_PM_PHYSICAL_PAGE_SIZE;
79
80 assert(util_is_power_of_two_nonzero64(size_alignment));
81
82 initial_size = align64(initial_size, size_alignment);
83 max_size = align64(max_size, size_alignment);
84 grow_size = align64(grow_size, size_alignment);
85
86 /* Make sure the 'max' size doesn't exceed what the firmware supports and
87 * adjust the other sizes accordingly.
88 */
89 if (max_size > runtime_info->max_free_list_size) {
90 max_size = runtime_info->max_free_list_size;
91 assert(align64(max_size, size_alignment) == max_size);
92 }
93
94 if (initial_size > max_size)
95 initial_size = max_size;
96
97 if (initial_size == max_size)
98 grow_size = 0;
99
100 initial_num_pages = initial_size >> ROGUE_BIF_PM_PHYSICAL_PAGE_SHIFT;
101 max_num_pages = max_size >> ROGUE_BIF_PM_PHYSICAL_PAGE_SHIFT;
102 grow_num_pages = grow_size >> ROGUE_BIF_PM_PHYSICAL_PAGE_SHIFT;
103
104 /* Calculate the size of the buffer needed to store the free list entries
105 * based on the maximum number of pages we can have.
106 */
107 size = max_num_pages * ROGUE_FREE_LIST_ENTRY_SIZE;
108 assert(align64(size, addr_alignment) == size);
109
110 free_list = vk_alloc(&device->vk.alloc,
111 sizeof(*free_list),
112 8,
113 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
114 if (!free_list)
115 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
116
117 /* FIXME: The memory is mapped GPU uncached, but this seems to contradict
118 * the comment above about aligning to the SLC cache line size.
119 */
120 result = pvr_bo_alloc(device,
121 device->heaps.general_heap,
122 size,
123 addr_alignment,
124 bo_flags,
125 &free_list->bo);
126 if (result != VK_SUCCESS)
127 goto err_vk_free_free_list;
128
129 result = device->ws->ops->free_list_create(device->ws,
130 free_list->bo->vma,
131 initial_num_pages,
132 max_num_pages,
133 grow_num_pages,
134 grow_threshold,
135 parent_ws_free_list,
136 &free_list->ws_free_list);
137 if (result != VK_SUCCESS)
138 goto err_pvr_bo_free_bo;
139
140 free_list->device = device;
141 free_list->size = size;
142
143 *free_list_out = free_list;
144
145 return VK_SUCCESS;
146
147 err_pvr_bo_free_bo:
148 pvr_bo_free(device, free_list->bo);
149
150 err_vk_free_free_list:
151 vk_free(&device->vk.alloc, free_list);
152
153 return result;
154 }
155
156 void pvr_free_list_destroy(struct pvr_free_list *free_list)
157 {
158 struct pvr_device *device = free_list->device;
159
160 device->ws->ops->free_list_destroy(free_list->ws_free_list);
161 pvr_bo_free(device, free_list->bo);
162 vk_free(&device->vk.alloc, free_list);
163 }