ccci_lk_load_img_plat.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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 <debug.h>
  46. #define MODULE_NAME "LK_LD_MD"
  47. #include "ccci_ld_md_core.h"
  48. #include "ccci_ld_md_errno.h"
  49. #ifdef MTK_DFD_ENABLE_CACHE_DUMP
  50. #define CCCI_SMEM_SIZE_DFD (8*1024*1024)
  51. #else
  52. #define CCCI_SMEM_SIZE_DFD (448*1024)
  53. #endif
  54. #define CCCI_SMEM_SIZE_UDC_NONCACHE ((14*1024+448)*1024)
  55. #define CCCI_SMEM_SIZE_UDC_CACHE (512*1024)
  56. #define CCCI_SMEM_SIZE_CCB_DHL (2*1024*1024)
  57. #define CCCI_SMEM_SIZE_RAW_DHL (20*1024*1024)
  58. #define CCCI_SMEM_SIZE_LWA (0) // (8*1024*1024)
  59. #define CCCI_SMEM_SIZE_PHY_C_L0 (0*1024*1024)
  60. #define CCCI_SMEM_SIZE_PHY_C_L1 (32*1024*1024)
  61. #define CCCI_SMEM_SIZE_PHY_C_L2 (64*1024*1024)
  62. #define CCCI_SMEM_SIZE_PHY_C_L3 (128*1024*1024)
  63. #define CCCI_SMEM_SIZE_PHY_C_L4 (222*1024*1024)
  64. #define CCB_DATA_BUF_SIZE (CCCI_SMEM_SIZE_CCB_DHL + CCCI_SMEM_SIZE_RAW_DHL)
  65. #define CCB_DATA_BUF_DEFAULT_GEAR 1 /* NOTE: This value may be different at different platform */
  66. #define CACHABLE_SMEM_MAX_SIZE (64*1024*1024)
  67. /***************************************************************************************************
  68. ** Feature Option setting part
  69. ***************************************************************************************************/
  70. #define ENABLE_EMI_PROTECTION
  71. /*------------------------------------------------------------------------------------------------*/
  72. /* Suppor function for share memory calculate */
  73. /*------------------------------------------------------------------------------------------------*/
  74. /* copy from kernel */
  75. enum SMEM_USER_ID_K {
  76. /* this should remain to be 0 for backward compatibility */
  77. SMEM_USER_RAW_DBM = 0,
  78. /* sequence in CCB users matters, must align with ccb_configs[] */
  79. SMEM_USER_CCB_START,
  80. SMEM_USER_CCB_DHL = SMEM_USER_CCB_START,
  81. SMEM_USER_CCB_MD_MONITOR,
  82. SMEM_USER_CCB_META,
  83. SMEM_USER_CCB_END = SMEM_USER_CCB_META,
  84. /* squence of other users does not matter */
  85. SMEM_USER_RAW_CCB_CTRL,
  86. SMEM_USER_RAW_DHL,
  87. SMEM_USER_RAW_MDM,
  88. SMEM_USER_RAW_NETD,
  89. SMEM_USER_RAW_USB,
  90. SMEM_USER_RAW_AUDIO,
  91. SMEM_USER_RAW_DFD_K,
  92. SMEM_USER_RAW_LWA,
  93. SMEM_USER_RAW_MDCCCI_DBG,
  94. SMEM_USER_RAW_MDSS_DBG,
  95. SMEM_USER_RAW_RUNTIME_DATA,
  96. SMEM_USER_RAW_FORCE_ASSERT,
  97. SMEM_USER_CCISM_SCP,
  98. SMEM_USER_RAW_MD2MD,
  99. SMEM_USER_RAW_RESERVED,
  100. SMEM_USER_CCISM_MCU,
  101. SMEM_USER_CCISM_MCU_EXP,
  102. SMEM_USER_SMART_LOGGING,
  103. SMEM_USER_RAW_MD_CONSYS,
  104. SMEM_USER_RAW_PHY_CAP,
  105. SMEM_USER_RAW_USIP,
  106. SMEM_USER_RAW_UDC_DATA,
  107. SMEM_USER_RAW_UDC_DESCTAB,
  108. SMEM_USER_MAX_K,
  109. SMEM_USER_NON_PADDING,
  110. };
  111. /*---------------------------------------------------------------------------------------------------*/
  112. /* Global variable for share memory */
  113. /*---------------------------------------------------------------------------------------------------*/
  114. static unsigned int ap_md1_smem_size_at_lk_env;
  115. static unsigned int md1_md3_smem_size_at_lk_env;
  116. static unsigned int ap_md3_smem_size_at_lk_env;
  117. static unsigned int ap_md1_smem_size_at_img;
  118. static unsigned int amms_pos_size_at_img;
  119. static unsigned int consys_size_at_img;
  120. static unsigned int udc_support_at_img;
  121. static unsigned int amms_pos_offset;
  122. #define AP_MD1_SMEM_SIZE 0x100000
  123. #define MAX_SMEM_SIZE 0x10000000 //history: 6M-->256M-->64M-->256M
  124. typedef struct _smem_layout {
  125. unsigned long long base_addr;
  126. unsigned int ap_md1_smem_offset;
  127. /* ap_md1_smem_size: this is for MD MPU, not total ap_md1 share size. */
  128. unsigned int ap_md1_smem_size;
  129. unsigned int ap_md3_smem_offset;
  130. unsigned int ap_md3_smem_size;
  131. unsigned int md1_md3_smem_offset;
  132. unsigned int md1_md3_smem_size;
  133. unsigned int total_smem_size;
  134. } smem_layout_t;
  135. static smem_layout_t smem_info;
  136. typedef struct _ccb_layout {
  137. unsigned long long ccb_data_buffer_addr;
  138. unsigned int ccb_data_buffer_size;
  139. } ccb_layout_t;
  140. static ccb_layout_t ccb_info;
  141. typedef struct _csmem_item {
  142. unsigned long long csmem_buffer_addr;
  143. unsigned int md_offset;
  144. unsigned int csmem_buffer_size;
  145. unsigned int item_cnt;
  146. } csmem_item_t;
  147. static csmem_item_t csmem_info;
  148. unsigned int md1_phy_cap_size;
  149. struct _udc_info {
  150. unsigned int noncache_size;
  151. unsigned int cache_size;
  152. };
  153. static struct _udc_info udc_size;
  154. #if WITH_GZ_MD_SHAREMEM
  155. extern BOOT_ARGUMENT *g_boot_arg;
  156. #endif
  157. void plat_md_bank4_amms_pos_notify_secure(unsigned long ap_view_addr,
  158. unsigned long md_view_addr, unsigned long length)
  159. {
  160. unsigned long addr_get,length_get;
  161. mt_secure_call(MTK_SIP_LK_AMMS_MD_POS_ADDR_AARCH32, ap_view_addr, 0, 0, 0);
  162. addr_get = mt_secure_call(MTK_SIP_LK_AMMS_GET_MD_POS_ADDR_AARCH32, 0, 0, 0, 0);
  163. mt_secure_call(MTK_SIP_LK_AMMS_MD_POS_MD_VIEW_ADDR_AARCH32, md_view_addr, 0, 0, 0);
  164. mt_secure_call(MTK_SIP_LK_AMMS_MD_POS_LENGTH_AARCH32, length, 0, 0, 0);
  165. length_get = mt_secure_call(MTK_SIP_LK_AMMS_GET_MD_POS_LENGTH_AARCH32, 0, 0, 0, 0);
  166. ALWAYS_LOG("mt_secure_call: set_pos_addr = 0x%lx(0x%lx), get_pos_addr = 0x%lx length=0x%lx get_length=0x%lx\n",
  167. ap_view_addr, md_view_addr, addr_get, length, length_get);
  168. }
  169. void plat_amms_pos_notify_secure(void *addr)
  170. {
  171. /* notify other user, such as ATF */
  172. if (amms_pos_size_at_img)
  173. plat_md_bank4_amms_pos_notify_secure(
  174. (unsigned long)(addr + amms_pos_offset),
  175. 0x40000000 + amms_pos_offset, amms_pos_size_at_img);
  176. }
  177. static int cal_share_mem_size(void)
  178. {
  179. unsigned int md1_phy_cap_gear;
  180. int smem_dfd_size = CCCI_SMEM_SIZE_DFD;
  181. unsigned int smem_align_padding_size;
  182. ap_md1_smem_size_at_lk_env = str2uint(get_env("apmd1_smem"));
  183. md1_md3_smem_size_at_lk_env = str2uint(get_env("md1md3_smem"));
  184. ap_md3_smem_size_at_lk_env = str2uint(get_env("apmd3_smem"));
  185. md1_phy_cap_gear = str2uint(get_env("md1_phy_cap_gear"));
  186. ALWAYS_LOG("env[apmd1_smem]%x.\n", ap_md1_smem_size_at_lk_env);
  187. ALWAYS_LOG("env[md1md3_smem]%x.\n", md1_md3_smem_size_at_lk_env);
  188. ALWAYS_LOG("env[apmd3_smem]%x.\n", ap_md3_smem_size_at_lk_env);
  189. ALWAYS_LOG("env[md1_phy_cap_gear]%x.\n", md1_phy_cap_gear);
  190. /* MD Share memory layout */
  191. /* AP <--> MD1 */
  192. smem_info.ap_md1_smem_offset = 0;
  193. if (ap_md1_smem_size_at_lk_env)
  194. smem_info.ap_md1_smem_size = ap_md1_smem_size_at_lk_env;
  195. else
  196. smem_info.ap_md1_smem_size = AP_MD1_SMEM_SIZE;
  197. smem_info.ap_md1_smem_size += smem_dfd_size;
  198. if (udc_support_at_img)
  199. smem_info.ap_md1_smem_size += CCCI_SMEM_SIZE_UDC_NONCACHE;
  200. smem_info.md1_md3_smem_offset = 0;
  201. smem_info.md1_md3_smem_size = 0;
  202. smem_info.ap_md3_smem_offset = 0;
  203. smem_info.ap_md3_smem_size = 0;
  204. /* no need add md3, 3967 none */
  205. smem_info.total_smem_size = smem_info.ap_md1_smem_size;
  206. if (amms_pos_size_at_img) {
  207. amms_pos_offset = smem_info.ap_md1_smem_size;
  208. smem_align_padding_size =
  209. (smem_info.ap_md1_smem_size + 0x400000 -1)&(~(0x400000 -1));
  210. smem_align_padding_size -= smem_info.ap_md1_smem_size;
  211. amms_pos_size_at_img += smem_align_padding_size;
  212. smem_info.ap_md1_smem_size += amms_pos_size_at_img;
  213. smem_align_padding_size = 0;
  214. if (smem_align_padding_size) {
  215. smem_info.ap_md1_smem_size += smem_align_padding_size;
  216. /*pass smem align padding size to Kernel*/
  217. if (insert_ccci_tag_inf("smem_align_padding_size",
  218. (char*)&smem_align_padding_size, sizeof(int)) < 0)
  219. ALWAYS_LOG("insert pos_align_size fail\n");
  220. ALWAYS_LOG("pos_align_size: 0x%x\n", smem_align_padding_size);
  221. }
  222. smem_info.total_smem_size = smem_info.ap_md1_smem_size;
  223. /*pass AMMS POS size to Kernel*/
  224. if (insert_ccci_tag_inf("smem_amms_pos_size",
  225. (char*)&amms_pos_size_at_img, sizeof(int)) < 0)
  226. ALWAYS_LOG("insert amms_pos_size fail\n");
  227. ALWAYS_LOG("amms_pos_size: 0x%x\n", amms_pos_size_at_img);
  228. }
  229. switch(md1_phy_cap_gear)
  230. {
  231. case 0:
  232. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L0;
  233. break;
  234. case 1:
  235. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L1;
  236. break;
  237. case 2:
  238. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L2;
  239. break;
  240. case 3:
  241. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L3;
  242. break;
  243. case 4:
  244. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L4;
  245. break;
  246. default:
  247. md1_phy_cap_size = CCCI_SMEM_SIZE_PHY_C_L0;
  248. break;
  249. }
  250. if (md1_phy_cap_size > (MAX_SMEM_SIZE - CACHABLE_SMEM_MAX_SIZE - smem_info.total_smem_size))
  251. md1_phy_cap_size = (MAX_SMEM_SIZE - CACHABLE_SMEM_MAX_SIZE - smem_info.total_smem_size);
  252. /* 2M for MD MPU include SIB header */
  253. if (md1_phy_cap_size > 2 * 1024 * 1024)
  254. smem_info.ap_md1_smem_size += md1_phy_cap_size;
  255. /* total_smem_size: in factly, this is for MD MPU, not ap-md share total size. */
  256. smem_info.total_smem_size += md1_phy_cap_size;
  257. ALWAYS_LOG("smem_info.ap_md1_smem_offset: %x\n", smem_info.ap_md1_smem_offset);
  258. ALWAYS_LOG("smem_info.ap_md1_smem_size: %x\n", smem_info.ap_md1_smem_size);
  259. ALWAYS_LOG("smem_info.ap_md3_smem_offset: %x\n", smem_info.ap_md3_smem_offset);
  260. ALWAYS_LOG("smem_info.ap_md3_smem_size: %x\n", smem_info.ap_md3_smem_size);
  261. ALWAYS_LOG("smem_info.md1_md3_smem_offset: %x\n", smem_info.md1_md3_smem_offset);
  262. ALWAYS_LOG("smem_info.md1_md3_smem_size: %x\n", smem_info.md1_md3_smem_size);
  263. ALWAYS_LOG("md1 phy capture size: %x\n", md1_phy_cap_size);
  264. ALWAYS_LOG("smem_info.total_smem_size: %x\n", smem_info.total_smem_size);
  265. ALWAYS_LOG("amms_pos_offset: %x\n", amms_pos_offset);
  266. /*pass DFD size to Kernel*/
  267. if (insert_ccci_tag_inf("smem_dfd_size", (char*)&smem_dfd_size, sizeof(int)) < 0)
  268. ALWAYS_LOG("insert smem_dfd_size fail\n");
  269. return (int)smem_info.total_smem_size; /* non-cacheable */
  270. }
  271. #define SMEM_MD_CONSYS_SIZE (4*1024*1024) /* must be 4M or 0M */
  272. #define SMEM_MD_USIP_SIZE (384*1024)
  273. #define SMEM_MD_PADDING_SIZE (640*1024)
  274. static csmem_item_t csmem_layout[] = {
  275. {0, 0, SMEM_MD_CONSYS_SIZE, SMEM_USER_RAW_MD_CONSYS},
  276. {0, 0, SMEM_MD_USIP_SIZE, SMEM_USER_RAW_USIP},
  277. {0, 0, SMEM_MD_PADDING_SIZE, SMEM_USER_NON_PADDING},
  278. {0, 0, (22*1024*1024), SMEM_USER_CCB_START},
  279. {0, 0, 0, SMEM_USER_RAW_UDC_DESCTAB},
  280. };
  281. struct ccb_gear_id_mapping {
  282. unsigned int gear_id;
  283. unsigned int size;
  284. };
  285. const struct ccb_gear_id_mapping ccb_support_tbl[] = {
  286. {1, 22 * 1024 * 1024},
  287. {2, 12 * 1024 * 1024},
  288. {3, 0 * 1024 * 1024},
  289. {11, 4 * 1024 * 1024},
  290. {6, 52 * 1024 * 1024}
  291. };
  292. static unsigned int get_ccb_size_from_gear_id(unsigned int gear_id)
  293. {
  294. int i;
  295. int count = sizeof(ccb_support_tbl) / sizeof(struct ccb_gear_id_mapping);
  296. for (i = 0; i < count; i++) {
  297. if (gear_id == ccb_support_tbl[i].gear_id)
  298. return ccb_support_tbl[i].size;
  299. }
  300. return 0xFFFFFFFF;
  301. }
  302. static void set_ccb_gear_val(unsigned int gear_id)
  303. {
  304. int ret;
  305. char env_buf[12];
  306. memset(env_buf, 0, sizeof(env_buf));
  307. snprintf(env_buf, sizeof(env_buf), "%u", gear_id);
  308. ret = set_env("md1_ccb_cap_gear", env_buf);
  309. if (ret < 0) {
  310. ALWAYS_LOG("set env[md1_ccb_cap_gear]fail, ret=%d\n", ret);
  311. assert(0);
  312. } else
  313. ALWAYS_LOG("set env[md1_ccb_cap_gear]%d; ret = %d\n",
  314. gear_id, ret);
  315. }
  316. static unsigned int get_ccb_gear_val(void)
  317. {
  318. unsigned int md1_ccb_cap_gear;
  319. unsigned int md1_ccb_size;
  320. if (g_boot_mode == META_BOOT || true == get_atm_enable_status()) {
  321. ALWAYS_LOG("meta mode[md1_ccb_cap_gear]%d\n", CCB_DATA_BUF_DEFAULT_GEAR);
  322. set_ccb_gear_val(CCB_DATA_BUF_DEFAULT_GEAR);
  323. return CCB_DATA_BUF_DEFAULT_GEAR;
  324. }
  325. md1_ccb_cap_gear = str2uint(get_env("md1_ccb_cap_gear"));
  326. if (md1_ccb_cap_gear != 0) {
  327. md1_ccb_size = get_ccb_size_from_gear_id(md1_ccb_cap_gear);
  328. if (md1_ccb_size == 0xFFFFFFFF) {
  329. ALWAYS_LOG("CCB cfg abnormal!!! un-support ccb gear id: %d\n", md1_ccb_cap_gear);
  330. assert(0);
  331. }
  332. ALWAYS_LOG("get env[md1_ccb_cap_gear]%d\n", md1_ccb_cap_gear);
  333. return md1_ccb_cap_gear;
  334. } else {
  335. #ifdef MTK_DYNAMIC_CCB_BUFFER_GEAR_ID
  336. md1_ccb_cap_gear = MTK_DYNAMIC_CCB_BUFFER_GEAR_ID;
  337. #else
  338. md1_ccb_cap_gear = CCB_DATA_BUF_DEFAULT_GEAR;
  339. #endif
  340. set_ccb_gear_val(md1_ccb_cap_gear);
  341. return md1_ccb_cap_gear;
  342. }
  343. }
  344. static void set_gear_id_list()
  345. {
  346. int ret;
  347. ret = set_env("md1_ccb_gear_list",
  348. "1(2,20);2(2,10);3(0,0);11(2,2);6(2,50)");
  349. if (ret) {
  350. ALWAYS_LOG("set_gear_id_list error: %x\n", ret);
  351. }
  352. }
  353. static int cal_share_mem_layout(int load_flag)
  354. {
  355. unsigned int md1_ccb_cap_gear;
  356. unsigned int md1_bank4_cache_offset;
  357. unsigned int md1_ccb_size = CCB_DATA_BUF_SIZE;
  358. unsigned char * ccb_data_buf = NULL;
  359. unsigned char *cacheable_buf = NULL;
  360. unsigned int cacheable_buf_sz = 0;
  361. int i;
  362. #if WITH_GZ_MD_SHAREMEM
  363. unsigned int mtee_support = 1;
  364. #endif
  365. ALWAYS_LOG("smem_info.base_addr: %x\n", (unsigned int)smem_info.base_addr);
  366. /* cacheable memory start: */
  367. md1_ccb_cap_gear = get_ccb_gear_val();
  368. md1_ccb_size = get_ccb_size_from_gear_id(md1_ccb_cap_gear);
  369. set_gear_id_list();
  370. ALWAYS_LOG("allocate ccb data buffer0x%x\n", md1_ccb_size);
  371. if (insert_ccci_tag_inf("ccb_gear_id", (char*)&md1_ccb_cap_gear, sizeof(unsigned int)) < 0)
  372. ALWAYS_LOG("insert ccb_gear_id fail\n");
  373. if (udc_support_at_img) {
  374. udc_size.noncache_size = CCCI_SMEM_SIZE_UDC_NONCACHE;
  375. udc_size.cache_size = CCCI_SMEM_SIZE_UDC_CACHE;
  376. }
  377. for (i = 0; i < sizeof(csmem_layout)/sizeof(csmem_item_t); i++) {
  378. switch (csmem_layout[i].item_cnt) {
  379. case SMEM_USER_RAW_MD_CONSYS:
  380. csmem_layout[i].csmem_buffer_size = consys_size_at_img;
  381. break;
  382. case SMEM_USER_NON_PADDING:
  383. if (i != (sizeof(csmem_layout)/sizeof(csmem_item_t) - 1))
  384. csmem_layout[i].csmem_buffer_size =
  385. ((cacheable_buf_sz + (0x100000 - 1))&(~(0x100000 - 1)))
  386. - cacheable_buf_sz;
  387. else
  388. csmem_layout[i].csmem_buffer_size = 0;
  389. break;
  390. case SMEM_USER_CCB_START:
  391. csmem_layout[i].csmem_buffer_size = md1_ccb_size;
  392. break;
  393. default:
  394. break;
  395. }
  396. if (csmem_layout[i].item_cnt == SMEM_USER_RAW_UDC_DESCTAB)
  397. csmem_layout[i].csmem_buffer_size = udc_size.cache_size;
  398. cacheable_buf_sz += csmem_layout[i].csmem_buffer_size;
  399. ALWAYS_LOG("%s:cacheable_buf_sz=0x%x,item=%d,size=0x%x\n",
  400. __func__, cacheable_buf_sz, csmem_layout[i].item_cnt, csmem_layout[i].csmem_buffer_size);
  401. }
  402. if (cacheable_buf_sz != 0)
  403. cacheable_buf = ccci_request_named_mem("ap_md_c_smem", cacheable_buf_sz);
  404. if (cacheable_buf_sz == NULL) {
  405. csmem_info.csmem_buffer_addr = 0;
  406. csmem_info.csmem_buffer_size = 0;
  407. csmem_info.csmem_buffer_size = 0;
  408. csmem_info.item_cnt = 0;
  409. ccb_info.ccb_data_buffer_addr = 0;
  410. ccb_info.ccb_data_buffer_size = 0;
  411. ALWAYS_LOG("allocate ccb data buffer share memory fail\n");
  412. } else {
  413. csmem_info.csmem_buffer_addr = (unsigned long long)((unsigned long)cacheable_buf);
  414. csmem_info.md_offset = MAX_SMEM_SIZE - CACHABLE_SMEM_MAX_SIZE;
  415. csmem_info.csmem_buffer_size = cacheable_buf_sz;
  416. csmem_info.item_cnt = sizeof(csmem_layout)/sizeof(csmem_item_t);
  417. ALWAYS_LOG("cache_buffer_addr: %x\n", (unsigned int)csmem_info.csmem_buffer_addr);
  418. ALWAYS_LOG("cache_buffer_size: %x\n", csmem_info.csmem_buffer_size);
  419. ALWAYS_LOG("cache_buffer_itm_cnt: %x\n", (unsigned int)csmem_info.item_cnt);
  420. for (i = 0; i < sizeof(csmem_layout)/sizeof(csmem_item_t); i++) {
  421. if (i == 0) {
  422. csmem_layout[0].csmem_buffer_addr =
  423. csmem_info.csmem_buffer_addr;
  424. csmem_layout[0].md_offset = 0;
  425. } else {
  426. cacheable_buf =
  427. csmem_layout[i - 1].csmem_buffer_addr +
  428. csmem_layout[i - 1].csmem_buffer_size;
  429. csmem_layout[i].csmem_buffer_addr =
  430. (unsigned long long)((unsigned long)cacheable_buf);
  431. csmem_layout[i].md_offset =
  432. csmem_layout[i - 1].md_offset +
  433. csmem_layout[i - 1].csmem_buffer_size;
  434. }
  435. if (csmem_layout[i].item_cnt == SMEM_USER_CCB_START)
  436. ccb_data_buf = csmem_layout[i].csmem_buffer_addr;
  437. }
  438. if (insert_ccci_tag_inf("md1_bank4_cache_info", (char *)&csmem_info,
  439. sizeof(csmem_info)) < 0)
  440. ALWAYS_LOG("insert md1_smem_cahce_info fail\n");
  441. if (insert_ccci_tag_inf("md1_bank4_cache_layout", (char *)&csmem_layout,
  442. sizeof(csmem_layout)) < 0)
  443. ALWAYS_LOG("insert md1_smem_cahce_layout fail\n");
  444. for (i = 0; i < sizeof(csmem_layout)/sizeof(csmem_item_t); i++) {
  445. ALWAYS_LOG("cache[%d]_buffer_addr: %x\n", i,
  446. (unsigned int)csmem_layout[i].csmem_buffer_addr);
  447. ALWAYS_LOG("cache[%d]_buffer_offset: %x\n", i,
  448. (unsigned int)csmem_layout[i].md_offset);
  449. ALWAYS_LOG("cache[%d]_buffer_size: %x\n", i,
  450. csmem_layout[i].csmem_buffer_size);
  451. }
  452. //ccb_data_buf = cacheable_buf + SMEM_MD_CONSYS_SIZE;
  453. ccb_info.ccb_data_buffer_addr = (unsigned long long)((unsigned long)ccb_data_buf);
  454. ccb_info.ccb_data_buffer_size = md1_ccb_size;
  455. /* CCB in slot3,4 */
  456. md1_bank4_cache_offset = 96 * 1024 * 1024;;
  457. if (insert_ccci_tag_inf("md1_smem_cahce_offset", (char*)&md1_bank4_cache_offset,
  458. sizeof(md1_bank4_cache_offset)) < 0)
  459. ALWAYS_LOG("insert md1_smem_cahce_offset fail\n");
  460. if (insert_ccci_tag_inf("ccb_info", (char*)&ccb_info, sizeof(ccb_layout_t)) < 0)
  461. ALWAYS_LOG("insert ccb_info fail\n");
  462. ALWAYS_LOG("ccb_info.ccb_data_buffer_addr: %x\n", (unsigned int)ccb_info.ccb_data_buffer_addr);
  463. ALWAYS_LOG("ccb_info.ccb_data_buffer_size: %x\n", ccb_info.ccb_data_buffer_size);
  464. if (insert_ccci_tag_inf("udc_layout", (char*)&udc_size,
  465. sizeof(struct _udc_info)) < 0)
  466. ALWAYS_LOG("insert udc_layout fail\n");
  467. ALWAYS_LOG("udc_noncache_size: 0x%x,udc_cache_size: 0x%x\n",
  468. udc_size.noncache_size, udc_size.cache_size);
  469. }
  470. /* cacheable memory End */
  471. ALWAYS_LOG("smem_info.total_smem_size: %x\n", smem_info.total_smem_size);
  472. /* insert share memory layout to lk info */
  473. if (insert_ccci_tag_inf("smem_layout", (char*)&smem_info, sizeof(smem_layout_t)) < 0)
  474. ALWAYS_LOG("insert smem_layout fail\n");
  475. ALWAYS_LOG("md1 phy capture size: %x\n", md1_phy_cap_size);
  476. if (insert_ccci_tag_inf("md1_phy_cap", (char*)&md1_phy_cap_size, sizeof(md1_phy_cap_size)) < 0)
  477. ALWAYS_LOG("insert md1_phy_cap fail\n");
  478. /* slot 5 */
  479. #if WITH_GZ_MD_SHAREMEM
  480. if (g_boot_arg->gz_md_shm_pa && g_boot_arg->gz_md_shm_sz) {
  481. ALWAYS_LOG("mtee.base_addr: 0x%x, 0x%x\n",
  482. (unsigned int)g_boot_arg->gz_md_shm_pa, g_boot_arg->gz_md_shm_sz);
  483. if (insert_ccci_tag_inf("mtee_support", (char *)&mtee_support,
  484. sizeof(mtee_support)) < 0)
  485. ALWAYS_LOG("insert mtee_support fail\n");
  486. }
  487. #endif
  488. return (int)smem_info.total_smem_size;
  489. }
  490. /***************************************************************************************************
  491. ** HW remap section
  492. ***************************************************************************************************/
  493. extern unsigned int ddr_enable_4gb(void)__attribute__((weak));
  494. static int is_4gb_ddr_support_en(void)
  495. {
  496. int ret;
  497. if (ddr_enable_4gb) {
  498. ret = ddr_enable_4gb();
  499. ALWAYS_LOG("ddr_enable_4GB sta:%d\n", ret);
  500. return ret;
  501. } else {
  502. ALWAYS_LOG("ddr 4GB disable\n");
  503. return 0;
  504. }
  505. }
  506. /*-------- Register base part -------------------------------*/
  507. /* HW remap for MD1 */
  508. #define INFRA_AO_BASE (0x10001000)
  509. /* -- MD1 Bank 0 */
  510. #define MD1_BANK0_MAP0 (INFRA_AO_BASE + 0x300)
  511. #define MD1_BANK0_MAP1 (INFRA_AO_BASE + 0x304)
  512. #define MD1_BANK0_MAP2 (INFRA_AO_BASE + 0x308)
  513. #define MD1_BANK0_MAP3 (INFRA_AO_BASE + 0x30C)
  514. /* -- MD1 Bank 1 */
  515. #define MD1_BANK1_MAP0 (INFRA_AO_BASE + 0x310)
  516. #define MD1_BANK1_MAP1 (INFRA_AO_BASE + 0x314)
  517. #define MD1_BANK1_MAP2 (INFRA_AO_BASE + 0x318)
  518. #define MD1_BANK1_MAP3 (INFRA_AO_BASE + 0x31C)
  519. /* -- MD1 Bank 4 */
  520. #define MD1_BANK4_MAP0 (INFRA_AO_BASE + 0x320)
  521. #define MD1_BANK4_MAP1 (INFRA_AO_BASE + 0x324)
  522. #define MD1_BANK4_MAP2 (INFRA_AO_BASE + 0x328)
  523. #define MD1_BANK4_MAP3 (INFRA_AO_BASE + 0x32C)
  524. /* HW remap lock register */
  525. #define MD_HW_REMAP_LOCK (INFRA_AO_BASE + 0xF80)
  526. #define MD1_LOCK (1<<16)
  527. static int md_mem_ro_rw_remapping(unsigned int md_id, unsigned long long addr)
  528. {
  529. unsigned long long md_img_start_addr;
  530. unsigned int hw_remapping_bank0_map0 = 0;
  531. unsigned int hw_remapping_bank0_map1 = 0;
  532. unsigned int hw_remapping_bank0_map2 = 0;
  533. unsigned int hw_remapping_bank0_map3 = 0;
  534. unsigned int write_val;
  535. switch (md_id) {
  536. case 0: // MD1
  537. hw_remapping_bank0_map0 = MD1_BANK0_MAP0;
  538. hw_remapping_bank0_map1 = MD1_BANK0_MAP1;
  539. hw_remapping_bank0_map2 = MD1_BANK0_MAP2;
  540. hw_remapping_bank0_map3 = MD1_BANK0_MAP3;
  541. break;
  542. default:
  543. ALWAYS_LOG("Invalid md id:%d\n", md_id);
  544. return -1;
  545. }
  546. md_img_start_addr = addr;
  547. ALWAYS_LOG("---> Map 0x00000000 to 0x%llx for MD%d\n", addr, md_id+1);
  548. /* For MDx_BANK0_MAP0 */
  549. write_val = (((md_img_start_addr >> 24) | 0x1) & 0x3FF)
  550. + ((((md_img_start_addr + 0x2000000*1) >> 8) | 1<<16) & 0x3FF0000);
  551. DRV_WriteReg32(hw_remapping_bank0_map0, write_val);
  552. ALWAYS_LOG("BANK0_MAP0 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map0));
  553. /* For MDx_BANK0_MAP1 */
  554. write_val = ((((md_img_start_addr + 0x2000000*2) >> 24) | 0x1) & 0x3FF)
  555. + ((((md_img_start_addr + 0x2000000*3) >> 8) | 1<<16) & 0x3FF0000);
  556. DRV_WriteReg32(hw_remapping_bank0_map1, write_val);
  557. ALWAYS_LOG("BANK0_MAP1 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map1));
  558. /* For MDx_BANK0_MAP2 */
  559. write_val = ((((md_img_start_addr + 0x2000000*4) >> 24) | 0x1) & 0x3FF)
  560. + ((((md_img_start_addr + 0x2000000*5) >> 8) | 1<<16) & 0x3FF0000);
  561. DRV_WriteReg32(hw_remapping_bank0_map2, write_val);
  562. ALWAYS_LOG("BANK0_MAP2 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map2));
  563. /* For MDx_BANK0_MAP3 */
  564. write_val = ((((md_img_start_addr + 0x2000000*6) >> 24) | 0x1) & 0x3FF)
  565. + ((((md_img_start_addr + 0x2000000*7) >> 8) | 1<<16) & 0x3FF0000);
  566. DRV_WriteReg32(hw_remapping_bank0_map3, write_val);
  567. ALWAYS_LOG("BANK0_MAP3 value:0x%X\n", DRV_Reg32(hw_remapping_bank0_map3));
  568. #ifdef DUMMY_AP_MODE
  569. /* For 256~512MB */
  570. if (md_id == MD_SYS1) {
  571. write_val = ((((md_img_start_addr + 0x2000000*8) >> 24) | 0x1) & 0x3FF)
  572. + ((((md_img_start_addr + 0x2000000*9) >> 8) | 1<<16) & 0x3FF0000);
  573. DRV_WriteReg32(MD1_BANK1_MAP0, write_val);
  574. ALWAYS_LOG("BANK1_MAP0 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP0));
  575. write_val = ((((md_img_start_addr + 0x2000000*10) >> 24) | 0x1) & 0x3FF)
  576. + ((((md_img_start_addr + 0x2000000*11) >> 8) | 1<<16) & 0x3FF0000);
  577. DRV_WriteReg32(MD1_BANK1_MAP1, write_val);
  578. ALWAYS_LOG("BANK1_MAP1 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP1));
  579. write_val = ((((md_img_start_addr + 0x2000000*12) >> 24) | 0x1) & 0x3FF)
  580. + ((((md_img_start_addr + 0x2000000*13) >> 8) | 1<<16) & 0x3FF0000);
  581. DRV_WriteReg32(MD1_BANK1_MAP2, write_val);
  582. ALWAYS_LOG("BANK1_MAP2 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP2));
  583. write_val = ((((md_img_start_addr + 0x2000000*14) >> 24) | 0x1) & 0x3FF)
  584. + ((((md_img_start_addr + 0x2000000*15) >> 8) | 1<<16) & 0x3FF0000);
  585. DRV_WriteReg32(MD1_BANK1_MAP3, write_val);
  586. ALWAYS_LOG("BANK1_MAP3 value:0x%X\n", DRV_Reg32(MD1_BANK1_MAP3));
  587. }
  588. #endif
  589. return 0;
  590. }
  591. static int md_bank4_remapping_by_slot(unsigned int md_id, unsigned long long addr, int slot)
  592. {
  593. unsigned int hw_remapping_bank4_map0 = 0;
  594. unsigned int hw_remapping_bank4_map1 = 0;
  595. unsigned int hw_remapping_bank4_map2 = 0;
  596. unsigned int hw_remapping_bank4_map3 = 0;
  597. unsigned int curr_val;
  598. switch (md_id) {
  599. case 0: // MD1
  600. hw_remapping_bank4_map0 = MD1_BANK4_MAP0;
  601. hw_remapping_bank4_map1 = MD1_BANK4_MAP1;
  602. hw_remapping_bank4_map2 = MD1_BANK4_MAP2;
  603. hw_remapping_bank4_map3 = MD1_BANK4_MAP3;
  604. break;
  605. default:
  606. ALWAYS_LOG("Invalid md id:%d\n", md_id);
  607. return -1;
  608. }
  609. switch(slot) {
  610. case 0:
  611. curr_val = DRV_Reg32(hw_remapping_bank4_map0);
  612. curr_val &= ~0x3FF;
  613. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  614. DRV_WriteReg32(hw_remapping_bank4_map0, curr_val);
  615. ALWAYS_LOG("BANK4_MAP0 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map0));
  616. break;
  617. case 1:
  618. curr_val = DRV_Reg32(hw_remapping_bank4_map0);
  619. curr_val &= ~0x3FF0000;
  620. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  621. DRV_WriteReg32(hw_remapping_bank4_map0, curr_val);
  622. ALWAYS_LOG("BANK4_MAP0 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map0));
  623. break;
  624. case 2:
  625. curr_val = DRV_Reg32(hw_remapping_bank4_map1);
  626. curr_val &= ~0x3FF;
  627. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  628. DRV_WriteReg32(hw_remapping_bank4_map1, curr_val);
  629. ALWAYS_LOG("BANK4_MAP1 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map1));
  630. break;
  631. case 3:
  632. curr_val = DRV_Reg32(hw_remapping_bank4_map1);
  633. curr_val &= ~0x3FF0000;
  634. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  635. DRV_WriteReg32(hw_remapping_bank4_map1, curr_val);
  636. ALWAYS_LOG("BANK4_MAP1 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map1));
  637. break;
  638. case 4:
  639. curr_val = DRV_Reg32(hw_remapping_bank4_map2);
  640. curr_val &= ~0x3FF;
  641. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  642. DRV_WriteReg32(hw_remapping_bank4_map2, curr_val);
  643. ALWAYS_LOG("BANK4_MAP2 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map2));
  644. break;
  645. case 5:
  646. curr_val = DRV_Reg32(hw_remapping_bank4_map2);
  647. curr_val &= ~0x3FF0000;
  648. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  649. DRV_WriteReg32(hw_remapping_bank4_map2, curr_val);
  650. ALWAYS_LOG("BANK4_MAP2 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map2));
  651. break;
  652. case 6:
  653. curr_val = DRV_Reg32(hw_remapping_bank4_map3);
  654. curr_val &= ~0x3FF;
  655. curr_val |= (((addr >> 24) | 0x1) & 0x3FF);
  656. DRV_WriteReg32(hw_remapping_bank4_map3, curr_val);
  657. ALWAYS_LOG("BANK4_MAP3 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map3));
  658. break;
  659. case 7:
  660. curr_val = DRV_Reg32(hw_remapping_bank4_map3);
  661. curr_val &= ~0x3FF0000;
  662. curr_val |= (((addr >> 8) | (1<<16)) & 0x3FF0000);
  663. DRV_WriteReg32(hw_remapping_bank4_map3, curr_val);
  664. ALWAYS_LOG("BANK4_MAP3 value:0x%X\n", DRV_Reg32(hw_remapping_bank4_map3));
  665. break;
  666. default:
  667. ALWAYS_LOG("Invalid slot id:%d\n", slot);
  668. return -1;
  669. }
  670. return 0;
  671. }
  672. static int md_smem_rw_remapping(unsigned int md_id, unsigned long long addr)
  673. {
  674. unsigned int i, csmem_32M_cnt;
  675. ALWAYS_LOG("---> Map 0x40000000 to 0x%llx for MD%d\n", addr, md_id+1);
  676. csmem_32M_cnt = 8 - CACHABLE_SMEM_MAX_SIZE/(32*1024*1024);
  677. for (i = 0; i < csmem_32M_cnt; i++)
  678. md_bank4_remapping_by_slot(md_id, addr + 0x2000000*i, i);
  679. #if WITH_GZ_MD_SHAREMEM
  680. if (g_boot_arg->gz_md_shm_pa && g_boot_arg->gz_md_shm_sz) {
  681. md_bank4_remapping_by_slot(MD_SYS1, g_boot_arg->gz_md_shm_pa, 5);
  682. }
  683. #endif
  684. /* remapping cacheable to last 2*32M in bank4 */
  685. ALWAYS_LOG("---> Map 0x40000000+0x%x to 0x%llx for MD%d\n", csmem_info.md_offset,
  686. csmem_info.csmem_buffer_addr, md_id+1);
  687. for (i = csmem_32M_cnt; i < 8; i++)
  688. md_bank4_remapping_by_slot(md_id, csmem_info.csmem_buffer_addr + 0x2000000*(i-csmem_32M_cnt), i);
  689. return 0;
  690. }
  691. static void md_emi_remapping_lock(unsigned int md_id)
  692. {
  693. unsigned int reg_val;
  694. unsigned int lock_bit;
  695. switch (md_id) {
  696. case 0: // MD1
  697. lock_bit = MD1_LOCK;
  698. break;
  699. default:
  700. ALWAYS_LOG("Invalid md id:%d for lock\n", md_id);
  701. return;
  702. }
  703. reg_val = DRV_Reg32(MD_HW_REMAP_LOCK);
  704. ALWAYS_LOG("before hw remap lock: MD1[%d]\n", !!(reg_val&MD1_LOCK));
  705. DRV_WriteReg32(MD_HW_REMAP_LOCK, (reg_val|lock_bit));
  706. reg_val = DRV_Reg32(MD_HW_REMAP_LOCK);
  707. ALWAYS_LOG("before hw remap lock: MD1[%d]\n", !!(reg_val&MD1_LOCK));
  708. }
  709. /* =================================================== */
  710. /* MPU Region defination */
  711. /* =================================================== */
  712. /* Note: This structure should sync with Kernel!!!! */
  713. typedef unsigned long long mpu_att_t;
  714. typedef struct _mpu_cfg {
  715. unsigned int start;
  716. unsigned int end;
  717. int region;
  718. unsigned int permission[EMI_MPU_DGROUP_NUM];
  719. int relate_region;
  720. } mpu_cfg_t;
  721. #define MPU_REGION_ID_MD1_ROM 11
  722. #define MPU_REGION_ID_MD_DSP1 12 /*DSP RO*/
  723. #define MPU_REGION_ID_MD_DSP2 13 /*DSP RW*/
  724. #define MPU_REGION_ID_MD_DRDI 14
  725. #define MPU_REGION_ID_MD1_MCURW_HWRW 15
  726. #define MPU_REGION_ID_MD1_MCURW_HWRO 16
  727. #define MPU_REGION_ID_MD1_MCURO_HWRW 17
  728. #define MPU_REGION_ID_PADDING1 19
  729. #define MPU_REGION_ID_PADDING2 20
  730. #define MPU_REGION_ID_MD_PROTECT 22
  731. #define MPU_REGION_ID_MD_CONSYS 23
  732. #define MPU_REGION_ID_MD1_CCB 24
  733. #define MPU_REGION_ID_MD1_SMEM 25
  734. #define MPU_REGION_ID_AP 31
  735. #define MPU_REGION_ID_TOTAL_NUM (MPU_REGION_ID_AP + 1)
  736. #define MPU_MDOMAIN_ID_AP 0
  737. #define MPU_MDOMAIN_ID_MD1 1
  738. #define MPU_MDOMAIN_ID_MDHW 7
  739. #define MPU_MDOMAIN_ID_TOTAL_NUM 16
  740. static const mpu_att_t mpu_att_default[MPU_REGION_ID_TOTAL_NUM][MPU_MDOMAIN_ID_TOTAL_NUM] = {
  741. /*===================================================================================================================*/
  742. /* No | | D0(AP) | D1(MD1) | D2(CONN) | D3(SCP) | D4(MM) | D5(Rsv ) | D6(MFG) | D7(MDHW)
  743. |D8(SSPM) |D9(SPM) |D10(ADSP) | D11-15(Rsv) */
  744. /*--------------+-----------------------------------------------------------------------------------------------------------------------*/
  745. /* 0*/{}, /*Secure OS*/
  746. /* 1*/{}, /*ATF*/
  747. /* 2*/{}, /*Secure Memory*/
  748. /* 3*/{}, /*Tinysys-SSPM ROM*/
  749. /* 4*/{}, /*Tinysys-SSPM share buffer*/
  750. /* 5*/{}, /*Tinysys-SCP ROM*/
  751. /* 6*/{}, /*Tinysys-SCP share buffer*/
  752. /* 7*/{}, /*Trusted UI*/
  753. /* 8*/{}, /*AMMS*/
  754. /* 9*/{}, /*AMMS*/
  755. /*10*/{}, /*Security CCCI share*/
  756. /*11*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN},
  757. /*12*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN},
  758. /*13*/{ SEC_R_NSEC_R, NO_PROTECTION, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  759. /*14*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN}, /*DRDI*/
  760. /*15*/{ SEC_R_NSEC_R, NO_PROTECTION, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  761. /*16*/{ SEC_R_NSEC_R, NO_PROTECTION, [2 ... 6] = FORBIDDEN, SEC_R_NSEC_R, [8 ... 15] = FORBIDDEN},
  762. /*17*/{ SEC_R_NSEC_R, SEC_R_NSEC_R, [2 ... 6] = FORBIDDEN, NO_PROTECTION, [8 ... 15] = FORBIDDEN},
  763. /*18*/{ SEC_R_NSEC_R, FORBIDDEN, [2 ... 6] = FORBIDDEN, FORBIDDEN, [8 ... 15] = FORBIDDEN}, /* WIFI Driver */
  764. /*19*/{ SEC_R_NSEC_R, FORBIDDEN, [2 ... 6] = FORBIDDEN, FORBIDDEN, [8 ... 15] = FORBIDDEN},
  765. /*20*/{ SEC_R_NSEC_R, FORBIDDEN, [2 ... 6] = FORBIDDEN, FORBIDDEN, [8 ... 15] = FORBIDDEN},
  766. /*21*/{}, /* VPU */
  767. /*22*/{
  768. NO_PROTECTION, SEC_R_NSEC_R, [2 ... 3] = FORBIDDEN, NO_PROTECTION, NO_PROTECTION, SEC_R_NSEC_RW,
  769. [7 ... 15] = FORBIDDEN
  770. },
  771. /*23*/{ SEC_R_NSEC_R, NO_PROTECTION, NO_PROTECTION, [3 ... 15] = FORBIDDEN},/*MD-Consys Direct Path*/
  772. /*24*/{ NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, [3 ... 6] = FORBIDDEN, NO_PROTECTION, FORBIDDEN, FORBIDDEN,
  773. NO_PROTECTION, [11 ... 15] = FORBIDDEN},
  774. /*25*/{ NO_PROTECTION, NO_PROTECTION, FORBIDDEN, NO_PROTECTION,[4 ... 6] = FORBIDDEN, NO_PROTECTION,
  775. [8 ... 15] = FORBIDDEN},
  776. /*26*/{}, /*WIFI EMI FW*/
  777. /*27*/{}, /*WMT*/
  778. /*28*/{}, /*ADSP*/
  779. /*29*/{}, /*GPS offload*/
  780. /*30*/{}, /*Set in LK MD dynamic*/
  781. /*31*/{
  782. NO_PROTECTION, FORBIDDEN, [2 ... 3] = FORBIDDEN, NO_PROTECTION, FORBIDDEN, SEC_R_NSEC_RW,
  783. FORBIDDEN, NO_PROTECTION, NO_PROTECTION, [10 ... 15] = FORBIDDEN
  784. },
  785. };
  786. #define MPU_STR_BUF_SIZE 64
  787. static void get_mpu_attr_str(int lock, unsigned int apc[EMI_MPU_DGROUP_NUM], char buf[], int size)
  788. {
  789. unsigned long long curr_attr;
  790. char ch = lock?'L':'U';
  791. if (EMI_MPU_DGROUP_NUM != 2) {
  792. ALWAYS_LOG("[error]abnormal mpu domain group number %d\n", EMI_MPU_DGROUP_NUM);
  793. return;
  794. }
  795. curr_attr = ((unsigned long long)apc[1] << 32) | apc[0];
  796. snprintf(buf, size, "%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld-%lld(%c)",
  797. curr_attr&7, (curr_attr>>3)&7, (curr_attr>>6)&7, (curr_attr>>9)&7,
  798. (curr_attr>>12)&7, (curr_attr>>15)&7, (curr_attr>>18)&7, (curr_attr>>21)&7,
  799. (curr_attr>>32)&7, (curr_attr>>35)&7, (curr_attr>>38)&7, (curr_attr>>41)&7,
  800. (curr_attr>>44)&7, (curr_attr>>47)&7, (curr_attr>>50)&7, (curr_attr>>53)&7, ch);
  801. }
  802. static const unsigned char region_mapping_at_hdr_md1[] = {
  803. MPU_REGION_ID_MD1_ROM, MPU_REGION_ID_MD1_MCURO_HWRW, MPU_REGION_ID_MD1_MCURW_HWRO,
  804. MPU_REGION_ID_MD1_MCURW_HWRW
  805. };
  806. static const int free_mpu_region[] = {
  807. MPU_REGION_ID_PADDING1, MPU_REGION_ID_PADDING2, MPU_REGION_ID_MD_PROTECT, -1};
  808. static int curr_free_mpu_idx;
  809. static int get_free_mpu_region(void)
  810. {
  811. int ret;
  812. if (curr_free_mpu_idx < (int)(sizeof(free_mpu_region)/sizeof(int))) {
  813. ret = free_mpu_region[curr_free_mpu_idx];
  814. curr_free_mpu_idx++;
  815. } else
  816. ret = -LD_ERR_PLAT_MPU_REGION_EMPTY;
  817. return ret;
  818. }
  819. /*make sure protect region is the last valid region*/
  820. static int get_md_protect_mpu_region(void)
  821. {
  822. int last_index;
  823. last_index = (int)(sizeof(free_mpu_region)/sizeof(int)) -1;
  824. if (free_mpu_region[last_index] < 0) /*free region end by -1*/
  825. last_index--; /*make sure it is index of last valid region*/
  826. if (curr_free_mpu_idx > last_index)
  827. return -LD_ERR_PLAT_MPU_REGION_EMPTY;
  828. return free_mpu_region[last_index];
  829. }
  830. static void get_mpu_region_default_access_att(
  831. unsigned int apc[EMI_MPU_DGROUP_NUM], int region, int lock)
  832. {
  833. #ifdef ENABLE_EMI_PROTECTION
  834. SET_ACCESS_PERMISSION(apc, lock,
  835. mpu_att_default[region][15], mpu_att_default[region][14],
  836. mpu_att_default[region][13], mpu_att_default[region][12],
  837. mpu_att_default[region][11], mpu_att_default[region][10],
  838. mpu_att_default[region][9], mpu_att_default[region][8],
  839. mpu_att_default[region][7], mpu_att_default[region][6],
  840. mpu_att_default[region][5], mpu_att_default[region][4],
  841. mpu_att_default[region][3], mpu_att_default[region][2],
  842. mpu_att_default[region][1], mpu_att_default[region][0]);
  843. #endif
  844. }
  845. static void mpu_attr_calculate(
  846. unsigned int apc[EMI_MPU_DGROUP_NUM], int region_id, unsigned int request_attr)
  847. {
  848. mpu_att_t tmp_mpu_att[MPU_MDOMAIN_ID_TOTAL_NUM], i;
  849. for (i = 0; i < MPU_MDOMAIN_ID_TOTAL_NUM; i++)
  850. tmp_mpu_att[i] = mpu_att_default[region_id][i];
  851. /* AP MD1 MDHW: AP */
  852. if ((request_attr & 0xF) <= FORBIDDEN)
  853. tmp_mpu_att[MPU_MDOMAIN_ID_AP] = (request_attr & 0xF);
  854. /* AP MD1 MDHW: MD1 */
  855. request_attr = (request_attr >> 4);
  856. if ((request_attr & 0xF) <= FORBIDDEN)
  857. tmp_mpu_att[MPU_MDOMAIN_ID_MD1] = (request_attr & 0xF);
  858. /* AP MD1 MDHW: MDHW */
  859. request_attr = (request_attr >> 4);
  860. if ((request_attr & 0xF) <= FORBIDDEN)
  861. tmp_mpu_att[MPU_MDOMAIN_ID_MDHW] = (request_attr & 0xF);
  862. #ifdef ENABLE_EMI_PROTECTION
  863. /* MPU region lock */
  864. SET_ACCESS_PERMISSION(apc, 1,
  865. tmp_mpu_att[15], tmp_mpu_att[14],
  866. tmp_mpu_att[13], tmp_mpu_att[12],
  867. tmp_mpu_att[11], tmp_mpu_att[10],
  868. tmp_mpu_att[9], tmp_mpu_att[8],
  869. tmp_mpu_att[7], tmp_mpu_att[6],
  870. tmp_mpu_att[5], tmp_mpu_att[4],
  871. tmp_mpu_att[3], tmp_mpu_att[2],
  872. tmp_mpu_att[1], tmp_mpu_att[0]);
  873. #endif
  874. }
  875. static void ccci_mem_access_cfg(mpu_cfg_t *mpu_cfg_list, int clear)
  876. {
  877. #ifdef ENABLE_EMI_PROTECTION
  878. mpu_cfg_t *curr;
  879. struct emi_region_info_t region_info;
  880. unsigned int curr_attr[EMI_MPU_DGROUP_NUM];
  881. char buf[MPU_STR_BUF_SIZE];
  882. int i;
  883. if (NULL == mpu_cfg_list)
  884. return;
  885. SET_ACCESS_PERMISSION(curr_attr, 0,
  886. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION,
  887. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION,
  888. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION,
  889. NO_PROTECTION, NO_PROTECTION, NO_PROTECTION, NO_PROTECTION);
  890. for (curr = mpu_cfg_list; curr->region != -1; curr++) {
  891. if (clear) {
  892. region_info.region = (unsigned int)curr->region;
  893. emi_mpu_clear_protection(&region_info);
  894. get_mpu_attr_str(0, curr_attr, buf, MPU_STR_BUF_SIZE);
  895. ALWAYS_LOG("Clr MPU:S:0x%x E:0x%x A:<%d>[0~15]%s\n",
  896. 0, 0, curr->region, buf);
  897. } else {
  898. region_info.start = curr->start;
  899. region_info.end = curr->end;
  900. region_info.region = (unsigned int)curr->region;
  901. for (i = 0; i < EMI_MPU_DGROUP_NUM; i++)
  902. region_info.apc[i] = curr->permission[i];
  903. emi_mpu_set_protection(&region_info);
  904. get_mpu_attr_str(0, curr->permission, buf, MPU_STR_BUF_SIZE);
  905. ALWAYS_LOG("Set MPU:S:0x%x E:0x%x A:<%d>[0~15]%s\n",
  906. curr->start, curr->end, curr->region, buf);
  907. }
  908. }
  909. #endif
  910. }
  911. /*--------- Implement one by one -------------------------------------------------------------------------------*/
  912. int plat_get_padding_mpu_num(void)
  913. {
  914. return (int)(sizeof(free_mpu_region)/sizeof(unsigned int)) - 1;
  915. }
  916. void plat_notify_secure(unsigned long base_addr)
  917. {
  918. unsigned long addr_get;
  919. mt_secure_call(MTK_SIP_LK_AMMS_MD_BASE_ADDR_AARCH32, base_addr, 0, 0, 0);
  920. addr_get = mt_secure_call(MTK_SIP_LK_AMMS_GET_MD_BASE_ADDR_AARCH32, 0, 0, 0, 0);
  921. ALWAYS_LOG("mt_secure_call: set_addr = 0x%lx, get_addr = 0x%lx\n", base_addr, addr_get);
  922. }
  923. /*---------------------------------------------------------------------------------------------------*/
  924. /* HW remap function implement */
  925. /*---------------------------------------------------------------------------------------------------*/
  926. int plat_apply_hw_remap_for_md_ro_rw(void* info)
  927. {
  928. modem_info_t *md_ld_info = (modem_info_t *)info;
  929. plat_notify_secure(md_ld_info->base_addr);
  930. return md_mem_ro_rw_remapping((unsigned int)md_ld_info->md_id, md_ld_info->base_addr);
  931. }
  932. int plat_apply_hw_remap_for_md_smem(void *addr, int size)
  933. {
  934. /* For share memory final size depends on MD number, just store start address and size
  935. ** actual setting will do later
  936. */
  937. smem_info.base_addr = (unsigned long long)((unsigned long)addr);
  938. return 0;
  939. }
  940. enum SMEM_USER_ID {
  941. SMEM_USER_RAW_DFD = 0,
  942. SMEM_USER_MAX,
  943. };
  944. int get_ccci_md_view_smem_addr_size(unsigned int user_id,
  945. unsigned long long *ap_addr, unsigned int *md_addr, unsigned int *size)
  946. {
  947. int ret = 0;
  948. switch(user_id){
  949. case SMEM_USER_RAW_DFD:
  950. *size = CCCI_SMEM_SIZE_DFD;
  951. if (ap_addr)
  952. *ap_addr = smem_info.base_addr + smem_info.ap_md1_smem_offset + 0x100000;
  953. else
  954. ret = -2;
  955. if (md_addr)
  956. *md_addr = 0x40000000 + smem_info.ap_md1_smem_offset + 0x100000;
  957. else
  958. ret = -3;
  959. break;
  960. default:
  961. ret = -1;
  962. }
  963. if (ret >= 0)
  964. ALWAYS_LOG("[ccci]get_md_view_smem: user_id = 0x%x, ap_addr = 0x%llx, md_addr = 0x%x, size = 0x%x\n",
  965. user_id, *ap_addr, *md_addr, *size);
  966. else
  967. ALWAYS_LOG("[ccci]get_md_view_smem: param error, ret = %d\n", ret);
  968. return ret;
  969. }
  970. /*---------------------------------------------------------------------------------------------------*/
  971. /* check header info collection by plat_post_hdr_info */
  972. /*---------------------------------------------------------------------------------------------------*/
  973. void plat_post_hdr_info(void* hdr, int ver, int id)
  974. {
  975. if (id == MD_SYS1) {
  976. ap_md1_smem_size_at_img =
  977. ((struct md_check_header_v6*)hdr)->ap_md_smem_size;
  978. //parsing check-header: AMMS POS, CONSYS
  979. amms_pos_size_at_img =
  980. ((struct md_check_header_v6*)hdr)->amms_pos_size;
  981. consys_size_at_img =
  982. ((struct md_check_header_v6*)hdr)->consys_size;
  983. udc_support_at_img =
  984. ((struct md_check_header_v6*)hdr)->udc_support;
  985. ALWAYS_LOG("[ccci]parse check-header: amms_pos_size = 0x%x,\
  986. consys_size = 0x%x, udc_support = %d\n", amms_pos_size_at_img,
  987. consys_size_at_img, udc_support_at_img);
  988. }
  989. }
  990. /*---------------------------------------------------------------------------------------------------*/
  991. /* MPU static global variable and mpu relate function implement */
  992. /*---------------------------------------------------------------------------------------------------*/
  993. #define MPU_REGION_TOTAL_NUM (16) /* = MD1+MD3 */
  994. static mpu_cfg_t mpu_tbl[MPU_REGION_TOTAL_NUM];
  995. static int s_g_curr_mpu_num;
  996. /*
  997. ** if set start=0x0, end=0x10000, the actural protected area will be 0x0-0x1FFFF,
  998. ** here we use 64KB align, MPU actually request 32KB align since MT6582, but this works...
  999. ** we assume emi_mpu_set_region_protection will round end address down to 64KB align.
  1000. */
  1001. static void dump_received_pure_mpu_setting(struct image_section_desc *mem_info, int item_num)
  1002. {
  1003. int i;
  1004. for (i =0; i < item_num; i++)
  1005. MPU_DBG_LOG("mpu sec dec %d: offset:%x, size:%x, mpu_attr:%x, ext_flag:%x, relate_idx:%x\n", i,
  1006. mem_info[i].offset, mem_info[i].size, mem_info[i].mpu_attr,
  1007. mem_info[i].ext_flag, mem_info[i].relate_idx);
  1008. }
  1009. static int find_bind_mpu_region(mpu_cfg_t *mpu_tbl_hdr, int item_num, unsigned int bind_key)
  1010. {
  1011. int i;
  1012. for (i = 0; i < item_num; i++) {
  1013. if (mpu_tbl_hdr[i].relate_region == (int)bind_key)
  1014. return i;
  1015. }
  1016. return -1;
  1017. }
  1018. static int md1_mpu_setting_process(void *p_md_ld_info, void *p_mem_info, mpu_cfg_t *mpu_tbl_hdr)
  1019. {
  1020. modem_info_t *md_ld_info = (modem_info_t *)p_md_ld_info;
  1021. struct image_section_desc *mem_info = (struct image_section_desc *)p_mem_info;
  1022. int normal_region_num = 0;
  1023. int total_region_num = 0;
  1024. int curr_idx = 0;
  1025. int i, j;
  1026. int all_range_region_idx = -1;
  1027. int bind_idx;
  1028. int free_region_id;
  1029. int didi_region_idx = -1;
  1030. /* Calculate mpu num and padding num */
  1031. for (i = 0; i < MPU_REGION_TOTAL_NUM; i++) {
  1032. if ((mem_info[i].offset == 0) && (mem_info[i].size == 0))
  1033. break;
  1034. if (mem_info[i].ext_flag & MD_ALL_RANGE)
  1035. all_range_region_idx = i;
  1036. if (mem_info[i].ext_flag & MD_DRDI_REGION)
  1037. didi_region_idx = i;
  1038. }
  1039. total_region_num = i;
  1040. dump_received_pure_mpu_setting(mem_info, total_region_num);
  1041. for (i = 0; i < total_region_num; i++) {
  1042. if (mem_info[i].ext_flag & (MD_DRDI_REGION|MD_ALL_RANGE|NEED_REMOVE|NEED_MPU_MORE))
  1043. continue;
  1044. /* Process normal case first */
  1045. if (curr_idx >= (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char))) {
  1046. ALWAYS_LOG("[error]md%d: mpu region too more %d\n", md_ld_info->md_id+1,
  1047. (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char)));
  1048. return -LD_ERR_PLAT_MPU_REGION_TOO_MORE;
  1049. }
  1050. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[i].offset;
  1051. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[i].size;
  1052. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;/* 64K align */
  1053. mpu_attr_calculate(
  1054. mpu_tbl_hdr[curr_idx].permission, region_mapping_at_hdr_md1[curr_idx], mem_info[i].mpu_attr);
  1055. mpu_tbl_hdr[curr_idx].region = (int)region_mapping_at_hdr_md1[curr_idx];
  1056. mpu_tbl_hdr[curr_idx].relate_region = mem_info[i].relate_idx;
  1057. curr_idx++;
  1058. normal_region_num++;
  1059. }
  1060. if (normal_region_num != (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char))) {
  1061. ALWAYS_LOG("[error]md%d: mpu region not sync %d:%d\n", md_ld_info->md_id+1, normal_region_num,
  1062. (int)(sizeof(region_mapping_at_hdr_md1)/sizeof(unsigned char)));
  1063. return -LD_ERR_PLAT_MPU_REGION_NUM_NOT_SYNC;
  1064. }
  1065. for (i = 0; i < total_region_num; i++) {
  1066. if (mem_info[i].ext_flag & NEED_MPU_MORE) {
  1067. bind_idx = find_bind_mpu_region(mpu_tbl_hdr, curr_idx, mem_info[i].relate_idx);
  1068. if (bind_idx >= 0) {
  1069. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[i].offset;
  1070. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[i].size;
  1071. /* 64K align */
  1072. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;
  1073. for (j = 0; j < EMI_MPU_DGROUP_NUM; j++)
  1074. mpu_tbl_hdr[curr_idx].permission[j] = mpu_tbl_hdr[bind_idx].permission[j];
  1075. /* setting relate region */
  1076. free_region_id = get_free_mpu_region();
  1077. if (free_region_id < 0) {
  1078. ALWAYS_LOG("[error]abnormal free region id %d +\n", free_region_id);
  1079. return -LD_ERR_PLAT_ABNORMAL_FREE_REGION;
  1080. }
  1081. mpu_tbl_hdr[curr_idx].region = free_region_id;
  1082. mpu_tbl_hdr[curr_idx].relate_region = mem_info[i].relate_idx;
  1083. mpu_tbl_hdr[bind_idx].relate_region = free_region_id;
  1084. curr_idx++;
  1085. } else {
  1086. ALWAYS_LOG("md%d: padding array abnormal\n", md_ld_info->md_id+1);
  1087. return -LD_ERR_PLAT_ABNORMAL_PAD_ARRAY;
  1088. }
  1089. }
  1090. }
  1091. /* Apply DRDI if needed */
  1092. if (didi_region_idx >= 0) {
  1093. get_mpu_region_default_access_att(mpu_tbl_hdr[curr_idx].permission, MPU_REGION_ID_MD_DRDI, 1);
  1094. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[didi_region_idx].offset;
  1095. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[didi_region_idx].size;
  1096. /* 64K align */
  1097. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;
  1098. mpu_tbl_hdr[curr_idx].region = MPU_REGION_ID_MD_DRDI;
  1099. mpu_tbl_hdr[curr_idx].relate_region = 0;
  1100. curr_idx++;
  1101. }
  1102. /* Apply MD all range mpu protect setting */
  1103. free_region_id = get_md_protect_mpu_region(); //get_free_mpu_region();
  1104. if (free_region_id < 0) {
  1105. ALWAYS_LOG("[error]no more free region\n");
  1106. return -LD_ERR_PLAT_NO_MORE_FREE_REGION;
  1107. }
  1108. get_mpu_region_default_access_att(mpu_tbl_hdr[curr_idx].permission, free_region_id, 1);
  1109. mpu_tbl_hdr[curr_idx].start = (unsigned int)md_ld_info->base_addr + mem_info[all_range_region_idx].offset;
  1110. /*mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + mem_info[all_range_region_idx].size;*/
  1111. mpu_tbl_hdr[curr_idx].end = mpu_tbl_hdr[curr_idx].start + 256 * 1024 * 1024;
  1112. /* 64K align */
  1113. mpu_tbl_hdr[curr_idx].end = ((mpu_tbl_hdr[curr_idx].end + 0xFFFF)&(~0xFFFF)) - 1;
  1114. mpu_tbl_hdr[curr_idx].region = free_region_id;
  1115. mpu_tbl_hdr[curr_idx].relate_region = 0;
  1116. curr_idx++;
  1117. /* Clear logic relate index to 0 to mark as end */
  1118. for (i = 0; i < curr_idx; i++) {
  1119. if (mpu_tbl_hdr[i].relate_region >= LOGIC_BINDING_IDX_START)
  1120. mpu_tbl_hdr[i].relate_region = 0;
  1121. }
  1122. return curr_idx;
  1123. }
  1124. int plat_send_mpu_info_to_platorm(void *p_md_ld_info, void *p_mem_info)
  1125. {
  1126. modem_info_t *md_ld_info = (modem_info_t *)p_md_ld_info;
  1127. struct image_section_desc *mem_info = (struct image_section_desc *)p_mem_info;
  1128. int md_id = md_ld_info->md_id;
  1129. int ret;
  1130. int i;
  1131. char buf[MPU_STR_BUF_SIZE];
  1132. if (md_id == MD_SYS1) {
  1133. ret = md1_mpu_setting_process(p_md_ld_info, p_mem_info, &mpu_tbl[s_g_curr_mpu_num]);
  1134. if (ret > 0)
  1135. s_g_curr_mpu_num += ret;
  1136. } else if (md_id == MD1_DSP) {
  1137. /* RO part */
  1138. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD_DSP1, 1);
  1139. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)md_ld_info->base_addr + mem_info[0].offset;
  1140. mpu_tbl[s_g_curr_mpu_num].end = mpu_tbl[s_g_curr_mpu_num].start + mem_info[0].size;
  1141. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD_DSP1;
  1142. /* 64K align */
  1143. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  1144. s_g_curr_mpu_num++;
  1145. /* RW part */
  1146. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD_DSP2, 1);
  1147. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)md_ld_info->base_addr + mem_info[1].offset;
  1148. mpu_tbl[s_g_curr_mpu_num].end = mpu_tbl[s_g_curr_mpu_num].start + mem_info[1].size;
  1149. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD_DSP2;
  1150. /* 64K align */
  1151. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  1152. s_g_curr_mpu_num++;
  1153. }
  1154. for (i =0; i < s_g_curr_mpu_num; i++) {
  1155. get_mpu_attr_str(0, mpu_tbl[i].permission, buf, MPU_STR_BUF_SIZE);
  1156. MPU_DBG_LOG("plat mpu dec %d: region:%d[%d], start:0x%x, end:0x%x, attr:%s\n", i,
  1157. mpu_tbl[i].region, mpu_tbl[i].relate_region, mpu_tbl[i].start, mpu_tbl[i].end, buf);
  1158. }
  1159. return 0;
  1160. }
  1161. #ifdef MPU_REGION_ID_MD_CONSYS
  1162. static void emi_mpu_region_setting(unsigned int region, unsigned long long start, unsigned long long end)
  1163. {
  1164. #ifdef ENABLE_EMI_PROTECTION
  1165. struct emi_region_info_t region_info;
  1166. char buf[MPU_STR_BUF_SIZE];
  1167. SET_ACCESS_PERMISSION(region_info.apc, 0,
  1168. mpu_att_default[region][15], mpu_att_default[region][14],
  1169. mpu_att_default[region][13], mpu_att_default[region][12],
  1170. mpu_att_default[region][11], mpu_att_default[region][10],
  1171. mpu_att_default[region][9], mpu_att_default[region][8],
  1172. mpu_att_default[region][7], mpu_att_default[region][6],
  1173. mpu_att_default[region][5], mpu_att_default[region][4],
  1174. mpu_att_default[region][3], mpu_att_default[region][2],
  1175. mpu_att_default[region][1], mpu_att_default[region][0]);
  1176. region_info.start = start;
  1177. region_info.end = end;
  1178. region_info.region = region;
  1179. emi_mpu_set_protection(&region_info);
  1180. get_mpu_attr_str(0, region_info.apc, buf, MPU_STR_BUF_SIZE);
  1181. ALWAYS_LOG("Set MPU:S:0x%llx E:0x%llx A:<%d>[0~15]%s\n",
  1182. start, end, region, buf);
  1183. #endif
  1184. }
  1185. static void md_special_mpu_set()
  1186. {
  1187. unsigned int i;
  1188. for (i = 0; i < sizeof(csmem_layout)/sizeof(csmem_item_t); i++) {
  1189. if (csmem_layout[i].item_cnt == SMEM_USER_RAW_MD_CONSYS) {
  1190. emi_mpu_region_setting(MPU_REGION_ID_MD_CONSYS,
  1191. (csmem_layout[i].md_offset + csmem_info.csmem_buffer_addr),
  1192. (csmem_layout[i].md_offset +csmem_layout[i].csmem_buffer_size + csmem_info.csmem_buffer_addr - 1));
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. #endif
  1198. static void boot_to_dummy_ap_mode(int load_md_flag);
  1199. /*------------------------------------------------------------------------------------------------*/
  1200. /* Note: This function using global variable
  1201. ** if set start=0x0, end=0x10000, the actural protected area will be 0x0-0x1FFFF,
  1202. ** here we use 64KB align, MPU actually request 32KB align since MT6582, but this works...
  1203. ** we assume emi_mpu_set_region_protection will round end address down to 64KB align.
  1204. */
  1205. int plat_apply_platform_setting(int load_md_flag)
  1206. {
  1207. int smem_final_size;
  1208. #ifdef DUMMY_AP_MODE
  1209. /* This function will never return */
  1210. ALWAYS_LOG("boot to dummy ap mode!!!\n");
  1211. boot_to_dummy_ap_mode(load_md_flag);
  1212. return 0;
  1213. #endif
  1214. /* Check loading validation */
  1215. if (((load_md_flag & (1<<MD_SYS1)) == 0) && (load_md_flag & (1<<MD_SYS3))) {
  1216. ALWAYS_LOG("md3 depends on md1,but md1 not loaded\n");
  1217. return -LD_ERR_PLAT_MD1_NOT_RDY;
  1218. }
  1219. if ((load_md_flag & ((1<<MD_SYS1)|(1<<MD_SYS3))) == 0) {
  1220. ALWAYS_LOG("both md1 and md3 not enable\n");
  1221. return 0;
  1222. }
  1223. smem_final_size = cal_share_mem_layout(load_md_flag);
  1224. ALWAYS_LOG("ap md1 share mem MPU need configure\n");
  1225. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD1_SMEM;
  1226. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD1_SMEM, 0);
  1227. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)smem_info.base_addr + smem_info.ap_md1_smem_offset;
  1228. mpu_tbl[s_g_curr_mpu_num].end = (unsigned int)smem_info.base_addr + smem_info.ap_md1_smem_offset
  1229. + smem_final_size; /* ap_md1 share && phy_cap had 2M overlap */
  1230. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  1231. s_g_curr_mpu_num++;
  1232. /* add for ccb data buffer mpu */
  1233. mpu_tbl[s_g_curr_mpu_num].region = MPU_REGION_ID_MD1_CCB;
  1234. get_mpu_region_default_access_att(mpu_tbl[s_g_curr_mpu_num].permission, MPU_REGION_ID_MD1_CCB, 0);
  1235. mpu_tbl[s_g_curr_mpu_num].start = (unsigned int)csmem_info.csmem_buffer_addr;//ccb_info.ccb_data_buffer_addr;
  1236. mpu_tbl[s_g_curr_mpu_num].end = (unsigned int)csmem_info.csmem_buffer_addr + csmem_info.csmem_buffer_size;//ccb_info.ccb_data_buffer_addr + ccb_info.ccb_data_buffer_size;
  1237. mpu_tbl[s_g_curr_mpu_num].end = ((mpu_tbl[s_g_curr_mpu_num].end + 0xFFFF)&(~0xFFFF)) - 1;
  1238. s_g_curr_mpu_num++;
  1239. mpu_tbl[s_g_curr_mpu_num].region = -1; /* mark for end */
  1240. /* Insert mpu tag info */
  1241. if (insert_ccci_tag_inf("md_mpu_inf", (char*)mpu_tbl, sizeof(mpu_cfg_t)*s_g_curr_mpu_num) < 0)
  1242. ALWAYS_LOG("insert md_mpu_inf fail\n");
  1243. if (insert_ccci_tag_inf("md_mpu_num", (char*)&s_g_curr_mpu_num, sizeof(int)) < 0)
  1244. ALWAYS_LOG("insert md_mpu_num fail\n");
  1245. /* Apply all MPU setting */
  1246. ccci_mem_access_cfg(mpu_tbl, 0);
  1247. #ifdef MPU_REGION_ID_MD_CONSYS
  1248. md_special_mpu_set();
  1249. #endif
  1250. /* Apply share memory HW remap setting and lock it */
  1251. if (load_md_flag & (1<<MD_SYS1)) {
  1252. md_smem_rw_remapping(MD_SYS1, (unsigned int)(smem_info.base_addr + smem_info.ap_md1_smem_offset));
  1253. /* remapping CCB to slot3,4 in bank4 */
  1254. md_bank4_remapping_by_slot(MD_SYS1, csmem_info.csmem_buffer_addr, 3);
  1255. md_bank4_remapping_by_slot(MD_SYS1, csmem_info.csmem_buffer_addr + 0x2000000, 4);
  1256. md_emi_remapping_lock(MD_SYS1);
  1257. }
  1258. return smem_final_size;
  1259. }
  1260. /*****************************************************************************/
  1261. /* Memory --- limit and align settings */
  1262. /*****************************************************************************/
  1263. /* Share memory cacheable for MD */
  1264. #define SMEM_CACHE_ALIGNMENT 0x2000000L
  1265. #define SMEM_CACHE_LIMIT 0x90000000LL
  1266. /* Share memory non-cacheable for MD */
  1267. #define SMEM_NONCACHE_ALIGNMENT 0x2000000L
  1268. #define SMEM_NONCACHE_LIMIT 0x90000000LL
  1269. /* RO + RW memory non-cacheable for MD */
  1270. #define RO_RW_MEM_ALIGNMENT 0x2000000L
  1271. #ifdef DUMMY_AP_MODE
  1272. #define RO_RW_MEM_LIMIT 0xA0000000LL
  1273. #else
  1274. #define RO_RW_MEM_LIMIT 0xC0000000LL
  1275. #endif
  1276. /*------------------------------------------------------------------------------------------------*/
  1277. /* platform configure setting info. */
  1278. /*------------------------------------------------------------------------------------------------*/
  1279. long long plat_ccci_get_ld_md_plat_setting(char cfg_name[])
  1280. {
  1281. if (strcmp(cfg_name, "share_memory_size") == 0) {
  1282. #ifdef DUMMY_AP_MODE
  1283. return 0x200000;
  1284. #endif
  1285. /* only for non-cacheable part. */
  1286. return (long long)cal_share_mem_size();
  1287. }
  1288. if (strcmp(cfg_name, "share_mem_limit") == 0)
  1289. return 0x90000000LL;
  1290. if (strcmp(cfg_name, "ro_rw_mem_limit") == 0) {
  1291. #ifdef DUMMY_AP_MODE
  1292. return 0x100000000LL;
  1293. #endif
  1294. return 0xC0000000LL;
  1295. }
  1296. if (strcmp(cfg_name, "ro_rw_mem_align") == 0)
  1297. return 0x2000000LL;
  1298. if (strcmp(cfg_name, "share_mem_align") == 0)
  1299. return 0x2000000LL;
  1300. if (strcmp(cfg_name, "ld_version") == 0) {
  1301. #ifdef DUMMY_AP_MODE
  1302. return 0x20001;
  1303. #endif
  1304. return 0x20000;/* xxxx_yyyy, xxxx: main id, yyyy sub id */
  1305. }
  1306. if (strcmp(cfg_name, "rat_plat_ver") == 0)
  1307. return RAT_VER_93;
  1308. return -1LL;
  1309. }
  1310. /* CCCI tag info memory */
  1311. #define TAG_MEM_ALIGNMENT 0x1000L
  1312. #define TAG_MEM_LIMIT 0xC0000000LL
  1313. #define PLAT_DEFAULT_CCB_GEAR 1
  1314. struct mem_attr_setting {
  1315. char *name;
  1316. unsigned long long limit;
  1317. unsigned long align;
  1318. };
  1319. const struct mem_attr_setting c_md_mem_attr_cfg[] = {
  1320. {"ap_md_c_smem", SMEM_CACHE_LIMIT, SMEM_CACHE_ALIGNMENT},
  1321. {"ap_md_nc_smem", SMEM_NONCACHE_LIMIT, SMEM_NONCACHE_ALIGNMENT},
  1322. {"md_mem_usage", RO_RW_MEM_LIMIT, RO_RW_MEM_ALIGNMENT},
  1323. {"ccci_tag_mem", TAG_MEM_LIMIT, TAG_MEM_ALIGNMENT},
  1324. {"md3rom", RO_RW_MEM_LIMIT, RO_RW_MEM_ALIGNMENT},
  1325. {NULL, 0LL, 0L},
  1326. };
  1327. int get_mem_limit_and_align(char key[], unsigned long long *limit, unsigned long *align)
  1328. {
  1329. int i = 0;
  1330. int mem_attr_num =
  1331. sizeof(c_md_mem_attr_cfg)/sizeof(struct mem_attr_setting);
  1332. while(1) {
  1333. if (i >= mem_attr_num) {
  1334. ALWAYS_LOG("invalid index i=%d\n", i);
  1335. break;
  1336. }
  1337. if (!c_md_mem_attr_cfg[i].name)
  1338. break;
  1339. if (strcmp(key, c_md_mem_attr_cfg[i].name) == 0) {
  1340. if (limit)
  1341. *limit = c_md_mem_attr_cfg[i].limit;
  1342. if (align)
  1343. *align = c_md_mem_attr_cfg[i].align;
  1344. return 0;
  1345. }
  1346. i++;
  1347. }
  1348. return -1;
  1349. }
  1350. #ifdef DUMMY_AP_MODE
  1351. #include <platform/mt_irq.h>
  1352. extern void dummy_ap_boot_up_md(int md_en_flag);
  1353. extern void load_modem_image(void);
  1354. extern int dummy_ap_irq_helper(unsigned int);
  1355. /* Remember add this function to file platform.c(platform code) */
  1356. void dummy_ap_entry(void)
  1357. {
  1358. load_modem_image();
  1359. }
  1360. /* Remember add this function to file interrupts.c(platform code) */
  1361. void dummy_ap_irq_handler(unsigned int irq)
  1362. {
  1363. if (dummy_ap_irq_helper(irq)) {
  1364. mt_irq_ack(irq);
  1365. mt_irq_unmask(irq);
  1366. }
  1367. }
  1368. void boot_to_dummy_ap_mode(int load_md_flag)
  1369. {
  1370. md_smem_rw_remapping(MD_SYS1, smem_info.base_addr);
  1371. /* Before boot dummy AP, clear share memory */
  1372. memset((void*)((unsigned long)smem_info.base_addr), 0, 0x200000);
  1373. dummy_ap_boot_up_md(load_md_flag);
  1374. }
  1375. #endif