nouveau_bo.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NOUVEAU_BO_H__
  3. #define __NOUVEAU_BO_H__
  4. #include <drm/drm_gem.h>
  5. struct nouveau_channel;
  6. struct nouveau_fence;
  7. struct nvkm_vma;
  8. struct nouveau_bo {
  9. struct ttm_buffer_object bo;
  10. struct ttm_placement placement;
  11. u32 valid_domains;
  12. struct ttm_place placements[3];
  13. struct ttm_place busy_placements[3];
  14. bool force_coherent;
  15. struct ttm_bo_kmap_obj kmap;
  16. struct list_head head;
  17. /* protected by ttm_bo_reserve() */
  18. struct drm_file *reserved_by;
  19. struct list_head entry;
  20. int pbbo_index;
  21. bool validate_mapped;
  22. struct list_head vma_list;
  23. struct nouveau_cli *cli;
  24. unsigned contig:1;
  25. unsigned page:5;
  26. unsigned kind:8;
  27. unsigned comp:3;
  28. unsigned zeta:3;
  29. unsigned mode;
  30. struct nouveau_drm_tile *tile;
  31. /* Only valid if allocated via nouveau_gem_new() and iff you hold a
  32. * gem reference to it! For debugging, use gem.filp != NULL to test
  33. * whether it is valid. */
  34. struct drm_gem_object gem;
  35. /* protect by the ttm reservation lock */
  36. int pin_refcnt;
  37. struct ttm_bo_kmap_obj dma_buf_vmap;
  38. };
  39. static inline struct nouveau_bo *
  40. nouveau_bo(struct ttm_buffer_object *bo)
  41. {
  42. return container_of(bo, struct nouveau_bo, bo);
  43. }
  44. static inline int
  45. nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pnvbo)
  46. {
  47. struct nouveau_bo *prev;
  48. if (!pnvbo)
  49. return -EINVAL;
  50. prev = *pnvbo;
  51. *pnvbo = ref ? nouveau_bo(ttm_bo_reference(&ref->bo)) : NULL;
  52. if (prev) {
  53. struct ttm_buffer_object *bo = &prev->bo;
  54. ttm_bo_unref(&bo);
  55. }
  56. return 0;
  57. }
  58. extern struct ttm_bo_driver nouveau_bo_driver;
  59. void nouveau_bo_move_init(struct nouveau_drm *);
  60. int nouveau_bo_new(struct nouveau_cli *, u64 size, int align, u32 flags,
  61. u32 tile_mode, u32 tile_flags, struct sg_table *sg,
  62. struct reservation_object *robj,
  63. struct nouveau_bo **);
  64. int nouveau_bo_pin(struct nouveau_bo *, u32 flags, bool contig);
  65. int nouveau_bo_unpin(struct nouveau_bo *);
  66. int nouveau_bo_map(struct nouveau_bo *);
  67. void nouveau_bo_unmap(struct nouveau_bo *);
  68. void nouveau_bo_placement_set(struct nouveau_bo *, u32 type, u32 busy);
  69. void nouveau_bo_wr16(struct nouveau_bo *, unsigned index, u16 val);
  70. u32 nouveau_bo_rd32(struct nouveau_bo *, unsigned index);
  71. void nouveau_bo_wr32(struct nouveau_bo *, unsigned index, u32 val);
  72. void nouveau_bo_fence(struct nouveau_bo *, struct nouveau_fence *, bool exclusive);
  73. int nouveau_bo_validate(struct nouveau_bo *, bool interruptible,
  74. bool no_wait_gpu);
  75. void nouveau_bo_sync_for_device(struct nouveau_bo *nvbo);
  76. void nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo);
  77. /* TODO: submit equivalent to TTM generic API upstream? */
  78. static inline void __iomem *
  79. nvbo_kmap_obj_iovirtual(struct nouveau_bo *nvbo)
  80. {
  81. bool is_iomem;
  82. void __iomem *ioptr = (void __force __iomem *)ttm_kmap_obj_virtual(
  83. &nvbo->kmap, &is_iomem);
  84. WARN_ON_ONCE(ioptr && !is_iomem);
  85. return ioptr;
  86. }
  87. static inline void
  88. nouveau_bo_unmap_unpin_unref(struct nouveau_bo **pnvbo)
  89. {
  90. if (*pnvbo) {
  91. nouveau_bo_unmap(*pnvbo);
  92. nouveau_bo_unpin(*pnvbo);
  93. nouveau_bo_ref(NULL, pnvbo);
  94. }
  95. }
  96. static inline int
  97. nouveau_bo_new_pin_map(struct nouveau_cli *cli, u64 size, int align, u32 flags,
  98. struct nouveau_bo **pnvbo)
  99. {
  100. int ret = nouveau_bo_new(cli, size, align, flags,
  101. 0, 0, NULL, NULL, pnvbo);
  102. if (ret == 0) {
  103. ret = nouveau_bo_pin(*pnvbo, flags, true);
  104. if (ret == 0) {
  105. ret = nouveau_bo_map(*pnvbo);
  106. if (ret == 0)
  107. return ret;
  108. nouveau_bo_unpin(*pnvbo);
  109. }
  110. nouveau_bo_ref(NULL, pnvbo);
  111. }
  112. return ret;
  113. }
  114. #endif