fsl_pamu_domain.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  16. * Author: Varun Sethi <varun.sethi@freescale.com>
  17. *
  18. */
  19. #define pr_fmt(fmt) "fsl-pamu-domain: %s: " fmt, __func__
  20. #include <linux/init.h>
  21. #include <linux/iommu.h>
  22. #include <linux/notifier.h>
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/device.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/bootmem.h>
  31. #include <linux/err.h>
  32. #include <asm/io.h>
  33. #include <asm/bitops.h>
  34. #include <asm/pci-bridge.h>
  35. #include <sysdev/fsl_pci.h>
  36. #include "fsl_pamu_domain.h"
  37. #include "pci.h"
  38. /*
  39. * Global spinlock that needs to be held while
  40. * configuring PAMU.
  41. */
  42. static DEFINE_SPINLOCK(iommu_lock);
  43. static struct kmem_cache *fsl_pamu_domain_cache;
  44. static struct kmem_cache *iommu_devinfo_cache;
  45. static DEFINE_SPINLOCK(device_domain_lock);
  46. static int __init iommu_init_mempool(void)
  47. {
  48. fsl_pamu_domain_cache = kmem_cache_create("fsl_pamu_domain",
  49. sizeof(struct fsl_dma_domain),
  50. 0,
  51. SLAB_HWCACHE_ALIGN,
  52. NULL);
  53. if (!fsl_pamu_domain_cache) {
  54. pr_debug("Couldn't create fsl iommu_domain cache\n");
  55. return -ENOMEM;
  56. }
  57. iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
  58. sizeof(struct device_domain_info),
  59. 0,
  60. SLAB_HWCACHE_ALIGN,
  61. NULL);
  62. if (!iommu_devinfo_cache) {
  63. pr_debug("Couldn't create devinfo cache\n");
  64. kmem_cache_destroy(fsl_pamu_domain_cache);
  65. return -ENOMEM;
  66. }
  67. return 0;
  68. }
  69. static phys_addr_t get_phys_addr(struct fsl_dma_domain *dma_domain, dma_addr_t iova)
  70. {
  71. u32 win_cnt = dma_domain->win_cnt;
  72. struct dma_window *win_ptr =
  73. &dma_domain->win_arr[0];
  74. struct iommu_domain_geometry *geom;
  75. geom = &dma_domain->iommu_domain->geometry;
  76. if (!win_cnt || !dma_domain->geom_size) {
  77. pr_debug("Number of windows/geometry not configured for the domain\n");
  78. return 0;
  79. }
  80. if (win_cnt > 1) {
  81. u64 subwin_size;
  82. dma_addr_t subwin_iova;
  83. u32 wnd;
  84. subwin_size = dma_domain->geom_size >> ilog2(win_cnt);
  85. subwin_iova = iova & ~(subwin_size - 1);
  86. wnd = (subwin_iova - geom->aperture_start) >> ilog2(subwin_size);
  87. win_ptr = &dma_domain->win_arr[wnd];
  88. }
  89. if (win_ptr->valid)
  90. return (win_ptr->paddr + (iova & (win_ptr->size - 1)));
  91. return 0;
  92. }
  93. static int map_subwins(int liodn, struct fsl_dma_domain *dma_domain)
  94. {
  95. struct dma_window *sub_win_ptr =
  96. &dma_domain->win_arr[0];
  97. int i, ret;
  98. unsigned long rpn, flags;
  99. for (i = 0; i < dma_domain->win_cnt; i++) {
  100. if (sub_win_ptr[i].valid) {
  101. rpn = sub_win_ptr[i].paddr >>
  102. PAMU_PAGE_SHIFT;
  103. spin_lock_irqsave(&iommu_lock, flags);
  104. ret = pamu_config_spaace(liodn, dma_domain->win_cnt, i,
  105. sub_win_ptr[i].size,
  106. ~(u32)0,
  107. rpn,
  108. dma_domain->snoop_id,
  109. dma_domain->stash_id,
  110. (i > 0) ? 1 : 0,
  111. sub_win_ptr[i].prot);
  112. spin_unlock_irqrestore(&iommu_lock, flags);
  113. if (ret) {
  114. pr_debug("PAMU SPAACE configuration failed for liodn %d\n",
  115. liodn);
  116. return ret;
  117. }
  118. }
  119. }
  120. return ret;
  121. }
  122. static int map_win(int liodn, struct fsl_dma_domain *dma_domain)
  123. {
  124. int ret;
  125. struct dma_window *wnd = &dma_domain->win_arr[0];
  126. phys_addr_t wnd_addr = dma_domain->iommu_domain->geometry.aperture_start;
  127. unsigned long flags;
  128. spin_lock_irqsave(&iommu_lock, flags);
  129. ret = pamu_config_ppaace(liodn, wnd_addr,
  130. wnd->size,
  131. ~(u32)0,
  132. wnd->paddr >> PAMU_PAGE_SHIFT,
  133. dma_domain->snoop_id, dma_domain->stash_id,
  134. 0, wnd->prot);
  135. spin_unlock_irqrestore(&iommu_lock, flags);
  136. if (ret)
  137. pr_debug("PAMU PAACE configuration failed for liodn %d\n",
  138. liodn);
  139. return ret;
  140. }
  141. /* Map the DMA window corresponding to the LIODN */
  142. static int map_liodn(int liodn, struct fsl_dma_domain *dma_domain)
  143. {
  144. if (dma_domain->win_cnt > 1)
  145. return map_subwins(liodn, dma_domain);
  146. else
  147. return map_win(liodn, dma_domain);
  148. }
  149. /* Update window/subwindow mapping for the LIODN */
  150. static int update_liodn(int liodn, struct fsl_dma_domain *dma_domain, u32 wnd_nr)
  151. {
  152. int ret;
  153. struct dma_window *wnd = &dma_domain->win_arr[wnd_nr];
  154. unsigned long flags;
  155. spin_lock_irqsave(&iommu_lock, flags);
  156. if (dma_domain->win_cnt > 1) {
  157. ret = pamu_config_spaace(liodn, dma_domain->win_cnt, wnd_nr,
  158. wnd->size,
  159. ~(u32)0,
  160. wnd->paddr >> PAMU_PAGE_SHIFT,
  161. dma_domain->snoop_id,
  162. dma_domain->stash_id,
  163. (wnd_nr > 0) ? 1 : 0,
  164. wnd->prot);
  165. if (ret)
  166. pr_debug("Subwindow reconfiguration failed for liodn %d\n", liodn);
  167. } else {
  168. phys_addr_t wnd_addr;
  169. wnd_addr = dma_domain->iommu_domain->geometry.aperture_start;
  170. ret = pamu_config_ppaace(liodn, wnd_addr,
  171. wnd->size,
  172. ~(u32)0,
  173. wnd->paddr >> PAMU_PAGE_SHIFT,
  174. dma_domain->snoop_id, dma_domain->stash_id,
  175. 0, wnd->prot);
  176. if (ret)
  177. pr_debug("Window reconfiguration failed for liodn %d\n", liodn);
  178. }
  179. spin_unlock_irqrestore(&iommu_lock, flags);
  180. return ret;
  181. }
  182. static int update_liodn_stash(int liodn, struct fsl_dma_domain *dma_domain,
  183. u32 val)
  184. {
  185. int ret = 0, i;
  186. unsigned long flags;
  187. spin_lock_irqsave(&iommu_lock, flags);
  188. if (!dma_domain->win_arr) {
  189. pr_debug("Windows not configured, stash destination update failed for liodn %d\n", liodn);
  190. spin_unlock_irqrestore(&iommu_lock, flags);
  191. return -EINVAL;
  192. }
  193. for (i = 0; i < dma_domain->win_cnt; i++) {
  194. ret = pamu_update_paace_stash(liodn, i, val);
  195. if (ret) {
  196. pr_debug("Failed to update SPAACE %d field for liodn %d\n ", i, liodn);
  197. spin_unlock_irqrestore(&iommu_lock, flags);
  198. return ret;
  199. }
  200. }
  201. spin_unlock_irqrestore(&iommu_lock, flags);
  202. return ret;
  203. }
  204. /* Set the geometry parameters for a LIODN */
  205. static int pamu_set_liodn(int liodn, struct device *dev,
  206. struct fsl_dma_domain *dma_domain,
  207. struct iommu_domain_geometry *geom_attr,
  208. u32 win_cnt)
  209. {
  210. phys_addr_t window_addr, window_size;
  211. phys_addr_t subwin_size;
  212. int ret = 0, i;
  213. u32 omi_index = ~(u32)0;
  214. unsigned long flags;
  215. /*
  216. * Configure the omi_index at the geometry setup time.
  217. * This is a static value which depends on the type of
  218. * device and would not change thereafter.
  219. */
  220. get_ome_index(&omi_index, dev);
  221. window_addr = geom_attr->aperture_start;
  222. window_size = dma_domain->geom_size;
  223. spin_lock_irqsave(&iommu_lock, flags);
  224. ret = pamu_disable_liodn(liodn);
  225. if (!ret)
  226. ret = pamu_config_ppaace(liodn, window_addr, window_size, omi_index,
  227. 0, dma_domain->snoop_id,
  228. dma_domain->stash_id, win_cnt, 0);
  229. spin_unlock_irqrestore(&iommu_lock, flags);
  230. if (ret) {
  231. pr_debug("PAMU PAACE configuration failed for liodn %d, win_cnt =%d\n", liodn, win_cnt);
  232. return ret;
  233. }
  234. if (win_cnt > 1) {
  235. subwin_size = window_size >> ilog2(win_cnt);
  236. for (i = 0; i < win_cnt; i++) {
  237. spin_lock_irqsave(&iommu_lock, flags);
  238. ret = pamu_disable_spaace(liodn, i);
  239. if (!ret)
  240. ret = pamu_config_spaace(liodn, win_cnt, i,
  241. subwin_size, omi_index,
  242. 0, dma_domain->snoop_id,
  243. dma_domain->stash_id,
  244. 0, 0);
  245. spin_unlock_irqrestore(&iommu_lock, flags);
  246. if (ret) {
  247. pr_debug("PAMU SPAACE configuration failed for liodn %d\n", liodn);
  248. return ret;
  249. }
  250. }
  251. }
  252. return ret;
  253. }
  254. static int check_size(u64 size, dma_addr_t iova)
  255. {
  256. /*
  257. * Size must be a power of two and at least be equal
  258. * to PAMU page size.
  259. */
  260. if (!is_power_of_2(size) || size < PAMU_PAGE_SIZE) {
  261. pr_debug("%s: size too small or not a power of two\n", __func__);
  262. return -EINVAL;
  263. }
  264. /* iova must be page size aligned*/
  265. if (iova & (size - 1)) {
  266. pr_debug("%s: address is not aligned with window size\n", __func__);
  267. return -EINVAL;
  268. }
  269. return 0;
  270. }
  271. static struct fsl_dma_domain *iommu_alloc_dma_domain(void)
  272. {
  273. struct fsl_dma_domain *domain;
  274. domain = kmem_cache_zalloc(fsl_pamu_domain_cache, GFP_KERNEL);
  275. if (!domain)
  276. return NULL;
  277. domain->stash_id = ~(u32)0;
  278. domain->snoop_id = ~(u32)0;
  279. domain->win_cnt = pamu_get_max_subwin_cnt();
  280. domain->geom_size = 0;
  281. INIT_LIST_HEAD(&domain->devices);
  282. spin_lock_init(&domain->domain_lock);
  283. return domain;
  284. }
  285. static inline struct device_domain_info *find_domain(struct device *dev)
  286. {
  287. return dev->archdata.iommu_domain;
  288. }
  289. static void remove_device_ref(struct device_domain_info *info, u32 win_cnt)
  290. {
  291. unsigned long flags;
  292. list_del(&info->link);
  293. spin_lock_irqsave(&iommu_lock, flags);
  294. if (win_cnt > 1)
  295. pamu_free_subwins(info->liodn);
  296. pamu_disable_liodn(info->liodn);
  297. spin_unlock_irqrestore(&iommu_lock, flags);
  298. spin_lock_irqsave(&device_domain_lock, flags);
  299. info->dev->archdata.iommu_domain = NULL;
  300. kmem_cache_free(iommu_devinfo_cache, info);
  301. spin_unlock_irqrestore(&device_domain_lock, flags);
  302. }
  303. static void detach_device(struct device *dev, struct fsl_dma_domain *dma_domain)
  304. {
  305. struct device_domain_info *info, *tmp;
  306. unsigned long flags;
  307. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  308. /* Remove the device from the domain device list */
  309. list_for_each_entry_safe(info, tmp, &dma_domain->devices, link) {
  310. if (!dev || (info->dev == dev))
  311. remove_device_ref(info, dma_domain->win_cnt);
  312. }
  313. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  314. }
  315. static void attach_device(struct fsl_dma_domain *dma_domain, int liodn, struct device *dev)
  316. {
  317. struct device_domain_info *info, *old_domain_info;
  318. unsigned long flags;
  319. spin_lock_irqsave(&device_domain_lock, flags);
  320. /*
  321. * Check here if the device is already attached to domain or not.
  322. * If the device is already attached to a domain detach it.
  323. */
  324. old_domain_info = find_domain(dev);
  325. if (old_domain_info && old_domain_info->domain != dma_domain) {
  326. spin_unlock_irqrestore(&device_domain_lock, flags);
  327. detach_device(dev, old_domain_info->domain);
  328. spin_lock_irqsave(&device_domain_lock, flags);
  329. }
  330. info = kmem_cache_zalloc(iommu_devinfo_cache, GFP_ATOMIC);
  331. info->dev = dev;
  332. info->liodn = liodn;
  333. info->domain = dma_domain;
  334. list_add(&info->link, &dma_domain->devices);
  335. /*
  336. * In case of devices with multiple LIODNs just store
  337. * the info for the first LIODN as all
  338. * LIODNs share the same domain
  339. */
  340. if (!old_domain_info)
  341. dev->archdata.iommu_domain = info;
  342. spin_unlock_irqrestore(&device_domain_lock, flags);
  343. }
  344. static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain,
  345. dma_addr_t iova)
  346. {
  347. struct fsl_dma_domain *dma_domain = domain->priv;
  348. if ((iova < domain->geometry.aperture_start) ||
  349. iova > (domain->geometry.aperture_end))
  350. return 0;
  351. return get_phys_addr(dma_domain, iova);
  352. }
  353. static int fsl_pamu_domain_has_cap(struct iommu_domain *domain,
  354. unsigned long cap)
  355. {
  356. return cap == IOMMU_CAP_CACHE_COHERENCY;
  357. }
  358. static void fsl_pamu_domain_destroy(struct iommu_domain *domain)
  359. {
  360. struct fsl_dma_domain *dma_domain = domain->priv;
  361. domain->priv = NULL;
  362. /* remove all the devices from the device list */
  363. detach_device(NULL, dma_domain);
  364. dma_domain->enabled = 0;
  365. dma_domain->mapped = 0;
  366. kmem_cache_free(fsl_pamu_domain_cache, dma_domain);
  367. }
  368. static int fsl_pamu_domain_init(struct iommu_domain *domain)
  369. {
  370. struct fsl_dma_domain *dma_domain;
  371. dma_domain = iommu_alloc_dma_domain();
  372. if (!dma_domain) {
  373. pr_debug("dma_domain allocation failed\n");
  374. return -ENOMEM;
  375. }
  376. domain->priv = dma_domain;
  377. dma_domain->iommu_domain = domain;
  378. /* defaul geometry 64 GB i.e. maximum system address */
  379. domain->geometry.aperture_start = 0;
  380. domain->geometry.aperture_end = (1ULL << 36) - 1;
  381. domain->geometry.force_aperture = true;
  382. return 0;
  383. }
  384. /* Configure geometry settings for all LIODNs associated with domain */
  385. static int pamu_set_domain_geometry(struct fsl_dma_domain *dma_domain,
  386. struct iommu_domain_geometry *geom_attr,
  387. u32 win_cnt)
  388. {
  389. struct device_domain_info *info;
  390. int ret = 0;
  391. list_for_each_entry(info, &dma_domain->devices, link) {
  392. ret = pamu_set_liodn(info->liodn, info->dev, dma_domain,
  393. geom_attr, win_cnt);
  394. if (ret)
  395. break;
  396. }
  397. return ret;
  398. }
  399. /* Update stash destination for all LIODNs associated with the domain */
  400. static int update_domain_stash(struct fsl_dma_domain *dma_domain, u32 val)
  401. {
  402. struct device_domain_info *info;
  403. int ret = 0;
  404. list_for_each_entry(info, &dma_domain->devices, link) {
  405. ret = update_liodn_stash(info->liodn, dma_domain, val);
  406. if (ret)
  407. break;
  408. }
  409. return ret;
  410. }
  411. /* Update domain mappings for all LIODNs associated with the domain */
  412. static int update_domain_mapping(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
  413. {
  414. struct device_domain_info *info;
  415. int ret = 0;
  416. list_for_each_entry(info, &dma_domain->devices, link) {
  417. ret = update_liodn(info->liodn, dma_domain, wnd_nr);
  418. if (ret)
  419. break;
  420. }
  421. return ret;
  422. }
  423. static int disable_domain_win(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
  424. {
  425. struct device_domain_info *info;
  426. int ret = 0;
  427. list_for_each_entry(info, &dma_domain->devices, link) {
  428. if (dma_domain->win_cnt == 1 && dma_domain->enabled) {
  429. ret = pamu_disable_liodn(info->liodn);
  430. if (!ret)
  431. dma_domain->enabled = 0;
  432. } else {
  433. ret = pamu_disable_spaace(info->liodn, wnd_nr);
  434. }
  435. }
  436. return ret;
  437. }
  438. static void fsl_pamu_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  439. {
  440. struct fsl_dma_domain *dma_domain = domain->priv;
  441. unsigned long flags;
  442. int ret;
  443. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  444. if (!dma_domain->win_arr) {
  445. pr_debug("Number of windows not configured\n");
  446. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  447. return;
  448. }
  449. if (wnd_nr >= dma_domain->win_cnt) {
  450. pr_debug("Invalid window index\n");
  451. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  452. return;
  453. }
  454. if (dma_domain->win_arr[wnd_nr].valid) {
  455. ret = disable_domain_win(dma_domain, wnd_nr);
  456. if (!ret) {
  457. dma_domain->win_arr[wnd_nr].valid = 0;
  458. dma_domain->mapped--;
  459. }
  460. }
  461. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  462. }
  463. static int fsl_pamu_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  464. phys_addr_t paddr, u64 size, int prot)
  465. {
  466. struct fsl_dma_domain *dma_domain = domain->priv;
  467. struct dma_window *wnd;
  468. int pamu_prot = 0;
  469. int ret;
  470. unsigned long flags;
  471. u64 win_size;
  472. if (prot & IOMMU_READ)
  473. pamu_prot |= PAACE_AP_PERMS_QUERY;
  474. if (prot & IOMMU_WRITE)
  475. pamu_prot |= PAACE_AP_PERMS_UPDATE;
  476. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  477. if (!dma_domain->win_arr) {
  478. pr_debug("Number of windows not configured\n");
  479. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  480. return -ENODEV;
  481. }
  482. if (wnd_nr >= dma_domain->win_cnt) {
  483. pr_debug("Invalid window index\n");
  484. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  485. return -EINVAL;
  486. }
  487. win_size = dma_domain->geom_size >> ilog2(dma_domain->win_cnt);
  488. if (size > win_size) {
  489. pr_debug("Invalid window size \n");
  490. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  491. return -EINVAL;
  492. }
  493. if (dma_domain->win_cnt == 1) {
  494. if (dma_domain->enabled) {
  495. pr_debug("Disable the window before updating the mapping\n");
  496. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  497. return -EBUSY;
  498. }
  499. ret = check_size(size, domain->geometry.aperture_start);
  500. if (ret) {
  501. pr_debug("Aperture start not aligned to the size\n");
  502. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  503. return -EINVAL;
  504. }
  505. }
  506. wnd = &dma_domain->win_arr[wnd_nr];
  507. if (!wnd->valid) {
  508. wnd->paddr = paddr;
  509. wnd->size = size;
  510. wnd->prot = pamu_prot;
  511. ret = update_domain_mapping(dma_domain, wnd_nr);
  512. if (!ret) {
  513. wnd->valid = 1;
  514. dma_domain->mapped++;
  515. }
  516. } else {
  517. pr_debug("Disable the window before updating the mapping\n");
  518. ret = -EBUSY;
  519. }
  520. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  521. return ret;
  522. }
  523. /*
  524. * Attach the LIODN to the DMA domain and configure the geometry
  525. * and window mappings.
  526. */
  527. static int handle_attach_device(struct fsl_dma_domain *dma_domain,
  528. struct device *dev, const u32 *liodn,
  529. int num)
  530. {
  531. unsigned long flags;
  532. struct iommu_domain *domain = dma_domain->iommu_domain;
  533. int ret = 0;
  534. int i;
  535. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  536. for (i = 0; i < num; i++) {
  537. /* Ensure that LIODN value is valid */
  538. if (liodn[i] >= PAACE_NUMBER_ENTRIES) {
  539. pr_debug("Invalid liodn %d, attach device failed for %s\n",
  540. liodn[i], dev->of_node->full_name);
  541. ret = -EINVAL;
  542. break;
  543. }
  544. attach_device(dma_domain, liodn[i], dev);
  545. /*
  546. * Check if geometry has already been configured
  547. * for the domain. If yes, set the geometry for
  548. * the LIODN.
  549. */
  550. if (dma_domain->win_arr) {
  551. u32 win_cnt = dma_domain->win_cnt > 1 ? dma_domain->win_cnt : 0;
  552. ret = pamu_set_liodn(liodn[i], dev, dma_domain,
  553. &domain->geometry,
  554. win_cnt);
  555. if (ret)
  556. break;
  557. if (dma_domain->mapped) {
  558. /*
  559. * Create window/subwindow mapping for
  560. * the LIODN.
  561. */
  562. ret = map_liodn(liodn[i], dma_domain);
  563. if (ret)
  564. break;
  565. }
  566. }
  567. }
  568. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  569. return ret;
  570. }
  571. static int fsl_pamu_attach_device(struct iommu_domain *domain,
  572. struct device *dev)
  573. {
  574. struct fsl_dma_domain *dma_domain = domain->priv;
  575. const u32 *liodn;
  576. u32 liodn_cnt;
  577. int len, ret = 0;
  578. struct pci_dev *pdev = NULL;
  579. struct pci_controller *pci_ctl;
  580. /*
  581. * Use LIODN of the PCI controller while attaching a
  582. * PCI device.
  583. */
  584. if (dev->bus == &pci_bus_type) {
  585. pdev = to_pci_dev(dev);
  586. pci_ctl = pci_bus_to_host(pdev->bus);
  587. /*
  588. * make dev point to pci controller device
  589. * so we can get the LIODN programmed by
  590. * u-boot.
  591. */
  592. dev = pci_ctl->parent;
  593. }
  594. liodn = of_get_property(dev->of_node, "fsl,liodn", &len);
  595. if (liodn) {
  596. liodn_cnt = len / sizeof(u32);
  597. ret = handle_attach_device(dma_domain, dev,
  598. liodn, liodn_cnt);
  599. } else {
  600. pr_debug("missing fsl,liodn property at %s\n",
  601. dev->of_node->full_name);
  602. ret = -EINVAL;
  603. }
  604. return ret;
  605. }
  606. static void fsl_pamu_detach_device(struct iommu_domain *domain,
  607. struct device *dev)
  608. {
  609. struct fsl_dma_domain *dma_domain = domain->priv;
  610. const u32 *prop;
  611. int len;
  612. struct pci_dev *pdev = NULL;
  613. struct pci_controller *pci_ctl;
  614. /*
  615. * Use LIODN of the PCI controller while detaching a
  616. * PCI device.
  617. */
  618. if (dev->bus == &pci_bus_type) {
  619. pdev = to_pci_dev(dev);
  620. pci_ctl = pci_bus_to_host(pdev->bus);
  621. /*
  622. * make dev point to pci controller device
  623. * so we can get the LIODN programmed by
  624. * u-boot.
  625. */
  626. dev = pci_ctl->parent;
  627. }
  628. prop = of_get_property(dev->of_node, "fsl,liodn", &len);
  629. if (prop)
  630. detach_device(dev, dma_domain);
  631. else
  632. pr_debug("missing fsl,liodn property at %s\n",
  633. dev->of_node->full_name);
  634. }
  635. static int configure_domain_geometry(struct iommu_domain *domain, void *data)
  636. {
  637. struct iommu_domain_geometry *geom_attr = data;
  638. struct fsl_dma_domain *dma_domain = domain->priv;
  639. dma_addr_t geom_size;
  640. unsigned long flags;
  641. geom_size = geom_attr->aperture_end - geom_attr->aperture_start + 1;
  642. /*
  643. * Sanity check the geometry size. Also, we do not support
  644. * DMA outside of the geometry.
  645. */
  646. if (check_size(geom_size, geom_attr->aperture_start) ||
  647. !geom_attr->force_aperture) {
  648. pr_debug("Invalid PAMU geometry attributes\n");
  649. return -EINVAL;
  650. }
  651. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  652. if (dma_domain->enabled) {
  653. pr_debug("Can't set geometry attributes as domain is active\n");
  654. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  655. return -EBUSY;
  656. }
  657. /* Copy the domain geometry information */
  658. memcpy(&domain->geometry, geom_attr,
  659. sizeof(struct iommu_domain_geometry));
  660. dma_domain->geom_size = geom_size;
  661. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  662. return 0;
  663. }
  664. /* Set the domain stash attribute */
  665. static int configure_domain_stash(struct fsl_dma_domain *dma_domain, void *data)
  666. {
  667. struct pamu_stash_attribute *stash_attr = data;
  668. unsigned long flags;
  669. int ret;
  670. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  671. memcpy(&dma_domain->dma_stash, stash_attr,
  672. sizeof(struct pamu_stash_attribute));
  673. dma_domain->stash_id = get_stash_id(stash_attr->cache,
  674. stash_attr->cpu);
  675. if (dma_domain->stash_id == ~(u32)0) {
  676. pr_debug("Invalid stash attributes\n");
  677. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  678. return -EINVAL;
  679. }
  680. ret = update_domain_stash(dma_domain, dma_domain->stash_id);
  681. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  682. return ret;
  683. }
  684. /* Configure domain dma state i.e. enable/disable DMA*/
  685. static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable)
  686. {
  687. struct device_domain_info *info;
  688. unsigned long flags;
  689. int ret;
  690. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  691. if (enable && !dma_domain->mapped) {
  692. pr_debug("Can't enable DMA domain without valid mapping\n");
  693. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  694. return -ENODEV;
  695. }
  696. dma_domain->enabled = enable;
  697. list_for_each_entry(info, &dma_domain->devices,
  698. link) {
  699. ret = (enable) ? pamu_enable_liodn(info->liodn) :
  700. pamu_disable_liodn(info->liodn);
  701. if (ret)
  702. pr_debug("Unable to set dma state for liodn %d",
  703. info->liodn);
  704. }
  705. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  706. return 0;
  707. }
  708. static int fsl_pamu_set_domain_attr(struct iommu_domain *domain,
  709. enum iommu_attr attr_type, void *data)
  710. {
  711. struct fsl_dma_domain *dma_domain = domain->priv;
  712. int ret = 0;
  713. switch (attr_type) {
  714. case DOMAIN_ATTR_GEOMETRY:
  715. ret = configure_domain_geometry(domain, data);
  716. break;
  717. case DOMAIN_ATTR_FSL_PAMU_STASH:
  718. ret = configure_domain_stash(dma_domain, data);
  719. break;
  720. case DOMAIN_ATTR_FSL_PAMU_ENABLE:
  721. ret = configure_domain_dma_state(dma_domain, *(int *)data);
  722. break;
  723. default:
  724. pr_debug("Unsupported attribute type\n");
  725. ret = -EINVAL;
  726. break;
  727. };
  728. return ret;
  729. }
  730. static int fsl_pamu_get_domain_attr(struct iommu_domain *domain,
  731. enum iommu_attr attr_type, void *data)
  732. {
  733. struct fsl_dma_domain *dma_domain = domain->priv;
  734. int ret = 0;
  735. switch (attr_type) {
  736. case DOMAIN_ATTR_FSL_PAMU_STASH:
  737. memcpy((struct pamu_stash_attribute *) data, &dma_domain->dma_stash,
  738. sizeof(struct pamu_stash_attribute));
  739. break;
  740. case DOMAIN_ATTR_FSL_PAMU_ENABLE:
  741. *(int *)data = dma_domain->enabled;
  742. break;
  743. case DOMAIN_ATTR_FSL_PAMUV1:
  744. *(int *)data = DOMAIN_ATTR_FSL_PAMUV1;
  745. break;
  746. default:
  747. pr_debug("Unsupported attribute type\n");
  748. ret = -EINVAL;
  749. break;
  750. };
  751. return ret;
  752. }
  753. #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
  754. static struct iommu_group *get_device_iommu_group(struct device *dev)
  755. {
  756. struct iommu_group *group;
  757. group = iommu_group_get(dev);
  758. if (!group)
  759. group = iommu_group_alloc();
  760. return group;
  761. }
  762. static bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl)
  763. {
  764. u32 version;
  765. /* Check the PCI controller version number by readding BRR1 register */
  766. version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2));
  767. version &= PCI_FSL_BRR1_VER;
  768. /* If PCI controller version is >= 0x204 we can partition endpoints*/
  769. if (version >= 0x204)
  770. return 1;
  771. return 0;
  772. }
  773. /* Get iommu group information from peer devices or devices on the parent bus */
  774. static struct iommu_group *get_shared_pci_device_group(struct pci_dev *pdev)
  775. {
  776. struct pci_dev *tmp;
  777. struct iommu_group *group;
  778. struct pci_bus *bus = pdev->bus;
  779. /*
  780. * Traverese the pci bus device list to get
  781. * the shared iommu group.
  782. */
  783. while (bus) {
  784. list_for_each_entry(tmp, &bus->devices, bus_list) {
  785. if (tmp == pdev)
  786. continue;
  787. group = iommu_group_get(&tmp->dev);
  788. if (group)
  789. return group;
  790. }
  791. bus = bus->parent;
  792. }
  793. return NULL;
  794. }
  795. static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
  796. {
  797. struct pci_controller *pci_ctl;
  798. bool pci_endpt_partioning;
  799. struct iommu_group *group = NULL;
  800. struct pci_dev *bridge, *dma_pdev = NULL;
  801. pci_ctl = pci_bus_to_host(pdev->bus);
  802. pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
  803. /* We can partition PCIe devices so assign device group to the device */
  804. if (pci_endpt_partioning) {
  805. bridge = pci_find_upstream_pcie_bridge(pdev);
  806. if (bridge) {
  807. if (pci_is_pcie(bridge))
  808. dma_pdev = pci_get_domain_bus_and_slot(
  809. pci_domain_nr(pdev->bus),
  810. bridge->subordinate->number, 0);
  811. if (!dma_pdev)
  812. dma_pdev = pci_dev_get(bridge);
  813. } else
  814. dma_pdev = pci_dev_get(pdev);
  815. /* Account for quirked devices */
  816. swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
  817. /*
  818. * If it's a multifunction device that does not support our
  819. * required ACS flags, add to the same group as lowest numbered
  820. * function that also does not suport the required ACS flags.
  821. */
  822. if (dma_pdev->multifunction &&
  823. !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)) {
  824. u8 i, slot = PCI_SLOT(dma_pdev->devfn);
  825. for (i = 0; i < 8; i++) {
  826. struct pci_dev *tmp;
  827. tmp = pci_get_slot(dma_pdev->bus, PCI_DEVFN(slot, i));
  828. if (!tmp)
  829. continue;
  830. if (!pci_acs_enabled(tmp, REQ_ACS_FLAGS)) {
  831. swap_pci_ref(&dma_pdev, tmp);
  832. break;
  833. }
  834. pci_dev_put(tmp);
  835. }
  836. }
  837. /*
  838. * Devices on the root bus go through the iommu. If that's not us,
  839. * find the next upstream device and test ACS up to the root bus.
  840. * Finding the next device may require skipping virtual buses.
  841. */
  842. while (!pci_is_root_bus(dma_pdev->bus)) {
  843. struct pci_bus *bus = dma_pdev->bus;
  844. while (!bus->self) {
  845. if (!pci_is_root_bus(bus))
  846. bus = bus->parent;
  847. else
  848. goto root_bus;
  849. }
  850. if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
  851. break;
  852. swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
  853. }
  854. root_bus:
  855. group = get_device_iommu_group(&dma_pdev->dev);
  856. pci_dev_put(dma_pdev);
  857. /*
  858. * PCIe controller is not a paritionable entity
  859. * free the controller device iommu_group.
  860. */
  861. if (pci_ctl->parent->iommu_group)
  862. iommu_group_remove_device(pci_ctl->parent);
  863. } else {
  864. /*
  865. * All devices connected to the controller will share the
  866. * PCI controllers device group. If this is the first
  867. * device to be probed for the pci controller, copy the
  868. * device group information from the PCI controller device
  869. * node and remove the PCI controller iommu group.
  870. * For subsequent devices, the iommu group information can
  871. * be obtained from sibling devices (i.e. from the bus_devices
  872. * link list).
  873. */
  874. if (pci_ctl->parent->iommu_group) {
  875. group = get_device_iommu_group(pci_ctl->parent);
  876. iommu_group_remove_device(pci_ctl->parent);
  877. } else
  878. group = get_shared_pci_device_group(pdev);
  879. }
  880. return group;
  881. }
  882. static int fsl_pamu_add_device(struct device *dev)
  883. {
  884. struct iommu_group *group = NULL;
  885. struct pci_dev *pdev;
  886. const u32 *prop;
  887. int ret, len;
  888. /*
  889. * For platform devices we allocate a separate group for
  890. * each of the devices.
  891. */
  892. if (dev->bus == &pci_bus_type) {
  893. pdev = to_pci_dev(dev);
  894. /* Don't create device groups for virtual PCI bridges */
  895. if (pdev->subordinate)
  896. return 0;
  897. group = get_pci_device_group(pdev);
  898. } else {
  899. prop = of_get_property(dev->of_node, "fsl,liodn", &len);
  900. if (prop)
  901. group = get_device_iommu_group(dev);
  902. }
  903. if (!group || IS_ERR(group))
  904. return PTR_ERR(group);
  905. ret = iommu_group_add_device(group, dev);
  906. iommu_group_put(group);
  907. return ret;
  908. }
  909. static void fsl_pamu_remove_device(struct device *dev)
  910. {
  911. iommu_group_remove_device(dev);
  912. }
  913. static int fsl_pamu_set_windows(struct iommu_domain *domain, u32 w_count)
  914. {
  915. struct fsl_dma_domain *dma_domain = domain->priv;
  916. unsigned long flags;
  917. int ret;
  918. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  919. /* Ensure domain is inactive i.e. DMA should be disabled for the domain */
  920. if (dma_domain->enabled) {
  921. pr_debug("Can't set geometry attributes as domain is active\n");
  922. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  923. return -EBUSY;
  924. }
  925. /* Ensure that the geometry has been set for the domain */
  926. if (!dma_domain->geom_size) {
  927. pr_debug("Please configure geometry before setting the number of windows\n");
  928. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  929. return -EINVAL;
  930. }
  931. /*
  932. * Ensure we have valid window count i.e. it should be less than
  933. * maximum permissible limit and should be a power of two.
  934. */
  935. if (w_count > pamu_get_max_subwin_cnt() || !is_power_of_2(w_count)) {
  936. pr_debug("Invalid window count\n");
  937. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  938. return -EINVAL;
  939. }
  940. ret = pamu_set_domain_geometry(dma_domain, &domain->geometry,
  941. ((w_count > 1) ? w_count : 0));
  942. if (!ret) {
  943. if (dma_domain->win_arr)
  944. kfree(dma_domain->win_arr);
  945. dma_domain->win_arr = kzalloc(sizeof(struct dma_window) *
  946. w_count, GFP_ATOMIC);
  947. if (!dma_domain->win_arr) {
  948. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  949. return -ENOMEM;
  950. }
  951. dma_domain->win_cnt = w_count;
  952. }
  953. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  954. return ret;
  955. }
  956. static u32 fsl_pamu_get_windows(struct iommu_domain *domain)
  957. {
  958. struct fsl_dma_domain *dma_domain = domain->priv;
  959. return dma_domain->win_cnt;
  960. }
  961. static struct iommu_ops fsl_pamu_ops = {
  962. .domain_init = fsl_pamu_domain_init,
  963. .domain_destroy = fsl_pamu_domain_destroy,
  964. .attach_dev = fsl_pamu_attach_device,
  965. .detach_dev = fsl_pamu_detach_device,
  966. .domain_window_enable = fsl_pamu_window_enable,
  967. .domain_window_disable = fsl_pamu_window_disable,
  968. .domain_get_windows = fsl_pamu_get_windows,
  969. .domain_set_windows = fsl_pamu_set_windows,
  970. .iova_to_phys = fsl_pamu_iova_to_phys,
  971. .domain_has_cap = fsl_pamu_domain_has_cap,
  972. .domain_set_attr = fsl_pamu_set_domain_attr,
  973. .domain_get_attr = fsl_pamu_get_domain_attr,
  974. .add_device = fsl_pamu_add_device,
  975. .remove_device = fsl_pamu_remove_device,
  976. };
  977. int pamu_domain_init()
  978. {
  979. int ret = 0;
  980. ret = iommu_init_mempool();
  981. if (ret)
  982. return ret;
  983. bus_set_iommu(&platform_bus_type, &fsl_pamu_ops);
  984. bus_set_iommu(&pci_bus_type, &fsl_pamu_ops);
  985. return ret;
  986. }