nouveau_grctx.c 4.3 KB

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