pSeries_iommu.c 15 KB

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