iommu.c 15 KB

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