pSeries_iommu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 "pci.h"
  48. #define DBG(fmt...)
  49. extern int is_python(struct device_node *);
  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. t.te_word = 0;
  57. t.te_rdwr = 1; // Read allowed
  58. if (direction != DMA_TO_DEVICE)
  59. t.te_pciwr = 1;
  60. tp = ((union tce_entry *)tbl->it_base) + index;
  61. while (npages--) {
  62. /* can't move this out since we might cross LMB boundary */
  63. t.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
  64. tp->te_word = t.te_word;
  65. uaddr += PAGE_SIZE;
  66. tp++;
  67. }
  68. }
  69. static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
  70. {
  71. union tce_entry t;
  72. union tce_entry *tp;
  73. t.te_word = 0;
  74. tp = ((union tce_entry *)tbl->it_base) + index;
  75. while (npages--) {
  76. tp->te_word = t.te_word;
  77. tp++;
  78. }
  79. }
  80. static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
  81. long npages, unsigned long uaddr,
  82. enum dma_data_direction direction)
  83. {
  84. u64 rc;
  85. union tce_entry tce;
  86. tce.te_word = 0;
  87. tce.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
  88. tce.te_rdwr = 1;
  89. if (direction != DMA_TO_DEVICE)
  90. tce.te_pciwr = 1;
  91. while (npages--) {
  92. rc = plpar_tce_put((u64)tbl->it_index,
  93. (u64)tcenum << 12,
  94. tce.te_word );
  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.te_word );
  100. show_stack(current, (unsigned long *)__get_SP());
  101. }
  102. tcenum++;
  103. tce.te_rpn++;
  104. }
  105. }
  106. static DEFINE_PER_CPU(void *, 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. union tce_entry tce, *tcep;
  113. long l, limit;
  114. if (npages == 1)
  115. return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
  116. direction);
  117. tcep = __get_cpu_var(tce_page);
  118. /* This is safe to do since interrupts are off when we're called
  119. * from iommu_alloc{,_sg}()
  120. */
  121. if (!tcep) {
  122. tcep = (void *)__get_free_page(GFP_ATOMIC);
  123. /* If allocation fails, fall back to the loop implementation */
  124. if (!tcep)
  125. return tce_build_pSeriesLP(tbl, tcenum, npages,
  126. uaddr, direction);
  127. __get_cpu_var(tce_page) = tcep;
  128. }
  129. tce.te_word = 0;
  130. tce.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
  131. tce.te_rdwr = 1;
  132. if (direction != DMA_TO_DEVICE)
  133. tce.te_pciwr = 1;
  134. /* We can map max one pageful of TCEs at a time */
  135. do {
  136. /*
  137. * Set up the page with TCE data, looping through and setting
  138. * the values.
  139. */
  140. limit = min_t(long, npages, PAGE_SIZE/sizeof(union tce_entry));
  141. for (l = 0; l < limit; l++) {
  142. tcep[l] = tce;
  143. tce.te_rpn++;
  144. }
  145. rc = plpar_tce_put_indirect((u64)tbl->it_index,
  146. (u64)tcenum << 12,
  147. (u64)virt_to_abs(tcep),
  148. limit);
  149. npages -= limit;
  150. tcenum += limit;
  151. } while (npages > 0 && !rc);
  152. if (rc && printk_ratelimit()) {
  153. printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
  154. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  155. printk("\tnpages = 0x%lx\n", (u64)npages);
  156. printk("\ttce[0] val = 0x%lx\n", tcep[0].te_word);
  157. show_stack(current, (unsigned long *)__get_SP());
  158. }
  159. }
  160. static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
  161. {
  162. u64 rc;
  163. union tce_entry tce;
  164. tce.te_word = 0;
  165. while (npages--) {
  166. rc = plpar_tce_put((u64)tbl->it_index,
  167. (u64)tcenum << 12,
  168. tce.te_word);
  169. if (rc && printk_ratelimit()) {
  170. printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
  171. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  172. printk("\ttcenum = 0x%lx\n", (u64)tcenum);
  173. printk("\ttce val = 0x%lx\n", tce.te_word );
  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. union tce_entry tce;
  183. tce.te_word = 0;
  184. rc = plpar_tce_stuff((u64)tbl->it_index,
  185. (u64)tcenum << 12,
  186. tce.te_word,
  187. npages);
  188. if (rc && printk_ratelimit()) {
  189. printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
  190. printk("\trc = %ld\n", rc);
  191. printk("\tindex = 0x%lx\n", (u64)tbl->it_index);
  192. printk("\tnpages = 0x%lx\n", (u64)npages);
  193. printk("\ttce val = 0x%lx\n", tce.te_word );
  194. show_stack(current, (unsigned long *)__get_SP());
  195. }
  196. }
  197. static void iommu_table_setparms(struct pci_controller *phb,
  198. struct device_node *dn,
  199. struct iommu_table *tbl)
  200. {
  201. struct device_node *node;
  202. unsigned long *basep;
  203. unsigned int *sizep;
  204. node = (struct device_node *)phb->arch_data;
  205. basep = (unsigned long *)get_property(node, "linux,tce-base", NULL);
  206. sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL);
  207. if (basep == NULL || sizep == NULL) {
  208. printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
  209. "missing tce entries !\n", dn->full_name);
  210. return;
  211. }
  212. tbl->it_base = (unsigned long)__va(*basep);
  213. memset((void *)tbl->it_base, 0, *sizep);
  214. tbl->it_busno = phb->bus->number;
  215. /* Units of tce entries */
  216. tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
  217. /* Test if we are going over 2GB of DMA space */
  218. if (phb->dma_window_base_cur + phb->dma_window_size > (1L << 31))
  219. panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  220. phb->dma_window_base_cur += phb->dma_window_size;
  221. /* Set the tce table size - measured in entries */
  222. tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
  223. tbl->it_index = 0;
  224. tbl->it_blocksize = 16;
  225. tbl->it_type = TCE_PCI;
  226. }
  227. /*
  228. * iommu_table_setparms_lpar
  229. *
  230. * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
  231. *
  232. * ToDo: properly interpret the ibm,dma-window property. The definition is:
  233. * logical-bus-number (1 word)
  234. * phys-address (#address-cells words)
  235. * size (#cell-size words)
  236. *
  237. * Currently we hard code these sizes (more or less).
  238. */
  239. static void iommu_table_setparms_lpar(struct pci_controller *phb,
  240. struct device_node *dn,
  241. struct iommu_table *tbl,
  242. unsigned int *dma_window)
  243. {
  244. tbl->it_busno = dn->bussubno;
  245. /* TODO: Parse field size properties properly. */
  246. tbl->it_size = (((unsigned long)dma_window[4] << 32) |
  247. (unsigned long)dma_window[5]) >> PAGE_SHIFT;
  248. tbl->it_offset = (((unsigned long)dma_window[2] << 32) |
  249. (unsigned long)dma_window[3]) >> PAGE_SHIFT;
  250. tbl->it_base = 0;
  251. tbl->it_index = dma_window[0];
  252. tbl->it_blocksize = 16;
  253. tbl->it_type = TCE_PCI;
  254. }
  255. static void iommu_bus_setup_pSeries(struct pci_bus *bus)
  256. {
  257. struct device_node *dn, *pdn;
  258. struct iommu_table *tbl;
  259. DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
  260. /* For each (root) bus, we carve up the available DMA space in 256MB
  261. * pieces. Since each piece is used by one (sub) bus/device, that would
  262. * give a maximum of 7 devices per PHB. In most cases, this is plenty.
  263. *
  264. * The exception is on Python PHBs (pre-POWER4). Here we don't have EADS
  265. * bridges below the PHB to allocate the sectioned tables to, so instead
  266. * we allocate a 1GB table at the PHB level.
  267. */
  268. dn = pci_bus_to_OF_node(bus);
  269. if (!bus->self) {
  270. /* Root bus */
  271. if (is_python(dn)) {
  272. unsigned int *iohole;
  273. DBG("Python root bus %s\n", bus->name);
  274. iohole = (unsigned int *)get_property(dn, "io-hole", 0);
  275. if (iohole) {
  276. /* On first bus we need to leave room for the
  277. * ISA address space. Just skip the first 256MB
  278. * alltogether. This leaves 768MB for the window.
  279. */
  280. DBG("PHB has io-hole, reserving 256MB\n");
  281. dn->phb->dma_window_size = 3 << 28;
  282. dn->phb->dma_window_base_cur = 1 << 28;
  283. } else {
  284. /* 1GB window by default */
  285. dn->phb->dma_window_size = 1 << 30;
  286. dn->phb->dma_window_base_cur = 0;
  287. }
  288. tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  289. iommu_table_setparms(dn->phb, dn, tbl);
  290. dn->iommu_table = iommu_init_table(tbl);
  291. } else {
  292. /* Do a 128MB table at root. This is used for the IDE
  293. * controller on some SMP-mode POWER4 machines. It
  294. * doesn't hurt to allocate it on other machines
  295. * -- it'll just be unused since new tables are
  296. * allocated on the EADS level.
  297. *
  298. * Allocate at offset 128MB to avoid having to deal
  299. * with ISA holes; 128MB table for IDE is plenty.
  300. */
  301. dn->phb->dma_window_size = 1 << 27;
  302. dn->phb->dma_window_base_cur = 1 << 27;
  303. tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  304. iommu_table_setparms(dn->phb, dn, tbl);
  305. dn->iommu_table = iommu_init_table(tbl);
  306. /* All child buses have 256MB tables */
  307. dn->phb->dma_window_size = 1 << 28;
  308. }
  309. } else {
  310. pdn = pci_bus_to_OF_node(bus->parent);
  311. if (!bus->parent->self && !is_python(pdn)) {
  312. struct iommu_table *tbl;
  313. /* First child and not python means this is the EADS
  314. * level. Allocate new table for this slot with 256MB
  315. * window.
  316. */
  317. tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  318. iommu_table_setparms(dn->phb, dn, tbl);
  319. dn->iommu_table = iommu_init_table(tbl);
  320. } else {
  321. /* Lower than first child or under python, use parent table */
  322. dn->iommu_table = pdn->iommu_table;
  323. }
  324. }
  325. }
  326. static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
  327. {
  328. struct iommu_table *tbl;
  329. struct device_node *dn, *pdn;
  330. unsigned int *dma_window = NULL;
  331. DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
  332. dn = pci_bus_to_OF_node(bus);
  333. /* Find nearest ibm,dma-window, walking up the device tree */
  334. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  335. dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL);
  336. if (dma_window != NULL)
  337. break;
  338. }
  339. if (dma_window == NULL) {
  340. DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
  341. return;
  342. }
  343. if (!pdn->iommu_table) {
  344. /* Bussubno hasn't been copied yet.
  345. * Do it now because iommu_table_setparms_lpar needs it.
  346. */
  347. pdn->bussubno = bus->number;
  348. tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
  349. GFP_KERNEL);
  350. iommu_table_setparms_lpar(pdn->phb, pdn, tbl, dma_window);
  351. pdn->iommu_table = iommu_init_table(tbl);
  352. }
  353. if (pdn != dn)
  354. dn->iommu_table = pdn->iommu_table;
  355. }
  356. static void iommu_dev_setup_pSeries(struct pci_dev *dev)
  357. {
  358. struct device_node *dn, *mydn;
  359. DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, dev->pretty_name);
  360. /* Now copy the iommu_table ptr from the bus device down to the
  361. * pci device_node. This means get_iommu_table() won't need to search
  362. * up the device tree to find it.
  363. */
  364. mydn = dn = pci_device_to_OF_node(dev);
  365. while (dn && dn->iommu_table == NULL)
  366. dn = dn->parent;
  367. if (dn) {
  368. mydn->iommu_table = dn->iommu_table;
  369. } else {
  370. DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, dev->pretty_name);
  371. }
  372. }
  373. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  374. {
  375. int err = NOTIFY_OK;
  376. struct device_node *np = node;
  377. switch (action) {
  378. case PSERIES_RECONFIG_REMOVE:
  379. if (np->iommu_table &&
  380. get_property(np, "ibm,dma-window", NULL))
  381. iommu_free_table(np);
  382. break;
  383. default:
  384. err = NOTIFY_DONE;
  385. break;
  386. }
  387. return err;
  388. }
  389. static struct notifier_block iommu_reconfig_nb = {
  390. .notifier_call = iommu_reconfig_notifier,
  391. };
  392. static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
  393. {
  394. struct device_node *pdn, *dn;
  395. struct iommu_table *tbl;
  396. int *dma_window = NULL;
  397. DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, dev->pretty_name);
  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 && !pdn->iommu_table; pdn = pdn->parent) {
  406. dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL);
  407. if (dma_window)
  408. break;
  409. }
  410. /* Check for parent == NULL so we don't try to setup the empty EADS
  411. * slots on POWER4 machines.
  412. */
  413. if (dma_window == NULL || pdn->parent == NULL) {
  414. /* Fall back to regular (non-LPAR) dev setup */
  415. DBG("No dma window for device, falling back to regular setup\n");
  416. iommu_dev_setup_pSeries(dev);
  417. return;
  418. } else {
  419. DBG("Found DMA window, allocating table\n");
  420. }
  421. if (!pdn->iommu_table) {
  422. /* iommu_table_setparms_lpar needs bussubno. */
  423. pdn->bussubno = pdn->phb->bus->number;
  424. tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
  425. GFP_KERNEL);
  426. iommu_table_setparms_lpar(pdn->phb, pdn, tbl, dma_window);
  427. pdn->iommu_table = iommu_init_table(tbl);
  428. }
  429. if (pdn != dn)
  430. dn->iommu_table = pdn->iommu_table;
  431. }
  432. static void iommu_bus_setup_null(struct pci_bus *b) { }
  433. static void iommu_dev_setup_null(struct pci_dev *d) { }
  434. /* These are called very early. */
  435. void iommu_init_early_pSeries(void)
  436. {
  437. if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
  438. /* Direct I/O, IOMMU off */
  439. ppc_md.iommu_dev_setup = iommu_dev_setup_null;
  440. ppc_md.iommu_bus_setup = iommu_bus_setup_null;
  441. pci_direct_iommu_init();
  442. return;
  443. }
  444. if (systemcfg->platform & PLATFORM_LPAR) {
  445. if (cur_cpu_spec->firmware_features & FW_FEATURE_MULTITCE) {
  446. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  447. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  448. } else {
  449. ppc_md.tce_build = tce_build_pSeriesLP;
  450. ppc_md.tce_free = tce_free_pSeriesLP;
  451. }
  452. ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
  453. ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
  454. } else {
  455. ppc_md.tce_build = tce_build_pSeries;
  456. ppc_md.tce_free = tce_free_pSeries;
  457. ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
  458. ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
  459. }
  460. pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
  461. pci_iommu_init();
  462. }