ccci_lk_load_img_plat.c 42 KB

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