ram.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2015 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs <bskeggs@redhat.com>
  23. */
  24. #include "ram.h"
  25. int
  26. nvkm_ram_init(struct nvkm_ram *ram)
  27. {
  28. if (ram->func->init)
  29. return ram->func->init(ram);
  30. return 0;
  31. }
  32. void
  33. nvkm_ram_del(struct nvkm_ram **pram)
  34. {
  35. struct nvkm_ram *ram = *pram;
  36. if (ram && !WARN_ON(!ram->func)) {
  37. if (ram->func->dtor)
  38. *pram = ram->func->dtor(ram);
  39. nvkm_mm_fini(&ram->tags);
  40. nvkm_mm_fini(&ram->vram);
  41. kfree(*pram);
  42. *pram = NULL;
  43. }
  44. }
  45. int
  46. nvkm_ram_ctor(const struct nvkm_ram_func *func, struct nvkm_fb *fb,
  47. enum nvkm_ram_type type, u64 size, u32 tags,
  48. struct nvkm_ram *ram)
  49. {
  50. static const char *name[] = {
  51. [NVKM_RAM_TYPE_UNKNOWN] = "of unknown memory type",
  52. [NVKM_RAM_TYPE_STOLEN ] = "stolen system memory",
  53. [NVKM_RAM_TYPE_SGRAM ] = "SGRAM",
  54. [NVKM_RAM_TYPE_SDRAM ] = "SDRAM",
  55. [NVKM_RAM_TYPE_DDR1 ] = "DDR1",
  56. [NVKM_RAM_TYPE_DDR2 ] = "DDR2",
  57. [NVKM_RAM_TYPE_DDR3 ] = "DDR3",
  58. [NVKM_RAM_TYPE_GDDR2 ] = "GDDR2",
  59. [NVKM_RAM_TYPE_GDDR3 ] = "GDDR3",
  60. [NVKM_RAM_TYPE_GDDR4 ] = "GDDR4",
  61. [NVKM_RAM_TYPE_GDDR5 ] = "GDDR5",
  62. };
  63. struct nvkm_subdev *subdev = &fb->subdev;
  64. int ret;
  65. nvkm_info(subdev, "%d MiB %s\n", (int)(size >> 20), name[type]);
  66. ram->func = func;
  67. ram->fb = fb;
  68. ram->type = type;
  69. ram->size = size;
  70. if (!nvkm_mm_initialised(&ram->vram)) {
  71. ret = nvkm_mm_init(&ram->vram, 0, size >> NVKM_RAM_MM_SHIFT, 1);
  72. if (ret)
  73. return ret;
  74. }
  75. if (!nvkm_mm_initialised(&ram->tags)) {
  76. ret = nvkm_mm_init(&ram->tags, 0, tags ? ++tags : 0, 1);
  77. if (ret)
  78. return ret;
  79. nvkm_debug(subdev, "%d compression tags\n", tags);
  80. }
  81. return 0;
  82. }
  83. int
  84. nvkm_ram_new_(const struct nvkm_ram_func *func, struct nvkm_fb *fb,
  85. enum nvkm_ram_type type, u64 size, u32 tags,
  86. struct nvkm_ram **pram)
  87. {
  88. if (!(*pram = kzalloc(sizeof(**pram), GFP_KERNEL)))
  89. return -ENOMEM;
  90. return nvkm_ram_ctor(func, fb, type, size, tags, *pram);
  91. }