iommu.c 16 KB

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