nouveau_bo.h 3.4 KB

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