acr_r352.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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 "acr_r352.h"
  23. #include <core/gpuobj.h>
  24. #include <core/firmware.h>
  25. #include <engine/falcon.h>
  26. /**
  27. * struct hsf_fw_header - HS firmware descriptor
  28. * @sig_dbg_offset: offset of the debug signature
  29. * @sig_dbg_size: size of the debug signature
  30. * @sig_prod_offset: offset of the production signature
  31. * @sig_prod_size: size of the production signature
  32. * @patch_loc: offset of the offset (sic) of where the signature is
  33. * @patch_sig: offset of the offset (sic) to add to sig_*_offset
  34. * @hdr_offset: offset of the load header (see struct hs_load_header)
  35. * @hdr_size: size of above header
  36. *
  37. * This structure is embedded in the HS firmware image at
  38. * hs_bin_hdr.header_offset.
  39. */
  40. struct hsf_fw_header {
  41. u32 sig_dbg_offset;
  42. u32 sig_dbg_size;
  43. u32 sig_prod_offset;
  44. u32 sig_prod_size;
  45. u32 patch_loc;
  46. u32 patch_sig;
  47. u32 hdr_offset;
  48. u32 hdr_size;
  49. };
  50. /**
  51. * struct acr_r352_flcn_bl_desc - DMEM bootloader descriptor
  52. * @signature: 16B signature for secure code. 0s if no secure code
  53. * @ctx_dma: DMA context to be used by BL while loading code/data
  54. * @code_dma_base: 256B-aligned Physical FB Address where code is located
  55. * (falcon's $xcbase register)
  56. * @non_sec_code_off: offset from code_dma_base where the non-secure code is
  57. * located. The offset must be multiple of 256 to help perf
  58. * @non_sec_code_size: the size of the nonSecure code part.
  59. * @sec_code_off: offset from code_dma_base where the secure code is
  60. * located. The offset must be multiple of 256 to help perf
  61. * @sec_code_size: offset from code_dma_base where the secure code is
  62. * located. The offset must be multiple of 256 to help perf
  63. * @code_entry_point: code entry point which will be invoked by BL after
  64. * code is loaded.
  65. * @data_dma_base: 256B aligned Physical FB Address where data is located.
  66. * (falcon's $xdbase register)
  67. * @data_size: size of data block. Should be multiple of 256B
  68. *
  69. * Structure used by the bootloader to load the rest of the code. This has
  70. * to be filled by host and copied into DMEM at offset provided in the
  71. * hsflcn_bl_desc.bl_desc_dmem_load_off.
  72. */
  73. struct acr_r352_flcn_bl_desc {
  74. u32 reserved[4];
  75. u32 signature[4];
  76. u32 ctx_dma;
  77. u32 code_dma_base;
  78. u32 non_sec_code_off;
  79. u32 non_sec_code_size;
  80. u32 sec_code_off;
  81. u32 sec_code_size;
  82. u32 code_entry_point;
  83. u32 data_dma_base;
  84. u32 data_size;
  85. u32 code_dma_base1;
  86. u32 data_dma_base1;
  87. };
  88. /**
  89. * acr_r352_generate_flcn_bl_desc - generate generic BL descriptor for LS image
  90. */
  91. static void
  92. acr_r352_generate_flcn_bl_desc(const struct nvkm_acr *acr,
  93. const struct ls_ucode_img *_img, u64 wpr_addr,
  94. void *_desc)
  95. {
  96. struct ls_ucode_img_r352 *img = ls_ucode_img_r352(_img);
  97. struct acr_r352_flcn_bl_desc *desc = _desc;
  98. const struct ls_ucode_img_desc *pdesc = &_img->ucode_desc;
  99. u64 base, addr_code, addr_data;
  100. base = wpr_addr + img->lsb_header.ucode_off + pdesc->app_start_offset;
  101. addr_code = (base + pdesc->app_resident_code_offset) >> 8;
  102. addr_data = (base + pdesc->app_resident_data_offset) >> 8;
  103. desc->ctx_dma = FALCON_DMAIDX_UCODE;
  104. desc->code_dma_base = lower_32_bits(addr_code);
  105. desc->code_dma_base1 = upper_32_bits(addr_code);
  106. desc->non_sec_code_off = pdesc->app_resident_code_offset;
  107. desc->non_sec_code_size = pdesc->app_resident_code_size;
  108. desc->code_entry_point = pdesc->app_imem_entry;
  109. desc->data_dma_base = lower_32_bits(addr_data);
  110. desc->data_dma_base1 = upper_32_bits(addr_data);
  111. desc->data_size = pdesc->app_resident_data_size;
  112. }
  113. /**
  114. * struct hsflcn_acr_desc - data section of the HS firmware
  115. *
  116. * This header is to be copied at the beginning of DMEM by the HS bootloader.
  117. *
  118. * @signature: signature of ACR ucode
  119. * @wpr_region_id: region ID holding the WPR header and its details
  120. * @wpr_offset: offset from the WPR region holding the wpr header
  121. * @regions: region descriptors
  122. * @nonwpr_ucode_blob_size: size of LS blob
  123. * @nonwpr_ucode_blob_start: FB location of LS blob is
  124. */
  125. struct hsflcn_acr_desc {
  126. union {
  127. u8 reserved_dmem[0x200];
  128. u32 signatures[4];
  129. } ucode_reserved_space;
  130. u32 wpr_region_id;
  131. u32 wpr_offset;
  132. u32 mmu_mem_range;
  133. #define FLCN_ACR_MAX_REGIONS 2
  134. struct {
  135. u32 no_regions;
  136. struct {
  137. u32 start_addr;
  138. u32 end_addr;
  139. u32 region_id;
  140. u32 read_mask;
  141. u32 write_mask;
  142. u32 client_mask;
  143. } region_props[FLCN_ACR_MAX_REGIONS];
  144. } regions;
  145. u32 ucode_blob_size;
  146. u64 ucode_blob_base __aligned(8);
  147. struct {
  148. u32 vpr_enabled;
  149. u32 vpr_start;
  150. u32 vpr_end;
  151. u32 hdcp_policies;
  152. } vpr_desc;
  153. };
  154. /*
  155. * Low-secure blob creation
  156. */
  157. /**
  158. * ls_ucode_img_load() - create a lsf_ucode_img and load it
  159. */
  160. struct ls_ucode_img *
  161. acr_r352_ls_ucode_img_load(const struct acr_r352 *acr,
  162. enum nvkm_secboot_falcon falcon_id)
  163. {
  164. const struct nvkm_subdev *subdev = acr->base.subdev;
  165. struct ls_ucode_img_r352 *img;
  166. int ret;
  167. img = kzalloc(sizeof(*img), GFP_KERNEL);
  168. if (!img)
  169. return ERR_PTR(-ENOMEM);
  170. img->base.falcon_id = falcon_id;
  171. ret = acr->func->ls_func[falcon_id]->load(subdev, &img->base);
  172. if (ret) {
  173. kfree(img->base.ucode_data);
  174. kfree(img->base.sig);
  175. kfree(img);
  176. return ERR_PTR(ret);
  177. }
  178. /* Check that the signature size matches our expectations... */
  179. if (img->base.sig_size != sizeof(img->lsb_header.signature)) {
  180. nvkm_error(subdev, "invalid signature size for %s falcon!\n",
  181. nvkm_secboot_falcon_name[falcon_id]);
  182. return ERR_PTR(-EINVAL);
  183. }
  184. /* Copy signature to the right place */
  185. memcpy(&img->lsb_header.signature, img->base.sig, img->base.sig_size);
  186. /* not needed? the signature should already have the right value */
  187. img->lsb_header.signature.falcon_id = falcon_id;
  188. return &img->base;
  189. }
  190. #define LSF_LSB_HEADER_ALIGN 256
  191. #define LSF_BL_DATA_ALIGN 256
  192. #define LSF_BL_DATA_SIZE_ALIGN 256
  193. #define LSF_BL_CODE_SIZE_ALIGN 256
  194. #define LSF_UCODE_DATA_ALIGN 4096
  195. /**
  196. * acr_r352_ls_img_fill_headers - fill the WPR and LSB headers of an image
  197. * @acr: ACR to use
  198. * @img: image to generate for
  199. * @offset: offset in the WPR region where this image starts
  200. *
  201. * Allocate space in the WPR area from offset and write the WPR and LSB headers
  202. * accordingly.
  203. *
  204. * Return: offset at the end of this image.
  205. */
  206. static u32
  207. acr_r352_ls_img_fill_headers(struct acr_r352 *acr,
  208. struct ls_ucode_img_r352 *img, u32 offset)
  209. {
  210. struct ls_ucode_img *_img = &img->base;
  211. struct acr_r352_lsf_wpr_header *whdr = &img->wpr_header;
  212. struct acr_r352_lsf_lsb_header *lhdr = &img->lsb_header;
  213. struct ls_ucode_img_desc *desc = &_img->ucode_desc;
  214. const struct acr_r352_ls_func *func =
  215. acr->func->ls_func[_img->falcon_id];
  216. /* Fill WPR header */
  217. whdr->falcon_id = _img->falcon_id;
  218. whdr->bootstrap_owner = acr->base.boot_falcon;
  219. whdr->status = LSF_IMAGE_STATUS_COPY;
  220. /* Skip bootstrapping falcons started by someone else than ACR */
  221. if (acr->lazy_bootstrap & BIT(_img->falcon_id))
  222. whdr->lazy_bootstrap = 1;
  223. /* Align, save off, and include an LSB header size */
  224. offset = ALIGN(offset, LSF_LSB_HEADER_ALIGN);
  225. whdr->lsb_offset = offset;
  226. offset += sizeof(*lhdr);
  227. /*
  228. * Align, save off, and include the original (static) ucode
  229. * image size
  230. */
  231. offset = ALIGN(offset, LSF_UCODE_DATA_ALIGN);
  232. lhdr->ucode_off = offset;
  233. offset += _img->ucode_size;
  234. /*
  235. * For falcons that use a boot loader (BL), we append a loader
  236. * desc structure on the end of the ucode image and consider
  237. * this the boot loader data. The host will then copy the loader
  238. * desc args to this space within the WPR region (before locking
  239. * down) and the HS bin will then copy them to DMEM 0 for the
  240. * loader.
  241. */
  242. lhdr->bl_code_size = ALIGN(desc->bootloader_size,
  243. LSF_BL_CODE_SIZE_ALIGN);
  244. lhdr->ucode_size = ALIGN(desc->app_resident_data_offset,
  245. LSF_BL_CODE_SIZE_ALIGN) + lhdr->bl_code_size;
  246. lhdr->data_size = ALIGN(desc->app_size, LSF_BL_CODE_SIZE_ALIGN) +
  247. lhdr->bl_code_size - lhdr->ucode_size;
  248. /*
  249. * Though the BL is located at 0th offset of the image, the VA
  250. * is different to make sure that it doesn't collide the actual
  251. * OS VA range
  252. */
  253. lhdr->bl_imem_off = desc->bootloader_imem_offset;
  254. lhdr->app_code_off = desc->app_start_offset +
  255. desc->app_resident_code_offset;
  256. lhdr->app_code_size = desc->app_resident_code_size;
  257. lhdr->app_data_off = desc->app_start_offset +
  258. desc->app_resident_data_offset;
  259. lhdr->app_data_size = desc->app_resident_data_size;
  260. lhdr->flags = func->lhdr_flags;
  261. if (_img->falcon_id == acr->base.boot_falcon)
  262. lhdr->flags |= LSF_FLAG_DMACTL_REQ_CTX;
  263. /* Align and save off BL descriptor size */
  264. lhdr->bl_data_size = ALIGN(func->bl_desc_size, LSF_BL_DATA_SIZE_ALIGN);
  265. /*
  266. * Align, save off, and include the additional BL data
  267. */
  268. offset = ALIGN(offset, LSF_BL_DATA_ALIGN);
  269. lhdr->bl_data_off = offset;
  270. offset += lhdr->bl_data_size;
  271. return offset;
  272. }
  273. /**
  274. * acr_r352_ls_fill_headers - fill WPR and LSB headers of all managed images
  275. */
  276. int
  277. acr_r352_ls_fill_headers(struct acr_r352 *acr, struct list_head *imgs)
  278. {
  279. struct ls_ucode_img_r352 *img;
  280. struct list_head *l;
  281. u32 count = 0;
  282. u32 offset;
  283. /* Count the number of images to manage */
  284. list_for_each(l, imgs)
  285. count++;
  286. /*
  287. * Start with an array of WPR headers at the base of the WPR.
  288. * The expectation here is that the secure falcon will do a single DMA
  289. * read of this array and cache it internally so it's ok to pack these.
  290. * Also, we add 1 to the falcon count to indicate the end of the array.
  291. */
  292. offset = sizeof(img->wpr_header) * (count + 1);
  293. /*
  294. * Walk the managed falcons, accounting for the LSB structs
  295. * as well as the ucode images.
  296. */
  297. list_for_each_entry(img, imgs, base.node) {
  298. offset = acr_r352_ls_img_fill_headers(acr, img, offset);
  299. }
  300. return offset;
  301. }
  302. /**
  303. * acr_r352_ls_write_wpr - write the WPR blob contents
  304. */
  305. int
  306. acr_r352_ls_write_wpr(struct acr_r352 *acr, struct list_head *imgs,
  307. struct nvkm_gpuobj *wpr_blob, u32 wpr_addr)
  308. {
  309. struct ls_ucode_img *_img;
  310. u32 pos = 0;
  311. nvkm_kmap(wpr_blob);
  312. list_for_each_entry(_img, imgs, node) {
  313. struct ls_ucode_img_r352 *img = ls_ucode_img_r352(_img);
  314. const struct acr_r352_ls_func *ls_func =
  315. acr->func->ls_func[_img->falcon_id];
  316. u8 gdesc[ls_func->bl_desc_size];
  317. nvkm_gpuobj_memcpy_to(wpr_blob, pos, &img->wpr_header,
  318. sizeof(img->wpr_header));
  319. nvkm_gpuobj_memcpy_to(wpr_blob, img->wpr_header.lsb_offset,
  320. &img->lsb_header, sizeof(img->lsb_header));
  321. /* Generate and write BL descriptor */
  322. memset(gdesc, 0, ls_func->bl_desc_size);
  323. ls_func->generate_bl_desc(&acr->base, _img, wpr_addr, gdesc);
  324. nvkm_gpuobj_memcpy_to(wpr_blob, img->lsb_header.bl_data_off,
  325. gdesc, ls_func->bl_desc_size);
  326. /* Copy ucode */
  327. nvkm_gpuobj_memcpy_to(wpr_blob, img->lsb_header.ucode_off,
  328. _img->ucode_data, _img->ucode_size);
  329. pos += sizeof(img->wpr_header);
  330. }
  331. nvkm_wo32(wpr_blob, pos, NVKM_SECBOOT_FALCON_INVALID);
  332. nvkm_done(wpr_blob);
  333. return 0;
  334. }
  335. /* Both size and address of WPR need to be 128K-aligned */
  336. #define WPR_ALIGNMENT 0x20000
  337. /**
  338. * acr_r352_prepare_ls_blob() - prepare the LS blob
  339. *
  340. * For each securely managed falcon, load the FW, signatures and bootloaders and
  341. * prepare a ucode blob. Then, compute the offsets in the WPR region for each
  342. * blob, and finally write the headers and ucode blobs into a GPU object that
  343. * will be copied into the WPR region by the HS firmware.
  344. */
  345. static int
  346. acr_r352_prepare_ls_blob(struct acr_r352 *acr, u64 wpr_addr, u32 wpr_size)
  347. {
  348. const struct nvkm_subdev *subdev = acr->base.subdev;
  349. struct list_head imgs;
  350. struct ls_ucode_img *img, *t;
  351. unsigned long managed_falcons = acr->base.managed_falcons;
  352. int managed_count = 0;
  353. u32 image_wpr_size;
  354. int falcon_id;
  355. int ret;
  356. INIT_LIST_HEAD(&imgs);
  357. /* Load all LS blobs */
  358. for_each_set_bit(falcon_id, &managed_falcons, NVKM_SECBOOT_FALCON_END) {
  359. struct ls_ucode_img *img;
  360. img = acr->func->ls_ucode_img_load(acr, falcon_id);
  361. if (IS_ERR(img)) {
  362. ret = PTR_ERR(img);
  363. goto cleanup;
  364. }
  365. list_add_tail(&img->node, &imgs);
  366. managed_count++;
  367. }
  368. /*
  369. * Fill the WPR and LSF headers with the right offsets and compute
  370. * required WPR size
  371. */
  372. image_wpr_size = acr->func->ls_fill_headers(acr, &imgs);
  373. image_wpr_size = ALIGN(image_wpr_size, WPR_ALIGNMENT);
  374. /* Allocate GPU object that will contain the WPR region */
  375. ret = nvkm_gpuobj_new(subdev->device, image_wpr_size, WPR_ALIGNMENT,
  376. false, NULL, &acr->ls_blob);
  377. if (ret)
  378. goto cleanup;
  379. nvkm_debug(subdev, "%d managed LS falcons, WPR size is %d bytes\n",
  380. managed_count, image_wpr_size);
  381. /* If WPR address and size are not fixed, set them to fit the LS blob */
  382. if (wpr_size == 0) {
  383. wpr_addr = acr->ls_blob->addr;
  384. wpr_size = image_wpr_size;
  385. /*
  386. * But if the WPR region is set by the bootloader, it is illegal for
  387. * the HS blob to be larger than this region.
  388. */
  389. } else if (image_wpr_size > wpr_size) {
  390. nvkm_error(subdev, "WPR region too small for FW blob!\n");
  391. nvkm_error(subdev, "required: %dB\n", image_wpr_size);
  392. nvkm_error(subdev, "available: %dB\n", wpr_size);
  393. ret = -ENOSPC;
  394. goto cleanup;
  395. }
  396. /* Write LS blob */
  397. ret = acr->func->ls_write_wpr(acr, &imgs, acr->ls_blob, wpr_addr);
  398. if (ret)
  399. nvkm_gpuobj_del(&acr->ls_blob);
  400. cleanup:
  401. list_for_each_entry_safe(img, t, &imgs, node) {
  402. kfree(img->ucode_data);
  403. kfree(img->sig);
  404. kfree(img);
  405. }
  406. return ret;
  407. }
  408. /**
  409. * acr_r352_hsf_patch_signature() - patch HS blob with correct signature
  410. */
  411. static void
  412. acr_r352_hsf_patch_signature(struct nvkm_secboot *sb, void *acr_image)
  413. {
  414. struct fw_bin_header *hsbin_hdr = acr_image;
  415. struct hsf_fw_header *fw_hdr = acr_image + hsbin_hdr->header_offset;
  416. void *hs_data = acr_image + hsbin_hdr->data_offset;
  417. void *sig;
  418. u32 sig_size;
  419. /* Falcon in debug or production mode? */
  420. if (sb->boot_falcon->debug) {
  421. sig = acr_image + fw_hdr->sig_dbg_offset;
  422. sig_size = fw_hdr->sig_dbg_size;
  423. } else {
  424. sig = acr_image + fw_hdr->sig_prod_offset;
  425. sig_size = fw_hdr->sig_prod_size;
  426. }
  427. /* Patch signature */
  428. memcpy(hs_data + fw_hdr->patch_loc, sig + fw_hdr->patch_sig, sig_size);
  429. }
  430. static void
  431. acr_r352_fixup_hs_desc(struct acr_r352 *acr, struct nvkm_secboot *sb,
  432. struct hsflcn_acr_desc *desc)
  433. {
  434. struct nvkm_gpuobj *ls_blob = acr->ls_blob;
  435. /* WPR region information if WPR is not fixed */
  436. if (sb->wpr_size == 0) {
  437. u32 wpr_start = ls_blob->addr;
  438. u32 wpr_end = wpr_start + ls_blob->size;
  439. desc->wpr_region_id = 1;
  440. desc->regions.no_regions = 2;
  441. desc->regions.region_props[0].start_addr = wpr_start >> 8;
  442. desc->regions.region_props[0].end_addr = wpr_end >> 8;
  443. desc->regions.region_props[0].region_id = 1;
  444. desc->regions.region_props[0].read_mask = 0xf;
  445. desc->regions.region_props[0].write_mask = 0xc;
  446. desc->regions.region_props[0].client_mask = 0x2;
  447. } else {
  448. desc->ucode_blob_base = ls_blob->addr;
  449. desc->ucode_blob_size = ls_blob->size;
  450. }
  451. }
  452. static void
  453. acr_r352_generate_hs_bl_desc(const struct hsf_load_header *hdr, void *_bl_desc,
  454. u64 offset)
  455. {
  456. struct acr_r352_flcn_bl_desc *bl_desc = _bl_desc;
  457. u64 addr_code, addr_data;
  458. addr_code = offset >> 8;
  459. addr_data = (offset + hdr->data_dma_base) >> 8;
  460. bl_desc->ctx_dma = FALCON_DMAIDX_VIRT;
  461. bl_desc->code_dma_base = lower_32_bits(addr_code);
  462. bl_desc->non_sec_code_off = hdr->non_sec_code_off;
  463. bl_desc->non_sec_code_size = hdr->non_sec_code_size;
  464. bl_desc->sec_code_off = hdr->app[0].sec_code_off;
  465. bl_desc->sec_code_size = hdr->app[0].sec_code_size;
  466. bl_desc->code_entry_point = 0;
  467. bl_desc->data_dma_base = lower_32_bits(addr_data);
  468. bl_desc->data_size = hdr->data_size;
  469. }
  470. /**
  471. * acr_r352_prepare_hs_blob - load and prepare a HS blob and BL descriptor
  472. *
  473. * @sb secure boot instance to prepare for
  474. * @fw name of the HS firmware to load
  475. * @blob pointer to gpuobj that will be allocated to receive the HS FW payload
  476. * @bl_desc pointer to the BL descriptor to write for this firmware
  477. * @patch whether we should patch the HS descriptor (only for HS loaders)
  478. */
  479. static int
  480. acr_r352_prepare_hs_blob(struct acr_r352 *acr, struct nvkm_secboot *sb,
  481. const char *fw, struct nvkm_gpuobj **blob,
  482. struct hsf_load_header *load_header, bool patch)
  483. {
  484. struct nvkm_subdev *subdev = &sb->subdev;
  485. void *acr_image;
  486. struct fw_bin_header *hsbin_hdr;
  487. struct hsf_fw_header *fw_hdr;
  488. struct hsf_load_header *load_hdr;
  489. void *acr_data;
  490. int ret;
  491. acr_image = nvkm_acr_load_firmware(subdev, fw, 0);
  492. if (IS_ERR(acr_image))
  493. return PTR_ERR(acr_image);
  494. hsbin_hdr = acr_image;
  495. fw_hdr = acr_image + hsbin_hdr->header_offset;
  496. load_hdr = acr_image + fw_hdr->hdr_offset;
  497. acr_data = acr_image + hsbin_hdr->data_offset;
  498. /* Patch signature */
  499. acr_r352_hsf_patch_signature(sb, acr_image);
  500. /* Patch descriptor with WPR information? */
  501. if (patch) {
  502. struct hsflcn_acr_desc *desc;
  503. desc = acr_data + load_hdr->data_dma_base;
  504. acr_r352_fixup_hs_desc(acr, sb, desc);
  505. }
  506. if (load_hdr->num_apps > ACR_R352_MAX_APPS) {
  507. nvkm_error(subdev, "more apps (%d) than supported (%d)!",
  508. load_hdr->num_apps, ACR_R352_MAX_APPS);
  509. ret = -EINVAL;
  510. goto cleanup;
  511. }
  512. memcpy(load_header, load_hdr, sizeof(*load_header) +
  513. (sizeof(load_hdr->app[0]) * load_hdr->num_apps));
  514. /* Create ACR blob and copy HS data to it */
  515. ret = nvkm_gpuobj_new(subdev->device, ALIGN(hsbin_hdr->data_size, 256),
  516. 0x1000, false, NULL, blob);
  517. if (ret)
  518. goto cleanup;
  519. nvkm_kmap(*blob);
  520. nvkm_gpuobj_memcpy_to(*blob, 0, acr_data, hsbin_hdr->data_size);
  521. nvkm_done(*blob);
  522. cleanup:
  523. kfree(acr_image);
  524. return ret;
  525. }
  526. static int
  527. acr_r352_prepare_hsbl_blob(struct acr_r352 *acr)
  528. {
  529. const struct nvkm_subdev *subdev = acr->base.subdev;
  530. struct fw_bin_header *hdr;
  531. struct fw_bl_desc *hsbl_desc;
  532. acr->hsbl_blob = nvkm_acr_load_firmware(subdev, "acr/bl", 0);
  533. if (IS_ERR(acr->hsbl_blob)) {
  534. int ret = PTR_ERR(acr->hsbl_blob);
  535. acr->hsbl_blob = NULL;
  536. return ret;
  537. }
  538. hdr = acr->hsbl_blob;
  539. hsbl_desc = acr->hsbl_blob + hdr->header_offset;
  540. /* virtual start address for boot vector */
  541. acr->base.start_address = hsbl_desc->start_tag << 8;
  542. return 0;
  543. }
  544. /**
  545. * acr_r352_load_blobs - load blobs common to all ACR V1 versions.
  546. *
  547. * This includes the LS blob, HS ucode loading blob, and HS bootloader.
  548. *
  549. * The HS ucode unload blob is only used on dGPU if the WPR region is variable.
  550. */
  551. int
  552. acr_r352_load_blobs(struct acr_r352 *acr, struct nvkm_secboot *sb)
  553. {
  554. int ret;
  555. /* Firmware already loaded? */
  556. if (acr->firmware_ok)
  557. return 0;
  558. /* Load and prepare the managed falcon's firmwares */
  559. ret = acr_r352_prepare_ls_blob(acr, sb->wpr_addr, sb->wpr_size);
  560. if (ret)
  561. return ret;
  562. /* Load the HS firmware that will load the LS firmwares */
  563. if (!acr->load_blob) {
  564. ret = acr_r352_prepare_hs_blob(acr, sb, "acr/ucode_load",
  565. &acr->load_blob,
  566. &acr->load_bl_header, true);
  567. if (ret)
  568. return ret;
  569. }
  570. /* If the ACR region is dynamically programmed, we need an unload FW */
  571. if (sb->wpr_size == 0) {
  572. ret = acr_r352_prepare_hs_blob(acr, sb, "acr/ucode_unload",
  573. &acr->unload_blob,
  574. &acr->unload_bl_header, false);
  575. if (ret)
  576. return ret;
  577. }
  578. /* Load the HS firmware bootloader */
  579. if (!acr->hsbl_blob) {
  580. ret = acr_r352_prepare_hsbl_blob(acr);
  581. if (ret)
  582. return ret;
  583. }
  584. acr->firmware_ok = true;
  585. nvkm_debug(&sb->subdev, "LS blob successfully created\n");
  586. return 0;
  587. }
  588. /**
  589. * acr_r352_load() - prepare HS falcon to run the specified blob, mapped
  590. * at GPU address offset.
  591. */
  592. static int
  593. acr_r352_load(struct nvkm_acr *_acr, struct nvkm_secboot *sb,
  594. struct nvkm_gpuobj *blob, u64 offset)
  595. {
  596. struct acr_r352 *acr = acr_r352(_acr);
  597. struct nvkm_falcon *falcon = sb->boot_falcon;
  598. struct fw_bin_header *hdr = acr->hsbl_blob;
  599. struct fw_bl_desc *hsbl_desc = acr->hsbl_blob + hdr->header_offset;
  600. void *blob_data = acr->hsbl_blob + hdr->data_offset;
  601. void *hsbl_code = blob_data + hsbl_desc->code_off;
  602. void *hsbl_data = blob_data + hsbl_desc->data_off;
  603. u32 code_size = ALIGN(hsbl_desc->code_size, 256);
  604. const struct hsf_load_header *load_hdr;
  605. const u32 bl_desc_size = acr->func->hs_bl_desc_size;
  606. u8 bl_desc[bl_desc_size];
  607. /* Find the bootloader descriptor for our blob and copy it */
  608. if (blob == acr->load_blob) {
  609. load_hdr = &acr->load_bl_header;
  610. } else if (blob == acr->unload_blob) {
  611. load_hdr = &acr->unload_bl_header;
  612. } else {
  613. nvkm_error(_acr->subdev, "invalid secure boot blob!\n");
  614. return -EINVAL;
  615. }
  616. /*
  617. * Copy HS bootloader data
  618. */
  619. nvkm_falcon_load_dmem(falcon, hsbl_data, 0x0, hsbl_desc->data_size, 0);
  620. /* Copy HS bootloader code to end of IMEM */
  621. nvkm_falcon_load_imem(falcon, hsbl_code, falcon->code.limit - code_size,
  622. code_size, hsbl_desc->start_tag, 0, false);
  623. /* Generate the BL header */
  624. memset(bl_desc, 0, bl_desc_size);
  625. acr->func->generate_hs_bl_desc(load_hdr, bl_desc, offset);
  626. /*
  627. * Copy HS BL header where the HS descriptor expects it to be
  628. */
  629. nvkm_falcon_load_dmem(falcon, bl_desc, hsbl_desc->dmem_load_off,
  630. bl_desc_size, 0);
  631. return 0;
  632. }
  633. static int
  634. acr_r352_shutdown(struct acr_r352 *acr, struct nvkm_secboot *sb)
  635. {
  636. int i;
  637. /* Run the unload blob to unprotect the WPR region */
  638. if (acr->unload_blob && sb->wpr_set) {
  639. int ret;
  640. nvkm_debug(&sb->subdev, "running HS unload blob\n");
  641. ret = sb->func->run_blob(sb, acr->unload_blob);
  642. if (ret)
  643. return ret;
  644. nvkm_debug(&sb->subdev, "HS unload blob completed\n");
  645. }
  646. for (i = 0; i < NVKM_SECBOOT_FALCON_END; i++)
  647. acr->falcon_state[i] = NON_SECURE;
  648. sb->wpr_set = false;
  649. return 0;
  650. }
  651. static int
  652. acr_r352_bootstrap(struct acr_r352 *acr, struct nvkm_secboot *sb)
  653. {
  654. int ret;
  655. if (sb->wpr_set)
  656. return 0;
  657. /* Make sure all blobs are ready */
  658. ret = acr_r352_load_blobs(acr, sb);
  659. if (ret)
  660. return ret;
  661. nvkm_debug(&sb->subdev, "running HS load blob\n");
  662. ret = sb->func->run_blob(sb, acr->load_blob);
  663. /* clear halt interrupt */
  664. nvkm_falcon_clear_interrupt(sb->boot_falcon, 0x10);
  665. if (ret)
  666. return ret;
  667. nvkm_debug(&sb->subdev, "HS load blob completed\n");
  668. sb->wpr_set = true;
  669. return 0;
  670. }
  671. /*
  672. * acr_r352_reset() - execute secure boot from the prepared state
  673. *
  674. * Load the HS bootloader and ask the falcon to run it. This will in turn
  675. * load the HS firmware and run it, so once the falcon stops all the managed
  676. * falcons should have their LS firmware loaded and be ready to run.
  677. */
  678. static int
  679. acr_r352_reset(struct nvkm_acr *_acr, struct nvkm_secboot *sb,
  680. enum nvkm_secboot_falcon falcon)
  681. {
  682. struct acr_r352 *acr = acr_r352(_acr);
  683. int ret;
  684. /*
  685. * Dummy GM200 implementation: perform secure boot each time we are
  686. * called on FECS. Since only FECS and GPCCS are managed and started
  687. * together, this ought to be safe.
  688. *
  689. * Once we have proper PMU firmware and support, this will be changed
  690. * to a proper call to the PMU method.
  691. */
  692. if (falcon != NVKM_SECBOOT_FALCON_FECS)
  693. goto end;
  694. ret = acr_r352_shutdown(acr, sb);
  695. if (ret)
  696. return ret;
  697. acr_r352_bootstrap(acr, sb);
  698. if (ret)
  699. return ret;
  700. end:
  701. acr->falcon_state[falcon] = RESET;
  702. return 0;
  703. }
  704. static int
  705. acr_r352_start(struct nvkm_acr *_acr, struct nvkm_secboot *sb,
  706. enum nvkm_secboot_falcon falcon)
  707. {
  708. struct acr_r352 *acr = acr_r352(_acr);
  709. const struct nvkm_subdev *subdev = &sb->subdev;
  710. int base;
  711. switch (falcon) {
  712. case NVKM_SECBOOT_FALCON_FECS:
  713. base = 0x409000;
  714. break;
  715. case NVKM_SECBOOT_FALCON_GPCCS:
  716. base = 0x41a000;
  717. break;
  718. default:
  719. nvkm_error(subdev, "cannot start unhandled falcon!\n");
  720. return -EINVAL;
  721. }
  722. nvkm_wr32(subdev->device, base + 0x130, 0x00000002);
  723. acr->falcon_state[falcon] = RUNNING;
  724. return 0;
  725. }
  726. static int
  727. acr_r352_fini(struct nvkm_acr *_acr, struct nvkm_secboot *sb, bool suspend)
  728. {
  729. struct acr_r352 *acr = acr_r352(_acr);
  730. return acr_r352_shutdown(acr, sb);
  731. }
  732. static void
  733. acr_r352_dtor(struct nvkm_acr *_acr)
  734. {
  735. struct acr_r352 *acr = acr_r352(_acr);
  736. nvkm_gpuobj_del(&acr->unload_blob);
  737. kfree(acr->hsbl_blob);
  738. nvkm_gpuobj_del(&acr->load_blob);
  739. nvkm_gpuobj_del(&acr->ls_blob);
  740. kfree(acr);
  741. }
  742. const struct acr_r352_ls_func
  743. acr_r352_ls_fecs_func = {
  744. .load = acr_ls_ucode_load_fecs,
  745. .generate_bl_desc = acr_r352_generate_flcn_bl_desc,
  746. .bl_desc_size = sizeof(struct acr_r352_flcn_bl_desc),
  747. };
  748. const struct acr_r352_ls_func
  749. acr_r352_ls_gpccs_func = {
  750. .load = acr_ls_ucode_load_gpccs,
  751. .generate_bl_desc = acr_r352_generate_flcn_bl_desc,
  752. .bl_desc_size = sizeof(struct acr_r352_flcn_bl_desc),
  753. /* GPCCS will be loaded using PRI */
  754. .lhdr_flags = LSF_FLAG_FORCE_PRIV_LOAD,
  755. };
  756. const struct acr_r352_func
  757. acr_r352_func = {
  758. .generate_hs_bl_desc = acr_r352_generate_hs_bl_desc,
  759. .hs_bl_desc_size = sizeof(struct acr_r352_flcn_bl_desc),
  760. .ls_ucode_img_load = acr_r352_ls_ucode_img_load,
  761. .ls_fill_headers = acr_r352_ls_fill_headers,
  762. .ls_write_wpr = acr_r352_ls_write_wpr,
  763. .ls_func = {
  764. [NVKM_SECBOOT_FALCON_FECS] = &acr_r352_ls_fecs_func,
  765. [NVKM_SECBOOT_FALCON_GPCCS] = &acr_r352_ls_gpccs_func,
  766. },
  767. };
  768. static const struct nvkm_acr_func
  769. acr_r352_base_func = {
  770. .dtor = acr_r352_dtor,
  771. .fini = acr_r352_fini,
  772. .load = acr_r352_load,
  773. .reset = acr_r352_reset,
  774. .start = acr_r352_start,
  775. };
  776. struct nvkm_acr *
  777. acr_r352_new_(const struct acr_r352_func *func,
  778. enum nvkm_secboot_falcon boot_falcon,
  779. unsigned long managed_falcons)
  780. {
  781. struct acr_r352 *acr;
  782. acr = kzalloc(sizeof(*acr), GFP_KERNEL);
  783. if (!acr)
  784. return ERR_PTR(-ENOMEM);
  785. acr->base.boot_falcon = boot_falcon;
  786. acr->base.managed_falcons = managed_falcons;
  787. acr->base.func = &acr_r352_base_func;
  788. acr->func = func;
  789. return &acr->base;
  790. }
  791. struct nvkm_acr *
  792. acr_r352_new(unsigned long managed_falcons)
  793. {
  794. return acr_r352_new_(&acr_r352_func, NVKM_SECBOOT_FALCON_PMU,
  795. managed_falcons);
  796. }