pSeries_iommu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 > (1L << 31))
  220. panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  221. phb->dma_window_base_cur += phb->dma_window_size;
  222. /* Set the tce table size - measured in entries */
  223. tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
  224. tbl->it_index = 0;
  225. tbl->it_blocksize = 16;
  226. tbl->it_type = TCE_PCI;
  227. }
  228. /*
  229. * iommu_table_setparms_lpar
  230. *
  231. * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
  232. *
  233. * ToDo: properly interpret the ibm,dma-window property. The definition is:
  234. * logical-bus-number (1 word)
  235. * phys-address (#address-cells words)
  236. * size (#cell-size words)
  237. *
  238. * Currently we hard code these sizes (more or less).
  239. */
  240. static void iommu_table_setparms_lpar(struct pci_controller *phb,
  241. struct device_node *dn,
  242. struct iommu_table *tbl,
  243. unsigned int *dma_window)
  244. {
  245. tbl->it_busno = dn->bussubno;
  246. /* TODO: Parse field size properties properly. */
  247. tbl->it_size = (((unsigned long)dma_window[4] << 32) |
  248. (unsigned long)dma_window[5]) >> PAGE_SHIFT;
  249. tbl->it_offset = (((unsigned long)dma_window[2] << 32) |
  250. (unsigned long)dma_window[3]) >> PAGE_SHIFT;
  251. tbl->it_base = 0;
  252. tbl->it_index = dma_window[0];
  253. tbl->it_blocksize = 16;
  254. tbl->it_type = TCE_PCI;
  255. }
  256. static void iommu_bus_setup_pSeries(struct pci_bus *bus)
  257. {
  258. struct device_node *dn, *pdn;
  259. struct iommu_table *tbl;
  260. DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
  261. /* For each (root) bus, we carve up the available DMA space in 256MB
  262. * pieces. Since each piece is used by one (sub) bus/device, that would
  263. * give a maximum of 7 devices per PHB. In most cases, this is plenty.
  264. *
  265. * The exception is on Python PHBs (pre-POWER4). Here we don't have EADS
  266. * bridges below the PHB to allocate the sectioned tables to, so instead
  267. * we allocate a 1GB table at the PHB level.
  268. */
  269. dn = pci_bus_to_OF_node(bus);
  270. if (!bus->self) {
  271. /* Root bus */
  272. if (is_python(dn)) {
  273. unsigned int *iohole;
  274. DBG("Python root bus %s\n", bus->name);
  275. iohole = (unsigned int *)get_property(dn, "io-hole", 0);
  276. if (iohole) {
  277. /* On first bus we need to leave room for the
  278. * ISA address space. Just skip the first 256MB
  279. * alltogether. This leaves 768MB for the window.
  280. */
  281. DBG("PHB has io-hole, reserving 256MB\n");
  282. dn->phb->dma_window_size = 3 << 28;
  283. dn->phb->dma_window_base_cur = 1 << 28;
  284. } else {
  285. /* 1GB window by default */
  286. dn->phb->dma_window_size = 1 << 30;
  287. dn->phb->dma_window_base_cur = 0;
  288. }
  289. tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  290. iommu_table_setparms(dn->phb, dn, tbl);
  291. dn->iommu_table = iommu_init_table(tbl);
  292. } else {
  293. /* Do a 128MB table at root. This is used for the IDE
  294. * controller on some SMP-mode POWER4 machines. It
  295. * doesn't hurt to allocate it on other machines
  296. * -- it'll just be unused since new tables are
  297. * allocated on the EADS level.
  298. *
  299. * Allocate at offset 128MB to avoid having to deal
  300. * with ISA holes; 128MB table for IDE is plenty.
  301. */
  302. dn->phb->dma_window_size = 1 << 27;
  303. dn->phb->dma_window_base_cur = 1 << 27;
  304. tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  305. iommu_table_setparms(dn->phb, dn, tbl);
  306. dn->iommu_table = iommu_init_table(tbl);
  307. /* All child buses have 256MB tables */
  308. dn->phb->dma_window_size = 1 << 28;
  309. }
  310. } else {
  311. pdn = pci_bus_to_OF_node(bus->parent);
  312. if (!bus->parent->self && !is_python(pdn)) {
  313. struct iommu_table *tbl;
  314. /* First child and not python means this is the EADS
  315. * level. Allocate new table for this slot with 256MB
  316. * window.
  317. */
  318. tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL);
  319. iommu_table_setparms(dn->phb, dn, tbl);
  320. dn->iommu_table = iommu_init_table(tbl);
  321. } else {
  322. /* Lower than first child or under python, use parent table */
  323. dn->iommu_table = pdn->iommu_table;
  324. }
  325. }
  326. }
  327. static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
  328. {
  329. struct iommu_table *tbl;
  330. struct device_node *dn, *pdn;
  331. unsigned int *dma_window = NULL;
  332. DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
  333. dn = pci_bus_to_OF_node(bus);
  334. /* Find nearest ibm,dma-window, walking up the device tree */
  335. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  336. dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL);
  337. if (dma_window != NULL)
  338. break;
  339. }
  340. if (dma_window == NULL) {
  341. DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
  342. return;
  343. }
  344. if (!pdn->iommu_table) {
  345. /* Bussubno hasn't been copied yet.
  346. * Do it now because iommu_table_setparms_lpar needs it.
  347. */
  348. pdn->bussubno = bus->number;
  349. tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
  350. GFP_KERNEL);
  351. iommu_table_setparms_lpar(pdn->phb, pdn, tbl, dma_window);
  352. pdn->iommu_table = iommu_init_table(tbl);
  353. }
  354. if (pdn != dn)
  355. dn->iommu_table = pdn->iommu_table;
  356. }
  357. static void iommu_dev_setup_pSeries(struct pci_dev *dev)
  358. {
  359. struct device_node *dn, *mydn;
  360. DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, dev->pretty_name);
  361. /* Now copy the iommu_table ptr from the bus device down to the
  362. * pci device_node. This means get_iommu_table() won't need to search
  363. * up the device tree to find it.
  364. */
  365. mydn = dn = pci_device_to_OF_node(dev);
  366. while (dn && dn->iommu_table == NULL)
  367. dn = dn->parent;
  368. if (dn) {
  369. mydn->iommu_table = dn->iommu_table;
  370. } else {
  371. DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, dev->pretty_name);
  372. }
  373. }
  374. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  375. {
  376. int err = NOTIFY_OK;
  377. struct device_node *np = node;
  378. switch (action) {
  379. case PSERIES_RECONFIG_REMOVE:
  380. if (np->iommu_table &&
  381. get_property(np, "ibm,dma-window", NULL))
  382. iommu_free_table(np);
  383. break;
  384. default:
  385. err = NOTIFY_DONE;
  386. break;
  387. }
  388. return err;
  389. }
  390. static struct notifier_block iommu_reconfig_nb = {
  391. .notifier_call = iommu_reconfig_notifier,
  392. };
  393. static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
  394. {
  395. struct device_node *pdn, *dn;
  396. struct iommu_table *tbl;
  397. int *dma_window = NULL;
  398. DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, dev->pretty_name);
  399. /* dev setup for LPAR is a little tricky, since the device tree might
  400. * contain the dma-window properties per-device and not neccesarily
  401. * for the bus. So we need to search upwards in the tree until we
  402. * either hit a dma-window property, OR find a parent with a table
  403. * already allocated.
  404. */
  405. dn = pci_device_to_OF_node(dev);
  406. for (pdn = dn; pdn && !pdn->iommu_table; pdn = pdn->parent) {
  407. dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL);
  408. if (dma_window)
  409. break;
  410. }
  411. /* Check for parent == NULL so we don't try to setup the empty EADS
  412. * slots on POWER4 machines.
  413. */
  414. if (dma_window == NULL || pdn->parent == NULL) {
  415. /* Fall back to regular (non-LPAR) dev setup */
  416. DBG("No dma window for device, falling back to regular setup\n");
  417. iommu_dev_setup_pSeries(dev);
  418. return;
  419. } else {
  420. DBG("Found DMA window, allocating table\n");
  421. }
  422. if (!pdn->iommu_table) {
  423. /* iommu_table_setparms_lpar needs bussubno. */
  424. pdn->bussubno = pdn->phb->bus->number;
  425. tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table),
  426. GFP_KERNEL);
  427. iommu_table_setparms_lpar(pdn->phb, pdn, tbl, dma_window);
  428. pdn->iommu_table = iommu_init_table(tbl);
  429. }
  430. if (pdn != dn)
  431. dn->iommu_table = pdn->iommu_table;
  432. }
  433. static void iommu_bus_setup_null(struct pci_bus *b) { }
  434. static void iommu_dev_setup_null(struct pci_dev *d) { }
  435. /* These are called very early. */
  436. void iommu_init_early_pSeries(void)
  437. {
  438. if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
  439. /* Direct I/O, IOMMU off */
  440. ppc_md.iommu_dev_setup = iommu_dev_setup_null;
  441. ppc_md.iommu_bus_setup = iommu_bus_setup_null;
  442. pci_direct_iommu_init();
  443. return;
  444. }
  445. if (systemcfg->platform & PLATFORM_LPAR) {
  446. if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
  447. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  448. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  449. } else {
  450. ppc_md.tce_build = tce_build_pSeriesLP;
  451. ppc_md.tce_free = tce_free_pSeriesLP;
  452. }
  453. ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
  454. ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
  455. } else {
  456. ppc_md.tce_build = tce_build_pSeries;
  457. ppc_md.tce_free = tce_free_pSeries;
  458. ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
  459. ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
  460. }
  461. pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
  462. pci_iommu_init();
  463. }