omap-iommu.c 27 KB

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