pSeries_iommu.c 16 KB

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