base.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "priv.h"
  25. #include "chan.h"
  26. #include <engine/fifo.h>
  27. bool
  28. nvkm_sw_mthd(struct nvkm_sw *sw, int chid, int subc, u32 mthd, u32 data)
  29. {
  30. struct nvkm_sw_chan *chan;
  31. bool handled = false;
  32. unsigned long flags;
  33. spin_lock_irqsave(&sw->engine.lock, flags);
  34. list_for_each_entry(chan, &sw->chan, head) {
  35. if (chan->fifo->chid == chid) {
  36. handled = nvkm_sw_chan_mthd(chan, subc, mthd, data);
  37. list_del(&chan->head);
  38. list_add(&chan->head, &sw->chan);
  39. break;
  40. }
  41. }
  42. spin_unlock_irqrestore(&sw->engine.lock, flags);
  43. return handled;
  44. }
  45. static int
  46. nvkm_sw_oclass_new(const struct nvkm_oclass *oclass, void *data, u32 size,
  47. struct nvkm_object **pobject)
  48. {
  49. struct nvkm_sw_chan *chan = nvkm_sw_chan(oclass->parent);
  50. const struct nvkm_sw_chan_sclass *sclass = oclass->engn;
  51. return sclass->ctor(chan, oclass, data, size, pobject);
  52. }
  53. static int
  54. nvkm_sw_oclass_get(struct nvkm_oclass *oclass, int index)
  55. {
  56. struct nvkm_sw *sw = nvkm_sw(oclass->engine);
  57. int c = 0;
  58. while (sw->func->sclass[c].ctor) {
  59. if (c++ == index) {
  60. oclass->engn = &sw->func->sclass[index];
  61. oclass->base = sw->func->sclass[index].base;
  62. oclass->base.ctor = nvkm_sw_oclass_new;
  63. return index;
  64. }
  65. }
  66. return c;
  67. }
  68. static int
  69. nvkm_sw_cclass_get(struct nvkm_fifo_chan *fifoch,
  70. const struct nvkm_oclass *oclass,
  71. struct nvkm_object **pobject)
  72. {
  73. struct nvkm_sw *sw = nvkm_sw(oclass->engine);
  74. return sw->func->chan_new(sw, fifoch, oclass, pobject);
  75. }
  76. static void *
  77. nvkm_sw_dtor(struct nvkm_engine *engine)
  78. {
  79. return nvkm_sw(engine);
  80. }
  81. static const struct nvkm_engine_func
  82. nvkm_sw = {
  83. .dtor = nvkm_sw_dtor,
  84. .fifo.cclass = nvkm_sw_cclass_get,
  85. .fifo.sclass = nvkm_sw_oclass_get,
  86. };
  87. int
  88. nvkm_sw_new_(const struct nvkm_sw_func *func, struct nvkm_device *device,
  89. int index, struct nvkm_sw **psw)
  90. {
  91. struct nvkm_sw *sw;
  92. if (!(sw = *psw = kzalloc(sizeof(*sw), GFP_KERNEL)))
  93. return -ENOMEM;
  94. INIT_LIST_HEAD(&sw->chan);
  95. sw->func = func;
  96. return nvkm_engine_ctor(&nvkm_sw, device, index, true, &sw->engine);
  97. }