1/*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#ifndef PVR_BO_H
25#define PVR_BO_H
26
27#include <stddef.h>
28#include <stdint.h>
29#include <vulkan/vulkan.h>
30
31#include "util/list.h"
32#include "util/macros.h"
33
34struct pvr_device;
35struct pvr_winsys_bo;
36struct pvr_winsys_vma;
37struct pvr_winsys_heap;
38
39struct pvr_bo {
40   /* Since multiple components (csb, caching logic, etc) can make use of
41    * linking buffers in a list, we add 'link' in pvr_bo to avoid an extra
42    * level of structure inheritance. It's the responsibility of the buffer
43    * user to manage the list and remove the buffer from the list before
44    * freeing it.
45    */
46   struct list_head link;
47
48   struct pvr_winsys_bo *bo;
49   struct pvr_winsys_vma *vma;
50};
51
52/**
53 * \brief Flag passed to #pvr_bo_alloc() to indicate that the buffer should be
54 * CPU accessible. This is required in order to map a buffer with
55 * #pvr_bo_cpu_map().
56 */
57#define PVR_BO_ALLOC_FLAG_CPU_ACCESS BITFIELD_BIT(0U)
58/**
59 * \brief Flag passed to #pvr_bo_alloc() to indicate that the buffer should
60 * be mapped to the CPU. Implies #PVR_BO_ALLOC_FLAG_CPU_ACCESS.
61 */
62#define PVR_BO_ALLOC_FLAG_CPU_MAPPED \
63   (BITFIELD_BIT(1U) | PVR_BO_ALLOC_FLAG_CPU_ACCESS)
64/**
65 * \brief Flag passed to #pvr_bo_alloc() to indicate that the buffer should be
66 * mapped to the GPU as uncached.
67 */
68#define PVR_BO_ALLOC_FLAG_GPU_UNCACHED BITFIELD_BIT(2U)
69/**
70 * \brief Flag passed to #pvr_bo_alloc() to indicate that the buffer GPU mapping
71 * should be restricted to only allow access to the Parameter Manager unit and
72 * firmware processor.
73 */
74#define PVR_BO_ALLOC_FLAG_PM_FW_PROTECT BITFIELD_BIT(3U)
75/**
76 * \brief Flag passed to #pvr_bo_alloc() to indicate that the buffer should be
77 * zeroed at allocation time.
78 */
79#define PVR_BO_ALLOC_FLAG_ZERO_ON_ALLOC BITFIELD_BIT(4U)
80
81VkResult pvr_bo_alloc(struct pvr_device *device,
82                      struct pvr_winsys_heap *heap,
83                      uint64_t size,
84                      uint64_t alignment,
85                      uint64_t flags,
86                      struct pvr_bo **const bo_out);
87void *pvr_bo_cpu_map(struct pvr_device *device, struct pvr_bo *bo);
88void pvr_bo_cpu_unmap(struct pvr_device *device, struct pvr_bo *bo);
89void pvr_bo_free(struct pvr_device *device, struct pvr_bo *bo);
90
91#endif /* PVR_BO_H */
92