pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. *
  7. * The System z PCI code is a rewrite from a prototype by
  8. * the following people (Kudoz!):
  9. * Alexander Schmidt
  10. * Christoph Raisch
  11. * Hannes Hering
  12. * Hoang-Nam Nguyen
  13. * Jan-Bernd Themann
  14. * Stefan Roscher
  15. * Thomas Klein
  16. */
  17. #define KMSG_COMPONENT "zpci"
  18. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/export.h>
  23. #include <linux/delay.h>
  24. #include <linux/irq.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/pci.h>
  28. #include <linux/msi.h>
  29. #include <asm/isc.h>
  30. #include <asm/airq.h>
  31. #include <asm/facility.h>
  32. #include <asm/pci_insn.h>
  33. #include <asm/pci_clp.h>
  34. #include <asm/pci_dma.h>
  35. #define DEBUG /* enable pr_debug */
  36. #define SIC_IRQ_MODE_ALL 0
  37. #define SIC_IRQ_MODE_SINGLE 1
  38. #define ZPCI_NR_DMA_SPACES 1
  39. #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
  40. /* list of all detected zpci devices */
  41. static LIST_HEAD(zpci_list);
  42. static DEFINE_SPINLOCK(zpci_list_lock);
  43. static struct irq_chip zpci_irq_chip = {
  44. .name = "zPCI",
  45. .irq_unmask = pci_msi_unmask_irq,
  46. .irq_mask = pci_msi_mask_irq,
  47. };
  48. static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
  49. static DEFINE_SPINLOCK(zpci_domain_lock);
  50. static struct airq_iv *zpci_aisb_iv;
  51. static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES];
  52. /* Adapter interrupt definitions */
  53. static void zpci_irq_handler(struct airq_struct *airq);
  54. static struct airq_struct zpci_airq = {
  55. .handler = zpci_irq_handler,
  56. .isc = PCI_ISC,
  57. };
  58. /* I/O Map */
  59. static DEFINE_SPINLOCK(zpci_iomap_lock);
  60. static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  61. struct zpci_iomap_entry *zpci_iomap_start;
  62. EXPORT_SYMBOL_GPL(zpci_iomap_start);
  63. static struct kmem_cache *zdev_fmb_cache;
  64. struct zpci_dev *get_zdev_by_fid(u32 fid)
  65. {
  66. struct zpci_dev *tmp, *zdev = NULL;
  67. spin_lock(&zpci_list_lock);
  68. list_for_each_entry(tmp, &zpci_list, entry) {
  69. if (tmp->fid == fid) {
  70. zdev = tmp;
  71. break;
  72. }
  73. }
  74. spin_unlock(&zpci_list_lock);
  75. return zdev;
  76. }
  77. static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
  78. {
  79. return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
  80. }
  81. int pci_domain_nr(struct pci_bus *bus)
  82. {
  83. return ((struct zpci_dev *) bus->sysdata)->domain;
  84. }
  85. EXPORT_SYMBOL_GPL(pci_domain_nr);
  86. int pci_proc_domain(struct pci_bus *bus)
  87. {
  88. return pci_domain_nr(bus);
  89. }
  90. EXPORT_SYMBOL_GPL(pci_proc_domain);
  91. /* Modify PCI: Register adapter interruptions */
  92. static int zpci_set_airq(struct zpci_dev *zdev)
  93. {
  94. u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
  95. struct zpci_fib fib = {0};
  96. fib.isc = PCI_ISC;
  97. fib.sum = 1; /* enable summary notifications */
  98. fib.noi = airq_iv_end(zdev->aibv);
  99. fib.aibv = (unsigned long) zdev->aibv->vector;
  100. fib.aibvo = 0; /* each zdev has its own interrupt vector */
  101. fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8;
  102. fib.aisbo = zdev->aisb & 63;
  103. return zpci_mod_fc(req, &fib);
  104. }
  105. struct mod_pci_args {
  106. u64 base;
  107. u64 limit;
  108. u64 iota;
  109. u64 fmb_addr;
  110. };
  111. static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
  112. {
  113. u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
  114. struct zpci_fib fib = {0};
  115. fib.pba = args->base;
  116. fib.pal = args->limit;
  117. fib.iota = args->iota;
  118. fib.fmb_addr = args->fmb_addr;
  119. return zpci_mod_fc(req, &fib);
  120. }
  121. /* Modify PCI: Register I/O address translation parameters */
  122. int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
  123. u64 base, u64 limit, u64 iota)
  124. {
  125. struct mod_pci_args args = { base, limit, iota, 0 };
  126. WARN_ON_ONCE(iota & 0x3fff);
  127. args.iota |= ZPCI_IOTA_RTTO_FLAG;
  128. return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
  129. }
  130. /* Modify PCI: Unregister I/O address translation parameters */
  131. int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
  132. {
  133. struct mod_pci_args args = { 0, 0, 0, 0 };
  134. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
  135. }
  136. /* Modify PCI: Unregister adapter interruptions */
  137. static int zpci_clear_airq(struct zpci_dev *zdev)
  138. {
  139. struct mod_pci_args args = { 0, 0, 0, 0 };
  140. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
  141. }
  142. /* Modify PCI: Set PCI function measurement parameters */
  143. int zpci_fmb_enable_device(struct zpci_dev *zdev)
  144. {
  145. struct mod_pci_args args = { 0, 0, 0, 0 };
  146. if (zdev->fmb)
  147. return -EINVAL;
  148. zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
  149. if (!zdev->fmb)
  150. return -ENOMEM;
  151. WARN_ON((u64) zdev->fmb & 0xf);
  152. /* reset software counters */
  153. atomic64_set(&zdev->allocated_pages, 0);
  154. atomic64_set(&zdev->mapped_pages, 0);
  155. atomic64_set(&zdev->unmapped_pages, 0);
  156. args.fmb_addr = virt_to_phys(zdev->fmb);
  157. return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  158. }
  159. /* Modify PCI: Disable PCI function measurement */
  160. int zpci_fmb_disable_device(struct zpci_dev *zdev)
  161. {
  162. struct mod_pci_args args = { 0, 0, 0, 0 };
  163. int rc;
  164. if (!zdev->fmb)
  165. return -EINVAL;
  166. /* Function measurement is disabled if fmb address is zero */
  167. rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  168. kmem_cache_free(zdev_fmb_cache, zdev->fmb);
  169. zdev->fmb = NULL;
  170. return rc;
  171. }
  172. #define ZPCI_PCIAS_CFGSPC 15
  173. static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
  174. {
  175. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  176. u64 data;
  177. int rc;
  178. rc = zpci_load(&data, req, offset);
  179. if (!rc) {
  180. data = data << ((8 - len) * 8);
  181. data = le64_to_cpu(data);
  182. *val = (u32) data;
  183. } else
  184. *val = 0xffffffff;
  185. return rc;
  186. }
  187. static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
  188. {
  189. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  190. u64 data = val;
  191. int rc;
  192. data = cpu_to_le64(data);
  193. data = data >> ((8 - len) * 8);
  194. rc = zpci_store(data, req, offset);
  195. return rc;
  196. }
  197. void pcibios_fixup_bus(struct pci_bus *bus)
  198. {
  199. }
  200. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  201. resource_size_t size,
  202. resource_size_t align)
  203. {
  204. return 0;
  205. }
  206. /* combine single writes by using store-block insn */
  207. void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
  208. {
  209. zpci_memcpy_toio(to, from, count);
  210. }
  211. /* Create a virtual mapping cookie for a PCI BAR */
  212. void __iomem *pci_iomap_range(struct pci_dev *pdev,
  213. int bar,
  214. unsigned long offset,
  215. unsigned long max)
  216. {
  217. struct zpci_dev *zdev = to_zpci(pdev);
  218. u64 addr;
  219. int idx;
  220. if ((bar & 7) != bar)
  221. return NULL;
  222. idx = zdev->bars[bar].map_idx;
  223. spin_lock(&zpci_iomap_lock);
  224. if (zpci_iomap_start[idx].count++) {
  225. BUG_ON(zpci_iomap_start[idx].fh != zdev->fh ||
  226. zpci_iomap_start[idx].bar != bar);
  227. } else {
  228. zpci_iomap_start[idx].fh = zdev->fh;
  229. zpci_iomap_start[idx].bar = bar;
  230. }
  231. /* Detect overrun */
  232. BUG_ON(!zpci_iomap_start[idx].count);
  233. spin_unlock(&zpci_iomap_lock);
  234. addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
  235. return (void __iomem *) addr + offset;
  236. }
  237. EXPORT_SYMBOL(pci_iomap_range);
  238. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  239. {
  240. return pci_iomap_range(dev, bar, 0, maxlen);
  241. }
  242. EXPORT_SYMBOL(pci_iomap);
  243. void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
  244. {
  245. unsigned int idx;
  246. idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
  247. spin_lock(&zpci_iomap_lock);
  248. /* Detect underrun */
  249. BUG_ON(!zpci_iomap_start[idx].count);
  250. if (!--zpci_iomap_start[idx].count) {
  251. zpci_iomap_start[idx].fh = 0;
  252. zpci_iomap_start[idx].bar = 0;
  253. }
  254. spin_unlock(&zpci_iomap_lock);
  255. }
  256. EXPORT_SYMBOL(pci_iounmap);
  257. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  258. int size, u32 *val)
  259. {
  260. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  261. int ret;
  262. if (!zdev || devfn != ZPCI_DEVFN)
  263. ret = -ENODEV;
  264. else
  265. ret = zpci_cfg_load(zdev, where, val, size);
  266. return ret;
  267. }
  268. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  269. int size, u32 val)
  270. {
  271. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  272. int ret;
  273. if (!zdev || devfn != ZPCI_DEVFN)
  274. ret = -ENODEV;
  275. else
  276. ret = zpci_cfg_store(zdev, where, val, size);
  277. return ret;
  278. }
  279. static struct pci_ops pci_root_ops = {
  280. .read = pci_read,
  281. .write = pci_write,
  282. };
  283. static void zpci_irq_handler(struct airq_struct *airq)
  284. {
  285. unsigned long si, ai;
  286. struct airq_iv *aibv;
  287. int irqs_on = 0;
  288. inc_irq_stat(IRQIO_PCI);
  289. for (si = 0;;) {
  290. /* Scan adapter summary indicator bit vector */
  291. si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv));
  292. if (si == -1UL) {
  293. if (irqs_on++)
  294. /* End of second scan with interrupts on. */
  295. break;
  296. /* First scan complete, reenable interrupts. */
  297. zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  298. si = 0;
  299. continue;
  300. }
  301. /* Scan the adapter interrupt vector for this device. */
  302. aibv = zpci_aibv[si];
  303. for (ai = 0;;) {
  304. ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv));
  305. if (ai == -1UL)
  306. break;
  307. inc_irq_stat(IRQIO_MSI);
  308. airq_iv_lock(aibv, ai);
  309. generic_handle_irq(airq_iv_get_data(aibv, ai));
  310. airq_iv_unlock(aibv, ai);
  311. }
  312. }
  313. }
  314. int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  315. {
  316. struct zpci_dev *zdev = to_zpci(pdev);
  317. unsigned int hwirq, msi_vecs;
  318. unsigned long aisb;
  319. struct msi_desc *msi;
  320. struct msi_msg msg;
  321. int rc, irq;
  322. if (type == PCI_CAP_ID_MSI && nvec > 1)
  323. return 1;
  324. msi_vecs = min_t(unsigned int, nvec, zdev->max_msi);
  325. /* Allocate adapter summary indicator bit */
  326. rc = -EIO;
  327. aisb = airq_iv_alloc_bit(zpci_aisb_iv);
  328. if (aisb == -1UL)
  329. goto out;
  330. zdev->aisb = aisb;
  331. /* Create adapter interrupt vector */
  332. rc = -ENOMEM;
  333. zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK);
  334. if (!zdev->aibv)
  335. goto out_si;
  336. /* Wire up shortcut pointer */
  337. zpci_aibv[aisb] = zdev->aibv;
  338. /* Request MSI interrupts */
  339. hwirq = 0;
  340. for_each_pci_msi_entry(msi, pdev) {
  341. rc = -EIO;
  342. irq = irq_alloc_desc(0); /* Alloc irq on node 0 */
  343. if (irq < 0)
  344. goto out_msi;
  345. rc = irq_set_msi_desc(irq, msi);
  346. if (rc)
  347. goto out_msi;
  348. irq_set_chip_and_handler(irq, &zpci_irq_chip,
  349. handle_simple_irq);
  350. msg.data = hwirq;
  351. msg.address_lo = zdev->msi_addr & 0xffffffff;
  352. msg.address_hi = zdev->msi_addr >> 32;
  353. pci_write_msi_msg(irq, &msg);
  354. airq_iv_set_data(zdev->aibv, hwirq, irq);
  355. hwirq++;
  356. }
  357. /* Enable adapter interrupts */
  358. rc = zpci_set_airq(zdev);
  359. if (rc)
  360. goto out_msi;
  361. return (msi_vecs == nvec) ? 0 : msi_vecs;
  362. out_msi:
  363. for_each_pci_msi_entry(msi, pdev) {
  364. if (hwirq-- == 0)
  365. break;
  366. irq_set_msi_desc(msi->irq, NULL);
  367. irq_free_desc(msi->irq);
  368. msi->msg.address_lo = 0;
  369. msi->msg.address_hi = 0;
  370. msi->msg.data = 0;
  371. msi->irq = 0;
  372. }
  373. zpci_aibv[aisb] = NULL;
  374. airq_iv_release(zdev->aibv);
  375. out_si:
  376. airq_iv_free_bit(zpci_aisb_iv, aisb);
  377. out:
  378. return rc;
  379. }
  380. void arch_teardown_msi_irqs(struct pci_dev *pdev)
  381. {
  382. struct zpci_dev *zdev = to_zpci(pdev);
  383. struct msi_desc *msi;
  384. int rc;
  385. /* Disable adapter interrupts */
  386. rc = zpci_clear_airq(zdev);
  387. if (rc)
  388. return;
  389. /* Release MSI interrupts */
  390. for_each_pci_msi_entry(msi, pdev) {
  391. if (msi->msi_attrib.is_msix)
  392. __pci_msix_desc_mask_irq(msi, 1);
  393. else
  394. __pci_msi_desc_mask_irq(msi, 1, 1);
  395. irq_set_msi_desc(msi->irq, NULL);
  396. irq_free_desc(msi->irq);
  397. msi->msg.address_lo = 0;
  398. msi->msg.address_hi = 0;
  399. msi->msg.data = 0;
  400. msi->irq = 0;
  401. }
  402. zpci_aibv[zdev->aisb] = NULL;
  403. airq_iv_release(zdev->aibv);
  404. airq_iv_free_bit(zpci_aisb_iv, zdev->aisb);
  405. }
  406. static void zpci_map_resources(struct pci_dev *pdev)
  407. {
  408. resource_size_t len;
  409. int i;
  410. for (i = 0; i < PCI_BAR_COUNT; i++) {
  411. len = pci_resource_len(pdev, i);
  412. if (!len)
  413. continue;
  414. pdev->resource[i].start =
  415. (resource_size_t __force) pci_iomap(pdev, i, 0);
  416. pdev->resource[i].end = pdev->resource[i].start + len - 1;
  417. }
  418. }
  419. static void zpci_unmap_resources(struct pci_dev *pdev)
  420. {
  421. resource_size_t len;
  422. int i;
  423. for (i = 0; i < PCI_BAR_COUNT; i++) {
  424. len = pci_resource_len(pdev, i);
  425. if (!len)
  426. continue;
  427. pci_iounmap(pdev, (void __iomem __force *)
  428. pdev->resource[i].start);
  429. }
  430. }
  431. static int __init zpci_irq_init(void)
  432. {
  433. int rc;
  434. rc = register_adapter_interrupt(&zpci_airq);
  435. if (rc)
  436. goto out;
  437. /* Set summary to 1 to be called every time for the ISC. */
  438. *zpci_airq.lsi_ptr = 1;
  439. rc = -ENOMEM;
  440. zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC);
  441. if (!zpci_aisb_iv)
  442. goto out_airq;
  443. zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  444. return 0;
  445. out_airq:
  446. unregister_adapter_interrupt(&zpci_airq);
  447. out:
  448. return rc;
  449. }
  450. static void zpci_irq_exit(void)
  451. {
  452. airq_iv_release(zpci_aisb_iv);
  453. unregister_adapter_interrupt(&zpci_airq);
  454. }
  455. static int zpci_alloc_iomap(struct zpci_dev *zdev)
  456. {
  457. int entry;
  458. spin_lock(&zpci_iomap_lock);
  459. entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  460. if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
  461. spin_unlock(&zpci_iomap_lock);
  462. return -ENOSPC;
  463. }
  464. set_bit(entry, zpci_iomap);
  465. spin_unlock(&zpci_iomap_lock);
  466. return entry;
  467. }
  468. static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
  469. {
  470. spin_lock(&zpci_iomap_lock);
  471. memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
  472. clear_bit(entry, zpci_iomap);
  473. spin_unlock(&zpci_iomap_lock);
  474. }
  475. static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
  476. unsigned long size, unsigned long flags)
  477. {
  478. struct resource *r;
  479. r = kzalloc(sizeof(*r), GFP_KERNEL);
  480. if (!r)
  481. return NULL;
  482. r->start = start;
  483. r->end = r->start + size - 1;
  484. r->flags = flags;
  485. r->name = zdev->res_name;
  486. if (request_resource(&iomem_resource, r)) {
  487. kfree(r);
  488. return NULL;
  489. }
  490. return r;
  491. }
  492. static int zpci_setup_bus_resources(struct zpci_dev *zdev,
  493. struct list_head *resources)
  494. {
  495. unsigned long addr, size, flags;
  496. struct resource *res;
  497. int i, entry;
  498. snprintf(zdev->res_name, sizeof(zdev->res_name),
  499. "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);
  500. for (i = 0; i < PCI_BAR_COUNT; i++) {
  501. if (!zdev->bars[i].size)
  502. continue;
  503. entry = zpci_alloc_iomap(zdev);
  504. if (entry < 0)
  505. return entry;
  506. zdev->bars[i].map_idx = entry;
  507. /* only MMIO is supported */
  508. flags = IORESOURCE_MEM;
  509. if (zdev->bars[i].val & 8)
  510. flags |= IORESOURCE_PREFETCH;
  511. if (zdev->bars[i].val & 4)
  512. flags |= IORESOURCE_MEM_64;
  513. addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
  514. size = 1UL << zdev->bars[i].size;
  515. res = __alloc_res(zdev, addr, size, flags);
  516. if (!res) {
  517. zpci_free_iomap(zdev, entry);
  518. return -ENOMEM;
  519. }
  520. zdev->bars[i].res = res;
  521. pci_add_resource(resources, res);
  522. }
  523. return 0;
  524. }
  525. static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
  526. {
  527. int i;
  528. for (i = 0; i < PCI_BAR_COUNT; i++) {
  529. if (!zdev->bars[i].size || !zdev->bars[i].res)
  530. continue;
  531. zpci_free_iomap(zdev, zdev->bars[i].map_idx);
  532. release_resource(zdev->bars[i].res);
  533. kfree(zdev->bars[i].res);
  534. }
  535. }
  536. int pcibios_add_device(struct pci_dev *pdev)
  537. {
  538. struct zpci_dev *zdev = to_zpci(pdev);
  539. struct resource *res;
  540. int i;
  541. zdev->pdev = pdev;
  542. pdev->dev.groups = zpci_attr_groups;
  543. zpci_map_resources(pdev);
  544. for (i = 0; i < PCI_BAR_COUNT; i++) {
  545. res = &pdev->resource[i];
  546. if (res->parent || !res->flags)
  547. continue;
  548. pci_claim_resource(pdev, i);
  549. }
  550. return 0;
  551. }
  552. void pcibios_release_device(struct pci_dev *pdev)
  553. {
  554. zpci_unmap_resources(pdev);
  555. }
  556. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  557. {
  558. struct zpci_dev *zdev = to_zpci(pdev);
  559. zdev->pdev = pdev;
  560. zpci_debug_init_device(zdev);
  561. zpci_fmb_enable_device(zdev);
  562. return pci_enable_resources(pdev, mask);
  563. }
  564. void pcibios_disable_device(struct pci_dev *pdev)
  565. {
  566. struct zpci_dev *zdev = to_zpci(pdev);
  567. zpci_fmb_disable_device(zdev);
  568. zpci_debug_exit_device(zdev);
  569. zdev->pdev = NULL;
  570. }
  571. #ifdef CONFIG_HIBERNATE_CALLBACKS
  572. static int zpci_restore(struct device *dev)
  573. {
  574. struct pci_dev *pdev = to_pci_dev(dev);
  575. struct zpci_dev *zdev = to_zpci(pdev);
  576. int ret = 0;
  577. if (zdev->state != ZPCI_FN_STATE_ONLINE)
  578. goto out;
  579. ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  580. if (ret)
  581. goto out;
  582. zpci_map_resources(pdev);
  583. zpci_register_ioat(zdev, 0, zdev->start_dma + PAGE_OFFSET,
  584. zdev->start_dma + zdev->iommu_size - 1,
  585. (u64) zdev->dma_table);
  586. out:
  587. return ret;
  588. }
  589. static int zpci_freeze(struct device *dev)
  590. {
  591. struct pci_dev *pdev = to_pci_dev(dev);
  592. struct zpci_dev *zdev = to_zpci(pdev);
  593. if (zdev->state != ZPCI_FN_STATE_ONLINE)
  594. return 0;
  595. zpci_unregister_ioat(zdev, 0);
  596. zpci_unmap_resources(pdev);
  597. return clp_disable_fh(zdev);
  598. }
  599. struct dev_pm_ops pcibios_pm_ops = {
  600. .thaw_noirq = zpci_restore,
  601. .freeze_noirq = zpci_freeze,
  602. .restore_noirq = zpci_restore,
  603. .poweroff_noirq = zpci_freeze,
  604. };
  605. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  606. static int zpci_alloc_domain(struct zpci_dev *zdev)
  607. {
  608. spin_lock(&zpci_domain_lock);
  609. zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
  610. if (zdev->domain == ZPCI_NR_DEVICES) {
  611. spin_unlock(&zpci_domain_lock);
  612. return -ENOSPC;
  613. }
  614. set_bit(zdev->domain, zpci_domain);
  615. spin_unlock(&zpci_domain_lock);
  616. return 0;
  617. }
  618. static void zpci_free_domain(struct zpci_dev *zdev)
  619. {
  620. spin_lock(&zpci_domain_lock);
  621. clear_bit(zdev->domain, zpci_domain);
  622. spin_unlock(&zpci_domain_lock);
  623. }
  624. void pcibios_remove_bus(struct pci_bus *bus)
  625. {
  626. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  627. zpci_exit_slot(zdev);
  628. zpci_cleanup_bus_resources(zdev);
  629. zpci_free_domain(zdev);
  630. spin_lock(&zpci_list_lock);
  631. list_del(&zdev->entry);
  632. spin_unlock(&zpci_list_lock);
  633. kfree(zdev);
  634. }
  635. static int zpci_scan_bus(struct zpci_dev *zdev)
  636. {
  637. LIST_HEAD(resources);
  638. int ret;
  639. ret = zpci_setup_bus_resources(zdev, &resources);
  640. if (ret)
  641. goto error;
  642. zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
  643. zdev, &resources);
  644. if (!zdev->bus) {
  645. ret = -EIO;
  646. goto error;
  647. }
  648. zdev->bus->max_bus_speed = zdev->max_bus_speed;
  649. pci_bus_add_devices(zdev->bus);
  650. return 0;
  651. error:
  652. zpci_cleanup_bus_resources(zdev);
  653. pci_free_resource_list(&resources);
  654. return ret;
  655. }
  656. int zpci_enable_device(struct zpci_dev *zdev)
  657. {
  658. int rc;
  659. rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  660. if (rc)
  661. goto out;
  662. rc = zpci_dma_init_device(zdev);
  663. if (rc)
  664. goto out_dma;
  665. zdev->state = ZPCI_FN_STATE_ONLINE;
  666. return 0;
  667. out_dma:
  668. clp_disable_fh(zdev);
  669. out:
  670. return rc;
  671. }
  672. EXPORT_SYMBOL_GPL(zpci_enable_device);
  673. int zpci_disable_device(struct zpci_dev *zdev)
  674. {
  675. zpci_dma_exit_device(zdev);
  676. return clp_disable_fh(zdev);
  677. }
  678. EXPORT_SYMBOL_GPL(zpci_disable_device);
  679. int zpci_create_device(struct zpci_dev *zdev)
  680. {
  681. int rc;
  682. rc = zpci_alloc_domain(zdev);
  683. if (rc)
  684. goto out;
  685. mutex_init(&zdev->lock);
  686. if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
  687. rc = zpci_enable_device(zdev);
  688. if (rc)
  689. goto out_free;
  690. }
  691. rc = zpci_scan_bus(zdev);
  692. if (rc)
  693. goto out_disable;
  694. spin_lock(&zpci_list_lock);
  695. list_add_tail(&zdev->entry, &zpci_list);
  696. spin_unlock(&zpci_list_lock);
  697. zpci_init_slot(zdev);
  698. return 0;
  699. out_disable:
  700. if (zdev->state == ZPCI_FN_STATE_ONLINE)
  701. zpci_disable_device(zdev);
  702. out_free:
  703. zpci_free_domain(zdev);
  704. out:
  705. return rc;
  706. }
  707. void zpci_stop_device(struct zpci_dev *zdev)
  708. {
  709. zpci_dma_exit_device(zdev);
  710. /*
  711. * Note: SCLP disables fh via set-pci-fn so don't
  712. * do that here.
  713. */
  714. }
  715. EXPORT_SYMBOL_GPL(zpci_stop_device);
  716. static inline int barsize(u8 size)
  717. {
  718. return (size) ? (1 << size) >> 10 : 0;
  719. }
  720. static int zpci_mem_init(void)
  721. {
  722. zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
  723. 16, 0, NULL);
  724. if (!zdev_fmb_cache)
  725. goto error_zdev;
  726. /* TODO: use realloc */
  727. zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
  728. GFP_KERNEL);
  729. if (!zpci_iomap_start)
  730. goto error_iomap;
  731. return 0;
  732. error_iomap:
  733. kmem_cache_destroy(zdev_fmb_cache);
  734. error_zdev:
  735. return -ENOMEM;
  736. }
  737. static void zpci_mem_exit(void)
  738. {
  739. kfree(zpci_iomap_start);
  740. kmem_cache_destroy(zdev_fmb_cache);
  741. }
  742. static unsigned int s390_pci_probe = 1;
  743. static unsigned int s390_pci_initialized;
  744. char * __init pcibios_setup(char *str)
  745. {
  746. if (!strcmp(str, "off")) {
  747. s390_pci_probe = 0;
  748. return NULL;
  749. }
  750. return str;
  751. }
  752. bool zpci_is_enabled(void)
  753. {
  754. return s390_pci_initialized;
  755. }
  756. static int __init pci_base_init(void)
  757. {
  758. int rc;
  759. if (!s390_pci_probe)
  760. return 0;
  761. if (!test_facility(69) || !test_facility(71) || !test_facility(72))
  762. return 0;
  763. rc = zpci_debug_init();
  764. if (rc)
  765. goto out;
  766. rc = zpci_mem_init();
  767. if (rc)
  768. goto out_mem;
  769. rc = zpci_irq_init();
  770. if (rc)
  771. goto out_irq;
  772. rc = zpci_dma_init();
  773. if (rc)
  774. goto out_dma;
  775. rc = clp_scan_pci_devices();
  776. if (rc)
  777. goto out_find;
  778. s390_pci_initialized = 1;
  779. return 0;
  780. out_find:
  781. zpci_dma_exit();
  782. out_dma:
  783. zpci_irq_exit();
  784. out_irq:
  785. zpci_mem_exit();
  786. out_mem:
  787. zpci_debug_exit();
  788. out:
  789. return rc;
  790. }
  791. subsys_initcall_sync(pci_base_init);
  792. void zpci_rescan(void)
  793. {
  794. if (zpci_is_enabled())
  795. clp_rescan_pci_devices_simple();
  796. }