ccci_lk_load_img_plat.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <sys/types.h>
  32. #include <stdint.h>
  33. #include <platform/partition.h>
  34. #include <platform/mt_typedefs.h>
  35. #include <platform/boot_mode.h>
  36. #include <platform/mt_reg_base.h>
  37. #include <platform/errno.h>
  38. #include <printf.h>
  39. #include <string.h>
  40. #include <malloc.h>
  41. #include <libfdt.h>
  42. #include <platform/mt_gpt.h>
  43. #include <platform/mt_emi_mpu.h>
  44. #include <debug.h>
  45. #define MODULE_NAME "LK_LD_MD"
  46. #include "ccci_ld_md_core.h"
  47. #include "ccci_ld_md_errno.h"
  48. #ifdef MTK_SMC_ID_MGMT
  49. #include "mtk_secure_api.h"
  50. #endif
  51. #define CCCI_SMEM_SIZE_CCB_DHL (2*1024*1024)
  52. #define CCCI_SMEM_SIZE_RAW_DHL (20*1024*1024)
  53. #define CCCI_SMEM_SIZE_LWA (0) // (8*1024*1024)
  54. #define CCCI_SMEM_SIZE_PHY_C_L0 (0*1024*1024)
  55. #define CCCI_SMEM_SIZE_PHY_C_L1 (32*1024*1024)
  56. #define CCCI_SMEM_SIZE_PHY_C_L2 (64*1024*1024)
  57. #define CCCI_SMEM_SIZE_PHY_C_L3 (128*1024*1024)
  58. #define CCCI_SMEM_SIZE_PHY_C_L4 (222*1024*1024)
  59. #define CCB_DATA_BUF_DEFAULT_GEAR 1
  60. #define CCB_DATA_BUF_SIZE (CCCI_SMEM_SIZE_CCB_DHL + CCCI_SMEM_SIZE_RAW_DHL)
  61. /***************************************************************************************************
  62. ** Feature Option setting part
  63. ***************************************************************************************************/
  64. #define ENABLE_EMI_PROTECTION
  65. /***************************************************************************************************
  66. ** HW remap section
  67. ***************************************************************************************************/
  68. #if WITH_GZ_MD_SHAREMEM
  69. extern BOOT_ARGUMENT *g_boot_arg;
  70. #endif
  71. extern unsigned int ddr_enable_4gb(void)__attribute__((weak));
  72. static int is_4gb_ddr_support_en(void)
  73. {
  74. int ret;
  75. if (ddr_enable_4gb) {
  76. ret = ddr_enable_4gb();
  77. ALWAYS_LOG("ddr_enable_4GB sta:%d\n", ret);
  78. return ret;
  79. } else {
  80. ALWAYS_LOG("ddr 4GB disable\n");
  81. return 0;
  82. }
  83. }
  84. /*-------- Register base part -------------------------------*/
  85. /* HW remap for MD1 */
  86. #define INFRA_AO_BASE (0x10001000)
  87. /* -- MD1 Bank 0 */
  88. #define MD1_BANK0_MAP0 (INFRA_AO_BASE + 0x300)
  89. #define MD1_BANK0_MAP1 (INFRA_AO_BASE + 0x304)
  90. #define MD1_BANK0_MAP2 (INFRA_AO_BASE + 0x308)
  91. #define MD1_BANK0_MAP3 (INFRA_AO_BASE + 0x30C)
  92. /* -- MD1 Bank 1 */
  93. #define MD1_BANK1_MAP0 (INFRA_AO_BASE + 0x310)
  94. #define MD1_BANK1_MAP1 (INFRA_AO_BASE + 0x314)
  95. #define MD1_BANK1_MAP2 (INFRA_AO_BASE + 0x318)
  96. #define MD1_BANK1_MAP3 (INFRA_AO_BASE + 0x31C)
  97. /* -- MD1 Bank 4 */
  98. #define MD1_BANK4_MAP0 (INFRA_AO_BASE + 0x320)
  99. #define MD1_BANK4_MAP1 (INFRA_AO_BASE + 0x324)
  100. #define MD1_BANK4_MAP2 (INFRA_AO_BASE + 0x328)
  101. #define MD1_BANK4_MAP3 (INFRA_AO_BASE + 0x32C)
  102. /* HW remap lock register */
  103. #define MD_HW_REMAP_LOCK (INFRA_AO_BASE + 0xF80)
  104. #define MD1_LOCK (1<<16)
  105. static int md_mem_ro_rw_remapping(unsigned int md_id, unsigned long long addr)
  106. {
  107. unsigned long long md_img_start_addr;
  108. unsigned int hw_remapping_bank0_map0 = 0;
  109. unsigned int hw_remapping_bank0_map1 = 0;
  110. unsigned int hw_remapping_bank0_map2 = 0;
  111. unsigned int hw_remapping_bank0_map3 = 0;
  112. unsigned int write_val;
  113. switch (md_id) {
  114. case 0: // MD1
  115. hw_remapping_bank0_map0 = MD1_BANK0_MAP0;
  116. hw_remapping_bank0_map1 = MD1_BANK0_MAP1;
  117. hw_remapping_bank0_map2 = MD1_BANK0_MAP2;
  118. hw_remapping_bank0_map3 = MD1_BANK0_MAP3;
  119. break;
  120. default:
  121. ALWAYS_LOG("Invalid md id:%d\n", md_id);
  122. return -1;
  123. }
  124. md_img_start_addr = addr;
  125. ALWAYS_LOG("---> Map 0x00000000 to 0x%llx for MD%d\n", addr, md_id+1);
  126. /* For MDx_BANK0_MAP0 */
  127. write_val = (((md_img_start_addr >> 24) | 0x1) & 0x3FF)
  128. + ((((md_img_start_addr + 0x2000000*1) >> 8) | 1<<16) & 0x3FF0000);
  129. DRV_WriteReg32(hw_remapping_bank0_map0, write_val);
  130. ALWAYS_LOG("BANK0_MAP0 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map0));
  131. /* For MDx_BANK0_MAP1 */
  132. write_val = ((((md_img_start_addr + 0x2000000*2) >> 24) | 0x1) & 0x3FF)
  133. + ((((md_img_start_addr + 0x2000000*3) >> 8) | 1<<16) & 0x3FF0000);
  134. DRV_WriteReg32(hw_remapping_bank0_map1, write_val);
  135. ALWAYS_LOG("BANK0_MAP1 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map1));
  136. /* For MDx_BANK0_MAP2 */
  137. write_val = ((((md_img_start_addr + 0x2000000*4) >> 24) | 0x1) & 0x3FF)
  138. + ((((md_img_start_addr + 0x2000000*5) >> 8) | 1<<16) & 0x3FF0000);
  139. DRV_WriteReg32(hw_remapping_bank0_map2, write_val);
  140. ALWAYS_LOG("BANK0_MAP2 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map2));
  141. /* For MDx_BANK0_MAP3 */
  142. write_val = ((((md_img_start_addr + 0x2000000*6) >> 24) | 0x1) & 0x3FF)
  143. + ((((md_img_start_addr + 0x2000000*7) >> 8) | 1<<16) & 0x3FF0000);
  144. DRV_WriteReg32(hw_remapping_bank0_map3, write_val);
  145. ALWAYS_LOG("BANK0_MAP3 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map3));
  146. #ifdef DUMMY_AP_MODE
  147. /* For 256~512MB */
  148. if (md_id == MD_SYS1) {
  149. write_val = ((((md_img_start_addr + 0x2000000*8) >> 24) | 0x1) & 0x3FF)
  150. + ((((md_img_start_addr + 0x2000000*9) >> 8) | 1<<16) & 0x3FF0000);
  151. DRV_WriteReg32(MD1_BANK1_MAP0, write_val);
  152. ALWAYS_LOG("BANK1_MAP0 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP0));
  153. write_val = ((((md_img_start_addr + 0x2000000*10) >> 24) | 0x1) & 0x3FF)
  154. + ((((md_img_start_addr + 0x2000000*11) >> 8) | 1<<16) & 0x3FF0000);
  155. DRV_WriteReg32(MD1_BANK1_MAP1, write_val);
  156. ALWAYS_LOG("BANK1_MAP1 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP1));
  157. write_val = ((((md_img_start_addr + 0x2000000*12) >> 24) | 0x1) & 0x3FF)
  158. + ((((md_img_start_addr + 0x2000000*13) >> 8) | 1<<16) & 0x3FF0000);
  159. DRV_WriteReg32(MD1_BANK1_MAP2, write_val);
  160. ALWAYS_LOG("BANK1_MAP2 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP2));
  161. write_val = ((((md_img_start_addr + 0x2000000*14) >> 24) | 0x1) & 0x3FF)
  162. + ((((md_img_start_addr + 0x2000000*15) >> 8) | 1<<16) & 0x3FF0000);
  163. DRV_WriteReg32(MD1_BANK1_MAP3, write_val);
  164. ALWAYS_LOG("BANK1_MAP3 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP3));
  165. }
  166. #endif
  167. return 0;
  168. }
  169. static int md_bank4_remapping_by_slot(unsigned int md_id, unsigned long long addr, int slot)
  170. {
  171. unsigned int hw_remapping_bank4_map0 = 0;
  172. unsigned int hw_remapping_bank4_map1 = 0;
  173. unsigned int hw_remapping_bank4_map2 = 0;
  174. unsigned int hw_remapping_bank4_map3 = 0;
  175. unsigned int curr_val;
  176. switch (md_id) {
  177. case 0: // MD1
  178. hw_remapping_bank4_map0 = MD1_BANK4_MAP0;
  179. hw_remapping_bank4_map1 = MD1_BANK4_MAP1;
  180. hw_remapping_bank4_map2 = MD1_BANK4_MAP2;
  181. hw_remapping_bank4_map3 = MD1_BANK4_MAP3;
  182. break;
  183. default:
  184. ALWAYS_LOG("Invalid md id:%d\n", md_id);
  185. return -1;
  186. }
  187. switch(slot) {
  188. case 0:
  189. curr_val = DRV_Reg32(hw_remapping_bank4_map0);
  190. curr_val &= ~0x3FF;
  191. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  192. DRV_WriteReg32(hw_remapping_bank4_map0, curr_val);
  193. ALWAYS_LOG("BANK4_MAP0 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map0));
  194. break;
  195. case 1:
  196. curr_val = DRV_Reg32(hw_remapping_bank4_map0);
  197. curr_val &= ~0x3FF0000;
  198. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  199. DRV_WriteReg32(hw_remapping_bank4_map0, curr_val);
  200. ALWAYS_LOG("BANK4_MAP0 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map0));
  201. break;
  202. case 2:
  203. curr_val = DRV_Reg32(hw_remapping_bank4_map1);
  204. curr_val &= ~0x3FF;
  205. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  206. DRV_WriteReg32(hw_remapping_bank4_map1, curr_val);
  207. ALWAYS_LOG("BANK4_MAP1 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map1));
  208. break;
  209. case 3:
  210. curr_val = DRV_Reg32(hw_remapping_bank4_map1);
  211. curr_val &= ~0x3FF0000;
  212. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  213. DRV_WriteReg32(hw_remapping_bank4_map1, curr_val);
  214. ALWAYS_LOG("BANK4_MAP1 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map1));
  215. break;
  216. case 4:
  217. curr_val = DRV_Reg32(hw_remapping_bank4_map2);
  218. curr_val &= ~0x3FF;
  219. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  220. DRV_WriteReg32(hw_remapping_bank4_map2, curr_val);
  221. ALWAYS_LOG("BANK4_MAP2 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map2));
  222. break;
  223. case 5:
  224. curr_val = DRV_Reg32(hw_remapping_bank4_map2);
  225. curr_val &= ~0x3FF0000;
  226. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  227. DRV_WriteReg32(hw_remapping_bank4_map2, curr_val);
  228. ALWAYS_LOG("BANK4_MAP2 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map2));
  229. break;
  230. case 6:
  231. curr_val = DRV_Reg32(hw_remapping_bank4_map3);
  232. curr_val &= ~0x3FF;
  233. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  234. DRV_WriteReg32(hw_remapping_bank4_map3, curr_val);
  235. ALWAYS_LOG("BANK4_MAP3 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map3));
  236. break;
  237. case 7:
  238. curr_val = DRV_Reg32(hw_remapping_bank4_map3);
  239. curr_val &= ~0x3FF0000;
  240. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  241. DRV_WriteReg32(hw_remapping_bank4_map3, curr_val);
  242. ALWAYS_LOG("BANK4_MAP3 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map3));
  243. break;
  244. default:
  245. ALWAYS_LOG("Invalid slot id:%d\n", slot);
  246. return -1;
  247. }
  248. return 0;
  249. }
  250. static int md_smem_rw_remapping(unsigned int md_id, unsigned long long addr)
  251. {
  252. unsigned int i;
  253. ALWAYS_LOG("---> Map 0x40000000 to 0x%llx for MD%d\n", addr, md_id+1);
  254. for (i = 0; i < 8; i++)
  255. md_bank4_remapping_by_slot(md_id, addr + 0x2000000*i, i);
  256. return 0;
  257. }
  258. static void md_emi_remapping_lock(unsigned int md_id)
  259. {
  260. unsigned int reg_val;
  261. unsigned int lock_bit;
  262. switch (md_id) {
  263. case 0: // MD1
  264. lock_bit = MD1_LOCK;
  265. break;
  266. default:
  267. ALWAYS_LOG("Invalid md id:%d for lock\n", md_id);
  268. return;
  269. }
  270. reg_val = DRV_Reg32(MD_HW_REMAP_LOCK);
  271. ALWAYS_LOG("before hw remap lock: MD1[%d]\n", !!(reg_val&MD1_LOCK));
  272. DRV_WriteReg32(MD_HW_REMAP_LOCK, (reg_val|lock_bit));
  273. reg_val = DRV_Reg32(MD_HW_REMAP_LOCK);
  274. ALWAYS_LOG("before hw remap lock: MD1[%d]\n", !!(reg_val&MD1_LOCK));
  275. }
  276. /* =================================================== */
  277. /* MPU Region defination */
  278. /* =================================================== */
  279. /* Note: This structure should sync with Kernel!!!! */
  280. typedef unsigned long long mpu_att_t;
  281. typedef struct _mpu_cfg {
  282. unsigned int start;
  283. unsigned int end;
  284. int region;
  285. unsigned int permission[EMI_MPU_DGROUP_NUM];
  286. int relate_region;
  287. } mpu_cfg_t;
  288. #define MPU_REGION_ID_MD1_ROM 11
  289. #define MPU_REGION_ID_MD_DSP1 12 /*DSP RO*/
  290. #define MPU_REGION_ID_MD_DSP2 13 /*DSP RW*/
  291. #define MPU_REGION_ID_MD_DRDI 14
  292. #define MPU_REGION_ID_MD1_MCURW_HWRW 15
  293. #define MPU_REGION_ID_MD1_MCURW_HWRO 16
  294. #define MPU_REGION_ID_MD1_MCURO_HWRW 17
  295. #define MPU_REGION_ID_PADDING2 18
  296. #define MPU_REGION_ID_PADDING3 19
  297. #define MPU_REGION_ID_PADDING4 20
  298. #define MPU_REGION_ID_PADDING5 21
  299. #define MPU_REGION_ID_MD_MTEE 22
  300. #define MPU_REGION_ID_MD1_CCB 23
  301. #define MPU_REGION_ID_MD1_SMEM 24
  302. #define MPU_REGION_ID_MD_PROTECT 28
  303. #define MPU_REGION_ID_AP 31
  304. #define MPU_REGION_ID_TOTAL_NUM (MPU_REGION_ID_AP + 1)
  305. #define MPU_MDOMAIN_ID_AP 0
  306. #define MPU_MDOMAIN_ID_MD1 1
  307. #define MPU_MDOMAIN_ID_MDHW 7
  308. #define MPU_MDOMAIN_ID_TOTAL_NUM 16
  309. static const mpu_att_t mpu_att_default[MPU_REGION_ID_TOTAL_NUM][MPU_MDOMAIN_ID_TOTAL_NUM] = {
  310. /*===================================================================================================================*/
  311. /* No | | D0(AP) | D1(MD1) | D2(CONN) | D3(SCP) | D4(MM) | D5(Rsv ) | D6(MFG) | D7(MDHW)
  312. |D8(SSPM) |D9(SPM) |D10-15(Rsv) */
  313. /*--------------+-----------------------------------------------------------------------------------------------------------------------*/
  314. /* 0*/{}, /*Secure OS*/
  315. /* 1*/{}, /*ATF*/
  316. /* 2*/{}, /*Secure Memory*/
  317. /* 3*/{}, /*Tinysys-SSPM ROM*/
  318. /* 4*/{}, /*Tinysys-SSPM share buffer*/
  319. /* 5*/{}, /*Tinysys-SCP ROM*/
  320. /* 6*/{}, /*Tinysys-SCP share buffer*/
  321. /* 7*/{}, /*Trusted UI*/
  322. /* 8*/{}, /*MD-Dynamic*/
  323. /* 9*/{}, /*Security CCCI share memory(MD0 Secure Share)*/
  324. /*10*/{ SEC_RW, NO_PROTECTION, [2 ... 15] = FORBIDDEN},
  325. /*11*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN},
  326. /*12*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN},
  327. /*13*/{ SEC_R_NSEC_R, NO_PROTECTION, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  328. /*14*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN}, /*DRDI*/
  329. /*15*/{ SEC_R_NSEC_R, NO_PROTECTION, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  330. /*16*/{ SEC_R_NSEC_R, NO_PROTECTION, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN},
  331. /*17*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  332. /*18*/{ SEC_R_NSEC_R, FORBIDDEN, [2 ... 15] = FORBIDDEN},
  333. /*19*/{ SEC_R_NSEC_R, FORBIDDEN, [2 ... 15] = FORBIDDEN},
  334. /*20*/{ NO_PROTECTION, FORBIDDEN, [2 ... 15] = FORBIDDEN},
  335. /*21*/{},
  336. /*22*/{ SEC_R_NSEC_R, NO_PROTECTION, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  337. /*23*/{ NO_PROTECTION, NO_PROTECTION, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  338. /*24*/{ NO_PROTECTION, NO_PROTECTION, FORBIDDEN, NO_PROTECTION, [4 ... 6] = FORBIDDEN, NO_PROTECTION,
  339. [8 ... 15] = FORBIDDEN},
  340. /*25*/{}, /*WIFI EMI FW*/
  341. /*26*/{}, /*WMT*/
  342. /*27*/{}, /*Consys-MD share memory*/
  343. /*28*/{ NO_PROTECTION, SEC_R_NSEC_R, [2 ... 3] = FORBIDDEN, NO_PROTECTION, FORBIDDEN, SEC_R_NSEC_RW,
  344. FORBIDDEN, NO_PROTECTION, NO_PROTECTION, [10 ... 15] = FORBIDDEN},
  345. /*29*/{}, /*Reserved*/
  346. /*30*/{}, /*GPS offload*/
  347. /*31*/{}, /*AP*/
  348. };
  349. #define MPU_STR_BUF_SIZE 64
  350. static void get_mpu_attr_str(int lock, unsigned int apc[EMI_MPU_DGROUP_NUM], char buf[], int size)
  351. {
  352. unsigned long long curr_attr;
  353. char ch = lock?'L':'U';
  354. if (EMI_MPU_DGROUP_NUM != 2) {
  355. ALWAYS_LOG("[error]abnormal mpu domain group number %d\n", EMI_MPU_DGROUP_NUM);
  356. return;
  357. }
  358. curr_attr = ((unsigned long long)apc[1] << 32) | apc[0];
  359. snprintf(buf, size, "%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld(%c)",
  360. curr_attr&7, (curr_attr>>3)&7, (curr_attr>>6)&7, (curr_attr>>9)&7,
  361. (curr_attr>>12)&7, (curr_attr>>15)&7, (curr_attr>>18)&7, (curr_attr>>21)&7,
  362. (curr_attr>>32)&7, (curr_attr>>35)&7, (curr_attr>>38)&7, (curr_attr>>41)&7,
  363. (curr_attr>>44)&7, (curr_attr>>47)&7, (curr_attr>>50)&7, (curr_attr>>53)&7, ch);
  364. }
  365. static const unsigned char region_mapping_at_hdr_md1[] = {
  366. MPU_REGION_ID_MD1_ROM, MPU_REGION_ID_MD1_MCURO_HWRW, MPU_REGION_ID_MD1_MCURW_HWRO,
  367. MPU_REGION_ID_MD1_MCURW_HWRW
  368. };
  369. static const int free_mpu_region[] = {MPU_REGION_ID_PADDING2,
  370. MPU_REGION_ID_PADDING3, MPU_REGION_ID_PADDING4, MPU_REGION_ID_MD_PROTECT, -1};
  371. static int curr_free_mpu_idx;
  372. static int get_free_mpu_region(void)
  373. {
  374. int ret;
  375. if (curr_free_mpu_idx < (int)(sizeof(free_mpu_region)/sizeof(int))) {
  376. ret = free_mpu_region[curr_free_mpu_idx];
  377. curr_free_mpu_idx++;
  378. } else
  379. ret = -LD_ERR_PLAT_MPU_REGION_EMPTY;
  380. return ret;
  381. }
  382. /*make sure protect region is the last valid region*/
  383. static int get_md_protect_mpu_region(void)
  384. {
  385. int last_index;
  386. last_index = (int)(sizeof(free_mpu_region)/sizeof(int)) -1;
  387. if (free_mpu_region[last_index] < 0) /*free region end by -1*/
  388. last_index--; /*make sure it is index of last valid region*/
  389. if (curr_free_mpu_idx > last_index)
  390. return -LD_ERR_PLAT_MPU_REGION_EMPTY;
  391. return free_mpu_region[last_index];
  392. }
  393. static void get_mpu_region_default_access_att(
  394. unsigned int apc[EMI_MPU_DGROUP_NUM], int region, int lock)
  395. {
  396. #ifdef ENABLE_EMI_PROTECTION
  397. ALWAYS_LOG("[%s] apc:%p region:%d lock:%d\n",
  398. __func__, apc, region, lock);
  399. SET_ACCESS_PERMISSION(apc, lock,
  400. mpu_att_default[region][15], mpu_att_default[region][14],
  401. mpu_att_default[region][13], mpu_att_default[region][12],
  402. mpu_att_default[region][11], mpu_att_default[region][10],
  403. mpu_att_default[region][9], mpu_att_default[region][8],
  404. mpu_att_default[region][7], mpu_att_default[region][6],
  405. mpu_att_default[region][5], mpu_att_default[region][4],
  406. mpu_att_default[region][3], mpu_att_default[region][2],
  407. mpu_att_default[region][1], mpu_att_default[region][0]);
  408. #endif
  409. }
  410. static void mpu_attr_calculate(
  411. unsigned int apc[EMI_MPU_DGROUP_NUM], int region_id, unsigned int request_attr)
  412. {
  413. mpu_att_t tmp_mpu_att[MPU_MDOMAIN_ID_TOTAL_NUM], i;
  414. for (i = 0; i < MPU_MDOMAIN_ID_TOTAL_NUM; i++)
  415. tmp_mpu_att[i] = mpu_att_default[region_id][i];
  416. /* AP MD1 MDHW: AP */
  417. if ((request_attr & 0xF) <= FORBIDDEN)
  418. tmp_mpu_att[MPU_MDOMAIN_ID_AP] = (request_attr & 0xF);
  419. /* AP MD1 MDHW: MD1 */
  420. request_attr = (request_attr >> 4);
  421. if ((request_attr & 0xF) <= FORBIDDEN)
  422. tmp_mpu_att[MPU_MDOMAIN_ID_MD1] = (request_attr & 0xF);
  423. /* AP MD1 MDHW: MDHW */
  424. request_attr = (request_attr >> 4);
  425. if ((request_attr & 0xF) <= FORBIDDEN)
  426. tmp_mpu_att[MPU_MDOMAIN_ID_MDHW] = (request_attr & 0xF);
  427. #ifdef ENABLE_EMI_PROTECTION
  428. /* MPU region lock */
  429. SET_ACCESS_PERMISSION(apc, 1,
  430. tmp_mpu_att[15], tmp_mpu_att[14],
  431. tmp_mpu_att[13], tmp_mpu_att[12],
  432. tmp_mpu_att[11], tmp_mpu_att[10],
  433. tmp_mpu_att[9], tmp_mpu_att[8],
  434. tmp_mpu_att[7], tmp_mpu_att[6],
  435. tmp_mpu_att[5], tmp_mpu_att[4],
  436. tmp_mpu_att[3], tmp_mpu_att[2],
  437. tmp_mpu_att[1], tmp_mpu_att[0]);
  438. #endif
  439. }
  440. static void ccci_mem_access_cfg(mpu_cfg_t *mpu_cfg_list, int clear)
  441. {
  442. #ifdef ENABLE_EMI_PROTECTION
  443. mpu_cfg_t *curr;
  444. struct emi_region_info_t region_info;
  445. unsigned int curr_attr[EMI_MPU_DGROUP_NUM];
  446. char buf[MPU_STR_BUF_SIZE];
  447. int i;
  448. if (NULL == mpu_cfg_list)
  449. return;
  450. SET_ACCESS_PERMISSION(curr_attr, 0,
  451. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION,
  452. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION,
  453. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION,
  454. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION);
  455. for (curr = mpu_cfg_list; curr->region != -1; curr++) {
  456. if (clear) {
  457. region_info.region = (unsigned int)curr->region;
  458. emi_mpu_clear_protection(&region_info);
  459. get_mpu_attr_str(0, curr_attr, buf, MPU_STR_BUF_SIZE);
  460. ALWAYS_LOG("Clr MPU:S:0x%x E:0x%x A:<%d>[0~15]%s\n",
  461. 0, 0, curr->region, buf);
  462. } else {
  463. region_info.start = curr->start;
  464. region_info.end = curr->end;
  465. region_info.region = (unsigned int)curr->region;
  466. for (i = 0; i < EMI_MPU_DGROUP_NUM; i++)
  467. region_info.apc[i] = curr->permission[i];
  468. emi_mpu_set_protection(&region_info);
  469. get_mpu_attr_str(0, curr->permission, buf, MPU_STR_BUF_SIZE);
  470. ALWAYS_LOG("Set MPU:S:0x%x E:0x%x A:<%d>[0~15]%s\n",
  471. curr->start, curr->end, curr->region, buf);
  472. }
  473. }
  474. #endif
  475. }
  476. /*--------- Implement one by one -------------------------------------------------------------------------------*/
  477. int plat_get_padding_mpu_num(void)
  478. {
  479. return (int)(sizeof(free_mpu_region)/sizeof(unsigned int)) - 1;
  480. }
  481. /*---------------------------------------------------------------------------------------------------*/
  482. /* Global variable for share memory */
  483. /*---------------------------------------------------------------------------------------------------*/
  484. static unsigned int ap_md1_smem_size_at_lk_env;
  485. static unsigned int md1_md3_smem_size_at_lk_env;
  486. static unsigned int ap_md3_smem_size_at_lk_env;
  487. static unsigned int ap_md1_smem_size_at_img;
  488. #define AP_MD1_SMEM_SIZE 0x100000
  489. #define MAX_SMEM_SIZE 0x4000000 //history: 6M-->256M-->64M
  490. typedef struct _smem_layout {
  491. unsigned long long base_addr;
  492. unsigned int ap_md1_smem_offset;
  493. unsigned int ap_md1_smem_size;
  494. unsigned int ap_md3_smem_offset;
  495. unsigned int ap_md3_smem_size;
  496. unsigned int md1_md3_smem_offset;
  497. unsigned int md1_md3_smem_size;
  498. unsigned int total_smem_size;
  499. } smem_layout_t;
  500. static smem_layout_t smem_info;
  501. typedef struct _ccb_layout {
  502. unsigned long long ccb_data_buffer_addr;
  503. unsigned int ccb_data_buffer_size;
  504. } ccb_layout_t;
  505. static ccb_layout_t ccb_info;
  506. unsigned int md1_phy_cap_size;
  507. void plat_notify_secure(unsigned long base_addr)
  508. {
  509. unsigned long addr_get;
  510. #ifdef MTK_SMC_ID_MGMT
  511. mt_secure_call(MTK_SIP_LK_AMMS_MD_BASE_ADDR_AARCH32, base_addr, 0, 0, 0);
  512. addr_get = mt_secure_call(MTK_SIP_LK_AMMS_GET_MD_BASE_ADDR_AARCH32, 0, 0, 0, 0);
  513. #else
  514. mt_secure_call(MTK_SIP_LK_AMMS_MD_BASE_ADDR_AARCH32, base_addr, 0, 0);
  515. addr_get = mt_secure_call(MTK_SIP_LK_AMMS_GET_MD_BASE_ADDR_AARCH32, 0, 0, 0);
  516. #endif
  517. ALWAYS_LOG("mt_secure_call: set_addr = 0x%lx, get_addr = 0x%lx\n", base_addr, addr_get);
  518. }
  519. /*---------------------------------------------------------------------------------------------------*/
  520. /* HW remap function implement */
  521. /*---------------------------------------------------------------------------------------------------*/
  522. int plat_apply_hw_remap_for_md_ro_rw(void* info)
  523. {
  524. modem_info_t *md_ld_info = (modem_info_t *)info;
  525. plat_notify_secure(md_ld_info->base_addr);
  526. return md_mem_ro_rw_remapping((unsigned int)md_ld_info->md_id, md_ld_info->base_addr);
  527. }
  528. int plat_apply_hw_remap_for_md_smem(void *addr, int size)
  529. {
  530. /* For share memory final size depends on MD number, just store start address and size
  531. ** actual setting will do later
  532. */
  533. smem_info.base_addr = (unsigned long long)((unsigned long)addr);
  534. return 0;
  535. }
  536. int get_ccci_md_view_smem_addr(unsigned long long *ap_addr, unsigned int *md_addr)
  537. {
  538. int ret = 0;
  539. if (ap_addr)
  540. *ap_addr = smem_info.base_addr + smem_info.ap_md1_smem_offset + 0x100000;
  541. else
  542. ret = -1;
  543. if (md_addr)
  544. *md_addr = 0x40000000 + smem_info.ap_md1_smem_offset + 0x100000;
  545. else
  546. ret = -2;
  547. if (ret >= 0)
  548. ALWAYS_LOG("[ccci]get_md_view_smem: ap_addr = 0x%llx, 0x%x\n", *ap_addr, *md_addr);
  549. else
  550. ALWAYS_LOG("[ccci]get_md_view_smem: param error, ret = %d\n", ret);
  551. return ret;
  552. }
  553. /*---------------------------------------------------------------------------------------------------*/
  554. /* check header info collection by plat_post_hdr_info */
  555. /*---------------------------------------------------------------------------------------------------*/
  556. void plat_post_hdr_info(void* hdr, int ver, int id)
  557. {
  558. if (id == MD_SYS1) {
  559. ap_md1_smem_size_at_img = ((struct md_check_header_v6*)hdr)->ap_md_smem_size;
  560. }
  561. }
  562. /*---------------------------------------------------------------------------------------------------*/
  563. /* MPU static global variable and mpu relate function implement */
  564. /*---------------------------------------------------------------------------------------------------*/
  565. #define MPU_REGION_TOTAL_NUM (16) /* = MD1+MD3 */
  566. static mpu_cfg_t mpu_tbl[MPU_REGION_TOTAL_NUM];
  567. static int s_g_curr_mpu_num;
  568. /*
  569. ** if set start=0x0, end=0x10000, the actural protected area will be 0x0-0x1FFFF,
  570. ** here we use 64KB align, MPU actually request 32KB align since MT6582, but this works...
  571. ** we assume emi_mpu_set_region_protection will round end address down to 64KB align.
  572. */
  573. static void dump_received_pure_mpu_setting(struct image_section_desc *mem_info, int item_num)
  574. {
  575. int i;
  576. for (i =0; i < item_num; i++)
  577. MPU_DBG_LOG("mpu sec dec %d: offset:%x, size:%x, mpu_attr:%x, ext_flag:%x, relate_idx:%x\n", i,
  578. mem_info[i].offset, mem_info[i].size, mem_info[i].mpu_attr,
  579. mem_info[i].ext_flag, mem_info[i].relate_idx);
  580. }
  581. static int find_bind_mpu_region(mpu_cfg_t *mpu_tbl_hdr, int item_num, unsigned int bind_key)
  582. {
  583. int i;
  584. for (i = 0; i < item_num; i++) {
  585. if (mpu_tbl_hdr[i].relate_region == (int)bind_key)
  586. return i;
  587. }
  588. return -1;
  589. }
  590. static int md1_mpu_setting_process(void *p_md_ld_info, void *p_mem_info, mpu_cfg_t *mpu_tbl_hdr)
  591. {
  592. modem_info_t *md_ld_info = (modem_info_t *)p_md_ld_info;
  593. struct image_section_desc *mem_info = (struct image_section_desc *)p_mem_info;
  594. int normal_region_num = 0;
  595. int total_region_num = 0;
  596. int curr_idx = 0;
  597. int i;
  598. int j;
  599. int all_range_region_idx = -1;
  600. int bind_idx;
  601. int free_region_id;
  602. int didi_region_idx = -1;
  603. /* Calculate mpu num and padding num */
  604. for (i = 0; i < MPU_REGION_TOTAL_NUM; i++) {
  605. if ((mem_info[i].offset == 0) && (mem_info[i].size == 0))
  606. break;
  607. if (mem_info[i].ext_flag & MD_ALL_RANGE)
  608. all_range_region_idx = i;
  609. if (mem_info[i].ext_flag & MD_DRDI_REGION)
  610. didi_region_idx = i;
  611. }
  612. total_region_num = i;
  613. dump_received_pure_mpu_setting(mem_info, total_region_num);
  614. for (i = 0; i < total_region_num; i++) {
  615. if (mem_info[i].ext_flag & (MD_DRDI_REGION|MD_ALL_RANGE|NEED_REMOVE|NEED_MPU_MORE))
  616. continue;
  617. /* Process normal case first */
  618. if (curr_idx >= (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char))) {
  619. ALWAYS_LOG("[error]md%d: mpu region too more %d\n", md_ld_info->md_id+1,
  620. (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char)));
  621. return -LD_ERR_PLAT_MPU_REGION_TOO_MORE;
  622. }
  623. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[i].offset;
  624. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[i].size;
  625. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;/* 64K align */
  626. mpu_attr_calculate(
  627. mpu_tbl_hdr[curr_idx].permission, region_mapping_at_hdr_md1[curr_idx], mem_info[i].mpu_attr);
  628. mpu_tbl_hdr[curr_idx].region = (int)region_mapping_at_hdr_md1[curr_idx];
  629. mpu_tbl_hdr[curr_idx].relate_region = mem_info[i].relate_idx;
  630. curr_idx++;
  631. normal_region_num++;
  632. }
  633. if (normal_region_num != (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char))) {
  634. ALWAYS_LOG("[error]md%d: mpu region not sync %d:%d\n", md_ld_info->md_id+1, normal_region_num,
  635. (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char)));
  636. return -LD_ERR_PLAT_MPU_REGION_NUM_NOT_SYNC;
  637. }
  638. for (i = 0; i < total_region_num; i++) {
  639. if (mem_info[i].ext_flag & NEED_MPU_MORE) {
  640. bind_idx = find_bind_mpu_region(mpu_tbl_hdr, curr_idx, mem_info[i].relate_idx);
  641. if (bind_idx >= 0) {
  642. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[i].offset;
  643. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[i].size;
  644. /* 64K align */
  645. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;
  646. for (j = 0; j < EMI_MPU_DGROUP_NUM; j++)
  647. mpu_tbl_hdr[curr_idx].permission[j] = mpu_tbl_hdr[bind_idx].permission[j];
  648. /* setting relate region */
  649. free_region_id = get_free_mpu_region();
  650. if (free_region_id < 0) {
  651. ALWAYS_LOG("[error]abnormal free region id %d +\n", free_region_id);
  652. return -LD_ERR_PLAT_ABNORMAL_FREE_REGION;
  653. }
  654. mpu_tbl_hdr[curr_idx].region = free_region_id;
  655. mpu_tbl_hdr[curr_idx].relate_region = mem_info[i].relate_idx;
  656. mpu_tbl_hdr[bind_idx].relate_region = free_region_id;
  657. curr_idx++;
  658. } else {
  659. ALWAYS_LOG("md%d: padding array abnormal\n", md_ld_info->md_id+1);
  660. return -LD_ERR_PLAT_ABNORMAL_PAD_ARRAY;
  661. }
  662. }
  663. }
  664. /* Apply DRDI if needed */
  665. if (didi_region_idx >= 0) {
  666. get_mpu_region_default_access_att(mpu_tbl_hdr[curr_idx].permission, MPU_REGION_ID_MD_DRDI, 1);
  667. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[didi_region_idx].offset;
  668. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[didi_region_idx].size;
  669. /* 64K align */
  670. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;
  671. mpu_tbl_hdr[curr_idx].region = MPU_REGION_ID_MD_DRDI;
  672. mpu_tbl_hdr[curr_idx].relate_region = 0;
  673. curr_idx++;
  674. }
  675. /* Apply MD all range mpu protect setting */
  676. free_region_id = get_md_protect_mpu_region(); //get_free_mpu_region();
  677. if (free_region_id < 0) {
  678. ALWAYS_LOG("[error]no more free region\n");
  679. return -LD_ERR_PLAT_NO_MORE_FREE_REGION;
  680. }
  681. get_mpu_region_default_access_att(mpu_tbl_hdr[curr_idx].permission, free_region_id, 1);
  682. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[all_range_region_idx].offset;
  683. /*mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[all_range_region_idx].size;*/
  684. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + 256 * 1024 * 1024;
  685. /* 64K align */
  686. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;
  687. mpu_tbl_hdr[curr_idx].region = free_region_id;
  688. mpu_tbl_hdr[curr_idx].relate_region = 0;
  689. curr_idx++;
  690. /* Clear logic relate index to 0 to mark as end */
  691. for (i = 0; i < curr_idx; i++) {
  692. if (mpu_tbl_hdr[i].relate_region >= LOGIC_BINDING_IDX_START)
  693. mpu_tbl_hdr[i].relate_region = 0;
  694. }
  695. return curr_idx;
  696. }
  697. int plat_send_mpu_info_to_platorm(void *p_md_ld_info, void *p_mem_info)
  698. {
  699. modem_info_t *md_ld_info = (modem_info_t *)p_md_ld_info;
  700. struct image_section_desc *mem_info = (struct image_section_desc *)p_mem_info;
  701. int md_id = md_ld_info->md_id;
  702. int ret;
  703. int i;
  704. char buf[MPU_STR_BUF_SIZE];
  705. if (md_id == MD_SYS1) {
  706. ret = md1_mpu_setting_process(p_md_ld_info, p_mem_info, &mpu_tbl[s_g_curr_mpu_num]);
  707. if (ret > 0)
  708. s_g_curr_mpu_num += ret;
  709. } else if (md_id == MD1_DSP) {
  710. /* RO part */
  711. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD_DSP1, 1);
  712. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)md_ld_info->base_addr + mem_info[0].offset;
  713. mpu_tbl[s_g_curr_mpu_num].end = mpu_tbl[s_g_curr_mpu_num].start + mem_info[0].size;
  714. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD_DSP1;
  715. /* 64K align */
  716. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  717. s_g_curr_mpu_num++;
  718. /* RW part */
  719. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD_DSP2, 1);
  720. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)md_ld_info->base_addr + mem_info[1].offset;
  721. mpu_tbl[s_g_curr_mpu_num].end = mpu_tbl[s_g_curr_mpu_num].start + mem_info[1].size;
  722. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD_DSP2;
  723. /* 64K align */
  724. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  725. s_g_curr_mpu_num++;
  726. }
  727. for (i =0; i < s_g_curr_mpu_num; i++) {
  728. get_mpu_attr_str(0, mpu_tbl[i].permission, buf, MPU_STR_BUF_SIZE);
  729. MPU_DBG_LOG("plat mpu dec %d: region:%d[%d], start:0x%x, end:0x%x, attr:%s\n", i,
  730. mpu_tbl[i].region, mpu_tbl[i].relate_region, mpu_tbl[i].start, mpu_tbl[i].end, buf);
  731. }
  732. return 0;
  733. }
  734. static void set_gear_id_list()
  735. {
  736. int ret;
  737. ret = set_env("md1_ccb_gear_list",
  738. "1(2,20);2(2,10);3(0,0);11(2,2)");
  739. if (ret) {
  740. ALWAYS_LOG("set_gear_id_list error: %x\n", ret);
  741. }
  742. }
  743. static void set_ccb_gear_val(unsigned int gear_id)
  744. {
  745. int ret;
  746. char env_buf[12];
  747. memset(env_buf, 0, sizeof(env_buf));
  748. snprintf(env_buf, sizeof(env_buf), "%u", gear_id);
  749. ret = set_env("md1_ccb_cap_gear", env_buf);
  750. if (ret < 0) {
  751. ALWAYS_LOG("set env[md1_ccb_cap_gear]fail, ret=%d\n", ret);
  752. assert(0);
  753. } else
  754. ALWAYS_LOG("set env[md1_ccb_cap_gear]%d\n", gear_id);
  755. }
  756. static unsigned int get_ccb_gear_val(void)
  757. {
  758. unsigned int md1_ccb_cap_gear;
  759. if (g_boot_mode == META_BOOT || true == get_atm_enable_status()) {
  760. ALWAYS_LOG("meta mode[md1_ccb_cap_gear]%d\n", CCB_DATA_BUF_DEFAULT_GEAR);
  761. set_ccb_gear_val(CCB_DATA_BUF_DEFAULT_GEAR);
  762. return CCB_DATA_BUF_DEFAULT_GEAR;
  763. }
  764. md1_ccb_cap_gear = str2uint(get_env("md1_ccb_cap_gear"));
  765. if (md1_ccb_cap_gear != 0) {
  766. ALWAYS_LOG("get env[md1_ccb_cap_gear]%d\n", md1_ccb_cap_gear);
  767. return md1_ccb_cap_gear;
  768. } else {
  769. #ifdef MTK_DYNAMIC_CCB_BUFFER_GEAR_ID
  770. md1_ccb_cap_gear = MTK_DYNAMIC_CCB_BUFFER_GEAR_ID;
  771. ALWAYS_LOG("[%s] MTK_DYNAMIC_CCB_BUFFER_GEAR_ID: %d\n",
  772. __func__, MTK_DYNAMIC_CCB_BUFFER_GEAR_ID);
  773. #else
  774. md1_ccb_cap_gear = CCB_DATA_BUF_DEFAULT_GEAR;
  775. ALWAYS_LOG("[%s] CCB_DATA_BUF_DEFAULT_GEAR: %d\n",
  776. __func__, CCB_DATA_BUF_DEFAULT_GEAR);
  777. #endif
  778. set_ccb_gear_val(md1_ccb_cap_gear);
  779. return md1_ccb_cap_gear;
  780. }
  781. }
  782. /*------------------------------------------------------------------------------------------------*/
  783. /* Suppor function for share memory calculate */
  784. /*------------------------------------------------------------------------------------------------*/
  785. static int cal_share_mem_layout(int load_flag)
  786. {
  787. unsigned int md1_phy_cap_gear, md1_ccb_cap_gear;
  788. unsigned int md1_bank4_cache_offset;
  789. unsigned int md1_ccb_size = CCB_DATA_BUF_SIZE;
  790. unsigned char * ccb_data_buf = NULL;
  791. #if WITH_GZ_MD_SHAREMEM
  792. unsigned int mtee_support = 1;
  793. #endif
  794. ap_md1_smem_size_at_lk_env = str2uint(get_env("apmd1_smem"));
  795. md1_md3_smem_size_at_lk_env = str2uint(get_env("md1md3_smem"));
  796. ap_md3_smem_size_at_lk_env = str2uint(get_env("apmd3_smem"));
  797. md1_phy_cap_gear = str2uint(get_env("md1_phy_cap_gear"));
  798. md1_ccb_cap_gear = get_ccb_gear_val();
  799. ALWAYS_LOG("env[apmd1_smem]%x.\n", ap_md1_smem_size_at_lk_env);
  800. ALWAYS_LOG("env[md1md3_smem]%x.\n", md1_md3_smem_size_at_lk_env);
  801. ALWAYS_LOG("env[apmd3_smem]%x.\n", ap_md3_smem_size_at_lk_env);
  802. ALWAYS_LOG("env[md1_phy_cap_gear]%x.\n", md1_phy_cap_gear);
  803. ALWAYS_LOG("env[md1_ccb_cap_gear]%x.\n", md1_ccb_cap_gear);
  804. switch(md1_ccb_cap_gear)
  805. {
  806. case 0: /* none, using default*/
  807. md1_ccb_size = CCB_DATA_BUF_SIZE;
  808. break;
  809. case 1:
  810. md1_ccb_size = 22 * 1024 * 1024;
  811. break;
  812. case 2:
  813. md1_ccb_size = 12 * 1024 * 1024;
  814. break;
  815. case 3:
  816. md1_ccb_size = 0 * 1024 * 1024;
  817. break;
  818. case 11:
  819. md1_ccb_size = 4 * 1024 * 1024;
  820. break;
  821. default:
  822. md1_ccb_size = CCB_DATA_BUF_SIZE;
  823. break;
  824. }
  825. ALWAYS_LOG("allocate ccb data buffer0x%x\n", md1_ccb_size);
  826. set_gear_id_list();
  827. if (md1_ccb_size != 0)
  828. ccb_data_buf = ccci_request_named_mem("ap_md_c_smem", md1_ccb_size);
  829. if (ccb_data_buf == NULL) {
  830. ccb_info.ccb_data_buffer_addr = 0;
  831. ccb_info.ccb_data_buffer_size = 0;
  832. ALWAYS_LOG("allocate ccb data buffer share memory fail\n");
  833. } else {
  834. ccb_info.ccb_data_buffer_addr = (unsigned long long)((unsigned long)ccb_data_buf);
  835. ccb_info.ccb_data_buffer_size = md1_ccb_size;
  836. if (insert_ccci_tag_inf("ccb_info", (char*)&ccb_info, sizeof(ccb_layout_t)) < 0)
  837. ALWAYS_LOG("insert ccb_info fail\n");
  838. ALWAYS_LOG("ccb_info.ccb_data_buffer_addr: %x\n", (unsigned int)ccb_info.ccb_data_buffer_addr);
  839. ALWAYS_LOG("ccb_info.ccb_data_buffer_size: %x\n", ccb_info.ccb_data_buffer_size);
  840. /* CCB must in last 32M */
  841. md1_bank4_cache_offset = 224 * 1024 * 1024;
  842. if (insert_ccci_tag_inf("md1_smem_cahce_offset", (char*)&md1_bank4_cache_offset,
  843. sizeof(md1_bank4_cache_offset)) < 0)
  844. ALWAYS_LOG("insert md1_smem_cahce_offset fail\n");
  845. }
  846. /* slot 6 */
  847. #if WITH_GZ_MD_SHAREMEM
  848. if (g_boot_arg->gz_md_shm_pa && g_boot_arg->gz_md_shm_sz) {
  849. ALWAYS_LOG("mtee.base_addr: 0x%x, 0x%x\n", (unsigned int)g_boot_arg->gz_md_shm_pa, g_boot_arg->gz_md_shm_sz);
  850. if (insert_ccci_tag_inf("mtee_support", (char *)&mtee_support, sizeof(mtee_support)) < 0)
  851. ALWAYS_LOG("insert mtee_support fail\n");
  852. }
  853. #endif
  854. /* slot 7 */
  855. switch(md1_phy_cap_gear)
  856. {
  857. case 0:
  858. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L0;
  859. break;
  860. case 1:
  861. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L1;
  862. break;
  863. case 2:
  864. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L2;
  865. break;
  866. case 3:
  867. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L3;
  868. break;
  869. case 4:
  870. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L4;
  871. break;
  872. default:
  873. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L0;
  874. break;
  875. }
  876. ALWAYS_LOG("md1 phy capture size: %x\n", md1_phy_cap_size);
  877. if (insert_ccci_tag_inf("md1_phy_cap", (char*)&md1_phy_cap_size, sizeof(md1_phy_cap_size)) < 0)
  878. ALWAYS_LOG("insert md1_phy_cap fail\n");
  879. /* MD Share memory layout */
  880. /* AP <--> MD1 */
  881. {
  882. smem_info.ap_md1_smem_offset = 0;
  883. if (ap_md1_smem_size_at_lk_env)
  884. smem_info.ap_md1_smem_size = ap_md1_smem_size_at_lk_env;
  885. else if (ap_md1_smem_size_at_img)
  886. smem_info.ap_md1_smem_size = ap_md1_smem_size_at_img;
  887. else
  888. smem_info.ap_md1_smem_size = AP_MD1_SMEM_SIZE;
  889. smem_info.md1_md3_smem_offset = 0;
  890. smem_info.md1_md3_smem_size = 0;
  891. smem_info.ap_md3_smem_offset = 0;
  892. smem_info.ap_md3_smem_size = 0;
  893. /* add 2M for SIB header to avoid MD MPU violation if SIB exist */
  894. if(md1_phy_cap_size != CCCI_SMEM_SIZE_PHY_C_L0)
  895. smem_info.ap_md1_smem_size += 2 * 1024 * 1024;
  896. smem_info.total_smem_size = smem_info.ap_md1_smem_size;
  897. }
  898. /* insert share memory layout to lk info */
  899. if (insert_ccci_tag_inf("smem_layout", (char*)&smem_info, sizeof(smem_layout_t)) < 0)
  900. ALWAYS_LOG("insert smem_layout fail\n");
  901. ALWAYS_LOG("smem_info.base_addr: %x\n", (unsigned int)smem_info.base_addr);
  902. ALWAYS_LOG("smem_info.ap_md1_smem_offset: %x\n", smem_info.ap_md1_smem_offset);
  903. ALWAYS_LOG("smem_info.ap_md1_smem_size: %x\n", smem_info.ap_md1_smem_size);
  904. ALWAYS_LOG("smem_info.ap_md3_smem_offset: %x\n", smem_info.ap_md3_smem_offset);
  905. ALWAYS_LOG("smem_info.ap_md3_smem_size: %x\n", smem_info.ap_md3_smem_size);
  906. ALWAYS_LOG("smem_info.md1_md3_smem_offset: %x\n", smem_info.md1_md3_smem_offset);
  907. ALWAYS_LOG("smem_info.md1_md3_smem_size: %x\n", smem_info.md1_md3_smem_size);
  908. ALWAYS_LOG("smem_info.total_smem_size: %x\n", smem_info.total_smem_size);
  909. return (int)smem_info.total_smem_size + md1_phy_cap_size;
  910. }
  911. static void boot_to_dummy_ap_mode(int load_md_flag);
  912. /*------------------------------------------------------------------------------------------------*/
  913. /* Note: This function using global variable
  914. ** if set start=0x0, end=0x10000, the actural protected area will be 0x0-0x1FFFF,
  915. ** here we use 64KB align, MPU actually request 32KB align since MT6582, but this works...
  916. ** we assume emi_mpu_set_region_protection will round end address down to 64KB align.
  917. */
  918. int plat_apply_platform_setting(int load_md_flag)
  919. {
  920. int smem_final_size;
  921. #ifdef DUMMY_AP_MODE
  922. /* This function will never return */
  923. ALWAYS_LOG("boot to dummy ap mode!!!\n");
  924. boot_to_dummy_ap_mode(load_md_flag);
  925. return 0;
  926. #endif
  927. /* Check loading validation */
  928. if (((load_md_flag & (1<<MD_SYS1)) == 0) && (load_md_flag & (1<<MD_SYS3))) {
  929. ALWAYS_LOG("md3 depends on md1,but md1 not loaded\n");
  930. return -LD_ERR_PLAT_MD1_NOT_RDY;
  931. }
  932. if ((load_md_flag & ((1<<MD_SYS1)|(1<<MD_SYS3))) == 0) {
  933. ALWAYS_LOG("both md1 and md3 not enable\n");
  934. return 0;
  935. }
  936. smem_final_size = cal_share_mem_layout(load_md_flag);
  937. ALWAYS_LOG("ap md1 share mem MPU need configure\n");
  938. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD1_SMEM;
  939. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD1_SMEM, 0);
  940. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)smem_info.base_addr + smem_info.ap_md1_smem_offset;
  941. mpu_tbl[s_g_curr_mpu_num].end = (unsigned int)smem_info.base_addr + smem_info.ap_md1_smem_offset
  942. + smem_info.ap_md1_smem_size + md1_phy_cap_size;
  943. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  944. s_g_curr_mpu_num++;
  945. /* add for ccb data buffer mpu */
  946. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD1_CCB;
  947. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD1_CCB, 0);
  948. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)ccb_info.ccb_data_buffer_addr;
  949. mpu_tbl[s_g_curr_mpu_num].end = (unsigned int)ccb_info.ccb_data_buffer_addr + ccb_info.ccb_data_buffer_size;
  950. //ccb_data_buf + CCB_DATA_BUF_SIZE;
  951. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  952. s_g_curr_mpu_num++;
  953. mpu_tbl[s_g_curr_mpu_num].region = -1; /* mark for end */
  954. /* Insert mpu tag info */
  955. if (insert_ccci_tag_inf("md_mpu_inf", (char*)mpu_tbl, sizeof(mpu_cfg_t)*s_g_curr_mpu_num) < 0)
  956. ALWAYS_LOG("insert md_mpu_inf fail\n");
  957. if (insert_ccci_tag_inf("md_mpu_num", (char*)&s_g_curr_mpu_num, sizeof(int)) < 0)
  958. ALWAYS_LOG("insert md_mpu_num fail\n");
  959. /* Apply all MPU setting */
  960. ccci_mem_access_cfg(mpu_tbl, 0);
  961. /* Apply share memory HW remap setting and lock it */
  962. if (load_md_flag & (1<<MD_SYS1)) {
  963. md_smem_rw_remapping(MD_SYS1, (unsigned int)(smem_info.base_addr + smem_info.ap_md1_smem_offset));
  964. #if WITH_GZ_MD_SHAREMEM
  965. if (g_boot_arg->gz_md_shm_pa && g_boot_arg->gz_md_shm_sz) {
  966. md_bank4_remapping_by_slot(MD_SYS1, g_boot_arg->gz_md_shm_pa, 6);
  967. }
  968. #endif
  969. /* remapping CCB to last 32M in bank4 */
  970. md_bank4_remapping_by_slot(MD_SYS1, ccb_info.ccb_data_buffer_addr, 7);
  971. md_emi_remapping_lock(MD_SYS1);
  972. }
  973. return smem_final_size;
  974. }
  975. /*------------------------------------------------------------------------------------------------*/
  976. /* platform configure setting info. */
  977. /*------------------------------------------------------------------------------------------------*/
  978. long long plat_ccci_get_ld_md_plat_setting(char cfg_name[])
  979. {
  980. if (strcmp(cfg_name, "share_memory_size") == 0) {
  981. #ifdef DUMMY_AP_MODE
  982. return 0x200000;
  983. #endif
  984. return (long long)(MAX_SMEM_SIZE);
  985. }
  986. if (strcmp(cfg_name, "share_mem_limit") == 0)
  987. return 0x90000000LL;
  988. if (strcmp(cfg_name, "ro_rw_mem_limit") == 0) {
  989. #ifdef DUMMY_AP_MODE
  990. return 0xA0000000LL;
  991. #endif
  992. return 0xA0000000LL;
  993. }
  994. if (strcmp(cfg_name, "ro_rw_mem_align") == 0)
  995. return 0x2000000LL;
  996. if (strcmp(cfg_name, "share_mem_align") == 0)
  997. return 0x2000000LL;
  998. if (strcmp(cfg_name, "ld_version") == 0) {
  999. #ifdef DUMMY_AP_MODE
  1000. return 0x20001;
  1001. #endif
  1002. return 0x20000;/* xxxx_yyyy, xxxx: main id, yyyy sub id */
  1003. }
  1004. if (strcmp(cfg_name, "rat_plat_ver") == 0)
  1005. return RAT_VER_93;
  1006. return -1LL;
  1007. }
  1008. #ifdef DUMMY_AP_MODE
  1009. #include <platform/mt_irq.h>
  1010. extern void dummy_ap_boot_up_md(int md_en_flag);
  1011. extern void load_modem_image(void);
  1012. extern int dummy_ap_irq_helper(unsigned int);
  1013. /* Remember add this function to file platform.c(platform code) */
  1014. void dummy_ap_entry(void)
  1015. {
  1016. load_modem_image();
  1017. }
  1018. /* Remember add this function to file interrupts.c(platform code) */
  1019. void dummy_ap_irq_handler(unsigned int irq)
  1020. {
  1021. if (dummy_ap_irq_helper(irq)) {
  1022. mt_irq_ack(irq);
  1023. mt_irq_unmask(irq);
  1024. }
  1025. }
  1026. void boot_to_dummy_ap_mode(int load_md_flag)
  1027. {
  1028. md_smem_rw_remapping(MD_SYS1, smem_info.base_addr);
  1029. /* Before boot dummy AP, clear share memory */
  1030. memset((void*)((unsigned long)smem_info.base_addr), 0, 0x200000);
  1031. dummy_ap_boot_up_md(load_md_flag);
  1032. }
  1033. #endif