base.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright 2012 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
  23. */
  24. #include "priv.h"
  25. #include <subdev/bar.h>
  26. /******************************************************************************
  27. * instmem object base implementation
  28. *****************************************************************************/
  29. static void
  30. nvkm_instobj_load(struct nvkm_instobj *iobj)
  31. {
  32. struct nvkm_memory *memory = &iobj->memory;
  33. const u64 size = nvkm_memory_size(memory);
  34. void __iomem *map;
  35. int i;
  36. if (!(map = nvkm_kmap(memory))) {
  37. for (i = 0; i < size; i += 4)
  38. nvkm_wo32(memory, i, iobj->suspend[i / 4]);
  39. } else {
  40. memcpy_toio(map, iobj->suspend, size);
  41. }
  42. nvkm_done(memory);
  43. kvfree(iobj->suspend);
  44. iobj->suspend = NULL;
  45. }
  46. static int
  47. nvkm_instobj_save(struct nvkm_instobj *iobj)
  48. {
  49. struct nvkm_memory *memory = &iobj->memory;
  50. const u64 size = nvkm_memory_size(memory);
  51. void __iomem *map;
  52. int i;
  53. iobj->suspend = kvmalloc(size, GFP_KERNEL);
  54. if (!iobj->suspend)
  55. return -ENOMEM;
  56. if (!(map = nvkm_kmap(memory))) {
  57. for (i = 0; i < size; i += 4)
  58. iobj->suspend[i / 4] = nvkm_ro32(memory, i);
  59. } else {
  60. memcpy_fromio(iobj->suspend, map, size);
  61. }
  62. nvkm_done(memory);
  63. return 0;
  64. }
  65. void
  66. nvkm_instobj_dtor(struct nvkm_instmem *imem, struct nvkm_instobj *iobj)
  67. {
  68. spin_lock(&imem->lock);
  69. list_del(&iobj->head);
  70. spin_unlock(&imem->lock);
  71. }
  72. void
  73. nvkm_instobj_ctor(const struct nvkm_memory_func *func,
  74. struct nvkm_instmem *imem, struct nvkm_instobj *iobj)
  75. {
  76. nvkm_memory_ctor(func, &iobj->memory);
  77. iobj->suspend = NULL;
  78. spin_lock(&imem->lock);
  79. list_add_tail(&iobj->head, &imem->list);
  80. spin_unlock(&imem->lock);
  81. }
  82. int
  83. nvkm_instobj_new(struct nvkm_instmem *imem, u32 size, u32 align, bool zero,
  84. struct nvkm_memory **pmemory)
  85. {
  86. struct nvkm_subdev *subdev = &imem->subdev;
  87. struct nvkm_memory *memory = NULL;
  88. u32 offset;
  89. int ret;
  90. ret = imem->func->memory_new(imem, size, align, zero, &memory);
  91. if (ret) {
  92. nvkm_error(subdev, "OOM: %08x %08x %d\n", size, align, ret);
  93. goto done;
  94. }
  95. nvkm_trace(subdev, "new %08x %08x %d: %010llx %010llx\n", size, align,
  96. zero, nvkm_memory_addr(memory), nvkm_memory_size(memory));
  97. if (!imem->func->zero && zero) {
  98. void __iomem *map = nvkm_kmap(memory);
  99. if (unlikely(!map)) {
  100. for (offset = 0; offset < size; offset += 4)
  101. nvkm_wo32(memory, offset, 0x00000000);
  102. } else {
  103. memset_io(map, 0x00, size);
  104. }
  105. nvkm_done(memory);
  106. }
  107. done:
  108. if (ret)
  109. nvkm_memory_unref(&memory);
  110. *pmemory = memory;
  111. return ret;
  112. }
  113. /******************************************************************************
  114. * instmem subdev base implementation
  115. *****************************************************************************/
  116. u32
  117. nvkm_instmem_rd32(struct nvkm_instmem *imem, u32 addr)
  118. {
  119. return imem->func->rd32(imem, addr);
  120. }
  121. void
  122. nvkm_instmem_wr32(struct nvkm_instmem *imem, u32 addr, u32 data)
  123. {
  124. return imem->func->wr32(imem, addr, data);
  125. }
  126. void
  127. nvkm_instmem_boot(struct nvkm_instmem *imem)
  128. {
  129. /* Separate bootstrapped objects from normal list, as we need
  130. * to make sure they're accessed with the slowpath on suspend
  131. * and resume.
  132. */
  133. struct nvkm_instobj *iobj, *itmp;
  134. spin_lock(&imem->lock);
  135. list_for_each_entry_safe(iobj, itmp, &imem->list, head) {
  136. list_move_tail(&iobj->head, &imem->boot);
  137. }
  138. spin_unlock(&imem->lock);
  139. }
  140. static int
  141. nvkm_instmem_fini(struct nvkm_subdev *subdev, bool suspend)
  142. {
  143. struct nvkm_instmem *imem = nvkm_instmem(subdev);
  144. struct nvkm_instobj *iobj;
  145. if (suspend) {
  146. list_for_each_entry(iobj, &imem->list, head) {
  147. int ret = nvkm_instobj_save(iobj);
  148. if (ret)
  149. return ret;
  150. }
  151. nvkm_bar_bar2_fini(subdev->device);
  152. list_for_each_entry(iobj, &imem->boot, head) {
  153. int ret = nvkm_instobj_save(iobj);
  154. if (ret)
  155. return ret;
  156. }
  157. }
  158. if (imem->func->fini)
  159. imem->func->fini(imem);
  160. return 0;
  161. }
  162. static int
  163. nvkm_instmem_init(struct nvkm_subdev *subdev)
  164. {
  165. struct nvkm_instmem *imem = nvkm_instmem(subdev);
  166. struct nvkm_instobj *iobj;
  167. list_for_each_entry(iobj, &imem->boot, head) {
  168. if (iobj->suspend)
  169. nvkm_instobj_load(iobj);
  170. }
  171. nvkm_bar_bar2_init(subdev->device);
  172. list_for_each_entry(iobj, &imem->list, head) {
  173. if (iobj->suspend)
  174. nvkm_instobj_load(iobj);
  175. }
  176. return 0;
  177. }
  178. static int
  179. nvkm_instmem_oneinit(struct nvkm_subdev *subdev)
  180. {
  181. struct nvkm_instmem *imem = nvkm_instmem(subdev);
  182. if (imem->func->oneinit)
  183. return imem->func->oneinit(imem);
  184. return 0;
  185. }
  186. static void *
  187. nvkm_instmem_dtor(struct nvkm_subdev *subdev)
  188. {
  189. struct nvkm_instmem *imem = nvkm_instmem(subdev);
  190. if (imem->func->dtor)
  191. return imem->func->dtor(imem);
  192. return imem;
  193. }
  194. static const struct nvkm_subdev_func
  195. nvkm_instmem = {
  196. .dtor = nvkm_instmem_dtor,
  197. .oneinit = nvkm_instmem_oneinit,
  198. .init = nvkm_instmem_init,
  199. .fini = nvkm_instmem_fini,
  200. };
  201. void
  202. nvkm_instmem_ctor(const struct nvkm_instmem_func *func,
  203. struct nvkm_device *device, int index,
  204. struct nvkm_instmem *imem)
  205. {
  206. nvkm_subdev_ctor(&nvkm_instmem, device, index, &imem->subdev);
  207. imem->func = func;
  208. spin_lock_init(&imem->lock);
  209. INIT_LIST_HEAD(&imem->list);
  210. INIT_LIST_HEAD(&imem->boot);
  211. }