ccci_lk_load_img_plat.c 41 KB

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