nouveau_sgdma.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0
  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. struct nouveau_sgdma_be {
  8. /* this has to be the first field so populate/unpopulated in
  9. * nouve_bo.c works properly, otherwise have to move them here
  10. */
  11. struct ttm_dma_tt ttm;
  12. struct nouveau_mem *mem;
  13. };
  14. static void
  15. nouveau_sgdma_destroy(struct ttm_tt *ttm)
  16. {
  17. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  18. if (ttm) {
  19. ttm_dma_tt_fini(&nvbe->ttm);
  20. kfree(nvbe);
  21. }
  22. }
  23. static int
  24. nv04_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *reg)
  25. {
  26. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  27. struct nouveau_mem *mem = nouveau_mem(reg);
  28. int ret;
  29. ret = nouveau_mem_host(reg, &nvbe->ttm);
  30. if (ret)
  31. return ret;
  32. ret = nouveau_mem_map(mem, &mem->cli->vmm.vmm, &mem->vma[0]);
  33. if (ret) {
  34. nouveau_mem_fini(mem);
  35. return ret;
  36. }
  37. nvbe->mem = mem;
  38. return 0;
  39. }
  40. static int
  41. nv04_sgdma_unbind(struct ttm_tt *ttm)
  42. {
  43. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  44. nouveau_mem_fini(nvbe->mem);
  45. return 0;
  46. }
  47. static struct ttm_backend_func nv04_sgdma_backend = {
  48. .bind = nv04_sgdma_bind,
  49. .unbind = nv04_sgdma_unbind,
  50. .destroy = nouveau_sgdma_destroy
  51. };
  52. static int
  53. nv50_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *reg)
  54. {
  55. struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm;
  56. struct nouveau_mem *mem = nouveau_mem(reg);
  57. int ret;
  58. ret = nouveau_mem_host(reg, &nvbe->ttm);
  59. if (ret)
  60. return ret;
  61. nvbe->mem = mem;
  62. return 0;
  63. }
  64. static struct ttm_backend_func nv50_sgdma_backend = {
  65. .bind = nv50_sgdma_bind,
  66. .unbind = nv04_sgdma_unbind,
  67. .destroy = nouveau_sgdma_destroy
  68. };
  69. struct ttm_tt *
  70. nouveau_sgdma_create_ttm(struct ttm_buffer_object *bo, uint32_t page_flags)
  71. {
  72. struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
  73. struct nouveau_sgdma_be *nvbe;
  74. nvbe = kzalloc(sizeof(*nvbe), GFP_KERNEL);
  75. if (!nvbe)
  76. return NULL;
  77. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA)
  78. nvbe->ttm.ttm.func = &nv04_sgdma_backend;
  79. else
  80. nvbe->ttm.ttm.func = &nv50_sgdma_backend;
  81. if (ttm_dma_tt_init(&nvbe->ttm, bo, page_flags))
  82. /*
  83. * A failing ttm_dma_tt_init() will call ttm_tt_destroy()
  84. * and thus our nouveau_sgdma_destroy() hook, so we don't need
  85. * to free nvbe here.
  86. */
  87. return NULL;
  88. return &nvbe->ttm.ttm;
  89. }