/drivers/gpu/drm/nouveau/nouveau_grctx.c

https://bitbucket.org/cresqo/cm7-p500-kernel · C · 160 lines · 116 code · 21 blank · 23 comment · 20 complexity · 5e4edc7da8815354410ab9e5fa83761e MD5 · raw file

  1. /*
  2. * Copyright 2009 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 <linux/firmware.h>
  25. #include <linux/slab.h>
  26. #include "drmP.h"
  27. #include "nouveau_drv.h"
  28. struct nouveau_ctxprog {
  29. uint32_t signature;
  30. uint8_t version;
  31. uint16_t length;
  32. uint32_t data[];
  33. } __attribute__ ((packed));
  34. struct nouveau_ctxvals {
  35. uint32_t signature;
  36. uint8_t version;
  37. uint32_t length;
  38. struct {
  39. uint32_t offset;
  40. uint32_t value;
  41. } data[];
  42. } __attribute__ ((packed));
  43. int
  44. nouveau_grctx_prog_load(struct drm_device *dev)
  45. {
  46. struct drm_nouveau_private *dev_priv = dev->dev_private;
  47. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  48. const int chipset = dev_priv->chipset;
  49. const struct firmware *fw;
  50. const struct nouveau_ctxprog *cp;
  51. const struct nouveau_ctxvals *cv;
  52. char name[32];
  53. int ret, i;
  54. if (pgraph->accel_blocked)
  55. return -ENODEV;
  56. if (!pgraph->ctxprog) {
  57. sprintf(name, "nouveau/nv%02x.ctxprog", chipset);
  58. ret = request_firmware(&fw, name, &dev->pdev->dev);
  59. if (ret) {
  60. NV_ERROR(dev, "No ctxprog for NV%02x\n", chipset);
  61. return ret;
  62. }
  63. pgraph->ctxprog = kmemdup(fw->data, fw->size, GFP_KERNEL);
  64. if (!pgraph->ctxprog) {
  65. NV_ERROR(dev, "OOM copying ctxprog\n");
  66. release_firmware(fw);
  67. return -ENOMEM;
  68. }
  69. cp = pgraph->ctxprog;
  70. if (le32_to_cpu(cp->signature) != 0x5043564e ||
  71. cp->version != 0 ||
  72. le16_to_cpu(cp->length) != ((fw->size - 7) / 4)) {
  73. NV_ERROR(dev, "ctxprog invalid\n");
  74. release_firmware(fw);
  75. nouveau_grctx_fini(dev);
  76. return -EINVAL;
  77. }
  78. release_firmware(fw);
  79. }
  80. if (!pgraph->ctxvals) {
  81. sprintf(name, "nouveau/nv%02x.ctxvals", chipset);
  82. ret = request_firmware(&fw, name, &dev->pdev->dev);
  83. if (ret) {
  84. NV_ERROR(dev, "No ctxvals for NV%02x\n", chipset);
  85. nouveau_grctx_fini(dev);
  86. return ret;
  87. }
  88. pgraph->ctxvals = kmemdup(fw->data, fw->size, GFP_KERNEL);
  89. if (!pgraph->ctxvals) {
  90. NV_ERROR(dev, "OOM copying ctxvals\n");
  91. release_firmware(fw);
  92. nouveau_grctx_fini(dev);
  93. return -ENOMEM;
  94. }
  95. cv = (void *)pgraph->ctxvals;
  96. if (le32_to_cpu(cv->signature) != 0x5643564e ||
  97. cv->version != 0 ||
  98. le32_to_cpu(cv->length) != ((fw->size - 9) / 8)) {
  99. NV_ERROR(dev, "ctxvals invalid\n");
  100. release_firmware(fw);
  101. nouveau_grctx_fini(dev);
  102. return -EINVAL;
  103. }
  104. release_firmware(fw);
  105. }
  106. cp = pgraph->ctxprog;
  107. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
  108. for (i = 0; i < le16_to_cpu(cp->length); i++)
  109. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA,
  110. le32_to_cpu(cp->data[i]));
  111. return 0;
  112. }
  113. void
  114. nouveau_grctx_fini(struct drm_device *dev)
  115. {
  116. struct drm_nouveau_private *dev_priv = dev->dev_private;
  117. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  118. if (pgraph->ctxprog) {
  119. kfree(pgraph->ctxprog);
  120. pgraph->ctxprog = NULL;
  121. }
  122. if (pgraph->ctxvals) {
  123. kfree(pgraph->ctxprog);
  124. pgraph->ctxvals = NULL;
  125. }
  126. }
  127. void
  128. nouveau_grctx_vals_load(struct drm_device *dev, struct nouveau_gpuobj *ctx)
  129. {
  130. struct drm_nouveau_private *dev_priv = dev->dev_private;
  131. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  132. struct nouveau_ctxvals *cv = pgraph->ctxvals;
  133. int i;
  134. if (!cv)
  135. return;
  136. for (i = 0; i < le32_to_cpu(cv->length); i++)
  137. nv_wo32(dev, ctx, le32_to_cpu(cv->data[i].offset),
  138. le32_to_cpu(cv->data[i].value));
  139. }