base.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2016 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 "priv.h"
  25. struct nvkm_top_device *
  26. nvkm_top_device_new(struct nvkm_top *top)
  27. {
  28. struct nvkm_top_device *info = kmalloc(sizeof(*info), GFP_KERNEL);
  29. if (info) {
  30. info->index = NVKM_SUBDEV_NR;
  31. info->addr = 0;
  32. info->fault = -1;
  33. info->engine = -1;
  34. info->runlist = -1;
  35. info->reset = -1;
  36. info->intr = -1;
  37. list_add_tail(&info->head, &top->device);
  38. }
  39. return info;
  40. }
  41. u32
  42. nvkm_top_reset(struct nvkm_device *device, enum nvkm_devidx index)
  43. {
  44. struct nvkm_top *top = device->top;
  45. struct nvkm_top_device *info;
  46. if (top) {
  47. list_for_each_entry(info, &top->device, head) {
  48. if (info->index == index && info->reset >= 0)
  49. return BIT(info->reset);
  50. }
  51. }
  52. return 0;
  53. }
  54. u32
  55. nvkm_top_intr_mask(struct nvkm_device *device, enum nvkm_devidx devidx)
  56. {
  57. struct nvkm_top *top = device->top;
  58. struct nvkm_top_device *info;
  59. if (top) {
  60. list_for_each_entry(info, &top->device, head) {
  61. if (info->index == devidx && info->intr >= 0)
  62. return BIT(info->intr);
  63. }
  64. }
  65. return 0;
  66. }
  67. u32
  68. nvkm_top_intr(struct nvkm_device *device, u32 intr, u64 *psubdevs)
  69. {
  70. struct nvkm_top *top = device->top;
  71. struct nvkm_top_device *info;
  72. u64 subdevs = 0;
  73. u32 handled = 0;
  74. if (top) {
  75. list_for_each_entry(info, &top->device, head) {
  76. if (info->index != NVKM_SUBDEV_NR && info->intr >= 0) {
  77. if (intr & BIT(info->intr)) {
  78. subdevs |= BIT_ULL(info->index);
  79. handled |= BIT(info->intr);
  80. }
  81. }
  82. }
  83. }
  84. *psubdevs = subdevs;
  85. return intr & ~handled;
  86. }
  87. enum nvkm_devidx
  88. nvkm_top_fault(struct nvkm_device *device, int fault)
  89. {
  90. struct nvkm_top *top = device->top;
  91. struct nvkm_top_device *info;
  92. list_for_each_entry(info, &top->device, head) {
  93. if (info->fault == fault)
  94. return info->index;
  95. }
  96. return NVKM_SUBDEV_NR;
  97. }
  98. enum nvkm_devidx
  99. nvkm_top_engine(struct nvkm_device *device, int index, int *runl, int *engn)
  100. {
  101. struct nvkm_top *top = device->top;
  102. struct nvkm_top_device *info;
  103. int n = 0;
  104. list_for_each_entry(info, &top->device, head) {
  105. if (info->engine >= 0 && info->runlist >= 0 && n++ == index) {
  106. *runl = info->runlist;
  107. *engn = info->engine;
  108. return info->index;
  109. }
  110. }
  111. return -ENODEV;
  112. }
  113. static int
  114. nvkm_top_oneinit(struct nvkm_subdev *subdev)
  115. {
  116. struct nvkm_top *top = nvkm_top(subdev);
  117. return top->func->oneinit(top);
  118. }
  119. static void *
  120. nvkm_top_dtor(struct nvkm_subdev *subdev)
  121. {
  122. struct nvkm_top *top = nvkm_top(subdev);
  123. struct nvkm_top_device *info, *temp;
  124. list_for_each_entry_safe(info, temp, &top->device, head) {
  125. list_del(&info->head);
  126. kfree(info);
  127. }
  128. return top;
  129. }
  130. static const struct nvkm_subdev_func
  131. nvkm_top = {
  132. .dtor = nvkm_top_dtor,
  133. .oneinit = nvkm_top_oneinit,
  134. };
  135. int
  136. nvkm_top_new_(const struct nvkm_top_func *func, struct nvkm_device *device,
  137. int index, struct nvkm_top **ptop)
  138. {
  139. struct nvkm_top *top;
  140. if (!(top = *ptop = kzalloc(sizeof(*top), GFP_KERNEL)))
  141. return -ENOMEM;
  142. nvkm_subdev_ctor(&nvkm_top, device, index, &top->subdev);
  143. top->func = func;
  144. INIT_LIST_HEAD(&top->device);
  145. return 0;
  146. }