omap-iommu.c 27 KB

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