omap-iommu.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /*
  2. * omap iommu: tlb and pagetable primitives
  3. *
  4. * Copyright (C) 2008-2010 Nokia Corporation
  5. *
  6. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>,
  7. * Paul Mundt and Toshihiro Kobayashi
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/iommu.h>
  20. #include <linux/omap-iommu.h>
  21. #include <linux/mutex.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/pm_runtime.h>
  25. #include <asm/cacheflush.h>
  26. #include <linux/platform_data/iommu-omap.h>
  27. #include "omap-iopgtable.h"
  28. #include "omap-iommu.h"
  29. #define for_each_iotlb_cr(obj, n, __i, cr) \
  30. for (__i = 0; \
  31. (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true); \
  32. __i++)
  33. /* bitmap of the page sizes currently supported */
  34. #define OMAP_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
  35. /**
  36. * struct omap_iommu_domain - omap iommu domain
  37. * @pgtable: the page table
  38. * @iommu_dev: an omap iommu device attached to this domain. only a single
  39. * iommu device can be attached for now.
  40. * @dev: Device using this domain.
  41. * @lock: domain lock, should be taken when attaching/detaching
  42. */
  43. struct omap_iommu_domain {
  44. u32 *pgtable;
  45. struct omap_iommu *iommu_dev;
  46. struct device *dev;
  47. spinlock_t lock;
  48. };
  49. #define MMU_LOCK_BASE_SHIFT 10
  50. #define MMU_LOCK_BASE_MASK (0x1f << MMU_LOCK_BASE_SHIFT)
  51. #define MMU_LOCK_BASE(x) \
  52. ((x & MMU_LOCK_BASE_MASK) >> MMU_LOCK_BASE_SHIFT)
  53. #define MMU_LOCK_VICT_SHIFT 4
  54. #define MMU_LOCK_VICT_MASK (0x1f << MMU_LOCK_VICT_SHIFT)
  55. #define MMU_LOCK_VICT(x) \
  56. ((x & MMU_LOCK_VICT_MASK) >> MMU_LOCK_VICT_SHIFT)
  57. struct iotlb_lock {
  58. short base;
  59. short vict;
  60. };
  61. /* accommodate the difference between omap1 and omap2/3 */
  62. static const struct iommu_functions *arch_iommu;
  63. static struct platform_driver omap_iommu_driver;
  64. static struct kmem_cache *iopte_cachep;
  65. /**
  66. * omap_install_iommu_arch - Install archtecure specific iommu functions
  67. * @ops: a pointer to architecture specific iommu functions
  68. *
  69. * There are several kind of iommu algorithm(tlb, pagetable) among
  70. * omap series. This interface installs such an iommu algorighm.
  71. **/
  72. int omap_install_iommu_arch(const struct iommu_functions *ops)
  73. {
  74. if (arch_iommu)
  75. return -EBUSY;
  76. arch_iommu = ops;
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_GPL(omap_install_iommu_arch);
  80. /**
  81. * omap_uninstall_iommu_arch - Uninstall archtecure specific iommu functions
  82. * @ops: a pointer to architecture specific iommu functions
  83. *
  84. * This interface uninstalls the iommu algorighm installed previously.
  85. **/
  86. void omap_uninstall_iommu_arch(const struct iommu_functions *ops)
  87. {
  88. if (arch_iommu != ops)
  89. pr_err("%s: not your arch\n", __func__);
  90. arch_iommu = NULL;
  91. }
  92. EXPORT_SYMBOL_GPL(omap_uninstall_iommu_arch);
  93. /**
  94. * omap_iommu_save_ctx - Save registers for pm off-mode support
  95. * @dev: client device
  96. **/
  97. void omap_iommu_save_ctx(struct device *dev)
  98. {
  99. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  100. arch_iommu->save_ctx(obj);
  101. }
  102. EXPORT_SYMBOL_GPL(omap_iommu_save_ctx);
  103. /**
  104. * omap_iommu_restore_ctx - Restore registers for pm off-mode support
  105. * @dev: client device
  106. **/
  107. void omap_iommu_restore_ctx(struct device *dev)
  108. {
  109. struct omap_iommu *obj = dev_to_omap_iommu(dev);
  110. arch_iommu->restore_ctx(obj);
  111. }
  112. EXPORT_SYMBOL_GPL(omap_iommu_restore_ctx);
  113. /**
  114. * omap_iommu_arch_version - Return running iommu arch version
  115. **/
  116. u32 omap_iommu_arch_version(void)
  117. {
  118. return arch_iommu->version;
  119. }
  120. EXPORT_SYMBOL_GPL(omap_iommu_arch_version);
  121. static int iommu_enable(struct omap_iommu *obj)
  122. {
  123. int err;
  124. struct platform_device *pdev = to_platform_device(obj->dev);
  125. struct iommu_platform_data *pdata = pdev->dev.platform_data;
  126. if (!pdata)
  127. return -EINVAL;
  128. if (!arch_iommu)
  129. return -ENODEV;
  130. if (pdata->deassert_reset) {
  131. err = pdata->deassert_reset(pdev, pdata->reset_name);
  132. if (err) {
  133. dev_err(obj->dev, "deassert_reset failed: %d\n", err);
  134. return err;
  135. }
  136. }
  137. pm_runtime_get_sync(obj->dev);
  138. err = arch_iommu->enable(obj);
  139. return err;
  140. }
  141. static void iommu_disable(struct omap_iommu *obj)
  142. {
  143. struct platform_device *pdev = to_platform_device(obj->dev);
  144. struct iommu_platform_data *pdata = pdev->dev.platform_data;
  145. if (!pdata)
  146. return;
  147. arch_iommu->disable(obj);
  148. pm_runtime_put_sync(obj->dev);
  149. if (pdata->assert_reset)
  150. pdata->assert_reset(pdev, pdata->reset_name);
  151. }
  152. /*
  153. * TLB operations
  154. */
  155. void omap_iotlb_cr_to_e(struct cr_regs *cr, struct iotlb_entry *e)
  156. {
  157. BUG_ON(!cr || !e);
  158. arch_iommu->cr_to_e(cr, e);
  159. }
  160. EXPORT_SYMBOL_GPL(omap_iotlb_cr_to_e);
  161. static inline int iotlb_cr_valid(struct cr_regs *cr)
  162. {
  163. if (!cr)
  164. return -EINVAL;
  165. return arch_iommu->cr_valid(cr);
  166. }
  167. static inline struct cr_regs *iotlb_alloc_cr(struct omap_iommu *obj,
  168. struct iotlb_entry *e)
  169. {
  170. if (!e)
  171. return NULL;
  172. return arch_iommu->alloc_cr(obj, e);
  173. }
  174. static u32 iotlb_cr_to_virt(struct cr_regs *cr)
  175. {
  176. return arch_iommu->cr_to_virt(cr);
  177. }
  178. static u32 get_iopte_attr(struct iotlb_entry *e)
  179. {
  180. return arch_iommu->get_pte_attr(e);
  181. }
  182. static u32 iommu_report_fault(struct omap_iommu *obj, u32 *da)
  183. {
  184. return arch_iommu->fault_isr(obj, da);
  185. }
  186. static void iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l)
  187. {
  188. u32 val;
  189. val = iommu_read_reg(obj, MMU_LOCK);
  190. l->base = MMU_LOCK_BASE(val);
  191. l->vict = MMU_LOCK_VICT(val);
  192. }
  193. static void iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l)
  194. {
  195. u32 val;
  196. val = (l->base << MMU_LOCK_BASE_SHIFT);
  197. val |= (l->vict << MMU_LOCK_VICT_SHIFT);
  198. iommu_write_reg(obj, val, MMU_LOCK);
  199. }
  200. static void iotlb_read_cr(struct omap_iommu *obj, struct cr_regs *cr)
  201. {
  202. arch_iommu->tlb_read_cr(obj, cr);
  203. }
  204. static void iotlb_load_cr(struct omap_iommu *obj, struct cr_regs *cr)
  205. {
  206. arch_iommu->tlb_load_cr(obj, cr);
  207. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  208. iommu_write_reg(obj, 1, MMU_LD_TLB);
  209. }
  210. /**
  211. * iotlb_dump_cr - Dump an iommu tlb entry into buf
  212. * @obj: target iommu
  213. * @cr: contents of cam and ram register
  214. * @buf: output buffer
  215. **/
  216. static inline ssize_t iotlb_dump_cr(struct omap_iommu *obj, struct cr_regs *cr,
  217. char *buf)
  218. {
  219. BUG_ON(!cr || !buf);
  220. return arch_iommu->dump_cr(obj, cr, buf);
  221. }
  222. /* only used in iotlb iteration for-loop */
  223. static struct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n)
  224. {
  225. struct cr_regs cr;
  226. struct iotlb_lock l;
  227. iotlb_lock_get(obj, &l);
  228. l.vict = n;
  229. iotlb_lock_set(obj, &l);
  230. iotlb_read_cr(obj, &cr);
  231. return cr;
  232. }
  233. /**
  234. * load_iotlb_entry - Set an iommu tlb entry
  235. * @obj: target iommu
  236. * @e: an iommu tlb entry info
  237. **/
  238. #ifdef PREFETCH_IOTLB
  239. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  240. {
  241. int err = 0;
  242. struct iotlb_lock l;
  243. struct cr_regs *cr;
  244. if (!obj || !obj->nr_tlb_entries || !e)
  245. return -EINVAL;
  246. pm_runtime_get_sync(obj->dev);
  247. iotlb_lock_get(obj, &l);
  248. if (l.base == obj->nr_tlb_entries) {
  249. dev_warn(obj->dev, "%s: preserve entries full\n", __func__);
  250. err = -EBUSY;
  251. goto out;
  252. }
  253. if (!e->prsvd) {
  254. int i;
  255. struct cr_regs tmp;
  256. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, tmp)
  257. if (!iotlb_cr_valid(&tmp))
  258. break;
  259. if (i == obj->nr_tlb_entries) {
  260. dev_dbg(obj->dev, "%s: full: no entry\n", __func__);
  261. err = -EBUSY;
  262. goto out;
  263. }
  264. iotlb_lock_get(obj, &l);
  265. } else {
  266. l.vict = l.base;
  267. iotlb_lock_set(obj, &l);
  268. }
  269. cr = iotlb_alloc_cr(obj, e);
  270. if (IS_ERR(cr)) {
  271. pm_runtime_put_sync(obj->dev);
  272. return PTR_ERR(cr);
  273. }
  274. iotlb_load_cr(obj, cr);
  275. kfree(cr);
  276. if (e->prsvd)
  277. l.base++;
  278. /* increment victim for next tlb load */
  279. if (++l.vict == obj->nr_tlb_entries)
  280. l.vict = l.base;
  281. iotlb_lock_set(obj, &l);
  282. out:
  283. pm_runtime_put_sync(obj->dev);
  284. return err;
  285. }
  286. #else /* !PREFETCH_IOTLB */
  287. static int load_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  288. {
  289. return 0;
  290. }
  291. #endif /* !PREFETCH_IOTLB */
  292. static int prefetch_iotlb_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  293. {
  294. return load_iotlb_entry(obj, e);
  295. }
  296. /**
  297. * flush_iotlb_page - Clear an iommu tlb entry
  298. * @obj: target iommu
  299. * @da: iommu device virtual address
  300. *
  301. * Clear an iommu tlb entry which includes 'da' address.
  302. **/
  303. static void flush_iotlb_page(struct omap_iommu *obj, u32 da)
  304. {
  305. int i;
  306. struct cr_regs cr;
  307. pm_runtime_get_sync(obj->dev);
  308. for_each_iotlb_cr(obj, obj->nr_tlb_entries, i, cr) {
  309. u32 start;
  310. size_t bytes;
  311. if (!iotlb_cr_valid(&cr))
  312. continue;
  313. start = iotlb_cr_to_virt(&cr);
  314. bytes = iopgsz_to_bytes(cr.cam & 3);
  315. if ((start <= da) && (da < start + bytes)) {
  316. dev_dbg(obj->dev, "%s: %08x<=%08x(%x)\n",
  317. __func__, start, da, bytes);
  318. iotlb_load_cr(obj, &cr);
  319. iommu_write_reg(obj, 1, MMU_FLUSH_ENTRY);
  320. }
  321. }
  322. pm_runtime_put_sync(obj->dev);
  323. if (i == obj->nr_tlb_entries)
  324. dev_dbg(obj->dev, "%s: no page for %08x\n", __func__, da);
  325. }
  326. /**
  327. * flush_iotlb_all - Clear all iommu tlb entries
  328. * @obj: target iommu
  329. **/
  330. static void flush_iotlb_all(struct omap_iommu *obj)
  331. {
  332. struct iotlb_lock l;
  333. pm_runtime_get_sync(obj->dev);
  334. l.base = 0;
  335. l.vict = 0;
  336. iotlb_lock_set(obj, &l);
  337. iommu_write_reg(obj, 1, MMU_GFLUSH);
  338. pm_runtime_put_sync(obj->dev);
  339. }
  340. #if defined(CONFIG_OMAP_IOMMU_DEBUG) || defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
  341. ssize_t omap_iommu_dump_ctx(struct omap_iommu *obj, char *buf, ssize_t bytes)
  342. {
  343. if (!obj || !buf)
  344. return -EINVAL;
  345. pm_runtime_get_sync(obj->dev);
  346. bytes = arch_iommu->dump_ctx(obj, buf, bytes);
  347. pm_runtime_put_sync(obj->dev);
  348. return bytes;
  349. }
  350. EXPORT_SYMBOL_GPL(omap_iommu_dump_ctx);
  351. static int
  352. __dump_tlb_entries(struct omap_iommu *obj, struct cr_regs *crs, int num)
  353. {
  354. int i;
  355. struct iotlb_lock saved;
  356. struct cr_regs tmp;
  357. struct cr_regs *p = crs;
  358. pm_runtime_get_sync(obj->dev);
  359. iotlb_lock_get(obj, &saved);
  360. for_each_iotlb_cr(obj, num, i, tmp) {
  361. if (!iotlb_cr_valid(&tmp))
  362. continue;
  363. *p++ = tmp;
  364. }
  365. iotlb_lock_set(obj, &saved);
  366. pm_runtime_put_sync(obj->dev);
  367. return p - crs;
  368. }
  369. /**
  370. * omap_dump_tlb_entries - dump cr arrays to given buffer
  371. * @obj: target iommu
  372. * @buf: output buffer
  373. **/
  374. size_t omap_dump_tlb_entries(struct omap_iommu *obj, char *buf, ssize_t bytes)
  375. {
  376. int i, num;
  377. struct cr_regs *cr;
  378. char *p = buf;
  379. num = bytes / sizeof(*cr);
  380. num = min(obj->nr_tlb_entries, num);
  381. cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
  382. if (!cr)
  383. return 0;
  384. num = __dump_tlb_entries(obj, cr, num);
  385. for (i = 0; i < num; i++)
  386. p += iotlb_dump_cr(obj, cr + i, p);
  387. kfree(cr);
  388. return p - buf;
  389. }
  390. EXPORT_SYMBOL_GPL(omap_dump_tlb_entries);
  391. int omap_foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
  392. {
  393. return driver_for_each_device(&omap_iommu_driver.driver,
  394. NULL, data, fn);
  395. }
  396. EXPORT_SYMBOL_GPL(omap_foreach_iommu_device);
  397. #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
  398. /*
  399. * H/W pagetable operations
  400. */
  401. static void flush_iopgd_range(u32 *first, u32 *last)
  402. {
  403. /* FIXME: L2 cache should be taken care of if it exists */
  404. do {
  405. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
  406. : : "r" (first));
  407. first += L1_CACHE_BYTES / sizeof(*first);
  408. } while (first <= last);
  409. }
  410. static void flush_iopte_range(u32 *first, u32 *last)
  411. {
  412. /* FIXME: L2 cache should be taken care of if it exists */
  413. do {
  414. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
  415. : : "r" (first));
  416. first += L1_CACHE_BYTES / sizeof(*first);
  417. } while (first <= last);
  418. }
  419. static void iopte_free(u32 *iopte)
  420. {
  421. /* Note: freed iopte's must be clean ready for re-use */
  422. kmem_cache_free(iopte_cachep, iopte);
  423. }
  424. static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
  425. {
  426. u32 *iopte;
  427. /* a table has already existed */
  428. if (*iopgd)
  429. goto pte_ready;
  430. /*
  431. * do the allocation outside the page table lock
  432. */
  433. spin_unlock(&obj->page_table_lock);
  434. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  435. spin_lock(&obj->page_table_lock);
  436. if (!*iopgd) {
  437. if (!iopte)
  438. return ERR_PTR(-ENOMEM);
  439. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  440. flush_iopgd_range(iopgd, iopgd);
  441. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  442. } else {
  443. /* We raced, free the reduniovant table */
  444. iopte_free(iopte);
  445. }
  446. pte_ready:
  447. iopte = iopte_offset(iopgd, da);
  448. dev_vdbg(obj->dev,
  449. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  450. __func__, da, iopgd, *iopgd, iopte, *iopte);
  451. return iopte;
  452. }
  453. static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  454. {
  455. u32 *iopgd = iopgd_offset(obj, da);
  456. if ((da | pa) & ~IOSECTION_MASK) {
  457. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  458. __func__, da, pa, IOSECTION_SIZE);
  459. return -EINVAL;
  460. }
  461. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  462. flush_iopgd_range(iopgd, iopgd);
  463. return 0;
  464. }
  465. static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  466. {
  467. u32 *iopgd = iopgd_offset(obj, da);
  468. int i;
  469. if ((da | pa) & ~IOSUPER_MASK) {
  470. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  471. __func__, da, pa, IOSUPER_SIZE);
  472. return -EINVAL;
  473. }
  474. for (i = 0; i < 16; i++)
  475. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  476. flush_iopgd_range(iopgd, iopgd + 15);
  477. return 0;
  478. }
  479. static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  480. {
  481. u32 *iopgd = iopgd_offset(obj, da);
  482. u32 *iopte = iopte_alloc(obj, iopgd, da);
  483. if (IS_ERR(iopte))
  484. return PTR_ERR(iopte);
  485. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  486. flush_iopte_range(iopte, iopte);
  487. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  488. __func__, da, pa, iopte, *iopte);
  489. return 0;
  490. }
  491. static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot)
  492. {
  493. u32 *iopgd = iopgd_offset(obj, da);
  494. u32 *iopte = iopte_alloc(obj, iopgd, da);
  495. int i;
  496. if ((da | pa) & ~IOLARGE_MASK) {
  497. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  498. __func__, da, pa, IOLARGE_SIZE);
  499. return -EINVAL;
  500. }
  501. if (IS_ERR(iopte))
  502. return PTR_ERR(iopte);
  503. for (i = 0; i < 16; i++)
  504. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  505. flush_iopte_range(iopte, iopte + 15);
  506. return 0;
  507. }
  508. static int
  509. iopgtable_store_entry_core(struct omap_iommu *obj, struct iotlb_entry *e)
  510. {
  511. int (*fn)(struct omap_iommu *, u32, u32, u32);
  512. u32 prot;
  513. int err;
  514. if (!obj || !e)
  515. return -EINVAL;
  516. switch (e->pgsz) {
  517. case MMU_CAM_PGSZ_16M:
  518. fn = iopgd_alloc_super;
  519. break;
  520. case MMU_CAM_PGSZ_1M:
  521. fn = iopgd_alloc_section;
  522. break;
  523. case MMU_CAM_PGSZ_64K:
  524. fn = iopte_alloc_large;
  525. break;
  526. case MMU_CAM_PGSZ_4K:
  527. fn = iopte_alloc_page;
  528. break;
  529. default:
  530. fn = NULL;
  531. BUG();
  532. break;
  533. }
  534. prot = get_iopte_attr(e);
  535. spin_lock(&obj->page_table_lock);
  536. err = fn(obj, e->da, e->pa, prot);
  537. spin_unlock(&obj->page_table_lock);
  538. return err;
  539. }
  540. /**
  541. * omap_iopgtable_store_entry - Make an iommu pte entry
  542. * @obj: target iommu
  543. * @e: an iommu tlb entry info
  544. **/
  545. int omap_iopgtable_store_entry(struct omap_iommu *obj, struct iotlb_entry *e)
  546. {
  547. int err;
  548. flush_iotlb_page(obj, e->da);
  549. err = iopgtable_store_entry_core(obj, e);
  550. if (!err)
  551. prefetch_iotlb_entry(obj, e);
  552. return err;
  553. }
  554. EXPORT_SYMBOL_GPL(omap_iopgtable_store_entry);
  555. /**
  556. * iopgtable_lookup_entry - Lookup an iommu pte entry
  557. * @obj: target iommu
  558. * @da: iommu device virtual address
  559. * @ppgd: iommu pgd entry pointer to be returned
  560. * @ppte: iommu pte entry pointer to be returned
  561. **/
  562. static void
  563. iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  564. {
  565. u32 *iopgd, *iopte = NULL;
  566. iopgd = iopgd_offset(obj, da);
  567. if (!*iopgd)
  568. goto out;
  569. if (iopgd_is_table(*iopgd))
  570. iopte = iopte_offset(iopgd, da);
  571. out:
  572. *ppgd = iopgd;
  573. *ppte = iopte;
  574. }
  575. static size_t iopgtable_clear_entry_core(struct omap_iommu *obj, u32 da)
  576. {
  577. size_t bytes;
  578. u32 *iopgd = iopgd_offset(obj, da);
  579. int nent = 1;
  580. if (!*iopgd)
  581. return 0;
  582. if (iopgd_is_table(*iopgd)) {
  583. int i;
  584. u32 *iopte = iopte_offset(iopgd, da);
  585. bytes = IOPTE_SIZE;
  586. if (*iopte & IOPTE_LARGE) {
  587. nent *= 16;
  588. /* rewind to the 1st entry */
  589. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  590. }
  591. bytes *= nent;
  592. memset(iopte, 0, nent * sizeof(*iopte));
  593. flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
  594. /*
  595. * do table walk to check if this table is necessary or not
  596. */
  597. iopte = iopte_offset(iopgd, 0);
  598. for (i = 0; i < PTRS_PER_IOPTE; i++)
  599. if (iopte[i])
  600. goto out;
  601. iopte_free(iopte);
  602. nent = 1; /* for the next L1 entry */
  603. } else {
  604. bytes = IOPGD_SIZE;
  605. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  606. nent *= 16;
  607. /* rewind to the 1st entry */
  608. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  609. }
  610. bytes *= nent;
  611. }
  612. memset(iopgd, 0, nent * sizeof(*iopgd));
  613. flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
  614. out:
  615. return bytes;
  616. }
  617. /**
  618. * iopgtable_clear_entry - Remove an iommu pte entry
  619. * @obj: target iommu
  620. * @da: iommu device virtual address
  621. **/
  622. static size_t iopgtable_clear_entry(struct omap_iommu *obj, u32 da)
  623. {
  624. size_t bytes;
  625. spin_lock(&obj->page_table_lock);
  626. bytes = iopgtable_clear_entry_core(obj, da);
  627. flush_iotlb_page(obj, da);
  628. spin_unlock(&obj->page_table_lock);
  629. return bytes;
  630. }
  631. static void iopgtable_clear_entry_all(struct omap_iommu *obj)
  632. {
  633. int i;
  634. spin_lock(&obj->page_table_lock);
  635. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  636. u32 da;
  637. u32 *iopgd;
  638. da = i << IOPGD_SHIFT;
  639. iopgd = iopgd_offset(obj, da);
  640. if (!*iopgd)
  641. continue;
  642. if (iopgd_is_table(*iopgd))
  643. iopte_free(iopte_offset(iopgd, 0));
  644. *iopgd = 0;
  645. flush_iopgd_range(iopgd, iopgd);
  646. }
  647. flush_iotlb_all(obj);
  648. spin_unlock(&obj->page_table_lock);
  649. }
  650. /*
  651. * Device IOMMU generic operations
  652. */
  653. static irqreturn_t iommu_fault_handler(int irq, void *data)
  654. {
  655. u32 da, errs;
  656. u32 *iopgd, *iopte;
  657. struct omap_iommu *obj = data;
  658. struct iommu_domain *domain = obj->domain;
  659. if (!obj->refcount)
  660. return IRQ_NONE;
  661. errs = iommu_report_fault(obj, &da);
  662. if (errs == 0)
  663. return IRQ_HANDLED;
  664. /* Fault callback or TLB/PTE Dynamic loading */
  665. if (!report_iommu_fault(domain, obj->dev, da, 0))
  666. return IRQ_HANDLED;
  667. iommu_disable(obj);
  668. iopgd = iopgd_offset(obj, da);
  669. if (!iopgd_is_table(*iopgd)) {
  670. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
  671. "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
  672. return IRQ_NONE;
  673. }
  674. iopte = iopte_offset(iopgd, da);
  675. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
  676. "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
  677. iopte, *iopte);
  678. return IRQ_NONE;
  679. }
  680. static int device_match_by_alias(struct device *dev, void *data)
  681. {
  682. struct omap_iommu *obj = to_iommu(dev);
  683. const char *name = data;
  684. pr_debug("%s: %s %s\n", __func__, obj->name, name);
  685. return strcmp(obj->name, name) == 0;
  686. }
  687. /**
  688. * omap_iommu_attach() - attach iommu device to an iommu domain
  689. * @name: name of target omap iommu device
  690. * @iopgd: page table
  691. **/
  692. static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
  693. {
  694. int err = -ENOMEM;
  695. struct device *dev;
  696. struct omap_iommu *obj;
  697. dev = driver_find_device(&omap_iommu_driver.driver, NULL,
  698. (void *)name,
  699. device_match_by_alias);
  700. if (!dev)
  701. return NULL;
  702. obj = to_iommu(dev);
  703. spin_lock(&obj->iommu_lock);
  704. /* an iommu device can only be attached once */
  705. if (++obj->refcount > 1) {
  706. dev_err(dev, "%s: already attached!\n", obj->name);
  707. err = -EBUSY;
  708. goto err_enable;
  709. }
  710. obj->iopgd = iopgd;
  711. err = iommu_enable(obj);
  712. if (err)
  713. goto err_enable;
  714. flush_iotlb_all(obj);
  715. if (!try_module_get(obj->owner))
  716. goto err_module;
  717. spin_unlock(&obj->iommu_lock);
  718. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  719. return obj;
  720. err_module:
  721. if (obj->refcount == 1)
  722. iommu_disable(obj);
  723. err_enable:
  724. obj->refcount--;
  725. spin_unlock(&obj->iommu_lock);
  726. return ERR_PTR(err);
  727. }
  728. /**
  729. * omap_iommu_detach - release iommu device
  730. * @obj: target iommu
  731. **/
  732. static void omap_iommu_detach(struct omap_iommu *obj)
  733. {
  734. if (!obj || IS_ERR(obj))
  735. return;
  736. spin_lock(&obj->iommu_lock);
  737. if (--obj->refcount == 0)
  738. iommu_disable(obj);
  739. module_put(obj->owner);
  740. obj->iopgd = NULL;
  741. spin_unlock(&obj->iommu_lock);
  742. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  743. }
  744. /*
  745. * OMAP Device MMU(IOMMU) detection
  746. */
  747. static int omap_iommu_probe(struct platform_device *pdev)
  748. {
  749. int err = -ENODEV;
  750. int irq;
  751. struct omap_iommu *obj;
  752. struct resource *res;
  753. struct iommu_platform_data *pdata = pdev->dev.platform_data;
  754. obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  755. if (!obj)
  756. return -ENOMEM;
  757. obj->nr_tlb_entries = pdata->nr_tlb_entries;
  758. obj->name = pdata->name;
  759. obj->dev = &pdev->dev;
  760. obj->ctx = (void *)obj + sizeof(*obj);
  761. obj->da_start = pdata->da_start;
  762. obj->da_end = pdata->da_end;
  763. spin_lock_init(&obj->iommu_lock);
  764. mutex_init(&obj->mmap_lock);
  765. spin_lock_init(&obj->page_table_lock);
  766. INIT_LIST_HEAD(&obj->mmap);
  767. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  768. if (!res) {
  769. err = -ENODEV;
  770. goto err_mem;
  771. }
  772. res = request_mem_region(res->start, resource_size(res),
  773. dev_name(&pdev->dev));
  774. if (!res) {
  775. err = -EIO;
  776. goto err_mem;
  777. }
  778. obj->regbase = ioremap(res->start, resource_size(res));
  779. if (!obj->regbase) {
  780. err = -ENOMEM;
  781. goto err_ioremap;
  782. }
  783. irq = platform_get_irq(pdev, 0);
  784. if (irq < 0) {
  785. err = -ENODEV;
  786. goto err_irq;
  787. }
  788. err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
  789. dev_name(&pdev->dev), obj);
  790. if (err < 0)
  791. goto err_irq;
  792. platform_set_drvdata(pdev, obj);
  793. pm_runtime_irq_safe(obj->dev);
  794. pm_runtime_enable(obj->dev);
  795. dev_info(&pdev->dev, "%s registered\n", obj->name);
  796. return 0;
  797. err_irq:
  798. iounmap(obj->regbase);
  799. err_ioremap:
  800. release_mem_region(res->start, resource_size(res));
  801. err_mem:
  802. kfree(obj);
  803. return err;
  804. }
  805. static int omap_iommu_remove(struct platform_device *pdev)
  806. {
  807. int irq;
  808. struct resource *res;
  809. struct omap_iommu *obj = platform_get_drvdata(pdev);
  810. platform_set_drvdata(pdev, NULL);
  811. iopgtable_clear_entry_all(obj);
  812. irq = platform_get_irq(pdev, 0);
  813. free_irq(irq, obj);
  814. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  815. release_mem_region(res->start, resource_size(res));
  816. iounmap(obj->regbase);
  817. pm_runtime_disable(obj->dev);
  818. dev_info(&pdev->dev, "%s removed\n", obj->name);
  819. kfree(obj);
  820. return 0;
  821. }
  822. static struct platform_driver omap_iommu_driver = {
  823. .probe = omap_iommu_probe,
  824. .remove = omap_iommu_remove,
  825. .driver = {
  826. .name = "omap-iommu",
  827. },
  828. };
  829. static void iopte_cachep_ctor(void *iopte)
  830. {
  831. clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
  832. }
  833. static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa,
  834. u32 flags)
  835. {
  836. memset(e, 0, sizeof(*e));
  837. e->da = da;
  838. e->pa = pa;
  839. e->valid = 1;
  840. /* FIXME: add OMAP1 support */
  841. e->pgsz = flags & MMU_CAM_PGSZ_MASK;
  842. e->endian = flags & MMU_RAM_ENDIAN_MASK;
  843. e->elsz = flags & MMU_RAM_ELSZ_MASK;
  844. e->mixed = flags & MMU_RAM_MIXED_MASK;
  845. return iopgsz_to_bytes(e->pgsz);
  846. }
  847. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  848. phys_addr_t pa, size_t bytes, int prot)
  849. {
  850. struct omap_iommu_domain *omap_domain = domain->priv;
  851. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  852. struct device *dev = oiommu->dev;
  853. struct iotlb_entry e;
  854. int omap_pgsz;
  855. u32 ret, flags;
  856. /* we only support mapping a single iommu page for now */
  857. omap_pgsz = bytes_to_iopgsz(bytes);
  858. if (omap_pgsz < 0) {
  859. dev_err(dev, "invalid size to map: %d\n", bytes);
  860. return -EINVAL;
  861. }
  862. dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
  863. flags = omap_pgsz | prot;
  864. iotlb_init_entry(&e, da, pa, flags);
  865. ret = omap_iopgtable_store_entry(oiommu, &e);
  866. if (ret)
  867. dev_err(dev, "omap_iopgtable_store_entry failed: %d\n", ret);
  868. return ret;
  869. }
  870. static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  871. size_t size)
  872. {
  873. struct omap_iommu_domain *omap_domain = domain->priv;
  874. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  875. struct device *dev = oiommu->dev;
  876. dev_dbg(dev, "unmapping da 0x%lx size %u\n", da, size);
  877. return iopgtable_clear_entry(oiommu, da);
  878. }
  879. static int
  880. omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  881. {
  882. struct omap_iommu_domain *omap_domain = domain->priv;
  883. struct omap_iommu *oiommu;
  884. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  885. int ret = 0;
  886. spin_lock(&omap_domain->lock);
  887. /* only a single device is supported per domain for now */
  888. if (omap_domain->iommu_dev) {
  889. dev_err(dev, "iommu domain is already attached\n");
  890. ret = -EBUSY;
  891. goto out;
  892. }
  893. /* get a handle to and enable the omap iommu */
  894. oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
  895. if (IS_ERR(oiommu)) {
  896. ret = PTR_ERR(oiommu);
  897. dev_err(dev, "can't get omap iommu: %d\n", ret);
  898. goto out;
  899. }
  900. omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
  901. omap_domain->dev = dev;
  902. oiommu->domain = domain;
  903. out:
  904. spin_unlock(&omap_domain->lock);
  905. return ret;
  906. }
  907. static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
  908. struct device *dev)
  909. {
  910. struct omap_iommu *oiommu = dev_to_omap_iommu(dev);
  911. struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
  912. /* only a single device is supported per domain for now */
  913. if (omap_domain->iommu_dev != oiommu) {
  914. dev_err(dev, "invalid iommu device\n");
  915. return;
  916. }
  917. iopgtable_clear_entry_all(oiommu);
  918. omap_iommu_detach(oiommu);
  919. omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
  920. omap_domain->dev = NULL;
  921. }
  922. static void omap_iommu_detach_dev(struct iommu_domain *domain,
  923. struct device *dev)
  924. {
  925. struct omap_iommu_domain *omap_domain = domain->priv;
  926. spin_lock(&omap_domain->lock);
  927. _omap_iommu_detach_dev(omap_domain, dev);
  928. spin_unlock(&omap_domain->lock);
  929. }
  930. static int omap_iommu_domain_init(struct iommu_domain *domain)
  931. {
  932. struct omap_iommu_domain *omap_domain;
  933. omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
  934. if (!omap_domain) {
  935. pr_err("kzalloc failed\n");
  936. goto out;
  937. }
  938. omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
  939. if (!omap_domain->pgtable) {
  940. pr_err("kzalloc failed\n");
  941. goto fail_nomem;
  942. }
  943. /*
  944. * should never fail, but please keep this around to ensure
  945. * we keep the hardware happy
  946. */
  947. BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
  948. clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
  949. spin_lock_init(&omap_domain->lock);
  950. domain->priv = omap_domain;
  951. domain->geometry.aperture_start = 0;
  952. domain->geometry.aperture_end = (1ULL << 32) - 1;
  953. domain->geometry.force_aperture = true;
  954. return 0;
  955. fail_nomem:
  956. kfree(omap_domain);
  957. out:
  958. return -ENOMEM;
  959. }
  960. static void omap_iommu_domain_destroy(struct iommu_domain *domain)
  961. {
  962. struct omap_iommu_domain *omap_domain = domain->priv;
  963. domain->priv = NULL;
  964. /*
  965. * An iommu device is still attached
  966. * (currently, only one device can be attached) ?
  967. */
  968. if (omap_domain->iommu_dev)
  969. _omap_iommu_detach_dev(omap_domain, omap_domain->dev);
  970. kfree(omap_domain->pgtable);
  971. kfree(omap_domain);
  972. }
  973. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  974. unsigned long da)
  975. {
  976. struct omap_iommu_domain *omap_domain = domain->priv;
  977. struct omap_iommu *oiommu = omap_domain->iommu_dev;
  978. struct device *dev = oiommu->dev;
  979. u32 *pgd, *pte;
  980. phys_addr_t ret = 0;
  981. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  982. if (pte) {
  983. if (iopte_is_small(*pte))
  984. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  985. else if (iopte_is_large(*pte))
  986. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  987. else
  988. dev_err(dev, "bogus pte 0x%x, da 0x%lx", *pte, da);
  989. } else {
  990. if (iopgd_is_section(*pgd))
  991. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  992. else if (iopgd_is_super(*pgd))
  993. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  994. else
  995. dev_err(dev, "bogus pgd 0x%x, da 0x%lx", *pgd, da);
  996. }
  997. return ret;
  998. }
  999. static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
  1000. unsigned long cap)
  1001. {
  1002. return 0;
  1003. }
  1004. static struct iommu_ops omap_iommu_ops = {
  1005. .domain_init = omap_iommu_domain_init,
  1006. .domain_destroy = omap_iommu_domain_destroy,
  1007. .attach_dev = omap_iommu_attach_dev,
  1008. .detach_dev = omap_iommu_detach_dev,
  1009. .map = omap_iommu_map,
  1010. .unmap = omap_iommu_unmap,
  1011. .iova_to_phys = omap_iommu_iova_to_phys,
  1012. .domain_has_cap = omap_iommu_domain_has_cap,
  1013. .pgsize_bitmap = OMAP_IOMMU_PGSIZES,
  1014. };
  1015. static int __init omap_iommu_init(void)
  1016. {
  1017. struct kmem_cache *p;
  1018. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  1019. size_t align = 1 << 10; /* L2 pagetable alignement */
  1020. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  1021. iopte_cachep_ctor);
  1022. if (!p)
  1023. return -ENOMEM;
  1024. iopte_cachep = p;
  1025. bus_set_iommu(&platform_bus_type, &omap_iommu_ops);
  1026. return platform_driver_register(&omap_iommu_driver);
  1027. }
  1028. /* must be ready before omap3isp is probed */
  1029. subsys_initcall(omap_iommu_init);
  1030. static void __exit omap_iommu_exit(void)
  1031. {
  1032. kmem_cache_destroy(iopte_cachep);
  1033. platform_driver_unregister(&omap_iommu_driver);
  1034. }
  1035. module_exit(omap_iommu_exit);
  1036. MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
  1037. MODULE_ALIAS("platform:omap-iommu");
  1038. MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
  1039. MODULE_LICENSE("GPL v2");