omap-iommu.c 27 KB

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