nouveau_grctx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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->ctxprog) {
  54. sprintf(name, "nouveau/nv%02x.ctxprog", chipset);
  55. ret = request_firmware(&fw, name, &dev->pdev->dev);
  56. if (ret) {
  57. NV_ERROR(dev, "No ctxprog for NV%02x\n", chipset);
  58. return ret;
  59. }
  60. pgraph->ctxprog = kmalloc(fw->size, GFP_KERNEL);
  61. if (!pgraph->ctxprog) {
  62. NV_ERROR(dev, "OOM copying ctxprog\n");
  63. release_firmware(fw);
  64. return -ENOMEM;
  65. }
  66. memcpy(pgraph->ctxprog, fw->data, fw->size);
  67. cp = pgraph->ctxprog;
  68. if (le32_to_cpu(cp->signature) != 0x5043564e ||
  69. cp->version != 0 ||
  70. le16_to_cpu(cp->length) != ((fw->size - 7) / 4)) {
  71. NV_ERROR(dev, "ctxprog invalid\n");
  72. release_firmware(fw);
  73. nouveau_grctx_fini(dev);
  74. return -EINVAL;
  75. }
  76. release_firmware(fw);
  77. }
  78. if (!pgraph->ctxvals) {
  79. sprintf(name, "nouveau/nv%02x.ctxvals", chipset);
  80. ret = request_firmware(&fw, name, &dev->pdev->dev);
  81. if (ret) {
  82. NV_ERROR(dev, "No ctxvals for NV%02x\n", chipset);
  83. nouveau_grctx_fini(dev);
  84. return ret;
  85. }
  86. pgraph->ctxvals = kmalloc(fw->size, GFP_KERNEL);
  87. if (!pgraph->ctxprog) {
  88. NV_ERROR(dev, "OOM copying ctxprog\n");
  89. release_firmware(fw);
  90. nouveau_grctx_fini(dev);
  91. return -ENOMEM;
  92. }
  93. memcpy(pgraph->ctxvals, fw->data, fw->size);
  94. cv = (void *)pgraph->ctxvals;
  95. if (le32_to_cpu(cv->signature) != 0x5643564e ||
  96. cv->version != 0 ||
  97. le32_to_cpu(cv->length) != ((fw->size - 9) / 8)) {
  98. NV_ERROR(dev, "ctxvals invalid\n");
  99. release_firmware(fw);
  100. nouveau_grctx_fini(dev);
  101. return -EINVAL;
  102. }
  103. release_firmware(fw);
  104. }
  105. cp = pgraph->ctxprog;
  106. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
  107. for (i = 0; i < le16_to_cpu(cp->length); i++)
  108. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA,
  109. le32_to_cpu(cp->data[i]));
  110. return 0;
  111. }
  112. void
  113. nouveau_grctx_fini(struct drm_device *dev)
  114. {
  115. struct drm_nouveau_private *dev_priv = dev->dev_private;
  116. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  117. if (pgraph->ctxprog) {
  118. kfree(pgraph->ctxprog);
  119. pgraph->ctxprog = NULL;
  120. }
  121. if (pgraph->ctxvals) {
  122. kfree(pgraph->ctxprog);
  123. pgraph->ctxvals = NULL;
  124. }
  125. }
  126. void
  127. nouveau_grctx_vals_load(struct drm_device *dev, struct nouveau_gpuobj *ctx)
  128. {
  129. struct drm_nouveau_private *dev_priv = dev->dev_private;
  130. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  131. struct nouveau_ctxvals *cv = pgraph->ctxvals;
  132. int i;
  133. if (!cv)
  134. return;
  135. for (i = 0; i < le32_to_cpu(cv->length); i++)
  136. nv_wo32(dev, ctx, le32_to_cpu(cv->data[i].offset),
  137. le32_to_cpu(cv->data[i].value));
  138. }