base.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /*
  23. * Secure boot is the process by which NVIDIA-signed firmware is loaded into
  24. * some of the falcons of a GPU. For production devices this is the only way
  25. * for the firmware to access useful (but sensitive) registers.
  26. *
  27. * A Falcon microprocessor supporting advanced security modes can run in one of
  28. * three modes:
  29. *
  30. * - Non-secure (NS). In this mode, functionality is similar to Falcon
  31. * architectures before security modes were introduced (pre-Maxwell), but
  32. * capability is restricted. In particular, certain registers may be
  33. * inaccessible for reads and/or writes, and physical memory access may be
  34. * disabled (on certain Falcon instances). This is the only possible mode that
  35. * can be used if you don't have microcode cryptographically signed by NVIDIA.
  36. *
  37. * - Heavy Secure (HS). In this mode, the microprocessor is a black box - it's
  38. * not possible to read or write any Falcon internal state or Falcon registers
  39. * from outside the Falcon (for example, from the host system). The only way
  40. * to enable this mode is by loading microcode that has been signed by NVIDIA.
  41. * (The loading process involves tagging the IMEM block as secure, writing the
  42. * signature into a Falcon register, and starting execution. The hardware will
  43. * validate the signature, and if valid, grant HS privileges.)
  44. *
  45. * - Light Secure (LS). In this mode, the microprocessor has more privileges
  46. * than NS but fewer than HS. Some of the microprocessor state is visible to
  47. * host software to ease debugging. The only way to enable this mode is by HS
  48. * microcode enabling LS mode. Some privileges available to HS mode are not
  49. * available here. LS mode is introduced in GM20x.
  50. *
  51. * Secure boot consists in temporarily switching a HS-capable falcon (typically
  52. * PMU) into HS mode in order to validate the LS firmwares of managed falcons,
  53. * load them, and switch managed falcons into LS mode. Once secure boot
  54. * completes, no falcon remains in HS mode.
  55. *
  56. * Secure boot requires a write-protected memory region (WPR) which can only be
  57. * written by the secure falcon. On dGPU, the driver sets up the WPR region in
  58. * video memory. On Tegra, it is set up by the bootloader and its location and
  59. * size written into memory controller registers.
  60. *
  61. * The secure boot process takes place as follows:
  62. *
  63. * 1) A LS blob is constructed that contains all the LS firmwares we want to
  64. * load, along with their signatures and bootloaders.
  65. *
  66. * 2) A HS blob (also called ACR) is created that contains the signed HS
  67. * firmware in charge of loading the LS firmwares into their respective
  68. * falcons.
  69. *
  70. * 3) The HS blob is loaded (via its own bootloader) and executed on the
  71. * HS-capable falcon. It authenticates itself, switches the secure falcon to
  72. * HS mode and setup the WPR region around the LS blob (dGPU) or copies the
  73. * LS blob into the WPR region (Tegra).
  74. *
  75. * 4) The LS blob is now secure from all external tampering. The HS falcon
  76. * checks the signatures of the LS firmwares and, if valid, switches the
  77. * managed falcons to LS mode and makes them ready to run the LS firmware.
  78. *
  79. * 5) The managed falcons remain in LS mode and can be started.
  80. *
  81. */
  82. #include "priv.h"
  83. #include "acr.h"
  84. #include <subdev/mc.h>
  85. #include <subdev/timer.h>
  86. #include <subdev/pmu.h>
  87. const char *
  88. nvkm_secboot_falcon_name[] = {
  89. [NVKM_SECBOOT_FALCON_PMU] = "PMU",
  90. [NVKM_SECBOOT_FALCON_RESERVED] = "<reserved>",
  91. [NVKM_SECBOOT_FALCON_FECS] = "FECS",
  92. [NVKM_SECBOOT_FALCON_GPCCS] = "GPCCS",
  93. [NVKM_SECBOOT_FALCON_END] = "<invalid>",
  94. };
  95. /**
  96. * nvkm_secboot_reset() - reset specified falcon
  97. */
  98. int
  99. nvkm_secboot_reset(struct nvkm_secboot *sb, enum nvkm_secboot_falcon falcon)
  100. {
  101. /* Unmanaged falcon? */
  102. if (!(BIT(falcon) & sb->acr->managed_falcons)) {
  103. nvkm_error(&sb->subdev, "cannot reset unmanaged falcon!\n");
  104. return -EINVAL;
  105. }
  106. return sb->acr->func->reset(sb->acr, sb, falcon);
  107. }
  108. /**
  109. * nvkm_secboot_is_managed() - check whether a given falcon is securely-managed
  110. */
  111. bool
  112. nvkm_secboot_is_managed(struct nvkm_secboot *sb, enum nvkm_secboot_falcon fid)
  113. {
  114. if (!sb)
  115. return false;
  116. return sb->acr->managed_falcons & BIT(fid);
  117. }
  118. static int
  119. nvkm_secboot_oneinit(struct nvkm_subdev *subdev)
  120. {
  121. struct nvkm_secboot *sb = nvkm_secboot(subdev);
  122. int ret = 0;
  123. switch (sb->acr->boot_falcon) {
  124. case NVKM_SECBOOT_FALCON_PMU:
  125. sb->boot_falcon = subdev->device->pmu->falcon;
  126. break;
  127. default:
  128. nvkm_error(subdev, "Unmanaged boot falcon %s!\n",
  129. nvkm_secboot_falcon_name[sb->acr->boot_falcon]);
  130. return -EINVAL;
  131. }
  132. /* Call chip-specific init function */
  133. if (sb->func->oneinit)
  134. ret = sb->func->oneinit(sb);
  135. if (ret) {
  136. nvkm_error(subdev, "Secure Boot initialization failed: %d\n",
  137. ret);
  138. return ret;
  139. }
  140. return 0;
  141. }
  142. static int
  143. nvkm_secboot_fini(struct nvkm_subdev *subdev, bool suspend)
  144. {
  145. struct nvkm_secboot *sb = nvkm_secboot(subdev);
  146. int ret = 0;
  147. if (sb->func->fini)
  148. ret = sb->func->fini(sb, suspend);
  149. return ret;
  150. }
  151. static void *
  152. nvkm_secboot_dtor(struct nvkm_subdev *subdev)
  153. {
  154. struct nvkm_secboot *sb = nvkm_secboot(subdev);
  155. void *ret = NULL;
  156. if (sb->func->dtor)
  157. ret = sb->func->dtor(sb);
  158. return ret;
  159. }
  160. static const struct nvkm_subdev_func
  161. nvkm_secboot = {
  162. .oneinit = nvkm_secboot_oneinit,
  163. .fini = nvkm_secboot_fini,
  164. .dtor = nvkm_secboot_dtor,
  165. };
  166. int
  167. nvkm_secboot_ctor(const struct nvkm_secboot_func *func, struct nvkm_acr *acr,
  168. struct nvkm_device *device, int index,
  169. struct nvkm_secboot *sb)
  170. {
  171. unsigned long fid;
  172. nvkm_subdev_ctor(&nvkm_secboot, device, index, &sb->subdev);
  173. sb->func = func;
  174. sb->acr = acr;
  175. acr->subdev = &sb->subdev;
  176. nvkm_debug(&sb->subdev, "securely managed falcons:\n");
  177. for_each_set_bit(fid, &sb->acr->managed_falcons,
  178. NVKM_SECBOOT_FALCON_END)
  179. nvkm_debug(&sb->subdev, "- %s\n",
  180. nvkm_secboot_falcon_name[fid]);
  181. return 0;
  182. }