omap-iommu.c 28 KB

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