fsl_pamu_domain.c 27 KB

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