hantro_mmu.c 58 KB

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