nouveau_grctx.c 4.3 KB

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