iommu.c 16 KB

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