fsl_pamu_domain.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 ((size & (size - 1)) || 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 void remove_device_ref(struct device_domain_info *info, u32 win_cnt)
  286. {
  287. unsigned long flags;
  288. list_del(&info->link);
  289. spin_lock_irqsave(&iommu_lock, flags);
  290. if (win_cnt > 1)
  291. pamu_free_subwins(info->liodn);
  292. pamu_disable_liodn(info->liodn);
  293. spin_unlock_irqrestore(&iommu_lock, flags);
  294. spin_lock_irqsave(&device_domain_lock, flags);
  295. info->dev->archdata.iommu_domain = NULL;
  296. kmem_cache_free(iommu_devinfo_cache, info);
  297. spin_unlock_irqrestore(&device_domain_lock, flags);
  298. }
  299. static void detach_device(struct device *dev, struct fsl_dma_domain *dma_domain)
  300. {
  301. struct device_domain_info *info, *tmp;
  302. unsigned long flags;
  303. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  304. /* Remove the device from the domain device list */
  305. list_for_each_entry_safe(info, tmp, &dma_domain->devices, link) {
  306. if (!dev || (info->dev == dev))
  307. remove_device_ref(info, dma_domain->win_cnt);
  308. }
  309. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  310. }
  311. static void attach_device(struct fsl_dma_domain *dma_domain, int liodn, struct device *dev)
  312. {
  313. struct device_domain_info *info, *old_domain_info;
  314. unsigned long flags;
  315. spin_lock_irqsave(&device_domain_lock, flags);
  316. /*
  317. * Check here if the device is already attached to domain or not.
  318. * If the device is already attached to a domain detach it.
  319. */
  320. old_domain_info = dev->archdata.iommu_domain;
  321. if (old_domain_info && old_domain_info->domain != dma_domain) {
  322. spin_unlock_irqrestore(&device_domain_lock, flags);
  323. detach_device(dev, old_domain_info->domain);
  324. spin_lock_irqsave(&device_domain_lock, flags);
  325. }
  326. info = kmem_cache_zalloc(iommu_devinfo_cache, GFP_ATOMIC);
  327. info->dev = dev;
  328. info->liodn = liodn;
  329. info->domain = dma_domain;
  330. list_add(&info->link, &dma_domain->devices);
  331. /*
  332. * In case of devices with multiple LIODNs just store
  333. * the info for the first LIODN as all
  334. * LIODNs share the same domain
  335. */
  336. if (!dev->archdata.iommu_domain)
  337. dev->archdata.iommu_domain = info;
  338. spin_unlock_irqrestore(&device_domain_lock, flags);
  339. }
  340. static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain,
  341. dma_addr_t iova)
  342. {
  343. struct fsl_dma_domain *dma_domain = domain->priv;
  344. if ((iova < domain->geometry.aperture_start) ||
  345. iova > (domain->geometry.aperture_end))
  346. return 0;
  347. return get_phys_addr(dma_domain, iova);
  348. }
  349. static int fsl_pamu_domain_has_cap(struct iommu_domain *domain,
  350. unsigned long cap)
  351. {
  352. return cap == IOMMU_CAP_CACHE_COHERENCY;
  353. }
  354. static void fsl_pamu_domain_destroy(struct iommu_domain *domain)
  355. {
  356. struct fsl_dma_domain *dma_domain = domain->priv;
  357. domain->priv = NULL;
  358. /* remove all the devices from the device list */
  359. detach_device(NULL, dma_domain);
  360. dma_domain->enabled = 0;
  361. dma_domain->mapped = 0;
  362. kmem_cache_free(fsl_pamu_domain_cache, dma_domain);
  363. }
  364. static int fsl_pamu_domain_init(struct iommu_domain *domain)
  365. {
  366. struct fsl_dma_domain *dma_domain;
  367. dma_domain = iommu_alloc_dma_domain();
  368. if (!dma_domain) {
  369. pr_debug("dma_domain allocation failed\n");
  370. return -ENOMEM;
  371. }
  372. domain->priv = dma_domain;
  373. dma_domain->iommu_domain = domain;
  374. /* defaul geometry 64 GB i.e. maximum system address */
  375. domain->geometry.aperture_start = 0;
  376. domain->geometry.aperture_end = (1ULL << 36) - 1;
  377. domain->geometry.force_aperture = true;
  378. return 0;
  379. }
  380. /* Configure geometry settings for all LIODNs associated with domain */
  381. static int pamu_set_domain_geometry(struct fsl_dma_domain *dma_domain,
  382. struct iommu_domain_geometry *geom_attr,
  383. u32 win_cnt)
  384. {
  385. struct device_domain_info *info;
  386. int ret = 0;
  387. list_for_each_entry(info, &dma_domain->devices, link) {
  388. ret = pamu_set_liodn(info->liodn, info->dev, dma_domain,
  389. geom_attr, win_cnt);
  390. if (ret)
  391. break;
  392. }
  393. return ret;
  394. }
  395. /* Update stash destination for all LIODNs associated with the domain */
  396. static int update_domain_stash(struct fsl_dma_domain *dma_domain, u32 val)
  397. {
  398. struct device_domain_info *info;
  399. int ret = 0;
  400. list_for_each_entry(info, &dma_domain->devices, link) {
  401. ret = update_liodn_stash(info->liodn, dma_domain, val);
  402. if (ret)
  403. break;
  404. }
  405. return ret;
  406. }
  407. /* Update domain mappings for all LIODNs associated with the domain */
  408. static int update_domain_mapping(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
  409. {
  410. struct device_domain_info *info;
  411. int ret = 0;
  412. list_for_each_entry(info, &dma_domain->devices, link) {
  413. ret = update_liodn(info->liodn, dma_domain, wnd_nr);
  414. if (ret)
  415. break;
  416. }
  417. return ret;
  418. }
  419. static int disable_domain_win(struct fsl_dma_domain *dma_domain, u32 wnd_nr)
  420. {
  421. struct device_domain_info *info;
  422. int ret = 0;
  423. list_for_each_entry(info, &dma_domain->devices, link) {
  424. if (dma_domain->win_cnt == 1 && dma_domain->enabled) {
  425. ret = pamu_disable_liodn(info->liodn);
  426. if (!ret)
  427. dma_domain->enabled = 0;
  428. } else {
  429. ret = pamu_disable_spaace(info->liodn, wnd_nr);
  430. }
  431. }
  432. return ret;
  433. }
  434. static void fsl_pamu_window_disable(struct iommu_domain *domain, u32 wnd_nr)
  435. {
  436. struct fsl_dma_domain *dma_domain = domain->priv;
  437. unsigned long flags;
  438. int ret;
  439. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  440. if (!dma_domain->win_arr) {
  441. pr_debug("Number of windows not configured\n");
  442. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  443. return;
  444. }
  445. if (wnd_nr >= dma_domain->win_cnt) {
  446. pr_debug("Invalid window index\n");
  447. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  448. return;
  449. }
  450. if (dma_domain->win_arr[wnd_nr].valid) {
  451. ret = disable_domain_win(dma_domain, wnd_nr);
  452. if (!ret) {
  453. dma_domain->win_arr[wnd_nr].valid = 0;
  454. dma_domain->mapped--;
  455. }
  456. }
  457. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  458. }
  459. static int fsl_pamu_window_enable(struct iommu_domain *domain, u32 wnd_nr,
  460. phys_addr_t paddr, u64 size, int prot)
  461. {
  462. struct fsl_dma_domain *dma_domain = domain->priv;
  463. struct dma_window *wnd;
  464. int pamu_prot = 0;
  465. int ret;
  466. unsigned long flags;
  467. u64 win_size;
  468. if (prot & IOMMU_READ)
  469. pamu_prot |= PAACE_AP_PERMS_QUERY;
  470. if (prot & IOMMU_WRITE)
  471. pamu_prot |= PAACE_AP_PERMS_UPDATE;
  472. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  473. if (!dma_domain->win_arr) {
  474. pr_debug("Number of windows not configured\n");
  475. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  476. return -ENODEV;
  477. }
  478. if (wnd_nr >= dma_domain->win_cnt) {
  479. pr_debug("Invalid window index\n");
  480. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  481. return -EINVAL;
  482. }
  483. win_size = dma_domain->geom_size >> ilog2(dma_domain->win_cnt);
  484. if (size > win_size) {
  485. pr_debug("Invalid window size \n");
  486. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  487. return -EINVAL;
  488. }
  489. if (dma_domain->win_cnt == 1) {
  490. if (dma_domain->enabled) {
  491. pr_debug("Disable the window before updating the mapping\n");
  492. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  493. return -EBUSY;
  494. }
  495. ret = check_size(size, domain->geometry.aperture_start);
  496. if (ret) {
  497. pr_debug("Aperture start not aligned to the size\n");
  498. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  499. return -EINVAL;
  500. }
  501. }
  502. wnd = &dma_domain->win_arr[wnd_nr];
  503. if (!wnd->valid) {
  504. wnd->paddr = paddr;
  505. wnd->size = size;
  506. wnd->prot = pamu_prot;
  507. ret = update_domain_mapping(dma_domain, wnd_nr);
  508. if (!ret) {
  509. wnd->valid = 1;
  510. dma_domain->mapped++;
  511. }
  512. } else {
  513. pr_debug("Disable the window before updating the mapping\n");
  514. ret = -EBUSY;
  515. }
  516. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  517. return ret;
  518. }
  519. /*
  520. * Attach the LIODN to the DMA domain and configure the geometry
  521. * and window mappings.
  522. */
  523. static int handle_attach_device(struct fsl_dma_domain *dma_domain,
  524. struct device *dev, const u32 *liodn,
  525. int num)
  526. {
  527. unsigned long flags;
  528. struct iommu_domain *domain = dma_domain->iommu_domain;
  529. int ret = 0;
  530. int i;
  531. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  532. for (i = 0; i < num; i++) {
  533. /* Ensure that LIODN value is valid */
  534. if (liodn[i] >= PAACE_NUMBER_ENTRIES) {
  535. pr_debug("Invalid liodn %d, attach device failed for %s\n",
  536. liodn[i], dev->of_node->full_name);
  537. ret = -EINVAL;
  538. break;
  539. }
  540. attach_device(dma_domain, liodn[i], dev);
  541. /*
  542. * Check if geometry has already been configured
  543. * for the domain. If yes, set the geometry for
  544. * the LIODN.
  545. */
  546. if (dma_domain->win_arr) {
  547. u32 win_cnt = dma_domain->win_cnt > 1 ? dma_domain->win_cnt : 0;
  548. ret = pamu_set_liodn(liodn[i], dev, dma_domain,
  549. &domain->geometry,
  550. win_cnt);
  551. if (ret)
  552. break;
  553. if (dma_domain->mapped) {
  554. /*
  555. * Create window/subwindow mapping for
  556. * the LIODN.
  557. */
  558. ret = map_liodn(liodn[i], dma_domain);
  559. if (ret)
  560. break;
  561. }
  562. }
  563. }
  564. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  565. return ret;
  566. }
  567. static int fsl_pamu_attach_device(struct iommu_domain *domain,
  568. struct device *dev)
  569. {
  570. struct fsl_dma_domain *dma_domain = domain->priv;
  571. const u32 *liodn;
  572. u32 liodn_cnt;
  573. int len, ret = 0;
  574. struct pci_dev *pdev = NULL;
  575. struct pci_controller *pci_ctl;
  576. /*
  577. * Use LIODN of the PCI controller while attaching a
  578. * PCI device.
  579. */
  580. if (dev_is_pci(dev)) {
  581. pdev = to_pci_dev(dev);
  582. pci_ctl = pci_bus_to_host(pdev->bus);
  583. /*
  584. * make dev point to pci controller device
  585. * so we can get the LIODN programmed by
  586. * u-boot.
  587. */
  588. dev = pci_ctl->parent;
  589. }
  590. liodn = of_get_property(dev->of_node, "fsl,liodn", &len);
  591. if (liodn) {
  592. liodn_cnt = len / sizeof(u32);
  593. ret = handle_attach_device(dma_domain, dev,
  594. liodn, liodn_cnt);
  595. } else {
  596. pr_debug("missing fsl,liodn property at %s\n",
  597. dev->of_node->full_name);
  598. ret = -EINVAL;
  599. }
  600. return ret;
  601. }
  602. static void fsl_pamu_detach_device(struct iommu_domain *domain,
  603. struct device *dev)
  604. {
  605. struct fsl_dma_domain *dma_domain = domain->priv;
  606. const u32 *prop;
  607. int len;
  608. struct pci_dev *pdev = NULL;
  609. struct pci_controller *pci_ctl;
  610. /*
  611. * Use LIODN of the PCI controller while detaching a
  612. * PCI device.
  613. */
  614. if (dev_is_pci(dev)) {
  615. pdev = to_pci_dev(dev);
  616. pci_ctl = pci_bus_to_host(pdev->bus);
  617. /*
  618. * make dev point to pci controller device
  619. * so we can get the LIODN programmed by
  620. * u-boot.
  621. */
  622. dev = pci_ctl->parent;
  623. }
  624. prop = of_get_property(dev->of_node, "fsl,liodn", &len);
  625. if (prop)
  626. detach_device(dev, dma_domain);
  627. else
  628. pr_debug("missing fsl,liodn property at %s\n",
  629. dev->of_node->full_name);
  630. }
  631. static int configure_domain_geometry(struct iommu_domain *domain, void *data)
  632. {
  633. struct iommu_domain_geometry *geom_attr = data;
  634. struct fsl_dma_domain *dma_domain = domain->priv;
  635. dma_addr_t geom_size;
  636. unsigned long flags;
  637. geom_size = geom_attr->aperture_end - geom_attr->aperture_start + 1;
  638. /*
  639. * Sanity check the geometry size. Also, we do not support
  640. * DMA outside of the geometry.
  641. */
  642. if (check_size(geom_size, geom_attr->aperture_start) ||
  643. !geom_attr->force_aperture) {
  644. pr_debug("Invalid PAMU geometry attributes\n");
  645. return -EINVAL;
  646. }
  647. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  648. if (dma_domain->enabled) {
  649. pr_debug("Can't set geometry attributes as domain is active\n");
  650. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  651. return -EBUSY;
  652. }
  653. /* Copy the domain geometry information */
  654. memcpy(&domain->geometry, geom_attr,
  655. sizeof(struct iommu_domain_geometry));
  656. dma_domain->geom_size = geom_size;
  657. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  658. return 0;
  659. }
  660. /* Set the domain stash attribute */
  661. static int configure_domain_stash(struct fsl_dma_domain *dma_domain, void *data)
  662. {
  663. struct pamu_stash_attribute *stash_attr = data;
  664. unsigned long flags;
  665. int ret;
  666. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  667. memcpy(&dma_domain->dma_stash, stash_attr,
  668. sizeof(struct pamu_stash_attribute));
  669. dma_domain->stash_id = get_stash_id(stash_attr->cache,
  670. stash_attr->cpu);
  671. if (dma_domain->stash_id == ~(u32)0) {
  672. pr_debug("Invalid stash attributes\n");
  673. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  674. return -EINVAL;
  675. }
  676. ret = update_domain_stash(dma_domain, dma_domain->stash_id);
  677. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  678. return ret;
  679. }
  680. /* Configure domain dma state i.e. enable/disable DMA*/
  681. static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable)
  682. {
  683. struct device_domain_info *info;
  684. unsigned long flags;
  685. int ret;
  686. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  687. if (enable && !dma_domain->mapped) {
  688. pr_debug("Can't enable DMA domain without valid mapping\n");
  689. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  690. return -ENODEV;
  691. }
  692. dma_domain->enabled = enable;
  693. list_for_each_entry(info, &dma_domain->devices,
  694. link) {
  695. ret = (enable) ? pamu_enable_liodn(info->liodn) :
  696. pamu_disable_liodn(info->liodn);
  697. if (ret)
  698. pr_debug("Unable to set dma state for liodn %d",
  699. info->liodn);
  700. }
  701. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  702. return 0;
  703. }
  704. static int fsl_pamu_set_domain_attr(struct iommu_domain *domain,
  705. enum iommu_attr attr_type, void *data)
  706. {
  707. struct fsl_dma_domain *dma_domain = domain->priv;
  708. int ret = 0;
  709. switch (attr_type) {
  710. case DOMAIN_ATTR_GEOMETRY:
  711. ret = configure_domain_geometry(domain, data);
  712. break;
  713. case DOMAIN_ATTR_FSL_PAMU_STASH:
  714. ret = configure_domain_stash(dma_domain, data);
  715. break;
  716. case DOMAIN_ATTR_FSL_PAMU_ENABLE:
  717. ret = configure_domain_dma_state(dma_domain, *(int *)data);
  718. break;
  719. default:
  720. pr_debug("Unsupported attribute type\n");
  721. ret = -EINVAL;
  722. break;
  723. };
  724. return ret;
  725. }
  726. static int fsl_pamu_get_domain_attr(struct iommu_domain *domain,
  727. enum iommu_attr attr_type, void *data)
  728. {
  729. struct fsl_dma_domain *dma_domain = domain->priv;
  730. int ret = 0;
  731. switch (attr_type) {
  732. case DOMAIN_ATTR_FSL_PAMU_STASH:
  733. memcpy((struct pamu_stash_attribute *) data, &dma_domain->dma_stash,
  734. sizeof(struct pamu_stash_attribute));
  735. break;
  736. case DOMAIN_ATTR_FSL_PAMU_ENABLE:
  737. *(int *)data = dma_domain->enabled;
  738. break;
  739. case DOMAIN_ATTR_FSL_PAMUV1:
  740. *(int *)data = DOMAIN_ATTR_FSL_PAMUV1;
  741. break;
  742. default:
  743. pr_debug("Unsupported attribute type\n");
  744. ret = -EINVAL;
  745. break;
  746. };
  747. return ret;
  748. }
  749. #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
  750. static struct iommu_group *get_device_iommu_group(struct device *dev)
  751. {
  752. struct iommu_group *group;
  753. group = iommu_group_get(dev);
  754. if (!group)
  755. group = iommu_group_alloc();
  756. return group;
  757. }
  758. static bool check_pci_ctl_endpt_part(struct pci_controller *pci_ctl)
  759. {
  760. u32 version;
  761. /* Check the PCI controller version number by readding BRR1 register */
  762. version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2));
  763. version &= PCI_FSL_BRR1_VER;
  764. /* If PCI controller version is >= 0x204 we can partition endpoints*/
  765. if (version >= 0x204)
  766. return 1;
  767. return 0;
  768. }
  769. /* Get iommu group information from peer devices or devices on the parent bus */
  770. static struct iommu_group *get_shared_pci_device_group(struct pci_dev *pdev)
  771. {
  772. struct pci_dev *tmp;
  773. struct iommu_group *group;
  774. struct pci_bus *bus = pdev->bus;
  775. /*
  776. * Traverese the pci bus device list to get
  777. * the shared iommu group.
  778. */
  779. while (bus) {
  780. list_for_each_entry(tmp, &bus->devices, bus_list) {
  781. if (tmp == pdev)
  782. continue;
  783. group = iommu_group_get(&tmp->dev);
  784. if (group)
  785. return group;
  786. }
  787. bus = bus->parent;
  788. }
  789. return NULL;
  790. }
  791. static struct iommu_group *get_pci_device_group(struct pci_dev *pdev)
  792. {
  793. struct pci_controller *pci_ctl;
  794. bool pci_endpt_partioning;
  795. struct iommu_group *group = NULL;
  796. struct pci_dev *bridge, *dma_pdev = NULL;
  797. pci_ctl = pci_bus_to_host(pdev->bus);
  798. pci_endpt_partioning = check_pci_ctl_endpt_part(pci_ctl);
  799. /* We can partition PCIe devices so assign device group to the device */
  800. if (pci_endpt_partioning) {
  801. bridge = pci_find_upstream_pcie_bridge(pdev);
  802. if (bridge) {
  803. if (pci_is_pcie(bridge))
  804. dma_pdev = pci_get_domain_bus_and_slot(
  805. pci_domain_nr(pdev->bus),
  806. bridge->subordinate->number, 0);
  807. if (!dma_pdev)
  808. dma_pdev = pci_dev_get(bridge);
  809. } else
  810. dma_pdev = pci_dev_get(pdev);
  811. /* Account for quirked devices */
  812. swap_pci_ref(&dma_pdev, pci_get_dma_source(dma_pdev));
  813. /*
  814. * If it's a multifunction device that does not support our
  815. * required ACS flags, add to the same group as lowest numbered
  816. * function that also does not suport the required ACS flags.
  817. */
  818. if (dma_pdev->multifunction &&
  819. !pci_acs_enabled(dma_pdev, REQ_ACS_FLAGS)) {
  820. u8 i, slot = PCI_SLOT(dma_pdev->devfn);
  821. for (i = 0; i < 8; i++) {
  822. struct pci_dev *tmp;
  823. tmp = pci_get_slot(dma_pdev->bus, PCI_DEVFN(slot, i));
  824. if (!tmp)
  825. continue;
  826. if (!pci_acs_enabled(tmp, REQ_ACS_FLAGS)) {
  827. swap_pci_ref(&dma_pdev, tmp);
  828. break;
  829. }
  830. pci_dev_put(tmp);
  831. }
  832. }
  833. /*
  834. * Devices on the root bus go through the iommu. If that's not us,
  835. * find the next upstream device and test ACS up to the root bus.
  836. * Finding the next device may require skipping virtual buses.
  837. */
  838. while (!pci_is_root_bus(dma_pdev->bus)) {
  839. struct pci_bus *bus = dma_pdev->bus;
  840. while (!bus->self) {
  841. if (!pci_is_root_bus(bus))
  842. bus = bus->parent;
  843. else
  844. goto root_bus;
  845. }
  846. if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
  847. break;
  848. swap_pci_ref(&dma_pdev, pci_dev_get(bus->self));
  849. }
  850. root_bus:
  851. group = get_device_iommu_group(&dma_pdev->dev);
  852. pci_dev_put(dma_pdev);
  853. /*
  854. * PCIe controller is not a paritionable entity
  855. * free the controller device iommu_group.
  856. */
  857. if (pci_ctl->parent->iommu_group)
  858. iommu_group_remove_device(pci_ctl->parent);
  859. } else {
  860. /*
  861. * All devices connected to the controller will share the
  862. * PCI controllers device group. If this is the first
  863. * device to be probed for the pci controller, copy the
  864. * device group information from the PCI controller device
  865. * node and remove the PCI controller iommu group.
  866. * For subsequent devices, the iommu group information can
  867. * be obtained from sibling devices (i.e. from the bus_devices
  868. * link list).
  869. */
  870. if (pci_ctl->parent->iommu_group) {
  871. group = get_device_iommu_group(pci_ctl->parent);
  872. iommu_group_remove_device(pci_ctl->parent);
  873. } else
  874. group = get_shared_pci_device_group(pdev);
  875. }
  876. if (!group)
  877. group = ERR_PTR(-ENODEV);
  878. return group;
  879. }
  880. static int fsl_pamu_add_device(struct device *dev)
  881. {
  882. struct iommu_group *group = ERR_PTR(-ENODEV);
  883. struct pci_dev *pdev;
  884. const u32 *prop;
  885. int ret, len;
  886. /*
  887. * For platform devices we allocate a separate group for
  888. * each of the devices.
  889. */
  890. if (dev_is_pci(dev)) {
  891. pdev = to_pci_dev(dev);
  892. /* Don't create device groups for virtual PCI bridges */
  893. if (pdev->subordinate)
  894. return 0;
  895. group = get_pci_device_group(pdev);
  896. } else {
  897. prop = of_get_property(dev->of_node, "fsl,liodn", &len);
  898. if (prop)
  899. group = get_device_iommu_group(dev);
  900. }
  901. if (IS_ERR(group))
  902. return PTR_ERR(group);
  903. ret = iommu_group_add_device(group, dev);
  904. iommu_group_put(group);
  905. return ret;
  906. }
  907. static void fsl_pamu_remove_device(struct device *dev)
  908. {
  909. iommu_group_remove_device(dev);
  910. }
  911. static int fsl_pamu_set_windows(struct iommu_domain *domain, u32 w_count)
  912. {
  913. struct fsl_dma_domain *dma_domain = domain->priv;
  914. unsigned long flags;
  915. int ret;
  916. spin_lock_irqsave(&dma_domain->domain_lock, flags);
  917. /* Ensure domain is inactive i.e. DMA should be disabled for the domain */
  918. if (dma_domain->enabled) {
  919. pr_debug("Can't set geometry attributes as domain is active\n");
  920. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  921. return -EBUSY;
  922. }
  923. /* Ensure that the geometry has been set for the domain */
  924. if (!dma_domain->geom_size) {
  925. pr_debug("Please configure geometry before setting the number of windows\n");
  926. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  927. return -EINVAL;
  928. }
  929. /*
  930. * Ensure we have valid window count i.e. it should be less than
  931. * maximum permissible limit and should be a power of two.
  932. */
  933. if (w_count > pamu_get_max_subwin_cnt() || !is_power_of_2(w_count)) {
  934. pr_debug("Invalid window count\n");
  935. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  936. return -EINVAL;
  937. }
  938. ret = pamu_set_domain_geometry(dma_domain, &domain->geometry,
  939. ((w_count > 1) ? w_count : 0));
  940. if (!ret) {
  941. if (dma_domain->win_arr)
  942. kfree(dma_domain->win_arr);
  943. dma_domain->win_arr = kzalloc(sizeof(struct dma_window) *
  944. w_count, GFP_ATOMIC);
  945. if (!dma_domain->win_arr) {
  946. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  947. return -ENOMEM;
  948. }
  949. dma_domain->win_cnt = w_count;
  950. }
  951. spin_unlock_irqrestore(&dma_domain->domain_lock, flags);
  952. return ret;
  953. }
  954. static u32 fsl_pamu_get_windows(struct iommu_domain *domain)
  955. {
  956. struct fsl_dma_domain *dma_domain = domain->priv;
  957. return dma_domain->win_cnt;
  958. }
  959. static struct iommu_ops fsl_pamu_ops = {
  960. .domain_init = fsl_pamu_domain_init,
  961. .domain_destroy = fsl_pamu_domain_destroy,
  962. .attach_dev = fsl_pamu_attach_device,
  963. .detach_dev = fsl_pamu_detach_device,
  964. .domain_window_enable = fsl_pamu_window_enable,
  965. .domain_window_disable = fsl_pamu_window_disable,
  966. .domain_get_windows = fsl_pamu_get_windows,
  967. .domain_set_windows = fsl_pamu_set_windows,
  968. .iova_to_phys = fsl_pamu_iova_to_phys,
  969. .domain_has_cap = fsl_pamu_domain_has_cap,
  970. .domain_set_attr = fsl_pamu_set_domain_attr,
  971. .domain_get_attr = fsl_pamu_get_domain_attr,
  972. .add_device = fsl_pamu_add_device,
  973. .remove_device = fsl_pamu_remove_device,
  974. };
  975. int pamu_domain_init()
  976. {
  977. int ret = 0;
  978. ret = iommu_init_mempool();
  979. if (ret)
  980. return ret;
  981. bus_set_iommu(&platform_bus_type, &fsl_pamu_ops);
  982. bus_set_iommu(&pci_bus_type, &fsl_pamu_ops);
  983. return ret;
  984. }