pci.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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(struct pci_dev *pdev)
  65. {
  66. return (struct zpci_dev *) pdev->sysdata;
  67. }
  68. struct zpci_dev *get_zdev_by_fid(u32 fid)
  69. {
  70. struct zpci_dev *tmp, *zdev = NULL;
  71. spin_lock(&zpci_list_lock);
  72. list_for_each_entry(tmp, &zpci_list, entry) {
  73. if (tmp->fid == fid) {
  74. zdev = tmp;
  75. break;
  76. }
  77. }
  78. spin_unlock(&zpci_list_lock);
  79. return zdev;
  80. }
  81. static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
  82. {
  83. return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
  84. }
  85. int pci_domain_nr(struct pci_bus *bus)
  86. {
  87. return ((struct zpci_dev *) bus->sysdata)->domain;
  88. }
  89. EXPORT_SYMBOL_GPL(pci_domain_nr);
  90. int pci_proc_domain(struct pci_bus *bus)
  91. {
  92. return pci_domain_nr(bus);
  93. }
  94. EXPORT_SYMBOL_GPL(pci_proc_domain);
  95. /* Modify PCI: Register adapter interruptions */
  96. static int zpci_set_airq(struct zpci_dev *zdev)
  97. {
  98. u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
  99. struct zpci_fib fib = {0};
  100. fib.isc = PCI_ISC;
  101. fib.sum = 1; /* enable summary notifications */
  102. fib.noi = airq_iv_end(zdev->aibv);
  103. fib.aibv = (unsigned long) zdev->aibv->vector;
  104. fib.aibvo = 0; /* each zdev has its own interrupt vector */
  105. fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8;
  106. fib.aisbo = zdev->aisb & 63;
  107. return zpci_mod_fc(req, &fib);
  108. }
  109. struct mod_pci_args {
  110. u64 base;
  111. u64 limit;
  112. u64 iota;
  113. u64 fmb_addr;
  114. };
  115. static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
  116. {
  117. u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
  118. struct zpci_fib fib = {0};
  119. fib.pba = args->base;
  120. fib.pal = args->limit;
  121. fib.iota = args->iota;
  122. fib.fmb_addr = args->fmb_addr;
  123. return zpci_mod_fc(req, &fib);
  124. }
  125. /* Modify PCI: Register I/O address translation parameters */
  126. int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
  127. u64 base, u64 limit, u64 iota)
  128. {
  129. struct mod_pci_args args = { base, limit, iota, 0 };
  130. WARN_ON_ONCE(iota & 0x3fff);
  131. args.iota |= ZPCI_IOTA_RTTO_FLAG;
  132. return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
  133. }
  134. /* Modify PCI: Unregister I/O address translation parameters */
  135. int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
  136. {
  137. struct mod_pci_args args = { 0, 0, 0, 0 };
  138. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
  139. }
  140. /* Modify PCI: Unregister adapter interruptions */
  141. static int zpci_clear_airq(struct zpci_dev *zdev)
  142. {
  143. struct mod_pci_args args = { 0, 0, 0, 0 };
  144. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
  145. }
  146. /* Modify PCI: Set PCI function measurement parameters */
  147. int zpci_fmb_enable_device(struct zpci_dev *zdev)
  148. {
  149. struct mod_pci_args args = { 0, 0, 0, 0 };
  150. if (zdev->fmb)
  151. return -EINVAL;
  152. zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
  153. if (!zdev->fmb)
  154. return -ENOMEM;
  155. WARN_ON((u64) zdev->fmb & 0xf);
  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(struct pci_dev *pdev, int bar, unsigned long max)
  213. {
  214. struct zpci_dev *zdev = get_zdev(pdev);
  215. u64 addr;
  216. int idx;
  217. if ((bar & 7) != bar)
  218. return NULL;
  219. idx = zdev->bars[bar].map_idx;
  220. spin_lock(&zpci_iomap_lock);
  221. zpci_iomap_start[idx].fh = zdev->fh;
  222. zpci_iomap_start[idx].bar = bar;
  223. spin_unlock(&zpci_iomap_lock);
  224. addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
  225. return (void __iomem *) addr;
  226. }
  227. EXPORT_SYMBOL_GPL(pci_iomap);
  228. void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
  229. {
  230. unsigned int idx;
  231. idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
  232. spin_lock(&zpci_iomap_lock);
  233. zpci_iomap_start[idx].fh = 0;
  234. zpci_iomap_start[idx].bar = 0;
  235. spin_unlock(&zpci_iomap_lock);
  236. }
  237. EXPORT_SYMBOL_GPL(pci_iounmap);
  238. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  239. int size, u32 *val)
  240. {
  241. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  242. int ret;
  243. if (!zdev || devfn != ZPCI_DEVFN)
  244. ret = -ENODEV;
  245. else
  246. ret = zpci_cfg_load(zdev, where, val, size);
  247. return ret;
  248. }
  249. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  250. int size, u32 val)
  251. {
  252. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  253. int ret;
  254. if (!zdev || devfn != ZPCI_DEVFN)
  255. ret = -ENODEV;
  256. else
  257. ret = zpci_cfg_store(zdev, where, val, size);
  258. return ret;
  259. }
  260. static struct pci_ops pci_root_ops = {
  261. .read = pci_read,
  262. .write = pci_write,
  263. };
  264. static void zpci_irq_handler(struct airq_struct *airq)
  265. {
  266. unsigned long si, ai;
  267. struct airq_iv *aibv;
  268. int irqs_on = 0;
  269. inc_irq_stat(IRQIO_PCI);
  270. for (si = 0;;) {
  271. /* Scan adapter summary indicator bit vector */
  272. si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv));
  273. if (si == -1UL) {
  274. if (irqs_on++)
  275. /* End of second scan with interrupts on. */
  276. break;
  277. /* First scan complete, reenable interrupts. */
  278. zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  279. si = 0;
  280. continue;
  281. }
  282. /* Scan the adapter interrupt vector for this device. */
  283. aibv = zpci_aibv[si];
  284. for (ai = 0;;) {
  285. ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv));
  286. if (ai == -1UL)
  287. break;
  288. inc_irq_stat(IRQIO_MSI);
  289. airq_iv_lock(aibv, ai);
  290. generic_handle_irq(airq_iv_get_data(aibv, ai));
  291. airq_iv_unlock(aibv, ai);
  292. }
  293. }
  294. }
  295. int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  296. {
  297. struct zpci_dev *zdev = get_zdev(pdev);
  298. unsigned int hwirq, msi_vecs;
  299. unsigned long aisb;
  300. struct msi_desc *msi;
  301. struct msi_msg msg;
  302. int rc, irq;
  303. if (type == PCI_CAP_ID_MSI && nvec > 1)
  304. return 1;
  305. msi_vecs = min_t(unsigned int, nvec, zdev->max_msi);
  306. /* Allocate adapter summary indicator bit */
  307. rc = -EIO;
  308. aisb = airq_iv_alloc_bit(zpci_aisb_iv);
  309. if (aisb == -1UL)
  310. goto out;
  311. zdev->aisb = aisb;
  312. /* Create adapter interrupt vector */
  313. rc = -ENOMEM;
  314. zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK);
  315. if (!zdev->aibv)
  316. goto out_si;
  317. /* Wire up shortcut pointer */
  318. zpci_aibv[aisb] = zdev->aibv;
  319. /* Request MSI interrupts */
  320. hwirq = 0;
  321. list_for_each_entry(msi, &pdev->msi_list, list) {
  322. rc = -EIO;
  323. irq = irq_alloc_desc(0); /* Alloc irq on node 0 */
  324. if (irq < 0)
  325. goto out_msi;
  326. rc = irq_set_msi_desc(irq, msi);
  327. if (rc)
  328. goto out_msi;
  329. irq_set_chip_and_handler(irq, &zpci_irq_chip,
  330. handle_simple_irq);
  331. msg.data = hwirq;
  332. msg.address_lo = zdev->msi_addr & 0xffffffff;
  333. msg.address_hi = zdev->msi_addr >> 32;
  334. pci_write_msi_msg(irq, &msg);
  335. airq_iv_set_data(zdev->aibv, hwirq, irq);
  336. hwirq++;
  337. }
  338. /* Enable adapter interrupts */
  339. rc = zpci_set_airq(zdev);
  340. if (rc)
  341. goto out_msi;
  342. return (msi_vecs == nvec) ? 0 : msi_vecs;
  343. out_msi:
  344. list_for_each_entry(msi, &pdev->msi_list, list) {
  345. if (hwirq-- == 0)
  346. break;
  347. irq_set_msi_desc(msi->irq, NULL);
  348. irq_free_desc(msi->irq);
  349. msi->msg.address_lo = 0;
  350. msi->msg.address_hi = 0;
  351. msi->msg.data = 0;
  352. msi->irq = 0;
  353. }
  354. zpci_aibv[aisb] = NULL;
  355. airq_iv_release(zdev->aibv);
  356. out_si:
  357. airq_iv_free_bit(zpci_aisb_iv, aisb);
  358. out:
  359. return rc;
  360. }
  361. void arch_teardown_msi_irqs(struct pci_dev *pdev)
  362. {
  363. struct zpci_dev *zdev = get_zdev(pdev);
  364. struct msi_desc *msi;
  365. int rc;
  366. /* Disable adapter interrupts */
  367. rc = zpci_clear_airq(zdev);
  368. if (rc)
  369. return;
  370. /* Release MSI interrupts */
  371. list_for_each_entry(msi, &pdev->msi_list, list) {
  372. if (msi->msi_attrib.is_msix)
  373. __pci_msix_desc_mask_irq(msi, 1);
  374. else
  375. __pci_msi_desc_mask_irq(msi, 1, 1);
  376. irq_set_msi_desc(msi->irq, NULL);
  377. irq_free_desc(msi->irq);
  378. msi->msg.address_lo = 0;
  379. msi->msg.address_hi = 0;
  380. msi->msg.data = 0;
  381. msi->irq = 0;
  382. }
  383. zpci_aibv[zdev->aisb] = NULL;
  384. airq_iv_release(zdev->aibv);
  385. airq_iv_free_bit(zpci_aisb_iv, zdev->aisb);
  386. }
  387. static void zpci_map_resources(struct zpci_dev *zdev)
  388. {
  389. struct pci_dev *pdev = zdev->pdev;
  390. resource_size_t len;
  391. int i;
  392. for (i = 0; i < PCI_BAR_COUNT; i++) {
  393. len = pci_resource_len(pdev, i);
  394. if (!len)
  395. continue;
  396. pdev->resource[i].start =
  397. (resource_size_t __force) pci_iomap(pdev, i, 0);
  398. pdev->resource[i].end = pdev->resource[i].start + len - 1;
  399. }
  400. }
  401. static void zpci_unmap_resources(struct zpci_dev *zdev)
  402. {
  403. struct pci_dev *pdev = zdev->pdev;
  404. resource_size_t len;
  405. int i;
  406. for (i = 0; i < PCI_BAR_COUNT; i++) {
  407. len = pci_resource_len(pdev, i);
  408. if (!len)
  409. continue;
  410. pci_iounmap(pdev, (void __iomem __force *)
  411. pdev->resource[i].start);
  412. }
  413. }
  414. static int __init zpci_irq_init(void)
  415. {
  416. int rc;
  417. rc = register_adapter_interrupt(&zpci_airq);
  418. if (rc)
  419. goto out;
  420. /* Set summary to 1 to be called every time for the ISC. */
  421. *zpci_airq.lsi_ptr = 1;
  422. rc = -ENOMEM;
  423. zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC);
  424. if (!zpci_aisb_iv)
  425. goto out_airq;
  426. zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  427. return 0;
  428. out_airq:
  429. unregister_adapter_interrupt(&zpci_airq);
  430. out:
  431. return rc;
  432. }
  433. static void zpci_irq_exit(void)
  434. {
  435. airq_iv_release(zpci_aisb_iv);
  436. unregister_adapter_interrupt(&zpci_airq);
  437. }
  438. static int zpci_alloc_iomap(struct zpci_dev *zdev)
  439. {
  440. int entry;
  441. spin_lock(&zpci_iomap_lock);
  442. entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  443. if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
  444. spin_unlock(&zpci_iomap_lock);
  445. return -ENOSPC;
  446. }
  447. set_bit(entry, zpci_iomap);
  448. spin_unlock(&zpci_iomap_lock);
  449. return entry;
  450. }
  451. static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
  452. {
  453. spin_lock(&zpci_iomap_lock);
  454. memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
  455. clear_bit(entry, zpci_iomap);
  456. spin_unlock(&zpci_iomap_lock);
  457. }
  458. static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
  459. unsigned long size, unsigned long flags)
  460. {
  461. struct resource *r;
  462. r = kzalloc(sizeof(*r), GFP_KERNEL);
  463. if (!r)
  464. return NULL;
  465. r->start = start;
  466. r->end = r->start + size - 1;
  467. r->flags = flags;
  468. r->name = zdev->res_name;
  469. if (request_resource(&iomem_resource, r)) {
  470. kfree(r);
  471. return NULL;
  472. }
  473. return r;
  474. }
  475. static int zpci_setup_bus_resources(struct zpci_dev *zdev,
  476. struct list_head *resources)
  477. {
  478. unsigned long addr, size, flags;
  479. struct resource *res;
  480. int i, entry;
  481. snprintf(zdev->res_name, sizeof(zdev->res_name),
  482. "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);
  483. for (i = 0; i < PCI_BAR_COUNT; i++) {
  484. if (!zdev->bars[i].size)
  485. continue;
  486. entry = zpci_alloc_iomap(zdev);
  487. if (entry < 0)
  488. return entry;
  489. zdev->bars[i].map_idx = entry;
  490. /* only MMIO is supported */
  491. flags = IORESOURCE_MEM;
  492. if (zdev->bars[i].val & 8)
  493. flags |= IORESOURCE_PREFETCH;
  494. if (zdev->bars[i].val & 4)
  495. flags |= IORESOURCE_MEM_64;
  496. addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
  497. size = 1UL << zdev->bars[i].size;
  498. res = __alloc_res(zdev, addr, size, flags);
  499. if (!res) {
  500. zpci_free_iomap(zdev, entry);
  501. return -ENOMEM;
  502. }
  503. zdev->bars[i].res = res;
  504. pci_add_resource(resources, res);
  505. }
  506. return 0;
  507. }
  508. static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
  509. {
  510. int i;
  511. for (i = 0; i < PCI_BAR_COUNT; i++) {
  512. if (!zdev->bars[i].size)
  513. continue;
  514. zpci_free_iomap(zdev, zdev->bars[i].map_idx);
  515. release_resource(zdev->bars[i].res);
  516. kfree(zdev->bars[i].res);
  517. }
  518. }
  519. int pcibios_add_device(struct pci_dev *pdev)
  520. {
  521. struct zpci_dev *zdev = get_zdev(pdev);
  522. struct resource *res;
  523. int i;
  524. zdev->pdev = pdev;
  525. pdev->dev.groups = zpci_attr_groups;
  526. zpci_map_resources(zdev);
  527. for (i = 0; i < PCI_BAR_COUNT; i++) {
  528. res = &pdev->resource[i];
  529. if (res->parent || !res->flags)
  530. continue;
  531. pci_claim_resource(pdev, i);
  532. }
  533. return 0;
  534. }
  535. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  536. {
  537. struct zpci_dev *zdev = get_zdev(pdev);
  538. zdev->pdev = pdev;
  539. zpci_debug_init_device(zdev);
  540. zpci_fmb_enable_device(zdev);
  541. zpci_map_resources(zdev);
  542. return pci_enable_resources(pdev, mask);
  543. }
  544. void pcibios_disable_device(struct pci_dev *pdev)
  545. {
  546. struct zpci_dev *zdev = get_zdev(pdev);
  547. zpci_unmap_resources(zdev);
  548. zpci_fmb_disable_device(zdev);
  549. zpci_debug_exit_device(zdev);
  550. zdev->pdev = NULL;
  551. }
  552. #ifdef CONFIG_HIBERNATE_CALLBACKS
  553. static int zpci_restore(struct device *dev)
  554. {
  555. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  556. int ret = 0;
  557. if (zdev->state != ZPCI_FN_STATE_ONLINE)
  558. goto out;
  559. ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  560. if (ret)
  561. goto out;
  562. zpci_map_resources(zdev);
  563. zpci_register_ioat(zdev, 0, zdev->start_dma + PAGE_OFFSET,
  564. zdev->start_dma + zdev->iommu_size - 1,
  565. (u64) zdev->dma_table);
  566. out:
  567. return ret;
  568. }
  569. static int zpci_freeze(struct device *dev)
  570. {
  571. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  572. if (zdev->state != ZPCI_FN_STATE_ONLINE)
  573. return 0;
  574. zpci_unregister_ioat(zdev, 0);
  575. return clp_disable_fh(zdev);
  576. }
  577. struct dev_pm_ops pcibios_pm_ops = {
  578. .thaw_noirq = zpci_restore,
  579. .freeze_noirq = zpci_freeze,
  580. .restore_noirq = zpci_restore,
  581. .poweroff_noirq = zpci_freeze,
  582. };
  583. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  584. static int zpci_alloc_domain(struct zpci_dev *zdev)
  585. {
  586. spin_lock(&zpci_domain_lock);
  587. zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
  588. if (zdev->domain == ZPCI_NR_DEVICES) {
  589. spin_unlock(&zpci_domain_lock);
  590. return -ENOSPC;
  591. }
  592. set_bit(zdev->domain, zpci_domain);
  593. spin_unlock(&zpci_domain_lock);
  594. return 0;
  595. }
  596. static void zpci_free_domain(struct zpci_dev *zdev)
  597. {
  598. spin_lock(&zpci_domain_lock);
  599. clear_bit(zdev->domain, zpci_domain);
  600. spin_unlock(&zpci_domain_lock);
  601. }
  602. void pcibios_remove_bus(struct pci_bus *bus)
  603. {
  604. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  605. zpci_exit_slot(zdev);
  606. zpci_cleanup_bus_resources(zdev);
  607. zpci_free_domain(zdev);
  608. spin_lock(&zpci_list_lock);
  609. list_del(&zdev->entry);
  610. spin_unlock(&zpci_list_lock);
  611. kfree(zdev);
  612. }
  613. static int zpci_scan_bus(struct zpci_dev *zdev)
  614. {
  615. LIST_HEAD(resources);
  616. int ret;
  617. ret = zpci_setup_bus_resources(zdev, &resources);
  618. if (ret)
  619. return ret;
  620. zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
  621. zdev, &resources);
  622. if (!zdev->bus) {
  623. zpci_cleanup_bus_resources(zdev);
  624. return -EIO;
  625. }
  626. zdev->bus->max_bus_speed = zdev->max_bus_speed;
  627. return 0;
  628. }
  629. int zpci_enable_device(struct zpci_dev *zdev)
  630. {
  631. int rc;
  632. rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  633. if (rc)
  634. goto out;
  635. rc = zpci_dma_init_device(zdev);
  636. if (rc)
  637. goto out_dma;
  638. zdev->state = ZPCI_FN_STATE_ONLINE;
  639. return 0;
  640. out_dma:
  641. clp_disable_fh(zdev);
  642. out:
  643. return rc;
  644. }
  645. EXPORT_SYMBOL_GPL(zpci_enable_device);
  646. int zpci_disable_device(struct zpci_dev *zdev)
  647. {
  648. zpci_dma_exit_device(zdev);
  649. return clp_disable_fh(zdev);
  650. }
  651. EXPORT_SYMBOL_GPL(zpci_disable_device);
  652. int zpci_create_device(struct zpci_dev *zdev)
  653. {
  654. int rc;
  655. rc = zpci_alloc_domain(zdev);
  656. if (rc)
  657. goto out;
  658. if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
  659. rc = zpci_enable_device(zdev);
  660. if (rc)
  661. goto out_free;
  662. }
  663. rc = zpci_scan_bus(zdev);
  664. if (rc)
  665. goto out_disable;
  666. spin_lock(&zpci_list_lock);
  667. list_add_tail(&zdev->entry, &zpci_list);
  668. spin_unlock(&zpci_list_lock);
  669. zpci_init_slot(zdev);
  670. return 0;
  671. out_disable:
  672. if (zdev->state == ZPCI_FN_STATE_ONLINE)
  673. zpci_disable_device(zdev);
  674. out_free:
  675. zpci_free_domain(zdev);
  676. out:
  677. return rc;
  678. }
  679. void zpci_stop_device(struct zpci_dev *zdev)
  680. {
  681. zpci_dma_exit_device(zdev);
  682. /*
  683. * Note: SCLP disables fh via set-pci-fn so don't
  684. * do that here.
  685. */
  686. }
  687. EXPORT_SYMBOL_GPL(zpci_stop_device);
  688. static inline int barsize(u8 size)
  689. {
  690. return (size) ? (1 << size) >> 10 : 0;
  691. }
  692. static int zpci_mem_init(void)
  693. {
  694. zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
  695. 16, 0, NULL);
  696. if (!zdev_fmb_cache)
  697. goto error_zdev;
  698. /* TODO: use realloc */
  699. zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
  700. GFP_KERNEL);
  701. if (!zpci_iomap_start)
  702. goto error_iomap;
  703. return 0;
  704. error_iomap:
  705. kmem_cache_destroy(zdev_fmb_cache);
  706. error_zdev:
  707. return -ENOMEM;
  708. }
  709. static void zpci_mem_exit(void)
  710. {
  711. kfree(zpci_iomap_start);
  712. kmem_cache_destroy(zdev_fmb_cache);
  713. }
  714. static unsigned int s390_pci_probe = 1;
  715. static unsigned int s390_pci_initialized;
  716. char * __init pcibios_setup(char *str)
  717. {
  718. if (!strcmp(str, "off")) {
  719. s390_pci_probe = 0;
  720. return NULL;
  721. }
  722. return str;
  723. }
  724. bool zpci_is_enabled(void)
  725. {
  726. return s390_pci_initialized;
  727. }
  728. static int __init pci_base_init(void)
  729. {
  730. int rc;
  731. if (!s390_pci_probe)
  732. return 0;
  733. if (!test_facility(2) || !test_facility(69)
  734. || !test_facility(71) || !test_facility(72))
  735. return 0;
  736. rc = zpci_debug_init();
  737. if (rc)
  738. goto out;
  739. rc = zpci_mem_init();
  740. if (rc)
  741. goto out_mem;
  742. rc = zpci_irq_init();
  743. if (rc)
  744. goto out_irq;
  745. rc = zpci_dma_init();
  746. if (rc)
  747. goto out_dma;
  748. rc = clp_scan_pci_devices();
  749. if (rc)
  750. goto out_find;
  751. s390_pci_initialized = 1;
  752. return 0;
  753. out_find:
  754. zpci_dma_exit();
  755. out_dma:
  756. zpci_irq_exit();
  757. out_irq:
  758. zpci_mem_exit();
  759. out_mem:
  760. zpci_debug_exit();
  761. out:
  762. return rc;
  763. }
  764. subsys_initcall_sync(pci_base_init);
  765. void zpci_rescan(void)
  766. {
  767. if (zpci_is_enabled())
  768. clp_rescan_pci_devices_simple();
  769. }