iommu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  3. *
  4. * Rewrite, cleanup:
  5. *
  6. * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
  7. * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
  8. *
  9. * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/mm.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/string.h>
  32. #include <linux/pci.h>
  33. #include <linux/dma-mapping.h>
  34. #include <asm/io.h>
  35. #include <asm/prom.h>
  36. #include <asm/rtas.h>
  37. #include <asm/iommu.h>
  38. #include <asm/pci-bridge.h>
  39. #include <asm/machdep.h>
  40. #include <asm/abs_addr.h>
  41. #include <asm/pSeries_reconfig.h>
  42. #include <asm/firmware.h>
  43. #include <asm/tce.h>
  44. #include <asm/ppc-pci.h>
  45. #include <asm/udbg.h>
  46. #include "plpar_wrappers.h"
  47. #define DBG(fmt...)
  48. static void tce_build_pSeries(struct iommu_table *tbl, long index,
  49. long npages, unsigned long uaddr,
  50. enum dma_data_direction direction)
  51. {
  52. u64 proto_tce;
  53. u64 *tcep;
  54. u64 rpn;
  55. index <<= TCE_PAGE_FACTOR;
  56. npages <<= TCE_PAGE_FACTOR;
  57. proto_tce = TCE_PCI_READ; // Read allowed
  58. if (direction != DMA_TO_DEVICE)
  59. proto_tce |= TCE_PCI_WRITE;
  60. tcep = ((u64 *)tbl->it_base) + index;
  61. while (npages--) {
  62. /* can't move this out since we might cross LMB boundary */
  63. rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
  64. *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
  65. uaddr += TCE_PAGE_SIZE;
  66. tcep++;
  67. }
  68. }
  69. static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
  70. {
  71. u64 *tcep;
  72. npages <<= TCE_PAGE_FACTOR;
  73. index <<= TCE_PAGE_FACTOR;
  74. tcep = ((u64 *)tbl->it_base) + index;
  75. while (npages--)
  76. *(tcep++) = 0;
  77. }
  78. static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
  79. {
  80. u64 *tcep;
  81. index <<= TCE_PAGE_FACTOR;
  82. tcep = ((u64 *)tbl->it_base) + index;
  83. return *tcep;
  84. }
  85. static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
  86. long npages, unsigned long uaddr,
  87. enum dma_data_direction direction)
  88. {
  89. u64 rc;
  90. u64 proto_tce, tce;
  91. u64 rpn;
  92. tcenum <<= TCE_PAGE_FACTOR;
  93. npages <<= TCE_PAGE_FACTOR;
  94. rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
  95. proto_tce = TCE_PCI_READ;
  96. if (direction != DMA_TO_DEVICE)
  97. proto_tce |= TCE_PCI_WRITE;
  98. while (npages--) {
  99. tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
  100. rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
  101. if (rc && printk_ratelimit()) {
  102. printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
  103. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  104. printk("\ttcenum = 0x%lx\n", (u64)tcenum);
  105. printk("\ttce val = 0x%lx\n", tce );
  106. show_stack(current, (unsigned long *)__get_SP());
  107. }
  108. tcenum++;
  109. rpn++;
  110. }
  111. }
  112. static DEFINE_PER_CPU(u64 *, tce_page) = NULL;
  113. static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
  114. long npages, unsigned long uaddr,
  115. enum dma_data_direction direction)
  116. {
  117. u64 rc;
  118. u64 proto_tce;
  119. u64 *tcep;
  120. u64 rpn;
  121. long l, limit;
  122. if (TCE_PAGE_FACTOR == 0 && npages == 1)
  123. return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
  124. direction);
  125. tcep = __get_cpu_var(tce_page);
  126. /* This is safe to do since interrupts are off when we're called
  127. * from iommu_alloc{,_sg}()
  128. */
  129. if (!tcep) {
  130. tcep = (u64 *)__get_free_page(GFP_ATOMIC);
  131. /* If allocation fails, fall back to the loop implementation */
  132. if (!tcep)
  133. return tce_build_pSeriesLP(tbl, tcenum, npages,
  134. uaddr, direction);
  135. __get_cpu_var(tce_page) = tcep;
  136. }
  137. tcenum <<= TCE_PAGE_FACTOR;
  138. npages <<= TCE_PAGE_FACTOR;
  139. rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
  140. proto_tce = TCE_PCI_READ;
  141. if (direction != DMA_TO_DEVICE)
  142. proto_tce |= TCE_PCI_WRITE;
  143. /* We can map max one pageful of TCEs at a time */
  144. do {
  145. /*
  146. * Set up the page with TCE data, looping through and setting
  147. * the values.
  148. */
  149. limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
  150. for (l = 0; l < limit; l++) {
  151. tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
  152. rpn++;
  153. }
  154. rc = plpar_tce_put_indirect((u64)tbl->it_index,
  155. (u64)tcenum << 12,
  156. (u64)virt_to_abs(tcep),
  157. limit);
  158. npages -= limit;
  159. tcenum += limit;
  160. } while (npages > 0 && !rc);
  161. if (rc && printk_ratelimit()) {
  162. printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
  163. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  164. printk("\tnpages = 0x%lx\n", (u64)npages);
  165. printk("\ttce[0] val = 0x%lx\n", tcep[0]);
  166. show_stack(current, (unsigned long *)__get_SP());
  167. }
  168. }
  169. static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
  170. {
  171. u64 rc;
  172. tcenum <<= TCE_PAGE_FACTOR;
  173. npages <<= TCE_PAGE_FACTOR;
  174. while (npages--) {
  175. rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
  176. if (rc && printk_ratelimit()) {
  177. printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
  178. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  179. printk("\ttcenum = 0x%lx\n", (u64)tcenum);
  180. show_stack(current, (unsigned long *)__get_SP());
  181. }
  182. tcenum++;
  183. }
  184. }
  185. static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
  186. {
  187. u64 rc;
  188. tcenum <<= TCE_PAGE_FACTOR;
  189. npages <<= TCE_PAGE_FACTOR;
  190. rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
  191. if (rc && printk_ratelimit()) {
  192. printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
  193. printk("\trc = %ld\n", rc);
  194. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  195. printk("\tnpages = 0x%lx\n", (u64)npages);
  196. show_stack(current, (unsigned long *)__get_SP());
  197. }
  198. }
  199. static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
  200. {
  201. u64 rc;
  202. unsigned long tce_ret;
  203. tcenum <<= TCE_PAGE_FACTOR;
  204. rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
  205. if (rc && printk_ratelimit()) {
  206. printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%ld\n",
  207. rc);
  208. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  209. printk("\ttcenum = 0x%lx\n", (u64)tcenum);
  210. show_stack(current, (unsigned long *)__get_SP());
  211. }
  212. return tce_ret;
  213. }
  214. static void iommu_table_setparms(struct pci_controller *phb,
  215. struct device_node *dn,
  216. struct iommu_table *tbl)
  217. {
  218. struct device_node *node;
  219. const unsigned long *basep;
  220. const u32 *sizep;
  221. node = (struct device_node *)phb->arch_data;
  222. basep = get_property(node, "linux,tce-base", NULL);
  223. sizep = get_property(node, "linux,tce-size", NULL);
  224. if (basep == NULL || sizep == NULL) {
  225. printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
  226. "missing tce entries !\n", dn->full_name);
  227. return;
  228. }
  229. tbl->it_base = (unsigned long)__va(*basep);
  230. #ifndef CONFIG_CRASH_DUMP
  231. memset((void *)tbl->it_base, 0, *sizep);
  232. #endif
  233. tbl->it_busno = phb->bus->number;
  234. /* Units of tce entries */
  235. tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
  236. /* Test if we are going over 2GB of DMA space */
  237. if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
  238. udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  239. panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  240. }
  241. phb->dma_window_base_cur += phb->dma_window_size;
  242. /* Set the tce table size - measured in entries */
  243. tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
  244. tbl->it_index = 0;
  245. tbl->it_blocksize = 16;
  246. tbl->it_type = TCE_PCI;
  247. }
  248. /*
  249. * iommu_table_setparms_lpar
  250. *
  251. * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
  252. */
  253. static void iommu_table_setparms_lpar(struct pci_controller *phb,
  254. struct device_node *dn,
  255. struct iommu_table *tbl,
  256. const void *dma_window)
  257. {
  258. unsigned long offset, size;
  259. tbl->it_busno = PCI_DN(dn)->bussubno;
  260. of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
  261. tbl->it_base = 0;
  262. tbl->it_blocksize = 16;
  263. tbl->it_type = TCE_PCI;
  264. tbl->it_offset = offset >> PAGE_SHIFT;
  265. tbl->it_size = size >> PAGE_SHIFT;
  266. }
  267. static void iommu_bus_setup_pSeries(struct pci_bus *bus)
  268. {
  269. struct device_node *dn;
  270. struct iommu_table *tbl;
  271. struct device_node *isa_dn, *isa_dn_orig;
  272. struct device_node *tmp;
  273. struct pci_dn *pci;
  274. int children;
  275. DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
  276. dn = pci_bus_to_OF_node(bus);
  277. pci = PCI_DN(dn);
  278. if (bus->self) {
  279. /* This is not a root bus, any setup will be done for the
  280. * device-side of the bridge in iommu_dev_setup_pSeries().
  281. */
  282. return;
  283. }
  284. /* Check if the ISA bus on the system is under
  285. * this PHB.
  286. */
  287. isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
  288. while (isa_dn && isa_dn != dn)
  289. isa_dn = isa_dn->parent;
  290. if (isa_dn_orig)
  291. of_node_put(isa_dn_orig);
  292. /* Count number of direct PCI children of the PHB. */
  293. for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
  294. children++;
  295. DBG("Children: %d\n", children);
  296. /* Calculate amount of DMA window per slot. Each window must be
  297. * a power of two (due to pci_alloc_consistent requirements).
  298. *
  299. * Keep 256MB aside for PHBs with ISA.
  300. */
  301. if (!isa_dn) {
  302. /* No ISA/IDE - just set window size and return */
  303. pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
  304. while (pci->phb->dma_window_size * children > 0x80000000ul)
  305. pci->phb->dma_window_size >>= 1;
  306. DBG("No ISA/IDE, window size is 0x%lx\n",
  307. pci->phb->dma_window_size);
  308. pci->phb->dma_window_base_cur = 0;
  309. return;
  310. }
  311. /* If we have ISA, then we probably have an IDE
  312. * controller too. Allocate a 128MB table but
  313. * skip the first 128MB to avoid stepping on ISA
  314. * space.
  315. */
  316. pci->phb->dma_window_size = 0x8000000ul;
  317. pci->phb->dma_window_base_cur = 0x8000000ul;
  318. tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  319. pci->phb->node);
  320. iommu_table_setparms(pci->phb, dn, tbl);
  321. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  322. /* Divide the rest (1.75GB) among the children */
  323. pci->phb->dma_window_size = 0x80000000ul;
  324. while (pci->phb->dma_window_size * children > 0x70000000ul)
  325. pci->phb->dma_window_size >>= 1;
  326. DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size);
  327. }
  328. static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
  329. {
  330. struct iommu_table *tbl;
  331. struct device_node *dn, *pdn;
  332. struct pci_dn *ppci;
  333. const void *dma_window = NULL;
  334. DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
  335. dn = pci_bus_to_OF_node(bus);
  336. /* Find nearest ibm,dma-window, walking up the device tree */
  337. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  338. dma_window = get_property(pdn, "ibm,dma-window", NULL);
  339. if (dma_window != NULL)
  340. break;
  341. }
  342. if (dma_window == NULL) {
  343. DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
  344. return;
  345. }
  346. ppci = PCI_DN(pdn);
  347. if (!ppci->iommu_table) {
  348. /* Bussubno hasn't been copied yet.
  349. * Do it now because iommu_table_setparms_lpar needs it.
  350. */
  351. ppci->bussubno = bus->number;
  352. tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  353. ppci->phb->node);
  354. iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
  355. ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
  356. }
  357. if (pdn != dn)
  358. PCI_DN(dn)->iommu_table = ppci->iommu_table;
  359. }
  360. static void iommu_dev_setup_pSeries(struct pci_dev *dev)
  361. {
  362. struct device_node *dn, *mydn;
  363. struct iommu_table *tbl;
  364. DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev));
  365. mydn = dn = pci_device_to_OF_node(dev);
  366. /* If we're the direct child of a root bus, then we need to allocate
  367. * an iommu table ourselves. The bus setup code should have setup
  368. * the window sizes already.
  369. */
  370. if (!dev->bus->self) {
  371. DBG(" --> first child, no bridge. Allocating iommu table.\n");
  372. tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  373. PCI_DN(dn)->phb->node);
  374. iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl);
  375. PCI_DN(dn)->iommu_table = iommu_init_table(tbl,
  376. PCI_DN(dn)->phb->node);
  377. return;
  378. }
  379. /* If this device is further down the bus tree, search upwards until
  380. * an already allocated iommu table is found and use that.
  381. */
  382. while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
  383. dn = dn->parent;
  384. if (dn && PCI_DN(dn)) {
  385. PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table;
  386. } else {
  387. DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev));
  388. }
  389. }
  390. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  391. {
  392. int err = NOTIFY_OK;
  393. struct device_node *np = node;
  394. struct pci_dn *pci = PCI_DN(np);
  395. switch (action) {
  396. case PSERIES_RECONFIG_REMOVE:
  397. if (pci && pci->iommu_table &&
  398. get_property(np, "ibm,dma-window", NULL))
  399. iommu_free_table(np);
  400. break;
  401. default:
  402. err = NOTIFY_DONE;
  403. break;
  404. }
  405. return err;
  406. }
  407. static struct notifier_block iommu_reconfig_nb = {
  408. .notifier_call = iommu_reconfig_notifier,
  409. };
  410. static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
  411. {
  412. struct device_node *pdn, *dn;
  413. struct iommu_table *tbl;
  414. const void *dma_window = NULL;
  415. struct pci_dn *pci;
  416. DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev));
  417. /* dev setup for LPAR is a little tricky, since the device tree might
  418. * contain the dma-window properties per-device and not neccesarily
  419. * for the bus. So we need to search upwards in the tree until we
  420. * either hit a dma-window property, OR find a parent with a table
  421. * already allocated.
  422. */
  423. dn = pci_device_to_OF_node(dev);
  424. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  425. pdn = pdn->parent) {
  426. dma_window = get_property(pdn, "ibm,dma-window", NULL);
  427. if (dma_window)
  428. break;
  429. }
  430. /* Check for parent == NULL so we don't try to setup the empty EADS
  431. * slots on POWER4 machines.
  432. */
  433. if (dma_window == NULL || pdn->parent == NULL) {
  434. DBG("No dma window for device, linking to parent\n");
  435. PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table;
  436. return;
  437. } else {
  438. DBG("Found DMA window, allocating table\n");
  439. }
  440. pci = PCI_DN(pdn);
  441. if (!pci->iommu_table) {
  442. /* iommu_table_setparms_lpar needs bussubno. */
  443. pci->bussubno = pci->phb->bus->number;
  444. tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  445. pci->phb->node);
  446. iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
  447. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  448. }
  449. if (pdn != dn)
  450. PCI_DN(dn)->iommu_table = pci->iommu_table;
  451. }
  452. static void iommu_bus_setup_null(struct pci_bus *b) { }
  453. static void iommu_dev_setup_null(struct pci_dev *d) { }
  454. /* These are called very early. */
  455. void iommu_init_early_pSeries(void)
  456. {
  457. if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
  458. /* Direct I/O, IOMMU off */
  459. ppc_md.iommu_dev_setup = iommu_dev_setup_null;
  460. ppc_md.iommu_bus_setup = iommu_bus_setup_null;
  461. pci_direct_iommu_init();
  462. return;
  463. }
  464. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  465. if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
  466. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  467. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  468. } else {
  469. ppc_md.tce_build = tce_build_pSeriesLP;
  470. ppc_md.tce_free = tce_free_pSeriesLP;
  471. }
  472. ppc_md.tce_get = tce_get_pSeriesLP;
  473. ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
  474. ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
  475. } else {
  476. ppc_md.tce_build = tce_build_pSeries;
  477. ppc_md.tce_free = tce_free_pSeries;
  478. ppc_md.tce_get = tce_get_pseries;
  479. ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
  480. ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
  481. }
  482. pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
  483. pci_iommu_init();
  484. }