fsl_pamu_domain.c 28 KB

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