1 /* $NetBSD: nouveau_bo.h,v 1.6 2021/12/19 10:51:56 riastradh Exp $ */ 2 3 /* SPDX-License-Identifier: MIT */ 4 #ifndef __NOUVEAU_BO_H__ 5 #define __NOUVEAU_BO_H__ 6 7 #include <drm/drm_gem.h> 8 9 #include <ttm/ttm_bo_api.h> 10 11 struct nouveau_channel; 12 struct nouveau_cli; 13 struct nouveau_drm; 14 struct nouveau_fence; 15 struct nvkm_vm; 16 struct nvkm_vma; 17 18 struct nouveau_bo { 19 struct ttm_buffer_object bo; 20 struct ttm_placement placement; 21 u32 valid_domains; 22 struct ttm_place placements[3]; 23 struct ttm_place busy_placements[3]; 24 bool force_coherent; 25 struct ttm_bo_kmap_obj kmap; 26 struct list_head head; 27 28 /* protected by ttm_bo_reserve() */ 29 struct drm_file *reserved_by; 30 struct list_head entry; 31 int pbbo_index; 32 bool validate_mapped; 33 34 struct list_head vma_list; 35 36 unsigned contig:1; 37 unsigned page:5; 38 unsigned kind:8; 39 unsigned comp:3; 40 unsigned zeta:3; 41 unsigned mode; 42 43 struct nouveau_drm_tile *tile; 44 45 /* protect by the ttm reservation lock */ 46 int pin_refcnt; 47 48 struct ttm_bo_kmap_obj dma_buf_vmap; 49 }; 50 51 static inline struct nouveau_bo * 52 nouveau_bo(struct ttm_buffer_object *bo) 53 { 54 return container_of(bo, struct nouveau_bo, bo); 55 } 56 57 static inline int 58 nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pnvbo) 59 { 60 struct nouveau_bo *prev; 61 62 if (!pnvbo) 63 return -EINVAL; 64 prev = *pnvbo; 65 66 if (ref) { 67 ttm_bo_get(&ref->bo); 68 *pnvbo = nouveau_bo(&ref->bo); 69 } else { 70 *pnvbo = NULL; 71 } 72 if (prev) 73 ttm_bo_put(&prev->bo); 74 75 return 0; 76 } 77 78 extern struct ttm_bo_driver nouveau_bo_driver; 79 80 void nouveau_bo_move_init(struct nouveau_drm *); 81 struct nouveau_bo *nouveau_bo_alloc(struct nouveau_cli *, u64 *size, int *align, 82 u32 flags, u32 tile_mode, u32 tile_flags); 83 int nouveau_bo_init(struct nouveau_bo *, u64 size, int align, u32 flags, 84 struct sg_table *sg, struct dma_resv *robj); 85 int nouveau_bo_new(struct nouveau_cli *, u64 size, int align, u32 flags, 86 u32 tile_mode, u32 tile_flags, struct sg_table *sg, 87 struct dma_resv *robj, 88 struct nouveau_bo **); 89 int nouveau_bo_pin(struct nouveau_bo *, u32 flags, bool contig); 90 int nouveau_bo_unpin(struct nouveau_bo *); 91 int nouveau_bo_map(struct nouveau_bo *); 92 void nouveau_bo_unmap(struct nouveau_bo *); 93 void nouveau_bo_placement_set(struct nouveau_bo *, u32 type, u32 busy); 94 void nouveau_bo_wr16(struct nouveau_bo *, unsigned index, u16 val); 95 u32 nouveau_bo_rd32(struct nouveau_bo *, unsigned index); 96 void nouveau_bo_wr32(struct nouveau_bo *, unsigned index, u32 val); 97 void nouveau_bo_fence(struct nouveau_bo *, struct nouveau_fence *, bool exclusive); 98 int nouveau_bo_validate(struct nouveau_bo *, bool interruptible, 99 bool no_wait_gpu); 100 void nouveau_bo_sync_for_device(struct nouveau_bo *nvbo); 101 void nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo); 102 103 #ifdef __NetBSD__ 104 # define __iomem volatile 105 # define __force 106 #endif 107 108 /* TODO: submit equivalent to TTM generic API upstream? */ 109 static inline void __iomem * 110 nvbo_kmap_obj_iovirtual(struct nouveau_bo *nvbo) 111 { 112 bool is_iomem; 113 void __iomem *ioptr = (void __force __iomem *)ttm_kmap_obj_virtual( 114 &nvbo->kmap, &is_iomem); 115 WARN_ON_ONCE(ioptr && !is_iomem); 116 return ioptr; 117 } 118 119 #ifdef __NetBSD__ 120 # undef __iomem 121 # undef __force 122 #endif 123 124 static inline void 125 nouveau_bo_unmap_unpin_unref(struct nouveau_bo **pnvbo) 126 { 127 if (*pnvbo) { 128 nouveau_bo_unmap(*pnvbo); 129 nouveau_bo_unpin(*pnvbo); 130 nouveau_bo_ref(NULL, pnvbo); 131 } 132 } 133 134 static inline int 135 nouveau_bo_new_pin_map(struct nouveau_cli *cli, u64 size, int align, u32 flags, 136 struct nouveau_bo **pnvbo) 137 { 138 int ret = nouveau_bo_new(cli, size, align, flags, 139 0, 0, NULL, NULL, pnvbo); 140 if (ret == 0) { 141 ret = nouveau_bo_pin(*pnvbo, flags, true); 142 if (ret == 0) { 143 ret = nouveau_bo_map(*pnvbo); 144 if (ret == 0) 145 return ret; 146 nouveau_bo_unpin(*pnvbo); 147 } 148 nouveau_bo_ref(NULL, pnvbo); 149 } 150 return ret; 151 } 152 #endif 153