omap-iommu.c 27 KB

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