nouveau_sgdma.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: MIT
  2. #include <linux/pagemap.h>
  3. #include <linux/slab.h>
  4. #include "nouveau_drv.h"
  5. #include "nouveau_mem.h"
  6. #include "nouveau_ttm.h"
  7. #include "nouveau_bo.h"
  8. struct nouveau_sgdma_be {
  9. /* this has to be the first field so populate/unpopulated in
  10. * nouve_bo.c works properly, otherwise have to move them here
  11. */
  12. struct ttm_tt ttm;
  13. struct nouveau_mem *mem;
  14. };
  15. void
  16. nouveau_sgdma_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
  17. {
  18. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  19. if (ttm) {
  20. ttm_tt_fini(&nvbe->ttm);
  21. kfree(nvbe);
  22. }
  23. }
  24. int
  25. nouveau_sgdma_bind(struct ttm_device *bdev, struct ttm_tt *ttm, struct ttm_resource *reg)
  26. {
  27. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  28. struct nouveau_drm *drm = nouveau_bdev(bdev);
  29. struct nouveau_mem *mem = nouveau_mem(reg);
  30. int ret;
  31. if (nvbe->mem)
  32. return 0;
  33. ret = nouveau_mem_host(reg, &nvbe->ttm);
  34. if (ret)
  35. return ret;
  36. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  37. ret = nouveau_mem_map(mem, &mem->cli->vmm.vmm, &mem->vma[0]);
  38. if (ret) {
  39. nouveau_mem_fini(mem);
  40. return ret;
  41. }
  42. }
  43. nvbe->mem = mem;
  44. return 0;
  45. }
  46. void
  47. nouveau_sgdma_unbind(struct ttm_device *bdev, struct ttm_tt *ttm)
  48. {
  49. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  50. if (nvbe->mem) {
  51. nouveau_mem_fini(nvbe->mem);
  52. nvbe->mem = NULL;
  53. }
  54. }
  55. struct ttm_tt *
  56. nouveau_sgdma_create_ttm(struct ttm_buffer_object *bo, uint32_t page_flags)
  57. {
  58. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  59. struct nouveau_bo *nvbo = nouveau_bo(bo);
  60. struct nouveau_sgdma_be *nvbe;
  61. enum ttm_caching caching;
  62. if (nvbo->force_coherent)
  63. caching = ttm_uncached;
  64. else if (drm->agp.bridge)
  65. caching = ttm_write_combined;
  66. else
  67. caching = ttm_cached;
  68. nvbe = kzalloc(sizeof(*nvbe), GFP_KERNEL);
  69. if (!nvbe)
  70. return NULL;
  71. if (ttm_sg_tt_init(&nvbe->ttm, bo, page_flags, caching)) {
  72. kfree(nvbe);
  73. return NULL;
  74. }
  75. return &nvbe->ttm;
  76. }