omap-iommu.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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 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. * 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 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(install_iommu_arch);
  61. /**
  62. * 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 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(uninstall_iommu_arch);
  74. /**
  75. * iommu_save_ctx - Save registers for pm off-mode support
  76. * @obj: target iommu
  77. **/
  78. void iommu_save_ctx(struct iommu *obj)
  79. {
  80. arch_iommu->save_ctx(obj);
  81. }
  82. EXPORT_SYMBOL_GPL(iommu_save_ctx);
  83. /**
  84. * iommu_restore_ctx - Restore registers for pm off-mode support
  85. * @obj: target iommu
  86. **/
  87. void iommu_restore_ctx(struct iommu *obj)
  88. {
  89. arch_iommu->restore_ctx(obj);
  90. }
  91. EXPORT_SYMBOL_GPL(iommu_restore_ctx);
  92. /**
  93. * iommu_arch_version - Return running iommu arch version
  94. **/
  95. u32 iommu_arch_version(void)
  96. {
  97. return arch_iommu->version;
  98. }
  99. EXPORT_SYMBOL_GPL(iommu_arch_version);
  100. static int iommu_enable(struct 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 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 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(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 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 iommu *obj, u32 *da)
  151. {
  152. return arch_iommu->fault_isr(obj, da);
  153. }
  154. static void iotlb_lock_get(struct 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 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 iommu *obj, struct cr_regs *cr)
  169. {
  170. arch_iommu->tlb_read_cr(obj, cr);
  171. }
  172. static void iotlb_load_cr(struct 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 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 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 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 iommu *obj, struct iotlb_entry *e)
  256. {
  257. return 0;
  258. }
  259. #endif /* !PREFETCH_IOTLB */
  260. static int prefetch_iotlb_entry(struct 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 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_range - Clear an iommu tlb entries
  296. * @obj: target iommu
  297. * @start: iommu device virtual address(start)
  298. * @end: iommu device virtual address(end)
  299. *
  300. * Clear an iommu tlb entry which includes 'da' address.
  301. **/
  302. void flush_iotlb_range(struct iommu *obj, u32 start, u32 end)
  303. {
  304. u32 da = start;
  305. while (da < end) {
  306. flush_iotlb_page(obj, da);
  307. /* FIXME: Optimize for multiple page size */
  308. da += IOPTE_SIZE;
  309. }
  310. }
  311. EXPORT_SYMBOL_GPL(flush_iotlb_range);
  312. /**
  313. * flush_iotlb_all - Clear all iommu tlb entries
  314. * @obj: target iommu
  315. **/
  316. static void flush_iotlb_all(struct iommu *obj)
  317. {
  318. struct iotlb_lock l;
  319. clk_enable(obj->clk);
  320. l.base = 0;
  321. l.vict = 0;
  322. iotlb_lock_set(obj, &l);
  323. iommu_write_reg(obj, 1, MMU_GFLUSH);
  324. clk_disable(obj->clk);
  325. }
  326. /**
  327. * iommu_set_twl - enable/disable table walking logic
  328. * @obj: target iommu
  329. * @on: enable/disable
  330. *
  331. * Function used to enable/disable TWL. If one wants to work
  332. * exclusively with locked TLB entries and receive notifications
  333. * for TLB miss then call this function to disable TWL.
  334. */
  335. void iommu_set_twl(struct iommu *obj, bool on)
  336. {
  337. clk_enable(obj->clk);
  338. arch_iommu->set_twl(obj, on);
  339. clk_disable(obj->clk);
  340. }
  341. EXPORT_SYMBOL_GPL(iommu_set_twl);
  342. #if defined(CONFIG_OMAP_IOMMU_DEBUG_MODULE)
  343. ssize_t iommu_dump_ctx(struct iommu *obj, char *buf, ssize_t bytes)
  344. {
  345. if (!obj || !buf)
  346. return -EINVAL;
  347. clk_enable(obj->clk);
  348. bytes = arch_iommu->dump_ctx(obj, buf, bytes);
  349. clk_disable(obj->clk);
  350. return bytes;
  351. }
  352. EXPORT_SYMBOL_GPL(iommu_dump_ctx);
  353. static int __dump_tlb_entries(struct iommu *obj, struct cr_regs *crs, int num)
  354. {
  355. int i;
  356. struct iotlb_lock saved;
  357. struct cr_regs tmp;
  358. struct cr_regs *p = crs;
  359. clk_enable(obj->clk);
  360. iotlb_lock_get(obj, &saved);
  361. for_each_iotlb_cr(obj, num, i, tmp) {
  362. if (!iotlb_cr_valid(&tmp))
  363. continue;
  364. *p++ = tmp;
  365. }
  366. iotlb_lock_set(obj, &saved);
  367. clk_disable(obj->clk);
  368. return p - crs;
  369. }
  370. /**
  371. * dump_tlb_entries - dump cr arrays to given buffer
  372. * @obj: target iommu
  373. * @buf: output buffer
  374. **/
  375. size_t dump_tlb_entries(struct iommu *obj, char *buf, ssize_t bytes)
  376. {
  377. int i, num;
  378. struct cr_regs *cr;
  379. char *p = buf;
  380. num = bytes / sizeof(*cr);
  381. num = min(obj->nr_tlb_entries, num);
  382. cr = kcalloc(num, sizeof(*cr), GFP_KERNEL);
  383. if (!cr)
  384. return 0;
  385. num = __dump_tlb_entries(obj, cr, num);
  386. for (i = 0; i < num; i++)
  387. p += iotlb_dump_cr(obj, cr + i, p);
  388. kfree(cr);
  389. return p - buf;
  390. }
  391. EXPORT_SYMBOL_GPL(dump_tlb_entries);
  392. int foreach_iommu_device(void *data, int (*fn)(struct device *, void *))
  393. {
  394. return driver_for_each_device(&omap_iommu_driver.driver,
  395. NULL, data, fn);
  396. }
  397. EXPORT_SYMBOL_GPL(foreach_iommu_device);
  398. #endif /* CONFIG_OMAP_IOMMU_DEBUG_MODULE */
  399. /*
  400. * H/W pagetable operations
  401. */
  402. static void flush_iopgd_range(u32 *first, u32 *last)
  403. {
  404. /* FIXME: L2 cache should be taken care of if it exists */
  405. do {
  406. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd"
  407. : : "r" (first));
  408. first += L1_CACHE_BYTES / sizeof(*first);
  409. } while (first <= last);
  410. }
  411. static void flush_iopte_range(u32 *first, u32 *last)
  412. {
  413. /* FIXME: L2 cache should be taken care of if it exists */
  414. do {
  415. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte"
  416. : : "r" (first));
  417. first += L1_CACHE_BYTES / sizeof(*first);
  418. } while (first <= last);
  419. }
  420. static void iopte_free(u32 *iopte)
  421. {
  422. /* Note: freed iopte's must be clean ready for re-use */
  423. kmem_cache_free(iopte_cachep, iopte);
  424. }
  425. static u32 *iopte_alloc(struct iommu *obj, u32 *iopgd, u32 da)
  426. {
  427. u32 *iopte;
  428. /* a table has already existed */
  429. if (*iopgd)
  430. goto pte_ready;
  431. /*
  432. * do the allocation outside the page table lock
  433. */
  434. spin_unlock(&obj->page_table_lock);
  435. iopte = kmem_cache_zalloc(iopte_cachep, GFP_KERNEL);
  436. spin_lock(&obj->page_table_lock);
  437. if (!*iopgd) {
  438. if (!iopte)
  439. return ERR_PTR(-ENOMEM);
  440. *iopgd = virt_to_phys(iopte) | IOPGD_TABLE;
  441. flush_iopgd_range(iopgd, iopgd);
  442. dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
  443. } else {
  444. /* We raced, free the reduniovant table */
  445. iopte_free(iopte);
  446. }
  447. pte_ready:
  448. iopte = iopte_offset(iopgd, da);
  449. dev_vdbg(obj->dev,
  450. "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n",
  451. __func__, da, iopgd, *iopgd, iopte, *iopte);
  452. return iopte;
  453. }
  454. static int iopgd_alloc_section(struct iommu *obj, u32 da, u32 pa, u32 prot)
  455. {
  456. u32 *iopgd = iopgd_offset(obj, da);
  457. if ((da | pa) & ~IOSECTION_MASK) {
  458. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  459. __func__, da, pa, IOSECTION_SIZE);
  460. return -EINVAL;
  461. }
  462. *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION;
  463. flush_iopgd_range(iopgd, iopgd);
  464. return 0;
  465. }
  466. static int iopgd_alloc_super(struct iommu *obj, u32 da, u32 pa, u32 prot)
  467. {
  468. u32 *iopgd = iopgd_offset(obj, da);
  469. int i;
  470. if ((da | pa) & ~IOSUPER_MASK) {
  471. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  472. __func__, da, pa, IOSUPER_SIZE);
  473. return -EINVAL;
  474. }
  475. for (i = 0; i < 16; i++)
  476. *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER;
  477. flush_iopgd_range(iopgd, iopgd + 15);
  478. return 0;
  479. }
  480. static int iopte_alloc_page(struct iommu *obj, u32 da, u32 pa, u32 prot)
  481. {
  482. u32 *iopgd = iopgd_offset(obj, da);
  483. u32 *iopte = iopte_alloc(obj, iopgd, da);
  484. if (IS_ERR(iopte))
  485. return PTR_ERR(iopte);
  486. *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL;
  487. flush_iopte_range(iopte, iopte);
  488. dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n",
  489. __func__, da, pa, iopte, *iopte);
  490. return 0;
  491. }
  492. static int iopte_alloc_large(struct iommu *obj, u32 da, u32 pa, u32 prot)
  493. {
  494. u32 *iopgd = iopgd_offset(obj, da);
  495. u32 *iopte = iopte_alloc(obj, iopgd, da);
  496. int i;
  497. if ((da | pa) & ~IOLARGE_MASK) {
  498. dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n",
  499. __func__, da, pa, IOLARGE_SIZE);
  500. return -EINVAL;
  501. }
  502. if (IS_ERR(iopte))
  503. return PTR_ERR(iopte);
  504. for (i = 0; i < 16; i++)
  505. *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE;
  506. flush_iopte_range(iopte, iopte + 15);
  507. return 0;
  508. }
  509. static int iopgtable_store_entry_core(struct iommu *obj, struct iotlb_entry *e)
  510. {
  511. int (*fn)(struct iommu *, u32, u32, u32);
  512. u32 prot;
  513. int err;
  514. if (!obj || !e)
  515. return -EINVAL;
  516. switch (e->pgsz) {
  517. case MMU_CAM_PGSZ_16M:
  518. fn = iopgd_alloc_super;
  519. break;
  520. case MMU_CAM_PGSZ_1M:
  521. fn = iopgd_alloc_section;
  522. break;
  523. case MMU_CAM_PGSZ_64K:
  524. fn = iopte_alloc_large;
  525. break;
  526. case MMU_CAM_PGSZ_4K:
  527. fn = iopte_alloc_page;
  528. break;
  529. default:
  530. fn = NULL;
  531. BUG();
  532. break;
  533. }
  534. prot = get_iopte_attr(e);
  535. spin_lock(&obj->page_table_lock);
  536. err = fn(obj, e->da, e->pa, prot);
  537. spin_unlock(&obj->page_table_lock);
  538. return err;
  539. }
  540. /**
  541. * iopgtable_store_entry - Make an iommu pte entry
  542. * @obj: target iommu
  543. * @e: an iommu tlb entry info
  544. **/
  545. int iopgtable_store_entry(struct iommu *obj, struct iotlb_entry *e)
  546. {
  547. int err;
  548. flush_iotlb_page(obj, e->da);
  549. err = iopgtable_store_entry_core(obj, e);
  550. if (!err)
  551. prefetch_iotlb_entry(obj, e);
  552. return err;
  553. }
  554. EXPORT_SYMBOL_GPL(iopgtable_store_entry);
  555. /**
  556. * iopgtable_lookup_entry - Lookup an iommu pte entry
  557. * @obj: target iommu
  558. * @da: iommu device virtual address
  559. * @ppgd: iommu pgd entry pointer to be returned
  560. * @ppte: iommu pte entry pointer to be returned
  561. **/
  562. static void
  563. iopgtable_lookup_entry(struct omap_iommu *obj, u32 da, u32 **ppgd, u32 **ppte)
  564. {
  565. u32 *iopgd, *iopte = NULL;
  566. iopgd = iopgd_offset(obj, da);
  567. if (!*iopgd)
  568. goto out;
  569. if (iopgd_is_table(*iopgd))
  570. iopte = iopte_offset(iopgd, da);
  571. out:
  572. *ppgd = iopgd;
  573. *ppte = iopte;
  574. }
  575. static size_t iopgtable_clear_entry_core(struct iommu *obj, u32 da)
  576. {
  577. size_t bytes;
  578. u32 *iopgd = iopgd_offset(obj, da);
  579. int nent = 1;
  580. if (!*iopgd)
  581. return 0;
  582. if (iopgd_is_table(*iopgd)) {
  583. int i;
  584. u32 *iopte = iopte_offset(iopgd, da);
  585. bytes = IOPTE_SIZE;
  586. if (*iopte & IOPTE_LARGE) {
  587. nent *= 16;
  588. /* rewind to the 1st entry */
  589. iopte = iopte_offset(iopgd, (da & IOLARGE_MASK));
  590. }
  591. bytes *= nent;
  592. memset(iopte, 0, nent * sizeof(*iopte));
  593. flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte));
  594. /*
  595. * do table walk to check if this table is necessary or not
  596. */
  597. iopte = iopte_offset(iopgd, 0);
  598. for (i = 0; i < PTRS_PER_IOPTE; i++)
  599. if (iopte[i])
  600. goto out;
  601. iopte_free(iopte);
  602. nent = 1; /* for the next L1 entry */
  603. } else {
  604. bytes = IOPGD_SIZE;
  605. if ((*iopgd & IOPGD_SUPER) == IOPGD_SUPER) {
  606. nent *= 16;
  607. /* rewind to the 1st entry */
  608. iopgd = iopgd_offset(obj, (da & IOSUPER_MASK));
  609. }
  610. bytes *= nent;
  611. }
  612. memset(iopgd, 0, nent * sizeof(*iopgd));
  613. flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd));
  614. out:
  615. return bytes;
  616. }
  617. /**
  618. * iopgtable_clear_entry - Remove an iommu pte entry
  619. * @obj: target iommu
  620. * @da: iommu device virtual address
  621. **/
  622. static size_t iopgtable_clear_entry(struct iommu *obj, u32 da)
  623. {
  624. size_t bytes;
  625. spin_lock(&obj->page_table_lock);
  626. bytes = iopgtable_clear_entry_core(obj, da);
  627. flush_iotlb_page(obj, da);
  628. spin_unlock(&obj->page_table_lock);
  629. return bytes;
  630. }
  631. static void iopgtable_clear_entry_all(struct iommu *obj)
  632. {
  633. int i;
  634. spin_lock(&obj->page_table_lock);
  635. for (i = 0; i < PTRS_PER_IOPGD; i++) {
  636. u32 da;
  637. u32 *iopgd;
  638. da = i << IOPGD_SHIFT;
  639. iopgd = iopgd_offset(obj, da);
  640. if (!*iopgd)
  641. continue;
  642. if (iopgd_is_table(*iopgd))
  643. iopte_free(iopte_offset(iopgd, 0));
  644. *iopgd = 0;
  645. flush_iopgd_range(iopgd, iopgd);
  646. }
  647. flush_iotlb_all(obj);
  648. spin_unlock(&obj->page_table_lock);
  649. }
  650. /*
  651. * Device IOMMU generic operations
  652. */
  653. static irqreturn_t iommu_fault_handler(int irq, void *data)
  654. {
  655. u32 da, errs;
  656. u32 *iopgd, *iopte;
  657. struct iommu *obj = data;
  658. if (!obj->refcount)
  659. return IRQ_NONE;
  660. clk_enable(obj->clk);
  661. errs = iommu_report_fault(obj, &da);
  662. clk_disable(obj->clk);
  663. if (errs == 0)
  664. return IRQ_HANDLED;
  665. /* Fault callback or TLB/PTE Dynamic loading */
  666. if (obj->isr && !obj->isr(obj, da, errs, obj->isr_priv))
  667. return IRQ_HANDLED;
  668. iommu_disable(obj);
  669. iopgd = iopgd_offset(obj, da);
  670. if (!iopgd_is_table(*iopgd)) {
  671. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p "
  672. "*pgd:px%08x\n", obj->name, errs, da, iopgd, *iopgd);
  673. return IRQ_NONE;
  674. }
  675. iopte = iopte_offset(iopgd, da);
  676. dev_err(obj->dev, "%s: errs:0x%08x da:0x%08x pgd:0x%p *pgd:0x%08x "
  677. "pte:0x%p *pte:0x%08x\n", obj->name, errs, da, iopgd, *iopgd,
  678. iopte, *iopte);
  679. return IRQ_NONE;
  680. }
  681. static int device_match_by_alias(struct device *dev, void *data)
  682. {
  683. struct iommu *obj = to_iommu(dev);
  684. const char *name = data;
  685. pr_debug("%s: %s %s\n", __func__, obj->name, name);
  686. return strcmp(obj->name, name) == 0;
  687. }
  688. /**
  689. * iommu_set_da_range - Set a valid device address range
  690. * @obj: target iommu
  691. * @start Start of valid range
  692. * @end End of valid range
  693. **/
  694. int iommu_set_da_range(struct iommu *obj, u32 start, u32 end)
  695. {
  696. if (!obj)
  697. return -EFAULT;
  698. if (end < start || !PAGE_ALIGN(start | end))
  699. return -EINVAL;
  700. obj->da_start = start;
  701. obj->da_end = end;
  702. return 0;
  703. }
  704. EXPORT_SYMBOL_GPL(iommu_set_da_range);
  705. /**
  706. * omap_find_iommu_device() - find an omap iommu device by name
  707. * @name: name of the iommu device
  708. *
  709. * The generic iommu API requires the caller to provide the device
  710. * he wishes to attach to a certain iommu domain.
  711. *
  712. * Drivers generally should not bother with this as it should just
  713. * be taken care of by the DMA-API using dev_archdata.
  714. *
  715. * This function is provided as an interim solution until the latter
  716. * materializes, and omap3isp is fully migrated to the DMA-API.
  717. */
  718. struct device *omap_find_iommu_device(const char *name)
  719. {
  720. return driver_find_device(&omap_iommu_driver.driver, NULL,
  721. (void *)name,
  722. device_match_by_alias);
  723. }
  724. EXPORT_SYMBOL_GPL(omap_find_iommu_device);
  725. /**
  726. * omap_iommu_attach() - attach iommu device to an iommu domain
  727. * @dev: target omap iommu device
  728. * @iopgd: page table
  729. **/
  730. static struct iommu *omap_iommu_attach(struct device *dev, u32 *iopgd)
  731. {
  732. int err = -ENOMEM;
  733. struct iommu *obj = to_iommu(dev);
  734. spin_lock(&obj->iommu_lock);
  735. /* an iommu device can only be attached once */
  736. if (++obj->refcount > 1) {
  737. dev_err(dev, "%s: already attached!\n", obj->name);
  738. err = -EBUSY;
  739. goto err_enable;
  740. }
  741. obj->iopgd = iopgd;
  742. err = iommu_enable(obj);
  743. if (err)
  744. goto err_enable;
  745. flush_iotlb_all(obj);
  746. if (!try_module_get(obj->owner))
  747. goto err_module;
  748. spin_unlock(&obj->iommu_lock);
  749. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  750. return obj;
  751. err_module:
  752. if (obj->refcount == 1)
  753. iommu_disable(obj);
  754. err_enable:
  755. obj->refcount--;
  756. spin_unlock(&obj->iommu_lock);
  757. return ERR_PTR(err);
  758. }
  759. /**
  760. * omap_iommu_detach - release iommu device
  761. * @obj: target iommu
  762. **/
  763. static void omap_iommu_detach(struct iommu *obj)
  764. {
  765. if (!obj || IS_ERR(obj))
  766. return;
  767. spin_lock(&obj->iommu_lock);
  768. if (--obj->refcount == 0)
  769. iommu_disable(obj);
  770. module_put(obj->owner);
  771. obj->iopgd = NULL;
  772. spin_unlock(&obj->iommu_lock);
  773. dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
  774. }
  775. int iommu_set_isr(const char *name,
  776. int (*isr)(struct iommu *obj, u32 da, u32 iommu_errs,
  777. void *priv),
  778. void *isr_priv)
  779. {
  780. struct device *dev;
  781. struct iommu *obj;
  782. dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
  783. device_match_by_alias);
  784. if (!dev)
  785. return -ENODEV;
  786. obj = to_iommu(dev);
  787. mutex_lock(&obj->iommu_lock);
  788. if (obj->refcount != 0) {
  789. mutex_unlock(&obj->iommu_lock);
  790. return -EBUSY;
  791. }
  792. obj->isr = isr;
  793. obj->isr_priv = isr_priv;
  794. mutex_unlock(&obj->iommu_lock);
  795. return 0;
  796. }
  797. EXPORT_SYMBOL_GPL(iommu_set_isr);
  798. /*
  799. * OMAP Device MMU(IOMMU) detection
  800. */
  801. static int __devinit omap_iommu_probe(struct platform_device *pdev)
  802. {
  803. int err = -ENODEV;
  804. int irq;
  805. struct iommu *obj;
  806. struct resource *res;
  807. struct iommu_platform_data *pdata = pdev->dev.platform_data;
  808. if (pdev->num_resources != 2)
  809. return -EINVAL;
  810. obj = kzalloc(sizeof(*obj) + MMU_REG_SIZE, GFP_KERNEL);
  811. if (!obj)
  812. return -ENOMEM;
  813. obj->clk = clk_get(&pdev->dev, pdata->clk_name);
  814. if (IS_ERR(obj->clk))
  815. goto err_clk;
  816. obj->nr_tlb_entries = pdata->nr_tlb_entries;
  817. obj->name = pdata->name;
  818. obj->dev = &pdev->dev;
  819. obj->ctx = (void *)obj + sizeof(*obj);
  820. obj->da_start = pdata->da_start;
  821. obj->da_end = pdata->da_end;
  822. spin_lock_init(&obj->iommu_lock);
  823. mutex_init(&obj->mmap_lock);
  824. spin_lock_init(&obj->page_table_lock);
  825. INIT_LIST_HEAD(&obj->mmap);
  826. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  827. if (!res) {
  828. err = -ENODEV;
  829. goto err_mem;
  830. }
  831. res = request_mem_region(res->start, resource_size(res),
  832. dev_name(&pdev->dev));
  833. if (!res) {
  834. err = -EIO;
  835. goto err_mem;
  836. }
  837. obj->regbase = ioremap(res->start, resource_size(res));
  838. if (!obj->regbase) {
  839. err = -ENOMEM;
  840. goto err_ioremap;
  841. }
  842. irq = platform_get_irq(pdev, 0);
  843. if (irq < 0) {
  844. err = -ENODEV;
  845. goto err_irq;
  846. }
  847. err = request_irq(irq, iommu_fault_handler, IRQF_SHARED,
  848. dev_name(&pdev->dev), obj);
  849. if (err < 0)
  850. goto err_irq;
  851. platform_set_drvdata(pdev, obj);
  852. dev_info(&pdev->dev, "%s registered\n", obj->name);
  853. return 0;
  854. err_irq:
  855. iounmap(obj->regbase);
  856. err_ioremap:
  857. release_mem_region(res->start, resource_size(res));
  858. err_mem:
  859. clk_put(obj->clk);
  860. err_clk:
  861. kfree(obj);
  862. return err;
  863. }
  864. static int __devexit omap_iommu_remove(struct platform_device *pdev)
  865. {
  866. int irq;
  867. struct resource *res;
  868. struct iommu *obj = platform_get_drvdata(pdev);
  869. platform_set_drvdata(pdev, NULL);
  870. iopgtable_clear_entry_all(obj);
  871. irq = platform_get_irq(pdev, 0);
  872. free_irq(irq, obj);
  873. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  874. release_mem_region(res->start, resource_size(res));
  875. iounmap(obj->regbase);
  876. clk_put(obj->clk);
  877. dev_info(&pdev->dev, "%s removed\n", obj->name);
  878. kfree(obj);
  879. return 0;
  880. }
  881. static struct platform_driver omap_iommu_driver = {
  882. .probe = omap_iommu_probe,
  883. .remove = __devexit_p(omap_iommu_remove),
  884. .driver = {
  885. .name = "omap-iommu",
  886. },
  887. };
  888. static void iopte_cachep_ctor(void *iopte)
  889. {
  890. clean_dcache_area(iopte, IOPTE_TABLE_SIZE);
  891. }
  892. static int omap_iommu_map(struct iommu_domain *domain, unsigned long da,
  893. phys_addr_t pa, int order, int prot)
  894. {
  895. struct omap_iommu_domain *omap_domain = domain->priv;
  896. struct iommu *oiommu = omap_domain->iommu_dev;
  897. struct device *dev = oiommu->dev;
  898. size_t bytes = PAGE_SIZE << order;
  899. struct iotlb_entry e;
  900. int omap_pgsz;
  901. u32 ret, flags;
  902. /* we only support mapping a single iommu page for now */
  903. omap_pgsz = bytes_to_iopgsz(bytes);
  904. if (omap_pgsz < 0) {
  905. dev_err(dev, "invalid size to map: %d\n", bytes);
  906. return -EINVAL;
  907. }
  908. dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes);
  909. flags = omap_pgsz | prot;
  910. iotlb_init_entry(&e, da, pa, flags);
  911. ret = iopgtable_store_entry(oiommu, &e);
  912. if (ret) {
  913. dev_err(dev, "iopgtable_store_entry failed: %d\n", ret);
  914. return ret;
  915. }
  916. return 0;
  917. }
  918. static int omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
  919. int order)
  920. {
  921. struct omap_iommu_domain *omap_domain = domain->priv;
  922. struct iommu *oiommu = omap_domain->iommu_dev;
  923. struct device *dev = oiommu->dev;
  924. size_t bytes = PAGE_SIZE << order;
  925. size_t ret;
  926. dev_dbg(dev, "unmapping da 0x%lx size 0x%x\n", da, bytes);
  927. ret = iopgtable_clear_entry(oiommu, da);
  928. if (ret != bytes) {
  929. dev_err(dev, "entry @ 0x%lx was %d; not %d\n", da, ret, bytes);
  930. return -EINVAL;
  931. }
  932. return 0;
  933. }
  934. static int
  935. omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
  936. {
  937. struct omap_iommu_domain *omap_domain = domain->priv;
  938. struct iommu *oiommu;
  939. int ret = 0;
  940. spin_lock(&omap_domain->lock);
  941. /* only a single device is supported per domain for now */
  942. if (omap_domain->iommu_dev) {
  943. dev_err(dev, "iommu domain is already attached\n");
  944. ret = -EBUSY;
  945. goto out;
  946. }
  947. /* get a handle to and enable the omap iommu */
  948. oiommu = omap_iommu_attach(dev, omap_domain->pgtable);
  949. if (IS_ERR(oiommu)) {
  950. ret = PTR_ERR(oiommu);
  951. dev_err(dev, "can't get omap iommu: %d\n", ret);
  952. goto out;
  953. }
  954. omap_domain->iommu_dev = oiommu;
  955. out:
  956. spin_unlock(&omap_domain->lock);
  957. return ret;
  958. }
  959. static void omap_iommu_detach_dev(struct iommu_domain *domain,
  960. struct device *dev)
  961. {
  962. struct omap_iommu_domain *omap_domain = domain->priv;
  963. struct iommu *oiommu = to_iommu(dev);
  964. spin_lock(&omap_domain->lock);
  965. /* only a single device is supported per domain for now */
  966. if (omap_domain->iommu_dev != oiommu) {
  967. dev_err(dev, "invalid iommu device\n");
  968. goto out;
  969. }
  970. iopgtable_clear_entry_all(oiommu);
  971. omap_iommu_detach(oiommu);
  972. omap_domain->iommu_dev = NULL;
  973. out:
  974. spin_unlock(&omap_domain->lock);
  975. }
  976. static int omap_iommu_domain_init(struct iommu_domain *domain)
  977. {
  978. struct omap_iommu_domain *omap_domain;
  979. omap_domain = kzalloc(sizeof(*omap_domain), GFP_KERNEL);
  980. if (!omap_domain) {
  981. pr_err("kzalloc failed\n");
  982. goto out;
  983. }
  984. omap_domain->pgtable = kzalloc(IOPGD_TABLE_SIZE, GFP_KERNEL);
  985. if (!omap_domain->pgtable) {
  986. pr_err("kzalloc failed\n");
  987. goto fail_nomem;
  988. }
  989. /*
  990. * should never fail, but please keep this around to ensure
  991. * we keep the hardware happy
  992. */
  993. BUG_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE));
  994. clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE);
  995. spin_lock_init(&omap_domain->lock);
  996. domain->priv = omap_domain;
  997. return 0;
  998. fail_nomem:
  999. kfree(omap_domain);
  1000. out:
  1001. return -ENOMEM;
  1002. }
  1003. /* assume device was already detached */
  1004. static void omap_iommu_domain_destroy(struct iommu_domain *domain)
  1005. {
  1006. struct omap_iommu_domain *omap_domain = domain->priv;
  1007. domain->priv = NULL;
  1008. kfree(omap_domain->pgtable);
  1009. kfree(omap_domain);
  1010. }
  1011. static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
  1012. unsigned long da)
  1013. {
  1014. struct omap_iommu_domain *omap_domain = domain->priv;
  1015. struct iommu *oiommu = omap_domain->iommu_dev;
  1016. struct device *dev = oiommu->dev;
  1017. u32 *pgd, *pte;
  1018. phys_addr_t ret = 0;
  1019. iopgtable_lookup_entry(oiommu, da, &pgd, &pte);
  1020. if (pte) {
  1021. if (iopte_is_small(*pte))
  1022. ret = omap_iommu_translate(*pte, da, IOPTE_MASK);
  1023. else if (iopte_is_large(*pte))
  1024. ret = omap_iommu_translate(*pte, da, IOLARGE_MASK);
  1025. else
  1026. dev_err(dev, "bogus pte 0x%x", *pte);
  1027. } else {
  1028. if (iopgd_is_section(*pgd))
  1029. ret = omap_iommu_translate(*pgd, da, IOSECTION_MASK);
  1030. else if (iopgd_is_super(*pgd))
  1031. ret = omap_iommu_translate(*pgd, da, IOSUPER_MASK);
  1032. else
  1033. dev_err(dev, "bogus pgd 0x%x", *pgd);
  1034. }
  1035. return ret;
  1036. }
  1037. static int omap_iommu_domain_has_cap(struct iommu_domain *domain,
  1038. unsigned long cap)
  1039. {
  1040. return 0;
  1041. }
  1042. static struct iommu_ops omap_iommu_ops = {
  1043. .domain_init = omap_iommu_domain_init,
  1044. .domain_destroy = omap_iommu_domain_destroy,
  1045. .attach_dev = omap_iommu_attach_dev,
  1046. .detach_dev = omap_iommu_detach_dev,
  1047. .map = omap_iommu_map,
  1048. .unmap = omap_iommu_unmap,
  1049. .iova_to_phys = omap_iommu_iova_to_phys,
  1050. .domain_has_cap = omap_iommu_domain_has_cap,
  1051. };
  1052. static int __init omap_iommu_init(void)
  1053. {
  1054. struct kmem_cache *p;
  1055. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  1056. size_t align = 1 << 10; /* L2 pagetable alignement */
  1057. p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags,
  1058. iopte_cachep_ctor);
  1059. if (!p)
  1060. return -ENOMEM;
  1061. iopte_cachep = p;
  1062. register_iommu(&omap_iommu_ops);
  1063. return platform_driver_register(&omap_iommu_driver);
  1064. }
  1065. module_init(omap_iommu_init);
  1066. static void __exit omap_iommu_exit(void)
  1067. {
  1068. kmem_cache_destroy(iopte_cachep);
  1069. platform_driver_unregister(&omap_iommu_driver);
  1070. }
  1071. module_exit(omap_iommu_exit);
  1072. MODULE_DESCRIPTION("omap iommu: tlb and pagetable primitives");
  1073. MODULE_ALIAS("platform:omap-iommu");
  1074. MODULE_AUTHOR("Hiroshi DOYU, Paul Mundt and Toshihiro Kobayashi");
  1075. MODULE_LICENSE("GPL v2");