PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/gpu/drm/nouveau/nvkm/subdev/secboot/ls_ucode_gr.c

https://github.com/gby/linux
C | 158 lines | 99 code | 21 blank | 38 comment | 7 complexity | 9f4b5acd8ac77315c529b17ebe3fc6fb MD5 | raw file
  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. #include "ls_ucode.h"
  23. #include "acr.h"
  24. #include <core/firmware.h>
  25. #define BL_DESC_BLK_SIZE 256
  26. /**
  27. * Build a ucode image and descriptor from provided bootloader, code and data.
  28. *
  29. * @bl: bootloader image, including 16-bytes descriptor
  30. * @code: LS firmware code segment
  31. * @data: LS firmware data segment
  32. * @desc: ucode descriptor to be written
  33. *
  34. * Return: allocated ucode image with corresponding descriptor information. desc
  35. * is also updated to contain the right offsets within returned image.
  36. */
  37. static void *
  38. ls_ucode_img_build(const struct firmware *bl, const struct firmware *code,
  39. const struct firmware *data, struct ls_ucode_img_desc *desc)
  40. {
  41. struct fw_bin_header *bin_hdr = (void *)bl->data;
  42. struct fw_bl_desc *bl_desc = (void *)bl->data + bin_hdr->header_offset;
  43. void *bl_data = (void *)bl->data + bin_hdr->data_offset;
  44. u32 pos = 0;
  45. void *image;
  46. desc->bootloader_start_offset = pos;
  47. desc->bootloader_size = ALIGN(bl_desc->code_size, sizeof(u32));
  48. desc->bootloader_imem_offset = bl_desc->start_tag * 256;
  49. desc->bootloader_entry_point = bl_desc->start_tag * 256;
  50. pos = ALIGN(pos + desc->bootloader_size, BL_DESC_BLK_SIZE);
  51. desc->app_start_offset = pos;
  52. desc->app_size = ALIGN(code->size, BL_DESC_BLK_SIZE) +
  53. ALIGN(data->size, BL_DESC_BLK_SIZE);
  54. desc->app_imem_offset = 0;
  55. desc->app_imem_entry = 0;
  56. desc->app_dmem_offset = 0;
  57. desc->app_resident_code_offset = 0;
  58. desc->app_resident_code_size = ALIGN(code->size, BL_DESC_BLK_SIZE);
  59. pos = ALIGN(pos + desc->app_resident_code_size, BL_DESC_BLK_SIZE);
  60. desc->app_resident_data_offset = pos - desc->app_start_offset;
  61. desc->app_resident_data_size = ALIGN(data->size, BL_DESC_BLK_SIZE);
  62. desc->image_size = ALIGN(bl_desc->code_size, BL_DESC_BLK_SIZE) +
  63. desc->app_size;
  64. image = kzalloc(desc->image_size, GFP_KERNEL);
  65. if (!image)
  66. return ERR_PTR(-ENOMEM);
  67. memcpy(image + desc->bootloader_start_offset, bl_data,
  68. bl_desc->code_size);
  69. memcpy(image + desc->app_start_offset, code->data, code->size);
  70. memcpy(image + desc->app_start_offset + desc->app_resident_data_offset,
  71. data->data, data->size);
  72. return image;
  73. }
  74. /**
  75. * ls_ucode_img_load_gr() - load and prepare a LS GR ucode image
  76. *
  77. * Load the LS microcode, bootloader and signature and pack them into a single
  78. * blob. Also generate the corresponding ucode descriptor.
  79. */
  80. static int
  81. ls_ucode_img_load_gr(const struct nvkm_subdev *subdev, struct ls_ucode_img *img,
  82. const char *falcon_name)
  83. {
  84. const struct firmware *bl, *code, *data, *sig;
  85. char f[64];
  86. int ret;
  87. snprintf(f, sizeof(f), "gr/%s_bl", falcon_name);
  88. ret = nvkm_firmware_get(subdev->device, f, &bl);
  89. if (ret)
  90. goto error;
  91. snprintf(f, sizeof(f), "gr/%s_inst", falcon_name);
  92. ret = nvkm_firmware_get(subdev->device, f, &code);
  93. if (ret)
  94. goto free_bl;
  95. snprintf(f, sizeof(f), "gr/%s_data", falcon_name);
  96. ret = nvkm_firmware_get(subdev->device, f, &data);
  97. if (ret)
  98. goto free_inst;
  99. snprintf(f, sizeof(f), "gr/%s_sig", falcon_name);
  100. ret = nvkm_firmware_get(subdev->device, f, &sig);
  101. if (ret)
  102. goto free_data;
  103. img->sig = kmemdup(sig->data, sig->size, GFP_KERNEL);
  104. if (!img->sig) {
  105. ret = -ENOMEM;
  106. goto free_sig;
  107. }
  108. img->sig_size = sig->size;
  109. img->ucode_data = ls_ucode_img_build(bl, code, data,
  110. &img->ucode_desc);
  111. if (IS_ERR(img->ucode_data)) {
  112. kfree(img->sig);
  113. ret = PTR_ERR(img->ucode_data);
  114. goto free_sig;
  115. }
  116. img->ucode_size = img->ucode_desc.image_size;
  117. free_sig:
  118. nvkm_firmware_put(sig);
  119. free_data:
  120. nvkm_firmware_put(data);
  121. free_inst:
  122. nvkm_firmware_put(code);
  123. free_bl:
  124. nvkm_firmware_put(bl);
  125. error:
  126. return ret;
  127. }
  128. int
  129. acr_ls_ucode_load_fecs(const struct nvkm_secboot *sb, struct ls_ucode_img *img)
  130. {
  131. return ls_ucode_img_load_gr(&sb->subdev, img, "fecs");
  132. }
  133. int
  134. acr_ls_ucode_load_gpccs(const struct nvkm_secboot *sb, struct ls_ucode_img *img)
  135. {
  136. return ls_ucode_img_load_gr(&sb->subdev, img, "gpccs");
  137. }