pSeries_iommu.c 15 KB

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