iommu.c 16 KB

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