hantro_mmu.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /****************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * Copyright (c) 2014 - 2021 VERISILICON
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. *****************************************************************************
  26. *
  27. * The GPL License (GPL)
  28. *
  29. * Copyright (C) 2014 - 2021 VERISILICON
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version 2
  34. * of the License, or (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software Foundation,
  43. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  44. *
  45. *****************************************************************************
  46. *
  47. * Note: This software is released under dual MIT and GPL licenses. A
  48. * recipient may use this file under the terms of either the MIT license or
  49. * GPL License. If you wish to use only one license not the other, you can
  50. * indicate your decision by deleting one of the above license notices in your
  51. * version of this file.
  52. *
  53. *****************************************************************************/
  54. #ifdef __FREERTOS__
  55. #include "base_type.h"
  56. #include "dev_common_freertos.h"
  57. #include "io_tools.h"
  58. #elif defined(__linux__)
  59. #include <linux/version.h>
  60. #include <linux/slab.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/module.h>
  63. #include <linux/init.h>
  64. #include <linux/kernel.h>
  65. #include <linux/debugfs.h>
  66. #include <linux/types.h>
  67. #include <linux/device.h>
  68. #include <linux/pagemap.h>
  69. #include <linux/sched.h>
  70. #include <stddef.h>
  71. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,10,0))
  72. #include <linux/dma-map-ops.h>
  73. #else
  74. #include <linux/dma-contiguous.h>
  75. #endif
  76. #include <linux/platform_device.h>
  77. #include <linux/mod_devicetable.h>
  78. #include <linux/dma-buf.h>
  79. #include <asm/io.h>
  80. #endif
  81. #include "hantrommu.h"
  82. MODULE_DESCRIPTION("Verisilicon VPU Driver");
  83. MODULE_LICENSE("GPL");
  84. #ifndef NULL
  85. #ifdef __cplusplus
  86. #define NULL 0
  87. #else
  88. #define NULL ((void *)0)
  89. #endif
  90. #endif
  91. /*******************************************************************************
  92. ***** New MMU Defination *******************************************************/
  93. #define MMU_MTLB_SHIFT 22
  94. #define MMU_STLB_4K_SHIFT 12
  95. #define MMU_STLB_64K_SHIFT 16
  96. #define MMU_MTLB_BITS (32 - MMU_MTLB_SHIFT)
  97. #define MMU_PAGE_4K_BITS MMU_STLB_4K_SHIFT
  98. #define MMU_STLB_4K_BITS (32 - MMU_MTLB_BITS - MMU_PAGE_4K_BITS)
  99. #define MMU_PAGE_64K_BITS MMU_STLB_64K_SHIFT
  100. #define MMU_STLB_64K_BITS (32 - MMU_MTLB_BITS - MMU_PAGE_64K_BITS)
  101. #define MMU_MTLB_ENTRY_NUM (1 << MMU_MTLB_BITS)
  102. #define MMU_MTLB_SIZE (MMU_MTLB_ENTRY_NUM << 2)
  103. #define MMU_STLB_4K_ENTRY_NUM (1 << MMU_STLB_4K_BITS)
  104. #define MMU_STLB_4K_SIZE (MMU_STLB_4K_ENTRY_NUM << 2)
  105. #define MMU_PAGE_4K_SIZE (1 << MMU_STLB_4K_SHIFT)
  106. #define MMU_STLB_64K_ENTRY_NUM (1 << MMU_STLB_64K_BITS)
  107. #define MMU_STLB_64K_SIZE (MMU_STLB_64K_ENTRY_NUM << 2)
  108. #define MMU_PAGE_64K_SIZE (1 << MMU_STLB_64K_SHIFT)
  109. #define MMU_MTLB_MASK (~((1U << MMU_MTLB_SHIFT)-1))
  110. #define MMU_STLB_4K_MASK ((~0U << MMU_STLB_4K_SHIFT) ^ MMU_MTLB_MASK)
  111. #define MMU_PAGE_4K_MASK (MMU_PAGE_4K_SIZE - 1)
  112. #define MMU_STLB_64K_MASK ((~((1U << MMU_STLB_64K_SHIFT)-1)) ^ MMU_MTLB_MASK)
  113. #define MMU_PAGE_64K_MASK (MMU_PAGE_64K_SIZE - 1)
  114. /* Page offset definitions. */
  115. #define MMU_OFFSET_4K_BITS (32 - MMU_MTLB_BITS - MMU_STLB_4K_BITS)
  116. #define MMU_OFFSET_4K_MASK ((1U << MMU_OFFSET_4K_BITS) - 1)
  117. #define MMU_OFFSET_16K_BITS (32 - MMU_MTLB_BITS - MMU_STLB_16K_BITS)
  118. #define MMU_OFFSET_16K_MASK ((1U << MMU_OFFSET_16K_BITS) - 1)
  119. #define MMU_MTLB_ENTRY_HINTS_BITS 6
  120. #define MMU_MTLB_ENTRY_STLB_MASK (~((1U << MMU_MTLB_ENTRY_HINTS_BITS) - 1))
  121. #define MMU_MTLB_PRESENT 0x00000001
  122. #define MMU_MTLB_EXCEPTION 0x00000002
  123. #define MMU_MTLB_4K_PAGE 0x00000000
  124. #define MMU_STLB_PRESENT 0x00000001
  125. #define MMU_STLB_EXCEPTION 0x00000002
  126. #define MMU_STLB_4K_PAGE 0x00000000
  127. #define MMU_FALSE 0
  128. #define MMU_TRUE 1
  129. #define MMU_ERR_OS_FAIL (0xffff)
  130. #define MMU_EFAULT MMU_ERR_OS_FAIL
  131. #define MMU_ENOTTY MMU_ERR_OS_FAIL
  132. #define MMU_INFINITE ((u32) ~0U)
  133. #define MAX_NOPAGED_SIZE 0x20000
  134. #define MMU_SUPPRESS_OOM_MESSAGE 1
  135. #if MMU_SUPPRESS_OOM_MESSAGE
  136. #define MMU_NOWARN __GFP_NOWARN
  137. #else
  138. #define MMU_NOWARN 0
  139. #endif
  140. #define MMU_IS_ERROR(status) (status < 0)
  141. #define MMU_NO_ERROR(status) (status >= 0)
  142. #define MMU_IS_SUCCESS(status) (status == MMU_STATUS_OK)
  143. #undef MMUDEBUG
  144. #ifdef HANTROMMU_DEBUG
  145. # ifdef __KERNEL__
  146. # define MMUDEBUG(fmt, args...) printk( KERN_INFO "hantrommu: " fmt, ## args)
  147. # else
  148. # define MMUDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
  149. # endif
  150. #else
  151. # define MMUDEBUG(fmt, args...)
  152. #endif
  153. #define MMU_ON_ERROR(func) \
  154. do { \
  155. status = func; \
  156. if (MMU_IS_ERROR(status)){ \
  157. goto onerror; \
  158. } \
  159. }while (MMU_FALSE)
  160. #define WritePageEntry(page_entry, entry_value) \
  161. *(unsigned int *)(page_entry) =(unsigned int)(entry_value)
  162. #define ReadPageEntry(page_entry) *(unsigned int *)(page_entry)
  163. #define DRIVER_NAME "hantroencdma"
  164. /* simple map mode: generate mmu address which is same as input bus address*/
  165. unsigned int simple_map = 0;
  166. /* this shift should be an integral multiple of mmu page size(4096).
  167. It can generate a mmu address shift in simple map mode*/
  168. unsigned int map_shift = 0;
  169. /* module_param(name, type, perm) */
  170. module_param(simple_map, uint, 0);
  171. module_param(map_shift, uint, 0);
  172. enum MMURegion {
  173. MMU_REGION_IN,
  174. MMU_REGION_OUT,
  175. MMU_REGION_PRIVATE,
  176. MMU_REGION_PUB,
  177. MMU_REGION_COUNT
  178. };
  179. struct MMUNode {
  180. void *buf_virtual_address;
  181. unsigned int buf_bus_address; /* used in kernel map mode */
  182. int mtlb_start;
  183. int stlb_start;
  184. int mtlb_end;
  185. int stlb_end;
  186. unsigned int page_count;
  187. int process_id;
  188. struct file* filp;
  189. struct MMUNode *next;
  190. struct MMUNode *prev;
  191. };
  192. struct MMUDDRRegion {
  193. unsigned long long physical_address;
  194. unsigned long long virtual_address;
  195. unsigned int page_count;
  196. void *node_mutex;
  197. struct MMUNode *simple_map_head;
  198. struct MMUNode *simple_map_tail;
  199. struct MMUNode *free_map_head;
  200. struct MMUNode *map_head;
  201. struct MMUNode *free_map_tail;
  202. struct MMUNode *map_tail;
  203. };
  204. struct MMU {
  205. void *page_table_mutex;
  206. /* Master TLB information. */
  207. unsigned int mtlb_size;
  208. unsigned long long mtlb_physical;
  209. void *mtlb_virtual;
  210. unsigned int mtlb_entries;
  211. int enabled;
  212. unsigned int stlb_size;
  213. unsigned long long stlb_physical;
  214. void *stlb_virtual;
  215. struct MMUDDRRegion region[MMU_REGION_COUNT];
  216. unsigned int page_table_array_size;
  217. unsigned long long page_table_array_physical;
  218. void *page_table_array;
  219. };
  220. static struct MMU *g_mmu = NULL;
  221. extern unsigned long gBaseDDRHw;
  222. unsigned int mmu_enable = MMU_FALSE;
  223. static unsigned int mmu_init = MMU_FALSE;
  224. extern unsigned int pcie;
  225. static unsigned int region_in_mmu_start = REGION_IN_MMU_START;
  226. static unsigned int region_in_mmu_end = REGION_IN_MMU_END;
  227. static unsigned int region_out_mmu_start = REGION_OUT_MMU_START;
  228. static unsigned int region_out_mmu_end = REGION_OUT_MMU_END;
  229. static unsigned int region_private_mmu_start = REGION_PRIVATE_MMU_START;
  230. static unsigned int region_private_mmu_end = REGION_PRIVATE_MMU_END;
  231. static const struct platform_device_info hantro_platform_info = {
  232. .name = DRIVER_NAME,
  233. .id = -1,
  234. .dma_mask = DMA_BIT_MASK(32),
  235. };
  236. static int hantro_drm_probe(struct platform_device *pdev)
  237. {
  238. int result;
  239. struct device *dev = &pdev->dev;
  240. (void) dev;
  241. (void) result;
  242. return 0;
  243. }
  244. static int hantro_drm_remove(struct platform_device *pdev)
  245. {
  246. struct device *dev = &pdev->dev;
  247. (void) dev;
  248. return 0;
  249. }
  250. static const struct platform_device_id hantro_drm_platform_ids[] = {
  251. {
  252. .name = DRIVER_NAME,
  253. },
  254. {/* sentinel */ },
  255. };
  256. static const struct of_device_id hantro_of_match[] = {
  257. { .compatible = "thead,light-vc8000e-mmu", },
  258. {/* sentinel */}
  259. };
  260. static struct platform_driver hantro_drm_platform_driver = {
  261. .probe = hantro_drm_probe,
  262. .remove = hantro_drm_remove,
  263. .driver = {
  264. .name = DRIVER_NAME,
  265. .owner = THIS_MODULE,
  266. .of_match_table = hantro_of_match,
  267. },
  268. .id_table = hantro_drm_platform_ids,
  269. };
  270. struct platform_device *platformdev;
  271. static enum MMUStatus ZeroMemory(void *memory, unsigned int bytes) {
  272. memset(memory, 0, bytes);
  273. return MMU_STATUS_OK;
  274. }
  275. static enum MMUStatus AllocateMemory(unsigned int bytes, void **memory){
  276. void *pointer;
  277. enum MMUStatus status;
  278. if (bytes > MAX_NOPAGED_SIZE) {
  279. pointer = (void*) vmalloc(bytes);
  280. MMUDEBUG(" *****VMALLOC size*****%d\n", bytes);
  281. } else {
  282. pointer = (void*) kmalloc(bytes, GFP_KERNEL | MMU_NOWARN);
  283. MMUDEBUG(" *****KMALLOC size*****%d\n", bytes);
  284. }
  285. if (pointer == NULL) {
  286. /* Out of memory. */
  287. status = MMU_STATUS_OUT_OF_MEMORY;
  288. goto onerror;
  289. }
  290. /* Return pointer to the memory allocation. */
  291. *memory = pointer;
  292. return MMU_STATUS_OK;
  293. onerror:
  294. /* Return the status. */
  295. return status;
  296. }
  297. static enum MMUStatus FreeMemory(void *memory) {
  298. /* Free the memory from the OS pool. */
  299. if (is_vmalloc_addr(memory)) {
  300. MMUDEBUG(" *****VFREE*****%p\n", memory);
  301. vfree(memory);
  302. } else {
  303. MMUDEBUG(" *****KFREE*****%p\n", memory);
  304. kfree(memory);
  305. }
  306. return MMU_STATUS_OK;
  307. }
  308. static enum MMUStatus SMDeleteNode(struct MMUNode **pp) {
  309. (*pp)->prev->next = (*pp)->next;
  310. (*pp)->next->prev = (*pp)->prev;
  311. MMUDEBUG(" *****DeleteNode size*****%d\n", (*pp)->page_count);
  312. FreeMemory(*pp);
  313. return MMU_STATUS_OK;
  314. }
  315. static enum MMUStatus DeleteNode(struct MMUNode **pp) {
  316. (*pp)->prev->next = (*pp)->next;
  317. (*pp)->next->prev = (*pp)->prev;
  318. MMUDEBUG(" *****DeleteNode size*****%d\n", (*pp)->page_count);
  319. FreeMemory(*pp);
  320. return MMU_STATUS_OK;
  321. }
  322. static enum MMUStatus MergeNode(struct MMUNode *h,
  323. struct MMUNode **pp) {
  324. struct MMUNode *tmp0 = h->next;
  325. struct MMUNode *tmp1 = h->next;
  326. while(tmp0) {
  327. /* 1th step: find front contiguous memory node */
  328. if(tmp0->mtlb_end == (*pp)->mtlb_start &&
  329. tmp0->stlb_end == (*pp)->stlb_start) {
  330. tmp0->mtlb_end = (*pp)->mtlb_end;
  331. tmp0->stlb_end = (*pp)->stlb_end;
  332. tmp0->page_count += (*pp)->page_count;
  333. DeleteNode(pp);
  334. MMUDEBUG(" *****first merge to front. node size*****%d\n", tmp0->page_count);
  335. /* after merge to front contiguous memory node,
  336. find if there is behind contiguous memory node */
  337. while(tmp1) {
  338. /* merge */
  339. if(tmp1->mtlb_start == tmp0->mtlb_end &&
  340. tmp1->stlb_start == tmp0->stlb_end) {
  341. tmp1->mtlb_start = tmp0->mtlb_start;
  342. tmp1->stlb_start = tmp0->stlb_start;
  343. tmp1->page_count += tmp0->page_count;
  344. MMUDEBUG(" *****second merge to behind. node size*****%d\n", tmp1->page_count);
  345. DeleteNode(&tmp0);
  346. return MMU_STATUS_OK;
  347. }
  348. tmp1 = tmp1->next;
  349. }
  350. return MMU_STATUS_OK;
  351. /* 1th step: find behind contiguous memory node */
  352. } else if(tmp0->mtlb_start == (*pp)->mtlb_end &&
  353. tmp0->stlb_start == (*pp)->stlb_end) {
  354. tmp0->mtlb_start = (*pp)->mtlb_start;
  355. tmp0->stlb_start = (*pp)->stlb_start;
  356. tmp0->page_count += (*pp)->page_count;
  357. DeleteNode(pp);
  358. MMUDEBUG(" *****first merge to behind. node size*****%d\n", tmp0->page_count);
  359. /* after merge to behind contiguous memory node,
  360. find if there is front contiguous memory node */
  361. while(tmp1) {
  362. /* merge */
  363. if(tmp1->mtlb_end == tmp0->mtlb_start &&
  364. tmp1->stlb_end == tmp0->stlb_start) {
  365. tmp1->mtlb_end = tmp0->mtlb_end;
  366. tmp1->stlb_end = tmp0->stlb_end;
  367. tmp1->page_count += tmp0->page_count;
  368. MMUDEBUG(" *****second merge to front. node size*****%d\n", tmp1->page_count);
  369. DeleteNode(&tmp0);
  370. return MMU_STATUS_OK;
  371. }
  372. tmp1 = tmp1->next;
  373. }
  374. return MMU_STATUS_OK;
  375. }
  376. tmp0 = tmp0->next;
  377. }
  378. return MMU_STATUS_FALSE;
  379. }
  380. /* Insert a node to map list */
  381. static enum MMUStatus SMInsertNode(enum MMURegion e,
  382. struct MMUNode **pp) {
  383. struct MMUNode *h;
  384. h = g_mmu->region[e].simple_map_head;
  385. h->next->prev = *pp;
  386. (*pp)->next = h->next;
  387. (*pp)->prev = h;
  388. h->next = *pp;
  389. MMUDEBUG(" *****insert bm node*****%d\n", (*pp)->page_count);
  390. return MMU_STATUS_OK;
  391. }
  392. static enum MMUStatus InsertNode(enum MMURegion e,
  393. struct MMUNode **pp,
  394. unsigned int free) {
  395. enum MMUStatus status;
  396. struct MMUNode *h, *b;
  397. if(free) {
  398. h = g_mmu->region[e].free_map_head;
  399. b = g_mmu->region[e].map_head;
  400. status = MergeNode(h, pp);
  401. MMUDEBUG(" *****insert free*****%d\n", (*pp)->page_count);
  402. if(MMU_IS_ERROR(status)) {
  403. /* remove from map*/
  404. if((*pp)->prev != NULL && (*pp)->next != NULL) {
  405. (*pp)->prev->next = (*pp)->next;
  406. (*pp)->next->prev = (*pp)->prev;
  407. }
  408. /* insert to free map */
  409. h->next->prev = *pp;
  410. (*pp)->next = h->next;
  411. (*pp)->prev = h;
  412. h->next = *pp;
  413. }
  414. } else {
  415. h = g_mmu->region[e].map_head;
  416. h->next->prev = *pp;
  417. (*pp)->next = h->next;
  418. (*pp)->prev = h;
  419. h->next = *pp;
  420. MMUDEBUG(" *****insert unfree*****%d\n", (*pp)->page_count);
  421. }
  422. return MMU_STATUS_OK;
  423. }
  424. /* Create a Node */
  425. static enum MMUStatus SMCreateNode(enum MMURegion e,
  426. struct MMUNode **node,
  427. unsigned int page_count) {
  428. struct MMUNode *p, **new;
  429. p = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  430. new = &p;
  431. (*new)->mtlb_start = -1;
  432. (*new)->stlb_start = -1;
  433. (*new)->mtlb_end = -1;
  434. (*new)->stlb_end = -1;
  435. (*new)->process_id = 0;
  436. (*new)->filp = NULL;
  437. (*new)->page_count = 0;
  438. (*new)->prev = NULL;
  439. (*new)->next = NULL;
  440. /* Insert a uncomplete Node, it will be initialized later */
  441. SMInsertNode(e, new);
  442. /* return a new node for map buffer */
  443. *node = *new;
  444. return MMU_STATUS_OK;
  445. }
  446. /* Create initial Nodes */
  447. static enum MMUStatus SMCreateNodes(void) {
  448. struct MMUNode *simple_map_head;
  449. struct MMUNode *simple_map_tail;
  450. int i;
  451. /* Init each region map node */
  452. for (i = 0; i < MMU_REGION_COUNT ; i++) {
  453. simple_map_head = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  454. simple_map_tail = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  455. simple_map_head->mtlb_start = -1;
  456. simple_map_head->stlb_start = -1;
  457. simple_map_head->mtlb_end = -1;
  458. simple_map_head->stlb_end = -1;
  459. simple_map_head->process_id = 0;
  460. simple_map_head->filp = NULL;
  461. simple_map_head->page_count = 0;
  462. simple_map_head->prev = NULL;
  463. simple_map_head->next = simple_map_tail;
  464. simple_map_tail->mtlb_start = -1;
  465. simple_map_tail->stlb_start = -1;
  466. simple_map_tail->mtlb_end = -1;
  467. simple_map_tail->stlb_end = -1;
  468. simple_map_tail->process_id = 0;
  469. simple_map_tail->filp = NULL;
  470. simple_map_tail->page_count = 0;
  471. simple_map_tail->prev = simple_map_head;
  472. simple_map_tail->next = NULL;
  473. g_mmu->region[i].simple_map_head = simple_map_head;
  474. g_mmu->region[i].simple_map_tail = simple_map_tail;
  475. }
  476. return MMU_STATUS_OK;
  477. }
  478. static enum MMUStatus CreateNode(void) {
  479. struct MMUNode *free_map_head, *map_head, *p, **pp;
  480. struct MMUNode *free_map_tail, *map_tail;
  481. int i;
  482. unsigned int page_count;
  483. unsigned int prev_stlb = 0, prev_mtlb = 0;
  484. /* Init each region map node */
  485. for (i = 0; i < MMU_REGION_COUNT ; i++) {
  486. free_map_head = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  487. map_head = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  488. free_map_tail = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  489. map_tail = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  490. free_map_head->mtlb_start = map_head->mtlb_start = -1;
  491. free_map_head->stlb_start = map_head->stlb_start = -1;
  492. free_map_head->mtlb_end = map_head->mtlb_end = -1;
  493. free_map_head->stlb_end = map_head->stlb_end = -1;
  494. free_map_head->process_id = map_head->process_id = 0;
  495. free_map_head->filp = map_head->filp = NULL;
  496. free_map_head->page_count = map_head->page_count = 0;
  497. free_map_head->prev = map_head->prev = NULL;
  498. free_map_head->next = free_map_tail;
  499. map_head->next = map_tail;
  500. free_map_tail->mtlb_start = map_tail->mtlb_start = -1;
  501. free_map_tail->stlb_start = map_tail->stlb_start = -1;
  502. free_map_tail->mtlb_end = map_tail->mtlb_end = -1;
  503. free_map_tail->stlb_end = map_tail->stlb_end = -1;
  504. free_map_tail->process_id = map_tail->process_id = 0;
  505. free_map_tail->filp = map_tail->filp = NULL;
  506. free_map_tail->page_count = map_tail->page_count = 0;
  507. free_map_tail->prev = free_map_head;
  508. map_tail->prev = map_head;
  509. free_map_tail->next = map_tail->next = NULL;
  510. g_mmu->region[i].free_map_head = free_map_head;
  511. g_mmu->region[i].map_head = map_head;
  512. g_mmu->region[i].free_map_tail = free_map_tail;
  513. g_mmu->region[i].map_tail = map_tail;
  514. p = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  515. pp = &p;
  516. switch(i) {
  517. case MMU_REGION_IN:
  518. page_count = (REGION_IN_END - REGION_IN_START + 1)/PAGE_SIZE;
  519. p->stlb_start = region_in_mmu_start >> 12 & 0x3FF; //hold mmu addr: 0x0
  520. p->mtlb_start = region_in_mmu_start >> 22;
  521. //end point next region start: +1; for remainder: +1
  522. p->stlb_end = prev_stlb = region_in_mmu_end >> 12 & 0x3FF;
  523. p->mtlb_end = prev_mtlb = region_in_mmu_end >> 22;
  524. p->page_count = page_count - 1; //hold mmu addr: 0x0
  525. break;
  526. case MMU_REGION_OUT:
  527. page_count = (REGION_OUT_END - REGION_OUT_START + 1)/PAGE_SIZE;
  528. p->stlb_start = region_out_mmu_start >> 12 & 0x3FF;
  529. p->mtlb_start = region_out_mmu_start >> 22;
  530. p->stlb_end = prev_stlb = region_out_mmu_end >> 12 & 0x3FF;
  531. p->mtlb_end = prev_mtlb = region_out_mmu_end >> 22;
  532. p->page_count = page_count;
  533. break;
  534. case MMU_REGION_PRIVATE:
  535. page_count = (REGION_PRIVATE_END - REGION_PRIVATE_START + 1)/PAGE_SIZE;
  536. p->stlb_start = region_private_mmu_start >> 12 & 0x3FF;
  537. p->mtlb_start = region_private_mmu_start >> 22;
  538. p->stlb_end = prev_stlb = region_private_mmu_end >> 12 & 0x3FF;
  539. p->mtlb_end = prev_mtlb = region_private_mmu_end >> 22;
  540. p->page_count = page_count;
  541. break;
  542. case MMU_REGION_PUB:
  543. p->stlb_start = prev_stlb;
  544. p->mtlb_start = prev_mtlb;
  545. p->stlb_end = prev_stlb = MMU_STLB_4K_ENTRY_NUM - 1;
  546. p->mtlb_end = prev_mtlb = MMU_MTLB_ENTRY_NUM - 1;
  547. p->page_count = (p->mtlb_end - p->mtlb_start) * MMU_STLB_4K_ENTRY_NUM +
  548. p->stlb_end - p->stlb_start + 1;
  549. break;
  550. default:
  551. pr_notice(" *****MMU Region Error*****\n");
  552. break;
  553. }
  554. p->process_id = 0;
  555. p->filp = NULL;
  556. p->next = p->prev = NULL;
  557. InsertNode(i, pp, 1);
  558. }
  559. return MMU_STATUS_OK;
  560. }
  561. /* A simpile function to check if the map buffer is existed.
  562. it needs more complex version*/
  563. static enum MMUStatus SMCheckAddress(enum MMURegion e,
  564. void *virtual_address) {
  565. struct MMUNode *p;
  566. p = g_mmu->region[e].simple_map_head->next;
  567. while(p) {
  568. if(p->buf_virtual_address == virtual_address) {
  569. return MMU_STATUS_FALSE;
  570. }
  571. p = p->next;
  572. }
  573. return MMU_STATUS_OK;
  574. }
  575. static enum MMUStatus FindFreeNode(enum MMURegion e,
  576. struct MMUNode **node,
  577. unsigned int page_count) {
  578. struct MMUNode *p;
  579. p = g_mmu->region[e].free_map_head->next;
  580. while(p) {
  581. if(p->page_count >= page_count) {
  582. *node = p;
  583. return MMU_STATUS_OK;
  584. }
  585. p = p->next;
  586. }
  587. return MMU_STATUS_FALSE;
  588. }
  589. static enum MMUStatus SplitFreeNode(enum MMURegion e,
  590. struct MMUNode **node,
  591. unsigned int page_count) {
  592. struct MMUNode *p, **new;
  593. p = kmalloc(sizeof(struct MMUNode), GFP_KERNEL | MMU_NOWARN);
  594. new = &p;
  595. **new = **node;
  596. (*new)->mtlb_start = (*node)->mtlb_start;
  597. (*new)->stlb_start = (*node)->stlb_start;
  598. (*new)->mtlb_end = (page_count + (*node)->stlb_start) /
  599. MMU_STLB_4K_ENTRY_NUM +
  600. (*node)->mtlb_start;
  601. (*new)->stlb_end = (page_count + (*node)->stlb_start) %
  602. MMU_STLB_4K_ENTRY_NUM;
  603. (*new)->process_id = (*node)->process_id;
  604. (*new)->page_count = page_count;
  605. MMUDEBUG(" *****new mtlb_start*****%d\n", (*new)->mtlb_start);
  606. MMUDEBUG(" *****new stlb_start*****%d\n", (*new)->stlb_start);
  607. MMUDEBUG(" *****new mtlb_end*****%d\n", (*new)->mtlb_end);
  608. MMUDEBUG(" *****new stlb_end*****%d\n", (*new)->stlb_end);
  609. /* Insert a new node in map */
  610. InsertNode(e, new, 0);
  611. /* Update free node in free map*/
  612. (*node)->page_count -= page_count;
  613. if((*node)->page_count == 0) {
  614. DeleteNode(node);
  615. MMUDEBUG(" *****old node deleted*****\n");
  616. } else {
  617. (*node)->mtlb_start = (*new)->mtlb_end;
  618. (*node)->stlb_start = (*new)->stlb_end;
  619. MMUDEBUG(" *****old mtlb_start*****%d\n", (*node)->mtlb_start);
  620. MMUDEBUG(" *****old stlb_start*****%d\n", (*node)->stlb_start);
  621. MMUDEBUG(" *****old mtlb_end*****%d\n", (*node)->mtlb_end);
  622. MMUDEBUG(" *****old stlb_end*****%d\n", (*node)->stlb_end);
  623. }
  624. /* return a new node for map buffer */
  625. *node = *new;
  626. return MMU_STATUS_OK;
  627. }
  628. static enum MMUStatus SMRemoveNode(enum MMURegion e,
  629. void *buf_virtual_address,
  630. unsigned int process_id) {
  631. struct MMUNode *p, **pp;
  632. p = g_mmu->region[e].simple_map_head->next;
  633. pp = &p;
  634. while(*pp) {
  635. if((*pp)->buf_virtual_address == buf_virtual_address &&
  636. (*pp)->process_id == process_id) {
  637. SMDeleteNode(pp);
  638. break;
  639. }
  640. *pp = (*pp)->next;
  641. }
  642. return MMU_STATUS_OK;
  643. }
  644. static enum MMUStatus RemoveNode(enum MMURegion e,
  645. void *buf_virtual_address,
  646. unsigned int process_id) {
  647. struct MMUNode *p, **pp;
  648. p = g_mmu->region[e].map_head->next;
  649. pp = &p;
  650. while(*pp) {
  651. if((*pp)->buf_virtual_address == buf_virtual_address &&
  652. (*pp)->process_id == process_id) {
  653. InsertNode(e, pp, 1);
  654. break;
  655. }
  656. *pp = (*pp)->next;
  657. }
  658. return MMU_STATUS_OK;
  659. }
  660. static enum MMUStatus SMRemoveKernelNode(enum MMURegion e,
  661. unsigned int buf_bus_address,
  662. unsigned int process_id) {
  663. struct MMUNode *p, **pp;
  664. p = g_mmu->region[e].simple_map_head->next;
  665. pp = &p;
  666. while(*pp) {
  667. if((*pp)->buf_bus_address == buf_bus_address &&
  668. (*pp)->process_id == process_id) {
  669. SMDeleteNode(pp);
  670. break;
  671. }
  672. *pp = (*pp)->next;
  673. }
  674. return MMU_STATUS_OK;
  675. }
  676. static enum MMUStatus RemoveKernelNode(enum MMURegion e,
  677. unsigned int buf_bus_address,
  678. unsigned int process_id) {
  679. struct MMUNode *p, **pp;
  680. p = g_mmu->region[e].map_head->next;
  681. pp = &p;
  682. while(*pp) {
  683. if((*pp)->buf_bus_address == buf_bus_address &&
  684. (*pp)->process_id == process_id) {
  685. InsertNode(e, pp, 1);
  686. break;
  687. }
  688. *pp = (*pp)->next;
  689. }
  690. return MMU_STATUS_OK;
  691. }
  692. static enum MMUStatus Delay(unsigned int delay) {
  693. if(delay > 0) {
  694. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
  695. ktime_t dl = ktime_set((delay / MSEC_PER_SEC),
  696. (delay % MSEC_PER_SEC) * NSEC_PER_MSEC);
  697. __set_current_state(TASK_UNINTERRUPTIBLE);
  698. schedule_hrtimeout(&dl, HRTIMER_MODE_REL);
  699. #else
  700. msleep(delay);
  701. #endif
  702. }
  703. return MMU_STATUS_OK;
  704. }
  705. static enum MMUStatus CreateMutex(void **mtx) {
  706. enum MMUStatus status;
  707. /* Allocate the mutex structure. */
  708. status = AllocateMemory(sizeof(struct mutex), mtx);
  709. if (MMU_IS_SUCCESS(status)) {
  710. /* Initialize the mutex. */
  711. mutex_init(*(struct mutex **)mtx);
  712. }
  713. return status;
  714. }
  715. static enum MMUStatus DeleteMutex(void *mtx) {
  716. /* Destroy the mutex. */
  717. mutex_destroy((struct mutex *)mtx);
  718. /* Free the mutex structure. */
  719. FreeMemory(mtx);
  720. return MMU_STATUS_OK;
  721. }
  722. static enum MMUStatus AcquireMutex(void *mtx, unsigned int timeout) {
  723. if (timeout == MMU_INFINITE)
  724. {
  725. /* Lock the mutex. */
  726. mutex_lock(mtx);
  727. /* Success. */
  728. return MMU_STATUS_OK;
  729. }
  730. for (;;) {
  731. /* Try to acquire the mutex. */
  732. if (mutex_trylock(mtx)) {
  733. /* Success. */
  734. return MMU_STATUS_OK;
  735. }
  736. if (timeout-- == 0) {
  737. break;
  738. }
  739. /* Wait for 1 millisecond. */
  740. Delay(1);
  741. }
  742. return MMU_STATUS_OK;
  743. }
  744. static enum MMUStatus ReleaseMutex(void *mtx) {
  745. /* Release the mutex. */
  746. mutex_unlock(mtx);
  747. return MMU_STATUS_OK;
  748. }
  749. static inline enum MMUStatus QueryProcessPageTable(void *logical,
  750. unsigned long long *address) {
  751. unsigned long lg = (unsigned long)logical;
  752. unsigned long offset = lg & ~PAGE_MASK;
  753. struct vm_area_struct *vma;
  754. spinlock_t *ptl;
  755. pgd_t *pgd;
  756. pud_t *pud;
  757. pmd_t *pmd;
  758. pte_t *pte;
  759. if (is_vmalloc_addr(logical)) {
  760. /* vmalloc area. */
  761. *address = page_to_phys(vmalloc_to_page(logical)) | offset;
  762. return MMU_STATUS_OK;
  763. } else if (virt_addr_valid(lg)) {
  764. /* Kernel logical address. */
  765. *address = virt_to_phys(logical);
  766. return MMU_STATUS_OK;
  767. } else {
  768. /* Try user VM area. */
  769. if (!current->mm)
  770. return MMU_STATUS_NOT_FOUND;
  771. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
  772. down_read(&current->mm->mmap_lock);
  773. #else
  774. down_read(&current->mm->mmap_sem);
  775. #endif
  776. vma = find_vma(current->mm, lg);
  777. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
  778. up_read(&current->mm->mmap_lock);
  779. #else
  780. up_read(&current->mm->mmap_sem);
  781. #endif
  782. /* To check if mapped to user. */
  783. if (!vma)
  784. return MMU_STATUS_NOT_FOUND;
  785. pgd = pgd_offset(current->mm, lg);
  786. if (pgd_none(*pgd) || pgd_bad(*pgd))
  787. return MMU_STATUS_NOT_FOUND;
  788. #if (defined(CONFIG_CPU_CSKYV2) || defined(CONFIG_X86)) \
  789. && LINUX_VERSION_CODE >= KERNEL_VERSION (4,12,0)
  790. pud = pud_offset((p4d_t*)pgd, lg);
  791. #elif (defined(CONFIG_CPU_CSKYV2)) \
  792. && LINUX_VERSION_CODE >= KERNEL_VERSION (4,11,0)
  793. pud = pud_offset((p4d_t*)pgd, lg);
  794. #else
  795. pud = pud_offset((p4d_t*)pgd, lg);
  796. #endif
  797. if (pud_none(*pud) || pud_bad(*pud))
  798. return MMU_STATUS_NOT_FOUND;
  799. pmd = pmd_offset(pud, lg);
  800. if (pmd_none(*pmd) || pmd_bad(*pmd))
  801. return MMU_STATUS_NOT_FOUND;
  802. pte = pte_offset_map_lock(current->mm, pmd, lg, &ptl);
  803. if (!pte) {
  804. spin_unlock(ptl);
  805. return MMU_STATUS_NOT_FOUND;
  806. }
  807. if (!pte_present(*pte)) {
  808. pte_unmap_unlock(pte, ptl);
  809. return MMU_STATUS_NOT_FOUND;
  810. }
  811. *address = (pte_pfn(*pte) << PAGE_SHIFT) | offset;
  812. pte_unmap_unlock(pte, ptl);
  813. *address -= gBaseDDRHw;
  814. //MMUDEBUG(" QueryProcessPageTable map: virt %p -> %p\n", logical, (void *)*address);
  815. return MMU_STATUS_OK;
  816. }
  817. }
  818. static inline int GetProcessID(void) {
  819. return current->tgid;
  820. }
  821. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
  822. static inline int is_vmalloc_addr(void *addr) {
  823. unsigned long long addr = (unsigned long long)Addr;
  824. return addr >= VMALLOC_START && addr < VMALLOC_END;
  825. }
  826. #endif
  827. static enum MMUStatus GetPhysicalAddress(void *logical,
  828. unsigned long long *address) {
  829. enum MMUStatus status;
  830. status = QueryProcessPageTable(logical, address);
  831. return status;
  832. }
  833. static enum MMUStatus GetPageEntry(struct MMUNode *node,
  834. unsigned int **page_table_entry,
  835. unsigned int i) {
  836. int num = node->mtlb_start * MMU_STLB_4K_ENTRY_NUM +
  837. node->stlb_start + i;
  838. *page_table_entry = (unsigned int*)g_mmu->stlb_virtual + num;
  839. return MMU_STATUS_OK;
  840. }
  841. static enum MMUStatus SetupDynamicSpace(void) {
  842. int i;
  843. enum MMUStatus status;
  844. unsigned int stlb_entry;
  845. void *pointer;
  846. unsigned long long address;
  847. dma_addr_t dma_handle;
  848. unsigned int num_entries = MMU_MTLB_ENTRY_NUM;
  849. unsigned int *mtlb_virtual = (unsigned int *)g_mmu->mtlb_virtual;
  850. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  851. if(pcie) {
  852. pointer = ioremap(gBaseDDRHw+STLB_PCIE_START_ADDRESS, num_entries*MMU_STLB_4K_SIZE);
  853. g_mmu->stlb_virtual = pointer;
  854. MMUDEBUG(" *****stlb_virtual = %p**%d\n", pointer, num_entries*MMU_STLB_4K_SIZE);
  855. address = STLB_PCIE_START_ADDRESS;
  856. for(i = 0; i < num_entries; i++){
  857. stlb_entry = address
  858. /* 4KB page size */
  859. | (0 << 2)
  860. /* Ignore exception */
  861. | (0 << 1)
  862. /* Present */
  863. | (1 << 0);
  864. WritePageEntry(mtlb_virtual++, stlb_entry);
  865. address += MMU_STLB_4K_SIZE;
  866. }
  867. } else {
  868. g_mmu->stlb_virtual = (void *)((u64)(g_mmu->mtlb_virtual) + MMU_MTLB_SIZE);
  869. g_mmu->stlb_physical = address = g_mmu->mtlb_physical + MMU_MTLB_SIZE;
  870. g_mmu->stlb_size = num_entries * MMU_STLB_4K_SIZE;
  871. for(i = 0; i < num_entries; i++){
  872. stlb_entry = address
  873. /* 4KB page size */
  874. | (0 << 2)
  875. /* Ignore exception */
  876. | (0 << 1)
  877. /* Present */
  878. | (1 << 0);
  879. WritePageEntry(mtlb_virtual++, stlb_entry);
  880. address += MMU_STLB_4K_SIZE;
  881. }
  882. }
  883. ReleaseMutex(g_mmu->page_table_mutex);
  884. /* Initial map info. */
  885. if (simple_map)
  886. SMCreateNodes();
  887. else
  888. CreateNode();
  889. return MMU_STATUS_OK;
  890. onerror:
  891. /* Return status. */
  892. return status;
  893. }
  894. enum MMUStatus MMUInit(volatile unsigned char *hwregs) {
  895. enum MMUStatus status;
  896. unsigned i;
  897. int result;
  898. void *pointer;
  899. if (mmu_init == MMU_TRUE) {
  900. /* All mmu use common table and dev, just initial once*/
  901. pr_notice(" *****MMU Already Initialed*****\n");
  902. return MMU_STATUS_OK;
  903. }
  904. if(!hwregs || (ioread32((void*)(hwregs + MMU_REG_HW_ID))>>16) != 0x4D4D)
  905. return MMU_STATUS_NOT_FOUND;
  906. pr_notice(" *****MMU Init*****\n");
  907. platformdev = platform_device_register_full(&hantro_platform_info);
  908. if(platformdev == NULL) {
  909. pr_err("hantrodec create platform device fail\n");
  910. status = MMU_STATUS_FALSE;
  911. goto onerror;
  912. } else {
  913. pr_info("Create platform device success\n");
  914. }
  915. result = platform_driver_register(&hantro_drm_platform_driver);
  916. pr_notice("Platform driver status is %d\n", result);
  917. /* Allocate memory for the MMU object. */
  918. MMU_ON_ERROR(AllocateMemory(sizeof(struct MMU), &pointer));
  919. ZeroMemory(pointer, sizeof(struct MMU));
  920. g_mmu = pointer;
  921. g_mmu->page_table_mutex = NULL;
  922. /* Create the page table mutex. */
  923. MMU_ON_ERROR(CreateMutex(&g_mmu->page_table_mutex));
  924. for (i = 0; i < MMU_REGION_COUNT;i++) {
  925. MMU_ON_ERROR(CreateMutex(&g_mmu->region[i].node_mutex));
  926. }
  927. mmu_init = MMU_TRUE;
  928. return MMU_STATUS_OK;
  929. onerror:
  930. pr_notice(" *****MMU Init Error*****\n");
  931. return status;
  932. }
  933. enum MMUStatus MMURelease(void *filp, volatile unsigned char *hwregs) {
  934. int i, j;
  935. struct MMUNode *p, *tmp;
  936. unsigned long long address;
  937. unsigned int *page_table_entry;
  938. if(!hwregs || (ioread32((void*)(hwregs + MMU_REG_HW_ID))>>16) != 0x4D4D)
  939. return MMU_STATUS_FALSE;
  940. /* if mmu or TLB not enabled, return */
  941. if (simple_map) {
  942. if(g_mmu == NULL || g_mmu->region[0].simple_map_head == NULL)
  943. return MMU_STATUS_OK;
  944. } else {
  945. if(g_mmu == NULL || g_mmu->region[0].map_head == NULL)
  946. return MMU_STATUS_OK;
  947. }
  948. pr_notice(" *****MMU Release*****\n");
  949. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  950. if (simple_map) {
  951. for (i = 0; i < MMU_REGION_COUNT; i++) {
  952. p = g_mmu->region[i].simple_map_head->next;
  953. while(p) {
  954. tmp = p->next;
  955. if(p->filp == (struct file *)filp) {
  956. for(j = 0;j < p->page_count; j++) {
  957. GetPageEntry(p, &page_table_entry, j);
  958. address = 0;
  959. WritePageEntry(page_table_entry, address);
  960. }
  961. SMRemoveNode(i, p->buf_virtual_address, p->process_id);
  962. }
  963. p = tmp;
  964. }
  965. }
  966. } else {
  967. for (i = 0; i < MMU_REGION_COUNT; i++) {
  968. p = g_mmu->region[i].map_head->next;
  969. while(p) {
  970. tmp = p->next;
  971. if(p->filp == (struct file *)filp) {
  972. for(j = 0;j < p->page_count; j++) {
  973. GetPageEntry(p, &page_table_entry, j);
  974. address = 0;
  975. WritePageEntry(page_table_entry, address);
  976. }
  977. RemoveNode(i, p->buf_virtual_address, p->process_id);
  978. }
  979. p = tmp;
  980. }
  981. }
  982. }
  983. ReleaseMutex(g_mmu->page_table_mutex);
  984. return MMU_STATUS_OK;
  985. }
  986. enum MMUStatus MMUCleanup(volatile unsigned char *hwregs[MAX_SUBSYS_NUM][2]) {
  987. int i;
  988. struct MMUNode *p, *tmp;
  989. struct MMUNode *fp;
  990. for (i = 0; i < MAX_SUBSYS_NUM; i++) {
  991. if (hwregs[i][0] != NULL &&
  992. (ioread32((void*)(hwregs[i][0] + MMU_REG_HW_ID))>>16) != 0x4D4D)
  993. return MMU_STATUS_FALSE;
  994. if (hwregs[i][1] != NULL &&
  995. (ioread32((void*)(hwregs[i][1] + MMU_REG_HW_ID))>>16) != 0x4D4D)
  996. return MMU_STATUS_FALSE;
  997. }
  998. pr_info(" *****MMU cleanup*****\n");
  999. if (pcie) {
  1000. if (g_mmu->stlb_virtual)
  1001. iounmap(g_mmu->stlb_virtual);
  1002. if (g_mmu->mtlb_virtual)
  1003. iounmap(g_mmu->mtlb_virtual);
  1004. if (g_mmu->page_table_array)
  1005. iounmap(g_mmu->page_table_array);
  1006. } else {
  1007. /* stlb_virtual is same alloc on alloc mtlb_virtual in func MMUEnable()
  1008. * so, should not free g_mmu->stlb_virtual.But free handle g_mmu->mtlb_physical
  1009. * size should be ( g_mmu->mtlb_size+g_mmu->stlb_size)
  1010. * */
  1011. if (g_mmu->mtlb_virtual)
  1012. dma_free_coherent(&platformdev->dev, g_mmu->mtlb_size+g_mmu->stlb_size,
  1013. g_mmu->mtlb_virtual, (dma_addr_t)g_mmu->mtlb_physical);
  1014. if (g_mmu->page_table_array)
  1015. dma_free_coherent(&platformdev->dev, g_mmu->page_table_array_size,
  1016. g_mmu->page_table_array, (dma_addr_t)g_mmu->page_table_array_physical);
  1017. }
  1018. DeleteMutex(g_mmu->page_table_mutex);
  1019. for (i = 0; i < MMU_REGION_COUNT; i++) {
  1020. DeleteMutex(g_mmu->region[i].node_mutex);
  1021. if (simple_map) {
  1022. p = g_mmu->region[i].simple_map_head;
  1023. while(p) {
  1024. tmp = p->next;
  1025. FreeMemory(p);
  1026. p = tmp;
  1027. MMUDEBUG(" *****clean node*****\n");
  1028. }
  1029. } else {
  1030. fp = g_mmu->region[i].free_map_head;
  1031. p = g_mmu->region[i].map_head;
  1032. while(fp) {
  1033. tmp = fp->next;
  1034. FreeMemory(fp);
  1035. fp = tmp;
  1036. MMUDEBUG(" *****clean free node*****\n");
  1037. }
  1038. while(p) {
  1039. tmp = p->next;
  1040. FreeMemory(p);
  1041. p = tmp;
  1042. MMUDEBUG(" *****clean node*****\n");
  1043. }
  1044. }
  1045. }
  1046. FreeMemory(g_mmu);
  1047. platform_device_unregister(platformdev);
  1048. platform_driver_unregister(&hantro_drm_platform_driver);
  1049. pr_info("Unregister platform device.\n");
  1050. for (i = 0; i < MAX_SUBSYS_NUM; i++) {
  1051. if (hwregs[i][0] != NULL)
  1052. iowrite32(0, (void*)(hwregs[i][0] + MMU_REG_CONTROL));
  1053. if (hwregs[i][1] != NULL)
  1054. iowrite32(0, (void*)(hwregs[i][1] + MMU_REG_CONTROL));
  1055. }
  1056. mmu_enable = 0;
  1057. mmu_init = 0;
  1058. return MMU_STATUS_OK;
  1059. }
  1060. /*------------------------------------------------------------------------------
  1061. Function name: MMUEnable
  1062. Description:
  1063. Create TLB, set registers and enable MMU
  1064. For pcie, TLB buffers come from FPGA memory and The distribution is as follows
  1065. MTLB: start from: 0x00100000, size: 4K bits
  1066. page table array: 0x00200000 64 bits
  1067. STLB: 0x00300000 4M bits
  1068. ------------------------------------------------------------------------------*/
  1069. enum MMUStatus MMUEnable(volatile unsigned char *hwregs[MAX_SUBSYS_NUM][2]) {
  1070. enum MMUStatus status;
  1071. unsigned int address;
  1072. unsigned int mutex = MMU_FALSE;
  1073. dma_addr_t dma_handle;
  1074. u32 i = 0;
  1075. u32 address_ext;
  1076. u32 total_table_size;
  1077. if(mmu_enable == MMU_TRUE) {
  1078. pr_info(" *****MMU Already Enabled*****\n");
  1079. return MMU_STATUS_OK;
  1080. }
  1081. pr_info(" *****MMU Enable...*****\n");
  1082. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  1083. mutex = MMU_TRUE;
  1084. if(pcie) {
  1085. g_mmu->mtlb_size = MMU_MTLB_SIZE;
  1086. g_mmu->mtlb_virtual = ioremap(gBaseDDRHw+MTLB_PCIE_START_ADDRESS, g_mmu->mtlb_size);
  1087. MMUDEBUG("gBaseDDRHw=0x%llx, g_mmu->mtlb_virtual=0x%llx\n", gBaseDDRHw, g_mmu->mtlb_virtual);
  1088. g_mmu->mtlb_physical = MTLB_PCIE_START_ADDRESS;
  1089. g_mmu->page_table_array = ioremap(gBaseDDRHw+PAGE_PCIE_START_ADDRESS, PAGE_TABLE_ENTRY_SIZE);
  1090. } else {
  1091. /* Allocate the 4K mode MTLB table. */
  1092. total_table_size = MMU_MTLB_SIZE + MMU_MTLB_ENTRY_NUM*MMU_STLB_4K_SIZE;
  1093. g_mmu->mtlb_size = MMU_MTLB_SIZE;
  1094. g_mmu->mtlb_virtual = dma_alloc_coherent(&platformdev->dev, total_table_size,
  1095. &dma_handle, GFP_KERNEL | GFP_DMA);
  1096. MMUDEBUG(" *****g_mmu->mtlb_virtual = 0x%llx\n", g_mmu->mtlb_virtual);
  1097. g_mmu->mtlb_physical = (unsigned long long)dma_handle;
  1098. MMUDEBUG(" *****mtlb_physical = 0x%llx\n", (unsigned int)g_mmu->mtlb_physical);
  1099. if(g_mmu->mtlb_virtual == NULL) {
  1100. pr_err("hantrodec alloc buffer fail\n");
  1101. status = MMU_STATUS_FALSE;
  1102. goto onerror;
  1103. }
  1104. g_mmu->page_table_array_size = PAGE_TABLE_ENTRY_SIZE;
  1105. g_mmu->page_table_array = dma_alloc_coherent(&platformdev->dev, g_mmu->page_table_array_size,
  1106. &dma_handle, GFP_KERNEL | GFP_DMA);
  1107. MMUDEBUG(" *****g_mmu->page_table_array = 0x%llx\n", g_mmu->page_table_array);
  1108. g_mmu->page_table_array_physical = (unsigned long long)dma_handle;
  1109. MMUDEBUG(" *****page_table_array_physical = 0x%llx\n", (unsigned int)g_mmu->page_table_array_physical);
  1110. if(g_mmu->page_table_array == NULL) {
  1111. pr_err("hantrodec alloc buffer fail\n");
  1112. status = MMU_STATUS_FALSE;
  1113. goto onerror;
  1114. }
  1115. }
  1116. *((unsigned int*)g_mmu->page_table_array) =
  1117. (g_mmu->mtlb_physical & 0xFFFFFC00) | (0 << 0);
  1118. *((unsigned int *)g_mmu->page_table_array+1) =
  1119. (u32)(g_mmu->mtlb_physical >> 32)&0xff;
  1120. *((unsigned int *)g_mmu->page_table_array+2) =
  1121. (g_mmu->mtlb_physical & 0xFFFFFC00) | (0 << 0);
  1122. *((unsigned int *)g_mmu->page_table_array+3) =
  1123. (u32)(g_mmu->mtlb_physical >> 32)&0xff;
  1124. MMUDEBUG(" Page table array[0]: lsb = 0x%08x\n", ((int *)g_mmu->page_table_array)[0]);
  1125. MMUDEBUG(" msb = 0x%08x\n", ((int *)g_mmu->page_table_array)[1]);
  1126. ZeroMemory(g_mmu->mtlb_virtual, total_table_size);
  1127. ReleaseMutex(g_mmu->page_table_mutex);
  1128. MMU_ON_ERROR(SetupDynamicSpace());
  1129. if(pcie) {
  1130. address = PAGE_PCIE_START_ADDRESS;
  1131. } else {
  1132. address = g_mmu->page_table_array_physical;
  1133. address_ext = ((u32)(g_mmu->page_table_array_physical >> 32))&0xff;
  1134. }
  1135. #ifndef HANTROVCMD_ENABLE_IP_SUPPORT
  1136. /* set regs of all MMUs */
  1137. for (i = 0; i < MAX_SUBSYS_NUM; i++) {
  1138. if (hwregs[i][0] != NULL) {
  1139. MMUDEBUG("hwregs[%d][0]=%p, id=0x%08x", i, hwregs[i][0], ioread32((void*)hwregs[i][0] + MMU_REG_HW_ID));
  1140. iowrite32(address, (void*)(hwregs[i][0] + MMU_REG_ADDRESS));
  1141. iowrite32(address_ext, (void *)(hwregs[i][0] + MMU_REG_ADDRESS_MSB));
  1142. iowrite32(0x10000, (void*)(hwregs[i][0] + MMU_REG_PAGE_TABLE_ID));
  1143. iowrite32(0x00000, (void*)(hwregs[i][0] + MMU_REG_PAGE_TABLE_ID));
  1144. iowrite32(1, (void*)(hwregs[i][0] + MMU_REG_CONTROL));
  1145. }
  1146. if (hwregs[i][1] != NULL) {
  1147. MMUDEBUG("hwregs[%d][1]=%p, id=0x%08x", i, hwregs[i][1], ioread32((void*)hwregs[i][1] + MMU_REG_HW_ID));
  1148. iowrite32(address, (void*)(hwregs[i][1] + MMU_REG_ADDRESS));
  1149. iowrite32(address_ext, (void *)(hwregs[i][1] + MMU_REG_ADDRESS_MSB));
  1150. iowrite32(0x10000, (void*)(hwregs[i][1] + MMU_REG_PAGE_TABLE_ID));
  1151. iowrite32(0x00000, (void*)(hwregs[i][1] + MMU_REG_PAGE_TABLE_ID));
  1152. iowrite32(1, (void*)(hwregs[i][1] + MMU_REG_CONTROL));
  1153. }
  1154. }
  1155. #endif
  1156. mmu_enable = MMU_TRUE;
  1157. return MMU_STATUS_OK;
  1158. onerror:
  1159. if (mutex) {
  1160. ReleaseMutex(g_mmu->page_table_mutex);
  1161. }
  1162. MMUDEBUG(" *****MMU Enable Error*****\n");
  1163. return status;
  1164. }
  1165. /*------------------------------------------------------------------------------
  1166. Function name: MMUFlush
  1167. Description:
  1168. Flush MMU reg to update cache in MMU.
  1169. ------------------------------------------------------------------------------*/
  1170. static enum MMUStatus MMUFlush(u32 core_id, volatile unsigned char *hwregs[MAX_SUBSYS_NUM][2]) {
  1171. enum MMUStatus status;
  1172. unsigned int mutex = MMU_FALSE;
  1173. MMUDEBUG(" *****MMU Flush*****\n");
  1174. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  1175. mutex = MMU_TRUE;
  1176. if (hwregs[core_id][0] != NULL) {
  1177. iowrite32(0x10, (void*)(hwregs[core_id][0] + MMU_REG_FLUSH));
  1178. iowrite32(0x00, (void*)(hwregs[core_id][0] + MMU_REG_FLUSH));
  1179. } else {
  1180. pr_err("hantrodec alloc buffer fail\n");
  1181. status = MMU_STATUS_FALSE;
  1182. goto onerror;
  1183. }
  1184. if (hwregs[core_id][1] != NULL) {
  1185. iowrite32(0x10, (void*)(hwregs[core_id][1] + MMU_REG_FLUSH));
  1186. iowrite32(0x00, (void*)(hwregs[core_id][1] + MMU_REG_FLUSH));
  1187. }
  1188. ReleaseMutex(g_mmu->page_table_mutex);
  1189. return MMU_STATUS_OK;
  1190. onerror:
  1191. if (mutex) {
  1192. ReleaseMutex(g_mmu->page_table_mutex);
  1193. }
  1194. MMUDEBUG(" *****MMU Flush Error*****\n");
  1195. return status;
  1196. }
  1197. static enum MMUStatus MMUMemNodeMap(struct addr_desc *addr, struct file *filp) {
  1198. enum MMUStatus status;
  1199. unsigned int page_count = 0;
  1200. unsigned int i = 0;
  1201. struct MMUNode *p;
  1202. unsigned long long address = 0x0;
  1203. unsigned int *page_table_entry;
  1204. enum MMURegion e;
  1205. unsigned int mutex = MMU_FALSE;
  1206. u32 ext_addr;
  1207. MMUDEBUG(" *****MMU Map*****\n");
  1208. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  1209. mutex = MMU_TRUE;
  1210. page_count = (addr->size - 1)/PAGE_SIZE + 1;
  1211. GetPhysicalAddress(addr->virtual_address, &address);
  1212. MMUDEBUG(" *****MMU map address*****%x\n", address);
  1213. if(address >= REGION_IN_START &&
  1214. address + addr->size < REGION_IN_END)
  1215. e = MMU_REGION_IN;
  1216. else if(address >= REGION_OUT_START &&
  1217. address + addr->size < REGION_OUT_END)
  1218. e = MMU_REGION_OUT;
  1219. else if(address >= REGION_PRIVATE_START &&
  1220. address + addr->size < REGION_PRIVATE_END)
  1221. e = MMU_REGION_PRIVATE;
  1222. else
  1223. e = MMU_REGION_PUB;
  1224. if (simple_map) {
  1225. MMU_ON_ERROR(SMCheckAddress(e, addr->virtual_address));
  1226. SMCreateNode(e, &p, page_count);
  1227. MMUDEBUG(" *****Node map size*****%d\n", page_count);
  1228. p->buf_virtual_address = addr->virtual_address;
  1229. p->process_id = GetProcessID();
  1230. p->filp = filp;
  1231. p->mtlb_start = ((address + map_shift) >> MMU_MTLB_SHIFT);
  1232. p->stlb_start = ((address + map_shift) >> MMU_STLB_4K_SHIFT ) & 0x3FF;
  1233. p->mtlb_end = (page_count + p->stlb_start) / MMU_STLB_4K_ENTRY_NUM +
  1234. p->mtlb_start;
  1235. p->stlb_end = (page_count + p->stlb_start) % MMU_STLB_4K_ENTRY_NUM;
  1236. p->page_count = page_count;
  1237. for(i = 0;i < page_count; i++) {
  1238. GetPhysicalAddress(addr->virtual_address + i * PAGE_SIZE, &address);
  1239. GetPageEntry(p, &page_table_entry, i);
  1240. ext_addr = ((u32)(address>>32))&0xff;
  1241. address = (address & 0xFFFFF000)
  1242. /* ext address , physical address bits [39,32]*/
  1243. | (ext_addr << 4)
  1244. /* writable */
  1245. | (1 << 2)
  1246. /* Ignore exception */
  1247. | (0 << 1)
  1248. /* Present */
  1249. | (1 << 0);
  1250. WritePageEntry(page_table_entry, address);
  1251. }
  1252. /* Purpose of Bare_metal mode: input bus address==mmu address*/
  1253. addr->bus_address = p->mtlb_start << MMU_MTLB_SHIFT
  1254. | p->stlb_start << MMU_STLB_4K_SHIFT;
  1255. } else {
  1256. MMU_ON_ERROR(FindFreeNode(e, &p, page_count));
  1257. SplitFreeNode(e, &p, page_count);
  1258. MMUDEBUG(" *****Node map size*****%d\n", p->page_count);
  1259. p->buf_virtual_address = addr->virtual_address;
  1260. p->process_id = GetProcessID();
  1261. p->filp = filp;
  1262. for(i = 0;i < page_count; i++) {
  1263. GetPhysicalAddress(addr->virtual_address + i * PAGE_SIZE, &address);
  1264. GetPageEntry(p, &page_table_entry, i);
  1265. ext_addr = ((u32)(address>>32))&0xff;
  1266. address = (address & 0xFFFFF000)
  1267. /* ext address , physical address bits [39,32]*/
  1268. | (ext_addr << 4)
  1269. /* writable */
  1270. | (1 << 2)
  1271. /* Ignore exception */
  1272. | (0 << 1)
  1273. /* Present */
  1274. | (1 << 0);
  1275. WritePageEntry(page_table_entry, address);
  1276. }
  1277. addr->bus_address = p->mtlb_start << MMU_MTLB_SHIFT
  1278. | p->stlb_start << MMU_STLB_4K_SHIFT;
  1279. }
  1280. MMUDEBUG(" MMU_MTLB_SHIFT %d MMU_STLB_4K_SHIFT %d\n", MMU_MTLB_SHIFT, MMU_STLB_4K_SHIFT);
  1281. MMUDEBUG(" MMUMemNodeMap map total %d pages in region %d\nMTLB/STLB starts %d/%d, MTLB/STLB ends %d/%d\n",
  1282. page_count, (u32)e, p->mtlb_start, p->stlb_start, p->mtlb_end, p->stlb_end);
  1283. MMUDEBUG(" MMUMemNodeMap map %p -> 0x%08x\n", addr->virtual_address, addr->bus_address);
  1284. ReleaseMutex(g_mmu->page_table_mutex);
  1285. return MMU_STATUS_OK;
  1286. onerror:
  1287. if (mutex) {
  1288. ReleaseMutex(g_mmu->page_table_mutex);
  1289. }
  1290. MMUDEBUG(" *****MMU Map Error*****\n");
  1291. return status;
  1292. }
  1293. static enum MMUStatus MMUMemNodeUnmap(struct addr_desc *addr) {
  1294. unsigned int i;
  1295. unsigned long long address = 0x0;
  1296. unsigned int *page_table_entry;
  1297. int process_id = GetProcessID();
  1298. enum MMURegion e = MMU_REGION_COUNT;
  1299. enum MMUStatus status = MMU_STATUS_OUT_OF_MEMORY;
  1300. struct MMUNode *p;
  1301. unsigned int mutex = MMU_FALSE;
  1302. MMUDEBUG(" *****MMU Unmap*****\n");
  1303. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  1304. mutex = MMU_TRUE;
  1305. GetPhysicalAddress(addr->virtual_address, &address);
  1306. if(address >= REGION_IN_START &&
  1307. address < REGION_IN_END)
  1308. e = MMU_REGION_IN;
  1309. else if(address >= REGION_OUT_START &&
  1310. address < REGION_OUT_END)
  1311. e = MMU_REGION_OUT;
  1312. else if(address >= REGION_PRIVATE_START &&
  1313. address < REGION_PRIVATE_END)
  1314. e = MMU_REGION_PRIVATE;
  1315. else
  1316. e = MMU_REGION_PUB;
  1317. if (simple_map)
  1318. p = g_mmu->region[e].simple_map_head->next;
  1319. else
  1320. p = g_mmu->region[e].map_head->next;
  1321. /* Reset STLB of the node */
  1322. while(p) {
  1323. if(p->buf_virtual_address == addr->virtual_address &&
  1324. p->process_id == process_id) {
  1325. for(i = 0;i < p->page_count; i++) {
  1326. GetPageEntry(p, &page_table_entry, i);
  1327. address = 0;
  1328. WritePageEntry(page_table_entry, address);
  1329. }
  1330. break;
  1331. }
  1332. p = p->next;
  1333. }
  1334. if(!p)
  1335. goto onerror;
  1336. if (simple_map)
  1337. SMRemoveNode(e, addr->virtual_address, process_id);
  1338. else
  1339. RemoveNode(e, addr->virtual_address, process_id);
  1340. ReleaseMutex(g_mmu->page_table_mutex);
  1341. return MMU_STATUS_OK;
  1342. onerror:
  1343. if (mutex) {
  1344. ReleaseMutex(g_mmu->page_table_mutex);
  1345. }
  1346. MMUDEBUG(" *****MMU Unmap Error*****\n");
  1347. return status;
  1348. }
  1349. enum MMUStatus MMUKernelMemNodeMap(struct kernel_addr_desc *addr) {
  1350. enum MMUStatus status;
  1351. unsigned int page_count = 0;
  1352. unsigned int i = 0;
  1353. struct MMUNode *p;
  1354. unsigned long long address = 0x0;
  1355. unsigned int *page_table_entry;
  1356. enum MMURegion e;
  1357. unsigned int mutex = MMU_FALSE;
  1358. u32 ext_addr;
  1359. u32 page_entry_value = 0;
  1360. MMUDEBUG(" *****MMU Map*****\n");
  1361. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  1362. mutex = MMU_TRUE;
  1363. page_count = (addr->size - 1)/PAGE_SIZE + 1;
  1364. address = addr->bus_address;
  1365. MMUDEBUG(" *****MMU map address*****%x\n", address);
  1366. if(address >= REGION_IN_START &&
  1367. address + addr->size < REGION_IN_END)
  1368. e = MMU_REGION_IN;
  1369. else if(address >= REGION_OUT_START &&
  1370. address + addr->size < REGION_OUT_END)
  1371. e = MMU_REGION_OUT;
  1372. else if(address >= REGION_PRIVATE_START &&
  1373. address + addr->size < REGION_PRIVATE_END)
  1374. e = MMU_REGION_PRIVATE;
  1375. else
  1376. e = MMU_REGION_PUB;
  1377. if (simple_map) {
  1378. //TODO: should check bus addr
  1379. //MMU_ON_ERROR(SMCheckAddress(e, addr->virtual_address));
  1380. SMCreateNode(e, &p, page_count);
  1381. MMUDEBUG(" *****Node map size*****%d\n", page_count);
  1382. p->buf_bus_address = addr->bus_address;
  1383. p->process_id = GetProcessID();
  1384. p->filp = NULL;
  1385. p->mtlb_start = ((address + map_shift) >> MMU_MTLB_SHIFT);
  1386. p->stlb_start = ((address + map_shift) >> MMU_STLB_4K_SHIFT ) & 0x3FF;
  1387. p->mtlb_end = (page_count + p->stlb_start) / MMU_STLB_4K_ENTRY_NUM +
  1388. p->mtlb_start;
  1389. p->stlb_end = (page_count + p->stlb_start) % MMU_STLB_4K_ENTRY_NUM;
  1390. p->page_count = page_count;
  1391. for(i = 0;i < page_count; i++) {
  1392. /* this function used in kernel only, so we think it's a contunuous buffer*/
  1393. address += (i ? PAGE_SIZE : 0);
  1394. GetPageEntry(p, &page_table_entry, i);
  1395. ext_addr = ((u32)(address>>32))&0xff;
  1396. page_entry_value = (address & 0xFFFFF000)
  1397. /* ext address , physical address bits [39,32]*/
  1398. | (ext_addr << 4)
  1399. /* writable */
  1400. | (1 << 2)
  1401. /* Ignore exception */
  1402. | (0 << 1)
  1403. /* Present */
  1404. | (1 << 0);
  1405. WritePageEntry(page_table_entry, page_entry_value);
  1406. }
  1407. /* Purpose of Bare_metal mode: input bus address==mmu address*/
  1408. addr->mmu_bus_address = p->mtlb_start << MMU_MTLB_SHIFT
  1409. | p->stlb_start << MMU_STLB_4K_SHIFT;
  1410. } else {
  1411. MMU_ON_ERROR(FindFreeNode(e, &p, page_count));
  1412. SplitFreeNode(e, &p, page_count);
  1413. MMUDEBUG(" *****Node map size*****%d\n", p->page_count);
  1414. p->buf_bus_address = addr->bus_address;
  1415. p->process_id = GetProcessID();
  1416. p->filp = NULL;
  1417. for(i = 0;i < page_count; i++) {
  1418. /* this function used in kernel only, so we think it's a contunuous buffer*/
  1419. address += (i ? PAGE_SIZE : 0);
  1420. GetPageEntry(p, &page_table_entry, i);
  1421. ext_addr = ((u32)(address>>32))&0xff;
  1422. page_entry_value = (address & 0xFFFFF000)
  1423. /* ext address , physical address bits [39,32]*/
  1424. | (ext_addr << 4)
  1425. /* writable */
  1426. | (1 << 2)
  1427. /* Ignore exception */
  1428. | (0 << 1)
  1429. /* Present */
  1430. | (1 << 0);
  1431. WritePageEntry(page_table_entry, page_entry_value);
  1432. }
  1433. addr->mmu_bus_address = p->mtlb_start << MMU_MTLB_SHIFT
  1434. | p->stlb_start << MMU_STLB_4K_SHIFT;
  1435. }
  1436. MMUDEBUG(" KERNEL MMU_MTLB_SHIFT %d MMU_STLB_4K_SHIFT %d\n", MMU_MTLB_SHIFT, MMU_STLB_4K_SHIFT);
  1437. MMUDEBUG(" MMUKernelMemNodeMap map total %d pages in region %d\nMTLB/STLB starts %d/%d, MTLB/STLB ends %d/%d\n",
  1438. page_count, (u32)e, p->mtlb_start, p->stlb_start, p->mtlb_end, p->stlb_end);
  1439. MMUDEBUG(" MMUKernelMemNodeMap map 0x%llx -> 0x%08x\n", addr->bus_address, addr->mmu_bus_address);
  1440. ReleaseMutex(g_mmu->page_table_mutex);
  1441. return MMU_STATUS_OK;
  1442. onerror:
  1443. if (mutex) {
  1444. ReleaseMutex(g_mmu->page_table_mutex);
  1445. }
  1446. MMUDEBUG(" *****MMU Map Error*****\n");
  1447. return status;
  1448. }
  1449. enum MMUStatus MMUKernelMemNodeUnmap(struct kernel_addr_desc *addr) {
  1450. unsigned int i;
  1451. unsigned long long address = 0x0;
  1452. unsigned int *page_table_entry;
  1453. int process_id = GetProcessID();
  1454. enum MMURegion e = MMU_REGION_COUNT;
  1455. enum MMUStatus status = MMU_STATUS_OUT_OF_MEMORY;
  1456. struct MMUNode *p;
  1457. unsigned int mutex = MMU_FALSE;
  1458. MMUDEBUG(" *****MMU Unmap*****\n");
  1459. AcquireMutex(g_mmu->page_table_mutex, MMU_INFINITE);
  1460. mutex = MMU_TRUE;
  1461. address = addr->bus_address;
  1462. if(address >= REGION_IN_START &&
  1463. address < REGION_IN_END)
  1464. e = MMU_REGION_IN;
  1465. else if(address >= REGION_OUT_START &&
  1466. address < REGION_OUT_END)
  1467. e = MMU_REGION_OUT;
  1468. else if(address >= REGION_PRIVATE_START &&
  1469. address < REGION_PRIVATE_END)
  1470. e = MMU_REGION_PRIVATE;
  1471. else
  1472. e = MMU_REGION_PUB;
  1473. if (simple_map)
  1474. p = g_mmu->region[e].simple_map_head->next;
  1475. else
  1476. p = g_mmu->region[e].map_head->next;
  1477. /* Reset STLB of the node */
  1478. while(p) {
  1479. if(p->buf_bus_address == addr->bus_address &&
  1480. p->process_id == process_id) {
  1481. for(i = 0;i < p->page_count; i++) {
  1482. GetPageEntry(p, &page_table_entry, i);
  1483. address = 0;
  1484. WritePageEntry(page_table_entry, address);
  1485. }
  1486. break;
  1487. }
  1488. p = p->next;
  1489. }
  1490. if(!p)
  1491. goto onerror;
  1492. if (simple_map)
  1493. SMRemoveKernelNode(e, addr->bus_address, process_id);
  1494. else
  1495. RemoveKernelNode(e, addr->bus_address, process_id);
  1496. ReleaseMutex(g_mmu->page_table_mutex);
  1497. return MMU_STATUS_OK;
  1498. onerror:
  1499. if (mutex) {
  1500. ReleaseMutex(g_mmu->page_table_mutex);
  1501. }
  1502. MMUDEBUG(" *****MMU Unmap Error*****\n");
  1503. return status;
  1504. }
  1505. static long MMUCtlBufferMap(struct file *filp, unsigned long arg) {
  1506. struct addr_desc addr;
  1507. long tmp;
  1508. tmp = copy_from_user(&addr, (void*)arg, sizeof(struct addr_desc));
  1509. if (tmp) {
  1510. MMUDEBUG("copy_from_user failed, returned %li\n", tmp);
  1511. return -MMU_EFAULT;
  1512. }
  1513. MMUMemNodeMap(&addr, filp);
  1514. tmp = copy_to_user((void*) arg, &addr, sizeof(struct addr_desc));
  1515. if (tmp) {
  1516. MMUDEBUG("copy_to_user failed, returned %li\n", tmp);
  1517. return -MMU_EFAULT;
  1518. }
  1519. return 0;
  1520. }
  1521. static long MMUCtlBufferUnmap(unsigned long arg) {
  1522. struct addr_desc addr;
  1523. long tmp;
  1524. tmp = copy_from_user(&addr, (void*)arg, sizeof(struct addr_desc));
  1525. if (tmp) {
  1526. MMUDEBUG("copy_from_user failed, returned %li\n", tmp);
  1527. return -MMU_EFAULT;
  1528. }
  1529. MMUMemNodeUnmap(&addr);
  1530. return 0;
  1531. }
  1532. static long MMUCtlEnable(unsigned long arg, volatile unsigned char *hwregs[HXDEC_MAX_CORES][2]) {
  1533. unsigned int enable;
  1534. long tmp;
  1535. tmp = copy_from_user(&enable, (void*)arg, sizeof(unsigned int));
  1536. if (tmp) {
  1537. MMUDEBUG("copy_from_user failed, returned %li\n", tmp);
  1538. return -MMU_EFAULT;
  1539. }
  1540. MMUEnable(hwregs);
  1541. return 0;
  1542. }
  1543. static long MMUCtlFlush(unsigned long arg, volatile unsigned char *hwregs[HXDEC_MAX_CORES][2]) {
  1544. unsigned int core_id;
  1545. long tmp;
  1546. tmp = copy_from_user(&core_id, (void*)arg, sizeof(unsigned int));
  1547. if (tmp) {
  1548. MMUDEBUG("copy_from_user failed, returned %li\n", tmp);
  1549. return -MMU_EFAULT;
  1550. }
  1551. MMUFlush(core_id, hwregs);
  1552. return 0;
  1553. }
  1554. long MMUIoctl(unsigned int cmd, void *filp, unsigned long arg,
  1555. volatile unsigned char *hwregs[HXDEC_MAX_CORES][2]) {
  1556. u32 i = 0;
  1557. for (i = 0; i < MAX_SUBSYS_NUM; i++) {
  1558. if (hwregs[i][0] != NULL &&
  1559. (ioread32((void*)(hwregs[i][0] + MMU_REG_HW_ID))>>16) != 0x4D4D)
  1560. return -MMU_ENOTTY;
  1561. if (hwregs[i][1] != NULL &&
  1562. (ioread32((void*)(hwregs[i][1] + MMU_REG_HW_ID))>>16) != 0x4D4D)
  1563. return -MMU_ENOTTY;
  1564. MMUDEBUG("mmu_hwregs[%d][0].mmu_hwregs[0]=%p", i, hwregs[i][0]);
  1565. MMUDEBUG("mmu_hwregs[%d][1].mmu_hwregs[0]=%p", i, hwregs[i][1]);
  1566. }
  1567. switch (cmd) {
  1568. case HANTRO_IOCS_MMU_MEM_MAP: {
  1569. return (MMUCtlBufferMap((struct file *)filp, arg));
  1570. }
  1571. case HANTRO_IOCS_MMU_MEM_UNMAP: {
  1572. return (MMUCtlBufferUnmap(arg));
  1573. }
  1574. case HANTRO_IOCS_MMU_ENABLE: {
  1575. return (MMUCtlEnable(arg, hwregs));
  1576. }
  1577. case HANTRO_IOCS_MMU_FLUSH: {
  1578. return (MMUCtlFlush(arg, hwregs));
  1579. }
  1580. default:
  1581. return -MMU_ENOTTY;
  1582. }
  1583. }
  1584. unsigned long long GetMMUAddress(void)
  1585. {
  1586. unsigned long long address = 0;
  1587. if(pcie)
  1588. address = PAGE_PCIE_START_ADDRESS;
  1589. else
  1590. address = g_mmu->page_table_array_physical;
  1591. return address;
  1592. }
  1593. void MMURestore(volatile unsigned char *hwregs[MAX_SUBSYS_NUM][2])
  1594. {
  1595. if (g_mmu == NULL)
  1596. return;
  1597. int i;
  1598. unsigned int address;
  1599. u32 address_ext;
  1600. address = g_mmu->page_table_array_physical;
  1601. address_ext = ((u32)(g_mmu->page_table_array_physical >> 32))&0xff;
  1602. for (i = 0; i < MAX_SUBSYS_NUM; i++) {
  1603. if (hwregs[i][0] != NULL) {
  1604. iowrite32(address, (void*)(hwregs[i][0] + MMU_REG_ADDRESS));
  1605. iowrite32(address_ext, (void *)(hwregs[i][0] + MMU_REG_ADDRESS_MSB));
  1606. iowrite32(0x10000, (void*)(hwregs[i][0] + MMU_REG_PAGE_TABLE_ID));
  1607. iowrite32(0x00000, (void*)(hwregs[i][0] + MMU_REG_PAGE_TABLE_ID));
  1608. iowrite32(1, (void*)(hwregs[i][0] + MMU_REG_CONTROL));
  1609. }
  1610. if (hwregs[i][1] != NULL) {
  1611. iowrite32(address, (void*)(hwregs[i][1] + MMU_REG_ADDRESS));
  1612. iowrite32(address_ext, (void *)(hwregs[i][1] + MMU_REG_ADDRESS_MSB));
  1613. iowrite32(0x10000, (void*)(hwregs[i][1] + MMU_REG_PAGE_TABLE_ID));
  1614. iowrite32(0x00000, (void*)(hwregs[i][1] + MMU_REG_PAGE_TABLE_ID));
  1615. iowrite32(1, (void*)(hwregs[i][1] + MMU_REG_CONTROL));
  1616. }
  1617. }
  1618. }