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